Skip to content

Commit 28fc097

Browse files
committed
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: KSPP#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>
1 parent 32913fe commit 28fc097

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

arch/parisc/include/asm/bitops.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static __inline__ int test_and_change_bit(int nr, volatile unsigned long * addr)
123123
* cycles for each mispredicted branch.
124124
*/
125125

126-
static __inline__ unsigned long __ffs(unsigned long x)
126+
static __inline__ __attribute_const__ unsigned long __ffs(unsigned long x)
127127
{
128128
unsigned long ret;
129129

@@ -161,7 +161,7 @@ static __inline__ unsigned long __ffs(unsigned long x)
161161
* This is defined the same way as the libc and compiler builtin
162162
* ffs routines, therefore differs in spirit from the above ffz (man ffs).
163163
*/
164-
static __inline__ int ffs(int x)
164+
static __inline__ __attribute_const__ int ffs(int x)
165165
{
166166
return x ? (__ffs((unsigned long)x) + 1) : 0;
167167
}
@@ -171,7 +171,7 @@ static __inline__ int ffs(int x)
171171
* fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
172172
*/
173173

174-
static __inline__ int fls(unsigned int x)
174+
static __inline__ __attribute_const__ int fls(unsigned int x)
175175
{
176176
int ret;
177177
if (!x)

0 commit comments

Comments
 (0)