Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * hangcheck-timer.c
0004  *
0005  * Driver for a little io fencing timer.
0006  *
0007  * Copyright (C) 2002, 2003 Oracle.  All rights reserved.
0008  *
0009  * Author: Joel Becker <joel.becker@oracle.com>
0010  */
0011 
0012 /*
0013  * The hangcheck-timer driver uses the TSC to catch delays that
0014  * jiffies does not notice.  A timer is set.  When the timer fires, it
0015  * checks whether it was delayed and if that delay exceeds a given
0016  * margin of error.  The hangcheck_tick module parameter takes the timer
0017  * duration in seconds.  The hangcheck_margin parameter defines the
0018  * margin of error, in seconds.  The defaults are 60 seconds for the
0019  * timer and 180 seconds for the margin of error.  IOW, a timer is set
0020  * for 60 seconds.  When the timer fires, the callback checks the
0021  * actual duration that the timer waited.  If the duration exceeds the
0022  * allotted time and margin (here 60 + 180, or 240 seconds), the machine
0023  * is restarted.  A healthy machine will have the duration match the
0024  * expected timeout very closely.
0025  */
0026 
0027 #include <linux/module.h>
0028 #include <linux/moduleparam.h>
0029 #include <linux/types.h>
0030 #include <linux/kernel.h>
0031 #include <linux/fs.h>
0032 #include <linux/mm.h>
0033 #include <linux/reboot.h>
0034 #include <linux/init.h>
0035 #include <linux/delay.h>
0036 #include <linux/uaccess.h>
0037 #include <linux/sysrq.h>
0038 #include <linux/timer.h>
0039 #include <linux/hrtimer.h>
0040 
0041 #define VERSION_STR "0.9.1"
0042 
0043 #define DEFAULT_IOFENCE_MARGIN 60   /* Default fudge factor, in seconds */
0044 #define DEFAULT_IOFENCE_TICK 180    /* Default timer timeout, in seconds */
0045 
0046 static int hangcheck_tick = DEFAULT_IOFENCE_TICK;
0047 static int hangcheck_margin = DEFAULT_IOFENCE_MARGIN;
0048 static int hangcheck_reboot;  /* Defaults to not reboot */
0049 static int hangcheck_dump_tasks;  /* Defaults to not dumping SysRQ T */
0050 
0051 /* options - modular */
0052 module_param(hangcheck_tick, int, 0);
0053 MODULE_PARM_DESC(hangcheck_tick, "Timer delay.");
0054 module_param(hangcheck_margin, int, 0);
0055 MODULE_PARM_DESC(hangcheck_margin, "If the hangcheck timer has been delayed more than hangcheck_margin seconds, the driver will fire.");
0056 module_param(hangcheck_reboot, int, 0);
0057 MODULE_PARM_DESC(hangcheck_reboot, "If nonzero, the machine will reboot when the timer margin is exceeded.");
0058 module_param(hangcheck_dump_tasks, int, 0);
0059 MODULE_PARM_DESC(hangcheck_dump_tasks, "If nonzero, the machine will dump the system task state when the timer margin is exceeded.");
0060 
0061 MODULE_AUTHOR("Oracle");
0062 MODULE_DESCRIPTION("Hangcheck-timer detects when the system has gone out to lunch past a certain margin.");
0063 MODULE_LICENSE("GPL");
0064 MODULE_VERSION(VERSION_STR);
0065 
0066 /* options - nonmodular */
0067 #ifndef MODULE
0068 
0069 static int __init hangcheck_parse_tick(char *str)
0070 {
0071     int par;
0072     if (get_option(&str,&par))
0073         hangcheck_tick = par;
0074     return 1;
0075 }
0076 
0077 static int __init hangcheck_parse_margin(char *str)
0078 {
0079     int par;
0080     if (get_option(&str,&par))
0081         hangcheck_margin = par;
0082     return 1;
0083 }
0084 
0085 static int __init hangcheck_parse_reboot(char *str)
0086 {
0087     int par;
0088     if (get_option(&str,&par))
0089         hangcheck_reboot = par;
0090     return 1;
0091 }
0092 
0093 static int __init hangcheck_parse_dump_tasks(char *str)
0094 {
0095     int par;
0096     if (get_option(&str,&par))
0097         hangcheck_dump_tasks = par;
0098     return 1;
0099 }
0100 
0101 __setup("hcheck_tick", hangcheck_parse_tick);
0102 __setup("hcheck_margin", hangcheck_parse_margin);
0103 __setup("hcheck_reboot", hangcheck_parse_reboot);
0104 __setup("hcheck_dump_tasks", hangcheck_parse_dump_tasks);
0105 #endif /* not MODULE */
0106 
0107 #define TIMER_FREQ 1000000000ULL
0108 
0109 /* Last time scheduled */
0110 static unsigned long long hangcheck_tsc, hangcheck_tsc_margin;
0111 
0112 static void hangcheck_fire(struct timer_list *);
0113 
0114 static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire);
0115 
0116 static void hangcheck_fire(struct timer_list *unused)
0117 {
0118     unsigned long long cur_tsc, tsc_diff;
0119 
0120     cur_tsc = ktime_get_ns();
0121 
0122     if (cur_tsc > hangcheck_tsc)
0123         tsc_diff = cur_tsc - hangcheck_tsc;
0124     else
0125         tsc_diff = (cur_tsc + (~0ULL - hangcheck_tsc)); /* or something */
0126 
0127     if (tsc_diff > hangcheck_tsc_margin) {
0128         if (hangcheck_dump_tasks) {
0129             printk(KERN_CRIT "Hangcheck: Task state:\n");
0130 #ifdef CONFIG_MAGIC_SYSRQ
0131             handle_sysrq('t');
0132 #endif  /* CONFIG_MAGIC_SYSRQ */
0133         }
0134         if (hangcheck_reboot) {
0135             printk(KERN_CRIT "Hangcheck: hangcheck is restarting the machine.\n");
0136             emergency_restart();
0137         } else {
0138             printk(KERN_CRIT "Hangcheck: hangcheck value past margin!\n");
0139         }
0140     }
0141 #if 0
0142     /*
0143      * Enable to investigate delays in detail
0144      */
0145     printk("Hangcheck: called %Ld ns since last time (%Ld ns overshoot)\n",
0146             tsc_diff, tsc_diff - hangcheck_tick*TIMER_FREQ);
0147 #endif
0148     mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
0149     hangcheck_tsc = ktime_get_ns();
0150 }
0151 
0152 
0153 static int __init hangcheck_init(void)
0154 {
0155     printk("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n",
0156            VERSION_STR, hangcheck_tick, hangcheck_margin);
0157     hangcheck_tsc_margin =
0158         (unsigned long long)hangcheck_margin + hangcheck_tick;
0159     hangcheck_tsc_margin *= TIMER_FREQ;
0160 
0161     hangcheck_tsc = ktime_get_ns();
0162     mod_timer(&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ));
0163 
0164     return 0;
0165 }
0166 
0167 
0168 static void __exit hangcheck_exit(void)
0169 {
0170     del_timer_sync(&hangcheck_ticktock);
0171         printk("Hangcheck: Stopped hangcheck timer.\n");
0172 }
0173 
0174 module_init(hangcheck_init);
0175 module_exit(hangcheck_exit);