rdma.c 18 KB

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