[PATCH] sched/hrtick: Name the minimum slice and derive the rearm slack

Liang Hao <[email protected]>
Newsgroups org.kernel.vger.linux-kernel
Message-ID <[email protected]>
hrtick_start() floors delays at 10000ns to avoid programming slices
too short to matter and to prevent timer DoS.  hrtick_needs_rearm()
separately ignores expiry adjustments below 5000ns as not worth the
reprogram.

Define the slack as half the floor so an expiry shift of one
minimum slice still crosses the rearm threshold.  Name both so
the relationship reads off the constants.

No functional change.

Signed-off-by: Liang Hao <[email protected]>
---
The 10us floor and 5us slack were introduced years apart. This patch
only names the existing values and encodes the slack as half the floor
so a one-slice expiry shift still rearms.

Was the 5us threshold chosen deliberately to be half of the 10us floor,
or as a separate heuristic? Any background on that choice would be helpful.

 kernel/sched/core.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index b77152edafd9..c2149b7c7a9c 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -905,6 +905,9 @@ enum {
 	HRTICK_SCHED_REARM_HRTIMER	= BIT(3)
 };
 
+#define HRTICK_MIN_SLICE_NS	(10 * NSEC_PER_USEC)
+#define HRTICK_REARM_SLACK_NS	(HRTICK_MIN_SLICE_NS / 2)
+
 static void __used hrtick_clear(struct rq *rq)
 {
 	if (hrtimer_active(&rq->hrtick_timer))
@@ -938,7 +941,7 @@ static inline bool hrtick_needs_rearm(struct hrtimer *timer, ktime_t expires)
 	 * whether the expiry time actually changes substantially.
 	 */
 	return !hrtimer_is_queued(timer) ||
-		abs(expires - hrtimer_get_expires(timer)) > 5000;
+		abs(expires - hrtimer_get_expires(timer)) > HRTICK_REARM_SLACK_NS;
 }
 
 static void hrtick_cond_restart(struct rq *rq)
@@ -973,10 +976,10 @@ void hrtick_start(struct rq *rq, u64 delay)
 	s64 delta;
 
 	/*
-	 * Don't schedule slices shorter than 10000ns, that just
-	 * doesn't make sense and can cause timer DoS.
+	 * Don't schedule slices shorter than the minimum hrtick slice.
+	 * That doesn't make sense and can cause timer DoS.
 	 */
-	delta = max_t(s64, delay, 10000LL);
+	delta = max_t(s64, delay, HRTICK_MIN_SLICE_NS);
 
 	/*
 	 * If this is in the middle of schedule() only note the delay
-- 
2.50.1 (Apple Git-155)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.