Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/xprt.c |
| 3 | * |
| 4 | * This is a generic RPC call interface supporting congestion avoidance, |
| 5 | * and asynchronous calls. |
| 6 | * |
| 7 | * The interface works like this: |
| 8 | * |
| 9 | * - When a process places a call, it allocates a request slot if |
| 10 | * one is available. Otherwise, it sleeps on the backlog queue |
| 11 | * (xprt_reserve). |
| 12 | * - Next, the caller puts together the RPC message, stuffs it into |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 13 | * the request struct, and calls xprt_transmit(). |
| 14 | * - xprt_transmit sends the message and installs the caller on the |
Ricardo Labiaga | 55ae1aa | 2009-04-01 09:23:03 -0400 | [diff] [blame] | 15 | * transport's wait list. At the same time, if a reply is expected, |
| 16 | * it installs a timer that is run after the packet's timeout has |
| 17 | * expired. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * - When a packet arrives, the data_ready handler walks the list of |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 19 | * pending requests for that transport. If a matching XID is found, the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * caller is woken up, and the timer removed. |
| 21 | * - When no reply arrives within the timeout interval, the timer is |
| 22 | * fired by the kernel and runs xprt_timer(). It either adjusts the |
| 23 | * timeout values (minor timeout) or wakes up the caller with a status |
| 24 | * of -ETIMEDOUT. |
| 25 | * - When the caller receives a notification from RPC that a reply arrived, |
| 26 | * it should release the RPC slot, and process the reply. |
| 27 | * If the call timed out, it may choose to retry the operation by |
| 28 | * adjusting the initial timeout value, and simply calling rpc_call |
| 29 | * again. |
| 30 | * |
| 31 | * Support for async RPC is done through a set of RPC-specific scheduling |
| 32 | * primitives that `transparently' work for processes as well as async |
| 33 | * tasks that rely on callbacks. |
| 34 | * |
| 35 | * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 36 | * |
| 37 | * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | */ |
| 39 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 40 | #include <linux/module.h> |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/types.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 43 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/workqueue.h> |
Chuck Lever | bf3fcf8 | 2006-05-25 01:40:51 -0400 | [diff] [blame] | 45 | #include <linux/net.h> |
Chuck Lever | ff83997 | 2010-05-07 13:34:47 -0400 | [diff] [blame] | 46 | #include <linux/ktime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 48 | #include <linux/sunrpc/clnt.h> |
Chuck Lever | 11c556b | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 49 | #include <linux/sunrpc/metrics.h> |
Trond Myklebust | c9acb42 | 2010-03-19 15:36:22 -0400 | [diff] [blame] | 50 | #include <linux/sunrpc/bc_xprt.h> |
Trond Myklebust | fda1bfe | 2015-02-14 17:48:49 -0500 | [diff] [blame] | 51 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Jeff Layton | 3705ad6 | 2014-10-28 14:24:13 -0400 | [diff] [blame] | 53 | #include <trace/events/sunrpc.h> |
| 54 | |
Ricardo Labiaga | 55ae1aa | 2009-04-01 09:23:03 -0400 | [diff] [blame] | 55 | #include "sunrpc.h" |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Local variables |
| 59 | */ |
| 60 | |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame] | 61 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | # define RPCDBG_FACILITY RPCDBG_XPRT |
| 63 | #endif |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Local functions |
| 67 | */ |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 68 | static void xprt_init(struct rpc_xprt *xprt, struct net *net); |
Chuck Lever | 37ac86c | 2018-05-04 15:34:53 -0400 | [diff] [blame] | 69 | static __be32 xprt_alloc_xid(struct rpc_xprt *xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | static void xprt_connect_status(struct rpc_task *task); |
Trond Myklebust | 4e0038b | 2012-03-01 17:01:05 -0500 | [diff] [blame] | 71 | static void xprt_destroy(struct rpc_xprt *xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Jiri Slaby | 5ba03e8 | 2007-11-22 19:40:22 +0800 | [diff] [blame] | 73 | static DEFINE_SPINLOCK(xprt_list_lock); |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 74 | static LIST_HEAD(xprt_list); |
| 75 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 76 | /** |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 77 | * xprt_register_transport - register a transport implementation |
| 78 | * @transport: transport to register |
| 79 | * |
| 80 | * If a transport implementation is loaded as a kernel module, it can |
| 81 | * call this interface to make itself known to the RPC client. |
| 82 | * |
| 83 | * Returns: |
| 84 | * 0: transport successfully registered |
| 85 | * -EEXIST: transport already registered |
| 86 | * -EINVAL: transport module being unloaded |
| 87 | */ |
| 88 | int xprt_register_transport(struct xprt_class *transport) |
| 89 | { |
| 90 | struct xprt_class *t; |
| 91 | int result; |
| 92 | |
| 93 | result = -EEXIST; |
| 94 | spin_lock(&xprt_list_lock); |
| 95 | list_for_each_entry(t, &xprt_list, list) { |
| 96 | /* don't register the same transport class twice */ |
\"Talpey, Thomas\ | 4fa016e | 2007-09-10 13:47:57 -0400 | [diff] [blame] | 97 | if (t->ident == transport->ident) |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 98 | goto out; |
| 99 | } |
| 100 | |
Denis V. Lunev | c9f6cde | 2008-07-31 09:53:56 +0400 | [diff] [blame] | 101 | list_add_tail(&transport->list, &xprt_list); |
| 102 | printk(KERN_INFO "RPC: Registered %s transport module.\n", |
| 103 | transport->name); |
| 104 | result = 0; |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 105 | |
| 106 | out: |
| 107 | spin_unlock(&xprt_list_lock); |
| 108 | return result; |
| 109 | } |
| 110 | EXPORT_SYMBOL_GPL(xprt_register_transport); |
| 111 | |
| 112 | /** |
| 113 | * xprt_unregister_transport - unregister a transport implementation |
Randy Dunlap | 65b6e42 | 2008-02-13 15:03:23 -0800 | [diff] [blame] | 114 | * @transport: transport to unregister |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 115 | * |
| 116 | * Returns: |
| 117 | * 0: transport successfully unregistered |
| 118 | * -ENOENT: transport never registered |
| 119 | */ |
| 120 | int xprt_unregister_transport(struct xprt_class *transport) |
| 121 | { |
| 122 | struct xprt_class *t; |
| 123 | int result; |
| 124 | |
| 125 | result = 0; |
| 126 | spin_lock(&xprt_list_lock); |
| 127 | list_for_each_entry(t, &xprt_list, list) { |
| 128 | if (t == transport) { |
| 129 | printk(KERN_INFO |
| 130 | "RPC: Unregistered %s transport module.\n", |
| 131 | transport->name); |
| 132 | list_del_init(&transport->list); |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 133 | goto out; |
| 134 | } |
| 135 | } |
| 136 | result = -ENOENT; |
| 137 | |
| 138 | out: |
| 139 | spin_unlock(&xprt_list_lock); |
| 140 | return result; |
| 141 | } |
| 142 | EXPORT_SYMBOL_GPL(xprt_unregister_transport); |
| 143 | |
| 144 | /** |
Tom Talpey | 441e3e2 | 2009-03-11 14:37:56 -0400 | [diff] [blame] | 145 | * xprt_load_transport - load a transport implementation |
| 146 | * @transport_name: transport to load |
| 147 | * |
| 148 | * Returns: |
| 149 | * 0: transport successfully loaded |
| 150 | * -ENOENT: transport module not available |
| 151 | */ |
| 152 | int xprt_load_transport(const char *transport_name) |
| 153 | { |
| 154 | struct xprt_class *t; |
Tom Talpey | 441e3e2 | 2009-03-11 14:37:56 -0400 | [diff] [blame] | 155 | int result; |
| 156 | |
| 157 | result = 0; |
| 158 | spin_lock(&xprt_list_lock); |
| 159 | list_for_each_entry(t, &xprt_list, list) { |
| 160 | if (strcmp(t->name, transport_name) == 0) { |
| 161 | spin_unlock(&xprt_list_lock); |
| 162 | goto out; |
| 163 | } |
| 164 | } |
| 165 | spin_unlock(&xprt_list_lock); |
Alex Riesen | ef7ffe8 | 2010-05-24 14:33:05 -0700 | [diff] [blame] | 166 | result = request_module("xprt%s", transport_name); |
Tom Talpey | 441e3e2 | 2009-03-11 14:37:56 -0400 | [diff] [blame] | 167 | out: |
| 168 | return result; |
| 169 | } |
| 170 | EXPORT_SYMBOL_GPL(xprt_load_transport); |
| 171 | |
| 172 | /** |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 173 | * xprt_reserve_xprt - serialize write access to transports |
| 174 | * @task: task that is requesting access to the transport |
Randy Dunlap | 177c27b | 2011-07-28 06:54:36 +0000 | [diff] [blame] | 175 | * @xprt: pointer to the target transport |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 176 | * |
| 177 | * This prevents mixing the payload of separate requests, and prevents |
| 178 | * transport connects from colliding with writes. No congestion control |
| 179 | * is provided. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | */ |
Trond Myklebust | 43cedbf0e | 2011-07-17 16:01:03 -0400 | [diff] [blame] | 181 | int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 183 | struct rpc_rqst *req = task->tk_rqstp; |
Trond Myklebust | 34006ce | 2011-07-17 18:11:34 -0400 | [diff] [blame] | 184 | int priority; |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 185 | |
| 186 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { |
| 187 | if (task == xprt->snd_task) |
| 188 | return 1; |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 189 | goto out_sleep; |
| 190 | } |
| 191 | xprt->snd_task = task; |
j223yang@asset.uwaterloo.ca | 4d4a76f | 2011-03-10 12:40:28 -0500 | [diff] [blame] | 192 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 193 | return 1; |
| 194 | |
| 195 | out_sleep: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 196 | dprintk("RPC: %5u failed to lock transport %p\n", |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 197 | task->tk_pid, xprt); |
Trond Myklebust | f05d54e | 2018-09-03 23:11:15 -0400 | [diff] [blame^] | 198 | task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 199 | task->tk_status = -EAGAIN; |
Trond Myklebust | 34006ce | 2011-07-17 18:11:34 -0400 | [diff] [blame] | 200 | if (req == NULL) |
| 201 | priority = RPC_PRIORITY_LOW; |
| 202 | else if (!req->rq_ntrans) |
| 203 | priority = RPC_PRIORITY_NORMAL; |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 204 | else |
Trond Myklebust | 34006ce | 2011-07-17 18:11:34 -0400 | [diff] [blame] | 205 | priority = RPC_PRIORITY_HIGH; |
| 206 | rpc_sleep_on_priority(&xprt->sending, task, NULL, priority); |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 209 | EXPORT_SYMBOL_GPL(xprt_reserve_xprt); |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 210 | |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 211 | static void xprt_clear_locked(struct rpc_xprt *xprt) |
| 212 | { |
| 213 | xprt->snd_task = NULL; |
Trond Myklebust | d19751e | 2012-09-11 17:21:25 -0400 | [diff] [blame] | 214 | if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 215 | smp_mb__before_atomic(); |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 216 | clear_bit(XPRT_LOCKED, &xprt->state); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 217 | smp_mb__after_atomic(); |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 218 | } else |
Trond Myklebust | 40a5f1b | 2016-05-27 10:39:50 -0400 | [diff] [blame] | 219 | queue_work(xprtiod_workqueue, &xprt->task_cleanup); |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 222 | static bool |
| 223 | xprt_need_congestion_window_wait(struct rpc_xprt *xprt) |
| 224 | { |
| 225 | return test_bit(XPRT_CWND_WAIT, &xprt->state); |
| 226 | } |
| 227 | |
| 228 | static void |
| 229 | xprt_set_congestion_window_wait(struct rpc_xprt *xprt) |
| 230 | { |
| 231 | if (!list_empty(&xprt->xmit_queue)) { |
| 232 | /* Peek at head of queue to see if it can make progress */ |
| 233 | if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst, |
| 234 | rq_xmit)->rq_cong) |
| 235 | return; |
| 236 | } |
| 237 | set_bit(XPRT_CWND_WAIT, &xprt->state); |
| 238 | } |
| 239 | |
| 240 | static void |
| 241 | xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt) |
| 242 | { |
| 243 | if (!RPCXPRT_CONGESTED(xprt)) |
| 244 | clear_bit(XPRT_CWND_WAIT, &xprt->state); |
| 245 | } |
| 246 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 247 | /* |
| 248 | * xprt_reserve_xprt_cong - serialize write access to transports |
| 249 | * @task: task that is requesting access to the transport |
| 250 | * |
| 251 | * Same as xprt_reserve_xprt, but Van Jacobson congestion control is |
| 252 | * integrated into the decision of whether a request is allowed to be |
| 253 | * woken up and given access to the transport. |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 254 | * Note that the lock is only granted if we know there are free slots. |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 255 | */ |
Trond Myklebust | 43cedbf0e | 2011-07-17 16:01:03 -0400 | [diff] [blame] | 256 | int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 257 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | struct rpc_rqst *req = task->tk_rqstp; |
Trond Myklebust | 34006ce | 2011-07-17 18:11:34 -0400 | [diff] [blame] | 259 | int priority; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 261 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (task == xprt->snd_task) |
| 263 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | goto out_sleep; |
| 265 | } |
Trond Myklebust | 43cedbf0e | 2011-07-17 16:01:03 -0400 | [diff] [blame] | 266 | if (req == NULL) { |
| 267 | xprt->snd_task = task; |
| 268 | return 1; |
| 269 | } |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 270 | if (!xprt_need_congestion_window_wait(xprt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | xprt->snd_task = task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | return 1; |
| 273 | } |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 274 | xprt_clear_locked(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | out_sleep: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 276 | dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); |
Trond Myklebust | f05d54e | 2018-09-03 23:11:15 -0400 | [diff] [blame^] | 277 | task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | task->tk_status = -EAGAIN; |
Trond Myklebust | 34006ce | 2011-07-17 18:11:34 -0400 | [diff] [blame] | 279 | if (req == NULL) |
| 280 | priority = RPC_PRIORITY_LOW; |
| 281 | else if (!req->rq_ntrans) |
| 282 | priority = RPC_PRIORITY_NORMAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | else |
Trond Myklebust | 34006ce | 2011-07-17 18:11:34 -0400 | [diff] [blame] | 284 | priority = RPC_PRIORITY_HIGH; |
| 285 | rpc_sleep_on_priority(&xprt->sending, task, NULL, priority); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | return 0; |
| 287 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 288 | EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 290 | static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | int retval; |
| 293 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 294 | spin_lock_bh(&xprt->transport_lock); |
Trond Myklebust | 43cedbf0e | 2011-07-17 16:01:03 -0400 | [diff] [blame] | 295 | retval = xprt->ops->reserve_xprt(xprt, task); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 296 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return retval; |
| 298 | } |
| 299 | |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 300 | static bool __xprt_lock_write_func(struct rpc_task *task, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 302 | struct rpc_xprt *xprt = data; |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 303 | |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 304 | xprt->snd_task = task; |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 305 | return true; |
| 306 | } |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 307 | |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 308 | static void __xprt_lock_write_next(struct rpc_xprt *xprt) |
| 309 | { |
| 310 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
| 311 | return; |
| 312 | |
Trond Myklebust | f1dc237 | 2016-05-27 12:59:33 -0400 | [diff] [blame] | 313 | if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, |
| 314 | __xprt_lock_write_func, xprt)) |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 315 | return; |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 316 | xprt_clear_locked(xprt); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 319 | static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) |
| 320 | { |
| 321 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
| 322 | return; |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 323 | if (xprt_need_congestion_window_wait(xprt)) |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 324 | goto out_unlock; |
Trond Myklebust | f1dc237 | 2016-05-27 12:59:33 -0400 | [diff] [blame] | 325 | if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 326 | __xprt_lock_write_func, xprt)) |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 327 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | out_unlock: |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 329 | xprt_clear_locked(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 332 | /** |
| 333 | * xprt_release_xprt - allow other requests to use a transport |
| 334 | * @xprt: transport with other tasks potentially waiting |
| 335 | * @task: task that is releasing access to the transport |
| 336 | * |
| 337 | * Note that "task" can be NULL. No congestion control is provided. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | */ |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 339 | void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | { |
| 341 | if (xprt->snd_task == task) { |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 342 | xprt_clear_locked(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | __xprt_lock_write_next(xprt); |
| 344 | } |
| 345 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 346 | EXPORT_SYMBOL_GPL(xprt_release_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 348 | /** |
| 349 | * xprt_release_xprt_cong - allow other requests to use a transport |
| 350 | * @xprt: transport with other tasks potentially waiting |
| 351 | * @task: task that is releasing access to the transport |
| 352 | * |
| 353 | * Note that "task" can be NULL. Another task is awoken to use the |
| 354 | * transport if the transport's congestion window allows it. |
| 355 | */ |
| 356 | void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) |
| 357 | { |
| 358 | if (xprt->snd_task == task) { |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 359 | xprt_clear_locked(xprt); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 360 | __xprt_lock_write_next_cong(xprt); |
| 361 | } |
| 362 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 363 | EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 364 | |
| 365 | static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 367 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 368 | xprt->ops->release_xprt(xprt, task); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 369 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | * Van Jacobson congestion avoidance. Check if the congestion window |
| 374 | * overflowed. Put the task to sleep if this is the case. |
| 375 | */ |
| 376 | static int |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 377 | __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (req->rq_cong) |
| 380 | return 1; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 381 | dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 382 | req->rq_task->tk_pid, xprt->cong, xprt->cwnd); |
| 383 | if (RPCXPRT_CONGESTED(xprt)) { |
| 384 | xprt_set_congestion_window_wait(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return 0; |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | req->rq_cong = 1; |
| 388 | xprt->cong += RPC_CWNDSCALE; |
| 389 | return 1; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Adjust the congestion window, and wake up the next task |
| 394 | * that has been sleeping due to congestion |
| 395 | */ |
| 396 | static void |
| 397 | __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) |
| 398 | { |
| 399 | if (!req->rq_cong) |
| 400 | return; |
| 401 | req->rq_cong = 0; |
| 402 | xprt->cong -= RPC_CWNDSCALE; |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 403 | xprt_test_and_clear_congestion_window_wait(xprt); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 404 | __xprt_lock_write_next_cong(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 407 | /** |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 408 | * xprt_request_get_cong - Request congestion control credits |
| 409 | * @xprt: pointer to transport |
| 410 | * @req: pointer to RPC request |
| 411 | * |
| 412 | * Useful for transports that require congestion control. |
| 413 | */ |
| 414 | bool |
| 415 | xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) |
| 416 | { |
| 417 | bool ret = false; |
| 418 | |
| 419 | if (req->rq_cong) |
| 420 | return true; |
| 421 | spin_lock_bh(&xprt->transport_lock); |
| 422 | ret = __xprt_get_cong(xprt, req) != 0; |
| 423 | spin_unlock_bh(&xprt->transport_lock); |
| 424 | return ret; |
| 425 | } |
| 426 | EXPORT_SYMBOL_GPL(xprt_request_get_cong); |
| 427 | |
| 428 | /** |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 429 | * xprt_release_rqst_cong - housekeeping when request is complete |
| 430 | * @task: RPC request that recently completed |
| 431 | * |
| 432 | * Useful for transports that require congestion control. |
| 433 | */ |
| 434 | void xprt_release_rqst_cong(struct rpc_task *task) |
| 435 | { |
Trond Myklebust | a4f0835 | 2013-01-08 09:10:21 -0500 | [diff] [blame] | 436 | struct rpc_rqst *req = task->tk_rqstp; |
| 437 | |
| 438 | __xprt_put_cong(req->rq_xprt, req); |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 439 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 440 | EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 441 | |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 442 | /* |
| 443 | * Clear the congestion window wait flag and wake up the next |
| 444 | * entry on xprt->sending |
| 445 | */ |
| 446 | static void |
| 447 | xprt_clear_congestion_window_wait(struct rpc_xprt *xprt) |
| 448 | { |
| 449 | if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) { |
| 450 | spin_lock_bh(&xprt->transport_lock); |
| 451 | __xprt_lock_write_next_cong(xprt); |
| 452 | spin_unlock_bh(&xprt->transport_lock); |
| 453 | } |
| 454 | } |
| 455 | |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 456 | /** |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 457 | * xprt_adjust_cwnd - adjust transport congestion window |
Trond Myklebust | 6a24dfb | 2013-01-08 09:48:15 -0500 | [diff] [blame] | 458 | * @xprt: pointer to xprt |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 459 | * @task: recently completed RPC request used to adjust window |
| 460 | * @result: result code of completed RPC request |
| 461 | * |
Chuck Lever | 4f4cf5a | 2014-05-28 10:34:49 -0400 | [diff] [blame] | 462 | * The transport code maintains an estimate on the maximum number of out- |
| 463 | * standing RPC requests, using a smoothed version of the congestion |
| 464 | * avoidance implemented in 44BSD. This is basically the Van Jacobson |
| 465 | * congestion algorithm: If a retransmit occurs, the congestion window is |
| 466 | * halved; otherwise, it is incremented by 1/cwnd when |
| 467 | * |
| 468 | * - a reply is received and |
| 469 | * - a full number of requests are outstanding and |
| 470 | * - the congestion window hasn't been updated recently. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | */ |
Trond Myklebust | 6a24dfb | 2013-01-08 09:48:15 -0500 | [diff] [blame] | 472 | void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 474 | struct rpc_rqst *req = task->tk_rqstp; |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 475 | unsigned long cwnd = xprt->cwnd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | if (result >= 0 && cwnd <= xprt->cong) { |
| 478 | /* The (cwnd >> 1) term makes sure |
| 479 | * the result gets rounded properly. */ |
| 480 | cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; |
| 481 | if (cwnd > RPC_MAXCWND(xprt)) |
| 482 | cwnd = RPC_MAXCWND(xprt); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 483 | __xprt_lock_write_next_cong(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } else if (result == -ETIMEDOUT) { |
| 485 | cwnd >>= 1; |
| 486 | if (cwnd < RPC_CWNDSCALE) |
| 487 | cwnd = RPC_CWNDSCALE; |
| 488 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 489 | dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | xprt->cong, xprt->cwnd, cwnd); |
| 491 | xprt->cwnd = cwnd; |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 492 | __xprt_put_cong(xprt, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 494 | EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 496 | /** |
| 497 | * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue |
| 498 | * @xprt: transport with waiting tasks |
| 499 | * @status: result code to plant in each task before waking it |
| 500 | * |
| 501 | */ |
| 502 | void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) |
| 503 | { |
| 504 | if (status < 0) |
| 505 | rpc_wake_up_status(&xprt->pending, status); |
| 506 | else |
| 507 | rpc_wake_up(&xprt->pending); |
| 508 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 509 | EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 510 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 511 | /** |
| 512 | * xprt_wait_for_buffer_space - wait for transport output buffer to clear |
| 513 | * @task: task to be put to sleep |
Randy Dunlap | 0b80ae4 | 2008-04-26 22:59:02 -0700 | [diff] [blame] | 514 | * @action: function pointer to be executed after wait |
Trond Myklebust | a9a6b52 | 2013-02-22 14:57:57 -0500 | [diff] [blame] | 515 | * |
| 516 | * Note that we only set the timer for the case of RPC_IS_SOFT(), since |
| 517 | * we don't in general want to force a socket disconnection due to |
| 518 | * an incomplete RPC call transmission. |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 519 | */ |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 520 | void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action) |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 521 | { |
| 522 | struct rpc_rqst *req = task->tk_rqstp; |
| 523 | struct rpc_xprt *xprt = req->rq_xprt; |
| 524 | |
Trond Myklebust | a9a6b52 | 2013-02-22 14:57:57 -0500 | [diff] [blame] | 525 | task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 526 | rpc_sleep_on(&xprt->pending, task, action); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 527 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 528 | EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 529 | |
| 530 | /** |
| 531 | * xprt_write_space - wake the task waiting for transport output buffer space |
| 532 | * @xprt: transport with waiting tasks |
| 533 | * |
| 534 | * Can be called in a soft IRQ context, so xprt_write_space never sleeps. |
| 535 | */ |
| 536 | void xprt_write_space(struct rpc_xprt *xprt) |
| 537 | { |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 538 | spin_lock_bh(&xprt->transport_lock); |
| 539 | if (xprt->snd_task) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 540 | dprintk("RPC: write space: waking waiting task on " |
| 541 | "xprt %p\n", xprt); |
Trond Myklebust | 2275cde | 2018-02-07 09:13:04 -0500 | [diff] [blame] | 542 | rpc_wake_up_queued_task_on_wq(xprtiod_workqueue, |
| 543 | &xprt->pending, xprt->snd_task); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 544 | } |
| 545 | spin_unlock_bh(&xprt->transport_lock); |
| 546 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 547 | EXPORT_SYMBOL_GPL(xprt_write_space); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 548 | |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 549 | /** |
| 550 | * xprt_set_retrans_timeout_def - set a request's retransmit timeout |
| 551 | * @task: task whose timeout is to be set |
| 552 | * |
| 553 | * Set a request's retransmit timeout based on the transport's |
| 554 | * default timeout parameters. Used by transports that don't adjust |
| 555 | * the retransmit timeout based on round-trip time estimation. |
| 556 | */ |
| 557 | void xprt_set_retrans_timeout_def(struct rpc_task *task) |
| 558 | { |
| 559 | task->tk_timeout = task->tk_rqstp->rq_timeout; |
| 560 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 561 | EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def); |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 562 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 563 | /** |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 564 | * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout |
| 565 | * @task: task whose timeout is to be set |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 566 | * |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 567 | * Set a request's retransmit timeout using the RTT estimator. |
| 568 | */ |
| 569 | void xprt_set_retrans_timeout_rtt(struct rpc_task *task) |
| 570 | { |
| 571 | int timer = task->tk_msg.rpc_proc->p_timer; |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 572 | struct rpc_clnt *clnt = task->tk_client; |
| 573 | struct rpc_rtt *rtt = clnt->cl_rtt; |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 574 | struct rpc_rqst *req = task->tk_rqstp; |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 575 | unsigned long max_timeout = clnt->cl_timeout->to_maxval; |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 576 | |
| 577 | task->tk_timeout = rpc_calc_rto(rtt, timer); |
| 578 | task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; |
| 579 | if (task->tk_timeout > max_timeout || task->tk_timeout == 0) |
| 580 | task->tk_timeout = max_timeout; |
| 581 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 582 | EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt); |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | static void xprt_reset_majortimeo(struct rpc_rqst *req) |
| 585 | { |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 586 | const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | req->rq_majortimeo = req->rq_timeout; |
| 589 | if (to->to_exponential) |
| 590 | req->rq_majortimeo <<= to->to_retries; |
| 591 | else |
| 592 | req->rq_majortimeo += to->to_increment * to->to_retries; |
| 593 | if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) |
| 594 | req->rq_majortimeo = to->to_maxval; |
| 595 | req->rq_majortimeo += jiffies; |
| 596 | } |
| 597 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 598 | /** |
| 599 | * xprt_adjust_timeout - adjust timeout values for next retransmit |
| 600 | * @req: RPC request containing parameters to use for the adjustment |
| 601 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | */ |
| 603 | int xprt_adjust_timeout(struct rpc_rqst *req) |
| 604 | { |
| 605 | struct rpc_xprt *xprt = req->rq_xprt; |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 606 | const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | int status = 0; |
| 608 | |
| 609 | if (time_before(jiffies, req->rq_majortimeo)) { |
| 610 | if (to->to_exponential) |
| 611 | req->rq_timeout <<= 1; |
| 612 | else |
| 613 | req->rq_timeout += to->to_increment; |
| 614 | if (to->to_maxval && req->rq_timeout >= to->to_maxval) |
| 615 | req->rq_timeout = to->to_maxval; |
| 616 | req->rq_retries++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | } else { |
| 618 | req->rq_timeout = to->to_initval; |
| 619 | req->rq_retries = 0; |
| 620 | xprt_reset_majortimeo(req); |
| 621 | /* Reset the RTT counters == "slow start" */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 622 | spin_lock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 624 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | status = -ETIMEDOUT; |
| 626 | } |
| 627 | |
| 628 | if (req->rq_timeout == 0) { |
| 629 | printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); |
| 630 | req->rq_timeout = 5 * HZ; |
| 631 | } |
| 632 | return status; |
| 633 | } |
| 634 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 635 | static void xprt_autoclose(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 637 | struct rpc_xprt *xprt = |
| 638 | container_of(work, struct rpc_xprt, task_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Trond Myklebust | 66af1e5 | 2007-11-06 10:18:36 -0500 | [diff] [blame] | 640 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
Trond Myklebust | 4876cc7 | 2015-06-19 16:17:57 -0400 | [diff] [blame] | 641 | xprt->ops->close(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | xprt_release_write(xprt, NULL); |
Trond Myklebust | 79234c3 | 2015-09-18 15:53:24 -0400 | [diff] [blame] | 643 | wake_up_bit(&xprt->state, XPRT_LOCKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 646 | /** |
Trond Myklebust | 62da3b2 | 2007-11-06 18:44:20 -0500 | [diff] [blame] | 647 | * xprt_disconnect_done - mark a transport as disconnected |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 648 | * @xprt: transport to flag for disconnect |
| 649 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | */ |
Trond Myklebust | 62da3b2 | 2007-11-06 18:44:20 -0500 | [diff] [blame] | 651 | void xprt_disconnect_done(struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 653 | dprintk("RPC: disconnected transport %p\n", xprt); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 654 | spin_lock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | xprt_clear_connected(xprt); |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 656 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 657 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
Trond Myklebust | 62da3b2 | 2007-11-06 18:44:20 -0500 | [diff] [blame] | 659 | EXPORT_SYMBOL_GPL(xprt_disconnect_done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Trond Myklebust | 66af1e5 | 2007-11-06 10:18:36 -0500 | [diff] [blame] | 661 | /** |
| 662 | * xprt_force_disconnect - force a transport to disconnect |
| 663 | * @xprt: transport to disconnect |
| 664 | * |
| 665 | */ |
| 666 | void xprt_force_disconnect(struct rpc_xprt *xprt) |
| 667 | { |
| 668 | /* Don't race with the test_bit() in xprt_clear_locked() */ |
| 669 | spin_lock_bh(&xprt->transport_lock); |
| 670 | set_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 671 | /* Try to schedule an autoclose RPC call */ |
| 672 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) |
Trond Myklebust | 40a5f1b | 2016-05-27 10:39:50 -0400 | [diff] [blame] | 673 | queue_work(xprtiod_workqueue, &xprt->task_cleanup); |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 674 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
Trond Myklebust | 66af1e5 | 2007-11-06 10:18:36 -0500 | [diff] [blame] | 675 | spin_unlock_bh(&xprt->transport_lock); |
| 676 | } |
Chuck Lever | e2a4f4f | 2017-04-11 13:22:38 -0400 | [diff] [blame] | 677 | EXPORT_SYMBOL_GPL(xprt_force_disconnect); |
Trond Myklebust | 66af1e5 | 2007-11-06 10:18:36 -0500 | [diff] [blame] | 678 | |
Trond Myklebust | 7f3a1d1 | 2018-08-23 00:03:43 -0400 | [diff] [blame] | 679 | static unsigned int |
| 680 | xprt_connect_cookie(struct rpc_xprt *xprt) |
| 681 | { |
| 682 | return READ_ONCE(xprt->connect_cookie); |
| 683 | } |
| 684 | |
| 685 | static bool |
| 686 | xprt_request_retransmit_after_disconnect(struct rpc_task *task) |
| 687 | { |
| 688 | struct rpc_rqst *req = task->tk_rqstp; |
| 689 | struct rpc_xprt *xprt = req->rq_xprt; |
| 690 | |
| 691 | return req->rq_connect_cookie != xprt_connect_cookie(xprt) || |
| 692 | !xprt_connected(xprt); |
| 693 | } |
| 694 | |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 695 | /** |
| 696 | * xprt_conditional_disconnect - force a transport to disconnect |
| 697 | * @xprt: transport to disconnect |
| 698 | * @cookie: 'connection cookie' |
| 699 | * |
| 700 | * This attempts to break the connection if and only if 'cookie' matches |
| 701 | * the current transport 'connection cookie'. It ensures that we don't |
| 702 | * try to break the connection more than once when we need to retransmit |
| 703 | * a batch of RPC requests. |
| 704 | * |
| 705 | */ |
| 706 | void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) |
| 707 | { |
| 708 | /* Don't race with the test_bit() in xprt_clear_locked() */ |
| 709 | spin_lock_bh(&xprt->transport_lock); |
| 710 | if (cookie != xprt->connect_cookie) |
| 711 | goto out; |
NeilBrown | 2c2ee6d | 2016-11-23 14:44:58 +1100 | [diff] [blame] | 712 | if (test_bit(XPRT_CLOSING, &xprt->state)) |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 713 | goto out; |
| 714 | set_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 715 | /* Try to schedule an autoclose RPC call */ |
| 716 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) |
Trond Myklebust | 40a5f1b | 2016-05-27 10:39:50 -0400 | [diff] [blame] | 717 | queue_work(xprtiod_workqueue, &xprt->task_cleanup); |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 718 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 719 | out: |
| 720 | spin_unlock_bh(&xprt->transport_lock); |
| 721 | } |
| 722 | |
Trond Myklebust | ad3331a | 2016-08-02 13:47:43 -0400 | [diff] [blame] | 723 | static bool |
| 724 | xprt_has_timer(const struct rpc_xprt *xprt) |
| 725 | { |
| 726 | return xprt->idle_timeout != 0; |
| 727 | } |
| 728 | |
| 729 | static void |
| 730 | xprt_schedule_autodisconnect(struct rpc_xprt *xprt) |
| 731 | __must_hold(&xprt->transport_lock) |
| 732 | { |
Trond Myklebust | ef3f5434 | 2018-08-08 09:23:32 -0400 | [diff] [blame] | 733 | if (list_empty(&xprt->recv_queue) && xprt_has_timer(xprt)) |
Trond Myklebust | ad3331a | 2016-08-02 13:47:43 -0400 | [diff] [blame] | 734 | mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout); |
| 735 | } |
| 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | static void |
Kees Cook | ff861c4 | 2017-10-16 17:29:42 -0700 | [diff] [blame] | 738 | xprt_init_autodisconnect(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
Kees Cook | ff861c4 | 2017-10-16 17:29:42 -0700 | [diff] [blame] | 740 | struct rpc_xprt *xprt = from_timer(xprt, t, timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 742 | spin_lock(&xprt->transport_lock); |
Trond Myklebust | ef3f5434 | 2018-08-08 09:23:32 -0400 | [diff] [blame] | 743 | if (!list_empty(&xprt->recv_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | goto out_abort; |
Trond Myklebust | ad3331a | 2016-08-02 13:47:43 -0400 | [diff] [blame] | 745 | /* Reset xprt->last_used to avoid connect/autodisconnect cycling */ |
| 746 | xprt->last_used = jiffies; |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 747 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | goto out_abort; |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 749 | spin_unlock(&xprt->transport_lock); |
Trond Myklebust | 40a5f1b | 2016-05-27 10:39:50 -0400 | [diff] [blame] | 750 | queue_work(xprtiod_workqueue, &xprt->task_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | return; |
| 752 | out_abort: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 753 | spin_unlock(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Trond Myklebust | 718ba5b | 2015-02-08 18:19:25 -0500 | [diff] [blame] | 756 | bool xprt_lock_connect(struct rpc_xprt *xprt, |
| 757 | struct rpc_task *task, |
| 758 | void *cookie) |
| 759 | { |
| 760 | bool ret = false; |
| 761 | |
| 762 | spin_lock_bh(&xprt->transport_lock); |
| 763 | if (!test_bit(XPRT_LOCKED, &xprt->state)) |
| 764 | goto out; |
| 765 | if (xprt->snd_task != task) |
| 766 | goto out; |
| 767 | xprt->snd_task = cookie; |
| 768 | ret = true; |
| 769 | out: |
| 770 | spin_unlock_bh(&xprt->transport_lock); |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie) |
| 775 | { |
| 776 | spin_lock_bh(&xprt->transport_lock); |
| 777 | if (xprt->snd_task != cookie) |
| 778 | goto out; |
| 779 | if (!test_bit(XPRT_LOCKED, &xprt->state)) |
| 780 | goto out; |
| 781 | xprt->snd_task =NULL; |
| 782 | xprt->ops->release_xprt(xprt, NULL); |
Trond Myklebust | ad3331a | 2016-08-02 13:47:43 -0400 | [diff] [blame] | 783 | xprt_schedule_autodisconnect(xprt); |
Trond Myklebust | 718ba5b | 2015-02-08 18:19:25 -0500 | [diff] [blame] | 784 | out: |
| 785 | spin_unlock_bh(&xprt->transport_lock); |
Trond Myklebust | 79234c3 | 2015-09-18 15:53:24 -0400 | [diff] [blame] | 786 | wake_up_bit(&xprt->state, XPRT_LOCKED); |
Trond Myklebust | 718ba5b | 2015-02-08 18:19:25 -0500 | [diff] [blame] | 787 | } |
| 788 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 789 | /** |
| 790 | * xprt_connect - schedule a transport connect operation |
| 791 | * @task: RPC task that is requesting the connect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | * |
| 793 | */ |
| 794 | void xprt_connect(struct rpc_task *task) |
| 795 | { |
Trond Myklebust | ad2368d | 2013-01-08 10:08:33 -0500 | [diff] [blame] | 796 | struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 798 | dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | xprt, (xprt_connected(xprt) ? "is" : "is not")); |
| 800 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 801 | if (!xprt_bound(xprt)) { |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 802 | task->tk_status = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | return; |
| 804 | } |
| 805 | if (!xprt_lock_write(xprt, task)) |
| 806 | return; |
Trond Myklebust | feb8ca3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 807 | |
| 808 | if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) |
| 809 | xprt->ops->close(xprt); |
| 810 | |
Trond Myklebust | 718ba5b | 2015-02-08 18:19:25 -0500 | [diff] [blame] | 811 | if (!xprt_connected(xprt)) { |
Trond Myklebust | a8ce4a8 | 2010-04-16 16:42:12 -0400 | [diff] [blame] | 812 | task->tk_timeout = task->tk_rqstp->rq_timeout; |
NeilBrown | 2c2ee6d | 2016-11-23 14:44:58 +1100 | [diff] [blame] | 813 | task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie; |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 814 | rpc_sleep_on(&xprt->pending, task, xprt_connect_status); |
Trond Myklebust | 0b9e794 | 2010-04-16 16:41:57 -0400 | [diff] [blame] | 815 | |
| 816 | if (test_bit(XPRT_CLOSING, &xprt->state)) |
| 817 | return; |
| 818 | if (xprt_test_and_set_connecting(xprt)) |
| 819 | return; |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 820 | xprt->stat.connect_start = jiffies; |
Trond Myklebust | 1b09209 | 2013-01-08 09:26:49 -0500 | [diff] [blame] | 821 | xprt->ops->connect(xprt, task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
Trond Myklebust | 718ba5b | 2015-02-08 18:19:25 -0500 | [diff] [blame] | 823 | xprt_release_write(xprt, task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 826 | static void xprt_connect_status(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | { |
Trond Myklebust | ad2368d | 2013-01-08 10:08:33 -0500 | [diff] [blame] | 828 | struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Chuck Lever | cd983ef | 2008-06-11 17:56:13 -0400 | [diff] [blame] | 830 | if (task->tk_status == 0) { |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 831 | xprt->stat.connect_count++; |
| 832 | xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 833 | dprintk("RPC: %5u xprt_connect_status: connection established\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | task->tk_pid); |
| 835 | return; |
| 836 | } |
| 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | switch (task->tk_status) { |
Trond Myklebust | 0fe8d04 | 2013-12-31 13:13:30 -0500 | [diff] [blame] | 839 | case -ECONNREFUSED: |
| 840 | case -ECONNRESET: |
| 841 | case -ECONNABORTED: |
| 842 | case -ENETUNREACH: |
| 843 | case -EHOSTUNREACH: |
Trond Myklebust | 2fc193c | 2014-07-03 00:02:57 -0400 | [diff] [blame] | 844 | case -EPIPE: |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 845 | case -EAGAIN: |
| 846 | dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid); |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 847 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | case -ETIMEDOUT: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 849 | dprintk("RPC: %5u xprt_connect_status: connect attempt timed " |
| 850 | "out\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | break; |
| 852 | default: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 853 | dprintk("RPC: %5u xprt_connect_status: error %d connecting to " |
| 854 | "server %s\n", task->tk_pid, -task->tk_status, |
Trond Myklebust | 4e0038b | 2012-03-01 17:01:05 -0500 | [diff] [blame] | 855 | xprt->servername); |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 856 | task->tk_status = -EIO; |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 857 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 860 | /** |
| 861 | * xprt_lookup_rqst - find an RPC request corresponding to an XID |
| 862 | * @xprt: transport on which the original request was transmitted |
| 863 | * @xid: RPC XID of incoming reply |
| 864 | * |
Trond Myklebust | 75c8415 | 2018-08-31 10:21:00 -0400 | [diff] [blame] | 865 | * Caller holds xprt->queue_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 867 | struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | { |
Pavel Emelyanov | 8f3a6de | 2010-10-05 23:30:19 +0400 | [diff] [blame] | 869 | struct rpc_rqst *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
Trond Myklebust | ef3f5434 | 2018-08-08 09:23:32 -0400 | [diff] [blame] | 871 | list_for_each_entry(entry, &xprt->recv_queue, rq_recv) |
Jeff Layton | 3705ad6 | 2014-10-28 14:24:13 -0400 | [diff] [blame] | 872 | if (entry->rq_xid == xid) { |
| 873 | trace_xprt_lookup_rqst(xprt, xid, 0); |
Chuck Lever | 0b87a46 | 2018-03-05 15:13:02 -0500 | [diff] [blame] | 874 | entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 875 | return entry; |
Jeff Layton | 3705ad6 | 2014-10-28 14:24:13 -0400 | [diff] [blame] | 876 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 877 | |
| 878 | dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", |
| 879 | ntohl(xid)); |
Jeff Layton | 3705ad6 | 2014-10-28 14:24:13 -0400 | [diff] [blame] | 880 | trace_xprt_lookup_rqst(xprt, xid, -ENOENT); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 881 | xprt->stat.bad_xids++; |
| 882 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 884 | EXPORT_SYMBOL_GPL(xprt_lookup_rqst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
Trond Myklebust | cf9946c | 2018-08-06 12:55:34 -0400 | [diff] [blame] | 886 | static bool |
| 887 | xprt_is_pinned_rqst(struct rpc_rqst *req) |
| 888 | { |
| 889 | return atomic_read(&req->rq_pin) != 0; |
| 890 | } |
| 891 | |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 892 | /** |
| 893 | * xprt_pin_rqst - Pin a request on the transport receive list |
| 894 | * @req: Request to pin |
| 895 | * |
| 896 | * Caller must ensure this is atomic with the call to xprt_lookup_rqst() |
Trond Myklebust | cf9946c | 2018-08-06 12:55:34 -0400 | [diff] [blame] | 897 | * so should be holding the xprt receive lock. |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 898 | */ |
| 899 | void xprt_pin_rqst(struct rpc_rqst *req) |
| 900 | { |
Trond Myklebust | cf9946c | 2018-08-06 12:55:34 -0400 | [diff] [blame] | 901 | atomic_inc(&req->rq_pin); |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 902 | } |
Chuck Lever | 9590d08 | 2017-08-23 17:05:58 -0400 | [diff] [blame] | 903 | EXPORT_SYMBOL_GPL(xprt_pin_rqst); |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 904 | |
| 905 | /** |
| 906 | * xprt_unpin_rqst - Unpin a request on the transport receive list |
| 907 | * @req: Request to pin |
| 908 | * |
Trond Myklebust | cf9946c | 2018-08-06 12:55:34 -0400 | [diff] [blame] | 909 | * Caller should be holding the xprt receive lock. |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 910 | */ |
| 911 | void xprt_unpin_rqst(struct rpc_rqst *req) |
| 912 | { |
Trond Myklebust | cf9946c | 2018-08-06 12:55:34 -0400 | [diff] [blame] | 913 | if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) { |
| 914 | atomic_dec(&req->rq_pin); |
| 915 | return; |
| 916 | } |
| 917 | if (atomic_dec_and_test(&req->rq_pin)) |
| 918 | wake_up_var(&req->rq_pin); |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 919 | } |
Chuck Lever | 9590d08 | 2017-08-23 17:05:58 -0400 | [diff] [blame] | 920 | EXPORT_SYMBOL_GPL(xprt_unpin_rqst); |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 921 | |
| 922 | static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req) |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 923 | { |
Trond Myklebust | cf9946c | 2018-08-06 12:55:34 -0400 | [diff] [blame] | 924 | wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req)); |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 925 | } |
| 926 | |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 927 | static bool |
| 928 | xprt_request_data_received(struct rpc_task *task) |
| 929 | { |
| 930 | return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && |
| 931 | READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0; |
| 932 | } |
| 933 | |
| 934 | static bool |
| 935 | xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req) |
| 936 | { |
| 937 | return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && |
| 938 | READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0; |
| 939 | } |
| 940 | |
| 941 | /** |
| 942 | * xprt_request_enqueue_receive - Add an request to the receive queue |
| 943 | * @task: RPC task |
| 944 | * |
| 945 | */ |
| 946 | void |
| 947 | xprt_request_enqueue_receive(struct rpc_task *task) |
| 948 | { |
| 949 | struct rpc_rqst *req = task->tk_rqstp; |
| 950 | struct rpc_xprt *xprt = req->rq_xprt; |
| 951 | |
| 952 | if (!xprt_request_need_enqueue_receive(task, req)) |
| 953 | return; |
| 954 | spin_lock(&xprt->queue_lock); |
| 955 | |
| 956 | /* Update the softirq receive buffer */ |
| 957 | memcpy(&req->rq_private_buf, &req->rq_rcv_buf, |
| 958 | sizeof(req->rq_private_buf)); |
| 959 | |
| 960 | /* Add request to the receive list */ |
Trond Myklebust | ef3f5434 | 2018-08-08 09:23:32 -0400 | [diff] [blame] | 961 | list_add_tail(&req->rq_recv, &xprt->recv_queue); |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 962 | set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate); |
| 963 | spin_unlock(&xprt->queue_lock); |
| 964 | |
| 965 | xprt_reset_majortimeo(req); |
| 966 | /* Turn off autodisconnect */ |
| 967 | del_singleshot_timer_sync(&xprt->timer); |
| 968 | } |
| 969 | |
| 970 | /** |
| 971 | * xprt_request_dequeue_receive_locked - Remove a request from the receive queue |
| 972 | * @task: RPC task |
| 973 | * |
| 974 | * Caller must hold xprt->queue_lock. |
| 975 | */ |
| 976 | static void |
| 977 | xprt_request_dequeue_receive_locked(struct rpc_task *task) |
| 978 | { |
| 979 | if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) |
Trond Myklebust | ef3f5434 | 2018-08-08 09:23:32 -0400 | [diff] [blame] | 980 | list_del(&task->tk_rqstp->rq_recv); |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 981 | } |
| 982 | |
Chuck Lever | ecd465e | 2018-03-05 15:12:57 -0500 | [diff] [blame] | 983 | /** |
| 984 | * xprt_update_rtt - Update RPC RTT statistics |
| 985 | * @task: RPC request that recently completed |
| 986 | * |
Trond Myklebust | 75c8415 | 2018-08-31 10:21:00 -0400 | [diff] [blame] | 987 | * Caller holds xprt->queue_lock. |
Chuck Lever | ecd465e | 2018-03-05 15:12:57 -0500 | [diff] [blame] | 988 | */ |
| 989 | void xprt_update_rtt(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | { |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 991 | struct rpc_rqst *req = task->tk_rqstp; |
| 992 | struct rpc_rtt *rtt = task->tk_client->cl_rtt; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 993 | unsigned int timer = task->tk_msg.rpc_proc->p_timer; |
Trond Myklebust | d60dbb2 | 2010-05-13 12:51:49 -0400 | [diff] [blame] | 994 | long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 996 | if (timer) { |
| 997 | if (req->rq_ntrans == 1) |
Chuck Lever | ff83997 | 2010-05-07 13:34:47 -0400 | [diff] [blame] | 998 | rpc_update_rtt(rtt, timer, m); |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 999 | rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | } |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1001 | } |
Chuck Lever | ecd465e | 2018-03-05 15:12:57 -0500 | [diff] [blame] | 1002 | EXPORT_SYMBOL_GPL(xprt_update_rtt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1004 | /** |
| 1005 | * xprt_complete_rqst - called when reply processing is complete |
| 1006 | * @task: RPC request that recently completed |
| 1007 | * @copied: actual number of bytes received from the transport |
| 1008 | * |
Trond Myklebust | 75c8415 | 2018-08-31 10:21:00 -0400 | [diff] [blame] | 1009 | * Caller holds xprt->queue_lock. |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1010 | */ |
| 1011 | void xprt_complete_rqst(struct rpc_task *task, int copied) |
| 1012 | { |
| 1013 | struct rpc_rqst *req = task->tk_rqstp; |
Trond Myklebust | fda1393 | 2008-02-22 16:34:12 -0500 | [diff] [blame] | 1014 | struct rpc_xprt *xprt = req->rq_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1016 | dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", |
| 1017 | task->tk_pid, ntohl(req->rq_xid), copied); |
Jeff Layton | 3705ad6 | 2014-10-28 14:24:13 -0400 | [diff] [blame] | 1018 | trace_xprt_complete_rqst(xprt, req->rq_xid, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
Trond Myklebust | fda1393 | 2008-02-22 16:34:12 -0500 | [diff] [blame] | 1020 | xprt->stat.recvs++; |
Chuck Lever | ef759a2 | 2006-03-20 13:44:17 -0500 | [diff] [blame] | 1021 | |
Trond Myklebust | 1e799b6 | 2008-03-21 16:19:41 -0400 | [diff] [blame] | 1022 | req->rq_private_buf.len = copied; |
Ricardo Labiaga | dd2b63d | 2009-04-01 09:23:28 -0400 | [diff] [blame] | 1023 | /* Ensure all writes are done before we update */ |
| 1024 | /* req->rq_reply_bytes_recvd */ |
Trond Myklebust | 43ac3f2 | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1025 | smp_wmb(); |
Ricardo Labiaga | dd2b63d | 2009-04-01 09:23:28 -0400 | [diff] [blame] | 1026 | req->rq_reply_bytes_recvd = copied; |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 1027 | xprt_request_dequeue_receive_locked(task); |
Trond Myklebust | fda1393 | 2008-02-22 16:34:12 -0500 | [diff] [blame] | 1028 | rpc_wake_up_queued_task(&xprt->pending, task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 1030 | EXPORT_SYMBOL_GPL(xprt_complete_rqst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1032 | static void xprt_timer(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1034 | struct rpc_rqst *req = task->tk_rqstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | struct rpc_xprt *xprt = req->rq_xprt; |
| 1036 | |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 1037 | if (task->tk_status != -ETIMEDOUT) |
| 1038 | return; |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1039 | |
Chuck Lever | 82476d9 | 2018-01-03 15:38:25 -0500 | [diff] [blame] | 1040 | trace_xprt_timer(xprt, req->rq_xid, task->tk_status); |
Ricardo Labiaga | dd2b63d | 2009-04-01 09:23:28 -0400 | [diff] [blame] | 1041 | if (!req->rq_reply_bytes_recvd) { |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1042 | if (xprt->ops->timer) |
Trond Myklebust | 6a24dfb | 2013-01-08 09:48:15 -0500 | [diff] [blame] | 1043 | xprt->ops->timer(xprt, task); |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 1044 | } else |
| 1045 | task->tk_status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1048 | /** |
Trond Myklebust | 7f3a1d1 | 2018-08-23 00:03:43 -0400 | [diff] [blame] | 1049 | * xprt_request_wait_receive - wait for the reply to an RPC request |
| 1050 | * @task: RPC task about to send a request |
| 1051 | * |
| 1052 | */ |
| 1053 | void xprt_request_wait_receive(struct rpc_task *task) |
| 1054 | { |
| 1055 | struct rpc_rqst *req = task->tk_rqstp; |
| 1056 | struct rpc_xprt *xprt = req->rq_xprt; |
| 1057 | |
| 1058 | if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) |
| 1059 | return; |
| 1060 | /* |
| 1061 | * Sleep on the pending queue if we're expecting a reply. |
| 1062 | * The spinlock ensures atomicity between the test of |
| 1063 | * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on(). |
| 1064 | */ |
| 1065 | spin_lock(&xprt->queue_lock); |
| 1066 | if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) { |
| 1067 | xprt->ops->set_retrans_timeout(task); |
| 1068 | rpc_sleep_on(&xprt->pending, task, xprt_timer); |
| 1069 | /* |
| 1070 | * Send an extra queue wakeup call if the |
| 1071 | * connection was dropped in case the call to |
| 1072 | * rpc_sleep_on() raced. |
| 1073 | */ |
| 1074 | if (xprt_request_retransmit_after_disconnect(task)) |
| 1075 | rpc_wake_up_queued_task_set_status(&xprt->pending, |
| 1076 | task, -ENOTCONN); |
| 1077 | } |
| 1078 | spin_unlock(&xprt->queue_lock); |
| 1079 | } |
| 1080 | |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1081 | static bool |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1082 | xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req) |
| 1083 | { |
Trond Myklebust | 762e4e6 | 2018-08-24 16:28:28 -0400 | [diff] [blame] | 1084 | return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * xprt_request_enqueue_transmit - queue a task for transmission |
| 1089 | * @task: pointer to rpc_task |
| 1090 | * |
| 1091 | * Add a task to the transmission queue. |
| 1092 | */ |
| 1093 | void |
| 1094 | xprt_request_enqueue_transmit(struct rpc_task *task) |
| 1095 | { |
Trond Myklebust | 918f3c1 | 2018-09-09 11:37:22 -0400 | [diff] [blame] | 1096 | struct rpc_rqst *pos, *req = task->tk_rqstp; |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1097 | struct rpc_xprt *xprt = req->rq_xprt; |
| 1098 | |
| 1099 | if (xprt_request_need_enqueue_transmit(task, req)) { |
| 1100 | spin_lock(&xprt->queue_lock); |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 1101 | /* |
| 1102 | * Requests that carry congestion control credits are added |
| 1103 | * to the head of the list to avoid starvation issues. |
| 1104 | */ |
| 1105 | if (req->rq_cong) { |
| 1106 | xprt_clear_congestion_window_wait(xprt); |
| 1107 | list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { |
| 1108 | if (pos->rq_cong) |
| 1109 | continue; |
| 1110 | /* Note: req is added _before_ pos */ |
| 1111 | list_add_tail(&req->rq_xmit, &pos->rq_xmit); |
| 1112 | INIT_LIST_HEAD(&req->rq_xmit2); |
| 1113 | goto out; |
| 1114 | } |
Trond Myklebust | 86aeee0 | 2018-09-08 14:22:41 -0400 | [diff] [blame] | 1115 | } else if (RPC_IS_SWAPPER(task)) { |
| 1116 | list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { |
| 1117 | if (pos->rq_cong || pos->rq_bytes_sent) |
| 1118 | continue; |
| 1119 | if (RPC_IS_SWAPPER(pos->rq_task)) |
| 1120 | continue; |
| 1121 | /* Note: req is added _before_ pos */ |
| 1122 | list_add_tail(&req->rq_xmit, &pos->rq_xmit); |
| 1123 | INIT_LIST_HEAD(&req->rq_xmit2); |
| 1124 | goto out; |
| 1125 | } |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 1126 | } else { |
| 1127 | list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { |
| 1128 | if (pos->rq_task->tk_owner != task->tk_owner) |
| 1129 | continue; |
| 1130 | list_add_tail(&req->rq_xmit2, &pos->rq_xmit2); |
| 1131 | INIT_LIST_HEAD(&req->rq_xmit); |
| 1132 | goto out; |
| 1133 | } |
Trond Myklebust | 918f3c1 | 2018-09-09 11:37:22 -0400 | [diff] [blame] | 1134 | } |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1135 | list_add_tail(&req->rq_xmit, &xprt->xmit_queue); |
Trond Myklebust | 918f3c1 | 2018-09-09 11:37:22 -0400 | [diff] [blame] | 1136 | INIT_LIST_HEAD(&req->rq_xmit2); |
| 1137 | out: |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1138 | set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); |
| 1139 | spin_unlock(&xprt->queue_lock); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | /** |
| 1144 | * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue |
| 1145 | * @task: pointer to rpc_task |
| 1146 | * |
| 1147 | * Remove a task from the transmission queue |
| 1148 | * Caller must hold xprt->queue_lock |
| 1149 | */ |
| 1150 | static void |
| 1151 | xprt_request_dequeue_transmit_locked(struct rpc_task *task) |
| 1152 | { |
Trond Myklebust | 918f3c1 | 2018-09-09 11:37:22 -0400 | [diff] [blame] | 1153 | struct rpc_rqst *req = task->tk_rqstp; |
| 1154 | |
| 1155 | if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) |
| 1156 | return; |
| 1157 | if (!list_empty(&req->rq_xmit)) { |
| 1158 | list_del(&req->rq_xmit); |
| 1159 | if (!list_empty(&req->rq_xmit2)) { |
| 1160 | struct rpc_rqst *next = list_first_entry(&req->rq_xmit2, |
| 1161 | struct rpc_rqst, rq_xmit2); |
| 1162 | list_del(&req->rq_xmit2); |
| 1163 | list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue); |
| 1164 | } |
| 1165 | } else |
| 1166 | list_del(&req->rq_xmit2); |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /** |
| 1170 | * xprt_request_dequeue_transmit - remove a task from the transmission queue |
| 1171 | * @task: pointer to rpc_task |
| 1172 | * |
| 1173 | * Remove a task from the transmission queue |
| 1174 | */ |
| 1175 | static void |
| 1176 | xprt_request_dequeue_transmit(struct rpc_task *task) |
| 1177 | { |
| 1178 | struct rpc_rqst *req = task->tk_rqstp; |
| 1179 | struct rpc_xprt *xprt = req->rq_xprt; |
| 1180 | |
| 1181 | spin_lock(&xprt->queue_lock); |
| 1182 | xprt_request_dequeue_transmit_locked(task); |
| 1183 | spin_unlock(&xprt->queue_lock); |
| 1184 | } |
| 1185 | |
Trond Myklebust | 7f3a1d1 | 2018-08-23 00:03:43 -0400 | [diff] [blame] | 1186 | /** |
Trond Myklebust | 762e4e6 | 2018-08-24 16:28:28 -0400 | [diff] [blame] | 1187 | * xprt_request_need_retransmit - Test if a task needs retransmission |
| 1188 | * @task: pointer to rpc_task |
| 1189 | * |
| 1190 | * Test for whether a connection breakage requires the task to retransmit |
| 1191 | */ |
| 1192 | bool |
| 1193 | xprt_request_need_retransmit(struct rpc_task *task) |
| 1194 | { |
| 1195 | return xprt_request_retransmit_after_disconnect(task); |
| 1196 | } |
| 1197 | |
| 1198 | /** |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1199 | * xprt_prepare_transmit - reserve the transport before sending a request |
| 1200 | * @task: RPC task about to send a request |
| 1201 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | */ |
Trond Myklebust | 90051ea | 2013-09-25 12:17:18 -0400 | [diff] [blame] | 1203 | bool xprt_prepare_transmit(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | { |
| 1205 | struct rpc_rqst *req = task->tk_rqstp; |
| 1206 | struct rpc_xprt *xprt = req->rq_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1208 | dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | |
Trond Myklebust | 5f2f6bd | 2018-09-01 14:25:24 -0400 | [diff] [blame] | 1210 | if (!xprt_lock_write(xprt, task)) { |
| 1211 | /* Race breaker: someone may have transmitted us */ |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1212 | if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) |
Trond Myklebust | 5f2f6bd | 2018-09-01 14:25:24 -0400 | [diff] [blame] | 1213 | rpc_wake_up_queued_task_set_status(&xprt->sending, |
| 1214 | task, 0); |
| 1215 | return false; |
| 1216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | } |
Trond Myklebust | 5f2f6bd | 2018-09-01 14:25:24 -0400 | [diff] [blame] | 1218 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 1221 | void xprt_end_transmit(struct rpc_task *task) |
Trond Myklebust | 5e5ce5b | 2005-10-18 14:20:11 -0700 | [diff] [blame] | 1222 | { |
Rahul Iyer | 343952f | 2009-04-01 09:23:17 -0400 | [diff] [blame] | 1223 | xprt_release_write(task->tk_rqstp->rq_xprt, task); |
Trond Myklebust | 5e5ce5b | 2005-10-18 14:20:11 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1226 | /** |
Trond Myklebust | 89f90fe | 2018-08-29 17:40:55 -0400 | [diff] [blame] | 1227 | * xprt_request_transmit - send an RPC request on a transport |
| 1228 | * @req: pointer to request to transmit |
| 1229 | * @snd_task: RPC task that owns the transport lock |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1230 | * |
Trond Myklebust | 89f90fe | 2018-08-29 17:40:55 -0400 | [diff] [blame] | 1231 | * This performs the transmission of a single request. |
| 1232 | * Note that if the request is not the same as snd_task, then it |
| 1233 | * does need to be pinned. |
| 1234 | * Returns '0' on success. |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1235 | */ |
Trond Myklebust | 89f90fe | 2018-08-29 17:40:55 -0400 | [diff] [blame] | 1236 | static int |
| 1237 | xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | { |
Trond Myklebust | 89f90fe | 2018-08-29 17:40:55 -0400 | [diff] [blame] | 1239 | struct rpc_xprt *xprt = req->rq_xprt; |
| 1240 | struct rpc_task *task = req->rq_task; |
Trond Myklebust | 90d91b0 | 2017-12-14 21:24:08 -0500 | [diff] [blame] | 1241 | unsigned int connect_cookie; |
Trond Myklebust | dcbbeda | 2018-09-01 14:29:18 -0400 | [diff] [blame] | 1242 | int is_retrans = RPC_WAS_SENT(task); |
Chuck Lever | ff699ea8 | 2018-03-05 15:13:13 -0500 | [diff] [blame] | 1243 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1245 | dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 1247 | if (!req->rq_bytes_sent) { |
Trond Myklebust | 89f90fe | 2018-08-29 17:40:55 -0400 | [diff] [blame] | 1248 | if (xprt_request_data_received(task)) { |
| 1249 | status = 0; |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1250 | goto out_dequeue; |
Trond Myklebust | 89f90fe | 2018-08-29 17:40:55 -0400 | [diff] [blame] | 1251 | } |
Trond Myklebust | 3021a5bb | 2018-08-14 13:50:21 -0400 | [diff] [blame] | 1252 | /* Verify that our message lies in the RPCSEC_GSS window */ |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 1253 | if (rpcauth_xmit_need_reencode(task)) { |
Trond Myklebust | 89f90fe | 2018-08-29 17:40:55 -0400 | [diff] [blame] | 1254 | status = -EBADMSG; |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1255 | goto out_dequeue; |
Trond Myklebust | 3021a5bb | 2018-08-14 13:50:21 -0400 | [diff] [blame] | 1256 | } |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 1257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
Trond Myklebust | dcbbeda | 2018-09-01 14:29:18 -0400 | [diff] [blame] | 1259 | /* |
| 1260 | * Update req->rq_ntrans before transmitting to avoid races with |
| 1261 | * xprt_update_rtt(), which needs to know that it is recording a |
| 1262 | * reply to the first transmission. |
| 1263 | */ |
| 1264 | req->rq_ntrans++; |
| 1265 | |
Trond Myklebust | 90d91b0 | 2017-12-14 21:24:08 -0500 | [diff] [blame] | 1266 | connect_cookie = xprt->connect_cookie; |
Trond Myklebust | 89f90fe | 2018-08-29 17:40:55 -0400 | [diff] [blame] | 1267 | status = xprt->ops->send_request(req, snd_task); |
Jeff Layton | 3705ad6 | 2014-10-28 14:24:13 -0400 | [diff] [blame] | 1268 | trace_xprt_transmit(xprt, req->rq_xid, status); |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 1269 | if (status != 0) { |
Trond Myklebust | dcbbeda | 2018-09-01 14:29:18 -0400 | [diff] [blame] | 1270 | req->rq_ntrans--; |
Trond Myklebust | 89f90fe | 2018-08-29 17:40:55 -0400 | [diff] [blame] | 1271 | return status; |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 1272 | } |
Trond Myklebust | 7ebbbc6 | 2018-08-28 09:00:27 -0400 | [diff] [blame] | 1273 | |
Trond Myklebust | dcbbeda | 2018-09-01 14:29:18 -0400 | [diff] [blame] | 1274 | if (is_retrans) |
| 1275 | task->tk_client->cl_stats->rpcretrans++; |
| 1276 | |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 1277 | xprt_inject_disconnect(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 1279 | dprintk("RPC: %5u xmit complete\n", task->tk_pid); |
Bryan Schumaker | 468f861 | 2011-04-18 15:57:32 -0400 | [diff] [blame] | 1280 | task->tk_flags |= RPC_TASK_SENT; |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 1281 | spin_lock_bh(&xprt->transport_lock); |
| 1282 | |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 1283 | xprt->stat.sends++; |
| 1284 | xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; |
| 1285 | xprt->stat.bklog_u += xprt->backlog.qlen; |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 1286 | xprt->stat.sending_u += xprt->sending.qlen; |
| 1287 | xprt->stat.pending_u += xprt->pending.qlen; |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 1288 | spin_unlock_bh(&xprt->transport_lock); |
Trond Myklebust | 90d91b0 | 2017-12-14 21:24:08 -0500 | [diff] [blame] | 1289 | |
| 1290 | req->rq_connect_cookie = connect_cookie; |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1291 | out_dequeue: |
| 1292 | xprt_request_dequeue_transmit(task); |
Trond Myklebust | 89f90fe | 2018-08-29 17:40:55 -0400 | [diff] [blame] | 1293 | rpc_wake_up_queued_task_set_status(&xprt->sending, task, status); |
| 1294 | return status; |
| 1295 | } |
| 1296 | |
| 1297 | /** |
| 1298 | * xprt_transmit - send an RPC request on a transport |
| 1299 | * @task: controlling RPC task |
| 1300 | * |
| 1301 | * Attempts to drain the transmit queue. On exit, either the transport |
| 1302 | * signalled an error that needs to be handled before transmission can |
| 1303 | * resume, or @task finished transmitting, and detected that it already |
| 1304 | * received a reply. |
| 1305 | */ |
| 1306 | void |
| 1307 | xprt_transmit(struct rpc_task *task) |
| 1308 | { |
| 1309 | struct rpc_rqst *next, *req = task->tk_rqstp; |
| 1310 | struct rpc_xprt *xprt = req->rq_xprt; |
| 1311 | int status; |
| 1312 | |
| 1313 | spin_lock(&xprt->queue_lock); |
| 1314 | while (!list_empty(&xprt->xmit_queue)) { |
| 1315 | next = list_first_entry(&xprt->xmit_queue, |
| 1316 | struct rpc_rqst, rq_xmit); |
| 1317 | xprt_pin_rqst(next); |
| 1318 | spin_unlock(&xprt->queue_lock); |
| 1319 | status = xprt_request_transmit(next, task); |
| 1320 | if (status == -EBADMSG && next != req) |
| 1321 | status = 0; |
| 1322 | cond_resched(); |
| 1323 | spin_lock(&xprt->queue_lock); |
| 1324 | xprt_unpin_rqst(next); |
| 1325 | if (status == 0) { |
| 1326 | if (!xprt_request_data_received(task) || |
| 1327 | test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) |
| 1328 | continue; |
| 1329 | } else if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) |
| 1330 | rpc_wake_up_queued_task(&xprt->pending, task); |
| 1331 | else |
| 1332 | task->tk_status = status; |
| 1333 | break; |
| 1334 | } |
| 1335 | spin_unlock(&xprt->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
Trond Myklebust | ba60eb2 | 2013-04-14 10:49:37 -0400 | [diff] [blame] | 1338 | static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) |
| 1339 | { |
| 1340 | set_bit(XPRT_CONGESTED, &xprt->state); |
| 1341 | rpc_sleep_on(&xprt->backlog, task, NULL); |
| 1342 | } |
| 1343 | |
| 1344 | static void xprt_wake_up_backlog(struct rpc_xprt *xprt) |
| 1345 | { |
| 1346 | if (rpc_wake_up_next(&xprt->backlog) == NULL) |
| 1347 | clear_bit(XPRT_CONGESTED, &xprt->state); |
| 1348 | } |
| 1349 | |
| 1350 | static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) |
| 1351 | { |
| 1352 | bool ret = false; |
| 1353 | |
| 1354 | if (!test_bit(XPRT_CONGESTED, &xprt->state)) |
| 1355 | goto out; |
| 1356 | spin_lock(&xprt->reserve_lock); |
| 1357 | if (test_bit(XPRT_CONGESTED, &xprt->state)) { |
| 1358 | rpc_sleep_on(&xprt->backlog, task, NULL); |
| 1359 | ret = true; |
| 1360 | } |
| 1361 | spin_unlock(&xprt->reserve_lock); |
| 1362 | out: |
| 1363 | return ret; |
| 1364 | } |
| 1365 | |
Trond Myklebust | 92ea011 | 2017-06-20 19:35:39 -0400 | [diff] [blame] | 1366 | static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt) |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1367 | { |
| 1368 | struct rpc_rqst *req = ERR_PTR(-EAGAIN); |
| 1369 | |
Chuck Lever | ff699ea8 | 2018-03-05 15:13:13 -0500 | [diff] [blame] | 1370 | if (xprt->num_reqs >= xprt->max_reqs) |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1371 | goto out; |
Chuck Lever | ff699ea8 | 2018-03-05 15:13:13 -0500 | [diff] [blame] | 1372 | ++xprt->num_reqs; |
Trond Myklebust | 92ea011 | 2017-06-20 19:35:39 -0400 | [diff] [blame] | 1373 | spin_unlock(&xprt->reserve_lock); |
| 1374 | req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS); |
| 1375 | spin_lock(&xprt->reserve_lock); |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1376 | if (req != NULL) |
| 1377 | goto out; |
Chuck Lever | ff699ea8 | 2018-03-05 15:13:13 -0500 | [diff] [blame] | 1378 | --xprt->num_reqs; |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1379 | req = ERR_PTR(-ENOMEM); |
| 1380 | out: |
| 1381 | return req; |
| 1382 | } |
| 1383 | |
| 1384 | static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) |
| 1385 | { |
Chuck Lever | ff699ea8 | 2018-03-05 15:13:13 -0500 | [diff] [blame] | 1386 | if (xprt->num_reqs > xprt->min_reqs) { |
| 1387 | --xprt->num_reqs; |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1388 | kfree(req); |
| 1389 | return true; |
| 1390 | } |
| 1391 | return false; |
| 1392 | } |
| 1393 | |
Trond Myklebust | f39c1bf | 2012-09-07 11:08:50 -0400 | [diff] [blame] | 1394 | void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | { |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1396 | struct rpc_rqst *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | |
Trond Myklebust | f39c1bf | 2012-09-07 11:08:50 -0400 | [diff] [blame] | 1398 | spin_lock(&xprt->reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | if (!list_empty(&xprt->free)) { |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1400 | req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); |
| 1401 | list_del(&req->rq_list); |
| 1402 | goto out_init_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | } |
Trond Myklebust | 92ea011 | 2017-06-20 19:35:39 -0400 | [diff] [blame] | 1404 | req = xprt_dynamic_alloc_slot(xprt); |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1405 | if (!IS_ERR(req)) |
| 1406 | goto out_init_req; |
| 1407 | switch (PTR_ERR(req)) { |
| 1408 | case -ENOMEM: |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1409 | dprintk("RPC: dynamic allocation of request slot " |
| 1410 | "failed! Retrying\n"); |
Trond Myklebust | 1afeaf5 | 2012-05-19 12:12:53 -0400 | [diff] [blame] | 1411 | task->tk_status = -ENOMEM; |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1412 | break; |
| 1413 | case -EAGAIN: |
Trond Myklebust | ba60eb2 | 2013-04-14 10:49:37 -0400 | [diff] [blame] | 1414 | xprt_add_backlog(xprt, task); |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1415 | dprintk("RPC: waiting for request slot\n"); |
Gustavo A. R. Silva | e9d4763 | 2017-10-20 11:48:30 -0500 | [diff] [blame] | 1416 | /* fall through */ |
Trond Myklebust | 1afeaf5 | 2012-05-19 12:12:53 -0400 | [diff] [blame] | 1417 | default: |
| 1418 | task->tk_status = -EAGAIN; |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1419 | } |
Trond Myklebust | f39c1bf | 2012-09-07 11:08:50 -0400 | [diff] [blame] | 1420 | spin_unlock(&xprt->reserve_lock); |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1421 | return; |
| 1422 | out_init_req: |
Chuck Lever | ff699ea8 | 2018-03-05 15:13:13 -0500 | [diff] [blame] | 1423 | xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots, |
| 1424 | xprt->num_reqs); |
Chuck Lever | 37ac86c | 2018-05-04 15:34:53 -0400 | [diff] [blame] | 1425 | spin_unlock(&xprt->reserve_lock); |
| 1426 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1427 | task->tk_status = 0; |
| 1428 | task->tk_rqstp = req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | } |
Trond Myklebust | f39c1bf | 2012-09-07 11:08:50 -0400 | [diff] [blame] | 1430 | EXPORT_SYMBOL_GPL(xprt_alloc_slot); |
| 1431 | |
| 1432 | void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) |
| 1433 | { |
| 1434 | /* Note: grabbing the xprt_lock_write() ensures that we throttle |
| 1435 | * new slot allocation if the transport is congested (i.e. when |
| 1436 | * reconnecting a stream transport or when out of socket write |
| 1437 | * buffer space). |
| 1438 | */ |
| 1439 | if (xprt_lock_write(xprt, task)) { |
| 1440 | xprt_alloc_slot(xprt, task); |
| 1441 | xprt_release_write(xprt, task); |
| 1442 | } |
| 1443 | } |
| 1444 | EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
Chuck Lever | a9cde23 | 2018-05-04 15:34:59 -0400 | [diff] [blame] | 1446 | void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) |
Trond Myklebust | ee5ebe8 | 2010-04-16 16:37:01 -0400 | [diff] [blame] | 1447 | { |
Trond Myklebust | ee5ebe8 | 2010-04-16 16:37:01 -0400 | [diff] [blame] | 1448 | spin_lock(&xprt->reserve_lock); |
Trond Myklebust | c25573b | 2011-12-01 14:16:17 -0500 | [diff] [blame] | 1449 | if (!xprt_dynamic_free_slot(xprt, req)) { |
| 1450 | memset(req, 0, sizeof(*req)); /* mark unused */ |
| 1451 | list_add(&req->rq_list, &xprt->free); |
| 1452 | } |
Trond Myklebust | ba60eb2 | 2013-04-14 10:49:37 -0400 | [diff] [blame] | 1453 | xprt_wake_up_backlog(xprt); |
Trond Myklebust | ee5ebe8 | 2010-04-16 16:37:01 -0400 | [diff] [blame] | 1454 | spin_unlock(&xprt->reserve_lock); |
| 1455 | } |
Chuck Lever | a9cde23 | 2018-05-04 15:34:59 -0400 | [diff] [blame] | 1456 | EXPORT_SYMBOL_GPL(xprt_free_slot); |
Trond Myklebust | ee5ebe8 | 2010-04-16 16:37:01 -0400 | [diff] [blame] | 1457 | |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1458 | static void xprt_free_all_slots(struct rpc_xprt *xprt) |
| 1459 | { |
| 1460 | struct rpc_rqst *req; |
| 1461 | while (!list_empty(&xprt->free)) { |
| 1462 | req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); |
| 1463 | list_del(&req->rq_list); |
| 1464 | kfree(req); |
| 1465 | } |
| 1466 | } |
| 1467 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1468 | struct rpc_xprt *xprt_alloc(struct net *net, size_t size, |
| 1469 | unsigned int num_prealloc, |
| 1470 | unsigned int max_alloc) |
Pavel Emelyanov | bd1722d | 2010-09-29 16:02:43 +0400 | [diff] [blame] | 1471 | { |
| 1472 | struct rpc_xprt *xprt; |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1473 | struct rpc_rqst *req; |
| 1474 | int i; |
Pavel Emelyanov | bd1722d | 2010-09-29 16:02:43 +0400 | [diff] [blame] | 1475 | |
| 1476 | xprt = kzalloc(size, GFP_KERNEL); |
| 1477 | if (xprt == NULL) |
| 1478 | goto out; |
| 1479 | |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1480 | xprt_init(xprt, net); |
| 1481 | |
| 1482 | for (i = 0; i < num_prealloc; i++) { |
| 1483 | req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); |
| 1484 | if (!req) |
wangweidong | 8313164 | 2013-10-15 11:44:30 +0800 | [diff] [blame] | 1485 | goto out_free; |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1486 | list_add(&req->rq_list, &xprt->free); |
| 1487 | } |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 1488 | if (max_alloc > num_prealloc) |
| 1489 | xprt->max_reqs = max_alloc; |
| 1490 | else |
| 1491 | xprt->max_reqs = num_prealloc; |
| 1492 | xprt->min_reqs = num_prealloc; |
Chuck Lever | ff699ea8 | 2018-03-05 15:13:13 -0500 | [diff] [blame] | 1493 | xprt->num_reqs = num_prealloc; |
Pavel Emelyanov | bd1722d | 2010-09-29 16:02:43 +0400 | [diff] [blame] | 1494 | |
| 1495 | return xprt; |
| 1496 | |
| 1497 | out_free: |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1498 | xprt_free(xprt); |
Pavel Emelyanov | bd1722d | 2010-09-29 16:02:43 +0400 | [diff] [blame] | 1499 | out: |
| 1500 | return NULL; |
| 1501 | } |
| 1502 | EXPORT_SYMBOL_GPL(xprt_alloc); |
| 1503 | |
Pavel Emelyanov | e204e62 | 2010-09-29 16:03:13 +0400 | [diff] [blame] | 1504 | void xprt_free(struct rpc_xprt *xprt) |
| 1505 | { |
Pavel Emelyanov | 37aa213 | 2010-09-29 16:05:43 +0400 | [diff] [blame] | 1506 | put_net(xprt->xprt_net); |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1507 | xprt_free_all_slots(xprt); |
Trond Myklebust | fda1bfe | 2015-02-14 17:48:49 -0500 | [diff] [blame] | 1508 | kfree_rcu(xprt, rcu); |
Pavel Emelyanov | e204e62 | 2010-09-29 16:03:13 +0400 | [diff] [blame] | 1509 | } |
| 1510 | EXPORT_SYMBOL_GPL(xprt_free); |
| 1511 | |
Trond Myklebust | 902c588 | 2018-09-01 17:21:01 -0400 | [diff] [blame] | 1512 | static void |
| 1513 | xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt) |
| 1514 | { |
| 1515 | req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1; |
| 1516 | } |
| 1517 | |
Trond Myklebust | 9dc6edc | 2018-08-22 14:24:16 -0400 | [diff] [blame] | 1518 | static __be32 |
| 1519 | xprt_alloc_xid(struct rpc_xprt *xprt) |
| 1520 | { |
| 1521 | __be32 xid; |
| 1522 | |
| 1523 | spin_lock(&xprt->reserve_lock); |
| 1524 | xid = (__force __be32)xprt->xid++; |
| 1525 | spin_unlock(&xprt->reserve_lock); |
| 1526 | return xid; |
| 1527 | } |
| 1528 | |
| 1529 | static void |
| 1530 | xprt_init_xid(struct rpc_xprt *xprt) |
| 1531 | { |
| 1532 | xprt->xid = prandom_u32(); |
| 1533 | } |
| 1534 | |
| 1535 | static void |
| 1536 | xprt_request_init(struct rpc_task *task) |
| 1537 | { |
| 1538 | struct rpc_xprt *xprt = task->tk_xprt; |
| 1539 | struct rpc_rqst *req = task->tk_rqstp; |
| 1540 | |
Trond Myklebust | 9dc6edc | 2018-08-22 14:24:16 -0400 | [diff] [blame] | 1541 | req->rq_timeout = task->tk_client->cl_timeout->to_initval; |
| 1542 | req->rq_task = task; |
| 1543 | req->rq_xprt = xprt; |
| 1544 | req->rq_buffer = NULL; |
| 1545 | req->rq_xid = xprt_alloc_xid(xprt); |
Trond Myklebust | 902c588 | 2018-09-01 17:21:01 -0400 | [diff] [blame] | 1546 | xprt_init_connect_cookie(req, xprt); |
Trond Myklebust | 9dc6edc | 2018-08-22 14:24:16 -0400 | [diff] [blame] | 1547 | req->rq_bytes_sent = 0; |
| 1548 | req->rq_snd_buf.len = 0; |
| 1549 | req->rq_snd_buf.buflen = 0; |
| 1550 | req->rq_rcv_buf.len = 0; |
| 1551 | req->rq_rcv_buf.buflen = 0; |
| 1552 | req->rq_release_snd_buf = NULL; |
| 1553 | xprt_reset_majortimeo(req); |
| 1554 | dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, |
| 1555 | req, ntohl(req->rq_xid)); |
| 1556 | } |
| 1557 | |
| 1558 | static void |
| 1559 | xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task) |
| 1560 | { |
| 1561 | xprt->ops->alloc_slot(xprt, task); |
| 1562 | if (task->tk_rqstp != NULL) |
| 1563 | xprt_request_init(task); |
| 1564 | } |
| 1565 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1566 | /** |
| 1567 | * xprt_reserve - allocate an RPC request slot |
| 1568 | * @task: RPC task requesting a slot allocation |
| 1569 | * |
Trond Myklebust | ba60eb2 | 2013-04-14 10:49:37 -0400 | [diff] [blame] | 1570 | * If the transport is marked as being congested, or if no more |
| 1571 | * slots are available, place the task on the transport's |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1572 | * backlog queue. |
| 1573 | */ |
| 1574 | void xprt_reserve(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | { |
Trond Myklebust | fb43d17 | 2016-01-30 16:39:26 -0500 | [diff] [blame] | 1576 | struct rpc_xprt *xprt = task->tk_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | |
Trond Myklebust | 43cedbf0e | 2011-07-17 16:01:03 -0400 | [diff] [blame] | 1578 | task->tk_status = 0; |
| 1579 | if (task->tk_rqstp != NULL) |
| 1580 | return; |
| 1581 | |
Trond Myklebust | 43cedbf0e | 2011-07-17 16:01:03 -0400 | [diff] [blame] | 1582 | task->tk_timeout = 0; |
| 1583 | task->tk_status = -EAGAIN; |
Trond Myklebust | ba60eb2 | 2013-04-14 10:49:37 -0400 | [diff] [blame] | 1584 | if (!xprt_throttle_congested(xprt, task)) |
Trond Myklebust | 9dc6edc | 2018-08-22 14:24:16 -0400 | [diff] [blame] | 1585 | xprt_do_reserve(xprt, task); |
Trond Myklebust | ba60eb2 | 2013-04-14 10:49:37 -0400 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | /** |
| 1589 | * xprt_retry_reserve - allocate an RPC request slot |
| 1590 | * @task: RPC task requesting a slot allocation |
| 1591 | * |
| 1592 | * If no more slots are available, place the task on the transport's |
| 1593 | * backlog queue. |
| 1594 | * Note that the only difference with xprt_reserve is that we now |
| 1595 | * ignore the value of the XPRT_CONGESTED flag. |
| 1596 | */ |
| 1597 | void xprt_retry_reserve(struct rpc_task *task) |
| 1598 | { |
Trond Myklebust | fb43d17 | 2016-01-30 16:39:26 -0500 | [diff] [blame] | 1599 | struct rpc_xprt *xprt = task->tk_xprt; |
Trond Myklebust | ba60eb2 | 2013-04-14 10:49:37 -0400 | [diff] [blame] | 1600 | |
| 1601 | task->tk_status = 0; |
| 1602 | if (task->tk_rqstp != NULL) |
| 1603 | return; |
| 1604 | |
| 1605 | task->tk_timeout = 0; |
| 1606 | task->tk_status = -EAGAIN; |
Trond Myklebust | 9dc6edc | 2018-08-22 14:24:16 -0400 | [diff] [blame] | 1607 | xprt_do_reserve(xprt, task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | } |
| 1609 | |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 1610 | static void |
| 1611 | xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req) |
| 1612 | { |
| 1613 | struct rpc_xprt *xprt = req->rq_xprt; |
| 1614 | |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1615 | if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) || |
| 1616 | test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) || |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 1617 | xprt_is_pinned_rqst(req)) { |
| 1618 | spin_lock(&xprt->queue_lock); |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1619 | xprt_request_dequeue_transmit_locked(task); |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 1620 | xprt_request_dequeue_receive_locked(task); |
| 1621 | while (xprt_is_pinned_rqst(req)) { |
| 1622 | set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); |
| 1623 | spin_unlock(&xprt->queue_lock); |
| 1624 | xprt_wait_on_pinned_rqst(req); |
| 1625 | spin_lock(&xprt->queue_lock); |
| 1626 | clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); |
| 1627 | } |
| 1628 | spin_unlock(&xprt->queue_lock); |
| 1629 | } |
| 1630 | } |
| 1631 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1632 | /** |
| 1633 | * xprt_release - release an RPC request slot |
| 1634 | * @task: task which is finished with the slot |
| 1635 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1637 | void xprt_release(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | { |
Ricardo Labiaga | 55ae1aa | 2009-04-01 09:23:03 -0400 | [diff] [blame] | 1639 | struct rpc_xprt *xprt; |
Trond Myklebust | 87ed500 | 2013-01-07 14:30:46 -0500 | [diff] [blame] | 1640 | struct rpc_rqst *req = task->tk_rqstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | |
Trond Myklebust | 87ed500 | 2013-01-07 14:30:46 -0500 | [diff] [blame] | 1642 | if (req == NULL) { |
| 1643 | if (task->tk_client) { |
Trond Myklebust | fb43d17 | 2016-01-30 16:39:26 -0500 | [diff] [blame] | 1644 | xprt = task->tk_xprt; |
Trond Myklebust | 87ed500 | 2013-01-07 14:30:46 -0500 | [diff] [blame] | 1645 | if (xprt->snd_task == task) |
| 1646 | xprt_release_write(xprt, task); |
Trond Myklebust | 87ed500 | 2013-01-07 14:30:46 -0500 | [diff] [blame] | 1647 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | return; |
Trond Myklebust | 87ed500 | 2013-01-07 14:30:46 -0500 | [diff] [blame] | 1649 | } |
Ricardo Labiaga | 55ae1aa | 2009-04-01 09:23:03 -0400 | [diff] [blame] | 1650 | |
Ricardo Labiaga | 55ae1aa | 2009-04-01 09:23:03 -0400 | [diff] [blame] | 1651 | xprt = req->rq_xprt; |
Weston Andros Adamson | 0a70219 | 2012-02-17 13:15:24 -0500 | [diff] [blame] | 1652 | if (task->tk_ops->rpc_count_stats != NULL) |
| 1653 | task->tk_ops->rpc_count_stats(task, task->tk_calldata); |
| 1654 | else if (task->tk_client) |
| 1655 | rpc_count_iostats(task, task->tk_client->cl_metrics); |
Trond Myklebust | edc81dc | 2018-08-22 17:55:46 -0400 | [diff] [blame] | 1656 | xprt_request_dequeue_all(task, req); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1657 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1658 | xprt->ops->release_xprt(xprt, task); |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 1659 | if (xprt->ops->release_request) |
| 1660 | xprt->ops->release_request(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | xprt->last_used = jiffies; |
Trond Myklebust | ad3331a | 2016-08-02 13:47:43 -0400 | [diff] [blame] | 1662 | xprt_schedule_autodisconnect(xprt); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1663 | spin_unlock_bh(&xprt->transport_lock); |
Trond Myklebust | ee5ebe8 | 2010-04-16 16:37:01 -0400 | [diff] [blame] | 1664 | if (req->rq_buffer) |
Chuck Lever | 3435c74 | 2016-09-15 10:55:29 -0400 | [diff] [blame] | 1665 | xprt->ops->buf_free(task); |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 1666 | xprt_inject_disconnect(xprt); |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 1667 | if (req->rq_cred != NULL) |
| 1668 | put_rpccred(req->rq_cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | task->tk_rqstp = NULL; |
J. Bruce Fields | ead5e1c | 2005-10-13 16:54:43 -0400 | [diff] [blame] | 1670 | if (req->rq_release_snd_buf) |
| 1671 | req->rq_release_snd_buf(req); |
Ricardo Labiaga | 55ae1aa | 2009-04-01 09:23:03 -0400 | [diff] [blame] | 1672 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1673 | dprintk("RPC: %5u release request %p\n", task->tk_pid, req); |
Trond Myklebust | ee5ebe8 | 2010-04-16 16:37:01 -0400 | [diff] [blame] | 1674 | if (likely(!bc_prealloc(req))) |
Chuck Lever | a9cde23 | 2018-05-04 15:34:59 -0400 | [diff] [blame] | 1675 | xprt->ops->free_slot(xprt, req); |
Trond Myklebust | ee5ebe8 | 2010-04-16 16:37:01 -0400 | [diff] [blame] | 1676 | else |
Trond Myklebust | c9acb42 | 2010-03-19 15:36:22 -0400 | [diff] [blame] | 1677 | xprt_free_bc_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
Trond Myklebust | 902c588 | 2018-09-01 17:21:01 -0400 | [diff] [blame] | 1680 | #ifdef CONFIG_SUNRPC_BACKCHANNEL |
| 1681 | void |
| 1682 | xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task) |
| 1683 | { |
| 1684 | struct xdr_buf *xbufp = &req->rq_snd_buf; |
| 1685 | |
| 1686 | task->tk_rqstp = req; |
| 1687 | req->rq_task = task; |
| 1688 | xprt_init_connect_cookie(req, req->rq_xprt); |
| 1689 | /* |
| 1690 | * Set up the xdr_buf length. |
| 1691 | * This also indicates that the buffer is XDR encoded already. |
| 1692 | */ |
| 1693 | xbufp->len = xbufp->head[0].iov_len + xbufp->page_len + |
| 1694 | xbufp->tail[0].iov_len; |
| 1695 | req->rq_bytes_sent = 0; |
| 1696 | } |
| 1697 | #endif |
| 1698 | |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1699 | static void xprt_init(struct rpc_xprt *xprt, struct net *net) |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1700 | { |
Trond Myklebust | 30c5116 | 2015-02-24 20:31:39 -0500 | [diff] [blame] | 1701 | kref_init(&xprt->kref); |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1702 | |
| 1703 | spin_lock_init(&xprt->transport_lock); |
| 1704 | spin_lock_init(&xprt->reserve_lock); |
Trond Myklebust | 75c8415 | 2018-08-31 10:21:00 -0400 | [diff] [blame] | 1705 | spin_lock_init(&xprt->queue_lock); |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1706 | |
| 1707 | INIT_LIST_HEAD(&xprt->free); |
Trond Myklebust | ef3f5434 | 2018-08-08 09:23:32 -0400 | [diff] [blame] | 1708 | INIT_LIST_HEAD(&xprt->recv_queue); |
Trond Myklebust | 944b042 | 2018-08-09 23:33:21 -0400 | [diff] [blame] | 1709 | INIT_LIST_HEAD(&xprt->xmit_queue); |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1710 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
Ricardo Labiaga | f9acac1 | 2009-04-01 09:22:59 -0400 | [diff] [blame] | 1711 | spin_lock_init(&xprt->bc_pa_lock); |
| 1712 | INIT_LIST_HEAD(&xprt->bc_pa_list); |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1713 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
Trond Myklebust | 80b14d5 | 2015-02-14 20:31:59 -0500 | [diff] [blame] | 1714 | INIT_LIST_HEAD(&xprt->xprt_switch); |
Ricardo Labiaga | f9acac1 | 2009-04-01 09:22:59 -0400 | [diff] [blame] | 1715 | |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1716 | xprt->last_used = jiffies; |
| 1717 | xprt->cwnd = RPC_INITCWND; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1718 | xprt->bind_index = 0; |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1719 | |
| 1720 | rpc_init_wait_queue(&xprt->binding, "xprt_binding"); |
| 1721 | rpc_init_wait_queue(&xprt->pending, "xprt_pending"); |
Trond Myklebust | 34006ce | 2011-07-17 18:11:34 -0400 | [diff] [blame] | 1722 | rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending"); |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1723 | rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); |
| 1724 | |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1725 | xprt_init_xid(xprt); |
| 1726 | |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1727 | xprt->xprt_net = get_net(net); |
Trond Myklebust | 8d9266f | 2011-07-17 16:01:09 -0400 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | /** |
| 1731 | * xprt_create_transport - create an RPC transport |
| 1732 | * @args: rpc transport creation arguments |
| 1733 | * |
| 1734 | */ |
| 1735 | struct rpc_xprt *xprt_create_transport(struct xprt_create *args) |
| 1736 | { |
| 1737 | struct rpc_xprt *xprt; |
| 1738 | struct xprt_class *t; |
| 1739 | |
| 1740 | spin_lock(&xprt_list_lock); |
| 1741 | list_for_each_entry(t, &xprt_list, list) { |
| 1742 | if (t->ident == args->ident) { |
| 1743 | spin_unlock(&xprt_list_lock); |
| 1744 | goto found; |
| 1745 | } |
| 1746 | } |
| 1747 | spin_unlock(&xprt_list_lock); |
Chuck Lever | 3c45ddf | 2014-07-16 15:38:32 -0400 | [diff] [blame] | 1748 | dprintk("RPC: transport (%d) not supported\n", args->ident); |
Trond Myklebust | 8d9266f | 2011-07-17 16:01:09 -0400 | [diff] [blame] | 1749 | return ERR_PTR(-EIO); |
| 1750 | |
| 1751 | found: |
| 1752 | xprt = t->setup(args); |
| 1753 | if (IS_ERR(xprt)) { |
| 1754 | dprintk("RPC: xprt_create_transport: failed, %ld\n", |
| 1755 | -PTR_ERR(xprt)); |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1756 | goto out; |
Trond Myklebust | 8d9266f | 2011-07-17 16:01:09 -0400 | [diff] [blame] | 1757 | } |
J. Bruce Fields | 33d90ac | 2013-04-11 15:06:36 -0400 | [diff] [blame] | 1758 | if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) |
| 1759 | xprt->idle_timeout = 0; |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1760 | INIT_WORK(&xprt->task_cleanup, xprt_autoclose); |
| 1761 | if (xprt_has_timer(xprt)) |
Kees Cook | ff861c4 | 2017-10-16 17:29:42 -0700 | [diff] [blame] | 1762 | timer_setup(&xprt->timer, xprt_init_autodisconnect, 0); |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1763 | else |
Kees Cook | ff861c4 | 2017-10-16 17:29:42 -0700 | [diff] [blame] | 1764 | timer_setup(&xprt->timer, NULL, 0); |
Trond Myklebust | 4e0038b | 2012-03-01 17:01:05 -0500 | [diff] [blame] | 1765 | |
| 1766 | if (strlen(args->servername) > RPC_MAXNETNAMELEN) { |
| 1767 | xprt_destroy(xprt); |
| 1768 | return ERR_PTR(-EINVAL); |
| 1769 | } |
| 1770 | xprt->servername = kstrdup(args->servername, GFP_KERNEL); |
| 1771 | if (xprt->servername == NULL) { |
| 1772 | xprt_destroy(xprt); |
| 1773 | return ERR_PTR(-ENOMEM); |
| 1774 | } |
| 1775 | |
Jeff Layton | 3f94009 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 1776 | rpc_xprt_debugfs_register(xprt); |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 1777 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1778 | dprintk("RPC: created transport %p with %u slots\n", xprt, |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1779 | xprt->max_reqs); |
Trond Myklebust | 21de0a9 | 2011-07-17 16:57:32 -0400 | [diff] [blame] | 1780 | out: |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1781 | return xprt; |
| 1782 | } |
| 1783 | |
Trond Myklebust | 528fd35 | 2017-10-19 12:13:10 -0400 | [diff] [blame] | 1784 | static void xprt_destroy_cb(struct work_struct *work) |
| 1785 | { |
| 1786 | struct rpc_xprt *xprt = |
| 1787 | container_of(work, struct rpc_xprt, task_cleanup); |
| 1788 | |
| 1789 | rpc_xprt_debugfs_unregister(xprt); |
| 1790 | rpc_destroy_wait_queue(&xprt->binding); |
| 1791 | rpc_destroy_wait_queue(&xprt->pending); |
| 1792 | rpc_destroy_wait_queue(&xprt->sending); |
| 1793 | rpc_destroy_wait_queue(&xprt->backlog); |
| 1794 | kfree(xprt->servername); |
| 1795 | /* |
| 1796 | * Tear down transport state and free the rpc_xprt |
| 1797 | */ |
| 1798 | xprt->ops->destroy(xprt); |
| 1799 | } |
| 1800 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1801 | /** |
| 1802 | * xprt_destroy - destroy an RPC transport, killing off all requests. |
Trond Myklebust | a8de240 | 2011-03-15 19:56:30 -0400 | [diff] [blame] | 1803 | * @xprt: transport to destroy |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1804 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | */ |
Trond Myklebust | a8de240 | 2011-03-15 19:56:30 -0400 | [diff] [blame] | 1806 | static void xprt_destroy(struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1808 | dprintk("RPC: destroying transport %p\n", xprt); |
Trond Myklebust | 79234c3 | 2015-09-18 15:53:24 -0400 | [diff] [blame] | 1809 | |
Trond Myklebust | 528fd35 | 2017-10-19 12:13:10 -0400 | [diff] [blame] | 1810 | /* |
| 1811 | * Exclude transport connect/disconnect handlers and autoclose |
| 1812 | */ |
Trond Myklebust | 79234c3 | 2015-09-18 15:53:24 -0400 | [diff] [blame] | 1813 | wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE); |
| 1814 | |
Trond Myklebust | 0065db3 | 2006-01-03 09:55:56 +0100 | [diff] [blame] | 1815 | del_timer_sync(&xprt->timer); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1816 | |
| 1817 | /* |
Trond Myklebust | 528fd35 | 2017-10-19 12:13:10 -0400 | [diff] [blame] | 1818 | * Destroy sockets etc from the system workqueue so they can |
| 1819 | * safely flush receive work running on rpciod. |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1820 | */ |
Trond Myklebust | 528fd35 | 2017-10-19 12:13:10 -0400 | [diff] [blame] | 1821 | INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb); |
| 1822 | schedule_work(&xprt->task_cleanup); |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 1823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | |
Trond Myklebust | 30c5116 | 2015-02-24 20:31:39 -0500 | [diff] [blame] | 1825 | static void xprt_destroy_kref(struct kref *kref) |
| 1826 | { |
| 1827 | xprt_destroy(container_of(kref, struct rpc_xprt, kref)); |
| 1828 | } |
| 1829 | |
| 1830 | /** |
| 1831 | * xprt_get - return a reference to an RPC transport. |
| 1832 | * @xprt: pointer to the transport |
| 1833 | * |
| 1834 | */ |
| 1835 | struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) |
| 1836 | { |
| 1837 | if (xprt != NULL && kref_get_unless_zero(&xprt->kref)) |
| 1838 | return xprt; |
| 1839 | return NULL; |
| 1840 | } |
| 1841 | EXPORT_SYMBOL_GPL(xprt_get); |
| 1842 | |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 1843 | /** |
| 1844 | * xprt_put - release a reference to an RPC transport. |
| 1845 | * @xprt: pointer to the transport |
| 1846 | * |
| 1847 | */ |
| 1848 | void xprt_put(struct rpc_xprt *xprt) |
| 1849 | { |
Trond Myklebust | 30c5116 | 2015-02-24 20:31:39 -0500 | [diff] [blame] | 1850 | if (xprt != NULL) |
| 1851 | kref_put(&xprt->kref, xprt_destroy_kref); |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 1852 | } |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 1853 | EXPORT_SYMBOL_GPL(xprt_put); |