GNU Linux-libre 4.19.264-gnu1
[releases.git] / net / sunrpc / backchannel_rqst.c
1 /******************************************************************************
2
3 (c) 2007 Network Appliance, Inc.  All Rights Reserved.
4 (c) 2009 NetApp.  All Rights Reserved.
5
6 NetApp provides this source code under the GPL v2 License.
7 The GPL v2 license is available at
8 http://opensource.org/licenses/gpl-license.php.
9
10 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
11 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
13 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
14 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
15 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
17 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
18 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21
22 ******************************************************************************/
23
24 #include <linux/tcp.h>
25 #include <linux/slab.h>
26 #include <linux/sunrpc/xprt.h>
27 #include <linux/export.h>
28 #include <linux/sunrpc/bc_xprt.h>
29
30 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
31 #define RPCDBG_FACILITY RPCDBG_TRANS
32 #endif
33
34 /*
35  * Helper routines that track the number of preallocation elements
36  * on the transport.
37  */
38 static inline int xprt_need_to_requeue(struct rpc_xprt *xprt)
39 {
40         return xprt->bc_alloc_count < atomic_read(&xprt->bc_free_slots);
41 }
42
43 static inline void xprt_inc_alloc_count(struct rpc_xprt *xprt, unsigned int n)
44 {
45         atomic_add(n, &xprt->bc_free_slots);
46         xprt->bc_alloc_count += n;
47 }
48
49 static inline int xprt_dec_alloc_count(struct rpc_xprt *xprt, unsigned int n)
50 {
51         atomic_sub(n, &xprt->bc_free_slots);
52         return xprt->bc_alloc_count -= n;
53 }
54
55 /*
56  * Free the preallocated rpc_rqst structure and the memory
57  * buffers hanging off of it.
58  */
59 static void xprt_free_allocation(struct rpc_rqst *req)
60 {
61         struct xdr_buf *xbufp;
62
63         dprintk("RPC:        free allocations for req= %p\n", req);
64         WARN_ON_ONCE(test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state));
65         xbufp = &req->rq_rcv_buf;
66         free_page((unsigned long)xbufp->head[0].iov_base);
67         xbufp = &req->rq_snd_buf;
68         free_page((unsigned long)xbufp->head[0].iov_base);
69         kfree(req);
70 }
71
72 static void xprt_bc_reinit_xdr_buf(struct xdr_buf *buf)
73 {
74         buf->head[0].iov_len = PAGE_SIZE;
75         buf->tail[0].iov_len = 0;
76         buf->pages = NULL;
77         buf->page_len = 0;
78         buf->flags = 0;
79         buf->len = 0;
80         buf->buflen = PAGE_SIZE;
81 }
82
83 static int xprt_alloc_xdr_buf(struct xdr_buf *buf, gfp_t gfp_flags)
84 {
85         struct page *page;
86         /* Preallocate one XDR receive buffer */
87         page = alloc_page(gfp_flags);
88         if (page == NULL)
89                 return -ENOMEM;
90         xdr_buf_init(buf, page_address(page), PAGE_SIZE);
91         return 0;
92 }
93
94 static
95 struct rpc_rqst *xprt_alloc_bc_req(struct rpc_xprt *xprt, gfp_t gfp_flags)
96 {
97         struct rpc_rqst *req;
98
99         /* Pre-allocate one backchannel rpc_rqst */
100         req = kzalloc(sizeof(*req), gfp_flags);
101         if (req == NULL)
102                 return NULL;
103
104         req->rq_xprt = xprt;
105         INIT_LIST_HEAD(&req->rq_list);
106         INIT_LIST_HEAD(&req->rq_bc_list);
107
108         /* Preallocate one XDR receive buffer */
109         if (xprt_alloc_xdr_buf(&req->rq_rcv_buf, gfp_flags) < 0) {
110                 printk(KERN_ERR "Failed to create bc receive xbuf\n");
111                 goto out_free;
112         }
113         req->rq_rcv_buf.len = PAGE_SIZE;
114
115         /* Preallocate one XDR send buffer */
116         if (xprt_alloc_xdr_buf(&req->rq_snd_buf, gfp_flags) < 0) {
117                 printk(KERN_ERR "Failed to create bc snd xbuf\n");
118                 goto out_free;
119         }
120         return req;
121 out_free:
122         xprt_free_allocation(req);
123         return NULL;
124 }
125
126 /*
127  * Preallocate up to min_reqs structures and related buffers for use
128  * by the backchannel.  This function can be called multiple times
129  * when creating new sessions that use the same rpc_xprt.  The
130  * preallocated buffers are added to the pool of resources used by
131  * the rpc_xprt.  Anyone of these resources may be used used by an
132  * incoming callback request.  It's up to the higher levels in the
133  * stack to enforce that the maximum number of session slots is not
134  * being exceeded.
135  *
136  * Some callback arguments can be large.  For example, a pNFS server
137  * using multiple deviceids.  The list can be unbound, but the client
138  * has the ability to tell the server the maximum size of the callback
139  * requests.  Each deviceID is 16 bytes, so allocate one page
140  * for the arguments to have enough room to receive a number of these
141  * deviceIDs.  The NFS client indicates to the pNFS server that its
142  * callback requests can be up to 4096 bytes in size.
143  */
144 int xprt_setup_backchannel(struct rpc_xprt *xprt, unsigned int min_reqs)
145 {
146         if (!xprt->ops->bc_setup)
147                 return 0;
148         return xprt->ops->bc_setup(xprt, min_reqs);
149 }
150 EXPORT_SYMBOL_GPL(xprt_setup_backchannel);
151
152 int xprt_setup_bc(struct rpc_xprt *xprt, unsigned int min_reqs)
153 {
154         struct rpc_rqst *req;
155         struct list_head tmp_list;
156         int i;
157
158         dprintk("RPC:       setup backchannel transport\n");
159
160         /*
161          * We use a temporary list to keep track of the preallocated
162          * buffers.  Once we're done building the list we splice it
163          * into the backchannel preallocation list off of the rpc_xprt
164          * struct.  This helps minimize the amount of time the list
165          * lock is held on the rpc_xprt struct.  It also makes cleanup
166          * easier in case of memory allocation errors.
167          */
168         INIT_LIST_HEAD(&tmp_list);
169         for (i = 0; i < min_reqs; i++) {
170                 /* Pre-allocate one backchannel rpc_rqst */
171                 req = xprt_alloc_bc_req(xprt, GFP_KERNEL);
172                 if (req == NULL) {
173                         printk(KERN_ERR "Failed to create bc rpc_rqst\n");
174                         goto out_free;
175                 }
176
177                 /* Add the allocated buffer to the tmp list */
178                 dprintk("RPC:       adding req= %p\n", req);
179                 list_add(&req->rq_bc_pa_list, &tmp_list);
180         }
181
182         /*
183          * Add the temporary list to the backchannel preallocation list
184          */
185         spin_lock(&xprt->bc_pa_lock);
186         list_splice(&tmp_list, &xprt->bc_pa_list);
187         xprt_inc_alloc_count(xprt, min_reqs);
188         spin_unlock(&xprt->bc_pa_lock);
189
190         dprintk("RPC:       setup backchannel transport done\n");
191         return 0;
192
193 out_free:
194         /*
195          * Memory allocation failed, free the temporary list
196          */
197         while (!list_empty(&tmp_list)) {
198                 req = list_first_entry(&tmp_list,
199                                 struct rpc_rqst,
200                                 rq_bc_pa_list);
201                 list_del(&req->rq_bc_pa_list);
202                 xprt_free_allocation(req);
203         }
204
205         dprintk("RPC:       setup backchannel transport failed\n");
206         return -ENOMEM;
207 }
208
209 /**
210  * xprt_destroy_backchannel - Destroys the backchannel preallocated structures.
211  * @xprt:       the transport holding the preallocated strucures
212  * @max_reqs    the maximum number of preallocated structures to destroy
213  *
214  * Since these structures may have been allocated by multiple calls
215  * to xprt_setup_backchannel, we only destroy up to the maximum number
216  * of reqs specified by the caller.
217  */
218 void xprt_destroy_backchannel(struct rpc_xprt *xprt, unsigned int max_reqs)
219 {
220         if (xprt->ops->bc_destroy)
221                 xprt->ops->bc_destroy(xprt, max_reqs);
222 }
223 EXPORT_SYMBOL_GPL(xprt_destroy_backchannel);
224
225 void xprt_destroy_bc(struct rpc_xprt *xprt, unsigned int max_reqs)
226 {
227         struct rpc_rqst *req = NULL, *tmp = NULL;
228
229         dprintk("RPC:        destroy backchannel transport\n");
230
231         if (max_reqs == 0)
232                 goto out;
233
234         spin_lock_bh(&xprt->bc_pa_lock);
235         xprt_dec_alloc_count(xprt, max_reqs);
236         list_for_each_entry_safe(req, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
237                 dprintk("RPC:        req=%p\n", req);
238                 list_del(&req->rq_bc_pa_list);
239                 xprt_free_allocation(req);
240                 if (--max_reqs == 0)
241                         break;
242         }
243         spin_unlock_bh(&xprt->bc_pa_lock);
244
245 out:
246         dprintk("RPC:        backchannel list empty= %s\n",
247                 list_empty(&xprt->bc_pa_list) ? "true" : "false");
248 }
249
250 static struct rpc_rqst *xprt_alloc_bc_request(struct rpc_xprt *xprt, __be32 xid)
251 {
252         struct rpc_rqst *req = NULL;
253
254         dprintk("RPC:       allocate a backchannel request\n");
255         if (atomic_read(&xprt->bc_free_slots) <= 0)
256                 goto not_found;
257         if (list_empty(&xprt->bc_pa_list)) {
258                 req = xprt_alloc_bc_req(xprt, GFP_ATOMIC);
259                 if (!req)
260                         goto not_found;
261                 list_add_tail(&req->rq_bc_pa_list, &xprt->bc_pa_list);
262                 xprt->bc_alloc_count++;
263         }
264         req = list_first_entry(&xprt->bc_pa_list, struct rpc_rqst,
265                                 rq_bc_pa_list);
266         req->rq_reply_bytes_recvd = 0;
267         req->rq_bytes_sent = 0;
268         memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
269                         sizeof(req->rq_private_buf));
270         req->rq_xid = xid;
271         req->rq_connect_cookie = xprt->connect_cookie;
272 not_found:
273         dprintk("RPC:       backchannel req=%p\n", req);
274         return req;
275 }
276
277 /*
278  * Return the preallocated rpc_rqst structure and XDR buffers
279  * associated with this rpc_task.
280  */
281 void xprt_free_bc_request(struct rpc_rqst *req)
282 {
283         struct rpc_xprt *xprt = req->rq_xprt;
284
285         xprt->ops->bc_free_rqst(req);
286 }
287
288 void xprt_free_bc_rqst(struct rpc_rqst *req)
289 {
290         struct rpc_xprt *xprt = req->rq_xprt;
291
292         dprintk("RPC:       free backchannel req=%p\n", req);
293
294         req->rq_connect_cookie = xprt->connect_cookie - 1;
295         smp_mb__before_atomic();
296         clear_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
297         smp_mb__after_atomic();
298
299         /*
300          * Return it to the list of preallocations so that it
301          * may be reused by a new callback request.
302          */
303         spin_lock_bh(&xprt->bc_pa_lock);
304         if (xprt_need_to_requeue(xprt)) {
305                 xprt_bc_reinit_xdr_buf(&req->rq_snd_buf);
306                 xprt_bc_reinit_xdr_buf(&req->rq_rcv_buf);
307                 req->rq_rcv_buf.len = PAGE_SIZE;
308                 list_add_tail(&req->rq_bc_pa_list, &xprt->bc_pa_list);
309                 xprt->bc_alloc_count++;
310                 req = NULL;
311         }
312         spin_unlock_bh(&xprt->bc_pa_lock);
313         if (req != NULL) {
314                 /*
315                  * The last remaining session was destroyed while this
316                  * entry was in use.  Free the entry and don't attempt
317                  * to add back to the list because there is no need to
318                  * have anymore preallocated entries.
319                  */
320                 dprintk("RPC:       Last session removed req=%p\n", req);
321                 xprt_free_allocation(req);
322                 return;
323         }
324 }
325
326 /*
327  * One or more rpc_rqst structure have been preallocated during the
328  * backchannel setup.  Buffer space for the send and private XDR buffers
329  * has been preallocated as well.  Use xprt_alloc_bc_request to allocate
330  * to this request.  Use xprt_free_bc_request to return it.
331  *
332  * We know that we're called in soft interrupt context, grab the spin_lock
333  * since there is no need to grab the bottom half spin_lock.
334  *
335  * Return an available rpc_rqst, otherwise NULL if non are available.
336  */
337 struct rpc_rqst *xprt_lookup_bc_request(struct rpc_xprt *xprt, __be32 xid)
338 {
339         struct rpc_rqst *req;
340
341         spin_lock(&xprt->bc_pa_lock);
342         list_for_each_entry(req, &xprt->bc_pa_list, rq_bc_pa_list) {
343                 if (req->rq_connect_cookie != xprt->connect_cookie)
344                         continue;
345                 if (req->rq_xid == xid)
346                         goto found;
347         }
348         req = xprt_alloc_bc_request(xprt, xid);
349 found:
350         spin_unlock(&xprt->bc_pa_lock);
351         return req;
352 }
353
354 /*
355  * Add callback request to callback list.  The callback
356  * service sleeps on the sv_cb_waitq waiting for new
357  * requests.  Wake it up after adding enqueing the
358  * request.
359  */
360 void xprt_complete_bc_request(struct rpc_rqst *req, uint32_t copied)
361 {
362         struct rpc_xprt *xprt = req->rq_xprt;
363         struct svc_serv *bc_serv = xprt->bc_serv;
364
365         spin_lock(&xprt->bc_pa_lock);
366         list_del(&req->rq_bc_pa_list);
367         xprt_dec_alloc_count(xprt, 1);
368         spin_unlock(&xprt->bc_pa_lock);
369
370         req->rq_private_buf.len = copied;
371         set_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
372
373         dprintk("RPC:       add callback request to list\n");
374         spin_lock(&bc_serv->sv_cb_lock);
375         list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
376         wake_up(&bc_serv->sv_cb_waitq);
377         spin_unlock(&bc_serv->sv_cb_lock);
378 }