blob: c7bdc986dca1c249c2b61ad902504cdd1df4fb2d [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
Wolfram Sang2accf322016-10-07 15:41:38 +030075static struct watchdog_device softdog_dev;
76
77static void softdog_pretimeout(unsigned long data)
78{
79 watchdog_notify_pretimeout(&softdog_dev);
80}
81
82static struct timer_list softdog_preticktock =
83 TIMER_INITIALIZER(softdog_pretimeout, 0, 0);
84
Alan Coxa5132ca2012-02-28 22:48:11 +000085static int softdog_ping(struct watchdog_device *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Wolfram Sang4a23e2b2016-05-25 08:37:49 +020087 if (!mod_timer(&softdog_ticktock, jiffies + (w->timeout * HZ)))
Li RongQing5889f062015-12-17 21:30:02 +080088 __module_get(THIS_MODULE);
Wolfram Sang2accf322016-10-07 15:41:38 +030089
90 if (w->pretimeout)
91 mod_timer(&softdog_preticktock, jiffies +
92 (w->timeout - w->pretimeout) * HZ);
93 else
94 del_timer(&softdog_preticktock);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
97}
98
Alan Coxa5132ca2012-02-28 22:48:11 +000099static int softdog_stop(struct watchdog_device *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200101 if (del_timer(&softdog_ticktock))
Li RongQing5889f062015-12-17 21:30:02 +0800102 module_put(THIS_MODULE);
103
Wolfram Sang2accf322016-10-07 15:41:38 +0300104 del_timer(&softdog_preticktock);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
107}
108
Alan Coxa5132ca2012-02-28 22:48:11 +0000109static struct watchdog_info softdog_info = {
110 .identity = "Software Watchdog",
Wolfram Sang2accf322016-10-07 15:41:38 +0300111 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE |
112 WDIOF_PRETIMEOUT,
Alan Coxa5132ca2012-02-28 22:48:11 +0000113};
114
Julia Lawall85f15cf2016-09-01 19:35:26 +0200115static const struct watchdog_ops softdog_ops = {
Alan Coxa5132ca2012-02-28 22:48:11 +0000116 .owner = THIS_MODULE,
117 .start = softdog_ping,
118 .stop = softdog_stop,
Alan Coxa5132ca2012-02-28 22:48:11 +0000119};
120
121static struct watchdog_device softdog_dev = {
122 .info = &softdog_info,
123 .ops = &softdog_ops,
124 .min_timeout = 1,
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200125 .max_timeout = 65535,
126 .timeout = TIMER_MARGIN,
Alan Coxa5132ca2012-02-28 22:48:11 +0000127};
128
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200129static int __init softdog_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 int ret;
132
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200133 watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
Alan Coxa5132ca2012-02-28 22:48:11 +0000134 watchdog_set_nowayout(&softdog_dev, nowayout);
Damien Riegel84ebcc12015-11-20 16:54:55 -0500135 watchdog_stop_on_reboot(&softdog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Alan Coxa5132ca2012-02-28 22:48:11 +0000137 ret = watchdog_register_device(&softdog_dev);
Damien Riegel84ebcc12015-11-20 16:54:55 -0500138 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200141 pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
142 soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return 0;
145}
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200146module_init(softdog_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200148static void __exit softdog_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Alan Coxa5132ca2012-02-28 22:48:11 +0000150 watchdog_unregister_device(&softdog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200152module_exit(softdog_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154MODULE_AUTHOR("Alan Cox");
155MODULE_DESCRIPTION("Software Watchdog Device Driver");
156MODULE_LICENSE("GPL");