Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

watchdog: softdog: fire watchdog even if softirqs do not get to run

Checking for timer expiration is done from the softirq TIMER_SOFTIRQ.

Since commit 4cd13c21b207 ("softirq: Let ksoftirqd do its job"),
pending softirqs are no longer always handled immediately, instead,
if there are pending softirqs, and ksoftirqd is in state TASK_RUNNING,
the handling of the softirqs are deferred, and are instead supposed
to be handled by ksoftirqd, when ksoftirqd gets scheduled.

If a user space process with a real-time policy starts to misbehave
by never relinquishing the CPU while ksoftirqd is in state TASK_RUNNING,
what will happen is that all softirqs will get deferred, while ksoftirqd,
which is supposed to handle the deferred softirqs, will never get to run.

To make sure that the watchdog is able to fire even when we do not get
to run softirqs, replace the timers with hrtimers.

Signed-off-by: Niklas Cassel <niklas.cassel@axis.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

authored by

Niklas Cassel and committed by
Guenter Roeck
8d5755b3 ed4a9eca

+27 -17
+27 -17
drivers/watchdog/softdog.c
··· 21 21 22 22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 23 23 24 + #include <linux/hrtimer.h> 24 25 #include <linux/init.h> 25 - #include <linux/jiffies.h> 26 26 #include <linux/kernel.h> 27 27 #include <linux/module.h> 28 28 #include <linux/moduleparam.h> 29 29 #include <linux/reboot.h> 30 - #include <linux/timer.h> 31 30 #include <linux/types.h> 32 31 #include <linux/watchdog.h> 33 32 ··· 53 54 MODULE_PARM_DESC(soft_panic, 54 55 "Softdog action, set to 1 to panic, 0 to reboot (default=0)"); 55 56 56 - static void softdog_fire(unsigned long data) 57 + static struct hrtimer softdog_ticktock; 58 + static struct hrtimer softdog_preticktock; 59 + 60 + static enum hrtimer_restart softdog_fire(struct hrtimer *timer) 57 61 { 58 62 module_put(THIS_MODULE); 59 63 if (soft_noboot) { ··· 69 67 emergency_restart(); 70 68 pr_crit("Reboot didn't ?????\n"); 71 69 } 72 - } 73 70 74 - static struct timer_list softdog_ticktock = 75 - TIMER_INITIALIZER(softdog_fire, 0, 0); 71 + return HRTIMER_NORESTART; 72 + } 76 73 77 74 static struct watchdog_device softdog_dev; 78 75 79 - static void softdog_pretimeout(unsigned long data) 76 + static enum hrtimer_restart softdog_pretimeout(struct hrtimer *timer) 80 77 { 81 78 watchdog_notify_pretimeout(&softdog_dev); 82 - } 83 79 84 - static struct timer_list softdog_preticktock = 85 - TIMER_INITIALIZER(softdog_pretimeout, 0, 0); 80 + return HRTIMER_NORESTART; 81 + } 86 82 87 83 static int softdog_ping(struct watchdog_device *w) 88 84 { 89 - if (!mod_timer(&softdog_ticktock, jiffies + (w->timeout * HZ))) 85 + if (!hrtimer_active(&softdog_ticktock)) 90 86 __module_get(THIS_MODULE); 87 + hrtimer_start(&softdog_ticktock, ktime_set(w->timeout, 0), 88 + HRTIMER_MODE_REL); 91 89 92 90 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) { 93 91 if (w->pretimeout) 94 - mod_timer(&softdog_preticktock, jiffies + 95 - (w->timeout - w->pretimeout) * HZ); 92 + hrtimer_start(&softdog_preticktock, 93 + ktime_set(w->timeout - w->pretimeout, 0), 94 + HRTIMER_MODE_REL); 96 95 else 97 - del_timer(&softdog_preticktock); 96 + hrtimer_cancel(&softdog_preticktock); 98 97 } 99 98 100 99 return 0; ··· 103 100 104 101 static int softdog_stop(struct watchdog_device *w) 105 102 { 106 - if (del_timer(&softdog_ticktock)) 103 + if (hrtimer_cancel(&softdog_ticktock)) 107 104 module_put(THIS_MODULE); 108 105 109 106 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) 110 - del_timer(&softdog_preticktock); 107 + hrtimer_cancel(&softdog_preticktock); 111 108 112 109 return 0; 113 110 } ··· 139 136 watchdog_set_nowayout(&softdog_dev, nowayout); 140 137 watchdog_stop_on_reboot(&softdog_dev); 141 138 142 - if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) 139 + hrtimer_init(&softdog_ticktock, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 140 + softdog_ticktock.function = softdog_fire; 141 + 142 + if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) { 143 143 softdog_info.options |= WDIOF_PRETIMEOUT; 144 + hrtimer_init(&softdog_preticktock, CLOCK_MONOTONIC, 145 + HRTIMER_MODE_REL); 146 + softdog_preticktock.function = softdog_pretimeout; 147 + } 144 148 145 149 ret = watchdog_register_device(&softdog_dev); 146 150 if (ret)