summaryrefslogtreecommitdiffstats
path: root/sys
diff options
context:
space:
mode:
authoralfred <alfred@FreeBSD.org>2013-07-27 20:47:01 +0000
committeralfred <alfred@FreeBSD.org>2013-07-27 20:47:01 +0000
commitfdd68bea0e12d92e6afb245364fba13c3f8b499b (patch)
tree56df7df9ac8f7b431ecb8a19f2ae000fa5ebdd85 /sys
parent24e94a1aa8ae0ae20697703ef7605e13c814621f (diff)
downloadFreeBSD-src-fdd68bea0e12d92e6afb245364fba13c3f8b499b.zip
FreeBSD-src-fdd68bea0e12d92e6afb245364fba13c3f8b499b.tar.gz
Fix watchdog pretimeout.
The original API calls for pow2ns, however the new APIs from Linux call for seconds. We need to be able to convert to/from 2^Nns to seconds in both userland and kernel to fix this and properly compare units.
Diffstat (limited to 'sys')
-rw-r--r--sys/conf/files1
-rw-r--r--sys/dev/watchdog/watchdog.c74
-rw-r--r--sys/libkern/flsll.c47
-rw-r--r--sys/sys/libkern.h4
4 files changed, 122 insertions, 4 deletions
diff --git a/sys/conf/files b/sys/conf/files
index c74aa71..4a1a432 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -2976,6 +2976,7 @@ libkern/arc4random.c standard
libkern/bcd.c standard
libkern/bsearch.c standard
libkern/crc32.c standard
+libkern/flsll.c standard
libkern/fnmatch.c standard
libkern/iconv.c optional libiconv
libkern/iconv_converter_if.m optional libiconv
diff --git a/sys/dev/watchdog/watchdog.c b/sys/dev/watchdog/watchdog.c
index 71d8ecd..54a5db9 100644
--- a/sys/dev/watchdog/watchdog.c
+++ b/sys/dev/watchdog/watchdog.c
@@ -39,6 +39,7 @@ __FBSDID("$FreeBSD$");
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
+#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/watchdog.h>
#include <sys/bus.h>
@@ -60,10 +61,56 @@ static int wd_softtimeout_act = WD_SOFT_LOG; /* action for the software timeout
static struct cdev *wd_dev;
static volatile u_int wd_last_u; /* last timeout value set by kern_do_pat */
+static u_int wd_last_u_sysctl; /* last timeout value set by kern_do_pat */
+static u_int wd_last_u_sysctl_secs; /* wd_last_u in seconds */
+
+SYSCTL_NODE(_hw, OID_AUTO, watchdog, CTLFLAG_RD, 0, "Main watchdog device");
+SYSCTL_UINT(_hw_watchdog, OID_AUTO, wd_last_u, CTLFLAG_RD,
+ &wd_last_u_sysctl, 0, "Watchdog last update time");
+SYSCTL_UINT(_hw_watchdog, OID_AUTO, wd_last_u_secs, CTLFLAG_RD,
+ &wd_last_u_sysctl_secs, 0, "Watchdog last update time");
static int wd_lastpat_valid = 0;
static time_t wd_lastpat = 0; /* when the watchdog was last patted */
+static void
+pow2ns_to_ts(int pow2ns, struct timespec *ts)
+{
+ uint64_t ns;
+
+ ns = 1ULL << pow2ns;
+ ts->tv_sec = ns / 1000000000ULL;
+ ts->tv_nsec = ns % 1000000000ULL;
+}
+
+static int
+pow2ns_to_ticks(int pow2ns)
+{
+ struct timeval tv;
+ struct timespec ts;
+
+ pow2ns_to_ts(pow2ns, &ts);
+ TIMESPEC_TO_TIMEVAL(&tv, &ts);
+ return (tvtohz(&tv));
+}
+
+static int
+seconds_to_pow2ns(int seconds)
+{
+ uint64_t power;
+ uint64_t ns;
+ uint64_t shifted;
+
+ ns = ((uint64_t)seconds) * 1000000000ULL;
+ power = flsll(ns);
+ shifted = 1ULL << power;
+ if (shifted <= ns) {
+ power++;
+ }
+ return (power);
+}
+
+
int
wdog_kern_pat(u_int utim)
{
@@ -86,6 +133,8 @@ wdog_kern_pat(u_int utim)
* This can be zero (to disable the watchdog)
*/
wd_last_u = (utim & WD_INTERVAL);
+ wd_last_u_sysctl = wd_last_u;
+ wd_last_u_sysctl_secs = pow2ns_to_ticks(wd_last_u) / hz;
}
if ((utim & WD_INTERVAL) == WD_TO_NEVER) {
utim = 0;
@@ -101,7 +150,7 @@ wdog_kern_pat(u_int utim)
callout_stop(&wd_softtimeo_handle);
} else {
(void) callout_reset(&wd_softtimeo_handle,
- hz*utim, wd_timeout_cb, "soft");
+ pow2ns_to_ticks(utim), wd_timeout_cb, "soft");
}
error = 0;
} else {
@@ -201,10 +250,13 @@ static int
wd_set_pretimeout(int newtimeout, int disableiftoolong)
{
u_int utime;
+ struct timespec utime_ts;
+ int timeout_ticks;
utime = wdog_kern_last_timeout();
+ pow2ns_to_ts(utime, &utime_ts);
/* do not permit a pre-timeout >= than the timeout. */
- if (newtimeout >= utime) {
+ if (newtimeout >= utime_ts.tv_sec) {
/*
* If 'disableiftoolong' then just fall through
* so as to disable the pre-watchdog
@@ -222,8 +274,22 @@ wd_set_pretimeout(int newtimeout, int disableiftoolong)
return 0;
}
+ timeout_ticks = pow2ns_to_ticks(utime) - (hz*newtimeout);
+#if 0
+ printf("wd_set_pretimeout: "
+ "newtimeout: %d, "
+ "utime: %d -> utime_ticks: %d, "
+ "hz*newtimeout: %d, "
+ "timeout_ticks: %d -> sec: %d\n",
+ newtimeout,
+ utime, pow2ns_to_ticks(utime),
+ hz*newtimeout,
+ timeout_ticks, timeout_ticks / hz);
+#endif
+
/* We determined the value is sane, so reset the callout */
- (void) callout_reset(&wd_pretimeo_handle, hz*(utime - newtimeout),
+ (void) callout_reset(&wd_pretimeo_handle,
+ timeout_ticks,
wd_timeout_cb, "pre-timeout");
wd_pretimeout = newtimeout;
return 0;
@@ -282,7 +348,7 @@ wd_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
break;
case WDIOC_SETTIMEOUT:
u = *(u_int *)data;
- error = wdog_kern_pat(u);
+ error = wdog_kern_pat(seconds_to_pow2ns(u));
break;
case WDIOC_GETTIMEOUT:
u = wdog_kern_last_timeout();
diff --git a/sys/libkern/flsll.c b/sys/libkern/flsll.c
new file mode 100644
index 0000000..0629480
--- /dev/null
+++ b/sys/libkern/flsll.c
@@ -0,0 +1,47 @@
+/*-
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#include <sys/libkern.h>
+__FBSDID("$FreeBSD$");
+
+/*
+ * Find Last Set bit
+ */
+int
+flsll(long long mask)
+{
+ int bit;
+
+ if (mask == 0)
+ return (0);
+ for (bit = 1; mask != 1; bit++)
+ mask = (unsigned long long)mask >> 1;
+ return (bit);
+}
diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h
index c04e88d..3665b3a 100644
--- a/sys/sys/libkern.h
+++ b/sys/sys/libkern.h
@@ -94,6 +94,10 @@ int fls(int);
#ifndef HAVE_INLINE_FLSL
int flsl(long);
#endif
+#ifndef HAVE_INLINE_FLSLL
+int flsll(long long);
+#endif
+
int fnmatch(const char *, const char *, int);
int locc(int, char *, u_int);
void *memchr(const void *s, int c, size_t n);
OpenPOWER on IntegriCloud