blob: 3e4885c1545e605ee7a33a4a05d545cae16e6f4c [file] [log] [blame]
Guenter Roeckd0173272019-06-20 09:28:46 -07001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Alan Coxa5132ca2012-02-28 22:48:11 +00003 * SoftDog: A Software Watchdog Device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00005 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
6 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
9 * warranty for any of this software. This material is provided
10 * "AS-IS" and at no charge.
11 *
12 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
13 *
14 * Software only watchdog driver. Unlike its big brother the WDT501P
15 * driver this won't always recover a failed machine.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Joe Perches27c766a2012-02-15 15:06:19 -080018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Niklas Cassel8d5755b2017-02-27 13:49:09 +010020#include <linux/hrtimer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070022#include <linux/kernel.h>
Wolfram Sange65c5822016-05-25 08:37:47 +020023#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/reboot.h>
Wolfram Sange65c5822016-05-25 08:37:47 +020026#include <linux/types.h>
27#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define TIMER_MARGIN 60 /* Default is 60 seconds */
Alan Coxa5132ca2012-02-28 22:48:11 +000030static unsigned int soft_margin = TIMER_MARGIN; /* in seconds */
31module_param(soft_margin, uint, 0);
Alan Coxf92d3742008-05-19 14:09:06 +010032MODULE_PARM_DESC(soft_margin,
33 "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
34 __MODULE_STRING(TIMER_MARGIN) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010036static bool nowayout = WATCHDOG_NOWAYOUT;
37module_param(nowayout, bool, 0);
Alan Coxf92d3742008-05-19 14:09:06 +010038MODULE_PARM_DESC(nowayout,
39 "Watchdog cannot be stopped once started (default="
40 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jingoo Han5f5e1902014-02-27 14:41:42 +090042static int soft_noboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043module_param(soft_noboot, int, 0);
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000044MODULE_PARM_DESC(soft_noboot,
Alan Coxa5132ca2012-02-28 22:48:11 +000045 "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070047static int soft_panic;
48module_param(soft_panic, int, 0);
49MODULE_PARM_DESC(soft_panic,
50 "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
51
Niklas Cassel8d5755b2017-02-27 13:49:09 +010052static struct hrtimer softdog_ticktock;
53static struct hrtimer softdog_preticktock;
54
55static enum hrtimer_restart softdog_fire(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Li RongQing5889f062015-12-17 21:30:02 +080057 module_put(THIS_MODULE);
Wolfram Sang4a23e2b2016-05-25 08:37:49 +020058 if (soft_noboot) {
Joe Perches27c766a2012-02-15 15:06:19 -080059 pr_crit("Triggered - Reboot ignored\n");
Wolfram Sang4a23e2b2016-05-25 08:37:49 +020060 } else if (soft_panic) {
Joe Perches27c766a2012-02-15 15:06:19 -080061 pr_crit("Initiating panic\n");
62 panic("Software Watchdog Timer expired");
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070063 } else {
Joe Perches27c766a2012-02-15 15:06:19 -080064 pr_crit("Initiating system reboot\n");
Andrew Morton479d0f42005-07-26 21:41:38 -070065 emergency_restart();
Joe Perches27c766a2012-02-15 15:06:19 -080066 pr_crit("Reboot didn't ?????\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Niklas Cassel8d5755b2017-02-27 13:49:09 +010069 return HRTIMER_NORESTART;
70}
Wolfram Sang44ba0f02016-05-25 08:37:46 +020071
Wolfram Sang2accf322016-10-07 15:41:38 +030072static struct watchdog_device softdog_dev;
73
Niklas Cassel8d5755b2017-02-27 13:49:09 +010074static enum hrtimer_restart softdog_pretimeout(struct hrtimer *timer)
Wolfram Sang2accf322016-10-07 15:41:38 +030075{
76 watchdog_notify_pretimeout(&softdog_dev);
Wolfram Sang2accf322016-10-07 15:41:38 +030077
Niklas Cassel8d5755b2017-02-27 13:49:09 +010078 return HRTIMER_NORESTART;
79}
Wolfram Sang2accf322016-10-07 15:41:38 +030080
Alan Coxa5132ca2012-02-28 22:48:11 +000081static int softdog_ping(struct watchdog_device *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Niklas Cassel8d5755b2017-02-27 13:49:09 +010083 if (!hrtimer_active(&softdog_ticktock))
Li RongQing5889f062015-12-17 21:30:02 +080084 __module_get(THIS_MODULE);
Niklas Cassel8d5755b2017-02-27 13:49:09 +010085 hrtimer_start(&softdog_ticktock, ktime_set(w->timeout, 0),
86 HRTIMER_MODE_REL);
Wolfram Sang2accf322016-10-07 15:41:38 +030087
Wolfram Sang4cbc6902017-02-07 15:03:29 +010088 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
89 if (w->pretimeout)
Niklas Cassel8d5755b2017-02-27 13:49:09 +010090 hrtimer_start(&softdog_preticktock,
91 ktime_set(w->timeout - w->pretimeout, 0),
92 HRTIMER_MODE_REL);
Wolfram Sang4cbc6902017-02-07 15:03:29 +010093 else
Niklas Cassel8d5755b2017-02-27 13:49:09 +010094 hrtimer_cancel(&softdog_preticktock);
Wolfram Sang4cbc6902017-02-07 15:03:29 +010095 }
Wolfram Sang2accf322016-10-07 15:41:38 +030096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return 0;
98}
99
Alan Coxa5132ca2012-02-28 22:48:11 +0000100static int softdog_stop(struct watchdog_device *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Niklas Cassel8d5755b2017-02-27 13:49:09 +0100102 if (hrtimer_cancel(&softdog_ticktock))
Li RongQing5889f062015-12-17 21:30:02 +0800103 module_put(THIS_MODULE);
104
Wolfram Sang4cbc6902017-02-07 15:03:29 +0100105 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT))
Niklas Cassel8d5755b2017-02-27 13:49:09 +0100106 hrtimer_cancel(&softdog_preticktock);
Wolfram Sang2accf322016-10-07 15:41:38 +0300107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109}
110
Alan Coxa5132ca2012-02-28 22:48:11 +0000111static struct watchdog_info softdog_info = {
112 .identity = "Software Watchdog",
Wolfram Sang4cbc6902017-02-07 15:03:29 +0100113 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
Alan Coxa5132ca2012-02-28 22:48:11 +0000114};
115
Julia Lawall85f15cf2016-09-01 19:35:26 +0200116static const struct watchdog_ops softdog_ops = {
Alan Coxa5132ca2012-02-28 22:48:11 +0000117 .owner = THIS_MODULE,
118 .start = softdog_ping,
119 .stop = softdog_stop,
Alan Coxa5132ca2012-02-28 22:48:11 +0000120};
121
122static struct watchdog_device softdog_dev = {
123 .info = &softdog_info,
124 .ops = &softdog_ops,
125 .min_timeout = 1,
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200126 .max_timeout = 65535,
127 .timeout = TIMER_MARGIN,
Alan Coxa5132ca2012-02-28 22:48:11 +0000128};
129
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200130static int __init softdog_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 int ret;
133
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200134 watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
Alan Coxa5132ca2012-02-28 22:48:11 +0000135 watchdog_set_nowayout(&softdog_dev, nowayout);
Damien Riegel84ebcc12015-11-20 16:54:55 -0500136 watchdog_stop_on_reboot(&softdog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Niklas Cassel8d5755b2017-02-27 13:49:09 +0100138 hrtimer_init(&softdog_ticktock, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
139 softdog_ticktock.function = softdog_fire;
140
141 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
Wolfram Sang4cbc6902017-02-07 15:03:29 +0100142 softdog_info.options |= WDIOF_PRETIMEOUT;
Niklas Cassel8d5755b2017-02-27 13:49:09 +0100143 hrtimer_init(&softdog_preticktock, CLOCK_MONOTONIC,
144 HRTIMER_MODE_REL);
145 softdog_preticktock.function = softdog_pretimeout;
146 }
Wolfram Sang4cbc6902017-02-07 15:03:29 +0100147
Alan Coxa5132ca2012-02-28 22:48:11 +0000148 ret = watchdog_register_device(&softdog_dev);
Damien Riegel84ebcc12015-11-20 16:54:55 -0500149 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200152 pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
153 soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 return 0;
156}
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200157module_init(softdog_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200159static void __exit softdog_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Alan Coxa5132ca2012-02-28 22:48:11 +0000161 watchdog_unregister_device(&softdog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200163module_exit(softdog_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165MODULE_AUTHOR("Alan Cox");
166MODULE_DESCRIPTION("Software Watchdog Device Driver");
167MODULE_LICENSE("GPL");