message.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * Copyright (c) 2006, 2020 Oracle and/or its affiliates.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/list.h>
  38. #include <linux/errqueue.h>
  39. #include "rds.h"
  40. static unsigned int rds_exthdr_size[__RDS_EXTHDR_MAX] = {
  41. [RDS_EXTHDR_NONE] = 0,
  42. [RDS_EXTHDR_VERSION] = sizeof(struct rds_ext_header_version),
  43. [RDS_EXTHDR_RDMA] = sizeof(struct rds_ext_header_rdma),
  44. [RDS_EXTHDR_RDMA_DEST] = sizeof(struct rds_ext_header_rdma_dest),
  45. [RDS_EXTHDR_RDMA_BYTES] = sizeof(struct rds_ext_header_rdma_bytes),
  46. [RDS_EXTHDR_NPATHS] = sizeof(__be16),
  47. [RDS_EXTHDR_GEN_NUM] = sizeof(__be32),
  48. [RDS_EXTHDR_SPORT_IDX] = 1,
  49. };
  50. void rds_message_addref(struct rds_message *rm)
  51. {
  52. rdsdebug("addref rm %p ref %d\n", rm, refcount_read(&rm->m_refcount));
  53. refcount_inc(&rm->m_refcount);
  54. }
  55. EXPORT_SYMBOL_GPL(rds_message_addref);
  56. static inline bool rds_zcookie_add(struct rds_msg_zcopy_info *info, u32 cookie)
  57. {
  58. struct rds_zcopy_cookies *ck = &info->zcookies;
  59. int ncookies = ck->num;
  60. if (ncookies == RDS_MAX_ZCOOKIES)
  61. return false;
  62. ck->cookies[ncookies] = cookie;
  63. ck->num = ++ncookies;
  64. return true;
  65. }
  66. static struct rds_msg_zcopy_info *rds_info_from_znotifier(struct rds_znotifier *znotif)
  67. {
  68. return container_of(znotif, struct rds_msg_zcopy_info, znotif);
  69. }
  70. void rds_notify_msg_zcopy_purge(struct rds_msg_zcopy_queue *q)
  71. {
  72. unsigned long flags;
  73. LIST_HEAD(copy);
  74. struct rds_msg_zcopy_info *info, *tmp;
  75. spin_lock_irqsave(&q->lock, flags);
  76. list_splice(&q->zcookie_head, &copy);
  77. INIT_LIST_HEAD(&q->zcookie_head);
  78. spin_unlock_irqrestore(&q->lock, flags);
  79. list_for_each_entry_safe(info, tmp, &copy, rs_zcookie_next) {
  80. list_del(&info->rs_zcookie_next);
  81. kfree(info);
  82. }
  83. }
  84. static void rds_rm_zerocopy_callback(struct rds_sock *rs,
  85. struct rds_znotifier *znotif)
  86. {
  87. struct rds_msg_zcopy_info *info;
  88. struct rds_msg_zcopy_queue *q;
  89. u32 cookie = znotif->z_cookie;
  90. struct rds_zcopy_cookies *ck;
  91. struct list_head *head;
  92. unsigned long flags;
  93. mm_unaccount_pinned_pages(&znotif->z_mmp);
  94. q = &rs->rs_zcookie_queue;
  95. spin_lock_irqsave(&q->lock, flags);
  96. head = &q->zcookie_head;
  97. if (!list_empty(head)) {
  98. info = list_first_entry(head, struct rds_msg_zcopy_info,
  99. rs_zcookie_next);
  100. if (rds_zcookie_add(info, cookie)) {
  101. spin_unlock_irqrestore(&q->lock, flags);
  102. kfree(rds_info_from_znotifier(znotif));
  103. /* caller invokes rds_wake_sk_sleep() */
  104. return;
  105. }
  106. }
  107. info = rds_info_from_znotifier(znotif);
  108. ck = &info->zcookies;
  109. memset(ck, 0, sizeof(*ck));
  110. WARN_ON(!rds_zcookie_add(info, cookie));
  111. list_add_tail(&info->rs_zcookie_next, &q->zcookie_head);
  112. spin_unlock_irqrestore(&q->lock, flags);
  113. /* caller invokes rds_wake_sk_sleep() */
  114. }
  115. /*
  116. * This relies on dma_map_sg() not touching sg[].page during merging.
  117. */
  118. static void rds_message_purge(struct rds_message *rm)
  119. {
  120. unsigned long i, flags;
  121. bool zcopy = false;
  122. if (unlikely(test_bit(RDS_MSG_PAGEVEC, &rm->m_flags)))
  123. return;
  124. spin_lock_irqsave(&rm->m_rs_lock, flags);
  125. if (rm->m_rs) {
  126. struct rds_sock *rs = rm->m_rs;
  127. if (rm->data.op_mmp_znotifier) {
  128. zcopy = true;
  129. rds_rm_zerocopy_callback(rs, rm->data.op_mmp_znotifier);
  130. rds_wake_sk_sleep(rs);
  131. rm->data.op_mmp_znotifier = NULL;
  132. }
  133. sock_put(rds_rs_to_sk(rs));
  134. rm->m_rs = NULL;
  135. }
  136. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  137. for (i = 0; i < rm->data.op_nents; i++) {
  138. /* XXX will have to put_page for page refs */
  139. if (!zcopy)
  140. __free_page(sg_page(&rm->data.op_sg[i]));
  141. else
  142. put_page(sg_page(&rm->data.op_sg[i]));
  143. }
  144. rm->data.op_nents = 0;
  145. if (rm->rdma.op_active)
  146. rds_rdma_free_op(&rm->rdma);
  147. if (rm->rdma.op_rdma_mr)
  148. kref_put(&rm->rdma.op_rdma_mr->r_kref, __rds_put_mr_final);
  149. if (rm->atomic.op_active)
  150. rds_atomic_free_op(&rm->atomic);
  151. if (rm->atomic.op_rdma_mr)
  152. kref_put(&rm->atomic.op_rdma_mr->r_kref, __rds_put_mr_final);
  153. }
  154. void rds_message_put(struct rds_message *rm)
  155. {
  156. rdsdebug("put rm %p ref %d\n", rm, refcount_read(&rm->m_refcount));
  157. WARN(!refcount_read(&rm->m_refcount), "danger refcount zero on %p\n", rm);
  158. if (refcount_dec_and_test(&rm->m_refcount)) {
  159. BUG_ON(!list_empty(&rm->m_sock_item));
  160. BUG_ON(!list_empty(&rm->m_conn_item));
  161. rds_message_purge(rm);
  162. kfree(rm);
  163. }
  164. }
  165. EXPORT_SYMBOL_GPL(rds_message_put);
  166. void rds_message_populate_header(struct rds_header *hdr, __be16 sport,
  167. __be16 dport, u64 seq)
  168. {
  169. hdr->h_flags = 0;
  170. hdr->h_sport = sport;
  171. hdr->h_dport = dport;
  172. hdr->h_sequence = cpu_to_be64(seq);
  173. /* see rds_find_next_ext_space for reason why we memset the
  174. * ext header
  175. */
  176. memset(hdr->h_exthdr, RDS_EXTHDR_NONE, RDS_HEADER_EXT_SPACE);
  177. }
  178. EXPORT_SYMBOL_GPL(rds_message_populate_header);
  179. /*
  180. * Find the next place we can add an RDS header extension with
  181. * specific length. Extension headers are pushed one after the
  182. * other. In the following, the number after the colon is the number
  183. * of bytes:
  184. *
  185. * [ type1:1 dta1:len1 [ type2:1 dta2:len2 ] ... ] RDS_EXTHDR_NONE
  186. *
  187. * If the extension headers fill the complete extension header space
  188. * (16 bytes), the trailing RDS_EXTHDR_NONE is omitted.
  189. */
  190. static int rds_find_next_ext_space(struct rds_header *hdr, unsigned int len,
  191. u8 **ext_start)
  192. {
  193. unsigned int ext_len;
  194. unsigned int type;
  195. int ind = 0;
  196. while ((ind + 1 + len) <= RDS_HEADER_EXT_SPACE) {
  197. if (hdr->h_exthdr[ind] == RDS_EXTHDR_NONE) {
  198. *ext_start = hdr->h_exthdr + ind;
  199. return 0;
  200. }
  201. type = hdr->h_exthdr[ind];
  202. ext_len = (type < __RDS_EXTHDR_MAX) ? rds_exthdr_size[type] : 0;
  203. WARN_ONCE(!ext_len, "Unknown ext hdr type %d\n", type);
  204. if (!ext_len)
  205. return -EINVAL;
  206. /* ind points to a valid ext hdr with known length */
  207. ind += 1 + ext_len;
  208. }
  209. /* no room for extension */
  210. return -ENOSPC;
  211. }
  212. /* The ext hdr space is prefilled with zero from the kzalloc() */
  213. int rds_message_add_extension(struct rds_header *hdr,
  214. unsigned int type, const void *data)
  215. {
  216. unsigned char *dst;
  217. unsigned int len;
  218. len = (type < __RDS_EXTHDR_MAX) ? rds_exthdr_size[type] : 0;
  219. if (!len)
  220. return 0;
  221. if (rds_find_next_ext_space(hdr, len, &dst))
  222. return 0;
  223. *dst++ = type;
  224. memcpy(dst, data, len);
  225. return 1;
  226. }
  227. EXPORT_SYMBOL_GPL(rds_message_add_extension);
  228. /*
  229. * If a message has extension headers, retrieve them here.
  230. * Call like this:
  231. *
  232. * unsigned int pos = 0;
  233. *
  234. * while (1) {
  235. * buflen = sizeof(buffer);
  236. * type = rds_message_next_extension(hdr, &pos, buffer, &buflen);
  237. * if (type == RDS_EXTHDR_NONE)
  238. * break;
  239. * ...
  240. * }
  241. */
  242. int rds_message_next_extension(struct rds_header *hdr,
  243. unsigned int *pos, void *buf, unsigned int *buflen)
  244. {
  245. unsigned int offset, ext_type, ext_len;
  246. u8 *src = hdr->h_exthdr;
  247. offset = *pos;
  248. if (offset >= RDS_HEADER_EXT_SPACE)
  249. goto none;
  250. /* Get the extension type and length. For now, the
  251. * length is implied by the extension type. */
  252. ext_type = src[offset++];
  253. if (ext_type == RDS_EXTHDR_NONE || ext_type >= __RDS_EXTHDR_MAX)
  254. goto none;
  255. ext_len = rds_exthdr_size[ext_type];
  256. if (offset + ext_len > RDS_HEADER_EXT_SPACE)
  257. goto none;
  258. *pos = offset + ext_len;
  259. if (ext_len < *buflen)
  260. *buflen = ext_len;
  261. memcpy(buf, src + offset, *buflen);
  262. return ext_type;
  263. none:
  264. *pos = RDS_HEADER_EXT_SPACE;
  265. *buflen = 0;
  266. return RDS_EXTHDR_NONE;
  267. }
  268. int rds_message_add_rdma_dest_extension(struct rds_header *hdr, u32 r_key, u32 offset)
  269. {
  270. struct rds_ext_header_rdma_dest ext_hdr;
  271. ext_hdr.h_rdma_rkey = cpu_to_be32(r_key);
  272. ext_hdr.h_rdma_offset = cpu_to_be32(offset);
  273. return rds_message_add_extension(hdr, RDS_EXTHDR_RDMA_DEST, &ext_hdr);
  274. }
  275. EXPORT_SYMBOL_GPL(rds_message_add_rdma_dest_extension);
  276. /*
  277. * Each rds_message is allocated with extra space for the scatterlist entries
  278. * rds ops will need. This is to minimize memory allocation count. Then, each rds op
  279. * can grab SGs when initializing its part of the rds_message.
  280. */
  281. struct rds_message *rds_message_alloc(unsigned int extra_len, gfp_t gfp)
  282. {
  283. struct rds_message *rm;
  284. if (extra_len > KMALLOC_MAX_SIZE - sizeof(struct rds_message))
  285. return NULL;
  286. rm = kzalloc(sizeof(struct rds_message) + extra_len, gfp);
  287. if (!rm)
  288. goto out;
  289. rm->m_used_sgs = 0;
  290. rm->m_total_sgs = extra_len / sizeof(struct scatterlist);
  291. refcount_set(&rm->m_refcount, 1);
  292. INIT_LIST_HEAD(&rm->m_sock_item);
  293. INIT_LIST_HEAD(&rm->m_conn_item);
  294. spin_lock_init(&rm->m_rs_lock);
  295. init_waitqueue_head(&rm->m_flush_wait);
  296. out:
  297. return rm;
  298. }
  299. /*
  300. * RDS ops use this to grab SG entries from the rm's sg pool.
  301. */
  302. struct scatterlist *rds_message_alloc_sgs(struct rds_message *rm, int nents)
  303. {
  304. struct scatterlist *sg_first = (struct scatterlist *) &rm[1];
  305. struct scatterlist *sg_ret;
  306. if (nents <= 0) {
  307. pr_warn("rds: alloc sgs failed! nents <= 0\n");
  308. return ERR_PTR(-EINVAL);
  309. }
  310. if (rm->m_used_sgs + nents > rm->m_total_sgs) {
  311. pr_warn("rds: alloc sgs failed! total %d used %d nents %d\n",
  312. rm->m_total_sgs, rm->m_used_sgs, nents);
  313. return ERR_PTR(-ENOMEM);
  314. }
  315. sg_ret = &sg_first[rm->m_used_sgs];
  316. sg_init_table(sg_ret, nents);
  317. rm->m_used_sgs += nents;
  318. return sg_ret;
  319. }
  320. struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len)
  321. {
  322. struct rds_message *rm;
  323. unsigned int i;
  324. int num_sgs = DIV_ROUND_UP(total_len, PAGE_SIZE);
  325. int extra_bytes = num_sgs * sizeof(struct scatterlist);
  326. rm = rds_message_alloc(extra_bytes, GFP_NOWAIT);
  327. if (!rm)
  328. return ERR_PTR(-ENOMEM);
  329. set_bit(RDS_MSG_PAGEVEC, &rm->m_flags);
  330. rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len);
  331. rm->data.op_nents = DIV_ROUND_UP(total_len, PAGE_SIZE);
  332. rm->data.op_sg = rds_message_alloc_sgs(rm, num_sgs);
  333. if (IS_ERR(rm->data.op_sg)) {
  334. void *err = ERR_CAST(rm->data.op_sg);
  335. rds_message_put(rm);
  336. return err;
  337. }
  338. for (i = 0; i < rm->data.op_nents; ++i) {
  339. sg_set_page(&rm->data.op_sg[i],
  340. virt_to_page((void *)page_addrs[i]),
  341. PAGE_SIZE, 0);
  342. }
  343. return rm;
  344. }
  345. static int rds_message_zcopy_from_user(struct rds_message *rm, struct iov_iter *from)
  346. {
  347. struct scatterlist *sg;
  348. int ret = 0;
  349. int length = iov_iter_count(from);
  350. struct rds_msg_zcopy_info *info;
  351. rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from));
  352. /*
  353. * now allocate and copy in the data payload.
  354. */
  355. sg = rm->data.op_sg;
  356. info = kzalloc_obj(*info);
  357. if (!info)
  358. return -ENOMEM;
  359. INIT_LIST_HEAD(&info->rs_zcookie_next);
  360. rm->data.op_mmp_znotifier = &info->znotif;
  361. if (mm_account_pinned_pages(&rm->data.op_mmp_znotifier->z_mmp,
  362. length)) {
  363. ret = -ENOMEM;
  364. goto err;
  365. }
  366. while (iov_iter_count(from)) {
  367. struct page *pages;
  368. size_t start;
  369. ssize_t copied;
  370. copied = iov_iter_get_pages2(from, &pages, PAGE_SIZE,
  371. 1, &start);
  372. if (copied < 0) {
  373. struct mmpin *mmp;
  374. int i;
  375. for (i = 0; i < rm->data.op_nents; i++)
  376. put_page(sg_page(&rm->data.op_sg[i]));
  377. mmp = &rm->data.op_mmp_znotifier->z_mmp;
  378. mm_unaccount_pinned_pages(mmp);
  379. ret = -EFAULT;
  380. goto err;
  381. }
  382. length -= copied;
  383. sg_set_page(sg, pages, copied, start);
  384. rm->data.op_nents++;
  385. sg++;
  386. }
  387. WARN_ON_ONCE(length != 0);
  388. return ret;
  389. err:
  390. kfree(info);
  391. rm->data.op_mmp_znotifier = NULL;
  392. return ret;
  393. }
  394. int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from,
  395. bool zcopy)
  396. {
  397. unsigned long to_copy, nbytes;
  398. unsigned long sg_off;
  399. struct scatterlist *sg;
  400. int ret = 0;
  401. rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from));
  402. /* now allocate and copy in the data payload. */
  403. sg = rm->data.op_sg;
  404. sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */
  405. if (zcopy)
  406. return rds_message_zcopy_from_user(rm, from);
  407. while (iov_iter_count(from)) {
  408. if (!sg_page(sg)) {
  409. ret = rds_page_remainder_alloc(sg, iov_iter_count(from),
  410. GFP_HIGHUSER);
  411. if (ret)
  412. return ret;
  413. rm->data.op_nents++;
  414. sg_off = 0;
  415. }
  416. to_copy = min_t(unsigned long, iov_iter_count(from),
  417. sg->length - sg_off);
  418. rds_stats_add(s_copy_from_user, to_copy);
  419. nbytes = copy_page_from_iter(sg_page(sg), sg->offset + sg_off,
  420. to_copy, from);
  421. if (nbytes != to_copy)
  422. return -EFAULT;
  423. sg_off += to_copy;
  424. if (sg_off == sg->length)
  425. sg++;
  426. }
  427. return ret;
  428. }
  429. int rds_message_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to)
  430. {
  431. struct rds_message *rm;
  432. struct scatterlist *sg;
  433. unsigned long to_copy;
  434. unsigned long vec_off;
  435. int copied;
  436. int ret;
  437. u32 len;
  438. rm = container_of(inc, struct rds_message, m_inc);
  439. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  440. sg = rm->data.op_sg;
  441. vec_off = 0;
  442. copied = 0;
  443. while (iov_iter_count(to) && copied < len) {
  444. to_copy = min_t(unsigned long, iov_iter_count(to),
  445. sg->length - vec_off);
  446. to_copy = min_t(unsigned long, to_copy, len - copied);
  447. rds_stats_add(s_copy_to_user, to_copy);
  448. ret = copy_page_to_iter(sg_page(sg), sg->offset + vec_off,
  449. to_copy, to);
  450. if (ret != to_copy)
  451. return -EFAULT;
  452. vec_off += to_copy;
  453. copied += to_copy;
  454. if (vec_off == sg->length) {
  455. vec_off = 0;
  456. sg++;
  457. }
  458. }
  459. return copied;
  460. }
  461. /*
  462. * If the message is still on the send queue, wait until the transport
  463. * is done with it. This is particularly important for RDMA operations.
  464. */
  465. void rds_message_wait(struct rds_message *rm)
  466. {
  467. wait_event_interruptible(rm->m_flush_wait,
  468. !test_bit(RDS_MSG_MAPPED, &rm->m_flags));
  469. }
  470. void rds_message_unmapped(struct rds_message *rm)
  471. {
  472. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  473. wake_up_interruptible(&rm->m_flush_wait);
  474. }
  475. EXPORT_SYMBOL_GPL(rds_message_unmapped);