diff mbox series

[v2] gpio: nomadik: Back out some managed resources

Message ID 20240305-fix-nomadik-gpio-v2-1-e5d1fbdc3f5c@linaro.org
State New
Headers show
Series [v2] gpio: nomadik: Back out some managed resources | expand

Commit Message

Linus Walleij March 5, 2024, 10:09 p.m. UTC
Several commits introduce managed resources (devm_*) into the
nmk_gpio_populate_chip() function.

This isn't always working because when called from the Nomadik pin
control driver we just want to populate some states for the device as
the same states are used by the pin control driver.

Some managed resources such as devm_kzalloc() etc will work, as the
passed in platform device will be used for lifecycle management,
but in some cases where we used the looked-up platform device
for the GPIO block, this will cause problems for the combined
pin control and GPIO driver, because it adds managed resources
to the GPIO device before it is probed, which is something that
the device core will not accept, and all of the GPIO blocks will
refuse to probe:

platform 8012e000.gpio: Resources present before probing
platform 8012e080.gpio: Resources present before probing
(...)

Fix this by not tying any managed resources to the looked-up
gpio_pdev/gpio_dev device, let's just live with the fact that
these need imperative resource management for now.

Drop in some notes and use a local *dev variable to clarify the
code.

Cc: Théo Lebrun <theo.lebrun@bootlin.com>
Fixes: 12410e95903c ("gpio: nomadik: use devm_platform_ioremap_resource() helper")
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
Changes in v2:
- Fix all the errorpaths.
- I did consider using the goto pattern, but the PTR_CAST() cases
  make it just uglier: we have to cast pointers to integers and back
  to pointers to return them in a goto :/
- Add some missing platform_device_put():s on some errorpaths.
- use PTR_CAST() instead of (void *) cast in one site.
- Link to v1: https://lore.kernel.org/r/20240305-fix-nomadik-gpio-v1-1-73162e3a388e@linaro.org
---
 drivers/gpio/gpio-nomadik.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)


---
base-commit: caddc92c57451d983c7e31e60b961c5aae4ece63
change-id: 20240305-fix-nomadik-gpio-50f2dddaa2dd

Best regards,

Comments

Andy Shevchenko March 6, 2024, 11:19 a.m. UTC | #1
On Wed, Mar 6, 2024 at 12:09 AM Linus Walleij <linus.walleij@linaro.org> wrote:
>
> Several commits introduce managed resources (devm_*) into the
> nmk_gpio_populate_chip() function.
>
> This isn't always working because when called from the Nomadik pin
> control driver we just want to populate some states for the device as
> the same states are used by the pin control driver.
>
> Some managed resources such as devm_kzalloc() etc will work, as the
> passed in platform device will be used for lifecycle management,
> but in some cases where we used the looked-up platform device
> for the GPIO block, this will cause problems for the combined
> pin control and GPIO driver, because it adds managed resources
> to the GPIO device before it is probed, which is something that
> the device core will not accept, and all of the GPIO blocks will
> refuse to probe:
>
> platform 8012e000.gpio: Resources present before probing
> platform 8012e080.gpio: Resources present before probing
> (...)
>
> Fix this by not tying any managed resources to the looked-up
> gpio_pdev/gpio_dev device, let's just live with the fact that
> these need imperative resource management for now.
>
> Drop in some notes and use a local *dev variable to clarify the
> code.

LGTM, some minor remarks below.

...

> Cc: Théo Lebrun <theo.lebrun@bootlin.com>

Note, you can put Cc:s after --- line and it won't go to the commit
message while Cc to the respective people.

...

>         if (device_property_read_u32(gpio_dev, "ngpios", &ngpio)) {
>                 ngpio = NMK_GPIO_PER_CHIP;
> -               dev_dbg(&pdev->dev, "populate: using default ngpio (%d)\n", ngpio);
> +               dev_dbg(dev, "populate: using default ngpio (%d)\n", ngpio);

While at it %d --> %u.

>         }

...

> +               dev_err(dev, "failed getting reset control: %ld\n",
>                         PTR_ERR(reset));

Also possible %pe.
Linus Walleij March 6, 2024, 12:51 p.m. UTC | #2
On Wed, Mar 6, 2024 at 12:20 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
> On Wed, Mar 6, 2024 at 12:09 AM Linus Walleij <linus.walleij@linaro.org> wrote:
> >
> > Several commits introduce managed resources (devm_*) into the
> > nmk_gpio_populate_chip() function.
> >
> > This isn't always working because when called from the Nomadik pin
> > control driver we just want to populate some states for the device as
> > the same states are used by the pin control driver.
> >
> > Some managed resources such as devm_kzalloc() etc will work, as the
> > passed in platform device will be used for lifecycle management,
> > but in some cases where we used the looked-up platform device
> > for the GPIO block, this will cause problems for the combined
> > pin control and GPIO driver, because it adds managed resources
> > to the GPIO device before it is probed, which is something that
> > the device core will not accept, and all of the GPIO blocks will
> > refuse to probe:
> >
> > platform 8012e000.gpio: Resources present before probing
> > platform 8012e080.gpio: Resources present before probing
> > (...)
> >
> > Fix this by not tying any managed resources to the looked-up
> > gpio_pdev/gpio_dev device, let's just live with the fact that
> > these need imperative resource management for now.
> >
> > Drop in some notes and use a local *dev variable to clarify the
> > code.
>
> LGTM, some minor remarks below.
>
> ...
>
> > Cc: Théo Lebrun <theo.lebrun@bootlin.com>
>
> Note, you can put Cc:s after --- line and it won't go to the commit
> message while Cc to the respective people.

Yeah old habit, actually b4 handles it fine by recording the
recipients only in the cover letter.

> > +               dev_dbg(dev, "populate: using default ngpio (%d)\n", ngpio);
>
> While at it %d --> %u.
(...)
> > +               dev_err(dev, "failed getting reset control: %ld\n",
> >                         PTR_ERR(reset));
>
> Also possible %pe.

Fixed them both when applying! Thanks!

Yours,
Linus Walleij
Théo Lebrun March 6, 2024, 3:13 p.m. UTC | #3
Hello,

On Wed Mar 6, 2024 at 1:51 PM CET, Linus Walleij wrote:
> On Wed, Mar 6, 2024 at 12:20 PM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> > On Wed, Mar 6, 2024 at 12:09 AM Linus Walleij <linus.walleij@linaro.org> wrote:
> > >
> > > Several commits introduce managed resources (devm_*) into the
> > > nmk_gpio_populate_chip() function.
> > >
> > > This isn't always working because when called from the Nomadik pin
> > > control driver we just want to populate some states for the device as
> > > the same states are used by the pin control driver.
> > >
> > > Some managed resources such as devm_kzalloc() etc will work, as the
> > > passed in platform device will be used for lifecycle management,
> > > but in some cases where we used the looked-up platform device
> > > for the GPIO block, this will cause problems for the combined
> > > pin control and GPIO driver, because it adds managed resources
> > > to the GPIO device before it is probed, which is something that
> > > the device core will not accept, and all of the GPIO blocks will
> > > refuse to probe:
> > >
> > > platform 8012e000.gpio: Resources present before probing
> > > platform 8012e080.gpio: Resources present before probing
> > > (...)
> > >
> > > Fix this by not tying any managed resources to the looked-up
> > > gpio_pdev/gpio_dev device, let's just live with the fact that
> > > these need imperative resource management for now.
> > >
> > > Drop in some notes and use a local *dev variable to clarify the
> > > code.
> >
> > LGTM, some minor remarks below.
> >
> > ...
> >
> > > Cc: Théo Lebrun <theo.lebrun@bootlin.com>
> >
> > Note, you can put Cc:s after --- line and it won't go to the commit
> > message while Cc to the respective people.
>
> Yeah old habit, actually b4 handles it fine by recording the
> recipients only in the cover letter.
>
> > > +               dev_dbg(dev, "populate: using default ngpio (%d)\n", ngpio);
> >
> > While at it %d --> %u.
> (...)
> > > +               dev_err(dev, "failed getting reset control: %ld\n",
> > >                         PTR_ERR(reset));
> >
> > Also possible %pe.
>
> Fixed them both when applying! Thanks!

Format specifier %pe takes a pointer, ie it should be reset and not
PTR_ERR(reset). See efaa90ed2cff ("gpio: nomadik: Back out some managed
resources") on linux-pinctrl/ib-nomadik-gpio.

Apart from that, tested efaa90ed2cff on Mobileye hardware.

GCC warning:

In file included from ./include/linux/device.h:15,
                 from ./include/linux/platform_device.h:13,
                 from drivers/gpio/gpio-nomadik.c:28:
drivers/gpio/gpio-nomadik.c: In function ‘nmk_gpio_populate_chip’:
drivers/gpio/gpio-nomadik.c:588:30: warning: format ‘%p’ expects argument of type ‘void *’, but argument 3 has type ‘long int’ [-Wformat=]
  588 |                 dev_err(dev, "failed getting reset control: %pe\n",
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./include/linux/dev_printk.h:110:30: note: in definition of macro ‘dev_printk_index_wrap’
  110 |                 _p_func(dev, fmt, ##__VA_ARGS__);                       \
      |                              ^~~
./include/linux/dev_printk.h:144:56: note: in expansion of macro ‘dev_fmt’
  144 |         dev_printk_index_wrap(_dev_err, KERN_ERR, dev, dev_fmt(fmt), ##__VA_ARGS__)
      |                                                        ^~~~~~~
drivers/gpio/gpio-nomadik.c:588:17: note: in expansion of macro ‘dev_err’
  588 |                 dev_err(dev, "failed getting reset control: %pe\n",
      |                 ^~~~~~~
drivers/gpio/gpio-nomadik.c:588:62: note: format string is defined here
  588 |                 dev_err(dev, "failed getting reset control: %pe\n",
      |                                                             ~^
      |                                                              |
      |                                                              void *
      |                                                             %ld



Regards,

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Andy Shevchenko March 6, 2024, 4:17 p.m. UTC | #4
On Wed, Mar 06, 2024 at 01:51:09PM +0100, Linus Walleij wrote:
> On Wed, Mar 6, 2024 at 12:20 PM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:

...

> Fixed them both when applying! Thanks!

There is still an issue with error path. The problem is that the populate is
called too early and even though the failure of let's say gpiochip_add_data()
(as it seems the only one that really needed the restoration) will NOT reverse
the all allocated gpio_pdev/gpio_dev related resources.

Hence what I see is
1) move populate as down as possible in probe();
2) create unpopulate counterpart;
3) call it in probe().
Linus Walleij March 6, 2024, 7:46 p.m. UTC | #5
On Wed, Mar 6, 2024 at 4:13 PM Théo Lebrun <theo.lebrun@bootlin.com> wrote:

> > Fixed them both when applying! Thanks!
>
> Format specifier %pe takes a pointer, ie it should be reset and not
> PTR_ERR(reset). See efaa90ed2cff ("gpio: nomadik: Back out some managed
> resources") on linux-pinctrl/ib-nomadik-gpio.

Ooopps of course. Fixed it up.

Yours,
Linus Walleij
diff mbox series

Patch

diff --git a/drivers/gpio/gpio-nomadik.c b/drivers/gpio/gpio-nomadik.c
index 483086deb397..e744beafdd00 100644
--- a/drivers/gpio/gpio-nomadik.c
+++ b/drivers/gpio/gpio-nomadik.c
@@ -509,9 +509,11 @@  struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 {
 	struct nmk_gpio_chip *nmk_chip;
 	struct platform_device *gpio_pdev;
+	struct device *dev = &pdev->dev;
 	struct reset_control *reset;
 	struct device *gpio_dev;
 	struct gpio_chip *chip;
+	struct resource *res;
 	struct clk *clk;
 	void __iomem *base;
 	u32 id, ngpio;
@@ -519,13 +521,13 @@  struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 
 	gpio_dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode);
 	if (!gpio_dev) {
-		dev_err(&pdev->dev, "populate \"%pfwP\": device not found\n", fwnode);
+		dev_err(dev, "populate \"%pfwP\": device not found\n", fwnode);
 		return ERR_PTR(-ENODEV);
 	}
 	gpio_pdev = to_platform_device(gpio_dev);
 
 	if (device_property_read_u32(gpio_dev, "gpio-bank", &id)) {
-		dev_err(&pdev->dev, "populate: gpio-bank property not found\n");
+		dev_err(dev, "populate: gpio-bank property not found\n");
 		platform_device_put(gpio_pdev);
 		return ERR_PTR(-EINVAL);
 	}
@@ -539,7 +541,7 @@  struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 	}
 #endif
 
-	nmk_chip = devm_kzalloc(&pdev->dev, sizeof(*nmk_chip), GFP_KERNEL);
+	nmk_chip = devm_kzalloc(dev, sizeof(*nmk_chip), GFP_KERNEL);
 	if (!nmk_chip) {
 		platform_device_put(gpio_pdev);
 		return ERR_PTR(-ENOMEM);
@@ -547,7 +549,7 @@  struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 
 	if (device_property_read_u32(gpio_dev, "ngpios", &ngpio)) {
 		ngpio = NMK_GPIO_PER_CHIP;
-		dev_dbg(&pdev->dev, "populate: using default ngpio (%d)\n", ngpio);
+		dev_dbg(dev, "populate: using default ngpio (%d)\n", ngpio);
 	}
 
 	nmk_chip->is_mobileye_soc = device_is_compatible(gpio_dev,
@@ -559,24 +561,31 @@  struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 	chip->label = dev_name(gpio_dev);
 	chip->parent = gpio_dev;
 
-	base = devm_platform_ioremap_resource(pdev, 0);
+	/* NOTE: different devices! No devm_platform_ioremap_resource() here! */
+	res = platform_get_resource(gpio_pdev, IORESOURCE_MEM, 0);
+	base = devm_ioremap_resource(dev, res);
 	if (IS_ERR(base)) {
 		platform_device_put(gpio_pdev);
 		return ERR_CAST(base);
 	}
 	nmk_chip->addr = base;
 
-	clk = devm_clk_get_optional(gpio_dev, NULL);
+	/* NOTE: do not use devm_ here! */
+	clk = clk_get_optional(gpio_dev, NULL);
 	if (IS_ERR(clk)) {
 		platform_device_put(gpio_pdev);
-		return (void *)clk;
+		return ERR_CAST(clk);
 	}
 	clk_prepare(clk);
 	nmk_chip->clk = clk;
 
-	reset = devm_reset_control_get_optional_shared(gpio_dev, NULL);
+	/* NOTE: do not use devm_ here! */
+	reset = reset_control_get_optional_shared(gpio_dev, NULL);
 	if (IS_ERR(reset)) {
-		dev_err(&pdev->dev, "failed getting reset control: %ld\n",
+		clk_unprepare(clk);
+		clk_put(clk);
+		platform_device_put(gpio_pdev);
+		dev_err(dev, "failed getting reset control: %ld\n",
 			PTR_ERR(reset));
 		return ERR_CAST(reset);
 	}
@@ -588,7 +597,11 @@  struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 	 */
 	ret = reset_control_deassert(reset);
 	if (ret) {
-		dev_err(&pdev->dev, "failed reset deassert: %d\n", ret);
+		reset_control_put(reset);
+		clk_unprepare(clk);
+		clk_put(clk);
+		platform_device_put(gpio_pdev);
+		dev_err(dev, "failed reset deassert: %d\n", ret);
 		return ERR_PTR(ret);
 	}