timers: Move from a push remote at enqueue to a pull at expiry model
Subsystems (6)
Anna-Maria Behnsen
maintainerv9 cover timers: Move from a push remote at enqueue to a pull at expiry model
Hi,
the queue grew because of some additional cleanup patches and splitting already existing patches to make review easier. Several minor things had to be reworked. For all people who did testing, I would be really happy if you could test v9 as well!
The queue contains three parts:
- Patches 1 - 12: Cleanups and minor fixes
- Patches 13 - 16: timer base idle marking rework with two preparatory changes. See the section below for more details.
- Patches 17 - 32: Updated timer pull model on top of timer idle rework
The queue is available here:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel timers/pushpull
Move marking timer bases as idle into tick_nohz_stop_tick()
-----------------------------------------------------------
The idle marking of timer bases is done in get_next_timer_interrupt() whenever possible. The timer bases are idle, even if the tick will not be stopped. This lead to an IPI when a new first timer is enqueued remote. To prevent this, setting timer_base->in_idle flag is postponed to tick_nohz_stop_tick().
Furthermore this synchronizes the states of timer base is_idle and tick_stopped. With the timer pull model in place, also the idle state in the hierarchy of a CPU is synchronized with the other idle related states.
Timer pull model
----------------
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
~~~~~~~
Enqueue
^^^^^^^
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.
dbench test
^^^^^^^^^^^
A dbench test starting X pairs of client servers are used to create load on the system. The measurable value is the throughput. The tests were executed on a zen3 machine. The base is the tip tree branch timers/core which is based on a v6.6-rc1.
governor menu
NR timers/core pull-model impact
----------------------------------------------
1 353.19 (0.19) 353.45 (0.30) 0.07%
2 700.10 (0.96) 687.00 (0.20) -1.87%
4 1329.37 (0.63) 1282.91 (0.64) -3.49%
8 2561.16 (1.28) 2493.56 (1.76) -2.64%
16 4959.96 (0.80) 4914.59 (0.64) -0.91%
32 9741.92 (3.44) 8979.83 (1.13) -7.82%
64 16535.40 (2.84) 16388.47 (4.02) -0.89%
128 22136.83 (2.42) 23174.50 (1.43) 4.69%
256 39256.77 (4.48) 38994.00 (0.39) -0.67%
512 36799.03 (1.83) 38091.10 (0.63) 3.51%
1024 32903.03 (0.86) 35370.70 (0.89) 7.50%
governor teo
NR timers/core pull-model impact
----------------------------------------------
1 350.83 (1.27) 352.45 (0.96) 0.46%
2 699.52 (0.85) 690.10 (0.54) -1.35%
4 1339.53 (1.99) 1294.71 (2.71) -3.35%
8 2574.10 (0.76) 2495.46 (1.97) -3.06%
16 4898.50 (1.74) 4783.06 (1.64) -2.36%
32 9115.50 (4.63) 9037.83 (1.58) -0.85%
64 16663.90 (3.80) 16042.00 (1.72) -3.73%
128 25044.93 (1.11) 23250.03 (1.08) -7.17%
256 38059.53 (1.70) 39658.57 (2.98) 4.20%
512 36369.30 (0.39) 38890.13 (0.36) 6.93%
1024 33956.83 (1.14) 35514.83 (0.29) 4.59%
Ping Pong Oberservation
^^^^^^^^^^^^^^^^^^^^^^^
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.
Possible 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.
v8..v9: https://lore.kernel.org/r/[email protected]
- Address review feedback
- Add more minor cleanup fixes
- fixes inconsistent idle related states
v7..v8: https://lore.kernel.org/r/[email protected]
- Address review feedback
- Move marking timer base idle into tick_nohz_stop_tick()
- Look ahead function to determine possible sleep lenght
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/[email protected]/
- Minimize usage of locks by storing data using atomic_cmpxchg() for migrator information and information about active cpus.
Thanks,
Anna-Maria
Anna-Maria Behnsen (29):
tick-sched: Fix function names in comments
tick/sched: Cleanup confusing variables
tick-sched: Warn when next tick seems to be in the past tracing/timers: Enhance timer_start tracepoint tracing/timers: Add tracepoint for tracking timer base is_idle flag
timers: Do not IPI for deferrable timers
timers: Move store of next event into __next_timer_interrupt()
timers: Clarify check in forward_timer_base()
timers: Split out forward timer base functionality
timers: Use already existing function for forwarding timer base
timers: Fix nextevt calculation when no timers are pending
timers: Restructure get_next_timer_interrupt()
timers: Split out get next timer interrupt
timers: Move marking timer bases idle into tick_nohz_stop_tick()
timers: Optimization for timer_base_try_to_set_idle()
timers: Introduce add_timer() variants which modify timer flags
workqueue: Use global variant for add_timer()
timers: add_timer_on(): Make sure TIMER_PINNED flag is set
timers: Ease code in run_local_timers()
timers: Split next timer interrupt logic
timers: Keep the pinned timers separate from the others
timers: Retrieve next expiry of pinned/non-pinned timers separately
timers: Split out "get next timer interrupt" functionality
timers: Add get next timer interrupt functionality for remote CPUs
timers: Check if timers base is handled already
timers: Introduce function to check timer base is_idle flag
timers: Implement the hierarchical pull model
timer_migration: Add tracepoints
timers: Always queue timers on the local CPU
Richard Cochran (linutronix GmbH) (2):
timers: Restructure internal locking tick/sched: Split out jiffies update helper function
Thomas Gleixner (1):
timers: Rework idle logic
include/linux/cpuhotplug.h | 1 + include/linux/timer.h | 16 +- include/trace/events/timer.h | 40 +- include/trace/events/timer_migration.h | 297 +++++ kernel/time/Makefile | 3 + kernel/time/tick-internal.h | 14 + kernel/time/tick-sched.c | 89 +- kernel/time/timer.c | 562 ++++++-- kernel/time/timer_migration.c | 1662 ++++++++++++++++++++++++ kernel/time/timer_migration.h | 144 ++ kernel/workqueue.c | 2 +- 11 files changed, 2680 insertions(+), 150 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
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerPATCH v9 1/32 tick-sched: Fix function names in comments
When referencing functions in comments, it might be helpful to use full function names (including the prefix) to be able to find it when grepping.
---
v9: New cleanup patch
--- kernel/time/tick-sched.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/time/tick-sched.c +5 -5
@@ -920,11 +920,11 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu) }/* -* nohz_stop_sched_tick() can be called several times before -* nohz_restart_sched_tick() is called. This happens when -* interrupts arrive which do not cause a reschedule. In the -* first call we save the current tick time, so we can restart -* the scheduler tick in nohz_restart_sched_tick(). +* tick_nohz_stop_sched_tick() can be called several times before +* tick_nohz_restart_sched_tick() is called. This happens when +* interrupts arrive which do not cause a reschedule. In the first call +* we save the current tick time, so we can restart the scheduler tick +* in tick_nohz_restart_sched_tick(). */ if (!ts->tick_stopped) {calc_load_nohz_start ();
Signature · 1 line
2.39.2
Re: [PATCH v9 01/32] tick-sched: Fix function names in comments
Quoted 4 lines · Anna-Maria Behnsen
When referencing functions in comments, it might be helpful to use full function names (including the prefix) to be able to find it when grepping.
Signed-off-by Anna-Maria Behnsen <[email protected]>
Just a small detail that be addressed as a further patch (or edited while applying).
Quoted 11 lines
---
v9: New cleanup patch--- kernel/time/tick-sched.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)kernel/time/tick-sched.c +0
-0
kernel/time/tick-sched.c static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu) · line 923
Quoted 9 lines
kernel/time/tick-sched.c +1
-5@@ -920,11 +920,11 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu) }/* -* nohz_stop_sched_tick() can be called several times before -* nohz_restart_sched_tick() is called. This happens when -* interrupts arrive which do not cause a reschedule. In the -* first call we save the current tick time, so we can restart -* the scheduler tick in nohz_restart_sched_tick(). +* tick_nohz_stop_sched_tick() can be called several times before
s/tick_nohz_stop_sched_tick/tick_nohz_stop_tick
Because in the case of nohz_full it's indeed true. But idle behaves the same even though it uses another function (tick_nohz_idle_stop_tick()).
More generally tick_nohz_stop_tick() covers both idle and nohz_full case and behaves as described.
I guess at some point we should rename tick_nohz_stop_sched_tick() to tick_nohz_full_stop_tick() to avoid such confusion.
Thanks.
Quoted 10 lines
kernel/time/tick-sched.c +4
-0+* tick_nohz_restart_sched_tick() is called. This happens when +* interrupts arrive which do not cause a reschedule. In the first call +* we save the current tick time, so we can restart the scheduler tick +* in tick_nohz_restart_sched_tick(). */ if (!ts->tick_stopped) {calc_load_nohz_start ();Signature · 1 line
2.39.2
[tip: timers/core] tick-sched: Fix function names in comments
In reply to Anna-Maria BehnsenThe following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: cb665db94fc61512c9c94ed1d42af67e7bf6ce01 Gitweb: https://git.kernel.org/tip/cb665db94fc61512c9c94ed1d42af67e7bf6ce01 Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:23 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:37 +01:00
tick-sched: Fix function names in comments
When referencing functions in comments, it might be helpful to use full function names (including the prefix) to be able to find it when grepping.
--- kernel/time/tick-sched.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/time/tick-sched.c +5 -5
@@ -920,11 +920,11 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu) }/* -* nohz_stop_sched_tick() can be called several times before -* nohz_restart_sched_tick() is called. This happens when -* interrupts arrive which do not cause a reschedule. In the -* first call we save the current tick time, so we can restart -* the scheduler tick in nohz_restart_sched_tick(). +* tick_nohz_stop_tick() can be called several times before +* tick_nohz_restart_sched_tick() is called. This happens when +* interrupts arrive which do not cause a reschedule. In the first +* call we save the current tick time, so we can restart the +* scheduler tick in tick_nohz_restart_sched_tick(). */ if (!ts->tick_stopped) {calc_load_nohz_start ();
Anna-Maria Behnsen
maintainerPATCH v9 2/32 tick/sched: Cleanup confusing variables
In reply to Anna-Maria Behnsentick_nohz_stop_tick() contains the expires (u64 variable) and tick (ktime_t) variable. In the beginning the value of expires is written to tick. Afterwards none of the variables is changed. They are only used for checks.
Drop the not required variable tick and use always expires instead.
---
v9: Fix typo in commit message
--- kernel/time/tick-sched.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/time/tick-sched.c +5 -6
@@ -887,7 +887,6 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)struct clock_event_device *dev =__this_cpu_read (tick_cpu_device.evtdev);u64 basemono = ts->timer_expires_base;u64 expires = ts->timer_expires; -ktime_t tick = expires;/* Make sure we won't be trying to stop it twice in a row. */ ts->timer_expires_base =0 ;
@@ -910,7 +909,7 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)/* Skip reprogram of event if it's not changed */ if (ts->tick_stopped && (expires == ts->next_tick)) {/* Sanity check: make sure clockevent is actually programmed */ -if (tick ==KTIME_MAX || ts->next_tick ==hrtimer_get_expires (&ts->sched_timer)) +if (expires ==KTIME_MAX || ts->next_tick ==hrtimer_get_expires (&ts->sched_timer))return ;WARN_ON_ONCE (1 );
@@ -935,7 +934,7 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)trace_tick_stop (1 ,TICK_DEP_MASK_NONE ); } - ts->next_tick = tick; + ts->next_tick = expires;/* * If the expiration time == KTIME_MAX, then we simply stop
@@ -950,11 +949,11 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu) }if (ts->nohz_mode ==NOHZ_MODE_HIGHRES ) { -hrtimer_start (&ts->sched_timer, tick, +hrtimer_start (&ts->sched_timer, expires,HRTIMER_MODE_ABS_PINNED_HARD ); }else { -hrtimer_set_expires (&ts->sched_timer, tick); -tick_program_event (tick,1 ); +hrtimer_set_expires (&ts->sched_timer, expires); +tick_program_event (expires,1 ); } }
Signature · 1 line
2.39.2
[tip: timers/core] tick/sched: Cleanup confusing variables
The following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: 318050671affa92fd166d988d08d4041c7b113c4 Gitweb: https://git.kernel.org/tip/318050671affa92fd166d988d08d4041c7b113c4 Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:24 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:37 +01:00
tick/sched: Cleanup confusing variables
tick_nohz_stop_tick() contains the expires (u64 variable) and tick (ktime_t) variable. In the beginning the value of expires is written to tick. Afterwards none of the variables is changed. They are only used for checks.
Drop the not required variable tick and use always expires instead.
--- kernel/time/tick-sched.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/time/tick-sched.c +5 -6
@@ -887,7 +887,6 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)struct clock_event_device *dev =__this_cpu_read (tick_cpu_device.evtdev);u64 basemono = ts->timer_expires_base;u64 expires = ts->timer_expires; -ktime_t tick = expires;/* Make sure we won't be trying to stop it twice in a row. */ ts->timer_expires_base =0 ;
@@ -910,7 +909,7 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)/* Skip reprogram of event if it's not changed */ if (ts->tick_stopped && (expires == ts->next_tick)) {/* Sanity check: make sure clockevent is actually programmed */ -if (tick ==KTIME_MAX || ts->next_tick ==hrtimer_get_expires (&ts->sched_timer)) +if (expires ==KTIME_MAX || ts->next_tick ==hrtimer_get_expires (&ts->sched_timer))return ;WARN_ON_ONCE (1 );
@@ -935,7 +934,7 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)trace_tick_stop (1 ,TICK_DEP_MASK_NONE ); } - ts->next_tick = tick; + ts->next_tick = expires;/* * If the expiration time == KTIME_MAX, then we simply stop
@@ -950,11 +949,11 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu) }if (ts->nohz_mode ==NOHZ_MODE_HIGHRES ) { -hrtimer_start (&ts->sched_timer, tick, +hrtimer_start (&ts->sched_timer, expires,HRTIMER_MODE_ABS_PINNED_HARD ); }else { -hrtimer_set_expires (&ts->sched_timer, tick); -tick_program_event (tick,1 ); +hrtimer_set_expires (&ts->sched_timer, expires); +tick_program_event (expires,1 ); } }
Anna-Maria Behnsen
maintainerPATCH v9 3/32 tick-sched: Warn when next tick seems to be in the past
In reply to Anna-Maria BehnsenWhen 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. To prevent complete misbehaviour, also reset next_tick to basemono in this case.
---
v9: Add reset of next_tick to basemono
--- kernel/time/tick-sched.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/time/tick-sched.c +4 -0
@@ -839,6 +839,10 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) ts->next_timer = next_tick; } +/* Make sure next_tick is never before basemono! */ +if (WARN_ON_ONCE (basemono > next_tick)) + next_tick = basemono; +/* * If the tick is due in the next period, keep it ticking or * force prod the timer.
Signature · 1 line
2.39.2
Re: [PATCH v9 03/32] tick-sched: Warn when next tick seems to be in the past
Quoted 21 lines · Anna-Maria Behnsen
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_ONCEinto this code path. To prevent complete misbehaviour, also resetnext_tickto basemono in this case.Signed-off-by Anna-Maria Behnsen <[email protected]>---
v9: Add reset ofnext_tickto basemono--- kernel/time/tick-sched.c | 4 ++++ 1 file changed, 4 insertions(+)kernel/time/tick-sched.c +0
-0
kernel/time/tick-sched.c static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) · line 845
Quoted 7 lines
kernel/time/tick-sched.c +4
-0@@ -839,6 +839,10 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) ts->next_timer = next_tick; } +/* Make sure next_tick is never before basemono! */ +if (WARN_ON_ONCE (basemono > next_tick)) + next_tick = basemono; +
And some food for thoughts:
1) Is it possible for hrtimer_get_next_event() to return values in the past?
2) Is hrtimer_get_next_event() unconditionally locking &cpu_base->lock even high resolution is active? Can we avoid that?
Thanks.
Quoted 6 lines
kernel/time/tick-sched.c +0
-0/* * If the tick is due in the next period, keep it ticking or * force prod the timer. Signature · 1 line
2.39.2
[tip: timers/core] tick-sched: Warn when next tick seems to be in the past
In reply to Anna-Maria BehnsenThe following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: cbf04a22026100dceeceec67fcbf1973383eb32f Gitweb: https://git.kernel.org/tip/cbf04a22026100dceeceec67fcbf1973383eb32f Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:25 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:38 +01:00
tick-sched: Warn when next tick seems to be in the past
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. To prevent complete misbehaviour, also reset next_tick to basemono in this case.
--- kernel/time/tick-sched.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/time/tick-sched.c +4 -0
@@ -839,6 +839,10 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) ts->next_timer = next_tick; } +/* Make sure next_tick is never before basemono! */ +if (WARN_ON_ONCE (basemono > next_tick)) + next_tick = basemono; +/* * If the tick is due in the next period, keep it ticking or * force prod the timer.
Anna-Maria Behnsen
maintainerPATCH v9 4/32 tracing/timers: Enhance timer_start tracepoint
In reply to Anna-Maria BehnsenFor starting a timer, the timer is enqueued into a bucket of the timer wheel. The bucket expiry is the defacto expiry of the timer but it is not equal the timer expiry because of increasing granularity when bucket is in a higher level of the wheel. To be able to figure out in a trace whether a timer expired in time or not, the bucket expiry time is required as well.
Add bucket expiry time to the timer_start tracepoint and thereby simplify the arguments.
---
v9: New in v9
--- include/trace/events/timer.h | 20 ++++++++++---------- kernel/time/timer.c | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-)
include/trace/events/timer.h +10 -10
@@ -46,22 +46,21 @@ DEFINE_EVENT(timer_class, timer_init,/** * timer_start - called when the timer is started -* @timer: pointer to struct timer_list -* @expires: the timers expiry time -* @flags: the timers flags +* @timer: pointer to struct timer_list +* @bucket_expiry: the bucket expiry time */ TRACE_EVENT(timer_start, TP_PROTO(struct timer_list *timer, - unsigned long expires, - unsigned int flags), + unsigned long bucket_expiry), - TP_ARGS(timer, expires, flags), + TP_ARGS(timer, bucket_expiry), TP_STRUCT__entry( __field( void *, timer ) __field( void *, function ) __field( unsigned long, expires ) + __field( unsigned long, bucket_expiry ) __field( unsigned long, now ) __field( unsigned int, flags ) ),
@@ -69,15 +68,16 @@ TRACE_EVENT(timer_start,TP_fast_assign ( __entry->timer = timer; __entry->function = timer->function; - __entry->expires = expires; + __entry->expires = timer->expires; + __entry->bucket_expiry = bucket_expiry; __entry->now = jiffies; - __entry->flags = flags; + __entry->flags = timer->flags; ), -TP_printk ("timer=%p function=%ps expires=%lu [timeout=%ld] cpu=%u idx=%u flags=%s" , +TP_printk ("timer=%p function=%ps expires=%lu [timeout=%ld] bucket_expiry=%lu cpu=%u idx=%u flags=%s" , __entry->timer, __entry->function, __entry->expires, (long )__entry->expires - __entry->now, - __entry->flags &TIMER_CPUMASK , + __entry->bucket_expiry, __entry->flags &TIMER_CPUMASK , __entry->flags >>TIMER_ARRAYSHIFT ,decode_timer_flags (__entry->flags &TIMER_TRACE_FLAGMASK )) );
kernel/time/timer.c +1 -1
@@ -606,7 +606,7 @@ static void enqueue_timer(struct timer_base *base, struct timer_list *timer,__set_bit (idx, base->pending_map);timer_set_idx (timer, idx); -trace_timer_start (timer, timer->expires, timer->flags); +trace_timer_start (timer, bucket_expiry);/* * Check whether this is the new first expiring timer. The
Signature · 1 line
2.39.2
Re: [PATCH v9 04/32] tracing/timers: Enhance timer_start tracepoint
Quoted 10 lines · Anna-Maria Behnsen
For starting a timer, the timer is enqueued into a bucket of the timer wheel. The bucket expiry is the defacto expiry of the timer but it is not equal the timer expiry because of increasing granularity when bucket is in a higher level of the wheel. To be able to figure out in a trace whether a timer expired in time or not, the bucket expiry time is required as well.
Add bucket expiry time to the
timer_starttracepoint and thereby simplify the arguments.Signed-off-by Anna-Maria Behnsen <[email protected]>
[tip: timers/core] tracing/timers: Enhance timer_start tracepoint
In reply to Anna-Maria BehnsenThe following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: dbcdcb62b59db2cf6a24113873b90da15c6f0b19 Gitweb: https://git.kernel.org/tip/dbcdcb62b59db2cf6a24113873b90da15c6f0b19 Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:26 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:38 +01:00
tracing/timers: Enhance timer_start tracepoint
For starting a timer, the timer is enqueued into a bucket of the timer wheel. The bucket expiry is the defacto expiry of the timer but it is not equal the timer expiry because of increasing granularity when bucket is in a higher level of the wheel. To be able to figure out in a trace whether a timer expired in time or not, the bucket expiry time is required as well.
Add bucket expiry time to the timer_start tracepoint and thereby simplify the arguments.
--- include/trace/events/timer.h | 20 ++++++++++---------- kernel/time/timer.c | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-)
include/trace/events/timer.h +10 -10
@@ -46,22 +46,21 @@ DEFINE_EVENT(timer_class, timer_init,/** * timer_start - called when the timer is started -* @timer: pointer to struct timer_list -* @expires: the timers expiry time -* @flags: the timers flags +* @timer: pointer to struct timer_list +* @bucket_expiry: the bucket expiry time */ TRACE_EVENT(timer_start, TP_PROTO(struct timer_list *timer, - unsigned long expires, - unsigned int flags), + unsigned long bucket_expiry), - TP_ARGS(timer, expires, flags), + TP_ARGS(timer, bucket_expiry), TP_STRUCT__entry( __field( void *, timer ) __field( void *, function ) __field( unsigned long, expires ) + __field( unsigned long, bucket_expiry ) __field( unsigned long, now ) __field( unsigned int, flags ) ),
@@ -69,15 +68,16 @@ TRACE_EVENT(timer_start,TP_fast_assign ( __entry->timer = timer; __entry->function = timer->function; - __entry->expires = expires; + __entry->expires = timer->expires; + __entry->bucket_expiry = bucket_expiry; __entry->now = jiffies; - __entry->flags = flags; + __entry->flags = timer->flags; ), -TP_printk ("timer=%p function=%ps expires=%lu [timeout=%ld] cpu=%u idx=%u flags=%s" , +TP_printk ("timer=%p function=%ps expires=%lu [timeout=%ld] bucket_expiry=%lu cpu=%u idx=%u flags=%s" , __entry->timer, __entry->function, __entry->expires, (long )__entry->expires - __entry->now, - __entry->flags &TIMER_CPUMASK , + __entry->bucket_expiry, __entry->flags &TIMER_CPUMASK , __entry->flags >>TIMER_ARRAYSHIFT ,decode_timer_flags (__entry->flags &TIMER_TRACE_FLAGMASK )) );
kernel/time/timer.c +1 -1
@@ -606,7 +606,7 @@ static void enqueue_timer(struct timer_base *base, struct timer_list *timer,__set_bit (idx, base->pending_map);timer_set_idx (timer, idx); -trace_timer_start (timer, timer->expires, timer->flags); +trace_timer_start (timer, bucket_expiry);/* * Check whether this is the new first expiring timer. The
Anna-Maria Behnsen
maintainerPATCH v9 5/32 tracing/timers: Add tracepoint for tracking timer base is_idle flag
In reply to Anna-Maria BehnsenWhen debugging timer code the timer tracepoints are very important. There is no tracepoint when the is_idle flag of the timer base changes. Instead of always adding manually trace_printk(), add tracepoints which can be easily enabled whenever required.
---
v9: New in v9
--- include/trace/events/timer.h | 20 ++++++++++++++++++++ kernel/time/timer.c | 2 ++ 2 files changed, 22 insertions(+)
include/trace/events/timer.h +20 -0
@@ -142,6 +142,26 @@ DEFINE_EVENT(timer_class, timer_cancel,TP_ARGS (timer) ); +TRACE_EVENT (timer_base_idle, + + TP_PROTO(bool is_idle, unsigned int cpu), + +TP_ARGS (is_idle, cpu), + +TP_STRUCT__entry ( + __field( bool, is_idle ) + __field( unsigned int, cpu ) + ), + +TP_fast_assign ( + __entry->is_idle = is_idle; + __entry->cpu = cpu; + ), + +TP_printk ("is_idle=%d cpu=%d" , + __entry->is_idle, __entry->cpu) +); +#define decode_clockid (type) \ __print_symbolic(type, \ { CLOCK_REALTIME, "CLOCK_REALTIME" }, \
kernel/time/timer.c +2 -0
@@ -1964,6 +1964,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)if ((expires - basem) >TICK_NSEC ) base->is_idle = true; } +trace_timer_base_idle (base->is_idle, base->cpu);raw_spin_unlock (&base->lock);return cmp_next_hrtimer_event (basem, expires);
@@ -1985,6 +1986,7 @@ void timer_clear_idle(void)* the lock in the exit from idle path. */ base->is_idle = false; + trace_timer_base_idle(0, smp_processor_id()); }#endif
Signature · 1 line
2.39.2
Re: [PATCH v9 05/32] tracing/timers: Add tracepoint for tracking timer base is_idle flag
Quoted 6 lines · Anna-Maria Behnsen
When debugging timer code the timer tracepoints are very important. There is no tracepoint when the
is_idleflag of the timer base changes. Instead of always adding manuallytrace_printk(), add tracepoints which can be easily enabled whenever required.Signed-off-by Anna-Maria Behnsen <[email protected]>
Just a detail below, again this can be posted as a delta patch or edited before applying:
Quoted 43 lines
---
v9: New in v9--- include/trace/events/timer.h | 20 ++++++++++++++++++++ kernel/time/timer.c | 2 ++ 2 files changed, 22 insertions(+)include/trace/events/timer.h +20
-0@@ -142,6 +142,26 @@ DEFINE_EVENT(timer_class, timer_cancel,TP_ARGS (timer) ); +TRACE_EVENT (timer_base_idle, + + TP_PROTO(bool is_idle, unsigned int cpu), + +TP_ARGS (is_idle, cpu), + +TP_STRUCT__entry ( + __field( bool, is_idle ) + __field( unsigned int, cpu ) + ), + +TP_fast_assign ( + __entry->is_idle = is_idle; + __entry->cpu = cpu; + ), + +TP_printk ("is_idle=%d cpu=%d" , + __entry->is_idle, __entry->cpu) +); +#define decode_clockid (type) \ __print_symbolic(type, \ { CLOCK_REALTIME, "CLOCK_REALTIME" }, \kernel/time/timer.c +0
-0
kernel/time/timer.c u64 get_next_timer_interrupt(unsigned long basej, u64 basem) · line 1967
Quoted 4 lines
kernel/time/timer.c +1
-0@@ -1964,6 +1964,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)if ((expires - basem) >TICK_NSEC ) base->is_idle = true; } +trace_timer_base_idle (base->is_idle, base->cpu);
This will trigger a trace everytime we loop into idle or remotely compute the next timer. Can we move that to when base->is_idle is set from false to true only?
kernel/time/timer.c void timer_clear_idle(void) · line 1989
Quoted 8 lines
kernel/time/timer.c +1
-0raw_spin_unlock (&base->lock);return cmp_next_hrtimer_event (basem, expires);@@ -1985,6 +1986,7 @@ void timer_clear_idle(void)* the lock in the exit from idle path. */ base->is_idle = false; +trace_timer_base_idle (0 ,smp_processor_id ());
Same here. If base->is_idle was already false, you could spare a noisy trace.
Thanks.
Quoted 6 lines
kernel/time/timer.c +0
-0}#endif Signature · 1 line
2.39.2
[tip: timers/core] tracing/timers: Add tracepoint for tracking timer base is_idle flag
In reply to Anna-Maria BehnsenThe following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: b573c73101d8786446535b2ab28cbc8907bda9a9 Gitweb: https://git.kernel.org/tip/b573c73101d8786446535b2ab28cbc8907bda9a9 Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:27 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:38 +01:00
tracing/timers: Add tracepoint for tracking timer base is_idle flag
When debugging timer code the timer tracepoints are very important. There is no tracepoint when the is_idle flag of the timer base changes. Instead of always adding manually trace_printk(), add tracepoints which can be easily enabled whenever required.
--- include/trace/events/timer.h | 20 ++++++++++++++++++++ kernel/time/timer.c | 14 +++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-)
include/trace/events/timer.h +20 -0
@@ -142,6 +142,26 @@ DEFINE_EVENT(timer_class, timer_cancel,TP_ARGS (timer) ); +TRACE_EVENT (timer_base_idle, + + TP_PROTO(bool is_idle, unsigned int cpu), + +TP_ARGS (is_idle, cpu), + +TP_STRUCT__entry ( + __field( bool, is_idle ) + __field( unsigned int, cpu ) + ), + +TP_fast_assign ( + __entry->is_idle = is_idle; + __entry->cpu = cpu; + ), + +TP_printk ("is_idle=%d cpu=%d" , + __entry->is_idle, __entry->cpu) +); +#define decode_clockid (type) \ __print_symbolic(type, \ { CLOCK_REALTIME, "CLOCK_REALTIME" }, \
kernel/time/timer.c +11 -3
@@ -1950,7 +1950,10 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)if (time_before_eq (nextevt, basej)) { expires = basem; - base->is_idle = false; +if (base->is_idle) { + base->is_idle = false; +trace_timer_base_idle (false, base->cpu); + } }else {if (base->timers_pending) expires = basem + (u64)(nextevt - basej) *TICK_NSEC ;
@@ -1961,8 +1964,10 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)* logic is only maintained for the BASE_STD base, deferrable * timers may still see large granularity skew (by design). */ -if ((expires - basem) >TICK_NSEC ) +if ((expires - basem) >TICK_NSEC && !base->is_idle) { base->is_idle = true; +trace_timer_base_idle (true, base->cpu); + } }raw_spin_unlock (&base->lock);
@@ -1984,7 +1989,10 @@ void timer_clear_idle(void)* sending the IPI a few instructions smaller for the cost of taking * the lock in the exit from idle path. */ - base->is_idle = false; +if (base->is_idle) { + base->is_idle = false; +trace_timer_base_idle (false,smp_processor_id ()); + } }#endif
Anna-Maria Behnsen
maintainerPATCH v9 6/32 timers: Do not IPI for deferrable timers
In reply to Anna-Maria BehnsenDeferrable 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 but nothing will be done regarding the deferrable timers.
Drop IPI completely when a new first deferrable timer was enqueued.
---
v9: Only a typo fix
v8: Update comment
v6: new patch
--- kernel/time/timer.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
kernel/time/timer.c +6 -9
@@ -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 the CPU from entering dynticks and +* are not taken into account on the 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 +* the 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
Signature · 1 line
2.39.2
[tip: timers/core] timers: Do not IPI for deferrable timers
The following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: d124c3393e798b1fb142ee728d5c8976d11e722d Gitweb: https://git.kernel.org/tip/d124c3393e798b1fb142ee728d5c8976d11e722d Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:28 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:38 +01:00
timers: Do not IPI for deferrable timers
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 but nothing will be done regarding the deferrable timers.
Drop IPI completely when a new first deferrable timer was enqueued.
--- kernel/time/timer.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
kernel/time/timer.c +6 -9
@@ -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 the CPU from entering dynticks and +* are not taken into account on the 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 +* the 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
Anna-Maria Behnsen
maintainerPATCH v9 7/32 timers: Move store of next event into __next_timer_interrupt()
In reply to Anna-Maria BehnsenBoth call sites of __next_timer_interrupt() store the return value directly in base->next_expiry. Move the store into __next_timer_interrupt() and to make its purpose more clear, rename the function to next_expiry_recalc().
No functional change.
---
v9: Typo fix only
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(-)
kernel/time/timer.c +6 -5
@@ -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
@@ -1930,7 +1931,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;/*
@@ -2015,7 +2016,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);
Signature · 1 line
2.39.2
[tip: timers/core] timers: Move store of next event into __next_timer_interrupt()
The following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: b5e6f59888c7bde3c05f61b3ce06b78a86713fc0 Gitweb: https://git.kernel.org/tip/b5e6f59888c7bde3c05f61b3ce06b78a86713fc0 Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:29 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:38 +01:00
timers: Move store of next event into __next_timer_interrupt()
Both call sites of __next_timer_interrupt() store the return value directly in base->next_expiry. Move the store into __next_timer_interrupt() and to make its purpose more clear, rename the function to next_expiry_recalc().
No functional change.
--- kernel/time/timer.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
kernel/time/timer.c +6 -5
@@ -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
@@ -1930,7 +1931,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;/*
@@ -2021,7 +2022,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);
Anna-Maria Behnsen
maintainerPATCH v9 8/32 timers: Clarify check in forward_timer_base()
In reply to Anna-Maria BehnsenThe current check whether a forward of the timer base is required can be simplified by using an already existing comparison function which is easier to read. The related comment is outdated and was not updated when the check changed in commit 36cd28a4cdd0 ("timers: Lower base clock forwarding threshold").
Use time_before_eq() for the check and replace the comment by copying the comment from the same check inside get_next_timer_interrupt(). Move the precious information of the outdated comment to the proper place in __run_timers().
No functional change.
---
v9: Move precious information of outdated comment to proper place (as
suggested by Frederic)
--- kernel/time/timer.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
kernel/time/timer.c +7 -4
@@ -944,11 +944,10 @@ static inline void forward_timer_base(struct timer_base *base)unsigned long jnow =READ_ONCE (jiffies);/* -* No need to forward if we are close enough below jiffies. -* Also while executing timers, base->clk is 1 offset ahead -* of jiffies to avoid endless requeuing to current jiffies. +* Check whether we can forward the base. We can only do that when +* @basej is past base->clk otherwise we might rewind base->clk. */ -if ((long )(jnow - base->clk) <1 ) +if (time_before_eq (jnow, base->clk))return ;/*
@@ -2015,6 +2014,10 @@ static inline void __run_timers(struct timer_base *base)*/ WARN_ON_ONCE (!levels && !base->next_expiry_recalc && base->timers_pending); +/* +* While executing timers, base->clk is set 1 offset ahead of +* jiffies to avoid endless requeuing to current jiffies. +*/ base->clk++;next_expiry_recalc (base);
Signature · 1 line
2.39.2
[tip: timers/core] timers: Clarify check in forward_timer_base()
The following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: 8a2c9c7e7848d7f63d38b698209148b5bb4ba7f3 Gitweb: https://git.kernel.org/tip/8a2c9c7e7848d7f63d38b698209148b5bb4ba7f3 Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:30 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:38 +01:00
timers: Clarify check in forward_timer_base()
The current check whether a forward of the timer base is required can be simplified by using an already existing comparison function which is easier to read. The related comment is outdated and was not updated when the check changed in commit 36cd28a4cdd0 ("timers: Lower base clock forwarding threshold").
Use time_before_eq() for the check and replace the comment by copying the comment from the same check inside get_next_timer_interrupt(). Move the precious information of the outdated comment to the proper place in __run_timers().
No functional change.
--- kernel/time/timer.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
kernel/time/timer.c +7 -4
@@ -944,11 +944,10 @@ static inline void forward_timer_base(struct timer_base *base)unsigned long jnow =READ_ONCE (jiffies);/* -* No need to forward if we are close enough below jiffies. -* Also while executing timers, base->clk is 1 offset ahead -* of jiffies to avoid endless requeuing to current jiffies. +* Check whether we can forward the base. We can only do that when +* @basej is past base->clk otherwise we might rewind base->clk. */ -if ((long )(jnow - base->clk) <1 ) +if (time_before_eq (jnow, base->clk))return ;/*
@@ -2021,6 +2020,10 @@ static inline void __run_timers(struct timer_base *base)*/ WARN_ON_ONCE (!levels && !base->next_expiry_recalc && base->timers_pending); +/* +* While executing timers, base->clk is set 1 offset ahead of +* jiffies to avoid endless requeuing to current jiffies. +*/ base->clk++;next_expiry_recalc (base);
Anna-Maria Behnsen
maintainerPATCH v9 9/32 timers: Split out forward timer base functionality
In reply to Anna-Maria BehnsenForwarding timer base is done when the next expiry value is calculated and when a new timer is enqueued. When the next expiry value is calculated the jiffies value is already available and does not need to be reread a second time.
Splitting out the forward timer base functionality to make it executable via both contextes - those where jiffies are already known and those, where jiffies need to be read.
No functional change.
--- kernel/time/timer.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
kernel/time/timer.c +10 -6
@@ -939,30 +939,34 @@ get_target_base(struct timer_base *base, unsigned tflags)return get_timer_this_cpu_base (tflags); } -static inline void forward_timer_base (struct timer_base *base) +static inline void __forward_timer_base (struct timer_base *base, +unsigned long basej) { -unsigned long jnow =READ_ONCE (jiffies); -/* * Check whether we can forward the base. We can only do that when * @basej is past base->clk otherwise we might rewind base->clk. */ -if (time_before_eq (jnow, base->clk)) +if (time_before_eq (basej, base->clk))return ;/* * If the next expiry value is > jiffies, then we fast forward to * jiffies otherwise we forward to the next expiry value. */ -if (time_after (base->next_expiry, jnow)) { - base->clk = jnow; +if (time_after (base->next_expiry, basej)) { + base->clk = basej; }else {if (WARN_ON_ONCE (time_before (base->next_expiry, base->clk)))return ; base->clk = base->next_expiry; } + } +static inline void forward_timer_base (struct timer_base *base) +{ +__forward_timer_base (base,READ_ONCE (jiffies)); +}/* * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
Signature · 1 line
2.39.2
[tip: timers/core] timers: Split out forward timer base functionality
The following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: 1e490484aa3af42d4eeffabf96d6a02be69d586b Gitweb: https://git.kernel.org/tip/1e490484aa3af42d4eeffabf96d6a02be69d586b Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:31 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:38 +01:00
timers: Split out forward timer base functionality
Forwarding timer base is done when the next expiry value is calculated and when a new timer is enqueued. When the next expiry value is calculated the jiffies value is already available and does not need to be reread a second time.
Splitting out the forward timer base functionality to make it executable via both contextes - those where jiffies are already known and those, where jiffies need to be read.
No functional change.
--- kernel/time/timer.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
kernel/time/timer.c +10 -6
@@ -939,30 +939,34 @@ get_target_base(struct timer_base *base, unsigned tflags)return get_timer_this_cpu_base (tflags); } -static inline void forward_timer_base (struct timer_base *base) +static inline void __forward_timer_base (struct timer_base *base, +unsigned long basej) { -unsigned long jnow =READ_ONCE (jiffies); -/* * Check whether we can forward the base. We can only do that when * @basej is past base->clk otherwise we might rewind base->clk. */ -if (time_before_eq (jnow, base->clk)) +if (time_before_eq (basej, base->clk))return ;/* * If the next expiry value is > jiffies, then we fast forward to * jiffies otherwise we forward to the next expiry value. */ -if (time_after (base->next_expiry, jnow)) { - base->clk = jnow; +if (time_after (base->next_expiry, basej)) { + base->clk = basej; }else {if (WARN_ON_ONCE (time_before (base->next_expiry, base->clk)))return ; base->clk = base->next_expiry; } + } +static inline void forward_timer_base (struct timer_base *base) +{ +__forward_timer_base (base,READ_ONCE (jiffies)); +}/* * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
Anna-Maria Behnsen
maintainerPATCH v9 10/32 timers: Use already existing function for forwarding timer base
In reply to Anna-Maria BehnsenThere is an already existing function for forwarding the timer base. Forwarding the timer base is implemented directly in get_next_timer_interrupt() as well.
Remove the code duplication and invoke __forward_timer_base() instead.
--- kernel/time/timer.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
kernel/time/timer.c +2 -8
@@ -1939,15 +1939,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_timer_base (base, basej);if (time_before_eq (nextevt, basej)) { expires = basem;
Signature · 1 line
2.39.2
[tip: timers/core] timers: Use already existing function for forwarding timer base
The following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: 7a39a5080ef0e3cf233d92165f6a778f08a08244 Gitweb: https://git.kernel.org/tip/7a39a5080ef0e3cf233d92165f6a778f08a08244 Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:32 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:38 +01:00
timers: Use already existing function for forwarding timer base
There is an already existing function for forwarding the timer base. Forwarding the timer base is implemented directly in get_next_timer_interrupt() as well.
Remove the code duplication and invoke __forward_timer_base() instead.
--- kernel/time/timer.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
kernel/time/timer.c +2 -8
@@ -1939,15 +1939,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_timer_base (base, basej);if (time_before_eq (nextevt, basej)) { expires = basem;
Anna-Maria Behnsen
maintainerPATCH v9 11/32 timers: Rework idle logic
In reply to Anna-Maria BehnsenFrom: Thomas Gleixner <[email protected]>
To improve readability of the code, split base->idle calculation and expires calculation into separate parts. While at it, update the comment about timer base idle marking.
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.
---
v9: Re-ordering to not hurt the eyes and update comment
v4: Change condition to force 0 delta and update commit message (Frederic)
--- kernel/time/timer.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
kernel/time/timer.c +16 -15
@@ -1943,22 +1943,23 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)*/ __forward_timer_base (base, basej); -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; +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 ; } + +/* +* Base is idle if the next event is more than a tick away. +* +* If the base is marked idle then any timer add operation must forward +* the base clk itself to keep granularity small. This idle logic is +* only maintained for the BASE_STD base, deferrable timers may still +* see large granularity skew (by design). +*/ + base->is_idle =time_after (nextevt, basej +1 ); +trace_timer_base_idle (base->is_idle, base->cpu);raw_spin_unlock (&base->lock);
Signature · 1 line
2.39.2
Frederic Weisbecker
maintainerRe: [PATCH v9 11/32] timers: Rework idle logic
Quoted 52 lines · Anna-Maria Behnsen
From: Thomas Gleixner <[email protected]>
To improve readability of the code, split
base->idlecalculation and expires calculation into separate parts. While at it, update the comment about timer base idle marking.Thereby the following subtle change happens if the next event is just one jiffy ahead and the tick was already stopped: Originally
base->is_idleremains true in this situation. Nowbase->is_idleturns 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 <[email protected]>Signed-off-by Anna-Maria Behnsen <[email protected]>Reviewed-by Frederic Weisbecker <[email protected]>---
v9: Re-ordering to not hurt the eyes and update comment
v4: Change condition to force 0 delta and update commit message (Frederic)--- kernel/time/timer.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)kernel/time/timer.c +6
-15@@ -1943,22 +1943,23 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)*/ __forward_timer_base (base, basej); -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; +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 ; } +
kernel/time/timer.c u64 get_next_timer_interrupt(unsigned long basej, u64 basem) · line 1962
Quoted 10 lines
kernel/time/timer.c +10
-0+/* +* Base is idle if the next event is more than a tick away. +* +* If the base is marked idle then any timer add operation must forward +* the base clk itself to keep granularity small. This idle logic is +* only maintained for the BASE_STD base, deferrable timers may still +* see large granularity skew (by design). +*/ + base->is_idle =time_after (nextevt, basej +1 ); +
Much better, thanks! :-)
[tip: timers/core] timers: Rework idle logic
In reply to Anna-Maria BehnsenThe following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: bb8caad5083f8fbba70faf41f1d3bab7cf09da6d Gitweb: https://git.kernel.org/tip/bb8caad5083f8fbba70faf41f1d3bab7cf09da6d Author: Thomas Gleixner <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:33 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:39 +01:00
timers: Rework idle logic
To improve readability of the code, split base->idle calculation and expires calculation into separate parts. While at it, update the comment about timer base idle marking.
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.
--- kernel/time/timer.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
kernel/time/timer.c +20 -20
@@ -1924,6 +1924,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)struct timer_base *base =this_cpu_ptr (&timer_bases[BASE_STD ]);u64 expires =KTIME_MAX ;unsigned long nextevt; +bool was_idle;/* * Pretend that there is no timer pending if the cpu is offline.
@@ -1943,27 +1944,26 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)*/ __forward_timer_base (base, basej); -if (time_before_eq (nextevt, basej)) { - expires = basem; -if (base->is_idle) { - base->is_idle = false; -trace_timer_base_idle (false, base->cpu); - } - }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) { - base->is_idle = true; -trace_timer_base_idle (true, base->cpu); - } +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 ; } + +/* +* Base is idle if the next event is more than a tick away. +* +* If the base is marked idle then any timer add operation must forward +* the base clk itself to keep granularity small. This idle logic is +* only maintained for the BASE_STD base, deferrable timers may still +* see large granularity skew (by design). +*/ + was_idle = base->is_idle; + base->is_idle =time_after (nextevt, basej +1 ); +if (was_idle != base->is_idle) +trace_timer_base_idle (base->is_idle, base->cpu); +raw_spin_unlock (&base->lock);return cmp_next_hrtimer_event (basem, expires);
Anna-Maria Behnsen
maintainerPATCH v9 12/32 timers: Fix nextevt calculation when no timers are pending
In reply to Anna-Maria BehnsenWhen no timer is queued into an empty timer base, the next_expiry will not be updated. It was originally calculated as
base->clk + NEXT_TIMER_MAX_DELTA
When the timer base stays empty long enough (> NEXT_TIMER_MAX_DELTA), the next_expiry value of the empty base suggests that there is a timer pending soon. This might be more a kind of a theoretical problem, but the fix doesn't hurt.
Use only base->next_expiry value as nextevt when timers are pending. Otherwise nextevt will be jiffies + NEXT_TIMER_MAX_DELTA. As all information is in place, update base->next_expiry value of the empty timer base as well.
---
v9: New patch
--- kernel/time/timer.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
kernel/time/timer.c +11 -2
@@ -1922,8 +1922,8 @@ static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)u64 get_next_timer_interrupt (unsigned long basej,u64 basem) {struct timer_base *base =this_cpu_ptr (&timer_bases[BASE_STD ]); +unsigned long nextevt = basej +NEXT_TIMER_MAX_DELTA ;u64 expires =KTIME_MAX ; -unsigned long nextevt;/* * Pretend that there is no timer pending if the cpu is offline.
@@ -1935,7 +1935,6 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)raw_spin_lock (&base->lock);if (base->next_expiry_recalc)next_expiry_recalc (base); - nextevt = base->next_expiry;/* * We have a fresh next event. Check whether we can forward the
@@ -1944,10 +1943,20 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)__forward_timer_base (base, basej);if (base->timers_pending) { + nextevt = base->next_expiry; +/* If we missed a tick already, force 0 delta */ if (time_before (nextevt, basej)) nextevt = basej; expires = basem + (u64)(nextevt - basej) *TICK_NSEC ; + }else { +/* +* Move next_expiry for the empty base into the future to +* prevent a unnecessary raise of the timer softirq when the +* next_expiry value will be reached even if there is no timer +* pending. +*/ + base->next_expiry = nextevt; }/*
Signature · 1 line
2.39.2
Re: [PATCH v9 12/32] timers: Fix nextevt calculation when no timers are pending
Quoted 9 lines · Anna-Maria Behnsen
When no timer is queued into an empty timer base, the
next_expirywill not be updated. It was originally calculated as
base->clk+NEXT_TIMER_MAX_DELTAWhen the timer base stays empty long enough (>
NEXT_TIMER_MAX_DELTA), thenext_expiryvalue of the empty base suggests that there is a timer pending soon. This might be more a kind of a theoretical problem, but the fix doesn't hurt.
So __run_timers() sets base::next_expiry to base->clk + NEXT_TIMER_MAX_DELTA and then we have no more timers enqueued.
But wouldn't base->timers_pending remain false? Therefore it would use "expires = KTIME_MAX" as return value (well cmp_next_hrtimer_event())?
Based on the code as of #11, it would only set timer_base::is_idle wrongly false if it wraps around. Other than that, I don't see an issue. What do I miss?
If you update it regardless here then it would make a difference to run_local_timers() assuming we have still hrtimer which expire and this next_expiry check might raise a softirq since it does not consider the timers_pending value.
Quoted 4 lines
Use only
base->next_expiryvalue as nextevt when timers are pending. Otherwise nextevt will be jiffies +NEXT_TIMER_MAX_DELTA. As all information is in place, updatebase->next_expiryvalue of the empty timer base as well.
or consider timers_pending in run_local_timers()? An additional read vs write?
Quoted 6 lines
kernel/time/timer.c +0
-0@@ -1944,10 +1943,20 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)__forward_timer_base (base, basej);if (base->timers_pending) {
kernel/time/timer.c u64 get_next_timer_interrupt(unsigned long basej, u64 basem) · line 1955
Quoted 10 lines
kernel/time/timer.c +6
-0+ nextevt = base->next_expiry; +/* If we missed a tick already, force 0 delta */ if (time_before (nextevt, basej)) nextevt = basej; expires = basem + (u64)(nextevt - basej) *TICK_NSEC ; + }else { +/* +* Move next_expiry for the empty base into the future to +* prevent a unnecessary raise of the timer softirq when the
an
kernel/time/timer.c u64 get_next_timer_interrupt(unsigned long basej, u64 basem) · line 1962
Quoted 7 lines
kernel/time/timer.c +4
-0+* next_expiry value will be reached even if there is no timer +* pending. +*/ + base->next_expiry = nextevt; }/*
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 12/32] timers: Fix nextevt calculation when no timers are pending
Quoted 11 lines · Sebastian Siewior
Quoted 9 lines · Anna-Maria Behnsen
When no timer is queued into an empty timer base, the
next_expirywill not be updated. It was originally calculated as
base->clk+NEXT_TIMER_MAX_DELTAWhen the timer base stays empty long enough (>
NEXT_TIMER_MAX_DELTA), thenext_expiryvalue of the empty base suggests that there is a timer pending soon. This might be more a kind of a theoretical problem, but the fix doesn't hurt.
Quoted 5 lines
So
__run_timers()setsbase::next_expirytobase->clk+NEXT_TIMER_MAX_DELTAand then we have no more timers enqueued.But wouldn't
base->timers_pendingremain false? Therefore it would use "expires =KTIME_MAX" as return value (wellcmp_next_hrtimer_event())?
Jupp.
Quoted 3 lines
Based on the code as of #11, it would only set
timer_base::is_idlewrongly false if it wraps around. Other than that, I don't see an issue. What do I miss?
And it will raise an unnecessary softirq when it wraps around as you also mentioned on the next paragraph.
Quoted 4 lines
If you update it regardless here then it would make a difference to
run_local_timers()assuming we have still hrtimer which expire and thisnext_expirycheck might raise a softirq since it does not consider thetimers_pendingvalue.
The only difference with this change would be that the softirq will not be raised when it wraps around.
Quoted 7 lines
Quoted 4 lines
Use only
base->next_expiryvalue as nextevt when timers are pending. Otherwise nextevt will be jiffies +NEXT_TIMER_MAX_DELTA. As all information is in place, updatebase->next_expiryvalue of the empty timer base as well.or consider
timers_pendinginrun_local_timers()? An additional read vs write?
This would also be a possibility to add the check in run_local_timers() with timers_pending. And we also have to make the is_idle marking in get_next_timer_interrupt() dependant on base::timers_pending bit. But this also means, we cannot rely on next_expiry when no timer is pending.
Frederic, what do you think?
Thanks,
Anna-Maria
Frederic Weisbecker
maintainerRe: [PATCH v9 12/32] timers: Fix nextevt calculation when no timers are pending
Quoted 11 lines · Anna-Maria Behnsen
Quoted 7 lines · Sebastian Siewior
Quoted 4 lines
Use only
base->next_expiryvalue as nextevt when timers are pending. Otherwise nextevt will be jiffies +NEXT_TIMER_MAX_DELTA. As all information is in place, updatebase->next_expiryvalue of the empty timer base as well.or consider
timers_pendinginrun_local_timers()? An additional read vs write?This would also be a possibility to add the check in
run_local_timers()withtimers_pending.
We could but do we really care about avoiding a potential softirq every 12 days (on 1000 Hz...)
Quoted 2 lines
And we also have to make the
is_idlemarking inget_next_timer_interrupt()dependant onbase::timers_pendingbit.
Yes that, on the other hand, looks mandatory! Because if the CPU sleeps for 12 days and then gets an interrupt and then go back to sleep, base->is_idle will be set as false and remote enqueues won't be notified.
Quoted 1 line
But this also means, we cannot rely on
next_expirywhen no timer is pending.
But note that this patch only fixes that partially anyway. Suppose the tick is stopped entirely and the CPU sleeps for 13 days without any interruption. Then it's woken up with TIF_RESCHED without any timer queued, get_next_timer_interrupt() won't be called upon tick restart to fix ->next_expiry.
Quoted 2 lines
Frederic, what do you think?
So it looks like is_idle must be fixed.
As for the timer softirq, ->next_expiry is already unreliable because when a timer is removed, ->next_expiry is not updated (even though that removed timer might have been the earliest). So ->next_expiry can already carry a "too early" value. The only constraint is that ->next_expiry can't be later than the first timer.
So I'd rather put a comment somewhere about the fact that wrapping is expected to behave ok. But it's your call.
Thanks.
Anna-Maria Behnsen
maintainerRe: [PATCH v9 12/32] timers: Fix nextevt calculation when no timers are pending
Quoted 6 lines · Frederic Weisbecker
Quoted 2 lines · Anna-Maria Behnsen
Frederic, what do you think?
So it looks like
is_idlemust be fixed.
Quoted 8 lines
As for the timer softirq, ->next_expiry is already unreliable because when a timer is removed, ->next_expiry is not updated (even though that removed timer might have been the earliest). So ->next_expiry can already carry a "too early" value. The only constraint is that ->next_expiry can't be later than the first timer.
So I'd rather put a comment somewhere about the fact that wrapping is expected to behave ok. But it's your call.
Ok. If both solutions are fine, I would like to take the solution with updating the next_expiry values for empty bases. It will make the compare of expiry values of global and local timer base easier in one of the patches later on.
Thanks,
Anna-Maria
Frederic Weisbecker
maintainerRe: [PATCH v9 12/32] timers: Fix nextevt calculation when no timers are pending
Quoted 14 lines · Anna-Maria Behnsen
Quoted 12 lines · Frederic Weisbecker
Quoted 2 lines · Anna-Maria Behnsen
Frederic, what do you think?
So it looks like
is_idlemust be fixed.As for the timer softirq, ->next_expiry is already unreliable because when a timer is removed, ->next_expiry is not updated (even though that removed timer might have been the earliest). So ->next_expiry can already carry a "too early" value. The only constraint is that ->next_expiry can't be later than the first timer.
Quoted 7 lines
Quoted 2 lines
So I'd rather put a comment somewhere about the fact that wrapping is expected to behave ok. But it's your call.
Ok. If both solutions are fine, I would like to take the solution with updating the
next_expiryvalues for empty bases. It will make the compare of expiry values of global and local timer base easier in one of the patches later on.
Fine by me at least!
Thanks.
Quoted 4 lines
Thanks,
Anna-Maria
Re: [PATCH v9 12/32] timers: Fix nextevt calculation when no timers are pending
In reply to Anna-Maria BehnsenQuoted 9 lines · Anna-Maria Behnsen
When no timer is queued into an empty timer base, the
next_expirywill not be updated. It was originally calculated as
base->clk+NEXT_TIMER_MAX_DELTAWhen the timer base stays empty long enough (>
NEXT_TIMER_MAX_DELTA), thenext_expiryvalue of the empty base suggests that there is a timer pending soon. This might be more a kind of a theoretical problem, but the fix doesn't hurt.
This solves a real issue. I suggest removing the last sentence and add instead:
If the CPU sleeps in idle for a bit more than NEXT_TIMER_MAX_DELTA (~12 days in HZ=1000) and then an interrupt fires, upon going back to idle get_next_timer_interrupt() will still return KTIME_MAX but incorrectly set is_idle to false. Therefore the CPU will keep the tick stopped and go back to sleep though further remote enqueue of timers to this CPU will fail to send an IPI. As a result the timer will remain ignored.
[tip: timers/core] timers: Fix nextevt calculation when no timers are pending
In reply to Anna-Maria BehnsenThe following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: da65f29dada7f7cbbf0d6375b88a0316f5f7d6f5 Gitweb: https://git.kernel.org/tip/da65f29dada7f7cbbf0d6375b88a0316f5f7d6f5 Author: Anna-Maria Behnsen <[email protected]> AuthorDate: Fri, 01 Dec 2023 10:26:34 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 20 Dec 2023 16:49:39 +01:00
timers: Fix nextevt calculation when no timers are pending
When no timer is queued into an empty timer base, the next_expiry will not be updated. It was originally calculated as
base->clk + NEXT_TIMER_MAX_DELTA
When the timer base stays empty long enough (> NEXT_TIMER_MAX_DELTA), the next_expiry value of the empty base suggests that there is a timer pending soon. This might be more a kind of a theoretical problem, but the fix doesn't hurt.
Use only base->next_expiry value as nextevt when timers are pending. Otherwise nextevt will be jiffies + NEXT_TIMER_MAX_DELTA. As all information is in place, update base->next_expiry value of the empty timer base as well.
--- kernel/time/timer.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
kernel/time/timer.c +11 -2
@@ -1922,8 +1922,8 @@ static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)u64 get_next_timer_interrupt (unsigned long basej,u64 basem) {struct timer_base *base =this_cpu_ptr (&timer_bases[BASE_STD ]); +unsigned long nextevt = basej +NEXT_TIMER_MAX_DELTA ;u64 expires =KTIME_MAX ; -unsigned long nextevt;bool was_idle;/*
@@ -1936,7 +1936,6 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)raw_spin_lock (&base->lock);if (base->next_expiry_recalc)next_expiry_recalc (base); - nextevt = base->next_expiry;/* * We have a fresh next event. Check whether we can forward the
@@ -1945,10 +1944,20 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)__forward_timer_base (base, basej);if (base->timers_pending) { + nextevt = base->next_expiry; +/* If we missed a tick already, force 0 delta */ if (time_before (nextevt, basej)) nextevt = basej; expires = basem + (u64)(nextevt - basej) *TICK_NSEC ; + }else { +/* +* Move next_expiry for the empty base into the future to +* prevent a unnecessary raise of the timer softirq when the +* next_expiry value will be reached even if there is no timer +* pending. +*/ + base->next_expiry = nextevt; }/*
Anna-Maria Behnsen
maintainerPATCH v9 13/32 timers: Restructure get_next_timer_interrupt()
In reply to Anna-Maria Behnsenget_next_timer_interrupt() contains two parts for the next timer interrupt calculation. Those two parts are separated by forwarding the base clock. But the second part does not depend on the forwarded base clock.
Therefore restructure get_next_timer_interrupt() to keep things together which belong together.
No functional change.
---
v9: New patch to eases patch "timers: Split out get next timer functionality"
--- kernel/time/timer.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/time/timer.c +6 -6
@@ -1936,12 +1936,6 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)if (base->next_expiry_recalc)next_expiry_recalc (base); -/* -* We have a fresh next event. Check whether we can forward the -* base. -*/ -__forward_timer_base (base, basej); -if (base->timers_pending) { nextevt = base->next_expiry;
@@ -1959,6 +1953,12 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) base->next_expiry = nextevt; } +/* +* We have a fresh next event. Check whether we can forward the +* base. +*/ +__forward_timer_base (base, basej); +/* * Base is idle if the next event is more than a tick away. *
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerPATCH v9 14/32 timers: Split out get next timer interrupt
In reply to Anna-Maria BehnsenSplit out get_next_timer_interrupt() to be able to extend it and make it reusable for other call sites.
No functional change.
---
v9: Move whole get_next_timer_interrupt() into a separate function instead
of splitting the bits and pieces.
--- kernel/time/timer.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-)
kernel/time/timer.c +14 -9
@@ -1911,15 +1911,7 @@ static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)return DIV_ROUND_UP_ULL (nextevt,TICK_NSEC ) *TICK_NSEC ; } -/** -* get_next_timer_interrupt - return the time (clock mono) of the next timer -* @basej: base time jiffies -* @basem: base time clock monotonic -* -* Returns the tick aligned clock monotonic time of the next pending -* timer or KTIME_MAX if no timer is pending. -*/ -u64 get_next_timer_interrupt (unsigned long basej,u64 basem) +static inline u64 __get_next_timer_interrupt (unsigned long basej,u64 basem) {struct timer_base *base =this_cpu_ptr (&timer_bases[BASE_STD ]);unsigned long nextevt = basej +NEXT_TIMER_MAX_DELTA ;
@@ -1975,6 +1967,19 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)return cmp_next_hrtimer_event (basem, expires); } +/** +* get_next_timer_interrupt() - return the time (clock mono) of the next timer +* @basej: base time jiffies +* @basem: base time clock monotonic +* +* Returns the tick aligned clock monotonic time of the next pending +* timer or KTIME_MAX if no timer is pending. +*/ +u64 get_next_timer_interrupt (unsigned long basej,u64 basem) +{ +return __get_next_timer_interrupt (basej, basem); +} +/** * timer_clear_idle - Clear the idle state of the timer base *
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerPATCH v9 15/32 timers: Move marking timer bases idle into tick_nohz_stop_tick()
In reply to Anna-Maria BehnsenThe timer base is marked idle when get_next_timer_interrupt() is executed. But the decision whether the tick will be stopped and whether the system is able to go idle is done later. When the timer bases is marked idle and a new first timer is enqueued remote an IPI is raised. Even if it is not required because the tick is not stopped and the timer base is evaluated again at the next tick.
To prevent this, the timer base is marked idle in tick_nohz_stop_tick() and get_next_timer_interrupt() is streamlined by only looking for the next timer interrupt. All other work is postponed to timer_base_try_to_set_idle() which is called by tick_nohz_stop_tick().
While at it fix some nearby whitespace damage as well.
---
v9:
- update to the changes of the patch before
- Cleanup logic in tick_nohz_stop_tick() after executing timer_base_try_to_set_idle()
--- kernel/time/tick-internal.h | 1 + kernel/time/tick-sched.c | 46 ++++++++++++++++++++++++++--------- kernel/time/timer.c | 48 ++++++++++++++++++++++++++++--------- 3 files changed, 73 insertions(+), 22 deletions(-)
kernel/time/tick-internal.h +1 -0
@@ -164,6 +164,7 @@ static inline void timers_update_nohz(void) { } DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases);extern u64 get_next_timer_interrupt (unsigned long basej,u64 basem); +u64 timer_base_try_to_set_idle (unsigned long basej,u64 basem,bool *idle);void timer_clear_idle (void );#define CLOCK_SET_WALL \
kernel/time/tick-sched.c +35 -11
81 lines not on this page · raw message
kernel/time/timer.c +37 -11
75 lines not on this page · raw message
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerPATCH v9 16/32 timers: Optimization for timer_base_try_to_set_idle()
In reply to Anna-Maria BehnsenWhen tick is stopped also the timer base is_idle flag is set. When reentering the timer_base_try_to_set_idle() with the tick stopped, there is no need to check whether the timer base needs to be set idle again. When a timer was enqueued in the meantime, this is already handled by the nohz_get_next_event() call which was executed before tick_nohz_stop_tick().
---
v9: New, as this optimization was splitted from the patch before.
--- kernel/time/tick-sched.c | 2 +- kernel/time/timer.c | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-)
kernel/time/tick-sched.c +1 -1
@@ -886,7 +886,7 @@ static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)struct clock_event_device *dev =__this_cpu_read (tick_cpu_device.evtdev);unsigned long basejiff = ts->last_jiffies;u64 basemono = ts->timer_expires_base; -bool timer_idle; +bool timer_idle = ts->tick_stopped;u64 expires;/* Make sure we won't be trying to stop it twice in a row. */
kernel/time/timer.c +8 -3
@@ -1996,13 +1996,18 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)* timer_base_try_to_set_idle() - Try to set the idle state of the timer bases * @basej: base time jiffies * @basem: base time clock monotonic -* @idle: pointer to store the value of timer_base->is_idle +* @idle: pointer to store the value of timer_base->is_idle on return; +* *idle contains the information whether tick was already stopped * -* 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. When tick was already stopped KTIME_MAX is +* returned as well. */ u64 timer_base_try_to_set_idle (unsigned long basej,u64 basem,bool *idle) { +if (*idle) +return KTIME_MAX ; +return __get_next_timer_interrupt (basej, basem, idle); }
Signature · 1 line
2.39.2
Re: [PATCH v9 16/32] timers: Optimization for timer_base_try_to_set_idle()
Quoted 5 lines · Anna-Maria Behnsen
When tick is stopped also the timer base
is_idleflag is set. When reentering thetimer_base_try_to_set_idle()with the tick stopped, there is no need to check whether the timer base needs to be set idle again. When a timer was enqueued in the meantime, this is already handled by thenohz_get_next_event()call which was executed beforetick_nohz_stop_tick().
as of #15 tick_stopped is set later in tick_nohz_stop_tick() and both (tick_sched::tick_stopped and timer_base::is_idle) are cleared in tick_nohz_restart_sched_tick().
Then we have tick_nohz_idle_retain_tick() with only one caller and is only clearing timer_base::is_idle. Now, wouldn't it make sense to preload timer_idle based on timer_base::is_idle?
I don't know if it there is a different outcome if timer_base::is_idle gets cleared in the idle path vs tick_sched::tick_stopped. I can't find nohz_get_next_event().
Quoted 1 line
Signed-off-by Anna-Maria Behnsen <[email protected]>
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 16/32] timers: Optimization for timer_base_try_to_set_idle()
Quoted 7 lines · Sebastian Siewior
Quoted 5 lines · Anna-Maria Behnsen
When tick is stopped also the timer base
is_idleflag is set. When reentering thetimer_base_try_to_set_idle()with the tick stopped, there is no need to check whether the timer base needs to be set idle again. When a timer was enqueued in the meantime, this is already handled by thenohz_get_next_event()call which was executed beforetick_nohz_stop_tick().
Quoted 7 lines
as of #15
tick_stoppedis set later intick_nohz_stop_tick()and both (tick_sched::tick_stoppedandtimer_base::is_idle) are cleared intick_nohz_restart_sched_tick().Then we have
tick_nohz_idle_retain_tick()with only one caller and is only clearingtimer_base::is_idle. Now, wouldn't it make sense to preloadtimer_idlebased ontimer_base::is_idle?
When revisting the code, this timer_clear_idle() is no longer required in tick_nohz_idle_retain_tick(). This is only called when the tick is not stopped - so timer base is not idle as well and this call is superfluous.
As we keep both states in sync (tick_sched::tick_stopped and timer_base::is_idle) it doesn't matter which one is used. In tick_nohz_stop_tick() I don't have access to timer base. I could add it to timer_base_try_to_set_idle() but it will not make a difference.
Quoted 3 lines
I don't know if it there is a different outcome if
timer_base::is_idlegets cleared in the idle path vstick_sched::tick_stopped. I can't findnohz_get_next_event().
s/nohz_get_next_event/tick_nohz_next_event/ ...
Quoted 3 lines
Quoted 1 line
Signed-off-by Anna-Maria Behnsen <[email protected]>Sebastian
Thanks,
Anna-Maria
Anna-Maria Behnsen
maintainerPATCH v9 17/32 timers: Introduce add_timer() variants which modify timer flags
In reply to Anna-Maria BehnsenTimer 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.
---
v9: Update documentation to match kernel-doc style (missing brackets after
function names)
New in v6
--- include/linux/timer.h | 2 ++ kernel/time/timer.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+)
include/linux/timer.h +2 -0
@@ -179,6 +179,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);
kernel/time/timer.c +34 -0
@@ -1245,6 +1245,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
Signature · 1 line
2.39.2
Re: [PATCH v9 17/32] timers: Introduce add_timer() variants which modify timer flags
Quoted 5 lines · Anna-Maria Behnsen
Timer might be used as pinned timer (using
add_timer_on()) and later on as non pinned timers usingadd_timer(). When the NOHZ timer pull at expiry model is in place,TIMER_PINNEDflag 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.
Slightly reworded.
| A timer might be used as a pinned timer (using add_timer_on()) and later | on as non-pinned timer using add_timer(). When the "NOHZ timer pull at | expiry model" is in place, the TIMER_PINNED flag is required to be used | whenever a timer needs to expire on a dedicated CPU. Otherwise the flag | must not be set if expiration on a dedicated CPU is not required.
Quoted 4 lines
add_timer_on()'s behavior will be changed during the preparation patches for the NOHZ timer pull at expiry model to unconditionally set
TIMER_PINNEDflag. To be able to reset/set the flag when queueing a timer, two variants ofadd_timer()are introduced.
and here.
| 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 clear/ set the flag when queueing a | timer, two variants of add_timer() are introduced.
I let you be judge of this.
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 17/32] timers: Introduce add_timer() variants which modify timer flags
Quoted 20 lines · Sebastian Siewior
Quoted 5 lines · Anna-Maria Behnsen
Timer might be used as pinned timer (using
add_timer_on()) and later on as non pinned timers usingadd_timer(). When the NOHZ timer pull at expiry model is in place,TIMER_PINNEDflag 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.Slightly reworded.
| A timer might be used as a pinned timer (using add_timer_on()) and later | on as non-pinned timer using add_timer(). When the "NOHZ timer pull at | expiry model" is in place, the TIMER_PINNED flag is required to be used | whenever a timer needs to expire on a dedicated CPU. Otherwise the flag | must not be set if expiration on a dedicated CPU is not required.Quoted 4 lines
add_timer_on()'s behavior will be changed during the preparation patches for the NOHZ timer pull at expiry model to unconditionally set
TIMER_PINNEDflag. To be able to reset/set the flag when queueing a timer, two variants ofadd_timer()are introduced.
Quoted 8 lines
and here.
| 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 clear/ set the flag when queueing a | timer, two variants of add_timer() are introduced.I let you be judge of this.
I will take your reworded version.
Thanks,
Anna-Maria
Anna-Maria Behnsen
maintainerPATCH v9 18/32 workqueue: Use global variant for add_timer()
In reply to Anna-Maria BehnsenThe 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.
---
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(-)
kernel/workqueue.c +1 -1
@@ -1958,7 +1958,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); }/**
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerPATCH v9 19/32 timers: add_timer_on(): Make sure TIMER_PINNED flag is set
In reply to Anna-Maria BehnsenWhen 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.
--- kernel/time/timer.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/time/timer.c +7 -1
@@ -1284,7 +1284,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. */
@@ -1298,6 +1301,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);/*
Signature · 1 line
2.39.2
Re: [PATCH v9 19/32] timers: add_timer_on(): Make sure TIMER_PINNED flag is set
Quoted 5 lines · 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_PINNEDflag is required to make sure timers end up in proper base.Add
TIMER_PINNEDflag unconditionally whenadd_timer_on()is executed.
This is odd. I have some vague memory that this was already the case. Otherwise all add_timer_on() users without TIMER_PINNED may get it wrong due to optimisation. Looking at git history it was never the case and I can't confuse it with hrtimer since it never supported the "_on()" feature…
At least the mix timer in drivers/char/random.c is not using the PINNED flag with _on(). So this was wrong then?…
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 19/32] timers: add_timer_on(): Make sure TIMER_PINNED flag is set
Quoted 10 lines · Sebastian Siewior
Quoted 5 lines · 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_PINNEDflag is required to make sure timers end up in proper base.Add
TIMER_PINNEDflag unconditionally whenadd_timer_on()is executed.This is odd. I have some vague memory that this was already the case. Otherwise all
add_timer_on()users withoutTIMER_PINNEDmay get it wrong due to optimisation.
Which optimisation are you talking about? Are you talking about the heuristic for finding the best CPU in get_target_base()? This heuristic is not used for add_timer_on().
Quoted 5 lines
Looking at git history it was never the case and I
can't confuse it with hrtimer since it never supported the "_on()" feature…
At least the mix timer indrivers/char/random.cis not using the PINNED flag with_on(). So this was wrong then?…
No, this it is not wrong, as at the moment timers expires always on the CPU where they have been queued. So when a timer should be queued on a dedicated CPU several approaches are valid:
- using add_timer_on() with or without TIMER_PINNED flag set to enqueue timers on any specified CPU
- use add_timer()/mod_timer()/... with TIMER_PINNED flag set - but this only works to enqueue timer on this CPU!
When using the add_timer()/mod_timer()/... functions without TIMER_PINNED flag, the heuristic is used for finding the best CPU.
So without the timer pull model the change doesn't hurt.
But with the timer pull model in place, it is required to keep the pinned and non pinned timers in separate per CPU wheels (local wheel = TIMER_PINNED is set; global wheel = TIMER_PINNED is not set). So without this change but with the timer pull model, the mix timer of random.c would be enqueued on the dedicated CPU, but it would end up in the wrong wheel (global wheel). And then the timer could also expire on another CPUs as the global wheels are handled by the migrator when the CPU is idle.
Does this makes it a little more clear, why the change is required and why it is also valid right now?
Thanks,
Anna-Maria
Re: [PATCH v9 19/32] timers: add_timer_on(): Make sure TIMER_PINNED flag is set
Quoted 9 lines · Anna-Maria Behnsen
Quoted 7 lines · Sebastian Siewior
Quoted 5 lines · 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_PINNEDflag is required to make sure timers end up in proper base.Add
TIMER_PINNEDflag unconditionally whenadd_timer_on()is executed.
Quoted 7 lines
Quoted 3 lines
This is odd. I have some vague memory that this was already the case. Otherwise all
add_timer_on()users withoutTIMER_PINNEDmay get it wrong due to optimisation.Which optimisation are you talking about? Are you talking about the heuristic for finding the best CPU in
get_target_base()? This heuristic is not used foradd_timer_on().
Yes, true. And nobody probably mixes add_timer_on() and mod_timer().
Quoted 2 lines
Does this makes it a little more clear, why the change is required and why it is also valid right now?
Yes, all good. Thanks.
Quoted 3 lines
Thanks,
Anna-Maria
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 19/32] timers: add_timer_on(): Make sure TIMER_PINNED flag is set
Quoted 14 lines · Sebastian Siewior
Quoted 13 lines · Anna-Maria Behnsen
Quoted 10 lines · Sebastian Siewior
Quoted 5 lines · 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_PINNEDflag is required to make sure timers end up in proper base.Add
TIMER_PINNEDflag unconditionally whenadd_timer_on()is executed.This is odd. I have some vague memory that this was already the case. Otherwise all
add_timer_on()users withoutTIMER_PINNEDmay get it wrong due to optimisation.
Quoted 5 lines
Quoted 3 lines
Which optimisation are you talking about? Are you talking about the heuristic for finding the best CPU in
get_target_base()? This heuristic is not used foradd_timer_on().Yes, true. And nobody probably mixes
add_timer_on()andmod_timer().
Workqueue mixes add_timer_on() and add_timer(). But therefore the add_timer_global() function is introduced in patch 17 which drops the TIMER_PINNED flag. In patch 18 the add_timer() in workqueue code is replaced by the new function.
Thanks,
Anna-Maria
Anna-Maria Behnsen
maintainerPATCH v9 20/32 timers: Ease code in run_local_timers()
In reply to Anna-Maria BehnsenThe 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.
---
v5: New patch to decrease patch size of follow up patches
--- kernel/time/timer.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
kernel/time/timer.c +6 -8
@@ -2132,16 +2132,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 ); }/*
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerPATCH v9 21/32 timers: Split next timer interrupt logic
In reply to Anna-Maria BehnsenLogic 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.
---
v9: Adapt to the fix for empty timer bases.
--- kernel/time/timer.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-)
kernel/time/timer.c +19 -13
@@ -1951,12 +1951,29 @@ 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, +unsigned long basej) +{ +if (base->next_expiry_recalc) +next_expiry_recalc (base); + +/* +* Move next_expiry for the empty base into the future to prevent a +* unnecessary raise of the timer softirq when the next_expiry value +* will be reached even if there is no timer pending. +*/ +if (!base->timers_pending) + base->next_expiry = basej +NEXT_TIMER_MAX_DELTA ; + +return base->next_expiry; +} +static inline u64 __get_next_timer_interrupt (unsigned long basej,u64 basem,bool *idle) {struct timer_base *base =this_cpu_ptr (&timer_bases[BASE_STD ]); -unsigned long nextevt = basej +NEXT_TIMER_MAX_DELTA ;u64 expires =KTIME_MAX ; +unsigned long nextevt;/* * Pretend that there is no timer pending if the cpu is offline.
@@ -1969,24 +1986,13 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, }raw_spin_lock (&base->lock); -if (base->next_expiry_recalc) -next_expiry_recalc (base); + nextevt =next_timer_interrupt (base, basej);if (base->timers_pending) { - nextevt = base->next_expiry; -/* If we missed a tick already, force 0 delta */ if (time_before (nextevt, basej)) nextevt = basej; expires = basem + (u64)(nextevt - basej) *TICK_NSEC ; - }else { -/* -* Move next_expiry for the empty base into the future to -* prevent a unnecessary raise of the timer softirq when the -* next_expiry value will be reached even if there is no timer -* pending. -*/ - base->next_expiry = nextevt; }/*
Signature · 1 line
2.39.2
Re: [PATCH v9 21/32] timers: Split next timer interrupt logic
Quoted 3 lines · Anna-Maria Behnsen
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.
Be authoritative.
| Split the logic for getting next timer interrupt (no matter of recalculated or | already stored in base->next_expiry) into a separate function named | next_timer_interrupt(). Make it available to local call sites only.
Quoted 3 lines
No functional change.
Signed-off-by Anna-Maria Behnsen <[email protected]>
Sebastian
Anna-Maria Behnsen
maintainerPATCH v9 22/32 timers: Keep the pinned timers separate from the others
In reply to Anna-Maria BehnsenSeparate 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.
---
v9:
- Update was required (change of preceding patches)
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 | 95 ++++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 27 deletions(-)
kernel/time/timer.c +68 -27
@@ -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
@@ -1971,9 +1983,10 @@ static unsigned long next_timer_interrupt(struct timer_base *base,static inline u64 __get_next_timer_interrupt (unsigned long basej,u64 basem,bool *idle) { -struct timer_base *base =this_cpu_ptr (&timer_bases[BASE_STD ]); +unsigned long nextevt, nextevt_local, nextevt_global; +struct timer_base *base_local, *base_global;u64 expires =KTIME_MAX ; -unsigned long nextevt; +bool local_first;/* * Pretend that there is no timer pending if the cpu is offline.
@@ -1985,10 +1998,31 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,return expires; } -raw_spin_lock (&base->lock); - nextevt =next_timer_interrupt (base, basej); + base_local =this_cpu_ptr (&timer_bases[BASE_LOCAL ]); + base_global =this_cpu_ptr (&timer_bases[BASE_GLOBAL ]); + +raw_spin_lock (&base_local->lock); +raw_spin_lock_nested (&base_global->lock,SINGLE_DEPTH_NESTING ); + + nextevt_local =next_timer_interrupt (base_local, basej); + nextevt_global =next_timer_interrupt (base_global, basej); -if (base->timers_pending) { +/* +* 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; + +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;
@@ -1999,28 +2033,33 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,* We have a fresh next event. Check whether we can forward the * base. */ -__forward_timer_base (base, basej); +__forward_timer_base (base_local, basej); +__forward_timer_base (base_global, basej);/* * Set base->is_idle only when caller is timer_base_try_to_set_idle() */ if (idle) {/* -* Base is idle if the next event is more than a tick away. +* Bases are idle if the next event is more than a tick away. * * If the base is marked idle then any timer add operation must * forward the base clk itself to keep granularity small. This -* idle logic is only maintained for the BASE_STD base, -* deferrable timers may still see large granularity skew (by -* design). +* idle logic is only maintained for the BASE_LOCAL and +* BASE_GLOBAL base, deferrable timers may still see large +* granularity skew (by design). */ -if (!base->is_idle) - base->is_idle =time_after (nextevt, basej +1 ); - *idle = base->is_idle; -trace_timer_base_idle (base->is_idle, base->cpu); +if (!base_local->is_idle) { +bool is_idle =time_after (nextevt, basej +1 ); + + base_local->is_idle = base_global->is_idle = is_idle; + } + *idle = base_local->is_idle; +trace_timer_base_idle (base_local->is_idle, base_local->cpu); } -raw_spin_unlock (&base->lock); +raw_spin_unlock (&base_global->lock); +raw_spin_unlock (&base_local->lock);return cmp_next_hrtimer_event (basem, expires); }
@@ -2064,15 +2103,15 @@ u64 timer_base_try_to_set_idle(unsigned long basej, u64 basem, bool *idle)*/ 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); +trace_timer_base_idle (0 ,smp_processor_id ()); }#endif
@@ -2123,11 +2162,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 ])); + } }/*
@@ -2135,7 +2176,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 ();
Signature · 1 line
2.39.2
Re: [PATCH v9 22/32] timers: Keep the pinned timers separate from the others
Quoted 15 lines · Anna-Maria Behnsen
kernel/time/timer.c +7
-2@@ -1985,10 +1998,31 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,return expires; } -raw_spin_lock (&base->lock); - nextevt =next_timer_interrupt (base, basej); + base_local =this_cpu_ptr (&timer_bases[BASE_LOCAL ]); + base_global =this_cpu_ptr (&timer_bases[BASE_GLOBAL ]); + +raw_spin_lock (&base_local->lock); +raw_spin_lock_nested (&base_global->lock,SINGLE_DEPTH_NESTING ); + + nextevt_local =next_timer_interrupt (base_local, basej);
kernel/time/timer.c static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, · line 2016
Quoted 10 lines
kernel/time/timer.c +8
-1+ nextevt_global =next_timer_interrupt (base_global, basej); -if (base->timers_pending) { +/* +* 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.
This ends like an unsolved mystery case. Could you add why one should not worry about an incorrect local_first?
But seriously, how far apart can they get and what difference does it make? At timer enqueue time clk equals jiffies. At this point one clk base could be at jiffies and the other might be a few jiffies before that.
The next event (as in next_expiry) should be valid for both compare wise. Both must be larger than jiffies. The delta between jiffies and next event has to be less than NEXT_TIMER_MAX_DELTA for each base.
kernel/time/timer.c static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, · line 2028
Quoted 12 lines
kernel/time/timer.c +9
-0+*/ +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; + +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;
So if nextevt_local missed a tick and nextevt_global is NEXT_TIMER_MAX_DELTA-1 (so we get the largest difference possible between those two) then the time_before_eq() should still come out right. We could still miss more than one tick.
This looks good. I just don't understand the (above) comment.
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 22/32] timers: Keep the pinned timers separate from the others
Quoted 47 lines · Sebastian Siewior
Quoted 15 lines · Anna-Maria Behnsen
kernel/time/timer.c +7
-2@@ -1985,10 +1998,31 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,return expires; } -raw_spin_lock (&base->lock); - nextevt =next_timer_interrupt (base, basej); + base_local =this_cpu_ptr (&timer_bases[BASE_LOCAL ]); + base_global =this_cpu_ptr (&timer_bases[BASE_GLOBAL ]); + +raw_spin_lock (&base_local->lock); +raw_spin_lock_nested (&base_global->lock,SINGLE_DEPTH_NESTING ); + + nextevt_local =next_timer_interrupt (base_local, basej);kernel/time/timer.c static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, · line 2016
Quoted 10 lines
kernel/time/timer.c +8
-1+ nextevt_global =next_timer_interrupt (base_global, basej); -if (base->timers_pending) { +/* +* 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. This ends like an unsolved mystery case. Could you add why one should not worry about an incorrect
local_first?But seriously, how far apart can they get and what difference does it make? At timer enqueue time clk equals jiffies. At this point one clk base could be at jiffies and the other might be a few jiffies before that.
The next event (as innext_expiry) should be valid for both compare wise. Both must be larger than jiffies. The delta between jiffies and next event has to be less thanNEXT_TIMER_MAX_DELTAfor each base.Quoted 9 lines
kernel/time/timer.c +9
-0+*/ +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; + +if (base_local->timers_pending || base_global->timers_pending) {
Quoted 9 lines
kernel/time/timer.c static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, · line 2028
Quoted 3 lines
kernel/time/timer.c +0
-0/* If we missed a tick already, force 0 delta */ if (time_before (nextevt, basej)) nextevt = basej;So if
nextevt_localmissed a tick andnextevt_globalis NEXT_TIMER_MAX_DELTA-1 (so we get the largest difference possible between those two) then thetime_before_eq()should still come out right. We could still miss more than one tick.
This problem was only there when comparing _empty_ bases (!timer_base::timers_pending) because of the different base clocks and the stale next_expiry.
But I didn't update the check and the comment after introducing the forward of the next_expiry when !timer_base::timers_pending in next_timer_interrupt(). So now it is sufficient to replace the local_first detection by simply doing:
local_first = time_before_eq(nextevt_local, nextevt_global);
Will fix it and will also add a comment to next_timer_interrupt() where the next_expiry is updated when !timer_base::timers_pending.
Thanks,
Anna-Maria
Anna-Maria Behnsen
maintainerPATCH v9 23/32 timers: Retrieve next expiry of pinned/non-pinned timers separately
In reply to Anna-Maria BehnsenFor 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
---
v9: Update was required (change of preceding patches)
--- kernel/time/timer.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-)
kernel/time/timer.c +31 -4
@@ -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);
@@ -1983,10 +1988,11 @@ static unsigned long next_timer_interrupt(struct timer_base *base,static inline u64 __get_next_timer_interrupt (unsigned long basej,u64 basem,bool *idle) { +struct timer_events tevt = { .local =KTIME_MAX , .global =KTIME_MAX };unsigned long nextevt, nextevt_local, nextevt_global;struct timer_base *base_local, *base_global; -u64 expires =KTIME_MAX ;bool local_first; +u64 expires;/* * Pretend that there is no timer pending if the cpu is offline.
@@ -1995,7 +2001,7 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,if (cpu_is_offline (smp_processor_id ())) {if (idle) *idle = true; -return expires; +return tevt.local; } base_local =this_cpu_ptr (&timer_bases[BASE_LOCAL ]);
@@ -2022,13 +2028,31 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, nextevt = local_first ? nextevt_local : nextevt_global; -if (base_local->timers_pending || base_global->timers_pending) { +/* +* If the @nextevt is at max. one tick away, use @nextevt and store +* it in the local expiry value. The next global event is irrelevant in +* this case and can be left as KTIME_MAX. +*/ +if (time_before_eq (nextevt, basej +1 )) {/* 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; } +/* +* 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 ; +/* * We have a fresh next event. Check whether we can forward the * base.
@@ -2058,9 +2082,12 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,trace_timer_base_idle (base_local->is_idle, base_local->cpu); } +unlock:raw_spin_unlock (&base_global->lock);raw_spin_unlock (&base_local->lock); + expires =min_t (u64, tevt.local, tevt.global); +return cmp_next_hrtimer_event (basem, expires); }
Signature · 1 line
2.39.2
Re: [PATCH v9 23/32] timers: Retrieve next expiry of pinned/non-pinned timers separately
Quoted 9 lines · Anna-Maria Behnsen
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_eventsis introduced.No functional change
Originally-by Richard Cochran (linutronix GmbH) <[email protected]>Signed-off-by Anna-Maria Behnsen <[email protected]>Reviewed-by Frederic Weisbecker <[email protected]>
…
Quoted 3 lines
index
366ea26ce3ba..0d53d853ae22100644kernel/time/timer.c +0
-0
…
Quoted 7 lines
kernel/time/timer.c +2
-1@@ -2022,13 +2028,31 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, nextevt = local_first ? nextevt_local : nextevt_global; -if (base_local->timers_pending || base_global->timers_pending) { +/* +* If the @nextevt is at max. one tick away, use @nextevt and store
kernel/time/timer.c static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, · line 2041
Quoted 10 lines
kernel/time/timer.c +6
-1+* it in the local expiry value. The next global event is irrelevant in +* this case and can be left as KTIME_MAX. +*/ +if (time_before_eq (nextevt, basej +1 )) {/* 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;
You claim "No functional change" in the patch description. However if you take the shortcut here you don't update idle' if set and you don't __forward_timer_base(). The idle` parameter doesn't matter because it was false and will remain false as per current logic.
But what about the forward of the timer base? It is probably not real problem since the next add/mod timer call will forward it.
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 23/32] timers: Retrieve next expiry of pinned/non-pinned timers separately
Quoted 38 lines · Sebastian Siewior
Quoted 9 lines · Anna-Maria Behnsen
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_eventsis introduced.No functional change
Originally-by Richard Cochran (linutronix GmbH) <[email protected]>Signed-off-by Anna-Maria Behnsen <[email protected]>Reviewed-by Frederic Weisbecker <[email protected]>…
Quoted 3 lines
index
366ea26ce3ba..0d53d853ae22100644kernel/time/timer.c +0
-0…
Quoted 7 lines
kernel/time/timer.c +2
-1@@ -2022,13 +2028,31 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, nextevt = local_first ? nextevt_local : nextevt_global; -if (base_local->timers_pending || base_global->timers_pending) { +/* +* If the @nextevt is at max. one tick away, use @nextevt and store kernel/time/timer.c static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, · line 2041
Quoted 10 lines
kernel/time/timer.c +6
-1+* it in the local expiry value. The next global event is irrelevant in +* this case and can be left as KTIME_MAX. +*/ +if (time_before_eq (nextevt, basej +1 )) {/* 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;You claim "No functional change" in the patch description. However if you take the shortcut here you don't update
idle' if set and you don't __forward_timer_base(). Theidle` parameter doesn't matter because it was false and will remain false as per current logic.
Quoted 2 lines
But what about the forward of the timer base? It is probably not real problem since the next add/mod timer call will forward it.
You are right. It is not a problem as the timer base will be forwarded by add/mod timer and also when timers needs to expire. (It is reworked by the next patch...)
But it is not consistent and happend within one of the last rework iterations. I'll change the goto label into 'forward' and place it before the forward calls.
Thanks,
Anna-Maria
Anna-Maria Behnsen
maintainerPATCH v9 24/32 timers: Split out "get next timer interrupt" functionality
In reply to Anna-Maria BehnsenThe 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.
---
v9: Update was required (change of preceding patches)
v6: s/splitted/split
v5: Update commit message
v4: Fix typo in comment
--- kernel/time/timer.c | 64 +++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 26 deletions(-)
kernel/time/timer.c +38 -26
@@ -1985,30 +1985,13 @@ static unsigned long next_timer_interrupt(struct timer_base *base,return base->next_expiry; } -static inline u64 __get_next_timer_interrupt (unsigned long basej,u64 basem, -bool *idle) +static unsigned long fetch_next_timer_interrupt (unsigned long basej,u64 basem, +struct timer_base *base_local, +struct timer_base *base_global, +struct timer_events *tevt) { -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; -u64 expires; - -/* -* Pretend that there is no timer pending if the cpu is offline. -* Possible pending timers will be migrated later to an active cpu. -*/ -if (cpu_is_offline (smp_processor_id ())) { -if (idle) - *idle = true; -return tevt.local; - } - - base_local =this_cpu_ptr (&timer_bases[BASE_LOCAL ]); - base_global =this_cpu_ptr (&timer_bases[BASE_GLOBAL ]); - -raw_spin_lock (&base_local->lock); -raw_spin_lock_nested (&base_global->lock,SINGLE_DEPTH_NESTING ); nextevt_local =next_timer_interrupt (base_local, basej); nextevt_global =next_timer_interrupt (base_global, basej);
@@ -2037,8 +2020,8 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,/* If we missed a tick already, force 0 delta */ if (time_before (nextevt, basej)) nextevt = basej; - tevt.local = basem + (u64)(nextevt - basej) *TICK_NSEC ; - goto unlock; + tevt->local = basem + (u64)(nextevt - basej) *TICK_NSEC ; +return nextevt; }/*
@@ -2048,10 +2031,40 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,* 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 ; + tevt->global = basem + (u64)(nextevt_global - basej) *TICK_NSEC ;if (base_local->timers_pending) - tevt.local = basem + (u64)(nextevt_local - basej) *TICK_NSEC ; + tevt->local = basem + (u64)(nextevt_local - basej) *TICK_NSEC ; + +return nextevt; +} + +static inline u64 __get_next_timer_interrupt (unsigned long basej,u64 basem, +bool *idle) +{ +struct timer_events tevt = { .local =KTIME_MAX , .global =KTIME_MAX }; +struct timer_base *base_local, *base_global; +unsigned long nextevt; +u64 expires; + +/* +* Pretend that there is no timer pending if the cpu is offline. +* Possible pending timers will be migrated later to an active cpu. +*/ +if (cpu_is_offline (smp_processor_id ())) { +if (idle) + *idle = true; +return tevt.local; + } + + base_local =this_cpu_ptr (&timer_bases[BASE_LOCAL ]); + base_global =this_cpu_ptr (&timer_bases[BASE_GLOBAL ]); + +raw_spin_lock (&base_local->lock); +raw_spin_lock_nested (&base_global->lock,SINGLE_DEPTH_NESTING ); + + nextevt =fetch_next_timer_interrupt (basej, basem, base_local, + base_global, &tevt);/* * We have a fresh next event. Check whether we can forward the
@@ -2082,7 +2095,6 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,trace_timer_base_idle (base_local->is_idle, base_local->cpu); } -unlock:raw_spin_unlock (&base_global->lock);raw_spin_unlock (&base_local->lock);
Signature · 1 line
2.39.2
Re: [PATCH v9 24/32] timers: Split out "get next timer interrupt" functionality
Quoted 4 lines · Anna-Maria Behnsen
kernel/time/timer.c +0
-0
…
Quoted 16 lines
kernel/time/timer.c +16
-0+static inline u64 __get_next_timer_interrupt (unsigned long basej,u64 basem, +bool *idle) +{ +struct timer_events tevt = { .local =KTIME_MAX , .global =KTIME_MAX }; +struct timer_base *base_local, *base_global; +unsigned long nextevt; +u64 expires; + +/* +* Pretend that there is no timer pending if the cpu is offline. +* Possible pending timers will be migrated later to an active cpu. +*/ +if (cpu_is_offline (smp_processor_id ())) { +if (idle) + *idle = true; +return tevt.local;
kernel/time/timer.c static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, · line 2067
Quoted 10 lines
kernel/time/timer.c +10
-0+ } + + base_local =this_cpu_ptr (&timer_bases[BASE_LOCAL ]); + base_global =this_cpu_ptr (&timer_bases[BASE_GLOBAL ]); + +raw_spin_lock (&base_local->lock); +raw_spin_lock_nested (&base_global->lock,SINGLE_DEPTH_NESTING ); + + nextevt =fetch_next_timer_interrupt (basej, basem, base_local, + base_global, &tevt);
Now you split it, move it and we have the __forward_timer_base() back in case of the shortcut which is now in fetch_next_timer_interrupt(). All good.
kernel/time/timer.c static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, · line 2070
Quoted 3 lines
kernel/time/timer.c +0
-0/* * We have a fresh next event. Check whether we can forward the
Sebastian
Anna-Maria Behnsen
maintainerPATCH v9 25/32 timers: Add get next timer interrupt functionality for remote CPUs
In reply to Anna-Maria BehnsenTo 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.
---
v8:
- Update comment
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 | 76 ++++++++++++++++++++++++++++++++++--- 2 files changed, 81 insertions(+), 5 deletions(-)
kernel/time/tick-internal.h +10 -0
@@ -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 ) { }
kernel/time/timer.c +71 -5
88 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH v9 25/32] timers: Add get next timer interrupt functionality for remote CPUs
Quoted 10 lines · 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 <[email protected]>Reviewed-by Frederic Weisbecker <[email protected]>
Please fold the hunk below, it keeps sparse happy.
------->8---------
kernel/time/timer.c +4 -0
@@ -2075,6 +2075,8 @@ void fetch_next_timer_interrupt_remote(unsigned long basej, u64 basem,* Unlocks the remote timer bases. */ void timer_unlock_remote_bases(unsigned int cpu) + __releases(timer_bases[BASE_LOCAL]->lock) + __releases(timer_bases[BASE_GLOBAL]->lock) {struct timer_base *base_local, *base_global;
@@ -2092,6 +2094,8 @@ void timer_unlock_remote_bases(unsigned int cpu)* Locks the remote timer bases. */ void timer_lock_remote_bases(unsigned int cpu) + __acquires(timer_bases[BASE_LOCAL]->lock) + __acquires(timer_bases[BASE_GLOBAL]->lock) {struct timer_base *base_local, *base_global;
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 25/32] timers: Add get next timer interrupt functionality for remote CPUs
Quoted 5 lines · Sebastian Siewior
Quoted 4 lines · 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.
Quoted 8 lines
Quoted 6 lines
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 <[email protected]>Reviewed-by Frederic Weisbecker <[email protected]>Please fold the hunk below, it keeps sparse happy.
Thanks, I will do!
Anna-Maria
Anna-Maria Behnsen
maintainerPATCH v9 26/32 timers: Restructure internal locking
In reply to Anna-Maria BehnsenFrom: "Richard Cochran (linutronix GmbH)" <[email protected]>
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.
--- kernel/time/timer.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-)
kernel/time/timer.c +21 -10
@@ -2230,11 +2230,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)) {
@@ -2258,21 +2254,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 ); } }
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerPATCH v9 27/32 timers: Check if timers base is handled already
In reply to Anna-Maria BehnsenDue 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.
---
v6: Drop double negation
--- kernel/time/timer.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/time/timer.c +3 -0
@@ -2232,6 +2232,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);
Signature · 1 line
2.39.2
Re: [PATCH v9 27/32] timers: Check if timers base is handled already
Quoted 4 lines · 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
CPU
so it is consistent with the other.
Quoted 7 lines
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 <[email protected]>
Sebastian
Anna-Maria Behnsen
maintainerPATCH v9 28/32 tick/sched: Split out jiffies update helper function
In reply to Anna-Maria BehnsenFrom: "Richard Cochran (linutronix GmbH)" <[email protected]>
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.
--- kernel/time/tick-internal.h | 1 + kernel/time/tick-sched.c | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-)
kernel/time/tick-internal.h +1 -0
@@ -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,
kernel/time/tick-sched.c +15 -3
@@ -799,18 +799,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;
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerPATCH v9 29/32 timers: Introduce function to check timer base is_idle flag
In reply to Anna-Maria BehnsenTo prepare for the conversion of the NOHZ timer placement to a pull at expiry time model it's required to have a function that returns the value of the is_idle flag of the timer base to keep the hierarchy states during online in sync with timer base state.
No functional change.
---
v9: new in v9
--- kernel/time/tick-internal.h | 1 + kernel/time/timer.c | 10 ++++++++++ 2 files changed, 11 insertions(+)
kernel/time/tick-internal.h +1 -0
@@ -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 bool timer_base_is_idle (void );# endif #else /* CONFIG_NO_HZ_COMMON */ static inline void timers_update_nohz (void ) { }
kernel/time/timer.c +10 -0
@@ -2201,6 +2201,16 @@ u64 timer_base_try_to_set_idle(unsigned long basej, u64 basem, bool *idle)return __get_next_timer_interrupt (basej, basem, idle); } +/** +* timer_base_is_idle() - Return whether timer base is set idle +* +* Returns value of local timer base is_idle value. +*/ +bool timer_base_is_idle (void ) +{ +return __this_cpu_read (timer_bases[BASE_LOCAL ].is_idle); +} +/** * timer_clear_idle - Clear the idle state of the timer base *
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerPATCH v9 30/32 timers: Implement the hierarchical pull model
In reply to Anna-Maria BehnsenPlacing 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.
---
v9:
- Adapt to the changes of the preceding patches
- Fix state inconsitency (when timer base is idle, cpu must also be marked as idle in hierarchy)
- Make sure new timers are considered, when timer base is idle and a
timer is enqueued into global queue (e.g. during interrupt) ->
timer_use_tmigr() - Changes which are required due to the timer code change of marking the timer base idle in tick_nohz_stop_tick()
v8:
- Review of Frederic:
- Fix hotplug race (introduction of wakeup_recalc)
- Make wakeup and wakeup_recalc logic consistent all over the place
- Fix child/group state race and read it with locks held
- Add more clarifying comments
- Fix grammar all over the place
- change integers which act as boolean value into bool
- rewrite condition in tmigr_check_migrator() without negation
- Improve update events logic with a check of the first event
- Implement a quick forecast which is called when get_next_timer_interrupt() is executed.
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 | 111 ++- kernel/time/timer_migration.c | 1636 +++++++++++++++++++++++++++++++++ kernel/time/timer_migration.h | 144 +++ 6 files changed, 1888 insertions(+), 8 deletions(-) create mode 100644 kernel/time/timer_migration.c create mode 100644 kernel/time/timer_migration.h
include/linux/cpuhotplug.h +1 -0
@@ -245,6 +245,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,
kernel/time/Makefile +3 -0
@@ -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
kernel/time/tick-internal.h +1 -0
@@ -167,6 +167,7 @@ extern void fetch_next_timer_interrupt_remote(unsigned long basej, u64 basem,extern void timer_lock_remote_bases (unsigned int cpu);extern void timer_unlock_remote_bases (unsigned int cpu);extern bool timer_base_is_idle (void ); +extern void timer_expire_remote (unsigned int cpu);# endif #else /* CONFIG_NO_HZ_COMMON */ static inline void timers_update_nohz (void ) { }
kernel/time/timer.c +103 -8
176 lines not on this page · raw message
kernel/time/timer_migration.c new file +1636 -0
1,636 lines not on this page · raw message
kernel/time/timer_migration.h new file +144 -0
144 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH v9 30/32] timers: Implement the hierarchical pull model
On 2023-12-01 10:26:52 [+0100], Anna-Maria Behnsen wrote:
…
Quoted 6 lines
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
an
Quoted 1 line
timerqueue and eventually expired by some other active CPU.
s/some other/another ?
…
Quoted 8 lines
Signed-off-by Anna-Maria Behnsen <[email protected]>---
kernel/time/timer.c +0
-0@@ -2103,6 +2104,64 @@ void timer_lock_remote_bases(unsigned int cpu)
…
Quoted 16 lines
kernel/time/timer.c +16
-0+static void timer_use_tmigr (unsigned long basej,u64 basem, +unsigned long *nextevt,bool *tick_stop_path, +bool timer_base_idle,struct timer_events *tevt) +{ +u64 next_tmigr; + +if (timer_base_idle) + next_tmigr =tmigr_cpu_new_timer (tevt->global); +else if (tick_stop_path) + next_tmigr =tmigr_cpu_deactivate (tevt->global); +else + next_tmigr =tmigr_quick_check (); + +/* +* If the CPU is the last going idle in timer migration hierarchy, make +* sure the CPU will wake up in time to handle remote timers.
kernel/time/timer.c void timer_lock_remote_bases(unsigned int cpu) · line 2148
Quoted 10 lines
kernel/time/timer.c +10
-0+* 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 );
Is this considered a hot path? Asking because u64 divs are nice if can be avoided ;)
I guess the original value is from fetch_next_timer_interrupt(). But then you only need it if the caller (__get_next_timer_interrupt()) has the `idle' value set. Otherwise the operation is pointless. Would it somehow work to replace
base_local->is_idle = time_after(nextevt, basej + 1);
with maybe something like
base_local->is_idle = tevt.local > basem + TICK_NSEC
If so you could avoid the `nextevt' maneuver.
Quoted 5 lines
kernel/time/timer.c +5
-0+ *nextevt = basej + (unsigned long )tmp; + tevt->local = next_tmigr; + } +} +# else
…
kernel/time/timer.c static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, · line 2194
Quoted 6 lines
kernel/time/timer.c +2
-0@@ -2132,6 +2190,21 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, nextevt =fetch_next_timer_interrupt (basej, basem, base_local, base_global, &tevt); +/* +* When the when the next event is only one jiffie ahead there is no
If the next event is only one jiffy ahead then there is no
kernel/time/timer.c static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem, · line 2197
Quoted 3 lines
kernel/time/timer.c +3
-0+* need to call timer migration hierarchy related +* functions. @tevt->global will be KTIME_MAX, nevertheless if the next +* timer is a global timer. This is also true, when the timer base is
The second sentence is hard to parse.
Quoted 19 lines
kernel/time/timer.c +10
-0+* idle. +* +* The proper timer migration hierarchy function depends on the callsite +* and whether timer base is idle or not. @nextevt will be updated when +* this CPU needs to handle the first timer migration hierarchy event. +*/ +if (time_after (nextevt, basej +1 )) +timer_use_tmigr (basej, basem, &nextevt, idle, + base_local->is_idle, &tevt); +/* * We have a fresh next event. Check whether we can forward the * base. kernel/time/timer_migration.c new file +0
-0@@ -0,0 +1,1636 @@
…
Quoted 16 lines
+/*
+ * 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
+ *
Quoted 2 lines
+ * 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
In the CPUS list between 24-31 and 32-39 is a tab while the other separators are spaces. Could you please align it with spaces? Judging form the top you have tabstop=8 but here tabstop=4 looks "nice".
Quoted 5 lines
+ *
+ * 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 flag of events is set.
+ *
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 30/32] timers: Implement the hierarchical pull model
Quoted 1 line · Sebastian Siewior
On 2023-12-01 10:26:52 [+0100], Anna-Maria Behnsen wrote:
[...]
Quoted 27 lines
Quoted 5 lines
kernel/time/timer.c +0
-0@@ -2103,6 +2104,64 @@ void timer_lock_remote_bases(unsigned int cpu)…
Quoted 21 lines
kernel/time/timer.c +21
-0+static void timer_use_tmigr (unsigned long basej,u64 basem, +unsigned long *nextevt,bool *tick_stop_path, +bool timer_base_idle,struct timer_events *tevt) +{ +u64 next_tmigr; + +if (timer_base_idle) + next_tmigr =tmigr_cpu_new_timer (tevt->global); +else if (tick_stop_path) + next_tmigr =tmigr_cpu_deactivate (tevt->global); +else + next_tmigr =tmigr_quick_check (); + +/* +* If the CPU is the last going idle in timer migration hierarchy, make +* sure the 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; +
Quoted 8 lines
kernel/time/timer.c void timer_lock_remote_bases(unsigned int cpu) · line 2148
Quoted 5 lines
kernel/time/timer.c +5
-0+/* If we missed a tick already, force 0 delta */ +if (next_tmigr < basem) + next_tmigr = basem; + + tmp =div_u64 (next_tmigr - basem,TICK_NSEC );Is this considered a hot path? Asking because u64 divs are nice if can be avoided ;)
It's the 'try to go idle path' - so no hot path. Please correct me if I'm wrong.
Quoted 11 lines
I guess the original value is from
fetch_next_timer_interrupt(). But then you only need it if the caller (__get_next_timer_interrupt()) has the `idle' value set. Otherwise the operation is pointless. Would it somehow work to replacebase_local->is_idle = time_after(nextevt, basej + 1);with maybe something like
base_local->is_idle = tevt.local > basem + TICK_NSECIf so you could avoid the `nextevt' maneuver.
This change could be done indepentant as an improvement on top of the queue as well. I will not improve it right now, if it's ok.
Quoted 5 lines
kernel/time/timer.c void timer_lock_remote_bases(unsigned int cpu) · line 2154
Quoted 5 lines
kernel/time/timer.c +5
-0+ *nextevt = basej + (unsigned long )tmp; + tevt->local = next_tmigr; + } +} +# else
Thanks for the other input - I already changed it for v10 of the queue.
Thanks,
Anna-Maria
Re: [PATCH v9 30/32] timers: Implement the hierarchical pull model
In reply to Anna-Maria BehnsenQuoted 6 lines · Anna-Maria Behnsen
kernel/time/timer_migration.c new file +0
-0@@ -0,0 +1,1636 @@
…
Quoted 5 lines
+ * Protection of the 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 a
s/a$//
Quoted 16 lines
+ * 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
Quoted 5 lines
+ *
+ *
+ * 1. CPU0 goes idle (changes are updated in GRP0:0; afterwards the current
+ * states of GRP0:0 and GRP1:0 are stored in the data for walking the
+ * hierarchy):
CPU0 goes idle. The state update is performed lock less and group wise. In the first step only GRP0:0 has been updated. The update of GRP1:0 is pending, the CPU walks through the hierarchy.
Quoted 12 lines
+ *
+ * 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
Quoted 3 lines
+ * 2. CPU1 comes out of idle (changes are update in GRP0:0; afterwards the
+ * current states of GRP0:0 and GRP1:0 are stored in the data for walking the
+ * hierarchy):
While CPU0 goes idle and continues to update the state, CPU1 comes out of idle. CPU1 updates GRP0:0. The update for GRP1:0 is pending, tge CPU walks through the hierarchy. Both CPUs now walk the hierarchy to perform the needed update from their point of view. The currently visible state:
Quoted 11 lines
+ *
+ * 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
Quoted 4 lines
+ *
+ * 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.
Here is the race condition: CPU1 managed to propagate its changes through the hierarchy to GRP1:0 before CPU0 did. The active members of GRP1:0 remain unchanged after the update since it is still valid from CPU1 current point of view:
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
[ I take it as the migrator remains set to GRP0:1 by CPU1 but it could
be changed to GRP0:0. I assume that both fields (migrator+active) are changed there via the propagation and the arrow in both fields denotes this. ]
Quoted 1 line
+ * 4. Propagating the changes of step 1 through the hierarchy to GRP1:0
Now CPU0 finally propagates its changes to GRP1:0.
Quoted 11 lines
+ *
+ * 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
Quoted 4 lines
+ *
+ * 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.
The race of CPU0 vs CPU1 led to an inconsistent state in GRP1:0. CPU1 is active and is correctly listed as active in GRP0:0. However GRP1:0 does not have GRP0:0 listed as active which is wrong. The sequence counter has been added to avoid inconsistent states during updates. The state is updated atomically only if all members, including the sequence counter, match the expected value (compare-and-exchange).
Looking back at the previous example with the addition of the sequence number: The update as performed by CPU0 in step 4 will fail. CPU1 changed the sequence number during the update in step 3 so the expected old value (as seen by CPU0 before starting the walk) does not match.
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 30/32] timers: Implement the hierarchical pull model
Quoted 121 lines · Sebastian Siewior
Quoted 6 lines · Anna-Maria Behnsen
kernel/time/timer_migration.c new file +0
-0@@ -0,0 +1,1636 @@…
Quoted 5 lines
+ * Protection of the 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 as/a$//
Quoted 16 lines
+ * 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 idleQuoted 5 lines
+ *
+ *
+ * 1. CPU0 goes idle (changes are updated in GRP0:0; afterwards the current
+ * states of GRP0:0 and GRP1:0 are stored in the data for walking the
+ * hierarchy):CPU0 goes idle. The state update is performed lock less and group wise. In the first step only GRP0:0 has been updated. The update of GRP1:0 is pending, the CPU walks through the hierarchy.
Quoted 12 lines
+ *
+ * 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 idleQuoted 3 lines
+ * 2. CPU1 comes out of idle (changes are update in GRP0:0; afterwards the
+ * current states of GRP0:0 and GRP1:0 are stored in the data for walking the
+ * hierarchy):While CPU0 goes idle and continues to update the state, CPU1 comes out of idle. CPU1 updates GRP0:0. The update for GRP1:0 is pending, tge CPU walks through the hierarchy. Both CPUs now walk the hierarchy to perform the needed update from their point of view. The currently visible state:
Quoted 11 lines
+ *
+ * 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 idleQuoted 4 lines
+ *
+ * 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.Here is the race condition: CPU1 managed to propagate its changes through the hierarchy to GRP1:0 before CPU0 did. The active members of GRP1:0 remain unchanged after the update since it is still valid from CPU1 current point of view:
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[ I take it as the migrator remains set to GRP0:1 by CPU1 but it could
be changed to GRP0:0. I assume that both fields (migrator+active) are changed there via the propagation and the arrow in both fields denotes this. ]Quoted 1 line
+ * 4. Propagating the changes of step 1 through the hierarchy to GRP1:0
Now CPU0 finally propagates its changes to GRP1:0.
Quoted 11 lines
+ *
+ * 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 idleQuoted 4 lines
+ *
+ * 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.
Quoted 13 lines
The race of CPU0 vs CPU1 led to an inconsistent state in GRP1:0. CPU1 is active and is correctly listed as active in GRP0:0. However GRP1:0 does not have GRP0:0 listed as active which is wrong. The sequence counter has been added to avoid inconsistent states during updates. The state is updated atomically only if all members, including the sequence counter, match the expected value (compare-and-exchange).
Looking back at the previous example with the addition of the sequence number: The update as performed by CPU0 in step 4 will fail. CPU1 changed the sequence number during the update in step 3 so the expected old value (as seen by CPU0 before starting the walk) does not match.
Thanks a lot for rephrasing the documentation to make it clearer for the reader! I use your proposal with some minor changes.
Thanks,
Anna-Maria
Re: [PATCH v9 30/32] timers: Implement the hierarchical pull model
In reply to Anna-Maria BehnsenQuoted 6 lines · Anna-Maria Behnsen
kernel/time/timer_migration.c new file +0
-0@@ -0,0 +1,1636 @@
…
Quoted 7 lines
+ * Required event and timerqueue update after a remote expiry:
+ * -----------------------------------------------------------
+ *
+ * After a remote expiry of a CPU, a walk through the hierarchy updating the
+ * 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:
After expiring timers of a remote CPU, a walk through the hierarchy and updating events timerqueues is required. It is obviously needed if there is a 'new' global timer but also if there no new global timer but the remote CPU is still idle.
Quoted 2 lines
+ * 1. CPU2 is the migrator and does the remote expiry in GRP1:0; expiry of
+ * evt-CPU0 and evt-CPU1 are equal:
CPU0 and CPU1 have both a timer expiring at the same time so both have an event enqueued in the timerqueue. CPU2 and CPU3 have no global timer pending and CPU2 is the only active CPU and also the migrator.
Quoted 16 lines
+ *
+ * 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 = false groupevt.ignore = true + * groupevt.cpu = CPU0 groupevt.cpu = + * timerqueue = evt-CPU0, timerqueue = + * evt-CPU1 + * / \ / \ + * CPUs 0 1 2 3 + * idle idle active idle
Quoted 3 lines
+ *
+ * 2. Remove the first event of the timerqueue in GRP1:0 and expire the timers
+ * of CPU0 (see evt-GRP0:0->cpu value):
CPU2 begins to expire remote timers. It starts with own group GRP0:1. GRP0:1 has nothing in ts timerqueue and continues with its parent, GRP1:0. In GRP1:0 it dequeues the first event. It looks at CPU member expires the pending timer of CPU0.
Quoted 12 lines
+ * 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 = false groupevt.ignore = true + * --> groupevt.cpu = CPU0 groupevt.cpu = + * timerqueue = evt-CPU0, timerqueue =+ * evt-CPU1
Quoted 8 lines
+ * / \ / \
+ * CPUs 0 1 2 3 + * idle idle active idle + * + * 3. After the remote expiry CPU0 has no global timer that needs to be + * enqueued. When skipping the walk, the global timer of CPU1 is not handled, + * as the 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:
The work isn't over after expiring timers of CPU0. If we stop here, then CPU1's timer have not been expired and the timerqueue of GRP0:0 has still an event for CPU0 enqueued which has just been processed. So it is required to walk the hierarchy from CPU0's point of view and update it accordingly.
CPU0 will be removed from the timerqueue because it has no pending timer. If CPU0 would have a timer pending then it has to expire after CPU1's first timer because all timer from this period have just been expired.
Either way CPU1 will be first in GRP0:0's timerqueue and therefore set in the CPU field of the group event which is enqueued in GRP1:0's timerqueue.
Quoted 14 lines
+ * 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 = false groupevt.ignore = true + * --> groupevt.cpu = CPU1 groupevt.cpu = + * --> timerqueue = evt-CPU1 timerqueue = + * / \ / \ + * CPUs 0 1 2 3 + * idle idle active idle
Quoted 3 lines
+ *
+ * Now CPU2 (migrator) is able to handle the timer of CPU1 as CPU2 only scans
+ * the timerqueues of GRP0:1 and GRP1:0.
Now CPU2 continues step 2 at GRP1:0 and will expire the timer of CPU1.
Quoted 4 lines
+ * The update of step 3 is valid to be skipped, when the remote CPU went offline
+ * in the meantime because an 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.
The hierarchy walk in step 3 can be skipped if the migrator notices that a CPU of GRP0:0 is active. The CPU will mark GRP0:0 active and take care of the group and any needed updates within the hierarchy.
I skipped the "offline" part because it is not needed. Before the CPU can go offline it has first to come out of idle. While going offline it won't (probably) participate here and the remaining timer will be migrated to another CPU.
Quoted 1 line
+ */
…
Quoted 15 lines
kernel/time/timer_migration.c new file +15
-0+ +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;
kernel/time/timer_migration.c line 342
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+ 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); +}
So these two. walk_groups() uses all have tmigr_cpu::lock acquired and __walk_groups() don't. Also the `up' function passed walk_groups() has always the same data type while the data argument passed to __walk_groups() has also the same type but different compared to the former.
Given the locking situation and the type of the data argument looks like walk_groups() is used for thing#1 and __walk_groups() for thing#2. Therefore it could make sense have two separate functions (instead of walk_groups() and __walk_groups()) to distinguish this.
Now it is too late but maybe later I figure out why the one type requires locking and the other doesn't.
…
Quoted 7 lines
kernel/time/timer_migration.c new file +7
-0+/* +* Return the next event which is already expired of the group timerqueue +* +* Event, which is returned, is also removed from the queue. +*/ +static struct tmigr_event *tmigr_next_expired_groupevt (struct tmigr_group *group, +u64 now)
kernel/time/timer_migration.c line 448
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+{ +struct tmigr_event *evt =tmigr_next_groupevt (group); + +if (!evt || now < evt->nextevt.expires) +return NULL ; + +/* +* The event is already expired. Remove it. If it's not the last event, +* then update all group event related information. +*/
The event expired, remove it. Update group's next expire time.
kernel/time/timer_migration.c line 452
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+if (timerqueue_del (&group->events, &evt->nextevt)) +tmigr_next_groupevt (group); +else +WRITE_ONCE (group->next_expiry,KTIME_MAX );
And then you can invoke tmigr_next_groupevt() unconditionally.
kernel/time/timer_migration.c line 420
Quoted 3 lines
kernel/time/timer_migration.c new file +3
-0+return evt; +} +
Sebastian
Re: [PATCH v9 30/32] timers: Implement the hierarchical pull model
In reply to Anna-Maria BehnsenQuoted 6 lines · Anna-Maria Behnsen
kernel/time/timer_migration.c new file +0
-0@@ -0,0 +1,1636 @@
…
Quoted 18 lines
kernel/time/timer_migration.c new file +18
-0+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; +
kernel/time/timer_migration.c line 496
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+/* 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;
This does not look right. If
group->migr_state != curstate.state
then
curstate.state = newstate.state
does not make a difference since curstate is on stack. So this should become
| if (!atomic_try_cmpxchg(&group->migr_state, &curstate.state, newstate.state)) {
| curstate.state = newstate.state = atomic_read(&group->parent->migr_state);
and now I question the existence of tmigr_walk::groupstate. It does not match the comment for the struct saying it will be re-read if the cmpxchg() fails because it does not happen (at least here). Also why do we have it? Is it avoid atomic_read() and have it "cached"?
kernel/time/timer_migration.c line 500
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+ goto retry; + } + +if (group->parent && (walk_done == false)) {
The group's parent doesn't change so it can be accessed lock-less. It is just that the topmost group has no parent so we need this check. I would move the walk_done check to the left so it can be evaluated first.
kernel/time/timer_migration.c line 502
Quoted 2 lines
kernel/time/timer_migration.c new file +2
-0+ data->groupstate.state =atomic_read (&group->parent->migr_state); + data->childmask = group->childmask;
We don't re-read in case the cmpxchg failed assuming someone else is updating the state. Wouldn't it make sense to read the childmask at top of the function from the child pointer. There is no need to keep around in the data pointer, right?
kernel/time/timer_migration.c line 511
Quoted 9 lines
kernel/time/timer_migration.c new file +9
-0+ } + +/* +* The group is active and the event will be ignored - the ignore flag is +* updated without holding the lock. In case the bit is set while +* another CPU already handles remote events, nothing happens, because +* it is clear that the CPU became active just in this moment, or in +* worst case the event is handled remote. Nothing to worry about. +*/
The CPU is becoming active, so is the group. The ignore flag for the group is updated lock less to reflect this. Another CPU might also set it true while becoming active. In worst case the migrator observes it too late and expires remotely timer belonging to this group. The lock is held while going idle (and setting ignore to false) so the state is not lost.
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+ group->groupevt.ignore = true; + +return walk_done; +}
…
Quoted 3 lines
+/*
+ * Returns the expiry of the next timer that needs to be handled.KTIME_MAXis
+ * returned, when an active CPU will handle all the timer migration hierarchy
s/when/if ?
Quoted 29 lines
+ * timers.
+ */kernel/time/timer_migration.c new file +27
-0+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 = false; + 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 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 ; +
kernel/time/timer_migration.c line 750
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+ tmc =per_cpu_ptr (&tmigr_cpu, cpu); + +raw_spin_lock_irq (&tmc->lock); + +/* +* The remote CPU is offline or the CPU event does not has to be handled +* (the CPU is active or there is no longer an event to expire) or +* another CPU handles the CPU timers already or the next event was +* already expired - return! +*/
The comment is the english version of the C code below. The *why* is usually the thing we care about. This basically sums up to:
If remote CPU is offline then the timer events have been migrated away.
If tmigr_cpu::remote is set then another CPU takes care of this. If tmigr_event::ignore is set then the CPU is returning from idle and take care of its timers.
If the next event expires in the future then the event node has been updated and there are no timer to expire right now.
kernel/time/timer_migration.c line 754
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+if (!tmc->online || tmc->remote || tmc->cpuevt.ignore || + now < tmc->cpuevt.nextevt.expires) { +raw_spin_unlock_irq (&tmc->lock); +return next;
Looking at the last condition where the timerqueue has been forwarded by a jiffy+, shouldn't we return _that_ values next instead of KTIME_MAX?
Quoted 26 lines
kernel/time/timer_migration.c new file +26
-0+ } + + tmc->remote = true; +WRITE_ONCE (tmc->wakeup,KTIME_MAX ); + +/* 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 +* the hierarchy and goes back to idle. +* +* timer base locks are dropped as fast as possible: After checking +* whether the remote CPU went 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. +*/
kernel/time/timer_migration.c line 790
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+local_irq_disable (); +timer_lock_remote_bases (cpu); +raw_spin_lock (&tmc->lock); + +/* +* When the 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 the CPU offline in the hierarchy. +* +* When the CPU is no longer idle, the CPU takes care of the timers and
If the CPU is no longer idle then it takes care of its timers and also of the timers in its hierarchy.
kernel/time/timer_migration.c line 794
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+* also of the timers in the path to the top. +* +* (See also section "Required event and timerqueue update after +* remote expiry" in the documentation at the top)
Perfect. The reasoning why it is safe to skip walk + expire timers, has been explained.
Quoted 31 lines
kernel/time/timer_migration.c new file +31
-0+*/ +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.remote = true; + +/* +* The update is done even when there is no 'new' global timer pending +* on the remote CPU (see section "Required event and timerqueue update +* after remote expiry" in the documentation at the top) +*/ +walk_groups (&tmigr_new_timer_up, &data, tmc); + + next = data.nextexp; + +unlock: + tmc->remote = false; +raw_spin_unlock_irq (&tmc->lock); + +return next; +} +
…
Quoted 9 lines
kernel/time/timer_migration.c new file +9
-0+/** +* tmigr_handle_remote() - Handle global timers of 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;
kernel/time/timer_migration.c line 892
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+ +if (tmigr_is_not_available (tmc)) +return ; + + data.childmask = tmc->childmask; + data.nextexp =KTIME_MAX ; + +/* +* NOTE: This is a doubled check because the migrator test will be done +* in tmigr_handle_remote_up() anyway. Keep this check to fasten the
s/fasten/speed up/
Quoted 42 lines
kernel/time/timer_migration.c new file +42
-0+* return when nothing has to be done. +*/ +if (!tmigr_check_migrator (tmc->tmgroup, tmc->childmask)) +return ; + + data.now =get_jiffies_update (&data.basej); + +/* +* Update @tmc->wakeup only at the end and do not reset @tmc->wakeup to +* KTIME_MAX. Even if tmc->lock is not held during the whole remote +* handling, tmc->wakeup is fine to be stale as it is called in +* interrupt context and tick_nohz_next_event() is executed in interrupt +* exit path only after processing the last pending interrupt. +*/ + +__walk_groups (&tmigr_handle_remote_up, &data, tmc); + +raw_spin_lock_irq (&tmc->lock); +WRITE_ONCE (tmc->wakeup, data.nextexp); +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 the 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; + +/* +* When there is a parent group and the CPU which triggered the +* hierarchy walk is not active, proceed the walk to reach the top level
kernel/time/timer_migration.c line 944
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+* group before reading the next_expiry value. +*/ +if (group->parent && !data->tmc_active) + goto out; + +/* +* 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. +*/
The lock is required on 32bit architectures to read the variable consistently with a concurrent writer. On 64bit the lock is not required because the read operation is not split and so is always consistent.
Quoted 37 lines
kernel/time/timer_migration.c new file +37
-0+if (IS_ENABLED (CONFIG_64BIT )) { + data->nextexp =READ_ONCE (group->next_expiry); +if (data->now >= data->nextexp) { + data->check = true; +return true; + } + }else { +raw_spin_lock (&group->lock); + data->nextexp = group->next_expiry; +if (data->now >= group->next_expiry) { + data->check = true; +raw_spin_unlock (&group->lock); +return true; + } +raw_spin_unlock (&group->lock); + } + +out: +/* Update of childmask for the next level */ + data->childmask = group->childmask; +return false; +} + +/** +* tmigr_requires_handle_remote() - Check the need of remote timer handling +* +* 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; +unsigned int ret =0 ; +unsigned long jif; + +if (tmigr_is_not_available (tmc)) +return ret;
kernel/time/timer_migration.c line 991
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+ + data.now =get_jiffies_update (&jif); + data.childmask = tmc->childmask; + data.nextexp =KTIME_MAX ; + data.tmc_active = !tmc->idle; + data.check = false; + +/* +* When the CPU is active, walking the hierarchy to check whether a +* remote expiry is required.
s/When/If
s/walking/walk
Quoted 9 lines
kernel/time/timer_migration.c new file +9
-0+* +* Check is done lockless as interrupts are disabled and @tmc->idle is +* set only by the local CPU. +*/ +if (!tmc->idle) { +__walk_groups (&tmigr_requires_handle_remote_up, &data, tmc); + +if (data.nextexp !=KTIME_MAX ) + ret =1 ;
kernel/time/timer_migration.c line 1010
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+ +return ret; + } + +/* +* When the CPU is idle, check whether the recalculation of @tmc->wakeup +* is required. @tmc->wakeup_recalc is set by a remote CPU which is +* about to go offline, was the last active CPU in the whole timer +* migration hierarchy and now delegates handling of the hierarchy to +* this CPU.
I'm failing here…
Quoted 18 lines
kernel/time/timer_migration.c new file +18
-0+* Racy lockless check is valid: +* - @tmc->wakeup_recalc is set by the remote CPU before it issues +* reschedule IPI. +* - As interrupts are disabled here this CPU will either observe +* @tmc->wakeup_recalc set before the reschedule IPI can be handled or +* it will observe it when this function is called again on return +* from handling the reschedule IPI. +*/ +if (tmc->wakeup_recalc) { +raw_spin_lock (&tmc->lock); + +__walk_groups (&tmigr_requires_handle_remote_up, &data, tmc); + +if (data.nextexp !=KTIME_MAX ) + ret =1 ; + +WRITE_ONCE (tmc->wakeup, data.nextexp); + tmc->wakeup_recalc = false;
kernel/time/timer_migration.c line 1039
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+raw_spin_unlock (&tmc->lock); + +return ret; + } + +/* +* When the CPU is idle and @tmc->wakeup is reliable, compare it with +* @data.now. On 64 bit it is valid to do this lockless. On 32 bit +* systems, holding the lock is required to get valid data on concurrent +* writers.
The wakeup value is reliable when the CPU is idle?
See the comments regarding 64bit.
Quoted 13 lines
kernel/time/timer_migration.c new file +13
-0+*/ +if (IS_ENABLED (CONFIG_64BIT )) { +if (data.now >=READ_ONCE (tmc->wakeup)) + ret =1 ; + }else { +raw_spin_lock (&tmc->lock); +if (data.now >= tmc->wakeup) + ret =1 ; +raw_spin_unlock (&tmc->lock); + } + +return ret; +}
…
Quoted 35 lines
kernel/time/timer_migration.c new file +35
-0+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 the child is no longer active */ +if (!data->childstate.active) + newstate.active &= ~childmask; + +if (newstate.migrator == childmask) { +/* +* Find a new migrator for the group, because the 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 */
kernel/time/timer_migration.c line 1152
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+ 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)) {
This one appears wrong, too. The curstate is not getting updated during retry.
Quoted 47 lines
kernel/time/timer_migration.c new file +47
-0+ newstate.state = curstate.state; + +/* +* Something changed in the child/parent group in the meantime, +* reread the state of the 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 the first global event which needs to be handled +*/ +if (data->nextexp !=KTIME_MAX ) { +WARN_ON_ONCE (group->parent); +/* +* Top level 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. +* +* Set wakeup_recalc of remote CPU, to make sure the complete +* idle hierarchy with enqueued timers is reevaluated. +*/ +if (!(this_cpu_ptr (&tmigr_cpu)->online)) { +struct tmigr_cpu *tmc =this_cpu_ptr (&tmigr_cpu); +unsigned int cpu =smp_processor_id (); +struct tmigr_cpu *tmc_resched; + + cpu =cpumask_any_but (cpu_online_mask, cpu);
kernel/time/timer_migration.c line 1209
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+ tmc_resched =per_cpu_ptr (&tmigr_cpu, cpu); + +raw_spin_unlock (&tmc->lock); + +raw_spin_lock (&tmc_resched->lock); + tmc_resched->wakeup_recalc = true; +raw_spin_unlock (&tmc_resched->lock); + +raw_spin_lock (&tmc->lock); +smp_send_reschedule (cpu);
This whole thing confuses me.
If the CPU goes offline, it needs to get removed from the migration hierarchy and this is it. Everything else is handled by the migrator. If the migrator is going offline then it needs wake a random CPU and make sure it takes the migrator role. I am confused by the whole ::wakeup and ::wakeup_recalc thingy.
kernel/time/timer_migration.c line 515
Quoted 5 lines
kernel/time/timer_migration.c new file +5
-0+ } + } + +return walk_done; +}
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 30/32] timers: Implement the hierarchical pull model
Quoted 45 lines · Sebastian Siewior
Quoted 6 lines · Anna-Maria Behnsen
kernel/time/timer_migration.c new file +0
-0@@ -0,0 +1,1636 @@…
Quoted 18 lines
kernel/time/timer_migration.c new file +18
-0+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; +kernel/time/timer_migration.c line 496
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+/* 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;This does not look right. If
group->migr_state != curstate.statethen
curstate.state = newstate.statedoes not make a difference since curstate is on stack. So this should become
Quoted 7 lines
| if (!atomic_try_cmpxchg(&group->migr_state, &curstate.state, newstate.state)) {
| curstate.state = newstate.state =atomic_read(&group->parent->migr_state);and now I question the existence of
tmigr_walk::groupstate. It does not match the comment for the struct saying it will be re-read if thecmpxchg()fails because it does not happen (at least here). Also why do we have it? Is it avoidatomic_read()and have it "cached"?
atomic_try_cmpxchg() updates curstate.state with the actual group->migr_state when those values do not match. So it is reread by atomic_try_cmpxchg() and still matches the description. (This at least tells the function description of atomic_try_cmpxchg()).
But beside of this, why do I want to update curstate.state with group->parent->migr_state when cmpxchg of this group wasn't successful yet or was it a copy paste error?
Quoted 8 lines
kernel/time/timer_migration.c line 500
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+ goto retry; + } + +if (group->parent && (walk_done == false)) {The group's parent doesn't change so it can be accessed lock-less. It is just that the topmost group has no parent so we need this check. I would move the
walk_donecheck to the left so it can be evaluated first.
Will change it.
Quoted 7 lines
kernel/time/timer_migration.c line 502
Quoted 2 lines
kernel/time/timer_migration.c new file +2
-0+ data->groupstate.state =atomic_read (&group->parent->migr_state); + data->childmask = group->childmask;We don't re-read in case the cmpxchg failed assuming someone else is updating the state. Wouldn't it make sense to read the childmask at top of the function from the child pointer. There is no need to keep around in the data pointer, right?
When we are in lvl0, then @child is NULL as the child is a tmigr_cpu and not a tmigr_group. This is the reason why I decided to store it inside the tmigr_walk struct.
Quoted 8 lines
Quoted 8 lines
kernel/time/timer_migration.c new file +8
-0+ } + +/* +* The group is active and the event will be ignored - the ignore flag is +* updated without holding the lock. In case the bit is set while +* another CPU already handles remote events, nothing happens, because +* it is clear that the CPU became active just in this moment, or in +* worst case the event is handled remote. Nothing to worry about.
Quoted 9 lines
kernel/time/timer_migration.c
Quoted 1 line
kernel/time/timer_migration.c new file +1
-0+*/ The CPU is becoming active, so is the group. The ignore flag for the group is updated lock less to reflect this. Another CPU might also set it true while becoming active. In worst case the migrator observes it too late and expires remotely timer belonging to this group. The lock is held while going idle (and setting ignore to false) so the state is not lost.
This is what I wanted to explain:
/*
* The group is active (again). The group event might be still queued
* into the parent group's timerqueue but can now be handled by the the
* migrator of this group. Therefore the ignore flag for the group event
* is updated to reflect this.
*
* The update of the ignore flag in the active path is done lock
* less. In worst case the migrator of the parent group observes the
* change too late and expires remotely timer belonging to this
* group. The lock is held while updating the ignore flag in idle
* path. So this state change will not be lost.
*/
Quoted 4 lines
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+ group->groupevt.ignore = true; + +return walk_done; +}
[...]
Quoted 23 lines
Quoted 9 lines
kernel/time/timer_migration.c new file +9
-0+ +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 ; +kernel/time/timer_migration.c line 750
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+ tmc =per_cpu_ptr (&tmigr_cpu, cpu); + +raw_spin_lock_irq (&tmc->lock); + +/* +* The remote CPU is offline or the CPU event does not has to be handled +* (the CPU is active or there is no longer an event to expire) or +* another CPU handles the CPU timers already or the next event was +* already expired - return! +*/ The comment is the english version of the C code below. The *why* is usually the thing we care about. This basically sums up to:
Quoted 7 lines
If remote CPU is offline then the timer events have been migrated away.
Iftmigr_cpu::remoteis set then another CPU takes care of this. Iftmigr_event::ignoreis set then the CPU is returning from idle and take care of its timers.
If the next event expires in the future then the event node has been updated and there are no timer to expire right now.
I'll change it. Thanks.
Quoted 7 lines
kernel/time/timer_migration.c line 754
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+if (!tmc->online || tmc->remote || tmc->cpuevt.ignore || + now < tmc->cpuevt.nextevt.expires) { +raw_spin_unlock_irq (&tmc->lock); +return next;Looking at the last condition where the timerqueue has been forwarded by a jiffy+, shouldn't we return _that_ values next instead of
KTIME_MAX?
No. Because the event is already queued into the hierarchy and the migrator takes care. If hiererachy is completely idle, the CPU which updated the event takes care. I'll add this to the comment above.
Quoted 39 lines
Quoted 26 lines
kernel/time/timer_migration.c new file +26
-0+ } + + tmc->remote = true; +WRITE_ONCE (tmc->wakeup,KTIME_MAX ); + +/* 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 +* the hierarchy and goes back to idle. +* +* timer base locks are dropped as fast as possible: After checking +* whether the remote CPU went 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. +*/ kernel/time/timer_migration.c line 790
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+local_irq_disable (); +timer_lock_remote_bases (cpu); +raw_spin_lock (&tmc->lock); + +/* +* When the 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 the CPU offline in the hierarchy. +* +* When the CPU is no longer idle, the CPU takes care of the timers and If the CPU is no longer idle then it takes care of its timers and also of the timers in its hierarchy.
Quoted 7 lines
kernel/time/timer_migration.c line 794
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+* also of the timers in the path to the top. +* +* (See also section "Required event and timerqueue update after +* remote expiry" in the documentation at the top) Perfect. The reasoning why it is safe to skip walk + expire timers, has been explained.
:)
Quoted 30 lines
Quoted 30 lines
kernel/time/timer_migration.c new file +30
-0+*/ +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.remote = true; + +/* +* The update is done even when there is no 'new' global timer pending +* on the remote CPU (see section "Required event and timerqueue update +* after remote expiry" in the documentation at the top) +*/ +walk_groups (&tmigr_new_timer_up, &data, tmc); + + next = data.nextexp; + +unlock: + tmc->remote = false; +raw_spin_unlock_irq (&tmc->lock); + +return next; +}
[...]
Quoted 41 lines
Quoted 14 lines
kernel/time/timer_migration.c new file +14
-0+/** +* tmigr_requires_handle_remote() - Check the need of remote timer handling +* +* 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; +unsigned int ret =0 ; +unsigned long jif; + +if (tmigr_is_not_available (tmc)) +return ret;kernel/time/timer_migration.c line 991
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+ + data.now =get_jiffies_update (&jif); + data.childmask = tmc->childmask; + data.nextexp =KTIME_MAX ; + data.tmc_active = !tmc->idle; + data.check = false; + +/* +* When the CPU is active, walking the hierarchy to check whether a +* remote expiry is required. s/When/If
s/walking/walkQuoted 13 lines
kernel/time/timer_migration.c new file +13
-0+* +* Check is done lockless as interrupts are disabled and @tmc->idle is +* set only by the local CPU. +*/ +if (!tmc->idle) { +__walk_groups (&tmigr_requires_handle_remote_up, &data, tmc); + +if (data.nextexp !=KTIME_MAX ) + ret =1 ; + +return ret; + } +
Quoted 8 lines
kernel/time/timer_migration.c line 1010
Quoted 6 lines
kernel/time/timer_migration.c new file +6
-0+/* +* When the CPU is idle, check whether the recalculation of @tmc->wakeup +* is required. @tmc->wakeup_recalc is set by a remote CPU which is +* about to go offline, was the last active CPU in the whole timer +* migration hierarchy and now delegates handling of the hierarchy to +* this CPU. I'm failing here…
If the CPU is idle, check whether the recalculation of @tmc->wakeup is required. @tmc->wakeup_recalc is set, when the last active CPU went offline. The last active CPU delegated the handling of the timer migration hierarchy to another (this) CPU by updating this flag and sending a reschedule.
Better?
Quoted 22 lines
Quoted 22 lines
kernel/time/timer_migration.c new file +22
-0+* Racy lockless check is valid: +* - @tmc->wakeup_recalc is set by the remote CPU before it issues +* reschedule IPI. +* - As interrupts are disabled here this CPU will either observe +* @tmc->wakeup_recalc set before the reschedule IPI can be handled or +* it will observe it when this function is called again on return +* from handling the reschedule IPI. +*/ +if (tmc->wakeup_recalc) { +raw_spin_lock (&tmc->lock); + +__walk_groups (&tmigr_requires_handle_remote_up, &data, tmc); + +if (data.nextexp !=KTIME_MAX ) + ret =1 ; + +WRITE_ONCE (tmc->wakeup, data.nextexp); + tmc->wakeup_recalc = false; +raw_spin_unlock (&tmc->lock); + +return ret; + }
Quoted 8 lines
kernel/time/timer_migration.c line 1039
Quoted 6 lines
kernel/time/timer_migration.c new file +6
-0+ +/* +* When the CPU is idle and @tmc->wakeup is reliable, compare it with +* @data.now. On 64 bit it is valid to do this lockless. On 32 bit +* systems, holding the lock is required to get valid data on concurrent +* writers. The wakeup value is reliable when the CPU is idle?
Yes and when wakeup_recalc is not set.
Quoted 1 line
See the comments regarding 64bit.
Updated it accordingly.
Quoted 57 lines
Quoted 14 lines
kernel/time/timer_migration.c new file +13
-0+*/ +if (IS_ENABLED (CONFIG_64BIT )) { +if (data.now >=READ_ONCE (tmc->wakeup)) + ret =1 ; + }else { +raw_spin_lock (&tmc->lock); +if (data.now >= tmc->wakeup) + ret =1 ; +raw_spin_unlock (&tmc->lock); + } + +return ret; +}…
Quoted 40 lines
kernel/time/timer_migration.c new file +40
-0+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 the child is no longer active */ +if (!data->childstate.active) + newstate.active &= ~childmask; + +if (newstate.migrator == childmask) { +/* +* Find a new migrator for the group, because the 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; + } + } + } +
Quoted 8 lines
kernel/time/timer_migration.c line 1152
Quoted 5 lines
kernel/time/timer_migration.c new file +5
-0+ newstate.seq++; + +WARN_ON_ONCE ((newstate.migrator !=TMIGR_NONE ) && !(newstate.active)); + +if (!atomic_try_cmpxchg (&group->migr_state, &curstate.state, newstate.state)) {This one appears wrong, too. The curstate is not getting updated during retry.
See the answer above.
Quoted 56 lines
Quoted 56 lines
kernel/time/timer_migration.c new file +56
-0+ newstate.state = curstate.state; + +/* +* Something changed in the child/parent group in the meantime, +* reread the state of the 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 the first global event which needs to be handled +*/ +if (data->nextexp !=KTIME_MAX ) { +WARN_ON_ONCE (group->parent); +/* +* Top level 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. +* +* Set wakeup_recalc of remote CPU, to make sure the complete +* idle hierarchy with enqueued timers is reevaluated. +*/ +if (!(this_cpu_ptr (&tmigr_cpu)->online)) { +struct tmigr_cpu *tmc =this_cpu_ptr (&tmigr_cpu); +unsigned int cpu =smp_processor_id (); +struct tmigr_cpu *tmc_resched; + + cpu =cpumask_any_but (cpu_online_mask, cpu); + tmc_resched =per_cpu_ptr (&tmigr_cpu, cpu); + +raw_spin_unlock (&tmc->lock); + +raw_spin_lock (&tmc_resched->lock); + tmc_resched->wakeup_recalc = true; +raw_spin_unlock (&tmc_resched->lock); + +raw_spin_lock (&tmc->lock);
Quoted 9 lines
kernel/time/timer_migration.c line 1209
Quoted 1 line
kernel/time/timer_migration.c new file +1
-0+smp_send_reschedule (cpu);This whole thing confuses me.
If the CPU goes offline, it needs to get removed from the migration hierarchy and this is it. Everything else is handled by the migrator. If the migrator is going offline then it needs wake a random CPU and make sure it takes the migrator role. I am confused by the whole ::wakeup and ::wakeup_recalc thingy.
wakeup_recalc is required to indicate, that the CPU was chosen as the new migrator CPU when the last active CPU in timer migration hierarchy went offline.
When a CPU goes idle and it is the last active CPU in the timer migration hierarchy, it has to make sure that it wakes up in time to handle the first event of the hierarchy. On the normal idle path this is not a problem as the value of the first event of the hierarchy is returned. But when an IRQ occurs on this idle CPU, the timers are revisited again. But then it is also required that the first event of the timer migration hierarchy is still considered, as the CPU cannot make sure another CPU will handle it. So the value is stored on idle path to tmc->wakeup. Otherwise every idle CPU has to walk the hierarchy again after an IRQ to make sure nothing is lost as the CPU doesn't know what happend in the meantime. I'm aware, that it is possible that several CPUs have the same wakeup value when there is no new first event and if the hierarchy is idle and some CPUs become active and go idle directly again. But if we want to cover this, we need a single point with the first event and with the last migrator information and I'm quite sure that it will not scale.
Hopefully this is now clear?
Quoted 7 lines
kernel/time/timer_migration.c line 515
Quoted 5 lines
kernel/time/timer_migration.c new file +5
-0+ } + } + +return walk_done; +}Sebastian
Thanks,
Anna-Maria
Anna-Maria Behnsen
maintainerRe: [PATCH v9 30/32] timers: Implement the hierarchical pull model
Quoted 16 lines · Anna-Maria Behnsen
Quoted 9 lines · Sebastian Siewior
On 2023-12-01 10:26:52 [+0100], Anna-Maria Behnsen wrote:
This whole thing confuses me.
If the CPU goes offline, it needs to get removed from the migration hierarchy and this is it. Everything else is handled by the migrator. If the migrator is going offline then it needs wake a random CPU and make sure it takes the migrator role. I am confused by the whole ::wakeup and ::wakeup_recalc thingy.
wakeup_recalcis required to indicate, that the CPU was chosen as the new migrator CPU when the last active CPU in timer migration hierarchy went offline.
Quoted 17 lines
When a CPU goes idle and it is the last active CPU in the timer migration hierarchy, it has to make sure that it wakes up in time to handle the first event of the hierarchy. On the normal idle path this is not a problem as the value of the first event of the hierarchy is returned. But when an IRQ occurs on this idle CPU, the timers are revisited again. But then it is also required that the first event of the timer migration hierarchy is still considered, as the CPU cannot make sure another CPU will handle it. So the value is stored on idle path to
tmc->wakeup. Otherwise every idle CPU has to walk the hierarchy again after an IRQ to make sure nothing is lost as the CPU doesn't know what happend in the meantime. I'm aware, that it is possible that several CPUs have the same wakeup value when there is no new first event and if the hierarchy is idle and some CPUs become active and go idle directly again. But if we want to cover this, we need a single point with the first event and with the last migrator information and I'm quite sure that it will not scale.
I forgot to explain one thing: A CPU does remote expiry and it is possible that the CPU is idle in timer migration hierarchy. Therefore the wakeup value is updated at the end of remote handling with the new first event of the timer migration hierarchy unconditionally.
Thanks,
Anna-Maria
Re: [PATCH v9 30/32] timers: Implement the hierarchical pull model
In reply to Anna-Maria BehnsenQuoted 55 lines · Anna-Maria Behnsen
Quoted 45 lines · Sebastian Siewior
Quoted 6 lines · Anna-Maria Behnsen
kernel/time/timer_migration.c new file +0
-0@@ -0,0 +1,1636 @@…
Quoted 18 lines
kernel/time/timer_migration.c new file +18
-0+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; +kernel/time/timer_migration.c line 496
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+/* 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;This does not look right. If
group->migr_state != curstate.statethen
curstate.state = newstate.statedoes not make a difference since curstate is on stack. So this should become
Quoted 7 lines
| if (!atomic_try_cmpxchg(&group->migr_state, &curstate.state, newstate.state)) {
| curstate.state = newstate.state =atomic_read(&group->parent->migr_state);and now I question the existence of
tmigr_walk::groupstate. It does not match the comment for the struct saying it will be re-read if thecmpxchg()fails because it does not happen (at least here). Also why do we have it? Is it avoidatomic_read()and have it "cached"?
Quoted 4 lines
atomic_try_cmpxchg()updates curstate.state with the actualgroup->migr_statewhen those values do not match. So it is reread byatomic_try_cmpxchg()and still matches the description. (This at least tells the function description ofatomic_try_cmpxchg()).
Ach. Indeed. That part slipped my mind. Could still replace it with:
newstate = curstate
to match the assignment at the top of the function? Or do something like:
| childmask = data->childmask; | curstate = data->groupstate; | retry: | newstate = curstate; | | walk_done = true;
…
| if (!atomic_try_cmpxchg(&group->migr_state, &curstate.state, newstate.state))
| goto retry;
So gcc can save a branch and recycle the upper the cooking the code. gcc-13 does not recognise this, clang-16 does.
Quoted 3 lines
But beside of this, why do I want to update curstate.state with
group->parent->migr_statewhen cmpxchg of this group wasn't successful yet or was it a copy paste error?
It was an error.
Quoted 11 lines
Quoted 7 lines
kernel/time/timer_migration.c line 502
Quoted 2 lines
kernel/time/timer_migration.c new file +2
-0+ data->groupstate.state =atomic_read (&group->parent->migr_state); + data->childmask = group->childmask;We don't re-read in case the cmpxchg failed assuming someone else is updating the state. Wouldn't it make sense to read the childmask at top of the function from the child pointer. There is no need to keep around in the data pointer, right?
When we are in lvl0, then @child is NULL as the child is a
tmigr_cpuand not atmigr_group. This is the reason why I decided to store it inside thetmigr_walkstruct.
But it supposed to be group->migr_state for the cmpxchg. So considering the previous bit, why not:
| childmask = data->childmask;
| curstate = atomic_read(&group->migr_state);
|
| do {
| newstate = curstate;
| walk_done = true;
|
| if (newstate.migrator == TMIGR_NONE) {
| newstate.migrator = childmask;
|
| /* Changes need to be propagated */
| walk_done = false;
| }
|
| newstate.active |= childmask;
|
| newstate.seq++;
|
| } while (!atomic_try_cmpxchg(&group->migr_state, &curstate.state, newstate.state));this seems nice.
Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
kernel/time/timer_migration.c new file +8
-0+ } + +/* +* The group is active and the event will be ignored - the ignore flag is +* updated without holding the lock. In case the bit is set while +* another CPU already handles remote events, nothing happens, because +* it is clear that the CPU became active just in this moment, or in +* worst case the event is handled remote. Nothing to worry about. Quoted 9 lines
kernel/time/timer_migration.c
Quoted 1 line
kernel/time/timer_migration.c new file +1
-0+*/ The CPU is becoming active, so is the group. The ignore flag for the group is updated lock less to reflect this. Another CPU might also set it true while becoming active. In worst case the migrator observes it too late and expires remotely timer belonging to this group. The lock is held while going idle (and setting ignore to false) so the state is not lost.
Quoted 5 lines
This is what I wanted to explain:
/*
* The group is active (again). The group event might be still queued
* into the parent group's timerqueue but can now be handled by the the
s/the$@@
Quoted 4 lines
* migrator of this group. Therefore the ignore flag for the group event
* is updated to reflect this.
*
* The update of the ignore flag in the active path is done lock
lockless
Quoted 2 lines
* less. In worst case the migrator of the parent group observes the
* change too late and expires remotely timer belonging to this
a timer?
Quoted 8 lines
* group. The lock is held while updating the ignore flag in idle
* path. So this state change will not be lost.
*/Quoted 4 lines
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+ group->groupevt.ignore = true; + +return walk_done; +}
…
Quoted 11 lines
Quoted 7 lines
kernel/time/timer_migration.c line 754
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+if (!tmc->online || tmc->remote || tmc->cpuevt.ignore || + now < tmc->cpuevt.nextevt.expires) { +raw_spin_unlock_irq (&tmc->lock); +return next;Looking at the last condition where the timerqueue has been forwarded by a jiffy+, shouldn't we return _that_ values next instead of
KTIME_MAX?No. Because the event is already queued into the hierarchy and the migrator takes care. If hiererachy is completely idle, the CPU which updated the event takes care. I'll add this to the comment above.
So another CPU took care of it and we set tmc->wakeup to KTIME_MAX…
One confusing part is that this return value (if not aborted early but after completing this function) is used to set tmc->wakeup based on the next pending timer for the CPU that was expired remotely.
We could have expired four CPUs and the next timer of the last CPU may not be the earliest timer for the fist CPU in the group.
And this fine because it can be stale and only valid if the CPU goes idle?
Quoted 9 lines
Quoted 8 lines
kernel/time/timer_migration.c line 1010
Quoted 6 lines
kernel/time/timer_migration.c new file +6
-0+/* +* When the CPU is idle, check whether the recalculation of @tmc->wakeup +* is required. @tmc->wakeup_recalc is set by a remote CPU which is +* about to go offline, was the last active CPU in the whole timer +* migration hierarchy and now delegates handling of the hierarchy to +* this CPU. I'm failing here…
Quoted 7 lines
If the CPU is idle, check whether the recalculation of @tmc->wakeup is required. @tmc->wakeup_recalc is set, when the last active CPU went offline. The last active CPU delegated the handling of the timer migration hierarchy to another (this) CPU by updating this flag and sending a reschedule.
Better?
So the last CPU going offline had to be the migrator because otherwise it wouldn't matter?
…
Quoted 7 lines
Quoted 5 lines
kernel/time/timer_migration.c
Quoted 2 lines
kernel/time/timer_migration.c new file +2
-0+ +if (!atomic_try_cmpxchg (&group->migr_state, &curstate.state, newstate.state)) {This one appears wrong, too. The curstate is not getting updated during retry.
See the answer above.
Yes, and I think the do { } while should work here, too.
Quoted 40 lines
Quoted 30 lines
Quoted 30 lines
kernel/time/timer_migration.c new file +30
-0+* data->nextexp was set by tmigr_update_events() and contains the +* expiry of the first global event which needs to be handled +*/ +if (data->nextexp !=KTIME_MAX ) { +WARN_ON_ONCE (group->parent); +/* +* Top level 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. +* +* Set wakeup_recalc of remote CPU, to make sure the complete +* idle hierarchy with enqueued timers is reevaluated. +*/ +if (!(this_cpu_ptr (&tmigr_cpu)->online)) { +struct tmigr_cpu *tmc =this_cpu_ptr (&tmigr_cpu); +unsigned int cpu =smp_processor_id (); +struct tmigr_cpu *tmc_resched; + + cpu =cpumask_any_but (cpu_online_mask, cpu); + tmc_resched =per_cpu_ptr (&tmigr_cpu, cpu); + +raw_spin_unlock (&tmc->lock); + +raw_spin_lock (&tmc_resched->lock); + tmc_resched->wakeup_recalc = true; +raw_spin_unlock (&tmc_resched->lock); + +raw_spin_lock (&tmc->lock);Quoted 9 lines
kernel/time/timer_migration.c line 1209
Quoted 1 line
kernel/time/timer_migration.c new file +1
-0+smp_send_reschedule (cpu);This whole thing confuses me.
If the CPU goes offline, it needs to get removed from the migration hierarchy and this is it. Everything else is handled by the migrator. If the migrator is going offline then it needs wake a random CPU and make sure it takes the migrator role. I am confused by the whole ::wakeup and ::wakeup_recalc thingy.
Quoted 3 lines
wakeup_recalcis required to indicate, that the CPU was chosen as the new migrator CPU when the last active CPU in timer migration hierarchy went offline.
Aha! I suspected this. So this is more like need_new_migrator.
…
Quoted 1 line
Hopefully this is now clear?
yes.
Quoted 3 lines
Thanks,
Anna-Maria
Sebastian
Re: [PATCH v9 30/32] timers: Implement the hierarchical pull model
In reply to Anna-Maria BehnsenQuoted 6 lines · Anna-Maria Behnsen
kernel/time/timer_migration.c new file +0
-0@@ -0,0 +1,1636 @@
…
Quoted 5 lines
kernel/time/timer_migration.c new file +5
-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 ();
kernel/time/timer_migration.c line 1585
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+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.
NUMA
kernel/time/timer_migration.c line 1591
Quoted 6 lines
kernel/time/timer_migration.c new file +6
-0+* +* 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. +*/
We cannot rely on cpumask_of_node() because it only works for online CPUs.
Quoted 4 lines
kernel/time/timer_migration.c new file +4
-0+ 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),
kernel/time/timer_migration.c line 1605
Quoted 10 lines
kernel/time/timer_migration.c new file +10
-0+ ilog2(TMIGR_CHILDREN_PER_GROUP)); + +/* Calculate the extra levels to connect all nodes */ + nodelvl =DIV_ROUND_UP (order_base_2 (nnodes), +ilog2 (TMIGR_CHILDREN_PER_GROUP )); + + tmigr_hierarchy_levels = cpulvl + nodelvl; + +/* +* If a numa node spawns more than one CPU level group then the next
NUMA
kernel/time/timer_migration.c line 1607
Quoted 2 lines
kernel/time/timer_migration.c new file +2
-0+* 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
NUMA
kernel/time/timer_migration.c line 1609
Quoted 2 lines
kernel/time/timer_migration.c new file +2
-0+* this information for the setup code to decide when node matching is +* not longer required.
s/not longer/no longer ?
Quoted 33 lines
kernel/time/timer_migration.c new file +27
-0+*/ + 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_CHILDREN_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; + +return 0 ; + +err: +pr_err ("Timer migration setup failed\n" ); +return ret; +} +late_initcall (tmigr_init);kernel/time/timer_migration.h new file +0
-0
kernel/time/timer_migration.h line 6
Quoted 6 lines
kernel/time/timer_migration.h new file +6
-0@@ -0,0 +1,144 @@ +/* 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_CHILDREN_PER_GROUP 8
BUILD_BUG_ON_NOT_POWER_OF_2(TMIGR_CHILDREN_PER_GROUP)
Maybe in the .c file.
Quoted 8 lines
kernel/time/timer_migration.h new file +8
-0+/** +* 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 {
kernel/time/timer_migration.h line 25
Quoted 10 lines
kernel/time/timer_migration.h new file +10
-0+struct timerqueue_node nextevt; +unsigned int cpu; +bool ignore; +}; + +/** +* struct tmigr_group - timer migration hierarchy group +* @lock: Lock protecting the event information and group hierarchy +* information during setup +* @migr_state: State of the group (see union tmigr_state)
So the lock does not protect migr_state? Mind moving it a little down the road? Not only would it be more obvious what is protected by the lock but it would also move migr_state in another/ later cache line.
kernel/time/timer_migration.h line 36
Quoted 11 lines
kernel/time/timer_migration.h new file +11
-0+* @parent: Pointer to the parent group +* @groupevt: Next event of the group which is only used when the +* group is !active. The group event is then queued into +* the parent timer queue. +* Ignore bit of @groupevt is set when the group is active. +* @next_expiry: Base monotonic expiry time of the next event of the +* group; It is used for the racy lockless check whether a +* remote expiry is required; it is always reliable +* @events: Timer queue for child events queued in the group +* @childmask: childmask of the group in the parent group; is set +* during setup and will never change; could be read
_can_ be read lockless.
kernel/time/timer_migration.h line 43
Quoted 7 lines
kernel/time/timer_migration.h new file +7
-0+* lockless +* @level: Hierarchy level of the group; Required during setup +* @list: List head that is added to the 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;
NUMA
(as long as the group level is per NUMA node).
kernel/time/timer_migration.h line 46
Quoted 3 lines
kernel/time/timer_migration.h new file +3
-0+* 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
… to make sure CPU and group information is NUMA local. This is true until the top most hierarchy level (level < tmigr_crossnode_level).
Quoted 14 lines
kernel/time/timer_migration.h new file +14
-0+* @num_children: Counter of group children to make sure the group is only +* filled with TMIGR_CHILDREN_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;
kernel/time/timer_migration.h line 70
Quoted 10 lines
kernel/time/timer_migration.h new file +10
-0+int numa_node; +unsigned int num_children; +}; + +/** +* struct tmigr_cpu - timer migration per CPU group +* @lock: Lock protecting the tmigr_cpu group information +* @online: Indicates whether the CPU is online; In deactivate path +* it is required to know whether the migrator in the top +* level group is on the way to go offline when a timer is
level group, which is to be set offline, while a timer is pending.
kernel/time/timer_migration.h line 72
Quoted 2 lines
kernel/time/timer_migration.h new file +2
-0+* pending. Then another online CPU needs to be rescheduled +* to make sure the timers are handled properly;
Then another online CPU needs to be notified to take over the migrator role.
The "rescheduled" part sounds like the current implementation.
kernel/time/timer_migration.h line 76
Quoted 4 lines
kernel/time/timer_migration.h new file +4
-0+* Furthermore the information is required in CPU hotplug +* path as the CPU is able to go idle before the timer +* migration hierarchy hotplug AP is reached. During this +* phase, the CPU has to handle the global timers by its
s/by its own/on its own/
kernel/time/timer_migration.h line 77
Quoted 1 line
kernel/time/timer_migration.h new file +1
-0+* own and does not act as a migrator.
s/does not/must not
kernel/time/timer_migration.h line 80
Quoted 3 lines
kernel/time/timer_migration.h new file +3
-0+* @idle: Indicates whether the CPU is idle in the timer migration +* hierarchy +* @remote: Is set when timers of the CPU are expired remote
s/remote/remotely
kernel/time/timer_migration.h line 83
Quoted 3 lines
kernel/time/timer_migration.h new file +3
-0+* @wakeup_recalc: Indicates, whether a recalculation of the @wakeup value +* is required. It is only used when the CPU is marked idle +* in the timer migration hierarchy.
What does It' refer to? Is it wakeup_recalc' or `wakeup' ?
kernel/time/timer_migration.h line 88
Quoted 5 lines
kernel/time/timer_migration.h new file +5
-0+* @tmgroup: Pointer to the parent group +* @childmask: childmask of tmigr_cpu in the 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 in the idle path; it is only
is used in the idle path only (what is the idle path (probably obvious))
kernel/time/timer_migration.h line 90
Quoted 2 lines
kernel/time/timer_migration.h new file +2
-0+* valid, when @wakeup_recalc is not set +* @cpuevt: CPU event which could be queued into the parent group
I don't know why but it feels like s/queued/enqueued/g But it might be a British vs American thing.
Sebastian
Anna-Maria Behnsen
maintainerRe: [PATCH v9 30/32] timers: Implement the hierarchical pull model
Quoted 1 line · Sebastian Siewior
On 2023-12-01 10:26:52 [+0100], Anna-Maria Behnsen wrote:
[...]
Quoted 15 lines
kernel/time/timer_migration.h line 6
Quoted 12 lines
kernel/time/timer_migration.h new file +6
-0@@ -0,0 +1,144 @@ +/* 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_CHILDREN_PER_GROUP 8
BUILD_BUG_ON_NOT_POWER_OF_2(TMIGR_CHILDREN_PER_GROUP)
Quoted 2 lines
Maybe in the .c file.
in tmigr_init() ?
Quoted 12 lines
Quoted 12 lines
kernel/time/timer_migration.h new file +12
-0+/** +* 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; +bool ignore; +};
Quoted 8 lines
kernel/time/timer_migration.h line 25
Quoted 6 lines
kernel/time/timer_migration.h new file +6
-0+ +/** +* struct tmigr_group - timer migration hierarchy group +* @lock: Lock protecting the event information and group hierarchy +* information during setup +* @migr_state: State of the group (see union tmigr_state) So the lock does not protect
migr_state?
Right - this is not required due to the atomic cmpxchg and seqence counter.
Quoted 4 lines
Mind moving it a little down the road? Not only would it be more obvious what is protected by the lock but it would also move
migr_statein another/ later cache line.
Where do you want me to move it? Switch places of lock and migr_state? When I put it to another place, I would generate holes. A general
question: Is it required to have a look at the struct with pahole also with LOCKDEP enabled? If yes, lock should stay at the first position.
[...]
Quoted 4 lines
kernel/time/timer_migration.h line 90
Quoted 1 line
kernel/time/timer_migration.h new file +1
-0+* @cpuevt: CPU event which could be queued into the parent group I don't know why but it feels like s/queued/enqueued/g But it might be a British vs American thing.
I think this queued/enqueued is not used consistent all over the place (in my patchset). But I'm also not a native speaker and not sure which is the proper one :). Nevertheless, I'll change it.
Thanks,
Anna-Maria
Re: [PATCH v9 30/32] timers: Implement the hierarchical pull model
Quoted 11 lines · Anna-Maria Behnsen
Quoted 7 lines · Sebastian Siewior
kernel/time/timer_migration.h line 6
Quoted 2 lines
kernel/time/timer_migration.h new file +2
-0+/* Per group capacity. Must be a power of 2! */ +#define TMIGR_CHILDREN_PER_GROUP 8
BUILD_BUG_ON_NOT_POWER_OF_2(TMIGR_CHILDREN_PER_GROUP)Maybe in the .c file.
in
tmigr_init()?
Yeah why not. It is used there for the init of the structs.
Quoted 16 lines
Quoted 7 lines
Quoted 5 lines
+/**
+ * structtmigr_group- timer migration hierarchy group
+ * @lock: Lock protecting the event information and group hierarchy
+ * information during setup
+ * @migr_state: State of the group (see uniontmigr_state)So the lock does not protect
migr_state?Right - this is not required due to the atomic cmpxchg and seqence counter.
Quoted 4 lines
Mind moving it a little down the road? Not only would it be more obvious what is protected by the lock but it would also move
migr_statein another/ later cache line.
Quoted 4 lines
Where do you want me to move it? Switch places of lock and
migr_state? When I put it to another place, I would generate holes. A general
question: Is it required to have a look at the struct with pahole also with LOCKDEP enabled? If yes, lock should stay at the first position.
Maybe something like:
| struct tmigr_group {
| raw_spinlock_t lock; /* 0 4 */
|
| /* XXX 4 bytes hole, try to pack */
|
| struct tmigr_group * parent; /* 8 8 */
| struct tmigr_event groupevt __attribute__((__aligned__(8))); /* 16 40 */
|
| /* XXX last struct has 3 bytes of padding */
|
| u64 next_expiry; /* 56 8 */
| /* --- cacheline 1 boundary (64 bytes) --- */
| struct timerqueue_head events; /* 64 16 */
| atomic_t migr_state; /* 80 4 */
| unsigned int level; /* 84 4 */
| int numa_node; /* 88 4 */
| unsigned int num_children; /* 92 4 */
| u8 childmask; /* 96 1 */
|
| /* XXX 7 bytes hole, try to pack */
|
| struct list_head list; /* 104 16 */Starting with lock isn't bad as you see everything from here is protected by lock. If it makes sense you could start with list so that the container_of() becomes a NOP.
I wouldn't make lockdep a thing and assume it is off. Also, I would assume the architecture is 64bit.
However with lockdep enabled it becomes:
| struct tmigr_group {
| raw_spinlock_t lock; /* 0 64 */
| /* --- cacheline 1 boundary (64 bytes) --- */
| struct tmigr_group * parent; /* 64 8 */
| struct tmigr_event groupevt __attribute__((__aligned__(8))); /* 72 40 */
|
| /* XXX last struct has 3 bytes of padding */
|
| u64 next_expiry; /* 112 8 */
| struct timerqueue_head events; /* 120 16 */
| /* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */
| atomic_t migr_state; /* 136 4 */
| unsigned int level; /* 140 4 */
| int numa_node; /* 144 4 */
| unsigned int num_children; /* 148 4 */
| u8 childmask; /* 152 1 */
|
| /* XXX 7 bytes hole, try to pack */
|
| struct list_head list; /* 160 16 */
| } __attribute__((__aligned__(8)));so it didn't change much.
I shuffled it a bit and everything after migr_state is read only. I don't think looking at pahole is required but in your case it makes sense to put the locked section into a separate cache line vs migr_state variable. It doesn't cost much.
You can decide if it is worth to move childmask after the lock so you so you avoid the 7 byte hole at the end. I wouldn't do it to satisfy pahole here. If it makes sense, doesn't hurt/ confuse why not.
You would consider the pahole output more on a structure like dentry which is used a _lot_. So saving 4 bytes would mean save a megabyte or ten in the end.
Quoted 4 lines
Thanks,
Anna-Maria
Sebastian
Anna-Maria Behnsen
maintainerPATCH v9 31/32 timer_migration: Add tracepoints
In reply to Anna-Maria BehnsenThe timer pull logic needs proper debugging aids. Add tracepoints so the hierarchical idle machinery can be diagnosed.
---
v9: Add tmigr_cpu_new_timer_idle tracepoint
v8: Add wakeup value to tracepoints
--- include/trace/events/timer_migration.h | 297 +++++++++++++++++++++++++ kernel/time/timer_migration.c | 26 +++ 2 files changed, 323 insertions(+) create mode 100644 include/trace/events/timer_migration.h
include/trace/events/timer_migration.h new file +297 -0
@@ -0,0 +1,297 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#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) +__field ( u64, wakeup) + ), + +TP_fast_assign ( + __entry->cpu = tmc->cpuevt.cpu; + __entry->parent = tmc->tmgroup; + __entry->wakeup = tmc->wakeup; + ), + +TP_printk ("cpu=%d parent=%p wakeup=%llu" , __entry->cpu, __entry->parent, __entry->wakeup) +); + +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) +); + +DECLARE_EVENT_CLASS (tmigr_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) +__field ( u64, wakeup) + ), + +TP_fast_assign ( + __entry->cpu = tmc->cpuevt.cpu; + __entry->parent = tmc->tmgroup; + __entry->nextevt = nextevt; + __entry->wakeup = tmc->wakeup; + ), + +TP_printk ("cpu=%d parent=%p nextevt=%llu wakeup=%llu" , + __entry->cpu, __entry->parent, __entry->nextevt, __entry->wakeup) +); + +DEFINE_EVENT (tmigr_idle, tmigr_cpu_idle, + + TP_PROTO(struct tmigr_cpu *tmc, u64 nextevt), + +TP_ARGS (tmc, nextevt) +); + +DEFINE_EVENT (tmigr_idle, tmigr_cpu_new_timer_idle, + + TP_PROTO(struct tmigr_cpu *tmc, u64 nextevt), + +TP_ARGS (tmc, 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>
kernel/time/timer_migration.c +26 -0
@@ -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
@@ -511,6 +514,8 @@ static bool tmigr_active_up(struct tmigr_group *group,*/ group->groupevt.ignore = true; +trace_tmigr_group_set_cpu_active (group, newstate, childmask); +return walk_done; }
@@ -521,6 +526,8 @@ static void __tmigr_cpu_activate(struct tmigr_cpu *tmc) data.childmask = tmc->childmask; data.groupstate.state =atomic_read (&tmc->tmgroup->migr_state); +trace_tmigr_cpu_active (tmc); + tmc->cpuevt.ignore = true;WRITE_ONCE (tmc->wakeup,KTIME_MAX ); tmc->wakeup_recalc = false;
@@ -688,6 +695,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);
@@ -721,6 +731,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 = false; data.remote = false;
@@ -754,6 +766,8 @@ static u64 tmigr_handle_remote_cpu(unsigned int cpu, u64 now,return next; } +trace_tmigr_handle_remote_cpu (tmc); + tmc->remote = true;WRITE_ONCE (tmc->wakeup,KTIME_MAX );
@@ -838,6 +852,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
@@ -1101,6 +1116,7 @@ u64 tmigr_cpu_new_timer(u64 nextexp)*/ WRITE_ONCE (tmc->wakeup, ret); +trace_tmigr_cpu_new_timer_idle (tmc, nextexp);raw_spin_unlock (&tmc->lock);return ret; }
@@ -1210,6 +1226,8 @@ static bool tmigr_inactive_up(struct tmigr_group *group, } } +trace_tmigr_group_set_cpu_inactive (group, newstate, childmask); +return walk_done; }
@@ -1264,6 +1282,7 @@ u64 tmigr_cpu_deactivate(u64 nextexp)*/ WRITE_ONCE (tmc->wakeup, ret); +trace_tmigr_cpu_idle (tmc, nextexp);raw_spin_unlock (&tmc->lock);return ret; }
@@ -1376,6 +1395,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; }
@@ -1393,6 +1413,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 * the new parent as well. Inactive children are already marked inactive
@@ -1474,6 +1496,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 {
@@ -1541,6 +1565,7 @@ static int tmigr_cpu_online(unsigned int cpu)WRITE_ONCE (tmc->wakeup,KTIME_MAX ); }raw_spin_lock_irq (&tmc->lock); +trace_tmigr_cpu_online (tmc);if (timer_base_is_idle ()) tmc->idle = true;else
@@ -1563,6 +1588,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 ;
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerPATCH v9 32/32 timers: Always queue timers on the local CPU
In reply to Anna-Maria BehnsenThe 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.
---
v9:
- Update to the changes of the preceding patches
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 | 46 +++++++++++++++++++++---------------------- 2 files changed, 26 insertions(+), 34 deletions(-)
include/linux/timer.h +4 -10
@@ -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
kernel/time/timer.c +22 -24
80 lines not on this page · raw message
Signature · 1 line
2.39.2
Anna-Maria Behnsen
maintainerQuoted 6 lines · Anna-Maria Behnsen
Hi,
the queue grew because of some additional cleanup patches and splitting already existing patches to make review easier. Several minor things had to be reworked. For all people who did testing, I would be really happy if you could test v9 as well!
bigeasy triggered a warning during boot. So I'll go and fix it and will send a v10 when I'm done.
Thanks,
Anna-Maria