Skip to content

Commit 1c87a0c

Browse files
jtlaytonchucklever
authored andcommitted
nfsd: adjust number of running nfsd threads based on activity
nfsd() 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." Signed-off-by: NeilBrown <neil@brown.name> Signed-off-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
1 parent a0022a3 commit 1c87a0c

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

fs/nfsd/nfssvc.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,9 +882,11 @@ static int
882882
nfsd(void *vrqstp)
883883
{
884884
struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
885+
struct svc_pool *pool = rqstp->rq_pool;
885886
struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
886887
struct net *net = perm_sock->xpt_net;
887888
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
889+
bool have_mutex = false;
888890

889891
/* At this point, the thread shares current->fs
890892
* with the init process. We need to create files with the
@@ -902,14 +904,53 @@ nfsd(void *vrqstp)
902904
* The main request loop
903905
*/
904906
while (!svc_thread_should_stop(rqstp)) {
905-
svc_recv(rqstp, 0);
907+
switch (svc_recv(rqstp, 5 * HZ)) {
908+
case -ETIMEDOUT:
909+
/* No work arrived within the timeout window */
910+
if (mutex_trylock(&nfsd_mutex)) {
911+
if (pool->sp_nrthreads > pool->sp_nrthrmin) {
912+
trace_nfsd_dynthread_kill(net, pool);
913+
set_bit(RQ_VICTIM, &rqstp->rq_flags);
914+
have_mutex = true;
915+
} else {
916+
mutex_unlock(&nfsd_mutex);
917+
}
918+
} else {
919+
trace_nfsd_dynthread_trylock_fail(net, pool);
920+
}
921+
break;
922+
case -EBUSY:
923+
/* No idle threads; consider spawning another */
924+
if (pool->sp_nrthreads < pool->sp_nrthrmax) {
925+
if (mutex_trylock(&nfsd_mutex)) {
926+
if (pool->sp_nrthreads < pool->sp_nrthrmax) {
927+
int ret;
928+
929+
trace_nfsd_dynthread_start(net, pool);
930+
ret = svc_new_thread(rqstp->rq_server, pool);
931+
if (ret)
932+
pr_notice_ratelimited("%s: unable to spawn new thread: %d\n",
933+
__func__, ret);
934+
}
935+
mutex_unlock(&nfsd_mutex);
936+
} else {
937+
trace_nfsd_dynthread_trylock_fail(net, pool);
938+
}
939+
}
940+
clear_bit(SP_TASK_STARTING, &pool->sp_flags);
941+
break;
942+
default:
943+
break;
944+
}
906945
nfsd_file_net_dispose(nn);
907946
}
908947

909948
atomic_dec(&nfsd_th_cnt);
910949

911950
/* Release the thread */
912951
svc_exit_thread(rqstp);
952+
if (have_mutex)
953+
mutex_unlock(&nfsd_mutex);
913954
return 0;
914955
}
915956

fs/nfsd/trace.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,41 @@ DEFINE_EVENT(nfsd_xdr_err_class, nfsd_##name##_err, \
9191
DEFINE_NFSD_XDR_ERR_EVENT(garbage_args);
9292
DEFINE_NFSD_XDR_ERR_EVENT(cant_encode);
9393

94+
DECLARE_EVENT_CLASS(nfsd_dynthread_class,
95+
TP_PROTO(
96+
const struct net *net,
97+
const struct svc_pool *pool
98+
),
99+
TP_ARGS(net, pool),
100+
TP_STRUCT__entry(
101+
__field(unsigned int, netns_ino)
102+
__field(unsigned int, pool_id)
103+
__field(unsigned int, nrthreads)
104+
__field(unsigned int, nrthrmin)
105+
__field(unsigned int, nrthrmax)
106+
),
107+
TP_fast_assign(
108+
__entry->netns_ino = net->ns.inum;
109+
__entry->pool_id = pool->sp_id;
110+
__entry->nrthreads = pool->sp_nrthreads;
111+
__entry->nrthrmin = pool->sp_nrthrmin;
112+
__entry->nrthrmax = pool->sp_nrthrmax;
113+
),
114+
TP_printk("pool=%u nrthreads=%u nrthrmin=%u nrthrmax=%u",
115+
__entry->pool_id, __entry->nrthreads,
116+
__entry->nrthrmin, __entry->nrthrmax
117+
)
118+
);
119+
120+
#define DEFINE_NFSD_DYNTHREAD_EVENT(name) \
121+
DEFINE_EVENT(nfsd_dynthread_class, nfsd_dynthread_##name, \
122+
TP_PROTO(const struct net *net, const struct svc_pool *pool), \
123+
TP_ARGS(net, pool))
124+
125+
DEFINE_NFSD_DYNTHREAD_EVENT(start);
126+
DEFINE_NFSD_DYNTHREAD_EVENT(kill);
127+
DEFINE_NFSD_DYNTHREAD_EVENT(trylock_fail);
128+
94129
#define show_nfsd_may_flags(x) \
95130
__print_flags(x, "|", \
96131
{ NFSD_MAY_EXEC, "EXEC" }, \

0 commit comments

Comments
 (0)