From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796295505; b=S7A0sMsSnHtj7gdXofuzvSUCYsYeG5N3SU8ZBjreL3sYaBen7FlMDrSqWsp3z00VSZdG sLZnb/9sjFfpbEZkzFUrEgUkUPb3cFrVxmCDo6uK3Lu1AbvBW89pQmK4E0tePr95nS7ou twIY/wiG61ziBqHD3bk1rx8Vuqh7lPvjGy0SrCUmxcVfzPOoAMr448rzYYtY+Tbs+lVzN fVrLg1TkEPEgVbgoyNbloTxIvrIEuCTRjaAYzw2A2DZqFG7f7/qlLY1oci1cT4mDtJwQ5 FpcPzdizACz+8QWmGUsnVzruPoWRRQz0fcGOr6TLFB8+1Q7mNLeGv6zoonDqWpOHbog== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796295505; 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=R4xUseo6bSNVqnrdVCLflv/mUxvSM6eONAcRMKedAQs=; b=t9jsof7N8ez2bV8hoIXtKHyW19EEYuY8C5REPMpo5xLUo6TFviO1Jsvv3nhx6asUb+Wd DSkA0Syol+fVigo4lHDy65qPX2A+eIV/liFbd2sLeqJR5JaZQqyhZm1SjwP6vsCXC0Qno tAYZ+8D6duOHFITNM3LK6m0syyK+ZMjc/FWadnNO58hob+XEa5Oh2DvtjidUMNsbXVzKo NF9Akg+ITWZeQZecoaZZ1kWv28JugDr2l289K4vDYN5QXQMXG9VuP8pqDHW6Hy+qaalKN u1vrcPYXTvp9FYTgXcefvdrqY0REUE9cZNaYAhaBWpiyB+WD9o3MTa63wH/uHMXS84g== 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=eE4EQV8R; 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 A8379606A332; Thu, 3 Dec 2026 10:58:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A8379606A332 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=eE4EQV8R 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 2CB591516; Thu, 3 Dec 2026 02:58:21 -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 470D23F86F; Thu, 3 Dec 2026 02:58:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796295503; bh=nNO9wLgXkRaag3/N+nWG9Okn1rYWlO2NBr1iKmEwFzk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eE4EQV8RgB6fQCkgS2LcZ+nZnhUrlcx9i+37xQQlEG/7iDO4WjAy6houe5bvGqoTm TOtn0ML/bAhHqaEdmcKBRN8pd7FjY3tZywfnrOiWc4oEdsJJ2ambl/F+kVzS20eNml 6VI/L1Yw9G/IJZQIFSIFk9D8psBrxUypIy+pBuaM= 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, Sashiko Subject: [PATCH v4 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA Date: Thu, 3 Dec 2026 10:58:02 +0000 Message-Id: <20261203105805.165749-2-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: linux-kernel@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 The VNCR area is 4K aligned. On a host with 16K or 64K pages, KVM maps the host page containing the VNCR area at a fixmap address. __compute_hcr() currently takes the offset within that page from the guest's VNCR_EL2, which holds a VA. The guest hypervisor can use a smaller EL2 stage-1 granule than the host page size. This is possible with nested stage-2 disabled (HCR_EL2.VM clear), or with a nested stage-2 granule at least as large as the host page. The VA and PA offsets can then differ, and VNCR accesses land in the wrong 4K area. Keep the translated offset in the cached HPA. Align the HPA when installing the fixmap and use its offset when programming VNCR_EL2. Reported-by: Sashiko Closes: https://lore.kernel.org/all/sashiko-outbox-232440@kernel.org/ Fixes: 73e1b621b25d ("KVM: arm64: nv: Program host's VNCR_EL2 to the fixmap address") Signed-off-by: Jia He --- arch/arm64/include/asm/kvm_nested.h | 20 ++++++++++++++++++++ arch/arm64/kvm/hyp/vhe/switch.c | 7 +++++-- arch/arm64/kvm/nested.c | 24 +++--------------------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h index 0f899addafa8..4b0057413229 100644 --- a/arch/arm64/include/asm/kvm_nested.h +++ b/arch/arm64/include/asm/kvm_nested.h @@ -430,6 +430,26 @@ int __kvm_find_s1_desc_level(struct kvm_vcpu *vcpu, u64 va, u64 ipa, int *level); /* VNCR management */ +struct vncr_tlb { + /* The guest's VNCR_EL2 */ + u64 gva; + struct s1_walk_info wi; + struct s1_walk_result wr; + + /* Host physical address of the 4K VNCR page. */ + u64 hpa; + bool hpa_writable; + + /* -1 when not mapped on a CPU */ + atomic_t cpu; + + /* + * true if the TLB is valid. Can only be changed with the + * mmu_lock held. + */ + bool valid; +}; + int kvm_vcpu_allocate_vncr_tlb(struct kvm_vcpu *vcpu); int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu); void kvm_handle_s1e2_tlbi(struct kvm_vcpu *vcpu, u32 inst, u64 val); diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c index 3f2390a13173..c6aab5940376 100644 --- a/arch/arm64/kvm/hyp/vhe/switch.c +++ b/arch/arm64/kvm/hyp/vhe/switch.c @@ -94,8 +94,11 @@ static u64 __compute_hcr(struct kvm_vcpu *vcpu) if (guest_hcr & HCR_EL2_NV) { u64 va = __fix_to_virt(vncr_fixmap(smp_processor_id())); - /* Inherit the low bits from the actual register */ - va |= __vcpu_sys_reg(vcpu, VNCR_EL2) & GENMASK(PAGE_SHIFT - 1, 0); + /* + * Inherit the offset within the fixmap page from the PA + * obtained after translating the guest's VA. + */ + va |= offset_in_page(vcpu->arch.vncr_tlb->hpa); write_sysreg_s(va, SYS_VNCR_EL2); /* Force NV2 in case the guest is forgetful... */ diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c index 4d83281a7a28..ff78d2784db5 100644 --- a/arch/arm64/kvm/nested.c +++ b/arch/arm64/kvm/nested.c @@ -19,25 +19,6 @@ #include "sys_regs.h" #include "vgic/vgic.h" -struct vncr_tlb { - /* The guest's VNCR_EL2 */ - u64 gva; - struct s1_walk_info wi; - struct s1_walk_result wr; - - u64 hpa; - bool hpa_writable; - - /* -1 when not mapped on a CPU */ - atomic_t cpu; - - /* - * true if the TLB is valid. Can only be changed with the - * mmu_lock held. - */ - bool valid; -}; - /* * Ratio of live shadow S2 MMU per vcpu. This is a trade-off between * memory usage and potential number of different sets of S2 PTs in @@ -1643,7 +1624,7 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) } vt->gva = va; - vt->hpa = pfn << PAGE_SHIFT; + vt->hpa = (pfn << PAGE_SHIFT) | offset_in_page(vt->wr.pa); vt->hpa_writable = writable; vt->valid = true; atomic_set(&vt->cpu, -1); @@ -1805,7 +1786,8 @@ static void kvm_map_l1_vncr(struct kvm_vcpu *vcpu) */ if (pgprot_val(prot) != pgprot_val(PAGE_NONE)) { atomic_set(&vt->cpu, smp_processor_id()); - __set_fixmap(vncr_fixmap(atomic_read(&vt->cpu)), vt->hpa, prot); + __set_fixmap(vncr_fixmap(atomic_read(&vt->cpu)), + vt->hpa & PAGE_MASK, prot); host_data_set_flag(L1_VNCR_MAPPED); } } -- 2.34.1