Re: [PATCH v2 05/47] arm64: cputype: Silence -Wshorten-64-to-32 warnings

From: Mark Rutland
Date: Fri May 09 2025 - 09:00:59 EST


On Wed, Apr 30, 2025 at 10:49:53AM -0700, Ian Rogers wrote:
> The clang warning -Wshorten-64-to-32 can be useful to catch
> inadvertent truncation. In some instances this truncation can lead to
> changing the sign of a result, for example, truncation to return an
> int to fit a sort routine. Silence the warning by making the implicit
> truncation explicit.
>
> Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
> ---
> tools/arch/arm64/include/asm/cputype.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/arch/arm64/include/asm/cputype.h b/tools/arch/arm64/include/asm/cputype.h
> index 488f8e751349..d7289b9d2758 100644
> --- a/tools/arch/arm64/include/asm/cputype.h
> +++ b/tools/arch/arm64/include/asm/cputype.h
> @@ -227,7 +227,7 @@
>
> #include <asm/sysreg.h>
>
> -#define read_cpuid(reg) read_sysreg_s(SYS_ ## reg)
> +#define read_cpuid(reg) ((u32)read_sysreg_s(SYS_ ## reg))

This isn't right.

Architecturally, system registers are 64-bit wide, and some of the ID
registers have allocated fields in the upper 32 bits, e.g. in MPIDR,
where we this will silently discard those when accessed via read_cpuid_mpidr():

static inline u64 __attribute_const__ read_cpuid_mpidr(void)
{
return read_cpuid(MPIDR_EL1);
}

Mark.