Skip to content

Commit 11f7000

Browse files
mitapavelmachek
authored andcommitted
leds: remove PAGE_SIZE limit of /sys/class/leds/<led>/trigger
Reading /sys/class/leds/<led>/trigger returns all available LED triggers. However, the size of this file is limited to PAGE_SIZE because of the limitation for sysfs attribute. Enabling LED CPU trigger on systems with thousands of CPUs easily hits PAGE_SIZE limit, and makes it impossible to see all available LED triggers and which trigger is currently activated. We work around it here by converting /sys/class/leds/<led>/trigger to binary attribute, which is not limited by length. This is _not_ good design, do not copy it. Signed-off-by: Akinobu Mita <[email protected]> Cc: "Rafael J. Wysocki" <[email protected]> Cc: Pavel Machek <[email protected]> Cc: Dan Murphy <[email protected]>A Reviewed-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Pavel Machek <[email protected]>
1 parent a2cafdf commit 11f7000

File tree

4 files changed

+78
-31
lines changed

4 files changed

+78
-31
lines changed

drivers/leds/led-class.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ static ssize_t max_brightness_show(struct device *dev,
7474
static DEVICE_ATTR_RO(max_brightness);
7575

7676
#ifdef CONFIG_LEDS_TRIGGERS
77-
static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store);
78-
static struct attribute *led_trigger_attrs[] = {
79-
&dev_attr_trigger.attr,
77+
static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0);
78+
static struct bin_attribute *led_trigger_bin_attrs[] = {
79+
&bin_attr_trigger,
8080
NULL,
8181
};
8282
static const struct attribute_group led_trigger_group = {
83-
.attrs = led_trigger_attrs,
83+
.bin_attrs = led_trigger_bin_attrs,
8484
};
8585
#endif
8686

drivers/leds/led-triggers.c

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/rwsem.h>
1717
#include <linux/leds.h>
1818
#include <linux/slab.h>
19+
#include <linux/mm.h>
1920
#include "leds.h"
2021

2122
/*
@@ -26,9 +27,11 @@ LIST_HEAD(trigger_list);
2627

2728
/* Used by LED Class */
2829

29-
ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
30-
const char *buf, size_t count)
30+
ssize_t led_trigger_write(struct file *filp, struct kobject *kobj,
31+
struct bin_attribute *bin_attr, char *buf,
32+
loff_t pos, size_t count)
3133
{
34+
struct device *dev = kobj_to_dev(kobj);
3235
struct led_classdev *led_cdev = dev_get_drvdata(dev);
3336
struct led_trigger *trig;
3437
int ret = count;
@@ -64,39 +67,82 @@ ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
6467
mutex_unlock(&led_cdev->led_access);
6568
return ret;
6669
}
67-
EXPORT_SYMBOL_GPL(led_trigger_store);
70+
EXPORT_SYMBOL_GPL(led_trigger_write);
6871

69-
ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
70-
char *buf)
72+
__printf(3, 4)
73+
static int led_trigger_snprintf(char *buf, ssize_t size, const char *fmt, ...)
74+
{
75+
va_list args;
76+
int i;
77+
78+
va_start(args, fmt);
79+
if (size <= 0)
80+
i = vsnprintf(NULL, 0, fmt, args);
81+
else
82+
i = vscnprintf(buf, size, fmt, args);
83+
va_end(args);
84+
85+
return i;
86+
}
87+
88+
static int led_trigger_format(char *buf, size_t size,
89+
struct led_classdev *led_cdev)
7190
{
72-
struct led_classdev *led_cdev = dev_get_drvdata(dev);
7391
struct led_trigger *trig;
74-
int len = 0;
92+
int len = led_trigger_snprintf(buf, size, "%s",
93+
led_cdev->trigger ? "none" : "[none]");
94+
95+
list_for_each_entry(trig, &trigger_list, next_trig) {
96+
bool hit = led_cdev->trigger &&
97+
!strcmp(led_cdev->trigger->name, trig->name);
98+
99+
len += led_trigger_snprintf(buf + len, size - len,
100+
" %s%s%s", hit ? "[" : "",
101+
trig->name, hit ? "]" : "");
102+
}
103+
104+
len += led_trigger_snprintf(buf + len, size - len, "\n");
105+
106+
return len;
107+
}
108+
109+
/*
110+
* It was stupid to create 10000 cpu triggers, but we are stuck with it now.
111+
* Don't make that mistake again. We work around it here by creating binary
112+
* attribute, which is not limited by length. This is _not_ good design, do not
113+
* copy it.
114+
*/
115+
ssize_t led_trigger_read(struct file *filp, struct kobject *kobj,
116+
struct bin_attribute *attr, char *buf,
117+
loff_t pos, size_t count)
118+
{
119+
struct device *dev = kobj_to_dev(kobj);
120+
struct led_classdev *led_cdev = dev_get_drvdata(dev);
121+
void *data;
122+
int len;
75123

76124
down_read(&triggers_list_lock);
77125
down_read(&led_cdev->trigger_lock);
78126

79-
if (!led_cdev->trigger)
80-
len += scnprintf(buf+len, PAGE_SIZE - len, "[none] ");
81-
else
82-
len += scnprintf(buf+len, PAGE_SIZE - len, "none ");
83-
84-
list_for_each_entry(trig, &trigger_list, next_trig) {
85-
if (led_cdev->trigger && !strcmp(led_cdev->trigger->name,
86-
trig->name))
87-
len += scnprintf(buf+len, PAGE_SIZE - len, "[%s] ",
88-
trig->name);
89-
else
90-
len += scnprintf(buf+len, PAGE_SIZE - len, "%s ",
91-
trig->name);
127+
len = led_trigger_format(NULL, 0, led_cdev);
128+
data = kvmalloc(len + 1, GFP_KERNEL);
129+
if (!data) {
130+
up_read(&led_cdev->trigger_lock);
131+
up_read(&triggers_list_lock);
132+
return -ENOMEM;
92133
}
134+
len = led_trigger_format(data, len + 1, led_cdev);
135+
93136
up_read(&led_cdev->trigger_lock);
94137
up_read(&triggers_list_lock);
95138

96-
len += scnprintf(len+buf, PAGE_SIZE - len, "\n");
139+
len = memory_read_from_buffer(buf, count, &pos, data, len);
140+
141+
kvfree(data);
142+
97143
return len;
98144
}
99-
EXPORT_SYMBOL_GPL(led_trigger_show);
145+
EXPORT_SYMBOL_GPL(led_trigger_read);
100146

101147
/* Caller must ensure led_cdev->trigger_lock held */
102148
int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)

drivers/leds/leds.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ void led_set_brightness_nopm(struct led_classdev *led_cdev,
2323
enum led_brightness value);
2424
void led_set_brightness_nosleep(struct led_classdev *led_cdev,
2525
enum led_brightness value);
26+
ssize_t led_trigger_read(struct file *filp, struct kobject *kobj,
27+
struct bin_attribute *attr, char *buf,
28+
loff_t pos, size_t count);
29+
ssize_t led_trigger_write(struct file *filp, struct kobject *kobj,
30+
struct bin_attribute *bin_attr, char *buf,
31+
loff_t pos, size_t count);
2632

2733
extern struct rw_semaphore leds_list_lock;
2834
extern struct list_head leds_list;

include/linux/leds.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,6 @@ struct led_trigger {
361361
#define led_trigger_get_led(dev) ((struct led_classdev *)dev_get_drvdata((dev)))
362362
#define led_trigger_get_drvdata(dev) (led_get_trigger_data(led_trigger_get_led(dev)))
363363

364-
ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
365-
const char *buf, size_t count);
366-
ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
367-
char *buf);
368-
369364
/* Registration functions for complex triggers */
370365
extern int led_trigger_register(struct led_trigger *trigger);
371366
extern void led_trigger_unregister(struct led_trigger *trigger);

0 commit comments

Comments
 (0)