softdog.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * SoftDog: A Software Watchdog Device
  4. *
  5. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  6. * All Rights Reserved.
  7. *
  8. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  9. * warranty for any of this software. This material is provided
  10. * "AS-IS" and at no charge.
  11. *
  12. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  13. *
  14. * Software only watchdog driver. Unlike its big brother the WDT501P
  15. * driver this won't always recover a failed machine.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/hrtimer.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/kthread.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/reboot.h>
  25. #include <linux/types.h>
  26. #include <linux/watchdog.h>
  27. #include <linux/workqueue.h>
  28. #define TIMER_MARGIN 60 /* Default is 60 seconds */
  29. static unsigned int soft_margin = TIMER_MARGIN; /* in seconds */
  30. module_param(soft_margin, uint, 0);
  31. MODULE_PARM_DESC(soft_margin,
  32. "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
  33. __MODULE_STRING(TIMER_MARGIN) ")");
  34. static bool nowayout = WATCHDOG_NOWAYOUT;
  35. module_param(nowayout, bool, 0);
  36. MODULE_PARM_DESC(nowayout,
  37. "Watchdog cannot be stopped once started (default="
  38. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  39. static int soft_noboot;
  40. module_param(soft_noboot, int, 0);
  41. MODULE_PARM_DESC(soft_noboot,
  42. "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
  43. static int soft_panic;
  44. module_param(soft_panic, int, 0);
  45. MODULE_PARM_DESC(soft_panic,
  46. "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
  47. static char *soft_reboot_cmd;
  48. module_param(soft_reboot_cmd, charp, 0000);
  49. MODULE_PARM_DESC(soft_reboot_cmd,
  50. "Set reboot command. Emergency reboot takes place if unset");
  51. static bool soft_active_on_boot;
  52. module_param(soft_active_on_boot, bool, 0000);
  53. MODULE_PARM_DESC(soft_active_on_boot,
  54. "Set to true to active Softdog on boot (default=false)");
  55. static struct hrtimer softdog_ticktock;
  56. static struct hrtimer softdog_preticktock;
  57. static int reboot_kthread_fn(void *data)
  58. {
  59. kernel_restart(soft_reboot_cmd);
  60. return -EPERM; /* Should not reach here */
  61. }
  62. static void reboot_work_fn(struct work_struct *unused)
  63. {
  64. kthread_run(reboot_kthread_fn, NULL, "softdog_reboot");
  65. }
  66. static enum hrtimer_restart softdog_fire(struct hrtimer *timer)
  67. {
  68. static bool soft_reboot_fired;
  69. module_put(THIS_MODULE);
  70. if (soft_noboot) {
  71. pr_crit("Triggered - Reboot ignored\n");
  72. } else if (soft_panic) {
  73. pr_crit("Initiating panic\n");
  74. panic("Software Watchdog Timer expired");
  75. } else {
  76. pr_crit("Initiating system reboot\n");
  77. if (!soft_reboot_fired && soft_reboot_cmd != NULL) {
  78. static DECLARE_WORK(reboot_work, reboot_work_fn);
  79. /*
  80. * The 'kernel_restart' is a 'might-sleep' operation.
  81. * Also, executing it in system-wide workqueues blocks
  82. * any driver from using the same workqueue in its
  83. * shutdown callback function. Thus, we should execute
  84. * the 'kernel_restart' in a standalone kernel thread.
  85. * But since starting a kernel thread is also a
  86. * 'might-sleep' operation, so the 'reboot_work' is
  87. * required as a launcher of the kernel thread.
  88. *
  89. * After request the reboot, restart the timer to
  90. * schedule an 'emergency_restart' reboot after
  91. * 'TIMER_MARGIN' seconds. It's because if the softdog
  92. * hangs, it might be because of scheduling issues. And
  93. * if that is the case, both 'schedule_work' and
  94. * 'kernel_restart' may possibly be malfunctional at the
  95. * same time.
  96. */
  97. soft_reboot_fired = true;
  98. schedule_work(&reboot_work);
  99. hrtimer_add_expires_ns(timer,
  100. (u64)TIMER_MARGIN * NSEC_PER_SEC);
  101. return HRTIMER_RESTART;
  102. }
  103. emergency_restart();
  104. pr_crit("Reboot didn't ?????\n");
  105. }
  106. return HRTIMER_NORESTART;
  107. }
  108. static struct watchdog_device softdog_dev;
  109. static enum hrtimer_restart softdog_pretimeout(struct hrtimer *timer)
  110. {
  111. watchdog_notify_pretimeout(&softdog_dev);
  112. return HRTIMER_NORESTART;
  113. }
  114. static int softdog_ping(struct watchdog_device *w)
  115. {
  116. if (!hrtimer_active(&softdog_ticktock))
  117. __module_get(THIS_MODULE);
  118. hrtimer_start(&softdog_ticktock, ktime_set(w->timeout, 0),
  119. HRTIMER_MODE_REL);
  120. if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
  121. if (w->pretimeout)
  122. hrtimer_start(&softdog_preticktock,
  123. ktime_set(w->timeout - w->pretimeout, 0),
  124. HRTIMER_MODE_REL);
  125. else
  126. hrtimer_cancel(&softdog_preticktock);
  127. }
  128. return 0;
  129. }
  130. static int softdog_stop(struct watchdog_device *w)
  131. {
  132. if (hrtimer_cancel(&softdog_ticktock))
  133. module_put(THIS_MODULE);
  134. if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT))
  135. hrtimer_cancel(&softdog_preticktock);
  136. return 0;
  137. }
  138. static struct watchdog_info softdog_info = {
  139. .identity = "Software Watchdog",
  140. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  141. };
  142. static const struct watchdog_ops softdog_ops = {
  143. .owner = THIS_MODULE,
  144. .start = softdog_ping,
  145. .stop = softdog_stop,
  146. };
  147. static struct watchdog_device softdog_dev = {
  148. .info = &softdog_info,
  149. .ops = &softdog_ops,
  150. .min_timeout = 1,
  151. .max_timeout = 65535,
  152. .timeout = TIMER_MARGIN,
  153. };
  154. static int __init softdog_init(void)
  155. {
  156. int ret;
  157. watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
  158. watchdog_set_nowayout(&softdog_dev, nowayout);
  159. watchdog_stop_on_reboot(&softdog_dev);
  160. hrtimer_init(&softdog_ticktock, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  161. softdog_ticktock.function = softdog_fire;
  162. if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
  163. softdog_info.options |= WDIOF_PRETIMEOUT;
  164. hrtimer_init(&softdog_preticktock, CLOCK_MONOTONIC,
  165. HRTIMER_MODE_REL);
  166. softdog_preticktock.function = softdog_pretimeout;
  167. }
  168. if (soft_active_on_boot)
  169. softdog_ping(&softdog_dev);
  170. ret = watchdog_register_device(&softdog_dev);
  171. if (ret)
  172. return ret;
  173. pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
  174. soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
  175. pr_info(" soft_reboot_cmd=%s soft_active_on_boot=%d\n",
  176. soft_reboot_cmd ?: "<not set>", soft_active_on_boot);
  177. return 0;
  178. }
  179. module_init(softdog_init);
  180. static void __exit softdog_exit(void)
  181. {
  182. watchdog_unregister_device(&softdog_dev);
  183. }
  184. module_exit(softdog_exit);
  185. MODULE_AUTHOR("Alan Cox");
  186. MODULE_DESCRIPTION("Software Watchdog Device Driver");
  187. MODULE_LICENSE("GPL");