Skip to contentSkip to conversation

pinctrl: bcm2835: Fix a few error paths

PATCH In review Florian Fainelli 3 participants 5 messages Jan 27, 2022 – Jan 30, 2022

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

PATCH 1/1 pinctrl: bcm2835: Fix a few error paths

After commit 266423e60ea1 ("pinctrl: bcm2835: Change init order for gpio hogs") a few error paths would not unwind properly the registration of gpio ranges. Correct that by assigning a single error label and goto it whenever we encounter a fatal error.

Fixes 266423e60ea1 ("pinctrl: bcm2835: Change init order for gpio hogs")
---
 drivers/pinctrl/bcm/pinctrl-bcm2835.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
drivers/pinctrl/bcm/pinctrl-bcm2835.c +15 -8
index c4ebfa852b42..47e433e09c5c 100644
@@ -1269,16 +1269,18 @@ static int bcm2835_pinctrl_probe(struct platform_device *pdev)
 				     sizeof(*girq->parents),
 				     GFP_KERNEL);
 	if (!girq->parents) {
-		pinctrl_remove_gpio_range(pc->pctl_dev, &pc->gpio_range);
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto out_remove;
 	}
 
 	if (is_7211) {
 		pc->wake_irq = devm_kcalloc(dev, BCM2835_NUM_IRQS,
 					    sizeof(*pc->wake_irq),
 					    GFP_KERNEL);
-		if (!pc->wake_irq)
-			return -ENOMEM;
+		if (!pc->wake_irq) {
+			err = -ENOMEM;
+			goto out_remove;
+		}
 	}
 
 	/*
@@ -1306,8 +1308,10 @@ static int bcm2835_pinctrl_probe(struct platform_device *pdev)
 
 		len = strlen(dev_name(pc->dev)) + 16;
 		name = devm_kzalloc(pc->dev, len, GFP_KERNEL);
-		if (!name)
-			return -ENOMEM;
+		if (!name) {
+			err = -ENOMEM;
+			goto out_remove;
+		}
 
 		snprintf(name, len, "%s:bank%d", dev_name(pc->dev), i);
 
@@ -1326,11 +1330,14 @@ static int bcm2835_pinctrl_probe(struct platform_device *pdev)
 	err = gpiochip_add_data(&pc->gpio_chip, pc);
 	if (err) {
 		dev_err(dev, "could not add GPIO chip\n");
-		pinctrl_remove_gpio_range(pc->pctl_dev, &pc->gpio_range);
-		return err;
+		goto out_remove;
 	}
 
 	return 0;
+
+out_remove:
+	pinctrl_remove_gpio_range(pc->pctl_dev, &pc->gpio_range);
+	return err;
 }
 
 static struct platform_driver bcm2835_pinctrl_driver = {
Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines · Florian Fainelli

After commit 266423e60ea1 ("pinctrl: bcm2835: Change init order for gpio hogs") a few error paths would not unwind properly the registration of gpio ranges. Correct that by assigning a single error label and goto it whenever we encounter a fatal error.

 1 file changed, 15 insertions(+), 8 deletions(-)

While this seems legit per se, my eyes caught this:

drivers/pinctrl/bcm/pinctrl-bcm2835.c static int bcm2835_pinctrl_probe(struct platform_device *pdev) · line 1273

Quoted 5 lines
drivers/pinctrl/bcm/pinctrl-bcm2835.c +2 -2
 	if (!girq->parents) {
-		pinctrl_remove_gpio_range(pc->pctl_dev, &pc->gpio_range);
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto out_remove;

Non-devm....

drivers/pinctrl/bcm/pinctrl-bcm2835.c static int bcm2835_pinctrl_probe(struct platform_device *pdev) · line 1279

Quoted 6 lines
drivers/pinctrl/bcm/pinctrl-bcm2835.c +0 -0
 	}
 
 	if (is_7211) {
 		pc->wake_irq = devm_kcalloc(dev, BCM2835_NUM_IRQS,
 					    sizeof(*pc->wake_irq),
 					    GFP_KERNEL);

...followed by devm.

It means more ordering bugs in the ->remove() and error path are lurking around. Can you double check and be sure that we do not have a case where non-devm registration code followed by devm?

If that is the case it has to be fixed as well.

Quoted 7 lines
drivers/pinctrl/bcm/pinctrl-bcm2835.c +4 -2
-		if (!pc->wake_irq)
-			return -ENOMEM;
+		if (!pc->wake_irq) {
+			err = -ENOMEM;
+			goto out_remove;
+		}
 	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 26 lines · Andy Shevchenko
Quoted 6 lines · Florian Fainelli

After commit 266423e60ea1 ("pinctrl: bcm2835: Change init order for gpio hogs") a few error paths would not unwind properly the registration of gpio ranges. Correct that by assigning a single error label and goto it whenever we encounter a fatal error.

  1 file changed, 15 insertions(+), 8 deletions(-)

While this seems legit per se, my eyes caught this:

drivers/pinctrl/bcm/pinctrl-bcm2835.c static int bcm2835_pinctrl_probe(struct platform_device *pdev) · line 1273

Quoted 5 lines
  	if (!girq->parents) {
drivers/pinctrl/bcm/pinctrl-bcm2835.c +2 -2
-		pinctrl_remove_gpio_range(pc->pctl_dev, &pc->gpio_range);
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto out_remove;

Non-devm....

Quoted 6 lines
  	}

if (is_7211) {
pc->wake_irq = devm_kcalloc(dev, BCM2835_NUM_IRQS,
sizeof(*pc->wake_irq), GFP_KERNEL);

Quoted 5 lines

...followed by devm.

It means more ordering bugs in the ->remove() and error path are lurking around. Can you double check and be sure that we do not have a case where non-devm registration code followed by devm?

It seems to me like we are fine with the patch as is, because:

- girq->parents is allocated with devm
- pc->wake_irq is allocated with devm
- name is allocated with devm

and those are the only variables being allocated for which we also process an error handling path.

Signature · 1 line
Florian
#3 lore raw
Quoted 32 lines · Florian Fainelli
Quoted 25 lines · Andy Shevchenko
Quoted 6 lines · Florian Fainelli

After commit 266423e60ea1 ("pinctrl: bcm2835: Change init order for gpio hogs") a few error paths would not unwind properly the registration of gpio ranges. Correct that by assigning a single error label and goto it whenever we encounter a fatal error.

  1 file changed, 15 insertions(+), 8 deletions(-)

While this seems legit per se, my eyes caught this:

drivers/pinctrl/bcm/pinctrl-bcm2835.c static int bcm2835_pinctrl_probe(struct platform_device *pdev) · line 1273

Quoted 5 lines
  	if (!girq->parents) {
drivers/pinctrl/bcm/pinctrl-bcm2835.c +2 -2
-		pinctrl_remove_gpio_range(pc->pctl_dev, &pc->gpio_range);
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto out_remove;

Non-devm....

Quoted 5 lines
  	}
  	if (is_7211) {
  		pc->wake_irq = devm_kcalloc(dev, BCM2835_NUM_IRQS,
  					    sizeof(*pc->wake_irq),
  					    GFP_KERNEL);
Quoted 5 lines

...followed by devm.

It means more ordering bugs in the ->remove() and error path are lurking around. Can you double check and be sure that we do not have a case where non-devm registration code followed by devm?

Quoted 8 lines

It seems to me like we are fine with the patch as is, because:

- girq->parents is allocated with devm
- pc->wake_irq is allocated with devm
- name is allocated with devm

and those are the only variables being allocated for which we also process an error handling path.

Okay, thanks.

My worries that it might be the case when the GPIO ranges have been removed by explicit call in ->remove() followed by some interrupt or so and oops or misbehaviour because of that.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
In reply to Florian Fainelli
Quoted 7 lines · Florian Fainelli

After commit 266423e60ea1 ("pinctrl: bcm2835: Change init order for gpio hogs") a few error paths would not unwind properly the registration of gpio ranges. Correct that by assigning a single error label and goto it whenever we encounter a fatal error.

Fixes 266423e60ea1 ("pinctrl: bcm2835: Change init order for gpio hogs")

Patch applied.

Yours,
Linus Walleij