All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Zong Li" <zong.li@sifive.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v12 1/2] drivers/perf: riscv-iommu: add risc-v iommu pmu driver
Date: Fri, 18 Sep 2026 07:32:20 +0000	[thread overview]
Message-ID: <20260918073220.999291F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260918072027.639239-2-zong.li@sifive.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] perf: data corruption due to missing lock in pmu->read()
--

commit 7fd0bb502a12a89e86b91b6b0afc0e10a1763701
Author: Zong Li <zong.li@sifive.com>

drivers/perf: riscv-iommu: add risc-v iommu pmu driver

This commit adds a new driver to support the RISC-V IOMMU PMU, an auxiliary
device driver created by the parent RISC-V IOMMU driver. It implements
hardware counter detection and separates the cycle counter from event
counters. Raw spin locks are used to avoid CPU hotplug races and
IRQCHIP_MOVE_DEFERRED issues.

Link: https://lore.kernel.org/linux-riscv/20260618143634.7f3dd6c5@pumpkin/

> diff --git a/drivers/perf/riscv_iommu_pmu.c b/drivers/perf/riscv_iommu_pmu.c
> new file mode 100644
> index 0000000000000..ac4d225cca85f
> --- /dev/null
> +++ b/drivers/perf/riscv_iommu_pmu.c

[ ... ]

> +static void riscv_iommu_pmu_update(struct perf_event *event)
> +{
> +	struct hw_perf_event *hwc = &event->hw;
> +	struct riscv_iommu_pmu *pmu = to_riscv_iommu_pmu(event->pmu);
> +	u64 delta, prev, now;
> +	u32 idx = hwc->idx;
> +	u64 counter_mask = idx ? pmu->event_cntr_mask : pmu->cycle_cntr_mask;
> +
> +	/*
> +	 * riscv_iommu_pmu_set_period() resets the hardware counter and
> +	 * prev_count as two separate writes.
> +	 * Pairing a "prev" read from before that reset with a "now" read from
> +	 * after it would produce a nonsensical, possibly huge, delta.
> +	 * Re-checking prev_count after the hardware read detects that torn
> +	 * pairing and retries.
> +	 */
> +	do {
> +		prev = local64_read(&hwc->prev_count);
> +		now = riscv_iommu_pmu_get_counter(pmu, idx);
> +	} while (prev != local64_read(&hwc->prev_count) ||
> +		 local64_cmpxchg(&hwc->prev_count, prev, now) != prev);
> +
> +	delta = (now - prev) & counter_mask;
> +	local64_add(delta, &event->count);
> +}

[ ... ]

> +static void riscv_iommu_pmu_read(struct perf_event *event)
> +{
> +	riscv_iommu_pmu_update(event);

[Severity: High]
Does this missing pmu->lock expose the lockless update loop to a race with
the IRQ handler?

The commit message explicitly notes that due to PCI MSI/MSI-X on IMSIC
setting IRQCHIP_MOVE_DEFERRED, the IRQ handler can run on a different CPU
than the CPU the events are bound to.

If the IRQ handler on CPU A calls riscv_iommu_pmu_set_period() concurrently,
it writes the new hardware counter value and then updates prev_count. If an
unprivileged user reads the perf event fd on CPU B, triggering this
riscv_iommu_pmu_read() call without holding pmu->lock, the lockless loop in
riscv_iommu_pmu_update() can read the old prev_count and the newly updated
hardware counter.

This would allow the cmpxchg to succeed before the IRQ handler updates
prev_count, resulting in a massive bogus delta being calculated and added
to the performance event count.

> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260918072027.639239-1-zong.li@sifive.com?part=1

  reply	other threads:[~2026-09-18  7:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-18  7:20 [PATCH v12 0/2] RISC-V IOMMU HPM support Zong Li
2026-09-18  7:20 ` Zong Li
2026-09-18  7:20 ` [PATCH v12 1/2] drivers/perf: riscv-iommu: add risc-v iommu pmu driver Zong Li
2026-09-18  7:20   ` Zong Li
2026-09-18  7:32   ` sashiko-bot [this message]
2026-09-18  7:20 ` [PATCH v12 2/2] iommu/riscv: create a auxiliary device for HPM Zong Li
2026-09-18  7:20   ` Zong Li
2026-09-18  7:26   ` 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=20260918073220.999291F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=zong.li@sifive.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 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.