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