Skip to contentSkip to conversation

MTD SPI-NOR: BUG FIX of divide by zero in new n_banks value

PATCH In review Todd Brandt 3 participants 3 messages May 16, 2023 – May 17, 2023

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

PATCH 1/1 MTD SPI-NOR: BUG FIX of divide by zero in new n_banks value

While testing 6.4-rc1 on our Lenovo Thinkpad X1 I discovered that freeze, suspend, and shutdown, all hang the system. I bisected it to an addition made to the MTD spi-nor code.

The new "n_banks" value is being divided into without a proper check.. Thus on certain systems this causes a divide by zero hang.

Reported-and-tested-by Todd Brandt <[email protected]>
Fixes 9d6c5d64f028 ("mtd: spi-nor: Introduce the concept of bank")
---
 drivers/mtd/spi-nor/core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/core.c +4 -1
index 0bb0ad14a2fc..084117959be4 100644
@@ -2921,7 +2921,10 @@ static void spi_nor_late_init_params(struct spi_nor *nor)
 	if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops)
 		spi_nor_init_default_locking_ops(nor);
 
-	nor->params->bank_size = div64_u64(nor->params->size, nor->info->n_banks);
+	if (nor->info->n_banks > 0)
+		nor->params->bank_size = div64_u64(nor->params->size, nor->info->n_banks);
+	else
+		nor->params->bank_size = 0;
 }
 
 /**
Signature · 1 line
2.17.1
#1 lore raw

Hi, Todd,

Quoted 21 lines · Todd Brandt

While testing 6.4-rc1 on our Lenovo Thinkpad X1 I discovered that freeze, suspend, and shutdown, all hang the system. I bisected it to an addition made to the MTD spi-nor code.

The new "n_banks" value is being divided into without a proper check.. Thus on certain systems this causes a divide by zero hang.

Reported-and-tested-by Todd Brandt <[email protected]>
Fixes 9d6c5d64f028 ("mtd: spi-nor: Introduce the concept of bank")
---
 drivers/mtd/spi-nor/core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/core.c +0 -0
index 0bb0ad14a2fc..084117959be4 100644
@@ -2921,7 +2921,10 @@ static void spi_nor_late_init_params(struct spi_nor *nor)
 	if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops)

drivers/mtd/spi-nor/core.c static void spi_nor_late_init_params(struct spi_nor *nor) · line 2930

Quoted 10 lines
drivers/mtd/spi-nor/core.c +4 -1
 		spi_nor_init_default_locking_ops(nor);
 
-	nor->params->bank_size = div64_u64(nor->params->size, nor->info->n_banks);
+	if (nor->info->n_banks > 0)
+		nor->params->bank_size = div64_u64(nor->params->size, nor->info->n_banks);
+	else
+		nor->params->bank_size = 0;
 }
 
 /**

Indeed, thanks for the report and patch. You probably use the spi-nor-generic driver, don't you?

Can you try the following change instead?

drivers/mtd/spi-nor/core.c +0 -0
                      index 0bb0ad14a2fc..37f750bd7bfb 100644
drivers/mtd/spi-nor/core.cdrivers/mtd/spi-nor/core.c renamed from drivers/mtd/spi-nor/core.cdrivers/mtd/spi-nor/core.h +0 -0
@@ -2018,6 +2018,7 @@ static const struct spi_nor_manufacturer

*manufacturers[] = {

 static const struct flash_info spi_nor_generic_flash = {
        .name = "spi-nor-generic",

+ .n_banks = 1,
/*
* JESD216 rev A doesn't specify the page size, therefore we need a
* sane default.

#2 lore raw
Quoted 38 lines · Tudor Ambarus

Hi, Todd,

Quoted 35 lines · Todd Brandt

While testing 6.4-rc1 on our Lenovo Thinkpad X1 I discovered that freeze, suspend, and shutdown, all hang the system. I bisected it to an addition made to the MTD spi-nor code.

The new "n_banks" value is being divided into without a proper check..
Thus on certain systems this causes a divide by zero hang.

Reported-and-tested-by Todd Brandt <[email protected]>
Fixes 9d6c5d64f028 ("mtd: spi-nor: Introduce the concept of bank")
---
 drivers/mtd/spi-nor/core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/mtd/spi- renamed from drivers/mtd/spi-nor/core.c +0 -0

nor/core.c
index 0bb0ad14a2fc..084117959be4 100644

drivers/mtd/spi-nor/core.c +4 -1
@@ -2921,7 +2921,10 @@ static void spi_nor_late_init_params(struct
 spi_nor *nor)
 	if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops)
 		spi_nor_init_default_locking_ops(nor);
 
-	nor->params->bank_size = div64_u64(nor->params->size, nor-
 >info->n_banks);
+	if (nor->info->n_banks > 0)
+		nor->params->bank_size = div64_u64(nor->params->size,
 nor->info->n_banks);
+	else
+		nor->params->bank_size = 0;

}

Quoted 6 lines
Quoted 1 line

/**

Indeed, thanks for the report and patch. You probably use the spi-nor-generic driver, don't you?

Can you try the following change instead?

Yes, this also seems to fix things on the Lenovo Thinkpad X1, but the solution should be this AND the "if (nor->info->n_banks > 0)" patch. It doesn't hurt to check even if it's a very unlikely border case. It guarantees that the issue won't occur again even if somehow a struct instance is created uninitialized.

You could probably just slim it down to a single if line instead of if/else since the default bank_size is zero by default:

+ if (nor->info->n_banks > 0)
+ nor->params->bank_size = div64_u64(nor->params->size, nor-

Quoted 15 lines

info->n_banks);

drivers/mtd/spi-nor/core.c +0 -0
                      index 0bb0ad14a2fc..37f750bd7bfb 100644
drivers/mtd/spi-nor/core.cdrivers/mtd/spi-nor/core.c renamed from drivers/mtd/spi-nor/core.cdrivers/mtd/spi-nor/core.h +1 -0
@@ -2018,6 +2018,7 @@ static const struct spi_nor_manufacturer
 *manufacturers[] = {
 
 static const struct flash_info spi_nor_generic_flash = {
        .name = "spi-nor-generic",
+       .n_banks = 1,
        /*
         * JESD216 rev A doesn't specify the page size, therefore we
Quoted 2 lines

need a
* sane default.

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/