From d8920042b55e882518faeefa665ee7f67ce9c555 Mon Sep 17 00:00:00 2001 From: cheloha Date: Fri, 3 Mar 2023 20:16:44 +0000 Subject: [PATCH] initclocks: don't reinitialize ticks, jiffies at runtime Various drivers use ticks/jiffies before initclocks(). It isn't generally safe to reinitialize them at runtime. Hoist the conditional definition of HZ from param.c into sys/kernel.h so we can see it from kern_clock.c and statically initialize ticks/jiffies to the desired offset. With this change, timeouts scheduled before initclocks() do not all fire immediately during the first softclock(). With input from kettenis@. Link: https://marc.info/?l=openbsd-tech&m=167753870803378&w=2 --- sys/conf/param.c | 5 +---- sys/kern/kern_clock.c | 9 +++------ sys/sys/kernel.h | 6 +++++- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/sys/conf/param.c b/sys/conf/param.c index 6a3cd398c55..42f56d1eab2 100644 --- a/sys/conf/param.c +++ b/sys/conf/param.c @@ -1,4 +1,4 @@ -/* $OpenBSD: param.c,v 1.47 2022/04/13 10:08:10 sthen Exp $ */ +/* $OpenBSD: param.c,v 1.48 2023/03/03 20:16:44 cheloha Exp $ */ /* $NetBSD: param.c,v 1.16 1996/03/12 03:08:40 mrg Exp $ */ /* @@ -69,9 +69,6 @@ * Compiled with -DHZ=xx -DMAXUSERS=xx */ -#ifndef HZ -#define HZ 100 -#endif int hz = HZ; int tick = 1000000 / HZ; int tick_nsec = 1000000000 / HZ; diff --git a/sys/kern/kern_clock.c b/sys/kern/kern_clock.c index fc6d90486c3..1265d740dea 100644 --- a/sys/kern/kern_clock.c +++ b/sys/kern/kern_clock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_clock.c,v 1.106 2023/02/04 19:33:03 cheloha Exp $ */ +/* $OpenBSD: kern_clock.c,v 1.107 2023/03/03 20:16:44 cheloha Exp $ */ /* $NetBSD: kern_clock.c,v 1.34 1996/06/09 04:51:03 briggs Exp $ */ /*- @@ -86,11 +86,11 @@ int stathz; int schedhz; int profhz; int profprocs; -int ticks; +int ticks = INT_MAX - (15 * 60 * HZ); static int psdiv, pscnt; /* prof => stat divider */ int psratio; /* ratio: prof / stat */ -volatile unsigned long jiffies; /* XXX Linux API for drm(4) */ +volatile unsigned long jiffies = ULONG_MAX - (10 * 60 * HZ); /* * Initialize clock frequencies and start both clocks running. @@ -98,9 +98,6 @@ volatile unsigned long jiffies; /* XXX Linux API for drm(4) */ void initclocks(void) { - ticks = INT_MAX - (15 * 60 * hz); - jiffies = ULONG_MAX - (10 * 60 * hz); - /* * Set divisors to 1 (normal case) and let the machine-specific * code do its bit. diff --git a/sys/sys/kernel.h b/sys/sys/kernel.h index b0a66fdd47c..0147fde22c0 100644 --- a/sys/sys/kernel.h +++ b/sys/sys/kernel.h @@ -1,4 +1,4 @@ -/* $OpenBSD: kernel.h,v 1.25 2021/01/13 16:28:50 cheloha Exp $ */ +/* $OpenBSD: kernel.h,v 1.26 2023/03/03 20:16:44 cheloha Exp $ */ /* $NetBSD: kernel.h,v 1.11 1995/03/03 01:24:16 cgd Exp $ */ /*- @@ -56,3 +56,7 @@ extern int hz; /* system clock's frequency */ extern int stathz; /* statistics clock's frequency */ extern int profhz; /* profiling clock's frequency */ extern int lbolt; /* once a second sleep address */ + +#ifndef HZ +#define HZ 100 +#endif -- 2.20.1