linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtd: spi-nor: avoid holes in struct spi_mem_op
@ 2023-07-19 19:00 Arnd Bergmann
  2023-07-19 19:01 ` Mark Brown
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Arnd Bergmann @ 2023-07-19 19:00 UTC (permalink / raw)
  To: Tudor Ambarus, Pratyush Yadav, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra
  Cc: Arnd Bergmann, Peter Foley, Pedro Falcato, Michael Walle,
	Mark Brown, Takahiro Kuwano, Dhruva Gole, linux-mtd,
	linux-kernel, linux-spi

From: Arnd Bergmann <arnd@arndb.de>

gcc gets confused when -ftrivial-auto-var-init=pattern is used on sparse
bit fields such as 'struct spi_mem_op', which caused the previous false
positive warning about an uninitialized variable:

drivers/mtd/spi-nor/spansion.c: error: 'op' is used uninitialized [-Werror=uninitialized]

In fact, the variable is fully initialized and gcc does not see it being
used, so the warning is entirely bogus. The problem appears to be
a misoptimization in the initialization of single bit fields when the
rest of the bytes are not initialized.

A previous workaround added another initialization, which ended up
shutting up the warning in spansion.c, though it apparently still happens
in other files as reported by Peter Foley in the gcc bugzilla. The
workaround of adding a fake initialization seems particularly bad
because it would set values that can never be correct but prevent the
compiler from warning about actually missing initializations.

Revert the broken workaround and instead pad the structure to only
have bitfields that add up to full bytes, which should avoid this
behavior in all drivers.

I also filed a new bug against gcc with what I found, so this can
hopefully be addressed in future gcc releases. At the moment, only
gcc-12 and gcc-13 are affected.

Cc: Peter Foley <pefoley2@pefoley.com>
Cc: Pedro Falcato <pedro.falcato@gmail.com>
Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110743
Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108402
Link: https://godbolt.org/z/efMMsG1Kx
Fixes: 420c4495b5e56 ("mtd: spi-nor: spansion: make sure local struct does not contain garbage")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 drivers/mtd/spi-nor/spansion.c | 4 ++--
 include/linux/spi/spi-mem.h    | 4 ++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/spi-nor/spansion.c b/drivers/mtd/spi-nor/spansion.c
index 6d6466a3436e6..8397cf3cc3306 100644
--- a/drivers/mtd/spi-nor/spansion.c
+++ b/drivers/mtd/spi-nor/spansion.c
@@ -361,7 +361,7 @@ static int cypress_nor_determine_addr_mode_by_sr1(struct spi_nor *nor,
  */
 static int cypress_nor_set_addr_mode_nbytes(struct spi_nor *nor)
 {
-	struct spi_mem_op op = {};
+	struct spi_mem_op op;
 	u8 addr_mode;
 	int ret;
 
@@ -492,7 +492,7 @@ s25fs256t_post_bfpt_fixup(struct spi_nor *nor,
 			  const struct sfdp_parameter_header *bfpt_header,
 			  const struct sfdp_bfpt *bfpt)
 {
-	struct spi_mem_op op = {};
+	struct spi_mem_op op;
 	int ret;
 
 	ret = cypress_nor_set_addr_mode_nbytes(nor);
diff --git a/include/linux/spi/spi-mem.h b/include/linux/spi/spi-mem.h
index 8e984d75f5b6c..6b0a7dc48a4b7 100644
--- a/include/linux/spi/spi-mem.h
+++ b/include/linux/spi/spi-mem.h
@@ -101,6 +101,7 @@ struct spi_mem_op {
 		u8 nbytes;
 		u8 buswidth;
 		u8 dtr : 1;
+		u8 __pad : 7;
 		u16 opcode;
 	} cmd;
 
@@ -108,6 +109,7 @@ struct spi_mem_op {
 		u8 nbytes;
 		u8 buswidth;
 		u8 dtr : 1;
+		u8 __pad : 7;
 		u64 val;
 	} addr;
 
@@ -115,12 +117,14 @@ struct spi_mem_op {
 		u8 nbytes;
 		u8 buswidth;
 		u8 dtr : 1;
+		u8 __pad : 7;
 	} dummy;
 
 	struct {
 		u8 buswidth;
 		u8 dtr : 1;
 		u8 ecc : 1;
+		u8 __pad : 6;
 		enum spi_mem_data_dir dir;
 		unsigned int nbytes;
 		union {
-- 
2.39.2


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

* Re: [PATCH] mtd: spi-nor: avoid holes in struct spi_mem_op
  2023-07-19 19:00 [PATCH] mtd: spi-nor: avoid holes in struct spi_mem_op Arnd Bergmann
@ 2023-07-19 19:01 ` Mark Brown
  2023-07-20  6:50 ` Tudor Ambarus
  2023-07-27 15:17 ` Miquel Raynal
  2 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2023-07-19 19:01 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Tudor Ambarus, Pratyush Yadav, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, Arnd Bergmann, Peter Foley, Pedro Falcato,
	Michael Walle, Takahiro Kuwano, Dhruva Gole, linux-mtd,
	linux-kernel, linux-spi

[-- Attachment #1: Type: text/plain, Size: 356 bytes --]

On Wed, Jul 19, 2023 at 09:00:25PM +0200, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> gcc gets confused when -ftrivial-auto-var-init=pattern is used on sparse
> bit fields such as 'struct spi_mem_op', which caused the previous false
> positive warning about an uninitialized variable:

Acked-by: Mark Brown <broonie@kernel.org>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH] mtd: spi-nor: avoid holes in struct spi_mem_op
  2023-07-19 19:00 [PATCH] mtd: spi-nor: avoid holes in struct spi_mem_op Arnd Bergmann
  2023-07-19 19:01 ` Mark Brown
@ 2023-07-20  6:50 ` Tudor Ambarus
  2023-07-20  8:48   ` Miquel Raynal
  2023-07-27 15:17 ` Miquel Raynal
  2 siblings, 1 reply; 5+ messages in thread
From: Tudor Ambarus @ 2023-07-20  6:50 UTC (permalink / raw)
  To: Arnd Bergmann, Pratyush Yadav, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra
  Cc: Arnd Bergmann, Peter Foley, Pedro Falcato, Michael Walle,
	Mark Brown, Takahiro Kuwano, Dhruva Gole, linux-mtd,
	linux-kernel, linux-spi



On 7/19/23 20:00, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> gcc gets confused when -ftrivial-auto-var-init=pattern is used on sparse
> bit fields such as 'struct spi_mem_op', which caused the previous false
> positive warning about an uninitialized variable:
> 
> drivers/mtd/spi-nor/spansion.c: error: 'op' is used uninitialized [-Werror=uninitialized]
> 
> In fact, the variable is fully initialized and gcc does not see it being
> used, so the warning is entirely bogus. The problem appears to be
> a misoptimization in the initialization of single bit fields when the
> rest of the bytes are not initialized.
> 
> A previous workaround added another initialization, which ended up
> shutting up the warning in spansion.c, though it apparently still happens
> in other files as reported by Peter Foley in the gcc bugzilla. The
> workaround of adding a fake initialization seems particularly bad
> because it would set values that can never be correct but prevent the
> compiler from warning about actually missing initializations.
> 
> Revert the broken workaround and instead pad the structure to only
> have bitfields that add up to full bytes, which should avoid this
> behavior in all drivers.
> 
> I also filed a new bug against gcc with what I found, so this can
> hopefully be addressed in future gcc releases. At the moment, only
> gcc-12 and gcc-13 are affected.
> 
> Cc: Peter Foley <pefoley2@pefoley.com>
> Cc: Pedro Falcato <pedro.falcato@gmail.com>
> Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110743
> Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108402
> Link: https://godbolt.org/z/efMMsG1Kx
> Fixes: 420c4495b5e56 ("mtd: spi-nor: spansion: make sure local struct does not contain garbage")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>

Acked-by: Tudor Ambarus <tudor.ambarus@linaro.org>

Miquel, would you please take this through mtd/fixes?

Cheers,
ta

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

* Re: [PATCH] mtd: spi-nor: avoid holes in struct spi_mem_op
  2023-07-20  6:50 ` Tudor Ambarus
@ 2023-07-20  8:48   ` Miquel Raynal
  0 siblings, 0 replies; 5+ messages in thread
From: Miquel Raynal @ 2023-07-20  8:48 UTC (permalink / raw)
  To: Tudor Ambarus
  Cc: Arnd Bergmann, Pratyush Yadav, Richard Weinberger,
	Vignesh Raghavendra, Arnd Bergmann, Peter Foley, Pedro Falcato,
	Michael Walle, Mark Brown, Takahiro Kuwano, Dhruva Gole,
	linux-mtd, linux-kernel, linux-spi

Hi Tudor,

tudor.ambarus@linaro.org wrote on Thu, 20 Jul 2023 07:50:33 +0100:

> On 7/19/23 20:00, Arnd Bergmann wrote:
> > From: Arnd Bergmann <arnd@arndb.de>
> > 
> > gcc gets confused when -ftrivial-auto-var-init=pattern is used on sparse
> > bit fields such as 'struct spi_mem_op', which caused the previous false
> > positive warning about an uninitialized variable:
> > 
> > drivers/mtd/spi-nor/spansion.c: error: 'op' is used uninitialized [-Werror=uninitialized]
> > 
> > In fact, the variable is fully initialized and gcc does not see it being
> > used, so the warning is entirely bogus. The problem appears to be
> > a misoptimization in the initialization of single bit fields when the
> > rest of the bytes are not initialized.
> > 
> > A previous workaround added another initialization, which ended up
> > shutting up the warning in spansion.c, though it apparently still happens
> > in other files as reported by Peter Foley in the gcc bugzilla. The
> > workaround of adding a fake initialization seems particularly bad
> > because it would set values that can never be correct but prevent the
> > compiler from warning about actually missing initializations.
> > 
> > Revert the broken workaround and instead pad the structure to only
> > have bitfields that add up to full bytes, which should avoid this
> > behavior in all drivers.
> > 
> > I also filed a new bug against gcc with what I found, so this can
> > hopefully be addressed in future gcc releases. At the moment, only
> > gcc-12 and gcc-13 are affected.
> > 
> > Cc: Peter Foley <pefoley2@pefoley.com>
> > Cc: Pedro Falcato <pedro.falcato@gmail.com>
> > Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110743
> > Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108402
> > Link: https://godbolt.org/z/efMMsG1Kx
> > Fixes: 420c4495b5e56 ("mtd: spi-nor: spansion: make sure local struct does not contain garbage")
> > Signed-off-by: Arnd Bergmann <arnd@arndb.de>  
> 
> Acked-by: Tudor Ambarus <tudor.ambarus@linaro.org>
> 
> Miquel, would you please take this through mtd/fixes?

I will!

Thanks,
Miquèl

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

* Re: [PATCH] mtd: spi-nor: avoid holes in struct spi_mem_op
  2023-07-19 19:00 [PATCH] mtd: spi-nor: avoid holes in struct spi_mem_op Arnd Bergmann
  2023-07-19 19:01 ` Mark Brown
  2023-07-20  6:50 ` Tudor Ambarus
@ 2023-07-27 15:17 ` Miquel Raynal
  2 siblings, 0 replies; 5+ messages in thread
From: Miquel Raynal @ 2023-07-27 15:17 UTC (permalink / raw)
  To: Arnd Bergmann, Tudor Ambarus, Pratyush Yadav, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra
  Cc: Arnd Bergmann, Peter Foley, Pedro Falcato, Michael Walle,
	Mark Brown, Takahiro Kuwano, Dhruva Gole, linux-mtd,
	linux-kernel, linux-spi

On Wed, 2023-07-19 at 19:00:25 UTC, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> gcc gets confused when -ftrivial-auto-var-init=pattern is used on sparse
> bit fields such as 'struct spi_mem_op', which caused the previous false
> positive warning about an uninitialized variable:
> 
> drivers/mtd/spi-nor/spansion.c: error: 'op' is used uninitialized [-Werror=uninitialized]
> 
> In fact, the variable is fully initialized and gcc does not see it being
> used, so the warning is entirely bogus. The problem appears to be
> a misoptimization in the initialization of single bit fields when the
> rest of the bytes are not initialized.
> 
> A previous workaround added another initialization, which ended up
> shutting up the warning in spansion.c, though it apparently still happens
> in other files as reported by Peter Foley in the gcc bugzilla. The
> workaround of adding a fake initialization seems particularly bad
> because it would set values that can never be correct but prevent the
> compiler from warning about actually missing initializations.
> 
> Revert the broken workaround and instead pad the structure to only
> have bitfields that add up to full bytes, which should avoid this
> behavior in all drivers.
> 
> I also filed a new bug against gcc with what I found, so this can
> hopefully be addressed in future gcc releases. At the moment, only
> gcc-12 and gcc-13 are affected.
> 
> Cc: Peter Foley <pefoley2@pefoley.com>
> Cc: Pedro Falcato <pedro.falcato@gmail.com>
> Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110743
> Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108402
> Link: https://godbolt.org/z/efMMsG1Kx
> Fixes: 420c4495b5e56 ("mtd: spi-nor: spansion: make sure local struct does not contain garbage")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> Acked-by: Mark Brown <broonie@kernel.org>
> Acked-by: Tudor Ambarus <tudor.ambarus@linaro.org>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/fixes, thanks.

Miquel

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

end of thread, other threads:[~2023-07-27 15:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-19 19:00 [PATCH] mtd: spi-nor: avoid holes in struct spi_mem_op Arnd Bergmann
2023-07-19 19:01 ` Mark Brown
2023-07-20  6:50 ` Tudor Ambarus
2023-07-20  8:48   ` Miquel Raynal
2023-07-27 15:17 ` Miquel Raynal

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