Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

parisc: 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 PARISC's implementations of
ffs(), __ffs(), 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=parisc defconfig with GCC hppa-linux-gnu 14.2.0.

Link: https://github.com/KSPP/linux/issues/364 [1]
Acked-by: Helge Deller <deller@gmx.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20250804164417.1612371-13-kees@kernel.org
Signed-off-by: Kees Cook <kees@kernel.org>

Kees Cook 28fc0972 32913fe7

+3 -3
+3 -3
arch/parisc/include/asm/bitops.h
··· 123 123 * cycles for each mispredicted branch. 124 124 */ 125 125 126 - static __inline__ unsigned long __ffs(unsigned long x) 126 + static __inline__ __attribute_const__ unsigned long __ffs(unsigned long x) 127 127 { 128 128 unsigned long ret; 129 129 ··· 161 161 * This is defined the same way as the libc and compiler builtin 162 162 * ffs routines, therefore differs in spirit from the above ffz (man ffs). 163 163 */ 164 - static __inline__ int ffs(int x) 164 + static __inline__ __attribute_const__ int ffs(int x) 165 165 { 166 166 return x ? (__ffs((unsigned long)x) + 1) : 0; 167 167 } ··· 171 171 * fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 172 172 */ 173 173 174 - static __inline__ int fls(unsigned int x) 174 + static __inline__ __attribute_const__ int fls(unsigned int x) 175 175 { 176 176 int ret; 177 177 if (!x)