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=1796467145; b=O4hdzOlwouhBicAN9jU9RcbS5Zlak0Aen/kNfTsdSw/JXOFs4vvQ0RrJjrAwWA1utDRO xOBhvCSvVpVmPGSzstUBuMFF6LTnGCKrGNKeaRxsPNT44NjtPOj6Pg9B5MzZtOnW0sPiR TyaxPeDDOF6jVJIGtfCRQwjS8/ONYIPbARzm0F299n5HsetABAMY/JBcDA3mYOLsiOGQM GX2aiq8n6flUxWafopPkHjFL58fvisr2+qVyI7rAGDgrymUEeeeW7g5J1O8HUiATiuukO h9mRs7N/oGmce0lJ8vHkX972YAIQn6tp20mdHzPEgN500KxyehcXTVK0Hfpnt1gKk3Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796467145; 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=O6FPq0i4KFuS0IljSUXFvANjRiiXUdDAuqLmGOzF3gJ7uFRMoF4mwXFCt3cVPcWawD6/ SdEe2wgVV+upnA1EfNWgQEGIyLdcodaqyDpkoI+1gN4fG2NWldbjd7KRqLpfP8ZNz5er8 P+02b7pGm5z584JW8oubQaHJGZ+caMN/xjLdCTQmOWxjYcwX/I0jHHU32iikUrdKDxPei P3tp6NVQHe/hc70zCXCUWjJKRRJ9uoSFrspKYuFx5KLE2zVEeypjU0wiOVjtBjUa+Nu6e +Ttq5iLBuzZ+w8pO2YwHWOiv3FO+aj3apRUYuPKOP5/8ILl5dCKH87m9whReKocmXXA== 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=obJOTIPA; 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 38D8C606A335; Sat, 5 Dec 2026 10:39:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 38D8C606A335 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=obJOTIPA 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 C185316F8; Sat, 5 Dec 2026 02:39:01 -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 D09353F632; Sat, 5 Dec 2026 02:38:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796467144; bh=nNO9wLgXkRaag3/N+nWG9Okn1rYWlO2NBr1iKmEwFzk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=obJOTIPAubZg/ADO495oxviKgFP+Sc+N+KE2orXvWYJcwbefuRSfljGehwwG16B+w UfBDU0ujTy/ekcWOls3gVVCvyEyFqOMgdu3bJGJOVFUAMLSi0ybjSjcz/iU8Yc1WZ+ Ovyp1G/qR536wWrJgveQ52wFgKOOZMEuhila6sBg= 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 v5 1/3] KVM: arm64: nv: Locate the VNCR page in the host page from its PA Date: Sat, 5 Dec 2026 10:38:42 +0000 Message-Id: <20261205103844.3405166-2-justin.he@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261205103844.3405166-1-justin.he@arm.com> References: <20261205103844.3405166-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