Skip to content

Commit 0e0a53c

Browse files
committed
kvm: x86: optimize dr6 restore
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>
1 parent f21dd49 commit 0e0a53c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

arch/x86/kvm/x86.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3186,11 +3186,16 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
31863186
kvm_x86_ops->vcpu_put(vcpu);
31873187
vcpu->arch.last_host_tsc = rdtsc();
31883188
/*
3189-
* If userspace has set any breakpoints or watchpoints, dr6 is restored
3190-
* on every vmexit, but if not, we might have a stale dr6 from the
3191-
* guest. do_debug expects dr6 to be cleared after it runs, do the same.
3189+
* Here dr6 is either zero or, if the guest has run and userspace
3190+
* has not set any breakpoints or watchpoints, it can be set to
3191+
* the guest dr6 (stored in vcpu->arch.dr6). do_debug expects dr6
3192+
* to be cleared after it runs, so clear the host register. However,
3193+
* MOV to DR can be expensive when running nested, omit it if
3194+
* vcpu->arch.dr6 is already zero: in that case, the host dr6 cannot
3195+
* currently be nonzero.
31923196
*/
3193-
set_debugreg(0, 6);
3197+
if (vcpu->arch.dr6)
3198+
set_debugreg(0, 6);
31943199
}
31953200

31963201
static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,

0 commit comments

Comments
 (0)