2 * Copyright (c) 2015 Oracle. All rights reserved.
3 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
6 /* Lightweight memory registration using Fast Registration Work
7 * Requests (FRWR). Also referred to sometimes as FRMR mode.
9 * FRWR features ordered asynchronous registration and deregistration
10 * of arbitrarily sized memory regions. This is the fastest and safest
11 * but most complex memory registration mode.
16 * A Memory Region is prepared for RDMA READ or WRITE using a FAST_REG
17 * Work Request (frmr_op_map). When the RDMA operation is finished, this
18 * Memory Region is invalidated using a LOCAL_INV Work Request
21 * Typically these Work Requests are not signaled, and neither are RDMA
22 * SEND Work Requests (with the exception of signaling occasionally to
23 * prevent provider work queue overflows). This greatly reduces HCA
26 * As an optimization, frwr_op_unmap marks MRs INVALID before the
27 * LOCAL_INV WR is posted. If posting succeeds, the MR is placed on
28 * rb_mws immediately so that no work (like managing a linked list
29 * under a spinlock) is needed in the completion upcall.
31 * But this means that frwr_op_map() can occasionally encounter an MR
32 * that is INVALID but the LOCAL_INV WR has not completed. Work Queue
33 * ordering prevents a subsequent FAST_REG WR from executing against
34 * that MR while it is still being invalidated.
39 * ->op_map and the transport connect worker cannot run at the same
40 * time, but ->op_unmap can fire while the transport connect worker
41 * is running. Thus MR recovery is handled in ->op_map, to guarantee
42 * that recovered MRs are owned by a sending RPC, and not one where
43 * ->op_unmap could fire at the same time transport reconnect is
46 * When the underlying transport disconnects, MRs are left in one of
49 * INVALID: The MR was not in use before the QP entered ERROR state.
51 * VALID: The MR was registered before the QP entered ERROR state.
53 * FLUSHED_FR: The MR was being registered when the QP entered ERROR
54 * state, and the pending WR was flushed.
56 * FLUSHED_LI: The MR was being invalidated when the QP entered ERROR
57 * state, and the pending WR was flushed.
59 * When frwr_op_map encounters FLUSHED and VALID MRs, they are recovered
60 * with ib_dereg_mr and then are re-initialized. Because MR recovery
61 * allocates fresh resources, it is deferred to a workqueue, and the
62 * recovered MRs are placed back on the rb_mws list when recovery is
63 * complete. frwr_op_map allocates another MR for the current RPC while
64 * the broken MR is reset.
66 * To ensure that frwr_op_map doesn't encounter an MR that is marked
67 * INVALID but that is about to be flushed due to a previous transport
68 * disconnect, the transport connect worker attempts to drain all
69 * pending send queue WRs before the transport is reconnected.
72 #include <linux/sunrpc/rpc_rdma.h>
74 #include "xprt_rdma.h"
76 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
77 # define RPCDBG_FACILITY RPCDBG_TRANS
81 frwr_is_supported(struct rpcrdma_ia
*ia
)
83 struct ib_device_attr
*attrs
= &ia
->ri_device
->attrs
;
85 if (!(attrs
->device_cap_flags
& IB_DEVICE_MEM_MGT_EXTENSIONS
))
86 goto out_not_supported
;
87 if (attrs
->max_fast_reg_page_list_len
== 0)
88 goto out_not_supported
;
92 pr_info("rpcrdma: 'frwr' mode is not supported by device %s\n",
98 frwr_op_init_mr(struct rpcrdma_ia
*ia
, struct rpcrdma_mw
*r
)
100 unsigned int depth
= ia
->ri_max_frmr_depth
;
101 struct rpcrdma_frmr
*f
= &r
->frmr
;
104 f
->fr_mr
= ib_alloc_mr(ia
->ri_pd
, ia
->ri_mrtype
, depth
);
105 if (IS_ERR(f
->fr_mr
))
108 r
->mw_sg
= kcalloc(depth
, sizeof(*r
->mw_sg
), GFP_KERNEL
);
112 sg_init_table(r
->mw_sg
, depth
);
113 init_completion(&f
->fr_linv_done
);
117 rc
= PTR_ERR(f
->fr_mr
);
118 dprintk("RPC: %s: ib_alloc_mr status %i\n",
124 dprintk("RPC: %s: sg allocation failure\n",
126 ib_dereg_mr(f
->fr_mr
);
131 frwr_op_release_mr(struct rpcrdma_mw
*r
)
135 /* Ensure MW is not on any rl_registered list */
136 if (!list_empty(&r
->mw_list
))
137 list_del(&r
->mw_list
);
139 rc
= ib_dereg_mr(r
->frmr
.fr_mr
);
141 pr_err("rpcrdma: final ib_dereg_mr for %p returned %i\n",
148 __frwr_reset_mr(struct rpcrdma_ia
*ia
, struct rpcrdma_mw
*r
)
150 struct rpcrdma_frmr
*f
= &r
->frmr
;
153 rc
= ib_dereg_mr(f
->fr_mr
);
155 pr_warn("rpcrdma: ib_dereg_mr status %d, frwr %p orphaned\n",
160 f
->fr_mr
= ib_alloc_mr(ia
->ri_pd
, ia
->ri_mrtype
,
161 ia
->ri_max_frmr_depth
);
162 if (IS_ERR(f
->fr_mr
)) {
163 pr_warn("rpcrdma: ib_alloc_mr status %ld, frwr %p orphaned\n",
164 PTR_ERR(f
->fr_mr
), r
);
165 return PTR_ERR(f
->fr_mr
);
168 dprintk("RPC: %s: recovered FRMR %p\n", __func__
, f
);
169 f
->fr_state
= FRMR_IS_INVALID
;
173 /* Reset of a single FRMR. Generate a fresh rkey by replacing the MR.
176 frwr_op_recover_mr(struct rpcrdma_mw
*mw
)
178 enum rpcrdma_frmr_state state
= mw
->frmr
.fr_state
;
179 struct rpcrdma_xprt
*r_xprt
= mw
->mw_xprt
;
180 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
183 rc
= __frwr_reset_mr(ia
, mw
);
184 if (state
!= FRMR_FLUSHED_LI
)
185 ib_dma_unmap_sg(ia
->ri_device
,
186 mw
->mw_sg
, mw
->mw_nents
, mw
->mw_dir
);
190 rpcrdma_put_mw(r_xprt
, mw
);
191 r_xprt
->rx_stats
.mrs_recovered
++;
195 pr_err("rpcrdma: FRMR reset failed %d, %p release\n", rc
, mw
);
196 r_xprt
->rx_stats
.mrs_orphaned
++;
198 spin_lock(&r_xprt
->rx_buf
.rb_mwlock
);
199 list_del(&mw
->mw_all
);
200 spin_unlock(&r_xprt
->rx_buf
.rb_mwlock
);
202 frwr_op_release_mr(mw
);
206 frwr_op_open(struct rpcrdma_ia
*ia
, struct rpcrdma_ep
*ep
,
207 struct rpcrdma_create_data_internal
*cdata
)
209 struct ib_device_attr
*attrs
= &ia
->ri_device
->attrs
;
212 ia
->ri_mrtype
= IB_MR_TYPE_MEM_REG
;
213 if (attrs
->device_cap_flags
& IB_DEVICE_SG_GAPS_REG
)
214 ia
->ri_mrtype
= IB_MR_TYPE_SG_GAPS
;
216 ia
->ri_max_frmr_depth
=
217 min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS
,
218 attrs
->max_fast_reg_page_list_len
);
219 dprintk("RPC: %s: device's max FR page list len = %u\n",
220 __func__
, ia
->ri_max_frmr_depth
);
222 /* Add room for frmr register and invalidate WRs.
223 * 1. FRMR reg WR for head
224 * 2. FRMR invalidate WR for head
225 * 3. N FRMR reg WRs for pagelist
226 * 4. N FRMR invalidate WRs for pagelist
227 * 5. FRMR reg WR for tail
228 * 6. FRMR invalidate WR for tail
229 * 7. The RDMA_SEND WR
233 /* Calculate N if the device max FRMR depth is smaller than
234 * RPCRDMA_MAX_DATA_SEGS.
236 if (ia
->ri_max_frmr_depth
< RPCRDMA_MAX_DATA_SEGS
) {
237 delta
= RPCRDMA_MAX_DATA_SEGS
- ia
->ri_max_frmr_depth
;
239 depth
+= 2; /* FRMR reg + invalidate */
240 delta
-= ia
->ri_max_frmr_depth
;
244 ep
->rep_attr
.cap
.max_send_wr
*= depth
;
245 if (ep
->rep_attr
.cap
.max_send_wr
> attrs
->max_qp_wr
) {
246 cdata
->max_requests
= attrs
->max_qp_wr
/ depth
;
247 if (!cdata
->max_requests
)
249 ep
->rep_attr
.cap
.max_send_wr
= cdata
->max_requests
*
253 ia
->ri_max_segs
= max_t(unsigned int, 1, RPCRDMA_MAX_DATA_SEGS
/
254 ia
->ri_max_frmr_depth
);
258 /* FRWR mode conveys a list of pages per chunk segment. The
259 * maximum length of that list is the FRWR page list depth.
262 frwr_op_maxpages(struct rpcrdma_xprt
*r_xprt
)
264 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
266 return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS
,
267 RPCRDMA_MAX_HDR_SEGS
* ia
->ri_max_frmr_depth
);
271 __frwr_sendcompletion_flush(struct ib_wc
*wc
, const char *wr
)
273 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
274 pr_err("rpcrdma: %s: %s (%u/0x%x)\n",
275 wr
, ib_wc_status_msg(wc
->status
),
276 wc
->status
, wc
->vendor_err
);
280 * frwr_wc_fastreg - Invoked by RDMA provider for each polled FastReg WC
281 * @cq: completion queue (ignored)
286 frwr_wc_fastreg(struct ib_cq
*cq
, struct ib_wc
*wc
)
288 struct rpcrdma_frmr
*frmr
;
291 /* WARNING: Only wr_cqe and status are reliable at this point */
292 if (wc
->status
!= IB_WC_SUCCESS
) {
294 frmr
= container_of(cqe
, struct rpcrdma_frmr
, fr_cqe
);
295 frmr
->fr_state
= FRMR_FLUSHED_FR
;
296 __frwr_sendcompletion_flush(wc
, "fastreg");
301 * frwr_wc_localinv - Invoked by RDMA provider for each polled LocalInv WC
302 * @cq: completion queue (ignored)
307 frwr_wc_localinv(struct ib_cq
*cq
, struct ib_wc
*wc
)
309 struct rpcrdma_frmr
*frmr
;
312 /* WARNING: Only wr_cqe and status are reliable at this point */
313 if (wc
->status
!= IB_WC_SUCCESS
) {
315 frmr
= container_of(cqe
, struct rpcrdma_frmr
, fr_cqe
);
316 frmr
->fr_state
= FRMR_FLUSHED_LI
;
317 __frwr_sendcompletion_flush(wc
, "localinv");
322 * frwr_wc_localinv - Invoked by RDMA provider for each polled LocalInv WC
323 * @cq: completion queue (ignored)
326 * Awaken anyone waiting for an MR to finish being fenced.
329 frwr_wc_localinv_wake(struct ib_cq
*cq
, struct ib_wc
*wc
)
331 struct rpcrdma_frmr
*frmr
;
334 /* WARNING: Only wr_cqe and status are reliable at this point */
336 frmr
= container_of(cqe
, struct rpcrdma_frmr
, fr_cqe
);
337 if (wc
->status
!= IB_WC_SUCCESS
) {
338 frmr
->fr_state
= FRMR_FLUSHED_LI
;
339 __frwr_sendcompletion_flush(wc
, "localinv");
341 complete(&frmr
->fr_linv_done
);
344 /* Post a REG_MR Work Request to register a memory region
345 * for remote access via RDMA READ or RDMA WRITE.
348 frwr_op_map(struct rpcrdma_xprt
*r_xprt
, struct rpcrdma_mr_seg
*seg
,
349 int nsegs
, bool writing
, struct rpcrdma_mw
**out
)
351 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
352 bool holes_ok
= ia
->ri_mrtype
== IB_MR_TYPE_SG_GAPS
;
353 struct rpcrdma_mw
*mw
;
354 struct rpcrdma_frmr
*frmr
;
356 struct ib_reg_wr
*reg_wr
;
357 struct ib_send_wr
*bad_wr
;
358 int rc
, i
, n
, dma_nents
;
364 rpcrdma_defer_mr_recovery(mw
);
365 mw
= rpcrdma_get_mw(r_xprt
);
368 } while (mw
->frmr
.fr_state
!= FRMR_IS_INVALID
);
370 frmr
->fr_state
= FRMR_IS_VALID
;
372 reg_wr
= &frmr
->fr_regwr
;
374 if (nsegs
> ia
->ri_max_frmr_depth
)
375 nsegs
= ia
->ri_max_frmr_depth
;
376 for (i
= 0; i
< nsegs
;) {
378 sg_set_page(&mw
->mw_sg
[i
],
381 offset_in_page(seg
->mr_offset
));
383 sg_set_buf(&mw
->mw_sg
[i
], seg
->mr_offset
,
390 if ((i
< nsegs
&& offset_in_page(seg
->mr_offset
)) ||
391 offset_in_page((seg
-1)->mr_offset
+ (seg
-1)->mr_len
))
395 mw
->mw_dir
= rpcrdma_data_dir(writing
);
399 dma_nents
= ib_dma_map_sg(ia
->ri_device
,
400 mw
->mw_sg
, mw
->mw_nents
, mw
->mw_dir
);
404 n
= ib_map_mr_sg(mr
, mw
->mw_sg
, mw
->mw_nents
, NULL
, PAGE_SIZE
);
405 if (unlikely(n
!= mw
->mw_nents
))
408 dprintk("RPC: %s: Using frmr %p to map %u segments (%u bytes)\n",
409 __func__
, frmr
, mw
->mw_nents
, mr
->length
);
411 key
= (u8
)(mr
->rkey
& 0x000000FF);
412 ib_update_fast_reg_key(mr
, ++key
);
414 reg_wr
->wr
.next
= NULL
;
415 reg_wr
->wr
.opcode
= IB_WR_REG_MR
;
416 frmr
->fr_cqe
.done
= frwr_wc_fastreg
;
417 reg_wr
->wr
.wr_cqe
= &frmr
->fr_cqe
;
418 reg_wr
->wr
.num_sge
= 0;
419 reg_wr
->wr
.send_flags
= 0;
421 reg_wr
->key
= mr
->rkey
;
422 reg_wr
->access
= writing
?
423 IB_ACCESS_REMOTE_WRITE
| IB_ACCESS_LOCAL_WRITE
:
424 IB_ACCESS_REMOTE_READ
;
426 rpcrdma_set_signaled(&r_xprt
->rx_ep
, ®_wr
->wr
);
427 rc
= ib_post_send(ia
->ri_id
->qp
, ®_wr
->wr
, &bad_wr
);
431 mw
->mw_handle
= mr
->rkey
;
432 mw
->mw_length
= mr
->length
;
433 mw
->mw_offset
= mr
->iova
;
439 pr_err("rpcrdma: failed to dma map sg %p sg_nents %u\n",
440 mw
->mw_sg
, mw
->mw_nents
);
441 rpcrdma_defer_mr_recovery(mw
);
445 pr_err("rpcrdma: failed to map mr %p (%u/%u)\n",
446 frmr
->fr_mr
, n
, mw
->mw_nents
);
447 rpcrdma_defer_mr_recovery(mw
);
451 pr_err("rpcrdma: FRMR registration ib_post_send returned %i\n", rc
);
452 rpcrdma_defer_mr_recovery(mw
);
456 /* Invalidate all memory regions that were registered for "req".
458 * Sleeps until it is safe for the host CPU to access the
459 * previously mapped memory regions.
461 * Caller ensures that req->rl_registered is not empty.
464 frwr_op_unmap_sync(struct rpcrdma_xprt
*r_xprt
, struct rpcrdma_req
*req
)
466 struct ib_send_wr
*first
, **prev
, *last
, *bad_wr
;
467 struct rpcrdma_rep
*rep
= req
->rl_reply
;
468 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
469 struct rpcrdma_mw
*mw
, *tmp
;
470 struct rpcrdma_frmr
*f
;
473 dprintk("RPC: %s: req %p\n", __func__
, req
);
475 /* ORDER: Invalidate all of the req's MRs first
477 * Chain the LOCAL_INV Work Requests and post them with
478 * a single ib_post_send() call.
483 list_for_each_entry(mw
, &req
->rl_registered
, mw_list
) {
484 mw
->frmr
.fr_state
= FRMR_IS_INVALID
;
486 if ((rep
->rr_wc_flags
& IB_WC_WITH_INVALIDATE
) &&
487 (mw
->mw_handle
== rep
->rr_inv_rkey
))
491 dprintk("RPC: %s: invalidating frmr %p\n",
494 f
->fr_cqe
.done
= frwr_wc_localinv
;
496 memset(last
, 0, sizeof(*last
));
497 last
->wr_cqe
= &f
->fr_cqe
;
498 last
->opcode
= IB_WR_LOCAL_INV
;
499 last
->ex
.invalidate_rkey
= mw
->mw_handle
;
508 /* Strong send queue ordering guarantees that when the
509 * last WR in the chain completes, all WRs in the chain
512 last
->send_flags
= IB_SEND_SIGNALED
;
513 f
->fr_cqe
.done
= frwr_wc_localinv_wake
;
514 reinit_completion(&f
->fr_linv_done
);
516 /* Initialize CQ count, since there is always a signaled
517 * WR being posted here. The new cqcount depends on how
518 * many SQEs are about to be consumed.
520 rpcrdma_init_cqcount(&r_xprt
->rx_ep
, count
);
522 /* Transport disconnect drains the receive CQ before it
523 * replaces the QP. The RPC reply handler won't call us
524 * unless ri_id->qp is a valid pointer.
526 r_xprt
->rx_stats
.local_inv_needed
++;
527 rc
= ib_post_send(ia
->ri_id
->qp
, first
, &bad_wr
);
531 wait_for_completion(&f
->fr_linv_done
);
533 /* ORDER: Now DMA unmap all of the req's MRs, and return
534 * them to the free MW list.
537 list_for_each_entry_safe(mw
, tmp
, &req
->rl_registered
, mw_list
) {
538 dprintk("RPC: %s: DMA unmapping frmr %p\n",
539 __func__
, &mw
->frmr
);
540 list_del_init(&mw
->mw_list
);
541 ib_dma_unmap_sg(ia
->ri_device
,
542 mw
->mw_sg
, mw
->mw_nents
, mw
->mw_dir
);
543 rpcrdma_put_mw(r_xprt
, mw
);
548 pr_err("rpcrdma: FRMR invalidate ib_post_send returned %i\n", rc
);
549 rdma_disconnect(ia
->ri_id
);
551 /* Find and reset the MRs in the LOCAL_INV WRs that did not
552 * get posted. This is synchronous, and slow.
554 list_for_each_entry(mw
, &req
->rl_registered
, mw_list
) {
556 if (mw
->mw_handle
== bad_wr
->ex
.invalidate_rkey
) {
557 __frwr_reset_mr(ia
, mw
);
558 bad_wr
= bad_wr
->next
;
564 /* Use a slow, safe mechanism to invalidate all memory regions
565 * that were registered for "req".
568 frwr_op_unmap_safe(struct rpcrdma_xprt
*r_xprt
, struct rpcrdma_req
*req
,
571 struct rpcrdma_mw
*mw
;
573 while (!list_empty(&req
->rl_registered
)) {
574 mw
= list_first_entry(&req
->rl_registered
,
575 struct rpcrdma_mw
, mw_list
);
576 list_del_init(&mw
->mw_list
);
579 frwr_op_recover_mr(mw
);
581 rpcrdma_defer_mr_recovery(mw
);
585 const struct rpcrdma_memreg_ops rpcrdma_frwr_memreg_ops
= {
586 .ro_map
= frwr_op_map
,
587 .ro_unmap_sync
= frwr_op_unmap_sync
,
588 .ro_unmap_safe
= frwr_op_unmap_safe
,
589 .ro_recover_mr
= frwr_op_recover_mr
,
590 .ro_open
= frwr_op_open
,
591 .ro_maxpages
= frwr_op_maxpages
,
592 .ro_init_mr
= frwr_op_init_mr
,
593 .ro_release_mr
= frwr_op_release_mr
,
594 .ro_displayname
= "frwr",
595 .ro_send_w_inv_ok
= RPCRDMA_CMP_F_SND_W_INV_OK
,