Fix VMX preemption timer migration
Subsystems (5)
v4 cover Fix VMX preemption timer migration
v2: Added an u32 flags to the kvm_vmx_nested_state_hdr to check the validity of the preemption timer deadline. [Paolo]
v3: Moved the code to calculate timer value to a separate helper [Sean]
v3: Rename l1_scaled_tsc_value as l1_scaled_tsc. [Sean]
v3: Removed unncessary parenthesis and moved the >> operator to previous line [Sean]
v3: Changed >= to > for comparision between l1_scaled_tsc and
vmx->nested.preemption_timer_deadline [Sean]
v3: Removed the extra if check in sync_vmcs02_to_vmcs12 [Sean]
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 | 5 + arch/x86/include/uapi/asm/kvm.h | 4 + arch/x86/kvm/vmx/nested.c | 58 +++- arch/x86/kvm/vmx/vmx.h | 2 + arch/x86/kvm/x86.c | 1 + include/uapi/linux/kvm.h | 1 + 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 ++++++++++++++++++ 13 files changed, 356 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/vmx_preemption_timer_test.c
--
2.27.0.rc0.183.gde8f92d652-goog
PATCH v4 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 | 5 +++ arch/x86/include/uapi/asm/kvm.h | 4 +++ arch/x86/kvm/vmx/nested.c | 58 ++++++++++++++++++++++++++++----- arch/x86/kvm/vmx/vmx.h | 2 ++ arch/x86/kvm/x86.c | 1 + include/uapi/linux/kvm.h | 1 + 6 files changed, 62 insertions(+), 9 deletions(-)
Documentation/virt/kvm/api.rst +5 -0
@@ -4326,6 +4326,9 @@ Errors: #define KVM_STATE_NESTED_RUN_PENDING 0x00000002 #define KVM_STATE_NESTED_EVMCS 0x00000004 + /* Available with KVM_CAP_NESTED_STATE_PREEMPTION_TIMER */ + #define KVM_STATE_NESTED_PREEMPTION_TIMER 0x00000010 + #define KVM_STATE_NESTED_FORMAT_VMX 0 #define KVM_STATE_NESTED_FORMAT_SVM 1
@@ -4335,8 +4338,10 @@ Errors: #define KVM_STATE_NESTED_VMX_SMM_VMXON 0x00000002 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
@@ -391,6 +391,8 @@ struct kvm_sync_regs {#define KVM_STATE_NESTED_RUN_PENDING 0x00000002#define KVM_STATE_NESTED_EVMCS 0x00000004#define KVM_STATE_NESTED_MTF_PENDING 0x00000008 +/* Available with KVM_CAP_NESTED_STATE_PREEMPTION_TIMER */ +#define KVM_STATE_NESTED_PREEMPTION_TIMER 0x00000010#define KVM_STATE_NESTED_SMM_GUEST_MODE 0x00000001#define KVM_STATE_NESTED_SMM_VMXON 0x00000002
@@ -403,8 +405,10 @@ struct kvm_vmx_nested_state_data { };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 +49 -9
115 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 */
arch/x86/kvm/x86.c +1 -0
@@ -3418,6 +3418,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_MSR_PLATFORM_INFO :case KVM_CAP_EXCEPTION_PAYLOAD :case KVM_CAP_SET_GUEST_DEBUG : +case KVM_CAP_NESTED_STATE_PREEMPTION_TIMER : r =1 ;break ;case KVM_CAP_SYNC_REGS :
include/uapi/linux/kvm.h +1 -0
@@ -1018,6 +1018,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_S390_PROTECTED 180#define KVM_CAP_PPC_SECURE_GUEST 181#define KVM_CAP_HALT_POLL 182 +#define KVM_CAP_NESTED_STATE_PREEMPTION_TIMER 183#ifdef KVM_CAP_IRQ_ROUTING
--
2.27.0.rc0.183.gde8f92d652-goog
Paolo Bonzini
maintainerRe: [PATCH 1/2 v4] KVM: nVMX: Fix VMX preemption timer migration
Review
Quoted 5 lines · Makarand Sonare
#define KVM_STATE_NESTED_RUN_PENDING 0x00000002 #define KVM_STATE_NESTED_EVMCS 0x00000004(quoted diff) +2
-0+ /* Available with KVM_CAP_NESTED_STATE_PREEMPTION_TIMER */ + #define KVM_STATE_NESTED_PREEMPTION_TIMER 0x00000010
Putting this here is confusing, please rename it to KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE and number it 0x1. Also, I think the capability is not needed since userspace can expect the flags to be 0 on older kernels.
Paolo
PATCH v4 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
@@ -20,6 +20,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