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 29*8dfba4d7SJoe 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> 521da177e4SLinus Torvalds 53696f9486SJoel Becker #define VERSION_STR "0.9.0" 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #define DEFAULT_IOFENCE_MARGIN 60 /* Default fudge factor, in seconds */ 561da177e4SLinus Torvalds #define DEFAULT_IOFENCE_TICK 180 /* Default timer timeout, in seconds */ 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds static int hangcheck_tick = DEFAULT_IOFENCE_TICK; 591da177e4SLinus Torvalds static int hangcheck_margin = DEFAULT_IOFENCE_MARGIN; 601da177e4SLinus Torvalds static int hangcheck_reboot; /* Defaults to not reboot */ 61696f9486SJoel Becker static int hangcheck_dump_tasks; /* Defaults to not dumping SysRQ T */ 621da177e4SLinus Torvalds 63696f9486SJoel Becker /* options - modular */ 641da177e4SLinus Torvalds module_param(hangcheck_tick, int, 0); 651da177e4SLinus Torvalds MODULE_PARM_DESC(hangcheck_tick, "Timer delay."); 661da177e4SLinus Torvalds module_param(hangcheck_margin, int, 0); 671da177e4SLinus Torvalds MODULE_PARM_DESC(hangcheck_margin, "If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire."); 681da177e4SLinus Torvalds module_param(hangcheck_reboot, int, 0); 691da177e4SLinus Torvalds MODULE_PARM_DESC(hangcheck_reboot, "If nonzero, the machine will reboot when the timer margin is exceeded."); 70696f9486SJoel Becker module_param(hangcheck_dump_tasks, int, 0); 71696f9486SJoel Becker MODULE_PARM_DESC(hangcheck_dump_tasks, "If nonzero, the machine will dump the system task state when the timer margin is exceeded."); 721da177e4SLinus Torvalds 73696f9486SJoel Becker MODULE_AUTHOR("Oracle"); 741da177e4SLinus Torvalds MODULE_DESCRIPTION("Hangcheck-timer detects when the system has gone out to lunch past a certain margin."); 751da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 76696f9486SJoel Becker MODULE_VERSION(VERSION_STR); 77696f9486SJoel Becker 78696f9486SJoel Becker /* options - nonmodular */ 79696f9486SJoel Becker #ifndef MODULE 80696f9486SJoel Becker 81696f9486SJoel Becker static int __init hangcheck_parse_tick(char *str) 82696f9486SJoel Becker { 83696f9486SJoel Becker int par; 84696f9486SJoel Becker if (get_option(&str,&par)) 85696f9486SJoel Becker hangcheck_tick = par; 86696f9486SJoel Becker return 1; 87696f9486SJoel Becker } 88696f9486SJoel Becker 89696f9486SJoel Becker static int __init hangcheck_parse_margin(char *str) 90696f9486SJoel Becker { 91696f9486SJoel Becker int par; 92696f9486SJoel Becker if (get_option(&str,&par)) 93696f9486SJoel Becker hangcheck_margin = par; 94696f9486SJoel Becker return 1; 95696f9486SJoel Becker } 96696f9486SJoel Becker 97696f9486SJoel Becker static int __init hangcheck_parse_reboot(char *str) 98696f9486SJoel Becker { 99696f9486SJoel Becker int par; 100696f9486SJoel Becker if (get_option(&str,&par)) 101696f9486SJoel Becker hangcheck_reboot = par; 102696f9486SJoel Becker return 1; 103696f9486SJoel Becker } 104696f9486SJoel Becker 105696f9486SJoel Becker static int __init hangcheck_parse_dump_tasks(char *str) 106696f9486SJoel Becker { 107696f9486SJoel Becker int par; 108696f9486SJoel Becker if (get_option(&str,&par)) 109696f9486SJoel Becker hangcheck_dump_tasks = par; 110696f9486SJoel Becker return 1; 111696f9486SJoel Becker } 112696f9486SJoel Becker 113696f9486SJoel Becker __setup("hcheck_tick", hangcheck_parse_tick); 114696f9486SJoel Becker __setup("hcheck_margin", hangcheck_parse_margin); 115696f9486SJoel Becker __setup("hcheck_reboot", hangcheck_parse_reboot); 116696f9486SJoel Becker __setup("hcheck_dump_tasks", hangcheck_parse_dump_tasks); 117696f9486SJoel Becker #endif /* not MODULE */ 118696f9486SJoel Becker 1191489939fSjohn stultz #if defined(CONFIG_S390) 120696f9486SJoel Becker # define HAVE_MONOTONIC 121696f9486SJoel Becker # define TIMER_FREQ 1000000000ULL 122696f9486SJoel Becker #elif defined(CONFIG_IA64) 123696f9486SJoel Becker # define TIMER_FREQ ((unsigned long long)local_cpu_data->itc_freq) 124ede65f39SAndrew Morton #else 125696f9486SJoel Becker # define TIMER_FREQ (HZ*loops_per_jiffy) 126696f9486SJoel Becker #endif 127696f9486SJoel Becker 128696f9486SJoel Becker #ifdef HAVE_MONOTONIC 129696f9486SJoel Becker extern unsigned long long monotonic_clock(void); 130696f9486SJoel Becker #else 131696f9486SJoel Becker static inline unsigned long long monotonic_clock(void) 132696f9486SJoel Becker { 133696f9486SJoel Becker return get_cycles(); 134696f9486SJoel Becker } 135696f9486SJoel Becker #endif /* HAVE_MONOTONIC */ 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* Last time scheduled */ 1391da177e4SLinus Torvalds static unsigned long long hangcheck_tsc, hangcheck_tsc_margin; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds static void hangcheck_fire(unsigned long); 1421da177e4SLinus Torvalds 1438d06afabSIngo Molnar static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire, 0, 0); 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds static void hangcheck_fire(unsigned long data) 1471da177e4SLinus Torvalds { 1481da177e4SLinus Torvalds unsigned long long cur_tsc, tsc_diff; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds cur_tsc = monotonic_clock(); 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds if (cur_tsc > hangcheck_tsc) 1531da177e4SLinus Torvalds tsc_diff = cur_tsc - hangcheck_tsc; 1541da177e4SLinus Torvalds else 1551da177e4SLinus Torvalds tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */ 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds if (tsc_diff > hangcheck_tsc_margin) { 158696f9486SJoel Becker if (hangcheck_dump_tasks) { 159696f9486SJoel Becker printk(KERN_CRIT "Hangcheck: Task state:\n"); 160696f9486SJoel Becker #ifdef CONFIG_MAGIC_SYSRQ 1617d12e780SDavid Howells handle_sysrq('t', NULL); 162696f9486SJoel Becker #endif /* CONFIG_MAGIC_SYSRQ */ 163696f9486SJoel Becker } 1641da177e4SLinus Torvalds if (hangcheck_reboot) { 1651da177e4SLinus Torvalds printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n"); 166970d3244SEric W. Biederman emergency_restart(); 1671da177e4SLinus Torvalds } else { 1681da177e4SLinus Torvalds printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n"); 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ)); 1721da177e4SLinus Torvalds hangcheck_tsc = monotonic_clock(); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds static int __init hangcheck_init(void) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n", 1791da177e4SLinus Torvalds VERSION_STR, hangcheck_tick, hangcheck_margin); 180696f9486SJoel Becker #if defined (HAVE_MONOTONIC) 181696f9486SJoel Becker printk("Hangcheck: Using monotonic_clock().\n"); 182696f9486SJoel Becker #else 183696f9486SJoel Becker printk("Hangcheck: Using get_cycles().\n"); 184696f9486SJoel Becker #endif /* HAVE_MONOTONIC */ 185696f9486SJoel Becker hangcheck_tsc_margin = 186696f9486SJoel Becker (unsigned long long)(hangcheck_margin + hangcheck_tick); 187696f9486SJoel Becker hangcheck_tsc_margin *= (unsigned long long)TIMER_FREQ; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds hangcheck_tsc = monotonic_clock(); 1901da177e4SLinus Torvalds mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ)); 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds return 0; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds static void __exit hangcheck_exit(void) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds del_timer_sync(&hangcheck_ticktock); 199696f9486SJoel Becker printk("Hangcheck: Stopped hangcheck timer.\n"); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds module_init(hangcheck_init); 2031da177e4SLinus Torvalds module_exit(hangcheck_exit); 204