sparc32,leon: Use CASA when available for atomic operations
Andreas Larsson
maintainerPATCH 1/1 sparc32,leon: Use CASA when available for atomic operations
This probes for CASA support, that is commonly present in LEON processors, and when available, uses the CASA instruction for atomic operations rather than the spinlock based emulated atomic operations.
All CASA instructions are encoded using .word to be able to assemble for v8.
---
This the followup version to the "sparc32,leon: Allow and use CAS for atomic operations for LEON" patch series that did not do any runtime detection.
There are a number of style warnings from checkpatch for this patch (and even errors for space after parenthesis) and warnings about volatile usage. However the style warnings are due to changes (like function renames) to existing code and the volatile warnings due to using the same function signatures for the CASA based functions as for the emulated atomics.
The reason for adding the new arch/sparc/include/asm/cas_32.h is that it is needed in several headers and trying to put the variable declaration in atomic_32.h or chpxchg_32 lead to circular inclusions.
arch/sparc/include/asm/atomic_32.h | 104 +++++++++++++++++++++++-----
arch/sparc/include/asm/bitops_32.h | 93 ++++++++++++++++++------- arch/sparc/include/asm/cas_32.h | 7 ++ arch/sparc/include/asm/cmpxchg_32.h | 59 ++++++++++++++-- arch/sparc/kernel/entry.S | 6 +- arch/sparc/kernel/head_32.S | 8 +++ arch/sparc/lib/Makefile | 1 + arch/sparc/lib/atomic32.c | 62 ++++++++++++----- arch/sparc/lib/atomic_cas_32.S | 71 +++++++++++++++++++ arch/sparc/lib/bitops_cas_32.S | 132 ++++++++++++++++++++++++++++++++++++ 10 files changed, 477 insertions(+), 66 deletions(-) create mode 100644 arch/sparc/include/asm/cas_32.h create mode 100644 arch/sparc/lib/atomic_cas_32.S create mode 100644 arch/sparc/lib/bitops_cas_32.S
arch/sparc/include/asm/atomic_32.h +86 -18
@@ -16,38 +16,106 @@#include <asm/cmpxchg.h> #include <asm/barrier.h> +#include <asm/cas_32.h> #include <asm-generic/atomic64.h> #define ATOMIC_INIT (i) { (i) } -int atomic_add_return (int ,atomic_t *); -int atomic_fetch_add (int ,atomic_t *); -int atomic_fetch_and (int ,atomic_t *); -int atomic_fetch_or (int ,atomic_t *); -int atomic_fetch_xor (int ,atomic_t *); -int atomic_cmpxchg (atomic_t *,int ,int ); -int atomic_xchg (atomic_t *,int ); +int emul_atomic_add_return (int ,atomic_t *); +int emul_atomic_fetch_add (int ,atomic_t *); +int emul_atomic_fetch_and (int ,atomic_t *); +int emul_atomic_fetch_or (int ,atomic_t *); +int emul_atomic_fetch_xor (int ,atomic_t *); +int emul_atomic_cmpxchg (atomic_t *,int ,int ); +int emul_atomic_xchg (atomic_t *,int ); +void emul_atomic_set (atomic_t *,int ); +int __atomic_add_unless (atomic_t *,int ,int ); -void atomic_set (atomic_t *,int );#define atomic_set_release (v, i) atomic_set((v), (i))#define atomic_read (v) READ_ONCE((v)->counter) -#define atomic_add (i, v) ((void)atomic_add_return( (int)(i), (v))) -#define atomic_sub (i, v) ((void)atomic_add_return(-(int)(i), (v))) -#define atomic_inc (v) ((void)atomic_add_return( 1, (v))) -#define atomic_dec (v) ((void)atomic_add_return( -1, (v))) +#define emul_atomic_add (i, v) ((void)emul_atomic_add_return( (int)(i), (v))) +#define emul_atomic_sub (i, v) ((void)emul_atomic_add_return(-(int)(i), (v))) + +#define emul_atomic_and (i, v) ((void)emul_atomic_fetch_and((i), (v))) +#define emul_atomic_or (i, v) ((void)emul_atomic_fetch_or((i), (v))) +#define emul_atomic_xor (i, v) ((void)emul_atomic_fetch_xor((i), (v))) + +#define emul_atomic_sub_return (i, v) (emul_atomic_add_return(-(int)(i), (v))) +#define emul_atomic_fetch_sub (i, v) (emul_atomic_fetch_add (-(int)(i), (v))) + +static inline void atomic_set (atomic_t *v,int i) +{ +if (sparc32_cas_capable) +WRITE_ONCE (v->counter, i); +else +emul_atomic_set (v, i); +} + +#define ATOMIC_VOID_FUNC (func) \ +void cas_atomic_##func(int i, atomic_t *v); \ +static inline void atomic_##func(int i, atomic_t *v) \ +{ \ + if (sparc32_cas_capable) \ + cas_atomic_##func(i, v); \ + else \ + emul_atomic_##func(i, v); \ +} + +#define ATOMIC_INT_FUNC (func) \ +int cas_atomic_##func(int i, atomic_t *v); \ +static inline int atomic_##func(int i, atomic_t *v) \ +{ \ + if (sparc32_cas_capable) \ + return cas_atomic_##func(i, v); \ + else \ + return emul_atomic_##func(i, v); \ +} + +ATOMIC_VOID_FUNC (add) +ATOMIC_VOID_FUNC (sub ) +ATOMIC_VOID_FUNC (and) +ATOMIC_VOID_FUNC (or) +ATOMIC_VOID_FUNC (xor) + +ATOMIC_INT_FUNC (fetch_add) +ATOMIC_INT_FUNC (fetch_sub) +ATOMIC_INT_FUNC (fetch_and) +ATOMIC_INT_FUNC (fetch_or) +ATOMIC_INT_FUNC (fetch_xor) + +ATOMIC_INT_FUNC (add_return) +ATOMIC_INT_FUNC (sub_return) + +#undef ATOMIC_VOID_FUNC +#undef ATOMIC_INT_FUNC + +#define cas_atomic_xchg(v, new) (xchg(&((v)->counter), (new))) +#define cas_atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n))) + +static inline int atomic_cmpxchg(atomic_t *v, int old, int new) +{ + if (sparc32_cas_capable) + return cas_atomic_cmpxchg(v, old, new); + else + return emul_atomic_cmpxchg(v, old, new); +} -#define atomic_and (i, v) ((void)atomic_fetch_and((i), (v))) -#define atomic_or (i, v) ((void)atomic_fetch_or((i), (v))) -#define atomic_xor (i, v) ((void)atomic_fetch_xor((i), (v))) +static inline int atomic_xchg (atomic_t *v,int new) +{ +if (sparc32_cas_capable) +return cas_atomic_xchg (v, new); +else +return emul_atomic_xchg (v, new); +} -#define atomic_sub_return (i, v) (atomic_add_return(-(int)(i), (v))) -#define atomic_fetch_sub (i, v) (atomic_fetch_add (-(int)(i), (v))) +#define atomic_inc (v) (atomic_add(1, (v))) +#define atomic_dec (v) (atomic_sub(1, (v)))#define atomic_inc_return (v) (atomic_add_return( 1, (v))) -#define atomic_dec_return (v) (atomic_add_return( -1, (v))) +#define atomic_dec_return (v) (atomic_sub_return( 1, (v)))#define atomic_add_negative (a, v) (atomic_add_return((a), (v)) < 0)
arch/sparc/include/asm/bitops_32.h +69 -24
143 lines not on this page · raw message
arch/sparc/include/asm/cas_32.h new file +7 -0
@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _SPARC_CAS_32_H +#define _SPARC_CAS_32_H + +extern int sparc32_cas_capable; + +#endif /* _SPARC_CAS_32_H */
arch/sparc/include/asm/cmpxchg_32.h +53 -6
90 lines not on this page · raw message
arch/sparc/kernel/entry.S +5 -1
@@ -439,6 +439,10 @@ bad_instruction: and %l5, %l4, %l5 cmp %l5, %l7 be 1f + sethi %hi(leon_cas_check), %l4 + or %l4, %lo(leon_cas_check), %l4 + cmp %l1, %l4 + be 1f SAVE_ALL wr %l0, PSR_ET, %psr ! re-enable traps
@@ -452,7 +456,7 @@ bad_instruction: RESTORE_ALL -1: /* unimplemented flush - just skip */ +1: /* unimplemented flush or probed CASA - just skip */ jmpl %l2, %g0 rett %l2 + 4
arch/sparc/kernel/head_32.S +8 -0
@@ -441,6 +441,14 @@ leon_init: /* Update boot_cpu_id only on boot cpu */ stub %g1, [%g2 + %lo(boot_cpu_id)] + /* Check if CASA is supported */ + set sparc32_cas_capable, %g1 + mov 1, %g2 + + .global leon_cas_check +leon_cas_check: + .word 0xc5e04160 /* casa [%g1] 0xb, %g0, %g2 */ + ba continue_boot nop
arch/sparc/lib/Makefile +1 -0
@@ -53,4 +53,5 @@ lib-$(CONFIG_SPARC64) += mcount.o ipcsum.o xor.o hweight.o ffs.o obj-$(CONFIG_SPARC64) += iomap.o obj-$(CONFIG_SPARC32) += atomic32.o ucmpdi2.o +obj-$(CONFIG_SPARC32) += atomic_cas_32.o bitops_cas_32.o obj-$(CONFIG_SPARC64) += PeeCeeI.o
arch/sparc/lib/atomic32.c +45 -17
146 lines not on this page · raw message
arch/sparc/lib/atomic_cas_32.S new file +71 -0
71 lines not on this page · raw message
arch/sparc/lib/bitops_cas_32.S new file +132 -0
132 lines not on this page · raw message
Signature · 1 line
2.8.0
Hi Andreas.
Quoted 6 lines · Andreas Larsson
This probes for CASA support, that is commonly present in LEON processors, and when available, uses the CASA instruction for atomic operations rather than the spinlock based emulated atomic operations.
All CASA instructions are encoded using .word to be able to assemble for v8.
The patch mixes several things, so parts was not easy to follow. It would have been much better if, based on the dynamic probing, to replace relevant assembler parts with the relevant implementation. So we avoid the check for sparc32_cas_capable in all the atomic_* functions.
And the end result would most likely also be a more readable/simple implementation.
And the end result could look like:
PATCH 1 - preparation
PATCH 2 - infrastructure
PATCH 3 - assembler version ready for patching
PATCH 4 - cas varaints - unused
PATCH 5 - detection and patching
Just to give you an idea.
You have most of the necessary bits in place already. So most is code shuffelign and creating assembler versions ready for patching.
You already have nice macros that avoids a lot of code duplication, and this principle can be reused following the scheme outlined above.
An open question. There is a long-standing issue in glibc where sparc32 does not support threading (IIRC). It had to do with missing atomic support, which had to be emulated in the kernel.
Will this patch move us closer to have that fixed?
Sam
arch/sparc/kernel/entry.S bad_instruction: · line 446
Quoted 13 lines
arch/sparc/kernel/entry.S +4
-0@@ -439,6 +439,10 @@ bad_instruction: and %l5, %l4, %l5 cmp %l5, %l7 be 1f + sethi %hi(leon_cas_check), %l4 + or %l4, %lo(leon_cas_check), %l4 + cmp %l1, %l4 + be 1f SAVE_ALL
Here a nop is missing in the delay slot after "be 1f"
Quoted 19 lines
arch/sparc/kernel/entry.S +1
-1wr %l0, PSR_ET, %psr ! re-enable traps@@ -452,7 +456,7 @@ bad_instruction: RESTORE_ALL -1: /* unimplemented flush - just skip */ +1: /* unimplemented flush or probed CASA - just skip */ jmpl %l2, %g0 rett %l2 + 4arch/sparc/kernel/head_32.S +0
-0@@ -441,6 +441,14 @@ leon_init: /* Update boot_cpu_id only on boot cpu */ stub %g1, [%g2 + %lo(boot_cpu_id)]
arch/sparc/kernel/head_32.S leon_init: · line 453
Quoted 10 lines
arch/sparc/kernel/head_32.S +8
-0+ /* Check if CASA is supported */ + set sparc32_cas_capable, %g1 + mov 1, %g2 + + .global leon_cas_check +leon_cas_check: + .word 0xc5e04160 /* casa [%g1] 0xb, %g0, %g2 */ + ba continue_boot nop
I could not follow this code-snippet.
Maybe this is my ignorance of the casa instruction. Will it store the value of %g2 (=1) in the address pointed by %g1 (sparc32_cas_capable) if casa is enabled? Maybe it is obvious for others, but it ws not for me. So one or two comments more...
Sam
Andreas Larsson
maintainerQuoted 26 lines · Sam Ravnborg
Hi Andreas.
Quoted 6 lines · Andreas Larsson
This probes for CASA support, that is commonly present in LEON processors, and when available, uses the CASA instruction for atomic operations rather than the spinlock based emulated atomic operations.
All CASA instructions are encoded using .word to be able to assemble for v8.
The patch mixes several things, so parts was not easy to follow. It would have been much better if, based on the dynamic probing, to replace relevant assembler parts with the relevant implementation. So we avoid the check for
sparc32_cas_capablein all the atomic_* functions.And the end result would most likely also be a more readable/simple implementation.
And the end result could look like:
PATCH 1 - preparation
PATCH 2 - infrastructure
PATCH 3 - assembler version ready for patching
PATCH 4 - cas varaints - unused
PATCH 5 - detection and patching
Quoted 8 lines
Just to give you an idea.
You have most of the necessary bits in place already. So most is code shuffelign and creating assembler versions ready for patching.
You already have nice macros that avoids a lot of code duplication, and this principle can be reused following the scheme outlined above.
Thank you for the feedback!
Sure I can try to split things up in more pieces. I am not sure about how well instruction patching will fit though. We have a good mix of assembly, inline C code and macros that makes up the different parts of the interface to the different kinds of atomic functionality. We don't have the "easy" assembly level patching possibilities with "if case A then use this set assembly instructions and if case B, patch it up with this other set of assembly instructions". It is not merely CAS that is either emulated or done in hardware. The emulation locking needs to be used for writes of all kinds to these addresses when emulating CAS to achieve atomicity.
Quoted 6 lines
An open question. There is a long-standing issue in glibc where sparc32 does not support threading (IIRC). It had to do with missing atomic support, which had to be emulated in the kernel.
Will this patch move us closer to have that fixed?
This does not add any of the main components of the CAS emulation for user space done by the kernel that Dave has planned. But it makes potential actual CAS available to the kernel when that can be used in such a kernel emulation "call". Also, the information if the hardware can use the CASA instruction can eventually be used in glibc to use CASA when possible and the kernel emulation otherwise, once the kernel emulation is in place. I have a followup patch (that I might just as well add to the series when breaking it up in pieces) that adds the CAS capability to the hardware capability information that glibc can use.
Quoted 4 lines
Sam
Quoted 14 lines
arch/sparc/kernel/entry.S bad_instruction: · line 446
Quoted 13 lines
arch/sparc/kernel/entry.S +4
-0@@ -439,6 +439,10 @@ bad_instruction: and %l5, %l4, %l5 cmp %l5, %l7 be 1f + sethi %hi(leon_cas_check), %l4 + or %l4, %lo(leon_cas_check), %l4 + cmp %l1, %l4 + be 1f SAVE_ALLHere a nop is missing in the delay slot after "be 1f"
Here I just followed the example of the previous check that let the innocuous first instruction of SAVE_ALL be in the delay slot. But I have nothing against clarity here.
Quoted 30 lines
Quoted 30 lines
arch/sparc/kernel/entry.S +1
-1wr %l0, PSR_ET, %psr ! re-enable traps@@ -452,7 +456,7 @@ bad_instruction: RESTORE_ALL -1: /* unimplemented flush - just skip */ +1: /* unimplemented flush or probed CASA - just skip */ jmpl %l2, %g0 rett %l2 + 4arch/sparc/kernel/head_32.S +8
-0@@ -441,6 +441,14 @@ leon_init: /* Update boot_cpu_id only on boot cpu */ stub %g1, [%g2 + %lo(boot_cpu_id)] + /* Check if CASA is supported */ + set sparc32_cas_capable, %g1 + mov 1, %g2 + + .global leon_cas_check +leon_cas_check: + .word 0xc5e04160 /* casa [%g1] 0xb, %g0, %g2 */ + ba continue_boot
Quoted 8 lines
arch/sparc/kernel/head_32.S leon_init: · line 453
Quoted 1 line
arch/sparc/kernel/head_32.S +0
-0nopI could not follow this code-snippet.
Maybe this is my ignorance of the casa instruction. Will it store the value of %g2 (=1) in the address pointed by %g1 (sparc32_cas_capable) if casa is enabled? Maybe it is obvious for others, but it ws not for me. So one or two comments more...
Yes. I will add a comment to this.
/Andreas