From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9F36606A31A for ; Wed, 2 Dec 2026 14:33:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=nkbyeHrZ; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=KnL7nppT DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=jJKVjJdfk0wPhSh6nGxz0njTeNSCMOi87B3Ek7fioQ8=; b=nkbyeHrZXHxcnXE0HhOoex1zbW k4qs57s9yIJCfFISTuSVxsz+kqf4yGXLYwbc5/rJABfq1tlT17u+8klBrA+8KKKJa4g3EhVY1tgyE oUkR6SOz6kv7PtgTDg77Wxbp6xSvMHzkyYV0Gjpi5DgJcEqJtgk5sT6vaoSCw0TmQ0yKdt3FNZI9C 3gCJGJo1z70GYABGfNfrNQv6/3N5g+0BXEQA+FFR/xdh4VqVR5a04w98dZdK8pKaUv7fILgqNTGBC 9Yni7qSQHLEhrRhsGyH+SDd4mKyRhkwwl5m1R0MuulhonDezWwiuY9ncKQoFL5DxgLRPh6wUSDvLn 3M+g9yiA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYlO5-0000000BlLS-0QxI; Wed, 02 Dec 2026 14:32:49 +0000 Received: from tor.source.kernel.org ([172.105.4.254]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYlO3-0000000BlKK-1MIG for linux-arm-kernel@lists.infradead.org; Wed, 02 Dec 2026 14:32:47 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id B66DF628AB; Wed, 2 Dec 2026 14:32:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D4B51F00893; Wed, 2 Dec 2026 14:32:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796221966; bh=jJKVjJdfk0wPhSh6nGxz0njTeNSCMOi87B3Ek7fioQ8=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=KnL7nppTWRVmrBMX91le+nNYWqbux/U66ghtTgGERcwULYTZoYSM/OcrZwIraHYqQ vI04uN4adNpYKo0AOy25JLzpOubJX/5dtUfQ1gC9qrNRbt5i3u8Tvh2szu7ZSPHW2n AUpV2lOEYu8Hz17Djg214x9UKCNBRbL0Z6x+Vroua5MbU8GdiDebxe2wbQbQ5Bv6Zl SVsPUhR7AnJuy6Mcocwz9XK12wij1+mcm19wEBna9FosrMaPUL+anUlwwsTx+luGPD z06Nct5uoi+qsjoi/kOXaubDesGuh4SxI3axk7pBTTTCVmUufLlwgx3TtjdJEYzRwB dBqwAWQ1SZpQg== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xYlNz-000000069WK-4665; Wed, 02 Dec 2026 14:32:44 +0000 Date: Wed, 02 Dec 2026 14:32:43 +0000 Message-ID: <86a4mnycqc.wl-maz@kernel.org> From: Marc Zyngier To: Jia He Cc: oupton@kernel.org, fuad.tabba@linux.dev, joey.gouly@arm.com, seiden@linux.ibm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, pbonzini@redhat.com, seanjc@google.com, 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: Re: [PATCH v3 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory In-Reply-To: <20261128011447.3588988-5-justin.he@arm.com> References: <20261128011447.3588988-1-justin.he@arm.com> <20261128011447.3588988-5-justin.he@arm.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) X-Archived-By: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: justin.he@arm.com, oupton@kernel.org, fuad.tabba@linux.dev, joey.gouly@arm.com, seiden@linux.ibm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, pbonzini@redhat.com, seanjc@google.com, 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 X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-15.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Sat, 28 Nov 2026 01:14:47 +0000, Jia He wrote: > > 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. > > For VM_PFNMAP and VM_MIXEDMAP VMAs, relax the check to match the stage-2 > fault path, and remove 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, because VNCR accesses use the EL2 fixmap and > require no cache maintenance. > > Get the HVA with gfn_to_hva_memslot_prot(), as kvm_handle_guest_abort() > does. gfn_to_hva_memslot() returns KVM_HVA_ERR_RO_BAD for a read-only > memslot, and the VMA lookup would then fail. A VNCR page in a read-only > memslot is valid and is mapped read-only since commit 2684e02bac41 > ("KVM: arm64: nv: Respect read-only PFN when mapping L1 VNCR"). > > 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. > > The read-only case was tested on a Neoverse-N2 host. The guest > hypervisor's VNCR page is in a cacheable PFNMAP registered as a > read-only memslot, and a nested context reads VTTBR_EL2. When the HVA > is looked up with gfn_to_hva_memslot(), the guest hypervisor takes an > SEA. With this patch, the read returns the contents of the VNCR page. Same as for patch #1: these two paragraphs are better kept either in the cover letter. Keep the commit message on point. > > Signed-off-by: Jia He > --- > 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? > +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, M. -- Without deviation from the norm, progress is not possible.