mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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, Sashiko <sashiko-bot@kernel.org>
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	[thread overview]
Message-ID: <20261203105805.165749-2-justin.he@arm.com> (raw)
In-Reply-To: <20261203105805.165749-1-justin.he@arm.com>

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 <sashiko-bot@kernel.org>
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 <justin.he@arm.com>
---
 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


  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 ` Jia He [this message]
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 ` [PATCH v4 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory Jia He

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-2-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=sashiko-bot@kernel.org \
    --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®