[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230920085639.152441-1-brgl@bgdev.pl>
Date: Wed, 20 Sep 2023 10:56:39 +0200
From: Bartosz Golaszewski <brgl@...ev.pl>
To: Linus Walleij <linus.walleij@...aro.org>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
Cc: linux-gpio@...r.kernel.org, linux-kernel@...r.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@...aro.org>
Subject: [PATCH] gpiolib: extend the critical sections of lookup tables
From: Bartosz Golaszewski <bartosz.golaszewski@...aro.org>
There are two places in the code where we retrieve a lookup table using
gpiod_find_lookup_table() (which protects the table list with the lookup
table lock) and then use it after the lock is released.
We need to keep the lookup table mutex locked the entire time we're using
the tables. Remove the locking from gpiod_find_lookup_table() and use
guards to protect the code actually using the table objects.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@...aro.org>
---
drivers/gpio/gpiolib.c | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index edffa0d2acaa..7c27a1efc1b0 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -3822,8 +3822,6 @@ static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
const char *dev_id = dev ? dev_name(dev) : NULL;
struct gpiod_lookup_table *table;
- mutex_lock(&gpio_lookup_lock);
-
list_for_each_entry(table, &gpio_lookup_list, list) {
if (table->dev_id && dev_id) {
/*
@@ -3831,21 +3829,18 @@ static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
* a match
*/
if (!strcmp(table->dev_id, dev_id))
- goto found;
+ return table;
} else {
/*
* One of the pointers is NULL, so both must be to have
* a match
*/
if (dev_id == table->dev_id)
- goto found;
+ return table;
}
}
- table = NULL;
-found:
- mutex_unlock(&gpio_lookup_lock);
- return table;
+ return NULL;
}
static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
@@ -3855,6 +3850,8 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
struct gpiod_lookup_table *table;
struct gpiod_lookup *p;
+ guard(mutex)(&gpio_lookup_lock);
+
table = gpiod_find_lookup_table(dev);
if (!table)
return desc;
@@ -3920,15 +3917,18 @@ static int platform_gpio_count(struct device *dev, const char *con_id)
struct gpiod_lookup *p;
unsigned int count = 0;
- table = gpiod_find_lookup_table(dev);
- if (!table)
- return -ENOENT;
+ scoped_guard(mutex, &gpio_lookup_lock) {
+ table = gpiod_find_lookup_table(dev);
+ if (!table)
+ return -ENOENT;
- for (p = &table->table[0]; p->key; p++) {
- if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
- (!con_id && !p->con_id))
- count++;
+ for (p = &table->table[0]; p->key; p++) {
+ if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
+ (!con_id && !p->con_id))
+ count++;
+ }
}
+
if (!count)
return -ENOENT;
--
2.39.2
Powered by blists - more mailing lists