* [PATCH 0/2] KVM: arm64: Fix stage-2 THP mapping on hosts with 2-level page tables
@ 2026-11-23 10:35 Fuad Tabba
2026-11-23 10:35 ` [PATCH 1/2] KVM: arm64: Fix THP block mapping when the PMD level is folded Fuad Tabba
2026-11-23 10:35 ` [PATCH 2/2] KVM: arm64: selftests: Test a THP whose first fault is on a tail page Fuad Tabba
0 siblings, 2 replies; 3+ messages in thread
From: Fuad Tabba @ 2026-11-23 10:35 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, kvmarm
Cc: Joey Gouly, Steffen Eiden, Suzuki K Poulose, Zenghui Yu,
Catalin Marinas, Will Deacon, Gavin Shan, Alexandru Elisei,
Vincent Donnefort, linux-arm-kernel, linux-kernel, Fuad Tabba
Hi folks,
A fix for how KVM maps a THP at stage-2 on a host with 2-level page
tables (64K pages with a 42-bit VA, or 16K pages with a 36-bit VA).
There, KVM also maps the host memory that follows a THP into the guest
if the guest's first access to the THP isn't to its first page.
transparent_hugepage_adjust() is meant to round the faulting pfn down to
the start of the THP, but its mask is a no-op when the PMD level is
folded, and has been since THP support was added. Before 6f745f1bb5bf2
("KVM: arm64: Convert user_mem_abort() to generic page-table API"), KVM
mapped only the THP anyway, which is why the Fixes: tag is on that
commit. Kernels before f583a53c2b8a ("KVM: arm64: Make fault_ipa
immutable") round the IPA with PMD_MASK, so only the pfn line of the fix
applies there.
Tested on QEMU with a 64K, 42-bit VA host kernel, where the new
selftest scenario fails without the fix and passes with it.
Based on Linux 7.4-rc3 (aebb727447640).
Cheers,
/fuad
Fuad Tabba (2):
KVM: arm64: Fix THP block mapping when the PMD level is folded
KVM: arm64: selftests: Test a THP whose first fault is on a tail page
arch/arm64/kvm/mmu.c | 4 +-
.../kvm/arm64/stage2_block_transitions.c | 56 ++++++++++++++++++-
2 files changed, 55 insertions(+), 5 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] KVM: arm64: Fix THP block mapping when the PMD level is folded
2026-11-23 10:35 [PATCH 0/2] KVM: arm64: Fix stage-2 THP mapping on hosts with 2-level page tables Fuad Tabba
@ 2026-11-23 10:35 ` Fuad Tabba
2026-11-23 10:35 ` [PATCH 2/2] KVM: arm64: selftests: Test a THP whose first fault is on a tail page Fuad Tabba
1 sibling, 0 replies; 3+ messages in thread
From: Fuad Tabba @ 2026-11-23 10:35 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, kvmarm
Cc: Joey Gouly, Steffen Eiden, Suzuki K Poulose, Zenghui Yu,
Catalin Marinas, Will Deacon, Gavin Shan, Alexandru Elisei,
Vincent Donnefort, linux-arm-kernel, linux-kernel, Fuad Tabba,
stable
When a THP backs guest memory, transparent_hugepage_adjust() rounds the
faulting gfn and pfn down to the THP's first page, and KVM maps the
whole THP with a PMD_SIZE block. However, the rounding uses a mask of
PTRS_PER_PMD - 1, and PTRS_PER_PMD is 1 when the PMD level is folded
(64K pages with a 42-bit VA, or 16K pages with a 36-bit VA). KVM then
maps PMD_SIZE worth of pages starting at the faulting page, and the
guest gets the host memory that follows the THP. With pKVM, the
hypervisor rejects the unaligned block, so the guest doesn't get that
memory.
Before the conversion to the generic page-table API, the unrounded pfn
went into a block descriptor, where the address bits below the block
size aren't part of the output address, so only the THP was mapped.
Round to PMD_SIZE >> PAGE_SHIFT pages instead.
Fixes: 6f745f1bb5bf2 ("KVM: arm64: Convert user_mem_abort() to generic page-table API")
Cc: stable@vger.kernel.org
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
arch/arm64/kvm/mmu.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 1db10d8d6db73..bab44021654a0 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1573,9 +1573,9 @@ transparent_hugepage_adjust(const struct kvm_s2_fault_desc *s2fd, kvm_pfn_t *pfn
if (sz < PMD_SIZE)
return PAGE_SIZE;
- gfn &= ~(PTRS_PER_PMD - 1);
+ gfn = ALIGN_DOWN(gfn, PMD_SIZE >> PAGE_SHIFT);
*gfnp = gfn;
- pfn &= ~(PTRS_PER_PMD - 1);
+ pfn = ALIGN_DOWN(pfn, PMD_SIZE >> PAGE_SHIFT);
*pfnp = pfn;
return PMD_SIZE;
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] KVM: arm64: selftests: Test a THP whose first fault is on a tail page
2026-11-23 10:35 [PATCH 0/2] KVM: arm64: Fix stage-2 THP mapping on hosts with 2-level page tables Fuad Tabba
2026-11-23 10:35 ` [PATCH 1/2] KVM: arm64: Fix THP block mapping when the PMD level is folded Fuad Tabba
@ 2026-11-23 10:35 ` Fuad Tabba
1 sibling, 0 replies; 3+ messages in thread
From: Fuad Tabba @ 2026-11-23 10:35 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, kvmarm
Cc: Joey Gouly, Steffen Eiden, Suzuki K Poulose, Zenghui Yu,
Catalin Marinas, Will Deacon, Gavin Shan, Alexandru Elisei,
Vincent Donnefort, linux-arm-kernel, linux-kernel, Fuad Tabba
On a host whose PMD level is folded, KVM mapped host memory past a THP
when the guest first touched the THP at a page other than its first.
Add a scenario for that case. The guest writes to the second host page
of a memslot backed by one THP, then reads the first address past the
memslot. The test checks that the write landed in that page, and that
the read exits to userspace as MMIO. On a host whose PMD level isn't
folded, the scenario passes with or without the fix. It doesn't use
hugetlb pages, so it runs before the probe that skips the rest of the
test when they aren't reserved.
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
.../kvm/arm64/stage2_block_transitions.c | 56 ++++++++++++++++++-
1 file changed, 53 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/kvm/arm64/stage2_block_transitions.c b/tools/testing/selftests/kvm/arm64/stage2_block_transitions.c
index 5fd47f4ada1f3..000e8ec7ce8cc 100644
--- a/tools/testing/selftests/kvm/arm64/stage2_block_transitions.c
+++ b/tools/testing/selftests/kvm/arm64/stage2_block_transitions.c
@@ -3,10 +3,10 @@
* Copyright (c) 2026 Google LLC
* Author: Fuad Tabba <fuad.tabba@linux.dev>
*
- * stage2_block_transitions - Exercise stage-2 block/page granularity changes
- * that dirty logging forces at fault time, and assert the guest completes.
+ * stage2_block_transitions - Exercise stage-2 block/page granularity at fault
+ * time: the changes dirty logging forces, and how far a THP block reaches.
*
- * Both scenarios need the fault handler to allocate at fault time (a fresh
+ * Scenarios 1 and 2 need the fault handler to allocate at fault time (a fresh
* mapping and/or page-table pages while holding mmu_lock), so a fault path
* that fails to stage that memory manifests as a KVM_RUN error or, worse, a
* host crash. The asserted property is host-agnostic: the guest runs the
@@ -27,6 +27,11 @@
* fault that must split the block into pages during logging, draining
* page-table pages. Skipped on CTR_EL0.DIC hardware, where mappings are
* made executable eagerly and the execute fault never occurs.
+ *
+ * Scenario 3 - THP whose first fault is on a tail page:
+ * The guest writes to the second host page of a memslot backed by one
+ * THP, then reads the first address past it. If KVM mapped the THP and
+ * nothing past it, the write lands in the memslot and the read exits as MMIO.
*/
#include <linux/bitfield.h>
#include <linux/bitmap.h>
@@ -198,6 +203,49 @@ static void test_exec_split_drain(void)
kvm_vm_free(vm);
}
+static void thp_guest_code(u64 touch_gva, u64 past_gva)
+{
+ WRITE_ONCE(*(u64 *)touch_gva, 0x1);
+ READ_ONCE(*(u64 *)past_gva);
+ GUEST_DONE();
+}
+
+static void test_thp_tail_first_touch(void)
+{
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ struct ucall uc;
+ size_t npages;
+ u64 thp_size;
+ u64 gpa;
+
+ if (!thp_configured()) {
+ ksft_print_msg("SKIP THP tail first touch: THP not configured\n");
+ return;
+ }
+ thp_size = get_trans_hugepagesz();
+
+ vm = vm_create_with_one_vcpu(&vcpu, thp_guest_code);
+ npages = thp_size / vm->page_size;
+
+ gpa = (vm_compute_max_gfn(vm) * vm->page_size) - 2 * thp_size;
+ gpa = align_down(gpa, thp_size);
+
+ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_THP, gpa, DATA_SLOT, npages, 0);
+ virt_pg_map(vm, TEST_GVA + getpagesize(), gpa + getpagesize());
+ virt_pg_map(vm, TEST_GVA + thp_size, gpa + thp_size);
+ vcpu_args_set(vcpu, 2, TEST_GVA + getpagesize(), TEST_GVA + thp_size);
+
+ vcpu_run(vcpu);
+ TEST_ASSERT(get_ucall(vcpu, &uc) != UCALL_DONE,
+ "Read past the memslot completed without an exit");
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_MMIO);
+ TEST_ASSERT_EQ(vcpu->run->mmio.phys_addr, gpa + thp_size);
+ TEST_ASSERT_EQ(*(u64 *)addr_gpa2hva(vm, gpa + getpagesize()), 0x1);
+
+ kvm_vm_free(vm);
+}
+
/*
* The explicit-size hugetlb backing hard-fails region creation if the pages
* are not already reserved, so probe here and skip rather than abort. The
@@ -216,6 +264,8 @@ static void require_hugepages(size_t bytes)
int main(void)
{
+ test_thp_tail_first_touch();
+
require_hugepages(NR_BLOCKS * BLOCK_SIZE);
test_block_collapse();
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-23 11:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 10:35 [PATCH 0/2] KVM: arm64: Fix stage-2 THP mapping on hosts with 2-level page tables Fuad Tabba
2026-11-23 10:35 ` [PATCH 1/2] KVM: arm64: Fix THP block mapping when the PMD level is folded Fuad Tabba
2026-11-23 10:35 ` [PATCH 2/2] KVM: arm64: selftests: Test a THP whose first fault is on a tail page Fuad Tabba
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox