Add support for the Bus Lock Threshold
Subsystems (5)
v5 cover Add support for the Bus Lock Threshold
Misbehaving guests can cause bus locks to degrade the performance of a system. Non-WB (write-back) and misaligned locked RMW (read-modify-write) instructions are referred to as "bus locks" and require system wide synchronization among all processors to guarantee the atomicity. The bus locks can impose notable performance penalties for all processors within the system.
Support for the Bus Lock Threshold is indicated by CPUID Fn8000_000A_EDX[29] BusLockThreshold=1, the VMCB provides a Bus Lock Threshold enable bit and an unsigned 16-bit Bus Lock Threshold count.
VMCB intercept bit
VMCB Offset Bits Function
14h 5 Intercept bus lock operationsBus lock threshold count
VMCB Offset Bits Function
120h 15:0 Bus lock counterDuring VMRUN, the bus lock threshold count is fetched and stored in an internal count register. Prior to executing a bus lock within the guest, the processor verifies the count in the bus lock register. If the count is greater than zero, the processor executes the bus lock, reducing the count. However, if the count is zero, the bus lock operation is not performed, and instead, a Bus Lock Threshold #VMEXIT is triggered to transfer control to the Virtual Machine Monitor (VMM).
A Bus Lock Threshold #VMEXIT is reported to the VMM with VMEXIT code 0xA5h, VMEXIT_BUSLOCK. EXITINFO1 and EXITINFO2 are set to 0 on a VMEXIT_BUSLOCK. On a #VMEXIT, the processor writes the current value of the Bus Lock Threshold Counter to the VMCB.
Note: Currently, virtualizing the Bus Lock Threshold feature for L1 guest is not supported.
More details about the Bus Lock Threshold feature can be found in AMD APM [1].
v4 -> v5
- Incorporated review comments from Sean, Boris and Xiaoyao Li.
- Massage commit messages and documentation based on review comments.
- Removed bus_lock_rip from vcpu_svm and modified the implementation to continue running bus_lock_counter across the nested traslations accordingly.
- Removed pr_info which says "Buslock Threshold is supported"
v3 -> v4
- Incorporated Sean's review comments
- Added a preparatory patch to move linear_rip out of kvm_pio_request, so that it can be used by the bus lock threshold patches.
- Added complete_userspace_buslock() function to reload bus_lock_counter to '1' only if the usespace has not changed the RIP.
- Added changes to continue running bus_lock_counter across the nested transitions.
v2 -> v3
- Drop parch to add virt tag in /proc/cpuinfo.
- Incorporated Tom's review comments.
v1 -> v2
- Incorporated misc review comments from Sean.
- Removed bus_lock_counter module parameter.
- Set the value of bus_lock_counter to zero by default and reload the value by 1 in bus lock exit handler.
- Add documentation for the behavioral difference for KVM_EXIT_BUS_LOCK.
- Improved selftest for buslock to work on SVM and VMX.
- Rewrite the commit messages.
Patches are prepared on kvm-next/svm (bb5081f4abf2).
Testing done:
- Tested the Bus Lock Threshold functionality on normal, SEV, SEV-ES and SEV-SNP guests.
- Tested the Bus Lock Threshold functionality on nested guests.
[1]: AMD64 Architecture Programmer's Manual Pub. 24593, April 2024,
Vol 2, 15.14.5 Bus Lock Threshold.
https://bugzilla.kernel.org/attachment.cgi?id=306250---
v1: https://lore.kernel.org/kvm/[email protected]/T/
v2: https://lore.kernel.org/kvm/[email protected]/T/
v3: https://lore.kernel.org/kvm/[email protected]/T/
v4: https://lore.kernel.org/kvm/[email protected]/
Manali Shukla (3):
KVM: x86: Make kvm_pio_request.linear_rip a common field for user
exits
x86/cpufeatures: Add CPUID feature bit for the Bus Lock Threshold
KVM: SVM: Add support for KVM_CAP_X86_BUS_LOCK_EXIT on SVM CPUs
Nikunj A Dadhania (2):
KVM: SVM: Enable Bus lock threshold exit
KVM: selftests: Add bus lock exit test
Documentation/virt/kvm/api.rst | 5 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/include/asm/svm.h | 7 +- arch/x86/include/uapi/asm/svm.h | 2 + arch/x86/kvm/svm/nested.c | 35 +++++ arch/x86/kvm/svm/svm.c | 38 +++++ arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/x86.c | 8 +- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/kvm_buslock_test.c | 135 ++++++++++++++++++ 11 files changed, 228 insertions(+), 7 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/kvm_buslock_test.c
14,521 lines not shown · raw message
Part of this message is not shown here. View the raw message.
PATCH v5 1/5 KVM: x86: Make kvm_pio_request.linear_rip a common field for user exits
Move and rename kvm_pio_request.linear_rip to kvm_vcpu_arch.cui_linear_rip so that the field can be used by other userspace exit completion flows that need to take action if and only if userspace has not modified RIP.
No functional changes intended.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/x86.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -412,7 +412,6 @@ struct kvm_rmap_head { };struct kvm_pio_request { -unsigned long linear_rip;unsigned long count;int in;int port;
@@ -918,6 +917,7 @@ struct kvm_vcpu_arch {bool emulate_regs_need_sync_to_vcpu;bool emulate_regs_need_sync_from_vcpu;int (*complete_userspace_io)(struct kvm_vcpu *vcpu); +unsigned long cui_linear_rip;gpa_t time;s8 pvclock_tsc_shift;
arch/x86/kvm/x86.c +4 -4
@@ -9359,7 +9359,7 @@ static int complete_fast_pio_out(struct kvm_vcpu *vcpu) { vcpu->arch.pio.count =0 ; -if (unlikely (!kvm_is_linear_rip (vcpu, vcpu->arch.pio.linear_rip))) +if (unlikely (!kvm_is_linear_rip (vcpu, vcpu->arch.cui_linear_rip)))return 1 ;return kvm_skip_emulated_instruction (vcpu);
@@ -9384,7 +9384,7 @@ static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, complete_fast_pio_out_port_0x7e;kvm_skip_emulated_instruction (vcpu); }else { - vcpu->arch.pio.linear_rip =kvm_get_linear_rip (vcpu); + vcpu->arch.cui_linear_rip =kvm_get_linear_rip (vcpu); vcpu->arch.complete_userspace_io = complete_fast_pio_out; }return 0 ;
@@ -9397,7 +9397,7 @@ static int complete_fast_pio_in(struct kvm_vcpu *vcpu)/* We should only ever be called with arch.pio.count equal to 1 */ BUG_ON (vcpu->arch.pio.count !=1 ); -if (unlikely (!kvm_is_linear_rip (vcpu, vcpu->arch.pio.linear_rip))) { +if (unlikely (!kvm_is_linear_rip (vcpu, vcpu->arch.cui_linear_rip))) { vcpu->arch.pio.count =0 ;return 1 ; }
@@ -9426,7 +9426,7 @@ static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,return ret; } - vcpu->arch.pio.linear_rip =kvm_get_linear_rip (vcpu); + vcpu->arch.cui_linear_rip =kvm_get_linear_rip (vcpu); vcpu->arch.complete_userspace_io = complete_fast_pio_in;return 0 ;
14,521 lines not shown · raw message
Part of this message is not shown here. View the raw message.
PATCH v5 2/5 x86/cpufeatures: Add CPUID feature bit for the Bus Lock Threshold
In reply to Manali ShuklaMisbehaving guests can cause bus locks to degrade the performance of the system. The Bus Lock Threshold feature can be used to address this issue by providing capability to the hypervisor to limit guest's ability to generate bus lock, thereby preventing system slowdown due to performance penalities.
When the Bus Lock Threshold feature is enabled, the processor checks the bus lock threshold count before executing the buslock and decides whether to trigger bus lock exit or not.
The value of the bus lock threshold count '0' generates bus lock exits, and if the value is greater than '0', the bus lock is executed successfully and the bus lock threshold count is decremented.
Presence of the Bus Lock threshold feature is indicated via CPUID function 0x8000000A_EDX[29].
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -378,6 +378,7 @@#define X86_FEATURE_V_SPEC_CTRL (15*32+20)/* "v_spec_ctrl" Virtual SPEC_CTRL */ #define X86_FEATURE_VNMI (15*32+25)/* "vnmi" Virtual NMI */ #define X86_FEATURE_SVME_ADDR_CHK (15*32+28)/* SVME addr check */ +#define X86_FEATURE_BUS_LOCK_THRESHOLD (15*32+29)/* Bus lock threshold */ #define X86_FEATURE_IDLE_HLT (15*32+30)/* IDLE HLT intercept */ /* Intel-defined CPU features, CPUID level 0x00000007:0 (ECX), word 16 */
Signature · 1 line
2.34.1
Re: [PATCH v5 2/5] x86/cpufeatures: Add CPUID feature bit for the Bus Lock Threshold
Quoted 28 lines · Manali Shukla
Misbehaving guests can cause bus locks to degrade the performance of the system. The Bus Lock Threshold feature can be used to address this issue by providing capability to the hypervisor to limit guest's ability to generate bus lock, thereby preventing system slowdown due to performance penalities.
When the Bus Lock Threshold feature is enabled, the processor checks the bus lock threshold count before executing the buslock and decides whether to trigger bus lock exit or not.
The value of the bus lock threshold count '0' generates bus lock exits, and if the value is greater than '0', the bus lock is executed successfully and the bus lock threshold count is decremented.
Presence of the Bus Lock threshold feature is indicated via CPUID function 0x8000000A_EDX[29].
Signed-off-by Manali Shukla <[email protected]>--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)arch/x86/include/asm/cpufeatures.h +0
-0
arch/x86/include/asm/cpufeatures.h line 384
Quoted 7 lines
arch/x86/include/asm/cpufeatures.h +1
-0@@ -378,6 +378,7 @@#define X86_FEATURE_V_SPEC_CTRL (15*32+20)/* "v_spec_ctrl" Virtual SPEC_CTRL */ #define X86_FEATURE_VNMI (15*32+25)/* "vnmi" Virtual NMI */ #define X86_FEATURE_SVME_ADDR_CHK (15*32+28)/* SVME addr check */ +#define X86_FEATURE_BUS_LOCK_THRESHOLD (15*32+29)/* Bus lock threshold */ #define X86_FEATURE_IDLE_HLT (15*32+30)/* IDLE HLT intercept */ /* Intel-defined CPU features, CPUID level 0x00000007:0 (ECX), word 16 */
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v5 3/5 KVM: SVM: Enable Bus lock threshold exit
In reply to Manali ShuklaFrom: Nikunj A Dadhania <[email protected]>
Virtual machines can exploit bus locks to degrade the performance of the system. Bus locks can be caused by Non-WB(Write back) and misaligned locked RMW (Read-modify-Write) instructions and require systemwide synchronization among all processors which can result into significant performance penalties.
To address this issue, the Bus Lock Threshold feature is introduced to provide ability to hypervisor to restrict guests' capability of initiating mulitple buslocks, thereby preventing system slowdowns.
Support for the buslock threshold is indicated via CPUID function 0x8000000A_EDX[29].
On the processors that support the Bus Lock Threshold feature, the VMCB provides a Bus Lock Threshold enable bit and an unsigned 16-bit Bus Lock threshold count.
VMCB intercept bit
VMCB Offset Bits Function 14h 5 Intercept bus lock operations
Bus lock threshold count
VMCB Offset Bits Function 120h 15:0 Bus lock counter
When a VMRUN instruction is executed, the bus lock threshold count is loaded into an internal count register. Before the processor executes a bus lock in the guest, it checks the value of this register:
- If the value is greater than '0', the processor successfully executes the bus lock and decrements the count.
- If the value is '0', the bus lock is not executed, and a #VMEXIT to the VMM is taken.
The bus lock threshold #VMEXIT is reported to the VMM with the VMEXIT code A5h, SVM_EXIT_BUS_LOCK.
--- arch/x86/include/asm/svm.h | 7 +++++-- arch/x86/include/uapi/asm/svm.h | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-)
arch/x86/include/asm/svm.h +5 -2
@@ -116,6 +116,7 @@ enum {INTERCEPT_INVPCID ,INTERCEPT_MCOMMIT ,INTERCEPT_TLBSYNC , +INTERCEPT_BUSLOCK , INTERCEPT_IDLE_HLT = 166, };
@@ -159,10 +160,12 @@ struct __attribute__ ((__packed__)) vmcb_control_area {u64 avic_physical_id;/* Offset 0xf8 */ u8 reserved_7[8 ];u64 vmsa_pa;/* Used for an SEV-ES guest */ -u8 reserved_8[40 ]; +u8 reserved_8[16 ]; +u16 bus_lock_counter;/* Offset 0x120 */ +u8 reserved_9[22 ];u64 allowed_sev_features;/* Offset 0x138 */ u64 guest_sev_features;/* Offset 0x140 */ -u8 reserved_9[664 ]; +u8 reserved_10[664 ];/* * Offset 0x3e0, 32 bytes reserved * for use by hypervisor/software.
arch/x86/include/uapi/asm/svm.h +2 -0
@@ -95,6 +95,7 @@#define SVM_EXIT_CR14_WRITE_TRAP 0x09e#define SVM_EXIT_CR15_WRITE_TRAP 0x09f#define SVM_EXIT_INVPCID 0x0a2 +#define SVM_EXIT_BUS_LOCK 0x0a5#define SVM_EXIT_IDLE_HLT 0x0a6#define SVM_EXIT_NPF 0x400#define SVM_EXIT_AVIC_INCOMPLETE_IPI 0x401
@@ -225,6 +226,7 @@ { SVM_EXIT_CR4_WRITE_TRAP, "write_cr4_trap" }, \ { SVM_EXIT_CR8_WRITE_TRAP, "write_cr8_trap" }, \ { SVM_EXIT_INVPCID, "invpcid" }, \ + { SVM_EXIT_BUS_LOCK, "buslock" }, \ { SVM_EXIT_IDLE_HLT, "idle-halt" }, \ { SVM_EXIT_NPF, "npf" }, \ { SVM_EXIT_AVIC_INCOMPLETE_IPI, "avic_incomplete_ipi" }, \
Signature · 1 line
2.34.1
PATCH v5 4/5 KVM: SVM: Add support for KVM_CAP_X86_BUS_LOCK_EXIT on SVM CPUs
In reply to Manali ShuklaAdd support for KVM_CAP_X86_BUS_LOCK_EXIT on SVM CPUs with Bus Lock Threshold, which is close enough to VMX's Bus Lock Detection VM-Exit to allow reusing KVM_CAP_X86_BUS_LOCK_EXIT.
The biggest difference between the two features is that Threshold is fault-like, whereas Detection is trap-like. To allow the guest to make forward progress, Threshold provides a per-VMCB counter which is decremented every time a bus lock occurs, and a VM-Exit is triggered if and only if the counter is '0'.
To provide Detection-like semantics, initialize the counter to '0', i.e. exit on every bus lock, and when re-executing the guilty instruction, set the counter to '1' to effectively step past the instruction.
Note, in the unlikely scenario that re-executing the instruction doesn't trigger a bus lock, e.g. because the guest has changed memory types or patched the guilty instruction, the bus lock counter will be left at '1', i.e. the guest will be able to do a bus lock on a different instruction. In a perfect world, KVM would ensure the counter is '0' if the guest has made forward progress, e.g. if RIP has changed. But trying to close that hole would incur non-trivial complexity, for marginal benefit; the intent of KVM_CAP_X86_BUS_LOCK_EXIT is to allow userspace rate-limit bus locks, not to allow for precise detection of problematic guest code. And, it's simply not feasible to fully close the hole, e.g. if an interrupt arrives before the original instruction can re-execute, the guest could step past a different bus lock.
--- Documentation/virt/kvm/api.rst | 5 +++++ arch/x86/kvm/svm/nested.c | 34 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 38 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + 4 files changed, 78 insertions(+)
Documentation/virt/kvm/api.rst +5 -0
@@ -7989,6 +7989,11 @@ apply some other policy-based mitigation. When exiting to userspace, KVM sets KVM_RUN_X86_BUS_LOCK in vcpu-run->flags, and conditionally sets the exit_reason to KVM_EXIT_X86_BUS_LOCK. +Due to differences in the underlying hardware implementation, the vCPU's RIP at +the time of exit diverges between Intel and AMD. On Intel hosts, RIP points at +the next instruction, i.e. the exit is trap-like. On AMD hosts, RIP points at +the offending instruction, i.e. the exit is fault-like. + Note! Detected bus locks may be coincident with other exits to userspace, i.e. KVM_RUN_X86_BUS_LOCK should be checked regardless of the primary exit reason if userspace wants to take action on all detected bus locks.
arch/x86/kvm/svm/nested.c +34 -0
@@ -678,6 +678,33 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm, vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa; vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa; +/* +* Stash vmcb02's counter if the guest hasn't moved past the guilty +* instrution; otherwise, reset the counter to '0'. +* +* In order to detect if L2 has made forward progress or not, track the +* RIP at which a bus lock has occurred on a per-vmcb12 basis. If RIP +* is changed, guest has clearly made forward progress, bus_lock_counter +* still remained '1', so reset bus_lock_counter to '0'. Eg. In the +* scenario, where a buslock happened in L1 before VMRUN, the bus lock +* firmly happened on an instruction in the past. Even if vmcb01's +* counter is still '1', (because the guilty instruction got patched), +* the vCPU has clearly made forward progress and so KVM should reset +* vmcb02's counter to '0'. +* +* If the RIP hasn't changed, stash the bus lock counter at nested VMRUN +* to prevent the same guilty instruction from triggering a VM-Exit. Eg. +* if userspace rate-limits the vCPU, then it's entirely possible that +* L1's tick interrupt is pending by the time userspace re-runs the +* vCPU. If KVM unconditionally clears the counter on VMRUN, then when +* L1 re-enters L2, the same instruction will trigger a VM-Exit and the +* entire cycle start over. +*/ +if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip)) + vmcb02->control.bus_lock_counter =1 ; +else + vmcb02->control.bus_lock_counter =0 ; +/* Done at vmrun: asid. */ /* Also overwritten later if necessary. */
@@ -1039,6 +1066,13 @@ int nested_svm_vmexit(struct vcpu_svm *svm) } +/* +* Invalidate bus_lock_rip unless KVM is still waiting for the guest +* to make forward progress before re-enabling bus lock detection. +*/ +if (!vmcb02->control.bus_lock_counter) + svm->nested.ctl.bus_lock_rip =INVALID_GPA ; +nested_svm_copy_common_state (svm->nested.vmcb02.ptr, svm->vmcb01.ptr);svm_switch_vmcb (svm, &svm->vmcb01);
arch/x86/kvm/svm/svm.c +38 -0
@@ -1384,6 +1384,9 @@ static void init_vmcb(struct kvm_vcpu *vcpu) svm->vmcb->control.int_ctl |=V_GIF_ENABLE_MASK ; } +if (vcpu->kvm->arch.bus_lock_detection_enabled) +svm_set_intercept (svm,INTERCEPT_BUSLOCK ); +if (sev_guest (vcpu->kvm))sev_init_vmcb (svm);
@@ -3317,6 +3320,37 @@ static int invpcid_interception(struct kvm_vcpu *vcpu)return kvm_handle_invpcid (vcpu, type, gva); } +static inline int complete_userspace_buslock (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +/* +* If userspace has NOT changed RIP, then KVM's ABI is to let the guest +* execute the bus-locking instruction. Set the bus lock counter to '1' +* to effectively step past the bus lock. +*/ +if (kvm_is_linear_rip (vcpu, vcpu->arch.cui_linear_rip)) + svm->vmcb->control.bus_lock_counter =1 ; + +return 1 ; +} + +static int bus_lock_exit (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); + + vcpu->run->exit_reason =KVM_EXIT_X86_BUS_LOCK ; + vcpu->run->flags |=KVM_RUN_X86_BUS_LOCK ; + + vcpu->arch.cui_linear_rip =kvm_get_linear_rip (vcpu); + vcpu->arch.complete_userspace_io = complete_userspace_buslock; + +if (is_guest_mode (vcpu)) + svm->nested.ctl.bus_lock_rip = vcpu->arch.cui_linear_rip; + +return 0 ; +} +static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { [SVM_EXIT_READ_CR0 ] = cr_interception, [SVM_EXIT_READ_CR3 ] = cr_interception,
@@ -3386,6 +3420,7 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { [SVM_EXIT_INVPCID] = invpcid_interception, [SVM_EXIT_IDLE_HLT] = kvm_emulate_halt, [SVM_EXIT_NPF] = npf_interception, + [SVM_EXIT_BUS_LOCK] = bus_lock_exit, [SVM_EXIT_RSM] = rsm_interception, [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
@@ -5388,6 +5423,9 @@ static __init void svm_set_cpu_caps(void)kvm_cpu_cap_set (X86_FEATURE_SVME_ADDR_CHK ); } +if (cpu_feature_enabled (X86_FEATURE_BUS_LOCK_THRESHOLD )) + kvm_caps.has_bus_lock_exit = true; +/* CPUID 0x80000008 */ if (boot_cpu_has (X86_FEATURE_LS_CFG_SSBD ) ||boot_cpu_has (X86_FEATURE_AMD_SSBD ))
arch/x86/kvm/svm/svm.h +1 -0
@@ -173,6 +173,7 @@ struct vmcb_ctrl_area_cached {u64 nested_cr3;u64 virt_ext;u32 clean; +u64 bus_lock_rip;union {#if IS_ENABLED (CONFIG_HYPERV ) ||IS_ENABLED (CONFIG_KVM_HYPERV )struct hv_vmcb_enlightenments hv_enlightenments;
Signature · 1 line
2.34.1
Re: [PATCH v5 4/5] KVM: SVM: Add support for KVM_CAP_X86_BUS_LOCK_EXIT on SVM CPUs
arch/x86/kvm/svm/nested.c static void nested_vmcb02_prepare_control(struct vcpu_svm *svm, · line 683
Quoted 3 lines · Manali Shukla
arch/x86/kvm/svm/nested.c +3
-0+/* +* Stash vmcb02's counter if the guest hasn't moved past the guilty +* instrution; otherwise, reset the counter to '0'.
typo instrution -> instruction
Quoted 9 lines
arch/x86/kvm/svm/nested.c +9
-0+* +* In order to detect if L2 has made forward progress or not, track the +* RIP at which a bus lock has occurred on a per-vmcb12 basis. If RIP +* is changed, guest has clearly made forward progress, bus_lock_counter +* still remained '1', so reset bus_lock_counter to '0'. Eg. In the +* scenario, where a buslock happened in L1 before VMRUN, the bus lock +* firmly happened on an instruction in the past. Even if vmcb01's +* counter is still '1', (because the guilty instruction got patched), +* the vCPU has clearly made forward progress and so KVM should reset
arch/x86/kvm/svm/nested.c static void nested_vmcb02_prepare_control(struct vcpu_svm *svm, · line 702
Quoted 10 lines
arch/x86/kvm/svm/nested.c +10
-0+* vmcb02's counter to '0'. +* +* If the RIP hasn't changed, stash the bus lock counter at nested VMRUN +* to prevent the same guilty instruction from triggering a VM-Exit. Eg. +* if userspace rate-limits the vCPU, then it's entirely possible that +* L1's tick interrupt is pending by the time userspace re-runs the +* vCPU. If KVM unconditionally clears the counter on VMRUN, then when +* L1 re-enters L2, the same instruction will trigger a VM-Exit and the +* entire cycle start over. +*/
Thanks,
Alok
Re: [PATCH v5 4/5] KVM: SVM: Add support for KVM_CAP_X86_BUS_LOCK_EXIT on SVM CPUs
In reply to Manali ShuklaQuoted 73 lines · Manali Shukla
Add support for
KVM_CAP_X86_BUS_LOCK_EXITon SVM CPUs with Bus Lock Threshold, which is close enough to VMX's Bus Lock Detection VM-Exit to allow reusingKVM_CAP_X86_BUS_LOCK_EXIT.The biggest difference between the two features is that Threshold is fault-like, whereas Detection is trap-like. To allow the guest to make forward progress, Threshold provides a per-VMCB counter which is decremented every time a bus lock occurs, and a VM-Exit is triggered if and only if the counter is '0'.
To provide Detection-like semantics, initialize the counter to '0', i.e. exit on every bus lock, and when re-executing the guilty instruction, set the counter to '1' to effectively step past the instruction.
Note, in the unlikely scenario that re-executing the instruction doesn't trigger a bus lock, e.g. because the guest has changed memory types or patched the guilty instruction, the bus lock counter will be left at '1', i.e. the guest will be able to do a bus lock on a different instruction. In a perfect world, KVM would ensure the counter is '0' if the guest has made forward progress, e.g. if RIP has changed. But trying to close that hole would incur non-trivial complexity, for marginal benefit; the intent of
KVM_CAP_X86_BUS_LOCK_EXITis to allow userspace rate-limit bus locks, not to allow for precise detection of problematic guest code. And, it's simply not feasible to fully close the hole, e.g. if an interrupt arrives before the original instruction can re-execute, the guest could step past a different bus lock.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Manali Shukla <[email protected]>--- Documentation/virt/kvm/api.rst | 5 +++++ arch/x86/kvm/svm/nested.c | 34 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 38 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + 4 files changed, 78 insertions(+)Documentation/virt/kvm/api.rst +5
-0@@ -7989,6 +7989,11 @@ apply some other policy-based mitigation. When exiting to userspace, KVM sets KVM_RUN_X86_BUS_LOCK in vcpu-run->flags, and conditionally sets the exit_reason to KVM_EXIT_X86_BUS_LOCK. +Due to differences in the underlying hardware implementation, the vCPU's RIP at +the time of exit diverges between Intel and AMD. On Intel hosts, RIP points at +the next instruction, i.e. the exit is trap-like. On AMD hosts, RIP points at +the offending instruction, i.e. the exit is fault-like. + Note! Detected bus locks may be coincident with other exits to userspace, i.e. KVM_RUN_X86_BUS_LOCK should be checked regardless of the primary exit reason if userspace wants to take action on all detected bus locks.arch/x86/kvm/svm/nested.c +13
-0@@ -678,6 +678,33 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm, vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa; vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa; +/* +* Stash vmcb02's counter if the guest hasn't moved past the guilty +* instrution; otherwise, reset the counter to '0'. +* +* In order to detect if L2 has made forward progress or not, track the +* RIP at which a bus lock has occurred on a per-vmcb12 basis. If RIP +* is changed, guest has clearly made forward progress, bus_lock_counter +* still remained '1', so reset bus_lock_counter to '0'. Eg. In the +* scenario, where a buslock happened in L1 before VMRUN, the bus lock +* firmly happened on an instruction in the past. Even if vmcb01's +* counter is still '1', (because the guilty instruction got patched), +* the vCPU has clearly made forward progress and so KVM should reset +* vmcb02's counter to '0'.
arch/x86/kvm/svm/nested.c static void nested_vmcb02_prepare_control(struct vcpu_svm *svm, · line 703
Quoted 10 lines
arch/x86/kvm/svm/nested.c +10
-0+* +* If the RIP hasn't changed, stash the bus lock counter at nested VMRUN +* to prevent the same guilty instruction from triggering a VM-Exit. Eg. +* if userspace rate-limits the vCPU, then it's entirely possible that +* L1's tick interrupt is pending by the time userspace re-runs the +* vCPU. If KVM unconditionally clears the counter on VMRUN, then when +* L1 re-enters L2, the same instruction will trigger a VM-Exit and the +* entire cycle start over. +*/ +if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip))
Is vmcb02->save.rip the right thing to use here?
IIUC, we want to find out if L2 made forward progress since the last bus lock #VMEXIT from L2 to L0, at which point we record bus_lock_rip. However, on nested VMRUN, vmcb02->save.rip is only populated from the vmcb12 in nested_vmcb02_prepare_save(), which doesn't happen until after nested_vmcb02_prepare_control(). So waht we're comparing against here is L2's RIP last time KVM ran it.
It's even worse in the KVM_SET_NESTED_STATE path, because vmcb02->save.rip will be unintialized (probably zero).
Probably you want to use vmcb12_rip here, as this is the RIP that L1 wants to run L2 with, and what will end up in vmcb02->save.rip.
HOWEVER, that is also broken in the KVM_SET_NESTED_STATE path. In that path we pass in the uninitialized vmcb02->save.rip as vmcb12_rip anyway. Fixing this is non-trivial because KVM_SET_REGS could be called before or after KVM_SET_NESTED_STATE, so the RIP may not be available at all at that point.
We probably want to set a flag in svm_set_nested_state() that the RIP needs fixing up, and the perhaps in svm_vcpu_pre_run() fix up the control fields in vmcb02 that depend on it: namely the bus_lock_counter, next_rip, and soft_int_* fields.
It gets worse because I think next_rip is also not always properly saved/restored because we do not sync it from vmcb02 to cache in nested_sync_control_from_vmcb02() -- but that one is not relevant to bus_lock_counter AFAICT.
Quoted 7 lines
arch/x86/kvm/svm/nested.c +4
-0+ vmcb02->control.bus_lock_counter =1 ; + else + vmcb02->control.bus_lock_counter = 0; +/* Done at vmrun: asid. */ /* Also overwritten later if necessary. */
Sean Christopherson
maintainerRe: [PATCH v5 4/5] KVM: SVM: Add support for KVM_CAP_X86_BUS_LOCK_EXIT on SVM CPUs
Quoted 26 lines · Yosry Ahmed
Quoted 25 lines · Manali Shukla
arch/x86/kvm/svm/nested.c +17
-0@@ -678,6 +678,33 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm, vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa; vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa; +/* +* Stash vmcb02's counter if the guest hasn't moved past the guilty +* instrution; otherwise, reset the counter to '0'. +* +* In order to detect if L2 has made forward progress or not, track the +* RIP at which a bus lock has occurred on a per-vmcb12 basis. If RIP +* is changed, guest has clearly made forward progress, bus_lock_counter +* still remained '1', so reset bus_lock_counter to '0'. Eg. In the +* scenario, where a buslock happened in L1 before VMRUN, the bus lock +* firmly happened on an instruction in the past. Even if vmcb01's +* counter is still '1', (because the guilty instruction got patched), +* the vCPU has clearly made forward progress and so KVM should reset +* vmcb02's counter to '0'. +* +* If the RIP hasn't changed, stash the bus lock counter at nested VMRUN +* to prevent the same guilty instruction from triggering a VM-Exit. Eg. +* if userspace rate-limits the vCPU, then it's entirely possible that
Quoted 8 lines
arch/x86/kvm/svm/nested.c static void nested_vmcb02_prepare_control(struct vcpu_svm *svm, · line 703
Quoted 6 lines
arch/x86/kvm/svm/nested.c +6
-0+* L1's tick interrupt is pending by the time userspace re-runs the +* vCPU. If KVM unconditionally clears the counter on VMRUN, then when +* L1 re-enters L2, the same instruction will trigger a VM-Exit and the +* entire cycle start over. +*/ +if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip))Is
vmcb02->save.ripthe right thing to use here?
Objection, leading the witness your honor.
Quoted 2 lines
IIUC, we want to find out if L2 made forward progress since the last bus lock #VMEXIT from L2 to L0
More or less.
Quoted 5 lines
, at which point we record
bus_lock_rip.
However, on nested VMRUN,vmcb02->save.ripis only populated from the vmcb12 innested_vmcb02_prepare_save(), which doesn't happen until afternested_vmcb02_prepare_control(). So waht we're comparing against here is L2's RIP last time KVM ran it.
Hrm, that definitely seems like what past me intended[*], as this quite clearly suggests my intent was to check the RIP coming from vmcb12:
: Again, it's imperfect, e.g. if L1 happens to run a different vCPU at the same : RIP, then KVM will allow a bus lock for the wrong vCPU. But the odds of that : happening are absurdly low unless L1 is deliberately doing weird things, and so : I don't think
I have this vague feeling that I deliberately didn't check @vmcb12_rip, but the more I look at this, the more I'm convinced I simply didn't notice @vmcb12_rip when typing up the suggestion.
[*] https://lore.kernel.org/all/[email protected]
Quoted 2 lines
It's even worse in the
KVM_SET_NESTED_STATEpath, becausevmcb02->save.ripwill be unintialized (probably zero).
FWIW, this one is arguably ideal, in the sense that KVM should probably assume the worst if userspace is loading guest state.
Quoted 10 lines
Probably you want to use
vmcb12_riphere, as this is the RIP that L1 wants to run L2 with, and what will end up invmcb02->save.rip.HOWEVER, that is also broken in the
KVM_SET_NESTED_STATEpath. In that path we pass in the uninitializedvmcb02->save.ripasvmcb12_ripanyway.Fixing this is non-trivial because
KVM_SET_REGScould be called before or afterKVM_SET_NESTED_STATE, so the RIP may not be available at all at that point.
Eh, this code is completely meaningless on KVM_SET_NESTED_STATE. There is no "right" answer, because KVM has no clue what constitutes forward progress.
Luckily, no answer is completely "wrong" either, because the #VMEXIT is transparent to L1 and L2. E.g. it's like saying that sending an IRQ to CPU running a vCPU is "wrong"; it's never truly "wrong", just suboptimal (sometimes *very* suboptimal).
Quoted 4 lines
We probably want to set a flag in
svm_set_nested_state()that the RIP needs fixing up, and the perhaps insvm_vcpu_pre_run()fix up the control fields in vmcb02 that depend on it: namely thebus_lock_counter,next_rip, and soft_int_* fields.
Nah, not unless there's a meaningful, negative impact on a real world setup. And as above, in practice the only impact is effectively a performance blip due to generating an unwanted userspace exit. If userspace is stuffing nested state so often that that's actually a performance problem, then userspace can set regs before nested state.
There are a pile of edge cases and "what ifs" around this, none of which have a perfect answer since KVM doesn't know userspace's intent. And so I want to go with a solution that is as simple as possible without risking putting L2 into an infinite loop.
Quoted 4 lines
It gets worse because I think
next_ripis also not always properly saved/restored because we do not sync it from vmcb02 to cache innested_sync_control_from_vmcb02()-- but that one is not relevant tobus_lock_counterAFAICT.
Correct.
All in all, I think just this?
arch/x86/kvm/svm/nested.c +1 -1
@@ -809,7 +809,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,* L1 re-enters L2, the same instruction will trigger a VM-Exit and the * entire cycle start over. */ -if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip)) +if (vmcb12_rip && (svm->nested.ctl.bus_lock_rip == vmcb12_rip)) vmcb02->control.bus_lock_counter =1 ;else vmcb02->control.bus_lock_counter =0 ;
Re: [PATCH v5 4/5] KVM: SVM: Add support for KVM_CAP_X86_BUS_LOCK_EXIT on SVM CPUs
Quoted 36 lines · Sean Christopherson
Quoted 26 lines · Yosry Ahmed
Quoted 25 lines · Manali Shukla
arch/x86/kvm/svm/nested.c +17
-0@@ -678,6 +678,33 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm, vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa; vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa; +/* +* Stash vmcb02's counter if the guest hasn't moved past the guilty +* instrution; otherwise, reset the counter to '0'. +* +* In order to detect if L2 has made forward progress or not, track the +* RIP at which a bus lock has occurred on a per-vmcb12 basis. If RIP +* is changed, guest has clearly made forward progress, bus_lock_counter +* still remained '1', so reset bus_lock_counter to '0'. Eg. In the +* scenario, where a buslock happened in L1 before VMRUN, the bus lock +* firmly happened on an instruction in the past. Even if vmcb01's +* counter is still '1', (because the guilty instruction got patched), +* the vCPU has clearly made forward progress and so KVM should reset +* vmcb02's counter to '0'. +* +* If the RIP hasn't changed, stash the bus lock counter at nested VMRUN +* to prevent the same guilty instruction from triggering a VM-Exit. Eg. +* if userspace rate-limits the vCPU, then it's entirely possible that Quoted 8 lines
arch/x86/kvm/svm/nested.c static void nested_vmcb02_prepare_control(struct vcpu_svm *svm, · line 703
Quoted 6 lines
arch/x86/kvm/svm/nested.c +6
-0+* L1's tick interrupt is pending by the time userspace re-runs the +* vCPU. If KVM unconditionally clears the counter on VMRUN, then when +* L1 re-enters L2, the same instruction will trigger a VM-Exit and the +* entire cycle start over. +*/ +if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip))Is
vmcb02->save.ripthe right thing to use here?
Quoted 1 line
Objection, leading the witness your honor.
Withdrawn.
Quoted 15 lines
Quoted 3 lines
IIUC, we want to find out if L2 made forward progress since the last bus lock #VMEXIT from L2 to L0
More or less.
Quoted 5 lines
, at which point we record
bus_lock_rip.
However, on nested VMRUN,vmcb02->save.ripis only populated from the vmcb12 innested_vmcb02_prepare_save(), which doesn't happen until afternested_vmcb02_prepare_control(). So waht we're comparing against here is L2's RIP last time KVM ran it.Hrm, that definitely seems like what past me intended[*], as this quite clearly suggests my intent was to check the RIP coming from vmcb12:
Quoted 8 lines
: Again, it's imperfect, e.g. if L1 happens to run a different vCPU at the same : RIP, then KVM will allow a bus lock for the wrong vCPU. But the odds of that : happening are absurdly low unless L1 is deliberately doing weird things, and so : I don't think
I have this vague feeling that I deliberately didn't check @vmcb12_rip, but the more I look at this, the more I'm convinced I simply didn't notice @vmcb12_rip when typing up the suggestion.
Yeah I looked through the history trying to figure out if the choice was intentional but I couldn't find a clue.
Quoted 32 lines
[*] https://lore.kernel.org/all/[email protected]
Quoted 2 lines
It's even worse in the
KVM_SET_NESTED_STATEpath, becausevmcb02->save.ripwill be unintialized (probably zero).FWIW, this one is arguably ideal, in the sense that KVM should probably assume the worst if userspace is loading guest state.
Quoted 10 lines
Probably you want to use
vmcb12_riphere, as this is the RIP that L1 wants to run L2 with, and what will end up invmcb02->save.rip.HOWEVER, that is also broken in the
KVM_SET_NESTED_STATEpath. In that path we pass in the uninitializedvmcb02->save.ripasvmcb12_ripanyway.Fixing this is non-trivial because
KVM_SET_REGScould be called before or afterKVM_SET_NESTED_STATE, so the RIP may not be available at all at that point.Eh, this code is completely meaningless on
KVM_SET_NESTED_STATE. There is no "right" answer, because KVM has no clue what constitutes forward progress.Luckily, no answer is completely "wrong" either, because the #VMEXIT is transparent to L1 and L2. E.g. it's like saying that sending an IRQ to CPU running a vCPU is "wrong"; it's never truly "wrong", just suboptimal (sometimes *very* suboptimal).
Quoted 4 lines
We probably want to set a flag in
svm_set_nested_state()that the RIP needs fixing up, and the perhaps insvm_vcpu_pre_run()fix up the control fields in vmcb02 that depend on it: namely thebus_lock_counter,next_rip, and soft_int_* fields.
Quoted 5 lines
Nah, not unless there's a meaningful, negative impact on a real world setup. And as above, in practice the only impact is effectively a performance blip due to generating an unwanted userspace exit. If userspace is stuffing nested state so often that that's actually a performance problem, then userspace can set regs before nested state.
But the wrong @vmcb12_rip also affects soft IRQ injection, which I know is not relevant here, but is part of the reason I brought this up.
If the guest has NRIPS, we use @vmcb12_rip to figure out if we need to reset next_rip after a VMRUN. If the guest does not have NRIPS, we put it directly in next_rip.
Either way we could end up with a messed up next_rip, and that can mess up the guest IIUC. Of course, all of that only happens if we migrate with a pending soft IRQ which is probably unlikely, but could happen.
Quoted 6 lines
There are a pile of edge cases and "what ifs" around this, none of which have a perfect answer since KVM doesn't know userspace's intent. And so I want to go with a solution that is as simple as possible without risking putting L2 into an infinite loop.
Quoted 8 lines
Quoted 4 lines
It gets worse because I think
next_ripis also not always properly saved/restored because we do not sync it from vmcb02 to cache innested_sync_control_from_vmcb02()-- but that one is not relevant tobus_lock_counterAFAICT.Correct.
All in all, I think just this?
For the bus lock thing that's probably the right thing to do. It fixes the usual nested VMRUN path and doesn't really make the KVM_SET_NESTED_STATE path any worse (feel free to add a Reviewed-by from me).
But I am wondering if we should also fix @vmcb12_rip in the KVM_SET_NESTED_STATE path (separately), for the soft IRQ case as well.
Quoted 14 lines
arch/x86/kvm/svm/nested.c +1
-1@@ -809,7 +809,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,* L1 re-enters L2, the same instruction will trigger a VM-Exit and the * entire cycle start over. */ -if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip)) +if (vmcb12_rip && (svm->nested.ctl.bus_lock_rip == vmcb12_rip)) vmcb02->control.bus_lock_counter =1 ;else vmcb02->control.bus_lock_counter =0 ;
PATCH v5 5/5 KVM: selftests: Add bus lock exit test
In reply to Manali ShuklaFrom: Nikunj A Dadhania <[email protected]>
Add a test case to verify the Bus Lock exit feature
The main thing that the selftest verifies is that when a Buslock is generated in the guest context, the KVM_EXIT_X86_BUS_LOCK is triggered for SVM or VMX when the KVM capability KVM_CAP_X86_BUS_LOCK_EXIT is enabled.
This test case also verifies the Bus Lock exit in nested scenario.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/kvm_buslock_test.c | 135 ++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/kvm_buslock_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -78,6 +78,7 @@ TEST_GEN_PROGS_x86 += x86/hyperv_svm_test TEST_GEN_PROGS_x86 += x86/hyperv_tlb_flush TEST_GEN_PROGS_x86 += x86/kvm_clock_test TEST_GEN_PROGS_x86 += x86/kvm_pv_test +TEST_GEN_PROGS_x86 += x86/kvm_buslock_test TEST_GEN_PROGS_x86 += x86/monitor_mwait_test TEST_GEN_PROGS_x86 += x86/nested_emulation_test TEST_GEN_PROGS_x86 += x86/nested_exceptions_test
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +135 -0
135 lines not on this page · raw message
Signature · 1 line
2.34.1
Sean Christopherson
maintainerRe: [PATCH v5 5/5] KVM: selftests: Add bus lock exit test
Quoted 12 lines · Manali Shukla
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +6
-0@@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2024 Advanced Micro Devices, Inc. +*/ + +#include "test_util.h"
tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 16
Quoted 10 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +10
-0+#include "kvm_util.h" +#include "processor.h" +#include "svm_util.h" +#include "vmx.h" + +#define NR_ITERATIONS 100 +#define L2_GUEST_STACK_SIZE 64 + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
Eww.
tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 21
Quoted 5 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +5
-0+ +struct buslock_test { +unsigned char pad[PAGE_SIZE -2 ]; +atomic_long_t val; +} __packed;
You don't need an entire page to generate a bus lock, two cache lines will do nicely. And there's certain no need for __packed.
tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 29
Quoted 8 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +8
-0+struct buslock_test test__aligned(PAGE_SIZE) ; + +static __always_inline void buslock_atomic_add(int i, atomic_long_t *v) +{ + asmvolatile (LOCK_PREFIX "addl %1,%0" + :"+m" (v->counter) + :"ir" (i) :"memory" ); +}
If only there were utilities for atomics...
tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 30
Quoted 1 line
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +1
-0+static void buslock_add (void )
guest_generate_buslocks()
tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 34
Quoted 4 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +4
-0+{ +/* +* Increment a page unaligned variable atomically. +* This should generate a bus lock exit.
Not should, will.
tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 37
Quoted 3 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +3
-0+*/ +for (int i =0 ; i <NR_ITERATIONS ; i++) +buslock_atomic_add (2 , &test.val);
Don't do weird and completely arbitrary things like adding '2' instead of '1', it makes readers look for intent and purpose that doesn't exist.
Quoted 1 line
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +1
-0+}
...
tools/testing/selftests/kvm/x86/kvm_buslock_test.c
Quoted 8 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +8
-0+int main (int argc,char *argv[]) +{ +struct kvm_vcpu *vcpu; +struct kvm_run *run; +struct kvm_vm *vm; +vm_vaddr_t nested_test_data_gva; + +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM ) ||kvm_cpu_has (X86_FEATURE_VMX ));
There's no reason to make nested support a hard dependency, it's just as easy to make it conditional.
Quoted 26 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +26
-0+TEST_REQUIRE (kvm_has_cap (KVM_CAP_X86_BUS_LOCK_EXIT )); + + vm =vm_create (1 ); +vm_enable_cap (vm,KVM_CAP_X86_BUS_LOCK_EXIT ,KVM_BUS_LOCK_DETECTION_EXIT ); + vcpu =vm_vcpu_add (vm,0 , guest_code); + +if (kvm_cpu_has (X86_FEATURE_SVM )) +vcpu_alloc_svm (vm, &nested_test_data_gva); +else +vcpu_alloc_vmx (vm, &nested_test_data_gva); + +vcpu_args_set (vcpu,1 , nested_test_data_gva); + + run = vcpu->run; + +for (;;) { +struct ucall uc; + +vcpu_run (vcpu); + +if (run->exit_reason ==KVM_EXIT_IO ) { +switch (get_ucall (vcpu, &uc)) { +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +/* NOT REACHED */ +case UCALL_SYNC :
tools/testing/selftests/kvm/x86/kvm_buslock_test.c
Quoted 10 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +10
-0+continue ; +case UCALL_DONE : + goto done; +default : +TEST_FAIL ("Unknown ucall 0x%lx." , uc.cmd); + } + } + +TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_X86_BUS_LOCK ); + }
*sigh*
This doesn't actually ****VERIFY**** that the expected number of bus lock exits were generated. KVM could literally do nothing and the test will pass. E.g. the test passes if I do this:
tools/testing/selftests/kvm/x86/kvm_buslock_test.c +2 -2
@@ -93,10 +93,10 @@ int main(int argc, char *argv[])vm_vaddr_t nested_test_data_gva;TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM ) ||kvm_cpu_has (X86_FEATURE_VMX )); -TEST_REQUIRE (kvm_has_cap (KVM_CAP_X86_BUS_LOCK_EXIT )); +// TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_BUS_LOCK_EXIT)); vm =vm_create (1 ); -vm_enable_cap (vm,KVM_CAP_X86_BUS_LOCK_EXIT ,KVM_BUS_LOCK_DETECTION_EXIT ); +// vm_enable_cap(vm, KVM_CAP_X86_BUS_LOCK_EXIT, KVM_BUS_LOCK_DETECTION_EXIT); vcpu =vm_vcpu_add (vm,0 , guest_code);if (kvm_cpu_has (X86_FEATURE_SVM ))
--
The test would also fail to detect if KVM completely skipped the instruction.
This is not rocket science. If you can't make your test fail by introducing bugs in what you're testing, then your test is worthless.
No need for a v6, I'm going to do surgery when I apply, this series has dragged on for far too long.
Re: [PATCH v5 5/5] KVM: selftests: Add bus lock exit test
Quoted 57 lines · Sean Christopherson
Quoted 12 lines · Manali Shukla
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +6
-0@@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2024 Advanced Micro Devices, Inc. +*/ + +#include "test_util.h" tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 16
Quoted 10 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +10
-0+#include "kvm_util.h" +#include "processor.h" +#include "svm_util.h" +#include "vmx.h" + +#define NR_ITERATIONS 100 +#define L2_GUEST_STACK_SIZE 64 + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Waddress-of-packed-member"Eww.
tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 21
Quoted 5 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +5
-0+ +struct buslock_test { +unsigned char pad[PAGE_SIZE -2 ]; +atomic_long_t val; +} __packed;You don't need an entire page to generate a bus lock, two cache lines will do nicely. And there's certain no need for
__packed.tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 29
Quoted 8 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +8
-0+struct buslock_test test__aligned(PAGE_SIZE) ; + +static __always_inline void buslock_atomic_add(int i, atomic_long_t *v) +{ + asmvolatile (LOCK_PREFIX "addl %1,%0" + :"+m" (v->counter) + :"ir" (i) :"memory" ); +}If only there were utilities for atomics...
tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 30
Quoted 1 line
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +1
-0+static void buslock_add (void )
guest_generate_buslocks()tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 34
Quoted 4 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +4
-0+{ +/* +* Increment a page unaligned variable atomically. +* This should generate a bus lock exit. Not should, will.
Quoted 7 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c line 37
Quoted 3 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +3
-0+*/ +for (int i =0 ; i <NR_ITERATIONS ; i++) +buslock_atomic_add (2 , &test.val);Don't do weird and completely arbitrary things like adding '2' instead of '1', it makes readers look for intent and purpose that doesn't exist.
Got it. Thanks for the feedback.
Quoted 78 lines
Quoted 1 line
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +1
-0+}...
tools/testing/selftests/kvm/x86/kvm_buslock_test.c
Quoted 8 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +8
-0+int main (int argc,char *argv[]) +{ +struct kvm_vcpu *vcpu; +struct kvm_run *run; +struct kvm_vm *vm; +vm_vaddr_t nested_test_data_gva; + +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM ) ||kvm_cpu_has (X86_FEATURE_VMX ));There's no reason to make nested support a hard dependency, it's just as easy to make it conditional.
Quoted 26 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +26
-0+TEST_REQUIRE (kvm_has_cap (KVM_CAP_X86_BUS_LOCK_EXIT )); + + vm =vm_create (1 ); +vm_enable_cap (vm,KVM_CAP_X86_BUS_LOCK_EXIT ,KVM_BUS_LOCK_DETECTION_EXIT ); + vcpu =vm_vcpu_add (vm,0 , guest_code); + +if (kvm_cpu_has (X86_FEATURE_SVM )) +vcpu_alloc_svm (vm, &nested_test_data_gva); +else +vcpu_alloc_vmx (vm, &nested_test_data_gva); + +vcpu_args_set (vcpu,1 , nested_test_data_gva); + + run = vcpu->run; + +for (;;) { +struct ucall uc; + +vcpu_run (vcpu); + +if (run->exit_reason ==KVM_EXIT_IO ) { +switch (get_ucall (vcpu, &uc)) { +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +/* NOT REACHED */ +case UCALL_SYNC :tools/testing/selftests/kvm/x86/kvm_buslock_test.c
Quoted 10 lines
tools/testing/selftests/kvm/x86/kvm_buslock_test.c new file +10
-0+continue ; +case UCALL_DONE : + goto done; +default : +TEST_FAIL ("Unknown ucall 0x%lx." , uc.cmd); + } + } + +TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_X86_BUS_LOCK ); + }*sigh*
This doesn't actually ****VERIFY**** that the expected number of bus lock exits were generated. KVM could literally do nothing and the test will pass. E.g. the test passes if I do this:
tools/testing/selftests/kvm/x86/kvm_buslock_test.c +2
-3@@ -93,10 +93,10 @@ int main(int argc, char *argv[])vm_vaddr_t nested_test_data_gva;TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM ) ||kvm_cpu_has (X86_FEATURE_VMX )); -TEST_REQUIRE (kvm_has_cap (KVM_CAP_X86_BUS_LOCK_EXIT )); +// TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_BUS_LOCK_EXIT)); vm =vm_create (1 ); -vm_enable_cap (vm,KVM_CAP_X86_BUS_LOCK_EXIT ,KVM_BUS_LOCK_DETECTION_EXIT ); +// vm_enable_cap(vm, KVM_CAP_X86_BUS_LOCK_EXIT, KVM_BUS_LOCK_DETECTION_EXIT); vcpu =vm_vcpu_add (vm,0 , guest_code);if (kvm_cpu_has (X86_FEATURE_SVM )) --
Quoted 7 lines
The test would also fail to detect if KVM completely skipped the instruction.
This is not rocket science. If you can't make your test fail by introducing bugs in what you're testing, then your test is worthless.
No need for a v6, I'm going to do surgery when I apply, this series has dragged on for far too long.
Understood. I agree the test should catch such failures — I’ll take that into account in future work. Thanks for the review and for pushing this forward.
-Manali