Skip to content

Commit 653f52c

Browse files
Rik van Rielbonzini
authored andcommitted
kvm,x86: load guest FPU context more eagerly
Currently KVM will clear the FPU bits in CR0.TS in the VMCS, and trap to re-load them every time the guest accesses the FPU after a switch back into the guest from the host. This patch copies the x86 task switch semantics for FPU loading, with the FPU loaded eagerly after first use if the system uses eager fpu mode, or if the guest uses the FPU frequently. In the latter case, after loading the FPU for 255 times, the fpu_counter will roll over, and we will revert to loading the FPU on demand, until it has been established that the guest is still actively using the FPU. This mirrors the x86 task switch policy, which seems to work. Signed-off-by: Rik van Riel <riel@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent d1ebdbf commit 653f52c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

arch/x86/kvm/x86.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7086,14 +7086,25 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
70867086
{
70877087
kvm_put_guest_xcr0(vcpu);
70887088

7089-
if (!vcpu->guest_fpu_loaded)
7089+
if (!vcpu->guest_fpu_loaded) {
7090+
vcpu->fpu_counter = 0;
70907091
return;
7092+
}
70917093

70927094
vcpu->guest_fpu_loaded = 0;
70937095
fpu_save_init(&vcpu->arch.guest_fpu);
70947096
__kernel_fpu_end();
70957097
++vcpu->stat.fpu_reload;
7096-
kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
7098+
/*
7099+
* If using eager FPU mode, or if the guest is a frequent user
7100+
* of the FPU, just leave the FPU active for next time.
7101+
* Every 255 times fpu_counter rolls over to 0; a guest that uses
7102+
* the FPU in bursts will revert to loading it on demand.
7103+
*/
7104+
if (!use_eager_fpu()) {
7105+
if (++vcpu->fpu_counter < 5)
7106+
kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
7107+
}
70977108
trace_kvm_fpu(0);
70987109
}
70997110

include/linux/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ struct kvm_vcpu {
230230

231231
int fpu_active;
232232
int guest_fpu_loaded, guest_xcr0_loaded;
233+
unsigned char fpu_counter;
233234
wait_queue_head_t wq;
234235
struct pid *pid;
235236
int sigset_active;

0 commit comments

Comments
 (0)