Linux Perf Users
 help / color / mirror / Atom feed
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 7/7] dt-bindings: riscv: add Sxctr ISA extension description
Date: Wed, 18 Nov 2026 10:24:47 +0000	[thread overview]
Message-ID: <sashiko-outbox-220685@kernel.org> (raw)
In-Reply-To: <20261118095053.3871267-8-vikram.singh3@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [Critical] perf: riscv: out-of-bounds slab corruption via direct cast
  of perf_ctx_data
- [High] perf: riscv: duplicate cpuhp_state_add_instance() in
  pmu_sbi_device_probe()
- [High] perf: riscv: NULL event->ctx->task dereference in
  riscv_pmu_ctr_add() and del()
- [High] perf: riscv: Kconfig redefines RISCV_M_MODE without !MMU
  dependency
- [Medium] perf: riscv: skipped riscv_pmu_ctr_reset() when
  riscv_pmu_ctr_restore() aborts
- [Medium] perf: riscv: missing inline on riscv_pmu_ctr_add() and del()
  stubs
- [Medium] perf: riscv: per-CPU branch_records leak in
  branch_records_alloc()
- [Medium] perf: riscv: missing SxCSRIND check and hardcoded CSR_SIREG*
  in CTR accessors
--

commit 7e8027524e26d037976ef2abf89eb8bdde55ea3d
Author: Vikram Singh <vikram.singh3@oss.qualcomm.com>

dt-bindings: riscv: add Sxctr ISA extension description

This series adds support for the RISC-V Control Transfer Records (Smctr and
Ssctr) ISA extensions to the RISC-V PMU driver and device tree bindings.

Link: https://lore.kernel.org/r/20261106173942.3175069-8-vikram.singh3@oss.qualcomm.com

> diff --git a/drivers/perf/Kconfig b/drivers/perf/Kconfig
> index 245e7bb763b99..0b4c04c50c227 100644
> --- a/drivers/perf/Kconfig
> +++ b/drivers/perf/Kconfig
> @@ -129,6 +129,22 @@ config ANDES_CUSTOM_PMU
>  
>  	  If you don't know what to do here, say "Y".
>  
> +config RISCV_CTR
> +	bool "Enable support for Control Transfer Records (CTR)"
> +	depends on PERF_EVENTS && RISCV_PMU
> +	default y
> +	help
> +	  Enable support for Control Transfer Records (CTR) which
> +	  allows recording branches, Jumps, Calls, returns etc taken in an
> +	  execution path. This also supports privilege based filtering. It
> +	  captures additional relevant information such as cycle count,
> +	  branch misprediction etc.
> +
> +config RISCV_M_MODE
> +		bool "Enable M Mode"
> +		depends on RISCV_CTR
> +		default n

[Severity: High]
Does defining config RISCV_M_MODE here conflict with the architecture-wide
RISCV_M_MODE symbol in arch/riscv/Kconfig?

In arch/riscv/Kconfig, RISCV_M_MODE depends on !MMU and RISCV_SBI depends
on !RISCV_M_MODE.  Adding a second definition that depends only on
RISCV_CTR allows CONFIG_RISCV_M_MODE=y on CONFIG_MMU=y kernels (such as
allyesconfig and allmodconfig), which disables CONFIG_RISCV_SBI and switches
CSR macros to M-mode, breaking the build.

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

[ ... ]

> +static inline u64 get_ctr_src_reg(unsigned int ctr_idx)
> +{
> +	return csr_indirect_read(CSR_SIREG, CTR_ENTRIES_FIRST, ctr_idx);
> +}
> +
> +static inline void set_ctr_src_reg(unsigned int ctr_idx, u64 value)
> +{
> +	return csr_indirect_write(CSR_SIREG, CTR_ENTRIES_FIRST, ctr_idx, value);
> +}
> +
> +static inline u64 get_ctr_tgt_reg(unsigned int ctr_idx)
> +{
> +	return csr_indirect_read(CSR_SIREG2, CTR_ENTRIES_FIRST, ctr_idx);
> +}
> +
> +static inline void set_ctr_tgt_reg(unsigned int ctr_idx, u64 value)
> +{
> +	return csr_indirect_write(CSR_SIREG2, CTR_ENTRIES_FIRST, ctr_idx, value);
> +}
> +
> +static inline u64 get_ctr_data_reg(unsigned int ctr_idx)
> +{
> +	return csr_indirect_read(CSR_SIREG3, CTR_ENTRIES_FIRST, ctr_idx);
> +}
> +
> +static inline void set_ctr_data_reg(unsigned int ctr_idx, u64 value)
> +{
> +	return csr_indirect_write(CSR_SIREG3, CTR_ENTRIES_FIRST, ctr_idx, value);
> +}

[Severity: Medium]
Should these accessors use CSR_IREG, CSR_IREG2, and CSR_IREG3 instead of
CSR_SIREG, CSR_SIREG2, and CSR_SIREG3, and should riscv_pmu_ctr_init() also
check that SxCSRIND is available?

csr_indirect_read() and csr_indirect_write() write CSR_ISELECT, which maps
to CSR_MISELECT under CONFIG_RISCV_M_MODE, so hardcoding CSR_SIREG*
mismatches the indirect select CSR in M-mode.  In addition,
riscv_pmu_ctr_init(), riscv_pmu_ctr_starting_cpu(), and
riscv_pmu_ctr_dying_cpu() check SxCTR and SSCOFPMF without checking
riscv_isa_extension_available(NULL, SxCSRIND), which causes an illegal
instruction trap if SxCTR is advertised without SxCSRIND.

[ ... ]

> +static void riscv_pmu_ctr_restore(void *ctx)
> +{
> +	if (task_context(ctx)->stack_state == CTR_STATE_NONE ||
> +	    task_context(ctx)->callstack_users == 0) {
> +		return;
> +	}
> +
> +	riscv_pmu_ctr_reset();
> +	__riscv_pmu_ctr_restore(ctx);
> +
> +	task_context(ctx)->stack_state = CTR_STATE_NONE;
> +}

[ ... ]

> +void riscv_pmu_ctr_sched_task(struct perf_event_pmu_context *pmu_ctx, struct task_struct *task,
> +			      bool sched_in)
> +{
> +	struct riscv_pmu *rvpmu = to_riscv_pmu(pmu_ctx->pmu);
> +	struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
> +	void *task_ctx;
> +
> +	if (!cpuc->ctr_users)
> +		return;
> +
> +	/* 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;
> +	}
> +
> +	/* Reset branch records on sched in */
> +	if (sched_in)
> +		riscv_pmu_ctr_reset();
> +}

[Severity: Medium]
When sched_in is true and task_ctx is non-NULL, does returning early from
riscv_pmu_ctr_restore() skip resetting the CTR registers?

If stack_state is CTR_STATE_NONE (for example on the task's first
schedule-in) or callstack_users is 0, riscv_pmu_ctr_restore() returns
before calling riscv_pmu_ctr_reset(), and riscv_pmu_ctr_sched_task() then
returns immediately without reaching the fallback riscv_pmu_ctr_reset().
That leaves the previous task's branch records in the hardware registers
across the context switch.

> +
> +static inline bool branch_user_callstack(unsigned int br_type)
> +{
> +	return (br_type & PERF_SAMPLE_BRANCH_USER) &&
> +		(br_type & PERF_SAMPLE_BRANCH_CALL_STACK);
> +}
> +
> +void riscv_pmu_ctr_add(struct perf_event *event)
> +{
> +	struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
> +	struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
> +
> +	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++;
> +
> +	perf_sched_cb_inc(event->pmu);
> +
> +	if (!cpuc->ctr_users++)
> +		riscv_pmu_ctr_reset();
> +}
> +
> +void riscv_pmu_ctr_del(struct perf_event *event)
> +{
> +	struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
> +	struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
> +
> +	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--;

[Severity: Critical]
Does casting task->perf_ctx_data directly to struct riscv_perf_task_context
in riscv_pmu_ctr_sched_task(), riscv_pmu_ctr_add(), and
riscv_pmu_ctr_del() corrupt struct perf_ctx_data and adjacent slab memory?

task->perf_ctx_data is a struct perf_ctx_data __rcu * wrapper whose void
*data member holds the object allocated from pmu->task_ctx_cache:

include/linux/perf_event.h:
struct perf_ctx_data {
	struct rcu_head rcu_head;
	refcount_t refcount;
	struct kmem_cache *ctx_cache;
	void *data;
};

Treating task->perf_ctx_data itself as struct riscv_perf_task_context *
without dereferencing cd->data under RCU overwrites struct perf_ctx_data
(starting at rcu_head.next when updating callstack_users and stack_state)
and writes up to 256 struct ctr_regset entries (~6 KB) out of bounds past
the 48-byte struct perf_ctx_data slab object during __riscv_pmu_ctr_save().

[Severity: High]
Can event->ctx->task be NULL in riscv_pmu_ctr_add() and
riscv_pmu_ctr_del()?

For a CPU-wide perf event opened with PERF_SAMPLE_BRANCH_USER |
PERF_SAMPLE_BRANCH_CALL_STACK, event->ctx->task is NULL, so evaluating
event->ctx->task->perf_ctx_data causes a NULL pointer dereference when the
event is added or deleted.

> diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
> index 21a2078eab98a..637b9d97e20a4 100644
> --- a/drivers/perf/riscv_pmu_sbi.c
> +++ b/drivers/perf/riscv_pmu_sbi.c

[ ... ]

> @@ -1353,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 allocation from
alloc_percpu_gfp()?

tmp_alloc_ptr is a local __percpu pointer that is discarded when
branch_records_alloc() returns, and only the resolved per-CPU pointers are
stored in events->branches.  If pmu_sbi_device_probe() fails at a later
step or the PMU is torn down, there is no __percpu pointer saved to pass to
free_percpu(), leaking the per-CPU branch_records memory.

[ ... ]

> @@ -1517,6 +1592,11 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)
>  	pmu->event_mapped = pmu_sbi_event_mapped;
>  	pmu->event_unmapped = pmu_sbi_event_unmapped;
>  	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)

[Severity: High]
Does adding cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING,
&pmu->node) here register &pmu->node twice during pmu_sbi_device_probe()?

The existing cpuhp_state_add_instance() call later in pmu_sbi_device_probe()
is still present:

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 hlist_node invokes
pmu_sbi_starting_cpu() a second time on each online CPU and re-inserts
&pmu->node at the head of the hotplug state list while it is already the
head, corrupting the list into a circular self-loop.

> diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_pmu.h
> index ecaa40370830a..848977ecf319f 100644
> --- a/include/linux/perf/riscv_pmu.h
> +++ b/include/linux/perf/riscv_pmu.h

[ ... ]

> +#else
> +
> +static inline bool riscv_pmu_ctr_valid(struct perf_event *event) { return false; }
> +static inline void riscv_pmu_ctr_consume(struct cpu_hw_events *cpuc,
> +					 struct perf_event *event) { }
> +static inline void riscv_pmu_ctr_sched_task(struct perf_event_pmu_context *,
> +					    struct task_struct *task, bool sched_in) { }
> +static void riscv_pmu_ctr_add(struct perf_event *event) { }
> +static void riscv_pmu_ctr_del(struct perf_event *event) { }

[Severity: Medium]
Should riscv_pmu_ctr_add() and riscv_pmu_ctr_del() be marked static inline
in the !CONFIG_RISCV_CTR block?  Without inline, any translation unit that
includes include/linux/perf/riscv_pmu.h without calling both helpers emits
-Wunused-function warnings when CONFIG_RISCV_CTR is disabled.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118095053.3871267-1-vikram.singh3@oss.qualcomm.com?part=7

      reply	other threads:[~2026-11-18 10:24 UTC|newest]

Thread overview: 15+ 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:58   ` sashiko-bot
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
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 [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=sashiko-outbox-220685@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