gpiolib: protect the list of GPIO devices with a mutex
v2 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.
Patch 2/2 here is actually a v2 of the original submission.
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 | 26 +----- drivers/gpio/gpiolib-sysfs.h | 6 ++ drivers/gpio/gpiolib.c | 166 ++++++++++++++++++----------------- drivers/gpio/gpiolib.h | 1 - 4 files changed, 94 insertions(+), 105 deletions(-)
Signature · 1 line
2.40.1
PATCH v2 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 v2 1/2] gpiolib: rename static functions that are called with the lock taken
Quoted 7 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.
Okay,
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 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 | 26 +----- drivers/gpio/gpiolib-sysfs.h | 6 ++ drivers/gpio/gpiolib.c | 162 ++++++++++++++++++----------------- drivers/gpio/gpiolib.h | 1 - 4 files changed, 92 insertions(+), 103 deletions(-)
drivers/gpio/gpiolib-sysfs.c +2 -24
@@ -790,9 +790,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 +802,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 +84 -78
272 lines not on this page · raw message
drivers/gpio/gpiolib.h +0 -1
@@ -135,7 +135,6 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep,int gpiod_set_transitory (struct gpio_desc *desc,bool transitory);extern spinlock_t gpio_lock; -extern struct list_head gpio_devices;void gpiod_line_state_notify (struct gpio_desc *desc,unsigned long action);
Signature · 1 line
2.40.1
Re: [PATCH v2 2/2] gpiolib: use a mutex to protect the list of GPIO devices
Quoted 8 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.
Quoted 4 lines
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]>
If there are no objections, I'll apply it tomorrow.
Bart
Re: [PATCH v2 2/2] gpiolib: use a mutex to protect the list of GPIO devices
In reply to Bartosz GolaszewskiQuoted 6 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.
...
Quoted 2 lines
While at it: fold the sysfs registering of existing devices into
gpiolib.cand makegpio_devicesstatic within its compilation unit.
TBH I do not like injecting sysfs (legacy!) code into gpiolib. It makes things at very least confusing.
That _ugly_ ifdeffery and sysfs in the function name are not okay.
If you want do that, please create a separate change and explain the rationale behind with answering to the Q "Why do we need all that and why is it better than any alternatives?".
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/2] gpiolib: use a mutex to protect the list of GPIO devices
Quoted 14 lines · Andy Shevchenko
Quoted 6 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....
Quoted 2 lines
While at it: fold the sysfs registering of existing devices into
gpiolib.cand makegpio_devicesstatic within its compilation unit.
Quoted 9 lines
TBH I do not like injecting sysfs (legacy!) code into gpiolib. It makes things at very least confusing.
That _ugly_ ifdeffery and sysfs in the function name are not okay.
If you want do that, please create a separate change and explain the rationale behind with answering to the Q "Why do we need all that and why is it better than any alternatives?".
I can move it back to gpiolib-sysfs.c but this way we'll have to keep the GPIO device mutex public in gpiolib.h.
Bart
Quoted 5 lines
--
With Best Regards,
Andy Shevchenko
Re: [PATCH v2 2/2] gpiolib: use a mutex to protect the list of GPIO devices
Quoted 10 lines · Bartosz Golaszewski
Quoted 8 lines · Andy Shevchenko
Quoted 6 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.
...
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
While at it: fold the sysfs registering of existing devices into
gpiolib.cand makegpio_devicesstatic within its compilation unit.TBH I do not like injecting sysfs (legacy!) code into gpiolib. It makes things at very least confusing.
Quoted 8 lines
Quoted 5 lines
That _ugly_ ifdeffery and sysfs in the function name are not okay.
If you want do that, please create a separate change and explain the rationale behind with answering to the Q "Why do we need all that and why is it better than any alternatives?".
I can move it back to
gpiolib-sysfs.cbut this way we'll have to keep the GPIO device mutex public ingpiolib.h.
And I'm fine with that. Again, we can discuss this in a separate change that will do that (make that mutex local with the explanation why).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/2] gpiolib: use a mutex to protect the list of GPIO devices
Quoted 27 lines · Andy Shevchenko
Quoted 10 lines · Bartosz Golaszewski
Quoted 8 lines · Andy Shevchenko
Quoted 6 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....
Quoted 12 lines
Quoted 11 lines
Quoted 2 lines
While at it: fold the sysfs registering of existing devices into
gpiolib.cand makegpio_devicesstatic within its compilation unit.TBH I do not like injecting sysfs (legacy!) code into gpiolib. It makes things at very least confusing.
That _ugly_ ifdeffery and sysfs in the function name are not okay.
If you want do that, please create a separate change and explain the rationale behind with answering to the Q "Why do we need all that and why is it better than any alternatives?".
Quoted 6 lines
Quoted 2 lines
I can move it back to
gpiolib-sysfs.cbut this way we'll have to keep the GPIO device mutex public ingpiolib.h.And I'm fine with that. Again, we can discuss this in a separate change that will do that (make that mutex local with the explanation why).
No, I won't be sending one. I'll send another iteration of this with sysfs stuff contained to gpiolib-sysfs.c.
Bart
Quoted 5 lines
--
With Best Regards,
Andy Shevchenko