Skip to content

Commit 0915e6f

Browse files
jhovoldlinusw
authored andcommitted
gpio: sysfs: fix gpio device-attribute leak
The gpio device attributes were never destroyed when the gpio was unexported (or on export failures). Use device_create_with_groups() to create the default device attributes of the gpio class device. Note that this also fixes the attribute-creation race with userspace for these attributes. Remove contingent attributes in export error path and on unexport. Fixes: d8f388d ("gpio: sysfs interface") Cc: stable <stable@vger.kernel.org> # v2.6.27+ Signed-off-by: Johan Hovold <johan@kernel.org> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 121b6a7 commit 0915e6f

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

drivers/gpio/gpiolib-sysfs.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static ssize_t gpio_value_store(struct device *dev,
128128
return status;
129129
}
130130

131-
static const DEVICE_ATTR(value, 0644,
131+
static DEVICE_ATTR(value, 0644,
132132
gpio_value_show, gpio_value_store);
133133

134134
static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
@@ -353,18 +353,15 @@ static ssize_t gpio_active_low_store(struct device *dev,
353353
return status ? : size;
354354
}
355355

356-
static const DEVICE_ATTR(active_low, 0644,
356+
static DEVICE_ATTR(active_low, 0644,
357357
gpio_active_low_show, gpio_active_low_store);
358358

359-
static const struct attribute *gpio_attrs[] = {
359+
static struct attribute *gpio_attrs[] = {
360360
&dev_attr_value.attr,
361361
&dev_attr_active_low.attr,
362362
NULL,
363363
};
364-
365-
static const struct attribute_group gpio_attr_group = {
366-
.attrs = (struct attribute **) gpio_attrs,
367-
};
364+
ATTRIBUTE_GROUPS(gpio);
368365

369366
/*
370367
* /sys/class/gpio/gpiochipN/
@@ -561,18 +558,15 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
561558
if (desc->chip->names && desc->chip->names[offset])
562559
ioname = desc->chip->names[offset];
563560

564-
dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
565-
desc, ioname ? ioname : "gpio%u",
566-
desc_to_gpio(desc));
561+
dev = device_create_with_groups(&gpio_class, desc->chip->dev,
562+
MKDEV(0, 0), desc, gpio_groups,
563+
ioname ? ioname : "gpio%u",
564+
desc_to_gpio(desc));
567565
if (IS_ERR(dev)) {
568566
status = PTR_ERR(dev);
569567
goto fail_unlock;
570568
}
571569

572-
status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
573-
if (status)
574-
goto fail_unregister_device;
575-
576570
if (direction_may_change) {
577571
status = device_create_file(dev, &dev_attr_direction);
578572
if (status)
@@ -583,13 +577,15 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
583577
!test_bit(FLAG_IS_OUT, &desc->flags))) {
584578
status = device_create_file(dev, &dev_attr_edge);
585579
if (status)
586-
goto fail_unregister_device;
580+
goto fail_remove_attr_direction;
587581
}
588582

589583
set_bit(FLAG_EXPORT, &desc->flags);
590584
mutex_unlock(&sysfs_lock);
591585
return 0;
592586

587+
fail_remove_attr_direction:
588+
device_remove_file(dev, &dev_attr_direction);
593589
fail_unregister_device:
594590
device_unregister(dev);
595591
fail_unlock:
@@ -723,6 +719,8 @@ void gpiod_unexport(struct gpio_desc *desc)
723719
mutex_unlock(&sysfs_lock);
724720

725721
if (dev) {
722+
device_remove_file(dev, &dev_attr_edge);
723+
device_remove_file(dev, &dev_attr_direction);
726724
device_unregister(dev);
727725
put_device(dev);
728726
}

0 commit comments

Comments
 (0)