public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Ivan Orlov <iorlov@amazon.com>
Cc: Ivan Orlov <ivan.orlov0322@gmail.com>,
	bp@alien8.de, dave.hansen@linux.intel.com,  mingo@redhat.com,
	pbonzini@redhat.com, shuah@kernel.org, tglx@linutronix.de,
	 hpa@zytor.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org,  linux-kselftest@vger.kernel.org,
	x86@kernel.org, pdurrant@amazon.co.uk,  dwmw@amazon.co.uk
Subject: Re: [PATCH v2 3/6] KVM: VMX: Handle vectoring error in check_emulate_instruction
Date: Fri, 13 Dec 2024 12:09:21 -0800	[thread overview]
Message-ID: <Z1yHAAomsCdn5B8z@google.com> (raw)
In-Reply-To: <20241213173816.GA7768@dev-dsk-iorlov-1b-d2eae488.eu-west-1.amazon.com>

On Fri, Dec 13, 2024, Ivan Orlov wrote:
> On Thu, Dec 12, 2024 at 11:42:37AM -0800, Sean Christopherson wrote:
> > Unprotect and re-execute is fine, what I'm worried about is *successfully*
> > emulating the instruction.  E.g.
> > 
> >   1. CPU executes instruction X and hits a #GP.
> >   2. While vectoring the #GP, a shadow #PF is taken.
> >   3. On VM-Exit, KVM re-injects the #GP (see __vmx_complete_interrupts()).
> >   4. KVM emulates because of the write-protected page.
> >   5. KVM "successfully" emulates and also detects the #GP
> >   6. KVM synthesizes a #GP, and because the vCPU already has injected #GP,
> >      incorrectly escalates to a #DF.
> > 
> > The above is a bit contrived, but I think it could happen if the guest reused a
> > page that _was_ a page table, for a vCPU's kernel stack.
> > 
> 
> Does it work like that only for contributory exceptions / page faults?

The #DF case, yes.

> In case if it's not #GP but (for instance) #UD, (as far as I understand)
> KVM will queue only one of them without causing #DF so it's gonna be
> valid?

No, it can still be invalid.  E.g. initialize hit a #BP, replace it with a #UD,
but there may be guest-visibile side effects from the original #BP.

> > > However, I'm not sure what happens if vectoring is caused by external
> > > interrupt: if we unprotect the page and re-execute the instruction,
> > > will IRQ be delivered nonetheless, or it will be lost as irq is
> > > already in ISR? Do we need to re-inject it in such a case?
> > 
> > In all cases, the event that was being vectored is re-injected.  Restarting from
> > scratch would be a bug.  E.g. if the cause of initial exception was "fixed", say
> > because the initial exception was #BP, and the guest finished patching out the INT3,
> > then restarting would execute the _new_ instruction, and the INT3 would be lost.
> > 
> 
> Cool, that is what I was concerned about, glad that it is already
> implemented :)
> 
> > 
> > As far as unprotect+retry being viable, I think we're on the same page.  What I'm
> > getting at is that I think KVM should never allow emulating on #PF when the #PF
> > occurred while vectoring.  E.g. this:
> > 
> >   static inline bool kvm_can_emulate_event_vectoring(int emul_type)
> >   {
> >         return !(emul_type & EMULTYPE_PF);
> >   }
> > 
> 
> Yeah, I agree. I'll post a V3 with suggested fixes (after running all of the
> selftests to be sure that it doesn't break anything).
> 
> > and then I believe this?  Where this diff can be a separate prep patch (though I'm
> > pretty sure it's technically pointless without the vectoring angle, because shadow
> > #PF can't coincide with any of the failure paths for kvm_check_emulate_insn()).
> > 
> 
> Looks good. If you don't mind, I could add this patch to the series with `Suggested-by`
> tag since it's neccessary to allow unprotect+retry in case of shadow #PF during
> vectoring.

Ya, go for it.

  reply	other threads:[~2024-12-13 20:09 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-11 10:27 [PATCH v2 0/6] Enhance event delivery error handling Ivan Orlov
2024-11-11 10:27 ` [PATCH v2 1/6] KVM: x86: Add function for vectoring error generation Ivan Orlov
2024-12-11 18:02   ` Sean Christopherson
2024-11-11 10:27 ` [PATCH v2 2/6] KVM: x86: Add emulation status for vectoring during MMIO Ivan Orlov
2024-11-11 10:27 ` [PATCH v2 3/6] KVM: VMX: Handle vectoring error in check_emulate_instruction Ivan Orlov
2024-12-11 18:15   ` Sean Christopherson
2024-12-11 22:05     ` Ivan Orlov
2024-12-11 23:12     ` Ivan Orlov
2024-12-12  1:01       ` Sean Christopherson
2024-12-12 16:41         ` Ivan Orlov
2024-12-12 19:42           ` Sean Christopherson
2024-12-13 17:38             ` Ivan Orlov
2024-12-13 20:09               ` Sean Christopherson [this message]
2024-11-11 10:27 ` [PATCH v2 4/6] KVM: SVM: Handle MMIO during vectroing error Ivan Orlov
2024-12-11 18:16   ` Sean Christopherson
2024-11-11 10:27 ` [PATCH v2 5/6] selftests: KVM: extract lidt into helper function Ivan Orlov
2024-11-11 10:27 ` [PATCH v2 6/6] selftests: KVM: Add test case for MMIO during vectoring Ivan Orlov
2024-12-11 18:19   ` Sean Christopherson
2024-12-12 17:11     ` Ivan Orlov
2024-12-11 18:20 ` [PATCH v2 0/6] Enhance event delivery error handling Sean Christopherson
2024-12-11 21:45   ` Ivan Orlov

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=Z1yHAAomsCdn5B8z@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=dwmw@amazon.co.uk \
    --cc=hpa@zytor.com \
    --cc=iorlov@amazon.com \
    --cc=ivan.orlov0322@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=pdurrant@amazon.co.uk \
    --cc=shuah@kernel.org \
    --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