[PATCH -next] x86/apic: Force logial APIC ID in range from 0 to 8

From: Xu Yihang
Date: Fri Apr 23 2021 - 03:53:33 EST


According to Intel 64 and IA-32 Architectures Software Developer’s Manuals
Vol. 3A 10.6.2.2, Logical APIC ID locates on bit 24 to 31, can only
support up to 8 local APIC under flat mode. Also C99 says left shift exceeding
maximum value representable in the result type is undefined behavior. But
under x86 architecture, it seems doesn't do any actual damage.

There is KASAN warning on a 80 cores machine after booting #64 CPU.
```
[ 1.681097] UBSAN: Undefined behaviour in arch/x86/kernel/apic/apic_flat_64.c:51:11
[ 1.688739] shift exponent 64 is too large for 64-bit type 'long unsigned int'
```

Signed-off-by: Xu Yihang <xuyihang@xxxxxxxxxx>
---
arch/x86/include/asm/apicdef.h | 1 +
arch/x86/kernel/apic/apic_flat_64.c | 2 +-
2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/apicdef.h b/arch/x86/include/asm/apicdef.h
index 5716f22f81ac..150a5e831368 100644
--- a/arch/x86/include/asm/apicdef.h
+++ b/arch/x86/include/asm/apicdef.h
@@ -42,6 +42,7 @@
#define APIC_RRR 0xC0
#define APIC_LDR 0xD0
#define APIC_LDR_MASK (0xFFu << 24)
+#define APIC_LOGICAL_ID_MAX 8
#define GET_APIC_LOGICAL_ID(x) (((x) >> 24) & 0xFFu)
#define SET_APIC_LOGICAL_ID(x) (((x) << 24))
#define APIC_ALL_CPUS 0xFFu
diff --git a/arch/x86/kernel/apic/apic_flat_64.c b/arch/x86/kernel/apic/apic_flat_64.c
index 8f72b4351c9f..7bf91cadee21 100644
--- a/arch/x86/kernel/apic/apic_flat_64.c
+++ b/arch/x86/kernel/apic/apic_flat_64.c
@@ -41,7 +41,7 @@ void flat_init_apic_ldr(void)
unsigned long num, id;

num = smp_processor_id();
- id = 1UL << num;
+ id = 1UL << (num % APIC_LOGICAL_ID_MAX);
apic_write(APIC_DFR, APIC_DFR_FLAT);
val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
val |= SET_APIC_LOGICAL_ID(id);
--
2.17.1