blob: 4181bcc1c7960b44ec24ea8491d00afdd10dcba6 [file] [log] [blame]
Thomas Gleixner921a3d42019-06-01 10:08:20 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * hangcheck-timer.c
4 *
5 * Driver for a little io fencing timer.
6 *
Joel Becker696f9482005-05-01 08:59:08 -07007 * Copyright (C) 2002, 2003 Oracle. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Author: Joel Becker <joel.becker@oracle.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12/*
13 * The hangcheck-timer driver uses the TSC to catch delays that
14 * jiffies does not notice. A timer is set. When the timer fires, it
15 * checks whether it was delayed and if that delay exceeds a given
Joe Perches8dfba4d2008-02-03 17:11:42 +020016 * margin of error. The hangcheck_tick module parameter takes the timer
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * duration in seconds. The hangcheck_margin parameter defines the
18 * margin of error, in seconds. The defaults are 60 seconds for the
19 * timer and 180 seconds for the margin of error. IOW, a timer is set
20 * for 60 seconds. When the timer fires, the callback checks the
21 * actual duration that the timer waited. If the duration exceeds the
Shile Zhang8b932ed2017-03-24 05:41:04 +080022 * allotted time and margin (here 60 + 180, or 240 seconds), the machine
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * is restarted. A healthy machine will have the duration match the
24 * expected timeout very closely.
25 */
26
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/types.h>
30#include <linux/kernel.h>
31#include <linux/fs.h>
32#include <linux/mm.h>
33#include <linux/reboot.h>
34#include <linux/init.h>
Joel Becker696f9482005-05-01 08:59:08 -070035#include <linux/delay.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080036#include <linux/uaccess.h>
Joel Becker696f9482005-05-01 08:59:08 -070037#include <linux/sysrq.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040038#include <linux/timer.h>
Thomas Gleixner2044fdb02014-07-16 21:05:03 +000039#include <linux/hrtimer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Yury Polyanskiy940370f2010-05-24 14:33:02 -070041#define VERSION_STR "0.9.1"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define DEFAULT_IOFENCE_MARGIN 60 /* Default fudge factor, in seconds */
44#define DEFAULT_IOFENCE_TICK 180 /* Default timer timeout, in seconds */
45
46static int hangcheck_tick = DEFAULT_IOFENCE_TICK;
47static int hangcheck_margin = DEFAULT_IOFENCE_MARGIN;
48static int hangcheck_reboot; /* Defaults to not reboot */
Joel Becker696f9482005-05-01 08:59:08 -070049static int hangcheck_dump_tasks; /* Defaults to not dumping SysRQ T */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Joel Becker696f9482005-05-01 08:59:08 -070051/* options - modular */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052module_param(hangcheck_tick, int, 0);
53MODULE_PARM_DESC(hangcheck_tick, "Timer delay.");
54module_param(hangcheck_margin, int, 0);
55MODULE_PARM_DESC(hangcheck_margin, "If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire.");
56module_param(hangcheck_reboot, int, 0);
57MODULE_PARM_DESC(hangcheck_reboot, "If nonzero, the machine will reboot when the timer margin is exceeded.");
Joel Becker696f9482005-05-01 08:59:08 -070058module_param(hangcheck_dump_tasks, int, 0);
59MODULE_PARM_DESC(hangcheck_dump_tasks, "If nonzero, the machine will dump the system task state when the timer margin is exceeded.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Joel Becker696f9482005-05-01 08:59:08 -070061MODULE_AUTHOR("Oracle");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_DESCRIPTION("Hangcheck-timer detects when the system has gone out to lunch past a certain margin.");
63MODULE_LICENSE("GPL");
Joel Becker696f9482005-05-01 08:59:08 -070064MODULE_VERSION(VERSION_STR);
65
66/* options - nonmodular */
67#ifndef MODULE
68
69static int __init hangcheck_parse_tick(char *str)
70{
71 int par;
72 if (get_option(&str,&par))
73 hangcheck_tick = par;
74 return 1;
75}
76
77static int __init hangcheck_parse_margin(char *str)
78{
79 int par;
80 if (get_option(&str,&par))
81 hangcheck_margin = par;
82 return 1;
83}
84
85static int __init hangcheck_parse_reboot(char *str)
86{
87 int par;
88 if (get_option(&str,&par))
89 hangcheck_reboot = par;
90 return 1;
91}
92
93static int __init hangcheck_parse_dump_tasks(char *str)
94{
95 int par;
96 if (get_option(&str,&par))
97 hangcheck_dump_tasks = par;
98 return 1;
99}
100
101__setup("hcheck_tick", hangcheck_parse_tick);
102__setup("hcheck_margin", hangcheck_parse_margin);
103__setup("hcheck_reboot", hangcheck_parse_reboot);
104__setup("hcheck_dump_tasks", hangcheck_parse_dump_tasks);
105#endif /* not MODULE */
106
Thomas Gleixner2044fdb02014-07-16 21:05:03 +0000107#define TIMER_FREQ 1000000000ULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/* Last time scheduled */
110static unsigned long long hangcheck_tsc, hangcheck_tsc_margin;
111
Kees Cook24ed9602017-08-28 11:28:21 -0700112static void hangcheck_fire(struct timer_list *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Kees Cook1d27e3e2017-10-04 16:27:04 -0700114static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Kees Cook24ed9602017-08-28 11:28:21 -0700116static void hangcheck_fire(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 unsigned long long cur_tsc, tsc_diff;
119
Thomas Gleixner2044fdb02014-07-16 21:05:03 +0000120 cur_tsc = ktime_get_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (cur_tsc > hangcheck_tsc)
123 tsc_diff = cur_tsc - hangcheck_tsc;
124 else
125 tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */
126
127 if (tsc_diff > hangcheck_tsc_margin) {
Joel Becker696f9482005-05-01 08:59:08 -0700128 if (hangcheck_dump_tasks) {
129 printk(KERN_CRIT "Hangcheck: Task state:\n");
130#ifdef CONFIG_MAGIC_SYSRQ
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700131 handle_sysrq('t');
Joel Becker696f9482005-05-01 08:59:08 -0700132#endif /* CONFIG_MAGIC_SYSRQ */
133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (hangcheck_reboot) {
135 printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n");
Eric W. Biederman970d3242005-07-26 11:55:59 -0600136 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 } else {
138 printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n");
139 }
140 }
Yury Polyanskiy940370f2010-05-24 14:33:02 -0700141#if 0
142 /*
143 * Enable to investigate delays in detail
144 */
145 printk("Hangcheck: called %Ld ns since last time (%Ld ns overshoot)\n",
146 tsc_diff, tsc_diff - hangcheck_tick*TIMER_FREQ);
147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
Thomas Gleixner2044fdb02014-07-16 21:05:03 +0000149 hangcheck_tsc = ktime_get_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152
153static int __init hangcheck_init(void)
154{
155 printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n",
156 VERSION_STR, hangcheck_tick, hangcheck_margin);
Joel Becker696f9482005-05-01 08:59:08 -0700157 hangcheck_tsc_margin =
Dan Carpenterd0439a52014-10-29 11:42:50 +0300158 (unsigned long long)hangcheck_margin + hangcheck_tick;
159 hangcheck_tsc_margin *= TIMER_FREQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Thomas Gleixner2044fdb02014-07-16 21:05:03 +0000161 hangcheck_tsc = ktime_get_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
163
164 return 0;
165}
166
167
168static void __exit hangcheck_exit(void)
169{
170 del_timer_sync(&hangcheck_ticktock);
Joel Becker696f9482005-05-01 08:59:08 -0700171 printk("Hangcheck: Stopped hangcheck timer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174module_init(hangcheck_init);
175module_exit(hangcheck_exit);