From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Shrink the code and remove some goto labels by using guards around the
sysfs_lock mutex. While at it: use __free(kfree) when allocating sysfs
callback data.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
drivers/gpio/gpiolib-sysfs.c | 64 ++++++++++++++++++--------------------------
1 file changed, 26 insertions(+), 38 deletions(-)
diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c
index a0926a1061ae..72617f929a2d 100644
--- a/drivers/gpio/gpiolib-sysfs.c
+++ b/drivers/gpio/gpiolib-sysfs.c
@@ -551,7 +551,6 @@ static const struct class gpio_class = {
int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
{
struct gpio_device *gdev;
- struct gpiod_data *data;
struct device *dev;
int status;
@@ -575,24 +574,25 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
gdev = desc->gdev;
- mutex_lock(&sysfs_lock);
+ guard(mutex)(&sysfs_lock);
/* check if chip is being removed */
if (!gdev->mockdev) {
status = -ENODEV;
- goto err_unlock;
+ goto err_clear_bit;
}
if (!test_bit(FLAG_REQUESTED, &desc->flags)) {
gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__);
status = -EPERM;
- goto err_unlock;
+ goto err_clear_bit;
}
- data = kzalloc(sizeof(*data), GFP_KERNEL);
+ struct gpiod_data *data __free(kfree) = kzalloc(sizeof(*data),
+ GFP_KERNEL);
if (!data) {
status = -ENOMEM;
- goto err_unlock;
+ goto err_clear_bit;
}
data->desc = desc;
@@ -607,16 +607,13 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
"gpio%u", desc_to_gpio(desc));
if (IS_ERR(dev)) {
status = PTR_ERR(dev);
- goto err_free_data;
+ goto err_clear_bit;
}
- mutex_unlock(&sysfs_lock);
+ data = NULL;
return 0;
-err_free_data:
- kfree(data);
-err_unlock:
- mutex_unlock(&sysfs_lock);
+err_clear_bit:
clear_bit(FLAG_EXPORT, &desc->flags);
gpiod_dbg(desc, "%s: status %d\n", __func__, status);
return status;
@@ -680,36 +677,28 @@ void gpiod_unexport(struct gpio_desc *desc)
return;
}
- mutex_lock(&sysfs_lock);
+ scoped_guard(mutex, &sysfs_lock) {
+ if (!test_bit(FLAG_EXPORT, &desc->flags))
+ return;
- if (!test_bit(FLAG_EXPORT, &desc->flags))
- goto err_unlock;
+ dev = class_find_device(&gpio_class, NULL, desc, match_export);
+ if (!dev)
+ return;
- dev = class_find_device(&gpio_class, NULL, desc, match_export);
- if (!dev)
- goto err_unlock;
+ data = dev_get_drvdata(dev);
+ clear_bit(FLAG_EXPORT, &desc->flags);
+ device_unregister(dev);
- data = dev_get_drvdata(dev);
-
- clear_bit(FLAG_EXPORT, &desc->flags);
-
- device_unregister(dev);
-
- /*
- * Release irq after deregistration to prevent race with edge_store.
- */
- if (data->irq_flags)
- gpio_sysfs_free_irq(dev);
-
- mutex_unlock(&sysfs_lock);
+ /*
+ * Release irq after deregistration to prevent race with
+ * edge_store.
+ */
+ if (data->irq_flags)
+ gpio_sysfs_free_irq(dev);
+ }
put_device(dev);
kfree(data);
-
- return;
-
-err_unlock:
- mutex_unlock(&sysfs_lock);
}
EXPORT_SYMBOL_GPL(gpiod_unexport);
@@ -750,9 +739,8 @@ int gpiochip_sysfs_register(struct gpio_device *gdev)
if (IS_ERR(dev))
return PTR_ERR(dev);
- mutex_lock(&sysfs_lock);
+ guard(mutex)(&sysfs_lock);
gdev->mockdev = dev;
- mutex_unlock(&sysfs_lock);
return 0;
}
--
2.45.2
Hi Bartosz,
kernel test robot noticed the following build errors:
[auto build test ERROR on a39230ecf6b3057f5897bc4744a790070cfbe7a8]
url: https://github.com/intel-lab-lkp/linux/commits/Bartosz-Golaszewski/gpio-sysfs-use-cleanup-guards-for-gpiod_data-mutex/20241026-210033
base: a39230ecf6b3057f5897bc4744a790070cfbe7a8
patch link: https://lore.kernel.org/r/20241026-gpio-notify-sysfs-v3-2-ad8f127d12f5%40linaro.org
patch subject: [PATCH v3 2/5] gpio: sysfs: use cleanup guards for the sysfs_lock mutex
config: i386-buildonly-randconfig-004-20241028 (https://download.01.org/0day-ci/archive/20241028/202410280514.EkyQpKfw-lkp@intel.com/config)
compiler: clang version 19.1.2 (https://github.com/llvm/llvm-project 7ba7d8e2f7b6445b60679da826210cdde29eaf8b)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241028/202410280514.EkyQpKfw-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202410280514.EkyQpKfw-lkp@intel.com/
All errors (new ones prefixed by >>):
>> drivers/gpio/gpiolib-sysfs.c:588:3: error: cannot jump from this goto statement to its label
588 | goto err_clear_bit;
| ^
drivers/gpio/gpiolib-sysfs.c:591:21: note: jump bypasses initialization of variable with __attribute__((cleanup))
591 | struct gpiod_data *data __free(kfree) = kzalloc(sizeof(*data),
| ^
drivers/gpio/gpiolib-sysfs.c:582:3: error: cannot jump from this goto statement to its label
582 | goto err_clear_bit;
| ^
drivers/gpio/gpiolib-sysfs.c:591:21: note: jump bypasses initialization of variable with __attribute__((cleanup))
591 | struct gpiod_data *data __free(kfree) = kzalloc(sizeof(*data),
| ^
2 errors generated.
vim +588 drivers/gpio/gpiolib-sysfs.c
534
535 /**
536 * gpiod_export - export a GPIO through sysfs
537 * @desc: GPIO to make available, already requested
538 * @direction_may_change: true if userspace may change GPIO direction
539 * Context: arch_initcall or later
540 *
541 * When drivers want to make a GPIO accessible to userspace after they
542 * have requested it -- perhaps while debugging, or as part of their
543 * public interface -- they may use this routine. If the GPIO can
544 * change direction (some can't) and the caller allows it, userspace
545 * will see "direction" sysfs attribute which may be used to change
546 * the gpio's direction. A "value" attribute will always be provided.
547 *
548 * Returns:
549 * 0 on success, or negative errno on failure.
550 */
551 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
552 {
553 struct gpio_device *gdev;
554 struct device *dev;
555 int status;
556
557 /* can't export until sysfs is available ... */
558 if (!class_is_registered(&gpio_class)) {
559 pr_debug("%s: called too early!\n", __func__);
560 return -ENOENT;
561 }
562
563 if (!desc) {
564 pr_debug("%s: invalid gpio descriptor\n", __func__);
565 return -EINVAL;
566 }
567
568 CLASS(gpio_chip_guard, guard)(desc);
569 if (!guard.gc)
570 return -ENODEV;
571
572 if (test_and_set_bit(FLAG_EXPORT, &desc->flags))
573 return -EPERM;
574
575 gdev = desc->gdev;
576
577 guard(mutex)(&sysfs_lock);
578
579 /* check if chip is being removed */
580 if (!gdev->mockdev) {
581 status = -ENODEV;
582 goto err_clear_bit;
583 }
584
585 if (!test_bit(FLAG_REQUESTED, &desc->flags)) {
586 gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__);
587 status = -EPERM;
> 588 goto err_clear_bit;
589 }
590
591 struct gpiod_data *data __free(kfree) = kzalloc(sizeof(*data),
592 GFP_KERNEL);
593 if (!data) {
594 status = -ENOMEM;
595 goto err_clear_bit;
596 }
597
598 data->desc = desc;
599 mutex_init(&data->mutex);
600 if (guard.gc->direction_input && guard.gc->direction_output)
601 data->direction_can_change = direction_may_change;
602 else
603 data->direction_can_change = false;
604
605 dev = device_create_with_groups(&gpio_class, &gdev->dev,
606 MKDEV(0, 0), data, gpio_groups,
607 "gpio%u", desc_to_gpio(desc));
608 if (IS_ERR(dev)) {
609 status = PTR_ERR(dev);
610 goto err_clear_bit;
611 }
612
613 data = NULL;
614 return 0;
615
616 err_clear_bit:
617 clear_bit(FLAG_EXPORT, &desc->flags);
618 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
619 return status;
620 }
621 EXPORT_SYMBOL_GPL(gpiod_export);
622
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
© 2016 - 2026 Red Hat, Inc.