Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH v5 0/7] riscv: pmu: Add support for Control Transfer Records Ext.
@ 2026-11-18  9:50 Vikram Singh
  2026-11-18  9:50 ` [PATCH v5 1/7] perf: Increase the maximum number of branches remove_loops() can process Vikram Singh
                   ` (6 more replies)
  0 siblings, 7 replies; 15+ messages in thread
From: Vikram Singh @ 2026-11-18  9:50 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, paul.walmsley, palmer, aou, alex,
	atishp, anup.patel, will, robh, krzk+dt, conor+dt, beeman
  Cc: Vikram Singh, linux-perf-users, linux-kernel, linux-riscv,
	linux-arm-kernel, Conor Dooley, devicetree

his series enables Control Transfer Records extension support on riscv
architecture. This extension is similar to Arch LBR in x86 and BRBE in ARM.
The Extension has been ratified and latest release can be found here [0]

CTR extension depends on both the implementation of S-mode and Sscsrind
extension v1.0.0 [1]. CTR access ctrsource, ctrtartget and ctrdata CSRs
using sscsrind extension.

The series is based on Smcdeleg/Ssccfg counter delegation extension [2]
patches [3]. CTR itself doesn't depend on counter delegation support. This
rebase is basically to include the Smcsrind patches.

The last patch is in the perf tool to allow processing 256 entries. Without
this perf seems to consider that sample as corrupted and discards it.

Here is the link to a quick guide [4] to setup and run a basic perf demo on
Linux to use CTR Ext.

Qemu patches are merged upstream and is available with latest version.

Opensbi patch is merged upstream and is available with latest version.

[0]: https://github.com/riscv/riscv-control-transfer-records/releases/tag/v1.0
[1]: https://github.com/riscvarchive/riscv-indirect-csr-access/releases/tag/v1.0.0
[2]: https://github.com/riscvarchive/riscv-smcdeleg-ssccfg/releases/tag/v1.0.0
[3]: https://lore.kernel.org/lkml/20250114-counter_delegation-v2-0-8ba74cdb851b@rivosinc.com/
[4]: https://github.com/vikramsgh/riscv_linux_ctr_demo/wiki/Steps-to-run-basic-CTR-demo-on-RISCV-Virt

To: Peter Zijlstra <peterz@infradead.org>
To: Ingo Molnar <mingo@redhat.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Namhyung Kim <namhyung@kernel.org>
To: Mark Rutland <mark.rutland@arm.com>
To: Alexander Shishkin <alexander.shishkin@linux.intel.com>
To: Jiri Olsa <jolsa@kernel.org>
To: Ian Rogers <irogers@google.com>
To: Adrian Hunter <adrian.hunter@intel.com>
To: Paul Walmsley <paul.walmsley@sifive.com>
To: Palmer Dabbelt <palmer@dabbelt.com>
To: Albert Ou <aou@eecs.berkeley.edu>
To: Alexandre Ghiti <alex@ghiti.fr>
To: Atish Kumar Patra <atishp@rivosinc.com>
To: Anup Patel <anup.patel@oss.qualcomm.com>
To: Will Deacon <will@kernel.org>
To: Rob Herring <robh@kernel.org>
To: Krzysztof Kozlowski <krzk+dt@kernel.org>
To: Conor Dooley <conor+dt@kernel.org>
To: Beeman Strong <beeman@rivosinc.com>
Cc: linux-perf-users@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-riscv@lists.infradead.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: Conor Dooley <conor@kernel.org>
Cc: devicetree@vger.kernel.org

---
Changelog:
v5:
- Rebased CTR driver and support files to Linux kernel version 7.4 RC3 

v4:
- Ported CTR driver and support files from kernel version 6.14 to kernel version 7.2
- Added CSR indirect access header file to be used by CTR driver
- Changes in sched_task call back signature to accomodate new kernel headers
- Minor clean up

v3:
- Added missing To/Cc, especially devicetree@vger.kernel.org
- Changed the commit description to better describe the need of
  perf tool change that increases max sample size processing
  capacity of remove_loops function.
- Minor fixes found while doing a virtualization PoC.
- Added the missing commit and repository information to dt-bindings,
  indicating where the extension was ratified.

v2
- Added context save/restore support for tasks using CTR feature.
- https://lore.kernel.org/lkml/20250116230955.867152-1-rkanwal@rivosinc.com/ 

v1:
- https://lore.kernel.org/lkml/20240529185337.182722-1-rkanwal@rivosinc.com/
---

Vikram Singh (7):
  perf: Increase the maximum number of branches remove_loops() can
    process.
  riscv: pmu: Add Control transfer records CSR definations.
  riscv: Add Control Transfer Records extension parsing
  riscv: pmu: Add infrastructure for Control Transfer Record
  riscv: pmu: Add driver for Control Transfer Records Ext
  riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver
  dt-bindings: riscv: add Sxctr ISA extension description

 .../devicetree/bindings/riscv/extensions.yaml |  29 +
 MAINTAINERS                                   |   3 +
 arch/riscv/include/asm/csr.h                  |  95 +++
 arch/riscv/include/asm/hwcap.h                |   6 +
 arch/riscv/kernel/cpufeature.c                |   2 +
 drivers/perf/Kconfig                          |  16 +
 drivers/perf/Makefile                         |   1 +
 drivers/perf/riscv_ctr.c                      | 609 ++++++++++++++++++
 drivers/perf/riscv_pmu.c                      |  21 +-
 drivers/perf/riscv_pmu_legacy.c               |   2 +
 drivers/perf/riscv_pmu_sbi.c                  |  87 ++-
 include/linux/perf/riscv_pmu.h                |  59 +-
 tools/perf/util/machine.c                     |  21 +-
 13 files changed, 939 insertions(+), 12 deletions(-)
 create mode 100644 drivers/perf/riscv_ctr.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v5 1/7] perf: Increase the maximum number of branches remove_loops() can process.
  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 ` 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
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 15+ messages in thread
From: Vikram Singh @ 2026-11-18  9:50 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, paul.walmsley, palmer, aou, alex,
	atishp, anup.patel, will, robh, krzk+dt, conor+dt, beeman
  Cc: Vikram Singh, linux-perf-users, linux-kernel, linux-riscv,
	linux-arm-kernel, Conor Dooley, devicetree, Rajnesh Kanwal,
	Anup Patel

RISCV CTR extension supports a maximum depth of 256 last branch records.
Currently remove_loops() can only process 127 entries at max. This leads
to samples with more than 127 entries being skipped. This change simply
updates the remove_loops() logic to be able to process 256 entries.

Co-developed-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Vikram Singh <vikram.singh3@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261106173942.3175069-2-vikram.singh3@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
---
 tools/perf/util/machine.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 076d167380f2..f1efd1120a95 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -2334,25 +2334,32 @@ static void save_iterations(struct iterations *iter,
 		iter->cycles += be[i].flags.cycles;
 }
 
-#define CHASHSZ 127
-#define CHASHBITS 7
-#define NO_ENTRY 0xff
+#define CHASHBITS 8
+#define NO_ENTRY 0xffU
 
-#define PERF_MAX_BRANCH_DEPTH 127
+#define PERF_MAX_BRANCH_DEPTH 256
 
 /* Remove loops. */
+/* Note: Last entry (i==ff) will never be checked against NO_ENTRY
+ * so it's safe to have an unsigned char array to process 256 entries
+ * without causing clash between last entry and NO_ENTRY value.
+ */
 static int remove_loops(struct branch_entry *l, int nr,
 			struct iterations *iter)
 {
 	int i, j, off;
-	unsigned char chash[CHASHSZ];
+	unsigned char chash[PERF_MAX_BRANCH_DEPTH];
 
 	memset(chash, NO_ENTRY, sizeof(chash));
 
-	BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
+	BUG_ON(PERF_MAX_BRANCH_DEPTH > 256);
 
 	for (i = 0; i < nr; i++) {
-		int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
+		/* Remainder division by PERF_MAX_BRANCH_DEPTH is not
+		 * needed as hash_64 will anyway limit the hash
+		 * to CHASHBITS
+		 */
+		int h = hash_64(l[i].from, CHASHBITS);
 
 		/* no collision handling for now */
 		if (chash[h] == NO_ENTRY) {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v5 2/7] riscv: pmu: Add Control transfer records CSR definations.
  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 ` 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
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 15+ messages in thread
From: Vikram Singh @ 2026-11-18  9:50 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, paul.walmsley, palmer, aou, alex,
	atishp, anup.patel, will, robh, krzk+dt, conor+dt, beeman
  Cc: Vikram Singh, linux-perf-users, linux-kernel, linux-riscv,
	linux-arm-kernel, Conor Dooley, devicetree, Rajnesh Kanwal,
	Anup Patel

Adding CSR defines for RISCV Control Transfer Records extension [0]
along with bit-field macros for each CSR.

[0]: https://github.com/riscv/riscv-control-transfer-records

Co-developed-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Vikram Singh <vikram.singh3@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261106173942.3175069-3-vikram.singh3@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
---
 arch/riscv/include/asm/csr.h | 95 ++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h
index 621f84d1898f..063351d9b406 100644
--- a/arch/riscv/include/asm/csr.h
+++ b/arch/riscv/include/asm/csr.h
@@ -350,6 +350,97 @@
 
 #define CSR_SCOUNTOVF		0xda0
 
+/* M-mode Control Transfer Records CSRs */
+#define CSR_MCTRCTL		0x34e
+
+/* S-mode Control Transfer Records CSRs */
+#define CSR_SCTRCTL		0x14e
+#define CSR_SCTRSTATUS		0x14f
+#define CSR_SCTRDEPTH		0x15f
+
+/* VS-mode Control Transfer Records CSRs */
+#define CSR_VSCTRCTL		0x24e
+
+/* xctrtl CSR bits. */
+#define CTRCTL_U_ENABLE		_AC(0x1, UL)
+#define CTRCTL_S_ENABLE		_AC(0x2, UL)
+#define CTRCTL_M_ENABLE		_AC(0x4, UL)
+#define CTRCTL_RASEMU		_AC(0x80, UL)
+#define CTRCTL_STE		_AC(0x100, UL)
+#define CTRCTL_MTE		_AC(0x200, UL)
+#define CTRCTL_BPFRZ		_AC(0x800, UL)
+#define CTRCTL_LCOFIFRZ		_AC(0x1000, UL)
+#define CTRCTL_EXCINH		_AC(0x200000000, UL)
+#define CTRCTL_INTRINH		_AC(0x400000000, UL)
+#define CTRCTL_TRETINH		_AC(0x800000000, UL)
+#define CTRCTL_NTBREN		_AC(0x1000000000, UL)
+#define CTRCTL_TKBRINH		_AC(0x2000000000, UL)
+#define CTRCTL_INDCALL_INH	_AC(0x10000000000, UL)
+#define CTRCTL_DIRCALL_INH	_AC(0x20000000000, UL)
+#define CTRCTL_INDJUMP_INH	_AC(0x40000000000, UL)
+#define CTRCTL_DIRJUMP_INH	_AC(0x80000000000, UL)
+#define CTRCTL_CORSWAP_INH	_AC(0x100000000000, UL)
+#define CTRCTL_RET_INH		_AC(0x200000000000, UL)
+#define CTRCTL_INDOJUMP_INH	_AC(0x400000000000, UL)
+#define CTRCTL_DIROJUMP_INH	_AC(0x800000000000, UL)
+
+/* sctrstatus CSR bits. */
+#define SCTRSTATUS_WRPTR_MASK	0xFF
+#define SCTRSTATUS_FROZEN	_AC(0x80000000, UL)
+
+#ifdef CONFIG_RISCV_M_MODE
+#define CTRCTL_KERNEL_ENABLE	CTRCTL_M_ENABLE
+#else
+#define CTRCTL_KERNEL_ENABLE	CTRCTL_S_ENABLE
+#endif
+
+/* sctrdepth CSR bits. */
+#define SCTRDEPTH_MASK		0x7
+
+#define SCTRDEPTH_MIN		0x0 /* 16 Entries. */
+#define SCTRDEPTH_MAX		0x4 /* 256 Entries. */
+
+/* ctrsource, ctrtarget and ctrdata CSR bits. */
+#define CTRSOURCE_VALID		0x1ULL
+#define CTRTARGET_MISP		0x1ULL
+
+#define CTRDATA_TYPE_MASK	0xF
+#define CTRDATA_CCV		0x8000
+#define CTRDATA_CCM_MASK	0xFFF0000
+#define CTRDATA_CCE_MASK	0xF0000000
+
+#define CTRDATA_TYPE_NONE			0
+#define CTRDATA_TYPE_EXCEPTION			1
+#define CTRDATA_TYPE_INTERRUPT			2
+#define CTRDATA_TYPE_TRAP_RET			3
+#define CTRDATA_TYPE_NONTAKEN_BRANCH		4
+#define CTRDATA_TYPE_TAKEN_BRANCH		5
+#define CTRDATA_TYPE_RESERVED_6			6
+#define CTRDATA_TYPE_RESERVED_7			7
+#define CTRDATA_TYPE_INDIRECT_CALL		8
+#define CTRDATA_TYPE_DIRECT_CALL		9
+#define CTRDATA_TYPE_INDIRECT_JUMP		10
+#define CTRDATA_TYPE_DIRECT_JUMP		11
+#define CTRDATA_TYPE_CO_ROUTINE_SWAP		12
+#define CTRDATA_TYPE_RETURN			13
+#define CTRDATA_TYPE_OTHER_INDIRECT_JUMP	14
+#define CTRDATA_TYPE_OTHER_DIRECT_JUMP		15
+
+#define CTR_ENTRIES_FIRST	0x200
+#define CTR_ENTRIES_LAST	0x2ff
+
+/* Temporary addresses for Sspesa (Precise Event Sample Attribution) CSRs. */
+#define CSR_SHPMSPC		0x5d0
+#define CSR_SHPMSDATA		0x5d1
+
+/* shpmsdata fields */
+#define SHPMSDATA_CNTRID	GENMASK(4, 0)
+#define SHPMSDATA_MODE		GENMASK(6, 5)
+#define SHPMSDATA_V		BIT(7)
+#define SHPMSDATA_METADATA_SHIFT	8
+#define SHPMSDATA_MODE_U	0
+#define SHPMSDATA_MODE_S	1
+
 #define CSR_SSTATUS		0x100
 #define CSR_SIE			0x104
 #define CSR_STVEC		0x105
@@ -563,6 +654,8 @@
 # define CSR_TOPEI	CSR_MTOPEI
 # define CSR_TOPI	CSR_MTOPI
 
+# define CSR_CTRCTL	CSR_MCTRCTL
+
 # define SR_IE		SR_MIE
 # define SR_PIE		SR_MPIE
 # define SR_PP		SR_MPP
@@ -593,6 +686,8 @@
 # define CSR_TOPEI	CSR_STOPEI
 # define CSR_TOPI	CSR_STOPI
 
+# define CSR_CTRCTL	CSR_SCTRCTL
+
 # define SR_IE		SR_SIE
 # define SR_PIE		SR_SPIE
 # define SR_PP		SR_SPP
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v5 3/7] riscv: Add Control Transfer Records extension parsing
  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  9:50 ` 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
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 15+ messages in thread
From: Vikram Singh @ 2026-11-18  9:50 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, paul.walmsley, palmer, aou, alex,
	atishp, anup.patel, will, robh, krzk+dt, conor+dt, beeman
  Cc: Vikram Singh, linux-perf-users, linux-kernel, linux-riscv,
	linux-arm-kernel, Conor Dooley, devicetree, Rajnesh Kanwal,
	Anup Patel

Adding CTR extension in ISA extension map to lookup for extension
availability.

Co-developed-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Vikram Singh <vikram.singh3@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261106173942.3175069-4-vikram.singh3@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
---
 arch/riscv/include/asm/hwcap.h | 6 ++++++
 arch/riscv/kernel/cpufeature.c | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h
index b5952db9259f..ee200d8cfccb 100644
--- a/arch/riscv/include/asm/hwcap.h
+++ b/arch/riscv/include/asm/hwcap.h
@@ -126,6 +126,10 @@
 #define RISCV_ISA_EXT_ZIC64B		116
 #define RISCV_ISA_EXT_SV48		117
 #define RISCV_ISA_EXT_SV57		118
+#define RISCV_ISA_EXT_SSPESA		119
+#define RISCV_ISA_EXT_SSPLCOFI		120
+#define RISCV_ISA_EXT_SMCTR		121
+#define RISCV_ISA_EXT_SSCTR		122
 
 #define RISCV_ISA_EXT_XLINUXENVCFG	127
 
@@ -136,10 +140,12 @@
 #define RISCV_ISA_EXT_SxAIA		RISCV_ISA_EXT_SMAIA
 #define RISCV_ISA_EXT_SUPM		RISCV_ISA_EXT_SMNPM
 #define RISCV_ISA_EXT_SxCSRIND		RISCV_ISA_EXT_SMCSRIND
+#define RISCV_ISA_EXT_SxCTR		RISCV_ISA_EXT_SMCTR
 #else
 #define RISCV_ISA_EXT_SxAIA		RISCV_ISA_EXT_SSAIA
 #define RISCV_ISA_EXT_SUPM		RISCV_ISA_EXT_SSNPM
 #define RISCV_ISA_EXT_SxCSRIND		RISCV_ISA_EXT_SSCSRIND
+#define RISCV_ISA_EXT_SxCTR		RISCV_ISA_EXT_SSCTR
 #endif
 
 #endif /* _ASM_RISCV_HWCAP_H */
diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
index 72bce616491f..b3e3c30f4c8b 100644
--- a/arch/riscv/kernel/cpufeature.c
+++ b/arch/riscv/kernel/cpufeature.c
@@ -652,6 +652,7 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
 	__RISCV_ISA_EXT_DATA(smmpm, RISCV_ISA_EXT_SMMPM),
 	__RISCV_ISA_EXT_SUPERSET(smnpm, RISCV_ISA_EXT_SMNPM, riscv_xlinuxenvcfg_exts),
 	__RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN),
+	__RISCV_ISA_EXT_DATA(smctr, RISCV_ISA_EXT_SMCTR),
 	__RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
 	__RISCV_ISA_EXT_DATA_VALIDATE(ssccfg, RISCV_ISA_EXT_SSCCFG, riscv_ext_ssccfg_validate),
 	__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
@@ -659,6 +660,7 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
 	__RISCV_ISA_EXT_SUPERSET(ssnpm, RISCV_ISA_EXT_SSNPM, riscv_xlinuxenvcfg_exts),
 	__RISCV_ISA_EXT_DATA(ssqosid, RISCV_ISA_EXT_SSQOSID),
 	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
+	__RISCV_ISA_EXT_DATA(ssctr, RISCV_ISA_EXT_SSCTR),
 	__RISCV_ISA_EXT_DATA(svade, RISCV_ISA_EXT_SVADE),
 	__RISCV_ISA_EXT_DATA_VALIDATE(svadu, RISCV_ISA_EXT_SVADU, riscv_ext_svadu_validate),
 	__RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v5 4/7] riscv: pmu: Add infrastructure for Control Transfer Record
  2026-11-18  9:50 [PATCH v5 0/7] riscv: pmu: Add support for Control Transfer Records Ext Vikram Singh
                   ` (2 preceding siblings ...)
  2026-11-18  9:50 ` [PATCH v5 3/7] riscv: Add Control Transfer Records extension parsing Vikram Singh
@ 2026-11-18  9:50 ` 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
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 15+ messages in thread
From: Vikram Singh @ 2026-11-18  9:50 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, paul.walmsley, palmer, aou, alex,
	atishp, anup.patel, will, robh, krzk+dt, conor+dt, beeman
  Cc: Vikram Singh, linux-perf-users, linux-kernel, linux-riscv,
	linux-arm-kernel, Conor Dooley, devicetree, Rajnesh Kanwal,
	Anup Patel

To support Control Transfer Records (CTR) extension, we need to extend the
riscv_pmu framework with some basic infrastructure for branch stack
sampling. Subsequent patches will use this to add support for CTR in the
riscv_pmu_dev driver.

With CTR, the branches are stored into a hardware FIFO, which will be
sampled by software when perf events overflow. A task may be
context-switched between overflows, and to avoid leaking samples we need
to clear the last task's records when a task is context-switched in.
To do this we will be using the pmu::sched_task() callback added in this
patch.

Co-developed-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Vikram Singh <vikram.singh3@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261106173942.3175069-5-vikram.singh3@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
---
 drivers/perf/riscv_pmu.c        | 19 +++++++++++++++++++
 drivers/perf/riscv_pmu_legacy.c |  2 ++
 drivers/perf/riscv_pmu_sbi.c    | 17 +++++++++++++++++
 include/linux/perf/riscv_pmu.h  | 23 +++++++++++++++++++++--
 4 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/drivers/perf/riscv_pmu.c b/drivers/perf/riscv_pmu.c
index 8e3cd0f35336..d4826eb73b16 100644
--- a/drivers/perf/riscv_pmu.c
+++ b/drivers/perf/riscv_pmu.c
@@ -157,6 +157,19 @@ u64 riscv_pmu_ctr_get_width_mask(struct perf_event *event)
 	return GENMASK_ULL(cwidth, 0);
 }
 
+static void riscv_pmu_sched_task(struct perf_event_pmu_context *pmu_ctx, struct task_struct *task,
+				 bool sched_in)
+{
+	struct riscv_pmu *pmu;
+
+	if (!pmu_ctx)
+		return;
+
+	pmu = to_riscv_pmu(pmu_ctx->pmu);
+	if (pmu->sched_task)
+		pmu->sched_task(pmu_ctx, task, sched_in);
+}
+
 u64 riscv_pmu_event_update(struct perf_event *event)
 {
 	struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
@@ -269,6 +282,8 @@ static int riscv_pmu_add(struct perf_event *event, int flags)
 	cpuc->events[idx] = event;
 	cpuc->n_events++;
 	hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
+	if (rvpmu->ctr_add)
+		rvpmu->ctr_add(event, flags);
 	if (flags & PERF_EF_START)
 		riscv_pmu_start(event, PERF_EF_RELOAD);
 
@@ -290,6 +305,8 @@ static void riscv_pmu_del(struct perf_event *event, int flags)
 	if (rvpmu->ctr_stop)
 		rvpmu->ctr_stop(event, RISCV_PMU_STOP_FLAG_RESET);
 	cpuc->n_events--;
+	if (rvpmu->ctr_del)
+		rvpmu->ctr_del(event, flags);
 	if (rvpmu->ctr_clear_idx)
 		rvpmu->ctr_clear_idx(event);
 	perf_event_update_userpage(event);
@@ -402,6 +419,7 @@ struct riscv_pmu *riscv_pmu_alloc(void)
 	for_each_possible_cpu(cpuid) {
 		cpuc = per_cpu_ptr(pmu->hw_events, cpuid);
 		cpuc->n_events = 0;
+		cpuc->ctr_users = 0;
 		for (i = 0; i < RISCV_MAX_COUNTERS; i++)
 			cpuc->events[i] = NULL;
 		cpuc->snapshot_addr = NULL;
@@ -416,6 +434,7 @@ struct riscv_pmu *riscv_pmu_alloc(void)
 		.start		= riscv_pmu_start,
 		.stop		= riscv_pmu_stop,
 		.read		= riscv_pmu_read,
+		.sched_task	= riscv_pmu_sched_task,
 	};
 
 	return pmu;
diff --git a/drivers/perf/riscv_pmu_legacy.c b/drivers/perf/riscv_pmu_legacy.c
index 1b8e4789cb3c..5214e90c61a6 100644
--- a/drivers/perf/riscv_pmu_legacy.c
+++ b/drivers/perf/riscv_pmu_legacy.c
@@ -115,6 +115,8 @@ static void pmu_legacy_init(struct riscv_pmu *pmu)
 	set_bit(RISCV_PMU_LEGACY_INSTRET, pmu->cmask);
 	pmu->ctr_start = pmu_legacy_ctr_start;
 	pmu->ctr_stop = NULL;
+	pmu->ctr_add = NULL;
+	pmu->ctr_del = NULL;
 	pmu->event_map = pmu_legacy_event_map;
 	pmu->ctr_get_idx = pmu_legacy_ctr_get_idx;
 	pmu->ctr_get_width = pmu_legacy_ctr_get_width;
diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index 21a2078eab98..b14b3acf7567 100644
--- a/drivers/perf/riscv_pmu_sbi.c
+++ b/drivers/perf/riscv_pmu_sbi.c
@@ -825,6 +825,14 @@ static void pmu_sbi_reset_scounteren(void *arg)
 			  csr_read(CSR_SCOUNTEREN) & ~BIT(pmu_sbi_csr_index(event)));
 }
 
+static void pmu_sbi_ctr_add(struct perf_event *event, int flags)
+{
+}
+
+static void pmu_sbi_ctr_del(struct perf_event *event, int flags)
+{
+}
+
 static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)
 {
 	struct sbiret ret;
@@ -885,6 +893,12 @@ 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. */
+}
+
 static int pmu_sbi_find_num_ctrs(void)
 {
 	struct sbiret ret;
@@ -1506,6 +1520,8 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)
 	pmu->pmu.attr_groups = riscv_pmu_attr_groups;
 	pmu->pmu.parent = &pdev->dev;
 	bitmap_copy(pmu->cmask, cmask, RISCV_MAX_COUNTERS);
+	pmu->ctr_add = pmu_sbi_ctr_add;
+	pmu->ctr_del = pmu_sbi_ctr_del;
 	pmu->ctr_start = pmu_sbi_ctr_start;
 	pmu->ctr_stop = pmu_sbi_ctr_stop;
 	pmu->event_map = pmu_sbi_event_map;
@@ -1517,6 +1533,7 @@ 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 = riscv_pm_pmu_register(pmu);
 	if (ret)
diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_pmu.h
index ecaa40370830..c748a06ad751 100644
--- a/include/linux/perf/riscv_pmu.h
+++ b/include/linux/perf/riscv_pmu.h
@@ -28,6 +28,15 @@
 
 #define RISCV_PMU_CONFIG1_GUEST_EVENTS 0x1
 
+struct sse_event;
+
+#define MAX_BRANCH_RECORDS 256
+
+struct branch_records {
+	struct perf_branch_stack branch_stack;
+	struct perf_branch_entry branch_entries[MAX_BRANCH_RECORDS];
+};
+
 struct cpu_hw_events {
 	/* currently enabled events */
 	int			n_events;
@@ -47,6 +56,11 @@ struct cpu_hw_events {
 	bool snapshot_set_done;
 	/* A shadow copy of the counter values to avoid clobbering during multiple SBI calls */
 	u64 snapshot_cval_shcopy[RISCV_MAX_COUNTERS];
+	/* Saved branch records. */
+	struct branch_records *branches;
+
+	/* Active events requesting branch records */
+	int ctr_users;
 };
 
 struct riscv_pmu {
@@ -60,17 +74,22 @@ struct riscv_pmu {
 	int		(*ctr_get_idx)(struct perf_event *event);
 	int		(*ctr_get_width)(int idx);
 	void		(*ctr_clear_idx)(struct perf_event *event);
+	void		(*ctr_add)(struct perf_event *event, int flags);
+	void		(*ctr_del)(struct perf_event *event, int flags);
 	void		(*ctr_start)(struct perf_event *event, u64 init_val);
 	void		(*ctr_stop)(struct perf_event *event, unsigned long flag);
 	int		(*event_map)(struct perf_event *event, u64 *config);
-	void		(*event_init)(struct perf_event *event);
+	int		(*event_init)(struct perf_event *event);
 	void		(*event_mapped)(struct perf_event *event, struct mm_struct *mm);
 	void		(*event_unmapped)(struct perf_event *event, struct mm_struct *mm);
 	uint8_t		(*csr_index)(struct perf_event *event);
-
+	void		(*sched_task)(struct perf_event_pmu_context *ctx, struct task_struct *task,
+				      bool sched_in);
 	struct cpu_hw_events	__percpu *hw_events;
 	struct hlist_node	node;
 	struct notifier_block   riscv_pm_nb;
+
+	unsigned int	ctr_depth;
 };
 
 #define to_riscv_pmu(p) (container_of(p, struct riscv_pmu, pmu))
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v5 5/7] riscv: pmu: Add driver for Control Transfer Records Ext
  2026-11-18  9:50 [PATCH v5 0/7] riscv: pmu: Add support for Control Transfer Records Ext Vikram Singh
                   ` (3 preceding siblings ...)
  2026-11-18  9:50 ` [PATCH v5 4/7] riscv: pmu: Add infrastructure for Control Transfer Record Vikram Singh
@ 2026-11-18  9:50 ` 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  9:50 ` [PATCH v5 7/7] dt-bindings: riscv: add Sxctr ISA extension description Vikram Singh
  6 siblings, 1 reply; 15+ messages in thread
From: Vikram Singh @ 2026-11-18  9:50 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, paul.walmsley, palmer, aou, alex,
	atishp, anup.patel, will, robh, krzk+dt, conor+dt, beeman
  Cc: Vikram Singh, linux-perf-users, linux-kernel, linux-riscv,
	linux-arm-kernel, Conor Dooley, devicetree, Rajnesh Kanwal,
	Anup Patel

This adds support for CTR Ext defined in [0]. The extension
allows to records a maximum for 256 last branch records.

CTR extension depends on s[m|s]csrind and Sscofpmf extensions.

Co-developed-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Vikram Singh <vikram.singh3@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261106173942.3175069-6-vikram.singh3@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
---
 MAINTAINERS                    |   3 +
 drivers/perf/Kconfig           |  16 +
 drivers/perf/Makefile          |   1 +
 drivers/perf/riscv_ctr.c       | 609 +++++++++++++++++++++++++++++++++
 include/linux/perf/riscv_pmu.h |  44 ++-
 5 files changed, 669 insertions(+), 4 deletions(-)
 create mode 100644 drivers/perf/riscv_ctr.c

diff --git a/MAINTAINERS b/MAINTAINERS
index ccbcdc0799b9..87bba12adc7f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -24225,8 +24225,11 @@ F:	arch/riscv/boot/dts/sifive/
 RISC-V PMU DRIVERS
 M:	Atish Patra <atish.patra@linux.dev>
 R:	Anup Patel <anup@brainfault.org>
+R:	Vikram Singh <vikram.singh3@oss.qualcomm.com>
 L:	linux-riscv@lists.infradead.org
 S:	Supported
+F:	Documentation/arch/riscv/pmu-sse.rst
+F:	drivers/perf/riscv_ctr.c
 F:	drivers/perf/riscv_pmu.c
 F:	drivers/perf/riscv_pmu_legacy.c
 F:	drivers/perf/riscv_pmu_sbi.c
diff --git a/drivers/perf/Kconfig b/drivers/perf/Kconfig
index 245e7bb763b9..0b4c04c50c22 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
+
 config ARM_PMU_ACPI
 	depends on ARM_PMU && ACPI
 	def_bool y
diff --git a/drivers/perf/Makefile b/drivers/perf/Makefile
index eb8a022dad9a..02cba90afb54 100644
--- a/drivers/perf/Makefile
+++ b/drivers/perf/Makefile
@@ -21,6 +21,7 @@ obj-$(CONFIG_RISCV_PMU) += riscv_pmu.o
 obj-$(CONFIG_RISCV_PMU_LEGACY) += riscv_pmu_legacy.o
 obj-$(CONFIG_RISCV_PMU_SBI) += riscv_pmu_sbi.o
 obj-$(CONFIG_STARFIVE_STARLINK_PMU) += starfive_starlink_pmu.o
+obj-$(CONFIG_RISCV_CTR) += riscv_ctr.o
 obj-$(CONFIG_THUNDERX2_PMU) += thunderx2_pmu.o
 obj-$(CONFIG_XGENE_PMU) += xgene_pmu.o
 obj-$(CONFIG_ARM_SPE_PMU) += arm_spe_pmu.o
diff --git a/drivers/perf/riscv_ctr.c b/drivers/perf/riscv_ctr.c
new file mode 100644
index 000000000000..1af8a304dd69
--- /dev/null
+++ b/drivers/perf/riscv_ctr.c
@@ -0,0 +1,609 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Control transfer records extension Helpers.
+ *
+ * Copyright (C) 2024 Rivos Inc.
+ *
+ * Author: Rajnesh Kanwal <rkanwal@rivosinc.com>
+ */
+
+#define pr_fmt(fmt) "CTR: " fmt
+
+#include <linux/bitfield.h>
+#include <linux/printk.h>
+#include <linux/types.h>
+#include <linux/perf_event.h>
+#include <linux/perf/riscv_pmu.h>
+#include <linux/cpufeature.h>
+#include <asm/hwcap.h>
+#include <asm/csr_indirect.h>
+#include <asm/csr.h>
+
+#define CTR_BRANCH_FILTERS_INH  (CTRCTL_EXCINH       | \
+				 CTRCTL_INTRINH      | \
+				 CTRCTL_TRETINH      | \
+				 CTRCTL_TKBRINH      | \
+				 CTRCTL_INDCALL_INH  | \
+				 CTRCTL_DIRCALL_INH  | \
+				 CTRCTL_INDJUMP_INH  | \
+				 CTRCTL_DIRJUMP_INH  | \
+				 CTRCTL_CORSWAP_INH  | \
+				 CTRCTL_RET_INH      | \
+				 CTRCTL_INDOJUMP_INH | \
+				 CTRCTL_DIROJUMP_INH)
+
+#define CTR_BRANCH_ENABLE_BITS (CTRCTL_KERNEL_ENABLE | CTRCTL_U_ENABLE)
+
+/* Branch filters not-supported by CTR extension. */
+#define CTR_EXCLUDE_BRANCH_FILTERS (PERF_SAMPLE_BRANCH_ABORT_TX	        | \
+				    PERF_SAMPLE_BRANCH_IN_TX		| \
+				    PERF_SAMPLE_BRANCH_PRIV_SAVE        | \
+				    PERF_SAMPLE_BRANCH_NO_TX            | \
+				    PERF_SAMPLE_BRANCH_COUNTERS)
+
+/* Branch filters supported by CTR extension. */
+#define CTR_ALLOWED_BRANCH_FILTERS (PERF_SAMPLE_BRANCH_USER		| \
+				    PERF_SAMPLE_BRANCH_KERNEL		| \
+				    PERF_SAMPLE_BRANCH_HV		| \
+				    PERF_SAMPLE_BRANCH_ANY		| \
+				    PERF_SAMPLE_BRANCH_ANY_CALL	        | \
+				    PERF_SAMPLE_BRANCH_ANY_RETURN	| \
+				    PERF_SAMPLE_BRANCH_IND_CALL	        | \
+				    PERF_SAMPLE_BRANCH_COND		| \
+				    PERF_SAMPLE_BRANCH_IND_JUMP	        | \
+				    PERF_SAMPLE_BRANCH_HW_INDEX	        | \
+				    PERF_SAMPLE_BRANCH_NO_FLAGS	        | \
+				    PERF_SAMPLE_BRANCH_NO_CYCLES	| \
+				    PERF_SAMPLE_BRANCH_CALL_STACK       | \
+				    PERF_SAMPLE_BRANCH_CALL		| \
+				    PERF_SAMPLE_BRANCH_TYPE_SAVE)
+
+#define CTR_PERF_BRANCH_FILTERS    (CTR_ALLOWED_BRANCH_FILTERS	        | \
+				    CTR_EXCLUDE_BRANCH_FILTERS)
+
+static u64 allowed_filters __read_mostly;
+
+struct ctr_regset {
+	unsigned long src;
+	unsigned long target;
+	unsigned long ctr_data;
+};
+
+enum {
+	CTR_STATE_NONE,
+	CTR_STATE_VALID,
+};
+
+/* Head is the idx of the next available slot. The slot may be already populated
+ * by an old entry which will be lost on new writes.
+ */
+struct riscv_perf_task_context {
+	int callstack_users;
+	int stack_state;
+	unsigned int num_entries;
+	u32 ctr_status;
+	u64 ctr_control;
+	struct ctr_regset store[MAX_BRANCH_RECORDS];
+};
+
+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);
+}
+
+static inline bool ctr_record_valid(u64 ctr_src)
+{
+	return !!FIELD_GET(CTRSOURCE_VALID, ctr_src);
+}
+
+static inline int ctr_get_mispredict(u64 ctr_target)
+{
+	return FIELD_GET(CTRTARGET_MISP, ctr_target);
+}
+
+static inline unsigned int ctr_get_cycles(u64 ctr_data)
+{
+	const unsigned int cce = FIELD_GET(CTRDATA_CCE_MASK, ctr_data);
+	const unsigned int ccm = FIELD_GET(CTRDATA_CCM_MASK, ctr_data);
+
+	if (ctr_data & CTRDATA_CCV)
+		return 0;
+
+	/* Formula to calculate cycles from spec: (2^12 + CCM) << CCE-1 */
+	if (cce > 0)
+		return (4096 + ccm) << (cce - 1);
+
+	return FIELD_GET(CTRDATA_CCM_MASK, ctr_data);
+}
+
+static inline unsigned int ctr_get_type(u64 ctr_data)
+{
+	return FIELD_GET(CTRDATA_TYPE_MASK, ctr_data);
+}
+
+static inline unsigned int ctr_get_depth(u64 ctr_depth)
+{
+	/* Depth table from CTR Spec: 2.4 sctrdepth.
+	 *
+	 * sctrdepth.depth       Depth
+	 * 000			- 16
+	 * 001			- 32
+	 * 010			- 64
+	 * 011			- 128
+	 * 100			- 256
+	 *
+	 * Depth = 16 * 2 ^ (ctrdepth.depth)
+	 * or
+	 * Depth = 16 << ctrdepth.depth.
+	 */
+	return 16 << FIELD_GET(SCTRDEPTH_MASK, ctr_depth);
+}
+
+static inline struct riscv_perf_task_context *task_context(void *ctx)
+{
+	return (struct riscv_perf_task_context *)ctx;
+}
+
+/* Reads CTR entry at idx and stores it in entry struct. */
+static bool get_ctr_regset(struct ctr_regset *entry, unsigned int idx)
+{
+	entry->src = get_ctr_src_reg(idx);
+
+	if (!ctr_record_valid(entry->src))
+		return false;
+
+	entry->src = entry->src;
+	entry->target = get_ctr_tgt_reg(idx);
+	entry->ctr_data = get_ctr_data_reg(idx);
+
+	return true;
+}
+
+static void set_ctr_regset(struct ctr_regset *entry, unsigned int idx)
+{
+	set_ctr_src_reg(idx, entry->src);
+	set_ctr_tgt_reg(idx, entry->target);
+	set_ctr_data_reg(idx, entry->ctr_data);
+}
+
+static u64 branch_type_to_ctr(int branch_type)
+{
+	u64 config = CTR_BRANCH_FILTERS_INH | CTRCTL_LCOFIFRZ;
+
+	if (branch_type & PERF_SAMPLE_BRANCH_USER)
+		config |= CTRCTL_U_ENABLE;
+
+	if (branch_type & PERF_SAMPLE_BRANCH_KERNEL)
+		config |= CTRCTL_KERNEL_ENABLE;
+
+	if (branch_type & PERF_SAMPLE_BRANCH_HV) {
+		if (riscv_isa_extension_available(NULL, H))
+			config |= CTRCTL_KERNEL_ENABLE;
+	}
+
+	if (branch_type & PERF_SAMPLE_BRANCH_ANY) {
+		config &= ~CTR_BRANCH_FILTERS_INH;
+		return config;
+	}
+
+	if (branch_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
+		config &= ~CTRCTL_INDCALL_INH;
+		config &= ~CTRCTL_DIRCALL_INH;
+		config &= ~CTRCTL_EXCINH;
+		config &= ~CTRCTL_INTRINH;
+	}
+
+	if (branch_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
+		config &= ~(CTRCTL_RET_INH | CTRCTL_TRETINH);
+
+	if (branch_type & PERF_SAMPLE_BRANCH_IND_CALL)
+		config &= ~CTRCTL_INDCALL_INH;
+
+	if (branch_type & PERF_SAMPLE_BRANCH_COND)
+		config &= ~CTRCTL_TKBRINH;
+
+	if (branch_type & PERF_SAMPLE_BRANCH_CALL_STACK)
+		config |= CTRCTL_RASEMU;
+
+	if (branch_type & PERF_SAMPLE_BRANCH_IND_JUMP) {
+		config &= ~CTRCTL_INDJUMP_INH;
+		config &= ~CTRCTL_INDOJUMP_INH;
+	}
+
+	if (branch_type & PERF_SAMPLE_BRANCH_CALL)
+		config &= ~CTRCTL_DIRCALL_INH;
+
+	return config;
+}
+
+static const int ctr_perf_map[] = {
+	[CTRDATA_TYPE_NONE]			= PERF_BR_UNKNOWN,
+	[CTRDATA_TYPE_EXCEPTION]		= PERF_BR_SYSCALL,
+	[CTRDATA_TYPE_INTERRUPT]		= PERF_BR_IRQ,
+	[CTRDATA_TYPE_TRAP_RET]			= PERF_BR_ERET,
+	[CTRDATA_TYPE_NONTAKEN_BRANCH]		= PERF_BR_COND,
+	[CTRDATA_TYPE_TAKEN_BRANCH]		= PERF_BR_COND,
+	[CTRDATA_TYPE_RESERVED_6]		= PERF_BR_UNKNOWN,
+	[CTRDATA_TYPE_RESERVED_7]		= PERF_BR_UNKNOWN,
+	[CTRDATA_TYPE_INDIRECT_CALL]		= PERF_BR_IND_CALL,
+	[CTRDATA_TYPE_DIRECT_CALL]		= PERF_BR_CALL,
+	[CTRDATA_TYPE_INDIRECT_JUMP]		= PERF_BR_IND,
+	[CTRDATA_TYPE_DIRECT_JUMP]		= PERF_BR_UNCOND,
+	[CTRDATA_TYPE_CO_ROUTINE_SWAP]		= PERF_BR_UNKNOWN,
+	[CTRDATA_TYPE_RETURN]			= PERF_BR_RET,
+	[CTRDATA_TYPE_OTHER_INDIRECT_JUMP]	= PERF_BR_IND,
+	[CTRDATA_TYPE_OTHER_DIRECT_JUMP]	= PERF_BR_UNCOND,
+};
+
+static void ctr_set_perf_entry_type(struct perf_branch_entry *entry,
+				    u64 ctr_data)
+{
+	int ctr_type = ctr_get_type(ctr_data);
+
+	entry->type = ctr_perf_map[ctr_type];
+	if (entry->type == PERF_BR_UNKNOWN)
+		pr_warn("%d - unknown branch type captured\n", ctr_type);
+}
+
+static void capture_ctr_flags(struct perf_branch_entry *entry,
+			      struct perf_event *event, u64 ctr_data,
+			      u64 ctr_target)
+{
+	if (branch_sample_type(event))
+		ctr_set_perf_entry_type(entry, ctr_data);
+
+	if (!branch_sample_no_cycles(event))
+		entry->cycles = ctr_get_cycles(ctr_data);
+
+	if (!branch_sample_no_flags(event)) {
+		entry->abort = 0;
+		entry->mispred = ctr_get_mispredict(ctr_target);
+		entry->predicted = !entry->mispred;
+	}
+
+	if (branch_sample_priv(event))
+		entry->priv = PERF_BR_PRIV_UNKNOWN;
+}
+
+static void ctr_regset_to_branch_entry(struct cpu_hw_events *cpuc,
+				       struct perf_event *event,
+				       struct ctr_regset *regset,
+				       unsigned int idx)
+{
+	struct perf_branch_entry *entry = &cpuc->branches->branch_entries[idx];
+
+	*entry = (struct perf_branch_entry){ };
+	entry->from = regset->src & (~CTRSOURCE_VALID);
+	entry->to = regset->target & (~CTRTARGET_MISP);
+	capture_ctr_flags(entry, event, regset->ctr_data, regset->target);
+}
+
+static void ctr_read_entries(struct cpu_hw_events *cpuc,
+			     struct perf_event *event,
+			     unsigned int depth)
+{
+	struct ctr_regset entry = {};
+	u64 ctr_ctl;
+	int i;
+
+	ctr_ctl = csr_read_clear(CSR_CTRCTL, CTR_BRANCH_ENABLE_BITS);
+
+	for (i = 0; i < depth; i++) {
+		if (!get_ctr_regset(&entry, i))
+			break;
+
+		ctr_regset_to_branch_entry(cpuc, event, &entry, i);
+	}
+
+	csr_set(CSR_CTRCTL, ctr_ctl & CTR_BRANCH_ENABLE_BITS);
+
+	cpuc->branches->branch_stack.nr = i;
+	cpuc->branches->branch_stack.hw_idx = 0;
+}
+
+bool riscv_pmu_ctr_valid(struct perf_event *event)
+{
+	u64 branch_type = event->attr.branch_sample_type;
+
+	if (branch_type & ~allowed_filters) {
+		pr_debug_once("Requested branch filters not supported 0x%llx\n",
+			      branch_type & ~allowed_filters);
+		return false;
+	}
+
+	return true;
+}
+
+void riscv_pmu_ctr_consume(struct cpu_hw_events *cpuc, struct perf_event *event)
+{
+	unsigned int depth = to_riscv_pmu(event->pmu)->ctr_depth;
+
+	ctr_read_entries(cpuc, event, depth);
+
+	/* Clear frozen bit. */
+	csr_clear(CSR_SCTRSTATUS, SCTRSTATUS_FROZEN);
+}
+
+static void riscv_pmu_ctr_reset(void)
+{
+	/* FIXME: Replace with sctrclr instruction once support is merged
+	 * into toolchain.
+	 */
+	asm volatile(".4byte 0x10400073\n" ::: "memory");
+	csr_write(CSR_SCTRSTATUS, 0);
+}
+
+static void __riscv_pmu_ctr_restore(void *ctx)
+{
+	struct riscv_perf_task_context *task_ctx = ctx;
+	unsigned int i;
+
+	csr_write(CSR_SCTRSTATUS, task_ctx->ctr_status);
+
+	for (i = 0; i < task_ctx->num_entries; i++)
+		set_ctr_regset(&task_ctx->store[i], i);
+}
+
+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;
+}
+
+static void __riscv_pmu_ctr_save(void *ctx, unsigned int depth)
+{
+	struct riscv_perf_task_context *task_ctx = ctx;
+	struct ctr_regset *dst;
+	unsigned int i;
+
+	for (i = 0; i < depth; i++) {
+		dst = &task_ctx->store[i];
+		if (!get_ctr_regset(dst, i))
+			break;
+	}
+
+	task_ctx->num_entries = i;
+
+	task_ctx->ctr_status = csr_read(CSR_SCTRSTATUS);
+}
+
+static void riscv_pmu_ctr_save(void *ctx, unsigned int depth)
+{
+	if (task_context(ctx)->stack_state == CTR_STATE_VALID)
+		return;
+
+	if (task_context(ctx)->callstack_users == 0) {
+		task_context(ctx)->stack_state = CTR_STATE_NONE;
+		return;
+	}
+
+	__riscv_pmu_ctr_save(ctx, depth);
+
+	task_context(ctx)->stack_state = CTR_STATE_VALID;
+}
+
+/*
+ * On context switch in, we need to make sure no samples from previous tasks
+ * are left in the CTR.
+ *
+ * On ctxswin, sched_in = true, called after the PMU has started
+ * On ctxswout, sched_in = false, called before the PMU is stopped
+ */
+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();
+}
+
+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--;
+
+	perf_sched_cb_dec(event->pmu);
+	cpuc->ctr_users--;
+	WARN_ON_ONCE(cpuc->ctr_users < 0);
+}
+
+void riscv_pmu_ctr_enable(struct perf_event *event)
+{
+	u64 branch_type = event->attr.branch_sample_type;
+	u64 ctr;
+
+	ctr = branch_type_to_ctr(branch_type);
+	csr_write(CSR_CTRCTL, ctr);
+}
+
+void riscv_pmu_ctr_disable(struct perf_event *event)
+{
+	/* Clear CTRCTL to disable the recording. */
+	csr_write(CSR_CTRCTL, 0);
+}
+
+/*
+ * Check for hardware supported perf filters here. To avoid missing
+ * any new added filter in perf, we do a BUILD_BUG_ON check, so make sure
+ * to update CTR_ALLOWED_BRANCH_FILTERS or CTR_EXCLUDE_BRANCH_FILTERS
+ * defines when adding support for it in below function.
+ */
+static void __init check_available_filters(void)
+{
+	u64 ctr_ctl;
+
+	/*
+	 * Ensure both perf branch filter allowed and exclude
+	 * masks are always in sync with the generic perf ABI.
+	 */
+	BUILD_BUG_ON(CTR_PERF_BRANCH_FILTERS != (PERF_SAMPLE_BRANCH_MAX - 1));
+
+	allowed_filters = PERF_SAMPLE_BRANCH_USER      |
+			  PERF_SAMPLE_BRANCH_KERNEL    |
+			  PERF_SAMPLE_BRANCH_ANY       |
+			  PERF_SAMPLE_BRANCH_HW_INDEX  |
+			  PERF_SAMPLE_BRANCH_NO_FLAGS  |
+			  PERF_SAMPLE_BRANCH_NO_CYCLES |
+			  PERF_SAMPLE_BRANCH_TYPE_SAVE;
+
+	csr_write(CSR_CTRCTL, ~0);
+	ctr_ctl = csr_read(CSR_CTRCTL);
+	csr_write(CSR_CTRCTL, 0);
+
+	if (riscv_isa_extension_available(NULL, H))
+		allowed_filters |= PERF_SAMPLE_BRANCH_HV;
+
+	if (ctr_ctl & (CTRCTL_INDCALL_INH | CTRCTL_DIRCALL_INH))
+		allowed_filters |= PERF_SAMPLE_BRANCH_ANY_CALL;
+
+	if (ctr_ctl & (CTRCTL_RET_INH | CTRCTL_TRETINH))
+		allowed_filters |= PERF_SAMPLE_BRANCH_ANY_RETURN;
+
+	if (ctr_ctl & CTRCTL_INDCALL_INH)
+		allowed_filters |= PERF_SAMPLE_BRANCH_IND_CALL;
+
+	if (ctr_ctl & CTRCTL_TKBRINH)
+		allowed_filters |= PERF_SAMPLE_BRANCH_COND;
+
+	if (ctr_ctl & CTRCTL_RASEMU)
+		allowed_filters |= PERF_SAMPLE_BRANCH_CALL_STACK;
+
+	if (ctr_ctl & (CTRCTL_INDOJUMP_INH | CTRCTL_INDJUMP_INH))
+		allowed_filters |= PERF_SAMPLE_BRANCH_IND_JUMP;
+
+	if (ctr_ctl & CTRCTL_DIRCALL_INH)
+		allowed_filters |= PERF_SAMPLE_BRANCH_CALL;
+}
+
+void riscv_pmu_ctr_starting_cpu(void)
+{
+	if (!riscv_isa_extension_available(NULL, SxCTR) ||
+	    !riscv_isa_extension_available(NULL, SSCOFPMF))
+		return;
+
+	/* Set depth to maximum. */
+	csr_write(CSR_SCTRDEPTH, SCTRDEPTH_MASK);
+}
+
+void riscv_pmu_ctr_dying_cpu(void)
+{
+	if (!riscv_isa_extension_available(NULL, SxCTR) ||
+	    !riscv_isa_extension_available(NULL, SSCOFPMF))
+		return;
+
+	/* Clear and reset CTR CSRs. */
+	csr_write(CSR_SCTRDEPTH, 0);
+	csr_write(CSR_CTRCTL, 0);
+	riscv_pmu_ctr_reset();
+}
+
+int riscv_pmu_ctr_init(struct riscv_pmu *riscv_pmu)
+{
+	size_t size = sizeof(struct riscv_perf_task_context);
+
+	if (!riscv_isa_extension_available(NULL, SxCTR) ||
+	    !riscv_isa_extension_available(NULL, SSCOFPMF))
+		return 0;
+
+	riscv_pmu->pmu.task_ctx_cache =
+		kmem_cache_create("ctr_task_ctx", size, sizeof(u64), 0, NULL);
+	if (!riscv_pmu->pmu.task_ctx_cache)
+		return -ENOMEM;
+
+	check_available_filters();
+
+	/* Set depth to maximum. */
+	csr_write(CSR_SCTRDEPTH, SCTRDEPTH_MASK);
+	riscv_pmu->ctr_depth = ctr_get_depth(csr_read(CSR_SCTRDEPTH));
+
+	pr_info("Perf CTR available, with %d depth\n", riscv_pmu->ctr_depth);
+
+	return 0;
+}
+
+void riscv_pmu_ctr_finish(struct riscv_pmu *riscv_pmu)
+{
+	if (!riscv_pmu_ctr_supported(riscv_pmu))
+		return;
+
+	riscv_pmu->ctr_depth = 0;
+	csr_write(CSR_SCTRDEPTH, 0);
+	csr_write(CSR_CTRCTL, 0);
+	riscv_pmu_ctr_reset();
+
+	kmem_cache_destroy(riscv_pmu->pmu.task_ctx_cache);
+}
diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_pmu.h
index c748a06ad751..848977ecf319 100644
--- a/include/linux/perf/riscv_pmu.h
+++ b/include/linux/perf/riscv_pmu.h
@@ -56,9 +56,8 @@ struct cpu_hw_events {
 	bool snapshot_set_done;
 	/* A shadow copy of the counter values to avoid clobbering during multiple SBI calls */
 	u64 snapshot_cval_shcopy[RISCV_MAX_COUNTERS];
-	/* Saved branch records. */
+	/* Saved branch records */
 	struct branch_records *branches;
-
 	/* Active events requesting branch records */
 	int ctr_users;
 };
@@ -88,8 +87,7 @@ struct riscv_pmu {
 	struct cpu_hw_events	__percpu *hw_events;
 	struct hlist_node	node;
 	struct notifier_block   riscv_pm_nb;
-
-	unsigned int	ctr_depth;
+	unsigned int ctr_depth;
 };
 
 #define to_riscv_pmu(p) (container_of(p, struct riscv_pmu, pmu))
@@ -111,6 +109,44 @@ int riscv_pmu_get_hpm_info(u32 *hw_ctr_width, u32 *num_hw_ctr);
 int riscv_pmu_get_event_info(u32 type, u64 config, u64 *econfig);
 #endif
 
+static inline bool riscv_pmu_ctr_supported(struct riscv_pmu *pmu)
+{
+	return !!pmu->ctr_depth;
+}
+
 #endif /* CONFIG_RISCV_PMU */
 
+#ifdef CONFIG_RISCV_CTR
+
+bool riscv_pmu_ctr_valid(struct perf_event *event);
+void riscv_pmu_ctr_consume(struct cpu_hw_events *cpuc, struct perf_event *event);
+void riscv_pmu_ctr_sched_task(struct perf_event_pmu_context *pmu_ctx, struct task_struct *task,
+			      bool sched_in);
+void riscv_pmu_ctr_add(struct perf_event *event);
+void riscv_pmu_ctr_del(struct perf_event *event);
+void riscv_pmu_ctr_enable(struct perf_event *event);
+void riscv_pmu_ctr_disable(struct perf_event *event);
+void riscv_pmu_ctr_dying_cpu(void);
+void riscv_pmu_ctr_starting_cpu(void);
+int  riscv_pmu_ctr_init(struct riscv_pmu *riscv_pmu);
+void riscv_pmu_ctr_finish(struct riscv_pmu *riscv_pmu);
+
+#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) { }
+static inline void riscv_pmu_ctr_enable(struct perf_event *event) { }
+static inline void riscv_pmu_ctr_disable(struct perf_event *event) { }
+static inline void riscv_pmu_ctr_dying_cpu(void) { }
+static inline void riscv_pmu_ctr_starting_cpu(void) { }
+static inline int riscv_pmu_ctr_init(struct riscv_pmu *riscv_pmu) { return 0; }
+static inline void riscv_pmu_ctr_finish(struct riscv_pmu *riscv_pmu) { }
+
+#endif /* CONFIG_RISCV_CTR */
+
 #endif /* _RISCV_PMU_H */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v5 6/7] riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver
  2026-11-18  9:50 [PATCH v5 0/7] riscv: pmu: Add support for Control Transfer Records Ext Vikram Singh
                   ` (4 preceding siblings ...)
  2026-11-18  9:50 ` [PATCH v5 5/7] riscv: pmu: Add driver for Control Transfer Records Ext Vikram Singh
@ 2026-11-18  9:50 ` 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
  6 siblings, 1 reply; 15+ messages in thread
From: Vikram Singh @ 2026-11-18  9:50 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, paul.walmsley, palmer, aou, alex,
	atishp, anup.patel, will, robh, krzk+dt, conor+dt, beeman
  Cc: Vikram Singh, linux-perf-users, linux-kernel, linux-riscv,
	linux-arm-kernel, Conor Dooley, devicetree, Rajnesh Kanwal,
	Anup Patel

This integrates recently added CTR ext support in riscv_pmu_dev driver
to enable branch stack sampling using PMU events.

This mainly adds CTR enable/disable callbacks in rvpmu_ctr_stop()
and rvpmu_ctr_start() function to start/stop branch recording along
with the event.

PMU overflow handler rvpmu_ovf_handler() is also updated to sample
CTR entries in case of the overflow for the particular event programmed
to records branches. The recorded entries are fed to core perf for
further processing.

Co-developed-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Vikram Singh <vikram.singh3@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261106173942.3175069-7-vikram.singh3@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
---
 drivers/perf/riscv_pmu.c     |  2 +-
 drivers/perf/riscv_pmu_sbi.c | 72 ++++++++++++++++++++++++++++++++++--
 2 files changed, 70 insertions(+), 4 deletions(-)

diff --git a/drivers/perf/riscv_pmu.c b/drivers/perf/riscv_pmu.c
index d4826eb73b16..fa9ae4e659d2 100644
--- a/drivers/perf/riscv_pmu.c
+++ b/drivers/perf/riscv_pmu.c
@@ -327,7 +327,7 @@ static int riscv_pmu_event_init(struct perf_event *event)
 	uint64_t cmask;
 
 	/* driver does not support branch stack sampling */
-	if (has_branch_stack(event))
+	if (has_branch_stack(event) && !riscv_pmu_ctr_supported(rvpmu))
 		return -EOPNOTSUPP;
 
 	hwc->flags = 0;
diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index b14b3acf7567..637b9d97e20a 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);
 }
 
@@ -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);
 }
 
 static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)
@@ -847,6 +854,10 @@ static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)
 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
 			1, flag, ival);
 #endif
+
+	if (needs_branch_stack(event))
+		riscv_pmu_ctr_enable(event);
+
 	if (ret.error && (ret.error != SBI_ERR_ALREADY_STARTED))
 		pr_err("Starting counter idx %d failed with error %d\n",
 			hwc->idx, sbi_err_map_linux_errno(ret.error));
@@ -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
@@ -1222,6 +1242,8 @@ static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
 		enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE);
 	}
 
+	riscv_pmu_ctr_starting_cpu();
+
 	if (sbi_pmu_snapshot_available())
 		return pmu_sbi_snapshot_setup(pmu, cpu);
 
@@ -1237,6 +1259,8 @@ static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node)
 	/* Disable all counters access for user mode now */
 	csr_write(CSR_SCOUNTEREN, 0x0);
 
+	riscv_pmu_ctr_dying_cpu();
+
 	if (sbi_pmu_snapshot_available())
 		return pmu_sbi_snapshot_disable();
 
@@ -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;
+}
+
+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;
 }
 
 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;
+
+	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;
@@ -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);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v5 7/7] dt-bindings: riscv: add Sxctr ISA extension description
  2026-11-18  9:50 [PATCH v5 0/7] riscv: pmu: Add support for Control Transfer Records Ext Vikram Singh
                   ` (5 preceding siblings ...)
  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  9:50 ` Vikram Singh
  2026-11-18 10:24   ` sashiko-bot
  6 siblings, 1 reply; 15+ messages in thread
From: Vikram Singh @ 2026-11-18  9:50 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, paul.walmsley, palmer, aou, alex,
	atishp, anup.patel, will, robh, krzk+dt, conor+dt, beeman
  Cc: Vikram Singh, linux-perf-users, linux-kernel, linux-riscv,
	linux-arm-kernel, Conor Dooley, devicetree, Rajnesh Kanwal,
	Anup Patel

Add the S[m|s]ctr ISA extension description.

Co-developed-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
Signed-off-by: Vikram Singh <vikram.singh3@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261106173942.3175069-8-vikram.singh3@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
---
 .../devicetree/bindings/riscv/extensions.yaml | 29 +++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Documentation/devicetree/bindings/riscv/extensions.yaml
index 79491ef8f86a..dde2f28cfda5 100644
--- a/Documentation/devicetree/bindings/riscv/extensions.yaml
+++ b/Documentation/devicetree/bindings/riscv/extensions.yaml
@@ -202,6 +202,13 @@ properties:
             mechanism in M-mode as ratified in the 20240326 version of the
             privileged ISA specification.
 
+        - const: smctr
+          description: |
+            The standard Smctr supervisor-level extension for the machine mode
+            to enable recording limited branch history in a register-accessible
+            internal core storage as ratified at commit 9c87013 ("Merge pull
+            request #44 from riscv/issue-42-fix") of riscv-control-transfer-records.
+
         - const: smmpm
           description: |
             The standard Smmpm extension for M-mode pointer masking as
@@ -263,6 +270,13 @@ properties:
             mechanism in S-mode as ratified in the 20240326 version of the
             privileged ISA specification.
 
+        - const: ssctr
+          description: |
+            The standard Ssctr supervisor-level extension for recording limited
+            branch history in a register-accessible internal core storage as
+            ratified at commit 9c87013 ("Merge pull request #44 from
+            riscv/issue-42-fix") of riscv-control-transfer-records.
+
         - const: ssnpm
           description: |
             The standard Ssnpm extension for next-mode pointer masking as
@@ -1189,6 +1203,21 @@ properties:
             - contains:
                 const: zicntr
 
+      # Smctr depends on Sscsrind
+      - if:
+          contains:
+            const: smctr
+        then:
+          contains:
+            const: sscsrind
+      # Ssctr depends on Sscsrind
+      - if:
+          contains:
+            const: ssctr
+        then:
+          contains:
+            const: sscsrind
+
 allOf:
   # Zcf extension does not exist on rv64
   - if:
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v5 1/7] perf: Increase the maximum number of branches remove_loops() can process.
  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
  0 siblings, 0 replies; 15+ messages in thread
From: sashiko-bot @ 2026-11-18  9:58 UTC (permalink / raw)
  To: Vikram Singh; +Cc: linux-perf-users

> RISCV CTR extension supports a maximum depth of 256 last branch records.
> Currently remove_loops() can only process 127 entries at max. This leads
> to samples with more than 127 entries being skipped. This change simply
> updates the remove_loops() logic to be able to process 256 entries.
> 
> Co-developed-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
> Signed-off-by: Rajnesh Kanwal <rajnesh.kanwal49@gmail.com>
> Signed-off-by: Vikram Singh <vikram.singh3@oss.qualcomm.com>
> Link: https://lore.kernel.org/r/20261106173942.3175069-2-vikram.singh3@oss.qualcomm.com
> Signed-off-by: Anup Patel <anup@brainfault.org>

Sashiko has reviewed this patch and found no issues.

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


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v5 2/7] riscv: pmu: Add Control transfer records CSR definations.
  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
  0 siblings, 0 replies; 15+ messages in thread
From: sashiko-bot @ 2026-11-18 10:00 UTC (permalink / raw)
  To: Vikram Singh; +Cc: linux-perf-users, devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] riscv: csr: unrelated temporary Sspesa CSR definitions in csr.h
--

commit 0d6ea9c7c50171c8bc5449b363f28764d22f3466
Author: Vikram Singh <vikram.singh3@oss.qualcomm.com>

riscv: pmu: Add Control transfer records CSR definations.

This commit adds CSR addresses and bit-field definitions for the RISC-V
Control Transfer Records (CTR) extension to arch/riscv/include/asm/csr.h.

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

> diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h
> index 621f84d1898f4..063351d9b406c 100644
> --- a/arch/riscv/include/asm/csr.h
> +++ b/arch/riscv/include/asm/csr.h
> @@ -350,6 +350,97 @@

[ ... ]

> +#define CTR_ENTRIES_FIRST	0x200
> +#define CTR_ENTRIES_LAST	0x2ff
> +
> +/* Temporary addresses for Sspesa (Precise Event Sample Attribution) CSRs. */
> +#define CSR_SHPMSPC		0x5d0
> +#define CSR_SHPMSDATA		0x5d1
> +
> +/* shpmsdata fields */
> +#define SHPMSDATA_CNTRID	GENMASK(4, 0)
> +#define SHPMSDATA_MODE		GENMASK(6, 5)
> +#define SHPMSDATA_V		BIT(7)
> +#define SHPMSDATA_METADATA_SHIFT	8
> +#define SHPMSDATA_MODE_U	0
> +#define SHPMSDATA_MODE_S	1

[Severity: Low]
Were CSR_SHPMSPC, CSR_SHPMSDATA, and the SHPMSDATA_* definitions included
here by mistake? They belong to the separate, unratified Sspesa extension,
use temporary S-mode custom CSR addresses (0x5d0 and 0x5d1), and are
unused in the tree and unlisted in the commit message.

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v5 3/7] riscv: Add Control Transfer Records extension parsing
  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
  0 siblings, 0 replies; 15+ messages in thread
From: sashiko-bot @ 2026-11-18 10:02 UTC (permalink / raw)
  To: Vikram Singh; +Cc: linux-perf-users, devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] riscv: unused RISCV_ISA_EXT_SSPESA and RISCV_ISA_EXT_SSPLCOFI in
  hwcap.h
- [Low] riscv: non-canonical ordering of smctr and ssctr in
  riscv_isa_ext[]
--

commit 4ae624ba46f94bfad975e230dbad25a6ad5f53d0
Author: Vikram Singh <vikram.singh3@oss.qualcomm.com>

riscv: Add Control Transfer Records extension parsing

This commit adds ISA extension definitions and parsing entries for the
Smctr and Ssctr Control Transfer Records extensions.

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

> diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h
> index b5952db9259f8..ee200d8cfccbb 100644
> --- a/arch/riscv/include/asm/hwcap.h
> +++ b/arch/riscv/include/asm/hwcap.h
> @@ -126,6 +126,10 @@
>  #define RISCV_ISA_EXT_ZIC64B		116
>  #define RISCV_ISA_EXT_SV48		117
>  #define RISCV_ISA_EXT_SV57		118
> +#define RISCV_ISA_EXT_SSPESA		119
> +#define RISCV_ISA_EXT_SSPLCOFI		120
> +#define RISCV_ISA_EXT_SMCTR		121
> +#define RISCV_ISA_EXT_SSCTR		122

[Severity: Low]
Are RISCV_ISA_EXT_SSPESA and RISCV_ISA_EXT_SSPLCOFI left over from
another change?  Neither has a corresponding entry in riscv_isa_ext[] or
any reference in the tree, so bits 119 and 120 can never be set and use
up two of the remaining IDs below RISCV_ISA_EXT_MAX (128).

[ ... ]

> diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
> index 72bce616491f1..b3e3c30f4c8bf 100644
> --- a/arch/riscv/kernel/cpufeature.c
> +++ b/arch/riscv/kernel/cpufeature.c
> @@ -652,6 +652,7 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
>  	__RISCV_ISA_EXT_DATA(smmpm, RISCV_ISA_EXT_SMMPM),
>  	__RISCV_ISA_EXT_SUPERSET(smnpm, RISCV_ISA_EXT_SMNPM, riscv_xlinuxenvcfg_exts),
>  	__RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN),
> +	__RISCV_ISA_EXT_DATA(smctr, RISCV_ISA_EXT_SMCTR),
>  	__RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
>  	__RISCV_ISA_EXT_DATA_VALIDATE(ssccfg, RISCV_ISA_EXT_SSCCFG, riscv_ext_ssccfg_validate),
>  	__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
> @@ -659,6 +660,7 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
>  	__RISCV_ISA_EXT_SUPERSET(ssnpm, RISCV_ISA_EXT_SSNPM, riscv_xlinuxenvcfg_exts),
>  	__RISCV_ISA_EXT_DATA(ssqosid, RISCV_ISA_EXT_SSQOSID),
>  	__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
> +	__RISCV_ISA_EXT_DATA(ssctr, RISCV_ISA_EXT_SSCTR),
>  	__RISCV_ISA_EXT_DATA(svade, RISCV_ISA_EXT_SVADE),

[Severity: Low]
Should smctr and ssctr be placed in alphabetical order (after smcsrind
and sscsrind, respectively)?  print_isa() walks riscv_isa_ext[] in array
order to format /proc/cpuinfo, which expects machine-level and
supervisor-level extensions to be sorted alphabetically for canonical ISA
strings.

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v5 4/7] riscv: pmu: Add infrastructure for Control Transfer Record
  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
  0 siblings, 0 replies; 15+ messages in thread
From: sashiko-bot @ 2026-11-18 10:09 UTC (permalink / raw)
  To: Vikram Singh; +Cc: linux-perf-users, devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] riscv: pmu: incompatible event_init() pointer type and ignored
  return value
--

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

riscv: pmu: Add infrastructure for Control Transfer Record

This commit adds branch stack sampling data structures and PMU callbacks
to struct riscv_pmu and struct cpu_hw_events to prepare for Control
Transfer Records (CTR) support.

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

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

[ ... ]

> @@ -60,17 +74,22 @@ struct riscv_pmu {
>  	int		(*ctr_get_idx)(struct perf_event *event);
>  	int		(*ctr_get_width)(int idx);
>  	void		(*ctr_clear_idx)(struct perf_event *event);
> +	void		(*ctr_add)(struct perf_event *event, int flags);
> +	void		(*ctr_del)(struct perf_event *event, int flags);
>  	void		(*ctr_start)(struct perf_event *event, u64 init_val);
>  	void		(*ctr_stop)(struct perf_event *event, unsigned long flag);
>  	int		(*event_map)(struct perf_event *event, u64 *config);
> -	void		(*event_init)(struct perf_event *event);
> +	int		(*event_init)(struct perf_event *event);

[Severity: High]
Does changing the return type of struct riscv_pmu::event_init here break
the build with -Werror=incompatible-pointer-types when CONFIG_RISCV_PMU_SBI
is enabled?

In this commit, pmu_sbi_event_init() in drivers/perf/riscv_pmu_sbi.c still
returns void while pmu_sbi_device_probe() assigns it to pmu->event_init;
its signature is only updated in a later patch in the series, commit
"riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver", breaking
bisection.

In addition, riscv_pmu_event_init() in drivers/perf/riscv_pmu.c calls
rvpmu->event_init(event) without checking or returning its int result, so
initialization errors from the backend callback are silently ignored.

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v5 5/7] riscv: pmu: Add driver for Control Transfer Records Ext
  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
  0 siblings, 0 replies; 15+ messages in thread
From: sashiko-bot @ 2026-11-18 10:20 UTC (permalink / raw)
  To: Vikram Singh; +Cc: linux-perf-users, devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 13 potential
issues to consider:
- [Critical] riscv: pmu: perf_ctx_data type confusion and missing RCU in
  CTR task context
- [High] riscv: pmu: duplicate RISCV_M_MODE Kconfig definition and
  broken M-mode CSRs
- [High] riscv: pmu: NULL event->ctx->task dereference in
  riscv_pmu_ctr_add() and del()
- [High] riscv: pmu: inverted CTRDATA_CCV check and cycle truncation in
  ctr_get_cycles()
- [High] riscv: pmu: missing SxCSRIND check, CSR_SIREG* mismatch, and
  RV32 truncation
- [High] riscv: pmu: unmerged CSR_CTRCTL without filtering and hardirq
  pr_warn()
- [Medium] riscv: pmu: 32-bit truncation of CSR_CTRCTL filter bits on
  CONFIG_32BIT
- [Medium] riscv: pmu: missing CTR reset in riscv_pmu_ctr_restore() and
  unpaused save
- [Medium] riscv: pmu: section mismatch on check_available_filters() and
  filter validation
- [Medium] riscv: pmu: missing inline on CTR stubs and wrong header
  guard in riscv_pmu.h
- [Medium] riscv: pmu: reserved SCTRDEPTH_MASK write and unclamped
  ctr_get_depth()
- [Medium] riscv: pmu: early return in branch_type_to_ctr() skips
  CTRCTL_RASEMU
- [Low] riscv: pmu: non-existent Documentation/arch/riscv/pmu-sse.rst in
  MAINTAINERS
--

commit 10ed00c3c43c9d9b32ecbe8d5c8ccf6143b76904
Author: Vikram Singh <vikram.singh3@oss.qualcomm.com>

riscv: pmu: Add driver for Control Transfer Records Ext

This commit adds a driver for the RISC-V Control Transfer Records (CTR)
extension to support hardware branch stack sampling in perf.

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

> diff --git a/MAINTAINERS b/MAINTAINERS
> index 93abbb315065d..19ddb35654445 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -24126,8 +24126,11 @@ F:	arch/riscv/boot/dts/sifive/
>  RISC-V PMU DRIVERS
>  M:	Atish Patra <atish.patra@linux.dev>
>  R:	Anup Patel <anup@brainfault.org>
> +R:	Vikram Singh <vikram.singh3@oss.qualcomm.com>
>  L:	linux-riscv@lists.infradead.org
>  S:	Supported
> +F:	Documentation/arch/riscv/pmu-sse.rst
> +F:	drivers/perf/riscv_ctr.c

[Severity: Low]
Documentation/arch/riscv/pmu-sse.rst does not exist in the tree, which causes
get_maintainer.pl and documentation file reference checks to warn.

> 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.

[Severity: Medium]
Should RISCV_CTR depend on 64BIT?

On CONFIG_32BIT=y builds, csr_read() and csr_write() cast values to a 32-bit
unsigned long, truncating bits 33..47 (CTRCTL_EXCINH through
CTRCTL_DIROJUMP_INH) of CSR_CTRCTL in check_available_filters() and
riscv_pmu_ctr_enable().

> +
> +config RISCV_M_MODE
> +		bool "Enable M Mode"
> +		depends on RISCV_CTR
> +		default n

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

arch/riscv/Kconfig restricts RISCV_M_MODE with "depends on !MMU", whereas
redefining it here without "!MMU" makes CONFIG_RISCV_M_MODE selectable on
S-mode MMU kernels (and enabled in allyesconfig/allmodconfig).  That disables
CONFIG_RISCV_SBI and switches core CSR macros such as CSR_STATUS, CSR_IE and
CSR_TVEC to Machine-mode CSRs, breaking S-mode builds and boot.

> 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: High]
Should these helpers pass CSR_IREG, CSR_IREG2 and CSR_IREG3 instead of
hardcoding S-mode CSR_SIREG, CSR_SIREG2 and CSR_SIREG3?

csr_indirect_read() and csr_indirect_write() program CSR_ISELECT (which maps
to CSR_MISELECT under CONFIG_RISCV_M_MODE), so passing CSR_SIREG* accesses the
wrong indirect CSR window in M-mode.  In addition, riscv_pmu_ctr_init(),
riscv_pmu_ctr_starting_cpu() and riscv_pmu_ctr_dying_cpu() only check for
SxCTR and SSCOFPMF without checking riscv_isa_extension_available(NULL,
SxCSRIND), which traps with an illegal instruction if SxCSRIND is absent.

[ ... ]

> +static inline unsigned int ctr_get_cycles(u64 ctr_data)
> +{
> +	const unsigned int cce = FIELD_GET(CTRDATA_CCE_MASK, ctr_data);
> +	const unsigned int ccm = FIELD_GET(CTRDATA_CCM_MASK, ctr_data);
> +
> +	if (ctr_data & CTRDATA_CCV)
> +		return 0;
> +
> +	/* Formula to calculate cycles from spec: (2^12 + CCM) << CCE-1 */
> +	if (cce > 0)
> +		return (4096 + ccm) << (cce - 1);
> +
> +	return FIELD_GET(CTRDATA_CCM_MASK, ctr_data);
> +}

[Severity: High]
Is the CTRDATA_CCV check inverted here?

Per the CTR specification, CTRDATA_CCV (Cycle Count Valid) is 1 when the cycle
count is valid and 0 when invalid, so returning 0 when CTRDATA_CCV is set
discards all valid cycle counts.  Also, (4096 + ccm) << (cce - 1) can reach
27 bits when cce >= 5, so assigning the unclamped return value to the 16-bit
entry->cycles bitfield in capture_ctr_flags() truncates the upper bits instead
of saturating at U16_MAX.

> +
> +static inline unsigned int ctr_get_type(u64 ctr_data)
> +{
> +	return FIELD_GET(CTRDATA_TYPE_MASK, ctr_data);
> +}
> +
> +static inline unsigned int ctr_get_depth(u64 ctr_depth)
> +{
> +	/* Depth table from CTR Spec: 2.4 sctrdepth.
> +	 *
> +	 * sctrdepth.depth       Depth
> +	 * 000			- 16
> +	 * 001			- 32
> +	 * 010			- 64
> +	 * 011			- 128
> +	 * 100			- 256
> +	 *
> +	 * Depth = 16 * 2 ^ (ctrdepth.depth)
> +	 * or
> +	 * Depth = 16 << ctrdepth.depth.
> +	 */
> +	return 16 << FIELD_GET(SCTRDEPTH_MASK, ctr_depth);
> +}

[Severity: Medium]
Can riscv_pmu->ctr_depth exceed MAX_BRANCH_RECORDS (256) or select a
non-maximum WARL depth?

riscv_pmu_ctr_init() and riscv_pmu_ctr_starting_cpu() write the reserved
encoding SCTRDEPTH_MASK (0x7) instead of SCTRDEPTH_MAX (0x4) to CSR_SCTRDEPTH.
Under WARL rules a reserved value may map to any legal depth, and if an
implementation retains a field value in 5..7, ctr_get_depth() returns
512..2048 and overflows cpuc->branches->branch_entries[MAX_BRANCH_RECORDS] and
task_ctx->store[MAX_BRANCH_RECORDS].

[ ... ]

> +static u64 branch_type_to_ctr(int branch_type)
> +{
> +	u64 config = CTR_BRANCH_FILTERS_INH | CTRCTL_LCOFIFRZ;
> +
> +	if (branch_type & PERF_SAMPLE_BRANCH_USER)
> +		config |= CTRCTL_U_ENABLE;
> +
> +	if (branch_type & PERF_SAMPLE_BRANCH_KERNEL)
> +		config |= CTRCTL_KERNEL_ENABLE;
> +
> +	if (branch_type & PERF_SAMPLE_BRANCH_HV) {
> +		if (riscv_isa_extension_available(NULL, H))
> +			config |= CTRCTL_KERNEL_ENABLE;
> +	}
> +
> +	if (branch_type & PERF_SAMPLE_BRANCH_ANY) {
> +		config &= ~CTR_BRANCH_FILTERS_INH;
> +		return config;
> +	}

[Severity: Medium]
Does returning early for PERF_SAMPLE_BRANCH_ANY skip setting CTRCTL_RASEMU
when PERF_SAMPLE_BRANCH_CALL_STACK is also requested?

riscv_pmu_ctr_valid() and riscv_pmu_ctr_add() accept events combining
PERF_SAMPLE_BRANCH_ANY and PERF_SAMPLE_BRANCH_CALL_STACK, but the early return
here bypasses the PERF_SAMPLE_BRANCH_CALL_STACK check below and leaves RAS
emulation disabled in hardware.

[ ... ]

> +static void ctr_set_perf_entry_type(struct perf_branch_entry *entry,
> +				    u64 ctr_data)
> +{
> +	int ctr_type = ctr_get_type(ctr_data);
> +
> +	entry->type = ctr_perf_map[ctr_type];
> +	if (entry->type == PERF_BR_UNKNOWN)
> +		pr_warn("%d - unknown branch type captured\n", ctr_type);
> +}
[ ... ]
> +static void ctr_regset_to_branch_entry(struct cpu_hw_events *cpuc,
> +				       struct perf_event *event,
> +				       struct ctr_regset *regset,
> +				       unsigned int idx)
> +{
> +	struct perf_branch_entry *entry = &cpuc->branches->branch_entries[idx];
> +
> +	*entry = (struct perf_branch_entry){ };
> +	entry->from = regset->src & (~CTRSOURCE_VALID);
> +	entry->to = regset->target & (~CTRTARGET_MISP);
> +	capture_ctr_flags(entry, event, regset->ctr_data, regset->target);
> +}

[Severity: High]
Can concurrent CTR events clobber CSR_CTRCTL or leak kernel branch addresses
to unprivileged user-only events, and can ctr_set_perf_entry_type() flood the
kernel log from hardirq context?

riscv_pmu_ctr_enable() overwrites CSR_CTRCTL with a single event's
configuration and riscv_pmu_ctr_disable() unconditionally zeroes CSR_CTRCTL
while other CTR events on the CPU are still active.  When a privileged kernel
event and an unprivileged PERF_SAMPLE_BRANCH_USER event share a CPU (or when
branch_type_to_ctr() clears CTRCTL_EXCINH, CTRCTL_INTRINH and CTRCTL_TRETINH
without PERF_SAMPLE_BRANCH_KERNEL), ctr_regset_to_branch_entry() copies
entries without software privilege or type filtering.  Also,
CTRDATA_TYPE_CO_ROUTINE_SWAP (produced by unprivileged jalr x1, x5, 0
instructions) maps to PERF_BR_UNKNOWN and triggers an un-ratelimited pr_warn()
up to 256 times per PMU overflow interrupt.

[ ... ]

> +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;
> +}
> +
> +static void __riscv_pmu_ctr_save(void *ctx, unsigned int depth)
> +{
> +	struct riscv_perf_task_context *task_ctx = ctx;
> +	struct ctr_regset *dst;
> +	unsigned int i;
> +
> +	for (i = 0; i < depth; i++) {
> +		dst = &task_ctx->store[i];
> +		if (!get_ctr_regset(dst, i))
> +			break;
> +	}
> +
> +	task_ctx->num_entries = i;
> +
> +	task_ctx->ctr_status = csr_read(CSR_SCTRSTATUS);
> +}

[Severity: Medium]
Does returning early in riscv_pmu_ctr_restore() leave the previous task's
branch records in the CTR registers?

When task_ctx is non-NULL on context switch in, riscv_pmu_ctr_sched_task()
calls riscv_pmu_ctr_restore() and returns before reaching the fallback
riscv_pmu_ctr_reset(), so an early return when stack_state == CTR_STATE_NONE
or callstack_users == 0 exposes stale hardware records to the incoming task.
In addition, __riscv_pmu_ctr_save() and __riscv_pmu_ctr_restore() access the
CTR registers without clearing CTR_BRANCH_ENABLE_BITS in CSR_CTRCTL first, so
branches taken during the save/restore loops corrupt the state when kernel
branch recording is enabled.

[ ... ]

> +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();
> +}
> +
> +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 passing task->perf_ctx_data directly to task_context(),
riscv_pmu_ctr_save() and riscv_pmu_ctr_restore() corrupt struct perf_ctx_data
and overflow the slab allocation?

task->perf_ctx_data is an RCU-protected pointer to a 40-byte struct
perf_ctx_data wrapper whose ->data field holds the PMU's task_ctx_cache
object:

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

Treating task->perf_ctx_data itself as struct riscv_perf_task_context
overwrites rcu_head when updating callstack_users and writes up to 256
ctr_regset entries (6144 bytes) past the 40-byte struct perf_ctx_data on
context switch.  These accesses should use rcu_dereference(task->perf_ctx_data)
under rcu_read_lock() and pass cd->data to task_context().

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

For per-CPU events (pid == -1) configured 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 scheduled in or out.

[ ... ]

> +static void __init check_available_filters(void)
> +{
[ ... ]
> +	if (ctr_ctl & (CTRCTL_INDCALL_INH | CTRCTL_DIRCALL_INH))
> +		allowed_filters |= PERF_SAMPLE_BRANCH_ANY_CALL;
[ ... ]
> +}
[ ... ]
> +int riscv_pmu_ctr_init(struct riscv_pmu *riscv_pmu)
> +{
> +	size_t size = sizeof(struct riscv_perf_task_context);
> +
> +	if (!riscv_isa_extension_available(NULL, SxCTR) ||
> +	    !riscv_isa_extension_available(NULL, SSCOFPMF))
> +		return 0;
> +
> +	riscv_pmu->pmu.task_ctx_cache =
> +		kmem_cache_create("ctr_task_ctx", size, sizeof(u64), 0, NULL);
> +	if (!riscv_pmu->pmu.task_ctx_cache)
> +		return -ENOMEM;
> +
> +	check_available_filters();

[Severity: Medium]
Does calling __init check_available_filters() from non-__init
riscv_pmu_ctr_init() cause a section mismatch?

riscv_pmu_ctr_init() is in .text and called from platform driver probe, so
calling check_available_filters() in .init.text triggers a modpost warning and
will execute freed init memory if probe is deferred or rebound after boot.
Also, check_available_filters() checks whether each branch type's own inhibit
bit is writable rather than the complement inhibit bits used by
branch_type_to_ctr() to isolate that type, and riscv_pmu_ctr_valid() does not
verify that at least one branch type filter is set in branch_sample_type.

> diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_pmu.h
> index c748a06ad7514..848977ecf319f 100644
> --- a/include/linux/perf/riscv_pmu.h
> +++ b/include/linux/perf/riscv_pmu.h
[ ... ]
>  #endif /* CONFIG_RISCV_PMU */
>  
> +#ifdef CONFIG_RISCV_CTR
> +
> +bool riscv_pmu_ctr_valid(struct perf_event *event);
> +void riscv_pmu_ctr_consume(struct cpu_hw_events *cpuc, struct perf_event *event);
[ ... ]
> +#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,
and should the CONFIG_RISCV_CTR block sit inside #ifdef CONFIG_RISCV_PMU?

Without inline, every translation unit including this header when
CONFIG_RISCV_CTR=n triggers -Wunused-function warnings.  Also, struct
cpu_hw_events and struct riscv_pmu are only declared inside #ifdef
CONFIG_RISCV_PMU, so placing these prototypes after #endif /* CONFIG_RISCV_PMU
*/ triggers -Wvisibility warnings when CONFIG_RISCV_PMU=n.

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v5 6/7] riscv: pmu: Integrate CTR Ext support in riscv_pmu_dev driver
  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
  0 siblings, 0 replies; 15+ messages in thread
From: sashiko-bot @ 2026-11-18 10:21 UTC (permalink / raw)
  To: Vikram Singh; +Cc: linux-perf-users, devicetree, robh, conor+dt

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v5 7/7] dt-bindings: riscv: add Sxctr ISA extension description
  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
  0 siblings, 0 replies; 15+ messages in thread
From: sashiko-bot @ 2026-11-18 10:24 UTC (permalink / raw)
  To: Vikram Singh; +Cc: linux-perf-users, devicetree, robh, conor+dt

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2026-11-18 10:24 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox