message.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Copyright (c) 2006 Oracle. All rights reserved.
  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_entry(head, struct rds_msg_zcopy_info,
  97. rs_zcookie_next);
  98. if (info && 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(&q->zcookie_head, &info->rs_zcookie_next);
  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. rds_mr_put(rm->rdma.op_rdma_mr);
  147. if (rm->atomic.op_active)
  148. rds_atomic_free_op(&rm->atomic);
  149. if (rm->atomic.op_rdma_mr)
  150. rds_mr_put(rm->atomic.op_rdma_mr);
  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. WARN_ON(rm->m_used_sgs + nents > rm->m_total_sgs);
  272. WARN_ON(!nents);
  273. if (rm->m_used_sgs + nents > rm->m_total_sgs)
  274. return NULL;
  275. sg_ret = &sg_first[rm->m_used_sgs];
  276. sg_init_table(sg_ret, nents);
  277. rm->m_used_sgs += nents;
  278. return sg_ret;
  279. }
  280. struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len)
  281. {
  282. struct rds_message *rm;
  283. unsigned int i;
  284. int num_sgs = ceil(total_len, PAGE_SIZE);
  285. int extra_bytes = num_sgs * sizeof(struct scatterlist);
  286. rm = rds_message_alloc(extra_bytes, GFP_NOWAIT);
  287. if (!rm)
  288. return ERR_PTR(-ENOMEM);
  289. set_bit(RDS_MSG_PAGEVEC, &rm->m_flags);
  290. rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len);
  291. rm->data.op_nents = ceil(total_len, PAGE_SIZE);
  292. rm->data.op_sg = rds_message_alloc_sgs(rm, num_sgs);
  293. if (!rm->data.op_sg) {
  294. rds_message_put(rm);
  295. return ERR_PTR(-ENOMEM);
  296. }
  297. for (i = 0; i < rm->data.op_nents; ++i) {
  298. sg_set_page(&rm->data.op_sg[i],
  299. virt_to_page(page_addrs[i]),
  300. PAGE_SIZE, 0);
  301. }
  302. return rm;
  303. }
  304. static int rds_message_zcopy_from_user(struct rds_message *rm, struct iov_iter *from)
  305. {
  306. struct scatterlist *sg;
  307. int ret = 0;
  308. int length = iov_iter_count(from);
  309. int total_copied = 0;
  310. struct rds_msg_zcopy_info *info;
  311. rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from));
  312. /*
  313. * now allocate and copy in the data payload.
  314. */
  315. sg = rm->data.op_sg;
  316. info = kzalloc(sizeof(*info), GFP_KERNEL);
  317. if (!info)
  318. return -ENOMEM;
  319. INIT_LIST_HEAD(&info->rs_zcookie_next);
  320. rm->data.op_mmp_znotifier = &info->znotif;
  321. if (mm_account_pinned_pages(&rm->data.op_mmp_znotifier->z_mmp,
  322. length)) {
  323. ret = -ENOMEM;
  324. goto err;
  325. }
  326. while (iov_iter_count(from)) {
  327. struct page *pages;
  328. size_t start;
  329. ssize_t copied;
  330. copied = iov_iter_get_pages(from, &pages, PAGE_SIZE,
  331. 1, &start);
  332. if (copied < 0) {
  333. struct mmpin *mmp;
  334. int i;
  335. for (i = 0; i < rm->data.op_nents; i++)
  336. put_page(sg_page(&rm->data.op_sg[i]));
  337. mmp = &rm->data.op_mmp_znotifier->z_mmp;
  338. mm_unaccount_pinned_pages(mmp);
  339. ret = -EFAULT;
  340. goto err;
  341. }
  342. total_copied += copied;
  343. iov_iter_advance(from, copied);
  344. length -= copied;
  345. sg_set_page(sg, pages, copied, start);
  346. rm->data.op_nents++;
  347. sg++;
  348. }
  349. WARN_ON_ONCE(length != 0);
  350. return ret;
  351. err:
  352. kfree(info);
  353. rm->data.op_mmp_znotifier = NULL;
  354. return ret;
  355. }
  356. int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from,
  357. bool zcopy)
  358. {
  359. unsigned long to_copy, nbytes;
  360. unsigned long sg_off;
  361. struct scatterlist *sg;
  362. int ret = 0;
  363. rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from));
  364. /* now allocate and copy in the data payload. */
  365. sg = rm->data.op_sg;
  366. sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */
  367. if (zcopy)
  368. return rds_message_zcopy_from_user(rm, from);
  369. while (iov_iter_count(from)) {
  370. if (!sg_page(sg)) {
  371. ret = rds_page_remainder_alloc(sg, iov_iter_count(from),
  372. GFP_HIGHUSER);
  373. if (ret)
  374. return ret;
  375. rm->data.op_nents++;
  376. sg_off = 0;
  377. }
  378. to_copy = min_t(unsigned long, iov_iter_count(from),
  379. sg->length - sg_off);
  380. rds_stats_add(s_copy_from_user, to_copy);
  381. nbytes = copy_page_from_iter(sg_page(sg), sg->offset + sg_off,
  382. to_copy, from);
  383. if (nbytes != to_copy)
  384. return -EFAULT;
  385. sg_off += to_copy;
  386. if (sg_off == sg->length)
  387. sg++;
  388. }
  389. return ret;
  390. }
  391. int rds_message_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to)
  392. {
  393. struct rds_message *rm;
  394. struct scatterlist *sg;
  395. unsigned long to_copy;
  396. unsigned long vec_off;
  397. int copied;
  398. int ret;
  399. u32 len;
  400. rm = container_of(inc, struct rds_message, m_inc);
  401. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  402. sg = rm->data.op_sg;
  403. vec_off = 0;
  404. copied = 0;
  405. while (iov_iter_count(to) && copied < len) {
  406. to_copy = min_t(unsigned long, iov_iter_count(to),
  407. sg->length - vec_off);
  408. to_copy = min_t(unsigned long, to_copy, len - copied);
  409. rds_stats_add(s_copy_to_user, to_copy);
  410. ret = copy_page_to_iter(sg_page(sg), sg->offset + vec_off,
  411. to_copy, to);
  412. if (ret != to_copy)
  413. return -EFAULT;
  414. vec_off += to_copy;
  415. copied += to_copy;
  416. if (vec_off == sg->length) {
  417. vec_off = 0;
  418. sg++;
  419. }
  420. }
  421. return copied;
  422. }
  423. /*
  424. * If the message is still on the send queue, wait until the transport
  425. * is done with it. This is particularly important for RDMA operations.
  426. */
  427. void rds_message_wait(struct rds_message *rm)
  428. {
  429. wait_event_interruptible(rm->m_flush_wait,
  430. !test_bit(RDS_MSG_MAPPED, &rm->m_flags));
  431. }
  432. void rds_message_unmapped(struct rds_message *rm)
  433. {
  434. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  435. wake_up_interruptible(&rm->m_flush_wait);
  436. }
  437. EXPORT_SYMBOL_GPL(rds_message_unmapped);