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> 522044fdb0SThomas Gleixner #include <linux/hrtimer.h> 531da177e4SLinus Torvalds 54940370fcSYury 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 120696f9486SJoel Becker #define TIMER_FREQ 1000000000ULL 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* Last time scheduled */ 1231da177e4SLinus Torvalds static unsigned long long hangcheck_tsc, hangcheck_tsc_margin; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds static void hangcheck_fire(unsigned long); 1261da177e4SLinus Torvalds 1278d06afabSIngo Molnar static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire, 0, 0); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds static void hangcheck_fire(unsigned long data) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds unsigned long long cur_tsc, tsc_diff; 1321da177e4SLinus Torvalds 1332044fdb0SThomas Gleixner cur_tsc = ktime_get_ns(); 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds if (cur_tsc > hangcheck_tsc) 1361da177e4SLinus Torvalds tsc_diff = cur_tsc - hangcheck_tsc; 1371da177e4SLinus Torvalds else 1381da177e4SLinus Torvalds tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */ 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds if (tsc_diff > hangcheck_tsc_margin) { 141696f9486SJoel Becker if (hangcheck_dump_tasks) { 142696f9486SJoel Becker printk(KERN_CRIT "Hangcheck: Task state:\n"); 143696f9486SJoel Becker #ifdef CONFIG_MAGIC_SYSRQ 144f335397dSDmitry Torokhov handle_sysrq('t'); 145696f9486SJoel Becker #endif /* CONFIG_MAGIC_SYSRQ */ 146696f9486SJoel Becker } 1471da177e4SLinus Torvalds if (hangcheck_reboot) { 1481da177e4SLinus Torvalds printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n"); 149970d3244SEric W. Biederman emergency_restart(); 1501da177e4SLinus Torvalds } else { 1511da177e4SLinus Torvalds printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n"); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds } 154940370fcSYury Polyanskiy #if 0 155940370fcSYury Polyanskiy /* 156940370fcSYury Polyanskiy * Enable to investigate delays in detail 157940370fcSYury Polyanskiy */ 158940370fcSYury Polyanskiy printk("Hangcheck: called %Ld ns since last time (%Ld ns overshoot)\n", 159940370fcSYury Polyanskiy tsc_diff, tsc_diff - hangcheck_tick*TIMER_FREQ); 160940370fcSYury Polyanskiy #endif 1611da177e4SLinus Torvalds mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ)); 1622044fdb0SThomas Gleixner hangcheck_tsc = ktime_get_ns(); 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds static int __init hangcheck_init(void) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n", 1691da177e4SLinus Torvalds VERSION_STR, hangcheck_tick, hangcheck_margin); 170696f9486SJoel Becker hangcheck_tsc_margin = 171*d0439a54SDan Carpenter (unsigned long long)hangcheck_margin + hangcheck_tick; 172*d0439a54SDan Carpenter hangcheck_tsc_margin *= TIMER_FREQ; 1731da177e4SLinus Torvalds 1742044fdb0SThomas Gleixner hangcheck_tsc = ktime_get_ns(); 1751da177e4SLinus Torvalds mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ)); 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds return 0; 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static void __exit hangcheck_exit(void) 1821da177e4SLinus Torvalds { 1831da177e4SLinus Torvalds del_timer_sync(&hangcheck_ticktock); 184696f9486SJoel Becker printk("Hangcheck: Stopped hangcheck timer.\n"); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds module_init(hangcheck_init); 1881da177e4SLinus Torvalds module_exit(hangcheck_exit); 189