Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / net / sunrpc / xprtrdma / frwr_ops.c
blob90f688f19783d089fe90fffc40f104bb6e43a94c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2015, 2017 Oracle. All rights reserved.
4 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
5 */
7 /* Lightweight memory registration using Fast Registration Work
8 * Requests (FRWR).
10 * FRWR features ordered asynchronous registration and deregistration
11 * of arbitrarily sized memory regions. This is the fastest and safest
12 * but most complex memory registration mode.
15 /* Normal operation
17 * A Memory Region is prepared for RDMA READ or WRITE using a FAST_REG
18 * Work Request (frwr_op_map). When the RDMA operation is finished, this
19 * Memory Region is invalidated using a LOCAL_INV Work Request
20 * (frwr_op_unmap_sync).
22 * Typically these Work Requests are not signaled, and neither are RDMA
23 * SEND Work Requests (with the exception of signaling occasionally to
24 * prevent provider work queue overflows). This greatly reduces HCA
25 * interrupt workload.
27 * As an optimization, frwr_op_unmap marks MRs INVALID before the
28 * LOCAL_INV WR is posted. If posting succeeds, the MR is placed on
29 * rb_mrs immediately so that no work (like managing a linked list
30 * under a spinlock) is needed in the completion upcall.
32 * But this means that frwr_op_map() can occasionally encounter an MR
33 * that is INVALID but the LOCAL_INV WR has not completed. Work Queue
34 * ordering prevents a subsequent FAST_REG WR from executing against
35 * that MR while it is still being invalidated.
38 /* Transport recovery
40 * ->op_map and the transport connect worker cannot run at the same
41 * time, but ->op_unmap can fire while the transport connect worker
42 * is running. Thus MR recovery is handled in ->op_map, to guarantee
43 * that recovered MRs are owned by a sending RPC, and not one where
44 * ->op_unmap could fire at the same time transport reconnect is
45 * being done.
47 * When the underlying transport disconnects, MRs are left in one of
48 * four states:
50 * INVALID: The MR was not in use before the QP entered ERROR state.
52 * VALID: The MR was registered before the QP entered ERROR state.
54 * FLUSHED_FR: The MR was being registered when the QP entered ERROR
55 * state, and the pending WR was flushed.
57 * FLUSHED_LI: The MR was being invalidated when the QP entered ERROR
58 * state, and the pending WR was flushed.
60 * When frwr_op_map encounters FLUSHED and VALID MRs, they are recovered
61 * with ib_dereg_mr and then are re-initialized. Because MR recovery
62 * allocates fresh resources, it is deferred to a workqueue, and the
63 * recovered MRs are placed back on the rb_mrs list when recovery is
64 * complete. frwr_op_map allocates another MR for the current RPC while
65 * the broken MR is reset.
67 * To ensure that frwr_op_map doesn't encounter an MR that is marked
68 * INVALID but that is about to be flushed due to a previous transport
69 * disconnect, the transport connect worker attempts to drain all
70 * pending send queue WRs before the transport is reconnected.
73 #include <linux/sunrpc/rpc_rdma.h>
75 #include "xprt_rdma.h"
77 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
78 # define RPCDBG_FACILITY RPCDBG_TRANS
79 #endif
81 bool
82 frwr_is_supported(struct rpcrdma_ia *ia)
84 struct ib_device_attr *attrs = &ia->ri_device->attrs;
86 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
87 goto out_not_supported;
88 if (attrs->max_fast_reg_page_list_len == 0)
89 goto out_not_supported;
90 return true;
92 out_not_supported:
93 pr_info("rpcrdma: 'frwr' mode is not supported by device %s\n",
94 ia->ri_device->name);
95 return false;
98 static int
99 frwr_op_init_mr(struct rpcrdma_ia *ia, struct rpcrdma_mr *mr)
101 unsigned int depth = ia->ri_max_frwr_depth;
102 struct rpcrdma_frwr *frwr = &mr->frwr;
103 int rc;
105 frwr->fr_mr = ib_alloc_mr(ia->ri_pd, ia->ri_mrtype, depth);
106 if (IS_ERR(frwr->fr_mr))
107 goto out_mr_err;
109 mr->mr_sg = kcalloc(depth, sizeof(*mr->mr_sg), GFP_KERNEL);
110 if (!mr->mr_sg)
111 goto out_list_err;
113 sg_init_table(mr->mr_sg, depth);
114 init_completion(&frwr->fr_linv_done);
115 return 0;
117 out_mr_err:
118 rc = PTR_ERR(frwr->fr_mr);
119 dprintk("RPC: %s: ib_alloc_mr status %i\n",
120 __func__, rc);
121 return rc;
123 out_list_err:
124 rc = -ENOMEM;
125 dprintk("RPC: %s: sg allocation failure\n",
126 __func__);
127 ib_dereg_mr(frwr->fr_mr);
128 return rc;
131 static void
132 frwr_op_release_mr(struct rpcrdma_mr *mr)
134 int rc;
136 /* Ensure MR is not on any rl_registered list */
137 if (!list_empty(&mr->mr_list))
138 list_del(&mr->mr_list);
140 rc = ib_dereg_mr(mr->frwr.fr_mr);
141 if (rc)
142 pr_err("rpcrdma: final ib_dereg_mr for %p returned %i\n",
143 mr, rc);
144 kfree(mr->mr_sg);
145 kfree(mr);
148 static int
149 __frwr_mr_reset(struct rpcrdma_ia *ia, struct rpcrdma_mr *mr)
151 struct rpcrdma_frwr *frwr = &mr->frwr;
152 int rc;
154 rc = ib_dereg_mr(frwr->fr_mr);
155 if (rc) {
156 pr_warn("rpcrdma: ib_dereg_mr status %d, frwr %p orphaned\n",
157 rc, mr);
158 return rc;
161 frwr->fr_mr = ib_alloc_mr(ia->ri_pd, ia->ri_mrtype,
162 ia->ri_max_frwr_depth);
163 if (IS_ERR(frwr->fr_mr)) {
164 pr_warn("rpcrdma: ib_alloc_mr status %ld, frwr %p orphaned\n",
165 PTR_ERR(frwr->fr_mr), mr);
166 return PTR_ERR(frwr->fr_mr);
169 dprintk("RPC: %s: recovered FRWR %p\n", __func__, frwr);
170 frwr->fr_state = FRWR_IS_INVALID;
171 return 0;
174 /* Reset of a single FRWR. Generate a fresh rkey by replacing the MR.
176 static void
177 frwr_op_recover_mr(struct rpcrdma_mr *mr)
179 enum rpcrdma_frwr_state state = mr->frwr.fr_state;
180 struct rpcrdma_xprt *r_xprt = mr->mr_xprt;
181 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
182 int rc;
184 rc = __frwr_mr_reset(ia, mr);
185 if (state != FRWR_FLUSHED_LI) {
186 trace_xprtrdma_dma_unmap(mr);
187 ib_dma_unmap_sg(ia->ri_device,
188 mr->mr_sg, mr->mr_nents, mr->mr_dir);
190 if (rc)
191 goto out_release;
193 rpcrdma_mr_put(mr);
194 r_xprt->rx_stats.mrs_recovered++;
195 return;
197 out_release:
198 pr_err("rpcrdma: FRWR reset failed %d, %p release\n", rc, mr);
199 r_xprt->rx_stats.mrs_orphaned++;
201 spin_lock(&r_xprt->rx_buf.rb_mrlock);
202 list_del(&mr->mr_all);
203 spin_unlock(&r_xprt->rx_buf.rb_mrlock);
205 frwr_op_release_mr(mr);
208 static int
209 frwr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep,
210 struct rpcrdma_create_data_internal *cdata)
212 struct ib_device_attr *attrs = &ia->ri_device->attrs;
213 int depth, delta;
215 ia->ri_mrtype = IB_MR_TYPE_MEM_REG;
216 if (attrs->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
217 ia->ri_mrtype = IB_MR_TYPE_SG_GAPS;
219 ia->ri_max_frwr_depth =
220 min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
221 attrs->max_fast_reg_page_list_len);
222 dprintk("RPC: %s: device's max FR page list len = %u\n",
223 __func__, ia->ri_max_frwr_depth);
225 /* Add room for frwr register and invalidate WRs.
226 * 1. FRWR reg WR for head
227 * 2. FRWR invalidate WR for head
228 * 3. N FRWR reg WRs for pagelist
229 * 4. N FRWR invalidate WRs for pagelist
230 * 5. FRWR reg WR for tail
231 * 6. FRWR invalidate WR for tail
232 * 7. The RDMA_SEND WR
234 depth = 7;
236 /* Calculate N if the device max FRWR depth is smaller than
237 * RPCRDMA_MAX_DATA_SEGS.
239 if (ia->ri_max_frwr_depth < RPCRDMA_MAX_DATA_SEGS) {
240 delta = RPCRDMA_MAX_DATA_SEGS - ia->ri_max_frwr_depth;
241 do {
242 depth += 2; /* FRWR reg + invalidate */
243 delta -= ia->ri_max_frwr_depth;
244 } while (delta > 0);
247 ep->rep_attr.cap.max_send_wr *= depth;
248 if (ep->rep_attr.cap.max_send_wr > attrs->max_qp_wr) {
249 cdata->max_requests = attrs->max_qp_wr / depth;
250 if (!cdata->max_requests)
251 return -EINVAL;
252 ep->rep_attr.cap.max_send_wr = cdata->max_requests *
253 depth;
256 ia->ri_max_segs = max_t(unsigned int, 1, RPCRDMA_MAX_DATA_SEGS /
257 ia->ri_max_frwr_depth);
258 return 0;
261 /* FRWR mode conveys a list of pages per chunk segment. The
262 * maximum length of that list is the FRWR page list depth.
264 static size_t
265 frwr_op_maxpages(struct rpcrdma_xprt *r_xprt)
267 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
269 return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
270 RPCRDMA_MAX_HDR_SEGS * ia->ri_max_frwr_depth);
273 static void
274 __frwr_sendcompletion_flush(struct ib_wc *wc, const char *wr)
276 if (wc->status != IB_WC_WR_FLUSH_ERR)
277 pr_err("rpcrdma: %s: %s (%u/0x%x)\n",
278 wr, ib_wc_status_msg(wc->status),
279 wc->status, wc->vendor_err);
283 * frwr_wc_fastreg - Invoked by RDMA provider for a flushed FastReg WC
284 * @cq: completion queue (ignored)
285 * @wc: completed WR
288 static void
289 frwr_wc_fastreg(struct ib_cq *cq, struct ib_wc *wc)
291 struct ib_cqe *cqe = wc->wr_cqe;
292 struct rpcrdma_frwr *frwr =
293 container_of(cqe, struct rpcrdma_frwr, fr_cqe);
295 /* WARNING: Only wr_cqe and status are reliable at this point */
296 if (wc->status != IB_WC_SUCCESS) {
297 frwr->fr_state = FRWR_FLUSHED_FR;
298 __frwr_sendcompletion_flush(wc, "fastreg");
300 trace_xprtrdma_wc_fastreg(wc, frwr);
304 * frwr_wc_localinv - Invoked by RDMA provider for a flushed LocalInv WC
305 * @cq: completion queue (ignored)
306 * @wc: completed WR
309 static void
310 frwr_wc_localinv(struct ib_cq *cq, struct ib_wc *wc)
312 struct ib_cqe *cqe = wc->wr_cqe;
313 struct rpcrdma_frwr *frwr = container_of(cqe, struct rpcrdma_frwr,
314 fr_cqe);
316 /* WARNING: Only wr_cqe and status are reliable at this point */
317 if (wc->status != IB_WC_SUCCESS) {
318 frwr->fr_state = FRWR_FLUSHED_LI;
319 __frwr_sendcompletion_flush(wc, "localinv");
321 trace_xprtrdma_wc_li(wc, frwr);
325 * frwr_wc_localinv_wake - Invoked by RDMA provider for a signaled LocalInv WC
326 * @cq: completion queue (ignored)
327 * @wc: completed WR
329 * Awaken anyone waiting for an MR to finish being fenced.
331 static void
332 frwr_wc_localinv_wake(struct ib_cq *cq, struct ib_wc *wc)
334 struct ib_cqe *cqe = wc->wr_cqe;
335 struct rpcrdma_frwr *frwr = container_of(cqe, struct rpcrdma_frwr,
336 fr_cqe);
338 /* WARNING: Only wr_cqe and status are reliable at this point */
339 if (wc->status != IB_WC_SUCCESS) {
340 frwr->fr_state = FRWR_FLUSHED_LI;
341 __frwr_sendcompletion_flush(wc, "localinv");
343 complete(&frwr->fr_linv_done);
344 trace_xprtrdma_wc_li_wake(wc, frwr);
347 /* Post a REG_MR Work Request to register a memory region
348 * for remote access via RDMA READ or RDMA WRITE.
350 static struct rpcrdma_mr_seg *
351 frwr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg,
352 int nsegs, bool writing, struct rpcrdma_mr **out)
354 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
355 bool holes_ok = ia->ri_mrtype == IB_MR_TYPE_SG_GAPS;
356 struct rpcrdma_frwr *frwr;
357 struct rpcrdma_mr *mr;
358 struct ib_mr *ibmr;
359 struct ib_reg_wr *reg_wr;
360 struct ib_send_wr *bad_wr;
361 int rc, i, n;
362 u8 key;
364 mr = NULL;
365 do {
366 if (mr)
367 rpcrdma_mr_defer_recovery(mr);
368 mr = rpcrdma_mr_get(r_xprt);
369 if (!mr)
370 return ERR_PTR(-ENOBUFS);
371 } while (mr->frwr.fr_state != FRWR_IS_INVALID);
372 frwr = &mr->frwr;
373 frwr->fr_state = FRWR_IS_VALID;
375 if (nsegs > ia->ri_max_frwr_depth)
376 nsegs = ia->ri_max_frwr_depth;
377 for (i = 0; i < nsegs;) {
378 if (seg->mr_page)
379 sg_set_page(&mr->mr_sg[i],
380 seg->mr_page,
381 seg->mr_len,
382 offset_in_page(seg->mr_offset));
383 else
384 sg_set_buf(&mr->mr_sg[i], seg->mr_offset,
385 seg->mr_len);
387 ++seg;
388 ++i;
389 if (holes_ok)
390 continue;
391 if ((i < nsegs && offset_in_page(seg->mr_offset)) ||
392 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
393 break;
395 mr->mr_dir = rpcrdma_data_dir(writing);
397 mr->mr_nents = ib_dma_map_sg(ia->ri_device, mr->mr_sg, i, mr->mr_dir);
398 if (!mr->mr_nents)
399 goto out_dmamap_err;
401 ibmr = frwr->fr_mr;
402 n = ib_map_mr_sg(ibmr, mr->mr_sg, mr->mr_nents, NULL, PAGE_SIZE);
403 if (unlikely(n != mr->mr_nents))
404 goto out_mapmr_err;
406 key = (u8)(ibmr->rkey & 0x000000FF);
407 ib_update_fast_reg_key(ibmr, ++key);
409 reg_wr = &frwr->fr_regwr;
410 reg_wr->wr.next = NULL;
411 reg_wr->wr.opcode = IB_WR_REG_MR;
412 frwr->fr_cqe.done = frwr_wc_fastreg;
413 reg_wr->wr.wr_cqe = &frwr->fr_cqe;
414 reg_wr->wr.num_sge = 0;
415 reg_wr->wr.send_flags = 0;
416 reg_wr->mr = ibmr;
417 reg_wr->key = ibmr->rkey;
418 reg_wr->access = writing ?
419 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
420 IB_ACCESS_REMOTE_READ;
422 rc = ib_post_send(ia->ri_id->qp, &reg_wr->wr, &bad_wr);
423 if (rc)
424 goto out_senderr;
426 mr->mr_handle = ibmr->rkey;
427 mr->mr_length = ibmr->length;
428 mr->mr_offset = ibmr->iova;
430 *out = mr;
431 return seg;
433 out_dmamap_err:
434 pr_err("rpcrdma: failed to DMA map sg %p sg_nents %d\n",
435 mr->mr_sg, i);
436 frwr->fr_state = FRWR_IS_INVALID;
437 rpcrdma_mr_put(mr);
438 return ERR_PTR(-EIO);
440 out_mapmr_err:
441 pr_err("rpcrdma: failed to map mr %p (%d/%d)\n",
442 frwr->fr_mr, n, mr->mr_nents);
443 rpcrdma_mr_defer_recovery(mr);
444 return ERR_PTR(-EIO);
446 out_senderr:
447 pr_err("rpcrdma: FRWR registration ib_post_send returned %i\n", rc);
448 rpcrdma_mr_defer_recovery(mr);
449 return ERR_PTR(-ENOTCONN);
452 /* Handle a remotely invalidated mr on the @mrs list
454 static void
455 frwr_op_reminv(struct rpcrdma_rep *rep, struct list_head *mrs)
457 struct rpcrdma_mr *mr;
459 list_for_each_entry(mr, mrs, mr_list)
460 if (mr->mr_handle == rep->rr_inv_rkey) {
461 list_del(&mr->mr_list);
462 trace_xprtrdma_remoteinv(mr);
463 mr->frwr.fr_state = FRWR_IS_INVALID;
464 rpcrdma_mr_unmap_and_put(mr);
465 break; /* only one invalidated MR per RPC */
469 /* Invalidate all memory regions that were registered for "req".
471 * Sleeps until it is safe for the host CPU to access the
472 * previously mapped memory regions.
474 * Caller ensures that @mrs is not empty before the call. This
475 * function empties the list.
477 static void
478 frwr_op_unmap_sync(struct rpcrdma_xprt *r_xprt, struct list_head *mrs)
480 struct ib_send_wr *first, **prev, *last, *bad_wr;
481 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
482 struct rpcrdma_frwr *frwr;
483 struct rpcrdma_mr *mr;
484 int count, rc;
486 /* ORDER: Invalidate all of the MRs first
488 * Chain the LOCAL_INV Work Requests and post them with
489 * a single ib_post_send() call.
491 frwr = NULL;
492 count = 0;
493 prev = &first;
494 list_for_each_entry(mr, mrs, mr_list) {
495 mr->frwr.fr_state = FRWR_IS_INVALID;
497 frwr = &mr->frwr;
498 trace_xprtrdma_localinv(mr);
500 frwr->fr_cqe.done = frwr_wc_localinv;
501 last = &frwr->fr_invwr;
502 memset(last, 0, sizeof(*last));
503 last->wr_cqe = &frwr->fr_cqe;
504 last->opcode = IB_WR_LOCAL_INV;
505 last->ex.invalidate_rkey = mr->mr_handle;
506 count++;
508 *prev = last;
509 prev = &last->next;
511 if (!frwr)
512 goto unmap;
514 /* Strong send queue ordering guarantees that when the
515 * last WR in the chain completes, all WRs in the chain
516 * are complete.
518 last->send_flags = IB_SEND_SIGNALED;
519 frwr->fr_cqe.done = frwr_wc_localinv_wake;
520 reinit_completion(&frwr->fr_linv_done);
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 bad_wr = NULL;
528 rc = ib_post_send(ia->ri_id->qp, first, &bad_wr);
529 if (bad_wr != first)
530 wait_for_completion(&frwr->fr_linv_done);
531 if (rc)
532 goto reset_mrs;
534 /* ORDER: Now DMA unmap all of the MRs, and return
535 * them to the free MR list.
537 unmap:
538 while (!list_empty(mrs)) {
539 mr = rpcrdma_mr_pop(mrs);
540 rpcrdma_mr_unmap_and_put(mr);
542 return;
544 reset_mrs:
545 pr_err("rpcrdma: FRWR invalidate ib_post_send returned %i\n", rc);
547 /* Find and reset the MRs in the LOCAL_INV WRs that did not
548 * get posted.
550 while (bad_wr) {
551 frwr = container_of(bad_wr, struct rpcrdma_frwr,
552 fr_invwr);
553 mr = container_of(frwr, struct rpcrdma_mr, frwr);
555 __frwr_mr_reset(ia, mr);
557 bad_wr = bad_wr->next;
559 goto unmap;
562 const struct rpcrdma_memreg_ops rpcrdma_frwr_memreg_ops = {
563 .ro_map = frwr_op_map,
564 .ro_reminv = frwr_op_reminv,
565 .ro_unmap_sync = frwr_op_unmap_sync,
566 .ro_recover_mr = frwr_op_recover_mr,
567 .ro_open = frwr_op_open,
568 .ro_maxpages = frwr_op_maxpages,
569 .ro_init_mr = frwr_op_init_mr,
570 .ro_release_mr = frwr_op_release_mr,
571 .ro_displayname = "frwr",
572 .ro_send_w_inv_ok = RPCRDMA_CMP_F_SND_W_INV_OK,