public inbox for linux-nfs@vger.kernel.org
 help / color / mirror / Atom feed
From: NeilBrown <neilb@suse.de>
To: Chuck Lever <chuck.lever@oracle.com>, Jeff Layton <jlayton@kernel.org>
Cc: linux-nfs@vger.kernel.org, Olga Kornievskaia <kolga@netapp.com>,
	Dai Ngo <Dai.Ngo@oracle.com>, Tom Talpey <tom@talpey.com>,
	Steve Dickson <steved@redhat.com>
Subject: [PATCH 07/14] Change unshare_fs_struct() to never fail.
Date: Mon, 15 Jul 2024 17:14:20 +1000	[thread overview]
Message-ID: <20240715074657.18174-8-neilb@suse.de> (raw)
In-Reply-To: <20240715074657.18174-1-neilb@suse.de>

nfsd threads need to not share the init fs_struct as they need to
manipulate umask independently.  So they call unshare_fs_struct() and
are the only user of that function.

In the unlikely event that unshare_fs_struct() fails, the thread will
exit calling svc_exit_thread() BEFORE svc_thread_should_stop() reports
'true'.

This is a problem because svc_exit_thread() assumes that
svc_stop_threads() is running and consequently (in the nfsd case)
nfsd_mutex is held.  This ensures that the list_del_rcu() call in
svc_exit_thread() cannot race with any other manipulation of
->sp_all_threads.

While it would be possible to add some other exclusion, doing so would
introduce unnecessary complexity.  unshare_fs_struct() does not fail in
practice.  So the simplest solution is to make this explicit.  i.e.  use
__GFP_NOFAIL which is safe on such a small allocation - about 64 bytes.

Change unshare_fs_struct() to not return any error, and remove the error
handling from nfsd().

An alternate approach would be to create a variant of
kthread_create_on_node() which didn't set CLONE_FS.

Signed-off-by: NeilBrown <neilb@suse.de>
---
 fs/fs_struct.c            | 42 ++++++++++++++++++++-------------------
 fs/nfsd/nfssvc.c          |  9 +++------
 include/linux/fs_struct.h |  2 +-
 3 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/fs/fs_struct.c b/fs/fs_struct.c
index 64c2d0814ed6..49fba862e408 100644
--- a/fs/fs_struct.c
+++ b/fs/fs_struct.c
@@ -109,35 +109,39 @@ void exit_fs(struct task_struct *tsk)
 	}
 }
 
+static void init_fs_struct(struct fs_struct *fs, struct fs_struct *old)
+{
+	fs->users = 1;
+	fs->in_exec = 0;
+	spin_lock_init(&fs->lock);
+	seqcount_spinlock_init(&fs->seq, &fs->lock);
+	fs->umask = old->umask;
+
+	spin_lock(&old->lock);
+	fs->root = old->root;
+	path_get(&fs->root);
+	fs->pwd = old->pwd;
+	path_get(&fs->pwd);
+	spin_unlock(&old->lock);
+}
+
 struct fs_struct *copy_fs_struct(struct fs_struct *old)
 {
 	struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
 	/* We don't need to lock fs - think why ;-) */
-	if (fs) {
-		fs->users = 1;
-		fs->in_exec = 0;
-		spin_lock_init(&fs->lock);
-		seqcount_spinlock_init(&fs->seq, &fs->lock);
-		fs->umask = old->umask;
-
-		spin_lock(&old->lock);
-		fs->root = old->root;
-		path_get(&fs->root);
-		fs->pwd = old->pwd;
-		path_get(&fs->pwd);
-		spin_unlock(&old->lock);
-	}
+	if (fs)
+		init_fs_struct(fs, old);
 	return fs;
 }
 
-int unshare_fs_struct(void)
+void unshare_fs_struct(void)
 {
 	struct fs_struct *fs = current->fs;
-	struct fs_struct *new_fs = copy_fs_struct(fs);
+	struct fs_struct *new_fs = kmem_cache_alloc(fs_cachep,
+						    GFP_KERNEL| __GFP_NOFAIL);
 	int kill;
 
-	if (!new_fs)
-		return -ENOMEM;
+	init_fs_struct(new_fs, fs);
 
 	task_lock(current);
 	spin_lock(&fs->lock);
@@ -148,8 +152,6 @@ int unshare_fs_struct(void)
 
 	if (kill)
 		free_fs_struct(fs);
-
-	return 0;
 }
 EXPORT_SYMBOL_GPL(unshare_fs_struct);
 
diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c
index 7377422a34df..f5de04a63c6f 100644
--- a/fs/nfsd/nfssvc.c
+++ b/fs/nfsd/nfssvc.c
@@ -873,11 +873,9 @@ nfsd(void *vrqstp)
 
 	/* At this point, the thread shares current->fs
 	 * with the init process. We need to create files with the
-	 * umask as defined by the client instead of init's umask. */
-	if (unshare_fs_struct() < 0) {
-		printk("Unable to start nfsd thread: out of memory\n");
-		goto out;
-	}
+	 * umask as defined by the client instead of init's umask.
+	 */
+	unshare_fs_struct();
 
 	current->fs->umask = 0;
 
@@ -899,7 +897,6 @@ nfsd(void *vrqstp)
 
 	atomic_dec(&nfsd_th_cnt);
 
-out:
 	/* Release the thread */
 	svc_exit_thread(rqstp);
 	return 0;
diff --git a/include/linux/fs_struct.h b/include/linux/fs_struct.h
index 783b48dedb72..8282e6c7ff29 100644
--- a/include/linux/fs_struct.h
+++ b/include/linux/fs_struct.h
@@ -22,7 +22,7 @@ extern void set_fs_root(struct fs_struct *, const struct path *);
 extern void set_fs_pwd(struct fs_struct *, const struct path *);
 extern struct fs_struct *copy_fs_struct(struct fs_struct *);
 extern void free_fs_struct(struct fs_struct *);
-extern int unshare_fs_struct(void);
+extern void unshare_fs_struct(void);
 
 static inline void get_fs_root(struct fs_struct *fs, struct path *root)
 {
-- 
2.44.0


  parent reply	other threads:[~2024-07-15  7:48 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-15  7:14 [PATCH 00/14 RFC] support automatic changes to nfsd thread count NeilBrown
2024-07-15  7:14 ` [PATCH 01/14] lockd: discard nlmsvc_timeout NeilBrown
2024-07-15  7:14 ` [PATCH 02/14] SUNRPC: make various functions static, or not exported NeilBrown
2024-07-15  7:14 ` [PATCH 03/14] nfsd: move nfsd_pool_stats_open into nfsctl.c NeilBrown
2024-07-15  7:14 ` [PATCH 04/14] nfsd: don't allocate the versions array NeilBrown
2024-08-02 21:34   ` Mike Snitzer
2024-08-02 23:04     ` NeilBrown
2024-08-05  4:55       ` NeilBrown
2024-07-15  7:14 ` [PATCH 05/14] sunrpc: change sp_nrthreads from atomic_t to unsigned int NeilBrown
2024-07-15 14:12   ` Jeff Layton
2024-07-15 14:33     ` Jeff Layton
2024-07-16  1:33     ` NeilBrown
2024-07-24 19:36       ` Chuck Lever
2024-07-15  7:14 ` [PATCH 06/14] sunrpc: don't take ->sv_lock when updating ->sv_nrthreads NeilBrown
2024-07-15  7:14 ` NeilBrown [this message]
2024-07-15 14:39   ` [PATCH 07/14] Change unshare_fs_struct() to never fail Jeff Layton
2024-07-16  1:48     ` NeilBrown
2024-07-15  7:14 ` [PATCH 08/14] SUNRPC: move nrthreads counting to start/stop threads NeilBrown
2024-07-15  7:14 ` [PATCH 09/14] nfsd: return hard failure for OP_SETCLIENTID when there are too many clients NeilBrown
2024-07-15 15:21   ` Jeff Layton
2024-07-15  7:14 ` [PATCH 10/14] nfs: dynamically adjust per-client DRC slot limits NeilBrown
2024-07-15  7:14 ` [PATCH 11/14] nfsd: don't use sv_nrthreads in connection limiting calculations NeilBrown
2024-07-15 15:52   ` Jeff Layton
2024-07-16  2:04     ` NeilBrown
2024-07-15  7:14 ` [PATCH 12/14] sunrpc: introduce possibility that requested number of threads is different from actual NeilBrown
2024-07-15 16:00   ` Jeff Layton
2024-07-15  7:14 ` [PATCH 13/14] nfsd: introduce concept of a maximum number of threads NeilBrown
2024-07-15 17:06   ` Jeff Layton
2024-07-16  3:21     ` NeilBrown
2024-07-16 11:00       ` Jeff Layton
2024-07-16 13:31         ` Chuck Lever III
2024-07-16 18:49           ` Tom Talpey
2024-07-17 15:24             ` Chuck Lever III
2024-07-15  7:14 ` [PATCH 14/14] nfsd: adjust number of running nfsd threads NeilBrown
2024-07-15 17:29 ` [PATCH 00/14 RFC] support automatic changes to nfsd thread count Jeff Layton
2024-07-24 19:43 ` Chuck Lever III
2024-07-24 21:25   ` NeilBrown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240715074657.18174-8-neilb@suse.de \
    --to=neilb@suse.de \
    --cc=Dai.Ngo@oracle.com \
    --cc=chuck.lever@oracle.com \
    --cc=jlayton@kernel.org \
    --cc=kolga@netapp.com \
    --cc=linux-nfs@vger.kernel.org \
    --cc=steved@redhat.com \
    --cc=tom@talpey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox