blob: 0607406254856a4d071cf48f1338f6ce124747e4 [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
Niklas Cassel8d5755b2017-02-27 13:49:09 +010024#include <linux/hrtimer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.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>
Wolfram Sange65c5822016-05-25 08:37:47 +020030#include <linux/types.h>
31#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define TIMER_MARGIN 60 /* Default is 60 seconds */
Alan Coxa5132ca2012-02-28 22:48:11 +000034static unsigned int soft_margin = TIMER_MARGIN; /* in seconds */
35module_param(soft_margin, uint, 0);
Alan Coxf92d3742008-05-19 14:09:06 +010036MODULE_PARM_DESC(soft_margin,
37 "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
38 __MODULE_STRING(TIMER_MARGIN) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010040static bool nowayout = WATCHDOG_NOWAYOUT;
41module_param(nowayout, bool, 0);
Alan Coxf92d3742008-05-19 14:09:06 +010042MODULE_PARM_DESC(nowayout,
43 "Watchdog cannot be stopped once started (default="
44 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Jingoo Han5f5e1902014-02-27 14:41:42 +090046static int soft_noboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047module_param(soft_noboot, int, 0);
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000048MODULE_PARM_DESC(soft_noboot,
Alan Coxa5132ca2012-02-28 22:48:11 +000049 "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070051static int soft_panic;
52module_param(soft_panic, int, 0);
53MODULE_PARM_DESC(soft_panic,
54 "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
55
Niklas Cassel8d5755b2017-02-27 13:49:09 +010056static struct hrtimer softdog_ticktock;
57static struct hrtimer softdog_preticktock;
58
59static enum hrtimer_restart softdog_fire(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Li RongQing5889f062015-12-17 21:30:02 +080061 module_put(THIS_MODULE);
Wolfram Sang4a23e2b2016-05-25 08:37:49 +020062 if (soft_noboot) {
Joe Perches27c766a2012-02-15 15:06:19 -080063 pr_crit("Triggered - Reboot ignored\n");
Wolfram Sang4a23e2b2016-05-25 08:37:49 +020064 } else if (soft_panic) {
Joe Perches27c766a2012-02-15 15:06:19 -080065 pr_crit("Initiating panic\n");
66 panic("Software Watchdog Timer expired");
Anithra P Janakiraman7fff4be2011-03-28 14:29:19 -070067 } else {
Joe Perches27c766a2012-02-15 15:06:19 -080068 pr_crit("Initiating system reboot\n");
Andrew Morton479d0f42005-07-26 21:41:38 -070069 emergency_restart();
Joe Perches27c766a2012-02-15 15:06:19 -080070 pr_crit("Reboot didn't ?????\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Niklas Cassel8d5755b2017-02-27 13:49:09 +010073 return HRTIMER_NORESTART;
74}
Wolfram Sang44ba0f02016-05-25 08:37:46 +020075
Wolfram Sang2accf322016-10-07 15:41:38 +030076static struct watchdog_device softdog_dev;
77
Niklas Cassel8d5755b2017-02-27 13:49:09 +010078static enum hrtimer_restart softdog_pretimeout(struct hrtimer *timer)
Wolfram Sang2accf322016-10-07 15:41:38 +030079{
80 watchdog_notify_pretimeout(&softdog_dev);
Wolfram Sang2accf322016-10-07 15:41:38 +030081
Niklas Cassel8d5755b2017-02-27 13:49:09 +010082 return HRTIMER_NORESTART;
83}
Wolfram Sang2accf322016-10-07 15:41:38 +030084
Alan Coxa5132ca2012-02-28 22:48:11 +000085static int softdog_ping(struct watchdog_device *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Niklas Cassel8d5755b2017-02-27 13:49:09 +010087 if (!hrtimer_active(&softdog_ticktock))
Li RongQing5889f062015-12-17 21:30:02 +080088 __module_get(THIS_MODULE);
Niklas Cassel8d5755b2017-02-27 13:49:09 +010089 hrtimer_start(&softdog_ticktock, ktime_set(w->timeout, 0),
90 HRTIMER_MODE_REL);
Wolfram Sang2accf322016-10-07 15:41:38 +030091
Wolfram Sang4cbc6902017-02-07 15:03:29 +010092 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
93 if (w->pretimeout)
Niklas Cassel8d5755b2017-02-27 13:49:09 +010094 hrtimer_start(&softdog_preticktock,
95 ktime_set(w->timeout - w->pretimeout, 0),
96 HRTIMER_MODE_REL);
Wolfram Sang4cbc6902017-02-07 15:03:29 +010097 else
Niklas Cassel8d5755b2017-02-27 13:49:09 +010098 hrtimer_cancel(&softdog_preticktock);
Wolfram Sang4cbc6902017-02-07 15:03:29 +010099 }
Wolfram Sang2accf322016-10-07 15:41:38 +0300100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return 0;
102}
103
Alan Coxa5132ca2012-02-28 22:48:11 +0000104static int softdog_stop(struct watchdog_device *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Niklas Cassel8d5755b2017-02-27 13:49:09 +0100106 if (hrtimer_cancel(&softdog_ticktock))
Li RongQing5889f062015-12-17 21:30:02 +0800107 module_put(THIS_MODULE);
108
Wolfram Sang4cbc6902017-02-07 15:03:29 +0100109 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT))
Niklas Cassel8d5755b2017-02-27 13:49:09 +0100110 hrtimer_cancel(&softdog_preticktock);
Wolfram Sang2accf322016-10-07 15:41:38 +0300111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
113}
114
Alan Coxa5132ca2012-02-28 22:48:11 +0000115static struct watchdog_info softdog_info = {
116 .identity = "Software Watchdog",
Wolfram Sang4cbc6902017-02-07 15:03:29 +0100117 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
Alan Coxa5132ca2012-02-28 22:48:11 +0000118};
119
Julia Lawall85f15cf2016-09-01 19:35:26 +0200120static const struct watchdog_ops softdog_ops = {
Alan Coxa5132ca2012-02-28 22:48:11 +0000121 .owner = THIS_MODULE,
122 .start = softdog_ping,
123 .stop = softdog_stop,
Alan Coxa5132ca2012-02-28 22:48:11 +0000124};
125
126static struct watchdog_device softdog_dev = {
127 .info = &softdog_info,
128 .ops = &softdog_ops,
129 .min_timeout = 1,
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200130 .max_timeout = 65535,
131 .timeout = TIMER_MARGIN,
Alan Coxa5132ca2012-02-28 22:48:11 +0000132};
133
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200134static int __init softdog_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 int ret;
137
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200138 watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
Alan Coxa5132ca2012-02-28 22:48:11 +0000139 watchdog_set_nowayout(&softdog_dev, nowayout);
Damien Riegel84ebcc12015-11-20 16:54:55 -0500140 watchdog_stop_on_reboot(&softdog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Niklas Cassel8d5755b2017-02-27 13:49:09 +0100142 hrtimer_init(&softdog_ticktock, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
143 softdog_ticktock.function = softdog_fire;
144
145 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
Wolfram Sang4cbc6902017-02-07 15:03:29 +0100146 softdog_info.options |= WDIOF_PRETIMEOUT;
Niklas Cassel8d5755b2017-02-27 13:49:09 +0100147 hrtimer_init(&softdog_preticktock, CLOCK_MONOTONIC,
148 HRTIMER_MODE_REL);
149 softdog_preticktock.function = softdog_pretimeout;
150 }
Wolfram Sang4cbc6902017-02-07 15:03:29 +0100151
Alan Coxa5132ca2012-02-28 22:48:11 +0000152 ret = watchdog_register_device(&softdog_dev);
Damien Riegel84ebcc12015-11-20 16:54:55 -0500153 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Wolfram Sange8cf96a2016-05-25 08:37:44 +0200156 pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
157 soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 return 0;
160}
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200161module_init(softdog_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200163static void __exit softdog_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Alan Coxa5132ca2012-02-28 22:48:11 +0000165 watchdog_unregister_device(&softdog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
Wolfram Sang0efc70b2016-05-25 08:37:45 +0200167module_exit(softdog_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169MODULE_AUTHOR("Alan Cox");
170MODULE_DESCRIPTION("Software Watchdog Device Driver");
171MODULE_LICENSE("GPL");