improve some nfsd_mutex locking
Subsystems (4)
NeilBrown
reviewercover improve some nfsd_mutex locking
The first patch hopefully fixes a bug with locking as reported by Li
Lingfeng: some write_foo functions aren't locked properly.
The other two improve the locking code, particulary so that we don't need a global mutex to change per-netns data.
I've revised the locking to use guard(mutex) for (almost) all places that the per-netfs mutex is used. I think this is an improvement but would like to know what others think.
I haven't changed _get/_put to _pin/_unpin as Chuck wondered about. I'm not against that (though get/put are widely understood) but nor am I particularly for it yet. Again, opinions are welcome.
NeilBrown
[PATCH 1/3] nfsd: provide proper locking for all write_ function [PATCH 2/3] nfsd: use kref and new mutex for global config management [PATCH 3/3] nfsd: split nfsd_mutex into one mutex per net-namespace.
NeilBrown
reviewerPATCH 1/3 nfsd: provide proper locking for all write_ function
write_foo functions are called to handle IO to files in /proc/fs/nfsd/. They can be called at any time and so generally need locking to ensure they don't happen at an awkward time.
Many already take nfsd_mutex and check if nfsd_serv has been set. This ensures they only run when the server is fully configured.
write_filehandle() does *not* need locking. It interacts with the export table which is set up when the netns is set up, so it is always valid and it has its own locking. write_filehandle() is needed before the nfs server is started so checking nfsd_serv would be wrong.
The remaining files which do not have any locking are write_v4_end_grace(), write_unlock_ip(), and write_unlock_fs(). None of these make sense when the nfs server is not running and there is evidence that write_v4_end_grace() can race with ->client_tracking_op setup/shutdown and cause problems.
This patch adds locking to these three and ensures the "unlock" functions abort if ->nfsd_serv is not set. It uses
guard(mutex)(&nfsd_mutex);
so there is no need to ensure we unlock on every patch.
--- fs/nfsd/nfsctl.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
fs/nfsd/nfsctl.c +14 -0
@@ -221,6 +221,12 @@ static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)size_t salen =sizeof (address);char *fo_path;struct net *net =netns (file); +struct nfsd_net *nn =net_generic (net, nfsd_net_id); + +guard (mutex)(&nfsd_mutex); +if (!nn->nfsd_serv) +/* There cannot be any files to unlock */ +return -EINVAL ;/* sanity check */ if (size ==0 )
@@ -259,6 +265,12 @@ static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)struct path path;char *fo_path;int error; +struct nfsd_net *nn =net_generic (netns (file), nfsd_net_id); + +guard (mutex)(&nfsd_mutex); +if (!nn->nfsd_serv) +/* There cannot be any files to unlock */ +return -EINVAL ;/* sanity check */ if (size ==0 )
@@ -1053,6 +1065,7 @@ static ssize_t write_recoverydir(struct file *file, char *buf, size_t size) }#endif +/* * write_v4_end_grace - release grace period for nfsd's v4.x lock manager *
@@ -1077,6 +1090,7 @@ static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size) {struct nfsd_net *nn =net_generic (netns (file), nfsd_net_id); +guard (mutex)(&nfsd_mutex);if (size >0 ) {switch (buf[0 ]) {case 'Y' :
Signature · 1 line
2.49.0
Re: [PATCH 1/3] nfsd: provide proper locking for all write_ function
Thank you for the patch, it did fix the issue.
Quoted 68 lines · NeilBrown
write_foofunctions are called to handle IO to files in/proc/fs/nfsd/. They can be called at any time and so generally need locking to ensure they don't happen at an awkward time.Many already take
nfsd_mutexand check ifnfsd_servhas been set. This ensures they only run when the server is fully configured.
write_filehandle()does *not* need locking. It interacts with the export table which is set up when the netns is set up, so it is always valid and it has its own locking.write_filehandle()is needed before the nfs server is started so checkingnfsd_servwould be wrong.The remaining files which do not have any locking are
write_v4_end_grace(),write_unlock_ip(), andwrite_unlock_fs(). None of these make sense when the nfs server is not running and there is evidence thatwrite_v4_end_grace()can race with ->client_tracking_op setup/shutdown and cause problems.This patch adds locking to these three and ensures the "unlock" functions abort if ->nfsd_serv is not set. It uses
guard(mutex)(&nfsd_mutex);so there is no need to ensure we unlock on every patch.
Reported-by Li Lingfeng <[email protected]>Signed-off-by NeilBrown <[email protected]>--- fs/nfsd/nfsctl.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)fs/nfsd/nfsctl.c +13
-0@@ -221,6 +221,12 @@ static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)size_t salen =sizeof (address);char *fo_path;struct net *net =netns (file); +struct nfsd_net *nn =net_generic (net, nfsd_net_id); + +guard (mutex)(&nfsd_mutex); +if (!nn->nfsd_serv) +/* There cannot be any files to unlock */ +return -EINVAL ;/* sanity check */ if (size ==0 )@@ -259,6 +265,12 @@ static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)struct path path;char *fo_path;int error; +struct nfsd_net *nn =net_generic (netns (file), nfsd_net_id); + +guard (mutex)(&nfsd_mutex); +if (!nn->nfsd_serv) +/* There cannot be any files to unlock */ +return -EINVAL ;/* sanity check */ if (size ==0 )@@ -1053,6 +1065,7 @@ static ssize_t write_recoverydir(struct file *file, char *buf, size_t size) }#endif +/* * write_v4_end_grace - release grace period for nfsd's v4.x lock manager *
fs/nfsd/nfsctl.c static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size) · line 1096
Quoted 7 lines
fs/nfsd/nfsctl.c +1
-0@@ -1077,6 +1090,7 @@ static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size) {struct nfsd_net *nn =net_generic (netns (file), nfsd_net_id); +guard (mutex)(&nfsd_mutex);if (size >0 ) {switch (buf[0 ]) {case 'Y' :
NeilBrown
reviewerPATCH 2/3 nfsd: use kref and new mutex for global config management
In reply to NeilBrownnfsd_mutex is used for two quite different things:
1/ it prevents races when start/stoping global resources:
the filecache and the v4 state table
2/ it prevents races for per-netns config, typically
ensure config changes are synchronised w.r.t. server startup/shutdown.
This patch splits out the first used. A subsequent patch improves the second.
"nfsd_users" is changed to a kref which is can be taken to delay the shutdown of global services. nfsd_startup_get(), it is succeeds, ensure the global services will remain until nfsd_startup_put().
The new mutex, nfsd_startup_mutex, is only take for startup and shutdown. It is not needed to protect the kref.
The locking needed by nfsd_file_cache_purge() is now provided internally by that function so calls don't need to be concerned.
This replaces NFSD_FILE_CACHE_UP which is effective just a flag which says nfsd_users is non-zero.
--- fs/nfsd/export.c | 6 ------ fs/nfsd/filecache.c | 31 +++++++++++-------------------- fs/nfsd/nfsd.h | 3 +++ fs/nfsd/nfssvc.c | 41 +++++++++++++++++++++++++++-------------- 4 files changed, 41 insertions(+), 40 deletions(-)
fs/nfsd/export.c +0 -6
@@ -243,13 +243,7 @@ static struct cache_head *expkey_alloc(void)static void expkey_flush (void ) { -/* -* Take the nfsd_mutex here to ensure that the file cache is not -* destroyed while we're in the middle of flushing. -*/ -mutex_lock (&nfsd_mutex);nfsd_file_cache_purge (current->nsproxy->net_ns); -mutex_unlock (&nfsd_mutex); }static const struct cache_detail svc_expkey_cache_template = {
fs/nfsd/filecache.c +11 -20
@@ -50,8 +50,6 @@#define NFSD_LAUNDRETTE_DELAY (2 * HZ) -#define NFSD_FILE_CACHE_UP (0) -/* We only care about NFSD_MAY_READ/WRITE for this cache */ #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE|NFSD_MAY_LOCALIO)
@@ -70,7 +68,6 @@ struct nfsd_fcache_disposal {static struct kmem_cache *nfsd_file_slab;static struct kmem_cache *nfsd_file_mark_slab;static struct list_lru nfsd_file_lru; -static unsigned long nfsd_file_flags;static struct fsnotify_group *nfsd_file_fsnotify_group;static struct delayed_work nfsd_filecache_laundrette;static struct rhltable nfsd_file_rhltable
@@ -112,9 +109,12 @@ static const struct rhashtable_params nfsd_file_rhash_params = {static void nfsd_file_schedule_laundrette (void ) { -if (test_bit (NFSD_FILE_CACHE_UP , &nfsd_file_flags)) -queue_delayed_work (system_unbound_wq, &nfsd_filecache_laundrette, +if (nfsd_startup_get ()) { +queue_delayed_work (system_unbound_wq, + &nfsd_filecache_laundrette,NFSD_LAUNDRETTE_DELAY ); +nfsd_startup_put (); + } } static void
@@ -795,10 +795,6 @@ nfsd_file_cache_init(void) {int ret; -lockdep_assert_held (&nfsd_mutex); -if (test_and_set_bit (NFSD_FILE_CACHE_UP , &nfsd_file_flags) ==1 ) -return 0 ; - ret =rhltable_init (&nfsd_file_rhltable, &nfsd_file_rhash_params);if (ret) goto out;
@@ -853,8 +849,6 @@ nfsd_file_cache_init(void)INIT_DELAYED_WORK (&nfsd_filecache_laundrette, nfsd_file_gc_worker); out: -if (ret) -clear_bit (NFSD_FILE_CACHE_UP , &nfsd_file_flags);return ret; out_notifier:lease_unregister_notifier (&nfsd_file_lease_notifier);
@@ -958,9 +952,10 @@ nfsd_file_cache_start_net(struct net *net)void nfsd_file_cache_purge (struct net *net) { -lockdep_assert_held (&nfsd_mutex); -if (test_bit (NFSD_FILE_CACHE_UP , &nfsd_file_flags) ==1 ) +if (nfsd_startup_get ()) {__nfsd_file_cache_purge (net); +nfsd_startup_put (); + } }void
@@ -975,10 +970,6 @@ nfsd_file_cache_shutdown(void) {int i; -lockdep_assert_held (&nfsd_mutex); -if (test_and_clear_bit (NFSD_FILE_CACHE_UP , &nfsd_file_flags) ==0 ) -return ; -lease_unregister_notifier (&nfsd_file_lease_notifier);shrinker_free (nfsd_file_shrinker);/*
@@ -1347,8 +1338,7 @@ int nfsd_file_cache_stats_show(struct seq_file *m, void *v)unsigned long lru =0 , total_age =0 ;/* Serialize with server shutdown */ -mutex_lock (&nfsd_mutex); -if (test_bit (NFSD_FILE_CACHE_UP , &nfsd_file_flags) ==1 ) { +if (nfsd_startup_get ()) {struct bucket_table *tbl;struct rhashtable *ht;
@@ -1360,8 +1350,9 @@ int nfsd_file_cache_stats_show(struct seq_file *m, void *v) tbl =rht_dereference_rcu (ht->tbl, ht); buckets = tbl->size;rcu_read_unlock (); + +nfsd_startup_put (); } -mutex_unlock (&nfsd_mutex);for_each_possible_cpu (i) { hits +=per_cpu (nfsd_file_cache_hits, i);
fs/nfsd/nfsd.h +3 -0
@@ -80,6 +80,9 @@ extern const struct svc_version nfsd_version2, nfsd_version3, nfsd_version4;extern struct mutex nfsd_mutex;extern atomic_t nfsd_th_cnt;/* number of available threads */ +bool nfsd_startup_get (void ); +void nfsd_startup_put (void ); +extern const struct seq_operations nfs_exports_op;/*
fs/nfsd/nfssvc.c +27 -14
79 lines not on this page · raw message
Signature · 1 line
2.49.0
NeilBrown
reviewerPATCH 3/3 nfsd: split nfsd_mutex into one mutex per net-namespace.
In reply to NeilBrownThe remaining uses for nfsd_mutex are all to protect per-netns resources.
This patch replaces the global mutex with one per netns. The "svc_info" struct now contains that mutex rather than a pointer to the mutex.
The use of guard(mutex)(...) is extended to all users of this lock.
--- .../admin-guide/nfs/nfsd-admin-interfaces.rst | 4 +- fs/nfsd/netns.h | 1 + fs/nfsd/nfsctl.c | 158 +++++++----------- fs/nfsd/nfsd.h | 1 - fs/nfsd/nfssvc.c | 40 ++--- include/linux/sunrpc/svc.h | 2 +- net/sunrpc/svc_xprt.c | 4 +- 7 files changed, 73 insertions(+), 137 deletions(-)
Documentation/admin-guide/nfs/nfsd-admin-interfaces.rst +3 -1
@@ -37,4 +37,6 @@ Implementation notes Note that the rpc server requires the caller to serialize addition and removal of listening sockets, and startup and shutdown of the server. -For nfsd this is done using nfsd_mutex. +For nfsd this is done using nfsd_info.mutex in struct nfsd_net, which is +called config_mutex. +
fs/nfsd/netns.h +1 -0
@@ -134,6 +134,7 @@ struct nfsd_net {struct svc_info nfsd_info;#define nfsd_serv nfsd_info.serv +#define config_mutex nfsd_info.mutexstruct percpu_ref nfsd_net_ref;struct completion nfsd_net_confirm_done;
fs/nfsd/nfsctl.c +56 -102
435 lines not on this page · raw message
fs/nfsd/nfsd.h +0 -1
@@ -77,7 +77,6 @@ struct nfsd_genl_rqstp {extern struct svc_program nfsd_programs[];extern const struct svc_version nfsd_version2, nfsd_version3, nfsd_version4; -extern struct mutex nfsd_mutex;extern atomic_t nfsd_th_cnt;/* number of available threads */ bool nfsd_startup_get (void );
fs/nfsd/nfssvc.c +10 -30
@@ -56,20 +56,6 @@ static __be32 nfsd_init_request(struct svc_rqst *, const struct svc_program *, struct svc_process_info *); -/* -* nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and some members -* of the svc_serv struct such as ->sv_temp_socks and ->sv_permsocks. -* -* Finally, the nfsd_mutex also protects some of the global variables that are -* accessed when nfsd starts and that are settable via the write_* routines in -* nfsctl.c. In particular: -* -* user_recovery_dirname -* user_lease_time -* nfsd_versions -*/ -DEFINE_MUTEX (nfsd_mutex); -#if IS_ENABLED (CONFIG_NFS_LOCALIO )static const struct svc_version *localio_versions[] = { [1] = &localio_version1,
@@ -239,14 +225,12 @@ static void nfsd_net_free(struct percpu_ref *ref)int nfsd_nrthreads (struct net *net) { -int rv =0 ;struct nfsd_net *nn =net_generic (net, nfsd_net_id); -mutex_lock (&nfsd_mutex); -if (nn->nfsd_serv) - rv = nn->nfsd_serv->sv_nrthreads; -mutex_unlock (&nfsd_mutex); -return rv; +guard (mutex)(&nn->config_mutex); +if (!nn->nfsd_serv) +return 0 ; +return nn->nfsd_serv->sv_nrthreads; }static int nfsd_init_socks (struct net *net,const struct cred *cred)
@@ -522,7 +506,6 @@ static struct notifier_block nfsd_inet6addr_notifier = { };#endif -/* Only used under nfsd_mutex, so this atomic may be overkill: */ static atomic_t nfsd_notifier_refcount =ATOMIC_INIT (0 );/**
@@ -534,7 +517,7 @@ void nfsd_destroy_serv(struct net *net)struct nfsd_net *nn =net_generic (net, nfsd_net_id);struct svc_serv *serv = nn->nfsd_serv; -lockdep_assert_held (&nfsd_mutex); +lockdep_assert_held (&nn->config_mutex);spin_lock (&nfsd_notifier_lock); nn->nfsd_serv =NULL ;
@@ -606,17 +589,14 @@ void nfsd_shutdown_threads(struct net *net)struct nfsd_net *nn =net_generic (net, nfsd_net_id);struct svc_serv *serv; -mutex_lock (&nfsd_mutex); +guard (mutex)(&nn->config_mutex); serv = nn->nfsd_serv; -if (serv ==NULL ) { -mutex_unlock (&nfsd_mutex); +if (serv ==NULL )return ; - }/* Kill outstanding nfsd threads */ svc_set_num_threads (serv,NULL ,0 );nfsd_destroy_serv (net); -mutex_unlock (&nfsd_mutex); }struct svc_rqst *nfsd_current_rqst (void )
@@ -632,7 +612,7 @@ int nfsd_create_serv(struct net *net)struct nfsd_net *nn =net_generic (net, nfsd_net_id);struct svc_serv *serv; -WARN_ON (!mutex_is_locked (&nfsd_mutex)); +WARN_ON (!mutex_is_locked (&nn->config_mutex));if (nn->nfsd_serv)return 0 ;
@@ -714,7 +694,7 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)int err =0 ;struct nfsd_net *nn =net_generic (net, nfsd_net_id); -lockdep_assert_held (&nfsd_mutex); +lockdep_assert_held (&nn->config_mutex);if (nn->nfsd_serv ==NULL || n <=0 )return 0 ;
@@ -787,7 +767,7 @@ nfsd_svc(int n, int *nthreads, struct net *net, const struct cred *cred, const cstruct nfsd_net *nn =net_generic (net, nfsd_net_id);struct svc_serv *serv; -lockdep_assert_held (&nfsd_mutex); +lockdep_assert_held (&nn->config_mutex);dprintk ("nfsd: creating service\n" );
include/linux/sunrpc/svc.h +1 -1
@@ -98,7 +98,7 @@ struct svc_serv {/* This is used by pool_stats to find and lock an svc */ struct svc_info {struct svc_serv *serv; -struct mutex *mutex; +struct mutex mutex; };void svc_destroy (struct svc_serv **svcp);
net/sunrpc/svc_xprt.c +2 -2
@@ -1399,7 +1399,7 @@ static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos)dprintk ("svc_pool_stats_start, *pidx=%u\n" , pidx); -mutex_lock (si->mutex); +mutex_lock (&si->mutex);if (!pidx)return SEQ_START_TOKEN ;
@@ -1436,7 +1436,7 @@ static void svc_pool_stats_stop(struct seq_file *m, void *p) {struct svc_info *si = m->private; -mutex_unlock (si->mutex); +mutex_unlock (&si->mutex); }static int svc_pool_stats_show (struct seq_file *m,void *p)
Signature · 1 line
2.49.0
kernel test robot
build botRe: [PATCH 3/3] nfsd: split nfsd_mutex into one mutex per net-namespace.
kernel test robot noticed the following build errors:
Hi NeilBrown,
kernel test robot noticed the following build errors:
[auto build test ERROR on brauner-vfs/vfs.all]
[also build test ERROR on trondmy-nfs/linux-next linus/master v6.16-rc2 next-20250620] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/NeilBrown/nfsd-provide-proper-locking-for-all-write_-function/20250621-073955 base: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all patch link: https://lore.kernel.org/r/20250620233802.1453016-4-neil%40brown.name patch subject: [PATCH 3/3] nfsd: split nfsd_mutex into one mutex per net-namespace. config: i386-buildonly-randconfig-003-20250621 (https://download.01.org/0day-ci/archive/20250621/[email protected]/config) compiler: clang version 20.1.2 (https://github.com/llvm/llvm-project 58df0ef89dd64126512e4ee27b4ac3fd8ddf6247) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250621/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> fs/nfsd/nfsctl.c:1699:3: error: cannot jump from this goto statement to its label
1699 | goto err_free_msg;
| ^
fs/nfsd/nfsctl.c:1702:2: note: jump bypasses initialization of variable with __attribute__((cleanup))
1702 | guard(mutex)(&nn->config_mutex);
| ^
include/linux/cleanup.h:338:15: note: expanded from macro 'guard'
338 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/compiler.h:166:29: note: expanded from macro '__UNIQUE_ID'
166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
| ^
include/linux/compiler_types.h:84:22: note: expanded from macro '__PASTE'
84 | #define __PASTE(a,b) ___PASTE(a,b)
| ^
include/linux/compiler_types.h:83:23: note: expanded from macro '___PASTE'
83 | #define ___PASTE(a,b) a##b
| ^
<scratch space>:45:1: note: expanded from here
45 | __UNIQUE_ID_guard1097
| ^
fs/nfsd/nfsctl.c:1825:3: error: cannot jump from this goto statement to its label
1825 | goto err_free_msg;
| ^
fs/nfsd/nfsctl.c:1829:2: note: jump bypasses initialization of variable with __attribute__((cleanup))
1829 | guard(mutex)(&nn->config_mutex);
| ^
include/linux/cleanup.h:338:15: note: expanded from macro 'guard'
338 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/compiler.h:166:29: note: expanded from macro '__UNIQUE_ID'
166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
| ^
include/linux/compiler_types.h:84:22: note: expanded from macro '__PASTE'
84 | #define __PASTE(a,b) ___PASTE(a,b)
| ^
include/linux/compiler_types.h:83:23: note: expanded from macro '___PASTE'
83 | #define ___PASTE(a,b) a##b
| ^
<scratch space>:68:1: note: expanded from here
68 | __UNIQUE_ID_guard1099
| ^
fs/nfsd/nfsctl.c:2044:3: error: cannot jump from this goto statement to its label
2044 | goto err_free_msg;
| ^
fs/nfsd/nfsctl.c:2048:2: note: jump bypasses initialization of variable with __attribute__((cleanup))
2048 | guard(mutex)(&nn->config_mutex);
| ^
include/linux/cleanup.h:338:15: note: expanded from macro 'guard'
338 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/compiler.h:166:29: note: expanded from macro '__UNIQUE_ID'
166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
| ^
include/linux/compiler_types.h:84:22: note: expanded from macro '__PASTE'
84 | #define __PASTE(a,b) ___PASTE(a,b)
| ^
include/linux/compiler_types.h:83:23: note: expanded from macro '___PASTE'
83 | #define ___PASTE(a,b) a##b
| ^
<scratch space>:118:1: note: expanded from here
118 | __UNIQUE_ID_guard1101
| ^3 errors generated.
vim +1699 fs/nfsd/nfsctl.c
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1677
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1678 /**
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1679 * nfsd_nl_threads_get_doit - get the number of running threads
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1680 * @skb: reply buffer
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1681 * @info: netlink metadata and command arguments
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1682 *
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1683 * Return 0 on success or a negative errno.
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1684 */
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1685 int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info)
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1686 {
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1687 struct net *net = genl_info_net(info);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1688 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1689 void *hdr;
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1690 int err;
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1691
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1692 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1693 if (!skb)
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1694 return -ENOMEM;
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1695
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1696 hdr = genlmsg_iput(skb, info);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1697 if (!hdr) {
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1698 err = -EMSGSIZE;
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 @1699 goto err_free_msg;
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1700 }
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1701
40677c06f2c6a4 NeilBrown 2025-06-21 1702 guard(mutex)(&nn->config_mutex);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1703
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1704 err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME,
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1705 nn->nfsd4_grace) ||
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1706 nla_put_u32(skb, NFSD_A_SERVER_LEASETIME,
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1707 nn->nfsd4_lease) ||
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1708 nla_put_string(skb, NFSD_A_SERVER_SCOPE,
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1709 nn->nfsd_name);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1710 if (err)
40677c06f2c6a4 NeilBrown 2025-06-21 1711 goto err_free_msg;
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1712
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1713 if (nn->nfsd_serv) {
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1714 int i;
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1715
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1716 for (i = 0; i < nfsd_nrpools(net); ++i) {
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1717 struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i];
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1718
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1719 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS,
60749cbe3d8ae5 NeilBrown 2024-07-15 1720 sp->sp_nrthreads);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1721 if (err)
40677c06f2c6a4 NeilBrown 2025-06-21 1722 goto err_free_msg;
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1723 }
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1724 } else {
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1725 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1726 if (err)
40677c06f2c6a4 NeilBrown 2025-06-21 1727 goto err_free_msg;
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1728 }
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1729
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1730 genlmsg_end(skb, hdr);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1731
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1732 return genlmsg_reply(skb, info);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1733
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1734 err_free_msg:
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1735 nlmsg_free(skb);
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1736
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1737 return err;
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1738 }
924f4fb003ba11 Lorenzo Bianconi 2024-04-23 1739 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 6 lines · NeilBrown
The first patch hopefully fixes a bug with locking as reported by Li
Lingfeng: somewrite_foofunctions aren't locked properly.The other two improve the locking code, particulary so that we don't need a global mutex to change per-netns data.
Quoted 7 lines
I've revised the locking to use guard(mutex) for (almost) all places that the per-netfs mutex is used. I think this is an improvement but would like to know what others think.
I haven't changed _get/_put to _pin/_unpin as Chuck wondered about. I'm not against that (though get/put are widely understood) but nor am I particularly for it yet. Again, opinions are welcome.
I think of get and put as operations you do on an object. Saying
nfsd_startup_get();
seems a little strange to me. As I said before, it seems like you are protecting a critical section, not a particular object.
Signature · 1 line
Chuck Lever
Jeff Layton
maintainerQuoted 15 lines · Chuck Lever
Quoted 6 lines · NeilBrown
The first patch hopefully fixes a bug with locking as reported by Li
Lingfeng: somewrite_foofunctions aren't locked properly.The other two improve the locking code, particulary so that we don't need a global mutex to change per-netns data.
Quoted 7 lines
I've revised the locking to use guard(mutex) for (almost) all places that the per-netfs mutex is used. I think this is an improvement but would like to know what others think.
I haven't changed _get/_put to _pin/_unpin as Chuck wondered about. I'm not against that (though get/put are widely understood) but nor am I particularly for it yet. Again, opinions are welcome.
Quoted 7 lines
I think of get and put as operations you do on an object. Saying
nfsd_startup_get();seems a little strange to me. As I said before, it seems like you are protecting a critical section, not a particular object.
I think of it as taking a reference to the service being up and running. Maybe nfsd_service_get/put() ?
Signature · 1 line
Jeff Layton <[email protected]>
NeilBrown
reviewerQuoted 15 lines · Chuck Lever
Quoted 6 lines · NeilBrown
The first patch hopefully fixes a bug with locking as reported by Li
Lingfeng: somewrite_foofunctions aren't locked properly.The other two improve the locking code, particulary so that we don't need a global mutex to change per-netns data.
Quoted 7 lines
I've revised the locking to use guard(mutex) for (almost) all places that the per-netfs mutex is used. I think this is an improvement but would like to know what others think.
I haven't changed _get/_put to _pin/_unpin as Chuck wondered about. I'm not against that (though get/put are widely understood) but nor am I particularly for it yet. Again, opinions are welcome.
Quoted 6 lines
I think of get and put as operations you do on an object. Saying
nfsd_startup_get();seems a little strange to me. As I said before, it seems like you are protecting a critical section, not a particular object.
I agree it looks like a critical section. That suggests lock/unlock naming.
A "get" is somewhat like a read_trylock. But a put isn't much like an unlock.
But maybe I can side-step the whole issue. I think it is reasonable to move the nfsd_shutdown_generic() code in to nfsd_exit() which is called at module_put time. There is no real need for any this before then. With that done the ref on the module that we already maintain correctly will ensure no code can race with the cleanup in nfsd_shutdown_generic().
Then we don't need nfsd_users as a counter - just a flag to say if startup has completed yet.
This series does that. It also delays the conversion to guard(mutex) until a final patch so it can be reviewed separately - or rejected separately.
Thanks,
NeilBrown