Support waiting in smp_cond_load_relaxed_timeout() via __cmpwait_relaxed(). To ensure that we wake from waiting in WFE periodically and don't block forever if there are no stores to ptr, this path is only used when the event-stream is enabled. Note that when using __cmpwait_relaxed() we ignore the timeout value, allowing an overshoot by up to the event-stream period. And, in the unlikely event that the event-stream is unavailable, fallback to spin-waiting. Define CPU_POLL_RELAX_WAITS to state that we have a waiting implementation. (Non-production environments might not have arch_timer_evtstrm_available() but we don't care about that configuration.) Also mark arch_timer_evtstrm_available() noinstr since we will be using smp_cond_load_relaxed_timeout() from the cpuidle context which is non-instrumentable. Note that with this we have enough to define ARCH_HAS_CPU_RELAX to indicate that we support an optimized implementation of cpu_poll_relax(). However, defer defining ARCH_HAS_CPU_RELAX as that enables polling based C-state handling, which really needs TIF_POLLING_NRFLAG. Cc: Arnd Bergmann Cc: Will Deacon Cc: Catalin Marinas Cc: linux-arm-kernel@lists.infradead.org Suggested-by: Will Deacon Acked-by: Will Deacon Reviewed-by: Catalin Marinas Tested-by: Haris Okanovic Tested-by: Sahil Chandna Signed-off-by: Ankur Arora --- Notes: - mark arch_timer_evtstrm_available() noinstr. This is needed because smp_cond_load_relaxed_timeout() will be called from a noinstr context via __cpuidle poll_idle(). arch/arm64/include/asm/barrier.h | 22 ++++++++++++++++++++++ drivers/clocksource/arm_arch_timer.c | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/barrier.h b/arch/arm64/include/asm/barrier.h index 9495c4441a46..e2263c754a55 100644 --- a/arch/arm64/include/asm/barrier.h +++ b/arch/arm64/include/asm/barrier.h @@ -12,6 +12,7 @@ #include #include +#include #define __nops(n) ".rept " #n "\nnop\n.endr\n" #define nops(n) asm volatile(__nops(n)) @@ -219,6 +220,27 @@ do { \ (typeof(*ptr))VAL; \ }) +/* Re-declared here to avoid include dependency. */ +extern bool arch_timer_evtstrm_available(void); + +/* + * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed() + * for @ptr value to change. + * + * State this by defining CPU_POLL_RELAX_WAITS. This enables a time-check + * optimization in smp_cond_load_{relaxed,acquire}_timeout(). Note that this + * is not ideal for the cpu_relax() fallback but that's not a production + * configuration. + */ +#define CPU_POLL_RELAX_WAITS + +#define cpu_poll_relax(ptr, val, timeout_ns) do { \ + if (arch_timer_evtstrm_available()) \ + __cmpwait_relaxed(ptr, val); \ + else \ + cpu_relax(); \ +} while (0) + #include #endif /* __ASSEMBLER__ */ diff --git a/drivers/clocksource/arm_arch_timer.c b/drivers/clocksource/arm_arch_timer.c index cc9a8b399004..bf27b709d23a 100644 --- a/drivers/clocksource/arm_arch_timer.c +++ b/drivers/clocksource/arm_arch_timer.c @@ -884,7 +884,7 @@ u32 arch_timer_get_rate(void) return arch_timer_rate; } -bool arch_timer_evtstrm_available(void) +bool noinstr arch_timer_evtstrm_available(void) { /* * We might get called from a preemptible context. This is fine -- 2.43.7