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

m68k: 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 M68K's implementations
of ffs(), __ffs(), fls(), __fls(), and ffz() 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=m68k defconfig with GCC m68k-linux-gnu 14.2.0.

Link: https://github.com/KSPP/linux/issues/364 [1]
Reviewed-by: Geert Uytterhoeven <geert@linux-m68k.org>
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20250804164417.1612371-11-kees@kernel.org
Signed-off-by: Kees Cook <kees@kernel.org>

Kees Cook 50c869a6 acfab97b

+7 -7
+7 -7
arch/m68k/include/asm/bitops.h
··· 465 465 * ffz = Find First Zero in word. Undefined if no zero exists, 466 466 * so code should check against ~0UL first.. 467 467 */ 468 - static inline unsigned long ffz(unsigned long word) 468 + static inline unsigned long __attribute_const__ ffz(unsigned long word) 469 469 { 470 470 int res; 471 471 ··· 488 488 */ 489 489 #if (defined(__mcfisaaplus__) || defined(__mcfisac__)) && \ 490 490 !defined(CONFIG_M68000) 491 - static inline unsigned long __ffs(unsigned long x) 491 + static inline __attribute_const__ unsigned long __ffs(unsigned long x) 492 492 { 493 493 __asm__ __volatile__ ("bitrev %0; ff1 %0" 494 494 : "=d" (x) ··· 496 496 return x; 497 497 } 498 498 499 - static inline int ffs(int x) 499 + static inline __attribute_const__ int ffs(int x) 500 500 { 501 501 if (!x) 502 502 return 0; ··· 518 518 * the libc and compiler builtin ffs routines, therefore 519 519 * differs in spirit from the above ffz (man ffs). 520 520 */ 521 - static inline int ffs(int x) 521 + static inline __attribute_const__ int ffs(int x) 522 522 { 523 523 int cnt; 524 524 ··· 528 528 return 32 - cnt; 529 529 } 530 530 531 - static inline unsigned long __ffs(unsigned long x) 531 + static inline __attribute_const__ unsigned long __ffs(unsigned long x) 532 532 { 533 533 return ffs(x) - 1; 534 534 } ··· 536 536 /* 537 537 * fls: find last bit set. 538 538 */ 539 - static inline int fls(unsigned int x) 539 + static inline __attribute_const__ int fls(unsigned int x) 540 540 { 541 541 int cnt; 542 542 ··· 546 546 return 32 - cnt; 547 547 } 548 548 549 - static inline unsigned long __fls(unsigned long x) 549 + static inline __attribute_const__ unsigned long __fls(unsigned long x) 550 550 { 551 551 return fls(x) - 1; 552 552 }