locking/generic: Add generic support for sync_try_cmpxchg and its falback
Subsystems (3)
PATCH 1/3 locking/generic: Add generic support for sync_try_cmpxchg and its falback
Provide the generic sync_try_cmpxchg function from the raw_ prefixed version, also adding explicit instrumentation.
Cc Will Deacon <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Mark Rutland <[email protected]>
Signed-off-by Uros Bizjak <[email protected]>
--- include/linux/atomic/atomic-arch-fallback.h | 15 +++++++++- include/linux/atomic/atomic-instrumented.h | 10 ++++++- scripts/atomic/gen-atomic-fallback.sh | 33 +++++++++++---------- scripts/atomic/gen-atomic-instrumented.sh | 3 +- 4 files changed, 43 insertions(+), 18 deletions(-)
include/linux/atomic/atomic-arch-fallback.h +14 -1
@@ -428,6 +428,19 @@ extern void raw_cmpxchg128_relaxed_not_implemented(void);#define raw_sync_cmpxchg arch_sync_cmpxchg +#ifdef arch_sync_try_cmpxchg +#define raw_sync_try_cmpxchg arch_sync_try_cmpxchg +#else +#define raw_sync_try_cmpxchg (_ptr, _oldp, _new) \ +({ \ + typeof(*(_ptr)) *___op = (_oldp), ___o = *___op, ___r; \ + ___r = raw_sync_cmpxchg((_ptr), ___o, (_new)); \ + if (unlikely(___r != ___o)) \ + *___op = ___r; \ + likely(___r == ___o); \ +}) +#endif +/** * raw_atomic_read() - atomic load with relaxed ordering * @v: pointer to atomic_t
@@ -4657,4 +4670,4 @@ raw_atomic64_dec_if_positive(atomic64_t *v) }#endif /* _LINUX_ATOMIC_FALLBACK_H */ -// 202b45c7db600ce36198eb1f1fc2c2d5268ace2d +// 2eb1fe994b6dcab645bbb5a642a4aaa9374c5600
include/linux/atomic/atomic-instrumented.h +9 -1
@@ -4998,6 +4998,14 @@ atomic_long_dec_if_positive(atomic_long_t *v)raw_try_cmpxchg128_local (__ai_ptr, __ai_oldp, __VA_ARGS__); \ }) +#define sync_try_cmpxchg (ptr, ...) \ +({ \ + typeof(ptr) __ai_ptr = (ptr); \ + kcsan_mb(); \ + instrument_atomic_read_write(__ai_ptr, sizeof(*__ai_ptr)); \ + raw_sync_try_cmpxchg(__ai_ptr, __VA_ARGS__); \ +}) +#endif /* _LINUX_ATOMIC_INSTRUMENTED_H */ -// 1568f875fef72097413caab8339120c065a39aa4 +// 2cc4bc990fef44d3836ec108f11b610f3f438184
scripts/atomic/gen-atomic-fallback.sh +18 -15
@@ -223,14 +223,15 @@ gen_xchg_fallbacks() gen_try_cmpxchg_fallback() { + local prefix="$1"; shift local cmpxchg="$1"; shift; - local order="$1"; shift; + local suffix="$1"; shift; cat <<EOF -#define raw_try_${cmpxchg}${order}(_ptr, _oldp, _new) \\ +#define raw_${prefix}try_${cmpxchg}${suffix}(_ptr, _oldp, _new) \\ ({ \\ typeof(*(_ptr)) *___op = (_oldp), ___o = *___op, ___r; \\ - ___r = raw_${cmpxchg}${order}((_ptr), ___o, (_new)); \\ + ___r = raw_${prefix}${cmpxchg}${suffix}((_ptr), ___o, (_new)); \\ if (unlikely(___r != ___o)) \\ *___op = ___r; \\ likely(___r == ___o); \\
@@ -259,11 +260,11 @@ gen_try_cmpxchg_order_fallback() fi printf "#else\n" - gen_try_cmpxchg_fallback "${cmpxchg}" "${order}" + gen_try_cmpxchg_fallback "" "${cmpxchg}" "${order}" printf "#endif\n\n" } -gen_try_cmpxchg_fallbacks() +gen_try_cmpxchg_order_fallbacks() { local cmpxchg="$1"; shift;
@@ -272,15 +273,17 @@ gen_try_cmpxchg_fallbacks() done } -gen_cmpxchg_local_fallbacks() +gen_def_and_try_cmpxchg_fallback() { + local prefix="$1"; shift local cmpxchg="$1"; shift + local suffix="$1"; shift - printf "#define raw_${cmpxchg} arch_${cmpxchg}\n\n" - printf "#ifdef arch_try_${cmpxchg}\n" - printf "#define raw_try_${cmpxchg} arch_try_${cmpxchg}\n" + printf "#define raw_${prefix}${cmpxchg}${suffix} arch_${prefix}${cmpxchg}${suffix}\n\n" + printf "#ifdef arch_${prefix}try_${cmpxchg}${suffix}\n" + printf "#define raw_${prefix}try_${cmpxchg}${suffix} arch_${prefix}try_${cmpxchg}${suffix}\n" printf "#else\n" - gen_try_cmpxchg_fallback "${cmpxchg}" "" + gen_try_cmpxchg_fallback "${prefix}" "${cmpxchg}" "${suffix}" printf "#endif\n\n" }
@@ -302,15 +305,15 @@ for xchg in "xchg" "cmpxchg" "cmpxchg64" "cmpxchg128"; do done for cmpxchg in "cmpxchg" "cmpxchg64" "cmpxchg128"; do - gen_try_cmpxchg_fallbacks "${cmpxchg}" + gen_try_cmpxchg_order_fallbacks "${cmpxchg}" done -for cmpxchg in "cmpxchg_local" "cmpxchg64_local" "cmpxchg128_local"; do - gen_cmpxchg_local_fallbacks "${cmpxchg}" "" +for cmpxchg in "cmpxchg" "cmpxchg64" "cmpxchg128"; do + gen_def_and_try_cmpxchg_fallback "" "${cmpxchg}" "_local" done -for cmpxchg in "sync_cmpxchg"; do - printf "#define raw_${cmpxchg} arch_${cmpxchg}\n\n" +for cmpxchg in "cmpxchg"; do + gen_def_and_try_cmpxchg_fallback "sync_" "${cmpxchg}" "" done grep '^[a-z]' "$1" | while read name meta args; do
scripts/atomic/gen-atomic-instrumented.sh +2 -1
@@ -169,7 +169,8 @@ for xchg in "xchg" "cmpxchg" "cmpxchg64" "cmpxchg128" "try_cmpxchg" "try_cmpxchg done done -for xchg in "cmpxchg_local" "cmpxchg64_local" "cmpxchg128_local" "sync_cmpxchg" "try_cmpxchg_local" "try_cmpxchg64_local" "try_cmpxchg128_local"; do +for xchg in "cmpxchg_local" "cmpxchg64_local" "cmpxchg128_local" "sync_cmpxchg" \ + "try_cmpxchg_local" "try_cmpxchg64_local" "try_cmpxchg128_local" "sync_try_cmpxchg"; do gen_xchg "${xchg}" "" printf "\n" done
Signature · 1 line
2.41.0
PATCH 2/3 locking/x86: Wire up sync_try_cmpxchg
Implement target specific support for sync_try_cmpxchg.
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Signed-off-by Uros Bizjak <[email protected]>
--- arch/x86/include/asm/cmpxchg.h | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/include/asm/cmpxchg.h +6 -0
@@ -221,12 +221,18 @@ extern void __add_wrong_size(void)#define __try_cmpxchg (ptr, pold, new, size) \ __raw_try_cmpxchg((ptr), (pold), (new), (size), LOCK_PREFIX) +#define __sync_try_cmpxchg (ptr, pold, new, size) \ + __raw_try_cmpxchg((ptr), (pold), (new), (size), "lock; ") +#define __try_cmpxchg_local (ptr, pold, new, size) \ __raw_try_cmpxchg((ptr), (pold), (new), (size), "")#define arch_try_cmpxchg (ptr, pold, new) \ __try_cmpxchg((ptr), (pold), (new), sizeof(*(ptr))) +#define arch_sync_try_cmpxchg (ptr, pold, new) \ + __sync_try_cmpxchg((ptr), (pold), (new), sizeof(*(ptr))) +#define arch_try_cmpxchg_local (ptr, pold, new) \ __try_cmpxchg_local((ptr), (pold), (new), sizeof(*(ptr)))
Signature · 1 line
2.41.0
PATCH 3/3 xen: Illustrate the transition to sync_try_cmpxchg
In reply to Uros BizjakThis patch illustrates the transition to sync_try_cmpxchg. It is not intended to be merged as-is.
Cc Peter Zijlstra <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Oleksandr Tyshchenko <[email protected]>
Signed-off-by Uros Bizjak <[email protected]>
--- drivers/xen/events/events_fifo.c | 26 ++++++++++++-------------- drivers/xen/grant-table.c | 10 ++++------ 2 files changed, 16 insertions(+), 20 deletions(-)
drivers/xen/events/events_fifo.c +12 -14
@@ -226,21 +226,20 @@ static bool evtchn_fifo_is_masked(evtchn_port_t port)*/ static bool clear_masked_cond (volatile event_word_t *word) { -event_word_t new, old, w; +event_word_t new, old; - w = *word; + old = *word;do { -if (!(w & (1 <<EVTCHN_FIFO_MASKED ))) +if (!(old & (1 <<EVTCHN_FIFO_MASKED )))return true; -if (w & (1 <<EVTCHN_FIFO_PENDING )) +if (old & (1 <<EVTCHN_FIFO_PENDING ))return false; - old = w & ~(1 <<EVTCHN_FIFO_BUSY ); + old = old & ~(1 <<EVTCHN_FIFO_BUSY ); new = old & ~(1 <<EVTCHN_FIFO_MASKED ); - w =sync_cmpxchg (word, old, new); - }while (w != old); + }while (!sync_try_cmpxchg (word, &old, new));return true; }
@@ -259,17 +258,16 @@ static void evtchn_fifo_unmask(evtchn_port_t port)static uint32_t clear_linked (volatile event_word_t *word) { -event_word_t new, old, w; +event_word_t new, old; - w = *word; + old = *word;do { - old = w; - new = (w & ~((1 <<EVTCHN_FIFO_LINKED ) - |EVTCHN_FIFO_LINK_MASK )); - }while ((w =sync_cmpxchg (word, old, new)) != old); + new = (old & ~((1 <<EVTCHN_FIFO_LINKED ) + |EVTCHN_FIFO_LINK_MASK )); + }while (!sync_try_cmpxchg (word, &old, new)); -return w &EVTCHN_FIFO_LINK_MASK ; +return old &EVTCHN_FIFO_LINK_MASK ; } static void consume_one_event(unsigned cpu, struct evtchn_loop_ctrl *ctrl,
drivers/xen/grant-table.c +4 -6
@@ -427,16 +427,14 @@ EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);static int gnttab_end_foreign_access_ref_v1 (grant_ref_t ref) { -u16 flags, nflags; -u16 *pflags; +u16 *pflags = &gnttab_shared.v1[ref].flags; +u16 flags; - pflags = &gnttab_shared.v1[ref].flags; - nflags = *pflags; + flags = *pflags;do { - flags = nflags;if (flags & (GTF_reading|GTF_writing))return 0 ; - }while ((nflags =sync_cmpxchg (pflags, flags,0 )) != flags); + }while (!sync_try_cmpxchg (pflags, &flags,0 ));return 1 ; }
Signature · 1 line
2.41.0
Re: [PATCH 3/3] xen: Illustrate the transition to sync_try_cmpxchg
Quoted 8 lines · Uros Bizjak
This patch illustrates the transition to
sync_try_cmpxchg. It is not intended to be merged as-is.Cc Peter Zijlstra <[email protected]>Cc Juergen Gross <[email protected]>Cc Stefano Stabellini <[email protected]>Cc Oleksandr Tyshchenko <[email protected]>Signed-off-by Uros Bizjak <[email protected]>
Acked-by Juergen Gross <[email protected]>
Juergen