mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot2 for Anna-Maria Behnsen" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: "Anna-Maria Behnsen" <anna-maria@linutronix.de>,
	Thomas Gleixner <tglx@linutronix.de>,
	Frederic Weisbecker <frederic@kernel.org>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: timers/core] timers: Move marking timer bases idle into tick_nohz_stop_tick()
Date: Thu, 22 Feb 2024 17:12:15 -0000	[thread overview]
Message-ID: <170862193597.398.11739577160319960761.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20240221090548.36600-4-anna-maria@linutronix.de>

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

Commit-ID:     e2e1d724e948c87a31c18c34c6b6a193a9b2a0f0
Gitweb:        https://git.kernel.org/tip/e2e1d724e948c87a31c18c34c6b6a193a9b2a0f0
Author:        Anna-Maria Behnsen <anna-maria@linutronix.de>
AuthorDate:    Wed, 21 Feb 2024 10:05:31 +01:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Thu, 22 Feb 2024 17:52:30 +01:00

timers: Move marking timer bases idle into tick_nohz_stop_tick()

The timer base is marked idle when get_next_timer_interrupt() is
executed. But the decision whether the tick will be stopped and whether the
system is able to go idle is done later. When the timer bases is marked
idle and a new first timer is enqueued remote an IPI is raised. Even if it
is not required because the tick is not stopped and the timer base is
evaluated again at the next tick.

To prevent this, the timer base is marked idle in tick_nohz_stop_tick() and
get_next_timer_interrupt() is streamlined by only looking for the next timer
interrupt. All other work is postponed to timer_base_try_to_set_idle() which is
called by tick_nohz_stop_tick(). timer_base_try_to_set_idle() never resets
timer_base::is_idle state. This is done when the tick is restarted via
tick_nohz_restart_sched_tick().

With this, tick_sched::tick_stopped and timer_base::is_idle are always in
sync. So there is no longer the need to execute timer_clear_idle() in
tick_nohz_idle_retain_tick(). This was required before, as
tick_nohz_next_event() set timer_base::is_idle even if the tick would not be
stopped. So timer_clear_idle() is only executed, when timer base is idle. So the
check whether timer base is idle, is now no longer required as well.

While at it fix some nearby whitespace damage as well.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
Link: https://lore.kernel.org/r/20240221090548.36600-4-anna-maria@linutronix.de

---
 kernel/time/tick-internal.h |  1 +-
 kernel/time/tick-sched.c    | 40 ++++++++++++++++--------
 kernel/time/timer.c         | 60 +++++++++++++++++++++++++-----------
 3 files changed, 71 insertions(+), 30 deletions(-)

diff --git a/kernel/time/tick-internal.h b/kernel/time/tick-internal.h
index 481b7ab..47df30b 100644
--- a/kernel/time/tick-internal.h
+++ b/kernel/time/tick-internal.h
@@ -163,6 +163,7 @@ static inline void timers_update_nohz(void) { }
 DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases);
 
 extern u64 get_next_timer_interrupt(unsigned long basej, u64 basem);
+u64 timer_base_try_to_set_idle(unsigned long basej, u64 basem, bool *idle);
 void timer_clear_idle(void);
 
 #define CLOCK_SET_WALL							\
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 7c9efe3..344b904 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -860,11 +860,6 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
 	delta = next_tick - basemono;
 	if (delta <= (u64)TICK_NSEC) {
 		/*
-		 * Tell the timer code that the base is not idle, i.e. undo
-		 * the effect of get_next_timer_interrupt():
-		 */
-		timer_clear_idle();
-		/*
 		 * We've not stopped the tick yet, and there's a timer in the
 		 * next period, so no point in stopping it either, bail.
 		 */
@@ -899,13 +894,39 @@ out:
 static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)
 {
 	struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
+	unsigned long basejiff = ts->last_jiffies;
 	u64 basemono = ts->timer_expires_base;
-	u64 expires = ts->timer_expires;
+	bool timer_idle;
+	u64 expires;
 
 	/* Make sure we won't be trying to stop it twice in a row. */
 	ts->timer_expires_base = 0;
 
 	/*
+	 * Now the tick should be stopped definitely - so the timer base needs
+	 * to be marked idle as well to not miss a newly queued timer.
+	 */
+	expires = timer_base_try_to_set_idle(basejiff, basemono, &timer_idle);
+	if (expires > ts->timer_expires) {
+		/*
+		 * This path could only happen when the first timer was removed
+		 * between calculating the possible sleep length and now (when
+		 * high resolution mode is not active, timer could also be a
+		 * hrtimer).
+		 *
+		 * We have to stick to the original calculated expiry value to
+		 * not stop the tick for too long with a shallow C-state (which
+		 * was programmed by cpuidle because of an early next expiration
+		 * value).
+		 */
+		expires = ts->timer_expires;
+	}
+
+	/* If the timer base is not idle, retain the not yet stopped tick. */
+	if (!timer_idle)
+		return;
+
+	/*
 	 * If this CPU is the one which updates jiffies, then give up
 	 * the assignment and let it be taken by the CPU which runs
 	 * the tick timer next, which might be this CPU as well. If we
@@ -1001,7 +1022,7 @@ static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
 	touch_softlockup_watchdog_sched();
 
 	/* Cancel the scheduled timer and restore the tick: */
-	ts->tick_stopped  = 0;
+	ts->tick_stopped = 0;
 	tick_nohz_restart(ts, now);
 }
 
@@ -1157,11 +1178,6 @@ void tick_nohz_idle_stop_tick(void)
 void tick_nohz_idle_retain_tick(void)
 {
 	tick_nohz_retain_tick(this_cpu_ptr(&tick_cpu_sched));
-	/*
-	 * Undo the effect of get_next_timer_interrupt() called from
-	 * tick_nohz_next_event().
-	 */
-	timer_clear_idle();
 }
 
 /**
diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 9f0cdba..a4b8a58 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1956,19 +1956,22 @@ static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
 	return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
 }
 
-static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem)
+static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,
+					     bool *idle)
 {
 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
 	unsigned long nextevt = basej + NEXT_TIMER_MAX_DELTA;
 	u64 expires = KTIME_MAX;
-	bool was_idle;
 
 	/*
 	 * Pretend that there is no timer pending if the cpu is offline.
 	 * Possible pending timers will be migrated later to an active cpu.
 	 */
-	if (cpu_is_offline(smp_processor_id()))
+	if (cpu_is_offline(smp_processor_id())) {
+		if (idle)
+			*idle = true;
 		return expires;
+	}
 
 	raw_spin_lock(&base->lock);
 	if (base->next_expiry_recalc)
@@ -1998,17 +2001,26 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem)
 	__forward_timer_base(base, basej);
 
 	/*
-	 * Base is idle if the next event is more than a tick away.
-	 *
-	 * If the base is marked idle then any timer add operation must forward
-	 * the base clk itself to keep granularity small. This idle logic is
-	 * only maintained for the BASE_STD base, deferrable timers may still
-	 * see large granularity skew (by design).
+	 * Set base->is_idle only when caller is timer_base_try_to_set_idle()
 	 */
-	was_idle = base->is_idle;
-	base->is_idle = time_after(nextevt, basej + 1);
-	if (was_idle != base->is_idle)
-		trace_timer_base_idle(base->is_idle, base->cpu);
+	if (idle) {
+		/*
+		 * Base is idle if the next event is more than a tick away.
+		 *
+		 * If the base is marked idle then any timer add operation must
+		 * forward the base clk itself to keep granularity small. This
+		 * idle logic is only maintained for the BASE_STD base,
+		 * deferrable timers may still see large granularity skew (by
+		 * design).
+		 */
+		if (!base->is_idle) {
+			if (time_after(nextevt, basej + 1)) {
+				base->is_idle = true;
+				trace_timer_base_idle(true, base->cpu);
+			}
+		}
+		*idle = base->is_idle;
+	}
 
 	raw_spin_unlock(&base->lock);
 
@@ -2025,7 +2037,21 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem)
  */
 u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 {
-	return __get_next_timer_interrupt(basej, basem);
+	return __get_next_timer_interrupt(basej, basem, NULL);
+}
+
+/**
+ * timer_base_try_to_set_idle() - Try to set the idle state of the timer bases
+ * @basej:	base time jiffies
+ * @basem:	base time clock monotonic
+ * @idle:	pointer to store the value of timer_base->is_idle
+ *
+ * Returns the tick aligned clock monotonic time of the next pending
+ * timer or KTIME_MAX if no timer is pending.
+ */
+u64 timer_base_try_to_set_idle(unsigned long basej, u64 basem, bool *idle)
+{
+	return __get_next_timer_interrupt(basej, basem, idle);
 }
 
 /**
@@ -2043,10 +2069,8 @@ void timer_clear_idle(void)
 	 * sending the IPI a few instructions smaller for the cost of taking
 	 * the lock in the exit from idle path.
 	 */
-	if (base->is_idle) {
-		base->is_idle = false;
-		trace_timer_base_idle(false, smp_processor_id());
-	}
+	base->is_idle = false;
+	trace_timer_base_idle(false, smp_processor_id());
 }
 #endif
 

  parent reply	other threads:[~2024-02-22 17:12 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-21  9:05 [PATCH v11 00/20] timers: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 01/20] timers: Restructure get_next_timer_interrupt() Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 02/20] timers: Split out get next timer interrupt Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 03/20] timers: Move marking timer bases idle into tick_nohz_stop_tick() Anna-Maria Behnsen
2024-02-21 20:36   ` Frederic Weisbecker
2024-02-22 17:12   ` tip-bot2 for Anna-Maria Behnsen [this message]
2024-02-21  9:05 ` [PATCH v11 04/20] timers: Optimization for timer_base_try_to_set_idle() Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 05/20] timers: Introduce add_timer() variants which modify timer flags Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 06/20] workqueue: Use global variant for add_timer() Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 07/20] timers: add_timer_on(): Make sure TIMER_PINNED flag is set Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] timers: Make sure TIMER_PINNED flag is set in add_timer_on() tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 08/20] timers: Ease code in run_local_timers() Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] timers: Simplify " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 09/20] timers: Split next timer interrupt logic Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 10/20] timers: Keep the pinned timers separate from the others Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 11/20] timers: Retrieve next expiry of pinned/non-pinned timers separately Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 12/20] timers: Split out "get next timer interrupt" functionality Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 13/20] timers: Add get next timer interrupt functionality for remote CPUs Anna-Maria Behnsen
2024-02-21 20:50   ` Frederic Weisbecker
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 14/20] timers: Restructure internal locking Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Richard Cochran (linutronix GmbH)
2024-02-21  9:05 ` [PATCH v11 15/20] timers: Check if timers base is handled already Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 16/20] tick/sched: Split out jiffies update helper function Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Richard Cochran (linutronix GmbH)
2024-02-21  9:05 ` [PATCH v11 17/20] timers: Introduce function to check timer base is_idle flag Anna-Maria Behnsen
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 18/20] timers: Implement the hierarchical pull model Anna-Maria Behnsen
2024-02-21 10:37   ` [PATCH v11a] " Anna-Maria Behnsen
2024-02-21 22:45   ` [PATCH v11 18/20] " Frederic Weisbecker
2024-02-22  8:17     ` Anna-Maria Behnsen
2024-02-22 10:25       ` Frederic Weisbecker
2024-02-22 10:37   ` [PATCH v11b " Anna-Maria Behnsen
2024-02-22 10:50     ` Frederic Weisbecker
2024-02-22 17:12     ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 19/20] timer_migration: Add tracepoints Anna-Maria Behnsen
2024-02-21 22:46   ` Frederic Weisbecker
2024-02-21 23:17   ` Steven Rostedt
2024-02-22 10:34   ` [PATCH v11a " Anna-Maria Behnsen
2024-02-22 14:59     ` Steven Rostedt
2024-02-22 17:12     ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-21  9:05 ` [PATCH v11 20/20] timers: Always queue timers on the local CPU Anna-Maria Behnsen
2024-02-21 22:57   ` Frederic Weisbecker
2024-02-22 17:12   ` [tip: timers/core] " tip-bot2 for Anna-Maria Behnsen
2024-02-22 13:33 ` [PATCH] timers/timer_migration: Fix memory barrier comment Anna-Maria Behnsen
2024-02-22 13:44   ` Frederic Weisbecker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=170862193597.398.11739577160319960761.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=anna-maria@linutronix.de \
    --cc=frederic@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®