* [PATCH] asm-generic/atomic: Add try_cmpxchg() fallbacks [not found] ` <20200218131310.GZ14914@hirez.programming.kicks-ass.net> @ 2020-02-18 14:27 ` Peter Zijlstra 2020-02-18 14:40 ` Peter Zijlstra 2020-02-20 10:30 ` Will Deacon 0 siblings, 2 replies; 5+ messages in thread From: Peter Zijlstra @ 2020-02-18 14:27 UTC (permalink / raw) To: Jens Axboe Cc: Carter Li 李通洲, Pavel Begunkov, io-uring, Oleg Nesterov, Mark Rutland, linux-kernel, Will Deacon On Tue, Feb 18, 2020 at 02:13:10PM +0100, Peter Zijlstra wrote: > (with the caveat that try_cmpxchg() doesn't seem available on !x86 -- I > should go fix that) Completely untested (lemme go do that shortly), but something like so I suppose. --- Subject: asm-generic/atomic: Add try_cmpxchg() fallbacks Only x86 provides try_cmpxchg() outside of the atomic_t interfaces, provide generic fallbacks to create this interface from the widely available cmpxchg() function. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> --- diff --git a/include/linux/atomic-fallback.h b/include/linux/atomic-fallback.h index 656b5489b673..243f61d6c35f 100644 --- a/include/linux/atomic-fallback.h +++ b/include/linux/atomic-fallback.h @@ -77,6 +77,50 @@ #endif /* cmpxchg64_relaxed */ +#ifndef try_cmpxchg +#define try_cmpxchg(_ptr, _oldp, _new) \ +({ \ + typeof(*ptr) ___r, ___o = *(_oldp); \ + ___r = cmpxchg((_ptr), ___o, (_new)); \ + if (unlikely(___r != ___o)) \ + *(_old) = ___r; \ + likely(___r == ___o); \ +}) +#endif /* try_cmpxchg */ + +#ifndef try_cmpxchg_acquire +#define try_cmpxchg_acquire(_ptr, _oldp, _new) \ +({ \ + typeof(*ptr) ___r, ___o = *(_oldp); \ + ___r = cmpxchg_acquire((_ptr), ___o, (_new)); \ + if (unlikely(___r != ___o)) \ + *(_old) = ___r; \ + likely(___r == ___o); \ +}) +#endif /* try_cmpxchg_acquire */ + +#ifndef try_cmpxchg_release +#define try_cmpxchg_release(_ptr, _oldp, _new) \ +({ \ + typeof(*ptr) ___r, ___o = *(_oldp); \ + ___r = cmpxchg_release((_ptr), ___o, (_new)); \ + if (unlikely(___r != ___o)) \ + *(_old) = ___r; \ + likely(___r == ___o); \ +}) +#endif /* try_cmpxchg_release */ + +#ifndef try_cmpxchg_relaxed +#define try_cmpxchg_relaxed(_ptr, _oldp, _new) \ +({ \ + typeof(*ptr) ___r, ___o = *(_oldp); \ + ___r = cmpxchg_relaxed((_ptr), ___o, (_new)); \ + if (unlikely(___r != ___o)) \ + *(_old) = ___r; \ + likely(___r == ___o); \ +}) +#endif /* try_cmpxchg_relaxed */ + #ifndef atomic_read_acquire static __always_inline int atomic_read_acquire(const atomic_t *v) @@ -2294,4 +2338,4 @@ atomic64_dec_if_positive(atomic64_t *v) #define atomic64_cond_read_relaxed(v, c) smp_cond_load_relaxed(&(v)->counter, (c)) #endif /* _LINUX_ATOMIC_FALLBACK_H */ -// baaf45f4c24ed88ceae58baca39d7fd80bb8101b +// 2dfbc767ce308d2edd67a49bd7b764dd07f62f6c diff --git a/scripts/atomic/gen-atomic-fallback.sh b/scripts/atomic/gen-atomic-fallback.sh index b6c6f5d306a7..3c9be8d550e0 100755 --- a/scripts/atomic/gen-atomic-fallback.sh +++ b/scripts/atomic/gen-atomic-fallback.sh @@ -140,6 +140,32 @@ cat <<EOF EOF } +gen_try_cmpxchg_fallback() +{ + local order="$1"; shift; + +cat <<EOF +#ifndef try_cmpxchg${order} +#define try_cmpxchg${order}(_ptr, _oldp, _new) \\ +({ \\ + typeof(*ptr) ___r, ___o = *(_oldp); \\ + ___r = cmpxchg${order}((_ptr), ___o, (_new)); \\ + if (unlikely(___r != ___o)) \\ + *(_old) = ___r; \\ + likely(___r == ___o); \\ +}) +#endif /* try_cmpxchg${order} */ + +EOF +} + +gen_try_cmpxchg_fallbacks() +{ + for order in "" "_acquire" "_release" "_relaxed"; do + gen_try_cmpxchg_fallback "${order}" + done +} + cat << EOF // SPDX-License-Identifier: GPL-2.0 @@ -157,6 +183,8 @@ for xchg in "xchg" "cmpxchg" "cmpxchg64"; do gen_xchg_fallbacks "${xchg}" done +gen_try_cmpxchg_fallbacks + grep '^[a-z]' "$1" | while read name meta args; do gen_proto "${meta}" "${name}" "atomic" "int" ${args} done ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] asm-generic/atomic: Add try_cmpxchg() fallbacks 2020-02-18 14:27 ` [PATCH] asm-generic/atomic: Add try_cmpxchg() fallbacks Peter Zijlstra @ 2020-02-18 14:40 ` Peter Zijlstra 2020-02-20 10:30 ` Will Deacon 1 sibling, 0 replies; 5+ messages in thread From: Peter Zijlstra @ 2020-02-18 14:40 UTC (permalink / raw) To: Jens Axboe Cc: Carter Li 李通洲, Pavel Begunkov, io-uring, Oleg Nesterov, Mark Rutland, linux-kernel, Will Deacon On Tue, Feb 18, 2020 at 03:27:00PM +0100, Peter Zijlstra wrote: > diff --git a/scripts/atomic/gen-atomic-fallback.sh b/scripts/atomic/gen-atomic-fallback.sh > index b6c6f5d306a7..3c9be8d550e0 100755 > --- a/scripts/atomic/gen-atomic-fallback.sh > +++ b/scripts/atomic/gen-atomic-fallback.sh > @@ -140,6 +140,32 @@ cat <<EOF > EOF > } > > +gen_try_cmpxchg_fallback() > +{ > + local order="$1"; shift; > + > +cat <<EOF > +#ifndef try_cmpxchg${order} > +#define try_cmpxchg${order}(_ptr, _oldp, _new) \\ > +({ \\ > + typeof(*ptr) ___r, ___o = *(_oldp); \\ *(_ptr) > + ___r = cmpxchg${order}((_ptr), ___o, (_new)); \\ > + if (unlikely(___r != ___o)) \\ > + *(_old) = ___r; \\ *(_oldp) > + likely(___r == ___o); \\ > +}) > +#endif /* try_cmpxchg${order} */ And it actually builds, as tested on an ARM build. ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] asm-generic/atomic: Add try_cmpxchg() fallbacks 2020-02-18 14:27 ` [PATCH] asm-generic/atomic: Add try_cmpxchg() fallbacks Peter Zijlstra 2020-02-18 14:40 ` Peter Zijlstra @ 2020-02-20 10:30 ` Will Deacon 2020-02-20 10:37 ` Peter Zijlstra 1 sibling, 1 reply; 5+ messages in thread From: Will Deacon @ 2020-02-20 10:30 UTC (permalink / raw) To: Peter Zijlstra Cc: Jens Axboe, Carter Li 李通洲, Pavel Begunkov, io-uring, Oleg Nesterov, Mark Rutland, linux-kernel On Tue, Feb 18, 2020 at 03:27:00PM +0100, Peter Zijlstra wrote: > On Tue, Feb 18, 2020 at 02:13:10PM +0100, Peter Zijlstra wrote: > > (with the caveat that try_cmpxchg() doesn't seem available on !x86 -- I > > should go fix that) > > Completely untested (lemme go do that shortly), but something like so I > suppose. > > --- > Subject: asm-generic/atomic: Add try_cmpxchg() fallbacks > > Only x86 provides try_cmpxchg() outside of the atomic_t interfaces, > provide generic fallbacks to create this interface from the widely > available cmpxchg() function. > > Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> > --- > diff --git a/include/linux/atomic-fallback.h b/include/linux/atomic-fallback.h > index 656b5489b673..243f61d6c35f 100644 > --- a/include/linux/atomic-fallback.h > +++ b/include/linux/atomic-fallback.h > @@ -77,6 +77,50 @@ > > #endif /* cmpxchg64_relaxed */ > > +#ifndef try_cmpxchg > +#define try_cmpxchg(_ptr, _oldp, _new) \ > +({ \ > + typeof(*ptr) ___r, ___o = *(_oldp); \ Probably worth pointing out that this will have the nasty behaviour for volatile pointers that we're tackling for READ_ONCE. Obviously no need to hold this up, but just mentioning it here in the hope that one of us remembers to fix it later on. > diff --git a/scripts/atomic/gen-atomic-fallback.sh b/scripts/atomic/gen-atomic-fallback.sh > index b6c6f5d306a7..3c9be8d550e0 100755 > --- a/scripts/atomic/gen-atomic-fallback.sh > +++ b/scripts/atomic/gen-atomic-fallback.sh > @@ -140,6 +140,32 @@ cat <<EOF > EOF > } > > +gen_try_cmpxchg_fallback() > +{ > + local order="$1"; shift; > + > +cat <<EOF > +#ifndef try_cmpxchg${order} > +#define try_cmpxchg${order}(_ptr, _oldp, _new) \\ > +({ \\ > + typeof(*ptr) ___r, ___o = *(_oldp); \\ > + ___r = cmpxchg${order}((_ptr), ___o, (_new)); \\ > + if (unlikely(___r != ___o)) \\ > + *(_old) = ___r; \\ This doesn't compile because _old isn't declared. Probably best to avoid evaluating _oldp twice though. Will ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] asm-generic/atomic: Add try_cmpxchg() fallbacks 2020-02-20 10:30 ` Will Deacon @ 2020-02-20 10:37 ` Peter Zijlstra 2020-02-20 10:39 ` Will Deacon 0 siblings, 1 reply; 5+ messages in thread From: Peter Zijlstra @ 2020-02-20 10:37 UTC (permalink / raw) To: Will Deacon Cc: Jens Axboe, Carter Li 李通洲, Pavel Begunkov, io-uring, Oleg Nesterov, Mark Rutland, linux-kernel On Thu, Feb 20, 2020 at 10:30:45AM +0000, Will Deacon wrote: > On Tue, Feb 18, 2020 at 03:27:00PM +0100, Peter Zijlstra wrote: > > On Tue, Feb 18, 2020 at 02:13:10PM +0100, Peter Zijlstra wrote: > > > (with the caveat that try_cmpxchg() doesn't seem available on !x86 -- I > > > should go fix that) > > > > Completely untested (lemme go do that shortly), but something like so I > > suppose. > > > > --- > > Subject: asm-generic/atomic: Add try_cmpxchg() fallbacks > > > > Only x86 provides try_cmpxchg() outside of the atomic_t interfaces, > > provide generic fallbacks to create this interface from the widely > > available cmpxchg() function. > > > > Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> > > --- > > diff --git a/include/linux/atomic-fallback.h b/include/linux/atomic-fallback.h > > index 656b5489b673..243f61d6c35f 100644 > > --- a/include/linux/atomic-fallback.h > > +++ b/include/linux/atomic-fallback.h > > @@ -77,6 +77,50 @@ > > > > #endif /* cmpxchg64_relaxed */ > > > > +#ifndef try_cmpxchg > > +#define try_cmpxchg(_ptr, _oldp, _new) \ > > +({ \ > > + typeof(*ptr) ___r, ___o = *(_oldp); \ > > Probably worth pointing out that this will have the nasty behaviour > for volatile pointers that we're tackling for READ_ONCE. Obviously no > need to hold this up, but just mentioning it here in the hope that one > of us remembers to fix it later on. Right :/ > > diff --git a/scripts/atomic/gen-atomic-fallback.sh b/scripts/atomic/gen-atomic-fallback.sh > > index b6c6f5d306a7..3c9be8d550e0 100755 > > --- a/scripts/atomic/gen-atomic-fallback.sh > > +++ b/scripts/atomic/gen-atomic-fallback.sh > > @@ -140,6 +140,32 @@ cat <<EOF > > EOF > > } > > > > +gen_try_cmpxchg_fallback() > > +{ > > + local order="$1"; shift; > > + > > +cat <<EOF > > +#ifndef try_cmpxchg${order} > > +#define try_cmpxchg${order}(_ptr, _oldp, _new) \\ > > +({ \\ > > + typeof(*ptr) ___r, ___o = *(_oldp); \\ > > + ___r = cmpxchg${order}((_ptr), ___o, (_new)); \\ > > + if (unlikely(___r != ___o)) \\ > > + *(_old) = ___r; \\ > > This doesn't compile because _old isn't declared. Probably best to avoid > evaluating _oldp twice though. Compiler had already spotted that, I'll make it something like: typeof(*ptr) *___op = (_oldp), ___o = *___op; ... *___op = ___r; Which avoids the double eval. ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] asm-generic/atomic: Add try_cmpxchg() fallbacks 2020-02-20 10:37 ` Peter Zijlstra @ 2020-02-20 10:39 ` Will Deacon 0 siblings, 0 replies; 5+ messages in thread From: Will Deacon @ 2020-02-20 10:39 UTC (permalink / raw) To: Peter Zijlstra Cc: Jens Axboe, Carter Li 李通洲, Pavel Begunkov, io-uring, Oleg Nesterov, Mark Rutland, linux-kernel On Thu, Feb 20, 2020 at 11:37:27AM +0100, Peter Zijlstra wrote: > On Thu, Feb 20, 2020 at 10:30:45AM +0000, Will Deacon wrote: > > On Tue, Feb 18, 2020 at 03:27:00PM +0100, Peter Zijlstra wrote: > > > diff --git a/scripts/atomic/gen-atomic-fallback.sh b/scripts/atomic/gen-atomic-fallback.sh > > > index b6c6f5d306a7..3c9be8d550e0 100755 > > > --- a/scripts/atomic/gen-atomic-fallback.sh > > > +++ b/scripts/atomic/gen-atomic-fallback.sh > > > @@ -140,6 +140,32 @@ cat <<EOF > > > EOF > > > } > > > > > > +gen_try_cmpxchg_fallback() > > > +{ > > > + local order="$1"; shift; > > > + > > > +cat <<EOF > > > +#ifndef try_cmpxchg${order} > > > +#define try_cmpxchg${order}(_ptr, _oldp, _new) \\ > > > +({ \\ > > > + typeof(*ptr) ___r, ___o = *(_oldp); \\ > > > + ___r = cmpxchg${order}((_ptr), ___o, (_new)); \\ > > > + if (unlikely(___r != ___o)) \\ > > > + *(_old) = ___r; \\ > > > > This doesn't compile because _old isn't declared. Probably best to avoid > > evaluating _oldp twice though. > > Compiler had already spotted that, I'll make it something like: > > typeof(*ptr) *___op = (_oldp), ___o = *___op; > > ... > > *___op = ___r; > > Which avoids the double eval. Cool, you can stick my Ack on the patch with that change. Will ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-02-20 10:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <5995f84e-8a6c-e774-6bb5-5b9b87a9cd3c@kernel.dk>
[not found] ` <7c4c3996-4886-eb58-cdee-fe0951907ab5@kernel.dk>
[not found] ` <addcd44e-ed9b-5f82-517d-c1ed3ee2d85c@kernel.dk>
[not found] ` <b8069e62-7ea4-c7f3-55a3-838241951068@kernel.dk>
[not found] ` <20200217120920.GQ14914@hirez.programming.kicks-ass.net>
[not found] ` <53de3581-b902-89ba-3f53-fd46b052df40@kernel.dk>
[not found] ` <43c066d1-a892-6a02-82e7-7be850d9454d@kernel.dk>
[not found] ` <20200217174610.GU14897@hirez.programming.kicks-ass.net>
[not found] ` <592cf069-41ee-0bc1-1f83-e058e5dd53ff@kernel.dk>
[not found] ` <20200218131310.GZ14914@hirez.programming.kicks-ass.net>
2020-02-18 14:27 ` [PATCH] asm-generic/atomic: Add try_cmpxchg() fallbacks Peter Zijlstra
2020-02-18 14:40 ` Peter Zijlstra
2020-02-20 10:30 ` Will Deacon
2020-02-20 10:37 ` Peter Zijlstra
2020-02-20 10:39 ` Will Deacon
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
Powered by JetHome