1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (c) 2016-2018 Oracle. All rights reserved.
4 * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the BSD-type
11 * license below:
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials provided
23 * with the distribution.
24 *
25 * Neither the name of the Network Appliance, Inc. nor the names of
26 * its contributors may be used to endorse or promote products
27 * derived from this software without specific prior written
28 * permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Author: Tom Tucker <tom@opengridcomputing.com>
43 */
44
45 /* Operation
46 *
47 * The main entry point is svc_rdma_recvfrom. This is called from
48 * svc_recv when the transport indicates there is incoming data to
49 * be read. "Data Ready" is signaled when an RDMA Receive completes,
50 * or when a set of RDMA Reads complete.
51 *
52 * An svc_rqst is passed in. This structure contains an array of
53 * free pages (rq_pages) that will contain the incoming RPC message.
54 *
55 * Short messages are moved directly into svc_rqst::rq_arg, and
56 * the RPC Call is ready to be processed by the Upper Layer.
57 * svc_rdma_recvfrom returns the length of the RPC Call message,
58 * completing the reception of the RPC Call.
59 *
60 * However, when an incoming message has Read chunks,
61 * svc_rdma_recvfrom must post RDMA Reads to pull the RPC Call's
62 * data payload from the client. svc_rdma_recvfrom sets up the
63 * RDMA Reads using pages in svc_rqst::rq_pages, which are
64 * transferred to an svc_rdma_recv_ctxt for the duration of the
65 * I/O. svc_rdma_recvfrom then returns zero, since the RPC message
66 * is still not yet ready.
67 *
68 * When the Read chunk payloads have become available on the
69 * server, "Data Ready" is raised again, and svc_recv calls
70 * svc_rdma_recvfrom again. This second call may use a different
71 * svc_rqst than the first one, thus any information that needs
72 * to be preserved across these two calls is kept in an
73 * svc_rdma_recv_ctxt.
74 *
75 * The second call to svc_rdma_recvfrom performs final assembly
76 * of the RPC Call message, using the RDMA Read sink pages kept in
77 * the svc_rdma_recv_ctxt. The xdr_buf is copied from the
78 * svc_rdma_recv_ctxt to the second svc_rqst. The second call returns
79 * the length of the completed RPC Call message.
80 *
81 * Page Management
82 *
83 * Pages under I/O must be transferred from the first svc_rqst to an
84 * svc_rdma_recv_ctxt before the first svc_rdma_recvfrom call returns.
85 *
86 * The first svc_rqst supplies pages for RDMA Reads. These are moved
87 * from rqstp::rq_pages into ctxt::pages. The consumed elements of
88 * the rq_pages array are set to NULL and refilled with the first
89 * svc_rdma_recvfrom call returns.
90 *
91 * During the second svc_rdma_recvfrom call, RDMA Read sink pages
92 * are transferred from the svc_rdma_recv_ctxt to the second svc_rqst.
93 */
94
95 #include <linux/slab.h>
96 #include <linux/spinlock.h>
97 #include <linux/unaligned.h>
98 #include <rdma/ib_verbs.h>
99 #include <rdma/rdma_cm.h>
100
101 #include <linux/sunrpc/xdr.h>
102 #include <linux/sunrpc/debug.h>
103 #include <linux/sunrpc/rpc_rdma.h>
104 #include <linux/sunrpc/svc_rdma.h>
105
106 #include "xprt_rdma.h"
107 #include <trace/events/rpcrdma.h>
108
109 static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc);
110
111 static inline struct svc_rdma_recv_ctxt *
svc_rdma_next_recv_ctxt(struct list_head * list)112 svc_rdma_next_recv_ctxt(struct list_head *list)
113 {
114 return list_first_entry_or_null(list, struct svc_rdma_recv_ctxt,
115 rc_list);
116 }
117
118 static struct svc_rdma_recv_ctxt *
svc_rdma_recv_ctxt_alloc(struct svcxprt_rdma * rdma)119 svc_rdma_recv_ctxt_alloc(struct svcxprt_rdma *rdma)
120 {
121 struct ib_device *device = rdma->sc_cm_id->device;
122 int node = ibdev_to_node(device);
123 struct svc_rdma_recv_ctxt *ctxt;
124 unsigned long pages;
125 dma_addr_t addr;
126 void *buffer;
127
128 pages = svc_serv_maxpages(rdma->sc_xprt.xpt_server);
129 ctxt = kzalloc_node(struct_size(ctxt, rc_pages, pages),
130 GFP_KERNEL, node);
131 if (!ctxt)
132 goto fail0;
133 ctxt->rc_maxpages = pages;
134 buffer = kmalloc_node(rdma->sc_max_req_size, GFP_KERNEL, node);
135 if (!buffer)
136 goto fail1;
137 addr = ib_dma_map_single(device, buffer, rdma->sc_max_req_size,
138 DMA_FROM_DEVICE);
139 if (ib_dma_mapping_error(device, addr))
140 goto fail2;
141
142 svc_rdma_recv_cid_init(rdma, &ctxt->rc_cid);
143 pcl_init(&ctxt->rc_call_pcl);
144 pcl_init(&ctxt->rc_read_pcl);
145 pcl_init(&ctxt->rc_write_pcl);
146 pcl_init(&ctxt->rc_reply_pcl);
147
148 ctxt->rc_recv_wr.next = NULL;
149 ctxt->rc_recv_wr.wr_cqe = &ctxt->rc_cqe;
150 ctxt->rc_recv_wr.sg_list = &ctxt->rc_recv_sge;
151 ctxt->rc_recv_wr.num_sge = 1;
152 ctxt->rc_cqe.done = svc_rdma_wc_receive;
153 ctxt->rc_recv_sge.addr = addr;
154 ctxt->rc_recv_sge.length = rdma->sc_max_req_size;
155 ctxt->rc_recv_sge.lkey = rdma->sc_pd->local_dma_lkey;
156 ctxt->rc_recv_buf = buffer;
157 svc_rdma_cc_init(rdma, &ctxt->rc_cc);
158 return ctxt;
159
160 fail2:
161 kfree(buffer);
162 fail1:
163 kfree(ctxt);
164 fail0:
165 return NULL;
166 }
167
svc_rdma_recv_ctxt_destroy(struct svcxprt_rdma * rdma,struct svc_rdma_recv_ctxt * ctxt)168 static void svc_rdma_recv_ctxt_destroy(struct svcxprt_rdma *rdma,
169 struct svc_rdma_recv_ctxt *ctxt)
170 {
171 ib_dma_unmap_single(rdma->sc_cm_id->device, ctxt->rc_recv_sge.addr,
172 ctxt->rc_recv_sge.length, DMA_FROM_DEVICE);
173 kfree(ctxt->rc_recv_buf);
174 kfree(ctxt);
175 }
176
177 /**
178 * svc_rdma_recv_ctxts_destroy - Release all recv_ctxt's for an xprt
179 * @rdma: svcxprt_rdma being torn down
180 *
181 */
svc_rdma_recv_ctxts_destroy(struct svcxprt_rdma * rdma)182 void svc_rdma_recv_ctxts_destroy(struct svcxprt_rdma *rdma)
183 {
184 struct svc_rdma_recv_ctxt *ctxt;
185 struct llist_node *node;
186
187 while ((node = llist_del_first(&rdma->sc_recv_ctxts))) {
188 ctxt = llist_entry(node, struct svc_rdma_recv_ctxt, rc_node);
189 svc_rdma_recv_ctxt_destroy(rdma, ctxt);
190 }
191 }
192
193 /**
194 * svc_rdma_recv_ctxt_get - Allocate a recv_ctxt
195 * @rdma: controlling svcxprt_rdma
196 *
197 * Returns a recv_ctxt or (rarely) NULL if none are available.
198 */
svc_rdma_recv_ctxt_get(struct svcxprt_rdma * rdma)199 struct svc_rdma_recv_ctxt *svc_rdma_recv_ctxt_get(struct svcxprt_rdma *rdma)
200 {
201 struct svc_rdma_recv_ctxt *ctxt;
202 struct llist_node *node;
203
204 node = llist_del_first(&rdma->sc_recv_ctxts);
205 if (!node)
206 return NULL;
207
208 ctxt = llist_entry(node, struct svc_rdma_recv_ctxt, rc_node);
209 ctxt->rc_page_count = 0;
210 return ctxt;
211 }
212
213 /**
214 * svc_rdma_recv_ctxt_put - Return recv_ctxt to free list
215 * @rdma: controlling svcxprt_rdma
216 * @ctxt: object to return to the free list
217 *
218 */
svc_rdma_recv_ctxt_put(struct svcxprt_rdma * rdma,struct svc_rdma_recv_ctxt * ctxt)219 void svc_rdma_recv_ctxt_put(struct svcxprt_rdma *rdma,
220 struct svc_rdma_recv_ctxt *ctxt)
221 {
222 svc_rdma_cc_release(rdma, &ctxt->rc_cc, DMA_FROM_DEVICE);
223
224 /* @rc_page_count is normally zero here, but error flows
225 * can leave pages in @rc_pages.
226 */
227 release_pages(ctxt->rc_pages, ctxt->rc_page_count);
228
229 pcl_free(&ctxt->rc_call_pcl);
230 pcl_free(&ctxt->rc_read_pcl);
231 pcl_free(&ctxt->rc_write_pcl);
232 pcl_free(&ctxt->rc_reply_pcl);
233
234 llist_add(&ctxt->rc_node, &rdma->sc_recv_ctxts);
235 }
236
237 /**
238 * svc_rdma_release_ctxt - Release transport-specific per-rqst resources
239 * @xprt: the transport which owned the context
240 * @vctxt: the context from rqstp->rq_xprt_ctxt or dr->xprt_ctxt
241 *
242 * Ensure that the recv_ctxt is released whether or not a Reply
243 * was sent. For example, the client could close the connection,
244 * or svc_process could drop an RPC, before the Reply is sent.
245 *
246 * Also drain any send_ctxts queued for deferred release so that
247 * DMA unmap and page release run in nfsd thread context between
248 * RPCs rather than on the Send completion path.
249 */
svc_rdma_release_ctxt(struct svc_xprt * xprt,void * vctxt)250 void svc_rdma_release_ctxt(struct svc_xprt *xprt, void *vctxt)
251 {
252 struct svc_rdma_recv_ctxt *ctxt = vctxt;
253 struct svcxprt_rdma *rdma =
254 container_of(xprt, struct svcxprt_rdma, sc_xprt);
255
256 if (ctxt)
257 svc_rdma_recv_ctxt_put(rdma, ctxt);
258
259 svc_rdma_send_ctxts_drain(rdma);
260 }
261
svc_rdma_refresh_recvs(struct svcxprt_rdma * rdma,unsigned int wanted)262 static bool svc_rdma_refresh_recvs(struct svcxprt_rdma *rdma,
263 unsigned int wanted)
264 {
265 const struct ib_recv_wr *bad_wr = NULL;
266 struct svc_rdma_recv_ctxt *ctxt;
267 struct ib_recv_wr *recv_chain;
268 int ret;
269
270 if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
271 return false;
272
273 recv_chain = NULL;
274 while (wanted--) {
275 ctxt = svc_rdma_recv_ctxt_get(rdma);
276 if (!ctxt)
277 break;
278
279 trace_svcrdma_post_recv(&ctxt->rc_cid);
280 ctxt->rc_recv_wr.next = recv_chain;
281 recv_chain = &ctxt->rc_recv_wr;
282 rdma->sc_pending_recvs++;
283 }
284 if (!recv_chain)
285 return true;
286
287 ret = ib_post_recv(rdma->sc_qp, recv_chain, &bad_wr);
288 if (ret)
289 goto err_free;
290 return true;
291
292 err_free:
293 trace_svcrdma_rq_post_err(rdma, ret);
294 while (bad_wr) {
295 ctxt = container_of(bad_wr, struct svc_rdma_recv_ctxt,
296 rc_recv_wr);
297 bad_wr = bad_wr->next;
298 svc_rdma_recv_ctxt_put(rdma, ctxt);
299 }
300 /* Since we're destroying the xprt, no need to reset
301 * sc_pending_recvs. */
302 return false;
303 }
304
305 /**
306 * svc_rdma_post_recvs - Post initial set of Recv WRs
307 * @rdma: fresh svcxprt_rdma
308 *
309 * Return values:
310 * %true: Receive Queue initialization successful
311 * %false: memory allocation or DMA error
312 */
svc_rdma_post_recvs(struct svcxprt_rdma * rdma)313 bool svc_rdma_post_recvs(struct svcxprt_rdma *rdma)
314 {
315 unsigned int total;
316
317 /* For each credit, allocate enough recv_ctxts for one
318 * posted Receive and one RPC in process.
319 */
320 total = (rdma->sc_max_requests * 2) + rdma->sc_recv_batch;
321 while (total--) {
322 struct svc_rdma_recv_ctxt *ctxt;
323
324 ctxt = svc_rdma_recv_ctxt_alloc(rdma);
325 if (!ctxt)
326 return false;
327 llist_add(&ctxt->rc_node, &rdma->sc_recv_ctxts);
328 }
329
330 return svc_rdma_refresh_recvs(rdma, rdma->sc_max_requests);
331 }
332
333 /**
334 * svc_rdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
335 * @cq: Completion Queue context
336 * @wc: Work Completion object
337 *
338 */
svc_rdma_wc_receive(struct ib_cq * cq,struct ib_wc * wc)339 static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
340 {
341 struct svcxprt_rdma *rdma = cq->cq_context;
342 struct ib_cqe *cqe = wc->wr_cqe;
343 struct svc_rdma_recv_ctxt *ctxt;
344
345 rdma->sc_pending_recvs--;
346
347 /* WARNING: Only wc->wr_cqe and wc->status are reliable */
348 ctxt = container_of(cqe, struct svc_rdma_recv_ctxt, rc_cqe);
349
350 if (wc->status != IB_WC_SUCCESS)
351 goto flushed;
352 trace_svcrdma_wc_recv(wc, &ctxt->rc_cid);
353
354 /* If receive posting fails, the connection is about to be
355 * lost anyway. The server will not be able to send a reply
356 * for this RPC, and the client will retransmit this RPC
357 * anyway when it reconnects.
358 *
359 * Therefore we drop the Receive, even if status was SUCCESS
360 * to reduce the likelihood of replayed requests once the
361 * client reconnects.
362 */
363 if (rdma->sc_pending_recvs < rdma->sc_max_requests)
364 if (!svc_rdma_refresh_recvs(rdma, rdma->sc_recv_batch))
365 goto dropped;
366
367 /* All wc fields are now known to be valid */
368 ctxt->rc_byte_len = wc->byte_len;
369
370 spin_lock(&rdma->sc_rq_dto_lock);
371 list_add_tail(&ctxt->rc_list, &rdma->sc_rq_dto_q);
372 /* Note the unlock pairs with the smp_rmb in svc_xprt_ready: */
373 set_bit(XPT_DATA, &rdma->sc_xprt.xpt_flags);
374 spin_unlock(&rdma->sc_rq_dto_lock);
375 if (!test_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags))
376 svc_xprt_enqueue(&rdma->sc_xprt);
377 return;
378
379 flushed:
380 if (wc->status == IB_WC_WR_FLUSH_ERR)
381 trace_svcrdma_wc_recv_flush(wc, &ctxt->rc_cid);
382 else
383 trace_svcrdma_wc_recv_err(wc, &ctxt->rc_cid);
384 dropped:
385 svc_rdma_recv_ctxt_put(rdma, ctxt);
386 svc_rdma_xprt_deferred_close(rdma);
387 }
388
389 /**
390 * svc_rdma_flush_recv_queues - Drain pending Receive work
391 * @rdma: svcxprt_rdma being shut down
392 *
393 * Caller must guarantee that @rdma's Send and Recv Completion
394 * Queues are empty (e.g., via ib_drain_qp()), so that no completion
395 * handlers can still produce work on the queues being drained.
396 */
svc_rdma_flush_recv_queues(struct svcxprt_rdma * rdma)397 void svc_rdma_flush_recv_queues(struct svcxprt_rdma *rdma)
398 {
399 struct svc_rdma_recv_ctxt *ctxt;
400
401 while ((ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_read_complete_q))) {
402 list_del(&ctxt->rc_list);
403 svc_rdma_recv_ctxt_put(rdma, ctxt);
404 }
405 while ((ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_rq_dto_q))) {
406 list_del(&ctxt->rc_list);
407 svc_rdma_recv_ctxt_put(rdma, ctxt);
408 }
409 }
410
svc_rdma_build_arg_xdr(struct svc_rqst * rqstp,struct svc_rdma_recv_ctxt * ctxt)411 static void svc_rdma_build_arg_xdr(struct svc_rqst *rqstp,
412 struct svc_rdma_recv_ctxt *ctxt)
413 {
414 struct xdr_buf *arg = &rqstp->rq_arg;
415
416 arg->head[0].iov_base = ctxt->rc_recv_buf;
417 arg->head[0].iov_len = ctxt->rc_byte_len;
418 arg->tail[0].iov_base = NULL;
419 arg->tail[0].iov_len = 0;
420 arg->page_len = 0;
421 arg->page_base = 0;
422 arg->buflen = ctxt->rc_byte_len;
423 arg->len = ctxt->rc_byte_len;
424 }
425
426 /**
427 * xdr_count_read_segments - Count number of Read segments in Read list
428 * @rctxt: Ingress receive context
429 * @p: Start of an un-decoded Read list
430 *
431 * Before allocating anything, ensure the ingress Read list is safe
432 * to use.
433 *
434 * The segment count is limited to how many segments can fit in the
435 * transport header without overflowing the buffer. That's about 40
436 * Read segments for a 1KB inline threshold.
437 *
438 * Return values:
439 * %true: Read list is valid. @rctxt's xdr_stream is updated to point
440 * to the first byte past the Read list. rc_read_pcl and
441 * rc_call_pcl cl_count fields are set to the number of
442 * Read segments in the list.
443 * %false: Read list is corrupt or exceeds the page budget. @rctxt's
444 * xdr_stream is left in an unknown state.
445 */
xdr_count_read_segments(struct svc_rdma_recv_ctxt * rctxt,__be32 * p)446 static bool xdr_count_read_segments(struct svc_rdma_recv_ctxt *rctxt, __be32 *p)
447 {
448 unsigned int maxlen = rctxt->rc_maxpages << PAGE_SHIFT;
449 unsigned int total_len = 0;
450
451 rctxt->rc_call_pcl.cl_count = 0;
452 rctxt->rc_read_pcl.cl_count = 0;
453 while (xdr_item_is_present(p)) {
454 u32 position, handle, length;
455 u64 offset;
456
457 p = xdr_inline_decode(&rctxt->rc_stream,
458 rpcrdma_readseg_maxsz * sizeof(*p));
459 if (!p)
460 return false;
461
462 xdr_decode_read_segment(p, &position, &handle,
463 &length, &offset);
464 if (length > maxlen)
465 return false;
466 total_len += length;
467 if (PAGE_ALIGN(total_len) > maxlen)
468 return false;
469 if (position) {
470 if (position & 3)
471 return false;
472 ++rctxt->rc_read_pcl.cl_count;
473 } else {
474 ++rctxt->rc_call_pcl.cl_count;
475 }
476
477 p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
478 if (!p)
479 return false;
480 }
481 return true;
482 }
483
484 /* Sanity check the Read list.
485 *
486 * Sanity checks:
487 * - Read list does not overflow Receive buffer.
488 * - Chunk size limited by largest NFS data payload.
489 *
490 * Return values:
491 * %true: Read list is valid. @rctxt's xdr_stream is updated
492 * to point to the first byte past the Read list.
493 * %false: Read list is corrupt. @rctxt's xdr_stream is left
494 * in an unknown state.
495 */
xdr_check_read_list(struct svc_rdma_recv_ctxt * rctxt)496 static bool xdr_check_read_list(struct svc_rdma_recv_ctxt *rctxt)
497 {
498 __be32 *p;
499
500 p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
501 if (!p)
502 return false;
503 if (!xdr_count_read_segments(rctxt, p))
504 return false;
505 if (!pcl_alloc_call(rctxt, p))
506 return false;
507 return pcl_alloc_read(rctxt, p);
508 }
509
xdr_check_write_chunk(struct svc_rdma_recv_ctxt * rctxt)510 static bool xdr_check_write_chunk(struct svc_rdma_recv_ctxt *rctxt)
511 {
512 u32 segcount;
513 __be32 *p;
514
515 if (xdr_stream_decode_u32(&rctxt->rc_stream, &segcount))
516 return false;
517
518 /* Before trusting the segcount value enough to use it in
519 * a computation, perform a simple range check. A zero
520 * segcount describes no remote buffer and is rejected so
521 * downstream consumers never see a degenerate ch_segcount==0
522 * chunk. The upper bound is an arbitrary but sensible limit
523 * (ie, not architectural).
524 */
525 if (segcount == 0 || unlikely(segcount > rctxt->rc_maxpages))
526 return false;
527
528 p = xdr_inline_decode(&rctxt->rc_stream,
529 segcount * rpcrdma_segment_maxsz * sizeof(*p));
530 return p != NULL;
531 }
532
533 /**
534 * xdr_count_write_chunks - Count number of Write chunks in Write list
535 * @rctxt: Received header and decoding state
536 * @p: start of an un-decoded Write list
537 *
538 * Before allocating anything, ensure the ingress Write list is
539 * safe to use.
540 *
541 * Return values:
542 * %true: Write list is valid. @rctxt's xdr_stream is updated
543 * to point to the first byte past the Write list, and
544 * the number of Write chunks is in rc_write_pcl.cl_count.
545 * %false: Write list is corrupt. @rctxt's xdr_stream is left
546 * in an indeterminate state.
547 */
xdr_count_write_chunks(struct svc_rdma_recv_ctxt * rctxt,__be32 * p)548 static bool xdr_count_write_chunks(struct svc_rdma_recv_ctxt *rctxt, __be32 *p)
549 {
550 rctxt->rc_write_pcl.cl_count = 0;
551 while (xdr_item_is_present(p)) {
552 if (!xdr_check_write_chunk(rctxt))
553 return false;
554 ++rctxt->rc_write_pcl.cl_count;
555 p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
556 if (!p)
557 return false;
558 }
559 return true;
560 }
561
562 /* Sanity check the Write list.
563 *
564 * Implementation limits:
565 * - This implementation currently supports only one Write chunk.
566 *
567 * Sanity checks:
568 * - Write list does not overflow Receive buffer.
569 * - Chunk size limited by largest NFS data payload.
570 *
571 * Return values:
572 * %true: Write list is valid. @rctxt's xdr_stream is updated
573 * to point to the first byte past the Write list.
574 * %false: Write list is corrupt. @rctxt's xdr_stream is left
575 * in an unknown state.
576 */
xdr_check_write_list(struct svc_rdma_recv_ctxt * rctxt)577 static bool xdr_check_write_list(struct svc_rdma_recv_ctxt *rctxt)
578 {
579 __be32 *p;
580
581 p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
582 if (!p)
583 return false;
584 if (!xdr_count_write_chunks(rctxt, p))
585 return false;
586 if (!pcl_alloc_write(rctxt, &rctxt->rc_write_pcl, p))
587 return false;
588
589 rctxt->rc_cur_result_payload = pcl_first_chunk(&rctxt->rc_write_pcl);
590 return true;
591 }
592
593 /* Sanity check the Reply chunk.
594 *
595 * Sanity checks:
596 * - Reply chunk does not overflow Receive buffer.
597 * - Chunk size limited by largest NFS data payload.
598 *
599 * Return values:
600 * %true: Reply chunk is valid. @rctxt's xdr_stream is updated
601 * to point to the first byte past the Reply chunk.
602 * %false: Reply chunk is corrupt. @rctxt's xdr_stream is left
603 * in an unknown state.
604 */
xdr_check_reply_chunk(struct svc_rdma_recv_ctxt * rctxt)605 static bool xdr_check_reply_chunk(struct svc_rdma_recv_ctxt *rctxt)
606 {
607 __be32 *p;
608
609 p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
610 if (!p)
611 return false;
612
613 if (!xdr_item_is_present(p))
614 return true;
615 if (!xdr_check_write_chunk(rctxt))
616 return false;
617
618 rctxt->rc_reply_pcl.cl_count = 1;
619 return pcl_alloc_write(rctxt, &rctxt->rc_reply_pcl, p);
620 }
621
622 /* RPC-over-RDMA Version One private extension: Remote Invalidation.
623 * Responder's choice: requester signals it can handle Send With
624 * Invalidate, and responder chooses one R_key to invalidate.
625 *
626 * If there is exactly one distinct R_key in the received transport
627 * header, set rc_inv_rkey to that R_key. Otherwise, set it to zero.
628 */
svc_rdma_get_inv_rkey(struct svcxprt_rdma * rdma,struct svc_rdma_recv_ctxt * ctxt)629 static void svc_rdma_get_inv_rkey(struct svcxprt_rdma *rdma,
630 struct svc_rdma_recv_ctxt *ctxt)
631 {
632 struct svc_rdma_segment *segment;
633 struct svc_rdma_chunk *chunk;
634 u32 inv_rkey;
635
636 ctxt->rc_inv_rkey = 0;
637
638 if (!rdma->sc_snd_w_inv)
639 return;
640
641 inv_rkey = 0;
642 pcl_for_each_chunk(chunk, &ctxt->rc_call_pcl) {
643 pcl_for_each_segment(segment, chunk) {
644 if (inv_rkey == 0)
645 inv_rkey = segment->rs_handle;
646 else if (inv_rkey != segment->rs_handle)
647 return;
648 }
649 }
650 pcl_for_each_chunk(chunk, &ctxt->rc_read_pcl) {
651 pcl_for_each_segment(segment, chunk) {
652 if (inv_rkey == 0)
653 inv_rkey = segment->rs_handle;
654 else if (inv_rkey != segment->rs_handle)
655 return;
656 }
657 }
658 pcl_for_each_chunk(chunk, &ctxt->rc_write_pcl) {
659 pcl_for_each_segment(segment, chunk) {
660 if (inv_rkey == 0)
661 inv_rkey = segment->rs_handle;
662 else if (inv_rkey != segment->rs_handle)
663 return;
664 }
665 }
666 pcl_for_each_chunk(chunk, &ctxt->rc_reply_pcl) {
667 pcl_for_each_segment(segment, chunk) {
668 if (inv_rkey == 0)
669 inv_rkey = segment->rs_handle;
670 else if (inv_rkey != segment->rs_handle)
671 return;
672 }
673 }
674 ctxt->rc_inv_rkey = inv_rkey;
675 }
676
677 /**
678 * svc_rdma_xdr_decode_req - Decode the transport header
679 * @rq_arg: xdr_buf containing ingress RPC/RDMA message
680 * @rctxt: state of decoding
681 *
682 * On entry, xdr->head[0].iov_base points to first byte of the
683 * RPC-over-RDMA transport header.
684 *
685 * On successful exit, head[0] points to first byte past the
686 * RPC-over-RDMA header. For RDMA_MSG, this is the RPC message.
687 *
688 * The length of the RPC-over-RDMA header is returned.
689 *
690 * Assumptions:
691 * - The transport header is entirely contained in the head iovec.
692 */
svc_rdma_xdr_decode_req(struct xdr_buf * rq_arg,struct svc_rdma_recv_ctxt * rctxt)693 static int svc_rdma_xdr_decode_req(struct xdr_buf *rq_arg,
694 struct svc_rdma_recv_ctxt *rctxt)
695 {
696 __be32 *p, *rdma_argp;
697 unsigned int hdr_len;
698
699 rdma_argp = rq_arg->head[0].iov_base;
700 xdr_init_decode(&rctxt->rc_stream, rq_arg, rdma_argp, NULL);
701
702 p = xdr_inline_decode(&rctxt->rc_stream,
703 rpcrdma_fixed_maxsz * sizeof(*p));
704 if (unlikely(!p))
705 goto out_short;
706 p++;
707 if (*p != rpcrdma_version)
708 goto out_version;
709 p += 2;
710 rctxt->rc_msgtype = *p;
711 switch (rctxt->rc_msgtype) {
712 case rdma_msg:
713 break;
714 case rdma_nomsg:
715 break;
716 case rdma_done:
717 goto out_drop;
718 case rdma_error:
719 goto out_drop;
720 default:
721 goto out_proc;
722 }
723
724 if (!xdr_check_read_list(rctxt))
725 goto out_inval;
726 if (!xdr_check_write_list(rctxt))
727 goto out_inval;
728 if (!xdr_check_reply_chunk(rctxt))
729 goto out_inval;
730
731 rq_arg->head[0].iov_base = rctxt->rc_stream.p;
732 hdr_len = xdr_stream_pos(&rctxt->rc_stream);
733 if (!pcl_check_read_chunk_positions(rctxt,
734 rq_arg->head[0].iov_len - hdr_len))
735 goto out_inval;
736 rq_arg->head[0].iov_len -= hdr_len;
737 rq_arg->len -= hdr_len;
738 trace_svcrdma_decode_rqst(rctxt, rdma_argp, hdr_len);
739 return hdr_len;
740
741 out_short:
742 trace_svcrdma_decode_short_err(rctxt, rq_arg->len);
743 return -EINVAL;
744
745 out_version:
746 trace_svcrdma_decode_badvers_err(rctxt, rdma_argp);
747 return -EPROTONOSUPPORT;
748
749 out_drop:
750 trace_svcrdma_decode_drop_err(rctxt, rdma_argp);
751 return 0;
752
753 out_proc:
754 trace_svcrdma_decode_badproc_err(rctxt, rdma_argp);
755 return -EINVAL;
756
757 out_inval:
758 trace_svcrdma_decode_parse_err(rctxt, rdma_argp);
759 return -EINVAL;
760 }
761
svc_rdma_send_error(struct svcxprt_rdma * rdma,struct svc_rdma_recv_ctxt * rctxt,int status)762 static void svc_rdma_send_error(struct svcxprt_rdma *rdma,
763 struct svc_rdma_recv_ctxt *rctxt,
764 int status)
765 {
766 struct svc_rdma_send_ctxt *sctxt;
767
768 sctxt = svc_rdma_send_ctxt_get(rdma);
769 if (!sctxt)
770 return;
771 svc_rdma_send_error_msg(rdma, sctxt, rctxt, status);
772 }
773
774 /* By convention, backchannel calls arrive via rdma_msg type
775 * messages, and never populate the chunk lists. This makes
776 * the RPC/RDMA header small and fixed in size, so it is
777 * straightforward to check the RPC header's direction field.
778 */
svc_rdma_is_reverse_direction_reply(struct svc_xprt * xprt,struct svc_rdma_recv_ctxt * rctxt)779 static bool svc_rdma_is_reverse_direction_reply(struct svc_xprt *xprt,
780 struct svc_rdma_recv_ctxt *rctxt)
781 {
782 __be32 *p = rctxt->rc_recv_buf;
783
784 if (!xprt->xpt_bc_xprt)
785 return false;
786
787 if (rctxt->rc_msgtype != rdma_msg)
788 return false;
789
790 if (!pcl_is_empty(&rctxt->rc_call_pcl))
791 return false;
792 if (!pcl_is_empty(&rctxt->rc_read_pcl))
793 return false;
794 if (!pcl_is_empty(&rctxt->rc_write_pcl))
795 return false;
796 if (!pcl_is_empty(&rctxt->rc_reply_pcl))
797 return false;
798
799 /* RPC call direction */
800 if (*(p + 8) == cpu_to_be32(RPC_CALL))
801 return false;
802
803 return true;
804 }
805
806 /* Finish constructing the RPC Call message in rqstp::rq_arg.
807 *
808 * The incoming RPC/RDMA message is an RDMA_MSG type message
809 * with a single Read chunk (only the upper layer data payload
810 * was conveyed via RDMA Read).
811 */
svc_rdma_read_complete_one(struct svc_rqst * rqstp,struct svc_rdma_recv_ctxt * ctxt)812 static void svc_rdma_read_complete_one(struct svc_rqst *rqstp,
813 struct svc_rdma_recv_ctxt *ctxt)
814 {
815 struct svc_rdma_chunk *chunk = pcl_first_chunk(&ctxt->rc_read_pcl);
816 struct xdr_buf *buf = &rqstp->rq_arg;
817 unsigned int length;
818
819 /* Split the Receive buffer between the head and tail
820 * buffers at Read chunk's position. XDR roundup of the
821 * chunk is not included in either the pagelist or in
822 * the tail.
823 */
824 buf->tail[0].iov_base = buf->head[0].iov_base + chunk->ch_position;
825 buf->tail[0].iov_len = buf->head[0].iov_len - chunk->ch_position;
826 buf->head[0].iov_len = chunk->ch_position;
827
828 /* Read chunk may need XDR roundup (see RFC 8166, s. 3.4.5.2).
829 *
830 * If the client already rounded up the chunk length, the
831 * length does not change. Otherwise, the length of the page
832 * list is increased to include XDR round-up.
833 *
834 * Currently these chunks always start at page offset 0,
835 * thus the rounded-up length never crosses a page boundary.
836 */
837 buf->pages = &rqstp->rq_pages[0];
838 length = xdr_align_size(chunk->ch_length);
839 buf->page_len = length;
840 buf->len += length;
841 buf->buflen += length;
842 }
843
844 /* Finish constructing the RPC Call message in rqstp::rq_arg.
845 *
846 * The incoming RPC/RDMA message is an RDMA_MSG type message
847 * with payload in multiple Read chunks and no PZRC.
848 */
svc_rdma_read_complete_multiple(struct svc_rqst * rqstp,struct svc_rdma_recv_ctxt * ctxt)849 static void svc_rdma_read_complete_multiple(struct svc_rqst *rqstp,
850 struct svc_rdma_recv_ctxt *ctxt)
851 {
852 struct xdr_buf *buf = &rqstp->rq_arg;
853
854 buf->len += ctxt->rc_readbytes;
855 buf->buflen += ctxt->rc_readbytes;
856
857 buf->head[0].iov_base = page_address(rqstp->rq_pages[0]);
858 buf->head[0].iov_len = min_t(size_t, PAGE_SIZE, ctxt->rc_readbytes);
859 buf->pages = &rqstp->rq_pages[1];
860 buf->page_len = ctxt->rc_readbytes - buf->head[0].iov_len;
861 }
862
863 /* Finish constructing the RPC Call message in rqstp::rq_arg.
864 *
865 * The incoming RPC/RDMA message is an RDMA_NOMSG type message
866 * (the RPC message body was conveyed via RDMA Read).
867 */
svc_rdma_read_complete_pzrc(struct svc_rqst * rqstp,struct svc_rdma_recv_ctxt * ctxt)868 static void svc_rdma_read_complete_pzrc(struct svc_rqst *rqstp,
869 struct svc_rdma_recv_ctxt *ctxt)
870 {
871 struct xdr_buf *buf = &rqstp->rq_arg;
872
873 buf->len += ctxt->rc_readbytes;
874 buf->buflen += ctxt->rc_readbytes;
875
876 buf->head[0].iov_base = page_address(rqstp->rq_pages[0]);
877 buf->head[0].iov_len = min_t(size_t, PAGE_SIZE, ctxt->rc_readbytes);
878 buf->pages = &rqstp->rq_pages[1];
879 buf->page_len = ctxt->rc_readbytes - buf->head[0].iov_len;
880 }
881
svc_rdma_read_complete(struct svc_rqst * rqstp,struct svc_rdma_recv_ctxt * ctxt)882 static noinline void svc_rdma_read_complete(struct svc_rqst *rqstp,
883 struct svc_rdma_recv_ctxt *ctxt)
884 {
885 unsigned int i;
886
887 /* Transfer the Read chunk pages into @rqstp.rq_pages, replacing
888 * the receive buffer pages already allocated for this rqstp.
889 */
890 release_pages(rqstp->rq_pages, ctxt->rc_page_count);
891 for (i = 0; i < ctxt->rc_page_count; i++)
892 rqstp->rq_pages[i] = ctxt->rc_pages[i];
893
894 /* Prevent svc_rdma_recv_ctxt_put() from releasing the
895 * pages in ctxt::rc_pages a second time.
896 */
897 ctxt->rc_page_count = 0;
898
899 /* Finish constructing the RPC Call message. The exact
900 * procedure for that depends on what kind of RPC/RDMA
901 * chunks were provided by the client.
902 */
903 rqstp->rq_arg = ctxt->rc_saved_arg;
904 if (pcl_is_empty(&ctxt->rc_call_pcl)) {
905 if (ctxt->rc_read_pcl.cl_count == 1)
906 svc_rdma_read_complete_one(rqstp, ctxt);
907 else
908 svc_rdma_read_complete_multiple(rqstp, ctxt);
909 } else {
910 svc_rdma_read_complete_pzrc(rqstp, ctxt);
911 }
912
913 trace_svcrdma_read_finished(&ctxt->rc_cid);
914 }
915
916 /**
917 * svc_rdma_recvfrom - Receive an RPC call
918 * @rqstp: request structure into which to receive an RPC Call
919 *
920 * Returns:
921 * The positive number of bytes in the RPC Call message,
922 * %0 if there were no Calls ready to return,
923 * %-EINVAL if the Read chunk data is too large,
924 * %-ENOMEM if rdma_rw context pool was exhausted,
925 * %-ENOTCONN if posting failed (connection is lost),
926 * %-EIO if rdma_rw initialization failed (DMA mapping, etc).
927 *
928 * Called in a loop when XPT_DATA is set. XPT_DATA is cleared only
929 * when there are no remaining ctxt's to process.
930 *
931 * The next ctxt is removed from the "receive" lists.
932 *
933 * - If the ctxt completes a Receive, then construct the Call
934 * message from the contents of the Receive buffer.
935 *
936 * - If there are no Read chunks in this message, then finish
937 * assembling the Call message and return the number of bytes
938 * in the message.
939 *
940 * - If there are Read chunks in this message, post Read WRs to
941 * pull that payload. When the Read WRs complete, build the
942 * full message and return the number of bytes in it.
943 */
svc_rdma_recvfrom(struct svc_rqst * rqstp)944 int svc_rdma_recvfrom(struct svc_rqst *rqstp)
945 {
946 struct svc_xprt *xprt = rqstp->rq_xprt;
947 struct svcxprt_rdma *rdma_xprt =
948 container_of(xprt, struct svcxprt_rdma, sc_xprt);
949 struct svc_rdma_recv_ctxt *ctxt;
950 int ret;
951
952 /* Precaution: a zero page count on error return causes
953 * svc_rqst_release_pages() to release nothing.
954 */
955 rqstp->rq_next_page = rqstp->rq_respages;
956
957 rqstp->rq_xprt_ctxt = NULL;
958
959 spin_lock(&rdma_xprt->sc_rq_dto_lock);
960 ctxt = svc_rdma_next_recv_ctxt(&rdma_xprt->sc_read_complete_q);
961 if (ctxt) {
962 list_del(&ctxt->rc_list);
963 spin_unlock(&rdma_xprt->sc_rq_dto_lock);
964 svc_xprt_received(xprt);
965 svc_rdma_read_complete(rqstp, ctxt);
966 goto complete;
967 }
968 ctxt = svc_rdma_next_recv_ctxt(&rdma_xprt->sc_rq_dto_q);
969 if (ctxt)
970 list_del(&ctxt->rc_list);
971 else
972 /* No new incoming requests, terminate the loop */
973 clear_bit(XPT_DATA, &xprt->xpt_flags);
974 spin_unlock(&rdma_xprt->sc_rq_dto_lock);
975
976 /* Unblock the transport for the next receive */
977 svc_xprt_received(xprt);
978 if (!ctxt)
979 return 0;
980
981 percpu_counter_inc(&svcrdma_stat_recv);
982 ib_dma_sync_single_for_cpu(rdma_xprt->sc_cm_id->device,
983 ctxt->rc_recv_sge.addr, ctxt->rc_byte_len,
984 DMA_FROM_DEVICE);
985 svc_rdma_build_arg_xdr(rqstp, ctxt);
986
987 ret = svc_rdma_xdr_decode_req(&rqstp->rq_arg, ctxt);
988 if (ret < 0)
989 goto out_err;
990 if (ret == 0)
991 goto out_drop;
992
993 if (svc_rdma_is_reverse_direction_reply(xprt, ctxt))
994 goto out_backchannel;
995
996 svc_rdma_get_inv_rkey(rdma_xprt, ctxt);
997
998 if (!pcl_is_empty(&ctxt->rc_read_pcl) ||
999 !pcl_is_empty(&ctxt->rc_call_pcl))
1000 goto out_readlist;
1001
1002 complete:
1003 rqstp->rq_xprt_ctxt = ctxt;
1004 rqstp->rq_prot = IPPROTO_MAX;
1005 svc_xprt_copy_addrs(rqstp, xprt);
1006 set_bit(RQ_SECURE, &rqstp->rq_flags);
1007 return rqstp->rq_arg.len;
1008
1009 out_err:
1010 svc_rdma_send_error(rdma_xprt, ctxt, ret);
1011 svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
1012 return 0;
1013
1014 out_readlist:
1015 /* This @rqstp is about to be recycled. Save the work
1016 * already done constructing the Call message in rq_arg
1017 * so it can be restored when the RDMA Reads have
1018 * completed.
1019 */
1020 ctxt->rc_saved_arg = rqstp->rq_arg;
1021
1022 ret = svc_rdma_process_read_list(rdma_xprt, rqstp, ctxt);
1023 if (ret < 0) {
1024 if (ret == -EINVAL)
1025 svc_rdma_send_error(rdma_xprt, ctxt, ret);
1026 svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
1027 svc_rdma_xprt_deferred_close(rdma_xprt);
1028 return ret;
1029 }
1030 return 0;
1031
1032 out_backchannel:
1033 svc_rdma_handle_bc_reply(rqstp, ctxt);
1034 out_drop:
1035 svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
1036 return 0;
1037 }
1038