x86/topology: Handle the !APIC case gracefully
authorThomas Gleixner <tglx@linutronix.de>
Fri, 22 Mar 2024 18:56:38 +0000 (19:56 +0100)
committerBorislav Petkov (AMD) <bp@alien8.de>
Sat, 23 Mar 2024 11:35:56 +0000 (12:35 +0100)
If there is no local APIC enumerated and registered then the topology
bitmaps are empty. Therefore, topology_init_possible_cpus() will die with
a division by zero exception.

Prevent this by registering a fake APIC id to populate the topology
bitmap. This also allows to use all topology query interfaces
unconditionally. It does not affect the actual APIC code because either
the local APIC address was not registered or no local APIC could be
detected.

Fixes: f1f758a80516 ("x86/topology: Add a mechanism to track topology via APIC IDs")
Reported-by: Guenter Roeck <linux@roeck-us.net>
Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/r/20240322185305.242709302@linutronix.de
arch/x86/kernel/cpu/topology.c

index 3259b1d4fefe3c370e0d57e697388fa8468e6fff..aaca8d235dc2bbee08ab6de2bdb91b231963a7f6 100644 (file)
@@ -415,6 +415,17 @@ void __init topology_init_possible_cpus(void)
        unsigned int total = assigned + disabled;
        u32 apicid, firstid;
 
+       /*
+        * If there was no APIC registered, then fake one so that the
+        * topology bitmap is populated. That ensures that the code below
+        * is valid and the various query interfaces can be used
+        * unconditionally. This does not affect the actual APIC code in
+        * any way because either the local APIC address has not been
+        * registered or the local APIC was disabled on the command line.
+        */
+       if (topo_info.boot_cpu_apic_id == BAD_APICID)
+               topology_register_boot_apic(0);
+
        if (!restrict_to_up()) {
                if (WARN_ON_ONCE(assigned > nr_cpu_ids)) {
                        disabled += assigned - nr_cpu_ids;