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
3822 const char *dev_id = dev ? dev_name(dev) : NULL;3822 const char *dev_id = dev ? dev_name(dev) : NULL;
3823 struct gpiod_lookup_table *table;3823 struct gpiod_lookup_table *table;
3824 3824
3825 mutex_lock(&gpio_lookup_lock);
3826
3827 list_for_each_entry(table, &gpio_lookup_list, list) {3825 list_for_each_entry(table, &gpio_lookup_list, list) {
3828 if (table->dev_id && dev_id) {3826 if (table->dev_id && dev_id) {
3829 /*3827 /*
@@ -3831,21 +3829,18 @@ static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) · 1 unmodified line
3831 * a match3829 * a match
3832 */3830 */
3833 if (!strcmp(table->dev_id, dev_id))3831 if (!strcmp(table->dev_id, dev_id))
3834 goto found;3832 return table;
3835 } else {3833 } else {
3836 /*3834 /*
3837 * One of the pointers is NULL, so both must be to have3835 * One of the pointers is NULL, so both must be to have
3838 * a match3836 * a match
3839 */3837 */
3840 if (dev_id == table->dev_id)3838 if (dev_id == table->dev_id)
3841 goto found;3839 return table;
3842 }3840 }
3843 }3841 }
3844 table = NULL;
3845 3842
3846found:3843 return NULL;
3847 mutex_unlock(&gpio_lookup_lock);
3848 return table;
3849}3844}
3850 3845
3851static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,3846static 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
3855 struct gpiod_lookup_table *table;3850 struct gpiod_lookup_table *table;
3856 struct gpiod_lookup *p;3851 struct gpiod_lookup *p;
3857 3852
3853 guard(mutex)(&gpio_lookup_lock);
3854
3858 table = gpiod_find_lookup_table(dev);3855 table = gpiod_find_lookup_table(dev);
3859 if (!table)3856 if (!table)
3860 return desc;3857 return desc;
@@ -3920,15 +3917,18 @@ static int platform_gpio_count(struct device *dev, const char *con_id) · 59 unmodified lines
3920 struct gpiod_lookup *p;3917 struct gpiod_lookup *p;
3921 unsigned int count = 0;3918 unsigned int count = 0;
3922 3919
3923 table = gpiod_find_lookup_table(dev);3920 scoped_guard(mutex, &gpio_lookup_lock) {
3924 if (!table)3921 table = gpiod_find_lookup_table(dev);
3925 return -ENOENT;3922 if (!table)
3923 return -ENOENT;
3926 3924
3927 for (p = &table->table[0]; p->key; p++) {3925 for (p = &table->table[0]; p->key; p++) {
3928 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||3926 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3929 (!con_id && !p->con_id))3927 (!con_id && !p->con_id))
3930 count++;3928 count++;
3929 }
3931 }3930 }
3931
3932 if (!count)3932 if (!count)
3933 return -ENOENT;3933 return -ENOENT;
3934 3934

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