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