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 "xprt_rdma.h"
74 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
75 # define RPCDBG_FACILITY RPCDBG_TRANS
79 frwr_is_supported(struct rpcrdma_ia
*ia
)
81 struct ib_device_attr
*attrs
= &ia
->ri_device
->attrs
;
83 if (!(attrs
->device_cap_flags
& IB_DEVICE_MEM_MGT_EXTENSIONS
))
84 goto out_not_supported
;
85 if (attrs
->max_fast_reg_page_list_len
== 0)
86 goto out_not_supported
;
90 pr_info("rpcrdma: 'frwr' mode is not supported by device %s\n",
96 frwr_op_init_mr(struct rpcrdma_ia
*ia
, struct rpcrdma_mw
*r
)
98 unsigned int depth
= ia
->ri_max_frmr_depth
;
99 struct rpcrdma_frmr
*f
= &r
->frmr
;
102 f
->fr_mr
= ib_alloc_mr(ia
->ri_pd
, IB_MR_TYPE_MEM_REG
, depth
);
103 if (IS_ERR(f
->fr_mr
))
106 r
->mw_sg
= kcalloc(depth
, sizeof(*r
->mw_sg
), GFP_KERNEL
);
110 sg_init_table(r
->mw_sg
, depth
);
111 init_completion(&f
->fr_linv_done
);
115 rc
= PTR_ERR(f
->fr_mr
);
116 dprintk("RPC: %s: ib_alloc_mr status %i\n",
122 dprintk("RPC: %s: sg allocation failure\n",
124 ib_dereg_mr(f
->fr_mr
);
129 frwr_op_release_mr(struct rpcrdma_mw
*r
)
133 /* Ensure MW is not on any rl_registered list */
134 if (!list_empty(&r
->mw_list
))
135 list_del(&r
->mw_list
);
137 rc
= ib_dereg_mr(r
->frmr
.fr_mr
);
139 pr_err("rpcrdma: final ib_dereg_mr for %p returned %i\n",
146 __frwr_reset_mr(struct rpcrdma_ia
*ia
, struct rpcrdma_mw
*r
)
148 struct rpcrdma_frmr
*f
= &r
->frmr
;
151 rc
= ib_dereg_mr(f
->fr_mr
);
153 pr_warn("rpcrdma: ib_dereg_mr status %d, frwr %p orphaned\n",
158 f
->fr_mr
= ib_alloc_mr(ia
->ri_pd
, IB_MR_TYPE_MEM_REG
,
159 ia
->ri_max_frmr_depth
);
160 if (IS_ERR(f
->fr_mr
)) {
161 pr_warn("rpcrdma: ib_alloc_mr status %ld, frwr %p orphaned\n",
162 PTR_ERR(f
->fr_mr
), r
);
163 return PTR_ERR(f
->fr_mr
);
166 dprintk("RPC: %s: recovered FRMR %p\n", __func__
, r
);
167 f
->fr_state
= FRMR_IS_INVALID
;
171 /* Reset of a single FRMR. Generate a fresh rkey by replacing the MR.
173 * There's no recovery if this fails. The FRMR is abandoned, but
174 * remains in rb_all. It will be cleaned up when the transport is
178 frwr_op_recover_mr(struct rpcrdma_mw
*mw
)
180 enum rpcrdma_frmr_state state
= mw
->frmr
.fr_state
;
181 struct rpcrdma_xprt
*r_xprt
= mw
->mw_xprt
;
182 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
185 rc
= __frwr_reset_mr(ia
, mw
);
186 if (state
!= FRMR_FLUSHED_LI
)
187 ib_dma_unmap_sg(ia
->ri_device
,
188 mw
->mw_sg
, mw
->mw_nents
, mw
->mw_dir
);
192 rpcrdma_put_mw(r_xprt
, mw
);
193 r_xprt
->rx_stats
.mrs_recovered
++;
197 pr_err("rpcrdma: FRMR reset failed %d, %p release\n", rc
, mw
);
198 r_xprt
->rx_stats
.mrs_orphaned
++;
200 spin_lock(&r_xprt
->rx_buf
.rb_mwlock
);
201 list_del(&mw
->mw_all
);
202 spin_unlock(&r_xprt
->rx_buf
.rb_mwlock
);
204 frwr_op_release_mr(mw
);
208 frwr_op_open(struct rpcrdma_ia
*ia
, struct rpcrdma_ep
*ep
,
209 struct rpcrdma_create_data_internal
*cdata
)
213 ia
->ri_max_frmr_depth
=
214 min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS
,
215 ia
->ri_device
->attrs
.max_fast_reg_page_list_len
);
216 dprintk("RPC: %s: device's max FR page list len = %u\n",
217 __func__
, ia
->ri_max_frmr_depth
);
219 /* Add room for frmr register and invalidate WRs.
220 * 1. FRMR reg WR for head
221 * 2. FRMR invalidate WR for head
222 * 3. N FRMR reg WRs for pagelist
223 * 4. N FRMR invalidate WRs for pagelist
224 * 5. FRMR reg WR for tail
225 * 6. FRMR invalidate WR for tail
226 * 7. The RDMA_SEND WR
230 /* Calculate N if the device max FRMR depth is smaller than
231 * RPCRDMA_MAX_DATA_SEGS.
233 if (ia
->ri_max_frmr_depth
< RPCRDMA_MAX_DATA_SEGS
) {
234 delta
= RPCRDMA_MAX_DATA_SEGS
- ia
->ri_max_frmr_depth
;
236 depth
+= 2; /* FRMR reg + invalidate */
237 delta
-= ia
->ri_max_frmr_depth
;
241 ep
->rep_attr
.cap
.max_send_wr
*= depth
;
242 if (ep
->rep_attr
.cap
.max_send_wr
> ia
->ri_device
->attrs
.max_qp_wr
) {
243 cdata
->max_requests
= ia
->ri_device
->attrs
.max_qp_wr
/ depth
;
244 if (!cdata
->max_requests
)
246 ep
->rep_attr
.cap
.max_send_wr
= cdata
->max_requests
*
250 rpcrdma_set_max_header_sizes(ia
, cdata
, max_t(unsigned int, 1,
251 RPCRDMA_MAX_DATA_SEGS
/
252 ia
->ri_max_frmr_depth
));
256 /* FRWR mode conveys a list of pages per chunk segment. The
257 * maximum length of that list is the FRWR page list depth.
260 frwr_op_maxpages(struct rpcrdma_xprt
*r_xprt
)
262 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
264 return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS
,
265 RPCRDMA_MAX_HDR_SEGS
* ia
->ri_max_frmr_depth
);
269 __frwr_sendcompletion_flush(struct ib_wc
*wc
, const char *wr
)
271 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
272 pr_err("rpcrdma: %s: %s (%u/0x%x)\n",
273 wr
, ib_wc_status_msg(wc
->status
),
274 wc
->status
, wc
->vendor_err
);
278 * frwr_wc_fastreg - Invoked by RDMA provider for each polled FastReg WC
279 * @cq: completion queue (ignored)
284 frwr_wc_fastreg(struct ib_cq
*cq
, struct ib_wc
*wc
)
286 struct rpcrdma_frmr
*frmr
;
289 /* WARNING: Only wr_cqe and status are reliable at this point */
290 if (wc
->status
!= IB_WC_SUCCESS
) {
292 frmr
= container_of(cqe
, struct rpcrdma_frmr
, fr_cqe
);
293 frmr
->fr_state
= FRMR_FLUSHED_FR
;
294 __frwr_sendcompletion_flush(wc
, "fastreg");
299 * frwr_wc_localinv - Invoked by RDMA provider for each polled LocalInv WC
300 * @cq: completion queue (ignored)
305 frwr_wc_localinv(struct ib_cq
*cq
, struct ib_wc
*wc
)
307 struct rpcrdma_frmr
*frmr
;
310 /* WARNING: Only wr_cqe and status are reliable at this point */
311 if (wc
->status
!= IB_WC_SUCCESS
) {
313 frmr
= container_of(cqe
, struct rpcrdma_frmr
, fr_cqe
);
314 frmr
->fr_state
= FRMR_FLUSHED_LI
;
315 __frwr_sendcompletion_flush(wc
, "localinv");
320 * frwr_wc_localinv - Invoked by RDMA provider for each polled LocalInv WC
321 * @cq: completion queue (ignored)
324 * Awaken anyone waiting for an MR to finish being fenced.
327 frwr_wc_localinv_wake(struct ib_cq
*cq
, struct ib_wc
*wc
)
329 struct rpcrdma_frmr
*frmr
;
332 /* WARNING: Only wr_cqe and status are reliable at this point */
334 frmr
= container_of(cqe
, struct rpcrdma_frmr
, fr_cqe
);
335 if (wc
->status
!= IB_WC_SUCCESS
) {
336 frmr
->fr_state
= FRMR_FLUSHED_LI
;
337 __frwr_sendcompletion_flush(wc
, "localinv");
339 complete(&frmr
->fr_linv_done
);
342 /* Post a REG_MR Work Request to register a memory region
343 * for remote access via RDMA READ or RDMA WRITE.
346 frwr_op_map(struct rpcrdma_xprt
*r_xprt
, struct rpcrdma_mr_seg
*seg
,
347 int nsegs
, bool writing
, struct rpcrdma_mw
**out
)
349 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
350 struct rpcrdma_mw
*mw
;
351 struct rpcrdma_frmr
*frmr
;
353 struct ib_reg_wr
*reg_wr
;
354 struct ib_send_wr
*bad_wr
;
355 int rc
, i
, n
, dma_nents
;
361 rpcrdma_defer_mr_recovery(mw
);
362 mw
= rpcrdma_get_mw(r_xprt
);
365 } while (mw
->frmr
.fr_state
!= FRMR_IS_INVALID
);
367 frmr
->fr_state
= FRMR_IS_VALID
;
369 reg_wr
= &frmr
->fr_regwr
;
371 if (nsegs
> ia
->ri_max_frmr_depth
)
372 nsegs
= ia
->ri_max_frmr_depth
;
373 for (i
= 0; i
< nsegs
;) {
375 sg_set_page(&mw
->mw_sg
[i
],
378 offset_in_page(seg
->mr_offset
));
380 sg_set_buf(&mw
->mw_sg
[i
], seg
->mr_offset
,
386 /* Check for holes */
387 if ((i
< nsegs
&& offset_in_page(seg
->mr_offset
)) ||
388 offset_in_page((seg
-1)->mr_offset
+ (seg
-1)->mr_len
))
392 mw
->mw_dir
= rpcrdma_data_dir(writing
);
396 dma_nents
= ib_dma_map_sg(ia
->ri_device
,
397 mw
->mw_sg
, mw
->mw_nents
, mw
->mw_dir
);
401 n
= ib_map_mr_sg(mr
, mw
->mw_sg
, mw
->mw_nents
, NULL
, PAGE_SIZE
);
402 if (unlikely(n
!= mw
->mw_nents
))
405 dprintk("RPC: %s: Using frmr %p to map %u segments (%u bytes)\n",
406 __func__
, mw
, mw
->mw_nents
, mr
->length
);
408 key
= (u8
)(mr
->rkey
& 0x000000FF);
409 ib_update_fast_reg_key(mr
, ++key
);
411 reg_wr
->wr
.next
= NULL
;
412 reg_wr
->wr
.opcode
= IB_WR_REG_MR
;
413 frmr
->fr_cqe
.done
= frwr_wc_fastreg
;
414 reg_wr
->wr
.wr_cqe
= &frmr
->fr_cqe
;
415 reg_wr
->wr
.num_sge
= 0;
416 reg_wr
->wr
.send_flags
= 0;
418 reg_wr
->key
= mr
->rkey
;
419 reg_wr
->access
= writing
?
420 IB_ACCESS_REMOTE_WRITE
| IB_ACCESS_LOCAL_WRITE
:
421 IB_ACCESS_REMOTE_READ
;
423 DECR_CQCOUNT(&r_xprt
->rx_ep
);
424 rc
= ib_post_send(ia
->ri_id
->qp
, ®_wr
->wr
, &bad_wr
);
428 mw
->mw_handle
= mr
->rkey
;
429 mw
->mw_length
= mr
->length
;
430 mw
->mw_offset
= mr
->iova
;
436 pr_err("rpcrdma: failed to dma map sg %p sg_nents %u\n",
437 mw
->mw_sg
, mw
->mw_nents
);
438 rpcrdma_defer_mr_recovery(mw
);
442 pr_err("rpcrdma: failed to map mr %p (%u/%u)\n",
443 frmr
->fr_mr
, n
, mw
->mw_nents
);
444 rpcrdma_defer_mr_recovery(mw
);
448 pr_err("rpcrdma: FRMR registration ib_post_send returned %i\n", rc
);
449 rpcrdma_defer_mr_recovery(mw
);
453 static struct ib_send_wr
*
454 __frwr_prepare_linv_wr(struct rpcrdma_mw
*mw
)
456 struct rpcrdma_frmr
*f
= &mw
->frmr
;
457 struct ib_send_wr
*invalidate_wr
;
459 f
->fr_state
= FRMR_IS_INVALID
;
460 invalidate_wr
= &f
->fr_invwr
;
462 memset(invalidate_wr
, 0, sizeof(*invalidate_wr
));
463 f
->fr_cqe
.done
= frwr_wc_localinv
;
464 invalidate_wr
->wr_cqe
= &f
->fr_cqe
;
465 invalidate_wr
->opcode
= IB_WR_LOCAL_INV
;
466 invalidate_wr
->ex
.invalidate_rkey
= f
->fr_mr
->rkey
;
468 return invalidate_wr
;
471 /* Invalidate all memory regions that were registered for "req".
473 * Sleeps until it is safe for the host CPU to access the
474 * previously mapped memory regions.
476 * Caller ensures that req->rl_registered is not empty.
479 frwr_op_unmap_sync(struct rpcrdma_xprt
*r_xprt
, struct rpcrdma_req
*req
)
481 struct ib_send_wr
*invalidate_wrs
, *pos
, *prev
, *bad_wr
;
482 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
483 struct rpcrdma_mw
*mw
, *tmp
;
484 struct rpcrdma_frmr
*f
;
487 dprintk("RPC: %s: req %p\n", __func__
, req
);
489 /* ORDER: Invalidate all of the req's MRs first
491 * Chain the LOCAL_INV Work Requests and post them with
492 * a single ib_post_send() call.
495 invalidate_wrs
= pos
= prev
= NULL
;
496 list_for_each_entry(mw
, &req
->rl_registered
, mw_list
) {
497 pos
= __frwr_prepare_linv_wr(mw
);
500 invalidate_wrs
= pos
;
507 /* Strong send queue ordering guarantees that when the
508 * last WR in the chain completes, all WRs in the chain
511 f
->fr_invwr
.send_flags
= IB_SEND_SIGNALED
;
512 f
->fr_cqe
.done
= frwr_wc_localinv_wake
;
513 reinit_completion(&f
->fr_linv_done
);
514 INIT_CQCOUNT(&r_xprt
->rx_ep
);
516 /* Transport disconnect drains the receive CQ before it
517 * replaces the QP. The RPC reply handler won't call us
518 * unless ri_id->qp is a valid pointer.
520 rc
= ib_post_send(ia
->ri_id
->qp
, invalidate_wrs
, &bad_wr
);
524 wait_for_completion(&f
->fr_linv_done
);
526 /* ORDER: Now DMA unmap all of the req's MRs, and return
527 * them to the free MW list.
530 list_for_each_entry_safe(mw
, tmp
, &req
->rl_registered
, mw_list
) {
531 list_del_init(&mw
->mw_list
);
532 ib_dma_unmap_sg(ia
->ri_device
,
533 mw
->mw_sg
, mw
->mw_nents
, mw
->mw_dir
);
534 rpcrdma_put_mw(r_xprt
, mw
);
539 pr_err("rpcrdma: FRMR invalidate ib_post_send returned %i\n", rc
);
540 rdma_disconnect(ia
->ri_id
);
542 /* Find and reset the MRs in the LOCAL_INV WRs that did not
543 * get posted. This is synchronous, and slow.
545 list_for_each_entry(mw
, &req
->rl_registered
, mw_list
) {
547 if (mw
->frmr
.fr_mr
->rkey
== bad_wr
->ex
.invalidate_rkey
) {
548 __frwr_reset_mr(ia
, mw
);
549 bad_wr
= bad_wr
->next
;
555 /* Use a slow, safe mechanism to invalidate all memory regions
556 * that were registered for "req".
559 frwr_op_unmap_safe(struct rpcrdma_xprt
*r_xprt
, struct rpcrdma_req
*req
,
562 struct rpcrdma_mw
*mw
;
564 while (!list_empty(&req
->rl_registered
)) {
565 mw
= list_first_entry(&req
->rl_registered
,
566 struct rpcrdma_mw
, mw_list
);
567 list_del_init(&mw
->mw_list
);
570 frwr_op_recover_mr(mw
);
572 rpcrdma_defer_mr_recovery(mw
);
576 const struct rpcrdma_memreg_ops rpcrdma_frwr_memreg_ops
= {
577 .ro_map
= frwr_op_map
,
578 .ro_unmap_sync
= frwr_op_unmap_sync
,
579 .ro_unmap_safe
= frwr_op_unmap_safe
,
580 .ro_recover_mr
= frwr_op_recover_mr
,
581 .ro_open
= frwr_op_open
,
582 .ro_maxpages
= frwr_op_maxpages
,
583 .ro_init_mr
= frwr_op_init_mr
,
584 .ro_release_mr
= frwr_op_release_mr
,
585 .ro_displayname
= "frwr",