Skip to content

Commit 2e8fc92

Browse files
neilbrownchucklever
authored andcommitted
SUNRPC: change sp_nrthreads to atomic_t
Using an atomic_t avoids the need to take a spinlock (which can soon be removed). Choosing a thread to kill needs to be careful as we cannot set the "die now" bit atomically with the test on the count. Instead we temporarily increase the count. Signed-off-by: NeilBrown <neilb@suse.de> Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
1 parent 9a0e6ac commit 2e8fc92

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

fs/nfsd/nfssvc.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -713,14 +713,13 @@ int nfsd_nrpools(struct net *net)
713713

714714
int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
715715
{
716-
int i = 0;
717716
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
717+
struct svc_serv *serv = nn->nfsd_serv;
718+
int i;
718719

719-
if (nn->nfsd_serv != NULL) {
720-
for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
721-
nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
722-
}
723-
720+
if (serv)
721+
for (i = 0; i < serv->sv_nrpools && i < n; i++)
722+
nthreads[i] = atomic_read(&serv->sv_pools[i].sp_nrthreads);
724723
return 0;
725724
}
726725

include/linux/sunrpc/svc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct svc_pool {
3636
unsigned int sp_id; /* pool id; also node id on NUMA */
3737
spinlock_t sp_lock; /* protects all fields */
3838
struct lwq sp_xprts; /* pending transports */
39-
unsigned int sp_nrthreads; /* # of threads in pool */
39+
atomic_t sp_nrthreads; /* # of threads in pool */
4040
struct list_head sp_all_threads; /* all server threads */
4141
struct llist_head sp_idle_threads; /* idle server threads */
4242

net/sunrpc/svc.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,8 @@ svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)
681681
serv->sv_nrthreads += 1;
682682
spin_unlock_bh(&serv->sv_lock);
683683

684+
atomic_inc(&pool->sp_nrthreads);
684685
spin_lock_bh(&pool->sp_lock);
685-
pool->sp_nrthreads++;
686686
list_add_rcu(&rqstp->rq_all, &pool->sp_all_threads);
687687
spin_unlock_bh(&pool->sp_lock);
688688
return rqstp;
@@ -727,32 +727,37 @@ svc_pool_next(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
727727
}
728728

729729
static struct svc_pool *
730-
svc_pool_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
730+
svc_pool_victim(struct svc_serv *serv, struct svc_pool *target_pool,
731+
unsigned int *state)
731732
{
733+
struct svc_pool *pool;
732734
unsigned int i;
733735

736+
retry:
737+
pool = target_pool;
738+
734739
if (pool != NULL) {
735-
spin_lock_bh(&pool->sp_lock);
736-
if (pool->sp_nrthreads)
740+
if (atomic_inc_not_zero(&pool->sp_nrthreads))
737741
goto found_pool;
738-
spin_unlock_bh(&pool->sp_lock);
739742
return NULL;
740743
} else {
741744
for (i = 0; i < serv->sv_nrpools; i++) {
742745
pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
743-
spin_lock_bh(&pool->sp_lock);
744-
if (pool->sp_nrthreads)
746+
if (atomic_inc_not_zero(&pool->sp_nrthreads))
745747
goto found_pool;
746-
spin_unlock_bh(&pool->sp_lock);
747748
}
748749
return NULL;
749750
}
750751

751752
found_pool:
752753
set_bit(SP_VICTIM_REMAINS, &pool->sp_flags);
753754
set_bit(SP_NEED_VICTIM, &pool->sp_flags);
754-
spin_unlock_bh(&pool->sp_lock);
755-
return pool;
755+
if (!atomic_dec_and_test(&pool->sp_nrthreads))
756+
return pool;
757+
/* Nothing left in this pool any more */
758+
clear_bit(SP_NEED_VICTIM, &pool->sp_flags);
759+
clear_bit(SP_VICTIM_REMAINS, &pool->sp_flags);
760+
goto retry;
756761
}
757762

758763
static int
@@ -828,13 +833,10 @@ svc_stop_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
828833
int
829834
svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
830835
{
831-
if (pool == NULL) {
836+
if (!pool)
832837
nrservs -= serv->sv_nrthreads;
833-
} else {
834-
spin_lock_bh(&pool->sp_lock);
835-
nrservs -= pool->sp_nrthreads;
836-
spin_unlock_bh(&pool->sp_lock);
837-
}
838+
else
839+
nrservs -= atomic_read(&pool->sp_nrthreads);
838840

839841
if (nrservs > 0)
840842
return svc_start_kthreads(serv, pool, nrservs);
@@ -921,10 +923,11 @@ svc_exit_thread(struct svc_rqst *rqstp)
921923
struct svc_pool *pool = rqstp->rq_pool;
922924

923925
spin_lock_bh(&pool->sp_lock);
924-
pool->sp_nrthreads--;
925926
list_del_rcu(&rqstp->rq_all);
926927
spin_unlock_bh(&pool->sp_lock);
927928

929+
atomic_dec(&pool->sp_nrthreads);
930+
928931
spin_lock_bh(&serv->sv_lock);
929932
serv->sv_nrthreads -= 1;
930933
spin_unlock_bh(&serv->sv_lock);

0 commit comments

Comments
 (0)