x86/hyperv: Use per cpu initial stack for vtl context
Subsystems (3)
PATCH 1/1 x86/hyperv: Use per cpu initial stack for vtl context
Currently, the secondary vCPUs in Hyper-V VTL context lack support for parallel startup. Therefore, relying on the single initial_stack fetched from the current task structure suffices for all vCPUs.
However, common initial_stack risks stack corruption when parallel startup is enabled. In order to facilitate parallel startup, use the initial_stack from the per CPU idle thread instead of the current task.
--- arch/x86/hyperv/hv_vtl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/hyperv/hv_vtl.c +3 -1
@@ -12,6 +12,7 @@#include <asm/i8259.h> #include <asm/mshyperv.h> #include <asm/realmode.h> +#include <../kernel/smpboot.h> extern struct boot_params boot_params;static struct real_mode_header hv_vtl_real_mode_header;
@@ -71,7 +72,8 @@ static int hv_vtl_bringup_vcpu(u32 target_vp_index, u64 eip_ignored)struct ldttss_desc *ldt;struct desc_struct *gdt; -u64 rsp = current->thread.sp; +struct task_struct *idle =idle_thread_get (target_vp_index); +u64 rsp = (unsigned long )idle->thread.sp;u64 rip = (u64)&hv_vtl_ap_entry;native_store_gdt (&gdt_ptr);
Signature · 1 line
2.34.1
From: Saurabh Sengar <[email protected]> Sent: Thursday, February 1, 2024 10:26 AM
Quoted 28 lines
Currently, the secondary vCPUs in Hyper-V VTL context lack support for parallel startup. Therefore, relying on the single
initial_stackfetched from the current task structure suffices for all vCPUs.However, common
initial_stackrisks stack corruption when parallel startup is enabled. In order to facilitate parallel startup, use theinitial_stackfrom the per CPU idle thread instead of the current task.Fixes 18415f33e2ac ("cpu/hotplug: Allow "parallel" bringup up to CPUHP_BP_KICK_AP_STATE")Signed-off-by Saurabh Sengar <[email protected]>--- arch/x86/hyperv/hv_vtl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/x86/hyperv/hv_vtl.c +1
-0@@ -12,6 +12,7 @@#include <asm/i8259.h> #include <asm/mshyperv.h> #include <asm/realmode.h> +#include <../kernel/smpboot.h> extern struct boot_params boot_params;static struct real_mode_header hv_vtl_real_mode_header;
arch/x86/hyperv/hv_vtl.c static int hv_vtl_bringup_vcpu(u32 target_vp_index, u64 eip_ignored) · line 75
Quoted 5 lines
arch/x86/hyperv/hv_vtl.c +1
-1@@ -71,7 +72,8 @@ static int hv_vtl_bringup_vcpu(u32 target_vp_index, u64 eip_ignored)struct ldttss_desc *ldt;struct desc_struct *gdt; -u64 rsp = current->thread.sp; +struct task_struct *idle =idle_thread_get (target_vp_index);
The "VP index" of a vCPU is a Hyper-V concept, and may not match
the Linux concept of a CPU number. In most cases, they *do* match,
so your testing of this patch probably worked. But there's no guarantee that they match. The Hyper-V TLFS does not even guarantee that VP indices are dense or that they start with 0 (even though they do in current versions of Hyper-V).
As a different kind of example, in a kdump kernel, Linux labels the booting CPU as CPU 0, but it may not be the 0th CPU in the guest VM, and hence may not have VP index of 0. Of course, in a kdump kernel nr_cpus is typically 1, so you aren't bringing up secondary CPUs. But sometimes kdump kernels boot with nr_cpus=2 or greater, in which case the mismatch would occur.
This conceptual difference in VP index and Linux CPU numbers is why the hv_vp_index array exists -- to map from a Linux CPU number to a Hyper-V VP index, and thereby avoid assuming they are equal.
So before hv_vtl_wakeup_secondary_cpu() calls this function, it needs to separately map the apicid to a Linux CPU number, which can then be passed to idle_thread_get().
Michael
Quoted 7 lines
arch/x86/hyperv/hv_vtl.c +1
-1+u64 rsp = (unsigned long )idle->thread.sp;u64 rip = (u64)&hv_vtl_ap_entry;native_store_gdt (&gdt_ptr); --2.34.1
Quoted 53 lines · Michael Kelley
From: Saurabh Sengar <[email protected]> Sent: Thursday, February 1, 2024 10:26 AM
Quoted 28 lines
Currently, the secondary vCPUs in Hyper-V VTL context lack support for parallel startup. Therefore, relying on the single
initial_stackfetched from the current task structure suffices for all vCPUs.However, common
initial_stackrisks stack corruption when parallel startup is enabled. In order to facilitate parallel startup, use theinitial_stackfrom the per CPU idle thread instead of the current task.Fixes 18415f33e2ac ("cpu/hotplug: Allow "parallel" bringup up to CPUHP_BP_KICK_AP_STATE")Signed-off-by Saurabh Sengar <[email protected]>--- arch/x86/hyperv/hv_vtl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/x86/hyperv/hv_vtl.c +1
-0@@ -12,6 +12,7 @@#include <asm/i8259.h> #include <asm/mshyperv.h> #include <asm/realmode.h> +#include <../kernel/smpboot.h> extern struct boot_params boot_params;static struct real_mode_header hv_vtl_real_mode_header;arch/x86/hyperv/hv_vtl.c static int hv_vtl_bringup_vcpu(u32 target_vp_index, u64 eip_ignored) · line 75
Quoted 5 lines
arch/x86/hyperv/hv_vtl.c +1
-1@@ -71,7 +72,8 @@ static int hv_vtl_bringup_vcpu(u32 target_vp_index, u64 eip_ignored)struct ldttss_desc *ldt;struct desc_struct *gdt; -u64 rsp = current->thread.sp; +struct task_struct *idle =idle_thread_get (target_vp_index);The "VP index" of a vCPU is a Hyper-V concept, and may not match
the Linux concept of a CPU number. In most cases, they *do* match,
so your testing of this patch probably worked. But there's no guarantee that they match. The Hyper-V TLFS does not even guarantee that VP indices are dense or that they start with 0 (even though they do in current versions of Hyper-V).As a different kind of example, in a kdump kernel, Linux labels the booting CPU as CPU 0, but it may not be the 0th CPU in the guest VM, and hence may not have VP index of 0. Of course, in a kdump kernel
nr_cpusis typically 1, so you aren't bringing up secondary CPUs. But sometimes kdump kernels boot with nr_cpus=2 or greater, in which case the mismatch would occur.This conceptual difference in VP index and Linux CPU numbers is why the
hv_vp_indexarray exists -- to map from a Linux CPU number to a Hyper-V VP index, and thereby avoid assuming they are equal.
Quoted 5 lines
So before
hv_vtl_wakeup_secondary_cpu()calls this function, it needs to separately map the apicid to a Linux CPU number, which can then be passed toidle_thread_get().Michael
Thanks for the review. I will fix this in V2.
Quoted 9 lines
Quoted 8 lines
arch/x86/hyperv/hv_vtl.c +1
-1+u64 rsp = (unsigned long )idle->thread.sp;u64 rip = (u64)&hv_vtl_ap_entry;native_store_gdt (&gdt_ptr); --2.34.1