gpio: gpiolib-of: Fix refcount bugs in of_mm_gpiochip_add_data()
PATCH 1/1 gpio: gpiolib-of: Fix refcount bugs in of_mm_gpiochip_add_data()
We should use of_node_get() when a new reference of device_node is created. It is noted that the old reference stored in 'mm_gc->gc.of_node' should also be decreased.
--- drivers/gpio/gpiolib-of.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib-of.c +3 -1
@@ -860,7 +860,8 @@ int of_mm_gpiochip_add_data(struct device_node *np,if (mm_gc->save_regs) mm_gc->save_regs (mm_gc); - mm_gc->gc.of_node = np; +of_node_put (mm_gc->gc.of_node); + mm_gc->gc.of_node =of_node_get (np); ret =gpiochip_add_data (gc, data);if (ret)
@@ -868,6 +869,7 @@ int of_mm_gpiochip_add_data(struct device_node *np,return 0 ; err2: +of_node_put (np);iounmap (mm_gc->regs); err1:kfree (gc->label);
Signature · 1 line
2.25.1
Quoted 34 lines · Liang He
We should use
of_node_get()when a new reference ofdevice_nodeis created. It is noted that the old reference stored in 'mm_gc->gc.of_node' should also be decreased.Fixes f141ed65f256 ("gpio: Move DT support code into drivers/gpio")Signed-off-by Liang He <[email protected]>--- drivers/gpio/gpiolib-of.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/gpio/gpiolib-of.c +3
-2@@ -860,7 +860,8 @@ int of_mm_gpiochip_add_data(struct device_node *np,if (mm_gc->save_regs) mm_gc->save_regs (mm_gc); - mm_gc->gc.of_node = np; +of_node_put (mm_gc->gc.of_node); + mm_gc->gc.of_node =of_node_get (np); ret =gpiochip_add_data (gc, data);if (ret)@@ -868,6 +869,7 @@ int of_mm_gpiochip_add_data(struct device_node *np,return 0 ; err2: +of_node_put (np);iounmap (mm_gc->regs); err1:kfree (gc->label); --
Quoted 2 lines
2.25.1
Have you noticed any issue with reference counting that made you post this patch? We typically "borrow" the reference to the platform device's of_node in GPIO drivers (and everywhere I can think of too).
Bart
Quoted 42 lines · Bartosz Golaszewski
Quoted 34 lines · Liang He
We should use
of_node_get()when a new reference ofdevice_nodeis created. It is noted that the old reference stored in 'mm_gc->gc.of_node' should also be decreased.Fixes f141ed65f256 ("gpio: Move DT support code into drivers/gpio")Signed-off-by Liang He <[email protected]>--- drivers/gpio/gpiolib-of.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/gpio/gpiolib-of.c +3
-2@@ -860,7 +860,8 @@ int of_mm_gpiochip_add_data(struct device_node *np,if (mm_gc->save_regs) mm_gc->save_regs (mm_gc); - mm_gc->gc.of_node = np; +of_node_put (mm_gc->gc.of_node); + mm_gc->gc.of_node =of_node_get (np); ret =gpiochip_add_data (gc, data);if (ret)@@ -868,6 +869,7 @@ int of_mm_gpiochip_add_data(struct device_node *np,return 0 ; err2: +of_node_put (np);iounmap (mm_gc->regs); err1:kfree (gc->label); --Quoted 2 lines
2.25.1
Have you noticed any issue with reference counting that made you post this patch? We typically "borrow" the reference to the platform device's
of_nodein GPIO drivers (and everywhere I can think of too).
Quoted 1 line
Bart
Hi, Bart.
This patch is based on the fact that there is a call site in function 'qe_add_gpiochips()' of src file 'drivers\soc\fsl\qe\gpio.c'.
In that function, of_mm_gpiochip_add_data() is contained in a iteration of for_each_compatible_node(), which will automatically increase and decrease the refcount.
As there is a new reference escaped into the mm_gc->gc.of_node, we should increase the refcount, otherwise, there may be a premature free as we do not know the existence of the reference in 'mm_gc->gc.of_node'.
If my understanding is wrong, please correct me.
Thanks,
Liang
Quoted 2 lines · Liang He
Quoted 1 line · Bartosz Golaszewski
On Mon, Jul 4, 2022 at 11:13 AM Liang He <[email protected]> wrote:
...
Quoted 5 lines
This patch is based on the fact that there is a call site in function '
qe_add_gpiochips()' of src file 'drivers\soc\fsl\qe\gpio.c'.In that function,
of_mm_gpiochip_add_data()is contained in a iteration offor_each_compatible_node(), which will automatically increase and decrease the refcount.
"and decrease"
Quoted 5 lines
As there is a new reference escaped into the
mm_gc->gc.of_node, we should increase the refcount, otherwise, there may be a premature free as we do not know the existence of the reference in 'mm_gc->gc.of_node'.If my understanding is wrong, please correct me.
Yes, see above.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 15 lines · Andy Shevchenko
Quoted 2 lines · Liang He
Quoted 1 line · Bartosz Golaszewski
On Mon, Jul 4, 2022 at 11:13 AM Liang He <[email protected]> wrote:
...
Quoted 5 lines
This patch is based on the fact that there is a call site in function '
qe_add_gpiochips()' of src file 'drivers\soc\fsl\qe\gpio.c'.In that function,
of_mm_gpiochip_add_data()is contained in a iteration offor_each_compatible_node(), which will automatically increase and decrease the refcount."and decrease"
Quoted 11 lines
Quoted 5 lines
As there is a new reference escaped into the
mm_gc->gc.of_node, we should increase the refcount, otherwise, there may be a premature free as we do not know the existence of the reference in 'mm_gc->gc.of_node'.If my understanding is wrong, please correct me.
Yes, see above.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi,Andy,
I think you did not get my point.
The for_each_xxx OF api has decreased the refcount, but there is a reference |ESCAPE| out of the local area, so we need additional refcounting, right?
Thanks,
Liang
Hi Liang,
thanks for your patch!
Quoted 5 lines · Liang He
We should use
of_node_get()when a new reference ofdevice_nodeis created. It is noted that the old reference stored in 'mm_gc->gc.of_node' should also be decreased.Fixes f141ed65f256 ("gpio: Move DT support code into drivers/gpio")
I doubt this is fixing that commit since it is only moving code?
drivers/gpio/gpiolib-of.c int of_mm_gpiochip_add_data(struct device_node *np, · line 864
Quoted 3 lines
drivers/gpio/gpiolib-of.c +2
-1- mm_gc->gc.of_node = np; +of_node_put (mm_gc->gc.of_node); + mm_gc->gc.of_node =of_node_get (np);
Aha
Quoted 5 lines
This patch is based on the fact that there is a call site in function '
qe_add_gpiochips()' of src file 'drivers\soc\fsl\qe\gpio.c'.In that function,
of_mm_gpiochip_add_data()is contained in a iteration offor_each_compatible_node(), which will automatically increase and decrease the refcount.
Put this into the commit log message!
I guess it solves the immediate problem, but the real solution is to get rid of of_mm_gpiochip_add_data() and struct of_mm_gpio_chip and replace all calls with instances of proper gpiochips using [devm_]gpiochip_add_data().
Which is in our TODO file.
It's not very much using this old helper:
$ git grep of_mm_gpiochip_add_data arch/powerpc/platforms/4xx/gpio.c: ret = of_mm_gpiochip_add_data(np, mm_gc, ppc4xx_gc); arch/powerpc/platforms/8xx/cpm1.c: return of_mm_gpiochip_add_data(np, mm_gc, cpm1_gc); arch/powerpc/platforms/8xx/cpm1.c: return of_mm_gpiochip_add_data(np, mm_gc, cpm1_gc); arch/powerpc/sysdev/cpm_common.c: return of_mm_gpiochip_add_data(np, mm_gc, cpm2_gc); drivers/gpio/gpio-altera.c: ret = of_mm_gpiochip_add_data(node, &altera_gc->mmchip, altera_gc); drivers/gpio/gpio-mm-lantiq.c: return of_mm_gpiochip_add_data(pdev->dev.of_node, &chip->mmchip, chip); drivers/gpio/gpio-mpc5200.c: ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip); drivers/gpio/gpio-mpc5200.c: ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip); drivers/soc/fsl/qe/gpio.c: ret = of_mm_gpiochip_add_data(np, mm_gc, qe_gc);
Those are all.
They all seem like they can be simplified a lot by using select GPIO_GENERIC and bgpio_init() in the manner of eg drivers/gpio/gpio-ftgpio010.c replacing a lot of boilerplate code.
If you have access to the FSL board and can test this, please consider doing this bigger change instead, at least for that board. It is certainly making the world a much better place than reparing the mistakes in code using of_mm_gpio_chip.
Yours,
Linus Walleij
Quoted 5 lines · Linus Walleij
Hi Liang,
thanks for your patch!
Thanks!
Quoted 17 lines · Liang He
Quoted 6 lines
We should use
of_node_get()when a new reference ofdevice_nodeis created. It is noted that the old reference stored in 'mm_gc->gc.of_node' should also be decreased.Fixes f141ed65f256 ("gpio: Move DT support code into drivers/gpio")I doubt this is fixing that commit since it is only moving code?
drivers/gpio/gpiolib-of.c int of_mm_gpiochip_add_data(struct device_node *np, · line 864
Quoted 3 lines
drivers/gpio/gpiolib-of.c +2
-1- mm_gc->gc.of_node = np; +of_node_put (mm_gc->gc.of_node); + mm_gc->gc.of_node =of_node_get (np);Aha
Quoted 9 lines
Quoted 5 lines
This patch is based on the fact that there is a call site in function '
qe_add_gpiochips()' of src file 'drivers\soc\fsl\qe\gpio.c'.In that function,
of_mm_gpiochip_add_data()is contained in a iteration offor_each_compatible_node(), which will automatically increase and decrease the refcount.Put this into the commit log message!
I will put it in new version patch.
Quoted 35 lines
I guess it solves the immediate problem, but the real solution is to get rid of
of_mm_gpiochip_add_data()and structof_mm_gpio_chipand replace all calls with instances of proper gpiochips using [devm_]gpiochip_add_data().Which is in our TODO file.
It's not very much using this old helper:
$ git grep of_mm_gpiochip_add_data arch/powerpc/platforms/4xx/gpio.c: ret = of_mm_gpiochip_add_data(np, mm_gc, ppc4xx_gc); arch/powerpc/platforms/8xx/cpm1.c: return of_mm_gpiochip_add_data(np, mm_gc, cpm1_gc); arch/powerpc/platforms/8xx/cpm1.c: return of_mm_gpiochip_add_data(np, mm_gc, cpm1_gc); arch/powerpc/sysdev/cpm_common.c: return of_mm_gpiochip_add_data(np, mm_gc, cpm2_gc); drivers/gpio/gpio-altera.c: ret = of_mm_gpiochip_add_data(node, &altera_gc->mmchip, altera_gc); drivers/gpio/gpio-mm-lantiq.c: return of_mm_gpiochip_add_data(pdev->dev.of_node, &chip->mmchip, chip); drivers/gpio/gpio-mpc5200.c: ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip); drivers/gpio/gpio-mpc5200.c: ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip); drivers/soc/fsl/qe/gpio.c: ret = of_mm_gpiochip_add_data(np, mm_gc, qe_gc);Those are all.
They all seem like they can be simplified a lot by using select
GPIO_GENERICandbgpio_init()in the manner of egdrivers/gpio/gpio-ftgpio010.creplacing a lot of boilerplate code.
Quoted 8 lines
If you have access to the FSL board and can test this, please consider doing this bigger change instead, at least for that board. It is certainly making the world a much better place than reparing the mistakes in code using
of_mm_gpio_chip.Quoted 1 line
Yours,
Linus Walleij
Hi, Linus,
First of all, thanks for your effort to review and confirm my current patch.
Second, while I would like very much to make a bigger change, it will need days for me to learn the whole semantic of the source code as now I only have learned the semantic of OF APIs and can only make a small step to decide if there is a refcount bug.
But now, should I re-post this patch with the above commit log you suggested or do more things after I can?
Thanks again,
Liang
Quoted 7 lines · Liang He
At 2022-07-06 20:47:08, "Linus Walleij" <[email protected]> wrote:
Second, while I would like very much to make a bigger change, it will need days for me to learn the whole semantic of the source code as now I only have learned the semantic of OF APIs and can only make a small step to decide if there is a refcount bug.But now, should I re-post this patch with the above commit log you suggested or do more things after I can?
Sure no problem, just post it!
Yours,
Linus Walleij