Skip to content

Commit 50c869a

Browse files
committed
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: KSPP#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>
1 parent acfab97 commit 50c869a

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

arch/m68k/include/asm/bitops.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ static inline int find_next_bit(const unsigned long *vaddr, int size,
465465
* ffz = Find First Zero in word. Undefined if no zero exists,
466466
* so code should check against ~0UL first..
467467
*/
468-
static inline unsigned long ffz(unsigned long word)
468+
static inline unsigned long __attribute_const__ ffz(unsigned long word)
469469
{
470470
int res;
471471

@@ -488,15 +488,15 @@ static inline unsigned long ffz(unsigned long word)
488488
*/
489489
#if (defined(__mcfisaaplus__) || defined(__mcfisac__)) && \
490490
!defined(CONFIG_M68000)
491-
static inline unsigned long __ffs(unsigned long x)
491+
static inline __attribute_const__ unsigned long __ffs(unsigned long x)
492492
{
493493
__asm__ __volatile__ ("bitrev %0; ff1 %0"
494494
: "=d" (x)
495495
: "0" (x));
496496
return x;
497497
}
498498

499-
static inline int ffs(int x)
499+
static inline __attribute_const__ int ffs(int x)
500500
{
501501
if (!x)
502502
return 0;
@@ -518,7 +518,7 @@ static inline int ffs(int x)
518518
* the libc and compiler builtin ffs routines, therefore
519519
* differs in spirit from the above ffz (man ffs).
520520
*/
521-
static inline int ffs(int x)
521+
static inline __attribute_const__ int ffs(int x)
522522
{
523523
int cnt;
524524

@@ -528,15 +528,15 @@ static inline int ffs(int x)
528528
return 32 - cnt;
529529
}
530530

531-
static inline unsigned long __ffs(unsigned long x)
531+
static inline __attribute_const__ unsigned long __ffs(unsigned long x)
532532
{
533533
return ffs(x) - 1;
534534
}
535535

536536
/*
537537
* fls: find last bit set.
538538
*/
539-
static inline int fls(unsigned int x)
539+
static inline __attribute_const__ int fls(unsigned int x)
540540
{
541541
int cnt;
542542

@@ -546,7 +546,7 @@ static inline int fls(unsigned int x)
546546
return 32 - cnt;
547547
}
548548

549-
static inline unsigned long __fls(unsigned long x)
549+
static inline __attribute_const__ unsigned long __fls(unsigned long x)
550550
{
551551
return fls(x) - 1;
552552
}

0 commit comments

Comments
 (0)