[PATCH] gpiolib: tie module references to GPIO devices, not requested descs
From: Bartosz Golaszewski
Date: Fri Aug 18 2023 - 15:02:08 EST
From: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
After a deeper look at commit 3386fb86ecde ("gpiolib: fix reference
leaks when removing GPIO chips still in use") I'm now convinced that
gpiolib gets module reference counting wrong.
As we only take the reference to the owner module when a descriptor is
requested and put it when it's freed, we can easily trigger a crash by
removing a module which registered a driver bound to a GPIO chip which
is unused as nothing prevents us from doing so.
For correct behavior, we should take the reference to the module when
we're creating a GPIO device and only put it when that device is
released as it's at this point that we can safely remove the module's
code from memory.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
---
drivers/gpio/gpiolib.c | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 76e0c38026c3..cb0072d2d137 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -583,6 +583,7 @@ static void gpiodev_release(struct device *dev)
list_del(&gdev->list);
spin_unlock_irqrestore(&gpio_lock, flags);
+ module_put(gdev->owner);
ida_free(&gpio_ida, gdev->id);
kfree_const(gdev->label);
kfree(gdev->descs);
@@ -753,6 +754,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
else
gdev->owner = THIS_MODULE;
+ ret = try_module_get(gdev->owner);
+ if (!ret)
+ goto err_free_dev_name;
+
/*
* Try the device properties if the driver didn't supply the number
* of GPIO lines.
@@ -769,7 +774,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
*/
ngpios = 0;
else if (ret)
- goto err_free_dev_name;
+ goto err_put_module;
gc->ngpio = ngpios;
}
@@ -777,7 +782,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
if (gc->ngpio == 0) {
chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
ret = -EINVAL;
- goto err_free_dev_name;
+ goto err_put_module;
}
if (gc->ngpio > FASTPATH_NGPIO)
@@ -787,7 +792,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
if (!gdev->descs) {
ret = -ENOMEM;
- goto err_free_dev_name;
+ goto err_put_module;
}
gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
@@ -937,6 +942,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
kfree_const(gdev->label);
err_free_descs:
kfree(gdev->descs);
+err_put_module:
+ module_put(gdev->owner);
err_free_dev_name:
kfree(dev_name(&gdev->dev));
err_free_ida:
@@ -2101,20 +2108,16 @@ static int validate_desc(const struct gpio_desc *desc, const char *func)
int gpiod_request(struct gpio_desc *desc, const char *label)
{
- int ret = -EPROBE_DEFER;
+ int ret;
VALIDATE_DESC(desc);
- if (try_module_get(desc->gdev->owner)) {
- ret = gpiod_request_commit(desc, label);
- if (ret)
- module_put(desc->gdev->owner);
- else
- gpio_device_get(desc->gdev);
- }
-
+ ret = gpiod_request_commit(desc, label);
if (ret)
- gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
+ return ret;
+
+ gpio_device_get(desc->gdev);
+ gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
return ret;
}
@@ -2177,7 +2180,6 @@ void gpiod_free(struct gpio_desc *desc)
if (!gpiod_free_commit(desc))
WARN_ON(extra_checks);
- module_put(desc->gdev->owner);
gpio_device_put(desc->gdev);
}
--
2.39.2