From: Jia He <justin.he@arm.com>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>
Cc: Jia He <justin.he@arm.com>, Fuad Tabba <fuad.tabba@linux.dev>,
Joey Gouly <joey.gouly@arm.com>,
Steffen Eiden <seiden@linux.ibm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Shuah Khan <shuah@kernel.org>,
linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v4 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
Date: Thu, 3 Dec 2026 10:58:05 +0000 [thread overview]
Message-ID: <20261203105805.165749-5-justin.he@arm.com> (raw)
In-Reply-To: <20261203105805.165749-1-justin.he@arm.com>
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@arm.com>
---
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
prev parent reply other threads:[~2026-12-03 10:58 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 10:58 [PATCH v4 0/4] KVM: arm64: nv: VNCR page mapping fixes Jia He
2026-12-03 10:58 ` [PATCH v4 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA Jia He
2026-12-03 10:58 ` [PATCH v4 2/4] KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF Jia He
2026-12-03 10:58 ` [PATCH v4 3/4] KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults Jia He
2026-12-03 10:58 ` Jia He [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203105805.165749-5-justin.he@arm.com \
--to=justin.he@arm.com \
--cc=catalin.marinas@arm.com \
--cc=fuad.tabba@linux.dev \
--cc=joey.gouly@arm.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=seiden@linux.ibm.com \
--cc=shuah@kernel.org \
--cc=suzuki.poulose@arm.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®