Add a test case for KVM_X86_DISABLE_EXIT
v2 cover Add a test case for KVM_X86_DISABLE_EXIT
By default, HLT instruction executed by guest is intercepted by hypervisor. However, KVM_CAP_X86_DISABLE_EXITS capability can be used to not intercept HLT by setting KVM_X86_DISABLE_EXITS_HLT.
By default, vms are created with in-kernel APIC support in KVM selftests. VM needs to be created without in-kernel APIC support for this test, so that HLT will exit to userspace. To do so, __vm_create() is modified to not call KVM_CREATE_IRQCHIP ioctl while creating vm.
Add a test case to test KVM_X86_DISABLE_EXITS_HLT functionality.
Patch 1, 2 -> Preparatory patches to add the KVM_X86_DISABLE_EXITS_HLT test
case
Patch 3 -> Adds a test case for KVM_X86_DISABLE_EXITS_HLT
Testing done:
Tested KVM_X86_DISABLE_EXITS_HLT test case on AMD and Intel machines.
v1 -> v2
- Extended @shape to allow creation of VM without in-kernel APIC support (Andrew Jones)
- Changed the test case based on Andrew's comments.
- Few more changes to the test case to pass the address of the flag on which guest waits to execute HLT.
Manali Shukla (3):
KVM: selftests: Add safe_halt() and cli() helpers to common code
KVM: selftests: Extend @shape to allow creation of VM without
in-kernel APIC
KVM: selftests: Add a test case for KVM_X86_DISABLE_EXITS_HLT
tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/kvm_util_base.h | 17 ++- .../selftests/kvm/include/x86_64/processor.h | 17 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 1 + .../selftests/kvm/lib/x86_64/processor.c | 4 +- .../kvm/x86_64/halt_disable_exit_test.c | 120 ++++++++++++++++++ 6 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/halt_disable_exit_test.c
base-commit: e9da6f08edb0bd4c621165496778d77a222e1174
Signature · 1 line
2.34.1
PATCH v2 1/3 KVM: selftests: Add safe_halt() and cli() helpers to common code
Add safe_halt() and cli() helpers to processor.h to make them broadly available in KVM selftests.
--- .../selftests/kvm/include/x86_64/processor.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
tools/testing/selftests/kvm/include/x86_64/processor.h +17 -0
@@ -1217,6 +1217,23 @@ static inline void kvm_hypercall_map_gpa_range(uint64_t gpa, uint64_t size,GUEST_ASSERT (!ret); } +/* +* Execute HLT in an STI interrupt shadow to ensure that a pending IRQ that's +* intended to be a wake event arrives *after* HLT is executed. Modern CPUs, +* except for a few oddballs that KVM is unlikely to run on, block IRQs for one +* instruction after STI, *if* RFLAGS.IF=0 before STI. Note, Intel CPUs may +* block other events beyond regular IRQs, e.g. may block NMIs and SMIs too. +*/ +static inline void safe_halt (void ) +{ + asmvolatile ("sti; hlt" ); +} + +static inline void cli (void ) +{ + asmvolatile ("cli" ); +} +void __vm_xsave_require_permission (uint64_t xfeature,const char *name);#define vm_xsave_require_permission (xfeature) \
Signature · 1 line
2.34.1
PATCH v2 2/3 KVM: selftests: Extend @shape to allow creation of VM without in-kernel APIC
In reply to Manali ShuklaCurrently, all the VMs are created with in-kernel APIC support in KVM selftests because KVM_CREATE_IRQCHIP ioctl is called by default from kvm_arch_vm_post_create().
Carve out space in the @shape passed to the various VM creation helpers to allow using the shape to control creation of a VM without in-kernel APIC support or with in-kernel APIC support.
This is a preparatory patch to create a vm without in-kernel APIC support for the KVM_X86_DISABLE_EXITS_HLT test.
--- .../selftests/kvm/include/kvm_util_base.h | 17 ++++++++++++++++- tools/testing/selftests/kvm/lib/kvm_util.c | 1 + .../selftests/kvm/lib/x86_64/processor.c | 4 +++- 3 files changed, 20 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +16 -1
@@ -130,6 +130,7 @@ struct kvm_vm {* memslot. */ uint32_t memslots[NR_MEM_REGIONS ]; +uint8_t flags; };struct vcpu_reg_sublist {
@@ -197,11 +198,14 @@ enum vm_guest_mode { NUM_VM_MODES, }; +#define NO_IRQCHIP 0x01 +struct vm_shape {uint32_t type;uint8_t mode;uint8_t subtype; -uint16_t padding; +uint8_t flags; +uint8_t padding; };kvm_static_assert (sizeof (struct vm_shape ) ==sizeof (uint64_t ));
@@ -218,6 +222,17 @@ kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t)); shape; \ }) +#define VM_SHAPE_FLAGS (__FLAGS) \ +({ \ + struct vm_shape shape = { \ + .mode = VM_MODE_DEFAULT, \ + .type = VM_TYPE_DEFAULT, \ + .flags = __FLAGS \ + }; \ + \ + shape; \ +}) +#if defined(__aarch64__)extern enum vm_guest_mode vm_mode_default;
tools/testing/selftests/kvm/lib/kvm_util.c +1 -0
@@ -226,6 +226,7 @@ struct kvm_vm *____vm_create(struct vm_shape shape) vm->mode = shape.mode; vm->type = shape.type; vm->subtype = shape.subtype; + vm->flags = shape.flags; vm->pa_bits = vm_guest_mode_params[vm->mode].pa_bits; vm->va_bits = vm_guest_mode_params[vm->mode].va_bits;
tools/testing/selftests/kvm/lib/x86_64/processor.c +3 -1
@@ -574,7 +574,9 @@ static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu)void kvm_arch_vm_post_create (struct kvm_vm *vm) { -vm_create_irqchip (vm); +if (!(vm->flags &NO_IRQCHIP )) +vm_create_irqchip (vm); +sync_global_to_guest (vm, host_cpu_is_intel);sync_global_to_guest (vm, host_cpu_is_amd);
Signature · 1 line
2.34.1
Sean Christopherson
reviewerRe: [PATCH v2 2/3] KVM: selftests: Extend @shape to allow creation of VM without in-kernel APIC
Quoted 10 lines · Manali Shukla
Currently, all the VMs are created with in-kernel APIC support in KVM selftests because
KVM_CREATE_IRQCHIPioctl is called by default fromkvm_arch_vm_post_create().Carve out space in the @shape passed to the various VM creation helpers to allow using the shape to control creation of a VM without in-kernel APIC support or with in-kernel APIC support.
This is a preparatory patch to create a vm without in-kernel APIC support for the
KVM_X86_DISABLE_EXITS_HLTtest.
Ugh, when I suggested creating a VM without an in-kernel APIC as away to easily test that HLT doesn't exit, I wasn't thinking about the side effects of creating a runnable VM without an in-kernel APIC. The other downside is that practically no one uses a userspace local APIC these days, i.e. the selftest isn't a great representation of real world setups.
Given that KVM already provides vcpu->stat.halt_exits, using a stats FD for verifying exiting behavior is probably a better option. The other check that could be added would be to verify that mp_state is always RUNNABLE (which is a bug/gap in KVM as migrating a vCPU that was halted in the guest won't resume in a halted state on the target).
Re: [PATCH v2 2/3] KVM: selftests: Extend @shape to allow creation of VM without in-kernel APIC
Hi Sean,
Quoted 18 lines · Sean Christopherson
Quoted 10 lines · Manali Shukla
Currently, all the VMs are created with in-kernel APIC support in KVM selftests because
KVM_CREATE_IRQCHIPioctl is called by default fromkvm_arch_vm_post_create().Carve out space in the @shape passed to the various VM creation helpers to allow using the shape to control creation of a VM without in-kernel APIC support or with in-kernel APIC support.
This is a preparatory patch to create a vm without in-kernel APIC support for the
KVM_X86_DISABLE_EXITS_HLTtest.Ugh, when I suggested creating a VM without an in-kernel APIC as away to easily test that HLT doesn't exit, I wasn't thinking about the side effects of creating a runnable VM without an in-kernel APIC. The other downside is that practically no one uses a userspace local APIC these days, i.e. the selftest isn't a great representation of real world setups.
Quoted 5 lines
Given that KVM already provides
vcpu->stat.halt_exits, using a stats FD for verifying exiting behavior is probably a better option. The other check that could be added would be to verify thatmp_stateis always RUNNABLE (which is a bug/gap in KVM as migrating a vCPU that was halted in the guest won't resume in a halted state on the target).
Sure. I will work on it.
- Manali
PATCH v2 3/3 KVM: selftests: Add a test case for KVM_X86_DISABLE_EXITS_HLT
In reply to Manali ShuklaBy default, HLT instruction executed by guest is intercepted by hypervisor. However, KVM_CAP_X86_DISABLE_EXITS capability can be used to not intercept HLT by setting KVM_X86_DISABLE_EXITS_HLT.
Add a test case to test KVM_X86_DISABLE_EXITS_HLT functionality.
--- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/halt_disable_exit_test.c | 119 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/halt_disable_exit_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -89,6 +89,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test TEST_GEN_PROGS_x86_64 += x86_64/smm_test TEST_GEN_PROGS_x86_64 += x86_64/state_test +TEST_GEN_PROGS_x86_64 += x86_64/halt_disable_exit_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test TEST_GEN_PROGS_x86_64 += x86_64/svm_int_ctl_test
tools/testing/selftests/kvm/x86_64/halt_disable_exit_test.c new file +119 -0
119 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 3/3] KVM: selftests: Add a test case for KVM_X86_DISABLE_EXITS_HLT
Quoted 43 lines · Manali Shukla
By default, HLT instruction executed by guest is intercepted by hypervisor. However,
KVM_CAP_X86_DISABLE_EXITScapability can be used to not intercept HLT by settingKVM_X86_DISABLE_EXITS_HLT.Add a test case to test
KVM_X86_DISABLE_EXITS_HLTfunctionality.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Manali Shukla <[email protected]>--- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/halt_disable_exit_test.c | 119 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/halt_disable_exit_test.ctools/testing/selftests/kvm/Makefile +1
-0@@ -89,6 +89,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test TEST_GEN_PROGS_x86_64 += x86_64/smm_test TEST_GEN_PROGS_x86_64 += x86_64/state_test +TEST_GEN_PROGS_x86_64 += x86_64/halt_disable_exit_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test TEST_GEN_PROGS_x86_64 += x86_64/svm_int_ctl_testtools/testing/selftests/kvm/x86_64/halt_disable_exit_test.c new file +11
-0@@ -0,0 +1,119 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* KVM disable halt exit test +* +* Copyright (C) 2024 Advanced Micro Devices, Inc. +*/ +#include <pthread.h> +#include <signal.h> +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h"
tools/testing/selftests/kvm/x86_64/halt_disable_exit_test.c line 21
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/halt_disable_exit_test.c new file +10
-0+ +#define SIG_IPI SIGUSR1 +static pthread_t task_thread, vcpu_thread; + +static void guest_code (uint64_t *is_hlt_exec) +{ +while (!READ_ONCE (*is_hlt_exec)) + ; + +safe_halt ();
May I confirm if this selftest works on nested L1 VM as a hypervisor?
Thank you very much!
Dongli Zhang
Re: [PATCH v2 3/3] KVM: selftests: Add a test case for KVM_X86_DISABLE_EXITS_HLT
Quoted 50 lines · Dongli Zhang
Quoted 47 lines · Manali Shukla
By default, HLT instruction executed by guest is intercepted by hypervisor. However,
KVM_CAP_X86_DISABLE_EXITScapability can be used to not intercept HLT by settingKVM_X86_DISABLE_EXITS_HLT.Add a test case to test
KVM_X86_DISABLE_EXITS_HLTfunctionality.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Manali Shukla <[email protected]>--- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/halt_disable_exit_test.c | 119 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/halt_disable_exit_test.ctools/testing/selftests/kvm/Makefile +1
-0@@ -89,6 +89,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test TEST_GEN_PROGS_x86_64 += x86_64/smm_test TEST_GEN_PROGS_x86_64 += x86_64/state_test +TEST_GEN_PROGS_x86_64 += x86_64/halt_disable_exit_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test TEST_GEN_PROGS_x86_64 += x86_64/svm_int_ctl_testtools/testing/selftests/kvm/x86_64/halt_disable_exit_test.c new file +15
-0@@ -0,0 +1,119 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* KVM disable halt exit test +* +* Copyright (C) 2024 Advanced Micro Devices, Inc. +*/ +#include <pthread.h> +#include <signal.h> +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" + +#define SIG_IPI SIGUSR1 +static pthread_t task_thread, vcpu_thread; +
Quoted 8 lines
tools/testing/selftests/kvm/x86_64/halt_disable_exit_test.c line 21
Quoted 6 lines
tools/testing/selftests/kvm/x86_64/halt_disable_exit_test.c new file +6
-0+static void guest_code (uint64_t *is_hlt_exec) +{ +while (!READ_ONCE (*is_hlt_exec)) + ; + +safe_halt ();May I confirm if this selftest works on nested L1 VM as a hypervisor?
Yes, this selftest works on nested L1 VM.
Quoted 4 lines
Thank you very much!
Dongli Zhang