rdma.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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/rbtree.h>
  35. #include <linux/dma-mapping.h> /* for DMA_*_DEVICE */
  36. #include "rdma.h"
  37. /*
  38. * XXX
  39. * - build with sparse
  40. * - should we limit the size of a mr region? let transport return failure?
  41. * - should we detect duplicate keys on a socket? hmm.
  42. * - an rdma is an mlock, apply rlimit?
  43. */
  44. /*
  45. * get the number of pages by looking at the page indices that the start and
  46. * end addresses fall in.
  47. *
  48. * Returns 0 if the vec is invalid. It is invalid if the number of bytes
  49. * causes the address to wrap or overflows an unsigned int. This comes
  50. * from being stored in the 'length' member of 'struct scatterlist'.
  51. */
  52. static unsigned int rds_pages_in_vec(struct rds_iovec *vec)
  53. {
  54. if ((vec->addr + vec->bytes <= vec->addr) ||
  55. (vec->bytes > (u64)UINT_MAX))
  56. return 0;
  57. return ((vec->addr + vec->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT) -
  58. (vec->addr >> PAGE_SHIFT);
  59. }
  60. static struct rds_mr *rds_mr_tree_walk(struct rb_root *root, u64 key,
  61. struct rds_mr *insert)
  62. {
  63. struct rb_node **p = &root->rb_node;
  64. struct rb_node *parent = NULL;
  65. struct rds_mr *mr;
  66. while (*p) {
  67. parent = *p;
  68. mr = rb_entry(parent, struct rds_mr, r_rb_node);
  69. if (key < mr->r_key)
  70. p = &(*p)->rb_left;
  71. else if (key > mr->r_key)
  72. p = &(*p)->rb_right;
  73. else
  74. return mr;
  75. }
  76. if (insert) {
  77. rb_link_node(&insert->r_rb_node, parent, p);
  78. rb_insert_color(&insert->r_rb_node, root);
  79. atomic_inc(&insert->r_refcount);
  80. }
  81. return NULL;
  82. }
  83. /*
  84. * Destroy the transport-specific part of a MR.
  85. */
  86. static void rds_destroy_mr(struct rds_mr *mr)
  87. {
  88. struct rds_sock *rs = mr->r_sock;
  89. void *trans_private = NULL;
  90. unsigned long flags;
  91. rdsdebug("RDS: destroy mr key is %x refcnt %u\n",
  92. mr->r_key, atomic_read(&mr->r_refcount));
  93. if (test_and_set_bit(RDS_MR_DEAD, &mr->r_state))
  94. return;
  95. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  96. if (!RB_EMPTY_NODE(&mr->r_rb_node))
  97. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  98. trans_private = mr->r_trans_private;
  99. mr->r_trans_private = NULL;
  100. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  101. if (trans_private)
  102. mr->r_trans->free_mr(trans_private, mr->r_invalidate);
  103. }
  104. void __rds_put_mr_final(struct rds_mr *mr)
  105. {
  106. rds_destroy_mr(mr);
  107. kfree(mr);
  108. }
  109. /*
  110. * By the time this is called we can't have any more ioctls called on
  111. * the socket so we don't need to worry about racing with others.
  112. */
  113. void rds_rdma_drop_keys(struct rds_sock *rs)
  114. {
  115. struct rds_mr *mr;
  116. struct rb_node *node;
  117. /* Release any MRs associated with this socket */
  118. while ((node = rb_first(&rs->rs_rdma_keys))) {
  119. mr = container_of(node, struct rds_mr, r_rb_node);
  120. if (mr->r_trans == rs->rs_transport)
  121. mr->r_invalidate = 0;
  122. rds_mr_put(mr);
  123. }
  124. if (rs->rs_transport && rs->rs_transport->flush_mrs)
  125. rs->rs_transport->flush_mrs();
  126. }
  127. /*
  128. * Helper function to pin user pages.
  129. */
  130. static int rds_pin_pages(unsigned long user_addr, unsigned int nr_pages,
  131. struct page **pages, int write)
  132. {
  133. int ret;
  134. ret = get_user_pages_fast(user_addr, nr_pages, write, pages);
  135. if (ret >= 0 && ret < nr_pages) {
  136. while (ret--)
  137. put_page(pages[ret]);
  138. ret = -EFAULT;
  139. }
  140. return ret;
  141. }
  142. static int __rds_rdma_map(struct rds_sock *rs, struct rds_get_mr_args *args,
  143. u64 *cookie_ret, struct rds_mr **mr_ret)
  144. {
  145. struct rds_mr *mr = NULL, *found;
  146. unsigned int nr_pages;
  147. struct page **pages = NULL;
  148. struct scatterlist *sg;
  149. void *trans_private;
  150. unsigned long flags;
  151. rds_rdma_cookie_t cookie;
  152. unsigned int nents;
  153. long i;
  154. int ret;
  155. if (rs->rs_bound_addr == 0) {
  156. ret = -ENOTCONN; /* XXX not a great errno */
  157. goto out;
  158. }
  159. if (rs->rs_transport->get_mr == NULL) {
  160. ret = -EOPNOTSUPP;
  161. goto out;
  162. }
  163. nr_pages = rds_pages_in_vec(&args->vec);
  164. if (nr_pages == 0) {
  165. ret = -EINVAL;
  166. goto out;
  167. }
  168. rdsdebug("RDS: get_mr addr %llx len %llu nr_pages %u\n",
  169. args->vec.addr, args->vec.bytes, nr_pages);
  170. /* XXX clamp nr_pages to limit the size of this alloc? */
  171. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  172. if (pages == NULL) {
  173. ret = -ENOMEM;
  174. goto out;
  175. }
  176. mr = kzalloc(sizeof(struct rds_mr), GFP_KERNEL);
  177. if (mr == NULL) {
  178. ret = -ENOMEM;
  179. goto out;
  180. }
  181. atomic_set(&mr->r_refcount, 1);
  182. RB_CLEAR_NODE(&mr->r_rb_node);
  183. mr->r_trans = rs->rs_transport;
  184. mr->r_sock = rs;
  185. if (args->flags & RDS_RDMA_USE_ONCE)
  186. mr->r_use_once = 1;
  187. if (args->flags & RDS_RDMA_INVALIDATE)
  188. mr->r_invalidate = 1;
  189. if (args->flags & RDS_RDMA_READWRITE)
  190. mr->r_write = 1;
  191. /*
  192. * Pin the pages that make up the user buffer and transfer the page
  193. * pointers to the mr's sg array. We check to see if we've mapped
  194. * the whole region after transferring the partial page references
  195. * to the sg array so that we can have one page ref cleanup path.
  196. *
  197. * For now we have no flag that tells us whether the mapping is
  198. * r/o or r/w. We need to assume r/w, or we'll do a lot of RDMA to
  199. * the zero page.
  200. */
  201. ret = rds_pin_pages(args->vec.addr & PAGE_MASK, nr_pages, pages, 1);
  202. if (ret < 0)
  203. goto out;
  204. nents = ret;
  205. sg = kcalloc(nents, sizeof(*sg), GFP_KERNEL);
  206. if (sg == NULL) {
  207. ret = -ENOMEM;
  208. goto out;
  209. }
  210. WARN_ON(!nents);
  211. sg_init_table(sg, nents);
  212. /* Stick all pages into the scatterlist */
  213. for (i = 0 ; i < nents; i++)
  214. sg_set_page(&sg[i], pages[i], PAGE_SIZE, 0);
  215. rdsdebug("RDS: trans_private nents is %u\n", nents);
  216. /* Obtain a transport specific MR. If this succeeds, the
  217. * s/g list is now owned by the MR.
  218. * Note that dma_map() implies that pending writes are
  219. * flushed to RAM, so no dma_sync is needed here. */
  220. trans_private = rs->rs_transport->get_mr(sg, nents, rs,
  221. &mr->r_key);
  222. if (IS_ERR(trans_private)) {
  223. for (i = 0 ; i < nents; i++)
  224. put_page(sg_page(&sg[i]));
  225. kfree(sg);
  226. ret = PTR_ERR(trans_private);
  227. goto out;
  228. }
  229. mr->r_trans_private = trans_private;
  230. rdsdebug("RDS: get_mr put_user key is %x cookie_addr %p\n",
  231. mr->r_key, (void *)(unsigned long) args->cookie_addr);
  232. /* The user may pass us an unaligned address, but we can only
  233. * map page aligned regions. So we keep the offset, and build
  234. * a 64bit cookie containing <R_Key, offset> and pass that
  235. * around. */
  236. cookie = rds_rdma_make_cookie(mr->r_key, args->vec.addr & ~PAGE_MASK);
  237. if (cookie_ret)
  238. *cookie_ret = cookie;
  239. if (args->cookie_addr && put_user(cookie, (u64 __user *)(unsigned long) args->cookie_addr)) {
  240. ret = -EFAULT;
  241. goto out;
  242. }
  243. /* Inserting the new MR into the rbtree bumps its
  244. * reference count. */
  245. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  246. found = rds_mr_tree_walk(&rs->rs_rdma_keys, mr->r_key, mr);
  247. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  248. BUG_ON(found && found != mr);
  249. rdsdebug("RDS: get_mr key is %x\n", mr->r_key);
  250. if (mr_ret) {
  251. atomic_inc(&mr->r_refcount);
  252. *mr_ret = mr;
  253. }
  254. ret = 0;
  255. out:
  256. kfree(pages);
  257. if (mr)
  258. rds_mr_put(mr);
  259. return ret;
  260. }
  261. int rds_get_mr(struct rds_sock *rs, char __user *optval, int optlen)
  262. {
  263. struct rds_get_mr_args args;
  264. if (optlen != sizeof(struct rds_get_mr_args))
  265. return -EINVAL;
  266. if (copy_from_user(&args, (struct rds_get_mr_args __user *)optval,
  267. sizeof(struct rds_get_mr_args)))
  268. return -EFAULT;
  269. return __rds_rdma_map(rs, &args, NULL, NULL);
  270. }
  271. int rds_get_mr_for_dest(struct rds_sock *rs, char __user *optval, int optlen)
  272. {
  273. struct rds_get_mr_for_dest_args args;
  274. struct rds_get_mr_args new_args;
  275. if (optlen != sizeof(struct rds_get_mr_for_dest_args))
  276. return -EINVAL;
  277. if (copy_from_user(&args, (struct rds_get_mr_for_dest_args __user *)optval,
  278. sizeof(struct rds_get_mr_for_dest_args)))
  279. return -EFAULT;
  280. /*
  281. * Initially, just behave like get_mr().
  282. * TODO: Implement get_mr as wrapper around this
  283. * and deprecate it.
  284. */
  285. new_args.vec = args.vec;
  286. new_args.cookie_addr = args.cookie_addr;
  287. new_args.flags = args.flags;
  288. return __rds_rdma_map(rs, &new_args, NULL, NULL);
  289. }
  290. /*
  291. * Free the MR indicated by the given R_Key
  292. */
  293. int rds_free_mr(struct rds_sock *rs, char __user *optval, int optlen)
  294. {
  295. struct rds_free_mr_args args;
  296. struct rds_mr *mr;
  297. unsigned long flags;
  298. if (optlen != sizeof(struct rds_free_mr_args))
  299. return -EINVAL;
  300. if (copy_from_user(&args, (struct rds_free_mr_args __user *)optval,
  301. sizeof(struct rds_free_mr_args)))
  302. return -EFAULT;
  303. /* Special case - a null cookie means flush all unused MRs */
  304. if (args.cookie == 0) {
  305. if (!rs->rs_transport || !rs->rs_transport->flush_mrs)
  306. return -EINVAL;
  307. rs->rs_transport->flush_mrs();
  308. return 0;
  309. }
  310. /* Look up the MR given its R_key and remove it from the rbtree
  311. * so nobody else finds it.
  312. * This should also prevent races with rds_rdma_unuse.
  313. */
  314. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  315. mr = rds_mr_tree_walk(&rs->rs_rdma_keys, rds_rdma_cookie_key(args.cookie), NULL);
  316. if (mr) {
  317. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  318. RB_CLEAR_NODE(&mr->r_rb_node);
  319. if (args.flags & RDS_RDMA_INVALIDATE)
  320. mr->r_invalidate = 1;
  321. }
  322. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  323. if (!mr)
  324. return -EINVAL;
  325. /*
  326. * call rds_destroy_mr() ourselves so that we're sure it's done by the time
  327. * we return. If we let rds_mr_put() do it it might not happen until
  328. * someone else drops their ref.
  329. */
  330. rds_destroy_mr(mr);
  331. rds_mr_put(mr);
  332. return 0;
  333. }
  334. /*
  335. * This is called when we receive an extension header that
  336. * tells us this MR was used. It allows us to implement
  337. * use_once semantics
  338. */
  339. void rds_rdma_unuse(struct rds_sock *rs, u32 r_key, int force)
  340. {
  341. struct rds_mr *mr;
  342. unsigned long flags;
  343. int zot_me = 0;
  344. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  345. mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
  346. if (mr && (mr->r_use_once || force)) {
  347. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  348. RB_CLEAR_NODE(&mr->r_rb_node);
  349. zot_me = 1;
  350. } else if (mr)
  351. atomic_inc(&mr->r_refcount);
  352. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  353. /* May have to issue a dma_sync on this memory region.
  354. * Note we could avoid this if the operation was a RDMA READ,
  355. * but at this point we can't tell. */
  356. if (mr != NULL) {
  357. if (mr->r_trans->sync_mr)
  358. mr->r_trans->sync_mr(mr->r_trans_private, DMA_FROM_DEVICE);
  359. /* If the MR was marked as invalidate, this will
  360. * trigger an async flush. */
  361. if (zot_me)
  362. rds_destroy_mr(mr);
  363. rds_mr_put(mr);
  364. }
  365. }
  366. void rds_rdma_free_op(struct rds_rdma_op *ro)
  367. {
  368. unsigned int i;
  369. for (i = 0; i < ro->r_nents; i++) {
  370. struct page *page = sg_page(&ro->r_sg[i]);
  371. /* Mark page dirty if it was possibly modified, which
  372. * is the case for a RDMA_READ which copies from remote
  373. * to local memory */
  374. if (!ro->r_write) {
  375. BUG_ON(in_interrupt());
  376. set_page_dirty(page);
  377. }
  378. put_page(page);
  379. }
  380. kfree(ro->r_notifier);
  381. kfree(ro);
  382. }
  383. /*
  384. * args is a pointer to an in-kernel copy in the sendmsg cmsg.
  385. */
  386. static struct rds_rdma_op *rds_rdma_prepare(struct rds_sock *rs,
  387. struct rds_rdma_args *args)
  388. {
  389. struct rds_iovec vec;
  390. struct rds_rdma_op *op = NULL;
  391. unsigned int nr_pages;
  392. unsigned int max_pages;
  393. unsigned int nr_bytes;
  394. struct page **pages = NULL;
  395. struct rds_iovec __user *local_vec;
  396. struct scatterlist *sg;
  397. unsigned int nr;
  398. unsigned int i, j;
  399. int ret;
  400. if (rs->rs_bound_addr == 0) {
  401. ret = -ENOTCONN; /* XXX not a great errno */
  402. goto out;
  403. }
  404. if (args->nr_local > (u64)UINT_MAX) {
  405. ret = -EMSGSIZE;
  406. goto out;
  407. }
  408. nr_pages = 0;
  409. max_pages = 0;
  410. local_vec = (struct rds_iovec __user *)(unsigned long) args->local_vec_addr;
  411. /* figure out the number of pages in the vector */
  412. for (i = 0; i < args->nr_local; i++) {
  413. if (copy_from_user(&vec, &local_vec[i],
  414. sizeof(struct rds_iovec))) {
  415. ret = -EFAULT;
  416. goto out;
  417. }
  418. nr = rds_pages_in_vec(&vec);
  419. if (nr == 0) {
  420. ret = -EINVAL;
  421. goto out;
  422. }
  423. max_pages = max(nr, max_pages);
  424. nr_pages += nr;
  425. }
  426. pages = kcalloc(max_pages, sizeof(struct page *), GFP_KERNEL);
  427. if (pages == NULL) {
  428. ret = -ENOMEM;
  429. goto out;
  430. }
  431. op = kzalloc(offsetof(struct rds_rdma_op, r_sg[nr_pages]), GFP_KERNEL);
  432. if (op == NULL) {
  433. ret = -ENOMEM;
  434. goto out;
  435. }
  436. op->r_write = !!(args->flags & RDS_RDMA_READWRITE);
  437. op->r_fence = !!(args->flags & RDS_RDMA_FENCE);
  438. op->r_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME);
  439. op->r_recverr = rs->rs_recverr;
  440. WARN_ON(!nr_pages);
  441. sg_init_table(op->r_sg, nr_pages);
  442. if (op->r_notify || op->r_recverr) {
  443. /* We allocate an uninitialized notifier here, because
  444. * we don't want to do that in the completion handler. We
  445. * would have to use GFP_ATOMIC there, and don't want to deal
  446. * with failed allocations.
  447. */
  448. op->r_notifier = kmalloc(sizeof(struct rds_notifier), GFP_KERNEL);
  449. if (!op->r_notifier) {
  450. ret = -ENOMEM;
  451. goto out;
  452. }
  453. op->r_notifier->n_user_token = args->user_token;
  454. op->r_notifier->n_status = RDS_RDMA_SUCCESS;
  455. }
  456. /* The cookie contains the R_Key of the remote memory region, and
  457. * optionally an offset into it. This is how we implement RDMA into
  458. * unaligned memory.
  459. * When setting up the RDMA, we need to add that offset to the
  460. * destination address (which is really an offset into the MR)
  461. * FIXME: We may want to move this into ib_rdma.c
  462. */
  463. op->r_key = rds_rdma_cookie_key(args->cookie);
  464. op->r_remote_addr = args->remote_vec.addr + rds_rdma_cookie_offset(args->cookie);
  465. nr_bytes = 0;
  466. rdsdebug("RDS: rdma prepare nr_local %llu rva %llx rkey %x\n",
  467. (unsigned long long)args->nr_local,
  468. (unsigned long long)args->remote_vec.addr,
  469. op->r_key);
  470. for (i = 0; i < args->nr_local; i++) {
  471. if (copy_from_user(&vec, &local_vec[i],
  472. sizeof(struct rds_iovec))) {
  473. ret = -EFAULT;
  474. goto out;
  475. }
  476. nr = rds_pages_in_vec(&vec);
  477. if (nr == 0) {
  478. ret = -EINVAL;
  479. goto out;
  480. }
  481. rs->rs_user_addr = vec.addr;
  482. rs->rs_user_bytes = vec.bytes;
  483. /* did the user change the vec under us? */
  484. if (nr > max_pages || op->r_nents + nr > nr_pages) {
  485. ret = -EINVAL;
  486. goto out;
  487. }
  488. /* If it's a WRITE operation, we want to pin the pages for reading.
  489. * If it's a READ operation, we need to pin the pages for writing.
  490. */
  491. ret = rds_pin_pages(vec.addr & PAGE_MASK, nr, pages, !op->r_write);
  492. if (ret < 0)
  493. goto out;
  494. rdsdebug("RDS: nr_bytes %u nr %u vec.bytes %llu vec.addr %llx\n",
  495. nr_bytes, nr, vec.bytes, vec.addr);
  496. nr_bytes += vec.bytes;
  497. for (j = 0; j < nr; j++) {
  498. unsigned int offset = vec.addr & ~PAGE_MASK;
  499. sg = &op->r_sg[op->r_nents + j];
  500. sg_set_page(sg, pages[j],
  501. min_t(unsigned int, vec.bytes, PAGE_SIZE - offset),
  502. offset);
  503. rdsdebug("RDS: sg->offset %x sg->len %x vec.addr %llx vec.bytes %llu\n",
  504. sg->offset, sg->length, vec.addr, vec.bytes);
  505. vec.addr += sg->length;
  506. vec.bytes -= sg->length;
  507. }
  508. op->r_nents += nr;
  509. }
  510. if (nr_bytes > args->remote_vec.bytes) {
  511. rdsdebug("RDS nr_bytes %u remote_bytes %u do not match\n",
  512. nr_bytes,
  513. (unsigned int) args->remote_vec.bytes);
  514. ret = -EINVAL;
  515. goto out;
  516. }
  517. op->r_bytes = nr_bytes;
  518. ret = 0;
  519. out:
  520. kfree(pages);
  521. if (ret) {
  522. if (op)
  523. rds_rdma_free_op(op);
  524. op = ERR_PTR(ret);
  525. }
  526. return op;
  527. }
  528. /*
  529. * The application asks for a RDMA transfer.
  530. * Extract all arguments and set up the rdma_op
  531. */
  532. int rds_cmsg_rdma_args(struct rds_sock *rs, struct rds_message *rm,
  533. struct cmsghdr *cmsg)
  534. {
  535. struct rds_rdma_op *op;
  536. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_rdma_args)) ||
  537. rm->m_rdma_op != NULL)
  538. return -EINVAL;
  539. op = rds_rdma_prepare(rs, CMSG_DATA(cmsg));
  540. if (IS_ERR(op))
  541. return PTR_ERR(op);
  542. rds_stats_inc(s_send_rdma);
  543. rm->m_rdma_op = op;
  544. return 0;
  545. }
  546. /*
  547. * The application wants us to pass an RDMA destination (aka MR)
  548. * to the remote
  549. */
  550. int rds_cmsg_rdma_dest(struct rds_sock *rs, struct rds_message *rm,
  551. struct cmsghdr *cmsg)
  552. {
  553. unsigned long flags;
  554. struct rds_mr *mr;
  555. u32 r_key;
  556. int err = 0;
  557. if (cmsg->cmsg_len < CMSG_LEN(sizeof(rds_rdma_cookie_t)) ||
  558. rm->m_rdma_cookie != 0)
  559. return -EINVAL;
  560. memcpy(&rm->m_rdma_cookie, CMSG_DATA(cmsg), sizeof(rm->m_rdma_cookie));
  561. /* We are reusing a previously mapped MR here. Most likely, the
  562. * application has written to the buffer, so we need to explicitly
  563. * flush those writes to RAM. Otherwise the HCA may not see them
  564. * when doing a DMA from that buffer.
  565. */
  566. r_key = rds_rdma_cookie_key(rm->m_rdma_cookie);
  567. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  568. mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
  569. if (mr == NULL)
  570. err = -EINVAL; /* invalid r_key */
  571. else
  572. atomic_inc(&mr->r_refcount);
  573. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  574. if (mr) {
  575. mr->r_trans->sync_mr(mr->r_trans_private, DMA_TO_DEVICE);
  576. rm->m_rdma_mr = mr;
  577. }
  578. return err;
  579. }
  580. /*
  581. * The application passes us an address range it wants to enable RDMA
  582. * to/from. We map the area, and save the <R_Key,offset> pair
  583. * in rm->m_rdma_cookie. This causes it to be sent along to the peer
  584. * in an extension header.
  585. */
  586. int rds_cmsg_rdma_map(struct rds_sock *rs, struct rds_message *rm,
  587. struct cmsghdr *cmsg)
  588. {
  589. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_get_mr_args)) ||
  590. rm->m_rdma_cookie != 0)
  591. return -EINVAL;
  592. return __rds_rdma_map(rs, CMSG_DATA(cmsg), &rm->m_rdma_cookie, &rm->m_rdma_mr);
  593. }