mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Todd Brandt <todd.e.brandt@linux.intel.com>
To: Tudor Ambarus <tudor.ambarus@linaro.org>,
	Todd Brandt <todd.e.brandt@intel.com>,
	miquel.raynal@bootlin.com, linux-kernel@vger.kernel.org
Cc: pratyush@kernel.org,
	"linux-mtd@lists.infradead.org" <linux-mtd@lists.infradead.org>,
	Thorsten Leemhuis <regressions@leemhuis.info>
Subject: Re: [PATCH] MTD SPI-NOR: BUG FIX of divide by zero in new n_banks value
Date: Wed, 17 May 2023 08:03:23 -0700	[thread overview]
Message-ID: <38bf4bce7961c674b7ac798a8413cb1256eea8f4.camel@linux.intel.com> (raw)
In-Reply-To: <3ab922a4-255a-5aef-86ba-d906819f0f3a@linaro.org>

On Wed, 2023-05-17 at 08:10 +0100, Tudor Ambarus wrote:
> Hi, Todd,
> 
> On 5/16/23 23:51, Todd Brandt wrote:
> > 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 <todd.e.brandt@linux.intel.com>
> > Closes: https://bugzilla.kernel.org/show_bug.cgi?id=217448
> > Fixes: 9d6c5d64f028 ("mtd: spi-nor: Introduce the concept of bank")
> > Signed-off-by: Todd Brandt <todd.e.brandt@intel.com>
> > ---
> >  drivers/mtd/spi-nor/core.c | 5 ++++-
> >  1 file changed, 4 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-
> > nor/core.c
> > index 0bb0ad14a2fc..084117959be4 100644
> > --- a/drivers/mtd/spi-nor/core.c
> > +++ b/drivers/mtd/spi-nor/core.c
> > @@ -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;
> >  }
> >  
> >  /**
> 
> 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-
>info->n_banks);

> 
> diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
>                       index 0bb0ad14a2fc..37f750bd7bfb 100644
> --- a/drivers/mtd/spi-nor/core.cdrivers/mtd/spi-nor/core.h
> +++ b/drivers/mtd/spi-nor/core.cdrivers/mtd/spi-nor/core.c
> @@ -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.


      reply	other threads:[~2023-05-17 15:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-16 22:51 Todd Brandt
2023-05-17  7:10 ` Tudor Ambarus
2023-05-17 15:03   ` Todd Brandt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=38bf4bce7961c674b7ac798a8413cb1256eea8f4.camel@linux.intel.com \
    --to=todd.e.brandt@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=pratyush@kernel.org \
    --cc=regressions@leemhuis.info \
    --cc=todd.e.brandt@intel.com \
    --cc=tudor.ambarus@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®