message.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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_NPATHS] = sizeof(u16),
  46. [RDS_EXTHDR_GEN_NUM] = sizeof(u32),
  47. };
  48. void rds_message_addref(struct rds_message *rm)
  49. {
  50. rdsdebug("addref rm %p ref %d\n", rm, refcount_read(&rm->m_refcount));
  51. refcount_inc(&rm->m_refcount);
  52. }
  53. EXPORT_SYMBOL_GPL(rds_message_addref);
  54. static inline bool rds_zcookie_add(struct rds_msg_zcopy_info *info, u32 cookie)
  55. {
  56. struct rds_zcopy_cookies *ck = &info->zcookies;
  57. int ncookies = ck->num;
  58. if (ncookies == RDS_MAX_ZCOOKIES)
  59. return false;
  60. ck->cookies[ncookies] = cookie;
  61. ck->num = ++ncookies;
  62. return true;
  63. }
  64. static struct rds_msg_zcopy_info *rds_info_from_znotifier(struct rds_znotifier *znotif)
  65. {
  66. return container_of(znotif, struct rds_msg_zcopy_info, znotif);
  67. }
  68. void rds_notify_msg_zcopy_purge(struct rds_msg_zcopy_queue *q)
  69. {
  70. unsigned long flags;
  71. LIST_HEAD(copy);
  72. struct rds_msg_zcopy_info *info, *tmp;
  73. spin_lock_irqsave(&q->lock, flags);
  74. list_splice(&q->zcookie_head, &copy);
  75. INIT_LIST_HEAD(&q->zcookie_head);
  76. spin_unlock_irqrestore(&q->lock, flags);
  77. list_for_each_entry_safe(info, tmp, &copy, rs_zcookie_next) {
  78. list_del(&info->rs_zcookie_next);
  79. kfree(info);
  80. }
  81. }
  82. static void rds_rm_zerocopy_callback(struct rds_sock *rs,
  83. struct rds_znotifier *znotif)
  84. {
  85. struct rds_msg_zcopy_info *info;
  86. struct rds_msg_zcopy_queue *q;
  87. u32 cookie = znotif->z_cookie;
  88. struct rds_zcopy_cookies *ck;
  89. struct list_head *head;
  90. unsigned long flags;
  91. mm_unaccount_pinned_pages(&znotif->z_mmp);
  92. q = &rs->rs_zcookie_queue;
  93. spin_lock_irqsave(&q->lock, flags);
  94. head = &q->zcookie_head;
  95. if (!list_empty(head)) {
  96. info = list_first_entry(head, struct rds_msg_zcopy_info,
  97. rs_zcookie_next);
  98. if (rds_zcookie_add(info, cookie)) {
  99. spin_unlock_irqrestore(&q->lock, flags);
  100. kfree(rds_info_from_znotifier(znotif));
  101. /* caller invokes rds_wake_sk_sleep() */
  102. return;
  103. }
  104. }
  105. info = rds_info_from_znotifier(znotif);
  106. ck = &info->zcookies;
  107. memset(ck, 0, sizeof(*ck));
  108. WARN_ON(!rds_zcookie_add(info, cookie));
  109. list_add_tail(&info->rs_zcookie_next, &q->zcookie_head);
  110. spin_unlock_irqrestore(&q->lock, flags);
  111. /* caller invokes rds_wake_sk_sleep() */
  112. }
  113. /*
  114. * This relies on dma_map_sg() not touching sg[].page during merging.
  115. */
  116. static void rds_message_purge(struct rds_message *rm)
  117. {
  118. unsigned long i, flags;
  119. bool zcopy = false;
  120. if (unlikely(test_bit(RDS_MSG_PAGEVEC, &rm->m_flags)))
  121. return;
  122. spin_lock_irqsave(&rm->m_rs_lock, flags);
  123. if (rm->m_rs) {
  124. struct rds_sock *rs = rm->m_rs;
  125. if (rm->data.op_mmp_znotifier) {
  126. zcopy = true;
  127. rds_rm_zerocopy_callback(rs, rm->data.op_mmp_znotifier);
  128. rds_wake_sk_sleep(rs);
  129. rm->data.op_mmp_znotifier = NULL;
  130. }
  131. sock_put(rds_rs_to_sk(rs));
  132. rm->m_rs = NULL;
  133. }
  134. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  135. for (i = 0; i < rm->data.op_nents; i++) {
  136. /* XXX will have to put_page for page refs */
  137. if (!zcopy)
  138. __free_page(sg_page(&rm->data.op_sg[i]));
  139. else
  140. put_page(sg_page(&rm->data.op_sg[i]));
  141. }
  142. rm->data.op_nents = 0;
  143. if (rm->rdma.op_active)
  144. rds_rdma_free_op(&rm->rdma);
  145. if (rm->rdma.op_rdma_mr)
  146. kref_put(&rm->rdma.op_rdma_mr->r_kref, __rds_put_mr_final);
  147. if (rm->atomic.op_active)
  148. rds_atomic_free_op(&rm->atomic);
  149. if (rm->atomic.op_rdma_mr)
  150. kref_put(&rm->atomic.op_rdma_mr->r_kref, __rds_put_mr_final);
  151. }
  152. void rds_message_put(struct rds_message *rm)
  153. {
  154. rdsdebug("put rm %p ref %d\n", rm, refcount_read(&rm->m_refcount));
  155. WARN(!refcount_read(&rm->m_refcount), "danger refcount zero on %p\n", rm);
  156. if (refcount_dec_and_test(&rm->m_refcount)) {
  157. BUG_ON(!list_empty(&rm->m_sock_item));
  158. BUG_ON(!list_empty(&rm->m_conn_item));
  159. rds_message_purge(rm);
  160. kfree(rm);
  161. }
  162. }
  163. EXPORT_SYMBOL_GPL(rds_message_put);
  164. void rds_message_populate_header(struct rds_header *hdr, __be16 sport,
  165. __be16 dport, u64 seq)
  166. {
  167. hdr->h_flags = 0;
  168. hdr->h_sport = sport;
  169. hdr->h_dport = dport;
  170. hdr->h_sequence = cpu_to_be64(seq);
  171. hdr->h_exthdr[0] = RDS_EXTHDR_NONE;
  172. }
  173. EXPORT_SYMBOL_GPL(rds_message_populate_header);
  174. int rds_message_add_extension(struct rds_header *hdr, unsigned int type,
  175. const void *data, unsigned int len)
  176. {
  177. unsigned int ext_len = sizeof(u8) + len;
  178. unsigned char *dst;
  179. /* For now, refuse to add more than one extension header */
  180. if (hdr->h_exthdr[0] != RDS_EXTHDR_NONE)
  181. return 0;
  182. if (type >= __RDS_EXTHDR_MAX || len != rds_exthdr_size[type])
  183. return 0;
  184. if (ext_len >= RDS_HEADER_EXT_SPACE)
  185. return 0;
  186. dst = hdr->h_exthdr;
  187. *dst++ = type;
  188. memcpy(dst, data, len);
  189. dst[len] = RDS_EXTHDR_NONE;
  190. return 1;
  191. }
  192. EXPORT_SYMBOL_GPL(rds_message_add_extension);
  193. /*
  194. * If a message has extension headers, retrieve them here.
  195. * Call like this:
  196. *
  197. * unsigned int pos = 0;
  198. *
  199. * while (1) {
  200. * buflen = sizeof(buffer);
  201. * type = rds_message_next_extension(hdr, &pos, buffer, &buflen);
  202. * if (type == RDS_EXTHDR_NONE)
  203. * break;
  204. * ...
  205. * }
  206. */
  207. int rds_message_next_extension(struct rds_header *hdr,
  208. unsigned int *pos, void *buf, unsigned int *buflen)
  209. {
  210. unsigned int offset, ext_type, ext_len;
  211. u8 *src = hdr->h_exthdr;
  212. offset = *pos;
  213. if (offset >= RDS_HEADER_EXT_SPACE)
  214. goto none;
  215. /* Get the extension type and length. For now, the
  216. * length is implied by the extension type. */
  217. ext_type = src[offset++];
  218. if (ext_type == RDS_EXTHDR_NONE || ext_type >= __RDS_EXTHDR_MAX)
  219. goto none;
  220. ext_len = rds_exthdr_size[ext_type];
  221. if (offset + ext_len > RDS_HEADER_EXT_SPACE)
  222. goto none;
  223. *pos = offset + ext_len;
  224. if (ext_len < *buflen)
  225. *buflen = ext_len;
  226. memcpy(buf, src + offset, *buflen);
  227. return ext_type;
  228. none:
  229. *pos = RDS_HEADER_EXT_SPACE;
  230. *buflen = 0;
  231. return RDS_EXTHDR_NONE;
  232. }
  233. int rds_message_add_rdma_dest_extension(struct rds_header *hdr, u32 r_key, u32 offset)
  234. {
  235. struct rds_ext_header_rdma_dest ext_hdr;
  236. ext_hdr.h_rdma_rkey = cpu_to_be32(r_key);
  237. ext_hdr.h_rdma_offset = cpu_to_be32(offset);
  238. return rds_message_add_extension(hdr, RDS_EXTHDR_RDMA_DEST, &ext_hdr, sizeof(ext_hdr));
  239. }
  240. EXPORT_SYMBOL_GPL(rds_message_add_rdma_dest_extension);
  241. /*
  242. * Each rds_message is allocated with extra space for the scatterlist entries
  243. * rds ops will need. This is to minimize memory allocation count. Then, each rds op
  244. * can grab SGs when initializing its part of the rds_message.
  245. */
  246. struct rds_message *rds_message_alloc(unsigned int extra_len, gfp_t gfp)
  247. {
  248. struct rds_message *rm;
  249. if (extra_len > KMALLOC_MAX_SIZE - sizeof(struct rds_message))
  250. return NULL;
  251. rm = kzalloc(sizeof(struct rds_message) + extra_len, gfp);
  252. if (!rm)
  253. goto out;
  254. rm->m_used_sgs = 0;
  255. rm->m_total_sgs = extra_len / sizeof(struct scatterlist);
  256. refcount_set(&rm->m_refcount, 1);
  257. INIT_LIST_HEAD(&rm->m_sock_item);
  258. INIT_LIST_HEAD(&rm->m_conn_item);
  259. spin_lock_init(&rm->m_rs_lock);
  260. init_waitqueue_head(&rm->m_flush_wait);
  261. out:
  262. return rm;
  263. }
  264. /*
  265. * RDS ops use this to grab SG entries from the rm's sg pool.
  266. */
  267. struct scatterlist *rds_message_alloc_sgs(struct rds_message *rm, int nents)
  268. {
  269. struct scatterlist *sg_first = (struct scatterlist *) &rm[1];
  270. struct scatterlist *sg_ret;
  271. if (nents <= 0) {
  272. pr_warn("rds: alloc sgs failed! nents <= 0\n");
  273. return ERR_PTR(-EINVAL);
  274. }
  275. if (rm->m_used_sgs + nents > rm->m_total_sgs) {
  276. pr_warn("rds: alloc sgs failed! total %d used %d nents %d\n",
  277. rm->m_total_sgs, rm->m_used_sgs, nents);
  278. return ERR_PTR(-ENOMEM);
  279. }
  280. sg_ret = &sg_first[rm->m_used_sgs];
  281. sg_init_table(sg_ret, nents);
  282. rm->m_used_sgs += nents;
  283. return sg_ret;
  284. }
  285. struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len)
  286. {
  287. struct rds_message *rm;
  288. unsigned int i;
  289. int num_sgs = DIV_ROUND_UP(total_len, PAGE_SIZE);
  290. int extra_bytes = num_sgs * sizeof(struct scatterlist);
  291. rm = rds_message_alloc(extra_bytes, GFP_NOWAIT);
  292. if (!rm)
  293. return ERR_PTR(-ENOMEM);
  294. set_bit(RDS_MSG_PAGEVEC, &rm->m_flags);
  295. rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len);
  296. rm->data.op_nents = DIV_ROUND_UP(total_len, PAGE_SIZE);
  297. rm->data.op_sg = rds_message_alloc_sgs(rm, num_sgs);
  298. if (IS_ERR(rm->data.op_sg)) {
  299. void *err = ERR_CAST(rm->data.op_sg);
  300. rds_message_put(rm);
  301. return err;
  302. }
  303. for (i = 0; i < rm->data.op_nents; ++i) {
  304. sg_set_page(&rm->data.op_sg[i],
  305. virt_to_page((void *)page_addrs[i]),
  306. PAGE_SIZE, 0);
  307. }
  308. return rm;
  309. }
  310. static int rds_message_zcopy_from_user(struct rds_message *rm, struct iov_iter *from)
  311. {
  312. struct scatterlist *sg;
  313. int ret = 0;
  314. int length = iov_iter_count(from);
  315. int total_copied = 0;
  316. struct rds_msg_zcopy_info *info;
  317. rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from));
  318. /*
  319. * now allocate and copy in the data payload.
  320. */
  321. sg = rm->data.op_sg;
  322. info = kzalloc(sizeof(*info), GFP_KERNEL);
  323. if (!info)
  324. return -ENOMEM;
  325. INIT_LIST_HEAD(&info->rs_zcookie_next);
  326. rm->data.op_mmp_znotifier = &info->znotif;
  327. if (mm_account_pinned_pages(&rm->data.op_mmp_znotifier->z_mmp,
  328. length)) {
  329. ret = -ENOMEM;
  330. goto err;
  331. }
  332. while (iov_iter_count(from)) {
  333. struct page *pages;
  334. size_t start;
  335. ssize_t copied;
  336. copied = iov_iter_get_pages2(from, &pages, PAGE_SIZE,
  337. 1, &start);
  338. if (copied < 0) {
  339. struct mmpin *mmp;
  340. int i;
  341. for (i = 0; i < rm->data.op_nents; i++)
  342. put_page(sg_page(&rm->data.op_sg[i]));
  343. mmp = &rm->data.op_mmp_znotifier->z_mmp;
  344. mm_unaccount_pinned_pages(mmp);
  345. ret = -EFAULT;
  346. goto err;
  347. }
  348. total_copied += copied;
  349. length -= copied;
  350. sg_set_page(sg, pages, copied, start);
  351. rm->data.op_nents++;
  352. sg++;
  353. }
  354. WARN_ON_ONCE(length != 0);
  355. return ret;
  356. err:
  357. kfree(info);
  358. rm->data.op_mmp_znotifier = NULL;
  359. return ret;
  360. }
  361. int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from,
  362. bool zcopy)
  363. {
  364. unsigned long to_copy, nbytes;
  365. unsigned long sg_off;
  366. struct scatterlist *sg;
  367. int ret = 0;
  368. rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from));
  369. /* now allocate and copy in the data payload. */
  370. sg = rm->data.op_sg;
  371. sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */
  372. if (zcopy)
  373. return rds_message_zcopy_from_user(rm, from);
  374. while (iov_iter_count(from)) {
  375. if (!sg_page(sg)) {
  376. ret = rds_page_remainder_alloc(sg, iov_iter_count(from),
  377. GFP_HIGHUSER);
  378. if (ret)
  379. return ret;
  380. rm->data.op_nents++;
  381. sg_off = 0;
  382. }
  383. to_copy = min_t(unsigned long, iov_iter_count(from),
  384. sg->length - sg_off);
  385. rds_stats_add(s_copy_from_user, to_copy);
  386. nbytes = copy_page_from_iter(sg_page(sg), sg->offset + sg_off,
  387. to_copy, from);
  388. if (nbytes != to_copy)
  389. return -EFAULT;
  390. sg_off += to_copy;
  391. if (sg_off == sg->length)
  392. sg++;
  393. }
  394. return ret;
  395. }
  396. int rds_message_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to)
  397. {
  398. struct rds_message *rm;
  399. struct scatterlist *sg;
  400. unsigned long to_copy;
  401. unsigned long vec_off;
  402. int copied;
  403. int ret;
  404. u32 len;
  405. rm = container_of(inc, struct rds_message, m_inc);
  406. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  407. sg = rm->data.op_sg;
  408. vec_off = 0;
  409. copied = 0;
  410. while (iov_iter_count(to) && copied < len) {
  411. to_copy = min_t(unsigned long, iov_iter_count(to),
  412. sg->length - vec_off);
  413. to_copy = min_t(unsigned long, to_copy, len - copied);
  414. rds_stats_add(s_copy_to_user, to_copy);
  415. ret = copy_page_to_iter(sg_page(sg), sg->offset + vec_off,
  416. to_copy, to);
  417. if (ret != to_copy)
  418. return -EFAULT;
  419. vec_off += to_copy;
  420. copied += to_copy;
  421. if (vec_off == sg->length) {
  422. vec_off = 0;
  423. sg++;
  424. }
  425. }
  426. return copied;
  427. }
  428. /*
  429. * If the message is still on the send queue, wait until the transport
  430. * is done with it. This is particularly important for RDMA operations.
  431. */
  432. void rds_message_wait(struct rds_message *rm)
  433. {
  434. wait_event_interruptible(rm->m_flush_wait,
  435. !test_bit(RDS_MSG_MAPPED, &rm->m_flags));
  436. }
  437. void rds_message_unmapped(struct rds_message *rm)
  438. {
  439. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  440. wake_up_interruptible(&rm->m_flush_wait);
  441. }
  442. EXPORT_SYMBOL_GPL(rds_message_unmapped);