xref: /openbmc/linux/drivers/char/hangcheck-timer.c (revision 940370fc86b920b51a34217a1facc3e9e97c2456)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * hangcheck-timer.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Driver for a little io fencing timer.
51da177e4SLinus Torvalds  *
6696f9486SJoel Becker  * Copyright (C) 2002, 2003 Oracle.  All rights reserved.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Author: Joel Becker <joel.becker@oracle.com>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public
121da177e4SLinus Torvalds  * License version 2 as published by the Free Software Foundation.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
151da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
161da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
171da177e4SLinus Torvalds  * General Public License for more details.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public
201da177e4SLinus Torvalds  * License along with this program; if not, write to the
211da177e4SLinus Torvalds  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
221da177e4SLinus Torvalds  * Boston, MA 021110-1307, USA.
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /*
261da177e4SLinus Torvalds  * The hangcheck-timer driver uses the TSC to catch delays that
271da177e4SLinus Torvalds  * jiffies does not notice.  A timer is set.  When the timer fires, it
281da177e4SLinus Torvalds  * checks whether it was delayed and if that delay exceeds a given
298dfba4d7SJoe Perches  * margin of error.  The hangcheck_tick module parameter takes the timer
301da177e4SLinus Torvalds  * duration in seconds.  The hangcheck_margin parameter defines the
311da177e4SLinus Torvalds  * margin of error, in seconds.  The defaults are 60 seconds for the
321da177e4SLinus Torvalds  * timer and 180 seconds for the margin of error.  IOW, a timer is set
331da177e4SLinus Torvalds  * for 60 seconds.  When the timer fires, the callback checks the
341da177e4SLinus Torvalds  * actual duration that the timer waited.  If the duration exceeds the
351da177e4SLinus Torvalds  * alloted time and margin (here 60 + 180, or 240 seconds), the machine
361da177e4SLinus Torvalds  * is restarted.  A healthy machine will have the duration match the
371da177e4SLinus Torvalds  * expected timeout very closely.
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <linux/module.h>
411da177e4SLinus Torvalds #include <linux/moduleparam.h>
421da177e4SLinus Torvalds #include <linux/types.h>
431da177e4SLinus Torvalds #include <linux/kernel.h>
441da177e4SLinus Torvalds #include <linux/fs.h>
451da177e4SLinus Torvalds #include <linux/mm.h>
461da177e4SLinus Torvalds #include <linux/reboot.h>
471da177e4SLinus Torvalds #include <linux/init.h>
48696f9486SJoel Becker #include <linux/delay.h>
491da177e4SLinus Torvalds #include <asm/uaccess.h>
50696f9486SJoel Becker #include <linux/sysrq.h>
51e8edc6e0SAlexey Dobriyan #include <linux/timer.h>
52*940370fcSYury Polyanskiy #include <linux/time.h>
531da177e4SLinus Torvalds 
54*940370fcSYury Polyanskiy #define VERSION_STR "0.9.1"
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #define DEFAULT_IOFENCE_MARGIN 60	/* Default fudge factor, in seconds */
571da177e4SLinus Torvalds #define DEFAULT_IOFENCE_TICK 180	/* Default timer timeout, in seconds */
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds static int hangcheck_tick = DEFAULT_IOFENCE_TICK;
601da177e4SLinus Torvalds static int hangcheck_margin = DEFAULT_IOFENCE_MARGIN;
611da177e4SLinus Torvalds static int hangcheck_reboot;  /* Defaults to not reboot */
62696f9486SJoel Becker static int hangcheck_dump_tasks;  /* Defaults to not dumping SysRQ T */
631da177e4SLinus Torvalds 
64696f9486SJoel Becker /* options - modular */
651da177e4SLinus Torvalds module_param(hangcheck_tick, int, 0);
661da177e4SLinus Torvalds MODULE_PARM_DESC(hangcheck_tick, "Timer delay.");
671da177e4SLinus Torvalds module_param(hangcheck_margin, int, 0);
681da177e4SLinus Torvalds MODULE_PARM_DESC(hangcheck_margin, "If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire.");
691da177e4SLinus Torvalds module_param(hangcheck_reboot, int, 0);
701da177e4SLinus Torvalds MODULE_PARM_DESC(hangcheck_reboot, "If nonzero, the machine will reboot when the timer margin is exceeded.");
71696f9486SJoel Becker module_param(hangcheck_dump_tasks, int, 0);
72696f9486SJoel Becker MODULE_PARM_DESC(hangcheck_dump_tasks, "If nonzero, the machine will dump the system task state when the timer margin is exceeded.");
731da177e4SLinus Torvalds 
74696f9486SJoel Becker MODULE_AUTHOR("Oracle");
751da177e4SLinus Torvalds MODULE_DESCRIPTION("Hangcheck-timer detects when the system has gone out to lunch past a certain margin.");
761da177e4SLinus Torvalds MODULE_LICENSE("GPL");
77696f9486SJoel Becker MODULE_VERSION(VERSION_STR);
78696f9486SJoel Becker 
79696f9486SJoel Becker /* options - nonmodular */
80696f9486SJoel Becker #ifndef MODULE
81696f9486SJoel Becker 
82696f9486SJoel Becker static int __init hangcheck_parse_tick(char *str)
83696f9486SJoel Becker {
84696f9486SJoel Becker 	int par;
85696f9486SJoel Becker 	if (get_option(&str,&par))
86696f9486SJoel Becker 		hangcheck_tick = par;
87696f9486SJoel Becker 	return 1;
88696f9486SJoel Becker }
89696f9486SJoel Becker 
90696f9486SJoel Becker static int __init hangcheck_parse_margin(char *str)
91696f9486SJoel Becker {
92696f9486SJoel Becker 	int par;
93696f9486SJoel Becker 	if (get_option(&str,&par))
94696f9486SJoel Becker 		hangcheck_margin = par;
95696f9486SJoel Becker 	return 1;
96696f9486SJoel Becker }
97696f9486SJoel Becker 
98696f9486SJoel Becker static int __init hangcheck_parse_reboot(char *str)
99696f9486SJoel Becker {
100696f9486SJoel Becker 	int par;
101696f9486SJoel Becker 	if (get_option(&str,&par))
102696f9486SJoel Becker 		hangcheck_reboot = par;
103696f9486SJoel Becker 	return 1;
104696f9486SJoel Becker }
105696f9486SJoel Becker 
106696f9486SJoel Becker static int __init hangcheck_parse_dump_tasks(char *str)
107696f9486SJoel Becker {
108696f9486SJoel Becker 	int par;
109696f9486SJoel Becker 	if (get_option(&str,&par))
110696f9486SJoel Becker 		hangcheck_dump_tasks = par;
111696f9486SJoel Becker 	return 1;
112696f9486SJoel Becker }
113696f9486SJoel Becker 
114696f9486SJoel Becker __setup("hcheck_tick", hangcheck_parse_tick);
115696f9486SJoel Becker __setup("hcheck_margin", hangcheck_parse_margin);
116696f9486SJoel Becker __setup("hcheck_reboot", hangcheck_parse_reboot);
117696f9486SJoel Becker __setup("hcheck_dump_tasks", hangcheck_parse_dump_tasks);
118696f9486SJoel Becker #endif /* not MODULE */
119696f9486SJoel Becker 
1201489939fSjohn stultz #if defined(CONFIG_S390)
121696f9486SJoel Becker # define HAVE_MONOTONIC
122696f9486SJoel Becker # define TIMER_FREQ 1000000000ULL
123ede65f39SAndrew Morton #else
124*940370fcSYury Polyanskiy # define TIMER_FREQ 1000000000ULL
125696f9486SJoel Becker #endif
126696f9486SJoel Becker 
127696f9486SJoel Becker #ifdef HAVE_MONOTONIC
128696f9486SJoel Becker extern unsigned long long monotonic_clock(void);
129696f9486SJoel Becker #else
130696f9486SJoel Becker static inline unsigned long long monotonic_clock(void)
131696f9486SJoel Becker {
132*940370fcSYury Polyanskiy 	struct timespec ts;
133*940370fcSYury Polyanskiy 	getrawmonotonic(&ts);
134*940370fcSYury Polyanskiy 	return timespec_to_ns(&ts);
135696f9486SJoel Becker }
136696f9486SJoel Becker #endif  /* HAVE_MONOTONIC */
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /* Last time scheduled */
1401da177e4SLinus Torvalds static unsigned long long hangcheck_tsc, hangcheck_tsc_margin;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static void hangcheck_fire(unsigned long);
1431da177e4SLinus Torvalds 
1448d06afabSIngo Molnar static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire, 0, 0);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds static void hangcheck_fire(unsigned long data)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	unsigned long long cur_tsc, tsc_diff;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	cur_tsc = monotonic_clock();
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	if (cur_tsc > hangcheck_tsc)
1541da177e4SLinus Torvalds 		tsc_diff = cur_tsc - hangcheck_tsc;
1551da177e4SLinus Torvalds 	else
1561da177e4SLinus Torvalds 		tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	if (tsc_diff > hangcheck_tsc_margin) {
159696f9486SJoel Becker 		if (hangcheck_dump_tasks) {
160696f9486SJoel Becker 			printk(KERN_CRIT "Hangcheck: Task state:\n");
161696f9486SJoel Becker #ifdef CONFIG_MAGIC_SYSRQ
1627d12e780SDavid Howells 			handle_sysrq('t', NULL);
163696f9486SJoel Becker #endif  /* CONFIG_MAGIC_SYSRQ */
164696f9486SJoel Becker 		}
1651da177e4SLinus Torvalds 		if (hangcheck_reboot) {
1661da177e4SLinus Torvalds 			printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n");
167970d3244SEric W. Biederman 			emergency_restart();
1681da177e4SLinus Torvalds 		} else {
1691da177e4SLinus Torvalds 			printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n");
1701da177e4SLinus Torvalds 		}
1711da177e4SLinus Torvalds 	}
172*940370fcSYury Polyanskiy #if 0
173*940370fcSYury Polyanskiy 	/*
174*940370fcSYury Polyanskiy 	 * Enable to investigate delays in detail
175*940370fcSYury Polyanskiy 	 */
176*940370fcSYury Polyanskiy 	printk("Hangcheck: called %Ld ns since last time (%Ld ns overshoot)\n",
177*940370fcSYury Polyanskiy 			tsc_diff, tsc_diff - hangcheck_tick*TIMER_FREQ);
178*940370fcSYury Polyanskiy #endif
1791da177e4SLinus Torvalds 	mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
1801da177e4SLinus Torvalds 	hangcheck_tsc = monotonic_clock();
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds static int __init hangcheck_init(void)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds 	printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n",
1871da177e4SLinus Torvalds 	       VERSION_STR, hangcheck_tick, hangcheck_margin);
188696f9486SJoel Becker #if defined (HAVE_MONOTONIC)
189696f9486SJoel Becker 	printk("Hangcheck: Using monotonic_clock().\n");
190696f9486SJoel Becker #else
191*940370fcSYury Polyanskiy 	printk("Hangcheck: Using getrawmonotonic().\n");
192696f9486SJoel Becker #endif  /* HAVE_MONOTONIC */
193696f9486SJoel Becker 	hangcheck_tsc_margin =
194696f9486SJoel Becker 		(unsigned long long)(hangcheck_margin + hangcheck_tick);
195696f9486SJoel Becker 	hangcheck_tsc_margin *= (unsigned long long)TIMER_FREQ;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	hangcheck_tsc = monotonic_clock();
1981da177e4SLinus Torvalds 	mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	return 0;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static void __exit hangcheck_exit(void)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	del_timer_sync(&hangcheck_ticktock);
207696f9486SJoel Becker         printk("Hangcheck: Stopped hangcheck timer.\n");
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds module_init(hangcheck_init);
2111da177e4SLinus Torvalds module_exit(hangcheck_exit);
212