kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <sean.j.christopherson@intel.com>
To: Jim Mattson <jmattson@google.com>
Cc: kvm list <kvm@vger.kernel.org>, Oliver Upton <oupton@google.com>,
	Peter Shier <pshier@google.com>
Subject: Re: [PATCH 2/2] kvm: nVMX: Single-step traps trump expired VMX-preemption timer
Date: Tue, 14 Apr 2020 17:12:12 -0700	[thread overview]
Message-ID: <20200415001212.GA12547@linux.intel.com> (raw)
In-Reply-To: <CALMp9eT23AUTU3m_oADKw3O_NMpuX3crx7eqSB8Rbgh3k0s_Jw@mail.gmail.com>

On Tue, Apr 14, 2020 at 09:47:53AM -0700, Jim Mattson wrote:
> On Mon, Apr 13, 2020 at 8:17 PM Sean Christopherson
> <sean.j.christopherson@intel.com> wrote:
> >
> > On Mon, Apr 13, 2020 at 05:09:46PM -0700, Jim Mattson wrote:
> > > Previously, if the hrtimer for the nested VMX-preemption timer fired
> > > while L0 was emulating an L2 instruction with RFLAGS.TF set, the
> > > synthesized single-step trap would be unceremoniously dropped when
> > > synthesizing the "VMX-preemption timer expired" VM-exit from L2 to L1.
> > >
> > > To fix this, don't synthesize a "VMX-preemption timer expired" VM-exit
> > > from L2 to L1 when there is a pending debug trap, such as a
> > > single-step trap.
> > >
> > > Fixes: f4124500c2c13 ("KVM: nVMX: Fully emulate preemption timer")
> > > Signed-off-by: Jim Mattson <jmattson@google.com>
> > > Reviewed-by: Oliver Upton <oupton@google.com>
> > > Reviewed-by: Peter Shier <pshier@google.com>
> > > ---
> > >  arch/x86/kvm/vmx/nested.c | 4 +++-
> > >  1 file changed, 3 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> > > index cbc9ea2de28f..6ab974debd44 100644
> > > --- a/arch/x86/kvm/vmx/nested.c
> > > +++ b/arch/x86/kvm/vmx/nested.c
> > > @@ -3690,7 +3690,9 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
> > >           vmx->nested.preemption_timer_expired) {
> > >               if (block_nested_events)
> > >                       return -EBUSY;
> > > -             nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
> > > +             if (!vmx_pending_dbg_trap(vcpu))
> >
> > IMO this one warrants a comment.  It's not immediately obvious that this
> > only applies to #DBs that are being injected into L2, and that returning
> > -EBUSY will do the wrong thing.
> 
> Regarding -EBUSY, I'm in complete agreement. However, I'm not sure
> what the potential confusion is regarding the event. Are you
> suggesting that one might think that we have a #DB to deliver to L1
> while we're in guest mode? IIRC, that can happen under SVM, but I
> don't believe it can happen under VMX.

The potential confusion is that vcpu->arch.exception.pending was already
checked, twice.  It makes one wonder why it needs to be checked a third
time.  And actually, I think that's probably a good indicator that singling
out single-step #DB isn't the correct fix, it just happens to be the only
case that's been encountered thus far, e.g. a #PF when fetching the instr
for emulation should also get priority over the preemption timer.  On real
hardware, expiration of the preemption timer while vectoring a #PF wouldn't
wouldn't get recognized until the next instruction boundary, i.e. at the
start of the first instruction of the #PF handler.  Dropping the #PF isn't
a problem in most cases, because unlike the single-step #DB, it will be
re-encountered when L1 resumes L2.  But, dropping the #PF is still wrong.

In general, interception of an event doesn't change the priority of events,
e.g. INTR shouldn't get priority over NMI just because if L1 wants to
intercept INTR but not NMI.

TL;DR: I think the fix should instead be:

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index c868c64770e0..042d7a9037be 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -3724,9 +3724,10 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
        /*
         * Process any exceptions that are not debug traps before MTF.
         */
-       if (vcpu->arch.exception.pending &&
-           !vmx_pending_dbg_trap(vcpu) &&
-           nested_vmx_check_exception(vcpu, &exit_qual)) {
+       if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu))
+               if (!nested_vmx_check_exception(vcpu, &exit_qual))
+                       return 0;
+
                if (block_nested_events)
                        return -EBUSY;
                nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
@@ -3741,8 +3742,10 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
                return 0;
        }

-       if (vcpu->arch.exception.pending &&
-           nested_vmx_check_exception(vcpu, &exit_qual)) {
+       if (vcpu->arch.exception.pending) {
+               if (!nested_vmx_check_exception(vcpu, &exit_qual))
+                       return 0;
+
                if (block_nested_events)
                        return -EBUSY;
                nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
@@ -3757,7 +3760,10 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
                return 0;
        }

-       if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
+       if (vcpu->arch.nmi_pending) {
+               if (!nested_exit_on_nmi(vcpu))
+                       return 0;
+
                if (block_nested_events)
                        return -EBUSY;
                nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
@@ -3772,7 +3778,10 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
                return 0;
        }

-       if (kvm_cpu_has_interrupt(vcpu) && nested_exit_on_intr(vcpu)) {
+       if (kvm_cpu_has_interrupt(vcpu) {
+               if (!nested_exit_on_intr(vcpu))
+                       return 0;
+
                if (block_nested_events)
                        return -EBUSY;
                nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);


  reply	other threads:[~2020-04-15  0:12 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-14  0:09 [PATCH 1/2] kvm: nVMX: Pending debug exceptions trump expired VMX-preemption timer Jim Mattson
2020-04-14  0:09 ` [PATCH 2/2] kvm: nVMX: Single-step traps " Jim Mattson
2020-04-14  3:17   ` Sean Christopherson
2020-04-14 16:47     ` Jim Mattson
2020-04-15  0:12       ` Sean Christopherson [this message]
2020-04-15  0:20         ` Sean Christopherson
2020-04-15  0:22           ` Sean Christopherson
2020-04-15 23:33         ` Jim Mattson
2020-04-18  4:21           ` Sean Christopherson
2020-04-20 17:18             ` Jim Mattson
2020-04-21  4:41               ` Sean Christopherson
2020-04-21 18:28                 ` Jim Mattson
2020-04-22  0:16                   ` Sean Christopherson
2020-04-22  8:30   ` Paolo Bonzini
2020-04-22 15:48     ` Sean Christopherson
2020-04-22 16:28     ` Jim Mattson
2020-04-22 16:42       ` Sean Christopherson
2020-04-22 21:06 ` [PATCH 1/2] kvm: nVMX: Pending debug exceptions " Sean Christopherson
2020-04-22 21:23   ` Sean Christopherson
2020-04-22 21:27   ` Jim Mattson
2020-04-22 22:06     ` Sean Christopherson

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=20200415001212.GA12547@linux.intel.com \
    --to=sean.j.christopherson@intel.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=oupton@google.com \
    --cc=pshier@google.com \
    /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;
as well as URLs for NNTP newsgroup(s).