locking/arch: Rewrite local_add_unless as static inline function
Subsystems (6)
PATCH 1/1 locking/arch: Rewrite local_add_unless as static inline function
Rewrite local_add_unless as a static inline function with boolean return value, similar to arch_atomic_add_unless arch fallbacks.
The function is currently unused.
--- arch/alpha/include/asm/local.h | 33 +++++++++++++++--------------- arch/loongarch/include/asm/local.h | 27 ++++++++++++++---------- arch/mips/include/asm/local.h | 27 ++++++++++++++---------- arch/powerpc/include/asm/local.h | 12 +++++------ arch/x86/include/asm/local.h | 33 +++++++++++++++--------------- 5 files changed, 70 insertions(+), 62 deletions(-)
arch/alpha/include/asm/local.h +16 -17
@@ -65,28 +65,27 @@ static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (xchg_local(&((l)->a.counter), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - for (;;) { \ - if (unlikely(c == (u))) \ - break; \ - old = local_cmpxchg((l), c, c + (a)); \ - if (likely(old == c)) \ - break; \ - c = old; \ - } \ - c != (u); \ -}) +static __inline__bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)#define local_add_negative (a, l) (local_add_return((a), (l)) < 0)
arch/loongarch/include/asm/local.h +16 -11
@@ -70,22 +70,27 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (atomic_long_xchg((&(l)->a), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \ - c = old; \ - c != (u); \ -}) +static inline bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)#define local_dec_return (l) local_sub_return(1, (l))
arch/mips/include/asm/local.h +16 -11
@@ -108,22 +108,27 @@ static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (atomic_long_xchg((&(l)->a), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \ - c = old; \ - c != (u); \ -}) +static __inline__bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)#define local_dec_return (l) local_sub_return(1, (l))
arch/powerpc/include/asm/local.h +6 -6
@@ -115,23 +115,23 @@ static __inline__ long local_xchg(local_t *l, long n) }/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to v... * @u: ...unless v is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -static __inline__int local_add_unless (local_t *l,long a,long u) +static __inline__bool local_add_unless (local_t *l,long a,long u) {unsigned long flags; -int ret =0 ; +bool ret = false;powerpc_local_irq_pmu_save (flags);if (l->v != u) { l->v += a; - ret =1 ; + ret = true; }powerpc_local_irq_pmu_restore (flags);
arch/x86/include/asm/local.h +16 -17
@@ -135,28 +135,27 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (xchg(&((l)->a.counter), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read((l)); \ - for (;;) { \ - if (unlikely(c == (u))) \ - break; \ - old = local_cmpxchg((l), c, c + (a)); \ - if (likely(old == c)) \ - break; \ - c = old; \ - } \ - c != (u); \ -}) +static __always_inline bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)/* On x86_32, these are no better than the atomic variants.
Signature · 1 line
2.41.0
Le 31/07/2023 à 10:42, Uros Bizjak a écrit :
Quoted 4 lines
Rewrite
local_add_unlessas a static inline function with boolean return value, similar toarch_atomic_add_unlessarch fallbacks.The function is currently unused.
Is it worth keeping the function if it is not used ?
Christophe
Quoted 248 lines
Cc Will Deacon <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Boqun Feng <[email protected]>Cc Mark Rutland <[email protected]>Cc Richard Henderson <[email protected]>Cc Ivan Kokshaysky <[email protected]>Cc Matt Turner <[email protected]>Cc Huacai Chen <[email protected]>Cc WANG Xuerui <[email protected]>Cc Jiaxun Yang <[email protected]>Cc Jun Yi <[email protected]>Cc Thomas Bogendoerfer <[email protected]>Cc Michael Ellerman <[email protected]>Cc Nicholas Piggin <[email protected]>Cc Christophe Leroy <[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/alpha/include/asm/local.h | 33 +++++++++++++++--------------- arch/loongarch/include/asm/local.h | 27 ++++++++++++++---------- arch/mips/include/asm/local.h | 27 ++++++++++++++---------- arch/powerpc/include/asm/local.h | 12 +++++------ arch/x86/include/asm/local.h | 33 +++++++++++++++--------------- 5 files changed, 70 insertions(+), 62 deletions(-)arch/alpha/include/asm/local.h +16
-17@@ -65,28 +65,27 @@ static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (xchg_local(&((l)->a.counter), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - for (;;) { \ - if (unlikely(c == (u))) \ - break; \ - old = local_cmpxchg((l), c, c + (a)); \ - if (likely(old == c)) \ - break; \ - c = old; \ - } \ - c != (u); \ -}) +static __inline__bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)#define local_add_negative (a, l) (local_add_return((a), (l)) < 0)arch/loongarch/include/asm/local.h +16
-11@@ -70,22 +70,27 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (atomic_long_xchg((&(l)->a), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \ - c = old; \ - c != (u); \ -}) +static inline bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)#define local_dec_return (l) local_sub_return(1, (l))arch/mips/include/asm/local.h +16
-11@@ -108,22 +108,27 @@ static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (atomic_long_xchg((&(l)->a), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \ - c = old; \ - c != (u); \ -}) +static __inline__bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)#define local_dec_return (l) local_sub_return(1, (l))arch/powerpc/include/asm/local.h +6
-6@@ -115,23 +115,23 @@ static __inline__ long local_xchg(local_t *l, long n) }/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to v... * @u: ...unless v is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -static __inline__int local_add_unless (local_t *l,long a,long u) +static __inline__bool local_add_unless (local_t *l,long a,long u) {unsigned long flags; -int ret =0 ; +bool ret = false;powerpc_local_irq_pmu_save (flags);if (l->v != u) { l->v += a; - ret =1 ; + ret = true; }powerpc_local_irq_pmu_restore (flags);arch/x86/include/asm/local.h +16
-17@@ -135,28 +135,27 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (xchg(&((l)->a.counter), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read((l)); \ - for (;;) { \ - if (unlikely(c == (u))) \ - break; \ - old = local_cmpxchg((l), c, c + (a)); \ - if (likely(old == c)) \ - break; \ - c = old; \ - } \ - c != (u); \ -}) +static __always_inline bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)/* On x86_32, these are no better than the atomic variants.
Quoted 10 lines · Christophe Leroy
Quoted 4 lines · Uros Bizjak
Rewrite
local_add_unlessas a static inline function with boolean return value, similar toarch_atomic_add_unlessarch fallbacks.The function is currently unused.
Is it worth keeping the function if it is not used ?
We already have plenty of these kinds of unused locking functions in arch/*/include/asm/atomic.h, so I thought we could leave this one as well.
Uros.
[tip: locking/core] locking/local, arch: Rewrite local_add_unless() as a static inline function
In reply to Uros BizjakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 5e0eb67974e88dbaded765278a3ffe7af33e3b22 Gitweb: https://git.kernel.org/tip/5e0eb67974e88dbaded765278a3ffe7af33e3b22 Author: Uros Bizjak <[email protected]> AuthorDate: Mon, 31 Jul 2023 10:42:23 +02:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 04 Oct 2023 11:38:11 +02:00
locking/local, arch: Rewrite local_add_unless() as a static inline function
Rewrite local_add_unless() as a static inline function with boolean return value, similar to the arch_atomic_add_unless() arch fallbacks.
The function is currently unused.
--- arch/alpha/include/asm/local.h | 33 ++++++++++++++--------------- arch/loongarch/include/asm/local.h | 27 ++++++++++++++---------- arch/mips/include/asm/local.h | 27 ++++++++++++++---------- arch/powerpc/include/asm/local.h | 12 +++++------ arch/x86/include/asm/local.h | 33 ++++++++++++++--------------- 5 files changed, 70 insertions(+), 62 deletions(-)
arch/alpha/include/asm/local.h +16 -17
@@ -65,28 +65,27 @@ static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (xchg_local(&((l)->a.counter), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - for (;;) { \ - if (unlikely(c == (u))) \ - break; \ - old = local_cmpxchg((l), c, c + (a)); \ - if (likely(old == c)) \ - break; \ - c = old; \ - } \ - c != (u); \ -}) +static __inline__bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)#define local_add_negative (a, l) (local_add_return((a), (l)) < 0)
arch/loongarch/include/asm/local.h +16 -11
@@ -70,22 +70,27 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (atomic_long_xchg((&(l)->a), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \ - c = old; \ - c != (u); \ -}) +static inline bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)#define local_dec_return (l) local_sub_return(1, (l))
arch/mips/include/asm/local.h +16 -11
@@ -108,22 +108,27 @@ static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (atomic_long_xchg((&(l)->a), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \ - c = old; \ - c != (u); \ -}) +static __inline__bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)#define local_dec_return (l) local_sub_return(1, (l))
arch/powerpc/include/asm/local.h +6 -6
@@ -115,23 +115,23 @@ static __inline__ long local_xchg(local_t *l, long n) }/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to v... * @u: ...unless v is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -static __inline__int local_add_unless (local_t *l,long a,long u) +static __inline__bool local_add_unless (local_t *l,long a,long u) {unsigned long flags; -int ret =0 ; +bool ret = false;powerpc_local_irq_pmu_save (flags);if (l->v != u) { l->v += a; - ret =1 ; + ret = true; }powerpc_local_irq_pmu_restore (flags);
arch/x86/include/asm/local.h +16 -17
@@ -135,28 +135,27 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new)#define local_xchg (l, n) (xchg(&((l)->a.counter), (n)))/** -* local_add_unless - add unless the number is a given value +* local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * -* Atomically adds @a to @l, so long as it was not @u. -* Returns non-zero if @l was not @u, and zero otherwise. +* Atomically adds @a to @l, if @v was not already @u. +* Returns true if the addition was done. */ -#define local_add_unless (l, a, u) \ -({ \ - long c, old; \ - c = local_read((l)); \ - for (;;) { \ - if (unlikely(c == (u))) \ - break; \ - old = local_cmpxchg((l), c, c + (a)); \ - if (likely(old == c)) \ - break; \ - c = old; \ - } \ - c != (u); \ -}) +static __always_inline bool +local_add_unless (local_t *l,long a,long u) +{ +long c =local_read (l); + +do { +if (unlikely (c == u)) +return false; + }while (!local_try_cmpxchg (l, &c, c + a)); + +return true; +} +#define local_inc_not_zero (l) local_add_unless((l), 1, 0)/* On x86_32, these are no better than the atomic variants.