KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
PATCH 1/1 KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
kvm_translate_vncr() refuses any PFN that isn't memory and injects an SEA into the guest hypervisor. That is stricter than what the stage-2 fault path allows (kvm_s2_fault_pin_pfn()): a VM_PFNMAP or VM_MIXEDMAP VMA whose host stage-1 attributes are cacheable is mapped cacheable at stage-2 when FWB and DIC are present, and the guest can use such memory as RAM (e.g. device memory that it onlines as a NUMA node). If the guest then picks a page of that memory as the VNCR page of one of its own guests, it takes an SEA as soon as it enters that guest.
Relax the check to match the stage-2 fault path, addressing the FIXME added by commit 4bd7dbe0b224 ("KVM: arm64: nv: Inject SEA if guest VNCR isn't normal memory"): look up the VMA, and allow the PFN if the VMA is a cacheable VM_PFNMAP or VM_MIXEDMAP one and the system supports cacheable PFNMAP at stage-2. No linear mapping is required, as VNCR accesses use the EL2 fixmap and require no cache maintenance. mmu_invalidate_seq is sampled before the PFN is faulted in, so a VMA change after the lookup is caught by the mmu_invalidate_retry() check that follows, as for the PFN itself.
Tested on a host with FEAT_FGT, FWB and DIC, with an L1 whose whole RAM is a cacheable PFNMAP (host RAM withheld with mem= and handed to the VMM by a test driver with remap_pfn_range()) starting an L2 guest hypervisor. Without the patch, L2 repeatedly takes an SEA at the same PC and fails to boot. With the patch, L2 boots successfully.
--- arch/arm64/include/asm/kvm_mmu.h | 2 ++ arch/arm64/kvm/mmu.c | 2 +- arch/arm64/kvm/nested.c | 37 ++++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_mmu.h +2 -0
@@ -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
arch/arm64/kvm/mmu.c +1 -1
@@ -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 :
arch/arm64/kvm/nested.c +31 -6
@@ -1553,6 +1553,36 @@ 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, as for a stage-2 mapping. The VMA is looked up after the +* PFN has been faulted in: the caller must check mmu_invalidate_retry() +* against a sequence count sampled before the fault-in. +*/ +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 (memslot, gfn); + +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;
@@ -1625,12 +1655,7 @@ 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)) { +if (!vncr_pfn_is_cacheable (memslot, gfn, pfn)) {kvm_release_faultin_page (vcpu->kvm, page, true, false);fail_s1_walk (&vt->wr, ESR_ELx_FSC_EXTABT, false);return -EINVAL ;
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: arm64: nv: vncr_pfn_is_cacheable() rejects read-only memslots It also noticed 1 related…
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: arm64: nv: vncr_pfn_is_cacheable() rejects read-only memslots
It also noticed 1 related pre-existing issue this patch does not introduce:
- [High] KVM: arm64: nv: unhandled -EPERM in kvm_handle_vncr_abort() [1] --
commit 9fdd9f2ca0f89e14eb8d6921a8424af26dfde3e6
Author: Jia He <[email protected]>
KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
This commit allows the L1 VNCR page to reside in a cacheable VM_PFNMAP or VM_MIXEDMAP VMA when the system supports cacheable PFNMAP mappings at stage-2, matching the stage-2 fault path.
Quoted 27 lines
arch/arm64/kvm/nested.c +19
-0@@ -1561,6 +1561,36 @@ 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, as for a stage-2 mapping. The VMA is looked up after the +* PFN has been faulted in: the caller must check mmu_invalidate_retry() +* against a sequence count sampled before the fault-in. +*/ +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; +
arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1592
Quoted 10 lines
arch/arm64/kvm/nested.c +10
-0+ hva =gfn_to_hva_memslot (memslot, gfn); + +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; +}
[Severity: Medium]
Does gfn_to_hva_memslot() reject a KVM_MEM_READONLY memslot here?
gfn_to_hva_memslot() assumes a write access and returns KVM_HVA_ERR_RO_BAD when the memslot is read-only. On a read access to an L1 VNCR page in a read-only memslot backed by a cacheable VM_PFNMAP or VM_MIXEDMAP VMA, __kvm_faultin_pfn() faults in the PFN with writable = false, but vma_lookup() then looks up KVM_HVA_ERR_RO_BAD and returns NULL, causing kvm_translate_vncr() to inject an SEA instead of installing a read-only VNCR mapping as supported since 2684e02bac41 ("KVM: arm64: nv: Respect read-only PFN when mapping L1 VNCR"). Using __gfn_to_hva_memslot() (or gfn_to_hva_memslot_prot() as in kvm_handle_guest_abort()) would avoid failing on read-only memslots.
Quoted 4 lines
arch/arm64/kvm/nested.c +0
-0@@ -1633,12 +1663,7 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) writable = !(memslot->flags &KVM_MEM_READONLY ); }
arch/arm64/kvm/nested.c static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) · line 1669
Quoted 10 lines
arch/arm64/kvm/nested.c +1
-6-/* -* 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)) { +if (!vncr_pfn_is_cacheable (memslot, gfn, pfn)) {kvm_release_faultin_page (vcpu->kvm, page, true, false);fail_s1_walk (&vt->wr, ESR_ELx_FSC_EXTABT, false);return -EINVAL ;
[Severity: High]
This problem wasn't introduced by this patch, but can kvm_handle_vncr_abort() drop a pending SEA and loop forever when kvm_translate_vncr() returns -EPERM [1]?
When hardware Access Flag updates (TCR_EL2.HA) are enabled for guest EL2&0 and a stage-1 descriptor with PTE_AF == 0 resides in a read-only memslot, walk_s1() -> kvm_swap_s1_desc() -> __kvm_at_swap_desc() fails with -EPERM and records ESR_ELx_FSC_SEA_TTW(level) in vt->wr.fst. Because the switch statement in kvm_handle_vncr_abort() does not include -EPERM in its fault-injection cases:
arch/arm64/kvm/nested.c:kvm_handle_vncr_abort() {
...
case -EFAULT:
case -EIO:
case -EHWPOISON:
if (is_gmem)
return 0;
fallthrough;
case -EINVAL:
case -ENOENT:
case -EACCES:
/*
* Translation failed, inject the corresponding
* exception back to EL2.
*/
esr &= ~ESR_ELx_FSC;
esr |= FIELD_PREP(ESR_ELx_FSC, vt->wr.fst);
kvm_inject_nested_sync(vcpu, esr);
break;
...
}KVM returns 1 without injecting the abort or mapping the VNCR page, trapping the vCPU in an infinite VNCR abort loop.
[1] https://sashiko.dev/#/bug/linux-5a55db9b-f840-4142-8a1e-110e2882e8fa
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 66 lines
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Tuesday, November 24, 2026 11:08 AM
To: Justin He <[email protected]>
Cc: [email protected]; Oliver Upton <[email protected]>; Marc Zyngier <[email protected]>
Subject: Re: [PATCH] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memoryThank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: arm64: nv:vncr_pfn_is_cacheable()rejects read-only memslots
It also noticed 1 related pre-existing issue this patch does not introduce:
- [High] KVM: arm64: nv: unhandled-EPERMinkvm_handle_vncr_abort()[1] --commit
9fdd9f2ca0f89e14eb8d6921a8424af26dfde3e6
Author: Jia He <[email protected]>KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
This commit allows the L1 VNCR page to reside in a cacheable
VM_PFNMAPorVM_MIXEDMAPVMA when the system supports cacheable PFNMAP mappings at
stage-2, matching the stage-2 fault path.arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1565
Quoted 10 lines
arch/arm64/kvm/nested.c +2
-0@@ -1561,6 +1561,36 @@ 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_PFNMAPorarch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1566
Quoted 1 line
arch/arm64/kvm/nested.c +1
-0+* VM_MIXEDMAP VMA, as for a stage-2 mapping. The VMA is looked up after the
arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu)
Quoted 4 lines
arch/arm64/kvm/nested.c +4
-0+* PFN has been faulted in: the caller must check mmu_invalidate_retry() +* against a sequence count sampled before the fault-in. +*/ +static bool vncr_pfn_is_cacheable(struct kvm_memory_slot *memslot,
gfn_tgfn,Quoted 17 lines
arch/arm64/kvm/nested.c +17
-0+ 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 (memslot, gfn); + +mmap_read_lock (current->mm); + vma =vma_lookup (current->mm, hva); +if (vma && (vma->vm_flags & (VM_PFNMAP |VM_MIXEDMAP )))
Quoted 9 lines
arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1584
Quoted 5 lines
arch/arm64/kvm/nested.c +5
-0+ ret =kvm_vma_is_cacheable (vma); +mmap_read_unlock (current->mm); + +return ret; +}[Severity: Medium]
Doesgfn_to_hva_memslot()reject aKVM_MEM_READONLYmemslot here?
Indeed. gfn_to_hva_memslot() treats the lookup as a write and returns KVM_HVA_ERR_RO_BAD for a read-only memslot. The VMA lookup then fails
and the guest hypervisor gets an SEA instead of a read-only VNCR
mapping. I reproduced the SEA with a VNCR page in a read-only cacheable
PFNMAP memslot. I am writing v2, using gfn_to_hva_memslot_prot(), as kvm_handle_guest_abort() does.
Quoted 31 lines
gfn_to_hva_memslot()assumes a write access and returnsKVM_HVA_ERR_RO_BAD
when the memslot is read-only. On a read access to an L1 VNCR page in a read-only memslot backed by a cacheableVM_PFNMAPorVM_MIXEDMAPVMA,__kvm_faultin_pfn()faults in the PFN with writable = false, butvma_lookup()then looks upKVM_HVA_ERR_RO_BADand returns NULL, causingkvm_translate_vncr()to inject an SEA instead of installing a read-only VNCR mapping as supported since2684e02bac41("KVM: arm64: nv: Respect read-only PFN when mapping L1 VNCR"). Using__gfn_to_hva_memslot()(orgfn_to_hva_memslot_prot()as inkvm_handle_guest_abort()) would avoid failing on read-only memslots.arch/arm64/kvm/nested.c static int kvm_translate_vncr(struct kvm_vcpu
Quoted 1 line
arch/arm64/kvm/nested.c +0
-0@@ -1633,12 +1663,7 @@ static int kvm_translate_vncr(struct kvm_vcpu*vcpu, bool
*is_gmem)arch/arm64/kvm/nested.c static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
Quoted 5 lines
arch/arm64/kvm/nested.c +0
-2writable = !(memslot->flags &KVM_MEM_READONLY ); } -/* -* FIXME: This check is too restrictive as KVM allows cacheable memory
arch/arm64/kvm/nested.c static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) · line 1661
Quoted 8 lines
arch/arm64/kvm/nested.c +1
-4-* attributes for PFNMAP VMAs that have cacheable attributes in host -* stage-1. -*/ -if (!pfn_is_map_memory (pfn)) { +if (!vncr_pfn_is_cacheable (memslot, gfn, pfn)) {kvm_release_faultin_page (vcpu->kvm, page, true, false);fail_s1_walk (&vt->wr, ESR_ELx_FSC_EXTABT, false);return -EINVAL ;
Quoted 5 lines
[Severity: High]
This problem wasn't introduced by this patch, but cankvm_handle_vncr_abort()drop a pending SEA and loop forever whenkvm_translate_vncr()returns-EPERM[1]?
Yes. Since bff8aa213dee, __kvm_at_swap_desc() returns -EPERM when the descriptor is in a read-only memslot, and kvm_handle_vncr_abort() has no case for it. The vCPU keeps taking the same VNCR abort. Only a guest that puts its own EL2 stage-1 table in a read-only memslot is affected. Will fix and send it separately in v2.
Thanks,
Jia He(Justin)
Quoted 30 lines
When hardware Access Flag updates (TCR_EL2.HA) are enabled for guest EL2&0 and a stage-1 descriptor with
PTE_AF== 0 resides in a read-only memslot,walk_s1()->kvm_swap_s1_desc()->__kvm_at_swap_desc()fails with-EPERMand recordsESR_ELx_FSC_SEA_TTW(level)invt->wr.fst. Because the switch statement inkvm_handle_vncr_abort()does not include-EPERMin its fault-injection cases:arch/arm64/kvm/nested.c:kvm_handle_vncr_abort() { ... case -EFAULT: case -EIO: case -EHWPOISON: if (is_gmem) return 0; fallthrough; case -EINVAL: case -ENOENT: case -EACCES: /* * Translation failed, inject the corresponding * exception back to EL2. */ esr &= ~ESR_ELx_FSC; esr |= FIELD_PREP(ESR_ELx_FSC, vt->wr.fst); kvm_inject_nested_sync(vcpu, esr); break; ... }
Quoted 8 lines
KVM returns 1 without injecting the abort or mapping the VNCR page, trapping the vCPU in an infinite VNCR abort loop.
[1] https://sashiko.dev/#/bug/linux-5a55db9b-f840-4142-8a1e-110e2882e8fa
--
Sashiko AI review * https://sashiko.dev/#/patchset/20261124025336.4107575-
[email protected]?part=1
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.