Skip to contentSkip to conversation

mtd: rawnand: Fix dma_map_sg error check

PATCH In review Jack Wang 2 participants 4 messages Aug 25, 2022 – Sep 19, 2022

Subsystems (4)
lore
Share & tools
lore

Jack Wang

author
#0 lore raw

cover mtd: rawnand: Fix dma_map_sg error check

Hi, all,

While working on a bugfix on RTRS[1], I noticed there are quite a few other drivers have the same problem, due to the fact dma_map_sg return 0 on error, not like most of the cases, return negative value for error.

I "grep -A 5 dma_map_sg' in kernel tree, and audit/fix the one I feel is buggy, hence this patchset. As suggested by Christoph Hellwig, I now send the patches per subsystem, this is for mtd subsystem.

Thanks!

[1] https://lore.kernel.org/linux-rdma/[email protected]/T/#t

Jack Wang (2):
mtd: rawnand: stm32_fmc2: Fix dma_map_sg error check
mtd: rawnand: marvell: Fix error handle regarding dma_map_sg

 drivers/mtd/nand/raw/marvell_nand.c    | 8 +++++++-
 drivers/mtd/nand/raw/stm32_fmc2_nand.c | 8 +++++---
 2 files changed, 12 insertions(+), 4 deletions(-)
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
author
#1 lore raw

PATCH 1/2 mtd: rawnand: stm32_fmc2: Fix dma_map_sg error check

dma_map_sg return 0 on error, in case of error return -EIO.

Cc Miquel Raynal <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Vignesh Raghavendra <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Philipp Zabel <[email protected]>
Cc Christophe Kerello <[email protected]>
Cc Cai Huoqing <[email protected]>
Fixes 2cd457f328c1 ("mtd: rawnand: stm32_fmc2: add STM32 FMC2 NAND flash controller driver")
---
 drivers/mtd/nand/raw/stm32_fmc2_nand.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/mtd/nand/raw/stm32_fmc2_nand.c +5 -3
index 87c1c7dd97eb..a0c825af19fa 100644
@@ -862,8 +862,8 @@ static int stm32_fmc2_nfc_xfer(struct nand_chip *chip, const u8 *buf,
 
 	ret = dma_map_sg(nfc->dev, nfc->dma_data_sg.sgl,
 			 eccsteps, dma_data_dir);
-	if (ret < 0)
-		return ret;
+	if (!ret)
+		return -EIO;
 
 	desc_data = dmaengine_prep_slave_sg(dma_ch, nfc->dma_data_sg.sgl,
 					    eccsteps, dma_transfer_dir,
@@ -893,8 +893,10 @@ static int stm32_fmc2_nfc_xfer(struct nand_chip *chip, const u8 *buf,
 
 		ret = dma_map_sg(nfc->dev, nfc->dma_ecc_sg.sgl,
 				 eccsteps, dma_data_dir);
-		if (ret < 0)
+		if (!ret) {
+			ret = -EIO;
 			goto err_unmap_data;
+		}
 
 		desc_ecc = dmaengine_prep_slave_sg(nfc->dma_ecc_ch,
 						   nfc->dma_ecc_sg.sgl,
Signature · 1 line
2.34.1
maintainer Reviewed-by Christophe Kerello on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mtd: rawnand: stm32_fmc2: Fix dma_map_sg error check

Hi Jack,

[email protected] wrote on Thu, 25 Aug 2022 09:53:37 +0200:

Quoted 16 lines

dma_map_sg return 0 on error, in case of error return -EIO.

Cc Miquel Raynal <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Vignesh Raghavendra <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Philipp Zabel <[email protected]>
Cc Christophe Kerello <[email protected]>
Cc Cai Huoqing <[email protected]>
Fixes 2cd457f328c1 ("mtd: rawnand: stm32_fmc2: add STM32 FMC2 NAND flash controller driver")

I'll apply this series so I need to re-add Christophe's tag sent on the patch alone (which is identical as far as I see):

Quoted 21 lines
---
 drivers/mtd/nand/raw/stm32_fmc2_nand.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/mtd/nand/raw/stm32_fmc2_nand.c +2 -2
index 87c1c7dd97eb..a0c825af19fa 100644
@@ -862,8 +862,8 @@ static int stm32_fmc2_nfc_xfer(struct nand_chip *chip, const u8 *buf,
 
 	ret = dma_map_sg(nfc->dev, nfc->dma_data_sg.sgl,
 			 eccsteps, dma_data_dir);
-	if (ret < 0)
-		return ret;
+	if (!ret)
+		return -EIO;
 
 	desc_data = dmaengine_prep_slave_sg(dma_ch, nfc->dma_data_sg.sgl,
 					    eccsteps, dma_transfer_dir,
@@ -893,8 +893,10 @@ static int stm32_fmc2_nfc_xfer(struct nand_chip *chip, const u8 *buf,
 

drivers/mtd/nand/raw/stm32_fmc2_nand.c static int stm32_fmc2_nfc_xfer(struct nand_chip *chip, const u8 *buf, · line 902

Quoted 10 lines
drivers/mtd/nand/raw/stm32_fmc2_nand.c +3 -1
 		ret = dma_map_sg(nfc->dev, nfc->dma_ecc_sg.sgl,
 				 eccsteps, dma_data_dir);
-		if (ret < 0)
+		if (!ret) {
+			ret = -EIO;
 			goto err_unmap_data;
+		}
 
 		desc_ecc = dmaengine_prep_slave_sg(nfc->dma_ecc_ch,
 						   nfc->dma_ecc_sg.sgl,

Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author
#3 lore raw

PATCH 2/2 mtd: rawnand: marvell: Fix error handle regarding dma_map_sg

In reply to Jack Wang

dma_map_sg return 0 on error, in case of error return -ENXIO, also add the dma_unmap_sg as rollback on the following error.

Cc Miquel Raynal <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Vignesh Raghavendra <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Philipp Zabel <[email protected]>
Cc Christophe Kerello <[email protected]>
Cc Cai Huoqing <[email protected]>
Fixes 02f26ecf8c77 ("mtd: nand: add reworked Marvell NAND controller driver")
---
 drivers/mtd/nand/raw/marvell_nand.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/mtd/nand/raw/marvell_nand.c +7 -1
index 2455a581fd70..d9f2f1d0b5ef 100644
@@ -865,13 +865,19 @@ static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
 	marvell_nfc_enable_dma(nfc);
 	/* Prepare the DMA transfer */
 	sg_init_one(&sg, nfc->dma_buf, dma_len);
-	dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
+	ret = dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
+	if (!ret) {
+		dev_err(nfc->dev, "Could not map DMA S/G list\n");
+		return -ENXIO;
+	}
+
 	tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1,
 				     direction == DMA_FROM_DEVICE ?
 				     DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
 				     DMA_PREP_INTERRUPT);
 	if (!tx) {
 		dev_err(nfc->dev, "Could not prepare DMA S/G list\n");
+		dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
 		return -ENXIO;
 	}
 
Signature · 1 line
2.34.1