watchdog_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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_alias_get_id */
  35. #include <linux/property.h> /* For device_property_read_u32 */
  36. #include <linux/suspend.h>
  37. #include "watchdog_core.h" /* For watchdog_dev_register/... */
  38. #define CREATE_TRACE_POINTS
  39. #include <trace/events/watchdog.h>
  40. static DEFINE_IDA(watchdog_ida);
  41. static int stop_on_reboot = -1;
  42. module_param(stop_on_reboot, int, 0444);
  43. MODULE_PARM_DESC(stop_on_reboot, "Stop watchdogs on reboot (0=keep watching, 1=stop)");
  44. /*
  45. * Deferred Registration infrastructure.
  46. *
  47. * Sometimes watchdog drivers needs to be loaded as soon as possible,
  48. * for example when it's impossible to disable it. To do so,
  49. * raising the initcall level of the watchdog driver is a solution.
  50. * But in such case, the miscdev is maybe not ready (subsys_initcall), and
  51. * watchdog_core need miscdev to register the watchdog as a char device.
  52. *
  53. * The deferred registration infrastructure offer a way for the watchdog
  54. * subsystem to register a watchdog properly, even before miscdev is ready.
  55. */
  56. static DEFINE_MUTEX(wtd_deferred_reg_mutex);
  57. static LIST_HEAD(wtd_deferred_reg_list);
  58. static bool wtd_deferred_reg_done;
  59. static void watchdog_deferred_registration_add(struct watchdog_device *wdd)
  60. {
  61. list_add_tail(&wdd->deferred,
  62. &wtd_deferred_reg_list);
  63. }
  64. static void watchdog_deferred_registration_del(struct watchdog_device *wdd)
  65. {
  66. struct list_head *p, *n;
  67. struct watchdog_device *wdd_tmp;
  68. list_for_each_safe(p, n, &wtd_deferred_reg_list) {
  69. wdd_tmp = list_entry(p, struct watchdog_device,
  70. deferred);
  71. if (wdd_tmp == wdd) {
  72. list_del(&wdd_tmp->deferred);
  73. break;
  74. }
  75. }
  76. }
  77. static void watchdog_check_min_max_timeout(struct watchdog_device *wdd)
  78. {
  79. /*
  80. * Check that we have valid min and max timeout values, if
  81. * not reset them both to 0 (=not used or unknown)
  82. */
  83. if (!wdd->max_hw_heartbeat_ms && wdd->min_timeout > wdd->max_timeout) {
  84. pr_info("Invalid min and max timeout values, resetting to 0!\n");
  85. wdd->min_timeout = 0;
  86. wdd->max_timeout = 0;
  87. }
  88. }
  89. /**
  90. * watchdog_init_timeout() - initialize the timeout field
  91. * @wdd: watchdog device
  92. * @timeout_parm: timeout module parameter
  93. * @dev: Device that stores the timeout-sec property
  94. *
  95. * Initialize the timeout field of the watchdog_device struct with either the
  96. * timeout module parameter (if it is valid value) or the timeout-sec property
  97. * (only if it is a valid value and the timeout_parm is out of bounds).
  98. * If none of them are valid then we keep the old value (which should normally
  99. * be the default timeout value). Note that for the module parameter, '0' means
  100. * 'use default' while it is an invalid value for the timeout-sec property.
  101. * It should simply be dropped if you want to use the default value then.
  102. *
  103. * A zero is returned on success or -EINVAL if all provided values are out of
  104. * bounds.
  105. */
  106. int watchdog_init_timeout(struct watchdog_device *wdd,
  107. unsigned int timeout_parm,
  108. const struct device *dev)
  109. {
  110. const char *dev_str = wdd->parent ? dev_name(wdd->parent) :
  111. (const char *)wdd->info->identity;
  112. unsigned int t = 0;
  113. int ret = 0;
  114. watchdog_check_min_max_timeout(wdd);
  115. /* check the driver supplied value (likely a module parameter) first */
  116. if (timeout_parm) {
  117. if (!watchdog_timeout_invalid(wdd, timeout_parm)) {
  118. wdd->timeout = timeout_parm;
  119. return 0;
  120. }
  121. pr_err("%s: driver supplied timeout (%u) out of range\n",
  122. dev_str, timeout_parm);
  123. ret = -EINVAL;
  124. }
  125. /* try to get the timeout_sec property */
  126. if (dev && device_property_read_u32(dev, "timeout-sec", &t) == 0) {
  127. if (t && !watchdog_timeout_invalid(wdd, t)) {
  128. wdd->timeout = t;
  129. return 0;
  130. }
  131. pr_err("%s: DT supplied timeout (%u) out of range\n", dev_str, t);
  132. ret = -EINVAL;
  133. }
  134. if (ret < 0 && wdd->timeout)
  135. pr_warn("%s: falling back to default timeout (%u)\n", dev_str,
  136. wdd->timeout);
  137. return ret;
  138. }
  139. EXPORT_SYMBOL_GPL(watchdog_init_timeout);
  140. static int watchdog_reboot_notifier(struct notifier_block *nb,
  141. unsigned long code, void *data)
  142. {
  143. struct watchdog_device *wdd;
  144. wdd = container_of(nb, struct watchdog_device, reboot_nb);
  145. if (code == SYS_DOWN || code == SYS_HALT || code == SYS_POWER_OFF) {
  146. if (watchdog_hw_running(wdd)) {
  147. int ret;
  148. ret = wdd->ops->stop(wdd);
  149. trace_watchdog_stop(wdd, ret);
  150. if (ret)
  151. return NOTIFY_BAD;
  152. }
  153. }
  154. return NOTIFY_DONE;
  155. }
  156. static int watchdog_restart_notifier(struct notifier_block *nb,
  157. unsigned long action, void *data)
  158. {
  159. struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
  160. restart_nb);
  161. int ret;
  162. ret = wdd->ops->restart(wdd, action, data);
  163. if (ret)
  164. return NOTIFY_BAD;
  165. return NOTIFY_DONE;
  166. }
  167. static int watchdog_pm_notifier(struct notifier_block *nb, unsigned long mode,
  168. void *data)
  169. {
  170. struct watchdog_device *wdd;
  171. int ret = 0;
  172. wdd = container_of(nb, struct watchdog_device, pm_nb);
  173. switch (mode) {
  174. case PM_HIBERNATION_PREPARE:
  175. case PM_RESTORE_PREPARE:
  176. case PM_SUSPEND_PREPARE:
  177. ret = watchdog_dev_suspend(wdd);
  178. break;
  179. case PM_POST_HIBERNATION:
  180. case PM_POST_RESTORE:
  181. case PM_POST_SUSPEND:
  182. ret = watchdog_dev_resume(wdd);
  183. break;
  184. }
  185. if (ret)
  186. return NOTIFY_BAD;
  187. return NOTIFY_DONE;
  188. }
  189. /**
  190. * watchdog_set_restart_priority - Change priority of restart handler
  191. * @wdd: watchdog device
  192. * @priority: priority of the restart handler, should follow these guidelines:
  193. * 0: use watchdog's restart function as last resort, has limited restart
  194. * capabilies
  195. * 128: default restart handler, use if no other handler is expected to be
  196. * available and/or if restart is sufficient to restart the entire system
  197. * 255: preempt all other handlers
  198. *
  199. * If a wdd->ops->restart function is provided when watchdog_register_device is
  200. * called, it will be registered as a restart handler with the priority given
  201. * here.
  202. */
  203. void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
  204. {
  205. wdd->restart_nb.priority = priority;
  206. }
  207. EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
  208. static int ___watchdog_register_device(struct watchdog_device *wdd)
  209. {
  210. int ret, id = -1;
  211. if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
  212. return -EINVAL;
  213. /* Mandatory operations need to be supported */
  214. if (!wdd->ops->start || (!wdd->ops->stop && !wdd->max_hw_heartbeat_ms))
  215. return -EINVAL;
  216. watchdog_check_min_max_timeout(wdd);
  217. /*
  218. * Note: now that all watchdog_device data has been verified, we
  219. * will not check this anymore in other functions. If data gets
  220. * corrupted in a later stage then we expect a kernel panic!
  221. */
  222. /* Use alias for watchdog id if possible */
  223. if (wdd->parent) {
  224. ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
  225. if (ret >= 0)
  226. id = ida_alloc_range(&watchdog_ida, ret, ret,
  227. GFP_KERNEL);
  228. }
  229. if (id < 0)
  230. id = ida_alloc_max(&watchdog_ida, MAX_DOGS - 1, GFP_KERNEL);
  231. if (id < 0)
  232. return id;
  233. wdd->id = id;
  234. ret = watchdog_dev_register(wdd);
  235. if (ret) {
  236. ida_free(&watchdog_ida, id);
  237. if (!(id == 0 && ret == -EBUSY))
  238. return ret;
  239. /* Retry in case a legacy watchdog module exists */
  240. id = ida_alloc_range(&watchdog_ida, 1, MAX_DOGS - 1,
  241. GFP_KERNEL);
  242. if (id < 0)
  243. return id;
  244. wdd->id = id;
  245. ret = watchdog_dev_register(wdd);
  246. if (ret) {
  247. ida_free(&watchdog_ida, id);
  248. return ret;
  249. }
  250. }
  251. /* Module parameter to force watchdog policy on reboot. */
  252. if (stop_on_reboot != -1) {
  253. if (stop_on_reboot)
  254. set_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
  255. else
  256. clear_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
  257. }
  258. if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
  259. if (!wdd->ops->stop)
  260. pr_warn("watchdog%d: stop_on_reboot not supported\n", wdd->id);
  261. else {
  262. wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
  263. ret = register_reboot_notifier(&wdd->reboot_nb);
  264. if (ret) {
  265. pr_err("watchdog%d: Cannot register reboot notifier (%d)\n",
  266. wdd->id, ret);
  267. watchdog_dev_unregister(wdd);
  268. ida_free(&watchdog_ida, id);
  269. return ret;
  270. }
  271. }
  272. }
  273. if (wdd->ops->restart) {
  274. wdd->restart_nb.notifier_call = watchdog_restart_notifier;
  275. ret = register_restart_handler(&wdd->restart_nb);
  276. if (ret)
  277. pr_warn("watchdog%d: Cannot register restart handler (%d)\n",
  278. wdd->id, ret);
  279. }
  280. if (test_bit(WDOG_NO_PING_ON_SUSPEND, &wdd->status)) {
  281. wdd->pm_nb.notifier_call = watchdog_pm_notifier;
  282. ret = register_pm_notifier(&wdd->pm_nb);
  283. if (ret)
  284. pr_warn("watchdog%d: Cannot register pm handler (%d)\n",
  285. wdd->id, ret);
  286. }
  287. return 0;
  288. }
  289. static int __watchdog_register_device(struct watchdog_device *wdd)
  290. {
  291. const char *dev_str;
  292. int ret;
  293. ret = ___watchdog_register_device(wdd);
  294. if (ret) {
  295. dev_str = wdd->parent ? dev_name(wdd->parent) :
  296. (const char *)wdd->info->identity;
  297. pr_err("%s: failed to register watchdog device (err = %d)\n",
  298. dev_str, ret);
  299. }
  300. return ret;
  301. }
  302. /**
  303. * watchdog_register_device() - register a watchdog device
  304. * @wdd: watchdog device
  305. *
  306. * Register a watchdog device with the kernel so that the
  307. * watchdog timer can be accessed from userspace.
  308. *
  309. * A zero is returned on success and a negative errno code for
  310. * failure.
  311. */
  312. int watchdog_register_device(struct watchdog_device *wdd)
  313. {
  314. int ret = 0;
  315. mutex_lock(&wtd_deferred_reg_mutex);
  316. if (wtd_deferred_reg_done)
  317. ret = __watchdog_register_device(wdd);
  318. else
  319. watchdog_deferred_registration_add(wdd);
  320. mutex_unlock(&wtd_deferred_reg_mutex);
  321. return ret;
  322. }
  323. EXPORT_SYMBOL_GPL(watchdog_register_device);
  324. static void __watchdog_unregister_device(struct watchdog_device *wdd)
  325. {
  326. if (wdd == NULL)
  327. return;
  328. if (wdd->ops->restart)
  329. unregister_restart_handler(&wdd->restart_nb);
  330. if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status))
  331. unregister_reboot_notifier(&wdd->reboot_nb);
  332. watchdog_dev_unregister(wdd);
  333. ida_free(&watchdog_ida, wdd->id);
  334. }
  335. /**
  336. * watchdog_unregister_device() - unregister a watchdog device
  337. * @wdd: watchdog device to unregister
  338. *
  339. * Unregister a watchdog device that was previously successfully
  340. * registered with watchdog_register_device().
  341. */
  342. void watchdog_unregister_device(struct watchdog_device *wdd)
  343. {
  344. mutex_lock(&wtd_deferred_reg_mutex);
  345. if (wtd_deferred_reg_done)
  346. __watchdog_unregister_device(wdd);
  347. else
  348. watchdog_deferred_registration_del(wdd);
  349. mutex_unlock(&wtd_deferred_reg_mutex);
  350. }
  351. EXPORT_SYMBOL_GPL(watchdog_unregister_device);
  352. static void devm_watchdog_unregister_device(struct device *dev, void *res)
  353. {
  354. watchdog_unregister_device(*(struct watchdog_device **)res);
  355. }
  356. /**
  357. * devm_watchdog_register_device() - resource managed watchdog_register_device()
  358. * @dev: device that is registering this watchdog device
  359. * @wdd: watchdog device
  360. *
  361. * Managed watchdog_register_device(). For watchdog device registered by this
  362. * function, watchdog_unregister_device() is automatically called on driver
  363. * detach. See watchdog_register_device() for more information.
  364. */
  365. int devm_watchdog_register_device(struct device *dev,
  366. struct watchdog_device *wdd)
  367. {
  368. struct watchdog_device **rcwdd;
  369. int ret;
  370. rcwdd = devres_alloc(devm_watchdog_unregister_device, sizeof(*rcwdd),
  371. GFP_KERNEL);
  372. if (!rcwdd)
  373. return -ENOMEM;
  374. ret = watchdog_register_device(wdd);
  375. if (!ret) {
  376. *rcwdd = wdd;
  377. devres_add(dev, rcwdd);
  378. } else {
  379. devres_free(rcwdd);
  380. }
  381. return ret;
  382. }
  383. EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
  384. static int __init watchdog_deferred_registration(void)
  385. {
  386. mutex_lock(&wtd_deferred_reg_mutex);
  387. wtd_deferred_reg_done = true;
  388. while (!list_empty(&wtd_deferred_reg_list)) {
  389. struct watchdog_device *wdd;
  390. wdd = list_first_entry(&wtd_deferred_reg_list,
  391. struct watchdog_device, deferred);
  392. list_del(&wdd->deferred);
  393. __watchdog_register_device(wdd);
  394. }
  395. mutex_unlock(&wtd_deferred_reg_mutex);
  396. return 0;
  397. }
  398. static int __init watchdog_init(void)
  399. {
  400. int err;
  401. err = watchdog_dev_init();
  402. if (err < 0)
  403. return err;
  404. watchdog_deferred_registration();
  405. return 0;
  406. }
  407. static void __exit watchdog_exit(void)
  408. {
  409. watchdog_dev_exit();
  410. ida_destroy(&watchdog_ida);
  411. }
  412. subsys_initcall_sync(watchdog_init);
  413. module_exit(watchdog_exit);
  414. MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
  415. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  416. MODULE_DESCRIPTION("WatchDog Timer Driver Core");
  417. MODULE_LICENSE("GPL");