softdog.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/reboot.h>
  24. #include <linux/types.h>
  25. #include <linux/watchdog.h>
  26. #define TIMER_MARGIN 60 /* Default is 60 seconds */
  27. static unsigned int soft_margin = TIMER_MARGIN; /* in seconds */
  28. module_param(soft_margin, uint, 0);
  29. MODULE_PARM_DESC(soft_margin,
  30. "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
  31. __MODULE_STRING(TIMER_MARGIN) ")");
  32. static bool nowayout = WATCHDOG_NOWAYOUT;
  33. module_param(nowayout, bool, 0);
  34. MODULE_PARM_DESC(nowayout,
  35. "Watchdog cannot be stopped once started (default="
  36. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  37. static int soft_noboot;
  38. module_param(soft_noboot, int, 0);
  39. MODULE_PARM_DESC(soft_noboot,
  40. "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
  41. static int soft_panic;
  42. module_param(soft_panic, int, 0);
  43. MODULE_PARM_DESC(soft_panic,
  44. "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
  45. static struct hrtimer softdog_ticktock;
  46. static struct hrtimer softdog_preticktock;
  47. static enum hrtimer_restart softdog_fire(struct hrtimer *timer)
  48. {
  49. module_put(THIS_MODULE);
  50. if (soft_noboot) {
  51. pr_crit("Triggered - Reboot ignored\n");
  52. } else if (soft_panic) {
  53. pr_crit("Initiating panic\n");
  54. panic("Software Watchdog Timer expired");
  55. } else {
  56. pr_crit("Initiating system reboot\n");
  57. emergency_restart();
  58. pr_crit("Reboot didn't ?????\n");
  59. }
  60. return HRTIMER_NORESTART;
  61. }
  62. static struct watchdog_device softdog_dev;
  63. static enum hrtimer_restart softdog_pretimeout(struct hrtimer *timer)
  64. {
  65. watchdog_notify_pretimeout(&softdog_dev);
  66. return HRTIMER_NORESTART;
  67. }
  68. static int softdog_ping(struct watchdog_device *w)
  69. {
  70. if (!hrtimer_active(&softdog_ticktock))
  71. __module_get(THIS_MODULE);
  72. hrtimer_start(&softdog_ticktock, ktime_set(w->timeout, 0),
  73. HRTIMER_MODE_REL);
  74. if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
  75. if (w->pretimeout)
  76. hrtimer_start(&softdog_preticktock,
  77. ktime_set(w->timeout - w->pretimeout, 0),
  78. HRTIMER_MODE_REL);
  79. else
  80. hrtimer_cancel(&softdog_preticktock);
  81. }
  82. return 0;
  83. }
  84. static int softdog_stop(struct watchdog_device *w)
  85. {
  86. if (hrtimer_cancel(&softdog_ticktock))
  87. module_put(THIS_MODULE);
  88. if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT))
  89. hrtimer_cancel(&softdog_preticktock);
  90. return 0;
  91. }
  92. static struct watchdog_info softdog_info = {
  93. .identity = "Software Watchdog",
  94. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  95. };
  96. static const struct watchdog_ops softdog_ops = {
  97. .owner = THIS_MODULE,
  98. .start = softdog_ping,
  99. .stop = softdog_stop,
  100. };
  101. static struct watchdog_device softdog_dev = {
  102. .info = &softdog_info,
  103. .ops = &softdog_ops,
  104. .min_timeout = 1,
  105. .max_timeout = 65535,
  106. .timeout = TIMER_MARGIN,
  107. };
  108. static int __init softdog_init(void)
  109. {
  110. int ret;
  111. watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
  112. watchdog_set_nowayout(&softdog_dev, nowayout);
  113. watchdog_stop_on_reboot(&softdog_dev);
  114. hrtimer_init(&softdog_ticktock, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  115. softdog_ticktock.function = softdog_fire;
  116. if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
  117. softdog_info.options |= WDIOF_PRETIMEOUT;
  118. hrtimer_init(&softdog_preticktock, CLOCK_MONOTONIC,
  119. HRTIMER_MODE_REL);
  120. softdog_preticktock.function = softdog_pretimeout;
  121. }
  122. ret = watchdog_register_device(&softdog_dev);
  123. if (ret)
  124. return ret;
  125. pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
  126. soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
  127. return 0;
  128. }
  129. module_init(softdog_init);
  130. static void __exit softdog_exit(void)
  131. {
  132. watchdog_unregister_device(&softdog_dev);
  133. }
  134. module_exit(softdog_exit);
  135. MODULE_AUTHOR("Alan Cox");
  136. MODULE_DESCRIPTION("Software Watchdog Device Driver");
  137. MODULE_LICENSE("GPL");