mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] tick/nohz: Avoid unused timekeeping_max_deferment() calls
@ 2026-09-18 13:34 Usama Arif
  2026-09-25  9:16 ` Usama Arif
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Usama Arif @ 2026-09-18 13:34 UTC (permalink / raw)
  To: anna-maria, frederic, linux-kernel, mingo, tglx
  Cc: hannes, shakeel.butt, riel, Usama Arif

tick_nohz_next_event() limits a CPU's sleep interval to the maximum
deferment supported by the current clocksource when that CPU owns the
do_timer() duty. If the duty is unassigned, the limit also applies when
the CPU's TS_FLAG_DO_TIMER_LAST flag is set.

After the early timer checks, the function currently reads the maximum
deferment unconditionally. It then replaces the result with KTIME_MAX
unless one of the two conditions above applies.

timekeeping_max_deferment() performs a seqcount-protected read of the
shared timekeeper and follows its clocksource pointer. Check the do_timer
state first and avoid this work when the result would be discarded. This
leaves the resulting expiry unchanged and reduces accesses to timekeeper
data that is modified regularly.

On x86-64 this removes 18-20 dynamically executed instructions, including
the call, from the common non-owner path when the seqcount does not retry.

Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
v1 -> v2:
- Remove the unnecessary comment and delta variable (Frederic Weisbecker).
---
 kernel/time/tick-sched.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index c8f2c4a503b08..a7893a079a83f 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -816,7 +816,7 @@ u64 get_jiffies_update(unsigned long *basej)
  */
 static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
 {
-	u64 basemono, next_tick, delta, expires;
+	u64 basemono, next_tick, expires;
 	unsigned long basejiff;
 	int tick_cpu;
 
@@ -856,8 +856,7 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
 	 * If the tick is due in the next period, keep it ticking or
 	 * force prod the timer.
 	 */
-	delta = next_tick - basemono;
-	if (delta <= (u64)TICK_NSEC) {
+	if (next_tick - basemono <= (u64)TICK_NSEC) {
 		/*
 		 * We've not stopped the tick yet, and there's a timer in the
 		 * next period, so no point in stopping it either, bail.
@@ -873,17 +872,19 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
 	 * the sleep time to the timekeeping 'max_deferment' value.
 	 * Otherwise we can sleep as long as we want.
 	 */
-	delta = timekeeping_max_deferment();
 	tick_cpu = READ_ONCE(tick_do_timer_cpu);
 	if (tick_cpu != cpu &&
-	    (tick_cpu != TICK_DO_TIMER_NONE || !tick_sched_flag_test(ts, TS_FLAG_DO_TIMER_LAST)))
-		delta = KTIME_MAX;
-
-	/* Calculate the next expiry time */
-	if (delta < (KTIME_MAX - basemono))
-		expires = basemono + delta;
-	else
+	    (tick_cpu != TICK_DO_TIMER_NONE || !tick_sched_flag_test(ts, TS_FLAG_DO_TIMER_LAST))) {
 		expires = KTIME_MAX;
+	} else {
+		expires = timekeeping_max_deferment();
+
+		/* Calculate the next expiry time */
+		if (expires < (KTIME_MAX - basemono))
+			expires += basemono;
+		else
+			expires = KTIME_MAX;
+	}
 
 	ts->timer_expires = min_t(u64, expires, next_tick);
 
-- 
2.53.0-Meta


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] tick/nohz: Avoid unused timekeeping_max_deferment() calls
  2026-09-18 13:34 [PATCH v2] tick/nohz: Avoid unused timekeeping_max_deferment() calls Usama Arif
@ 2026-09-25  9:16 ` Usama Arif
  2026-09-27 13:08 ` Rik van Riel
  2026-09-29 19:05 ` [tip: timers/nohz] " tip-bot2 for Usama Arif
  2 siblings, 0 replies; 4+ messages in thread
From: Usama Arif @ 2026-09-25  9:16 UTC (permalink / raw)
  To: Usama Arif
  Cc: anna-maria, frederic, linux-kernel, mingo, tglx, hannes,
	shakeel.butt, riel

On Fri, 18 Sep 2026 06:34:08 -0700 Usama Arif <usama.arif@linux.dev> wrote:

> tick_nohz_next_event() limits a CPU's sleep interval to the maximum
> deferment supported by the current clocksource when that CPU owns the
> do_timer() duty. If the duty is unassigned, the limit also applies when
> the CPU's TS_FLAG_DO_TIMER_LAST flag is set.
> 
> After the early timer checks, the function currently reads the maximum
> deferment unconditionally. It then replaces the result with KTIME_MAX
> unless one of the two conditions above applies.
> 
> timekeeping_max_deferment() performs a seqcount-protected read of the
> shared timekeeper and follows its clocksource pointer. Check the do_timer
> state first and avoid this work when the result would be discarded. This
> leaves the resulting expiry unchanged and reduces accesses to timekeeper
> data that is modified regularly.
> 
> On x86-64 this removes 18-20 dynamically executed instructions, including
> the call, from the common non-owner path when the seqcount does not retry.
> 
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> v1 -> v2:
> - Remove the unnecessary comment and delta variable (Frederic Weisbecker).
> ---
>  kernel/time/tick-sched.c | 23 ++++++++++++-----------
>  1 file changed, 12 insertions(+), 11 deletions(-)

Hi,

Just wanted to check if there are any comments or reviews for this patch?

Thanks!
Usama

> 
> diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
> index c8f2c4a503b08..a7893a079a83f 100644
> --- a/kernel/time/tick-sched.c
> +++ b/kernel/time/tick-sched.c
> @@ -816,7 +816,7 @@ u64 get_jiffies_update(unsigned long *basej)
>   */
>  static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
>  {
> -	u64 basemono, next_tick, delta, expires;
> +	u64 basemono, next_tick, expires;
>  	unsigned long basejiff;
>  	int tick_cpu;
>  
> @@ -856,8 +856,7 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
>  	 * If the tick is due in the next period, keep it ticking or
>  	 * force prod the timer.
>  	 */
> -	delta = next_tick - basemono;
> -	if (delta <= (u64)TICK_NSEC) {
> +	if (next_tick - basemono <= (u64)TICK_NSEC) {
>  		/*
>  		 * We've not stopped the tick yet, and there's a timer in the
>  		 * next period, so no point in stopping it either, bail.
> @@ -873,17 +872,19 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
>  	 * the sleep time to the timekeeping 'max_deferment' value.
>  	 * Otherwise we can sleep as long as we want.
>  	 */
> -	delta = timekeeping_max_deferment();
>  	tick_cpu = READ_ONCE(tick_do_timer_cpu);
>  	if (tick_cpu != cpu &&
> -	    (tick_cpu != TICK_DO_TIMER_NONE || !tick_sched_flag_test(ts, TS_FLAG_DO_TIMER_LAST)))
> -		delta = KTIME_MAX;
> -
> -	/* Calculate the next expiry time */
> -	if (delta < (KTIME_MAX - basemono))
> -		expires = basemono + delta;
> -	else
> +	    (tick_cpu != TICK_DO_TIMER_NONE || !tick_sched_flag_test(ts, TS_FLAG_DO_TIMER_LAST))) {
>  		expires = KTIME_MAX;
> +	} else {
> +		expires = timekeeping_max_deferment();
> +
> +		/* Calculate the next expiry time */
> +		if (expires < (KTIME_MAX - basemono))
> +			expires += basemono;
> +		else
> +			expires = KTIME_MAX;
> +	}
>  
>  	ts->timer_expires = min_t(u64, expires, next_tick);
>  
> -- 
> 2.53.0-Meta
> 
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] tick/nohz: Avoid unused timekeeping_max_deferment() calls
  2026-09-18 13:34 [PATCH v2] tick/nohz: Avoid unused timekeeping_max_deferment() calls Usama Arif
  2026-09-25  9:16 ` Usama Arif
@ 2026-09-27 13:08 ` Rik van Riel
  2026-09-29 19:05 ` [tip: timers/nohz] " tip-bot2 for Usama Arif
  2 siblings, 0 replies; 4+ messages in thread
From: Rik van Riel @ 2026-09-27 13:08 UTC (permalink / raw)
  To: Usama Arif, anna-maria, frederic, linux-kernel, mingo, tglx
  Cc: hannes, shakeel.butt

On Fri, 2026-09-18 at 06:34 -0700, Usama Arif wrote:
> tick_nohz_next_event() limits a CPU's sleep interval to the maximum
> deferment supported by the current clocksource when that CPU owns the
> do_timer() duty. If the duty is unassigned, the limit also applies
> when
> the CPU's TS_FLAG_DO_TIMER_LAST flag is set.
> 
> After the early timer checks, the function currently reads the
> maximum
> deferment unconditionally. It then replaces the result with KTIME_MAX
> unless one of the two conditions above applies.
> 
> timekeeping_max_deferment() performs a seqcount-protected read of the
> shared timekeeper and follows its clocksource pointer. Check the
> do_timer
> state first and avoid this work when the result would be discarded.
> This
> leaves the resulting expiry unchanged and reduces accesses to
> timekeeper
> data that is modified regularly.
> 
> On x86-64 this removes 18-20 dynamically executed instructions,
> including
> the call, from the common non-owner path when the seqcount does not
> retry.
> 
> Signed-off-by: Usama Arif <usama.arif@linux.dev>

The changelog confused me a little at first, but after
reading through the code it all made sense.

Reviewed-by: Rik van Riel <riel@surriel.com>

-- 
All Rights Reversed.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tip: timers/nohz] tick/nohz: Avoid unused timekeeping_max_deferment() calls
  2026-09-18 13:34 [PATCH v2] tick/nohz: Avoid unused timekeeping_max_deferment() calls Usama Arif
  2026-09-25  9:16 ` Usama Arif
  2026-09-27 13:08 ` Rik van Riel
@ 2026-09-29 19:05 ` tip-bot2 for Usama Arif
  2 siblings, 0 replies; 4+ messages in thread
From: tip-bot2 for Usama Arif @ 2026-09-29 19:05 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Usama Arif, Thomas Gleixner, Rik van Riel, x86, linux-kernel

The following commit has been merged into the timers/nohz branch of tip:

Commit-ID:     d305927765cf6025bc11db9d96c06ea663230ddd
Gitweb:        https://git.kernel.org/tip/d305927765cf6025bc11db9d96c06ea663230ddd
Author:        Usama Arif <usama.arif@linux.dev>
AuthorDate:    Fri, 18 Sep 2026 06:34:08 -07:00
Committer:     Thomas Gleixner <tglx@kernel.org>
CommitterDate: Tue, 29 Sep 2026 21:00:08 +02:00

tick/nohz: Avoid unused timekeeping_max_deferment() calls

tick_nohz_next_event() limits a CPU's sleep interval to the maximum
deferment supported by the current clocksource when that CPU owns the
do_timer() duty. If the duty is unassigned, the limit also applies when
the CPU's TS_FLAG_DO_TIMER_LAST flag is set.

After the early timer checks, the function currently reads the maximum
deferment unconditionally. It then replaces the result with KTIME_MAX
unless one of the two conditions above applies.

timekeeping_max_deferment() performs a seqcount-protected read of the
shared timekeeper and follows its clocksource pointer. Check the do_timer
state first and avoid this work when the result would be discarded. This
leaves the resulting expiry unchanged and reduces accesses to timekeeper
data that is modified regularly.

On x86-64 this removes 18-20 dynamically executed instructions, including
the call, from the common non-owner path when the seqcount does not retry.

Signed-off-by: Usama Arif <usama.arif@linux.dev>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Reviewed-by: Rik van Riel <riel@surriel.com>
Link: https://patch.msgid.link/20260918133408.2834751-1-usama.arif@linux.dev
---
 kernel/time/tick-sched.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index c8f2c4a..a7893a0 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -816,7 +816,7 @@ u64 get_jiffies_update(unsigned long *basej)
  */
 static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
 {
-	u64 basemono, next_tick, delta, expires;
+	u64 basemono, next_tick, expires;
 	unsigned long basejiff;
 	int tick_cpu;
 
@@ -856,8 +856,7 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
 	 * If the tick is due in the next period, keep it ticking or
 	 * force prod the timer.
 	 */
-	delta = next_tick - basemono;
-	if (delta <= (u64)TICK_NSEC) {
+	if (next_tick - basemono <= (u64)TICK_NSEC) {
 		/*
 		 * We've not stopped the tick yet, and there's a timer in the
 		 * next period, so no point in stopping it either, bail.
@@ -873,17 +872,19 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
 	 * the sleep time to the timekeeping 'max_deferment' value.
 	 * Otherwise we can sleep as long as we want.
 	 */
-	delta = timekeeping_max_deferment();
 	tick_cpu = READ_ONCE(tick_do_timer_cpu);
 	if (tick_cpu != cpu &&
-	    (tick_cpu != TICK_DO_TIMER_NONE || !tick_sched_flag_test(ts, TS_FLAG_DO_TIMER_LAST)))
-		delta = KTIME_MAX;
-
-	/* Calculate the next expiry time */
-	if (delta < (KTIME_MAX - basemono))
-		expires = basemono + delta;
-	else
+	    (tick_cpu != TICK_DO_TIMER_NONE || !tick_sched_flag_test(ts, TS_FLAG_DO_TIMER_LAST))) {
 		expires = KTIME_MAX;
+	} else {
+		expires = timekeeping_max_deferment();
+
+		/* Calculate the next expiry time */
+		if (expires < (KTIME_MAX - basemono))
+			expires += basemono;
+		else
+			expires = KTIME_MAX;
+	}
 
 	ts->timer_expires = min_t(u64, expires, next_tick);
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-29 19:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-18 13:34 [PATCH v2] tick/nohz: Avoid unused timekeeping_max_deferment() calls Usama Arif
2026-09-25  9:16 ` Usama Arif
2026-09-27 13:08 ` Rik van Riel
2026-09-29 19:05 ` [tip: timers/nohz] " tip-bot2 for Usama Arif

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®