blob: a9ad27dd46502b2ea6a794e1653b9e61a4533d39 [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/module.h>
25#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/types.h>
27#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/reboot.h>
30#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080031#include <linux/jiffies.h>
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070032#include <linux/kernel.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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void watchdog_fire(unsigned long);
58
59static struct timer_list watchdog_ticktock =
60 TIMER_INITIALIZER(watchdog_fire, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void watchdog_fire(unsigned long data)
63{
Li RongQing5889f062015-12-17 21:30:02 +080064 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (soft_noboot)
Joe Perches27c766a2012-02-15 15:06:19 -080066 pr_crit("Triggered - Reboot ignored\n");
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070067 else if (soft_panic) {
Joe Perches27c766a2012-02-15 15:06:19 -080068 pr_crit("Initiating panic\n");
69 panic("Software Watchdog Timer expired");
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070070 } else {
Joe Perches27c766a2012-02-15 15:06:19 -080071 pr_crit("Initiating system reboot\n");
Andrew Morton479d0f42005-07-26 21:41:38 -070072 emergency_restart();
Joe Perches27c766a2012-02-15 15:06:19 -080073 pr_crit("Reboot didn't ?????\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75}
76
Alan Coxa5132ca2012-02-28 22:48:11 +000077static int softdog_ping(struct watchdog_device *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Li RongQing5889f062015-12-17 21:30:02 +080079 if (!mod_timer(&watchdog_ticktock, jiffies+(w->timeout*HZ)))
80 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 0;
82}
83
Alan Coxa5132ca2012-02-28 22:48:11 +000084static int softdog_stop(struct watchdog_device *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Li RongQing5889f062015-12-17 21:30:02 +080086 if (del_timer(&watchdog_ticktock))
87 module_put(THIS_MODULE);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return 0;
90}
91
Alan Coxa5132ca2012-02-28 22:48:11 +000092static int softdog_set_timeout(struct watchdog_device *w, unsigned int t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Alan Coxa5132ca2012-02-28 22:48:11 +000094 w->timeout = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return 0;
96}
97
Alan Coxa5132ca2012-02-28 22:48:11 +000098static struct watchdog_info softdog_info = {
99 .identity = "Software Watchdog",
100 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
101};
102
103static struct watchdog_ops softdog_ops = {
104 .owner = THIS_MODULE,
105 .start = softdog_ping,
106 .stop = softdog_stop,
Alan Coxa5132ca2012-02-28 22:48:11 +0000107 .set_timeout = softdog_set_timeout,
108};
109
110static struct watchdog_device softdog_dev = {
111 .info = &softdog_info,
112 .ops = &softdog_ops,
113 .min_timeout = 1,
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200114 .max_timeout = 65535,
115 .timeout = TIMER_MARGIN,
Alan Coxa5132ca2012-02-28 22:48:11 +0000116};
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int __init watchdog_init(void)
119{
120 int ret;
121
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200122 watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
Alan Coxa5132ca2012-02-28 22:48:11 +0000123 watchdog_set_nowayout(&softdog_dev, nowayout);
Damien Riegel84ebcc12015-11-20 16:54:55 -0500124 watchdog_stop_on_reboot(&softdog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Alan Coxa5132ca2012-02-28 22:48:11 +0000126 ret = watchdog_register_device(&softdog_dev);
Damien Riegel84ebcc12015-11-20 16:54:55 -0500127 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200130 pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
131 soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 return 0;
134}
135
136static void __exit watchdog_exit(void)
137{
Alan Coxa5132ca2012-02-28 22:48:11 +0000138 watchdog_unregister_device(&softdog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141module_init(watchdog_init);
142module_exit(watchdog_exit);
143
144MODULE_AUTHOR("Alan Cox");
145MODULE_DESCRIPTION("Software Watchdog Device Driver");
146MODULE_LICENSE("GPL");