All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marc Zyngier <maz@kernel.org>
To: "Oliver Upton" <oupton@kernel.org>
Cc: "Dan Carpenter" <dan@pereq.eu>,
	kvmarm@lists.linux.dev, sashiko-reviews@lists.linux.dev
Subject: Re: [PATCH v2 1/2] KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS
Date: Wed, 18 Nov 2026 17:44:06 +0000	[thread overview]
Message-ID: <86zev6yqzd.wl-maz@kernel.org> (raw)
In-Reply-To: <sashiko-outbox-221074@kernel.org>

On Wed, 18 Nov 2026 13:45:34 +0000,
sashiko-bot@kernel.org wrote:
> 
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] KVM: arm64: stale mmio_needed after kvm_inject_nested() in
>   KVM_SET_VCPU_EVENTS
> --
> 
> commit 178d0de7451612b8ddf14a943cb82094efbd77d1
> Author: Dan Carpenter <dan@pereq.eu>
> 
> KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS
> 
> This commit wraps __kvm_arm_vcpu_set_events() in vcpu_load() and vcpu_put()
> so that kvm_inject_nested() operates on a loaded vCPU during
> KVM_SET_VCPU_EVENTS.
> 
> > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
> > index 84a52e27ed859..a148e80d0aef7 100644
> > --- a/arch/arm64/kvm/arm.c
> > +++ b/arch/arm64/kvm/arm.c
> > @@ -2002,7 +2002,7 @@ static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
> >  static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
> >  				   struct kvm_vcpu_events *events)
> >  {
> > -	int i;
> > +	int i, ret;
> >  
> >  	/* check whether the reserved field is zero */
> >  	for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
> > @@ -2014,7 +2014,11 @@ static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
> >  		if (events->exception.pad[i])
> >  			return -EINVAL;
> >  
> > -	return __kvm_arm_vcpu_set_events(vcpu, events);
> > +	vcpu_load(vcpu);
> > +	ret = __kvm_arm_vcpu_set_events(vcpu, events);
> > +	vcpu_put(vcpu);
> > +
> > +	return ret;
> >  }
> 
> [Severity: Medium]
> Does vcpu->mmio_needed remain set when KVM_SET_VCPU_EVENTS injects an
> exception from L2 that routes to L1 EL2?
> 
> When an L2 guest exits with KVM_EXIT_MMIO and userspace aborts the access via
> KVM_SET_VCPU_EVENTS (for example with ext_dabt_pending = 1 while HCR_EL2.TEA
> is set), kvm_inject_sea_dabt() -> kvm_inject_nested() commits the EL2
> exception entry directly via __kvm_adjust_pc(), which clears
> PENDING_EXCEPTION.  When __kvm_arm_vcpu_set_events() then calls
> commit_pending_events(), PENDING_EXCEPTION is already clear, so it returns
> early without clearing vcpu->mmio_needed:
> 
> arch/arm64/kvm/guest.c:commit_pending_events() {
> 	if (!vcpu_get_flag(vcpu, PENDING_EXCEPTION))
> 		return;
> 
> 	vcpu->mmio_needed = false;
> 	kvm_call_hyp(__kvm_adjust_pc, vcpu);
> }
> 
> On the next KVM_RUN, kvm_handle_mmio_return() sees vcpu->mmio_needed still
> set, writes the MMIO read value into L1 EL2's destination register and
> advances L1 EL2's exception vector PC by 4 bytes via kvm_incr_pc().

I think this is a separate issue, which doesn't intersect with the
current patch. The problem is that we have multiple ways to "commit'
the state on exception delivery, and that we miss MMIO handling in the
EL1->EL2 path.

Oliver, WDYT?

	M.

diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 97332ffcb1300..33bced55ce3bb 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -1352,6 +1352,7 @@ int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
 int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
 			      struct kvm_vcpu_events *events);
 
+void commit_pending_events(struct kvm_vcpu *vcpu);
 void kvm_arm_halt_guest(struct kvm *kvm);
 void kvm_arm_resume_guest(struct kvm *kvm);
 void kvm_pause_vcpu(struct kvm_vcpu *vcpu);
diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c
index d04ac52034541..83272adefc4c0 100644
--- a/arch/arm64/kvm/emulate-nested.c
+++ b/arch/arm64/kvm/emulate-nested.c
@@ -2900,7 +2900,7 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	 * We may have an exception or PC update in the EL0/EL1 context.
 	 * Commit it before entering EL2.
 	 */
-	__kvm_adjust_pc(vcpu);
+	commit_pending_events(vcpu);
 
 	kvm_arch_vcpu_put(vcpu);
 
diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c
index 878ea4500c5c5..595b23b29af89 100644
--- a/arch/arm64/kvm/guest.c
+++ b/arch/arm64/kvm/guest.c
@@ -770,14 +770,16 @@ int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
 	return 0;
 }
 
-static void commit_pending_events(struct kvm_vcpu *vcpu)
+void commit_pending_events(struct kvm_vcpu *vcpu)
 {
-	if (!vcpu_get_flag(vcpu, PENDING_EXCEPTION))
+	if (!vcpu_get_flag(vcpu, PENDING_EXCEPTION) &&
+	    !vcpu_get_flag(vcpu, INCREMENT_PC))
 		return;
 
 	/*
 	 * Reset the MMIO emulation state to avoid stepping PC after emulating
-	 * the exception entry.
+	 * the exception entry. This also applies if for any reason, we adjust
+	 * PC while in an MMIO phase. But that's rather dodgy...
 	 */
 	vcpu->mmio_needed = false;
 	kvm_call_hyp(__kvm_adjust_pc, vcpu);

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

  reply	other threads:[~2026-11-18 17:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 13:05 [PATCH v2 0/2] KVM: arm64: nv: Fix a couple NULL dereferences in kvm_inject_nested() Dan Carpenter
2026-11-18 13:05 ` [PATCH v2 1/2] KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS Dan Carpenter
2026-11-18 13:45   ` sashiko-bot
2026-11-18 17:44     ` Marc Zyngier [this message]
2026-11-19  8:18       ` Dan Carpenter
2026-11-19  9:53         ` Marc Zyngier
2026-11-18 13:05 ` [PATCH v2 2/2] KVM: arm64: nv: Fix NULL dereference in kvm_pmu_nested_transition() Dan Carpenter
2026-11-19 12:43 ` [PATCH v2 0/2] KVM: arm64: nv: Fix a couple NULL dereferences in kvm_inject_nested() 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=86zev6yqzd.wl-maz@kernel.org \
    --to=maz@kernel.org \
    --cc=dan@pereq.eu \
    --cc=kvmarm@lists.linux.dev \
    --cc=oupton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.