All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH -next] x86/apic: Force logial APIC ID in range from 0 to 8
@ 2021-04-23  7:53 Xu Yihang
  2021-04-23  8:37 ` Thomas Gleixner
  0 siblings, 1 reply; 4+ messages in thread
From: Xu Yihang @ 2021-04-23  7:53 UTC (permalink / raw)
  To: mingo, bp, x86, hpa, dwmw, linux-kernel; +Cc: xuyihang

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@huawei.com>
---
 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-04-26 11:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-23  7:53 [PATCH -next] x86/apic: Force logial APIC ID in range from 0 to 8 Xu Yihang
2021-04-23  8:37 ` Thomas Gleixner
2021-04-26  2:09   ` xuyihang
2021-04-26 11:09     ` Borislav Petkov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.