Skip to contentSkip to conversation

gpio: em: remove the gpiochip before removing the irq domain

PATCH Superseded Bartosz Golaszewski 5 participants 6 messages Jul 10, 2019 – Jul 28, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 gpio: em: remove the gpiochip before removing the irq domain

From: Bartosz Golaszewski <[email protected]>

In commit 8764c4ca5049 ("gpio: em: use the managed version of gpiochip_add_data()") we implicitly altered the ordering of resource
freeing: since gpiochip_remove() calls gpiochip_irqchip_remove() internally, we now can potentially use the irq_domain after it was destroyed in the remove() callback (as devm resources are freed after remove() has returned).

Use devm_add_action() to keep the ordering right and entirely kill the remove() callback in the driver.

Fixes 8764c4ca5049 ("gpio: em: use the managed version of gpiochip_add_data()")
---
 drivers/gpio/gpio-em.c | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)
drivers/gpio/gpio-em.c +17 -18
index b6af705a4e5f..c88028ac66f2 100644
@@ -259,6 +259,13 @@ static const struct irq_domain_ops em_gio_irq_domain_ops = {
 	.xlate	= irq_domain_xlate_twocell,
 };
 
+static void em_gio_irq_domain_remove(void *data)
+{
+	struct irq_domain *domain = data;
+
+	irq_domain_remove(domain);
+}
+
 static int em_gio_probe(struct platform_device *pdev)
 {
 	struct em_gio_priv *p;
@@ -333,39 +340,32 @@ static int em_gio_probe(struct platform_device *pdev)
 		return -ENXIO;
 	}
 
+	ret = devm_add_action(&pdev->dev,
+			      em_gio_irq_domain_remove, p->irq_domain);
+	if (ret) {
+		irq_domain_remove(p->irq_domain);
+		return ret;
+	}
+
 	if (devm_request_irq(&pdev->dev, irq[0]->start,
 			     em_gio_irq_handler, 0, name, p)) {
 		dev_err(&pdev->dev, "failed to request low IRQ\n");
-		ret = -ENOENT;
-		goto err1;
+		return -ENOENT;
 	}
 
 	if (devm_request_irq(&pdev->dev, irq[1]->start,
 			     em_gio_irq_handler, 0, name, p)) {
 		dev_err(&pdev->dev, "failed to request high IRQ\n");
-		ret = -ENOENT;
-		goto err1;
+		return -ENOENT;
 	}
 
 	ret = devm_gpiochip_add_data(&pdev->dev, gpio_chip, p);
 	if (ret) {
 		dev_err(&pdev->dev, "failed to add GPIO controller\n");
-		goto err1;
+		return ret;
 	}
 
 	return 0;
-
-err1:
-	irq_domain_remove(p->irq_domain);
-	return ret;
-}
-
-static int em_gio_remove(struct platform_device *pdev)
-{
-	struct em_gio_priv *p = platform_get_drvdata(pdev);
-
-	irq_domain_remove(p->irq_domain);
-	return 0;
 }
 
 static const struct of_device_id em_gio_dt_ids[] = {
@@ -376,7 +376,6 @@ MODULE_DEVICE_TABLE(of, em_gio_dt_ids);
 
 static struct platform_driver em_gio_device_driver = {
 	.probe		= em_gio_probe,
-	.remove		= em_gio_remove,
 	.driver		= {
 		.name	= "em_gio",
 		.of_match_table = em_gio_dt_ids,
Signature · 1 line
2.21.0
#1 lore raw

PATCH 2/2 gpio: em: use a helper variable for &pdev->dev

From: Bartosz Golaszewski <[email protected]>

Instead of always dereferencing &pdev->dev, just assign a helper local variable of type struct device * and use it where applicable.

---
 drivers/gpio/gpio-em.c | 37 +++++++++++++++++++------------------
 1 file changed, 19 insertions(+), 18 deletions(-)
drivers/gpio/gpio-em.c +19 -18
index c88028ac66f2..e3aa6fe3a320 100644
@@ -272,11 +272,12 @@ static int em_gio_probe(struct platform_device *pdev)
 	struct resource *io[2], *irq[2];
 	struct gpio_chip *gpio_chip;
 	struct irq_chip *irq_chip;
-	const char *name = dev_name(&pdev->dev);
+	struct device *dev = &pdev->dev;
+	const char *name = dev_name(dev);
 	unsigned int ngpios;
 	int ret;
 
-	p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
+	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
 	if (!p)
 		return -ENOMEM;
 
@@ -290,27 +291,27 @@ static int em_gio_probe(struct platform_device *pdev)
 	irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
 
 	if (!io[0] || !io[1] || !irq[0] || !irq[1]) {
-		dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
+		dev_err(dev, "missing IRQ or IOMEM\n");
 		return -EINVAL;
 	}
 
-	p->base0 = devm_ioremap_nocache(&pdev->dev, io[0]->start,
+	p->base0 = devm_ioremap_nocache(dev, io[0]->start,
 					resource_size(io[0]));
 	if (!p->base0)
 		return -ENOMEM;
 
-	p->base1 = devm_ioremap_nocache(&pdev->dev, io[1]->start,
+	p->base1 = devm_ioremap_nocache(dev, io[1]->start,
 				   resource_size(io[1]));
 	if (!p->base1)
 		return -ENOMEM;
 
-	if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
-		dev_err(&pdev->dev, "Missing ngpios OF property\n");
+	if (of_property_read_u32(dev->of_node, "ngpios", &ngpios)) {
+		dev_err(dev, "Missing ngpios OF property\n");
 		return -EINVAL;
 	}
 
 	gpio_chip = &p->gpio_chip;
-	gpio_chip->of_node = pdev->dev.of_node;
+	gpio_chip->of_node = dev->of_node;
 	gpio_chip->direction_input = em_gio_direction_input;
 	gpio_chip->get = em_gio_get;
 	gpio_chip->direction_output = em_gio_direction_output;
@@ -319,7 +320,7 @@ static int em_gio_probe(struct platform_device *pdev)
 	gpio_chip->request = em_gio_request;
 	gpio_chip->free = em_gio_free;
 	gpio_chip->label = name;
-	gpio_chip->parent = &pdev->dev;
+	gpio_chip->parent = dev;
 	gpio_chip->owner = THIS_MODULE;
 	gpio_chip->base = -1;
 	gpio_chip->ngpio = ngpios;
@@ -333,35 +334,35 @@ static int em_gio_probe(struct platform_device *pdev)
 	irq_chip->irq_release_resources = em_gio_irq_relres;
 	irq_chip->flags	= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND;
 
-	p->irq_domain = irq_domain_add_simple(pdev->dev.of_node, ngpios, 0,
+	p->irq_domain = irq_domain_add_simple(dev->of_node, ngpios, 0,
 					      &em_gio_irq_domain_ops, p);
 	if (!p->irq_domain) {
-		dev_err(&pdev->dev, "cannot initialize irq domain\n");
+		dev_err(dev, "cannot initialize irq domain\n");
 		return -ENXIO;
 	}
 
-	ret = devm_add_action(&pdev->dev,
+	ret = devm_add_action(dev,
 			      em_gio_irq_domain_remove, p->irq_domain);
 	if (ret) {
 		irq_domain_remove(p->irq_domain);
 		return ret;
 	}
 
-	if (devm_request_irq(&pdev->dev, irq[0]->start,
+	if (devm_request_irq(dev, irq[0]->start,
 			     em_gio_irq_handler, 0, name, p)) {
-		dev_err(&pdev->dev, "failed to request low IRQ\n");
+		dev_err(dev, "failed to request low IRQ\n");
 		return -ENOENT;
 	}
 
-	if (devm_request_irq(&pdev->dev, irq[1]->start,
+	if (devm_request_irq(dev, irq[1]->start,
 			     em_gio_irq_handler, 0, name, p)) {
-		dev_err(&pdev->dev, "failed to request high IRQ\n");
+		dev_err(dev, "failed to request high IRQ\n");
 		return -ENOENT;
 	}
 
-	ret = devm_gpiochip_add_data(&pdev->dev, gpio_chip, p);
+	ret = devm_gpiochip_add_data(dev, gpio_chip, p);
 	if (ret) {
-		dev_err(&pdev->dev, "failed to add GPIO controller\n");
+		dev_err(dev, "failed to add GPIO controller\n");
 		return ret;
 	}
 
Signature · 1 line
2.21.0
#3 lore raw
In reply to Bartosz Golaszewski

G'day Bartosz,

One comment below

Quoted 39 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

In commit 8764c4ca5049 ("gpio: em: use the managed version of gpiochip_add_data()") we implicitly altered the ordering of resource
freeing: since gpiochip_remove() calls gpiochip_irqchip_remove() internally, we now can potentially use the irq_domain after it was destroyed in the remove() callback (as devm resources are freed after remove() has returned).

Use devm_add_action() to keep the ordering right and entirely kill the remove() callback in the driver.

Fixes 8764c4ca5049 ("gpio: em: use the managed version of gpiochip_add_data()")
---
  drivers/gpio/gpio-em.c | 35 +++++++++++++++++------------------
  1 file changed, 17 insertions(+), 18 deletions(-)
drivers/gpio/gpio-em.c +7 -0
index b6af705a4e5f..c88028ac66f2 100644
@@ -259,6 +259,13 @@ static const struct irq_domain_ops em_gio_irq_domain_ops = {
  	.xlate	= irq_domain_xlate_twocell,
  };
 
+static void em_gio_irq_domain_remove(void *data)
+{
+	struct irq_domain *domain = data;
+
+	irq_domain_remove(domain);
+}
+
  static int em_gio_probe(struct platform_device *pdev)
  {
  	struct em_gio_priv *p;

drivers/gpio/gpio-em.c static int em_gio_probe(struct platform_device *pdev) · line 344

Quoted 5 lines
drivers/gpio/gpio-em.c +2 -0
@@ -333,39 +340,32 @@ static int em_gio_probe(struct platform_device *pdev)
  		return -ENXIO;
  	}
 
+	ret = devm_add_action(&pdev->dev,
+			      em_gio_irq_domain_remove, p->irq_domain);

Could devm_add_action_or_reset be used?

Quoted 53 lines
drivers/gpio/gpio-em.c +8 -18
+	if (ret) {
+		irq_domain_remove(p->irq_domain);
+		return ret;
+	}
+
  	if (devm_request_irq(&pdev->dev, irq[0]->start,
  			     em_gio_irq_handler, 0, name, p)) {
  		dev_err(&pdev->dev, "failed to request low IRQ\n");
-		ret = -ENOENT;
-		goto err1;
+		return -ENOENT;
  	}
 
  	if (devm_request_irq(&pdev->dev, irq[1]->start,
  			     em_gio_irq_handler, 0, name, p)) {
  		dev_err(&pdev->dev, "failed to request high IRQ\n");
-		ret = -ENOENT;
-		goto err1;
+		return -ENOENT;
  	}
 
  	ret = devm_gpiochip_add_data(&pdev->dev, gpio_chip, p);
  	if (ret) {
  		dev_err(&pdev->dev, "failed to add GPIO controller\n");
-		goto err1;
+		return ret;
  	}
 
  	return 0;
-
-err1:
-	irq_domain_remove(p->irq_domain);
-	return ret;
-}
-
-static int em_gio_remove(struct platform_device *pdev)
-{
-	struct em_gio_priv *p = platform_get_drvdata(pdev);
-
-	irq_domain_remove(p->irq_domain);
-	return 0;
  }
 
  static const struct of_device_id em_gio_dt_ids[] = {
@@ -376,7 +376,6 @@ MODULE_DEVICE_TABLE(of, em_gio_dt_ids);
 
  static struct platform_driver em_gio_device_driver = {
  	.probe		= em_gio_probe,
-	.remove		= em_gio_remove,
  	.driver		= {
  		.name	= "em_gio",
  		.of_match_table = em_gio_dt_ids,
Signature · 2 lines
Regards
Phil Reid
#4 lore raw

śr., 10 lip 2019 o 11:37 Phil Reid <[email protected]> napisał(a):

Quoted 48 lines

G'day Bartosz,

One comment below

Quoted 42 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

In commit 8764c4ca5049 ("gpio: em: use the managed version of gpiochip_add_data()") we implicitly altered the ordering of resource
freeing: since gpiochip_remove() calls gpiochip_irqchip_remove() internally, we now can potentially use the irq_domain after it was destroyed in the remove() callback (as devm resources are freed after remove() has returned).

Use devm_add_action() to keep the ordering right and entirely kill the remove() callback in the driver.

Fixes 8764c4ca5049 ("gpio: em: use the managed version of gpiochip_add_data()")
---
  drivers/gpio/gpio-em.c | 35 +++++++++++++++++------------------
  1 file changed, 17 insertions(+), 18 deletions(-)
drivers/gpio/gpio-em.c +7 -0
index b6af705a4e5f..c88028ac66f2 100644
@@ -259,6 +259,13 @@ static const struct irq_domain_ops em_gio_irq_domain_ops = {
      .xlate  = irq_domain_xlate_twocell,
  };
 
+static void em_gio_irq_domain_remove(void *data)
+{
+     struct irq_domain *domain = data;
+
+     irq_domain_remove(domain);
+}
+
  static int em_gio_probe(struct platform_device *pdev)
  {
      struct em_gio_priv *p;
@@ -333,39 +340,32 @@ static int em_gio_probe(struct platform_device *pdev)
              return -ENXIO;
      }
 
Quoted 5 lines

drivers/gpio/gpio-em.c static int em_gio_probe(struct platform_device *pdev) · line 344

Quoted 2 lines
drivers/gpio/gpio-em.c +2 -0
+     ret = devm_add_action(&pdev->dev,
+                           em_gio_irq_domain_remove, p->irq_domain);

Could devm_add_action_or_reset be used?

Of course it could and it should. :)

I'll resend tomorrow to not spam the mailing list.

Thanks,
Bart

Quoted 59 lines
Quoted 46 lines
drivers/gpio/gpio-em.c +8 -17
+     if (ret) {
+             irq_domain_remove(p->irq_domain);
+             return ret;
+     }
+
      if (devm_request_irq(&pdev->dev, irq[0]->start,
                           em_gio_irq_handler, 0, name, p)) {
              dev_err(&pdev->dev, "failed to request low IRQ\n");
-             ret = -ENOENT;
-             goto err1;
+             return -ENOENT;
      }
 
      if (devm_request_irq(&pdev->dev, irq[1]->start,
                           em_gio_irq_handler, 0, name, p)) {
              dev_err(&pdev->dev, "failed to request high IRQ\n");
-             ret = -ENOENT;
-             goto err1;
+             return -ENOENT;
      }
 
      ret = devm_gpiochip_add_data(&pdev->dev, gpio_chip, p);
      if (ret) {
              dev_err(&pdev->dev, "failed to add GPIO controller\n");
-             goto err1;
+             return ret;
      }
 
      return 0;
-
-err1:
-     irq_domain_remove(p->irq_domain);
-     return ret;
-}
-
-static int em_gio_remove(struct platform_device *pdev)
-{
-     struct em_gio_priv *p = platform_get_drvdata(pdev);
-
-     irq_domain_remove(p->irq_domain);
-     return 0;
  }
 
  static const struct of_device_id em_gio_dt_ids[] = {

drivers/gpio/gpio-em.c MODULE_DEVICE_TABLE(of, em_gio_dt_ids); · line 381

Quoted 7 lines
drivers/gpio/gpio-em.c +0 -1
@@ -376,7 +376,6 @@ MODULE_DEVICE_TABLE(of, em_gio_dt_ids);
 
  static struct platform_driver em_gio_device_driver = {
      .probe          = em_gio_probe,
-     .remove         = em_gio_remove,
      .driver         = {
              .name   = "em_gio",
              .of_match_table = em_gio_dt_ids,

--
Regards
Phil Reid

#5 lore raw
In reply to Phil Reid

Hi Phil,

Quoted 21 lines · Phil Reid
Quoted 19 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

In commit 8764c4ca5049 ("gpio: em: use the managed version of gpiochip_add_data()") we implicitly altered the ordering of resource
freeing: since gpiochip_remove() calls gpiochip_irqchip_remove() internally, we now can potentially use the irq_domain after it was destroyed in the remove() callback (as devm resources are freed after remove() has returned).

Use devm_add_action() to keep the ordering right and entirely kill the remove() callback in the driver.

Fixes 8764c4ca5049 ("gpio: em: use the managed version of gpiochip_add_data()")
drivers/gpio/gpio-em.c +0 -0
Quoted 8 lines

drivers/gpio/gpio-em.c static int em_gio_probe(struct platform_device *pdev) · line 344

Quoted 6 lines
drivers/gpio/gpio-em.c +2 -0
@@ -333,39 +340,32 @@ static int em_gio_probe(struct platform_device *pdev)
              return -ENXIO;
      }
 
+     ret = devm_add_action(&pdev->dev,
+                           em_gio_irq_domain_remove, p->irq_domain);

Could devm_add_action_or_reset be used?

Thank you very much for bringing this function to my attention! I was just wondering if devm_add_action() should call the action on failure, as this is what most callers seem to do anyway.

Quoted 5 lines

drivers/gpio/gpio-em.c static int em_gio_probe(struct platform_device *pdev) · line 348

Quoted 5 lines
drivers/gpio/gpio-em.c +4 -0
+     if (ret) {
+             irq_domain_remove(p->irq_domain);
+             return ret;
+     }

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] gpio: em: use a helper variable for &pdev->dev

In reply to Bartosz Golaszewski
Quoted 6 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Instead of always dereferencing &pdev->dev, just assign a helper local variable of type struct device * and use it where applicable.

I expect I get this and other EM patches in a pull request from you.

Yours,
Linus Walleij