arm64: Fix bit-shifting UB in the MIDR_CPU_MODEL() macro
PATCH v2 1/1 arm64: Fix bit-shifting UB in the MIDR_CPU_MODEL() macro
CONFIG_UBSAN_SHIFT with gcc-5 complains that the shifting of ARM_CPU_IMP_AMPERE (0xC0) into bits [31:24] by MIDR_CPU_MODEL() is undefined behavior. Well, sort of, it actually spells the error as:
arch/arm64/kernel/proton-pack.c: In function 'spectre_bhb_loop_affected':
arch/arm64/include/asm/cputype.h:44:2: error: initializer element is not constant
(((imp) << MIDR_IMPLEMENTOR_SHIFT) | \
^
This isn't an issue for other Implementor codes, as all the other codes have zero in the top bit and so are representable as a signed int.
Cast the implementor code to unsigned in MIDR_CPU_MODEL to remove the undefined behavior.
---
Changes since v1 (https://lore.kernel.org/all/[email protected]/):
- cast in MIDR_CPU_MODEL() instead of changing ARM_CPU_IMP_AMPERE
arch/arm64/include/asm/cputype.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/cputype.h +1 -1
@@ -41,7 +41,7 @@ (((midr) & MIDR_IMPLEMENTOR_MASK) >> MIDR_IMPLEMENTOR_SHIFT)#define MIDR_CPU_MODEL (imp, partnum) \ - (((imp) << MIDR_IMPLEMENTOR_SHIFT) | \ + ((_AT(u32, imp) << MIDR_IMPLEMENTOR_SHIFT) | \ (0xf << MIDR_ARCHITECTURE_SHIFT) | \ ((partnum) << MIDR_PARTNUM_SHIFT))
Signature · 7 lines
2.37.3 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 10 lines · D Scott Phillips
CONFIG_UBSAN_SHIFTwith gcc-5 complains that the shifting ofARM_CPU_IMP_AMPERE(0xC0) into bits [31:24] byMIDR_CPU_MODEL()is undefined behavior. Well, sort of, it actually spells the error as:
arch/arm64/kernel/proton-pack.c: In function 'spectre_bhb_loop_affected':
arch/arm64/include/asm/cputype.h:44:2: error: initializer element is not constant(((imp) << MIDR_IMPLEMENTOR_SHIFT) | \ ^[...]
Applied to arm64 (for-next/fixes), thanks!
[1/1] arm64: Fix bit-shifting UB in the MIDR_CPU_MODEL() macro
https://git.kernel.org/arm64/c/8ec8490a1950
Signature · 7 lines
Catalin _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel