]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/commitdiff
kvm: x86: optimize dr6 restore
authorPaolo Bonzini <pbonzini@redhat.com>
Wed, 22 Aug 2018 17:59:33 +0000 (19:59 +0200)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 16 Oct 2018 22:30:02 +0000 (00:30 +0200)
The quote from the comment almost says it all: we are currently zeroing
the guest dr6 in kvm_arch_vcpu_put, because do_debug expects it.  However,
the host %dr6 is either:

- zero because the guest hasn't run after kvm_arch_vcpu_load

- written from vcpu->arch.dr6 by vcpu_enter_guest

- written by the guest and copied to vcpu->arch.dr6 by ->sync_dirty_debug_regs().

Therefore, we can skip the write if vcpu->arch.dr6 is already zero.  We
may do extra useless writes if vcpu->arch.dr6 is nonzero but the guest
hasn't run; however that is less important for performance.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/x86.c

index 20a667da0a31ac25001a612e314bb969477f29bb..92159db9cf7194d72927bacd98aff262c0901451 100644 (file)
@@ -3186,11 +3186,16 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
        kvm_x86_ops->vcpu_put(vcpu);
        vcpu->arch.last_host_tsc = rdtsc();
        /*
-        * If userspace has set any breakpoints or watchpoints, dr6 is restored
-        * on every vmexit, but if not, we might have a stale dr6 from the
-        * guest. do_debug expects dr6 to be cleared after it runs, do the same.
+        * Here dr6 is either zero or, if the guest has run and userspace
+        * has not set any breakpoints or watchpoints, it can be set to
+        * the guest dr6 (stored in vcpu->arch.dr6). do_debug expects dr6
+        * to be cleared after it runs, so clear the host register.  However,
+        * MOV to DR can be expensive when running nested, omit it if
+        * vcpu->arch.dr6 is already zero: in that case, the host dr6 cannot
+        * currently be nonzero.
         */
-       set_debugreg(0, 6);
+       if (vcpu->arch.dr6)
+               set_debugreg(0, 6);
 }
 
 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,