Linux NFS development
 help / color / mirror / Atom feed
From: Mike Snitzer <snitzer@kernel.org>
To: Jonathan Curley <jcurley@purestorage.com>,
	Anna Schumaker <anna@kernel.org>
Cc: Trond Myklebust <trondmy@kernel.org>,
	Luis Chamberlain <mcgrof@kernel.org>,
	linux-nfs@vger.kernel.org
Subject: [PATCH v2] NFSv4/flexfiles: fix to allocate mirror->dss before use
Date: Tue, 7 Oct 2025 13:39:05 -0400	[thread overview]
Message-ID: <aOVQORJe8DkQrHH4@kernel.org> (raw)
In-Reply-To: <aOU7Yc8RnF4Eq65C@kernel.org>

Move mirror_array's dss_count initialization and dss allocation to
ff_layout_alloc_mirror(), just before the loop that initializes each
nfs4_ff_layout_ds_stripe's nfs_file_localio.

Also handle NULL return from kcalloc() and remove one level of ident
in ff_layout_alloc_mirror().

This commit fixes dangling nfsd_serv refcount issues seen when using
NFS LOCALIO and then attempting to stop the NFSD service.

Fixes: 20b1d75fb840 ("NFSv4/flexfiles: Add support for striped layouts")
Signed-off-by: Mike Snitzer <snitzer@kernel.org>
---
v2: checks for NULL return from kcalloc() and remove one level of ident in ff_layout_alloc_mirror

diff --git a/fs/nfs/flexfilelayout/flexfilelayout.c b/fs/nfs/flexfilelayout/flexfilelayout.c
index fedd7d90e12f..b7d2c0ef25fe 100644
--- a/fs/nfs/flexfilelayout/flexfilelayout.c
+++ b/fs/nfs/flexfilelayout/flexfilelayout.c
@@ -270,19 +270,31 @@ ff_layout_remove_mirror(struct nfs4_ff_layout_mirror *mirror)
 	mirror->layout = NULL;
 }
 
-static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(gfp_t gfp_flags)
+static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(u32 dss_count,
+							    gfp_t gfp_flags)
 {
 	struct nfs4_ff_layout_mirror *mirror;
-	u32 dss_id;
 
 	mirror = kzalloc(sizeof(*mirror), gfp_flags);
-	if (mirror != NULL) {
-		spin_lock_init(&mirror->lock);
-		refcount_set(&mirror->ref, 1);
-		INIT_LIST_HEAD(&mirror->mirrors);
-		for (dss_id = 0; dss_id < mirror->dss_count; dss_id++)
-			nfs_localio_file_init(&mirror->dss[dss_id].nfl);
+	if (mirror == NULL)
+		return NULL;
+
+	spin_lock_init(&mirror->lock);
+	refcount_set(&mirror->ref, 1);
+	INIT_LIST_HEAD(&mirror->mirrors);
+
+	mirror->dss_count = dss_count;
+	mirror->dss =
+		kcalloc(dss_count, sizeof(struct nfs4_ff_layout_ds_stripe),
+			gfp_flags);
+	if (mirror->dss == NULL) {
+		kfree(mirror);
+		return NULL;
 	}
+
+	for (u32 dss_id = 0; dss_id < mirror->dss_count; dss_id++)
+		nfs_localio_file_init(&mirror->dss[dss_id].nfl);
+
 	return mirror;
 }
 
@@ -507,17 +519,12 @@ ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh,
 		if (dss_count > 1 && stripe_unit == 0)
 			goto out_err_free;
 
-		fls->mirror_array[i] = ff_layout_alloc_mirror(gfp_flags);
+		fls->mirror_array[i] = ff_layout_alloc_mirror(dss_count, gfp_flags);
 		if (fls->mirror_array[i] == NULL) {
 			rc = -ENOMEM;
 			goto out_err_free;
 		}
 
-		fls->mirror_array[i]->dss_count = dss_count;
-		fls->mirror_array[i]->dss =
-		    kcalloc(dss_count, sizeof(struct nfs4_ff_layout_ds_stripe),
-			    gfp_flags);
-
 		for (dss_id = 0; dss_id < dss_count; dss_id++) {
 			dss_info = &fls->mirror_array[i]->dss[dss_id];
 			dss_info->mirror = fls->mirror_array[i];

  reply	other threads:[~2025-10-07 17:39 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-24 16:20 [RFC PATCH v4 0/9] NFSv4/flexfiles: Add support for striped layouts Jonathan Curley
2025-09-24 16:20 ` [RFC PATCH v4 1/9] NFSv4/flexfiles: Remove cred local variable dependency Jonathan Curley
2025-09-24 16:20 ` [RFC PATCH v4 2/9] NFSv4/flexfiles: Use ds_commit_idx when marking a write commit Jonathan Curley
2025-09-24 16:20 ` [RFC PATCH v4 3/9] NFSv4/flexfiles: Add data structure support for striped layouts Jonathan Curley
2025-09-24 16:20 ` [RFC PATCH v4 4/9] NFSv4/flexfiles: Update low level helper functions to be DS stripe aware Jonathan Curley
2025-09-24 16:20 ` [RFC PATCH v4 5/9] NFSv4/flexfiles: Read path updates for striped layouts Jonathan Curley
2025-09-24 16:20 ` [RFC PATCH v4 6/9] NFSv4/flexfiles: Commit " Jonathan Curley
2025-09-24 16:20 ` [RFC PATCH v4 7/9] NFSv4/flexfiles: Write " Jonathan Curley
2025-09-24 16:20 ` [RFC PATCH v4 8/9] NFSv4/flexfiles: Update layout stats & error paths " Jonathan Curley
2025-09-24 16:20 ` [RFC PATCH v4 9/9] NFSv4/flexfiles: Add support " Jonathan Curley
2025-10-07 14:05 ` [RFC PATCH v4 0/9] " Mike Snitzer
2025-10-07 14:50   ` Mike Snitzer
2025-10-07 16:10     ` [PATCH] NFSv4/flexfiles: fix to allocate mirror->dss before use Mike Snitzer
2025-10-07 17:39       ` Mike Snitzer [this message]
2025-10-07 18:03         ` [PATCH v2] " Jon Curley
2025-10-15 13:09 ` [RFC PATCH v4 0/9] NFSv4/flexfiles: Add support for striped layouts Mike Snitzer
2025-10-20 19:58   ` Mike Snitzer

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=aOVQORJe8DkQrHH4@kernel.org \
    --to=snitzer@kernel.org \
    --cc=anna@kernel.org \
    --cc=jcurley@purestorage.com \
    --cc=linux-nfs@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=trondmy@kernel.org \
    /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