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 */ 6 7 /* Lightweight memory registration using Fast Registration Work 8 * Requests (FRWR). 9 * 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. 13 */ 14 15 /* Normal operation 16 * 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). 21 * 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. 26 * 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. 31 * 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. 36 */ 37 38 /* Transport recovery 39 * 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. 46 * 47 * When the underlying transport disconnects, MRs are left in one of 48 * four states: 49 * 50 * INVALID: The MR was not in use before the QP entered ERROR state. 51 * 52 * VALID: The MR was registered before the QP entered ERROR state. 53 * 54 * FLUSHED_FR: The MR was being registered when the QP entered ERROR 55 * state, and the pending WR was flushed. 56 * 57 * FLUSHED_LI: The MR was being invalidated when the QP entered ERROR 58 * state, and the pending WR was flushed. 59 * 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. 66 * 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. 71 */ 72 73 #include <linux/sunrpc/rpc_rdma.h> 74 75 #include "xprt_rdma.h" 76 77 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 78 # define RPCDBG_FACILITY RPCDBG_TRANS 79 #endif 80 81 bool 82 frwr_is_supported(struct rpcrdma_ia *ia) 83 { 84 struct ib_device_attr *attrs = &ia->ri_device->attrs; 85 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; 91 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; 96 } 97 98 static int 99 frwr_op_init_mr(struct rpcrdma_ia *ia, struct rpcrdma_mr *mr) 100 { 101 unsigned int depth = ia->ri_max_frwr_depth; 102 struct rpcrdma_frwr *frwr = &mr->frwr; 103 int rc; 104 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; 108 109 mr->mr_sg = kcalloc(depth, sizeof(*mr->mr_sg), GFP_KERNEL); 110 if (!mr->mr_sg) 111 goto out_list_err; 112 113 sg_init_table(mr->mr_sg, depth); 114 init_completion(&frwr->fr_linv_done); 115 return 0; 116 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; 122 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; 129 } 130 131 static void 132 frwr_op_release_mr(struct rpcrdma_mr *mr) 133 { 134 int rc; 135 136 /* Ensure MR is not on any rl_registered list */ 137 if (!list_empty(&mr->mr_list)) 138 list_del(&mr->mr_list); 139 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); 146 } 147 148 static int 149 __frwr_mr_reset(struct rpcrdma_ia *ia, struct rpcrdma_mr *mr) 150 { 151 struct rpcrdma_frwr *frwr = &mr->frwr; 152 int rc; 153 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; 159 } 160 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); 167 } 168 169 dprintk("RPC: %s: recovered FRWR %p\n", __func__, frwr); 170 frwr->fr_state = FRWR_IS_INVALID; 171 return 0; 172 } 173 174 /* Reset of a single FRWR. Generate a fresh rkey by replacing the MR. 175 */ 176 static void 177 frwr_op_recover_mr(struct rpcrdma_mr *mr) 178 { 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; 183 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); 189 } 190 if (rc) 191 goto out_release; 192 193 rpcrdma_mr_put(mr); 194 r_xprt->rx_stats.mrs_recovered++; 195 return; 196 197 out_release: 198 pr_err("rpcrdma: FRWR reset failed %d, %p release\n", rc, mr); 199 r_xprt->rx_stats.mrs_orphaned++; 200 201 spin_lock(&r_xprt->rx_buf.rb_mrlock); 202 list_del(&mr->mr_all); 203 spin_unlock(&r_xprt->rx_buf.rb_mrlock); 204 205 frwr_op_release_mr(mr); 206 } 207 208 static int 209 frwr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep, 210 struct rpcrdma_create_data_internal *cdata) 211 { 212 struct ib_device_attr *attrs = &ia->ri_device->attrs; 213 int depth, delta; 214 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; 218 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); 224 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 233 */ 234 depth = 7; 235 236 /* Calculate N if the device max FRWR depth is smaller than 237 * RPCRDMA_MAX_DATA_SEGS. 238 */ 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); 245 } 246 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; 254 } 255 256 ia->ri_max_segs = max_t(unsigned int, 1, RPCRDMA_MAX_DATA_SEGS / 257 ia->ri_max_frwr_depth); 258 return 0; 259 } 260 261 /* FRWR mode conveys a list of pages per chunk segment. The 262 * maximum length of that list is the FRWR page list depth. 263 */ 264 static size_t 265 frwr_op_maxpages(struct rpcrdma_xprt *r_xprt) 266 { 267 struct rpcrdma_ia *ia = &r_xprt->rx_ia; 268 269 return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS, 270 RPCRDMA_MAX_HDR_SEGS * ia->ri_max_frwr_depth); 271 } 272 273 static void 274 __frwr_sendcompletion_flush(struct ib_wc *wc, const char *wr) 275 { 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); 280 } 281 282 /** 283 * frwr_wc_fastreg - Invoked by RDMA provider for a flushed FastReg WC 284 * @cq: completion queue (ignored) 285 * @wc: completed WR 286 * 287 */ 288 static void 289 frwr_wc_fastreg(struct ib_cq *cq, struct ib_wc *wc) 290 { 291 struct ib_cqe *cqe = wc->wr_cqe; 292 struct rpcrdma_frwr *frwr = 293 container_of(cqe, struct rpcrdma_frwr, fr_cqe); 294 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"); 299 } 300 trace_xprtrdma_wc_fastreg(wc, frwr); 301 } 302 303 /** 304 * frwr_wc_localinv - Invoked by RDMA provider for a flushed LocalInv WC 305 * @cq: completion queue (ignored) 306 * @wc: completed WR 307 * 308 */ 309 static void 310 frwr_wc_localinv(struct ib_cq *cq, struct ib_wc *wc) 311 { 312 struct ib_cqe *cqe = wc->wr_cqe; 313 struct rpcrdma_frwr *frwr = container_of(cqe, struct rpcrdma_frwr, 314 fr_cqe); 315 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"); 320 } 321 trace_xprtrdma_wc_li(wc, frwr); 322 } 323 324 /** 325 * frwr_wc_localinv_wake - Invoked by RDMA provider for a signaled LocalInv WC 326 * @cq: completion queue (ignored) 327 * @wc: completed WR 328 * 329 * Awaken anyone waiting for an MR to finish being fenced. 330 */ 331 static void 332 frwr_wc_localinv_wake(struct ib_cq *cq, struct ib_wc *wc) 333 { 334 struct ib_cqe *cqe = wc->wr_cqe; 335 struct rpcrdma_frwr *frwr = container_of(cqe, struct rpcrdma_frwr, 336 fr_cqe); 337 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"); 342 } 343 complete(&frwr->fr_linv_done); 344 trace_xprtrdma_wc_li_wake(wc, frwr); 345 } 346 347 /* Post a REG_MR Work Request to register a memory region 348 * for remote access via RDMA READ or RDMA WRITE. 349 */ 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) 353 { 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 int i, n; 361 u8 key; 362 363 mr = NULL; 364 do { 365 if (mr) 366 rpcrdma_mr_defer_recovery(mr); 367 mr = rpcrdma_mr_get(r_xprt); 368 if (!mr) 369 return ERR_PTR(-EAGAIN); 370 } while (mr->frwr.fr_state != FRWR_IS_INVALID); 371 frwr = &mr->frwr; 372 frwr->fr_state = FRWR_IS_VALID; 373 374 if (nsegs > ia->ri_max_frwr_depth) 375 nsegs = ia->ri_max_frwr_depth; 376 for (i = 0; i < nsegs;) { 377 if (seg->mr_page) 378 sg_set_page(&mr->mr_sg[i], 379 seg->mr_page, 380 seg->mr_len, 381 offset_in_page(seg->mr_offset)); 382 else 383 sg_set_buf(&mr->mr_sg[i], seg->mr_offset, 384 seg->mr_len); 385 386 ++seg; 387 ++i; 388 if (holes_ok) 389 continue; 390 if ((i < nsegs && offset_in_page(seg->mr_offset)) || 391 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len)) 392 break; 393 } 394 mr->mr_dir = rpcrdma_data_dir(writing); 395 396 mr->mr_nents = ib_dma_map_sg(ia->ri_device, mr->mr_sg, i, mr->mr_dir); 397 if (!mr->mr_nents) 398 goto out_dmamap_err; 399 400 ibmr = frwr->fr_mr; 401 n = ib_map_mr_sg(ibmr, mr->mr_sg, mr->mr_nents, NULL, PAGE_SIZE); 402 if (unlikely(n != mr->mr_nents)) 403 goto out_mapmr_err; 404 405 key = (u8)(ibmr->rkey & 0x000000FF); 406 ib_update_fast_reg_key(ibmr, ++key); 407 408 reg_wr = &frwr->fr_regwr; 409 reg_wr->mr = ibmr; 410 reg_wr->key = ibmr->rkey; 411 reg_wr->access = writing ? 412 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE : 413 IB_ACCESS_REMOTE_READ; 414 415 mr->mr_handle = ibmr->rkey; 416 mr->mr_length = ibmr->length; 417 mr->mr_offset = ibmr->iova; 418 419 *out = mr; 420 return seg; 421 422 out_dmamap_err: 423 pr_err("rpcrdma: failed to DMA map sg %p sg_nents %d\n", 424 mr->mr_sg, i); 425 frwr->fr_state = FRWR_IS_INVALID; 426 rpcrdma_mr_put(mr); 427 return ERR_PTR(-EIO); 428 429 out_mapmr_err: 430 pr_err("rpcrdma: failed to map mr %p (%d/%d)\n", 431 frwr->fr_mr, n, mr->mr_nents); 432 rpcrdma_mr_defer_recovery(mr); 433 return ERR_PTR(-EIO); 434 } 435 436 /* Post Send WR containing the RPC Call message. 437 * 438 * For FRMR, chain any FastReg WRs to the Send WR. Only a 439 * single ib_post_send call is needed to register memory 440 * and then post the Send WR. 441 */ 442 static int 443 frwr_op_send(struct rpcrdma_ia *ia, struct rpcrdma_req *req) 444 { 445 struct ib_send_wr *post_wr, *bad_wr; 446 struct rpcrdma_mr *mr; 447 448 post_wr = &req->rl_sendctx->sc_wr; 449 list_for_each_entry(mr, &req->rl_registered, mr_list) { 450 struct rpcrdma_frwr *frwr; 451 452 frwr = &mr->frwr; 453 454 frwr->fr_cqe.done = frwr_wc_fastreg; 455 frwr->fr_regwr.wr.next = post_wr; 456 frwr->fr_regwr.wr.wr_cqe = &frwr->fr_cqe; 457 frwr->fr_regwr.wr.num_sge = 0; 458 frwr->fr_regwr.wr.opcode = IB_WR_REG_MR; 459 frwr->fr_regwr.wr.send_flags = 0; 460 461 post_wr = &frwr->fr_regwr.wr; 462 } 463 464 /* If ib_post_send fails, the next ->send_request for 465 * @req will queue these MWs for recovery. 466 */ 467 return ib_post_send(ia->ri_id->qp, post_wr, &bad_wr); 468 } 469 470 /* Handle a remotely invalidated mr on the @mrs list 471 */ 472 static void 473 frwr_op_reminv(struct rpcrdma_rep *rep, struct list_head *mrs) 474 { 475 struct rpcrdma_mr *mr; 476 477 list_for_each_entry(mr, mrs, mr_list) 478 if (mr->mr_handle == rep->rr_inv_rkey) { 479 list_del(&mr->mr_list); 480 trace_xprtrdma_remoteinv(mr); 481 mr->frwr.fr_state = FRWR_IS_INVALID; 482 rpcrdma_mr_unmap_and_put(mr); 483 break; /* only one invalidated MR per RPC */ 484 } 485 } 486 487 /* Invalidate all memory regions that were registered for "req". 488 * 489 * Sleeps until it is safe for the host CPU to access the 490 * previously mapped memory regions. 491 * 492 * Caller ensures that @mrs is not empty before the call. This 493 * function empties the list. 494 */ 495 static void 496 frwr_op_unmap_sync(struct rpcrdma_xprt *r_xprt, struct list_head *mrs) 497 { 498 struct ib_send_wr *first, **prev, *last, *bad_wr; 499 struct rpcrdma_ia *ia = &r_xprt->rx_ia; 500 struct rpcrdma_frwr *frwr; 501 struct rpcrdma_mr *mr; 502 int count, rc; 503 504 /* ORDER: Invalidate all of the MRs first 505 * 506 * Chain the LOCAL_INV Work Requests and post them with 507 * a single ib_post_send() call. 508 */ 509 frwr = NULL; 510 count = 0; 511 prev = &first; 512 list_for_each_entry(mr, mrs, mr_list) { 513 mr->frwr.fr_state = FRWR_IS_INVALID; 514 515 frwr = &mr->frwr; 516 trace_xprtrdma_localinv(mr); 517 518 frwr->fr_cqe.done = frwr_wc_localinv; 519 last = &frwr->fr_invwr; 520 memset(last, 0, sizeof(*last)); 521 last->wr_cqe = &frwr->fr_cqe; 522 last->opcode = IB_WR_LOCAL_INV; 523 last->ex.invalidate_rkey = mr->mr_handle; 524 count++; 525 526 *prev = last; 527 prev = &last->next; 528 } 529 if (!frwr) 530 goto unmap; 531 532 /* Strong send queue ordering guarantees that when the 533 * last WR in the chain completes, all WRs in the chain 534 * are complete. 535 */ 536 last->send_flags = IB_SEND_SIGNALED; 537 frwr->fr_cqe.done = frwr_wc_localinv_wake; 538 reinit_completion(&frwr->fr_linv_done); 539 540 /* Transport disconnect drains the receive CQ before it 541 * replaces the QP. The RPC reply handler won't call us 542 * unless ri_id->qp is a valid pointer. 543 */ 544 r_xprt->rx_stats.local_inv_needed++; 545 bad_wr = NULL; 546 rc = ib_post_send(ia->ri_id->qp, first, &bad_wr); 547 if (bad_wr != first) 548 wait_for_completion(&frwr->fr_linv_done); 549 if (rc) 550 goto reset_mrs; 551 552 /* ORDER: Now DMA unmap all of the MRs, and return 553 * them to the free MR list. 554 */ 555 unmap: 556 while (!list_empty(mrs)) { 557 mr = rpcrdma_mr_pop(mrs); 558 rpcrdma_mr_unmap_and_put(mr); 559 } 560 return; 561 562 reset_mrs: 563 pr_err("rpcrdma: FRWR invalidate ib_post_send returned %i\n", rc); 564 565 /* Find and reset the MRs in the LOCAL_INV WRs that did not 566 * get posted. 567 */ 568 while (bad_wr) { 569 frwr = container_of(bad_wr, struct rpcrdma_frwr, 570 fr_invwr); 571 mr = container_of(frwr, struct rpcrdma_mr, frwr); 572 573 __frwr_mr_reset(ia, mr); 574 575 bad_wr = bad_wr->next; 576 } 577 goto unmap; 578 } 579 580 const struct rpcrdma_memreg_ops rpcrdma_frwr_memreg_ops = { 581 .ro_map = frwr_op_map, 582 .ro_send = frwr_op_send, 583 .ro_reminv = frwr_op_reminv, 584 .ro_unmap_sync = frwr_op_unmap_sync, 585 .ro_recover_mr = frwr_op_recover_mr, 586 .ro_open = frwr_op_open, 587 .ro_maxpages = frwr_op_maxpages, 588 .ro_init_mr = frwr_op_init_mr, 589 .ro_release_mr = frwr_op_release_mr, 590 .ro_displayname = "frwr", 591 .ro_send_w_inv_ok = RPCRDMA_CMP_F_SND_W_INV_OK, 592 }; 593