mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Justin He <Justin.He@arm.com>
To: Marc Zyngier <maz@kernel.org>
Cc: "oupton@kernel.org" <oupton@kernel.org>,
	"fuad.tabba@linux.dev" <fuad.tabba@linux.dev>,
	Joey Gouly <Joey.Gouly@arm.com>,
	"seiden@linux.ibm.com" <seiden@linux.ibm.com>,
	Suzuki Poulose <Suzuki.Poulose@arm.com>,
	"yuzenghui@huawei.com" <yuzenghui@huawei.com>,
	Catalin Marinas <Catalin.Marinas@arm.com>,
	"will@kernel.org" <will@kernel.org>,
	Mark Rutland <Mark.Rutland@arm.com>,
	"pbonzini@redhat.com" <pbonzini@redhat.com>,
	"seanjc@google.com" <seanjc@google.com>,
	"shuah@kernel.org" <shuah@kernel.org>,
	"linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>,
	"kvmarm@lists.linux.dev" <kvmarm@lists.linux.dev>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"linux-kselftest@vger.kernel.org"
	<linux-kselftest@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Sashiko <sashiko-bot@kernel.org>
Subject: RE: [PATCH v3 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA
Date: Mon, 30 Nov 2026 05:04:15 +0000	[thread overview]
Message-ID: <AS2PR08MB9786C2127C244A95981A3CE9F74E2@AS2PR08MB9786.eurprd08.prod.outlook.com> (raw)
In-Reply-To: <86mrqryhvu.wl-maz@kernel.org>

Hi Marc,

> -----Original Message-----
[...]
> >
> > 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.
> 

The selftest uses a 4K EL2 stage-1 with HCR_EL2.VM clear, so no nested
stage-2 translation is involved.

> > 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.

Sure, I'll move the test results to the selftest commit message in v4.

> 
> >
> > 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 | 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:

Agreed. I'll drop the mapped check and use the offset directly from the
cached HPA, along with your suggested comment.

> 
> 	/*
> 	 * 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.

Agreed, I'll explicitly page-align the HPA before passing it to set_fixmap()

> 
> >  		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.
> 

Sure, I'll drop the helper in v4.
Thanks.

___ 
Best Regards,
Jia He (Justin)

  reply	other threads:[~2026-11-30  5:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28  1:14 [PATCH v3 0/4] KVM: arm64: nv: VNCR page mapping fixes Jia He
2026-11-28  1:14 ` [PATCH v3 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA Jia He
2026-11-29 11:52   ` Marc Zyngier
2026-11-30  5:04     ` Justin He [this message]
2026-11-28  1:14 ` [PATCH v3 2/4] KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF Jia He
2026-11-28  1:14 ` [PATCH v3 3/4] KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults Jia He
2026-11-28  1:14 ` [PATCH v3 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory Jia He
2026-12-02 14:32   ` Marc Zyngier

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=AS2PR08MB9786C2127C244A95981A3CE9F74E2@AS2PR08MB9786.eurprd08.prod.outlook.com \
    --to=justin.he@arm.com \
    --cc=Catalin.Marinas@arm.com \
    --cc=Joey.Gouly@arm.com \
    --cc=Mark.Rutland@arm.com \
    --cc=Suzuki.Poulose@arm.com \
    --cc=fuad.tabba@linux.dev \
    --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=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=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®