Skip to content

Commit 51ca274

Browse files
committed
KVM: nVMX: Switch to vmcs01 to update TPR threshold on-demand if L2 is active
If KVM updates L1's TPR Threshold while L2 is active, temporarily load vmcs01 and immediately update TPR_THRESHOLD instead of deferring the update until the next nested VM-Exit. Deferring the TPR Threshold update is relatively straightforward, but for several APICv related updates, deferring updates creates ordering and state consistency problems, e.g. KVM at-large thinks APICv is enabled, but vmcs01 is still running with stale (and effectively unknown) state. Reviewed-by: Chao Gao <chao.gao@intel.com> Link: https://patch.msgid.link/20260109034532.1012993-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 3e013d0 commit 51ca274

3 files changed

Lines changed: 3 additions & 11 deletions

File tree

arch/x86/kvm/vmx/nested.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,6 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0
24022402
exec_control &= ~CPU_BASED_TPR_SHADOW;
24032403
exec_control |= vmcs12->cpu_based_vm_exec_control;
24042404

2405-
vmx->nested.l1_tpr_threshold = -1;
24062405
if (exec_control & CPU_BASED_TPR_SHADOW)
24072406
vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
24082407
#ifdef CONFIG_X86_64
@@ -5123,9 +5122,6 @@ void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
51235122
if (kvm_caps.has_tsc_control)
51245123
vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
51255124

5126-
if (vmx->nested.l1_tpr_threshold != -1)
5127-
vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
5128-
51295125
if (vmx->nested.change_vmcs01_virtual_apic_mode) {
51305126
vmx->nested.change_vmcs01_virtual_apic_mode = false;
51315127
vmx_set_virtual_apic_mode(vcpu);

arch/x86/kvm/vmx/vmx.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6836,11 +6836,10 @@ void vmx_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
68366836
nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
68376837
return;
68386838

6839+
guard(vmx_vmcs01)(vcpu);
6840+
68396841
tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
6840-
if (is_guest_mode(vcpu))
6841-
to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6842-
else
6843-
vmcs_write32(TPR_THRESHOLD, tpr_threshold);
6842+
vmcs_write32(TPR_THRESHOLD, tpr_threshold);
68446843
}
68456844

68466845
void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)

arch/x86/kvm/vmx/vmx.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ struct nested_vmx {
184184
u64 pre_vmenter_ssp;
185185
u64 pre_vmenter_ssp_tbl;
186186

187-
/* to migrate it to L1 if L2 writes to L1's CR8 directly */
188-
int l1_tpr_threshold;
189-
190187
u16 vpid02;
191188
u16 last_vpid;
192189

0 commit comments

Comments
 (0)