Skip to content

Commit cef9572

Browse files
Tero KristoWim Van Sebroeck
authored andcommitted
watchdog: add support for adjusting last known HW keepalive time
Certain watchdogs require the watchdog only to be pinged within a specific time window, pinging too early or too late cause the watchdog to fire. In cases where this sort of watchdog has been started before kernel comes up, we must adjust the watchdog keepalive window to match the actually running timer, so add a new driver API for this purpose. Signed-off-by: Tero Kristo <[email protected]> Reviewed-by: Guenter Roeck <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Guenter Roeck <[email protected]> Signed-off-by: Wim Van Sebroeck <[email protected]>
1 parent fbbe35d commit cef9572

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

Documentation/watchdog/watchdog-kernel-api.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,15 @@ an action is taken by a preconfigured pretimeout governor preassigned to
336336
the watchdog device. If watchdog pretimeout governor framework is not
337337
enabled, watchdog_notify_pretimeout() prints a notification message to
338338
the kernel log buffer.
339+
340+
To set the last known HW keepalive time for a watchdog, the following function
341+
should be used::
342+
343+
int watchdog_set_last_hw_keepalive(struct watchdog_device *wdd,
344+
unsigned int last_ping_ms)
345+
346+
This function must be called immediately after watchdog registration. It
347+
sets the last known hardware heartbeat to have happened last_ping_ms before
348+
current time. Calling this is only needed if the watchdog is already running
349+
when probe is called, and the watchdog can only be pinged after the
350+
min_hw_heartbeat_ms time has passed from the last ping.

drivers/watchdog/watchdog_dev.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,36 @@ void watchdog_dev_unregister(struct watchdog_device *wdd)
11381138
watchdog_cdev_unregister(wdd);
11391139
}
11401140

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+
return __watchdog_ping(wdd);
1168+
}
1169+
EXPORT_SYMBOL_GPL(watchdog_set_last_hw_keepalive);
1170+
11411171
/*
11421172
* watchdog_dev_init: init dev part of watchdog core
11431173
*

include/linux/watchdog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ extern int watchdog_init_timeout(struct watchdog_device *wdd,
210210
extern int watchdog_register_device(struct watchdog_device *);
211211
extern void watchdog_unregister_device(struct watchdog_device *);
212212

213+
int watchdog_set_last_hw_keepalive(struct watchdog_device *, unsigned int);
214+
213215
/* devres register variant */
214216
int devm_watchdog_register_device(struct device *dev, struct watchdog_device *);
215217

0 commit comments

Comments
 (0)