nfsd, sunrpc: allow for a dynamically-sized threadpool
Subsystems (4)
Jeff Layton
maintainerv2 cover nfsd, sunrpc: allow for a dynamically-sized threadpool
This version of the patchset fixes a number of warts in the first, and hopefully gets this closer to something mergeable.
This patchset allows nfsd to dynamically size its threadpool as needed. The main user-visible change is the addition of new controls that allow the admin to set a minimum number of threads.
When the minimum is set to a non-zero value, the traditional "threads" setting is interpreted as a maximum number of threads instead of a static count. The server will start the minimum number of threads, and then ramp up the thread count as needed. When the server is idle, it will gradually ramp down the thread count.
This control scheme should allow us to sanely switch between kernels that do and do not support dynamic threading. In the case where dynamic threading is not supported, the user will just get the static maximum number of threads, just like they do today.
So far this is only lightly tested, but it seems to work well. I still need to do some benchmarking to see whether this affects performance, so I'm posting this as an RFC for now.
Does this approach look sane to everyone?
---
Changes in v2:
- svc_recv() now takes a timeout parameter. This should mean that
non-dynamic RPC services are unaffected by these changes.
- if min_threads is larger than the max, then clamp it to the max
- simplify SP_TASK_STARTING usage. Have same task set and clear it.
- rework thread starting logic (EBUSY handling)
- reorder arguments to svc_set_num_threads() and svc_set_pool_threads()
- break up larger patches
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Jeff Layton (8):
sunrpc: split svc_set_num_threads() into two functions
sunrpc: remove special handling of NULL pool from svc_start/stop_kthreads()
sunrpc: track the max number of requested threads in a pool
sunrpc: introduce the concept of a minimum number of threads per pool
sunrpc: split new thread creation into a separate function
sunrpc: allow svc_recv() to return -ETIMEDOUT and -EBUSY
nfsd: adjust number of running nfsd threads based on activity
nfsd: add controls to set the minimum number of threads per pool
Documentation/netlink/specs/nfsd.yaml | 5 + fs/lockd/svc.c | 6 +- fs/nfs/callback.c | 10 +- fs/nfsd/netlink.c | 5 +- fs/nfsd/netns.h | 6 + fs/nfsd/nfsctl.c | 50 ++++++++ fs/nfsd/nfssvc.c | 63 +++++++--- fs/nfsd/trace.h | 54 +++++++++ include/linux/sunrpc/svc.h | 13 ++- include/linux/sunrpc/svcsock.h | 2 +- include/uapi/linux/nfsd_netlink.h | 1 + net/sunrpc/svc.c | 210 ++++++++++++++++++++-------------- net/sunrpc/svc_xprt.c | 44 +++++-- 13 files changed, 349 insertions(+), 120 deletions(-)
---
base-commit: 83f633515af9382e7201e205112e18b995a80f70
change-id: 20251212-nfsd-dynathread-9f7a31172005
Best regards,
Signature · 1 line
Jeff Layton <[email protected]>
Jeff Layton
maintainerPATCH v2 1/8 sunrpc: split svc_set_num_threads() into two functions
svc_set_num_threads() will set the number of running threads for a given pool. If the pool argument is set to NULL however, it will distribute the threads among all of the pools evenly.
These divergent codepaths complicate the move to dynamic threading. Simplify the API by splitting these two cases into different helpers:
Add a new svc_set_pool_threads() function that sets the number of threads in a single, given pool. Modify svc_set_num_threads() to distribute the threads evenly between all of the pools and then call svc_set_pool_threads() for each.
--- fs/lockd/svc.c | 4 +-- fs/nfs/callback.c | 8 +++--- fs/nfsd/nfssvc.c | 21 +++++++-------- include/linux/sunrpc/svc.h | 4 ++- net/sunrpc/svc.c | 67 +++++++++++++++++++++++++++++++++++----------- 5 files changed, 70 insertions(+), 34 deletions(-)
fs/lockd/svc.c +2 -2
@@ -340,7 +340,7 @@ static int lockd_get(void)return -ENOMEM ; } - error =svc_set_num_threads (serv,NULL ,1 ); + error =svc_set_num_threads (serv,1 );if (error <0 ) {svc_destroy (&serv);return error;
@@ -368,7 +368,7 @@ static void lockd_put(void)unregister_inet6addr_notifier (&lockd_inet6addr_notifier);#endif -svc_set_num_threads (nlmsvc_serv,NULL ,0 ); +svc_set_num_threads (nlmsvc_serv,0 );timer_delete_sync (&nlmsvc_retry);svc_destroy (&nlmsvc_serv);dprintk ("lockd_down: service destroyed\n" );
fs/nfs/callback.c +4 -4
@@ -119,9 +119,9 @@ static int nfs_callback_start_svc(int minorversion, struct rpc_xprt *xprt,if (serv->sv_nrthreads == nrservs)return 0 ; - ret =svc_set_num_threads (serv,NULL , nrservs); + ret =svc_set_num_threads (serv, nrservs);if (ret) { -svc_set_num_threads (serv,NULL ,0 ); +svc_set_num_threads (serv,0 );return ret; }dprintk ("nfs_callback_up: service started\n" );
@@ -242,7 +242,7 @@ int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt) cb_info->users++; err_net:if (!cb_info->users) { -svc_set_num_threads (cb_info->serv,NULL ,0 ); +svc_set_num_threads (cb_info->serv,0 );svc_destroy (&cb_info->serv); } err_create:
@@ -268,7 +268,7 @@ void nfs_callback_down(int minorversion, struct net *net, struct rpc_xprt *xprt)nfs_callback_down_net (minorversion, serv, net); cb_info->users--;if (cb_info->users ==0 ) { -svc_set_num_threads (serv,NULL ,0 ); +svc_set_num_threads (serv,0 );dprintk ("nfs_callback_down: service destroyed\n" );xprt_svc_destroy_nullify_bc (xprt, &cb_info->serv); }
fs/nfsd/nfssvc.c +9 -12
@@ -580,7 +580,7 @@ void nfsd_shutdown_threads(struct net *net) }/* Kill outstanding nfsd threads */ -svc_set_num_threads (serv,NULL ,0 ); +svc_set_num_threads (serv,0 );nfsd_destroy_serv (net);mutex_unlock (&nfsd_mutex); }
@@ -688,12 +688,9 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)if (nn->nfsd_serv ==NULL || n <=0 )return 0 ; -/* -* Special case: When n == 1, pass in NULL for the pool, so that the -* change is distributed equally among them. -*/ +/* Special case: When n == 1, distribute threads equally among pools. */ if (n ==1 ) -return svc_set_num_threads (nn->nfsd_serv,NULL , nthreads[0 ]); +return svc_set_num_threads (nn->nfsd_serv, nthreads[0 ]);if (n > nn->nfsd_serv->sv_nrpools) n = nn->nfsd_serv->sv_nrpools;
@@ -719,18 +716,18 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)/* apply the new numbers */ for (i =0 ; i < n; i++) { - err =svc_set_num_threads (nn->nfsd_serv, - &nn->nfsd_serv->sv_pools[i], - nthreads[i]); + err =svc_set_pool_threads (nn->nfsd_serv, + &nn->nfsd_serv->sv_pools[i], + nthreads[i]);if (err) goto out; }/* Anything undefined in array is considered to be 0 */ for (i = n; i < nn->nfsd_serv->sv_nrpools; ++i) { - err =svc_set_num_threads (nn->nfsd_serv, - &nn->nfsd_serv->sv_pools[i], -0 ); + err =svc_set_pool_threads (nn->nfsd_serv, + &nn->nfsd_serv->sv_pools[i], +0 );if (err) goto out; }
include/linux/sunrpc/svc.h +3 -1
@@ -446,7 +446,9 @@ struct svc_serv * svc_create_pooled(struct svc_program *prog,struct svc_stat *stats, unsigned int bufsize, int (*threadfn)(void *data)); -int svc_set_num_threads (struct svc_serv *,struct svc_pool *,int ); +int svc_set_pool_threads (struct svc_serv *serv,struct svc_pool *pool, +unsigned int nrservs); +int svc_set_num_threads (struct svc_serv *serv,unsigned int nrservs);int svc_pool_stats_open (struct svc_info *si,struct file *file);void svc_process (struct svc_rqst *rqstp);void svc_process_bc (struct rpc_rqst *req,struct svc_rqst *rqstp);
net/sunrpc/svc.c +52 -15
86 lines not on this page · raw message
Signature · 1 line
2.52.0
Jeff Layton
maintainerPATCH v2 2/8 sunrpc: remove special handling of NULL pool from svc_start/stop_kthreads()
In reply to Jeff LaytonNow that svc_set_num_threads() handles distributing the threads among the available pools, remove the special handling of a NULL pool pointer from svc_start_kthreads() and svc_stop_kthreads().
--- net/sunrpc/svc.c | 53 +++++++---------------------------------------------- 1 file changed, 7 insertions(+), 46 deletions(-)
net/sunrpc/svc.c +7 -46
@@ -763,53 +763,19 @@ void svc_pool_wake_idle_thread(struct svc_pool *pool) }EXPORT_SYMBOL_GPL (svc_pool_wake_idle_thread); -static struct svc_pool * -svc_pool_next (struct svc_serv *serv,struct svc_pool *pool,unsigned int *state) -{ -return pool ? pool : &serv->sv_pools[(*state)++ % serv->sv_nrpools]; -} - -static struct svc_pool * -svc_pool_victim (struct svc_serv *serv,struct svc_pool *target_pool, -unsigned int *state) -{ -struct svc_pool *pool; -unsigned int i; - - pool = target_pool; - -if (!pool) { -for (i =0 ; i < serv->sv_nrpools; i++) { - pool = &serv->sv_pools[--(*state) % serv->sv_nrpools]; -if (pool->sp_nrthreads) -break ; - } - } - -if (pool && pool->sp_nrthreads) { -set_bit (SP_VICTIM_REMAINS , &pool->sp_flags); -set_bit (SP_NEED_VICTIM , &pool->sp_flags); -return pool; - } -return NULL ; -} - static int svc_start_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs) { struct svc_rqst *rqstp; struct task_struct *task; - struct svc_pool *chosen_pool; - unsigned int state = serv->sv_nrthreads-1; int node; int err; do { nrservs--; - chosen_pool = svc_pool_next(serv, pool, &state); - node = svc_pool_map_get_node(chosen_pool->sp_id); + node = svc_pool_map_get_node(pool->sp_id); - rqstp = svc_prepare_thread(serv, chosen_pool, node); + rqstp = svc_prepare_thread(serv, pool, node); if (!rqstp) return -ENOMEM; task = kthread_create_on_node(serv->sv_threadfn, rqstp,
@@ -821,7 +787,7 @@ svc_start_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs) rqstp->rq_task = task;if (serv->sv_nrpools >1 ) -svc_pool_map_set_cpumask (task, chosen_pool->sp_id); +svc_pool_map_set_cpumask (task, pool->sp_id);svc_sock_update_bufs (serv);wake_up_process (task);
@@ -840,16 +806,11 @@ svc_start_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)static int svc_stop_kthreads (struct svc_serv *serv,struct svc_pool *pool,int nrservs) { -unsigned int state = serv->sv_nrthreads-1 ; -struct svc_pool *victim; -do { - victim =svc_pool_victim (serv, pool, &state); -if (!victim) -break ; -svc_pool_wake_idle_thread (victim); -wait_on_bit (&victim->sp_flags,SP_VICTIM_REMAINS , -TASK_IDLE ); +set_bit (SP_VICTIM_REMAINS , &pool->sp_flags); +set_bit (SP_NEED_VICTIM , &pool->sp_flags); +svc_pool_wake_idle_thread (pool); +wait_on_bit (&pool->sp_flags,SP_VICTIM_REMAINS ,TASK_IDLE ); nrservs++; }while (nrservs <0 );return 0 ;
Signature · 1 line
2.52.0
Jeff Layton
maintainerPATCH v2 3/8 sunrpc: track the max number of requested threads in a pool
In reply to Jeff LaytonThe kernel currently tracks the number of threads running in a pool in the "sp_nrthreads" field. In the future, where threads are dynamically spun up and down, it'll be necessary to keep track of the maximum number of requested threads separately from the actual number running.
Add a pool->sp_nrthrmax parameter to track this. When userland changes the number of threads in a pool, update that value accordingly.
--- include/linux/sunrpc/svc.h | 3 ++- net/sunrpc/svc.c | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/sunrpc/svc.h +2 -1
@@ -35,8 +35,9 @@*/ struct svc_pool {unsigned int sp_id;/* pool id; also node id on NUMA */ +unsigned int sp_nrthreads;/* # of threads currently running in pool */ +unsigned int sp_nrthrmax;/* Max requested number of threads in pool */ struct lwq sp_xprts;/* pending transports */ -unsigned int sp_nrthreads;/* # of threads in pool */ struct list_head sp_all_threads;/* all server threads */ struct llist_head sp_idle_threads;/* idle server threads */
net/sunrpc/svc.c +1 -0
@@ -839,6 +839,7 @@ svc_set_pool_threads(struct svc_serv *serv, struct svc_pool *pool,if (!pool)return -EINVAL ; + pool->sp_nrthrmax = nrservs; delta -= pool->sp_nrthreads;if (delta >0 )
Signature · 1 line
2.52.0
Jeff Layton
maintainerPATCH v2 4/8 sunrpc: introduce the concept of a minimum number of threads per pool
In reply to Jeff LaytonAdd a new pool->sp_nrthrmin field to track the minimum number of threads in a pool. Add min_threads parameters to both svc_set_num_threads() and svc_set_pool_threads(). If min_threads is non-zero and less than the max, svc_set_num_threads() will ensure that the number of running threads is between the min and the max.
If the min is 0 or greater than the max, then it is ignored, and the maximum number of threads will be started, and never spun down.
For now, the min_threads is always 0, but a later patch will pass the proper value through from nfsd.
--- fs/lockd/svc.c | 4 ++-- fs/nfs/callback.c | 8 ++++---- fs/nfsd/nfssvc.c | 8 ++++---- include/linux/sunrpc/svc.h | 8 +++++--- net/sunrpc/svc.c | 45 +++++++++++++++++++++++++++++++++++++-------- 5 files changed, 52 insertions(+), 21 deletions(-)
fs/lockd/svc.c +2 -2
@@ -340,7 +340,7 @@ static int lockd_get(void)return -ENOMEM ; } - error =svc_set_num_threads (serv,1 ); + error =svc_set_num_threads (serv,0 ,1 );if (error <0 ) {svc_destroy (&serv);return error;
@@ -368,7 +368,7 @@ static void lockd_put(void)unregister_inet6addr_notifier (&lockd_inet6addr_notifier);#endif -svc_set_num_threads (nlmsvc_serv,0 ); +svc_set_num_threads (nlmsvc_serv,0 ,0 );timer_delete_sync (&nlmsvc_retry);svc_destroy (&nlmsvc_serv);dprintk ("lockd_down: service destroyed\n" );
fs/nfs/callback.c +4 -4
@@ -119,9 +119,9 @@ static int nfs_callback_start_svc(int minorversion, struct rpc_xprt *xprt,if (serv->sv_nrthreads == nrservs)return 0 ; - ret =svc_set_num_threads (serv, nrservs); + ret =svc_set_num_threads (serv,0 , nrservs);if (ret) { -svc_set_num_threads (serv,0 ); +svc_set_num_threads (serv,0 ,0 );return ret; }dprintk ("nfs_callback_up: service started\n" );
@@ -242,7 +242,7 @@ int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt) cb_info->users++; err_net:if (!cb_info->users) { -svc_set_num_threads (cb_info->serv,0 ); +svc_set_num_threads (cb_info->serv,0 ,0 );svc_destroy (&cb_info->serv); } err_create:
@@ -268,7 +268,7 @@ void nfs_callback_down(int minorversion, struct net *net, struct rpc_xprt *xprt)nfs_callback_down_net (minorversion, serv, net); cb_info->users--;if (cb_info->users ==0 ) { -svc_set_num_threads (serv,0 ); +svc_set_num_threads (serv,0 ,0 );dprintk ("nfs_callback_down: service destroyed\n" );xprt_svc_destroy_nullify_bc (xprt, &cb_info->serv); }
fs/nfsd/nfssvc.c +4 -4
@@ -580,7 +580,7 @@ void nfsd_shutdown_threads(struct net *net) }/* Kill outstanding nfsd threads */ -svc_set_num_threads (serv,0 ); +svc_set_num_threads (serv,0 ,0 );nfsd_destroy_serv (net);mutex_unlock (&nfsd_mutex); }
@@ -690,7 +690,7 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)/* Special case: When n == 1, distribute threads equally among pools. */ if (n ==1 ) -return svc_set_num_threads (nn->nfsd_serv, nthreads[0 ]); +return svc_set_num_threads (nn->nfsd_serv,0 , nthreads[0 ]);if (n > nn->nfsd_serv->sv_nrpools) n = nn->nfsd_serv->sv_nrpools;
@@ -718,7 +718,7 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)for (i =0 ; i < n; i++) { err =svc_set_pool_threads (nn->nfsd_serv, &nn->nfsd_serv->sv_pools[i], - nthreads[i]); +0 , nthreads[i]);if (err) goto out; }
@@ -727,7 +727,7 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)for (i = n; i < nn->nfsd_serv->sv_nrpools; ++i) { err =svc_set_pool_threads (nn->nfsd_serv, &nn->nfsd_serv->sv_pools[i], -0 ); +0 ,0 );if (err) goto out; }
include/linux/sunrpc/svc.h +5 -3
@@ -36,6 +36,7 @@struct svc_pool {unsigned int sp_id;/* pool id; also node id on NUMA */ unsigned int sp_nrthreads;/* # of threads currently running in pool */ +unsigned int sp_nrthrmin;/* Min number of threads to run per pool */ unsigned int sp_nrthrmax;/* Max requested number of threads in pool */ struct lwq sp_xprts;/* pending transports */ struct list_head sp_all_threads;/* all server threads */
@@ -72,7 +73,7 @@ struct svc_serv {struct svc_stat * sv_stats;/* RPC statistics */ spinlock_t sv_lock;unsigned int sv_nprogs;/* Number of sv_programs */ -unsigned int sv_nrthreads;/* # of server threads */ +unsigned int sv_nrthreads;/* # of running server threads */ unsigned int sv_max_payload;/* datagram payload size */ unsigned int sv_max_mesg;/* max_payload + 1 page for overheads */ unsigned int sv_xdrsize;/* XDR buffer size */
@@ -448,8 +449,9 @@ struct svc_serv * svc_create_pooled(struct svc_program *prog,unsigned int bufsize, int (*threadfn)(void *data));int svc_set_pool_threads (struct svc_serv *serv,struct svc_pool *pool, -unsigned int nrservs); -int svc_set_num_threads (struct svc_serv *serv,unsigned int nrservs); +unsigned int min_threads,unsigned int max_threads); +int svc_set_num_threads (struct svc_serv *serv,unsigned int min_threads, +unsigned int nrservs);int svc_pool_stats_open (struct svc_info *si,struct file *file);void svc_process (struct svc_rqst *rqstp);void svc_process_bc (struct rpc_rqst *req,struct svc_rqst *rqstp);
net/sunrpc/svc.c +37 -8
88 lines not on this page · raw message
Signature · 1 line
2.52.0
Jeff Layton
maintainerPATCH v2 5/8 sunrpc: split new thread creation into a separate function
In reply to Jeff LaytonBreak out the part of svc_start_kthreads() that creates a thread into svc_new_thread(), as a new exported helper function.
--- include/linux/sunrpc/svc.h | 1 + net/sunrpc/svc.c | 72 +++++++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 29 deletions(-)
include/linux/sunrpc/svc.h +1 -0
@@ -442,6 +442,7 @@ struct svc_serv *svc_create(struct svc_program *, unsigned int,bool svc_rqst_replace_page (struct svc_rqst *rqstp,struct page *page);void svc_rqst_release_pages (struct svc_rqst *rqstp); +int svc_new_thread (struct svc_serv *serv,struct svc_pool *pool);void svc_exit_thread (struct svc_rqst *); struct svc_serv * svc_create_pooled(struct svc_program *prog, unsigned int nprog,
net/sunrpc/svc.c +43 -29
87 lines not on this page · raw message
Signature · 1 line
2.52.0
Jeff Layton
maintainerPATCH v2 6/8 sunrpc: allow svc_recv() to return -ETIMEDOUT and -EBUSY
In reply to Jeff LaytonTo dynamically adjust the thread count, nfsd requires some information about how busy things are.
Change svc_recv() to take a timeout value, and then allow the wait for work to time out if it's set. If a timeout is not defined, then the schedule will be set to MAX_SCHEDULE_TIMEOUT. If the task waits for the full timeout, then have it return -ETIMEDOUT to the caller.
If it wakes up, finds that there is more work and that no threads are available, then attempt to set SP_TASK_STARTING. If wasn't already set, have the task return -EBUSY to cue to the caller that the service could use more threads.
--- fs/lockd/svc.c | 2 +- fs/nfs/callback.c | 2 +- fs/nfsd/nfssvc.c | 2 +- include/linux/sunrpc/svc.h | 1 + include/linux/sunrpc/svcsock.h | 2 +- net/sunrpc/svc_xprt.c | 44 +++++++++++++++++++++++++++++++++--------- 6 files changed, 40 insertions(+), 13 deletions(-)
fs/lockd/svc.c +1 -1
@@ -141,7 +141,7 @@ lockd(void *vrqstp)*/ while (!svc_thread_should_stop (rqstp)) {nlmsvc_retry_blocked (rqstp); -svc_recv (rqstp); +svc_recv (rqstp,0 ); }if (nlmsvc_ops)nlmsvc_invalidate_all ();
fs/nfs/callback.c +1 -1
@@ -81,7 +81,7 @@ nfs4_callback_svc(void *vrqstp)set_freezable ();while (!svc_thread_should_stop (rqstp)) -svc_recv (rqstp); +svc_recv (rqstp,0 );svc_exit_thread (rqstp);return 0 ;
fs/nfsd/nfssvc.c +1 -1
@@ -902,7 +902,7 @@ nfsd(void *vrqstp)* The main request loop */ while (!svc_thread_should_stop (rqstp)) { -svc_recv (rqstp); +svc_recv (rqstp,0 );nfsd_file_net_dispose (nn); }
include/linux/sunrpc/svc.h +1 -0
@@ -55,6 +55,7 @@ enum {SP_TASK_PENDING ,/* still work to do even if no xprt is queued */ SP_NEED_VICTIM ,/* One thread needs to agree to exit */ SP_VICTIM_REMAINS ,/* One thread needs to actually exit */ + SP_TASK_STARTING, /* Task has started but not added to idle yet */ };
include/linux/sunrpc/svcsock.h +1 -1
@@ -61,7 +61,7 @@ static inline u32 svc_sock_final_rec(struct svc_sock *svsk)/* * Function prototypes. */ -void svc_recv (struct svc_rqst *rqstp); +int svc_recv (struct svc_rqst *rqstp,long timeo);void svc_send (struct svc_rqst *rqstp); int svc_addsock(struct svc_serv *serv, struct net *net, const int fd, char *name_return, const size_t len,
net/sunrpc/svc_xprt.c +35 -9
104 lines not on this page · raw message
Signature · 1 line
2.52.0
Jeff Layton
maintainerPATCH v2 7/8 nfsd: adjust number of running nfsd threads based on activity
In reply to Jeff Laytonnfsd() is changed to pass a timeout to svc_recv() when there is a min number of threads set, and to handle error returns from it:
In the case of -ETIMEDOUT, if the service mutex can be taken (via trylock), the thread becomes an RQ_VICTIM so that it will exit, providing that the actual number of threads is above pool->sp_nrthrmin.
In the case of -EBUSY, if the actual number of threads is below pool->sp_nrthrmax, it will attempt to start a new thread. This attempt is gated on a new SP_TASK_STARTING pool flag that serializes thread creation attempts within a pool, and further by mutex_trylock().
Neil says: "I think we want memory pressure to be able to push a thread into returning -ETIMEDOUT. That can come later."
--- fs/nfsd/nfssvc.c | 42 +++++++++++++++++++++++++++++++++++++++++- fs/nfsd/trace.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-)
fs/nfsd/nfssvc.c +41 -1
@@ -882,9 +882,11 @@ static int nfsd(void *vrqstp) {struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp; +struct svc_pool *pool = rqstp->rq_pool; struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);struct net *net = perm_sock->xpt_net;struct nfsd_net *nn =net_generic (net, nfsd_net_id); +bool have_mutex = false;/* At this point, the thread shares current->fs * with the init process. We need to create files with the
@@ -902,7 +904,43 @@ nfsd(void *vrqstp)* The main request loop */ while (!svc_thread_should_stop (rqstp)) { -svc_recv (rqstp,0 ); +switch (svc_recv (rqstp,5 *HZ )) { +case -ETIMEDOUT : +/* Nothing to do */ +if (mutex_trylock (&nfsd_mutex)) { +if (pool->sp_nrthreads > pool->sp_nrthrmin) { +trace_nfsd_dynthread_kill (net, pool); +set_bit (RQ_VICTIM , &rqstp->rq_flags); + have_mutex = true; + }else +mutex_unlock (&nfsd_mutex); + }else { +trace_nfsd_dynthread_trylock_fail (net, pool); + } +break ; +case -EBUSY : +/* Too much to do */ +if (pool->sp_nrthreads < pool->sp_nrthrmax) { +if (mutex_trylock (&nfsd_mutex)) { +if (pool->sp_nrthreads < pool->sp_nrthrmax) { +int ret; + +trace_nfsd_dynthread_start (net, pool); + ret =svc_new_thread (rqstp->rq_server, pool); +if (ret) +pr_notice_ratelimited ("%s: unable to spawn new thread: %d\n" , + __func__, ret); + } +mutex_unlock (&nfsd_mutex); + }else { +trace_nfsd_dynthread_trylock_fail (net, pool); + } + } +clear_bit (SP_TASK_STARTING , &pool->sp_flags); +break ; +default : +break ; + }nfsd_file_net_dispose (nn); }
@@ -910,6 +948,8 @@ nfsd(void *vrqstp)/* Release the thread */ svc_exit_thread (rqstp); +if (have_mutex) +mutex_unlock (&nfsd_mutex);return 0 ; }
fs/nfsd/trace.h +35 -0
@@ -91,6 +91,41 @@ DEFINE_EVENT(nfsd_xdr_err_class, nfsd_##name##_err, \DEFINE_NFSD_XDR_ERR_EVENT (garbage_args);DEFINE_NFSD_XDR_ERR_EVENT (cant_encode); +DECLARE_EVENT_CLASS (nfsd_dynthread_class, +TP_PROTO ( + const struct net *net, + const struct svc_pool *pool + ), +TP_ARGS (net, pool), +TP_STRUCT__entry ( + __field(unsigned int, netns_ino) + __field(unsigned int, pool_id) + __field(unsigned int, nrthreads) + __field(unsigned int, nrthrmin) + __field(unsigned int, nrthrmax) + ), +TP_fast_assign ( + __entry->netns_ino = net->ns.inum; + __entry->pool_id = pool->sp_id; + __entry->nrthreads = pool->sp_nrthreads; + __entry->nrthrmin = pool->sp_nrthrmin; + __entry->nrthrmax = pool->sp_nrthrmax; + ), +TP_printk ("pool=%u nrthreads=%u nrthrmin=%u nrthrmax=%u" , + __entry->pool_id, __entry->nrthreads, + __entry->nrthrmin, __entry->nrthrmax + ) +); + +#define DEFINE_NFSD_DYNTHREAD_EVENT (name) \ +DEFINE_EVENT(nfsd_dynthread_class, nfsd_dynthread_##name, \ + TP_PROTO(const struct net *net, const struct svc_pool *pool), \ + TP_ARGS(net, pool)) + +DEFINE_NFSD_DYNTHREAD_EVENT (start); +DEFINE_NFSD_DYNTHREAD_EVENT (kill); +DEFINE_NFSD_DYNTHREAD_EVENT (trylock_fail); +#define show_nfsd_may_flags (x) \ __print_flags(x, "|", \ { NFSD_MAY_EXEC, "EXEC" }, \
Signature · 1 line
2.52.0
Jeff Layton
maintainerPATCH v2 8/8 nfsd: add controls to set the minimum number of threads per pool
In reply to Jeff LaytonAdd a new "min_threads" variable to the nfsd_net, along with the corresponding nfsdfs and netlink interfaces to set that value from userland. Pass that value to svc_set_pool_threads() and svc_set_num_threads().
--- Documentation/netlink/specs/nfsd.yaml | 5 ++++ fs/nfsd/netlink.c | 5 ++-- fs/nfsd/netns.h | 6 +++++ fs/nfsd/nfsctl.c | 50 +++++++++++++++++++++++++++++++++++ fs/nfsd/nfssvc.c | 4 +-- fs/nfsd/trace.h | 19 +++++++++++++ include/uapi/linux/nfsd_netlink.h | 1 + 7 files changed, 86 insertions(+), 4 deletions(-)
Documentation/netlink/specs/nfsd.yaml +5 -0
@@ -78,6 +78,9 @@ attribute-sets: - name: scope type: string + - + name: min-threads + type: u32 - name: version attributes:
@@ -159,6 +162,7 @@ operations: - gracetime - leasetime - scope + - min-threads - name: threads-get doc: get the number of running threads
@@ -170,6 +174,7 @@ operations: - gracetime - leasetime - scope + - min-threads - name: version-set doc: set nfs enabled versions
fs/nfsd/netlink.c +3 -2
@@ -24,11 +24,12 @@ const struct nla_policy nfsd_version_nl_policy[NFSD_A_VERSION_ENABLED + 1] = { };/* NFSD_CMD_THREADS_SET - do */ -static const struct nla_policy nfsd_threads_set_nl_policy[NFSD_A_SERVER_SCOPE +1 ] = { +static const struct nla_policy nfsd_threads_set_nl_policy[NFSD_A_SERVER_MIN_THREADS +1 ] = { [NFSD_A_SERVER_THREADS ] = { .type =NLA_U32 , }, [NFSD_A_SERVER_GRACETIME ] = { .type =NLA_U32 , }, [NFSD_A_SERVER_LEASETIME ] = { .type =NLA_U32 , }, [NFSD_A_SERVER_SCOPE ] = { .type =NLA_NUL_STRING , }, + [NFSD_A_SERVER_MIN_THREADS ] = { .type =NLA_U32 , }, };/* NFSD_CMD_VERSION_SET - do */
@@ -57,7 +58,7 @@ static const struct genl_split_ops nfsd_nl_ops[] = { .cmd = NFSD_CMD_THREADS_SET, .doit = nfsd_nl_threads_set_doit, .policy = nfsd_threads_set_nl_policy, - .maxattr = NFSD_A_SERVER_SCOPE, + .maxattr = NFSD_A_SERVER_MIN_THREADS, .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO, }, {
fs/nfsd/netns.h +6 -0
@@ -130,6 +130,12 @@ struct nfsd_net {seqlock_t writeverf_lock;unsigned char writeverf[8 ]; +/* +* Minimum number of threads to run per pool. If 0 then the +* min == max requested number of threads. +*/ +unsigned int min_threads; +u32 clientid_base;u32 clientid_counter;u32 clverifier_counter;
fs/nfsd/nfsctl.c +50 -0
92 lines not on this page · raw message
fs/nfsd/nfssvc.c +2 -2
@@ -690,7 +690,7 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)/* Special case: When n == 1, distribute threads equally among pools. */ if (n ==1 ) -return svc_set_num_threads (nn->nfsd_serv,0 , nthreads[0 ]); +return svc_set_num_threads (nn->nfsd_serv, nn->min_threads, nthreads[0 ]);if (n > nn->nfsd_serv->sv_nrpools) n = nn->nfsd_serv->sv_nrpools;
@@ -718,7 +718,7 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)for (i =0 ; i < n; i++) { err =svc_set_pool_threads (nn->nfsd_serv, &nn->nfsd_serv->sv_pools[i], -0 , nthreads[i]); + nn->min_threads, nthreads[i]);if (err) goto out; }
fs/nfsd/trace.h +19 -0
@@ -2164,6 +2164,25 @@ TRACE_EVENT(nfsd_ctl_maxblksize, ) ); +TRACE_EVENT (nfsd_ctl_minthreads, +TP_PROTO ( + const struct net *net, + int minthreads + ), +TP_ARGS (net, minthreads), +TP_STRUCT__entry ( + __field(unsigned int, netns_ino) +__field (int, minthreads) + ), +TP_fast_assign ( + __entry->netns_ino = net->ns.inum; + __entry->minthreads = minthreads + ), +TP_printk ("minthreads=%d" , + __entry->minthreads + ) +); + TRACE_EVENT(nfsd_ctl_time, TP_PROTO( const struct net *net,
include/uapi/linux/nfsd_netlink.h +1 -0
@@ -35,6 +35,7 @@ enum {NFSD_A_SERVER_GRACETIME ,NFSD_A_SERVER_LEASETIME ,NFSD_A_SERVER_SCOPE , +NFSD_A_SERVER_MIN_THREADS , __NFSD_A_SERVER_MAX,NFSD_A_SERVER_MAX = (__NFSD_A_SERVER_MAX -1 )
Signature · 1 line
2.52.0
Chuck Lever
maintainerQuoted 64 lines · Jeff Layton
This version of the patchset fixes a number of warts in the first, and hopefully gets this closer to something mergeable.
This patchset allows nfsd to dynamically size its threadpool as needed. The main user-visible change is the addition of new controls that allow the admin to set a minimum number of threads.
When the minimum is set to a non-zero value, the traditional "threads" setting is interpreted as a maximum number of threads instead of a static count. The server will start the minimum number of threads, and then ramp up the thread count as needed. When the server is idle, it will gradually ramp down the thread count.
This control scheme should allow us to sanely switch between kernels that do and do not support dynamic threading. In the case where dynamic threading is not supported, the user will just get the static maximum number of threads, just like they do today.
So far this is only lightly tested, but it seems to work well. I still need to do some benchmarking to see whether this affects performance, so I'm posting this as an RFC for now.
Does this approach look sane to everyone?
Signed-off-by Jeff Layton <[email protected]>---
Changes in v2:
-svc_recv()now takes a timeout parameter. This should mean that non-dynamic RPC services are unaffected by these changes.
- ifmin_threadsis larger than the max, then clamp it to the max
- simplifySP_TASK_STARTINGusage. Have same task set and clear it.
- rework thread starting logic (EBUSY handling)
- reorder arguments tosvc_set_num_threads()andsvc_set_pool_threads()
- break up larger patches
- Link to v1:
https://lore.kernel.org/r/[email protected]---
Jeff Layton (8):
sunrpc: splitsvc_set_num_threads()into two functions
sunrpc: remove special handling of NULL pool from
svc_start/stop_kthreads()
sunrpc: track the max number of requested threads in a pool
sunrpc: introduce the concept of a minimum number of threads per
pool
sunrpc: split new thread creation into a separate function
sunrpc: allowsvc_recv()to return-ETIMEDOUTand-EBUSY
nfsd: adjust number of running nfsd threads based on activity
nfsd: add controls to set the minimum number of threads per poolDocumentation/netlink/specs/nfsd.yaml | 5 + fs/lockd/svc.c | 6 +- fs/nfs/callback.c | 10 +- fs/nfsd/netlink.c | 5 +- fs/nfsd/netns.h | 6 + fs/nfsd/nfsctl.c | 50 ++++++++ fs/nfsd/nfssvc.c | 63 +++++++--- fs/nfsd/trace.h | 54 +++++++++ include/linux/sunrpc/svc.h | 13 ++- include/linux/sunrpc/svcsock.h | 2 +- include/uapi/linux/nfsd_netlink.h | 1 + net/sunrpc/svc.c | 210 ++++++++++++++++++++-------------- net/sunrpc/svc_xprt.c | 44 +++++-- 13 files changed, 349 insertions(+), 120 deletions(-)
Quoted 7 lines
---
base-commit:83f633515af9382e7201e205112e18b995a80f70
change-id: 20251212-nfsd-dynathread-9f7a31172005Best regards,
Signature · 1 line
Jeff Layton <[email protected]>
I'm comfortable with this series. Let me know when you are ready for me to apply it.
Signature · 1 line
Chuck Lever
Jeff Layton
maintainerQuoted 70 lines · Chuck Lever
Quoted 68 lines · Jeff Layton
This version of the patchset fixes a number of warts in the first, and hopefully gets this closer to something mergeable.
This patchset allows nfsd to dynamically size its threadpool as needed. The main user-visible change is the addition of new controls that allow the admin to set a minimum number of threads.
When the minimum is set to a non-zero value, the traditional "threads" setting is interpreted as a maximum number of threads instead of a static count. The server will start the minimum number of threads, and then ramp up the thread count as needed. When the server is idle, it will gradually ramp down the thread count.
This control scheme should allow us to sanely switch between kernels that do and do not support dynamic threading. In the case where dynamic threading is not supported, the user will just get the static maximum number of threads, just like they do today.
So far this is only lightly tested, but it seems to work well. I still need to do some benchmarking to see whether this affects performance, so I'm posting this as an RFC for now.
Does this approach look sane to everyone?
Signed-off-by Jeff Layton <[email protected]>---
Changes in v2:
-svc_recv()now takes a timeout parameter. This should mean that non-dynamic RPC services are unaffected by these changes.
- ifmin_threadsis larger than the max, then clamp it to the max
- simplifySP_TASK_STARTINGusage. Have same task set and clear it.
- rework thread starting logic (EBUSY handling)
- reorder arguments tosvc_set_num_threads()andsvc_set_pool_threads()
- break up larger patches
- Link to v1:
https://lore.kernel.org/r/[email protected]---
Jeff Layton (8):
sunrpc: splitsvc_set_num_threads()into two functions
sunrpc: remove special handling of NULL pool from
svc_start/stop_kthreads()
sunrpc: track the max number of requested threads in a pool
sunrpc: introduce the concept of a minimum number of threads per
pool
sunrpc: split new thread creation into a separate function
sunrpc: allowsvc_recv()to return-ETIMEDOUTand-EBUSY
nfsd: adjust number of running nfsd threads based on activity
nfsd: add controls to set the minimum number of threads per poolDocumentation/netlink/specs/nfsd.yaml | 5 + fs/lockd/svc.c | 6 +- fs/nfs/callback.c | 10 +- fs/nfsd/netlink.c | 5 +- fs/nfsd/netns.h | 6 + fs/nfsd/nfsctl.c | 50 ++++++++ fs/nfsd/nfssvc.c | 63 +++++++--- fs/nfsd/trace.h | 54 +++++++++ include/linux/sunrpc/svc.h | 13 ++- include/linux/sunrpc/svcsock.h | 2 +- include/uapi/linux/nfsd_netlink.h | 1 + net/sunrpc/svc.c | 210 ++++++++++++++++++++-------------- net/sunrpc/svc_xprt.c | 44 +++++-- 13 files changed, 349 insertions(+), 120 deletions(-)---
base-commit:83f633515af9382e7201e205112e18b995a80f70
change-id: 20251212-nfsd-dynathread-9f7a31172005
Quoted 7 lines
Quoted 3 lines
Best regards,
Signature · 1 line
Jeff Layton <[email protected]>I'm comfortable with this series. Let me know when you are ready for me to apply it.
I think it'd be great to put this in nfsd-testing soon so we can start playing with it. I'll plan to post the nfs-utils patches for this in the near future too (they need a bit more work).
Thanks,
Signature · 1 line
Jeff Layton <[email protected]>
Chuck Lever
maintainerFrom: Chuck Lever <[email protected]>
Quoted 8 lines · Jeff Layton
This version of the patchset fixes a number of warts in the first, and hopefully gets this closer to something mergeable.
This patchset allows nfsd to dynamically size its threadpool as needed. The main user-visible change is the addition of new controls that allow the admin to set a minimum number of threads.
[...]
Applied to nfsd-testing, thanks!
[1/8] sunrpc: split svc_set_num_threads() into two functions
commit: 039d0b7837eaad0d904631df3ec26b4961ee740a
[2/8] sunrpc: remove special handling of NULL pool from svc_start/stop_kthreads()
commit: 040f69a4bff8ba6236d69bca06c62adb7588268b
[3/8] sunrpc: track the max number of requested threads in a pool
commit: 2672a852bfc6d7597a8b202cf5ee813a4a04b1b8
[4/8] sunrpc: introduce the concept of a minimum number of threads per pool
commit: 80099415fa314dec028bbb3e904ed57ddea55d1c
[5/8] sunrpc: split new thread creation into a separate function
commit: ad23853b5e7bc156652fd957425d6891dd864ea1
[6/8] sunrpc: allow svc_recv() to return -ETIMEDOUT and -EBUSY
commit: a37178a2642f98fd6cbed39fa0c508dce3bd7bf8
[7/8] nfsd: adjust number of running nfsd threads based on activity
commit: f375bafd744fa40cdc48d252e5f5db1242100881
[8/8] nfsd: add controls to set the minimum number of threads per pool
commit: fe054be1747e1754bc81ea3dabbf85dfab2bed27--
Chuck Lever