Skip to contentSkip to conversation

mtd: nand: atmel: fix of_irq_get() error check

PATCH In review Sergei Shtylyov 3 participants 7 messages Aug 5, 2017 – Aug 13, 2017

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

PATCH 1/1 mtd: nand: atmel: fix of_irq_get() error check

of_irq_get() may return 0 as well as negative error number on failure, while the driver only checks for the negative values. The driver would then call devm_request_irq() for IRQ0 in its probe method and never get a valid interrupt.

Check for 'nc->irq <= 0' instead and return -ENXIO from the driver's probe iff of_irq_get() returned 0.

Fixes f88fc122cc34 ("mtd: nand: Cleanup/rework the atmel_nand driver")

---
The patch is against the 'master' branch of the 'linux-mtd.git' repo -- the 'nand/fixes' branch seems too old...

 drivers/mtd/nand/atmel/nand-controller.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/mtd/nand/atmel/nand-controller.c +6 -6
Index: linux-mtd/drivers/mtd/nand/atmel/nand-controller.c
===================================================================
@@ -2078,11 +2078,11 @@ atmel_hsmc_nand_controller_legacy_init(s
 	}
 
 	nc->irq = of_irq_get(nand_np, 0);
-	if (nc->irq < 0) {
-		ret = nc->irq;
-		if (ret != -EPROBE_DEFER)
+	if (nc->irq <= 0) {
+		if (nc->irq != -EPROBE_DEFER)
 			dev_err(dev, "Failed to get IRQ number (err = %d)\n",
-				ret);
+				nc->irq);
+		ret = nc->irq ?: -ENXIO;
 		goto out;
 	}
 
@@ -2168,11 +2168,11 @@ atmel_hsmc_nand_controller_init(struct a
 
 	nc->irq = of_irq_get(np, 0);
 	of_node_put(np);
-	if (nc->irq < 0) {
+	if (nc->irq <= 0) {
 		if (nc->irq != -EPROBE_DEFER)
 			dev_err(dev, "Failed to get IRQ number (err = %d)\n",
 				nc->irq);
-		return nc->irq;
+		return nc->irq ?: -ENXIO;
 	}
 
 	np = of_parse_phandle(dev->of_node, "atmel,nfc-io", 0);
#2 lore raw

Sorry, noise, please ignore.

Quoted 10 lines

-----Original Message-----
From: Wenyou Yang - A41535
Sent: 2017年8月7日 11:39
To: Wenyou Yang - A41535 <[email protected]>; Boris Brezillon <[email protected]>; Richard Weinberger <[email protected]>; David Woodhouse <[email protected]>; Brian Norris <[email protected]>; Marek Vasut <[email protected]>; Cyrille Pitchen <[email protected]>; [email protected]
Subject: Re: [PATCH] mtd: nand: atmel: fix of_irq_get() error check

Quoted 4 lines

tested

On 2017/8/6 5:14, Sergei Shtylyov wrote:

Best Regards,
Wenyou Yang

Acked-by on v1 1/1
#3 lore raw
In reply to Sergei Shtylyov
Quoted 18 lines

-----Original Message-----
From: Sergei Shtylyov [mailto:[email protected]]
Sent: 2017年8月6日 5:14
To: Wenyou Yang - A41535 <[email protected]>; Josh Wu <[email protected]>; Boris Brezillon <boris.brezillon@free- electrons.com>; Richard Weinberger <[email protected]>; David Woodhouse <[email protected]>; Brian Norris <[email protected]>; Marek Vasut <[email protected]>; Cyrille Pitchen <[email protected]>; [email protected]
Cc: Sergei Shtylyov <[email protected]>
Subject: [PATCH] mtd: nand: atmel: fix of_irq_get() error check

of_irq_get() may return 0 as well as negative error number on failure, while the driver only checks for the negative values. The driver would then call devm_request_irq() for IRQ0 in its probe method and never get a valid interrupt.

Quoted 6 lines

Check for 'nc->irq <= 0' instead and return -ENXIO from the driver's probe iff of_irq_get() returned 0.

Fixes f88fc122cc34 ("mtd: nand: Cleanup/rework the atmel_nand driver")

Indeed.

Best Regards,
Wenyou Yang

#4 lore raw
In reply to Sergei Shtylyov
Quoted 24 lines · Sergei Shtylyov

of_irq_get() may return 0 as well as negative error number on failure, while the driver only checks for the negative values. The driver would then call devm_request_irq() for IRQ0 in its probe method and never get a valid interrupt.

Check for 'nc->irq <= 0' instead and return -ENXIO from the driver's probe iff of_irq_get() returned 0.

Fixes f88fc122cc34 ("mtd: nand: Cleanup/rework the atmel_nand driver")

---
The patch is against the 'master' branch of the 'linux-mtd.git' repo -- the 'nand/fixes' branch seems too old...

 drivers/mtd/nand/atmel/nand-controller.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Index: linux-mtd/drivers/mtd/nand/atmel/nand-controller.c
===================================================================

drivers/mtd/nand/atmel/nand-controller.c +0 -0
@@ -2078,11 +2078,11 @@ atmel_hsmc_nand_controller_legacy_init(s
 	}

drivers/mtd/nand/atmel/nand-controller.c atmel_hsmc_nand_controller_legacy_init(s · line 2084

Quoted 10 lines
drivers/mtd/nand/atmel/nand-controller.c +3 -4
 
 	nc->irq = of_irq_get(nand_np, 0);
-	if (nc->irq < 0) {
-		ret = nc->irq;
-		if (ret != -EPROBE_DEFER)
+	if (nc->irq <= 0) {
+		if (nc->irq != -EPROBE_DEFER)
 			dev_err(dev, "Failed to get IRQ number (err = %d)\n",
-				ret);
+				nc->irq);

nc->irq can be 0 here which doesn't make any sense for an error return code, so I'd suggest to still use ret and move the 'ret = nc->irq ?:
-ENXIO' line before doing the != -EPROBE_DEFER test.

drivers/mtd/nand/atmel/nand-controller.c atmel_hsmc_nand_controller_legacy_init(s · line 2085

Quoted 1 line
drivers/mtd/nand/atmel/nand-controller.c +1 -0
+		ret = nc->irq ?: -ENXIO;

Why ENXIO and not EINVAL?

Quoted 18 lines
drivers/mtd/nand/atmel/nand-controller.c +2 -2
 		goto out;
 	}
 
@@ -2168,11 +2168,11 @@ atmel_hsmc_nand_controller_init(struct a
 
 	nc->irq = of_irq_get(np, 0);
 	of_node_put(np);
-	if (nc->irq < 0) {
+	if (nc->irq <= 0) {
 		if (nc->irq != -EPROBE_DEFER)
 			dev_err(dev, "Failed to get IRQ number (err = %d)\n",
 				nc->irq);
-		return nc->irq;
+		return nc->irq ?: -ENXIO;
 	}
 
 	np = of_parse_phandle(dev->of_node, "atmel,nfc-io", 0);
#5 lore raw
Quoted 25 lines · Boris Brezillon
Quoted 25 lines

of_irq_get() may return 0 as well as negative error number on failure, while the driver only checks for the negative values. The driver would then call devm_request_irq() for IRQ0 in its probe method and never get a valid interrupt.

Check for 'nc->irq <= 0' instead and return -ENXIO from the driver's probe iff of_irq_get() returned 0.

Fixes f88fc122cc34 ("mtd: nand: Cleanup/rework the atmel_nand driver")

---
The patch is against the 'master' branch of the 'linux-mtd.git' repo -- the 'nand/fixes' branch seems too old...

  drivers/mtd/nand/atmel/nand-controller.c |   12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)

Index: linux-mtd/drivers/mtd/nand/atmel/nand-controller.c
===================================================================

drivers/mtd/nand/atmel/nand-controller.c +0 -0
@@ -2078,11 +2078,11 @@ atmel_hsmc_nand_controller_legacy_init(s
  	}
 
Quoted 13 lines

drivers/mtd/nand/atmel/nand-controller.c atmel_hsmc_nand_controller_legacy_init(s · line 2084

Quoted 9 lines
drivers/mtd/nand/atmel/nand-controller.c +3 -4
  	nc->irq = of_irq_get(nand_np, 0);
-	if (nc->irq < 0) {
-		ret = nc->irq;
-		if (ret != -EPROBE_DEFER)
+	if (nc->irq <= 0) {
+		if (nc->irq != -EPROBE_DEFER)
  			dev_err(dev, "Failed to get IRQ number (err = %d)\n",
-				ret);
+				nc->irq);

nc->irq can be 0 here which doesn't make any sense for an error return code, so I'd suggest to still use ret and move the 'ret = nc->irq ?:
-ENXIO' line before doing the != -EPROBE_DEFER test.

I tried to keep reporting the original of_irq_get() result. Can change it
if you prefer it another way...

Quoted 3 lines

drivers/mtd/nand/atmel/nand-controller.c atmel_hsmc_nand_controller_legacy_init(s · line 2085

Quoted 1 line
drivers/mtd/nand/atmel/nand-controller.c +1 -0
+		ret = nc->irq ?: -ENXIO;

Why ENXIO and not EINVAL?

That's what platform_get_irq() (another of_irq_get() user) returns if it
doesn't find the IRQ resource.

Quoted 3 lines

drivers/mtd/nand/atmel/nand-controller.c atmel_hsmc_nand_controller_legacy_init(s · line 2088

Quoted 3 lines
drivers/mtd/nand/atmel/nand-controller.c +0 -0
  		goto out;
  	}
 

[...]
MBR, Sergei

#6 lore raw
Quoted 41 lines · Sergei Shtylyov
Quoted 25 lines · Boris Brezillon
Quoted 25 lines

of_irq_get() may return 0 as well as negative error number on failure, while the driver only checks for the negative values. The driver would then call devm_request_irq() for IRQ0 in its probe method and never get a valid interrupt.

Check for 'nc->irq <= 0' instead and return -ENXIO from the driver's probe iff of_irq_get() returned 0.

Fixes f88fc122cc34 ("mtd: nand: Cleanup/rework the atmel_nand driver")

---
The patch is against the 'master' branch of the 'linux-mtd.git' repo -- the 'nand/fixes' branch seems too old...

  drivers/mtd/nand/atmel/nand-controller.c |   12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)

Index: linux-mtd/drivers/mtd/nand/atmel/nand-controller.c
===================================================================

drivers/mtd/nand/atmel/nand-controller.c +0 -0
@@ -2078,11 +2078,11 @@ atmel_hsmc_nand_controller_legacy_init(s
  	}
 
Quoted 13 lines

drivers/mtd/nand/atmel/nand-controller.c atmel_hsmc_nand_controller_legacy_init(s · line 2084

Quoted 9 lines
drivers/mtd/nand/atmel/nand-controller.c +3 -4
  	nc->irq = of_irq_get(nand_np, 0);
-	if (nc->irq < 0) {
-		ret = nc->irq;
-		if (ret != -EPROBE_DEFER)
+	if (nc->irq <= 0) {
+		if (nc->irq != -EPROBE_DEFER)
  			dev_err(dev, "Failed to get IRQ number (err = %d)\n",
-				ret);
+				nc->irq);  

nc->irq can be 0 here which doesn't make any sense for an error return code, so I'd suggest to still use ret and move the 'ret = nc->irq ?:
-ENXIO' line before doing the != -EPROBE_DEFER test.

Quoted 2 lines

I tried to keep reporting the original of_irq_get() result. Can change it
if you prefer it another way...

Yep, I prefer to have an error core here. No need to send a new version, I already fixed the patch when applying.

BTW, it's applied to nand/next not nand/fixes because this bug does not really impact users as long as the 'interrupts' property is correctly defined in the DT (which AFAICT is the case).

Quoted 7 lines
Quoted 4 lines

drivers/mtd/nand/atmel/nand-controller.c atmel_hsmc_nand_controller_legacy_init(s · line 2085

Quoted 2 lines
drivers/mtd/nand/atmel/nand-controller.c +1 -0
+		ret = nc->irq ?: -ENXIO;  

Why ENXIO and not EINVAL?

That's what platform_get_irq() (another of_irq_get() user) returns if it
doesn't find the IRQ resource.

Okay.

Quoted 6 lines
Quoted 4 lines

drivers/mtd/nand/atmel/nand-controller.c atmel_hsmc_nand_controller_legacy_init(s · line 2088

Quoted 4 lines
drivers/mtd/nand/atmel/nand-controller.c +0 -0
  		goto out;
  	}
 

[...]
MBR, Sergei