gpiolib: protect the list of GPIO devices with a mutex
v3 cover gpiolib: protect the list of GPIO devices with a mutex
From: Bartosz Golaszewski <[email protected]>
I figured that - since the descriptor locking is going to take some more time - we should at least start the conversion and protect the GPIO device list with a mutex.
v2 -> v3:
- move the new sysfs function into gpiolib-sysfs.c
v1 -> v2:
- add a patch renaming two functions
- protect the list in gpio_device_find() too
- coding style tweaks
Bartosz Golaszewski (2):
gpiolib: rename static functions that are called with the lock taken
gpiolib: use a mutex to protect the list of GPIO devices
drivers/gpio/gpiolib-sysfs.c | 45 +++++------ drivers/gpio/gpiolib-sysfs.h | 6 ++ drivers/gpio/gpiolib.c | 143 ++++++++++++++++------------------- drivers/gpio/gpiolib.h | 2 + 4 files changed, 93 insertions(+), 103 deletions(-)
Signature · 1 line
2.40.1
PATCH v3 1/2 gpiolib: rename static functions that are called with the lock taken
From: Bartosz Golaszewski <[email protected]>
Rename two functions that read or modify the global GPIO device list but don't take the lock themselves (and need to be called with it already acquired). Use the _unlocked() suffix which seems to be used quite consistently across the kernel despite there also existing the _locked() suffix for the same purpose.
--- drivers/gpio/gpiolib.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +4 -4
@@ -290,7 +290,7 @@ struct gpio_chip *gpio_device_get_chip(struct gpio_device *gdev)EXPORT_SYMBOL_GPL (gpio_device_get_chip);/* dynamic allocation of GPIOs, e.g. on a hotplugged device */ -static int gpiochip_find_base (int ngpio) +static int gpiochip_find_base_unlocked (int ngpio) {struct gpio_device *gdev;int base =GPIO_DYNAMIC_BASE ;
@@ -363,7 +363,7 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);* Return -EBUSY if the new chip overlaps with some other chip's integer * space. */ -static int gpiodev_add_to_list (struct gpio_device *gdev) +static int gpiodev_add_to_list_unlocked (struct gpio_device *gdev) {struct gpio_device *prev, *next;
@@ -907,7 +907,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,*/ base = gc->base;if (base <0 ) { - base =gpiochip_find_base (gc->ngpio); + base =gpiochip_find_base_unlocked (gc->ngpio);if (base <0 ) {spin_unlock_irqrestore (&gpio_lock, flags); ret = base;
@@ -927,7 +927,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, } gdev->base = base; - ret =gpiodev_add_to_list (gdev); + ret =gpiodev_add_to_list_unlocked (gdev);if (ret) {spin_unlock_irqrestore (&gpio_lock, flags);chip_err (gc,"GPIO integer space overlap, cannot add chip\n" );
Signature · 1 line
2.40.1
Re: [PATCH v3 1/2] gpiolib: rename static functions that are called with the lock taken
Quoted 10 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Rename two functions that read or modify the global GPIO device list but don't take the lock themselves (and need to be called with it already acquired). Use the
_unlocked()suffix which seems to be used quite consistently across the kernel despite there also existing the_locked()suffix for the same purpose.Signed-off-by Bartosz Golaszewski <[email protected]>Reviewed-by Andy Shevchenko <[email protected]>
Yours,
Linus Walleij
PATCH v3 2/2 gpiolib: use a mutex to protect the list of GPIO devices
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
The global list of GPIO devices is never modified or accessed from atomic context so it's fine to protect it using a mutex. Add a new global lock dedicated to the gpio_devices list and use it whenever accessing or modifying it.
While at it: fold the sysfs registering of existing devices into gpiolib.c and make gpio_devices static within its compilation unit.
--- drivers/gpio/gpiolib-sysfs.c | 45 ++++++------ drivers/gpio/gpiolib-sysfs.h | 6 ++ drivers/gpio/gpiolib.c | 139 ++++++++++++++++------------------- drivers/gpio/gpiolib.h | 2 + 4 files changed, 91 insertions(+), 101 deletions(-)
drivers/gpio/gpiolib-sysfs.c +21 -24
@@ -766,6 +766,25 @@ int gpiochip_sysfs_register(struct gpio_device *gdev)return 0 ; } +int gpiochip_sysfs_register_all (void ) +{ +struct gpio_device *gdev; +int ret; + +guard (mutex)(&gpio_devices_lock); + +list_for_each_entry (gdev, &gpio_devices, list) { +if (gdev->mockdev) +continue ; + + ret =gpiochip_sysfs_register (gdev); +if (ret) +return ret; + } + +return 0 ; +} +void gpiochip_sysfs_unregister (struct gpio_device *gdev) {struct gpio_desc *desc;
@@ -790,9 +809,7 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev)static int __init gpiolib_sysfs_init (void ) { -int status; -unsigned long flags; -struct gpio_device *gdev; +int status; status =class_register (&gpio_class);if (status <0 )
@@ -804,26 +821,6 @@ static int __init gpiolib_sysfs_init(void)* We run before arch_initcall() so chip->dev nodes can have * registered, and so arch_initcall() can always gpiod_export(). */ -spin_lock_irqsave (&gpio_lock, flags); -list_for_each_entry (gdev, &gpio_devices, list) { -if (gdev->mockdev) -continue ; - -/* -* TODO we yield gpio_lock here because -* gpiochip_sysfs_register() acquires a mutex. This is unsafe -* and needs to be fixed. -* -* Also it would be nice to use gpio_device_find() here so we -* can keep gpio_chips local to gpiolib.c, but the yield of -* gpio_lock prevents us from doing this. -*/ -spin_unlock_irqrestore (&gpio_lock, flags); - status =gpiochip_sysfs_register (gdev); -spin_lock_irqsave (&gpio_lock, flags); - } -spin_unlock_irqrestore (&gpio_lock, flags); - -return status; +return gpiochip_sysfs_register_all (); }postcore_initcall (gpiolib_sysfs_init);
drivers/gpio/gpiolib-sysfs.h +6 -0
@@ -8,6 +8,7 @@ struct gpio_device;#ifdef CONFIG_GPIO_SYSFS int gpiochip_sysfs_register (struct gpio_device *gdev); +int gpiochip_sysfs_register_all (void );void gpiochip_sysfs_unregister (struct gpio_device *gdev);#else
@@ -17,6 +18,11 @@ static inline int gpiochip_sysfs_register(struct gpio_device *gdev)return 0 ; } +static inline int gpiochip_sysfs_register_all (void ) +{ +return 0 ; +} +static inline void gpiochip_sysfs_unregister (struct gpio_device *gdev) { }
drivers/gpio/gpiolib.c +62 -77
244 lines not on this page · raw message
drivers/gpio/gpiolib.h +2 -0
@@ -15,6 +15,7 @@#include <linux/gpio/consumer.h> /* for enum gpiod_flags */ #include <linux/gpio/driver.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/notifier.h> #include <linux/rwsem.h>
@@ -136,6 +137,7 @@ int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);extern spinlock_t gpio_lock;extern struct list_head gpio_devices; +extern struct mutex gpio_devices_lock;void gpiod_line_state_notify (struct gpio_desc *desc,unsigned long action);
Signature · 1 line
2.40.1
Re: [PATCH v3 2/2] gpiolib: use a mutex to protect the list of GPIO devices
Quoted 9 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
The global list of GPIO devices is never modified or accessed from atomic context so it's fine to protect it using a mutex. Add a new global lock dedicated to the
gpio_deviceslist and use it whenever accessing or modifying it.While at it: fold the sysfs registering of existing devices into
gpiolib.cand makegpio_devicesstatic within its compilation unit.
LGTM,
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 2/2] gpiolib: use a mutex to protect the list of GPIO devices
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Quoted 8 lines
Quoted 7 lines
The global list of GPIO devices is never modified or accessed from atomic context so it's fine to protect it using a mutex. Add a new global lock dedicated to the
gpio_deviceslist and use it whenever accessing or modifying it.While at it: fold the sysfs registering of existing devices into
gpiolib.cand makegpio_devicesstatic within its compilation unit.
This part is no longer true, I'll remove it and apply the patches.
Bart
Quoted 8 lines
LGTM,
Reviewed-by Andy Shevchenko <[email protected]>--
With Best Regards,
Andy Shevchenko
Re: [PATCH v3 2/2] gpiolib: use a mutex to protect the list of GPIO devices
In reply to Bartosz GolaszewskiQuoted 11 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
The global list of GPIO devices is never modified or accessed from atomic context so it's fine to protect it using a mutex. Add a new global lock dedicated to the
gpio_deviceslist and use it whenever accessing or modifying it.While at it: fold the sysfs registering of existing devices into
gpiolib.cand makegpio_devicesstatic within its compilation unit.Signed-off-by Bartosz Golaszewski <[email protected]>
Excellent patch, and a conclusion of the previous discussions on this.
Yours,
Linus Walleij