Skip to contentSkip to conversation

gpio: shared: fix locking issues in remove path

PATCH Applied Bartosz Golaszewski 3 participants 7 messages May 22 – May 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover gpio: shared: fix locking issues in remove path

This fixes two issues observed with shared GPIO management enabled.

---
Bartosz Golaszewski (2):
gpio: shared: fix deadlock on shared proxy's parent removal
gpio: shared: fix lockdep false positive by removing unneeded lock

 drivers/gpio/gpiolib-shared.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

---
base-commit: 5200f5f493f79f14bbdc349e402a40dfb32f23c8
change-id: 20260521-gpio-shared-deadlock-c9b7697c0030

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
#1 lore raw

PATCH 1/2 gpio: shared: fix deadlock on shared proxy's parent removal

Commit 710abda58055 ("gpio: shared: call gpio_chip::of_xlate() if set") used the mutex embedded in struct gpio_shared_entry to protect the offset field which now can be modified after assignment. The critical section however is too wide and introduced a potential deadlock on the removal of the shared GPIO proxy's parent.

Make the critical section shorter - only protect the offset when it's being read.

While at it: mention the fact that the entry lock is now also used to protect against concurrent access to the offset field in the structure's documentation.

Fixes 710abda58055 ("gpio: shared: call gpio_chip::of_xlate() if set")
---
 drivers/gpio/gpiolib-shared.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-shared.c +3 -4
index e02d6b93a4ab42b197f0fd64e4854a303f54f140..087b64c06c9f42b698abe5741e63102538beb488 100644
@@ -53,7 +53,7 @@ struct gpio_shared_entry {
 	unsigned int offset;
 	/* Index in the property value array. */
 	size_t index;
-	/* Synchronizes the modification of shared_desc. */
+	/* Synchronizes the modification of shared_desc and offset. */
 	struct mutex lock;
 	struct gpio_shared_desc *shared_desc;
 	struct kref ref;
@@ -598,12 +598,11 @@ void gpio_device_teardown_shared(struct gpio_device *gdev)
 	struct gpio_shared_ref *ref;
 
 	list_for_each_entry(entry, &gpio_shared_list, list) {
-		guard(mutex)(&entry->lock);
-
 		if (!device_match_fwnode(&gdev->dev, entry->fwnode))
 			continue;
 
-		gpiod_free_commit(&gdev->descs[entry->offset]);
+		scoped_guard(mutex, &entry->lock)
+			gpiod_free_commit(&gdev->descs[entry->offset]);
 
 		list_for_each_entry(ref, &entry->refs, list) {
 			guard(mutex)(&ref->lock);
Signature · 1 line
2.47.3
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] gpio: shared: fix deadlock on shared proxy's parent removal

Quoted 9 lines · Bartosz Golaszewski

Commit 710abda58055 ("gpio: shared: call gpio_chip::of_xlate() if set") used the mutex embedded in struct gpio_shared_entry to protect the offset field which now can be modified after assignment. The critical section however is too wide and introduced a potential deadlock on the removal of the shared GPIO proxy's parent.

Make the critical section shorter - only protect the offset when it's being read.

Quoted 7 lines

While at it: mention the fact that the entry lock is now also used to protect against concurrent access to the offset field in the structure's documentation.

Fixes 710abda58055 ("gpio: shared: call gpio_chip::of_xlate() if set")

Yours,
Linus Walleij

#3 lore raw

PATCH 2/2 gpio: shared: fix lockdep false positive by removing unneeded lock

In reply to Bartosz Golaszewski

By the time gpio_device_teardown_shared() is called, the parent device is gone from the global list of GPIO devices and all outstanding SRCU read-side critical sections have completed. That means that no concurrent gpio_find_and_request() can call gpio_shared_add_proxy_lookup() for this device at this time. There's also no risk of the parent device being re-bound to the driver before the unbinding completes (including the child devices).

Lockdep produces a false-positive report about a possible circular dependency as it doesn't know the ordering guarantee. Not taking the ref->lock in gpio_device_teardown_shared() silences it and is safe to do.

Fixes ea513dd3c066 ("gpio: shared: make locking more fine-grained")
---
 drivers/gpio/gpiolib-shared.c | 2 --
 1 file changed, 2 deletions(-)
drivers/gpio/gpiolib-shared.c +0 -2
index 087b64c06c9f42b698abe5741e63102538beb488..de72776fb154f1f2ec97a3e186dc96366f3cee8b 100644
@@ -605,8 +605,6 @@ void gpio_device_teardown_shared(struct gpio_device *gdev)
 			gpiod_free_commit(&gdev->descs[entry->offset]);
 
 		list_for_each_entry(ref, &entry->refs, list) {
-			guard(mutex)(&ref->lock);
-
 			if (ref->lookup) {
 				gpiod_remove_lookup_table(ref->lookup);
 				kfree(ref->lookup->table[0].key);
Signature · 1 line
2.47.3
maintainer Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] gpio: shared: fix lockdep false positive by removing unneeded lock

Quoted 8 lines · Bartosz Golaszewski

By the time gpio_device_teardown_shared() is called, the parent device is gone from the global list of GPIO devices and all outstanding SRCU read-side critical sections have completed. That means that no concurrent gpio_find_and_request() can call gpio_shared_add_proxy_lookup() for this device at this time. There's also no risk of the parent device being re-bound to the driver before the unbinding completes (including the child devices).

Quoted 7 lines

Lockdep produces a false-positive report about a possible circular dependency as it doesn't know the ordering guarantee. Not taking the ref->lock in gpio_device_teardown_shared() silences it and is safe to do.

Fixes ea513dd3c066 ("gpio: shared: make locking more fine-grained")

Yours,
Linus Walleij

#5 lore raw
In reply to Bartosz Golaszewski
Quoted 11 lines · Bartosz Golaszewski

This fixes two issues observed with shared GPIO management enabled.

---
Bartosz Golaszewski (2):
gpio: shared: fix deadlock on shared proxy's parent removal
gpio: shared: fix lockdep false positive by removing unneeded lock

 drivers/gpio/gpiolib-shared.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
Quoted 8 lines

---
base-commit: 5200f5f493f79f14bbdc349e402a40dfb32f23c8
change-id: 20260521-gpio-shared-deadlock-c9b7697c0030

Best regards,
--
Bartosz Golaszewski <[email protected]>

For the record: it seems to me that sashiko reports under this series are incorrect. The model seems to confuse the GPIO shared proxy devices (managing access to the real shared GPIOs) and the GPIO controller devices exposing shared pins that are the parents of the former. We never enter the same code paths for both when setting up or tearing down shared proxies and lookups for them.

Bart

author Applied
#6 lore raw
In reply to Bartosz Golaszewski
Quoted 3 lines · Bartosz Golaszewski

This fixes two issues observed with shared GPIO management enabled.

Applied, thanks!

[1/2] gpio: shared: fix deadlock on shared proxy's parent removal

      https://git.kernel.org/brgl/c/755825b2ed6f3384a7f5d9cfcb2c8225df568796
[2/2] gpio: shared: fix lockdep false positive by removing unneeded lock
      https://git.kernel.org/brgl/c/8eaf57f93f1a43776c8a0036921bda9089ab72b7

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>