* [PATCH v2 1/3] mtd: rawnand: cadence: Fix DMA device dereference when DMA is disabled
2026-11-24 9:24 [PATCH v2 0/3] mtd: rawnand: cadence: Fix incorrect variable dereference Miquel Raynal
@ 2026-11-24 9:24 ` Miquel Raynal
2026-11-24 9:24 ` [PATCH v2 2/3] mtd: rawnand: cadence: Resize the bounce buffer before the BBT scan Miquel Raynal
2026-11-24 9:24 ` [PATCH v2 3/3] mtd: rawnand: cadence: Switch to dmaengine_get_dma_device() Miquel Raynal
2 siblings, 0 replies; 4+ messages in thread
From: Miquel Raynal @ 2026-11-24 9:24 UTC (permalink / raw)
To: Richard Weinberger, Vignesh Raghavendra, Niravkumar L Rabara
Cc: Thomas Petazzoni, linux-mtd, linux-kernel, Miquel Raynal,
kernel test robot, Dan Carpenter, stable
Move the DMA resource mapping into the has_dma branch and only unmap the
resource when a DMA channel was actually acquired.
Fixes: 5c56bf214af8 ("mtd: rawnand: cadence: fix DMA device NULL pointer dereference")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/all/202603252339.fQzI6oc8-lkp@intel.com/
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/all/202609010758.3N8tYZdG-lkp@intel.com/
Cc: stable@vger.kernel.org
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
drivers/mtd/nand/raw/cadence-nand-controller.c | 23 ++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/drivers/mtd/nand/raw/cadence-nand-controller.c b/drivers/mtd/nand/raw/cadence-nand-controller.c
index e19ceb2d5dfc..42cc62b36457 100644
--- a/drivers/mtd/nand/raw/cadence-nand-controller.c
+++ b/drivers/mtd/nand/raw/cadence-nand-controller.c
@@ -3168,17 +3168,17 @@ static int cadence_nand_init(struct cdns_nand_ctrl *cdns_ctrl)
"%d: Failed to get a DMA channel\n", ret);
goto disable_irq;
}
- }
- dma_dev = cdns_ctrl->dmac->device;
- cdns_ctrl->io.iova_dma = dma_map_resource(dma_dev->dev, cdns_ctrl->io.dma,
- cdns_ctrl->io.size,
- DMA_BIDIRECTIONAL, 0);
+ dma_dev = cdns_ctrl->dmac->device;
+ cdns_ctrl->io.iova_dma = dma_map_resource(dma_dev->dev, cdns_ctrl->io.dma,
+ cdns_ctrl->io.size,
+ DMA_BIDIRECTIONAL, 0);
- ret = dma_mapping_error(dma_dev->dev, cdns_ctrl->io.iova_dma);
- if (ret) {
- dev_err(cdns_ctrl->dev, "Failed to map I/O resource to DMA\n");
- goto dma_release_chnl;
+ ret = dma_mapping_error(dma_dev->dev, cdns_ctrl->io.iova_dma);
+ if (ret) {
+ dev_err(cdns_ctrl->dev, "Failed to map I/O resource to DMA\n");
+ goto dma_release_chnl;
+ }
}
nand_controller_init(&cdns_ctrl->controller);
@@ -3204,8 +3204,9 @@ static int cadence_nand_init(struct cdns_nand_ctrl *cdns_ctrl)
return 0;
unmap_dma_resource:
- dma_unmap_resource(dma_dev->dev, cdns_ctrl->io.iova_dma,
- cdns_ctrl->io.size, DMA_BIDIRECTIONAL, 0);
+ if (cdns_ctrl->dmac)
+ dma_unmap_resource(dma_dev->dev, cdns_ctrl->io.iova_dma,
+ cdns_ctrl->io.size, DMA_BIDIRECTIONAL, 0);
dma_release_chnl:
if (cdns_ctrl->dmac)
--
2.56.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v2 2/3] mtd: rawnand: cadence: Resize the bounce buffer before the BBT scan
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
2026-11-24 9:24 ` [PATCH v2 3/3] mtd: rawnand: cadence: Switch to dmaengine_get_dma_device() Miquel Raynal
2 siblings, 0 replies; 4+ messages in thread
From: Miquel Raynal @ 2026-11-24 9:24 UTC (permalink / raw)
To: Richard Weinberger, Vignesh Raghavendra, Niravkumar L Rabara
Cc: Thomas Petazzoni, linux-mtd, linux-kernel, Miquel Raynal, stable
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
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v2 3/3] mtd: rawnand: cadence: Switch to dmaengine_get_dma_device()
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 ` [PATCH v2 2/3] mtd: rawnand: cadence: Resize the bounce buffer before the BBT scan Miquel Raynal
@ 2026-11-24 9:24 ` Miquel Raynal
2 siblings, 0 replies; 4+ messages in thread
From: Miquel Raynal @ 2026-11-24 9:24 UTC (permalink / raw)
To: Richard Weinberger, Vignesh Raghavendra, Niravkumar L Rabara
Cc: Thomas Petazzoni, linux-mtd, linux-kernel, Miquel Raynal
Use the dmaengine consumer accessor instead of open-coding it (as
already done in the remove path). Do it as well for the buffer mapping
done in cadence_nand_slave_dma_transfer() so that both ends of the
transfer are mapped against the same device, which matters for DMA
engines exposing a per-channel DMA device.
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
drivers/mtd/nand/raw/cadence-nand-controller.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/drivers/mtd/nand/raw/cadence-nand-controller.c b/drivers/mtd/nand/raw/cadence-nand-controller.c
index 912148f90ece..6f990469b8e5 100644
--- a/drivers/mtd/nand/raw/cadence-nand-controller.c
+++ b/drivers/mtd/nand/raw/cadence-nand-controller.c
@@ -1873,16 +1873,16 @@ static int cadence_nand_slave_dma_transfer(struct cdns_nand_ctrl *cdns_ctrl,
{
DECLARE_COMPLETION_ONSTACK(finished);
struct dma_chan *chan;
- struct dma_device *dma_dev;
dma_addr_t src_dma, dst_dma, buf_dma;
struct dma_async_tx_descriptor *tx;
+ struct device *dma_dev;
dma_cookie_t cookie;
chan = cdns_ctrl->dmac;
- dma_dev = chan->device;
+ dma_dev = dmaengine_get_dma_device(chan);
- buf_dma = dma_map_single(dma_dev->dev, buf, len, dir);
- if (dma_mapping_error(dma_dev->dev, buf_dma)) {
+ buf_dma = dma_map_single(dma_dev, buf, len, dir);
+ if (dma_mapping_error(dma_dev, buf_dma)) {
dev_err(cdns_ctrl->dev, "Failed to map DMA buffer\n");
goto err;
}
@@ -1914,12 +1914,12 @@ static int cadence_nand_slave_dma_transfer(struct cdns_nand_ctrl *cdns_ctrl,
dma_async_issue_pending(cdns_ctrl->dmac);
wait_for_completion(&finished);
- dma_unmap_single(dma_dev->dev, buf_dma, len, dir);
+ dma_unmap_single(dma_dev, buf_dma, len, dir);
return 0;
err_unmap:
- dma_unmap_single(dma_dev->dev, buf_dma, len, dir);
+ dma_unmap_single(dma_dev, buf_dma, len, dir);
err:
dev_dbg(cdns_ctrl->dev, "Fall back to CPU I/O\n");
@@ -3134,8 +3134,8 @@ cadence_nand_irq_cleanup(int irqnum, struct cdns_nand_ctrl *cdns_ctrl)
static int cadence_nand_init(struct cdns_nand_ctrl *cdns_ctrl)
{
+ struct device *dma_dev;
dma_cap_mask_t mask;
- struct dma_device *dma_dev;
int ret;
cdns_ctrl->cdma_desc = dma_alloc_coherent(cdns_ctrl->dev,
@@ -3181,12 +3181,12 @@ static int cadence_nand_init(struct cdns_nand_ctrl *cdns_ctrl)
goto disable_irq;
}
- dma_dev = cdns_ctrl->dmac->device;
- cdns_ctrl->io.iova_dma = dma_map_resource(dma_dev->dev, cdns_ctrl->io.dma,
+ dma_dev = dmaengine_get_dma_device(cdns_ctrl->dmac);
+ cdns_ctrl->io.iova_dma = dma_map_resource(dma_dev, cdns_ctrl->io.dma,
cdns_ctrl->io.size,
DMA_BIDIRECTIONAL, 0);
- ret = dma_mapping_error(dma_dev->dev, cdns_ctrl->io.iova_dma);
+ ret = dma_mapping_error(dma_dev, cdns_ctrl->io.iova_dma);
if (ret) {
dev_err(cdns_ctrl->dev, "Failed to map I/O resource to DMA\n");
goto dma_release_chnl;
@@ -3210,7 +3210,7 @@ static int cadence_nand_init(struct cdns_nand_ctrl *cdns_ctrl)
unmap_dma_resource:
if (cdns_ctrl->dmac)
- dma_unmap_resource(dma_dev->dev, cdns_ctrl->io.iova_dma,
+ dma_unmap_resource(dma_dev, cdns_ctrl->io.iova_dma,
cdns_ctrl->io.size, DMA_BIDIRECTIONAL, 0);
dma_release_chnl:
--
2.56.0
^ permalink raw reply [flat|nested] 4+ messages in thread