leds: trigger: netdev: fix refcnt leak on interface rename
[muen/linux.git] / drivers / leds / trigger / ledtrig-netdev.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright 2017 Ben Whitten <ben.whitten@gmail.com>
3 // Copyright 2007 Oliver Jowett <oliver@opencloud.com>
4 //
5 // LED Kernel Netdev Trigger
6 //
7 // Toggles the LED to reflect the link and traffic state of a named net device
8 //
9 // Derived from ledtrig-timer.c which is:
10 //  Copyright 2005-2006 Openedhand Ltd.
11 //  Author: Richard Purdie <rpurdie@openedhand.com>
12
13 #include <linux/atomic.h>
14 #include <linux/ctype.h>
15 #include <linux/device.h>
16 #include <linux/init.h>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/leds.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
23 #include <linux/spinlock.h>
24 #include <linux/timer.h>
25 #include "../leds.h"
26
27 /*
28  * Configurable sysfs attributes:
29  *
30  * device_name - network device name to monitor
31  * interval - duration of LED blink, in milliseconds
32  * link -  LED's normal state reflects whether the link is up
33  *         (has carrier) or not
34  * tx -  LED blinks on transmitted data
35  * rx -  LED blinks on receive data
36  *
37  */
38
39 struct led_netdev_data {
40         spinlock_t lock;
41
42         struct delayed_work work;
43         struct notifier_block notifier;
44
45         struct led_classdev *led_cdev;
46         struct net_device *net_dev;
47
48         char device_name[IFNAMSIZ];
49         atomic_t interval;
50         unsigned int last_activity;
51
52         unsigned long mode;
53 #define NETDEV_LED_LINK 0
54 #define NETDEV_LED_TX   1
55 #define NETDEV_LED_RX   2
56 #define NETDEV_LED_MODE_LINKUP  3
57 };
58
59 enum netdev_led_attr {
60         NETDEV_ATTR_LINK,
61         NETDEV_ATTR_TX,
62         NETDEV_ATTR_RX
63 };
64
65 static void set_baseline_state(struct led_netdev_data *trigger_data)
66 {
67         int current_brightness;
68         struct led_classdev *led_cdev = trigger_data->led_cdev;
69
70         current_brightness = led_cdev->brightness;
71         if (current_brightness)
72                 led_cdev->blink_brightness = current_brightness;
73         if (!led_cdev->blink_brightness)
74                 led_cdev->blink_brightness = led_cdev->max_brightness;
75
76         if (!test_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode))
77                 led_set_brightness(led_cdev, LED_OFF);
78         else {
79                 if (test_bit(NETDEV_LED_LINK, &trigger_data->mode))
80                         led_set_brightness(led_cdev,
81                                            led_cdev->blink_brightness);
82                 else
83                         led_set_brightness(led_cdev, LED_OFF);
84
85                 /* If we are looking for RX/TX start periodically
86                  * checking stats
87                  */
88                 if (test_bit(NETDEV_LED_TX, &trigger_data->mode) ||
89                     test_bit(NETDEV_LED_RX, &trigger_data->mode))
90                         schedule_delayed_work(&trigger_data->work, 0);
91         }
92 }
93
94 static ssize_t device_name_show(struct device *dev,
95                                 struct device_attribute *attr, char *buf)
96 {
97         struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
98         ssize_t len;
99
100         spin_lock_bh(&trigger_data->lock);
101         len = sprintf(buf, "%s\n", trigger_data->device_name);
102         spin_unlock_bh(&trigger_data->lock);
103
104         return len;
105 }
106
107 static ssize_t device_name_store(struct device *dev,
108                                  struct device_attribute *attr, const char *buf,
109                                  size_t size)
110 {
111         struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
112
113         if (size >= IFNAMSIZ)
114                 return -EINVAL;
115
116         cancel_delayed_work_sync(&trigger_data->work);
117
118         spin_lock_bh(&trigger_data->lock);
119
120         if (trigger_data->net_dev) {
121                 dev_put(trigger_data->net_dev);
122                 trigger_data->net_dev = NULL;
123         }
124
125         strncpy(trigger_data->device_name, buf, size);
126         if (size > 0 && trigger_data->device_name[size - 1] == '\n')
127                 trigger_data->device_name[size - 1] = 0;
128
129         if (trigger_data->device_name[0] != 0)
130                 trigger_data->net_dev =
131                     dev_get_by_name(&init_net, trigger_data->device_name);
132
133         clear_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
134         if (trigger_data->net_dev != NULL)
135                 if (netif_carrier_ok(trigger_data->net_dev))
136                         set_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
137
138         trigger_data->last_activity = 0;
139
140         set_baseline_state(trigger_data);
141         spin_unlock_bh(&trigger_data->lock);
142
143         return size;
144 }
145
146 static DEVICE_ATTR_RW(device_name);
147
148 static ssize_t netdev_led_attr_show(struct device *dev, char *buf,
149         enum netdev_led_attr attr)
150 {
151         struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
152         int bit;
153
154         switch (attr) {
155         case NETDEV_ATTR_LINK:
156                 bit = NETDEV_LED_LINK;
157                 break;
158         case NETDEV_ATTR_TX:
159                 bit = NETDEV_LED_TX;
160                 break;
161         case NETDEV_ATTR_RX:
162                 bit = NETDEV_LED_RX;
163                 break;
164         default:
165                 return -EINVAL;
166         }
167
168         return sprintf(buf, "%u\n", test_bit(bit, &trigger_data->mode));
169 }
170
171 static ssize_t netdev_led_attr_store(struct device *dev, const char *buf,
172         size_t size, enum netdev_led_attr attr)
173 {
174         struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
175         unsigned long state;
176         int ret;
177         int bit;
178
179         ret = kstrtoul(buf, 0, &state);
180         if (ret)
181                 return ret;
182
183         switch (attr) {
184         case NETDEV_ATTR_LINK:
185                 bit = NETDEV_LED_LINK;
186                 break;
187         case NETDEV_ATTR_TX:
188                 bit = NETDEV_LED_TX;
189                 break;
190         case NETDEV_ATTR_RX:
191                 bit = NETDEV_LED_RX;
192                 break;
193         default:
194                 return -EINVAL;
195         }
196
197         cancel_delayed_work_sync(&trigger_data->work);
198
199         if (state)
200                 set_bit(bit, &trigger_data->mode);
201         else
202                 clear_bit(bit, &trigger_data->mode);
203
204         set_baseline_state(trigger_data);
205
206         return size;
207 }
208
209 static ssize_t link_show(struct device *dev,
210         struct device_attribute *attr, char *buf)
211 {
212         return netdev_led_attr_show(dev, buf, NETDEV_ATTR_LINK);
213 }
214
215 static ssize_t link_store(struct device *dev,
216         struct device_attribute *attr, const char *buf, size_t size)
217 {
218         return netdev_led_attr_store(dev, buf, size, NETDEV_ATTR_LINK);
219 }
220
221 static DEVICE_ATTR_RW(link);
222
223 static ssize_t tx_show(struct device *dev,
224         struct device_attribute *attr, char *buf)
225 {
226         return netdev_led_attr_show(dev, buf, NETDEV_ATTR_TX);
227 }
228
229 static ssize_t tx_store(struct device *dev,
230         struct device_attribute *attr, const char *buf, size_t size)
231 {
232         return netdev_led_attr_store(dev, buf, size, NETDEV_ATTR_TX);
233 }
234
235 static DEVICE_ATTR_RW(tx);
236
237 static ssize_t rx_show(struct device *dev,
238         struct device_attribute *attr, char *buf)
239 {
240         return netdev_led_attr_show(dev, buf, NETDEV_ATTR_RX);
241 }
242
243 static ssize_t rx_store(struct device *dev,
244         struct device_attribute *attr, const char *buf, size_t size)
245 {
246         return netdev_led_attr_store(dev, buf, size, NETDEV_ATTR_RX);
247 }
248
249 static DEVICE_ATTR_RW(rx);
250
251 static ssize_t interval_show(struct device *dev,
252                              struct device_attribute *attr, char *buf)
253 {
254         struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
255
256         return sprintf(buf, "%u\n",
257                        jiffies_to_msecs(atomic_read(&trigger_data->interval)));
258 }
259
260 static ssize_t interval_store(struct device *dev,
261                               struct device_attribute *attr, const char *buf,
262                               size_t size)
263 {
264         struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
265         unsigned long value;
266         int ret;
267
268         ret = kstrtoul(buf, 0, &value);
269         if (ret)
270                 return ret;
271
272         /* impose some basic bounds on the timer interval */
273         if (value >= 5 && value <= 10000) {
274                 cancel_delayed_work_sync(&trigger_data->work);
275
276                 atomic_set(&trigger_data->interval, msecs_to_jiffies(value));
277                 set_baseline_state(trigger_data);       /* resets timer */
278         }
279
280         return size;
281 }
282
283 static DEVICE_ATTR_RW(interval);
284
285 static struct attribute *netdev_trig_attrs[] = {
286         &dev_attr_device_name.attr,
287         &dev_attr_link.attr,
288         &dev_attr_rx.attr,
289         &dev_attr_tx.attr,
290         &dev_attr_interval.attr,
291         NULL
292 };
293 ATTRIBUTE_GROUPS(netdev_trig);
294
295 static int netdev_trig_notify(struct notifier_block *nb,
296                               unsigned long evt, void *dv)
297 {
298         struct net_device *dev =
299                 netdev_notifier_info_to_dev((struct netdev_notifier_info *)dv);
300         struct led_netdev_data *trigger_data =
301                 container_of(nb, struct led_netdev_data, notifier);
302
303         if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE
304             && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER)
305                 return NOTIFY_DONE;
306
307         if (!(dev == trigger_data->net_dev ||
308               (evt == NETDEV_REGISTER && !strcmp(dev->name, trigger_data->device_name))))
309                 return NOTIFY_DONE;
310
311         cancel_delayed_work_sync(&trigger_data->work);
312
313         spin_lock_bh(&trigger_data->lock);
314
315         clear_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
316         switch (evt) {
317         case NETDEV_REGISTER:
318                 if (trigger_data->net_dev)
319                         dev_put(trigger_data->net_dev);
320                 dev_hold(dev);
321                 trigger_data->net_dev = dev;
322                 break;
323         case NETDEV_UNREGISTER:
324                 dev_put(trigger_data->net_dev);
325                 trigger_data->net_dev = NULL;
326                 break;
327         case NETDEV_UP:
328         case NETDEV_CHANGE:
329                 if (netif_carrier_ok(dev))
330                         set_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
331                 break;
332         }
333
334         set_baseline_state(trigger_data);
335
336         spin_unlock_bh(&trigger_data->lock);
337
338         return NOTIFY_DONE;
339 }
340
341 /* here's the real work! */
342 static void netdev_trig_work(struct work_struct *work)
343 {
344         struct led_netdev_data *trigger_data =
345                 container_of(work, struct led_netdev_data, work.work);
346         struct rtnl_link_stats64 *dev_stats;
347         unsigned int new_activity;
348         struct rtnl_link_stats64 temp;
349         unsigned long interval;
350         int invert;
351
352         /* If we dont have a device, insure we are off */
353         if (!trigger_data->net_dev) {
354                 led_set_brightness(trigger_data->led_cdev, LED_OFF);
355                 return;
356         }
357
358         /* If we are not looking for RX/TX then return  */
359         if (!test_bit(NETDEV_LED_TX, &trigger_data->mode) &&
360             !test_bit(NETDEV_LED_RX, &trigger_data->mode))
361                 return;
362
363         dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
364         new_activity =
365             (test_bit(NETDEV_LED_TX, &trigger_data->mode) ?
366                 dev_stats->tx_packets : 0) +
367             (test_bit(NETDEV_LED_RX, &trigger_data->mode) ?
368                 dev_stats->rx_packets : 0);
369
370         if (trigger_data->last_activity != new_activity) {
371                 led_stop_software_blink(trigger_data->led_cdev);
372
373                 invert = test_bit(NETDEV_LED_LINK, &trigger_data->mode);
374                 interval = jiffies_to_msecs(
375                                 atomic_read(&trigger_data->interval));
376                 /* base state is ON (link present) */
377                 led_blink_set_oneshot(trigger_data->led_cdev,
378                                       &interval,
379                                       &interval,
380                                       invert);
381                 trigger_data->last_activity = new_activity;
382         }
383
384         schedule_delayed_work(&trigger_data->work,
385                         (atomic_read(&trigger_data->interval)*2));
386 }
387
388 static int netdev_trig_activate(struct led_classdev *led_cdev)
389 {
390         struct led_netdev_data *trigger_data;
391         int rc;
392
393         trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
394         if (!trigger_data)
395                 return -ENOMEM;
396
397         spin_lock_init(&trigger_data->lock);
398
399         trigger_data->notifier.notifier_call = netdev_trig_notify;
400         trigger_data->notifier.priority = 10;
401
402         INIT_DELAYED_WORK(&trigger_data->work, netdev_trig_work);
403
404         trigger_data->led_cdev = led_cdev;
405         trigger_data->net_dev = NULL;
406         trigger_data->device_name[0] = 0;
407
408         trigger_data->mode = 0;
409         atomic_set(&trigger_data->interval, msecs_to_jiffies(50));
410         trigger_data->last_activity = 0;
411
412         led_set_trigger_data(led_cdev, trigger_data);
413
414         rc = register_netdevice_notifier(&trigger_data->notifier);
415         if (rc)
416                 kfree(trigger_data);
417
418         return rc;
419 }
420
421 static void netdev_trig_deactivate(struct led_classdev *led_cdev)
422 {
423         struct led_netdev_data *trigger_data = led_get_trigger_data(led_cdev);
424
425         unregister_netdevice_notifier(&trigger_data->notifier);
426
427         cancel_delayed_work_sync(&trigger_data->work);
428
429         if (trigger_data->net_dev)
430                 dev_put(trigger_data->net_dev);
431
432         kfree(trigger_data);
433 }
434
435 static struct led_trigger netdev_led_trigger = {
436         .name = "netdev",
437         .activate = netdev_trig_activate,
438         .deactivate = netdev_trig_deactivate,
439         .groups = netdev_trig_groups,
440 };
441
442 static int __init netdev_trig_init(void)
443 {
444         return led_trigger_register(&netdev_led_trigger);
445 }
446
447 static void __exit netdev_trig_exit(void)
448 {
449         led_trigger_unregister(&netdev_led_trigger);
450 }
451
452 module_init(netdev_trig_init);
453 module_exit(netdev_trig_exit);
454
455 MODULE_AUTHOR("Ben Whitten <ben.whitten@gmail.com>");
456 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
457 MODULE_DESCRIPTION("Netdev LED trigger");
458 MODULE_LICENSE("GPL v2");