Skip to content

Stop the idle tick on polling idle on nohz_full CPUs

Marco Crivellari lkml 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest5 patches 3 repliesIn review
  2. v24 patches 9 repliesSuperseded
  3. v13 patches 7 repliesSuperseded

Changes in v3 from the cover letter

- tick_nohz_user_enter(); moved outside of CT_STATE_USER so that also guest can benefit from this.

- __tick_nohz_user_enter() has been moved after tick_nohz_full_stop_tick(), so that the same #ifdef is used.

- P2 now uses guard(irqsave)() instead of irq_save(flags) removing also the "goto".

- P4, Documentation, now is P5.

- Introduced P4: reset got_idle_tick on tick_nohz_idle_enter().

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5tick/nohz: Re-evaluate the tick on return to user spaceinclude/linux/tick.h kernel/context_tracking.c kernel/time/tick-sched.c+42 -01 reply
2/5sched/idle: Don't handle polling idle as dynticks idlekernel/sched/idle.c kernel/time/tick-sched.c+24 -130 replies
3/5tick/nohz: Remove tick_nohz_idle_restart_tick()include/linux/tick.h kernel/time/tick-sched.c+0 -190 replies
4/5tick/nohz: Reset got_idle_tick when entering dynticks idlekernel/time/tick-sched.c+2 -01 reply
5/5Documentation: no_hz: Add tick stop in polling idleDocumentation/timers/no_hz.rst+2 -00 replies

Reviews trailers per patch

All 5 patches have a Reviewed-by or Acked-by.

PatchFrederic WeisbeckerAaron Tomlin
SeriesT-b
1/5R-b
2/5R-b
3/5R-b
4/5R-b
5/5R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH v3 0/5] Stop the idle tick on polling idle on nohz_full CPUs

Hi,

On nohz_full CPUs booted with idle=poll, the tick interrupts the task once after every wakeup from idle. do_idle() restarts the tick before polling, and on wakeup tick_nohz_idle_exit() only re-evaluates the tick if it is stopped, so it keeps running and fires in userspace before tick_nohz_irq_exit() stops it. Without idle=poll this doesn't happen:
since commit a5183862e76f ("tick/nohz: Conditionally restart tick on idle exit") a nohz_full CPU leaves idle with the tick still stopped.

Marcelo Tosatti proposed stopping the tick on polling idle entry:
https://lore.kernel.org/all/aQiWfnnSzxsnwa2o@tpad/

Following Frederic's review, this series does it only on nohz_full CPUs, and handles on the target CPU the cases where stopping the tick in polling idle would leave a timer unprogrammed.

[ P1 ] tick/nohz: Re-evaluate the tick on return to user space
On nohz_full CPUs the tick is re-evaluated on IRQ exit and on idle exit.

This is not enough on return to user space. If the tick is left running in the kernel, for example because a tick dependency was cleared (tick_nohz_dep_clear*() don't kick the CPU) or a timer that kept it running was cancelled, it fires at least once in userspace before tick_nohz_irq_exit() stops it.

Stop it from context tracking right before resuming userspace. When the tick is already stopped, the common case, this costs a few flag tests on each return to userspace.

[ P2 ] Don't handle polling idle as dynticks idle

TS_FLAG_INIDLE tells nohz whether the tick is managed by the idle loop or on IRQ exit. With idle=poll it is set while polling, but cpu_idle_poll() doesn't run the idle loop again on interrupts: a nohz_full kick or the expiry of a stopped tick isn't re-evaluated until something sets TIF_NEED_RESCHED. The tick is therefore restarted before polling, and on nohz_full CPUs it then keeps running after the wake-up and interrupts userspace.

Enter dynticks idle only on the cpuidle path, from cpuidle_idle_call(), and leave it before cpu_idle_poll(). While polling, the tick is then managed on IRQ exit as for a running task: on nohz_full CPUs it stays stopped if possible, and nothing is left to do on wake-up, so the task doesn't pay for a tick stop after waking up. On other CPUs, leaving dynticks idle restarts the tick as before.

[ P3 ] tick/nohz: Remove tick_nohz_idle_restart_tick()

Polling idle now leaves dynticks idle through tick_nohz_idle_exit() before cpu_idle_poll(), which restarts the tick where needed. tick_nohz_idle_restart_tick() has no users left: remove it.

[ P4 ] tick/nohz: Reset got_idle_tick when entering dynticks idle

A tick firing after the last tick_nohz_idle_got_tick() and before tick_nohz_idle_exit() leaves got_idle_tick set, and the next cpuidle_idle_call() then assumes that a later dynticks idle period was ended by the tick. Now that the CPU leaves dynticks idle before polling, this can also happen within a single idle trip when it switches between cpuidle and polling, e.g. when it polls because the tick broadcast has expired.

Reset got_idle_tick when entering dynticks idle, so that it only reports the ticks of the current period.

[ P5 ] Documentation: no_hz: Add tick stop in polling idle

Document the above in the idle=poll item of "Known Issues".

With big effort on the Claude side, I made a test suite to help testing the work:

https://github.com/DispatchCode/nohz-full-polling-idle-tick-test-suite

The following is a short summary of the results.

On x86_64 guest, HZ=1000, nohz_full=2-3 idle=poll), a task alternating 100us sleeps with 10ms of userspace work, 200 times:

  - ticks in userspace:              200/200 -> 0/200
  - ticks while polling:             35 -> 0
  - housekeeping CPUs:               unchanged (tick runs while polling)
  - P2 without the kick change:      15 of 20 timers enqueued from an

interrupt on the polling CPU lost
- P2 without the stopped tick fix: the second of two queued timers up to 1.9s late
Tick dependencies (POSIX CPU timers, two tasks on the CPU) and CPU hotplug were also checked.

Thanks,

Marco

---
Changes in v3:
- tick_nohz_user_enter(); moved outside of CT_STATE_USER so that also guest can benefit from this.

- __tick_nohz_user_enter() has been moved after tick_nohz_full_stop_tick(), so that the same #ifdef is used.

- P2 now uses guard(irqsave)() instead of irq_save(flags) removing also the "goto".

- P4, Documentation, now is P5.

- Introduced P4: reset got_idle_tick on tick_nohz_idle_enter().

Link to v2: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- P1: Only for re-evaluate the tick on return to user space

- P2: Changed as suggested by Frederic: TS_FLAG_INIDLE is only set when the CPU goes idle and not in polling idle; tick_nohz_idle_enter() is called on every idle loop and runs with interrupt disabled. The tick is not restarted before polling idle, so tick_nohz_idle_restart_tick() has been removed.

- P3: Remove tick_nohz_idle_restart_tick(), now unused.

Link to v1: https://lore.kernel.org/all/[email protected]/

Marco Crivellari (5):
tick/nohz: Re-evaluate the tick on return to user space sched/idle: Don't handle polling idle as dynticks idle tick/nohz: Remove tick_nohz_idle_restart_tick() tick/nohz: Reset got_idle_tick when entering dynticks idle
Documentation: no_hz: Add tick stop in polling idle

 Documentation/timers/no_hz.rst |  2 ++
 include/linux/tick.h           | 10 ++++--
 kernel/context_tracking.c      | 12 +++++++
 kernel/sched/idle.c            | 13 +++++--
 kernel/time/tick-sched.c       | 65 +++++++++++++++++++---------------
 5 files changed, 70 insertions(+), 32 deletions(-)
Signature · 1 line
2.56.0