[PATCH 6.18 218/227] gpio: cdev: Fix resource leaks on errors in gpiolib_cdev_register()

Greg Kroah-Hartman
2026-01-28T16:02:19Z

From: Tzung-Bi Shih <tzungbi@kernel.org>

commit 8a8c942cad4cd12f739a8bb60cac77fd173c4e07 upstream.

On error handling paths, gpiolib_cdev_register() doesn't free the
allocated resources which results leaks. Fix it.

Cc: stable@vger.kernel.org
Fixes: 7b9b77a8bba9 ("gpiolib: add a per-gpio_device line state notification workqueue")
Fixes: d83cee3d2bb1 ("gpio: protect the pointer to gpio_chip in gpio_device with SRCU")
Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
Link: https://lore.kernel.org/r/20260120092650.2305319-1-tzungbi@kernel.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
drivers/gpio/gpiolib-cdev.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -2821,13 +2821,18 @@ int gpiolib_cdev_register(struct gpio_de
return -ENOMEM;

ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
- if (ret)
+ if (ret) {
+ destroy_workqueue(gdev->line_state_wq);
return ret;
+ }

guard(srcu)(&gdev->srcu);
gc = srcu_dereference(gdev->chip, &gdev->srcu);
- if (!gc)
+ if (!gc) {
+ cdev_device_del(&gdev->chrdev, &gdev->dev);
+ destroy_workqueue(gdev->line_state_wq);
return -ENODEV;
+ }

gpiochip_dbg(gc, "added GPIO chardev (%d:%d)\n", MAJOR(devt), gdev->id);