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