mirror of
https://github.com/torvalds/linux.git
synced 2025-11-01 17:18:25 +02:00
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 generic 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 with x86_64 defconfig using GCC 14.2.0, which should validate the implementations when used by ARM, ARM64, LoongArch, Microblaze, NIOS2, and SPARC32 architectures. Link: https://github.com/KSPP/linux/issues/364 [1] Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/r/20250804164417.1612371-2-kees@kernel.org Signed-off-by: Kees Cook <kees@kernel.org>
43 lines
979 B
C
43 lines
979 B
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* lib/clz_ctz.c
|
|
*
|
|
* Copyright (C) 2013 Chanho Min <chanho.min@lge.com>
|
|
*
|
|
* The functions in this file aren't called directly, but are required by
|
|
* GCC builtins such as __builtin_ctz, and therefore they can't be removed
|
|
* despite appearing unreferenced in kernel source.
|
|
*
|
|
* __c[lt]z[sd]i2 can be overridden by linking arch-specific versions.
|
|
*/
|
|
|
|
#include <linux/export.h>
|
|
#include <linux/kernel.h>
|
|
|
|
int __weak __ctzsi2(int val);
|
|
int __weak __attribute_const__ __ctzsi2(int val)
|
|
{
|
|
return __ffs(val);
|
|
}
|
|
EXPORT_SYMBOL(__ctzsi2);
|
|
|
|
int __weak __clzsi2(int val);
|
|
int __weak __attribute_const__ __clzsi2(int val)
|
|
{
|
|
return 32 - fls(val);
|
|
}
|
|
EXPORT_SYMBOL(__clzsi2);
|
|
|
|
int __weak __clzdi2(u64 val);
|
|
int __weak __attribute_const__ __clzdi2(u64 val)
|
|
{
|
|
return 64 - fls64(val);
|
|
}
|
|
EXPORT_SYMBOL(__clzdi2);
|
|
|
|
int __weak __ctzdi2(u64 val);
|
|
int __weak __attribute_const__ __ctzdi2(u64 val)
|
|
{
|
|
return __ffs64(val);
|
|
}
|
|
EXPORT_SYMBOL(__ctzdi2);
|