Skip to content

Commit 7ffc7ad

Browse files
jtlaytonchucklever
authored andcommitted
sunrpc: introduce the concept of a minimum number of threads per pool
Add 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. Signed-off-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
1 parent 6cd60f4 commit 7ffc7ad

5 files changed

Lines changed: 52 additions & 21 deletions

File tree

fs/lockd/svc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ static int lockd_get(void)
340340
return -ENOMEM;
341341
}
342342

343-
error = svc_set_num_threads(serv, 1);
343+
error = svc_set_num_threads(serv, 0, 1);
344344
if (error < 0) {
345345
svc_destroy(&serv);
346346
return error;
@@ -368,7 +368,7 @@ static void lockd_put(void)
368368
unregister_inet6addr_notifier(&lockd_inet6addr_notifier);
369369
#endif
370370

371-
svc_set_num_threads(nlmsvc_serv, 0);
371+
svc_set_num_threads(nlmsvc_serv, 0, 0);
372372
timer_delete_sync(&nlmsvc_retry);
373373
svc_destroy(&nlmsvc_serv);
374374
dprintk("lockd_down: service destroyed\n");

fs/nfs/callback.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ static int nfs_callback_start_svc(int minorversion, struct rpc_xprt *xprt,
119119
if (serv->sv_nrthreads == nrservs)
120120
return 0;
121121

122-
ret = svc_set_num_threads(serv, nrservs);
122+
ret = svc_set_num_threads(serv, 0, nrservs);
123123
if (ret) {
124-
svc_set_num_threads(serv, 0);
124+
svc_set_num_threads(serv, 0, 0);
125125
return ret;
126126
}
127127
dprintk("nfs_callback_up: service started\n");
@@ -242,7 +242,7 @@ int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt)
242242
cb_info->users++;
243243
err_net:
244244
if (!cb_info->users) {
245-
svc_set_num_threads(cb_info->serv, 0);
245+
svc_set_num_threads(cb_info->serv, 0, 0);
246246
svc_destroy(&cb_info->serv);
247247
}
248248
err_create:
@@ -268,7 +268,7 @@ void nfs_callback_down(int minorversion, struct net *net, struct rpc_xprt *xprt)
268268
nfs_callback_down_net(minorversion, serv, net);
269269
cb_info->users--;
270270
if (cb_info->users == 0) {
271-
svc_set_num_threads(serv, 0);
271+
svc_set_num_threads(serv, 0, 0);
272272
dprintk("nfs_callback_down: service destroyed\n");
273273
xprt_svc_destroy_nullify_bc(xprt, &cb_info->serv);
274274
}

fs/nfsd/nfssvc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ void nfsd_shutdown_threads(struct net *net)
580580
}
581581

582582
/* Kill outstanding nfsd threads */
583-
svc_set_num_threads(serv, 0);
583+
svc_set_num_threads(serv, 0, 0);
584584
nfsd_destroy_serv(net);
585585
mutex_unlock(&nfsd_mutex);
586586
}
@@ -690,7 +690,7 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
690690

691691
/* Special case: When n == 1, distribute threads equally among pools. */
692692
if (n == 1)
693-
return svc_set_num_threads(nn->nfsd_serv, nthreads[0]);
693+
return svc_set_num_threads(nn->nfsd_serv, 0, nthreads[0]);
694694

695695
if (n > nn->nfsd_serv->sv_nrpools)
696696
n = nn->nfsd_serv->sv_nrpools;
@@ -718,7 +718,7 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
718718
for (i = 0; i < n; i++) {
719719
err = svc_set_pool_threads(nn->nfsd_serv,
720720
&nn->nfsd_serv->sv_pools[i],
721-
nthreads[i]);
721+
0, nthreads[i]);
722722
if (err)
723723
goto out;
724724
}
@@ -727,7 +727,7 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
727727
for (i = n; i < nn->nfsd_serv->sv_nrpools; ++i) {
728728
err = svc_set_pool_threads(nn->nfsd_serv,
729729
&nn->nfsd_serv->sv_pools[i],
730-
0);
730+
0, 0);
731731
if (err)
732732
goto out;
733733
}

include/linux/sunrpc/svc.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
struct svc_pool {
3737
unsigned int sp_id; /* pool id; also node id on NUMA */
3838
unsigned int sp_nrthreads; /* # of threads currently running in pool */
39+
unsigned int sp_nrthrmin; /* Min number of threads to run per pool */
3940
unsigned int sp_nrthrmax; /* Max requested number of threads in pool */
4041
struct lwq sp_xprts; /* pending transports */
4142
struct list_head sp_all_threads; /* all server threads */
@@ -72,7 +73,7 @@ struct svc_serv {
7273
struct svc_stat * sv_stats; /* RPC statistics */
7374
spinlock_t sv_lock;
7475
unsigned int sv_nprogs; /* Number of sv_programs */
75-
unsigned int sv_nrthreads; /* # of server threads */
76+
unsigned int sv_nrthreads; /* # of running server threads */
7677
unsigned int sv_max_payload; /* datagram payload size */
7778
unsigned int sv_max_mesg; /* max_payload + 1 page for overheads */
7879
unsigned int sv_xdrsize; /* XDR buffer size */
@@ -448,8 +449,9 @@ struct svc_serv * svc_create_pooled(struct svc_program *prog,
448449
unsigned int bufsize,
449450
int (*threadfn)(void *data));
450451
int svc_set_pool_threads(struct svc_serv *serv, struct svc_pool *pool,
451-
unsigned int nrservs);
452-
int svc_set_num_threads(struct svc_serv *serv, unsigned int nrservs);
452+
unsigned int min_threads, unsigned int max_threads);
453+
int svc_set_num_threads(struct svc_serv *serv, unsigned int min_threads,
454+
unsigned int nrservs);
453455
int svc_pool_stats_open(struct svc_info *si, struct file *file);
454456
void svc_process(struct svc_rqst *rqstp);
455457
void svc_process_bc(struct rpc_rqst *req, struct svc_rqst *rqstp);

net/sunrpc/svc.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,14 @@ svc_stop_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
820820
* svc_set_pool_threads - adjust number of threads per pool
821821
* @serv: RPC service to adjust
822822
* @pool: Specific pool from which to choose threads
823-
* @nrservs: New number of threads for @serv (0 means kill all threads)
823+
* @min_threads: min number of threads to run in @pool
824+
* @max_threads: max number of threads in @pool (0 means kill all threads)
825+
*
826+
* Create or destroy threads in @pool to bring it into an acceptable range
827+
* between @min_threads and @max_threads.
824828
*
825-
* Create or destroy threads in @pool to bring it to @nrservs.
829+
* If @min_threads is 0 or larger than @max_threads, then it is ignored and
830+
* the pool will be set to run a static @max_threads number of threads.
826831
*
827832
* Caller must ensure mutual exclusion between this and server startup or
828833
* shutdown.
@@ -832,16 +837,36 @@ svc_stop_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
832837
*/
833838
int
834839
svc_set_pool_threads(struct svc_serv *serv, struct svc_pool *pool,
835-
unsigned int nrservs)
840+
unsigned int min_threads, unsigned int max_threads)
836841
{
837-
int delta = nrservs;
842+
int delta;
838843

839844
if (!pool)
840845
return -EINVAL;
841846

842-
pool->sp_nrthrmax = nrservs;
843-
delta -= pool->sp_nrthreads;
847+
/* clamp min threads to the max */
848+
if (min_threads > max_threads)
849+
min_threads = max_threads;
850+
851+
pool->sp_nrthrmin = min_threads;
852+
pool->sp_nrthrmax = max_threads;
853+
854+
/*
855+
* When min_threads is set, then only change the number of
856+
* threads to bring it within an acceptable range.
857+
*/
858+
if (min_threads) {
859+
if (pool->sp_nrthreads > max_threads)
860+
delta = max_threads;
861+
else if (pool->sp_nrthreads < min_threads)
862+
delta = min_threads;
863+
else
864+
return 0;
865+
} else {
866+
delta = max_threads;
867+
}
844868

869+
delta -= pool->sp_nrthreads;
845870
if (delta > 0)
846871
return svc_start_kthreads(serv, pool, delta);
847872
if (delta < 0)
@@ -853,6 +878,7 @@ EXPORT_SYMBOL_GPL(svc_set_pool_threads);
853878
/**
854879
* svc_set_num_threads - adjust number of threads in serv
855880
* @serv: RPC service to adjust
881+
* @min_threads: min number of threads to run per pool
856882
* @nrservs: New number of threads for @serv (0 means kill all threads)
857883
*
858884
* Create or destroy threads in @serv to bring it to @nrservs. If there
@@ -867,20 +893,23 @@ EXPORT_SYMBOL_GPL(svc_set_pool_threads);
867893
* adjusted; the caller is responsible for recovery.
868894
*/
869895
int
870-
svc_set_num_threads(struct svc_serv *serv, unsigned int nrservs)
896+
svc_set_num_threads(struct svc_serv *serv, unsigned int min_threads,
897+
unsigned int nrservs)
871898
{
872899
unsigned int base = nrservs / serv->sv_nrpools;
873900
unsigned int remain = nrservs % serv->sv_nrpools;
874901
int i, err = 0;
875902

876903
for (i = 0; i < serv->sv_nrpools; ++i) {
904+
struct svc_pool *pool = &serv->sv_pools[i];
877905
int threads = base;
878906

879907
if (remain) {
880908
++threads;
881909
--remain;
882910
}
883-
err = svc_set_pool_threads(serv, &serv->sv_pools[i], threads);
911+
912+
err = svc_set_pool_threads(serv, pool, min_threads, threads);
884913
if (err)
885914
break;
886915
}

0 commit comments

Comments
 (0)