frwr_ops.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright (c) 2015 Oracle. All rights reserved.
  3. * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
  4. */
  5. /* Lightweight memory registration using Fast Registration Work
  6. * Requests (FRWR). Also referred to sometimes as FRMR mode.
  7. *
  8. * FRWR features ordered asynchronous registration and deregistration
  9. * of arbitrarily sized memory regions. This is the fastest and safest
  10. * but most complex memory registration mode.
  11. */
  12. /* Normal operation
  13. *
  14. * A Memory Region is prepared for RDMA READ or WRITE using a FAST_REG
  15. * Work Request (frmr_op_map). When the RDMA operation is finished, this
  16. * Memory Region is invalidated using a LOCAL_INV Work Request
  17. * (frmr_op_unmap).
  18. *
  19. * Typically these Work Requests are not signaled, and neither are RDMA
  20. * SEND Work Requests (with the exception of signaling occasionally to
  21. * prevent provider work queue overflows). This greatly reduces HCA
  22. * interrupt workload.
  23. *
  24. * As an optimization, frwr_op_unmap marks MRs INVALID before the
  25. * LOCAL_INV WR is posted. If posting succeeds, the MR is placed on
  26. * rb_mws immediately so that no work (like managing a linked list
  27. * under a spinlock) is needed in the completion upcall.
  28. *
  29. * But this means that frwr_op_map() can occasionally encounter an MR
  30. * that is INVALID but the LOCAL_INV WR has not completed. Work Queue
  31. * ordering prevents a subsequent FAST_REG WR from executing against
  32. * that MR while it is still being invalidated.
  33. */
  34. /* Transport recovery
  35. *
  36. * ->op_map and the transport connect worker cannot run at the same
  37. * time, but ->op_unmap can fire while the transport connect worker
  38. * is running. Thus MR recovery is handled in ->op_map, to guarantee
  39. * that recovered MRs are owned by a sending RPC, and not one where
  40. * ->op_unmap could fire at the same time transport reconnect is
  41. * being done.
  42. *
  43. * When the underlying transport disconnects, MRs are left in one of
  44. * three states:
  45. *
  46. * INVALID: The MR was not in use before the QP entered ERROR state.
  47. * (Or, the LOCAL_INV WR has not completed or flushed yet).
  48. *
  49. * STALE: The MR was being registered or unregistered when the QP
  50. * entered ERROR state, and the pending WR was flushed.
  51. *
  52. * VALID: The MR was registered before the QP entered ERROR state.
  53. *
  54. * When frwr_op_map encounters STALE and VALID MRs, they are recovered
  55. * with ib_dereg_mr and then are re-initialized. Beause MR recovery
  56. * allocates fresh resources, it is deferred to a workqueue, and the
  57. * recovered MRs are placed back on the rb_mws list when recovery is
  58. * complete. frwr_op_map allocates another MR for the current RPC while
  59. * the broken MR is reset.
  60. *
  61. * To ensure that frwr_op_map doesn't encounter an MR that is marked
  62. * INVALID but that is about to be flushed due to a previous transport
  63. * disconnect, the transport connect worker attempts to drain all
  64. * pending send queue WRs before the transport is reconnected.
  65. */
  66. #include "xprt_rdma.h"
  67. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  68. # define RPCDBG_FACILITY RPCDBG_TRANS
  69. #endif
  70. static struct workqueue_struct *frwr_recovery_wq;
  71. #define FRWR_RECOVERY_WQ_FLAGS (WQ_UNBOUND | WQ_MEM_RECLAIM)
  72. int
  73. frwr_alloc_recovery_wq(void)
  74. {
  75. frwr_recovery_wq = alloc_workqueue("frwr_recovery",
  76. FRWR_RECOVERY_WQ_FLAGS, 0);
  77. return !frwr_recovery_wq ? -ENOMEM : 0;
  78. }
  79. void
  80. frwr_destroy_recovery_wq(void)
  81. {
  82. struct workqueue_struct *wq;
  83. if (!frwr_recovery_wq)
  84. return;
  85. wq = frwr_recovery_wq;
  86. frwr_recovery_wq = NULL;
  87. destroy_workqueue(wq);
  88. }
  89. /* Deferred reset of a single FRMR. Generate a fresh rkey by
  90. * replacing the MR.
  91. *
  92. * There's no recovery if this fails. The FRMR is abandoned, but
  93. * remains in rb_all. It will be cleaned up when the transport is
  94. * destroyed.
  95. */
  96. static void
  97. __frwr_recovery_worker(struct work_struct *work)
  98. {
  99. struct rpcrdma_mw *r = container_of(work, struct rpcrdma_mw,
  100. r.frmr.fr_work);
  101. struct rpcrdma_xprt *r_xprt = r->r.frmr.fr_xprt;
  102. unsigned int depth = r_xprt->rx_ia.ri_max_frmr_depth;
  103. struct ib_pd *pd = r_xprt->rx_ia.ri_pd;
  104. if (ib_dereg_mr(r->r.frmr.fr_mr))
  105. goto out_fail;
  106. r->r.frmr.fr_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG, depth);
  107. if (IS_ERR(r->r.frmr.fr_mr))
  108. goto out_fail;
  109. dprintk("RPC: %s: recovered FRMR %p\n", __func__, r);
  110. r->r.frmr.fr_state = FRMR_IS_INVALID;
  111. rpcrdma_put_mw(r_xprt, r);
  112. return;
  113. out_fail:
  114. pr_warn("RPC: %s: FRMR %p unrecovered\n",
  115. __func__, r);
  116. }
  117. /* A broken MR was discovered in a context that can't sleep.
  118. * Defer recovery to the recovery worker.
  119. */
  120. static void
  121. __frwr_queue_recovery(struct rpcrdma_mw *r)
  122. {
  123. INIT_WORK(&r->r.frmr.fr_work, __frwr_recovery_worker);
  124. queue_work(frwr_recovery_wq, &r->r.frmr.fr_work);
  125. }
  126. static int
  127. __frwr_init(struct rpcrdma_mw *r, struct ib_pd *pd, struct ib_device *device,
  128. unsigned int depth)
  129. {
  130. struct rpcrdma_frmr *f = &r->r.frmr;
  131. int rc;
  132. f->fr_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG, depth);
  133. if (IS_ERR(f->fr_mr))
  134. goto out_mr_err;
  135. f->sg = kcalloc(depth, sizeof(*f->sg), GFP_KERNEL);
  136. if (!f->sg)
  137. goto out_list_err;
  138. sg_init_table(f->sg, depth);
  139. return 0;
  140. out_mr_err:
  141. rc = PTR_ERR(f->fr_mr);
  142. dprintk("RPC: %s: ib_alloc_mr status %i\n",
  143. __func__, rc);
  144. return rc;
  145. out_list_err:
  146. rc = -ENOMEM;
  147. dprintk("RPC: %s: sg allocation failure\n",
  148. __func__);
  149. ib_dereg_mr(f->fr_mr);
  150. return rc;
  151. }
  152. static void
  153. __frwr_release(struct rpcrdma_mw *r)
  154. {
  155. int rc;
  156. rc = ib_dereg_mr(r->r.frmr.fr_mr);
  157. if (rc)
  158. dprintk("RPC: %s: ib_dereg_mr status %i\n",
  159. __func__, rc);
  160. kfree(r->r.frmr.sg);
  161. }
  162. static int
  163. frwr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep,
  164. struct rpcrdma_create_data_internal *cdata)
  165. {
  166. struct ib_device_attr *devattr = &ia->ri_devattr;
  167. int depth, delta;
  168. ia->ri_max_frmr_depth =
  169. min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
  170. devattr->max_fast_reg_page_list_len);
  171. dprintk("RPC: %s: device's max FR page list len = %u\n",
  172. __func__, ia->ri_max_frmr_depth);
  173. /* Add room for frmr register and invalidate WRs.
  174. * 1. FRMR reg WR for head
  175. * 2. FRMR invalidate WR for head
  176. * 3. N FRMR reg WRs for pagelist
  177. * 4. N FRMR invalidate WRs for pagelist
  178. * 5. FRMR reg WR for tail
  179. * 6. FRMR invalidate WR for tail
  180. * 7. The RDMA_SEND WR
  181. */
  182. depth = 7;
  183. /* Calculate N if the device max FRMR depth is smaller than
  184. * RPCRDMA_MAX_DATA_SEGS.
  185. */
  186. if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) {
  187. delta = RPCRDMA_MAX_DATA_SEGS - ia->ri_max_frmr_depth;
  188. do {
  189. depth += 2; /* FRMR reg + invalidate */
  190. delta -= ia->ri_max_frmr_depth;
  191. } while (delta > 0);
  192. }
  193. ep->rep_attr.cap.max_send_wr *= depth;
  194. if (ep->rep_attr.cap.max_send_wr > devattr->max_qp_wr) {
  195. cdata->max_requests = devattr->max_qp_wr / depth;
  196. if (!cdata->max_requests)
  197. return -EINVAL;
  198. ep->rep_attr.cap.max_send_wr = cdata->max_requests *
  199. depth;
  200. }
  201. return 0;
  202. }
  203. /* FRWR mode conveys a list of pages per chunk segment. The
  204. * maximum length of that list is the FRWR page list depth.
  205. */
  206. static size_t
  207. frwr_op_maxpages(struct rpcrdma_xprt *r_xprt)
  208. {
  209. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  210. return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
  211. rpcrdma_max_segments(r_xprt) * ia->ri_max_frmr_depth);
  212. }
  213. /* If FAST_REG or LOCAL_INV failed, indicate the frmr needs to be reset. */
  214. static void
  215. frwr_sendcompletion(struct ib_wc *wc)
  216. {
  217. struct rpcrdma_mw *r;
  218. if (likely(wc->status == IB_WC_SUCCESS))
  219. return;
  220. /* WARNING: Only wr_id and status are reliable at this point */
  221. r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
  222. if (wc->status == IB_WC_WR_FLUSH_ERR)
  223. dprintk("RPC: %s: frmr %p flushed\n", __func__, r);
  224. else
  225. pr_warn("RPC: %s: frmr %p error, status %s (%d)\n",
  226. __func__, r, ib_wc_status_msg(wc->status), wc->status);
  227. r->r.frmr.fr_state = FRMR_IS_STALE;
  228. }
  229. static int
  230. frwr_op_init(struct rpcrdma_xprt *r_xprt)
  231. {
  232. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  233. struct ib_device *device = r_xprt->rx_ia.ri_device;
  234. unsigned int depth = r_xprt->rx_ia.ri_max_frmr_depth;
  235. struct ib_pd *pd = r_xprt->rx_ia.ri_pd;
  236. int i;
  237. spin_lock_init(&buf->rb_mwlock);
  238. INIT_LIST_HEAD(&buf->rb_mws);
  239. INIT_LIST_HEAD(&buf->rb_all);
  240. i = max_t(int, RPCRDMA_MAX_DATA_SEGS / depth, 1);
  241. i += 2; /* head + tail */
  242. i *= buf->rb_max_requests; /* one set for each RPC slot */
  243. dprintk("RPC: %s: initalizing %d FRMRs\n", __func__, i);
  244. while (i--) {
  245. struct rpcrdma_mw *r;
  246. int rc;
  247. r = kzalloc(sizeof(*r), GFP_KERNEL);
  248. if (!r)
  249. return -ENOMEM;
  250. rc = __frwr_init(r, pd, device, depth);
  251. if (rc) {
  252. kfree(r);
  253. return rc;
  254. }
  255. list_add(&r->mw_list, &buf->rb_mws);
  256. list_add(&r->mw_all, &buf->rb_all);
  257. r->mw_sendcompletion = frwr_sendcompletion;
  258. r->r.frmr.fr_xprt = r_xprt;
  259. }
  260. return 0;
  261. }
  262. /* Post a FAST_REG Work Request to register a memory region
  263. * for remote access via RDMA READ or RDMA WRITE.
  264. */
  265. static int
  266. frwr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg,
  267. int nsegs, bool writing)
  268. {
  269. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  270. struct ib_device *device = ia->ri_device;
  271. enum dma_data_direction direction = rpcrdma_data_dir(writing);
  272. struct rpcrdma_mr_seg *seg1 = seg;
  273. struct rpcrdma_mw *mw;
  274. struct rpcrdma_frmr *frmr;
  275. struct ib_mr *mr;
  276. struct ib_reg_wr reg_wr;
  277. struct ib_send_wr *bad_wr;
  278. int rc, i, n, dma_nents;
  279. u8 key;
  280. mw = seg1->rl_mw;
  281. seg1->rl_mw = NULL;
  282. do {
  283. if (mw)
  284. __frwr_queue_recovery(mw);
  285. mw = rpcrdma_get_mw(r_xprt);
  286. if (!mw)
  287. return -ENOMEM;
  288. } while (mw->r.frmr.fr_state != FRMR_IS_INVALID);
  289. frmr = &mw->r.frmr;
  290. frmr->fr_state = FRMR_IS_VALID;
  291. mr = frmr->fr_mr;
  292. if (nsegs > ia->ri_max_frmr_depth)
  293. nsegs = ia->ri_max_frmr_depth;
  294. for (i = 0; i < nsegs;) {
  295. if (seg->mr_page)
  296. sg_set_page(&frmr->sg[i],
  297. seg->mr_page,
  298. seg->mr_len,
  299. offset_in_page(seg->mr_offset));
  300. else
  301. sg_set_buf(&frmr->sg[i], seg->mr_offset,
  302. seg->mr_len);
  303. ++seg;
  304. ++i;
  305. /* Check for holes */
  306. if ((i < nsegs && offset_in_page(seg->mr_offset)) ||
  307. offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
  308. break;
  309. }
  310. frmr->sg_nents = i;
  311. dma_nents = ib_dma_map_sg(device, frmr->sg, frmr->sg_nents, direction);
  312. if (!dma_nents) {
  313. pr_err("RPC: %s: failed to dma map sg %p sg_nents %u\n",
  314. __func__, frmr->sg, frmr->sg_nents);
  315. return -ENOMEM;
  316. }
  317. n = ib_map_mr_sg(mr, frmr->sg, frmr->sg_nents, PAGE_SIZE);
  318. if (unlikely(n != frmr->sg_nents)) {
  319. pr_err("RPC: %s: failed to map mr %p (%u/%u)\n",
  320. __func__, frmr->fr_mr, n, frmr->sg_nents);
  321. rc = n < 0 ? n : -EINVAL;
  322. goto out_senderr;
  323. }
  324. dprintk("RPC: %s: Using frmr %p to map %u segments (%u bytes)\n",
  325. __func__, mw, frmr->sg_nents, mr->length);
  326. key = (u8)(mr->rkey & 0x000000FF);
  327. ib_update_fast_reg_key(mr, ++key);
  328. reg_wr.wr.next = NULL;
  329. reg_wr.wr.opcode = IB_WR_REG_MR;
  330. reg_wr.wr.wr_id = (uintptr_t)mw;
  331. reg_wr.wr.num_sge = 0;
  332. reg_wr.wr.send_flags = 0;
  333. reg_wr.mr = mr;
  334. reg_wr.key = mr->rkey;
  335. reg_wr.access = writing ?
  336. IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
  337. IB_ACCESS_REMOTE_READ;
  338. DECR_CQCOUNT(&r_xprt->rx_ep);
  339. rc = ib_post_send(ia->ri_id->qp, &reg_wr.wr, &bad_wr);
  340. if (rc)
  341. goto out_senderr;
  342. seg1->mr_dir = direction;
  343. seg1->rl_mw = mw;
  344. seg1->mr_rkey = mr->rkey;
  345. seg1->mr_base = mr->iova;
  346. seg1->mr_nsegs = frmr->sg_nents;
  347. seg1->mr_len = mr->length;
  348. return frmr->sg_nents;
  349. out_senderr:
  350. dprintk("RPC: %s: ib_post_send status %i\n", __func__, rc);
  351. ib_dma_unmap_sg(device, frmr->sg, dma_nents, direction);
  352. __frwr_queue_recovery(mw);
  353. return rc;
  354. }
  355. /* Post a LOCAL_INV Work Request to prevent further remote access
  356. * via RDMA READ or RDMA WRITE.
  357. */
  358. static int
  359. frwr_op_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg)
  360. {
  361. struct rpcrdma_mr_seg *seg1 = seg;
  362. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  363. struct rpcrdma_mw *mw = seg1->rl_mw;
  364. struct rpcrdma_frmr *frmr = &mw->r.frmr;
  365. struct ib_send_wr invalidate_wr, *bad_wr;
  366. int rc, nsegs = seg->mr_nsegs;
  367. dprintk("RPC: %s: FRMR %p\n", __func__, mw);
  368. seg1->rl_mw = NULL;
  369. frmr->fr_state = FRMR_IS_INVALID;
  370. memset(&invalidate_wr, 0, sizeof(invalidate_wr));
  371. invalidate_wr.wr_id = (unsigned long)(void *)mw;
  372. invalidate_wr.opcode = IB_WR_LOCAL_INV;
  373. invalidate_wr.ex.invalidate_rkey = frmr->fr_mr->rkey;
  374. DECR_CQCOUNT(&r_xprt->rx_ep);
  375. ib_dma_unmap_sg(ia->ri_device, frmr->sg, frmr->sg_nents, seg1->mr_dir);
  376. read_lock(&ia->ri_qplock);
  377. rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
  378. read_unlock(&ia->ri_qplock);
  379. if (rc)
  380. goto out_err;
  381. rpcrdma_put_mw(r_xprt, mw);
  382. return nsegs;
  383. out_err:
  384. dprintk("RPC: %s: ib_post_send status %i\n", __func__, rc);
  385. __frwr_queue_recovery(mw);
  386. return nsegs;
  387. }
  388. static void
  389. frwr_op_destroy(struct rpcrdma_buffer *buf)
  390. {
  391. struct rpcrdma_mw *r;
  392. /* Ensure stale MWs for "buf" are no longer in flight */
  393. flush_workqueue(frwr_recovery_wq);
  394. while (!list_empty(&buf->rb_all)) {
  395. r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
  396. list_del(&r->mw_all);
  397. __frwr_release(r);
  398. kfree(r);
  399. }
  400. }
  401. const struct rpcrdma_memreg_ops rpcrdma_frwr_memreg_ops = {
  402. .ro_map = frwr_op_map,
  403. .ro_unmap = frwr_op_unmap,
  404. .ro_open = frwr_op_open,
  405. .ro_maxpages = frwr_op_maxpages,
  406. .ro_init = frwr_op_init,
  407. .ro_destroy = frwr_op_destroy,
  408. .ro_displayname = "frwr",
  409. };