Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Tom Tucker | 1d8206b9 | 2007-12-30 21:07:15 -0600 | [diff] [blame] | 2 | /* |
| 3 | * linux/net/sunrpc/svc_xprt.c |
| 4 | * |
| 5 | * Author: Tom Tucker <tom@opengridcomputing.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/errno.h> |
Tom Tucker | 1d8206b9 | 2007-12-30 21:07:15 -0600 | [diff] [blame] | 10 | #include <linux/freezer.h> |
Jeff Layton | 7086721 | 2008-02-07 16:34:54 -0500 | [diff] [blame] | 11 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Tom Tucker | 1d8206b9 | 2007-12-30 21:07:15 -0600 | [diff] [blame] | 13 | #include <net/sock.h> |
Scott Mayhew | c3d4879 | 2015-12-11 16:45:58 -0500 | [diff] [blame] | 14 | #include <linux/sunrpc/addr.h> |
Tom Tucker | 1d8206b9 | 2007-12-30 21:07:15 -0600 | [diff] [blame] | 15 | #include <linux/sunrpc/stats.h> |
| 16 | #include <linux/sunrpc/svc_xprt.h> |
H Hartley Sweeten | dcf1a35 | 2009-04-22 20:18:19 -0400 | [diff] [blame] | 17 | #include <linux/sunrpc/svcsock.h> |
J. Bruce Fields | 99de8ea | 2010-12-08 12:45:44 -0500 | [diff] [blame] | 18 | #include <linux/sunrpc/xprt.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Scott Mayhew | c3d4879 | 2015-12-11 16:45:58 -0500 | [diff] [blame] | 20 | #include <linux/netdevice.h> |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 21 | #include <trace/events/sunrpc.h> |
Tom Tucker | 1d8206b9 | 2007-12-30 21:07:15 -0600 | [diff] [blame] | 22 | |
| 23 | #define RPCDBG_FACILITY RPCDBG_SVCXPRT |
| 24 | |
Trond Myklebust | ff3ac5c | 2016-06-24 10:55:50 -0400 | [diff] [blame] | 25 | static unsigned int svc_rpc_per_connection_limit __read_mostly; |
| 26 | module_param(svc_rpc_per_connection_limit, uint, 0644); |
| 27 | |
| 28 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 29 | static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt); |
| 30 | static int svc_deferred_recv(struct svc_rqst *rqstp); |
| 31 | static struct cache_deferred_req *svc_defer(struct cache_req *req); |
Kees Cook | ff861c4 | 2017-10-16 17:29:42 -0700 | [diff] [blame] | 32 | static void svc_age_temp_xprts(struct timer_list *t); |
J. Bruce Fields | 7710ec3 | 2011-11-25 18:44:05 -0500 | [diff] [blame] | 33 | static void svc_delete_xprt(struct svc_xprt *xprt); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 34 | |
| 35 | /* apparently the "standard" is that clients close |
| 36 | * idle connections after 5 minutes, servers after |
| 37 | * 6 minutes |
J. Bruce Fields | 855c9e7 | 2019-06-18 16:43:11 -0400 | [diff] [blame] | 38 | * http://nfsv4bat.org/Documents/ConnectAThon/1996/nfstcp.pdf |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 39 | */ |
| 40 | static int svc_conn_age_period = 6*60; |
| 41 | |
Tom Tucker | 1d8206b9 | 2007-12-30 21:07:15 -0600 | [diff] [blame] | 42 | /* List of registered transport classes */ |
| 43 | static DEFINE_SPINLOCK(svc_xprt_class_lock); |
| 44 | static LIST_HEAD(svc_xprt_class_list); |
| 45 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 46 | /* SMP locking strategy: |
| 47 | * |
| 48 | * svc_pool->sp_lock protects most of the fields of that pool. |
| 49 | * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt. |
| 50 | * when both need to be taken (rare), svc_serv->sv_lock is first. |
Jeff Layton | 3c51991 | 2015-01-22 08:19:32 -0500 | [diff] [blame] | 51 | * The "service mutex" protects svc_serv->sv_nrthread. |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 52 | * svc_sock->sk_lock protects the svc_sock->sk_deferred list |
| 53 | * and the ->sk_info_authunix cache. |
| 54 | * |
| 55 | * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being |
| 56 | * enqueued multiply. During normal transport processing this bit |
| 57 | * is set by svc_xprt_enqueue and cleared by svc_xprt_received. |
| 58 | * Providers should not manipulate this bit directly. |
| 59 | * |
| 60 | * Some flags can be set to certain values at any time |
| 61 | * providing that certain rules are followed: |
| 62 | * |
| 63 | * XPT_CONN, XPT_DATA: |
| 64 | * - Can be set or cleared at any time. |
| 65 | * - After a set, svc_xprt_enqueue must be called to enqueue |
| 66 | * the transport for processing. |
| 67 | * - After a clear, the transport must be read/accepted. |
| 68 | * If this succeeds, it must be set again. |
| 69 | * XPT_CLOSE: |
| 70 | * - Can set at any time. It is never cleared. |
| 71 | * XPT_DEAD: |
| 72 | * - Can only be set while XPT_BUSY is held which ensures |
| 73 | * that no other thread will be using the transport or will |
| 74 | * try to set XPT_DEAD. |
| 75 | */ |
Tom Tucker | 1d8206b9 | 2007-12-30 21:07:15 -0600 | [diff] [blame] | 76 | int svc_reg_xprt_class(struct svc_xprt_class *xcl) |
| 77 | { |
| 78 | struct svc_xprt_class *cl; |
| 79 | int res = -EEXIST; |
| 80 | |
| 81 | dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name); |
| 82 | |
| 83 | INIT_LIST_HEAD(&xcl->xcl_list); |
| 84 | spin_lock(&svc_xprt_class_lock); |
| 85 | /* Make sure there isn't already a class with the same name */ |
| 86 | list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) { |
| 87 | if (strcmp(xcl->xcl_name, cl->xcl_name) == 0) |
| 88 | goto out; |
| 89 | } |
| 90 | list_add_tail(&xcl->xcl_list, &svc_xprt_class_list); |
| 91 | res = 0; |
| 92 | out: |
| 93 | spin_unlock(&svc_xprt_class_lock); |
| 94 | return res; |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(svc_reg_xprt_class); |
| 97 | |
| 98 | void svc_unreg_xprt_class(struct svc_xprt_class *xcl) |
| 99 | { |
| 100 | dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name); |
| 101 | spin_lock(&svc_xprt_class_lock); |
| 102 | list_del_init(&xcl->xcl_list); |
| 103 | spin_unlock(&svc_xprt_class_lock); |
| 104 | } |
| 105 | EXPORT_SYMBOL_GPL(svc_unreg_xprt_class); |
| 106 | |
Christophe JAILLET | b25b60d | 2020-03-27 17:15:39 +0100 | [diff] [blame] | 107 | /** |
| 108 | * svc_print_xprts - Format the transport list for printing |
| 109 | * @buf: target buffer for formatted address |
| 110 | * @maxlen: length of target buffer |
| 111 | * |
| 112 | * Fills in @buf with a string containing a list of transport names, each name |
| 113 | * terminated with '\n'. If the buffer is too small, some entries may be |
| 114 | * missing, but it is guaranteed that all lines in the output buffer are |
| 115 | * complete. |
| 116 | * |
| 117 | * Returns positive length of the filled-in string. |
Tom Tucker | dc9a16e | 2007-12-30 21:08:31 -0600 | [diff] [blame] | 118 | */ |
| 119 | int svc_print_xprts(char *buf, int maxlen) |
| 120 | { |
Pavel Emelyanov | 8f3a6de | 2010-10-05 23:30:19 +0400 | [diff] [blame] | 121 | struct svc_xprt_class *xcl; |
Tom Tucker | dc9a16e | 2007-12-30 21:08:31 -0600 | [diff] [blame] | 122 | char tmpstr[80]; |
| 123 | int len = 0; |
| 124 | buf[0] = '\0'; |
| 125 | |
| 126 | spin_lock(&svc_xprt_class_lock); |
Pavel Emelyanov | 8f3a6de | 2010-10-05 23:30:19 +0400 | [diff] [blame] | 127 | list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) { |
Tom Tucker | dc9a16e | 2007-12-30 21:08:31 -0600 | [diff] [blame] | 128 | int slen; |
Tom Tucker | dc9a16e | 2007-12-30 21:08:31 -0600 | [diff] [blame] | 129 | |
Christophe JAILLET | b25b60d | 2020-03-27 17:15:39 +0100 | [diff] [blame] | 130 | slen = snprintf(tmpstr, sizeof(tmpstr), "%s %d\n", |
| 131 | xcl->xcl_name, xcl->xcl_max_payload); |
| 132 | if (slen >= sizeof(tmpstr) || len + slen >= maxlen) |
Tom Tucker | dc9a16e | 2007-12-30 21:08:31 -0600 | [diff] [blame] | 133 | break; |
| 134 | len += slen; |
| 135 | strcat(buf, tmpstr); |
| 136 | } |
| 137 | spin_unlock(&svc_xprt_class_lock); |
| 138 | |
| 139 | return len; |
| 140 | } |
| 141 | |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 142 | static void svc_xprt_free(struct kref *kref) |
| 143 | { |
| 144 | struct svc_xprt *xprt = |
| 145 | container_of(kref, struct svc_xprt, xpt_ref); |
| 146 | struct module *owner = xprt->xpt_class->xcl_owner; |
Pavel Emelyanov | e3bfca0 | 2010-09-27 13:58:42 +0400 | [diff] [blame] | 147 | if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) |
| 148 | svcauth_unix_info_release(xprt); |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 149 | put_cred(xprt->xpt_cred); |
Pavel Emelyanov | 4fb8518 | 2010-09-27 14:00:49 +0400 | [diff] [blame] | 150 | put_net(xprt->xpt_net); |
J. Bruce Fields | 99de8ea | 2010-12-08 12:45:44 -0500 | [diff] [blame] | 151 | /* See comment on corresponding get in xs_setup_bc_tcp(): */ |
| 152 | if (xprt->xpt_bc_xprt) |
| 153 | xprt_put(xprt->xpt_bc_xprt); |
J. Bruce Fields | 39a9bea | 2016-05-17 12:38:21 -0400 | [diff] [blame] | 154 | if (xprt->xpt_bc_xps) |
| 155 | xprt_switch_put(xprt->xpt_bc_xps); |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 156 | xprt->xpt_ops->xpo_free(xprt); |
| 157 | module_put(owner); |
| 158 | } |
| 159 | |
| 160 | void svc_xprt_put(struct svc_xprt *xprt) |
| 161 | { |
| 162 | kref_put(&xprt->xpt_ref, svc_xprt_free); |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(svc_xprt_put); |
| 165 | |
Tom Tucker | 1d8206b9 | 2007-12-30 21:07:15 -0600 | [diff] [blame] | 166 | /* |
| 167 | * Called by transport drivers to initialize the transport independent |
| 168 | * portion of the transport instance. |
| 169 | */ |
Stanislav Kinsbursky | bd4620d | 2011-12-06 14:19:10 +0300 | [diff] [blame] | 170 | void svc_xprt_init(struct net *net, struct svc_xprt_class *xcl, |
| 171 | struct svc_xprt *xprt, struct svc_serv *serv) |
Tom Tucker | 1d8206b9 | 2007-12-30 21:07:15 -0600 | [diff] [blame] | 172 | { |
| 173 | memset(xprt, 0, sizeof(*xprt)); |
| 174 | xprt->xpt_class = xcl; |
| 175 | xprt->xpt_ops = xcl->xcl_ops; |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 176 | kref_init(&xprt->xpt_ref); |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 177 | xprt->xpt_server = serv; |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 178 | INIT_LIST_HEAD(&xprt->xpt_list); |
| 179 | INIT_LIST_HEAD(&xprt->xpt_ready); |
Tom Tucker | 8c7b017 | 2007-12-30 21:08:10 -0600 | [diff] [blame] | 180 | INIT_LIST_HEAD(&xprt->xpt_deferred); |
J. Bruce Fields | edc7a89 | 2010-03-22 15:37:17 -0400 | [diff] [blame] | 181 | INIT_LIST_HEAD(&xprt->xpt_users); |
Tom Tucker | a50fea2 | 2007-12-30 21:07:59 -0600 | [diff] [blame] | 182 | mutex_init(&xprt->xpt_mutex); |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame] | 183 | spin_lock_init(&xprt->xpt_lock); |
Tom Tucker | 4e5caaa | 2007-12-30 21:08:20 -0600 | [diff] [blame] | 184 | set_bit(XPT_BUSY, &xprt->xpt_flags); |
Stanislav Kinsbursky | bd4620d | 2011-12-06 14:19:10 +0300 | [diff] [blame] | 185 | xprt->xpt_net = get_net(net); |
Chuck Lever | ece200d | 2018-03-27 10:51:00 -0400 | [diff] [blame] | 186 | strcpy(xprt->xpt_remotebuf, "uninitialized"); |
Tom Tucker | 1d8206b9 | 2007-12-30 21:07:15 -0600 | [diff] [blame] | 187 | } |
| 188 | EXPORT_SYMBOL_GPL(svc_xprt_init); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 189 | |
Chuck Lever | 5dd248f | 2008-06-30 18:45:37 -0400 | [diff] [blame] | 190 | static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl, |
| 191 | struct svc_serv *serv, |
Pavel Emelyanov | 62832c0 | 2010-09-29 16:04:18 +0400 | [diff] [blame] | 192 | struct net *net, |
Chuck Lever | 9652ada | 2009-03-18 20:46:21 -0400 | [diff] [blame] | 193 | const int family, |
| 194 | const unsigned short port, |
| 195 | int flags) |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 196 | { |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 197 | struct sockaddr_in sin = { |
| 198 | .sin_family = AF_INET, |
Al Viro | e6f1ceb | 2008-03-17 22:44:53 -0700 | [diff] [blame] | 199 | .sin_addr.s_addr = htonl(INADDR_ANY), |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 200 | .sin_port = htons(port), |
| 201 | }; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 202 | #if IS_ENABLED(CONFIG_IPV6) |
Chuck Lever | 5dd248f | 2008-06-30 18:45:37 -0400 | [diff] [blame] | 203 | struct sockaddr_in6 sin6 = { |
| 204 | .sin6_family = AF_INET6, |
| 205 | .sin6_addr = IN6ADDR_ANY_INIT, |
| 206 | .sin6_port = htons(port), |
| 207 | }; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 208 | #endif |
Chuck Lever | 5dd248f | 2008-06-30 18:45:37 -0400 | [diff] [blame] | 209 | struct sockaddr *sap; |
| 210 | size_t len; |
| 211 | |
Chuck Lever | 9652ada | 2009-03-18 20:46:21 -0400 | [diff] [blame] | 212 | switch (family) { |
| 213 | case PF_INET: |
Chuck Lever | 5dd248f | 2008-06-30 18:45:37 -0400 | [diff] [blame] | 214 | sap = (struct sockaddr *)&sin; |
| 215 | len = sizeof(sin); |
| 216 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 217 | #if IS_ENABLED(CONFIG_IPV6) |
Chuck Lever | 9652ada | 2009-03-18 20:46:21 -0400 | [diff] [blame] | 218 | case PF_INET6: |
Chuck Lever | 5dd248f | 2008-06-30 18:45:37 -0400 | [diff] [blame] | 219 | sap = (struct sockaddr *)&sin6; |
| 220 | len = sizeof(sin6); |
| 221 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 222 | #endif |
Chuck Lever | 5dd248f | 2008-06-30 18:45:37 -0400 | [diff] [blame] | 223 | default: |
| 224 | return ERR_PTR(-EAFNOSUPPORT); |
| 225 | } |
| 226 | |
Pavel Emelyanov | 62832c0 | 2010-09-29 16:04:18 +0400 | [diff] [blame] | 227 | return xcl->xcl_ops->xpo_create(serv, net, sap, len, flags); |
Chuck Lever | 5dd248f | 2008-06-30 18:45:37 -0400 | [diff] [blame] | 228 | } |
| 229 | |
J. Bruce Fields | 6741019 | 2012-08-17 22:12:19 -0400 | [diff] [blame] | 230 | /* |
| 231 | * svc_xprt_received conditionally queues the transport for processing |
| 232 | * by another thread. The caller must hold the XPT_BUSY bit and must |
| 233 | * not thereafter touch transport data. |
| 234 | * |
| 235 | * Note: XPT_DATA only gets cleared when a read-attempt finds no (or |
| 236 | * insufficient) data. |
| 237 | */ |
| 238 | static void svc_xprt_received(struct svc_xprt *xprt) |
| 239 | { |
Jeff Layton | acf06a7 | 2014-12-01 13:45:24 -0500 | [diff] [blame] | 240 | if (!test_bit(XPT_BUSY, &xprt->xpt_flags)) { |
| 241 | WARN_ONCE(1, "xprt=0x%p already busy!", xprt); |
Weston Andros Adamson | ff1fdb9 | 2012-10-23 10:43:40 -0400 | [diff] [blame] | 242 | return; |
Jeff Layton | acf06a7 | 2014-12-01 13:45:24 -0500 | [diff] [blame] | 243 | } |
| 244 | |
J. Bruce Fields | 6741019 | 2012-08-17 22:12:19 -0400 | [diff] [blame] | 245 | /* As soon as we clear busy, the xprt could be closed and |
Jeff Layton | b9e13cd | 2015-06-08 12:06:51 -0700 | [diff] [blame] | 246 | * 'put', so we need a reference to call svc_enqueue_xprt with: |
J. Bruce Fields | 6741019 | 2012-08-17 22:12:19 -0400 | [diff] [blame] | 247 | */ |
| 248 | svc_xprt_get(xprt); |
Trond Myklebust | 0971374 | 2014-07-24 23:59:31 -0400 | [diff] [blame] | 249 | smp_mb__before_atomic(); |
J. Bruce Fields | 6741019 | 2012-08-17 22:12:19 -0400 | [diff] [blame] | 250 | clear_bit(XPT_BUSY, &xprt->xpt_flags); |
Jeff Layton | b9e13cd | 2015-06-08 12:06:51 -0700 | [diff] [blame] | 251 | xprt->xpt_server->sv_ops->svo_enqueue_xprt(xprt); |
J. Bruce Fields | 6741019 | 2012-08-17 22:12:19 -0400 | [diff] [blame] | 252 | svc_xprt_put(xprt); |
| 253 | } |
| 254 | |
J. Bruce Fields | 39b5530 | 2012-08-14 15:50:34 -0400 | [diff] [blame] | 255 | void svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *new) |
| 256 | { |
| 257 | clear_bit(XPT_TEMP, &new->xpt_flags); |
| 258 | spin_lock_bh(&serv->sv_lock); |
| 259 | list_add(&new->xpt_list, &serv->sv_permsocks); |
| 260 | spin_unlock_bh(&serv->sv_lock); |
| 261 | svc_xprt_received(new); |
| 262 | } |
| 263 | |
Colin Ian King | da36e6d | 2017-10-16 14:40:21 +0100 | [diff] [blame] | 264 | static int _svc_create_xprt(struct svc_serv *serv, const char *xprt_name, |
| 265 | struct net *net, const int family, |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 266 | const unsigned short port, int flags, |
| 267 | const struct cred *cred) |
Chuck Lever | 5dd248f | 2008-06-30 18:45:37 -0400 | [diff] [blame] | 268 | { |
| 269 | struct svc_xprt_class *xcl; |
| 270 | |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 271 | spin_lock(&svc_xprt_class_lock); |
| 272 | list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) { |
Tom Tucker | 4e5caaa | 2007-12-30 21:08:20 -0600 | [diff] [blame] | 273 | struct svc_xprt *newxprt; |
NeilBrown | ed2849d | 2010-11-16 16:55:19 +1100 | [diff] [blame] | 274 | unsigned short newport; |
Tom Tucker | 4e5caaa | 2007-12-30 21:08:20 -0600 | [diff] [blame] | 275 | |
| 276 | if (strcmp(xprt_name, xcl->xcl_name)) |
| 277 | continue; |
| 278 | |
| 279 | if (!try_module_get(xcl->xcl_owner)) |
| 280 | goto err; |
| 281 | |
| 282 | spin_unlock(&svc_xprt_class_lock); |
Pavel Emelyanov | 62832c0 | 2010-09-29 16:04:18 +0400 | [diff] [blame] | 283 | newxprt = __svc_xpo_create(xcl, serv, net, family, port, flags); |
Tom Tucker | 4e5caaa | 2007-12-30 21:08:20 -0600 | [diff] [blame] | 284 | if (IS_ERR(newxprt)) { |
| 285 | module_put(xcl->xcl_owner); |
| 286 | return PTR_ERR(newxprt); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 287 | } |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 288 | newxprt->xpt_cred = get_cred(cred); |
J. Bruce Fields | 39b5530 | 2012-08-14 15:50:34 -0400 | [diff] [blame] | 289 | svc_add_new_perm_xprt(serv, newxprt); |
NeilBrown | ed2849d | 2010-11-16 16:55:19 +1100 | [diff] [blame] | 290 | newport = svc_xprt_local_port(newxprt); |
NeilBrown | ed2849d | 2010-11-16 16:55:19 +1100 | [diff] [blame] | 291 | return newport; |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 292 | } |
Tom Tucker | 4e5caaa | 2007-12-30 21:08:20 -0600 | [diff] [blame] | 293 | err: |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 294 | spin_unlock(&svc_xprt_class_lock); |
Chuck Lever | 6871790 | 2010-01-26 14:04:13 -0500 | [diff] [blame] | 295 | /* This errno is exposed to user space. Provide a reasonable |
| 296 | * perror msg for a bad transport. */ |
| 297 | return -EPROTONOSUPPORT; |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 298 | } |
J. Bruce Fields | d96b9c9 | 2016-05-18 14:50:14 -0400 | [diff] [blame] | 299 | |
| 300 | int svc_create_xprt(struct svc_serv *serv, const char *xprt_name, |
| 301 | struct net *net, const int family, |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 302 | const unsigned short port, int flags, |
| 303 | const struct cred *cred) |
J. Bruce Fields | d96b9c9 | 2016-05-18 14:50:14 -0400 | [diff] [blame] | 304 | { |
| 305 | int err; |
| 306 | |
| 307 | dprintk("svc: creating transport %s[%d]\n", xprt_name, port); |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 308 | err = _svc_create_xprt(serv, xprt_name, net, family, port, flags, cred); |
J. Bruce Fields | d96b9c9 | 2016-05-18 14:50:14 -0400 | [diff] [blame] | 309 | if (err == -EPROTONOSUPPORT) { |
| 310 | request_module("svc%s", xprt_name); |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 311 | err = _svc_create_xprt(serv, xprt_name, net, family, port, flags, cred); |
J. Bruce Fields | d96b9c9 | 2016-05-18 14:50:14 -0400 | [diff] [blame] | 312 | } |
Vasily Averin | 9ac3128 | 2018-12-24 14:46:14 +0300 | [diff] [blame] | 313 | if (err < 0) |
J. Bruce Fields | d96b9c9 | 2016-05-18 14:50:14 -0400 | [diff] [blame] | 314 | dprintk("svc: transport %s not found, err %d\n", |
Vasily Averin | 9ac3128 | 2018-12-24 14:46:14 +0300 | [diff] [blame] | 315 | xprt_name, -err); |
J. Bruce Fields | d96b9c9 | 2016-05-18 14:50:14 -0400 | [diff] [blame] | 316 | return err; |
| 317 | } |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 318 | EXPORT_SYMBOL_GPL(svc_create_xprt); |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 319 | |
| 320 | /* |
| 321 | * Copy the local and remote xprt addresses to the rqstp structure |
| 322 | */ |
| 323 | void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt) |
| 324 | { |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 325 | memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen); |
| 326 | rqstp->rq_addrlen = xprt->xpt_remotelen; |
| 327 | |
| 328 | /* |
| 329 | * Destination address in request is needed for binding the |
| 330 | * source address in RPC replies/callbacks later. |
| 331 | */ |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 332 | memcpy(&rqstp->rq_daddr, &xprt->xpt_local, xprt->xpt_locallen); |
| 333 | rqstp->rq_daddrlen = xprt->xpt_locallen; |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 334 | } |
| 335 | EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs); |
| 336 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 337 | /** |
| 338 | * svc_print_addr - Format rq_addr field for printing |
| 339 | * @rqstp: svc_rqst struct containing address to print |
| 340 | * @buf: target buffer for formatted address |
| 341 | * @len: length of target buffer |
| 342 | * |
| 343 | */ |
| 344 | char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len) |
| 345 | { |
| 346 | return __svc_print_addr(svc_addr(rqstp), buf, len); |
| 347 | } |
| 348 | EXPORT_SYMBOL_GPL(svc_print_addr); |
| 349 | |
Trond Myklebust | ff3ac5c | 2016-06-24 10:55:50 -0400 | [diff] [blame] | 350 | static bool svc_xprt_slots_in_range(struct svc_xprt *xprt) |
| 351 | { |
| 352 | unsigned int limit = svc_rpc_per_connection_limit; |
| 353 | int nrqsts = atomic_read(&xprt->xpt_nr_rqsts); |
| 354 | |
| 355 | return limit == 0 || (nrqsts >= 0 && nrqsts < limit); |
| 356 | } |
| 357 | |
| 358 | static bool svc_xprt_reserve_slot(struct svc_rqst *rqstp, struct svc_xprt *xprt) |
| 359 | { |
| 360 | if (!test_bit(RQ_DATA, &rqstp->rq_flags)) { |
| 361 | if (!svc_xprt_slots_in_range(xprt)) |
| 362 | return false; |
| 363 | atomic_inc(&xprt->xpt_nr_rqsts); |
| 364 | set_bit(RQ_DATA, &rqstp->rq_flags); |
| 365 | } |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | static void svc_xprt_release_slot(struct svc_rqst *rqstp) |
| 370 | { |
| 371 | struct svc_xprt *xprt = rqstp->rq_xprt; |
| 372 | if (test_and_clear_bit(RQ_DATA, &rqstp->rq_flags)) { |
| 373 | atomic_dec(&xprt->xpt_nr_rqsts); |
J. Bruce Fields | 95503d2 | 2019-01-11 15:36:40 -0500 | [diff] [blame] | 374 | smp_wmb(); /* See smp_rmb() in svc_xprt_ready() */ |
Trond Myklebust | ff3ac5c | 2016-06-24 10:55:50 -0400 | [diff] [blame] | 375 | svc_xprt_enqueue(xprt); |
| 376 | } |
| 377 | } |
| 378 | |
J. Bruce Fields | 66c898ca | 2019-01-11 15:57:09 -0500 | [diff] [blame] | 379 | static bool svc_xprt_ready(struct svc_xprt *xprt) |
J. Bruce Fields | 9c335c0 | 2010-10-26 11:32:03 -0400 | [diff] [blame] | 380 | { |
Trond Myklebust | 1602a7b | 2019-01-03 09:17:12 -0500 | [diff] [blame] | 381 | unsigned long xpt_flags; |
| 382 | |
J. Bruce Fields | 95503d2 | 2019-01-11 15:36:40 -0500 | [diff] [blame] | 383 | /* |
| 384 | * If another cpu has recently updated xpt_flags, |
| 385 | * sk_sock->flags, xpt_reserved, or xpt_nr_rqsts, we need to |
| 386 | * know about it; otherwise it's possible that both that cpu and |
| 387 | * this one could call svc_xprt_enqueue() without either |
| 388 | * svc_xprt_enqueue() recognizing that the conditions below |
| 389 | * are satisfied, and we could stall indefinitely: |
| 390 | */ |
| 391 | smp_rmb(); |
Trond Myklebust | 1602a7b | 2019-01-03 09:17:12 -0500 | [diff] [blame] | 392 | xpt_flags = READ_ONCE(xprt->xpt_flags); |
| 393 | |
| 394 | if (xpt_flags & (BIT(XPT_CONN) | BIT(XPT_CLOSE))) |
J. Bruce Fields | 9c335c0 | 2010-10-26 11:32:03 -0400 | [diff] [blame] | 395 | return true; |
Trond Myklebust | 1602a7b | 2019-01-03 09:17:12 -0500 | [diff] [blame] | 396 | if (xpt_flags & (BIT(XPT_DATA) | BIT(XPT_DEFERRED))) { |
Trond Myklebust | ff3ac5c | 2016-06-24 10:55:50 -0400 | [diff] [blame] | 397 | if (xprt->xpt_ops->xpo_has_wspace(xprt) && |
| 398 | svc_xprt_slots_in_range(xprt)) |
Trond Myklebust | 82ea2d7 | 2016-06-24 10:55:45 -0400 | [diff] [blame] | 399 | return true; |
| 400 | trace_svc_xprt_no_write_space(xprt); |
| 401 | return false; |
| 402 | } |
J. Bruce Fields | 9c335c0 | 2010-10-26 11:32:03 -0400 | [diff] [blame] | 403 | return false; |
| 404 | } |
| 405 | |
Jeff Layton | b9e13cd | 2015-06-08 12:06:51 -0700 | [diff] [blame] | 406 | void svc_xprt_do_enqueue(struct svc_xprt *xprt) |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 407 | { |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 408 | struct svc_pool *pool; |
Jeff Layton | 83a712e | 2014-11-21 14:19:31 -0500 | [diff] [blame] | 409 | struct svc_rqst *rqstp = NULL; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 410 | int cpu; |
| 411 | |
J. Bruce Fields | 66c898ca | 2019-01-11 15:57:09 -0500 | [diff] [blame] | 412 | if (!svc_xprt_ready(xprt)) |
Chuck Lever | 7dbb53b | 2018-03-27 10:50:27 -0400 | [diff] [blame] | 413 | return; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 414 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 415 | /* Mark transport as busy. It will remain in this state until |
| 416 | * the provider calls svc_xprt_received. We update XPT_BUSY |
| 417 | * atomically because it also guards against trying to enqueue |
| 418 | * the transport twice. |
| 419 | */ |
Chuck Lever | 7dbb53b | 2018-03-27 10:50:27 -0400 | [diff] [blame] | 420 | if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) |
| 421 | return; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 422 | |
Trond Myklebust | 0c0746d | 2014-08-03 13:03:12 -0400 | [diff] [blame] | 423 | cpu = get_cpu(); |
| 424 | pool = svc_pool_for_cpu(xprt->xpt_server, cpu); |
Trond Myklebust | 0c0746d | 2014-08-03 13:03:12 -0400 | [diff] [blame] | 425 | |
Jeff Layton | 403c7b4 | 2014-11-21 14:19:29 -0500 | [diff] [blame] | 426 | atomic_long_inc(&pool->sp_stats.packets); |
Trond Myklebust | 0c0746d | 2014-08-03 13:03:12 -0400 | [diff] [blame] | 427 | |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 428 | spin_lock_bh(&pool->sp_lock); |
| 429 | list_add_tail(&xprt->xpt_ready, &pool->sp_sockets); |
| 430 | pool->sp_stats.sockets_queued++; |
| 431 | spin_unlock_bh(&pool->sp_lock); |
| 432 | |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 433 | /* find a thread for this xprt */ |
| 434 | rcu_read_lock(); |
| 435 | list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) { |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 436 | if (test_and_set_bit(RQ_BUSY, &rqstp->rq_flags)) |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 437 | continue; |
Jeff Layton | 403c7b4 | 2014-11-21 14:19:29 -0500 | [diff] [blame] | 438 | atomic_long_inc(&pool->sp_stats.threads_woken); |
Chuck Lever | 55f5088 | 2018-03-27 10:52:27 -0400 | [diff] [blame] | 439 | rqstp->rq_qtime = ktime_get(); |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 440 | wake_up_process(rqstp->rq_task); |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 441 | goto out_unlock; |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 442 | } |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 443 | set_bit(SP_CONGESTED, &pool->sp_flags); |
Jeff Layton | 83a712e | 2014-11-21 14:19:31 -0500 | [diff] [blame] | 444 | rqstp = NULL; |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 445 | out_unlock: |
| 446 | rcu_read_unlock(); |
Trond Myklebust | 983c684 | 2014-08-03 13:03:10 -0400 | [diff] [blame] | 447 | put_cpu(); |
Jeff Layton | 83a712e | 2014-11-21 14:19:31 -0500 | [diff] [blame] | 448 | trace_svc_xprt_do_enqueue(xprt, rqstp); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 449 | } |
Jeff Layton | b9e13cd | 2015-06-08 12:06:51 -0700 | [diff] [blame] | 450 | EXPORT_SYMBOL_GPL(svc_xprt_do_enqueue); |
Trond Myklebust | 0971374 | 2014-07-24 23:59:31 -0400 | [diff] [blame] | 451 | |
| 452 | /* |
| 453 | * Queue up a transport with data pending. If there are idle nfsd |
| 454 | * processes, wake 'em up. |
| 455 | * |
| 456 | */ |
| 457 | void svc_xprt_enqueue(struct svc_xprt *xprt) |
| 458 | { |
| 459 | if (test_bit(XPT_BUSY, &xprt->xpt_flags)) |
| 460 | return; |
Jeff Layton | b9e13cd | 2015-06-08 12:06:51 -0700 | [diff] [blame] | 461 | xprt->xpt_server->sv_ops->svo_enqueue_xprt(xprt); |
Trond Myklebust | 0971374 | 2014-07-24 23:59:31 -0400 | [diff] [blame] | 462 | } |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 463 | EXPORT_SYMBOL_GPL(svc_xprt_enqueue); |
| 464 | |
| 465 | /* |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 466 | * Dequeue the first transport, if there is one. |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 467 | */ |
| 468 | static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool) |
| 469 | { |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 470 | struct svc_xprt *xprt = NULL; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 471 | |
| 472 | if (list_empty(&pool->sp_sockets)) |
Jeff Layton | 83a712e | 2014-11-21 14:19:31 -0500 | [diff] [blame] | 473 | goto out; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 474 | |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 475 | spin_lock_bh(&pool->sp_lock); |
| 476 | if (likely(!list_empty(&pool->sp_sockets))) { |
| 477 | xprt = list_first_entry(&pool->sp_sockets, |
| 478 | struct svc_xprt, xpt_ready); |
| 479 | list_del_init(&xprt->xpt_ready); |
| 480 | svc_xprt_get(xprt); |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 481 | } |
| 482 | spin_unlock_bh(&pool->sp_lock); |
Jeff Layton | 83a712e | 2014-11-21 14:19:31 -0500 | [diff] [blame] | 483 | out: |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 484 | return xprt; |
| 485 | } |
| 486 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 487 | /** |
| 488 | * svc_reserve - change the space reserved for the reply to a request. |
| 489 | * @rqstp: The request in question |
| 490 | * @space: new max space to reserve |
| 491 | * |
| 492 | * Each request reserves some space on the output queue of the transport |
| 493 | * to make sure the reply fits. This function reduces that reserved |
| 494 | * space to be the amount of space used already, plus @space. |
| 495 | * |
| 496 | */ |
| 497 | void svc_reserve(struct svc_rqst *rqstp, int space) |
| 498 | { |
Vasily Averin | d4b09ac | 2018-12-24 14:44:52 +0300 | [diff] [blame] | 499 | struct svc_xprt *xprt = rqstp->rq_xprt; |
| 500 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 501 | space += rqstp->rq_res.head[0].iov_len; |
| 502 | |
Vasily Averin | d4b09ac | 2018-12-24 14:44:52 +0300 | [diff] [blame] | 503 | if (xprt && space < rqstp->rq_reserved) { |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 504 | atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved); |
| 505 | rqstp->rq_reserved = space; |
J. Bruce Fields | 95503d2 | 2019-01-11 15:36:40 -0500 | [diff] [blame] | 506 | smp_wmb(); /* See smp_rmb() in svc_xprt_ready() */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 507 | svc_xprt_enqueue(xprt); |
| 508 | } |
| 509 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 510 | EXPORT_SYMBOL_GPL(svc_reserve); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 511 | |
| 512 | static void svc_xprt_release(struct svc_rqst *rqstp) |
| 513 | { |
| 514 | struct svc_xprt *xprt = rqstp->rq_xprt; |
| 515 | |
Chuck Lever | 63a1b15 | 2018-03-27 10:49:22 -0400 | [diff] [blame] | 516 | xprt->xpt_ops->xpo_release_rqst(rqstp); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 517 | |
Tom Tucker | 2779e3a | 2009-01-05 11:12:52 -0600 | [diff] [blame] | 518 | kfree(rqstp->rq_deferred); |
| 519 | rqstp->rq_deferred = NULL; |
| 520 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 521 | svc_free_res_pages(rqstp); |
| 522 | rqstp->rq_res.page_len = 0; |
| 523 | rqstp->rq_res.page_base = 0; |
| 524 | |
| 525 | /* Reset response buffer and release |
| 526 | * the reservation. |
| 527 | * But first, check that enough space was reserved |
| 528 | * for the reply, otherwise we have a bug! |
| 529 | */ |
| 530 | if ((rqstp->rq_res.len) > rqstp->rq_reserved) |
| 531 | printk(KERN_ERR "RPC request reserved %d but used %d\n", |
| 532 | rqstp->rq_reserved, |
| 533 | rqstp->rq_res.len); |
| 534 | |
| 535 | rqstp->rq_res.head[0].iov_len = 0; |
| 536 | svc_reserve(rqstp, 0); |
Trond Myklebust | ff3ac5c | 2016-06-24 10:55:50 -0400 | [diff] [blame] | 537 | svc_xprt_release_slot(rqstp); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 538 | rqstp->rq_xprt = NULL; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 539 | svc_xprt_put(xprt); |
| 540 | } |
| 541 | |
| 542 | /* |
Jeff Layton | ceff739 | 2014-11-19 07:51:21 -0500 | [diff] [blame] | 543 | * Some svc_serv's will have occasional work to do, even when a xprt is not |
| 544 | * waiting to be serviced. This function is there to "kick" a task in one of |
| 545 | * those services so that it can wake up and do that work. Note that we only |
| 546 | * bother with pool 0 as we don't need to wake up more than one thread for |
| 547 | * this purpose. |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 548 | */ |
| 549 | void svc_wake_up(struct svc_serv *serv) |
| 550 | { |
| 551 | struct svc_rqst *rqstp; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 552 | struct svc_pool *pool; |
| 553 | |
Jeff Layton | ceff739 | 2014-11-19 07:51:21 -0500 | [diff] [blame] | 554 | pool = &serv->sv_pools[0]; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 555 | |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 556 | rcu_read_lock(); |
| 557 | list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) { |
| 558 | /* skip any that aren't queued */ |
| 559 | if (test_bit(RQ_BUSY, &rqstp->rq_flags)) |
| 560 | continue; |
| 561 | rcu_read_unlock(); |
Jeff Layton | ceff739 | 2014-11-19 07:51:21 -0500 | [diff] [blame] | 562 | wake_up_process(rqstp->rq_task); |
Jeff Layton | 83a712e | 2014-11-21 14:19:31 -0500 | [diff] [blame] | 563 | trace_svc_wake_up(rqstp->rq_task->pid); |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 564 | return; |
| 565 | } |
| 566 | rcu_read_unlock(); |
| 567 | |
| 568 | /* No free entries available */ |
| 569 | set_bit(SP_TASK_PENDING, &pool->sp_flags); |
| 570 | smp_wmb(); |
Jeff Layton | 83a712e | 2014-11-21 14:19:31 -0500 | [diff] [blame] | 571 | trace_svc_wake_up(0); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 572 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 573 | EXPORT_SYMBOL_GPL(svc_wake_up); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 574 | |
| 575 | int svc_port_is_privileged(struct sockaddr *sin) |
| 576 | { |
| 577 | switch (sin->sa_family) { |
| 578 | case AF_INET: |
| 579 | return ntohs(((struct sockaddr_in *)sin)->sin_port) |
| 580 | < PROT_SOCK; |
| 581 | case AF_INET6: |
| 582 | return ntohs(((struct sockaddr_in6 *)sin)->sin6_port) |
| 583 | < PROT_SOCK; |
| 584 | default: |
| 585 | return 0; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /* |
Jeff Layton | c9233eb | 2008-10-20 11:51:57 -0400 | [diff] [blame] | 590 | * Make sure that we don't have too many active connections. If we have, |
| 591 | * something must be dropped. It's not clear what will happen if we allow |
| 592 | * "too many" connections, but when dealing with network-facing software, |
| 593 | * we have to code defensively. Here we do that by imposing hard limits. |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 594 | * |
| 595 | * There's no point in trying to do random drop here for DoS |
| 596 | * prevention. The NFS clients does 1 reconnect in 15 seconds. An |
| 597 | * attacker can easily beat that. |
| 598 | * |
| 599 | * The only somewhat efficient mechanism would be if drop old |
| 600 | * connections from the same IP first. But right now we don't even |
| 601 | * record the client IP in svc_sock. |
Jeff Layton | c9233eb | 2008-10-20 11:51:57 -0400 | [diff] [blame] | 602 | * |
| 603 | * single-threaded services that expect a lot of clients will probably |
| 604 | * need to set sv_maxconn to override the default value which is based |
| 605 | * on the number of threads |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 606 | */ |
| 607 | static void svc_check_conn_limits(struct svc_serv *serv) |
| 608 | { |
Jeff Layton | c9233eb | 2008-10-20 11:51:57 -0400 | [diff] [blame] | 609 | unsigned int limit = serv->sv_maxconn ? serv->sv_maxconn : |
| 610 | (serv->sv_nrthreads+3) * 20; |
| 611 | |
| 612 | if (serv->sv_tmpcnt > limit) { |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 613 | struct svc_xprt *xprt = NULL; |
| 614 | spin_lock_bh(&serv->sv_lock); |
| 615 | if (!list_empty(&serv->sv_tempsocks)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 616 | /* Try to help the admin */ |
| 617 | net_notice_ratelimited("%s: too many open connections, consider increasing the %s\n", |
| 618 | serv->sv_name, serv->sv_maxconn ? |
| 619 | "max number of connections" : |
| 620 | "number of threads"); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 621 | /* |
| 622 | * Always select the oldest connection. It's not fair, |
| 623 | * but so is life |
| 624 | */ |
| 625 | xprt = list_entry(serv->sv_tempsocks.prev, |
| 626 | struct svc_xprt, |
| 627 | xpt_list); |
| 628 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
| 629 | svc_xprt_get(xprt); |
| 630 | } |
| 631 | spin_unlock_bh(&serv->sv_lock); |
| 632 | |
| 633 | if (xprt) { |
| 634 | svc_xprt_enqueue(xprt); |
| 635 | svc_xprt_put(xprt); |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
Rashika Kheria | e1d83ee | 2014-02-09 22:33:03 +0530 | [diff] [blame] | 640 | static int svc_alloc_arg(struct svc_rqst *rqstp) |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 641 | { |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 642 | struct svc_serv *serv = rqstp->rq_server; |
| 643 | struct xdr_buf *arg; |
| 644 | int pages; |
| 645 | int i; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 646 | |
| 647 | /* now allocate needed pages. If we get a failure, sleep briefly */ |
Chuck Lever | 8c6ae49 | 2017-06-30 12:03:54 -0400 | [diff] [blame] | 648 | pages = (serv->sv_max_mesg + 2 * PAGE_SIZE) >> PAGE_SHIFT; |
| 649 | if (pages > RPCSVC_MAXPAGES) { |
| 650 | pr_warn_once("svc: warning: pages=%u > RPCSVC_MAXPAGES=%lu\n", |
| 651 | pages, RPCSVC_MAXPAGES); |
Weston Andros Adamson | b25cd05 | 2012-10-23 10:43:41 -0400 | [diff] [blame] | 652 | /* use as many pages as possible */ |
Chuck Lever | 8c6ae49 | 2017-06-30 12:03:54 -0400 | [diff] [blame] | 653 | pages = RPCSVC_MAXPAGES; |
| 654 | } |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 655 | for (i = 0; i < pages ; i++) |
| 656 | while (rqstp->rq_pages[i] == NULL) { |
| 657 | struct page *p = alloc_page(GFP_KERNEL); |
| 658 | if (!p) { |
Jeff Layton | 7b54fe6 | 2008-02-12 11:47:24 -0500 | [diff] [blame] | 659 | set_current_state(TASK_INTERRUPTIBLE); |
| 660 | if (signalled() || kthread_should_stop()) { |
| 661 | set_current_state(TASK_RUNNING); |
Jeff Layton | 7086721 | 2008-02-07 16:34:54 -0500 | [diff] [blame] | 662 | return -EINTR; |
Jeff Layton | 7b54fe6 | 2008-02-12 11:47:24 -0500 | [diff] [blame] | 663 | } |
| 664 | schedule_timeout(msecs_to_jiffies(500)); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 665 | } |
| 666 | rqstp->rq_pages[i] = p; |
| 667 | } |
J. Bruce Fields | 2825a7f | 2013-08-26 16:04:46 -0400 | [diff] [blame] | 668 | rqstp->rq_page_end = &rqstp->rq_pages[i]; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 669 | rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 670 | |
| 671 | /* Make arg->head point to first page and arg->pages point to rest */ |
| 672 | arg = &rqstp->rq_arg; |
| 673 | arg->head[0].iov_base = page_address(rqstp->rq_pages[0]); |
| 674 | arg->head[0].iov_len = PAGE_SIZE; |
| 675 | arg->pages = rqstp->rq_pages + 1; |
| 676 | arg->page_base = 0; |
| 677 | /* save at least one page for response */ |
| 678 | arg->page_len = (pages-2)*PAGE_SIZE; |
| 679 | arg->len = (pages-1)*PAGE_SIZE; |
| 680 | arg->tail[0].iov_len = 0; |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 681 | return 0; |
| 682 | } |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 683 | |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 684 | static bool |
| 685 | rqst_should_sleep(struct svc_rqst *rqstp) |
| 686 | { |
| 687 | struct svc_pool *pool = rqstp->rq_pool; |
| 688 | |
| 689 | /* did someone call svc_wake_up? */ |
| 690 | if (test_and_clear_bit(SP_TASK_PENDING, &pool->sp_flags)) |
| 691 | return false; |
| 692 | |
| 693 | /* was a socket queued? */ |
| 694 | if (!list_empty(&pool->sp_sockets)) |
| 695 | return false; |
| 696 | |
| 697 | /* are we shutting down? */ |
| 698 | if (signalled() || kthread_should_stop()) |
| 699 | return false; |
| 700 | |
| 701 | /* are we freezing? */ |
| 702 | if (freezing(current)) |
| 703 | return false; |
| 704 | |
| 705 | return true; |
| 706 | } |
| 707 | |
Rashika Kheria | e1d83ee | 2014-02-09 22:33:03 +0530 | [diff] [blame] | 708 | static struct svc_xprt *svc_get_next_xprt(struct svc_rqst *rqstp, long timeout) |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 709 | { |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 710 | struct svc_pool *pool = rqstp->rq_pool; |
Trond Myklebust | a4aa805 | 2014-08-03 13:03:11 -0400 | [diff] [blame] | 711 | long time_left = 0; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 712 | |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 713 | /* rq_xprt should be clear on entry */ |
| 714 | WARN_ON_ONCE(rqstp->rq_xprt); |
| 715 | |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 716 | rqstp->rq_xprt = svc_xprt_dequeue(pool); |
| 717 | if (rqstp->rq_xprt) |
| 718 | goto out_found; |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 719 | |
| 720 | /* |
| 721 | * We have to be able to interrupt this wait |
| 722 | * to bring down the daemons ... |
| 723 | */ |
| 724 | set_current_state(TASK_INTERRUPTIBLE); |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 725 | smp_mb__before_atomic(); |
| 726 | clear_bit(SP_CONGESTED, &pool->sp_flags); |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 727 | clear_bit(RQ_BUSY, &rqstp->rq_flags); |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 728 | smp_mb__after_atomic(); |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 729 | |
| 730 | if (likely(rqst_should_sleep(rqstp))) |
| 731 | time_left = schedule_timeout(timeout); |
| 732 | else |
| 733 | __set_current_state(TASK_RUNNING); |
| 734 | |
| 735 | try_to_freeze(); |
| 736 | |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 737 | set_bit(RQ_BUSY, &rqstp->rq_flags); |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 738 | smp_mb__after_atomic(); |
| 739 | rqstp->rq_xprt = svc_xprt_dequeue(pool); |
| 740 | if (rqstp->rq_xprt) |
| 741 | goto out_found; |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 742 | |
| 743 | if (!time_left) |
| 744 | atomic_long_inc(&pool->sp_stats.threads_timedout); |
| 745 | |
| 746 | if (signalled() || kthread_should_stop()) |
| 747 | return ERR_PTR(-EINTR); |
| 748 | return ERR_PTR(-EAGAIN); |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 749 | out_found: |
| 750 | /* Normally we will wait up to 5 seconds for any required |
| 751 | * cache information to be provided. |
| 752 | */ |
| 753 | if (!test_bit(SP_CONGESTED, &pool->sp_flags)) |
| 754 | rqstp->rq_chandle.thread_wait = 5*HZ; |
| 755 | else |
| 756 | rqstp->rq_chandle.thread_wait = 1*HZ; |
Chuck Lever | 55f5088 | 2018-03-27 10:52:27 -0400 | [diff] [blame] | 757 | trace_svc_xprt_dequeue(rqstp); |
Trond Myklebust | 22700f3c | 2017-10-10 17:31:43 -0400 | [diff] [blame] | 758 | return rqstp->rq_xprt; |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 759 | } |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 760 | |
Rashika Kheria | e1d83ee | 2014-02-09 22:33:03 +0530 | [diff] [blame] | 761 | static void svc_add_new_temp_xprt(struct svc_serv *serv, struct svc_xprt *newxpt) |
J. Bruce Fields | 65b2e66 | 2012-08-18 15:44:33 -0400 | [diff] [blame] | 762 | { |
| 763 | spin_lock_bh(&serv->sv_lock); |
| 764 | set_bit(XPT_TEMP, &newxpt->xpt_flags); |
| 765 | list_add(&newxpt->xpt_list, &serv->sv_tempsocks); |
| 766 | serv->sv_tmpcnt++; |
| 767 | if (serv->sv_temptimer.function == NULL) { |
| 768 | /* setup timer to age temp transports */ |
Kees Cook | 841b86f | 2017-10-23 09:40:42 +0200 | [diff] [blame] | 769 | serv->sv_temptimer.function = svc_age_temp_xprts; |
J. Bruce Fields | 65b2e66 | 2012-08-18 15:44:33 -0400 | [diff] [blame] | 770 | mod_timer(&serv->sv_temptimer, |
| 771 | jiffies + svc_conn_age_period * HZ); |
| 772 | } |
| 773 | spin_unlock_bh(&serv->sv_lock); |
| 774 | svc_xprt_received(newxpt); |
| 775 | } |
| 776 | |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 777 | static int svc_handle_xprt(struct svc_rqst *rqstp, struct svc_xprt *xprt) |
| 778 | { |
| 779 | struct svc_serv *serv = rqstp->rq_server; |
| 780 | int len = 0; |
| 781 | |
J. Bruce Fields | 1b644b6 | 2010-02-28 16:33:31 -0500 | [diff] [blame] | 782 | if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) { |
| 783 | dprintk("svc_recv: found XPT_CLOSE\n"); |
Scott Mayhew | 546125d | 2017-01-05 16:34:51 -0500 | [diff] [blame] | 784 | if (test_and_clear_bit(XPT_KILL_TEMP, &xprt->xpt_flags)) |
| 785 | xprt->xpt_ops->xpo_kill_temp_xprt(xprt); |
J. Bruce Fields | 1b644b6 | 2010-02-28 16:33:31 -0500 | [diff] [blame] | 786 | svc_delete_xprt(xprt); |
J. Bruce Fields | ca7896cd | 2010-10-25 14:12:40 -0400 | [diff] [blame] | 787 | /* Leave XPT_BUSY set on the dead xprt: */ |
Jeff Layton | 83a712e | 2014-11-21 14:19:31 -0500 | [diff] [blame] | 788 | goto out; |
J. Bruce Fields | ca7896cd | 2010-10-25 14:12:40 -0400 | [diff] [blame] | 789 | } |
| 790 | if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) { |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 791 | struct svc_xprt *newxpt; |
J. Bruce Fields | 65b2e66 | 2012-08-18 15:44:33 -0400 | [diff] [blame] | 792 | /* |
| 793 | * We know this module_get will succeed because the |
| 794 | * listener holds a reference too |
| 795 | */ |
| 796 | __module_get(xprt->xpt_class->xcl_owner); |
| 797 | svc_check_conn_limits(xprt->xpt_server); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 798 | newxpt = xprt->xpt_ops->xpo_accept(xprt); |
Trond Myklebust | 1237d35 | 2019-04-09 12:13:38 -0400 | [diff] [blame] | 799 | if (newxpt) { |
| 800 | newxpt->xpt_cred = get_cred(xprt->xpt_cred); |
J. Bruce Fields | 65b2e66 | 2012-08-18 15:44:33 -0400 | [diff] [blame] | 801 | svc_add_new_temp_xprt(serv, newxpt); |
Trond Myklebust | 1237d35 | 2019-04-09 12:13:38 -0400 | [diff] [blame] | 802 | } else |
Trond Myklebust | c789102 | 2014-05-18 14:05:22 -0400 | [diff] [blame] | 803 | module_put(xprt->xpt_class->xcl_owner); |
Trond Myklebust | ff3ac5c | 2016-06-24 10:55:50 -0400 | [diff] [blame] | 804 | } else if (svc_xprt_reserve_slot(rqstp, xprt)) { |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 805 | /* XPT_DATA|XPT_DEFERRED case: */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 806 | dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n", |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 807 | rqstp, rqstp->rq_pool->sp_id, xprt, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 808 | kref_read(&xprt->xpt_ref)); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 809 | rqstp->rq_deferred = svc_deferred_dequeue(xprt); |
J. Bruce Fields | ca7896cd | 2010-10-25 14:12:40 -0400 | [diff] [blame] | 810 | if (rqstp->rq_deferred) |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 811 | len = svc_deferred_recv(rqstp); |
J. Bruce Fields | ca7896cd | 2010-10-25 14:12:40 -0400 | [diff] [blame] | 812 | else |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 813 | len = xprt->xpt_ops->xpo_recvfrom(rqstp); |
Chuck Lever | b20dfc3 | 2020-03-02 15:01:08 -0500 | [diff] [blame] | 814 | if (len > 0) |
| 815 | trace_svc_recvfrom(&rqstp->rq_arg); |
Chuck Lever | aaba72c | 2018-03-27 10:51:39 -0400 | [diff] [blame] | 816 | rqstp->rq_stime = ktime_get(); |
J. Bruce Fields | d10f27a | 2012-08-17 17:31:53 -0400 | [diff] [blame] | 817 | rqstp->rq_reserved = serv->sv_max_mesg; |
| 818 | atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 819 | } |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 820 | /* clear XPT_BUSY: */ |
J. Bruce Fields | ca7896cd | 2010-10-25 14:12:40 -0400 | [diff] [blame] | 821 | svc_xprt_received(xprt); |
Jeff Layton | 83a712e | 2014-11-21 14:19:31 -0500 | [diff] [blame] | 822 | out: |
| 823 | trace_svc_handle_xprt(xprt, len); |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 824 | return len; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * Receive the next request on any transport. This code is carefully |
| 829 | * organised not to touch any cachelines in the shared svc_serv |
| 830 | * structure, only cachelines in the local svc_pool. |
| 831 | */ |
| 832 | int svc_recv(struct svc_rqst *rqstp, long timeout) |
| 833 | { |
| 834 | struct svc_xprt *xprt = NULL; |
| 835 | struct svc_serv *serv = rqstp->rq_server; |
| 836 | int len, err; |
| 837 | |
| 838 | dprintk("svc: server %p waiting for data (to = %ld)\n", |
| 839 | rqstp, timeout); |
| 840 | |
| 841 | if (rqstp->rq_xprt) |
| 842 | printk(KERN_ERR |
| 843 | "svc_recv: service %p, transport not NULL!\n", |
| 844 | rqstp); |
Trond Myklebust | 983c684 | 2014-08-03 13:03:10 -0400 | [diff] [blame] | 845 | |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 846 | err = svc_alloc_arg(rqstp); |
| 847 | if (err) |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 848 | goto out; |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 849 | |
| 850 | try_to_freeze(); |
| 851 | cond_resched(); |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 852 | err = -EINTR; |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 853 | if (signalled() || kthread_should_stop()) |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 854 | goto out; |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 855 | |
| 856 | xprt = svc_get_next_xprt(rqstp, timeout); |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 857 | if (IS_ERR(xprt)) { |
| 858 | err = PTR_ERR(xprt); |
| 859 | goto out; |
| 860 | } |
J. Bruce Fields | 6797fa5 | 2012-08-18 15:33:51 -0400 | [diff] [blame] | 861 | |
| 862 | len = svc_handle_xprt(rqstp, xprt); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 863 | |
| 864 | /* No data, incomplete (TCP) read, or accept() */ |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 865 | err = -EAGAIN; |
J. Bruce Fields | 9f9d2eb | 2012-08-17 21:35:24 -0400 | [diff] [blame] | 866 | if (len <= 0) |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 867 | goto out_release; |
J. Bruce Fields | ca7896cd | 2010-10-25 14:12:40 -0400 | [diff] [blame] | 868 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 869 | clear_bit(XPT_OLD, &xprt->xpt_flags); |
| 870 | |
Chuck Lever | 989f881 | 2018-03-27 10:49:38 -0400 | [diff] [blame] | 871 | xprt->xpt_ops->xpo_secure_port(rqstp); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 872 | rqstp->rq_chandle.defer = svc_defer; |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 873 | rqstp->rq_xid = svc_getu32(&rqstp->rq_arg.head[0]); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 874 | |
| 875 | if (serv->sv_stats) |
| 876 | serv->sv_stats->netcnt++; |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 877 | trace_svc_recv(rqstp, len); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 878 | return len; |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 879 | out_release: |
J. Bruce Fields | ca7896cd | 2010-10-25 14:12:40 -0400 | [diff] [blame] | 880 | rqstp->rq_res.len = 0; |
| 881 | svc_xprt_release(rqstp); |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 882 | out: |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 883 | return err; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 884 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 885 | EXPORT_SYMBOL_GPL(svc_recv); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 886 | |
| 887 | /* |
| 888 | * Drop request |
| 889 | */ |
| 890 | void svc_drop(struct svc_rqst *rqstp) |
| 891 | { |
Trond Myklebust | 104f635 | 2016-06-24 10:55:46 -0400 | [diff] [blame] | 892 | trace_svc_drop(rqstp); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 893 | dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt); |
| 894 | svc_xprt_release(rqstp); |
| 895 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 896 | EXPORT_SYMBOL_GPL(svc_drop); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 897 | |
| 898 | /* |
| 899 | * Return reply to client. |
| 900 | */ |
| 901 | int svc_send(struct svc_rqst *rqstp) |
| 902 | { |
| 903 | struct svc_xprt *xprt; |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 904 | int len = -EFAULT; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 905 | struct xdr_buf *xb; |
| 906 | |
| 907 | xprt = rqstp->rq_xprt; |
| 908 | if (!xprt) |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 909 | goto out; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 910 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 911 | /* calculate over-all length */ |
| 912 | xb = &rqstp->rq_res; |
| 913 | xb->len = xb->head[0].iov_len + |
| 914 | xb->page_len + |
| 915 | xb->tail[0].iov_len; |
Chuck Lever | b20dfc3 | 2020-03-02 15:01:08 -0500 | [diff] [blame] | 916 | trace_svc_sendto(xb); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 917 | |
| 918 | /* Grab mutex to serialize outgoing data. */ |
| 919 | mutex_lock(&xprt->xpt_mutex); |
Chuck Lever | aaba72c | 2018-03-27 10:51:39 -0400 | [diff] [blame] | 920 | trace_svc_stats_latency(rqstp); |
J. Bruce Fields | f06f00a | 2012-08-20 16:04:40 -0400 | [diff] [blame] | 921 | if (test_bit(XPT_DEAD, &xprt->xpt_flags) |
| 922 | || test_bit(XPT_CLOSE, &xprt->xpt_flags)) |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 923 | len = -ENOTCONN; |
| 924 | else |
| 925 | len = xprt->xpt_ops->xpo_sendto(rqstp); |
| 926 | mutex_unlock(&xprt->xpt_mutex); |
Chuck Lever | ece200d | 2018-03-27 10:51:00 -0400 | [diff] [blame] | 927 | trace_svc_send(rqstp, len); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 928 | svc_xprt_release(rqstp); |
| 929 | |
| 930 | if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN) |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 931 | len = 0; |
| 932 | out: |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 933 | return len; |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * Timer function to close old temporary transports, using |
| 938 | * a mark-and-sweep algorithm. |
| 939 | */ |
Kees Cook | ff861c4 | 2017-10-16 17:29:42 -0700 | [diff] [blame] | 940 | static void svc_age_temp_xprts(struct timer_list *t) |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 941 | { |
Kees Cook | ff861c4 | 2017-10-16 17:29:42 -0700 | [diff] [blame] | 942 | struct svc_serv *serv = from_timer(serv, t, sv_temptimer); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 943 | struct svc_xprt *xprt; |
| 944 | struct list_head *le, *next; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 945 | |
| 946 | dprintk("svc_age_temp_xprts\n"); |
| 947 | |
| 948 | if (!spin_trylock_bh(&serv->sv_lock)) { |
| 949 | /* busy, try again 1 sec later */ |
| 950 | dprintk("svc_age_temp_xprts: busy\n"); |
| 951 | mod_timer(&serv->sv_temptimer, jiffies + HZ); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | list_for_each_safe(le, next, &serv->sv_tempsocks) { |
| 956 | xprt = list_entry(le, struct svc_xprt, xpt_list); |
| 957 | |
| 958 | /* First time through, just mark it OLD. Second time |
| 959 | * through, close it. */ |
| 960 | if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags)) |
| 961 | continue; |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 962 | if (kref_read(&xprt->xpt_ref) > 1 || |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 963 | test_bit(XPT_BUSY, &xprt->xpt_flags)) |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 964 | continue; |
J. Bruce Fields | e75bafb | 2013-02-10 11:33:48 -0500 | [diff] [blame] | 965 | list_del_init(le); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 966 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 967 | dprintk("queuing xprt %p for closing\n", xprt); |
| 968 | |
| 969 | /* a thread will dequeue and close it soon */ |
| 970 | svc_xprt_enqueue(xprt); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 971 | } |
J. Bruce Fields | e75bafb | 2013-02-10 11:33:48 -0500 | [diff] [blame] | 972 | spin_unlock_bh(&serv->sv_lock); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 973 | |
| 974 | mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ); |
| 975 | } |
| 976 | |
Scott Mayhew | c3d4879 | 2015-12-11 16:45:58 -0500 | [diff] [blame] | 977 | /* Close temporary transports whose xpt_local matches server_addr immediately |
| 978 | * instead of waiting for them to be picked up by the timer. |
| 979 | * |
| 980 | * This is meant to be called from a notifier_block that runs when an ip |
| 981 | * address is deleted. |
| 982 | */ |
| 983 | void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr) |
| 984 | { |
| 985 | struct svc_xprt *xprt; |
Scott Mayhew | c3d4879 | 2015-12-11 16:45:58 -0500 | [diff] [blame] | 986 | struct list_head *le, *next; |
| 987 | LIST_HEAD(to_be_closed); |
Scott Mayhew | c3d4879 | 2015-12-11 16:45:58 -0500 | [diff] [blame] | 988 | |
| 989 | spin_lock_bh(&serv->sv_lock); |
| 990 | list_for_each_safe(le, next, &serv->sv_tempsocks) { |
| 991 | xprt = list_entry(le, struct svc_xprt, xpt_list); |
| 992 | if (rpc_cmp_addr(server_addr, (struct sockaddr *) |
| 993 | &xprt->xpt_local)) { |
| 994 | dprintk("svc_age_temp_xprts_now: found %p\n", xprt); |
| 995 | list_move(le, &to_be_closed); |
| 996 | } |
| 997 | } |
| 998 | spin_unlock_bh(&serv->sv_lock); |
| 999 | |
| 1000 | while (!list_empty(&to_be_closed)) { |
| 1001 | le = to_be_closed.next; |
| 1002 | list_del_init(le); |
| 1003 | xprt = list_entry(le, struct svc_xprt, xpt_list); |
Scott Mayhew | 546125d | 2017-01-05 16:34:51 -0500 | [diff] [blame] | 1004 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
| 1005 | set_bit(XPT_KILL_TEMP, &xprt->xpt_flags); |
| 1006 | dprintk("svc_age_temp_xprts_now: queuing xprt %p for closing\n", |
| 1007 | xprt); |
| 1008 | svc_xprt_enqueue(xprt); |
Scott Mayhew | c3d4879 | 2015-12-11 16:45:58 -0500 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | EXPORT_SYMBOL_GPL(svc_age_temp_xprts_now); |
| 1012 | |
J. Bruce Fields | edc7a89 | 2010-03-22 15:37:17 -0400 | [diff] [blame] | 1013 | static void call_xpt_users(struct svc_xprt *xprt) |
| 1014 | { |
| 1015 | struct svc_xpt_user *u; |
| 1016 | |
| 1017 | spin_lock(&xprt->xpt_lock); |
| 1018 | while (!list_empty(&xprt->xpt_users)) { |
| 1019 | u = list_first_entry(&xprt->xpt_users, struct svc_xpt_user, list); |
Trond Myklebust | bb6ad55 | 2018-10-09 15:54:15 -0400 | [diff] [blame] | 1020 | list_del_init(&u->list); |
J. Bruce Fields | edc7a89 | 2010-03-22 15:37:17 -0400 | [diff] [blame] | 1021 | u->callback(u); |
| 1022 | } |
| 1023 | spin_unlock(&xprt->xpt_lock); |
| 1024 | } |
| 1025 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1026 | /* |
| 1027 | * Remove a dead transport |
| 1028 | */ |
J. Bruce Fields | 7710ec3 | 2011-11-25 18:44:05 -0500 | [diff] [blame] | 1029 | static void svc_delete_xprt(struct svc_xprt *xprt) |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1030 | { |
| 1031 | struct svc_serv *serv = xprt->xpt_server; |
Tom Tucker | 22945e4 | 2009-01-05 15:21:19 -0600 | [diff] [blame] | 1032 | struct svc_deferred_req *dr; |
| 1033 | |
| 1034 | /* Only do this once */ |
| 1035 | if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) |
J. Bruce Fields | ac9303e | 2010-10-23 11:16:10 -0400 | [diff] [blame] | 1036 | BUG(); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1037 | |
| 1038 | dprintk("svc: svc_delete_xprt(%p)\n", xprt); |
| 1039 | xprt->xpt_ops->xpo_detach(xprt); |
Chuck Lever | 6221f1d | 2020-04-17 12:40:31 -0400 | [diff] [blame] | 1040 | if (xprt->xpt_bc_xprt) |
| 1041 | xprt->xpt_bc_xprt->ops->close(xprt->xpt_bc_xprt); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1042 | |
| 1043 | spin_lock_bh(&serv->sv_lock); |
Jeff Layton | 8d65ef7 | 2014-11-17 17:02:57 -0500 | [diff] [blame] | 1044 | list_del_init(&xprt->xpt_list); |
Weston Andros Adamson | 0104729 | 2012-10-23 10:43:48 -0400 | [diff] [blame] | 1045 | WARN_ON_ONCE(!list_empty(&xprt->xpt_ready)); |
Tom Tucker | 22945e4 | 2009-01-05 15:21:19 -0600 | [diff] [blame] | 1046 | if (test_bit(XPT_TEMP, &xprt->xpt_flags)) |
| 1047 | serv->sv_tmpcnt--; |
J. Bruce Fields | 788e69e | 2010-03-29 21:02:31 -0400 | [diff] [blame] | 1048 | spin_unlock_bh(&serv->sv_lock); |
Tom Tucker | 22945e4 | 2009-01-05 15:21:19 -0600 | [diff] [blame] | 1049 | |
Neil Brown | ab1b18f | 2010-02-27 09:33:40 +1100 | [diff] [blame] | 1050 | while ((dr = svc_deferred_dequeue(xprt)) != NULL) |
Tom Tucker | 22945e4 | 2009-01-05 15:21:19 -0600 | [diff] [blame] | 1051 | kfree(dr); |
Tom Tucker | 22945e4 | 2009-01-05 15:21:19 -0600 | [diff] [blame] | 1052 | |
J. Bruce Fields | edc7a89 | 2010-03-22 15:37:17 -0400 | [diff] [blame] | 1053 | call_xpt_users(xprt); |
Tom Tucker | 22945e4 | 2009-01-05 15:21:19 -0600 | [diff] [blame] | 1054 | svc_xprt_put(xprt); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | void svc_close_xprt(struct svc_xprt *xprt) |
| 1058 | { |
| 1059 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
| 1060 | if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) |
| 1061 | /* someone else will have to effect the close */ |
| 1062 | return; |
J. Bruce Fields | b176331 | 2010-10-25 20:24:48 -0400 | [diff] [blame] | 1063 | /* |
| 1064 | * We expect svc_close_xprt() to work even when no threads are |
| 1065 | * running (e.g., while configuring the server before starting |
| 1066 | * any threads), so if the transport isn't busy, we delete |
| 1067 | * it ourself: |
| 1068 | */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1069 | svc_delete_xprt(xprt); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1070 | } |
Tom Tucker | a217813f | 2007-12-30 21:08:35 -0600 | [diff] [blame] | 1071 | EXPORT_SYMBOL_GPL(svc_close_xprt); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1072 | |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1073 | static int svc_close_list(struct svc_serv *serv, struct list_head *xprt_list, struct net *net) |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1074 | { |
| 1075 | struct svc_xprt *xprt; |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1076 | int ret = 0; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1077 | |
J. Bruce Fields | 719f8bc | 2012-08-13 17:03:00 -0400 | [diff] [blame] | 1078 | spin_lock(&serv->sv_lock); |
J. Bruce Fields | b4f36f8 | 2011-11-29 17:00:26 -0500 | [diff] [blame] | 1079 | list_for_each_entry(xprt, xprt_list, xpt_list) { |
Stanislav Kinsbursky | 7b147f1 | 2012-01-31 14:09:17 +0400 | [diff] [blame] | 1080 | if (xprt->xpt_net != net) |
| 1081 | continue; |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1082 | ret++; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1083 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1084 | svc_xprt_enqueue(xprt); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1085 | } |
J. Bruce Fields | 719f8bc | 2012-08-13 17:03:00 -0400 | [diff] [blame] | 1086 | spin_unlock(&serv->sv_lock); |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1087 | return ret; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1088 | } |
| 1089 | |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1090 | static struct svc_xprt *svc_dequeue_net(struct svc_serv *serv, struct net *net) |
J. Bruce Fields | 2fefb8a | 2011-11-29 11:35:35 -0500 | [diff] [blame] | 1091 | { |
J. Bruce Fields | b4f36f8 | 2011-11-29 17:00:26 -0500 | [diff] [blame] | 1092 | struct svc_pool *pool; |
| 1093 | struct svc_xprt *xprt; |
| 1094 | struct svc_xprt *tmp; |
| 1095 | int i; |
| 1096 | |
J. Bruce Fields | b4f36f8 | 2011-11-29 17:00:26 -0500 | [diff] [blame] | 1097 | for (i = 0; i < serv->sv_nrpools; i++) { |
| 1098 | pool = &serv->sv_pools[i]; |
| 1099 | |
| 1100 | spin_lock_bh(&pool->sp_lock); |
Stanislav Kinsbursky | 6f51336 | 2012-01-31 14:09:00 +0400 | [diff] [blame] | 1101 | list_for_each_entry_safe(xprt, tmp, &pool->sp_sockets, xpt_ready) { |
Stanislav Kinsbursky | 7b147f1 | 2012-01-31 14:09:17 +0400 | [diff] [blame] | 1102 | if (xprt->xpt_net != net) |
| 1103 | continue; |
J. Bruce Fields | b4f36f8 | 2011-11-29 17:00:26 -0500 | [diff] [blame] | 1104 | list_del_init(&xprt->xpt_ready); |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1105 | spin_unlock_bh(&pool->sp_lock); |
| 1106 | return xprt; |
J. Bruce Fields | b4f36f8 | 2011-11-29 17:00:26 -0500 | [diff] [blame] | 1107 | } |
| 1108 | spin_unlock_bh(&pool->sp_lock); |
| 1109 | } |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1110 | return NULL; |
Stanislav Kinsbursky | 6f51336 | 2012-01-31 14:09:00 +0400 | [diff] [blame] | 1111 | } |
| 1112 | |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1113 | static void svc_clean_up_xprts(struct svc_serv *serv, struct net *net) |
Stanislav Kinsbursky | 6f51336 | 2012-01-31 14:09:00 +0400 | [diff] [blame] | 1114 | { |
| 1115 | struct svc_xprt *xprt; |
Stanislav Kinsbursky | 6f51336 | 2012-01-31 14:09:00 +0400 | [diff] [blame] | 1116 | |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1117 | while ((xprt = svc_dequeue_net(serv, net))) { |
| 1118 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
J. Bruce Fields | 719f8bc | 2012-08-13 17:03:00 -0400 | [diff] [blame] | 1119 | svc_delete_xprt(xprt); |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1120 | } |
Stanislav Kinsbursky | 3a22bf5 | 2012-01-31 14:09:08 +0400 | [diff] [blame] | 1121 | } |
| 1122 | |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1123 | /* |
| 1124 | * Server threads may still be running (especially in the case where the |
| 1125 | * service is still running in other network namespaces). |
| 1126 | * |
| 1127 | * So we shut down sockets the same way we would on a running server, by |
| 1128 | * setting XPT_CLOSE, enqueuing, and letting a thread pick it up to do |
| 1129 | * the close. In the case there are no such other threads, |
| 1130 | * threads running, svc_clean_up_xprts() does a simple version of a |
| 1131 | * server's main event loop, and in the case where there are other |
| 1132 | * threads, we may need to wait a little while and then check again to |
| 1133 | * see if they're done. |
| 1134 | */ |
Stanislav Kinsbursky | 7b147f1 | 2012-01-31 14:09:17 +0400 | [diff] [blame] | 1135 | void svc_close_net(struct svc_serv *serv, struct net *net) |
Stanislav Kinsbursky | 3a22bf5 | 2012-01-31 14:09:08 +0400 | [diff] [blame] | 1136 | { |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1137 | int delay = 0; |
Stanislav Kinsbursky | 6f51336 | 2012-01-31 14:09:00 +0400 | [diff] [blame] | 1138 | |
J. Bruce Fields | cc630d9 | 2013-02-10 16:08:11 -0500 | [diff] [blame] | 1139 | while (svc_close_list(serv, &serv->sv_permsocks, net) + |
| 1140 | svc_close_list(serv, &serv->sv_tempsocks, net)) { |
| 1141 | |
| 1142 | svc_clean_up_xprts(serv, net); |
| 1143 | msleep(delay++); |
| 1144 | } |
J. Bruce Fields | 2fefb8a | 2011-11-29 11:35:35 -0500 | [diff] [blame] | 1145 | } |
| 1146 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1147 | /* |
| 1148 | * Handle defer and revisit of requests |
| 1149 | */ |
| 1150 | |
| 1151 | static void svc_revisit(struct cache_deferred_req *dreq, int too_many) |
| 1152 | { |
| 1153 | struct svc_deferred_req *dr = |
| 1154 | container_of(dreq, struct svc_deferred_req, handle); |
| 1155 | struct svc_xprt *xprt = dr->xprt; |
| 1156 | |
Tom Tucker | 22945e4 | 2009-01-05 15:21:19 -0600 | [diff] [blame] | 1157 | spin_lock(&xprt->xpt_lock); |
| 1158 | set_bit(XPT_DEFERRED, &xprt->xpt_flags); |
| 1159 | if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) { |
| 1160 | spin_unlock(&xprt->xpt_lock); |
| 1161 | dprintk("revisit canceled\n"); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1162 | svc_xprt_put(xprt); |
Trond Myklebust | 104f635 | 2016-06-24 10:55:46 -0400 | [diff] [blame] | 1163 | trace_svc_drop_deferred(dr); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1164 | kfree(dr); |
| 1165 | return; |
| 1166 | } |
| 1167 | dprintk("revisit queued\n"); |
| 1168 | dr->xprt = NULL; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1169 | list_add(&dr->handle.recent, &xprt->xpt_deferred); |
| 1170 | spin_unlock(&xprt->xpt_lock); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1171 | svc_xprt_enqueue(xprt); |
| 1172 | svc_xprt_put(xprt); |
| 1173 | } |
| 1174 | |
Tom Tucker | 260c1d1 | 2007-12-30 21:08:29 -0600 | [diff] [blame] | 1175 | /* |
| 1176 | * Save the request off for later processing. The request buffer looks |
| 1177 | * like this: |
| 1178 | * |
| 1179 | * <xprt-header><rpc-header><rpc-pagelist><rpc-tail> |
| 1180 | * |
| 1181 | * This code can only handle requests that consist of an xprt-header |
| 1182 | * and rpc-header. |
| 1183 | */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1184 | static struct cache_deferred_req *svc_defer(struct cache_req *req) |
| 1185 | { |
| 1186 | struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1187 | struct svc_deferred_req *dr; |
| 1188 | |
Jeff Layton | 30660e04b | 2014-11-19 07:51:16 -0500 | [diff] [blame] | 1189 | if (rqstp->rq_arg.page_len || !test_bit(RQ_USEDEFERRAL, &rqstp->rq_flags)) |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1190 | return NULL; /* if more than a page, give up FIXME */ |
| 1191 | if (rqstp->rq_deferred) { |
| 1192 | dr = rqstp->rq_deferred; |
| 1193 | rqstp->rq_deferred = NULL; |
| 1194 | } else { |
Tom Tucker | 260c1d1 | 2007-12-30 21:08:29 -0600 | [diff] [blame] | 1195 | size_t skip; |
| 1196 | size_t size; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1197 | /* FIXME maybe discard if size too large */ |
Tom Tucker | 260c1d1 | 2007-12-30 21:08:29 -0600 | [diff] [blame] | 1198 | size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1199 | dr = kmalloc(size, GFP_KERNEL); |
| 1200 | if (dr == NULL) |
| 1201 | return NULL; |
| 1202 | |
| 1203 | dr->handle.owner = rqstp->rq_server; |
| 1204 | dr->prot = rqstp->rq_prot; |
| 1205 | memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen); |
| 1206 | dr->addrlen = rqstp->rq_addrlen; |
| 1207 | dr->daddr = rqstp->rq_daddr; |
| 1208 | dr->argslen = rqstp->rq_arg.len >> 2; |
Tom Tucker | 260c1d1 | 2007-12-30 21:08:29 -0600 | [diff] [blame] | 1209 | dr->xprt_hlen = rqstp->rq_xprt_hlen; |
| 1210 | |
| 1211 | /* back up head to the start of the buffer and copy */ |
| 1212 | skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len; |
| 1213 | memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip, |
| 1214 | dr->argslen << 2); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1215 | } |
| 1216 | svc_xprt_get(rqstp->rq_xprt); |
| 1217 | dr->xprt = rqstp->rq_xprt; |
Jeff Layton | 78b65eb | 2014-11-19 07:51:17 -0500 | [diff] [blame] | 1218 | set_bit(RQ_DROPME, &rqstp->rq_flags); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1219 | |
| 1220 | dr->handle.revisit = svc_revisit; |
Trond Myklebust | 104f635 | 2016-06-24 10:55:46 -0400 | [diff] [blame] | 1221 | trace_svc_defer(rqstp); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1222 | return &dr->handle; |
| 1223 | } |
| 1224 | |
| 1225 | /* |
| 1226 | * recv data from a deferred request into an active one |
| 1227 | */ |
| 1228 | static int svc_deferred_recv(struct svc_rqst *rqstp) |
| 1229 | { |
| 1230 | struct svc_deferred_req *dr = rqstp->rq_deferred; |
| 1231 | |
Tom Tucker | 260c1d1 | 2007-12-30 21:08:29 -0600 | [diff] [blame] | 1232 | /* setup iov_base past transport header */ |
| 1233 | rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2); |
| 1234 | /* The iov_len does not include the transport header bytes */ |
| 1235 | rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1236 | rqstp->rq_arg.page_len = 0; |
Tom Tucker | 260c1d1 | 2007-12-30 21:08:29 -0600 | [diff] [blame] | 1237 | /* The rq_arg.len includes the transport header bytes */ |
| 1238 | rqstp->rq_arg.len = dr->argslen<<2; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1239 | rqstp->rq_prot = dr->prot; |
| 1240 | memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen); |
| 1241 | rqstp->rq_addrlen = dr->addrlen; |
Tom Tucker | 260c1d1 | 2007-12-30 21:08:29 -0600 | [diff] [blame] | 1242 | /* Save off transport header len in case we get deferred again */ |
| 1243 | rqstp->rq_xprt_hlen = dr->xprt_hlen; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1244 | rqstp->rq_daddr = dr->daddr; |
| 1245 | rqstp->rq_respages = rqstp->rq_pages; |
Tom Tucker | 260c1d1 | 2007-12-30 21:08:29 -0600 | [diff] [blame] | 1246 | return (dr->argslen<<2) - dr->xprt_hlen; |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt) |
| 1251 | { |
| 1252 | struct svc_deferred_req *dr = NULL; |
| 1253 | |
| 1254 | if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags)) |
| 1255 | return NULL; |
| 1256 | spin_lock(&xprt->xpt_lock); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1257 | if (!list_empty(&xprt->xpt_deferred)) { |
| 1258 | dr = list_entry(xprt->xpt_deferred.next, |
| 1259 | struct svc_deferred_req, |
| 1260 | handle.recent); |
| 1261 | list_del_init(&dr->handle.recent); |
Trond Myklebust | 104f635 | 2016-06-24 10:55:46 -0400 | [diff] [blame] | 1262 | trace_svc_revisit_deferred(dr); |
J. Bruce Fields | 62bac4a | 2010-10-25 12:50:15 -0400 | [diff] [blame] | 1263 | } else |
| 1264 | clear_bit(XPT_DEFERRED, &xprt->xpt_flags); |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1265 | spin_unlock(&xprt->xpt_lock); |
| 1266 | return dr; |
| 1267 | } |
Tom Tucker | 7fcb98d | 2007-12-30 21:08:33 -0600 | [diff] [blame] | 1268 | |
Chuck Lever | 156e620 | 2009-03-18 20:45:58 -0400 | [diff] [blame] | 1269 | /** |
| 1270 | * svc_find_xprt - find an RPC transport instance |
| 1271 | * @serv: pointer to svc_serv to search |
| 1272 | * @xcl_name: C string containing transport's class name |
Stanislav Kinsbursky | 4cb54ca | 2012-01-20 16:50:53 +0400 | [diff] [blame] | 1273 | * @net: owner net pointer |
Chuck Lever | 156e620 | 2009-03-18 20:45:58 -0400 | [diff] [blame] | 1274 | * @af: Address family of transport's local address |
| 1275 | * @port: transport's IP port number |
| 1276 | * |
Tom Tucker | 7fcb98d | 2007-12-30 21:08:33 -0600 | [diff] [blame] | 1277 | * Return the transport instance pointer for the endpoint accepting |
| 1278 | * connections/peer traffic from the specified transport class, |
| 1279 | * address family and port. |
| 1280 | * |
| 1281 | * Specifying 0 for the address family or port is effectively a |
| 1282 | * wild-card, and will result in matching the first transport in the |
| 1283 | * service's list that has a matching class name. |
| 1284 | */ |
Chuck Lever | 156e620 | 2009-03-18 20:45:58 -0400 | [diff] [blame] | 1285 | struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name, |
Stanislav Kinsbursky | 4cb54ca | 2012-01-20 16:50:53 +0400 | [diff] [blame] | 1286 | struct net *net, const sa_family_t af, |
| 1287 | const unsigned short port) |
Tom Tucker | 7fcb98d | 2007-12-30 21:08:33 -0600 | [diff] [blame] | 1288 | { |
| 1289 | struct svc_xprt *xprt; |
| 1290 | struct svc_xprt *found = NULL; |
| 1291 | |
| 1292 | /* Sanity check the args */ |
Chuck Lever | 156e620 | 2009-03-18 20:45:58 -0400 | [diff] [blame] | 1293 | if (serv == NULL || xcl_name == NULL) |
Tom Tucker | 7fcb98d | 2007-12-30 21:08:33 -0600 | [diff] [blame] | 1294 | return found; |
| 1295 | |
| 1296 | spin_lock_bh(&serv->sv_lock); |
| 1297 | list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) { |
Stanislav Kinsbursky | 4cb54ca | 2012-01-20 16:50:53 +0400 | [diff] [blame] | 1298 | if (xprt->xpt_net != net) |
| 1299 | continue; |
Tom Tucker | 7fcb98d | 2007-12-30 21:08:33 -0600 | [diff] [blame] | 1300 | if (strcmp(xprt->xpt_class->xcl_name, xcl_name)) |
| 1301 | continue; |
| 1302 | if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family) |
| 1303 | continue; |
Chuck Lever | 156e620 | 2009-03-18 20:45:58 -0400 | [diff] [blame] | 1304 | if (port != 0 && port != svc_xprt_local_port(xprt)) |
Tom Tucker | 7fcb98d | 2007-12-30 21:08:33 -0600 | [diff] [blame] | 1305 | continue; |
| 1306 | found = xprt; |
Tom Tucker | a217813f | 2007-12-30 21:08:35 -0600 | [diff] [blame] | 1307 | svc_xprt_get(xprt); |
Tom Tucker | 7fcb98d | 2007-12-30 21:08:33 -0600 | [diff] [blame] | 1308 | break; |
| 1309 | } |
| 1310 | spin_unlock_bh(&serv->sv_lock); |
| 1311 | return found; |
| 1312 | } |
| 1313 | EXPORT_SYMBOL_GPL(svc_find_xprt); |
Tom Tucker | 9571af1 | 2007-12-30 21:08:37 -0600 | [diff] [blame] | 1314 | |
Chuck Lever | 335c54b | 2009-04-23 19:32:25 -0400 | [diff] [blame] | 1315 | static int svc_one_xprt_name(const struct svc_xprt *xprt, |
| 1316 | char *pos, int remaining) |
| 1317 | { |
| 1318 | int len; |
| 1319 | |
| 1320 | len = snprintf(pos, remaining, "%s %u\n", |
| 1321 | xprt->xpt_class->xcl_name, |
| 1322 | svc_xprt_local_port(xprt)); |
| 1323 | if (len >= remaining) |
| 1324 | return -ENAMETOOLONG; |
| 1325 | return len; |
| 1326 | } |
| 1327 | |
| 1328 | /** |
| 1329 | * svc_xprt_names - format a buffer with a list of transport names |
| 1330 | * @serv: pointer to an RPC service |
| 1331 | * @buf: pointer to a buffer to be filled in |
| 1332 | * @buflen: length of buffer to be filled in |
| 1333 | * |
| 1334 | * Fills in @buf with a string containing a list of transport names, |
| 1335 | * each name terminated with '\n'. |
| 1336 | * |
| 1337 | * Returns positive length of the filled-in string on success; otherwise |
| 1338 | * a negative errno value is returned if an error occurs. |
Tom Tucker | 9571af1 | 2007-12-30 21:08:37 -0600 | [diff] [blame] | 1339 | */ |
Chuck Lever | 335c54b | 2009-04-23 19:32:25 -0400 | [diff] [blame] | 1340 | int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen) |
Tom Tucker | 9571af1 | 2007-12-30 21:08:37 -0600 | [diff] [blame] | 1341 | { |
| 1342 | struct svc_xprt *xprt; |
Chuck Lever | 335c54b | 2009-04-23 19:32:25 -0400 | [diff] [blame] | 1343 | int len, totlen; |
| 1344 | char *pos; |
Tom Tucker | 9571af1 | 2007-12-30 21:08:37 -0600 | [diff] [blame] | 1345 | |
| 1346 | /* Sanity check args */ |
| 1347 | if (!serv) |
| 1348 | return 0; |
| 1349 | |
| 1350 | spin_lock_bh(&serv->sv_lock); |
Chuck Lever | 335c54b | 2009-04-23 19:32:25 -0400 | [diff] [blame] | 1351 | |
| 1352 | pos = buf; |
| 1353 | totlen = 0; |
Tom Tucker | 9571af1 | 2007-12-30 21:08:37 -0600 | [diff] [blame] | 1354 | list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) { |
Chuck Lever | 335c54b | 2009-04-23 19:32:25 -0400 | [diff] [blame] | 1355 | len = svc_one_xprt_name(xprt, pos, buflen - totlen); |
| 1356 | if (len < 0) { |
| 1357 | *buf = '\0'; |
| 1358 | totlen = len; |
| 1359 | } |
| 1360 | if (len <= 0) |
Tom Tucker | 9571af1 | 2007-12-30 21:08:37 -0600 | [diff] [blame] | 1361 | break; |
Chuck Lever | 335c54b | 2009-04-23 19:32:25 -0400 | [diff] [blame] | 1362 | |
| 1363 | pos += len; |
Tom Tucker | 9571af1 | 2007-12-30 21:08:37 -0600 | [diff] [blame] | 1364 | totlen += len; |
| 1365 | } |
Chuck Lever | 335c54b | 2009-04-23 19:32:25 -0400 | [diff] [blame] | 1366 | |
Tom Tucker | 9571af1 | 2007-12-30 21:08:37 -0600 | [diff] [blame] | 1367 | spin_unlock_bh(&serv->sv_lock); |
| 1368 | return totlen; |
| 1369 | } |
| 1370 | EXPORT_SYMBOL_GPL(svc_xprt_names); |
Greg Banks | 03cf6c9 | 2009-01-13 21:26:36 +1100 | [diff] [blame] | 1371 | |
| 1372 | |
| 1373 | /*----------------------------------------------------------------------------*/ |
| 1374 | |
| 1375 | static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos) |
| 1376 | { |
| 1377 | unsigned int pidx = (unsigned int)*pos; |
| 1378 | struct svc_serv *serv = m->private; |
| 1379 | |
| 1380 | dprintk("svc_pool_stats_start, *pidx=%u\n", pidx); |
| 1381 | |
Greg Banks | 03cf6c9 | 2009-01-13 21:26:36 +1100 | [diff] [blame] | 1382 | if (!pidx) |
| 1383 | return SEQ_START_TOKEN; |
| 1384 | return (pidx > serv->sv_nrpools ? NULL : &serv->sv_pools[pidx-1]); |
| 1385 | } |
| 1386 | |
| 1387 | static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos) |
| 1388 | { |
| 1389 | struct svc_pool *pool = p; |
| 1390 | struct svc_serv *serv = m->private; |
| 1391 | |
| 1392 | dprintk("svc_pool_stats_next, *pos=%llu\n", *pos); |
| 1393 | |
| 1394 | if (p == SEQ_START_TOKEN) { |
| 1395 | pool = &serv->sv_pools[0]; |
| 1396 | } else { |
| 1397 | unsigned int pidx = (pool - &serv->sv_pools[0]); |
| 1398 | if (pidx < serv->sv_nrpools-1) |
| 1399 | pool = &serv->sv_pools[pidx+1]; |
| 1400 | else |
| 1401 | pool = NULL; |
| 1402 | } |
| 1403 | ++*pos; |
| 1404 | return pool; |
| 1405 | } |
| 1406 | |
| 1407 | static void svc_pool_stats_stop(struct seq_file *m, void *p) |
| 1408 | { |
Greg Banks | 03cf6c9 | 2009-01-13 21:26:36 +1100 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | static int svc_pool_stats_show(struct seq_file *m, void *p) |
| 1412 | { |
| 1413 | struct svc_pool *pool = p; |
| 1414 | |
| 1415 | if (p == SEQ_START_TOKEN) { |
J. Bruce Fields | 78c210e | 2009-08-06 15:41:34 -0400 | [diff] [blame] | 1416 | seq_puts(m, "# pool packets-arrived sockets-enqueued threads-woken threads-timedout\n"); |
Greg Banks | 03cf6c9 | 2009-01-13 21:26:36 +1100 | [diff] [blame] | 1417 | return 0; |
| 1418 | } |
| 1419 | |
J. Bruce Fields | 78c210e | 2009-08-06 15:41:34 -0400 | [diff] [blame] | 1420 | seq_printf(m, "%u %lu %lu %lu %lu\n", |
Greg Banks | 03cf6c9 | 2009-01-13 21:26:36 +1100 | [diff] [blame] | 1421 | pool->sp_id, |
Jeff Layton | 403c7b4 | 2014-11-21 14:19:29 -0500 | [diff] [blame] | 1422 | (unsigned long)atomic_long_read(&pool->sp_stats.packets), |
Greg Banks | 03cf6c9 | 2009-01-13 21:26:36 +1100 | [diff] [blame] | 1423 | pool->sp_stats.sockets_queued, |
Jeff Layton | 403c7b4 | 2014-11-21 14:19:29 -0500 | [diff] [blame] | 1424 | (unsigned long)atomic_long_read(&pool->sp_stats.threads_woken), |
| 1425 | (unsigned long)atomic_long_read(&pool->sp_stats.threads_timedout)); |
Greg Banks | 03cf6c9 | 2009-01-13 21:26:36 +1100 | [diff] [blame] | 1426 | |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | static const struct seq_operations svc_pool_stats_seq_ops = { |
| 1431 | .start = svc_pool_stats_start, |
| 1432 | .next = svc_pool_stats_next, |
| 1433 | .stop = svc_pool_stats_stop, |
| 1434 | .show = svc_pool_stats_show, |
| 1435 | }; |
| 1436 | |
| 1437 | int svc_pool_stats_open(struct svc_serv *serv, struct file *file) |
| 1438 | { |
| 1439 | int err; |
| 1440 | |
| 1441 | err = seq_open(file, &svc_pool_stats_seq_ops); |
| 1442 | if (!err) |
| 1443 | ((struct seq_file *) file->private_data)->private = serv; |
| 1444 | return err; |
| 1445 | } |
| 1446 | EXPORT_SYMBOL(svc_pool_stats_open); |
| 1447 | |
| 1448 | /*----------------------------------------------------------------------------*/ |