mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] mtd: rawnand: protect DMA engines from vmalloc'd buffers
@ 2026-11-23  5:50 ping.li
  2026-11-23  5:50 ` [PATCH 1/3] mtd: rawnand: fsmc: declare NAND_USES_DMA ping.li
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: ping.li @ 2026-11-23  5:50 UTC (permalink / raw)
  To: miquel.raynal, richard, vigneshr; +Cc: linux-mtd, linux-kernel, Ping Li

From: Ping Li <leeonion.muyu@gmail.com>

While discussing a proposal to also allocate the mtdchar read/write
bounce buffers with kvmalloc() [1], an audit showed that three raw
NAND controllers DMA the buffers handed by the NAND core without
declaring NAND_USES_DMA, so nand_base never bounces non-linear-map
buffers for them (see the use_bounce_buf logic in
nand_do_read_ops()/nand_do_write_ops()).

The exposure predates that proposal: since the MEMREAD/MEMWRITE
ioctls were converted to kvmalloc() (83208e106a8e8), a heavily
fragmented system can already feed a vmalloc'd buffer into their
dma_map_single() calls, which is not a valid input for
non-linear-map addresses.

fsmc and intel have no protection at all; declaring
NAND_USES_DMA lets nand_base bounce through chip->data_buf as
the other DMA-capable controllers (arasan, denali, meson, mtk,
nuvoton...) already do.  r852 has an internal bounce buffer but
only checks alignment; a virt_addr_valid() check extends it to
cover non-linear-map buffers too.

These fixes are independent of the direction taken in [1].

[1] https://lore.kernel.org/linux-mtd/20261119090055.1255028-1-ping.li@horizon.auto/

Ping Li (3):
  mtd: rawnand: fsmc: declare NAND_USES_DMA
  mtd: rawnand: intel: declare NAND_USES_DMA
  mtd: rawnand: r852: bounce non-linear-map buffers

 drivers/mtd/nand/raw/fsmc_nand.c             | 2 +-
 drivers/mtd/nand/raw/intel-nand-controller.c | 1 +
 drivers/mtd/nand/raw/r852.c                  | 4 ++--
 3 files changed, 4 insertions(+), 3 deletions(-)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.34.1


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

* [PATCH 1/3] mtd: rawnand: fsmc: declare NAND_USES_DMA
  2026-11-23  5:50 [PATCH 0/3] mtd: rawnand: protect DMA engines from vmalloc'd buffers ping.li
@ 2026-11-23  5:50 ` ping.li
  2026-11-23  5:50 ` [PATCH 2/3] mtd: rawnand: intel: " ping.li
  2026-11-23  5:50 ` [PATCH 3/3] mtd: rawnand: r852: bounce non-linear-map buffers ping.li
  2 siblings, 0 replies; 5+ messages in thread
From: ping.li @ 2026-11-23  5:50 UTC (permalink / raw)
  To: miquel.raynal, richard, vigneshr; +Cc: linux-mtd, linux-kernel, Ping Li

From: Ping Li <leeonion.muyu@gmail.com>

fsmc_exec_op() passes the data buffers provided by the NAND core
straight to dma_map_single() when the controller runs in DMA mode,
but the driver never declares NAND_USES_DMA.  Because of this,
nand_base cannot know the controller DMAs these buffers and never
bounces non-linear-map ones (see the use_bounce_buf logic in
nand_do_read_ops()/nand_do_write_ops()).

Since the mtdchar MEMREAD/MEMWRITE ioctls allocate their buffers
with kvmalloc(), a heavily fragmented system can already feed a
vmalloc'd buffer into fsmc's DMA engine, which is not a valid
dma_map_single() input.

Declare NAND_USES_DMA so that nand_base bounces non-linear-map
buffers through chip->data_buf, as the other DMA-capable
controllers (arasan, denali, meson, mtk, nuvoton...) already do.

Fixes: 83208e106a8e8 ("mtdchar: use kvmalloc() for potentially large allocations")
Signed-off-by: Ping Li <leeonion.muyu@gmail.com>
---
 drivers/mtd/nand/raw/fsmc_nand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/fsmc_nand.c b/drivers/mtd/nand/raw/fsmc_nand.c
index eee00c6f43e0..2c3da9242a14 100644
--- a/drivers/mtd/nand/raw/fsmc_nand.c
+++ b/drivers/mtd/nand/raw/fsmc_nand.c
@@ -871,7 +871,7 @@ static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
 	u32 val;
 	int ret;
 
-	nand->options = 0;
+	nand->options = NAND_USES_DMA;
 
 	if (!of_property_read_u32(np, "bank-width", &val)) {
 		if (val == 2) {

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
-- 
2.34.1


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

* [PATCH 2/3] mtd: rawnand: intel: declare NAND_USES_DMA
  2026-11-23  5:50 [PATCH 0/3] mtd: rawnand: protect DMA engines from vmalloc'd buffers ping.li
  2026-11-23  5:50 ` [PATCH 1/3] mtd: rawnand: fsmc: declare NAND_USES_DMA ping.li
@ 2026-11-23  5:50 ` ping.li
  2026-11-23  5:50 ` [PATCH 3/3] mtd: rawnand: r852: bounce non-linear-map buffers ping.li
  2 siblings, 0 replies; 5+ messages in thread
From: ping.li @ 2026-11-23  5:50 UTC (permalink / raw)
  To: miquel.raynal, richard, vigneshr; +Cc: linux-mtd, linux-kernel, Ping Li

From: Ping Li <leeonion.muyu@gmail.com>

ebu_nand_read_page_hwecc()/ebu_nand_write_page_hwecc() feed the
page buffers provided by the NAND core straight to the DMA
engine, but the driver never declares NAND_USES_DMA.  Because of
this, nand_base cannot know the controller DMAs these buffers and
never bounces non-linear-map ones (see the use_bounce_buf logic
in nand_do_read_ops()/nand_do_write_ops()).

Since the mtdchar MEMREAD/MEMWRITE ioctls allocate their buffers
with kvmalloc(), a heavily fragmented system can already feed a
vmalloc'd buffer into the controller's DMA engine, which is not a
valid dma_map_single() input.

Declare NAND_USES_DMA so that nand_base bounces non-linear-map
buffers through chip->data_buf, as the other DMA-capable
controllers already do.

Fixes: 83208e106a8e8 ("mtdchar: use kvmalloc() for potentially large allocations")
Signed-off-by: Ping Li <leeonion.muyu@gmail.com>
---
 drivers/mtd/nand/raw/intel-nand-controller.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/mtd/nand/raw/intel-nand-controller.c b/drivers/mtd/nand/raw/intel-nand-controller.c
index cfb4b231e067..1f493fb426d6 100644
--- a/drivers/mtd/nand/raw/intel-nand-controller.c
+++ b/drivers/mtd/nand/raw/intel-nand-controller.c
@@ -688,6 +688,7 @@ static int ebu_nand_probe(struct platform_device *pdev)
 	nand = &ebu_host->chip;
 	nand->controller = &ebu_host->controller;
 	nand->controller->ops = &ebu_nand_controller_ops;
+	nand->options |= NAND_USES_DMA;
 
 	/* Scan to find existence of the device */
 	ret = nand_scan(&ebu_host->chip, 1);
-- 
2.34.1


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

* [PATCH 3/3] mtd: rawnand: r852: bounce non-linear-map buffers
  2026-11-23  5:50 [PATCH 0/3] mtd: rawnand: protect DMA engines from vmalloc'd buffers ping.li
  2026-11-23  5:50 ` [PATCH 1/3] mtd: rawnand: fsmc: declare NAND_USES_DMA ping.li
  2026-11-23  5:50 ` [PATCH 2/3] mtd: rawnand: intel: " ping.li
@ 2026-11-23  5:50 ` ping.li
  2 siblings, 0 replies; 5+ messages in thread
From: ping.li @ 2026-11-23  5:50 UTC (permalink / raw)
  To: miquel.raynal, richard, vigneshr; +Cc: linux-mtd, linux-kernel, Ping Li

From: Ping Li <leeonion.muyu@gmail.com>

r852_do_dma() only bounces buffers that are not R852_DMA_LEN
aligned.  An aligned vmalloc'd buffer passes this check and gets
fed to dma_map_single(), which is only valid for linear-map
addresses.  Since the mtdchar MEMREAD/MEMWRITE ioctls allocate
their buffers with kvmalloc(), a heavily fragmented system can
already hand such a buffer to the reader.

Extend the bounce condition with a virt_addr_valid() check so
that non-linear-map buffers go through the existing internal
bounce buffer instead.

Fixes: 83208e106a8e8 ("mtdchar: use kvmalloc() for potentially large allocations")
Signed-off-by: Ping Li <leeonion.muyu@gmail.com>
---
 drivers/mtd/nand/raw/r852.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/nand/raw/r852.c b/drivers/mtd/nand/raw/r852.c
index 92c47d351c27..4dbfc053fb21 100644
--- a/drivers/mtd/nand/raw/r852.c
+++ b/drivers/mtd/nand/raw/r852.c
@@ -188,8 +188,8 @@ static void r852_do_dma(struct r852_device *dev, uint8_t *buf, int do_read)
 	  from device, for writes first fill the buffer  from memory*/
 	dev->dma_state = do_read ? DMA_INTERNAL : DMA_MEMORY;
 
-	/* if incoming buffer is not page aligned, we should do bounce */
-	if ((unsigned long)buf & (R852_DMA_LEN-1))
+	/* if incoming buffer is not usable for DMA, we should do bounce */
+	if (!virt_addr_valid(buf) || (unsigned long)buf & (R852_DMA_LEN - 1))
 		bounce = 1;
 
 	if (!bounce) {
-- 
2.34.1


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

* [PATCH 0/3] mtd: rawnand: protect DMA engines from vmalloc'd buffers
@ 2026-11-23  5:49 ping.li
  0 siblings, 0 replies; 5+ messages in thread
From: ping.li @ 2026-11-23  5:49 UTC (permalink / raw)
  To: miquel.raynal, richard, vigneshr; +Cc: linux-mtd, linux-kernel, Ping Li

From: Ping Li <leeonion.muyu@gmail.com>

While discussing a proposal to also allocate the mtdchar read/write
bounce buffers with kvmalloc() [1], an audit showed that three raw
NAND controllers DMA the buffers handed by the NAND core without
declaring NAND_USES_DMA, so nand_base never bounces non-linear-map
buffers for them (see the use_bounce_buf logic in
nand_do_read_ops()/nand_do_write_ops()).

The exposure predates that proposal: since the MEMREAD/MEMWRITE
ioctls were converted to kvmalloc() (83208e106a8e8), a heavily
fragmented system can already feed a vmalloc'd buffer into their
dma_map_single() calls, which is not a valid input for
non-linear-map addresses.

fsmc and intel have no protection at all; declaring
NAND_USES_DMA lets nand_base bounce through chip->data_buf as
the other DMA-capable controllers (arasan, denali, meson, mtk,
nuvoton...) already do.  r852 has an internal bounce buffer but
only checks alignment; a virt_addr_valid() check extends it to
cover non-linear-map buffers too.

These fixes are independent of the direction taken in [1].

[1] https://lore.kernel.org/linux-mtd/20261119090055.1255028-1-ping.li@horizon.auto/

Ping Li (3):
  mtd: rawnand: fsmc: declare NAND_USES_DMA
  mtd: rawnand: intel: declare NAND_USES_DMA
  mtd: rawnand: r852: bounce non-linear-map buffers

 drivers/mtd/nand/raw/fsmc_nand.c             | 2 +-
 drivers/mtd/nand/raw/intel-nand-controller.c | 1 +
 drivers/mtd/nand/raw/r852.c                  | 4 ++--
 3 files changed, 4 insertions(+), 3 deletions(-)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.34.1


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

end of thread, other threads:[~2026-11-23  5:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23  5:50 [PATCH 0/3] mtd: rawnand: protect DMA engines from vmalloc'd buffers ping.li
2026-11-23  5:50 ` [PATCH 1/3] mtd: rawnand: fsmc: declare NAND_USES_DMA ping.li
2026-11-23  5:50 ` [PATCH 2/3] mtd: rawnand: intel: " ping.li
2026-11-23  5:50 ` [PATCH 3/3] mtd: rawnand: r852: bounce non-linear-map buffers ping.li
  -- strict thread matches above, loose matches on Subject: below --
2026-11-23  5:49 [PATCH 0/3] mtd: rawnand: protect DMA engines from vmalloc'd buffers ping.li

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®