Skip to content

Commit 07008b9

Browse files
committed
sparc: Add __attribute_const__ to ffs()-family implementations
While tracking down a problem where constant expressions used by BUILD_BUG_ON() suddenly stopped working[1], we found that an added static initializer was convincing the compiler that it couldn't track the state of the prior statically initialized value. Tracing this down found that ffs() was used in the initializer macro, but since it wasn't marked with __attribute__const__, the compiler had to assume the function might change variable states as a side-effect (which is not true for ffs(), which provides deterministic math results). Add missing __attribute_const__ annotations to sparc64's implementations of ffs(), __ffs(), fls(), and __fls() functions. These are pure mathematical functions that always return the same result for the same input with no side effects, making them eligible for compiler optimization. Build tested ARCH=sparc defconfig with GCC sparc64-linux-gnu 14.2.0. Link: KSPP#364 [1] Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/r/20250804164417.1612371-16-kees@kernel.org Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 945fc9d commit 07008b9

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

arch/sparc/include/asm/bitops_64.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ void set_bit(unsigned long nr, volatile unsigned long *addr);
2323
void clear_bit(unsigned long nr, volatile unsigned long *addr);
2424
void change_bit(unsigned long nr, volatile unsigned long *addr);
2525

26-
int fls(unsigned int word);
27-
int __fls(unsigned long word);
26+
int __attribute_const__ fls(unsigned int word);
27+
int __attribute_const__ __fls(unsigned long word);
2828

2929
#include <asm-generic/bitops/non-atomic.h>
3030

3131
#include <asm-generic/bitops/fls64.h>
3232

3333
#ifdef __KERNEL__
3434

35-
int ffs(int x);
36-
unsigned long __ffs(unsigned long);
35+
int __attribute_const__ ffs(int x);
36+
unsigned long __attribute_const__ __ffs(unsigned long);
3737

3838
#include <asm-generic/bitops/ffz.h>
3939
#include <asm-generic/bitops/sched.h>

0 commit comments

Comments
 (0)