Merge branch 'for-4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[sfrench/cifs-2.6.git] / net / sunrpc / xprtrdma / svc_rdma_backchannel.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2015-2018 Oracle.  All rights reserved.
4  *
5  * Support for backward direction RPCs on RPC/RDMA (server-side).
6  */
7
8 #include <linux/module.h>
9
10 #include <linux/sunrpc/svc_rdma.h>
11
12 #include "xprt_rdma.h"
13 #include <trace/events/rpcrdma.h>
14
15 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
16
17 #undef SVCRDMA_BACKCHANNEL_DEBUG
18
19 /**
20  * svc_rdma_handle_bc_reply - Process incoming backchannel reply
21  * @xprt: controlling backchannel transport
22  * @rdma_resp: pointer to incoming transport header
23  * @rcvbuf: XDR buffer into which to decode the reply
24  *
25  * Returns:
26  *      %0 if @rcvbuf is filled in, xprt_complete_rqst called,
27  *      %-EAGAIN if server should call ->recvfrom again.
28  */
29 int svc_rdma_handle_bc_reply(struct rpc_xprt *xprt, __be32 *rdma_resp,
30                              struct xdr_buf *rcvbuf)
31 {
32         struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
33         struct kvec *dst, *src = &rcvbuf->head[0];
34         struct rpc_rqst *req;
35         unsigned long cwnd;
36         u32 credits;
37         size_t len;
38         __be32 xid;
39         __be32 *p;
40         int ret;
41
42         p = (__be32 *)src->iov_base;
43         len = src->iov_len;
44         xid = *rdma_resp;
45
46 #ifdef SVCRDMA_BACKCHANNEL_DEBUG
47         pr_info("%s: xid=%08x, length=%zu\n",
48                 __func__, be32_to_cpu(xid), len);
49         pr_info("%s: RPC/RDMA: %*ph\n",
50                 __func__, (int)RPCRDMA_HDRLEN_MIN, rdma_resp);
51         pr_info("%s:      RPC: %*ph\n",
52                 __func__, (int)len, p);
53 #endif
54
55         ret = -EAGAIN;
56         if (src->iov_len < 24)
57                 goto out_shortreply;
58
59         spin_lock(&xprt->recv_lock);
60         req = xprt_lookup_rqst(xprt, xid);
61         if (!req)
62                 goto out_notfound;
63
64         dst = &req->rq_private_buf.head[0];
65         memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
66         if (dst->iov_len < len)
67                 goto out_unlock;
68         memcpy(dst->iov_base, p, len);
69
70         credits = be32_to_cpup(rdma_resp + 2);
71         if (credits == 0)
72                 credits = 1;    /* don't deadlock */
73         else if (credits > r_xprt->rx_buf.rb_bc_max_requests)
74                 credits = r_xprt->rx_buf.rb_bc_max_requests;
75
76         spin_lock_bh(&xprt->transport_lock);
77         cwnd = xprt->cwnd;
78         xprt->cwnd = credits << RPC_CWNDSHIFT;
79         if (xprt->cwnd > cwnd)
80                 xprt_release_rqst_cong(req->rq_task);
81         spin_unlock_bh(&xprt->transport_lock);
82
83
84         ret = 0;
85         xprt_complete_rqst(req->rq_task, rcvbuf->len);
86         rcvbuf->len = 0;
87
88 out_unlock:
89         spin_unlock(&xprt->recv_lock);
90 out:
91         return ret;
92
93 out_shortreply:
94         dprintk("svcrdma: short bc reply: xprt=%p, len=%zu\n",
95                 xprt, src->iov_len);
96         goto out;
97
98 out_notfound:
99         dprintk("svcrdma: unrecognized bc reply: xprt=%p, xid=%08x\n",
100                 xprt, be32_to_cpu(xid));
101         goto out_unlock;
102 }
103
104 /* Send a backwards direction RPC call.
105  *
106  * Caller holds the connection's mutex and has already marshaled
107  * the RPC/RDMA request.
108  *
109  * This is similar to svc_rdma_send_reply_msg, but takes a struct
110  * rpc_rqst instead, does not support chunks, and avoids blocking
111  * memory allocation.
112  *
113  * XXX: There is still an opportunity to block in svc_rdma_send()
114  * if there are no SQ entries to post the Send. This may occur if
115  * the adapter has a small maximum SQ depth.
116  */
117 static int svc_rdma_bc_sendto(struct svcxprt_rdma *rdma,
118                               struct rpc_rqst *rqst,
119                               struct svc_rdma_send_ctxt *ctxt)
120 {
121         int ret;
122
123         ret = svc_rdma_map_reply_msg(rdma, ctxt, &rqst->rq_snd_buf, NULL);
124         if (ret < 0)
125                 return -EIO;
126
127         /* Bump page refcnt so Send completion doesn't release
128          * the rq_buffer before all retransmits are complete.
129          */
130         get_page(virt_to_page(rqst->rq_buffer));
131         ctxt->sc_send_wr.opcode = IB_WR_SEND;
132         return svc_rdma_send(rdma, &ctxt->sc_send_wr);
133 }
134
135 /* Server-side transport endpoint wants a whole page for its send
136  * buffer. The client RPC code constructs the RPC header in this
137  * buffer before it invokes ->send_request.
138  */
139 static int
140 xprt_rdma_bc_allocate(struct rpc_task *task)
141 {
142         struct rpc_rqst *rqst = task->tk_rqstp;
143         size_t size = rqst->rq_callsize;
144         struct page *page;
145
146         if (size > PAGE_SIZE) {
147                 WARN_ONCE(1, "svcrdma: large bc buffer request (size %zu)\n",
148                           size);
149                 return -EINVAL;
150         }
151
152         page = alloc_page(RPCRDMA_DEF_GFP);
153         if (!page)
154                 return -ENOMEM;
155         rqst->rq_buffer = page_address(page);
156
157         rqst->rq_rbuffer = kmalloc(rqst->rq_rcvsize, RPCRDMA_DEF_GFP);
158         if (!rqst->rq_rbuffer) {
159                 put_page(page);
160                 return -ENOMEM;
161         }
162         return 0;
163 }
164
165 static void
166 xprt_rdma_bc_free(struct rpc_task *task)
167 {
168         struct rpc_rqst *rqst = task->tk_rqstp;
169
170         put_page(virt_to_page(rqst->rq_buffer));
171         kfree(rqst->rq_rbuffer);
172 }
173
174 static int
175 rpcrdma_bc_send_request(struct svcxprt_rdma *rdma, struct rpc_rqst *rqst)
176 {
177         struct rpc_xprt *xprt = rqst->rq_xprt;
178         struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
179         struct svc_rdma_send_ctxt *ctxt;
180         __be32 *p;
181         int rc;
182
183         ctxt = svc_rdma_send_ctxt_get(rdma);
184         if (!ctxt)
185                 goto drop_connection;
186
187         p = ctxt->sc_xprt_buf;
188         *p++ = rqst->rq_xid;
189         *p++ = rpcrdma_version;
190         *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_max_requests);
191         *p++ = rdma_msg;
192         *p++ = xdr_zero;
193         *p++ = xdr_zero;
194         *p   = xdr_zero;
195         svc_rdma_sync_reply_hdr(rdma, ctxt, RPCRDMA_HDRLEN_MIN);
196
197 #ifdef SVCRDMA_BACKCHANNEL_DEBUG
198         pr_info("%s: %*ph\n", __func__, 64, rqst->rq_buffer);
199 #endif
200
201         rc = svc_rdma_bc_sendto(rdma, rqst, ctxt);
202         if (rc) {
203                 svc_rdma_send_ctxt_put(rdma, ctxt);
204                 goto drop_connection;
205         }
206         return rc;
207
208 drop_connection:
209         dprintk("svcrdma: failed to send bc call\n");
210         xprt_disconnect_done(xprt);
211         return -ENOTCONN;
212 }
213
214 /* Send an RPC call on the passive end of a transport
215  * connection.
216  */
217 static int
218 xprt_rdma_bc_send_request(struct rpc_task *task)
219 {
220         struct rpc_rqst *rqst = task->tk_rqstp;
221         struct svc_xprt *sxprt = rqst->rq_xprt->bc_xprt;
222         struct svcxprt_rdma *rdma;
223         int ret;
224
225         dprintk("svcrdma: sending bc call with xid: %08x\n",
226                 be32_to_cpu(rqst->rq_xid));
227
228         if (!mutex_trylock(&sxprt->xpt_mutex)) {
229                 rpc_sleep_on(&sxprt->xpt_bc_pending, task, NULL);
230                 if (!mutex_trylock(&sxprt->xpt_mutex))
231                         return -EAGAIN;
232                 rpc_wake_up_queued_task(&sxprt->xpt_bc_pending, task);
233         }
234
235         ret = -ENOTCONN;
236         rdma = container_of(sxprt, struct svcxprt_rdma, sc_xprt);
237         if (!test_bit(XPT_DEAD, &sxprt->xpt_flags))
238                 ret = rpcrdma_bc_send_request(rdma, rqst);
239
240         mutex_unlock(&sxprt->xpt_mutex);
241
242         if (ret < 0)
243                 return ret;
244         return 0;
245 }
246
247 static void
248 xprt_rdma_bc_close(struct rpc_xprt *xprt)
249 {
250         dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
251 }
252
253 static void
254 xprt_rdma_bc_put(struct rpc_xprt *xprt)
255 {
256         dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
257
258         xprt_free(xprt);
259         module_put(THIS_MODULE);
260 }
261
262 static const struct rpc_xprt_ops xprt_rdma_bc_procs = {
263         .reserve_xprt           = xprt_reserve_xprt_cong,
264         .release_xprt           = xprt_release_xprt_cong,
265         .alloc_slot             = xprt_alloc_slot,
266         .free_slot              = xprt_free_slot,
267         .release_request        = xprt_release_rqst_cong,
268         .buf_alloc              = xprt_rdma_bc_allocate,
269         .buf_free               = xprt_rdma_bc_free,
270         .send_request           = xprt_rdma_bc_send_request,
271         .set_retrans_timeout    = xprt_set_retrans_timeout_def,
272         .close                  = xprt_rdma_bc_close,
273         .destroy                = xprt_rdma_bc_put,
274         .print_stats            = xprt_rdma_print_stats
275 };
276
277 static const struct rpc_timeout xprt_rdma_bc_timeout = {
278         .to_initval = 60 * HZ,
279         .to_maxval = 60 * HZ,
280 };
281
282 /* It shouldn't matter if the number of backchannel session slots
283  * doesn't match the number of RPC/RDMA credits. That just means
284  * one or the other will have extra slots that aren't used.
285  */
286 static struct rpc_xprt *
287 xprt_setup_rdma_bc(struct xprt_create *args)
288 {
289         struct rpc_xprt *xprt;
290         struct rpcrdma_xprt *new_xprt;
291
292         if (args->addrlen > sizeof(xprt->addr)) {
293                 dprintk("RPC:       %s: address too large\n", __func__);
294                 return ERR_PTR(-EBADF);
295         }
296
297         xprt = xprt_alloc(args->net, sizeof(*new_xprt),
298                           RPCRDMA_MAX_BC_REQUESTS,
299                           RPCRDMA_MAX_BC_REQUESTS);
300         if (!xprt) {
301                 dprintk("RPC:       %s: couldn't allocate rpc_xprt\n",
302                         __func__);
303                 return ERR_PTR(-ENOMEM);
304         }
305
306         xprt->timeout = &xprt_rdma_bc_timeout;
307         xprt_set_bound(xprt);
308         xprt_set_connected(xprt);
309         xprt->bind_timeout = RPCRDMA_BIND_TO;
310         xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
311         xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
312
313         xprt->prot = XPRT_TRANSPORT_BC_RDMA;
314         xprt->tsh_size = 0;
315         xprt->ops = &xprt_rdma_bc_procs;
316
317         memcpy(&xprt->addr, args->dstaddr, args->addrlen);
318         xprt->addrlen = args->addrlen;
319         xprt_rdma_format_addresses(xprt, (struct sockaddr *)&xprt->addr);
320         xprt->resvport = 0;
321
322         xprt->max_payload = xprt_rdma_max_inline_read;
323
324         new_xprt = rpcx_to_rdmax(xprt);
325         new_xprt->rx_buf.rb_bc_max_requests = xprt->max_reqs;
326
327         xprt_get(xprt);
328         args->bc_xprt->xpt_bc_xprt = xprt;
329         xprt->bc_xprt = args->bc_xprt;
330
331         if (!try_module_get(THIS_MODULE))
332                 goto out_fail;
333
334         /* Final put for backchannel xprt is in __svc_rdma_free */
335         xprt_get(xprt);
336         return xprt;
337
338 out_fail:
339         xprt_rdma_free_addresses(xprt);
340         args->bc_xprt->xpt_bc_xprt = NULL;
341         args->bc_xprt->xpt_bc_xps = NULL;
342         xprt_put(xprt);
343         xprt_free(xprt);
344         return ERR_PTR(-EINVAL);
345 }
346
347 struct xprt_class xprt_rdma_bc = {
348         .list                   = LIST_HEAD_INIT(xprt_rdma_bc.list),
349         .name                   = "rdma backchannel",
350         .owner                  = THIS_MODULE,
351         .ident                  = XPRT_TRANSPORT_BC_RDMA,
352         .setup                  = xprt_setup_rdma_bc,
353 };