barrier: Add smp_cond_load_{relaxed,acquire}_timeout()
Subsystems (10)
v17 cover barrier: Add smp_cond_load_{relaxed,acquire}_timeout()
Hi,
Changes in this version address comments from Catalin and sashiko/bpf-bot [19]. The major change addresses build breakage in unrelated modules and changes the exit path in __smp_cond_load_relaxed_timeout() to simplify providing ACQUIRE semantics in smp_cond_load_acquire_timeout():
- patch-1, "asm-generic: barrier: Add smp_cond_load_relaxed_timeout()"
- expose the timeout state in __smp_cond_load_relaxed_timeout() so We use it later to provide acquire ordering for the timeout case in smp_cond_load_acquire_timeout().
- Associate the kernel-doc comment with the actual barrier, not the helper.
- patch-2, "arm64: barrier: Support smp_cond_load_relaxed_timeout()"
- mark arch_timer_evtstrm_available() noinstr. This is needed because smp_cond_load_relaxed_timeout() will be called from a noinstr context via __cpuidle poll_idle().
- patch-3, "arm64/delay: define arm64_wfet_deadline()"
- keep nsecs_to_cycles(), usecs_to_cycles() local to lib/delay.c
- patch-6, "asm-generic: barrier: Add smp_cond_load_acquire_timeout()",
- The change in patch-1 allows us to provide full ACQUIRE ordering for the timeout case. Earlier versions had a possible (compiler dependeent) hole around the case where cond_expr becomes true simultaneously with timeout.
- patch 14,15,16 "barrier: add tests for smp_cond_load_*_timeout()"
- rename barrier-timeout-test.c to barrier_timeout_kunit.c
Haris Okaovic tested v15 [18] "Tested again on AWS Graviton3/4/5 (ARM Neoverse V1/V2/V3) with haltpoll changes atop latest master 8ab1afb2e: still seeing average 6% improvement across various workloads (mysql, postgresql, cassandra, memcached) in latency sensitive configuration. The new unit tests all pass on both metal and virt variants of the same machines. -- Haris"
Sahil tested v16 [20] (with the arm64 polling in idle series [21] on a guest running on HyperV) and saw fairly significant guest latency, TPS improvements in database workloads:
"Workload: PostgreSQL 16.15/pgbench scale 100, client and server in the same VM over a unix socket, with single run:
with (-S) TPC-B-like r/w
clients base TPS haltpoll lat avg base TPS haltpoll lat avg
2 38658 +89% 52->27us 5299 +86% 378->203us 4 72987 +99% 55->28 10305 +87% 388->208 16 300895 +91% 53->28 42035 +83% 381->208 32 590987 +93% 55->28 78319 +85% 410->221 64 987749 +0% 65->65 130675 +3% 490->474"
Possible reason for the improvement is lower wakeup latency, where polling in idle allows us to not switch in and out of deeper idle states or from the hypervisor (when run in a VM.)
A little more on this in the performance section below.
Motivation
===
The core kernel often uses smp_cond_load_{relaxed,acquire}() to spin on condition variables with architectural primitives used to avoid hammering the relevant cachelines.
(This primitive can vary greatly across architectures: on x86 it's a cpu_relax() to slow down the pipeline. On arm64, this is a __cmpwait() which waits for a cacheline to change state in a time limited fashion.)
Regardless of architectural details, typical smp_cond_load*() usage does not allow for termination until the condition change occurs.
Beyond the core kernel, there are cases where it is useful to additionally terminate on a timeout. Two cases:
- cpuidle poll_idle(): wait for need-resched until the cpuidle polling duration expires.
- rqspinlock: nested qspinlock acquisition that terminates on timeout or deadlock.
Accordingly add two interfaces (with their generic and arm64 specific implementations):
smp_cond_load_relaxed_timeout(ptr, cond_expr, time_expr, timeout) smp_cond_load_acquire_timeout(ptr, cond_expr, time_expr, timeout)
Also add tif_need_resched_relaxed_wait() which wraps the polling pattern and its scheduler specific details in poll_idle(). In addition add atomic_cond_read_*_timeout(), atomic64_cond_read_*_timeout(), and atomic_long wrappers.
Structurally, both the smp_cond_load_*_timeout() interfaces are similar to smp_cond_load*(), with the addition of a rate-limited time-check.
Usage
==
These interfaces drop straight-forwardly into the rqspinlock logic since qspinlock already uses smp_cond_load*(), and the time-check extension can now be used for timeout and deadlock handling.
Using tif_need_resched_relaxed_wait() in poll_idle() removes any architectural details allowing arm64 to straight-forwardly support that path.
(However, for efficiency reasons cpuidle/poll_state.c continues to depend on ARCH_HAS_CPU_RELAX since that is defined on architectures with an optimized architectural primitive.)
Performance
==
Apart from simplifications due to this change, supporting polling in cpuidle on arm64 helps improve wakeup latency (needs a few cpuidle/acpi patches):
# perf stat -r 5 --cpu 4,5 -e task-clock,cycles,instructions,sched:sched_wake_idle_without_ipi \
perf bench sched pipe -l 1000000 -c 4 # No haltpoll (and, no TIF_POLLING_NRFLAG):
Performance counter stats for 'CPU(s) 4,5' (5 runs):
25,229.57 msec task-clock # 2.000 CPUs utilized ( +- 7.75% )
45,821,250,284 cycles # 1.816 GHz ( +- 10.07% )
26,557,496,665 instructions # 0.58 insn per cycle ( +- 0.21% )0 sched:sched_wake_idle_without_ipi # 0.000 /sec
12.615 +- 0.977 seconds time elapsed ( +- 7.75% )
# Haltpoll:
Performance counter stats for 'CPU(s) 4,5' (5 runs):
15,131.58 msec task-clock # 2.000 CPUs utilized ( +- 10.00% )
34,158,188,839 cycles # 2.257 GHz ( +- 6.91% )
20,824,950,916 instructions # 0.61 insn per cycle ( +- 0.09% )
1,983,822 sched:sched_wake_idle_without_ipi # 131.105 K/sec ( +- 0.78% )7.566 +- 0.756 seconds time elapsed ( +- 10.00% )
We get improved latency because we don't switch in and out of a deeper sleep state or from the hypervisor. This also causes us to execute ~20% fewer instructions.
Changelog:
==
v16 [19] (as above):
- patch-1, "asm-generic: barrier: Add smp_cond_load_relaxed_timeout()"
- expose the timeout state in __smp_cond_load_relaxed_timeout() so We use it later to provide acquire ordering for the timeout case in smp_cond_load_acquire_timeout().
- Associate the kernel-doc comment with the actual barrier, not the helper.
- patch-2, "arm64: barrier: Support smp_cond_load_relaxed_timeout()"
- mark arch_timer_evtstrm_available() noinstr. This is needed because smp_cond_load_relaxed_timeout() will be called from a noinstr context via __cpuidle poll_idle().
- patch-3, "arm64/delay: define arm64_wfet_deadline()"
- keep nsecs_to_cycles(), usecs_to_cycles() local to lib/delay.c
- patch-6, "asm-generic: barrier: Add smp_cond_load_acquire_timeout()",
- The change in patch-1 allows us to provide full ACQUIRE ordering for the timeout case. Earlier versions had a possible (compiler dependeent) hole around the case where cond_expr becomes true simultaneously with timeout.
- patch 14,15,16 "barrier: add tests for smp_cond_load_*_timeout()"
- rename barrier-timeout-test.c to barrier_timeout_kunit.c
v15 [17]:
- patch-1, "asm-generic: barrier: Add smp_cond_load_relaxed_timeout()"
- unify the exit path for the loop condition and timeout case
- includes vdso/time64.h
- document the timeout_ns == 0 case
- use u64 for internal arithmetic
- patch-3, "arm64/delay: move, fixup usecs_to_cycles()"
- While moving USECS_TO_CYCLES() into the driver namespace also make it a static inline rpmh_usecs_to_cycles()
- patch-6, "asm-generic: barrier: Add smp_cond_load_acquire_timeout()",
- use the change in patch-1 to fixup, simplify the ACQUIRE path
- thresh out the commit message a bit more
- patch 14,15,16 "barrier: add tests for smp_cond_load_*_timeout()"
- enclose the calls to smp_cond_load_{relaxed,acquire}_timeout() in preempt_disable(), preempt_enable().
- WFET makes it difficult to use a synthetic timer. So skip timeout tests on configurations with WFET.
v14 [16]:
- patch-1, "asm-generic: barrier: Add smp_cond_load_relaxed_timeout()"
- deferred time-check is now limited to a single usec.
- smp_cond_load_relaxed_timeout() now enforces that the timeout_ns value does fit in s64.
- SMP_TIMEOUT_POLL_COUNT is explicitly defined to 1 if the architecture provides a waiting implementation of cpu_poll_relax() (defines CPU_POLL_RELAX_WAITS).
- patch-3, "arm64/delay: move, fixup usecs_to_cycles()"
- convert the 32-bit arithmetic to use mul_u64_u64_shr() to avoid truncating large delay values.
- make usecs_to_cycles(), nsecs_to_cycles() static inlines.
- patch-6, "asm-generic: barrier: Add smp_cond_load_acquire_timeout()"
- provide full acquire semantics (via smp_load_acquire()) in the failure path.
- patch-16, "barrier: timeout validity checks for smp_cond_load_acquire_timeout()"
- add some tests for clocks that can fail (rqspinlock like semantics)
v13 [15]:
- rename kconfig entry for the barrier kunit test to follow the kunit style guide (s/BARRIER_TIMEOUT_TEST/BARRIER_TIMEOUT_KUNIT_TEST)
- make the kunit test be visible only if CONFIG_KUNIT_ALL_TESTS is not enabled.
Both comments from Julian Braha.
v12 [14]:
- smp_cond_load_acquire_timeout() now only has acquire semantics in the success (non-timeout) case.
- arm64 now does not define ARCH_HAS_CPU_RELAX as without also defining TIF_POLLING_NRFLAG, in some cases we end up with a degenerate version of poll_idle().
- kunit: removed the test case for timeout=-1 (not supported) Also add test cases for timeout=0, timeout=1.
(All of these address review comments from sashiko/bpf-bot.)
v11 [13]:
- addressed some review comments from sashiko (see commit notes)
- The one notable change is to the implementation of smp_cond_load_acquire_timeout() where there was a missed control dependency in the timeout case.
All the others are minor.
- fixed a low probability race in the kunit test added in v11.
- added a bunch of kunit tests validating the implementation's use of the clock.
v10 [10]:
- add a comment mentioning that smp_cond_load_relaxed_timeout() might be using architectural primitives that don't support MMIO. (David Laight, Catalin Marinas)
- added a kunit test for smp_cond_load_relaxed_timeout() (Andrew Morton.)
v9 [9]:
- s/@cond/@cond_expr/ (Randy Dunlap)
- Clarify that SMP_TIMEOUT_POLL_COUNT is only around memory addresses. (David Laight)
- Add the missing config ARCH_HAS_CPU_RELAX in arch/arm64/Kconfig. (Catalin Marinas).
- Switch to arch_counter_get_cntvct_stable() (via __delay_cycles()) in the cmpwait path instead of using arch_timer_read_counter(). (Catalin Marinas)
v8 [0]:
- Defer evaluation of @time_expr_ns to when we hit the slowpath. (comment from Alexei Starovoitov).
- Mention that cpu_poll_relax() is better than raw CPU polling only where ARCH_HAS_CPU_RELAX is defined.
- also define ARCH_HAS_CPU_RELAX for arm64.
(Came out of a discussion with Will Deacon.)
- Split out WFET and WFE handling. I was doing both of these in a common handler.
(From Will Deacon and in an earlier revision by Catalin Marinas.)
- Add mentions of atomic_cond_read_{relaxed,acquire}(), atomic_cond_read_{relaxed,acquire}_timeout() in Documentation/atomic_t.txt.
- Use the BIT() macro to do the checking in tif_bitset_relaxed_wait().
- Cleanup unnecessary assignments, casts etc in poll_idle(). (From Rafael Wysocki.)
- Fixup warnings from kernel build robot
v7 [1]:
- 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 [2]:
- 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 [3]:
- 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 [4]:
- 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 [5]:
- further interface simplifications (suggested by Catalin Marinas)
v2 [6]:
- 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 [7]:
- 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. [8]
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]/
[8] https://lore.kernel.org/lkml/[email protected]/
[9] https://lore.kernel.org/lkml/[email protected]/
[10] https://lore.kernel.org/lkml/[email protected]/
[11] https://lore.kernel.org/lkml/[email protected]/
[12] https://lore.kernel.org/lkml/[email protected]/
[13] https://lore.kernel.org/all/[email protected]/#r
[14] https://lore.kernel.org/all/[email protected]/
[15] https://lore.kernel.org/all/[email protected]/
[16] https://lore.kernel.org/all/[email protected]/
[17] https://lore.kernel.org/all/[email protected]/
[18] https://lore.kernel.org/all/178908146776.2725577.2613699832033292946.b4-review@b4/
[19] https://lore.kernel.org/lkml/[email protected]/
[20] https://lore.kernel.org/lkml/auLHZnaCIajW1kxX@kali/
[21] https://lore.kernel.org/all/[email protected]/
Ankur Arora (16):
asm-generic: barrier: Add smp_cond_load_relaxed_timeout()
arm64: barrier: Support smp_cond_load_relaxed_timeout() arm64/delay: define arm64_wfet_deadline()
arm64: support WFET in smp_cond_load_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()
arm64/delay: enable testing smp_cond_load_relaxed_timeout()
barrier: add tests for smp_cond_load_*_timeout()
barrier: timeout validity checks for smp_cond_load_relaxed_timeout()
barrier: timeout validity checks for smp_cond_load_acquire_timeout()
Documentation/atomic_t.txt | 14 +- arch/arm64/include/asm/arch_timer.h | 2 + arch/arm64/include/asm/barrier.h | 24 +++ arch/arm64/include/asm/cmpxchg.h | 64 ++++-- arch/arm64/include/asm/rqspinlock.h | 85 -------- arch/arm64/lib/delay.c | 38 +++- drivers/clocksource/arm_arch_timer.c | 4 +- drivers/cpuidle/poll_state.c | 21 +- include/asm-generic/barrier.h | 164 +++++++++++++++ include/linux/atomic.h | 10 + include/linux/atomic/atomic-long.h | 18 +- include/linux/sched/idle.h | 29 +++ kernel/bpf/rqspinlock.c | 78 ++++--- lib/Kconfig.debug | 10 + lib/tests/Makefile | 1 + lib/tests/barrier_timeout_kunit.c | 297 +++++++++++++++++++++++++++ scripts/atomic/gen-atomic-long.sh | 16 +- 17 files changed, 704 insertions(+), 171 deletions(-) create mode 100644 lib/tests/barrier_timeout_kunit.c
Signature · 1 line
2.43.7
PATCH v17 1/16 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.
The interface loops around doing a periodic time-check, waiting for the condition variable to change. Loop iterations use cpu_poll_relax() to slow down the busy-wait and these, unless overridden by the architecture code, amount to a cpu_relax().
There are two ways for the time-check to fail: the timeout case or, @time_expr_ns returning an invalid value (negative or zero). The second failure mode allows for clocks attached to the clock-domain of @cond_expr -- clocks which might cease to operate meaningfully once some state internal to @cond_expr has changed -- to fail.
Evaluation of @time_expr_ns: in the fastpath we want to keep the performance close to smp_cond_load_relaxed(). So defer evaluation of the potentially costly @time_expr_ns to the slowpath.
This also means that there will always be some hardware dependent duration that has passed in cpu_poll_relax() iterations at the time of first evaluation. Additionally cpu_poll_relax() is not guaranteed to return at timeout boundary. In sum, expect timeout overshoot when we exit due to expiration of the timeout.
The number of spin iterations before time-check, SMP_TIMEOUT_POLL_COUNT is chosen to be 200 by default. With a cpu_poll_relax() iteration taking ~20-30 cycles (measured on a variety of x86 platforms), we expect a time-check every ~4000-6000 cycles.
If a architecture provides a waiting implementation for cpu_poll_relax() (and signifies that by defining CPU_POLL_RELAX_WAITS) we define SMP_TIMEOUT_POLL_COUNT to 1.
Lastly, config option ARCH_HAS_CPU_RELAX indicates availability of a cpu_poll_relax() that is cheaper than polling. Long timeout values might not make sense for architectures not having ARCH_HAS_CPU_RELAX.
---
Notes:
- Expose the timeout state in __smp_cond_load_relaxed_timeout(). We use it later to provide acquire ordering for the timeout case in smp_cond_load_acquire_timeout().
- Associate the kernel-doc comment with the actual barrier, not the helper.
include/asm-generic/barrier.h | 112 ++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+)
include/asm-generic/barrier.h +112 -0
@@ -15,6 +15,7 @@#include <linux/compiler.h> #include <linux/kcsan-checks.h> +#include <vdso/time64.h> #include <asm/rwonce.h> #ifndef nop
@@ -273,6 +274,117 @@ do { \ })#endif +/* +* Number of times we iterate in the loop before doing the time check. +*/ +#ifndef SMP_TIMEOUT_POLL_COUNT +#ifdef CPU_POLL_RELAX_WAITS +#define SMP_TIMEOUT_POLL_COUNT 1/* Wait mode. No need to poll. */ +#else +/* +* Assume that cpu_poll_relax() provides a small blip in the pipeline. +* Combine a reasonable number of cpu_poll_relax() instances before +* doing anything substantial like a time-check. +* Note that this assumes that the rest of the loop (largely evaluation +* of the loop condition is relatively cheap.) +*/ +#define SMP_TIMEOUT_POLL_COUNT 200 +#endif +#endif + +/* +* cpu_poll_relax() stitches up two kinds of primitives: ones that provide +* a momentary blip in the pipeline (ex. cpu_relax() on x86), or ones that +* support waiting for @ptr value to change, coupled with a precise (or not) +* timeout. +* +* We keep both together because the objective is to minimize expensive +* operations while polling on @ptr waiting for it to change. Either +* version allows for that. +* The arguments (@ptr, @val, @timeout_ns) are only needed for waiting +* implementations. +* +* Note that platforms with a suitable cpu_poll_relax() implementation are +* expected to define ARCH_HAS_CPU_RELAX. +*/ +#ifndef cpu_poll_relax +#define cpu_poll_relax (ptr, val, timeout_ns) cpu_relax() +#endif + +#ifndef smp_cond_load_relaxed_timeout +#define __smp_cond_load_relaxed_timeout (ptr, cond_expr, \ + time_expr_ns, timeout_ns, \ + timed_out) \ +({ \ + typeof(ptr) __PTR = (ptr); \ + __unqual_scalar_typeof(*(ptr)) VAL; \ + u32 __scl_count = 0, __scl_spin = SMP_TIMEOUT_POLL_COUNT; \ + u64 __scl_timeout = NSEC_PER_USEC, __scl_time_end = 0; \ + s64 __scl_time_now; \ + \ + for (;;) { \ + VAL = READ_ONCE(*__PTR); \ + if (cond_expr || timed_out) \ + break; \ + cpu_poll_relax(__PTR, VAL, (u64)__scl_timeout); \ + if (++__scl_count < __scl_spin) \ + continue; \ + __scl_time_now = (s64)(time_expr_ns); \ + if (unlikely(__scl_time_end == 0)) { \ + __scl_timeout = (timeout_ns); \ + __scl_time_end = __scl_time_now + __scl_timeout;\ + } \ + if (__scl_time_now <= 0 || \ + __scl_time_end <= __scl_time_now) { \ + timed_out = true; \ + continue; \ + } \ + __scl_count = 0; \ + __scl_timeout = __scl_time_end - __scl_time_now; \ + } \ + (typeof(*(ptr)))VAL; \ +}) + +/** +* smp_cond_load_relaxed_timeout() - (Spin) wait for cond with no ordering +* until a timeout expires. +* @ptr: pointer to the variable to wait on. +* @cond_expr: boolean expression to wait for. +* @time_expr_ns: expression that evaluates to monotonic time (in ns) or, +* on failure, returns zero or a negative value. +* @timeout_ns: timeout value in ns. A timeout value of zero is treated +* as the minimum wait so we evaluate cond_expr at least once. +* Both of the above are expected to be compatible with s64; the signed +* value is used to handle the failure case in @time_expr_ns. +* +* Equivalent to using READ_ONCE() on the condition variable. +* +* Callers that expect to wait for prolonged durations might want +* to take into account the availability of ARCH_HAS_CPU_RELAX. +* +* Note that @ptr is expected to point to a memory address. Using this +* interface with MMIO will be slower (since SMP_TIMEOUT_POLL_COUNT is +* tuned for memory) and might also break in interesting architecture +* dependent ways. +*/ +#define smp_cond_load_relaxed_timeout (ptr, cond_expr, \ + time_expr_ns, timeout_ns) \ +({ \ + s64 __scl_timeout_ns = (s64)(timeout_ns); \ + __unqual_scalar_typeof(*(ptr)) VAL; \ + bool __maybe_unused __scl_timed_out = false; \ + \ + if (unlikely(__scl_timeout_ns < 0)) \ + VAL = READ_ONCE(*(ptr)); \ + else \ + VAL = __smp_cond_load_relaxed_timeout(ptr, cond_expr, \ + time_expr_ns, \ + __scl_timeout_ns, \ + __scl_timed_out); \ + (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.43.7
[email protected]
AI reviewRe: [PATCH v17 01/16] asm-generic: barrier: Add smp_cond_load_relaxed_timeout()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] asm-generic: barrier: VAL shadowing in smp_cond_load_relaxed_timeout() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] asm-generic: barrier: VAL shadowing in smp_cond_load_relaxed_timeout()
--
commit 02ca6bf44a71704c43817e6ae9df82d53740b4ff
Author: Ankur Arora <[email protected]>
asm-generic: barrier: Add smp_cond_load_relaxed_timeout()
This commit adds smp_cond_load_relaxed_timeout() and its helper __smp_cond_load_relaxed_timeout() to spin-wait on a condition with a timeout using cpu_poll_relax().
Quoted 4 lines
include/asm-generic/barrier.h +0
-0
[ ... ]
Quoted 3 lines
include/asm-generic/barrier.h +0
-0@@ -273,6 +274,117 @@ do { \ })#endif
[ ... ]
Quoted 7 lines
include/asm-generic/barrier.h +7
-0+#ifndef smp_cond_load_relaxed_timeout +#define __smp_cond_load_relaxed_timeout (ptr, cond_expr, \ + time_expr_ns, timeout_ns, \ + timed_out) \ +({ \ + typeof(ptr) __PTR = (ptr); \ + __unqual_scalar_typeof(*(ptr)) VAL; \
[ ... ]
Quoted 7 lines
include/asm-generic/barrier.h +7
-0+#define smp_cond_load_relaxed_timeout (ptr, cond_expr, \ + time_expr_ns, timeout_ns) \ +({ \ + s64 __scl_timeout_ns = (s64)(timeout_ns); \ + __unqual_scalar_typeof(*(ptr)) VAL; \ + bool __maybe_unused __scl_timed_out = false; \ + \
include/asm-generic/barrier.h do { \ · line 386
Quoted 10 lines
include/asm-generic/barrier.h +10
-0+if (unlikely (__scl_timeout_ns <0 )) \ +VAL =READ_ONCE (*(ptr)); \ +else \ +VAL =__smp_cond_load_relaxed_timeout (ptr, cond_expr, \ + time_expr_ns, \ + __scl_timeout_ns, \ + __scl_timed_out); \ + (typeof (*(ptr )))VAL ; \ +}) +#endif
[Severity: Low]
Does naming the outer temporary VAL in smp_cond_load_relaxed_timeout() cause -Wshadow warnings when __smp_cond_load_relaxed_timeout() declares its own VAL in the nested statement expression?
Unlike smp_cond_load_acquire(), which names its outer variable _val, reusing VAL here shadows the outer variable on every call (breaking W=2 builds with CONFIG_WERROR) and also declares VAL before evaluating ptr, which would capture any caller variable named VAL referenced in ptr.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v17 01/16] asm-generic: barrier: Add smp_cond_load_relaxed_timeout()
Quoted 55 lines · [email protected] writes:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] asm-generic: barrier: VAL shadowing insmp_cond_load_relaxed_timeout()
--commit
02ca6bf44a71704c43817e6ae9df82d53740b4ff
Author: Ankur Arora <[email protected]>asm-generic: barrier: Add
smp_cond_load_relaxed_timeout()This commit adds
smp_cond_load_relaxed_timeout()and its helper__smp_cond_load_relaxed_timeout()to spin-wait on a condition with a timeout usingcpu_poll_relax().Quoted 4 lines
include/asm-generic/barrier.h +0
-0[ ... ]
Quoted 3 lines
include/asm-generic/barrier.h +0
-0@@ -273,6 +274,117 @@ do { \ })#endif [ ... ]
Quoted 7 lines
include/asm-generic/barrier.h +7
-0+#ifndef smp_cond_load_relaxed_timeout +#define __smp_cond_load_relaxed_timeout (ptr, cond_expr, \ + time_expr_ns, timeout_ns, \ + timed_out) \ +({ \ + typeof(ptr) __PTR = (ptr); \ + __unqual_scalar_typeof(*(ptr)) VAL; \[ ... ]
Quoted 7 lines
include/asm-generic/barrier.h +7
-0+#define smp_cond_load_relaxed_timeout (ptr, cond_expr, \ + time_expr_ns, timeout_ns) \ +({ \ + s64 __scl_timeout_ns = (s64)(timeout_ns); \ + __unqual_scalar_typeof(*(ptr)) VAL; \ + bool __maybe_unused __scl_timed_out = false; \ + \include/asm-generic/barrier.h do { \ · line 386
Quoted 10 lines
include/asm-generic/barrier.h +10
-0+if (unlikely (__scl_timeout_ns <0 )) \ +VAL =READ_ONCE (*(ptr)); \ +else \ +VAL =__smp_cond_load_relaxed_timeout (ptr, cond_expr, \ + time_expr_ns, \ + __scl_timeout_ns, \ + __scl_timed_out); \ + (typeof (*(ptr )))VAL ; \ +}) +#endif [Severity: Low]
Does naming the outer temporary VAL insmp_cond_load_relaxed_timeout()cause -Wshadow warnings when__smp_cond_load_relaxed_timeout()declares its own VAL in the nested statement expression?
Quoted 4 lines
Unlike
smp_cond_load_acquire(), which names its outer variable_val, reusing VAL here shadows the outer variable on every call (breaking W=2 builds withCONFIG_WERROR) and also declares VAL before evaluating ptr, which would capture any caller variable named VAL referenced in ptr.
VAL is special given that it is referenced via cond_expr. However, in this case we don't evaluate cond_expr and so can safely rename.
Will send an updated version of this patch (and patch-6).
--
ankur
PATCH v17 2/16 arm64: barrier: Support smp_cond_load_relaxed_timeout()
In reply to Ankur AroraSupport waiting in smp_cond_load_relaxed_timeout() via __cmpwait_relaxed(). To ensure that we wake from waiting in WFE periodically and don't block forever if there are no stores to ptr, this path is only used when the event-stream is enabled.
Note that when using __cmpwait_relaxed() we ignore the timeout value, allowing an overshoot by up to the event-stream period. And, in the unlikely event that the event-stream is unavailable, fallback to spin-waiting.
Define CPU_POLL_RELAX_WAITS to state that we have a waiting implementation. (Non-production environments might not have arch_timer_evtstrm_available() but we don't care about that configuration.)
Also mark arch_timer_evtstrm_available() noinstr since we will be using smp_cond_load_relaxed_timeout() from the cpuidle context which is non-instrumentable.
Note that with this we have enough to define ARCH_HAS_CPU_RELAX to indicate that we support an optimized implementation of cpu_poll_relax(). However, defer defining ARCH_HAS_CPU_RELAX as that enables polling based C-state handling, which really needs TIF_POLLING_NRFLAG.
---
Notes:
- mark arch_timer_evtstrm_available() noinstr. This is needed because smp_cond_load_relaxed_timeout() will be called from a noinstr context via __cpuidle poll_idle().
arch/arm64/include/asm/barrier.h | 22 ++++++++++++++++++++++ drivers/clocksource/arm_arch_timer.c | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-)
arch/arm64/include/asm/barrier.h +22 -0
@@ -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,27 @@ do { \ (typeof(*ptr))VAL; \ }) +/* Re-declared here to avoid include dependency. */ +extern bool arch_timer_evtstrm_available (void ); + +/* +* In the common case, cpu_poll_relax() sits waiting in __cmpwait_relaxed() +* for @ptr value to change. +* +* State this by defining CPU_POLL_RELAX_WAITS. This enables a time-check +* optimization in smp_cond_load_{relaxed,acquire}_timeout(). Note that this +* is not ideal for the cpu_relax() fallback but that's not a production +* configuration. +*/ +#define CPU_POLL_RELAX_WAITS + +#define cpu_poll_relax (ptr, val, timeout_ns) do { \ + if (arch_timer_evtstrm_available()) \ + __cmpwait_relaxed(ptr, val); \ + else \ + cpu_relax(); \ +} while (0) +#include <asm-generic/barrier.h> #endif /* __ASSEMBLER__ */
drivers/clocksource/arm_arch_timer.c +1 -1
@@ -884,7 +884,7 @@ u32 arch_timer_get_rate(void)return arch_timer_rate; } -bool arch_timer_evtstrm_available (void ) +bool noinstr arch_timer_evtstrm_available(void) {/* * We might get called from a preemptible context. This is fine
Signature · 1 line
2.43.7
PATCH v17 3/16 arm64/delay: define arm64_wfet_deadline()
In reply to Ankur AroraMake the WFET deadline available via arm64_wfet_deadline().
Also update xloops_to_cycles() (used to compute nsecs_to_cycles()) to use mul_u64_u64_shr() instead of 32-bit fixed point arithmetic to avoid truncation for delay values larger than ~10s. This adds an extra mul operation but is only used from delay loops so shouldn't matter too much.
While at it, define some related constants and convert relevant macros to static inlines.
---
Notes:
- keep nsecs_to_cycles(), usecs_to_cycles() local to lib/delay.c
- note that the eventual intent is to use smp_cond_load_relaxed_timeout() and thus arm64_wfet_deadline() from poll_idle() (which is noinstr). Will do handling for noinstr in arm64_wfet_deadline() (and __delay_cycles() etc) as part of that series.
arch/arm64/include/asm/arch_timer.h | 2 ++ arch/arm64/lib/delay.c | 38 +++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-)
arch/arm64/include/asm/arch_timer.h +2 -0
@@ -86,6 +86,8 @@ static inline notrace u64 arch_timer_read_cntvct_el0(void)return cnt; } +u64 arm64_wfet_deadline (u64 timeout_ns); +#define arch_timer_reg_read_stable (reg) \ ({ \ erratum_handler(read_ ## reg)(); \
arch/arm64/lib/delay.c +31 -7
@@ -12,15 +12,33 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/timex.h> +#include <linux/math64.h> +#include <linux/types.h> +#include <asm/param.h> #include <clocksource/arm_arch_timer.h> -#define USECS_TO_CYCLES (time_usecs) \ - xloops_to_cycles((time_usecs) * 0x10C7UL) +/* 2**32 / 1000000 (rounded up) */ +#define __usecs_to_xloops_mult 0x10C7UL -static inline unsigned long xloops_to_cycles (unsigned long xloops) +/* 2**32 / 1000000000 (rounded up) */ +#define __nsecs_to_xloops_mult 0x5UL + +static inline u64 xloops_to_cycles (u64 xloops) +{ +u64 loops_per_sec = (u64 )loops_per_jiffy *HZ ; + +return mul_u64_u64_shr (xloops, loops_per_sec,32 ); +} + +static inline u64 usecs_to_cycles (u64 time_usecs) +{ +return xloops_to_cycles (time_usecs * __usecs_to_xloops_mult); +} + +static inline u64 nsecs_to_cycles (u64 time_nsecs) { -return (xloops * loops_per_jiffy *HZ ) >>32 ; +return xloops_to_cycles (time_nsecs * __nsecs_to_xloops_mult); }/*
@@ -54,7 +72,7 @@ void __delay(unsigned long cycles)wfet (end); }else if (arch_timer_evtstrm_available ()) {const cycles_t timer_evt_period = -USECS_TO_CYCLES (ARCH_TIMER_EVT_STREAM_PERIOD_US ); +usecs_to_cycles (ARCH_TIMER_EVT_STREAM_PERIOD_US );while ((__delay_cycles () - start + timer_evt_period) < cycles)wfe ();
@@ -65,6 +83,12 @@ void __delay(unsigned long cycles) }EXPORT_SYMBOL (__delay); +u64 arm64_wfet_deadline (u64 timeout_ns) +{ +return __delay_cycles () +nsecs_to_cycles (timeout_ns); +} +EXPORT_SYMBOL_GPL (arm64_wfet_deadline); +inline void __const_udelay (unsigned long xloops) {__delay (xloops_to_cycles (xloops));
@@ -73,12 +97,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);
Signature · 1 line
2.43.7
PATCH v17 4/16 arm64: support WFET in smp_cond_load_relaxed_timeout()
In reply to Ankur AroraWhen using WFE we can go beyond the timeout since, lacking an event we depend on the event-stream for wakeups when there's no event.
So, prefer WFET where available to take advantage of precise wait.
Handling of WFET is via __cmpwait_timeout() which behaves similarly to __cmpwait() calling out to the __cmpwait_case_timeout_##sz() or __cmpwait_case_##sz() functions.
---
Notes:
- use arm64_wfet_deadline() to calculate the deadline.
arch/arm64/include/asm/barrier.h | 8 ++-- arch/arm64/include/asm/cmpxchg.h | 64 ++++++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 15 deletions(-)
arch/arm64/include/asm/barrier.h +5 -3
@@ -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 @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. * * State this by defining CPU_POLL_RELAX_WAITS. This enables a time-check * optimization in smp_cond_load_{relaxed,acquire}_timeout(). Note that this
@@ -235,7 +235,9 @@ extern bool arch_timer_evtstrm_available(void);#define CPU_POLL_RELAX_WAITS #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 +52 -12
90 lines not on this page · raw message
Signature · 1 line
2.43.7
[email protected]
AI reviewRe: [PATCH v17 04/16] arm64: support WFET in smp_cond_load_relaxed_timeout()
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] arm64: cpu_poll_relax() calls non-noinstr arm64_wfet_deadline() - [Low] arm64: unexported…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] arm64: cpu_poll_relax() calls non-noinstr arm64_wfet_deadline()
- [Low] arm64: unexported arch_timer_evtstrm_available() in cpu_poll_relax()
--
commit 0bd9d96e9ca449b0fa90386c0f9dafaac6bd9c03
Author: Ankur Arora <[email protected]>
arm64: support WFET in smp_cond_load_relaxed_timeout()
This commit adds WFET support to cpu_poll_relax() on arm64 via __cmpwait_relaxed_timeout() and __cmpwait_case_timeout_##sz(), using arm64_wfet_deadline() to compute the wait deadline when ARM64_HAS_WFXT is present.
Quoted 16 lines
arch/arm64/include/asm/barrier.h +2
-2@@ -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 @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. * * State this by defining CPU_POLL_RELAX_WAITS. This enables a time-check * optimization in smp_cond_load_{relaxed,acquire}_timeout(). Note that this
arch/arm64/include/asm/barrier.h extern bool arch_timer_evtstrm_available(void); · line 243
Quoted 10 lines
arch/arm64/include/asm/barrier.h +3
-1@@ -235,7 +235,9 @@ extern bool arch_timer_evtstrm_available(void);#define CPU_POLL_RELAX_WAITS #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(); \
[Severity: Low]
Should arch_timer_evtstrm_available() be exported with EXPORT_SYMBOL_GPL() like arm64_wfet_deadline()?
Commit "arm64/delay: define arm64_wfet_deadline()" exports arm64_wfet_deadline() via EXPORT_SYMBOL_GPL(), while commit "arm64/delay:
enable testing smp_cond_load_relaxed_timeout()" later in the series only exports arch_timer_evtstrm_available() via EXPORT_SYMBOL_IF_KUNIT(). Because cpu_poll_relax() expands at each call site to both the __cmpwait_relaxed_timeout() branch and the arch_timer_evtstrm_available() fallback branch, any non-KUnit loadable module using smp_cond_load_relaxed_timeout() or smp_cond_load_acquire_timeout() will fail to link with an unresolved reference to arch_timer_evtstrm_available().
Quoted 4 lines
arch/arm64/include/asm/cmpxchg.h +0
-0
[ ... ]
Quoted 8 lines
arch/arm64/include/asm/cmpxchg.h +0
-4@@ -235,20 +236,55 @@ __CMPWAIT_CASE( , , 64);#undef __CMPWAIT_CASE -#define __CMPWAIT_GEN (sfx) \ -static __always_inline void __cmpwait##sfx(volatile void *ptr, \ - unsigned long val, \ - int size) \
arch/arm64/include/asm/cmpxchg.h __CMPWAIT_CASE( , , 64); · line 248
Quoted 10 lines
arch/arm64/include/asm/cmpxchg.h +10
-0+/* Declared locally to avoid recursive include dependency */ +u64 arm64_wfet_deadline (u64 timeout_ns); + +#define __CMPWAIT_TIMEOUT_CASE (w, sfx, sz) \ +static inline void __cmpwait_case_timeout_##sz(volatile void *ptr, \ + unsigned long val, \ + u64 timeout_ns) \ +{ \ + unsigned long tmp; \ + u64 ecycles = arm64_wfet_deadline(timeout_ns); \
[Severity: Medium]
Does calling arm64_wfet_deadline() from cpu_poll_relax() -> __cmpwait_relaxed_timeout() -> __cmpwait_case_timeout_##sz() violate noinstr constraints when smp_cond_load_relaxed_timeout() is used from cpuidle?
Commit "arm64: barrier: Support smp_cond_load_relaxed_timeout()" marked arch_timer_evtstrm_available() noinstr so cpu_poll_relax() can be called from .cpuidle.text while RCU is not watching. On systems with ARM64_HAS_WFXT, cpu_poll_relax() instead calls __cmpwait_case_timeout_##sz() (marked static inline rather than __always_inline) and the out-of-line arm64_wfet_deadline() in arch/arm64/lib/delay.c:
arch/arm64/lib/delay.c:arm64_wfet_deadline() {
return __delay_cycles() + nsecs_to_cycles(timeout_ns);
}Neither arm64_wfet_deadline() nor nsecs_to_cycles() is marked noinstr, and __delay_cycles() uses guard(preempt_notrace)() (which can invoke __preempt_schedule_notrace()) and __arch_counter_get_cntvct_stable() (which uses __this_cpu_read() and non-noinstr erratum callbacks under CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND). Calling this path from an RCU extended quiescent state can trigger RCU and lockdep warnings or execute ftrace, kprobe, or sanitizer instrumentation while RCU is not watching.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v17 04/16] arm64: support WFET in smp_cond_load_relaxed_timeout()
Quoted 49 lines · [email protected] writes:
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] arm64:cpu_poll_relax()calls non-noinstrarm64_wfet_deadline()
- [Low] arm64: unexportedarch_timer_evtstrm_available()incpu_poll_relax()
--commit
0bd9d96e9ca449b0fa90386c0f9dafaac6bd9c03
Author: Ankur Arora <[email protected]>arm64: support WFET in
smp_cond_load_relaxed_timeout()This commit adds WFET support to
cpu_poll_relax()on arm64 via__cmpwait_relaxed_timeout()and __cmpwait_case_timeout_##sz(), usingarm64_wfet_deadline()to compute the wait deadline whenARM64_HAS_WFXTis present.Quoted 16 lines
arch/arm64/include/asm/barrier.h +2
-2@@ -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 @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. * * State this by defining CPU_POLL_RELAX_WAITS. This enables a time-check * optimization in smp_cond_load_{relaxed,acquire}_timeout(). Note that this arch/arm64/include/asm/barrier.h extern bool arch_timer_evtstrm_available(void); · line 243
Quoted 10 lines
arch/arm64/include/asm/barrier.h +3
-1@@ -235,7 +235,9 @@ extern bool arch_timer_evtstrm_available(void);#define CPU_POLL_RELAX_WAITS #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(); \[Severity: Low]
Shouldarch_timer_evtstrm_available()be exported withEXPORT_SYMBOL_GPL()likearm64_wfet_deadline()?
Quoted 4 lines
[ ... ] any non-KUnit loadable module using
smp_cond_load_relaxed_timeout()orsmp_cond_load_acquire_timeout()will fail to link with an unresolved reference toarch_timer_evtstrm_available().
smp_cond_load_*_timeout() is a fairly niche interface. Let's not invent hypothetical modules that use this until they actually appear.
Quoted 22 lines
Quoted 5 lines
arch/arm64/include/asm/cmpxchg.h +0
-0[ ... ]
Quoted 16 lines
arch/arm64/include/asm/cmpxchg.h +8
-4@@ -235,20 +236,55 @@ __CMPWAIT_CASE( , , 64);#undef __CMPWAIT_CASE -#define __CMPWAIT_GEN (sfx) \ -static __always_inline void __cmpwait##sfx(volatile void *ptr, \ - unsigned long val, \ - int size) \ +/* Declared locally to avoid recursive include dependency */ +u64 arm64_wfet_deadline (u64 timeout_ns); + +#define __CMPWAIT_TIMEOUT_CASE (w, sfx, sz) \ +static inline void __cmpwait_case_timeout_##sz(volatile void *ptr, \ + unsigned long val, \ + u64 timeout_ns) \ +{ \
Quoted 8 lines
arch/arm64/include/asm/cmpxchg.h __CMPWAIT_CASE( , , 64); · line 248
Quoted 2 lines
arch/arm64/include/asm/cmpxchg.h +2
-0+unsigned long tmp; \ +u64 ecycles =arm64_wfet_deadline (timeout_ns); \[Severity: Medium]
Does callingarm64_wfet_deadline()fromcpu_poll_relax()->__cmpwait_relaxed_timeout()-> __cmpwait_case_timeout_##sz() violate noinstr constraints whensmp_cond_load_relaxed_timeout()is used from cpuidle?
As the previous patch notes, I plan to handle this when adding the poll_idle() user (updated version of [1]).
- note that the eventual intent is to use smp_cond_load_relaxed_timeout() and thus arm64_wfet_deadline() from poll_idle() (which is noinstr). Will do handling for noinstr in arm64_wfet_deadline() (and __delay_cycles() etc) as part of that series.
[1] https://lore.kernel.org/all/[email protected]/
--
ankur
PATCH v17 5/16 arm64: rqspinlock: Remove private copy of smp_cond_load_acquire_timewait()
In reply to Ankur AroraIn 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
@@ -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.43.7
PATCH v17 6/16 asm-generic: barrier: Add smp_cond_load_acquire_timeout()
In reply to Ankur AroraAdd the acquire variant of smp_cond_load_relaxed_timeout().
smp_cond_load_acquire_timeout() does its own error checking but reuses the relaxed logic in __smp_cond_load_relaxed_timeout() for the actual wait.
The success case (no error, no timeout) guarantees that we have evaluated the control-dependency. This means we can provide the additional LOAD->LOAD dependency via smp_acquire__after_ctrl_dep().
In the failure case we provide a full ACQUIRE.
---
Note:
- provide full ACQUIRE ordering for the timeout case instead of guessing around if the control-dependency was evaluated or not..
include/asm-generic/barrier.h | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)
include/asm-generic/barrier.h +52 -0
@@ -385,6 +385,58 @@ do { \ })#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_expr: boolean expression to wait for. +* @time_expr_ns: monotonic expression that evaluates to time in ns or, +* on failure, returns zero or a negative value. +* @timeout_ns: timeout value in ns. A timeout value of zero is treated +* as the minimum wait so we evaluate cond_expr at least once. +* (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) \ +({ \ + typeof(ptr) __PTRA = (ptr); \ + s64 __scl_timeout_ns = (s64)(timeout_ns); \ + __unqual_scalar_typeof(*(ptr)) VAL; \ + bool __scl_timed_out = false; \ + \ + if (unlikely(__scl_timeout_ns < 0)) \ + __scl_timed_out = true; \ + else \ + VAL = __smp_cond_load_relaxed_timeout(__PTRA, \ + cond_expr, \ + time_expr_ns, \ + __scl_timeout_ns, \ + __scl_timed_out); \ + \ +/* \ +* We return after a timeout or, having waited on the control \ +* dependency. In the first case, the control-dependency might \ +* not be present so we provide full ACQUIRE ordering. \ +* \ +* In the second case, cond_expr provides a control-dependency \ +* on VAL, which orders any subsequent STOREs after the LOAD, \ +* taking care of the LOAD->STORE ordering. \ +* \ +* Provide the remaining LOAD->LOAD ordering. \ +*/ \ +if (unlikely (__scl_timed_out)) \ +VAL =smp_load_acquire (__PTRA); \ +else \ +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.43.7
PATCH v17 7/16 atomic: Add atomic_cond_read_*_timeout()
In reply to Ankur AroraAdd atomic load wrappers, atomic_cond_read_*_timeout() and atomic64_cond_read_*_timeout() for the cond-load timeout interfaces.
Also add a short description for the atomic_cond_read_{relaxed,acquire}(), and the atomic_cond_read_{relaxed,acquire}_timeout() interfaces.
--- Documentation/atomic_t.txt | 14 +++++++++----- include/linux/atomic.h | 10 ++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-)
Documentation/atomic_t.txt +9 -5
@@ -16,6 +16,10 @@ Non-RMW ops: atomic_read(), atomic_set() atomic_read_acquire(), atomic_set_release() +Non-RMW, non-atomic_t ops: + + atomic_cond_read_{relaxed,acquire}() + atomic_cond_read_{relaxed,acquire}_timeout() RMW atomic operations:
@@ -79,11 +83,11 @@ SEMANTICS Non-RMW ops: -The non-RMW ops are (typically) regular LOADs and STOREs and are canonically -implemented using READ_ONCE(), WRITE_ONCE(), smp_load_acquire() and -smp_store_release() respectively. Therefore, if you find yourself only using -the Non-RMW operations of atomic_t, you do not in fact need atomic_t at all -and are doing it wrong. +The non-RMW ops are (typically) regular, or conditional LOADs and STOREs and +are canonically implemented using READ_ONCE(), WRITE_ONCE(), +smp_load_acquire() and smp_store_release() respectively. Therefore, if you +find yourself only using the Non-RMW operations of atomic_t, you do not in +fact need atomic_t at all and are doing it wrong. A note for the implementation of atomic_set{}() is that it must not break the atomicity of the RMW ops. That is:
include/linux/atomic.h +10 -0
@@ -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.43.7
[email protected]
AI reviewRe: [PATCH v17 07/16] atomic: Add atomic_cond_read_*_timeout()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] Documentation/atomic_t: inaccurate non-atomic_t and conditional store text --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] Documentation/atomic_t: inaccurate non-atomic_t and conditional store text
--
commit d324a5d412956ca2cd74e953ba1e9d33ac409b28
Author: Ankur Arora <[email protected]>
atomic: Add atomic_cond_read_*_timeout()
This commit adds atomic_cond_read_{acquire,relaxed}_timeout() and atomic64_cond_read_{acquire,relaxed}_timeout() wrappers around the smp_cond_load_*_timeout() helpers, and documents the conditional read interfaces in Documentation/atomic_t.txt.
Quoted 19 lines
Documentation/atomic_t.txt +4
-0@@ -16,6 +16,10 @@ Non-RMW ops: atomic_read(), atomic_set() atomic_read_acquire(), atomic_set_release() +Non-RMW, non-atomic_t ops: + + atomic_cond_read_{relaxed,acquire}() + atomic_cond_read_{relaxed,acquire}_timeout() RMW atomic operations:@@ -79,11 +83,11 @@ SEMANTICS Non-RMW ops:
Documentation/atomic_t.txt SEMANTICS · line 90
Quoted 10 lines
Documentation/atomic_t.txt +5
-5-The non-RMW ops are (typically) regular LOADs and STOREs and are canonically -implemented using READ_ONCE(), WRITE_ONCE(), smp_load_acquire() and -smp_store_release() respectively. Therefore, if you find yourself only using -the Non-RMW operations of atomic_t, you do not in fact need atomic_t at all -and are doing it wrong. +The non-RMW ops are (typically) regular, or conditional LOADs and STOREs and +are canonically implemented using READ_ONCE(), WRITE_ONCE(), +smp_load_acquire() and smp_store_release() respectively. Therefore, if you +find yourself only using the Non-RMW operations of atomic_t, you do not in +fact need atomic_t at all and are doing it wrong.
[Severity: Low]
Are these two documentation updates accurate?
First, atomic_cond_read_{relaxed,acquire}() and atomic_cond_read_{relaxed,acquire}_timeout() take an atomic_t pointer and dereference &(v)->counter, so labeling them "non-atomic_t ops" seems misleading (the underlying non-atomic_t primitives are smp_cond_load_{relaxed,acquire}() and smp_cond_load_{relaxed,acquire}_timeout()).
Second, in the semantics paragraph, there are no conditional non-RMW store operations, and inserting "or conditional" breaks the "respectively" mapping between the four regular non-RMW operations and READ_ONCE(), WRITE_ONCE(), smp_load_acquire(), and smp_store_release() while omitting smp_cond_load_*().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v17 8/16 locking/atomic: scripts: build atomic_long_cond_read_*_timeout()
In reply to Ankur AroraAdd the atomic long wrappers for the cond-load timeout interfaces.
--- 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
@@ -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 */ -// 4b882bf19018602c10816c52f8b4ae280adc887b +// 79c1f4acb5774376ceed559843d5d9ed1348df99
scripts/atomic/gen-atomic-long.sh +10 -6
@@ -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.43.7
PATCH v17 9/16 bpf/rqspinlock: switch check_timeout() to a clock interface
In reply to Ankur Aroracheck_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 clock_deadlock(), a clock style interface, which 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.
Also adjust the RES_CHECK_TIMEOUT macro to discard the clock value when updating the explicit return status.
--- kernel/bpf/rqspinlock.c | 45 +++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 15 deletions(-)
kernel/bpf/rqspinlock.c +29 -15
@@ -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,14 @@ 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; }/*
@@ -231,15 +238,22 @@ static noinline int check_timeout(rqspinlock_t *lock, u32 mask,* as the macro does internal amortization for us. */ #ifndef res_smp_cond_load_acquire -#define RES_CHECK_TIMEOUT (ts, ret, mask) \ - ({ \ - if (!(ts).spin++) \ - (ret) = check_timeout((lock), (mask), &(ts)); \ - (ret); \ +#define RES_CHECK_TIMEOUT (ts, ret, mask) \ + ({ \ + s64 __timeval_err = 0; \ + if (!(ts).spin++) \ + __timeval_err = clock_deadlock((lock), (mask), &(ts)); \ + (ret) = __timeval_err < 0 ? __timeval_err : 0; \ + __timeval_err; \ })#else -#define RES_CHECK_TIMEOUT (ts, ret, mask) \ - ({ (ret) = check_timeout((lock), (mask), &(ts)); }) +#define RES_CHECK_TIMEOUT (ts, ret, mask) \ + ({ \ + s64 __timeval_err; \ + __timeval_err = clock_deadlock((lock), (mask), &(ts)); \ + (ret) = __timeval_err < 0 ? __timeval_err : 0; \ + __timeval_err; \ + })#endif /*
@@ -281,7 +295,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;
@@ -406,7 +420,7 @@ 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, ret, _Q_LOCKED_MASK) <0 ); }if (ret) {
@@ -568,7 +582,7 @@ 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, ret, _Q_LOCKED_PENDING_MASK) <0 );/* Disable queue destruction when we detect deadlocks. */ if (ret == -EDEADLK ) {
Signature · 1 line
2.43.7
PATCH v17 10/16 bpf/rqspinlock: Use smp_cond_load_acquire_timeout()
In reply to Ankur AroraSwitch out the conditional load interfaces used by rqspinlock to smp_cond_load_acquire_timeout() and its wrapper, atomic_cond_read_acquire_timeout().
Both these handle the timeout and amortize as needed, so use the non-amortized RES_CHECK_TIMEOUT.
RES_CHECK_TIMEOUT does double duty here -- presenting the current clock value, the timeout/deadlock error from clock_deadlock() to the cond-load and, returning the error value via ret.
For correctness, we need to ensure that the error case of the cond-load interface always agrees with that in clock_deadlock().
For the most part, this is fine because there's no independent clock, or double reads from the clock in cond-load -- either of which could lead to its internal state going 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:
if (__scl_time_now <= 0 ||
__scl_time_end <= __scl_time_now) {
timed_out = true;
...This runs into a problem when (__scl_time_end == __scl_time_now), which clock_deadlock() does not treat as a timeout condition but the second clause in the conditional above does. So, make clock_deadlock() check for equality as well.
Finally, redefine SMP_TIMEOUT_POLL_COUNT to be 16k to be similar to the spin-count used in the amortized version. We only do this for architectures without a waiting implementation of cpu_poll_relax().
--- kernel/bpf/rqspinlock.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-)
kernel/bpf/rqspinlock.c +24 -17
@@ -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,11 +235,10 @@ 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. +* Spin amortized version of RES_CHECK_TIMEOUT. Used when busy-waiting in +* atomic_try_cmpxchg(). */ -#ifndef res_smp_cond_load_acquire -#define RES_CHECK_TIMEOUT (ts, ret, mask) \ +#define RES_CHECK_TIMEOUT_AMORTIZED (ts, ret, mask) \ ({ \ s64 __timeval_err = 0; \ if (!(ts).spin++) \
@@ -247,7 +246,7 @@ static noinline s64 clock_deadlock(rqspinlock_t *lock, u32 mask, (ret) = __timeval_err <0 ? __timeval_err :0 ; \ __timeval_err; \ }) -#else +#define RES_CHECK_TIMEOUT (ts, ret, mask) \ ({ \ s64 __timeval_err; \
@@ -255,7 +254,6 @@ static noinline s64 clock_deadlock(rqspinlock_t *lock, u32 mask, (ret) = __timeval_err <0 ? __timeval_err :0 ; \ __timeval_err; \ }) -#endif /* * Initialize the 'spin' member.
@@ -269,6 +267,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(). +* +* We only override the default value for architectures without a waiting +* implementation. +*/ +#ifndef CPU_POLL_RELAX_WAITS +#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.
@@ -296,7 +306,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 ) <0 ) +if (RES_CHECK_TIMEOUT_AMORTIZED (ts, ret, ~0u ) <0 ) goto out;cpu_relax (); goto retry;
@@ -319,12 +329,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
@@ -421,7 +425,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, ret, _Q_LOCKED_MASK) <0 ); +smp_cond_load_acquire_timeout (&lock->locked, !VAL , +RES_CHECK_TIMEOUT (ts, ret, _Q_LOCKED_MASK), + ts.duration); }if (ret) {
@@ -582,8 +588,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, ret, _Q_LOCKED_PENDING_MASK) <0 ); + val =atomic_cond_read_acquire_timeout (&lock->val, !(VAL & _Q_LOCKED_PENDING_MASK), +RES_CHECK_TIMEOUT (ts, ret, _Q_LOCKED_PENDING_MASK), + ts.duration);/* Disable queue destruction when we detect deadlocks. */ if (ret == -EDEADLK ) {
Signature · 1 line
2.43.7
PATCH v17 11/16 sched: add need-resched timed wait interface
In reply to Ankur AroraAdd 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_relaxed_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.
--- include/linux/sched/idle.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
include/linux/sched/idle.h +29 -0
@@ -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 __always_inline bool tif_bitset_relaxed_wait(int tif, u64 timeout_ns) +{ +unsigned long flags; + + flags =smp_cond_load_relaxed_timeout (¤t_thread_info ()->flags, + (VAL &BIT (tif)), +local_clock_noinstr (), + timeout_ns); +return flags &BIT (tif); +} + +/** +* tif_need_resched_relaxed_wait() - Wait for need-resched being set +* with no ordering guarantees until a timeout expires. +* +* @timeout_ns: timeout value. +*/ +static __always_inline bool tif_need_resched_relaxed_wait(u64 timeout_ns) +{ +return tif_bitset_relaxed_wait (TIF_NEED_RESCHED , timeout_ns); +} +#endif /* _LINUX_SCHED_IDLE_H */
Signature · 1 line
2.43.7
PATCH v17 12/16 cpuidle/poll_state: Wait for need-resched via tif_need_resched_relaxed_wait()
In reply to Ankur AroraThe 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, since we want to minimize power consumption in idle, building of cpuidle/poll_state.c continues 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_relaxed_timeout()).
--- drivers/cpuidle/poll_state.c | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-)
drivers/cpuidle/poll_state.c +1 -20
@@ -6,41 +6,22 @@#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; limit =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 ; - } - } + dev->poll_time_limit = !tif_need_resched_relaxed_wait (limit); }raw_local_irq_disable ();
Signature · 1 line
2.43.7
PATCH v17 13/16 arm64/delay: enable testing smp_cond_load_relaxed_timeout()
In reply to Ankur AroraMake the symbol arch_timer_evtstrm_available available to kunit. This enables the barrier tests to be built as a module.
--- drivers/clocksource/arm_arch_timer.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/clocksource/arm_arch_timer.c +2 -0
@@ -28,6 +28,7 @@#include <linux/acpi.h> #include <linux/arm-smccc.h> #include <linux/ptp_kvm.h> +#include <kunit/visibility.h> #include <asm/arch_timer.h> #include <asm/virt.h>
@@ -893,6 +894,7 @@ bool noinstr arch_timer_evtstrm_available(void)*/ return cpumask_test_cpu (raw_smp_processor_id (), &evtstrm_available); } +EXPORT_SYMBOL_IF_KUNIT (arch_timer_evtstrm_available);static struct arch_timer_kvm_info arch_timer_kvm_info;
Signature · 1 line
2.43.7
PATCH v17 14/16 barrier: add tests for smp_cond_load_*_timeout()
In reply to Ankur AroraAdd success and failure case tests for smp_cond_load_*_timeout().
Success or failure cases depend on the expected bit being set (or not). Additionally in failure cases smp_cond_load_*_timeout() cannot return before timeout.
---
Notes:
- rename barrier-timeout-test.c to barrier_timeout_kunit.c
lib/Kconfig.debug | 10 +++ lib/tests/Makefile | 1 + lib/tests/barrier_timeout_kunit.c | 129 ++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 lib/tests/barrier_timeout_kunit.c
lib/Kconfig.debug +10 -0
@@ -2556,6 +2556,16 @@ config FFS_KUNIT_TEST For more information on KUnit and unit tests in general, please refer to Documentation/dev-tools/kunit/. +config BARRIER_TIMEOUT_KUNIT_TEST + tristate "KUnit tests for smp_cond_load_*_timeout()" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Builds KUnit tests that validate wake-up and timeout handling paths in + smp_cond_load_relaxed_timeout() and smp_cond_load_acquire_timeout(). + + If unsure, say N. + config TEST_KSTRTOX tristate "Test kstrto*() family of functions at runtime"
lib/tests/Makefile +1 -0
@@ -60,3 +60,4 @@ obj-$(CONFIG_RATELIMIT_KUNIT_TEST) += test_ratelimit.o obj-$(CONFIG_UUID_KUNIT_TEST) += uuid_kunit.o obj-$(CONFIG_TEST_RUNTIME_MODULE) += module/ +obj-$(CONFIG_BARRIER_TIMEOUT_KUNIT_TEST) += barrier_timeout_kunit.o
lib/tests/barrier_timeout_kunit.c new file +129 -0
129 lines not on this page · raw message
Signature · 1 line
2.43.7
PATCH v17 15/16 barrier: timeout validity checks for smp_cond_load_relaxed_timeout()
In reply to Ankur AroraAdd timeout tests for smp_cond_load_relaxed_timeout(). These check that the implementation returns early on invalid timeout values and handles edge cases sanely.
--- lib/tests/barrier_timeout_kunit.c | 112 ++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+)
lib/tests/barrier_timeout_kunit.c +112 -0
@@ -19,6 +19,8 @@ MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");struct clock_state {s64 start_time;s64 end_time; +s64 extra; +u32 niters; };#define TIMEOUT_MSEC 2
@@ -113,8 +115,118 @@ static void test_smp_cond_timeout(struct kunit *test)KUNIT_EXPECT_GE (test, runtime, timeout_ns); } +static s64 synthetic_clock (struct clock_state *clk) +{ + clk->end_time += clk->extra; + clk->niters++; + +return clk->end_time; +} + +struct smp_cond_expiry_params { +char *desc; +u64 timeout_ns; +s64 clk_unit; +s64 miniters; +s64 maxiters; +}; + +static const struct smp_cond_expiry_params expiry_params_list[] = { +/* timeout_ns is invalid/out-of-range */ + { .clk_unit =0 , .timeout_ns =-1LL , .miniters =-1 , .maxiters =0 , .desc ="invalid (-1LL)" , }, + { .clk_unit =0 , .timeout_ns = ~0ULL , .miniters =-1 , .maxiters =0 , .desc ="invalid (~0ULL)" , }, + { .clk_unit =0 , .timeout_ns =S64_MAX +1ULL , .miniters =-1 , .maxiters =0 , .desc ="out-of-range (S64_MAX+1)" , }, + { .clk_unit =0 , .timeout_ns =U64_MAX , .miniters =-1 , .maxiters =0 , .desc ="out-of-range (U64_MAX)" , }, + { .clk_unit =0 , .timeout_ns =0 , .miniters =-1 , .maxiters =1 , .desc ="degenerate (0)" , }, + +/* timeout_ns is valid */ + { .clk_unit = (0x1ULL <<28 ), .timeout_ns =1 , .miniters =1 , .maxiters =-1 , .desc ="1" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns = (0x1ULL <<30 ), .miniters =1 << (30 -28 ), .maxiters =-1 , .desc ="1<<30" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns =S32_MAX , .miniters =1 << (31 -28 ), .maxiters =-1 , .desc ="S32_MAX" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns =U32_MAX , .miniters =1 << (32 -28 ), .maxiters =-1 , .desc ="U32_MAX" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns = (0x1ULL <<33 ), .miniters =1 << (33 -28 ), .maxiters =-1 , .desc ="1<<33" , }, +}; + +static void expiry_param_to_desc (const struct smp_cond_expiry_params *p,char *desc) +{ +char iters[32 ] ="" ; + +if (p->miniters !=-1 ) +snprintf (iters,32 ,">= 0x%llx" , (u64 )p->miniters); +else if (p->maxiters !=-1 ) +snprintf (iters,32 ,"%s 0x%llx" , p->maxiters ==0 ?"==" :"<=" , (u64 )p->maxiters); + +snprintf (desc,KUNIT_PARAM_DESC_SIZE , +"smp_cond_*_timeout: clock=%s, timeout=%s, iterations %s" , +"synthetic" , p->desc, iters); +} + +static char *expiry_skip_test (const struct smp_cond_expiry_params *p) +{ +#ifdef CONFIG_ARM64 + +/* +* WFXT waits for the full p->timeout_ns so limit it to a reasonable +* duration. +*/ +if (alternative_has_cap_unlikely (ARM64_HAS_WFXT ) && + p->timeout_ns >=NSEC_PER_SEC ) { + +return "runtime duration too long" ; + } +#endif +return NULL ; +} + +static void test_smp_cond_relaxed (struct kunit *test) +{ +const struct smp_cond_expiry_params *p = test->param_value; +struct clock_state clk = { + .start_time =0 , + .end_time =0 , + .extra = p->clk_unit, + .niters =0 , + }; +unsigned int result; +s64 runtime; +char *reason; + + reason =expiry_skip_test (p); +if (reason) +kunit_skip (test,"%s" , reason); + + scu_flag =0 ; + +preempt_disable (); + result =smp_cond_load_relaxed_timeout (&scu_flag, +0 , +synthetic_clock (&clk), + p->timeout_ns); +preempt_enable (); + + runtime = (u64 )clk.end_time - (u64 )clk.start_time; + +KUNIT_EXPECT_EQ (test, result, scu_flag); +/* +* Check if we do the expected number of iterations. +*/ +if (p->miniters !=-1 ) +KUNIT_EXPECT_GE (test, clk.niters, p->miniters); +if (p->maxiters !=-1 ) +KUNIT_EXPECT_LE (test, clk.niters, p->maxiters); + +/* +* maxiters == 0 means that the timeout is invalid/out-of-range. +* When not, we cannot return with runtime < timeout_ns. +*/ +if (p->maxiters !=0 ) +KUNIT_EXPECT_GE (test, runtime, p->timeout_ns); +} + +KUNIT_ARRAY_PARAM (smp_cond_expiry_params, expiry_params_list, expiry_param_to_desc);static struct kunit_case barrier_timeout_test_cases[] = {KUNIT_CASE_PARAM (test_smp_cond_timeout, smp_cond_update_params_gen_params), +KUNIT_CASE_PARAM (test_smp_cond_relaxed, smp_cond_expiry_params_gen_params), {} };
Signature · 1 line
2.43.7
[email protected]
AI reviewRe: [PATCH v17 15/16] barrier: timeout validity checks for smp_cond_load_relaxed_timeout()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] kunit: barrier: expiry_skip_test() skips invalid timeouts on arm64 WFXT --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] kunit: barrier: expiry_skip_test() skips invalid timeouts on arm64 WFXT
--
commit b346a6c0c892c3f1d3df9f9238d93e9e4a6dba04
Author: Ankur Arora <[email protected]>
barrier: timeout validity checks for smp_cond_load_relaxed_timeout()
This commit adds KUnit parameterized tests to verify how smp_cond_load_relaxed_timeout() handles invalid, out-of-range, and edge-case timeout values using a synthetic clock.
Quoted 4 lines
lib/tests/barrier_timeout_kunit.c +0
-0
[ ... ]
Quoted 35 lines
lib/tests/barrier_timeout_kunit.c +31
-0@@ -113,8 +115,118 @@ static void test_smp_cond_timeout(struct kunit *test)KUNIT_EXPECT_GE (test, runtime, timeout_ns); } +static s64 synthetic_clock (struct clock_state *clk) +{ + clk->end_time += clk->extra; + clk->niters++; + +return clk->end_time; +} + +struct smp_cond_expiry_params { +char *desc; +u64 timeout_ns; +s64 clk_unit; +s64 miniters; +s64 maxiters; +}; + +static const struct smp_cond_expiry_params expiry_params_list[] = { +/* timeout_ns is invalid/out-of-range */ + { .clk_unit =0 , .timeout_ns =-1LL , .miniters =-1 , .maxiters =0 , .desc ="invalid (-1LL)" , }, + { .clk_unit =0 , .timeout_ns = ~0ULL , .miniters =-1 , .maxiters =0 , .desc ="invalid (~0ULL)" , }, + { .clk_unit =0 , .timeout_ns =S64_MAX +1ULL , .miniters =-1 , .maxiters =0 , .desc ="out-of-range (S64_MAX+1)" , }, + { .clk_unit =0 , .timeout_ns =U64_MAX , .miniters =-1 , .maxiters =0 , .desc ="out-of-range (U64_MAX)" , }, + { .clk_unit =0 , .timeout_ns =0 , .miniters =-1 , .maxiters =1 , .desc ="degenerate (0)" , }, + +/* timeout_ns is valid */ + { .clk_unit = (0x1ULL <<28 ), .timeout_ns =1 , .miniters =1 , .maxiters =-1 , .desc ="1" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns = (0x1ULL <<30 ), .miniters =1 << (30 -28 ), .maxiters =-1 , .desc ="1<<30" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns =S32_MAX , .miniters =1 << (31 -28 ), .maxiters =-1 , .desc ="S32_MAX" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns =U32_MAX , .miniters =1 << (32 -28 ), .maxiters =-1 , .desc ="U32_MAX" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns = (0x1ULL <<33 ), .miniters =1 << (33 -28 ), .maxiters =-1 , .desc ="1<<33" , }, +};
[ ... ]
Quoted 6 lines
lib/tests/barrier_timeout_kunit.c +6
-0+static char *expiry_skip_test (const struct smp_cond_expiry_params *p) +{ +#ifdef CONFIG_ARM64 + +/* +* WFXT waits for the full p->timeout_ns so limit it to a reasonable
lib/tests/barrier_timeout_kunit.c static void test_smp_cond_timeout(struct kunit *test) · line 179
Quoted 10 lines
lib/tests/barrier_timeout_kunit.c +10
-0+* duration. +*/ +if (alternative_has_cap_unlikely (ARM64_HAS_WFXT ) && + p->timeout_ns >=NSEC_PER_SEC ) { + +return "runtime duration too long" ; + } +#endif +return NULL ; +}
[Severity: Low]
Does expiry_skip_test() skip the invalid and out-of-range test cases on arm64 systems with ARM64_HAS_WFXT?
Because p->timeout_ns is a u64, values such as -1LL, ~0ULL, S64_MAX+1ULL, and U64_MAX all compare >= NSEC_PER_SEC in an unsigned comparison, even though smp_cond_load_relaxed_timeout() casts timeout_ns to s64 and returns immediately without entering the WFXT wait loop. Casting p->timeout_ns to s64 here (or checking p->maxiters != 0) would keep coverage for those test cases on WFXT-capable hardware.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
Re: [PATCH v17 15/16] barrier: timeout validity checks for smp_cond_load_relaxed_timeout()
Quoted 81 lines · [email protected] writes:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] kunit: barrier:expiry_skip_test()skips invalid timeouts on arm64 WFXT
--commit
b346a6c0c892c3f1d3df9f9238d93e9e4a6dba04
Author: Ankur Arora <[email protected]>barrier: timeout validity checks for
smp_cond_load_relaxed_timeout()This commit adds KUnit parameterized tests to verify how
smp_cond_load_relaxed_timeout()handles invalid, out-of-range, and edge-case timeout values using a synthetic clock.Quoted 4 lines
lib/tests/barrier_timeout_kunit.c +0
-0[ ... ]
Quoted 35 lines
lib/tests/barrier_timeout_kunit.c +31
-0@@ -113,8 +115,118 @@ static void test_smp_cond_timeout(struct kunit *test)KUNIT_EXPECT_GE (test, runtime, timeout_ns); } +static s64 synthetic_clock (struct clock_state *clk) +{ + clk->end_time += clk->extra; + clk->niters++; + +return clk->end_time; +} + +struct smp_cond_expiry_params { +char *desc; +u64 timeout_ns; +s64 clk_unit; +s64 miniters; +s64 maxiters; +}; + +static const struct smp_cond_expiry_params expiry_params_list[] = { +/* timeout_ns is invalid/out-of-range */ + { .clk_unit =0 , .timeout_ns =-1LL , .miniters =-1 , .maxiters =0 , .desc ="invalid (-1LL)" , }, + { .clk_unit =0 , .timeout_ns = ~0ULL , .miniters =-1 , .maxiters =0 , .desc ="invalid (~0ULL)" , }, + { .clk_unit =0 , .timeout_ns =S64_MAX +1ULL , .miniters =-1 , .maxiters =0 , .desc ="out-of-range (S64_MAX+1)" , }, + { .clk_unit =0 , .timeout_ns =U64_MAX , .miniters =-1 , .maxiters =0 , .desc ="out-of-range (U64_MAX)" , }, + { .clk_unit =0 , .timeout_ns =0 , .miniters =-1 , .maxiters =1 , .desc ="degenerate (0)" , }, + +/* timeout_ns is valid */ + { .clk_unit = (0x1ULL <<28 ), .timeout_ns =1 , .miniters =1 , .maxiters =-1 , .desc ="1" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns = (0x1ULL <<30 ), .miniters =1 << (30 -28 ), .maxiters =-1 , .desc ="1<<30" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns =S32_MAX , .miniters =1 << (31 -28 ), .maxiters =-1 , .desc ="S32_MAX" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns =U32_MAX , .miniters =1 << (32 -28 ), .maxiters =-1 , .desc ="U32_MAX" , }, + { .clk_unit = (0x1ULL <<28 ), .timeout_ns = (0x1ULL <<33 ), .miniters =1 << (33 -28 ), .maxiters =-1 , .desc ="1<<33" , }, +};[ ... ]
Quoted 6 lines
lib/tests/barrier_timeout_kunit.c +6
-0+static char *expiry_skip_test (const struct smp_cond_expiry_params *p) +{ +#ifdef CONFIG_ARM64 + +/* +* WFXT waits for the full p->timeout_ns so limit it to a reasonable lib/tests/barrier_timeout_kunit.c static void test_smp_cond_timeout(struct kunit *test) · line 179
Quoted 10 lines
lib/tests/barrier_timeout_kunit.c +10
-0+* duration. +*/ +if (alternative_has_cap_unlikely (ARM64_HAS_WFXT ) && + p->timeout_ns >=NSEC_PER_SEC ) { + +return "runtime duration too long" ; + } +#endif +return NULL ; +}[Severity: Low]
Doesexpiry_skip_test()skip the invalid and out-of-range test cases on arm64 systems withARM64_HAS_WFXT?
Quoted 6 lines
Because
p->timeout_nsis a u64, values such as -1LL, ~0ULL, S64_MAX+1ULL, andU64_MAXall compare >=NSEC_PER_SECin an unsigned comparison, even thoughsmp_cond_load_relaxed_timeout()caststimeout_nsto s64 and returns immediately without entering the WFXT wait loop. Castingp->timeout_nsto s64 here (or checkingp->maxiters!= 0) would keep coverage for those test cases on WFXT-capable hardware.
There's no clean way to test with synthetic timeouts with WFET: unlike when running with WFE, it waits for the specified duration.
In any case, the loss is not great: we test with WFET for small timeout values. And, a fairly similar path is taken when the test runs with WFE.
--
ankur
PATCH v17 16/16 barrier: timeout validity checks for smp_cond_load_acquire_timeout()
In reply to Ankur AroraExtend timeout tests in smp_cond_expiry_params to also test smp_cond_load_acquire_timeout().
The interface supports clocks that can return error on timeout. These are used by rqspinlock. Test that by using a similar clock that starts returning error past timeout.
--- lib/tests/barrier_timeout_kunit.c | 58 ++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-)
lib/tests/barrier_timeout_kunit.c +57 -1
@@ -158,7 +158,7 @@ static void expiry_param_to_desc(const struct smp_cond_expiry_params *p, char *dsnprintf (desc,KUNIT_PARAM_DESC_SIZE ,"smp_cond_*_timeout: clock=%s, timeout=%s, iterations %s" , -"synthetic" , p->desc, iters); +"synthetic/failing" , p->desc, iters); }static char *expiry_skip_test (const struct smp_cond_expiry_params *p)
@@ -223,10 +223,66 @@ static void test_smp_cond_relaxed(struct kunit *test)KUNIT_EXPECT_GE (test, runtime, p->timeout_ns); } +/* +* Use a clock similar to the one used in rqspinlock where it start +* returning failure value once it goes beyond the timeout. +*/ +static s64 failing_clock (const struct smp_cond_expiry_params *p, +struct clock_state *clk) +{ +if ((u64 )clk->end_time >= (u64 )p->timeout_ns) +return -ETIMEDOUT ; + + clk->end_time += clk->extra; + clk->niters++; + +return clk->end_time; +} + +static void test_smp_cond_acquire_error (struct kunit *test) +{ +const struct smp_cond_expiry_params *p = test->param_value; +struct clock_state clk = { + .start_time =0 , + .end_time =0 , + .extra = p->clk_unit, + .niters =0 , + }; +unsigned int result; +s64 ret = -ETIMEDOUT ; +char *reason; + + reason =expiry_skip_test (p); +if (reason) +kunit_skip (test,"%s" , reason); + + scu_flag =0 ; + +preempt_disable (); + result =smp_cond_load_acquire_timeout (&scu_flag, +0 , + (ret =failing_clock (p, &clk)), + p->timeout_ns); +preempt_enable (); + +KUNIT_EXPECT_EQ (test, result, scu_flag); +if (p->miniters !=-1 ) +KUNIT_EXPECT_GE (test, clk.niters, p->miniters); +if (p->maxiters !=-1 ) +KUNIT_EXPECT_LE (test, clk.niters, p->maxiters); + +/* +* scu_flag is never updated so the clock should eventually +* hit the timeout and error out. +*/ +KUNIT_EXPECT_EQ (test, ret, -ETIMEDOUT ); +} +KUNIT_ARRAY_PARAM (smp_cond_expiry_params, expiry_params_list, expiry_param_to_desc);static struct kunit_case barrier_timeout_test_cases[] = {KUNIT_CASE_PARAM (test_smp_cond_timeout, smp_cond_update_params_gen_params),KUNIT_CASE_PARAM (test_smp_cond_relaxed, smp_cond_expiry_params_gen_params), +KUNIT_CASE_PARAM (test_smp_cond_acquire_error, smp_cond_expiry_params_gen_params), {} };
Signature · 1 line
2.43.7