History

gpiolib: extend the critical sections of lookup tables

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.

Bartosz Golaszewski committed 3 years ago

Commit c31071e1 parent d9d58291 file · +15 −15Browse files

Unified · Split

drivers/gpio/gpiolib.c+15−15
@@ -3822,8 +3822,6 @@ static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) · 3821 unmodified lines
38223822 const char *dev_id = dev ? dev_name(dev) : NULL;
38233823 struct gpiod_lookup_table *table;
38243824
3825− mutex_lock(&gpio_lookup_lock);
3826−
38273825 list_for_each_entry(table, &gpio_lookup_list, list) {
38283826 if (table->dev_id && dev_id) {
38293827 /*
@@ -3831,21 +3829,18 @@ static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) · 1 unmodified line
38313829 * a match
38323830 */
38333831 if (!strcmp(table->dev_id, dev_id))
3834− goto found;
3832+ return table;
38353833 } else {
38363834 /*
38373835 * One of the pointers is NULL, so both must be to have
38383836 * a match
38393837 */
38403838 if (dev_id == table->dev_id)
3841− goto found;
3839+ return table;
38423840 }
38433841 }
3844− table = NULL;
38453842
3846−found:
3847− mutex_unlock(&gpio_lookup_lock);
3848− return table;
3843+ return NULL;
38493844 }
38503845
38513846 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, · 3 unmodified lines
38553850 struct gpiod_lookup_table *table;
38563851 struct gpiod_lookup *p;
38573852
3853+ guard(mutex)(&gpio_lookup_lock);
3854+
38583855 table = gpiod_find_lookup_table(dev);
38593856 if (!table)
38603857 return desc;
@@ -3920,15 +3917,18 @@ static int platform_gpio_count(struct device *dev, const char *con_id) · 59 unmodified lines
39203917 struct gpiod_lookup *p;
39213918 unsigned int count = 0;
39223919
3923− table = gpiod_find_lookup_table(dev);
3924− if (!table)
3925− return -ENOENT;
3920+ scoped_guard(mutex, &gpio_lookup_lock) {
3921+ table = gpiod_find_lookup_table(dev);
3922+ if (!table)
3923+ return -ENOENT;
39263924
3927− for (p = &table->table[0]; p->key; p++) {
3928− if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3929− (!con_id && !p->con_id))
3930− count++;
3925+ for (p = &table->table[0]; p->key; p++) {
3926+ if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3927+ (!con_id && !p->con_id))
3928+ count++;
3929+ }
39313930 }
3931+
39323932 if (!count)
39333933 return -ENOENT;
39343934

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t