4 * Driver for a little io fencing timer.
6 * Copyright (C) 2002, 2003 Oracle. All rights reserved.
8 * Author: Joel Becker <joel.becker@oracle.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 021110-1307, USA.
26 * The hangcheck-timer driver uses the TSC to catch delays that
27 * jiffies does not notice. A timer is set. When the timer fires, it
28 * checks whether it was delayed and if that delay exceeds a given
29 * margin of error. The hangcheck_tick module parameter takes the timer
30 * duration in seconds. The hangcheck_margin parameter defines the
31 * margin of error, in seconds. The defaults are 60 seconds for the
32 * timer and 180 seconds for the margin of error. IOW, a timer is set
33 * for 60 seconds. When the timer fires, the callback checks the
34 * actual duration that the timer waited. If the duration exceeds the
35 * alloted time and margin (here 60 + 180, or 240 seconds), the machine
36 * is restarted. A healthy machine will have the duration match the
37 * expected timeout very closely.
40 #include <linux/module.h>
41 #include <linux/moduleparam.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
46 #include <linux/reboot.h>
47 #include <linux/init.h>
48 #include <linux/delay.h>
49 #include <asm/uaccess.h>
50 #include <linux/sysrq.h>
51 #include <linux/timer.h>
52 #include <linux/hrtimer.h>
54 #define VERSION_STR "0.9.1"
56 #define DEFAULT_IOFENCE_MARGIN 60 /* Default fudge factor, in seconds */
57 #define DEFAULT_IOFENCE_TICK 180 /* Default timer timeout, in seconds */
59 static int hangcheck_tick = DEFAULT_IOFENCE_TICK;
60 static int hangcheck_margin = DEFAULT_IOFENCE_MARGIN;
61 static int hangcheck_reboot; /* Defaults to not reboot */
62 static int hangcheck_dump_tasks; /* Defaults to not dumping SysRQ T */
64 /* options - modular */
65 module_param(hangcheck_tick, int, 0);
66 MODULE_PARM_DESC(hangcheck_tick, "Timer delay.");
67 module_param(hangcheck_margin, int, 0);
68 MODULE_PARM_DESC(hangcheck_margin, "If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire.");
69 module_param(hangcheck_reboot, int, 0);
70 MODULE_PARM_DESC(hangcheck_reboot, "If nonzero, the machine will reboot when the timer margin is exceeded.");
71 module_param(hangcheck_dump_tasks, int, 0);
72 MODULE_PARM_DESC(hangcheck_dump_tasks, "If nonzero, the machine will dump the system task state when the timer margin is exceeded.");
74 MODULE_AUTHOR("Oracle");
75 MODULE_DESCRIPTION("Hangcheck-timer detects when the system has gone out to lunch past a certain margin.");
76 MODULE_LICENSE("GPL");
77 MODULE_VERSION(VERSION_STR);
79 /* options - nonmodular */
82 static int __init hangcheck_parse_tick(char *str)
85 if (get_option(&str,&par))
90 static int __init hangcheck_parse_margin(char *str)
93 if (get_option(&str,&par))
94 hangcheck_margin = par;
98 static int __init hangcheck_parse_reboot(char *str)
101 if (get_option(&str,&par))
102 hangcheck_reboot = par;
106 static int __init hangcheck_parse_dump_tasks(char *str)
109 if (get_option(&str,&par))
110 hangcheck_dump_tasks = par;
114 __setup("hcheck_tick", hangcheck_parse_tick);
115 __setup("hcheck_margin", hangcheck_parse_margin);
116 __setup("hcheck_reboot", hangcheck_parse_reboot);
117 __setup("hcheck_dump_tasks", hangcheck_parse_dump_tasks);
118 #endif /* not MODULE */
120 #define TIMER_FREQ 1000000000ULL
122 /* Last time scheduled */
123 static unsigned long long hangcheck_tsc, hangcheck_tsc_margin;
125 static void hangcheck_fire(unsigned long);
127 static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire, 0, 0);
129 static void hangcheck_fire(unsigned long data)
131 unsigned long long cur_tsc, tsc_diff;
133 cur_tsc = ktime_get_ns();
135 if (cur_tsc > hangcheck_tsc)
136 tsc_diff = cur_tsc - hangcheck_tsc;
138 tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */
140 if (tsc_diff > hangcheck_tsc_margin) {
141 if (hangcheck_dump_tasks) {
142 printk(KERN_CRIT "Hangcheck: Task state:\n");
143 #ifdef CONFIG_MAGIC_SYSRQ
145 #endif /* CONFIG_MAGIC_SYSRQ */
147 if (hangcheck_reboot) {
148 printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n");
151 printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n");
156 * Enable to investigate delays in detail
158 printk("Hangcheck: called %Ld ns since last time (%Ld ns overshoot)\n",
159 tsc_diff, tsc_diff - hangcheck_tick*TIMER_FREQ);
161 mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
162 hangcheck_tsc = ktime_get_ns();
166 static int __init hangcheck_init(void)
168 printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n",
169 VERSION_STR, hangcheck_tick, hangcheck_margin);
170 hangcheck_tsc_margin =
171 (unsigned long long)hangcheck_margin + hangcheck_tick;
172 hangcheck_tsc_margin *= TIMER_FREQ;
174 hangcheck_tsc = ktime_get_ns();
175 mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
181 static void __exit hangcheck_exit(void)
183 del_timer_sync(&hangcheck_ticktock);
184 printk("Hangcheck: Stopped hangcheck timer.\n");
187 module_init(hangcheck_init);
188 module_exit(hangcheck_exit);