提交 dc47206e 编写于 作者: R Richard Purdie

leds: Fix led trigger locking bugs

Convert part of the led trigger core from rw spinlocks to rw
semaphores. We're calling functions which can sleep from invalid
contexts otherwise. Fixes bug #9264.
Signed-off-by: NRichard Purdie <rpurdie@rpsys.net>
上级 f194d132
...@@ -111,7 +111,7 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev) ...@@ -111,7 +111,7 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
write_unlock(&leds_list_lock); write_unlock(&leds_list_lock);
#ifdef CONFIG_LEDS_TRIGGERS #ifdef CONFIG_LEDS_TRIGGERS
rwlock_init(&led_cdev->trigger_lock); init_rwsem(&led_cdev->trigger_lock);
rc = device_create_file(led_cdev->dev, &dev_attr_trigger); rc = device_create_file(led_cdev->dev, &dev_attr_trigger);
if (rc) if (rc)
...@@ -147,10 +147,10 @@ void led_classdev_unregister(struct led_classdev *led_cdev) ...@@ -147,10 +147,10 @@ void led_classdev_unregister(struct led_classdev *led_cdev)
device_remove_file(led_cdev->dev, &dev_attr_brightness); device_remove_file(led_cdev->dev, &dev_attr_brightness);
#ifdef CONFIG_LEDS_TRIGGERS #ifdef CONFIG_LEDS_TRIGGERS
device_remove_file(led_cdev->dev, &dev_attr_trigger); device_remove_file(led_cdev->dev, &dev_attr_trigger);
write_lock(&led_cdev->trigger_lock); down_write(&led_cdev->trigger_lock);
if (led_cdev->trigger) if (led_cdev->trigger)
led_trigger_set(led_cdev, NULL); led_trigger_set(led_cdev, NULL);
write_unlock(&led_cdev->trigger_lock); up_write(&led_cdev->trigger_lock);
#endif #endif
device_unregister(led_cdev->dev); device_unregister(led_cdev->dev);
......
...@@ -19,13 +19,14 @@ ...@@ -19,13 +19,14 @@
#include <linux/device.h> #include <linux/device.h>
#include <linux/sysdev.h> #include <linux/sysdev.h>
#include <linux/timer.h> #include <linux/timer.h>
#include <linux/rwsem.h>
#include <linux/leds.h> #include <linux/leds.h>
#include "leds.h" #include "leds.h"
/* /*
* Nests outside led_cdev->trigger_lock * Nests outside led_cdev->trigger_lock
*/ */
static DEFINE_RWLOCK(triggers_list_lock); static DECLARE_RWSEM(triggers_list_lock);
static LIST_HEAD(trigger_list); static LIST_HEAD(trigger_list);
ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr, ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
...@@ -44,24 +45,24 @@ ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr, ...@@ -44,24 +45,24 @@ ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
trigger_name[len - 1] = '\0'; trigger_name[len - 1] = '\0';
if (!strcmp(trigger_name, "none")) { if (!strcmp(trigger_name, "none")) {
write_lock(&led_cdev->trigger_lock); down_write(&led_cdev->trigger_lock);
led_trigger_set(led_cdev, NULL); led_trigger_set(led_cdev, NULL);
write_unlock(&led_cdev->trigger_lock); up_write(&led_cdev->trigger_lock);
return count; return count;
} }
read_lock(&triggers_list_lock); down_read(&triggers_list_lock);
list_for_each_entry(trig, &trigger_list, next_trig) { list_for_each_entry(trig, &trigger_list, next_trig) {
if (!strcmp(trigger_name, trig->name)) { if (!strcmp(trigger_name, trig->name)) {
write_lock(&led_cdev->trigger_lock); down_write(&led_cdev->trigger_lock);
led_trigger_set(led_cdev, trig); led_trigger_set(led_cdev, trig);
write_unlock(&led_cdev->trigger_lock); up_write(&led_cdev->trigger_lock);
read_unlock(&triggers_list_lock); up_read(&triggers_list_lock);
return count; return count;
} }
} }
read_unlock(&triggers_list_lock); up_read(&triggers_list_lock);
return -EINVAL; return -EINVAL;
} }
...@@ -74,8 +75,8 @@ ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr, ...@@ -74,8 +75,8 @@ ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
struct led_trigger *trig; struct led_trigger *trig;
int len = 0; int len = 0;
read_lock(&triggers_list_lock); down_read(&triggers_list_lock);
read_lock(&led_cdev->trigger_lock); down_read(&led_cdev->trigger_lock);
if (!led_cdev->trigger) if (!led_cdev->trigger)
len += sprintf(buf+len, "[none] "); len += sprintf(buf+len, "[none] ");
...@@ -89,8 +90,8 @@ ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr, ...@@ -89,8 +90,8 @@ ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
else else
len += sprintf(buf+len, "%s ", trig->name); len += sprintf(buf+len, "%s ", trig->name);
} }
read_unlock(&led_cdev->trigger_lock); up_read(&led_cdev->trigger_lock);
read_unlock(&triggers_list_lock); up_read(&triggers_list_lock);
len += sprintf(len+buf, "\n"); len += sprintf(len+buf, "\n");
return len; return len;
...@@ -145,14 +146,14 @@ void led_trigger_set_default(struct led_classdev *led_cdev) ...@@ -145,14 +146,14 @@ void led_trigger_set_default(struct led_classdev *led_cdev)
if (!led_cdev->default_trigger) if (!led_cdev->default_trigger)
return; return;
read_lock(&triggers_list_lock); down_read(&triggers_list_lock);
write_lock(&led_cdev->trigger_lock); down_write(&led_cdev->trigger_lock);
list_for_each_entry(trig, &trigger_list, next_trig) { list_for_each_entry(trig, &trigger_list, next_trig) {
if (!strcmp(led_cdev->default_trigger, trig->name)) if (!strcmp(led_cdev->default_trigger, trig->name))
led_trigger_set(led_cdev, trig); led_trigger_set(led_cdev, trig);
} }
write_unlock(&led_cdev->trigger_lock); up_write(&led_cdev->trigger_lock);
read_unlock(&triggers_list_lock); up_read(&triggers_list_lock);
} }
int led_trigger_register(struct led_trigger *trigger) int led_trigger_register(struct led_trigger *trigger)
...@@ -163,18 +164,18 @@ int led_trigger_register(struct led_trigger *trigger) ...@@ -163,18 +164,18 @@ int led_trigger_register(struct led_trigger *trigger)
INIT_LIST_HEAD(&trigger->led_cdevs); INIT_LIST_HEAD(&trigger->led_cdevs);
/* Add to the list of led triggers */ /* Add to the list of led triggers */
write_lock(&triggers_list_lock); down_write(&triggers_list_lock);
list_add_tail(&trigger->next_trig, &trigger_list); list_add_tail(&trigger->next_trig, &trigger_list);
write_unlock(&triggers_list_lock); up_write(&triggers_list_lock);
/* Register with any LEDs that have this as a default trigger */ /* Register with any LEDs that have this as a default trigger */
read_lock(&leds_list_lock); read_lock(&leds_list_lock);
list_for_each_entry(led_cdev, &leds_list, node) { list_for_each_entry(led_cdev, &leds_list, node) {
write_lock(&led_cdev->trigger_lock); down_write(&led_cdev->trigger_lock);
if (!led_cdev->trigger && led_cdev->default_trigger && if (!led_cdev->trigger && led_cdev->default_trigger &&
!strcmp(led_cdev->default_trigger, trigger->name)) !strcmp(led_cdev->default_trigger, trigger->name))
led_trigger_set(led_cdev, trigger); led_trigger_set(led_cdev, trigger);
write_unlock(&led_cdev->trigger_lock); up_write(&led_cdev->trigger_lock);
} }
read_unlock(&leds_list_lock); read_unlock(&leds_list_lock);
...@@ -206,17 +207,17 @@ void led_trigger_unregister(struct led_trigger *trigger) ...@@ -206,17 +207,17 @@ void led_trigger_unregister(struct led_trigger *trigger)
struct led_classdev *led_cdev; struct led_classdev *led_cdev;
/* Remove from the list of led triggers */ /* Remove from the list of led triggers */
write_lock(&triggers_list_lock); down_write(&triggers_list_lock);
list_del(&trigger->next_trig); list_del(&trigger->next_trig);
write_unlock(&triggers_list_lock); up_write(&triggers_list_lock);
/* Remove anyone actively using this trigger */ /* Remove anyone actively using this trigger */
read_lock(&leds_list_lock); read_lock(&leds_list_lock);
list_for_each_entry(led_cdev, &leds_list, node) { list_for_each_entry(led_cdev, &leds_list, node) {
write_lock(&led_cdev->trigger_lock); down_write(&led_cdev->trigger_lock);
if (led_cdev->trigger == trigger) if (led_cdev->trigger == trigger)
led_trigger_set(led_cdev, NULL); led_trigger_set(led_cdev, NULL);
write_unlock(&led_cdev->trigger_lock); up_write(&led_cdev->trigger_lock);
} }
read_unlock(&leds_list_lock); read_unlock(&leds_list_lock);
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <linux/list.h> #include <linux/list.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/rwsem.h>
struct device; struct device;
/* /*
...@@ -43,7 +44,7 @@ struct led_classdev { ...@@ -43,7 +44,7 @@ struct led_classdev {
#ifdef CONFIG_LEDS_TRIGGERS #ifdef CONFIG_LEDS_TRIGGERS
/* Protects the trigger data below */ /* Protects the trigger data below */
rwlock_t trigger_lock; struct rw_semaphore trigger_lock;
struct led_trigger *trigger; struct led_trigger *trigger;
struct list_head trig_list; struct list_head trig_list;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册