blob: b067edf246dff2e594429b36d968b698bcd71e60 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alan Coxa5132ca2012-02-28 22:48:11 +00002 * SoftDog: A Software Watchdog Device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00004 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
5 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
13 * warranty for any of this software. This material is provided
14 * "AS-IS" and at no charge.
15 *
16 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
17 *
18 * Software only watchdog driver. Unlike its big brother the WDT501P
19 * driver this won't always recover a failed machine.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
Joe Perches27c766a2012-02-15 15:06:19 -080022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080025#include <linux/jiffies.h>
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070026#include <linux/kernel.h>
Wolfram Sange65c5822016-05-25 08:37:47 +020027#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/reboot.h>
30#include <linux/timer.h>
31#include <linux/types.h>
32#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define TIMER_MARGIN 60 /* Default is 60 seconds */
Alan Coxa5132ca2012-02-28 22:48:11 +000035static unsigned int soft_margin = TIMER_MARGIN; /* in seconds */
36module_param(soft_margin, uint, 0);
Alan Coxf92d3742008-05-19 14:09:06 +010037MODULE_PARM_DESC(soft_margin,
38 "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
39 __MODULE_STRING(TIMER_MARGIN) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010041static bool nowayout = WATCHDOG_NOWAYOUT;
42module_param(nowayout, bool, 0);
Alan Coxf92d3742008-05-19 14:09:06 +010043MODULE_PARM_DESC(nowayout,
44 "Watchdog cannot be stopped once started (default="
45 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jingoo Han5f5e1902014-02-27 14:41:42 +090047static int soft_noboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048module_param(soft_noboot, int, 0);
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000049MODULE_PARM_DESC(soft_noboot,
Alan Coxa5132ca2012-02-28 22:48:11 +000050 "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070052static int soft_panic;
53module_param(soft_panic, int, 0);
54MODULE_PARM_DESC(soft_panic,
55 "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
56
Wolfram Sang0efc70b2016-05-25 08:37:45 +020057static void softdog_fire(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Li RongQing5889f062015-12-17 21:30:02 +080059 module_put(THIS_MODULE);
Wolfram Sang4a23e2b2016-05-25 08:37:49 +020060 if (soft_noboot) {
Joe Perches27c766a2012-02-15 15:06:19 -080061 pr_crit("Triggered - Reboot ignored\n");
Wolfram Sang4a23e2b2016-05-25 08:37:49 +020062 } else if (soft_panic) {
Joe Perches27c766a2012-02-15 15:06:19 -080063 pr_crit("Initiating panic\n");
64 panic("Software Watchdog Timer expired");
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070065 } else {
Joe Perches27c766a2012-02-15 15:06:19 -080066 pr_crit("Initiating system reboot\n");
Andrew Morton479d0f42005-07-26 21:41:38 -070067 emergency_restart();
Joe Perches27c766a2012-02-15 15:06:19 -080068 pr_crit("Reboot didn't ?????\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70}
71
Wolfram Sang44ba0f02016-05-25 08:37:46 +020072static struct timer_list softdog_ticktock =
73 TIMER_INITIALIZER(softdog_fire, 0, 0);
74
Alan Coxa5132ca2012-02-28 22:48:11 +000075static int softdog_ping(struct watchdog_device *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Wolfram Sang4a23e2b2016-05-25 08:37:49 +020077 if (!mod_timer(&softdog_ticktock, jiffies + (w->timeout * HZ)))
Li RongQing5889f062015-12-17 21:30:02 +080078 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return 0;
80}
81
Alan Coxa5132ca2012-02-28 22:48:11 +000082static int softdog_stop(struct watchdog_device *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Wolfram Sang0efc70b2016-05-25 08:37:45 +020084 if (del_timer(&softdog_ticktock))
Li RongQing5889f062015-12-17 21:30:02 +080085 module_put(THIS_MODULE);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88}
89
Alan Coxa5132ca2012-02-28 22:48:11 +000090static struct watchdog_info softdog_info = {
91 .identity = "Software Watchdog",
92 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
93};
94
95static struct watchdog_ops softdog_ops = {
96 .owner = THIS_MODULE,
97 .start = softdog_ping,
98 .stop = softdog_stop,
Alan Coxa5132ca2012-02-28 22:48:11 +000099};
100
101static struct watchdog_device softdog_dev = {
102 .info = &softdog_info,
103 .ops = &softdog_ops,
104 .min_timeout = 1,
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200105 .max_timeout = 65535,
106 .timeout = TIMER_MARGIN,
Alan Coxa5132ca2012-02-28 22:48:11 +0000107};
108
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200109static int __init softdog_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 int ret;
112
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200113 watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
Alan Coxa5132ca2012-02-28 22:48:11 +0000114 watchdog_set_nowayout(&softdog_dev, nowayout);
Damien Riegel84ebcc12015-11-20 16:54:55 -0500115 watchdog_stop_on_reboot(&softdog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Alan Coxa5132ca2012-02-28 22:48:11 +0000117 ret = watchdog_register_device(&softdog_dev);
Damien Riegel84ebcc12015-11-20 16:54:55 -0500118 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200121 pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
122 soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return 0;
125}
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200126module_init(softdog_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200128static void __exit softdog_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Alan Coxa5132ca2012-02-28 22:48:11 +0000130 watchdog_unregister_device(&softdog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200132module_exit(softdog_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134MODULE_AUTHOR("Alan Cox");
135MODULE_DESCRIPTION("Software Watchdog Device Driver");
136MODULE_LICENSE("GPL");