* [PATCH] KVM: x86/mmu: Prevent hugepage recovery on non-guest_memfd private GFNs
@ 2026-11-20 21:07 Ackerley Tng via B4 Relay
2026-11-22 4:52 ` Jiaming Fan
0 siblings, 1 reply; 5+ messages in thread
From: Ackerley Tng via B4 Relay @ 2026-11-20 21:07 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Fuad Tabba,
David Hildenbrand
Cc: kvm, linux-kernel, Ackerley Tng
From: Ackerley Tng <ackerleytng@google.com>
When determining the maximum mapping level outside of a page fault
(such as when disabling dirty logging), KVM queries the VM memory
attributes to determine whether a GFN is private. Userspace can mark a
GFN as private in the VM attributes even when the covering memslot is
not backed by guest_memfd.
Previously, this caused the mapping level calculation to fall into
kvm_gmem_max_mapping_level(), triggering a kernel warning.
Here's 1 way to trigger the warning on a TDX VM, with VM memory attributes
enabled on KVM.
1. Create a non-guest_memfd memslot covering a 1G-aligned base GFN with
dirty logging enabled.
2. In that memslot, fault a shared page outside the first 2M chunk so a
non-leaf PTE at the 1G level is installed.
3. Mark the first 2M chunk (at the 1G-aligned base GFN) private. Conversion
zaps that 2M chunk, while the shared mapping outside it keeps the 1G
non-leaf PTE present.
4. Disable dirty logging on the memslot.
Hugepage recovery iterates over the present 1G non-leaf PTE, sees that its
base GFN is private and the 2M range is not mixed, and calls into
kvm_gmem_max_mapping_level() without a fault context, hitting the WARN.
Return 4K as the maximum mapping level when a GFN is marked private in
a non-fault path for a memslot without guest_memfd. Fault paths already
disallow mapping private GFNs in non-guest_memfd memslots, and
returning 4K prevents hugepage recovery on such GFNs.
Fixes: a3522ac71f5e ("KVM: x86/mmu: Enforce guest_memfd's max order when recovering hugepages")
Signed-off-by: Ackerley Tng <ackerleytng@google.com>
---
arch/x86/kvm/mmu/mmu.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index f89e9f8a6cc2f..af40fbe9001a7 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -3489,6 +3489,14 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm, struct kvm_page_fault *fault,
} else {
max_level = PG_LEVEL_NUM;
is_private = kvm_is_private_gfn(kvm, gfn);
+
+ /*
+ * is_private (set in VM attributes) should never have been used
+ * with a non-guest_memfd memslot. Return PG_LEVEL_4K to prevent
+ * hugepage recovery.
+ */
+ if (is_private && !kvm_slot_has_gmem(slot))
+ return PG_LEVEL_4K;
}
max_level = min(max_level, max_huge_page_level);
---
base-commit: d0f871bfb831705d9f55479f5b2b40f636ea7b16
change-id: 20261120-gmem-fix-gmem-max-mapping-level-accbb71c3352
Best regards,
--
Ackerley Tng <ackerleytng@google.com>
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] KVM: x86/mmu: Prevent hugepage recovery on non-guest_memfd private GFNs 2026-11-20 21:07 [PATCH] KVM: x86/mmu: Prevent hugepage recovery on non-guest_memfd private GFNs Ackerley Tng via B4 Relay @ 2026-11-22 4:52 ` Jiaming Fan 2026-12-03 17:57 ` Sean Christopherson 0 siblings, 1 reply; 5+ messages in thread From: Jiaming Fan @ 2026-11-22 4:52 UTC (permalink / raw) To: Ackerley Tng Cc: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Fuad Tabba, David Hildenbrand, kvm, linux-kernel On Fri, Nov 20, 2026, Ackerley Tng wrote: > + /* > + * is_private (set in VM attributes) should never have been used > + * with a non-guest_memfd memslot. Return PG_LEVEL_4K to prevent > + * hugepage recovery. > + */ Hi Ackerley, This comment feels a bit odd to me. KVM does allow marking a GFN private when its memslot has no guest_memfd, since PRIVATE attributes are per VM, not per memslot. I could reproduce the WARN with a KVM_X86_SW_PROTECTED_VM on AMD (nested, not TDX), and it's gone with this patch. Tested-by: Jiaming Fan <fan.jiaming@hotmail.com> ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KVM: x86/mmu: Prevent hugepage recovery on non-guest_memfd private GFNs 2026-11-22 4:52 ` Jiaming Fan @ 2026-12-03 17:57 ` Sean Christopherson 2026-12-05 0:17 ` Ackerley Tng 0 siblings, 1 reply; 5+ messages in thread From: Sean Christopherson @ 2026-12-03 17:57 UTC (permalink / raw) To: Jiaming Fan Cc: Ackerley Tng, Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Fuad Tabba, David Hildenbrand, kvm, linux-kernel On Sun, Nov 22, 2026, Jiaming Fan wrote: > On Fri, Nov 20, 2026, Ackerley Tng wrote: > > + /* > > + * is_private (set in VM attributes) should never have been used > > + * with a non-guest_memfd memslot. Return PG_LEVEL_4K to prevent > > + * hugepage recovery. > > + */ > > Hi Ackerley, > > This comment feels a bit odd to me. KVM does allow marking a GFN private > when its memslot has no guest_memfd, since PRIVATE attributes are per VM, > not per memslot. 100% agreed. And FWIW, I don't love the code, but I also can't come up with a more elegant solution. To address both to some degree, I'll squash the below. That way it's more obvious how/why we can end up with a private GFN without an associated gmem slot, and it makes me dislike the one-off check a bit less. diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 8848ebd9feac..408187ccfdc8 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3489,14 +3489,10 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm, struct kvm_page_fault *fault, } else { max_level = PG_LEVEL_NUM; is_private = kvm_is_private_gfn(kvm, gfn); - - /* - * is_private (set in VM attributes) should never have been used - * with a non-guest_memfd memslot. Return PG_LEVEL_4K to prevent - * hugepage recovery. - */ - if (is_private && !kvm_slot_has_gmem(slot)) + if (is_private && !kvm_slot_has_gmem(slot)) { + WARN_ON_ONCE(gmem_in_place_conversion); return PG_LEVEL_4K; + } } max_level = min(max_level, max_huge_page_level); > I could reproduce the WARN with a KVM_X86_SW_PROTECTED_VM on AMD (nested, > not TDX), and it's gone with this patch. > > Tested-by: Jiaming Fan <fan.jiaming@hotmail.com> ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KVM: x86/mmu: Prevent hugepage recovery on non-guest_memfd private GFNs 2026-12-03 17:57 ` Sean Christopherson @ 2026-12-05 0:17 ` Ackerley Tng 2026-12-05 0:48 ` Sean Christopherson 0 siblings, 1 reply; 5+ messages in thread From: Ackerley Tng @ 2026-12-05 0:17 UTC (permalink / raw) To: Sean Christopherson, Jiaming Fan Cc: Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Fuad Tabba, David Hildenbrand, kvm, linux-kernel Correction to the commit message 3. Mark the first 2M chunk (at the 1G-aligned base GFN) private. Conversion zaps that 2M chunk, while the shared mapping outside it keeps the 1G non-leaf PTE present. should read 3. Mark the first 2M chunk (at the 1G-aligned base GFN) private. The shared mapping outside the private range keeps the 1G non-leaf PTE present. (no zapping since the range just converted was not mapped) > On Sun, Nov 22, 2026, Jiaming Fan wrote: >> On Fri, Nov 20, 2026, Ackerley Tng wrote: >> > + /* >> > + * is_private (set in VM attributes) should never have been used >> > + * with a non-guest_memfd memslot. Return PG_LEVEL_4K to prevent >> > + * hugepage recovery. >> > + */ >> >> Hi Ackerley, >> >> This comment feels a bit odd to me. KVM does allow marking a GFN private >> when its memslot has no guest_memfd, since PRIVATE attributes are per VM, >> not per memslot. > > 100% agreed. And FWIW, I don't love the code, but I also can't come up with a > more elegant solution. To address both to some degree, I'll squash the below. > That way it's more obvious how/why we can end up with a private GFN without an > associated gmem slot, and it makes me dislike the one-off check a bit less. > KVM does allow marking a GFN private when the memslot has no guest_memfd. What I meant with the comment is that the only provider that really works with private memory for SNP, TDX and CCA, is guest_memfd, and so this is a path that userspace shouldn't really have been set up for any legitimate use cases. > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index 8848ebd9feac..408187ccfdc8 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -3489,14 +3489,10 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm, struct kvm_page_fault *fault, > } else { > max_level = PG_LEVEL_NUM; > is_private = kvm_is_private_gfn(kvm, gfn); > - > - /* > - * is_private (set in VM attributes) should never have been used > - * with a non-guest_memfd memslot. Return PG_LEVEL_4K to prevent > - * hugepage recovery. > - */ > - if (is_private && !kvm_slot_has_gmem(slot)) > + if (is_private && !kvm_slot_has_gmem(slot)) { > + WARN_ON_ONCE(gmem_in_place_conversion); Good to have this as a second layer of defence. At least for now gmem can't be used legitimately with anything that takes the !fault branch (this one being changed) but good to know if/when gmem has support for dirty logging. Not sure if you want to take this separately, AI found a reproducer to hit this WARN_ON_ONCE() you added deterministically. With gmem_in_place_conversion=true, 1. Use a KVM_X86_DEFAULT_VM, where kvm_arch_has_private_mem(kvm) is false. 2. Create a non-gmem memslot with as_id=1 with dirty logging enabled, overlapping with a gmem memslot with as_id=0. The gmem associated should be all-private (no INIT_SHARED) 3. Disable dirty logging on the as_id=1 memslot. is_private is true since we consulted as_id=0, kvm_slot_has_gmem(slot) is false because we checked on as_id=1. We've been assuming gmem only works with as_id=0, which is still what we want, but kvm_is_private_gfn() always checks as_id=0. Kinda wonky because if gmem_in_place_conversion=false, VM-level privateness is per-VM, which I guess can be thought of as defaulting to as_id=0. So the two options I think are viable are 1. In kvm_gmem_is_private_gfn(), return false if the !kvm_arch_has_private_mem(). This isn't as_id specific but it is correct and blocks this path. 2. Check as_id here in the caller. kvm_mmu_faultin_pfn() will be able to fault in a gmem-private page into SMM, but only for VMs that don't support private pages... so that's a userspace bug? > return PG_LEVEL_4K; > + } > } > > max_level = min(max_level, max_huge_page_level); > >> I could reproduce the WARN with a KVM_X86_SW_PROTECTED_VM on AMD (nested, >> not TDX), and it's gone with this patch. >> >> Tested-by: Jiaming Fan <fan.jiaming@hotmail.com> Thanks for testing! Sean, when you said squash, do you mean you'd do that when applying? Do you need me to send v2? ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KVM: x86/mmu: Prevent hugepage recovery on non-guest_memfd private GFNs 2026-12-05 0:17 ` Ackerley Tng @ 2026-12-05 0:48 ` Sean Christopherson 0 siblings, 0 replies; 5+ messages in thread From: Sean Christopherson @ 2026-12-05 0:48 UTC (permalink / raw) To: Ackerley Tng Cc: Jiaming Fan, Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Fuad Tabba, David Hildenbrand, kvm, linux-kernel On Fri, Dec 04, 2026, Ackerley Tng wrote: > Not sure if you want to take this separately, AI found a reproducer to > hit this WARN_ON_ONCE() you added deterministically. Separately, I suspect in we have an internal syzkaller report that's hitting the same underlying flaw. > With gmem_in_place_conversion=true, > > 1. Use a KVM_X86_DEFAULT_VM, where kvm_arch_has_private_mem(kvm) is > false. > 2. Create a non-gmem memslot with as_id=1 with dirty logging enabled, > overlapping with a gmem memslot with as_id=0. The gmem associated > should be all-private (no INIT_SHARED) > 3. Disable dirty logging on the as_id=1 memslot. > > is_private is true since we consulted as_id=0, kvm_slot_has_gmem(slot) > is false because we checked on as_id=1. > > We've been assuming gmem only works with as_id=0, which is still what we > want, but kvm_is_private_gfn() always checks as_id=0. > > Kinda wonky because if gmem_in_place_conversion=false, VM-level > privateness is per-VM, which I guess can be thought of as defaulting to > as_id=0. > > So the two options I think are viable are > > 1. In kvm_gmem_is_private_gfn(), return false if the > !kvm_arch_has_private_mem(). This isn't as_id specific but it is correct > and blocks this path. > > 2. Check as_id here in the caller. kvm_mmu_faultin_pfn() will be able to > fault in a gmem-private page into SMM, but only for VMs that don't > support private pages... so that's a userspace bug? Hmm, screw SMM, that's my answer. :-D #1 is the way to go, but we don't need to add to the hot path. set_attributes() already disallows marking a GFN private in that case, we just forgot to require that non-private memory be INIT_SHARED. diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 93f3d29d5751..778ac32930c5 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -1007,6 +1007,10 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) if (flags & ~kvm_gmem_get_supported_flags(kvm)) return -EINVAL; + if (!kvm_arch_has_private_mem(kvm) && + !(flags & GUEST_MEMFD_FLAG_INIT_SHARED)) + return -EINVAL; + if (size <= 0 || !PAGE_ALIGNED(size)) return -EINVAL; > > return PG_LEVEL_4K; > > + } > > } > > > > max_level = min(max_level, max_huge_page_level); > > > >> I could reproduce the WARN with a KVM_X86_SW_PROTECTED_VM on AMD (nested, > >> not TDX), and it's gone with this patch. > >> > >> Tested-by: Jiaming Fan <fan.jiaming@hotmail.com> > > Thanks for testing! > > Sean, when you said squash, do you mean you'd do that when applying? Do > you need me to send v2? No, I already squashed. ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-05 0:48 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-20 21:07 [PATCH] KVM: x86/mmu: Prevent hugepage recovery on non-guest_memfd private GFNs Ackerley Tng via B4 Relay 2026-11-22 4:52 ` Jiaming Fan 2026-12-03 17:57 ` Sean Christopherson 2026-12-05 0:17 ` Ackerley Tng 2026-12-05 0:48 ` Sean Christopherson
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®