1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Activity LED trigger
4  *
5  * Copyright (C) 2017 Willy Tarreau <w@1wt.eu>
6  * Partially based on Atsushi Nemoto's ledtrig-heartbeat.c.
7  */
8 
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/leds.h>
13 #include <linux/module.h>
14 #include <linux/reboot.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/timer.h>
18 #include "../leds.h"
19 
20 static int panic_detected;
21 
22 struct activity_data {
23 	struct timer_list timer;
24 	struct led_classdev *led_cdev;
25 	u64 last_used;
26 	u64 last_boot;
27 	int time_left;
28 	int state;
29 	int invert;
30 };
31 
32 static void led_activity_function(struct timer_list *t)
33 {
34 	struct activity_data *activity_data = from_timer(activity_data, t,
35 							 timer);
36 	struct led_classdev *led_cdev = activity_data->led_cdev;
37 	unsigned int target;
38 	unsigned int usage;
39 	int delay;
40 	u64 curr_used;
41 	u64 curr_boot;
42 	s32 diff_used;
43 	s32 diff_boot;
44 	int cpus;
45 	int i;
46 
47 	if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags))
48 		led_cdev->blink_brightness = led_cdev->new_blink_brightness;
49 
50 	if (unlikely(panic_detected)) {
51 		/* full brightness in case of panic */
52 		led_set_brightness_nosleep(led_cdev, led_cdev->blink_brightness);
53 		return;
54 	}
55 
56 	cpus = 0;
57 	curr_used = 0;
58 
59 	for_each_possible_cpu(i) {
60 		curr_used += kcpustat_cpu(i).cpustat[CPUTIME_USER]
61 			  +  kcpustat_cpu(i).cpustat[CPUTIME_NICE]
62 			  +  kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]
63 			  +  kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]
64 			  +  kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
65 		cpus++;
66 	}
67 
68 	/* We come here every 100ms in the worst case, so that's 100M ns of
69 	 * cumulated time. By dividing by 2^16, we get the time resolution
70 	 * down to 16us, ensuring we won't overflow 32-bit computations below
71 	 * even up to 3k CPUs, while keeping divides cheap on smaller systems.
72 	 */
73 	curr_boot = ktime_get_boottime_ns() * cpus;
74 	diff_boot = (curr_boot - activity_data->last_boot) >> 16;
75 	diff_used = (curr_used - activity_data->last_used) >> 16;
76 	activity_data->last_boot = curr_boot;
77 	activity_data->last_used = curr_used;
78 
79 	if (diff_boot <= 0 || diff_used < 0)
80 		usage = 0;
81 	else if (diff_used >= diff_boot)
82 		usage = 100;
83 	else
84 		usage = 100 * diff_used / diff_boot;
85 
86 	/*
87 	 * Now we know the total boot_time multiplied by the number of CPUs, and
88 	 * the total idle+wait time for all CPUs. We'll compare how they evolved
89 	 * since last call. The % of overall CPU usage is :
90 	 *
91 	 *      1 - delta_idle / delta_boot
92 	 *
93 	 * What we want is that when the CPU usage is zero, the LED must blink
94 	 * slowly with very faint flashes that are detectable but not disturbing
95 	 * (typically 10ms every second, or 10ms ON, 990ms OFF). Then we want
96 	 * blinking frequency to increase up to the point where the load is
97 	 * enough to saturate one core in multi-core systems or 50% in single
98 	 * core systems. At this point it should reach 10 Hz with a 10/90 duty
99 	 * cycle (10ms ON, 90ms OFF). After this point, the blinking frequency
100 	 * remains stable (10 Hz) and only the duty cycle increases to report
101 	 * the activity, up to the point where we have 90ms ON, 10ms OFF when
102 	 * all cores are saturated. It's important that the LED never stays in
103 	 * a steady state so that it's easy to distinguish an idle or saturated
104 	 * machine from a hung one.
105 	 *
106 	 * This gives us :
107 	 *   - a target CPU usage of min(50%, 100%/#CPU) for a 10% duty cycle
108 	 *     (10ms ON, 90ms OFF)
109 	 *   - below target :
110 	 *      ON_ms  = 10
111 	 *      OFF_ms = 90 + (1 - usage/target) * 900
112 	 *   - above target :
113 	 *      ON_ms  = 10 + (usage-target)/(100%-target) * 80
114 	 *      OFF_ms = 90 - (usage-target)/(100%-target) * 80
115 	 *
116 	 * In order to keep a good responsiveness, we cap the sleep time to
117 	 * 100 ms and keep track of the sleep time left. This allows us to
118 	 * quickly change it if needed.
119 	 */
120 
121 	activity_data->time_left -= 100;
122 	if (activity_data->time_left <= 0) {
123 		activity_data->time_left = 0;
124 		activity_data->state = !activity_data->state;
125 		led_set_brightness_nosleep(led_cdev,
126 			(activity_data->state ^ activity_data->invert) ?
127 			led_cdev->blink_brightness : LED_OFF);
128 	}
129 
130 	target = (cpus > 1) ? (100 / cpus) : 50;
131 
132 	if (usage < target)
133 		delay = activity_data->state ?
134 			10 :                        /* ON  */
135 			990 - 900 * usage / target; /* OFF */
136 	else
137 		delay = activity_data->state ?
138 			10 + 80 * (usage - target) / (100 - target) : /* ON  */
139 			90 - 80 * (usage - target) / (100 - target);  /* OFF */
140 
141 
142 	if (!activity_data->time_left || delay <= activity_data->time_left)
143 		activity_data->time_left = delay;
144 
145 	delay = min_t(int, activity_data->time_left, 100);
146 	mod_timer(&activity_data->timer, jiffies + msecs_to_jiffies(delay));
147 }
148 
149 static ssize_t led_invert_show(struct device *dev,
150                                struct device_attribute *attr, char *buf)
151 {
152 	struct activity_data *activity_data = led_trigger_get_drvdata(dev);
153 
154 	return sprintf(buf, "%u\n", activity_data->invert);
155 }
156 
157 static ssize_t led_invert_store(struct device *dev,
158                                 struct device_attribute *attr,
159                                 const char *buf, size_t size)
160 {
161 	struct activity_data *activity_data = led_trigger_get_drvdata(dev);
162 	unsigned long state;
163 	int ret;
164 
165 	ret = kstrtoul(buf, 0, &state);
166 	if (ret)
167 		return ret;
168 
169 	activity_data->invert = !!state;
170 
171 	return size;
172 }
173 
174 static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
175 
176 static struct attribute *activity_led_attrs[] = {
177 	&dev_attr_invert.attr,
178 	NULL
179 };
180 ATTRIBUTE_GROUPS(activity_led);
181 
182 static int activity_activate(struct led_classdev *led_cdev)
183 {
184 	struct activity_data *activity_data;
185 
186 	activity_data = kzalloc(sizeof(*activity_data), GFP_KERNEL);
187 	if (!activity_data)
188 		return -ENOMEM;
189 
190 	led_set_trigger_data(led_cdev, activity_data);
191 
192 	activity_data->led_cdev = led_cdev;
193 	timer_setup(&activity_data->timer, led_activity_function, 0);
194 	if (!led_cdev->blink_brightness)
195 		led_cdev->blink_brightness = led_cdev->max_brightness;
196 	led_activity_function(&activity_data->timer);
197 	set_bit(LED_BLINK_SW, &led_cdev->work_flags);
198 
199 	return 0;
200 }
201 
202 static void activity_deactivate(struct led_classdev *led_cdev)
203 {
204 	struct activity_data *activity_data = led_get_trigger_data(led_cdev);
205 
206 	del_timer_sync(&activity_data->timer);
207 	kfree(activity_data);
208 	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
209 }
210 
211 static struct led_trigger activity_led_trigger = {
212 	.name       = "activity",
213 	.activate   = activity_activate,
214 	.deactivate = activity_deactivate,
215 	.groups     = activity_led_groups,
216 };
217 
218 static int activity_reboot_notifier(struct notifier_block *nb,
219                                     unsigned long code, void *unused)
220 {
221 	led_trigger_unregister(&activity_led_trigger);
222 	return NOTIFY_DONE;
223 }
224 
225 static int activity_panic_notifier(struct notifier_block *nb,
226                                    unsigned long code, void *unused)
227 {
228 	panic_detected = 1;
229 	return NOTIFY_DONE;
230 }
231 
232 static struct notifier_block activity_reboot_nb = {
233 	.notifier_call = activity_reboot_notifier,
234 };
235 
236 static struct notifier_block activity_panic_nb = {
237 	.notifier_call = activity_panic_notifier,
238 };
239 
240 static int __init activity_init(void)
241 {
242 	int rc = led_trigger_register(&activity_led_trigger);
243 
244 	if (!rc) {
245 		atomic_notifier_chain_register(&panic_notifier_list,
246 					       &activity_panic_nb);
247 		register_reboot_notifier(&activity_reboot_nb);
248 	}
249 	return rc;
250 }
251 
252 static void __exit activity_exit(void)
253 {
254 	unregister_reboot_notifier(&activity_reboot_nb);
255 	atomic_notifier_chain_unregister(&panic_notifier_list,
256 					 &activity_panic_nb);
257 	led_trigger_unregister(&activity_led_trigger);
258 }
259 
260 module_init(activity_init);
261 module_exit(activity_exit);
262 
263 MODULE_AUTHOR("Willy Tarreau <w@1wt.eu>");
264 MODULE_DESCRIPTION("Activity LED trigger");
265 MODULE_LICENSE("GPL v2");
266