From: Sean Christopherson <seanjc@google.com>
To: bugzilla-daemon@kernel.org
Cc: kvm@vger.kernel.org
Subject: Re: [Bug 216212] New: KVM does not handle nested guest enable PAE paging correctly when CR3 is not mapped in EPT
Date: Fri, 8 Jul 2022 00:07:25 +0000 [thread overview]
Message-ID: <Ysd1PfDyF1crnmyA@google.com> (raw)
In-Reply-To: <bug-216212-28872@https.bugzilla.kernel.org/>
On Thu, Jul 07, 2022, bugzilla-daemon@kernel.org wrote:
> Likely stack trace and cause of this bug (Linux source code version is 5.18.9):
>
> Stack trace:
>
> handle_cr
> kvm_set_cr0
> load_pdptrs
> kvm_translate_gpa
Yeah, load_pdptrs() needs to call kvm_inject_emulated_page_fault() to inject a
TDP fault if translating the L2 GPA to an L1 GPA fails. That part is easy to fix,
but communicating up the stack that the instruction has already faulted is going
to be painful due to the use of kvm_complete_insn_gp(). Ugh, and the emulator gets
involved too.
Not that it makes things worse than they already are, but I'm pretty sure MOV CR3
(via the emulator) and MOV CR4 are also affected.
I suspect the least awful solution will be to use proper error codes instead of 0/1
so that kvm_complete_insn_gp() and friends can differentiate between "success",
"injected #GP", and "already exploded", but it's still going to require a lot of
churn.
A more drastic, but maybe less painful (though as I type this out, it's becoming
ridiculously painful) alternative would be to not intercept CR0/CR4 paging bits
when running L2 and TDP is enabled, which would in theory allow KVM to drop
the call to kvm_translate_gpa(). load_pdptrs() would still be reachable via the
emulator, but I think iff the guest is playing TLB, so KVM could probably just
resume the guest in that case.
The primary reason KVM intercepts CR0/CR4 paging bits even when using TDP is so
that KVM doesn't need to refresh state to do software gva->gpa walks, e.g. to
correctly emulate memory accesses and reserved PTE bits. The argument for
intercepting is that changing paging modes is a rare guest operation, whereas
emulating some form of memory access is relatively common. And it's also simpler
in the sense that KVM can use common code for TDP and !TDP (shadow paging heavily
depends on caching paging state).
But emulating on behalf of L2 is quite rare, and having to deal with this bug
counters the "it's simpler" argument to some extent. I _think_ ensuring the nested
MMU is properly initialized could be solved by adding a nested_gva_to_gpa() wrapper
instead of directly wiring mmu->gva_to_gpa() to the correct helper.
The messier part would be handling intercepts. VMX would have to adjust
vmcs02.CRx_READ_SHADOW and resume the guest to deal with incidental interception,
e.g. if the guest toggles both CR0.CD and CR0.PG. SVM is all or nothing for
intercepts, but PAE under NPT isn't required to load PDTRs at MOV CR, so we could
just drop that entire path for SVM+NPT. But that would rely on KVM correctly
handling L1 NPT faults during PDPTE accesses on behalf of L2, which of course KVM
doesn't get right.
So yeah, maybe KVM can avoid some of the PAE pain in the long term if KVM stops
intercepting CR0/CR4 paging bits, but it's probably a bad idea for an immediate
fix.
next prev parent reply other threads:[~2022-07-08 0:07 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-07 2:38 [Bug 216212] New: KVM does not handle nested guest enable PAE paging correctly when CR3 is not mapped in EPT bugzilla-daemon
2022-07-07 15:24 ` [Bug 216212] " bugzilla-daemon
2022-07-08 0:07 ` Sean Christopherson [this message]
2022-07-08 0:07 ` bugzilla-daemon
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=Ysd1PfDyF1crnmyA@google.com \
--to=seanjc@google.com \
--cc=bugzilla-daemon@kernel.org \
--cc=kvm@vger.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