0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0031
0032 #include <linux/cdev.h> /* For character device */
0033 #include <linux/errno.h> /* For the -ENODEV/... values */
0034 #include <linux/fs.h> /* For file operations */
0035 #include <linux/init.h> /* For __init/__exit/... */
0036 #include <linux/hrtimer.h> /* For hrtimers */
0037 #include <linux/kernel.h> /* For printk/panic/... */
0038 #include <linux/kthread.h> /* For kthread_work */
0039 #include <linux/miscdevice.h> /* For handling misc devices */
0040 #include <linux/module.h> /* For module stuff/... */
0041 #include <linux/mutex.h> /* For mutexes */
0042 #include <linux/slab.h> /* For memory functions */
0043 #include <linux/types.h> /* For standard types (like size_t) */
0044 #include <linux/watchdog.h> /* For watchdog specific items */
0045 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
0046
0047 #include "watchdog_core.h"
0048 #include "watchdog_pretimeout.h"
0049
0050
0051 static dev_t watchdog_devt;
0052
0053 static struct watchdog_core_data *old_wd_data;
0054
0055 static struct kthread_worker *watchdog_kworker;
0056
0057 static bool handle_boot_enabled =
0058 IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED);
0059
0060 static unsigned open_timeout = CONFIG_WATCHDOG_OPEN_TIMEOUT;
0061
0062 static bool watchdog_past_open_deadline(struct watchdog_core_data *data)
0063 {
0064 return ktime_after(ktime_get(), data->open_deadline);
0065 }
0066
0067 static void watchdog_set_open_deadline(struct watchdog_core_data *data)
0068 {
0069 data->open_deadline = open_timeout ?
0070 ktime_get() + ktime_set(open_timeout, 0) : KTIME_MAX;
0071 }
0072
0073 static inline bool watchdog_need_worker(struct watchdog_device *wdd)
0074 {
0075
0076 unsigned int hm = wdd->max_hw_heartbeat_ms;
0077 unsigned int t = wdd->timeout * 1000;
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092 return (hm && watchdog_active(wdd) && t > hm) ||
0093 (t && !watchdog_active(wdd) && watchdog_hw_running(wdd));
0094 }
0095
0096 static ktime_t watchdog_next_keepalive(struct watchdog_device *wdd)
0097 {
0098 struct watchdog_core_data *wd_data = wdd->wd_data;
0099 unsigned int timeout_ms = wdd->timeout * 1000;
0100 ktime_t keepalive_interval;
0101 ktime_t last_heartbeat, latest_heartbeat;
0102 ktime_t virt_timeout;
0103 unsigned int hw_heartbeat_ms;
0104
0105 if (watchdog_active(wdd))
0106 virt_timeout = ktime_add(wd_data->last_keepalive,
0107 ms_to_ktime(timeout_ms));
0108 else
0109 virt_timeout = wd_data->open_deadline;
0110
0111 hw_heartbeat_ms = min_not_zero(timeout_ms, wdd->max_hw_heartbeat_ms);
0112 keepalive_interval = ms_to_ktime(hw_heartbeat_ms / 2);
0113
0114
0115
0116
0117
0118
0119 last_heartbeat = ktime_sub(virt_timeout, ms_to_ktime(hw_heartbeat_ms));
0120 latest_heartbeat = ktime_sub(last_heartbeat, ktime_get());
0121 if (ktime_before(latest_heartbeat, keepalive_interval))
0122 return latest_heartbeat;
0123 return keepalive_interval;
0124 }
0125
0126 static inline void watchdog_update_worker(struct watchdog_device *wdd)
0127 {
0128 struct watchdog_core_data *wd_data = wdd->wd_data;
0129
0130 if (watchdog_need_worker(wdd)) {
0131 ktime_t t = watchdog_next_keepalive(wdd);
0132
0133 if (t > 0)
0134 hrtimer_start(&wd_data->timer, t,
0135 HRTIMER_MODE_REL_HARD);
0136 } else {
0137 hrtimer_cancel(&wd_data->timer);
0138 }
0139 }
0140
0141 static int __watchdog_ping(struct watchdog_device *wdd)
0142 {
0143 struct watchdog_core_data *wd_data = wdd->wd_data;
0144 ktime_t earliest_keepalive, now;
0145 int err;
0146
0147 earliest_keepalive = ktime_add(wd_data->last_hw_keepalive,
0148 ms_to_ktime(wdd->min_hw_heartbeat_ms));
0149 now = ktime_get();
0150
0151 if (ktime_after(earliest_keepalive, now)) {
0152 hrtimer_start(&wd_data->timer,
0153 ktime_sub(earliest_keepalive, now),
0154 HRTIMER_MODE_REL_HARD);
0155 return 0;
0156 }
0157
0158 wd_data->last_hw_keepalive = now;
0159
0160 if (wdd->ops->ping)
0161 err = wdd->ops->ping(wdd);
0162 else
0163 err = wdd->ops->start(wdd);
0164
0165 if (err == 0)
0166 watchdog_hrtimer_pretimeout_start(wdd);
0167
0168 watchdog_update_worker(wdd);
0169
0170 return err;
0171 }
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185 static int watchdog_ping(struct watchdog_device *wdd)
0186 {
0187 struct watchdog_core_data *wd_data = wdd->wd_data;
0188
0189 if (!watchdog_active(wdd) && !watchdog_hw_running(wdd))
0190 return 0;
0191
0192 set_bit(_WDOG_KEEPALIVE, &wd_data->status);
0193
0194 wd_data->last_keepalive = ktime_get();
0195 return __watchdog_ping(wdd);
0196 }
0197
0198 static bool watchdog_worker_should_ping(struct watchdog_core_data *wd_data)
0199 {
0200 struct watchdog_device *wdd = wd_data->wdd;
0201
0202 if (!wdd)
0203 return false;
0204
0205 if (watchdog_active(wdd))
0206 return true;
0207
0208 return watchdog_hw_running(wdd) && !watchdog_past_open_deadline(wd_data);
0209 }
0210
0211 static void watchdog_ping_work(struct kthread_work *work)
0212 {
0213 struct watchdog_core_data *wd_data;
0214
0215 wd_data = container_of(work, struct watchdog_core_data, work);
0216
0217 mutex_lock(&wd_data->lock);
0218 if (watchdog_worker_should_ping(wd_data))
0219 __watchdog_ping(wd_data->wdd);
0220 mutex_unlock(&wd_data->lock);
0221 }
0222
0223 static enum hrtimer_restart watchdog_timer_expired(struct hrtimer *timer)
0224 {
0225 struct watchdog_core_data *wd_data;
0226
0227 wd_data = container_of(timer, struct watchdog_core_data, timer);
0228
0229 kthread_queue_work(watchdog_kworker, &wd_data->work);
0230 return HRTIMER_NORESTART;
0231 }
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 static int watchdog_start(struct watchdog_device *wdd)
0243 {
0244 struct watchdog_core_data *wd_data = wdd->wd_data;
0245 ktime_t started_at;
0246 int err;
0247
0248 if (watchdog_active(wdd))
0249 return 0;
0250
0251 set_bit(_WDOG_KEEPALIVE, &wd_data->status);
0252
0253 started_at = ktime_get();
0254 if (watchdog_hw_running(wdd) && wdd->ops->ping) {
0255 err = __watchdog_ping(wdd);
0256 if (err == 0) {
0257 set_bit(WDOG_ACTIVE, &wdd->status);
0258 watchdog_hrtimer_pretimeout_start(wdd);
0259 }
0260 } else {
0261 err = wdd->ops->start(wdd);
0262 if (err == 0) {
0263 set_bit(WDOG_ACTIVE, &wdd->status);
0264 wd_data->last_keepalive = started_at;
0265 wd_data->last_hw_keepalive = started_at;
0266 watchdog_update_worker(wdd);
0267 watchdog_hrtimer_pretimeout_start(wdd);
0268 }
0269 }
0270
0271 return err;
0272 }
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284 static int watchdog_stop(struct watchdog_device *wdd)
0285 {
0286 int err = 0;
0287
0288 if (!watchdog_active(wdd))
0289 return 0;
0290
0291 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
0292 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
0293 wdd->id);
0294 return -EBUSY;
0295 }
0296
0297 if (wdd->ops->stop) {
0298 clear_bit(WDOG_HW_RUNNING, &wdd->status);
0299 err = wdd->ops->stop(wdd);
0300 } else {
0301 set_bit(WDOG_HW_RUNNING, &wdd->status);
0302 }
0303
0304 if (err == 0) {
0305 clear_bit(WDOG_ACTIVE, &wdd->status);
0306 watchdog_update_worker(wdd);
0307 watchdog_hrtimer_pretimeout_stop(wdd);
0308 }
0309
0310 return err;
0311 }
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322 static unsigned int watchdog_get_status(struct watchdog_device *wdd)
0323 {
0324 struct watchdog_core_data *wd_data = wdd->wd_data;
0325 unsigned int status;
0326
0327 if (wdd->ops->status)
0328 status = wdd->ops->status(wdd);
0329 else
0330 status = wdd->bootstatus & (WDIOF_CARDRESET |
0331 WDIOF_OVERHEAT |
0332 WDIOF_FANFAULT |
0333 WDIOF_EXTERN1 |
0334 WDIOF_EXTERN2 |
0335 WDIOF_POWERUNDER |
0336 WDIOF_POWEROVER);
0337
0338 if (test_bit(_WDOG_ALLOW_RELEASE, &wd_data->status))
0339 status |= WDIOF_MAGICCLOSE;
0340
0341 if (test_and_clear_bit(_WDOG_KEEPALIVE, &wd_data->status))
0342 status |= WDIOF_KEEPALIVEPING;
0343
0344 if (IS_ENABLED(CONFIG_WATCHDOG_HRTIMER_PRETIMEOUT))
0345 status |= WDIOF_PRETIMEOUT;
0346
0347 return status;
0348 }
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359 static int watchdog_set_timeout(struct watchdog_device *wdd,
0360 unsigned int timeout)
0361 {
0362 int err = 0;
0363
0364 if (!(wdd->info->options & WDIOF_SETTIMEOUT))
0365 return -EOPNOTSUPP;
0366
0367 if (watchdog_timeout_invalid(wdd, timeout))
0368 return -EINVAL;
0369
0370 if (wdd->ops->set_timeout) {
0371 err = wdd->ops->set_timeout(wdd, timeout);
0372 } else {
0373 wdd->timeout = timeout;
0374
0375 if (wdd->pretimeout >= wdd->timeout)
0376 wdd->pretimeout = 0;
0377 }
0378
0379 watchdog_update_worker(wdd);
0380
0381 return err;
0382 }
0383
0384
0385
0386
0387
0388
0389
0390
0391 static int watchdog_set_pretimeout(struct watchdog_device *wdd,
0392 unsigned int timeout)
0393 {
0394 int err = 0;
0395
0396 if (!watchdog_have_pretimeout(wdd))
0397 return -EOPNOTSUPP;
0398
0399 if (watchdog_pretimeout_invalid(wdd, timeout))
0400 return -EINVAL;
0401
0402 if (wdd->ops->set_pretimeout && (wdd->info->options & WDIOF_PRETIMEOUT))
0403 err = wdd->ops->set_pretimeout(wdd, timeout);
0404 else
0405 wdd->pretimeout = timeout;
0406
0407 return err;
0408 }
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420 static int watchdog_get_timeleft(struct watchdog_device *wdd,
0421 unsigned int *timeleft)
0422 {
0423 *timeleft = 0;
0424
0425 if (!wdd->ops->get_timeleft)
0426 return -EOPNOTSUPP;
0427
0428 *timeleft = wdd->ops->get_timeleft(wdd);
0429
0430 return 0;
0431 }
0432
0433 #ifdef CONFIG_WATCHDOG_SYSFS
0434 static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
0435 char *buf)
0436 {
0437 struct watchdog_device *wdd = dev_get_drvdata(dev);
0438
0439 return sysfs_emit(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT,
0440 &wdd->status));
0441 }
0442
0443 static ssize_t nowayout_store(struct device *dev, struct device_attribute *attr,
0444 const char *buf, size_t len)
0445 {
0446 struct watchdog_device *wdd = dev_get_drvdata(dev);
0447 unsigned int value;
0448 int ret;
0449
0450 ret = kstrtouint(buf, 0, &value);
0451 if (ret)
0452 return ret;
0453 if (value > 1)
0454 return -EINVAL;
0455
0456 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status) && !value)
0457 return -EPERM;
0458 watchdog_set_nowayout(wdd, value);
0459 return len;
0460 }
0461 static DEVICE_ATTR_RW(nowayout);
0462
0463 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
0464 char *buf)
0465 {
0466 struct watchdog_device *wdd = dev_get_drvdata(dev);
0467 struct watchdog_core_data *wd_data = wdd->wd_data;
0468 unsigned int status;
0469
0470 mutex_lock(&wd_data->lock);
0471 status = watchdog_get_status(wdd);
0472 mutex_unlock(&wd_data->lock);
0473
0474 return sysfs_emit(buf, "0x%x\n", status);
0475 }
0476 static DEVICE_ATTR_RO(status);
0477
0478 static ssize_t bootstatus_show(struct device *dev,
0479 struct device_attribute *attr, char *buf)
0480 {
0481 struct watchdog_device *wdd = dev_get_drvdata(dev);
0482
0483 return sysfs_emit(buf, "%u\n", wdd->bootstatus);
0484 }
0485 static DEVICE_ATTR_RO(bootstatus);
0486
0487 static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr,
0488 char *buf)
0489 {
0490 struct watchdog_device *wdd = dev_get_drvdata(dev);
0491 struct watchdog_core_data *wd_data = wdd->wd_data;
0492 ssize_t status;
0493 unsigned int val;
0494
0495 mutex_lock(&wd_data->lock);
0496 status = watchdog_get_timeleft(wdd, &val);
0497 mutex_unlock(&wd_data->lock);
0498 if (!status)
0499 status = sysfs_emit(buf, "%u\n", val);
0500
0501 return status;
0502 }
0503 static DEVICE_ATTR_RO(timeleft);
0504
0505 static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
0506 char *buf)
0507 {
0508 struct watchdog_device *wdd = dev_get_drvdata(dev);
0509
0510 return sysfs_emit(buf, "%u\n", wdd->timeout);
0511 }
0512 static DEVICE_ATTR_RO(timeout);
0513
0514 static ssize_t min_timeout_show(struct device *dev,
0515 struct device_attribute *attr, char *buf)
0516 {
0517 struct watchdog_device *wdd = dev_get_drvdata(dev);
0518
0519 return sysfs_emit(buf, "%u\n", wdd->min_timeout);
0520 }
0521 static DEVICE_ATTR_RO(min_timeout);
0522
0523 static ssize_t max_timeout_show(struct device *dev,
0524 struct device_attribute *attr, char *buf)
0525 {
0526 struct watchdog_device *wdd = dev_get_drvdata(dev);
0527
0528 return sysfs_emit(buf, "%u\n", wdd->max_timeout);
0529 }
0530 static DEVICE_ATTR_RO(max_timeout);
0531
0532 static ssize_t pretimeout_show(struct device *dev,
0533 struct device_attribute *attr, char *buf)
0534 {
0535 struct watchdog_device *wdd = dev_get_drvdata(dev);
0536
0537 return sysfs_emit(buf, "%u\n", wdd->pretimeout);
0538 }
0539 static DEVICE_ATTR_RO(pretimeout);
0540
0541 static ssize_t identity_show(struct device *dev, struct device_attribute *attr,
0542 char *buf)
0543 {
0544 struct watchdog_device *wdd = dev_get_drvdata(dev);
0545
0546 return sysfs_emit(buf, "%s\n", wdd->info->identity);
0547 }
0548 static DEVICE_ATTR_RO(identity);
0549
0550 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
0551 char *buf)
0552 {
0553 struct watchdog_device *wdd = dev_get_drvdata(dev);
0554
0555 if (watchdog_active(wdd))
0556 return sysfs_emit(buf, "active\n");
0557
0558 return sysfs_emit(buf, "inactive\n");
0559 }
0560 static DEVICE_ATTR_RO(state);
0561
0562 static ssize_t pretimeout_available_governors_show(struct device *dev,
0563 struct device_attribute *attr, char *buf)
0564 {
0565 return watchdog_pretimeout_available_governors_get(buf);
0566 }
0567 static DEVICE_ATTR_RO(pretimeout_available_governors);
0568
0569 static ssize_t pretimeout_governor_show(struct device *dev,
0570 struct device_attribute *attr,
0571 char *buf)
0572 {
0573 struct watchdog_device *wdd = dev_get_drvdata(dev);
0574
0575 return watchdog_pretimeout_governor_get(wdd, buf);
0576 }
0577
0578 static ssize_t pretimeout_governor_store(struct device *dev,
0579 struct device_attribute *attr,
0580 const char *buf, size_t count)
0581 {
0582 struct watchdog_device *wdd = dev_get_drvdata(dev);
0583 int ret = watchdog_pretimeout_governor_set(wdd, buf);
0584
0585 if (!ret)
0586 ret = count;
0587
0588 return ret;
0589 }
0590 static DEVICE_ATTR_RW(pretimeout_governor);
0591
0592 static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr,
0593 int n)
0594 {
0595 struct device *dev = kobj_to_dev(kobj);
0596 struct watchdog_device *wdd = dev_get_drvdata(dev);
0597 umode_t mode = attr->mode;
0598
0599 if (attr == &dev_attr_timeleft.attr && !wdd->ops->get_timeleft)
0600 mode = 0;
0601 else if (attr == &dev_attr_pretimeout.attr && !watchdog_have_pretimeout(wdd))
0602 mode = 0;
0603 else if ((attr == &dev_attr_pretimeout_governor.attr ||
0604 attr == &dev_attr_pretimeout_available_governors.attr) &&
0605 (!watchdog_have_pretimeout(wdd) || !IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV)))
0606 mode = 0;
0607
0608 return mode;
0609 }
0610 static struct attribute *wdt_attrs[] = {
0611 &dev_attr_state.attr,
0612 &dev_attr_identity.attr,
0613 &dev_attr_timeout.attr,
0614 &dev_attr_min_timeout.attr,
0615 &dev_attr_max_timeout.attr,
0616 &dev_attr_pretimeout.attr,
0617 &dev_attr_timeleft.attr,
0618 &dev_attr_bootstatus.attr,
0619 &dev_attr_status.attr,
0620 &dev_attr_nowayout.attr,
0621 &dev_attr_pretimeout_governor.attr,
0622 &dev_attr_pretimeout_available_governors.attr,
0623 NULL,
0624 };
0625
0626 static const struct attribute_group wdt_group = {
0627 .attrs = wdt_attrs,
0628 .is_visible = wdt_is_visible,
0629 };
0630 __ATTRIBUTE_GROUPS(wdt);
0631 #else
0632 #define wdt_groups NULL
0633 #endif
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645 static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
0646 unsigned long arg)
0647 {
0648 if (!wdd->ops->ioctl)
0649 return -ENOIOCTLCMD;
0650
0651 return wdd->ops->ioctl(wdd, cmd, arg);
0652 }
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667 static ssize_t watchdog_write(struct file *file, const char __user *data,
0668 size_t len, loff_t *ppos)
0669 {
0670 struct watchdog_core_data *wd_data = file->private_data;
0671 struct watchdog_device *wdd;
0672 int err;
0673 size_t i;
0674 char c;
0675
0676 if (len == 0)
0677 return 0;
0678
0679
0680
0681
0682
0683 clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
0684
0685
0686 for (i = 0; i != len; i++) {
0687 if (get_user(c, data + i))
0688 return -EFAULT;
0689 if (c == 'V')
0690 set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
0691 }
0692
0693
0694
0695 err = -ENODEV;
0696 mutex_lock(&wd_data->lock);
0697 wdd = wd_data->wdd;
0698 if (wdd)
0699 err = watchdog_ping(wdd);
0700 mutex_unlock(&wd_data->lock);
0701
0702 if (err < 0)
0703 return err;
0704
0705 return len;
0706 }
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720 static long watchdog_ioctl(struct file *file, unsigned int cmd,
0721 unsigned long arg)
0722 {
0723 struct watchdog_core_data *wd_data = file->private_data;
0724 void __user *argp = (void __user *)arg;
0725 struct watchdog_device *wdd;
0726 int __user *p = argp;
0727 unsigned int val;
0728 int err;
0729
0730 mutex_lock(&wd_data->lock);
0731
0732 wdd = wd_data->wdd;
0733 if (!wdd) {
0734 err = -ENODEV;
0735 goto out_ioctl;
0736 }
0737
0738 err = watchdog_ioctl_op(wdd, cmd, arg);
0739 if (err != -ENOIOCTLCMD)
0740 goto out_ioctl;
0741
0742 switch (cmd) {
0743 case WDIOC_GETSUPPORT:
0744 err = copy_to_user(argp, wdd->info,
0745 sizeof(struct watchdog_info)) ? -EFAULT : 0;
0746 break;
0747 case WDIOC_GETSTATUS:
0748 val = watchdog_get_status(wdd);
0749 err = put_user(val, p);
0750 break;
0751 case WDIOC_GETBOOTSTATUS:
0752 err = put_user(wdd->bootstatus, p);
0753 break;
0754 case WDIOC_SETOPTIONS:
0755 if (get_user(val, p)) {
0756 err = -EFAULT;
0757 break;
0758 }
0759 if (val & WDIOS_DISABLECARD) {
0760 err = watchdog_stop(wdd);
0761 if (err < 0)
0762 break;
0763 }
0764 if (val & WDIOS_ENABLECARD)
0765 err = watchdog_start(wdd);
0766 break;
0767 case WDIOC_KEEPALIVE:
0768 if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
0769 err = -EOPNOTSUPP;
0770 break;
0771 }
0772 err = watchdog_ping(wdd);
0773 break;
0774 case WDIOC_SETTIMEOUT:
0775 if (get_user(val, p)) {
0776 err = -EFAULT;
0777 break;
0778 }
0779 err = watchdog_set_timeout(wdd, val);
0780 if (err < 0)
0781 break;
0782
0783
0784
0785 err = watchdog_ping(wdd);
0786 if (err < 0)
0787 break;
0788 fallthrough;
0789 case WDIOC_GETTIMEOUT:
0790
0791 if (wdd->timeout == 0) {
0792 err = -EOPNOTSUPP;
0793 break;
0794 }
0795 err = put_user(wdd->timeout, p);
0796 break;
0797 case WDIOC_GETTIMELEFT:
0798 err = watchdog_get_timeleft(wdd, &val);
0799 if (err < 0)
0800 break;
0801 err = put_user(val, p);
0802 break;
0803 case WDIOC_SETPRETIMEOUT:
0804 if (get_user(val, p)) {
0805 err = -EFAULT;
0806 break;
0807 }
0808 err = watchdog_set_pretimeout(wdd, val);
0809 break;
0810 case WDIOC_GETPRETIMEOUT:
0811 err = put_user(wdd->pretimeout, p);
0812 break;
0813 default:
0814 err = -ENOTTY;
0815 break;
0816 }
0817
0818 out_ioctl:
0819 mutex_unlock(&wd_data->lock);
0820 return err;
0821 }
0822
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832
0833
0834 static int watchdog_open(struct inode *inode, struct file *file)
0835 {
0836 struct watchdog_core_data *wd_data;
0837 struct watchdog_device *wdd;
0838 bool hw_running;
0839 int err;
0840
0841
0842 if (imajor(inode) == MISC_MAJOR)
0843 wd_data = old_wd_data;
0844 else
0845 wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
0846 cdev);
0847
0848
0849 if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
0850 return -EBUSY;
0851
0852 wdd = wd_data->wdd;
0853
0854
0855
0856
0857
0858 hw_running = watchdog_hw_running(wdd);
0859 if (!hw_running && !try_module_get(wdd->ops->owner)) {
0860 err = -EBUSY;
0861 goto out_clear;
0862 }
0863
0864 err = watchdog_start(wdd);
0865 if (err < 0)
0866 goto out_mod;
0867
0868 file->private_data = wd_data;
0869
0870 if (!hw_running)
0871 get_device(&wd_data->dev);
0872
0873
0874
0875
0876
0877
0878
0879
0880 wd_data->open_deadline = KTIME_MAX;
0881
0882
0883 return stream_open(inode, file);
0884
0885 out_mod:
0886 module_put(wd_data->wdd->ops->owner);
0887 out_clear:
0888 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
0889 return err;
0890 }
0891
0892 static void watchdog_core_data_release(struct device *dev)
0893 {
0894 struct watchdog_core_data *wd_data;
0895
0896 wd_data = container_of(dev, struct watchdog_core_data, dev);
0897
0898 kfree(wd_data);
0899 }
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912 static int watchdog_release(struct inode *inode, struct file *file)
0913 {
0914 struct watchdog_core_data *wd_data = file->private_data;
0915 struct watchdog_device *wdd;
0916 int err = -EBUSY;
0917 bool running;
0918
0919 mutex_lock(&wd_data->lock);
0920
0921 wdd = wd_data->wdd;
0922 if (!wdd)
0923 goto done;
0924
0925
0926
0927
0928
0929
0930 if (!watchdog_active(wdd))
0931 err = 0;
0932 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
0933 !(wdd->info->options & WDIOF_MAGICCLOSE))
0934 err = watchdog_stop(wdd);
0935
0936
0937 if (err < 0) {
0938 pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
0939 watchdog_ping(wdd);
0940 }
0941
0942 watchdog_update_worker(wdd);
0943
0944
0945 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
0946
0947 done:
0948 running = wdd && watchdog_hw_running(wdd);
0949 mutex_unlock(&wd_data->lock);
0950
0951
0952
0953
0954
0955 if (!running) {
0956 module_put(wd_data->cdev.owner);
0957 put_device(&wd_data->dev);
0958 }
0959 return 0;
0960 }
0961
0962 static const struct file_operations watchdog_fops = {
0963 .owner = THIS_MODULE,
0964 .write = watchdog_write,
0965 .unlocked_ioctl = watchdog_ioctl,
0966 .compat_ioctl = compat_ptr_ioctl,
0967 .open = watchdog_open,
0968 .release = watchdog_release,
0969 };
0970
0971 static struct miscdevice watchdog_miscdev = {
0972 .minor = WATCHDOG_MINOR,
0973 .name = "watchdog",
0974 .fops = &watchdog_fops,
0975 };
0976
0977 static struct class watchdog_class = {
0978 .name = "watchdog",
0979 .owner = THIS_MODULE,
0980 .dev_groups = wdt_groups,
0981 };
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991
0992
0993 static int watchdog_cdev_register(struct watchdog_device *wdd)
0994 {
0995 struct watchdog_core_data *wd_data;
0996 int err;
0997
0998 wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
0999 if (!wd_data)
1000 return -ENOMEM;
1001 mutex_init(&wd_data->lock);
1002
1003 wd_data->wdd = wdd;
1004 wdd->wd_data = wd_data;
1005
1006 if (IS_ERR_OR_NULL(watchdog_kworker)) {
1007 kfree(wd_data);
1008 return -ENODEV;
1009 }
1010
1011 device_initialize(&wd_data->dev);
1012 wd_data->dev.devt = MKDEV(MAJOR(watchdog_devt), wdd->id);
1013 wd_data->dev.class = &watchdog_class;
1014 wd_data->dev.parent = wdd->parent;
1015 wd_data->dev.groups = wdd->groups;
1016 wd_data->dev.release = watchdog_core_data_release;
1017 dev_set_drvdata(&wd_data->dev, wdd);
1018 dev_set_name(&wd_data->dev, "watchdog%d", wdd->id);
1019
1020 kthread_init_work(&wd_data->work, watchdog_ping_work);
1021 hrtimer_init(&wd_data->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
1022 wd_data->timer.function = watchdog_timer_expired;
1023 watchdog_hrtimer_pretimeout_init(wdd);
1024
1025 if (wdd->id == 0) {
1026 old_wd_data = wd_data;
1027 watchdog_miscdev.parent = wdd->parent;
1028 err = misc_register(&watchdog_miscdev);
1029 if (err != 0) {
1030 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
1031 wdd->info->identity, WATCHDOG_MINOR, err);
1032 if (err == -EBUSY)
1033 pr_err("%s: a legacy watchdog module is probably present.\n",
1034 wdd->info->identity);
1035 old_wd_data = NULL;
1036 put_device(&wd_data->dev);
1037 return err;
1038 }
1039 }
1040
1041
1042 cdev_init(&wd_data->cdev, &watchdog_fops);
1043
1044
1045 err = cdev_device_add(&wd_data->cdev, &wd_data->dev);
1046 if (err) {
1047 pr_err("watchdog%d unable to add device %d:%d\n",
1048 wdd->id, MAJOR(watchdog_devt), wdd->id);
1049 if (wdd->id == 0) {
1050 misc_deregister(&watchdog_miscdev);
1051 old_wd_data = NULL;
1052 put_device(&wd_data->dev);
1053 }
1054 return err;
1055 }
1056
1057 wd_data->cdev.owner = wdd->ops->owner;
1058
1059
1060 wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1);
1061 watchdog_set_open_deadline(wd_data);
1062
1063
1064
1065
1066
1067 if (watchdog_hw_running(wdd)) {
1068 __module_get(wdd->ops->owner);
1069 get_device(&wd_data->dev);
1070 if (handle_boot_enabled)
1071 hrtimer_start(&wd_data->timer, 0,
1072 HRTIMER_MODE_REL_HARD);
1073 else
1074 pr_info("watchdog%d running and kernel based pre-userspace handler disabled\n",
1075 wdd->id);
1076 }
1077
1078 return 0;
1079 }
1080
1081
1082
1083
1084
1085
1086
1087
1088 static void watchdog_cdev_unregister(struct watchdog_device *wdd)
1089 {
1090 struct watchdog_core_data *wd_data = wdd->wd_data;
1091
1092 cdev_device_del(&wd_data->cdev, &wd_data->dev);
1093 if (wdd->id == 0) {
1094 misc_deregister(&watchdog_miscdev);
1095 old_wd_data = NULL;
1096 }
1097
1098 if (watchdog_active(wdd) &&
1099 test_bit(WDOG_STOP_ON_UNREGISTER, &wdd->status)) {
1100 watchdog_stop(wdd);
1101 }
1102
1103 watchdog_hrtimer_pretimeout_stop(wdd);
1104
1105 mutex_lock(&wd_data->lock);
1106 wd_data->wdd = NULL;
1107 wdd->wd_data = NULL;
1108 mutex_unlock(&wd_data->lock);
1109
1110 hrtimer_cancel(&wd_data->timer);
1111 kthread_cancel_work_sync(&wd_data->work);
1112
1113 put_device(&wd_data->dev);
1114 }
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126 int watchdog_dev_register(struct watchdog_device *wdd)
1127 {
1128 int ret;
1129
1130 ret = watchdog_cdev_register(wdd);
1131 if (ret)
1132 return ret;
1133
1134 ret = watchdog_register_pretimeout(wdd);
1135 if (ret)
1136 watchdog_cdev_unregister(wdd);
1137
1138 return ret;
1139 }
1140
1141
1142
1143
1144
1145
1146
1147
1148 void watchdog_dev_unregister(struct watchdog_device *wdd)
1149 {
1150 watchdog_unregister_pretimeout(wdd);
1151 watchdog_cdev_unregister(wdd);
1152 }
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167 int watchdog_set_last_hw_keepalive(struct watchdog_device *wdd,
1168 unsigned int last_ping_ms)
1169 {
1170 struct watchdog_core_data *wd_data;
1171 ktime_t now;
1172
1173 if (!wdd)
1174 return -EINVAL;
1175
1176 wd_data = wdd->wd_data;
1177
1178 now = ktime_get();
1179
1180 wd_data->last_hw_keepalive = ktime_sub(now, ms_to_ktime(last_ping_ms));
1181
1182 if (watchdog_hw_running(wdd) && handle_boot_enabled)
1183 return __watchdog_ping(wdd);
1184
1185 return 0;
1186 }
1187 EXPORT_SYMBOL_GPL(watchdog_set_last_hw_keepalive);
1188
1189
1190
1191
1192
1193
1194
1195
1196 int __init watchdog_dev_init(void)
1197 {
1198 int err;
1199
1200 watchdog_kworker = kthread_create_worker(0, "watchdogd");
1201 if (IS_ERR(watchdog_kworker)) {
1202 pr_err("Failed to create watchdog kworker\n");
1203 return PTR_ERR(watchdog_kworker);
1204 }
1205 sched_set_fifo(watchdog_kworker->task);
1206
1207 err = class_register(&watchdog_class);
1208 if (err < 0) {
1209 pr_err("couldn't register class\n");
1210 goto err_register;
1211 }
1212
1213 err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
1214 if (err < 0) {
1215 pr_err("watchdog: unable to allocate char dev region\n");
1216 goto err_alloc;
1217 }
1218
1219 return 0;
1220
1221 err_alloc:
1222 class_unregister(&watchdog_class);
1223 err_register:
1224 kthread_destroy_worker(watchdog_kworker);
1225 return err;
1226 }
1227
1228
1229
1230
1231
1232
1233 void __exit watchdog_dev_exit(void)
1234 {
1235 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
1236 class_unregister(&watchdog_class);
1237 kthread_destroy_worker(watchdog_kworker);
1238 }
1239
1240 int watchdog_dev_suspend(struct watchdog_device *wdd)
1241 {
1242 struct watchdog_core_data *wd_data = wdd->wd_data;
1243 int ret = 0;
1244
1245 if (!wdd->wd_data)
1246 return -ENODEV;
1247
1248
1249 mutex_lock(&wd_data->lock);
1250 if (watchdog_worker_should_ping(wd_data))
1251 ret = __watchdog_ping(wd_data->wdd);
1252 mutex_unlock(&wd_data->lock);
1253
1254 if (ret)
1255 return ret;
1256
1257
1258
1259
1260
1261 hrtimer_cancel(&wd_data->timer);
1262 kthread_cancel_work_sync(&wd_data->work);
1263
1264 return 0;
1265 }
1266
1267 int watchdog_dev_resume(struct watchdog_device *wdd)
1268 {
1269 struct watchdog_core_data *wd_data = wdd->wd_data;
1270 int ret = 0;
1271
1272 if (!wdd->wd_data)
1273 return -ENODEV;
1274
1275
1276
1277
1278
1279 mutex_lock(&wd_data->lock);
1280 if (watchdog_worker_should_ping(wd_data))
1281 ret = __watchdog_ping(wd_data->wdd);
1282 mutex_unlock(&wd_data->lock);
1283
1284 return ret;
1285 }
1286
1287 module_param(handle_boot_enabled, bool, 0444);
1288 MODULE_PARM_DESC(handle_boot_enabled,
1289 "Watchdog core auto-updates boot enabled watchdogs before userspace takes over (default="
1290 __MODULE_STRING(IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED)) ")");
1291
1292 module_param(open_timeout, uint, 0644);
1293 MODULE_PARM_DESC(open_timeout,
1294 "Maximum time (in seconds, 0 means infinity) for userspace to take over a running watchdog (default="
1295 __MODULE_STRING(CONFIG_WATCHDOG_OPEN_TIMEOUT) ")");