^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * hangcheck-timer.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Driver for a little io fencing timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2002, 2003 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Author: Joel Becker <joel.becker@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * The hangcheck-timer driver uses the TSC to catch delays that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * jiffies does not notice. A timer is set. When the timer fires, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * checks whether it was delayed and if that delay exceeds a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * margin of error. The hangcheck_tick module parameter takes the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * duration in seconds. The hangcheck_margin parameter defines the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * margin of error, in seconds. The defaults are 60 seconds for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * timer and 180 seconds for the margin of error. IOW, a timer is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * for 60 seconds. When the timer fires, the callback checks the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * actual duration that the timer waited. If the duration exceeds the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * allotted time and margin (here 60 + 180, or 240 seconds), the machine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * is restarted. A healthy machine will have the duration match the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * expected timeout very closely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/sysrq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define VERSION_STR "0.9.1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define DEFAULT_IOFENCE_MARGIN 60 /* Default fudge factor, in seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define DEFAULT_IOFENCE_TICK 180 /* Default timer timeout, in seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static int hangcheck_tick = DEFAULT_IOFENCE_TICK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int hangcheck_margin = DEFAULT_IOFENCE_MARGIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int hangcheck_reboot; /* Defaults to not reboot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int hangcheck_dump_tasks; /* Defaults to not dumping SysRQ T */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* options - modular */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) module_param(hangcheck_tick, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) MODULE_PARM_DESC(hangcheck_tick, "Timer delay.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) module_param(hangcheck_margin, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) MODULE_PARM_DESC(hangcheck_margin, "If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) module_param(hangcheck_reboot, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) MODULE_PARM_DESC(hangcheck_reboot, "If nonzero, the machine will reboot when the timer margin is exceeded.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) module_param(hangcheck_dump_tasks, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) MODULE_PARM_DESC(hangcheck_dump_tasks, "If nonzero, the machine will dump the system task state when the timer margin is exceeded.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) MODULE_AUTHOR("Oracle");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) MODULE_DESCRIPTION("Hangcheck-timer detects when the system has gone out to lunch past a certain margin.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) MODULE_VERSION(VERSION_STR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* options - nonmodular */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #ifndef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int __init hangcheck_parse_tick(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int par;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (get_option(&str,&par))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) hangcheck_tick = par;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static int __init hangcheck_parse_margin(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int par;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (get_option(&str,&par))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) hangcheck_margin = par;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int __init hangcheck_parse_reboot(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int par;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (get_option(&str,&par))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) hangcheck_reboot = par;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static int __init hangcheck_parse_dump_tasks(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int par;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (get_option(&str,&par))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) hangcheck_dump_tasks = par;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) __setup("hcheck_tick", hangcheck_parse_tick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) __setup("hcheck_margin", hangcheck_parse_margin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) __setup("hcheck_reboot", hangcheck_parse_reboot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) __setup("hcheck_dump_tasks", hangcheck_parse_dump_tasks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #endif /* not MODULE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define TIMER_FREQ 1000000000ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Last time scheduled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static unsigned long long hangcheck_tsc, hangcheck_tsc_margin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void hangcheck_fire(struct timer_list *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static void hangcheck_fire(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned long long cur_tsc, tsc_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) cur_tsc = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (cur_tsc > hangcheck_tsc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) tsc_diff = cur_tsc - hangcheck_tsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (tsc_diff > hangcheck_tsc_margin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (hangcheck_dump_tasks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) printk(KERN_CRIT "Hangcheck: Task state:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #ifdef CONFIG_MAGIC_SYSRQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) handle_sysrq('t');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #endif /* CONFIG_MAGIC_SYSRQ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (hangcheck_reboot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) emergency_restart();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * Enable to investigate delays in detail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) printk("Hangcheck: called %Ld ns since last time (%Ld ns overshoot)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) tsc_diff, tsc_diff - hangcheck_tick*TIMER_FREQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) hangcheck_tsc = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int __init hangcheck_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) VERSION_STR, hangcheck_tick, hangcheck_margin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) hangcheck_tsc_margin =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) (unsigned long long)hangcheck_margin + hangcheck_tick;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) hangcheck_tsc_margin *= TIMER_FREQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) hangcheck_tsc = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void __exit hangcheck_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) del_timer_sync(&hangcheck_ticktock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) printk("Hangcheck: Stopped hangcheck timer.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) module_init(hangcheck_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) module_exit(hangcheck_exit);