Skip to contentSkip to conversation

KVM: arm64: pKVM stage-2 mapping and memcache fixes

PATCH Applied Fuad Tabba 6 participants 23 messages Jul 17 – Jul 31

Subsystems (4)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

v5 cover KVM: arm64: pKVM stage-2 mapping and memcache fixes

Hi folks,

Changes since v4 [1]:
- Restored the permission-fault and dirty-logging memcache top-ups that v4 dropped. Both fix real bugs.
- Patch 1 uses the anonymous bitfield encoding rather than the open-coded mask and helpers. (Marc)
- Reshaped the permission-fault top-up to stage the full memcache under pKVM, not just the mapping object. The object-only form still returned -ENOMEM and tripped a WARN under the hypervisor's unconditional min-pages check.
- Re-scoped the dirty-logging top-up to its generic, non-pKVM failure mode, now that the permission-fault patch covers pKVM.
- Added three adjacent fixes found while going through the series, and a selftest for the block transitions.
- Patches 1 and 3 keep Bradley's Signed-off-by from v3; my changes to each are noted in a [tabba: ...] line.

I picked this up while reviewing Bradley's "mapping cache" series [2]:
v4 dropped two fixes from v3 that address real bugs, so I've collected the three fixes back together, reshaped patch 1 per Marc's review [3], added a few adjacent fixes I found along the way and a selftest, and am reposting as v5.

Most of the fixes are in the pKVM stage-2 walker. On a pKVM host a non-protected guest's stage-2 faults go through pkvm_pgtable_*(), which diverges from the generic walker in several ways that are bugs: cache maintenance on non-cacheable mappings (patch 1), a missing memcache top-up on permission faults that under pKVM still allocate (patch 2), a full flush walk on FWB hardware (patch 4), a WARN on a guest-reachable map failure (patch 5), and an eager-split capability whose pKVM backend is only a stub (patch 6). These affect non-protected guests only, since dispatch and memcache selection key on the host-global pKVM mode, and protected guests take no permission faults.

Patch 3 is not pKVM-specific. During dirty logging a non-write permission fault, an instruction fetch, still needs a page-table page to split a block, but the memcache top-up is gated on write faults. That fault path is generic, so the fix is too.

The two memcache top-ups came from sashiko review-bot findings [4][5], and both check out against the code.

The series is structured as follows:

1: Skip cache maintenance for non-cacheable mappings.
2-3: Top up the memcache for the permission and dirty-logging faults
that force stage-2 block transitions.
4-6: Adjacent pkvm_pgtable_*() fixes: FWB flush early-out, drop a
spurious map WARN, and gate the eager-split capability.
7: Selftest for the block-collapse and block-split transitions.

Testing: the selftest in patch 7 covers the two dirty-logging block transitions (page->block collapse and block->page split) that patch 2 stages; on the base kernel the collapse oopses the host in pkvm_pgtable_stage2_map() with a NULL dereference under mmu_lock, and with the series applied it passes. It is a standalone test rather than an extension of kvm_page_table_test, whose default anonymous-4K backing forms no huge-page blocks, so an automated run never exercises these transitions, and whose worker/stage harness does not fit the multi-stage logging sequence. Run it with 2M hugepages
reserved and, for the split half, on a CPU with CTR_EL0.DIC == 0 (e.g. -cpu cortex-a710 under QEMU); it self-skips those otherwise. It also passes on a non-pKVM host (VHE and nVHE), where patch 3's generic change applies. The other fixes are not exercised by the selftest and rest on the analysis in their commit messages: patch 3's fault path is generic and non-pKVM (under pKVM patch 2 already tops it up, and its failure is a WARN_ON(!nobjs) in kvm_mmu_memory_cache_alloc(), not a KVM_RUN error), and patches 1 and 4-6 each need a specific pKVM configuration to hit.

Based on Linux v7.2-rc3 (a13c140cc289).

Cheers,
/fuad

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/

Bradley Morgan (2):
KVM: arm64: Skip cache maintenance for non-cacheable pKVM mappings
KVM: arm64: Top up stage-2 memcache for dirty logging faults

Fuad Tabba (5):
KVM: arm64: Top up the memcache for pKVM permission faults
KVM: arm64: Skip pKVM stage-2 flush when FWB is enabled
KVM: arm64: Don't WARN on pKVM stage-2 map failures
KVM: arm64: Don't advertise eager page splitting under pKVM
KVM: arm64: selftests: Add stage-2 block transition test

 arch/arm64/include/asm/kvm_pkvm.h             |   8 +-
 arch/arm64/kvm/mmu.c                          |   9 +-
 arch/arm64/kvm/pkvm.c                         |  21 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../kvm/arm64/stage2_block_transitions.c      | 226 ++++++++++++++++++
 5 files changed, 254 insertions(+), 11 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/arm64/stage2_block_transitions.c

base-commit: a13c140cc289c0b7b3770bce5b3ad42ab35074aa

Signature · 1 line
2.39.5
author reviewer
#1 lore raw

PATCH v5 1/7 KVM: arm64: Skip cache maintenance for non-cacheable pKVM mappings

From: Bradley Morgan <[email protected]>

The pKVM flush path walks its own pkvm_mappings list and cleans the data cache for every mapping, unlike the generic stage-2 walker it shadows, which skips non-cacheable leaves. Cleaning the cacheable alias of a non-cacheable mapping is pointless and can corrupt a device endpoint. Record whether a mapping is non-cacheable in spare bits of nr_pages and skip cache maintenance for it.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")
[tabba: use Marc's anonymous bitfield in place of the open-coded mask and helpers]
---
 arch/arm64/include/asm/kvm_pkvm.h |  5 ++++-
 arch/arm64/kvm/pkvm.c             | 15 +++++++++------
 2 files changed, 13 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_pkvm.h +4 -1
index 74fedd9c5ff0..57afb07d6b13 100644
@@ -195,7 +195,10 @@ struct pkvm_mapping {
 	struct rb_node node;
 	u64 gfn;
 	u64 pfn;
-	u64 nr_pages;
+	struct {
+		u64 nr_pages:48;
+		u64 nc:1;
+	};
 	u64 __subtree_last;	/* Internal member for interval tree */
 };
 
arch/arm64/kvm/pkvm.c +9 -6
index 053e4f733e4b..f70c601dcf4c 100644
@@ -369,7 +369,7 @@ static int __pkvm_pgtable_stage2_unshare(struct kvm_pgtable *pgt, u64 start, u64
 
 	for_each_mapping_in_range_safe(pgt, start, end, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			return ret;
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -473,6 +473,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 	mapping->gfn = gfn;
 	mapping->pfn = pfn;
 	mapping->nr_pages = size / PAGE_SIZE;
+	mapping->nc = !!(prot & (KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC));
 	pkvm_mapping_insert(mapping, &pgt->pkvm_mappings);
 
 	return ret;
@@ -503,7 +504,7 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			break;
 	}
@@ -517,9 +518,11 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
-	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
-		__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
-					  PAGE_SIZE * mapping->nr_pages);
+	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
+		if (!mapping->nc)
+			__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
+						  PAGE_SIZE * mapping->nr_pages);
+	}
 
 	return 0;
 }
@@ -537,7 +540,7 @@ bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
 		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest, handle, mapping->gfn,
-					   mapping->nr_pages, mkold);
+					   (u64)mapping->nr_pages, mkold);
 
 	return young;
 }
Signature · 1 line
2.39.5
Reviewed-by on v5 1/7
#2 lore raw

Re: [PATCH v5 1/7] KVM: arm64: Skip cache maintenance for non-cacheable pKVM mappings

Quoted 9 lines · Fuad Tabba

From: Bradley Morgan <[email protected]>

The pKVM flush path walks its own pkvm_mappings list and cleans the data cache for every mapping, unlike the generic stage-2 walker it shadows, which skips non-cacheable leaves. Cleaning the cacheable alias of a non-cacheable mapping is pointless and can corrupt a device endpoint. Record whether a mapping is non-cacheable in spare bits of nr_pages and skip cache maintenance for it.

Quoted 5 lines
Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")
[tabba: use Marc's anonymous bitfield in place of the open-coded mask and helpers]
Quoted 78 lines
---
 arch/arm64/include/asm/kvm_pkvm.h |  5 ++++-
 arch/arm64/kvm/pkvm.c             | 15 +++++++++------
 2 files changed, 13 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_pkvm.h +4 -1
index 74fedd9c5ff0..57afb07d6b13 100644
@@ -195,7 +195,10 @@ struct pkvm_mapping {
 	struct rb_node node;
 	u64 gfn;
 	u64 pfn;
-	u64 nr_pages;
+	struct {
+		u64 nr_pages:48;
+		u64 nc:1;
+	};
 	u64 __subtree_last;	/* Internal member for interval tree */
 };
 
arch/arm64/kvm/pkvm.c +9 -6
index 053e4f733e4b..f70c601dcf4c 100644
@@ -369,7 +369,7 @@ static int __pkvm_pgtable_stage2_unshare(struct kvm_pgtable *pgt, u64 start, u64
 
 	for_each_mapping_in_range_safe(pgt, start, end, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			return ret;
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -473,6 +473,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 	mapping->gfn = gfn;
 	mapping->pfn = pfn;
 	mapping->nr_pages = size / PAGE_SIZE;
+	mapping->nc = !!(prot & (KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC));
 	pkvm_mapping_insert(mapping, &pgt->pkvm_mappings);
 
 	return ret;
@@ -503,7 +504,7 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			break;
 	}
@@ -517,9 +518,11 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
-	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
-		__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
-					  PAGE_SIZE * mapping->nr_pages);
+	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
+		if (!mapping->nc)
+			__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
+						  PAGE_SIZE * mapping->nr_pages);
+	}
 
 	return 0;
 }
@@ -537,7 +540,7 @@ bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
 		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest, handle, mapping->gfn,
-					   mapping->nr_pages, mkold);
+					   (u64)mapping->nr_pages, mkold);
 
 	return young;
 }
Signature · 1 line
2.39.5
Signature · 2 lines
-- 
Vincent
author reviewer
#3 lore raw

PATCH v5 2/7 KVM: arm64: Top up the memcache for pKVM permission faults

In reply to Fuad Tabba

A permission fault normally only relaxes a leaf, so user_mem_abort() skips the memcache top-up. Under pKVM such a fault can instead collapse pages into a block. That needs a fresh pkvm_mapping object, and without it cache->mapping is NULL, so pkvm_pgtable_stage2_map() dereferences NULL and faults the host under mmu_lock. Staging only the object is not
enough: the hypervisor requires kvm_mmu_cache_min_pages in the memcache even for the allocation-free install, so under memcache pressure the collapse returns -ENOMEM and trips the WARN_ON(ret) in pkvm_pgtable_stage2_map() where a non-pKVM guest succeeds.

Top up the full memcache for pKVM permission faults so both the mapping object and the min-pages are staged before mmu_lock.

Fixes db14091d8f75 ("KVM: arm64: Stage-2 huge mappings for np-guests")
---
 arch/arm64/kvm/mmu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/arm64/kvm/mmu.c +5 -1
index 6c941aaa10c6..4d7c9bdcef00 100644
@@ -2114,10 +2114,14 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd)
 	 * and so normally don't require allocations from the memcache. The
 	 * only exception to this is when dirty logging is enabled at runtime
 	 * and a write fault needs to collapse a block entry into a table.
+	 * Under pKVM a permission fault can also collapse pages into a block,
+	 * which needs a fresh mapping object, and the hypervisor requires the
+	 * min-pages memcache even when the install allocates nothing.
 	 */
 	memcache = get_mmu_memcache(s2fd->vcpu);
 	if (!perm_fault || (memslot_is_logging(s2fd->memslot) &&
-			    kvm_is_write_fault(s2fd->vcpu))) {
+			    kvm_is_write_fault(s2fd->vcpu)) ||
+	    is_protected_kvm_enabled()) {
 		ret = topup_mmu_memcache(s2fd->vcpu, memcache);
 		if (ret)
 			return ret;
Signature · 1 line
2.39.5
author reviewer
#4 lore raw

PATCH v5 3/7 KVM: arm64: Top up stage-2 memcache for dirty logging faults

In reply to Fuad Tabba

From: Bradley Morgan <[email protected]>

Dirty logging forces new stage-2 mappings to page size but does not always split an existing block first (eager splitting is best effort and off by default). A non-write permission fault on such a block, an instruction fetch, still needs a page-table page to split it, but the top-up is gated on write faults.

With the cache empty, kvm_mmu_memory_cache_alloc() hits its guest-triggerable WARN_ON(!nobjs) and falls back to a GFP_ATOMIC allocation under mmu_lock, with a BUG_ON() if that fails.

Top up the memcache for any permission fault while dirty logging is active.

Fixes 6f745f1bb5bf ("KVM: arm64: Convert user_mem_abort() to generic page-table API")
[tabba: reword the commit message for the generic, non-pKVM failure mode]
---
 arch/arm64/kvm/mmu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +2 -3
index 4d7c9bdcef00..74e7e7f7564c 100644
@@ -2113,14 +2113,13 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd)
 	 * Permission faults just need to update the existing leaf entry,
 	 * and so normally don't require allocations from the memcache. The
 	 * only exception to this is when dirty logging is enabled at runtime
-	 * and a write fault needs to collapse a block entry into a table.
+	 * and a fault needs to collapse a block entry into a table.
 	 * Under pKVM a permission fault can also collapse pages into a block,
 	 * which needs a fresh mapping object, and the hypervisor requires the
 	 * min-pages memcache even when the install allocates nothing.
 	 */
 	memcache = get_mmu_memcache(s2fd->vcpu);
-	if (!perm_fault || (memslot_is_logging(s2fd->memslot) &&
-			    kvm_is_write_fault(s2fd->vcpu)) ||
+	if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
 	    is_protected_kvm_enabled()) {
 		ret = topup_mmu_memcache(s2fd->vcpu, memcache);
 		if (ret)
Signature · 1 line
2.39.5
#5 lore raw

Re: [PATCH v5 3/7] KVM: arm64: Top up stage-2 memcache for dirty logging faults

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] A malicious nested (L1) guest can crash the KVM host via a Break-Before-Make (BBM) violation that triggers a BUG_ON() due to an empty memory cache during a permission fault. --

--- Patch [3]: [PATCH v5 3/7] KVM: arm64: Top up stage-2 memcache for dirty logging faults --- commit bac5488834df4c0a16b08356c8263e8eb67e0fe4
Author: Bradley Morgan <[email protected]>

KVM: arm64: Top up stage-2 memcache for dirty logging faults

Dirty logging forces new stage-2 mappings to page size but does not always split an existing block first. A non-write permission fault on such a block still needs a page-table page to split it, but the top-up was previously gated on write faults. This patch tops up the memcache for any permission fault while dirty logging is active.

Quoted 9 lines
arch/arm64/kvm/mmu.c +0 -1
index 4d7c9bdcef004..74e7e7f7564c8 100644
@@ -2113,14 +2113,13 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd)
 	 * Permission faults just need to update the existing leaf entry,
 	 * and so normally don't require allocations from the memcache. The
 	 * only exception to this is when dirty logging is enabled at runtime
-	 * and a write fault needs to collapse a block entry into a table.

arch/arm64/kvm/mmu.c static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd) · line 2123

Quoted 10 lines
arch/arm64/kvm/mmu.c +2 -2
+	 * and a fault needs to collapse a block entry into a table.
 	 * Under pKVM a permission fault can also collapse pages into a block,
 	 * which needs a fresh mapping object, and the hypervisor requires the
 	 * min-pages memcache even when the install allocates nothing.
 	 */
 	memcache = get_mmu_memcache(s2fd->vcpu);
-	if (!perm_fault || (memslot_is_logging(s2fd->memslot) &&
-			    kvm_is_write_fault(s2fd->vcpu)) ||
+	if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
 	    is_protected_kvm_enabled()) {

[Severity: High]
This is a pre-existing issue, but does skipping the memcache top-up here leave the host vulnerable to a crash from a malicious nested L1 guest?

If a nested L1 guest establishes a 2MB stage-2 block mapping, and then maliciously overwrites it with a 4KB page table descriptor without issuing a TLBI (an architectural Break-Before-Make violation), an L2 guest can trigger a permission fault on the stale 2MB shadow mapping.

Since KVM optimizes permission faults by assuming they only relax permissions, the memcache top-up is skipped here.

However, in kvm_s2_fault_map(), if the mapping size and fault granule don't match:

arch/arm64/kvm/mmu.c:kvm_s2_fault_map()

    ...
	} else {
		ret = KVM_PGT_FN(kvm_pgtable_stage2_map)(pgt, gfn_to_gpa(gfn), mapping_size,
							 __pfn_to_phys(pfn), prot,
							 memcache, flags);
	}
    ...

kvm_pgtable_stage2_map() will attempt to split the block, which requires a new page table allocation. Because the cache wasn't topped up, kvm_mmu_memory_cache_alloc() will hit WARN_ON(!mc->nobjs) and fall back to a GFP_ATOMIC allocation. If this atomic allocation fails under memory pressure, it hits BUG_ON(!p) and crashes the host.

Quoted 2 lines
arch/arm64/kvm/mmu.c +0 -0
 		ret = topup_mmu_memcache(s2fd->vcpu, memcache);
 		if (ret)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author reviewer
#6 lore raw

Re: [PATCH v5 3/7] KVM: arm64: Top up stage-2 memcache for dirty logging faults

Quoted 5 lines · On Fri, 17 Jul 2026 at 14:15, <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] A malicious nested (L1) guest can crash the KVM host via a Break-Before-Make (BBM) violation that triggers a BUG_ON() due to an empty memory cache during a permission fault.

From my understanding of NV code, this looks like a real issue. I don't think it changes this patch, though: this one only widens the top-up for the dirty-logging case, and the path you describe is a non-logging permission fault. So, it would be a separate fix, a top up for nested permission faults too, the same way we already do for pKVM?

        if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
            is_protected_kvm_enabled() || s2fd->nested) {

If that's right, I'll send a separate fix later. Marc, what do you think?

/fuad

/fuad

Quoted 72 lines

--

--- Patch [3]: [PATCH v5 3/7] KVM: arm64: Top up stage-2 memcache for dirty logging faults --- commit bac5488834df4c0a16b08356c8263e8eb67e0fe4
Author: Bradley Morgan <[email protected]>

KVM: arm64: Top up stage-2 memcache for dirty logging faults

Dirty logging forces new stage-2 mappings to page size but does not always split an existing block first. A non-write permission fault on such a block still needs a page-table page to split it, but the top-up was previously gated on write faults. This patch tops up the memcache for any permission fault while dirty logging is active.

Quoted 9 lines
arch/arm64/kvm/mmu.c +0 -1
index 4d7c9bdcef004..74e7e7f7564c8 100644
@@ -2113,14 +2113,13 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd)
       * Permission faults just need to update the existing leaf entry,
       * and so normally don't require allocations from the memcache. The
       * only exception to this is when dirty logging is enabled at runtime
-      * and a write fault needs to collapse a block entry into a table.

arch/arm64/kvm/mmu.c static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd) · line 2123

Quoted 10 lines
arch/arm64/kvm/mmu.c +2 -2
+      * and a fault needs to collapse a block entry into a table.
       * Under pKVM a permission fault can also collapse pages into a block,
       * which needs a fresh mapping object, and the hypervisor requires the
       * min-pages memcache even when the install allocates nothing.
       */
      memcache = get_mmu_memcache(s2fd->vcpu);
-     if (!perm_fault || (memslot_is_logging(s2fd->memslot) &&
-                         kvm_is_write_fault(s2fd->vcpu)) ||
+     if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
          is_protected_kvm_enabled()) {

[Severity: High]
This is a pre-existing issue, but does skipping the memcache top-up here leave the host vulnerable to a crash from a malicious nested L1 guest?

If a nested L1 guest establishes a 2MB stage-2 block mapping, and then maliciously overwrites it with a 4KB page table descriptor without issuing a TLBI (an architectural Break-Before-Make violation), an L2 guest can trigger a permission fault on the stale 2MB shadow mapping.

Since KVM optimizes permission faults by assuming they only relax permissions, the memcache top-up is skipped here.

However, in kvm_s2_fault_map(), if the mapping size and fault granule don't match:

arch/arm64/kvm/mmu.c:kvm_s2_fault_map()

    ...
        } else {
                ret = KVM_PGT_FN(kvm_pgtable_stage2_map)(pgt, gfn_to_gpa(gfn), mapping_size,
                                                         __pfn_to_phys(pfn), prot,
                                                         memcache, flags);
        }
    ...

kvm_pgtable_stage2_map() will attempt to split the block, which requires a new page table allocation. Because the cache wasn't topped up, kvm_mmu_memory_cache_alloc() will hit WARN_ON(!mc->nobjs) and fall back to a GFP_ATOMIC allocation. If this atomic allocation fails under memory pressure, it hits BUG_ON(!p) and crashes the host.

arch/arm64/kvm/mmu.c static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd) · line 2125

Quoted 2 lines
arch/arm64/kvm/mmu.c +0 -0
              ret = topup_mmu_memcache(s2fd->vcpu, memcache);
              if (ret)

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

maintainer
#7 lore raw

Re: [PATCH v5 3/7] KVM: arm64: Top up stage-2 memcache for dirty logging faults

Quoted 14 lines · Fuad Tabba
Quoted 5 lines · On Fri, 17 Jul 2026 at 14:15, <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] A malicious nested (L1) guest can crash the KVM host via a Break-Before-Make (BBM) violation that triggers a BUG_ON() due to an empty memory cache during a permission fault.

From my understanding of NV code, this looks like a real issue. I don't think it changes this patch, though: this one only widens the top-up for the dirty-logging case, and the path you describe is a non-logging permission fault. So, it would be a separate fix, a top up for nested permission faults too, the same way we already do for pKVM?

Quoted 4 lines
        if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
            is_protected_kvm_enabled() || s2fd->nested) {

If that's right, I'll send a separate fix later. Marc, what do you think?

I don't think this is correct. Yes, this papers over the guest being buggy, but i don't think that's what we should really do.

Doing the same thing (changing the output size without a TLBI) on real HW would result in a permission fault being signalled to EL2, and we should model our SW MMU the same way.

I think the latent bug here is the way we always walk L1's S2 on S2 fault, irrespective of the fault type, and that feels wrong. This is ignoring the fact that we already have a TLB (the shadow S2) for this mapping, and rewalk anyway. Since we now find valid permissions, we take it at face value and try to install this new translation (which could point to a different OA, and even bigger problem).

So ideally we'd simply tell the guest to bugger off, but we need to solve a few problems first:

- some permission faults are caused by the host rather than the guest (dirty logging, HAFDBS), and we need to treat those specially.

- we need to rebuild an ESR based on the content of our TLB, not L1's S2, which is not always easy to obtain (we have some limited TTL caching in the shadow S2, but that's not always reliable).

All of this strongly intersects with Wei-Lin's reverse map, and Oliver's HAFDBS support.

Oliver?

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
maintainer
#8 lore raw

Re: [PATCH v5 3/7] KVM: arm64: Top up stage-2 memcache for dirty logging faults

Hey,

Sorry for the latency, stumbled upon this as I was looking for stuff to grab for 7.3...

Quoted 19 lines · Marc Zyngier
Quoted 17 lines · Fuad Tabba
Quoted 5 lines · On Fri, 17 Jul 2026 at 14:15, <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] A malicious nested (L1) guest can crash the KVM host via a Break-Before-Make (BBM) violation that triggers a BUG_ON() due to an empty memory cache during a permission fault.

From my understanding of NV code, this looks like a real issue. I don't think it changes this patch, though: this one only widens the top-up for the dirty-logging case, and the path you describe is a non-logging permission fault. So, it would be a separate fix, a top up for nested permission faults too, the same way we already do for pKVM?

        if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
            is_protected_kvm_enabled() || s2fd->nested) {
Quoted 8 lines
Quoted 1 line

If that's right, I'll send a separate fix later. Marc, what do you think?

I don't think this is correct. Yes, this papers over the guest being buggy, but i don't think that's what we should really do.

Doing the same thing (changing the output size without a TLBI) on real HW would result in a permission fault being signalled to EL2, and we should model our SW MMU the same way.

FEAT_BBML3 shifts responsibility onto the implementation to reconcile multihit due to mapping granularity changes. A hypervisor could choose to do eager page splitting at the beginning of dirty logging and elide TLBIs.

Quoted 13 lines

I think the latent bug here is the way we always walk L1's S2 on S2 fault, irrespective of the fault type, and that feels wrong. This is ignoring the fact that we already have a TLB (the shadow S2) for this mapping, and rewalk anyway. Since we now find valid permissions, we take it at face value and try to install this new translation (which could point to a different OA, and even bigger problem).

So ideally we'd simply tell the guest to bugger off, but we need to solve a few problems first:

- some permission faults are caused by the host rather than the guest (dirty logging, HAFDBS), and we need to treat those specially.

Quoted 8 lines

- we need to rebuild an ESR based on the content of our TLB, not L1's S2, which is not always easy to obtain (we have some limited TTL caching in the shadow S2, but that's not always reliable).

All of this strongly intersects with Wei-Lin's reverse map, and Oliver's HAFDBS support.

Oliver?

Hmm, I'm more of the mind that we should just keep the memcache topped up for any fault because clearly the prediction has gotten out of sync with what happens later down the line. We're bound to screw it up again.

Generating faults solely based on the TLB seems to be at odds with supporting FEAT_ETS* in the shadow stage-2 and practically means we need to re-walk for most stage-2 aborts anyway. And as you point out, I need to do this for HAFDBS dirty state updates.

BBML3 and ETS2/3 aren't terribly high priority but I'd rather not paper over a rather straightforward KVM bug by making design decisions that limit our feature set for nested.

Thanks,
Oliver

maintainer
#9 lore raw

Re: [PATCH v5 3/7] KVM: arm64: Top up stage-2 memcache for dirty logging faults

Quoted 31 lines · Oliver Upton

Hey,

Sorry for the latency, stumbled upon this as I was looking for stuff to grab for 7.3...

Quoted 24 lines · Marc Zyngier
Quoted 14 lines · Fuad Tabba
Quoted 5 lines · On Fri, 17 Jul 2026 at 14:15, <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] A malicious nested (L1) guest can crash the KVM host via a Break-Before-Make (BBM) violation that triggers a BUG_ON() due to an empty memory cache during a permission fault.

From my understanding of NV code, this looks like a real issue. I don't think it changes this patch, though: this one only widens the top-up for the dirty-logging case, and the path you describe is a non-logging permission fault. So, it would be a separate fix, a top up for nested permission faults too, the same way we already do for pKVM?

Quoted 4 lines
        if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
            is_protected_kvm_enabled() || s2fd->nested) {

If that's right, I'll send a separate fix later. Marc, what do you think?

I don't think this is correct. Yes, this papers over the guest being buggy, but i don't think that's what we should really do.

Quoted 8 lines
Quoted 3 lines

Doing the same thing (changing the output size without a TLBI) on real HW would result in a permission fault being signalled to EL2, and we should model our SW MMU the same way.

FEAT_BBML3 shifts responsibility onto the implementation to reconcile multihit due to mapping granularity changes. A hypervisor could choose to do eager page splitting at the beginning of dirty logging and elide TLBIs.

Sure. We just don't advertise BBML3. and I'm not even sure we always can emulate it.

Quoted 27 lines
Quoted 14 lines

I think the latent bug here is the way we always walk L1's S2 on S2 fault, irrespective of the fault type, and that feels wrong. This is ignoring the fact that we already have a TLB (the shadow S2) for this mapping, and rewalk anyway. Since we now find valid permissions, we take it at face value and try to install this new translation (which could point to a different OA, and even bigger problem).

So ideally we'd simply tell the guest to bugger off, but we need to solve a few problems first:

- some permission faults are caused by the host rather than the guest (dirty logging, HAFDBS), and we need to treat those specially.

Quoted 8 lines

- we need to rebuild an ESR based on the content of our TLB, not L1's S2, which is not always easy to obtain (we have some limited TTL caching in the shadow S2, but that's not always reliable).

All of this strongly intersects with Wei-Lin's reverse map, and Oliver's HAFDBS support.

Oliver?

Hmm, I'm more of the mind that we should just keep the memcache topped up for any fault because clearly the prediction has gotten out of sync with what happens later down the line. We're bound to screw it up again.

Quoted 8 lines

Generating faults solely based on the TLB seems to be at odds with supporting FEAT_ETS* in the shadow stage-2 and practically means we need to re-walk for most stage-2 aborts anyway. And as you point out, I need to do this for HAFDBS dirty state updates.

BBML3 and ETS2/3 aren't terribly high priority but I'd rather not paper over a rather straightforward KVM bug by making design decisions that limit our feature set for nested.

My problem here is not the top-up of the memcache, but the fact that the OA may have changed in L1's S2. This potentially result in an inconsistent S2, and in the future a wonky reverse map. So I'm not advocating we paper over anything. It's exactly the opposite.

As for future features, I don't think they should take priority over correctness, and taking the guest S2 at face value is incorrect.

If you want to hide this from the guest, fine. But that probably means that we need to detect this situation, and nuke part (or all) of the shadow S2 as a consequence. Today, we're just going to mess things up.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#10 lore raw

Re: [PATCH v5 3/7] KVM: arm64: Top up stage-2 memcache for dirty logging faults

Quoted 7 lines · Marc Zyngier
Quoted 4 lines

FEAT_BBML3 shifts responsibility onto the implementation to reconcile multihit due to mapping granularity changes. A hypervisor could choose to do eager page splitting at the beginning of dirty logging and elide TLBIs.

Sure. We just don't advertise BBML3. and I'm not even sure we always can emulate it.

Well, we'd at minimum need supporting hardware since the feature must also exist at stage-1.

Quoted 32 lines
Quoted 32 lines
Quoted 14 lines

I think the latent bug here is the way we always walk L1's S2 on S2 fault, irrespective of the fault type, and that feels wrong. This is ignoring the fact that we already have a TLB (the shadow S2) for this mapping, and rewalk anyway. Since we now find valid permissions, we take it at face value and try to install this new translation (which could point to a different OA, and even bigger problem).

So ideally we'd simply tell the guest to bugger off, but we need to solve a few problems first:

- some permission faults are caused by the host rather than the guest (dirty logging, HAFDBS), and we need to treat those specially.

Quoted 8 lines

- we need to rebuild an ESR based on the content of our TLB, not L1's S2, which is not always easy to obtain (we have some limited TTL caching in the shadow S2, but that's not always reliable).

All of this strongly intersects with Wei-Lin's reverse map, and Oliver's HAFDBS support.

Oliver?

Hmm, I'm more of the mind that we should just keep the memcache topped up for any fault because clearly the prediction has gotten out of sync with what happens later down the line. We're bound to screw it up again.

Generating faults solely based on the TLB seems to be at odds with supporting FEAT_ETS* in the shadow stage-2 and practically means we need to re-walk for most stage-2 aborts anyway. And as you point out, I need to do this for HAFDBS dirty state updates.

Quoted 8 lines
Quoted 3 lines

BBML3 and ETS2/3 aren't terribly high priority but I'd rather not paper over a rather straightforward KVM bug by making design decisions that limit our feature set for nested.

My problem here is not the top-up of the memcache, but the fact that the OA may have changed in L1's S2. This potentially result in an inconsistent S2, and in the future a wonky reverse map. So I'm not advocating we paper over anything. It's exactly the opposite.

AIUI the bug is that a change in mapping granularity (but not necessarily OA) can cause a shadow stage-2 permission fault to remap at a finer granularity and allocate table pages. This seems like something we will eventually have to deal with for BBML3.

OTOH, I completely agree with you about changes to the OA. We should synthesize a TLB conflict abort and let the L1 clean up the mess.

I was actually thinking about hardening the stage-2 map path, rejecting valid->valid conversions that result in a change of the OA. We'll want to have that anyway as Mostafa is adding support for BBML3, that error could be propagated as a TLB conflict abort to the L1.

Thanks,
Oliver

author reviewer
#11 lore raw

PATCH v5 4/7 KVM: arm64: Skip pKVM stage-2 flush when FWB is enabled

In reply to Fuad Tabba

pkvm_pgtable_stage2_flush() cleans the D-cache for every mapping in the range even on hardware with stage-2 Force Write-Back, where FWB keeps guest memory coherent to the PoC and the maintenance is unnecessary. The generic kvm_pgtable_stage2_flush() returns early in that case, but the pKVM MMU does not, so it needlessly cleans the whole range on, e.g., every set/way trap.

Return early when FWB is enabled, matching the generic walker.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")
---
 arch/arm64/kvm/pkvm.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/kvm/pkvm.c +4 -0
index f70c601dcf4c..ba6570e37545 100644
@@ -518,6 +518,10 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
+
+	if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
+		return 0;
+
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		if (!mapping->nc)
 			__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
Signature · 1 line
2.39.5
Reviewed-by on v5 4/7
#12 lore raw

Re: [PATCH v5 4/7] KVM: arm64: Skip pKVM stage-2 flush when FWB is enabled

Quoted 11 lines · Fuad Tabba

pkvm_pgtable_stage2_flush() cleans the D-cache for every mapping in the range even on hardware with stage-2 Force Write-Back, where FWB keeps guest memory coherent to the PoC and the maintenance is unnecessary. The generic kvm_pgtable_stage2_flush() returns early in that case, but the pKVM MMU does not, so it needlessly cleans the whole range on, e.g., every set/way trap.

Return early when FWB is enabled, matching the generic walker.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

LGTM,

Quoted 10 lines

---
arch/arm64/kvm/pkvm.c | 4 ++++

1 file changed, 4 insertions(+)
arch/arm64/kvm/pkvm.c +0 -0
index f70c601dcf4c..ba6570e37545 100644

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size) · line 527

Quoted 10 lines
arch/arm64/kvm/pkvm.c +4 -0
@@ -518,6 +518,10 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
+
+	if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
+		return 0;
+
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		if (!mapping->nc)
 			__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),

Thanks!

author reviewer
#13 lore raw

PATCH v5 5/7 KVM: arm64: Don't WARN on pKVM stage-2 map failures

In reply to Fuad Tabba

pkvm_pgtable_stage2_map() wraps the __pkvm_host_share_guest() and __pkvm_host_donate_guest() return in WARN_ON(), but those hypercalls fail for reasons that are not EL1 invariant violations: -EINVAL for a pfn that is not memblock RAM (check_range_allowed_memory() rejects a device page mapped into a non-protected guest) and -ENOMEM under memcache pressure. Both are reachable from a guest fault, so the WARN splats on host input.

Return the error without warning. The unshare and write-protect WARNs stay, since a failure there does signal a broken EL1 invariant.

Fixes 3669ddd8fa8b5 ("KVM: arm64: Add a range to pkvm_mappings")
---
 arch/arm64/kvm/pkvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/pkvm.c +1 -1
index ba6570e37545..1212a2c5613d 100644
@@ -466,7 +466,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 					size / PAGE_SIZE, prot);
 	}
 
-	if (WARN_ON(ret))
+	if (ret)
 		return ret;
 
 	swap(mapping, cache->mapping);
Signature · 1 line
2.39.5
Reviewed-by on v5 5/7
#14 lore raw

Re: [PATCH v5 5/7] KVM: arm64: Don't WARN on pKVM stage-2 map failures

Quoted 8 lines · Fuad Tabba

pkvm_pgtable_stage2_map() wraps the __pkvm_host_share_guest() and __pkvm_host_donate_guest() return in WARN_ON(), but those hypercalls fail for reasons that are not EL1 invariant violations: -EINVAL for a pfn that is not memblock RAM (check_range_allowed_memory() rejects a device page mapped into a non-protected guest) and -ENOMEM under memcache pressure. Both are reachable from a guest fault, so the WARN splats on host input.

Quoted 5 lines

Return the error without warning. The unshare and write-protect WARNs stay, since a failure there does signal a broken EL1 invariant.

Fixes 3669ddd8fa8b5 ("KVM: arm64: Add a range to pkvm_mappings")

LGTM,

Quoted 10 lines

---
arch/arm64/kvm/pkvm.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/pkvm.c +0 -0
index ba6570e37545..1212a2c5613d 100644

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, · line 472

Quoted 8 lines
arch/arm64/kvm/pkvm.c +1 -1
@@ -466,7 +466,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 					size / PAGE_SIZE, prot);
 	}
 
-	if (WARN_ON(ret))
+	if (ret)
 		return ret;
 
 	swap(mapping, cache->mapping);

Thanks!

author reviewer
#15 lore raw

PATCH v5 6/7 KVM: arm64: Don't advertise eager page splitting under pKVM

In reply to Fuad Tabba

Under pKVM the stage-2 walker resolves to pkvm_pgtable_stage2_split(), a WARN_ON_ONCE(1) stub, yet KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE is still enabled and reported for non-protected guests: the capability check keys on the per-VM protected state while the walker dispatch keys on the host-global mode. Enabling the cap and then dirty-logging the guest reaches the stub, splatting a userspace-reachable WARN.

Reject the capability, and stop reporting a chunk size and the supported block sizes, for every VM once pKVM is enabled, keyed on the host-global mode like the split dispatch. Gating only protected VMs would leave the non-protected guests that reach the stub still able to enable it. Userspace decides whether eager splitting is available from the block-size bitmap (QEMU falls back to no eager splitting when it reads 0), so leaving it advertised steers an explicit request into the enable failure instead of the fallback.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")
---
 arch/arm64/include/asm/kvm_pkvm.h | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/include/asm/kvm_pkvm.h +3 -0
index 57afb07d6b13..beea00e693a0 100644
@@ -45,6 +45,9 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)
 		return true;
 	case KVM_CAP_ARM_MTE:
 		return false;
+	case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
+	case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
+		return false;
 	default:
 		return !kvm || !kvm_vm_is_protected(kvm);
 	}
Signature · 1 line
2.39.5
#16 lore raw

Re: [PATCH v5 6/7] KVM: arm64: Don't advertise eager page splitting under pKVM

Quoted 16 lines · Fuad Tabba

Under pKVM the stage-2 walker resolves to pkvm_pgtable_stage2_split(), a WARN_ON_ONCE(1) stub, yet KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE is still enabled and reported for non-protected guests: the capability check keys on the per-VM protected state while the walker dispatch keys on the host-global mode. Enabling the cap and then dirty-logging the guest reaches the stub, splatting a userspace-reachable WARN.

Reject the capability, and stop reporting a chunk size and the supported block sizes, for every VM once pKVM is enabled, keyed on the host-global mode like the split dispatch. Gating only protected VMs would leave the non-protected guests that reach the stub still able to enable it. Userspace decides whether eager splitting is available from the block-size bitmap (QEMU falls back to no eager splitting when it reads 0), so leaving it advertised steers an explicit request into the enable failure instead of the fallback.

Quoted 2 lines
Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

Do you think CC stable would be good? I do but sometimes I'm embarrassingly wrong

Quoted 9 lines

---
arch/arm64/include/asm/kvm_pkvm.h | 3 +++

1 file changed, 3 insertions(+)
arch/arm64/include/asm/kvm_pkvm.h +0 -0
index 57afb07d6b13..beea00e693a0 100644

arch/arm64/include/asm/kvm_pkvm.h static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext) · line 50

Quoted 6 lines
arch/arm64/include/asm/kvm_pkvm.h +3 -0
@@ -45,6 +45,9 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)
 		return true;
 	case KVM_CAP_ARM_MTE:
 		return false;
+	case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
+	case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
+		return false;

Nit: KVM_CAP_ARM_MTE right above already returns false, fold the two new cases into that and drop the duplicate return. And please put a small comment saying eager splitting is unimplemented under pKVM (WARN stub), so nobody reads the list later and "fixes" it back, here's a example, feel free to bikeshed

	case KVM_CAP_ARM_MTE:
	/* eager splitting is unimplemented in pKVM, please don't refix this */
	case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
	case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
		return false;
	default:
		return !kvm || !kvm_vm_is_protected(kvm);
	}

Killing the reporting too is right imo. Qemu reads a 0 block size bitmap and quietly falls back, an explicit enable eating EINVAL is the worse failure mode. Good.

if u choose not to, it's ok, please add

Reviewed-by: Bradley Morgan
<[email protected]>

arch/arm64/include/asm/kvm_pkvm.h static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext) · line 53

Quoted 4 lines
arch/arm64/include/asm/kvm_pkvm.h +0 -0
 	default:
 		return !kvm || !kvm_vm_is_protected(kvm);
 	}

Thanks!

author reviewer
#17 lore raw

Re: [PATCH v5 6/7] KVM: arm64: Don't advertise eager page splitting under pKVM

Hi Bradley,

Quoted 19 lines · Bradley Morgan
Quoted 16 lines · Fuad Tabba

Under pKVM the stage-2 walker resolves to pkvm_pgtable_stage2_split(), a WARN_ON_ONCE(1) stub, yet KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE is still enabled and reported for non-protected guests: the capability check keys on the per-VM protected state while the walker dispatch keys on the host-global mode. Enabling the cap and then dirty-logging the guest reaches the stub, splatting a userspace-reachable WARN.

Reject the capability, and stop reporting a chunk size and the supported block sizes, for every VM once pKVM is enabled, keyed on the host-global mode like the split dispatch. Gating only protected VMs would leave the non-protected guests that reach the stub still able to enable it. Userspace decides whether eager splitting is available from the block-size bitmap (QEMU falls back to no eager splitting when it reads 0), so leaving it advertised steers an explicit request into the enable failure instead of the fallback.

Quoted 5 lines
Quoted 2 lines
Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

Do you think CC stable would be good? I do but sometimes I'm embarrassingly wrong

I'd lean against it here. The "Fixes" commit has been in mainline since v6.14, the symptom is a WARN rather than a crash or data corruption.

Quoted 15 lines
Quoted 15 lines

---
arch/arm64/include/asm/kvm_pkvm.h | 3 +++

1 file changed, 3 insertions(+)
arch/arm64/include/asm/kvm_pkvm.h +2 -0
index 57afb07d6b13..beea00e693a0 100644
@@ -45,6 +45,9 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)
              return true;
      case KVM_CAP_ARM_MTE:
              return false;
+      case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
+      case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
Quoted 8 lines

arch/arm64/include/asm/kvm_pkvm.h static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext) · line 50

Quoted 1 line
arch/arm64/include/asm/kvm_pkvm.h +1 -0
+              return false;

Nit: KVM_CAP_ARM_MTE right above already returns false, fold the two new cases into that and drop the duplicate return. And please put a small comment saying eager splitting is unimplemented under pKVM (WARN stub), so nobody reads the list later and "fixes" it back, here's a example, feel free to bikeshed

I'd prefer to keep the separate return. The grouping is deliberate:
MTE and eager splitting are unrelated features, and the two eager splitting caps belong together. The function is self-explanatory from its name and header comment, so I don't think an inline comment is needed.

Thanks for the reviews!
/fuad

Quoted 30 lines

        case KVM_CAP_ARM_MTE:
        /* eager splitting is unimplemented in pKVM, please don't refix this */
        case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
        case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
                return false;
        default:
                return !kvm || !kvm_vm_is_protected(kvm);
        }

Killing the reporting too is right imo. Qemu reads a 0 block size bitmap and quietly falls back, an explicit enable eating EINVAL is the worse failure mode. Good.

if u choose not to, it's ok, please add

Reviewed-by: Bradley Morgan
<[email protected]>

arch/arm64/include/asm/kvm_pkvm.h static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext) · line 53

Quoted 4 lines
arch/arm64/include/asm/kvm_pkvm.h +0 -0
      default:
              return !kvm || !kvm_vm_is_protected(kvm);
      }

Thanks!

#18 lore raw

Re: [PATCH v5 6/7] KVM: arm64: Don't advertise eager page splitting under pKVM

Quoted 27 lines · Fuad Tabba

Hi Bradley,

Quoted 4 lines · Bradley Morgan
Quoted 1 line · Fuad Tabba

Under pKVM the stage-2 walker resolves to pkvm_pgtable_stage2_split(),

a

Quoted 2 lines
Quoted 2 lines

WARN_ON_ONCE(1) stub, yet KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE is still enabled and reported for non-protected guests: the capability check

keys

Quoted 16 lines
Quoted 15 lines

on the per-VM protected state while the walker dispatch keys on the host-global mode. Enabling the cap and then dirty-logging the guest reaches the stub, splatting a userspace-reachable WARN.

Reject the capability, and stop reporting a chunk size and the supported block sizes, for every VM once pKVM is enabled, keyed on the host-global mode like the split dispatch. Gating only protected VMs would leave the non-protected guests that reach the stub still able to enable it. Userspace decides whether eager splitting is available from the block-size bitmap (QEMU falls back to no eager splitting when it reads 0), so leaving it advertised steers an explicit request into the enable failure instead of the fallback.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")
Quoted 6 lines
Quoted 2 lines

Do you think CC stable would be good? I do but sometimes I'm embarrassingly wrong

I'd lean against it here. The "Fixes" commit has been in mainline since v6.14, the symptom is a WARN rather than a crash or data corruption.

Don't worry, I mean. It could be back ported to ACK, right? Or anything against it?

If you want I'll have to sign up for the gerrit and I'll do the backports

Quoted 27 lines
Quoted 6 lines

Review

Quoted 6 lines

---
arch/arm64/include/asm/kvm_pkvm.h | 3 +++

1 file changed, 3 insertions(+)
(quoted diff) +0 -0

b/arch/arm64/include/asm/kvm_pkvm.h

Quoted 4 lines

arch/arm64/include/asm/kvm_pkvm.h static inline bool kvm_pkvm_ext_allowed(struct kvm

Quoted 4 lines

index 57afb07d6b13..beea00e693a0 100644

arch/arm64/include/asm/kvm_pkvm.h +0 -0
@@ -45,6 +45,9 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm

*kvm, long ext)

Quoted 10 lines

arch/arm64/include/asm/kvm_pkvm.h static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext) · line 50

Quoted 6 lines
arch/arm64/include/asm/kvm_pkvm.h +3 -0
              return true;
      case KVM_CAP_ARM_MTE:
              return false;
+      case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
+      case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
+              return false;

Nit: KVM_CAP_ARM_MTE right above already returns false, fold the two new cases into that and drop the duplicate return. And please put a

small

Quoted 3 lines

comment saying eager splitting is unimplemented under pKVM (WARN stub), so nobody reads the list later and "fixes" it back, here's a example, feel free to bikeshed

Quoted 5 lines

I'd prefer to keep the separate return. The grouping is deliberate:
MTE and eager splitting are unrelated features, and the two eager splitting caps belong together. The function is self-explanatory from its name and header comment, so I don't think an inline comment is needed.

hmm. ok fair enough, this comment opinion is just my opinion anyway, not a blocker or anything.

Quoted 5 lines

Thanks for the reviews!
/fuad

Quoted 2 lines
Quoted 30 lines
Quoted 2 lines
        case KVM_CAP_ARM_MTE:
        /* eager splitting is unimplemented in pKVM, please don't refix

this */

Quoted 26 lines
        case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
        case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
                return false;
        default:
                return !kvm || !kvm_vm_is_protected(kvm);
        }

Killing the reporting too is right imo. Qemu reads a 0 block size bitmap and quietly falls back, an explicit enable eating EINVAL is the worse failure mode. Good.

if u choose not to, it's ok, please add

Reviewed-by: Bradley Morgan
<[email protected]>

arch/arm64/include/asm/kvm_pkvm.h static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext) · line 53

Quoted 4 lines
arch/arm64/include/asm/kvm_pkvm.h +0 -0
      default:
              return !kvm || !kvm_vm_is_protected(kvm);
      }

Thanks!

Thanks!

author reviewer
#19 lore raw

PATCH v5 7/7 KVM: arm64: selftests: Add stage-2 block transition test

In reply to Fuad Tabba

Add a test for the two stage-2 granularity changes dirty logging forces at fault time, asserting the guest completes with no KVM_RUN error. The first scenario collapses a page into a hugetlb-backed block: it writes under logging, re-write-protects the page via GET_DIRTY_LOG, then writes again with logging off. The second splits blocks: it faults in several non-executable 2M blocks, enables logging, then executes in each block so an execute permission fault splits it. It is skipped when CTR_EL0.DIC is set, since mappings are then executable and no execute fault occurs.

Both paths make the fault handler allocate under mmu_lock, so a backend that fails to stage that memory returns a KVM_RUN error or crashes the host. The property holds on any host. On a pKVM host, where a non-protected guest uses the pkvm_pgtable_*() backend, it also guards that backend's fault-time staging.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../kvm/arm64/stage2_block_transitions.c      | 226 ++++++++++++++++++
 2 files changed, 227 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/stage2_block_transitions.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index d28a057fa6c2..ed2877825eee 100644
@@ -179,6 +179,7 @@ TEST_GEN_PROGS_arm64 += arm64/psci_test
 TEST_GEN_PROGS_arm64 += arm64/sea_to_user
 TEST_GEN_PROGS_arm64 += arm64/set_id_regs
 TEST_GEN_PROGS_arm64 += arm64/smccc_filter
+TEST_GEN_PROGS_arm64 += arm64/stage2_block_transitions
 TEST_GEN_PROGS_arm64 += arm64/vcpu_width_config
 TEST_GEN_PROGS_arm64 += arm64/vgic_init
 TEST_GEN_PROGS_arm64 += arm64/vgic_irq
tools/testing/selftests/kvm/arm64/stage2_block_transitions.c new file +226 -0

226 lines not on this page · raw message

Signature · 1 line
2.39.5
Tested-by on v5 7/7
#20 lore raw

Re: [PATCH v5 7/7] KVM: arm64: selftests: Add stage-2 block transition test

Quoted 9 lines · Fuad Tabba

Add a test for the two stage-2 granularity changes dirty logging forces at fault time, asserting the guest completes with no KVM_RUN error. The first scenario collapses a page into a hugetlb-backed block: it writes under logging, re-write-protects the page via GET_DIRTY_LOG, then writes again with logging off. The second splits blocks: it faults in several non-executable 2M blocks, enables logging, then executes in each block so an execute permission fault splits it. It is skipped when CTR_EL0.DIC is set, since mappings are then executable and no execute fault occurs.

Quoted 8 lines

Both paths make the fault handler allocate under mmu_lock, so a backend that fails to stage that memory returns a KVM_RUN error or crashes the host. The property holds on any host. On a pKVM host, where a non-protected guest uses the pkvm_pgtable_*() backend, it also guards that backend's fault-time staging.

Assisted-by Antigravity:gemini-3.1-pro
Quoted 242 lines

---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../kvm/arm64/stage2_block_transitions.c | 226 ++++++++++++++++++

2 files changed, 227 insertions(+)

create mode 100644 tools/testing/selftests/kvm/arm64/stage2_block_transitions.c

tools/testing/selftests/kvm/Makefile.kvm +0 -0
index d28a057fa6c2..ed2877825eee 100644
@@ -179,6 +179,7 @@ TEST_GEN_PROGS_arm64 += arm64/psci_test

TEST_GEN_PROGS_arm64 += arm64/sea_to_user TEST_GEN_PROGS_arm64 += arm64/set_id_regs TEST_GEN_PROGS_arm64 += arm64/smccc_filter +TEST_GEN_PROGS_arm64 += arm64/stage2_block_transitions TEST_GEN_PROGS_arm64 += arm64/vcpu_width_config TEST_GEN_PROGS_arm64 += arm64/vgic_init TEST_GEN_PROGS_arm64 += arm64/vgic_irq

tools/testing/selftests/kvm/arm64/stage2_block_transitions.c new file +216 -0
new file mode 100644
index 000000000000..5fd47f4ada1f
@@ -0,0 +1,226 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2026 Google LLC
+ * Author: Fuad Tabba <[email protected]>
+ *
+ * stage2_block_transitions - Exercise stage-2 block/page granularity changes
+ * that dirty logging forces at fault time, and assert the guest completes.
+ *
+ * Both scenarios 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
+ * sequence to completion and every KVM_RUN succeeds. On a pKVM host, where a
+ * non-protected guest's stage-2 faults are serviced by the pkvm_pgtable_*()
+ * backend, the same sequences also guard that backend's fault-time staging.
+ *
+ * Scenario 1 - block collapse on dirty-logging disable:
+ *   A write under dirty logging installs a 4K page; GET_DIRTY_LOG
+ *   re-write-protects it; logging is disabled; a second write takes a
+ *   permission fault that collapses the page into a hugetlb-backed block,
+ *   which requires a fresh mapping object under mmu_lock.
+ *
+ * Scenario 2 - block split under dirty logging:
+ *   Several hugetlb-backed blocks are faulted in as non-executable blocks,
+ *   dirty logging is enabled (write-protect only), then the guest executes
+ *   into each block. Each instruction fetch takes an execute permission
+ *   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.
+ */
+#include <linux/bitfield.h>
+#include <linux/bitmap.h>
+#include <linux/mman.h>
+#include <linux/sizes.h>
+#include <sys/mman.h>
+
+#include <asm/sysreg.h>
+
+#include "kvm_util.h"
+#include "processor.h"
+#include "test_util.h"
+#include "ucall.h"
+
+#define DATA_SLOT		1
+#define TEST_GVA		0xc0000000UL
+#define BLOCK_SIZE		SZ_2M
+
+/* AArch64 "ret" (ret x30): a self-contained, returnable executable payload. */
+#define RET_INSN		0xd65f03c0U
+
+/*
+ * A non-protected guest's per-VM stage-2 pool is seeded only with the PGD
+ * donation, which stage-2 init immediately consumes, so the page-table budget
+ * for a fault that does not top up is just the handful (~2x the stage-2 min
+ * pages) of memcache leftovers. Executing into this many distinct blocks
+ * demands far more than that budget: a fault path that tops up on every fault
+ * completes all of them, one that skips non-write faults runs out mid-sequence.
+ */
+#define NR_BLOCKS		16
+
+/* Scenario 2 guest -> host sync stages. */
+#define STAGE_SKIP_DIC		1
+#define STAGE_BLOCKS_READY	2
+
+static void collapse_guest_code(u64 gva)
+{
+	u64 *data = (u64 *)gva;
+
+	/* Under dirty logging: install a 4K writable page. */
+	WRITE_ONCE(*data, 0x1);
+	GUEST_SYNC(1);
+
+	/* Logging disabled: a permission fault collapses the page into a block. */
+	WRITE_ONCE(*data, 0x2);
+	GUEST_SYNC(2);
+
+	GUEST_DONE();
+}
+
+static void test_block_collapse(void)
+{
+	struct kvm_vcpu *vcpu;
+	unsigned long *bmap;
+	struct kvm_vm *vm;
+	struct ucall uc;
+	size_t npages;
+	u64 gpa;
+
+	vm = vm_create_with_one_vcpu(&vcpu, collapse_guest_code);
+	npages = BLOCK_SIZE / vm->page_size;
+
+	gpa = (vm_compute_max_gfn(vm) * vm->page_size) - BLOCK_SIZE;
+	gpa = align_down(gpa, BLOCK_SIZE);
+
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB, gpa,
+				    DATA_SLOT, npages, KVM_MEM_LOG_DIRTY_PAGES);
+	virt_map(vm, TEST_GVA, gpa, npages);
+	vcpu_args_set(vcpu, 1, TEST_GVA);
+
+	bmap = bitmap_zalloc(BLOCK_SIZE / getpagesize());
+
+	vcpu_run(vcpu);
+	TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_SYNC && uc.args[1] == 1,
+		    "Expected first sync, got cmd %lu arg %lu", uc.cmd, uc.args[1]);
+
+	/* GET_DIRTY_LOG re-write-protects the dirtied page; then stop logging. */
+	kvm_vm_get_dirty_log(vm, DATA_SLOT, bmap);
+	vm_mem_region_set_flags(vm, DATA_SLOT, 0);
+
+	/* The collapsing permission fault: a broken fault path faults here. */
+	vcpu_run(vcpu);
+	TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_SYNC && uc.args[1] == 2,
+		    "Expected second sync, got cmd %lu arg %lu", uc.cmd, uc.args[1]);
+
+	vcpu_run(vcpu);
+	TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_DONE,
+		    "Expected done, got cmd %lu", uc.cmd);
+
+	free(bmap);
+	kvm_vm_free(vm);
+}
+
+static void guest_sync_insn(u64 va)
+{
+	/* Make the just-written instruction coherent for execution (!DIC). */
+	asm volatile("dc cvau, %0\n"
+		     "dsb ish\n"
+		     "ic ivau, %0\n"
+		     "dsb ish\n"
+		     "isb\n"
+		     :: "r" (va) : "memory");
+}
+
+static void split_guest_code(u64 base_gva, u64 nblocks)
+{
+	u64 i, va;
+
+	if (FIELD_GET(CTR_EL0_DIC_MASK, read_sysreg(ctr_el0))) {
+		GUEST_SYNC(STAGE_SKIP_DIC);
+		GUEST_DONE();
+		return;
+	}
+
+	/* Fault in each block (non-executable) and stage an executable payload. */
+	for (i = 0; i < nblocks; i++) {
+		va = base_gva + i * BLOCK_SIZE;
+		WRITE_ONCE(*(u32 *)va, RET_INSN);
+		guest_sync_insn(va);
+	}
+	GUEST_SYNC(STAGE_BLOCKS_READY);
+
+	/* Logging is now on: executing into each block splits it into pages. */
+	for (i = 0; i < nblocks; i++) {
+		va = base_gva + i * BLOCK_SIZE;
+		((void (*)(void))va)();
+	}
+
+	GUEST_DONE();
+}
+
+static void test_exec_split_drain(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	struct ucall uc;
+	size_t npages;
+	u64 gpa;
+
+	vm = vm_create_with_one_vcpu(&vcpu, split_guest_code);
+	npages = NR_BLOCKS * (BLOCK_SIZE / vm->page_size);
+
+	gpa = (vm_compute_max_gfn(vm) * vm->page_size) - NR_BLOCKS * BLOCK_SIZE;
+	gpa = align_down(gpa, BLOCK_SIZE);
+
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB, gpa,
+				    DATA_SLOT, npages, 0);
+	virt_map(vm, TEST_GVA, gpa, npages);
+	vcpu_args_set(vcpu, 2, TEST_GVA, (u64)NR_BLOCKS);
+
+	vcpu_run(vcpu);
+	TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_SYNC,
+		    "Expected sync, got cmd %lu", uc.cmd);
+	if (uc.args[1] == STAGE_SKIP_DIC) {
+		ksft_print_msg("SKIP block split: CTR_EL0.DIC == 1\n");
+		kvm_vm_free(vm);
+		return;
+	}
+	TEST_ASSERT(uc.args[1] == STAGE_BLOCKS_READY,
+		    "Expected blocks-ready sync, got arg %lu", uc.args[1]);
+
+	/* Write-protect the blocks; the guest then splits them by executing. */
+	vm_mem_region_set_flags(vm, DATA_SLOT, KVM_MEM_LOG_DIRTY_PAGES);
+
+	vcpu_run(vcpu);
+	TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_DONE,
+		    "Expected done, got cmd %lu", uc.cmd);
+
+	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
+ * peak reservation is scenario 2's; the two scenarios run and free in turn.
+ */
+static void require_hugepages(size_t bytes)
+{
+	void *mem = mmap(NULL, bytes, PROT_READ | PROT_WRITE,
+			 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_2MB,
+			 -1, 0);
+
+	if (mem == MAP_FAILED)
+		ksft_exit_skip("Need %zu bytes of reserved 2M hugepages\n", bytes);
+	munmap(mem, bytes);
+}
+

tools/testing/selftests/kvm/arm64/stage2_block_transitions.c line 226

Quoted 10 lines
tools/testing/selftests/kvm/arm64/stage2_block_transitions.c new file +10 -0
+int main(void)
+{
+	require_hugepages(NR_BLOCKS * BLOCK_SIZE);
+
+	test_block_collapse();
+	test_exec_split_drain();
+
+	ksft_print_msg("All ok!\n");
+	return 0;
+}

Thanks!

Tested-by on the v5 series
#21 lore raw
In reply to Fuad Tabba
Quoted 75 lines · Fuad Tabba

Hi folks,

Changes since v4 [1]:
- Restored the permission-fault and dirty-logging memcache top-ups that v4 dropped. Both fix real bugs.
- Patch 1 uses the anonymous bitfield encoding rather than the open-coded mask and helpers. (Marc)
- Reshaped the permission-fault top-up to stage the full memcache under pKVM, not just the mapping object. The object-only form still returned -ENOMEM and tripped a WARN under the hypervisor's unconditional min-pages check.
- Re-scoped the dirty-logging top-up to its generic, non-pKVM failure mode, now that the permission-fault patch covers pKVM.
- Added three adjacent fixes found while going through the series, and a selftest for the block transitions.
- Patches 1 and 3 keep Bradley's Signed-off-by from v3; my changes to each are noted in a [tabba: ...] line.

I picked this up while reviewing Bradley's "mapping cache" series [2]:
v4 dropped two fixes from v3 that address real bugs, so I've collected the three fixes back together, reshaped patch 1 per Marc's review [3], added a few adjacent fixes I found along the way and a selftest, and am reposting as v5.

Most of the fixes are in the pKVM stage-2 walker. On a pKVM host a non-protected guest's stage-2 faults go through pkvm_pgtable_*(), which diverges from the generic walker in several ways that are bugs: cache maintenance on non-cacheable mappings (patch 1), a missing memcache top-up on permission faults that under pKVM still allocate (patch 2), a full flush walk on FWB hardware (patch 4), a WARN on a guest-reachable map failure (patch 5), and an eager-split capability whose pKVM backend is only a stub (patch 6). These affect non-protected guests only, since dispatch and memcache selection key on the host-global pKVM mode, and protected guests take no permission faults.

Patch 3 is not pKVM-specific. During dirty logging a non-write permission fault, an instruction fetch, still needs a page-table page to split a block, but the memcache top-up is gated on write faults. That fault path is generic, so the fix is too.

The two memcache top-ups came from sashiko review-bot findings [4][5], and both check out against the code.

The series is structured as follows:

1: Skip cache maintenance for non-cacheable mappings.
2-3: Top up the memcache for the permission and dirty-logging faults
that force stage-2 block transitions.
4-6: Adjacent pkvm_pgtable_*() fixes: FWB flush early-out, drop a
spurious map WARN, and gate the eager-split capability.
7: Selftest for the block-collapse and block-split transitions.

Testing: the selftest in patch 7 covers the two dirty-logging block transitions (page->block collapse and block->page split) that patch 2 stages; on the base kernel the collapse oopses the host in pkvm_pgtable_stage2_map() with a NULL dereference under mmu_lock, and with the series applied it passes. It is a standalone test rather than an extension of kvm_page_table_test, whose default anonymous-4K backing forms no huge-page blocks, so an automated run never exercises these transitions, and whose worker/stage harness does not fit the multi-stage logging sequence. Run it with 2M hugepages
reserved and, for the split half, on a CPU with CTR_EL0.DIC == 0 (e.g. -cpu cortex-a710 under QEMU); it self-skips those otherwise. It also passes on a non-pKVM host (VHE and nVHE), where patch 3's generic change applies. The other fixes are not exercised by the selftest and rest on the analysis in their commit messages: patch 3's fault path is generic and non-pKVM (under pKVM patch 2 already tops it up, and its failure is a WARN_ON(!nobjs) in kvm_mmu_memory_cache_alloc(), not a KVM_RUN error), and patches 1 and 4-6 each need a specific pKVM configuration to hit.

Based on Linux v7.2-rc3 (a13c140cc289).

Cheers,
/fuad

Quoted 7 lines

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4]
https://lore.kernel.org/all/[email protected]/
[5]
https://lore.kernel.org/all/[email protected]/

Whole series

PKVM.c/.h

Test: Tested-by: Bradley Morgan <[email protected]> # On QEMU arm64 host

Thanks!

maintainer Applied
#22 lore raw
In reply to Fuad Tabba
Quoted 16 lines · Fuad Tabba

Changes since v4 [1]:
- Restored the permission-fault and dirty-logging memcache top-ups that v4 dropped. Both fix real bugs.
- Patch 1 uses the anonymous bitfield encoding rather than the open-coded mask and helpers. (Marc)
- Reshaped the permission-fault top-up to stage the full memcache under pKVM, not just the mapping object. The object-only form still returned -ENOMEM and tripped a WARN under the hypervisor's unconditional min-pages check.
- Re-scoped the dirty-logging top-up to its generic, non-pKVM failure mode, now that the permission-fault patch covers pKVM.
- Added three adjacent fixes found while going through the series, and a selftest for the block transitions.
- Patches 1 and 3 keep Bradley's Signed-off-by from v3; my changes to each are noted in a [tabba: ...] line.

Quoted 1 line

[...]

Applied to next, thanks!

[1/7] KVM: arm64: Skip cache maintenance for non-cacheable pKVM mappings

      https://git.kernel.org/kvmarm/kvmarm/c/fff4eb33ffa0
[2/7] KVM: arm64: Top up the memcache for pKVM permission faults
      https://git.kernel.org/kvmarm/kvmarm/c/6bf0215c565e
[3/7] KVM: arm64: Top up stage-2 memcache for dirty logging faults
      https://git.kernel.org/kvmarm/kvmarm/c/56fd1fc526e6
[4/7] KVM: arm64: Skip pKVM stage-2 flush when FWB is enabled
      https://git.kernel.org/kvmarm/kvmarm/c/1feb9f6af895
[5/7] KVM: arm64: Don't WARN on pKVM stage-2 map failures
      https://git.kernel.org/kvmarm/kvmarm/c/c24322270169
[6/7] KVM: arm64: Don't advertise eager page splitting under pKVM
      https://git.kernel.org/kvmarm/kvmarm/c/c6f308b4d341
[7/7] KVM: arm64: selftests: Add stage-2 block transition test
      https://git.kernel.org/kvmarm/kvmarm/c/979ae12a2d09

--
Best,
Oliver