xref: /linux/net/sunrpc/backchannel_rqst.c (revision d141ec2825b4d3ec52f27c43bdd864090159273a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 
4 (c) 2007 Network Appliance, Inc.  All Rights Reserved.
5 (c) 2009 NetApp.  All Rights Reserved.
6 
7 
8 ******************************************************************************/
9 
10 #include <linux/tcp.h>
11 #include <linux/slab.h>
12 #include <linux/sunrpc/xprt.h>
13 #include <linux/export.h>
14 #include <linux/sunrpc/bc_xprt.h>
15 
16 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
17 #define RPCDBG_FACILITY	RPCDBG_TRANS
18 #endif
19 
20 #define BC_MAX_SLOTS	64U
21 
xprt_bc_max_slots(struct rpc_xprt * xprt)22 unsigned int xprt_bc_max_slots(struct rpc_xprt *xprt)
23 {
24 	return BC_MAX_SLOTS;
25 }
26 
27 /*
28  * Close the backchannel producer side, drain any requests still
29  * queued on sv_cb_list, then destroy the callback service.
30  */
xprt_svc_destroy_nullify_bc(struct rpc_xprt * xprt,struct svc_serv ** serv)31 void xprt_svc_destroy_nullify_bc(struct rpc_xprt *xprt, struct svc_serv **serv)
32 {
33 	struct svc_serv *bc_serv = *serv;
34 	struct rpc_rqst *req;
35 
36 	xprt_svc_shutdown_bc(xprt);
37 	while ((req = lwq_dequeue(&bc_serv->sv_cb_list, struct rpc_rqst,
38 				  rq_bc_list)) != NULL) {
39 		atomic_dec(&req->rq_xprt->bc_slot_count);
40 		xprt_free_bc_request(req);
41 	}
42 	svc_destroy(serv);
43 }
44 EXPORT_SYMBOL_GPL(xprt_svc_destroy_nullify_bc);
45 
46 /*
47  * Clear the backchannel server pointer in the transport.  The NULL
48  * store is serialized under bc_pa_lock against readers of
49  * xprt->bc_serv in xprt_complete_bc_request() and
50  * rpcrdma_bc_receive_call().  Clearing it before the callback service
51  * is stopped prevents a producer from enqueueing onto a service that
52  * is being torn down.
53  */
xprt_svc_shutdown_bc(struct rpc_xprt * xprt)54 void xprt_svc_shutdown_bc(struct rpc_xprt *xprt)
55 {
56 	spin_lock(&xprt->bc_pa_lock);
57 	xprt->bc_serv = NULL;
58 	spin_unlock(&xprt->bc_pa_lock);
59 }
60 EXPORT_SYMBOL_GPL(xprt_svc_shutdown_bc);
61 
62 /*
63  * Helper routines that track the number of preallocation elements
64  * on the transport.
65  */
xprt_need_to_requeue(struct rpc_xprt * xprt)66 static inline int xprt_need_to_requeue(struct rpc_xprt *xprt)
67 {
68 	return xprt->bc_alloc_count < xprt->bc_alloc_max;
69 }
70 
71 /*
72  * Free the preallocated rpc_rqst structure and the memory
73  * buffers hanging off of it.
74  */
xprt_free_allocation(struct rpc_rqst * req)75 static void xprt_free_allocation(struct rpc_rqst *req)
76 {
77 	struct xdr_buf *xbufp;
78 
79 	dprintk("RPC:        free allocations for req= %p\n", req);
80 	WARN_ON_ONCE(test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state));
81 	xbufp = &req->rq_rcv_buf;
82 	free_page((unsigned long)xbufp->head[0].iov_base);
83 	xbufp = &req->rq_snd_buf;
84 	free_page((unsigned long)xbufp->head[0].iov_base);
85 	kfree(req);
86 }
87 
xprt_bc_reinit_xdr_buf(struct xdr_buf * buf)88 static void xprt_bc_reinit_xdr_buf(struct xdr_buf *buf)
89 {
90 	buf->head[0].iov_len = PAGE_SIZE;
91 	buf->tail[0].iov_len = 0;
92 	buf->pages = NULL;
93 	buf->page_len = 0;
94 	buf->flags = 0;
95 	buf->len = 0;
96 	buf->buflen = PAGE_SIZE;
97 }
98 
xprt_alloc_xdr_buf(struct xdr_buf * buf,gfp_t gfp_flags)99 static int xprt_alloc_xdr_buf(struct xdr_buf *buf, gfp_t gfp_flags)
100 {
101 	struct page *page;
102 	/* Preallocate one XDR receive buffer */
103 	page = alloc_page(gfp_flags);
104 	if (page == NULL)
105 		return -ENOMEM;
106 	xdr_buf_init(buf, page_address(page), PAGE_SIZE);
107 	return 0;
108 }
109 
xprt_alloc_bc_req(struct rpc_xprt * xprt)110 static struct rpc_rqst *xprt_alloc_bc_req(struct rpc_xprt *xprt)
111 {
112 	gfp_t gfp_flags = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
113 	struct rpc_rqst *req;
114 
115 	/* Pre-allocate one backchannel rpc_rqst */
116 	req = kzalloc_obj(*req, gfp_flags);
117 	if (req == NULL)
118 		return NULL;
119 
120 	req->rq_xprt = xprt;
121 
122 	/* Preallocate one XDR receive buffer */
123 	if (xprt_alloc_xdr_buf(&req->rq_rcv_buf, gfp_flags) < 0) {
124 		printk(KERN_ERR "Failed to create bc receive xbuf\n");
125 		goto out_free;
126 	}
127 	req->rq_rcv_buf.len = PAGE_SIZE;
128 
129 	/* Preallocate one XDR send buffer */
130 	if (xprt_alloc_xdr_buf(&req->rq_snd_buf, gfp_flags) < 0) {
131 		printk(KERN_ERR "Failed to create bc snd xbuf\n");
132 		goto out_free;
133 	}
134 	return req;
135 out_free:
136 	xprt_free_allocation(req);
137 	return NULL;
138 }
139 
140 /*
141  * Preallocate up to min_reqs structures and related buffers for use
142  * by the backchannel.  This function can be called multiple times
143  * when creating new sessions that use the same rpc_xprt.  The
144  * preallocated buffers are added to the pool of resources used by
145  * the rpc_xprt.  Any one of these resources may be used by an
146  * incoming callback request.  It's up to the higher levels in the
147  * stack to enforce that the maximum number of session slots is not
148  * being exceeded.
149  *
150  * Some callback arguments can be large.  For example, a pNFS server
151  * using multiple deviceids.  The list can be unbound, but the client
152  * has the ability to tell the server the maximum size of the callback
153  * requests.  Each deviceID is 16 bytes, so allocate one page
154  * for the arguments to have enough room to receive a number of these
155  * deviceIDs.  The NFS client indicates to the pNFS server that its
156  * callback requests can be up to 4096 bytes in size.
157  */
xprt_setup_backchannel(struct rpc_xprt * xprt,unsigned int min_reqs)158 int xprt_setup_backchannel(struct rpc_xprt *xprt, unsigned int min_reqs)
159 {
160 	if (!xprt->ops->bc_setup)
161 		return 0;
162 	return xprt->ops->bc_setup(xprt, min_reqs);
163 }
164 EXPORT_SYMBOL_GPL(xprt_setup_backchannel);
165 
xprt_setup_bc(struct rpc_xprt * xprt,unsigned int min_reqs)166 int xprt_setup_bc(struct rpc_xprt *xprt, unsigned int min_reqs)
167 {
168 	struct rpc_rqst *req;
169 	LIST_HEAD(tmp_list);
170 	int i;
171 
172 	dprintk("RPC:       setup backchannel transport\n");
173 
174 	if (min_reqs > BC_MAX_SLOTS)
175 		min_reqs = BC_MAX_SLOTS;
176 
177 	/*
178 	 * We use a temporary list to keep track of the preallocated
179 	 * buffers.  Once we're done building the list we splice it
180 	 * into the backchannel preallocation list off of the rpc_xprt
181 	 * struct.  This helps minimize the amount of time the list
182 	 * lock is held on the rpc_xprt struct.  It also makes cleanup
183 	 * easier in case of memory allocation errors.
184 	 */
185 	for (i = 0; i < min_reqs; i++) {
186 		/* Pre-allocate one backchannel rpc_rqst */
187 		req = xprt_alloc_bc_req(xprt);
188 		if (req == NULL) {
189 			printk(KERN_ERR "Failed to create bc rpc_rqst\n");
190 			goto out_free;
191 		}
192 
193 		/* Add the allocated buffer to the tmp list */
194 		dprintk("RPC:       adding req= %p\n", req);
195 		list_add(&req->rq_bc_pa_list, &tmp_list);
196 	}
197 
198 	/*
199 	 * Add the temporary list to the backchannel preallocation list
200 	 */
201 	spin_lock(&xprt->bc_pa_lock);
202 	list_splice(&tmp_list, &xprt->bc_pa_list);
203 	xprt->bc_alloc_count += min_reqs;
204 	xprt->bc_alloc_max += min_reqs;
205 	atomic_add(min_reqs, &xprt->bc_slot_count);
206 	spin_unlock(&xprt->bc_pa_lock);
207 
208 	dprintk("RPC:       setup backchannel transport done\n");
209 	return 0;
210 
211 out_free:
212 	/*
213 	 * Memory allocation failed, free the temporary list
214 	 */
215 	while (!list_empty(&tmp_list)) {
216 		req = list_first_entry(&tmp_list,
217 				struct rpc_rqst,
218 				rq_bc_pa_list);
219 		list_del(&req->rq_bc_pa_list);
220 		xprt_free_allocation(req);
221 	}
222 
223 	dprintk("RPC:       setup backchannel transport failed\n");
224 	return -ENOMEM;
225 }
226 
227 /**
228  * xprt_destroy_backchannel - Destroys the backchannel preallocated structures.
229  * @xprt:	the transport holding the preallocated strucures
230  * @max_reqs:	the maximum number of preallocated structures to destroy
231  *
232  * Since these structures may have been allocated by multiple calls
233  * to xprt_setup_backchannel, we only destroy up to the maximum number
234  * of reqs specified by the caller.
235  */
xprt_destroy_backchannel(struct rpc_xprt * xprt,unsigned int max_reqs)236 void xprt_destroy_backchannel(struct rpc_xprt *xprt, unsigned int max_reqs)
237 {
238 	if (xprt->ops->bc_destroy)
239 		xprt->ops->bc_destroy(xprt, max_reqs);
240 }
241 EXPORT_SYMBOL_GPL(xprt_destroy_backchannel);
242 
xprt_destroy_bc(struct rpc_xprt * xprt,unsigned int max_reqs)243 void xprt_destroy_bc(struct rpc_xprt *xprt, unsigned int max_reqs)
244 {
245 	struct rpc_rqst *req = NULL, *tmp = NULL;
246 
247 	dprintk("RPC:        destroy backchannel transport\n");
248 
249 	if (max_reqs == 0)
250 		goto out;
251 
252 	spin_lock_bh(&xprt->bc_pa_lock);
253 	xprt->bc_alloc_max -= min(max_reqs, xprt->bc_alloc_max);
254 	list_for_each_entry_safe(req, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
255 		dprintk("RPC:        req=%p\n", req);
256 		list_del(&req->rq_bc_pa_list);
257 		xprt_free_allocation(req);
258 		xprt->bc_alloc_count--;
259 		atomic_dec(&xprt->bc_slot_count);
260 		if (--max_reqs == 0)
261 			break;
262 	}
263 	spin_unlock_bh(&xprt->bc_pa_lock);
264 
265 out:
266 	dprintk("RPC:        backchannel list empty= %s\n",
267 		list_empty(&xprt->bc_pa_list) ? "true" : "false");
268 }
269 
xprt_get_bc_request(struct rpc_xprt * xprt,__be32 xid,struct rpc_rqst * new)270 static struct rpc_rqst *xprt_get_bc_request(struct rpc_xprt *xprt, __be32 xid,
271 		struct rpc_rqst *new)
272 {
273 	struct rpc_rqst *req = NULL;
274 
275 	dprintk("RPC:       allocate a backchannel request\n");
276 	if (list_empty(&xprt->bc_pa_list)) {
277 		if (!new)
278 			goto not_found;
279 		if (atomic_read(&xprt->bc_slot_count) >= BC_MAX_SLOTS)
280 			goto not_found;
281 		list_add_tail(&new->rq_bc_pa_list, &xprt->bc_pa_list);
282 		xprt->bc_alloc_count++;
283 		atomic_inc(&xprt->bc_slot_count);
284 	}
285 	req = list_first_entry(&xprt->bc_pa_list, struct rpc_rqst,
286 				rq_bc_pa_list);
287 	req->rq_reply_bytes_recvd = 0;
288 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
289 			sizeof(req->rq_private_buf));
290 	req->rq_xid = xid;
291 	req->rq_connect_cookie = xprt->connect_cookie;
292 	dprintk("RPC:       backchannel req=%p\n", req);
293 not_found:
294 	return req;
295 }
296 
297 /*
298  * Return the preallocated rpc_rqst structure and XDR buffers
299  * associated with this rpc_task.
300  */
xprt_free_bc_request(struct rpc_rqst * req)301 void xprt_free_bc_request(struct rpc_rqst *req)
302 {
303 	struct rpc_xprt *xprt = req->rq_xprt;
304 
305 	xprt->ops->bc_free_rqst(req);
306 }
307 
xprt_free_bc_rqst(struct rpc_rqst * req)308 void xprt_free_bc_rqst(struct rpc_rqst *req)
309 {
310 	struct rpc_xprt *xprt = req->rq_xprt;
311 
312 	dprintk("RPC:       free backchannel req=%p\n", req);
313 
314 	req->rq_connect_cookie = xprt->connect_cookie - 1;
315 	smp_mb__before_atomic();
316 	clear_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
317 	smp_mb__after_atomic();
318 
319 	/*
320 	 * Return it to the list of preallocations so that it
321 	 * may be reused by a new callback request.
322 	 */
323 	spin_lock_bh(&xprt->bc_pa_lock);
324 	if (xprt_need_to_requeue(xprt)) {
325 		xprt_bc_reinit_xdr_buf(&req->rq_snd_buf);
326 		xprt_bc_reinit_xdr_buf(&req->rq_rcv_buf);
327 		req->rq_rcv_buf.len = PAGE_SIZE;
328 		list_add_tail(&req->rq_bc_pa_list, &xprt->bc_pa_list);
329 		xprt->bc_alloc_count++;
330 		atomic_inc(&xprt->bc_slot_count);
331 		req = NULL;
332 	}
333 	spin_unlock_bh(&xprt->bc_pa_lock);
334 	if (req != NULL) {
335 		/*
336 		 * The last remaining session was destroyed while this
337 		 * entry was in use.  Free the entry and don't attempt
338 		 * to add back to the list because there is no need to
339 		 * have anymore preallocated entries.
340 		 */
341 		dprintk("RPC:       Last session removed req=%p\n", req);
342 		xprt_free_allocation(req);
343 	}
344 	xprt_put(xprt);
345 }
346 
347 /*
348  * One or more rpc_rqst structure have been preallocated during the
349  * backchannel setup.  Buffer space for the send and private XDR buffers
350  * has been preallocated as well.  Use xprt_alloc_bc_request to allocate
351  * to this request.  Use xprt_free_bc_request to return it.
352  *
353  * We know that we're called in soft interrupt context, grab the spin_lock
354  * since there is no need to grab the bottom half spin_lock.
355  *
356  * Return an available rpc_rqst, otherwise NULL if non are available.
357  */
xprt_lookup_bc_request(struct rpc_xprt * xprt,__be32 xid)358 struct rpc_rqst *xprt_lookup_bc_request(struct rpc_xprt *xprt, __be32 xid)
359 {
360 	struct rpc_rqst *req, *new = NULL;
361 
362 	do {
363 		spin_lock(&xprt->bc_pa_lock);
364 		list_for_each_entry(req, &xprt->bc_pa_list, rq_bc_pa_list) {
365 			if (req->rq_connect_cookie != xprt->connect_cookie)
366 				continue;
367 			if (req->rq_xid == xid)
368 				goto found;
369 		}
370 		req = xprt_get_bc_request(xprt, xid, new);
371 found:
372 		spin_unlock(&xprt->bc_pa_lock);
373 		if (new) {
374 			if (req != new)
375 				xprt_free_allocation(new);
376 			break;
377 		} else if (req)
378 			break;
379 		new = xprt_alloc_bc_req(xprt);
380 	} while (new);
381 	return req;
382 }
383 
384 /*
385  * Add callback request to callback list.  Wake a thread
386  * on the first pool (usually the only pool) to handle it.
387  */
xprt_complete_bc_request(struct rpc_rqst * req,uint32_t copied)388 void xprt_complete_bc_request(struct rpc_rqst *req, uint32_t copied)
389 {
390 	struct rpc_xprt *xprt = req->rq_xprt;
391 
392 	spin_lock(&xprt->bc_pa_lock);
393 	list_del(&req->rq_bc_pa_list);
394 	xprt->bc_alloc_count--;
395 	spin_unlock(&xprt->bc_pa_lock);
396 
397 	req->rq_private_buf.len = copied;
398 	set_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
399 
400 	dprintk("RPC:       add callback request to list\n");
401 	xprt_enqueue_bc_request(req);
402 }
403 
xprt_enqueue_bc_request(struct rpc_rqst * req)404 void xprt_enqueue_bc_request(struct rpc_rqst *req)
405 {
406 	struct rpc_xprt *xprt = req->rq_xprt;
407 	struct svc_serv *bc_serv;
408 
409 	xprt_get(xprt);
410 	spin_lock(&xprt->bc_pa_lock);
411 	bc_serv = xprt->bc_serv;
412 	if (bc_serv) {
413 		lwq_enqueue(&req->rq_bc_list, &bc_serv->sv_cb_list);
414 		svc_pool_wake_idle_thread(&bc_serv->sv_pools[0]);
415 		spin_unlock(&xprt->bc_pa_lock);
416 		return;
417 	}
418 	spin_unlock(&xprt->bc_pa_lock);
419 
420 	atomic_dec(&xprt->bc_slot_count);
421 	xprt_free_bc_request(req);
422 }
423 EXPORT_SYMBOL_GPL(xprt_enqueue_bc_request);
424