| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * hangcheck-timer.c | 
|  | 3 | * | 
|  | 4 | * Driver for a little io fencing timer. | 
|  | 5 | * | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 6 | * Copyright (C) 2002, 2003 Oracle.  All rights reserved. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * | 
|  | 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> | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 47 | #include <linux/smp_lock.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/init.h> | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 49 | #include <linux/delay.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <asm/uaccess.h> | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 51 | #include <linux/sysrq.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 |  | 
|  | 53 |  | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 54 | #define VERSION_STR "0.9.0" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | #define DEFAULT_IOFENCE_MARGIN 60	/* Default fudge factor, in seconds */ | 
|  | 57 | #define DEFAULT_IOFENCE_TICK 180	/* Default timer timeout, in seconds */ | 
|  | 58 |  | 
|  | 59 | static int hangcheck_tick = DEFAULT_IOFENCE_TICK; | 
|  | 60 | static int hangcheck_margin = DEFAULT_IOFENCE_MARGIN; | 
|  | 61 | static int hangcheck_reboot;  /* Defaults to not reboot */ | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 62 | static int hangcheck_dump_tasks;  /* Defaults to not dumping SysRQ T */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 |  | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 64 | /* options - modular */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | module_param(hangcheck_tick, int, 0); | 
|  | 66 | MODULE_PARM_DESC(hangcheck_tick, "Timer delay."); | 
|  | 67 | module_param(hangcheck_margin, int, 0); | 
|  | 68 | MODULE_PARM_DESC(hangcheck_margin, "If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire."); | 
|  | 69 | module_param(hangcheck_reboot, int, 0); | 
|  | 70 | MODULE_PARM_DESC(hangcheck_reboot, "If nonzero, the machine will reboot when the timer margin is exceeded."); | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 71 | module_param(hangcheck_dump_tasks, int, 0); | 
|  | 72 | MODULE_PARM_DESC(hangcheck_dump_tasks, "If nonzero, the machine will dump the system task state when the timer margin is exceeded."); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 |  | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 74 | MODULE_AUTHOR("Oracle"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | MODULE_DESCRIPTION("Hangcheck-timer detects when the system has gone out to lunch past a certain margin."); | 
|  | 76 | MODULE_LICENSE("GPL"); | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 77 | MODULE_VERSION(VERSION_STR); | 
|  | 78 |  | 
|  | 79 | /* options - nonmodular */ | 
|  | 80 | #ifndef MODULE | 
|  | 81 |  | 
|  | 82 | static int __init hangcheck_parse_tick(char *str) | 
|  | 83 | { | 
|  | 84 | int par; | 
|  | 85 | if (get_option(&str,&par)) | 
|  | 86 | hangcheck_tick = par; | 
|  | 87 | return 1; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | static int __init hangcheck_parse_margin(char *str) | 
|  | 91 | { | 
|  | 92 | int par; | 
|  | 93 | if (get_option(&str,&par)) | 
|  | 94 | hangcheck_margin = par; | 
|  | 95 | return 1; | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | static int __init hangcheck_parse_reboot(char *str) | 
|  | 99 | { | 
|  | 100 | int par; | 
|  | 101 | if (get_option(&str,&par)) | 
|  | 102 | hangcheck_reboot = par; | 
|  | 103 | return 1; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | static int __init hangcheck_parse_dump_tasks(char *str) | 
|  | 107 | { | 
|  | 108 | int par; | 
|  | 109 | if (get_option(&str,&par)) | 
|  | 110 | hangcheck_dump_tasks = par; | 
|  | 111 | return 1; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | __setup("hcheck_tick", hangcheck_parse_tick); | 
|  | 115 | __setup("hcheck_margin", hangcheck_parse_margin); | 
|  | 116 | __setup("hcheck_reboot", hangcheck_parse_reboot); | 
|  | 117 | __setup("hcheck_dump_tasks", hangcheck_parse_dump_tasks); | 
|  | 118 | #endif /* not MODULE */ | 
|  | 119 |  | 
| Brian Gerst | 0d078f6 | 2005-10-30 14:59:20 -0800 | [diff] [blame] | 120 | #if defined(CONFIG_X86) | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 121 | # define HAVE_MONOTONIC | 
|  | 122 | # define TIMER_FREQ 1000000000ULL | 
| Martin Schwidefsky | 347a8dc | 2006-01-06 00:19:28 -0800 | [diff] [blame] | 123 | #elif defined(CONFIG_S390) | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 124 | /* FA240000 is 1 Second in the IBM time universe (Page 4-38 Principles of Op for zSeries */ | 
|  | 125 | # define TIMER_FREQ 0xFA240000ULL | 
|  | 126 | #elif defined(CONFIG_IA64) | 
|  | 127 | # define TIMER_FREQ ((unsigned long long)local_cpu_data->itc_freq) | 
|  | 128 | #elif defined(CONFIG_PPC64) | 
|  | 129 | # define TIMER_FREQ (HZ*loops_per_jiffy) | 
|  | 130 | #endif | 
|  | 131 |  | 
|  | 132 | #ifdef HAVE_MONOTONIC | 
|  | 133 | extern unsigned long long monotonic_clock(void); | 
|  | 134 | #else | 
|  | 135 | static inline unsigned long long monotonic_clock(void) | 
|  | 136 | { | 
|  | 137 | # ifdef __s390__ | 
|  | 138 | /* returns the TOD.  see 4-38 Principles of Op of zSeries */ | 
|  | 139 | return get_clock(); | 
|  | 140 | # else | 
|  | 141 | return get_cycles(); | 
|  | 142 | # endif  /* __s390__ */ | 
|  | 143 | } | 
|  | 144 | #endif  /* HAVE_MONOTONIC */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 |  | 
|  | 146 |  | 
|  | 147 | /* Last time scheduled */ | 
|  | 148 | static unsigned long long hangcheck_tsc, hangcheck_tsc_margin; | 
|  | 149 |  | 
|  | 150 | static void hangcheck_fire(unsigned long); | 
|  | 151 |  | 
| Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 152 | static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire, 0, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 |  | 
|  | 155 | static void hangcheck_fire(unsigned long data) | 
|  | 156 | { | 
|  | 157 | unsigned long long cur_tsc, tsc_diff; | 
|  | 158 |  | 
|  | 159 | cur_tsc = monotonic_clock(); | 
|  | 160 |  | 
|  | 161 | if (cur_tsc > hangcheck_tsc) | 
|  | 162 | tsc_diff = cur_tsc - hangcheck_tsc; | 
|  | 163 | else | 
|  | 164 | tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */ | 
|  | 165 |  | 
|  | 166 | if (tsc_diff > hangcheck_tsc_margin) { | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 167 | if (hangcheck_dump_tasks) { | 
|  | 168 | printk(KERN_CRIT "Hangcheck: Task state:\n"); | 
|  | 169 | #ifdef CONFIG_MAGIC_SYSRQ | 
|  | 170 | handle_sysrq('t', NULL, NULL); | 
|  | 171 | #endif  /* CONFIG_MAGIC_SYSRQ */ | 
|  | 172 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (hangcheck_reboot) { | 
|  | 174 | printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n"); | 
| Eric W. Biederman | 970d324 | 2005-07-26 11:55:59 -0600 | [diff] [blame] | 175 | emergency_restart(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } else { | 
|  | 177 | printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n"); | 
|  | 178 | } | 
|  | 179 | } | 
|  | 180 | mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ)); | 
|  | 181 | hangcheck_tsc = monotonic_clock(); | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 |  | 
|  | 185 | static int __init hangcheck_init(void) | 
|  | 186 | { | 
|  | 187 | printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n", | 
|  | 188 | VERSION_STR, hangcheck_tick, hangcheck_margin); | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 189 | #if defined (HAVE_MONOTONIC) | 
|  | 190 | printk("Hangcheck: Using monotonic_clock().\n"); | 
|  | 191 | #elif defined(__s390__) | 
|  | 192 | printk("Hangcheck: Using TOD.\n"); | 
|  | 193 | #else | 
|  | 194 | printk("Hangcheck: Using get_cycles().\n"); | 
|  | 195 | #endif  /* HAVE_MONOTONIC */ | 
|  | 196 | hangcheck_tsc_margin = | 
|  | 197 | (unsigned long long)(hangcheck_margin + hangcheck_tick); | 
|  | 198 | hangcheck_tsc_margin *= (unsigned long long)TIMER_FREQ; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 |  | 
|  | 200 | hangcheck_tsc = monotonic_clock(); | 
|  | 201 | mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ)); | 
|  | 202 |  | 
|  | 203 | return 0; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 |  | 
|  | 207 | static void __exit hangcheck_exit(void) | 
|  | 208 | { | 
|  | 209 | del_timer_sync(&hangcheck_ticktock); | 
| Joel Becker | 696f948 | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 210 | printk("Hangcheck: Stopped hangcheck timer.\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
|  | 213 | module_init(hangcheck_init); | 
|  | 214 | module_exit(hangcheck_exit); |