Skip to content

Commit b5274b1

Browse files
ktian1bonzini
authored andcommitted
kvm: x86: Disable interception for IA32_XFD on demand
Always intercepting IA32_XFD causes non-negligible overhead when this register is updated frequently in the guest. Disable r/w emulation after intercepting the first WRMSR(IA32_XFD) with a non-zero value. Disable WRMSR emulation implies that IA32_XFD becomes out-of-sync with the software states in fpstate and the per-cpu xfd cache. This leads to two additional changes accordingly: - Call fpu_sync_guest_vmexit_xfd_state() after vm-exit to bring software states back in-sync with the MSR, before handle_exit_irqoff() is called. - Always trap #NM once write interception is disabled for IA32_XFD. The #NM exception is rare if the guest doesn't use dynamic features. Otherwise, there is at most one exception per guest task given a dynamic feature. p.s. We have confirmed that SDM is being revised to say that when setting IA32_XFD[18] the AMX register state is not guaranteed to be preserved. This clarification avoids adding mess for a creative guest which sets IA32_XFD[18]=1 before saving active AMX state to its own storage. Signed-off-by: Kevin Tian <kevin.tian@intel.com> Signed-off-by: Jing Liu <jing2.liu@intel.com> Signed-off-by: Yang Zhong <yang.zhong@intel.com> Message-Id: <20220105123532.12586-22-yang.zhong@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 5429cea commit b5274b1

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ struct kvm_vcpu_arch {
647647
u64 smi_count;
648648
bool tpr_access_reporting;
649649
bool xsaves_enabled;
650+
bool xfd_no_write_intercept;
650651
u64 ia32_xss;
651652
u64 microcode_version;
652653
u64 arch_capabilities;

arch/x86/kvm/vmx/vmx.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ static u32 vmx_possible_passthrough_msrs[MAX_POSSIBLE_PASSTHROUGH_MSRS] = {
162162
MSR_FS_BASE,
163163
MSR_GS_BASE,
164164
MSR_KERNEL_GS_BASE,
165+
MSR_IA32_XFD,
165166
MSR_IA32_XFD_ERR,
166167
#endif
167168
MSR_IA32_SYSENTER_CS,
@@ -764,10 +765,11 @@ void vmx_update_exception_bitmap(struct kvm_vcpu *vcpu)
764765
}
765766

766767
/*
767-
* Trap #NM if guest xfd contains a non-zero value so guest XFD_ERR
768-
* can be saved timely.
768+
* Disabling xfd interception indicates that dynamic xfeatures
769+
* might be used in the guest. Always trap #NM in this case
770+
* to save guest xfd_err timely.
769771
*/
770-
if (vcpu->arch.guest_fpu.fpstate->xfd)
772+
if (vcpu->arch.xfd_no_write_intercept)
771773
eb |= (1u << NM_VECTOR);
772774

773775
vmcs_write32(EXCEPTION_BITMAP, eb);
@@ -1978,9 +1980,21 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
19781980
break;
19791981
case MSR_IA32_XFD:
19801982
ret = kvm_set_msr_common(vcpu, msr_info);
1981-
/* Update #NM interception according to guest xfd */
1982-
if (!ret)
1983+
/*
1984+
* Always intercepting WRMSR could incur non-negligible
1985+
* overhead given xfd might be changed frequently in
1986+
* guest context switch. Disable write interception
1987+
* upon the first write with a non-zero value (indicating
1988+
* potential usage on dynamic xfeatures). Also update
1989+
* exception bitmap to trap #NM for proper virtualization
1990+
* of guest xfd_err.
1991+
*/
1992+
if (!ret && data) {
1993+
vmx_disable_intercept_for_msr(vcpu, MSR_IA32_XFD,
1994+
MSR_TYPE_RW);
1995+
vcpu->arch.xfd_no_write_intercept = true;
19831996
vmx_update_exception_bitmap(vcpu);
1997+
}
19841998
break;
19851999
#endif
19862000
case MSR_IA32_SYSENTER_CS:

arch/x86/kvm/vmx/vmx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ struct vcpu_vmx {
349349
struct lbr_desc lbr_desc;
350350

351351
/* Save desired MSR intercept (read: pass-through) state */
352-
#define MAX_POSSIBLE_PASSTHROUGH_MSRS 14
352+
#define MAX_POSSIBLE_PASSTHROUGH_MSRS 15
353353
struct {
354354
DECLARE_BITMAP(read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
355355
DECLARE_BITMAP(write, MAX_POSSIBLE_PASSTHROUGH_MSRS);

arch/x86/kvm/x86.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10083,6 +10083,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
1008310083
vcpu->mode = OUTSIDE_GUEST_MODE;
1008410084
smp_wmb();
1008510085

10086+
/*
10087+
* Sync xfd before calling handle_exit_irqoff() which may
10088+
* rely on the fact that guest_fpu::xfd is up-to-date (e.g.
10089+
* in #NM irqoff handler).
10090+
*/
10091+
if (vcpu->arch.xfd_no_write_intercept)
10092+
fpu_sync_guest_vmexit_xfd_state();
10093+
1008610094
static_call(kvm_x86_handle_exit_irqoff)(vcpu);
1008710095

1008810096
if (vcpu->arch.guest_fpu.xfd_err)

0 commit comments

Comments
 (0)