[PATCH 1/6] x86/smpboot: Use CPU number instead of APIC ID for single CPU startup

Brian Gerst
2023-02-22T22:13:44Z

Encoding the CPU number directly in smpboot_control skips the APIC ID
lookup when booting a single CPU. This will enable the boot CPU to use
the same code as secondaries, since the APIC ID array is not populated
during early boot.

Signed-off-by: Brian Gerst <brgerst@gmail.com>
---
arch/x86/kernel/head_64.S | 12 +++++++-----
arch/x86/kernel/smpboot.c | 2 +-
2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S
index 0e4e53d231db..c1253aa737ca 100644
--- a/arch/x86/kernel/head_64.S
+++ b/arch/x86/kernel/head_64.S
@@ -252,20 +252,22 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
jz .Lsetup_cpu

/*
- * Secondary CPUs find out the offsets via the APIC ID. For parallel
- * boot the APIC ID is retrieved from CPUID, otherwise it's encoded
- * in smpboot_control:
+ * For parallel boot, the APIC ID is retrieved from CPUID, and then
+ * used to look up the CPU number. For booting a single CPU, the
+ * CPU number is encoded in smpboot_control.
+ *
* Bit 31 STARTUP_SECONDARY flag (checked above)
* Bit 30 STARTUP_APICID_CPUID_0B flag (use CPUID 0x0b)
* Bit 29 STARTUP_APICID_CPUID_01 flag (use CPUID 0x01)
- * Bit 0-24 APIC ID if STARTUP_APICID_CPUID_xx flags are not set
+ * Bit 0-24 CPU# if STARTUP_APICID_CPUID_xx flags are not set
*/
testl $STARTUP_APICID_CPUID_0B, %edx
jnz .Luse_cpuid_0b
testl $STARTUP_APICID_CPUID_01, %edx
jnz .Luse_cpuid_01
andl $0x0FFFFFFF, %edx
- jmp .Lsetup_AP
+ movl %edx, %ecx
+ jmp .Linit_cpu_data

.Luse_cpuid_01:
mov $0x01, %eax
diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index 5b6d72b3d14b..e1a2843c2841 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -1140,7 +1140,7 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle,
early_gdt_descr.address = (unsigned long)get_cpu_gdt_rw(cpu);
initial_stack = idle->thread.sp;
} else if (!do_parallel_bringup) {
- smpboot_control = STARTUP_SECONDARY | apicid;
+ smpboot_control = STARTUP_SECONDARY | cpu;
}

/* Enable the espfix hack for this CPU */
--
2.39.2