[PATCH v4 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory

From: Jia He

Date: Thu Dec 03 2026 - 05:59:08 EST


An L1 hypervisor can use cacheable PFNMAP memory as RAM and put its
VNCR page there. KVM currently rejects the PFN if it lacks a linear
mapping and injects an SEA on the first VNCR access.

For PFNs outside the linear map, use the stage-2 VMA cacheability
check for VM_PFNMAP and VM_MIXEDMAP mappings that KVM can fault in.
Require FWB and DIC. Use gfn_to_hva_memslot_prot() to look up the
VMA even for a read-only memslot.

During a concurrent MAP_FIXED replacement, GUP can fail on the old
PFNMAP VMA and the fallback can find the new shmem VMA. The existing
fault-in error path injects an SEA before checking for invalidation.
The cacheability lookup can also see a replacement VMA. Check the
invalidation sequence before injecting an SEA for either failure.

Signed-off-by: Jia He <justin.he@xxxxxxx>
---
arch/arm64/include/asm/kvm_mmu.h | 2 ++
arch/arm64/kvm/mmu.c | 2 +-
arch/arm64/kvm/nested.c | 57 +++++++++++++++++++++++---------
3 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
index 32392abf2e21..e59c6603b137 100644
--- a/arch/arm64/include/asm/kvm_mmu.h
+++ b/arch/arm64/include/asm/kvm_mmu.h
@@ -385,6 +385,8 @@ static inline void kvm_fault_unlock(struct kvm *kvm)
read_unlock(&kvm->mmu_lock);
}

+bool kvm_vma_is_cacheable(struct vm_area_struct *vma);
+
/*
* ARM64 KVM relies on a simple conversion from physaddr to a kernel
* virtual address (KVA) when it does cache maintenance as the CMO
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 1db10d8d6db7..cbd563ba4dd9 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1639,7 +1639,7 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)
return vma->vm_flags & VM_MTE_ALLOWED;
}

-static bool kvm_vma_is_cacheable(struct vm_area_struct *vma)
+bool kvm_vma_is_cacheable(struct vm_area_struct *vma)
{
switch (FIELD_GET(PTE_ATTRINDX_MASK, pgprot_val(vma->vm_page_prot))) {
case MT_NORMAL_NC:
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 867bcdd69382..e29fb1a8a9ba 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -1534,10 +1534,38 @@ static u64 read_vncr_el2(struct kvm_vcpu *vcpu)
return (u64)sign_extend64(__vcpu_sys_reg(vcpu, VNCR_EL2), 48);
}

+/*
+ * Allow a non-memory PFN only if it comes from a cacheable VM_PFNMAP or
+ * VM_MIXEDMAP VMA and the system supports cacheable PFNMAP at stage-2.
+ */
+static bool vncr_pfn_is_cacheable(struct kvm_memory_slot *memslot, gfn_t gfn,
+ kvm_pfn_t pfn)
+{
+ struct vm_area_struct *vma;
+ unsigned long hva;
+ bool ret = false;
+
+ if (pfn_is_map_memory(pfn))
+ return true;
+
+ if (!kvm_supports_cacheable_pfnmap())
+ return false;
+
+ hva = gfn_to_hva_memslot_prot(memslot, gfn, NULL);
+
+ mmap_read_lock(current->mm);
+ vma = vma_lookup(current->mm, hva);
+ if (vma && (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)))
+ ret = kvm_vma_is_cacheable(vma);
+ mmap_read_unlock(current->mm);
+
+ return ret;
+}
+
static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
{
struct kvm_memory_slot *memslot;
- bool write_fault, writable;
+ bool write_fault, writable, cacheable;
unsigned long mmu_seq;
struct vncr_tlb *vt;
struct page *page = NULL;
@@ -1591,10 +1619,6 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
if (!*is_gmem) {
pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0,
&writable, &page);
- if (is_error_noslot_pfn(pfn)) {
- fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
- return -EFAULT;
- }
} else {
ret = kvm_gmem_get_pfn(vcpu->kvm, memslot, gfn, &pfn, NULL);
if (ret) {
@@ -1606,16 +1630,8 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
writable = !(memslot->flags & KVM_MEM_READONLY);
}

- /*
- * FIXME: This check is too restrictive as KVM allows cacheable memory
- * attributes for PFNMAP VMAs that have cacheable attributes in host
- * stage-1.
- */
- if (!pfn_is_map_memory(pfn)) {
- kvm_release_faultin_page(vcpu->kvm, page, true, false);
- fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
- return -EINVAL;
- }
+ cacheable = !is_error_noslot_pfn(pfn) &&
+ vncr_pfn_is_cacheable(memslot, gfn, pfn);

scoped_guard(write_lock, &vcpu->kvm->mmu_lock) {
if (mmu_invalidate_retry(vcpu->kvm, mmu_seq)) {
@@ -1623,6 +1639,17 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
return -EAGAIN;
}

+ if (is_error_noslot_pfn(pfn)) {
+ fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
+ return -EFAULT;
+ }
+
+ if (!cacheable) {
+ kvm_release_faultin_page(vcpu->kvm, page, true, false);
+ fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
+ return -EINVAL;
+ }
+
vt->gva = va;
vt->hpa = (pfn << PAGE_SHIFT) | offset_in_page(vt->wr.pa);
vt->hpa_writable = writable;
--
2.34.1