rdma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * Copyright (c) 2007 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/pagemap.h>
  34. #include <linux/slab.h>
  35. #include <linux/rbtree.h>
  36. #include <linux/dma-mapping.h> /* for DMA_*_DEVICE */
  37. #include "rds.h"
  38. /*
  39. * XXX
  40. * - build with sparse
  41. * - should we limit the size of a mr region? let transport return failure?
  42. * - should we detect duplicate keys on a socket? hmm.
  43. * - an rdma is an mlock, apply rlimit?
  44. */
  45. /*
  46. * get the number of pages by looking at the page indices that the start and
  47. * end addresses fall in.
  48. *
  49. * Returns 0 if the vec is invalid. It is invalid if the number of bytes
  50. * causes the address to wrap or overflows an unsigned int. This comes
  51. * from being stored in the 'length' member of 'struct scatterlist'.
  52. */
  53. static unsigned int rds_pages_in_vec(struct rds_iovec *vec)
  54. {
  55. if ((vec->addr + vec->bytes <= vec->addr) ||
  56. (vec->bytes > (u64)UINT_MAX))
  57. return 0;
  58. return ((vec->addr + vec->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT) -
  59. (vec->addr >> PAGE_SHIFT);
  60. }
  61. static struct rds_mr *rds_mr_tree_walk(struct rb_root *root, u64 key,
  62. struct rds_mr *insert)
  63. {
  64. struct rb_node **p = &root->rb_node;
  65. struct rb_node *parent = NULL;
  66. struct rds_mr *mr;
  67. while (*p) {
  68. parent = *p;
  69. mr = rb_entry(parent, struct rds_mr, r_rb_node);
  70. if (key < mr->r_key)
  71. p = &(*p)->rb_left;
  72. else if (key > mr->r_key)
  73. p = &(*p)->rb_right;
  74. else
  75. return mr;
  76. }
  77. if (insert) {
  78. rb_link_node(&insert->r_rb_node, parent, p);
  79. rb_insert_color(&insert->r_rb_node, root);
  80. atomic_inc(&insert->r_refcount);
  81. }
  82. return NULL;
  83. }
  84. /*
  85. * Destroy the transport-specific part of a MR.
  86. */
  87. static void rds_destroy_mr(struct rds_mr *mr)
  88. {
  89. struct rds_sock *rs = mr->r_sock;
  90. void *trans_private = NULL;
  91. unsigned long flags;
  92. rdsdebug("RDS: destroy mr key is %x refcnt %u\n",
  93. mr->r_key, atomic_read(&mr->r_refcount));
  94. if (test_and_set_bit(RDS_MR_DEAD, &mr->r_state))
  95. return;
  96. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  97. if (!RB_EMPTY_NODE(&mr->r_rb_node))
  98. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  99. trans_private = mr->r_trans_private;
  100. mr->r_trans_private = NULL;
  101. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  102. if (trans_private)
  103. mr->r_trans->free_mr(trans_private, mr->r_invalidate);
  104. }
  105. void __rds_put_mr_final(struct rds_mr *mr)
  106. {
  107. rds_destroy_mr(mr);
  108. kfree(mr);
  109. }
  110. /*
  111. * By the time this is called we can't have any more ioctls called on
  112. * the socket so we don't need to worry about racing with others.
  113. */
  114. void rds_rdma_drop_keys(struct rds_sock *rs)
  115. {
  116. struct rds_mr *mr;
  117. struct rb_node *node;
  118. unsigned long flags;
  119. /* Release any MRs associated with this socket */
  120. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  121. while ((node = rb_first(&rs->rs_rdma_keys))) {
  122. mr = container_of(node, struct rds_mr, r_rb_node);
  123. if (mr->r_trans == rs->rs_transport)
  124. mr->r_invalidate = 0;
  125. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  126. RB_CLEAR_NODE(&mr->r_rb_node);
  127. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  128. rds_destroy_mr(mr);
  129. rds_mr_put(mr);
  130. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  131. }
  132. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  133. if (rs->rs_transport && rs->rs_transport->flush_mrs)
  134. rs->rs_transport->flush_mrs();
  135. }
  136. /*
  137. * Helper function to pin user pages.
  138. */
  139. static int rds_pin_pages(unsigned long user_addr, unsigned int nr_pages,
  140. struct page **pages, int write)
  141. {
  142. int ret;
  143. ret = get_user_pages_fast(user_addr, nr_pages, write, pages);
  144. if (ret >= 0 && ret < nr_pages) {
  145. while (ret--)
  146. put_page(pages[ret]);
  147. ret = -EFAULT;
  148. }
  149. return ret;
  150. }
  151. static int __rds_rdma_map(struct rds_sock *rs, struct rds_get_mr_args *args,
  152. u64 *cookie_ret, struct rds_mr **mr_ret)
  153. {
  154. struct rds_mr *mr = NULL, *found;
  155. unsigned int nr_pages;
  156. struct page **pages = NULL;
  157. struct scatterlist *sg;
  158. void *trans_private;
  159. unsigned long flags;
  160. rds_rdma_cookie_t cookie;
  161. unsigned int nents;
  162. long i;
  163. int ret;
  164. if (rs->rs_bound_addr == 0 || !rs->rs_transport) {
  165. ret = -ENOTCONN; /* XXX not a great errno */
  166. goto out;
  167. }
  168. if (!rs->rs_transport->get_mr) {
  169. ret = -EOPNOTSUPP;
  170. goto out;
  171. }
  172. nr_pages = rds_pages_in_vec(&args->vec);
  173. if (nr_pages == 0) {
  174. ret = -EINVAL;
  175. goto out;
  176. }
  177. rdsdebug("RDS: get_mr addr %llx len %llu nr_pages %u\n",
  178. args->vec.addr, args->vec.bytes, nr_pages);
  179. /* XXX clamp nr_pages to limit the size of this alloc? */
  180. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  181. if (!pages) {
  182. ret = -ENOMEM;
  183. goto out;
  184. }
  185. mr = kzalloc(sizeof(struct rds_mr), GFP_KERNEL);
  186. if (!mr) {
  187. ret = -ENOMEM;
  188. goto out;
  189. }
  190. atomic_set(&mr->r_refcount, 1);
  191. RB_CLEAR_NODE(&mr->r_rb_node);
  192. mr->r_trans = rs->rs_transport;
  193. mr->r_sock = rs;
  194. if (args->flags & RDS_RDMA_USE_ONCE)
  195. mr->r_use_once = 1;
  196. if (args->flags & RDS_RDMA_INVALIDATE)
  197. mr->r_invalidate = 1;
  198. if (args->flags & RDS_RDMA_READWRITE)
  199. mr->r_write = 1;
  200. /*
  201. * Pin the pages that make up the user buffer and transfer the page
  202. * pointers to the mr's sg array. We check to see if we've mapped
  203. * the whole region after transferring the partial page references
  204. * to the sg array so that we can have one page ref cleanup path.
  205. *
  206. * For now we have no flag that tells us whether the mapping is
  207. * r/o or r/w. We need to assume r/w, or we'll do a lot of RDMA to
  208. * the zero page.
  209. */
  210. ret = rds_pin_pages(args->vec.addr, nr_pages, pages, 1);
  211. if (ret < 0)
  212. goto out;
  213. nents = ret;
  214. sg = kcalloc(nents, sizeof(*sg), GFP_KERNEL);
  215. if (!sg) {
  216. ret = -ENOMEM;
  217. goto out;
  218. }
  219. WARN_ON(!nents);
  220. sg_init_table(sg, nents);
  221. /* Stick all pages into the scatterlist */
  222. for (i = 0 ; i < nents; i++)
  223. sg_set_page(&sg[i], pages[i], PAGE_SIZE, 0);
  224. rdsdebug("RDS: trans_private nents is %u\n", nents);
  225. /* Obtain a transport specific MR. If this succeeds, the
  226. * s/g list is now owned by the MR.
  227. * Note that dma_map() implies that pending writes are
  228. * flushed to RAM, so no dma_sync is needed here. */
  229. trans_private = rs->rs_transport->get_mr(sg, nents, rs,
  230. &mr->r_key);
  231. if (IS_ERR(trans_private)) {
  232. for (i = 0 ; i < nents; i++)
  233. put_page(sg_page(&sg[i]));
  234. kfree(sg);
  235. ret = PTR_ERR(trans_private);
  236. goto out;
  237. }
  238. mr->r_trans_private = trans_private;
  239. rdsdebug("RDS: get_mr put_user key is %x cookie_addr %p\n",
  240. mr->r_key, (void *)(unsigned long) args->cookie_addr);
  241. /* The user may pass us an unaligned address, but we can only
  242. * map page aligned regions. So we keep the offset, and build
  243. * a 64bit cookie containing <R_Key, offset> and pass that
  244. * around. */
  245. cookie = rds_rdma_make_cookie(mr->r_key, args->vec.addr & ~PAGE_MASK);
  246. if (cookie_ret)
  247. *cookie_ret = cookie;
  248. if (args->cookie_addr && put_user(cookie, (u64 __user *)(unsigned long) args->cookie_addr)) {
  249. ret = -EFAULT;
  250. goto out;
  251. }
  252. /* Inserting the new MR into the rbtree bumps its
  253. * reference count. */
  254. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  255. found = rds_mr_tree_walk(&rs->rs_rdma_keys, mr->r_key, mr);
  256. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  257. BUG_ON(found && found != mr);
  258. rdsdebug("RDS: get_mr key is %x\n", mr->r_key);
  259. if (mr_ret) {
  260. atomic_inc(&mr->r_refcount);
  261. *mr_ret = mr;
  262. }
  263. ret = 0;
  264. out:
  265. kfree(pages);
  266. if (mr)
  267. rds_mr_put(mr);
  268. return ret;
  269. }
  270. int rds_get_mr(struct rds_sock *rs, char __user *optval, int optlen)
  271. {
  272. struct rds_get_mr_args args;
  273. if (optlen != sizeof(struct rds_get_mr_args))
  274. return -EINVAL;
  275. if (copy_from_user(&args, (struct rds_get_mr_args __user *)optval,
  276. sizeof(struct rds_get_mr_args)))
  277. return -EFAULT;
  278. return __rds_rdma_map(rs, &args, NULL, NULL);
  279. }
  280. int rds_get_mr_for_dest(struct rds_sock *rs, char __user *optval, int optlen)
  281. {
  282. struct rds_get_mr_for_dest_args args;
  283. struct rds_get_mr_args new_args;
  284. if (optlen != sizeof(struct rds_get_mr_for_dest_args))
  285. return -EINVAL;
  286. if (copy_from_user(&args, (struct rds_get_mr_for_dest_args __user *)optval,
  287. sizeof(struct rds_get_mr_for_dest_args)))
  288. return -EFAULT;
  289. /*
  290. * Initially, just behave like get_mr().
  291. * TODO: Implement get_mr as wrapper around this
  292. * and deprecate it.
  293. */
  294. new_args.vec = args.vec;
  295. new_args.cookie_addr = args.cookie_addr;
  296. new_args.flags = args.flags;
  297. return __rds_rdma_map(rs, &new_args, NULL, NULL);
  298. }
  299. /*
  300. * Free the MR indicated by the given R_Key
  301. */
  302. int rds_free_mr(struct rds_sock *rs, char __user *optval, int optlen)
  303. {
  304. struct rds_free_mr_args args;
  305. struct rds_mr *mr;
  306. unsigned long flags;
  307. if (optlen != sizeof(struct rds_free_mr_args))
  308. return -EINVAL;
  309. if (copy_from_user(&args, (struct rds_free_mr_args __user *)optval,
  310. sizeof(struct rds_free_mr_args)))
  311. return -EFAULT;
  312. /* Special case - a null cookie means flush all unused MRs */
  313. if (args.cookie == 0) {
  314. if (!rs->rs_transport || !rs->rs_transport->flush_mrs)
  315. return -EINVAL;
  316. rs->rs_transport->flush_mrs();
  317. return 0;
  318. }
  319. /* Look up the MR given its R_key and remove it from the rbtree
  320. * so nobody else finds it.
  321. * This should also prevent races with rds_rdma_unuse.
  322. */
  323. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  324. mr = rds_mr_tree_walk(&rs->rs_rdma_keys, rds_rdma_cookie_key(args.cookie), NULL);
  325. if (mr) {
  326. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  327. RB_CLEAR_NODE(&mr->r_rb_node);
  328. if (args.flags & RDS_RDMA_INVALIDATE)
  329. mr->r_invalidate = 1;
  330. }
  331. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  332. if (!mr)
  333. return -EINVAL;
  334. /*
  335. * call rds_destroy_mr() ourselves so that we're sure it's done by the time
  336. * we return. If we let rds_mr_put() do it it might not happen until
  337. * someone else drops their ref.
  338. */
  339. rds_destroy_mr(mr);
  340. rds_mr_put(mr);
  341. return 0;
  342. }
  343. /*
  344. * This is called when we receive an extension header that
  345. * tells us this MR was used. It allows us to implement
  346. * use_once semantics
  347. */
  348. void rds_rdma_unuse(struct rds_sock *rs, u32 r_key, int force)
  349. {
  350. struct rds_mr *mr;
  351. unsigned long flags;
  352. int zot_me = 0;
  353. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  354. mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
  355. if (!mr) {
  356. printk(KERN_ERR "rds: trying to unuse MR with unknown r_key %u!\n", r_key);
  357. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  358. return;
  359. }
  360. if (mr->r_use_once || force) {
  361. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  362. RB_CLEAR_NODE(&mr->r_rb_node);
  363. zot_me = 1;
  364. }
  365. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  366. /* May have to issue a dma_sync on this memory region.
  367. * Note we could avoid this if the operation was a RDMA READ,
  368. * but at this point we can't tell. */
  369. if (mr->r_trans->sync_mr)
  370. mr->r_trans->sync_mr(mr->r_trans_private, DMA_FROM_DEVICE);
  371. /* If the MR was marked as invalidate, this will
  372. * trigger an async flush. */
  373. if (zot_me) {
  374. rds_destroy_mr(mr);
  375. rds_mr_put(mr);
  376. }
  377. }
  378. void rds_rdma_free_op(struct rm_rdma_op *ro)
  379. {
  380. unsigned int i;
  381. for (i = 0; i < ro->op_nents; i++) {
  382. struct page *page = sg_page(&ro->op_sg[i]);
  383. /* Mark page dirty if it was possibly modified, which
  384. * is the case for a RDMA_READ which copies from remote
  385. * to local memory */
  386. if (!ro->op_write) {
  387. WARN_ON(!page->mapping && irqs_disabled());
  388. set_page_dirty(page);
  389. }
  390. put_page(page);
  391. }
  392. kfree(ro->op_notifier);
  393. ro->op_notifier = NULL;
  394. ro->op_active = 0;
  395. }
  396. void rds_atomic_free_op(struct rm_atomic_op *ao)
  397. {
  398. struct page *page = sg_page(ao->op_sg);
  399. /* Mark page dirty if it was possibly modified, which
  400. * is the case for a RDMA_READ which copies from remote
  401. * to local memory */
  402. set_page_dirty(page);
  403. put_page(page);
  404. kfree(ao->op_notifier);
  405. ao->op_notifier = NULL;
  406. ao->op_active = 0;
  407. }
  408. /*
  409. * Count the number of pages needed to describe an incoming iovec array.
  410. */
  411. static int rds_rdma_pages(struct rds_iovec iov[], int nr_iovecs)
  412. {
  413. int tot_pages = 0;
  414. unsigned int nr_pages;
  415. unsigned int i;
  416. /* figure out the number of pages in the vector */
  417. for (i = 0; i < nr_iovecs; i++) {
  418. nr_pages = rds_pages_in_vec(&iov[i]);
  419. if (nr_pages == 0)
  420. return -EINVAL;
  421. tot_pages += nr_pages;
  422. /*
  423. * nr_pages for one entry is limited to (UINT_MAX>>PAGE_SHIFT)+1,
  424. * so tot_pages cannot overflow without first going negative.
  425. */
  426. if (tot_pages < 0)
  427. return -EINVAL;
  428. }
  429. return tot_pages;
  430. }
  431. int rds_rdma_extra_size(struct rds_rdma_args *args)
  432. {
  433. struct rds_iovec vec;
  434. struct rds_iovec __user *local_vec;
  435. int tot_pages = 0;
  436. unsigned int nr_pages;
  437. unsigned int i;
  438. local_vec = (struct rds_iovec __user *)(unsigned long) args->local_vec_addr;
  439. if (args->nr_local == 0)
  440. return -EINVAL;
  441. /* figure out the number of pages in the vector */
  442. for (i = 0; i < args->nr_local; i++) {
  443. if (copy_from_user(&vec, &local_vec[i],
  444. sizeof(struct rds_iovec)))
  445. return -EFAULT;
  446. nr_pages = rds_pages_in_vec(&vec);
  447. if (nr_pages == 0)
  448. return -EINVAL;
  449. tot_pages += nr_pages;
  450. /*
  451. * nr_pages for one entry is limited to (UINT_MAX>>PAGE_SHIFT)+1,
  452. * so tot_pages cannot overflow without first going negative.
  453. */
  454. if (tot_pages < 0)
  455. return -EINVAL;
  456. }
  457. return tot_pages * sizeof(struct scatterlist);
  458. }
  459. /*
  460. * The application asks for a RDMA transfer.
  461. * Extract all arguments and set up the rdma_op
  462. */
  463. int rds_cmsg_rdma_args(struct rds_sock *rs, struct rds_message *rm,
  464. struct cmsghdr *cmsg)
  465. {
  466. struct rds_rdma_args *args;
  467. struct rm_rdma_op *op = &rm->rdma;
  468. int nr_pages;
  469. unsigned int nr_bytes;
  470. struct page **pages = NULL;
  471. struct rds_iovec iovstack[UIO_FASTIOV], *iovs = iovstack;
  472. int iov_size;
  473. unsigned int i, j;
  474. int ret = 0;
  475. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_rdma_args))
  476. || rm->rdma.op_active)
  477. return -EINVAL;
  478. args = CMSG_DATA(cmsg);
  479. if (rs->rs_bound_addr == 0) {
  480. ret = -ENOTCONN; /* XXX not a great errno */
  481. goto out_ret;
  482. }
  483. if (args->nr_local > UIO_MAXIOV) {
  484. ret = -EMSGSIZE;
  485. goto out_ret;
  486. }
  487. /* Check whether to allocate the iovec area */
  488. iov_size = args->nr_local * sizeof(struct rds_iovec);
  489. if (args->nr_local > UIO_FASTIOV) {
  490. iovs = sock_kmalloc(rds_rs_to_sk(rs), iov_size, GFP_KERNEL);
  491. if (!iovs) {
  492. ret = -ENOMEM;
  493. goto out_ret;
  494. }
  495. }
  496. if (copy_from_user(iovs, (struct rds_iovec __user *)(unsigned long) args->local_vec_addr, iov_size)) {
  497. ret = -EFAULT;
  498. goto out;
  499. }
  500. nr_pages = rds_rdma_pages(iovs, args->nr_local);
  501. if (nr_pages < 0) {
  502. ret = -EINVAL;
  503. goto out;
  504. }
  505. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  506. if (!pages) {
  507. ret = -ENOMEM;
  508. goto out;
  509. }
  510. op->op_write = !!(args->flags & RDS_RDMA_READWRITE);
  511. op->op_fence = !!(args->flags & RDS_RDMA_FENCE);
  512. op->op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME);
  513. op->op_silent = !!(args->flags & RDS_RDMA_SILENT);
  514. op->op_active = 1;
  515. op->op_recverr = rs->rs_recverr;
  516. WARN_ON(!nr_pages);
  517. op->op_sg = rds_message_alloc_sgs(rm, nr_pages);
  518. if (!op->op_sg) {
  519. ret = -ENOMEM;
  520. goto out;
  521. }
  522. if (op->op_notify || op->op_recverr) {
  523. /* We allocate an uninitialized notifier here, because
  524. * we don't want to do that in the completion handler. We
  525. * would have to use GFP_ATOMIC there, and don't want to deal
  526. * with failed allocations.
  527. */
  528. op->op_notifier = kmalloc(sizeof(struct rds_notifier), GFP_KERNEL);
  529. if (!op->op_notifier) {
  530. ret = -ENOMEM;
  531. goto out;
  532. }
  533. op->op_notifier->n_user_token = args->user_token;
  534. op->op_notifier->n_status = RDS_RDMA_SUCCESS;
  535. /* Enable rmda notification on data operation for composite
  536. * rds messages and make sure notification is enabled only
  537. * for the data operation which follows it so that application
  538. * gets notified only after full message gets delivered.
  539. */
  540. if (rm->data.op_sg) {
  541. rm->rdma.op_notify = 0;
  542. rm->data.op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME);
  543. }
  544. }
  545. /* The cookie contains the R_Key of the remote memory region, and
  546. * optionally an offset into it. This is how we implement RDMA into
  547. * unaligned memory.
  548. * When setting up the RDMA, we need to add that offset to the
  549. * destination address (which is really an offset into the MR)
  550. * FIXME: We may want to move this into ib_rdma.c
  551. */
  552. op->op_rkey = rds_rdma_cookie_key(args->cookie);
  553. op->op_remote_addr = args->remote_vec.addr + rds_rdma_cookie_offset(args->cookie);
  554. nr_bytes = 0;
  555. rdsdebug("RDS: rdma prepare nr_local %llu rva %llx rkey %x\n",
  556. (unsigned long long)args->nr_local,
  557. (unsigned long long)args->remote_vec.addr,
  558. op->op_rkey);
  559. for (i = 0; i < args->nr_local; i++) {
  560. struct rds_iovec *iov = &iovs[i];
  561. /* don't need to check, rds_rdma_pages() verified nr will be +nonzero */
  562. unsigned int nr = rds_pages_in_vec(iov);
  563. rs->rs_user_addr = iov->addr;
  564. rs->rs_user_bytes = iov->bytes;
  565. /* If it's a WRITE operation, we want to pin the pages for reading.
  566. * If it's a READ operation, we need to pin the pages for writing.
  567. */
  568. ret = rds_pin_pages(iov->addr, nr, pages, !op->op_write);
  569. if (ret < 0)
  570. goto out;
  571. else
  572. ret = 0;
  573. rdsdebug("RDS: nr_bytes %u nr %u iov->bytes %llu iov->addr %llx\n",
  574. nr_bytes, nr, iov->bytes, iov->addr);
  575. nr_bytes += iov->bytes;
  576. for (j = 0; j < nr; j++) {
  577. unsigned int offset = iov->addr & ~PAGE_MASK;
  578. struct scatterlist *sg;
  579. sg = &op->op_sg[op->op_nents + j];
  580. sg_set_page(sg, pages[j],
  581. min_t(unsigned int, iov->bytes, PAGE_SIZE - offset),
  582. offset);
  583. rdsdebug("RDS: sg->offset %x sg->len %x iov->addr %llx iov->bytes %llu\n",
  584. sg->offset, sg->length, iov->addr, iov->bytes);
  585. iov->addr += sg->length;
  586. iov->bytes -= sg->length;
  587. }
  588. op->op_nents += nr;
  589. }
  590. if (nr_bytes > args->remote_vec.bytes) {
  591. rdsdebug("RDS nr_bytes %u remote_bytes %u do not match\n",
  592. nr_bytes,
  593. (unsigned int) args->remote_vec.bytes);
  594. ret = -EINVAL;
  595. goto out;
  596. }
  597. op->op_bytes = nr_bytes;
  598. out:
  599. if (iovs != iovstack)
  600. sock_kfree_s(rds_rs_to_sk(rs), iovs, iov_size);
  601. kfree(pages);
  602. out_ret:
  603. if (ret)
  604. rds_rdma_free_op(op);
  605. else
  606. rds_stats_inc(s_send_rdma);
  607. return ret;
  608. }
  609. /*
  610. * The application wants us to pass an RDMA destination (aka MR)
  611. * to the remote
  612. */
  613. int rds_cmsg_rdma_dest(struct rds_sock *rs, struct rds_message *rm,
  614. struct cmsghdr *cmsg)
  615. {
  616. unsigned long flags;
  617. struct rds_mr *mr;
  618. u32 r_key;
  619. int err = 0;
  620. if (cmsg->cmsg_len < CMSG_LEN(sizeof(rds_rdma_cookie_t)) ||
  621. rm->m_rdma_cookie != 0)
  622. return -EINVAL;
  623. memcpy(&rm->m_rdma_cookie, CMSG_DATA(cmsg), sizeof(rm->m_rdma_cookie));
  624. /* We are reusing a previously mapped MR here. Most likely, the
  625. * application has written to the buffer, so we need to explicitly
  626. * flush those writes to RAM. Otherwise the HCA may not see them
  627. * when doing a DMA from that buffer.
  628. */
  629. r_key = rds_rdma_cookie_key(rm->m_rdma_cookie);
  630. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  631. mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
  632. if (!mr)
  633. err = -EINVAL; /* invalid r_key */
  634. else
  635. atomic_inc(&mr->r_refcount);
  636. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  637. if (mr) {
  638. mr->r_trans->sync_mr(mr->r_trans_private, DMA_TO_DEVICE);
  639. rm->rdma.op_rdma_mr = mr;
  640. }
  641. return err;
  642. }
  643. /*
  644. * The application passes us an address range it wants to enable RDMA
  645. * to/from. We map the area, and save the <R_Key,offset> pair
  646. * in rm->m_rdma_cookie. This causes it to be sent along to the peer
  647. * in an extension header.
  648. */
  649. int rds_cmsg_rdma_map(struct rds_sock *rs, struct rds_message *rm,
  650. struct cmsghdr *cmsg)
  651. {
  652. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_get_mr_args)) ||
  653. rm->m_rdma_cookie != 0)
  654. return -EINVAL;
  655. return __rds_rdma_map(rs, CMSG_DATA(cmsg), &rm->m_rdma_cookie, &rm->rdma.op_rdma_mr);
  656. }
  657. /*
  658. * Fill in rds_message for an atomic request.
  659. */
  660. int rds_cmsg_atomic(struct rds_sock *rs, struct rds_message *rm,
  661. struct cmsghdr *cmsg)
  662. {
  663. struct page *page = NULL;
  664. struct rds_atomic_args *args;
  665. int ret = 0;
  666. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_atomic_args))
  667. || rm->atomic.op_active)
  668. return -EINVAL;
  669. args = CMSG_DATA(cmsg);
  670. /* Nonmasked & masked cmsg ops converted to masked hw ops */
  671. switch (cmsg->cmsg_type) {
  672. case RDS_CMSG_ATOMIC_FADD:
  673. rm->atomic.op_type = RDS_ATOMIC_TYPE_FADD;
  674. rm->atomic.op_m_fadd.add = args->fadd.add;
  675. rm->atomic.op_m_fadd.nocarry_mask = 0;
  676. break;
  677. case RDS_CMSG_MASKED_ATOMIC_FADD:
  678. rm->atomic.op_type = RDS_ATOMIC_TYPE_FADD;
  679. rm->atomic.op_m_fadd.add = args->m_fadd.add;
  680. rm->atomic.op_m_fadd.nocarry_mask = args->m_fadd.nocarry_mask;
  681. break;
  682. case RDS_CMSG_ATOMIC_CSWP:
  683. rm->atomic.op_type = RDS_ATOMIC_TYPE_CSWP;
  684. rm->atomic.op_m_cswp.compare = args->cswp.compare;
  685. rm->atomic.op_m_cswp.swap = args->cswp.swap;
  686. rm->atomic.op_m_cswp.compare_mask = ~0;
  687. rm->atomic.op_m_cswp.swap_mask = ~0;
  688. break;
  689. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  690. rm->atomic.op_type = RDS_ATOMIC_TYPE_CSWP;
  691. rm->atomic.op_m_cswp.compare = args->m_cswp.compare;
  692. rm->atomic.op_m_cswp.swap = args->m_cswp.swap;
  693. rm->atomic.op_m_cswp.compare_mask = args->m_cswp.compare_mask;
  694. rm->atomic.op_m_cswp.swap_mask = args->m_cswp.swap_mask;
  695. break;
  696. default:
  697. BUG(); /* should never happen */
  698. }
  699. rm->atomic.op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME);
  700. rm->atomic.op_silent = !!(args->flags & RDS_RDMA_SILENT);
  701. rm->atomic.op_active = 1;
  702. rm->atomic.op_recverr = rs->rs_recverr;
  703. rm->atomic.op_sg = rds_message_alloc_sgs(rm, 1);
  704. if (!rm->atomic.op_sg) {
  705. ret = -ENOMEM;
  706. goto err;
  707. }
  708. /* verify 8 byte-aligned */
  709. if (args->local_addr & 0x7) {
  710. ret = -EFAULT;
  711. goto err;
  712. }
  713. ret = rds_pin_pages(args->local_addr, 1, &page, 1);
  714. if (ret != 1)
  715. goto err;
  716. ret = 0;
  717. sg_set_page(rm->atomic.op_sg, page, 8, offset_in_page(args->local_addr));
  718. if (rm->atomic.op_notify || rm->atomic.op_recverr) {
  719. /* We allocate an uninitialized notifier here, because
  720. * we don't want to do that in the completion handler. We
  721. * would have to use GFP_ATOMIC there, and don't want to deal
  722. * with failed allocations.
  723. */
  724. rm->atomic.op_notifier = kmalloc(sizeof(*rm->atomic.op_notifier), GFP_KERNEL);
  725. if (!rm->atomic.op_notifier) {
  726. ret = -ENOMEM;
  727. goto err;
  728. }
  729. rm->atomic.op_notifier->n_user_token = args->user_token;
  730. rm->atomic.op_notifier->n_status = RDS_RDMA_SUCCESS;
  731. }
  732. rm->atomic.op_rkey = rds_rdma_cookie_key(args->cookie);
  733. rm->atomic.op_remote_addr = args->remote_addr + rds_rdma_cookie_offset(args->cookie);
  734. return ret;
  735. err:
  736. if (page)
  737. put_page(page);
  738. rm->atomic.op_active = 0;
  739. kfree(rm->atomic.op_notifier);
  740. return ret;
  741. }