* [PATCH 1/4] KVM: x86/mmu: Reload MMU on *every* page pre-fault attempt/iteration
2026-08-06 21:40 [PATCH 0/4] KVM: x86/mmu: Fix pre-fault and map private loops Sean Christopherson
@ 2026-08-06 21:40 ` Sean Christopherson
2026-08-07 8:58 ` Huang, Kai
2026-08-07 19:04 ` Edgecombe, Rick P
2026-08-06 21:40 ` [PATCH 2/4] KVM: x86/mmu: Harden "map private PFN" against unexpected root invalidation Sean Christopherson
` (2 subsequent siblings)
3 siblings, 2 replies; 12+ messages in thread
From: Sean Christopherson @ 2026-08-06 21:40 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini
Cc: kvm, linux-kernel, Kai Huang, Yan Zhao, Rick Edgecombe,
Sashiko Bot
Reload the MMU (which is a nop if the MMU doesn't need to be reloaded) on
every attempt to pre-fault a guest page, i.e. when the page fault path
signals that the caller should retry. If the synchronize_srcu_expedited()
in kvm_invalidate_memslot() completes before kvm_vcpu_pre_fault_memory()
grabs SRCU, but kvm_mmu_reload() in the pre-fault path completes before
kvm_invalidate_memslot() triggers x86's "fast zap all", then the pre-fault
task will reach kvm_tdp_page_prefault() with an invalid root.
Attempting to fault-in memory with an invalid root ultimately puts
kvm_tdp_page_prefault() into an infinite (breakable) retry loop, which
manifests most obviously as a hang in the pre_fault_memory_test selftest,
but also eventually causes RCU (SRCU?) to complain.
INFO: rcu_tasks detected stalls on tasks:
000000000cda47bd: .. nvcsw: 6/6 holdout: 1 idle_cpu: -1/25
task:pre_fault_memor state:R running task stack:12696
pid:95588 tgid:95588 ppid:95584 task_flags:0x400000 flags:0x00080801
Call Trace:
<TASK>
lock_release+0x4e/0x320
__get_user_pages+0x546/0xcd0
up_read+0x1b/0x30
get_user_pages_unlocked+0xee/0x350
hva_to_pfn+0xd3/0x3d0 [kvm]
lock_release+0x4e/0x320
xa_load+0x5c/0x170
xa_load+0x14c/0x170
__kvm_faultin_pfn+0xd9/0x130 [kvm]
lock_acquire+0x65/0x2b0
lock_release+0x4e/0x320
kvm_mmu_faultin_pfn+0x1e1/0x690 [kvm]
gup_fast_fallback+0x63e/0xdf0
kvm_tdp_page_fault+0xeb/0x140 [kvm]
kvm_mmu_do_page_fault+0x12e/0x200 [kvm]
kvm_arch_vcpu_pre_fault_memory+0x16e/0x200 [kvm]
kvm_vcpu_pre_fault_memory+0xc1/0x1f0 [kvm]
kvm_vcpu_pre_fault_memory+0x116/0x1f0 [kvm]
kvm_vcpu_ioctl+0x3a4/0x6b0 [kvm]
clockevents_program_event+0x5d/0x170
__se_sys_ioctl+0x6d/0xb0
entry_SYSCALL_64_after_hwframe+0x4b/0x53
do_syscall_64+0x10a/0x480
__irq_exit_rcu+0x8e/0x140
entry_SYSCALL_64_after_hwframe+0x4b/0x53
</TASK>
Fixes: 6e01b7601dfe ("KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory()")
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/mmu/mmu.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index c519e8e8d646..621b0a42f2a1 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -5036,6 +5036,10 @@ static int kvm_tdp_page_prefault(struct kvm_vcpu *vcpu, gpa_t gpa,
if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu))
return -EIO;
+ r = kvm_mmu_reload(vcpu);
+ if (r)
+ return r;
+
cond_resched();
r = kvm_mmu_do_page_fault(vcpu, gpa, error_code, true, NULL, level);
} while (r == RET_PF_RETRY);
@@ -5076,14 +5080,6 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
if (kvm_is_gfn_alias(vcpu->kvm, gpa_to_gfn(range->gpa)))
return -EINVAL;
- /*
- * reload is efficient when called repeatedly, so we can do it on
- * every iteration.
- */
- r = kvm_mmu_reload(vcpu);
- if (r)
- return r;
-
direct_bits = 0;
if (kvm_arch_has_private_mem(vcpu->kvm) &&
kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(range->gpa)))
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH 1/4] KVM: x86/mmu: Reload MMU on *every* page pre-fault attempt/iteration
2026-08-06 21:40 ` [PATCH 1/4] KVM: x86/mmu: Reload MMU on *every* page pre-fault attempt/iteration Sean Christopherson
@ 2026-08-07 8:58 ` Huang, Kai
2026-08-07 19:04 ` Edgecombe, Rick P
1 sibling, 0 replies; 12+ messages in thread
From: Huang, Kai @ 2026-08-07 8:58 UTC (permalink / raw)
To: pbonzini@redhat.com, seanjc@google.com
Cc: sashiko-bot@kernel.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, Zhao, Yan Y, Edgecombe, Rick P
On Thu, 2026-08-06 at 14:40 -0700, Sean Christopherson wrote:
> Reload the MMU (which is a nop if the MMU doesn't need to be reloaded) on
> every attempt to pre-fault a guest page, i.e. when the page fault path
> signals that the caller should retry. If the synchronize_srcu_expedited()
> in kvm_invalidate_memslot() completes before kvm_vcpu_pre_fault_memory()
> grabs SRCU, but kvm_mmu_reload() in the pre-fault path completes before
> kvm_invalidate_memslot() triggers x86's "fast zap all", then the pre-fault
> task will reach kvm_tdp_page_prefault() with an invalid root.
>
> Attempting to fault-in memory with an invalid root ultimately puts
> kvm_tdp_page_prefault() into an infinite (breakable) retry loop, which
> manifests most obviously as a hang in the pre_fault_memory_test selftest,
> but also eventually causes RCU (SRCU?) to complain.
>
> INFO: rcu_tasks detected stalls on tasks:
> 000000000cda47bd: .. nvcsw: 6/6 holdout: 1 idle_cpu: -1/25
> task:pre_fault_memor state:R running task stack:12696
> pid:95588 tgid:95588 ppid:95584 task_flags:0x400000 flags:0x00080801
> Call Trace:
> <TASK>
> lock_release+0x4e/0x320
> __get_user_pages+0x546/0xcd0
> up_read+0x1b/0x30
> get_user_pages_unlocked+0xee/0x350
> hva_to_pfn+0xd3/0x3d0 [kvm]
> lock_release+0x4e/0x320
> xa_load+0x5c/0x170
> xa_load+0x14c/0x170
> __kvm_faultin_pfn+0xd9/0x130 [kvm]
> lock_acquire+0x65/0x2b0
> lock_release+0x4e/0x320
> kvm_mmu_faultin_pfn+0x1e1/0x690 [kvm]
> gup_fast_fallback+0x63e/0xdf0
> kvm_tdp_page_fault+0xeb/0x140 [kvm]
> kvm_mmu_do_page_fault+0x12e/0x200 [kvm]
> kvm_arch_vcpu_pre_fault_memory+0x16e/0x200 [kvm]
> kvm_vcpu_pre_fault_memory+0xc1/0x1f0 [kvm]
> kvm_vcpu_pre_fault_memory+0x116/0x1f0 [kvm]
> kvm_vcpu_ioctl+0x3a4/0x6b0 [kvm]
> clockevents_program_event+0x5d/0x170
> __se_sys_ioctl+0x6d/0xb0
> entry_SYSCALL_64_after_hwframe+0x4b/0x53
> do_syscall_64+0x10a/0x480
> __irq_exit_rcu+0x8e/0x140
> entry_SYSCALL_64_after_hwframe+0x4b/0x53
> </TASK>
>
> Fixes: 6e01b7601dfe ("KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory()")
> Signed-off-by: Sean Christopherson <seanjc@google.com>
>
Reviewed-by: Kai Huang <kai.huang@intel.com>
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [PATCH 1/4] KVM: x86/mmu: Reload MMU on *every* page pre-fault attempt/iteration
2026-08-06 21:40 ` [PATCH 1/4] KVM: x86/mmu: Reload MMU on *every* page pre-fault attempt/iteration Sean Christopherson
2026-08-07 8:58 ` Huang, Kai
@ 2026-08-07 19:04 ` Edgecombe, Rick P
2026-08-07 19:11 ` Sean Christopherson
1 sibling, 1 reply; 12+ messages in thread
From: Edgecombe, Rick P @ 2026-08-07 19:04 UTC (permalink / raw)
To: pbonzini@redhat.com, seanjc@google.com
Cc: sashiko-bot@kernel.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, Huang, Kai, Zhao, Yan Y
On Thu, 2026-08-06 at 14:40 -0700, Sean Christopherson wrote:
> Reload the MMU (which is a nop if the MMU doesn't need to be reloaded) on
> every attempt to pre-fault a guest page, i.e. when the page fault path
> signals that the caller should retry. If the synchronize_srcu_expedited()
> in kvm_invalidate_memslot() completes before kvm_vcpu_pre_fault_memory()
> grabs SRCU, but kvm_mmu_reload() in the pre-fault path completes before
> kvm_invalidate_memslot() triggers x86's "fast zap all", then the pre-fault
> task will reach kvm_tdp_page_prefault() with an invalid root.
>
> Attempting to fault-in memory with an invalid root ultimately puts
> kvm_tdp_page_prefault() into an infinite (breakable) retry loop, which
> manifests most obviously as a hang in the pre_fault_memory_test selftest,
> but also eventually causes RCU (SRCU?) to complain.
>
> INFO: rcu_tasks detected stalls on tasks:
> 000000000cda47bd: .. nvcsw: 6/6 holdout: 1 idle_cpu: -1/25
> task:pre_fault_memor state:R running task stack:12696
> pid:95588 tgid:95588 ppid:95584 task_flags:0x400000 flags:0x00080801
> Call Trace:
> <TASK>
> lock_release+0x4e/0x320
> __get_user_pages+0x546/0xcd0
> up_read+0x1b/0x30
> get_user_pages_unlocked+0xee/0x350
> hva_to_pfn+0xd3/0x3d0 [kvm]
> lock_release+0x4e/0x320
> xa_load+0x5c/0x170
> xa_load+0x14c/0x170
> __kvm_faultin_pfn+0xd9/0x130 [kvm]
> lock_acquire+0x65/0x2b0
> lock_release+0x4e/0x320
> kvm_mmu_faultin_pfn+0x1e1/0x690 [kvm]
> gup_fast_fallback+0x63e/0xdf0
> kvm_tdp_page_fault+0xeb/0x140 [kvm]
> kvm_mmu_do_page_fault+0x12e/0x200 [kvm]
> kvm_arch_vcpu_pre_fault_memory+0x16e/0x200 [kvm]
> kvm_vcpu_pre_fault_memory+0xc1/0x1f0 [kvm]
> kvm_vcpu_pre_fault_memory+0x116/0x1f0 [kvm]
> kvm_vcpu_ioctl+0x3a4/0x6b0 [kvm]
> clockevents_program_event+0x5d/0x170
> __se_sys_ioctl+0x6d/0xb0
> entry_SYSCALL_64_after_hwframe+0x4b/0x53
> do_syscall_64+0x10a/0x480
> __irq_exit_rcu+0x8e/0x140
> entry_SYSCALL_64_after_hwframe+0x4b/0x53
> </TASK>
>
> Fixes: 6e01b7601dfe ("KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory()")
> Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
But, did you hit this somehow with an unmodified test and kernel? The log makes
me think it was hit in a normal test run. (which I failed to reproduce)
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [PATCH 1/4] KVM: x86/mmu: Reload MMU on *every* page pre-fault attempt/iteration
2026-08-07 19:04 ` Edgecombe, Rick P
@ 2026-08-07 19:11 ` Sean Christopherson
0 siblings, 0 replies; 12+ messages in thread
From: Sean Christopherson @ 2026-08-07 19:11 UTC (permalink / raw)
To: Rick P Edgecombe
Cc: pbonzini@redhat.com, sashiko-bot@kernel.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, Kai Huang, Yan Y Zhao
On Fri, Aug 07, 2026, Rick P Edgecombe wrote:
> On Thu, 2026-08-06 at 14:40 -0700, Sean Christopherson wrote:
> > Reload the MMU (which is a nop if the MMU doesn't need to be reloaded) on
> > every attempt to pre-fault a guest page, i.e. when the page fault path
> > signals that the caller should retry. If the synchronize_srcu_expedited()
> > in kvm_invalidate_memslot() completes before kvm_vcpu_pre_fault_memory()
> > grabs SRCU, but kvm_mmu_reload() in the pre-fault path completes before
> > kvm_invalidate_memslot() triggers x86's "fast zap all", then the pre-fault
> > task will reach kvm_tdp_page_prefault() with an invalid root.
> >
> > Attempting to fault-in memory with an invalid root ultimately puts
> > kvm_tdp_page_prefault() into an infinite (breakable) retry loop, which
> > manifests most obviously as a hang in the pre_fault_memory_test selftest,
> > but also eventually causes RCU (SRCU?) to complain.
> >
> > INFO: rcu_tasks detected stalls on tasks:
> > 000000000cda47bd: .. nvcsw: 6/6 holdout: 1 idle_cpu: -1/25
> > task:pre_fault_memor state:R running task stack:12696
> > pid:95588 tgid:95588 ppid:95584 task_flags:0x400000 flags:0x00080801
> > Call Trace:
> > <TASK>
> > lock_release+0x4e/0x320
> > __get_user_pages+0x546/0xcd0
> > up_read+0x1b/0x30
> > get_user_pages_unlocked+0xee/0x350
> > hva_to_pfn+0xd3/0x3d0 [kvm]
> > lock_release+0x4e/0x320
> > xa_load+0x5c/0x170
> > xa_load+0x14c/0x170
> > __kvm_faultin_pfn+0xd9/0x130 [kvm]
> > lock_acquire+0x65/0x2b0
> > lock_release+0x4e/0x320
> > kvm_mmu_faultin_pfn+0x1e1/0x690 [kvm]
> > gup_fast_fallback+0x63e/0xdf0
> > kvm_tdp_page_fault+0xeb/0x140 [kvm]
> > kvm_mmu_do_page_fault+0x12e/0x200 [kvm]
> > kvm_arch_vcpu_pre_fault_memory+0x16e/0x200 [kvm]
> > kvm_vcpu_pre_fault_memory+0xc1/0x1f0 [kvm]
> > kvm_vcpu_pre_fault_memory+0x116/0x1f0 [kvm]
> > kvm_vcpu_ioctl+0x3a4/0x6b0 [kvm]
> > clockevents_program_event+0x5d/0x170
> > __se_sys_ioctl+0x6d/0xb0
> > entry_SYSCALL_64_after_hwframe+0x4b/0x53
> > do_syscall_64+0x10a/0x480
> > __irq_exit_rcu+0x8e/0x140
> > entry_SYSCALL_64_after_hwframe+0x4b/0x53
> > </TASK>
> >
> > Fixes: 6e01b7601dfe ("KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory()")
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
>
>
> Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
>
> But, did you hit this somehow with an unmodified test and kernel? The log makes
> me think it was hit in a normal test run. (which I failed to reproduce)
Yes and yes. I often test by running all selftests in parallel. My guess is that
PREEMPT_LAZY plus oversubscribed CPUs allowed the timing condition to be hit. It
wasn't anywhere near 100% reproducible on my end either, maybe 1 out of every 10
runs? 20 runs? (of the entire suite of selftests).
P.S. I was very proud of myself: because the failure was so flaky, I used perf
and bpftrace to debug the issue instead of my usual hack-the-kernel approach. :-)
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 2/4] KVM: x86/mmu: Harden "map private PFN" against unexpected root invalidation
2026-08-06 21:40 [PATCH 0/4] KVM: x86/mmu: Fix pre-fault and map private loops Sean Christopherson
2026-08-06 21:40 ` [PATCH 1/4] KVM: x86/mmu: Reload MMU on *every* page pre-fault attempt/iteration Sean Christopherson
@ 2026-08-06 21:40 ` Sean Christopherson
2026-08-07 20:26 ` Edgecombe, Rick P
2026-08-06 21:40 ` [PATCH 3/4] KVM: x86/mmu: Top-up memory caches when retrying "map private PFN" Sean Christopherson
2026-08-06 21:40 ` [PATCH 4/4] KVM: x86/mmu: Add sanity check to detect stale page faults in " Sean Christopherson
3 siblings, 1 reply; 12+ messages in thread
From: Sean Christopherson @ 2026-08-06 21:40 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini
Cc: kvm, linux-kernel, Kai Huang, Yan Zhao, Rick Edgecombe,
Sashiko Bot
Move kvm_tdp_mmu_map_private_pfn()'s reload of the MMU into its tight loop
so that an unexpected root invalidation has a better chance of being
handled gracefully, even though it should be impossible for the vCPU's root
to be invalidated after the initial reload. As is, encountering an invalid
root is *guaranteed* to put the task into an infinite loop (albeit a
breakable loop that honors NEED_RESCHED).
Add a WARN to try and detect bugs that break KVM's expectations, along with
a comment to explain why it should be impossible for the root to be
invalidated.
Cc: Kai Huang <kai.huang@intel.com>
Cc: Yan Zhao <yan.y.zhao@intel.com>
Cc: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/mmu/mmu.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 621b0a42f2a1..c6cac893cbad 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -5184,10 +5184,6 @@ int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
if (kvm_gfn_is_write_tracked(kvm, fault.slot, fault.gfn))
return -EPERM;
- r = kvm_mmu_reload(vcpu);
- if (r)
- return r;
-
r = mmu_topup_memory_caches(vcpu, false);
if (r)
return r;
@@ -5199,10 +5195,21 @@ int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
if (kvm_test_request(KVM_REQ_VM_DEAD, vcpu))
return -EIO;
+ r = kvm_mmu_reload(vcpu);
+ if (r)
+ return r;
+
cond_resched();
guard(read_lock)(&kvm->mmu_lock);
+ /*
+ * Because slots_lock is held, it should be impossible for roots
+ * to be invalidated after the initial MMU reload. WARN, but
+ * continue and re-reload the MMU to try and keep the VM alive.
+ */
+ WARN_ON_ONCE(kvm_test_request(KVM_REQ_MMU_FREE_OBSOLETE_ROOTS, vcpu));
+
r = kvm_tdp_mmu_map(vcpu, &fault);
} while (r == RET_PF_RETRY);
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH 2/4] KVM: x86/mmu: Harden "map private PFN" against unexpected root invalidation
2026-08-06 21:40 ` [PATCH 2/4] KVM: x86/mmu: Harden "map private PFN" against unexpected root invalidation Sean Christopherson
@ 2026-08-07 20:26 ` Edgecombe, Rick P
2026-08-07 22:18 ` Sean Christopherson
0 siblings, 1 reply; 12+ messages in thread
From: Edgecombe, Rick P @ 2026-08-07 20:26 UTC (permalink / raw)
To: pbonzini@redhat.com, seanjc@google.com
Cc: sashiko-bot@kernel.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, Huang, Kai, Zhao, Yan Y
On Thu, 2026-08-06 at 14:40 -0700, Sean Christopherson wrote:
> @@ -5199,10 +5195,21 @@ int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
> if (kvm_test_request(KVM_REQ_VM_DEAD, vcpu))
> return -EIO;
>
> + r = kvm_mmu_reload(vcpu);
> + if (r)
> + return r;
> +
> cond_resched();
>
> guard(read_lock)(&kvm->mmu_lock);
>
> + /*
> + * Because slots_lock is held, it should be impossible for roots
> + * to be invalidated after the initial MMU reload. WARN, but
> + * continue and re-reload the MMU to try and keep the VM alive.
> + */
It might be a little confusing when no MMU reload follows the comment. Maybe
mention that the MMU reload is above? Either way ok to me.
> + WARN_ON_ONCE(kvm_test_request(KVM_REQ_MMU_FREE_OBSOLETE_ROOTS, vcpu));
> +
> r = kvm_tdp_mmu_map(vcpu, &fault);
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 2/4] KVM: x86/mmu: Harden "map private PFN" against unexpected root invalidation
2026-08-07 20:26 ` Edgecombe, Rick P
@ 2026-08-07 22:18 ` Sean Christopherson
0 siblings, 0 replies; 12+ messages in thread
From: Sean Christopherson @ 2026-08-07 22:18 UTC (permalink / raw)
To: Rick P Edgecombe
Cc: pbonzini@redhat.com, sashiko-bot@kernel.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, Kai Huang, Yan Y Zhao
On Fri, Aug 07, 2026, Rick P Edgecombe wrote:
> On Thu, 2026-08-06 at 14:40 -0700, Sean Christopherson wrote:
> > @@ -5199,10 +5195,21 @@ int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
> > if (kvm_test_request(KVM_REQ_VM_DEAD, vcpu))
> > return -EIO;
> >
> > + r = kvm_mmu_reload(vcpu);
> > + if (r)
> > + return r;
> > +
> > cond_resched();
> >
> > guard(read_lock)(&kvm->mmu_lock);
> >
> > + /*
> > + * Because slots_lock is held, it should be impossible for roots
> > + * to be invalidated after the initial MMU reload. WARN, but
> > + * continue and re-reload the MMU to try and keep the VM alive.
> > + */
>
> It might be a little confusing when no MMU reload follows the comment. Maybe
> mention that the MMU reload is above? Either way ok to me.
Yeah, I don't love the comment either. The subtlety that it doesn't capture is
that KVM_REQ_MMU_FREE_OBSOLETE_ROOTS doesn't actually mean the current root is
invalid, just that it might be invalid and the vCPU needs to check.
How about this?
/*
* Because slots_lock is held, it should be impossible for *any*
* roots to be invalidated after the initial MMU reload. WARN,
* but continue on; the above MMU reload will do the right thing
* if the current root is actually invalid.
*/
> > + WARN_ON_ONCE(kvm_test_request(KVM_REQ_MMU_FREE_OBSOLETE_ROOTS, vcpu));
> > +
> > r = kvm_tdp_mmu_map(vcpu, &fault);
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 3/4] KVM: x86/mmu: Top-up memory caches when retrying "map private PFN"
2026-08-06 21:40 [PATCH 0/4] KVM: x86/mmu: Fix pre-fault and map private loops Sean Christopherson
2026-08-06 21:40 ` [PATCH 1/4] KVM: x86/mmu: Reload MMU on *every* page pre-fault attempt/iteration Sean Christopherson
2026-08-06 21:40 ` [PATCH 2/4] KVM: x86/mmu: Harden "map private PFN" against unexpected root invalidation Sean Christopherson
@ 2026-08-06 21:40 ` Sean Christopherson
2026-08-07 20:38 ` Edgecombe, Rick P
2026-08-06 21:40 ` [PATCH 4/4] KVM: x86/mmu: Add sanity check to detect stale page faults in " Sean Christopherson
3 siblings, 1 reply; 12+ messages in thread
From: Sean Christopherson @ 2026-08-06 21:40 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini
Cc: kvm, linux-kernel, Kai Huang, Yan Zhao, Rick Edgecombe,
Sashiko Bot
When mapping a private PFN in TDX's post-populate callback, top-up the
memory caches on every attempt to map the PFN to harden against bugs in the
map flow that could consume cache entries even if mapping ultimately fails.
E.g. as pointed out by Sashiko, the in-progress Dynamic PAMT support could
consume PAMT cache entries on TDX-Module lock contention.
Harden KVM even though consuming an entry on failure is considered a KVM
bug, as retry is uncommon, top-up is "free" if there's no work to be done,
and populating a TDX guest's memory is a slow path, i.e. there's no
meaningful downside to the hardening.
Reported-by: Sashiko Bot <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260718061050.E17B01F000E9@smtp.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/mmu/mmu.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index c6cac893cbad..379f570ef04f 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -5184,10 +5184,6 @@ int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
if (kvm_gfn_is_write_tracked(kvm, fault.slot, fault.gfn))
return -EPERM;
- r = mmu_topup_memory_caches(vcpu, false);
- if (r)
- return r;
-
do {
if (signal_pending(current))
return -EINTR;
@@ -5199,6 +5195,10 @@ int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
if (r)
return r;
+ r = mmu_topup_memory_caches(vcpu, false);
+ if (r)
+ return r;
+
cond_resched();
guard(read_lock)(&kvm->mmu_lock);
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH 3/4] KVM: x86/mmu: Top-up memory caches when retrying "map private PFN"
2026-08-06 21:40 ` [PATCH 3/4] KVM: x86/mmu: Top-up memory caches when retrying "map private PFN" Sean Christopherson
@ 2026-08-07 20:38 ` Edgecombe, Rick P
2026-08-07 22:13 ` Sean Christopherson
0 siblings, 1 reply; 12+ messages in thread
From: Edgecombe, Rick P @ 2026-08-07 20:38 UTC (permalink / raw)
To: pbonzini@redhat.com, seanjc@google.com
Cc: sashiko-bot@kernel.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, Huang, Kai, Zhao, Yan Y
On Thu, 2026-08-06 at 14:40 -0700, Sean Christopherson wrote:
> When mapping a private PFN in TDX's post-populate callback, top-up the
> memory caches on every attempt to map the PFN to harden against bugs in the
> map flow that could consume cache entries even if mapping ultimately fails.
> E.g. as pointed out by Sashiko, the in-progress Dynamic PAMT support could
> consume PAMT cache entries on TDX-Module lock contention.
I think it is the same for the other caches consumed by the fault. I guess
"e.g." covers it. But it's not new after DPAMT.
>
> Harden KVM even though consuming an entry on failure is considered a KVM
> bug, as retry is uncommon
>
The locks held by the sole call path will prevent retries from being needed due
to TDX-specific details. So in the place where this code lives, it is a bug. But
can't really be hit. To me "retry is uncommon" sounds like it's a rare case that
is hittable. I guess you mean only in the uncommon case of bugs.
> , top-up is "free" if there's no work to be done,
> and populating a TDX guest's memory is a slow path, i.e. there's no
> meaningful downside to the hardening.
>
> Reported-by: Sashiko Bot <sashiko-bot@kernel.org>
> Closes: https://lore.kernel.org/all/20260718061050.E17B01F000E9@smtp.kernel.org
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
Thanks! And...
Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 3/4] KVM: x86/mmu: Top-up memory caches when retrying "map private PFN"
2026-08-07 20:38 ` Edgecombe, Rick P
@ 2026-08-07 22:13 ` Sean Christopherson
0 siblings, 0 replies; 12+ messages in thread
From: Sean Christopherson @ 2026-08-07 22:13 UTC (permalink / raw)
To: Rick P Edgecombe
Cc: pbonzini@redhat.com, sashiko-bot@kernel.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, Kai Huang, Yan Y Zhao
On Fri, Aug 07, 2026, Rick P Edgecombe wrote:
> On Thu, 2026-08-06 at 14:40 -0700, Sean Christopherson wrote:
> > When mapping a private PFN in TDX's post-populate callback, top-up the
> > memory caches on every attempt to map the PFN to harden against bugs in the
> > map flow that could consume cache entries even if mapping ultimately fails.
> > E.g. as pointed out by Sashiko, the in-progress Dynamic PAMT support could
> > consume PAMT cache entries on TDX-Module lock contention.
>
> I think it is the same for the other caches consumed by the fault. I guess
> "e.g." covers it. But it's not new after DPAMT.
I don't think so? Especially since as you point out below, nothing else can
muck with the SPTEs. The TDP MMU only consumes an cache entry if it successfully
creates a SPTE, and since nothing can muck with SPTEs, anything created on the
first attempt will still be there on subsequent attempts. I.e. the TDP MMU might
create SPTEs that are ultimately unused, but I don't think it can exhaust a cache.
> > Harden KVM even though consuming an entry on failure is considered a KVM
> > bug, as retry is uncommon
> >
>
> The locks held by the sole call path will prevent retries from being needed due
> to TDX-specific details. So in the place where this code lives, it is a bug. But
> can't really be hit. To me "retry is uncommon" sounds like it's a rare case that
> is hittable. I guess you mean only in the uncommon case of bugs.
Ah, I was thinking a different task could pre-fault memory, but pre-fault isn't
allowed until the VM is TD_STATE_RUNNABLE, and KVM_TDX_INIT_MEM_REGION is only
usable if the VM is *not* TD_STATE_RUNNABLE.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 4/4] KVM: x86/mmu: Add sanity check to detect stale page faults in "map private PFN"
2026-08-06 21:40 [PATCH 0/4] KVM: x86/mmu: Fix pre-fault and map private loops Sean Christopherson
` (2 preceding siblings ...)
2026-08-06 21:40 ` [PATCH 3/4] KVM: x86/mmu: Top-up memory caches when retrying "map private PFN" Sean Christopherson
@ 2026-08-06 21:40 ` Sean Christopherson
3 siblings, 0 replies; 12+ messages in thread
From: Sean Christopherson @ 2026-08-06 21:40 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini
Cc: kvm, linux-kernel, Kai Huang, Yan Zhao, Rick Edgecombe,
Sashiko Bot
Harden the "map private PFN" flow against potentially-fatal bugs or future
KVM changes by checking for a stale "fault" prior to actually mapping the
PFN into the guest. While it should be impossible for the "page fault" to
become stale, the sanity check is cheap, whereas a broken assumption would
have a high probability of leading to a guest-expoitable use-after-free.
Snapshot the invalidation sequence after acquiring mmu_lock to avoid false
positives, even though doing so completely voids anys and all protection
against unexpected invalidations. Pretty much the entire point of
kvm_tdp_mmu_map_private_pfn() is that it allows mapping a PFN that was
gifted by the caller, i.e. the caller would have to mess up its one and
only responsibility.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/mmu/mmu.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 379f570ef04f..76e3cd717324 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -5210,6 +5210,16 @@ int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
*/
WARN_ON_ONCE(kvm_test_request(KVM_REQ_MMU_FREE_OBSOLETE_ROOTS, vcpu));
+ /*
+ * Snapshot the invalidation sequence counter after acquiring
+ * mmu_lock, as guest_memfd guarantees the validity of the pfn,
+ * i.e. any concurrent invalidations are guaranteed to be
+ * irrelevant.
+ */
+ fault.mmu_seq = vcpu->kvm->mmu_invalidate_seq;
+ if (is_page_fault_stale(vcpu, &fault))
+ continue;
+
r = kvm_tdp_mmu_map(vcpu, &fault);
} while (r == RET_PF_RETRY);
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply related [flat|nested] 12+ messages in thread