mtd: rawnand: protect DMA engines from vmalloc'd buffers
cover mtd: rawnand: protect DMA engines from vmalloc'd buffers
From: Ping Li <[email protected]>
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/[email protected]/
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
PATCH 1/3 mtd: rawnand: fsmc: declare NAND_USES_DMA
From: Ping Li <[email protected]>
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.
--- drivers/mtd/nand/raw/fsmc_nand.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/nand/raw/fsmc_nand.c +1 -1
@@ -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
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 1/3] mtd: rawnand: fsmc: declare NAND_USES_DMA
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mtd: rawnand: fsmc: unconditional NAND_USES_DMA on PIO-only path - [Low] mtd: rawnand: fsmc…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mtd: rawnand: fsmc: unconditional NAND_USES_DMA on PIO-only path
- [Low] mtd: rawnand: fsmc: inaccurate Fixes trailer citing mtdchar commit
--
commit d8b98a2d7f37ac877e92b27587661c944ec57d7b
Author: Ping Li <[email protected]>
Quoted 14 lines
mtd: rawnand: fsmc: declare
NAND_USES_DMA
fsmc_exec_op()passes the data buffers provided by the NAND core straight todma_map_single()when the controller runs in DMA mode, but the driver never declaresNAND_USES_DMA. Because of this,nand_basecannot know the controller DMAs these buffers and never bounces non-linear-map ones (see theuse_bounce_buflogic 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 validdma_map_single()input.
Quoted 6 lines
Declare
NAND_USES_DMAso thatnand_basebounces non-linear-map buffers throughchip->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 <[email protected]>
[Severity: Low]
Does the Fixes: tag point to the right commit? 83208e106a8e ("mtdchar:
use kvmalloc() for potentially large allocations") only changed mtdchar_write_ioctl(), whereas upper MTD layers such as UBIFS and JFFS2 passed vmalloc buffers long before that; DMA support in this driver came from 4774fb0a48aa ("mtd: nand/fsmc: Add DMA support") and bounce buffer support in nand_base came from 66507c7bc889 ("mtd: nand: Add support to use nand_base poi databuf as bounce buffer").
drivers/mtd/nand/raw/fsmc_nand.c static int fsmc_nand_probe_config_dt(struct platform_device *pdev, · line 874
Quoted 10 lines
drivers/mtd/nand/raw/fsmc_nand.c +1
-1@@ -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 ;
[Severity: Medium]
Should nand->options set NAND_USES_DMA unconditionally when the driver never runs in DMA mode?
In fsmc_nand_probe(), host is zero-allocated via devm_kzalloc_obj(), leaving host->mode at 0, and ever since a1b1e1d5bdfe ("mtd: nand: fsmc:
finally remove fsmc_nand_platform_data") nothing in the driver ever sets host->mode to USE_DMA_ACCESS (1). As a result, fsmc_exec_op() always takes the MMIO PIO path (fsmc_read_buf() and fsmc_write_buf()), and setting NAND_USES_DMA forces nand_do_read_ops() and nand_do_write_ops() to bounce every vmalloc or kvmalloc buffer through chip->data_buf via an unnecessary full-page copy.
Signature · 5 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
Re: [PATCH 1/3] mtd: rawnand: fsmc: declare NAND_USES_DMA
From: [email protected]
Hi,
Thanks for the review. I verified the three findings and they all check out:
- fsmc: indeed, nothing has set host->mode since the platform data removal (a1b1e1d5bdfe), so the DMA paths are dead code and fsmc_exec_op() always takes the PIO branch. I am dropping this patch; declaring NAND_USES_DMA there would only add an unnecessary full-page bounce for every vmalloc'd buffer.
- intel: the unmap problems in ebu_dma_start() are real, thanks for catching them. I will include a fix in v2: unmap on the success path as well, return directly when the mapping itself fails, and unmap with the device the buffer was mapped with.
- The Fixes: tags were indeed wrong: mtdblock's vmalloc'ed write cache and UBIFS LPT buffers reach mtd_write() long before the mtdchar ioctls did, so the exposure goes back to each driver's introduction. v2 will point the tags at 0b1039f016e8a (intel) and 67e054e919248 (r852).
I will send a v2 with the fsmc patch dropped, the Fixes: tags corrected and the ebu_dma_start() fix added.
Thanks,
Ping
PATCH 2/3 mtd: rawnand: intel: declare NAND_USES_DMA
In reply to ping.liFrom: Ping Li <[email protected]>
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.
--- drivers/mtd/nand/raw/intel-nand-controller.c | 1 + 1 file changed, 1 insertion(+)
drivers/mtd/nand/raw/intel-nand-controller.c +1 -0
@@ -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 );
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 2/3] mtd: rawnand: intel: declare NAND_USES_DMA
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mtd: rawnand: intel: Fixes tag cites mtdchar instead of driver commit It also noticed 1 related…
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mtd: rawnand: intel: Fixes tag cites mtdchar instead of driver commit
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Critical] mtd: rawnand: intel: DMA mapping leak and invalid unmap in ebu_dma_start() [1]
--
commit 414432fa9d307fa5e3ae8ecf35d7868fc7b41467
Author: Ping Li <[email protected]>
Quoted 14 lines
mtd: rawnand: intel: declare
NAND_USES_DMAebu_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_basecannot know the controller DMAs these buffers and never bounces non-linear-map ones (see theuse_bounce_buflogic 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 validdma_map_single()input.
Quoted 6 lines
Declare
NAND_USES_DMAso thatnand_basebounces non-linear-map buffers throughchip->data_buf, as the other DMA-capable controllers already do.Fixes 83208e106a8e8 ("mtdchar: use kvmalloc() for potentially large allocations")Signed-off-by Ping Li <[email protected]>
[Severity: Low]
Should the Fixes: tag point to 0b1039f016e8 ("mtd: rawnand: Add NAND controller support on Intel LGM SoC") instead? Upper MTD layers such as UBI, JFFS2, and mtdblock already pass vmalloc() buffers to mtd_read() and mtd_write(), so the missing NAND_USES_DMA flag has been an issue since the driver was introduced.
drivers/mtd/nand/raw/intel-nand-controller.c static int ebu_nand_probe(struct platform_device *pdev) · line 694
Quoted 12 lines
drivers/mtd/nand/raw/intel-nand-controller.c +1
-0@@ -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 );
[Severity: Critical]
This problem wasn't introduced by this patch, but does ebu_dma_start() leak buf_dma on the success path and unmap the wrong device on error [1]?
drivers/mtd/nand/raw/intel-nand-controller.c:ebu_dma_start() {
...
dma_dev = dmaengine_get_dma_device(chan);
buf_dma = dma_map_single(dma_dev, (void *)buf, len, dir);
if (dma_mapping_error(dma_dev, buf_dma)) {
dev_err(ebu_host->dev, "Failed to map DMA buffer\n");
ret = -EIO;
goto err_unmap;
}
...
return 0;err_unmap:
dma_unmap_single(ebu_host->dev, buf_dma, len, dir); return ret; }
On the success path, buf_dma is never unmapped after the transfer completes, leaking IOMMU mappings and skipping cache invalidations on non-coherent architectures. Also, when dma_map_single() fails, jumping to err_unmap unmaps an invalid DMA address, and err_unmap passes ebu_host->dev instead of dma_dev.
[1] https://sashiko.dev/#/bug/linux-f7fd8979-ca31-481c-b79a-253f8225990b
Signature · 5 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
PATCH 3/3 mtd: rawnand: r852: bounce non-linear-map buffers
In reply to ping.liFrom: Ping Li <[email protected]>
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.
--- drivers/mtd/nand/raw/r852.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/nand/raw/r852.c +2 -2
@@ -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) {
Signature · 1 line
2.34.1
v2 cover mtd: rawnand: protect DMA engines from vmalloc'd buffers
In reply to ping.liWhile discussing a proposal to also allocate the mtdchar read/write bounce buffers with kvmalloc() [1], an audit showed that raw NAND controllers may 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()).
Non-linear-map buffers are not hypothetical: mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers to mtd_write().
The first revision of this series covered three controllers; the v2 changes (following the AI review [2]) are:
- Drop the fsmc patch: nothing has set host->mode since a1b1e1d5bdfe ("mtd: nand: fsmc: finally remove fsmc_nand_platform_data"), so the DMA paths are dead code and fsmc_exec_op() always takes the PIO branch; declaring NAND_USES_DMA would only add an unnecessary bounce.
- Fix the Fixes: tags: the exposure goes back to each driver's introduction, not to the 2022 mtdchar kvmalloc conversion.
- Add a fix for the ebu_dma_start() unmap problems found by the same review.
[1] https://lore.kernel.org/linux-mtd/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Ping Li (3):
mtd: rawnand: intel: declare NAND_USES_DMA
mtd: rawnand: intel: unmap the DMA buffer in ebu_dma_start()
mtd: rawnand: r852: bounce non-linear-map buffers
drivers/mtd/nand/raw/intel-nand-controller.c | 8 ++++---- drivers/mtd/nand/raw/r852.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.34.1
PATCH v2 1/3 mtd: rawnand: intel: declare NAND_USES_DMA
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()).
Non-linear-map buffers are not hypothetical here: mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers, so such buffers can reach the controller's DMA engine, for which they are 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.
--- drivers/mtd/nand/raw/intel-nand-controller.c | 1 + 1 file changed, 1 insertion(+)
drivers/mtd/nand/raw/intel-nand-controller.c +1 -0
@@ -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 );
Signature · 1 line
2.34.1
PATCH v2 2/3 mtd: rawnand: intel: unmap the DMA buffer in ebu_dma_start()
In reply to Ping Liebu_dma_start() maps the data buffer for every page transfer but only unmaps it on the error path. Successful transfers thus leak the mapping, which accumulates IOVA space on IOMMU systems and skips any cache maintenance the unmap would perform on non-coherent platforms. The error path has two more problems:
when the mapping itself fails it jumps to the unmap with an invalid dma address, and the unmap is performed with ebu_host->dev instead of the dma_dev device the buffer was mapped with.
Return directly when the mapping fails, and unmap with dma_dev on both the success and the error paths.
--- drivers/mtd/nand/raw/intel-nand-controller.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/mtd/nand/raw/intel-nand-controller.c +3 -4
@@ -312,8 +312,7 @@ static int ebu_dma_start(struct ebu_nand_controller *ebu_host, u32 dir, buf_dma =dma_map_single (dma_dev, (void *)buf, len, dir);if (dma_mapping_error (dma_dev, buf_dma)) {dev_err (ebu_host->dev,"Failed to map DMA buffer\n" ); - ret = -EIO ; - goto err_unmap; +return -EIO ; } tx =dmaengine_prep_slave_single (chan, buf_dma, len, dir, flags);
@@ -346,10 +345,10 @@ static int ebu_dma_start(struct ebu_nand_controller *ebu_host, u32 dir, goto err_unmap; } -return 0 ; + ret =0 ; err_unmap: -dma_unmap_single (ebu_host->dev, buf_dma, len, dir); +dma_unmap_single (dma_dev, buf_dma, len, dir);return ret; }
Signature · 1 line
2.34.1
PATCH v2 3/3 mtd: rawnand: r852: bounce non-linear-map buffers
In reply to Ping Lir852_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. Such buffers are not hypothetical: mtdblock's write cache is vmalloc'ed, and UBIFS passes vmalloc'ed LPT buffers.
Extend the bounce condition with a virt_addr_valid() check so that non-linear-map buffers go through the existing internal bounce buffer instead.
--- drivers/mtd/nand/raw/r852.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/nand/raw/r852.c +2 -2
@@ -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) {
Signature · 1 line
2.34.1
Miquel Raynal
maintainerRe: [PATCH v2 3/3] mtd: rawnand: r852: bounce non-linear-map buffers
Quoted 21 lines · Ping Li
r852_do_dma()only bounces buffers that are notR852_DMA_LENaligned. An aligned vmalloc'd buffer passes this check and gets fed todma_map_single(), which is only valid for linear-map addresses. Such buffers are not hypothetical: mtdblock's write cache is vmalloc'ed, and UBIFS passes vmalloc'ed LPT buffers.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 67e054e919248 ("mtd: nand: Add driver for Ricoh xD/SmartMedia reader")Signed-off-by Ping Li <[email protected]>--- drivers/mtd/nand/raw/r852.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/mtd/nand/raw/r852.c +0
-0
drivers/mtd/nand/raw/r852.c static void r852_do_dma(struct r852_device *dev, uint8_t *buf, int do_read) · line 193
Quoted 8 lines
drivers/mtd/nand/raw/r852.c +2
-3@@ -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))
Why do we need a local bounce if the core can do it with NAND_USES_DMA?
Also please add Cc: stable to these three patches.
Thanks,
Miquèl
Re: [PATCH v2 3/3] mtd: rawnand: r852: bounce non-linear-map buffers
From: [email protected]
Hi Miquel,
Quoted 2 lines · Miquel Raynal
Why do we need a local bounce if the core can do it with
NAND_USES_DMA?
You are right, declaring NAND_USES_DMA is the consistent approach with the other controllers in this series, and I will rework this patch that way. One part of the local check needs to stay though:
the 512-byte alignment condition. The hardware requires R852_DMA_LEN alignment, which the core condition (chip->buf_align, default 1) does not know about, so misaligned linear buffers still need the pre-existing local bounce. The virt_addr_valid() part of the local condition becomes redundant once the core bounces the non-linear-map buffers, and v3 reverts it to the original form.
Quoted 1 line
Also please add Cc: stable to these three patches.
Done in v3, thanks.
Ping
v3 cover mtd: rawnand: protect DMA engines from vmalloc'd buffers
In reply to Ping LiWhile discussing a proposal to also allocate the mtdchar read/write bounce buffers with kvmalloc() [1], an audit showed that raw NAND controllers may 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()).
Non-linear-map buffers are not hypothetical: mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers to mtd_write().
intel and r852 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 keeps its existing local bounce, which is still needed for the hardware's 512-byte alignment requirement.
While at it, a leak in intel's ebu_dma_start() (mapping never unmapped on success, wrong device used on the error path) found during the audit is fixed as well.
Changes in v3 (following Miquel's review [2]):
- r852: use the core bounce through NAND_USES_DMA instead of extending the local condition; the local alignment check stays, as the core condition (chip->buf_align, default 1) does not cover the 512-byte hardware requirement.
- Add Cc: stable to the three patches.
Changes in v2 (following the AI review of v1):
- Drop the fsmc patch: nothing has set host->mode since a1b1e1d5bdfe ("mtd: nand: fsmc: finally remove fsmc_nand_platform_data"), so the DMA paths are dead code and fsmc_exec_op() always takes the PIO branch.
- Fix the Fixes: tags.
- Add a fix for the ebu_dma_start() unmap problems.
[1] https://lore.kernel.org/linux-mtd/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Ping Li (3):
mtd: rawnand: intel: declare NAND_USES_DMA
mtd: rawnand: intel: unmap the DMA buffer in ebu_dma_start()
mtd: rawnand: r852: declare NAND_USES_DMA
drivers/mtd/nand/raw/intel-nand-controller.c | 8 +++++--- drivers/mtd/nand/raw/r852.c | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.34.1
PATCH v3 1/3 mtd: rawnand: intel: declare NAND_USES_DMA
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()).
Non-linear-map buffers are not hypothetical here: mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers, so such buffers can reach the controller's DMA engine, for which they are 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.
--- drivers/mtd/nand/raw/intel-nand-controller.c | 1 + 1 file changed, 1 insertion(+)
drivers/mtd/nand/raw/intel-nand-controller.c +1 -0
@@ -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 );
Signature · 1 line
2.34.1
PATCH v3 2/3 mtd: rawnand: intel: unmap the DMA buffer in ebu_dma_start()
In reply to Ping Liebu_dma_start() maps the data buffer for every page transfer but only unmaps it on the error path. Successful transfers thus leak the mapping, which accumulates IOVA space on IOMMU systems and skips any cache maintenance the unmap would perform on non-coherent platforms. The error path has two more problems:
when the mapping itself fails it jumps to the unmap with an invalid dma address, and the unmap is performed with ebu_host->dev instead of the dma_dev device the buffer was mapped with.
Return directly when the mapping fails, and unmap with dma_dev on both the success and the error paths.
--- drivers/mtd/nand/raw/intel-nand-controller.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/mtd/nand/raw/intel-nand-controller.c +4 -3
@@ -312,8 +312,7 @@ static int ebu_dma_start(struct ebu_nand_controller *ebu_host, u32 dir, buf_dma =dma_map_single (dma_dev, (void *)buf, len, dir);if (dma_mapping_error (dma_dev, buf_dma)) {dev_err (ebu_host->dev,"Failed to map DMA buffer\n" ); - ret = -EIO ; - goto err_unmap; +return -EIO ; } tx =dmaengine_prep_slave_single (chan, buf_dma, len, dir, flags);
@@ -346,10 +345,12 @@ static int ebu_dma_start(struct ebu_nand_controller *ebu_host, u32 dir, goto err_unmap; } +dma_unmap_single (dma_dev, buf_dma, len, dir); +return 0 ; err_unmap: -dma_unmap_single (ebu_host->dev, buf_dma, len, dir); +dma_unmap_single (dma_dev, buf_dma, len, dir);return ret; }
Signature · 1 line
2.34.1
PATCH v3 3/3 mtd: rawnand: r852: declare NAND_USES_DMA
In reply to Ping Lir852_do_dma() feeds the buffers provided by the NAND core straight to dma_map_single(), which is only valid for linear-map addresses, 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. Such buffers are not hypothetical:
mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers.
Declare NAND_USES_DMA so that nand_base bounces non-linear-map buffers through chip->data_buf, as the other DMA-capable controllers do. The existing local bounce in r852_do_dma() is still needed for the hardware's 512-byte alignment requirement, which the core condition (chip->buf_align, default 1) does not cover.
--- drivers/mtd/nand/raw/r852.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/mtd/nand/raw/r852.c +10 -0
@@ -640,6 +640,16 @@ static int r852_register_nand_device(struct r852_device *dev)if (dev->readonly) dev->chip->options |=NAND_ROM ; +/* +* r852_do_dma() feeds the buffers provided by the NAND core +* straight to dma_map_single(), so ask nand_base to bounce +* non-linear-map buffers. The local bounce in r852_do_dma() +* is still needed for the hardware's 512-byte alignment +* requirement, which the core condition (chip->buf_align, +* default 1) does not cover. +*/ + dev->chip->options |=NAND_USES_DMA ; +r852_engine_enable (dev);if (sm_register_device (mtd, dev->sm))
Signature · 1 line
2.34.1
Miquel Raynal
maintainerRe: [PATCH v3 3/3] mtd: rawnand: r852: declare NAND_USES_DMA
drivers/mtd/nand/raw/r852.c static int r852_register_nand_device(struct r852_device *dev) · line 649
Quoted 8 lines
drivers/mtd/nand/raw/r852.c +7
-0+/* +* r852_do_dma() feeds the buffers provided by the NAND core +* straight to dma_map_single(), so ask nand_base to bounce +* non-linear-map buffers. The local bounce in r852_do_dma() +* is still needed for the hardware's 512-byte alignment +* requirement, which the core condition (chip->buf_align, +* default 1) does not cover.
Can't we change the default for this chip then?
Please drop this comment after fixing that, it is not useful, this is information that should belong in the commit log.
Quoted 6 lines
drivers/mtd/nand/raw/r852.c +3
-0+*/ + dev->chip->options |=NAND_USES_DMA ; +r852_engine_enable (dev);if (sm_register_device (mtd, dev->sm))
Re: [PATCH v3 3/3] mtd: rawnand: r852: declare NAND_USES_DMA
From: [email protected]
Hi Miquel,
Quoted 1 line · Miquel Raynal
Can't we change the default for this chip then?
Right, advertising the requirement through chip->buf_align is much better. v4 sets chip->buf_align = R852_DMA_LEN next to the NAND_USES_DMA declaration, so the core also bounces misaligned linear buffers, and the local condition no longer needs a justification of its own.
Quoted 2 lines
Please drop this comment after fixing that, it is not useful, this is information that should belong in the commit log.
Done, the rationale lives in the commit log now.
Ping
v4 cover mtd: rawnand: protect DMA engines from vmalloc'd buffers
In reply to Ping LiWhile discussing a proposal to also allocate the mtdchar read/write bounce buffers with kvmalloc() [1], an audit showed that raw NAND controllers may 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()).
Non-linear-map buffers are not hypothetical: mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers to mtd_write().
intel and r852 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 also advertises its 512-byte alignment requirement through chip->buf_align, so misaligned linear buffers are bounced by the core as well.
While at it, a leak in intel's ebu_dma_start() (mapping never unmapped on success, wrong device used on the error path) found during the audit is fixed as well.
Changes in v4 (following Miquel's review [2]):
- r852: advertise the 512-byte alignment requirement through chip->buf_align instead of keeping a local justification; drop the code comment.
Changes in v3 (following Miquel's review [3]):
- r852: use the core bounce through NAND_USES_DMA instead of extending the local condition.
- Add Cc: stable to the three patches.
Changes in v2 (following the AI review of v1):
- Drop the fsmc patch: nothing has set host->mode since a1b1e1d5bdfe ("mtd: nand: fsmc: finally remove fsmc_nand_platform_data"), so the DMA paths are dead code and fsmc_exec_op() always takes the PIO branch.
- Fix the Fixes: tags.
- Add a fix for the ebu_dma_start() unmap problems.
[1] https://lore.kernel.org/linux-mtd/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
Ping Li (3):
mtd: rawnand: intel: declare NAND_USES_DMA
mtd: rawnand: intel: unmap the DMA buffer in ebu_dma_start()
mtd: rawnand: r852: declare NAND_USES_DMA
drivers/mtd/nand/raw/intel-nand-controller.c | 8 +++++--- drivers/mtd/nand/raw/r852.c | 3 +++ 2 files changed, 8 insertions(+), 3 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.34.1
PATCH v4 1/3 mtd: rawnand: intel: declare NAND_USES_DMA
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()).
Non-linear-map buffers are not hypothetical here: mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers, so such buffers can reach the controller's DMA engine, for which they are 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.
--- drivers/mtd/nand/raw/intel-nand-controller.c | 1 + 1 file changed, 1 insertion(+)
drivers/mtd/nand/raw/intel-nand-controller.c +1 -0
@@ -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 );
Signature · 1 line
2.34.1
PATCH v4 2/3 mtd: rawnand: intel: unmap the DMA buffer in ebu_dma_start()
In reply to Ping Liebu_dma_start() maps the data buffer for every page transfer but only unmaps it on the error path. Successful transfers thus leak the mapping, which accumulates IOVA space on IOMMU systems and skips any cache maintenance the unmap would perform on non-coherent platforms. The error path has two more problems:
when the mapping itself fails it jumps to the unmap with an invalid dma address, and the unmap is performed with ebu_host->dev instead of the dma_dev device the buffer was mapped with.
Return directly when the mapping fails, and unmap with dma_dev on both the success and the error paths.
--- drivers/mtd/nand/raw/intel-nand-controller.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/mtd/nand/raw/intel-nand-controller.c +4 -3
@@ -312,8 +312,7 @@ static int ebu_dma_start(struct ebu_nand_controller *ebu_host, u32 dir, buf_dma =dma_map_single (dma_dev, (void *)buf, len, dir);if (dma_mapping_error (dma_dev, buf_dma)) {dev_err (ebu_host->dev,"Failed to map DMA buffer\n" ); - ret = -EIO ; - goto err_unmap; +return -EIO ; } tx =dmaengine_prep_slave_single (chan, buf_dma, len, dir, flags);
@@ -346,10 +345,12 @@ static int ebu_dma_start(struct ebu_nand_controller *ebu_host, u32 dir, goto err_unmap; } +dma_unmap_single (dma_dev, buf_dma, len, dir); +return 0 ; err_unmap: -dma_unmap_single (ebu_host->dev, buf_dma, len, dir); +dma_unmap_single (dma_dev, buf_dma, len, dir);return ret; }
Signature · 1 line
2.34.1
PATCH v4 3/3 mtd: rawnand: r852: declare NAND_USES_DMA
In reply to Ping Lir852_do_dma() feeds the buffers provided by the NAND core straight to dma_map_single(), which is only valid for linear-map addresses, 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. Such buffers are not hypothetical:
mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers.
Declare NAND_USES_DMA so that nand_base bounces non-linear-map buffers through chip->data_buf, and advertise the hardware's 512-byte alignment requirement through chip->buf_align so that misaligned linear buffers are bounced as well, like the other DMA-capable controllers do.
--- drivers/mtd/nand/raw/r852.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/mtd/nand/raw/r852.c +3 -0
@@ -640,6 +640,9 @@ static int r852_register_nand_device(struct r852_device *dev)if (dev->readonly) dev->chip->options |=NAND_ROM ; + dev->chip->options |=NAND_USES_DMA ; + dev->chip->buf_align =R852_DMA_LEN ; +r852_engine_enable (dev);if (sm_register_device (mtd, dev->sm))
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v4 3/3] mtd: rawnand: r852: declare NAND_USES_DMA
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mtd: rawnand: r852: unconditional NAND_USES_DMA in r852_register_nand_device() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mtd: rawnand: r852: unconditional NAND_USES_DMA in r852_register_nand_device()
--
commit bcc683752c018cf038ec971c8ed1a1fbca49d325
Author: Ping Li <[email protected]>
mtd: rawnand: r852: declare NAND_USES_DMA
This commit sets NAND_USES_DMA and chip->buf_align = R852_DMA_LEN in r852_register_nand_device() so nand_base bounces non-linear and misaligned buffers before r852_do_dma() maps them with dma_map_single().
drivers/mtd/nand/raw/r852.c static int r852_register_nand_device(struct r852_device *dev) · line 644
Quoted 10 lines
drivers/mtd/nand/raw/r852.c +2
-0@@ -640,6 +640,9 @@ static int r852_register_nand_device(struct r852_device *dev)if (dev->readonly) dev->chip->options |=NAND_ROM ; + dev->chip->options |=NAND_USES_DMA ; + dev->chip->buf_align =R852_DMA_LEN ;
[Severity: Low]
Should NAND_USES_DMA and chip->buf_align be gated on dev->dma_usable?
r852_dma_test() sets dev->dma_usable = 0 when the controller lacks DMA support or the r852_enable_dma module parameter is disabled, and r852_read_buf() and r852_write_buf() only call r852_do_dma() when len == R852_DMA_LEN && dev->dma_usable. Setting these unconditionally forces nand_base to bounce non-linear and non-512-byte-aligned buffers through chip->data_buf even when the driver operates purely in PIO mode.
Signature · 5 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
Miquel Raynal
maintainerRe: [PATCH v4 3/3] mtd: rawnand: r852: declare NAND_USES_DMA
Quoted 4 lines · On 25/11/2026 at 12:10:29 GMT, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mtd: rawnand: r852: unconditionalNAND_USES_DMAinr852_register_nand_device()
Correct.
Please be a little bit more cautious with your AI submissions.
Miquèl
v5 cover mtd: rawnand: protect DMA engines from vmalloc'd buffers
In reply to Ping LiWhile discussing a proposal to also allocate the mtdchar read/write bounce buffers with kvmalloc() [1], an audit showed that raw NAND controllers may 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()).
Non-linear-map buffers are not hypothetical: mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers to mtd_write().
intel and r852 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 also advertises its 512-byte alignment requirement through chip->buf_align, so misaligned linear buffers are bounced by the core as well.
While at it, a leak in intel's ebu_dma_start() (mapping never unmapped on success, wrong device used on the error path) found during the audit is fixed as well.
Changes in v5 (following the AI review [2]):
- r852: gate NAND_USES_DMA and chip->buf_align on dev->dma_usable, as the PIO fallback path does not care about buffer addresses being linear-map or 512-byte aligned.
Changes in v4 (following Miquel's review [3]):
- r852: advertise the 512-byte alignment requirement through chip->buf_align instead of keeping a local justification; drop the code comment.
Changes in v3 (following Miquel's review [4]):
- r852: use the core bounce through NAND_USES_DMA instead of extending the local condition.
- Add Cc: stable to the three patches.
Changes in v2 (following the AI review of v1):
- Drop the fsmc patch: nothing has set host->mode since a1b1e1d5bdfe ("mtd: nand: fsmc: finally remove fsmc_nand_platform_data"), so the DMA paths are dead code and fsmc_exec_op() always takes the PIO branch.
- Fix the Fixes: tags.
- Add a fix for the ebu_dma_start() unmap problems.
[1] https://lore.kernel.org/linux-mtd/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
Ping Li (3):
mtd: rawnand: intel: declare NAND_USES_DMA
mtd: rawnand: intel: unmap the DMA buffer in ebu_dma_start()
mtd: rawnand: r852: declare NAND_USES_DMA
drivers/mtd/nand/raw/intel-nand-controller.c | 8 +++++--- drivers/mtd/nand/raw/r852.c | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.34.1
PATCH v5 1/3 mtd: rawnand: intel: declare NAND_USES_DMA
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()).
Non-linear-map buffers are not hypothetical here: mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers, so such buffers can reach the controller's DMA engine, for which they are 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.
--- drivers/mtd/nand/raw/intel-nand-controller.c | 1 + 1 file changed, 1 insertion(+)
drivers/mtd/nand/raw/intel-nand-controller.c +1 -0
@@ -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 );
Signature · 1 line
2.34.1
PATCH v5 2/3 mtd: rawnand: intel: unmap the DMA buffer in ebu_dma_start()
In reply to Ping Liebu_dma_start() maps the data buffer for every page transfer but only unmaps it on the error path. Successful transfers thus leak the mapping, which accumulates IOVA space on IOMMU systems and skips any cache maintenance the unmap would perform on non-coherent platforms. The error path has two more problems:
when the mapping itself fails it jumps to the unmap with an invalid dma address, and the unmap is performed with ebu_host->dev instead of the dma_dev device the buffer was mapped with.
Return directly when the mapping fails, and unmap with dma_dev on both the success and the error paths.
--- drivers/mtd/nand/raw/intel-nand-controller.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/mtd/nand/raw/intel-nand-controller.c +4 -3
@@ -312,8 +312,7 @@ static int ebu_dma_start(struct ebu_nand_controller *ebu_host, u32 dir, buf_dma =dma_map_single (dma_dev, (void *)buf, len, dir);if (dma_mapping_error (dma_dev, buf_dma)) {dev_err (ebu_host->dev,"Failed to map DMA buffer\n" ); - ret = -EIO ; - goto err_unmap; +return -EIO ; } tx =dmaengine_prep_slave_single (chan, buf_dma, len, dir, flags);
@@ -346,10 +345,12 @@ static int ebu_dma_start(struct ebu_nand_controller *ebu_host, u32 dir, goto err_unmap; } +dma_unmap_single (dma_dev, buf_dma, len, dir); +return 0 ; err_unmap: -dma_unmap_single (ebu_host->dev, buf_dma, len, dir); +dma_unmap_single (dma_dev, buf_dma, len, dir);return ret; }
Signature · 1 line
2.34.1
PATCH v5 3/3 mtd: rawnand: r852: declare NAND_USES_DMA
In reply to Ping Lir852_do_dma() feeds the buffers provided by the NAND core straight to dma_map_single(), which is only valid for linear-map addresses, 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. Such buffers are not hypothetical:
mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers.
Declare NAND_USES_DMA so that nand_base bounces non-linear-map buffers through chip->data_buf, and advertise the hardware's 512-byte alignment requirement through chip->buf_align so that misaligned linear buffers are bounced as well, like the other DMA-capable controllers do. Both only apply when the controller actually uses DMA: the PIO fallback path does not care about buffer addresses being linear-map or 512-byte aligned.
--- drivers/mtd/nand/raw/r852.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/mtd/nand/raw/r852.c +5 -0
@@ -640,6 +640,11 @@ static int r852_register_nand_device(struct r852_device *dev)if (dev->readonly) dev->chip->options |=NAND_ROM ; +if (dev->dma_usable) { + dev->chip->options |=NAND_USES_DMA ; + dev->chip->buf_align =R852_DMA_LEN ; + } +r852_engine_enable (dev);if (sm_register_device (mtd, dev->sm))
Signature · 1 line
2.34.1
Miquel Raynal
maintainerRe: [PATCH v5 3/3] mtd: rawnand: r852: declare NAND_USES_DMA
Quoted 28 lines · Ping Li
r852_do_dma()feeds the buffers provided by the NAND core straight todma_map_single(), which is only valid for linear-map addresses, but the driver never declaresNAND_USES_DMA. Because of this,nand_basecannot know the controller DMAs these buffers and never bounces non-linear-map ones. Such buffers are not hypothetical:
mtdblock's write cache is vmalloc'ed and UBIFS passes vmalloc'ed LPT buffers.Declare
NAND_USES_DMAso thatnand_basebounces non-linear-map buffers throughchip->data_buf, and advertise the hardware's 512-byte alignment requirement throughchip->buf_alignso that misaligned linear buffers are bounced as well, like the other DMA-capable controllers do. Both only apply when the controller actually uses DMA: the PIO fallback path does not care about buffer addresses being linear-map or 512-byte aligned.Fixes 67e054e919248 ("mtd: nand: Add driver for Ricoh xD/SmartMedia reader")Signed-off-by Ping Li <[email protected]>--- drivers/mtd/nand/raw/r852.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/mtd/nand/raw/r852.c +0
-0
drivers/mtd/nand/raw/r852.c static int r852_register_nand_device(struct r852_device *dev) · line 646
Quoted 7 lines
drivers/mtd/nand/raw/r852.c +4
-0@@ -640,6 +640,11 @@ static int r852_register_nand_device(struct r852_device *dev)if (dev->readonly) dev->chip->options |=NAND_ROM ; +if (dev->dma_usable) { + dev->chip->options |=NAND_USES_DMA ; + dev->chip->buf_align =R852_DMA_LEN ; + }
Wasn't in this driver a bounce buffering operation that is no longer needed?
Re: [PATCH v5 3/3] mtd: rawnand: r852: declare NAND_USES_DMA
From: [email protected]
Hi Miquel,
Quoted 2 lines
Wasn't in this driver a bounce buffering operation that is no longer needed?
With our patch in place, a vmalloc'd buffer now gets bounced by the core through chip->data_buf. But data_buf comes from a bare kmalloc(), and kmalloc() does not guarantee the returned address to be 512-aligned (with SLUB debug options, for instance, the redzone padding shifts the object placement). So the check in r852_do_dma() -- (unsigned long)buf & (R852_DMA_LEN - 1) -- still has something to catch, and I believe it cannot be removed.
The second reason to keep it is that the local bounce is also the fallback when dma_map_single() fails.
Ping
Miquel Raynal
maintainerRe: [PATCH v5 3/3] mtd: rawnand: r852: declare NAND_USES_DMA
Quoted 15 lines · Ping Li
From: [email protected]
Hi Miquel,
Quoted 2 lines
Wasn't in this driver a bounce buffering operation that is no longer needed?
With our patch in place, a vmalloc'd buffer now gets bounced by the core through
chip->data_buf. Butdata_bufcomes from a barekmalloc(), andkmalloc()does not guarantee the returned address to be 512-aligned (with SLUB debug options, for instance, the redzone padding shifts the object placement). So the check inr852_do_dma()-- (unsigned long)buf & (R852_DMA_LEN- 1) -- still has something to catch, and I believe it cannot be removed.
Quoted 2 lines
The second reason to keep it is that the local bounce is also the fallback when
dma_map_single()fails.
Ok.
Please do not send new iterations in responses of your previous patchset.
Thanks,
Miquèl
Re: [PATCH v5 3/3] mtd: rawnand: r852: declare NAND_USES_DMA
From: [email protected]
Hi Miquel,
Thanks for the reminder about the threading, and thanks a lot for your guidance on this series.
By the way, if you have some time to spare, I would appreciate if you could also have a look at the kvmalloc RFC this series came from:
https://lore.kernel.org/all/[email protected]/
Thanks again,
Ping
Miquel Raynal
maintainerRe: [PATCH v5 3/3] mtd: rawnand: r852: declare NAND_USES_DMA
Quoted 9 lines
Hi Miquel,
Thanks for the reminder about the threading, and thanks a lot for your guidance on this series.
By the way, if you have some time to spare, I would appreciate if you could also have a look at the kvmalloc RFC this series came from:
I gave feedback 3 days ago. I am litteraly spending hours every day reviewing AI generated patchsets like yours and you dare asking if I have "some time to spare"? because your v2 has been waiting for 2 days? :-)
Re: [PATCH v5 3/3] mtd: rawnand: r852: declare NAND_USES_DMA
From: [email protected]
Hi Miquel,
Sorry for the noise on both topics -- I had not realized how much review load AI-generated submissions have been putting on you. And I admit the quality control on my side was not tight enough in some places.
Thanks for your patience, and have a good day. :-)
Ping
Quoted 8 lines · Ping Li
While discussing a proposal to also allocate the mtdchar read/write bounce buffers with
kvmalloc()[1], an audit showed that raw NAND controllers may DMA the buffers handed by the NAND core without declaringNAND_USES_DMA, sonand_basenever bounces non-linear-map buffers for them (see theuse_bounce_buflogic in nand_do_read_ops()/nand_do_write_ops()).[...]
Applied to nand/next, thanks!
[1/3] mtd: rawnand: intel: declare NAND_USES_DMA
commit: 9c6cf2bb11ca99bdbf789d17d59f5cc3044c0459
[2/3] mtd: rawnand: intel: unmap the DMA buffer in ebu_dma_start()
commit: c408988a803b0bc2ebbc9402210d7a1f5a35cb48
[3/3] mtd: rawnand: r852: declare NAND_USES_DMA
commit: bb29c651f8ecb8aa005630d7739859bbc2760714Patche(s) should be available on mtd/linux.git and will be part of the next PR (provided that no robot complains by then).
Kind regards,
Miquèl