Fix VMX preemption timer migration
Subsystems (5)
v5 cover Fix VMX preemption timer migration
v4: Renamed flag to KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE
and removed the KVM_CAP_NESTED_STATE_PREEMPTION_TIMER check [Paolo]
Makarand Sonare (1):
KVM: selftests: VMX preemption timer migration test
Peter Shier (1):
KVM: nVMX: Fix VMX preemption timer migration
Documentation/virt/kvm/api.rst | 4 + arch/x86/include/uapi/asm/kvm.h | 4 + arch/x86/kvm/vmx/nested.c | 55 +++- arch/x86/kvm/vmx/vmx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 1 + tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/include/kvm_util.h | 2 + .../selftests/kvm/include/x86_64/processor.h | 11 +- .../selftests/kvm/include/x86_64/vmx.h | 27 ++ .../kvm/x86_64/vmx_preemption_timer_test.c | 255 ++++++++++++++++++ 11 files changed, 351 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/vmx_preemption_timer_test.c
--
2.27.0.rc0.183.gde8f92d652-goog
PATCH v5 1/2 KVM: nVMX: Fix VMX preemption timer migration
From: Peter Shier <[email protected]>
Add new field to hold preemption timer expiration deadline appended to struct kvm_vmx_nested_state_hdr. This is to prevent the first VM-Enter after migration from incorrectly restarting the timer with the full timer value instead of partially decayed timer value. KVM_SET_NESTED_STATE restarts timer using migrated state regardless of whether L1 sets VM_EXIT_SAVE_VMX_PREEMPTION_TIMER.
--- Documentation/virt/kvm/api.rst | 4 +++ arch/x86/include/uapi/asm/kvm.h | 4 +++ arch/x86/kvm/vmx/nested.c | 55 ++++++++++++++++++++++++++++----- arch/x86/kvm/vmx/vmx.h | 2 ++ 4 files changed, 57 insertions(+), 8 deletions(-)
Documentation/virt/kvm/api.rst +4 -0
@@ -4334,9 +4334,13 @@ Errors: #define KVM_STATE_NESTED_VMX_SMM_GUEST_MODE 0x00000001 #define KVM_STATE_NESTED_VMX_SMM_VMXON 0x00000002 +#define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001 + struct kvm_vmx_nested_state_hdr { + __u32 flags; __u64 vmxon_pa; __u64 vmcs12_pa; + __u64 preemption_timer_deadline; struct { __u16 flags;
arch/x86/include/uapi/asm/kvm.h +4 -0
@@ -397,14 +397,18 @@ struct kvm_sync_regs {#define KVM_STATE_NESTED_VMX_VMCS_SIZE 0x1000 +#define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001 +struct kvm_vmx_nested_state_data {__u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE ];__u8 shadow_vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE ]; };struct kvm_vmx_nested_state_hdr { +__u32 flags;__u64 vmxon_pa;__u64 vmcs12_pa; +__u64 preemption_timer_deadline;struct {__u16 flags;
arch/x86/kvm/vmx/nested.c +47 -8
106 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.h +2 -0
@@ -169,6 +169,8 @@ struct nested_vmx {u16 posted_intr_nv;struct hrtimer preemption_timer; +u64 preemption_timer_deadline; +bool has_preemption_timer_deadline;bool preemption_timer_expired;/* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
--
2.27.0.rc0.183.gde8f92d652-goog
Re: [PATCH 1/2 v5] KVM: nVMX: Fix VMX preemption timer migration
Review
Quoted 13 lines · Makarand Sonare
(quoted diff) +13
-0+ + u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >> + VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; + + if (!vmx->nested.has_preemption_timer_deadline) { + timer_value = vmcs12->vmx_preemption_timer_value; + vmx->nested.preemption_timer_deadline = timer_value + + l1_scaled_tsc; + vmx->nested.has_preemption_timer_deadline = true; + } else if (l1_scaled_tsc < vmx->nested.preemption_timer_deadline) + timer_value = vmx->nested.preemption_timer_deadline - + l1_scaled_tsc; + return timer_value;
Queued, thanks! Just a tiny change that I squashed here:
arch/x86/kvm/vmx/nested.c +4 -8
@@ -2091,20 +2091,16 @@ static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); -u64 timer_value =0 ;u64 l1_scaled_tsc =kvm_read_l1_tsc (vcpu,rdtsc ()) >>VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE ;if (!vmx->nested.has_preemption_timer_deadline) { - timer_value = vmcs12->vmx_preemption_timer_value; - vmx->nested.preemption_timer_deadline = timer_value + - l1_scaled_tsc; + vmx->nested.preemption_timer_deadline = + vmcs12->vmx_preemption_timer_value + l1_scaled_tsc; vmx->nested.has_preemption_timer_deadline = true; - }else if (l1_scaled_tsc < vmx->nested.preemption_timer_deadline) - timer_value = vmx->nested.preemption_timer_deadline - - l1_scaled_tsc; -return timer_value; + } +return vmx->nested.preemption_timer_deadline - l1_scaled_tsc; } static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
Thanks,
Paolo
PATCH v5 2/2 KVM: selftests: VMX preemption timer migration test
In reply to Makarand SonareWhen a nested VM with a VMX-preemption timer is migrated, verify that the nested VM and its parent VM observe the VMX-preemption timer exit close to the original expiration deadline.
--- tools/arch/x86/include/uapi/asm/kvm.h | 1 + tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/include/kvm_util.h | 2 + .../selftests/kvm/include/x86_64/processor.h | 11 +- .../selftests/kvm/include/x86_64/vmx.h | 27 ++ .../kvm/x86_64/vmx_preemption_timer_test.c | 255 ++++++++++++++++++ 7 files changed, 294 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/vmx_preemption_timer_test.c
tools/arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -400,6 +400,7 @@ struct kvm_sync_regs {struct kvm_vmx_nested_state_data {__u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE ];__u8 shadow_vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE ]; +__u64 preemption_timer_deadline; };struct kvm_vmx_nested_state_hdr {
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -10,6 +10,7 @@ /x86_64/set_sregs_test /x86_64/smm_test /x86_64/state_test +/x86_64/vmx_preemption_timer_test /x86_64/svm_vmcall_test /x86_64/sync_regs_test /x86_64/vmx_close_while_nested_test
tools/testing/selftests/kvm/Makefile +1 -0
@@ -46,6 +46,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_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/vmx_preemption_timer_test TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test TEST_GEN_PROGS_x86_64 += x86_64/sync_regs_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
@@ -314,6 +314,8 @@ void ucall_uninit(struct kvm_vm *vm);void ucall (uint64_t cmd,int nargs, ...);uint64_t get_ucall (struct kvm_vm *vm,uint32_t vcpu_id,struct ucall *uc); +#define GUEST_SYNC_ARGS (stage, arg1, arg2, arg3, arg4) \ + ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)#define GUEST_SYNC (stage) ucall(UCALL_SYNC, 2, "hello", stage)#define GUEST_DONE () ucall(UCALL_DONE, 0) #define __GUEST_ASSERT(_condition, _nargs, _args...) do { \
tools/testing/selftests/kvm/include/x86_64/processor.h +7 -4
@@ -79,13 +79,16 @@ static inline uint64_t get_desc64_base(const struct desc64 *desc)static inline uint64_t rdtsc (void ) {uint32_t eax, edx; - +uint32_t tsc_val;/* * The lfence is to wait (on Intel CPUs) until all previous -* instructions have been executed. +* instructions have been executed. If software requires RDTSC to be +* executed prior to execution of any subsequent instruction, it can +* execute LFENCE immediately after RDTSC */ - __asm__ __volatile__("lfence; rdtsc" :"=a" (eax),"=d" (edx)); -return ((uint64_t )edx) <<32 | eax; + __asm__ __volatile__("lfence; rdtsc; lfence" :"=a" (eax),"=d" (edx)); + tsc_val = ((uint64_t )edx) <<32 | eax; +return tsc_val; }static inline uint64_t rdtscp (uint32_t *aux)
tools/testing/selftests/kvm/include/x86_64/vmx.h +27 -0
@@ -575,6 +575,33 @@ struct vmx_pages {void *eptp; }; +union vmx_basic { +u64 val; +struct { +u32 revision; +u32 size:13 , + reserved1:3 , + width:1 , + dual:1 , + type:4 , + insouts:1 , + ctrl:1 , + vm_entry_exception_ctrl:1 , + reserved2:7 ; + }; +}; + +union vmx_ctrl_msr { +u64 val; +struct { +u32 set, clr; + }; +}; + +union vmx_basic basic; +union vmx_ctrl_msr ctrl_pin_rev; +union vmx_ctrl_msr ctrl_exit_rev; +struct vmx_pages *vcpu_alloc_vmx (struct kvm_vm *vm,vm_vaddr_t *p_vmx_gva);bool prepare_for_vmx_operation (struct vmx_pages *vmx);void prepare_vmcs (struct vmx_pages *vmx,void *guest_rip,void *guest_rsp);
tools/testing/selftests/kvm/x86_64/vmx_preemption_timer_test.c new file +255 -0
255 lines not on this page · raw message
--
2.27.0.rc0.183.gde8f92d652-goog