mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] fat: avoid stack overflow warning
@ 2026-05-15 20:44 Arnd Bergmann
  2026-05-21 11:41 ` Christian Brauner
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Arnd Bergmann @ 2026-05-15 20:44 UTC (permalink / raw)
  To: OGAWA Hirofumi
  Cc: Arnd Bergmann, Christian Brauner, Jan Kara, avivdaum, Adi Nata,
	Andrew Morton, linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

Building the fat kunit tests on with -fsanitize=alignment
reveals some rather excessive stack usage:

fs/fat/fat_test.c: In function 'fat_clus_to_blknr_test':
fs/fat/fat_test.c:33:1: error: the frame size of 4736 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
   33 | }
      | ^
fs/fat/fat_test.c: In function 'fat_get_blknr_offset_test':
fs/fat/fat_test.c:52:1: error: the frame size of 4800 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]

The problem is clearly related to the on-stack copy of a local
msdos_sb_info structure. Avoid this by making that copy
'static const' and changing the called functions to accept
a constant input.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 fs/fat/fat.h      | 4 ++--
 fs/fat/fat_test.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/fat/fat.h b/fs/fat/fat.h
index 5a58f0bf8ce8..52bced59abe4 100644
--- a/fs/fat/fat.h
+++ b/fs/fat/fat.h
@@ -247,13 +247,13 @@ static inline unsigned char fat_checksum(const __u8 *name)
 	return s;
 }
 
-static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
+static inline sector_t fat_clus_to_blknr(const struct msdos_sb_info *sbi, int clus)
 {
 	return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
 		+ sbi->data_start;
 }
 
-static inline void fat_get_blknr_offset(struct msdos_sb_info *sbi,
+static inline void fat_get_blknr_offset(const struct msdos_sb_info *sbi,
 				loff_t i_pos, sector_t *blknr, int *offset)
 {
 	*blknr = i_pos >> sbi->dir_per_block_bits;
diff --git a/fs/fat/fat_test.c b/fs/fat/fat_test.c
index 4eeed9dca549..9583ce66dca3 100644
--- a/fs/fat/fat_test.c
+++ b/fs/fat/fat_test.c
@@ -22,7 +22,7 @@ static void fat_checksum_test(struct kunit *test)
 
 static void fat_clus_to_blknr_test(struct kunit *test)
 {
-	struct msdos_sb_info sbi = {
+	static const struct msdos_sb_info sbi = {
 		.sec_per_clus = 4,
 		.data_start = 100,
 	};
@@ -34,7 +34,7 @@ static void fat_clus_to_blknr_test(struct kunit *test)
 
 static void fat_get_blknr_offset_test(struct kunit *test)
 {
-	struct msdos_sb_info sbi = {
+	static const struct msdos_sb_info sbi = {
 		.dir_per_block = 16,
 		.dir_per_block_bits = 4,
 	};
-- 
2.39.5


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

* Re: [PATCH] fat: avoid stack overflow warning
  2026-05-15 20:44 [PATCH] fat: avoid stack overflow warning Arnd Bergmann
@ 2026-05-21 11:41 ` Christian Brauner
  2026-05-21 12:03   ` Arnd Bergmann
  2026-05-21 12:36 ` David Laight
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Christian Brauner @ 2026-05-21 11:41 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: OGAWA Hirofumi, Arnd Bergmann, Jan Kara, avivdaum, Adi Nata,
	Andrew Morton, linux-kernel

On Fri, May 15, 2026 at 10:44:46PM +0200, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> Building the fat kunit tests on with -fsanitize=alignment
> reveals some rather excessive stack usage:
> 
> fs/fat/fat_test.c: In function 'fat_clus_to_blknr_test':
> fs/fat/fat_test.c:33:1: error: the frame size of 4736 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
>    33 | }
>       | ^
> fs/fat/fat_test.c: In function 'fat_get_blknr_offset_test':
> fs/fat/fat_test.c:52:1: error: the frame size of 4800 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
> 
> The problem is clearly related to the on-stack copy of a local
> msdos_sb_info structure. Avoid this by making that copy
> 'static const' and changing the called functions to accept
> a constant input.
> 
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---

What is this based on, Arnd? I'm getting errors applying this.

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

* Re: [PATCH] fat: avoid stack overflow warning
  2026-05-21 11:41 ` Christian Brauner
@ 2026-05-21 12:03   ` Arnd Bergmann
  0 siblings, 0 replies; 7+ messages in thread
From: Arnd Bergmann @ 2026-05-21 12:03 UTC (permalink / raw)
  To: Christian Brauner, Arnd Bergmann
  Cc: OGAWA Hirofumi, Jan Kara, avivdaum, Adi Nata, Andrew Morton,
	linux-kernel

On Thu, May 21, 2026, at 13:41, Christian Brauner wrote:
> On Fri, May 15, 2026 at 10:44:46PM +0200, Arnd Bergmann wrote:
>> From: Arnd Bergmann <arnd@arndb.de>
>> 
>> Building the fat kunit tests on with -fsanitize=alignment
>> reveals some rather excessive stack usage:
>> 
>> fs/fat/fat_test.c: In function 'fat_clus_to_blknr_test':
>> fs/fat/fat_test.c:33:1: error: the frame size of 4736 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
>>    33 | }
>>       | ^
>> fs/fat/fat_test.c: In function 'fat_get_blknr_offset_test':
>> fs/fat/fat_test.c:52:1: error: the frame size of 4800 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
>> 
>> The problem is clearly related to the on-stack copy of a local
>> msdos_sb_info structure. Avoid this by making that copy
>> 'static const' and changing the called functions to accept
>> a constant input.
>> 
>> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
>> ---
>
> What is this based on, Arnd? I'm getting errors applying this.

I had assumed this was an old bug, as it showed up right after
I changed toolchains and ran into similar problems elsewhere.

Looking again, I see now that this just came in through
aecd952e23ee ("kunit: fat: test cluster and directory
i_pos layout helpers"), which is in the mm-nonmm-unstable
branch in current linux-next.

Maybe Andrew can just fold my fixup into that patch.

      Arnd

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

* Re: [PATCH] fat: avoid stack overflow warning
  2026-05-15 20:44 [PATCH] fat: avoid stack overflow warning Arnd Bergmann
  2026-05-21 11:41 ` Christian Brauner
@ 2026-05-21 12:36 ` David Laight
  2026-05-21 14:20   ` Arnd Bergmann
  2026-06-03  6:16 ` OGAWA Hirofumi
  2026-06-03 10:06 ` David Laight
  3 siblings, 1 reply; 7+ messages in thread
From: David Laight @ 2026-05-21 12:36 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: OGAWA Hirofumi, Arnd Bergmann, Christian Brauner, Jan Kara,
	avivdaum, Adi Nata, Andrew Morton, linux-kernel

On Fri, 15 May 2026 22:44:46 +0200
Arnd Bergmann <arnd@kernel.org> wrote:

> From: Arnd Bergmann <arnd@arndb.de>
> 
> Building the fat kunit tests on with -fsanitize=alignment
> reveals some rather excessive stack usage:

What on earth is -fsanitize=alignment doing here?
(Or even what do it do in general!)

From a 'testing the kernel' point of view I don't think you'd ever
want to copy a structure that might be misaligned.
(Quite how it does that in the general case when the address of something
in the structure might get passed to passed to code that knows the address
by other means is anybodies guess.)

The most you might want for 'kernel hardening' is a software check that
run-time errors misaligned structures.

-- David


> 
> fs/fat/fat_test.c: In function 'fat_clus_to_blknr_test':
> fs/fat/fat_test.c:33:1: error: the frame size of 4736 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
>    33 | }
>       | ^
> fs/fat/fat_test.c: In function 'fat_get_blknr_offset_test':
> fs/fat/fat_test.c:52:1: error: the frame size of 4800 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
> 
> The problem is clearly related to the on-stack copy of a local
> msdos_sb_info structure. Avoid this by making that copy
> 'static const' and changing the called functions to accept
> a constant input.
> 
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  fs/fat/fat.h      | 4 ++--
>  fs/fat/fat_test.c | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/fat/fat.h b/fs/fat/fat.h
> index 5a58f0bf8ce8..52bced59abe4 100644
> --- a/fs/fat/fat.h
> +++ b/fs/fat/fat.h
> @@ -247,13 +247,13 @@ static inline unsigned char fat_checksum(const __u8 *name)
>  	return s;
>  }
>  
> -static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
> +static inline sector_t fat_clus_to_blknr(const struct msdos_sb_info *sbi, int clus)
>  {
>  	return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
>  		+ sbi->data_start;
>  }
>  
> -static inline void fat_get_blknr_offset(struct msdos_sb_info *sbi,
> +static inline void fat_get_blknr_offset(const struct msdos_sb_info *sbi,
>  				loff_t i_pos, sector_t *blknr, int *offset)
>  {
>  	*blknr = i_pos >> sbi->dir_per_block_bits;
> diff --git a/fs/fat/fat_test.c b/fs/fat/fat_test.c
> index 4eeed9dca549..9583ce66dca3 100644
> --- a/fs/fat/fat_test.c
> +++ b/fs/fat/fat_test.c
> @@ -22,7 +22,7 @@ static void fat_checksum_test(struct kunit *test)
>  
>  static void fat_clus_to_blknr_test(struct kunit *test)
>  {
> -	struct msdos_sb_info sbi = {
> +	static const struct msdos_sb_info sbi = {
>  		.sec_per_clus = 4,
>  		.data_start = 100,
>  	};
> @@ -34,7 +34,7 @@ static void fat_clus_to_blknr_test(struct kunit *test)
>  
>  static void fat_get_blknr_offset_test(struct kunit *test)
>  {
> -	struct msdos_sb_info sbi = {
> +	static const struct msdos_sb_info sbi = {
>  		.dir_per_block = 16,
>  		.dir_per_block_bits = 4,
>  	};


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

* Re: [PATCH] fat: avoid stack overflow warning
  2026-05-21 12:36 ` David Laight
@ 2026-05-21 14:20   ` Arnd Bergmann
  0 siblings, 0 replies; 7+ messages in thread
From: Arnd Bergmann @ 2026-05-21 14:20 UTC (permalink / raw)
  To: David Laight, Arnd Bergmann
  Cc: OGAWA Hirofumi, Christian Brauner, Jan Kara, avivdaum, Adi Nata,
	Andrew Morton, linux-kernel

On Thu, May 21, 2026, at 14:36, David Laight wrote:
> On Fri, 15 May 2026 22:44:46 +0200
> Arnd Bergmann <arnd@kernel.org> wrote:
>
>> From: Arnd Bergmann <arnd@arndb.de>
>> 
>> Building the fat kunit tests on with -fsanitize=alignment
>> reveals some rather excessive stack usage:
>
> What on earth is -fsanitize=alignment doing here?
> (Or even what do it do in general!)

The idea is that the compiler adds sanity checks on accesses
to variables that may be misaligned and print a runtime
warning if this actually happens, see lib/ubsan.c.

As I understand it, the increased stack usage is an
unintended side-effect here, which is the result of
skipping some of the default optimization steps or
being less aggressive about function inlining.

      Arnd

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

* Re: [PATCH] fat: avoid stack overflow warning
  2026-05-15 20:44 [PATCH] fat: avoid stack overflow warning Arnd Bergmann
  2026-05-21 11:41 ` Christian Brauner
  2026-05-21 12:36 ` David Laight
@ 2026-06-03  6:16 ` OGAWA Hirofumi
  2026-06-03 10:06 ` David Laight
  3 siblings, 0 replies; 7+ messages in thread
From: OGAWA Hirofumi @ 2026-06-03  6:16 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Arnd Bergmann, Arnd Bergmann, Christian Brauner, Jan Kara,
	avivdaum, Adi Nata, linux-kernel

Arnd Bergmann <arnd@kernel.org> writes:

> From: Arnd Bergmann <arnd@arndb.de>
>
> Building the fat kunit tests on with -fsanitize=alignment
> reveals some rather excessive stack usage:
>
> fs/fat/fat_test.c: In function 'fat_clus_to_blknr_test':
> fs/fat/fat_test.c:33:1: error: the frame size of 4736 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
>    33 | }
>       | ^
> fs/fat/fat_test.c: In function 'fat_get_blknr_offset_test':
> fs/fat/fat_test.c:52:1: error: the frame size of 4800 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
>
> The problem is clearly related to the on-stack copy of a local
> msdos_sb_info structure. Avoid this by making that copy
> 'static const' and changing the called functions to accept
> a constant input.

Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  fs/fat/fat.h      | 4 ++--
>  fs/fat/fat_test.c | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/fs/fat/fat.h b/fs/fat/fat.h
> index 5a58f0bf8ce8..52bced59abe4 100644
> --- a/fs/fat/fat.h
> +++ b/fs/fat/fat.h
> @@ -247,13 +247,13 @@ static inline unsigned char fat_checksum(const __u8 *name)
>  	return s;
>  }
>  
> -static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
> +static inline sector_t fat_clus_to_blknr(const struct msdos_sb_info *sbi, int clus)
>  {
>  	return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
>  		+ sbi->data_start;
>  }
>  
> -static inline void fat_get_blknr_offset(struct msdos_sb_info *sbi,
> +static inline void fat_get_blknr_offset(const struct msdos_sb_info *sbi,
>  				loff_t i_pos, sector_t *blknr, int *offset)
>  {
>  	*blknr = i_pos >> sbi->dir_per_block_bits;
> diff --git a/fs/fat/fat_test.c b/fs/fat/fat_test.c
> index 4eeed9dca549..9583ce66dca3 100644
> --- a/fs/fat/fat_test.c
> +++ b/fs/fat/fat_test.c
> @@ -22,7 +22,7 @@ static void fat_checksum_test(struct kunit *test)
>  
>  static void fat_clus_to_blknr_test(struct kunit *test)
>  {
> -	struct msdos_sb_info sbi = {
> +	static const struct msdos_sb_info sbi = {
>  		.sec_per_clus = 4,
>  		.data_start = 100,
>  	};
> @@ -34,7 +34,7 @@ static void fat_clus_to_blknr_test(struct kunit *test)
>  
>  static void fat_get_blknr_offset_test(struct kunit *test)
>  {
> -	struct msdos_sb_info sbi = {
> +	static const struct msdos_sb_info sbi = {
>  		.dir_per_block = 16,
>  		.dir_per_block_bits = 4,
>  	};

-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

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

* Re: [PATCH] fat: avoid stack overflow warning
  2026-05-15 20:44 [PATCH] fat: avoid stack overflow warning Arnd Bergmann
                   ` (2 preceding siblings ...)
  2026-06-03  6:16 ` OGAWA Hirofumi
@ 2026-06-03 10:06 ` David Laight
  3 siblings, 0 replies; 7+ messages in thread
From: David Laight @ 2026-06-03 10:06 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: OGAWA Hirofumi, Arnd Bergmann, Christian Brauner, Jan Kara,
	avivdaum, Adi Nata, Andrew Morton, linux-kernel

On Fri, 15 May 2026 22:44:46 +0200
Arnd Bergmann <arnd@kernel.org> wrote:

> From: Arnd Bergmann <arnd@arndb.de>
> 
> Building the fat kunit tests on with -fsanitize=alignment
> reveals some rather excessive stack usage:
> 
> fs/fat/fat_test.c: In function 'fat_clus_to_blknr_test':
> fs/fat/fat_test.c:33:1: error: the frame size of 4736 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
>    33 | }
>       | ^
> fs/fat/fat_test.c: In function 'fat_get_blknr_offset_test':
> fs/fat/fat_test.c:52:1: error: the frame size of 4800 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
> 
> The problem is clearly related to the on-stack copy of a local
> msdos_sb_info structure. Avoid this by making that copy
> 'static const' and changing the called functions to accept
> a constant input.

Just FYI:

Without -fsanitize=alignment fat_clus_to_blknr() is almost certainly
inlined (as expected) and then 'sbi' is completely optimised away.

-- David

> 
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  fs/fat/fat.h      | 4 ++--
>  fs/fat/fat_test.c | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/fat/fat.h b/fs/fat/fat.h
> index 5a58f0bf8ce8..52bced59abe4 100644
> --- a/fs/fat/fat.h
> +++ b/fs/fat/fat.h
> @@ -247,13 +247,13 @@ static inline unsigned char fat_checksum(const __u8 *name)
>  	return s;
>  }
>  
> -static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus)
> +static inline sector_t fat_clus_to_blknr(const struct msdos_sb_info *sbi, int clus)
>  {
>  	return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus
>  		+ sbi->data_start;
>  }
>  
> -static inline void fat_get_blknr_offset(struct msdos_sb_info *sbi,
> +static inline void fat_get_blknr_offset(const struct msdos_sb_info *sbi,
>  				loff_t i_pos, sector_t *blknr, int *offset)
>  {
>  	*blknr = i_pos >> sbi->dir_per_block_bits;
> diff --git a/fs/fat/fat_test.c b/fs/fat/fat_test.c
> index 4eeed9dca549..9583ce66dca3 100644
> --- a/fs/fat/fat_test.c
> +++ b/fs/fat/fat_test.c
> @@ -22,7 +22,7 @@ static void fat_checksum_test(struct kunit *test)
>  
>  static void fat_clus_to_blknr_test(struct kunit *test)
>  {
> -	struct msdos_sb_info sbi = {
> +	static const struct msdos_sb_info sbi = {
>  		.sec_per_clus = 4,
>  		.data_start = 100,
>  	};
> @@ -34,7 +34,7 @@ static void fat_clus_to_blknr_test(struct kunit *test)
>  
>  static void fat_get_blknr_offset_test(struct kunit *test)
>  {
> -	struct msdos_sb_info sbi = {
> +	static const struct msdos_sb_info sbi = {
>  		.dir_per_block = 16,
>  		.dir_per_block_bits = 4,
>  	};


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

end of thread, other threads:[~2026-06-03 10:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-15 20:44 [PATCH] fat: avoid stack overflow warning Arnd Bergmann
2026-05-21 11:41 ` Christian Brauner
2026-05-21 12:03   ` Arnd Bergmann
2026-05-21 12:36 ` David Laight
2026-05-21 14:20   ` Arnd Bergmann
2026-06-03  6:16 ` OGAWA Hirofumi
2026-06-03 10:06 ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome