]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
arch/x86: Provide the CPU number in the wakeup AP callback
authorRoman Kisel <romank@linux.microsoft.com>
Wed, 7 May 2025 18:22:26 +0000 (11:22 -0700)
committerWei Liu <wei.liu@kernel.org>
Fri, 23 May 2025 16:30:56 +0000 (16:30 +0000)
When starting APs, confidential guests and paravisor guests
need to know the CPU number, and the pattern of using the linear
search has emerged in several places. With N processors that leads
to the O(N^2) time complexity.

Provide the CPU number in the AP wake up callback so that one can
get the CPU number in constant time.

Suggested-by: Michael Kelley <mhklinux@outlook.com>
Signed-off-by: Roman Kisel <romank@linux.microsoft.com>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Michael Kelley <mhklinux@outlook.com>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Link: https://lore.kernel.org/r/20250507182227.7421-3-romank@linux.microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
Message-ID: <20250507182227.7421-3-romank@linux.microsoft.com>

arch/x86/coco/sev/core.c
arch/x86/hyperv/hv_vtl.c
arch/x86/hyperv/ivm.c
arch/x86/include/asm/apic.h
arch/x86/include/asm/mshyperv.h
arch/x86/kernel/acpi/madt_wakeup.c
arch/x86/kernel/apic/apic_noop.c
arch/x86/kernel/apic/apic_numachip.c
arch/x86/kernel/apic/x2apic_uv_x.c
arch/x86/kernel/smpboot.c

index b0c1a7a574974a297b3078a9c9de942e1dc3434b..7780d55d18336c4faa3266401ed3bf96ec5d488b 100644 (file)
@@ -1177,7 +1177,7 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa, int apic_id)
                free_page((unsigned long)vmsa);
 }
 
-static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
+static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip, unsigned int cpu)
 {
        struct sev_es_save_area *cur_vmsa, *vmsa;
        struct ghcb_state state;
@@ -1185,7 +1185,7 @@ static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
        unsigned long flags;
        struct ghcb *ghcb;
        u8 sipi_vector;
-       int cpu, ret;
+       int ret;
        u64 cr4;
 
        /*
@@ -1206,15 +1206,6 @@ static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
 
        /* Override start_ip with known protected guest start IP */
        start_ip = real_mode_header->sev_es_trampoline_start;
-
-       /* Find the logical CPU for the APIC ID */
-       for_each_present_cpu(cpu) {
-               if (arch_match_cpu_phys_id(cpu, apic_id))
-                       break;
-       }
-       if (cpu >= nr_cpu_ids)
-               return -EINVAL;
-
        cur_vmsa = per_cpu(sev_vmsa, cpu);
 
        /*
index 2f32ac1ae40eab0d2f308196c94f94874a7ee59b..3d149a2ca4c87ce8ed41cb1b32279c7837869559 100644 (file)
@@ -211,17 +211,9 @@ free_lock:
        return ret;
 }
 
-static int hv_vtl_wakeup_secondary_cpu(u32 apicid, unsigned long start_eip)
+static int hv_vtl_wakeup_secondary_cpu(u32 apicid, unsigned long start_eip, unsigned int cpu)
 {
-       int vp_index, cpu;
-
-       /* Find the logical CPU for the APIC ID */
-       for_each_present_cpu(cpu) {
-               if (arch_match_cpu_phys_id(cpu, apicid))
-                       break;
-       }
-       if (cpu >= nr_cpu_ids)
-               return -EINVAL;
+       int vp_index;
 
        pr_debug("Bringing up CPU with APIC ID %d in VTL2...\n", apicid);
        vp_index = hv_apicid_to_vp_index(apicid);
index 0cc239cdb4dad87cd6a518fcb19a19f0d8bf8d22..e21557b24d19279d28c3279e8e78faf8deb8c162 100644 (file)
@@ -289,7 +289,7 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa)
                free_page((unsigned long)vmsa);
 }
 
-int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip)
+int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu)
 {
        struct sev_es_save_area *vmsa = (struct sev_es_save_area *)
                __get_free_page(GFP_KERNEL | __GFP_ZERO);
@@ -298,7 +298,7 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip)
        u64 ret, retry = 5;
        struct hv_enable_vp_vtl *start_vp_input;
        unsigned long flags;
-       int cpu, vp_index;
+       int vp_index;
 
        if (!vmsa)
                return -ENOMEM;
@@ -308,17 +308,6 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip)
        if (vp_index < 0 || vp_index > ms_hyperv.max_vp_index)
                return -EINVAL;
 
-       /*
-        * Find the Linux CPU number for addressing the per-CPU data, and it
-        * might not be the same as APIC ID.
-        */
-       for_each_present_cpu(cpu) {
-               if (arch_match_cpu_phys_id(cpu, apic_id))
-                       break;
-       }
-       if (cpu >= nr_cpu_ids)
-               return -EINVAL;
-
        native_store_gdt(&gdtr);
 
        vmsa->gdtr.base = gdtr.address;
index c903d358405d389b808c6075cf6bfa6032710c2a..eaf43d44620357cc7d525cc033bfd5009544b264 100644 (file)
@@ -313,9 +313,9 @@ struct apic {
        u32     (*get_apic_id)(u32 id);
 
        /* wakeup_secondary_cpu */
-       int     (*wakeup_secondary_cpu)(u32 apicid, unsigned long start_eip);
+       int     (*wakeup_secondary_cpu)(u32 apicid, unsigned long start_eip, unsigned int cpu);
        /* wakeup secondary CPU using 64-bit wakeup point */
-       int     (*wakeup_secondary_cpu_64)(u32 apicid, unsigned long start_eip);
+       int     (*wakeup_secondary_cpu_64)(u32 apicid, unsigned long start_eip, unsigned int cpu);
 
        char    *name;
 };
@@ -333,8 +333,8 @@ struct apic_override {
        void    (*send_IPI_self)(int vector);
        u64     (*icr_read)(void);
        void    (*icr_write)(u32 low, u32 high);
-       int     (*wakeup_secondary_cpu)(u32 apicid, unsigned long start_eip);
-       int     (*wakeup_secondary_cpu_64)(u32 apicid, unsigned long start_eip);
+       int     (*wakeup_secondary_cpu)(u32 apicid, unsigned long start_eip, unsigned int cpu);
+       int     (*wakeup_secondary_cpu_64)(u32 apicid, unsigned long start_eip, unsigned int cpu);
 };
 
 /*
index 0b9a3a307d06554452131709fbeb501cc08d09e5..5ec92e3e2e37655af0604cd6259f0fbf92c51c58 100644 (file)
@@ -268,11 +268,12 @@ int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
 #ifdef CONFIG_AMD_MEM_ENCRYPT
 bool hv_ghcb_negotiate_protocol(void);
 void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason);
-int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip);
+int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu);
 #else
 static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
 static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
-static inline int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip) { return 0; }
+static inline int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip,
+               unsigned int cpu) { return 0; }
 #endif
 
 #if defined(CONFIG_AMD_MEM_ENCRYPT) || defined(CONFIG_INTEL_TDX_GUEST)
index f36f28405dcc63abfef5ee089022c4ef159907d9..6d7603511f52c38b0290f600a94df046defb7a3a 100644 (file)
@@ -126,7 +126,7 @@ static int __init acpi_mp_setup_reset(u64 reset_vector)
        return 0;
 }
 
-static int acpi_wakeup_cpu(u32 apicid, unsigned long start_ip)
+static int acpi_wakeup_cpu(u32 apicid, unsigned long start_ip, unsigned int cpu)
 {
        if (!acpi_mp_wake_mailbox_paddr) {
                pr_warn_once("No MADT mailbox: cannot bringup secondary CPUs. Booting with kexec?\n");
index b5bb7a2e8340b20db3a04e3f4e1684e6c4404b45..58abb941c45bec715cb4e0830e120f168f72ca66 100644 (file)
@@ -27,7 +27,13 @@ static void noop_send_IPI_allbutself(int vector) { }
 static void noop_send_IPI_all(int vector) { }
 static void noop_send_IPI_self(int vector) { }
 static void noop_apic_icr_write(u32 low, u32 id) { }
-static int noop_wakeup_secondary_cpu(u32 apicid, unsigned long start_eip) { return -1; }
+
+static int noop_wakeup_secondary_cpu(u32 apicid, unsigned long start_eip,
+       unsigned int cpu)
+{
+       return -1;
+}
+
 static u64 noop_apic_icr_read(void) { return 0; }
 static u32 noop_get_apic_id(u32 apicid) { return 0; }
 static void noop_apic_eoi(void) { }
index 16410f087b7af4f8390ee4973d9ee4bbd5679827..333536b89bde3dff9d25272049c9537a8d2cc501 100644 (file)
@@ -56,7 +56,7 @@ static void numachip2_apic_icr_write(int apicid, unsigned int val)
        numachip2_write32_lcsr(NUMACHIP2_APIC_ICR, (apicid << 12) | val);
 }
 
-static int numachip_wakeup_secondary(u32 phys_apicid, unsigned long start_rip)
+static int numachip_wakeup_secondary(u32 phys_apicid, unsigned long start_rip, unsigned int cpu)
 {
        numachip_apic_icr_write(phys_apicid, APIC_DM_INIT);
        numachip_apic_icr_write(phys_apicid, APIC_DM_STARTUP |
index 7fef504ca508b7829b89a3ca2dad9be1e22860e9..15209f220e1fda33feaea789312e94747a01911d 100644 (file)
@@ -667,7 +667,7 @@ static __init void build_uv_gr_table(void)
        }
 }
 
-static int uv_wakeup_secondary(u32 phys_apicid, unsigned long start_rip)
+static int uv_wakeup_secondary(u32 phys_apicid, unsigned long start_rip, unsigned int cpu)
 {
        unsigned long val;
        int pnode;
index d6cf1e23c2a32677a40db13b8d0166b705d0d83f..d52e9238e9fd135ecc52cca8ab28fcb3d9d7aad4 100644 (file)
@@ -695,7 +695,7 @@ static void send_init_sequence(u32 phys_apicid)
 /*
  * Wake up AP by INIT, INIT, STARTUP sequence.
  */
-static int wakeup_secondary_cpu_via_init(u32 phys_apicid, unsigned long start_eip)
+static int wakeup_secondary_cpu_via_init(u32 phys_apicid, unsigned long start_eip, unsigned int cpu)
 {
        unsigned long send_status = 0, accept_status = 0;
        int num_starts, j, maxlvt;
@@ -842,7 +842,7 @@ int common_cpu_up(unsigned int cpu, struct task_struct *idle)
  * Returns zero if startup was successfully sent, else error code from
  * ->wakeup_secondary_cpu.
  */
-static int do_boot_cpu(u32 apicid, int cpu, struct task_struct *idle)
+static int do_boot_cpu(u32 apicid, unsigned int cpu, struct task_struct *idle)
 {
        unsigned long start_ip = real_mode_header->trampoline_start;
        int ret;
@@ -896,11 +896,11 @@ static int do_boot_cpu(u32 apicid, int cpu, struct task_struct *idle)
         * - Use an INIT boot APIC message
         */
        if (apic->wakeup_secondary_cpu_64)
-               ret = apic->wakeup_secondary_cpu_64(apicid, start_ip);
+               ret = apic->wakeup_secondary_cpu_64(apicid, start_ip, cpu);
        else if (apic->wakeup_secondary_cpu)
-               ret = apic->wakeup_secondary_cpu(apicid, start_ip);
+               ret = apic->wakeup_secondary_cpu(apicid, start_ip, cpu);
        else
-               ret = wakeup_secondary_cpu_via_init(apicid, start_ip);
+               ret = wakeup_secondary_cpu_via_init(apicid, start_ip, cpu);
 
        /* If the wakeup mechanism failed, cleanup the warm reset vector */
        if (ret)