linux-nfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Nicholas Piggin <npiggin@gmail.com>
To: linux-nfs@vger.kernel.org
Cc: Nicholas Piggin <npiggin@gmail.com>,
	Trond Myklebust <trond.myklebust@hammerspace.com>,
	Anna Schumaker <anna@kernel.org>,
	linux-kernel@vger.kernel.org
Subject: [RFC PATCH] nfs: reduce stack usage of nfs_get_root
Date: Wed, 27 Sep 2023 10:16:23 +1000	[thread overview]
Message-ID: <20230927001624.750031-1-npiggin@gmail.com> (raw)

Move fsinfo allocation off stack, reducing stack overhead of
nfs_get_root from 304 to 192 bytes.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
Hi,

This is motivated by a stack overflow described here:
https://lore.kernel.org/netdev/20230927001308.749910-1-npiggin@gmail.com/

NFS is not really a major culprit but it seems not too hard to
shrink the stack a little.

Thanks,
Nick

 fs/nfs/getroot.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/fs/nfs/getroot.c b/fs/nfs/getroot.c
index 11ff2b2e060f..6e4188c09639 100644
--- a/fs/nfs/getroot.c
+++ b/fs/nfs/getroot.c
@@ -68,7 +68,7 @@ int nfs_get_root(struct super_block *s, struct fs_context *fc)
 {
 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
 	struct nfs_server *server = NFS_SB(s), *clone_server;
-	struct nfs_fsinfo fsinfo;
+	struct nfs_fsinfo *fsinfo;
 	struct dentry *root;
 	struct inode *inode;
 	char *name;
@@ -79,19 +79,23 @@ int nfs_get_root(struct super_block *s, struct fs_context *fc)
 	if (!name)
 		goto out;
 
-	/* get the actual root for this mount */
-	fsinfo.fattr = nfs_alloc_fattr_with_label(server);
-	if (fsinfo.fattr == NULL)
+	fsinfo = kmalloc(sizeof(*fsinfo), GFP_KERNEL);
+	if (!fsinfo)
 		goto out_name;
 
-	error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, &fsinfo);
+	/* get the actual root for this mount */
+	fsinfo->fattr = nfs_alloc_fattr_with_label(server);
+	if (fsinfo->fattr == NULL)
+		goto out_fsinfo;
+
+	error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, fsinfo);
 	if (error < 0) {
 		dprintk("nfs_get_root: getattr error = %d\n", -error);
 		nfs_errorf(fc, "NFS: Couldn't getattr on root");
 		goto out_fattr;
 	}
 
-	inode = nfs_fhget(s, ctx->mntfh, fsinfo.fattr);
+	inode = nfs_fhget(s, ctx->mntfh, fsinfo->fattr);
 	if (IS_ERR(inode)) {
 		dprintk("nfs_get_root: get root inode failed\n");
 		error = PTR_ERR(inode);
@@ -148,11 +152,13 @@ int nfs_get_root(struct super_block *s, struct fs_context *fc)
 		!(kflags_out & SECURITY_LSM_NATIVE_LABELS))
 		server->caps &= ~NFS_CAP_SECURITY_LABEL;
 
-	nfs_setsecurity(inode, fsinfo.fattr);
+	nfs_setsecurity(inode, fsinfo->fattr);
 	error = 0;
 
 out_fattr:
-	nfs_free_fattr(fsinfo.fattr);
+	nfs_free_fattr(fsinfo->fattr);
+out_fsinfo:
+	kfree(fsinfo);
 out_name:
 	kfree(name);
 out:
-- 
2.40.1


             reply	other threads:[~2023-09-27  4:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-27  0:16 Nicholas Piggin [this message]
2023-09-27 13:45 ` [RFC PATCH] nfs: reduce stack usage of nfs_get_root Benjamin Coddington

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=20230927001624.750031-1-npiggin@gmail.com \
    --to=npiggin@gmail.com \
    --cc=anna@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=trond.myklebust@hammerspace.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;
as well as URLs for NNTP newsgroup(s).