mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ackerley Tng via B4 Relay <devnull+ackerleytng.google.com@kernel.org>
To: Sean Christopherson <seanjc@google.com>,
	 Paolo Bonzini <pbonzini@redhat.com>,
	Thomas Gleixner <tglx@kernel.org>,
	 Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org,  "H. Peter Anvin" <hpa@zytor.com>,
	Fuad Tabba <fuad.tabba@linux.dev>,
	 David Hildenbrand <david@kernel.org>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Ackerley Tng <ackerleytng@google.com>
Subject: [PATCH] KVM: x86/mmu: Prevent hugepage recovery on non-guest_memfd private GFNs
Date: Fri, 20 Nov 2026 13:07:12 -0800	[thread overview]
Message-ID: <20261120-gmem-fix-gmem-max-mapping-level-v1-1-108788914fe8@google.com> (raw)

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>



             reply	other threads:[~2026-11-20 21:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 21:07 Ackerley Tng via B4 Relay [this message]
2026-11-22  4:52 ` Jiaming Fan
2026-12-03 17:57   ` Sean Christopherson

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=20261120-gmem-fix-gmem-max-mapping-level-v1-1-108788914fe8@google.com \
    --to=devnull+ackerleytng.google.com@kernel.org \
    --cc=ackerleytng@google.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@kernel.org \
    --cc=fuad.tabba@linux.dev \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=tglx@kernel.org \
    --cc=x86@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 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®