1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <linux/module.h>
41#include <linux/moduleparam.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
44#include <linux/fs.h>
45#include <linux/mm.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>
53
54#define VERSION_STR "0.9.1"
55
56#define DEFAULT_IOFENCE_MARGIN 60
57#define DEFAULT_IOFENCE_TICK 180
58
59static int hangcheck_tick = DEFAULT_IOFENCE_TICK;
60static int hangcheck_margin = DEFAULT_IOFENCE_MARGIN;
61static int hangcheck_reboot;
62static int hangcheck_dump_tasks;
63
64
65module_param(hangcheck_tick, int, 0);
66MODULE_PARM_DESC(hangcheck_tick, "Timer delay.");
67module_param(hangcheck_margin, int, 0);
68MODULE_PARM_DESC(hangcheck_margin, "If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire.");
69module_param(hangcheck_reboot, int, 0);
70MODULE_PARM_DESC(hangcheck_reboot, "If nonzero, the machine will reboot when the timer margin is exceeded.");
71module_param(hangcheck_dump_tasks, int, 0);
72MODULE_PARM_DESC(hangcheck_dump_tasks, "If nonzero, the machine will dump the system task state when the timer margin is exceeded.");
73
74MODULE_AUTHOR("Oracle");
75MODULE_DESCRIPTION("Hangcheck-timer detects when the system has gone out to lunch past a certain margin.");
76MODULE_LICENSE("GPL");
77MODULE_VERSION(VERSION_STR);
78
79
80#ifndef MODULE
81
82static int __init hangcheck_parse_tick(char *str)
83{
84 int par;
85 if (get_option(&str,&par))
86 hangcheck_tick = par;
87 return 1;
88}
89
90static int __init hangcheck_parse_margin(char *str)
91{
92 int par;
93 if (get_option(&str,&par))
94 hangcheck_margin = par;
95 return 1;
96}
97
98static int __init hangcheck_parse_reboot(char *str)
99{
100 int par;
101 if (get_option(&str,&par))
102 hangcheck_reboot = par;
103 return 1;
104}
105
106static int __init hangcheck_parse_dump_tasks(char *str)
107{
108 int par;
109 if (get_option(&str,&par))
110 hangcheck_dump_tasks = par;
111 return 1;
112}
113
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
119
120#define TIMER_FREQ 1000000000ULL
121
122
123static unsigned long long hangcheck_tsc, hangcheck_tsc_margin;
124
125static void hangcheck_fire(unsigned long);
126
127static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire, 0, 0);
128
129static void hangcheck_fire(unsigned long data)
130{
131 unsigned long long cur_tsc, tsc_diff;
132
133 cur_tsc = ktime_get_ns();
134
135 if (cur_tsc > hangcheck_tsc)
136 tsc_diff = cur_tsc - hangcheck_tsc;
137 else
138 tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc));
139
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
144 handle_sysrq('t');
145#endif
146 }
147 if (hangcheck_reboot) {
148 printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n");
149 emergency_restart();
150 } else {
151 printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n");
152 }
153 }
154#if 0
155
156
157
158 printk("Hangcheck: called %Ld ns since last time (%Ld ns overshoot)\n",
159 tsc_diff, tsc_diff - hangcheck_tick*TIMER_FREQ);
160#endif
161 mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
162 hangcheck_tsc = ktime_get_ns();
163}
164
165
166static int __init hangcheck_init(void)
167{
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;
173
174 hangcheck_tsc = ktime_get_ns();
175 mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
176
177 return 0;
178}
179
180
181static void __exit hangcheck_exit(void)
182{
183 del_timer_sync(&hangcheck_ticktock);
184 printk("Hangcheck: Stopped hangcheck timer.\n");
185}
186
187module_init(hangcheck_init);
188module_exit(hangcheck_exit);
189