1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015, 2017 Oracle. All rights reserved.
4 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
7 /* Lightweight memory registration using Fast Registration Work
10 * FRWR features ordered asynchronous registration and invalidation
11 * of arbitrarily-sized memory regions. This is the fastest and safest
12 * but most complex memory registration mode.
17 * A Memory Region is prepared for RDMA Read or Write using a FAST_REG
18 * Work Request (frwr_map). When the RDMA operation is finished, this
19 * Memory Region is invalidated using a LOCAL_INV Work Request
20 * (frwr_unmap_async and frwr_unmap_sync).
22 * Typically FAST_REG Work Requests are not signaled, and neither are
23 * RDMA Send Work Requests (with the exception of signaling occasionally
24 * to prevent provider work queue overflows). This greatly reduces HCA
30 * frwr_map and frwr_unmap_* cannot run at the same time the transport
31 * connect worker is running. The connect worker holds the transport
32 * send lock, just as ->send_request does. This prevents frwr_map and
33 * the connect worker from running concurrently. When a connection is
34 * closed, the Receive completion queue is drained before the allowing
35 * the connect worker to get control. This prevents frwr_unmap and the
36 * connect worker from running concurrently.
38 * When the underlying transport disconnects, MRs that are in flight
39 * are flushed and are likely unusable. Thus all MRs are destroyed.
40 * New MRs are created on demand.
43 #include <linux/sunrpc/svc_rdma.h>
45 #include "xprt_rdma.h"
46 #include <trace/events/rpcrdma.h>
48 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
49 # define RPCDBG_FACILITY RPCDBG_TRANS
52 static void frwr_mr_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr *mr)
55 trace_xprtrdma_mr_unmap(mr);
56 ib_dma_unmap_sg(mr->mr_device, mr->mr_sg, mr->mr_nents,
63 * frwr_mr_release - Destroy one MR
64 * @mr: MR allocated by frwr_mr_init
67 void frwr_mr_release(struct rpcrdma_mr *mr)
71 frwr_mr_unmap(mr->mr_xprt, mr);
73 rc = ib_dereg_mr(mr->frwr.fr_mr);
75 trace_xprtrdma_frwr_dereg(mr, rc);
80 static void frwr_mr_put(struct rpcrdma_mr *mr)
82 frwr_mr_unmap(mr->mr_xprt, mr);
84 /* The MR is returned to the req's MR free list instead
85 * of to the xprt's MR free list. No spinlock is needed.
87 rpcrdma_mr_push(mr, &mr->mr_req->rl_free_mrs);
90 /* frwr_reset - Place MRs back on the free list
91 * @req: request to reset
93 * Used after a failed marshal. For FRWR, this means the MRs
94 * don't have to be fully released and recreated.
96 * NB: This is safe only as long as none of @req's MRs are
97 * involved with an ongoing asynchronous FAST_REG or LOCAL_INV
100 void frwr_reset(struct rpcrdma_req *req)
102 struct rpcrdma_mr *mr;
104 while ((mr = rpcrdma_mr_pop(&req->rl_registered)))
109 * frwr_mr_init - Initialize one MR
110 * @r_xprt: controlling transport instance
111 * @mr: generic MR to prepare for FRWR
113 * Returns zero if successful. Otherwise a negative errno
116 int frwr_mr_init(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr *mr)
118 struct rpcrdma_ep *ep = r_xprt->rx_ep;
119 unsigned int depth = ep->re_max_fr_depth;
120 struct scatterlist *sg;
124 frmr = ib_alloc_mr(ep->re_pd, ep->re_mrtype, depth);
128 sg = kmalloc_array(depth, sizeof(*sg), GFP_NOFS);
132 mr->mr_xprt = r_xprt;
133 mr->frwr.fr_mr = frmr;
134 mr->mr_device = NULL;
135 INIT_LIST_HEAD(&mr->mr_list);
136 init_completion(&mr->frwr.fr_linv_done);
138 sg_init_table(sg, depth);
144 trace_xprtrdma_frwr_alloc(mr, rc);
153 * frwr_query_device - Prepare a transport for use with FRWR
154 * @ep: endpoint to fill in
155 * @device: RDMA device to query
159 * ep->re_max_requests
160 * ep->re_max_rdma_segs
161 * ep->re_max_fr_depth
165 * On success, returns zero.
166 * %-EINVAL - the device does not support FRWR memory registration
167 * %-ENOMEM - the device is not sufficiently capable for NFS/RDMA
169 int frwr_query_device(struct rpcrdma_ep *ep, const struct ib_device *device)
171 const struct ib_device_attr *attrs = &device->attrs;
172 int max_qp_wr, depth, delta;
173 unsigned int max_sge;
175 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) ||
176 attrs->max_fast_reg_page_list_len == 0) {
177 pr_err("rpcrdma: 'frwr' mode is not supported by device %s\n",
182 max_sge = min_t(unsigned int, attrs->max_send_sge,
183 RPCRDMA_MAX_SEND_SGES);
184 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
185 pr_err("rpcrdma: HCA provides only %u send SGEs\n", max_sge);
188 ep->re_attr.cap.max_send_sge = max_sge;
189 ep->re_attr.cap.max_recv_sge = 1;
191 ep->re_mrtype = IB_MR_TYPE_MEM_REG;
192 if (attrs->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
193 ep->re_mrtype = IB_MR_TYPE_SG_GAPS;
195 /* Quirk: Some devices advertise a large max_fast_reg_page_list_len
196 * capability, but perform optimally when the MRs are not larger
199 if (attrs->max_sge_rd > RPCRDMA_MAX_HDR_SEGS)
200 ep->re_max_fr_depth = attrs->max_sge_rd;
202 ep->re_max_fr_depth = attrs->max_fast_reg_page_list_len;
203 if (ep->re_max_fr_depth > RPCRDMA_MAX_DATA_SEGS)
204 ep->re_max_fr_depth = RPCRDMA_MAX_DATA_SEGS;
206 /* Add room for frwr register and invalidate WRs.
207 * 1. FRWR reg WR for head
208 * 2. FRWR invalidate WR for head
209 * 3. N FRWR reg WRs for pagelist
210 * 4. N FRWR invalidate WRs for pagelist
211 * 5. FRWR reg WR for tail
212 * 6. FRWR invalidate WR for tail
213 * 7. The RDMA_SEND WR
217 /* Calculate N if the device max FRWR depth is smaller than
218 * RPCRDMA_MAX_DATA_SEGS.
220 if (ep->re_max_fr_depth < RPCRDMA_MAX_DATA_SEGS) {
221 delta = RPCRDMA_MAX_DATA_SEGS - ep->re_max_fr_depth;
223 depth += 2; /* FRWR reg + invalidate */
224 delta -= ep->re_max_fr_depth;
228 max_qp_wr = attrs->max_qp_wr;
229 max_qp_wr -= RPCRDMA_BACKWARD_WRS;
231 if (max_qp_wr < RPCRDMA_MIN_SLOT_TABLE)
233 if (ep->re_max_requests > max_qp_wr)
234 ep->re_max_requests = max_qp_wr;
235 ep->re_attr.cap.max_send_wr = ep->re_max_requests * depth;
236 if (ep->re_attr.cap.max_send_wr > max_qp_wr) {
237 ep->re_max_requests = max_qp_wr / depth;
238 if (!ep->re_max_requests)
240 ep->re_attr.cap.max_send_wr = ep->re_max_requests * depth;
242 ep->re_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
243 ep->re_attr.cap.max_send_wr += 1; /* for ib_drain_sq */
244 ep->re_attr.cap.max_recv_wr = ep->re_max_requests;
245 ep->re_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
246 ep->re_attr.cap.max_recv_wr += RPCRDMA_MAX_RECV_BATCH;
247 ep->re_attr.cap.max_recv_wr += 1; /* for ib_drain_rq */
249 ep->re_max_rdma_segs =
250 DIV_ROUND_UP(RPCRDMA_MAX_DATA_SEGS, ep->re_max_fr_depth);
251 /* Reply chunks require segments for head and tail buffers */
252 ep->re_max_rdma_segs += 2;
253 if (ep->re_max_rdma_segs > RPCRDMA_MAX_HDR_SEGS)
254 ep->re_max_rdma_segs = RPCRDMA_MAX_HDR_SEGS;
256 /* Ensure the underlying device is capable of conveying the
257 * largest r/wsize NFS will ask for. This guarantees that
258 * failing over from one RDMA device to another will not
261 if ((ep->re_max_rdma_segs * ep->re_max_fr_depth) < RPCRDMA_MAX_SEGS)
268 * frwr_map - Register a memory region
269 * @r_xprt: controlling transport
270 * @seg: memory region co-ordinates
271 * @nsegs: number of segments remaining
272 * @writing: true when RDMA Write will be used
273 * @xid: XID of RPC using the registered memory
276 * Prepare a REG_MR Work Request to register a memory region
277 * for remote access via RDMA READ or RDMA WRITE.
279 * Returns the next segment or a negative errno pointer.
280 * On success, @mr is filled in.
282 struct rpcrdma_mr_seg *frwr_map(struct rpcrdma_xprt *r_xprt,
283 struct rpcrdma_mr_seg *seg,
284 int nsegs, bool writing, __be32 xid,
285 struct rpcrdma_mr *mr)
287 struct rpcrdma_ep *ep = r_xprt->rx_ep;
288 struct ib_reg_wr *reg_wr;
293 if (nsegs > ep->re_max_fr_depth)
294 nsegs = ep->re_max_fr_depth;
295 for (i = 0; i < nsegs;) {
296 sg_set_page(&mr->mr_sg[i], seg->mr_page,
297 seg->mr_len, seg->mr_offset);
301 if (ep->re_mrtype == IB_MR_TYPE_SG_GAPS)
303 if ((i < nsegs && seg->mr_offset) ||
304 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
307 mr->mr_dir = rpcrdma_data_dir(writing);
310 dma_nents = ib_dma_map_sg(ep->re_id->device, mr->mr_sg, mr->mr_nents,
314 mr->mr_device = ep->re_id->device;
316 ibmr = mr->frwr.fr_mr;
317 n = ib_map_mr_sg(ibmr, mr->mr_sg, dma_nents, NULL, PAGE_SIZE);
321 ibmr->iova &= 0x00000000ffffffff;
322 ibmr->iova |= ((u64)be32_to_cpu(xid)) << 32;
323 key = (u8)(ibmr->rkey & 0x000000FF);
324 ib_update_fast_reg_key(ibmr, ++key);
326 reg_wr = &mr->frwr.fr_regwr;
328 reg_wr->key = ibmr->rkey;
329 reg_wr->access = writing ?
330 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
331 IB_ACCESS_REMOTE_READ;
333 mr->mr_handle = ibmr->rkey;
334 mr->mr_length = ibmr->length;
335 mr->mr_offset = ibmr->iova;
336 trace_xprtrdma_mr_map(mr);
341 trace_xprtrdma_frwr_sgerr(mr, i);
342 return ERR_PTR(-EIO);
345 trace_xprtrdma_frwr_maperr(mr, n);
346 return ERR_PTR(-EIO);
350 * frwr_wc_fastreg - Invoked by RDMA provider for a flushed FastReg WC
351 * @cq: completion queue
352 * @wc: WCE for a completed FastReg WR
354 * Each flushed MR gets destroyed after the QP has drained.
356 static void frwr_wc_fastreg(struct ib_cq *cq, struct ib_wc *wc)
358 struct ib_cqe *cqe = wc->wr_cqe;
359 struct rpcrdma_frwr *frwr =
360 container_of(cqe, struct rpcrdma_frwr, fr_cqe);
362 /* WARNING: Only wr_cqe and status are reliable at this point */
363 trace_xprtrdma_wc_fastreg(wc, &frwr->fr_cid);
365 rpcrdma_flush_disconnect(cq->cq_context, wc);
368 static void frwr_cid_init(struct rpcrdma_ep *ep,
369 struct rpcrdma_frwr *frwr)
371 struct rpc_rdma_cid *cid = &frwr->fr_cid;
373 cid->ci_queue_id = ep->re_attr.send_cq->res.id;
374 cid->ci_completion_id = frwr->fr_mr->res.id;
378 * frwr_send - post Send WRs containing the RPC Call message
379 * @r_xprt: controlling transport instance
380 * @req: prepared RPC Call
382 * For FRWR, chain any FastReg WRs to the Send WR. Only a
383 * single ib_post_send call is needed to register memory
384 * and then post the Send WR.
386 * Returns the return code from ib_post_send.
388 * Caller must hold the transport send lock to ensure that the
389 * pointers to the transport's rdma_cm_id and QP are stable.
391 int frwr_send(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
393 struct rpcrdma_ep *ep = r_xprt->rx_ep;
394 struct ib_send_wr *post_wr;
395 struct rpcrdma_mr *mr;
397 post_wr = &req->rl_wr;
398 list_for_each_entry(mr, &req->rl_registered, mr_list) {
399 struct rpcrdma_frwr *frwr;
403 frwr->fr_cqe.done = frwr_wc_fastreg;
404 frwr_cid_init(ep, frwr);
405 frwr->fr_regwr.wr.next = post_wr;
406 frwr->fr_regwr.wr.wr_cqe = &frwr->fr_cqe;
407 frwr->fr_regwr.wr.num_sge = 0;
408 frwr->fr_regwr.wr.opcode = IB_WR_REG_MR;
409 frwr->fr_regwr.wr.send_flags = 0;
411 post_wr = &frwr->fr_regwr.wr;
414 return ib_post_send(ep->re_id->qp, post_wr, NULL);
418 * frwr_reminv - handle a remotely invalidated mr on the @mrs list
419 * @rep: Received reply
420 * @mrs: list of MRs to check
423 void frwr_reminv(struct rpcrdma_rep *rep, struct list_head *mrs)
425 struct rpcrdma_mr *mr;
427 list_for_each_entry(mr, mrs, mr_list)
428 if (mr->mr_handle == rep->rr_inv_rkey) {
429 list_del_init(&mr->mr_list);
431 break; /* only one invalidated MR per RPC */
435 static void frwr_mr_done(struct ib_wc *wc, struct rpcrdma_mr *mr)
437 if (likely(wc->status == IB_WC_SUCCESS))
442 * frwr_wc_localinv - Invoked by RDMA provider for a LOCAL_INV WC
443 * @cq: completion queue
444 * @wc: WCE for a completed LocalInv WR
447 static void frwr_wc_localinv(struct ib_cq *cq, struct ib_wc *wc)
449 struct ib_cqe *cqe = wc->wr_cqe;
450 struct rpcrdma_frwr *frwr =
451 container_of(cqe, struct rpcrdma_frwr, fr_cqe);
452 struct rpcrdma_mr *mr = container_of(frwr, struct rpcrdma_mr, frwr);
454 /* WARNING: Only wr_cqe and status are reliable at this point */
455 trace_xprtrdma_wc_li(wc, &frwr->fr_cid);
456 frwr_mr_done(wc, mr);
458 rpcrdma_flush_disconnect(cq->cq_context, wc);
462 * frwr_wc_localinv_wake - Invoked by RDMA provider for a LOCAL_INV WC
463 * @cq: completion queue
464 * @wc: WCE for a completed LocalInv WR
466 * Awaken anyone waiting for an MR to finish being fenced.
468 static void frwr_wc_localinv_wake(struct ib_cq *cq, struct ib_wc *wc)
470 struct ib_cqe *cqe = wc->wr_cqe;
471 struct rpcrdma_frwr *frwr =
472 container_of(cqe, struct rpcrdma_frwr, fr_cqe);
473 struct rpcrdma_mr *mr = container_of(frwr, struct rpcrdma_mr, frwr);
475 /* WARNING: Only wr_cqe and status are reliable at this point */
476 trace_xprtrdma_wc_li_wake(wc, &frwr->fr_cid);
477 frwr_mr_done(wc, mr);
478 complete(&frwr->fr_linv_done);
480 rpcrdma_flush_disconnect(cq->cq_context, wc);
484 * frwr_unmap_sync - invalidate memory regions that were registered for @req
485 * @r_xprt: controlling transport instance
486 * @req: rpcrdma_req with a non-empty list of MRs to process
488 * Sleeps until it is safe for the host CPU to access the previously mapped
489 * memory regions. This guarantees that registered MRs are properly fenced
490 * from the server before the RPC consumer accesses the data in them. It
491 * also ensures proper Send flow control: waking the next RPC waits until
492 * this RPC has relinquished all its Send Queue entries.
494 void frwr_unmap_sync(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
496 struct ib_send_wr *first, **prev, *last;
497 struct rpcrdma_ep *ep = r_xprt->rx_ep;
498 const struct ib_send_wr *bad_wr;
499 struct rpcrdma_frwr *frwr;
500 struct rpcrdma_mr *mr;
503 /* ORDER: Invalidate all of the MRs first
505 * Chain the LOCAL_INV Work Requests and post them with
506 * a single ib_post_send() call.
510 while ((mr = rpcrdma_mr_pop(&req->rl_registered))) {
512 trace_xprtrdma_mr_localinv(mr);
513 r_xprt->rx_stats.local_inv_needed++;
516 frwr->fr_cqe.done = frwr_wc_localinv;
517 frwr_cid_init(ep, frwr);
518 last = &frwr->fr_invwr;
520 last->wr_cqe = &frwr->fr_cqe;
521 last->sg_list = NULL;
523 last->opcode = IB_WR_LOCAL_INV;
524 last->send_flags = IB_SEND_SIGNALED;
525 last->ex.invalidate_rkey = mr->mr_handle;
531 /* Strong send queue ordering guarantees that when the
532 * last WR in the chain completes, all WRs in the chain
535 frwr->fr_cqe.done = frwr_wc_localinv_wake;
536 reinit_completion(&frwr->fr_linv_done);
538 /* Transport disconnect drains the receive CQ before it
539 * replaces the QP. The RPC reply handler won't call us
540 * unless re_id->qp is a valid pointer.
543 rc = ib_post_send(ep->re_id->qp, first, &bad_wr);
545 /* The final LOCAL_INV WR in the chain is supposed to
546 * do the wake. If it was never posted, the wake will
547 * not happen, so don't wait in that case.
550 wait_for_completion(&frwr->fr_linv_done);
554 /* On error, the MRs get destroyed once the QP has drained. */
555 trace_xprtrdma_post_linv_err(req, rc);
559 * frwr_wc_localinv_done - Invoked by RDMA provider for a signaled LOCAL_INV WC
560 * @cq: completion queue
561 * @wc: WCE for a completed LocalInv WR
564 static void frwr_wc_localinv_done(struct ib_cq *cq, struct ib_wc *wc)
566 struct ib_cqe *cqe = wc->wr_cqe;
567 struct rpcrdma_frwr *frwr =
568 container_of(cqe, struct rpcrdma_frwr, fr_cqe);
569 struct rpcrdma_mr *mr = container_of(frwr, struct rpcrdma_mr, frwr);
570 struct rpcrdma_rep *rep;
572 /* WARNING: Only wr_cqe and status are reliable at this point */
573 trace_xprtrdma_wc_li_done(wc, &frwr->fr_cid);
575 /* Ensure that @rep is generated before the MR is released */
576 rep = mr->mr_req->rl_reply;
579 if (wc->status != IB_WC_SUCCESS) {
581 rpcrdma_unpin_rqst(rep);
582 rpcrdma_flush_disconnect(cq->cq_context, wc);
586 rpcrdma_complete_rqst(rep);
590 * frwr_unmap_async - invalidate memory regions that were registered for @req
591 * @r_xprt: controlling transport instance
592 * @req: rpcrdma_req with a non-empty list of MRs to process
594 * This guarantees that registered MRs are properly fenced from the
595 * server before the RPC consumer accesses the data in them. It also
596 * ensures proper Send flow control: waking the next RPC waits until
597 * this RPC has relinquished all its Send Queue entries.
599 void frwr_unmap_async(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
601 struct ib_send_wr *first, *last, **prev;
602 struct rpcrdma_ep *ep = r_xprt->rx_ep;
603 struct rpcrdma_frwr *frwr;
604 struct rpcrdma_mr *mr;
607 /* Chain the LOCAL_INV Work Requests and post them with
608 * a single ib_post_send() call.
612 while ((mr = rpcrdma_mr_pop(&req->rl_registered))) {
614 trace_xprtrdma_mr_localinv(mr);
615 r_xprt->rx_stats.local_inv_needed++;
618 frwr->fr_cqe.done = frwr_wc_localinv;
619 frwr_cid_init(ep, frwr);
620 last = &frwr->fr_invwr;
622 last->wr_cqe = &frwr->fr_cqe;
623 last->sg_list = NULL;
625 last->opcode = IB_WR_LOCAL_INV;
626 last->send_flags = IB_SEND_SIGNALED;
627 last->ex.invalidate_rkey = mr->mr_handle;
633 /* Strong send queue ordering guarantees that when the
634 * last WR in the chain completes, all WRs in the chain
635 * are complete. The last completion will wake up the
638 frwr->fr_cqe.done = frwr_wc_localinv_done;
640 /* Transport disconnect drains the receive CQ before it
641 * replaces the QP. The RPC reply handler won't call us
642 * unless re_id->qp is a valid pointer.
644 rc = ib_post_send(ep->re_id->qp, first, NULL);
648 /* On error, the MRs get destroyed once the QP has drained. */
649 trace_xprtrdma_post_linv_err(req, rc);
651 /* The final LOCAL_INV WR in the chain is supposed to
652 * do the wake. If it was never posted, the wake does
653 * not happen. Unpin the rqst in preparation for its
656 rpcrdma_unpin_rqst(req->rl_reply);