git: 98be2ec6df2c - stable/13 - time(3): Declare the minimum and maximum hz values supported.

From: Hans Petter Selasky <hselasky_at_FreeBSD.org>
Date: Sat, 12 Nov 2022 12:41:46 UTC
The branch stable/13 has been updated by hselasky:

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

commit 98be2ec6df2cbce44271c8a096dff814d949fc75
Author:     Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2022-10-03 09:08:53 +0000
Commit:     Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2022-11-12 11:59:40 +0000

    time(3): Declare the minimum and maximum hz values supported.
    
    Reviewed by:    kib@ and imp@
    Sponsored by:   NVIDIA Networking
    Differential Revision:  https://reviews.freebsd.org/D37072
    
    (cherry picked from commit ee29897fc3d66ba7006c9ee3f524bf2e8cf30a4f)
---
 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 9c07fa7c7157..94b60a6e5538 100644
--- a/sys/kern/subr_param.c
+++ b/sys/kern/subr_param.c
@@ -115,6 +115,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,
@@ -177,6 +181,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)                                                     \