Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Dan Carpenter" <dan@pereq.eu>
Cc: kvmarm@lists.linux.dev, kvm@vger.kernel.org,
	"Oliver Upton" <oupton@kernel.org>,
	"Marc Zyngier" <maz@kernel.org>
Subject: Re: [PATCH v4 7/9] KVM: arm64: nv: Don't deliver a pending SError to the wrong guest
Date: Fri, 04 Dec 2026 09:17:16 +0000	[thread overview]
Message-ID: <sashiko-outbox-249482@kernel.org> (raw)
In-Reply-To: <20261204-double_exception_v4-v4-7-523921511673@pereq.eu>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] KVM: arm64: non-atomic hcr_el2 RMW in kvm_nested_stash_serror()
  races
--

commit 9a7da4512bb06d4ccc09ceb91f2bedac7608acb2
Author: Dan Carpenter <dan@pereq.eu>

KVM: arm64: nv: Don't deliver a pending SError to the wrong guest

This commit adds kvm_nested_stash_serror() to clear HCR_EL2_VSE and set
NESTED_SERROR_PENDING when switching between L1 and L2 in
kvm_emulate_nested_eret() and kvm_inject_nested().

> diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c
> index ee71dfc4b8617..d1446081fbbf9 100644
> --- a/arch/arm64/kvm/emulate-nested.c
> +++ b/arch/arm64/kvm/emulate-nested.c
> @@ -2792,6 +2792,18 @@ static u64 kvm_check_illegal_exception_return(struct kvm_vcpu *vcpu, u64 spsr)
>  	return spsr;
>  }
>  
> +/*
> + * HCR_EL2.VSE was set for the context we are leaving.  Store the SError
> + * instead, so that kvm_nested_flush_hwstate() routes it in the new context.
> + */
> +static void kvm_nested_stash_serror(struct kvm_vcpu *vcpu)
> +{
> +	if (*vcpu_hcr(vcpu) & HCR_EL2_VSE) {
> +		*vcpu_hcr(vcpu) &= ~HCR_EL2_VSE;
> +		vcpu_set_flag(vcpu, NESTED_SERROR_PENDING);
> +	}
> +}

[Severity: Low]
Can the non-atomic read-modify-write on *vcpu_hcr(vcpu) race with
vcpu_interrupt_line()?

Before an in-kernel vGIC is created on a nested-virt vCPU, one thread can
reach kvm_nested_stash_serror() under vcpu->mutex via KVM_SET_VCPU_EVENTS
(kvm_inject_sea_dabt() -> kvm_inject_nested()).

Concurrently, another thread can call KVM_IRQ_LINE with
KVM_ARM_IRQ_TYPE_CPU, which does not take vcpu->mutex and updates
HCR_EL2_VI or HCR_EL2_VF atomically in the same word via
vcpu_interrupt_line():

CPU0                                   CPU1
kvm_arm_vcpu_set_events()
  ...
  kvm_nested_stash_serror()
    load *vcpu_hcr(vcpu)
                                       kvm_vm_ioctl_irq_line()
                                         vcpu_interrupt_line()
                                           test_and_set_bit(VI, hcr)
    store (*vcpu_hcr(vcpu) & ~VSE)     <-- VI update lost

This is a data race that can lose a concurrent set of HCR_EL2_VI or
HCR_EL2_VF, or overwrite a concurrent clear with a stale 1.

Using test_and_clear_bit(__ffs(HCR_EL2_VSE), vcpu_hcr(vcpu)) would avoid
the non-atomic read-modify-write here.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-double_exception_v4-v4-0-523921511673@pereq.eu?part=7

  reply	other threads:[~2026-12-04  9:17 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  8:48 [PATCH v4 0/9] KVM: arm64: Fix kvm_reset_vcpu() and SError handling for nested virt Dan Carpenter
2026-12-04  8:48 ` [PATCH v4 1/9] KVM: arm64: nv: Use a vcpu request to reprogram the PMU Dan Carpenter
2026-12-04  8:48 ` [PATCH v4 2/9] KVM: arm64: Drop an incomplete MMIO access when resetting a vcpu Dan Carpenter
2026-12-04  9:24   ` Steffen Eiden
2026-12-04  8:48 ` [PATCH v4 3/9] KVM: arm64: Reset SP_EL1, ELR_EL1 and SPSR_EL1 " Dan Carpenter
2026-12-04  8:48 ` [PATCH v4 4/9] KVM: arm64: Discard a pending SError " Dan Carpenter
2026-12-04  9:03   ` sashiko-bot
2026-12-04  8:48 ` [PATCH v4 5/9] KVM: arm64: nv: Discard nested SError in kvm_reset_vcpu() Dan Carpenter
2026-12-04  9:03   ` sashiko-bot
2026-12-04  8:48 ` [PATCH v4 6/9] KVM: arm64: nv: Replace a pending SError when injecting a new one Dan Carpenter
2026-12-04  8:48 ` [PATCH v4 7/9] KVM: arm64: nv: Don't deliver a pending SError to the wrong guest Dan Carpenter
2026-12-04  9:17   ` sashiko-bot [this message]
2026-12-04  8:48 ` [PATCH v4 8/9] KVM: arm64: nv: Re-inject a stored SError after the exit is handled Dan Carpenter
2026-12-04  9:22   ` sashiko-bot
2026-12-04  8:48 ` [PATCH v4 9/9] KVM: arm64: selftests: Test that a vcpu reset clears the vcpu's state Dan Carpenter
2026-12-04  9:20   ` sashiko-bot

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=sashiko-outbox-249482@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dan@pereq.eu \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox