blob: 053de053a0248c12516775c0e19292d119bcacd9 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Chuck Lever55aa4f52005-08-11 16:25:47 -040014 * the request struct, and calls xprt_transmit().
15 * - xprt_transmit sends the message and installs the caller on the
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040016 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * - When a packet arrives, the data_ready handler walks the list of
Chuck Lever55aa4f52005-08-11 16:25:47 -040020 * pending requests for that transport. If a matching XID is found, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * 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>
Chuck Lever55aa4f52005-08-11 16:25:47 -040037 *
38 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Chuck Levera246b012005-08-11 16:25:23 -040041#include <linux/module.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/types.h>
Chuck Levera246b012005-08-11 16:25:23 -040044#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/workqueue.h>
Chuck Leverbf3fcf82006-05-25 01:40:51 -040046#include <linux/net.h>
Chuck Leverff839972010-05-07 13:34:47 -040047#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Chuck Levera246b012005-08-11 16:25:23 -040049#include <linux/sunrpc/clnt.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050050#include <linux/sunrpc/metrics.h>
Trond Myklebustc9acb422010-03-19 15:36:22 -040051#include <linux/sunrpc/bc_xprt.h>
Trond Myklebustfda1bfe2015-02-14 17:48:49 -050052#include <linux/rcupdate.h>
Trond Myklebusta1231fd2019-02-18 10:02:29 -050053#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jeff Layton3705ad62014-10-28 14:24:13 -040055#include <trace/events/sunrpc.h>
56
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040057#include "sunrpc.h"
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * Local variables
61 */
62
Jeff Laytonf895b252014-11-17 16:58:04 -050063#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064# define RPCDBG_FACILITY RPCDBG_XPRT
65#endif
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Local functions
69 */
Trond Myklebust21de0a92011-07-17 16:57:32 -040070static void xprt_init(struct rpc_xprt *xprt, struct net *net);
Chuck Lever37ac86c2018-05-04 15:34:53 -040071static __be32 xprt_alloc_xid(struct rpc_xprt *xprt);
Trond Myklebust4e0038b2012-03-01 17:01:05 -050072static void xprt_destroy(struct rpc_xprt *xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Jiri Slaby5ba03e82007-11-22 19:40:22 +080074static DEFINE_SPINLOCK(xprt_list_lock);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040075static LIST_HEAD(xprt_list);
76
Trond Myklebust9e910bf2019-04-07 13:58:53 -040077static 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
Chuck Lever12a80462005-08-25 16:25:51 -070086/**
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040087 * 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 */
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400107 if (t->ident == transport->ident)
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400108 goto out;
109 }
110
Denis V. Lunevc9f6cde2008-07-31 09:53:56 +0400111 list_add_tail(&transport->list, &xprt_list);
112 printk(KERN_INFO "RPC: Registered %s transport module.\n",
113 transport->name);
114 result = 0;
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400115
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
Randy Dunlap65b6e422008-02-13 15:03:23 -0800124 * @transport: transport to unregister
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400125 *
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);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400143 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
154/**
Tom Talpey441e3e22009-03-11 14:37:56 -0400155 * xprt_load_transport - load a transport implementation
156 * @transport_name: transport to load
157 *
158 * Returns:
159 * 0: transport successfully loaded
160 * -ENOENT: transport module not available
161 */
162int xprt_load_transport(const char *transport_name)
163{
164 struct xprt_class *t;
Tom Talpey441e3e22009-03-11 14:37:56 -0400165 int result;
166
167 result = 0;
168 spin_lock(&xprt_list_lock);
169 list_for_each_entry(t, &xprt_list, list) {
170 if (strcmp(t->name, transport_name) == 0) {
171 spin_unlock(&xprt_list_lock);
172 goto out;
173 }
174 }
175 spin_unlock(&xprt_list_lock);
Alex Riesenef7ffe82010-05-24 14:33:05 -0700176 result = request_module("xprt%s", transport_name);
Tom Talpey441e3e22009-03-11 14:37:56 -0400177out:
178 return result;
179}
180EXPORT_SYMBOL_GPL(xprt_load_transport);
181
Trond Myklebustc5445772018-09-03 23:39:27 -0400182static void xprt_clear_locked(struct rpc_xprt *xprt)
183{
184 xprt->snd_task = NULL;
185 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
186 smp_mb__before_atomic();
187 clear_bit(XPRT_LOCKED, &xprt->state);
188 smp_mb__after_atomic();
189 } else
190 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
191}
192
Tom Talpey441e3e22009-03-11 14:37:56 -0400193/**
Chuck Lever12a80462005-08-25 16:25:51 -0700194 * xprt_reserve_xprt - serialize write access to transports
195 * @task: task that is requesting access to the transport
Randy Dunlap177c27b2011-07-28 06:54:36 +0000196 * @xprt: pointer to the target transport
Chuck Lever12a80462005-08-25 16:25:51 -0700197 *
198 * This prevents mixing the payload of separate requests, and prevents
199 * transport connects from colliding with writes. No congestion control
200 * is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400202int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Chuck Lever12a80462005-08-25 16:25:51 -0700204 struct rpc_rqst *req = task->tk_rqstp;
205
206 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
207 if (task == xprt->snd_task)
Chuck Leverbf7ca702019-10-09 12:58:14 -0400208 goto out_locked;
Chuck Lever12a80462005-08-25 16:25:51 -0700209 goto out_sleep;
210 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400211 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
212 goto out_unlock;
Chuck Lever12a80462005-08-25 16:25:51 -0700213 xprt->snd_task = task;
j223yang@asset.uwaterloo.ca4d4a76f2011-03-10 12:40:28 -0500214
Chuck Leverbf7ca702019-10-09 12:58:14 -0400215out_locked:
216 trace_xprt_reserve_xprt(xprt, task);
Chuck Lever12a80462005-08-25 16:25:51 -0700217 return 1;
218
Trond Myklebustc5445772018-09-03 23:39:27 -0400219out_unlock:
220 xprt_clear_locked(xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700221out_sleep:
Chuck Lever12a80462005-08-25 16:25:51 -0700222 task->tk_status = -EAGAIN;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400223 if (RPC_IS_SOFT(task))
224 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400225 xprt_request_timeout(req));
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400226 else
227 rpc_sleep_on(&xprt->sending, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700228 return 0;
229}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400230EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700231
Trond Myklebust75891f52018-09-03 17:37:36 -0400232static bool
233xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
234{
235 return test_bit(XPRT_CWND_WAIT, &xprt->state);
236}
237
238static void
239xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
240{
241 if (!list_empty(&xprt->xmit_queue)) {
242 /* Peek at head of queue to see if it can make progress */
243 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
244 rq_xmit)->rq_cong)
245 return;
246 }
247 set_bit(XPRT_CWND_WAIT, &xprt->state);
248}
249
250static void
251xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
252{
253 if (!RPCXPRT_CONGESTED(xprt))
254 clear_bit(XPRT_CWND_WAIT, &xprt->state);
255}
256
Chuck Lever12a80462005-08-25 16:25:51 -0700257/*
258 * xprt_reserve_xprt_cong - serialize write access to transports
259 * @task: task that is requesting access to the transport
260 *
261 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
262 * integrated into the decision of whether a request is allowed to be
263 * woken up and given access to the transport.
Trond Myklebust75891f52018-09-03 17:37:36 -0400264 * Note that the lock is only granted if we know there are free slots.
Chuck Lever12a80462005-08-25 16:25:51 -0700265 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400266int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever12a80462005-08-25 16:25:51 -0700267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct rpc_rqst *req = task->tk_rqstp;
269
Chuck Lever2226feb2005-08-11 16:25:38 -0400270 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (task == xprt->snd_task)
Chuck Leverbf7ca702019-10-09 12:58:14 -0400272 goto out_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 goto out_sleep;
274 }
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400275 if (req == NULL) {
276 xprt->snd_task = task;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400277 goto out_locked;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400278 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400279 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
280 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400281 if (!xprt_need_congestion_window_wait(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 xprt->snd_task = task;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400283 goto out_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400285out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100286 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287out_sleep:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 task->tk_status = -EAGAIN;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400289 if (RPC_IS_SOFT(task))
290 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400291 xprt_request_timeout(req));
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400292 else
293 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400295out_locked:
296 trace_xprt_reserve_cong(xprt, task);
297 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400299EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Chuck Lever12a80462005-08-25 16:25:51 -0700301static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 int retval;
304
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400305 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
306 return 1;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400307 spin_lock(&xprt->transport_lock);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400308 retval = xprt->ops->reserve_xprt(xprt, task);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400309 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return retval;
311}
312
Trond Myklebust961a8282012-01-17 22:57:37 -0500313static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Trond Myklebust961a8282012-01-17 22:57:37 -0500315 struct rpc_xprt *xprt = data;
Chuck Lever49e9a892005-08-25 16:25:51 -0700316
Chuck Lever49e9a892005-08-25 16:25:51 -0700317 xprt->snd_task = task;
Trond Myklebust961a8282012-01-17 22:57:37 -0500318 return true;
319}
Chuck Lever49e9a892005-08-25 16:25:51 -0700320
Trond Myklebust961a8282012-01-17 22:57:37 -0500321static void __xprt_lock_write_next(struct rpc_xprt *xprt)
322{
323 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
324 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400325 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
326 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400327 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
328 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500329 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400330out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100331 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700332}
333
Trond Myklebust961a8282012-01-17 22:57:37 -0500334static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
335{
336 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
337 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400338 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
339 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400340 if (xprt_need_congestion_window_wait(xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500341 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400342 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
Trond Myklebust75891f52018-09-03 17:37:36 -0400343 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100346 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Chuck Lever49e9a892005-08-25 16:25:51 -0700349/**
350 * xprt_release_xprt - allow other requests to use a transport
351 * @xprt: transport with other tasks potentially waiting
352 * @task: task that is releasing access to the transport
353 *
354 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700356void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100359 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 __xprt_lock_write_next(xprt);
361 }
Chuck Leverbf7ca702019-10-09 12:58:14 -0400362 trace_xprt_release_xprt(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400364EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Chuck Lever49e9a892005-08-25 16:25:51 -0700366/**
367 * xprt_release_xprt_cong - allow other requests to use a transport
368 * @xprt: transport with other tasks potentially waiting
369 * @task: task that is releasing access to the transport
370 *
371 * Note that "task" can be NULL. Another task is awoken to use the
372 * transport if the transport's congestion window allows it.
373 */
374void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
375{
376 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100377 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700378 __xprt_lock_write_next_cong(xprt);
379 }
Chuck Leverbf7ca702019-10-09 12:58:14 -0400380 trace_xprt_release_cong(xprt, task);
Chuck Lever49e9a892005-08-25 16:25:51 -0700381}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400382EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700383
384static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400386 if (xprt->snd_task != task)
387 return;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400388 spin_lock(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700389 xprt->ops->release_xprt(xprt, task);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400390 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * Van Jacobson congestion avoidance. Check if the congestion window
395 * overflowed. Put the task to sleep if this is the case.
396 */
397static int
Trond Myklebust75891f52018-09-03 17:37:36 -0400398__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (req->rq_cong)
401 return 1;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400402 trace_xprt_get_cong(xprt, req->rq_task);
Trond Myklebust75891f52018-09-03 17:37:36 -0400403 if (RPCXPRT_CONGESTED(xprt)) {
404 xprt_set_congestion_window_wait(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
Trond Myklebust75891f52018-09-03 17:37:36 -0400406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 req->rq_cong = 1;
408 xprt->cong += RPC_CWNDSCALE;
409 return 1;
410}
411
412/*
413 * Adjust the congestion window, and wake up the next task
414 * that has been sleeping due to congestion
415 */
416static void
417__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
418{
419 if (!req->rq_cong)
420 return;
421 req->rq_cong = 0;
422 xprt->cong -= RPC_CWNDSCALE;
Trond Myklebust75891f52018-09-03 17:37:36 -0400423 xprt_test_and_clear_congestion_window_wait(xprt);
Chuck Leverbf7ca702019-10-09 12:58:14 -0400424 trace_xprt_put_cong(xprt, req->rq_task);
Chuck Lever49e9a892005-08-25 16:25:51 -0700425 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Chuck Lever46c0ee82005-08-25 16:25:52 -0700428/**
Trond Myklebust75891f52018-09-03 17:37:36 -0400429 * xprt_request_get_cong - Request congestion control credits
430 * @xprt: pointer to transport
431 * @req: pointer to RPC request
432 *
433 * Useful for transports that require congestion control.
434 */
435bool
436xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
437{
438 bool ret = false;
439
440 if (req->rq_cong)
441 return true;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400442 spin_lock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400443 ret = __xprt_get_cong(xprt, req) != 0;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400444 spin_unlock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400445 return ret;
446}
447EXPORT_SYMBOL_GPL(xprt_request_get_cong);
448
449/**
Chuck Levera58dd392005-08-25 16:25:53 -0700450 * xprt_release_rqst_cong - housekeeping when request is complete
451 * @task: RPC request that recently completed
452 *
453 * Useful for transports that require congestion control.
454 */
455void xprt_release_rqst_cong(struct rpc_task *task)
456{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500457 struct rpc_rqst *req = task->tk_rqstp;
458
459 __xprt_put_cong(req->rq_xprt, req);
Chuck Levera58dd392005-08-25 16:25:53 -0700460}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400461EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700462
Chuck Lever8593e012019-09-13 16:01:07 -0400463static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
464{
465 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
466 __xprt_lock_write_next_cong(xprt);
467}
468
Trond Myklebust75891f52018-09-03 17:37:36 -0400469/*
470 * Clear the congestion window wait flag and wake up the next
471 * entry on xprt->sending
472 */
473static void
474xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
475{
476 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
Trond Myklebustb5e92412019-05-02 11:21:08 -0400477 spin_lock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400478 __xprt_lock_write_next_cong(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400479 spin_unlock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400480 }
481}
482
Chuck Levera58dd392005-08-25 16:25:53 -0700483/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700484 * xprt_adjust_cwnd - adjust transport congestion window
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500485 * @xprt: pointer to xprt
Chuck Lever46c0ee82005-08-25 16:25:52 -0700486 * @task: recently completed RPC request used to adjust window
487 * @result: result code of completed RPC request
488 *
Chuck Lever4f4cf5a2014-05-28 10:34:49 -0400489 * The transport code maintains an estimate on the maximum number of out-
490 * standing RPC requests, using a smoothed version of the congestion
491 * avoidance implemented in 44BSD. This is basically the Van Jacobson
492 * congestion algorithm: If a retransmit occurs, the congestion window is
493 * halved; otherwise, it is incremented by 1/cwnd when
494 *
495 * - a reply is received and
496 * - a full number of requests are outstanding and
497 * - the congestion window hasn't been updated recently.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500499void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700501 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700502 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (result >= 0 && cwnd <= xprt->cong) {
505 /* The (cwnd >> 1) term makes sure
506 * the result gets rounded properly. */
507 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
508 if (cwnd > RPC_MAXCWND(xprt))
509 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700510 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else if (result == -ETIMEDOUT) {
512 cwnd >>= 1;
513 if (cwnd < RPC_CWNDSCALE)
514 cwnd = RPC_CWNDSCALE;
515 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500516 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 xprt->cong, xprt->cwnd, cwnd);
518 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700519 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400521EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Chuck Lever44fbac22005-08-11 16:25:44 -0400523/**
524 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
525 * @xprt: transport with waiting tasks
526 * @status: result code to plant in each task before waking it
527 *
528 */
529void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
530{
531 if (status < 0)
532 rpc_wake_up_status(&xprt->pending, status);
533 else
534 rpc_wake_up(&xprt->pending);
535}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400536EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400537
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400538/**
539 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
Trond Myklebustc5445772018-09-03 23:39:27 -0400540 * @xprt: transport
Trond Myklebusta9a6b522013-02-22 14:57:57 -0500541 *
542 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
543 * we don't in general want to force a socket disconnection due to
544 * an incomplete RPC call transmission.
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400545 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400546void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400547{
Trond Myklebustc5445772018-09-03 23:39:27 -0400548 set_bit(XPRT_WRITE_SPACE, &xprt->state);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400549}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400550EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400551
Trond Myklebustc5445772018-09-03 23:39:27 -0400552static bool
553xprt_clear_write_space_locked(struct rpc_xprt *xprt)
554{
555 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
556 __xprt_lock_write_next(xprt);
557 dprintk("RPC: write space: waking waiting task on "
558 "xprt %p\n", xprt);
559 return true;
560 }
561 return false;
562}
563
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400564/**
565 * xprt_write_space - wake the task waiting for transport output buffer space
566 * @xprt: transport with waiting tasks
567 *
568 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
569 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400570bool xprt_write_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400571{
Trond Myklebustc5445772018-09-03 23:39:27 -0400572 bool ret;
573
574 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
575 return false;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400576 spin_lock(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400577 ret = xprt_clear_write_space_locked(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400578 spin_unlock(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400579 return ret;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400580}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400581EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400582
Trond Myklebustda953062019-04-07 13:58:56 -0400583static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
584{
585 s64 delta = ktime_to_ns(ktime_get() - abstime);
586 return likely(delta >= 0) ?
587 jiffies - nsecs_to_jiffies(delta) :
588 jiffies + nsecs_to_jiffies(-delta);
589}
590
591static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Trond Myklebustba7392b2007-12-20 16:03:55 -0500593 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Trond Myklebustda953062019-04-07 13:58:56 -0400594 unsigned long majortimeo = req->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (to->to_exponential)
Trond Myklebustda953062019-04-07 13:58:56 -0400597 majortimeo <<= to->to_retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 else
Trond Myklebustda953062019-04-07 13:58:56 -0400599 majortimeo += to->to_increment * to->to_retries;
600 if (majortimeo > to->to_maxval || majortimeo == 0)
601 majortimeo = to->to_maxval;
602 return majortimeo;
603}
604
605static void xprt_reset_majortimeo(struct rpc_rqst *req)
606{
607 req->rq_majortimeo += xprt_calc_majortimeo(req);
608}
609
610static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
611{
612 unsigned long time_init;
613 struct rpc_xprt *xprt = req->rq_xprt;
614
615 if (likely(xprt && xprt_connected(xprt)))
616 time_init = jiffies;
617 else
618 time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
619 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
620 req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Chuck Lever9903cd12005-08-11 16:25:26 -0400623/**
624 * xprt_adjust_timeout - adjust timeout values for next retransmit
625 * @req: RPC request containing parameters to use for the adjustment
626 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
628int xprt_adjust_timeout(struct rpc_rqst *req)
629{
630 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500631 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int status = 0;
633
634 if (time_before(jiffies, req->rq_majortimeo)) {
635 if (to->to_exponential)
636 req->rq_timeout <<= 1;
637 else
638 req->rq_timeout += to->to_increment;
639 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
640 req->rq_timeout = to->to_maxval;
641 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 } else {
643 req->rq_timeout = to->to_initval;
644 req->rq_retries = 0;
645 xprt_reset_majortimeo(req);
646 /* Reset the RTT counters == "slow start" */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400647 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400649 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 status = -ETIMEDOUT;
651 }
652
653 if (req->rq_timeout == 0) {
654 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
655 req->rq_timeout = 5 * HZ;
656 }
657 return status;
658}
659
David Howells65f27f32006-11-22 14:55:48 +0000660static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
David Howells65f27f32006-11-22 14:55:48 +0000662 struct rpc_xprt *xprt =
663 container_of(work, struct rpc_xprt, task_cleanup);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500664 unsigned int pflags = memalloc_nofs_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Trond Myklebust66af1e552007-11-06 10:18:36 -0500666 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust4876cc72015-06-19 16:17:57 -0400667 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 xprt_release_write(xprt, NULL);
Trond Myklebust79234c32015-09-18 15:53:24 -0400669 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500670 memalloc_nofs_restore(pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Chuck Lever9903cd12005-08-11 16:25:26 -0400673/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500674 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400675 * @xprt: transport to flag for disconnect
676 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500678void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Chuck Lever46121cf2007-01-31 12:14:08 -0500680 dprintk("RPC: disconnected transport %p\n", xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400681 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 xprt_clear_connected(xprt);
Trond Myklebustc5445772018-09-03 23:39:27 -0400683 xprt_clear_write_space_locked(xprt);
Chuck Lever8593e012019-09-13 16:01:07 -0400684 xprt_clear_congestion_window_wait_locked(xprt);
Trond Myklebust27adc782019-03-15 08:01:16 -0400685 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400686 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500688EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Trond Myklebust66af1e552007-11-06 10:18:36 -0500690/**
691 * xprt_force_disconnect - force a transport to disconnect
692 * @xprt: transport to disconnect
693 *
694 */
695void xprt_force_disconnect(struct rpc_xprt *xprt)
696{
697 /* Don't race with the test_bit() in xprt_clear_locked() */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400698 spin_lock(&xprt->transport_lock);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500699 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
700 /* Try to schedule an autoclose RPC call */
701 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400702 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust0445f922018-12-17 13:34:59 -0500703 else if (xprt->snd_task)
704 rpc_wake_up_queued_task_set_status(&xprt->pending,
705 xprt->snd_task, -ENOTCONN);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400706 spin_unlock(&xprt->transport_lock);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500707}
Chuck Levere2a4f4f2017-04-11 13:22:38 -0400708EXPORT_SYMBOL_GPL(xprt_force_disconnect);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500709
Trond Myklebust7f3a1d12018-08-23 00:03:43 -0400710static unsigned int
711xprt_connect_cookie(struct rpc_xprt *xprt)
712{
713 return READ_ONCE(xprt->connect_cookie);
714}
715
716static bool
717xprt_request_retransmit_after_disconnect(struct rpc_task *task)
718{
719 struct rpc_rqst *req = task->tk_rqstp;
720 struct rpc_xprt *xprt = req->rq_xprt;
721
722 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
723 !xprt_connected(xprt);
724}
725
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400726/**
727 * xprt_conditional_disconnect - force a transport to disconnect
728 * @xprt: transport to disconnect
729 * @cookie: 'connection cookie'
730 *
731 * This attempts to break the connection if and only if 'cookie' matches
732 * the current transport 'connection cookie'. It ensures that we don't
733 * try to break the connection more than once when we need to retransmit
734 * a batch of RPC requests.
735 *
736 */
737void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
738{
739 /* Don't race with the test_bit() in xprt_clear_locked() */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400740 spin_lock(&xprt->transport_lock);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400741 if (cookie != xprt->connect_cookie)
742 goto out;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100743 if (test_bit(XPRT_CLOSING, &xprt->state))
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400744 goto out;
745 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
746 /* Try to schedule an autoclose RPC call */
747 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400748 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400749 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400750out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400751 spin_unlock(&xprt->transport_lock);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400752}
753
Trond Myklebustad3331a2016-08-02 13:47:43 -0400754static bool
755xprt_has_timer(const struct rpc_xprt *xprt)
756{
757 return xprt->idle_timeout != 0;
758}
759
760static void
761xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
762 __must_hold(&xprt->transport_lock)
763{
Dave Wysochanski80d3c452019-06-26 16:30:24 -0400764 xprt->last_used = jiffies;
Trond Myklebust95f76912018-09-07 08:35:22 -0400765 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
Trond Myklebustad3331a2016-08-02 13:47:43 -0400766 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static void
Kees Cookff861c42017-10-16 17:29:42 -0700770xprt_init_autodisconnect(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Kees Cookff861c42017-10-16 17:29:42 -0700772 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Trond Myklebust95f76912018-09-07 08:35:22 -0400774 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
Trond Myklebustb5e92412019-05-02 11:21:08 -0400775 return;
Trond Myklebustad3331a2016-08-02 13:47:43 -0400776 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
777 xprt->last_used = jiffies;
Chuck Lever2226feb2005-08-11 16:25:38 -0400778 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Trond Myklebustb5e92412019-05-02 11:21:08 -0400779 return;
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400780 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500783bool xprt_lock_connect(struct rpc_xprt *xprt,
784 struct rpc_task *task,
785 void *cookie)
786{
787 bool ret = false;
788
Trond Myklebustb5e92412019-05-02 11:21:08 -0400789 spin_lock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500790 if (!test_bit(XPRT_LOCKED, &xprt->state))
791 goto out;
792 if (xprt->snd_task != task)
793 goto out;
794 xprt->snd_task = cookie;
795 ret = true;
796out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400797 spin_unlock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500798 return ret;
799}
800
801void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
802{
Trond Myklebustb5e92412019-05-02 11:21:08 -0400803 spin_lock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500804 if (xprt->snd_task != cookie)
805 goto out;
806 if (!test_bit(XPRT_LOCKED, &xprt->state))
807 goto out;
808 xprt->snd_task =NULL;
809 xprt->ops->release_xprt(xprt, NULL);
Trond Myklebustad3331a2016-08-02 13:47:43 -0400810 xprt_schedule_autodisconnect(xprt);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500811out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400812 spin_unlock(&xprt->transport_lock);
Trond Myklebust79234c32015-09-18 15:53:24 -0400813 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500814}
815
Chuck Lever9903cd12005-08-11 16:25:26 -0400816/**
817 * xprt_connect - schedule a transport connect operation
818 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
820 */
821void xprt_connect(struct rpc_task *task)
822{
Trond Myklebustad2368d2013-01-08 10:08:33 -0500823 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Chuck Lever46121cf2007-01-31 12:14:08 -0500825 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 xprt, (xprt_connected(xprt) ? "is" : "is not"));
827
Chuck Leverec739ef2006-08-22 20:06:15 -0400828 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400829 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return;
831 }
832 if (!xprt_lock_write(xprt, task))
833 return;
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500834
835 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
836 xprt->ops->close(xprt);
837
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500838 if (!xprt_connected(xprt)) {
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100839 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400840 rpc_sleep_on_timeout(&xprt->pending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400841 xprt_request_timeout(task->tk_rqstp));
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400842
843 if (test_bit(XPRT_CLOSING, &xprt->state))
844 return;
845 if (xprt_test_and_set_connecting(xprt))
846 return;
Trond Myklebust0a9a4302018-12-01 23:18:00 -0500847 /* Race breaker */
848 if (!xprt_connected(xprt)) {
849 xprt->stat.connect_start = jiffies;
850 xprt->ops->connect(xprt, task);
851 } else {
852 xprt_clear_connecting(xprt);
853 task->tk_status = 0;
854 rpc_wake_up_queued_task(&xprt->pending, task);
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500857 xprt_release_write(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Chuck Lever675dd902019-06-19 10:33:42 -0400860/**
861 * xprt_reconnect_delay - compute the wait before scheduling a connect
862 * @xprt: transport instance
863 *
864 */
865unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
866{
867 unsigned long start, now = jiffies;
868
869 start = xprt->stat.connect_start + xprt->reestablish_timeout;
870 if (time_after(start, now))
871 return start - now;
872 return 0;
873}
874EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
875
876/**
877 * xprt_reconnect_backoff - compute the new re-establish timeout
878 * @xprt: transport instance
879 * @init_to: initial reestablish timeout
880 *
881 */
882void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
883{
884 xprt->reestablish_timeout <<= 1;
885 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
886 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
887 if (xprt->reestablish_timeout < init_to)
888 xprt->reestablish_timeout = init_to;
889}
890EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
891
Trond Myklebust95f76912018-09-07 08:35:22 -0400892enum xprt_xid_rb_cmp {
893 XID_RB_EQUAL,
894 XID_RB_LEFT,
895 XID_RB_RIGHT,
896};
897static enum xprt_xid_rb_cmp
898xprt_xid_cmp(__be32 xid1, __be32 xid2)
899{
900 if (xid1 == xid2)
901 return XID_RB_EQUAL;
902 if ((__force u32)xid1 < (__force u32)xid2)
903 return XID_RB_LEFT;
904 return XID_RB_RIGHT;
905}
906
907static struct rpc_rqst *
908xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
909{
910 struct rb_node *n = xprt->recv_queue.rb_node;
911 struct rpc_rqst *req;
912
913 while (n != NULL) {
914 req = rb_entry(n, struct rpc_rqst, rq_recv);
915 switch (xprt_xid_cmp(xid, req->rq_xid)) {
916 case XID_RB_LEFT:
917 n = n->rb_left;
918 break;
919 case XID_RB_RIGHT:
920 n = n->rb_right;
921 break;
922 case XID_RB_EQUAL:
923 return req;
924 }
925 }
926 return NULL;
927}
928
929static void
930xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
931{
932 struct rb_node **p = &xprt->recv_queue.rb_node;
933 struct rb_node *n = NULL;
934 struct rpc_rqst *req;
935
936 while (*p != NULL) {
937 n = *p;
938 req = rb_entry(n, struct rpc_rqst, rq_recv);
939 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
940 case XID_RB_LEFT:
941 p = &n->rb_left;
942 break;
943 case XID_RB_RIGHT:
944 p = &n->rb_right;
945 break;
946 case XID_RB_EQUAL:
947 WARN_ON_ONCE(new != req);
948 return;
949 }
950 }
951 rb_link_node(&new->rq_recv, n, p);
952 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
953}
954
955static void
956xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
957{
958 rb_erase(&req->rq_recv, &xprt->recv_queue);
959}
960
Chuck Lever9903cd12005-08-11 16:25:26 -0400961/**
962 * xprt_lookup_rqst - find an RPC request corresponding to an XID
963 * @xprt: transport on which the original request was transmitted
964 * @xid: RPC XID of incoming reply
965 *
Trond Myklebust75c84152018-08-31 10:21:00 -0400966 * Caller holds xprt->queue_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700968struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +0400970 struct rpc_rqst *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Trond Myklebust95f76912018-09-07 08:35:22 -0400972 entry = xprt_request_rb_find(xprt, xid);
973 if (entry != NULL) {
974 trace_xprt_lookup_rqst(xprt, xid, 0);
975 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
976 return entry;
977 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500978
979 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
980 ntohl(xid));
Jeff Layton3705ad62014-10-28 14:24:13 -0400981 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
Chuck Lever262ca072006-03-20 13:44:16 -0500982 xprt->stat.bad_xids++;
983 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400985EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400987static bool
988xprt_is_pinned_rqst(struct rpc_rqst *req)
989{
990 return atomic_read(&req->rq_pin) != 0;
991}
992
Trond Myklebust729749b2017-08-13 10:03:59 -0400993/**
994 * xprt_pin_rqst - Pin a request on the transport receive list
995 * @req: Request to pin
996 *
997 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
Chuck Lever1f7d1c72019-04-24 09:40:09 -0400998 * so should be holding xprt->queue_lock.
Trond Myklebust729749b2017-08-13 10:03:59 -0400999 */
1000void xprt_pin_rqst(struct rpc_rqst *req)
1001{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001002 atomic_inc(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -04001003}
Chuck Lever9590d082017-08-23 17:05:58 -04001004EXPORT_SYMBOL_GPL(xprt_pin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -04001005
1006/**
1007 * xprt_unpin_rqst - Unpin a request on the transport receive list
1008 * @req: Request to pin
1009 *
Chuck Lever1f7d1c72019-04-24 09:40:09 -04001010 * Caller should be holding xprt->queue_lock.
Trond Myklebust729749b2017-08-13 10:03:59 -04001011 */
1012void xprt_unpin_rqst(struct rpc_rqst *req)
1013{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001014 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
1015 atomic_dec(&req->rq_pin);
1016 return;
1017 }
1018 if (atomic_dec_and_test(&req->rq_pin))
1019 wake_up_var(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -04001020}
Chuck Lever9590d082017-08-23 17:05:58 -04001021EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -04001022
1023static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
Trond Myklebust729749b2017-08-13 10:03:59 -04001024{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001025 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
Trond Myklebust729749b2017-08-13 10:03:59 -04001026}
1027
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001028static bool
1029xprt_request_data_received(struct rpc_task *task)
1030{
1031 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1032 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1033}
1034
1035static bool
1036xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1037{
1038 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1039 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1040}
1041
1042/**
1043 * xprt_request_enqueue_receive - Add an request to the receive queue
1044 * @task: RPC task
1045 *
1046 */
1047void
1048xprt_request_enqueue_receive(struct rpc_task *task)
1049{
1050 struct rpc_rqst *req = task->tk_rqstp;
1051 struct rpc_xprt *xprt = req->rq_xprt;
1052
1053 if (!xprt_request_need_enqueue_receive(task, req))
1054 return;
Trond Myklebust75369082019-07-17 21:22:38 -04001055
1056 xprt_request_prepare(task->tk_rqstp);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001057 spin_lock(&xprt->queue_lock);
1058
1059 /* Update the softirq receive buffer */
1060 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1061 sizeof(req->rq_private_buf));
1062
1063 /* Add request to the receive list */
Trond Myklebust95f76912018-09-07 08:35:22 -04001064 xprt_request_rb_insert(xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001065 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1066 spin_unlock(&xprt->queue_lock);
1067
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001068 /* Turn off autodisconnect */
1069 del_singleshot_timer_sync(&xprt->timer);
1070}
1071
1072/**
1073 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1074 * @task: RPC task
1075 *
1076 * Caller must hold xprt->queue_lock.
1077 */
1078static void
1079xprt_request_dequeue_receive_locked(struct rpc_task *task)
1080{
Trond Myklebust95f76912018-09-07 08:35:22 -04001081 struct rpc_rqst *req = task->tk_rqstp;
1082
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001083 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
Trond Myklebust95f76912018-09-07 08:35:22 -04001084 xprt_request_rb_remove(req->rq_xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001085}
1086
Chuck Leverecd465e2018-03-05 15:12:57 -05001087/**
1088 * xprt_update_rtt - Update RPC RTT statistics
1089 * @task: RPC request that recently completed
1090 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001091 * Caller holds xprt->queue_lock.
Chuck Leverecd465e2018-03-05 15:12:57 -05001092 */
1093void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Chuck Lever1570c1e2005-08-25 16:25:52 -07001095 struct rpc_rqst *req = task->tk_rqstp;
1096 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
Eric Dumazet95c96172012-04-15 05:58:06 +00001097 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustd60dbb22010-05-13 12:51:49 -04001098 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Chuck Lever1570c1e2005-08-25 16:25:52 -07001100 if (timer) {
1101 if (req->rq_ntrans == 1)
Chuck Leverff839972010-05-07 13:34:47 -04001102 rpc_update_rtt(rtt, timer, m);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001103 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Chuck Lever1570c1e2005-08-25 16:25:52 -07001105}
Chuck Leverecd465e2018-03-05 15:12:57 -05001106EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Chuck Lever1570c1e2005-08-25 16:25:52 -07001108/**
1109 * xprt_complete_rqst - called when reply processing is complete
1110 * @task: RPC request that recently completed
1111 * @copied: actual number of bytes received from the transport
1112 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001113 * Caller holds xprt->queue_lock.
Chuck Lever1570c1e2005-08-25 16:25:52 -07001114 */
1115void xprt_complete_rqst(struct rpc_task *task, int copied)
1116{
1117 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -05001118 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Jeff Layton3705ad62014-10-28 14:24:13 -04001120 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Trond Myklebustfda13932008-02-22 16:34:12 -05001122 xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -05001123
Trond Myklebust1e799b62008-03-21 16:19:41 -04001124 req->rq_private_buf.len = copied;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001125 /* Ensure all writes are done before we update */
1126 /* req->rq_reply_bytes_recvd */
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001127 smp_wmb();
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001128 req->rq_reply_bytes_recvd = copied;
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001129 xprt_request_dequeue_receive_locked(task);
Trond Myklebustfda13932008-02-22 16:34:12 -05001130 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -04001132EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Chuck Lever46c0ee82005-08-25 16:25:52 -07001134static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Chuck Lever46c0ee82005-08-25 16:25:52 -07001136 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 struct rpc_xprt *xprt = req->rq_xprt;
1138
Trond Myklebust5d008372008-02-22 16:34:17 -05001139 if (task->tk_status != -ETIMEDOUT)
1140 return;
Chuck Lever46c0ee82005-08-25 16:25:52 -07001141
Chuck Lever82476d92018-01-03 15:38:25 -05001142 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001143 if (!req->rq_reply_bytes_recvd) {
Chuck Lever46c0ee82005-08-25 16:25:52 -07001144 if (xprt->ops->timer)
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001145 xprt->ops->timer(xprt, task);
Trond Myklebust5d008372008-02-22 16:34:17 -05001146 } else
1147 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Chuck Lever9903cd12005-08-11 16:25:26 -04001150/**
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001151 * xprt_wait_for_reply_request_def - wait for reply
1152 * @task: pointer to rpc_task
1153 *
1154 * Set a request's retransmit timeout based on the transport's
1155 * default timeout parameters. Used by transports that don't adjust
1156 * the retransmit timeout based on round-trip time estimation,
1157 * and put the task to sleep on the pending queue.
1158 */
1159void xprt_wait_for_reply_request_def(struct rpc_task *task)
1160{
1161 struct rpc_rqst *req = task->tk_rqstp;
1162
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001163 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
Trond Myklebust9e910bf2019-04-07 13:58:53 -04001164 xprt_request_timeout(req));
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001165}
1166EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
1167
1168/**
1169 * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
1170 * @task: pointer to rpc_task
1171 *
1172 * Set a request's retransmit timeout using the RTT estimator,
1173 * and put the task to sleep on the pending queue.
1174 */
1175void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
1176{
1177 int timer = task->tk_msg.rpc_proc->p_timer;
1178 struct rpc_clnt *clnt = task->tk_client;
1179 struct rpc_rtt *rtt = clnt->cl_rtt;
1180 struct rpc_rqst *req = task->tk_rqstp;
1181 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001182 unsigned long timeout;
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001183
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001184 timeout = rpc_calc_rto(rtt, timer);
1185 timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
1186 if (timeout > max_timeout || timeout == 0)
1187 timeout = max_timeout;
1188 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1189 jiffies + timeout);
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001190}
1191EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
1192
1193/**
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001194 * xprt_request_wait_receive - wait for the reply to an RPC request
1195 * @task: RPC task about to send a request
1196 *
1197 */
1198void xprt_request_wait_receive(struct rpc_task *task)
1199{
1200 struct rpc_rqst *req = task->tk_rqstp;
1201 struct rpc_xprt *xprt = req->rq_xprt;
1202
1203 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1204 return;
1205 /*
1206 * Sleep on the pending queue if we're expecting a reply.
1207 * The spinlock ensures atomicity between the test of
1208 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1209 */
1210 spin_lock(&xprt->queue_lock);
1211 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001212 xprt->ops->wait_for_reply_request(task);
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001213 /*
1214 * Send an extra queue wakeup call if the
1215 * connection was dropped in case the call to
1216 * rpc_sleep_on() raced.
1217 */
1218 if (xprt_request_retransmit_after_disconnect(task))
1219 rpc_wake_up_queued_task_set_status(&xprt->pending,
1220 task, -ENOTCONN);
1221 }
1222 spin_unlock(&xprt->queue_lock);
1223}
1224
Trond Myklebust944b0422018-08-09 23:33:21 -04001225static bool
Trond Myklebust944b0422018-08-09 23:33:21 -04001226xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1227{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001228 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
Trond Myklebust944b0422018-08-09 23:33:21 -04001229}
1230
1231/**
1232 * xprt_request_enqueue_transmit - queue a task for transmission
1233 * @task: pointer to rpc_task
1234 *
1235 * Add a task to the transmission queue.
1236 */
1237void
1238xprt_request_enqueue_transmit(struct rpc_task *task)
1239{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001240 struct rpc_rqst *pos, *req = task->tk_rqstp;
Trond Myklebust944b0422018-08-09 23:33:21 -04001241 struct rpc_xprt *xprt = req->rq_xprt;
1242
1243 if (xprt_request_need_enqueue_transmit(task, req)) {
Trond Myklebuste66721f2019-01-02 17:53:10 -05001244 req->rq_bytes_sent = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001245 spin_lock(&xprt->queue_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -04001246 /*
1247 * Requests that carry congestion control credits are added
1248 * to the head of the list to avoid starvation issues.
1249 */
1250 if (req->rq_cong) {
1251 xprt_clear_congestion_window_wait(xprt);
1252 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1253 if (pos->rq_cong)
1254 continue;
1255 /* Note: req is added _before_ pos */
1256 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1257 INIT_LIST_HEAD(&req->rq_xmit2);
Chuck Lever0c776682019-02-11 11:25:04 -05001258 trace_xprt_enq_xmit(task, 1);
Trond Myklebust75891f52018-09-03 17:37:36 -04001259 goto out;
1260 }
Trond Myklebust86aeee02018-09-08 14:22:41 -04001261 } else if (RPC_IS_SWAPPER(task)) {
1262 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1263 if (pos->rq_cong || pos->rq_bytes_sent)
1264 continue;
1265 if (RPC_IS_SWAPPER(pos->rq_task))
1266 continue;
1267 /* Note: req is added _before_ pos */
1268 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1269 INIT_LIST_HEAD(&req->rq_xmit2);
Chuck Lever0c776682019-02-11 11:25:04 -05001270 trace_xprt_enq_xmit(task, 2);
Trond Myklebust86aeee02018-09-08 14:22:41 -04001271 goto out;
1272 }
Chuck Leverdeaa5c92019-01-09 10:04:57 -05001273 } else if (!req->rq_seqno) {
Trond Myklebust75891f52018-09-03 17:37:36 -04001274 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1275 if (pos->rq_task->tk_owner != task->tk_owner)
1276 continue;
1277 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1278 INIT_LIST_HEAD(&req->rq_xmit);
Chuck Lever0c776682019-02-11 11:25:04 -05001279 trace_xprt_enq_xmit(task, 3);
Trond Myklebust75891f52018-09-03 17:37:36 -04001280 goto out;
1281 }
Trond Myklebust918f3c12018-09-09 11:37:22 -04001282 }
Trond Myklebust944b0422018-08-09 23:33:21 -04001283 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001284 INIT_LIST_HEAD(&req->rq_xmit2);
Chuck Lever0c776682019-02-11 11:25:04 -05001285 trace_xprt_enq_xmit(task, 4);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001286out:
Trond Myklebust944b0422018-08-09 23:33:21 -04001287 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1288 spin_unlock(&xprt->queue_lock);
1289 }
1290}
1291
1292/**
1293 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1294 * @task: pointer to rpc_task
1295 *
1296 * Remove a task from the transmission queue
1297 * Caller must hold xprt->queue_lock
1298 */
1299static void
1300xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1301{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001302 struct rpc_rqst *req = task->tk_rqstp;
1303
1304 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1305 return;
1306 if (!list_empty(&req->rq_xmit)) {
1307 list_del(&req->rq_xmit);
1308 if (!list_empty(&req->rq_xmit2)) {
1309 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1310 struct rpc_rqst, rq_xmit2);
1311 list_del(&req->rq_xmit2);
1312 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1313 }
1314 } else
1315 list_del(&req->rq_xmit2);
Trond Myklebust944b0422018-08-09 23:33:21 -04001316}
1317
1318/**
1319 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1320 * @task: pointer to rpc_task
1321 *
1322 * Remove a task from the transmission queue
1323 */
1324static void
1325xprt_request_dequeue_transmit(struct rpc_task *task)
1326{
1327 struct rpc_rqst *req = task->tk_rqstp;
1328 struct rpc_xprt *xprt = req->rq_xprt;
1329
1330 spin_lock(&xprt->queue_lock);
1331 xprt_request_dequeue_transmit_locked(task);
1332 spin_unlock(&xprt->queue_lock);
1333}
1334
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001335/**
Trond Myklebustcc204d02019-09-10 13:01:35 -04001336 * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
1337 * @task: pointer to rpc_task
1338 *
1339 * Remove a task from the transmit and receive queues, and ensure that
1340 * it is not pinned by the receive work item.
1341 */
1342void
1343xprt_request_dequeue_xprt(struct rpc_task *task)
1344{
1345 struct rpc_rqst *req = task->tk_rqstp;
1346 struct rpc_xprt *xprt = req->rq_xprt;
1347
1348 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1349 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1350 xprt_is_pinned_rqst(req)) {
1351 spin_lock(&xprt->queue_lock);
1352 xprt_request_dequeue_transmit_locked(task);
1353 xprt_request_dequeue_receive_locked(task);
1354 while (xprt_is_pinned_rqst(req)) {
1355 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1356 spin_unlock(&xprt->queue_lock);
1357 xprt_wait_on_pinned_rqst(req);
1358 spin_lock(&xprt->queue_lock);
1359 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1360 }
1361 spin_unlock(&xprt->queue_lock);
1362 }
1363}
1364
1365/**
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001366 * xprt_request_prepare - prepare an encoded request for transport
1367 * @req: pointer to rpc_rqst
1368 *
1369 * Calls into the transport layer to do whatever is needed to prepare
1370 * the request for transmission or receive.
1371 */
1372void
1373xprt_request_prepare(struct rpc_rqst *req)
1374{
1375 struct rpc_xprt *xprt = req->rq_xprt;
1376
1377 if (xprt->ops->prepare_request)
1378 xprt->ops->prepare_request(req);
1379}
1380
1381/**
Trond Myklebust762e4e62018-08-24 16:28:28 -04001382 * xprt_request_need_retransmit - Test if a task needs retransmission
1383 * @task: pointer to rpc_task
1384 *
1385 * Test for whether a connection breakage requires the task to retransmit
1386 */
1387bool
1388xprt_request_need_retransmit(struct rpc_task *task)
1389{
1390 return xprt_request_retransmit_after_disconnect(task);
1391}
1392
1393/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001394 * xprt_prepare_transmit - reserve the transport before sending a request
1395 * @task: RPC task about to send a request
1396 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 */
Trond Myklebust90051ea2013-09-25 12:17:18 -04001398bool xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 struct rpc_rqst *req = task->tk_rqstp;
1401 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Chuck Lever46121cf2007-01-31 12:14:08 -05001403 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001405 if (!xprt_lock_write(xprt, task)) {
1406 /* Race breaker: someone may have transmitted us */
Trond Myklebust944b0422018-08-09 23:33:21 -04001407 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001408 rpc_wake_up_queued_task_set_status(&xprt->sending,
1409 task, 0);
1410 return false;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001413 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001416void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001417{
Rahul Iyer343952f2009-04-01 09:23:17 -04001418 xprt_release_write(task->tk_rqstp->rq_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001419}
1420
Chuck Lever9903cd12005-08-11 16:25:26 -04001421/**
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001422 * xprt_request_transmit - send an RPC request on a transport
1423 * @req: pointer to request to transmit
1424 * @snd_task: RPC task that owns the transport lock
Chuck Lever9903cd12005-08-11 16:25:26 -04001425 *
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001426 * This performs the transmission of a single request.
1427 * Note that if the request is not the same as snd_task, then it
1428 * does need to be pinned.
1429 * Returns '0' on success.
Chuck Lever9903cd12005-08-11 16:25:26 -04001430 */
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001431static int
1432xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001434 struct rpc_xprt *xprt = req->rq_xprt;
1435 struct rpc_task *task = req->rq_task;
Trond Myklebust90d91b02017-12-14 21:24:08 -05001436 unsigned int connect_cookie;
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001437 int is_retrans = RPC_WAS_SENT(task);
Chuck Leverff699ea82018-03-05 15:13:13 -05001438 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001440 if (!req->rq_bytes_sent) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001441 if (xprt_request_data_received(task)) {
1442 status = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001443 goto out_dequeue;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001444 }
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001445 /* Verify that our message lies in the RPCSEC_GSS window */
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001446 if (rpcauth_xmit_need_reencode(task)) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001447 status = -EBADMSG;
Trond Myklebust944b0422018-08-09 23:33:21 -04001448 goto out_dequeue;
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001449 }
Trond Myklebustae67bd32019-04-07 13:58:44 -04001450 if (RPC_SIGNALLED(task)) {
1451 status = -ERESTARTSYS;
1452 goto out_dequeue;
1453 }
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001456 /*
1457 * Update req->rq_ntrans before transmitting to avoid races with
1458 * xprt_update_rtt(), which needs to know that it is recording a
1459 * reply to the first transmission.
1460 */
1461 req->rq_ntrans++;
1462
Chuck Leverc509f15a2020-05-12 17:13:28 -04001463 trace_rpc_xdr_sendto(task, &req->rq_snd_buf);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001464 connect_cookie = xprt->connect_cookie;
Trond Myklebustadfa7142018-09-03 23:58:59 -04001465 status = xprt->ops->send_request(req);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001466 if (status != 0) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001467 req->rq_ntrans--;
Chuck Lever0c776682019-02-11 11:25:04 -05001468 trace_xprt_transmit(req, status);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001469 return status;
Chuck Leverfe3aca22005-08-25 16:25:50 -07001470 }
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001471
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001472 if (is_retrans)
1473 task->tk_client->cl_stats->rpcretrans++;
1474
Chuck Lever4a068252015-05-11 14:02:25 -04001475 xprt_inject_disconnect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Bryan Schumaker468f8612011-04-18 15:57:32 -04001477 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustb5e92412019-05-02 11:21:08 -04001478 spin_lock(&xprt->transport_lock);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001479
Trond Myklebustc8485e42009-03-11 14:37:59 -04001480 xprt->stat.sends++;
1481 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1482 xprt->stat.bklog_u += xprt->backlog.qlen;
Andy Adamson15a45202012-02-14 16:19:18 -05001483 xprt->stat.sending_u += xprt->sending.qlen;
1484 xprt->stat.pending_u += xprt->pending.qlen;
Trond Myklebustb5e92412019-05-02 11:21:08 -04001485 spin_unlock(&xprt->transport_lock);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001486
1487 req->rq_connect_cookie = connect_cookie;
Trond Myklebust944b0422018-08-09 23:33:21 -04001488out_dequeue:
Chuck Lever0c776682019-02-11 11:25:04 -05001489 trace_xprt_transmit(req, status);
Trond Myklebust944b0422018-08-09 23:33:21 -04001490 xprt_request_dequeue_transmit(task);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001491 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1492 return status;
1493}
1494
1495/**
1496 * xprt_transmit - send an RPC request on a transport
1497 * @task: controlling RPC task
1498 *
1499 * Attempts to drain the transmit queue. On exit, either the transport
1500 * signalled an error that needs to be handled before transmission can
1501 * resume, or @task finished transmitting, and detected that it already
1502 * received a reply.
1503 */
1504void
1505xprt_transmit(struct rpc_task *task)
1506{
1507 struct rpc_rqst *next, *req = task->tk_rqstp;
1508 struct rpc_xprt *xprt = req->rq_xprt;
1509 int status;
1510
1511 spin_lock(&xprt->queue_lock);
1512 while (!list_empty(&xprt->xmit_queue)) {
1513 next = list_first_entry(&xprt->xmit_queue,
1514 struct rpc_rqst, rq_xmit);
1515 xprt_pin_rqst(next);
1516 spin_unlock(&xprt->queue_lock);
1517 status = xprt_request_transmit(next, task);
1518 if (status == -EBADMSG && next != req)
1519 status = 0;
1520 cond_resched();
1521 spin_lock(&xprt->queue_lock);
1522 xprt_unpin_rqst(next);
1523 if (status == 0) {
1524 if (!xprt_request_data_received(task) ||
1525 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1526 continue;
Trond Myklebustc5445772018-09-03 23:39:27 -04001527 } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001528 task->tk_status = status;
1529 break;
1530 }
1531 spin_unlock(&xprt->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Trond Myklebustba60eb22013-04-14 10:49:37 -04001534static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1535{
1536 set_bit(XPRT_CONGESTED, &xprt->state);
1537 rpc_sleep_on(&xprt->backlog, task, NULL);
1538}
1539
1540static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1541{
1542 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1543 clear_bit(XPRT_CONGESTED, &xprt->state);
1544}
1545
1546static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1547{
1548 bool ret = false;
1549
1550 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1551 goto out;
1552 spin_lock(&xprt->reserve_lock);
1553 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1554 rpc_sleep_on(&xprt->backlog, task, NULL);
1555 ret = true;
1556 }
1557 spin_unlock(&xprt->reserve_lock);
1558out:
1559 return ret;
1560}
1561
Trond Myklebust92ea0112017-06-20 19:35:39 -04001562static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001563{
1564 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1565
Chuck Leverff699ea82018-03-05 15:13:13 -05001566 if (xprt->num_reqs >= xprt->max_reqs)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001567 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001568 ++xprt->num_reqs;
Trond Myklebust92ea0112017-06-20 19:35:39 -04001569 spin_unlock(&xprt->reserve_lock);
1570 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1571 spin_lock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001572 if (req != NULL)
1573 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001574 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001575 req = ERR_PTR(-ENOMEM);
1576out:
1577 return req;
1578}
1579
1580static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1581{
Chuck Leverff699ea82018-03-05 15:13:13 -05001582 if (xprt->num_reqs > xprt->min_reqs) {
1583 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001584 kfree(req);
1585 return true;
1586 }
1587 return false;
1588}
1589
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001590void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001592 struct rpc_rqst *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001594 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (!list_empty(&xprt->free)) {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001596 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1597 list_del(&req->rq_list);
1598 goto out_init_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Trond Myklebust92ea0112017-06-20 19:35:39 -04001600 req = xprt_dynamic_alloc_slot(xprt);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001601 if (!IS_ERR(req))
1602 goto out_init_req;
1603 switch (PTR_ERR(req)) {
1604 case -ENOMEM:
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001605 dprintk("RPC: dynamic allocation of request slot "
1606 "failed! Retrying\n");
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001607 task->tk_status = -ENOMEM;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001608 break;
1609 case -EAGAIN:
Trond Myklebustba60eb22013-04-14 10:49:37 -04001610 xprt_add_backlog(xprt, task);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001611 dprintk("RPC: waiting for request slot\n");
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001612 /* fall through */
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001613 default:
1614 task->tk_status = -EAGAIN;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001615 }
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001616 spin_unlock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001617 return;
1618out_init_req:
Chuck Leverff699ea82018-03-05 15:13:13 -05001619 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1620 xprt->num_reqs);
Chuck Lever37ac86c2018-05-04 15:34:53 -04001621 spin_unlock(&xprt->reserve_lock);
1622
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001623 task->tk_status = 0;
1624 task->tk_rqstp = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001626EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1627
Chuck Levera9cde232018-05-04 15:34:59 -04001628void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001629{
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001630 spin_lock(&xprt->reserve_lock);
Trond Myklebustc25573b2011-12-01 14:16:17 -05001631 if (!xprt_dynamic_free_slot(xprt, req)) {
1632 memset(req, 0, sizeof(*req)); /* mark unused */
1633 list_add(&req->rq_list, &xprt->free);
1634 }
Trond Myklebustba60eb22013-04-14 10:49:37 -04001635 xprt_wake_up_backlog(xprt);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001636 spin_unlock(&xprt->reserve_lock);
1637}
Chuck Levera9cde232018-05-04 15:34:59 -04001638EXPORT_SYMBOL_GPL(xprt_free_slot);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001639
Trond Myklebust21de0a92011-07-17 16:57:32 -04001640static void xprt_free_all_slots(struct rpc_xprt *xprt)
1641{
1642 struct rpc_rqst *req;
1643 while (!list_empty(&xprt->free)) {
1644 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1645 list_del(&req->rq_list);
1646 kfree(req);
1647 }
1648}
1649
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001650struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1651 unsigned int num_prealloc,
1652 unsigned int max_alloc)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001653{
1654 struct rpc_xprt *xprt;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001655 struct rpc_rqst *req;
1656 int i;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001657
1658 xprt = kzalloc(size, GFP_KERNEL);
1659 if (xprt == NULL)
1660 goto out;
1661
Trond Myklebust21de0a92011-07-17 16:57:32 -04001662 xprt_init(xprt, net);
1663
1664 for (i = 0; i < num_prealloc; i++) {
1665 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1666 if (!req)
wangweidong83131642013-10-15 11:44:30 +08001667 goto out_free;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001668 list_add(&req->rq_list, &xprt->free);
1669 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001670 if (max_alloc > num_prealloc)
1671 xprt->max_reqs = max_alloc;
1672 else
1673 xprt->max_reqs = num_prealloc;
1674 xprt->min_reqs = num_prealloc;
Chuck Leverff699ea82018-03-05 15:13:13 -05001675 xprt->num_reqs = num_prealloc;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001676
1677 return xprt;
1678
1679out_free:
Trond Myklebust21de0a92011-07-17 16:57:32 -04001680 xprt_free(xprt);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001681out:
1682 return NULL;
1683}
1684EXPORT_SYMBOL_GPL(xprt_alloc);
1685
Pavel Emelyanove204e622010-09-29 16:03:13 +04001686void xprt_free(struct rpc_xprt *xprt)
1687{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04001688 put_net(xprt->xprt_net);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001689 xprt_free_all_slots(xprt);
Trond Myklebustfda1bfe2015-02-14 17:48:49 -05001690 kfree_rcu(xprt, rcu);
Pavel Emelyanove204e622010-09-29 16:03:13 +04001691}
1692EXPORT_SYMBOL_GPL(xprt_free);
1693
Trond Myklebust902c5882018-09-01 17:21:01 -04001694static void
1695xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1696{
1697 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1698}
1699
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001700static __be32
1701xprt_alloc_xid(struct rpc_xprt *xprt)
1702{
1703 __be32 xid;
1704
1705 spin_lock(&xprt->reserve_lock);
1706 xid = (__force __be32)xprt->xid++;
1707 spin_unlock(&xprt->reserve_lock);
1708 return xid;
1709}
1710
1711static void
1712xprt_init_xid(struct rpc_xprt *xprt)
1713{
1714 xprt->xid = prandom_u32();
1715}
1716
1717static void
1718xprt_request_init(struct rpc_task *task)
1719{
1720 struct rpc_xprt *xprt = task->tk_xprt;
1721 struct rpc_rqst *req = task->tk_rqstp;
1722
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001723 req->rq_task = task;
1724 req->rq_xprt = xprt;
1725 req->rq_buffer = NULL;
1726 req->rq_xid = xprt_alloc_xid(xprt);
Trond Myklebust902c5882018-09-01 17:21:01 -04001727 xprt_init_connect_cookie(req, xprt);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001728 req->rq_snd_buf.len = 0;
1729 req->rq_snd_buf.buflen = 0;
1730 req->rq_rcv_buf.len = 0;
1731 req->rq_rcv_buf.buflen = 0;
Trond Myklebust71700bb2018-11-30 16:11:15 -05001732 req->rq_snd_buf.bvec = NULL;
1733 req->rq_rcv_buf.bvec = NULL;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001734 req->rq_release_snd_buf = NULL;
Trond Myklebustda953062019-04-07 13:58:56 -04001735 xprt_init_majortimeo(task, req);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001736 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1737 req, ntohl(req->rq_xid));
1738}
1739
1740static void
1741xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1742{
1743 xprt->ops->alloc_slot(xprt, task);
1744 if (task->tk_rqstp != NULL)
1745 xprt_request_init(task);
1746}
1747
Chuck Lever9903cd12005-08-11 16:25:26 -04001748/**
1749 * xprt_reserve - allocate an RPC request slot
1750 * @task: RPC task requesting a slot allocation
1751 *
Trond Myklebustba60eb22013-04-14 10:49:37 -04001752 * If the transport is marked as being congested, or if no more
1753 * slots are available, place the task on the transport's
Chuck Lever9903cd12005-08-11 16:25:26 -04001754 * backlog queue.
1755 */
1756void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001758 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001760 task->tk_status = 0;
1761 if (task->tk_rqstp != NULL)
1762 return;
1763
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001764 task->tk_status = -EAGAIN;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001765 if (!xprt_throttle_congested(xprt, task))
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001766 xprt_do_reserve(xprt, task);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001767}
1768
1769/**
1770 * xprt_retry_reserve - allocate an RPC request slot
1771 * @task: RPC task requesting a slot allocation
1772 *
1773 * If no more slots are available, place the task on the transport's
1774 * backlog queue.
1775 * Note that the only difference with xprt_reserve is that we now
1776 * ignore the value of the XPRT_CONGESTED flag.
1777 */
1778void xprt_retry_reserve(struct rpc_task *task)
1779{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001780 struct rpc_xprt *xprt = task->tk_xprt;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001781
1782 task->tk_status = 0;
1783 if (task->tk_rqstp != NULL)
1784 return;
1785
Trond Myklebustba60eb22013-04-14 10:49:37 -04001786 task->tk_status = -EAGAIN;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001787 xprt_do_reserve(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Chuck Lever9903cd12005-08-11 16:25:26 -04001790/**
1791 * xprt_release - release an RPC request slot
1792 * @task: task which is finished with the slot
1793 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001795void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001797 struct rpc_xprt *xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001798 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Trond Myklebust87ed5002013-01-07 14:30:46 -05001800 if (req == NULL) {
1801 if (task->tk_client) {
Trond Myklebustfb43d172016-01-30 16:39:26 -05001802 xprt = task->tk_xprt;
Trond Myklebustbd79bc52018-09-07 19:38:55 -04001803 xprt_release_write(xprt, task);
Trond Myklebust87ed5002013-01-07 14:30:46 -05001804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001806 }
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001807
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001808 xprt = req->rq_xprt;
Trond Myklebustcc204d02019-09-10 13:01:35 -04001809 xprt_request_dequeue_xprt(task);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001810 spin_lock(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -07001811 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -07001812 if (xprt->ops->release_request)
1813 xprt->ops->release_request(task);
Trond Myklebustad3331a2016-08-02 13:47:43 -04001814 xprt_schedule_autodisconnect(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001815 spin_unlock(&xprt->transport_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001816 if (req->rq_buffer)
Chuck Lever3435c742016-09-15 10:55:29 -04001817 xprt->ops->buf_free(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001818 xprt_inject_disconnect(xprt);
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001819 xdr_free_bvec(&req->rq_rcv_buf);
Trond Myklebust0472e472019-02-19 13:00:13 -05001820 xdr_free_bvec(&req->rq_snd_buf);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001821 if (req->rq_cred != NULL)
1822 put_rpccred(req->rq_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001824 if (req->rq_release_snd_buf)
1825 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001826
Chuck Lever46121cf2007-01-31 12:14:08 -05001827 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001828 if (likely(!bc_prealloc(req)))
Chuck Levera9cde232018-05-04 15:34:59 -04001829 xprt->ops->free_slot(xprt, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001830 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001831 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Trond Myklebust902c5882018-09-01 17:21:01 -04001834#ifdef CONFIG_SUNRPC_BACKCHANNEL
1835void
1836xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1837{
1838 struct xdr_buf *xbufp = &req->rq_snd_buf;
1839
1840 task->tk_rqstp = req;
1841 req->rq_task = task;
1842 xprt_init_connect_cookie(req, req->rq_xprt);
1843 /*
1844 * Set up the xdr_buf length.
1845 * This also indicates that the buffer is XDR encoded already.
1846 */
1847 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1848 xbufp->tail[0].iov_len;
Trond Myklebust902c5882018-09-01 17:21:01 -04001849}
1850#endif
1851
Trond Myklebust21de0a92011-07-17 16:57:32 -04001852static void xprt_init(struct rpc_xprt *xprt, struct net *net)
Chuck Leverc2866762006-08-22 20:06:20 -04001853{
Trond Myklebust30c51162015-02-24 20:31:39 -05001854 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04001855
1856 spin_lock_init(&xprt->transport_lock);
1857 spin_lock_init(&xprt->reserve_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001858 spin_lock_init(&xprt->queue_lock);
Chuck Leverc2866762006-08-22 20:06:20 -04001859
1860 INIT_LIST_HEAD(&xprt->free);
Trond Myklebust95f76912018-09-07 08:35:22 -04001861 xprt->recv_queue = RB_ROOT;
Trond Myklebust944b0422018-08-09 23:33:21 -04001862 INIT_LIST_HEAD(&xprt->xmit_queue);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001863#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001864 spin_lock_init(&xprt->bc_pa_lock);
1865 INIT_LIST_HEAD(&xprt->bc_pa_list);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001866#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Trond Myklebust80b14d52015-02-14 20:31:59 -05001867 INIT_LIST_HEAD(&xprt->xprt_switch);
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001868
Chuck Leverc2866762006-08-22 20:06:20 -04001869 xprt->last_used = jiffies;
1870 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001871 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001872
1873 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1874 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
Trond Myklebust79c99152018-09-09 13:53:05 -04001875 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
Chuck Leverc2866762006-08-22 20:06:20 -04001876 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1877
Chuck Leverc2866762006-08-22 20:06:20 -04001878 xprt_init_xid(xprt);
1879
Trond Myklebust21de0a92011-07-17 16:57:32 -04001880 xprt->xprt_net = get_net(net);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001881}
1882
1883/**
1884 * xprt_create_transport - create an RPC transport
1885 * @args: rpc transport creation arguments
1886 *
1887 */
1888struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1889{
1890 struct rpc_xprt *xprt;
1891 struct xprt_class *t;
1892
1893 spin_lock(&xprt_list_lock);
1894 list_for_each_entry(t, &xprt_list, list) {
1895 if (t->ident == args->ident) {
1896 spin_unlock(&xprt_list_lock);
1897 goto found;
1898 }
1899 }
1900 spin_unlock(&xprt_list_lock);
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001901 dprintk("RPC: transport (%d) not supported\n", args->ident);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001902 return ERR_PTR(-EIO);
1903
1904found:
1905 xprt = t->setup(args);
1906 if (IS_ERR(xprt)) {
1907 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1908 -PTR_ERR(xprt));
Trond Myklebust21de0a92011-07-17 16:57:32 -04001909 goto out;
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001910 }
J. Bruce Fields33d90ac2013-04-11 15:06:36 -04001911 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1912 xprt->idle_timeout = 0;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001913 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1914 if (xprt_has_timer(xprt))
Anna Schumaker502980e2019-06-18 14:57:33 -04001915 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001916 else
Kees Cookff861c42017-10-16 17:29:42 -07001917 timer_setup(&xprt->timer, NULL, 0);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001918
1919 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1920 xprt_destroy(xprt);
1921 return ERR_PTR(-EINVAL);
1922 }
1923 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1924 if (xprt->servername == NULL) {
1925 xprt_destroy(xprt);
1926 return ERR_PTR(-ENOMEM);
1927 }
1928
Jeff Layton3f940092015-03-31 12:03:28 -04001929 rpc_xprt_debugfs_register(xprt);
Jeff Layton388f0c72014-11-26 14:44:44 -05001930
Chuck Lever46121cf2007-01-31 12:14:08 -05001931 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001932 xprt->max_reqs);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001933out:
Chuck Leverc2866762006-08-22 20:06:20 -04001934 return xprt;
1935}
1936
Trond Myklebust528fd352017-10-19 12:13:10 -04001937static void xprt_destroy_cb(struct work_struct *work)
1938{
1939 struct rpc_xprt *xprt =
1940 container_of(work, struct rpc_xprt, task_cleanup);
1941
1942 rpc_xprt_debugfs_unregister(xprt);
1943 rpc_destroy_wait_queue(&xprt->binding);
1944 rpc_destroy_wait_queue(&xprt->pending);
1945 rpc_destroy_wait_queue(&xprt->sending);
1946 rpc_destroy_wait_queue(&xprt->backlog);
1947 kfree(xprt->servername);
1948 /*
Trond Myklebust669996a2019-10-17 09:02:21 -04001949 * Destroy any existing back channel
1950 */
1951 xprt_destroy_backchannel(xprt, UINT_MAX);
1952
1953 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04001954 * Tear down transport state and free the rpc_xprt
1955 */
1956 xprt->ops->destroy(xprt);
1957}
1958
Chuck Lever9903cd12005-08-11 16:25:26 -04001959/**
1960 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04001961 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001962 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04001964static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Chuck Lever46121cf2007-01-31 12:14:08 -05001966 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust79234c32015-09-18 15:53:24 -04001967
Trond Myklebust528fd352017-10-19 12:13:10 -04001968 /*
1969 * Exclude transport connect/disconnect handlers and autoclose
1970 */
Trond Myklebust79234c32015-09-18 15:53:24 -04001971 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1972
Trond Myklebust0065db32006-01-03 09:55:56 +01001973 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001974
1975 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04001976 * Destroy sockets etc from the system workqueue so they can
1977 * safely flush receive work running on rpciod.
Chuck Leverc8541ec2006-10-17 14:44:27 -04001978 */
Trond Myklebust528fd352017-10-19 12:13:10 -04001979 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1980 schedule_work(&xprt->task_cleanup);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001981}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Trond Myklebust30c51162015-02-24 20:31:39 -05001983static void xprt_destroy_kref(struct kref *kref)
1984{
1985 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1986}
1987
1988/**
1989 * xprt_get - return a reference to an RPC transport.
1990 * @xprt: pointer to the transport
1991 *
1992 */
1993struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1994{
1995 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1996 return xprt;
1997 return NULL;
1998}
1999EXPORT_SYMBOL_GPL(xprt_get);
2000
Trond Myklebust6b6ca862006-09-05 12:55:57 -04002001/**
2002 * xprt_put - release a reference to an RPC transport.
2003 * @xprt: pointer to the transport
2004 *
2005 */
2006void xprt_put(struct rpc_xprt *xprt)
2007{
Trond Myklebust30c51162015-02-24 20:31:39 -05002008 if (xprt != NULL)
2009 kref_put(&xprt->kref, xprt_destroy_kref);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04002010}
Chuck Lever5d252f92016-01-07 14:50:10 -05002011EXPORT_SYMBOL_GPL(xprt_put);