Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796295523; b=fEBIwXQ0HU8MBIkRYG4vvm43TtrKuoO/VTVqGfEmhiRVU1q6HPthxflQdIK7wYN18IM5 38kHHmzW1r1r2FYVxpTLAfqLebREw2SZfPmTokzcUEQriXiPVnJ1WKzqgt5cTelo73wCP uAiEE+EL0El4vER8DG/NCyHgLB9Wg+6PaB1LODUMWPCg6n7pqk+meQzjaayQ2AcmEQuGL ydPFdr0sD9Flxljdcx7xCe2gASuSs94ptvme5Y/Evqb2RWa2GwnENbKDizgobE79QvGz0 Pt3SGp2rCPQ73qjw3r1gjdzePugZp/4Its/iPfAQO+UZ3XIIEkv+XYe9kBVOW2W/yYg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796295523; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=pP+2iGzuCiOOUsLE5iW8ltE2dIVOncoL61qDF7YHPHY=; b=YQ5gRdCs5ruGkSqPa6qaXrIrB9cKA7UcBYfMNs0DyywhOXmP8y7k+hT2ewUPno1uUsim P2blYvMOuR50PS27rZeDjXxB111b7P4KcY/5rNuuxiruOZYPQ2Tpya9sFBAG42klfI+wK QW55j53nNL8Hh5ioKd9dIoZhUvZDod4YVhCFikTQmq6+LCVeWMUJTYalA59T8XhCNbOCt K0+dJtIW6NkV8+hXQbn9En1t1xtDck0R7G2e3Cuw81GH8jTJbixyqcVvf1R4CJFHvRPS4 SFr1DDcKOmDgKu+pHktxDvBQe9twmak+AJXSOQsyS70nnfugGyxxkURgAiT4djGf5JQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=s9P1aNrW; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A071606A32D; Thu, 3 Dec 2026 10:58:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3A071606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=s9P1aNrW Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8EC19152B; Thu, 3 Dec 2026 02:58:39 -0800 (PST) Received: from entos-yitian-01.Arm.com (unknown [10.168.196.233]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id F0B773F86F; Thu, 3 Dec 2026 02:58:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796295522; bh=0xOYTXCwi9gb3k+3MhjbIG8XWYo5X8rw7hKYYXOLVao=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s9P1aNrW5KuvMQ92GF8gq6uGj4SgjFzx1t0Z6OfBTToE+OY4NTb/7LYG1xIG8RlEj EgKK1DWc0JObEw5htTWvlgcYjRFx+nJYCsX1nTg2H3T2WN4DQFU19okvgbb5ES9hxe KRsXCW6e/8/zvRUisdOTeewlwf4RP/opOUu4+ZEM= From: Jia He To: Marc Zyngier , Oliver Upton Cc: Jia He , Fuad Tabba , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mark Rutland , Paolo Bonzini , Sean Christopherson , Shuah Khan , 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 Message-Id: <20261203105805.165749-5-justin.he@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261203105805.165749-1-justin.he@arm.com> References: <20261203105805.165749-1-justin.he@arm.com> X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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