Re: [RFCv2 13/13] KVM: unmap guest memory using poisoned pages

From: Andy Lutomirski
Date: Fri Jun 04 2021 - 13:16:52 EST


On 5/21/21 5:31 AM, Kirill A. Shutemov wrote:
> Hi Sean,
>
> The core patch of the approach we've discussed before is below. It
> introduce a new page type with the required semantics.
>
> The full patchset can be found here:
>
> git://git.kernel.org/pub/scm/linux/kernel/git/kas/linux.git kvm-unmapped-guest-only
>
> but only the patch below is relevant for TDX. QEMU patch is attached.
>
> CONFIG_HAVE_KVM_PROTECTED_MEMORY has to be changed to what is appropriate
> for TDX and FOLL_GUEST has to be used in hva_to_pfn_slow() when running
> TDX guest.
>
> When page get inserted into private sept we must make sure it is
> PageGuest() or SIGBUS otherwise. Inserting PageGuest() into shared is
> fine, but the page will not be accessible from userspace.

I may have missed a detail, and I think Sean almost said this, but:

I don't think that a single bit like this is sufficient. A KVM host can
contain more than one TDX guest, and, to reliably avoid machine checks,
I think we need to make sure that secure pages are only ever mapped into
the guest to which they belong, as well as to the right GPA. If a KVM
user host process creates a PageGuest page, what stops it from being
mapped into two different guests? The refcount?

After contemplating this a bit, I have a somewhat different suggestion,
at least for TDX. In TDX, a guest logically has two entirely separate
physical address spaces: the secure address space and the shared address
space. They are managed separately, they have different contents, etc.
Normally one would expect a given numerical address (with the secure
selector bit masked off) to only exist in one of the two spaces, but
nothing in the architecture fundamentally requires this. And switching
a page between the secure and shared states is a heavyweight operation.

So what if KVM actually treated them completely separately? The secure
address space doesn't have any of the complex properties that the shared
address space has. There are no paravirt pages there, no KSM pages
there, no forked pages there, no MAP_ANONYMOUS pages there, no MMIO
pages there, etc. There could be a KVM API to allocate and deallocate a
secure page, and that would be it.

This could be inefficient if a guest does a bunch of MapGPA calls and
makes the shared address space highly sparse. That could potentially be
mitigated by allowing a (shared) user memory region to come with a
bitmap of which pages are actually mapped. Pages in the unmapped areas
are holes, and KVM won't look through to the QEMU page tables.

Does this make any sense? It would completely eliminate all this
PageGuest stuff -- a secure page would be backed by a *kernel*
allocation (potentially a pageable allocation a la tmpfs if TDX ever
learns how to swap, but that's no currently possible nor would it be
user visible), so the kernel can straightforwardly guarantee that user
pagetables will not contain references to secure pages and that GUP will
not be called on them.

I'm not sure how this would map to SEV. Certainly, with some degree of
host vs guest cooperation, SEV could work the same way, and the
migration support is pushing SEV in that direction.