Skip to contentSkip to conversation

gpio: gpiolib-of: Fix refcount bugs in of_mm_gpiochip_add_data()

PATCH Applied Liang He 3 participants 4 messages Jul 11, 2022 – Jul 12, 2022 GPIO

lore
Share & tools
lore

Liang He

author
#0 lore raw

PATCH v2 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.

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 this function, of_mm_gpiochip_add_data() is contained in an iteration of for_each_compatible_node() which will automatically increase and decrease the refcount. So we need additional of_node_get() for the reference escape in of_mm_gpiochip_add_data().

Fixes a19e3da5bc5f ("of/gpio: Kill of_gpio_chip and add members directly to gpio_chip")

---
changelog:

v2: (1) add more explaination advised by Linus Walleij
(2) use correct fix tag
v1: fix the refcount bug by
https://lore.kernel.org/all/[email protected]/

NOTE: I have confirmed that the mm_gc->gc.of_node has been correctly
put by of_mm_gpiochip_remove()-->gpiochip_remove()-->of_gpiochip_remove()

 drivers/gpio/gpiolib-of.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib-of.c +3 -1
index 3d6c3ffd5576..de100b0217da 100644
@@ -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
#1 lore raw
Quoted 11 lines · Liang He

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.

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 this function, of_mm_gpiochip_add_data() is contained in an iteration of for_each_compatible_node() which will automatically increase and decrease the refcount. So we need additional of_node_get() for the reference escape in of_mm_gpiochip_add_data().

Quoted 2 lines
Fixes a19e3da5bc5f ("of/gpio: Kill of_gpio_chip and add members directly to gpio_chip")

Patch applied for next as nonurgent fix!

Yours,
Linus Walleij

#2 lore raw
Quoted 13 lines · Linus Walleij
Quoted 11 lines · Liang He

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.

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 this function, of_mm_gpiochip_add_data() is contained in an iteration of for_each_compatible_node() which will automatically increase and decrease the refcount. So we need additional of_node_get() for the reference escape in of_mm_gpiochip_add_data().

Quoted 4 lines
Quoted 2 lines
Fixes a19e3da5bc5f ("of/gpio: Kill of_gpio_chip and add members directly to gpio_chip")

Patch applied for next as nonurgent fix!

Oh no I shouldn't. This goes to the GPIO tree, took it out again.

Yours,
Linus Walleij

#3 lore raw
Quoted 18 lines · Linus Walleij
Quoted 16 lines · Linus Walleij
Quoted 13 lines · Liang He

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.

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 this function, of_mm_gpiochip_add_data() is contained in an iteration of for_each_compatible_node() which will automatically increase and decrease the refcount. So we need additional of_node_get() for the reference escape in of_mm_gpiochip_add_data().

Fixes a19e3da5bc5f ("of/gpio: Kill of_gpio_chip and add members directly to gpio_chip")
Quoted 6 lines
Quoted 1 line

Patch applied for next as nonurgent fix!

Oh no I shouldn't. This goes to the GPIO tree, took it out again.

Yours,
Linus Walleij

Applied to GPIO tree.

Bart