blob: 3653898f465ff4b8e6640c74f4d1d9aa292d78cd [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * linux/net/sunrpc/xprt.c
4 *
5 * This is a generic RPC call interface supporting congestion avoidance,
6 * and asynchronous calls.
7 *
8 * The interface works like this:
9 *
10 * - When a process places a call, it allocates a request slot if
11 * one is available. Otherwise, it sleeps on the backlog queue
12 * (xprt_reserve).
13 * - Next, the caller puts together the RPC message, stuffs it into
14 * the request struct, and calls xprt_transmit().
15 * - xprt_transmit sends the message and installs the caller on the
16 * transport's wait list. At the same time, if a reply is expected,
17 * it installs a timer that is run after the packet's timeout has
18 * expired.
19 * - When a packet arrives, the data_ready handler walks the list of
20 * pending requests for that transport. If a matching XID is found, the
21 * caller is woken up, and the timer removed.
22 * - When no reply arrives within the timeout interval, the timer is
23 * fired by the kernel and runs xprt_timer(). It either adjusts the
24 * timeout values (minor timeout) or wakes up the caller with a status
25 * of -ETIMEDOUT.
26 * - When the caller receives a notification from RPC that a reply arrived,
27 * it should release the RPC slot, and process the reply.
28 * If the call timed out, it may choose to retry the operation by
29 * adjusting the initial timeout value, and simply calling rpc_call
30 * again.
31 *
32 * Support for async RPC is done through a set of RPC-specific scheduling
33 * primitives that `transparently' work for processes as well as async
34 * tasks that rely on callbacks.
35 *
36 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
37 *
38 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
39 */
40
41#include <linux/module.h>
42
43#include <linux/types.h>
44#include <linux/interrupt.h>
45#include <linux/workqueue.h>
46#include <linux/net.h>
47#include <linux/ktime.h>
48
49#include <linux/sunrpc/clnt.h>
50#include <linux/sunrpc/metrics.h>
51#include <linux/sunrpc/bc_xprt.h>
52#include <linux/rcupdate.h>
David Brazdil0f672f62019-12-10 10:32:29 +000053#include <linux/sched/mm.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054
55#include <trace/events/sunrpc.h>
56
57#include "sunrpc.h"
58
59/*
60 * Local variables
61 */
62
63#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
64# define RPCDBG_FACILITY RPCDBG_XPRT
65#endif
66
67/*
68 * Local functions
69 */
70static void xprt_init(struct rpc_xprt *xprt, struct net *net);
71static __be32 xprt_alloc_xid(struct rpc_xprt *xprt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072static void xprt_destroy(struct rpc_xprt *xprt);
73
74static DEFINE_SPINLOCK(xprt_list_lock);
75static LIST_HEAD(xprt_list);
76
David Brazdil0f672f62019-12-10 10:32:29 +000077static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
78{
79 unsigned long timeout = jiffies + req->rq_timeout;
80
81 if (time_before(timeout, req->rq_majortimeo))
82 return timeout;
83 return req->rq_majortimeo;
84}
85
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086/**
87 * xprt_register_transport - register a transport implementation
88 * @transport: transport to register
89 *
90 * If a transport implementation is loaded as a kernel module, it can
91 * call this interface to make itself known to the RPC client.
92 *
93 * Returns:
94 * 0: transport successfully registered
95 * -EEXIST: transport already registered
96 * -EINVAL: transport module being unloaded
97 */
98int xprt_register_transport(struct xprt_class *transport)
99{
100 struct xprt_class *t;
101 int result;
102
103 result = -EEXIST;
104 spin_lock(&xprt_list_lock);
105 list_for_each_entry(t, &xprt_list, list) {
106 /* don't register the same transport class twice */
107 if (t->ident == transport->ident)
108 goto out;
109 }
110
111 list_add_tail(&transport->list, &xprt_list);
112 printk(KERN_INFO "RPC: Registered %s transport module.\n",
113 transport->name);
114 result = 0;
115
116out:
117 spin_unlock(&xprt_list_lock);
118 return result;
119}
120EXPORT_SYMBOL_GPL(xprt_register_transport);
121
122/**
123 * xprt_unregister_transport - unregister a transport implementation
124 * @transport: transport to unregister
125 *
126 * Returns:
127 * 0: transport successfully unregistered
128 * -ENOENT: transport never registered
129 */
130int xprt_unregister_transport(struct xprt_class *transport)
131{
132 struct xprt_class *t;
133 int result;
134
135 result = 0;
136 spin_lock(&xprt_list_lock);
137 list_for_each_entry(t, &xprt_list, list) {
138 if (t == transport) {
139 printk(KERN_INFO
140 "RPC: Unregistered %s transport module.\n",
141 transport->name);
142 list_del_init(&transport->list);
143 goto out;
144 }
145 }
146 result = -ENOENT;
147
148out:
149 spin_unlock(&xprt_list_lock);
150 return result;
151}
152EXPORT_SYMBOL_GPL(xprt_unregister_transport);
153
Olivier Deprez0e641232021-09-23 10:07:05 +0200154static void
155xprt_class_release(const struct xprt_class *t)
156{
157 module_put(t->owner);
158}
159
160static const struct xprt_class *
161xprt_class_find_by_netid_locked(const char *netid)
162{
163 const struct xprt_class *t;
164 unsigned int i;
165
166 list_for_each_entry(t, &xprt_list, list) {
167 for (i = 0; t->netid[i][0] != '\0'; i++) {
168 if (strcmp(t->netid[i], netid) != 0)
169 continue;
170 if (!try_module_get(t->owner))
171 continue;
172 return t;
173 }
174 }
175 return NULL;
176}
177
178static const struct xprt_class *
179xprt_class_find_by_netid(const char *netid)
180{
181 const struct xprt_class *t;
182
183 spin_lock(&xprt_list_lock);
184 t = xprt_class_find_by_netid_locked(netid);
185 if (!t) {
186 spin_unlock(&xprt_list_lock);
187 request_module("rpc%s", netid);
188 spin_lock(&xprt_list_lock);
189 t = xprt_class_find_by_netid_locked(netid);
190 }
191 spin_unlock(&xprt_list_lock);
192 return t;
193}
194
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195/**
196 * xprt_load_transport - load a transport implementation
Olivier Deprez0e641232021-09-23 10:07:05 +0200197 * @netid: transport to load
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198 *
199 * Returns:
200 * 0: transport successfully loaded
201 * -ENOENT: transport module not available
202 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200203int xprt_load_transport(const char *netid)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000204{
Olivier Deprez0e641232021-09-23 10:07:05 +0200205 const struct xprt_class *t;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000206
Olivier Deprez0e641232021-09-23 10:07:05 +0200207 t = xprt_class_find_by_netid(netid);
208 if (!t)
209 return -ENOENT;
210 xprt_class_release(t);
211 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000212}
213EXPORT_SYMBOL_GPL(xprt_load_transport);
214
David Brazdil0f672f62019-12-10 10:32:29 +0000215static void xprt_clear_locked(struct rpc_xprt *xprt)
216{
217 xprt->snd_task = NULL;
218 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
219 smp_mb__before_atomic();
220 clear_bit(XPRT_LOCKED, &xprt->state);
221 smp_mb__after_atomic();
222 } else
223 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
224}
225
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000226/**
227 * xprt_reserve_xprt - serialize write access to transports
228 * @task: task that is requesting access to the transport
229 * @xprt: pointer to the target transport
230 *
231 * This prevents mixing the payload of separate requests, and prevents
232 * transport connects from colliding with writes. No congestion control
233 * is provided.
234 */
235int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
236{
237 struct rpc_rqst *req = task->tk_rqstp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238
239 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
240 if (task == xprt->snd_task)
241 return 1;
242 goto out_sleep;
243 }
David Brazdil0f672f62019-12-10 10:32:29 +0000244 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
245 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000246 xprt->snd_task = task;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247
248 return 1;
249
David Brazdil0f672f62019-12-10 10:32:29 +0000250out_unlock:
251 xprt_clear_locked(xprt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252out_sleep:
253 dprintk("RPC: %5u failed to lock transport %p\n",
254 task->tk_pid, xprt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000255 task->tk_status = -EAGAIN;
David Brazdil0f672f62019-12-10 10:32:29 +0000256 if (RPC_IS_SOFT(task))
257 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
258 xprt_request_timeout(req));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000259 else
David Brazdil0f672f62019-12-10 10:32:29 +0000260 rpc_sleep_on(&xprt->sending, task, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000261 return 0;
262}
263EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
264
David Brazdil0f672f62019-12-10 10:32:29 +0000265static bool
266xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267{
David Brazdil0f672f62019-12-10 10:32:29 +0000268 return test_bit(XPRT_CWND_WAIT, &xprt->state);
269}
270
271static void
272xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
273{
274 if (!list_empty(&xprt->xmit_queue)) {
275 /* Peek at head of queue to see if it can make progress */
276 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
277 rq_xmit)->rq_cong)
278 return;
279 }
280 set_bit(XPRT_CWND_WAIT, &xprt->state);
281}
282
283static void
284xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
285{
286 if (!RPCXPRT_CONGESTED(xprt))
287 clear_bit(XPRT_CWND_WAIT, &xprt->state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288}
289
290/*
291 * xprt_reserve_xprt_cong - serialize write access to transports
292 * @task: task that is requesting access to the transport
293 *
294 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
295 * integrated into the decision of whether a request is allowed to be
296 * woken up and given access to the transport.
David Brazdil0f672f62019-12-10 10:32:29 +0000297 * Note that the lock is only granted if we know there are free slots.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000298 */
299int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
300{
301 struct rpc_rqst *req = task->tk_rqstp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302
303 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
304 if (task == xprt->snd_task)
305 return 1;
306 goto out_sleep;
307 }
308 if (req == NULL) {
309 xprt->snd_task = task;
310 return 1;
311 }
David Brazdil0f672f62019-12-10 10:32:29 +0000312 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
313 goto out_unlock;
314 if (!xprt_need_congestion_window_wait(xprt)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000315 xprt->snd_task = task;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316 return 1;
317 }
David Brazdil0f672f62019-12-10 10:32:29 +0000318out_unlock:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000319 xprt_clear_locked(xprt);
320out_sleep:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000321 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322 task->tk_status = -EAGAIN;
David Brazdil0f672f62019-12-10 10:32:29 +0000323 if (RPC_IS_SOFT(task))
324 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
325 xprt_request_timeout(req));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326 else
David Brazdil0f672f62019-12-10 10:32:29 +0000327 rpc_sleep_on(&xprt->sending, task, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000328 return 0;
329}
330EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
331
332static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
333{
334 int retval;
335
David Brazdil0f672f62019-12-10 10:32:29 +0000336 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
337 return 1;
338 spin_lock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000339 retval = xprt->ops->reserve_xprt(xprt, task);
David Brazdil0f672f62019-12-10 10:32:29 +0000340 spin_unlock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000341 return retval;
342}
343
344static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
345{
346 struct rpc_xprt *xprt = data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000347
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000348 xprt->snd_task = task;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349 return true;
350}
351
352static void __xprt_lock_write_next(struct rpc_xprt *xprt)
353{
354 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
355 return;
David Brazdil0f672f62019-12-10 10:32:29 +0000356 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
357 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
359 __xprt_lock_write_func, xprt))
360 return;
David Brazdil0f672f62019-12-10 10:32:29 +0000361out_unlock:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000362 xprt_clear_locked(xprt);
363}
364
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000365static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
366{
367 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
368 return;
David Brazdil0f672f62019-12-10 10:32:29 +0000369 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
370 goto out_unlock;
371 if (xprt_need_congestion_window_wait(xprt))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000372 goto out_unlock;
373 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
David Brazdil0f672f62019-12-10 10:32:29 +0000374 __xprt_lock_write_func, xprt))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000375 return;
376out_unlock:
377 xprt_clear_locked(xprt);
378}
379
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000380/**
381 * xprt_release_xprt - allow other requests to use a transport
382 * @xprt: transport with other tasks potentially waiting
383 * @task: task that is releasing access to the transport
384 *
385 * Note that "task" can be NULL. No congestion control is provided.
386 */
387void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
388{
389 if (xprt->snd_task == task) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000390 xprt_clear_locked(xprt);
391 __xprt_lock_write_next(xprt);
392 }
393}
394EXPORT_SYMBOL_GPL(xprt_release_xprt);
395
396/**
397 * xprt_release_xprt_cong - allow other requests to use a transport
398 * @xprt: transport with other tasks potentially waiting
399 * @task: task that is releasing access to the transport
400 *
401 * Note that "task" can be NULL. Another task is awoken to use the
402 * transport if the transport's congestion window allows it.
403 */
404void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
405{
406 if (xprt->snd_task == task) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000407 xprt_clear_locked(xprt);
408 __xprt_lock_write_next_cong(xprt);
409 }
410}
411EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
412
413static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
414{
David Brazdil0f672f62019-12-10 10:32:29 +0000415 if (xprt->snd_task != task)
416 return;
417 spin_lock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000418 xprt->ops->release_xprt(xprt, task);
David Brazdil0f672f62019-12-10 10:32:29 +0000419 spin_unlock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000420}
421
422/*
423 * Van Jacobson congestion avoidance. Check if the congestion window
424 * overflowed. Put the task to sleep if this is the case.
425 */
426static int
David Brazdil0f672f62019-12-10 10:32:29 +0000427__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000428{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000429 if (req->rq_cong)
430 return 1;
431 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000432 req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
433 if (RPCXPRT_CONGESTED(xprt)) {
434 xprt_set_congestion_window_wait(xprt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000435 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000436 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000437 req->rq_cong = 1;
438 xprt->cong += RPC_CWNDSCALE;
439 return 1;
440}
441
442/*
443 * Adjust the congestion window, and wake up the next task
444 * that has been sleeping due to congestion
445 */
446static void
447__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
448{
449 if (!req->rq_cong)
450 return;
451 req->rq_cong = 0;
452 xprt->cong -= RPC_CWNDSCALE;
David Brazdil0f672f62019-12-10 10:32:29 +0000453 xprt_test_and_clear_congestion_window_wait(xprt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000454 __xprt_lock_write_next_cong(xprt);
455}
456
457/**
David Brazdil0f672f62019-12-10 10:32:29 +0000458 * xprt_request_get_cong - Request congestion control credits
459 * @xprt: pointer to transport
460 * @req: pointer to RPC request
461 *
462 * Useful for transports that require congestion control.
463 */
464bool
465xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
466{
467 bool ret = false;
468
469 if (req->rq_cong)
470 return true;
471 spin_lock(&xprt->transport_lock);
472 ret = __xprt_get_cong(xprt, req) != 0;
473 spin_unlock(&xprt->transport_lock);
474 return ret;
475}
476EXPORT_SYMBOL_GPL(xprt_request_get_cong);
477
478/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479 * xprt_release_rqst_cong - housekeeping when request is complete
480 * @task: RPC request that recently completed
481 *
482 * Useful for transports that require congestion control.
483 */
484void xprt_release_rqst_cong(struct rpc_task *task)
485{
486 struct rpc_rqst *req = task->tk_rqstp;
487
488 __xprt_put_cong(req->rq_xprt, req);
489}
490EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
491
David Brazdil0f672f62019-12-10 10:32:29 +0000492static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
493{
494 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
495 __xprt_lock_write_next_cong(xprt);
496}
497
498/*
499 * Clear the congestion window wait flag and wake up the next
500 * entry on xprt->sending
501 */
502static void
503xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
504{
505 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
506 spin_lock(&xprt->transport_lock);
507 __xprt_lock_write_next_cong(xprt);
508 spin_unlock(&xprt->transport_lock);
509 }
510}
511
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000512/**
513 * xprt_adjust_cwnd - adjust transport congestion window
514 * @xprt: pointer to xprt
515 * @task: recently completed RPC request used to adjust window
516 * @result: result code of completed RPC request
517 *
518 * The transport code maintains an estimate on the maximum number of out-
519 * standing RPC requests, using a smoothed version of the congestion
520 * avoidance implemented in 44BSD. This is basically the Van Jacobson
521 * congestion algorithm: If a retransmit occurs, the congestion window is
522 * halved; otherwise, it is incremented by 1/cwnd when
523 *
524 * - a reply is received and
525 * - a full number of requests are outstanding and
526 * - the congestion window hasn't been updated recently.
527 */
528void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
529{
530 struct rpc_rqst *req = task->tk_rqstp;
531 unsigned long cwnd = xprt->cwnd;
532
533 if (result >= 0 && cwnd <= xprt->cong) {
534 /* The (cwnd >> 1) term makes sure
535 * the result gets rounded properly. */
536 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
537 if (cwnd > RPC_MAXCWND(xprt))
538 cwnd = RPC_MAXCWND(xprt);
539 __xprt_lock_write_next_cong(xprt);
540 } else if (result == -ETIMEDOUT) {
541 cwnd >>= 1;
542 if (cwnd < RPC_CWNDSCALE)
543 cwnd = RPC_CWNDSCALE;
544 }
545 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
546 xprt->cong, xprt->cwnd, cwnd);
547 xprt->cwnd = cwnd;
548 __xprt_put_cong(xprt, req);
549}
550EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
551
552/**
553 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
554 * @xprt: transport with waiting tasks
555 * @status: result code to plant in each task before waking it
556 *
557 */
558void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
559{
560 if (status < 0)
561 rpc_wake_up_status(&xprt->pending, status);
562 else
563 rpc_wake_up(&xprt->pending);
564}
565EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
566
567/**
568 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
David Brazdil0f672f62019-12-10 10:32:29 +0000569 * @xprt: transport
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000570 *
571 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
572 * we don't in general want to force a socket disconnection due to
573 * an incomplete RPC call transmission.
574 */
David Brazdil0f672f62019-12-10 10:32:29 +0000575void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000576{
David Brazdil0f672f62019-12-10 10:32:29 +0000577 set_bit(XPRT_WRITE_SPACE, &xprt->state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000578}
579EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
580
David Brazdil0f672f62019-12-10 10:32:29 +0000581static bool
582xprt_clear_write_space_locked(struct rpc_xprt *xprt)
583{
584 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
585 __xprt_lock_write_next(xprt);
586 dprintk("RPC: write space: waking waiting task on "
587 "xprt %p\n", xprt);
588 return true;
589 }
590 return false;
591}
592
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000593/**
594 * xprt_write_space - wake the task waiting for transport output buffer space
595 * @xprt: transport with waiting tasks
596 *
597 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
598 */
David Brazdil0f672f62019-12-10 10:32:29 +0000599bool xprt_write_space(struct rpc_xprt *xprt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000600{
David Brazdil0f672f62019-12-10 10:32:29 +0000601 bool ret;
602
603 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
604 return false;
605 spin_lock(&xprt->transport_lock);
606 ret = xprt_clear_write_space_locked(xprt);
607 spin_unlock(&xprt->transport_lock);
608 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000609}
610EXPORT_SYMBOL_GPL(xprt_write_space);
611
David Brazdil0f672f62019-12-10 10:32:29 +0000612static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000613{
David Brazdil0f672f62019-12-10 10:32:29 +0000614 s64 delta = ktime_to_ns(ktime_get() - abstime);
615 return likely(delta >= 0) ?
616 jiffies - nsecs_to_jiffies(delta) :
617 jiffies + nsecs_to_jiffies(-delta);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000619
David Brazdil0f672f62019-12-10 10:32:29 +0000620static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000621{
David Brazdil0f672f62019-12-10 10:32:29 +0000622 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
623 unsigned long majortimeo = req->rq_timeout;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000624
David Brazdil0f672f62019-12-10 10:32:29 +0000625 if (to->to_exponential)
626 majortimeo <<= to->to_retries;
627 else
628 majortimeo += to->to_increment * to->to_retries;
629 if (majortimeo > to->to_maxval || majortimeo == 0)
630 majortimeo = to->to_maxval;
631 return majortimeo;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000632}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000633
634static void xprt_reset_majortimeo(struct rpc_rqst *req)
635{
David Brazdil0f672f62019-12-10 10:32:29 +0000636 req->rq_majortimeo += xprt_calc_majortimeo(req);
637}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000638
David Brazdil0f672f62019-12-10 10:32:29 +0000639static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
640{
641 unsigned long time_init;
642 struct rpc_xprt *xprt = req->rq_xprt;
643
644 if (likely(xprt && xprt_connected(xprt)))
645 time_init = jiffies;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000646 else
David Brazdil0f672f62019-12-10 10:32:29 +0000647 time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
648 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
649 req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000650}
651
652/**
653 * xprt_adjust_timeout - adjust timeout values for next retransmit
654 * @req: RPC request containing parameters to use for the adjustment
655 *
656 */
657int xprt_adjust_timeout(struct rpc_rqst *req)
658{
659 struct rpc_xprt *xprt = req->rq_xprt;
660 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
661 int status = 0;
662
663 if (time_before(jiffies, req->rq_majortimeo)) {
664 if (to->to_exponential)
665 req->rq_timeout <<= 1;
666 else
667 req->rq_timeout += to->to_increment;
668 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
669 req->rq_timeout = to->to_maxval;
670 req->rq_retries++;
671 } else {
672 req->rq_timeout = to->to_initval;
673 req->rq_retries = 0;
674 xprt_reset_majortimeo(req);
675 /* Reset the RTT counters == "slow start" */
David Brazdil0f672f62019-12-10 10:32:29 +0000676 spin_lock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000677 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
David Brazdil0f672f62019-12-10 10:32:29 +0000678 spin_unlock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000679 status = -ETIMEDOUT;
680 }
681
682 if (req->rq_timeout == 0) {
683 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
684 req->rq_timeout = 5 * HZ;
685 }
686 return status;
687}
688
689static void xprt_autoclose(struct work_struct *work)
690{
691 struct rpc_xprt *xprt =
692 container_of(work, struct rpc_xprt, task_cleanup);
David Brazdil0f672f62019-12-10 10:32:29 +0000693 unsigned int pflags = memalloc_nofs_save();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000694
695 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
696 xprt->ops->close(xprt);
697 xprt_release_write(xprt, NULL);
698 wake_up_bit(&xprt->state, XPRT_LOCKED);
David Brazdil0f672f62019-12-10 10:32:29 +0000699 memalloc_nofs_restore(pflags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000700}
701
702/**
703 * xprt_disconnect_done - mark a transport as disconnected
704 * @xprt: transport to flag for disconnect
705 *
706 */
707void xprt_disconnect_done(struct rpc_xprt *xprt)
708{
709 dprintk("RPC: disconnected transport %p\n", xprt);
David Brazdil0f672f62019-12-10 10:32:29 +0000710 spin_lock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000711 xprt_clear_connected(xprt);
David Brazdil0f672f62019-12-10 10:32:29 +0000712 xprt_clear_write_space_locked(xprt);
713 xprt_clear_congestion_window_wait_locked(xprt);
714 xprt_wake_pending_tasks(xprt, -ENOTCONN);
715 spin_unlock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000716}
717EXPORT_SYMBOL_GPL(xprt_disconnect_done);
718
719/**
720 * xprt_force_disconnect - force a transport to disconnect
721 * @xprt: transport to disconnect
722 *
723 */
724void xprt_force_disconnect(struct rpc_xprt *xprt)
725{
726 /* Don't race with the test_bit() in xprt_clear_locked() */
David Brazdil0f672f62019-12-10 10:32:29 +0000727 spin_lock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000728 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
729 /* Try to schedule an autoclose RPC call */
730 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
731 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Olivier Deprez0e641232021-09-23 10:07:05 +0200732 else if (xprt->snd_task && !test_bit(XPRT_SND_IS_COOKIE, &xprt->state))
David Brazdil0f672f62019-12-10 10:32:29 +0000733 rpc_wake_up_queued_task_set_status(&xprt->pending,
Olivier Deprez0e641232021-09-23 10:07:05 +0200734 xprt->snd_task, -ENOTCONN);
David Brazdil0f672f62019-12-10 10:32:29 +0000735 spin_unlock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000736}
737EXPORT_SYMBOL_GPL(xprt_force_disconnect);
738
David Brazdil0f672f62019-12-10 10:32:29 +0000739static unsigned int
740xprt_connect_cookie(struct rpc_xprt *xprt)
741{
742 return READ_ONCE(xprt->connect_cookie);
743}
744
745static bool
746xprt_request_retransmit_after_disconnect(struct rpc_task *task)
747{
748 struct rpc_rqst *req = task->tk_rqstp;
749 struct rpc_xprt *xprt = req->rq_xprt;
750
751 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
752 !xprt_connected(xprt);
753}
754
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000755/**
756 * xprt_conditional_disconnect - force a transport to disconnect
757 * @xprt: transport to disconnect
758 * @cookie: 'connection cookie'
759 *
760 * This attempts to break the connection if and only if 'cookie' matches
761 * the current transport 'connection cookie'. It ensures that we don't
762 * try to break the connection more than once when we need to retransmit
763 * a batch of RPC requests.
764 *
765 */
766void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
767{
768 /* Don't race with the test_bit() in xprt_clear_locked() */
David Brazdil0f672f62019-12-10 10:32:29 +0000769 spin_lock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000770 if (cookie != xprt->connect_cookie)
771 goto out;
772 if (test_bit(XPRT_CLOSING, &xprt->state))
773 goto out;
774 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
775 /* Try to schedule an autoclose RPC call */
776 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
777 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
778 xprt_wake_pending_tasks(xprt, -EAGAIN);
779out:
David Brazdil0f672f62019-12-10 10:32:29 +0000780 spin_unlock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000781}
782
783static bool
784xprt_has_timer(const struct rpc_xprt *xprt)
785{
786 return xprt->idle_timeout != 0;
787}
788
789static void
790xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
791 __must_hold(&xprt->transport_lock)
792{
David Brazdil0f672f62019-12-10 10:32:29 +0000793 xprt->last_used = jiffies;
794 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
796}
797
798static void
799xprt_init_autodisconnect(struct timer_list *t)
800{
801 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
802
David Brazdil0f672f62019-12-10 10:32:29 +0000803 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
804 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000805 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
806 xprt->last_used = jiffies;
807 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
David Brazdil0f672f62019-12-10 10:32:29 +0000808 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000809 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000810}
811
812bool xprt_lock_connect(struct rpc_xprt *xprt,
813 struct rpc_task *task,
814 void *cookie)
815{
816 bool ret = false;
817
David Brazdil0f672f62019-12-10 10:32:29 +0000818 spin_lock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000819 if (!test_bit(XPRT_LOCKED, &xprt->state))
820 goto out;
821 if (xprt->snd_task != task)
822 goto out;
Olivier Deprez0e641232021-09-23 10:07:05 +0200823 set_bit(XPRT_SND_IS_COOKIE, &xprt->state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000824 xprt->snd_task = cookie;
825 ret = true;
826out:
David Brazdil0f672f62019-12-10 10:32:29 +0000827 spin_unlock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000828 return ret;
829}
830
831void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
832{
David Brazdil0f672f62019-12-10 10:32:29 +0000833 spin_lock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000834 if (xprt->snd_task != cookie)
835 goto out;
836 if (!test_bit(XPRT_LOCKED, &xprt->state))
837 goto out;
838 xprt->snd_task =NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200839 clear_bit(XPRT_SND_IS_COOKIE, &xprt->state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000840 xprt->ops->release_xprt(xprt, NULL);
841 xprt_schedule_autodisconnect(xprt);
842out:
David Brazdil0f672f62019-12-10 10:32:29 +0000843 spin_unlock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000844 wake_up_bit(&xprt->state, XPRT_LOCKED);
845}
846
847/**
848 * xprt_connect - schedule a transport connect operation
849 * @task: RPC task that is requesting the connect
850 *
851 */
852void xprt_connect(struct rpc_task *task)
853{
854 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
855
856 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
857 xprt, (xprt_connected(xprt) ? "is" : "is not"));
858
859 if (!xprt_bound(xprt)) {
860 task->tk_status = -EAGAIN;
861 return;
862 }
863 if (!xprt_lock_write(xprt, task))
864 return;
865
866 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
867 xprt->ops->close(xprt);
868
869 if (!xprt_connected(xprt)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000870 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
David Brazdil0f672f62019-12-10 10:32:29 +0000871 rpc_sleep_on_timeout(&xprt->pending, task, NULL,
872 xprt_request_timeout(task->tk_rqstp));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000873
874 if (test_bit(XPRT_CLOSING, &xprt->state))
875 return;
876 if (xprt_test_and_set_connecting(xprt))
877 return;
878 /* Race breaker */
879 if (!xprt_connected(xprt)) {
880 xprt->stat.connect_start = jiffies;
881 xprt->ops->connect(xprt, task);
882 } else {
883 xprt_clear_connecting(xprt);
884 task->tk_status = 0;
885 rpc_wake_up_queued_task(&xprt->pending, task);
886 }
887 }
888 xprt_release_write(xprt, task);
889}
890
David Brazdil0f672f62019-12-10 10:32:29 +0000891/**
892 * xprt_reconnect_delay - compute the wait before scheduling a connect
893 * @xprt: transport instance
894 *
895 */
896unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000897{
David Brazdil0f672f62019-12-10 10:32:29 +0000898 unsigned long start, now = jiffies;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000899
David Brazdil0f672f62019-12-10 10:32:29 +0000900 start = xprt->stat.connect_start + xprt->reestablish_timeout;
901 if (time_after(start, now))
902 return start - now;
903 return 0;
904}
905EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000906
David Brazdil0f672f62019-12-10 10:32:29 +0000907/**
908 * xprt_reconnect_backoff - compute the new re-establish timeout
909 * @xprt: transport instance
910 * @init_to: initial reestablish timeout
911 *
912 */
913void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
914{
915 xprt->reestablish_timeout <<= 1;
916 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
917 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
918 if (xprt->reestablish_timeout < init_to)
919 xprt->reestablish_timeout = init_to;
920}
921EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
922
923enum xprt_xid_rb_cmp {
924 XID_RB_EQUAL,
925 XID_RB_LEFT,
926 XID_RB_RIGHT,
927};
928static enum xprt_xid_rb_cmp
929xprt_xid_cmp(__be32 xid1, __be32 xid2)
930{
931 if (xid1 == xid2)
932 return XID_RB_EQUAL;
933 if ((__force u32)xid1 < (__force u32)xid2)
934 return XID_RB_LEFT;
935 return XID_RB_RIGHT;
936}
937
938static struct rpc_rqst *
939xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
940{
941 struct rb_node *n = xprt->recv_queue.rb_node;
942 struct rpc_rqst *req;
943
944 while (n != NULL) {
945 req = rb_entry(n, struct rpc_rqst, rq_recv);
946 switch (xprt_xid_cmp(xid, req->rq_xid)) {
947 case XID_RB_LEFT:
948 n = n->rb_left;
949 break;
950 case XID_RB_RIGHT:
951 n = n->rb_right;
952 break;
953 case XID_RB_EQUAL:
954 return req;
955 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000956 }
David Brazdil0f672f62019-12-10 10:32:29 +0000957 return NULL;
958}
959
960static void
961xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
962{
963 struct rb_node **p = &xprt->recv_queue.rb_node;
964 struct rb_node *n = NULL;
965 struct rpc_rqst *req;
966
967 while (*p != NULL) {
968 n = *p;
969 req = rb_entry(n, struct rpc_rqst, rq_recv);
970 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
971 case XID_RB_LEFT:
972 p = &n->rb_left;
973 break;
974 case XID_RB_RIGHT:
975 p = &n->rb_right;
976 break;
977 case XID_RB_EQUAL:
978 WARN_ON_ONCE(new != req);
979 return;
980 }
981 }
982 rb_link_node(&new->rq_recv, n, p);
983 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
984}
985
986static void
987xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
988{
989 rb_erase(&req->rq_recv, &xprt->recv_queue);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000990}
991
992/**
993 * xprt_lookup_rqst - find an RPC request corresponding to an XID
994 * @xprt: transport on which the original request was transmitted
995 * @xid: RPC XID of incoming reply
996 *
David Brazdil0f672f62019-12-10 10:32:29 +0000997 * Caller holds xprt->queue_lock.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000998 */
999struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
1000{
1001 struct rpc_rqst *entry;
1002
David Brazdil0f672f62019-12-10 10:32:29 +00001003 entry = xprt_request_rb_find(xprt, xid);
1004 if (entry != NULL) {
1005 trace_xprt_lookup_rqst(xprt, xid, 0);
1006 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
1007 return entry;
1008 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001009
1010 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
1011 ntohl(xid));
1012 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
1013 xprt->stat.bad_xids++;
1014 return NULL;
1015}
1016EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
1017
David Brazdil0f672f62019-12-10 10:32:29 +00001018static bool
1019xprt_is_pinned_rqst(struct rpc_rqst *req)
1020{
1021 return atomic_read(&req->rq_pin) != 0;
1022}
1023
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001024/**
1025 * xprt_pin_rqst - Pin a request on the transport receive list
1026 * @req: Request to pin
1027 *
1028 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
David Brazdil0f672f62019-12-10 10:32:29 +00001029 * so should be holding xprt->queue_lock.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030 */
1031void xprt_pin_rqst(struct rpc_rqst *req)
1032{
David Brazdil0f672f62019-12-10 10:32:29 +00001033 atomic_inc(&req->rq_pin);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001034}
1035EXPORT_SYMBOL_GPL(xprt_pin_rqst);
1036
1037/**
1038 * xprt_unpin_rqst - Unpin a request on the transport receive list
1039 * @req: Request to pin
1040 *
David Brazdil0f672f62019-12-10 10:32:29 +00001041 * Caller should be holding xprt->queue_lock.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001042 */
1043void xprt_unpin_rqst(struct rpc_rqst *req)
1044{
David Brazdil0f672f62019-12-10 10:32:29 +00001045 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
1046 atomic_dec(&req->rq_pin);
1047 return;
1048 }
1049 if (atomic_dec_and_test(&req->rq_pin))
1050 wake_up_var(&req->rq_pin);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001051}
1052EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
1053
1054static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001055{
David Brazdil0f672f62019-12-10 10:32:29 +00001056 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
1057}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001058
David Brazdil0f672f62019-12-10 10:32:29 +00001059static bool
1060xprt_request_data_received(struct rpc_task *task)
1061{
1062 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1063 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1064}
1065
1066static bool
1067xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1068{
1069 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1070 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1071}
1072
1073/**
1074 * xprt_request_enqueue_receive - Add an request to the receive queue
1075 * @task: RPC task
1076 *
1077 */
1078void
1079xprt_request_enqueue_receive(struct rpc_task *task)
1080{
1081 struct rpc_rqst *req = task->tk_rqstp;
1082 struct rpc_xprt *xprt = req->rq_xprt;
1083
1084 if (!xprt_request_need_enqueue_receive(task, req))
1085 return;
1086
1087 xprt_request_prepare(task->tk_rqstp);
1088 spin_lock(&xprt->queue_lock);
1089
1090 /* Update the softirq receive buffer */
1091 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1092 sizeof(req->rq_private_buf));
1093
1094 /* Add request to the receive list */
1095 xprt_request_rb_insert(xprt, req);
1096 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1097 spin_unlock(&xprt->queue_lock);
1098
1099 /* Turn off autodisconnect */
1100 del_singleshot_timer_sync(&xprt->timer);
1101}
1102
1103/**
1104 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1105 * @task: RPC task
1106 *
1107 * Caller must hold xprt->queue_lock.
1108 */
1109static void
1110xprt_request_dequeue_receive_locked(struct rpc_task *task)
1111{
1112 struct rpc_rqst *req = task->tk_rqstp;
1113
1114 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1115 xprt_request_rb_remove(req->rq_xprt, req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001116}
1117
1118/**
1119 * xprt_update_rtt - Update RPC RTT statistics
1120 * @task: RPC request that recently completed
1121 *
David Brazdil0f672f62019-12-10 10:32:29 +00001122 * Caller holds xprt->queue_lock.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001123 */
1124void xprt_update_rtt(struct rpc_task *task)
1125{
1126 struct rpc_rqst *req = task->tk_rqstp;
1127 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
1128 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
1129 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
1130
1131 if (timer) {
1132 if (req->rq_ntrans == 1)
1133 rpc_update_rtt(rtt, timer, m);
1134 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
1135 }
1136}
1137EXPORT_SYMBOL_GPL(xprt_update_rtt);
1138
1139/**
1140 * xprt_complete_rqst - called when reply processing is complete
1141 * @task: RPC request that recently completed
1142 * @copied: actual number of bytes received from the transport
1143 *
David Brazdil0f672f62019-12-10 10:32:29 +00001144 * Caller holds xprt->queue_lock.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001145 */
1146void xprt_complete_rqst(struct rpc_task *task, int copied)
1147{
1148 struct rpc_rqst *req = task->tk_rqstp;
1149 struct rpc_xprt *xprt = req->rq_xprt;
1150
1151 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
1152 task->tk_pid, ntohl(req->rq_xid), copied);
1153 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
1154
1155 xprt->stat.recvs++;
1156
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001157 req->rq_private_buf.len = copied;
1158 /* Ensure all writes are done before we update */
1159 /* req->rq_reply_bytes_recvd */
1160 smp_wmb();
1161 req->rq_reply_bytes_recvd = copied;
David Brazdil0f672f62019-12-10 10:32:29 +00001162 xprt_request_dequeue_receive_locked(task);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001163 rpc_wake_up_queued_task(&xprt->pending, task);
1164}
1165EXPORT_SYMBOL_GPL(xprt_complete_rqst);
1166
1167static void xprt_timer(struct rpc_task *task)
1168{
1169 struct rpc_rqst *req = task->tk_rqstp;
1170 struct rpc_xprt *xprt = req->rq_xprt;
1171
1172 if (task->tk_status != -ETIMEDOUT)
1173 return;
1174
1175 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1176 if (!req->rq_reply_bytes_recvd) {
1177 if (xprt->ops->timer)
1178 xprt->ops->timer(xprt, task);
1179 } else
1180 task->tk_status = 0;
1181}
1182
1183/**
David Brazdil0f672f62019-12-10 10:32:29 +00001184 * xprt_wait_for_reply_request_def - wait for reply
1185 * @task: pointer to rpc_task
1186 *
1187 * Set a request's retransmit timeout based on the transport's
1188 * default timeout parameters. Used by transports that don't adjust
1189 * the retransmit timeout based on round-trip time estimation,
1190 * and put the task to sleep on the pending queue.
1191 */
1192void xprt_wait_for_reply_request_def(struct rpc_task *task)
1193{
1194 struct rpc_rqst *req = task->tk_rqstp;
1195
1196 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1197 xprt_request_timeout(req));
1198}
1199EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
1200
1201/**
1202 * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
1203 * @task: pointer to rpc_task
1204 *
1205 * Set a request's retransmit timeout using the RTT estimator,
1206 * and put the task to sleep on the pending queue.
1207 */
1208void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
1209{
1210 int timer = task->tk_msg.rpc_proc->p_timer;
1211 struct rpc_clnt *clnt = task->tk_client;
1212 struct rpc_rtt *rtt = clnt->cl_rtt;
1213 struct rpc_rqst *req = task->tk_rqstp;
1214 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
1215 unsigned long timeout;
1216
1217 timeout = rpc_calc_rto(rtt, timer);
1218 timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
1219 if (timeout > max_timeout || timeout == 0)
1220 timeout = max_timeout;
1221 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1222 jiffies + timeout);
1223}
1224EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
1225
1226/**
1227 * xprt_request_wait_receive - wait for the reply to an RPC request
1228 * @task: RPC task about to send a request
1229 *
1230 */
1231void xprt_request_wait_receive(struct rpc_task *task)
1232{
1233 struct rpc_rqst *req = task->tk_rqstp;
1234 struct rpc_xprt *xprt = req->rq_xprt;
1235
1236 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1237 return;
1238 /*
1239 * Sleep on the pending queue if we're expecting a reply.
1240 * The spinlock ensures atomicity between the test of
1241 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1242 */
1243 spin_lock(&xprt->queue_lock);
1244 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
1245 xprt->ops->wait_for_reply_request(task);
1246 /*
1247 * Send an extra queue wakeup call if the
1248 * connection was dropped in case the call to
1249 * rpc_sleep_on() raced.
1250 */
1251 if (xprt_request_retransmit_after_disconnect(task))
1252 rpc_wake_up_queued_task_set_status(&xprt->pending,
1253 task, -ENOTCONN);
1254 }
1255 spin_unlock(&xprt->queue_lock);
1256}
1257
1258static bool
1259xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1260{
1261 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1262}
1263
1264/**
1265 * xprt_request_enqueue_transmit - queue a task for transmission
1266 * @task: pointer to rpc_task
1267 *
1268 * Add a task to the transmission queue.
1269 */
1270void
1271xprt_request_enqueue_transmit(struct rpc_task *task)
1272{
1273 struct rpc_rqst *pos, *req = task->tk_rqstp;
1274 struct rpc_xprt *xprt = req->rq_xprt;
1275
1276 if (xprt_request_need_enqueue_transmit(task, req)) {
1277 req->rq_bytes_sent = 0;
1278 spin_lock(&xprt->queue_lock);
1279 /*
1280 * Requests that carry congestion control credits are added
1281 * to the head of the list to avoid starvation issues.
1282 */
1283 if (req->rq_cong) {
1284 xprt_clear_congestion_window_wait(xprt);
1285 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1286 if (pos->rq_cong)
1287 continue;
1288 /* Note: req is added _before_ pos */
1289 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1290 INIT_LIST_HEAD(&req->rq_xmit2);
1291 trace_xprt_enq_xmit(task, 1);
1292 goto out;
1293 }
1294 } else if (RPC_IS_SWAPPER(task)) {
1295 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1296 if (pos->rq_cong || pos->rq_bytes_sent)
1297 continue;
1298 if (RPC_IS_SWAPPER(pos->rq_task))
1299 continue;
1300 /* Note: req is added _before_ pos */
1301 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1302 INIT_LIST_HEAD(&req->rq_xmit2);
1303 trace_xprt_enq_xmit(task, 2);
1304 goto out;
1305 }
1306 } else if (!req->rq_seqno) {
1307 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1308 if (pos->rq_task->tk_owner != task->tk_owner)
1309 continue;
1310 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1311 INIT_LIST_HEAD(&req->rq_xmit);
1312 trace_xprt_enq_xmit(task, 3);
1313 goto out;
1314 }
1315 }
1316 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1317 INIT_LIST_HEAD(&req->rq_xmit2);
1318 trace_xprt_enq_xmit(task, 4);
1319out:
1320 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1321 spin_unlock(&xprt->queue_lock);
1322 }
1323}
1324
1325/**
1326 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1327 * @task: pointer to rpc_task
1328 *
1329 * Remove a task from the transmission queue
1330 * Caller must hold xprt->queue_lock
1331 */
1332static void
1333xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1334{
1335 struct rpc_rqst *req = task->tk_rqstp;
1336
1337 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1338 return;
1339 if (!list_empty(&req->rq_xmit)) {
1340 list_del(&req->rq_xmit);
1341 if (!list_empty(&req->rq_xmit2)) {
1342 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1343 struct rpc_rqst, rq_xmit2);
1344 list_del(&req->rq_xmit2);
1345 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1346 }
1347 } else
1348 list_del(&req->rq_xmit2);
1349}
1350
1351/**
1352 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1353 * @task: pointer to rpc_task
1354 *
1355 * Remove a task from the transmission queue
1356 */
1357static void
1358xprt_request_dequeue_transmit(struct rpc_task *task)
1359{
1360 struct rpc_rqst *req = task->tk_rqstp;
1361 struct rpc_xprt *xprt = req->rq_xprt;
1362
1363 spin_lock(&xprt->queue_lock);
1364 xprt_request_dequeue_transmit_locked(task);
1365 spin_unlock(&xprt->queue_lock);
1366}
1367
1368/**
1369 * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
1370 * @task: pointer to rpc_task
1371 *
1372 * Remove a task from the transmit and receive queues, and ensure that
1373 * it is not pinned by the receive work item.
1374 */
1375void
1376xprt_request_dequeue_xprt(struct rpc_task *task)
1377{
1378 struct rpc_rqst *req = task->tk_rqstp;
1379 struct rpc_xprt *xprt = req->rq_xprt;
1380
1381 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1382 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1383 xprt_is_pinned_rqst(req)) {
1384 spin_lock(&xprt->queue_lock);
1385 xprt_request_dequeue_transmit_locked(task);
1386 xprt_request_dequeue_receive_locked(task);
1387 while (xprt_is_pinned_rqst(req)) {
1388 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1389 spin_unlock(&xprt->queue_lock);
1390 xprt_wait_on_pinned_rqst(req);
1391 spin_lock(&xprt->queue_lock);
1392 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1393 }
1394 spin_unlock(&xprt->queue_lock);
1395 }
1396}
1397
1398/**
1399 * xprt_request_prepare - prepare an encoded request for transport
1400 * @req: pointer to rpc_rqst
1401 *
1402 * Calls into the transport layer to do whatever is needed to prepare
1403 * the request for transmission or receive.
1404 */
1405void
1406xprt_request_prepare(struct rpc_rqst *req)
1407{
1408 struct rpc_xprt *xprt = req->rq_xprt;
1409
1410 if (xprt->ops->prepare_request)
1411 xprt->ops->prepare_request(req);
1412}
1413
1414/**
1415 * xprt_request_need_retransmit - Test if a task needs retransmission
1416 * @task: pointer to rpc_task
1417 *
1418 * Test for whether a connection breakage requires the task to retransmit
1419 */
1420bool
1421xprt_request_need_retransmit(struct rpc_task *task)
1422{
1423 return xprt_request_retransmit_after_disconnect(task);
1424}
1425
1426/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001427 * xprt_prepare_transmit - reserve the transport before sending a request
1428 * @task: RPC task about to send a request
1429 *
1430 */
1431bool xprt_prepare_transmit(struct rpc_task *task)
1432{
1433 struct rpc_rqst *req = task->tk_rqstp;
1434 struct rpc_xprt *xprt = req->rq_xprt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001435
1436 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
1437
David Brazdil0f672f62019-12-10 10:32:29 +00001438 if (!xprt_lock_write(xprt, task)) {
1439 /* Race breaker: someone may have transmitted us */
1440 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1441 rpc_wake_up_queued_task_set_status(&xprt->sending,
1442 task, 0);
1443 return false;
1444
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001445 }
David Brazdil0f672f62019-12-10 10:32:29 +00001446 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001447}
1448
1449void xprt_end_transmit(struct rpc_task *task)
1450{
1451 xprt_release_write(task->tk_rqstp->rq_xprt, task);
1452}
1453
1454/**
David Brazdil0f672f62019-12-10 10:32:29 +00001455 * xprt_request_transmit - send an RPC request on a transport
1456 * @req: pointer to request to transmit
1457 * @snd_task: RPC task that owns the transport lock
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001458 *
David Brazdil0f672f62019-12-10 10:32:29 +00001459 * This performs the transmission of a single request.
1460 * Note that if the request is not the same as snd_task, then it
1461 * does need to be pinned.
1462 * Returns '0' on success.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001463 */
David Brazdil0f672f62019-12-10 10:32:29 +00001464static int
1465xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001466{
David Brazdil0f672f62019-12-10 10:32:29 +00001467 struct rpc_xprt *xprt = req->rq_xprt;
1468 struct rpc_task *task = req->rq_task;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001469 unsigned int connect_cookie;
David Brazdil0f672f62019-12-10 10:32:29 +00001470 int is_retrans = RPC_WAS_SENT(task);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001471 int status;
1472
David Brazdil0f672f62019-12-10 10:32:29 +00001473 if (!req->rq_bytes_sent) {
1474 if (xprt_request_data_received(task)) {
1475 status = 0;
1476 goto out_dequeue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001477 }
David Brazdil0f672f62019-12-10 10:32:29 +00001478 /* Verify that our message lies in the RPCSEC_GSS window */
1479 if (rpcauth_xmit_need_reencode(task)) {
1480 status = -EBADMSG;
1481 goto out_dequeue;
1482 }
1483 if (RPC_SIGNALLED(task)) {
1484 status = -ERESTARTSYS;
1485 goto out_dequeue;
1486 }
1487 }
1488
1489 /*
1490 * Update req->rq_ntrans before transmitting to avoid races with
1491 * xprt_update_rtt(), which needs to know that it is recording a
1492 * reply to the first transmission.
1493 */
1494 req->rq_ntrans++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001495
1496 connect_cookie = xprt->connect_cookie;
David Brazdil0f672f62019-12-10 10:32:29 +00001497 status = xprt->ops->send_request(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001498 if (status != 0) {
David Brazdil0f672f62019-12-10 10:32:29 +00001499 req->rq_ntrans--;
1500 trace_xprt_transmit(req, status);
1501 return status;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001502 }
David Brazdil0f672f62019-12-10 10:32:29 +00001503
1504 if (is_retrans)
1505 task->tk_client->cl_stats->rpcretrans++;
1506
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001507 xprt_inject_disconnect(xprt);
1508
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001509 task->tk_flags |= RPC_TASK_SENT;
David Brazdil0f672f62019-12-10 10:32:29 +00001510 spin_lock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001511
1512 xprt->stat.sends++;
1513 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1514 xprt->stat.bklog_u += xprt->backlog.qlen;
1515 xprt->stat.sending_u += xprt->sending.qlen;
1516 xprt->stat.pending_u += xprt->pending.qlen;
David Brazdil0f672f62019-12-10 10:32:29 +00001517 spin_unlock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001518
1519 req->rq_connect_cookie = connect_cookie;
David Brazdil0f672f62019-12-10 10:32:29 +00001520out_dequeue:
1521 trace_xprt_transmit(req, status);
1522 xprt_request_dequeue_transmit(task);
1523 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1524 return status;
1525}
1526
1527/**
1528 * xprt_transmit - send an RPC request on a transport
1529 * @task: controlling RPC task
1530 *
1531 * Attempts to drain the transmit queue. On exit, either the transport
1532 * signalled an error that needs to be handled before transmission can
1533 * resume, or @task finished transmitting, and detected that it already
1534 * received a reply.
1535 */
1536void
1537xprt_transmit(struct rpc_task *task)
1538{
1539 struct rpc_rqst *next, *req = task->tk_rqstp;
1540 struct rpc_xprt *xprt = req->rq_xprt;
Olivier Deprez0e641232021-09-23 10:07:05 +02001541 int counter, status;
David Brazdil0f672f62019-12-10 10:32:29 +00001542
1543 spin_lock(&xprt->queue_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02001544 counter = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001545 while (!list_empty(&xprt->xmit_queue)) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001546 if (++counter == 20)
1547 break;
David Brazdil0f672f62019-12-10 10:32:29 +00001548 next = list_first_entry(&xprt->xmit_queue,
1549 struct rpc_rqst, rq_xmit);
1550 xprt_pin_rqst(next);
1551 spin_unlock(&xprt->queue_lock);
1552 status = xprt_request_transmit(next, task);
1553 if (status == -EBADMSG && next != req)
1554 status = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001555 spin_lock(&xprt->queue_lock);
1556 xprt_unpin_rqst(next);
1557 if (status == 0) {
1558 if (!xprt_request_data_received(task) ||
1559 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1560 continue;
1561 } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1562 task->tk_status = status;
1563 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001564 }
David Brazdil0f672f62019-12-10 10:32:29 +00001565 spin_unlock(&xprt->queue_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001566}
1567
1568static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1569{
1570 set_bit(XPRT_CONGESTED, &xprt->state);
1571 rpc_sleep_on(&xprt->backlog, task, NULL);
1572}
1573
1574static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1575{
1576 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1577 clear_bit(XPRT_CONGESTED, &xprt->state);
1578}
1579
1580static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1581{
1582 bool ret = false;
1583
1584 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1585 goto out;
1586 spin_lock(&xprt->reserve_lock);
1587 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1588 rpc_sleep_on(&xprt->backlog, task, NULL);
1589 ret = true;
1590 }
1591 spin_unlock(&xprt->reserve_lock);
1592out:
1593 return ret;
1594}
1595
1596static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1597{
1598 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1599
1600 if (xprt->num_reqs >= xprt->max_reqs)
1601 goto out;
1602 ++xprt->num_reqs;
1603 spin_unlock(&xprt->reserve_lock);
1604 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1605 spin_lock(&xprt->reserve_lock);
1606 if (req != NULL)
1607 goto out;
1608 --xprt->num_reqs;
1609 req = ERR_PTR(-ENOMEM);
1610out:
1611 return req;
1612}
1613
1614static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1615{
1616 if (xprt->num_reqs > xprt->min_reqs) {
1617 --xprt->num_reqs;
1618 kfree(req);
1619 return true;
1620 }
1621 return false;
1622}
1623
1624void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1625{
1626 struct rpc_rqst *req;
1627
1628 spin_lock(&xprt->reserve_lock);
1629 if (!list_empty(&xprt->free)) {
1630 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1631 list_del(&req->rq_list);
1632 goto out_init_req;
1633 }
1634 req = xprt_dynamic_alloc_slot(xprt);
1635 if (!IS_ERR(req))
1636 goto out_init_req;
1637 switch (PTR_ERR(req)) {
1638 case -ENOMEM:
1639 dprintk("RPC: dynamic allocation of request slot "
1640 "failed! Retrying\n");
1641 task->tk_status = -ENOMEM;
1642 break;
1643 case -EAGAIN:
1644 xprt_add_backlog(xprt, task);
1645 dprintk("RPC: waiting for request slot\n");
1646 /* fall through */
1647 default:
1648 task->tk_status = -EAGAIN;
1649 }
1650 spin_unlock(&xprt->reserve_lock);
1651 return;
1652out_init_req:
1653 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1654 xprt->num_reqs);
1655 spin_unlock(&xprt->reserve_lock);
1656
1657 task->tk_status = 0;
1658 task->tk_rqstp = req;
1659}
1660EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1661
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001662void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1663{
1664 spin_lock(&xprt->reserve_lock);
1665 if (!xprt_dynamic_free_slot(xprt, req)) {
1666 memset(req, 0, sizeof(*req)); /* mark unused */
1667 list_add(&req->rq_list, &xprt->free);
1668 }
1669 xprt_wake_up_backlog(xprt);
1670 spin_unlock(&xprt->reserve_lock);
1671}
1672EXPORT_SYMBOL_GPL(xprt_free_slot);
1673
1674static void xprt_free_all_slots(struct rpc_xprt *xprt)
1675{
1676 struct rpc_rqst *req;
1677 while (!list_empty(&xprt->free)) {
1678 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1679 list_del(&req->rq_list);
1680 kfree(req);
1681 }
1682}
1683
1684struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1685 unsigned int num_prealloc,
1686 unsigned int max_alloc)
1687{
1688 struct rpc_xprt *xprt;
1689 struct rpc_rqst *req;
1690 int i;
1691
1692 xprt = kzalloc(size, GFP_KERNEL);
1693 if (xprt == NULL)
1694 goto out;
1695
1696 xprt_init(xprt, net);
1697
1698 for (i = 0; i < num_prealloc; i++) {
1699 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1700 if (!req)
1701 goto out_free;
1702 list_add(&req->rq_list, &xprt->free);
1703 }
1704 if (max_alloc > num_prealloc)
1705 xprt->max_reqs = max_alloc;
1706 else
1707 xprt->max_reqs = num_prealloc;
1708 xprt->min_reqs = num_prealloc;
1709 xprt->num_reqs = num_prealloc;
1710
1711 return xprt;
1712
1713out_free:
1714 xprt_free(xprt);
1715out:
1716 return NULL;
1717}
1718EXPORT_SYMBOL_GPL(xprt_alloc);
1719
1720void xprt_free(struct rpc_xprt *xprt)
1721{
1722 put_net(xprt->xprt_net);
1723 xprt_free_all_slots(xprt);
1724 kfree_rcu(xprt, rcu);
1725}
1726EXPORT_SYMBOL_GPL(xprt_free);
1727
David Brazdil0f672f62019-12-10 10:32:29 +00001728static void
1729xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1730{
1731 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1732}
1733
1734static __be32
1735xprt_alloc_xid(struct rpc_xprt *xprt)
1736{
1737 __be32 xid;
1738
1739 spin_lock(&xprt->reserve_lock);
1740 xid = (__force __be32)xprt->xid++;
1741 spin_unlock(&xprt->reserve_lock);
1742 return xid;
1743}
1744
1745static void
1746xprt_init_xid(struct rpc_xprt *xprt)
1747{
1748 xprt->xid = prandom_u32();
1749}
1750
1751static void
1752xprt_request_init(struct rpc_task *task)
1753{
1754 struct rpc_xprt *xprt = task->tk_xprt;
1755 struct rpc_rqst *req = task->tk_rqstp;
1756
1757 req->rq_task = task;
1758 req->rq_xprt = xprt;
1759 req->rq_buffer = NULL;
1760 req->rq_xid = xprt_alloc_xid(xprt);
1761 xprt_init_connect_cookie(req, xprt);
1762 req->rq_snd_buf.len = 0;
1763 req->rq_snd_buf.buflen = 0;
1764 req->rq_rcv_buf.len = 0;
1765 req->rq_rcv_buf.buflen = 0;
1766 req->rq_snd_buf.bvec = NULL;
1767 req->rq_rcv_buf.bvec = NULL;
1768 req->rq_release_snd_buf = NULL;
1769 xprt_init_majortimeo(task, req);
1770 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1771 req, ntohl(req->rq_xid));
1772}
1773
1774static void
1775xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1776{
1777 xprt->ops->alloc_slot(xprt, task);
1778 if (task->tk_rqstp != NULL)
1779 xprt_request_init(task);
1780}
1781
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001782/**
1783 * xprt_reserve - allocate an RPC request slot
1784 * @task: RPC task requesting a slot allocation
1785 *
1786 * If the transport is marked as being congested, or if no more
1787 * slots are available, place the task on the transport's
1788 * backlog queue.
1789 */
1790void xprt_reserve(struct rpc_task *task)
1791{
1792 struct rpc_xprt *xprt = task->tk_xprt;
1793
1794 task->tk_status = 0;
1795 if (task->tk_rqstp != NULL)
1796 return;
1797
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001798 task->tk_status = -EAGAIN;
1799 if (!xprt_throttle_congested(xprt, task))
David Brazdil0f672f62019-12-10 10:32:29 +00001800 xprt_do_reserve(xprt, task);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001801}
1802
1803/**
1804 * xprt_retry_reserve - allocate an RPC request slot
1805 * @task: RPC task requesting a slot allocation
1806 *
1807 * If no more slots are available, place the task on the transport's
1808 * backlog queue.
1809 * Note that the only difference with xprt_reserve is that we now
1810 * ignore the value of the XPRT_CONGESTED flag.
1811 */
1812void xprt_retry_reserve(struct rpc_task *task)
1813{
1814 struct rpc_xprt *xprt = task->tk_xprt;
1815
1816 task->tk_status = 0;
1817 if (task->tk_rqstp != NULL)
1818 return;
1819
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001820 task->tk_status = -EAGAIN;
David Brazdil0f672f62019-12-10 10:32:29 +00001821 xprt_do_reserve(xprt, task);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001822}
1823
1824/**
1825 * xprt_release - release an RPC request slot
1826 * @task: task which is finished with the slot
1827 *
1828 */
1829void xprt_release(struct rpc_task *task)
1830{
1831 struct rpc_xprt *xprt;
1832 struct rpc_rqst *req = task->tk_rqstp;
1833
1834 if (req == NULL) {
1835 if (task->tk_client) {
1836 xprt = task->tk_xprt;
David Brazdil0f672f62019-12-10 10:32:29 +00001837 xprt_release_write(xprt, task);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001838 }
1839 return;
1840 }
1841
1842 xprt = req->rq_xprt;
David Brazdil0f672f62019-12-10 10:32:29 +00001843 xprt_request_dequeue_xprt(task);
1844 spin_lock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001845 xprt->ops->release_xprt(xprt, task);
1846 if (xprt->ops->release_request)
1847 xprt->ops->release_request(task);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001848 xprt_schedule_autodisconnect(xprt);
David Brazdil0f672f62019-12-10 10:32:29 +00001849 spin_unlock(&xprt->transport_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001850 if (req->rq_buffer)
1851 xprt->ops->buf_free(task);
1852 xprt_inject_disconnect(xprt);
David Brazdil0f672f62019-12-10 10:32:29 +00001853 xdr_free_bvec(&req->rq_rcv_buf);
1854 xdr_free_bvec(&req->rq_snd_buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001855 if (req->rq_cred != NULL)
1856 put_rpccred(req->rq_cred);
1857 task->tk_rqstp = NULL;
1858 if (req->rq_release_snd_buf)
1859 req->rq_release_snd_buf(req);
1860
1861 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1862 if (likely(!bc_prealloc(req)))
1863 xprt->ops->free_slot(xprt, req);
1864 else
1865 xprt_free_bc_request(req);
1866}
1867
David Brazdil0f672f62019-12-10 10:32:29 +00001868#ifdef CONFIG_SUNRPC_BACKCHANNEL
1869void
1870xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1871{
1872 struct xdr_buf *xbufp = &req->rq_snd_buf;
1873
1874 task->tk_rqstp = req;
1875 req->rq_task = task;
1876 xprt_init_connect_cookie(req, req->rq_xprt);
1877 /*
1878 * Set up the xdr_buf length.
1879 * This also indicates that the buffer is XDR encoded already.
1880 */
1881 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1882 xbufp->tail[0].iov_len;
1883}
1884#endif
1885
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001886static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1887{
1888 kref_init(&xprt->kref);
1889
1890 spin_lock_init(&xprt->transport_lock);
1891 spin_lock_init(&xprt->reserve_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00001892 spin_lock_init(&xprt->queue_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001893
1894 INIT_LIST_HEAD(&xprt->free);
David Brazdil0f672f62019-12-10 10:32:29 +00001895 xprt->recv_queue = RB_ROOT;
1896 INIT_LIST_HEAD(&xprt->xmit_queue);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001897#if defined(CONFIG_SUNRPC_BACKCHANNEL)
1898 spin_lock_init(&xprt->bc_pa_lock);
1899 INIT_LIST_HEAD(&xprt->bc_pa_list);
1900#endif /* CONFIG_SUNRPC_BACKCHANNEL */
1901 INIT_LIST_HEAD(&xprt->xprt_switch);
1902
1903 xprt->last_used = jiffies;
1904 xprt->cwnd = RPC_INITCWND;
1905 xprt->bind_index = 0;
1906
1907 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1908 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
David Brazdil0f672f62019-12-10 10:32:29 +00001909 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001910 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1911
1912 xprt_init_xid(xprt);
1913
1914 xprt->xprt_net = get_net(net);
1915}
1916
1917/**
1918 * xprt_create_transport - create an RPC transport
1919 * @args: rpc transport creation arguments
1920 *
1921 */
1922struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1923{
1924 struct rpc_xprt *xprt;
1925 struct xprt_class *t;
1926
1927 spin_lock(&xprt_list_lock);
1928 list_for_each_entry(t, &xprt_list, list) {
1929 if (t->ident == args->ident) {
1930 spin_unlock(&xprt_list_lock);
1931 goto found;
1932 }
1933 }
1934 spin_unlock(&xprt_list_lock);
1935 dprintk("RPC: transport (%d) not supported\n", args->ident);
1936 return ERR_PTR(-EIO);
1937
1938found:
1939 xprt = t->setup(args);
1940 if (IS_ERR(xprt)) {
1941 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1942 -PTR_ERR(xprt));
1943 goto out;
1944 }
1945 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1946 xprt->idle_timeout = 0;
1947 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1948 if (xprt_has_timer(xprt))
1949 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
1950 else
1951 timer_setup(&xprt->timer, NULL, 0);
1952
1953 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1954 xprt_destroy(xprt);
1955 return ERR_PTR(-EINVAL);
1956 }
1957 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1958 if (xprt->servername == NULL) {
1959 xprt_destroy(xprt);
1960 return ERR_PTR(-ENOMEM);
1961 }
1962
1963 rpc_xprt_debugfs_register(xprt);
1964
1965 dprintk("RPC: created transport %p with %u slots\n", xprt,
1966 xprt->max_reqs);
1967out:
1968 return xprt;
1969}
1970
1971static void xprt_destroy_cb(struct work_struct *work)
1972{
1973 struct rpc_xprt *xprt =
1974 container_of(work, struct rpc_xprt, task_cleanup);
1975
1976 rpc_xprt_debugfs_unregister(xprt);
1977 rpc_destroy_wait_queue(&xprt->binding);
1978 rpc_destroy_wait_queue(&xprt->pending);
1979 rpc_destroy_wait_queue(&xprt->sending);
1980 rpc_destroy_wait_queue(&xprt->backlog);
1981 kfree(xprt->servername);
1982 /*
David Brazdil0f672f62019-12-10 10:32:29 +00001983 * Destroy any existing back channel
1984 */
1985 xprt_destroy_backchannel(xprt, UINT_MAX);
1986
1987 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001988 * Tear down transport state and free the rpc_xprt
1989 */
1990 xprt->ops->destroy(xprt);
1991}
1992
1993/**
1994 * xprt_destroy - destroy an RPC transport, killing off all requests.
1995 * @xprt: transport to destroy
1996 *
1997 */
1998static void xprt_destroy(struct rpc_xprt *xprt)
1999{
2000 dprintk("RPC: destroying transport %p\n", xprt);
2001
2002 /*
2003 * Exclude transport connect/disconnect handlers and autoclose
2004 */
2005 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
2006
2007 del_timer_sync(&xprt->timer);
2008
2009 /*
2010 * Destroy sockets etc from the system workqueue so they can
2011 * safely flush receive work running on rpciod.
2012 */
2013 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
2014 schedule_work(&xprt->task_cleanup);
2015}
2016
2017static void xprt_destroy_kref(struct kref *kref)
2018{
2019 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
2020}
2021
2022/**
2023 * xprt_get - return a reference to an RPC transport.
2024 * @xprt: pointer to the transport
2025 *
2026 */
2027struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
2028{
2029 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
2030 return xprt;
2031 return NULL;
2032}
2033EXPORT_SYMBOL_GPL(xprt_get);
2034
2035/**
2036 * xprt_put - release a reference to an RPC transport.
2037 * @xprt: pointer to the transport
2038 *
2039 */
2040void xprt_put(struct rpc_xprt *xprt)
2041{
2042 if (xprt != NULL)
2043 kref_put(&xprt->kref, xprt_destroy_kref);
2044}
2045EXPORT_SYMBOL_GPL(xprt_put);