From: Sean Christopherson <seanjc@google.com>
To: Chao Gao <chao.gao@intel.com>
Cc: Maxim Levitsky <mlevitsk@redhat.com>,
kvm@vger.kernel.org, Thomas Gleixner <tglx@linutronix.de>,
Borislav Petkov <bp@alien8.de>,
x86@kernel.org, Ingo Molnar <mingo@redhat.com>,
linux-kernel@vger.kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Dave Hansen <dave.hansen@linux.intel.com>
Subject: Re: [PATCH v4 2/4] KVM: x86: Drop kvm_x86_ops.set_dr6() in favor of a new KVM_RUN flag
Date: Fri, 16 May 2025 06:07:15 -0700 [thread overview]
Message-ID: <aCc4gyb_hrQZs9Ya@google.com> (raw)
In-Reply-To: <aCbf6JikteXzb0gB@intel.com>
On Fri, May 16, 2025, Chao Gao wrote:
> >diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> >index c8b8a9947057..026b28051fff 100644
> >--- a/arch/x86/kvm/svm/svm.c
> >+++ b/arch/x86/kvm/svm/svm.c
> >@@ -4308,10 +4308,13 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
> > svm_hv_update_vp_id(svm->vmcb, vcpu);
> >
> > /*
> >- * Run with all-zero DR6 unless needed, so that we can get the exact cause
> >- * of a #DB.
> >+ * Run with all-zero DR6 unless the guest can write DR6 freely, so that
> >+ * KVM can get the exact cause of a #DB. Note, loading guest DR6 from
> >+ * KVM's snapshot is only necessary when DR accesses won't exit.
> > */
> >- if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)))
> >+ if (unlikely(run_flags & KVM_RUN_LOAD_GUEST_DR6))
> >+ svm_set_dr6(vcpu, vcpu->arch.dr6);
> >+ else if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)))
> > svm_set_dr6(vcpu, DR6_ACTIVE_LOW);
>
> ...
>
> > void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
> > {
> > vmcs_writel(GUEST_DR7, val);
> >@@ -7371,6 +7365,9 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
> > vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
> > vcpu->arch.regs_dirty = 0;
> >
> >+ if (run_flags & KVM_RUN_LOAD_GUEST_DR6)
> >+ set_debugreg(vcpu->arch.dr6, 6);
> >+
> > /*
> > * Refresh vmcs.HOST_CR3 if necessary. This must be done immediately
> > * prior to VM-Enter, as the kernel may load a new ASID (PCID) any time
> >diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> >index 25de78cdab42..684b8047e0f2 100644
> >--- a/arch/x86/kvm/x86.c
> >+++ b/arch/x86/kvm/x86.c
> >@@ -11019,7 +11019,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
> > set_debugreg(vcpu->arch.eff_db[3], 3);
> > /* When KVM_DEBUGREG_WONT_EXIT, dr6 is accessible in guest. */
> > if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
> >- kvm_x86_call(set_dr6)(vcpu, vcpu->arch.dr6);
> >+ run_flags |= KVM_RUN_LOAD_GUEST_DR6;
>
> The new KVM_RUN flag isn't necessary. Vendor code can directly check
> switch_db_regs to decide whether to load the guest dr6 into hardware.
So thought Paolo, too.
> In svm_vcpu_run(), referencing both run_flags and switch_db_regs is confusing.
> The following logic is much clearer:
>
> if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)))
> svm_set_dr6(vcpu, DR6_ACTIVE_LOW);
> else
> svm_set_dr6(vcpu, vcpu->arch.dr6);
And wrong :-) vcpu->arch.dr6 is stale on fastpasth reentry, see commit c2fee09fc167
("KVM: x86: Load DR6 with guest value only before entering .vcpu_run() loop").
next prev parent reply other threads:[~2025-05-16 13:07 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-15 0:53 [PATCH v4 0/4] KVM: x86: allow DEBUGCTL.DEBUGCTLMSR_FREEZE_IN_SMM passthrough Maxim Levitsky
2025-05-15 0:53 ` [PATCH v4 1/4] KVM: x86: Convert vcpu_run()'s immediate exit param into a generic bitmap Maxim Levitsky
2025-05-15 0:53 ` [PATCH v4 2/4] KVM: x86: Drop kvm_x86_ops.set_dr6() in favor of a new KVM_RUN flag Maxim Levitsky
2025-05-16 6:49 ` Chao Gao
2025-05-16 13:07 ` Sean Christopherson [this message]
2025-05-15 0:53 ` [PATCH v4 3/4] x86: nVMX: check vmcs12->guest_ia32_debugctl value given by L2 Maxim Levitsky
2025-05-16 3:31 ` Chao Gao
2025-05-16 14:50 ` mlevitsk
2025-05-20 21:48 ` mlevitsk
2025-05-21 0:32 ` Chao Gao
2025-05-21 16:50 ` mlevitsk
2025-05-20 22:24 ` Sean Christopherson
2025-05-15 0:53 ` [PATCH v4 4/4] x86: KVM: VMX: preserve DEBUGCTLMSR_FREEZE_IN_SMM Maxim Levitsky
2025-05-16 3:39 ` Chao Gao
2025-05-16 14:49 ` mlevitsk
2025-05-20 22:57 ` Sean Christopherson
2025-05-21 20:43 ` mlevitsk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aCc4gyb_hrQZs9Ya@google.com \
--to=seanjc@google.com \
--cc=bp@alien8.de \
--cc=chao.gao@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=mlevitsk@redhat.com \
--cc=pbonzini@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox