All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: stable@vger.kernel.org, Hyunwoo Kim <imv4bel@gmail.com>,
	 Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [PATCH] KVM: x86: Check for invalid/obsolete root *after* making MMU pages available
Date: Mon, 10 Aug 2026 15:47:30 -0700	[thread overview]
Message-ID: <anpVAhjqvBvIuoa0@google.com> (raw)
In-Reply-To: <20260808062208.522577-1-senozhatsky@chromium.org>

On Sat, Aug 08, 2026, Sergey Senozhatsky wrote:
> From: Sean Christopherson <seanjc@google.com>
> 
> Check for a "stale" page fault, i.e. for an invalid and/or obsolete root,
> after making MMU pages available for the shadow MMU.  If reclaiming shadow
> pages zaps an in-use root, i.e. marks it invalid, then KVM will attempt to
> map memory into an invalid root.  On its own, populating an invalid root is
> "fine", but because child shadow pages inherit their parent's role, any
> children created during the map/fetch will be created as invalid pages,
> thus violating KVM's invariant that invalid pages are never on the list of
> active MMU pages.
> 
> Note, the underlying flaw has existed since KVM first started tracking
> invalid roots in 2008 (commit 2e53d63acba7, "KVM: MMU: ignore zapped root
> pagetables"), but the true badness only came along in 2020 (Linux 5.9)
> with the invariant that invalid shadow pages can't be on the list of
> active pages.
> 
> Note #2, inheriting role.invalid when creating child shadow pages is also
> far from ideal; that flaw will be addressed separately.
> 
> Conflicts:
> 	arch/x86/kvm/mmu/mmu.c
> 	arch/x86/kvm/mmu/paging_tmpl.h
> 
> Reported-by: Hyunwoo Kim <imv4bel@gmail.com>
> Fixes: f95eec9bed76 ("KVM: x86/mmu: Don't put invalid SPs back on the list of active pages")
> Cc: stable@vger.kernel.org
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> (cherry picked from commit 2abd5287f08319fa35764566b15c6e22cb1068db)
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
> ---
>  arch/x86/kvm/mmu/mmu.c         | 11 ++++++-----
>  arch/x86/kvm/mmu/paging_tmpl.h | 10 ++++++----
>  2 files changed, 12 insertions(+), 9 deletions(-)
> 
> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index 7785da8f0ad3..1f4d306e5c81 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c
> @@ -4272,22 +4272,23 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
>  
>  	orig_pfn = fault->pfn;
>  
> -	r = RET_PF_RETRY;
> -
>  	if (is_tdp_mmu_fault)
>  		read_lock(&vcpu->kvm->mmu_lock);
>  	else
>  		write_lock(&vcpu->kvm->mmu_lock);
>  
> -	if (is_page_fault_stale(vcpu, fault, mmu_seq))
> -		goto out_unlock;
> -
>  	if (is_tdp_mmu_fault) {
>  		r = kvm_tdp_mmu_map(vcpu, fault);

is_page_fault_stale() needs be checked for the TDP MMU as well.  I had the
pleasure of doing this cherry-pick for an internal kernel, this is what I thought
yielded the easiest-to-read sequence:

        if (is_tdp_mmu_fault) {
                read_lock(&vcpu->kvm->mmu_lock);
        } else {
                write_lock(&vcpu->kvm->mmu_lock);

                r = make_mmu_pages_available(vcpu);
                if (r)
                        goto out_unlock;
        }

        if (is_page_fault_stale(vcpu, fault, mmu_seq)) {
                r = RET_PF_RETRY;
                goto out_unlock;
        }

        if (is_tdp_mmu_fault)
                r = kvm_tdp_mmu_map(vcpu, fault);
        else
                r = __direct_map(vcpu, fault);

As a diff...

diff --git arch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/mmu.c
index 7785da8f0ad3..d05bd467dc5d 100644
--- arch/x86/kvm/mmu/mmu.c
+++ arch/x86/kvm/mmu/mmu.c
@@ -4272,24 +4272,25 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
 
        orig_pfn = fault->pfn;
 
-       r = RET_PF_RETRY;
-
-       if (is_tdp_mmu_fault)
-               read_lock(&vcpu->kvm->mmu_lock);
-       else
-               write_lock(&vcpu->kvm->mmu_lock);
-
-       if (is_page_fault_stale(vcpu, fault, mmu_seq))
-               goto out_unlock;
-
        if (is_tdp_mmu_fault) {
-               r = kvm_tdp_mmu_map(vcpu, fault);
+               read_lock(&vcpu->kvm->mmu_lock);
        } else {
+               write_lock(&vcpu->kvm->mmu_lock);
+
                r = make_mmu_pages_available(vcpu);
                if (r)
                        goto out_unlock;
+       }
+
+       if (is_page_fault_stale(vcpu, fault, mmu_seq)) {
+               r = RET_PF_RETRY;
+               goto out_unlock;
+       }
+
+       if (is_tdp_mmu_fault)
+               r = kvm_tdp_mmu_map(vcpu, fault);
+       else
                r = __direct_map(vcpu, fault);
-       }
 
 out_unlock:
        if (is_tdp_mmu_fault)
diff --git arch/x86/kvm/mmu/paging_tmpl.h arch/x86/kvm/mmu/paging_tmpl.h
index 1c7d73b8081c..75fd2b7dc0e5 100644
--- arch/x86/kvm/mmu/paging_tmpl.h
+++ arch/x86/kvm/mmu/paging_tmpl.h
@@ -869,15 +869,17 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
 
        orig_pfn = fault->pfn;
 
-       r = RET_PF_RETRY;
        write_lock(&vcpu->kvm->mmu_lock);
 
-       if (is_page_fault_stale(vcpu, fault, mmu_seq))
-               goto out_unlock;
-
        r = make_mmu_pages_available(vcpu);
        if (r)
                goto out_unlock;
+
+       if (is_page_fault_stale(vcpu, fault, mmu_seq)) {
+               r = RET_PF_RETRY;
+               goto out_unlock;
+       }
+
        r = FNAME(fetch)(vcpu, fault, &walker);
 
 out_unlock:


  parent reply	other threads:[~2026-08-10 22:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-08  6:22 [PATCH] KVM: x86: Check for invalid/obsolete root *after* making MMU pages available Sergey Senozhatsky
2026-08-08  6:27 ` Sergey Senozhatsky
2026-08-10 14:08   ` Sean Christopherson
2026-08-10 14:40     ` Sergey Senozhatsky
2026-08-10 22:47 ` Sean Christopherson [this message]
2026-08-11  1:49   ` Sergey Senozhatsky
2026-08-10 23:57 ` Sasha Levin
  -- strict thread matches above, loose matches on Subject: below --
2026-07-21 10:28 Paolo Bonzini

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=anpVAhjqvBvIuoa0@google.com \
    --to=seanjc@google.com \
    --cc=imv4bel@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=senozhatsky@chromium.org \
    --cc=stable@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.