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

Justin He Justin.He at arm.com
Wed Dec 2 18:32:23 PST 2026


Hi Marc,
> -----Original Message-----
[...]
> 
> Same as for patch #1: these two paragraphs are better kept either in
> the cover letter. Keep the commit message on point.
> 

Agreed, I will move both test descriptions to the cover letter in v4.

> >
> > Signed-off-by: Jia He <justin.he at arm.com>
> > ---
> >  arch/arm64/include/asm/kvm_mmu.h |  2 ++
> >  arch/arm64/kvm/mmu.c             |  2 +-
> >  arch/arm64/kvm/nested.c          | 38 +++++++++++++++++++++++++++-----
> >  3 files changed, 35 insertions(+), 7 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 523d71d4df13..d3b77ed53ef6 100644
> > --- a/arch/arm64/kvm/nested.c
> > +++ b/arch/arm64/kvm/nested.c
> > @@ -1553,6 +1553,37 @@ 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. 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.
> > + */
> 
> This applies to normal mapping as well, doesn't it? We already have
> the non-gmem path doing VMA lookups, so this seems rather superfluous.
> Unless this is covering something else?
> 

Yes, this adds a second VMA lookup for PFNMAP. The existing lookup
in hva_to_pfn_remapped() doesn't expose the VMA attributes to
kvm_translate_vncr(), so the patch looks up vm_page_prot again.
PFNs in the linear map bypass this lookup.

I'd prefer to keep this local to arm64 for v4 to stay aligned
with the stage-2 fault path, unless you'd rather extend the
generic fault-in interface to reuse the existing lookup.

I also reproduced a spurious SEA when replacing PFNMAP with shmem
using MAP_FIXED. In v4, both PFN fault-in errors and cacheability
failures are checked after mmu_invalidate_retry() under mmu_lock,
before SEA injection. If an invalidation is detected, KVM retries
instead.

> > +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;
> > @@ -1625,12 +1656,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;
> 
> Otherwise looks OK to me.
> 
Thanks for the review.

Best Regards,
Jia He (Justin)




More information about the linux-arm-kernel mailing list