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 v6 6/7] riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver
Date: Wed, 25 Nov 2026 09:01:37 +0000 [thread overview]
Message-ID: <sashiko-outbox-232966@kernel.org> (raw)
In-Reply-To: <20261125083942.3695072-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 event->ctx->task dereference in
riscv_pmu_ctr_add()
- [Medium] riscv: pmu: IRQ cleanup bypassed on error in
pmu_sbi_device_probe()
- [Medium] riscv: pmu: per-CPU branch_records leak in
branch_records_alloc()
- [Medium] riscv: pmu: broken CTR state in pmu_sbi_ctr_stop() and
pmu_sbi_ovf_handler()
- [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()
header stubs
--
commit e9fe3c69a6f658642dd8da7db09d885c5571874f
Author: Vikram Singh <vikram.singh3@oss.qualcomm.com>
riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver
This commit integrates Control Transfer Record (CTR) extension support
into the RISC-V SBI PMU driver to enable branch stack sampling on PMU
events.
> 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]
Should this check come after riscv_pmu_get_event_info() verifies that
event->attr.type belongs to this PMU?
Returning -EOPNOTSUPP before riscv_pmu_get_event_info() can return
-ENOENT for an unhandled event type causes perf_init_event() to abort
iterating through other registered PMUs when an event with branch filters
not in allowed_filters targets another PMU.
> @@ -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);
> }
>
> static void pmu_sbi_ctr_del(struct perf_event *event, int flags)
> {
> + if (needs_branch_stack(event))
> + riscv_pmu_ctr_del(event);
> }
[Severity: High]
Can riscv_pmu_ctr_add() and riscv_pmu_ctr_del() dereference a NULL
event->ctx->task on per-CPU events?
Nothing in event initialization rejects a per-CPU event (pid == -1, where
event->ctx->task is NULL) configured with PERF_SAMPLE_BRANCH_USER |
PERF_SAMPLE_BRANCH_CALL_STACK, and both helpers dereference
event->ctx->task without a NULL check:
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++;
...
}
In addition, when CONFIG_RISCV_CTR is disabled, the stub definitions of
riscv_pmu_ctr_add() and riscv_pmu_ctr_del() in
include/linux/perf/riscv_pmu.h are missing the inline keyword:
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) { }
This triggers -Wunused-function warnings in translation units that include
the header without calling them.
[ ... ]
> @@ -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);
> }
>
> static int pmu_sbi_find_num_ctrs(void)
> @@ -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);
> + }
> +
> /*
> * Unlike other ISAs, RISC-V don't have to disable interrupts
> * to avoid throttling here. As per the specification, the
[Severity: Medium]
Can pmu_sbi_ctr_stop() and pmu_sbi_ovf_handler() leave CTR disabled,
frozen, or recording kernel branches when multiple events or multi-part
sample periods are active?
In pmu_sbi_ctr_stop(), riscv_pmu_ctr_disable() unconditionally clears
CSR_CTRCTL even if other active branch-sampling events remain on the CPU
(cpu_hw_evt->ctr_users > 1).
In pmu_sbi_ovf_handler(), hardware sets SCTRSTATUS_FROZEN on any LCOFI
because CTRCTL_LCOFIFRZ is enabled, but SCTRSTATUS_FROZEN is only cleared
inside riscv_pmu_ctr_consume(). If a co-scheduled non-branch counter
overflows or riscv_pmu_event_set_period() returns 0 on an intermediate
overflow, SCTRSTATUS_FROZEN stays set when pmu_sbi_start_overflow_mask()
restarts the counters. Conversely, when riscv_pmu_ctr_consume() does run
inside the loop, ctr_read_entries() re-enables CTR_BRANCH_ENABLE_BITS and
clears SCTRSTATUS_FROZEN before perf_event_overflow() and
pmu_sbi_start_overflow_mask() without resetting the CTR FIFO, so CTR
records kernel branches while counters are still stopped and corrupts the
buffer for any subsequent overflowed counter in the same interrupt.
[ ... ]
> @@ -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 tmp_alloc_ptr if a later step in
pmu_sbi_device_probe() fails?
Only the per-CPU virtual pointers per_cpu_ptr(tmp_alloc_ptr, cpu) are
saved in events->branches, while the __percpu base pointer tmp_alloc_ptr
needed by free_percpu() is discarded on return and never freed by any
error unwind label or teardown helper.
> +
> +static int pmu_sbi_event_init(struct perf_event *event)
> {
> /*
> * The permissions are set at event_init so that we do not depend
> @@ -1379,6 +1426,11 @@ static void pmu_sbi_event_init(struct perf_event *event)
> event->hw.flags |= PERF_EVENT_FLAG_USER_ACCESS;
> else
> event->hw.flags |= PERF_EVENT_FLAG_LEGACY;
> +
> + if (branch_sample_call_stack(event))
> + event->attach_state |= PERF_ATTACH_TASK_DATA;
> +
> + return 0;
> }
[Severity: Critical]
Does setting PERF_ATTACH_TASK_DATA cause riscv_pmu_ctr_sched_task(),
riscv_pmu_ctr_add(), and riscv_pmu_ctr_del() to write struct
riscv_perf_task_context into the struct perf_ctx_data wrapper?
When PERF_ATTACH_TASK_DATA is set, attach_perf_ctx_data() stores a 40-byte
struct perf_ctx_data wrapper in task->perf_ctx_data and places the PMU's
struct riscv_perf_task_context allocation from pmu->task_ctx_cache in
task->perf_ctx_data->data. However, riscv_pmu_ctr_add(),
riscv_pmu_ctr_del(), and riscv_pmu_ctr_sched_task() access
task->perf_ctx_data directly without rcu_read_lock()/rcu_dereference() and
without 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_USER |
PERF_SAMPLE_BRANCH_CALL_STACK schedules out, riscv_pmu_ctr_save() writes
up to 6168 bytes into the 40-byte struct perf_ctx_data slab object,
corrupting its ctx_cache and data pointers and adjacent slab objects.
>
> static void pmu_sbi_event_mapped(struct perf_event *event, struct mm_struct *mm)
> @@ -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() live in .text, whereas
check_available_filters() in drivers/perf/riscv_ctr.c is marked __init, so
probing the driver after init memory is freed will execute freed
.init.text.
> +
> + 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;
> +
> ret = riscv_pm_pmu_register(pmu);
> if (ret)
> goto out_destroy;
[Severity: High]
Does this register &pmu->node with CPUHP_AP_PERF_RISCV_STARTING twice in
pmu_sbi_device_probe()?
pmu_sbi_device_probe() already registers &pmu->node 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;
...
}
Calling cpuhp_state_add_instance() twice with the same struct hlist_node
runs pmu_sbi_starting_cpu() twice on all present CPUs (the first time
before SBI PMU snapshot shared memory is set up) and links pmu->node.next
to itself in __cpuhp_state_add_instance_cpuslocked(), corrupting the
hotplug state's hlist into a circular loop.
> @@ -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]
Does jumping to out_ctr_finish or out_free after pmu_sbi_setup_irqs()
leave riscv_pmu_irq registered while freeing pmu->hw_events?
out_ctr_finish and out_free are placed below the irq_requested teardown
block in out_destroy, so if riscv_pmu_ctr_init(), branch_records_alloc(),
or cpuhp_state_add_instance() fails, free_percpu(pmu->hw_events) frees the
per-CPU dev_id token while the per-CPU IRQ and its mapping remain
registered.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125083942.3695072-1-vikram.singh3@oss.qualcomm.com?part=6
next prev parent reply other threads:[~2026-11-25 9:01 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 8:39 [PATCH v6 0/7] riscv: pmu: Add support for Control Transfer Records Ext Vikram Singh
2026-11-25 8:39 ` [PATCH v6 1/7] perf: Increase the maximum number of branches remove_loops() can process Vikram Singh
2026-11-25 8:39 ` [PATCH v6 2/7] riscv: pmu: Add Control transfer records CSR definations Vikram Singh
2026-11-25 8:39 ` [PATCH v6 3/7] riscv: Add Control Transfer Records extension parsing Vikram Singh
2026-11-25 8:39 ` [PATCH v6 4/7] riscv: pmu: Add infrastructure for Control Transfer Record Vikram Singh
2026-11-25 8:52 ` sashiko-bot
2026-11-25 8:39 ` [PATCH v6 5/7] riscv: pmu: Add driver for Control Transfer Records Ext Vikram Singh
2026-11-25 9:09 ` sashiko-bot
2026-11-25 8:39 ` [PATCH v6 6/7] riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver Vikram Singh
2026-11-25 9:01 ` sashiko-bot [this message]
2026-11-25 8:39 ` [PATCH v6 7/7] dt-bindings: riscv: add Sxctr ISA extension description Vikram Singh
2026-11-25 8:55 ` sashiko-bot
2026-11-25 9:09 ` [PATCH v6 0/7] riscv: pmu: Add support for Control Transfer Records Ext 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-232966@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