watchdog_dev.c 29 KB

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