watchdog_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * watchdog_core.c
  4. *
  5. * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  6. * All Rights Reserved.
  7. *
  8. * (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
  9. *
  10. * This source code is part of the generic code that can be used
  11. * by all the watchdog timer drivers.
  12. *
  13. * Based on source code of the following authors:
  14. * Matt Domsch <Matt_Domsch@dell.com>,
  15. * Rob Radez <rob@osinvestor.com>,
  16. * Rusty Lynch <rusty@linux.co.intel.com>
  17. * Satyam Sharma <satyam@infradead.org>
  18. * Randy Dunlap <randy.dunlap@oracle.com>
  19. *
  20. * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
  21. * admit liability nor provide warranty for any of this software.
  22. * This material is provided "AS-IS" and at no charge.
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/module.h> /* For EXPORT_SYMBOL/module stuff/... */
  26. #include <linux/types.h> /* For standard types */
  27. #include <linux/errno.h> /* For the -ENODEV/... values */
  28. #include <linux/kernel.h> /* For printk/panic/... */
  29. #include <linux/reboot.h> /* For restart handler */
  30. #include <linux/watchdog.h> /* For watchdog specific items */
  31. #include <linux/init.h> /* For __init/__exit/... */
  32. #include <linux/idr.h> /* For ida_* macros */
  33. #include <linux/err.h> /* For IS_ERR macros */
  34. #include <linux/of.h> /* For of_get_timeout_sec */
  35. #include "watchdog_core.h" /* For watchdog_dev_register/... */
  36. static DEFINE_IDA(watchdog_ida);
  37. static int stop_on_reboot = -1;
  38. module_param(stop_on_reboot, int, 0444);
  39. MODULE_PARM_DESC(stop_on_reboot, "Stop watchdogs on reboot (0=keep watching, 1=stop)");
  40. /*
  41. * Deferred Registration infrastructure.
  42. *
  43. * Sometimes watchdog drivers needs to be loaded as soon as possible,
  44. * for example when it's impossible to disable it. To do so,
  45. * raising the initcall level of the watchdog driver is a solution.
  46. * But in such case, the miscdev is maybe not ready (subsys_initcall), and
  47. * watchdog_core need miscdev to register the watchdog as a char device.
  48. *
  49. * The deferred registration infrastructure offer a way for the watchdog
  50. * subsystem to register a watchdog properly, even before miscdev is ready.
  51. */
  52. static DEFINE_MUTEX(wtd_deferred_reg_mutex);
  53. static LIST_HEAD(wtd_deferred_reg_list);
  54. static bool wtd_deferred_reg_done;
  55. static void watchdog_deferred_registration_add(struct watchdog_device *wdd)
  56. {
  57. list_add_tail(&wdd->deferred,
  58. &wtd_deferred_reg_list);
  59. }
  60. static void watchdog_deferred_registration_del(struct watchdog_device *wdd)
  61. {
  62. struct list_head *p, *n;
  63. struct watchdog_device *wdd_tmp;
  64. list_for_each_safe(p, n, &wtd_deferred_reg_list) {
  65. wdd_tmp = list_entry(p, struct watchdog_device,
  66. deferred);
  67. if (wdd_tmp == wdd) {
  68. list_del(&wdd_tmp->deferred);
  69. break;
  70. }
  71. }
  72. }
  73. static void watchdog_check_min_max_timeout(struct watchdog_device *wdd)
  74. {
  75. /*
  76. * Check that we have valid min and max timeout values, if
  77. * not reset them both to 0 (=not used or unknown)
  78. */
  79. if (!wdd->max_hw_heartbeat_ms && wdd->min_timeout > wdd->max_timeout) {
  80. pr_info("Invalid min and max timeout values, resetting to 0!\n");
  81. wdd->min_timeout = 0;
  82. wdd->max_timeout = 0;
  83. }
  84. }
  85. /**
  86. * watchdog_init_timeout() - initialize the timeout field
  87. * @wdd: watchdog device
  88. * @timeout_parm: timeout module parameter
  89. * @dev: Device that stores the timeout-sec property
  90. *
  91. * Initialize the timeout field of the watchdog_device struct with either the
  92. * timeout module parameter (if it is valid value) or the timeout-sec property
  93. * (only if it is a valid value and the timeout_parm is out of bounds).
  94. * If none of them are valid then we keep the old value (which should normally
  95. * be the default timeout value). Note that for the module parameter, '0' means
  96. * 'use default' while it is an invalid value for the timeout-sec property.
  97. * It should simply be dropped if you want to use the default value then.
  98. *
  99. * A zero is returned on success or -EINVAL if all provided values are out of
  100. * bounds.
  101. */
  102. int watchdog_init_timeout(struct watchdog_device *wdd,
  103. unsigned int timeout_parm, struct device *dev)
  104. {
  105. const char *dev_str = wdd->parent ? dev_name(wdd->parent) :
  106. (const char *)wdd->info->identity;
  107. unsigned int t = 0;
  108. int ret = 0;
  109. watchdog_check_min_max_timeout(wdd);
  110. /* check the driver supplied value (likely a module parameter) first */
  111. if (timeout_parm) {
  112. if (!watchdog_timeout_invalid(wdd, timeout_parm)) {
  113. wdd->timeout = timeout_parm;
  114. return 0;
  115. }
  116. pr_err("%s: driver supplied timeout (%u) out of range\n",
  117. dev_str, timeout_parm);
  118. ret = -EINVAL;
  119. }
  120. /* try to get the timeout_sec property */
  121. if (dev && dev->of_node &&
  122. of_property_read_u32(dev->of_node, "timeout-sec", &t) == 0) {
  123. if (t && !watchdog_timeout_invalid(wdd, t)) {
  124. wdd->timeout = t;
  125. return 0;
  126. }
  127. pr_err("%s: DT supplied timeout (%u) out of range\n", dev_str, t);
  128. ret = -EINVAL;
  129. }
  130. if (ret < 0 && wdd->timeout)
  131. pr_warn("%s: falling back to default timeout (%u)\n", dev_str,
  132. wdd->timeout);
  133. return ret;
  134. }
  135. EXPORT_SYMBOL_GPL(watchdog_init_timeout);
  136. static int watchdog_reboot_notifier(struct notifier_block *nb,
  137. unsigned long code, void *data)
  138. {
  139. struct watchdog_device *wdd;
  140. wdd = container_of(nb, struct watchdog_device, reboot_nb);
  141. if (code == SYS_DOWN || code == SYS_HALT) {
  142. if (watchdog_active(wdd)) {
  143. int ret;
  144. ret = wdd->ops->stop(wdd);
  145. if (ret)
  146. return NOTIFY_BAD;
  147. }
  148. }
  149. return NOTIFY_DONE;
  150. }
  151. static int watchdog_restart_notifier(struct notifier_block *nb,
  152. unsigned long action, void *data)
  153. {
  154. struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
  155. restart_nb);
  156. int ret;
  157. ret = wdd->ops->restart(wdd, action, data);
  158. if (ret)
  159. return NOTIFY_BAD;
  160. return NOTIFY_DONE;
  161. }
  162. /**
  163. * watchdog_set_restart_priority - Change priority of restart handler
  164. * @wdd: watchdog device
  165. * @priority: priority of the restart handler, should follow these guidelines:
  166. * 0: use watchdog's restart function as last resort, has limited restart
  167. * capabilies
  168. * 128: default restart handler, use if no other handler is expected to be
  169. * available and/or if restart is sufficient to restart the entire system
  170. * 255: preempt all other handlers
  171. *
  172. * If a wdd->ops->restart function is provided when watchdog_register_device is
  173. * called, it will be registered as a restart handler with the priority given
  174. * here.
  175. */
  176. void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
  177. {
  178. wdd->restart_nb.priority = priority;
  179. }
  180. EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
  181. static int __watchdog_register_device(struct watchdog_device *wdd)
  182. {
  183. int ret, id = -1;
  184. if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
  185. return -EINVAL;
  186. /* Mandatory operations need to be supported */
  187. if (!wdd->ops->start || (!wdd->ops->stop && !wdd->max_hw_heartbeat_ms))
  188. return -EINVAL;
  189. watchdog_check_min_max_timeout(wdd);
  190. /*
  191. * Note: now that all watchdog_device data has been verified, we
  192. * will not check this anymore in other functions. If data gets
  193. * corrupted in a later stage then we expect a kernel panic!
  194. */
  195. /* Use alias for watchdog id if possible */
  196. if (wdd->parent) {
  197. ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
  198. if (ret >= 0)
  199. id = ida_simple_get(&watchdog_ida, ret,
  200. ret + 1, GFP_KERNEL);
  201. }
  202. if (id < 0)
  203. id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
  204. if (id < 0)
  205. return id;
  206. wdd->id = id;
  207. ret = watchdog_dev_register(wdd);
  208. if (ret) {
  209. ida_simple_remove(&watchdog_ida, id);
  210. if (!(id == 0 && ret == -EBUSY))
  211. return ret;
  212. /* Retry in case a legacy watchdog module exists */
  213. id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
  214. if (id < 0)
  215. return id;
  216. wdd->id = id;
  217. ret = watchdog_dev_register(wdd);
  218. if (ret) {
  219. ida_simple_remove(&watchdog_ida, id);
  220. return ret;
  221. }
  222. }
  223. /* Module parameter to force watchdog policy on reboot. */
  224. if (stop_on_reboot != -1) {
  225. if (stop_on_reboot)
  226. set_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
  227. else
  228. clear_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
  229. }
  230. if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
  231. if (!wdd->ops->stop)
  232. pr_warn("watchdog%d: stop_on_reboot not supported\n", wdd->id);
  233. else {
  234. wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
  235. ret = register_reboot_notifier(&wdd->reboot_nb);
  236. if (ret) {
  237. pr_err("watchdog%d: Cannot register reboot notifier (%d)\n",
  238. wdd->id, ret);
  239. watchdog_dev_unregister(wdd);
  240. ida_simple_remove(&watchdog_ida, id);
  241. return ret;
  242. }
  243. }
  244. }
  245. if (wdd->ops->restart) {
  246. wdd->restart_nb.notifier_call = watchdog_restart_notifier;
  247. ret = register_restart_handler(&wdd->restart_nb);
  248. if (ret)
  249. pr_warn("watchdog%d: Cannot register restart handler (%d)\n",
  250. wdd->id, ret);
  251. }
  252. return 0;
  253. }
  254. /**
  255. * watchdog_register_device() - register a watchdog device
  256. * @wdd: watchdog device
  257. *
  258. * Register a watchdog device with the kernel so that the
  259. * watchdog timer can be accessed from userspace.
  260. *
  261. * A zero is returned on success and a negative errno code for
  262. * failure.
  263. */
  264. int watchdog_register_device(struct watchdog_device *wdd)
  265. {
  266. const char *dev_str;
  267. int ret = 0;
  268. mutex_lock(&wtd_deferred_reg_mutex);
  269. if (wtd_deferred_reg_done)
  270. ret = __watchdog_register_device(wdd);
  271. else
  272. watchdog_deferred_registration_add(wdd);
  273. mutex_unlock(&wtd_deferred_reg_mutex);
  274. if (ret) {
  275. dev_str = wdd->parent ? dev_name(wdd->parent) :
  276. (const char *)wdd->info->identity;
  277. pr_err("%s: failed to register watchdog device (err = %d)\n",
  278. dev_str, ret);
  279. }
  280. return ret;
  281. }
  282. EXPORT_SYMBOL_GPL(watchdog_register_device);
  283. static void __watchdog_unregister_device(struct watchdog_device *wdd)
  284. {
  285. if (wdd == NULL)
  286. return;
  287. if (wdd->ops->restart)
  288. unregister_restart_handler(&wdd->restart_nb);
  289. if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status))
  290. unregister_reboot_notifier(&wdd->reboot_nb);
  291. watchdog_dev_unregister(wdd);
  292. ida_simple_remove(&watchdog_ida, wdd->id);
  293. }
  294. /**
  295. * watchdog_unregister_device() - unregister a watchdog device
  296. * @wdd: watchdog device to unregister
  297. *
  298. * Unregister a watchdog device that was previously successfully
  299. * registered with watchdog_register_device().
  300. */
  301. void watchdog_unregister_device(struct watchdog_device *wdd)
  302. {
  303. mutex_lock(&wtd_deferred_reg_mutex);
  304. if (wtd_deferred_reg_done)
  305. __watchdog_unregister_device(wdd);
  306. else
  307. watchdog_deferred_registration_del(wdd);
  308. mutex_unlock(&wtd_deferred_reg_mutex);
  309. }
  310. EXPORT_SYMBOL_GPL(watchdog_unregister_device);
  311. static void devm_watchdog_unregister_device(struct device *dev, void *res)
  312. {
  313. watchdog_unregister_device(*(struct watchdog_device **)res);
  314. }
  315. /**
  316. * devm_watchdog_register_device() - resource managed watchdog_register_device()
  317. * @dev: device that is registering this watchdog device
  318. * @wdd: watchdog device
  319. *
  320. * Managed watchdog_register_device(). For watchdog device registered by this
  321. * function, watchdog_unregister_device() is automatically called on driver
  322. * detach. See watchdog_register_device() for more information.
  323. */
  324. int devm_watchdog_register_device(struct device *dev,
  325. struct watchdog_device *wdd)
  326. {
  327. struct watchdog_device **rcwdd;
  328. int ret;
  329. rcwdd = devres_alloc(devm_watchdog_unregister_device, sizeof(*rcwdd),
  330. GFP_KERNEL);
  331. if (!rcwdd)
  332. return -ENOMEM;
  333. ret = watchdog_register_device(wdd);
  334. if (!ret) {
  335. *rcwdd = wdd;
  336. devres_add(dev, rcwdd);
  337. } else {
  338. devres_free(rcwdd);
  339. }
  340. return ret;
  341. }
  342. EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
  343. static int __init watchdog_deferred_registration(void)
  344. {
  345. mutex_lock(&wtd_deferred_reg_mutex);
  346. wtd_deferred_reg_done = true;
  347. while (!list_empty(&wtd_deferred_reg_list)) {
  348. struct watchdog_device *wdd;
  349. wdd = list_first_entry(&wtd_deferred_reg_list,
  350. struct watchdog_device, deferred);
  351. list_del(&wdd->deferred);
  352. __watchdog_register_device(wdd);
  353. }
  354. mutex_unlock(&wtd_deferred_reg_mutex);
  355. return 0;
  356. }
  357. static int __init watchdog_init(void)
  358. {
  359. int err;
  360. err = watchdog_dev_init();
  361. if (err < 0)
  362. return err;
  363. watchdog_deferred_registration();
  364. return 0;
  365. }
  366. static void __exit watchdog_exit(void)
  367. {
  368. watchdog_dev_exit();
  369. ida_destroy(&watchdog_ida);
  370. }
  371. subsys_initcall_sync(watchdog_init);
  372. module_exit(watchdog_exit);
  373. MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
  374. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  375. MODULE_DESCRIPTION("WatchDog Timer Driver Core");
  376. MODULE_LICENSE("GPL");