From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795953145; b=WqM61dQsz56tdVEggL/CB9b6yl1gFIHnQH07+TG1rMfWpZ8o6G1F60Y4kxUF2dLDrZq4 odC0Cqvhpomgs7O+YQrtWDD/9XxIma1Gxz90RcbrrugKFbVHyVBi0YshzMgcPELo4tY7Y fvdiCExCr9Z3/0YKRrcXagKqN1napxyVESLrxcNpJsxGpkzUjmUnU6vLuyCC5qbw1Sl6h NvrP7x2d8EeTKXC0zcJHeshMGhjDXFaKm6MXU8ZbRbmiPuZgitB/iiPoAXnymJ89Vpvmp qlw69mutVNtD6HT35K7qjVKMflhw0jcle3tSr4uG3Ffq2no0y76O20SvwrdiutiHpxw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795953145; h=Received:DKIM-Signature:Received:Date:Message-ID:From:To:Cc:Subject: In-Reply-To:References:User-Agent:MIME-Version:Content-Type: X-SA-Exim-Connect-IP:X-SA-Exim-Rcpt-To:X-SA-Exim-Mail-From: X-SA-Exim-Scanned; bh=WAUYxhv0vB83YDZ8HT1pI2OUJIE6FerF+7V/RjNBtHU=; b=fzsl4m/+5HurNjUdlKxlJIKIp0eNjbNVjKHgOLYIGr5PhEoqeX5QSrlsPTgtCuOYOSO2 3syYXl1INLzZbeZZ6FLS143mh2IAWpd9K5ZOfSdxRp3zllctxCf+5/vQG42tYF5btHvGP El7sXH9/f1SH806XiNsvWR40HYj1GtfWAv7VHEemnZRRQzK1myqFSpnvfWzZQAG683xTT /Z2xbcyqwKRVXl54SNUN5y6jEqd45QmSO1NfjqESk/2wzEXWHj8+6AaIt9OFlTmISYp0l BLE7sMrmXKt23+tj6Jy0mPjoD+/HM/iw5GEdXB6GIvdGzw2AbfJctI5kJj+5p8Xpx0g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DLL4MKGu; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE52E606A68F; Sun, 29 Nov 2026 11:52:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DLL4MKGu Received: by smtp.kernel.org (Postfix) with ESMTPSA id B283A1F00896; Sun, 29 Nov 2026 11:52:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795953144; bh=WAUYxhv0vB83YDZ8HT1pI2OUJIE6FerF+7V/RjNBtHU=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=DLL4MKGu84N6q/qF0nc6sS+qmoh7aHcdyI14vg935LqLcYsYi0JxDMr5VNC23kAxO udg0wXuh57o0wEudrBUTk8mFMOMc6CaxELYa9nk4oAZVDERlIIp2dZqsAlZ+H2BiEG OxEhiZUTd7bPHQg6paqrp7R7A3cOETiQnHEAS/1VPRdoMKsrSO3YFrlAOsbsv2t1pj 248LiMhO/B1lPIvBff9aeRz1iy1W4/f5H552xresDCQUhZ/Pp4WBFm5KzpmYbwXWfN w9aL6zEAqRAxy3bFHAV9mURl1l95awx0DkFMIo0STlXsA9Il/29eKSYv5b+xCRRj8H P4EI+4zZ8JpRQ== 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 1xXdSA-00000004d4S-0ttU; Sun, 29 Nov 2026 11:52:22 +0000 Date: Sun, 29 Nov 2026 11:52:21 +0000 Message-ID: <86mrqryhvu.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, Sashiko Subject: Re: [PATCH v3 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA In-Reply-To: <20261128011447.3588988-2-justin.he@arm.com> References: <20261128011447.3588988-1-justin.he@arm.com> <20261128011447.3588988-2-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-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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, sashiko-bot@kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On Sat, 28 Nov 2026 01:14:44 +0000, Jia He wrote: > > VNCR_EL2 only needs to be 4K aligned. On a host with 16K or 64K pages, > kvm_translate_vncr() records the host page that contains the guest > hypervisor's VNCR page, and that host page is mapped at the VNCR fixmap. > __compute_hcr() then takes the offset of the VNCR page in the host page > from bits [PAGE_SHIFT-1:12] of the guest's VNCR_EL2, which is a VA. > > If the guest hypervisor uses a stage-1 granule smaller than the host > page, these bits of the VA and of the PA can differ. The nested context > then accesses another 4K page of the guest instead of the VNCR page. > That's a pretty dubious setup, given that a guest hypervisor cannot use a S2 that is smaller than the host page size, and therefore would have to use a S1 size different from its S2. Possible, but unlikely. > Take the offset from the PA that the walk returns, and keep it in > vt->hpa. Use it only while the VNCR page is mapped. Otherwise the > fixmap is clear, the access faults and KVM translates VNCR_EL2 again, > so the offset does not matter. > > Tested on a Neoverse-N2 host running 16K and 64K page kernels, with the > selftest added later in this series. The test maps the VNCR page at a VA > whose bits [15:12] differ from those of its PA. With the offset taken > from the VA, the nested context reads 0 instead of the value in the VNCR > page. With this patch, it reads the expected value. That's for the test commit message, not this. > > 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 | 1 + > arch/arm64/kvm/hyp/vhe/switch.c | 9 +++++++-- > arch/arm64/kvm/nested.c | 8 +++++++- > 3 files changed, 15 insertions(+), 3 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h > index 0f899addafa8..bc54a4b2e863 100644 > --- a/arch/arm64/include/asm/kvm_nested.h > +++ b/arch/arm64/include/asm/kvm_nested.h > @@ -432,6 +432,7 @@ int __kvm_find_s1_desc_level(struct kvm_vcpu *vcpu, u64 va, u64 ipa, > /* VNCR management */ > int kvm_vcpu_allocate_vncr_tlb(struct kvm_vcpu *vcpu); > int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu); > +unsigned long kvm_vncr_page_offset(struct kvm_vcpu *vcpu); > void kvm_handle_s1e2_tlbi(struct kvm_vcpu *vcpu, u32 inst, u64 val); > > u16 get_asid_by_regime(struct kvm_vcpu *vcpu, enum trans_regime regime); > diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c > index 3f2390a13173..0d15beea418c 100644 > --- a/arch/arm64/kvm/hyp/vhe/switch.c > +++ b/arch/arm64/kvm/hyp/vhe/switch.c > @@ -94,8 +94,13 @@ 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); > + /* > + * The fixmap maps the host page that contains the VNCR > + * page. Locate the VNCR page in it from its PA: with a > + * smaller guest granule, the VA may have another offset. > + */ > + if (host_data_test_flag(L1_VNCR_MAPPED)) > + va |= kvm_vncr_page_offset(vcpu); This is a bit gross. If nothing is mapped in the fixmap, then the page offset doesn't matter, does it? The comment is also pretty confusing. It should read something like: /* * Inherit the offset in the fixmap page from the PA bits * 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..08582fbc09d5 100644 > --- a/arch/arm64/kvm/nested.c > +++ b/arch/arm64/kvm/nested.c > @@ -1643,7 +1643,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); You should also make sure that when this is fed to set_fixmap(), the sub-page bits are cleared. Yes, it currently does the right thing, but I don't want to rely on this. > vt->hpa_writable = writable; > vt->valid = true; > atomic_set(&vt->cpu, -1); > @@ -1683,6 +1683,12 @@ static void handle_vncr_perm(struct kvm_vcpu *vcpu) > kvm_inject_nested_sync(vcpu, esr); > } > > +/* Offset of the VNCR page in the host page mapped at the VNCR fixmap */ > +unsigned long kvm_vncr_page_offset(struct kvm_vcpu *vcpu) > +{ > + return offset_in_page(vcpu->arch.vncr_tlb->hpa); > +} And this helper can go. Thanks, M. -- Without deviation from the norm, progress is not possible.