softdog.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * SoftDog: A Software Watchdog Device
  3. *
  4. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  5. * All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  13. * warranty for any of this software. This material is provided
  14. * "AS-IS" and at no charge.
  15. *
  16. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  17. *
  18. * Software only watchdog driver. Unlike its big brother the WDT501P
  19. * driver this won't always recover a failed machine.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/hrtimer.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/reboot.h>
  28. #include <linux/types.h>
  29. #include <linux/watchdog.h>
  30. #define TIMER_MARGIN 60 /* Default is 60 seconds */
  31. static unsigned int soft_margin = TIMER_MARGIN; /* in seconds */
  32. module_param(soft_margin, uint, 0);
  33. MODULE_PARM_DESC(soft_margin,
  34. "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
  35. __MODULE_STRING(TIMER_MARGIN) ")");
  36. static bool nowayout = WATCHDOG_NOWAYOUT;
  37. module_param(nowayout, bool, 0);
  38. MODULE_PARM_DESC(nowayout,
  39. "Watchdog cannot be stopped once started (default="
  40. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  41. static int soft_noboot;
  42. module_param(soft_noboot, int, 0);
  43. MODULE_PARM_DESC(soft_noboot,
  44. "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
  45. static int soft_panic;
  46. module_param(soft_panic, int, 0);
  47. MODULE_PARM_DESC(soft_panic,
  48. "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
  49. static struct hrtimer softdog_ticktock;
  50. static struct hrtimer softdog_preticktock;
  51. static enum hrtimer_restart softdog_fire(struct hrtimer *timer)
  52. {
  53. module_put(THIS_MODULE);
  54. if (soft_noboot) {
  55. pr_crit("Triggered - Reboot ignored\n");
  56. } else if (soft_panic) {
  57. pr_crit("Initiating panic\n");
  58. panic("Software Watchdog Timer expired");
  59. } else {
  60. pr_crit("Initiating system reboot\n");
  61. emergency_restart();
  62. pr_crit("Reboot didn't ?????\n");
  63. }
  64. return HRTIMER_NORESTART;
  65. }
  66. static struct watchdog_device softdog_dev;
  67. static enum hrtimer_restart softdog_pretimeout(struct hrtimer *timer)
  68. {
  69. watchdog_notify_pretimeout(&softdog_dev);
  70. return HRTIMER_NORESTART;
  71. }
  72. static int softdog_ping(struct watchdog_device *w)
  73. {
  74. if (!hrtimer_active(&softdog_ticktock))
  75. __module_get(THIS_MODULE);
  76. hrtimer_start(&softdog_ticktock, ktime_set(w->timeout, 0),
  77. HRTIMER_MODE_REL);
  78. if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
  79. if (w->pretimeout)
  80. hrtimer_start(&softdog_preticktock,
  81. ktime_set(w->timeout - w->pretimeout, 0),
  82. HRTIMER_MODE_REL);
  83. else
  84. hrtimer_cancel(&softdog_preticktock);
  85. }
  86. return 0;
  87. }
  88. static int softdog_stop(struct watchdog_device *w)
  89. {
  90. if (hrtimer_cancel(&softdog_ticktock))
  91. module_put(THIS_MODULE);
  92. if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT))
  93. hrtimer_cancel(&softdog_preticktock);
  94. return 0;
  95. }
  96. static struct watchdog_info softdog_info = {
  97. .identity = "Software Watchdog",
  98. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  99. };
  100. static const struct watchdog_ops softdog_ops = {
  101. .owner = THIS_MODULE,
  102. .start = softdog_ping,
  103. .stop = softdog_stop,
  104. };
  105. static struct watchdog_device softdog_dev = {
  106. .info = &softdog_info,
  107. .ops = &softdog_ops,
  108. .min_timeout = 1,
  109. .max_timeout = 65535,
  110. .timeout = TIMER_MARGIN,
  111. };
  112. static int __init softdog_init(void)
  113. {
  114. int ret;
  115. watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
  116. watchdog_set_nowayout(&softdog_dev, nowayout);
  117. watchdog_stop_on_reboot(&softdog_dev);
  118. hrtimer_init(&softdog_ticktock, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  119. softdog_ticktock.function = softdog_fire;
  120. if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
  121. softdog_info.options |= WDIOF_PRETIMEOUT;
  122. hrtimer_init(&softdog_preticktock, CLOCK_MONOTONIC,
  123. HRTIMER_MODE_REL);
  124. softdog_preticktock.function = softdog_pretimeout;
  125. }
  126. ret = watchdog_register_device(&softdog_dev);
  127. if (ret)
  128. return ret;
  129. pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
  130. soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
  131. return 0;
  132. }
  133. module_init(softdog_init);
  134. static void __exit softdog_exit(void)
  135. {
  136. watchdog_unregister_device(&softdog_dev);
  137. }
  138. module_exit(softdog_exit);
  139. MODULE_AUTHOR("Alan Cox");
  140. MODULE_DESCRIPTION("Software Watchdog Device Driver");
  141. MODULE_LICENSE("GPL");