Skip to contentSkip to conversation

mtd: rawnand: loongson1: Fix error code in ls1x_nand_dma_transfer()

PATCH Applied Dan Carpenter 3 participants 7 messages May 2, 2025 – May 12, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: rawnand: loongson1: Fix error code in ls1x_nand_dma_transfer()

The "desc" variable is NULL and PTR_ERR(NULL) is zero/success. Return a negative error code instead.

Fixes d2d10ede04b1 ("mtd: rawnand: Add Loongson-1 NAND Controller Driver")

---
It's hard to know what the patch prefix should be here. Ideally when we add a new driver we would use the patch prefix for the driver.

Tired: subsystem: Add driver XXX
Wired: subsystem: XXX: Add driver for XXX

 drivers/mtd/nand/raw/loongson1-nand-controller.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/nand/raw/loongson1-nand-controller.c +1 -1
index 6a369b1c7d86..8754bb4f8b56 100644
@@ -371,7 +371,7 @@ static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_
 	desc = dmaengine_prep_slave_single(chan, dma_addr, op->len, xfer_dir, DMA_PREP_INTERRUPT);
 	if (!desc) {
 		dev_err(dev, "failed to prepare DMA descriptor\n");
-		ret = PTR_ERR(desc);
+		ret = -ENOMEM;
 		goto err;
 	}
 	desc->callback = ls1x_nand_dma_callback;
Signature · 1 line
2.47.2
#1 lore raw
Quoted 21 lines · Dan Carpenter

The "desc" variable is NULL and PTR_ERR(NULL) is zero/success. Return a negative error code instead.

Fixes d2d10ede04b1 ("mtd: rawnand: Add Loongson-1 NAND Controller Driver")

---
It's hard to know what the patch prefix should be here. Ideally when we add a new driver we would use the patch prefix for the driver.

Tired: subsystem: Add driver XXX
Wired: subsystem: XXX: Add driver for XXX

 drivers/mtd/nand/raw/loongson1-nand-controller.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/nand/raw/loongson1-nand-controller.c +0 -0
index 6a369b1c7d86..8754bb4f8b56 100644

drivers/mtd/nand/raw/loongson1-nand-controller.c static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_ · line 374

Quoted 5 lines
drivers/mtd/nand/raw/loongson1-nand-controller.c +1 -1
@@ -371,7 +371,7 @@ static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_
        desc = dmaengine_prep_slave_single(chan, dma_addr, op->len, xfer_dir, DMA_PREP_INTERRUPT);
        if (!desc) {
                dev_err(dev, "failed to prepare DMA descriptor\n");
-               ret = PTR_ERR(desc);
+               ret = -ENOMEM;

Thank you for fixing this issue.
However, I believe -EIO is more appropriate than -ENOMEM, since dmaengine_prep_slave_single() can return errors other than -ENOMEM.

Quoted 6 lines
drivers/mtd/nand/raw/loongson1-nand-controller.c +0 -1
                goto err;
        }
        desc->callback = ls1x_nand_dma_callback;
--

2.47.2

Signature · 3 lines
Best regards,

Keguang Zhang
#2 lore raw
Quoted 17 lines · Keguang Zhang
Quoted 16 lines · Dan Carpenter

The "desc" variable is NULL and PTR_ERR(NULL) is zero/success. Return a negative error code instead.

Fixes d2d10ede04b1 ("mtd: rawnand: Add Loongson-1 NAND Controller Driver")

---
It's hard to know what the patch prefix should be here. Ideally when we add a new driver we would use the patch prefix for the driver.

Tired: subsystem: Add driver XXX
Wired: subsystem: XXX: Add driver for XXX

 drivers/mtd/nand/raw/loongson1-nand-controller.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

drivers/mtd/nand/raw/loongson1-nand-controller.c static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_ · line 374

Quoted 10 lines
drivers/mtd/nand/raw/loongson1-nand-controller.c +1 -1
index 6a369b1c7d86..8754bb4f8b56 100644
@@ -371,7 +371,7 @@ static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_
        desc = dmaengine_prep_slave_single(chan, dma_addr, op->len, xfer_dir, DMA_PREP_INTERRUPT);
        if (!desc) {
                dev_err(dev, "failed to prepare DMA descriptor\n");
-               ret = PTR_ERR(desc);
+               ret = -ENOMEM;

Thank you for fixing this issue.
However, I believe -EIO is more appropriate than -ENOMEM, since dmaengine_prep_slave_single() can return errors other than -ENOMEM.

It's not an I/O error so -EIO isn't correct.

There are a bunch of reasons it could fail but most likely dma_pool_alloc() failed. I think -ENOMEM is correct.

regards,
dan carpenter

#3 lore raw
Quoted 35 lines · Dan Carpenter
Quoted 17 lines · Keguang Zhang
Quoted 16 lines · Dan Carpenter

The "desc" variable is NULL and PTR_ERR(NULL) is zero/success. Return a negative error code instead.

Fixes d2d10ede04b1 ("mtd: rawnand: Add Loongson-1 NAND Controller Driver")

---
It's hard to know what the patch prefix should be here. Ideally when we add a new driver we would use the patch prefix for the driver.

Tired: subsystem: Add driver XXX
Wired: subsystem: XXX: Add driver for XXX

 drivers/mtd/nand/raw/loongson1-nand-controller.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

drivers/mtd/nand/raw/loongson1-nand-controller.c static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_ · line 374

Quoted 10 lines
drivers/mtd/nand/raw/loongson1-nand-controller.c +1 -1
index 6a369b1c7d86..8754bb4f8b56 100644
@@ -371,7 +371,7 @@ static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_
        desc = dmaengine_prep_slave_single(chan, dma_addr, op->len, xfer_dir, DMA_PREP_INTERRUPT);
        if (!desc) {
                dev_err(dev, "failed to prepare DMA descriptor\n");
-               ret = PTR_ERR(desc);
+               ret = -ENOMEM;

Thank you for fixing this issue.
However, I believe -EIO is more appropriate than -ENOMEM, since dmaengine_prep_slave_single() can return errors other than -ENOMEM.

Quoted 5 lines

It's not an I/O error so -EIO isn't correct.

There are a bunch of reasons it could fail but most likely dma_pool_alloc() failed. I think -ENOMEM is correct.

Have you reviewed the implementation of ls1x_dma_prep_slave_sg()? Errors in this function can be caused not only by -ENOMEM, but also by -EINVAL. Moreover, in most cases, the error handling logic for dmaengine_prep_slave_single() returns -EIO when the function returns NULL.

Quoted 3 lines

regards,
dan carpenter

Signature · 3 lines
Best regards,

Keguang Zhang
#4 lore raw
Quoted 36 lines · Keguang Zhang
Quoted 35 lines · Dan Carpenter
Quoted 32 lines · Keguang Zhang
Quoted 21 lines · Dan Carpenter

The "desc" variable is NULL and PTR_ERR(NULL) is zero/success. Return a negative error code instead.

Fixes d2d10ede04b1 ("mtd: rawnand: Add Loongson-1 NAND Controller Driver")

---
It's hard to know what the patch prefix should be here. Ideally when we add a new driver we would use the patch prefix for the driver.

Tired: subsystem: Add driver XXX
Wired: subsystem: XXX: Add driver for XXX

 drivers/mtd/nand/raw/loongson1-nand-controller.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/nand/raw/loongson1-nand-controller.c +0 -0
index 6a369b1c7d86..8754bb4f8b56 100644

drivers/mtd/nand/raw/loongson1-nand-controller.c static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_ · line 374

Quoted 5 lines
drivers/mtd/nand/raw/loongson1-nand-controller.c +1 -1
@@ -371,7 +371,7 @@ static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_
        desc = dmaengine_prep_slave_single(chan, dma_addr, op->len, xfer_dir, DMA_PREP_INTERRUPT);
        if (!desc) {
                dev_err(dev, "failed to prepare DMA descriptor\n");
-               ret = PTR_ERR(desc);
+               ret = -ENOMEM;

Thank you for fixing this issue.
However, I believe -EIO is more appropriate than -ENOMEM, since dmaengine_prep_slave_single() can return errors other than -ENOMEM.

Quoted 7 lines
Quoted 5 lines

It's not an I/O error so -EIO isn't correct.

There are a bunch of reasons it could fail but most likely dma_pool_alloc() failed. I think -ENOMEM is correct.

Have you reviewed the implementation of ls1x_dma_prep_slave_sg()?

The ls1x_dma_prep_slave_sg() has basically two error cases, buggy drivers and allocation errors. Someone could argue that if people pass invalid data then the correct return is -EINVAL but in these cases it's a buggy driver and we fix bugs, we don't work around them. So the correct return is -ENOMEM.

Quoted 4 lines

Errors in this function can be caused not only by -ENOMEM, but also by -EINVAL. Moreover, in most cases, the error handling logic for dmaengine_prep_slave_single() returns -EIO when the function returns NULL.

There are some that return -EIO but hardly the majority. Other places return -ENOMEM or -EINVAL. It's not worth going back and fixing all of these but really -ENOMEM is the correct return.

regards,
dan carpenter

#5 lore raw
Quoted 44 lines · Dan Carpenter
Quoted 42 lines · Keguang Zhang
Quoted 40 lines · Dan Carpenter
Quoted 17 lines · Keguang Zhang
Quoted 16 lines · Dan Carpenter

The "desc" variable is NULL and PTR_ERR(NULL) is zero/success. Return a negative error code instead.

Fixes d2d10ede04b1 ("mtd: rawnand: Add Loongson-1 NAND Controller Driver")

---
It's hard to know what the patch prefix should be here. Ideally when we add a new driver we would use the patch prefix for the driver.

Tired: subsystem: Add driver XXX
Wired: subsystem: XXX: Add driver for XXX

 drivers/mtd/nand/raw/loongson1-nand-controller.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

drivers/mtd/nand/raw/loongson1-nand-controller.c static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_ · line 374

Quoted 10 lines
drivers/mtd/nand/raw/loongson1-nand-controller.c +1 -1
index 6a369b1c7d86..8754bb4f8b56 100644
@@ -371,7 +371,7 @@ static int ls1x_nand_dma_transfer(struct ls1x_nand_host *host, struct ls1x_nand_
        desc = dmaengine_prep_slave_single(chan, dma_addr, op->len, xfer_dir, DMA_PREP_INTERRUPT);
        if (!desc) {
                dev_err(dev, "failed to prepare DMA descriptor\n");
-               ret = PTR_ERR(desc);
+               ret = -ENOMEM;

Thank you for fixing this issue.
However, I believe -EIO is more appropriate than -ENOMEM, since dmaengine_prep_slave_single() can return errors other than -ENOMEM.

It's not an I/O error so -EIO isn't correct.

There are a bunch of reasons it could fail but most likely dma_pool_alloc() failed. I think -ENOMEM is correct.

Quoted 8 lines
Quoted 1 line

Have you reviewed the implementation of ls1x_dma_prep_slave_sg()?

The ls1x_dma_prep_slave_sg() has basically two error cases, buggy drivers and allocation errors. Someone could argue that if people pass invalid data then the correct return is -EINVAL but in these cases it's a buggy driver and we fix bugs, we don't work around them. So the correct return is -ENOMEM.

Yes, -EINVAL indicates a bug rather than a real error.
However, this assumes that the caller is aware they are providing invalid data. Otherwise, they might be misled into thinking it is an out-of-memory error (-ENOMEM).

Quoted 5 lines
Quoted 4 lines

Errors in this function can be caused not only by -ENOMEM, but also by -EINVAL. Moreover, in most cases, the error handling logic for dmaengine_prep_slave_single() returns -EIO when the function returns NULL.

Quoted 7 lines

There are some that return -EIO but hardly the majority. Other places return -ENOMEM or -EINVAL. It's not worth going back and fixing all of these but really -ENOMEM is the correct return.

regards,
dan carpenter

--
Best regards,

Keguang Zhang

maintainer Applied
#6 lore raw
In reply to Dan Carpenter
Quoted 4 lines · Dan Carpenter

The "desc" variable is NULL and PTR_ERR(NULL) is zero/success. Return a negative error code instead.

Applied to nand/next, thanks!

[1/1] mtd: rawnand: loongson1: Fix error code in ls1x_nand_dma_transfer()

      commit: 0d470c72bea4d9f4c24b304fefdc857979cb5ca0

Patche(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