s390: Fix and optimize __flogr() inline assembly
Subsystems (2)
Heiko Carstens
maintainerv2 cover s390: Fix and optimize __flogr() inline assembly
v2:
- Split patches differently, so that the first patch is the actual fix, which addresses the reported warning / error. The subsequent patches are optimizations
v1:
https://lore.kernel.org/all/[email protected]/
A recent optimization of the s390 specific ffs() and ffs64() implementations leads to a new compiler warning. Instead of reverting the optimization address this with the rather new assume attribute, which generates even better code, if supported by compilers.
Since the assume attribute may be useful for others as well, add the __assume macro to compiler attributes, so it is kernel wide available, instead of adding an s390 specific optimization.
Heiko Carstens (4):
s390/bitops: Limit return value range of __flogr()
compiler_types: Add __assume macro
s390/bitops: Use __assume() for __flogr() inline assembly return value s390/bitops: Cleanup __flogr()
arch/s390/include/asm/bitops.h | 21 ++++++++++++++------- include/linux/compiler_types.h | 23 +++++++++++++++++++++++ init/Kconfig | 10 ++++++++++ 3 files changed, 47 insertions(+), 7 deletions(-)
Signature · 1 line
2.48.1
Heiko Carstens
maintainerPATCH v2 1/4 s390/bitops: Limit return value range of __flogr()
With the recent ffs() and ffs64() optimization a logical AND operation was removed, which allowed the compiler to tell the return value range of both functions. This may lead to compile warnings as reported by the kernel test robot:
drivers/infiniband/hw/mlx5/mr.c: In function 'mlx5r_cache_create_ent_locked':
Quoted 1 line
Quoted 1 line
drivers/infiniband/hw/mlx5/mr.c:840:31: warning: 'sprintf' may write a terminating nul past the end of the destination [-Wformat-overflow=]
840 | sprintf(ent->name, "%d", order);
| ^
In function 'mlx5_mkey_cache_debugfs_add_ent',
inlined from 'mlx5r_cache_create_ent_locked' at drivers/infiniband/hw/mlx5/mr.c:930:3:
drivers/infiniband/hw/mlx5/mr.c:840:9: note: 'sprintf' output between 2 and 5 bytes into a destination of size 4
840 | sprintf(ent->name, "%d", order);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Add the AND operation again to address the warning.
From a correctness point of view the AND operation is not necessary, however there is no other way to tell the compiler that the returned value of the flogr inline assembly is in the range of 0..64.
This increases the kernel image size by 566 bytes (defconfig, gcc 15.2.0).
--- arch/s390/include/asm/bitops.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/include/asm/bitops.h +1 -1
@@ -169,7 +169,7 @@ static __always_inline unsigned char __flogr(unsigned long word) asmvolatile (" flogr %[rp],%[rp]\n" : [rp]"+d" (rp.pair) : :"cc" ); -return rp.even; +return rp.even &127 ; } }
Signature · 1 line
2.48.1
Heiko Carstens
maintainerPATCH v2 3/4 s390/bitops: Use __assume() for __flogr() inline assembly return value
In reply to Heiko CarstensUse __assume() to tell compilers that the output operand of the __flogr() inline assembly contains a value in the range of 0..64. This allows to optimize the logical AND operation away.
This reduces the kernel image size by 2804 bytes (defconfig, gcc 15.2.0).
--- arch/s390/include/asm/bitops.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
arch/s390/include/asm/bitops.h +11 -3
@@ -132,9 +132,10 @@ static inline bool test_bit_inv(unsigned long nr,*/ static __always_inline unsigned char __flogr(unsigned long word) { -if (__builtin_constant_p (word)) { -unsigned long bit =0 ; +unsigned long bit; +if (__builtin_constant_p (word)) { + bit =0 ;if (!word)return 64 ;if (!(word &0xffffffff00000000UL )) {
@@ -169,7 +170,14 @@ static __always_inline unsigned char __flogr(unsigned long word) asmvolatile (" flogr %[rp],%[rp]\n" : [rp]"+d" (rp.pair) : :"cc" ); -return rp.even &127 ; + bit = rp.even; +/* +* The result of the flogr instruction is a value in the range +* of 0..64. Let the compiler know that the AND operation can +* be optimized away. +*/ +__assume (bit <=64 ); +return bit &127 ; } }
Signature · 1 line
2.48.1
Heiko Carstens
maintainerPATCH v2 2/4 compiler_types: Add __assume macro
In reply to Heiko CarstensMake the statement attribute "assume" with a new __assume macro available.
The assume attribute is used to indicate that a certain condition is assumed to be true. Compilers may or may not use this indication to generate optimized code. If this condition is violated at runtime, the behavior is undefined.
Note that the clang documentation states that optimizers may react differently to this attribute, and this may even have a negative performance impact. Therefore this attribute should be used with care.
--- include/linux/compiler_types.h | 23 +++++++++++++++++++++++ init/Kconfig | 10 ++++++++++ 2 files changed, 33 insertions(+)
include/linux/compiler_types.h +23 -0
@@ -329,6 +329,29 @@ struct ftrace_likely_data {#define __no_sanitize_or_inline __always_inline#endif +/* +* The assume attribute is used to indicate that a certain condition is +* assumed to be true. If this condition is violated at runtime, the behavior +* is undefined. Compilers may or may not use this indication to generate +* optimized code. +* +* Note that the clang documentation states that optimizers may react +* differently to this attribute, and this may even have a negative +* performance impact. Therefore this attribute should be used with care. +* +* Optional: only supported since gcc >= 13 +* Optional: only supported since clang >= 19 +* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#index-assume-statement-attribute +* clang: https://clang.llvm.org/docs/AttributeReference.html#id13 +* +*/ +#ifdef CONFIG_CC_HAS_ASSUME +# define __assume (expr) __attribute__((__assume__(expr))) +#else +# define __assume (expr) +#endif +/* * Optional: only supported since gcc >= 15 * Optional: only supported since clang >= 18
init/Kconfig +10 -0
@@ -112,6 +112,16 @@ config TOOLS_SUPPORT_RELR config CC_HAS_ASM_INLINE def_bool $(success,echo 'void foo(void) { asm inline (""); }' | $(CC) -x c - -c -o /dev/null) +config CC_HAS_ASSUME + bool + # clang needs to be at least 19.1.0 since the meaning of the assume + # attribute changed: + # https://github.com/llvm/llvm-project/commit/c44fa3e8a9a44c2e9a575768a3c185354b9f6c17 + default y if CC_IS_CLANG && CLANG_VERSION >= 190100 + # supported since gcc 13.1.0 + # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106654 + default y if CC_IS_GCC && GCC_VERSION >= 130100 + config CC_HAS_NO_PROFILE_FN_ATTR def_bool $(success,echo '__attribute__((no_profile_instrument_function)) int x();' | $(CC) -x c - -c -o /dev/null -Werror)
Signature · 1 line
2.48.1
Heiko Carstens
maintainerPATCH v2 4/4 s390/bitops: Cleanup __flogr()
In reply to Heiko CarstensThe flogr() inline assembly has no side effects and generates the same output if the input does not change. Therefore remove the volatile qualifier to allow the compiler to optimize the inline assembly away, if possible.
Also remove the superfluous '\n' which makes the inline assembly appear larger than it is according to compiler heuristics (number of lines).
Furthermore change the return type of flogr() to unsigned long and add the const attribute to the function.
This reduces the kernel image size by 994 bytes (defconfig, gcc 15.2.0).
--- arch/s390/include/asm/bitops.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
arch/s390/include/asm/bitops.h +3 -4
@@ -130,7 +130,7 @@ static inline bool test_bit_inv(unsigned long nr,* where the most significant bit has bit number 0. * If no bit is set this function returns 64. */ -static __always_inline unsigned char __flogr(unsigned long word) +static __always_inline __attribute_const__ unsigned long __flogr(unsigned long word) {unsigned long bit;
@@ -167,9 +167,8 @@ static __always_inline unsigned char __flogr(unsigned long word) union register_pair rp __uninitialized; rp.even = word; - asmvolatile ( -" flogr %[rp],%[rp]\n" - : [rp]"+d" (rp.pair) : :"cc" ); + asm("flogr %[rp],%[rp]" + : [rp]"+d" (rp.pair) : :"cc" ); bit = rp.even;/* * The result of the flogr instruction is a value in the range
Signature · 1 line
2.48.1
Re: [PATCH v2 2/4] compiler_types: Add __assume macro
In reply to Heiko CarstensQuoted 7 lines · Heiko Carstens
Make the statement attribute "assume" with a new
__assumemacro available.The assume attribute is used to indicate that a certain condition is assumed to be true. Compilers may or may not use this indication to generate optimized code. If this condition is violated at runtime, the behavior is undefined.
Quoted 5 lines
Note that the clang documentation states that optimizers may react differently to this attribute, and this may even have a negative performance impact. Therefore this attribute should be used with care.
Signed-off-by Heiko Carstens <[email protected]>
LGTM.
Quoted 63 lines
--- include/linux/compiler_types.h | 23 +++++++++++++++++++++++ init/Kconfig | 10 ++++++++++ 2 files changed, 33 insertions(+)include/linux/compiler_types.h +23
-0@@ -329,6 +329,29 @@ struct ftrace_likely_data {#define __no_sanitize_or_inline __always_inline#endif +/* +* The assume attribute is used to indicate that a certain condition is +* assumed to be true. If this condition is violated at runtime, the behavior +* is undefined. Compilers may or may not use this indication to generate +* optimized code. +* +* Note that the clang documentation states that optimizers may react +* differently to this attribute, and this may even have a negative +* performance impact. Therefore this attribute should be used with care. +* +* Optional: only supported since gcc >= 13 +* Optional: only supported since clang >= 19 +* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#index-assume-statement-attribute +* clang: https://clang.llvm.org/docs/AttributeReference.html#id13 +* +*/ +#ifdef CONFIG_CC_HAS_ASSUME +# define __assume (expr) __attribute__((__assume__(expr))) +#else +# define __assume (expr) +#endif +/* * Optional: only supported since gcc >= 15 * Optional: only supported since clang >= 18 init/Kconfig +10
-0@@ -112,6 +112,16 @@ config TOOLS_SUPPORT_RELR config CC_HAS_ASM_INLINE def_bool $(success,echo 'void foo(void) { asm inline (""); }' | $(CC) -x c - -c -o /dev/null) +config CC_HAS_ASSUME + bool + # clang needs to be at least 19.1.0 since the meaning of the assume + # attribute changed: + # https://github.com/llvm/llvm-project/commit/c44fa3e8a9a44c2e9a575768a3c185354b9f6c17 + default y if CC_IS_CLANG && CLANG_VERSION >= 190100 + # supported since gcc 13.1.0 + # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106654 + default y if CC_IS_GCC && GCC_VERSION >= 130100 + config CC_HAS_NO_PROFILE_FN_ATTR def_bool $(success,echo '__attribute__((no_profile_instrument_function)) int x();' | $(CC) -x c - -c -o /dev/null -Werror)Signature · 1 line
2.48.1
Quoted 15 lines · Heiko Carstens
A recent optimization of the s390 specific
ffs()andffs64()implementations leads to a new compiler warning. Instead of reverting the optimization address this with the rather new assume attribute, which generates even better code, if supported by compilers.Since the assume attribute may be useful for others as well, add the
__assumemacro to compiler attributes, so it is kernel wide available, instead of adding an s390 specific optimization.Heiko Carstens (4):
s390/bitops: Limit return value range of__flogr()
compiler_types: Add__assumemacro
s390/bitops: Use__assume()for__flogr()inline assembly return value s390/bitops: Cleanup__flogr()
Quoted 4 lines
arch/s390/include/asm/bitops.h | 21 ++++++++++++++------- include/linux/compiler_types.h | 23 +++++++++++++++++++++++ init/Kconfig | 10 ++++++++++ 3 files changed, 47 insertions(+), 7 deletions(-)
Applied, thanks!