Skip to contentSkip to conversation

nfs: reduce stack usage of nfs_get_root

RFC PATCH In review Nicholas Piggin 2 participants 2 messages Sep 27, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 nfs: reduce stack usage of nfs_get_root

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

---
Hi,

This is motivated by a stack overflow described here:
https://lore.kernel.org/netdev/[email protected]/

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(-)
fs/nfs/getroot.c +14 -8
index 11ff2b2e060f..6e4188c09639 100644
@@ -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:
Signature · 1 line
2.40.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Nicholas Piggin

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

---
Hi,

Quoted 8 lines

This is motivated by a stack overflow described here:
https://lore.kernel.org/netdev/[email protected]/

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

Thanks,
Nick

Looks good, might be able to get rid of the local "root" var too if the compiler doesn't optimize it away.

Ben