Skip to contentSkip to conversation

barrier: Add smp_cond_load_{relaxed,acquire}_timeout()

PATCH Superseded Ankur Arora 6 participants 34 messages Dec 15, 2025 – Jan 20

Subsystems (7)

v8 of 17 Superseded Latest is v17 →

lore
Share & tools
lore
#0 lore raw

v8 cover barrier: Add smp_cond_load_{relaxed,acquire}_timeout()

This series adds waited variants of the smp_cond_load() primitives:
smp_cond_load_relaxed_timeout(), and smp_cond_load_acquire_timeout().

As the name suggests, the new interfaces are meant for contexts where you want to wait on a condition variable for a finite duration. This is easy enough to do with a loop around cpu_relax(). There are, however, architectures (ex. arm64) that allow waiting on a cacheline instead.

So, these interfaces handle a mixture of spin/wait with a smp_cond_load() thrown in. The interfaces are:

smp_cond_load_relaxed_timeout(ptr, cond_expr, time_expr, timeout) smp_cond_load_acquire_timeout(ptr, cond_expr, time_expr, timeout)

The parameters, time_expr, timeout determine when to bail out.

Also add tif_need_resched_relaxed_wait() which wraps the pattern used in poll_idle() and abstracts out details of the interface and those of the scheduler.

In addition add atomic_cond_read_*_timeout(), atomic64_cond_read_*_timeout(), and atomic_long wrappers to the interfaces.

Finally update poll_idle() and resilient queued spinlocks to use them.

Changelog:

v7 [0]:
- change the interface to separately provide the timeout. This is useful for supporting WFET and similar primitives which can do timed waiting (suggested by Arnd Bergmann).

- Adapting rqspinlock code to this changed interface also necessitated allowing time_expr to fail.
- rqspinlock changes to adapt to the new smp_cond_load_acquire_timeout().

- add WFET support (suggested by Arnd Bergmann).
- add support for atomic-long wrappers.
- add a new scheduler interface tif_need_resched_relaxed_wait() which encapsulates the polling logic used by poll_idle().
- interface suggested by (Rafael J. Wysocki).

v6 [1]:
- fixup missing timeout parameters in atomic64_cond_read_*_timeout()
- remove a race between setting of TIF_NEED_RESCHED and the call to smp_cond_load_relaxed_timeout(). This would mean that dev->poll_time_limit would be set even if we hadn't spent any time waiting.
(The original check compared against local_clock(), which would have been fine, but I was instead using a cheaper check against _TIF_NEED_RESCHED.)
(Both from meta-CI bot)

v5 [2]:
- use cpu_poll_relax() instead of cpu_relax().
- instead of defining an arm64 specific smp_cond_load_relaxed_timeout(), just define the appropriate cpu_poll_relax().
- re-read the target pointer when we exit due to the time-check.
- s/SMP_TIMEOUT_SPIN_COUNT/SMP_TIMEOUT_POLL_COUNT/ (Suggested by Will Deacon)

- add atomic_cond_read_*_timeout() and atomic64_cond_read_*_timeout() interfaces.
- rqspinlock: use atomic_cond_read_acquire_timeout().
- cpuidle: use smp_cond_load_relaxed_tiemout() for polling. (Suggested by Catalin Marinas)

- rqspinlock: define SMP_TIMEOUT_POLL_COUNT to be 16k for non arm64

v4 [3]:
- naming change 's/timewait/timeout/'
- resilient spinlocks: get rid of res_smp_cond_load_acquire_waiting() and fixup use of RES_CHECK_TIMEOUT().
(Both suggested by Catalin Marinas)

v3 [4]:
- further interface simplifications (suggested by Catalin Marinas)

v2 [5]:
- simplified the interface (suggested by Catalin Marinas)
- get rid of wait_policy, and a multitude of constants
- adds a slack parameter
This helped remove a fair amount of duplicated code duplication and in hindsight unnecessary constants.

v1 [6]:
- add wait_policy (coarse and fine)
- derive spin-count etc at runtime instead of using arbitrary constants.

Haris Okanovic tested v4 of this series with poll_idle()/haltpoll patches. [7]

Comments appreciated!

Thanks
Ankur

[0] https://lore.kernel.org/lkml/[email protected]/
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
[5] https://lore.kernel.org/lkml/[email protected]/
[6] https://lore.kernel.org/lkml/[email protected]/
[7] https://lore.kernel.org/lkml/[email protected]/

Cc Arnd Bergmann <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Daniel Lezcano <[email protected]>
Cc Kumar Kartikeya Dwivedi <[email protected]>
Cc Alexei Starovoitov <[email protected]>

Ankur Arora (12):
asm-generic: barrier: Add smp_cond_load_relaxed_timeout()
arm64: barrier: Support smp_cond_load_relaxed_timeout() arm64/delay: move some constants out to a separate header
arm64: support WFET in smp_cond_relaxed_timeout()
arm64: rqspinlock: Remove private copy of
smp_cond_load_acquire_timewait()
asm-generic: barrier: Add smp_cond_load_acquire_timeout()
atomic: Add atomic_cond_read_*_timeout() locking/atomic: scripts: build atomic_long_cond_read_*_timeout() bpf/rqspinlock: switch check_timeout() to a clock interface bpf/rqspinlock: Use smp_cond_load_acquire_timeout()
sched: add need-resched timed wait interface cpuidle/poll_state: Wait for need-resched via
tif_need_resched_relaxed_wait()

 arch/arm64/include/asm/barrier.h     | 23 ++++++++
 arch/arm64/include/asm/cmpxchg.h     | 72 ++++++++++++++++-------
 arch/arm64/include/asm/delay-const.h | 25 ++++++++
 arch/arm64/include/asm/rqspinlock.h  | 85 ----------------------------
 arch/arm64/lib/delay.c               | 13 +----
 drivers/cpuidle/poll_state.c         | 27 ++-------
 drivers/soc/qcom/rpmh-rsc.c          |  9 +--
 include/asm-generic/barrier.h        | 84 +++++++++++++++++++++++++++
 include/linux/atomic.h               | 10 ++++
 include/linux/atomic/atomic-long.h   | 18 +++---
 include/linux/sched/idle.h           | 29 ++++++++++
 kernel/bpf/rqspinlock.c              | 72 ++++++++++++++---------
 scripts/atomic/gen-atomic-long.sh    | 16 ++++--
 13 files changed, 295 insertions(+), 188 deletions(-)
 create mode 100644 arch/arm64/include/asm/delay-const.h
Signature · 1 line
2.31.1
#1 lore raw

PATCH v8 1/12 asm-generic: barrier: Add smp_cond_load_relaxed_timeout()

Add smp_cond_load_relaxed_timeout(), which extends smp_cond_load_relaxed() to allow waiting for a duration.

We loop around waiting for the condition variable to change while peridically doing a time-check. The loop uses cpu_poll_relax() to slow down the busy-waiting, which, unless overridden by the architecture code, amounts to a cpu_relax().

Note that there are two ways for the time-check to fail: once we have timed out or, if the time_expr_ns returns an invalid value (negative or zero).

The number of times we spin before doing the time-check is specified by SMP_TIMEOUT_POLL_COUNT (chosen to be 200 by default) which, assuming each cpu_poll_relax() iteration takes ~20-30 cycles (measured on a variety of x86 platforms), for a total of ~4000-6000 cycles.

That is also the outer limit of the overshoot when working with the parameters above. This might be higher or lesser depending on the implementation of cpu_poll_relax() across architectures.

Cc Arnd Bergmann <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Peter Zijlstra <[email protected]>

---

Notes:

- the interface now breaks the time_check_expr into two parts:
time_expr_ns (evaluates to current time) and remaining_ns. The main reason for doing this was to support WFET and similar primitives which can do timed waiting.

- cpu_poll_relax() now takes an additional paramater to handle that.

- time_expr_ns can now return failure which needs a little more change in organization. This was needed because rqspinlock check_timeout() logic mapped naturally to the unified check in time_check_expr. Breaking up the time_check_expr, however needed check_timeout() to separate a clock interface (which could fail on deadlock or its internal timeout check) and the timeout duration.

- given the changes in logic I've removed Catalin and Haris' R-by and Tested-by.

 include/asm-generic/barrier.h | 58 +++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
include/asm-generic/barrier.h +58 -0
index d4f581c1e21d..e25592f9fcbf 100644
@@ -273,6 +273,64 @@ do {									\
 })
 #endif
 
+/*
+ * Number of times we iterate in the loop before doing the time check.
+ */
+#ifndef SMP_TIMEOUT_POLL_COUNT
+#define SMP_TIMEOUT_POLL_COUNT		200
+#endif
+
+#ifndef cpu_poll_relax
+#define cpu_poll_relax(ptr, val, timeout_ns)	cpu_relax()
+#endif
+
+/**
+ * smp_cond_load_relaxed_timeout() - (Spin) wait for cond with no ordering
+ * guarantees until a timeout expires.
+ * @ptr: pointer to the variable to wait on
+ * @cond: boolean expression to wait for
+ * @time_expr_ns: expression that evaluates to monotonic time (in ns) or,
+ *  on failure, returns a negative value.
+ * @timeout_ns: timeout value in ns
+ * (Both of the above are assumed to be compatible with s64.)
+ *
+ * Equivalent to using READ_ONCE() on the condition variable.
+ */
+#ifndef smp_cond_load_relaxed_timeout
+#define smp_cond_load_relaxed_timeout(ptr, cond_expr,			\
+				      time_expr_ns, timeout_ns)		\
+({									\
+	__label__ __out, __done;					\
+	typeof(ptr) __PTR = (ptr);					\
+	__unqual_scalar_typeof(*ptr) VAL;				\
+	u32 __n = 0, __spin = SMP_TIMEOUT_POLL_COUNT;			\
+	s64 __time_now = (s64)(time_expr_ns);				\
+	s64 __timeout = (s64)timeout_ns;				\
+	s64 __time_end = __time_now + __timeout;			\
+									\
+	if (__time_now <= 0)						\
+		goto __out;						\
+									\
+	for (;;) {							\
+		VAL = READ_ONCE(*__PTR);				\
+		if (cond_expr)						\
+			goto __done;					\
+		cpu_poll_relax(__PTR, VAL, __timeout);			\
+		if (++__n < __spin)					\
+			continue;					\
+		__time_now = (s64)(time_expr_ns);			\
+		__timeout = __time_end - __time_now;			\
+		if (__time_now <= 0 || __timeout <= 0)			\
+			goto __out;					\
+		__n = 0;						\
+	}								\
+__out:									\
+	VAL = READ_ONCE(*__PTR);					\
+__done:								\
+	(typeof(*ptr))VAL;						\
+})
+#endif
+
 /*
  * pmem_wmb() ensures that all stores for which the modification
  * are written to persistent storage by preceding instructions have
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH v8 01/12] asm-generic: barrier: Add smp_cond_load_relaxed_timeout()

Quoted 110 lines · Ankur Arora

Add smp_cond_load_relaxed_timeout(), which extends smp_cond_load_relaxed() to allow waiting for a duration.

We loop around waiting for the condition variable to change while peridically doing a time-check. The loop uses cpu_poll_relax() to slow down the busy-waiting, which, unless overridden by the architecture code, amounts to a cpu_relax().

Note that there are two ways for the time-check to fail: once we have timed out or, if the time_expr_ns returns an invalid value (negative or zero).

The number of times we spin before doing the time-check is specified by SMP_TIMEOUT_POLL_COUNT (chosen to be 200 by default) which, assuming each cpu_poll_relax() iteration takes ~20-30 cycles (measured on a variety of x86 platforms), for a total of ~4000-6000 cycles.

That is also the outer limit of the overshoot when working with the parameters above. This might be higher or lesser depending on the implementation of cpu_poll_relax() across architectures.

Cc Arnd Bergmann <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Peter Zijlstra <[email protected]>

---

Notes:

- the interface now breaks the time_check_expr into two parts:
time_expr_ns (evaluates to current time) and remaining_ns. The main reason for doing this was to support WFET and similar primitives which can do timed waiting.

- cpu_poll_relax() now takes an additional paramater to handle that.

- time_expr_ns can now return failure which needs a little more change in organization. This was needed because rqspinlock check_timeout() logic mapped naturally to the unified check in time_check_expr. Breaking up the time_check_expr, however needed check_timeout() to separate a clock interface (which could fail on deadlock or its internal timeout check) and the timeout duration.

- given the changes in logic I've removed Catalin and Haris' R-by and Tested-by.

 include/asm-generic/barrier.h | 58 +++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
include/asm-generic/barrier.h +51 -0
index d4f581c1e21d..e25592f9fcbf 100644
@@ -273,6 +273,64 @@ do {									\
 })
 #endif
 
+/*
+ * Number of times we iterate in the loop before doing the time check.
+ */
+#ifndef SMP_TIMEOUT_POLL_COUNT
+#define SMP_TIMEOUT_POLL_COUNT		200
+#endif
+
+#ifndef cpu_poll_relax
+#define cpu_poll_relax(ptr, val, timeout_ns)	cpu_relax()
+#endif
+
+/**
+ * smp_cond_load_relaxed_timeout() - (Spin) wait for cond with no ordering
+ * guarantees until a timeout expires.
+ * @ptr: pointer to the variable to wait on
+ * @cond: boolean expression to wait for
+ * @time_expr_ns: expression that evaluates to monotonic time (in ns) or,
+ *  on failure, returns a negative value.
+ * @timeout_ns: timeout value in ns
+ * (Both of the above are assumed to be compatible with s64.)
+ *
+ * Equivalent to using READ_ONCE() on the condition variable.
+ */
+#ifndef smp_cond_load_relaxed_timeout
+#define smp_cond_load_relaxed_timeout(ptr, cond_expr,			\
+				      time_expr_ns, timeout_ns)		\
+({									\
+	__label__ __out, __done;					\
+	typeof(ptr) __PTR = (ptr);					\
+	__unqual_scalar_typeof(*ptr) VAL;				\
+	u32 __n = 0, __spin = SMP_TIMEOUT_POLL_COUNT;			\
+	s64 __time_now = (s64)(time_expr_ns);				\
+	s64 __timeout = (s64)timeout_ns;				\
+	s64 __time_end = __time_now + __timeout;			\
+									\
+	if (__time_now <= 0)						\
+		goto __out;						\
+									\
+	for (;;) {							\
+		VAL = READ_ONCE(*__PTR);				\
+		if (cond_expr)						\
+			goto __done;					\
+		cpu_poll_relax(__PTR, VAL, __timeout);			\
+		if (++__n < __spin)					\
+			continue;					\
+		__time_now = (s64)(time_expr_ns);			\
+		__timeout = __time_end - __time_now;			\
+		if (__time_now <= 0 || __timeout <= 0)			\
+			goto __out;					\
+		__n = 0;						\
+	}								\

include/asm-generic/barrier.h do { \ · line 336

Quoted 10 lines
include/asm-generic/barrier.h +7 -0
+__out:									\
+	VAL = READ_ONCE(*__PTR);					\
+__done:								\
+	(typeof(*ptr))VAL;						\
+})
+#endif
+
 /*
  * pmem_wmb() ensures that all stores for which the modification
  * are written to persistent storage by preceding instructions have

Alexei Starovoitov pointed out in his review comment on patch-10 that evaluating time_expr_ns on entry means that this is unusable in the fastpath of something like lock acquisition (as rqspinlock does).

Something like the following should work better (it also gets rid of the gotos.) The only negative is that in the slowpath this would have a larger overshoot but I think that's a reasonable tradeoff.

I'll be sending out the next version with this changed but just wanted to gauge any preliminary opinions on this.

Thanks
Ankur

---

include/asm-generic/barrier.h +67 -0
index d4f581c1e21d..278eb51cf354 100644
@@ -273,6 +273,73 @@ do {									\
 })
 #endif
 
+/*
+ * Number of times we iterate in the loop before doing the time check.
+ */
+#ifndef SMP_TIMEOUT_POLL_COUNT
+#define SMP_TIMEOUT_POLL_COUNT		200
+#endif
+
+#ifndef cpu_poll_relax
+#define cpu_poll_relax(ptr, val, timeout_ns)	cpu_relax()
+#endif
+
+/**
+ * smp_cond_load_relaxed_timeout() - (Spin) wait for cond with no ordering
+ * guarantees until a timeout expires.
+ * @ptr: pointer to the variable to wait on
+ * @cond: boolean expression to wait for
+ * @time_expr_ns: expression that evaluates to monotonic time (in ns) or,
+ *  on failure, returns a negative value.
+ * @timeout_ns: timeout value in ns
+ * (Both of the above are assumed to be compatible with s64.)
+ *
+ * Equivalent to using READ_ONCE() on the condition variable.
+ *
+ * Note on timeout overshoot: in the fastpath, we want to keep this as close
+ * to performance as smp_cond_load_relaxed(). To do that we want to defer
+ * the evaluation of the potentially expensive @time_expr_ns to the
+ * earliest time that is needed.
+ *
+ * This means that there will always be some hardware dependent duration
+ * that has already passed inside cpu_poll_relax() at the time of first
+ * evaluation. In addition, cpu_poll_relax() is not guaranteed to return
+ * at timeout boundary.
+ *
+ * In sum, expect timeout overshoot when we return due to expiration of
+ * the timeout.
+ */
+#ifndef smp_cond_load_relaxed_timeout
+#define smp_cond_load_relaxed_timeout(ptr, cond_expr,			\
+				      time_expr_ns, timeout_ns)		\
+({									\
+	typeof(ptr) __PTR = (ptr);					\
+	__unqual_scalar_typeof(*ptr) VAL;				\
+	u32 __n = 0, __spin = SMP_TIMEOUT_POLL_COUNT;			\
+	s64 __timeout = (s64)timeout_ns;				\
+	s64 __time_now, __time_end = 0;					\
+									\
+	for (;;) {							\
+		VAL = READ_ONCE(*__PTR);				\
+		if (cond_expr) 						\
+			break;						\
+		cpu_poll_relax(__PTR, VAL, __timeout);			\
+		if (++__n < __spin)					\
+			continue;					\
+		__time_now = (s64)(time_expr_ns);			\
+		if (unlikely(__time_end == 0))				\
+			__time_end = __time_now + __timeout;		\
+		__timeout = __time_end - __time_now;			\
+		if (__time_now <= 0 || __timeout <= 0) {		\
+			VAL = READ_ONCE(*__PTR);			\
+			break;						\
+		}							\
+		__n = 0;						\
+	}								\
+	(typeof(*ptr))VAL;						\
+})
+#endif
+
 /*
  * pmem_wmb() ensures that all stores for which the modification
  * are written to persistent storage by preceding instructions have
#3 lore raw

PATCH v8 2/12 arm64: barrier: Support smp_cond_load_relaxed_timeout()

In reply to Ankur Arora

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 upto the event-stream period. And, in the unlikely event that the event-stream is unavailable, fallback to spin-waiting.

Also set SMP_TIMEOUT_POLL_COUNT to 1 so we do the time-check in each iteration of smp_cond_load_relaxed_timeout().

---

Notes:
- cpu_poll_relax() now takes an additional parameter.

- added a comment detailing why we define SMP_TIMEOUT_POLL_COUNT=1 and how it ties up with smp_cond_load_relaxed_timeout().

- explicitly include <asm/vdso/processor.h> for cpu_relax().

 arch/arm64/include/asm/barrier.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/arm64/include/asm/barrier.h +21 -0
index 9495c4441a46..6190e178db51 100644
@@ -12,6 +12,7 @@
 #include <linux/kasan-checks.h>
 
 #include <asm/alternative-macros.h>
+#include <asm/vdso/processor.h>
 
 #define __nops(n)	".rept	" #n "\nnop\n.endr\n"
 #define nops(n)		asm volatile(__nops(n))
@@ -219,6 +220,26 @@ 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 the ptr value to change.
+ *
+ * Since this period is reasonably long, choose SMP_TIMEOUT_POLL_COUNT
+ * to be 1, so smp_cond_load_{relaxed,acquire}_timeout() does a
+ * time-check in each iteration.
+ */
+#define SMP_TIMEOUT_POLL_COUNT	1
+
+#define cpu_poll_relax(ptr, val, timeout_ns) do {			\
+	if (arch_timer_evtstrm_available())				\
+		__cmpwait_relaxed(ptr, val);				\
+	else								\
+		cpu_relax();						\
+} while (0)
+
 #include <asm-generic/barrier.h>
 
 #endif	/* __ASSEMBLER__ */
Signature · 1 line
2.31.1
maintainer Acked-by on v8 2/12
#4 lore raw

Re: [PATCH v8 02/12] arm64: barrier: Support smp_cond_load_relaxed_timeout()

Quoted 57 lines · Ankur Arora

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 upto the event-stream period. And, in the unlikely event that the event-stream is unavailable, fallback to spin-waiting.

Also set SMP_TIMEOUT_POLL_COUNT to 1 so we do the time-check in each iteration of smp_cond_load_relaxed_timeout().

---

Notes:
- cpu_poll_relax() now takes an additional parameter.

- added a comment detailing why we define SMP_TIMEOUT_POLL_COUNT=1 and how it ties up with smp_cond_load_relaxed_timeout().

- explicitly include <asm/vdso/processor.h> for cpu_relax().

 arch/arm64/include/asm/barrier.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/arm64/include/asm/barrier.h +10 -0
index 9495c4441a46..6190e178db51 100644
@@ -12,6 +12,7 @@
 #include <linux/kasan-checks.h>
 
 #include <asm/alternative-macros.h>
+#include <asm/vdso/processor.h>
 
 #define __nops(n)	".rept	" #n "\nnop\n.endr\n"
 #define nops(n)		asm volatile(__nops(n))
@@ -219,6 +220,26 @@ 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 the ptr value to change.
+ *
+ * Since this period is reasonably long, choose SMP_TIMEOUT_POLL_COUNT
+ * to be 1, so smp_cond_load_{relaxed,acquire}_timeout() does a

arch/arm64/include/asm/barrier.h do { \ · line 241

Quoted 10 lines
arch/arm64/include/asm/barrier.h +10 -0
+ * time-check in each iteration.
+ */
+#define SMP_TIMEOUT_POLL_COUNT	1
+
+#define cpu_poll_relax(ptr, val, timeout_ns) do {			\
+	if (arch_timer_evtstrm_available())				\
+		__cmpwait_relaxed(ptr, val);				\
+	else								\
+		cpu_relax();						\
+} while (0)

Will

#5 lore raw

Re: [PATCH v8 02/12] arm64: barrier: Support smp_cond_load_relaxed_timeout()

Quoted 62 lines · Will Deacon
Quoted 61 lines · Ankur Arora

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 upto the event-stream period. And, in the unlikely event that the event-stream is unavailable, fallback to spin-waiting.

Also set SMP_TIMEOUT_POLL_COUNT to 1 so we do the time-check in each iteration of smp_cond_load_relaxed_timeout().

---

Notes:
- cpu_poll_relax() now takes an additional parameter.

- added a comment detailing why we define SMP_TIMEOUT_POLL_COUNT=1 and how it ties up with smp_cond_load_relaxed_timeout().

- explicitly include <asm/vdso/processor.h> for cpu_relax().

 arch/arm64/include/asm/barrier.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/arm64/include/asm/barrier.h +14 -0
index 9495c4441a46..6190e178db51 100644
@@ -12,6 +12,7 @@
 #include <linux/kasan-checks.h>
 
 #include <asm/alternative-macros.h>
+#include <asm/vdso/processor.h>
 
 #define __nops(n)	".rept	" #n "\nnop\n.endr\n"
 #define nops(n)		asm volatile(__nops(n))
@@ -219,6 +220,26 @@ 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 the ptr value to change.
+ *
+ * Since this period is reasonably long, choose SMP_TIMEOUT_POLL_COUNT
+ * to be 1, so smp_cond_load_{relaxed,acquire}_timeout() does a
+ * time-check in each iteration.
+ */
+#define SMP_TIMEOUT_POLL_COUNT	1
+
Quoted 8 lines

arch/arm64/include/asm/barrier.h do { \ · line 241

Quoted 6 lines
arch/arm64/include/asm/barrier.h +6 -0
+#define cpu_poll_relax(ptr, val, timeout_ns) do {			\
+	if (arch_timer_evtstrm_available())				\
+		__cmpwait_relaxed(ptr, val);				\
+	else								\
+		cpu_relax();						\
+} while (0)

Thanks!

--
ankur

#6 lore raw

PATCH v8 3/12 arm64/delay: move some constants out to a separate header

In reply to Ankur Arora

Moves some constants and functions related to xloops, cycles computation out to a new header.

No functional change.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Bjorn Andersson <[email protected]>
Cc Konrad Dybcio <[email protected]>
---
 arch/arm64/include/asm/delay-const.h | 25 +++++++++++++++++++++++++
 arch/arm64/lib/delay.c               | 13 +++----------
 drivers/soc/qcom/rpmh-rsc.c          |  9 +--------
 3 files changed, 29 insertions(+), 18 deletions(-)
 create mode 100644 arch/arm64/include/asm/delay-const.h
arch/arm64/include/asm/delay-const.h new file +25 -0
new file mode 100644
index 000000000000..63fb5fc24a90
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASM_DELAY_CONST_H
+#define _ASM_DELAY_CONST_H
+
+#include <asm/param.h>	/* For HZ */
+
+/* 2**32 / 1000000 (rounded up) */
+#define __usecs_to_xloops_mult	0x10C7UL
+
+/* 2**32 / 1000000000 (rounded up) */
+#define __nsecs_to_xloops_mult	0x5UL
+
+extern unsigned long loops_per_jiffy;
+static inline unsigned long xloops_to_cycles(unsigned long xloops)
+{
+	return (xloops * loops_per_jiffy * HZ) >> 32;
+}
+
+#define USECS_TO_CYCLES(time_usecs) \
+	xloops_to_cycles((time_usecs) * __usecs_to_xloops_mult)
+
+#define NSECS_TO_CYCLES(time_nsecs) \
+	xloops_to_cycles((time_nsecs) * __nsecs_to_xloops_mult)
+
+#endif	/* _ASM_DELAY_CONST_H */
arch/arm64/lib/delay.c +3 -10
index cb2062e7e234..511b5597e2a5 100644
@@ -12,17 +12,10 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/timex.h>
+#include <asm/delay-const.h>
 
 #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();
@@ -58,12 +51,12 @@ EXPORT_SYMBOL(__const_udelay);
 
 void __udelay(unsigned long usecs)
 {
-	__const_udelay(usecs * 0x10C7UL); /* 2**32 / 1000000 (rounded up) */
+	__const_udelay(usecs * __usecs_to_xloops_mult);
 }
 EXPORT_SYMBOL(__udelay);
 
 void __ndelay(unsigned long nsecs)
 {
-	__const_udelay(nsecs * 0x5UL); /* 2**32 / 1000000000 (rounded up) */
+	__const_udelay(nsecs * __nsecs_to_xloops_mult);
 }
 EXPORT_SYMBOL(__ndelay);
drivers/soc/qcom/rpmh-rsc.c +1 -8
index c6f7d5c9c493..95962fc37295 100644
@@ -26,6 +26,7 @@
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/wait.h>
+#include <asm/delay-const.h>
 
 #include <clocksource/arm_arch_timer.h>
 #include <soc/qcom/cmd-db.h>
@@ -146,14 +147,6 @@ enum {
  *  +---------------------------------------------------+
  */
 
-#define USECS_TO_CYCLES(time_usecs)			\
-	xloops_to_cycles((time_usecs) * 0x10C7UL)
-
-static inline unsigned long xloops_to_cycles(u64 xloops)
-{
-	return (xloops * loops_per_jiffy * HZ) >> 32;
-}
-
 static u32 rpmh_rsc_reg_offset_ver_2_7[] = {
 	[RSC_DRV_TCS_OFFSET]		= 672,
 	[RSC_DRV_CMD_OFFSET]		= 20,
Signature · 1 line
2.31.1
#7 lore raw

Re: [PATCH v8 03/12] arm64/delay: move some constants out to a separate header

kernel test robot noticed the following build errors:

Hi Ankur,

kernel test robot noticed the following build errors:

[auto build test ERROR on bpf-next/master]
[also build test ERROR on bpf/master linus/master arnd-asm-generic/master v6.19-rc1 next-20251215] [cannot apply to arm64/for-next/core bpf-next/net]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ankur-Arora/asm-generic-barrier-Add-smp_cond_load_relaxed_timeout/20251215-130116
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20251215044919.460086-4-ankur.a.arora%40oracle.com
patch subject: [PATCH v8 03/12] arm64/delay: move some constants out to a separate header
config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20251216/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251216/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/soc/qcom/rpmh-rsc.c:29:10: fatal error: asm/delay-const.h: No such file or directory
      29 | #include <asm/delay-const.h>
         |          ^~~~~~~~~~~~~~~~~~~

compilation terminated.

vim +29 drivers/soc/qcom/rpmh-rsc.c

     8	
     9	#include <linux/atomic.h>
    10	#include <linux/cpu_pm.h>
    11	#include <linux/delay.h>
    12	#include <linux/interrupt.h>
    13	#include <linux/io.h>
    14	#include <linux/iopoll.h>
    15	#include <linux/kernel.h>
    16	#include <linux/ktime.h>
    17	#include <linux/list.h>
    18	#include <linux/module.h>
    19	#include <linux/notifier.h>
    20	#include <linux/of.h>
    21	#include <linux/of_irq.h>
    22	#include <linux/of_platform.h>
    23	#include <linux/platform_device.h>
    24	#include <linux/pm_domain.h>
    25	#include <linux/pm_runtime.h>
    26	#include <linux/slab.h>
    27	#include <linux/spinlock.h>
    28	#include <linux/wait.h>
  > 29	#include <asm/delay-const.h>
    30	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#8 lore raw

Re: [PATCH v8 03/12] arm64/delay: move some constants out to a separate header

In reply to Ankur Arora
kernel test robot noticed the following build errors:

Hi Ankur,

kernel test robot noticed the following build errors:

[auto build test ERROR on bpf-next/master]
[also build test ERROR on bpf/master linus/master arnd-asm-generic/master v6.19-rc1 next-20251218] [cannot apply to arm64/for-next/core bpf-next/net]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ankur-Arora/asm-generic-barrier-Add-smp_cond_load_relaxed_timeout/20251215-130116
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20251215044919.460086-4-ankur.a.arora%40oracle.com
patch subject: [PATCH v8 03/12] arm64/delay: move some constants out to a separate header
config: x86_64-buildonly-randconfig-004-20251215 (https://download.01.org/0day-ci/archive/20251219/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251219/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/soc/qcom/rpmh-rsc.c:29:10: fatal error: 'asm/delay-const.h' file not found
      29 | #include <asm/delay-const.h>
         |          ^~~~~~~~~~~~~~~~~~~

1 error generated.

vim +29 drivers/soc/qcom/rpmh-rsc.c

     8	
     9	#include <linux/atomic.h>
    10	#include <linux/cpu_pm.h>
    11	#include <linux/delay.h>
    12	#include <linux/interrupt.h>
    13	#include <linux/io.h>
    14	#include <linux/iopoll.h>
    15	#include <linux/kernel.h>
    16	#include <linux/ktime.h>
    17	#include <linux/list.h>
    18	#include <linux/module.h>
    19	#include <linux/notifier.h>
    20	#include <linux/of.h>
    21	#include <linux/of_irq.h>
    22	#include <linux/of_platform.h>
    23	#include <linux/platform_device.h>
    24	#include <linux/pm_domain.h>
    25	#include <linux/pm_runtime.h>
    26	#include <linux/slab.h>
    27	#include <linux/spinlock.h>
    28	#include <linux/wait.h>
  > 29	#include <asm/delay-const.h>
    30	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#9 lore raw

PATCH v8 4/12 arm64: support WFET in smp_cond_relaxed_timeout()

In reply to Ankur Arora

Extend __cmpwait_relaxed() to __cmpwait_relaxed_timeout() which takes an additional timeout value in ns.

Lacking WFET, or with zero or negative value of timeout we fallback to WFE.

Cc Arnd Bergmann <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
---
 arch/arm64/include/asm/barrier.h |  8 ++--
 arch/arm64/include/asm/cmpxchg.h | 72 ++++++++++++++++++++++----------
 2 files changed, 55 insertions(+), 25 deletions(-)
arch/arm64/include/asm/barrier.h +5 -3
index 6190e178db51..fbd71cd4ef4e 100644
@@ -224,8 +224,8 @@ do {									\
 extern bool arch_timer_evtstrm_available(void);
 
 /*
- * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed()
- * for the ptr value to change.
+ * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed()/
+ * __cmpwait_relaxed_timeout() for the ptr value to change.
  *
  * Since this period is reasonably long, choose SMP_TIMEOUT_POLL_COUNT
  * to be 1, so smp_cond_load_{relaxed,acquire}_timeout() does a
@@ -234,7 +234,9 @@ extern bool arch_timer_evtstrm_available(void);
 #define SMP_TIMEOUT_POLL_COUNT	1
 
 #define cpu_poll_relax(ptr, val, timeout_ns) do {			\
-	if (arch_timer_evtstrm_available())				\
+	if (alternative_has_cap_unlikely(ARM64_HAS_WFXT))		\
+		__cmpwait_relaxed_timeout(ptr, val, timeout_ns);	\
+	else if (arch_timer_evtstrm_available())			\
 		__cmpwait_relaxed(ptr, val);				\
 	else								\
 		cpu_relax();						\
arch/arm64/include/asm/cmpxchg.h +50 -22

102 lines not on this page · raw message

Signature · 1 line
2.31.1
maintainer
#10 lore raw

Re: [PATCH v8 04/12] arm64: support WFET in smp_cond_relaxed_timeout()

Quoted 22 lines · Ankur Arora
arch/arm64/include/asm/cmpxchg.h +22 -0
+#define __CMPWAIT_CASE(w, sfx, sz)						\
+static inline void __cmpwait_case_##sz(volatile void *ptr,			\
+				       unsigned long val,			\
+				       s64 timeout_ns)				\
+{										\
+	unsigned long tmp;							\
+										\
+	if (!alternative_has_cap_unlikely(ARM64_HAS_WFXT) || timeout_ns <= 0) {	\
+		asm volatile(							\
+		"	sevl\n"							\
+		"	wfe\n"							\
+		"	ldxr" #sfx "\t%" #w "[tmp], %[v]\n"			\
+		"	eor	%" #w "[tmp], %" #w "[tmp], %" #w "[val]\n"	\
+		"	cbnz	%" #w "[tmp], 1f\n"				\
+		"	wfe\n"							\
+		"1:"								\
+		: [tmp] "=&r" (tmp), [v] "+Q" (*(u##sz *)ptr)			\
+		: [val] "r" (val));						\
+	} else {								\
+		u64 ecycles = arch_timer_read_counter() +			\
+				NSECS_TO_CYCLES(timeout_ns);			\
+		asm volatile(							\

arch/arm64/include/asm/cmpxchg.h __CMPXCHG_GEN(_mb) · line 246

Quoted 10 lines
arch/arm64/include/asm/cmpxchg.h +10 -0
+		"	sevl\n"							\
+		"	wfe\n"							\
+		"	ldxr" #sfx "\t%" #w "[tmp], %[v]\n"			\
+		"	eor	%" #w "[tmp], %" #w "[tmp], %" #w "[val]\n"	\
+		"	cbnz	%" #w "[tmp], 2f\n"				\
+		"	msr s0_3_c1_c0_0, %[ecycles]\n"				\
+		"2:"								\
+		: [tmp] "=&r" (tmp), [v] "+Q" (*(u##sz *)ptr)			\
+		: [val] "r" (val), [ecycles] "r" (ecycles));			\
+	}									\

Why not have a separate helper for the WFXT version and avoid the runtime check on timeout_ns?

Will

#11 lore raw

Re: [PATCH v8 04/12] arm64: support WFET in smp_cond_relaxed_timeout()

Quoted 28 lines · Will Deacon
Quoted 27 lines · Ankur Arora
arch/arm64/include/asm/cmpxchg.h +27 -0
+#define __CMPWAIT_CASE(w, sfx, sz)						\
+static inline void __cmpwait_case_##sz(volatile void *ptr,			\
+				       unsigned long val,			\
+				       s64 timeout_ns)				\
+{										\
+	unsigned long tmp;							\
+										\
+	if (!alternative_has_cap_unlikely(ARM64_HAS_WFXT) || timeout_ns <= 0) {	\
+		asm volatile(							\
+		"	sevl\n"							\
+		"	wfe\n"							\
+		"	ldxr" #sfx "\t%" #w "[tmp], %[v]\n"			\
+		"	eor	%" #w "[tmp], %" #w "[tmp], %" #w "[val]\n"	\
+		"	cbnz	%" #w "[tmp], 1f\n"				\
+		"	wfe\n"							\
+		"1:"								\
+		: [tmp] "=&r" (tmp), [v] "+Q" (*(u##sz *)ptr)			\
+		: [val] "r" (val));						\
+	} else {								\
+		u64 ecycles = arch_timer_read_counter() +			\
+				NSECS_TO_CYCLES(timeout_ns);			\
+		asm volatile(							\
+		"	sevl\n"							\
+		"	wfe\n"							\
+		"	ldxr" #sfx "\t%" #w "[tmp], %[v]\n"			\
+		"	eor	%" #w "[tmp], %" #w "[tmp], %" #w "[val]\n"	\
+		"	cbnz	%" #w "[tmp], 2f\n"				\
Quoted 8 lines

arch/arm64/include/asm/cmpxchg.h __CMPXCHG_GEN(_mb) · line 246

Quoted 5 lines
arch/arm64/include/asm/cmpxchg.h +5 -0
+		"	msr s0_3_c1_c0_0, %[ecycles]\n"				\
+		"2:"								\
+		: [tmp] "=&r" (tmp), [v] "+Q" (*(u##sz *)ptr)			\
+		: [val] "r" (val), [ecycles] "r" (ecycles));			\
+	}									\

Why not have a separate helper for the WFXT version and avoid the runtime check on timeout_ns?

My main reason for keeping them together was that a separate helper needed duplication of a lot of the __CMPWAIT_CASE and __CMPWAIT_GEN stuff.

Relooking at it, I think we could get by without duplicating the __CMPWAIT_GEN (the WFE helper just needs to take an unused timeout_ns paramter).

But, it seems overkill to get rid of the unnecessary check on timeout_ns (which AFAICT should be well predicted) and the duplicate static branch.

--
ankur

maintainer
#12 lore raw

Re: [PATCH v8 04/12] arm64: support WFET in smp_cond_relaxed_timeout()

Quoted 44 lines · Ankur Arora
Quoted 28 lines · Will Deacon
Quoted 27 lines · Ankur Arora
arch/arm64/include/asm/cmpxchg.h +27 -0
+#define __CMPWAIT_CASE(w, sfx, sz)						\
+static inline void __cmpwait_case_##sz(volatile void *ptr,			\
+				       unsigned long val,			\
+				       s64 timeout_ns)				\
+{										\
+	unsigned long tmp;							\
+										\
+	if (!alternative_has_cap_unlikely(ARM64_HAS_WFXT) || timeout_ns <= 0) {	\
+		asm volatile(							\
+		"	sevl\n"							\
+		"	wfe\n"							\
+		"	ldxr" #sfx "\t%" #w "[tmp], %[v]\n"			\
+		"	eor	%" #w "[tmp], %" #w "[tmp], %" #w "[val]\n"	\
+		"	cbnz	%" #w "[tmp], 1f\n"				\
+		"	wfe\n"							\
+		"1:"								\
+		: [tmp] "=&r" (tmp), [v] "+Q" (*(u##sz *)ptr)			\
+		: [val] "r" (val));						\
+	} else {								\
+		u64 ecycles = arch_timer_read_counter() +			\
+				NSECS_TO_CYCLES(timeout_ns);			\
+		asm volatile(							\
+		"	sevl\n"							\
+		"	wfe\n"							\
+		"	ldxr" #sfx "\t%" #w "[tmp], %[v]\n"			\
+		"	eor	%" #w "[tmp], %" #w "[tmp], %" #w "[val]\n"	\
+		"	cbnz	%" #w "[tmp], 2f\n"				\
Quoted 8 lines

arch/arm64/include/asm/cmpxchg.h __CMPXCHG_GEN(_mb) · line 246

Quoted 5 lines
arch/arm64/include/asm/cmpxchg.h +5 -0
+		"	msr s0_3_c1_c0_0, %[ecycles]\n"				\
+		"2:"								\
+		: [tmp] "=&r" (tmp), [v] "+Q" (*(u##sz *)ptr)			\
+		: [val] "r" (val), [ecycles] "r" (ecycles));			\
+	}									\

Why not have a separate helper for the WFXT version and avoid the runtime check on timeout_ns?

My main reason for keeping them together was that a separate helper needed duplication of a lot of the __CMPWAIT_CASE and __CMPWAIT_GEN stuff.

Quoted 6 lines

Relooking at it, I think we could get by without duplicating the __CMPWAIT_GEN (the WFE helper just needs to take an unused timeout_ns paramter).

But, it seems overkill to get rid of the unnecessary check on timeout_ns (which AFAICT should be well predicted) and the duplicate static branch.

tbh, I was actually struggling to see what the check achieves. In fact, why is 'timeout_ns' signed in the first place? Has BPF invented time travel now? :p

If the requested timeout is 0 then we should return immediately (or issue a WFET which will wake up immediately).

If the requested timeout is negative, then WFET may end up with a really long timeout, but that should still be no worse than a plain WFE.

If the check is only there to de-multiplex __cmpwait() vs __cmpwait_relaxed_timeout() as the caller, then I think we should just avoid muxing them in the first place. The duplication argument doesn't hold a lot of water when the asm block is already mostly copy-paste.

Will

#13 lore raw

Re: [PATCH v8 04/12] arm64: support WFET in smp_cond_relaxed_timeout()

Quoted 49 lines · Will Deacon
Quoted 48 lines · Ankur Arora
Quoted 28 lines · Will Deacon
Quoted 27 lines · Ankur Arora
arch/arm64/include/asm/cmpxchg.h +27 -0
+#define __CMPWAIT_CASE(w, sfx, sz)						\
+static inline void __cmpwait_case_##sz(volatile void *ptr,			\
+				       unsigned long val,			\
+				       s64 timeout_ns)				\
+{										\
+	unsigned long tmp;							\
+										\
+	if (!alternative_has_cap_unlikely(ARM64_HAS_WFXT) || timeout_ns <= 0) {	\
+		asm volatile(							\
+		"	sevl\n"							\
+		"	wfe\n"							\
+		"	ldxr" #sfx "\t%" #w "[tmp], %[v]\n"			\
+		"	eor	%" #w "[tmp], %" #w "[tmp], %" #w "[val]\n"	\
+		"	cbnz	%" #w "[tmp], 1f\n"				\
+		"	wfe\n"							\
+		"1:"								\
+		: [tmp] "=&r" (tmp), [v] "+Q" (*(u##sz *)ptr)			\
+		: [val] "r" (val));						\
+	} else {								\
+		u64 ecycles = arch_timer_read_counter() +			\
+				NSECS_TO_CYCLES(timeout_ns);			\
+		asm volatile(							\
+		"	sevl\n"							\
+		"	wfe\n"							\
+		"	ldxr" #sfx "\t%" #w "[tmp], %[v]\n"			\
+		"	eor	%" #w "[tmp], %" #w "[tmp], %" #w "[val]\n"	\
+		"	cbnz	%" #w "[tmp], 2f\n"				\
Quoted 8 lines

arch/arm64/include/asm/cmpxchg.h __CMPXCHG_GEN(_mb) · line 246

Quoted 5 lines
arch/arm64/include/asm/cmpxchg.h +5 -0
+		"	msr s0_3_c1_c0_0, %[ecycles]\n"				\
+		"2:"								\
+		: [tmp] "=&r" (tmp), [v] "+Q" (*(u##sz *)ptr)			\
+		: [val] "r" (val), [ecycles] "r" (ecycles));			\
+	}									\

Why not have a separate helper for the WFXT version and avoid the runtime check on timeout_ns?

My main reason for keeping them together was that a separate helper needed duplication of a lot of the __CMPWAIT_CASE and __CMPWAIT_GEN stuff.

Relooking at it, I think we could get by without duplicating the __CMPWAIT_GEN (the WFE helper just needs to take an unused timeout_ns paramter).

Quoted 6 lines
Quoted 2 lines

But, it seems overkill to get rid of the unnecessary check on timeout_ns (which AFAICT should be well predicted) and the duplicate static branch.

tbh, I was actually struggling to see what the check achieves. In fact, why is 'timeout_ns' signed in the first place? Has BPF invented time travel now? :p

Worse. I had to invent it for them :D.

The BPF rqspinlock needs to be able to return failure (-EDEADLOCK, -ETIMEDOUT). What seemed to me to be the most natural way was for the clock used by rqspinlock itself returning either the clock value or an error value.

So that necessitated the top level timeout_ns to be signed.

Now the error would get filtered out by smp_cond_load_relaxed_timeout() so cpu_poll_relax() should be called with a positive value of timeout_ns.

Quoted 5 lines

If the requested timeout is 0 then we should return immediately (or issue a WFET which will wake up immediately).

If the requested timeout is negative, then WFET may end up with a really long timeout, but that should still be no worse than a plain WFE.

cpu_poll_relax() should not be called with 0 or a negative value. I'll add a comment to that effect.

Quoted 3 lines

If the check is only there to de-multiplex __cmpwait() vs __cmpwait_relaxed_timeout() as the caller, then I think we should just avoid muxing them in the first place.

Yes that's a good point. That the only reason that check exists. And we can do without it since cpu_poll_relax() has all the information it needs to do the de-multiplexing.

Quoted 2 lines

The duplication argument doesn't
hold a lot of water when the asm block is already mostly copy-paste.

Fair enough.

--
ankur

maintainer
#14 lore raw

Re: [PATCH v8 04/12] arm64: support WFET in smp_cond_relaxed_timeout()

In reply to Ankur Arora
Quoted 11 lines · Ankur Arora

Extend __cmpwait_relaxed() to __cmpwait_relaxed_timeout() which takes an additional timeout value in ns.

Lacking WFET, or with zero or negative value of timeout we fallback to WFE.

Cc Arnd Bergmann <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Quoted 4 lines
---
 arch/arm64/include/asm/barrier.h |  8 ++--
 arch/arm64/include/asm/cmpxchg.h | 72 ++++++++++++++++++++++----------
 2 files changed, 55 insertions(+), 25 deletions(-)

Sorry, just spotted something else on this...

Quoted 16 lines
arch/arm64/include/asm/barrier.h +2 -2
index 6190e178db51..fbd71cd4ef4e 100644
@@ -224,8 +224,8 @@ do {									\
 extern bool arch_timer_evtstrm_available(void);
 
 /*
- * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed()
- * for the ptr value to change.
+ * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed()/
+ * __cmpwait_relaxed_timeout() for the ptr value to change.
  *
  * Since this period is reasonably long, choose SMP_TIMEOUT_POLL_COUNT
  * to be 1, so smp_cond_load_{relaxed,acquire}_timeout() does a

arch/arm64/include/asm/barrier.h extern bool arch_timer_evtstrm_available(void); · line 240

Quoted 8 lines
arch/arm64/include/asm/barrier.h +3 -1
@@ -234,7 +234,9 @@ extern bool arch_timer_evtstrm_available(void);
 #define SMP_TIMEOUT_POLL_COUNT	1
 
 #define cpu_poll_relax(ptr, val, timeout_ns) do {			\
-	if (arch_timer_evtstrm_available())				\
+	if (alternative_has_cap_unlikely(ARM64_HAS_WFXT))		\
+		__cmpwait_relaxed_timeout(ptr, val, timeout_ns);	\
+	else if (arch_timer_evtstrm_available())			\
 		__cmpwait_relaxed(ptr, val);				\

Don't you want to make sure that we have the event stream available for __cmpwait_relaxed_timeout() too? Otherwise, a large timeout is going to cause problems.

Will

#15 lore raw

Re: [PATCH v8 04/12] arm64: support WFET in smp_cond_relaxed_timeout()

Quoted 37 lines · Will Deacon
Quoted 11 lines · Ankur Arora

Extend __cmpwait_relaxed() to __cmpwait_relaxed_timeout() which takes an additional timeout value in ns.

Lacking WFET, or with zero or negative value of timeout we fallback to WFE.

Cc Arnd Bergmann <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Quoted 4 lines
---
 arch/arm64/include/asm/barrier.h |  8 ++--
 arch/arm64/include/asm/cmpxchg.h | 72 ++++++++++++++++++++++----------
 2 files changed, 55 insertions(+), 25 deletions(-)

Sorry, just spotted something else on this...

Quoted 18 lines
arch/arm64/include/asm/barrier.h +2 -2
index 6190e178db51..fbd71cd4ef4e 100644
@@ -224,8 +224,8 @@ do {									\
 extern bool arch_timer_evtstrm_available(void);
 
 /*
- * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed()
- * for the ptr value to change.
+ * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed()/
+ * __cmpwait_relaxed_timeout() for the ptr value to change.
  *
  * Since this period is reasonably long, choose SMP_TIMEOUT_POLL_COUNT
  * to be 1, so smp_cond_load_{relaxed,acquire}_timeout() does a
@@ -234,7 +234,9 @@ extern bool arch_timer_evtstrm_available(void);
 #define SMP_TIMEOUT_POLL_COUNT	1
 
Quoted 10 lines

arch/arm64/include/asm/barrier.h extern bool arch_timer_evtstrm_available(void); · line 240

Quoted 6 lines
arch/arm64/include/asm/barrier.h +3 -1
 #define cpu_poll_relax(ptr, val, timeout_ns) do {			\
-	if (arch_timer_evtstrm_available())				\
+	if (alternative_has_cap_unlikely(ARM64_HAS_WFXT))		\
+		__cmpwait_relaxed_timeout(ptr, val, timeout_ns);	\
+	else if (arch_timer_evtstrm_available())			\
 		__cmpwait_relaxed(ptr, val);				\

Don't you want to make sure that we have the event stream available for __cmpwait_relaxed_timeout() too? Otherwise, a large timeout is going to cause problems.

Would that help though? If called from smp_cond_load_relaxed_timeout() then we would wake up and just call __cmpwait_relaxed_timeout() again.

Or did you mean other future users of __cmpwait_relaxed_timeout()?

Also I remember the event stream coming up earlier and there the intent
was to disable it if WFET was available: https://lore.kernel.org/lkml/[email protected]/.

--
ankur

maintainer
#16 lore raw

Re: [PATCH v8 04/12] arm64: support WFET in smp_cond_relaxed_timeout()

Quoted 51 lines · Ankur Arora
Quoted 37 lines · Will Deacon
Quoted 11 lines · Ankur Arora

Extend __cmpwait_relaxed() to __cmpwait_relaxed_timeout() which takes an additional timeout value in ns.

Lacking WFET, or with zero or negative value of timeout we fallback to WFE.

Cc Arnd Bergmann <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Quoted 4 lines
---
 arch/arm64/include/asm/barrier.h |  8 ++--
 arch/arm64/include/asm/cmpxchg.h | 72 ++++++++++++++++++++++----------
 2 files changed, 55 insertions(+), 25 deletions(-)

Sorry, just spotted something else on this...

Quoted 18 lines
arch/arm64/include/asm/barrier.h +2 -2
index 6190e178db51..fbd71cd4ef4e 100644
@@ -224,8 +224,8 @@ do {									\
 extern bool arch_timer_evtstrm_available(void);
 
 /*
- * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed()
- * for the ptr value to change.
+ * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed()/
+ * __cmpwait_relaxed_timeout() for the ptr value to change.
  *
  * Since this period is reasonably long, choose SMP_TIMEOUT_POLL_COUNT
  * to be 1, so smp_cond_load_{relaxed,acquire}_timeout() does a
@@ -234,7 +234,9 @@ extern bool arch_timer_evtstrm_available(void);
 #define SMP_TIMEOUT_POLL_COUNT	1
 
Quoted 10 lines

arch/arm64/include/asm/barrier.h extern bool arch_timer_evtstrm_available(void); · line 240

Quoted 6 lines
arch/arm64/include/asm/barrier.h +3 -1
 #define cpu_poll_relax(ptr, val, timeout_ns) do {			\
-	if (arch_timer_evtstrm_available())				\
+	if (alternative_has_cap_unlikely(ARM64_HAS_WFXT))		\
+		__cmpwait_relaxed_timeout(ptr, val, timeout_ns);	\
+	else if (arch_timer_evtstrm_available())			\
 		__cmpwait_relaxed(ptr, val);				\

Don't you want to make sure that we have the event stream available for __cmpwait_relaxed_timeout() too? Otherwise, a large timeout is going to cause problems.

Quoted 2 lines

Would that help though? If called from smp_cond_load_relaxed_timeout() then we would wake up and just call __cmpwait_relaxed_timeout() again.

Fair enough, I can see that. Is it worth capping the maximum timeout like we do for udelay()?

Will

#17 lore raw

Re: [PATCH v8 04/12] arm64: support WFET in smp_cond_relaxed_timeout()

Quoted 52 lines · Will Deacon
Quoted 51 lines · Ankur Arora
Quoted 47 lines · Will Deacon
Quoted 11 lines · Ankur Arora

Extend __cmpwait_relaxed() to __cmpwait_relaxed_timeout() which takes an additional timeout value in ns.

Lacking WFET, or with zero or negative value of timeout we fallback to WFE.

Cc Arnd Bergmann <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Quoted 4 lines
---
 arch/arm64/include/asm/barrier.h |  8 ++--
 arch/arm64/include/asm/cmpxchg.h | 72 ++++++++++++++++++++++----------
 2 files changed, 55 insertions(+), 25 deletions(-)

Sorry, just spotted something else on this...

Quoted 16 lines
arch/arm64/include/asm/barrier.h +2 -2
index 6190e178db51..fbd71cd4ef4e 100644
@@ -224,8 +224,8 @@ do {									\
 extern bool arch_timer_evtstrm_available(void);
 
 /*
- * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed()
- * for the ptr value to change.
+ * In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed()/
+ * __cmpwait_relaxed_timeout() for the ptr value to change.
  *
  * Since this period is reasonably long, choose SMP_TIMEOUT_POLL_COUNT
  * to be 1, so smp_cond_load_{relaxed,acquire}_timeout() does a

arch/arm64/include/asm/barrier.h extern bool arch_timer_evtstrm_available(void); · line 240

Quoted 8 lines
arch/arm64/include/asm/barrier.h +3 -1
@@ -234,7 +234,9 @@ extern bool arch_timer_evtstrm_available(void);
 #define SMP_TIMEOUT_POLL_COUNT	1
 
 #define cpu_poll_relax(ptr, val, timeout_ns) do {			\
-	if (arch_timer_evtstrm_available())				\
+	if (alternative_has_cap_unlikely(ARM64_HAS_WFXT))		\
+		__cmpwait_relaxed_timeout(ptr, val, timeout_ns);	\
+	else if (arch_timer_evtstrm_available())			\
 		__cmpwait_relaxed(ptr, val);				\

Don't you want to make sure that we have the event stream available for __cmpwait_relaxed_timeout() too? Otherwise, a large timeout is going to cause problems.

Quoted 5 lines
Quoted 2 lines

Would that help though? If called from smp_cond_load_relaxed_timeout() then we would wake up and just call __cmpwait_relaxed_timeout() again.

Fair enough, I can see that. Is it worth capping the maximum timeout like we do for udelay()?

The DELAY_CONST_MAX thing?

So, I'm not sure your concern is about the overall timeout or timeout per WFET iteration?

For the overall limit, at least rqspinlock has a pretty large timeout value (NSEC_PER_SEC/4).

However, it might be a good idea to attach a DELAY_CONST_MAX like limit when using this interface -- for architectures that do not have an optimized way of polling/define ARCH_HAS_CPU_RELAX.

(Currently only x86 defines ARCH_HAS_CPU_RELAX but I had a series which is meant to go after this that renames it to ARCH_HAS_ OPTIMIZED_POLL and selects it for x86 and arm64 [1].)

But that still might mean that we could have fairly long WFET iterations. Do you forsee a problem with that?

[1] https://lore.kernel.org/lkml/[email protected]/

Thanks

Signature · 1 line
ankur
#18 lore raw

PATCH v8 5/12 arm64: rqspinlock: Remove private copy of smp_cond_load_acquire_timewait()

In reply to Ankur Arora

In preparation for defining smp_cond_load_acquire_timeout(), remove the private copy. Lacking this, the rqspinlock code falls back to using smp_cond_load_acquire().

---
 arch/arm64/include/asm/rqspinlock.h | 85 -----------------------------
 1 file changed, 85 deletions(-)
arch/arm64/include/asm/rqspinlock.h +0 -85
index 9ea0a74e5892..a385603436e9 100644
@@ -3,91 +3,6 @@
 #define _ASM_RQSPINLOCK_H
 
 #include <asm/barrier.h>
-
-/*
- * Hardcode res_smp_cond_load_acquire implementations for arm64 to a custom
- * version based on [0]. In rqspinlock code, our conditional expression involves
- * checking the value _and_ additionally a timeout. However, on arm64, the
- * WFE-based implementation may never spin again if no stores occur to the
- * locked byte in the lock word. As such, we may be stuck forever if
- * event-stream based unblocking is not available on the platform for WFE spin
- * loops (arch_timer_evtstrm_available).
- *
- * Once support for smp_cond_load_acquire_timewait [0] lands, we can drop this
- * copy-paste.
- *
- * While we rely on the implementation to amortize the cost of sampling
- * cond_expr for us, it will not happen when event stream support is
- * unavailable, time_expr check is amortized. This is not the common case, and
- * it would be difficult to fit our logic in the time_expr_ns >= time_limit_ns
- * comparison, hence just let it be. In case of event-stream, the loop is woken
- * up at microsecond granularity.
- *
- * [0]: https://lore.kernel.org/lkml/[email protected]
- */
-
-#ifndef smp_cond_load_acquire_timewait
-
-#define smp_cond_time_check_count	200
-
-#define __smp_cond_load_relaxed_spinwait(ptr, cond_expr, time_expr_ns,	\
-					 time_limit_ns) ({		\
-	typeof(ptr) __PTR = (ptr);					\
-	__unqual_scalar_typeof(*ptr) VAL;				\
-	unsigned int __count = 0;					\
-	for (;;) {							\
-		VAL = READ_ONCE(*__PTR);				\
-		if (cond_expr)						\
-			break;						\
-		cpu_relax();						\
-		if (__count++ < smp_cond_time_check_count)		\
-			continue;					\
-		if ((time_expr_ns) >= (time_limit_ns))			\
-			break;						\
-		__count = 0;						\
-	}								\
-	(typeof(*ptr))VAL;						\
-})
-
-#define __smp_cond_load_acquire_timewait(ptr, cond_expr,		\
-					 time_expr_ns, time_limit_ns)	\
-({									\
-	typeof(ptr) __PTR = (ptr);					\
-	__unqual_scalar_typeof(*ptr) VAL;				\
-	for (;;) {							\
-		VAL = smp_load_acquire(__PTR);				\
-		if (cond_expr)						\
-			break;						\
-		__cmpwait_relaxed(__PTR, VAL);				\
-		if ((time_expr_ns) >= (time_limit_ns))			\
-			break;						\
-	}								\
-	(typeof(*ptr))VAL;						\
-})
-
-#define smp_cond_load_acquire_timewait(ptr, cond_expr,			\
-				      time_expr_ns, time_limit_ns)	\
-({									\
-	__unqual_scalar_typeof(*ptr) _val;				\
-	int __wfe = arch_timer_evtstrm_available();			\
-									\
-	if (likely(__wfe)) {						\
-		_val = __smp_cond_load_acquire_timewait(ptr, cond_expr,	\
-							time_expr_ns,	\
-							time_limit_ns);	\
-	} else {							\
-		_val = __smp_cond_load_relaxed_spinwait(ptr, cond_expr,	\
-							time_expr_ns,	\
-							time_limit_ns);	\
-		smp_acquire__after_ctrl_dep();				\
-	}								\
-	(typeof(*ptr))_val;						\
-})
-
-#endif
-
-#define res_smp_cond_load_acquire(v, c) smp_cond_load_acquire_timewait(v, c, 0, 1)
-
 #include <asm-generic/rqspinlock.h>
 
 #endif /* _ASM_RQSPINLOCK_H */
Signature · 1 line
2.31.1
#19 lore raw

Re: [PATCH v8 05/12] arm64: rqspinlock: Remove private copy of smp_cond_load_acquire_timewait()

Can lock acquisition hang indefinitely on platforms without event-stream support? The deleted implementation provided special handling for the arch_timer_evtstrm_available() == false case using…
Quoted 7 lines
arch/arm64/include/asm/rqspinlock.h +0 -0
index 9ea0a74e5892..a385603436e9 100644
@@ -3,91 +3,6 @@
 #define _ASM_RQSPINLOCK_H
 

arch/arm64/include/asm/rqspinlock.h line 14

Quoted 10 lines
arch/arm64/include/asm/rqspinlock.h +0 -9
 #include <asm/barrier.h>
-
-/*
- * Hardcode res_smp_cond_load_acquire implementations for arm64 to a custom
- * version based on [0]. In rqspinlock code, our conditional expression involves
- * checking the value _and_ additionally a timeout. However, on arm64, the
- * WFE-based implementation may never spin again if no stores occur to the
- * locked byte in the lock word. As such, we may be stuck forever if
- * event-stream based unblocking is not available on the platform for WFE spin
- * loops (arch_timer_evtstrm_available).
                                               ^^^^

Can lock acquisition hang indefinitely on platforms without event-stream support? The deleted implementation provided special handling for the arch_timer_evtstrm_available() == false case using __smp_cond_load_relaxed_spinwait(), which checked the timeout condition every 200 iterations via cpu_relax().

After this removal, resilient_queued_spin_lock_slowpath() at line 408 calls:

    res_smp_cond_load_acquire(&lock->locked,
                              !VAL || RES_CHECK_TIMEOUT(ts, ret, _Q_LOCKED_MASK));

This now falls back to the generic smp_cond_load_acquire() (defined in kernel/bpf/rqspinlock.c:307), which uses __cmpwait_relaxed(). On platforms without event-stream, __cmpwait_relaxed() becomes just cpu_relax() (a yield instruction), and the WFE instruction may never wake up if no stores occur to the lock word.

Even if WFE does wake up periodically, the RES_CHECK_TIMEOUT macro is defined with amortization (checking timeout every ~256 iterations when !(ts.spin++) wraps), which may be too sparse compared to the deleted implementation's explicit check every 200 iterations.

This means the timeout and deadlock detection logic may not function correctly, potentially causing lock acquisition to hang instead of returning -ETIMEDOUT or -EDEADLK after the intended 250ms timeout (RES_DEF_TIMEOUT).

Note: The next commit in the series (f150b57ec7c8 "bpf/rqspinlock: Use smp_cond_load_acquire_timeout()") switches to proper timeout-aware primitives, which resolves this issue.

Quoted 3 lines
arch/arm64/include/asm/rqspinlock.h +0 -3
- *
- * Once support for smp_cond_load_acquire_timewait [0] lands, we can drop this
- * copy-paste.

[ ... ]

arch/arm64/include/asm/rqspinlock.h line 8

Quoted 5 lines
arch/arm64/include/asm/rqspinlock.h +0 -2
-#define res_smp_cond_load_acquire(v, c) smp_cond_load_acquire_timewait(v, c, 0, 1)
-
 #include <asm-generic/rqspinlock.h>
 
 #endif /* _ASM_RQSPINLOCK_H */

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20221060323

#20 lore raw

PATCH v8 6/12 asm-generic: barrier: Add smp_cond_load_acquire_timeout()

In reply to Ankur Arora

Add the acquire variant of smp_cond_load_relaxed_timeout(). This reuses the relaxed variant, with additional LOAD->LOAD ordering.

---
 include/asm-generic/barrier.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
include/asm-generic/barrier.h +26 -0
index e25592f9fcbf..d05d34bece0d 100644
@@ -331,6 +331,32 @@ __done:								\
 })
 #endif
 
+/**
+ * smp_cond_load_acquire_timeout() - (Spin) wait for cond with ACQUIRE ordering
+ * until a timeout expires.
+ * @ptr: pointer to the variable to wait on
+ * @cond: boolean expression to wait for
+ * @time_expr_ns: monotonic expression that evaluates to time in ns or,
+ *  on failure, returns a negative value.
+ * @timeout_ns: timeout value in ns
+ * (Both of the above are assumed to be compatible with s64.)
+ *
+ * Equivalent to using smp_cond_load_acquire() on the condition variable with
+ * a timeout.
+ */
+#ifndef smp_cond_load_acquire_timeout
+#define smp_cond_load_acquire_timeout(ptr, cond_expr,			\
+				      time_expr_ns, timeout_ns)		\
+({									\
+	__unqual_scalar_typeof(*ptr) _val;				\
+	_val = smp_cond_load_relaxed_timeout(ptr, cond_expr,		\
+					     time_expr_ns,		\
+					     timeout_ns);		\
+	smp_acquire__after_ctrl_dep();					\
+	(typeof(*ptr))_val;						\
+})
+#endif
+
 /*
  * pmem_wmb() ensures that all stores for which the modification
  * are written to persistent storage by preceding instructions have
Signature · 1 line
2.31.1
#21 lore raw

PATCH v8 7/12 atomic: Add atomic_cond_read_*_timeout()

In reply to Ankur Arora

Add atomic load wrappers, atomic_cond_read_*_timeout() and atomic64_cond_read_*_timeout() for the cond-load timeout interfaces.

Cc Will Deacon <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/atomic.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/atomic.h +10 -0
index 8dd57c3a99e9..5bcb86e07784 100644
@@ -31,6 +31,16 @@
 #define atomic64_cond_read_acquire(v, c) smp_cond_load_acquire(&(v)->counter, (c))
 #define atomic64_cond_read_relaxed(v, c) smp_cond_load_relaxed(&(v)->counter, (c))
 
+#define atomic_cond_read_acquire_timeout(v, c, e, t) \
+	smp_cond_load_acquire_timeout(&(v)->counter, (c), (e), (t))
+#define atomic_cond_read_relaxed_timeout(v, c, e, t) \
+	smp_cond_load_relaxed_timeout(&(v)->counter, (c), (e), (t))
+
+#define atomic64_cond_read_acquire_timeout(v, c, e, t) \
+	smp_cond_load_acquire_timeout(&(v)->counter, (c), (e), (t))
+#define atomic64_cond_read_relaxed_timeout(v, c, e, t) \
+	smp_cond_load_relaxed_timeout(&(v)->counter, (c), (e), (t))
+
 /*
  * The idea here is to build acquire/release variants by adding explicit
  * barriers on top of the relaxed variant. In the case where the relaxed
Signature · 1 line
2.31.1
maintainer
#22 lore raw

Re: [PATCH v8 07/12] atomic: Add atomic_cond_read_*_timeout()

Quoted 18 lines · Ankur Arora

Add atomic load wrappers, atomic_cond_read_*_timeout() and atomic64_cond_read_*_timeout() for the cond-load timeout interfaces.

Cc Will Deacon <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/atomic.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/atomic.h +0 -0
index 8dd57c3a99e9..5bcb86e07784 100644
@@ -31,6 +31,16 @@
 #define atomic64_cond_read_acquire(v, c) smp_cond_load_acquire(&(v)->counter, (c))
 #define atomic64_cond_read_relaxed(v, c) smp_cond_load_relaxed(&(v)->counter, (c))

include/linux/atomic.h line 42

Quoted 10 lines
include/linux/atomic.h +9 -0
 
+#define atomic_cond_read_acquire_timeout(v, c, e, t) \
+	smp_cond_load_acquire_timeout(&(v)->counter, (c), (e), (t))
+#define atomic_cond_read_relaxed_timeout(v, c, e, t) \
+	smp_cond_load_relaxed_timeout(&(v)->counter, (c), (e), (t))
+
+#define atomic64_cond_read_acquire_timeout(v, c, e, t) \
+	smp_cond_load_acquire_timeout(&(v)->counter, (c), (e), (t))
+#define atomic64_cond_read_relaxed_timeout(v, c, e, t) \
+	smp_cond_load_relaxed_timeout(&(v)->counter, (c), (e), (t))

Please update the atomic_t documentation to explain what these do.

Will

#23 lore raw

Re: [PATCH v8 07/12] atomic: Add atomic_cond_read_*_timeout()

Quoted 20 lines · Will Deacon
Quoted 19 lines · Ankur Arora

Add atomic load wrappers, atomic_cond_read_*_timeout() and atomic64_cond_read_*_timeout() for the cond-load timeout interfaces.

Cc Will Deacon <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/atomic.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/atomic.h +0 -0
index 8dd57c3a99e9..5bcb86e07784 100644
@@ -31,6 +31,16 @@
 #define atomic64_cond_read_acquire(v, c) smp_cond_load_acquire(&(v)->counter, (c))
 #define atomic64_cond_read_relaxed(v, c) smp_cond_load_relaxed(&(v)->counter, (c))
 
Quoted 11 lines

include/linux/atomic.h line 42

Quoted 9 lines
include/linux/atomic.h +9 -0
+#define atomic_cond_read_acquire_timeout(v, c, e, t) \
+	smp_cond_load_acquire_timeout(&(v)->counter, (c), (e), (t))
+#define atomic_cond_read_relaxed_timeout(v, c, e, t) \
+	smp_cond_load_relaxed_timeout(&(v)->counter, (c), (e), (t))
+
+#define atomic64_cond_read_acquire_timeout(v, c, e, t) \
+	smp_cond_load_acquire_timeout(&(v)->counter, (c), (e), (t))
+#define atomic64_cond_read_relaxed_timeout(v, c, e, t) \
+	smp_cond_load_relaxed_timeout(&(v)->counter, (c), (e), (t))

Please update the atomic_t documentation to explain what these do.

Missed that. Will update.

--
ankur

#24 lore raw

PATCH v8 8/12 locking/atomic: scripts: build atomic_long_cond_read_*_timeout()

In reply to Ankur Arora

Add the atomic long wrappers for the cond-load timeout interfaces.

Cc Will Deacon <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/atomic/atomic-long.h | 18 +++++++++++-------
 scripts/atomic/gen-atomic-long.sh  | 16 ++++++++++------
 2 files changed, 21 insertions(+), 13 deletions(-)
include/linux/atomic/atomic-long.h +11 -7
index f86b29d90877..e6da0189cbe6 100644
@@ -11,14 +11,18 @@
 
 #ifdef CONFIG_64BIT
 typedef atomic64_t atomic_long_t;
-#define ATOMIC_LONG_INIT(i)		ATOMIC64_INIT(i)
-#define atomic_long_cond_read_acquire	atomic64_cond_read_acquire
-#define atomic_long_cond_read_relaxed	atomic64_cond_read_relaxed
+#define ATOMIC_LONG_INIT(i)			ATOMIC64_INIT(i)
+#define atomic_long_cond_read_acquire		atomic64_cond_read_acquire
+#define atomic_long_cond_read_relaxed		atomic64_cond_read_relaxed
+#define atomic_long_cond_read_acquire_timeout	atomic64_cond_read_acquire_timeout
+#define atomic_long_cond_read_relaxed_timeout	atomic64_cond_read_relaxed_timeout
 #else
 typedef atomic_t atomic_long_t;
-#define ATOMIC_LONG_INIT(i)		ATOMIC_INIT(i)
-#define atomic_long_cond_read_acquire	atomic_cond_read_acquire
-#define atomic_long_cond_read_relaxed	atomic_cond_read_relaxed
+#define ATOMIC_LONG_INIT(i)			ATOMIC_INIT(i)
+#define atomic_long_cond_read_acquire		atomic_cond_read_acquire
+#define atomic_long_cond_read_relaxed		atomic_cond_read_relaxed
+#define atomic_long_cond_read_acquire_timeout	atomic_cond_read_acquire_timeout
+#define atomic_long_cond_read_relaxed_timeout	atomic_cond_read_relaxed_timeout
 #endif
 
 /**
@@ -1809,4 +1813,4 @@ raw_atomic_long_dec_if_positive(atomic_long_t *v)
 }
 
 #endif /* _LINUX_ATOMIC_LONG_H */
-// eadf183c3600b8b92b91839dd3be6bcc560c752d
+// 475f45a880d1625faa5116dcfd6e943e4dbe1cd5
scripts/atomic/gen-atomic-long.sh +10 -6
index 9826be3ba986..874643dc74bd 100755
@@ -79,14 +79,18 @@ cat << EOF
 
 #ifdef CONFIG_64BIT
 typedef atomic64_t atomic_long_t;
-#define ATOMIC_LONG_INIT(i)		ATOMIC64_INIT(i)
-#define atomic_long_cond_read_acquire	atomic64_cond_read_acquire
-#define atomic_long_cond_read_relaxed	atomic64_cond_read_relaxed
+#define ATOMIC_LONG_INIT(i)			ATOMIC64_INIT(i)
+#define atomic_long_cond_read_acquire		atomic64_cond_read_acquire
+#define atomic_long_cond_read_relaxed		atomic64_cond_read_relaxed
+#define atomic_long_cond_read_acquire_timeout	atomic64_cond_read_acquire_timeout
+#define atomic_long_cond_read_relaxed_timeout	atomic64_cond_read_relaxed_timeout
 #else
 typedef atomic_t atomic_long_t;
-#define ATOMIC_LONG_INIT(i)		ATOMIC_INIT(i)
-#define atomic_long_cond_read_acquire	atomic_cond_read_acquire
-#define atomic_long_cond_read_relaxed	atomic_cond_read_relaxed
+#define ATOMIC_LONG_INIT(i)			ATOMIC_INIT(i)
+#define atomic_long_cond_read_acquire		atomic_cond_read_acquire
+#define atomic_long_cond_read_relaxed		atomic_cond_read_relaxed
+#define atomic_long_cond_read_acquire_timeout	atomic_cond_read_acquire_timeout
+#define atomic_long_cond_read_relaxed_timeout	atomic_cond_read_relaxed_timeout
 #endif
 
 EOF
Signature · 1 line
2.31.1
#25 lore raw

PATCH v8 9/12 bpf/rqspinlock: switch check_timeout() to a clock interface

In reply to Ankur Arora

check_timeout() gets the current time value and depending on how much time has passed, checks for deadlock or times out, returning 0 or -errno on deadlock or timeout.

Switch this out to a clock style interface, where it functions as a clock in the "lock-domain", returning the current time until a deadlock or timeout occurs. Once a deadlock or timeout has occurred, it stops functioning as a clock and returns error.

Cc Kumar Kartikeya Dwivedi <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 kernel/bpf/rqspinlock.c | 41 +++++++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 14 deletions(-)
kernel/bpf/rqspinlock.c +27 -14
index f7d0c8d4644e..ac9b3572e42f 100644
@@ -196,8 +196,12 @@ static noinline int check_deadlock_ABBA(rqspinlock_t *lock, u32 mask)
 	return 0;
 }
 
-static noinline int check_timeout(rqspinlock_t *lock, u32 mask,
-				  struct rqspinlock_timeout *ts)
+/*
+ * Returns current monotonic time in ns on success or, negative errno
+ * value on failure due to timeout expiration or detection of deadlock.
+ */
+static noinline s64 clock_deadlock(rqspinlock_t *lock, u32 mask,
+				   struct rqspinlock_timeout *ts)
 {
 	u64 prev = ts->cur;
 	u64 time;
@@ -207,7 +211,7 @@ static noinline int check_timeout(rqspinlock_t *lock, u32 mask,
 			return -EDEADLK;
 		ts->cur = ktime_get_mono_fast_ns();
 		ts->timeout_end = ts->cur + ts->duration;
-		return 0;
+		return (s64)ts->cur;
 	}
 
 	time = ktime_get_mono_fast_ns();
@@ -219,11 +223,15 @@ static noinline int check_timeout(rqspinlock_t *lock, u32 mask,
 	 * checks.
 	 */
 	if (prev + NSEC_PER_MSEC < time) {
+		int ret;
 		ts->cur = time;
-		return check_deadlock_ABBA(lock, mask);
+		ret = check_deadlock_ABBA(lock, mask);
+		if (ret)
+			return ret;
+
 	}
 
-	return 0;
+	return (s64)time;
 }
 
 /*
@@ -234,12 +242,12 @@ static noinline int check_timeout(rqspinlock_t *lock, u32 mask,
 #define RES_CHECK_TIMEOUT(ts, ret, mask)                              \
 	({                                                            \
 		if (!(ts).spin++)                                     \
-			(ret) = check_timeout((lock), (mask), &(ts)); \
+			(ret) = clock_deadlock((lock), (mask), &(ts));\
 		(ret);                                                \
 	})
 #else
 #define RES_CHECK_TIMEOUT(ts, ret, mask)			      \
-	({ (ret) = check_timeout((lock), (mask), &(ts)); })
+	({ (ret) = clock_deadlock((lock), (mask), &(ts)); })
 #endif
 
 /*
@@ -261,7 +269,8 @@ static noinline int check_timeout(rqspinlock_t *lock, u32 mask,
 int __lockfunc resilient_tas_spin_lock(rqspinlock_t *lock)
 {
 	struct rqspinlock_timeout ts;
-	int val, ret = 0;
+	s64 ret = 0;
+	int val;
 
 	RES_INIT_TIMEOUT(ts);
 	/*
@@ -280,7 +289,7 @@ int __lockfunc resilient_tas_spin_lock(rqspinlock_t *lock)
 	val = atomic_read(&lock->val);
 
 	if (val || !atomic_try_cmpxchg(&lock->val, &val, 1)) {
-		if (RES_CHECK_TIMEOUT(ts, ret, ~0u))
+		if (RES_CHECK_TIMEOUT(ts, ret, ~0u) < 0)
 			goto out;
 		cpu_relax();
 		goto retry;
@@ -339,6 +348,7 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
 {
 	struct mcs_spinlock *prev, *next, *node;
 	struct rqspinlock_timeout ts;
+	s64 timeout_err = 0;
 	int idx, ret = 0;
 	u32 old, tail;
 
@@ -405,10 +415,10 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
 	 */
 	if (val & _Q_LOCKED_MASK) {
 		RES_RESET_TIMEOUT(ts, RES_DEF_TIMEOUT);
-		res_smp_cond_load_acquire(&lock->locked, !VAL || RES_CHECK_TIMEOUT(ts, ret, _Q_LOCKED_MASK));
+		res_smp_cond_load_acquire(&lock->locked, !VAL || RES_CHECK_TIMEOUT(ts, timeout_err, _Q_LOCKED_MASK) < 0);
 	}
 
-	if (ret) {
+	if (timeout_err < 0) {
 		/*
 		 * We waited for the locked bit to go back to 0, as the pending
 		 * waiter, but timed out. We need to clear the pending bit since
@@ -420,6 +430,7 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
 		 */
 		clear_pending(lock);
 		lockevent_inc(rqspinlock_lock_timeout);
+		ret = timeout_err;
 		goto err_release_entry;
 	}
 
@@ -567,18 +578,19 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
 	 */
 	RES_RESET_TIMEOUT(ts, RES_DEF_TIMEOUT * 2);
 	val = res_atomic_cond_read_acquire(&lock->val, !(VAL & _Q_LOCKED_PENDING_MASK) ||
-					   RES_CHECK_TIMEOUT(ts, ret, _Q_LOCKED_PENDING_MASK));
+					   RES_CHECK_TIMEOUT(ts, timeout_err, _Q_LOCKED_PENDING_MASK) < 0);
 
 	/* Disable queue destruction when we detect deadlocks. */
-	if (ret == -EDEADLK) {
+	if (timeout_err == -EDEADLK) {
 		if (!next)
 			next = smp_cond_load_relaxed(&node->next, (VAL));
 		arch_mcs_spin_unlock_contended(&next->locked);
+		ret = timeout_err;
 		goto err_release_node;
 	}
 
 waitq_timeout:
-	if (ret) {
+	if (timeout_err < 0) {
 		/*
 		 * If the tail is still pointing to us, then we are the final waiter,
 		 * and are responsible for resetting the tail back to 0. Otherwise, if
@@ -608,6 +620,7 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
 			WRITE_ONCE(next->locked, RES_TIMEOUT_VAL);
 		}
 		lockevent_inc(rqspinlock_lock_timeout);
+		ret = timeout_err;
 		goto err_release_node;
 	}
 
Signature · 1 line
2.31.1
#26 lore raw

PATCH v8 10/12 bpf/rqspinlock: Use smp_cond_load_acquire_timeout()

In reply to Ankur Arora

Switch out the conditional load interfaces used by rqspinlock to smp_cond_read_acquire_timeout() and its wrapper, atomic_cond_read_acquire_timeout().

Both these handle the timeout and amortize as needed, so use clock_deadlock() directly instead of going through RES_CHECK_TIMEOUT().

For correctness, however, we need to ensure that the timeout case in smp_cond_read_acquire_timeout() always agrees with that in clock_deadlock(), which returns with -ETIMEDOUT.

For the most part, this is fine because smp_cond_load_acquire_timeout() does not have an independent clock and does not do double reads from clock_deadlock() which could cause its internal state to go out of sync from that of clock_deadlock().

There is, however, an edge case where clock_deadlock() checks for:

        if (time > ts->timeout_end)
                return -ETIMEDOUT;

while smp_cond_load_acquire_timeout() checks for:

        __time_now = (time_expr_ns);
        if (__time_now <= 0 || __time_now >= __time_end) {
                VAL = READ_ONCE(*__PTR);
                break;
        }

This runs into a problem when (__time_now == __time_end) since clock_deadlock() does not treat it as a timeout condition but the second clause in the conditional above does.
So, add an equality check in clock_deadlock().

Finally, redefine SMP_TIMEOUT_POLL_COUNT to be 16k to be similar to the spin-count used in RES_CHECK_TIMEOUT(). We only do this for non-arm64 as that uses a waiting implementation.

Cc Kumar Kartikeya Dwivedi <[email protected]>
Cc Alexei Starovoitov <[email protected]>

---
Notes:
- change the check in clock_deadlock()

 kernel/bpf/rqspinlock.c | 37 ++++++++++++++++++++-----------------
 1 file changed, 20 insertions(+), 17 deletions(-)
kernel/bpf/rqspinlock.c +20 -17
index ac9b3572e42f..2a361c4c7393 100644
@@ -215,7 +215,7 @@ static noinline s64 clock_deadlock(rqspinlock_t *lock, u32 mask,
 	}
 
 	time = ktime_get_mono_fast_ns();
-	if (time > ts->timeout_end)
+	if (time >= ts->timeout_end)
 		return -ETIMEDOUT;
 
 	/*
@@ -235,20 +235,14 @@ static noinline s64 clock_deadlock(rqspinlock_t *lock, u32 mask,
 }
 
 /*
- * Do not amortize with spins when res_smp_cond_load_acquire is defined,
- * as the macro does internal amortization for us.
+ * Amortize timeout check for busy-wait loops.
  */
-#ifndef res_smp_cond_load_acquire
 #define RES_CHECK_TIMEOUT(ts, ret, mask)                              \
 	({                                                            \
 		if (!(ts).spin++)                                     \
 			(ret) = clock_deadlock((lock), (mask), &(ts));\
 		(ret);                                                \
 	})
-#else
-#define RES_CHECK_TIMEOUT(ts, ret, mask)			      \
-	({ (ret) = clock_deadlock((lock), (mask), &(ts)); })
-#endif
 
 /*
  * Initialize the 'spin' member.
@@ -262,6 +256,18 @@ static noinline s64 clock_deadlock(rqspinlock_t *lock, u32 mask,
  */
 #define RES_RESET_TIMEOUT(ts, _duration) ({ (ts).timeout_end = 0; (ts).duration = _duration; })
 
+/*
+ * Limit how often we invoke clock_deadlock() while spin-waiting in
+ * smp_cond_load_acquire_timeout() or atomic_cond_read_acquire_timeout().
+ *
+ * (ARM64 generally uses a waited implementation so we use the default
+ * value there.)
+ */
+#ifndef CONFIG_ARM64
+#undef SMP_TIMEOUT_POLL_COUNT
+#define SMP_TIMEOUT_POLL_COUNT	(16*1024)
+#endif
+
 /*
  * Provide a test-and-set fallback for cases when queued spin lock support is
  * absent from the architecture.
@@ -312,12 +318,6 @@ EXPORT_SYMBOL_GPL(resilient_tas_spin_lock);
  */
 static DEFINE_PER_CPU_ALIGNED(struct qnode, rqnodes[_Q_MAX_NODES]);
 
-#ifndef res_smp_cond_load_acquire
-#define res_smp_cond_load_acquire(v, c) smp_cond_load_acquire(v, c)
-#endif
-
-#define res_atomic_cond_read_acquire(v, c) res_smp_cond_load_acquire(&(v)->counter, (c))
-
 /**
  * resilient_queued_spin_lock_slowpath - acquire the queued spinlock
  * @lock: Pointer to queued spinlock structure
@@ -415,7 +415,9 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
 	 */
 	if (val & _Q_LOCKED_MASK) {
 		RES_RESET_TIMEOUT(ts, RES_DEF_TIMEOUT);
-		res_smp_cond_load_acquire(&lock->locked, !VAL || RES_CHECK_TIMEOUT(ts, timeout_err, _Q_LOCKED_MASK) < 0);
+		smp_cond_load_acquire_timeout(&lock->locked, !VAL,
+					      (timeout_err = clock_deadlock(lock, _Q_LOCKED_MASK, &ts)),
+					      ts.duration);
 	}
 
 	if (timeout_err < 0) {
@@ -577,8 +579,9 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
 	 * us.
 	 */
 	RES_RESET_TIMEOUT(ts, RES_DEF_TIMEOUT * 2);
-	val = res_atomic_cond_read_acquire(&lock->val, !(VAL & _Q_LOCKED_PENDING_MASK) ||
-					   RES_CHECK_TIMEOUT(ts, timeout_err, _Q_LOCKED_PENDING_MASK) < 0);
+	val = atomic_cond_read_acquire_timeout(&lock->val, !(VAL & _Q_LOCKED_PENDING_MASK),
+					       (timeout_err = clock_deadlock(lock, _Q_LOCKED_PENDING_MASK, &ts)),
+					       ts.duration);
 
 	/* Disable queue destruction when we detect deadlocks. */
 	if (timeout_err == -EDEADLK) {
Signature · 1 line
2.31.1
#27 lore raw

Re: [PATCH v8 10/12] bpf/rqspinlock: Use smp_cond_load_acquire_timeout()

kernel/bpf/rqspinlock.c int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val) · line 420

Quoted 12 lines · Ankur Arora

/**
* resilient_queued_spin_lock_slowpath - acquire the queued spinlock
* @lock: Pointer to queued spinlock structure

kernel/bpf/rqspinlock.c +3 -1
@@ -415,7 +415,9 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
         */
        if (val & _Q_LOCKED_MASK) {
                RES_RESET_TIMEOUT(ts, RES_DEF_TIMEOUT);
-               res_smp_cond_load_acquire(&lock->locked, !VAL || RES_CHECK_TIMEOUT(ts, timeout_err, _Q_LOCKED_MASK) < 0);
+               smp_cond_load_acquire_timeout(&lock->locked, !VAL,
+                                             (timeout_err = clock_deadlock(lock, _Q_LOCKED_MASK, &ts)),
+                                             ts.duration);

I'm pretty sure we already discussed this and pointed out that this approach is not acceptable.
We cannot call ktime_get_mono_fast_ns() first.
That's why RES_CHECK_TIMEOUT() exists and it does
if (!(ts).spin++)
before doing the first check_timeout() that will do ktime_get_mono_fast_ns(). Above is a performance critical lock acquisition path where pending is spinning on the lock word waiting for the owner to release the lock.
Adding unconditional ktime_get_mono_fast_ns() will destroy performance for quick critical section.

#28 lore raw

Re: [PATCH v8 10/12] bpf/rqspinlock: Use smp_cond_load_acquire_timeout()

Quoted 16 lines · Alexei Starovoitov

kernel/bpf/rqspinlock.c int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val) · line 420

Quoted 12 lines · Ankur Arora

/**
* resilient_queued_spin_lock_slowpath - acquire the queued spinlock
* @lock: Pointer to queued spinlock structure

kernel/bpf/rqspinlock.c +3 -1
@@ -415,7 +415,9 @@ int __lockfunc resilient_queued_spin_lock_slowpath(rqspinlock_t *lock, u32 val)
         */
        if (val & _Q_LOCKED_MASK) {
                RES_RESET_TIMEOUT(ts, RES_DEF_TIMEOUT);
-               res_smp_cond_load_acquire(&lock->locked, !VAL || RES_CHECK_TIMEOUT(ts, timeout_err, _Q_LOCKED_MASK) < 0);
+               smp_cond_load_acquire_timeout(&lock->locked, !VAL,
+                                             (timeout_err = clock_deadlock(lock, _Q_LOCKED_MASK, &ts)),
+                                             ts.duration);

I'm pretty sure we already discussed this and pointed out that this approach is not acceptable.

Where? I don't see any mail on this at all.

In any case your technical point below is quite reasonable. It's better to lead with that instead of peremptorily declaring what you find acceptable and what not.

Quoted 9 lines

We cannot call ktime_get_mono_fast_ns() first.
That's why RES_CHECK_TIMEOUT() exists and it does
if (!(ts).spin++)
before doing the first check_timeout() that will do ktime_get_mono_fast_ns(). Above is a performance critical lock acquisition path where pending is spinning on the lock word waiting for the owner to release the lock.
Adding unconditional ktime_get_mono_fast_ns() will destroy performance for quick critical section.

Yes that makes sense.

This is not ideal, but how about something like this:

  #define smp_cond_load_relaxed_timeout(ptr, cond_expr,                 \
                                      time_expr_ns, timeout_ns)         \
  ({                                                                    \
        typeof(ptr) __PTR = (ptr);                                      \
        __unqual_scalar_typeof(*ptr) VAL;                               \
        u32 __n = 0, __spin = SMP_TIMEOUT_POLL_COUNT;                   \
        s64 __timeout = (s64)timeout_ns;                                \
        s64 __time_now, __time_end = 0;                                 \
                                                                        \
        for (;;) {                                                      \
                VAL = READ_ONCE(*__PTR);                                \
                if (cond_expr)                                          \
                        break;                                          \
                cpu_poll_relax(__PTR, VAL, __timeout);                  \
                if (++__n < __spin)                                     \
                        continue;                                       \
                __time_now = (s64)(time_expr_ns);                       \
                if (unlikely(__time_end == 0))                          \
                        __time_end = __time_now + __timeout;            \
                __timeout = __time_end - __time_now;                    \
                if (__time_now <= 0 || __timeout <= 0) {                \
                        VAL = READ_ONCE(*__PTR);                        \
                        break;                                          \
                }                                                       \
                __n = 0;                                                \
        }                                                               \
                                                                        \
        (typeof(*ptr))VAL;                                              \

})

The problem with this version is that there's no way to know how much time has passed in the first cpu_poll_relax()). So for the waiting case this has a builtin overshoot of up to __timeout for the WFET case. And ~100us for WFE and ~2us for x86 cpu_relax.

Of course we could specify a small __timeout value for the first iteration which would help WFET.

Anyway let me take another look at this tomorrow.

But, that is more complexity.

Opinions?

--
ankur

#29 lore raw

PATCH v8 11/12 sched: add need-resched timed wait interface

In reply to Ankur Arora

Add tif_bitset_relaxed_wait() (and tif_need_resched_relaxed_wait() which wraps it) which takes the thread_info bit and timeout duration as parameters and waits until the bit is set or for the expiration of the timeout.

The wait is implemented via smp_cond_load_relaxed_timeout().

smp_cond_load_acquire_timeout() essentially provides the pattern used in poll_idle() where we spin in a loop waiting for the flag to change until a timeout occurs.

tif_need_resched_relaxed_wait() allows us to abstract out the internals of waiting, scheduler specific details etc.

Placed in linux/sched/idle.h instead of linux/thread_info.h to work around recursive include hell.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Daniel Lezcano <[email protected]>
---
 include/linux/sched/idle.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
include/linux/sched/idle.h +29 -0
index 8465ff1f20d1..6780ad760abb 100644
@@ -3,6 +3,7 @@
 #define _LINUX_SCHED_IDLE_H
 
 #include <linux/sched.h>
+#include <linux/sched/clock.h>
 
 enum cpu_idle_type {
 	__CPU_NOT_IDLE = 0,
@@ -113,4 +114,32 @@ static __always_inline void current_clr_polling(void)
 }
 #endif
 
+/*
+ * Caller needs to make sure that the thread context cannot be preempted
+ * or migrated, so current_thread_info() cannot change from under us.
+ *
+ * This also allows us to safely stay in the local_clock domain.
+ */
+static inline bool tif_bitset_relaxed_wait(int bit, s64 timeout_ns)
+{
+	unsigned int flags;
+
+	flags = smp_cond_load_relaxed_timeout(&current_thread_info()->flags,
+					      (VAL & bit),
+					      (s64)local_clock_noinstr(),
+					      timeout_ns);
+	return flags & bit;
+}
+
+/**
+ * tif_need_resched_relaxed_wait() - Wait for need-resched being set with
+ * no ordering guarantees until a timeout expires.
+ *
+ * @timeout_ns: timeout value.
+ */
+static inline bool tif_need_resched_relaxed_wait(s64 timeout_ns)
+{
+	return tif_bitset_relaxed_wait(TIF_NEED_RESCHED, timeout_ns);
+}
+
 #endif /* _LINUX_SCHED_IDLE_H */
Signature · 1 line
2.31.1
#30 lore raw

PATCH v8 12/12 cpuidle/poll_state: Wait for need-resched via tif_need_resched_relaxed_wait()

In reply to Ankur Arora

The inner loop in poll_idle() polls over the thread_info flags, waiting to see if the thread has TIF_NEED_RESCHED 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 in each iteration, the time check is rate-limited. In addition, each loop iteration executes cpu_relax() which on certain platforms provides a hint to the pipeline that the loop busy-waits, allowing the processor to reduce power consumption.

Switch over to tif_need_resched_relaxed_wait() instead, since that provides exactly that.

However, given that when running in idle we want to minimize our power consumption, continue to depend on CONFIG_ARCH_HAS_CPU_RELAX as that serves as an indicator that the platform supports an optimized version of tif_need_resched_relaxed_wait() (via smp_cond_load_acquire_timeout()).

---

Notes:
- use tif_need_resched_relaxed_wait() instead of smp_cond_load_relaxed_timeout()

 drivers/cpuidle/poll_state.c | 27 +++++----------------------
 1 file changed, 5 insertions(+), 22 deletions(-)
drivers/cpuidle/poll_state.c +5 -22
index c7524e4c522a..20136b3a08c2 100644
@@ -6,41 +6,24 @@
 #include <linux/cpuidle.h>
 #include <linux/export.h>
 #include <linux/irqflags.h>
-#include <linux/sched.h>
-#include <linux/sched/clock.h>
 #include <linux/sched/idle.h>
 #include <linux/sprintf.h>
 #include <linux/types.h>
 
-#define POLL_IDLE_RELAX_COUNT	200
-
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
 			       struct cpuidle_driver *drv, int index)
 {
-	u64 time_start;
-
-	time_start = local_clock_noinstr();
-
 	dev->poll_time_limit = false;
 
 	raw_local_irq_enable();
 	if (!current_set_polling_and_test()) {
-		unsigned int loop_count = 0;
-		u64 limit;
+		s64 limit;
+		bool nr_set;
 
-		limit = cpuidle_poll_time(drv, dev);
+		limit = (s64)cpuidle_poll_time(drv, dev);
 
-		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;
-			}
-		}
+		nr_set = tif_need_resched_relaxed_wait(limit);
+		dev->poll_time_limit = !nr_set;
 	}
 	raw_local_irq_disable();
 
Signature · 1 line
2.31.1
#31 lore raw

Re: [PATCH v8 12/12] cpuidle/poll_state: Wait for need-resched via tif_need_resched_relaxed_wait()

Quoted 56 lines · Ankur Arora

The inner loop in poll_idle() polls over the thread_info flags, waiting to see if the thread has TIF_NEED_RESCHED 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 in each iteration, the time check is rate-limited. In addition, each loop iteration executes cpu_relax() which on certain platforms provides a hint to the pipeline that the loop busy-waits, allowing the processor to reduce power consumption.

Switch over to tif_need_resched_relaxed_wait() instead, since that provides exactly that.

However, given that when running in idle we want to minimize our power consumption, continue to depend on CONFIG_ARCH_HAS_CPU_RELAX as that serves as an indicator that the platform supports an optimized version of tif_need_resched_relaxed_wait() (via smp_cond_load_acquire_timeout()).

---

Notes:
- use tif_need_resched_relaxed_wait() instead of smp_cond_load_relaxed_timeout()

 drivers/cpuidle/poll_state.c | 27 +++++----------------------
 1 file changed, 5 insertions(+), 22 deletions(-)
drivers/cpuidle/poll_state.c +0 -6
index c7524e4c522a..20136b3a08c2 100644
@@ -6,41 +6,24 @@
 #include <linux/cpuidle.h>
 #include <linux/export.h>
 #include <linux/irqflags.h>
-#include <linux/sched.h>
-#include <linux/sched/clock.h>
 #include <linux/sched/idle.h>
 #include <linux/sprintf.h>
 #include <linux/types.h>
 
-#define POLL_IDLE_RELAX_COUNT  200
-
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
                               struct cpuidle_driver *drv, int index)
 {
-       u64 time_start;
-

drivers/cpuidle/poll_state.c line 21

Quoted 10 lines
drivers/cpuidle/poll_state.c +2 -4
-       time_start = local_clock_noinstr();
-
        dev->poll_time_limit = false;
 
        raw_local_irq_enable();
        if (!current_set_polling_and_test()) {
-               unsigned int loop_count = 0;
-               u64 limit;
+               s64 limit;
+               bool nr_set;

It doesn't look like the nr_set variable is really needed.

drivers/cpuidle/poll_state.c line 23

Quoted 3 lines
drivers/cpuidle/poll_state.c +1 -1
-               limit = cpuidle_poll_time(drv, dev);
+               limit = (s64)cpuidle_poll_time(drv, dev);

Is the explicit cast needed to suppress a warning? If not, I'd drop it.

Quoted 4 lines
drivers/cpuidle/poll_state.c +0 -3
-               while (!need_resched()) {
-                       cpu_relax();
-                       if (loop_count++ < POLL_IDLE_RELAX_COUNT)

drivers/cpuidle/poll_state.c line 26

Quoted 10 lines
drivers/cpuidle/poll_state.c +2 -8
-                               continue;
-
-                       loop_count = 0;
-                       if (local_clock_noinstr() - time_start > limit) {
-                               dev->poll_time_limit = true;
-                               break;
-                       }
-               }
+               nr_set = tif_need_resched_relaxed_wait(limit);
+               dev->poll_time_limit = !nr_set;

This can be

dev->poll_time_limit = !tif_need_resched_relaxed_wait(limit);

Quoted 4 lines
drivers/cpuidle/poll_state.c +0 -1
        }
        raw_local_irq_disable();
 
--
#32 lore raw

Re: [PATCH v8 12/12] cpuidle/poll_state: Wait for need-resched via tif_need_resched_relaxed_wait()

Quoted 71 lines · Rafael J. Wysocki
Quoted 56 lines · Ankur Arora

The inner loop in poll_idle() polls over the thread_info flags, waiting to see if the thread has TIF_NEED_RESCHED 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 in each iteration, the time check is rate-limited. In addition, each loop iteration executes cpu_relax() which on certain platforms provides a hint to the pipeline that the loop busy-waits, allowing the processor to reduce power consumption.

Switch over to tif_need_resched_relaxed_wait() instead, since that provides exactly that.

However, given that when running in idle we want to minimize our power consumption, continue to depend on CONFIG_ARCH_HAS_CPU_RELAX as that serves as an indicator that the platform supports an optimized version of tif_need_resched_relaxed_wait() (via smp_cond_load_acquire_timeout()).

---

Notes:
- use tif_need_resched_relaxed_wait() instead of smp_cond_load_relaxed_timeout()

 drivers/cpuidle/poll_state.c | 27 +++++----------------------
 1 file changed, 5 insertions(+), 22 deletions(-)
drivers/cpuidle/poll_state.c +0 -6
index c7524e4c522a..20136b3a08c2 100644
@@ -6,41 +6,24 @@
 #include <linux/cpuidle.h>
 #include <linux/export.h>
 #include <linux/irqflags.h>
-#include <linux/sched.h>
-#include <linux/sched/clock.h>
 #include <linux/sched/idle.h>
 #include <linux/sprintf.h>
 #include <linux/types.h>
 
-#define POLL_IDLE_RELAX_COUNT  200
-
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
                               struct cpuidle_driver *drv, int index)
 {
-       u64 time_start;
-

drivers/cpuidle/poll_state.c line 21

Quoted 10 lines
drivers/cpuidle/poll_state.c +2 -4
-       time_start = local_clock_noinstr();
-
        dev->poll_time_limit = false;
 
        raw_local_irq_enable();
        if (!current_set_polling_and_test()) {
-               unsigned int loop_count = 0;
-               u64 limit;
+               s64 limit;
+               bool nr_set;

It doesn't look like the nr_set variable is really needed.

Quoted 1 line
Quoted 4 lines

drivers/cpuidle/poll_state.c line 23

Quoted 2 lines
drivers/cpuidle/poll_state.c +1 -1
-               limit = cpuidle_poll_time(drv, dev);
+               limit = (s64)cpuidle_poll_time(drv, dev);

Is the explicit cast needed to suppress a warning? If not, I'd drop it.

Ack.

Quoted 15 lines
Quoted 4 lines
drivers/cpuidle/poll_state.c +0 -3
-               while (!need_resched()) {
-                       cpu_relax();
-                       if (loop_count++ < POLL_IDLE_RELAX_COUNT)

drivers/cpuidle/poll_state.c line 26

Quoted 10 lines
drivers/cpuidle/poll_state.c +2 -8
-                               continue;
-
-                       loop_count = 0;
-                       if (local_clock_noinstr() - time_start > limit) {
-                               dev->poll_time_limit = true;
-                               break;
-                       }
-               }
+               nr_set = tif_need_resched_relaxed_wait(limit);
+               dev->poll_time_limit = !nr_set;
Quoted 3 lines

This can be

dev->poll_time_limit = !tif_need_resched_relaxed_wait(limit);

Yeah that looks pretty clear.

Thanks!
--
ankur

#33 lore raw

Re: [PATCH v8 12/12] cpuidle/poll_state: Wait for need-resched via tif_need_resched_relaxed_wait()

In reply to Ankur Arora
kernel test robot noticed the following build warnings:

Hi Ankur,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]
[also build test WARNING on bpf/master linus/master v6.19-rc1 next-20251215] [cannot apply to arm64/for-next/core bpf-next/net arnd-asm-generic/master] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ankur-Arora/asm-generic-barrier-Add-smp_cond_load_relaxed_timeout/20251215-130116
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20251215044919.460086-13-ankur.a.arora%40oracle.com
patch subject: [PATCH v8 12/12] cpuidle/poll_state: Wait for need-resched via tif_need_resched_relaxed_wait()
config: x86_64-randconfig-005-20251215 (https://download.01.org/0day-ci/archive/20251216/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251216/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> vmlinux.o: warning: objtool: poll_idle+0x3b: call to tif_need_resched_relaxed_wait() leaves .noinstr.text section
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki