watchdog_dev.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * watchdog_dev.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. * (c) Copyright 2021 Hewlett Packard Enterprise Development LP.
  11. *
  12. * This source code is part of the generic code that can be used
  13. * by all the watchdog timer drivers.
  14. *
  15. * This part of the generic code takes care of the following
  16. * misc device: /dev/watchdog.
  17. *
  18. * Based on source code of the following authors:
  19. * Matt Domsch <Matt_Domsch@dell.com>,
  20. * Rob Radez <rob@osinvestor.com>,
  21. * Rusty Lynch <rusty@linux.co.intel.com>
  22. * Satyam Sharma <satyam@infradead.org>
  23. * Randy Dunlap <randy.dunlap@oracle.com>
  24. *
  25. * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
  26. * admit liability nor provide warranty for any of this software.
  27. * This material is provided "AS-IS" and at no charge.
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/cdev.h> /* For character device */
  31. #include <linux/errno.h> /* For the -ENODEV/... values */
  32. #include <linux/fs.h> /* For file operations */
  33. #include <linux/init.h> /* For __init/__exit/... */
  34. #include <linux/hrtimer.h> /* For hrtimers */
  35. #include <linux/kernel.h> /* For printk/panic/... */
  36. #include <linux/kstrtox.h> /* For kstrto* */
  37. #include <linux/kthread.h> /* For kthread_work */
  38. #include <linux/miscdevice.h> /* For handling misc devices */
  39. #include <linux/module.h> /* For module stuff/... */
  40. #include <linux/mutex.h> /* For mutexes */
  41. #include <linux/slab.h> /* For memory functions */
  42. #include <linux/types.h> /* For standard types (like size_t) */
  43. #include <linux/watchdog.h> /* For watchdog specific items */
  44. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  45. #include "watchdog_core.h"
  46. #include "watchdog_pretimeout.h"
  47. #include <trace/events/watchdog.h>
  48. /* the dev_t structure to store the dynamically allocated watchdog devices */
  49. static dev_t watchdog_devt;
  50. /* Reference to watchdog device behind /dev/watchdog */
  51. static struct watchdog_core_data *old_wd_data;
  52. static struct kthread_worker *watchdog_kworker;
  53. static bool handle_boot_enabled =
  54. IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED);
  55. static unsigned open_timeout = CONFIG_WATCHDOG_OPEN_TIMEOUT;
  56. static bool watchdog_past_open_deadline(struct watchdog_core_data *data)
  57. {
  58. return ktime_after(ktime_get(), data->open_deadline);
  59. }
  60. static void watchdog_set_open_deadline(struct watchdog_core_data *data)
  61. {
  62. data->open_deadline = open_timeout ?
  63. ktime_get() + ktime_set(open_timeout, 0) : KTIME_MAX;
  64. }
  65. static inline bool watchdog_need_worker(struct watchdog_device *wdd)
  66. {
  67. /* All variables in milli-seconds */
  68. unsigned int hm = wdd->max_hw_heartbeat_ms;
  69. unsigned int t = wdd->timeout * 1000;
  70. /*
  71. * A worker to generate heartbeat requests is needed if all of the
  72. * following conditions are true.
  73. * - Userspace activated the watchdog.
  74. * - The driver provided a value for the maximum hardware timeout, and
  75. * thus is aware that the framework supports generating heartbeat
  76. * requests.
  77. * - Userspace requests a longer timeout than the hardware can handle.
  78. *
  79. * Alternatively, if userspace has not opened the watchdog
  80. * device, we take care of feeding the watchdog if it is
  81. * running.
  82. */
  83. return (hm && watchdog_active(wdd) && t > hm) ||
  84. (t && !watchdog_active(wdd) && watchdog_hw_running(wdd));
  85. }
  86. static ktime_t watchdog_next_keepalive(struct watchdog_device *wdd)
  87. {
  88. struct watchdog_core_data *wd_data = wdd->wd_data;
  89. unsigned int timeout_ms = wdd->timeout * 1000;
  90. ktime_t keepalive_interval;
  91. ktime_t last_heartbeat, latest_heartbeat;
  92. ktime_t virt_timeout;
  93. unsigned int hw_heartbeat_ms;
  94. if (watchdog_active(wdd))
  95. virt_timeout = ktime_add(wd_data->last_keepalive,
  96. ms_to_ktime(timeout_ms));
  97. else
  98. virt_timeout = wd_data->open_deadline;
  99. hw_heartbeat_ms = min_not_zero(timeout_ms, wdd->max_hw_heartbeat_ms);
  100. keepalive_interval = ms_to_ktime(hw_heartbeat_ms / 2);
  101. /*
  102. * To ensure that the watchdog times out wdd->timeout seconds
  103. * after the most recent ping from userspace, the last
  104. * worker ping has to come in hw_heartbeat_ms before this timeout.
  105. */
  106. last_heartbeat = ktime_sub(virt_timeout, ms_to_ktime(hw_heartbeat_ms));
  107. latest_heartbeat = ktime_sub(last_heartbeat, ktime_get());
  108. if (ktime_before(latest_heartbeat, keepalive_interval))
  109. return latest_heartbeat;
  110. return keepalive_interval;
  111. }
  112. static inline void watchdog_update_worker(struct watchdog_device *wdd)
  113. {
  114. struct watchdog_core_data *wd_data = wdd->wd_data;
  115. if (watchdog_need_worker(wdd)) {
  116. ktime_t t = watchdog_next_keepalive(wdd);
  117. if (t > 0)
  118. hrtimer_start(&wd_data->timer, t,
  119. HRTIMER_MODE_REL_HARD);
  120. } else {
  121. hrtimer_cancel(&wd_data->timer);
  122. }
  123. }
  124. static int __watchdog_ping(struct watchdog_device *wdd)
  125. {
  126. struct watchdog_core_data *wd_data = wdd->wd_data;
  127. ktime_t earliest_keepalive, now;
  128. int err;
  129. earliest_keepalive = ktime_add(wd_data->last_hw_keepalive,
  130. ms_to_ktime(wdd->min_hw_heartbeat_ms));
  131. now = ktime_get();
  132. if (ktime_after(earliest_keepalive, now)) {
  133. hrtimer_start(&wd_data->timer,
  134. ktime_sub(earliest_keepalive, now),
  135. HRTIMER_MODE_REL_HARD);
  136. return 0;
  137. }
  138. wd_data->last_hw_keepalive = now;
  139. if (wdd->ops->ping) {
  140. err = wdd->ops->ping(wdd); /* ping the watchdog */
  141. trace_watchdog_ping(wdd, err);
  142. } else {
  143. err = wdd->ops->start(wdd); /* restart watchdog */
  144. trace_watchdog_start(wdd, err);
  145. }
  146. if (err == 0)
  147. watchdog_hrtimer_pretimeout_start(wdd);
  148. watchdog_update_worker(wdd);
  149. return err;
  150. }
  151. /*
  152. * watchdog_ping - ping the watchdog
  153. * @wdd: The watchdog device to ping
  154. *
  155. * If the watchdog has no own ping operation then it needs to be
  156. * restarted via the start operation. This wrapper function does
  157. * exactly that.
  158. * We only ping when the watchdog device is running.
  159. * The caller must hold wd_data->lock.
  160. *
  161. * Return: 0 on success, error otherwise.
  162. */
  163. static int watchdog_ping(struct watchdog_device *wdd)
  164. {
  165. struct watchdog_core_data *wd_data = wdd->wd_data;
  166. if (!watchdog_hw_running(wdd))
  167. return 0;
  168. set_bit(_WDOG_KEEPALIVE, &wd_data->status);
  169. wd_data->last_keepalive = ktime_get();
  170. return __watchdog_ping(wdd);
  171. }
  172. static bool watchdog_worker_should_ping(struct watchdog_core_data *wd_data)
  173. {
  174. struct watchdog_device *wdd = wd_data->wdd;
  175. if (!wdd)
  176. return false;
  177. if (watchdog_active(wdd))
  178. return true;
  179. return watchdog_hw_running(wdd) && !watchdog_past_open_deadline(wd_data);
  180. }
  181. static void watchdog_ping_work(struct kthread_work *work)
  182. {
  183. struct watchdog_core_data *wd_data;
  184. wd_data = container_of(work, struct watchdog_core_data, work);
  185. mutex_lock(&wd_data->lock);
  186. if (watchdog_worker_should_ping(wd_data))
  187. __watchdog_ping(wd_data->wdd);
  188. mutex_unlock(&wd_data->lock);
  189. }
  190. static enum hrtimer_restart watchdog_timer_expired(struct hrtimer *timer)
  191. {
  192. struct watchdog_core_data *wd_data;
  193. wd_data = container_of(timer, struct watchdog_core_data, timer);
  194. kthread_queue_work(watchdog_kworker, &wd_data->work);
  195. return HRTIMER_NORESTART;
  196. }
  197. /*
  198. * watchdog_start - wrapper to start the watchdog
  199. * @wdd: The watchdog device to start
  200. *
  201. * Start the watchdog if it is not active and mark it active.
  202. * The caller must hold wd_data->lock.
  203. *
  204. * Return: 0 on success or a negative errno code for failure.
  205. */
  206. static int watchdog_start(struct watchdog_device *wdd)
  207. {
  208. struct watchdog_core_data *wd_data = wdd->wd_data;
  209. ktime_t started_at;
  210. int err;
  211. if (watchdog_active(wdd))
  212. return 0;
  213. set_bit(_WDOG_KEEPALIVE, &wd_data->status);
  214. started_at = ktime_get();
  215. if (watchdog_hw_running(wdd) && wdd->ops->ping) {
  216. err = __watchdog_ping(wdd);
  217. if (err == 0) {
  218. set_bit(WDOG_ACTIVE, &wdd->status);
  219. watchdog_hrtimer_pretimeout_start(wdd);
  220. }
  221. } else {
  222. err = wdd->ops->start(wdd);
  223. trace_watchdog_start(wdd, err);
  224. if (err == 0) {
  225. set_bit(WDOG_ACTIVE, &wdd->status);
  226. set_bit(WDOG_HW_RUNNING, &wdd->status);
  227. wd_data->last_keepalive = started_at;
  228. wd_data->last_hw_keepalive = started_at;
  229. watchdog_update_worker(wdd);
  230. watchdog_hrtimer_pretimeout_start(wdd);
  231. }
  232. }
  233. return err;
  234. }
  235. /*
  236. * watchdog_stop - wrapper to stop the watchdog
  237. * @wdd: The watchdog device to stop
  238. *
  239. * Stop the watchdog if it is still active and unmark it active.
  240. * If the 'nowayout' feature was set, the watchdog cannot be stopped.
  241. * The caller must hold wd_data->lock.
  242. *
  243. * Return: 0 on success or a negative errno code for failure.
  244. */
  245. static int watchdog_stop(struct watchdog_device *wdd)
  246. {
  247. int err = 0;
  248. if (!watchdog_active(wdd))
  249. return 0;
  250. if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
  251. pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
  252. wdd->id);
  253. return -EBUSY;
  254. }
  255. if (wdd->ops->stop) {
  256. clear_bit(WDOG_HW_RUNNING, &wdd->status);
  257. err = wdd->ops->stop(wdd);
  258. trace_watchdog_stop(wdd, err);
  259. } else {
  260. set_bit(WDOG_HW_RUNNING, &wdd->status);
  261. }
  262. if (err == 0) {
  263. clear_bit(WDOG_ACTIVE, &wdd->status);
  264. watchdog_update_worker(wdd);
  265. watchdog_hrtimer_pretimeout_stop(wdd);
  266. }
  267. return err;
  268. }
  269. /*
  270. * watchdog_get_status - wrapper to get the watchdog status
  271. * @wdd: The watchdog device to get the status from
  272. *
  273. * Get the watchdog's status flags.
  274. * The caller must hold wd_data->lock.
  275. *
  276. * Return: watchdog's status flags.
  277. */
  278. static unsigned int watchdog_get_status(struct watchdog_device *wdd)
  279. {
  280. struct watchdog_core_data *wd_data = wdd->wd_data;
  281. unsigned int status;
  282. if (wdd->ops->status)
  283. status = wdd->ops->status(wdd);
  284. else
  285. status = wdd->bootstatus & (WDIOF_CARDRESET |
  286. WDIOF_OVERHEAT |
  287. WDIOF_FANFAULT |
  288. WDIOF_EXTERN1 |
  289. WDIOF_EXTERN2 |
  290. WDIOF_POWERUNDER |
  291. WDIOF_POWEROVER);
  292. if (test_bit(_WDOG_ALLOW_RELEASE, &wd_data->status))
  293. status |= WDIOF_MAGICCLOSE;
  294. if (test_and_clear_bit(_WDOG_KEEPALIVE, &wd_data->status))
  295. status |= WDIOF_KEEPALIVEPING;
  296. if (IS_ENABLED(CONFIG_WATCHDOG_HRTIMER_PRETIMEOUT))
  297. status |= WDIOF_PRETIMEOUT;
  298. return status;
  299. }
  300. /*
  301. * watchdog_set_timeout - set the watchdog timer timeout
  302. * @wdd: The watchdog device to set the timeout for
  303. * @timeout: Timeout to set in seconds
  304. *
  305. * The caller must hold wd_data->lock.
  306. *
  307. * Return: 0 if successful, error otherwise.
  308. */
  309. static int watchdog_set_timeout(struct watchdog_device *wdd,
  310. unsigned int timeout)
  311. {
  312. int err = 0;
  313. if (!(wdd->info->options & WDIOF_SETTIMEOUT))
  314. return -EOPNOTSUPP;
  315. if (watchdog_timeout_invalid(wdd, timeout))
  316. return -EINVAL;
  317. if (wdd->ops->set_timeout) {
  318. err = wdd->ops->set_timeout(wdd, timeout);
  319. trace_watchdog_set_timeout(wdd, timeout, err);
  320. } else {
  321. wdd->timeout = timeout;
  322. /* Disable pretimeout if it doesn't fit the new timeout */
  323. if (wdd->pretimeout >= wdd->timeout)
  324. wdd->pretimeout = 0;
  325. }
  326. watchdog_update_worker(wdd);
  327. return err;
  328. }
  329. /*
  330. * watchdog_set_pretimeout - set the watchdog timer pretimeout
  331. * @wdd: The watchdog device to set the timeout for
  332. * @timeout: pretimeout to set in seconds
  333. *
  334. * Return: 0 if successful, error otherwise.
  335. */
  336. static int watchdog_set_pretimeout(struct watchdog_device *wdd,
  337. unsigned int timeout)
  338. {
  339. int err = 0;
  340. if (!watchdog_have_pretimeout(wdd))
  341. return -EOPNOTSUPP;
  342. if (watchdog_pretimeout_invalid(wdd, timeout))
  343. return -EINVAL;
  344. if (wdd->ops->set_pretimeout && (wdd->info->options & WDIOF_PRETIMEOUT))
  345. err = wdd->ops->set_pretimeout(wdd, timeout);
  346. else
  347. wdd->pretimeout = timeout;
  348. return err;
  349. }
  350. /*
  351. * watchdog_get_timeleft - wrapper to get the time left before a reboot
  352. * @wdd: The watchdog device to get the remaining time from
  353. * @timeleft: The time that's left
  354. *
  355. * Get the time before a watchdog will reboot (if not pinged).
  356. * The caller must hold wd_data->lock.
  357. */
  358. static void watchdog_get_timeleft(struct watchdog_device *wdd,
  359. unsigned int *timeleft)
  360. {
  361. *timeleft = 0;
  362. if (wdd->ops->get_timeleft) {
  363. *timeleft = wdd->ops->get_timeleft(wdd);
  364. } else {
  365. struct watchdog_core_data *wd_data = wdd->wd_data;
  366. s64 last_keepalive_ms = ktime_ms_delta(ktime_get(), wd_data->last_keepalive);
  367. s64 last_keepalive = DIV_ROUND_UP_ULL(last_keepalive_ms, 1000);
  368. if (wdd->timeout > last_keepalive)
  369. *timeleft = wdd->timeout - last_keepalive;
  370. }
  371. }
  372. #ifdef CONFIG_WATCHDOG_SYSFS
  373. static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
  374. char *buf)
  375. {
  376. struct watchdog_device *wdd = dev_get_drvdata(dev);
  377. return sysfs_emit(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT,
  378. &wdd->status));
  379. }
  380. static ssize_t nowayout_store(struct device *dev, struct device_attribute *attr,
  381. const char *buf, size_t len)
  382. {
  383. struct watchdog_device *wdd = dev_get_drvdata(dev);
  384. unsigned int value;
  385. int ret;
  386. ret = kstrtouint(buf, 0, &value);
  387. if (ret)
  388. return ret;
  389. if (value > 1)
  390. return -EINVAL;
  391. /* nowayout cannot be disabled once set */
  392. if (test_bit(WDOG_NO_WAY_OUT, &wdd->status) && !value)
  393. return -EPERM;
  394. watchdog_set_nowayout(wdd, value);
  395. return len;
  396. }
  397. static DEVICE_ATTR_RW(nowayout);
  398. static ssize_t status_show(struct device *dev, struct device_attribute *attr,
  399. char *buf)
  400. {
  401. struct watchdog_device *wdd = dev_get_drvdata(dev);
  402. struct watchdog_core_data *wd_data = wdd->wd_data;
  403. unsigned int status;
  404. mutex_lock(&wd_data->lock);
  405. status = watchdog_get_status(wdd);
  406. mutex_unlock(&wd_data->lock);
  407. return sysfs_emit(buf, "0x%x\n", status);
  408. }
  409. static DEVICE_ATTR_RO(status);
  410. static ssize_t bootstatus_show(struct device *dev,
  411. struct device_attribute *attr, char *buf)
  412. {
  413. struct watchdog_device *wdd = dev_get_drvdata(dev);
  414. return sysfs_emit(buf, "%u\n", wdd->bootstatus);
  415. }
  416. static DEVICE_ATTR_RO(bootstatus);
  417. static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr,
  418. char *buf)
  419. {
  420. struct watchdog_device *wdd = dev_get_drvdata(dev);
  421. struct watchdog_core_data *wd_data = wdd->wd_data;
  422. unsigned int val;
  423. mutex_lock(&wd_data->lock);
  424. watchdog_get_timeleft(wdd, &val);
  425. mutex_unlock(&wd_data->lock);
  426. return sysfs_emit(buf, "%u\n", val);
  427. }
  428. static DEVICE_ATTR_RO(timeleft);
  429. static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
  430. char *buf)
  431. {
  432. struct watchdog_device *wdd = dev_get_drvdata(dev);
  433. return sysfs_emit(buf, "%u\n", wdd->timeout);
  434. }
  435. static DEVICE_ATTR_RO(timeout);
  436. static ssize_t min_timeout_show(struct device *dev,
  437. struct device_attribute *attr, char *buf)
  438. {
  439. struct watchdog_device *wdd = dev_get_drvdata(dev);
  440. return sysfs_emit(buf, "%u\n", wdd->min_timeout);
  441. }
  442. static DEVICE_ATTR_RO(min_timeout);
  443. static ssize_t max_timeout_show(struct device *dev,
  444. struct device_attribute *attr, char *buf)
  445. {
  446. struct watchdog_device *wdd = dev_get_drvdata(dev);
  447. return sysfs_emit(buf, "%u\n", wdd->max_timeout);
  448. }
  449. static DEVICE_ATTR_RO(max_timeout);
  450. static ssize_t pretimeout_show(struct device *dev,
  451. struct device_attribute *attr, char *buf)
  452. {
  453. struct watchdog_device *wdd = dev_get_drvdata(dev);
  454. return sysfs_emit(buf, "%u\n", wdd->pretimeout);
  455. }
  456. static DEVICE_ATTR_RO(pretimeout);
  457. static ssize_t options_show(struct device *dev, struct device_attribute *attr,
  458. char *buf)
  459. {
  460. struct watchdog_device *wdd = dev_get_drvdata(dev);
  461. return sysfs_emit(buf, "0x%x\n", wdd->info->options);
  462. }
  463. static DEVICE_ATTR_RO(options);
  464. static ssize_t fw_version_show(struct device *dev, struct device_attribute *attr,
  465. char *buf)
  466. {
  467. struct watchdog_device *wdd = dev_get_drvdata(dev);
  468. return sysfs_emit(buf, "%d\n", wdd->info->firmware_version);
  469. }
  470. static DEVICE_ATTR_RO(fw_version);
  471. static ssize_t identity_show(struct device *dev, struct device_attribute *attr,
  472. char *buf)
  473. {
  474. struct watchdog_device *wdd = dev_get_drvdata(dev);
  475. return sysfs_emit(buf, "%s\n", wdd->info->identity);
  476. }
  477. static DEVICE_ATTR_RO(identity);
  478. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  479. char *buf)
  480. {
  481. struct watchdog_device *wdd = dev_get_drvdata(dev);
  482. if (watchdog_active(wdd))
  483. return sysfs_emit(buf, "active\n");
  484. return sysfs_emit(buf, "inactive\n");
  485. }
  486. static DEVICE_ATTR_RO(state);
  487. static ssize_t pretimeout_available_governors_show(struct device *dev,
  488. struct device_attribute *attr, char *buf)
  489. {
  490. return watchdog_pretimeout_available_governors_get(buf);
  491. }
  492. static DEVICE_ATTR_RO(pretimeout_available_governors);
  493. static ssize_t pretimeout_governor_show(struct device *dev,
  494. struct device_attribute *attr,
  495. char *buf)
  496. {
  497. struct watchdog_device *wdd = dev_get_drvdata(dev);
  498. return watchdog_pretimeout_governor_get(wdd, buf);
  499. }
  500. static ssize_t pretimeout_governor_store(struct device *dev,
  501. struct device_attribute *attr,
  502. const char *buf, size_t count)
  503. {
  504. struct watchdog_device *wdd = dev_get_drvdata(dev);
  505. int ret = watchdog_pretimeout_governor_set(wdd, buf);
  506. if (!ret)
  507. ret = count;
  508. return ret;
  509. }
  510. static DEVICE_ATTR_RW(pretimeout_governor);
  511. static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr,
  512. int n)
  513. {
  514. struct device *dev = kobj_to_dev(kobj);
  515. struct watchdog_device *wdd = dev_get_drvdata(dev);
  516. umode_t mode = attr->mode;
  517. if (attr == &dev_attr_pretimeout.attr && !watchdog_have_pretimeout(wdd))
  518. mode = 0;
  519. else if ((attr == &dev_attr_pretimeout_governor.attr ||
  520. attr == &dev_attr_pretimeout_available_governors.attr) &&
  521. (!watchdog_have_pretimeout(wdd) || !IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV)))
  522. mode = 0;
  523. return mode;
  524. }
  525. static struct attribute *wdt_attrs[] = {
  526. &dev_attr_state.attr,
  527. &dev_attr_options.attr,
  528. &dev_attr_fw_version.attr,
  529. &dev_attr_identity.attr,
  530. &dev_attr_timeout.attr,
  531. &dev_attr_min_timeout.attr,
  532. &dev_attr_max_timeout.attr,
  533. &dev_attr_pretimeout.attr,
  534. &dev_attr_timeleft.attr,
  535. &dev_attr_bootstatus.attr,
  536. &dev_attr_status.attr,
  537. &dev_attr_nowayout.attr,
  538. &dev_attr_pretimeout_governor.attr,
  539. &dev_attr_pretimeout_available_governors.attr,
  540. NULL,
  541. };
  542. static const struct attribute_group wdt_group = {
  543. .attrs = wdt_attrs,
  544. .is_visible = wdt_is_visible,
  545. };
  546. __ATTRIBUTE_GROUPS(wdt);
  547. #else
  548. #define wdt_groups NULL
  549. #endif
  550. /*
  551. * watchdog_ioctl_op - call the watchdog drivers ioctl op if defined
  552. * @wdd: The watchdog device to do the ioctl on
  553. * @cmd: Watchdog command
  554. * @arg: Argument pointer
  555. *
  556. * The caller must hold wd_data->lock.
  557. *
  558. * Return: 0 if successful, error otherwise.
  559. */
  560. static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
  561. unsigned long arg)
  562. {
  563. if (!wdd->ops->ioctl)
  564. return -ENOIOCTLCMD;
  565. return wdd->ops->ioctl(wdd, cmd, arg);
  566. }
  567. /*
  568. * watchdog_write - writes to the watchdog
  569. * @file: File from VFS
  570. * @data: User address of data
  571. * @len: Length of data
  572. * @ppos: Pointer to the file offset
  573. *
  574. * A write to a watchdog device is defined as a keepalive ping.
  575. * Writing the magic 'V' sequence allows the next close to turn
  576. * off the watchdog (if 'nowayout' is not set).
  577. *
  578. * Return: @len if successful, error otherwise.
  579. */
  580. static ssize_t watchdog_write(struct file *file, const char __user *data,
  581. size_t len, loff_t *ppos)
  582. {
  583. struct watchdog_core_data *wd_data = file->private_data;
  584. struct watchdog_device *wdd;
  585. int err;
  586. size_t i;
  587. char c;
  588. if (len == 0)
  589. return 0;
  590. /*
  591. * Note: just in case someone wrote the magic character
  592. * five months ago...
  593. */
  594. clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
  595. /* scan to see whether or not we got the magic character */
  596. for (i = 0; i != len; i++) {
  597. if (get_user(c, data + i))
  598. return -EFAULT;
  599. if (c == 'V')
  600. set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
  601. }
  602. /* someone wrote to us, so we send the watchdog a keepalive ping */
  603. err = -ENODEV;
  604. mutex_lock(&wd_data->lock);
  605. wdd = wd_data->wdd;
  606. if (wdd)
  607. err = watchdog_ping(wdd);
  608. mutex_unlock(&wd_data->lock);
  609. if (err < 0)
  610. return err;
  611. return len;
  612. }
  613. /*
  614. * watchdog_ioctl - handle the different ioctl's for the watchdog device
  615. * @file: File handle to the device
  616. * @cmd: Watchdog command
  617. * @arg: Argument pointer
  618. *
  619. * The watchdog API defines a common set of functions for all watchdogs
  620. * according to their available features.
  621. *
  622. * Return: 0 if successful, error otherwise.
  623. */
  624. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  625. unsigned long arg)
  626. {
  627. struct watchdog_core_data *wd_data = file->private_data;
  628. void __user *argp = (void __user *)arg;
  629. struct watchdog_device *wdd;
  630. int __user *p = argp;
  631. unsigned int val;
  632. int err;
  633. mutex_lock(&wd_data->lock);
  634. wdd = wd_data->wdd;
  635. if (!wdd) {
  636. err = -ENODEV;
  637. goto out_ioctl;
  638. }
  639. err = watchdog_ioctl_op(wdd, cmd, arg);
  640. if (err != -ENOIOCTLCMD)
  641. goto out_ioctl;
  642. switch (cmd) {
  643. case WDIOC_GETSUPPORT:
  644. err = copy_to_user(argp, wdd->info,
  645. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  646. break;
  647. case WDIOC_GETSTATUS:
  648. val = watchdog_get_status(wdd);
  649. err = put_user(val, p);
  650. break;
  651. case WDIOC_GETBOOTSTATUS:
  652. err = put_user(wdd->bootstatus, p);
  653. break;
  654. case WDIOC_SETOPTIONS:
  655. if (get_user(val, p)) {
  656. err = -EFAULT;
  657. break;
  658. }
  659. if (val & WDIOS_DISABLECARD) {
  660. err = watchdog_stop(wdd);
  661. if (err < 0)
  662. break;
  663. }
  664. if (val & WDIOS_ENABLECARD)
  665. err = watchdog_start(wdd);
  666. break;
  667. case WDIOC_KEEPALIVE:
  668. if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
  669. err = -EOPNOTSUPP;
  670. break;
  671. }
  672. err = watchdog_ping(wdd);
  673. break;
  674. case WDIOC_SETTIMEOUT:
  675. if (get_user(val, p)) {
  676. err = -EFAULT;
  677. break;
  678. }
  679. err = watchdog_set_timeout(wdd, val);
  680. if (err < 0)
  681. break;
  682. /* If the watchdog is active then we send a keepalive ping
  683. * to make sure that the watchdog keep's running (and if
  684. * possible that it takes the new timeout) */
  685. err = watchdog_ping(wdd);
  686. if (err < 0)
  687. break;
  688. fallthrough;
  689. case WDIOC_GETTIMEOUT:
  690. /* timeout == 0 means that we don't know the timeout */
  691. if (wdd->timeout == 0) {
  692. err = -EOPNOTSUPP;
  693. break;
  694. }
  695. err = put_user(wdd->timeout, p);
  696. break;
  697. case WDIOC_GETTIMELEFT:
  698. watchdog_get_timeleft(wdd, &val);
  699. err = put_user(val, p);
  700. break;
  701. case WDIOC_SETPRETIMEOUT:
  702. if (get_user(val, p)) {
  703. err = -EFAULT;
  704. break;
  705. }
  706. err = watchdog_set_pretimeout(wdd, val);
  707. break;
  708. case WDIOC_GETPRETIMEOUT:
  709. err = put_user(wdd->pretimeout, p);
  710. break;
  711. default:
  712. err = -ENOTTY;
  713. break;
  714. }
  715. out_ioctl:
  716. mutex_unlock(&wd_data->lock);
  717. return err;
  718. }
  719. /*
  720. * watchdog_open - open the /dev/watchdog* devices
  721. * @inode: Inode of device
  722. * @file: File handle to device
  723. *
  724. * When the /dev/watchdog* device gets opened, we start the watchdog.
  725. * Watch out: the /dev/watchdog device is single open, so we make sure
  726. * it can only be opened once.
  727. *
  728. * Return: 0 if successful, error otherwise.
  729. */
  730. static int watchdog_open(struct inode *inode, struct file *file)
  731. {
  732. struct watchdog_core_data *wd_data;
  733. struct watchdog_device *wdd;
  734. bool hw_running;
  735. int err;
  736. /* Get the corresponding watchdog device */
  737. if (imajor(inode) == MISC_MAJOR)
  738. wd_data = old_wd_data;
  739. else
  740. wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
  741. cdev);
  742. /* the watchdog is single open! */
  743. if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
  744. return -EBUSY;
  745. wdd = wd_data->wdd;
  746. /*
  747. * If the /dev/watchdog device is open, we don't want the module
  748. * to be unloaded.
  749. */
  750. hw_running = watchdog_hw_running(wdd);
  751. if (!hw_running && !try_module_get(wdd->ops->owner)) {
  752. err = -EBUSY;
  753. goto out_clear;
  754. }
  755. err = watchdog_start(wdd);
  756. if (err < 0)
  757. goto out_mod;
  758. file->private_data = wd_data;
  759. if (!hw_running)
  760. get_device(&wd_data->dev);
  761. /*
  762. * open_timeout only applies for the first open from
  763. * userspace. Set open_deadline to infinity so that the kernel
  764. * will take care of an always-running hardware watchdog in
  765. * case the device gets magic-closed or WDIOS_DISABLECARD is
  766. * applied.
  767. */
  768. wd_data->open_deadline = KTIME_MAX;
  769. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  770. return stream_open(inode, file);
  771. out_mod:
  772. module_put(wd_data->wdd->ops->owner);
  773. out_clear:
  774. clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
  775. return err;
  776. }
  777. static void watchdog_core_data_release(struct device *dev)
  778. {
  779. struct watchdog_core_data *wd_data;
  780. wd_data = container_of(dev, struct watchdog_core_data, dev);
  781. kfree(wd_data);
  782. }
  783. /*
  784. * watchdog_release - release the watchdog device
  785. * @inode: Inode of device
  786. * @file: File handle to device
  787. *
  788. * This is the code for when /dev/watchdog gets closed. We will only
  789. * stop the watchdog when we have received the magic char (and nowayout
  790. * was not set), else the watchdog will keep running.
  791. *
  792. * Always returns 0.
  793. */
  794. static int watchdog_release(struct inode *inode, struct file *file)
  795. {
  796. struct watchdog_core_data *wd_data = file->private_data;
  797. struct watchdog_device *wdd;
  798. int err = -EBUSY;
  799. bool running;
  800. mutex_lock(&wd_data->lock);
  801. wdd = wd_data->wdd;
  802. if (!wdd)
  803. goto done;
  804. /*
  805. * We only stop the watchdog if we received the magic character
  806. * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
  807. * watchdog_stop will fail.
  808. */
  809. if (!watchdog_active(wdd))
  810. err = 0;
  811. else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
  812. !(wdd->info->options & WDIOF_MAGICCLOSE))
  813. err = watchdog_stop(wdd);
  814. /* If the watchdog was not stopped, send a keepalive ping */
  815. if (err < 0) {
  816. pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
  817. watchdog_ping(wdd);
  818. }
  819. watchdog_update_worker(wdd);
  820. /* make sure that /dev/watchdog can be re-opened */
  821. clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
  822. done:
  823. running = wdd && watchdog_hw_running(wdd);
  824. mutex_unlock(&wd_data->lock);
  825. /*
  826. * Allow the owner module to be unloaded again unless the watchdog
  827. * is still running. If the watchdog is still running, it can not
  828. * be stopped, and its driver must not be unloaded.
  829. */
  830. if (!running) {
  831. module_put(wd_data->cdev.owner);
  832. put_device(&wd_data->dev);
  833. }
  834. return 0;
  835. }
  836. static const struct file_operations watchdog_fops = {
  837. .owner = THIS_MODULE,
  838. .write = watchdog_write,
  839. .unlocked_ioctl = watchdog_ioctl,
  840. .compat_ioctl = compat_ptr_ioctl,
  841. .open = watchdog_open,
  842. .release = watchdog_release,
  843. };
  844. static struct miscdevice watchdog_miscdev = {
  845. .minor = WATCHDOG_MINOR,
  846. .name = "watchdog",
  847. .fops = &watchdog_fops,
  848. };
  849. static const struct class watchdog_class = {
  850. .name = "watchdog",
  851. .dev_groups = wdt_groups,
  852. };
  853. /*
  854. * watchdog_cdev_register - register watchdog character device
  855. * @wdd: Watchdog device
  856. *
  857. * Register a watchdog character device including handling the legacy
  858. * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
  859. * thus we set it up like that.
  860. *
  861. * Return: 0 if successful, error otherwise.
  862. */
  863. static int watchdog_cdev_register(struct watchdog_device *wdd)
  864. {
  865. struct watchdog_core_data *wd_data;
  866. int err;
  867. wd_data = kzalloc_obj(struct watchdog_core_data);
  868. if (!wd_data)
  869. return -ENOMEM;
  870. mutex_init(&wd_data->lock);
  871. wd_data->wdd = wdd;
  872. wdd->wd_data = wd_data;
  873. if (IS_ERR_OR_NULL(watchdog_kworker)) {
  874. kfree(wd_data);
  875. return -ENODEV;
  876. }
  877. device_initialize(&wd_data->dev);
  878. wd_data->dev.devt = MKDEV(MAJOR(watchdog_devt), wdd->id);
  879. wd_data->dev.class = &watchdog_class;
  880. wd_data->dev.parent = wdd->parent;
  881. wd_data->dev.groups = wdd->groups;
  882. wd_data->dev.release = watchdog_core_data_release;
  883. dev_set_drvdata(&wd_data->dev, wdd);
  884. err = dev_set_name(&wd_data->dev, "watchdog%d", wdd->id);
  885. if (err) {
  886. put_device(&wd_data->dev);
  887. return err;
  888. }
  889. kthread_init_work(&wd_data->work, watchdog_ping_work);
  890. hrtimer_setup(&wd_data->timer, watchdog_timer_expired, CLOCK_MONOTONIC,
  891. HRTIMER_MODE_REL_HARD);
  892. watchdog_hrtimer_pretimeout_init(wdd);
  893. if (wdd->id == 0) {
  894. old_wd_data = wd_data;
  895. watchdog_miscdev.parent = wdd->parent;
  896. err = misc_register(&watchdog_miscdev);
  897. if (err != 0) {
  898. pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
  899. wdd->info->identity, WATCHDOG_MINOR, err);
  900. if (err == -EBUSY)
  901. pr_err("%s: a legacy watchdog module is probably present.\n",
  902. wdd->info->identity);
  903. old_wd_data = NULL;
  904. put_device(&wd_data->dev);
  905. return err;
  906. }
  907. }
  908. /* Fill in the data structures */
  909. cdev_init(&wd_data->cdev, &watchdog_fops);
  910. wd_data->cdev.owner = wdd->ops->owner;
  911. /* Add the device */
  912. err = cdev_device_add(&wd_data->cdev, &wd_data->dev);
  913. if (err) {
  914. pr_err("watchdog%d unable to add device %d:%d\n",
  915. wdd->id, MAJOR(watchdog_devt), wdd->id);
  916. if (wdd->id == 0) {
  917. misc_deregister(&watchdog_miscdev);
  918. old_wd_data = NULL;
  919. }
  920. put_device(&wd_data->dev);
  921. return err;
  922. }
  923. /* Record time of most recent heartbeat as 'just before now'. */
  924. wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1);
  925. watchdog_set_open_deadline(wd_data);
  926. /*
  927. * If the watchdog is running, prevent its driver from being unloaded,
  928. * and schedule an immediate ping.
  929. */
  930. if (watchdog_hw_running(wdd)) {
  931. __module_get(wdd->ops->owner);
  932. get_device(&wd_data->dev);
  933. if (handle_boot_enabled)
  934. hrtimer_start(&wd_data->timer, 0,
  935. HRTIMER_MODE_REL_HARD);
  936. else
  937. pr_info("watchdog%d running and kernel based pre-userspace handler disabled\n",
  938. wdd->id);
  939. }
  940. return 0;
  941. }
  942. /*
  943. * watchdog_cdev_unregister - unregister watchdog character device
  944. * @wdd: Watchdog device
  945. *
  946. * Unregister watchdog character device and if needed the legacy
  947. * /dev/watchdog device.
  948. */
  949. static void watchdog_cdev_unregister(struct watchdog_device *wdd)
  950. {
  951. struct watchdog_core_data *wd_data = wdd->wd_data;
  952. cdev_device_del(&wd_data->cdev, &wd_data->dev);
  953. if (wdd->id == 0) {
  954. misc_deregister(&watchdog_miscdev);
  955. old_wd_data = NULL;
  956. }
  957. if (watchdog_active(wdd) &&
  958. test_bit(WDOG_STOP_ON_UNREGISTER, &wdd->status)) {
  959. watchdog_stop(wdd);
  960. }
  961. watchdog_hrtimer_pretimeout_stop(wdd);
  962. mutex_lock(&wd_data->lock);
  963. wd_data->wdd = NULL;
  964. wdd->wd_data = NULL;
  965. mutex_unlock(&wd_data->lock);
  966. hrtimer_cancel(&wd_data->timer);
  967. kthread_cancel_work_sync(&wd_data->work);
  968. put_device(&wd_data->dev);
  969. }
  970. /**
  971. * watchdog_dev_register - register a watchdog device
  972. * @wdd: Watchdog device
  973. *
  974. * Register a watchdog device including handling the legacy
  975. * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
  976. * thus we set it up like that.
  977. *
  978. * Return: 0 if successful, error otherwise.
  979. */
  980. int watchdog_dev_register(struct watchdog_device *wdd)
  981. {
  982. int ret;
  983. ret = watchdog_cdev_register(wdd);
  984. if (ret)
  985. return ret;
  986. ret = watchdog_register_pretimeout(wdd);
  987. if (ret)
  988. watchdog_cdev_unregister(wdd);
  989. return ret;
  990. }
  991. /**
  992. * watchdog_dev_unregister - unregister a watchdog device
  993. * @wdd: watchdog device
  994. *
  995. * Unregister watchdog device and if needed the legacy
  996. * /dev/watchdog device.
  997. */
  998. void watchdog_dev_unregister(struct watchdog_device *wdd)
  999. {
  1000. watchdog_unregister_pretimeout(wdd);
  1001. watchdog_cdev_unregister(wdd);
  1002. }
  1003. /**
  1004. * watchdog_set_last_hw_keepalive - set last HW keepalive time for watchdog
  1005. * @wdd: Watchdog device
  1006. * @last_ping_ms: Time since last HW heartbeat
  1007. *
  1008. * Adjusts the last known HW keepalive time for a watchdog timer.
  1009. * This is needed if the watchdog is already running when the probe
  1010. * function is called, and it can't be pinged immediately. This
  1011. * function must be called immediately after watchdog registration,
  1012. * and min_hw_heartbeat_ms must be set for this to be useful.
  1013. *
  1014. * Return: 0 if successful, error otherwise.
  1015. */
  1016. int watchdog_set_last_hw_keepalive(struct watchdog_device *wdd,
  1017. unsigned int last_ping_ms)
  1018. {
  1019. struct watchdog_core_data *wd_data;
  1020. ktime_t now;
  1021. if (!wdd)
  1022. return -EINVAL;
  1023. wd_data = wdd->wd_data;
  1024. now = ktime_get();
  1025. wd_data->last_hw_keepalive = ktime_sub(now, ms_to_ktime(last_ping_ms));
  1026. if (watchdog_hw_running(wdd) && handle_boot_enabled)
  1027. return __watchdog_ping(wdd);
  1028. return 0;
  1029. }
  1030. EXPORT_SYMBOL_GPL(watchdog_set_last_hw_keepalive);
  1031. /**
  1032. * watchdog_dev_init - init dev part of watchdog core
  1033. *
  1034. * Allocate a range of chardev nodes to use for watchdog devices.
  1035. *
  1036. * Return: 0 if successful, error otherwise.
  1037. */
  1038. int __init watchdog_dev_init(void)
  1039. {
  1040. int err;
  1041. watchdog_kworker = kthread_run_worker(0, "watchdogd");
  1042. if (IS_ERR(watchdog_kworker)) {
  1043. pr_err("Failed to create watchdog kworker\n");
  1044. return PTR_ERR(watchdog_kworker);
  1045. }
  1046. sched_set_fifo(watchdog_kworker->task);
  1047. err = class_register(&watchdog_class);
  1048. if (err < 0) {
  1049. pr_err("couldn't register class\n");
  1050. goto err_register;
  1051. }
  1052. err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
  1053. if (err < 0) {
  1054. pr_err("watchdog: unable to allocate char dev region\n");
  1055. goto err_alloc;
  1056. }
  1057. return 0;
  1058. err_alloc:
  1059. class_unregister(&watchdog_class);
  1060. err_register:
  1061. kthread_destroy_worker(watchdog_kworker);
  1062. return err;
  1063. }
  1064. /**
  1065. * watchdog_dev_exit - exit dev part of watchdog core
  1066. *
  1067. * Release the range of chardev nodes used for watchdog devices.
  1068. */
  1069. void __exit watchdog_dev_exit(void)
  1070. {
  1071. unregister_chrdev_region(watchdog_devt, MAX_DOGS);
  1072. class_unregister(&watchdog_class);
  1073. kthread_destroy_worker(watchdog_kworker);
  1074. }
  1075. int watchdog_dev_suspend(struct watchdog_device *wdd)
  1076. {
  1077. struct watchdog_core_data *wd_data = wdd->wd_data;
  1078. int ret = 0;
  1079. if (!wdd->wd_data)
  1080. return -ENODEV;
  1081. /* ping for the last time before suspend */
  1082. mutex_lock(&wd_data->lock);
  1083. if (watchdog_worker_should_ping(wd_data))
  1084. ret = __watchdog_ping(wd_data->wdd);
  1085. mutex_unlock(&wd_data->lock);
  1086. if (ret)
  1087. return ret;
  1088. /*
  1089. * make sure that watchdog worker will not kick in when the wdog is
  1090. * suspended
  1091. */
  1092. hrtimer_cancel(&wd_data->timer);
  1093. kthread_cancel_work_sync(&wd_data->work);
  1094. return 0;
  1095. }
  1096. int watchdog_dev_resume(struct watchdog_device *wdd)
  1097. {
  1098. struct watchdog_core_data *wd_data = wdd->wd_data;
  1099. int ret = 0;
  1100. if (!wdd->wd_data)
  1101. return -ENODEV;
  1102. /*
  1103. * __watchdog_ping will also retrigger hrtimer and therefore restore the
  1104. * ping worker if needed.
  1105. */
  1106. mutex_lock(&wd_data->lock);
  1107. if (watchdog_worker_should_ping(wd_data))
  1108. ret = __watchdog_ping(wd_data->wdd);
  1109. mutex_unlock(&wd_data->lock);
  1110. return ret;
  1111. }
  1112. module_param(handle_boot_enabled, bool, 0444);
  1113. MODULE_PARM_DESC(handle_boot_enabled,
  1114. "Watchdog core auto-updates boot enabled watchdogs before userspace takes over (default="
  1115. __MODULE_STRING(IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED)) ")");
  1116. module_param(open_timeout, uint, 0644);
  1117. MODULE_PARM_DESC(open_timeout,
  1118. "Maximum time (in seconds, 0 means infinity) for userspace to take over a running watchdog (default="
  1119. __MODULE_STRING(CONFIG_WATCHDOG_OPEN_TIMEOUT) ")");