linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] [v2] statmount: reduce runtime stack usage
@ 2023-12-13  9:00 Arnd Bergmann
  2023-12-13 10:15 ` Christian Brauner
  2023-12-14  0:11 ` Ian Kent
  0 siblings, 2 replies; 3+ messages in thread
From: Arnd Bergmann @ 2023-12-13  9:00 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: Arnd Bergmann, Jan Kara, Ian Kent, Miklos Szeredi,
	Seth Forshee (DigitalOcean), Dave Chinner, Amir Goldstein,
	linux-fsdevel, linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

prepare_kstatmount() constructs a copy of 'struct kstatmount' on the stack
and copies it into the local variable on the stack of its caller. Because
of the size of this structure, this ends up overflowing the limit for
a single function's stack frame when prepare_kstatmount() gets inlined
and both copies are on the same frame without the compiler being able
to collapse them into one:

fs/namespace.c:4995:1: error: stack frame size (1536) exceeds limit (1024) in '__se_sys_statmount' [-Werror,-Wframe-larger-than]
 4995 | SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req,

Replace the assignment with an in-place memset() plus assignment that
should always be more efficient for both stack usage and runtime cost.

Fixes: 49889374ab92 ("statmount: simplify string option retrieval")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 fs/namespace.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/fs/namespace.c b/fs/namespace.c
index d036196f949c..159f1df379fc 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4957,15 +4957,12 @@ static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq,
 	if (!access_ok(buf, bufsize))
 		return -EFAULT;
 
-	*ks = (struct kstatmount){
-		.mask		= kreq->param,
-		.buf		= buf,
-		.bufsize	= bufsize,
-		.seq = {
-			.size	= seq_size,
-			.buf	= kvmalloc(seq_size, GFP_KERNEL_ACCOUNT),
-		},
-	};
+	memset(ks, 0, sizeof(*ks));
+	ks->mask = kreq->param;
+	ks->buf = buf;
+	ks->bufsize = bufsize;
+	ks->seq.size = seq_size;
+	ks->seq.buf = kvmalloc(seq_size, GFP_KERNEL_ACCOUNT);
 	if (!ks->seq.buf)
 		return -ENOMEM;
 	return 0;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] [v2] statmount: reduce runtime stack usage
  2023-12-13  9:00 [PATCH] [v2] statmount: reduce runtime stack usage Arnd Bergmann
@ 2023-12-13 10:15 ` Christian Brauner
  2023-12-14  0:11 ` Ian Kent
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2023-12-13 10:15 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Alexander Viro, Arnd Bergmann, Jan Kara, Ian Kent, Miklos Szeredi,
	Seth Forshee (DigitalOcean), Dave Chinner, Amir Goldstein,
	linux-fsdevel, linux-kernel

On Wed, Dec 13, 2023 at 10:00:03AM +0100, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> prepare_kstatmount() constructs a copy of 'struct kstatmount' on the stack
> and copies it into the local variable on the stack of its caller. Because
> of the size of this structure, this ends up overflowing the limit for
> a single function's stack frame when prepare_kstatmount() gets inlined
> and both copies are on the same frame without the compiler being able
> to collapse them into one:
> 
> fs/namespace.c:4995:1: error: stack frame size (1536) exceeds limit (1024) in '__se_sys_statmount' [-Werror,-Wframe-larger-than]
>  4995 | SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req,
> 
> Replace the assignment with an in-place memset() plus assignment that
> should always be more efficient for both stack usage and runtime cost.
> 
> Fixes: 49889374ab92 ("statmount: simplify string option retrieval")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---

I folded this patch and placed a link here. Thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] [v2] statmount: reduce runtime stack usage
  2023-12-13  9:00 [PATCH] [v2] statmount: reduce runtime stack usage Arnd Bergmann
  2023-12-13 10:15 ` Christian Brauner
@ 2023-12-14  0:11 ` Ian Kent
  1 sibling, 0 replies; 3+ messages in thread
From: Ian Kent @ 2023-12-14  0:11 UTC (permalink / raw)
  To: Arnd Bergmann, Alexander Viro, Christian Brauner
  Cc: Arnd Bergmann, Jan Kara, Miklos Szeredi,
	Seth Forshee (DigitalOcean), Dave Chinner, Amir Goldstein,
	linux-fsdevel, linux-kernel


On 13/12/23 17:00, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
>
> prepare_kstatmount() constructs a copy of 'struct kstatmount' on the stack
> and copies it into the local variable on the stack of its caller. Because
> of the size of this structure, this ends up overflowing the limit for
> a single function's stack frame when prepare_kstatmount() gets inlined
> and both copies are on the same frame without the compiler being able
> to collapse them into one:
>
> fs/namespace.c:4995:1: error: stack frame size (1536) exceeds limit (1024) in '__se_sys_statmount' [-Werror,-Wframe-larger-than]
>   4995 | SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req,
>
> Replace the assignment with an in-place memset() plus assignment that
> should always be more efficient for both stack usage and runtime cost.

Cunning plan, to use the work efficient instead of inefficient, ;(

But, TBH, the libc integration seems complex but I also feel there's

no choice and this looks fine too.


>
> Fixes: 49889374ab92 ("statmount: simplify string option retrieval")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>   fs/namespace.c | 15 ++++++---------
>   1 file changed, 6 insertions(+), 9 deletions(-)
>
> diff --git a/fs/namespace.c b/fs/namespace.c
> index d036196f949c..159f1df379fc 100644
> --- a/fs/namespace.c
> +++ b/fs/namespace.c
> @@ -4957,15 +4957,12 @@ static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq,
>   	if (!access_ok(buf, bufsize))
>   		return -EFAULT;
>   
> -	*ks = (struct kstatmount){
> -		.mask		= kreq->param,
> -		.buf		= buf,
> -		.bufsize	= bufsize,
> -		.seq = {
> -			.size	= seq_size,
> -			.buf	= kvmalloc(seq_size, GFP_KERNEL_ACCOUNT),
> -		},
> -	};
> +	memset(ks, 0, sizeof(*ks));
> +	ks->mask = kreq->param;
> +	ks->buf = buf;
> +	ks->bufsize = bufsize;
> +	ks->seq.size = seq_size;
> +	ks->seq.buf = kvmalloc(seq_size, GFP_KERNEL_ACCOUNT);
>   	if (!ks->seq.buf)
>   		return -ENOMEM;
>   	return 0;

This looks much better than what it replaces IMHO.


Reviewed-by: Ian Kent <raven@themaw.net>


Ian


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-12-14  0:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-13  9:00 [PATCH] [v2] statmount: reduce runtime stack usage Arnd Bergmann
2023-12-13 10:15 ` Christian Brauner
2023-12-14  0:11 ` Ian Kent

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).