mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: Richard Weinberger <richard@nod.at>,
	 Vignesh Raghavendra <vigneshr@ti.com>,
	 Niravkumar L Rabara <niravkumarlaxmidas.rabara@altera.com>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	 linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
	 Miquel Raynal <miquel.raynal@bootlin.com>,
	stable@vger.kernel.org
Subject: [PATCH v2 2/3] mtd: rawnand: cadence: Resize the bounce buffer before the BBT scan
Date: Tue, 24 Nov 2026 10:24:23 +0100	[thread overview]
Message-ID: <20261124-perso-upstreaming-rawnand-fixes-v2-2-69db17ad07bd@bootlin.com> (raw)
In-Reply-To: <20261124-perso-upstreaming-rawnand-fixes-v2-0-69db17ad07bd@bootlin.com>

The bounce buffer is allocated with a default size and only resized to
the actual page + OOB size once all the chips have been
scanned. However, the bad block table is scanned during nand_scan(),
before that resize happens, using the same bounce buffer. Chips with a
page + OOB size larger than the default size may therefore overflow the
heap buffer.

In practice, this default is safe since it is set to 16kiB and I
currently know no chip with a bigger page size, but chances are that
this is not a future-proof bet, so let's resize the bounce buffer
whenever we attach a chip whose geometry needs a bigger buffer.

Fixes: ec4ba01e894d ("mtd: rawnand: Add new Cadence NAND driver to MTD subsystem")
Cc: stable@vger.kernel.org
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/cadence-nand-controller.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/drivers/mtd/nand/raw/cadence-nand-controller.c b/drivers/mtd/nand/raw/cadence-nand-controller.c
index 42cc62b36457..912148f90ece 100644
--- a/drivers/mtd/nand/raw/cadence-nand-controller.c
+++ b/drivers/mtd/nand/raw/cadence-nand-controller.c
@@ -2970,8 +2970,17 @@ static int cadence_nand_attach_chip(struct nand_chip *chip)
 	chip->ecc.read_oob_raw = cadence_nand_read_oob_raw;
 	chip->ecc.write_oob_raw = cadence_nand_write_oob_raw;
 
-	if ((mtd->writesize + mtd->oobsize) > cdns_ctrl->buf_size)
+	if ((mtd->writesize + mtd->oobsize) > cdns_ctrl->buf_size) {
+		u8 *buf;
+
+		buf = krealloc(cdns_ctrl->buf, mtd->writesize + mtd->oobsize,
+			       GFP_KERNEL);
+		if (!buf)
+			return -ENOMEM;
+
+		cdns_ctrl->buf = buf;
 		cdns_ctrl->buf_size = mtd->writesize + mtd->oobsize;
+	}
 
 	/* Is 32-bit DMA supported? */
 	ret = dma_set_mask(cdns_ctrl->dev, DMA_BIT_MASK(32));
@@ -3136,8 +3145,11 @@ static int cadence_nand_init(struct cdns_nand_ctrl *cdns_ctrl)
 	if (!cdns_ctrl->cdma_desc)
 		return -ENOMEM;
 
-	cdns_ctrl->buf_size = SZ_16K;
-	cdns_ctrl->buf = kmalloc(cdns_ctrl->buf_size, GFP_KERNEL);
+	/*
+	 * Allocate a scratch buffer for the identification phase. It gets
+	 * resized to the actual chip geometry in ->attach_chip().
+	 */
+	cdns_ctrl->buf = kmalloc(SZ_16K, GFP_KERNEL);
 	if (!cdns_ctrl->buf) {
 		ret = -ENOMEM;
 		goto free_buf_desc;
@@ -3194,13 +3206,6 @@ static int cadence_nand_init(struct cdns_nand_ctrl *cdns_ctrl)
 		goto unmap_dma_resource;
 	}
 
-	kfree(cdns_ctrl->buf);
-	cdns_ctrl->buf = kzalloc(cdns_ctrl->buf_size, GFP_KERNEL);
-	if (!cdns_ctrl->buf) {
-		ret = -ENOMEM;
-		goto unmap_dma_resource;
-	}
-
 	return 0;
 
 unmap_dma_resource:

-- 
2.56.0


  parent reply	other threads:[~2026-11-24  9:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  9:24 [PATCH v2 0/3] mtd: rawnand: cadence: Fix incorrect variable dereference Miquel Raynal
2026-11-24  9:24 ` [PATCH v2 1/3] mtd: rawnand: cadence: Fix DMA device dereference when DMA is disabled Miquel Raynal
2026-11-24  9:24 ` Miquel Raynal [this message]
2026-11-24  9:24 ` [PATCH v2 3/3] mtd: rawnand: cadence: Switch to dmaengine_get_dma_device() Miquel Raynal

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=20261124-perso-upstreaming-rawnand-fixes-v2-2-69db17ad07bd@bootlin.com \
    --to=miquel.raynal@bootlin.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=niravkumarlaxmidas.rabara@altera.com \
    --cc=richard@nod.at \
    --cc=stable@vger.kernel.org \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=vigneshr@ti.com \
    /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®