2019-03-28 13:15:38

by Geert Uytterhoeven

[permalink] [raw]
Subject: [PATCH 0/3] gpio: Failure path fixes

Hi Linus, Bartosz,

After making a silly mistake in a pin control driver, pinctrl driver
initialization failed, causing an avalanche of GPIO driver failures, and
I was greeted by the following scary messages:

OF: ERROR: Bad of_node_put() on /soc/gpio@e6050000
...
WARNING: CPU: 0 PID: 1 at lib/refcount.c:156 kobject_get+0x38/0x4c
...
WARNING: CPU: 0 PID: 1 at lib/refcount.c:190 refcount_sub_and_test_checked+0x3c/0xa8

The first two patches fix bugs in related error paths, after which the
messages are gone.
The third patch is a drive-by fix for function comment.

Thanks for your comments!

Geert Uytterhoeven (3):
gpio: of: Fix of_gpiochip_add() error path
gpio: Fix gpiochip_add_data_with_key() error path
gpio: Remove obsolete comment about gpiochip_free_hogs() usage

drivers/gpio/gpiolib-of.c | 8 +++++++-
drivers/gpio/gpiolib.c | 14 ++++++++------
2 files changed, 15 insertions(+), 7 deletions(-)

--
2.17.1

Gr{oetje,eeting}s,

Geert

--
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


2019-03-28 13:14:51

by Geert Uytterhoeven

[permalink] [raw]
Subject: [PATCH 1/3] gpio: of: Fix of_gpiochip_add() error path

If the call to of_gpiochip_scan_gpios() in of_gpiochip_add() fails, no
error handling is performed. This lead to the need of callers to call
of_gpiochip_remove() on failure, which causes "BAD of_node_put() on ..."
if the failure happened before the call to of_node_get().

Fix this by adding proper error handling.

Note that calling gpiochip_remove_pin_ranges() multiple times causes no
harm: subsequent calls are a no-op.

Fixes: dfbd379ba9b7431e ("gpio: of: Return error if gpio hog configuration failed")
Signed-off-by: Geert Uytterhoeven <[email protected]>
---
drivers/gpio/gpiolib-of.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index 8b9c3ab70f6eade4..257be7bac032a855 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -717,7 +717,13 @@ int of_gpiochip_add(struct gpio_chip *chip)

of_node_get(chip->of_node);

- return of_gpiochip_scan_gpios(chip);
+ status = of_gpiochip_scan_gpios(chip);
+ if (status) {
+ of_node_put(chip->of_node);
+ gpiochip_remove_pin_ranges(chip);
+ }
+
+ return status;
}

void of_gpiochip_remove(struct gpio_chip *chip)
--
2.17.1


2019-03-28 13:14:54

by Geert Uytterhoeven

[permalink] [raw]
Subject: [PATCH 3/3] gpio: Remove obsolete comment about gpiochip_free_hogs() usage

gpiochip_free_hogs() was always called from gpiochip_remove(), not
of_gpiochip_remove(). It is now also called from the failure patch in
gpiochip_add_data_with_key().

Fixes: f625d4601759f1cf ("gpio: add GPIO hogging mechanism")
Signed-off-by: Geert Uytterhoeven <[email protected]>
---
drivers/gpio/gpiolib.c | 2 --
1 file changed, 2 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index ed4da07effe0ac40..ec326bd84b1f1c50 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -4449,8 +4449,6 @@ int gpiod_hog(struct gpio_desc *desc, const char *name,
/**
* gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
* @chip: gpio chip to act on
- *
- * This is only used by of_gpiochip_remove to free hogged gpios
*/
static void gpiochip_free_hogs(struct gpio_chip *chip)
{
--
2.17.1


2019-03-28 13:15:01

by Geert Uytterhoeven

[permalink] [raw]
Subject: [PATCH 2/3] gpio: Fix gpiochip_add_data_with_key() error path

The err_remove_chip block is too coarse, and may perform cleanup that
must not be done. E.g. if of_gpiochip_add() fails, of_gpiochip_remove()
is still called, causing:

OF: ERROR: Bad of_node_put() on /soc/gpio@e6050000
CPU: 1 PID: 20 Comm: kworker/1:1 Not tainted 5.1.0-rc2-koelsch+ #407
Hardware name: Generic R-Car Gen2 (Flattened Device Tree)
Workqueue: events deferred_probe_work_func
[<c020ec74>] (unwind_backtrace) from [<c020ae58>] (show_stack+0x10/0x14)
[<c020ae58>] (show_stack) from [<c07c1224>] (dump_stack+0x7c/0x9c)
[<c07c1224>] (dump_stack) from [<c07c5a80>] (kobject_put+0x94/0xbc)
[<c07c5a80>] (kobject_put) from [<c0470420>] (gpiochip_add_data_with_key+0x8d8/0xa3c)
[<c0470420>] (gpiochip_add_data_with_key) from [<c0473738>] (gpio_rcar_probe+0x1d4/0x314)
[<c0473738>] (gpio_rcar_probe) from [<c052fca8>] (platform_drv_probe+0x48/0x94)

and later, if a GPIO consumer tries to use a GPIO from a failed
controller:

WARNING: CPU: 0 PID: 1 at lib/refcount.c:156 kobject_get+0x38/0x4c
refcount_t: increment on 0; use-after-free.
Modules linked in:
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.1.0-rc2-koelsch+ #407
Hardware name: Generic R-Car Gen2 (Flattened Device Tree)
[<c020ec74>] (unwind_backtrace) from [<c020ae58>] (show_stack+0x10/0x14)
[<c020ae58>] (show_stack) from [<c07c1224>] (dump_stack+0x7c/0x9c)
[<c07c1224>] (dump_stack) from [<c0221580>] (__warn+0xd0/0xec)
[<c0221580>] (__warn) from [<c02215e0>] (warn_slowpath_fmt+0x44/0x6c)
[<c02215e0>] (warn_slowpath_fmt) from [<c07c58fc>] (kobject_get+0x38/0x4c)
[<c07c58fc>] (kobject_get) from [<c068b3ec>] (of_node_get+0x14/0x1c)
[<c068b3ec>] (of_node_get) from [<c0686f24>] (of_find_node_by_phandle+0xc0/0xf0)
[<c0686f24>] (of_find_node_by_phandle) from [<c0686fbc>] (of_phandle_iterator_next+0x68/0x154)
[<c0686fbc>] (of_phandle_iterator_next) from [<c0687fe4>] (__of_parse_phandle_with_args+0x40/0xd0)
[<c0687fe4>] (__of_parse_phandle_with_args) from [<c0688204>] (of_parse_phandle_with_args_map+0x100/0x3ac)
[<c0688204>] (of_parse_phandle_with_args_map) from [<c0471240>] (of_get_named_gpiod_flags+0x38/0x380)
[<c0471240>] (of_get_named_gpiod_flags) from [<c046f864>] (gpiod_get_from_of_node+0x24/0xd8)
[<c046f864>] (gpiod_get_from_of_node) from [<c0470aa4>] (devm_fwnode_get_index_gpiod_from_child+0xa0/0x144)
[<c0470aa4>] (devm_fwnode_get_index_gpiod_from_child) from [<c05f425c>] (gpio_keys_probe+0x418/0x7bc)
[<c05f425c>] (gpio_keys_probe) from [<c052fca8>] (platform_drv_probe+0x48/0x94)

Fix this by splitting the cleanup block, and adding a missing call to
gpiochip_irqchip_remove().

Fixes: 28355f81969962cf ("gpio: defer probe if pinctrl cannot be found")
Signed-off-by: Geert Uytterhoeven <[email protected]>
---
I'm not so sure about the need for the call to
gpiochip_irqchip_remove(), as add/remove are not really symmetrical.
Any comments?
---
drivers/gpio/gpiolib.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 144af07335815998..ed4da07effe0ac40 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1379,7 +1379,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,

status = gpiochip_add_irqchip(chip, lock_key, request_key);
if (status)
- goto err_remove_chip;
+ goto err_free_irqchip_mask;

status = of_gpiochip_add(chip);
if (status)
@@ -1387,7 +1387,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,

status = gpiochip_init_valid_mask(chip);
if (status)
- goto err_remove_chip;
+ goto err_remove_of_chip;

for (i = 0; i < chip->ngpio; i++) {
struct gpio_desc *desc = &gdev->descs[i];
@@ -1415,14 +1415,18 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
if (gpiolib_initialized) {
status = gpiochip_setup_dev(gdev);
if (status)
- goto err_remove_chip;
+ goto err_remove_acpi_chip;
}
return 0;

-err_remove_chip:
+err_remove_acpi_chip:
acpi_gpiochip_remove(chip);
+err_remove_of_chip:
gpiochip_free_hogs(chip);
of_gpiochip_remove(chip);
+err_remove_chip:
+ gpiochip_irqchip_remove(chip);
+err_free_irqchip_mask:
gpiochip_free_valid_mask(chip);
err_remove_irqchip_mask:
gpiochip_irqchip_free_valid_mask(chip);
--
2.17.1


2019-03-28 15:03:16

by Mukesh Ojha

[permalink] [raw]
Subject: Re: [PATCH 3/3] gpio: Remove obsolete comment about gpiochip_free_hogs() usage


On 3/28/2019 6:43 PM, Geert Uytterhoeven wrote:
> gpiochip_free_hogs() was always called from gpiochip_remove(), not
> of_gpiochip_remove(). It is now also called from the failure patch in
> gpiochip_add_data_with_key().
>
> Fixes: f625d4601759f1cf ("gpio: add GPIO hogging mechanism")
> Signed-off-by: Geert Uytterhoeven <[email protected]>


Looks good to be removed.
Reviewed-by: Mukesh Ojha <[email protected]>

-Mukesh

> ---
> drivers/gpio/gpiolib.c | 2 --
> 1 file changed, 2 deletions(-)
>
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index ed4da07effe0ac40..ec326bd84b1f1c50 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -4449,8 +4449,6 @@ int gpiod_hog(struct gpio_desc *desc, const char *name,
> /**
> * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
> * @chip: gpio chip to act on
> - *
> - * This is only used by of_gpiochip_remove to free hogged gpios
> */
> static void gpiochip_free_hogs(struct gpio_chip *chip)
> {

2019-03-28 15:32:46

by Mukesh Ojha

[permalink] [raw]
Subject: Re: [PATCH 2/3] gpio: Fix gpiochip_add_data_with_key() error path


On 3/28/2019 6:43 PM, Geert Uytterhoeven wrote:
> The err_remove_chip block is too coarse, and may perform cleanup that
> must not be done. E.g. if of_gpiochip_add() fails, of_gpiochip_remove()
> is still called, causing:
>
> OF: ERROR: Bad of_node_put() on /soc/gpio@e6050000
> CPU: 1 PID: 20 Comm: kworker/1:1 Not tainted 5.1.0-rc2-koelsch+ #407
> Hardware name: Generic R-Car Gen2 (Flattened Device Tree)
> Workqueue: events deferred_probe_work_func
> [<c020ec74>] (unwind_backtrace) from [<c020ae58>] (show_stack+0x10/0x14)
> [<c020ae58>] (show_stack) from [<c07c1224>] (dump_stack+0x7c/0x9c)
> [<c07c1224>] (dump_stack) from [<c07c5a80>] (kobject_put+0x94/0xbc)
> [<c07c5a80>] (kobject_put) from [<c0470420>] (gpiochip_add_data_with_key+0x8d8/0xa3c)
> [<c0470420>] (gpiochip_add_data_with_key) from [<c0473738>] (gpio_rcar_probe+0x1d4/0x314)
> [<c0473738>] (gpio_rcar_probe) from [<c052fca8>] (platform_drv_probe+0x48/0x94)
>
> and later, if a GPIO consumer tries to use a GPIO from a failed
> controller:
>
> WARNING: CPU: 0 PID: 1 at lib/refcount.c:156 kobject_get+0x38/0x4c
> refcount_t: increment on 0; use-after-free.
> Modules linked in:
> CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.1.0-rc2-koelsch+ #407
> Hardware name: Generic R-Car Gen2 (Flattened Device Tree)
> [<c020ec74>] (unwind_backtrace) from [<c020ae58>] (show_stack+0x10/0x14)
> [<c020ae58>] (show_stack) from [<c07c1224>] (dump_stack+0x7c/0x9c)
> [<c07c1224>] (dump_stack) from [<c0221580>] (__warn+0xd0/0xec)
> [<c0221580>] (__warn) from [<c02215e0>] (warn_slowpath_fmt+0x44/0x6c)
> [<c02215e0>] (warn_slowpath_fmt) from [<c07c58fc>] (kobject_get+0x38/0x4c)
> [<c07c58fc>] (kobject_get) from [<c068b3ec>] (of_node_get+0x14/0x1c)
> [<c068b3ec>] (of_node_get) from [<c0686f24>] (of_find_node_by_phandle+0xc0/0xf0)
> [<c0686f24>] (of_find_node_by_phandle) from [<c0686fbc>] (of_phandle_iterator_next+0x68/0x154)
> [<c0686fbc>] (of_phandle_iterator_next) from [<c0687fe4>] (__of_parse_phandle_with_args+0x40/0xd0)
> [<c0687fe4>] (__of_parse_phandle_with_args) from [<c0688204>] (of_parse_phandle_with_args_map+0x100/0x3ac)
> [<c0688204>] (of_parse_phandle_with_args_map) from [<c0471240>] (of_get_named_gpiod_flags+0x38/0x380)
> [<c0471240>] (of_get_named_gpiod_flags) from [<c046f864>] (gpiod_get_from_of_node+0x24/0xd8)
> [<c046f864>] (gpiod_get_from_of_node) from [<c0470aa4>] (devm_fwnode_get_index_gpiod_from_child+0xa0/0x144)
> [<c0470aa4>] (devm_fwnode_get_index_gpiod_from_child) from [<c05f425c>] (gpio_keys_probe+0x418/0x7bc)
> [<c05f425c>] (gpio_keys_probe) from [<c052fca8>] (platform_drv_probe+0x48/0x94)
>
> Fix this by splitting the cleanup block, and adding a missing call to
> gpiochip_irqchip_remove().
>
> Fixes: 28355f81969962cf ("gpio: defer probe if pinctrl cannot be found")
> Signed-off-by: Geert Uytterhoeven <[email protected]>
> ---
> I'm not so sure about the need for the call to
> gpiochip_irqchip_remove(), as add/remove are not really symmetrical.
> Any comments?
> ---
> drivers/gpio/gpiolib.c | 12 ++++++++----
> 1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index 144af07335815998..ed4da07effe0ac40 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -1379,7 +1379,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
>
> status = gpiochip_add_irqchip(chip, lock_key, request_key);
> if (status)
> - goto err_remove_chip;
> + goto err_free_irqchip_mask;

Name is quite confusing
this should be

s/err_free_irqchip_mask/err_free_gpiochip_mask


>
> status = of_gpiochip_add(chip);
> if (status)
> @@ -1387,7 +1387,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
>
> status = gpiochip_init_valid_mask(chip);
> if (status)
> - goto err_remove_chip;
> + goto err_remove_of_chip;
>
> for (i = 0; i < chip->ngpio; i++) {
> struct gpio_desc *desc = &gdev->descs[i];
> @@ -1415,14 +1415,18 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
> if (gpiolib_initialized) {
> status = gpiochip_setup_dev(gdev);
> if (status)
> - goto err_remove_chip;
> + goto err_remove_acpi_chip;
> }
> return 0;
>
> -err_remove_chip:
> +err_remove_acpi_chip:
> acpi_gpiochip_remove(chip);
> +err_remove_of_chip:
> gpiochip_free_hogs(chip);
> of_gpiochip_remove(chip);
> +err_remove_chip:
> + gpiochip_irqchip_remove(chip);
> +err_free_irqchip_mask:


same here.

After reviewing back and forth it looks good, apart from the naming.
Please fix.
Reviewed-by: Mukesh Ojha <[email protected]>

-Mukesh


> gpiochip_free_valid_mask(chip);
> err_remove_irqchip_mask:
> gpiochip_irqchip_free_valid_mask(chip);

2019-03-28 15:38:15

by Mukesh Ojha

[permalink] [raw]
Subject: Re: [PATCH 1/3] gpio: of: Fix of_gpiochip_add() error path


On 3/28/2019 6:43 PM, Geert Uytterhoeven wrote:
> If the call to of_gpiochip_scan_gpios() in of_gpiochip_add() fails, no
> error handling is performed. This lead to the need of callers to call
> of_gpiochip_remove() on failure, which causes "BAD of_node_put() on ..."
> if the failure happened before the call to of_node_get().
>
> Fix this by adding proper error handling.
>
> Note that calling gpiochip_remove_pin_ranges() multiple times causes no
> harm: subsequent calls are a no-op.
>
> Fixes: dfbd379ba9b7431e ("gpio: of: Return error if gpio hog configuration failed")
> Signed-off-by: Geert Uytterhoeven <[email protected]>


Reviewed-by: Mukesh Ojha <[email protected]>

-Mukesh

> ---
> drivers/gpio/gpiolib-of.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
> index 8b9c3ab70f6eade4..257be7bac032a855 100644
> --- a/drivers/gpio/gpiolib-of.c
> +++ b/drivers/gpio/gpiolib-of.c
> @@ -717,7 +717,13 @@ int of_gpiochip_add(struct gpio_chip *chip)
>
> of_node_get(chip->of_node);
>
> - return of_gpiochip_scan_gpios(chip);
> + status = of_gpiochip_scan_gpios(chip);
> + if (status) {
> + of_node_put(chip->of_node);
> + gpiochip_remove_pin_ranges(chip);
> + }
> +
> + return status;
> }
>
> void of_gpiochip_remove(struct gpio_chip *chip)

2019-03-28 17:01:17

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH 1/3] gpio: of: Fix of_gpiochip_add() error path

On Thu, Mar 28, 2019 at 2:13 PM Geert Uytterhoeven
<[email protected]> wrote:

> If the call to of_gpiochip_scan_gpios() in of_gpiochip_add() fails, no
> error handling is performed. This lead to the need of callers to call
> of_gpiochip_remove() on failure, which causes "BAD of_node_put() on ..."
> if the failure happened before the call to of_node_get().
>
> Fix this by adding proper error handling.
>
> Note that calling gpiochip_remove_pin_ranges() multiple times causes no
> harm: subsequent calls are a no-op.
>
> Fixes: dfbd379ba9b7431e ("gpio: of: Return error if gpio hog configuration failed")
> Signed-off-by: Geert Uytterhoeven <[email protected]>

Patch applied.

Yours,
Linus Walleij

2019-03-28 17:02:11

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH 3/3] gpio: Remove obsolete comment about gpiochip_free_hogs() usage

On Thu, Mar 28, 2019 at 2:13 PM Geert Uytterhoeven
<[email protected]> wrote:

> gpiochip_free_hogs() was always called from gpiochip_remove(), not
> of_gpiochip_remove(). It is now also called from the failure patch in
> gpiochip_add_data_with_key().
>
> Fixes: f625d4601759f1cf ("gpio: add GPIO hogging mechanism")
> Signed-off-by: Geert Uytterhoeven <[email protected]>

Patch applied for v5.2.

Yours,
Linus Walleij

2019-03-28 17:05:32

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH 2/3] gpio: Fix gpiochip_add_data_with_key() error path

On Thu, Mar 28, 2019 at 2:13 PM Geert Uytterhoeven
<[email protected]> wrote:

> The err_remove_chip block is too coarse, and may perform cleanup that
> must not be done. E.g. if of_gpiochip_add() fails, of_gpiochip_remove()
> is still called, causing:
>
> OF: ERROR: Bad of_node_put() on /soc/gpio@e6050000
> CPU: 1 PID: 20 Comm: kworker/1:1 Not tainted 5.1.0-rc2-koelsch+ #407
> Hardware name: Generic R-Car Gen2 (Flattened Device Tree)
> Workqueue: events deferred_probe_work_func
> [<c020ec74>] (unwind_backtrace) from [<c020ae58>] (show_stack+0x10/0x14)
> [<c020ae58>] (show_stack) from [<c07c1224>] (dump_stack+0x7c/0x9c)
> [<c07c1224>] (dump_stack) from [<c07c5a80>] (kobject_put+0x94/0xbc)
> [<c07c5a80>] (kobject_put) from [<c0470420>] (gpiochip_add_data_with_key+0x8d8/0xa3c)
> [<c0470420>] (gpiochip_add_data_with_key) from [<c0473738>] (gpio_rcar_probe+0x1d4/0x314)
> [<c0473738>] (gpio_rcar_probe) from [<c052fca8>] (platform_drv_probe+0x48/0x94)
>
> and later, if a GPIO consumer tries to use a GPIO from a failed
> controller:
>
> WARNING: CPU: 0 PID: 1 at lib/refcount.c:156 kobject_get+0x38/0x4c
> refcount_t: increment on 0; use-after-free.
> Modules linked in:
> CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.1.0-rc2-koelsch+ #407
> Hardware name: Generic R-Car Gen2 (Flattened Device Tree)
> [<c020ec74>] (unwind_backtrace) from [<c020ae58>] (show_stack+0x10/0x14)
> [<c020ae58>] (show_stack) from [<c07c1224>] (dump_stack+0x7c/0x9c)
> [<c07c1224>] (dump_stack) from [<c0221580>] (__warn+0xd0/0xec)
> [<c0221580>] (__warn) from [<c02215e0>] (warn_slowpath_fmt+0x44/0x6c)
> [<c02215e0>] (warn_slowpath_fmt) from [<c07c58fc>] (kobject_get+0x38/0x4c)
> [<c07c58fc>] (kobject_get) from [<c068b3ec>] (of_node_get+0x14/0x1c)
> [<c068b3ec>] (of_node_get) from [<c0686f24>] (of_find_node_by_phandle+0xc0/0xf0)
> [<c0686f24>] (of_find_node_by_phandle) from [<c0686fbc>] (of_phandle_iterator_next+0x68/0x154)
> [<c0686fbc>] (of_phandle_iterator_next) from [<c0687fe4>] (__of_parse_phandle_with_args+0x40/0xd0)
> [<c0687fe4>] (__of_parse_phandle_with_args) from [<c0688204>] (of_parse_phandle_with_args_map+0x100/0x3ac)
> [<c0688204>] (of_parse_phandle_with_args_map) from [<c0471240>] (of_get_named_gpiod_flags+0x38/0x380)
> [<c0471240>] (of_get_named_gpiod_flags) from [<c046f864>] (gpiod_get_from_of_node+0x24/0xd8)
> [<c046f864>] (gpiod_get_from_of_node) from [<c0470aa4>] (devm_fwnode_get_index_gpiod_from_child+0xa0/0x144)
> [<c0470aa4>] (devm_fwnode_get_index_gpiod_from_child) from [<c05f425c>] (gpio_keys_probe+0x418/0x7bc)
> [<c05f425c>] (gpio_keys_probe) from [<c052fca8>] (platform_drv_probe+0x48/0x94)
>
> Fix this by splitting the cleanup block, and adding a missing call to
> gpiochip_irqchip_remove().
>
> Fixes: 28355f81969962cf ("gpio: defer probe if pinctrl cannot be found")
> Signed-off-by: Geert Uytterhoeven <[email protected]>
> ---
> I'm not so sure about the need for the call to
> gpiochip_irqchip_remove(), as add/remove are not really symmetrical.
> Any comments?

I need to think about this.

Also Mukesh had some comments on naming.

I will try to read through the code closer for v2 and
figure out if it is what we want to do. But I think we need it.

Yours,
Linus Walleij

2019-03-28 18:21:50

by Geert Uytterhoeven

[permalink] [raw]
Subject: Re: [PATCH 2/3] gpio: Fix gpiochip_add_data_with_key() error path

Hi Mukesj,

On Thu, Mar 28, 2019 at 4:31 PM Mukesh Ojha <[email protected]> wrote:
> On 3/28/2019 6:43 PM, Geert Uytterhoeven wrote:
> > The err_remove_chip block is too coarse, and may perform cleanup that
> > must not be done. E.g. if of_gpiochip_add() fails, of_gpiochip_remove()
> > is still called, causing:
> >
> > OF: ERROR: Bad of_node_put() on /soc/gpio@e6050000
> > CPU: 1 PID: 20 Comm: kworker/1:1 Not tainted 5.1.0-rc2-koelsch+ #407
> > Hardware name: Generic R-Car Gen2 (Flattened Device Tree)
> > Workqueue: events deferred_probe_work_func
> > [<c020ec74>] (unwind_backtrace) from [<c020ae58>] (show_stack+0x10/0x14)
> > [<c020ae58>] (show_stack) from [<c07c1224>] (dump_stack+0x7c/0x9c)
> > [<c07c1224>] (dump_stack) from [<c07c5a80>] (kobject_put+0x94/0xbc)
> > [<c07c5a80>] (kobject_put) from [<c0470420>] (gpiochip_add_data_with_key+0x8d8/0xa3c)
> > [<c0470420>] (gpiochip_add_data_with_key) from [<c0473738>] (gpio_rcar_probe+0x1d4/0x314)
> > [<c0473738>] (gpio_rcar_probe) from [<c052fca8>] (platform_drv_probe+0x48/0x94)
> >
> > and later, if a GPIO consumer tries to use a GPIO from a failed
> > controller:
> >
> > WARNING: CPU: 0 PID: 1 at lib/refcount.c:156 kobject_get+0x38/0x4c
> > refcount_t: increment on 0; use-after-free.
> > Modules linked in:
> > CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.1.0-rc2-koelsch+ #407
> > Hardware name: Generic R-Car Gen2 (Flattened Device Tree)
> > [<c020ec74>] (unwind_backtrace) from [<c020ae58>] (show_stack+0x10/0x14)
> > [<c020ae58>] (show_stack) from [<c07c1224>] (dump_stack+0x7c/0x9c)
> > [<c07c1224>] (dump_stack) from [<c0221580>] (__warn+0xd0/0xec)
> > [<c0221580>] (__warn) from [<c02215e0>] (warn_slowpath_fmt+0x44/0x6c)
> > [<c02215e0>] (warn_slowpath_fmt) from [<c07c58fc>] (kobject_get+0x38/0x4c)
> > [<c07c58fc>] (kobject_get) from [<c068b3ec>] (of_node_get+0x14/0x1c)
> > [<c068b3ec>] (of_node_get) from [<c0686f24>] (of_find_node_by_phandle+0xc0/0xf0)
> > [<c0686f24>] (of_find_node_by_phandle) from [<c0686fbc>] (of_phandle_iterator_next+0x68/0x154)
> > [<c0686fbc>] (of_phandle_iterator_next) from [<c0687fe4>] (__of_parse_phandle_with_args+0x40/0xd0)
> > [<c0687fe4>] (__of_parse_phandle_with_args) from [<c0688204>] (of_parse_phandle_with_args_map+0x100/0x3ac)
> > [<c0688204>] (of_parse_phandle_with_args_map) from [<c0471240>] (of_get_named_gpiod_flags+0x38/0x380)
> > [<c0471240>] (of_get_named_gpiod_flags) from [<c046f864>] (gpiod_get_from_of_node+0x24/0xd8)
> > [<c046f864>] (gpiod_get_from_of_node) from [<c0470aa4>] (devm_fwnode_get_index_gpiod_from_child+0xa0/0x144)
> > [<c0470aa4>] (devm_fwnode_get_index_gpiod_from_child) from [<c05f425c>] (gpio_keys_probe+0x418/0x7bc)
> > [<c05f425c>] (gpio_keys_probe) from [<c052fca8>] (platform_drv_probe+0x48/0x94)
> >
> > Fix this by splitting the cleanup block, and adding a missing call to
> > gpiochip_irqchip_remove().
> >
> > Fixes: 28355f81969962cf ("gpio: defer probe if pinctrl cannot be found")
> > Signed-off-by: Geert Uytterhoeven <[email protected]>
> > ---
> > I'm not so sure about the need for the call to
> > gpiochip_irqchip_remove(), as add/remove are not really symmetrical.
> > Any comments?
> > ---
> > drivers/gpio/gpiolib.c | 12 ++++++++----
> > 1 file changed, 8 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> > index 144af07335815998..ed4da07effe0ac40 100644
> > --- a/drivers/gpio/gpiolib.c
> > +++ b/drivers/gpio/gpiolib.c
> > @@ -1379,7 +1379,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
> >
> > status = gpiochip_add_irqchip(chip, lock_key, request_key);
> > if (status)
> > - goto err_remove_chip;
> > + goto err_free_irqchip_mask;
>
> Name is quite confusing
> this should be
>
> s/err_free_irqchip_mask/err_free_gpiochip_mask

Thanks, makes perfect sense.

> After reviewing back and forth it looks good, apart from the naming.
> Please fix.

Done (locally).

> Reviewed-by: Mukesh Ojha <[email protected]>

Thanks!

Gr{oetje,eeting}s,

Geert

--
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