From: sashiko-bot@kernel.org
To: "Vikram Singh" <vikram.singh3@oss.qualcomm.com>
Cc: linux-perf-users@vger.kernel.org, devicetree@vger.kernel.org,
robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v5 6/7] riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver
Date: Wed, 18 Nov 2026 10:21:03 +0000 [thread overview]
Message-ID: <sashiko-outbox-220678@kernel.org> (raw)
In-Reply-To: <20261118095053.3871267-7-vikram.singh3@oss.qualcomm.com>
Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [Critical] riscv: pmu: perf_ctx_data type confusion and OOB write in
CTR callbacks
- [High] riscv: pmu: duplicate cpuhp_state_add_instance() in
pmu_sbi_device_probe()
- [High] riscv: pmu: NULL ctx->task deref in riscv_pmu_ctr_add() on CPU
events
- [Medium] riscv: pmu: IRQ cleanup bypassed on error in
pmu_sbi_device_probe()
- [Medium] riscv: pmu: per-CPU branch_records leak in
pmu_sbi_device_probe()
- [Medium] riscv: pmu: broken CTR state across concurrent events in SBI
PMU
- [Medium] riscv: pmu: premature -EOPNOTSUPP in pmu_sbi_event_map()
before type check
- [Medium] riscv: pmu: section mismatch calling __init
check_available_filters()
- [Medium] riscv: pmu: missing inline on riscv_pmu_ctr_add() and del()
stubs
--
commit e244663b591d7b5c6a7f72d19726b56679ea8ed0
Author: Vikram Singh <vikram.singh3@oss.qualcomm.com>
riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver
This commit integrates Control Transfer Records (CTR) extension support
into the RISC-V SBI PMU driver to enable branch stack sampling on PMU
events.
Link: https://lore.kernel.org/r/20261106173942.3175069-7-vikram.singh3@oss.qualcomm.com
> diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
> index b14b3acf75674..637b9d97e20a4 100644
> --- a/drivers/perf/riscv_pmu_sbi.c
> +++ b/drivers/perf/riscv_pmu_sbi.c
> @@ -678,6 +678,9 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
> */
> flush_work(&check_std_events_work);
>
> + if (needs_branch_stack(event) && !riscv_pmu_ctr_valid(event))
> + return -EOPNOTSUPP;
> +
> return riscv_pmu_get_event_info(type, config, econfig);
> }
[Severity: Medium]
Does returning -EOPNOTSUPP before riscv_pmu_get_event_info() break PMU
iteration in perf_init_event() for events targeting another PMU?
struct pmu::event_init must return -ENOENT when event->attr.type is not
handled by this PMU. Checking riscv_pmu_ctr_valid() before
riscv_pmu_get_event_info() validates type causes perf_init_event() to
abort searching other registered PMUs whenever a non-CPU event requests
branch filters outside allowed_filters.
> @@ -827,10 +830,14 @@ static void pmu_sbi_reset_scounteren(void *arg)
>
> static void pmu_sbi_ctr_add(struct perf_event *event, int flags)
> {
> + if (needs_branch_stack(event))
> + riscv_pmu_ctr_add(event);
> }
[Severity: High]
Does riscv_pmu_ctr_add() (and riscv_pmu_ctr_del()) cause a NULL pointer
dereference when scheduling a per-CPU event with
PERF_SAMPLE_BRANCH_USER | PERF_SAMPLE_BRANCH_CALL_STACK?
For a CPU-wide event (pid == -1), event->ctx->task is NULL, but
riscv_pmu_ctr_add() and riscv_pmu_ctr_del() dereference event->ctx->task
unconditionally:
drivers/perf/riscv_ctr.c:riscv_pmu_ctr_add() {
if (branch_user_callstack(event->attr.branch_sample_type) &&
event->ctx->task->perf_ctx_data)
task_context(event->ctx->task->perf_ctx_data)->callstack_users++;
...
}
> static void pmu_sbi_ctr_del(struct perf_event *event, int flags)
> {
> + if (needs_branch_stack(event))
> + riscv_pmu_ctr_del(event);
> }
[Severity: Medium]
When CONFIG_RISCV_PMU=y and CONFIG_RISCV_CTR=n, do the
riscv_pmu_ctr_add() and riscv_pmu_ctr_del() stubs in
include/linux/perf/riscv_pmu.h trigger -Wunused-function warnings in
other translation units?
include/linux/perf/riscv_pmu.h:
static void riscv_pmu_ctr_add(struct perf_event *event) { }
static void riscv_pmu_ctr_del(struct perf_event *event) { }
Both stubs are missing the inline keyword, so files such as
drivers/perf/riscv_pmu.c and arch/riscv/kvm/vcpu_pmu.c that include this
header without calling them fail to build under CONFIG_WERROR=y.
[ ... ]
> @@ -864,6 +875,9 @@ static void pmu_sbi_ctr_stop(struct perf_event *event, unsigned long flag)
> struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
> struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
>
> + if (needs_branch_stack(event) && flag != RISCV_PMU_STOP_FLAG_RESET)
> + riscv_pmu_ctr_disable(event);
> +
> if ((hwc->flags & PERF_EVENT_FLAG_USER_ACCESS) &&
> (hwc->flags & PERF_EVENT_FLAG_USER_READ_CNT))
> pmu_sbi_reset_scounteren((void *)event);
> @@ -896,7 +910,7 @@ static void pmu_sbi_ctr_stop(struct perf_event *event, unsigned long flag)
> static void pmu_sbi_sched_task(struct perf_event_pmu_context *pmu_ctx, struct task_struct *task,
> bool sched_in)
> {
> - /* Call CTR specific Sched hook. */
> + riscv_pmu_ctr_sched_task(pmu_ctx, task, sched_in);
> }
[Severity: Critical]
Does wiring up riscv_pmu_ctr_sched_task() alongside
PERF_ATTACH_TASK_DATA in pmu_sbi_event_init() cause an out-of-bounds
heap write into task->perf_ctx_data?
When PERF_ATTACH_TASK_DATA is set, core perf allocates a 40-byte
struct perf_ctx_data wrapper at task->perf_ctx_data and places the
struct riscv_perf_task_context allocation in task->perf_ctx_data->data.
However, riscv_pmu_ctr_sched_task(), riscv_pmu_ctr_add() and
riscv_pmu_ctr_del() cast task->perf_ctx_data directly to
struct riscv_perf_task_context * without RCU protection or dereferencing
->data:
drivers/perf/riscv_ctr.c:riscv_pmu_ctr_sched_task() {
...
/* Save branch records in task_ctx on sched out */
task_ctx = pmu_ctx ? task->perf_ctx_data : NULL;
if (task_ctx) {
if (sched_in)
riscv_pmu_ctr_restore(task_ctx);
else
riscv_pmu_ctr_save(task_ctx, rvpmu->ctr_depth);
return;
}
...
}
When a task monitored with PERF_SAMPLE_BRANCH_CALL_STACK schedules out,
riscv_pmu_ctr_save() overwrites the struct perf_ctx_data fields
(including ctx_cache and data) and writes up to 6144 bytes of branch
records past the end of the 40-byte slab object. Compare with
intel_pmu_lbr_sched_task(), which accesses
rcu_dereference(task->perf_ctx_data)->data under rcu_read_lock().
> @@ -1179,6 +1193,12 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
> hw_evt->state |= PERF_HES_UPTODATE;
> perf_sample_data_init(&data, 0, hw_evt->last_period);
> if (riscv_pmu_event_set_period(event)) {
> + if (needs_branch_stack(event)) {
> + riscv_pmu_ctr_consume(cpu_hw_evt, event);
> + perf_sample_save_brstack(&data, event,
> + &cpu_hw_evt->branches->branch_stack, NULL);
> + }
> +
[Severity: Medium]
How is CTR hardware state managed when multiple events are active on the
same CPU?
First, in pmu_sbi_ctr_stop(), riscv_pmu_ctr_disable() clears CSR_CTRCTL
unconditionally even when other active events on the CPU still have
needs_branch_stack(event) set. Second, because CTRCTL_LCOFIFRZ is set,
any local counter overflow interrupt freezes CTR by setting
SCTRSTATUS_FROZEN, but pmu_sbi_ovf_handler() only clears
SCTRSTATUS_FROZEN inside riscv_pmu_ctr_consume(); if a non-branch event
overflows (or riscv_pmu_event_set_period() returns 0), CTR remains
frozen for active branch-sampling events. Third, riscv_pmu_ctr_consume()
re-enables CSR_CTRCTL and clears SCTRSTATUS_FROZEN before
perf_event_overflow() and pmu_sbi_start_overflow_mask() run, so kernel
branches during overflow handling are recorded while the PMU counters are
still stopped.
[ ... ]
> @@ -1367,7 +1391,30 @@ static void riscv_pmu_destroy(struct riscv_pmu *pmu)
> cpuhp_state_remove_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
> }
>
> -static void pmu_sbi_event_init(struct perf_event *event)
> +static int branch_records_alloc(struct riscv_pmu *pmu)
> +{
> + struct branch_records __percpu *tmp_alloc_ptr;
> + struct branch_records *records;
> + struct cpu_hw_events *events;
> + int cpu;
> +
> + if (!riscv_pmu_ctr_supported(pmu))
> + return 0;
> +
> + tmp_alloc_ptr = alloc_percpu_gfp(struct branch_records, GFP_KERNEL);
> + if (!tmp_alloc_ptr)
> + return -ENOMEM;
> +
> + for_each_possible_cpu(cpu) {
> + events = per_cpu_ptr(pmu->hw_events, cpu);
> + records = per_cpu_ptr(tmp_alloc_ptr, cpu);
> + events->branches = records;
> + }
> +
> + return 0;
> +}
[Severity: Medium]
Does branch_records_alloc() leak the per-CPU tmp_alloc_ptr allocation if
pmu_sbi_device_probe() fails later?
tmp_alloc_ptr is only resolved via per_cpu_ptr() into events->branches,
and the __percpu base pointer is discarded on return, so neither the
probe error path nor riscv_pmu_destroy() ever calls free_percpu() on it.
[ ... ]
> @@ -1516,9 +1568,16 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)
> pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
> }
> irq_requested = (ret == 0);
> -
> pmu->pmu.attr_groups = riscv_pmu_attr_groups;
> pmu->pmu.parent = &pdev->dev;
> + ret = riscv_pmu_ctr_init(pmu);
> + if (ret)
> + goto out_free;
[Severity: Medium]
Does calling riscv_pmu_ctr_init() from pmu_sbi_device_probe() cause a
section mismatch with check_available_filters()?
pmu_sbi_device_probe() and riscv_pmu_ctr_init() are in .text, but
riscv_pmu_ctr_init() calls check_available_filters(), which is marked
__init in drivers/perf/riscv_ctr.c. This triggers a modpost section
mismatch warning and will execute freed initmem if the platform device
is probed after boot.
> +
> + ret = branch_records_alloc(pmu);
> + if (ret)
> + goto out_ctr_finish;
> +
> bitmap_copy(pmu->cmask, cmask, RISCV_MAX_COUNTERS);
> pmu->ctr_add = pmu_sbi_ctr_add;
> pmu->ctr_del = pmu_sbi_ctr_del;
> @@ -1535,6 +1594,10 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)
> pmu->csr_index = pmu_sbi_csr_index;
> pmu->sched_task = pmu_sbi_sched_task;
>
> + ret = cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
> + if (ret)
> + goto out_ctr_finish;
> +
[Severity: High]
Doesn't this register &pmu->node with cpuhp_state_add_instance() a
second time?
pmu_sbi_device_probe() already registers &pmu->node with
CPUHP_AP_PERF_RISCV_STARTING after register_sysctl():
drivers/perf/riscv_pmu_sbi.c:pmu_sbi_device_probe() {
...
register_sysctl("kernel", sbi_pmu_sysctl_table);
ret = cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
if (ret)
goto out_unregister;
...
}
Adding the same hlist_node twice corrupts the CPUHP_AP_PERF_RISCV_STARTING
instance list into a circular self-loop (pmu->node.next = &pmu->node) and
runs pmu_sbi_starting_cpu() twice per online CPU (with the first pass
running before SBI PMU snapshot setup).
> ret = riscv_pm_pmu_register(pmu);
> if (ret)
> goto out_destroy;
> @@ -1592,6 +1655,9 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)
> riscv_pmu_irq = 0;
> }
>
> +out_ctr_finish:
> + riscv_pmu_ctr_finish(pmu);
> +
> out_free:
> free_percpu(pmu->hw_events);
> kfree(pmu_ctr_list);
[Severity: Medium]
Do the error paths jumping to out_ctr_finish and out_free bypass freeing
riscv_pmu_irq?
pmu_sbi_setup_irqs() requests the per-CPU IRQ with pmu->hw_events as the
dev_id token before riscv_pmu_ctr_init() and branch_records_alloc() run.
Because out_ctr_finish and out_free are placed below the irq_requested
cleanup block in out_destroy, failing in riscv_pmu_ctr_init(),
branch_records_alloc(), or the new cpuhp_state_add_instance() call frees
pmu->hw_events while the per-CPU IRQ remains registered with that freed
pointer.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118095053.3871267-1-vikram.singh3@oss.qualcomm.com?part=6
next prev parent reply other threads:[~2026-11-18 10:21 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 9:50 [PATCH v5 0/7] riscv: pmu: Add support for Control Transfer Records Ext Vikram Singh
2026-11-18 9:50 ` [PATCH v5 1/7] perf: Increase the maximum number of branches remove_loops() can process Vikram Singh
2026-11-18 9:50 ` [PATCH v5 2/7] riscv: pmu: Add Control transfer records CSR definations Vikram Singh
2026-11-18 10:00 ` sashiko-bot
2026-11-18 9:50 ` [PATCH v5 3/7] riscv: Add Control Transfer Records extension parsing Vikram Singh
2026-11-18 10:02 ` sashiko-bot
2026-11-18 9:50 ` [PATCH v5 4/7] riscv: pmu: Add infrastructure for Control Transfer Record Vikram Singh
2026-11-18 10:09 ` sashiko-bot
2026-11-18 9:50 ` [PATCH v5 5/7] riscv: pmu: Add driver for Control Transfer Records Ext Vikram Singh
2026-11-18 10:20 ` sashiko-bot
2026-11-18 9:50 ` [PATCH v5 6/7] riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver Vikram Singh
2026-11-18 10:21 ` sashiko-bot [this message]
2026-11-18 9:50 ` [PATCH v5 7/7] dt-bindings: riscv: add Sxctr ISA extension description Vikram Singh
2026-11-18 10:24 ` 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-220678@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=vikram.singh3@oss.qualcomm.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox