Skip to contentSkip to conversation

gpio: sysfs: fix forward declaration of struct gpio_device

PATCH In review Bartosz Golaszewski 3 participants 10 messages Nov 27, 2023 – Nov 28, 2023 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 gpio: sysfs: fix forward declaration of struct gpio_device

From: Bartosz Golaszewski <[email protected]>

The forward declaration for struct gpio_device should be provided for both branches of the #ifdef.

Fixes 08a149c40bdb ("gpiolib: Clean up headers")
---
 drivers/gpio/gpiolib-sysfs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-sysfs.h +2 -2
index 0f213bdb4732..b794b396d6a5 100644
@@ -3,10 +3,10 @@
 #ifndef GPIOLIB_SYSFS_H
 #define GPIOLIB_SYSFS_H
 
-#ifdef CONFIG_GPIO_SYSFS
-
 struct gpio_device;
 
+#ifdef CONFIG_GPIO_SYSFS
+
 int gpiochip_sysfs_register(struct gpio_device *gdev);
 void gpiochip_sysfs_unregister(struct gpio_device *gdev);
 
Signature · 1 line
2.40.1
#1 lore raw

PATCH 2/2 gpio: use a mutex to protect the list of GPIO devices

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_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       | 158 ++++++++++++++++++-----------------
 drivers/gpio/gpiolib.h       |   1 -
 4 files changed, 89 insertions(+), 102 deletions(-)
drivers/gpio/gpiolib-sysfs.c +2 -24
index 6f309a3b2d9a..c538568604e8 100644
@@ -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
index b794b396d6a5..ab157cec0b4b 100644
@@ -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 +81 -77

262 lines not on this page · raw message

drivers/gpio/gpiolib.h +0 -1
index 3ccacf3c1288..9278796db079 100644
@@ -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
#2 lore raw

Re: [PATCH 2/2] gpio: use a mutex to protect the list of GPIO devices

Quoted 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_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.

Nice! I might have found some snag:

gpio_device_find() still does guard(spinlock_irqsave)(&gpio_lock); shouldn't that be switched to the mutex?

On top of this I can update my patch to the delete the comment for gpio_lock to just rename that thing to gpio_descriptor_lock and document it as such.

But when I think about it: gpio[_decriptor]_lock can now (after this patch) be moved into struct gpio_chip as it is really just protecting the descriptors on the same chip from simultaneous modification, especially desc->flags. This is a BIG WIN because it makes it a local lock not a global one, do you wanna try it or should I? (On top of these two patches, then.)

Yours,
Linus Walleij

#3 lore raw

Re: [PATCH 2/2] gpio: use a mutex to protect the list of GPIO devices

Quoted 13 lines · Linus Walleij
Quoted 10 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_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.

Quoted 7 lines
Quoted 1 line

Nice! I might have found some snag:

gpio_device_find() still does guard(spinlock_irqsave)(&gpio_lock); shouldn't that be switched to the mutex?

Good catch!

Quoted 4 lines

On top of this I can update my patch to the delete the comment for gpio_lock to just rename that thing to gpio_descriptor_lock and document it as such.

No need, this will soon go away anyway. See below.

Quoted 7 lines

But when I think about it: gpio[_decriptor]_lock can now (after this patch) be moved into struct gpio_chip as it is really just protecting the descriptors on the same chip from simultaneous modification, especially desc->flags. This is a BIG WIN because it makes it a local lock not a global one, do you wanna try it or should I? (On top of these two patches, then.)

I will have the series making locking in GPIOLIB more fine-grained ready tomorrow or on Thursday. It will have separate locks for each descriptor. We will use spinlock or mutex per descriptor depending on the value of gc->can_sleep. I think it should work fine as a sleeping chip can always use a mutex and a non-sleeping one cannot have sleeping callbacks (correct me if I'm wrong).

We don't need to lock the GPIO device or chip separately - the descriptor structs will stay alive as long as there's a live reference to the GPIO device. GPIO device will have an SRCU cookie for protecting API calls against removal of the chip.

To summarize: one mutex for the GPIO device list, one lock per GPIO descriptor and SRCU protection of the GPIO device's chip.

Does it make sense?

Bart

Quoted 2 lines

Yours,
Linus Walleij

#4 lore raw

Re: [PATCH 2/2] gpio: use a mutex to protect the list of GPIO devices

Quoted 9 lines · Bartosz Golaszewski

We don't need to lock the GPIO device or chip separately - the descriptor structs will stay alive as long as there's a live reference to the GPIO device. GPIO device will have an SRCU cookie for protecting API calls against removal of the chip.

To summarize: one mutex for the GPIO device list, one lock per GPIO descriptor and SRCU protection of the GPIO device's chip.

Does it make sense?

Absolutely, standing by to review, it's gonna look awesome!

Linus

#5 lore raw

Re: [PATCH 2/2] gpio: use a mutex to protect the list of GPIO devices

In reply to Bartosz Golaszewski
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_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.

...

Review

Quoted 1 line
(quoted diff) +1 -0
+	scoped_guard(mutex, &gpio_devices_lock) {

This is a lot of churn with this because of switching to RAII. Can the body be firstly moved to a helper?

Quoted 7 lines
drivers/gpio/gpiolib.c +7 -0
+		/*
+		 * TODO: this allocates a Linux GPIO number base in the global
+		 * GPIO numberspace for this chip. In the long run we want to
+		 * get *rid* of this numberspace and use only descriptors, but
+		 * it may be a pipe dream. It will not happen before we get rid
+		 * of the sysfs interface anyways.
+		 */

...

drivers/gpio/gpiolib.c static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) · line 4760

Quoted 3 lines
drivers/gpio/gpiolib.c +1 -0
 	list_for_each_entry(gdev, &gpio_devices, list)
+		if (index-- == 0)
 			return gdev;

I believe this is better with outer {}.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH 2/2] gpio: use a mutex to protect the list of GPIO devices

Quoted 12 lines · Andy Shevchenko
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_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.

Quoted 7 lines

...

Quoted 1 line

+ scoped_guard(mutex, &gpio_devices_lock) {

This is a lot of churn with this because of switching to RAII. Can the body be firstly moved to a helper?

But that would mean more churn. I don't get why you insist on splitting these everytime. We're going from spinlock to a mutex so we may as well use guards right away.

Quoted 8 lines

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 918

Quoted 7 lines
drivers/gpio/gpiolib.c +7 -0
+             /*
+              * TODO: this allocates a Linux GPIO number base in the global
+              * GPIO numberspace for this chip. In the long run we want to
+              * get *rid* of this numberspace and use only descriptors, but
+              * it may be a pipe dream. It will not happen before we get rid
+              * of the sysfs interface anyways.
+              */
Quoted 8 lines

...

Quoted 3 lines

list_for_each_entry(gdev, &gpio_devices, list)
+ if (index-- == 0)

                      return gdev;

I believe this is better with outer {}.

Right.

Bart

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko

#9 lore raw
In reply to Bartosz Golaszewski
Quoted 30 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

The forward declaration for struct gpio_device should be provided for both branches of the #ifdef.

Fixes 08a149c40bdb ("gpiolib: Clean up headers")
---
 drivers/gpio/gpiolib-sysfs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-sysfs.h +2 -3
index 0f213bdb4732..b794b396d6a5 100644
@@ -3,10 +3,10 @@
 #ifndef GPIOLIB_SYSFS_H
 #define GPIOLIB_SYSFS_H
 
-#ifdef CONFIG_GPIO_SYSFS
-
 struct gpio_device;
 
+#ifdef CONFIG_GPIO_SYSFS
+
 int gpiochip_sysfs_register(struct gpio_device *gdev);
 void gpiochip_sysfs_unregister(struct gpio_device *gdev);
 
--
Quoted 2 lines

2.40.1

I applied this. I'll make patch 2/2 part of the upcoming locking rework.

Bart