Skip to content

Commit 850448f

Browse files
Peter Shierbonzini
authored andcommitted
KVM: nVMX: Fix VMX preemption timer migration
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. Fixes: cf8b84f ("kvm: nVMX: Prepare for checkpointing L2 state") Signed-off-by: Peter Shier <[email protected]> Signed-off-by: Makarand Sonare <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent f7d31e6 commit 850448f

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

Documentation/virt/kvm/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4334,9 +4334,13 @@ Errors:
43344334
#define KVM_STATE_NESTED_VMX_SMM_GUEST_MODE 0x00000001
43354335
#define KVM_STATE_NESTED_VMX_SMM_VMXON 0x00000002
43364336

4337+
#define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001
4338+
43374339
struct kvm_vmx_nested_state_hdr {
4340+
__u32 flags;
43384341
__u64 vmxon_pa;
43394342
__u64 vmcs12_pa;
4343+
__u64 preemption_timer_deadline;
43404344

43414345
struct {
43424346
__u16 flags;

arch/x86/include/uapi/asm/kvm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,18 @@ struct kvm_sync_regs {
400400

401401
#define KVM_STATE_NESTED_SVM_VMCB_SIZE 0x1000
402402

403+
#define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001
403404

404405
struct kvm_vmx_nested_state_data {
405406
__u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
406407
__u8 shadow_vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
407408
};
408409

409410
struct kvm_vmx_nested_state_hdr {
411+
__u32 flags;
410412
__u64 vmxon_pa;
411413
__u64 vmcs12_pa;
414+
__u64 preemption_timer_deadline;
412415

413416
struct {
414417
__u16 flags;

arch/x86/kvm/vmx/nested.c

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,9 +2087,29 @@ static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
20872087
return HRTIMER_NORESTART;
20882088
}
20892089

2090-
static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
2090+
static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
2091+
{
2092+
struct vcpu_vmx *vmx = to_vmx(vcpu);
2093+
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2094+
u64 timer_value = 0;
2095+
2096+
u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2097+
VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2098+
2099+
if (!vmx->nested.has_preemption_timer_deadline) {
2100+
timer_value = vmcs12->vmx_preemption_timer_value;
2101+
vmx->nested.preemption_timer_deadline = timer_value +
2102+
l1_scaled_tsc;
2103+
vmx->nested.has_preemption_timer_deadline = true;
2104+
} else if (l1_scaled_tsc < vmx->nested.preemption_timer_deadline)
2105+
timer_value = vmx->nested.preemption_timer_deadline -
2106+
l1_scaled_tsc;
2107+
return timer_value;
2108+
}
2109+
2110+
static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2111+
u64 preemption_timeout)
20912112
{
2092-
u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
20932113
struct vcpu_vmx *vmx = to_vmx(vcpu);
20942114

20952115
/*
@@ -3348,8 +3368,10 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
33483368
* the timer.
33493369
*/
33503370
vmx->nested.preemption_timer_expired = false;
3351-
if (nested_cpu_has_preemption_timer(vmcs12))
3352-
vmx_start_preemption_timer(vcpu);
3371+
if (nested_cpu_has_preemption_timer(vmcs12)) {
3372+
u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3373+
vmx_start_preemption_timer(vcpu, timer_value);
3374+
}
33533375

33543376
/*
33553377
* Note no nested_vmx_succeed or nested_vmx_fail here. At this point
@@ -3457,6 +3479,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
34573479
* the nested entry.
34583480
*/
34593481
vmx->nested.nested_run_pending = 1;
3482+
vmx->nested.has_preemption_timer_deadline = false;
34603483
status = nested_vmx_enter_non_root_mode(vcpu, true);
34613484
if (unlikely(status != NVMX_VMENTRY_SUCCESS))
34623485
goto vmentry_failed;
@@ -3957,9 +3980,10 @@ static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
39573980
vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
39583981

39593982
if (nested_cpu_has_preemption_timer(vmcs12) &&
3960-
vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
3961-
vmcs12->vmx_preemption_timer_value =
3962-
vmx_get_preemption_timer_value(vcpu);
3983+
vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
3984+
!vmx->nested.nested_run_pending)
3985+
vmcs12->vmx_preemption_timer_value =
3986+
vmx_get_preemption_timer_value(vcpu);
39633987

39643988
/*
39653989
* In some cases (usually, nested EPT), L2 is allowed to change its
@@ -5891,8 +5915,10 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
58915915
.flags = 0,
58925916
.format = KVM_STATE_NESTED_FORMAT_VMX,
58935917
.size = sizeof(kvm_state),
5918+
.hdr.vmx.flags = 0,
58945919
.hdr.vmx.vmxon_pa = -1ull,
58955920
.hdr.vmx.vmcs12_pa = -1ull,
5921+
.hdr.vmx.preemption_timer_deadline = 0,
58965922
};
58975923
struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
58985924
&user_kvm_nested_state->data.vmx[0];
@@ -5934,6 +5960,14 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
59345960

59355961
if (vmx->nested.mtf_pending)
59365962
kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
5963+
5964+
if (nested_cpu_has_preemption_timer(vmcs12) &&
5965+
vmx->nested.has_preemption_timer_deadline) {
5966+
kvm_state.hdr.vmx.flags |=
5967+
KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
5968+
kvm_state.hdr.vmx.preemption_timer_deadline =
5969+
vmx->nested.preemption_timer_deadline;
5970+
}
59375971
}
59385972
}
59395973

@@ -5979,7 +6013,6 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
59796013
get_shadow_vmcs12(vcpu), VMCS12_SIZE))
59806014
return -EFAULT;
59816015
}
5982-
59836016
out:
59846017
return kvm_state.size;
59856018
}
@@ -6141,6 +6174,12 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
61416174
goto error_guest_mode;
61426175
}
61436176

6177+
if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6178+
vmx->nested.has_preemption_timer_deadline = true;
6179+
vmx->nested.preemption_timer_deadline =
6180+
kvm_state->hdr.vmx.preemption_timer_deadline;
6181+
}
6182+
61446183
if (nested_vmx_check_controls(vcpu, vmcs12) ||
61456184
nested_vmx_check_host_state(vcpu, vmcs12) ||
61466185
nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))

arch/x86/kvm/vmx/vmx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ struct nested_vmx {
169169
u16 posted_intr_nv;
170170

171171
struct hrtimer preemption_timer;
172+
u64 preemption_timer_deadline;
173+
bool has_preemption_timer_deadline;
172174
bool preemption_timer_expired;
173175

174176
/* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */

0 commit comments

Comments
 (0)