[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250128063301.3879317-3-jstultz@google.com>
Date: Mon, 27 Jan 2025 22:32:54 -0800
From: John Stultz <jstultz@...gle.com>
To: LKML <linux-kernel@...r.kernel.org>
Cc: John Stultz <jstultz@...gle.com>, Anna-Maria Behnsen <anna-maria@...utronix.de>,
Frederic Weisbecker <frederic@...nel.org>, Ingo Molnar <mingo@...nel.org>,
Thomas Gleixner <tglx@...utronix.de>, Peter Zijlstra <peterz@...radead.org>,
Juri Lelli <juri.lelli@...hat.com>, Vincent Guittot <vincent.guittot@...aro.org>,
Dietmar Eggemann <dietmar.eggemann@....com>, Steven Rostedt <rostedt@...dmis.org>,
Ben Segall <bsegall@...gle.com>, Mel Gorman <mgorman@...e.de>,
Valentin Schneider <vschneid@...hat.com>, Stephen Boyd <sboyd@...nel.org>,
Yury Norov <yury.norov@...il.com>, Bitao Hu <yaoma@...ux.alibaba.com>,
Andrew Morton <akpm@...ux-foundation.org>, kernel-team@...roid.com
Subject: [RFC][PATCH 2/3] time/tick: Introduce a dyn_hz boot option
Introduce dyn_hz= option, which allows the actual timer tick to
be scaled down at boot time.
This allows kerenls to be built with HZ=1000 but systems to
effectively run as if HZ=100 if specified.
The system will still run with the configured HZ value, but
ticks will just arrive "late".
The valid values are between 100 and the build time CONFIG_HZ
value.
Cc: Anna-Maria Behnsen <anna-maria@...utronix.de>
Cc: Frederic Weisbecker <frederic@...nel.org>
Cc: Ingo Molnar <mingo@...nel.org>
Cc: Thomas Gleixner <tglx@...utronix.de>
Cc: Peter Zijlstra <peterz@...radead.org>
Cc: Juri Lelli <juri.lelli@...hat.com>
Cc: Vincent Guittot <vincent.guittot@...aro.org>
Cc: Dietmar Eggemann <dietmar.eggemann@....com>
Cc: Steven Rostedt <rostedt@...dmis.org>
Cc: Ben Segall <bsegall@...gle.com>
Cc: Mel Gorman <mgorman@...e.de>
Cc: Valentin Schneider <vschneid@...hat.com>
Cc: Stephen Boyd <sboyd@...nel.org>
Cc: Yury Norov <yury.norov@...il.com>
Cc: Bitao Hu <yaoma@...ux.alibaba.com>
Cc: Andrew Morton <akpm@...ux-foundation.org>
Cc: kernel-team@...roid.com
Signed-off-by: John Stultz <jstultz@...gle.com>
---
include/linux/tick.h | 11 +++++++++--
kernel/Kconfig.hz | 9 +++++++++
kernel/time/tick-common.c | 30 ++++++++++++++++++++++++++++++
kernel/time/tick-sched.c | 4 ++--
4 files changed, 50 insertions(+), 4 deletions(-)
diff --git a/include/linux/tick.h b/include/linux/tick.h
index b8ddc8e631a3c..734ee1e08c9ef 100644
--- a/include/linux/tick.h
+++ b/include/linux/tick.h
@@ -14,6 +14,13 @@
#include <linux/rcupdate.h>
#include <linux/static_key.h>
+#ifdef CONFIG_DYN_HZ
+extern long long dyn_tick_nsec;
+#define DYN_TICK_NSEC (dyn_tick_nsec)
+#else
+#define DYN_TICK_NSEC TICK_NSEC
+#endif
+
#ifdef CONFIG_GENERIC_CLOCKEVENTS
extern void __init tick_init(void);
/* Should be core only, but ARM BL switcher requires it */
@@ -153,11 +160,11 @@ static inline bool tick_nohz_idle_got_tick(void) { return false; }
static inline ktime_t tick_nohz_get_next_hrtimer(void)
{
/* Next wake up is the tick period, assume it starts now */
- return ktime_add(ktime_get(), TICK_NSEC);
+ return ktime_add(ktime_get(), DYN_TICK_NSEC);
}
static inline ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
{
- *delta_next = TICK_NSEC;
+ *delta_next = DYN_TICK_NSEC;
return *delta_next;
}
static inline u64 get_cpu_idle_time_us(int cpu, u64 *unused) { return -1; }
diff --git a/kernel/Kconfig.hz b/kernel/Kconfig.hz
index 38ef6d06888ef..76714317674c5 100644
--- a/kernel/Kconfig.hz
+++ b/kernel/Kconfig.hz
@@ -55,5 +55,14 @@ config HZ
default 300 if HZ_300
default 1000 if HZ_1000
+config DYN_HZ
+ bool "Support dynamic HZ via boot argument"
+ default n
+ help
+ Allow for the tick rate to be set at boot time via the dynhz=
+ boot argument. This allows the tick rate to be lower then the
+ build time configured HZ value.
+ If you are unsure, say no.
+
config SCHED_HRTICK
def_bool HIGH_RES_TIMERS
diff --git a/kernel/time/tick-common.c b/kernel/time/tick-common.c
index ae5c5befdc58b..75fd9dadb8273 100644
--- a/kernel/time/tick-common.c
+++ b/kernel/time/tick-common.c
@@ -80,6 +80,30 @@ int tick_is_oneshot_available(void)
return tick_broadcast_oneshot_available();
}
+#ifdef CONFIG_DYN_HZ
+long long dyn_tick_nsec = TICK_NSEC;
+
+static int __init set_dyn_hz(char *str)
+{
+ int ret, dyn_hz;
+
+ ret = kstrtoint(str, 0, &dyn_hz);
+ if (ret)
+ return ret;
+ if (dyn_hz > HZ || dyn_hz < 100)
+ dyn_hz = HZ;
+ dyn_tick_nsec = TICK_NSEC * HZ / dyn_hz;
+ return 1;
+}
+#else /* !CONFIG_DYN_HZ */
+static int __init set_dyn_hz(char *str)
+{
+ pr_warn("CONFIG_DYN_HZ not enabled, ignoring dyn_hz boot argument\n");
+ return -1;
+}
+#endif /* CONFIG_DYN_HZ */
+__setup("dyn_hz=", set_dyn_hz);
+
/*
* Periodic tick
*/
@@ -575,4 +599,10 @@ void __init tick_init(void)
{
tick_broadcast_init();
tick_nohz_init();
+ if (DYN_TICK_NSEC != TICK_NSEC) {
+ long long dynhz = TICK_NSEC * HZ;
+
+ do_div(dynhz, DYN_TICK_NSEC);
+ pr_info("dynHZ in use! HZ=%ld dynHZ=%lld\n", (long)HZ, dynhz);
+ }
}
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 983790923aee9..040ad70c31d38 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -309,7 +309,7 @@ static enum hrtimer_restart tick_nohz_handler(struct hrtimer *timer)
if (unlikely(tick_sched_flag_test(ts, TS_FLAG_STOPPED)))
return HRTIMER_NORESTART;
- hrtimer_forward(timer, now, TICK_NSEC);
+ hrtimer_forward(timer, now, DYN_TICK_NSEC);
return HRTIMER_RESTART;
}
@@ -842,7 +842,7 @@ static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
/* Forward the time to expire in the future */
- hrtimer_forward(&ts->sched_timer, now, TICK_NSEC);
+ hrtimer_forward(&ts->sched_timer, now, DYN_TICK_NSEC);
if (tick_sched_flag_test(ts, TS_FLAG_HIGHRES)) {
hrtimer_start_expires(&ts->sched_timer,
--
2.48.1.262.g85cc9f2d1e-goog
Powered by blists - more mailing lists