Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Jeff Layton and committed by
Chuck Lever
1c87a0c3 a0022a38

+77 -1
+42 -1
fs/nfsd/nfssvc.c
··· 882 882 nfsd(void *vrqstp) 883 883 { 884 884 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp; 885 + struct svc_pool *pool = rqstp->rq_pool; 885 886 struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list); 886 887 struct net *net = perm_sock->xpt_net; 887 888 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 889 + bool have_mutex = false; 888 890 889 891 /* At this point, the thread shares current->fs 890 892 * with the init process. We need to create files with the ··· 904 902 * The main request loop 905 903 */ 906 904 while (!svc_thread_should_stop(rqstp)) { 907 - svc_recv(rqstp, 0); 905 + switch (svc_recv(rqstp, 5 * HZ)) { 906 + case -ETIMEDOUT: 907 + /* No work arrived within the timeout window */ 908 + if (mutex_trylock(&nfsd_mutex)) { 909 + if (pool->sp_nrthreads > pool->sp_nrthrmin) { 910 + trace_nfsd_dynthread_kill(net, pool); 911 + set_bit(RQ_VICTIM, &rqstp->rq_flags); 912 + have_mutex = true; 913 + } else { 914 + mutex_unlock(&nfsd_mutex); 915 + } 916 + } else { 917 + trace_nfsd_dynthread_trylock_fail(net, pool); 918 + } 919 + break; 920 + case -EBUSY: 921 + /* No idle threads; consider spawning another */ 922 + if (pool->sp_nrthreads < pool->sp_nrthrmax) { 923 + if (mutex_trylock(&nfsd_mutex)) { 924 + if (pool->sp_nrthreads < pool->sp_nrthrmax) { 925 + int ret; 926 + 927 + trace_nfsd_dynthread_start(net, pool); 928 + ret = svc_new_thread(rqstp->rq_server, pool); 929 + if (ret) 930 + pr_notice_ratelimited("%s: unable to spawn new thread: %d\n", 931 + __func__, ret); 932 + } 933 + mutex_unlock(&nfsd_mutex); 934 + } else { 935 + trace_nfsd_dynthread_trylock_fail(net, pool); 936 + } 937 + } 938 + clear_bit(SP_TASK_STARTING, &pool->sp_flags); 939 + break; 940 + default: 941 + break; 942 + } 908 943 nfsd_file_net_dispose(nn); 909 944 } 910 945 ··· 949 910 950 911 /* Release the thread */ 951 912 svc_exit_thread(rqstp); 913 + if (have_mutex) 914 + mutex_unlock(&nfsd_mutex); 952 915 return 0; 953 916 } 954 917
+35
fs/nfsd/trace.h
··· 91 91 DEFINE_NFSD_XDR_ERR_EVENT(garbage_args); 92 92 DEFINE_NFSD_XDR_ERR_EVENT(cant_encode); 93 93 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 + 94 129 #define show_nfsd_may_flags(x) \ 95 130 __print_flags(x, "|", \ 96 131 { NFSD_MAY_EXEC, "EXEC" }, \