Skip to contentSkip to conversation

gpiolib: allocate memory atomically with a spinlock held

PATCH In review Bartosz Golaszewski 2 participants 3 messages Dec 12, 2023 – Dec 15, 2023 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: allocate memory atomically with a spinlock held

From: Bartosz Golaszewski <[email protected]>

We will eventually switch to protecting the GPIO descriptors with a mutex but until then, we need to allocate memory for the label copy atomically while we're holding the global spinlock.

Fixes f8d05e276b45 ("gpiolib: remove gpiochip_is_requested()")
---
 drivers/gpio/gpiolib.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib.c +9 -1
index 4e190be75dc2..6efe44570333 100644
@@ -2399,7 +2399,15 @@ char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset)
 	if (!test_bit(FLAG_REQUESTED, &desc->flags))
 		return NULL;
 
-	label = kstrdup(desc->label, GFP_KERNEL);
+	/*
+	 * FIXME: Once we mark gpiod_direction_input/output() and
+	 * gpiod_get_direction() with might_sleep(), we'll be able to protect
+	 * the GPIO descriptors with mutex (while value setting operations will
+	 * become lockless).
+	 *
+	 * Until this happens, this allocation needs to be atomic.
+	 */
+	label = kstrdup(desc->label, GFP_ATOMIC);
 	if (!label)
 		return ERR_PTR(-ENOMEM);
 
Signature · 1 line
2.40.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We will eventually switch to protecting the GPIO descriptors with a mutex but until then, we need to allocate memory for the label copy atomically while we're holding the global spinlock.

Fixes f8d05e276b45 ("gpiolib: remove gpiochip_is_requested()")

Makes sense.

Yours,
Linus Walleij

#2 lore raw
In reply to Bartosz Golaszewski
Quoted 37 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We will eventually switch to protecting the GPIO descriptors with a mutex but until then, we need to allocate memory for the label copy atomically while we're holding the global spinlock.

Fixes f8d05e276b45 ("gpiolib: remove gpiochip_is_requested()")
---
 drivers/gpio/gpiolib.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib.c +9 -2
index 4e190be75dc2..6efe44570333 100644
@@ -2399,7 +2399,15 @@ char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset)
        if (!test_bit(FLAG_REQUESTED, &desc->flags))
                return NULL;
 
-       label = kstrdup(desc->label, GFP_KERNEL);
+       /*
+        * FIXME: Once we mark gpiod_direction_input/output() and
+        * gpiod_get_direction() with might_sleep(), we'll be able to protect
+        * the GPIO descriptors with mutex (while value setting operations will
+        * become lockless).
+        *
+        * Until this happens, this allocation needs to be atomic.
+        */
+       label = kstrdup(desc->label, GFP_ATOMIC);
        if (!label)
                return ERR_PTR(-ENOMEM);
 
--
Quoted 2 lines

2.40.1

Patch applied.

Bart