blob: 0e8ceea5ea780a10c0e5f9bbb506da486cd658c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * hangcheck-timer.c
3 *
4 * Driver for a little io fencing timer.
5 *
Joel Becker696f9482005-05-01 08:59:08 -07006 * Copyright (C) 2002, 2003 Oracle. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Author: Joel Becker <joel.becker@oracle.com>
9 *
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.
13 *
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.
18 *
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.
23 */
24
25/*
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 paramter 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.
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>
Joel Becker696f9482005-05-01 08:59:08 -070048#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/uaccess.h>
Joel Becker696f9482005-05-01 08:59:08 -070050#include <linux/sysrq.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040051#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Joel Becker696f9482005-05-01 08:59:08 -070053#define VERSION_STR "0.9.0"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define DEFAULT_IOFENCE_MARGIN 60 /* Default fudge factor, in seconds */
56#define DEFAULT_IOFENCE_TICK 180 /* Default timer timeout, in seconds */
57
58static int hangcheck_tick = DEFAULT_IOFENCE_TICK;
59static int hangcheck_margin = DEFAULT_IOFENCE_MARGIN;
60static int hangcheck_reboot; /* Defaults to not reboot */
Joel Becker696f9482005-05-01 08:59:08 -070061static int hangcheck_dump_tasks; /* Defaults to not dumping SysRQ T */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Joel Becker696f9482005-05-01 08:59:08 -070063/* options - modular */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064module_param(hangcheck_tick, int, 0);
65MODULE_PARM_DESC(hangcheck_tick, "Timer delay.");
66module_param(hangcheck_margin, int, 0);
67MODULE_PARM_DESC(hangcheck_margin, "If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire.");
68module_param(hangcheck_reboot, int, 0);
69MODULE_PARM_DESC(hangcheck_reboot, "If nonzero, the machine will reboot when the timer margin is exceeded.");
Joel Becker696f9482005-05-01 08:59:08 -070070module_param(hangcheck_dump_tasks, int, 0);
71MODULE_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 -070072
Joel Becker696f9482005-05-01 08:59:08 -070073MODULE_AUTHOR("Oracle");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074MODULE_DESCRIPTION("Hangcheck-timer detects when the system has gone out to lunch past a certain margin.");
75MODULE_LICENSE("GPL");
Joel Becker696f9482005-05-01 08:59:08 -070076MODULE_VERSION(VERSION_STR);
77
78/* options - nonmodular */
79#ifndef MODULE
80
81static int __init hangcheck_parse_tick(char *str)
82{
83 int par;
84 if (get_option(&str,&par))
85 hangcheck_tick = par;
86 return 1;
87}
88
89static int __init hangcheck_parse_margin(char *str)
90{
91 int par;
92 if (get_option(&str,&par))
93 hangcheck_margin = par;
94 return 1;
95}
96
97static int __init hangcheck_parse_reboot(char *str)
98{
99 int par;
100 if (get_option(&str,&par))
101 hangcheck_reboot = par;
102 return 1;
103}
104
105static int __init hangcheck_parse_dump_tasks(char *str)
106{
107 int par;
108 if (get_option(&str,&par))
109 hangcheck_dump_tasks = par;
110 return 1;
111}
112
113__setup("hcheck_tick", hangcheck_parse_tick);
114__setup("hcheck_margin", hangcheck_parse_margin);
115__setup("hcheck_reboot", hangcheck_parse_reboot);
116__setup("hcheck_dump_tasks", hangcheck_parse_dump_tasks);
117#endif /* not MODULE */
118
john stultz14899392007-02-16 01:28:20 -0800119#if defined(CONFIG_S390)
Joel Becker696f9482005-05-01 08:59:08 -0700120# define HAVE_MONOTONIC
121# define TIMER_FREQ 1000000000ULL
Joel Becker696f9482005-05-01 08:59:08 -0700122#elif defined(CONFIG_IA64)
123# define TIMER_FREQ ((unsigned long long)local_cpu_data->itc_freq)
Andrew Mortonede65f32006-06-26 00:25:09 -0700124#else
Joel Becker696f9482005-05-01 08:59:08 -0700125# define TIMER_FREQ (HZ*loops_per_jiffy)
126#endif
127
128#ifdef HAVE_MONOTONIC
129extern unsigned long long monotonic_clock(void);
130#else
131static inline unsigned long long monotonic_clock(void)
132{
Joel Becker696f9482005-05-01 08:59:08 -0700133 return get_cycles();
Joel Becker696f9482005-05-01 08:59:08 -0700134}
135#endif /* HAVE_MONOTONIC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137
138/* Last time scheduled */
139static unsigned long long hangcheck_tsc, hangcheck_tsc_margin;
140
141static void hangcheck_fire(unsigned long);
142
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700143static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146static void hangcheck_fire(unsigned long data)
147{
148 unsigned long long cur_tsc, tsc_diff;
149
150 cur_tsc = monotonic_clock();
151
152 if (cur_tsc > hangcheck_tsc)
153 tsc_diff = cur_tsc - hangcheck_tsc;
154 else
155 tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */
156
157 if (tsc_diff > hangcheck_tsc_margin) {
Joel Becker696f9482005-05-01 08:59:08 -0700158 if (hangcheck_dump_tasks) {
159 printk(KERN_CRIT "Hangcheck: Task state:\n");
160#ifdef CONFIG_MAGIC_SYSRQ
David Howells7d12e782006-10-05 14:55:46 +0100161 handle_sysrq('t', NULL);
Joel Becker696f9482005-05-01 08:59:08 -0700162#endif /* CONFIG_MAGIC_SYSRQ */
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (hangcheck_reboot) {
165 printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n");
Eric W. Biederman970d3242005-07-26 11:55:59 -0600166 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 } else {
168 printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n");
169 }
170 }
171 mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
172 hangcheck_tsc = monotonic_clock();
173}
174
175
176static int __init hangcheck_init(void)
177{
178 printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n",
179 VERSION_STR, hangcheck_tick, hangcheck_margin);
Joel Becker696f9482005-05-01 08:59:08 -0700180#if defined (HAVE_MONOTONIC)
181 printk("Hangcheck: Using monotonic_clock().\n");
Joel Becker696f9482005-05-01 08:59:08 -0700182#else
183 printk("Hangcheck: Using get_cycles().\n");
184#endif /* HAVE_MONOTONIC */
185 hangcheck_tsc_margin =
186 (unsigned long long)(hangcheck_margin + hangcheck_tick);
187 hangcheck_tsc_margin *= (unsigned long long)TIMER_FREQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 hangcheck_tsc = monotonic_clock();
190 mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
191
192 return 0;
193}
194
195
196static void __exit hangcheck_exit(void)
197{
198 del_timer_sync(&hangcheck_ticktock);
Joel Becker696f9482005-05-01 08:59:08 -0700199 printk("Hangcheck: Stopped hangcheck timer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202module_init(hangcheck_init);
203module_exit(hangcheck_exit);