Skip to content

Commit 55780d8

Browse files
yosrym93sean-jc
authored andcommitted
KVM: SVM: Stop toggling virtual VMSAVE/VMLOAD on intercept recalc
Virtual VMSAVE/VMLOAD enablement (i.e. VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK) is set/cleared by svm_recalc_instruction_intercepts() when the intercepts are cleared/set. This is unnecessary because the bit is meaningless when intercepts are set and KVM emulates the instructions. Initialize the bit in vmcb01 base on vls, and keep it unchanged. This is similar-ish to how vGIF is handled. It is enabled in init_vmcb() if vgif=1 and remains unchanged when the STGI intercept is enabled (e.g. for NMI windows). This fixes a bug in svm_recalc_instruction_intercepts(). The intercepts for VMSAVE/VMLOAD are always toggled in vmcb01, but VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK is toggled in the current VMCB, which could be vmcb02 instead of vmcb01 if L2 is active. Virtual VMSAVE/VMLOAD enablement in vmcb02 is separately controlled by nested_vmcb02_prepare_control() based on the vCPU features and VMCB12, and if intercepts are needed they are set by recalc_intercepts(). The bug is benign though. Not toggling the bit for vmcb01 is harmless because it's useless anyway. For vmcb02: - The bit could be incorrectly cleared when intercepts are set in vmcb01. This is harmless because VMSAVE/VMLOAD will be emulated by KVM anyway. - The bit could be incorrectly set when the intercepts are cleared in vmcb01. However, if the bit was originally clear in vmcb02, then recalc_intercepts() will enable in the intercepts in vmcb02 anyway and VMSAVE/VMLOAD will be emulated by KVM. Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev> Link: https://patch.msgid.link/20260110004821.3411245-3-yosry.ahmed@linux.dev Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 127ccae commit 55780d8

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

arch/x86/kvm/svm/svm.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,10 +995,14 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu)
995995
svm_set_intercept(svm, INTERCEPT_RDTSCP);
996996
}
997997

998+
/*
999+
* No need to toggle VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK here, it is
1000+
* always set if vls is enabled. If the intercepts are set, the bit is
1001+
* meaningless anyway.
1002+
*/
9981003
if (guest_cpuid_is_intel_compatible(vcpu)) {
9991004
svm_set_intercept(svm, INTERCEPT_VMLOAD);
10001005
svm_set_intercept(svm, INTERCEPT_VMSAVE);
1001-
svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
10021006
} else {
10031007
/*
10041008
* If hardware supports Virtual VMLOAD VMSAVE then enable it
@@ -1007,7 +1011,6 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu)
10071011
if (vls) {
10081012
svm_clr_intercept(svm, INTERCEPT_VMLOAD);
10091013
svm_clr_intercept(svm, INTERCEPT_VMSAVE);
1010-
svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
10111014
}
10121015
}
10131016
}
@@ -1155,6 +1158,9 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)
11551158
svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
11561159
}
11571160

1161+
if (vls)
1162+
svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1163+
11581164
if (vcpu->kvm->arch.bus_lock_detection_enabled)
11591165
svm_set_intercept(svm, INTERCEPT_BUSLOCK);
11601166

0 commit comments

Comments
 (0)