rdma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * Copyright (c) 2007, 2020 Oracle and/or its affiliates.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/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 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. kref_get(&insert->r_kref);
  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, kref_read(&mr->r_kref));
  93. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  94. if (!RB_EMPTY_NODE(&mr->r_rb_node))
  95. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  96. trans_private = mr->r_trans_private;
  97. mr->r_trans_private = NULL;
  98. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  99. if (trans_private)
  100. mr->r_trans->free_mr(trans_private, mr->r_invalidate);
  101. }
  102. void __rds_put_mr_final(struct kref *kref)
  103. {
  104. struct rds_mr *mr = container_of(kref, struct rds_mr, r_kref);
  105. rds_destroy_mr(mr);
  106. kfree(mr);
  107. }
  108. /*
  109. * By the time this is called we can't have any more ioctls called on
  110. * the socket so we don't need to worry about racing with others.
  111. */
  112. void rds_rdma_drop_keys(struct rds_sock *rs)
  113. {
  114. struct rds_mr *mr;
  115. struct rb_node *node;
  116. unsigned long flags;
  117. /* Release any MRs associated with this socket */
  118. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  119. while ((node = rb_first(&rs->rs_rdma_keys))) {
  120. mr = rb_entry(node, struct rds_mr, r_rb_node);
  121. if (mr->r_trans == rs->rs_transport)
  122. mr->r_invalidate = 0;
  123. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  124. RB_CLEAR_NODE(&mr->r_rb_node);
  125. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  126. kref_put(&mr->r_kref, __rds_put_mr_final);
  127. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  128. }
  129. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  130. if (rs->rs_transport && rs->rs_transport->flush_mrs)
  131. rs->rs_transport->flush_mrs();
  132. }
  133. /*
  134. * Helper function to pin user pages.
  135. */
  136. static int rds_pin_pages(unsigned long user_addr, unsigned int nr_pages,
  137. struct page **pages, int write)
  138. {
  139. unsigned int gup_flags = FOLL_LONGTERM;
  140. int ret;
  141. if (write)
  142. gup_flags |= FOLL_WRITE;
  143. ret = pin_user_pages_fast(user_addr, nr_pages, gup_flags, pages);
  144. if (ret >= 0 && ret < nr_pages) {
  145. unpin_user_pages(pages, ret);
  146. ret = -EFAULT;
  147. }
  148. return ret;
  149. }
  150. static int __rds_rdma_map(struct rds_sock *rs, struct rds_get_mr_args *args,
  151. u64 *cookie_ret, struct rds_mr **mr_ret,
  152. struct rds_conn_path *cp)
  153. {
  154. struct rds_mr *mr = NULL, *found;
  155. struct scatterlist *sg = NULL;
  156. unsigned int nr_pages;
  157. struct page **pages = NULL;
  158. void *trans_private;
  159. unsigned long flags;
  160. rds_rdma_cookie_t cookie;
  161. unsigned int nents = 0;
  162. int need_odp = 0;
  163. long i;
  164. int ret;
  165. if (ipv6_addr_any(&rs->rs_bound_addr) || !rs->rs_transport) {
  166. ret = -ENOTCONN; /* XXX not a great errno */
  167. goto out;
  168. }
  169. if (!rs->rs_transport->get_mr) {
  170. ret = -EOPNOTSUPP;
  171. goto out;
  172. }
  173. /* If the combination of the addr and size requested for this memory
  174. * region causes an integer overflow, return error.
  175. */
  176. if (((args->vec.addr + args->vec.bytes) < args->vec.addr) ||
  177. PAGE_ALIGN(args->vec.addr + args->vec.bytes) <
  178. (args->vec.addr + args->vec.bytes)) {
  179. ret = -EINVAL;
  180. goto out;
  181. }
  182. if (!can_do_mlock()) {
  183. ret = -EPERM;
  184. goto out;
  185. }
  186. nr_pages = rds_pages_in_vec(&args->vec);
  187. if (nr_pages == 0) {
  188. ret = -EINVAL;
  189. goto out;
  190. }
  191. /* Restrict the size of mr irrespective of underlying transport
  192. * To account for unaligned mr regions, subtract one from nr_pages
  193. */
  194. if ((nr_pages - 1) > (RDS_MAX_MSG_SIZE >> PAGE_SHIFT)) {
  195. ret = -EMSGSIZE;
  196. goto out;
  197. }
  198. rdsdebug("RDS: get_mr addr %llx len %llu nr_pages %u\n",
  199. args->vec.addr, args->vec.bytes, nr_pages);
  200. /* XXX clamp nr_pages to limit the size of this alloc? */
  201. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  202. if (!pages) {
  203. ret = -ENOMEM;
  204. goto out;
  205. }
  206. mr = kzalloc(sizeof(struct rds_mr), GFP_KERNEL);
  207. if (!mr) {
  208. ret = -ENOMEM;
  209. goto out;
  210. }
  211. kref_init(&mr->r_kref);
  212. RB_CLEAR_NODE(&mr->r_rb_node);
  213. mr->r_trans = rs->rs_transport;
  214. mr->r_sock = rs;
  215. if (args->flags & RDS_RDMA_USE_ONCE)
  216. mr->r_use_once = 1;
  217. if (args->flags & RDS_RDMA_INVALIDATE)
  218. mr->r_invalidate = 1;
  219. if (args->flags & RDS_RDMA_READWRITE)
  220. mr->r_write = 1;
  221. /*
  222. * Pin the pages that make up the user buffer and transfer the page
  223. * pointers to the mr's sg array. We check to see if we've mapped
  224. * the whole region after transferring the partial page references
  225. * to the sg array so that we can have one page ref cleanup path.
  226. *
  227. * For now we have no flag that tells us whether the mapping is
  228. * r/o or r/w. We need to assume r/w, or we'll do a lot of RDMA to
  229. * the zero page.
  230. */
  231. ret = rds_pin_pages(args->vec.addr, nr_pages, pages, 1);
  232. if (ret == -EOPNOTSUPP) {
  233. need_odp = 1;
  234. } else if (ret <= 0) {
  235. goto out;
  236. } else {
  237. nents = ret;
  238. sg = kmalloc_array(nents, sizeof(*sg), GFP_KERNEL);
  239. if (!sg) {
  240. ret = -ENOMEM;
  241. goto out;
  242. }
  243. WARN_ON(!nents);
  244. sg_init_table(sg, nents);
  245. /* Stick all pages into the scatterlist */
  246. for (i = 0 ; i < nents; i++)
  247. sg_set_page(&sg[i], pages[i], PAGE_SIZE, 0);
  248. rdsdebug("RDS: trans_private nents is %u\n", nents);
  249. }
  250. /* Obtain a transport specific MR. If this succeeds, the
  251. * s/g list is now owned by the MR.
  252. * Note that dma_map() implies that pending writes are
  253. * flushed to RAM, so no dma_sync is needed here. */
  254. trans_private = rs->rs_transport->get_mr(
  255. sg, nents, rs, &mr->r_key, cp ? cp->cp_conn : NULL,
  256. args->vec.addr, args->vec.bytes,
  257. need_odp ? ODP_ZEROBASED : ODP_NOT_NEEDED);
  258. if (IS_ERR(trans_private)) {
  259. /* In ODP case, we don't GUP pages, so don't need
  260. * to release anything.
  261. */
  262. if (!need_odp) {
  263. unpin_user_pages(pages, nr_pages);
  264. kfree(sg);
  265. }
  266. ret = PTR_ERR(trans_private);
  267. /* Trigger connection so that its ready for the next retry */
  268. if (ret == -ENODEV && cp)
  269. rds_conn_connect_if_down(cp->cp_conn);
  270. goto out;
  271. }
  272. mr->r_trans_private = trans_private;
  273. rdsdebug("RDS: get_mr put_user key is %x cookie_addr %p\n",
  274. mr->r_key, (void *)(unsigned long) args->cookie_addr);
  275. /* The user may pass us an unaligned address, but we can only
  276. * map page aligned regions. So we keep the offset, and build
  277. * a 64bit cookie containing <R_Key, offset> and pass that
  278. * around. */
  279. if (need_odp)
  280. cookie = rds_rdma_make_cookie(mr->r_key, 0);
  281. else
  282. cookie = rds_rdma_make_cookie(mr->r_key,
  283. args->vec.addr & ~PAGE_MASK);
  284. if (cookie_ret)
  285. *cookie_ret = cookie;
  286. if (args->cookie_addr &&
  287. put_user(cookie, (u64 __user *)(unsigned long)args->cookie_addr)) {
  288. if (!need_odp) {
  289. unpin_user_pages(pages, nr_pages);
  290. kfree(sg);
  291. }
  292. ret = -EFAULT;
  293. goto out;
  294. }
  295. /* Inserting the new MR into the rbtree bumps its
  296. * reference count. */
  297. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  298. found = rds_mr_tree_walk(&rs->rs_rdma_keys, mr->r_key, mr);
  299. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  300. BUG_ON(found && found != mr);
  301. rdsdebug("RDS: get_mr key is %x\n", mr->r_key);
  302. if (mr_ret) {
  303. kref_get(&mr->r_kref);
  304. *mr_ret = mr;
  305. }
  306. ret = 0;
  307. out:
  308. kfree(pages);
  309. if (mr)
  310. kref_put(&mr->r_kref, __rds_put_mr_final);
  311. return ret;
  312. }
  313. int rds_get_mr(struct rds_sock *rs, sockptr_t optval, int optlen)
  314. {
  315. struct rds_get_mr_args args;
  316. if (optlen != sizeof(struct rds_get_mr_args))
  317. return -EINVAL;
  318. if (copy_from_sockptr(&args, optval, sizeof(struct rds_get_mr_args)))
  319. return -EFAULT;
  320. return __rds_rdma_map(rs, &args, NULL, NULL, NULL);
  321. }
  322. int rds_get_mr_for_dest(struct rds_sock *rs, sockptr_t optval, int optlen)
  323. {
  324. struct rds_get_mr_for_dest_args args;
  325. struct rds_get_mr_args new_args;
  326. if (optlen != sizeof(struct rds_get_mr_for_dest_args))
  327. return -EINVAL;
  328. if (copy_from_sockptr(&args, optval,
  329. sizeof(struct rds_get_mr_for_dest_args)))
  330. return -EFAULT;
  331. /*
  332. * Initially, just behave like get_mr().
  333. * TODO: Implement get_mr as wrapper around this
  334. * and deprecate it.
  335. */
  336. new_args.vec = args.vec;
  337. new_args.cookie_addr = args.cookie_addr;
  338. new_args.flags = args.flags;
  339. return __rds_rdma_map(rs, &new_args, NULL, NULL, NULL);
  340. }
  341. /*
  342. * Free the MR indicated by the given R_Key
  343. */
  344. int rds_free_mr(struct rds_sock *rs, sockptr_t optval, int optlen)
  345. {
  346. struct rds_free_mr_args args;
  347. struct rds_mr *mr;
  348. unsigned long flags;
  349. if (optlen != sizeof(struct rds_free_mr_args))
  350. return -EINVAL;
  351. if (copy_from_sockptr(&args, optval, sizeof(struct rds_free_mr_args)))
  352. return -EFAULT;
  353. /* Special case - a null cookie means flush all unused MRs */
  354. if (args.cookie == 0) {
  355. if (!rs->rs_transport || !rs->rs_transport->flush_mrs)
  356. return -EINVAL;
  357. rs->rs_transport->flush_mrs();
  358. return 0;
  359. }
  360. /* Look up the MR given its R_key and remove it from the rbtree
  361. * so nobody else finds it.
  362. * This should also prevent races with rds_rdma_unuse.
  363. */
  364. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  365. mr = rds_mr_tree_walk(&rs->rs_rdma_keys, rds_rdma_cookie_key(args.cookie), NULL);
  366. if (mr) {
  367. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  368. RB_CLEAR_NODE(&mr->r_rb_node);
  369. if (args.flags & RDS_RDMA_INVALIDATE)
  370. mr->r_invalidate = 1;
  371. }
  372. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  373. if (!mr)
  374. return -EINVAL;
  375. kref_put(&mr->r_kref, __rds_put_mr_final);
  376. return 0;
  377. }
  378. /*
  379. * This is called when we receive an extension header that
  380. * tells us this MR was used. It allows us to implement
  381. * use_once semantics
  382. */
  383. void rds_rdma_unuse(struct rds_sock *rs, u32 r_key, int force)
  384. {
  385. struct rds_mr *mr;
  386. unsigned long flags;
  387. int zot_me = 0;
  388. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  389. mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
  390. if (!mr) {
  391. pr_debug("rds: trying to unuse MR with unknown r_key %u!\n",
  392. r_key);
  393. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  394. return;
  395. }
  396. /* Get a reference so that the MR won't go away before calling
  397. * sync_mr() below.
  398. */
  399. kref_get(&mr->r_kref);
  400. /* If it is going to be freed, remove it from the tree now so
  401. * that no other thread can find it and free it.
  402. */
  403. if (mr->r_use_once || force) {
  404. rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
  405. RB_CLEAR_NODE(&mr->r_rb_node);
  406. zot_me = 1;
  407. }
  408. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  409. /* May have to issue a dma_sync on this memory region.
  410. * Note we could avoid this if the operation was a RDMA READ,
  411. * but at this point we can't tell. */
  412. if (mr->r_trans->sync_mr)
  413. mr->r_trans->sync_mr(mr->r_trans_private, DMA_FROM_DEVICE);
  414. /* Release the reference held above. */
  415. kref_put(&mr->r_kref, __rds_put_mr_final);
  416. /* If the MR was marked as invalidate, this will
  417. * trigger an async flush. */
  418. if (zot_me)
  419. kref_put(&mr->r_kref, __rds_put_mr_final);
  420. }
  421. void rds_rdma_free_op(struct rm_rdma_op *ro)
  422. {
  423. unsigned int i;
  424. if (ro->op_odp_mr) {
  425. kref_put(&ro->op_odp_mr->r_kref, __rds_put_mr_final);
  426. } else {
  427. for (i = 0; i < ro->op_nents; i++) {
  428. struct page *page = sg_page(&ro->op_sg[i]);
  429. /* Mark page dirty if it was possibly modified, which
  430. * is the case for a RDMA_READ which copies from remote
  431. * to local memory
  432. */
  433. unpin_user_pages_dirty_lock(&page, 1, !ro->op_write);
  434. }
  435. }
  436. kfree(ro->op_notifier);
  437. ro->op_notifier = NULL;
  438. ro->op_active = 0;
  439. ro->op_odp_mr = NULL;
  440. }
  441. void rds_atomic_free_op(struct rm_atomic_op *ao)
  442. {
  443. struct page *page = sg_page(ao->op_sg);
  444. /* Mark page dirty if it was possibly modified, which
  445. * is the case for a RDMA_READ which copies from remote
  446. * to local memory */
  447. unpin_user_pages_dirty_lock(&page, 1, true);
  448. kfree(ao->op_notifier);
  449. ao->op_notifier = NULL;
  450. ao->op_active = 0;
  451. }
  452. /*
  453. * Count the number of pages needed to describe an incoming iovec array.
  454. */
  455. static int rds_rdma_pages(struct rds_iovec iov[], int nr_iovecs)
  456. {
  457. int tot_pages = 0;
  458. unsigned int nr_pages;
  459. unsigned int i;
  460. /* figure out the number of pages in the vector */
  461. for (i = 0; i < nr_iovecs; i++) {
  462. nr_pages = rds_pages_in_vec(&iov[i]);
  463. if (nr_pages == 0)
  464. return -EINVAL;
  465. tot_pages += nr_pages;
  466. /*
  467. * nr_pages for one entry is limited to (UINT_MAX>>PAGE_SHIFT)+1,
  468. * so tot_pages cannot overflow without first going negative.
  469. */
  470. if (tot_pages < 0)
  471. return -EINVAL;
  472. }
  473. return tot_pages;
  474. }
  475. int rds_rdma_extra_size(struct rds_rdma_args *args,
  476. struct rds_iov_vector *iov)
  477. {
  478. struct rds_iovec *vec;
  479. struct rds_iovec __user *local_vec;
  480. int tot_pages = 0;
  481. unsigned int nr_pages;
  482. unsigned int i;
  483. local_vec = (struct rds_iovec __user *)(unsigned long) args->local_vec_addr;
  484. if (args->nr_local == 0)
  485. return -EINVAL;
  486. if (args->nr_local > UIO_MAXIOV)
  487. return -EMSGSIZE;
  488. iov->iov = kcalloc(args->nr_local,
  489. sizeof(struct rds_iovec),
  490. GFP_KERNEL);
  491. if (!iov->iov)
  492. return -ENOMEM;
  493. vec = &iov->iov[0];
  494. if (copy_from_user(vec, local_vec, args->nr_local *
  495. sizeof(struct rds_iovec)))
  496. return -EFAULT;
  497. iov->len = args->nr_local;
  498. /* figure out the number of pages in the vector */
  499. for (i = 0; i < args->nr_local; i++, vec++) {
  500. nr_pages = rds_pages_in_vec(vec);
  501. if (nr_pages == 0)
  502. return -EINVAL;
  503. tot_pages += nr_pages;
  504. /*
  505. * nr_pages for one entry is limited to (UINT_MAX>>PAGE_SHIFT)+1,
  506. * so tot_pages cannot overflow without first going negative.
  507. */
  508. if (tot_pages < 0)
  509. return -EINVAL;
  510. }
  511. return tot_pages * sizeof(struct scatterlist);
  512. }
  513. /*
  514. * The application asks for a RDMA transfer.
  515. * Extract all arguments and set up the rdma_op
  516. */
  517. int rds_cmsg_rdma_args(struct rds_sock *rs, struct rds_message *rm,
  518. struct cmsghdr *cmsg,
  519. struct rds_iov_vector *vec)
  520. {
  521. struct rds_rdma_args *args;
  522. struct rm_rdma_op *op = &rm->rdma;
  523. int nr_pages;
  524. unsigned int nr_bytes;
  525. struct page **pages = NULL;
  526. struct rds_iovec *iovs;
  527. unsigned int i, j;
  528. int ret = 0;
  529. bool odp_supported = true;
  530. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_rdma_args))
  531. || rm->rdma.op_active)
  532. return -EINVAL;
  533. args = CMSG_DATA(cmsg);
  534. if (ipv6_addr_any(&rs->rs_bound_addr)) {
  535. ret = -ENOTCONN; /* XXX not a great errno */
  536. goto out_ret;
  537. }
  538. if (args->nr_local > UIO_MAXIOV) {
  539. ret = -EMSGSIZE;
  540. goto out_ret;
  541. }
  542. if (vec->len != args->nr_local) {
  543. ret = -EINVAL;
  544. goto out_ret;
  545. }
  546. /* odp-mr is not supported for multiple requests within one message */
  547. if (args->nr_local != 1)
  548. odp_supported = false;
  549. iovs = vec->iov;
  550. nr_pages = rds_rdma_pages(iovs, args->nr_local);
  551. if (nr_pages < 0) {
  552. ret = -EINVAL;
  553. goto out_ret;
  554. }
  555. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  556. if (!pages) {
  557. ret = -ENOMEM;
  558. goto out_ret;
  559. }
  560. op->op_write = !!(args->flags & RDS_RDMA_READWRITE);
  561. op->op_fence = !!(args->flags & RDS_RDMA_FENCE);
  562. op->op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME);
  563. op->op_silent = !!(args->flags & RDS_RDMA_SILENT);
  564. op->op_active = 1;
  565. op->op_recverr = rs->rs_recverr;
  566. op->op_odp_mr = NULL;
  567. WARN_ON(!nr_pages);
  568. op->op_sg = rds_message_alloc_sgs(rm, nr_pages);
  569. if (IS_ERR(op->op_sg)) {
  570. ret = PTR_ERR(op->op_sg);
  571. goto out_pages;
  572. }
  573. if (op->op_notify || op->op_recverr) {
  574. /* We allocate an uninitialized notifier here, because
  575. * we don't want to do that in the completion handler. We
  576. * would have to use GFP_ATOMIC there, and don't want to deal
  577. * with failed allocations.
  578. */
  579. op->op_notifier = kmalloc(sizeof(struct rds_notifier), GFP_KERNEL);
  580. if (!op->op_notifier) {
  581. ret = -ENOMEM;
  582. goto out_pages;
  583. }
  584. op->op_notifier->n_user_token = args->user_token;
  585. op->op_notifier->n_status = RDS_RDMA_SUCCESS;
  586. }
  587. /* The cookie contains the R_Key of the remote memory region, and
  588. * optionally an offset into it. This is how we implement RDMA into
  589. * unaligned memory.
  590. * When setting up the RDMA, we need to add that offset to the
  591. * destination address (which is really an offset into the MR)
  592. * FIXME: We may want to move this into ib_rdma.c
  593. */
  594. op->op_rkey = rds_rdma_cookie_key(args->cookie);
  595. op->op_remote_addr = args->remote_vec.addr + rds_rdma_cookie_offset(args->cookie);
  596. nr_bytes = 0;
  597. rdsdebug("RDS: rdma prepare nr_local %llu rva %llx rkey %x\n",
  598. (unsigned long long)args->nr_local,
  599. (unsigned long long)args->remote_vec.addr,
  600. op->op_rkey);
  601. for (i = 0; i < args->nr_local; i++) {
  602. struct rds_iovec *iov = &iovs[i];
  603. /* don't need to check, rds_rdma_pages() verified nr will be +nonzero */
  604. unsigned int nr = rds_pages_in_vec(iov);
  605. rs->rs_user_addr = iov->addr;
  606. rs->rs_user_bytes = iov->bytes;
  607. /* If it's a WRITE operation, we want to pin the pages for reading.
  608. * If it's a READ operation, we need to pin the pages for writing.
  609. */
  610. ret = rds_pin_pages(iov->addr, nr, pages, !op->op_write);
  611. if ((!odp_supported && ret <= 0) ||
  612. (odp_supported && ret <= 0 && ret != -EOPNOTSUPP))
  613. goto out_pages;
  614. if (ret == -EOPNOTSUPP) {
  615. struct rds_mr *local_odp_mr;
  616. if (!rs->rs_transport->get_mr) {
  617. ret = -EOPNOTSUPP;
  618. goto out_pages;
  619. }
  620. local_odp_mr =
  621. kzalloc(sizeof(*local_odp_mr), GFP_KERNEL);
  622. if (!local_odp_mr) {
  623. ret = -ENOMEM;
  624. goto out_pages;
  625. }
  626. RB_CLEAR_NODE(&local_odp_mr->r_rb_node);
  627. kref_init(&local_odp_mr->r_kref);
  628. local_odp_mr->r_trans = rs->rs_transport;
  629. local_odp_mr->r_sock = rs;
  630. local_odp_mr->r_trans_private =
  631. rs->rs_transport->get_mr(
  632. NULL, 0, rs, &local_odp_mr->r_key, NULL,
  633. iov->addr, iov->bytes, ODP_VIRTUAL);
  634. if (IS_ERR(local_odp_mr->r_trans_private)) {
  635. ret = PTR_ERR(local_odp_mr->r_trans_private);
  636. rdsdebug("get_mr ret %d %p\"", ret,
  637. local_odp_mr->r_trans_private);
  638. kfree(local_odp_mr);
  639. ret = -EOPNOTSUPP;
  640. goto out_pages;
  641. }
  642. rdsdebug("Need odp; local_odp_mr %p trans_private %p\n",
  643. local_odp_mr, local_odp_mr->r_trans_private);
  644. op->op_odp_mr = local_odp_mr;
  645. op->op_odp_addr = iov->addr;
  646. }
  647. rdsdebug("RDS: nr_bytes %u nr %u iov->bytes %llu iov->addr %llx\n",
  648. nr_bytes, nr, iov->bytes, iov->addr);
  649. nr_bytes += iov->bytes;
  650. for (j = 0; j < nr; j++) {
  651. unsigned int offset = iov->addr & ~PAGE_MASK;
  652. struct scatterlist *sg;
  653. sg = &op->op_sg[op->op_nents + j];
  654. sg_set_page(sg, pages[j],
  655. min_t(unsigned int, iov->bytes, PAGE_SIZE - offset),
  656. offset);
  657. sg_dma_len(sg) = sg->length;
  658. rdsdebug("RDS: sg->offset %x sg->len %x iov->addr %llx iov->bytes %llu\n",
  659. sg->offset, sg->length, iov->addr, iov->bytes);
  660. iov->addr += sg->length;
  661. iov->bytes -= sg->length;
  662. }
  663. op->op_nents += nr;
  664. }
  665. if (nr_bytes > args->remote_vec.bytes) {
  666. rdsdebug("RDS nr_bytes %u remote_bytes %u do not match\n",
  667. nr_bytes,
  668. (unsigned int) args->remote_vec.bytes);
  669. ret = -EINVAL;
  670. goto out_pages;
  671. }
  672. op->op_bytes = nr_bytes;
  673. ret = 0;
  674. out_pages:
  675. kfree(pages);
  676. out_ret:
  677. if (ret)
  678. rds_rdma_free_op(op);
  679. else
  680. rds_stats_inc(s_send_rdma);
  681. return ret;
  682. }
  683. /*
  684. * The application wants us to pass an RDMA destination (aka MR)
  685. * to the remote
  686. */
  687. int rds_cmsg_rdma_dest(struct rds_sock *rs, struct rds_message *rm,
  688. struct cmsghdr *cmsg)
  689. {
  690. unsigned long flags;
  691. struct rds_mr *mr;
  692. u32 r_key;
  693. int err = 0;
  694. if (cmsg->cmsg_len < CMSG_LEN(sizeof(rds_rdma_cookie_t)) ||
  695. rm->m_rdma_cookie != 0)
  696. return -EINVAL;
  697. memcpy(&rm->m_rdma_cookie, CMSG_DATA(cmsg), sizeof(rm->m_rdma_cookie));
  698. /* We are reusing a previously mapped MR here. Most likely, the
  699. * application has written to the buffer, so we need to explicitly
  700. * flush those writes to RAM. Otherwise the HCA may not see them
  701. * when doing a DMA from that buffer.
  702. */
  703. r_key = rds_rdma_cookie_key(rm->m_rdma_cookie);
  704. spin_lock_irqsave(&rs->rs_rdma_lock, flags);
  705. mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
  706. if (!mr)
  707. err = -EINVAL; /* invalid r_key */
  708. else
  709. kref_get(&mr->r_kref);
  710. spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
  711. if (mr) {
  712. mr->r_trans->sync_mr(mr->r_trans_private,
  713. DMA_TO_DEVICE);
  714. rm->rdma.op_rdma_mr = mr;
  715. }
  716. return err;
  717. }
  718. /*
  719. * The application passes us an address range it wants to enable RDMA
  720. * to/from. We map the area, and save the <R_Key,offset> pair
  721. * in rm->m_rdma_cookie. This causes it to be sent along to the peer
  722. * in an extension header.
  723. */
  724. int rds_cmsg_rdma_map(struct rds_sock *rs, struct rds_message *rm,
  725. struct cmsghdr *cmsg)
  726. {
  727. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_get_mr_args)) ||
  728. rm->m_rdma_cookie != 0)
  729. return -EINVAL;
  730. return __rds_rdma_map(rs, CMSG_DATA(cmsg), &rm->m_rdma_cookie,
  731. &rm->rdma.op_rdma_mr, rm->m_conn_path);
  732. }
  733. /*
  734. * Fill in rds_message for an atomic request.
  735. */
  736. int rds_cmsg_atomic(struct rds_sock *rs, struct rds_message *rm,
  737. struct cmsghdr *cmsg)
  738. {
  739. struct page *page = NULL;
  740. struct rds_atomic_args *args;
  741. int ret = 0;
  742. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_atomic_args))
  743. || rm->atomic.op_active)
  744. return -EINVAL;
  745. args = CMSG_DATA(cmsg);
  746. /* Nonmasked & masked cmsg ops converted to masked hw ops */
  747. switch (cmsg->cmsg_type) {
  748. case RDS_CMSG_ATOMIC_FADD:
  749. rm->atomic.op_type = RDS_ATOMIC_TYPE_FADD;
  750. rm->atomic.op_m_fadd.add = args->fadd.add;
  751. rm->atomic.op_m_fadd.nocarry_mask = 0;
  752. break;
  753. case RDS_CMSG_MASKED_ATOMIC_FADD:
  754. rm->atomic.op_type = RDS_ATOMIC_TYPE_FADD;
  755. rm->atomic.op_m_fadd.add = args->m_fadd.add;
  756. rm->atomic.op_m_fadd.nocarry_mask = args->m_fadd.nocarry_mask;
  757. break;
  758. case RDS_CMSG_ATOMIC_CSWP:
  759. rm->atomic.op_type = RDS_ATOMIC_TYPE_CSWP;
  760. rm->atomic.op_m_cswp.compare = args->cswp.compare;
  761. rm->atomic.op_m_cswp.swap = args->cswp.swap;
  762. rm->atomic.op_m_cswp.compare_mask = ~0;
  763. rm->atomic.op_m_cswp.swap_mask = ~0;
  764. break;
  765. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  766. rm->atomic.op_type = RDS_ATOMIC_TYPE_CSWP;
  767. rm->atomic.op_m_cswp.compare = args->m_cswp.compare;
  768. rm->atomic.op_m_cswp.swap = args->m_cswp.swap;
  769. rm->atomic.op_m_cswp.compare_mask = args->m_cswp.compare_mask;
  770. rm->atomic.op_m_cswp.swap_mask = args->m_cswp.swap_mask;
  771. break;
  772. default:
  773. BUG(); /* should never happen */
  774. }
  775. rm->atomic.op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME);
  776. rm->atomic.op_silent = !!(args->flags & RDS_RDMA_SILENT);
  777. rm->atomic.op_active = 1;
  778. rm->atomic.op_recverr = rs->rs_recverr;
  779. rm->atomic.op_sg = rds_message_alloc_sgs(rm, 1);
  780. if (IS_ERR(rm->atomic.op_sg)) {
  781. ret = PTR_ERR(rm->atomic.op_sg);
  782. goto err;
  783. }
  784. /* verify 8 byte-aligned */
  785. if (args->local_addr & 0x7) {
  786. ret = -EFAULT;
  787. goto err;
  788. }
  789. ret = rds_pin_pages(args->local_addr, 1, &page, 1);
  790. if (ret != 1)
  791. goto err;
  792. ret = 0;
  793. sg_set_page(rm->atomic.op_sg, page, 8, offset_in_page(args->local_addr));
  794. if (rm->atomic.op_notify || rm->atomic.op_recverr) {
  795. /* We allocate an uninitialized notifier here, because
  796. * we don't want to do that in the completion handler. We
  797. * would have to use GFP_ATOMIC there, and don't want to deal
  798. * with failed allocations.
  799. */
  800. rm->atomic.op_notifier = kmalloc(sizeof(*rm->atomic.op_notifier), GFP_KERNEL);
  801. if (!rm->atomic.op_notifier) {
  802. ret = -ENOMEM;
  803. goto err;
  804. }
  805. rm->atomic.op_notifier->n_user_token = args->user_token;
  806. rm->atomic.op_notifier->n_status = RDS_RDMA_SUCCESS;
  807. }
  808. rm->atomic.op_rkey = rds_rdma_cookie_key(args->cookie);
  809. rm->atomic.op_remote_addr = args->remote_addr + rds_rdma_cookie_offset(args->cookie);
  810. return ret;
  811. err:
  812. if (page)
  813. unpin_user_page(page);
  814. rm->atomic.op_active = 0;
  815. kfree(rm->atomic.op_notifier);
  816. return ret;
  817. }