1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31#include <linux/cdev.h>
32#include <linux/errno.h>
33#include <linux/fs.h>
34#include <linux/init.h>
35#include <linux/hrtimer.h>
36#include <linux/kernel.h>
37#include <linux/kthread.h>
38#include <linux/miscdevice.h>
39#include <linux/module.h>
40#include <linux/mutex.h>
41#include <linux/slab.h>
42#include <linux/types.h>
43#include <linux/watchdog.h>
44#include <linux/uaccess.h>
45
46#include "watchdog_core.h"
47#include "watchdog_pretimeout.h"
48
49
50
51
52
53
54
55
56
57struct watchdog_core_data {
58 struct device dev;
59 struct cdev cdev;
60 struct watchdog_device *wdd;
61 struct mutex lock;
62 ktime_t last_keepalive;
63 ktime_t last_hw_keepalive;
64 ktime_t open_deadline;
65 struct hrtimer timer;
66 struct kthread_work work;
67 unsigned long status;
68#define _WDOG_DEV_OPEN 0
69#define _WDOG_ALLOW_RELEASE 1
70#define _WDOG_KEEPALIVE 2
71};
72
73
74static dev_t watchdog_devt;
75
76static struct watchdog_core_data *old_wd_data;
77
78static struct kthread_worker *watchdog_kworker;
79
80static bool handle_boot_enabled =
81 IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED);
82
83static unsigned open_timeout = CONFIG_WATCHDOG_OPEN_TIMEOUT;
84
85static bool watchdog_past_open_deadline(struct watchdog_core_data *data)
86{
87 return ktime_after(ktime_get(), data->open_deadline);
88}
89
90static void watchdog_set_open_deadline(struct watchdog_core_data *data)
91{
92 data->open_deadline = open_timeout ?
93 ktime_get() + ktime_set(open_timeout, 0) : KTIME_MAX;
94}
95
96static inline bool watchdog_need_worker(struct watchdog_device *wdd)
97{
98
99 unsigned int hm = wdd->max_hw_heartbeat_ms;
100 unsigned int t = wdd->timeout * 1000;
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 return (hm && watchdog_active(wdd) && t > hm) ||
116 (t && !watchdog_active(wdd) && watchdog_hw_running(wdd));
117}
118
119static ktime_t watchdog_next_keepalive(struct watchdog_device *wdd)
120{
121 struct watchdog_core_data *wd_data = wdd->wd_data;
122 unsigned int timeout_ms = wdd->timeout * 1000;
123 ktime_t keepalive_interval;
124 ktime_t last_heartbeat, latest_heartbeat;
125 ktime_t virt_timeout;
126 unsigned int hw_heartbeat_ms;
127
128 if (watchdog_active(wdd))
129 virt_timeout = ktime_add(wd_data->last_keepalive,
130 ms_to_ktime(timeout_ms));
131 else
132 virt_timeout = wd_data->open_deadline;
133
134 hw_heartbeat_ms = min_not_zero(timeout_ms, wdd->max_hw_heartbeat_ms);
135 keepalive_interval = ms_to_ktime(hw_heartbeat_ms / 2);
136
137
138
139
140
141
142 last_heartbeat = ktime_sub(virt_timeout, ms_to_ktime(hw_heartbeat_ms));
143 latest_heartbeat = ktime_sub(last_heartbeat, ktime_get());
144 if (ktime_before(latest_heartbeat, keepalive_interval))
145 return latest_heartbeat;
146 return keepalive_interval;
147}
148
149static inline void watchdog_update_worker(struct watchdog_device *wdd)
150{
151 struct watchdog_core_data *wd_data = wdd->wd_data;
152
153 if (watchdog_need_worker(wdd)) {
154 ktime_t t = watchdog_next_keepalive(wdd);
155
156 if (t > 0)
157 hrtimer_start(&wd_data->timer, t,
158 HRTIMER_MODE_REL_HARD);
159 } else {
160 hrtimer_cancel(&wd_data->timer);
161 }
162}
163
164static int __watchdog_ping(struct watchdog_device *wdd)
165{
166 struct watchdog_core_data *wd_data = wdd->wd_data;
167 ktime_t earliest_keepalive, now;
168 int err;
169
170 earliest_keepalive = ktime_add(wd_data->last_hw_keepalive,
171 ms_to_ktime(wdd->min_hw_heartbeat_ms));
172 now = ktime_get();
173
174 if (ktime_after(earliest_keepalive, now)) {
175 hrtimer_start(&wd_data->timer,
176 ktime_sub(earliest_keepalive, now),
177 HRTIMER_MODE_REL_HARD);
178 return 0;
179 }
180
181 wd_data->last_hw_keepalive = now;
182
183 if (wdd->ops->ping)
184 err = wdd->ops->ping(wdd);
185 else
186 err = wdd->ops->start(wdd);
187
188 watchdog_update_worker(wdd);
189
190 return err;
191}
192
193
194
195
196
197
198
199
200
201
202
203
204
205static int watchdog_ping(struct watchdog_device *wdd)
206{
207 struct watchdog_core_data *wd_data = wdd->wd_data;
208
209 if (!watchdog_active(wdd) && !watchdog_hw_running(wdd))
210 return 0;
211
212 set_bit(_WDOG_KEEPALIVE, &wd_data->status);
213
214 wd_data->last_keepalive = ktime_get();
215 return __watchdog_ping(wdd);
216}
217
218static bool watchdog_worker_should_ping(struct watchdog_core_data *wd_data)
219{
220 struct watchdog_device *wdd = wd_data->wdd;
221
222 if (!wdd)
223 return false;
224
225 if (watchdog_active(wdd))
226 return true;
227
228 return watchdog_hw_running(wdd) && !watchdog_past_open_deadline(wd_data);
229}
230
231static void watchdog_ping_work(struct kthread_work *work)
232{
233 struct watchdog_core_data *wd_data;
234
235 wd_data = container_of(work, struct watchdog_core_data, work);
236
237 mutex_lock(&wd_data->lock);
238 if (watchdog_worker_should_ping(wd_data))
239 __watchdog_ping(wd_data->wdd);
240 mutex_unlock(&wd_data->lock);
241}
242
243static enum hrtimer_restart watchdog_timer_expired(struct hrtimer *timer)
244{
245 struct watchdog_core_data *wd_data;
246
247 wd_data = container_of(timer, struct watchdog_core_data, timer);
248
249 kthread_queue_work(watchdog_kworker, &wd_data->work);
250 return HRTIMER_NORESTART;
251}
252
253
254
255
256
257
258
259
260
261
262
263
264static int watchdog_start(struct watchdog_device *wdd)
265{
266 struct watchdog_core_data *wd_data = wdd->wd_data;
267 ktime_t started_at;
268 int err;
269
270 if (watchdog_active(wdd))
271 return 0;
272
273 set_bit(_WDOG_KEEPALIVE, &wd_data->status);
274
275 started_at = ktime_get();
276 if (watchdog_hw_running(wdd) && wdd->ops->ping) {
277 err = __watchdog_ping(wdd);
278 if (err == 0)
279 set_bit(WDOG_ACTIVE, &wdd->status);
280 } else {
281 err = wdd->ops->start(wdd);
282 if (err == 0) {
283 set_bit(WDOG_ACTIVE, &wdd->status);
284 wd_data->last_keepalive = started_at;
285 wd_data->last_hw_keepalive = started_at;
286 watchdog_update_worker(wdd);
287 }
288 }
289
290 return err;
291}
292
293
294
295
296
297
298
299
300
301
302
303
304
305static int watchdog_stop(struct watchdog_device *wdd)
306{
307 int err = 0;
308
309 if (!watchdog_active(wdd))
310 return 0;
311
312 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
313 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
314 wdd->id);
315 return -EBUSY;
316 }
317
318 if (wdd->ops->stop) {
319 clear_bit(WDOG_HW_RUNNING, &wdd->status);
320 err = wdd->ops->stop(wdd);
321 } else {
322 set_bit(WDOG_HW_RUNNING, &wdd->status);
323 }
324
325 if (err == 0) {
326 clear_bit(WDOG_ACTIVE, &wdd->status);
327 watchdog_update_worker(wdd);
328 }
329
330 return err;
331}
332
333
334
335
336
337
338
339
340
341
342static unsigned int watchdog_get_status(struct watchdog_device *wdd)
343{
344 struct watchdog_core_data *wd_data = wdd->wd_data;
345 unsigned int status;
346
347 if (wdd->ops->status)
348 status = wdd->ops->status(wdd);
349 else
350 status = wdd->bootstatus & (WDIOF_CARDRESET |
351 WDIOF_OVERHEAT |
352 WDIOF_FANFAULT |
353 WDIOF_EXTERN1 |
354 WDIOF_EXTERN2 |
355 WDIOF_POWERUNDER |
356 WDIOF_POWEROVER);
357
358 if (test_bit(_WDOG_ALLOW_RELEASE, &wd_data->status))
359 status |= WDIOF_MAGICCLOSE;
360
361 if (test_and_clear_bit(_WDOG_KEEPALIVE, &wd_data->status))
362 status |= WDIOF_KEEPALIVEPING;
363
364 return status;
365}
366
367
368
369
370
371
372
373
374
375static int watchdog_set_timeout(struct watchdog_device *wdd,
376 unsigned int timeout)
377{
378 int err = 0;
379
380 if (!(wdd->info->options & WDIOF_SETTIMEOUT))
381 return -EOPNOTSUPP;
382
383 if (watchdog_timeout_invalid(wdd, timeout))
384 return -EINVAL;
385
386 if (wdd->ops->set_timeout) {
387 err = wdd->ops->set_timeout(wdd, timeout);
388 } else {
389 wdd->timeout = timeout;
390
391 if (wdd->pretimeout >= wdd->timeout)
392 wdd->pretimeout = 0;
393 }
394
395 watchdog_update_worker(wdd);
396
397 return err;
398}
399
400
401
402
403
404
405
406static int watchdog_set_pretimeout(struct watchdog_device *wdd,
407 unsigned int timeout)
408{
409 int err = 0;
410
411 if (!(wdd->info->options & WDIOF_PRETIMEOUT))
412 return -EOPNOTSUPP;
413
414 if (watchdog_pretimeout_invalid(wdd, timeout))
415 return -EINVAL;
416
417 if (wdd->ops->set_pretimeout)
418 err = wdd->ops->set_pretimeout(wdd, timeout);
419 else
420 wdd->pretimeout = timeout;
421
422 return err;
423}
424
425
426
427
428
429
430
431
432
433
434
435static int watchdog_get_timeleft(struct watchdog_device *wdd,
436 unsigned int *timeleft)
437{
438 *timeleft = 0;
439
440 if (!wdd->ops->get_timeleft)
441 return -EOPNOTSUPP;
442
443 *timeleft = wdd->ops->get_timeleft(wdd);
444
445 return 0;
446}
447
448#ifdef CONFIG_WATCHDOG_SYSFS
449static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
450 char *buf)
451{
452 struct watchdog_device *wdd = dev_get_drvdata(dev);
453
454 return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status));
455}
456
457static ssize_t nowayout_store(struct device *dev, struct device_attribute *attr,
458 const char *buf, size_t len)
459{
460 struct watchdog_device *wdd = dev_get_drvdata(dev);
461 unsigned int value;
462 int ret;
463
464 ret = kstrtouint(buf, 0, &value);
465 if (ret)
466 return ret;
467 if (value > 1)
468 return -EINVAL;
469
470 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status) && !value)
471 return -EPERM;
472 watchdog_set_nowayout(wdd, value);
473 return len;
474}
475static DEVICE_ATTR_RW(nowayout);
476
477static ssize_t status_show(struct device *dev, struct device_attribute *attr,
478 char *buf)
479{
480 struct watchdog_device *wdd = dev_get_drvdata(dev);
481 struct watchdog_core_data *wd_data = wdd->wd_data;
482 unsigned int status;
483
484 mutex_lock(&wd_data->lock);
485 status = watchdog_get_status(wdd);
486 mutex_unlock(&wd_data->lock);
487
488 return sprintf(buf, "0x%x\n", status);
489}
490static DEVICE_ATTR_RO(status);
491
492static ssize_t bootstatus_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
494{
495 struct watchdog_device *wdd = dev_get_drvdata(dev);
496
497 return sprintf(buf, "%u\n", wdd->bootstatus);
498}
499static DEVICE_ATTR_RO(bootstatus);
500
501static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr,
502 char *buf)
503{
504 struct watchdog_device *wdd = dev_get_drvdata(dev);
505 struct watchdog_core_data *wd_data = wdd->wd_data;
506 ssize_t status;
507 unsigned int val;
508
509 mutex_lock(&wd_data->lock);
510 status = watchdog_get_timeleft(wdd, &val);
511 mutex_unlock(&wd_data->lock);
512 if (!status)
513 status = sprintf(buf, "%u\n", val);
514
515 return status;
516}
517static DEVICE_ATTR_RO(timeleft);
518
519static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
520 char *buf)
521{
522 struct watchdog_device *wdd = dev_get_drvdata(dev);
523
524 return sprintf(buf, "%u\n", wdd->timeout);
525}
526static DEVICE_ATTR_RO(timeout);
527
528static ssize_t pretimeout_show(struct device *dev,
529 struct device_attribute *attr, char *buf)
530{
531 struct watchdog_device *wdd = dev_get_drvdata(dev);
532
533 return sprintf(buf, "%u\n", wdd->pretimeout);
534}
535static DEVICE_ATTR_RO(pretimeout);
536
537static ssize_t identity_show(struct device *dev, struct device_attribute *attr,
538 char *buf)
539{
540 struct watchdog_device *wdd = dev_get_drvdata(dev);
541
542 return sprintf(buf, "%s\n", wdd->info->identity);
543}
544static DEVICE_ATTR_RO(identity);
545
546static ssize_t state_show(struct device *dev, struct device_attribute *attr,
547 char *buf)
548{
549 struct watchdog_device *wdd = dev_get_drvdata(dev);
550
551 if (watchdog_active(wdd))
552 return sprintf(buf, "active\n");
553
554 return sprintf(buf, "inactive\n");
555}
556static DEVICE_ATTR_RO(state);
557
558static ssize_t pretimeout_available_governors_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
560{
561 return watchdog_pretimeout_available_governors_get(buf);
562}
563static DEVICE_ATTR_RO(pretimeout_available_governors);
564
565static ssize_t pretimeout_governor_show(struct device *dev,
566 struct device_attribute *attr,
567 char *buf)
568{
569 struct watchdog_device *wdd = dev_get_drvdata(dev);
570
571 return watchdog_pretimeout_governor_get(wdd, buf);
572}
573
574static ssize_t pretimeout_governor_store(struct device *dev,
575 struct device_attribute *attr,
576 const char *buf, size_t count)
577{
578 struct watchdog_device *wdd = dev_get_drvdata(dev);
579 int ret = watchdog_pretimeout_governor_set(wdd, buf);
580
581 if (!ret)
582 ret = count;
583
584 return ret;
585}
586static DEVICE_ATTR_RW(pretimeout_governor);
587
588static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr,
589 int n)
590{
591 struct device *dev = kobj_to_dev(kobj);
592 struct watchdog_device *wdd = dev_get_drvdata(dev);
593 umode_t mode = attr->mode;
594
595 if (attr == &dev_attr_timeleft.attr && !wdd->ops->get_timeleft)
596 mode = 0;
597 else if (attr == &dev_attr_pretimeout.attr &&
598 !(wdd->info->options & WDIOF_PRETIMEOUT))
599 mode = 0;
600 else if ((attr == &dev_attr_pretimeout_governor.attr ||
601 attr == &dev_attr_pretimeout_available_governors.attr) &&
602 (!(wdd->info->options & WDIOF_PRETIMEOUT) ||
603 !IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV)))
604 mode = 0;
605
606 return mode;
607}
608static struct attribute *wdt_attrs[] = {
609 &dev_attr_state.attr,
610 &dev_attr_identity.attr,
611 &dev_attr_timeout.attr,
612 &dev_attr_pretimeout.attr,
613 &dev_attr_timeleft.attr,
614 &dev_attr_bootstatus.attr,
615 &dev_attr_status.attr,
616 &dev_attr_nowayout.attr,
617 &dev_attr_pretimeout_governor.attr,
618 &dev_attr_pretimeout_available_governors.attr,
619 NULL,
620};
621
622static const struct attribute_group wdt_group = {
623 .attrs = wdt_attrs,
624 .is_visible = wdt_is_visible,
625};
626__ATTRIBUTE_GROUPS(wdt);
627#else
628#define wdt_groups NULL
629#endif
630
631
632
633
634
635
636
637
638
639
640static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
641 unsigned long arg)
642{
643 if (!wdd->ops->ioctl)
644 return -ENOIOCTLCMD;
645
646 return wdd->ops->ioctl(wdd, cmd, arg);
647}
648
649
650
651
652
653
654
655
656
657
658
659
660
661static ssize_t watchdog_write(struct file *file, const char __user *data,
662 size_t len, loff_t *ppos)
663{
664 struct watchdog_core_data *wd_data = file->private_data;
665 struct watchdog_device *wdd;
666 int err;
667 size_t i;
668 char c;
669
670 if (len == 0)
671 return 0;
672
673
674
675
676
677 clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
678
679
680 for (i = 0; i != len; i++) {
681 if (get_user(c, data + i))
682 return -EFAULT;
683 if (c == 'V')
684 set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
685 }
686
687
688
689 err = -ENODEV;
690 mutex_lock(&wd_data->lock);
691 wdd = wd_data->wdd;
692 if (wdd)
693 err = watchdog_ping(wdd);
694 mutex_unlock(&wd_data->lock);
695
696 if (err < 0)
697 return err;
698
699 return len;
700}
701
702
703
704
705
706
707
708
709
710
711
712static long watchdog_ioctl(struct file *file, unsigned int cmd,
713 unsigned long arg)
714{
715 struct watchdog_core_data *wd_data = file->private_data;
716 void __user *argp = (void __user *)arg;
717 struct watchdog_device *wdd;
718 int __user *p = argp;
719 unsigned int val;
720 int err;
721
722 mutex_lock(&wd_data->lock);
723
724 wdd = wd_data->wdd;
725 if (!wdd) {
726 err = -ENODEV;
727 goto out_ioctl;
728 }
729
730 err = watchdog_ioctl_op(wdd, cmd, arg);
731 if (err != -ENOIOCTLCMD)
732 goto out_ioctl;
733
734 switch (cmd) {
735 case WDIOC_GETSUPPORT:
736 err = copy_to_user(argp, wdd->info,
737 sizeof(struct watchdog_info)) ? -EFAULT : 0;
738 break;
739 case WDIOC_GETSTATUS:
740 val = watchdog_get_status(wdd);
741 err = put_user(val, p);
742 break;
743 case WDIOC_GETBOOTSTATUS:
744 err = put_user(wdd->bootstatus, p);
745 break;
746 case WDIOC_SETOPTIONS:
747 if (get_user(val, p)) {
748 err = -EFAULT;
749 break;
750 }
751 if (val & WDIOS_DISABLECARD) {
752 err = watchdog_stop(wdd);
753 if (err < 0)
754 break;
755 }
756 if (val & WDIOS_ENABLECARD)
757 err = watchdog_start(wdd);
758 break;
759 case WDIOC_KEEPALIVE:
760 if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
761 err = -EOPNOTSUPP;
762 break;
763 }
764 err = watchdog_ping(wdd);
765 break;
766 case WDIOC_SETTIMEOUT:
767 if (get_user(val, p)) {
768 err = -EFAULT;
769 break;
770 }
771 err = watchdog_set_timeout(wdd, val);
772 if (err < 0)
773 break;
774
775
776
777 err = watchdog_ping(wdd);
778 if (err < 0)
779 break;
780 fallthrough;
781 case WDIOC_GETTIMEOUT:
782
783 if (wdd->timeout == 0) {
784 err = -EOPNOTSUPP;
785 break;
786 }
787 err = put_user(wdd->timeout, p);
788 break;
789 case WDIOC_GETTIMELEFT:
790 err = watchdog_get_timeleft(wdd, &val);
791 if (err < 0)
792 break;
793 err = put_user(val, p);
794 break;
795 case WDIOC_SETPRETIMEOUT:
796 if (get_user(val, p)) {
797 err = -EFAULT;
798 break;
799 }
800 err = watchdog_set_pretimeout(wdd, val);
801 break;
802 case WDIOC_GETPRETIMEOUT:
803 err = put_user(wdd->pretimeout, p);
804 break;
805 default:
806 err = -ENOTTY;
807 break;
808 }
809
810out_ioctl:
811 mutex_unlock(&wd_data->lock);
812 return err;
813}
814
815
816
817
818
819
820
821
822
823
824
825static int watchdog_open(struct inode *inode, struct file *file)
826{
827 struct watchdog_core_data *wd_data;
828 struct watchdog_device *wdd;
829 bool hw_running;
830 int err;
831
832
833 if (imajor(inode) == MISC_MAJOR)
834 wd_data = old_wd_data;
835 else
836 wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
837 cdev);
838
839
840 if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
841 return -EBUSY;
842
843 wdd = wd_data->wdd;
844
845
846
847
848
849 hw_running = watchdog_hw_running(wdd);
850 if (!hw_running && !try_module_get(wdd->ops->owner)) {
851 err = -EBUSY;
852 goto out_clear;
853 }
854
855 err = watchdog_start(wdd);
856 if (err < 0)
857 goto out_mod;
858
859 file->private_data = wd_data;
860
861 if (!hw_running)
862 get_device(&wd_data->dev);
863
864
865
866
867
868
869
870
871 wd_data->open_deadline = KTIME_MAX;
872
873
874 return stream_open(inode, file);
875
876out_mod:
877 module_put(wd_data->wdd->ops->owner);
878out_clear:
879 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
880 return err;
881}
882
883static void watchdog_core_data_release(struct device *dev)
884{
885 struct watchdog_core_data *wd_data;
886
887 wd_data = container_of(dev, struct watchdog_core_data, dev);
888
889 kfree(wd_data);
890}
891
892
893
894
895
896
897
898
899
900
901
902static int watchdog_release(struct inode *inode, struct file *file)
903{
904 struct watchdog_core_data *wd_data = file->private_data;
905 struct watchdog_device *wdd;
906 int err = -EBUSY;
907 bool running;
908
909 mutex_lock(&wd_data->lock);
910
911 wdd = wd_data->wdd;
912 if (!wdd)
913 goto done;
914
915
916
917
918
919
920 if (!watchdog_active(wdd))
921 err = 0;
922 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
923 !(wdd->info->options & WDIOF_MAGICCLOSE))
924 err = watchdog_stop(wdd);
925
926
927 if (err < 0) {
928 pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
929 watchdog_ping(wdd);
930 }
931
932 watchdog_update_worker(wdd);
933
934
935 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
936
937done:
938 running = wdd && watchdog_hw_running(wdd);
939 mutex_unlock(&wd_data->lock);
940
941
942
943
944
945 if (!running) {
946 module_put(wd_data->cdev.owner);
947 put_device(&wd_data->dev);
948 }
949 return 0;
950}
951
952static const struct file_operations watchdog_fops = {
953 .owner = THIS_MODULE,
954 .write = watchdog_write,
955 .unlocked_ioctl = watchdog_ioctl,
956 .compat_ioctl = compat_ptr_ioctl,
957 .open = watchdog_open,
958 .release = watchdog_release,
959};
960
961static struct miscdevice watchdog_miscdev = {
962 .minor = WATCHDOG_MINOR,
963 .name = "watchdog",
964 .fops = &watchdog_fops,
965};
966
967static struct class watchdog_class = {
968 .name = "watchdog",
969 .owner = THIS_MODULE,
970 .dev_groups = wdt_groups,
971};
972
973
974
975
976
977
978
979
980
981
982static int watchdog_cdev_register(struct watchdog_device *wdd)
983{
984 struct watchdog_core_data *wd_data;
985 int err;
986
987 wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
988 if (!wd_data)
989 return -ENOMEM;
990 mutex_init(&wd_data->lock);
991
992 wd_data->wdd = wdd;
993 wdd->wd_data = wd_data;
994
995 if (IS_ERR_OR_NULL(watchdog_kworker)) {
996 kfree(wd_data);
997 return -ENODEV;
998 }
999
1000 device_initialize(&wd_data->dev);
1001 wd_data->dev.devt = MKDEV(MAJOR(watchdog_devt), wdd->id);
1002 wd_data->dev.class = &watchdog_class;
1003 wd_data->dev.parent = wdd->parent;
1004 wd_data->dev.groups = wdd->groups;
1005 wd_data->dev.release = watchdog_core_data_release;
1006 dev_set_drvdata(&wd_data->dev, wdd);
1007 dev_set_name(&wd_data->dev, "watchdog%d", wdd->id);
1008
1009 kthread_init_work(&wd_data->work, watchdog_ping_work);
1010 hrtimer_init(&wd_data->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
1011 wd_data->timer.function = watchdog_timer_expired;
1012
1013 if (wdd->id == 0) {
1014 old_wd_data = wd_data;
1015 watchdog_miscdev.parent = wdd->parent;
1016 err = misc_register(&watchdog_miscdev);
1017 if (err != 0) {
1018 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
1019 wdd->info->identity, WATCHDOG_MINOR, err);
1020 if (err == -EBUSY)
1021 pr_err("%s: a legacy watchdog module is probably present.\n",
1022 wdd->info->identity);
1023 old_wd_data = NULL;
1024 put_device(&wd_data->dev);
1025 return err;
1026 }
1027 }
1028
1029
1030 cdev_init(&wd_data->cdev, &watchdog_fops);
1031
1032
1033 err = cdev_device_add(&wd_data->cdev, &wd_data->dev);
1034 if (err) {
1035 pr_err("watchdog%d unable to add device %d:%d\n",
1036 wdd->id, MAJOR(watchdog_devt), wdd->id);
1037 if (wdd->id == 0) {
1038 misc_deregister(&watchdog_miscdev);
1039 old_wd_data = NULL;
1040 put_device(&wd_data->dev);
1041 }
1042 return err;
1043 }
1044
1045 wd_data->cdev.owner = wdd->ops->owner;
1046
1047
1048 wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1);
1049 watchdog_set_open_deadline(wd_data);
1050
1051
1052
1053
1054
1055 if (watchdog_hw_running(wdd)) {
1056 __module_get(wdd->ops->owner);
1057 get_device(&wd_data->dev);
1058 if (handle_boot_enabled)
1059 hrtimer_start(&wd_data->timer, 0,
1060 HRTIMER_MODE_REL_HARD);
1061 else
1062 pr_info("watchdog%d running and kernel based pre-userspace handler disabled\n",
1063 wdd->id);
1064 }
1065
1066 return 0;
1067}
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077static void watchdog_cdev_unregister(struct watchdog_device *wdd)
1078{
1079 struct watchdog_core_data *wd_data = wdd->wd_data;
1080
1081 cdev_device_del(&wd_data->cdev, &wd_data->dev);
1082 if (wdd->id == 0) {
1083 misc_deregister(&watchdog_miscdev);
1084 old_wd_data = NULL;
1085 }
1086
1087 if (watchdog_active(wdd) &&
1088 test_bit(WDOG_STOP_ON_UNREGISTER, &wdd->status)) {
1089 watchdog_stop(wdd);
1090 }
1091
1092 mutex_lock(&wd_data->lock);
1093 wd_data->wdd = NULL;
1094 wdd->wd_data = NULL;
1095 mutex_unlock(&wd_data->lock);
1096
1097 hrtimer_cancel(&wd_data->timer);
1098 kthread_cancel_work_sync(&wd_data->work);
1099
1100 put_device(&wd_data->dev);
1101}
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112int watchdog_dev_register(struct watchdog_device *wdd)
1113{
1114 int ret;
1115
1116 ret = watchdog_cdev_register(wdd);
1117 if (ret)
1118 return ret;
1119
1120 ret = watchdog_register_pretimeout(wdd);
1121 if (ret)
1122 watchdog_cdev_unregister(wdd);
1123
1124 return ret;
1125}
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135void watchdog_dev_unregister(struct watchdog_device *wdd)
1136{
1137 watchdog_unregister_pretimeout(wdd);
1138 watchdog_cdev_unregister(wdd);
1139}
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152int watchdog_set_last_hw_keepalive(struct watchdog_device *wdd,
1153 unsigned int last_ping_ms)
1154{
1155 struct watchdog_core_data *wd_data;
1156 ktime_t now;
1157
1158 if (!wdd)
1159 return -EINVAL;
1160
1161 wd_data = wdd->wd_data;
1162
1163 now = ktime_get();
1164
1165 wd_data->last_hw_keepalive = ktime_sub(now, ms_to_ktime(last_ping_ms));
1166
1167 return __watchdog_ping(wdd);
1168}
1169EXPORT_SYMBOL_GPL(watchdog_set_last_hw_keepalive);
1170
1171
1172
1173
1174
1175
1176
1177int __init watchdog_dev_init(void)
1178{
1179 int err;
1180
1181 watchdog_kworker = kthread_create_worker(0, "watchdogd");
1182 if (IS_ERR(watchdog_kworker)) {
1183 pr_err("Failed to create watchdog kworker\n");
1184 return PTR_ERR(watchdog_kworker);
1185 }
1186 sched_set_fifo(watchdog_kworker->task);
1187
1188 err = class_register(&watchdog_class);
1189 if (err < 0) {
1190 pr_err("couldn't register class\n");
1191 goto err_register;
1192 }
1193
1194 err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
1195 if (err < 0) {
1196 pr_err("watchdog: unable to allocate char dev region\n");
1197 goto err_alloc;
1198 }
1199
1200 return 0;
1201
1202err_alloc:
1203 class_unregister(&watchdog_class);
1204err_register:
1205 kthread_destroy_worker(watchdog_kworker);
1206 return err;
1207}
1208
1209
1210
1211
1212
1213
1214
1215void __exit watchdog_dev_exit(void)
1216{
1217 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
1218 class_unregister(&watchdog_class);
1219 kthread_destroy_worker(watchdog_kworker);
1220}
1221
1222module_param(handle_boot_enabled, bool, 0444);
1223MODULE_PARM_DESC(handle_boot_enabled,
1224 "Watchdog core auto-updates boot enabled watchdogs before userspace takes over (default="
1225 __MODULE_STRING(IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED)) ")");
1226
1227module_param(open_timeout, uint, 0644);
1228MODULE_PARM_DESC(open_timeout,
1229 "Maximum time (in seconds, 0 means infinity) for userspace to take over a running watchdog (default="
1230 __MODULE_STRING(CONFIG_WATCHDOG_OPEN_TIMEOUT) ")");
1231