Merge branch 'kbuild' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[deliverable/linux.git] / net / sunrpc / xprtrdma / frwr_ops.c
1 /*
2 * Copyright (c) 2015 Oracle. All rights reserved.
3 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
4 */
5
6 /* Lightweight memory registration using Fast Registration Work
7 * Requests (FRWR). Also referred to sometimes as FRMR mode.
8 *
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.
12 */
13
14 /* Normal operation
15 *
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
19 * (frmr_op_unmap).
20 *
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
24 * interrupt workload.
25 *
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.
30 *
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.
35 */
36
37 /* Transport recovery
38 *
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
44 * being done.
45 *
46 * When the underlying transport disconnects, MRs are left in one of
47 * three states:
48 *
49 * INVALID: The MR was not in use before the QP entered ERROR state.
50 * (Or, the LOCAL_INV WR has not completed or flushed yet).
51 *
52 * STALE: The MR was being registered or unregistered when the QP
53 * entered ERROR state, and the pending WR was flushed.
54 *
55 * VALID: The MR was registered before the QP entered ERROR state.
56 *
57 * When frwr_op_map encounters STALE and VALID MRs, they are recovered
58 * with ib_dereg_mr and then are re-initialized. Beause MR recovery
59 * allocates fresh resources, it is deferred to a workqueue, and the
60 * recovered MRs are placed back on the rb_mws list when recovery is
61 * complete. frwr_op_map allocates another MR for the current RPC while
62 * the broken MR is reset.
63 *
64 * To ensure that frwr_op_map doesn't encounter an MR that is marked
65 * INVALID but that is about to be flushed due to a previous transport
66 * disconnect, the transport connect worker attempts to drain all
67 * pending send queue WRs before the transport is reconnected.
68 */
69
70 #include "xprt_rdma.h"
71
72 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
73 # define RPCDBG_FACILITY RPCDBG_TRANS
74 #endif
75
76 static struct workqueue_struct *frwr_recovery_wq;
77
78 #define FRWR_RECOVERY_WQ_FLAGS (WQ_UNBOUND | WQ_MEM_RECLAIM)
79
80 int
81 frwr_alloc_recovery_wq(void)
82 {
83 frwr_recovery_wq = alloc_workqueue("frwr_recovery",
84 FRWR_RECOVERY_WQ_FLAGS, 0);
85 return !frwr_recovery_wq ? -ENOMEM : 0;
86 }
87
88 void
89 frwr_destroy_recovery_wq(void)
90 {
91 struct workqueue_struct *wq;
92
93 if (!frwr_recovery_wq)
94 return;
95
96 wq = frwr_recovery_wq;
97 frwr_recovery_wq = NULL;
98 destroy_workqueue(wq);
99 }
100
101 /* Deferred reset of a single FRMR. Generate a fresh rkey by
102 * replacing the MR.
103 *
104 * There's no recovery if this fails. The FRMR is abandoned, but
105 * remains in rb_all. It will be cleaned up when the transport is
106 * destroyed.
107 */
108 static void
109 __frwr_recovery_worker(struct work_struct *work)
110 {
111 struct rpcrdma_mw *r = container_of(work, struct rpcrdma_mw,
112 r.frmr.fr_work);
113 struct rpcrdma_xprt *r_xprt = r->r.frmr.fr_xprt;
114 unsigned int depth = r_xprt->rx_ia.ri_max_frmr_depth;
115 struct ib_pd *pd = r_xprt->rx_ia.ri_pd;
116
117 if (ib_dereg_mr(r->r.frmr.fr_mr))
118 goto out_fail;
119
120 r->r.frmr.fr_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG, depth);
121 if (IS_ERR(r->r.frmr.fr_mr))
122 goto out_fail;
123
124 dprintk("RPC: %s: recovered FRMR %p\n", __func__, r);
125 r->r.frmr.fr_state = FRMR_IS_INVALID;
126 rpcrdma_put_mw(r_xprt, r);
127 return;
128
129 out_fail:
130 pr_warn("RPC: %s: FRMR %p unrecovered\n",
131 __func__, r);
132 }
133
134 /* A broken MR was discovered in a context that can't sleep.
135 * Defer recovery to the recovery worker.
136 */
137 static void
138 __frwr_queue_recovery(struct rpcrdma_mw *r)
139 {
140 INIT_WORK(&r->r.frmr.fr_work, __frwr_recovery_worker);
141 queue_work(frwr_recovery_wq, &r->r.frmr.fr_work);
142 }
143
144 static int
145 __frwr_init(struct rpcrdma_mw *r, struct ib_pd *pd, struct ib_device *device,
146 unsigned int depth)
147 {
148 struct rpcrdma_frmr *f = &r->r.frmr;
149 int rc;
150
151 f->fr_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG, depth);
152 if (IS_ERR(f->fr_mr))
153 goto out_mr_err;
154
155 f->sg = kcalloc(depth, sizeof(*f->sg), GFP_KERNEL);
156 if (!f->sg)
157 goto out_list_err;
158
159 sg_init_table(f->sg, depth);
160
161 return 0;
162
163 out_mr_err:
164 rc = PTR_ERR(f->fr_mr);
165 dprintk("RPC: %s: ib_alloc_mr status %i\n",
166 __func__, rc);
167 return rc;
168
169 out_list_err:
170 rc = -ENOMEM;
171 dprintk("RPC: %s: sg allocation failure\n",
172 __func__);
173 ib_dereg_mr(f->fr_mr);
174 return rc;
175 }
176
177 static void
178 __frwr_release(struct rpcrdma_mw *r)
179 {
180 int rc;
181
182 rc = ib_dereg_mr(r->r.frmr.fr_mr);
183 if (rc)
184 dprintk("RPC: %s: ib_dereg_mr status %i\n",
185 __func__, rc);
186 kfree(r->r.frmr.sg);
187 }
188
189 static int
190 frwr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep,
191 struct rpcrdma_create_data_internal *cdata)
192 {
193 struct ib_device_attr *devattr = &ia->ri_devattr;
194 int depth, delta;
195
196 ia->ri_max_frmr_depth =
197 min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
198 devattr->max_fast_reg_page_list_len);
199 dprintk("RPC: %s: device's max FR page list len = %u\n",
200 __func__, ia->ri_max_frmr_depth);
201
202 /* Add room for frmr register and invalidate WRs.
203 * 1. FRMR reg WR for head
204 * 2. FRMR invalidate WR for head
205 * 3. N FRMR reg WRs for pagelist
206 * 4. N FRMR invalidate WRs for pagelist
207 * 5. FRMR reg WR for tail
208 * 6. FRMR invalidate WR for tail
209 * 7. The RDMA_SEND WR
210 */
211 depth = 7;
212
213 /* Calculate N if the device max FRMR depth is smaller than
214 * RPCRDMA_MAX_DATA_SEGS.
215 */
216 if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) {
217 delta = RPCRDMA_MAX_DATA_SEGS - ia->ri_max_frmr_depth;
218 do {
219 depth += 2; /* FRMR reg + invalidate */
220 delta -= ia->ri_max_frmr_depth;
221 } while (delta > 0);
222 }
223
224 ep->rep_attr.cap.max_send_wr *= depth;
225 if (ep->rep_attr.cap.max_send_wr > devattr->max_qp_wr) {
226 cdata->max_requests = devattr->max_qp_wr / depth;
227 if (!cdata->max_requests)
228 return -EINVAL;
229 ep->rep_attr.cap.max_send_wr = cdata->max_requests *
230 depth;
231 }
232
233 return 0;
234 }
235
236 /* FRWR mode conveys a list of pages per chunk segment. The
237 * maximum length of that list is the FRWR page list depth.
238 */
239 static size_t
240 frwr_op_maxpages(struct rpcrdma_xprt *r_xprt)
241 {
242 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
243
244 return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
245 rpcrdma_max_segments(r_xprt) * ia->ri_max_frmr_depth);
246 }
247
248 /* If FAST_REG or LOCAL_INV failed, indicate the frmr needs
249 * to be reset.
250 *
251 * WARNING: Only wr_id and status are reliable at this point
252 */
253 static void
254 __frwr_sendcompletion_flush(struct ib_wc *wc, struct rpcrdma_mw *r)
255 {
256 if (likely(wc->status == IB_WC_SUCCESS))
257 return;
258
259 /* WARNING: Only wr_id and status are reliable at this point */
260 r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
261 if (wc->status == IB_WC_WR_FLUSH_ERR)
262 dprintk("RPC: %s: frmr %p flushed\n", __func__, r);
263 else
264 pr_warn("RPC: %s: frmr %p error, status %s (%d)\n",
265 __func__, r, ib_wc_status_msg(wc->status), wc->status);
266
267 r->r.frmr.fr_state = FRMR_IS_STALE;
268 }
269
270 static void
271 frwr_sendcompletion(struct ib_wc *wc)
272 {
273 struct rpcrdma_mw *r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
274 struct rpcrdma_frmr *f = &r->r.frmr;
275
276 if (unlikely(wc->status != IB_WC_SUCCESS))
277 __frwr_sendcompletion_flush(wc, r);
278
279 if (f->fr_waiter)
280 complete(&f->fr_linv_done);
281 }
282
283 static int
284 frwr_op_init(struct rpcrdma_xprt *r_xprt)
285 {
286 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
287 struct ib_device *device = r_xprt->rx_ia.ri_device;
288 unsigned int depth = r_xprt->rx_ia.ri_max_frmr_depth;
289 struct ib_pd *pd = r_xprt->rx_ia.ri_pd;
290 int i;
291
292 spin_lock_init(&buf->rb_mwlock);
293 INIT_LIST_HEAD(&buf->rb_mws);
294 INIT_LIST_HEAD(&buf->rb_all);
295
296 i = max_t(int, RPCRDMA_MAX_DATA_SEGS / depth, 1);
297 i += 2; /* head + tail */
298 i *= buf->rb_max_requests; /* one set for each RPC slot */
299 dprintk("RPC: %s: initalizing %d FRMRs\n", __func__, i);
300
301 while (i--) {
302 struct rpcrdma_mw *r;
303 int rc;
304
305 r = kzalloc(sizeof(*r), GFP_KERNEL);
306 if (!r)
307 return -ENOMEM;
308
309 rc = __frwr_init(r, pd, device, depth);
310 if (rc) {
311 kfree(r);
312 return rc;
313 }
314
315 list_add(&r->mw_list, &buf->rb_mws);
316 list_add(&r->mw_all, &buf->rb_all);
317 r->mw_sendcompletion = frwr_sendcompletion;
318 r->r.frmr.fr_xprt = r_xprt;
319 }
320
321 return 0;
322 }
323
324 /* Post a FAST_REG Work Request to register a memory region
325 * for remote access via RDMA READ or RDMA WRITE.
326 */
327 static int
328 frwr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg,
329 int nsegs, bool writing)
330 {
331 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
332 struct ib_device *device = ia->ri_device;
333 enum dma_data_direction direction = rpcrdma_data_dir(writing);
334 struct rpcrdma_mr_seg *seg1 = seg;
335 struct rpcrdma_mw *mw;
336 struct rpcrdma_frmr *frmr;
337 struct ib_mr *mr;
338 struct ib_reg_wr *reg_wr;
339 struct ib_send_wr *bad_wr;
340 int rc, i, n, dma_nents;
341 u8 key;
342
343 mw = seg1->rl_mw;
344 seg1->rl_mw = NULL;
345 do {
346 if (mw)
347 __frwr_queue_recovery(mw);
348 mw = rpcrdma_get_mw(r_xprt);
349 if (!mw)
350 return -ENOMEM;
351 } while (mw->r.frmr.fr_state != FRMR_IS_INVALID);
352 frmr = &mw->r.frmr;
353 frmr->fr_state = FRMR_IS_VALID;
354 frmr->fr_waiter = false;
355 mr = frmr->fr_mr;
356 reg_wr = &frmr->fr_regwr;
357
358 if (nsegs > ia->ri_max_frmr_depth)
359 nsegs = ia->ri_max_frmr_depth;
360
361 for (i = 0; i < nsegs;) {
362 if (seg->mr_page)
363 sg_set_page(&frmr->sg[i],
364 seg->mr_page,
365 seg->mr_len,
366 offset_in_page(seg->mr_offset));
367 else
368 sg_set_buf(&frmr->sg[i], seg->mr_offset,
369 seg->mr_len);
370
371 ++seg;
372 ++i;
373
374 /* Check for holes */
375 if ((i < nsegs && offset_in_page(seg->mr_offset)) ||
376 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
377 break;
378 }
379 frmr->sg_nents = i;
380
381 dma_nents = ib_dma_map_sg(device, frmr->sg, frmr->sg_nents, direction);
382 if (!dma_nents) {
383 pr_err("RPC: %s: failed to dma map sg %p sg_nents %u\n",
384 __func__, frmr->sg, frmr->sg_nents);
385 return -ENOMEM;
386 }
387
388 n = ib_map_mr_sg(mr, frmr->sg, frmr->sg_nents, PAGE_SIZE);
389 if (unlikely(n != frmr->sg_nents)) {
390 pr_err("RPC: %s: failed to map mr %p (%u/%u)\n",
391 __func__, frmr->fr_mr, n, frmr->sg_nents);
392 rc = n < 0 ? n : -EINVAL;
393 goto out_senderr;
394 }
395
396 dprintk("RPC: %s: Using frmr %p to map %u segments (%u bytes)\n",
397 __func__, mw, frmr->sg_nents, mr->length);
398
399 key = (u8)(mr->rkey & 0x000000FF);
400 ib_update_fast_reg_key(mr, ++key);
401
402 reg_wr->wr.next = NULL;
403 reg_wr->wr.opcode = IB_WR_REG_MR;
404 reg_wr->wr.wr_id = (uintptr_t)mw;
405 reg_wr->wr.num_sge = 0;
406 reg_wr->wr.send_flags = 0;
407 reg_wr->mr = mr;
408 reg_wr->key = mr->rkey;
409 reg_wr->access = writing ?
410 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
411 IB_ACCESS_REMOTE_READ;
412
413 DECR_CQCOUNT(&r_xprt->rx_ep);
414 rc = ib_post_send(ia->ri_id->qp, &reg_wr->wr, &bad_wr);
415 if (rc)
416 goto out_senderr;
417
418 seg1->mr_dir = direction;
419 seg1->rl_mw = mw;
420 seg1->mr_rkey = mr->rkey;
421 seg1->mr_base = mr->iova;
422 seg1->mr_nsegs = frmr->sg_nents;
423 seg1->mr_len = mr->length;
424
425 return frmr->sg_nents;
426
427 out_senderr:
428 dprintk("RPC: %s: ib_post_send status %i\n", __func__, rc);
429 ib_dma_unmap_sg(device, frmr->sg, dma_nents, direction);
430 __frwr_queue_recovery(mw);
431 return rc;
432 }
433
434 static struct ib_send_wr *
435 __frwr_prepare_linv_wr(struct rpcrdma_mr_seg *seg)
436 {
437 struct rpcrdma_mw *mw = seg->rl_mw;
438 struct rpcrdma_frmr *f = &mw->r.frmr;
439 struct ib_send_wr *invalidate_wr;
440
441 f->fr_waiter = false;
442 f->fr_state = FRMR_IS_INVALID;
443 invalidate_wr = &f->fr_invwr;
444
445 memset(invalidate_wr, 0, sizeof(*invalidate_wr));
446 invalidate_wr->wr_id = (unsigned long)(void *)mw;
447 invalidate_wr->opcode = IB_WR_LOCAL_INV;
448 invalidate_wr->ex.invalidate_rkey = f->fr_mr->rkey;
449
450 return invalidate_wr;
451 }
452
453 static void
454 __frwr_dma_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg,
455 int rc)
456 {
457 struct ib_device *device = r_xprt->rx_ia.ri_device;
458 struct rpcrdma_mw *mw = seg->rl_mw;
459 struct rpcrdma_frmr *f = &mw->r.frmr;
460
461 seg->rl_mw = NULL;
462
463 ib_dma_unmap_sg(device, f->sg, f->sg_nents, seg->mr_dir);
464
465 if (!rc)
466 rpcrdma_put_mw(r_xprt, mw);
467 else
468 __frwr_queue_recovery(mw);
469 }
470
471 /* Invalidate all memory regions that were registered for "req".
472 *
473 * Sleeps until it is safe for the host CPU to access the
474 * previously mapped memory regions.
475 */
476 static void
477 frwr_op_unmap_sync(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
478 {
479 struct ib_send_wr *invalidate_wrs, *pos, *prev, *bad_wr;
480 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
481 struct rpcrdma_mr_seg *seg;
482 unsigned int i, nchunks;
483 struct rpcrdma_frmr *f;
484 int rc;
485
486 dprintk("RPC: %s: req %p\n", __func__, req);
487
488 /* ORDER: Invalidate all of the req's MRs first
489 *
490 * Chain the LOCAL_INV Work Requests and post them with
491 * a single ib_post_send() call.
492 */
493 invalidate_wrs = pos = prev = NULL;
494 seg = NULL;
495 for (i = 0, nchunks = req->rl_nchunks; nchunks; nchunks--) {
496 seg = &req->rl_segments[i];
497
498 pos = __frwr_prepare_linv_wr(seg);
499
500 if (!invalidate_wrs)
501 invalidate_wrs = pos;
502 else
503 prev->next = pos;
504 prev = pos;
505
506 i += seg->mr_nsegs;
507 }
508 f = &seg->rl_mw->r.frmr;
509
510 /* Strong send queue ordering guarantees that when the
511 * last WR in the chain completes, all WRs in the chain
512 * are complete.
513 */
514 f->fr_invwr.send_flags = IB_SEND_SIGNALED;
515 f->fr_waiter = true;
516 init_completion(&f->fr_linv_done);
517 INIT_CQCOUNT(&r_xprt->rx_ep);
518
519 /* Transport disconnect drains the receive CQ before it
520 * replaces the QP. The RPC reply handler won't call us
521 * unless ri_id->qp is a valid pointer.
522 */
523 rc = ib_post_send(ia->ri_id->qp, invalidate_wrs, &bad_wr);
524 if (rc)
525 pr_warn("%s: ib_post_send failed %i\n", __func__, rc);
526
527 wait_for_completion(&f->fr_linv_done);
528
529 /* ORDER: Now DMA unmap all of the req's MRs, and return
530 * them to the free MW list.
531 */
532 for (i = 0, nchunks = req->rl_nchunks; nchunks; nchunks--) {
533 seg = &req->rl_segments[i];
534
535 __frwr_dma_unmap(r_xprt, seg, rc);
536
537 i += seg->mr_nsegs;
538 seg->mr_nsegs = 0;
539 }
540
541 req->rl_nchunks = 0;
542 }
543
544 /* Post a LOCAL_INV Work Request to prevent further remote access
545 * via RDMA READ or RDMA WRITE.
546 */
547 static int
548 frwr_op_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg)
549 {
550 struct rpcrdma_mr_seg *seg1 = seg;
551 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
552 struct rpcrdma_mw *mw = seg1->rl_mw;
553 struct rpcrdma_frmr *frmr = &mw->r.frmr;
554 struct ib_send_wr *invalidate_wr, *bad_wr;
555 int rc, nsegs = seg->mr_nsegs;
556
557 dprintk("RPC: %s: FRMR %p\n", __func__, mw);
558
559 seg1->rl_mw = NULL;
560 frmr->fr_state = FRMR_IS_INVALID;
561 invalidate_wr = &mw->r.frmr.fr_invwr;
562
563 memset(invalidate_wr, 0, sizeof(*invalidate_wr));
564 invalidate_wr->wr_id = (uintptr_t)mw;
565 invalidate_wr->opcode = IB_WR_LOCAL_INV;
566 invalidate_wr->ex.invalidate_rkey = frmr->fr_mr->rkey;
567 DECR_CQCOUNT(&r_xprt->rx_ep);
568
569 ib_dma_unmap_sg(ia->ri_device, frmr->sg, frmr->sg_nents, seg1->mr_dir);
570 read_lock(&ia->ri_qplock);
571 rc = ib_post_send(ia->ri_id->qp, invalidate_wr, &bad_wr);
572 read_unlock(&ia->ri_qplock);
573 if (rc)
574 goto out_err;
575
576 rpcrdma_put_mw(r_xprt, mw);
577 return nsegs;
578
579 out_err:
580 dprintk("RPC: %s: ib_post_send status %i\n", __func__, rc);
581 __frwr_queue_recovery(mw);
582 return nsegs;
583 }
584
585 static void
586 frwr_op_destroy(struct rpcrdma_buffer *buf)
587 {
588 struct rpcrdma_mw *r;
589
590 /* Ensure stale MWs for "buf" are no longer in flight */
591 flush_workqueue(frwr_recovery_wq);
592
593 while (!list_empty(&buf->rb_all)) {
594 r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
595 list_del(&r->mw_all);
596 __frwr_release(r);
597 kfree(r);
598 }
599 }
600
601 const struct rpcrdma_memreg_ops rpcrdma_frwr_memreg_ops = {
602 .ro_map = frwr_op_map,
603 .ro_unmap_sync = frwr_op_unmap_sync,
604 .ro_unmap = frwr_op_unmap,
605 .ro_open = frwr_op_open,
606 .ro_maxpages = frwr_op_maxpages,
607 .ro_init = frwr_op_init,
608 .ro_destroy = frwr_op_destroy,
609 .ro_displayname = "frwr",
610 };
This page took 0.080552 seconds and 5 git commands to generate.