Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Marc Zyngier <maz@kernel.org>
To: Oliver Upton <oupton@kernel.org>
Cc: Colton Lewis <coltonlewis@google.com>,
	Leonardo Bras <leo.bras@arm.com>, Joey Gouly <joey.gouly@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Zenghui Yu <yuzenghui@huawei.com>,
	Fuad Tabba <fuad.tabba@linux.dev>,
	kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/2] KVM: arm64: Fix inverted E2H0 check for HCR_EL2.NV1
Date: Wed, 18 Nov 2026 10:13:27 +0000	[thread overview]
Message-ID: <86a4n61m7s.wl-maz@kernel.org> (raw)
In-Reply-To: <avzZUyu9EhxAWtbd@kernel.org>

On Tue, 17 Nov 2026 22:46:43 +0000,
Oliver Upton <oupton@kernel.org> wrote:
> 
> On Tue, Nov 17, 2026 at 08:45:34PM +0000, Colton Lewis wrote:
> > ID_AA64MMFR4_EL1.E2H0 is a signed 4-bit field where:
> >   - 0b0000 (IMP, 0): FEAT_E2H0 is implemented.
> >   - 0b1111 (NI, -1): FEAT_E2H0 is not implemented, except HCR_EL2.NV1
> >     is implemented.
> >   - 0b1110 (NI_NV1, -2): Neither HCR_EL2.E2H == 0 nor HCR_EL2.NV1 is
> >     implemented, and HCR_EL2.NV1 is RES0.
> > 
> > Prior to commit b2a324ff01fe ("KVM: arm64: Use HCR_EL2 feature map to
> > drive fixed-value bits"), kvm_init_nv_sysregs() marked HCR_EL2.NV1 as
> > RES0 when !kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI), i.e., when the
> > signed E2H0 field is less than NI (-1), which matches NI_NV1 (-2).
> > 
> > When converting HCR_EL2 to the feature map, feat_nv2_e2h0_ni() was
> > written as !kvm_has_feat(kvm, FEAT_E2H0), where FEAT_E2H0 checks
> > E2H0 >= IMP (0). This inverted the condition: feat_nv2_e2h0_ni()
> > evaluates to true when E2H0 < 0 (including NI_NV1), leaving HCR_EL2.NV1
> > writable instead of RES0 for VHE-only nested guests where KVM advertises
> > ID_AA64MMFR4_EL1.E2H0 = NI_NV1.
> > 
> > Check kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI) so that HCR_EL2.NV1
> > is treated as implemented only when E2H0 is IMP or NI, and forced to
> > RES0 when E2H0 is NI_NV1. Rename the helper from feat_nv2_e2h0_ni() to
> > feat_nv2_nv1_not_res0() to clearly reflect what it tests.
> > 
> > Fixes: b2a324ff01fe ("KVM: arm64: Use HCR_EL2 feature map to drive fixed-value bits")
> > Signed-off-by: Colton Lewis <coltonlewis@google.com>
> > ---
> >  arch/arm64/kvm/config.c | 7 ++++---
> >  1 file changed, 4 insertions(+), 3 deletions(-)
> > 
> > diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
> > index adaf26ab93b85..a975ac73ed876 100644
> > --- a/arch/arm64/kvm/config.c
> > +++ b/arch/arm64/kvm/config.c
> > @@ -256,9 +256,10 @@ static bool feat_nv2(struct kvm *kvm)
> >  		kvm_has_feat(kvm, ID_AA64MMFR2_EL1, NV, NV2));
> >  }
> >  
> > -static bool feat_nv2_e2h0_ni(struct kvm *kvm)
> > +static bool feat_nv2_nv1_not_res0(struct kvm *kvm)
> >  {
> > -	return feat_nv2(kvm) && !kvm_has_feat(kvm, FEAT_E2H0);
> > +	return feat_nv2(kvm) &&
> > +	       kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI);
> >  }
> 
> This could benefit from a comment, because testing for NI as a feature
> is very misleading. Also, I don't why this needs to spill onto another
> line.
> 
> 	/*
> 	 * The E2H0 field is a 'negative' feature field, where a value
> 	 * of -1 implies FEAT_E2H0 is not implemented but does not
> 	 * restrict the use of HCR_EL2.NV1.
> 	 */
> 	return feat_nv2(kvm) && kvm_has_feat(kvm, ID_AA64MMFR4_EL1, E2H0, NI);
> 

I really wonder why I bothered describing this, introducing a bug in
the process. There is no known universe where we can deal with NV1 not
being RES0, and this helper is purely academic (and therefore wrong).

My personal take on this particular problem is as follows.

Thanks,

	M.

diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index adaf26ab93b85..2e81f45dc9921 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -256,11 +256,6 @@ static bool feat_nv2(struct kvm *kvm)
 		kvm_has_feat(kvm, ID_AA64MMFR2_EL1, NV, NV2));
 }
 
-static bool feat_nv2_e2h0_ni(struct kvm *kvm)
-{
-	return feat_nv2(kvm) && !kvm_has_feat(kvm, FEAT_E2H0);
-}
-
 static bool feat_rasv1p1(struct kvm *kvm)
 {
 	return (kvm_has_feat(kvm, ID_AA64PFR0_EL1, RAS, V1P1) ||
@@ -1034,7 +1029,7 @@ static const struct reg_bits_to_feat_map hcr_feat_map[] = {
 		   HCR_EL2_NV2		|
 		   HCR_EL2_NV,
 		   feat_nv2),
-	NEEDS_FEAT(HCR_EL2_NV1, feat_nv2_e2h0_ni), /* Missing from JSON */
+	NEEDS_FEAT_FLAG(HCR_EL2_NV1, FORCE_RESx), /* Missing from JSON, RES0 as an IMPDEF choice */
 	NEEDS_FEAT(HCR_EL2_API		|
 		   HCR_EL2_APK,
 		   feat_pauth),

-- 
Without deviation from the norm, progress is not possible.

  reply	other threads:[~2026-11-18 10:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 20:45 [PATCH v2 0/2] KVM: arm64: Fix inverted E2H0 check for HCR_EL2.NV1 Colton Lewis
2026-11-17 20:45 ` [PATCH v2 1/2] " Colton Lewis
2026-11-17 22:46   ` Oliver Upton
2026-11-18 10:13     ` Marc Zyngier [this message]
2026-11-17 20:45 ` [PATCH v2 2/2] KVM: arm64: selftests: Verify HCR_EL2.NV1 is RES0 when E2H0 is NI_NV1 Colton Lewis
2026-11-17 22:49   ` Oliver Upton

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=86a4n61m7s.wl-maz@kernel.org \
    --to=maz@kernel.org \
    --cc=coltonlewis@google.com \
    --cc=fuad.tabba@linux.dev \
    --cc=joey.gouly@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=leo.bras@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oupton@kernel.org \
    --cc=suzuki.poulose@arm.com \
    --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