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 | * |
| 6 | * Copyright (C) 2002 Oracle Corporation. All rights reserved. |
| 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> |
| 47 | #include <linux/init.h> |
| 48 | #include <asm/uaccess.h> |
| 49 | |
| 50 | |
| 51 | #define VERSION_STR "0.5.0" |
| 52 | |
| 53 | #define DEFAULT_IOFENCE_MARGIN 60 /* Default fudge factor, in seconds */ |
| 54 | #define DEFAULT_IOFENCE_TICK 180 /* Default timer timeout, in seconds */ |
| 55 | |
| 56 | static int hangcheck_tick = DEFAULT_IOFENCE_TICK; |
| 57 | static int hangcheck_margin = DEFAULT_IOFENCE_MARGIN; |
| 58 | static int hangcheck_reboot; /* Defaults to not reboot */ |
| 59 | |
| 60 | /* Driver options */ |
| 61 | module_param(hangcheck_tick, int, 0); |
| 62 | MODULE_PARM_DESC(hangcheck_tick, "Timer delay."); |
| 63 | module_param(hangcheck_margin, int, 0); |
| 64 | MODULE_PARM_DESC(hangcheck_margin, "If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire."); |
| 65 | module_param(hangcheck_reboot, int, 0); |
| 66 | MODULE_PARM_DESC(hangcheck_reboot, "If nonzero, the machine will reboot when the timer margin is exceeded."); |
| 67 | |
| 68 | MODULE_AUTHOR("Joel Becker"); |
| 69 | MODULE_DESCRIPTION("Hangcheck-timer detects when the system has gone out to lunch past a certain margin."); |
| 70 | MODULE_LICENSE("GPL"); |
| 71 | |
| 72 | |
| 73 | /* Last time scheduled */ |
| 74 | static unsigned long long hangcheck_tsc, hangcheck_tsc_margin; |
| 75 | |
| 76 | static void hangcheck_fire(unsigned long); |
| 77 | |
| 78 | static struct timer_list hangcheck_ticktock = |
| 79 | TIMER_INITIALIZER(hangcheck_fire, 0, 0); |
| 80 | |
| 81 | extern unsigned long long monotonic_clock(void); |
| 82 | |
| 83 | static void hangcheck_fire(unsigned long data) |
| 84 | { |
| 85 | unsigned long long cur_tsc, tsc_diff; |
| 86 | |
| 87 | cur_tsc = monotonic_clock(); |
| 88 | |
| 89 | if (cur_tsc > hangcheck_tsc) |
| 90 | tsc_diff = cur_tsc - hangcheck_tsc; |
| 91 | else |
| 92 | tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */ |
| 93 | |
| 94 | if (tsc_diff > hangcheck_tsc_margin) { |
| 95 | if (hangcheck_reboot) { |
| 96 | printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n"); |
| 97 | machine_restart(NULL); |
| 98 | } else { |
| 99 | printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n"); |
| 100 | } |
| 101 | } |
| 102 | mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ)); |
| 103 | hangcheck_tsc = monotonic_clock(); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static int __init hangcheck_init(void) |
| 108 | { |
| 109 | printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n", |
| 110 | VERSION_STR, hangcheck_tick, hangcheck_margin); |
| 111 | |
| 112 | hangcheck_tsc_margin = hangcheck_margin + hangcheck_tick; |
| 113 | hangcheck_tsc_margin *= 1000000000; |
| 114 | |
| 115 | |
| 116 | hangcheck_tsc = monotonic_clock(); |
| 117 | mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ)); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | static void __exit hangcheck_exit(void) |
| 124 | { |
| 125 | del_timer_sync(&hangcheck_ticktock); |
| 126 | } |
| 127 | |
| 128 | module_init(hangcheck_init); |
| 129 | module_exit(hangcheck_exit); |