s390/percpu: More small improvements
Subsystems (2)
Heiko Carstens
maintainerPATCH 1/4 s390/percpu: Use asm_inline for arch_this_cpu_add()
In reply to Heiko CarstensWhen converting to preemptible percpu operations the arch_this_cpu_add() inline assemblies were not converted to use asm_inline instead of a plain asm statement.
With all the macro usage the inline assemblies grow significantly, and the compiler may decide to not inline even small functions. This can be observed with gcc16 and a defconfig build where many calls to __srcu_read_lock_fast() are not inlined anymore, even though it is basically only a this_cpu_inc().
Use asm_inline to address this.
--- arch/s390/include/asm/percpu.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/s390/include/asm/percpu.h +2 -2
@@ -193,7 +193,7 @@ do { \ ptr__ =PERCPU_PTR (&(pcp)); \if (__builtin_constant_p (val__) && \ ((szcast )val__ >-129 ) && ((szcast )val__ <128 )) { \ - asm volatile( \ + asm_inline volatile( \ __PCPU_BEGIN("%[lcreg]","%[lcoff]","%[ptr__]", \ "%[pair__]","%N[pair__]") \ op2" 0(%N[pair__]),%[val__]\n" \
@@ -206,7 +206,7 @@ do { \ "m" (((struct lowcore *)0)->percpu_offset) \ : "cc"); \ } else { \ - asm volatile( \ + asm_inline volatile( \ __PCPU_BEGIN("%[lcreg]","%[lcoff]","%[ptr__]", \ "%[pair__]","%N[pair__]") \ op1 " %[old__],%[val__],0(%N[pair__])\n" \
Signature · 1 line
2.53.0
Heiko Carstens
maintainercover s390/percpu: More small improvements
Some more improvements for the preemptible percpu code. The missing asm_inline conversion addressed with the first patch is the most important one. While I first thought that it is a compiler glitch that some simple this_cpu operations are not inlined anymore it was a simple asm_inline vs asm in front of percpu inline assemblies which caused this. With this addressed inlining works again like expected.
Two more patches reduce register pressure for inline assemblies. The last patch implements this_cpu_try_cmpxchg() for s390 so the compiler can generate slightly better code similar to try_cmpxchg().
Series applies on top of linux-next 20261008.
Thanks,
Heiko
Heiko Carstens (4):
s390/percpu: Use asm_inline for arch_this_cpu_add() s390/percpu: Drop unused output register from arch_this_cpu_add() s390/percpu: Drop unused output register from arch_this_cpu_to_op() s390/percpu: Implement this_cpu_try_cmpxchg()
arch/s390/include/asm/percpu.h | 59 ++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 10 deletions(-)
base-commit: aac26bee2287c88af5be5a5ff96d783b19a28790
Signature · 1 line
2.53.0
Heiko Carstens
maintainerPATCH 2/4 s390/percpu: Drop unused output register from arch_this_cpu_add()
The laa/laag instructions return the old memory content, but arch_this_cpu_add() does not need it. Instead of allocating a dedicated output register old__ just to discard the returned value, change the val__ constraint from "d" to "+&d" so it is used as input/output register.
This reduces register pressure.
--- arch/s390/include/asm/percpu.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/s390/include/asm/percpu.h +4 -4
@@ -188,7 +188,7 @@ do { \typedef typeof (pcp)pcp_op_T__ ; \union register_pair rp__; \pcp_op_T__ val__ = (val); \ -pcp_op_T__ old__, *ptr__; \ +pcp_op_T__ *ptr__; \ \ ptr__ =PERCPU_PTR (&(pcp)); \ if (__builtin_constant_p(val__) && \
@@ -209,12 +209,12 @@ do { \ asm_inline volatile( \ __PCPU_BEGIN("%[lcreg]","%[lcoff]","%[ptr__]", \ "%[pair__]","%N[pair__]") \ - op1 " %[old__],%[val__],0(%N[pair__])\n" \ + op1 " %[val__],%[val__],0(%N[pair__])\n" \ __PCPU_END("%[lcreg]") \ - : [old__] "=&d" (old__), [pair__] "=&a" (rp__.pair), \ + : [val__] "+&d" (val__), [pair__] "=&a" (rp__.pair), \ "+m" (*ptr__), \ "=m" (((struct lowcore *)0)->percpu_register) \ - : [val__] "d" (val__), [ptr__] "a" (ptr__), \ + : [ptr__] "a" (ptr__), \[lcreg] " i" (LC_PERCPU_REGISTER), \ [lcoff] " i" (LC_PERCPU_OFFSET), \ " m" (((struct lowcore *)0)->percpu_offset) \
Signature · 1 line
2.53.0
Heiko Carstens
maintainerPATCH 3/4 s390/percpu: Drop unused output register from arch_this_cpu_to_op()
In reply to Heiko CarstensThe lan/lang/lao/laog instructions return the old memory content, but arch_this_cpu_to_op() does not need it. Instead of allocating a dedicated output register old__ just to discard the returned value, change the val__ constraint from "d" to "+&d" so it is used as input/output register.
This reduces register pressure.
--- arch/s390/include/asm/percpu.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/s390/include/asm/percpu.h +4 -4
@@ -257,18 +257,18 @@ do { \typedef typeof (pcp)pcp_op_T__ ; \union register_pair rp__; \pcp_op_T__ val__ = (val); \ -pcp_op_T__ old__, *ptr__; \ +pcp_op_T__ *ptr__; \ \ ptr__ =PERCPU_PTR (&(pcp)); \ asm_inline volatile( \ __PCPU_BEGIN("%[lcreg]","%[lcoff]","%[ptr__]", \ "%[pair__]","%N[pair__]") \ - op" %[old__],%[val__],0(%N[pair__])\n" \ + op" %[val__],%[val__],0(%N[pair__])\n" \ __PCPU_END("%[lcreg]" ) \ - : [old__] "=&d" (old__), [pair__] "=&a" (rp__.pair), \ + : [val__] "+&d" (val__), [pair__] "=&a" (rp__.pair), \ "+m" (*ptr__), \ "=m" (((struct lowcore *)0)->percpu_register) \ -: [val__] " d" (val__), [ptr__] " a" (ptr__), \ +: [ptr__] " a" (ptr__), \ [lcreg] " i" (LC_PERCPU_REGISTER), \ [lcoff] " i" (LC_PERCPU_OFFSET), \ " m" (((struct lowcore *)0)->percpu_offset) \
Signature · 1 line
2.53.0
Heiko Carstens
maintainerPATCH 4/4 s390/percpu: Implement this_cpu_try_cmpxchg()
In reply to Heiko CarstensProvide an s390 specific implementation of this_cpu_try_cmpxchg() for the four and eight byte cases, if the compiler supports flag output operands.
This variant uses the condition code set by cs/csg, which avoids a compare instruction required by the generic variant and generates slightly better code.
--- arch/s390/include/asm/percpu.h | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
arch/s390/include/asm/percpu.h +39 -0
@@ -5,6 +5,7 @@#include <linux/preempt.h> #include <asm/cmpxchg.h> #include <asm/march.h> +#include <asm/asm.h> /* * s390 uses its own implementation for per cpu data, the offset of
@@ -404,6 +405,44 @@ do { \ old__; \ }) +#ifdef __HAVE_ASM_FLAG_OUTPUTS__ + +#define arch_this_cpu_try_cmpxchg (pcp, ovalp, nval, op) \ +({ \ + typedef typeof(pcp) pcp_op_T__; \ + typeof(ovalp) ovalp__ = (ovalp); \ + pcp_op_T__ old__ = *ovalp__; \ + pcp_op_T__ new__ = (nval); \ + union register_pair rp__; \ + pcp_op_T__ *ptr__; \ + int cc__; \ + \ + ptr__ = PERCPU_PTR(&(pcp)); \ + asm_inline volatile( \ + __PCPU_BEGIN("%[lcreg]","%[lcoff]","%[ptr__]", \ + "%[pair__]","%N[pair__]") \ + op " %[old__],%[new__],0(%N[pair__])\n" \ + __PCPU_END("%[lcreg]") \ + : [old__] "+&d" (old__), [pair__] "=&a" (rp__.pair), \ + "+m" (*ptr__), "=@cc" (cc__), \ + "=m" (((struct lowcore *)0)->percpu_register) \ + : [new__] "d" (new__), [ptr__] "a" (ptr__), \ + [lcreg] "i" (LC_PERCPU_REGISTER), \ + [lcoff] "i" (LC_PERCPU_OFFSET), \ + "m" (((struct lowcore *)0)->percpu_offset) \ + : "memory"); \ + if (unlikely(cc__)) \ + *ovalp__ = old__; \ + likely(cc__ == 0); \ +}) + +#define this_cpu_try_cmpxchg_4 (pcp, ovalp, nval) arch_this_cpu_try_cmpxchg(pcp, ovalp, nval, "cs") +#define this_cpu_try_cmpxchg_8 (pcp, ovalp, nval) arch_this_cpu_try_cmpxchg(pcp, ovalp, nval, "csg") + +#define this_cpu_try_cmpxchg64 (pcp, ovalp, nval) this_cpu_try_cmpxchg_8(pcp, ovalp, nval) + +#endif /* __HAVE_ASM_FLAG_OUTPUTS__ */ +#define arch_this_cpu_xchg_simple (pcp, nval) \ ({ \ typeof(pcp) *ptr__; \
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 1/4] s390/percpu: Use asm_inline for arch_this_cpu_add()
In reply to Heiko CarstensSashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
When converting to preemptible percpu operations the
arch_this_cpu_add()inline assemblies were not converted to useasm_inlineinstead of a plain asm statement.With all the macro usage the inline assemblies grow significantly, and the compiler may decide to not inline even small functions. This can be observed with gcc16 and a defconfig build where many calls to
__srcu_read_lock_fast()are not inlined anymore, even though it is basically only athis_cpu_inc().
Quoted 4 lines
Use
asm_inlineto address this.Fixes 18ec6c5d7ec6 ("s390/percpu: Use new percpu code section for arch_this_cpu_add()")Signed-off-by Heiko Carstens <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH 2/4] s390/percpu: Drop unused output register from arch_this_cpu_add()
In reply to Heiko CarstensSashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
The laa/laag instructions return the old memory content, but
arch_this_cpu_add()does not need it. Instead of allocating a dedicated output registerold__just to discard the returned value, change theval__constraint from "d" to "+&d" so it is used as input/output register.This reduces register pressure.
Signed-off-by Heiko Carstens <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH 4/4] s390/percpu: Implement this_cpu_try_cmpxchg()
In reply to Heiko CarstensSashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
Provide an s390 specific implementation of
this_cpu_try_cmpxchg()for the four and eight byte cases, if the compiler supports flag output operands.This variant uses the condition code set by cs/csg, which avoids a compare instruction required by the generic variant and generates slightly better code.
Signed-off-by Heiko Carstens <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH 3/4] s390/percpu: Drop unused output register from arch_this_cpu_to_op()
In reply to Heiko CarstensThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] s390/percpu: unpredictable LAN/LAO behavior from R1==R3 --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] s390/percpu: unpredictable LAN/LAO behavior from R1==R3
--
commit 657f51d5e5b9873bdbdaca586024d213577edd7b
Author: Heiko Carstens <[email protected]>
s390/percpu: Drop unused output register from arch_this_cpu_to_op()
The lan/lang/lao/laog instructions return the old memory content, but arch_this_cpu_to_op() does not need it. This commit changes the val__ constraint to use it as an input/output register instead of a dedicated old__ register, aiming to reduce register pressure.
Quoted 7 lines
arch/s390/include/asm/percpu.h +0
-0@@ -257,18 +257,18 @@ do { \typedef typeof (pcp)pcp_op_T__ ; \union register_pair rp__; \
arch/s390/include/asm/percpu.h do { \ · line 266
Quoted 10 lines
arch/s390/include/asm/percpu.h +2
-2pcp_op_T__ val__ = (val); \ -pcp_op_T__ old__, *ptr__; \ +pcp_op_T__ *ptr__; \ \ ptr__ =PERCPU_PTR (&(pcp)); \ asm_inline volatile( \ __PCPU_BEGIN("%[lcreg]","%[lcoff]","%[ptr__]", \ "%[pair__]","%N[pair__]") \ - op" %[old__],%[val__],0(%N[pair__])\n" \ + op" %[val__],%[val__],0(%N[pair__])\n" \
[Severity: High]
Does this inline assembly string force the compiler to emit the atomic instruction using the identical register for both the r1 and r3 operands?
According to the z/Architecture Principles of Operation, if r1 and r3 designate the same register in laa, lan, and lao instructions, the results are unpredictable.
By hardcoding %[val__],%[val__], the exact same register is used for both r1 (the output destination for the old value) and r3 (the input addend/mask). Does this emit unpredictable assembly for invocations of this_cpu_and_* or this_cpu_or_*?
Also, arch_this_cpu_add() was modified similarly just one commit earlier in this series ("s390/percpu: Drop unused output register from arch_this_cpu_add()"). Does that macro have the same unpredictability regression for laa?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Heiko Carstens
maintainerRe: [PATCH 3/4] s390/percpu: Drop unused output register from arch_this_cpu_to_op()
Quoted 36 lines · On Mon, Oct 12, 2026 at 11:54:14AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] s390/percpu: unpredictable LAN/LAO behavior from R1==R3
--commit
657f51d5e5b9873bdbdaca586024d213577edd7b
Author: Heiko Carstens <[email protected]>s390/percpu: Drop unused output register from
arch_this_cpu_to_op()The lan/lang/lao/laog instructions return the old memory content, but
arch_this_cpu_to_op()does not need it. This commit changes theval__constraint to use it as an input/output register instead of a dedicatedold__register, aiming to reduce register pressure.Quoted 7 lines
arch/s390/include/asm/percpu.h +0
-0@@ -257,18 +257,18 @@ do { \typedef typeof (pcp)pcp_op_T__ ; \union register_pair rp__; \arch/s390/include/asm/percpu.h do { \ · line 266
Quoted 10 lines
arch/s390/include/asm/percpu.h +2
-2pcp_op_T__ val__ = (val); \ -pcp_op_T__ old__, *ptr__; \ +pcp_op_T__ *ptr__; \ \ ptr__ =PERCPU_PTR (&(pcp)); \ asm_inline volatile( \ __PCPU_BEGIN("%[lcreg]","%[lcoff]","%[ptr__]", \ "%[pair__]","%N[pair__]") \ - op" %[old__],%[val__],0(%N[pair__])\n" \ + op" %[val__],%[val__],0(%N[pair__])\n" \[Severity: High]
Does this inline assembly string force the compiler to emit the atomic instruction using the identical register for both the r1 and r3 operands?
Quoted 3 lines
According to the z/Architecture Principles of Operation, if r1 and r3 designate the same register in laa, lan, and lao instructions, the results are unpredictable.
Not true. According to the PoP this behaviour is well defined. See "Programming Notes" on page 7-306 in SA22-7832-14.
Re: [PATCH 1/4] s390/percpu: Use asm_inline for arch_this_cpu_add()
In reply to Heiko CarstensQuoted 10 lines · Heiko Carstens
When converting to preemptible percpu operations the
arch_this_cpu_add()inline assemblies were not converted to useasm_inlineinstead of a plain asm statement.With all the macro usage the inline assemblies grow significantly, and the compiler may decide to not inline even small functions. This can be observed with gcc16 and a defconfig build where many calls to
__srcu_read_lock_fast()are not inlined anymore, even though it is basically only athis_cpu_inc().
Quoted 7 lines
Use
asm_inlineto address this.Fixes 18ec6c5d7ec6 ("s390/percpu: Use new percpu code section for arch_this_cpu_add()")Signed-off-by Heiko Carstens <[email protected]>--- arch/s390/include/asm/percpu.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Alexander Gordeev
maintainerRe: [PATCH 2/4] s390/percpu: Drop unused output register from arch_this_cpu_add()
In reply to Heiko CarstensQuoted 7 lines · Heiko Carstens
The laa/laag instructions return the old memory content, but
arch_this_cpu_add()does not need it. Instead of allocating a dedicated output registerold__just to discard the returned value, change theval__constraint from "d" to "+&d" so it is used as input/output register.This reduces register pressure.
Do you know whether the compiler is may be smart enough to select the same register for old and val himself?
My concern is if we force the compiler to do so we might lose optimiztions where the val could be reused after the op.
(CC-ed Ilya)
Quoted 37 lines
Signed-off-by Heiko Carstens <[email protected]>--- arch/s390/include/asm/percpu.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)arch/s390/include/asm/percpu.h +4
-4@@ -188,7 +188,7 @@ do { \typedef typeof (pcp)pcp_op_T__ ; \union register_pair rp__; \pcp_op_T__ val__ = (val); \ -pcp_op_T__ old__, *ptr__; \ +pcp_op_T__ *ptr__; \ \ ptr__ =PERCPU_PTR (&(pcp)); \ if (__builtin_constant_p(val__) && \@@ -209,12 +209,12 @@ do { \ asm_inline volatile( \ __PCPU_BEGIN("%[lcreg]","%[lcoff]","%[ptr__]", \ "%[pair__]","%N[pair__]") \ - op1 " %[old__],%[val__],0(%N[pair__])\n" \ + op1 " %[val__],%[val__],0(%N[pair__])\n" \ __PCPU_END("%[lcreg]") \ - : [old__] "=&d" (old__), [pair__] "=&a" (rp__.pair), \ + : [val__] "+&d" (val__), [pair__] "=&a" (rp__.pair), \ "+m" (*ptr__), \ "=m" (((struct lowcore *)0)->percpu_register) \ - : [val__] "d" (val__), [ptr__] "a" (ptr__), \ + : [ptr__] "a" (ptr__), \[lcreg] " i" (LC_PERCPU_REGISTER), \ [lcoff] " i" (LC_PERCPU_OFFSET), \ " m" (((struct lowcore *)0)->percpu_offset) \ Signature · 1 line
2.53.0
Heiko Carstens
maintainerRe: [PATCH 2/4] s390/percpu: Drop unused output register from arch_this_cpu_add()
Quoted 11 lines · Alexander Gordeev
Quoted 7 lines · Heiko Carstens
The laa/laag instructions return the old memory content, but
arch_this_cpu_add()does not need it. Instead of allocating a dedicated output registerold__just to discard the returned value, change theval__constraint from "d" to "+&d" so it is used as input/output register.This reduces register pressure.
Do you know whether the compiler is may be smart enough to select the same register for old and val himself?
In theory, if the constraints would allow that, yes. But for the preemptible percpu inline asm all output operands need to be early clobbered. Therefore gcc is forced to not do that.
Quoted 2 lines
My concern is if we force the compiler to do so we might lose optimiztions where the val could be reused after the op.
The overall size change of the kernel image is six bytes :). I guess we shouldn't spend too much time with this.
Re: [PATCH 2/4] s390/percpu: Drop unused output register from arch_this_cpu_add()
In reply to Heiko CarstensQuoted 6 lines · Heiko Carstens
The laa/laag instructions return the old memory content, but
arch_this_cpu_add()does not need it. Instead of allocating a dedicated output registerold__just to discard the returned value, change theval__constraint from "d" to "+&d" so it is used as input/output register.
Quoted 6 lines
This reduces register pressure.
Signed-off-by Heiko Carstens <[email protected]>--- arch/s390/include/asm/percpu.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
Re: [PATCH 3/4] s390/percpu: Drop unused output register from arch_this_cpu_to_op()
In reply to Heiko CarstensQuoted 6 lines · Heiko Carstens
The lan/lang/lao/laog instructions return the old memory content, but
arch_this_cpu_to_op()does not need it. Instead of allocating a dedicated output registerold__just to discard the returned value, change theval__constraint from "d" to "+&d" so it is used as input/output register.
Quoted 6 lines
This reduces register pressure.
Signed-off-by Heiko Carstens <[email protected]>--- arch/s390/include/asm/percpu.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
Re: [PATCH 4/4] s390/percpu: Implement this_cpu_try_cmpxchg()
In reply to Heiko CarstensQuoted 4 lines · Heiko Carstens
Provide an s390 specific implementation of
this_cpu_try_cmpxchg()for the four and eight byte cases, if the compiler supports flag output operands.
Quoted 8 lines
This variant uses the condition code set by cs/csg, which avoids a compare instruction required by the generic variant and generates slightly better code.
Signed-off-by Heiko Carstens <[email protected]>--- arch/s390/include/asm/percpu.h | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
Re: [PATCH 2/4] s390/percpu: Drop unused output register from arch_this_cpu_add()
In reply to Alexander GordeevQuoted 11 lines · On Mon, 12 Oct 2026 15:24:20 +0200 Alexander Gordeev <[email protected]>…
Quoted 7 lines · Heiko Carstens
The laa/laag instructions return the old memory content, but
arch_this_cpu_add()does not need it. Instead of allocating a dedicated output registerold__just to discard the returned value, change theval__constraint from "d" to "+&d" so it is used as input/output register.This reduces register pressure.
Do you know whether the compiler is may be smart enough to select the same register for old and val himself?
Given gcc tends to allocate a new register for x++ I doubt it (and then has to do a reg-reg move to put it back).
David