Rockchip GPIO driver resource leaks
cover Rockchip GPIO driver resource leaks
From: Heiko Stuebner <[email protected]>
Patches 2+3 resolve resource leaks, while patch 1 is structural change without functionality change, that is needed for 2+3 to apply cleanly.
Marco Scardovi (2):
gpio: rockchip: teardown bugs and resource leaks
gpio: rockchip: fix generic IRQ chip leak on remove
Ye Zhang (1):
gpio: rockchip: change the GPIO version judgment logic
drivers/gpio/gpio-rockchip.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/3 gpio: rockchip: change the GPIO version judgment logic
From: Ye Zhang <[email protected]>
[ Upstream commit 41209307cad7f14c387c68375a93b50e54261a53 ]
Have a list of valid IDs and default to -ENODEV.
--- drivers/gpio/gpio-rockchip.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpio/gpio-rockchip.c +9 -3
@@ -653,8 +653,9 @@ static int rockchip_get_bank_data(struct rockchip_pin_bank *bank) id =readl (bank->reg_base + gpio_regs_v2.version_id); -/* If not gpio v2, that is default to v1. */ -if (id ==GPIO_TYPE_V2 || id ==GPIO_TYPE_V2_1 ) { +switch (id) { +case GPIO_TYPE_V2 : +case GPIO_TYPE_V2_1 : bank->gpio_regs = &gpio_regs_v2; bank->gpio_type =GPIO_TYPE_V2 ; bank->db_clk =of_clk_get (bank->of_node,1 );
@@ -662,9 +663,14 @@ static int rockchip_get_bank_data(struct rockchip_pin_bank *bank)dev_err (bank->dev,"cannot find debounce clk\n" );return -EINVAL ; } - }else { +break ; +case GPIO_TYPE_V1 : bank->gpio_regs = &gpio_regs_v1; bank->gpio_type =GPIO_TYPE_V1 ; +break ; +default : +dev_err (bank->dev,"unsupported version ID: 0x%08x\n" , id); +return -ENODEV ; }return 0 ;
Signature · 1 line
2.53.0
PATCH 2/3 gpio: rockchip: teardown bugs and resource leaks
In reply to Heiko StuebnerFrom: Marco Scardovi <[email protected]>
[ Upstream commit 9500077678230e36d22bf16d2b9539c13e59a801 ]
Address several teardown issues and resource leaks in the driver's remove path and error handling:
1. Debounce clock reference leak: The debounce clock (bank->db_clk) is obtained using of_clk_get() which increments the clock's reference count, but clk_put() is never called. Register a devm action to cleanly release it on unbind. Note that of_clk_get(..., 1) remains necessary over devm_clk_get() because the DT binding does not define clock-names, precluding name-based lookup.
OA
2. Unregistered chained IRQ handler: The chained IRQ handler is not disconnected in remove(). If a stray interrupt fires after the driver is removed, the kernel attempts to execute a stale handler, leading to a panic. Fix this by clearing the handler in remove().
3. IRQ domain leak: The linear IRQ domain and its generic chips are allocated manually during probe but never removed. Remove the IRQ domain during driver teardown to free the associated generic chips and mappings.
--- drivers/gpio/gpio-rockchip.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/gpio/gpio-rockchip.c +16 -1
@@ -629,10 +629,17 @@ static int rockchip_gpiolib_register(struct rockchip_pin_bank *bank)return ret; } +static void rockchip_clk_put (void *data) +{ +struct clk *clk = data; + +clk_put (clk); +} +static int rockchip_get_bank_data (struct rockchip_pin_bank *bank) {struct resource res; -int id =0 ; +int id =0 , ret;if (of_address_to_resource (bank->of_node,0 , &res)) {dev_err (bank->dev,"cannot find IO resource for bank\n" );
@@ -663,6 +670,11 @@ static int rockchip_get_bank_data(struct rockchip_pin_bank *bank)dev_err (bank->dev,"cannot find debounce clk\n" );return -EINVAL ; } + + ret =devm_add_action_or_reset (bank->dev, rockchip_clk_put, + bank->db_clk); +if (ret) +return ret;break ;case GPIO_TYPE_V1 : bank->gpio_regs = &gpio_regs_v1;
@@ -779,6 +791,9 @@ static void rockchip_gpio_remove(struct platform_device *pdev) {struct rockchip_pin_bank *bank =platform_get_drvdata (pdev); +irq_set_chained_handler_and_data (bank->irq,NULL ,NULL ); +if (bank->domain) +irq_domain_remove (bank->domain);gpiochip_remove (&bank->gpio_chip); }
Signature · 1 line
2.53.0
PATCH 3/3 gpio: rockchip: fix generic IRQ chip leak on remove
In reply to Heiko StuebnerFrom: Marco Scardovi <[email protected]>
[ Upstream commit 1c1e0fc88d6ef65bf15d517853251f75ab9d18c3 ]
The driver allocates domain generic chips using irq_alloc_domain_generic_chips() during probe. However, on driver remove/teardown, the generic chips are not automatically freed when the IRQ domain is removed because the domain flags do not include IRQ_DOMAIN_FLAG_DESTROY_GC.
This causes both the domain generic chips structure and the associated generic chips to be leaked. Additionally, the generic chips remain on the global gc_list and may later be visited by generic IRQ chip suspend, resume, or shutdown callbacks after the GPIO bank has been removed, potentially resulting in a use-after-free and kernel crash.
Fix the resource leak by explicitly calling irq_domain_remove_generic_chips() before removing the IRQ domain in rockchip_gpio_remove().
--- drivers/gpio/gpio-rockchip.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpio/gpio-rockchip.c +3 -1
@@ -792,8 +792,10 @@ static void rockchip_gpio_remove(struct platform_device *pdev)struct rockchip_pin_bank *bank =platform_get_drvdata (pdev);irq_set_chained_handler_and_data (bank->irq,NULL ,NULL ); -if (bank->domain) +if (bank->domain) { +irq_domain_remove_generic_chips (bank->domain);irq_domain_remove (bank->domain); + }gpiochip_remove (&bank->gpio_chip); }
Signature · 1 line
2.53.0
Quoted 3 lines
Patches 2+3 resolve resource leaks, while patch 1 is structural change without functionality change, that is needed for 2+3 to apply cleanly.
Queued the series for 6.12, thanks.
Signature · 2 lines
Thanks, Sasha