watchdog_core.c 12 KB

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