Skip to content

Commit 3386fb8

Browse files
author
Bartosz Golaszewski
committed
gpiolib: fix reference leaks when removing GPIO chips still in use
After we remove a GPIO chip that still has some requested descriptors, gpiod_free_commit() will fail and we will never put the references to the GPIO device and the owning module in gpiod_free(). Rework this function to: - not warn on desc == NULL as this is a use-case on which most free functions silently return - put the references to desc->gdev and desc->gdev->owner unconditionally so that the release callback actually gets called when the remaining references are dropped by external GPIO users Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org> Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 20d9b3b commit 3386fb8

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

drivers/gpio/gpiolib.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,12 +2167,18 @@ static bool gpiod_free_commit(struct gpio_desc *desc)
21672167

21682168
void gpiod_free(struct gpio_desc *desc)
21692169
{
2170-
if (desc && desc->gdev && gpiod_free_commit(desc)) {
2171-
module_put(desc->gdev->owner);
2172-
gpio_device_put(desc->gdev);
2173-
} else {
2170+
/*
2171+
* We must not use VALIDATE_DESC_VOID() as the underlying gdev->chip
2172+
* may already be NULL but we still want to put the references.
2173+
*/
2174+
if (!desc)
2175+
return;
2176+
2177+
if (!gpiod_free_commit(desc))
21742178
WARN_ON(extra_checks);
2175-
}
2179+
2180+
module_put(desc->gdev->owner);
2181+
gpio_device_put(desc->gdev);
21762182
}
21772183

21782184
/**

0 commit comments

Comments
 (0)