11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * hangcheck-timer.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Driver for a little io fencing timer. 51da177e4SLinus Torvalds * 6*696f9486SJoel 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 291da177e4SLinus Torvalds * margin of error. The hangcheck_tick module paramter 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> 47*696f9486SJoel Becker #include <linux/smp_lock.h> 481da177e4SLinus Torvalds #include <linux/init.h> 49*696f9486SJoel Becker #include <linux/delay.h> 501da177e4SLinus Torvalds #include <asm/uaccess.h> 51*696f9486SJoel Becker #include <linux/sysrq.h> 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds 54*696f9486SJoel Becker #define VERSION_STR "0.9.0" 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 */ 62*696f9486SJoel Becker static int hangcheck_dump_tasks; /* Defaults to not dumping SysRQ T */ 631da177e4SLinus Torvalds 64*696f9486SJoel 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."); 71*696f9486SJoel Becker module_param(hangcheck_dump_tasks, int, 0); 72*696f9486SJoel 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 74*696f9486SJoel 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"); 77*696f9486SJoel Becker MODULE_VERSION(VERSION_STR); 78*696f9486SJoel Becker 79*696f9486SJoel Becker /* options - nonmodular */ 80*696f9486SJoel Becker #ifndef MODULE 81*696f9486SJoel Becker 82*696f9486SJoel Becker static int __init hangcheck_parse_tick(char *str) 83*696f9486SJoel Becker { 84*696f9486SJoel Becker int par; 85*696f9486SJoel Becker if (get_option(&str,&par)) 86*696f9486SJoel Becker hangcheck_tick = par; 87*696f9486SJoel Becker return 1; 88*696f9486SJoel Becker } 89*696f9486SJoel Becker 90*696f9486SJoel Becker static int __init hangcheck_parse_margin(char *str) 91*696f9486SJoel Becker { 92*696f9486SJoel Becker int par; 93*696f9486SJoel Becker if (get_option(&str,&par)) 94*696f9486SJoel Becker hangcheck_margin = par; 95*696f9486SJoel Becker return 1; 96*696f9486SJoel Becker } 97*696f9486SJoel Becker 98*696f9486SJoel Becker static int __init hangcheck_parse_reboot(char *str) 99*696f9486SJoel Becker { 100*696f9486SJoel Becker int par; 101*696f9486SJoel Becker if (get_option(&str,&par)) 102*696f9486SJoel Becker hangcheck_reboot = par; 103*696f9486SJoel Becker return 1; 104*696f9486SJoel Becker } 105*696f9486SJoel Becker 106*696f9486SJoel Becker static int __init hangcheck_parse_dump_tasks(char *str) 107*696f9486SJoel Becker { 108*696f9486SJoel Becker int par; 109*696f9486SJoel Becker if (get_option(&str,&par)) 110*696f9486SJoel Becker hangcheck_dump_tasks = par; 111*696f9486SJoel Becker return 1; 112*696f9486SJoel Becker } 113*696f9486SJoel Becker 114*696f9486SJoel Becker __setup("hcheck_tick", hangcheck_parse_tick); 115*696f9486SJoel Becker __setup("hcheck_margin", hangcheck_parse_margin); 116*696f9486SJoel Becker __setup("hcheck_reboot", hangcheck_parse_reboot); 117*696f9486SJoel Becker __setup("hcheck_dump_tasks", hangcheck_parse_dump_tasks); 118*696f9486SJoel Becker #endif /* not MODULE */ 119*696f9486SJoel Becker 120*696f9486SJoel Becker #if defined(CONFIG_X86) || defined(CONFIG_X86_64) 121*696f9486SJoel Becker # define HAVE_MONOTONIC 122*696f9486SJoel Becker # define TIMER_FREQ 1000000000ULL 123*696f9486SJoel Becker #elif defined(CONFIG_ARCH_S390) 124*696f9486SJoel Becker /* FA240000 is 1 Second in the IBM time universe (Page 4-38 Principles of Op for zSeries */ 125*696f9486SJoel Becker # define TIMER_FREQ 0xFA240000ULL 126*696f9486SJoel Becker #elif defined(CONFIG_IA64) 127*696f9486SJoel Becker # define TIMER_FREQ ((unsigned long long)local_cpu_data->itc_freq) 128*696f9486SJoel Becker #elif defined(CONFIG_PPC64) 129*696f9486SJoel Becker # define TIMER_FREQ (HZ*loops_per_jiffy) 130*696f9486SJoel Becker #endif 131*696f9486SJoel Becker 132*696f9486SJoel Becker #ifdef HAVE_MONOTONIC 133*696f9486SJoel Becker extern unsigned long long monotonic_clock(void); 134*696f9486SJoel Becker #else 135*696f9486SJoel Becker static inline unsigned long long monotonic_clock(void) 136*696f9486SJoel Becker { 137*696f9486SJoel Becker # ifdef __s390__ 138*696f9486SJoel Becker /* returns the TOD. see 4-38 Principles of Op of zSeries */ 139*696f9486SJoel Becker return get_clock(); 140*696f9486SJoel Becker # else 141*696f9486SJoel Becker return get_cycles(); 142*696f9486SJoel Becker # endif /* __s390__ */ 143*696f9486SJoel Becker } 144*696f9486SJoel Becker #endif /* HAVE_MONOTONIC */ 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* Last time scheduled */ 1481da177e4SLinus Torvalds static unsigned long long hangcheck_tsc, hangcheck_tsc_margin; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds static void hangcheck_fire(unsigned long); 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds static struct timer_list hangcheck_ticktock = 1531da177e4SLinus Torvalds TIMER_INITIALIZER(hangcheck_fire, 0, 0); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds static void hangcheck_fire(unsigned long data) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds unsigned long long cur_tsc, tsc_diff; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds cur_tsc = monotonic_clock(); 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds if (cur_tsc > hangcheck_tsc) 1631da177e4SLinus Torvalds tsc_diff = cur_tsc - hangcheck_tsc; 1641da177e4SLinus Torvalds else 1651da177e4SLinus Torvalds tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */ 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds if (tsc_diff > hangcheck_tsc_margin) { 168*696f9486SJoel Becker if (hangcheck_dump_tasks) { 169*696f9486SJoel Becker printk(KERN_CRIT "Hangcheck: Task state:\n"); 170*696f9486SJoel Becker #ifdef CONFIG_MAGIC_SYSRQ 171*696f9486SJoel Becker handle_sysrq('t', NULL, NULL); 172*696f9486SJoel Becker #endif /* CONFIG_MAGIC_SYSRQ */ 173*696f9486SJoel Becker } 1741da177e4SLinus Torvalds if (hangcheck_reboot) { 1751da177e4SLinus Torvalds printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n"); 1761da177e4SLinus Torvalds machine_restart(NULL); 1771da177e4SLinus Torvalds } else { 1781da177e4SLinus Torvalds printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n"); 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ)); 1821da177e4SLinus Torvalds hangcheck_tsc = monotonic_clock(); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds static int __init hangcheck_init(void) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n", 1891da177e4SLinus Torvalds VERSION_STR, hangcheck_tick, hangcheck_margin); 190*696f9486SJoel Becker #if defined (HAVE_MONOTONIC) 191*696f9486SJoel Becker printk("Hangcheck: Using monotonic_clock().\n"); 192*696f9486SJoel Becker #elif defined(__s390__) 193*696f9486SJoel Becker printk("Hangcheck: Using TOD.\n"); 194*696f9486SJoel Becker #else 195*696f9486SJoel Becker printk("Hangcheck: Using get_cycles().\n"); 196*696f9486SJoel Becker #endif /* HAVE_MONOTONIC */ 197*696f9486SJoel Becker hangcheck_tsc_margin = 198*696f9486SJoel Becker (unsigned long long)(hangcheck_margin + hangcheck_tick); 199*696f9486SJoel Becker hangcheck_tsc_margin *= (unsigned long long)TIMER_FREQ; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds hangcheck_tsc = monotonic_clock(); 2021da177e4SLinus Torvalds mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ)); 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds return 0; 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds static void __exit hangcheck_exit(void) 2091da177e4SLinus Torvalds { 2101da177e4SLinus Torvalds del_timer_sync(&hangcheck_ticktock); 211*696f9486SJoel Becker printk("Hangcheck: Stopped hangcheck timer.\n"); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds module_init(hangcheck_init); 2151da177e4SLinus Torvalds module_exit(hangcheck_exit); 216