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