gpio: sysfs: extend the critical section for unregistering sysfs devices
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Wed, 24 Jan 2024 13:08:45 +0000 (14:08 +0100)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Mon, 12 Feb 2024 09:50:55 +0000 (10:50 +0100)
Checking the gdev->mockdev pointer for NULL must be part of the critical
section.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
drivers/gpio/gpiolib-sysfs.c

index 3ae7704b299632edd5725985a503a6cf94a4cce1..12ecb49fe75d0d8dc86abc7af21434f6fd55fc11 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/device.h>
 #include <linux/idr.h>
 #include <linux/init.h>
@@ -768,15 +769,15 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev)
        struct gpio_desc *desc;
        struct gpio_chip *chip = gdev->chip;
 
-       if (!gdev->mockdev)
-               return;
+       scoped_guard(mutex, &sysfs_lock) {
+               if (!gdev->mockdev)
+                       return;
 
-       device_unregister(gdev->mockdev);
+               device_unregister(gdev->mockdev);
 
-       /* prevent further gpiod exports */
-       mutex_lock(&sysfs_lock);
-       gdev->mockdev = NULL;
-       mutex_unlock(&sysfs_lock);
+               /* prevent further gpiod exports */
+               gdev->mockdev = NULL;
+       }
 
        /* unregister gpiod class devices owned by sysfs */
        for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) {