All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model
@ 2023-05-24  7:06 Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 01/21] tick-sched: Warn when next tick seems to be in the past Anna-Maria Behnsen
                   ` (20 more replies)
  0 siblings, 21 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen

Placing timers at enqueue time on a target CPU based on dubious heuristics
does not make any sense:

 1) Most timer wheel timers are canceled or rearmed before they expire.

 2) The heuristics to predict which CPU will be busy when the timer expires
    are wrong by definition.

So placing the timers at enqueue wastes precious cycles.

The proper solution to this problem is to always queue the timers on the
local CPU and allow the non pinned timers to be pulled onto a busy CPU at
expiry time.

Therefore split the timer storage into local pinned and global timers:
Local pinned timers are always expired on the CPU on which they have been
queued. Global timers can be expired on any CPU.

As long as a CPU is busy it expires both local and global timers. When a
CPU goes idle it arms for the first expiring local timer. If the first
expiring pinned (local) timer is before the first expiring movable timer,
then no action is required because the CPU will wake up before the first
movable timer expires. If the first expiring movable timer is before the
first expiring pinned (local) timer, then this timer is queued into a idle
timerqueue and eventually expired by some other active CPU.

To avoid global locking the timerqueues are implemented as a hierarchy. The
lowest level of the hierarchy holds the CPUs. The CPUs are associated to
groups of 8, which are separated per node. If more than one CPU group
exist, then a second level in the hierarchy collects the groups. Depending
on the size of the system more than 2 levels are required. Each group has a
"migrator" which checks the timerqueue during the tick for remote timers to
be expired.

If the last CPU in a group goes idle it reports the first expiring event in
the group up to the next group(s) in the hierarchy. If the last CPU goes
idle it arms its timer for the first system wide expiring timer to ensure
that no timer event is missed.


Testing
~~~~~~~

The impact of wasting cycles during enqueue by using the heuristic in
contrast to always queuing the timer on the local CPU was measured with a
micro benchmark. Therefore a timer is enqueued and dequeued in a loop with
1000 repetitions on a isolated CPU. The time the loop takes is measured. A
quarter of the remaining CPUs was kept busy. This measurement was repeated
several times. With the patch queue the average duration was reduced by
approximately 25%.

	145ns	plain v6
	109ns	v6 with patch queue


Furthermore the impact of residence in deep idle states of an idle system
was investigated. The patch queue doesn't downgrade this behavior.


During testing on a mostly idle machine a ping pong game could be observed:
a process_timeout timer is expired remotely on a non idle CPU. Then the CPU
where the schedule_timeout() was executed to enqueue the timer comes out of
idle and restarts the timer using schedule_timeout() and goes back to idle
again. This is due to the fair scheduler which tries to keep the task on
the CPU which it previously executed on.


Next Steps
~~~~~~~~~~

Simple deferrable timers are no longer required as they can be converted to
global timers. If a CPU goes idle, a formerly deferrable timer will not
prevent the CPU to sleep as long as possible. Only the last migrator CPU
has to take care of them. Deferrable timers with timer pinned flags needs
to be expired on the specified CPU but must not prevent CPU from going
idle. They require their own timer base which is never taken into account
when calculating the next expiry time. This conversation and required
cleanup will be done in a follow up series.

v6..v7:
  - Address review feedback of Frederic and bigeasy
  - Change lock, unlock fetch next timer interrupt logic after remote expiry
  - Move timer_expire_remote() into tick-internal.h
  - Add documentation section about "Required event and timerqueue update
    after remote expiry"
  - Fix fallout of kernel test robot


v5..v6:

  - Address review of Frederic Weisbecker and Peter Zijlstra (spelling,
    locking, race in tmigr_handle_remote_cpu())

  - unconditionally set TIMER_PINNED flag in add_timer_on(); introduce
    add_timer() variants which set/unset TIMER_PINNED flag; drop fixing
    add_timer_on() call sites, as TIMER_PINNED flag is set implicitly;
    Fixing workqueue to use add_timer_global() instead of simply
    add_timer() for unbound work.

  - Drop support for siblings to end up in the same level 0 group (could be
    added again in a better way as an improvement later on)

  - Do not send IPI for new first deferrable timers

v4..v5:
  - address review feedback of Frederic Weisbecker
  - fix issue with group timer update after remote expiry

v3..v4:
  - address review feedback of Frederic Weisbecker
  - address kernel test robot fallout
  - Move patch 16 "add_timer_on(): Make sure callers have TIMER_PINNED
    flag" at the begin of the queue to prevent timers to end up in global
    timer base when they were queued using add_timer_on()
  - Fix some comments and typos

v2..v3: https://lore.kernel.org/r/20170418111102.490432548@linutronix.de/
  - Minimize usage of locks by storing data using atomic_cmpxchg() for
    migrator information and information about active cpus.


Thanks,

	Anna-Maria



Anna-Maria Behnsen (18):
  tick-sched: Warn when next tick seems to be in the past
  timer: Do not IPI for deferrable timers
  timer: Add comment to get_next_timer_interrupt() description
  timer: Move store of next event into __next_timer_interrupt()
  timer: Split next timer interrupt logic
  timers: Introduce add_timer() variants which modify timer flags
  workqueue: Use global variant for add_timer()
  timer: add_timer_on(): Make sure TIMER_PINNED flag is set
  timers: Ease code in run_local_timers()
  timers: Create helper function to forward timer base clk
  timer: Keep the pinned timers separate from the others
  timer: Retrieve next expiry of pinned/non-pinned timers separately
  timer: Split out "get next timer interrupt" functionality
  timer: Add get next timer interrupt functionality for remote CPUs
  timer: Check if timers base is handled already
  timer: Implement the hierarchical pull model
  timer_migration: Add tracepoints
  timer: Always queue timers on the local CPU

Richard Cochran (linutronix GmbH) (2):
  timer: Restructure internal locking
  tick/sched: Split out jiffies update helper function

Thomas Gleixner (1):
  timer: Rework idle logic

 include/linux/cpuhotplug.h             |    1 +
 include/linux/timer.h                  |   16 +-
 include/trace/events/timer_migration.h |  277 +++++
 kernel/time/Makefile                   |    3 +
 kernel/time/tick-internal.h            |   12 +
 kernel/time/tick-sched.c               |   20 +-
 kernel/time/timer.c                    |  458 ++++++--
 kernel/time/timer_migration.c          | 1435 ++++++++++++++++++++++++
 kernel/time/timer_migration.h          |  136 +++
 kernel/workqueue.c                     |    2 +-
 10 files changed, 2254 insertions(+), 106 deletions(-)
 create mode 100644 include/trace/events/timer_migration.h
 create mode 100644 kernel/time/timer_migration.c
 create mode 100644 kernel/time/timer_migration.h

-- 
2.30.2


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

* [PATCH v7 01/21] tick-sched: Warn when next tick seems to be in the past
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 02/21] timer: Do not IPI for deferrable timers Anna-Maria Behnsen
                   ` (19 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Frederic Weisbecker

When the next tick is in the past, the delta between basemono and the next
tick gets negativ. But the next tick should never be in the past. The
negative effect of a wrong next tick might be a stop of the tick and timers
might expire late.

To prevent expensive debugging when changing underlying code, add a
WARN_ON_ONCE into this code path.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
---
 kernel/time/tick-sched.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 52254679ec48..62836490ba4d 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -831,6 +831,8 @@ 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.
 	 */
+	WARN_ON_ONCE(basemono > next_tick);
+
 	delta = next_tick - basemono;
 	if (delta <= (u64)TICK_NSEC) {
 		/*
-- 
2.30.2


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

* [PATCH v7 02/21] timer: Do not IPI for deferrable timers
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 01/21] tick-sched: Warn when next tick seems to be in the past Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-06-05 21:29   ` Frederic Weisbecker
  2023-05-24  7:06 ` [PATCH v7 03/21] timer: Add comment to get_next_timer_interrupt() description Anna-Maria Behnsen
                   ` (18 subsequent siblings)
  20 siblings, 1 reply; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen

Deferrable timers do not prevent CPU from going idle and are not taken into
account on idle path. Sending an IPI to a remote CPU when a new first
deferrable timer was enqueued will wake up the remote CPU and but nothing
will be done regarding the deferrable timers.

Drop IPI completely when a new first deferrable timer was enqueued.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
v6: new patch
---
 kernel/time/timer.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 63a8ce7177dd..6e251e3cf659 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -571,18 +571,15 @@ static int calc_wheel_index(unsigned long expires, unsigned long clk,
 static void
 trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
 {
-	if (!is_timers_nohz_active())
-		return;
-
 	/*
-	 * TODO: This wants some optimizing similar to the code below, but we
-	 * will do that when we switch from push to pull for deferrable timers.
+	 * Deferrable timers do not prevent CPU from going idle and are not
+	 * taken into account on idle path. An IPI when a new deferrable
+	 * timer is enqueued will wake up the remote CPU but nothing will
+	 * be done with the deferrable timer base. Therefore skip remote
+	 * IPI for deferrable timers completely.
 	 */
-	if (timer->flags & TIMER_DEFERRABLE) {
-		if (tick_nohz_full_cpu(base->cpu))
-			wake_up_nohz_cpu(base->cpu);
+	if (!is_timers_nohz_active() || timer->flags & TIMER_DEFERRABLE)
 		return;
-	}
 
 	/*
 	 * We might have to IPI the remote CPU if the base is idle and the
-- 
2.30.2


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

* [PATCH v7 03/21] timer: Add comment to get_next_timer_interrupt() description
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 01/21] tick-sched: Warn when next tick seems to be in the past Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 02/21] timer: Do not IPI for deferrable timers Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 04/21] timer: Move store of next event into __next_timer_interrupt() Anna-Maria Behnsen
                   ` (17 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Frederic Weisbecker

get_next_timer_interrupt() does more than simply getting the next timer
interrupt. The timer bases are forwarded and also marked as idle whenever
possible.

To get not confused, add a comment to function description.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
---
v6: s/handling/marking

v5: New patch, which adds only a comment to get_next_timer_interrupt()
instead of changing the function name
---
 kernel/time/timer.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 6e251e3cf659..0699fd14d00a 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1912,6 +1912,10 @@ static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
  * @basej:	base time jiffies
  * @basem:	base time clock monotonic
  *
+ * If required, base->clk is forwarded and base is also marked as
+ * idle. Idle marking of timer bases is allowed only to be done by CPU
+ * itself.
+ *
  * Returns the tick aligned clock monotonic time of the next pending
  * timer or KTIME_MAX if no timer is pending.
  */
-- 
2.30.2


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

* [PATCH v7 04/21] timer: Move store of next event into __next_timer_interrupt()
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (2 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 03/21] timer: Add comment to get_next_timer_interrupt() description Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 05/21] timer: Split next timer interrupt logic Anna-Maria Behnsen
                   ` (16 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Frederic Weisbecker

Both call sites of __next_timer_interrupt() store return value directly in
base->next_expiry. Move the store into __next_timer_interrupt() and to make
its purpose more clear, rename function to next_expiry_recalc().

No functional change.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
---
v6: Fix typos in commit message and drop not required return as suggested
    by Peter Zijlstra)

v4: rename function as suggested by Frederic Weisbecker
---
 kernel/time/timer.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 0699fd14d00a..59f2e15733ab 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1800,8 +1800,10 @@ static int next_pending_bucket(struct timer_base *base, unsigned offset,
 /*
  * Search the first expiring timer in the various clock levels. Caller must
  * hold base->lock.
+ *
+ * Store next expiry time in base->next_expiry.
  */
-static unsigned long __next_timer_interrupt(struct timer_base *base)
+static void next_expiry_recalc(struct timer_base *base)
 {
 	unsigned long clk, next, adj;
 	unsigned lvl, offset = 0;
@@ -1867,10 +1869,9 @@ static unsigned long __next_timer_interrupt(struct timer_base *base)
 		clk += adj;
 	}
 
+	base->next_expiry = next;
 	base->next_expiry_recalc = false;
 	base->timers_pending = !(next == base->clk + NEXT_TIMER_MAX_DELTA);
-
-	return next;
 }
 
 #ifdef CONFIG_NO_HZ_COMMON
@@ -1934,7 +1935,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 
 	raw_spin_lock(&base->lock);
 	if (base->next_expiry_recalc)
-		base->next_expiry = __next_timer_interrupt(base);
+		next_expiry_recalc(base);
 	nextevt = base->next_expiry;
 
 	/*
@@ -2017,7 +2018,7 @@ static inline void __run_timers(struct timer_base *base)
 		WARN_ON_ONCE(!levels && !base->next_expiry_recalc
 			     && base->timers_pending);
 		base->clk++;
-		base->next_expiry = __next_timer_interrupt(base);
+		next_expiry_recalc(base);
 
 		while (levels--)
 			expire_timers(base, heads + levels);
-- 
2.30.2


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

* [PATCH v7 05/21] timer: Split next timer interrupt logic
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (3 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 04/21] timer: Move store of next event into __next_timer_interrupt() Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 06/21] timer: Rework idle logic Anna-Maria Behnsen
                   ` (15 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Frederic Weisbecker

Logic for getting next timer interrupt (no matter of recalculated or
already stored in base->next_expiry) is split into a separate function
"next_timer_interrupt()" to make it available for new call sites.

No functional change.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
---
 kernel/time/timer.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 59f2e15733ab..1a61977f9efa 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1908,6 +1908,14 @@ static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
 	return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
 }
 
+static unsigned long next_timer_interrupt(struct timer_base *base)
+{
+	if (base->next_expiry_recalc)
+		next_expiry_recalc(base);
+
+	return base->next_expiry;
+}
+
 /**
  * get_next_timer_interrupt - return the time (clock mono) of the next timer
  * @basej:	base time jiffies
@@ -1934,9 +1942,8 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 		return expires;
 
 	raw_spin_lock(&base->lock);
-	if (base->next_expiry_recalc)
-		next_expiry_recalc(base);
-	nextevt = base->next_expiry;
+
+	nextevt = next_timer_interrupt(base);
 
 	/*
 	 * We have a fresh next event. Check whether we can forward the
-- 
2.30.2


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

* [PATCH v7 06/21] timer: Rework idle logic
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (4 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 05/21] timer: Split next timer interrupt logic Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 07/21] timers: Introduce add_timer() variants which modify timer flags Anna-Maria Behnsen
                   ` (14 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Frederic Weisbecker

From: Thomas Gleixner <tglx@linutronix.de>

To improve readability of the code, split base->idle calculation and
expires calculation into separate parts.

Thereby the following subtle change happens if the next event is just one
jiffy ahead and the tick was already stopped: Originally base->is_idle
remains true in this situation. Now base->is_idle turns to false. This may
spare an IPI if a timer is enqueued remotely to an idle CPU that is going
to tick on the next jiffy.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
---
v4: Change condition to force 0 delta and update commit message (Frederic)
---
 kernel/time/timer.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 1a61977f9efa..1522fb81887e 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1957,21 +1957,20 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 			base->clk = nextevt;
 	}
 
-	if (time_before_eq(nextevt, basej)) {
-		expires = basem;
-		base->is_idle = false;
-	} else {
-		if (base->timers_pending)
-			expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
-		/*
-		 * If we expect to sleep more than a tick, mark the base idle.
-		 * Also the tick is stopped so any added timer 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 ((expires - basem) > TICK_NSEC)
-			base->is_idle = true;
+	/*
+	 * Base is idle if the next event is more than a tick away. Also
+	 * the tick is stopped so any added timer 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).
+	 */
+	base->is_idle = time_after(nextevt, basej + 1);
+
+	if (base->timers_pending) {
+		/* If we missed a tick already, force 0 delta */
+		if (time_before(nextevt, basej))
+			nextevt = basej;
+		expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
 	}
 	raw_spin_unlock(&base->lock);
 
-- 
2.30.2


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

* [PATCH v7 07/21] timers: Introduce add_timer() variants which modify timer flags
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (5 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 06/21] timer: Rework idle logic Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 08/21] workqueue: Use global variant for add_timer() Anna-Maria Behnsen
                   ` (13 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen

Timer might be used as pinned timer (using add_timer_on()) and later on as
non pinned timers using add_timer(). When the NOHZ timer pull at expiry
model is in place, TIMER_PINNED flag is required to be used whenever a
timer needs to expire on a dedicated CPU. Flag must no be set, if
expiration on a dedicated CPU is not required.

add_timer_on()'s behavior will be changed during the preparation patches
for the NOHZ timer pull at expiry model to unconditionally set TIMER_PINNED
flag. To be able to reset/set the flag when queueing a timer, two variants
of add_timer() are introduced.

This is a preparatory patch and has no functional change.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
New in v6
---
 include/linux/timer.h |  2 ++
 kernel/time/timer.c   | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/include/linux/timer.h b/include/linux/timer.h
index 9162f275819a..6f96661480dd 100644
--- a/include/linux/timer.h
+++ b/include/linux/timer.h
@@ -180,6 +180,8 @@ extern int timer_reduce(struct timer_list *timer, unsigned long expires);
 #define NEXT_TIMER_MAX_DELTA	((1UL << 30) - 1)
 
 extern void add_timer(struct timer_list *timer);
+extern void add_timer_local(struct timer_list *timer);
+extern void add_timer_global(struct timer_list *timer);
 
 extern int try_to_del_timer_sync(struct timer_list *timer);
 extern int timer_delete_sync(struct timer_list *timer);
diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 1522fb81887e..ab9a8bb11a8a 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1242,6 +1242,40 @@ void add_timer(struct timer_list *timer)
 }
 EXPORT_SYMBOL(add_timer);
 
+/**
+ * add_timer_local - Start a timer on the local CPU
+ * @timer:	The timer to be started
+ *
+ * Same as add_timer() except that the timer flag TIMER_PINNED is set.
+ *
+ * See add_timer() for further details.
+ */
+void add_timer_local(struct timer_list *timer)
+{
+	if (WARN_ON_ONCE(timer_pending(timer)))
+		return;
+	timer->flags |= TIMER_PINNED;
+	__mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);
+}
+EXPORT_SYMBOL(add_timer_local);
+
+/**
+ * add_timer_global - Start a timer without TIMER_PINNED flag set
+ * @timer:	The timer to be started
+ *
+ * Same as add_timer() except that the timer flag TIMER_PINNED is unset.
+ *
+ * See add_timer() for further details.
+ */
+void add_timer_global(struct timer_list *timer)
+{
+	if (WARN_ON_ONCE(timer_pending(timer)))
+		return;
+	timer->flags &= ~TIMER_PINNED;
+	__mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);
+}
+EXPORT_SYMBOL(add_timer_global);
+
 /**
  * add_timer_on - Start a timer on a particular CPU
  * @timer:	The timer to be started
-- 
2.30.2


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

* [PATCH v7 08/21] workqueue: Use global variant for add_timer()
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (6 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 07/21] timers: Introduce add_timer() variants which modify timer flags Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-06-05 22:25   ` Frederic Weisbecker
  2023-05-24  7:06 ` [PATCH v7 09/21] timer: add_timer_on(): Make sure TIMER_PINNED flag is set Anna-Maria Behnsen
                   ` (12 subsequent siblings)
  20 siblings, 1 reply; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Tejun Heo, Lai Jiangshan

The implementation of the NOHZ pull at expiry model will change the timer
bases per CPU. Timers, that have to expire on a specific CPU, require the
TIMER_PINNED flag. If the CPU doesn't matter, the TIMER_PINNED flag must be
dropped. This is required for call sites which use the timer alternately as
pinned and not pinned timer like workqueues do.

Therefore use add_timer_global() to make sure TIMER_PINNED flag is dropped.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Acked-by: Tejun Heo <tj@kernel.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Lai Jiangshan <jiangshanlai@gmail.com>
---
v6:
  - New patch: As v6 provides unconditially setting TIMER_PINNED flag in
    add_timer_on() workqueue requires new add_timer_global() variant.
---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 4666a1a92a31..5c529f19e709 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -1682,7 +1682,7 @@ static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
 	if (unlikely(cpu != WORK_CPU_UNBOUND))
 		add_timer_on(timer, cpu);
 	else
-		add_timer(timer);
+		add_timer_global(timer);
 }
 
 /**
-- 
2.30.2


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

* [PATCH v7 09/21] timer: add_timer_on(): Make sure TIMER_PINNED flag is set
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (7 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 08/21] workqueue: Use global variant for add_timer() Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-06-05 22:26   ` Frederic Weisbecker
  2023-05-24  7:06 ` [PATCH v7 10/21] timers: Ease code in run_local_timers() Anna-Maria Behnsen
                   ` (11 subsequent siblings)
  20 siblings, 1 reply; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen

When adding a timer to the timer wheel using add_timer_on(), it is an
implicitly pinned timer. With the timer pull at expiry time model in place,
TIMER_PINNED flag is required to make sure timers end up in proper base.

Add TIMER_PINNED flag unconditionally when add_timer_on() is executed.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
 kernel/time/timer.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index ab9a8bb11a8a..b7599216d183 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1281,7 +1281,10 @@ EXPORT_SYMBOL(add_timer_global);
  * @timer:	The timer to be started
  * @cpu:	The CPU to start it on
  *
- * Same as add_timer() except that it starts the timer on the given CPU.
+ * Same as add_timer() except that it starts the timer on the given CPU and
+ * the TIMER_PINNED flag is set. When timer shouldn't be a pinned timer in
+ * the next round, add_timer_global() should be used instead as it unsets
+ * the TIMER_PINNED flag.
  *
  * See add_timer() for further details.
  */
@@ -1295,6 +1298,9 @@ void add_timer_on(struct timer_list *timer, int cpu)
 	if (WARN_ON_ONCE(timer_pending(timer)))
 		return;
 
+	/* Make sure timer flags have TIMER_PINNED flag set */
+	timer->flags |= TIMER_PINNED;
+
 	new_base = get_timer_cpu_base(timer->flags, cpu);
 
 	/*
-- 
2.30.2


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

* [PATCH v7 10/21] timers: Ease code in run_local_timers()
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (8 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 09/21] timer: add_timer_on(): Make sure TIMER_PINNED flag is set Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 11/21] timers: Create helper function to forward timer base clk Anna-Maria Behnsen
                   ` (10 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Frederic Weisbecker

The logic for raising a softirq the way it is implemented right now, is
readable for two timer bases. When increasing numbers of timer bases, code
gets harder to read. With the introduction of the timer migration
hierarchy, there will be three timer bases.

Therefore ease the code. No functional change.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
---
v5: New patch to decrease patch size of follow up patches
---
 kernel/time/timer.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index b7599216d183..749b1570bdcd 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -2093,16 +2093,14 @@ static void run_local_timers(void)
 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
 
 	hrtimer_run_queues();
-	/* Raise the softirq only if required. */
-	if (time_before(jiffies, base->next_expiry)) {
-		if (!IS_ENABLED(CONFIG_NO_HZ_COMMON))
-			return;
-		/* CPU is awake, so check the deferrable base. */
-		base++;
-		if (time_before(jiffies, base->next_expiry))
+
+	for (int i = 0; i < NR_BASES; i++, base++) {
+		/* Raise the softirq only if required. */
+		if (time_after_eq(jiffies, base->next_expiry)) {
+			raise_softirq(TIMER_SOFTIRQ);
 			return;
+		}
 	}
-	raise_softirq(TIMER_SOFTIRQ);
 }
 
 /*
-- 
2.30.2


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

* [PATCH v7 11/21] timers: Create helper function to forward timer base clk
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (9 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 10/21] timers: Ease code in run_local_timers() Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 12/21] timer: Keep the pinned timers separate from the others Anna-Maria Behnsen
                   ` (9 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Frederic Weisbecker

The logic for forwarding timer base clock is split into a separate function
to make it accessible for other call sites.

No functional change.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
---
v6: s/splitted/split
v5: New patch to simplify next patch
---
 kernel/time/timer.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 749b1570bdcd..fcff03757641 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1956,6 +1956,21 @@ static unsigned long next_timer_interrupt(struct timer_base *base)
 	return base->next_expiry;
 }
 
+/*
+ * Forward base clock is done only when @basej is past base->clk, otherwise
+ * base-clk might be rewind.
+ */
+static void forward_base_clk(struct timer_base *base, unsigned long nextevt,
+			     unsigned long basej)
+{
+	if (time_after(basej, base->clk)) {
+		if (time_after(nextevt, basej))
+			base->clk = basej;
+		else if (time_after(nextevt, base->clk))
+			base->clk = nextevt;
+	}
+}
+
 /**
  * get_next_timer_interrupt - return the time (clock mono) of the next timer
  * @basej:	base time jiffies
@@ -1987,15 +2002,9 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 
 	/*
 	 * We have a fresh next event. Check whether we can forward the
-	 * base. We can only do that when @basej is past base->clk
-	 * otherwise we might rewind base->clk.
+	 * base.
 	 */
-	if (time_after(basej, base->clk)) {
-		if (time_after(nextevt, basej))
-			base->clk = basej;
-		else if (time_after(nextevt, base->clk))
-			base->clk = nextevt;
-	}
+	forward_base_clk(base, nextevt, basej);
 
 	/*
 	 * Base is idle if the next event is more than a tick away. Also
-- 
2.30.2


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

* [PATCH v7 12/21] timer: Keep the pinned timers separate from the others
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (10 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 11/21] timers: Create helper function to forward timer base clk Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-06-06 10:27   ` Frederic Weisbecker
  2023-05-24  7:06 ` [PATCH v7 13/21] timer: Retrieve next expiry of pinned/non-pinned timers separately Anna-Maria Behnsen
                   ` (8 subsequent siblings)
  20 siblings, 1 reply; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Richard Cochran

Separate the storage space for pinned timers. Deferrable timers (doesn't
matter if pinned or non pinned) are still enqueued into their own base.

This is preparatory work for changing the NOHZ timer placement from a push
at enqueue time to a pull at expiry time model.

Originally-by: Richard Cochran (linutronix GmbH) <richardcochran@gmail.com>
Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
v6:
  - Drop set TIMER_PINNED flag in add_timer_on() and drop related
    warning. add_timer_on() fix is splitted into a separate
    patch. Therefore also drop "Reviewed-by" of Frederic Weisbecker

v5:
  - Add WARN_ONCE() in add_timer_on()
  - Decrease patch size by splitting into three patches (this patch and the
    two before)

v4:
  - split out logic to forward base clock into a helper function
    forward_base_clk() (Frederic)
  - ease the code in run_local_timers() and timer_clear_idle() (Frederic)
---
 kernel/time/timer.c | 85 +++++++++++++++++++++++++++++++++------------
 1 file changed, 62 insertions(+), 23 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index fcff03757641..010c8877fa85 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -187,12 +187,18 @@ EXPORT_SYMBOL(jiffies_64);
 #define WHEEL_SIZE	(LVL_SIZE * LVL_DEPTH)
 
 #ifdef CONFIG_NO_HZ_COMMON
-# define NR_BASES	2
-# define BASE_STD	0
-# define BASE_DEF	1
+/*
+ * If multiple bases need to be locked, use the base ordering for lock
+ * nesting, i.e. lowest number first.
+ */
+# define NR_BASES	3
+# define BASE_LOCAL	0
+# define BASE_GLOBAL	1
+# define BASE_DEF	2
 #else
 # define NR_BASES	1
-# define BASE_STD	0
+# define BASE_LOCAL	0
+# define BASE_GLOBAL	0
 # define BASE_DEF	0
 #endif
 
@@ -899,7 +905,10 @@ static int detach_if_pending(struct timer_list *timer, struct timer_base *base,
 
 static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
 {
-	struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu);
+	int index = tflags & TIMER_PINNED ? BASE_LOCAL : BASE_GLOBAL;
+	struct timer_base *base;
+
+	base = per_cpu_ptr(&timer_bases[index], cpu);
 
 	/*
 	 * If the timer is deferrable and NO_HZ_COMMON is set then we need
@@ -912,7 +921,10 @@ static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
 
 static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
 {
-	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
+	int index = tflags & TIMER_PINNED ? BASE_LOCAL : BASE_GLOBAL;
+	struct timer_base *base;
+
+	base = this_cpu_ptr(&timer_bases[index]);
 
 	/*
 	 * If the timer is deferrable and NO_HZ_COMMON is set then we need
@@ -1985,9 +1997,10 @@ static void forward_base_clk(struct timer_base *base, unsigned long nextevt,
  */
 u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 {
-	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
+	unsigned long nextevt, nextevt_local, nextevt_global;
+	struct timer_base *base_local, *base_global;
+	bool local_first, is_idle;
 	u64 expires = KTIME_MAX;
-	unsigned long nextevt;
 
 	/*
 	 * Pretend that there is no timer pending if the cpu is offline.
@@ -1996,32 +2009,57 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 	if (cpu_is_offline(smp_processor_id()))
 		return expires;
 
-	raw_spin_lock(&base->lock);
+	base_local = this_cpu_ptr(&timer_bases[BASE_LOCAL]);
+	base_global = this_cpu_ptr(&timer_bases[BASE_GLOBAL]);
 
-	nextevt = next_timer_interrupt(base);
+	raw_spin_lock(&base_local->lock);
+	raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING);
+
+	nextevt_local = next_timer_interrupt(base_local);
+	nextevt_global = next_timer_interrupt(base_global);
 
 	/*
 	 * We have a fresh next event. Check whether we can forward the
 	 * base.
 	 */
-	forward_base_clk(base, nextevt, basej);
+	forward_base_clk(base_local, nextevt_local, basej);
+	forward_base_clk(base_global, nextevt_global, basej);
+
+	/*
+	 * Check whether the local event is expiring before or at the same
+	 * time as the global event.
+	 *
+	 * Note, that nextevt_global and nextevt_local might be based on
+	 * different base->clk values. So it's not guaranteed that
+	 * comparing with empty bases results in a correct local_first.
+	 */
+	if (base_local->timers_pending && base_global->timers_pending)
+		local_first = time_before_eq(nextevt_local, nextevt_global);
+	else
+		local_first = base_local->timers_pending;
+
+	nextevt = local_first ? nextevt_local : nextevt_global;
 
 	/*
-	 * Base is idle if the next event is more than a tick away. Also
+	 * Bases are idle if the next event is more than a tick away. Also
 	 * the tick is stopped so any added timer 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).
+	 * maintained for the BASE_LOCAL and BASE_GLOBAL base, deferrable
+	 * timers may still see large granularity skew (by design).
 	 */
-	base->is_idle = time_after(nextevt, basej + 1);
+	is_idle = time_after(nextevt, basej + 1);
+
+	/* We need to mark both bases in sync */
+	base_local->is_idle = base_global->is_idle = is_idle;
 
-	if (base->timers_pending) {
+	if (base_local->timers_pending || base_global->timers_pending) {
 		/* If we missed a tick already, force 0 delta */
 		if (time_before(nextevt, basej))
 			nextevt = basej;
 		expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
 	}
-	raw_spin_unlock(&base->lock);
+	raw_spin_unlock(&base_global->lock);
+	raw_spin_unlock(&base_local->lock);
 
 	return cmp_next_hrtimer_event(basem, expires);
 }
@@ -2033,15 +2071,14 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
  */
 void timer_clear_idle(void)
 {
-	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
-
 	/*
 	 * We do this unlocked. The worst outcome is a remote enqueue sending
 	 * a pointless IPI, but taking the lock would just make the window for
 	 * sending the IPI a few instructions smaller for the cost of taking
 	 * the lock in the exit from idle path.
 	 */
-	base->is_idle = false;
+	__this_cpu_write(timer_bases[BASE_LOCAL].is_idle, false);
+	__this_cpu_write(timer_bases[BASE_GLOBAL].is_idle, false);
 }
 #endif
 
@@ -2087,11 +2124,13 @@ static inline void __run_timers(struct timer_base *base)
  */
 static __latent_entropy void run_timer_softirq(struct softirq_action *h)
 {
-	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
+	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_LOCAL]);
 
 	__run_timers(base);
-	if (IS_ENABLED(CONFIG_NO_HZ_COMMON))
+	if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) {
+		__run_timers(this_cpu_ptr(&timer_bases[BASE_GLOBAL]));
 		__run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
+	}
 }
 
 /*
@@ -2099,7 +2138,7 @@ static __latent_entropy void run_timer_softirq(struct softirq_action *h)
  */
 static void run_local_timers(void)
 {
-	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
+	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_LOCAL]);
 
 	hrtimer_run_queues();
 
-- 
2.30.2


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

* [PATCH v7 13/21] timer: Retrieve next expiry of pinned/non-pinned timers separately
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (11 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 12/21] timer: Keep the pinned timers separate from the others Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 14/21] timer: Split out "get next timer interrupt" functionality Anna-Maria Behnsen
                   ` (7 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Richard Cochran,
	Frederic Weisbecker

For the conversion of the NOHZ timer placement to a pull at expiry time
model it's required to have separate expiry times for the pinned and the
non-pinned (movable) timers. Therefore struct timer_events is introduced.

No functional change

Originally-by: Richard Cochran (linutronix GmbH) <richardcochran@gmail.com>
Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
---
 kernel/time/timer.c | 45 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 40 insertions(+), 5 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 010c8877fa85..e4b50760da3f 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -221,6 +221,11 @@ struct timer_base {
 
 static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
 
+struct timer_events {
+	u64	local;
+	u64	global;
+};
+
 #ifdef CONFIG_NO_HZ_COMMON
 
 static DEFINE_STATIC_KEY_FALSE(timers_nohz_active);
@@ -1997,17 +2002,17 @@ static void forward_base_clk(struct timer_base *base, unsigned long nextevt,
  */
 u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 {
+	struct timer_events tevt = { .local = KTIME_MAX, .global = KTIME_MAX };
 	unsigned long nextevt, nextevt_local, nextevt_global;
 	struct timer_base *base_local, *base_global;
 	bool local_first, is_idle;
-	u64 expires = KTIME_MAX;
 
 	/*
 	 * 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()))
-		return expires;
+		return tevt.local;
 
 	base_local = this_cpu_ptr(&timer_bases[BASE_LOCAL]);
 	base_global = this_cpu_ptr(&timer_bases[BASE_GLOBAL]);
@@ -2052,16 +2057,46 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 	/* We need to mark both bases in sync */
 	base_local->is_idle = base_global->is_idle = is_idle;
 
-	if (base_local->timers_pending || base_global->timers_pending) {
+	/*
+	 * If the bases are not marked idle, i.e one of the events is at
+	 * max. one tick away, then the CPU can't go into a NOHZ idle
+	 * sleep. Use the earlier event of both and store it in the local
+	 * expiry value. The next global event is irrelevant in this case
+	 * and can be left as KTIME_MAX. CPU will wakeup on time.
+	 */
+	if (!is_idle) {
 		/* If we missed a tick already, force 0 delta */
 		if (time_before(nextevt, basej))
 			nextevt = basej;
-		expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
+		tevt.local = basem + (u64)(nextevt - basej) * TICK_NSEC;
+		goto unlock;
 	}
+
+	/*
+	 * If the bases are marked idle, i.e. the next event on both the
+	 * local and the global queue are farther away than a tick,
+	 * evaluate both bases. No need to check whether one of the bases
+	 * has an already expired timer as this is caught by the !is_idle
+	 * condition above.
+	 */
+	if (base_local->timers_pending)
+		tevt.local = basem + (u64)(nextevt_local - basej) * TICK_NSEC;
+
+	/*
+	 * If the local queue expires first, then the global event can be
+	 * ignored. The CPU wakes up before that. If the global queue is
+	 * empty, nothing to do either.
+	 */
+	if (!local_first && base_global->timers_pending)
+		tevt.global = basem + (u64)(nextevt_global - basej) * TICK_NSEC;
+
+unlock:
 	raw_spin_unlock(&base_global->lock);
 	raw_spin_unlock(&base_local->lock);
 
-	return cmp_next_hrtimer_event(basem, expires);
+	tevt.local = min_t(u64, tevt.local, tevt.global);
+
+	return cmp_next_hrtimer_event(basem, tevt.local);
 }
 
 /**
-- 
2.30.2


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

* [PATCH v7 14/21] timer: Split out "get next timer interrupt" functionality
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (12 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 13/21] timer: Retrieve next expiry of pinned/non-pinned timers separately Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 15/21] timer: Add get next timer interrupt functionality for remote CPUs Anna-Maria Behnsen
                   ` (6 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Frederic Weisbecker

The functionality for getting the next timer interrupt in
get_next_timer_interrupt() is split into a separate function
fetch_next_timer_interrupt() to be usable by other call sites.

This is preparatory work for the conversion of the NOHZ timer
placement to a pull at expiry time model. No functional change.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
---
v6: s/splitted/split
v5: Update commit message
v4: Fix typo in comment
---
 kernel/time/timer.c | 91 +++++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 41 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index e4b50760da3f..4bb6c168d106 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1973,6 +1973,46 @@ static unsigned long next_timer_interrupt(struct timer_base *base)
 	return base->next_expiry;
 }
 
+static unsigned long fetch_next_timer_interrupt(struct timer_base *base_local,
+						struct timer_base *base_global,
+						unsigned long basej, u64 basem,
+						struct timer_events *tevt)
+{
+	unsigned long nextevt_local, nextevt_global;
+	bool local_first;
+
+	nextevt_local = next_timer_interrupt(base_local);
+	nextevt_global = next_timer_interrupt(base_global);
+
+	/*
+	 * Check whether the local event is expiring before or at the same
+	 * time as the global event.
+	 *
+	 * Note, that nextevt_global and nextevt_local might be based on
+	 * different base->clk values. So it's not guaranteed that
+	 * comparing with empty bases results in a correct local_first.
+	 */
+	if (base_local->timers_pending && base_global->timers_pending)
+		local_first = time_before_eq(nextevt_local, nextevt_global);
+	else
+		local_first = base_local->timers_pending;
+
+	/*
+	 * Update tevt->* values:
+	 *
+	 * If the local queue expires first, then the global event can
+	 * be ignored. If the global queue is empty, nothing to do
+	 * either.
+	 */
+	if (!local_first && base_global->timers_pending)
+		tevt->global = basem + (u64)(nextevt_global - basej) * TICK_NSEC;
+
+	if (base_local->timers_pending)
+		tevt->local = basem + (u64)(nextevt_local - basej) * TICK_NSEC;
+
+	return local_first ? nextevt_local : nextevt_global;
+}
+
 /*
  * Forward base clock is done only when @basej is past base->clk, otherwise
  * base-clk might be rewind.
@@ -2005,7 +2045,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 	struct timer_events tevt = { .local = KTIME_MAX, .global = KTIME_MAX };
 	unsigned long nextevt, nextevt_local, nextevt_global;
 	struct timer_base *base_local, *base_global;
-	bool local_first, is_idle;
+	bool is_idle;
 
 	/*
 	 * Pretend that there is no timer pending if the cpu is offline.
@@ -2020,8 +2060,11 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 	raw_spin_lock(&base_local->lock);
 	raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING);
 
-	nextevt_local = next_timer_interrupt(base_local);
-	nextevt_global = next_timer_interrupt(base_global);
+	nextevt = fetch_next_timer_interrupt(base_local, base_global,
+					     basej, basem, &tevt);
+
+	nextevt_local = base_local->next_expiry;
+	nextevt_global = base_global->next_expiry;
 
 	/*
 	 * We have a fresh next event. Check whether we can forward the
@@ -2030,21 +2073,6 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 	forward_base_clk(base_local, nextevt_local, basej);
 	forward_base_clk(base_global, nextevt_global, basej);
 
-	/*
-	 * Check whether the local event is expiring before or at the same
-	 * time as the global event.
-	 *
-	 * Note, that nextevt_global and nextevt_local might be based on
-	 * different base->clk values. So it's not guaranteed that
-	 * comparing with empty bases results in a correct local_first.
-	 */
-	if (base_local->timers_pending && base_global->timers_pending)
-		local_first = time_before_eq(nextevt_local, nextevt_global);
-	else
-		local_first = base_local->timers_pending;
-
-	nextevt = local_first ? nextevt_local : nextevt_global;
-
 	/*
 	 * Bases are idle if the next event is more than a tick away. Also
 	 * the tick is stopped so any added timer must forward the base clk
@@ -2057,6 +2085,9 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 	/* We need to mark both bases in sync */
 	base_local->is_idle = base_global->is_idle = is_idle;
 
+	raw_spin_unlock(&base_global->lock);
+	raw_spin_unlock(&base_local->lock);
+
 	/*
 	 * If the bases are not marked idle, i.e one of the events is at
 	 * max. one tick away, then the CPU can't go into a NOHZ idle
@@ -2069,31 +2100,9 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 		if (time_before(nextevt, basej))
 			nextevt = basej;
 		tevt.local = basem + (u64)(nextevt - basej) * TICK_NSEC;
-		goto unlock;
+		tevt.global = KTIME_MAX;
 	}
 
-	/*
-	 * If the bases are marked idle, i.e. the next event on both the
-	 * local and the global queue are farther away than a tick,
-	 * evaluate both bases. No need to check whether one of the bases
-	 * has an already expired timer as this is caught by the !is_idle
-	 * condition above.
-	 */
-	if (base_local->timers_pending)
-		tevt.local = basem + (u64)(nextevt_local - basej) * TICK_NSEC;
-
-	/*
-	 * If the local queue expires first, then the global event can be
-	 * ignored. The CPU wakes up before that. If the global queue is
-	 * empty, nothing to do either.
-	 */
-	if (!local_first && base_global->timers_pending)
-		tevt.global = basem + (u64)(nextevt_global - basej) * TICK_NSEC;
-
-unlock:
-	raw_spin_unlock(&base_global->lock);
-	raw_spin_unlock(&base_local->lock);
-
 	tevt.local = min_t(u64, tevt.local, tevt.global);
 
 	return cmp_next_hrtimer_event(basem, tevt.local);
-- 
2.30.2


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

* [PATCH v7 15/21] timer: Add get next timer interrupt functionality for remote CPUs
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (13 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 14/21] timer: Split out "get next timer interrupt" functionality Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-06-07 14:55   ` Frederic Weisbecker
  2023-05-24  7:06 ` [PATCH v7 16/21] timer: Restructure internal locking Anna-Maria Behnsen
                   ` (5 subsequent siblings)
  20 siblings, 1 reply; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen

To prepare for the conversion of the NOHZ timer placement to a pull at
expiry time model it's required to have functionality available getting the
next timer interrupt on a remote CPU.

Locking of the timer bases and getting the information for the next timer
interrupt functionality is split into separate functions. This is required
to be compliant with lock ordering when the new model is in place.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
v7:
 - Move functions into CONFIG_SMP && CONFIG_NO_HZ_COMMON section
 - change lock, fetch functions to be unconditional
 - split out unlock function into a separate function

v6:
 - introduce timer_lock_remote_bases() to fix race
---
 kernel/time/tick-internal.h | 10 +++++
 kernel/time/timer.c         | 77 ++++++++++++++++++++++++++++++++++---
 2 files changed, 82 insertions(+), 5 deletions(-)

diff --git a/kernel/time/tick-internal.h b/kernel/time/tick-internal.h
index 649f2b48e8f0..9b738117b2ab 100644
--- a/kernel/time/tick-internal.h
+++ b/kernel/time/tick-internal.h
@@ -8,6 +8,11 @@
 #include "timekeeping.h"
 #include "tick-sched.h"
 
+struct timer_events {
+	u64	local;
+	u64	global;
+};
+
 #ifdef CONFIG_GENERIC_CLOCKEVENTS
 
 # define TICK_DO_TIMER_NONE	-1
@@ -155,6 +160,11 @@ extern unsigned long tick_nohz_active;
 extern void timers_update_nohz(void);
 # ifdef CONFIG_SMP
 extern struct static_key_false timers_migration_enabled;
+extern void fetch_next_timer_interrupt_remote(unsigned long basej, u64 basem,
+					      struct timer_events *tevt,
+					      unsigned int cpu);
+extern void timer_lock_remote_bases(unsigned int cpu);
+extern void timer_unlock_remote_bases(unsigned int cpu);
 # endif
 #else /* CONFIG_NO_HZ_COMMON */
 static inline void timers_update_nohz(void) { }
diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 4bb6c168d106..564001b98098 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -221,11 +221,6 @@ struct timer_base {
 
 static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
 
-struct timer_events {
-	u64	local;
-	u64	global;
-};
-
 #ifdef CONFIG_NO_HZ_COMMON
 
 static DEFINE_STATIC_KEY_FALSE(timers_nohz_active);
@@ -2013,6 +2008,78 @@ static unsigned long fetch_next_timer_interrupt(struct timer_base *base_local,
 	return local_first ? nextevt_local : nextevt_global;
 }
 
+# ifdef CONFIG_SMP
+/**
+ * fetch_next_timer_interrupt_remote
+ * @basej:	base time jiffies
+ * @basem:	base time clock monotonic
+ * @tevt:	Pointer to the storage for the expiry values
+ * @cpu:	Remote CPU
+ *
+ * Stores the next pending local and global timer expiry values in the
+ * struct pointed to by @tevt. If a queue is empty the corresponding
+ * field is set to KTIME_MAX. If local event expires before global
+ * event, global event is set to KTIME_MAX as well.
+ *
+ * Caller needs to make sure timer base locks are held (use
+ * timer_lock_remote_bases() for this purpose). Caller must make sure
+ * interrupts are reopened, if required.
+ */
+void fetch_next_timer_interrupt_remote(unsigned long basej, u64 basem,
+				       struct timer_events *tevt,
+				       unsigned int cpu)
+{
+	struct timer_base *base_local, *base_global;
+
+	/* Preset local / global events */
+	tevt->local = tevt->global = KTIME_MAX;
+
+	base_local = per_cpu_ptr(&timer_bases[BASE_LOCAL], cpu);
+	base_global = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu);
+
+	lockdep_assert_held(&base_local->lock);
+	lockdep_assert_held(&base_global->lock);
+
+	fetch_next_timer_interrupt(base_local, base_global, basej, basem, tevt);
+}
+
+/**
+ * timer_unlock_remote_bases - unlock timer bases of cpu
+ * @cpu:	Remote CPU
+ *
+ * Unlocks the remote timer bases.
+ */
+void timer_unlock_remote_bases(unsigned int cpu)
+{
+	struct timer_base *base_local, *base_global;
+
+	base_local = per_cpu_ptr(&timer_bases[BASE_LOCAL], cpu);
+	base_global = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu);
+
+	raw_spin_unlock(&base_global->lock);
+	raw_spin_unlock(&base_local->lock);
+}
+
+/**
+ * timer_lock_remote_bases - lock timer bases of cpu
+ * @cpu:	Remote CPU
+ *
+ * Locks the remote timer bases.
+ */
+void timer_lock_remote_bases(unsigned int cpu)
+{
+	struct timer_base *base_local, *base_global;
+
+	base_local = per_cpu_ptr(&timer_bases[BASE_LOCAL], cpu);
+	base_global = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu);
+
+	lockdep_assert_irqs_disabled();
+
+	raw_spin_lock(&base_local->lock);
+	raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING);
+}
+# endif /* CONFIG_SMP */
+
 /*
  * Forward base clock is done only when @basej is past base->clk, otherwise
  * base-clk might be rewind.
-- 
2.30.2


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

* [PATCH v7 16/21] timer: Restructure internal locking
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (14 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 15/21] timer: Add get next timer interrupt functionality for remote CPUs Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 17/21] timer: Check if timers base is handled already Anna-Maria Behnsen
                   ` (4 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Richard Cochran (linutronix GmbH),
	Anna-Maria Behnsen

From: "Richard Cochran (linutronix GmbH)" <richardcochran@gmail.com>

Move the locking out from __run_timers() to the call sites, so the
protected section can be extended at the call site. Preparatory patch for
changing the NOHZ timer placement to a pull at expiry time model.

No functional change.

Signed-off-by: Richard Cochran (linutronix GmbH) <richardcochran@gmail.com>
Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
 kernel/time/timer.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 564001b98098..20106dad531a 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -2202,11 +2202,7 @@ static inline void __run_timers(struct timer_base *base)
 	struct hlist_head heads[LVL_DEPTH];
 	int levels;
 
-	if (time_before(jiffies, base->next_expiry))
-		return;
-
-	timer_base_lock_expiry(base);
-	raw_spin_lock_irq(&base->lock);
+	lockdep_assert_held(&base->lock);
 
 	while (time_after_eq(jiffies, base->clk) &&
 	       time_after_eq(jiffies, base->next_expiry)) {
@@ -2226,21 +2222,36 @@ static inline void __run_timers(struct timer_base *base)
 		while (levels--)
 			expire_timers(base, heads + levels);
 	}
+}
+
+static void __run_timer_base(struct timer_base *base)
+{
+	if (time_before(jiffies, base->next_expiry))
+		return;
+
+	timer_base_lock_expiry(base);
+	raw_spin_lock_irq(&base->lock);
+	__run_timers(base);
 	raw_spin_unlock_irq(&base->lock);
 	timer_base_unlock_expiry(base);
 }
 
+static void run_timer_base(int index)
+{
+	struct timer_base *base = this_cpu_ptr(&timer_bases[index]);
+
+	__run_timer_base(base);
+}
+
 /*
  * This function runs timers and the timer-tq in bottom half context.
  */
 static __latent_entropy void run_timer_softirq(struct softirq_action *h)
 {
-	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_LOCAL]);
-
-	__run_timers(base);
+	run_timer_base(BASE_LOCAL);
 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) {
-		__run_timers(this_cpu_ptr(&timer_bases[BASE_GLOBAL]));
-		__run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
+		run_timer_base(BASE_GLOBAL);
+		run_timer_base(BASE_DEF);
 	}
 }
 
-- 
2.30.2


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

* [PATCH v7 17/21] timer: Check if timers base is handled already
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (15 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 16/21] timer: Restructure internal locking Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 18/21] tick/sched: Split out jiffies update helper function Anna-Maria Behnsen
                   ` (3 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen

Due to the conversion of the NOHZ timer placement to a pull at expiry
time model, the per CPU timer bases with non pinned timers are no
longer handled only by the local CPU. In case a remote CPU already
expires the non pinned timers base of the local cpu, nothing more
needs to be done by the local CPU. A check at the begin of the expire
timers routine is required, because timer base lock is dropped before
executing the timer callback function.

This is a preparatory work, but has no functional impact right now.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
v6: Drop double negation
---
 kernel/time/timer.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 20106dad531a..c2d66fd4638f 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -2204,6 +2204,9 @@ static inline void __run_timers(struct timer_base *base)
 
 	lockdep_assert_held(&base->lock);
 
+	if (base->running_timer)
+		return;
+
 	while (time_after_eq(jiffies, base->clk) &&
 	       time_after_eq(jiffies, base->next_expiry)) {
 		levels = collect_expired_timers(base, heads);
-- 
2.30.2


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

* [PATCH v7 18/21] tick/sched: Split out jiffies update helper function
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (16 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 17/21] timer: Check if timers base is handled already Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 19/21] timer: Implement the hierarchical pull model Anna-Maria Behnsen
                   ` (2 subsequent siblings)
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Richard Cochran (linutronix GmbH),
	Anna-Maria Behnsen

From: "Richard Cochran (linutronix GmbH)" <richardcochran@gmail.com>

The logic to get the time of the last jiffies update will be needed by
the timer pull model as well.

Move the code into a global function in anticipation of the new caller.

No functional change.

Signed-off-by: Richard Cochran (linutronix GmbH) <richardcochran@gmail.com>
Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
 kernel/time/tick-internal.h |  1 +
 kernel/time/tick-sched.c    | 18 +++++++++++++++---
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/kernel/time/tick-internal.h b/kernel/time/tick-internal.h
index 9b738117b2ab..34d44fb50c81 100644
--- a/kernel/time/tick-internal.h
+++ b/kernel/time/tick-internal.h
@@ -158,6 +158,7 @@ static inline void tick_nohz_init(void) { }
 #ifdef CONFIG_NO_HZ_COMMON
 extern unsigned long tick_nohz_active;
 extern void timers_update_nohz(void);
+extern u64 get_jiffies_update(unsigned long *basej);
 # ifdef CONFIG_SMP
 extern struct static_key_false timers_migration_enabled;
 extern void fetch_next_timer_interrupt_remote(unsigned long basej, u64 basem,
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 62836490ba4d..847c459c01f2 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -787,18 +787,30 @@ static inline bool local_timer_softirq_pending(void)
 	return local_softirq_pending() & BIT(TIMER_SOFTIRQ);
 }
 
-static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
+/*
+ * Read jiffies and the time when jiffies were updated last
+ */
+u64 get_jiffies_update(unsigned long *basej)
 {
-	u64 basemono, next_tick, delta, expires;
 	unsigned long basejiff;
 	unsigned int seq;
+	u64 basemono;
 
-	/* Read jiffies and the time when jiffies were updated last */
 	do {
 		seq = read_seqcount_begin(&jiffies_seq);
 		basemono = last_jiffies_update;
 		basejiff = jiffies;
 	} while (read_seqcount_retry(&jiffies_seq, seq));
+	*basej = basejiff;
+	return basemono;
+}
+
+static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
+{
+	u64 basemono, next_tick, delta, expires;
+	unsigned long basejiff;
+
+	basemono = get_jiffies_update(&basejiff);
 	ts->last_jiffies = basejiff;
 	ts->timer_expires_base = basemono;
 
-- 
2.30.2


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

* [PATCH v7 19/21] timer: Implement the hierarchical pull model
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (17 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 18/21] tick/sched: Split out jiffies update helper function Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-06-06 20:10   ` Frederic Weisbecker
                     ` (4 more replies)
  2023-05-24  7:06 ` [PATCH v7 20/21] timer_migration: Add tracepoints Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 21/21] timer: Always queue timers on the local CPU Anna-Maria Behnsen
  20 siblings, 5 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen

Placing timers at enqueue time on a target CPU based on dubious heuristics
does not make any sense:

 1) Most timer wheel timers are canceled or rearmed before they expire.

 2) The heuristics to predict which CPU will be busy when the timer expires
    are wrong by definition.

So placing the timers at enqueue wastes precious cycles.

The proper solution to this problem is to always queue the timers on the
local CPU and allow the non pinned timers to be pulled onto a busy CPU at
expiry time.

Therefore split the timer storage into local pinned and global timers:
Local pinned timers are always expired on the CPU on which they have been
queued. Global timers can be expired on any CPU.

As long as a CPU is busy it expires both local and global timers. When a
CPU goes idle it arms for the first expiring local timer. If the first
expiring pinned (local) timer is before the first expiring movable timer,
then no action is required because the CPU will wake up before the first
movable timer expires. If the first expiring movable timer is before the
first expiring pinned (local) timer, then this timer is queued into a idle
timerqueue and eventually expired by some other active CPU.

To avoid global locking the timerqueues are implemented as a hierarchy. The
lowest level of the hierarchy holds the CPUs. The CPUs are associated to
groups of 8, which are separated per node. If more than one CPU group
exist, then a second level in the hierarchy collects the groups. Depending
on the size of the system more than 2 levels are required. Each group has a
"migrator" which checks the timerqueue during the tick for remote expirable
timers.

If the last CPU in a group goes idle it reports the first expiring event in
the group up to the next group(s) in the hierarchy. If the last CPU goes
idle it arms its timer for the first system wide expiring timer to ensure
that no timer event is missed.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
v7:
  - Review remarks of Frederic and bigeasy:
    - change logic in tmigr_handle_remote_cpu()
    - s/kzalloc/kcalloc
    - move timer_expire_remote() into NO_HZ_COMMON && SMP config section
    - drop DBG_BUG_ON() makro and use only WARN_ON_ONCE()
    - remove leftovers from sibling logic during setup
  - Move timer_expire_remote() into tick-internal.h
  - Add documentation section about "Required event and timerqueue update
    after remote expiry"
  - Fix fallout of kernel test robot

v6:
  - Fix typos
  - Review remarks of Peter Zijlstra (locking, struct member cleanup, use
    atomic_try_cmpxchg(), update struct member descriptions)
  - Fix race in tmigr_handle_remote_cpu() (Frederic Weisbecker)

v5:
  - Review remarks of Frederic
  - Return nextevt when CPU is marked offline in timer migration hierarchy
    instead of KTIME_MAX
  - Fix update of group events issue, after remote expiring

v4:
  - Fold typo fix in comment into proper patch "timer: Split out "get next
    timer interrupt" functionality"
  - Update wrong comment for tmigr_state union definition
  - Fix fallout of kernel test robot
---
 include/linux/cpuhotplug.h    |    1 +
 kernel/time/Makefile          |    3 +
 kernel/time/tick-internal.h   |    1 +
 kernel/time/timer.c           |   73 +-
 kernel/time/timer_migration.c | 1411 +++++++++++++++++++++++++++++++++
 kernel/time/timer_migration.h |  136 ++++
 6 files changed, 1617 insertions(+), 8 deletions(-)
 create mode 100644 kernel/time/timer_migration.c
 create mode 100644 kernel/time/timer_migration.h

diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h
index 0f1001dca0e0..a32395b128de 100644
--- a/include/linux/cpuhotplug.h
+++ b/include/linux/cpuhotplug.h
@@ -242,6 +242,7 @@ enum cpuhp_state {
 	CPUHP_AP_PERF_POWERPC_HV_24x7_ONLINE,
 	CPUHP_AP_PERF_POWERPC_HV_GPCI_ONLINE,
 	CPUHP_AP_PERF_CSKY_ONLINE,
+	CPUHP_AP_TMIGR_ONLINE,
 	CPUHP_AP_WATCHDOG_ONLINE,
 	CPUHP_AP_WORKQUEUE_ONLINE,
 	CPUHP_AP_RANDOM_ONLINE,
diff --git a/kernel/time/Makefile b/kernel/time/Makefile
index 7e875e63ff3b..4af2a264a160 100644
--- a/kernel/time/Makefile
+++ b/kernel/time/Makefile
@@ -17,6 +17,9 @@ endif
 obj-$(CONFIG_GENERIC_SCHED_CLOCK)		+= sched_clock.o
 obj-$(CONFIG_TICK_ONESHOT)			+= tick-oneshot.o tick-sched.o
 obj-$(CONFIG_LEGACY_TIMER_TICK)			+= tick-legacy.o
+ifeq ($(CONFIG_SMP),y)
+ obj-$(CONFIG_NO_HZ_COMMON)			+= timer_migration.o
+endif
 obj-$(CONFIG_HAVE_GENERIC_VDSO)			+= vsyscall.o
 obj-$(CONFIG_DEBUG_FS)				+= timekeeping_debug.o
 obj-$(CONFIG_TEST_UDELAY)			+= test_udelay.o
diff --git a/kernel/time/tick-internal.h b/kernel/time/tick-internal.h
index 34d44fb50c81..f5020473cc93 100644
--- a/kernel/time/tick-internal.h
+++ b/kernel/time/tick-internal.h
@@ -166,6 +166,7 @@ extern void fetch_next_timer_interrupt_remote(unsigned long basej, u64 basem,
 					      unsigned int cpu);
 extern void timer_lock_remote_bases(unsigned int cpu);
 extern void timer_unlock_remote_bases(unsigned int cpu);
+extern void timer_expire_remote(unsigned int cpu);
 # endif
 #else /* CONFIG_NO_HZ_COMMON */
 static inline void timers_update_nohz(void) { }
diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index c2d66fd4638f..3a8c2c88f845 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -53,6 +53,7 @@
 #include <asm/io.h>
 
 #include "tick-internal.h"
+#include "timer_migration.h"
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/timer.h>
@@ -2078,6 +2079,21 @@ void timer_lock_remote_bases(unsigned int cpu)
 	raw_spin_lock(&base_local->lock);
 	raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING);
 }
+
+static void __run_timer_base(struct timer_base *base);
+
+/**
+ * timer_expire_remote - expire global timers of cpu
+ * @cpu:	Remote CPU
+ *
+ * Expire timers of global base of remote CPU.
+ */
+void timer_expire_remote(unsigned int cpu)
+{
+	struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu);
+
+	__run_timer_base(base);
+}
 # endif /* CONFIG_SMP */
 
 /*
@@ -2104,8 +2120,11 @@ static void forward_base_clk(struct timer_base *base, unsigned long nextevt,
  * idle. Idle marking of timer bases is allowed only to be done by CPU
  * itself.
  *
- * Returns the tick aligned clock monotonic time of the next pending
- * timer or KTIME_MAX if no timer is pending.
+ * Returns the tick aligned clock monotonic time of the next pending timer
+ * or KTIME_MAX if no timer is pending. If timer of global base was queued
+ * into timer migration hierarchy, first global timer is not taken into
+ * account. If it was the last CPU of timer migration hierarchy going idle,
+ * first global event is taken into account.
  */
 u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 {
@@ -2149,6 +2168,40 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 	 */
 	is_idle = time_after(nextevt, basej + 1);
 
+	if (is_idle) {
+		u64 next_tmigr;
+
+		/*
+		 * Enqueue first global timer into timer migration
+		 * hierarchy, afterwards tevt.global is no longer used.
+		 */
+		next_tmigr = tmigr_cpu_deactivate(tevt.global);
+
+		/*
+		 * If CPU is the last going idle in timer migration
+		 * hierarchy, make sure CPU will wake up in time to handle
+		 * remote timers. next_tmigr == KTIME_MAX if other CPUs are
+		 * still active.
+		 */
+		if (next_tmigr < tevt.local) {
+			u64 tmp;
+
+			/* If we missed a tick already, force 0 delta */
+			if (next_tmigr < basem)
+				next_tmigr = basem;
+
+			tmp = div_u64(next_tmigr - basem, TICK_NSEC);
+
+			nextevt = basej + (unsigned long)tmp;
+			tevt.local = next_tmigr;
+			is_idle = time_after(nextevt, basej + 1);
+		}
+		/*
+		 * Update of nextevt is not required in an else path, as it
+		 * is revisited in !is_idle path only.
+		 */
+	}
+
 	/* We need to mark both bases in sync */
 	base_local->is_idle = base_global->is_idle = is_idle;
 
@@ -2159,19 +2212,16 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 	 * If the bases are not marked idle, i.e one of the events is at
 	 * max. one tick away, then the CPU can't go into a NOHZ idle
 	 * sleep. Use the earlier event of both and store it in the local
-	 * expiry value. The next global event is irrelevant in this case
-	 * and can be left as KTIME_MAX. CPU will wakeup on time.
+	 * expiry value. tevt.global update is superfluous and is
+	 * ignored. CPU will wakeup on time.
 	 */
 	if (!is_idle) {
 		/* If we missed a tick already, force 0 delta */
 		if (time_before(nextevt, basej))
 			nextevt = basej;
 		tevt.local = basem + (u64)(nextevt - basej) * TICK_NSEC;
-		tevt.global = KTIME_MAX;
 	}
 
-	tevt.local = min_t(u64, tevt.local, tevt.global);
-
 	return cmp_next_hrtimer_event(basem, tevt.local);
 }
 
@@ -2190,6 +2240,9 @@ void timer_clear_idle(void)
 	 */
 	__this_cpu_write(timer_bases[BASE_LOCAL].is_idle, false);
 	__this_cpu_write(timer_bases[BASE_GLOBAL].is_idle, false);
+
+	/* Activate without holding the timer_base->lock */
+	tmigr_cpu_activate();
 }
 #endif
 
@@ -2255,6 +2308,9 @@ static __latent_entropy void run_timer_softirq(struct softirq_action *h)
 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) {
 		run_timer_base(BASE_GLOBAL);
 		run_timer_base(BASE_DEF);
+
+		if (is_timers_nohz_active())
+			tmigr_handle_remote();
 	}
 }
 
@@ -2269,7 +2325,8 @@ static void run_local_timers(void)
 
 	for (int i = 0; i < NR_BASES; i++, base++) {
 		/* Raise the softirq only if required. */
-		if (time_after_eq(jiffies, base->next_expiry)) {
+		if (time_after_eq(jiffies, base->next_expiry) ||
+		    (i == BASE_DEF && tmigr_requires_handle_remote())) {
 			raise_softirq(TIMER_SOFTIRQ);
 			return;
 		}
diff --git a/kernel/time/timer_migration.c b/kernel/time/timer_migration.c
new file mode 100644
index 000000000000..eab58bc98c7f
--- /dev/null
+++ b/kernel/time/timer_migration.c
@@ -0,0 +1,1411 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Infrastructure for migratable timers
+ *
+ * Copyright(C) 2022 linutronix GmbH
+ */
+#include <linux/cpuhotplug.h>
+#include <linux/slab.h>
+#include <linux/smp.h>
+#include <linux/spinlock.h>
+#include <linux/timerqueue.h>
+#include <trace/events/ipi.h>
+
+#include "timer_migration.h"
+#include "tick-internal.h"
+
+/*
+ * The timer migration mechanism is built on a hierarchy of groups. The
+ * lowest level group contains CPUs, the next level groups of CPU groups
+ * and so forth. The CPU groups are kept per node so for the normal case
+ * lock contention won't happen across nodes. Depending on the number of
+ * CPUs per node even the next level might be kept as groups of CPU groups
+ * per node and only the levels above cross the node topology.
+ *
+ * Example topology for a two node system with 24 CPUs each.
+ *
+ * LVL 2			[GRP2:0]
+ *			      GRP1:0 = GRP1:M
+ *
+ * LVL 1            [GRP1:0]		         [GRP1:1]
+ *	         GRP0:0 - GRP0:2	      GRP0:3 - GRP0:5
+ *
+ * LVL 0  [GRP0:0]  [GRP0:1]  [GRP0:2]  [GRP0:3]  [GRP0:4]  [GRP0:5]
+ * CPUS     0-7       8-15     16-23     24-31	   32-39     40-47
+ *
+ * The groups hold a timer queue of events sorted by expiry time. These
+ * queues are updated when CPUs go in idle. When they come out of idle
+ * ignore bit of events is set.
+ *
+ * Each group has a designated migrator CPU/group as long as a CPU/group is
+ * active in the group. This designated role is necessary to avoid that all
+ * active CPUs in a group try to migrate expired timers from other cpus,
+ * which would result in massive lock bouncing.
+ *
+ * When a CPU is awake, it checks in it's own timer tick the group
+ * hierarchy up to the point where it is assigned the migrator role or if
+ * no CPU is active, it also checks the groups where no migrator is set
+ * (TMIGR_NONE).
+ *
+ * If it finds expired timers in one of the group queues it pulls them over
+ * from the idle CPU and runs the timer function. After that it updates the
+ * group and the parent groups if required.
+ *
+ * CPUs which go idle arm their CPU local timer hardware for the next local
+ * (pinned) timer event. If the next migratable timer expires after the
+ * next local timer or the CPU has no migratable timer pending then the
+ * CPU does not queue an event in the LVL0 group. If the next migratable
+ * timer expires before the next local timer then the CPU queues that timer
+ * in the LVL0 group. In both cases the CPU marks itself idle in the LVL0
+ * group.
+ *
+ * When CPU comes out of idle and when a group has at least a single active
+ * child, the ignore flag of the tmigr_event is set. This indicates, that
+ * the event is ignored even if it is still enqueued in the parent groups
+ * timer queue. It will be removed when touching the timer queue the next
+ * time. This spares locking in active path as the lock protects (after
+ * setup) only event information. For more information about locking,
+ * please read the section "Locking rules".
+ *
+ * If the CPU is the migrator of the group then it delegates that role to
+ * the next active CPU in the group or sets migrator to TMIGR_NONE when
+ * there is no active CPU in the group. This delegation needs to be
+ * propagated up the hierarchy so hand over from other leaves can happen at
+ * all hierarchy levels w/o doing a search.
+ *
+ * When the last CPU in the system goes idle, then it drops all migrator
+ * duties up to the top level of the hierarchy (LVL2 in the example). It
+ * then has to make sure, that it arms it's own local hardware timer for
+ * the earliest event in the system.
+ *
+ *
+ * Lifetime rules:
+ * ---------------
+ *
+ * The groups are built up at init time or when CPUs come online. They are
+ * not destroyed when a group becomes empty due to offlining. The group
+ * just won't participate in the hierachary management anymore. Destroying
+ * groups would result in interesting race conditions which would just make
+ * the whole mechanism slow and complex.
+ *
+ *
+ * Locking rules:
+ * --------------
+ *
+ * For setting up new groups and handling events it's required to lock both
+ * child and parent group. The lock odering is always bottom up. This also
+ * includes the per CPU locks in struct tmigr_cpu. For updating migrator
+ * and active CPU/group information atomic_try_cmpxchg() is used instead
+ * and only per CPU tmigr_cpu->lock is held.
+ *
+ * During setup of groups tmigr_level_list is required. It is protected by
+ * tmigr_mutex.
+ *
+ * When timer_base->lock as well as tmigr related locks are required, lock
+ * ordering is: first timer_base->lock, afterwards tmigr related locks.
+ *
+ *
+ * Protection of tmigr group state information:
+ * --------------------------------------------
+ *
+ * The state information with the list of active children and migrator needs
+ * to be protected by a sequence counter. It prevents a race when updates
+ * in child groups are propagated in changed order. The following scenario
+ * describes what happens without updating the sequence counter:
+ *
+ * Therefore, let's take three groups and four CPUs (CPU2 and CPU3 as well
+ * as GRP0:1 will not change during the scenario):
+ *
+ *    LVL 1            [GRP1:0]
+ *                     migrator = GRP0:1
+ *                     active   = GRP0:0, GRP0:1
+ *                   /                \
+ *    LVL 0  [GRP0:0]                  [GRP0:1]
+ *           migrator = CPU0           migrator = CPU2
+ *           active   = CPU0           active   = CPU2
+ *              /         \                /         \
+ *    CPUs     0           1              2           3
+ *             active      idle           active      idle
+ *
+ *
+ * 1. CPU0 goes idle (changes are updated in GRP0:0; afterwards current
+ *    states of GRP0:0 and GRP1:0 are stored in the data for walking the
+ *    hierarchy):
+ *
+ *    LVL 1            [GRP1:0]
+ *                     migrator = GRP0:1
+ *                     active   = GRP0:0, GRP0:1
+ *                   /                \
+ *    LVL 0  [GRP0:0]                  [GRP0:1]
+ *       --> migrator = TMIGR_NONE     migrator = CPU2
+ *       --> active   =                active   = CPU2
+ *              /         \                /         \
+ *    CPUs     0           1              2           3
+ *         --> idle        idle           active      idle
+ *
+ * 2. CPU1 comes out of idle (changes are update in GRP0:0; afterwards
+ *    current states of GRP0:0 and GRP1:0 are stored in the data for
+ *    walking the hierarchy):
+ *
+ *    LVL 1            [GRP1:0]
+ *                     migrator = GRP0:1
+ *                     active   = GRP0:0, GRP0:1
+ *                   /                \
+ *    LVL 0  [GRP0:0]                  [GRP0:1]
+ *       --> migrator = CPU1           migrator = CPU2
+ *       --> active   = CPU1           active   = CPU2
+ *              /         \                /         \
+ *    CPUs     0           1              2           3
+ *             idle    --> active         active      idle
+ *
+ * 3. Here comes the change of the order: Propagating the changes of step 2
+ *    through the hierarchy to GRP1:0 - nothing to be done, because GRP0:0
+ *    is already up to date.
+ *
+ * 4. Propagating the changes of step 1 through the hierarchy to GRP1:0
+ *
+ *    LVL 1            [GRP1:0]
+ *                 --> migrator = GRP0:1
+ *                 --> active   = GRP0:1
+ *                   /                \
+ *    LVL 0  [GRP0:0]                  [GRP0:1]
+ *           migrator = CPU1           migrator = CPU2
+ *           active   = CPU1           active   = CPU2
+ *              /         \                /         \
+ *    CPUs     0           1              2           3
+ *             idle        active         active      idle
+ *
+ * Now there is a inconsistent overall state because GRP0:0 is active, but
+ * it is marked as idle in the GRP1:0. This is prevented by incrementing
+ * sequence counter whenever changing the state.
+ *
+ *
+ * Required event and timerqueue update after remote expiry:
+ * ---------------------------------------------------------
+ *
+ * After remote expiry of a CPU, a walk through the hierarchy updating
+ * events and timerqueues has to be done when there is a 'new' global timer
+ * of the remote CPU (which is obvious) but also if there is no new global
+ * timer, but the remote CPU is still idle:
+ *
+ * 1. CPU2 is migrator and does the remote expiry in GRP1:0; expiry of
+ *    evt-CPU0 and evt-CPU1 are equal:
+ *
+ *    LVL 1            [GRP1:0]
+ *                     migrator = GRP0:1
+ *                     active   = GRP0:1
+ *                 --> timerqueue = evt-GRP0:0
+ *                   /                \
+ *    LVL 0  [GRP0:0]                  [GRP0:1]
+ *           migrator = TMIGR_NONE     migrator = CPU2
+ *           active   =                active   = CPU2
+ *           groupevt.ignore = 0       groupevt.ignore = 1
+ *           groupevt.cpu = CPU0       groupevt.cpu =
+ *           timerqueue = evt-CPU0,    timerqueue =
+ *                        evt-CPU1
+ *              /         \                /         \
+ *    CPUs     0           1              2           3
+ *             idle        idle           active      idle
+ *
+ * 2. Remove first event of timerqueue in GRP1:0 and expire timers of CPU0
+ *    (see evt-GRP0:0->cpu value):
+ *
+ *    LVL 1            [GRP1:0]
+ *                     migrator = GRP0:1
+ *                     active   = GRP0:1
+ *                 --> timerqueue =
+ *                   /                \
+ *    LVL 0  [GRP0:0]                  [GRP0:1]
+ *           migrator = TMIGR_NONE     migrator = CPU2
+ *           active   =                active   = CPU2
+ *           groupevt.ignore = 0       groupevt.ignore = 1
+ *       --> groupevt.cpu = CPU0       groupevt.cpu =
+ *           timerqueue = evt-CPU0,    timerqueue =
+ *                        evt-CPU1
+ *              /         \                /         \
+ *    CPUs     0           1              2           3
+ *             idle        idle           active      idle
+ *
+ * 3. After remote expiry CPU0 has no global timer that needs to be
+ *    enqueued. When skipping the walk, global timer of CPU1 is not
+ *    handled, as group event of GRP0:0 is not updated and not enqueued
+ *    into GRP1:0. The walk has to be done to update the group events and
+ *    timerqueues:
+ *
+ *    LVL 1            [GRP1:0]
+ *                     migrator = GRP0:1
+ *                     active   = GRP0:1
+ *                 --> timerqueue = evt-GRP0:0
+ *                   /                \
+ *    LVL 0  [GRP0:0]                  [GRP0:1]
+ *           migrator = TMIGR_NONE     migrator = CPU2
+ *           active   =                active   = CPU2
+ *           groupevt.ignore = 0       groupevt.ignore = 1
+ *       --> groupevt.cpu = CPU1       groupevt.cpu =
+ *       --> timerqueue = evt-CPU1     timerqueue =
+ *              /         \                /         \
+ *    CPUs     0           1              2           3
+ *             idle        idle           active      idle
+ *
+ * Now CPU2 (migrator) is able to handle timer of CPU1 as CPU2 only scans
+ * the timerqueues of GRP0:1 and GRP1:0.
+ *
+ * The update of step 3 is valid to be skipped, when remote CPU went
+ * offline in the meantime because update was already done during inactive
+ * path. When CPU became active in the meantime, update isn't required as
+ * well, because GRP0:0 is now longer idle.
+ */
+
+static DEFINE_MUTEX(tmigr_mutex);
+static struct list_head *tmigr_level_list __read_mostly;
+
+static unsigned int tmigr_hierarchy_levels __read_mostly;
+static unsigned int tmigr_crossnode_level __read_mostly;
+
+static DEFINE_PER_CPU(struct tmigr_cpu, tmigr_cpu);
+
+#define TMIGR_NONE	0xFF
+#define BIT_CNT		8
+
+static DEFINE_STATIC_KEY_FALSE(tmigr_enabled);
+
+static inline bool is_tmigr_enabled(void)
+{
+	return static_branch_unlikely(&tmigr_enabled);
+}
+
+/*
+ * Returns true, when @childmask corresponds to group migrator or when group
+ * is not active - so no migrator is set.
+ */
+static bool tmigr_check_migrator(struct tmigr_group *group, u8 childmask)
+{
+	union tmigr_state s;
+
+	s.state = atomic_read(&group->migr_state);
+
+	if ((s.migrator != childmask) && (s.migrator != TMIGR_NONE))
+		return false;
+
+	return true;
+}
+
+typedef bool (*up_f)(struct tmigr_group *, struct tmigr_group *, void *);
+
+static void __walk_groups(up_f up, void *data,
+			  struct tmigr_cpu *tmc)
+{
+	struct tmigr_group *child = NULL, *group = tmc->tmgroup;
+
+	do {
+		WARN_ON_ONCE(group->level >= tmigr_hierarchy_levels);
+
+		if (up(group, child, data))
+			break;
+
+		child = group;
+		group = group->parent;
+	} while (group);
+}
+
+static void walk_groups(up_f up, void *data, struct tmigr_cpu *tmc)
+{
+	lockdep_assert_held(&tmc->lock);
+
+	__walk_groups(up, data, tmc);
+}
+
+/**
+ * struct tmigr_walk - data required for walking the hierarchy
+ * @evt:		Pointer to tmigr_event which needs to be queued (of idle
+ *			child group)
+ * @childmask:		childmask of child group
+ * @nextexp:		Next CPU event expiry information which is handed
+ *			into tmigr code by timer code
+ *			(get_next_timer_interrupt()); it is furthermore
+ *			used for first event which is queued, if timer
+ *			migration hierarchy is completely idle
+ * @childstate:		tmigr_group->migr_state of child - will be only reread
+ *			when cmpxchg in group fails (is required for deactivate
+ *			path and new timer path)
+ * @groupstate:		tmigr_group->migr_state of group - will be only reread
+ *			when cmpxchg in group fails (is required for active,
+ *			deactivate and new timer path)
+ * @remote:		Is set, when new timer path is executed in
+ *			tmigr_handle_remote_cpu()
+ */
+struct tmigr_walk {
+	struct tmigr_event	*evt;
+	u8			childmask;
+	u64			nextexp;
+	union tmigr_state	childstate;
+	union tmigr_state	groupstate;
+	bool			remote;
+};
+
+/**
+ * struct tmigr_remote_data -	data required for (check) remote expiry
+ *				hierarchy walk
+ * @basej:	timer base in jiffies
+ * @now:	timer base monotonic
+ * @childmask:	childmask of child group
+ * @check:	is set to 1 if there is the need to handle remote timers;
+ *		required in tmigr_check_handle_remote() only
+ * @wakeup:	returns expiry of first timer in idle timer migration hierarchy
+ *		to make sure timer is handled in time; it is stored in per CPU
+ *		tmigr_cpu struct of CPU which expires remote timers
+ */
+struct tmigr_remote_data {
+	unsigned long	basej;
+	u64		now;
+	u8		childmask;
+	int		check;
+	u64		wakeup;
+};
+
+/*
+ * Returns next event of timerqueue @group->events
+ *
+ * Removes timers with ignore bits and update next_expiry of group. Values
+ * of group event are updated in tmigr_update_events() only.
+ */
+static struct tmigr_event *tmigr_next_groupevt(struct tmigr_group *group)
+{
+	struct timerqueue_node *node = NULL;
+	struct tmigr_event *evt = NULL;
+
+	lockdep_assert_held(&group->lock);
+
+	WRITE_ONCE(group->next_expiry, KTIME_MAX);
+
+	while ((node = timerqueue_getnext(&group->events))) {
+		evt = container_of(node, struct tmigr_event, nextevt);
+
+		if (!evt->ignore) {
+			WRITE_ONCE(group->next_expiry, evt->nextevt.expires);
+			return evt;
+		}
+
+		/*
+		 * Remove next timers with ignore bits, because group lock
+		 * is held anyway
+		 */
+		if (!timerqueue_del(&group->events, node))
+			break;
+	}
+
+	return NULL;
+}
+
+/*
+ * Return next event which is already expired of group timerqueue
+ *
+ * Event is also removed from queue.
+ */
+static struct tmigr_event *tmigr_next_expired_groupevt(struct tmigr_group *group,
+						     u64 now)
+{
+	struct tmigr_event *evt = tmigr_next_groupevt(group);
+
+	if (!evt || now < evt->nextevt.expires)
+		return NULL;
+
+	/*
+	 * Event is already expired. Remove it. If it's not the last event,
+	 * then update all group event related information.
+	 */
+	if (timerqueue_del(&group->events, &evt->nextevt))
+		tmigr_next_groupevt(group);
+	else
+		WRITE_ONCE(group->next_expiry, KTIME_MAX);
+
+	return evt;
+}
+
+static u64 tmigr_next_groupevt_expires(struct tmigr_group *group)
+{
+	struct tmigr_event *evt;
+
+	evt = tmigr_next_groupevt(group);
+
+	if (!evt)
+		return KTIME_MAX;
+	else
+		return evt->nextevt.expires;
+}
+
+static bool tmigr_active_up(struct tmigr_group *group,
+			    struct tmigr_group *child,
+			    void *ptr)
+{
+	union tmigr_state curstate, newstate;
+	struct tmigr_walk *data = ptr;
+	bool walk_done;
+	u8 childmask;
+
+	childmask = data->childmask;
+	newstate = curstate = data->groupstate;
+
+retry:
+	walk_done = true;
+
+	if (newstate.migrator == TMIGR_NONE) {
+		newstate.migrator = childmask;
+
+		/* Changes need to be propagated */
+		walk_done = false;
+	}
+
+	newstate.active |= childmask;
+
+	newstate.seq++;
+
+	if (!atomic_try_cmpxchg(&group->migr_state, &curstate.state, newstate.state)) {
+		newstate.state = curstate.state;
+		goto retry;
+	}
+
+	if (group->parent && (walk_done == false)) {
+		data->groupstate.state = atomic_read(&group->parent->migr_state);
+		data->childmask = group->childmask;
+	}
+
+	/*
+	 * Group is active, event will be ignored - bit is updated without
+	 * holding the lock. In case bit is set while another CPU already
+	 * handles remote events, nothing happens, because it is clear that
+	 * CPU became active just in this moment, or in worst case event is
+	 * handled remote. Nothing to worry about.
+	 */
+	group->groupevt.ignore = 1;
+
+	return walk_done;
+}
+
+static void __tmigr_cpu_activate(struct tmigr_cpu *tmc)
+{
+	struct tmigr_walk data;
+
+	data.childmask = tmc->childmask;
+	data.groupstate.state = atomic_read(&tmc->tmgroup->migr_state);
+
+	tmc->cpuevt.ignore = 1;
+
+	walk_groups(&tmigr_active_up, &data, tmc);
+}
+
+/**
+ * tmigr_cpu_activate - set CPU active in timer migration hierarchy
+ *
+ * Call site timer_clear_idle() is called with interrupts disabled
+ */
+void tmigr_cpu_activate(void)
+{
+	struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
+
+	if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online || !tmc->idle)
+		return;
+
+	raw_spin_lock(&tmc->lock);
+	tmc->idle = 0;
+	tmc->wakeup = KTIME_MAX;
+	__tmigr_cpu_activate(tmc);
+	raw_spin_unlock(&tmc->lock);
+}
+
+/*
+ * Returns true, if there is nothing to be propagated to the next level
+ *
+ * @data->nextexp is reset to KTIME_MAX; it is reused for first global
+ * event which needs to be handled by migrator (in toplevel group)
+ *
+ * This is the only place where group event expiry value is set.
+ */
+static bool tmigr_update_events(struct tmigr_group *group,
+				struct tmigr_group *child,
+				struct tmigr_walk *data)
+{
+	struct tmigr_event *evt, *first_childevt;
+	bool walk_done, remote = data->remote;
+	u64 nextexp;
+
+	if (child) {
+		if (data->childstate.active)
+			return true;
+
+		raw_spin_lock(&child->lock);
+		raw_spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING);
+
+		first_childevt = tmigr_next_groupevt(child);
+		nextexp = child->next_expiry;
+		evt = &child->groupevt;
+	} else {
+		nextexp = data->nextexp;
+
+		/*
+		 * Set @data->nextexp to KTIME_MAX; it is reused for first
+		 * global event which needs to be handled by migrator (in
+		 * toplevel group)
+		 */
+		data->nextexp = KTIME_MAX;
+
+		first_childevt = evt = data->evt;
+
+		/*
+		 * Walking the hierarchy is required in any case, when a
+		 * remote expiry was done before. This ensures to not lost
+		 * already queued events in non active groups (see section
+		 * "Required event and timerqueue update after remote
+		 * expiry" in documentation at the top).
+		 */
+		if (evt->ignore && !remote)
+			return true;
+
+		raw_spin_lock(&group->lock);
+	}
+
+	if (nextexp == KTIME_MAX) {
+		evt->ignore = 1;
+
+		/*
+		 * When next child event could be ignored (nextexp is
+		 * KTIME_MAX) and there was no remote timer handling before
+		 * or the group is already active, there is no need to walk
+		 * the hierarchy even if there is a parent group.
+		 *
+		 * The other way round: even if the event could be ignored,
+		 * but if a remote timer handling was executed before and
+		 * the group is not active, walking the hierarchy is
+		 * required to not miss an enqueued timer in the non active
+		 * group. The enqueued timer of the group needs to be
+		 * propagated to a higher level to ensure it is handled.
+		 */
+		if (!remote || data->groupstate.active) {
+			walk_done = true;
+			goto unlock;
+		}
+	} else {
+		/*
+		 * Update of event cpu and ignore bit is required only when
+		 * @child is set (child is equal or higher than lvl0), but
+		 * it doesn't matter if it is written once more to per cpu
+		 * event; make the update unconditional.
+		 */
+		evt->cpu = first_childevt->cpu;
+		evt->ignore = 0;
+	}
+
+	walk_done = !group->parent;
+
+	/*
+	 * If child event is already queued in group, remove it from queue
+	 * when expiry time changed only.
+	 */
+	if (timerqueue_node_queued(&evt->nextevt)) {
+		if (evt->nextevt.expires == nextexp)
+			goto check_toplvl;
+		else if (!timerqueue_del(&group->events, &evt->nextevt))
+			WRITE_ONCE(group->next_expiry, KTIME_MAX);
+	}
+
+	evt->nextevt.expires = nextexp;
+
+	if (timerqueue_add(&group->events, &evt->nextevt))
+		WRITE_ONCE(group->next_expiry, nextexp);
+
+check_toplvl:
+	if (walk_done && (data->groupstate.migrator == TMIGR_NONE)) {
+		/*
+		 * Toplevel group is idle and it has to be ensured global
+		 * timers are handled in time. (This could be optimized by
+		 * keeping track of the last global scheduled event and
+		 * only arming it on CPU if the new event is earlier. Not
+		 * sure if its worth the complexity.)
+		 */
+		data->nextexp = tmigr_next_groupevt_expires(group);
+	}
+
+unlock:
+	raw_spin_unlock(&group->lock);
+
+	if (child)
+		raw_spin_unlock(&child->lock);
+
+	return walk_done;
+}
+
+static bool tmigr_new_timer_up(struct tmigr_group *group,
+			       struct tmigr_group *child,
+			       void *ptr)
+{
+	struct tmigr_walk *data = ptr;
+	bool walk_done;
+
+	walk_done = tmigr_update_events(group, child, data);
+
+	if (!walk_done) {
+		/* Update state information for next iteration */
+		data->childstate.state = atomic_read(&group->migr_state);
+		if (group->parent)
+			data->groupstate.state = atomic_read(&group->parent->migr_state);
+	}
+
+	return walk_done;
+}
+
+/*
+ * Returns expiry of next timer that needs to be handled. KTIME_MAX is
+ * returned, when an active CPU will handle all timer migration hierarchy
+ * timers.
+ */
+static u64 tmigr_new_timer(struct tmigr_cpu *tmc, u64 nextexp)
+{
+	struct tmigr_walk data = { .evt = &tmc->cpuevt,
+				   .nextexp = nextexp };
+
+	lockdep_assert_held(&tmc->lock);
+
+	if (tmc->remote)
+		return KTIME_MAX;
+
+	tmc->cpuevt.ignore = 0;
+
+	data.groupstate.state = atomic_read(&tmc->tmgroup->migr_state);
+	data.remote = false;
+
+	walk_groups(&tmigr_new_timer_up, &data, tmc);
+
+	/* If there is a new first global event, make sure it is handled */
+	return data.nextexp;
+}
+
+static bool tmigr_inactive_up(struct tmigr_group *group,
+			      struct tmigr_group *child,
+			      void *ptr)
+{
+	union tmigr_state curstate, newstate;
+	struct tmigr_walk *data = ptr;
+	bool walk_done;
+	u8 childmask;
+
+	childmask = data->childmask;
+	newstate = curstate = data->groupstate;
+
+retry:
+	walk_done = true;
+
+	/* Reset active bit when child is no longer active */
+	if (!data->childstate.active)
+		newstate.active &= ~childmask;
+
+	if (newstate.migrator == childmask) {
+		/*
+		 * Find a new migrator for the group, because child group
+		 * is idle!
+		 */
+		if (!data->childstate.active) {
+			unsigned long new_migr_bit, active = newstate.active;
+
+			new_migr_bit = find_first_bit(&active, BIT_CNT);
+
+			if (new_migr_bit != BIT_CNT) {
+				newstate.migrator = BIT(new_migr_bit);
+			} else {
+				newstate.migrator = TMIGR_NONE;
+
+				/* Changes need to be propagated */
+				walk_done = false;
+			}
+		}
+	}
+
+	newstate.seq++;
+
+	WARN_ON_ONCE((newstate.migrator != TMIGR_NONE) && !(newstate.active));
+
+	if (!atomic_try_cmpxchg(&group->migr_state, &curstate.state, newstate.state)) {
+		newstate.state = curstate.state;
+
+		/*
+		 * Something changed in child/parent group in the meantime,
+		 * reread the state of child and parent; Update of
+		 * data->childstate is required for event handling;
+		 */
+		if (child)
+			data->childstate.state = atomic_read(&child->migr_state);
+
+		goto retry;
+	}
+
+	data->groupstate = newstate;
+	data->remote = false;
+
+	/* Event Handling */
+	tmigr_update_events(group, child, data);
+
+	if (group->parent && (walk_done == false)) {
+		data->childmask = group->childmask;
+		data->childstate = newstate;
+		data->groupstate.state = atomic_read(&group->parent->migr_state);
+	}
+
+	/*
+	 * data->nextexp was set by tmigr_update_events() and contains the
+	 * expiry of first global event which needs to be handled
+	 */
+	if (data->nextexp != KTIME_MAX) {
+		WARN_ON_ONCE(group->parent);
+		/*
+		 * Toplevel path - If this cpu is about going offline wake
+		 * up some random other cpu so it will take over the
+		 * migrator duty and program its timer properly. Ideally
+		 * wake the cpu with the closest expiry time, but that's
+		 * overkill to figure out.
+		 */
+		if (!(this_cpu_ptr(&tmigr_cpu)->online)) {
+			unsigned int cpu = smp_processor_id();
+
+			cpu = cpumask_any_but(cpu_online_mask, cpu);
+			smp_send_reschedule(cpu);
+		}
+	}
+
+	return walk_done;
+}
+
+static u64 __tmigr_cpu_deactivate(struct tmigr_cpu *tmc, u64 nextexp)
+{
+	struct tmigr_walk data = { .childmask = tmc->childmask,
+				   .evt = &tmc->cpuevt,
+				   .nextexp = nextexp,
+				   .childstate.state = 0 };
+
+	data.groupstate.state = atomic_read(&tmc->tmgroup->migr_state);
+
+	/*
+	 * If nextexp is KTIME_MAX, CPU event will be ignored because,
+	 * local timer expires before global timer, no global timer is set
+	 * or CPU goes offline.
+	 */
+	if (nextexp != KTIME_MAX)
+		tmc->cpuevt.ignore = 0;
+
+	walk_groups(&tmigr_inactive_up, &data, tmc);
+	return data.nextexp;
+}
+
+/**
+ * tmigr_cpu_deactivate - Put current CPU into inactive state
+ * @nextexp:	The next timer event expiry set in the current CPU
+ *
+ * Must be called with interrupts disabled.
+ *
+ * Return: next event of the current CPU or next event from the hierarchy
+ * if this CPU is the top level migrator or hierarchy is completely idle.
+ */
+u64 tmigr_cpu_deactivate(u64 nextexp)
+{
+	struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
+	u64 ret;
+
+	if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online)
+		return nextexp;
+
+	raw_spin_lock(&tmc->lock);
+
+	/*
+	 * CPU is already deactivated in timer migration
+	 * hierarchy. tick_nohz_get_sleep_length() calls
+	 * tick_nohz_next_event() and thereby timer idle path is
+	 * executed once more. tmc->wakeup holds the first timer, when
+	 * timer migration hierarchy is completely idle and remote
+	 * expiry was done. If there is no new next expiry value
+	 * handed in which should be inserted into the timer migration
+	 * hierarchy, wakeup value is returned.
+	 */
+	if (tmc->idle) {
+		ret = tmc->wakeup;
+
+		tmc->wakeup = KTIME_MAX;
+
+		if (nextexp != KTIME_MAX) {
+			if (nextexp != tmc->cpuevt.nextevt.expires ||
+			    tmc->cpuevt.ignore)
+				ret = tmigr_new_timer(tmc, nextexp);
+		}
+
+		goto unlock;
+	}
+
+	ret = __tmigr_cpu_deactivate(tmc, nextexp);
+
+	tmc->idle = 1;
+
+unlock:
+	raw_spin_unlock(&tmc->lock);
+	return ret;
+}
+
+static u64 tmigr_handle_remote_cpu(unsigned int cpu, u64 now,
+				   unsigned long jif)
+{
+	struct timer_events tevt;
+	struct tmigr_walk data;
+	struct tmigr_cpu *tmc;
+	u64 next = KTIME_MAX;
+
+	tmc = per_cpu_ptr(&tmigr_cpu, cpu);
+
+	raw_spin_lock_irq(&tmc->lock);
+	/*
+	 * Remote CPU is offline or no longer idle or other cpu handles cpu
+	 * timers already or next event was already expired - return!
+	 */
+	if (!tmc->online || tmc->remote || tmc->cpuevt.ignore ||
+	    now < tmc->cpuevt.nextevt.expires) {
+		raw_spin_unlock_irq(&tmc->lock);
+		return next;
+	}
+
+	tmc->remote = 1;
+
+	/* Drop the lock to allow the remote CPU to exit idle */
+	raw_spin_unlock_irq(&tmc->lock);
+
+	if (cpu != smp_processor_id())
+		timer_expire_remote(cpu);
+
+	/*
+	 * Lock ordering needs to be preserved - timer_base locks before
+	 * tmigr related locks (see section "Locking rules" in the
+	 * documentation at the top). During fetching the next timer
+	 * interrupt, also tmc->lock needs to be held. Otherwise there is a
+	 * possible race window against the CPU itself when it comes out of
+	 * idle, updates the first timer in hierarchy and goes back to
+	 * idle.
+	 *
+	 * timer base locks are dropped as fast as possible: After checking
+	 * whether the remote CPU when offline in the meantime and after
+	 * fetching the next remote timer interrupt. Dropping the locks as
+	 * fast as possible keeps the locking region small and prevents
+	 * holding several (unnecessary) locks during walking the hierarchy
+	 * for updating the timerqueue and group events.
+	 */
+	local_irq_disable();
+	timer_lock_remote_bases(cpu);
+	raw_spin_lock(&tmc->lock);
+
+	/*
+	 * When CPU went offline in the meantime, no hierarchy walk has to
+	 * be done for updating the queued events, because the walk was
+	 * already done during marking CPU offline in the hierarchy.
+	 *
+	 * When CPU is no longer idle, CPU takes care of the timers and
+	 * also of the timers in the path to the top.
+	 *
+	 * (See also section "Required event and timerqueue update after
+	 * remote expiry" in documentation at the top)
+	 */
+	if (!tmc->online || !tmc->idle) {
+		timer_unlock_remote_bases(cpu);
+		goto unlock;
+	} else {
+		/* next	event of cpu */
+		fetch_next_timer_interrupt_remote(jif, now, &tevt, cpu);
+	}
+
+	timer_unlock_remote_bases(cpu);
+
+	data.evt = &tmc->cpuevt;
+	data.nextexp = tevt.global;
+	data.groupstate.state = atomic_read(&tmc->tmgroup->migr_state);
+	data.remote = true;
+
+	/*
+	 * Update is done even when there is no 'new' global timer pending
+	 * on remote CPU (see section "Required event and timerqueue update
+	 * after remote expiry" in documentation at the top)
+	 */
+	walk_groups(&tmigr_new_timer_up, &data, tmc);
+
+	next = data.nextexp;
+
+unlock:
+	tmc->remote = 0;
+	raw_spin_unlock_irq(&tmc->lock);
+
+	return next;
+}
+
+static bool tmigr_handle_remote_up(struct tmigr_group *group,
+				   struct tmigr_group *child,
+				   void *ptr)
+{
+	struct tmigr_remote_data *data = ptr;
+	u64 now, next = KTIME_MAX;
+	struct tmigr_event *evt;
+	unsigned long jif;
+	u8 childmask;
+
+	jif = data->basej;
+	now = data->now;
+
+	childmask = data->childmask;
+
+again:
+	/*
+	 * Handle the group only if @childmask is the migrator or if the
+	 * group has no migrator. Otherwise the group is active and is
+	 * handled by its own migrator.
+	 */
+	if (!tmigr_check_migrator(group, childmask))
+		return true;
+
+	raw_spin_lock_irq(&group->lock);
+
+	evt = tmigr_next_expired_groupevt(group, now);
+
+	if (evt) {
+		unsigned int remote_cpu = evt->cpu;
+
+		raw_spin_unlock_irq(&group->lock);
+
+		next = tmigr_handle_remote_cpu(remote_cpu, now, jif);
+
+		/* check if there is another event, that needs to be handled */
+		goto again;
+	} else {
+		raw_spin_unlock_irq(&group->lock);
+	}
+
+	/* Update of childmask for next level */
+	data->childmask = group->childmask;
+	data->wakeup = next;
+
+	return false;
+}
+
+/**
+ * tmigr_handle_remote - Handle migratable timers on remote idle CPUs
+ *
+ * Called from the timer soft interrupt with interrupts enabled.
+ */
+void tmigr_handle_remote(void)
+{
+	struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
+	struct tmigr_remote_data data;
+
+	if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online)
+		return;
+
+	/*
+	 * NOTE: This is a doubled check because migrator test will be done
+	 * in tmigr_handle_remote_up() anyway. Keep this check to fasten
+	 * the return when nothing has to be done.
+	 */
+	if (!tmigr_check_migrator(tmc->tmgroup, tmc->childmask))
+		return;
+
+	data.now = get_jiffies_update(&data.basej);
+	data.childmask = tmc->childmask;
+	data.wakeup = KTIME_MAX;
+
+	__walk_groups(&tmigr_handle_remote_up, &data, tmc);
+
+	raw_spin_lock_irq(&tmc->lock);
+	if (tmc->idle)
+		tmc->wakeup = data.wakeup;
+
+	raw_spin_unlock_irq(&tmc->lock);
+}
+
+static bool tmigr_requires_handle_remote_up(struct tmigr_group *group,
+					    struct tmigr_group *child,
+					    void *ptr)
+{
+	struct tmigr_remote_data *data = ptr;
+	u8 childmask;
+
+	childmask = data->childmask;
+
+	/*
+	 * Handle the group only if child is the migrator or if the group
+	 * has no migrator. Otherwise the group is active and is handled by
+	 * its own migrator.
+	 */
+	if (!tmigr_check_migrator(group, childmask))
+		return true;
+
+	/*
+	 * On 32 bit systems the racy lockless check for next_expiry will
+	 * turn into a random number generator. Therefore do the lockless
+	 * check only on 64 bit systems.
+	 */
+	if (IS_ENABLED(CONFIG_64BIT)) {
+		if (data->now >= READ_ONCE(group->next_expiry)) {
+			data->check = 1;
+			return true;
+		}
+	} else {
+		raw_spin_lock(&group->lock);
+		if (data->now >= group->next_expiry) {
+			data->check = 1;
+			raw_spin_unlock(&group->lock);
+			return true;
+		}
+		raw_spin_unlock(&group->lock);
+	}
+
+	/* Update of childmask for next level */
+	data->childmask = group->childmask;
+	return false;
+}
+
+/**
+ * tmigr_requires_handle_remote - Check whether remote timer handling is required
+ *
+ * Must be called with interrupts disabled.
+ */
+int tmigr_requires_handle_remote(void)
+{
+	struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
+	struct tmigr_remote_data data;
+
+	if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online)
+		return 0;
+
+	if (!tmigr_check_migrator(tmc->tmgroup, tmc->childmask))
+		return 0;
+
+	data.now = get_jiffies_update(&data.basej);
+	data.childmask = tmc->childmask;
+
+	__walk_groups(&tmigr_requires_handle_remote_up, &data, tmc);
+
+	return data.check;
+}
+
+static void tmigr_init_group(struct tmigr_group *group, unsigned int lvl,
+			     int node)
+{
+	union tmigr_state s;
+
+	raw_spin_lock_init(&group->lock);
+
+	group->level = lvl;
+	group->numa_node = lvl < tmigr_crossnode_level ? node : NUMA_NO_NODE;
+
+	group->num_children = 0;
+
+	s.migrator = TMIGR_NONE;
+	s.active = 0;
+	s.seq = 0;
+	atomic_set(&group->migr_state, s.state);
+
+	timerqueue_init_head(&group->events);
+	timerqueue_init(&group->groupevt.nextevt);
+	group->groupevt.nextevt.expires = KTIME_MAX;
+	WRITE_ONCE(group->next_expiry, KTIME_MAX);
+	group->groupevt.ignore = 1;
+}
+
+static struct tmigr_group *tmigr_get_group(unsigned int cpu, int node,
+					   unsigned int lvl)
+{
+	struct tmigr_group *tmp, *group = NULL;
+
+	lockdep_assert_held(&tmigr_mutex);
+
+	/* Try to attach to an exisiting group first */
+	list_for_each_entry(tmp, &tmigr_level_list[lvl], list) {
+		/*
+		 * If @lvl is below the cross numa node level, check whether
+		 * this group belongs to the same numa node.
+		 */
+		if (lvl < tmigr_crossnode_level && tmp->numa_node != node)
+			continue;
+
+		/* Capacity left? */
+		if (tmp->num_children >= TMIGR_CHILDS_PER_GROUP)
+			continue;
+
+		/*
+		 * TODO: A possible further improvement: Make sure that all
+		 * CPU siblings end up in the same group of lowest level of
+		 * the hierarchy. Rely on topology sibling mask would be a
+		 * reasonable solution.
+		 */
+
+		group = tmp;
+		break;
+	}
+
+	if (group)
+		return group;
+
+	/* Allocate and	set up a new group */
+	group = kzalloc_node(sizeof(*group), GFP_KERNEL, node);
+	if (!group)
+		return ERR_PTR(-ENOMEM);
+
+	tmigr_init_group(group, lvl, node);
+
+	/* Setup successful. Add it to the hierarchy */
+	list_add(&group->list, &tmigr_level_list[lvl]);
+	return group;
+}
+
+static void tmigr_connect_child_parent(struct tmigr_group *child,
+				       struct tmigr_group *parent)
+{
+	union tmigr_state childstate;
+
+	raw_spin_lock_irq(&child->lock);
+	raw_spin_lock_nested(&parent->lock, SINGLE_DEPTH_NESTING);
+
+	child->parent = parent;
+	child->childmask = BIT(parent->num_children++);
+
+	raw_spin_unlock(&parent->lock);
+	raw_spin_unlock_irq(&child->lock);
+
+	/*
+	 * To prevent inconsistent states, active children need to be
+	 * active in new parent as well. Inactive children are already
+	 * marked inactive in parent group.
+	 */
+	childstate.state = atomic_read(&child->migr_state);
+	if (childstate.migrator != TMIGR_NONE) {
+		struct tmigr_walk data;
+
+		data.childmask = child->childmask;
+		data.groupstate.state = atomic_read(&parent->migr_state);
+
+		/*
+		 * There is only one new level per time. When connecting
+		 * child and parent and set child active when parent is
+		 * inactive, parent needs to be the uppermost
+		 * level. Otherwise there went something wrong!
+		 */
+		WARN_ON(!tmigr_active_up(parent, child, &data) && parent->parent);
+	}
+}
+
+static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
+{
+	struct tmigr_group *group, *child, **stack;
+	int top = 0, err = 0, i = 0;
+	struct list_head *lvllist;
+
+	stack = kcalloc(tmigr_hierarchy_levels, sizeof(*stack), GFP_KERNEL);
+	if (!stack)
+		return -ENOMEM;
+
+	do {
+		group = tmigr_get_group(cpu, node, i);
+		if (IS_ERR(group)) {
+			err = PTR_ERR(group);
+			break;
+		}
+
+		top = i;
+		stack[i++] = group;
+
+		/*
+		 * When booting only less CPUs of a system than CPUs are
+		 * available, not all calculated hierarchy levels are required.
+		 *
+		 * The loop is aborted as soon as the highest level, which might
+		 * be different from tmigr_hierarchy_levels, contains only a
+		 * single group.
+		 */
+		if (group->parent || i == tmigr_hierarchy_levels ||
+		    (list_empty(&tmigr_level_list[i]) &&
+		     list_is_singular(&tmigr_level_list[i - 1])))
+			break;
+
+	} while (i < tmigr_hierarchy_levels);
+
+	do {
+		group = stack[--i];
+
+		if (err < 0) {
+			list_del(&group->list);
+			kfree(group);
+			continue;
+		}
+
+		WARN_ON_ONCE(i != group->level);
+
+		/*
+		 * Update tmc -> group / child -> group connection
+		 */
+		if (i == 0) {
+			struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
+
+			raw_spin_lock_irq(&group->lock);
+
+			tmc->tmgroup = group;
+			tmc->childmask = BIT(group->num_children++);
+
+			raw_spin_unlock_irq(&group->lock);
+
+			/* There are no children that need to be connected */
+			continue;
+		} else {
+			child = stack[i - 1];
+			tmigr_connect_child_parent(child, group);
+		}
+
+		/* check if uppermost level was newly created */
+		if (top != i)
+			continue;
+
+		WARN_ON_ONCE(top == 0);
+
+		lvllist = &tmigr_level_list[top];
+		if (group->num_children == 1 && list_is_singular(lvllist)) {
+			lvllist = &tmigr_level_list[top - 1];
+			list_for_each_entry(child, lvllist, list) {
+				if (child->parent)
+					continue;
+
+				tmigr_connect_child_parent(child, group);
+			}
+		}
+	} while (i > 0);
+
+	kfree(stack);
+
+	return err;
+}
+
+static int tmigr_add_cpu(unsigned int cpu)
+{
+	int node = cpu_to_node(cpu);
+	int ret;
+
+	mutex_lock(&tmigr_mutex);
+	ret = tmigr_setup_groups(cpu, node);
+	mutex_unlock(&tmigr_mutex);
+
+	return ret;
+}
+
+static int tmigr_cpu_online(unsigned int cpu)
+{
+	struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
+	unsigned int ret;
+
+	/* First online attempt? Initialize CPU data */
+	if (!tmc->tmgroup) {
+		raw_spin_lock_init(&tmc->lock);
+
+		ret = tmigr_add_cpu(cpu);
+		if (ret < 0)
+			return ret;
+
+		if (tmc->childmask == 0)
+			return -EINVAL;
+
+		timerqueue_init(&tmc->cpuevt.nextevt);
+		tmc->cpuevt.nextevt.expires = KTIME_MAX;
+		tmc->cpuevt.ignore = 1;
+		tmc->cpuevt.cpu = cpu;
+
+		tmc->remote = 0;
+		tmc->idle = 0;
+		tmc->wakeup = KTIME_MAX;
+	}
+	raw_spin_lock_irq(&tmc->lock);
+	__tmigr_cpu_activate(tmc);
+	tmc->online = 1;
+	raw_spin_unlock_irq(&tmc->lock);
+	return 0;
+}
+
+static int tmigr_cpu_offline(unsigned int cpu)
+{
+	struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
+
+	raw_spin_lock_irq(&tmc->lock);
+	tmc->online = 0;
+
+	/*
+	 * CPU has to handle local events on his own, when on the way to
+	 * offline; Therefore nextevt value is set to KTIME_MAX
+	 */
+	__tmigr_cpu_deactivate(tmc, KTIME_MAX);
+	raw_spin_unlock_irq(&tmc->lock);
+
+	return 0;
+}
+
+static int __init tmigr_init(void)
+{
+	unsigned int cpulvl, nodelvl, cpus_per_node, i;
+	unsigned int nnodes = num_possible_nodes();
+	unsigned int ncpus = num_possible_cpus();
+	int ret = -ENOMEM;
+
+	/* Nothing to do if running on UP */
+	if (ncpus == 1)
+		return 0;
+
+	/*
+	 * Calculate the required hierarchy levels. Unfortunately there is no
+	 * reliable information available, unless all possible CPUs have been
+	 * brought up and all numa nodes are populated.
+	 *
+	 * Estimate the number of levels with the number of possible nodes and
+	 * the number of possible cpus. Assume CPUs are spread evenly across
+	 * nodes. We cannot rely on cpumask_of_node() because there only already
+	 * online CPUs are considered.
+	 */
+	cpus_per_node = DIV_ROUND_UP(ncpus, nnodes);
+
+	/* Calc the hierarchy levels required to hold the CPUs of a node */
+	cpulvl = DIV_ROUND_UP(order_base_2(cpus_per_node),
+			      ilog2(TMIGR_CHILDS_PER_GROUP));
+
+	/* Calculate the extra levels to connect all nodes */
+	nodelvl = DIV_ROUND_UP(order_base_2(nnodes),
+			       ilog2(TMIGR_CHILDS_PER_GROUP));
+
+	tmigr_hierarchy_levels = cpulvl + nodelvl;
+
+	/*
+	 * If a numa node spawns more than one CPU level group then the next
+	 * level(s) of the hierarchy contains groups which handle all CPU groups
+	 * of the same numa node. The level above goes across numa nodes. Store
+	 * this information for the setup code to decide when node matching is
+	 * not longer required.
+	 */
+	tmigr_crossnode_level = cpulvl;
+
+	tmigr_level_list = kcalloc(tmigr_hierarchy_levels, sizeof(struct list_head), GFP_KERNEL);
+	if (!tmigr_level_list)
+		goto err;
+
+	for (i = 0; i < tmigr_hierarchy_levels; i++)
+		INIT_LIST_HEAD(&tmigr_level_list[i]);
+
+	pr_info("Timer migration: %d hierarchy levels; %d children per group;"
+		" %d crossnode level\n",
+		tmigr_hierarchy_levels, TMIGR_CHILDS_PER_GROUP,
+		tmigr_crossnode_level);
+
+	ret = cpuhp_setup_state(CPUHP_AP_TMIGR_ONLINE, "tmigr:online",
+				tmigr_cpu_online, tmigr_cpu_offline);
+	if (ret)
+		goto err;
+
+	static_branch_enable(&tmigr_enabled);
+
+	return 0;
+
+err:
+	pr_err("Timer migration setup failed\n");
+	return ret;
+}
+late_initcall(tmigr_init);
diff --git a/kernel/time/timer_migration.h b/kernel/time/timer_migration.h
new file mode 100644
index 000000000000..3a31a16443d2
--- /dev/null
+++ b/kernel/time/timer_migration.h
@@ -0,0 +1,136 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _KERNEL_TIME_MIGRATION_H
+#define _KERNEL_TIME_MIGRATION_H
+
+/* Per group capacity. Must be a power of 2! */
+#define TMIGR_CHILDS_PER_GROUP 8
+
+/**
+ * struct tmigr_event - a timer event associated to a CPU
+ * @nextevt:	The node to enqueue an event in the parent group queue
+ * @cpu:	The CPU to which this event belongs
+ * @ignore:	Hint whether the event could be ignored; it is set when
+ *		CPU or group is active;
+ */
+struct tmigr_event {
+	struct timerqueue_node	nextevt;
+	unsigned int		cpu;
+	int			ignore;
+};
+
+/**
+ * struct tmigr_group - timer migration hierarchy group
+ * @lock:		Lock protecting the event information and group hierarchy
+ *			information during setup
+ * @migr_state:		State of group (see union tmigr_state)
+ * @parent:		Pointer to parent group
+ * @groupevt:		Next event of group which is only used when group is
+ *			!active. The group event is then queued into parent
+ *			timer queue.
+ *			Ignore bit of groupevt is set when group is active.
+ * @next_expiry:	Base monotonic expiry time of next event of group;
+ *			Used for racy lockless check whether remote expiry is
+ *			required; it is always reliable
+ * @events:		Timer queue for child events queued in the group
+ * @childmask:		childmask of group in parent group; is set during
+ *			setup and will never change; could be read lockless
+ * @level:		Hierarchy level of group; Required during setup and
+ *			when DEBUG is defined
+ * @list:		List head that is added to per level
+ *			tmigr_level_list; is required during setup when a
+ *			new group needs to be connected to the existing
+ *			hierarchy groups
+ * @numa_node:		Is set to numa node when level < tmigr_crossnode_level;
+ *			otherwise it is set to NUMA_NO_NODE; Required for
+ *			setup only to make sure CPUs and groups are per
+ *			numa node as long as level < tmigr_crossnode_level
+ * @num_children:	Counter of group children to make sure group is only filled
+ *			with TMIGR_CHILDS_PER_GROUP; Required for setup only
+ */
+struct tmigr_group {
+	raw_spinlock_t		lock;
+	atomic_t		migr_state;
+	struct tmigr_group	*parent;
+	struct tmigr_event	groupevt;
+	u64			next_expiry;
+	struct timerqueue_head	events;
+	u8			childmask;
+	unsigned int		level;
+	struct list_head	list;
+	int			numa_node;
+	unsigned int		num_children;
+};
+
+/**
+ * struct tmigr_cpu - timer migration per CPU group
+ * @lock:	Lock protecting tmigr_cpu group information
+ * @online:	Indicates whether CPU is online; In deactivate path it is
+ *		required to know whether the migrator in top level group is
+ *		on the way to go offline when a timer is pending. Then
+ *		another online CPU needs to be rescheduled to make sure
+ *		timers are handled properly; Furthermore the information is
+ *		required in hotplug path as CPU is able to go idle before
+ *		timer migration hierarchy hotplug AP is reached. During
+ *		this phase CPU has to handle global timers by its own and
+ *		does not act as migrator.
+ * @idle:	Indicates whether CPU is idle in timer migration hierarchy
+ * @remote:	Is set when timers of CPU are expired remote
+ * @tmgroup:	Pointer to parent group
+ * @childmask:	childmask of tmigr_cpu in parent group
+ * @wakeup:	Stores the first timer when the timer migration hierarchy is
+ *		completely idle and remote expiry was done; is returned to
+ *		timer code when tmigr_cpu_deactive() is called and group is
+ *		idle; afterwards a reset to KTIME_MAX is required;
+ * @cpuevt:	CPU event which could be queued into parent group
+ */
+struct tmigr_cpu {
+	raw_spinlock_t		lock;
+	int			online;
+	int			idle;
+	int			remote;
+	struct tmigr_group	*tmgroup;
+	u8			childmask;
+	u64			wakeup;
+	struct tmigr_event	cpuevt;
+};
+
+/**
+ * union tmigr_state - state of tmigr_group
+ * @state:	Combined version of the state - only used for atomic
+ *		read/cmpxchg function
+ * @struct:	Split version of the state - only use the struct members to
+ *		update information to stay independent of endianness
+ */
+union tmigr_state {
+	u32 state;
+	/**
+	 * struct - split state of tmigr_group
+	 * @active:	Contains each childmask bit of active children
+	 * @migrator:	Contains childmask of child which is migrator
+	 * @seq:	Sequence counter needs to be increased when update
+	 *		to the tmigr_state is done. It prevents a race when
+	 *		updates in child groups are propagated in changed
+	 *		order. Detailed information about the scenario is
+	 *		given in documentation at the begin of
+	 *		timer_migration.c.
+	 */
+	struct {
+		u8	active;
+		u8	migrator;
+		u16	seq;
+	} __packed;
+};
+
+#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
+extern void tmigr_handle_remote(void);
+extern int tmigr_requires_handle_remote(void);
+extern void tmigr_cpu_activate(void);
+extern u64 tmigr_cpu_deactivate(u64 nextevt);
+#else
+static inline void tmigr_handle_remote(void) { }
+static inline int tmigr_requires_handle_remote(void) { return 0; }
+static inline void tmigr_cpu_activate(void) { }
+static inline u64 tmigr_cpu_deactivate(u64 nextevt) { return KTIME_MAX; }
+#endif
+
+#endif
-- 
2.30.2


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

* [PATCH v7 20/21] timer_migration: Add tracepoints
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (18 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 19/21] timer: Implement the hierarchical pull model Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  2023-05-24  7:06 ` [PATCH v7 21/21] timer: Always queue timers on the local CPU Anna-Maria Behnsen
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen

The timer pull logic needs proper debugging aids. Add tracepoints so the
hierarchical idle machinery can be diagnosed.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
 include/trace/events/timer_migration.h | 277 +++++++++++++++++++++++++
 kernel/time/timer_migration.c          |  24 +++
 2 files changed, 301 insertions(+)
 create mode 100644 include/trace/events/timer_migration.h

diff --git a/include/trace/events/timer_migration.h b/include/trace/events/timer_migration.h
new file mode 100644
index 000000000000..9efbaf5dabe6
--- /dev/null
+++ b/include/trace/events/timer_migration.h
@@ -0,0 +1,277 @@
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM timer_migration
+
+#if !defined(_TRACE_TIMER_MIGRATION_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_TIMER_MIGRATION_H
+
+#include <linux/tracepoint.h>
+
+/* Group events */
+TRACE_EVENT(tmigr_group_set,
+
+	TP_PROTO(struct tmigr_group *group),
+
+	TP_ARGS(group),
+
+	TP_STRUCT__entry(
+		__field( void *,	group		)
+		__field( unsigned int,	lvl		)
+		__field( unsigned int,	numa_node	)
+	),
+
+	TP_fast_assign(
+		__entry->group		= group;
+		__entry->lvl		= group->level;
+		__entry->numa_node	= group->numa_node;
+	),
+
+	TP_printk("group=%p lvl=%d numa=%d",
+		  __entry->group, __entry->lvl, __entry->numa_node)
+);
+
+TRACE_EVENT(tmigr_connect_child_parent,
+
+	TP_PROTO(struct tmigr_group *child),
+
+	TP_ARGS(child),
+
+	TP_STRUCT__entry(
+		__field( void *,	child		)
+		__field( void *,	parent		)
+		__field( unsigned int,	lvl		)
+		__field( unsigned int,	numa_node	)
+		__field( unsigned int,	num_children	)
+		__field( u32,		childmask	)
+	),
+
+	TP_fast_assign(
+		__entry->child		= child;
+		__entry->parent		= child->parent;
+		__entry->lvl		= child->parent->level;
+		__entry->numa_node	= child->parent->numa_node;
+		__entry->numa_node	= child->parent->num_children;
+		__entry->childmask	= child->childmask;
+	),
+
+	TP_printk("group=%p childmask=%0x parent=%p lvl=%d numa=%d num_children=%d",
+		  __entry->child,  __entry->childmask, __entry->parent,
+		  __entry->lvl, __entry->numa_node, __entry->num_children)
+);
+
+TRACE_EVENT(tmigr_connect_cpu_parent,
+
+	TP_PROTO(struct tmigr_cpu *tmc),
+
+	TP_ARGS(tmc),
+
+	TP_STRUCT__entry(
+		__field( void *,	parent		)
+		__field( unsigned int,	cpu		)
+		__field( unsigned int,	lvl		)
+		__field( unsigned int,	numa_node	)
+		__field( unsigned int,	num_children	)
+		__field( u32,		childmask	)
+	),
+
+	TP_fast_assign(
+		__entry->parent		= tmc->tmgroup;
+		__entry->cpu		= tmc->cpuevt.cpu;
+		__entry->lvl		= tmc->tmgroup->level;
+		__entry->numa_node	= tmc->tmgroup->numa_node;
+		__entry->numa_node	= tmc->tmgroup->num_children;
+		__entry->childmask	= tmc->childmask;
+	),
+
+	TP_printk("cpu=%d childmask=%0x parent=%p lvl=%d numa=%d num_children=%d",
+		  __entry->cpu,	 __entry->childmask, __entry->parent,
+		  __entry->lvl, __entry->numa_node, __entry->num_children)
+);
+
+DECLARE_EVENT_CLASS(tmigr_group_and_cpu,
+
+	TP_PROTO(struct tmigr_group *group, union tmigr_state state, u32 childmask),
+
+	TP_ARGS(group, state, childmask),
+
+	TP_STRUCT__entry(
+		__field( void *,	group		)
+		__field( void *,	parent		)
+		__field( unsigned int,	lvl		)
+		__field( unsigned int,	numa_node	)
+		__field( u8,		active		)
+		__field( u8,		migrator	)
+		__field( u32,		childmask	)
+	),
+
+	TP_fast_assign(
+		__entry->group		= group;
+		__entry->parent		= group->parent;
+		__entry->lvl		= group->level;
+		__entry->numa_node	= group->numa_node;
+		__entry->active		= state.active;
+		__entry->migrator	= state.migrator;
+		__entry->childmask	= childmask;
+	),
+
+	TP_printk("group=%p lvl=%d numa=%d active=%0x migrator=%0x "
+		  "parent=%p childmask=%0x",
+		  __entry->group, __entry->lvl, __entry->numa_node,
+		  __entry->active, __entry->migrator,
+		  __entry->parent, __entry->childmask)
+);
+
+DEFINE_EVENT(tmigr_group_and_cpu, tmigr_group_set_cpu_inactive,
+
+	TP_PROTO(struct tmigr_group *group, union tmigr_state state, u32 childmask),
+
+	TP_ARGS(group, state, childmask)
+);
+
+DEFINE_EVENT(tmigr_group_and_cpu, tmigr_group_set_cpu_active,
+
+	TP_PROTO(struct tmigr_group *group, union tmigr_state state, u32 childmask),
+
+	TP_ARGS(group, state, childmask)
+);
+
+/* CPU events*/
+DECLARE_EVENT_CLASS(tmigr_cpugroup,
+
+	TP_PROTO(struct tmigr_cpu *tmc),
+
+	TP_ARGS(tmc),
+
+	TP_STRUCT__entry(
+		__field( void *,	parent)
+		__field( unsigned int,	cpu)
+	),
+
+	TP_fast_assign(
+		__entry->cpu		= tmc->cpuevt.cpu;
+		__entry->parent		= tmc->tmgroup;
+	),
+
+	TP_printk("cpu=%d parent=%p", __entry->cpu, __entry->parent)
+);
+
+DEFINE_EVENT(tmigr_cpugroup, tmigr_cpu_new_timer,
+
+	TP_PROTO(struct tmigr_cpu *tmc),
+
+	TP_ARGS(tmc)
+);
+
+DEFINE_EVENT(tmigr_cpugroup, tmigr_cpu_active,
+
+	TP_PROTO(struct tmigr_cpu *tmc),
+
+	TP_ARGS(tmc)
+);
+
+DEFINE_EVENT(tmigr_cpugroup, tmigr_cpu_online,
+
+	TP_PROTO(struct tmigr_cpu *tmc),
+
+	TP_ARGS(tmc)
+);
+
+DEFINE_EVENT(tmigr_cpugroup, tmigr_cpu_offline,
+
+	TP_PROTO(struct tmigr_cpu *tmc),
+
+	TP_ARGS(tmc)
+);
+
+DEFINE_EVENT(tmigr_cpugroup, tmigr_handle_remote_cpu,
+
+	TP_PROTO(struct tmigr_cpu *tmc),
+
+	TP_ARGS(tmc)
+);
+
+TRACE_EVENT(tmigr_cpu_idle,
+
+	TP_PROTO(struct tmigr_cpu *tmc, u64 nextevt),
+
+	TP_ARGS(tmc, nextevt),
+
+	TP_STRUCT__entry(
+		__field( void *,	parent)
+		__field( unsigned int,	cpu)
+		__field( u64,		nextevt)
+	),
+
+	TP_fast_assign(
+		__entry->cpu		= tmc->cpuevt.cpu;
+		__entry->parent		= tmc->tmgroup;
+		__entry->nextevt	= nextevt;
+	),
+
+	TP_printk("cpu=%d parent=%p nextevt=%llu",
+		  __entry->cpu, __entry->parent, __entry->nextevt)
+);
+
+TRACE_EVENT(tmigr_update_events,
+
+	TP_PROTO(struct tmigr_group *child, struct tmigr_group *group,
+		 union tmigr_state childstate,	union tmigr_state groupstate,
+		 u64 nextevt),
+
+	TP_ARGS(child, group, childstate, groupstate, nextevt),
+
+	TP_STRUCT__entry(
+		__field( void *,	child			)
+		__field( void *,	group			)
+		__field( u64,		nextevt			)
+		__field( u64,		group_next_expiry	)
+		__field( unsigned int,	group_lvl		)
+		__field( u8,		child_active		)
+		__field( u8,		group_active		)
+		__field( unsigned int,	child_evtcpu		)
+		__field( u64,		child_evt_expiry	)
+	),
+
+	TP_fast_assign(
+		__entry->child			= child;
+		__entry->group			= group;
+		__entry->nextevt		= nextevt;
+		__entry->group_next_expiry	= group->next_expiry;
+		__entry->group_lvl		= group->level;
+		__entry->child_active		= childstate.active;
+		__entry->group_active		= groupstate.active;
+		__entry->child_evtcpu		= child ? child->groupevt.cpu : 0;
+		__entry->child_evt_expiry	= child ? child->groupevt.nextevt.expires : 0;
+	),
+
+	TP_printk("child=%p group=%p group_lvl=%d child_active=%0x group_active=%0x "
+		  "nextevt=%llu next_expiry=%llu child_evt_expiry=%llu child_evtcpu=%d",
+		  __entry->child, __entry->group, __entry->group_lvl, __entry->child_active,
+		  __entry->group_active,
+		  __entry->nextevt, __entry->group_next_expiry, __entry->child_evt_expiry,
+		  __entry->child_evtcpu)
+);
+
+TRACE_EVENT(tmigr_handle_remote,
+
+	TP_PROTO(struct tmigr_group *group),
+
+	TP_ARGS(group),
+
+	TP_STRUCT__entry(
+		__field( void * ,	group	)
+		__field( unsigned int ,	lvl	)
+	),
+
+	TP_fast_assign(
+		__entry->group		= group;
+		__entry->lvl		= group->level;
+	),
+
+	TP_printk("group=%p lvl=%d",
+		   __entry->group, __entry->lvl)
+);
+
+#endif /*  _TRACE_TIMER_MIGRATION_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
diff --git a/kernel/time/timer_migration.c b/kernel/time/timer_migration.c
index eab58bc98c7f..1526a2d61050 100644
--- a/kernel/time/timer_migration.c
+++ b/kernel/time/timer_migration.c
@@ -14,6 +14,9 @@
 #include "timer_migration.h"
 #include "tick-internal.h"
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/timer_migration.h>
+
 /*
  * The timer migration mechanism is built on a hierarchy of groups. The
  * lowest level group contains CPUs, the next level groups of CPU groups
@@ -479,6 +482,8 @@ static bool tmigr_active_up(struct tmigr_group *group,
 	 */
 	group->groupevt.ignore = 1;
 
+	trace_tmigr_group_set_cpu_active(group, newstate, childmask);
+
 	return walk_done;
 }
 
@@ -509,6 +514,7 @@ void tmigr_cpu_activate(void)
 	raw_spin_lock(&tmc->lock);
 	tmc->idle = 0;
 	tmc->wakeup = KTIME_MAX;
+	trace_tmigr_cpu_active(tmc);
 	__tmigr_cpu_activate(tmc);
 	raw_spin_unlock(&tmc->lock);
 }
@@ -625,6 +631,9 @@ static bool tmigr_update_events(struct tmigr_group *group,
 		data->nextexp = tmigr_next_groupevt_expires(group);
 	}
 
+	trace_tmigr_update_events(child, group, data->childstate,
+				  data->groupstate, nextexp);
+
 unlock:
 	raw_spin_unlock(&group->lock);
 
@@ -668,6 +677,8 @@ static u64 tmigr_new_timer(struct tmigr_cpu *tmc, u64 nextexp)
 	if (tmc->remote)
 		return KTIME_MAX;
 
+	trace_tmigr_cpu_new_timer(tmc);
+
 	tmc->cpuevt.ignore = 0;
 
 	data.groupstate.state = atomic_read(&tmc->tmgroup->migr_state);
@@ -770,6 +781,8 @@ static bool tmigr_inactive_up(struct tmigr_group *group,
 		}
 	}
 
+	trace_tmigr_group_set_cpu_inactive(group, newstate, childmask);
+
 	return walk_done;
 }
 
@@ -842,6 +855,7 @@ u64 tmigr_cpu_deactivate(u64 nextexp)
 	tmc->idle = 1;
 
 unlock:
+	trace_tmigr_cpu_idle(tmc, ret);
 	raw_spin_unlock(&tmc->lock);
 	return ret;
 }
@@ -867,6 +881,8 @@ static u64 tmigr_handle_remote_cpu(unsigned int cpu, u64 now,
 		return next;
 	}
 
+	trace_tmigr_handle_remote_cpu(tmc);
+
 	tmc->remote = 1;
 
 	/* Drop the lock to allow the remote CPU to exit idle */
@@ -952,6 +968,7 @@ static bool tmigr_handle_remote_up(struct tmigr_group *group,
 
 	childmask = data->childmask;
 
+	trace_tmigr_handle_remote(group);
 again:
 	/*
 	 * Handle the group only if @childmask is the migrator or if the
@@ -1152,6 +1169,7 @@ static struct tmigr_group *tmigr_get_group(unsigned int cpu, int node,
 
 	/* Setup successful. Add it to the hierarchy */
 	list_add(&group->list, &tmigr_level_list[lvl]);
+	trace_tmigr_group_set(group);
 	return group;
 }
 
@@ -1169,6 +1187,8 @@ static void tmigr_connect_child_parent(struct tmigr_group *child,
 	raw_spin_unlock(&parent->lock);
 	raw_spin_unlock_irq(&child->lock);
 
+	trace_tmigr_connect_child_parent(child);
+
 	/*
 	 * To prevent inconsistent states, active children need to be
 	 * active in new parent as well. Inactive children are already
@@ -1250,6 +1270,8 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
 
 			raw_spin_unlock_irq(&group->lock);
 
+			trace_tmigr_connect_cpu_parent(tmc);
+
 			/* There are no children that need to be connected */
 			continue;
 		} else {
@@ -1318,6 +1340,7 @@ static int tmigr_cpu_online(unsigned int cpu)
 		tmc->wakeup = KTIME_MAX;
 	}
 	raw_spin_lock_irq(&tmc->lock);
+	trace_tmigr_cpu_online(tmc);
 	__tmigr_cpu_activate(tmc);
 	tmc->online = 1;
 	raw_spin_unlock_irq(&tmc->lock);
@@ -1336,6 +1359,7 @@ static int tmigr_cpu_offline(unsigned int cpu)
 	 * offline; Therefore nextevt value is set to KTIME_MAX
 	 */
 	__tmigr_cpu_deactivate(tmc, KTIME_MAX);
+	trace_tmigr_cpu_offline(tmc);
 	raw_spin_unlock_irq(&tmc->lock);
 
 	return 0;
-- 
2.30.2


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

* [PATCH v7 21/21] timer: Always queue timers on the local CPU
  2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
                   ` (19 preceding siblings ...)
  2023-05-24  7:06 ` [PATCH v7 20/21] timer_migration: Add tracepoints Anna-Maria Behnsen
@ 2023-05-24  7:06 ` Anna-Maria Behnsen
  20 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-05-24  7:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Zijlstra, John Stultz, Thomas Gleixner, Eric Dumazet,
	Rafael J . Wysocki, Arjan van de Ven, Paul E . McKenney,
	Frederic Weisbecker, Rik van Riel, Steven Rostedt,
	Sebastian Siewior, Giovanni Gherdovich, Lukasz Luba,
	Gautham R . Shenoy, Anna-Maria Behnsen, Richard Cochran

The timer pull model is in place so we can remove the heuristics which try
to guess the best target CPU at enqueue/modification time.

All non pinned timers are queued on the local CPU in the separate storage
and eventually pulled at expiry time to a remote CPU.

Originally-by: Richard Cochran (linutronix GmbH) <richardcochran@gmail.com>
Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
v6:
 - Update TIMER_PINNED flag description.

v5:
 - Move WARN_ONCE() in add_timer_on() into a previous patch
 - Fold crystallball magic related hunks into this patch

v4: Update comment about TIMER_PINNED flag (heristic is removed)
---
 include/linux/timer.h | 14 ++++----------
 kernel/time/timer.c   | 42 ++++++++++++++++++++----------------------
 2 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/include/linux/timer.h b/include/linux/timer.h
index 6f96661480dd..c39ae2ad0035 100644
--- a/include/linux/timer.h
+++ b/include/linux/timer.h
@@ -50,16 +50,10 @@ struct timer_list {
  * workqueue locking issues. It's not meant for executing random crap
  * with interrupts disabled. Abuse is monitored!
  *
- * @TIMER_PINNED: A pinned timer will not be affected by any timer
- * placement heuristics (like, NOHZ) and will always expire on the CPU
- * on which the timer was enqueued.
- *
- * Note: Because enqueuing of timers can migrate the timer from one
- * CPU to another, pinned timers are not guaranteed to stay on the
- * initialy selected CPU.  They move to the CPU on which the enqueue
- * function is invoked via mod_timer() or add_timer().  If the timer
- * should be placed on a particular CPU, then add_timer_on() has to be
- * used.
+ * @TIMER_PINNED: A pinned timer will always expire on the CPU on which the
+ * timer was enqueued. When a particular CPU is required, add_timer_on()
+ * has to be used. Enqueue via mod_timer() and add_timer() is always done
+ * on the local CPU.
  */
 #define TIMER_CPUMASK		0x0003FFFF
 #define TIMER_MIGRATING		0x00040000
diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 3a8c2c88f845..5602d411a70e 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -590,10 +590,13 @@ trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
 
 	/*
 	 * We might have to IPI the remote CPU if the base is idle and the
-	 * timer is not deferrable. If the other CPU is on the way to idle
-	 * then it can't set base->is_idle as we hold the base lock:
+	 * timer is pinned. If it is a non pinned timer, it is only queued
+	 * on the remote CPU, when timer was running during queueing. Then
+	 * everything is handled by remote CPU anyway. If the other CPU is
+	 * on the way to idle then it can't set base->is_idle as we hold
+	 * the base lock:
 	 */
-	if (base->is_idle)
+	if (base->is_idle && timer->flags & TIMER_PINNED)
 		wake_up_nohz_cpu(base->cpu);
 }
 
@@ -941,17 +944,6 @@ static inline struct timer_base *get_timer_base(u32 tflags)
 	return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
 }
 
-static inline struct timer_base *
-get_target_base(struct timer_base *base, unsigned tflags)
-{
-#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
-	if (static_branch_likely(&timers_migration_enabled) &&
-	    !(tflags & TIMER_PINNED))
-		return get_timer_cpu_base(tflags, get_nohz_timer_target());
-#endif
-	return get_timer_this_cpu_base(tflags);
-}
-
 static inline void forward_timer_base(struct timer_base *base)
 {
 	unsigned long jnow = READ_ONCE(jiffies);
@@ -1103,7 +1095,7 @@ __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int option
 	if (!ret && (options & MOD_TIMER_PENDING_ONLY))
 		goto out_unlock;
 
-	new_base = get_target_base(base, timer->flags);
+	new_base = get_timer_this_cpu_base(timer->flags);
 
 	if (base != new_base) {
 		/*
@@ -2202,8 +2194,14 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 		 */
 	}
 
-	/* We need to mark both bases in sync */
-	base_local->is_idle = base_global->is_idle = is_idle;
+	/*
+	 * base->is_idle information is required to wakeup a idle CPU when
+	 * a new timer was enqueued. Only pinned timers could be enqueued
+	 * remotely into a idle base. Therefore do maintain only
+	 * base_local->is_idle information and ignore base_global->is_idle
+	 * information.
+	 */
+	base_local->is_idle = is_idle;
 
 	raw_spin_unlock(&base_global->lock);
 	raw_spin_unlock(&base_local->lock);
@@ -2233,13 +2231,13 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 void timer_clear_idle(void)
 {
 	/*
-	 * We do this unlocked. The worst outcome is a remote enqueue sending
-	 * a pointless IPI, but taking the lock would just make the window for
-	 * sending the IPI a few instructions smaller for the cost of taking
-	 * the lock in the exit from idle path.
+	 * We do this unlocked. The worst outcome is a remote pinned timer
+	 * enqueue sending a pointless IPI, but taking the lock would just
+	 * make the window for sending the IPI a few instructions smaller
+	 * for the cost of taking the lock in the exit from idle
+	 * path. Required for BASE_LOCAL only.
 	 */
 	__this_cpu_write(timer_bases[BASE_LOCAL].is_idle, false);
-	__this_cpu_write(timer_bases[BASE_GLOBAL].is_idle, false);
 
 	/* Activate without holding the timer_base->lock */
 	tmigr_cpu_activate();
-- 
2.30.2


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

* Re: [PATCH v7 02/21] timer: Do not IPI for deferrable timers
  2023-05-24  7:06 ` [PATCH v7 02/21] timer: Do not IPI for deferrable timers Anna-Maria Behnsen
@ 2023-06-05 21:29   ` Frederic Weisbecker
  0 siblings, 0 replies; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-05 21:29 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

Le Wed, May 24, 2023 at 09:06:10AM +0200, Anna-Maria Behnsen a écrit :
> Deferrable timers do not prevent CPU from going idle and are not taken into
> account on idle path. Sending an IPI to a remote CPU when a new first
> deferrable timer was enqueued will wake up the remote CPU and but nothing
> will be done regarding the deferrable timers.
> 
> Drop IPI completely when a new first deferrable timer was enqueued.
> 
> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
> ---
> v6: new patch
> ---
>  kernel/time/timer.c | 15 ++++++---------
>  1 file changed, 6 insertions(+), 9 deletions(-)
> 
> diff --git a/kernel/time/timer.c b/kernel/time/timer.c
> index 63a8ce7177dd..6e251e3cf659 100644
> --- a/kernel/time/timer.c
> +++ b/kernel/time/timer.c
> @@ -571,18 +571,15 @@ static int calc_wheel_index(unsigned long expires, unsigned long clk,
>  static void
>  trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
>  {
> -	if (!is_timers_nohz_active())
> -		return;
> -
>  	/*
> -	 * TODO: This wants some optimizing similar to the code below, but we
> -	 * will do that when we switch from push to pull for deferrable timers.
> +	 * Deferrable timers do not prevent CPU from going idle and are not
> +	 * taken into account on idle path. An IPI when a new deferrable

Just to make sure everyone is aware that this concerns also nohz_full,
this could be:

	/*
	 * Deferrable timers do not prevent CPU from entering dynticks
	 * and are not taken into account on idle/nohz_full path. An IPI
	 * when a new deferrable timer is enqueued will wake up the remote
	 * CPU but nothing will be done with the deferrable timer base.
	 * Therefore skip remote IPI for deferrable timers completely.
	 */

But anyway:

Reviewed-by: Frederic Weisbecker <frederic@kernel.org>

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

* Re: [PATCH v7 08/21] workqueue: Use global variant for add_timer()
  2023-05-24  7:06 ` [PATCH v7 08/21] workqueue: Use global variant for add_timer() Anna-Maria Behnsen
@ 2023-06-05 22:25   ` Frederic Weisbecker
  0 siblings, 0 replies; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-05 22:25 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy, Tejun Heo, Lai Jiangshan

Le Wed, May 24, 2023 at 09:06:16AM +0200, Anna-Maria Behnsen a écrit :
> The implementation of the NOHZ pull at expiry model will change the timer
> bases per CPU. Timers, that have to expire on a specific CPU, require the
> TIMER_PINNED flag. If the CPU doesn't matter, the TIMER_PINNED flag must be
> dropped. This is required for call sites which use the timer alternately as
> pinned and not pinned timer like workqueues do.
> 
> Therefore use add_timer_global() to make sure TIMER_PINNED flag is dropped.
> 
> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
> Acked-by: Tejun Heo <tj@kernel.org>
> Cc: Tejun Heo <tj@kernel.org>
> Cc: Lai Jiangshan <jiangshanlai@gmail.com>

Getting confused between v6 and v7...

Anyway:

Reviewed-by: Frederic Weisbecker <frederic@kernel.org>

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

* Re: [PATCH v7 09/21] timer: add_timer_on(): Make sure TIMER_PINNED flag is set
  2023-05-24  7:06 ` [PATCH v7 09/21] timer: add_timer_on(): Make sure TIMER_PINNED flag is set Anna-Maria Behnsen
@ 2023-06-05 22:26   ` Frederic Weisbecker
  0 siblings, 0 replies; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-05 22:26 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

Le Wed, May 24, 2023 at 09:06:17AM +0200, Anna-Maria Behnsen a écrit :
> When adding a timer to the timer wheel using add_timer_on(), it is an
> implicitly pinned timer. With the timer pull at expiry time model in place,
> TIMER_PINNED flag is required to make sure timers end up in proper base.
> 
> Add TIMER_PINNED flag unconditionally when add_timer_on() is executed.
> 
> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>

So, as in v6 obviously:

Reviewed-by: Frederic Weisbecker <frederic@kernel.org>

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

* Re: [PATCH v7 12/21] timer: Keep the pinned timers separate from the others
  2023-05-24  7:06 ` [PATCH v7 12/21] timer: Keep the pinned timers separate from the others Anna-Maria Behnsen
@ 2023-06-06 10:27   ` Frederic Weisbecker
  0 siblings, 0 replies; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-06 10:27 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy, Richard Cochran

On Wed, May 24, 2023 at 09:06:20AM +0200, Anna-Maria Behnsen wrote:
> Separate the storage space for pinned timers. Deferrable timers (doesn't
> matter if pinned or non pinned) are still enqueued into their own base.
> 
> This is preparatory work for changing the NOHZ timer placement from a push
> at enqueue time to a pull at expiry time model.
> 
> Originally-by: Richard Cochran (linutronix GmbH) <richardcochran@gmail.com>
> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>

Reviewed-by: Frederic Weisbecker <frederic@kernel.org>

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

* Re: [PATCH v7 19/21] timer: Implement the hierarchical pull model
  2023-05-24  7:06 ` [PATCH v7 19/21] timer: Implement the hierarchical pull model Anna-Maria Behnsen
@ 2023-06-06 20:10   ` Frederic Weisbecker
  2023-06-07 13:54   ` Frederic Weisbecker
                     ` (3 subsequent siblings)
  4 siblings, 0 replies; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-06 20:10 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

Le Wed, May 24, 2023 at 09:06:27AM +0200, Anna-Maria Behnsen a écrit :
> +u64 tmigr_cpu_deactivate(u64 nextexp)
> +{
> +	struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
> +	u64 ret;
> +
> +	if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online)
> +		return nextexp;
> +
> +	raw_spin_lock(&tmc->lock);
> +
> +	/*
> +	 * CPU is already deactivated in timer migration
> +	 * hierarchy. tick_nohz_get_sleep_length() calls
> +	 * tick_nohz_next_event() and thereby timer idle path is
> +	 * executed once more. tmc->wakeup holds the first timer, when
> +	 * timer migration hierarchy is completely idle and remote
> +	 * expiry was done.

This can also hold the first timer in the hierarchy on nodes
having no true migrator, but still with upper nodes having potentially
a migrator, right??

> +	 * If there is no new next expiry value
> +	 * handed in which should be inserted into the timer migration
> +	 * hierarchy, wakeup value is returned.
> +	 */
> +	if (tmc->idle) {
> +		ret = tmc->wakeup;
> +
> +		tmc->wakeup = KTIME_MAX;
> +
> +		if (nextexp != KTIME_MAX) {

This seem to assume that tick_nohz_next_event() on the last idle CPU in an idle
hierarchy is always called right after a timer interrupt arriving on time
(ie: right after a call to tmigr_handle_remote()), but this can actually be called
after any interrupt. Can the following happen or am I overlooking something?

do_idle() {
	// ===> <IRQ>
	tmigr_handle_remote() {
		// find some timer in the hierarchy
		// expiring in 2 jiffies
		tmc->wakeup = jiffies + 2
	}
	//<=== </IRQ>
	tick_nohz_get_sleep_length() {
		get_next_timer_interrupt() {
			tmigr_cpu_deactivate() {
				wakeup = tmc->wakeup
				tmc->wakeup = KTIME_MAX
				return wakeup // jiffies + 2
			}
		}
	}
	tick_nohz_idle_stop_tick() {
		// SLEEP 2 jiffies
	}

	// ===> <IRQ>
	// whatever IRQ that is not timer
	// or a timer IRQ firing too early
	//<=== </IRQ>
	
	tick_nohz_get_sleep_length() {
		get_next_timer_interrupt() {
			tmigr_cpu_deactivate() {
				wakeup = tmc->wakeup
				 tmc->wakeup = KTIME_MAX
				return wakeup // KTIME_MAX
			}
		}
	}
	tick_nohz_idle_stop_tick() {
		// SLEEP forever, misses the timer in 2 jiffies
	}
}

Thanks.

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

* Re: [PATCH v7 19/21] timer: Implement the hierarchical pull model
  2023-05-24  7:06 ` [PATCH v7 19/21] timer: Implement the hierarchical pull model Anna-Maria Behnsen
  2023-06-06 20:10   ` Frederic Weisbecker
@ 2023-06-07 13:54   ` Frederic Weisbecker
  2023-06-12 12:29     ` Anna-Maria Behnsen
  2023-06-11 21:19   ` Frederic Weisbecker
                     ` (2 subsequent siblings)
  4 siblings, 1 reply; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-07 13:54 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

Le Wed, May 24, 2023 at 09:06:27AM +0200, Anna-Maria Behnsen a écrit :
> +/*
> + * Returns true, if there is nothing to be propagated to the next level
> + *
> + * @data->nextexp is reset to KTIME_MAX; it is reused for first global
> + * event which needs to be handled by migrator (in toplevel group)
> + *
> + * This is the only place where group event expiry value is set.
> + */
> +static bool tmigr_update_events(struct tmigr_group *group,
> +				struct tmigr_group *child,
> +				struct tmigr_walk *data)
> +{
> +	struct tmigr_event *evt, *first_childevt;
> +	bool walk_done, remote = data->remote;
> +	u64 nextexp;
> +
> +	if (child) {
> +		if (data->childstate.active)
> +			return true;
> +
> +		raw_spin_lock(&child->lock);
> +		raw_spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING);
> +
> +		first_childevt = tmigr_next_groupevt(child);
> +		nextexp = child->next_expiry;
> +		evt = &child->groupevt;
> +	} else {
> +		nextexp = data->nextexp;
> +
> +		/*
> +		 * Set @data->nextexp to KTIME_MAX; it is reused for first
> +		 * global event which needs to be handled by migrator (in
> +		 * toplevel group)
> +		 */
> +		data->nextexp = KTIME_MAX;
> +
> +		first_childevt = evt = data->evt;
> +
> +		/*
> +		 * Walking the hierarchy is required in any case, when a
> +		 * remote expiry was done before.

You can probably remove that comma because it feels like breaking the condition link.

> +		 * This ensures to not lost

lose

> +		 * already queued events in non active groups (see section
> +		 * "Required event and timerqueue update after remote
> +		 * expiry" in documentation at the top).
> +		 */
> +		if (evt->ignore && !remote)

It looks like in the case of !remote, this branch will never end up
stopping the propagation up because either:

* We come here from tmigr_inactive_up() which takes care of the propagation.

or

* We come here from tmigr_new_timer() and ->ignore can't be set.

If I'm right, can we add a comment about that so that the poor reviewer
doesn't stutter on that for too long?

Thanks.

> +			return true;

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

* Re: [PATCH v7 15/21] timer: Add get next timer interrupt functionality for remote CPUs
  2023-05-24  7:06 ` [PATCH v7 15/21] timer: Add get next timer interrupt functionality for remote CPUs Anna-Maria Behnsen
@ 2023-06-07 14:55   ` Frederic Weisbecker
  2023-06-12 12:01     ` Anna-Maria Behnsen
  0 siblings, 1 reply; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-07 14:55 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

On Wed, May 24, 2023 at 09:06:23AM +0200, Anna-Maria Behnsen wrote:
> +# ifdef CONFIG_SMP
> +/**
> + * fetch_next_timer_interrupt_remote
> + * @basej:	base time jiffies
> + * @basem:	base time clock monotonic
> + * @tevt:	Pointer to the storage for the expiry values
> + * @cpu:	Remote CPU
> + *
> + * Stores the next pending local and global timer expiry values in the
> + * struct pointed to by @tevt. If a queue is empty the corresponding
> + * field is set to KTIME_MAX. If local event expires before global
> + * event, global event is set to KTIME_MAX as well.
> + *
> + * Caller needs to make sure timer base locks are held (use
> + * timer_lock_remote_bases() for this purpose). Caller must make sure
> + * interrupts are reopened, if required.

That last sentence looks weird. All we want is the caller to call this
and the timer_[un]lock functions with interrupts disabled.

Now of course at some point the caller will need to unmask the IRQs
but heh, not our problem :)

Reviewed-by: Frederic Weisbecker <frederic@kernel.org>

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

* Re: [PATCH v7 19/21] timer: Implement the hierarchical pull model
  2023-05-24  7:06 ` [PATCH v7 19/21] timer: Implement the hierarchical pull model Anna-Maria Behnsen
  2023-06-06 20:10   ` Frederic Weisbecker
  2023-06-07 13:54   ` Frederic Weisbecker
@ 2023-06-11 21:19   ` Frederic Weisbecker
  2023-06-12 15:09   ` Frederic Weisbecker
  2023-06-13 10:51   ` Frederic Weisbecker
  4 siblings, 0 replies; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-11 21:19 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

Le Wed, May 24, 2023 at 09:06:27AM +0200, Anna-Maria Behnsen a écrit :
> +static bool tmigr_inactive_up(struct tmigr_group *group,
> +			      struct tmigr_group *child,
> +			      void *ptr)
> +{
> +	union tmigr_state curstate, newstate;
> +	struct tmigr_walk *data = ptr;
> +	bool walk_done;
> +	u8 childmask;
> +
> +	childmask = data->childmask;
> +	newstate = curstate = data->groupstate;
> +
> +retry:
> +	walk_done = true;
> +
> +	/* Reset active bit when child is no longer active */
> +	if (!data->childstate.active)
> +		newstate.active &= ~childmask;
> +
> +	if (newstate.migrator == childmask) {
> +		/*
> +		 * Find a new migrator for the group, because child group
> +		 * is idle!
> +		 */
> +		if (!data->childstate.active) {
> +			unsigned long new_migr_bit, active = newstate.active;
> +
> +			new_migr_bit = find_first_bit(&active, BIT_CNT);
> +
> +			if (new_migr_bit != BIT_CNT) {
> +				newstate.migrator = BIT(new_migr_bit);
> +			} else {
> +				newstate.migrator = TMIGR_NONE;
> +
> +				/* Changes need to be propagated */
> +				walk_done = false;
> +			}
> +		}
> +	}
> +
> +	newstate.seq++;
> +
> +	WARN_ON_ONCE((newstate.migrator != TMIGR_NONE) && !(newstate.active));
> +
> +	if (!atomic_try_cmpxchg(&group->migr_state, &curstate.state, newstate.state)) {
> +		newstate.state = curstate.state;
> +
> +		/*
> +		 * Something changed in child/parent group in the meantime,
> +		 * reread the state of child and parent; Update of
> +		 * data->childstate is required for event handling;
> +		 */
> +		if (child)
> +			data->childstate.state = atomic_read(&child->migr_state);
> +
> +		goto retry;
> +	}
> +
> +	data->groupstate = newstate;
> +	data->remote = false;
> +
> +	/* Event Handling */
> +	tmigr_update_events(group, child, data);
> +
> +	if (group->parent && (walk_done == false)) {
> +		data->childmask = group->childmask;
> +		data->childstate = newstate;
> +		data->groupstate.state = atomic_read(&group->parent->migr_state);
> +	}
> +
> +	/*
> +	 * data->nextexp was set by tmigr_update_events() and contains the
> +	 * expiry of first global event which needs to be handled
> +	 */
> +	if (data->nextexp != KTIME_MAX) {
> +		WARN_ON_ONCE(group->parent);
> +		/*
> +		 * Toplevel path - If this cpu is about going offline wake
> +		 * up some random other cpu so it will take over the
> +		 * migrator duty and program its timer properly. Ideally
> +		 * wake the cpu with the closest expiry time, but that's
> +		 * overkill to figure out.
> +		 */
> +		if (!(this_cpu_ptr(&tmigr_cpu)->online)) {
> +			unsigned int cpu = smp_processor_id();
> +
> +			cpu = cpumask_any_but(cpu_online_mask, cpu);
> +			smp_send_reschedule(cpu);

Is that enough?
Assume the following:

* CPU 0 is idle and CPU 1 is the migrator
* CPU 1 goes offline and send the above IPI to CPU 0.
* CPU 0 doesn't have any global timer in its own queue, it calls
  tmigr_cpu_deactivate() with KTIME_MAX. It's already ->idle, so it
  returns ->wakeup that may be KTIME_MAX (also the call to tmigr_new_timer()
  is ignored)
* CPU 0 doesn't handle the idle migrator duty

Or is there something else I'm missing?

I guess the effect should be mostly invisible due to the fact the hotplug
BP process has to be carried by another active CPU afterward, which will take
the migrator duty.

But still, it means you may have no migrator between CPUHP_AP_TMIGR_ONLINE and
CPUHP_AP_IDLE_DEAD.

Thanks.

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

* Re: [PATCH v7 15/21] timer: Add get next timer interrupt functionality for remote CPUs
  2023-06-07 14:55   ` Frederic Weisbecker
@ 2023-06-12 12:01     ` Anna-Maria Behnsen
  0 siblings, 0 replies; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-06-12 12:01 UTC (permalink / raw)
  To: Frederic Weisbecker
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

On Wed, 7 Jun 2023, Frederic Weisbecker wrote:

> On Wed, May 24, 2023 at 09:06:23AM +0200, Anna-Maria Behnsen wrote:
> > +# ifdef CONFIG_SMP
> > +/**
> > + * fetch_next_timer_interrupt_remote
> > + * @basej:	base time jiffies
> > + * @basem:	base time clock monotonic
> > + * @tevt:	Pointer to the storage for the expiry values
> > + * @cpu:	Remote CPU
> > + *
> > + * Stores the next pending local and global timer expiry values in the
> > + * struct pointed to by @tevt. If a queue is empty the corresponding
> > + * field is set to KTIME_MAX. If local event expires before global
> > + * event, global event is set to KTIME_MAX as well.
> > + *
> > + * Caller needs to make sure timer base locks are held (use
> > + * timer_lock_remote_bases() for this purpose). Caller must make sure
> > + * interrupts are reopened, if required.
> 
> That last sentence looks weird. All we want is the caller to call this
> and the timer_[un]lock functions with interrupts disabled.
> 
> Now of course at some point the caller will need to unmask the IRQs
> but heh, not our problem :)

I'll drop the last sentence :)

> Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
> 


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

* Re: [PATCH v7 19/21] timer: Implement the hierarchical pull model
  2023-06-07 13:54   ` Frederic Weisbecker
@ 2023-06-12 12:29     ` Anna-Maria Behnsen
  2023-06-12 14:30       ` Frederic Weisbecker
  0 siblings, 1 reply; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-06-12 12:29 UTC (permalink / raw)
  To: Frederic Weisbecker
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

[-- Attachment #1: Type: text/plain, Size: 3165 bytes --]

On Wed, 7 Jun 2023, Frederic Weisbecker wrote:

> Le Wed, May 24, 2023 at 09:06:27AM +0200, Anna-Maria Behnsen a écrit :
> > +/*
> > + * Returns true, if there is nothing to be propagated to the next level
> > + *
> > + * @data->nextexp is reset to KTIME_MAX; it is reused for first global
> > + * event which needs to be handled by migrator (in toplevel group)
> > + *
> > + * This is the only place where group event expiry value is set.
> > + */
> > +static bool tmigr_update_events(struct tmigr_group *group,
> > +				struct tmigr_group *child,
> > +				struct tmigr_walk *data)
> > +{
> > +	struct tmigr_event *evt, *first_childevt;
> > +	bool walk_done, remote = data->remote;
> > +	u64 nextexp;
> > +
> > +	if (child) {
> > +		if (data->childstate.active)
> > +			return true;
> > +
> > +		raw_spin_lock(&child->lock);
> > +		raw_spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING);
> > +
> > +		first_childevt = tmigr_next_groupevt(child);
> > +		nextexp = child->next_expiry;
> > +		evt = &child->groupevt;
> > +	} else {
> > +		nextexp = data->nextexp;
> > +
> > +		/*
> > +		 * Set @data->nextexp to KTIME_MAX; it is reused for first
> > +		 * global event which needs to be handled by migrator (in
> > +		 * toplevel group)
> > +		 */
> > +		data->nextexp = KTIME_MAX;
> > +
> > +		first_childevt = evt = data->evt;
> > +
> > +		/*
> > +		 * Walking the hierarchy is required in any case, when a
> > +		 * remote expiry was done before.
> 
> You can probably remove that comma because it feels like breaking the condition link.
> 
> > +		 * This ensures to not lost
> 
> lose
> 
> > +		 * already queued events in non active groups (see section
> > +		 * "Required event and timerqueue update after remote
> > +		 * expiry" in documentation at the top).
> > +		 */
> > +		if (evt->ignore && !remote)
> 
> It looks like in the case of !remote, this branch will never end up
> stopping the propagation up because either:
> 
> * We come here from tmigr_inactive_up() which takes care of the propagation.
> 
> or
> 
> * We come here from tmigr_new_timer() and ->ignore can't be set.
> 
> If I'm right, can we add a comment about that so that the poor reviewer
> doesn't stutter on that for too long?
> 

Right. It will never stop the propagation - but the condition could be
fulfilled when call site is tmigr_inactive_up(). My proposal for expanding
the comment is the following:

	/*
	 * Walking the hierarchy is required in any case when a
	 * remote expiry was done before. This ensures to not lose
	 * already queued events in non active groups (see section
	 * "Required event and timerqueue update after remote
	 * expiry" in documentation at the top).
	 *
	 * The two call sites which are executed without a remote expiry
	 * before, are not prevented from propagating changes through
	 * the hierarchy by the return:
	 *  - When entering this path by tmigr_new_timer(), evt->ignore
	 *    is never set.
	 *  - tmigr_inactive_up() takes care of propagation by itself
	 *    and ignores return value. But an immediate return is
	 *    required because nothing has to be done in this level as
	 *    event could be ignored.
	 */

Thanks,

	Anna-Maria

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

* Re: [PATCH v7 19/21] timer: Implement the hierarchical pull model
  2023-06-12 12:29     ` Anna-Maria Behnsen
@ 2023-06-12 14:30       ` Frederic Weisbecker
  0 siblings, 0 replies; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-12 14:30 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

Le Mon, Jun 12, 2023 at 02:29:25PM +0200, Anna-Maria Behnsen a écrit :
> On Wed, 7 Jun 2023, Frederic Weisbecker wrote:
> Right. It will never stop the propagation - but the condition could be
> fulfilled when call site is tmigr_inactive_up(). My proposal for expanding
> the comment is the following:
> 
> 	/*
> 	 * Walking the hierarchy is required in any case when a
> 	 * remote expiry was done before. This ensures to not lose
> 	 * already queued events in non active groups (see section
> 	 * "Required event and timerqueue update after remote
> 	 * expiry" in documentation at the top).
> 	 *
> 	 * The two call sites which are executed without a remote expiry
> 	 * before, are not prevented from propagating changes through
> 	 * the hierarchy by the return:
> 	 *  - When entering this path by tmigr_new_timer(), evt->ignore
> 	 *    is never set.
> 	 *  - tmigr_inactive_up() takes care of propagation by itself
> 	 *    and ignores return value. But an immediate return is
> 	 *    required because nothing has to be done in this level as
> 	 *    event could be ignored.
> 	 */

Yes, very good!

Thanks.

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

* Re: [PATCH v7 19/21] timer: Implement the hierarchical pull model
  2023-05-24  7:06 ` [PATCH v7 19/21] timer: Implement the hierarchical pull model Anna-Maria Behnsen
                     ` (2 preceding siblings ...)
  2023-06-11 21:19   ` Frederic Weisbecker
@ 2023-06-12 15:09   ` Frederic Weisbecker
  2023-06-12 15:57     ` Anna-Maria Behnsen
  2023-06-13 10:51   ` Frederic Weisbecker
  4 siblings, 1 reply; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-12 15:09 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

Le Wed, May 24, 2023 at 09:06:27AM +0200, Anna-Maria Behnsen a écrit :
> +void tmigr_handle_remote(void)
> +{
> +	struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
> +	struct tmigr_remote_data data;
> +
> +	if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online)
> +		return;
> +
> +	/*
> +	 * NOTE: This is a doubled check because migrator test will be done
> +	 * in tmigr_handle_remote_up() anyway. Keep this check to fasten
> +	 * the return when nothing has to be done.
> +	 */
> +	if (!tmigr_check_migrator(tmc->tmgroup, tmc->childmask))
> +		return;
> +
> +	data.now = get_jiffies_update(&data.basej);
> +	data.childmask = tmc->childmask;
> +	data.wakeup = KTIME_MAX;
> +
> +	__walk_groups(&tmigr_handle_remote_up, &data, tmc);
> +
> +	raw_spin_lock_irq(&tmc->lock);
> +	if (tmc->idle)
> +		tmc->wakeup = data.wakeup;

So this assumes there will be a timer re-eavluation right after
in the idle path but this is not true in the case of nohz_full.

If the softirq happens at the tail of the timer interrupt, you're
good because the dynticks is re-evaluated right after. But if the
softirq happens in ksoftirqd, you'll have no timer re-evaluation.

The crude trick used by nohz_full in order to re-evaluate the dynticks when
a timer is queued from the timer softirq is to launch a self IPI (from
trigger_dyntick_cpu()). Here you would need something like that but
that's not something we wish either.

In fact we don't want any nohz_full CPU to perform an idle migrator job.
And the problem here is that whenever a timer interrupt occurs while
tmc->idle == 1 (and that _might_ happen in nohz_full), it will go up the
hierarchy as long as there is no active migrator on a given level and
check for remote expiry. And if something expired it will not only perform
the remote callbacks handling but also reprogram the next tick to fire in
the next expiry. That's a potential disturbance on a nohz_full CPU.

There is always an active CPU in nohz_full so there is always an active
migrator at least at the top level. Therefore you can spare concurrent
idle migrators if they are nohz_full.

My suggestion is to make tmigr_requires_handle_remote() return 0 if
tick_nohz_full_cpu(smp_processor_id()), so that we don't even bother
raising the softirq. But if the timer softirq happens nevertheless, due
to some local timer to process, also make tmigr_handle_remote() to
return early.

Thanks.

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

* Re: [PATCH v7 19/21] timer: Implement the hierarchical pull model
  2023-06-12 15:09   ` Frederic Weisbecker
@ 2023-06-12 15:57     ` Anna-Maria Behnsen
  2023-06-12 21:20       ` Frederic Weisbecker
  0 siblings, 1 reply; 37+ messages in thread
From: Anna-Maria Behnsen @ 2023-06-12 15:57 UTC (permalink / raw)
  To: Frederic Weisbecker
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

[-- Attachment #1: Type: text/plain, Size: 3598 bytes --]

On Mon, 12 Jun 2023, Frederic Weisbecker wrote:

> Le Wed, May 24, 2023 at 09:06:27AM +0200, Anna-Maria Behnsen a écrit :
> > +void tmigr_handle_remote(void)
> > +{
> > +	struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
> > +	struct tmigr_remote_data data;
> > +
> > +	if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online)
> > +		return;
> > +
> > +	/*
> > +	 * NOTE: This is a doubled check because migrator test will be done
> > +	 * in tmigr_handle_remote_up() anyway. Keep this check to fasten
> > +	 * the return when nothing has to be done.
> > +	 */
> > +	if (!tmigr_check_migrator(tmc->tmgroup, tmc->childmask))
> > +		return;
> > +
> > +	data.now = get_jiffies_update(&data.basej);
> > +	data.childmask = tmc->childmask;
> > +	data.wakeup = KTIME_MAX;
> > +
> > +	__walk_groups(&tmigr_handle_remote_up, &data, tmc);
> > +
> > +	raw_spin_lock_irq(&tmc->lock);
> > +	if (tmc->idle)
> > +		tmc->wakeup = data.wakeup;
> 
> So this assumes there will be a timer re-eavluation right after
> in the idle path but this is not true in the case of nohz_full.
> 
> If the softirq happens at the tail of the timer interrupt, you're
> good because the dynticks is re-evaluated right after. But if the
> softirq happens in ksoftirqd, you'll have no timer re-evaluation.

Ok. This problem is around only for nohz_full and not for nohz idle?

> The crude trick used by nohz_full in order to re-evaluate the dynticks when
> a timer is queued from the timer softirq is to launch a self IPI (from
> trigger_dyntick_cpu()). Here you would need something like that but
> that's not something we wish either.
> 
> In fact we don't want any nohz_full CPU to perform an idle migrator job.
> And the problem here is that whenever a timer interrupt occurs while
> tmc->idle == 1 (and that _might_ happen in nohz_full), it will go up the
> hierarchy as long as there is no active migrator on a given level and
> check for remote expiry. And if something expired it will not only perform
> the remote callbacks handling but also reprogram the next tick to fire in
> the next expiry. That's a potential disturbance on a nohz_full CPU.
> 
> There is always an active CPU in nohz_full so there is always an active
> migrator at least at the top level. Therefore you can spare concurrent
> idle migrators if they are nohz_full.
>

As long as the top level group is not/never idle, the wakeup value will be
KTIME_MAX and so it is no problem for nohz_full cpus - or? The check for
handling remote expiry is still a problem but please read my proposal for
this below.

> My suggestion is to make tmigr_requires_handle_remote() return 0 if
> tick_nohz_full_cpu(smp_processor_id()), so that we don't even bother
> raising the softirq. But if the timer softirq happens nevertheless, due
> to some local timer to process, also make tmigr_handle_remote() to
> return early.

Regarding this problem and also the two things you mentioned in the two
earlier review remarks (timer IRQ which fires too early, IPI when CPU goes
offline), I would propose to use the tmc->wakeup value slightly different
as it is used right now:

 - Whenever a wakeup value is required, because top level group is
   completely idle, the value is set in per CPU tmc struct. It could be
   then reevaluated in idle code in irq exit path.

 - For checking whether remote expiry is required, the wakeup value could
   also be used.

 - For nohz_full CPUs wakeup will be always KTIME_MAX - under the
   assumption that there is alwasy an active CPU in top level group.

What do you think about this? Do I miss something else here?

Thanks,

	Anna-Maria

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

* Re: [PATCH v7 19/21] timer: Implement the hierarchical pull model
  2023-06-12 15:57     ` Anna-Maria Behnsen
@ 2023-06-12 21:20       ` Frederic Weisbecker
  0 siblings, 0 replies; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-12 21:20 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

Le Mon, Jun 12, 2023 at 05:57:00PM +0200, Anna-Maria Behnsen a écrit :
> Ok. This problem is around only for nohz_full and not for nohz idle?

Only nohz_full right.

> 
> > The crude trick used by nohz_full in order to re-evaluate the dynticks when
> > a timer is queued from the timer softirq is to launch a self IPI (from
> > trigger_dyntick_cpu()). Here you would need something like that but
> > that's not something we wish either.
> > 
> > In fact we don't want any nohz_full CPU to perform an idle migrator job.
> > And the problem here is that whenever a timer interrupt occurs while
> > tmc->idle == 1 (and that _might_ happen in nohz_full), it will go up the
> > hierarchy as long as there is no active migrator on a given level and
> > check for remote expiry. And if something expired it will not only perform
> > the remote callbacks handling but also reprogram the next tick to fire in
> > the next expiry. That's a potential disturbance on a nohz_full CPU.
> > 
> > There is always an active CPU in nohz_full so there is always an active
> > migrator at least at the top level. Therefore you can spare concurrent
> > idle migrators if they are nohz_full.
> >
> 
> As long as the top level group is not/never idle, the wakeup value will be
> KTIME_MAX and so it is no problem for nohz_full cpus - or? The check for
> handling remote expiry is still a problem but please read my proposal for
> this below.

Good point, I overlooked the fact that data->nextevt is only set if the
top level has no migrator.

So the only issue is that a nohz_full CPU may accidentally check/do the
remote expiry as a loose idle migrator. Which can add noise, etc...

> 
> > My suggestion is to make tmigr_requires_handle_remote() return 0 if
> > tick_nohz_full_cpu(smp_processor_id()), so that we don't even bother
> > raising the softirq. But if the timer softirq happens nevertheless, due
> > to some local timer to process, also make tmigr_handle_remote() to
> > return early.
> 
> Regarding this problem and also the two things you mentioned in the two
> earlier review remarks (timer IRQ which fires too early, IPI when CPU goes
> offline), I would propose to use the tmc->wakeup value slightly different
> as it is used right now:
> 
>  - Whenever a wakeup value is required, because top level group is
>    completely idle, the value is set in per CPU tmc struct. It could be
>    then reevaluated in idle code in irq exit path.

So you want to force reevaluation of tmc->wakeup unconditionally on
deactivate time through tmigr_new_timer(), right? That would indeed work
in any case. At the cost of some more overhead in the idle interrupt path,
but perhaps hardly measurable...

The alternative would be to reset tmc->wakeup only when that deadline is
reached in tmigr_requires_handle_remote(). And then have a special case
in the offlining case. That's less pretty of course.

> 
>  - For checking whether remote expiry is required, the wakeup value could
>    also be used.

Right.

> 
>  - For nohz_full CPUs wakeup will be always KTIME_MAX - under the
>    assumption that there is alwasy an active CPU in top level group.

Sounds good!

Thanks.

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

* Re: [PATCH v7 19/21] timer: Implement the hierarchical pull model
  2023-05-24  7:06 ` [PATCH v7 19/21] timer: Implement the hierarchical pull model Anna-Maria Behnsen
                     ` (3 preceding siblings ...)
  2023-06-12 15:09   ` Frederic Weisbecker
@ 2023-06-13 10:51   ` Frederic Weisbecker
  4 siblings, 0 replies; 37+ messages in thread
From: Frederic Weisbecker @ 2023-06-13 10:51 UTC (permalink / raw)
  To: Anna-Maria Behnsen
  Cc: linux-kernel, Peter Zijlstra, John Stultz, Thomas Gleixner,
	Eric Dumazet, Rafael J . Wysocki, Arjan van de Ven,
	Paul E . McKenney, Frederic Weisbecker, Rik van Riel,
	Steven Rostedt, Sebastian Siewior, Giovanni Gherdovich,
	Lukasz Luba, Gautham R . Shenoy

On Wed, May 24, 2023 at 09:06:27AM +0200, Anna-Maria Behnsen wrote:
> +static bool tmigr_update_events(struct tmigr_group *group,
> +				struct tmigr_group *child,
> +				struct tmigr_walk *data)
> +{
> +	struct tmigr_event *evt, *first_childevt;
> +	bool walk_done, remote = data->remote;
> +	u64 nextexp;
> +
> +	if (child) {
> +		if (data->childstate.active)
> +			return true;
> +
> +		raw_spin_lock(&child->lock);
> +		raw_spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING);
> +
> +		first_childevt = tmigr_next_groupevt(child);
> +		nextexp = child->next_expiry;
> +		evt = &child->groupevt;
> +	} else {
> +		nextexp = data->nextexp;
> +
> +		/*
> +		 * Set @data->nextexp to KTIME_MAX; it is reused for first
> +		 * global event which needs to be handled by migrator (in
> +		 * toplevel group)
> +		 */
> +		data->nextexp = KTIME_MAX;
> +
> +		first_childevt = evt = data->evt;
> +
> +		/*
> +		 * Walking the hierarchy is required in any case, when a
> +		 * remote expiry was done before. This ensures to not lost
> +		 * already queued events in non active groups (see section
> +		 * "Required event and timerqueue update after remote
> +		 * expiry" in documentation at the top).
> +		 */
> +		if (evt->ignore && !remote)
> +			return true;
> +
> +		raw_spin_lock(&group->lock);
> +	}
> +
> +	if (nextexp == KTIME_MAX) {
> +		evt->ignore = 1;
> +
> +		/*
> +		 * When next child event could be ignored (nextexp is
> +		 * KTIME_MAX) and there was no remote timer handling before
> +		 * or the group is already active, there is no need to walk
> +		 * the hierarchy even if there is a parent group.
> +		 *
> +		 * The other way round: even if the event could be ignored,
> +		 * but if a remote timer handling was executed before and
> +		 * the group is not active, walking the hierarchy is
> +		 * required to not miss an enqueued timer in the non active
> +		 * group. The enqueued timer of the group needs to be
> +		 * propagated to a higher level to ensure it is handled.
> +		 */
> +		if (!remote || data->groupstate.active) {
> +			walk_done = true;
> +			goto unlock;
> +		}
> +	} else {
> +		/*
> +		 * Update of event cpu and ignore bit is required only when
> +		 * @child is set (child is equal or higher than lvl0), but
> +		 * it doesn't matter if it is written once more to per cpu
> +		 * event; make the update unconditional.
> +		 */
> +		evt->cpu = first_childevt->cpu;
> +		evt->ignore = 0;
> +	}
> +
> +	walk_done = !group->parent;
> +
> +	/*
> +	 * If child event is already queued in group, remove it from queue
> +	 * when expiry time changed only.
> +	 */
> +	if (timerqueue_node_queued(&evt->nextevt)) {
> +		if (evt->nextevt.expires == nextexp)
> +			goto check_toplvl;
> +		else if (!timerqueue_del(&group->events, &evt->nextevt))
> +			WRITE_ONCE(group->next_expiry, KTIME_MAX);
> +	}
> +
> +	evt->nextevt.expires = nextexp;
> +
> +	if (timerqueue_add(&group->events, &evt->nextevt))
> +		WRITE_ONCE(group->next_expiry, nextexp);
> +
> +check_toplvl:
> +	if (walk_done && (data->groupstate.migrator == TMIGR_NONE)) {
> +		/*
> +		 * Toplevel group is idle and it has to be ensured global
> +		 * timers are handled in time. (This could be optimized by
> +		 * keeping track of the last global scheduled event and
> +		 * only arming it on CPU if the new event is earlier. Not
> +		 * sure if its worth the complexity.)
> +		 */
> +		data->nextexp = tmigr_next_groupevt_expires(group);

This looks racy against a concurrent top migrator going idle.

1) Suppose you have the following configuration:

    LVL 1            [GRP1:0]
                     migrator = GRP0:1
                     active   = GRP0:1
                     nextevt  = KTIME_MAX
                   /                \
    LVL 0  [GRP0:0]                  [GRP0:1]
           migrator = NONE           migrator = CPU2
           active   = NONE           active   = CPU2
           nextevt  = KTIME_MAX      nextevt  = KTIME_MAX
              /         \                /         \
    CPUs     0           1              2           3
            idle       idle           active      idle

2) Now CPU 2 goes idle and propagates that entirely:

    LVL 1            [GRP1:0]
                     migrator = NONE
                     active   = NONE
                     nextevt  = KTIME_MAX
                   /                \
    LVL 0  [GRP0:0]                  [GRP0:1]
           migrator = NONE           migrator = NONE
           active   = NONE           active   = NONE
           nextevt  = KTIME_MAX      nextevt  = KTIME_MAX
              /         \                /         \
    CPUs     0           1              2           3
            idle       idle           active      idle

3) CPU 0 has a new timer queued from idle, it propagates that
   to LVL 0, so far so good:

    LVL 1            [GRP1:0]
                     migrator = NONE
                     active   = NONE
                     nextevt  = KTIME_MAX
                   /                \
    LVL 0  [GRP0:0]                  [GRP0:1]
           migrator = NONE           migrator = NONE
           active   = NONE           active   = NONE
           nextevt  = TIMER0         nextevt  = KTIME_MAX
              /         \                /         \
    CPUs     0           1              2           3
            idle       idle           active      idle

4) Finally CPU 0 is about to propagate TIMER0 to LVL1,
   so in tmigr_new_timer_up() it reads the state of GRP1:0
   but what makes sure it reads the freshest value modified
   in step 2 ? There is no locking or cmpxchg() from the reader
   side. It's a plain atomic_read(). It may well observe
   the stalled value that was in step 1 which has migrator == GRP0:1
   and as a result data->nextexp will stay KTIME_MAX and TIMER0 will
   be ignored entirely.


As usual I prefer to announce I may be easily missing something that
makes this impossible to happen :)

Otherwise, fortunately there is an easy way to fix this, you need to read the
group state within the group->lock. Because then you have this ordering:

    tmigr_inactive_up()                               tmigr_new_timer_up()
    -------------------                               --------------------
    cmpxchg(&group->migr_state, ..., TMIGR_NONE)
    tmigr_update_events() {
        spin_lock(&group->lock)
        //update events ....
        spin_unlock(&group->lock) //release migr_state modification
    }
                                                      tmigr_update_events() {
                                                          spin_lock(&group->lock) // acquire migr_state modificaton
                                                          group_state = atomic_read(&group->migr_state)
                                                          //update events ....
                                                          spin_unlock(&group->lock) //makes migr_state visible
                                                       }						       

Everything happening before an UNLOCK (including before the preceding LOCK) is
visible after the next LOCK. So it works. And if tmigr_new_timer_up() runs
while tmigr_inactive_up() is between the group->migr_state modification and
the call to tmigr_update_events(), it's fine as well because it will lock
group->lock and re-evaluate the situation.

I see another check on data->groupstate.active in tmigr_new_timer_up()
when nextexp == KTIME_MAX. From a quick glance it has the same issue and
could be fixed along the same way.

Thanks.

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

end of thread, other threads:[~2023-06-13 10:52 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-24  7:06 [PATCH v7 00/21] timer: Move from a push remote at enqueue to a pull at expiry model Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 01/21] tick-sched: Warn when next tick seems to be in the past Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 02/21] timer: Do not IPI for deferrable timers Anna-Maria Behnsen
2023-06-05 21:29   ` Frederic Weisbecker
2023-05-24  7:06 ` [PATCH v7 03/21] timer: Add comment to get_next_timer_interrupt() description Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 04/21] timer: Move store of next event into __next_timer_interrupt() Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 05/21] timer: Split next timer interrupt logic Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 06/21] timer: Rework idle logic Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 07/21] timers: Introduce add_timer() variants which modify timer flags Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 08/21] workqueue: Use global variant for add_timer() Anna-Maria Behnsen
2023-06-05 22:25   ` Frederic Weisbecker
2023-05-24  7:06 ` [PATCH v7 09/21] timer: add_timer_on(): Make sure TIMER_PINNED flag is set Anna-Maria Behnsen
2023-06-05 22:26   ` Frederic Weisbecker
2023-05-24  7:06 ` [PATCH v7 10/21] timers: Ease code in run_local_timers() Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 11/21] timers: Create helper function to forward timer base clk Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 12/21] timer: Keep the pinned timers separate from the others Anna-Maria Behnsen
2023-06-06 10:27   ` Frederic Weisbecker
2023-05-24  7:06 ` [PATCH v7 13/21] timer: Retrieve next expiry of pinned/non-pinned timers separately Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 14/21] timer: Split out "get next timer interrupt" functionality Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 15/21] timer: Add get next timer interrupt functionality for remote CPUs Anna-Maria Behnsen
2023-06-07 14:55   ` Frederic Weisbecker
2023-06-12 12:01     ` Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 16/21] timer: Restructure internal locking Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 17/21] timer: Check if timers base is handled already Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 18/21] tick/sched: Split out jiffies update helper function Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 19/21] timer: Implement the hierarchical pull model Anna-Maria Behnsen
2023-06-06 20:10   ` Frederic Weisbecker
2023-06-07 13:54   ` Frederic Weisbecker
2023-06-12 12:29     ` Anna-Maria Behnsen
2023-06-12 14:30       ` Frederic Weisbecker
2023-06-11 21:19   ` Frederic Weisbecker
2023-06-12 15:09   ` Frederic Weisbecker
2023-06-12 15:57     ` Anna-Maria Behnsen
2023-06-12 21:20       ` Frederic Weisbecker
2023-06-13 10:51   ` Frederic Weisbecker
2023-05-24  7:06 ` [PATCH v7 20/21] timer_migration: Add tracepoints Anna-Maria Behnsen
2023-05-24  7:06 ` [PATCH v7 21/21] timer: Always queue timers on the local CPU Anna-Maria Behnsen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.