git: ee29897fc3d6 - main - time(3): Declare the minimum and maximum hz values supported.

From: Hans Petter Selasky <hselasky_at_FreeBSD.org>
Date: Sun, 23 Oct 2022 08:14:18 UTC
The branch main has been updated by hselasky:

URL: https://cgit.FreeBSD.org/src/commit/?id=ee29897fc3d66ba7006c9ee3f524bf2e8cf30a4f

commit ee29897fc3d66ba7006c9ee3f524bf2e8cf30a4f
Author:     Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2022-10-03 09:08:53 +0000
Commit:     Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2022-10-23 08:04:50 +0000

    time(3): Declare the minimum and maximum hz values supported.
    
    Reviewed by:    kib@ and imp@
    MFC after:      1 week
    Sponsored by:   NVIDIA Networking
    Differential Revision:  https://reviews.freebsd.org/D37072
---
 sys/kern/subr_param.c | 11 +++++++++++
 sys/sys/time.h        |  7 +++++++
 2 files changed, 18 insertions(+)

diff --git a/sys/kern/subr_param.c b/sys/kern/subr_param.c
index ebb21283c5f9..f9c2f8df87b8 100644
--- a/sys/kern/subr_param.c
+++ b/sys/kern/subr_param.c
@@ -111,6 +111,10 @@ u_long	sgrowsiz;			/* amount to grow stack */
 
 SYSCTL_INT(_kern, OID_AUTO, hz, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &hz, 0,
     "Number of clock ticks per second");
+SYSCTL_INT(_kern, OID_AUTO, hz_max, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, HZ_MAXIMUM,
+    "Maximum hz value supported");
+SYSCTL_INT(_kern, OID_AUTO, hz_min, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, HZ_MINIMUM,
+    "Minimum hz value supported");
 SYSCTL_INT(_kern, OID_AUTO, nbuf, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &nbuf, 0,
     "Number of buffers in the buffer cache");
 SYSCTL_INT(_kern, OID_AUTO, nswbuf, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &nswbuf, 0,
@@ -173,6 +177,13 @@ init_param1(void)
 	TUNABLE_INT_FETCH("kern.hz", &hz);
 	if (hz == -1)
 		hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ;
+
+	/* range check the "hz" value */
+	if (__predict_false(hz < HZ_MINIMUM))
+		hz = HZ_MINIMUM;
+	else if (__predict_false(hz > HZ_MAXIMUM))
+		hz = HZ_MAXIMUM;
+
 	tick = 1000000 / hz;
 	tick_sbt = SBT_1S / hz;
 	tick_bt = sbttobt(tick_sbt);
diff --git a/sys/sys/time.h b/sys/sys/time.h
index 41d84aab5640..5d7f3f07234e 100644
--- a/sys/sys/time.h
+++ b/sys/sys/time.h
@@ -583,6 +583,13 @@ void	timevaladd(struct timeval *t1, const struct timeval *t2);
 void	timevalsub(struct timeval *t1, const struct timeval *t2);
 int	tvtohz(struct timeval *tv);
 
+/*
+ * The following HZ limits allow the tvtohz() function
+ * to only use integer computations.
+ */
+#define	HZ_MAXIMUM (INT_MAX / (1000000 >> 6)) /* 137kHz */
+#define	HZ_MINIMUM 8 /* hz */
+
 #define	TC_DEFAULTPERC		5
 
 #define	BT2FREQ(bt)                                                     \