hangcheck-timer.c 6.2 KB

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