watchdog_dev.c 31 KB

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