svc: don't hold reference for poolstats, only mutex.
authorNeilBrown <neilb@suse.de>
Fri, 15 Dec 2023 00:56:32 +0000 (11:56 +1100)
committerChuck Lever <chuck.lever@oracle.com>
Sun, 7 Jan 2024 22:54:33 +0000 (17:54 -0500)
A future patch will remove refcounting on svc_serv as it is of little
use.
It is currently used to keep the svc around while the pool_stats file is
open.
Change this to get the pointer, protected by the mutex, only in
seq_start, and the release the mutex in seq_stop.
This means that if the nfsd server is stopped and restarted while the
pool_stats file it open, then some pool stats info could be from the
first instance and some from the second.  This might appear odd, but is
unlikely to be a problem in practice.

Signed-off-by: NeilBrown <neilb@suse.de>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
fs/nfsd/netns.h
fs/nfsd/nfsctl.c
fs/nfsd/nfssvc.c
include/linux/sunrpc/svc.h
net/sunrpc/svc_xprt.c

index ab303a8b77d5377b405d0382b3115ba53f9f4297..16dbef245dbbb3578f40471616bcd98a4e4c5df1 100644 (file)
@@ -123,7 +123,9 @@ struct nfsd_net {
        u32 clientid_counter;
        u32 clverifier_counter;
 
-       struct svc_serv *nfsd_serv;
+       struct svc_info nfsd_info;
+#define nfsd_serv nfsd_info.serv
+
        /* When a listening socket is added to nfsd, keep_active is set
         * and this justifies a reference on nfsd_serv.  This stops
         * nfsd_serv from being freed.  When the number of threads is
index 6a3b385703ccc9665f7f8e99dbd3cd4b8c37836e..46a001e81b55ebb87ebad5f4ed817dd9b96c5864 100644 (file)
@@ -183,7 +183,7 @@ static const struct file_operations pool_stats_operations = {
        .open           = nfsd_pool_stats_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
-       .release        = nfsd_pool_stats_release,
+       .release        = seq_release,
 };
 
 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
index ee835bf9ee421dee934c56344e7021362edf99d7..365968737923cc08caecca1dd9120265356fc0aa 100644 (file)
@@ -684,6 +684,7 @@ int nfsd_create_serv(struct net *net)
                return error;
        }
        spin_lock(&nfsd_notifier_lock);
+       nn->nfsd_info.mutex = &nfsd_mutex;
        nn->nfsd_serv = serv;
        spin_unlock(&nfsd_notifier_lock);
 
@@ -1082,28 +1083,7 @@ bool nfssvc_encode_voidres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
 
 int nfsd_pool_stats_open(struct inode *inode, struct file *file)
 {
-       int ret;
        struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
 
-       mutex_lock(&nfsd_mutex);
-       if (nn->nfsd_serv == NULL) {
-               mutex_unlock(&nfsd_mutex);
-               return -ENODEV;
-       }
-       svc_get(nn->nfsd_serv);
-       ret = svc_pool_stats_open(nn->nfsd_serv, file);
-       mutex_unlock(&nfsd_mutex);
-       return ret;
-}
-
-int nfsd_pool_stats_release(struct inode *inode, struct file *file)
-{
-       struct seq_file *seq = file->private_data;
-       struct svc_serv *serv = seq->private;
-       int ret = seq_release(inode, file);
-
-       mutex_lock(&nfsd_mutex);
-       svc_put(serv);
-       mutex_unlock(&nfsd_mutex);
-       return ret;
+       return svc_pool_stats_open(&nn->nfsd_info, file);
 }
index 544fcfe07479959ee5570b111c66b85176314237..3bea2840272d3c378517dfb5116a5bea9d243ecc 100644 (file)
@@ -97,6 +97,12 @@ struct svc_serv {
 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
 };
 
+/* This is used by pool_stats to find and lock an svc */
+struct svc_info {
+       struct svc_serv         *serv;
+       struct mutex            *mutex;
+};
+
 /**
  * svc_get() - increment reference count on a SUNRPC serv
  * @serv:  the svc_serv to have count incremented
@@ -431,7 +437,7 @@ void                   svc_exit_thread(struct svc_rqst *);
 struct svc_serv *  svc_create_pooled(struct svc_program *, unsigned int,
                                     int (*threadfn)(void *data));
 int               svc_set_num_threads(struct svc_serv *, struct svc_pool *, int);
-int               svc_pool_stats_open(struct svc_serv *serv, struct file *file);
+int               svc_pool_stats_open(struct svc_info *si, struct file *file);
 void              svc_process(struct svc_rqst *rqstp);
 void              svc_process_bc(struct rpc_rqst *req, struct svc_rqst *rqstp);
 int               svc_register(const struct svc_serv *, struct net *, const int,
index 1b71055fc391a65f07be09710d922218b0e9e6bc..b4a85a227bd7d0cb0b4d363e999f9331eca304f0 100644 (file)
@@ -1362,29 +1362,36 @@ int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen)
 }
 EXPORT_SYMBOL_GPL(svc_xprt_names);
 
-
 /*----------------------------------------------------------------------------*/
 
 static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos)
 {
        unsigned int pidx = (unsigned int)*pos;
-       struct svc_serv *serv = m->private;
+       struct svc_info *si = m->private;
 
        dprintk("svc_pool_stats_start, *pidx=%u\n", pidx);
 
+       mutex_lock(si->mutex);
+
        if (!pidx)
                return SEQ_START_TOKEN;
-       return (pidx > serv->sv_nrpools ? NULL : &serv->sv_pools[pidx-1]);
+       if (!si->serv)
+               return NULL;
+       return pidx > si->serv->sv_nrpools ? NULL
+               : &si->serv->sv_pools[pidx - 1];
 }
 
 static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos)
 {
        struct svc_pool *pool = p;
-       struct svc_serv *serv = m->private;
+       struct svc_info *si = m->private;
+       struct svc_serv *serv = si->serv;
 
        dprintk("svc_pool_stats_next, *pos=%llu\n", *pos);
 
-       if (p == SEQ_START_TOKEN) {
+       if (!serv) {
+               pool = NULL;
+       } else if (p == SEQ_START_TOKEN) {
                pool = &serv->sv_pools[0];
        } else {
                unsigned int pidx = (pool - &serv->sv_pools[0]);
@@ -1399,6 +1406,9 @@ static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos)
 
 static void svc_pool_stats_stop(struct seq_file *m, void *p)
 {
+       struct svc_info *si = m->private;
+
+       mutex_unlock(si->mutex);
 }
 
 static int svc_pool_stats_show(struct seq_file *m, void *p)
@@ -1426,14 +1436,18 @@ static const struct seq_operations svc_pool_stats_seq_ops = {
        .show   = svc_pool_stats_show,
 };
 
-int svc_pool_stats_open(struct svc_serv *serv, struct file *file)
+int svc_pool_stats_open(struct svc_info *info, struct file *file)
 {
+       struct seq_file *seq;
        int err;
 
        err = seq_open(file, &svc_pool_stats_seq_ops);
-       if (!err)
-               ((struct seq_file *) file->private_data)->private = serv;
-       return err;
+       if (err)
+               return err;
+       seq = file->private_data;
+       seq->private = info;
+
+       return 0;
 }
 EXPORT_SYMBOL(svc_pool_stats_open);