Re: [PATCH v2 2/4] KVM: SEV: Drop page refcount early during RMP fault handling

From: Sean Christopherson

Date: Thu Aug 20 2026 - 19:36:27 EST


On Thu, Aug 20, 2026, Michael Roth wrote:
> On Thu, Aug 20, 2026 at 03:35:31PM -0700, Ackerley Tng wrote:
> > Michael Roth <michael.roth@xxxxxxx> writes:
> > Ah I see what you mean. I think we mean the same thing, let me add to
> > the commit message that I meant after dropping the refcount early. Does
> > this help?
> >
> > The filemap_invalidate_lock() is already dropped in kvm_gmem_get_pfn()
> > before returning to sev_handle_rmp_fault(). After dropping the
> > refcount earlier with kvm_release_page_unused(), these scenarios are
> > possible:
> >
> > 1. Since the filemap_invalidate_lock() is dropped, the page can be
> > truncated (or in future, converted), and the RMP entry is now
> > shared.
> >
> > In this case, existing RMP table handling (psmash and checking for
> > errors) would be sufficient. On finding a shared entry, psmashing
> > would fail gracefully and no warning would be emitted.
> >
> > 2. The page is truncated and freed, and then re-allocated to another
> > SNP VM. The RMP entry is now assigned, but to another SNP VM.
> >
> > To address this, adopt the MMU invalidation protocol to guard
> > psmashing.
>
> This reads kinda weird to me, as if with #2 we're documenting a "bug" that
> this patch fixes, but the bug would only exist if we partially applied the
> bits of this patch the drops the ref counts earlier and left out the
> bits of the patch that introduce the mmu notifier logic that replaces it.
>
> I think with patch 1 applied (which covers the
> psmash-a-now-shared-entry case while retaining the original refcount
> logic), the only thing this patch is doing is replacing the elevated
> refcount logic with the MMU invalidation logic as prep for dropping
> reliance of refcounts entirely.

(I had already typed this up before I saw Ackerley's response, so dagnabbit I'm
hitting send).

Agreed. Less is more in this case, unless you want to explain all of the gory
details of how KVM handles MMU invalidations.

Rework KVM's handling of RMP faults to rely on MMU invalidation logic for
safety, instead of the current approach of holding onto a folio reference
until the RMP operations are complete. I.e. drop the reference gifted by
guest_memfd immediately after getting the PFN, and instead do RMP updates
under mmu_lock, after checking for relevant MMU invalidations.

This will allow dropping guest_memfd's reference gifting entirely, which is
ideally how KVM would operate for all "follow PFN" operations (GUP has many
more complications, which is why KVM holds a reference across page faults
*on top* of the standard MMU invalidation logic).