| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * watchdog_dev.c |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 19 | 20 | * Rusty Lynch <rusty@linux.co.intel.com> |
|---|
| 20 | 21 | * Satyam Sharma <satyam@infradead.org> |
|---|
| 21 | 22 | * 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 | 23 | * |
|---|
| 28 | 24 | * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw. |
|---|
| 29 | 25 | * admit liability nor provide warranty for any of this software. |
|---|
| .. | .. |
|---|
| 47 | 43 | #include <linux/watchdog.h> /* For watchdog specific items */ |
|---|
| 48 | 44 | #include <linux/uaccess.h> /* For copy_to_user/put_user/... */ |
|---|
| 49 | 45 | |
|---|
| 50 | | -#include <uapi/linux/sched/types.h> /* For struct sched_param */ |
|---|
| 51 | | - |
|---|
| 52 | 46 | #include "watchdog_core.h" |
|---|
| 53 | 47 | #include "watchdog_pretimeout.h" |
|---|
| 54 | 48 | |
|---|
| .. | .. |
|---|
| 67 | 61 | struct mutex lock; |
|---|
| 68 | 62 | ktime_t last_keepalive; |
|---|
| 69 | 63 | ktime_t last_hw_keepalive; |
|---|
| 64 | + ktime_t open_deadline; |
|---|
| 70 | 65 | struct hrtimer timer; |
|---|
| 71 | 66 | struct kthread_work work; |
|---|
| 72 | 67 | unsigned long status; /* Internal status bits */ |
|---|
| .. | .. |
|---|
| 84 | 79 | |
|---|
| 85 | 80 | static bool handle_boot_enabled = |
|---|
| 86 | 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 | +} |
|---|
| 87 | 95 | |
|---|
| 88 | 96 | static inline bool watchdog_need_worker(struct watchdog_device *wdd) |
|---|
| 89 | 97 | { |
|---|
| .. | .. |
|---|
| 117 | 125 | ktime_t virt_timeout; |
|---|
| 118 | 126 | unsigned int hw_heartbeat_ms; |
|---|
| 119 | 127 | |
|---|
| 120 | | - virt_timeout = ktime_add(wd_data->last_keepalive, |
|---|
| 121 | | - ms_to_ktime(timeout_ms)); |
|---|
| 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 | + |
|---|
| 122 | 134 | hw_heartbeat_ms = min_not_zero(timeout_ms, wdd->max_hw_heartbeat_ms); |
|---|
| 123 | 135 | keepalive_interval = ms_to_ktime(hw_heartbeat_ms / 2); |
|---|
| 124 | | - |
|---|
| 125 | | - if (!watchdog_active(wdd)) |
|---|
| 126 | | - return keepalive_interval; |
|---|
| 127 | 136 | |
|---|
| 128 | 137 | /* |
|---|
| 129 | 138 | * To ensure that the watchdog times out wdd->timeout seconds |
|---|
| .. | .. |
|---|
| 145 | 154 | ktime_t t = watchdog_next_keepalive(wdd); |
|---|
| 146 | 155 | |
|---|
| 147 | 156 | if (t > 0) |
|---|
| 148 | | - hrtimer_start(&wd_data->timer, t, HRTIMER_MODE_REL); |
|---|
| 157 | + hrtimer_start(&wd_data->timer, t, |
|---|
| 158 | + HRTIMER_MODE_REL_HARD); |
|---|
| 149 | 159 | } else { |
|---|
| 150 | 160 | hrtimer_cancel(&wd_data->timer); |
|---|
| 151 | 161 | } |
|---|
| .. | .. |
|---|
| 164 | 174 | if (ktime_after(earliest_keepalive, now)) { |
|---|
| 165 | 175 | hrtimer_start(&wd_data->timer, |
|---|
| 166 | 176 | ktime_sub(earliest_keepalive, now), |
|---|
| 167 | | - HRTIMER_MODE_REL); |
|---|
| 177 | + HRTIMER_MODE_REL_HARD); |
|---|
| 168 | 178 | return 0; |
|---|
| 169 | 179 | } |
|---|
| 170 | 180 | |
|---|
| .. | .. |
|---|
| 209 | 219 | { |
|---|
| 210 | 220 | struct watchdog_device *wdd = wd_data->wdd; |
|---|
| 211 | 221 | |
|---|
| 212 | | - return wdd && (watchdog_active(wdd) || watchdog_hw_running(wdd)); |
|---|
| 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); |
|---|
| 213 | 229 | } |
|---|
| 214 | 230 | |
|---|
| 215 | 231 | static void watchdog_ping_work(struct kthread_work *work) |
|---|
| .. | .. |
|---|
| 257 | 273 | set_bit(_WDOG_KEEPALIVE, &wd_data->status); |
|---|
| 258 | 274 | |
|---|
| 259 | 275 | started_at = ktime_get(); |
|---|
| 260 | | - if (watchdog_hw_running(wdd) && wdd->ops->ping) |
|---|
| 261 | | - err = wdd->ops->ping(wdd); |
|---|
| 262 | | - else |
|---|
| 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 { |
|---|
| 263 | 281 | err = wdd->ops->start(wdd); |
|---|
| 264 | | - if (err == 0) { |
|---|
| 265 | | - set_bit(WDOG_ACTIVE, &wdd->status); |
|---|
| 266 | | - wd_data->last_keepalive = started_at; |
|---|
| 267 | | - wd_data->last_hw_keepalive = started_at; |
|---|
| 268 | | - watchdog_update_worker(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 | + } |
|---|
| 269 | 288 | } |
|---|
| 270 | 289 | |
|---|
| 271 | 290 | return err; |
|---|
| .. | .. |
|---|
| 434 | 453 | |
|---|
| 435 | 454 | return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status)); |
|---|
| 436 | 455 | } |
|---|
| 437 | | -static DEVICE_ATTR_RO(nowayout); |
|---|
| 456 | + |
|---|
| 457 | +static ssize_t nowayout_store(struct device *dev, struct device_attribute *attr, |
|---|
| 458 | + const char *buf, size_t len) |
|---|
| 459 | +{ |
|---|
| 460 | + struct watchdog_device *wdd = dev_get_drvdata(dev); |
|---|
| 461 | + unsigned int value; |
|---|
| 462 | + int ret; |
|---|
| 463 | + |
|---|
| 464 | + ret = kstrtouint(buf, 0, &value); |
|---|
| 465 | + if (ret) |
|---|
| 466 | + return ret; |
|---|
| 467 | + if (value > 1) |
|---|
| 468 | + return -EINVAL; |
|---|
| 469 | + /* nowayout cannot be disabled once set */ |
|---|
| 470 | + if (test_bit(WDOG_NO_WAY_OUT, &wdd->status) && !value) |
|---|
| 471 | + return -EPERM; |
|---|
| 472 | + watchdog_set_nowayout(wdd, value); |
|---|
| 473 | + return len; |
|---|
| 474 | +} |
|---|
| 475 | +static DEVICE_ATTR_RW(nowayout); |
|---|
| 438 | 476 | |
|---|
| 439 | 477 | static ssize_t status_show(struct device *dev, struct device_attribute *attr, |
|---|
| 440 | 478 | char *buf) |
|---|
| .. | .. |
|---|
| 550 | 588 | static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr, |
|---|
| 551 | 589 | int n) |
|---|
| 552 | 590 | { |
|---|
| 553 | | - struct device *dev = container_of(kobj, struct device, kobj); |
|---|
| 591 | + struct device *dev = kobj_to_dev(kobj); |
|---|
| 554 | 592 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
|---|
| 555 | 593 | umode_t mode = attr->mode; |
|---|
| 556 | 594 | |
|---|
| .. | .. |
|---|
| 739 | 777 | err = watchdog_ping(wdd); |
|---|
| 740 | 778 | if (err < 0) |
|---|
| 741 | 779 | break; |
|---|
| 742 | | - /* fall through */ |
|---|
| 780 | + fallthrough; |
|---|
| 743 | 781 | case WDIOC_GETTIMEOUT: |
|---|
| 744 | 782 | /* timeout == 0 means that we don't know the timeout */ |
|---|
| 745 | 783 | if (wdd->timeout == 0) { |
|---|
| .. | .. |
|---|
| 823 | 861 | if (!hw_running) |
|---|
| 824 | 862 | get_device(&wd_data->dev); |
|---|
| 825 | 863 | |
|---|
| 864 | + /* |
|---|
| 865 | + * open_timeout only applies for the first open from |
|---|
| 866 | + * userspace. Set open_deadline to infinity so that the kernel |
|---|
| 867 | + * will take care of an always-running hardware watchdog in |
|---|
| 868 | + * case the device gets magic-closed or WDIOS_DISABLECARD is |
|---|
| 869 | + * applied. |
|---|
| 870 | + */ |
|---|
| 871 | + wd_data->open_deadline = KTIME_MAX; |
|---|
| 872 | + |
|---|
| 826 | 873 | /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ |
|---|
| 827 | | - return nonseekable_open(inode, file); |
|---|
| 874 | + return stream_open(inode, file); |
|---|
| 828 | 875 | |
|---|
| 829 | 876 | out_mod: |
|---|
| 830 | 877 | module_put(wd_data->wdd->ops->owner); |
|---|
| .. | .. |
|---|
| 870 | 917 | * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then |
|---|
| 871 | 918 | * watchdog_stop will fail. |
|---|
| 872 | 919 | */ |
|---|
| 873 | | - if (!test_bit(WDOG_ACTIVE, &wdd->status)) |
|---|
| 920 | + if (!watchdog_active(wdd)) |
|---|
| 874 | 921 | err = 0; |
|---|
| 875 | 922 | else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) || |
|---|
| 876 | 923 | !(wdd->info->options & WDIOF_MAGICCLOSE)) |
|---|
| .. | .. |
|---|
| 906 | 953 | .owner = THIS_MODULE, |
|---|
| 907 | 954 | .write = watchdog_write, |
|---|
| 908 | 955 | .unlocked_ioctl = watchdog_ioctl, |
|---|
| 956 | + .compat_ioctl = compat_ptr_ioctl, |
|---|
| 909 | 957 | .open = watchdog_open, |
|---|
| 910 | 958 | .release = watchdog_release, |
|---|
| 911 | 959 | }; |
|---|
| .. | .. |
|---|
| 959 | 1007 | dev_set_name(&wd_data->dev, "watchdog%d", wdd->id); |
|---|
| 960 | 1008 | |
|---|
| 961 | 1009 | kthread_init_work(&wd_data->work, watchdog_ping_work); |
|---|
| 962 | | - hrtimer_init(&wd_data->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
|---|
| 1010 | + hrtimer_init(&wd_data->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); |
|---|
| 963 | 1011 | wd_data->timer.function = watchdog_timer_expired; |
|---|
| 964 | 1012 | |
|---|
| 965 | 1013 | if (wdd->id == 0) { |
|---|
| .. | .. |
|---|
| 998 | 1046 | |
|---|
| 999 | 1047 | /* Record time of most recent heartbeat as 'just before now'. */ |
|---|
| 1000 | 1048 | wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1); |
|---|
| 1049 | + watchdog_set_open_deadline(wd_data); |
|---|
| 1001 | 1050 | |
|---|
| 1002 | 1051 | /* |
|---|
| 1003 | 1052 | * If the watchdog is running, prevent its driver from being unloaded, |
|---|
| .. | .. |
|---|
| 1007 | 1056 | __module_get(wdd->ops->owner); |
|---|
| 1008 | 1057 | get_device(&wd_data->dev); |
|---|
| 1009 | 1058 | if (handle_boot_enabled) |
|---|
| 1010 | | - hrtimer_start(&wd_data->timer, 0, HRTIMER_MODE_REL); |
|---|
| 1059 | + hrtimer_start(&wd_data->timer, 0, |
|---|
| 1060 | + HRTIMER_MODE_REL_HARD); |
|---|
| 1011 | 1061 | else |
|---|
| 1012 | 1062 | pr_info("watchdog%d running and kernel based pre-userspace handler disabled\n", |
|---|
| 1013 | 1063 | wdd->id); |
|---|
| .. | .. |
|---|
| 1089 | 1139 | } |
|---|
| 1090 | 1140 | |
|---|
| 1091 | 1141 | /* |
|---|
| 1142 | + * watchdog_set_last_hw_keepalive: set last HW keepalive time for watchdog |
|---|
| 1143 | + * @wdd: watchdog device |
|---|
| 1144 | + * @last_ping_ms: time since last HW heartbeat |
|---|
| 1145 | + * |
|---|
| 1146 | + * Adjusts the last known HW keepalive time for a watchdog timer. |
|---|
| 1147 | + * This is needed if the watchdog is already running when the probe |
|---|
| 1148 | + * function is called, and it can't be pinged immediately. This |
|---|
| 1149 | + * function must be called immediately after watchdog registration, |
|---|
| 1150 | + * and min_hw_heartbeat_ms must be set for this to be useful. |
|---|
| 1151 | + */ |
|---|
| 1152 | +int watchdog_set_last_hw_keepalive(struct watchdog_device *wdd, |
|---|
| 1153 | + unsigned int last_ping_ms) |
|---|
| 1154 | +{ |
|---|
| 1155 | + struct watchdog_core_data *wd_data; |
|---|
| 1156 | + ktime_t now; |
|---|
| 1157 | + |
|---|
| 1158 | + if (!wdd) |
|---|
| 1159 | + return -EINVAL; |
|---|
| 1160 | + |
|---|
| 1161 | + wd_data = wdd->wd_data; |
|---|
| 1162 | + |
|---|
| 1163 | + now = ktime_get(); |
|---|
| 1164 | + |
|---|
| 1165 | + wd_data->last_hw_keepalive = ktime_sub(now, ms_to_ktime(last_ping_ms)); |
|---|
| 1166 | + |
|---|
| 1167 | + if (watchdog_hw_running(wdd) && handle_boot_enabled) |
|---|
| 1168 | + return __watchdog_ping(wdd); |
|---|
| 1169 | + |
|---|
| 1170 | + return 0; |
|---|
| 1171 | +} |
|---|
| 1172 | +EXPORT_SYMBOL_GPL(watchdog_set_last_hw_keepalive); |
|---|
| 1173 | + |
|---|
| 1174 | +/* |
|---|
| 1092 | 1175 | * watchdog_dev_init: init dev part of watchdog core |
|---|
| 1093 | 1176 | * |
|---|
| 1094 | 1177 | * Allocate a range of chardev nodes to use for watchdog devices |
|---|
| .. | .. |
|---|
| 1097 | 1180 | int __init watchdog_dev_init(void) |
|---|
| 1098 | 1181 | { |
|---|
| 1099 | 1182 | int err; |
|---|
| 1100 | | - struct sched_param param = {.sched_priority = MAX_RT_PRIO - 1,}; |
|---|
| 1101 | 1183 | |
|---|
| 1102 | 1184 | watchdog_kworker = kthread_create_worker(0, "watchdogd"); |
|---|
| 1103 | 1185 | if (IS_ERR(watchdog_kworker)) { |
|---|
| 1104 | 1186 | pr_err("Failed to create watchdog kworker\n"); |
|---|
| 1105 | 1187 | return PTR_ERR(watchdog_kworker); |
|---|
| 1106 | 1188 | } |
|---|
| 1107 | | - sched_setscheduler(watchdog_kworker->task, SCHED_FIFO, ¶m); |
|---|
| 1189 | + sched_set_fifo(watchdog_kworker->task); |
|---|
| 1108 | 1190 | |
|---|
| 1109 | 1191 | err = class_register(&watchdog_class); |
|---|
| 1110 | 1192 | if (err < 0) { |
|---|
| .. | .. |
|---|
| 1144 | 1226 | MODULE_PARM_DESC(handle_boot_enabled, |
|---|
| 1145 | 1227 | "Watchdog core auto-updates boot enabled watchdogs before userspace takes over (default=" |
|---|
| 1146 | 1228 | __MODULE_STRING(IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED)) ")"); |
|---|
| 1229 | + |
|---|
| 1230 | +module_param(open_timeout, uint, 0644); |
|---|
| 1231 | +MODULE_PARM_DESC(open_timeout, |
|---|
| 1232 | + "Maximum time (in seconds, 0 means infinity) for userspace to take over a running watchdog (default=" |
|---|
| 1233 | + __MODULE_STRING(CONFIG_WATCHDOG_OPEN_TIMEOUT) ")"); |
|---|