Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Gautam Menghani" <gautam@linux.ibm.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v2 3/3] powerpc/perf: Update prev_count of event to get accurate values
Date: Thu, 13 Aug 2026 10:01:02 +0000	[thread overview]
Message-ID: <20260813100103.001361F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260813094535.10083-4-gautam@linux.ibm.com>

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

New issues:
- [Medium] Missing atomic cmpxchg loop in vpa_pmu_read() when updating prev_count.

Pre-existing issues:
- [High] The VPA PMU `add` callback stores a raw pointer to `struct kvm_vcpu` in `event->pmu_private` without reference counting, leading to a Use-After-Free.
--

--- Patch [3]: [PATCH v2 3/3] powerpc/perf: Update prev_count of event to get accurate values ---
commit 7c392d724ba481a5282a90aed95397fac4533493
Author: Gautam Menghani <gautam@linux.ibm.com>

powerpc/perf: Update prev_count of event to get accurate values

This patch updates the VPA PMU read function to keep event->hw.prev_count
current. This prevents stale values from being returned on consecutive reads
while a process is running on a CPU.

> diff --git a/arch/powerpc/perf/vpa-pmu.c b/arch/powerpc/perf/vpa-pmu.c
> index e79d98447c743..efc39f71553bf 100644
> --- a/arch/powerpc/perf/vpa-pmu.c
> +++ b/arch/powerpc/perf/vpa-pmu.c
> @@ -156,6 +156,7 @@ static void vpa_pmu_read(struct perf_event *event)
>  	final_data = new_data - prev_data;
>  
>  	local64_add(final_data, &event->count);
> +	local64_set(&event->hw.prev_count, new_data);

[Severity: Medium]
Is it safe to use local64_set() here without a local64_cmpxchg() loop?

If vpa_pmu_read() is interrupted by an NMI (such as a hardware PMI) and a BPF
program attached to the NMI reads the same event via bpf_perf_event_read(),
the nested read will calculate its delta and update prev_count.

When the outer vpa_pmu_read() resumes, it could calculate its delta using the
old prev_data (resulting in double-counting the inner delta) and then
non-atomically overwrite prev_count.

Should this use a local64_cmpxchg() loop to safely update prev_count?

>  }
>  
>  static void vpa_pmu_del(struct perf_event *event, int flags)

[Severity: High]
This is a pre-existing issue, but does this code leave a stale vcpu pointer
in event->pmu_private that can lead to a use-after-free?

Looking at vpa_pmu_add(), a raw pointer to struct kvm_vcpu is stored without
taking a reference:

arch/powerpc/perf/vpa-pmu.c:vpa_pmu_add() {
    ...
    vcpu = local_paca->kvm_hstate.kvm_vcpu;
    if (!vcpu)
        goto out;
    ...
    event->pmu_private = vcpu;
}

When the thread context-switches out, vpa_pmu_del() stops the event but
does not clear event->pmu_private to NULL. When the thread is scheduled
back in outside of KVM, vpa_pmu_add() sees the vcpu is NULL and returns
early, leaving the event active with the stale vcpu pointer.

If the VM is then destroyed and the vcpu freed, would a subsequent thread
exit or user read() calling vpa_pmu_read() trigger a use-after-free when
dereferencing vcpu->arch.l1_to_l2_cs?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260813094535.10083-1-gautam@linux.ibm.com?part=3

      reply	other threads:[~2026-08-13 10:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-13  9:45 [PATCH v2 0/3] powerpc/perf: Fix vpa-pmu driver to report correct numbers Gautam Menghani
2026-08-13  9:45 ` [PATCH v2 1/3] KVM: PPC: Book3S HV: Maintain aggregate context switch values for vCPUs Gautam Menghani
2026-08-13 10:03   ` sashiko-bot
2026-08-13  9:45 ` [PATCH v2 2/3] powerpc/perf: Use the aggregate context switch values from vcpu struct Gautam Menghani
2026-08-13  9:58   ` sashiko-bot
2026-08-13  9:45 ` [PATCH v2 3/3] powerpc/perf: Update prev_count of event to get accurate values Gautam Menghani
2026-08-13 10:01   ` sashiko-bot [this message]

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=20260813100103.001361F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gautam@linux.ibm.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