From: sashiko-bot@kernel.org
To: "Zhanpeng Zhang" <zhangzhanpeng.jasper@bytedance.com>,
linux-perf-users@vger.kernel.org
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks
Date: Wed, 18 Nov 2026 08:49:38 +0000 [thread overview]
Message-ID: <sashiko-outbox-220512@kernel.org> (raw)
In-Reply-To: <20261118081327.10684-11-zhangzhanpeng.jasper@bytedance.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] perf: riscv: stale pcpu->switching in pdis_del() inhibits
subsequent events
- [Medium] perf: riscv: pdis_event_update() drops period and corrupts
state on wrap
- [Medium] perf: riscv: unguarded for_each_sibling_event() in
pdis_event_init()
--
commit ae54dcd2ba8108979f94818a492fed1b6ab87dcc
Author: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
perf: riscv: Add PDIS event and counter callbacks
This commit implements perf event initialization, counter programming,
read/start/stop callbacks, and task context-switch handling for the
experimental RISC-V PDIS PMU.
> diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
> index b7671e5ad63eb..9a8fe5e4ab828 100644
> --- a/drivers/perf/riscv_pdis.c
> +++ b/drivers/perf/riscv_pdis.c
[ ... ]
> @@ -119,19 +148,299 @@ void riscv_pdis_cpu_starting(void)
> cpumask_set_cpu(cpu, &pdis_cpus);
> }
[ ... ]
> +static int pdis_event_init(struct perf_event *event)
> +{
> + struct perf_event *sibling;
> + u64 mode;
> + int ret;
> +
> + if (event->attr.type != pdis_pmu.type)
> + return -ENOENT;
> + if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK ||
> + event->attr.inherit || event->attr.freq || event->attr.exclusive ||
> + event->attr.aux_output ||
> + !is_sampling_event(event))
> + return -EOPNOTSUPP;
> + if (!cpumask_test_cpu(event->cpu, &pdis_cpus))
> + return -ENODEV;
> + if (event->attr.config > RISCV_PDIS_SELECT_CONTROL ||
> + event->attr.config1 || event->attr.config2)
> + return -EINVAL;
> + /* Discovery requires host-only execution; an explicit H is optional. */
> + if (event->attr.sample_type & ~RISCV_PDIS_SAMPLE_TYPES ||
> + event->attr.exclude_host ||
> + event->attr.exclude_idle || event->attr.precise_ip ||
> + event->attr.exclude_user == event->attr.exclude_kernel)
> + return -EOPNOTSUPP;
> + if (event->group_leader != event && event->group_leader->pmu == event->pmu)
> + return -EINVAL;
> + for_each_sibling_event(sibling, event->group_leader) {
> + if (sibling != event && sibling->pmu == event->pmu)
> + return -EINVAL;
> + }
[Severity: Medium]
Does calling for_each_sibling_event() when event->group_leader == event
trigger a lockdep splat under CONFIG_PROVE_LOCKING=y?
For a newly allocated standalone event or group leader, event->ctx is still
NULL during pmu->event_init(), and perf_try_init_event() only locks
group_leader->ctx->mutex when event->group_leader != event.
for_each_sibling_event() unconditionally runs lockdep_assert_event_ctx(),
which checks lockdep_is_held(&event->ctx->mutex) with event->ctx == NULL
and fires WARN_ON_ONCE. Guarding the loop with
if (event->group_leader != event) avoids this.
[ ... ]
> +/* COUNT is frozen by the caller; OF is sticky until acknowledged. */
> +static void pdis_event_update(struct perf_event *event)
> +{
> + struct hw_perf_event *hwc = &event->hw;
> + u64 period = hwc->last_period;
> + s64 prev = local64_read(&hwc->prev_count);
> + s64 delta, next, step;
> + u32 progress;
> +
> + do {
> + u32 count = csr_read(RISCV_PDIS_CSR_COUNTER);
> + bool pending = csr_read(CSR_SCOUNTOVF) & RISCV_PDIS_COUNTER_BIT;
> + bool accounted = prev & PDIS_OF_ACCOUNTED;
> +
> + progress = count + (u32)period;
> + /* COUNT may have overflowed without reloading yet. */
> + if (progress >= period)
> + return;
> +
> + /* OF can reveal one wrap; multiple unseen wraps cannot be recovered. */
> + step = (s64)progress - (u32)prev;
> + delta = step;
> + if (step < 0 || (pending && !accounted))
> + delta += period;
> + next = progress;
> + if (accounted || pending || step < 0)
> + next |= PDIS_OF_ACCOUNTED;
> + } while (!local64_try_cmpxchg(&hwc->prev_count, &prev, next));
> +
> + local64_add(delta, &event->count);
> + /* Apply the position change without overwriting a nested update. */
> + local64_sub(step, &hwc->period_left);
> +}
> +
> +static void pdis_update_and_quiesce(struct perf_event *event)
> +{
> + csr_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
> + pdis_event_update(event);
> + pdis_quiesce();
> +}
[Severity: Medium]
Can returning early on progress >= period in pdis_event_update() lose the
elapsed period and leave the counter state out of sync when called from
pdis_update_and_quiesce()?
If COUNT has wrapped past 0xffffffff and is waiting for an in-flight
selection to retire and reload INITVAL when pdis_stop() or
pdis_sched_task() calls pdis_update_and_quiesce(), pdis_event_update()
returns without updating event->count, hwc->prev_count, or
hwc->period_left, and pcpu->counter_valid remains true. pdis_quiesce()
then writes 0 to RISCV_PDIS_CSR_CTL (cancelling the in-flight selection)
and clears PDIS_OF_ACCOUNTED.
Because pcpu->counter_valid is still true, a subsequent sched-in or
non-reloading pdis_start() calls pdis_program(event, false) without
reloading RISCV_PDIS_CSR_COUNTER. The counts up to the wrap are lost from
event->count, hwc->period_left keeps its stale value across a stop, and if
cancelling the selection leaves COUNT at 0, every subsequent
pdis_event_update() continues to see progress >= period until COUNT
increments through 2^32 - period events.
[ ... ]
> +static void pdis_del(struct perf_event *event, int flags)
> +{
> + struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
> +
> + pdis_stop(event, PERF_EF_UPDATE);
> + pcpu->event = NULL;
> + pcpu->counter_valid = false;
> + perf_sched_cb_dec(event->pmu);
> +}
[ ... ]
> +/* Drop pending selections before changing the task used for sample metadata. */
> +static void pdis_sched_task(struct perf_event_pmu_context *ctx,
> + struct task_struct *task, bool sched_in)
> +{
> + struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
> + struct perf_event *event = pcpu->event;
> +
> + pcpu->switching = !sched_in;
> + if (!pcpu->ready || !event || (event->hw.state & PERF_HES_STOPPED))
> + return;
[Severity: Medium]
Can pdis_del() leave pcpu->switching stuck at true if the event is removed
during a context switch?
On task switch-out, __perf_event_task_sched_out() calls
perf_pmu_sched_task(task, next, false), which runs pdis_sched_task() and
sets pcpu->switching = true. Before perf_pmu_sched_task(prev, task, true)
runs on switch-in, perf_event_context_sched_in() can schedule out a
flexible CPU-wide PDIS event when the incoming task has a pinned task
event:
__perf_event_task_sched_out()
perf_pmu_sched_task(..., false)
pdis_sched_task(..., false)
pcpu->switching = true
__perf_event_task_sched_in()
perf_event_context_sched_in()
ctx_sched_out(&cpuctx->ctx, NULL, EVENT_FLEXIBLE)
event_sched_out()
pdis_del()
perf_sched_cb_dec(event->pmu)
If the PDIS event is not re-added in cpu_ctx_sched_in() (for example,
because event->pending_disable transitioned it to PERF_EVENT_STATE_OFF in
event_sched_out()), perf_sched_cb_dec() has already removed pdis_pmu from
sched_cb_list, so perf_pmu_sched_task(prev, task, true) never calls
pdis_sched_task() to clear pcpu->switching. Any subsequent PDIS event
added or started on that CPU outside a context switch will have
pdis_enable() skip clearing CSR_SCOUNTINHIBIT, leaving the counter
inhibited.
In addition, pdis_event_init() does not reject
is_cgroup_event(event->group_leader), so a PDIS event grouped under a
cgroup software leader can be scheduled out by perf_cgroup_switch() at the
end of __perf_event_task_sched_out() after pdis_sched_task(..., false) has
already set pcpu->switching = true.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118081327.10684-7-zhangzhanpeng.jasper@bytedance.com?part=4
next prev parent reply other threads:[~2026-11-18 8:49 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions Zhanpeng Zhang
2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:23 ` sashiko-bot
2026-11-18 8:13 ` [RFC PATCH v1 2/5] riscv: Detect " Zhanpeng Zhang
2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:20 ` sashiko-bot
2026-11-18 8:13 ` [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs Zhanpeng Zhang
2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:39 ` sashiko-bot
2026-11-18 8:13 ` [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks Zhanpeng Zhang
2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:49 ` sashiko-bot [this message]
2026-11-18 8:13 ` [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt Zhanpeng Zhang
2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:47 ` 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-220512@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=robh@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=zhangzhanpeng.jasper@bytedance.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.