On Mon, Feb 27, 2023 at 04:57:04PM -0600, Scott Cheloha wrote:
> ticks and jiffies start at zero. During boot in initclocks(), we
> reset them:
>
> /* sys/kern/kern_clock.c */
>
> 89 int ticks;
> 90 static int psdiv, pscnt; /* prof => stat divider
> */
> 91 int psratio; /* ratio: prof / stat */
> 92
> 93 volatile unsigned long jiffies; /* XXX Linux API for
> drm(4) */
> 94
> 95 /*
> 96 * Initialize clock frequencies and start both clocks running.
> 97 */
> 98 void
> 99 initclocks(void)
> 100 {
> 101 ticks = INT_MAX - (15 * 60 * hz);
> 102 jiffies = ULONG_MAX - (10 * 60 * hz);
> 103
> 104 /* [... ] */
>
> The idea here (committed by dlg@) is sound. We reset ticks and
> jiffies to near-rollover values to catch buggy code misusing them.
>
> But! That jump from zero to whatever violates valid assumptions made
> by correct code, too.
Assumptions made by what code? Does it exist in the tree?
>
> It would be better to just initialize ticks and jiffies to the
> near-rollover values when we declare them. To do this we need to
> move their declarations from sys/kern/kern_clock.c to sys/conf/param.c
> where HZ is visible.
>
> ok?
>
> Index: kern/kern_clock.c
> ===================================================================
> RCS file: /cvs/src/sys/kern/kern_clock.c,v
> retrieving revision 1.106
> diff -u -p -r1.106 kern_clock.c
> --- kern/kern_clock.c 4 Feb 2023 19:33:03 -0000 1.106
> +++ kern/kern_clock.c 27 Feb 2023 22:55:24 -0000
> @@ -86,21 +86,15 @@ int stathz;
> int schedhz;
> int profhz;
> int profprocs;
> -int ticks;
> static int psdiv, pscnt; /* prof => stat divider */
> int psratio; /* ratio: prof / stat */
>
> -volatile unsigned long jiffies; /* XXX Linux API for drm(4) */
> -
> /*
> * Initialize clock frequencies and start both clocks running.
> */
> 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.
> @@ -171,7 +165,8 @@ hardclock(struct clockframe *frame)
>
> tc_ticktock();
> ticks++;
> - jiffies++;
> + extern volatile unsigned long jiffies;
> + jiffies++; /* XXX drm(4) */
>
> /*
> * Update the timeout wheel.
> Index: conf/param.c
> ===================================================================
> RCS file: /cvs/src/sys/conf/param.c,v
> retrieving revision 1.47
> diff -u -p -r1.47 param.c
> --- conf/param.c 13 Apr 2022 10:08:10 -0000 1.47
> +++ conf/param.c 27 Feb 2023 22:55:24 -0000
> @@ -73,6 +73,8 @@
> #define HZ 100
> #endif
> int hz = HZ;
> +int ticks = INT_MAX - (15 * 60 * HZ);
> +volatile unsigned long jiffies = ULONG_MAX - (10 * 60 * HZ); /* drm(4) */
> int tick = 1000000 / HZ;
> int tick_nsec = 1000000000 / HZ;
> int utc_offset = 0;
>