Enable haltpoll on arm64
v8 cover Enable haltpoll on arm64
This patchset enables the cpuidle-haltpoll driver and its namesake governor on arm64. This is specifically interesting for KVM guests by reducing IPC latencies.
Comparing idle switching latencies on an arm64 KVM guest with perf bench sched pipe:
usecs/op %stdev
no haltpoll (baseline) 13.48 +- 5.19%
with haltpoll 6.84 +- 22.07%
No change in performance for a similar test on x86:
usecs/op %stdev
haltpoll w/ cpu_relax() (baseline) 4.75 +- 1.76% haltpoll w/ smp_cond_load_relaxed() 4.78 +- 2.31%
Both sets of tests were on otherwise idle systems with guest VCPUs pinned to specific PCPUs. One reason for the higher stdev on arm64 is that trapping of the WFE instruction by the host KVM is contingent on the number of tasks on the runqueue.
Tomohiro Misono and Haris Okanovic also report similar latency improvements on Grace and Graviton systems (for v7) [1] [2].
The patch series is organized in three parts:
- patch 1, reorganizes the poll_idle() loop, switching to smp_cond_load_relaxed() in the polling loop.
Relatedly patches 2, 3 mangle the config option ARCH_HAS_CPU_RELAX, renaming it to ARCH_HAS_OPTIMIZED_POLL.
- patches 4-6 reorganize the haltpoll selection and init logic to allow architecture code to select it.
- and finally, patches 7-11 add the bits for arm64 support.
What is still missing: this series largely completes the haltpoll side of functionality for arm64. There are, however, a few related areas that still need to be threshed out:
- WFET support: WFE on arm64 does not guarantee that poll_idle() would terminate in halt_poll_ns. Using WFET would address this.
- KVM_NO_POLL support on arm64
- KVM TWED support on arm64: allow the host to limit time spent in WFE.
Changelog:
v8: No logic changes. Largely respin of v7, with changes noted below:
- move selection of ARCH_HAS_OPTIMIZED_POLL on arm64 to its own patch.
(patch-9 "arm64: select ARCH_HAS_OPTIMIZED_POLL")
- address comments simplifying arm64 support (Will Deacon) (patch-11 "arm64: support cpuidle-haltpoll")
v7: No significant logic changes. Mostly a respin of v6.
- minor cleanup in poll_idle() (Christoph Lameter)
- fixes conflicts due to code movement in arch/arm64/kernel/cpuidle.c (Tomohiro Misono)
v6:
- reordered the patches to keep poll_idle() and ARCH_HAS_OPTIMIZED_POLL changes together (comment from Christoph Lameter)
- threshes out the commit messages a bit more (comments from Christoph Lameter, Sudeep Holla)
- also rework selection of cpuidle-haltpoll. Now selected based on the architectural selection of ARCH_CPUIDLE_HALTPOLL.
- moved back to arch_haltpoll_want() (comment from Joao Martins) Also, arch_haltpoll_want() now takes the force parameter and is now responsible for the complete selection (or not) of haltpoll.
- fixes the build breakage on i386
- fixes the cpuidle-haltpoll module breakage on arm64 (comment from Tomohiro Misono, Haris Okanovic)
v5:
- rework the poll_idle() loop around smp_cond_load_relaxed() (review comment from Tomohiro Misono.)
- also rework selection of cpuidle-haltpoll. Now selected based on the architectural selection of ARCH_CPUIDLE_HALTPOLL.
- arch_haltpoll_supported() (renamed from arch_haltpoll_want()) on arm64 now depends on the event-stream being enabled.
- limit POLL_IDLE_RELAX_COUNT on arm64 (review comment from Haris Okanovic)
- ARCH_HAS_CPU_RELAX is now renamed to ARCH_HAS_OPTIMIZED_POLL.
v4 changes from v3:
- change 7/8 per Rafael input: drop the parens and use ret for the final check
- add 8/8 which renames the guard for building poll_state
v3 changes from v2:
- fix 1/7 per Petr Mladek - remove ARCH_HAS_CPU_RELAX from arch/x86/Kconfig
- add Ack-by from Rafael Wysocki on 2/7
v2 changes from v1:
- added patch 7 where we change cpu_relax with smp_cond_load_relaxed per PeterZ (this improves by 50% at least the CPU cycles consumed in the tests above:
10,716,881,137 now vs 14,503,014,257 before)
- removed the ifdef from patch 1 per RafaelW
Please review.
[1] https://lore.kernel.org/lkml/TY3PR01MB111481E9B0AF263ACC8EA5D4AE5BA2@TY3PR01MB11148.jpnprd01.prod.outlook.com/
[2] https://lore.kernel.org/lkml/[email protected]/
Ankur Arora (6):
cpuidle: rename ARCH_HAS_CPU_RELAX to ARCH_HAS_OPTIMIZED_POLL
cpuidle-haltpoll: condition on ARCH_CPUIDLE_HALTPOLL
arm64: idle: export arch_cpu_idle
arm64: select ARCH_HAS_OPTIMIZED_POLL cpuidle/poll_state: limit POLL_IDLE_RELAX_COUNT on arm64
arm64: support cpuidle-haltpoll
Joao Martins (4):
Kconfig: move ARCH_HAS_OPTIMIZED_POLL to arch/Kconfig
cpuidle-haltpoll: define arch_haltpoll_want() governors/haltpoll: drop kvm_para_available() check
arm64: define TIF_POLLING_NRFLAG
Mihai Carabas (1):
cpuidle/poll_state: poll via smp_cond_load_relaxed()
arch/Kconfig | 3 +++ arch/arm64/Kconfig | 7 +++++++ arch/arm64/include/asm/cpuidle_haltpoll.h | 24 +++++++++++++++++++++++ arch/arm64/include/asm/thread_info.h | 2 ++ arch/arm64/kernel/idle.c | 1 + arch/x86/Kconfig | 5 ++--- arch/x86/include/asm/cpuidle_haltpoll.h | 1 + arch/x86/kernel/kvm.c | 13 ++++++++++++ drivers/acpi/processor_idle.c | 4 ++-- drivers/cpuidle/Kconfig | 5 ++--- drivers/cpuidle/Makefile | 2 +- drivers/cpuidle/cpuidle-haltpoll.c | 12 +----------- drivers/cpuidle/governors/haltpoll.c | 6 +----- drivers/cpuidle/poll_state.c | 22 +++++++++++++++------ drivers/idle/Kconfig | 1 + include/linux/cpuidle.h | 2 +- include/linux/cpuidle_haltpoll.h | 5 +++++ 17 files changed, 83 insertions(+), 32 deletions(-) create mode 100644 arch/arm64/include/asm/cpuidle_haltpoll.h
Signature · 1 line
2.43.5
PATCH v8 1/11 cpuidle/poll_state: poll via smp_cond_load_relaxed()
From: Mihai Carabas <[email protected]>
The inner loop in poll_idle() polls up to POLL_IDLE_RELAX_COUNT times, checking to see if the thread has the TIF_NEED_RESCHED bit set. The loop exits once the condition is met, or if the poll time limit has been exceeded.
To minimize the number of instructions executed each iteration, the time check is done only infrequently (once every POLL_IDLE_RELAX_COUNT iterations). In addition, each loop iteration executes cpu_relax() which on certain platforms provides a hint to the pipeline that the loop is busy-waiting, thus allowing the processor to reduce power consumption.
However, cpu_relax() is defined optimally only on x86. On arm64, for instance, it is implemented as a YIELD which only serves a hint to the CPU that it prioritize a different hardware thread if one is available. arm64, however, does expose a more optimal polling mechanism via smp_cond_load_relaxed() which uses LDXR, WFE to wait until a store to a specified region.
So restructure the loop, folding both checks in smp_cond_load_relaxed(). Also, move the time check to the head of the loop allowing it to exit straight-away once TIF_NEED_RESCHED is set.
--- drivers/cpuidle/poll_state.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/cpuidle/poll_state.c +5 -6
@@ -21,21 +21,20 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -unsigned int loop_count =0 ;u64 limit; limit =cpuidle_poll_time (drv, dev);while (!need_resched ()) { -cpu_relax (); -if (loop_count++ <POLL_IDLE_RELAX_COUNT ) -continue ; - - loop_count =0 ; +unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } + +smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT ); } }raw_local_irq_disable ();
Signature · 1 line
2.43.5
Catalin Marinas
maintainerRe: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 18 lines · Ankur Arora
drivers/cpuidle/poll_state.c +0
-5@@ -21,21 +21,20 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -unsigned int loop_count =0 ;u64 limit; limit =cpuidle_poll_time (drv, dev);while (!need_resched ()) { -cpu_relax (); -if (loop_count++ <POLL_IDLE_RELAX_COUNT ) -continue ; -
drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 10 lines
drivers/cpuidle/poll_state.c +5
-1- loop_count =0 ; +unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } + +smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );
The above is not guaranteed to make progress if _TIF_NEED_RESCHED is never set. With the event stream enabled on arm64, the WFE will eventually be woken up, loop_count incremented and the condition would become true. However, the smp_cond_load_relaxed() semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself. Also note that the event stream can be disabled on arm64 on the kernel command line.
Does the code above break any other architecture? I'd say if you want something like this, better introduce a new smp_cond_load_timeout() API. The above looks like a hack that may only work on arm64 when the event stream is enabled.
A generic option is udelay() (on arm64 it would use WFE/WFET by default). Not sure how important it is for poll_idle() but the downside of udelay() that it won't be able to also poll need_resched() while waiting for the timeout. If this matters, you could instead make smaller udelay() calls. Yet another problem, I don't know how energy efficient udelay() is on x86 vs cpu_relax().
So maybe an smp_cond_load_timeout() would be better, implemented with cpu_relax() generically and the arm64 would use LDXR, WFE and rely on the event stream (or fall back to cpu_relax() if the event stream is disabled).
Signature · 1 line
Catalin
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 10 lines · Catalin Marinas
drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 9 lines
drivers/cpuidle/poll_state.c +5
-0+unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } + +smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );
Quoted 7 lines
The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true. However, thesmp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself. Also note that the event stream can be disabled on arm64 on the kernel command line.
Setting of need_resched() from another processor involves sending an IPI after that was set. I dont think we need to smp_cond_load_relaxed since the IPI will cause an event. For ARM a WFE would be sufficient.
Catalin Marinas
maintainerRe: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 19 lines · Christoph Lameter (Ampere)
Quoted 10 lines · Catalin Marinas
drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 9 lines
drivers/cpuidle/poll_state.c +5
-0+unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } + +smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );Quoted 7 lines
The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true. However, thesmp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself. Also note that the event stream can be disabled on arm64 on the kernel command line.
Quoted 3 lines
Setting of
need_resched()from another processor involves sending an IPI after that was set. I dont think we need tosmp_cond_load_relaxedsince the IPI will cause an event. For ARM a WFE would be sufficient.
I'm not worried about the need_resched() case, even without an IPI it would still work.
The loop_count++ side of the condition is supposed to timeout in the absence of a need_resched() event. You can't do an smp_cond_load_*() on a variable that's only updated by the waiting CPU. Nothing guarantees to wake it up to update the variable (the event stream on arm64, yes, but that's generic code).
Signature · 1 line
Catalin
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 4 lines · Catalin Marinas
Quoted 3 lines
Setting of
need_resched()from another processor involves sending an IPI after that was set. I dont think we need tosmp_cond_load_relaxedsince the IPI will cause an event. For ARM a WFE would be sufficient.
Quoted 8 lines
I'm not worried about the
need_resched()case, even without an IPI it would still work.The loop_count++ side of the condition is supposed to timeout in the absence of a
need_resched()event. You can't do an smp_cond_load_*() on a variable that's only updated by the waiting CPU. Nothing guarantees to wake it up to update the variable (the event stream on arm64, yes, but that's generic code).
Hmm... I have WFET implementation here without smp_cond modelled after the delay() implementation ARM64 (but its not generic and there is an additional patch required to make this work. Intermediate patch attached)
From: Christoph Lameter (Ampere) <[email protected]>
Subject: [Haltpoll: Implement waiting using WFET
Use WFET if the hardware supports it to implement a wait until something happens to wake up the cpu.
If WFET is not available then use the stream event source to periodically wake up until an event happens or the timeout expires.
The smp_cond_wait() is not necessary because the scheduler will create an event on the targeted cpu by sending an IPI.
Without cond_wait we can simply take the basic approach from the delay() function and customize it a bit.
--- drivers/cpuidle/poll_state.c | 43 +++++++++++++++++------------------- 1 file changed, 20 insertions(+), 23 deletions(-)
drivers/cpuidle/poll_state.c +20 -27
@@ -5,48 +5,41 @@#include <linux/cpuidle.h> #include <linux/sched.h> -#include <linux/sched/clock.h> #include <linux/sched/idle.h> - -#ifdef CONFIG_ARM64 -/* -* POLL_IDLE_RELAX_COUNT determines how often we check for timeout -* while polling for TIF_NEED_RESCHED in thread_info->flags. -* -* Set this to a low value since arm64, instead of polling, uses a -* event based mechanism. -*/ -#define POLL_IDLE_RELAX_COUNT 1 -#else -#define POLL_IDLE_RELAX_COUNT 200 -#endif +#include <clocksource/arm_arch_timer.h> static int __cpuidle poll_idle(struct cpuidle_device *dev,struct cpuidle_driver *drv,int index) { -u64 time_start; - - time_start =local_clock_noinstr (); +const cycles_t start =get_cycles (); dev->poll_time_limit = false;raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -u64 limit; - limit =cpuidle_poll_time (drv, dev); +const cycles_t end = start +ARCH_TIMER_NSECS_TO_CYCLES (cpuidle_poll_time (drv, dev));while (!need_resched ()) { -unsigned int loop_count =0 ; -if (local_clock_noinstr () - time_start > limit) { - dev->poll_time_limit = true; -break ; - } -smp_cond_load_relaxed (¤t_thread_info ()->flags, -VAL & _TIF_NEED_RESCHED || - loop_count++ >=POLL_IDLE_RELAX_COUNT ); +if (alternative_has_cap_unlikely (ARM64_HAS_WFXT )) { + +/* We can power down for a configurable interval while waiting */ +while (!need_resched () &&get_cycles () < end) +wfet (end); + + }else if (arch_timer_evtstrm_available ()) { +const cycles_t timer_period =ARCH_TIMER_USECS_TO_CYCLES (ARCH_TIMER_EVT_STREAM_PERIOD_US ); + +/* Wake up periodically during evstream events */ +while (!need_resched () &&get_cycles () + timer_period <= end) +wfe (); + } } + +/* In case time is not up yet due to coarse time intervals above */ +while (!need_resched () &&get_cycles () < end) +cpu_relax (); }raw_local_irq_disable ();
From: Christoph Lameter (Ampere) <[email protected]>
Move the conversion from time to cycles of arch_timer into arch_timer.h. Add nsec conversion since we will need that soon.
arch/arm64/lib/delay.c +2 -10
@@ -15,14 +15,6 @@#include <clocksource/arm_arch_timer.h> -#define USECS_TO_CYCLES (time_usecs) \ - xloops_to_cycles((time_usecs) * 0x10C7UL) - -static inline unsigned long xloops_to_cycles (unsigned long xloops) -{ -return (xloops * loops_per_jiffy *HZ ) >>32 ; -} -void __delay (unsigned long cycles) {cycles_t start =get_cycles ();
@@ -39,7 +31,7 @@ void __delay(unsigned long cycles)wfet (end); }else if (arch_timer_evtstrm_available ()) {const cycles_t timer_evt_period = -USECS_TO_CYCLES (ARCH_TIMER_EVT_STREAM_PERIOD_US ); +ARCH_TIMER_USECS_TO_CYCLES (ARCH_TIMER_EVT_STREAM_PERIOD_US );while ((get_cycles () - start + timer_evt_period) < cycles)wfe ();
@@ -52,7 +44,7 @@ EXPORT_SYMBOL(__delay);inline void __const_udelay (unsigned long xloops) { -__delay (xloops_to_cycles (xloops)); +__delay (arch_timer_xloops_to_cycles (xloops)); }EXPORT_SYMBOL (__const_udelay);
include/clocksource/arm_arch_timer.h +13 -0
@@ -90,6 +90,19 @@ extern u64 (*arch_timer_read_counter)(voextern struct arch_timer_kvm_info *arch_timer_get_kvm_info (void );extern bool arch_timer_evtstrm_available (void ); +#include <linux/delay.h> + +static inline unsigned long arch_timer_xloops_to_cycles (unsigned long xloops) +{ +return (xloops * loops_per_jiffy *HZ ) >>32 ; +} + +#define ARCH_TIMER_USECS_TO_CYCLES (time_usecs) \ + arch_timer_xloops_to_cycles((time_usecs) * 0x10C7UL) + +#define ARCH_TIMER_NSECS_TO_CYCLES (time_nsecs) \ + arch_timer_xloops_to_cycles((time_nsecs) * 0x5UL) +#else static inline u32 arch_timer_get_rate (void )
Catalin Marinas
maintainerRe: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 14 lines · Christoph Lameter (Ampere)
Quoted 4 lines · Catalin Marinas
Quoted 3 lines
Setting of
need_resched()from another processor involves sending an IPI after that was set. I dont think we need tosmp_cond_load_relaxedsince the IPI will cause an event. For ARM a WFE would be sufficient.Quoted 8 lines
I'm not worried about the
need_resched()case, even without an IPI it would still work.The loop_count++ side of the condition is supposed to timeout in the absence of a
need_resched()event. You can't do an smp_cond_load_*() on a variable that's only updated by the waiting CPU. Nothing guarantees to wake it up to update the variable (the event stream on arm64, yes, but that's generic code).
Quoted 4 lines
Hmm... I have WFET implementation here without
smp_condmodelled after thedelay()implementation ARM64 (but its not generic and there is an additional patch required to make this work. Intermediate patch attached)
At least one additional patch ;). But yeah, I suggested hiding all this behind something like smp_cond_load_timeout() which would wait on current_thread_info()->flags but with a timeout. The arm64 implementation would follow some of the logic in __delay(). Others may simply poll with cpu_relax().
Alternatively, if we get an IPI anyway, we can avoid smp_cond_load() and rely on need_resched() and some new delay/cpu_relax() API that waits for a timeout or an IPI, whichever comes first. E.g. cpu_relax_timeout() which on arm64 it's just a simplified version of __delay() without the 'while' loops.
Signature · 1 line
Catalin
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 26 lines · Catalin Marinas
Quoted 14 lines · Christoph Lameter (Ampere)
Quoted 4 lines · Catalin Marinas
Quoted 3 lines
Setting of
need_resched()from another processor involves sending an IPI after that was set. I dont think we need tosmp_cond_load_relaxedsince the IPI will cause an event. For ARM a WFE would be sufficient.Quoted 8 lines
I'm not worried about the
need_resched()case, even without an IPI it would still work.The loop_count++ side of the condition is supposed to timeout in the absence of a
need_resched()event. You can't do an smp_cond_load_*() on a variable that's only updated by the waiting CPU. Nothing guarantees to wake it up to update the variable (the event stream on arm64, yes, but that's generic code).Quoted 4 lines
Hmm... I have WFET implementation here without
smp_condmodelled after thedelay()implementation ARM64 (but its not generic and there is an additional patch required to make this work. Intermediate patch attached)At least one additional patch ;). But yeah, I suggested hiding all this behind something like
smp_cond_load_timeout()which would wait on current_thread_info()->flags but with a timeout. The arm64 implementation would follow some of the logic in__delay(). Others may simply poll withcpu_relax().
Quoted 5 lines
Alternatively, if we get an IPI anyway, we can avoid
smp_cond_load()and rely onneed_resched()and some new delay/cpu_relax() API that waits for a timeout or an IPI, whichever comes first. E.g.cpu_relax_timeout()which on arm64 it's just a simplified version of__delay()without the 'while' loops.
AFAICT when polling (which we are since poll_idle() calls current_set_polling_and_test()), the scheduler will elide the IPI by remotely setting the need-resched bit via set_nr_if_polling().
Once we stop polling then the scheduler should take the IPI path because call_function_single_prep_ipi() will fail.
--
ankur
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 9 lines · Ankur Arora
Quoted 5 lines
Alternatively, if we get an IPI anyway, we can avoid
smp_cond_load()and rely onneed_resched()and some new delay/cpu_relax() API that waits for a timeout or an IPI, whichever comes first. E.g.cpu_relax_timeout()which on arm64 it's just a simplified version of__delay()without the 'while' loops.AFAICT when polling (which we are since
poll_idle()callscurrent_set_polling_and_test()), the scheduler will elide the IPI by remotely setting the need-resched bit viaset_nr_if_polling().
The scheduler runs on multiple cores. The core on which we are running this code puts the core into a wait state so the scheduler does not run on this core at all during the wait period.
The other cores may run scheduler functions and set the need_resched bit for the core where we are currently waiting.
The other core will wake our core up by sending an IPI. The IPI will invoke a scheduler function on our core and the WFE will continue.
Quoted 2 lines
Once we stop polling then the scheduler should take the IPI path because
call_function_single_prep_ipi()will fail.
The IPI stops the polling. IPI is an interrupt.
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 8 lines · Christoph Lameter (Ampere)
Quoted 6 lines · Ankur Arora
Quoted 5 lines
Alternatively, if we get an IPI anyway, we can avoid
smp_cond_load()and rely onneed_resched()and some new delay/cpu_relax() API that waits for a timeout or an IPI, whichever comes first. E.g.cpu_relax_timeout()which on arm64 it's just a simplified version of__delay()without the 'while' loops.
Quoted 7 lines
Quoted 3 lines
AFAICT when polling (which we are since
poll_idle()callscurrent_set_polling_and_test()), the scheduler will elide the IPI by remotely setting the need-resched bit viaset_nr_if_polling().The scheduler runs on multiple cores. The core on which we are running this code puts the core into a wait state so the scheduler does not run on this core at all during the wait period.
Yes.
Quoted 2 lines
The other cores may run scheduler functions and set the
need_reschedbit for the core where we are currently waiting.
Yes.
Quoted 2 lines
The other core will wake our core up by sending an IPI. The IPI will invoke a scheduler function on our core and the WFE will continue.
Why? The target core is not sleeping. It is *polling* on a memory address (on arm64, via LDXR; WFE). Ergo an IPI is not needed to tell it that a need-resched bit is set.
Quoted 4 lines
Quoted 2 lines
Once we stop polling then the scheduler should take the IPI path because
call_function_single_prep_ipi()will fail.The IPI stops the polling. IPI is an interrupt.
Yes an IPI is an interrupt. And, since the target is polling there's no need for an interrupt to inform it that a memory address on which it is polling has changed.
resched_curr() is a good example. It only sends the resched IPI if the target is not polling.
resched_curr() {
...
if (set_nr_and_not_polling(curr))
smp_send_reschedule(cpu);
else
trace_sched_wake_idle_without_ipi(cpu);
}--
ankur
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 6 lines · Ankur Arora
Quoted 2 lines
The other core will wake our core up by sending an IPI. The IPI will invoke a scheduler function on our core and the WFE will continue.
Why? The target core is not sleeping. It is *polling* on a memory address (on arm64, via LDXR; WFE). Ergo an IPI is not needed to tell it that a need-resched bit is set.
The IPI is sent to interrupt the process that is not sleeping. This is done so the busy processor can reschedule the currently running process and respond to the event.
It does not matter if the core is "sleeping" or not.
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 5 lines · Christoph Lameter (Ampere)
Quoted 3 lines · Ankur Arora
Quoted 2 lines
The other core will wake our core up by sending an IPI. The IPI will invoke a scheduler function on our core and the WFE will continue.
Quoted 7 lines
Quoted 3 lines
Why? The target core is not sleeping. It is *polling* on a memory address (on arm64, via LDXR; WFE). Ergo an IPI is not needed to tell it that a need-resched bit is set.
The IPI is sent to interrupt the process that is not sleeping. This is done so the busy processor can reschedule the currently running process and respond to the event.
The scheduler treats idle specially (if the architecture defines TIF_POLLING_NRFLAG). There's also the sched_wake_idle_without_ipi tracepoint for this path.
$ sudo perf stat -e sched:sched_wake_idle_without_ipi perf bench sched pipe # Running 'sched/pipe' benchmark: # Executed 1000000 pipe operations between two processes
Total time: 5.173 [sec]
5.173613 usecs/op
193288 ops/secPerformance counter stats for 'perf bench sched pipe':
1,992,368 sched:sched_wake_idle_without_ipi
5.178976487 seconds time elapsed
0.396076000 seconds user
6.999566000 seconds sys--
ankur
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
In reply to Ankur AroraHere is a patch that keeps the cpuidle stuiff generic but allows an override by arm64..
From: Christoph Lameter (Ampere) <[email protected]>
Subject: Revise cpu poll idle to make full use of wfet() and wfe()
ARM64 has instructions that can wait for an event and timeouts.
Clean up the code in drivers/cpuidle/ to wait until the end of a period and allow the override of the handling of the waiting by an architecture.
Provide an optimized wait function for arm64.
arch/arm64/lib/delay.c +26 -0
@@ -12,6 +12,8 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/timex.h> +#include <linux/sched/clock.h> +#include <linux/cpuidle.h> #include <clocksource/arm_arch_timer.h>
@@ -67,3 +69,27 @@ void __ndelay(unsigned long nsecs)__const_udelay (nsecs *0x5UL );/* 2**32 / 1000000000 (rounded up) */ }EXPORT_SYMBOL (__ndelay); + +void cpuidle_wait_for_resched_with_timeout (u64 end) +{ +u64 start; + +while (!need_resched () && (start =local_clock_noinstr ()) < end) { + +if (alternative_has_cap_unlikely (ARM64_HAS_WFXT )) { + +/* Processor supports waiting for a specified period */ +wfet (xloops_to_cycles ((end - start) *0x5UL )); + + }else +if (arch_timer_evtstrm_available () && start +ARCH_TIMER_EVT_STREAM_PERIOD_US *1000 < end) { + +/* We can wait until a periodic event occurs */ +wfe (); + + }else +/* Need to spin until the end */ +cpu_relax (); + } +} +
drivers/cpuidle/poll_state.c +12 -18
@@ -8,35 +8,29 @@#include <linux/sched/clock.h> #include <linux/sched/idle.h> -#define POLL_IDLE_RELAX_COUNT 200 +__weak void cpuidle_wait_for_resched_with_timeout (u64 end) +{ +while (!need_resched () &&local_clock_noinstr () < end) { +cpu_relax (); + } +} static int __cpuidle poll_idle(struct cpuidle_device *dev,struct cpuidle_driver *drv,int index) { -u64 time_start; - - time_start =local_clock_noinstr (); +u64 time_start =local_clock_noinstr (); +u64 time_end = time_start +cpuidle_poll_time (drv, dev); dev->poll_time_limit = false;raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -unsigned int loop_count =0 ; -u64 limit; - limit =cpuidle_poll_time (drv, dev); +cpuidle_wait_for_resched_with_timeout (time_end); + +if (!need_resched ()) + dev->poll_time_limit = true; -while (!need_resched ()) { -cpu_relax (); -if (loop_count++ <POLL_IDLE_RELAX_COUNT ) -continue ; - - loop_count =0 ; -if (local_clock_noinstr () - time_start > limit) { - dev->poll_time_limit = true; -break ; - } - } }raw_local_irq_disable ();
include/linux/cpuidle.h +3 -0
@@ -202,6 +202,9 @@ extern int cpuidle_play_dead(void);extern struct cpuidle_driver *cpuidle_get_cpu_driver (struct cpuidle_device *dev);static inline struct cpuidle_device *cpuidle_get_device (void ) {return __this_cpu_read (cpuidle_devices); } + +extern __weak void cpuidle_wait_for_resched_with_timeout (u64 ); +#else static inline void disable_cpuidle (void ) { } static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
Catalin Marinas
maintainerRe: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 30 lines · Christoph Lameter (Ampere)
Index:
linux/arch/arm64/lib/delay.c
===================================================================arch/arm64/lib/delay.c +15
-0@@ -12,6 +12,8 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/timex.h> +#include <linux/sched/clock.h> +#include <linux/cpuidle.h> #include <clocksource/arm_arch_timer.h> @@ -67,3 +69,27 @@ void __ndelay(unsigned long nsecs)__const_udelay (nsecs *0x5UL );/* 2**32 / 1000000000 (rounded up) */ }EXPORT_SYMBOL (__ndelay); + +void cpuidle_wait_for_resched_with_timeout (u64 end) +{ +u64 start; + +while (!need_resched () && (start =local_clock_noinstr ()) < end) { + +if (alternative_has_cap_unlikely (ARM64_HAS_WFXT )) { + +/* Processor supports waiting for a specified period */ +wfet (xloops_to_cycles ((end - start) *0x5UL )); + + }else
arch/arm64/lib/delay.c void __ndelay(unsigned long nsecs) · line 94
Quoted 10 lines
arch/arm64/lib/delay.c +10
-0+if (arch_timer_evtstrm_available () && start +ARCH_TIMER_EVT_STREAM_PERIOD_US *1000 < end) { + +/* We can wait until a periodic event occurs */ +wfe (); + + }else +/* Need to spin until the end */ +cpu_relax (); + } +}
The behaviour above is slightly different from the current poll_idle() implementation. The above is more like poll every timeout period rather than continuously poll until either the need_resched() condition is true _or_ the timeout expired. From Ankur's email, an IPI may not happen so we don't have any guarantee that WFET will wake up before the timeout. The only way for WFE/WFET to wake up on need_resched() is to use LDXR to arm the exclusive monitor. That's what smp_cond_load_relaxed() does.
If you only need the behaviour proposed above, you might as well go for udelay() directly. Otherwise I think we need to revisit Ankur's smp_cond_load_timeout() proposal from earlier this year.
Signature · 1 line
Catalin
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 7 lines · Catalin Marinas
The behaviour above is slightly different from the current
poll_idle()implementation. The above is more like poll every timeout period rather than continuously poll until either theneed_resched()condition is true _or_ the timeout expired. From Ankur's email, an IPI may not happen so we don't have any guarantee that WFET will wake up before the timeout. The only way for WFE/WFET to wake up onneed_resched()is to use LDXR to arm the exclusive monitor. That's whatsmp_cond_load_relaxed()does.
Sorry no. The IPI will cause the WFE to continue immediately and not wait till the end of the timeout period.
Catalin Marinas
maintainerRe: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 11 lines · Christoph Lameter (Ampere)
Quoted 7 lines · Catalin Marinas
The behaviour above is slightly different from the current
poll_idle()implementation. The above is more like poll every timeout period rather than continuously poll until either theneed_resched()condition is true _or_ the timeout expired. From Ankur's email, an IPI may not happen so we don't have any guarantee that WFET will wake up before the timeout. The only way for WFE/WFET to wake up onneed_resched()is to use LDXR to arm the exclusive monitor. That's whatsmp_cond_load_relaxed()does.Sorry no. The IPI will cause the WFE to continue immediately and not wait till the end of the timeout period.
*If* there is an IPI. The scheduler is not really my area but some functions like wake_up_idle_cpu() seem to elide the IPI if TIF_NR_POLLING is set.
But even if we had an IPI, it still feels like abusing the semantics of smp_cond_load_relaxed() when relying on it to increment a variable in the condition check as a result of some unrelated wake-up event. This API is meant to wait for a condition on a single variable. It cannot wait on multiple variables and especially not one it updates itself (even if it happens to work on arm64 under certain conditions).
My strong preference would be to revive the smp_cond_load_timeout() proposal from Ankur earlier in the year.
Signature · 1 line
Catalin
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 17 lines · Catalin Marinas
Quoted 11 lines · Christoph Lameter (Ampere)
Quoted 7 lines · Catalin Marinas
The behaviour above is slightly different from the current
poll_idle()implementation. The above is more like poll every timeout period rather than continuously poll until either theneed_resched()condition is true _or_ the timeout expired. From Ankur's email, an IPI may not happen so we don't have any guarantee that WFET will wake up before the timeout. The only way for WFE/WFET to wake up onneed_resched()is to use LDXR to arm the exclusive monitor. That's whatsmp_cond_load_relaxed()does.Sorry no. The IPI will cause the WFE to continue immediately and not wait till the end of the timeout period.
*If* there is an IPI. The scheduler is not really my area but some functions like
wake_up_idle_cpu()seem to elide the IPI ifTIF_NR_POLLINGis set.
Quoted 6 lines
But even if we had an IPI, it still feels like abusing the semantics of
smp_cond_load_relaxed()when relying on it to increment a variable in the condition check as a result of some unrelated wake-up event. This API is meant to wait for a condition on a single variable. It cannot wait on multiple variables and especially not one it updates itself (even if it happens to work on arm64 under certain conditions).
Yeah that makes sense. smp_cond_load_relaxed() uses two separate side-effects to make sure things work: the event-stream and the increment in the conditional.
I do want to thresh out smp_cond_load_timeout() a bit more but let me reply to your other mail for that.
Quoted 2 lines
My strong preference would be to revive the
smp_cond_load_timeout()proposal from Ankur earlier in the year.
Ack that.
--
ankur
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
In reply to Catalin MarinasQuoted 26 lines · Catalin Marinas
Quoted 25 lines · Ankur Arora
drivers/cpuidle/poll_state.c +2
-6@@ -21,21 +21,20 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -unsigned int loop_count =0 ;u64 limit; limit =cpuidle_poll_time (drv, dev);while (!need_resched ()) { -cpu_relax (); -if (loop_count++ <POLL_IDLE_RELAX_COUNT ) -continue ; - - loop_count =0 ; +unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } +
Quoted 8 lines
drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 3 lines
drivers/cpuidle/poll_state.c +3
-0+smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true.
That makes sense.
Quoted 3 lines
However, the
smp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself.
Right. And, that seems to work well with the semantics of WFE. And, the event stream (if enabled) has a side effect that allows the exit from the loop.
Quoted 2 lines
Also note that the event stream can be disabled
on arm64 on the kernel command line.
Yes, that's a good point. In patch-11 I tried to address that aspect by only allowing haltpoll to be force loaded.
But, I guess your point is that its not just haltpoll that has a problem, but also regular polling -- and maybe the right thing to do would be to disable polling if the event stream is disabled.
Quoted 1 line
Does the code above break any other architecture?
Me (and others) have so far tested x86, ARM64 (with/without the event stream), and I believe riscv. I haven't seen any obvious breakage. But, that's probably because most of the time somebody would be set TIF_NEED_RESCHED.
Quoted 4 lines
I'd say if you want
something like this, better introduce a newsmp_cond_load_timeout()API. The above looks like a hack that may only work on arm64 when the event stream is enabled.
I had a preliminary version of smp_cond_load_relaxed_timeout() here:
https://lore.kernel.org/lkml/[email protected]/
Even with an smp_cond_load_timeout(), we would need to fallback to something like the above for uarchs without WFxT.
Quoted 11 lines
A generic option is
udelay()(on arm64 it would use WFE/WFET by default). Not sure how important it is forpoll_idle()but the downside ofudelay()that it won't be able to also pollneed_resched()while waiting for the timeout. If this matters, you could instead make smallerudelay()calls. Yet another problem, I don't know how energy efficientudelay()is on x86 vscpu_relax().So maybe an
smp_cond_load_timeout()would be better, implemented withcpu_relax()generically and the arm64 would use LDXR, WFE and rely on the event stream (or fall back tocpu_relax()if the event stream is disabled).
Yeah, something like that might work.
--
ankur
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 83 lines · Ankur Arora
Quoted 26 lines · Catalin Marinas
Quoted 25 lines · Ankur Arora
drivers/cpuidle/poll_state.c +2
-6@@ -21,21 +21,20 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -unsigned int loop_count =0 ;u64 limit; limit =cpuidle_poll_time (drv, dev);while (!need_resched ()) { -cpu_relax (); -if (loop_count++ <POLL_IDLE_RELAX_COUNT ) -continue ; - - loop_count =0 ; +unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } +Quoted 8 lines
drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 3 lines
drivers/cpuidle/poll_state.c +3
-0+smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true.That makes sense.
Quoted 3 lines
However, the
smp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself.Right. And, that seems to work well with the semantics of WFE. And, the event stream (if enabled) has a side effect that allows the exit from the loop.
Quoted 2 lines
Also note that the event stream can be disabled
on arm64 on the kernel command line.Yes, that's a good point. In patch-11 I tried to address that aspect by only allowing haltpoll to be force loaded.
But, I guess your point is that its not just haltpoll that has a problem, but also regular polling -- and maybe the right thing to do would be to disable polling if the event stream is disabled.
Quoted 1 line
Does the code above break any other architecture?
Me (and others) have so far tested x86, ARM64 (with/without the event stream), and I believe riscv. I haven't seen any obvious breakage. But, that's probably because most of the time somebody would be set
TIF_NEED_RESCHED.Quoted 4 lines
I'd say if you want
something like this, better introduce a newsmp_cond_load_timeout()API. The above looks like a hack that may only work on arm64 when the event stream is enabled.I had a preliminary version of
smp_cond_load_relaxed_timeout()here:
https://lore.kernel.org/lkml/[email protected]/Even with an
smp_cond_load_timeout(), we would need to fallback to something like the above for uarchs without WFxT.Quoted 7 lines
A generic option is
udelay()(on arm64 it would use WFE/WFET by default). Not sure how important it is forpoll_idle()but the downside ofudelay()that it won't be able to also pollneed_resched()while waiting for the timeout. If this matters, you could instead make smallerudelay()calls. Yet another problem, I don't know how energy efficientudelay()is on x86 vscpu_relax().
Quoted 6 lines
Quoted 4 lines
So maybe an
smp_cond_load_timeout()would be better, implemented withcpu_relax()generically and the arm64 would use LDXR, WFE and rely on the event stream (or fall back tocpu_relax()if the event stream is disabled).Yeah, something like that might work.
Yeah, I like idea about smp_cond_load_timeout method. If generic smp_cond_load_timeout method does not meet the requirement, the architecture can define itself one. And this keeps less modification about original code logic.
Regards
Bibo Mao
Quoted 4 lines
--
ankur
Catalin Marinas
maintainerRe: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
In reply to Ankur AroraQuoted 11 lines · Ankur Arora
Quoted 4 lines · Catalin Marinas
I'd say if you want
something like this, better introduce a newsmp_cond_load_timeout()API. The above looks like a hack that may only work on arm64 when the event stream is enabled.I had a preliminary version of
smp_cond_load_relaxed_timeout()here:
https://lore.kernel.org/lkml/[email protected]/Even with an
smp_cond_load_timeout(), we would need to fallback to something like the above for uarchs without WFxT.
Yes, the default/generic implementation would use cpu_relax(). For the arm64 one, some combination of __cmpwait() and __delay() with the fallback to cpu_relax().
Signature · 1 line
Catalin
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
In reply to Catalin MarinasQuoted 34 lines · Catalin Marinas
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 18 lines · Ankur Arora
drivers/cpuidle/poll_state.c +0
-5@@ -21,21 +21,20 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -unsigned int loop_count =0 ;u64 limit; limit =cpuidle_poll_time (drv, dev);while (!need_resched ()) { -cpu_relax (); -if (loop_count++ <POLL_IDLE_RELAX_COUNT ) -continue ; -drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 10 lines
drivers/cpuidle/poll_state.c +5
-1- loop_count =0 ; +unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } + +smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );
Quoted 7 lines
The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true. However, thesmp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself. Also note that the event stream can be disabled on arm64 on the kernel command line.
Alternately could we condition arch_haltpoll_want() on arch_timer_evtstrm_available(), like v7?
Quoted 20 lines
Does the code above break any other architecture? I'd say if you want something like this, better introduce a new
smp_cond_load_timeout()API. The above looks like a hack that may only work on arm64 when the event stream is enabled.A generic option is
udelay()(on arm64 it would use WFE/WFET by default). Not sure how important it is forpoll_idle()but the downside ofudelay()that it won't be able to also pollneed_resched()while waiting for the timeout. If this matters, you could instead make smallerudelay()calls. Yet another problem, I don't know how energy efficientudelay()is on x86 vscpu_relax().So maybe an
smp_cond_load_timeout()would be better, implemented withcpu_relax()generically and the arm64 would use LDXR, WFE and rely on the event stream (or fall back tocpu_relax()if the event stream is disabled).--
Catalin
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 43 lines · Haris
Quoted 34 lines · Catalin Marinas
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 18 lines · Ankur Arora
drivers/cpuidle/poll_state.c +0
-5@@ -21,21 +21,20 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -unsigned int loop_count =0 ;u64 limit; limit =cpuidle_poll_time (drv, dev);while (!need_resched ()) { -cpu_relax (); -if (loop_count++ <POLL_IDLE_RELAX_COUNT ) -continue ; -drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 10 lines
drivers/cpuidle/poll_state.c +5
-1- loop_count =0 ; +unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } + +smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );Quoted 7 lines
The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true. However, thesmp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself. Also note that the event stream can be disabled on arm64 on the kernel command line.
Quoted 2 lines
Alternately could we condition
arch_haltpoll_want()onarch_timer_evtstrm_available(), like v7?
Yes, I'm thinking of staging it somewhat like that. First an smp_cond_load_relaxed() which gets rid of this issue, followed by one based on smp_cond_load_relaxed_timeout().
That said, conditioning just arch_haltpoll_want() won't suffice since what Catalin pointed out affects all users of poll_idle(), not just haltpoll.
Right now there's only haltpoll but there are future users like zhenglifeng with a patch for acpi-idle here:
https://lore.kernel.org/all/[email protected]/
Quoted 12 lines
Quoted 12 lines
Does the code above break any other architecture? I'd say if you want something like this, better introduce a new
smp_cond_load_timeout()API. The above looks like a hack that may only work on arm64 when the event stream is enabled.A generic option is
udelay()(on arm64 it would use WFE/WFET by default). Not sure how important it is forpoll_idle()but the downside ofudelay()that it won't be able to also pollneed_resched()while waiting for the timeout. If this matters, you could instead make smallerudelay()calls. Yet another problem, I don't know how energy efficientudelay()is on x86 vscpu_relax().
Quoted 7 lines
Quoted 7 lines
So maybe an
smp_cond_load_timeout()would be better, implemented withcpu_relax()generically and the arm64 would use LDXR, WFE and rely on the event stream (or fall back tocpu_relax()if the event stream is disabled).--
Catalin
--
ankur
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 52 lines · Ankur Arora
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 43 lines · Haris
Quoted 34 lines · Catalin Marinas
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 18 lines · Ankur Arora
drivers/cpuidle/poll_state.c +0
-5@@ -21,21 +21,20 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -unsigned int loop_count =0 ;u64 limit; limit =cpuidle_poll_time (drv, dev);while (!need_resched ()) { -cpu_relax (); -if (loop_count++ <POLL_IDLE_RELAX_COUNT ) -continue ; -drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 10 lines
drivers/cpuidle/poll_state.c +5
-1- loop_count =0 ; +unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } + +smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );Quoted 7 lines
The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true. However, thesmp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself. Also note that the event stream can be disabled on arm64 on the kernel command line.Quoted 2 lines
Alternately could we condition
arch_haltpoll_want()onarch_timer_evtstrm_available(), like v7?
Quoted 7 lines
Yes, I'm thinking of staging it somewhat like that. First an
smp_cond_load_relaxed()which gets rid of this issue, followed by one based onsmp_cond_load_relaxed_timeout().That said, conditioning just
arch_haltpoll_want()won't suffice since what Catalin pointed out affects all users ofpoll_idle(), not just haltpoll.
The only other users I see today are apm_init() and acpi_processor_setup_cstates(), both in x86 path. Perhaps not ideal, but should be sufficient.
Quoted 29 lines
Right now there's only haltpoll but there are future users like zhenglifeng with a patch for acpi-idle here:
https://lore.kernel.org/all/[email protected]/
Quoted 12 lines
Quoted 12 lines
Does the code above break any other architecture? I'd say if you want something like this, better introduce a new
smp_cond_load_timeout()API. The above looks like a hack that may only work on arm64 when the event stream is enabled.A generic option is
udelay()(on arm64 it would use WFE/WFET by default). Not sure how important it is forpoll_idle()but the downside ofudelay()that it won't be able to also pollneed_resched()while waiting for the timeout. If this matters, you could instead make smallerudelay()calls. Yet another problem, I don't know how energy efficientudelay()is on x86 vscpu_relax().Quoted 7 lines
Quoted 7 lines
So maybe an
smp_cond_load_timeout()would be better, implemented withcpu_relax()generically and the arm64 would use LDXR, WFE and rely on the event stream (or fall back tocpu_relax()if the event stream is disabled).--
Catalin--
ankur
Catalin Marinas
maintainerRe: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
In reply to Okanovic, HarisQuoted 39 lines · Haris
Quoted 30 lines · Catalin Marinas
Quoted 18 lines · Ankur Arora
drivers/cpuidle/poll_state.c +0
-5@@ -21,21 +21,20 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -unsigned int loop_count =0 ;u64 limit; limit =cpuidle_poll_time (drv, dev);while (!need_resched ()) { -cpu_relax (); -if (loop_count++ <POLL_IDLE_RELAX_COUNT ) -continue ; -drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 10 lines
drivers/cpuidle/poll_state.c +5
-1- loop_count =0 ; +unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } + +smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );Quoted 7 lines
The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true. However, thesmp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself. Also note that the event stream can be disabled on arm64 on the kernel command line.
Quoted 2 lines
Alternately could we condition
arch_haltpoll_want()onarch_timer_evtstrm_available(), like v7?
No. The problem is about the smp_cond_load_relaxed() semantics - it can't wait on a variable that's only updated in its exit condition. We need a new API for this, especially since we are changing generic code here (even it was arm64 code only, I'd still object to such smp_cond_load_*() constructs).
Signature · 1 line
Catalin
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 40 lines · Catalin Marinas
Quoted 39 lines · Haris
Quoted 37 lines · Catalin Marinas
Quoted 18 lines · Ankur Arora
drivers/cpuidle/poll_state.c +0
-5@@ -21,21 +21,20 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -unsigned int loop_count =0 ;u64 limit; limit =cpuidle_poll_time (drv, dev);while (!need_resched ()) { -cpu_relax (); -if (loop_count++ <POLL_IDLE_RELAX_COUNT ) -continue ; -drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 10 lines
drivers/cpuidle/poll_state.c +5
-1- loop_count =0 ; +unsigned int loop_count =0 ;if (local_clock_noinstr () - time_start > limit) { dev->poll_time_limit = true;break ; } + +smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true. However, thesmp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself. Also note that the event stream can be disabled on arm64 on the kernel command line.
Quoted 8 lines
Quoted 2 lines
Alternately could we condition
arch_haltpoll_want()onarch_timer_evtstrm_available(), like v7?No. The problem is about the
smp_cond_load_relaxed()semantics - it can't wait on a variable that's only updated in its exit condition. We need a new API for this, especially since we are changing generic code here (even it was arm64 code only, I'd still object to such smp_cond_load_*() constructs).
Right. The problem is that smp_cond_load_relaxed() used in this context depends on the event-stream side effect when the interface does not encode those semantics anywhere.
So, a smp_cond_load_timeout() like in [1] that continues to depend on the event-stream is better because it explicitly accounts for the side effect from the timeout.
This would cover both the WFxT and the event-stream case.
The part I'm a little less sure about is the case where WFxT and the event-stream are absent.
As you said earlier, for that case on arm64, we use either short __delay() calls or spin in cpu_relax(), both of which are essentially the same thing.
Now on x86 cpu_relax() is quite optimal. The spec explicitly recommends it and from my measurement a loop doing "while (!cond) cpu_relax()" gets an IPC of something like 0.1 or similar.
On my arm64 systems however the same loop gets an IPC of 2. Now this likely varies greatly but seems like it would run pretty hot some of the time.
So maybe the right thing to do would be to keep smp_cond_load_timeout() but only allow polling if WFxT or event-stream is enabled. And enhance cpuidle_poll_state_init() to fail if the above condition is not met.
Does that make sense?
Thanks
Ankur
Catalin Marinas
maintainerRe: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 29 lines · Ankur Arora
Quoted 15 lines · Catalin Marinas
Quoted 14 lines · Haris
Quoted 12 lines · Catalin Marinas
drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 3 lines · Ankur Arora
drivers/cpuidle/poll_state.c +3
-0+smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true. However, thesmp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself. Also note that the event stream can be disabled on arm64 on the kernel command line.Quoted 8 lines
Quoted 2 lines
Alternately could we condition
arch_haltpoll_want()onarch_timer_evtstrm_available(), like v7?No. The problem is about the
smp_cond_load_relaxed()semantics - it can't wait on a variable that's only updated in its exit condition. We need a new API for this, especially since we are changing generic code here (even it was arm64 code only, I'd still object to such smp_cond_load_*() constructs).Right. The problem is that
smp_cond_load_relaxed()used in this context depends on the event-stream side effect when the interface does not encode those semantics anywhere.
Quoted 5 lines
So, a
smp_cond_load_timeout()like in [1] that continues to depend on the event-stream is better because it explicitly accounts for the side effect from the timeout.This would cover both the WFxT and the event-stream case.
Indeed.
Quoted 6 lines
The part I'm a little less sure about is the case where WFxT and the event-stream are absent.
As you said earlier, for that case on arm64, we use either short
__delay()calls or spin incpu_relax(), both of which are essentially the same thing.
Something derived from __delay(), not exactly this function. We can't use it directly as we also want it to wake up if an event is generated as a result of a memory write (like the current smp_cond_load().
Quoted 7 lines
Now on x86
cpu_relax()is quite optimal. The spec explicitly recommends it and from my measurement a loop doing "while (!cond)cpu_relax()" gets an IPC of something like 0.1 or similar.On my arm64 systems however the same loop gets an IPC of 2. Now this likely varies greatly but seems like it would run pretty hot some of the time.
For the cpu_relax() fall-back, it wouldn't be any worse than the current poll_idle() code, though I guess in this instance we'd not enable idle polling.
I expect the event stream to be on in all production deployments. The reason we have a way to disable it is for testing. We've had hardware errata in the past where the event on spin_unlock doesn't cross the cluster boundary. We'd not notice because of the event stream.
Quoted 3 lines
So maybe the right thing to do would be to keep
smp_cond_load_timeout()but only allow polling if WFxT or event-stream is enabled. And enhancecpuidle_poll_state_init()to fail if the above condition is not met.
We could do this as well. Maybe hide this behind another function like arch_has_efficient_smp_cond_load_timeout() (well, some shorter name), checked somewhere in or on the path to cpuidle_poll_state_init(). Well, it might be simpler to do this in haltpoll_want(), backed by an arch_haltpoll_want() function.
I assume we want poll_idle() to wake up as soon as a task becomes available. Otherwise we could have just used udelay() for some fraction of cpuidle_poll_time() instead of cpu_relax().
Signature · 1 line
Catalin
Re: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 57 lines · Catalin Marinas
Quoted 29 lines · Ankur Arora
Quoted 15 lines · Catalin Marinas
Quoted 14 lines · Haris
Quoted 12 lines · Catalin Marinas
drivers/cpuidle/poll_state.c static int __cpuidle poll_idle(struct cpuidle_device *dev, · line 37
Quoted 3 lines · Ankur Arora
drivers/cpuidle/poll_state.c +3
-0+smp_cond_load_relaxed (¤t_thread_info ()->flags, +VAL & _TIF_NEED_RESCHED || + loop_count++ >=POLL_IDLE_RELAX_COUNT );The above is not guaranteed to make progress if
_TIF_NEED_RESCHEDis never set. With the event stream enabled on arm64, the WFE will eventually be woken up,loop_countincremented and the condition would become true. However, thesmp_cond_load_relaxed()semantics require that a different agent updates the variable being waited on, not the waiting CPU updating it itself. Also note that the event stream can be disabled on arm64 on the kernel command line.Quoted 8 lines
Quoted 2 lines
Alternately could we condition
arch_haltpoll_want()onarch_timer_evtstrm_available(), like v7?No. The problem is about the
smp_cond_load_relaxed()semantics - it can't wait on a variable that's only updated in its exit condition. We need a new API for this, especially since we are changing generic code here (even it was arm64 code only, I'd still object to such smp_cond_load_*() constructs).Right. The problem is that
smp_cond_load_relaxed()used in this context depends on the event-stream side effect when the interface does not encode those semantics anywhere.Quoted 5 lines
So, a
smp_cond_load_timeout()like in [1] that continues to depend on the event-stream is better because it explicitly accounts for the side effect from the timeout.This would cover both the WFxT and the event-stream case.
Indeed.
Quoted 6 lines
The part I'm a little less sure about is the case where WFxT and the event-stream are absent.
As you said earlier, for that case on arm64, we use either short
__delay()calls or spin incpu_relax(), both of which are essentially the same thing.Something derived from
__delay(), not exactly this function. We can't use it directly as we also want it to wake up if an event is generated as a result of a memory write (like the currentsmp_cond_load().Quoted 7 lines
Now on x86
cpu_relax()is quite optimal. The spec explicitly recommends it and from my measurement a loop doing "while (!cond)cpu_relax()" gets an IPC of something like 0.1 or similar.On my arm64 systems however the same loop gets an IPC of 2. Now this likely varies greatly but seems like it would run pretty hot some of the time.
Quoted 8 lines
For the
cpu_relax()fall-back, it wouldn't be any worse than the currentpoll_idle()code, though I guess in this instance we'd not enable idle polling.I expect the event stream to be on in all production deployments. The reason we have a way to disable it is for testing. We've had hardware errata in the past where the event on
spin_unlockdoesn't cross the cluster boundary. We'd not notice because of the event stream.
Ah, interesting. Thanks, that helps.
Quoted 9 lines
Quoted 3 lines
So maybe the right thing to do would be to keep
smp_cond_load_timeout()but only allow polling if WFxT or event-stream is enabled. And enhancecpuidle_poll_state_init()to fail if the above condition is not met.We could do this as well. Maybe hide this behind another function like
arch_has_efficient_smp_cond_load_timeout()(well, some shorter name), checked somewhere in or on the path tocpuidle_poll_state_init(). Well, it might be simpler to do this inhaltpoll_want(), backed by anarch_haltpoll_want()function.
Yeah, checking in arch_haltpoll_want() would mean that we can leave all the cpuidle_poll_state_init() call sites unchanged.
However, I suspect that even acpi-idle on arm64 might end up using poll_idle() (as this patch tries to do:
https://lore.kernel.org/lkml/[email protected]/).
So, let me try doing it both ways to see which one is simpler. Given that the event-stream can be assumed to be always-on it might just be more straight-forward to fallback to cpu_relax() in that edge case.
Quoted 3 lines
I assume we want
poll_idle()to wake up as soon as a task becomes available. Otherwise we could have just usedudelay()for some fraction ofcpuidle_poll_time()instead ofcpu_relax().
Yeah, agreed.
Thanks
--
ankur
Catalin Marinas
maintainerRe: [PATCH v8 01/11] cpuidle/poll_state: poll via smp_cond_load_relaxed()
Quoted 15 lines · Ankur Arora
Quoted 10 lines · Catalin Marinas
Quoted 3 lines · Ankur Arora
So maybe the right thing to do would be to keep
smp_cond_load_timeout()but only allow polling if WFxT or event-stream is enabled. And enhancecpuidle_poll_state_init()to fail if the above condition is not met.We could do this as well. Maybe hide this behind another function like
arch_has_efficient_smp_cond_load_timeout()(well, some shorter name), checked somewhere in or on the path tocpuidle_poll_state_init(). Well, it might be simpler to do this inhaltpoll_want(), backed by anarch_haltpoll_want()function.Yeah, checking in
arch_haltpoll_want()would mean that we can leave all thecpuidle_poll_state_init()call sites unchanged.
Quoted 7 lines
However, I suspect that even acpi-idle on arm64 might end up using
poll_idle()(as this patch tries to do:
https://lore.kernel.org/lkml/[email protected]/).So, let me try doing it both ways to see which one is simpler. Given that the event-stream can be assumed to be always-on it might just be more straight-forward to fallback to
cpu_relax()in that edge case.
I agree, let's go with the simplest. If one has some strong case for running with the event stream disabled and idle polling becomes too energy inefficient, we can revisit and add some run-time checks.
Signature · 1 line
Catalin
PATCH v8 2/11 cpuidle: rename ARCH_HAS_CPU_RELAX to ARCH_HAS_OPTIMIZED_POLL
In reply to Ankur AroraARCH_HAS_CPU_RELAX is defined on architectures that provide an primitive (via cpu_relax()) that can be used as part of a polling mechanism -- one that would be cheaper than spinning in a tight loop.
However, recent changes in poll_idle() mean that a higher level primitive -- smp_cond_load_relaxed() is used for polling. This would in-turn use cpu_relax() or an architecture specific implementation. On ARM64 in particular this turns into a WFE which waits on a store to a cacheline instead of a busy poll.
Accordingly condition the polling drivers on ARCH_HAS_OPTIMIZED_POLL instead of ARCH_HAS_CPU_RELAX. While at it, make both intel-idle and cpuidle-haltpoll explicitly depend on ARCH_HAS_CPU_RELAX.
--- arch/x86/Kconfig | 2 +- drivers/acpi/processor_idle.c | 4 ++-- drivers/cpuidle/Kconfig | 2 +- drivers/cpuidle/Makefile | 2 +- drivers/idle/Kconfig | 1 + include/linux/cpuidle.h | 2 +- 6 files changed, 7 insertions(+), 6 deletions(-)
arch/x86/Kconfig +1 -1
@@ -378,7 +378,7 @@ config ARCH_MAY_HAVE_PC_FDC config GENERIC_CALIBRATE_DELAY def_bool y -config ARCH_HAS_CPU_RELAX +config ARCH_HAS_OPTIMIZED_POLL def_bool y config ARCH_HIBERNATION_POSSIBLE
drivers/acpi/processor_idle.c +2 -2
@@ -35,7 +35,7 @@#include <asm/cpu.h> #endif -#define ACPI_IDLE_STATE_START (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX) ? 1 : 0) +#define ACPI_IDLE_STATE_START (IS_ENABLED(CONFIG_ARCH_HAS_OPTIMIZED_POLL) ? 1 : 0)static unsigned int max_cstate__read_mostly =ACPI_PROCESSOR_MAX_POWER ;module_param (max_cstate, uint,0400 );
@@ -782,7 +782,7 @@ static int acpi_processor_setup_cstates(struct acpi_processor *pr)if (max_cstate ==0 ) max_cstate =1 ; -if (IS_ENABLED (CONFIG_ARCH_HAS_CPU_RELAX )) { +if (IS_ENABLED (CONFIG_ARCH_HAS_OPTIMIZED_POLL )) {cpuidle_poll_state_init (drv); count =1 ; }else {
drivers/cpuidle/Kconfig +1 -1
@@ -73,7 +73,7 @@ endmenu config HALTPOLL_CPUIDLE tristate "Halt poll cpuidle driver" - depends on X86 && KVM_GUEST + depends on X86 && KVM_GUEST && ARCH_HAS_OPTIMIZED_POLL select CPU_IDLE_GOV_HALTPOLL default y help
drivers/cpuidle/Makefile +1 -1
@@ -7,7 +7,7 @@ obj-y += cpuidle.o driver.o governor.o sysfs.o governors/ obj-$(CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED) += coupled.o obj-$(CONFIG_DT_IDLE_STATES) += dt_idle_states.o obj-$(CONFIG_DT_IDLE_GENPD) += dt_idle_genpd.o -obj-$(CONFIG_ARCH_HAS_CPU_RELAX) += poll_state.o +obj-$(CONFIG_ARCH_HAS_OPTIMIZED_POLL) += poll_state.o obj-$(CONFIG_HALTPOLL_CPUIDLE) += cpuidle-haltpoll.o ##################################################################################
drivers/idle/Kconfig +1 -0
@@ -4,6 +4,7 @@ config INTEL_IDLE depends on CPU_IDLE depends on X86 depends on CPU_SUP_INTEL + depends on ARCH_HAS_OPTIMIZED_POLL help Enable intel_idle, a cpuidle driver that includes knowledge of native Intel hardware idle features. The acpi_idle driver
include/linux/cpuidle.h +1 -1
@@ -275,7 +275,7 @@ static inline void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, }#endif -#if defined(CONFIG_CPU_IDLE ) && defined(CONFIG_ARCH_HAS_CPU_RELAX ) +#if defined(CONFIG_CPU_IDLE ) && defined(CONFIG_ARCH_HAS_OPTIMIZED_POLL )void cpuidle_poll_state_init (struct cpuidle_driver *drv);#else static inline void cpuidle_poll_state_init (struct cpuidle_driver *drv) {}
Signature · 1 line
2.43.5
PATCH v8 3/11 Kconfig: move ARCH_HAS_OPTIMIZED_POLL to arch/Kconfig
In reply to Ankur AroraFrom: Joao Martins <[email protected]>
ARCH_HAS_OPTIMIZED_POLL gates selection of polling while idle in poll_idle(). Move the configuration option to arch/Kconfig to allow non-x86 architectures to select it.
Note that ARCH_HAS_OPTIMIZED_POLL should probably be exclusive with GENERIC_IDLE_POLL_SETUP (which controls the generic polling logic in cpu_idle_poll()). However, that would remove boot options (hlt=, nohlt=). So, leave it untouched for now.
--- arch/Kconfig | 3 +++ arch/x86/Kconfig | 4 +--- 2 files changed, 4 insertions(+), 3 deletions(-)
arch/Kconfig +3 -0
@@ -273,6 +273,9 @@ config HAVE_ARCH_TRACEHOOK config HAVE_DMA_CONTIGUOUS bool +config ARCH_HAS_OPTIMIZED_POLL + bool + config GENERIC_SMP_IDLE_THREAD bool
arch/x86/Kconfig +1 -3
@@ -138,6 +138,7 @@ config X86 select ARCH_WANTS_NO_INSTR select ARCH_WANT_GENERAL_HUGETLB select ARCH_WANT_HUGE_PMD_SHARE + select ARCH_HAS_OPTIMIZED_POLL select ARCH_WANT_LD_ORPHAN_WARN select ARCH_WANT_OPTIMIZE_DAX_VMEMMAP if X86_64 select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if X86_64
@@ -378,9 +379,6 @@ config ARCH_MAY_HAVE_PC_FDC config GENERIC_CALIBRATE_DELAY def_bool y -config ARCH_HAS_OPTIMIZED_POLL - def_bool y - config ARCH_HIBERNATION_POSSIBLE def_bool y
Signature · 1 line
2.43.5
PATCH v8 4/11 cpuidle-haltpoll: define arch_haltpoll_want()
In reply to Ankur AroraFrom: Joao Martins <[email protected]>
While initializing haltpoll we check if KVM supports the realtime hint and if idle is overridden at boot.
Both of these checks are x86 specific. So, in pursuit of making cpuidle-haltpoll architecture independent, move these checks out of common code.
--- arch/x86/include/asm/cpuidle_haltpoll.h | 1 + arch/x86/kernel/kvm.c | 13 +++++++++++++ drivers/cpuidle/cpuidle-haltpoll.c | 12 +----------- include/linux/cpuidle_haltpoll.h | 5 +++++ 4 files changed, 20 insertions(+), 11 deletions(-)
arch/x86/include/asm/cpuidle_haltpoll.h +1 -0
@@ -4,5 +4,6 @@void arch_haltpoll_enable (unsigned int cpu);void arch_haltpoll_disable (unsigned int cpu); +bool arch_haltpoll_want (bool force);#endif
arch/x86/kernel/kvm.c +13 -0
@@ -1151,4 +1151,17 @@ void arch_haltpoll_disable(unsigned int cpu) smp_call_function_single(cpu, kvm_enable_host_haltpoll, NULL, 1); }EXPORT_SYMBOL_GPL (arch_haltpoll_disable); + +bool arch_haltpoll_want (bool force) +{ +/* Do not load haltpoll if idle= is passed */ +if (boot_option_idle_override !=IDLE_NO_OVERRIDE ) +return false; + +if (!kvm_para_available ()) +return false; + +return kvm_para_has_hint (KVM_HINTS_REALTIME ) || force; +} +EXPORT_SYMBOL_GPL (arch_haltpoll_want);#endif
drivers/cpuidle/cpuidle-haltpoll.c +1 -11
@@ -15,7 +15,6 @@#include <linux/cpuidle.h> #include <linux/module.h> #include <linux/sched/idle.h> -#include <linux/kvm_para.h> #include <linux/cpuidle_haltpoll.h> static bool force__read_mostly ;
@@ -93,21 +92,12 @@ static void haltpoll_uninit(void) haltpoll_cpuidle_devices =NULL ; } -static bool haltpoll_want (void ) -{ -return kvm_para_has_hint (KVM_HINTS_REALTIME ) || force; -} -static int __init haltpoll_init (void ) {int ret;struct cpuidle_driver *drv = &haltpoll_driver; -/* Do not load haltpoll if idle= is passed */ -if (boot_option_idle_override !=IDLE_NO_OVERRIDE ) -return -ENODEV ; - -if (!kvm_para_available () || !haltpoll_want ()) +if (!arch_haltpoll_want (force))return -ENODEV ;cpuidle_poll_state_init (drv);
include/linux/cpuidle_haltpoll.h +5 -0
@@ -12,5 +12,10 @@ static inline void arch_haltpoll_enable(unsigned int cpu)static inline void arch_haltpoll_disable (unsigned int cpu) { } + +static inline bool arch_haltpoll_want (bool force) +{ +return false; +}#endif #endif
Signature · 1 line
2.43.5
PATCH v8 5/11 governors/haltpoll: drop kvm_para_available() check
In reply to Ankur AroraFrom: Joao Martins <[email protected]>
The haltpoll governor is selected either by the cpuidle-haltpoll driver, or explicitly by the user.
In particular, it is never selected by default since it has the lowest rating of all governors (menu=20, teo=19, ladder=10/25, haltpoll=9).
So, we can safely forgo the kvm_para_available() check. This also allows cpuidle-haltpoll to be tested on baremetal.
--- drivers/cpuidle/governors/haltpoll.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/cpuidle/governors/haltpoll.c +1 -5
@@ -18,7 +18,6 @@#include <linux/tick.h> #include <linux/sched.h> #include <linux/module.h> -#include <linux/kvm_para.h> #include <trace/events/power.h> static unsigned int guest_halt_poll_ns__read_mostly =200000 ;
@@ -148,10 +147,7 @@ static struct cpuidle_governor haltpoll_governor = {static int __init init_haltpoll (void ) { -if (kvm_para_available ()) -return cpuidle_register_governor (&haltpoll_governor); - -return 0 ; +return cpuidle_register_governor (&haltpoll_governor); }postcore_initcall (init_haltpoll);
Signature · 1 line
2.43.5
PATCH v8 6/11 cpuidle-haltpoll: condition on ARCH_CPUIDLE_HALTPOLL
In reply to Ankur AroraThe cpuidle-haltpoll driver and its namesake governor are selected under KVM_GUEST on X86. KVM_GUEST in-turn selects ARCH_CPUIDLE_HALTPOLL and defines the requisite arch_haltpoll_{enable,disable}() functions.
So remove the explicit dependence of HALTPOLL_CPUIDLE on KVM_GUEST, and instead use ARCH_CPUIDLE_HALTPOLL as proxy for architectural support for haltpoll.
Also change "halt poll" to "haltpoll" in one of the summary clauses, since the second form is used everywhere else.
--- arch/x86/Kconfig | 1 + drivers/cpuidle/Kconfig | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/Kconfig +1 -0
@@ -844,6 +844,7 @@ config KVM_GUEST config ARCH_CPUIDLE_HALTPOLL def_bool n + depends on KVM_GUEST prompt "Disable host haltpoll when loading haltpoll driver" help If virtualized under KVM, disable host haltpoll.
drivers/cpuidle/Kconfig +2 -3
@@ -35,7 +35,6 @@ config CPU_IDLE_GOV_TEO config CPU_IDLE_GOV_HALTPOLL bool "Haltpoll governor (for virtualized systems)" - depends on KVM_GUEST help This governor implements haltpoll idle state selection, to be used in conjunction with the haltpoll cpuidle driver, allowing
@@ -72,8 +71,8 @@ source "drivers/cpuidle/Kconfig.riscv" endmenu config HALTPOLL_CPUIDLE - tristate "Halt poll cpuidle driver" - depends on X86 && KVM_GUEST && ARCH_HAS_OPTIMIZED_POLL + tristate "Haltpoll cpuidle driver" + depends on ARCH_CPUIDLE_HALTPOLL && ARCH_HAS_OPTIMIZED_POLL select CPU_IDLE_GOV_HALTPOLL default y help
Signature · 1 line
2.43.5
PATCH v8 7/11 arm64: define TIF_POLLING_NRFLAG
In reply to Ankur AroraFrom: Joao Martins <[email protected]>
Commit 842514849a61 ("arm64: Remove TIF_POLLING_NRFLAG") had removed TIF_POLLING_NRFLAG because arm64 only supported non-polled idling via cpu_do_idle().
To add support for polling via cpuidle-haltpoll, we want to use the standard poll_idle() interface, which sets TIF_POLLING_NRFLAG while polling.
Reuse the same bit to define TIF_POLLING_NRFLAG.
--- arch/arm64/include/asm/thread_info.h | 2 ++ 1 file changed, 2 insertions(+)
arch/arm64/include/asm/thread_info.h +2 -0
@@ -69,6 +69,7 @@ void arch_setup_new_exec(void);#define TIF_SYSCALL_TRACEPOINT 10/* syscall tracepoint for ftrace */ #define TIF_SECCOMP 11/* syscall secure computing */ #define TIF_SYSCALL_EMU 12/* syscall emulation active */ +#define TIF_POLLING_NRFLAG 16/* set while polling in poll_idle() */ #define TIF_MEMDIE 18/* is terminating due to OOM killer */ #define TIF_FREEZE 19#define TIF_RESTORE_SIGMASK 20
@@ -92,6 +93,7 @@ void arch_setup_new_exec(void);#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)#define _TIF_SECCOMP (1 << TIF_SECCOMP)#define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU) +#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)#define _TIF_UPROBE (1 << TIF_UPROBE)#define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP)#define _TIF_32BIT (1 << TIF_32BIT)
Signature · 1 line
2.43.5
PATCH v8 8/11 arm64: idle: export arch_cpu_idle
In reply to Ankur AroraNeeded for cpuidle-haltpoll.
--- arch/arm64/kernel/idle.c | 1 + 1 file changed, 1 insertion(+)
arch/arm64/kernel/idle.c +1 -0
@@ -43,3 +43,4 @@ void __cpuidle arch_cpu_idle(void)*/ cpu_do_idle (); } +EXPORT_SYMBOL_GPL (arch_cpu_idle);
Signature · 1 line
2.43.5
PATCH v8 9/11 arm64: select ARCH_HAS_OPTIMIZED_POLL
In reply to Ankur Arorapoll_idle() uses smp_cond_load*() as its polling mechanism. arm64 supports an optimized version of this via LDXR, WFE, with LDXR loading a memory region in exclusive state and the WFE waiting for any stores to it.
Select ARCH_HAS_OPTIMIZED_POLL so poll_idle() can be used.
--- arch/arm64/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/arm64/Kconfig +1 -0
@@ -38,6 +38,7 @@ config ARM64 select ARCH_HAS_MEM_ENCRYPT select ARCH_HAS_NMI_SAFE_THIS_CPU_OPS select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE + select ARCH_HAS_OPTIMIZED_POLL select ARCH_HAS_PTE_DEVMAP select ARCH_HAS_PTE_SPECIAL select ARCH_HAS_HW_PTE_YOUNG
Signature · 1 line
2.43.5
Re: [PATCH v8 09/11] arm64: select ARCH_HAS_OPTIMIZED_POLL
Quoted 6 lines · Ankur Arora
poll_idle()uses smp_cond_load*() as its polling mechanism. arm64 supports an optimized version of this via LDXR, WFE, with LDXR loading a memory region in exclusive state and the WFE waiting for any stores to it.Select
ARCH_HAS_OPTIMIZED_POLLsopoll_idle()can be used.
PATCH v8 10/11 cpuidle/poll_state: limit POLL_IDLE_RELAX_COUNT on arm64
In reply to Ankur Arorasmp_cond_load_relaxed(), in its generic polling variant, polls on the loop condition waiting for it to change, eventually exiting the loop if the time limit has been exceeded.
To limit the frequency of the relatively expensive time check it is limited to once every POLL_IDLE_RELAX_COUNT iterations.
arm64, however, uses an event based mechanism where instead of polling, we wait for a store to a region.
Limit the POLL_IDLE_RELAX_COUNT to 1 for that case.
--- drivers/cpuidle/poll_state.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/cpuidle/poll_state.c +11 -0
@@ -8,7 +8,18 @@#include <linux/sched/clock.h> #include <linux/sched/idle.h> +#ifdef CONFIG_ARM64 +/* +* POLL_IDLE_RELAX_COUNT determines how often we check for timeout +* while polling for TIF_NEED_RESCHED in thread_info->flags. +* +* Set this to a low value since arm64, instead of polling, uses a +* event based mechanism. +*/ +#define POLL_IDLE_RELAX_COUNT 1 +#else #define POLL_IDLE_RELAX_COUNT 200 +#endif static int __cpuidle poll_idle(struct cpuidle_device *dev,struct cpuidle_driver *drv,int index)
Signature · 1 line
2.43.5
PATCH v8 11/11 arm64: support cpuidle-haltpoll
In reply to Ankur AroraAdd architectural support for the cpuidle-haltpoll driver by defining arch_haltpoll_*(). Also define ARCH_CPUIDLE_HALTPOLL to allow cpuidle-haltpoll to be selected.
Haltpoll uses poll_idle() to do the actual polling. This in turn uses smp_cond_load*() to wait until there's a specific store to a cacheline.
In the edge case -- no stores to the cacheline and no interrupt -- the event-stream provides the terminating condition ensuring we don't wait forever. But because the event-stream runs at a fixed frequency (configured at 10kHz) haltpoll might spend more time in the polling stage than specified by cpuidle_poll_time().
This would only happen in the last iteration, since overshooting the poll_limit means the governor will move out of the polling stage.
--- arch/arm64/Kconfig | 6 ++++++ arch/arm64/include/asm/cpuidle_haltpoll.h | 24 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 arch/arm64/include/asm/cpuidle_haltpoll.h
arch/arm64/Kconfig +6 -0
@@ -2415,6 +2415,12 @@ config ARCH_HIBERNATION_HEADER config ARCH_SUSPEND_POSSIBLE def_bool y +config ARCH_CPUIDLE_HALTPOLL + bool "Enable selection of the cpuidle-haltpoll driver" + help + cpuidle-haltpoll allows for adaptive polling based on + current load before entering the idle state. + endmenu # "Power management options" menu "CPU Power Management"
arch/arm64/include/asm/cpuidle_haltpoll.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _ARCH_HALTPOLL_H +#define _ARCH_HALTPOLL_H + +static inline void arch_haltpoll_enable (unsigned int cpu) { } +static inline void arch_haltpoll_disable (unsigned int cpu) { } + +static inline bool arch_haltpoll_want (bool force) +{ +/* +* Enabling haltpoll requires two things: +* +* - Event stream support to provide a terminating condition to the +* WFE in the poll loop. +* +* - KVM support for arch_haltpoll_enable(), arch_haltpoll_disable(). +* +* Given that the second is missing, only allow force loading for +* haltpoll. +*/ +return force; +} +#endif
Signature · 1 line
2.43.5
Re: [PATCH v8 11/11] arm64: support cpuidle-haltpoll
Quoted 77 lines · Ankur Arora
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Add architectural support for the cpuidle-haltpoll driver by defining arch_haltpoll_*(). Also define
ARCH_CPUIDLE_HALTPOLLto allow cpuidle-haltpoll to be selected.Haltpoll uses
poll_idle()to do the actual polling. This in turn uses smp_cond_load*() to wait until there's a specific store to a cacheline.
In the edge case -- no stores to the cacheline and no interrupt -- the event-stream provides the terminating condition ensuring we don't wait forever. But because the event-stream runs at a fixed frequency (configured at 10kHz) haltpoll might spend more time in the polling stage than specified bycpuidle_poll_time().This would only happen in the last iteration, since overshooting the
poll_limitmeans the governor will move out of the polling stage.Tested-by Haris Okanovic <[email protected]>Tested-by Misono Tomohiro <[email protected]>Signed-off-by Ankur Arora <[email protected]>--- arch/arm64/Kconfig | 6 ++++++ arch/arm64/include/asm/cpuidle_haltpoll.h | 24 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 arch/arm64/include/asm/cpuidle_haltpoll.harch/arm64/Kconfig +6
-0@@ -2415,6 +2415,12 @@ config ARCH_HIBERNATION_HEADER config ARCH_SUSPEND_POSSIBLE def_bool y +config ARCH_CPUIDLE_HALTPOLL + bool "Enable selection of the cpuidle-haltpoll driver" + help + cpuidle-haltpoll allows for adaptive polling based on + current load before entering the idle state. + endmenu # "Power management options" menu "CPU Power Management"arch/arm64/include/asm/cpuidle_haltpoll.h new file +24
-1@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _ARCH_HALTPOLL_H +#define _ARCH_HALTPOLL_H + +static inline void arch_haltpoll_enable (unsigned int cpu) { } +static inline void arch_haltpoll_disable (unsigned int cpu) { } + +static inline bool arch_haltpoll_want (bool force) +{ +/* +* Enabling haltpoll requires two things: +* +* - Event stream support to provide a terminating condition to the +* WFE in the poll loop. +* +* - KVM support for arch_haltpoll_enable(), arch_haltpoll_disable(). +* +* Given that the second is missing, only allow force loading for +* haltpoll. +*/ +return force; +} +#endif --
Quoted 2 lines
2.43.5
I applied your patches to master e32cde8d2bd7 and verified same performance gains on AWS Graviton.
Re: [PATCH v8 11/11] arm64: support cpuidle-haltpoll
Quoted 75 lines · Haris
Quoted 74 lines · Ankur Arora
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Add architectural support for the cpuidle-haltpoll driver by defining arch_haltpoll_*(). Also define
ARCH_CPUIDLE_HALTPOLLto allow cpuidle-haltpoll to be selected.Haltpoll uses
poll_idle()to do the actual polling. This in turn uses smp_cond_load*() to wait until there's a specific store to a cacheline.
In the edge case -- no stores to the cacheline and no interrupt -- the event-stream provides the terminating condition ensuring we don't wait forever. But because the event-stream runs at a fixed frequency (configured at 10kHz) haltpoll might spend more time in the polling stage than specified bycpuidle_poll_time().This would only happen in the last iteration, since overshooting the
poll_limitmeans the governor will move out of the polling stage.Tested-by Haris Okanovic <[email protected]>Tested-by Misono Tomohiro <[email protected]>Signed-off-by Ankur Arora <[email protected]>--- arch/arm64/Kconfig | 6 ++++++ arch/arm64/include/asm/cpuidle_haltpoll.h | 24 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 arch/arm64/include/asm/cpuidle_haltpoll.harch/arm64/Kconfig +6
-0@@ -2415,6 +2415,12 @@ config ARCH_HIBERNATION_HEADER config ARCH_SUSPEND_POSSIBLE def_bool y +config ARCH_CPUIDLE_HALTPOLL + bool "Enable selection of the cpuidle-haltpoll driver" + help + cpuidle-haltpoll allows for adaptive polling based on + current load before entering the idle state. + endmenu # "Power management options" menu "CPU Power Management"arch/arm64/include/asm/cpuidle_haltpoll.h new file +22
-0@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _ARCH_HALTPOLL_H +#define _ARCH_HALTPOLL_H + +static inline void arch_haltpoll_enable (unsigned int cpu) { } +static inline void arch_haltpoll_disable (unsigned int cpu) { } + +static inline bool arch_haltpoll_want (bool force) +{ +/* +* Enabling haltpoll requires two things: +* +* - Event stream support to provide a terminating condition to the +* WFE in the poll loop. +* +* - KVM support for arch_haltpoll_enable(), arch_haltpoll_disable(). +* +* Given that the second is missing, only allow force loading for +* haltpoll. +*/ +return force;
Quoted 8 lines
Quoted 5 lines
arch/arm64/include/asm/cpuidle_haltpoll.h new file +2
-1+} +#endif --2.43.5
I applied your patches to master
e32cde8d2bd7and verified same performance gains on AWS Graviton.
Great.
Quoted 2 lines
Reviewed-by Haris Okanovic <[email protected]>Tested-by Haris Okanovic <[email protected]>
Thanks!
--
ankur
Christoph Lameter (Ampere)
Reviewed-byRe: [PATCH v8 11/11] arm64: support cpuidle-haltpoll
Re: [PATCH v8 11/11] arm64: support cpuidle-haltpoll
Quoted 1 line · Christoph Lameter (Ampere)
Reviewed-by Christoph Lameter (Ampere) <[email protected]>
Thanks Christoph.
--
ankur
Re: [PATCH v8 11/11] arm64: support cpuidle-haltpoll
In reply to Ankur AroraQuoted 57 lines · Ankur Arora
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Add architectural support for the cpuidle-haltpoll driver by defining arch_haltpoll_*(). Also define
ARCH_CPUIDLE_HALTPOLLto allow cpuidle-haltpoll to be selected.Haltpoll uses
poll_idle()to do the actual polling. This in turn uses smp_cond_load*() to wait until there's a specific store to a cacheline.
In the edge case -- no stores to the cacheline and no interrupt -- the event-stream provides the terminating condition ensuring we don't wait forever. But because the event-stream runs at a fixed frequency (configured at 10kHz) haltpoll might spend more time in the polling stage than specified bycpuidle_poll_time().This would only happen in the last iteration, since overshooting the
poll_limitmeans the governor will move out of the polling stage.Tested-by Haris Okanovic <[email protected]>Tested-by Misono Tomohiro <[email protected]>Signed-off-by Ankur Arora <[email protected]>--- arch/arm64/Kconfig | 6 ++++++ arch/arm64/include/asm/cpuidle_haltpoll.h | 24 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 arch/arm64/include/asm/cpuidle_haltpoll.harch/arm64/Kconfig +6
-0@@ -2415,6 +2415,12 @@ config ARCH_HIBERNATION_HEADER config ARCH_SUSPEND_POSSIBLE def_bool y +config ARCH_CPUIDLE_HALTPOLL + bool "Enable selection of the cpuidle-haltpoll driver" + help + cpuidle-haltpoll allows for adaptive polling based on + current load before entering the idle state. + endmenu # "Power management options" menu "CPU Power Management"arch/arm64/include/asm/cpuidle_haltpoll.h new file +5
-0@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _ARCH_HALTPOLL_H +#define _ARCH_HALTPOLL_H +
arch/arm64/include/asm/cpuidle_haltpoll.h line 15
Quoted 10 lines
arch/arm64/include/asm/cpuidle_haltpoll.h new file +10
-0+static inline void arch_haltpoll_enable (unsigned int cpu) { } +static inline void arch_haltpoll_disable (unsigned int cpu) { } + +static inline bool arch_haltpoll_want (bool force) +{ +/* +* Enabling haltpoll requires two things: +* +* - Event stream support to provide a terminating condition to the +* WFE in the poll loop.
I missed this earlier: Why did you drop arch_timer_evtstrm_available()?
Quoted 12 lines
arch/arm64/include/asm/cpuidle_haltpoll.h new file +9
-1+* +* - KVM support for arch_haltpoll_enable(), arch_haltpoll_disable(). +* +* Given that the second is missing, only allow force loading for +* haltpoll. +*/ + return force; +} +#endif --2.43.5
Quoted 142 lines · Ankur Arora
This patchset enables the cpuidle-haltpoll driver and its namesake governor on arm64. This is specifically interesting for KVM guests by reducing IPC latencies.
Comparing idle switching latencies on an arm64 KVM guest with perf bench sched pipe:
usecs/op %stdev
no haltpoll (baseline) 13.48 +- 5.19%
with haltpoll 6.84 +- 22.07%No change in performance for a similar test on x86:
usecs/op %stdev
haltpoll w/ cpu_relax() (baseline) 4.75 +- 1.76% haltpoll w/ smp_cond_load_relaxed() 4.78 +- 2.31%Both sets of tests were on otherwise idle systems with guest VCPUs pinned to specific PCPUs. One reason for the higher stdev on arm64 is that trapping of the WFE instruction by the host KVM is contingent on the number of tasks on the runqueue.
Tomohiro Misono and Haris Okanovic also report similar latency improvements on Grace and Graviton systems (for v7) [1] [2].
The patch series is organized in three parts:
- patch 1, reorganizes the
poll_idle()loop, switching tosmp_cond_load_relaxed()in the polling loop.
Relatedly patches 2, 3 mangle the config optionARCH_HAS_CPU_RELAX, renaming it toARCH_HAS_OPTIMIZED_POLL.- patches 4-6 reorganize the haltpoll selection and init logic to allow architecture code to select it.
- and finally, patches 7-11 add the bits for arm64 support.
What is still missing: this series largely completes the haltpoll side of functionality for arm64. There are, however, a few related areas that still need to be threshed out:
- WFET support: WFE on arm64 does not guarantee that
poll_idle()would terminate inhalt_poll_ns. Using WFET would address this.
-KVM_NO_POLLsupport on arm64
- KVM TWED support on arm64: allow the host to limit time spent in WFE.Changelog:
v8: No logic changes. Largely respin of v7, with changes noted below:
- move selection of
ARCH_HAS_OPTIMIZED_POLLon arm64 to its own patch.
(patch-9 "arm64: selectARCH_HAS_OPTIMIZED_POLL")- address comments simplifying arm64 support (Will Deacon) (patch-11 "arm64: support cpuidle-haltpoll")
v7: No significant logic changes. Mostly a respin of v6.
- minor cleanup in
poll_idle()(Christoph Lameter)
- fixes conflicts due to code movement inarch/arm64/kernel/cpuidle.c(Tomohiro Misono)v6:
- reordered the patches to keep
poll_idle()andARCH_HAS_OPTIMIZED_POLLchanges together (comment from Christoph Lameter)
- threshes out the commit messages a bit more (comments from Christoph Lameter, Sudeep Holla)
- also rework selection of cpuidle-haltpoll. Now selected based on the architectural selection ofARCH_CPUIDLE_HALTPOLL.
- moved back toarch_haltpoll_want()(comment from Joao Martins) Also,arch_haltpoll_want()now takes the force parameter and is now responsible for the complete selection (or not) of haltpoll.
- fixes the build breakage on i386
- fixes the cpuidle-haltpoll module breakage on arm64 (comment from Tomohiro Misono, Haris Okanovic)v5:
- rework thepoll_idle()loop aroundsmp_cond_load_relaxed()(review comment from Tomohiro Misono.)
- also rework selection of cpuidle-haltpoll. Now selected based on the architectural selection ofARCH_CPUIDLE_HALTPOLL.
-arch_haltpoll_supported()(renamed fromarch_haltpoll_want()) on arm64 now depends on the event-stream being enabled.
- limitPOLL_IDLE_RELAX_COUNTon arm64 (review comment from Haris Okanovic)
-ARCH_HAS_CPU_RELAXis now renamed toARCH_HAS_OPTIMIZED_POLL.v4 changes from v3:
- change 7/8 per Rafael input: drop the parens and use ret for the final check
- add 8/8 which renames the guard for buildingpoll_statev3 changes from v2:
- fix 1/7 per Petr Mladek - removeARCH_HAS_CPU_RELAXfromarch/x86/Kconfig
- add Ack-by from Rafael Wysocki on 2/7v2 changes from v1:
- added patch 7 where we changecpu_relaxwithsmp_cond_load_relaxedper PeterZ (this improves by 50% at least the CPU cycles consumed in the tests above:10,716,881,137 now vs 14,503,014,257 before)- removed the ifdef from patch 1 per RafaelW
Please review.
[1] https://lore.kernel.org/lkml/TY3PR01MB111481E9B0AF263ACC8EA5D4AE5BA2@TY3PR01MB11148.jpnprd01.prod.outlook.com/
[2] https://lore.kernel.org/lkml/[email protected]/Ankur Arora (6):
cpuidle: renameARCH_HAS_CPU_RELAXtoARCH_HAS_OPTIMIZED_POLL
cpuidle-haltpoll: condition onARCH_CPUIDLE_HALTPOLL
arm64: idle: exportarch_cpu_idle
arm64: selectARCH_HAS_OPTIMIZED_POLLcpuidle/poll_state: limitPOLL_IDLE_RELAX_COUNTon arm64
arm64: support cpuidle-haltpollJoao Martins (4):
Kconfig: moveARCH_HAS_OPTIMIZED_POLLtoarch/Kconfig
cpuidle-haltpoll: definearch_haltpoll_want()governors/haltpoll: dropkvm_para_available()check
arm64: defineTIF_POLLING_NRFLAGMihai Carabas (1):
cpuidle/poll_state: poll viasmp_cond_load_relaxed()arch/Kconfig | 3 +++ arch/arm64/Kconfig | 7 +++++++ arch/arm64/include/asm/cpuidle_haltpoll.h | 24 +++++++++++++++++++++++ arch/arm64/include/asm/thread_info.h | 2 ++ arch/arm64/kernel/idle.c | 1 + arch/x86/Kconfig | 5 ++--- arch/x86/include/asm/cpuidle_haltpoll.h | 1 + arch/x86/kernel/kvm.c | 13 ++++++++++++ drivers/acpi/processor_idle.c | 4 ++-- drivers/cpuidle/Kconfig | 5 ++--- drivers/cpuidle/Makefile | 2 +-
Quoted 9 lines
drivers/cpuidle/cpuidle-haltpoll.c | 12 +----------- drivers/cpuidle/governors/haltpoll.c | 6 +----- drivers/cpuidle/poll_state.c | 22 +++++++++++++++------ drivers/idle/Kconfig | 1 + include/linux/cpuidle.h | 2 +- include/linux/cpuidle_haltpoll.h | 5 +++++ 17 files changed, 83 insertions(+), 32 deletions(-) create mode 100644 arch/arm64/include/asm/cpuidle_haltpoll.h
Hi Ankur,
Thanks for the patches!
We have tested these patches on our machine, with an adaptation of ACPI LPI states rather than c-states.
Include polling state, there would be three states to get in. Comparing idle switching latencies of different state with perf bench sched pipe:
usecs/op %stdev
state0(polling state) 7.36 +- 0.35% state1 8.78 +- 0.46% state2 77.32 +- 5.50%
It turns out that it works on our machine.
The adaptation of ACPI LPI states is shown below as a patch. Feel free to include this patch as part of your series, or I can also send it out after your series being merged.
From: Lifeng Zheng <[email protected]>
ACPI: processor_idle: Support polling state for LPI
Initialize an optional polling state besides LPI states.
Wrap up a new enter method to correctly reflect the actual entered state when the polling state is enabled.
--- drivers/acpi/processor_idle.c | 39 ++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-)
drivers/acpi/processor_idle.c +34 -5
@@ -1194,20 +1194,46 @@ static int acpi_idle_lpi_enter(struct cpuidle_device *dev,return -EINVAL ; } +/* To correctly reflect the entered state if the poll state is enabled. */ +static int acpi_idle_lpi_enter_with_poll_state (struct cpuidle_device *dev, +struct cpuidle_driver *drv,int index) +{ +int entered_state; + +if (unlikely (index <1 )) +return -EINVAL ; + + entered_state =acpi_idle_lpi_enter (dev, drv, index -1 ); +if (entered_state <0 ) +return entered_state; + +return entered_state +1 ; +} +static int acpi_processor_setup_lpi_states (struct acpi_processor *pr) { -int i; +int i, count;struct acpi_lpi_state *lpi;struct cpuidle_state *state;struct cpuidle_driver *drv = &acpi_idle_driver; + typeof(state->enter) enter_method;if (!pr->flags.has_lpi)return -EOPNOTSUPP ; +if (IS_ENABLED (CONFIG_ARCH_HAS_OPTIMIZED_POLL )) { +cpuidle_poll_state_init (drv); + count =1 ; + enter_method = acpi_idle_lpi_enter_with_poll_state; + }else { + count =0 ; + enter_method = acpi_idle_lpi_enter; + } +for (i =0 ; i < pr->power.count && i <CPUIDLE_STATE_MAX ; i++) { lpi = &pr->power.lpi_states[i]; - state = &drv->states[i]; + state = &drv->states[count];snprintf (state->name,CPUIDLE_NAME_LEN ,"LPI-%d" , i);strscpy (state->desc, lpi->desc,CPUIDLE_DESC_LEN ); state->exit_latency = lpi->wake_latency;
@@ -1215,11 +1241,14 @@ static int acpi_processor_setup_lpi_states(struct acpi_processor *pr) state->flags |=arch_get_idle_state_flags (lpi->arch_flags);if (i !=0 && lpi->entry_method ==ACPI_CSTATE_FFH ) state->flags |=CPUIDLE_FLAG_RCU_IDLE ; - state->enter = acpi_idle_lpi_enter; - drv->safe_state_index = i; + state->enter = enter_method; + drv->safe_state_index = count; + count++; +if (count ==CPUIDLE_STATE_MAX ) +break ; } - drv->state_count = i; + drv->state_count = count;return 0 ; }
Signature · 1 line
2.33.0
Quoted 165 lines · zhenglifeng (A)
Quoted 142 lines · Ankur Arora
This patchset enables the cpuidle-haltpoll driver and its namesake governor on arm64. This is specifically interesting for KVM guests by reducing IPC latencies.
Comparing idle switching latencies on an arm64 KVM guest with perf bench sched pipe:
usecs/op %stdev
no haltpoll (baseline) 13.48 +- 5.19%
with haltpoll 6.84 +- 22.07%No change in performance for a similar test on x86:
usecs/op %stdev
haltpoll w/ cpu_relax() (baseline) 4.75 +- 1.76% haltpoll w/ smp_cond_load_relaxed() 4.78 +- 2.31%Both sets of tests were on otherwise idle systems with guest VCPUs pinned to specific PCPUs. One reason for the higher stdev on arm64 is that trapping of the WFE instruction by the host KVM is contingent on the number of tasks on the runqueue.
Tomohiro Misono and Haris Okanovic also report similar latency improvements on Grace and Graviton systems (for v7) [1] [2].
The patch series is organized in three parts:
- patch 1, reorganizes the
poll_idle()loop, switching tosmp_cond_load_relaxed()in the polling loop.
Relatedly patches 2, 3 mangle the config optionARCH_HAS_CPU_RELAX, renaming it toARCH_HAS_OPTIMIZED_POLL.- patches 4-6 reorganize the haltpoll selection and init logic to allow architecture code to select it.
- and finally, patches 7-11 add the bits for arm64 support.
What is still missing: this series largely completes the haltpoll side of functionality for arm64. There are, however, a few related areas that still need to be threshed out:
- WFET support: WFE on arm64 does not guarantee that
poll_idle()would terminate inhalt_poll_ns. Using WFET would address this.
-KVM_NO_POLLsupport on arm64
- KVM TWED support on arm64: allow the host to limit time spent in WFE.Changelog:
v8: No logic changes. Largely respin of v7, with changes noted below:
- move selection of
ARCH_HAS_OPTIMIZED_POLLon arm64 to its own patch.
(patch-9 "arm64: selectARCH_HAS_OPTIMIZED_POLL")- address comments simplifying arm64 support (Will Deacon) (patch-11 "arm64: support cpuidle-haltpoll")
v7: No significant logic changes. Mostly a respin of v6.
- minor cleanup in
poll_idle()(Christoph Lameter)
- fixes conflicts due to code movement inarch/arm64/kernel/cpuidle.c(Tomohiro Misono)v6:
- reordered the patches to keep
poll_idle()andARCH_HAS_OPTIMIZED_POLLchanges together (comment from Christoph Lameter)
- threshes out the commit messages a bit more (comments from Christoph Lameter, Sudeep Holla)
- also rework selection of cpuidle-haltpoll. Now selected based on the architectural selection ofARCH_CPUIDLE_HALTPOLL.
- moved back toarch_haltpoll_want()(comment from Joao Martins) Also,arch_haltpoll_want()now takes the force parameter and is now responsible for the complete selection (or not) of haltpoll.
- fixes the build breakage on i386
- fixes the cpuidle-haltpoll module breakage on arm64 (comment from Tomohiro Misono, Haris Okanovic)v5:
- rework thepoll_idle()loop aroundsmp_cond_load_relaxed()(review comment from Tomohiro Misono.)
- also rework selection of cpuidle-haltpoll. Now selected based on the architectural selection ofARCH_CPUIDLE_HALTPOLL.
-arch_haltpoll_supported()(renamed fromarch_haltpoll_want()) on arm64 now depends on the event-stream being enabled.
- limitPOLL_IDLE_RELAX_COUNTon arm64 (review comment from Haris Okanovic)
-ARCH_HAS_CPU_RELAXis now renamed toARCH_HAS_OPTIMIZED_POLL.v4 changes from v3:
- change 7/8 per Rafael input: drop the parens and use ret for the final check
- add 8/8 which renames the guard for buildingpoll_statev3 changes from v2:
- fix 1/7 per Petr Mladek - removeARCH_HAS_CPU_RELAXfromarch/x86/Kconfig
- add Ack-by from Rafael Wysocki on 2/7v2 changes from v1:
- added patch 7 where we changecpu_relaxwithsmp_cond_load_relaxedper PeterZ (this improves by 50% at least the CPU cycles consumed in the tests above:10,716,881,137 now vs 14,503,014,257 before)- removed the ifdef from patch 1 per RafaelW
Please review.
[1] https://lore.kernel.org/lkml/TY3PR01MB111481E9B0AF263ACC8EA5D4AE5BA2@TY3PR01MB11148.jpnprd01.prod.outlook.com/
[2] https://lore.kernel.org/lkml/[email protected]/Ankur Arora (6):
cpuidle: renameARCH_HAS_CPU_RELAXtoARCH_HAS_OPTIMIZED_POLL
cpuidle-haltpoll: condition onARCH_CPUIDLE_HALTPOLL
arm64: idle: exportarch_cpu_idle
arm64: selectARCH_HAS_OPTIMIZED_POLLcpuidle/poll_state: limitPOLL_IDLE_RELAX_COUNTon arm64
arm64: support cpuidle-haltpollJoao Martins (4):
Kconfig: moveARCH_HAS_OPTIMIZED_POLLtoarch/Kconfig
cpuidle-haltpoll: definearch_haltpoll_want()governors/haltpoll: dropkvm_para_available()check
arm64: defineTIF_POLLING_NRFLAGMihai Carabas (1):
cpuidle/poll_state: poll viasmp_cond_load_relaxed()arch/Kconfig | 3 +++ arch/arm64/Kconfig | 7 +++++++ arch/arm64/include/asm/cpuidle_haltpoll.h | 24 +++++++++++++++++++++++ arch/arm64/include/asm/thread_info.h | 2 ++ arch/arm64/kernel/idle.c | 1 + arch/x86/Kconfig | 5 ++--- arch/x86/include/asm/cpuidle_haltpoll.h | 1 + arch/x86/kernel/kvm.c | 13 ++++++++++++ drivers/acpi/processor_idle.c | 4 ++-- drivers/cpuidle/Kconfig | 5 ++--- drivers/cpuidle/Makefile | 2 +-Quoted 9 lines
drivers/cpuidle/cpuidle-haltpoll.c | 12 +----------- drivers/cpuidle/governors/haltpoll.c | 6 +----- drivers/cpuidle/poll_state.c | 22 +++++++++++++++------ drivers/idle/Kconfig | 1 + include/linux/cpuidle.h | 2 +- include/linux/cpuidle_haltpoll.h | 5 +++++ 17 files changed, 83 insertions(+), 32 deletions(-) create mode 100644 arch/arm64/include/asm/cpuidle_haltpoll.hHi Ankur,
Thanks for the patches!
We have tested these patches on our machine, with an adaptation of ACPI LPI states rather than c-states.
Include polling state, there would be three states to get in. Comparing idle switching latencies of different state with perf bench sched pipe:
usecs/op %stdev
Quoted 7 lines
state0(polling state) 7.36 +- 0.35% state1 8.78 +- 0.46% state2 77.32 +- 5.50%It turns out that it works on our machine.
Tested-by Lifeng Zheng <[email protected]>
Great. Thanks Lifeng.
Quoted 3 lines
The adaptation of ACPI LPI states is shown below as a patch. Feel free to include this patch as part of your series, or I can also send it out after your series being merged.
Ah, so polling for the regular ACPI driver. From a quick look the patch looks good but this series is mostly focused on haltpoll so I think this patch can go in after.
Please Cc me when you send it.
Thanks
Ankur
Quoted 76 lines
From: Lifeng Zheng <[email protected]>
ACPI:
processor_idle: Support polling state for LPIInitialize an optional polling state besides LPI states.
Wrap up a new enter method to correctly reflect the actual entered state when the polling state is enabled.
Signed-off-by Lifeng Zheng <[email protected]>Reviewed-by Jie Zhan <[email protected]>--- drivers/acpi/processor_idle.c | 39 ++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-)drivers/acpi/processor_idle.c +30
-4@@ -1194,20 +1194,46 @@ static int acpi_idle_lpi_enter(struct cpuidle_device *dev,return -EINVAL ; } +/* To correctly reflect the entered state if the poll state is enabled. */ +static int acpi_idle_lpi_enter_with_poll_state (struct cpuidle_device *dev, +struct cpuidle_driver *drv,int index) +{ +int entered_state; + +if (unlikely (index <1 )) +return -EINVAL ; + + entered_state =acpi_idle_lpi_enter (dev, drv, index -1 ); +if (entered_state <0 ) +return entered_state; + +return entered_state +1 ; +} +static int acpi_processor_setup_lpi_states (struct acpi_processor *pr) { -int i; +int i, count;struct acpi_lpi_state *lpi;struct cpuidle_state *state;struct cpuidle_driver *drv = &acpi_idle_driver; + typeof(state->enter) enter_method;if (!pr->flags.has_lpi)return -EOPNOTSUPP ; +if (IS_ENABLED (CONFIG_ARCH_HAS_OPTIMIZED_POLL )) { +cpuidle_poll_state_init (drv); + count =1 ; + enter_method = acpi_idle_lpi_enter_with_poll_state; + }else { + count =0 ; + enter_method = acpi_idle_lpi_enter; + } +for (i =0 ; i < pr->power.count && i <CPUIDLE_STATE_MAX ; i++) { lpi = &pr->power.lpi_states[i]; - state = &drv->states[i]; + state = &drv->states[count];snprintf (state->name,CPUIDLE_NAME_LEN ,"LPI-%d" , i);strscpy (state->desc, lpi->desc,CPUIDLE_DESC_LEN ); state->exit_latency = lpi->wake_latency;@@ -1215,11 +1241,14 @@ static int acpi_processor_setup_lpi_states(struct acpi_processor *pr) state->flags |=arch_get_idle_state_flags (lpi->arch_flags);if (i !=0 && lpi->entry_method ==ACPI_CSTATE_FFH ) state->flags |=CPUIDLE_FLAG_RCU_IDLE ; - state->enter = acpi_idle_lpi_enter; - drv->safe_state_index = i; + state->enter = enter_method; + drv->safe_state_index = count;
drivers/acpi/processor_idle.c static int acpi_processor_setup_lpi_states(struct acpi_processor *pr) · line 1254
Quoted 10 lines
drivers/acpi/processor_idle.c +4
-1+ count++; +if (count ==CPUIDLE_STATE_MAX ) +break ; } - drv->state_count = i; + drv->state_count = count;return 0 ; }
--
ankur
Various versions of this patchset have been extensively tested at Ampere with numerous benchmarks by our performance testing groups.
Please merge this. We will continue to contribute to this patchset.
Quoted 18 lines · Ankur Arora
This patchset enables the cpuidle-haltpoll driver and its namesake governor on arm64. This is specifically interesting for KVM guests by reducing IPC latencies.
Comparing idle switching latencies on an arm64 KVM guest with perf bench sched pipe:
usecs/op %stdev
no haltpoll (baseline) 13.48 +- 5.19%
with haltpoll 6.84 +- 22.07%No change in performance for a similar test on x86:
usecs/op %stdev
Quoted 7 lines
haltpoll w/ cpu_relax() (baseline) 4.75 +- 1.76% haltpoll w/ smp_cond_load_relaxed() 4.78 +- 2.31%Both sets of tests were on otherwise idle systems with guest VCPUs pinned to specific PCPUs. One reason for the higher stdev on arm64 is that trapping of the WFE instruction by the host KVM is contingent on the number of tasks on the runqueue.
Sorry to state the obvious, but if that's the variable trapping of WFI/WFE is the cause of your trouble, why don't you simply turn it off (see 0b5afe05377d for the details)? Given that you pin your vcpus to physical CPUs, there is no need for any trapping.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 28 lines · Marc Zyngier
Quoted 18 lines · Ankur Arora
This patchset enables the cpuidle-haltpoll driver and its namesake governor on arm64. This is specifically interesting for KVM guests by reducing IPC latencies.
Comparing idle switching latencies on an arm64 KVM guest with perf bench sched pipe:
usecs/op %stdev
no haltpoll (baseline) 13.48 +- 5.19%
with haltpoll 6.84 +- 22.07%No change in performance for a similar test on x86:
usecs/op %stdev
Quoted 7 lines
haltpoll w/ cpu_relax() (baseline) 4.75 +- 1.76% haltpoll w/ smp_cond_load_relaxed() 4.78 +- 2.31%Both sets of tests were on otherwise idle systems with guest VCPUs pinned to specific PCPUs. One reason for the higher stdev on arm64 is that trapping of the WFE instruction by the host KVM is contingent on the number of tasks on the runqueue.
Quoted 4 lines
Sorry to state the obvious, but if that's the variable trapping of WFI/WFE is the cause of your trouble, why don't you simply turn it off (see
0b5afe05377dfor the details)? Given that you pin your vcpus to physical CPUs, there is no need for any trapping.
Good point. Thanks. That should help reduce the guessing games around the variance in these tests.
--
ankur
Quoted 32 lines · Ankur Arora
Quoted 28 lines · Marc Zyngier
Quoted 25 lines · Ankur Arora
This patchset enables the cpuidle-haltpoll driver and its namesake governor on arm64. This is specifically interesting for KVM guests by reducing IPC latencies.
Comparing idle switching latencies on an arm64 KVM guest with perf bench sched pipe:
usecs/op %stdev
no haltpoll (baseline) 13.48 +- 5.19%
with haltpoll 6.84 +- 22.07%No change in performance for a similar test on x86:
usecs/op %stdev
haltpoll w/ cpu_relax() (baseline) 4.75 +- 1.76% haltpoll w/ smp_cond_load_relaxed() 4.78 +- 2.31%Both sets of tests were on otherwise idle systems with guest VCPUs pinned to specific PCPUs. One reason for the higher stdev on arm64 is that trapping of the WFE instruction by the host KVM is contingent on the number of tasks on the runqueue.
Quoted 7 lines
Quoted 4 lines
Sorry to state the obvious, but if that's the variable trapping of WFI/WFE is the cause of your trouble, why don't you simply turn it off (see
0b5afe05377dfor the details)? Given that you pin your vcpus to physical CPUs, there is no need for any trapping.Good point. Thanks. That should help reduce the guessing games around the variance in these tests.
I'd be interested to find out whether there is still some benefit in this series once you disable the WFx trapping heuristics.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 39 lines · Marc Zyngier
Quoted 37 lines · Ankur Arora
Quoted 32 lines · Marc Zyngier
Quoted 18 lines · Ankur Arora
This patchset enables the cpuidle-haltpoll driver and its namesake governor on arm64. This is specifically interesting for KVM guests by reducing IPC latencies.
Comparing idle switching latencies on an arm64 KVM guest with perf bench sched pipe:
usecs/op %stdev
no haltpoll (baseline) 13.48 +- 5.19%
with haltpoll 6.84 +- 22.07%No change in performance for a similar test on x86:
usecs/op %stdev
Quoted 7 lines
haltpoll w/ cpu_relax() (baseline) 4.75 +- 1.76% haltpoll w/ smp_cond_load_relaxed() 4.78 +- 2.31%Both sets of tests were on otherwise idle systems with guest VCPUs pinned to specific PCPUs. One reason for the higher stdev on arm64 is that trapping of the WFE instruction by the host KVM is contingent on the number of tasks on the runqueue.
Sorry to state the obvious, but if that's the variable trapping of WFI/WFE is the cause of your trouble, why don't you simply turn it off (see
0b5afe05377dfor the details)? Given that you pin your vcpus to physical CPUs, there is no need for any trapping.
Quoted 5 lines
Quoted 2 lines
Good point. Thanks. That should help reduce the guessing games around the variance in these tests.
I'd be interested to find out whether there is still some benefit in this series once you disable the WFx trapping heuristics.
The benefit of polling in idle is more than just avoiding the cost of trapping and re-entering. The other benefit is that remote wakeups can now be done just by setting need-resched, instead of sending an IPI, and incurring the cost of handling the interrupt on the receiver side.
But let me get you some numbers with that.
--
ankur
Quoted 47 lines · Ankur Arora
Quoted 39 lines · Marc Zyngier
Quoted 37 lines · Ankur Arora
Quoted 32 lines · Marc Zyngier
Quoted 18 lines · Ankur Arora
This patchset enables the cpuidle-haltpoll driver and its namesake governor on arm64. This is specifically interesting for KVM guests by reducing IPC latencies.
Comparing idle switching latencies on an arm64 KVM guest with perf bench sched pipe:
usecs/op %stdev
no haltpoll (baseline) 13.48 +- 5.19%
with haltpoll 6.84 +- 22.07%No change in performance for a similar test on x86:
usecs/op %stdev
Quoted 7 lines
haltpoll w/ cpu_relax() (baseline) 4.75 +- 1.76% haltpoll w/ smp_cond_load_relaxed() 4.78 +- 2.31%Both sets of tests were on otherwise idle systems with guest VCPUs pinned to specific PCPUs. One reason for the higher stdev on arm64 is that trapping of the WFE instruction by the host KVM is contingent on the number of tasks on the runqueue.
Sorry to state the obvious, but if that's the variable trapping of WFI/WFE is the cause of your trouble, why don't you simply turn it off (see
0b5afe05377dfor the details)? Given that you pin your vcpus to physical CPUs, there is no need for any trapping.Quoted 5 lines
Quoted 2 lines
Good point. Thanks. That should help reduce the guessing games around the variance in these tests.
I'd be interested to find out whether there is still some benefit in this series once you disable the WFx trapping heuristics.
Quoted 7 lines
The benefit of polling in idle is more than just avoiding the cost of trapping and re-entering. The other benefit is that remote wakeups can now be done just by setting need-resched, instead of sending an IPI, and incurring the cost of handling the interrupt on the receiver side.
But let me get you some numbers with that.
So, I ran the sched-pipe test with processes on VCPUs 4 and 5 with kvm-arm.wfi_trap_policy=notrap.
# perf stat -r 5 --cpu 4,5 -e task-clock,cycles,instructions,sched:sched_wake_idle_without_ipi \ perf bench sched pipe -l 1000000 -c 4
# No haltpoll (and, no TIF_POLLING_NRFLAG):
Performance counter stats for 'CPU(s) 4,5' (5 runs):
25,229.57 msec task-clock # 2.000 CPUs utilized ( +- 7.75% )
45,821,250,284 cycles # 1.816 GHz ( +- 10.07% )
26,557,496,665 instructions # 0.58 insn per cycle ( +- 0.21% )0 sched:sched_wake_idle_without_ipi # 0.000 /sec
12.615 +- 0.977 seconds time elapsed ( +- 7.75% )
# Haltpoll:
Performance counter stats for 'CPU(s) 4,5' (5 runs):
15,131.58 msec task-clock # 2.000 CPUs utilized ( +- 10.00% )
34,158,188,839 cycles # 2.257 GHz ( +- 6.91% )
20,824,950,916 instructions # 0.61 insn per cycle ( +- 0.09% )
1,983,822 sched:sched_wake_idle_without_ipi # 131.105 K/sec ( +- 0.78% )
7.566 +- 0.756 seconds time elapsed ( +- 10.00% )We get a decent boost just because we are executing ~20% fewer instructions. Not sure how the cpu frequency scaling works in a VM but we also run at a higher frequency.
--
ankur
Hi Ankur, Catalin,
How about the following series based on a refactor of arm64's delay()? Does it address your earlier concerns?
delay() already implements wfet() and falls back to wfe() w/ evstream or a cpu_relax loop. I refactored it to poll an address, and wrapped in a new platform-agnostic smp_vcond_load_relaxed() macro. More details in the following commit log.
Regards,
Haris Okanovic
AWS Graviton Software
PATCH 1/5 asm-generic: add smp_vcond_load_relaxed()
Relaxed poll until desired mask/value is observed at the specified address or timeout.
This macro is a specialization of the generic smp_cond_load_relaxed(), which takes a simple mask/value condition (vcond) instead of an arbitrary expression. It allows architectures to better specialize the implementation, e.g. to enable wfe() polling of the address on arm.
--- include/asm-generic/barrier.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
include/asm-generic/barrier.h +25 -0
@@ -256,6 +256,31 @@ do { \ })#endif +/** +* smp_vcond_load_relaxed() - (Spin) wait until an expected value at address +* with no ordering guarantees. Spins until `(*addr & mask) == val` or +* `nsecs` elapse, and returns the last observed `*addr` value. +* +* @nsecs: timeout in nanoseconds +* @addr: pointer to an integer +* @mask: a bit mask applied to read values +* @val: Expected value with mask +*/ +#ifndef smp_vcond_load_relaxed +#define smp_vcond_load_relaxed (nsecs, addr, mask, val) ({ \ + const u64 __start = local_clock_noinstr(); \ + u64 __nsecs = (nsecs); \ + typeof(addr) __addr = (addr); \ + typeof(*__addr) __mask = (mask); \ + typeof(*__addr) __val = (val); \ + typeof(*__addr) __cur; \ + smp_cond_load_relaxed(__addr, ( \ + (VAL & __mask) == __val || \ + local_clock_noinstr() - __start > __nsecs \ + )); \ +}) +#endif +/** * smp_cond_load_acquire() - (Spin) wait for cond with ACQUIRE ordering * @ptr: pointer to the variable to wait on
Signature · 1 line
2.34.1
Re: [PATCH 1/5] asm-generic: add smp_vcond_load_relaxed()
Quoted 6 lines · Haris Okanovic
+/**
+ *smp_vcond_load_relaxed()- (Spin) wait until an expected value at address
+ * with no ordering guarantees. Spins until(*addr & mask) == valor
+ *nsecselapse, and returns the last observed*addrvalue.
+ *
+ * @nsecs: timeout in nanoseconds
Please use an absolute time in nsecs instead of a timeout. You do not know what will happen to your execution thread until the local_clock_noinstr() is run.
Re: [PATCH 1/5] asm-generic: add smp_vcond_load_relaxed()
Quoted 6 lines · Christoph Lameter (Ampere)
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
On Tue, 5 Nov 2024, Haris Okanovic wrote:
Quoted 8 lines
Quoted 6 lines
+/**
+ *smp_vcond_load_relaxed()- (Spin) wait until an expected value at address
+ * with no ordering guarantees. Spins until(*addr & mask) == valor
+ *nsecselapse, and returns the last observed*addrvalue.
+ *
+ * @nsecs: timeout in nanosecondsPlease use an absolute time in nsecs instead of a timeout.
I went with relative time because it clock agnostic. I agree deadline is nicer because it can propagate down layers of functions, but it pins the caller to single time base.
Quoted 3 lines
You do not know
what will happen to your execution thread until thelocal_clock_noinstr()is run.
Not sure what you mean. Could you perhaps give an example where it would break?
Quoted 2 lines
One alternative is to do timekeeping with delay() in all cases, to decouple from sched/clock.
Catalin Marinas
maintainerRe: [PATCH 1/5] asm-generic: add smp_vcond_load_relaxed()
In reply to Haris OkanovicQuoted 5 lines · Haris Okanovic
include/asm-generic/barrier.h +0
-0
include/asm-generic/barrier.h do { \ · line 264
Quoted 9 lines
include/asm-generic/barrier.h +6
-0@@ -256,6 +256,31 @@ do { \ })#endif +/** +* smp_vcond_load_relaxed() - (Spin) wait until an expected value at address +* with no ordering guarantees. Spins until `(*addr & mask) == val` or +* `nsecs` elapse, and returns the last observed `*addr` value. +* +* @nsecs: timeout in nanoseconds
FWIW, I don't mind the relative timeout, it makes the API easier to use. Yes, it may take longer in absolute time if the thread is scheduled out before local_clock_noinstr() is read but the same can happen in the caller anyway. It's similar to udelay(), it can take longer if the thread is scheduled out.
Quoted 7 lines
include/asm-generic/barrier.h +7
-0+* @addr: pointer to an integer +* @mask: a bit mask applied to read values +* @val: Expected value with mask +*/ +#ifndef smp_vcond_load_relaxed +#define smp_vcond_load_relaxed (nsecs, addr, mask, val) ({ \ + const u64 __start = local_clock_noinstr(); \
include/asm-generic/barrier.h do { \ · line 281
Quoted 10 lines
include/asm-generic/barrier.h +10
-0+u64 __nsecs = (nsecs); \ +typeof (addr) __addr = (addr); \ + typeof(*__addr) __mask = (mask); \ + typeof(*__addr) __val = (val); \ + typeof(*__addr) __cur; \ +smp_cond_load_relaxed (__addr, ( \ + (VAL & __mask) == __val || \ +local_clock_noinstr () - __start > __nsecs \ + )); \ +})
The generic implementation has the same problem as Ankur's current series. smp_cond_load_relaxed() can't wait on anything other than the variable at __addr. If it goes into a WFE, there's nothing executed to read the timer and check for progress. Any generic implementation of such function would have to use cpu_relax() and polling.
Signature · 1 line
Catalin
Re: [PATCH 1/5] asm-generic: add smp_vcond_load_relaxed()
Quoted 41 lines · Catalin Marinas
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 5 lines · Haris Okanovic
include/asm-generic/barrier.h +0
-0include/asm-generic/barrier.h do { \ · line 264
Quoted 9 lines
include/asm-generic/barrier.h +6
-0@@ -256,6 +256,31 @@ do { \ })#endif +/** +* smp_vcond_load_relaxed() - (Spin) wait until an expected value at address +* with no ordering guarantees. Spins until `(*addr & mask) == val` or +* `nsecs` elapse, and returns the last observed `*addr` value. +* +* @nsecs: timeout in nanoseconds FWIW, I don't mind the relative timeout, it makes the API easier to use. Yes, it may take longer in absolute time if the thread is scheduled out before
local_clock_noinstr()is read but the same can happen in the caller anyway. It's similar toudelay(), it can take longer if the thread is scheduled out.Quoted 15 lines
include/asm-generic/barrier.h +15
-0+* @addr: pointer to an integer +* @mask: a bit mask applied to read values +* @val: Expected value with mask +*/ +#ifndef smp_vcond_load_relaxed +#define smp_vcond_load_relaxed (nsecs, addr, mask, val) ({ \ + const u64 __start = local_clock_noinstr(); \ + u64 __nsecs = (nsecs); \ + typeof(addr) __addr = (addr); \ + typeof(*__addr) __mask = (mask); \ + typeof(*__addr) __val = (val); \ + typeof(*__addr) __cur; \ + smp_cond_load_relaxed(__addr, ( \ + (VAL & __mask) == __val || \ + local_clock_noinstr() - __start > __nsecs \
Quoted 8 lines
include/asm-generic/barrier.h do { \ · line 281
Quoted 2 lines
include/asm-generic/barrier.h +2
-0+ )); \ +})The generic implementation has the same problem as Ankur's current series.
smp_cond_load_relaxed()can't wait on anything other than the variable at__addr. If it goes into a WFE, there's nothing executed to read the timer and check for progress. Any generic implementation of such function would have to usecpu_relax()and polling.
How would the caller enter wfe()? Can you give a specific scenario that you're concerned about?
This code already reduces to a relaxed poll, something like this:
```
start = clock();
while((READ_ONCE(*addr) & mask) != val && (clock() - start) < nsecs) {
cpu_relax();
}
```Quoted 3 lines
--
Catalin
Catalin Marinas
maintainerRe: [PATCH 1/5] asm-generic: add smp_vcond_load_relaxed()
Quoted 47 lines · Haris
Quoted 37 lines · Catalin Marinas
Quoted 5 lines · Haris Okanovic
include/asm-generic/barrier.h +0
-0include/asm-generic/barrier.h do { \ · line 264
Quoted 9 lines
include/asm-generic/barrier.h +6
-0@@ -256,6 +256,31 @@ do { \ })#endif +/** +* smp_vcond_load_relaxed() - (Spin) wait until an expected value at address +* with no ordering guarantees. Spins until `(*addr & mask) == val` or +* `nsecs` elapse, and returns the last observed `*addr` value. +* +* @nsecs: timeout in nanoseconds FWIW, I don't mind the relative timeout, it makes the API easier to use. Yes, it may take longer in absolute time if the thread is scheduled out before
local_clock_noinstr()is read but the same can happen in the caller anyway. It's similar toudelay(), it can take longer if the thread is scheduled out.Quoted 15 lines
include/asm-generic/barrier.h +15
-0+* @addr: pointer to an integer +* @mask: a bit mask applied to read values +* @val: Expected value with mask +*/ +#ifndef smp_vcond_load_relaxed +#define smp_vcond_load_relaxed (nsecs, addr, mask, val) ({ \ + const u64 __start = local_clock_noinstr(); \ + u64 __nsecs = (nsecs); \ + typeof(addr) __addr = (addr); \ + typeof(*__addr) __mask = (mask); \ + typeof(*__addr) __val = (val); \ + typeof(*__addr) __cur; \ + smp_cond_load_relaxed(__addr, ( \ + (VAL & __mask) == __val || \ + local_clock_noinstr() - __start > __nsecs \Quoted 8 lines
include/asm-generic/barrier.h do { \ · line 281
Quoted 2 lines
include/asm-generic/barrier.h +2
-0+ )); \ +})The generic implementation has the same problem as Ankur's current series.
smp_cond_load_relaxed()can't wait on anything other than the variable at__addr. If it goes into a WFE, there's nothing executed to read the timer and check for progress. Any generic implementation of such function would have to usecpu_relax()and polling.
Quoted 2 lines
How would the caller enter
wfe()? Can you give a specific scenario that you're concerned about?
Let's take the arm64 example with the event stream disabled. Without the subsequent patches implementing smp_vcond_load_relaxed(), just expand the arm64 smp_cond_load_relaxed() implementation in the above macro. If the timer check doesn't trigger an exit from the loop, __cmpwait_relaxed() only waits on the variable to change its value, nothing to do with the timer.
Quoted 8 lines
This code already reduces to a relaxed poll, something like this:
``` start = clock(); while((READ_ONCE(*addr) & mask) != val && (clock() - start) < nsecs) { cpu_relax(); } ```
Well, that's if you also use the generic implementation of smp_cond_load_relaxed() but have you checked all the other architectures that don't do something similar to the arm64 wfe (riscv comes close)? Even if all other architectures just use a cpu_relax(), that's still abusing the smp_cond_load_relaxed() semantics. And what if one places another loop in their __cmpwait()? That's allowed because you are supposed to wait on a single variable to change not on multiple states.
Signature · 1 line
Catalin
Re: [PATCH 1/5] asm-generic: add smp_vcond_load_relaxed()
Quoted 71 lines · Catalin Marinas
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 47 lines · Haris
Quoted 37 lines · Catalin Marinas
Quoted 5 lines · Haris Okanovic
include/asm-generic/barrier.h +0
-0include/asm-generic/barrier.h do { \ · line 264
Quoted 9 lines
include/asm-generic/barrier.h +6
-0@@ -256,6 +256,31 @@ do { \ })#endif +/** +* smp_vcond_load_relaxed() - (Spin) wait until an expected value at address +* with no ordering guarantees. Spins until `(*addr & mask) == val` or +* `nsecs` elapse, and returns the last observed `*addr` value. +* +* @nsecs: timeout in nanoseconds FWIW, I don't mind the relative timeout, it makes the API easier to use. Yes, it may take longer in absolute time if the thread is scheduled out before
local_clock_noinstr()is read but the same can happen in the caller anyway. It's similar toudelay(), it can take longer if the thread is scheduled out.Quoted 15 lines
include/asm-generic/barrier.h +15
-0+* @addr: pointer to an integer +* @mask: a bit mask applied to read values +* @val: Expected value with mask +*/ +#ifndef smp_vcond_load_relaxed +#define smp_vcond_load_relaxed (nsecs, addr, mask, val) ({ \ + const u64 __start = local_clock_noinstr(); \ + u64 __nsecs = (nsecs); \ + typeof(addr) __addr = (addr); \ + typeof(*__addr) __mask = (mask); \ + typeof(*__addr) __val = (val); \ + typeof(*__addr) __cur; \ + smp_cond_load_relaxed(__addr, ( \ + (VAL & __mask) == __val || \ + local_clock_noinstr() - __start > __nsecs \Quoted 8 lines
include/asm-generic/barrier.h do { \ · line 281
Quoted 2 lines
include/asm-generic/barrier.h +2
-0+ )); \ +})The generic implementation has the same problem as Ankur's current series.
smp_cond_load_relaxed()can't wait on anything other than the variable at__addr. If it goes into a WFE, there's nothing executed to read the timer and check for progress. Any generic implementation of such function would have to usecpu_relax()and polling.Quoted 2 lines
How would the caller enter
wfe()? Can you give a specific scenario that you're concerned about?Let's take the arm64 example with the event stream disabled. Without the subsequent patches implementing
smp_vcond_load_relaxed(), just expand the arm64smp_cond_load_relaxed()implementation in the above macro. If the timer check doesn't trigger an exit from the loop,__cmpwait_relaxed()only waits on the variable to change its value, nothing to do with the timer.Quoted 8 lines
This code already reduces to a relaxed poll, something like this:
``` start = clock(); while((READ_ONCE(*addr) & mask) != val && (clock() - start) < nsecs) { cpu_relax(); } ```
Quoted 7 lines
Well, that's if you also use the generic implementation of
smp_cond_load_relaxed()but have you checked all the other architectures that don't do something similar to the arm64 wfe (riscv comes close)? Even if all other architectures just use acpu_relax(), that's still abusing thesmp_cond_load_relaxed()semantics. And what if one places another loop in their__cmpwait()? That's allowed because you are supposed to wait on a single variable to change not on multiple states.
I see what you mean now - I glossed over the use of __cmpwait_relaxed() in smp_cond_load_relaxed(). I'll post another rev with the fix, similar to the above "reduced" code.
Quoted 3 lines
--
Catalin
Will Deacon
maintainerRe: [PATCH 1/5] asm-generic: add smp_vcond_load_relaxed()
In reply to Haris OkanovicQuoted 7 lines · Haris Okanovic
Relaxed poll until desired mask/value is observed at the specified address or timeout.
This macro is a specialization of the generic
smp_cond_load_relaxed(), which takes a simple mask/value condition (vcond) instead of an arbitrary expression. It allows architectures to better specialize the implementation, e.g. to enablewfe()polling of the address on arm.
This doesn't make sense to me. The existing smp_cond_load() functions already use wfe on arm64 and I don't see why we need a special helper just to do a mask.
Quoted 14 lines
Signed-off-by Haris Okanovic <[email protected]>--- include/asm-generic/barrier.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)include/asm-generic/barrier.h +1
-0@@ -256,6 +256,31 @@ do { \ })#endif +/**
include/asm-generic/barrier.h do { \ · line 269
Quoted 10 lines
include/asm-generic/barrier.h +10
-0+* smp_vcond_load_relaxed() - (Spin) wait until an expected value at address +* with no ordering guarantees. Spins until `(*addr & mask) == val` or +* `nsecs` elapse, and returns the last observed `*addr` value. +* +* @nsecs: timeout in nanoseconds +* @addr: pointer to an integer +* @mask: a bit mask applied to read values +* @val: Expected value with mask +*/ +#ifndef smp_vcond_load_relaxed
I know naming is hard, but "vcond" is especially terrible. Perhaps smp_cond_load_timeout()?
Will
Re: [PATCH 1/5] asm-generic: add smp_vcond_load_relaxed()
Quoted 8 lines · Will Deacon
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 3 lines · Haris Okanovic
Relaxed poll until desired mask/value is observed at the specified address or timeout.
Quoted 8 lines
Quoted 4 lines
This macro is a specialization of the generic
smp_cond_load_relaxed(), which takes a simple mask/value condition (vcond) instead of an arbitrary expression. It allows architectures to better specialize the implementation, e.g. to enablewfe()polling of the address on arm.This doesn't make sense to me. The existing
smp_cond_load()functions already use wfe on arm64 and I don't see why we need a special helper just to do a mask.
We can't turn an arbitrary C expression into a wfe()/wfet() exit condition, which is one of the inputs to the existing smp_cond_load(). This API is therefore more amenable to hardware acceleration.
Quoted 14 lines
Quoted 14 lines
Signed-off-by Haris Okanovic <[email protected]>--- include/asm-generic/barrier.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)include/asm-generic/barrier.h +0
-0@@ -256,6 +256,31 @@ do { \ })#endif
Quoted 14 lines
include/asm-generic/barrier.h do { \ · line 269
Quoted 11 lines
include/asm-generic/barrier.h +11
-0+/** +* smp_vcond_load_relaxed() - (Spin) wait until an expected value at address +* with no ordering guarantees. Spins until `(*addr & mask) == val` or +* `nsecs` elapse, and returns the last observed `*addr` value. +* +* @nsecs: timeout in nanoseconds +* @addr: pointer to an integer +* @mask: a bit mask applied to read values +* @val: Expected value with mask +*/ +#ifndef smp_vcond_load_relaxedI know naming is hard, but "vcond" is especially terrible. Perhaps
smp_cond_load_timeout()?
I agree, naming is hard! I was trying to differentiate it from smp_cond_load() in some meaningful way - that one is an "expression" condition this one is a "value" condition.
I'll think it over a bit more.
Quoted 2 lines
Will
PATCH 2/5 arm64: add __READ_ONCE_EX()
In reply to Haris OkanovicPerform an exclusive load, which atomically loads a word and arms the exclusive monitor to enable wfet()/wfe() accelerated polling.
--- arch/arm64/include/asm/readex.h | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 arch/arm64/include/asm/readex.h
arch/arm64/include/asm/readex.h new file +46 -0
@@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Based on arch/arm64/include/asm/rwonce.h +* +* Copyright (C) 2020 Google LLC. +* Copyright (C) 2024 Amazon.com, Inc. or its affiliates. +*/ + +#ifndef __ASM_READEX_H +#define __ASM_READEX_H + +#define __LOAD_EX(sfx, regs...) "ldaxr" #sfx "\t" #regs + +#define __READ_ONCE_EX (x) \ +({ \ + typeof(&(x)) __x = &(x); \ + int atomic = 1; \ + union { __unqual_scalar_typeof(*__x) __val; char __c[1]; } __u; \ + switch (sizeof(x)) { \ + case 1: \ + asm volatile(__LOAD_EX(b, %w0, %1) \ + : "=r" (*(__u8 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + case 2: \ + asm volatile(__LOAD_EX(h, %w0, %1) \ + : "=r" (*(__u16 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + case 4: \ + asm volatile(__LOAD_EX(, %w0, %1) \ + : "=r" (*(__u32 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + case 8: \ + asm volatile(__LOAD_EX(, %0, %1) \ + : "=r" (*(__u64 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + default: \ + atomic = 0; \ + } \ + atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(__x))__x);\ +}) + +#endif
Signature · 1 line
2.34.1
Re: [PATCH 2/5] arm64: add __READ_ONCE_EX()
Quoted 1 line · Haris Okanovic
+#define
__READ_ONCE_EX(x)\
This is derived from READ_ONCE and named similarly so maybe it would better be placed into rwonce.h?
Re: [PATCH 2/5] arm64: add __READ_ONCE_EX()
Quoted 4 lines · Christoph Lameter (Ampere)
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 8 lines
Quoted 1 line · Haris Okanovic
+#define
__READ_ONCE_EX(x)\This is derived from
READ_ONCEand named similarly so maybe it would better be placed intorwonce.h?
I plan to remove this macro per other feedback in this thread.
Will Deacon
maintainerRe: [PATCH 2/5] arm64: add __READ_ONCE_EX()
In reply to Haris OkanovicQuoted 51 lines · Haris Okanovic
Perform an exclusive load, which atomically loads a word and arms the exclusive monitor to enable wfet()/wfe() accelerated polling.
Signed-off-by Haris Okanovic <[email protected]>--- arch/arm64/include/asm/readex.h | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 arch/arm64/include/asm/readex.harch/arm64/include/asm/readex.h new file +34
-0@@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Based on arch/arm64/include/asm/rwonce.h +* +* Copyright (C) 2020 Google LLC. +* Copyright (C) 2024 Amazon.com, Inc. or its affiliates. +*/ + +#ifndef __ASM_READEX_H +#define __ASM_READEX_H + +#define __LOAD_EX(sfx, regs...) "ldaxr" #sfx "\t" #regs + +#define __READ_ONCE_EX (x) \ +({ \ + typeof(&(x)) __x = &(x); \ + int atomic = 1; \ + union { __unqual_scalar_typeof(*__x) __val; char __c[1]; } __u; \ + switch (sizeof(x)) { \ + case 1: \ + asm volatile(__LOAD_EX(b, %w0, %1) \ + : "=r" (*(__u8 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + case 2: \ + asm volatile(__LOAD_EX(h, %w0, %1) \ + : "=r" (*(__u16 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + case 4: \ + asm volatile(__LOAD_EX(, %w0, %1) \ + : "=r" (*(__u32 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \
arch/arm64/include/asm/readex.h line 44
Quoted 10 lines
arch/arm64/include/asm/readex.h new file +10
-0+case 8 : \ + asm volatile(__LOAD_EX(, %0, %1) \ + : "=r" (*(__u64 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ +break; \ +default: \ +atomic = 0; \ +} \ +atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(__x))__x);\ +})
I think this is a bad idea. Load-exclusive needs to be used very carefully, preferably when you're able to see exactly what instructions it's interacting with. By making this into a macro, we're at the mercy of the compiler and we give the wrong impression that you could e.g. build atomic critical sections out of this macro.
So I'm fairly strongly against this interface.
Will
Re: [PATCH 2/5] arm64: add __READ_ONCE_EX()
Quoted 73 lines · Will Deacon
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 51 lines · Haris Okanovic
Perform an exclusive load, which atomically loads a word and arms the exclusive monitor to enable wfet()/wfe() accelerated polling.
Signed-off-by Haris Okanovic <[email protected]>--- arch/arm64/include/asm/readex.h | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 arch/arm64/include/asm/readex.harch/arm64/include/asm/readex.h new file +34
-0@@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Based on arch/arm64/include/asm/rwonce.h +* +* Copyright (C) 2020 Google LLC. +* Copyright (C) 2024 Amazon.com, Inc. or its affiliates. +*/ + +#ifndef __ASM_READEX_H +#define __ASM_READEX_H + +#define __LOAD_EX(sfx, regs...) "ldaxr" #sfx "\t" #regs + +#define __READ_ONCE_EX (x) \ +({ \ + typeof(&(x)) __x = &(x); \ + int atomic = 1; \ + union { __unqual_scalar_typeof(*__x) __val; char __c[1]; } __u; \ + switch (sizeof(x)) { \ + case 1: \ + asm volatile(__LOAD_EX(b, %w0, %1) \ + : "=r" (*(__u8 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + case 2: \ + asm volatile(__LOAD_EX(h, %w0, %1) \ + : "=r" (*(__u16 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + case 4: \ + asm volatile(__LOAD_EX(, %w0, %1) \ + : "=r" (*(__u32 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \arch/arm64/include/asm/readex.h line 44
Quoted 10 lines
arch/arm64/include/asm/readex.h new file +10
-0+case 8 : \ + asm volatile(__LOAD_EX(, %0, %1) \ + : "=r" (*(__u64 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ +break; \ +default: \ +atomic = 0; \ +} \ +atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(__x))__x);\ +})I think this is a bad idea. Load-exclusive needs to be used very carefully, preferably when you're able to see exactly what instructions it's interacting with. By making this into a macro, we're at the mercy of the compiler and we give the wrong impression that you could e.g. build atomic critical sections out of this macro.
Quoted 1 line
So I'm fairly strongly against this interface.
Fair point. I'll post an alternate delay() implementation in asm. It's a simple routine.
Quoted 2 lines
Will
RE: [PATCH 2/5] arm64: add __READ_ONCE_EX()
In reply to Haris OkanovicFrom: Haris Okanovic
Quoted 5 lines
Sent: 05 November 2024 18:31
Perform an exclusive load, which atomically loads a word and arms the exclusive monitor to enable wfet()/wfe() accelerated polling.
...
arch/arm64/include/asm/readex.h line 43
Quoted 1 line
arch/arm64/include/asm/readex.h new file +1
-0+ atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(__x))__x);\
That doesn't do what you want it to do.
(It is wrong in READ_ONCE() as well.)
?: is treated like an arithmetic operator and the result will get promoted to 'int'.
Moving the first cast outside the ?: probably works:
(typeof(*__x))(atomic ? __u.__val : (*(volatile typeof(__x))__x));
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
PATCH 3/5 arm64: refactor delay() to enable polling for value
In reply to Haris OkanovicRefactor arm64's delay() to poll for a mask/value condition (vcond) in it's wfet(), wfe(), and relaxed polling loops.
--- arch/arm64/lib/delay.c | 70 ++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 20 deletions(-)
arch/arm64/lib/delay.c +50 -20
@@ -14,43 +14,73 @@#include <linux/timex.h> #include <clocksource/arm_arch_timer.h> +#include <asm/readex.h> -#define USECS_TO_CYCLES (time_usecs) \ - xloops_to_cycles((time_usecs) * 0x10C7UL) - -static inline unsigned long xloops_to_cycles (unsigned long xloops) +static inline u64 xloops_to_cycles (u64 xloops) {return (xloops * loops_per_jiffy *HZ ) >>32 ; } -void __delay (unsigned long cycles) +#define USECS_TO_XLOOPS (time_usecs) \ + ((time_usecs) * 0x10C7UL) + +#define USECS_TO_CYCLES (time_usecs) \ + xloops_to_cycles(USECS_TO_XLOOPS(time_usecs)) + +#define NSECS_TO_XLOOPS (time_nsecs) \ + ((time_nsecs) * 0x10C7UL) + +#define NSECS_TO_CYCLES (time_nsecs) \ + xloops_to_cycles(NSECS_TO_XLOOPS(time_nsecs)) + +static unsigned long __delay_until_ul (u64 cycles,unsigned long * addr,unsigned long mask,unsigned long val) { -cycles_t start =get_cycles (); +u64 start =get_cycles (); +unsigned long cur;if (alternative_has_cap_unlikely (ARM64_HAS_WFXT )) {u64 end = start + cycles; -/* -* Start with WFIT. If an interrupt makes us resume -* early, use a WFET loop to complete the delay. -*/ -wfit (end); -while ((get_cycles () - start) < cycles) +do { + cur =__READ_ONCE_EX (*addr); +if ((cur & mask) == val) { +break ; + }wfet (end); - }else if (arch_timer_evtstrm_available ()) { -const cycles_t timer_evt_period = + }while ((get_cycles () - start) < cycles); + }else if (arch_timer_evtstrm_available ()) { +const u64 timer_evt_period =USECS_TO_CYCLES (ARCH_TIMER_EVT_STREAM_PERIOD_US ); -while ((get_cycles () - start + timer_evt_period) < cycles) +do { + cur =__READ_ONCE_EX (*addr); +if ((cur & mask) == val) { +break ; + }wfe (); + }while ((get_cycles () - start + timer_evt_period) < cycles); + }else { +do { + cur =__READ_ONCE_EX (*addr); +if ((cur & mask) == val) { +break ; + } +cpu_relax (); + }while ((get_cycles () - start) < cycles); } -while ((get_cycles () - start) < cycles) -cpu_relax (); +return cur; +} + +void __delay (unsigned long cycles) +{ +/* constant word for wfet()/wfe() to poll */ +unsigned long dummy____cacheline_aligned =0 ; +__delay_until_ul (cycles, &dummy,0 ,1 ); }EXPORT_SYMBOL (__delay); -inline void __const_udelay (unsigned long xloops) +void __const_udelay (unsigned long xloops) {__delay (xloops_to_cycles (xloops)); }
@@ -58,12 +88,12 @@ EXPORT_SYMBOL(__const_udelay);void __udelay (unsigned long usecs) { -__const_udelay (usecs *0x10C7UL );/* 2**32 / 1000000 (rounded up) */ +__delay (USECS_TO_CYCLES (usecs)); }EXPORT_SYMBOL (__udelay);void __ndelay (unsigned long nsecs) { -__const_udelay (nsecs *0x5UL );/* 2**32 / 1000000000 (rounded up) */ +__delay (NSECS_TO_CYCLES (nsecs)); }EXPORT_SYMBOL (__ndelay);
Signature · 1 line
2.34.1
Re: [PATCH 3/5] arm64: refactor delay() to enable polling for value
Quoted 10 lines · Haris Okanovic
arch/arm64/lib/delay.c +1
-5-#define USECS_TO_CYCLES (time_usecs) \ - xloops_to_cycles((time_usecs) * 0x10C7UL) - -static inline unsigned long xloops_to_cycles (unsigned long xloops) +static inline u64 xloops_to_cycles (u64 xloops) {return (xloops * loops_per_jiffy *HZ ) >>32 ; } -void __delay (unsigned long cycles)
arch/arm64/lib/delay.c
Quoted 10 lines
arch/arm64/lib/delay.c +8
-0+#define USECS_TO_XLOOPS (time_usecs) \ + ((time_usecs) * 0x10C7UL) + +#define USECS_TO_CYCLES (time_usecs) \ + xloops_to_cycles(USECS_TO_XLOOPS(time_usecs)) + +#define NSECS_TO_XLOOPS (time_nsecs) \ + ((time_nsecs) * 0x10C7UL)
The constant here is the same value as for microseconds. If I remember correctly its 5UL for nanoseconds.
Re: [PATCH 3/5] arm64: refactor delay() to enable polling for value
Quoted 24 lines · Christoph Lameter (Ampere)
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 16 lines · Haris Okanovic
arch/arm64/lib/delay.c +7
-5-#define USECS_TO_CYCLES (time_usecs) \ - xloops_to_cycles((time_usecs) * 0x10C7UL) - -static inline unsigned long xloops_to_cycles (unsigned long xloops) +static inline u64 xloops_to_cycles (u64 xloops) {return (xloops * loops_per_jiffy *HZ ) >>32 ; } -void __delay (unsigned long cycles) +#define USECS_TO_XLOOPS (time_usecs) \ + ((time_usecs) * 0x10C7UL) + +#define USECS_TO_CYCLES (time_usecs) \ + xloops_to_cycles(USECS_TO_XLOOPS(time_usecs)) +
Quoted 6 lines
arch/arm64/lib/delay.c line 31
Quoted 2 lines
arch/arm64/lib/delay.c +2
-0+#define NSECS_TO_XLOOPS (time_nsecs) \ + ((time_nsecs) * 0x10C7UL)The constant here is the same value as for microseconds. If I remember correctly its 5UL for nanoseconds.
You're right, good catch. Should be nsecs * 0x5UL per old code.
Catalin Marinas
maintainerRe: [PATCH 3/5] arm64: refactor delay() to enable polling for value
In reply to Haris Okanovicarch/arm64/lib/delay.c line 49
Quoted 6 lines · Haris Okanovic
arch/arm64/lib/delay.c +5
-0+ do { + cur = __READ_ONCE_EX(*addr); + if ((cur & mask) == val) { + break; + } wfet(end);
Constructs like this need to be entirely in assembly. The compiler may spill 'cur' onto the stack and the write could clear the exclusive monitor which makes the wfet return immediately. That's highly CPU implementation specific but it's the reason we have functions like __cmpwait() in assembly (or whatever else deals with exclusives). IOW, we can't have other memory accesses between the LDXR and whatever is consuming the exclusive monitor armed state (typically STXR but WFE/WFET can be another).
Signature · 1 line
Catalin
Re: [PATCH 3/5] arm64: refactor delay() to enable polling for value
Quoted 12 lines · Catalin Marinas
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
arch/arm64/lib/delay.c line 49
Quoted 6 lines · Haris Okanovic
arch/arm64/lib/delay.c +5
-0+ do { + cur = __READ_ONCE_EX(*addr); + if ((cur & mask) == val) { + break; + } wfet(end);
Quoted 8 lines
Constructs like this need to be entirely in assembly. The compiler may spill 'cur' onto the stack and the write could clear the exclusive monitor which makes the wfet return immediately. That's highly CPU implementation specific but it's the reason we have functions like
__cmpwait()in assembly (or whatever else deals with exclusives). IOW, we can't have other memory accesses between the LDXR and whatever is consuming the exclusive monitor armed state (typically STXR but WFE/WFET can be another).
Agreed, will rewrite parts of delay() in asm.
Quoted 3 lines
--
Catalin
PATCH 4/5 arm64: add smp_vcond_load_relaxed()
In reply to Haris OkanovicImplement smp_vcond_load_relaxed() atop __delay_until_ul() on arm64, to reduce number of busy loops while waiting for a value condition.
This implementation only support unsigned long words. It can be extended via the enclosed case structure in barrier.h as needed.
--- arch/arm64/include/asm/barrier.h | 18 ++++++++++++++++++ arch/arm64/lib/delay.c | 16 ++++++++++++++++ 2 files changed, 34 insertions(+)
arch/arm64/include/asm/barrier.h +18 -0
@@ -203,6 +203,24 @@ do { \ (typeof(*ptr))VAL; \ }) +extern unsigned long __smp_vcond_load_relaxed_ul ( +u64 nsecs,unsigned long * addr,unsigned long mask,unsigned long val); + +#define smp_vcond_load_relaxed (nsecs, addr, mask, val) ({ \ + u64 __nsecs = (nsecs); \ + typeof(addr) __addr = (addr); \ + typeof(*__addr) __mask = (mask); \ + typeof(*__addr) __val = (val); \ + typeof(*__addr) __cur; \ + switch (sizeof(*__addr)) { \ + case sizeof(unsigned long): \ + __cur = __smp_vcond_load_relaxed_ul( \ + __nsecs, __addr, __mask, __val); \ + break; \ + } \ + (__cur); \ +}) +#define smp_cond_load_acquire (ptr, cond_expr) \ ({ \ typeof(ptr) __PTR = (ptr); \
arch/arm64/lib/delay.c +16 -0
@@ -12,6 +12,7 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/timex.h> +#include <linux/sched/clock.h> #include <clocksource/arm_arch_timer.h> #include <asm/readex.h>
@@ -97,3 +98,18 @@ void __ndelay(unsigned long nsecs)__delay (NSECS_TO_CYCLES (nsecs)); }EXPORT_SYMBOL (__ndelay); + +unsigned long __smp_vcond_load_relaxed_ul ( +u64 nsecs,unsigned long * addr,unsigned long mask,unsigned long val) +{ +const u64 start =local_clock_noinstr (); +const u64 cycles =NSECS_TO_CYCLES (nsecs); +unsigned long cur; + +do { + cur =__delay_until_ul (cycles, addr, mask, val); + }while ((cur & mask) != val &&local_clock_noinstr () - start < nsecs); + +return cur; +} +EXPORT_SYMBOL (__smp_vcond_load_relaxed_ul);
Signature · 1 line
2.34.1
PATCH 5/5 cpuidle: implement poll_idle() using smp_vcond_load_relaxed()
In reply to Haris OkanovicImplement poll_idle() using smp_vcond_load_relaxed() function.
--- drivers/cpuidle/poll_state.c | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-)
drivers/cpuidle/poll_state.c +5 -31
@@ -7,46 +7,20 @@#include <linux/sched.h> #include <linux/sched/clock.h> #include <linux/sched/idle.h> - -#ifdef CONFIG_ARM64 -/* -* POLL_IDLE_RELAX_COUNT determines how often we check for timeout -* while polling for TIF_NEED_RESCHED in thread_info->flags. -* -* Set this to a low value since arm64, instead of polling, uses a -* event based mechanism. -*/ -#define POLL_IDLE_RELAX_COUNT 1 -#else -#define POLL_IDLE_RELAX_COUNT 200 -#endif +#include <asm/barrier.h> static int __cpuidle poll_idle(struct cpuidle_device *dev,struct cpuidle_driver *drv,int index) { -u64 time_start; - - time_start =local_clock_noinstr (); +unsigned long flags; dev->poll_time_limit = false;raw_local_irq_enable ();if (!current_set_polling_and_test ()) { -u64 limit; - - limit =cpuidle_poll_time (drv, dev); - -while (!need_resched ()) { -unsigned int loop_count =0 ; -if (local_clock_noinstr () - time_start > limit) { - dev->poll_time_limit = true; -break ; - } - -smp_cond_load_relaxed (¤t_thread_info ()->flags, -VAL & _TIF_NEED_RESCHED || - loop_count++ >=POLL_IDLE_RELAX_COUNT ); - } +u64 limit =cpuidle_poll_time (drv, dev); + flags =smp_vcond_load_relaxed (limit, ¤t_thread_info ()->flags, _TIF_NEED_RESCHED, _TIF_NEED_RESCHED); + dev->poll_time_limit = !(flags & _TIF_NEED_RESCHED); }raw_local_irq_disable ();
Signature · 1 line
2.34.1
Re: [PATCH 5/5] cpuidle: implement poll_idle() using smp_vcond_load_relaxed()
drivers/cpuidle/poll_state.c line 15
Quoted 5 lines · Haris Okanovic
{
drivers/cpuidle/poll_state.c +1
-3-u64 time_start; - - time_start =local_clock_noinstr (); +unsigned long flags;
Lets keep recording that start value here. Otherwise the timeout could me later than expected.
Quoted 9 lines · Haris Okanovic
Hi Ankur, Catalin,
How about the following series based on a refactor of arm64's
delay()? Does it address your earlier concerns?
delay()already implementswfet()and falls back towfe()w/ evstream or acpu_relaxloop. I refactored it to poll an address, and wrapped in a new platform-agnosticsmp_vcond_load_relaxed()macro. More details in the following commit log.
Haven't looked at your series too closely but it looks quite a bit different from the version I was working on.
Let me send out my version as well in the next few days.
--
ankur