Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Anup Patel" <anup.patel@oss.qualcomm.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v5 2/2] RISC-V: KVM: AIA: Make HGEI number management fully per-CPU
Date: Mon, 25 May 2026 10:58:45 +0000	[thread overview]
Message-ID: <20260525105845.9FCDA1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260525094945.3721783-3-anup.patel@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but a Use-After-Free vulnerability exists in `kvm_riscv_aia_disable()` during concurrent VM destruction and CPU offline.
--

commit b20fb1dd06460a83c6a2cbe4fe6445306d7467e2
Author: Guo Ren (Alibaba DAMO Academy) <guoren@kernel.org>

RISC-V: KVM: AIA: Make HGEI number management fully per-CPU

This commit introduces a per-CPU field for the number of Hypervisor Guest
External Interrupt (HGEI) lines, replacing a single global variable. It
updates allocation, freeing, and disabling paths to use the probed
per-CPU values. The global value is retained to represent the minimum
number of HGEI lines across all HARTs.

> diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c
> index 5ec503288555d..bafb009c5ce55 100644
> --- a/arch/riscv/kvm/aia.c
> +++ b/arch/riscv/kvm/aia.c
[ ... ]
> @@ -588,7 +617,7 @@ void kvm_riscv_aia_disable(void)
>  
>  	raw_spin_lock_irqsave(&hgctrl->lock, flags);
>  
> -	for (i = 0; i <= kvm_riscv_aia_nr_hgei; i++) {
> +	for (i = 0; i <= hgctrl->nr_hgei; i++) {
>  		vcpu = hgctrl->owners[i];
>  		if (!vcpu)
>  			continue;

[Severity: Critical]
This is a pre-existing issue, but could this lead to a use-after-free
during concurrent VM destruction and CPU offline?

Looking at kvm_riscv_aia_disable(), the code reads the vcpu pointer and
temporarily drops the lock before using it:

    vcpu = hgctrl->owners[i];
    if (!vcpu)
        continue;

    raw_spin_unlock_irqrestore(&hgctrl->lock, flags);
    kvm_riscv_vcpu_aia_imsic_release(vcpu);

If CPU hotplug runs concurrently with VM teardown:

CPU1 (CPU offline)
kvm_riscv_aia_disable()
    vcpu = hgctrl->owners[i];
    raw_spin_unlock_irqrestore(&hgctrl->lock, flags);

CPU2 (VM teardown)
kvm_destroy_vm()
    kvm_vcpu_destroy()
        kvm_riscv_vcpu_aia_deinit()
            imsic_vsfile_cleanup()
                kvm_riscv_aia_free_hgei()
                    // acquires hgctrl->lock, clears the owners slot
        kmem_cache_free(vcpu)

CPU1 (CPU offline resumes)
kvm_riscv_aia_disable()
    kvm_riscv_vcpu_aia_imsic_release(vcpu); // dereferences freed vcpu

Since kvm_vcpu_destroy() does not use RCU for reclamation and no reference
counts are held for the vcpu while the lock is dropped, the memory appears
to be freed immediately.

Is there a mechanism preventing this race?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260525094945.3721783-1-anup.patel@oss.qualcomm.com?part=2

  reply	other threads:[~2026-05-25 10:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-25  9:49 [PATCH v5 0/2] RISC-V: KVM: Convert HGEI management to fully per-HART Anup Patel
2026-05-25  9:49 ` [PATCH v5 1/2] irqchip/riscv-imsic: Add nr_guest_files in per-HART local config Anup Patel
2026-05-25 10:18   ` sashiko-bot
2026-05-25  9:49 ` [PATCH v5 2/2] RISC-V: KVM: AIA: Make HGEI number management fully per-CPU Anup Patel
2026-05-25 10:58   ` sashiko-bot [this message]
2026-05-26  1:12   ` Guo Ren
2026-05-31 15:10 ` [PATCH v5 0/2] RISC-V: KVM: Convert HGEI management to fully per-HART Anup Patel
2026-06-26  8:21 ` patchwork-bot+linux-riscv

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=20260525105845.9FCDA1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=anup.patel@oss.qualcomm.com \
    --cc=kvm@vger.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