Commit 72eba6f6 authored by Johan Hovold's avatar Johan Hovold Committed by Linus Walleij

gpio: sysfs: fix race between gpiod export and unexport

Make sure to deregister the class device (and release the irq) while
holding the sysfs lock in gpio_unexport to prevent racing with
gpio_export.

Note that this requires the recently introduced per-gpio locking to
avoid a deadlock with the kernfs active protection when waiting for the
attribute operations to drain during deregistration.
Signed-off-by: default avatarJohan Hovold <johan@kernel.org>
Reviewed-by: default avatarAlexandre Courbot <acourbot@nvidia.com>
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent 6ffcb797
...@@ -674,9 +674,8 @@ EXPORT_SYMBOL_GPL(gpiod_export_link); ...@@ -674,9 +674,8 @@ EXPORT_SYMBOL_GPL(gpiod_export_link);
*/ */
void gpiod_unexport(struct gpio_desc *desc) void gpiod_unexport(struct gpio_desc *desc)
{ {
struct gpiod_data *data; struct gpiod_data *data;
int status = 0; struct device *dev;
struct device *dev = NULL;
if (!desc) { if (!desc) {
pr_warn("%s: invalid GPIO\n", __func__); pr_warn("%s: invalid GPIO\n", __func__);
...@@ -685,33 +684,35 @@ void gpiod_unexport(struct gpio_desc *desc) ...@@ -685,33 +684,35 @@ void gpiod_unexport(struct gpio_desc *desc)
mutex_lock(&sysfs_lock); mutex_lock(&sysfs_lock);
if (test_bit(FLAG_EXPORT, &desc->flags)) { if (!test_bit(FLAG_EXPORT, &desc->flags))
goto err_unlock;
dev = class_find_device(&gpio_class, NULL, desc, match_export); dev = class_find_device(&gpio_class, NULL, desc, match_export);
if (dev) { if (!dev)
clear_bit(FLAG_SYSFS_DIR, &desc->flags); goto err_unlock;
clear_bit(FLAG_EXPORT, &desc->flags);
} else data = dev_get_drvdata(dev);
status = -ENODEV;
} clear_bit(FLAG_SYSFS_DIR, &desc->flags);
clear_bit(FLAG_EXPORT, &desc->flags);
device_unregister(dev);
/*
* Release irq after deregistration to prevent race with edge_store.
*/
if (desc->flags & GPIO_TRIGGER_MASK)
gpio_sysfs_free_irq(dev);
mutex_unlock(&sysfs_lock); mutex_unlock(&sysfs_lock);
if (dev) { put_device(dev);
data = dev_get_drvdata(dev); kfree(data);
device_unregister(dev);
/*
* Release irq after deregistration to prevent race with
* edge_store.
*/
if (desc->flags & GPIO_TRIGGER_MASK)
gpio_sysfs_free_irq(dev);
put_device(dev);
kfree(data);
}
if (status) return;
gpiod_dbg(desc, "%s: status %d\n", __func__, status);
err_unlock:
mutex_unlock(&sysfs_lock);
} }
EXPORT_SYMBOL_GPL(gpiod_unexport); EXPORT_SYMBOL_GPL(gpiod_unexport);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment