* [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
@ 2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:23 ` sashiko-bot
2026-11-18 8:13 ` [RFC PATCH v1 2/5] riscv: Detect " Zhanpeng Zhang
` (3 subsequent siblings)
4 siblings, 1 reply; 10+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
Describe the machine and supervisor PDIS extensions using the pinned
sampling draft. Machine support alone does not grant supervisor access.
These are draft bindings, not ratified extension or CSR allocations.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
.../devicetree/bindings/riscv/extensions.yaml | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Documentation/devicetree/bindings/riscv/extensions.yaml
index 79491ef8f86a..afda76b9c7c4 100644
--- a/Documentation/devicetree/bindings/riscv/extensions.yaml
+++ b/Documentation/devicetree/bindings/riscv/extensions.yaml
@@ -214,6 +214,15 @@ properties:
ratified at commit d70011dde6c2 ("Update to ratified state")
of riscv-j-extension.
+ - const: smpdis
+ description: |
+ The draft Smpdis machine-level extension for Precise Decoded
+ Instruction Sampling, as described at commit
+ 2de60e18118ce78e2da82ec33296345c94d8560c of the
+ riscv-performance-event-sampling specification. It defines
+ instruction selection, filtering and sample collection across
+ privilege levels. This does not imply supervisor access to PDIS.
+
- const: smstateen
description: |
The standard Smstateen extension for controlling access to CSRs
@@ -269,6 +278,16 @@ properties:
ratified at commit d70011dde6c2 ("Update to ratified state")
of riscv-j-extension.
+ - const: sspdis
+ description: |
+ The draft Sspdis supervisor-level extension for Precise Decoded
+ Instruction Sampling, as described at commit
+ 2de60e18118ce78e2da82ec33296345c94d8560c of the
+ riscv-performance-event-sampling specification. It provides the
+ supervisor-visible PDIS registers and behavior, excluding the
+ machine-only state. Firmware must make this state accessible to
+ supervisor software before reporting this extension.
+
- const: ssqosid
description: |
The standard Ssqosid extension for Quality of Service ID is
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions
2026-11-18 8:13 ` [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions Zhanpeng Zhang
@ 2026-11-18 8:23 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-11-18 8:23 UTC (permalink / raw)
To: Zhanpeng Zhang, linux-perf-users; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dt-bindings: riscv: unratified draft smpdis and sspdis
extensions added
--
commit 6221314245742eb097a15011dd35eda824258728
Author: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions
This commit adds devicetree binding descriptions for the draft Smpdis and
Sspdis RISC-V Precise Decoded Instruction Sampling extensions.
> diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Documentation/devicetree/bindings/riscv/extensions.yaml
> index 79491ef8f86a8..afda76b9c7c47 100644
> --- a/Documentation/devicetree/bindings/riscv/extensions.yaml
> +++ b/Documentation/devicetree/bindings/riscv/extensions.yaml
> @@ -214,6 +214,15 @@ properties:
> ratified at commit d70011dde6c2 ("Update to ratified state")
> of riscv-j-extension.
>
> + - const: smpdis
> + description: |
> + The draft Smpdis machine-level extension for Precise Decoded
> + Instruction Sampling, as described at commit
> + 2de60e18118ce78e2da82ec33296345c94d8560c of the
> + riscv-performance-event-sampling specification. It defines
> + instruction selection, filtering and sample collection across
> + privilege levels. This does not imply supervisor access to PDIS.
> +
> - const: smstateen
> description: |
> The standard Smstateen extension for controlling access to CSRs
> @@ -269,6 +278,16 @@ properties:
> ratified at commit d70011dde6c2 ("Update to ratified state")
> of riscv-j-extension.
>
> + - const: sspdis
> + description: |
> + The draft Sspdis supervisor-level extension for Precise Decoded
> + Instruction Sampling, as described at commit
> + 2de60e18118ce78e2da82ec33296345c94d8560c of the
> + riscv-performance-event-sampling specification. It provides the
> + supervisor-visible PDIS registers and behavior, excluding the
> + machine-only state. Firmware must make this state accessible to
> + supervisor software before reporting this extension.
> +
[Severity: Medium]
Should unratified, unfrozen draft specifications claim the canonical
smpdis and sspdis strings in riscv,isa-extensions?
Documentation/devicetree/bindings/riscv/extensions.yaml states that
standard extension properties map to their originally ratified states, and
Documentation/arch/riscv/patch-acceptance.rst requires RISC-V
specifications to be Frozen or Ratified before acceptance. Defining
unversioned standard extension names for a draft without final CSR
allocations breaks devicetree ABI stability if the specification or CSRs
change before ratification.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118081327.10684-7-zhangzhanpeng.jasper@bytedance.com?part=1
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 2/5] riscv: Detect Smpdis and Sspdis ISA extensions
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions Zhanpeng Zhang
@ 2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs Zhanpeng Zhang
` (2 subsequent siblings)
4 siblings, 0 replies; 10+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
Register separate machine and supervisor PDIS extension bits with the
existing ISA parser. Reuse the DT extension-list and legacy/RHCT parsing
paths without adding userspace hwprobe bits or inferring S-mode access
from an M-mode declaration.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
arch/riscv/include/asm/hwcap.h | 2 ++
arch/riscv/kernel/cpufeature.c | 2 ++
2 files changed, 4 insertions(+)
diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h
index b5952db9259f..0eea8158b4af 100644
--- a/arch/riscv/include/asm/hwcap.h
+++ b/arch/riscv/include/asm/hwcap.h
@@ -126,6 +126,8 @@
#define RISCV_ISA_EXT_ZIC64B 116
#define RISCV_ISA_EXT_SV48 117
#define RISCV_ISA_EXT_SV57 118
+#define RISCV_ISA_EXT_SMPDIS 119
+#define RISCV_ISA_EXT_SSPDIS 120
#define RISCV_ISA_EXT_XLINUXENVCFG 127
diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
index 72bce616491f..982231fa290a 100644
--- a/arch/riscv/kernel/cpufeature.c
+++ b/arch/riscv/kernel/cpufeature.c
@@ -651,12 +651,14 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
__RISCV_ISA_EXT_DATA(smcsrind, RISCV_ISA_EXT_SMCSRIND),
__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(smpdis, RISCV_ISA_EXT_SMPDIS),
__RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN),
__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),
__RISCV_ISA_EXT_DATA(sscsrind, RISCV_ISA_EXT_SSCSRIND),
__RISCV_ISA_EXT_SUPERSET(ssnpm, RISCV_ISA_EXT_SSNPM, riscv_xlinuxenvcfg_exts),
+ __RISCV_ISA_EXT_DATA(sspdis, RISCV_ISA_EXT_SSPDIS),
__RISCV_ISA_EXT_DATA(ssqosid, RISCV_ISA_EXT_SSQOSID),
__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
__RISCV_ISA_EXT_DATA(svade, RISCV_ISA_EXT_SVADE),
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 10+ messages in thread* [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 2/5] riscv: Detect " Zhanpeng Zhang
@ 2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:39 ` sashiko-bot
2026-11-18 8:13 ` [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks Zhanpeng Zhang
2026-11-18 8:13 ` [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt Zhanpeng Zhang
4 siblings, 1 reply; 10+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
The draft leaves CSR encodings unallocated. Require an explicit boot
opt-in, the csr-v1 test-model marker and the supervisor ISA extensions
before accessing the provisional registers. Probe the control, counter
width and filters with exception fixups and leave the unit inhibited.
Run the probe during CPU bringup on the standard local counter overflow
interrupt path. Quiesce the unit at CPU teardown. The experiment uses
RV64 with KVM disabled and matching firmware that excludes M/VS/VU.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
.../admin-guide/kernel-parameters.txt | 10 ++
MAINTAINERS | 1 +
drivers/perf/Kconfig | 13 ++
drivers/perf/Makefile | 1 +
drivers/perf/riscv_pdis.c | 137 ++++++++++++++++++
drivers/perf/riscv_pdis.h | 13 ++
drivers/perf/riscv_pmu_sbi.c | 5 +
7 files changed, 180 insertions(+)
create mode 100644 drivers/perf/riscv_pdis.c
create mode 100644 drivers/perf/riscv_pdis.h
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd9..dc15ddd29cf4 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -6722,6 +6722,16 @@ Kernel parameters
fcfi Disable user forward CFI ABI to userspace even if the
landing pad extension is available.
+ riscv_pdis_test= [RISCV,EARLY]
+ Format: <bool>
+ Authorize the experimental PDIS PMU to probe temporary
+ CSR encodings. Requires CONFIG_RISCV_PDIS_CSR,
+ CONFIG_KVM=n and matching test firmware. The private
+ test DT must mark each matching CPU with
+ riscv,pdis-test-model = "csr-v1".
+ Firmware must also advertise Sspdis on each usable CPU.
+ Default: disabled. Do not enable on production hardware.
+
ro [KNL] Mount root device read-only on boot
rodata= [KNL,EARLY]
diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315065..3e695d4c5fc1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -24128,6 +24128,7 @@ M: Atish Patra <atish.patra@linux.dev>
R: Anup Patel <anup@brainfault.org>
L: linux-riscv@lists.infradead.org
S: Supported
+F: drivers/perf/riscv_pdis*
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..791d252d4952 100644
--- a/drivers/perf/Kconfig
+++ b/drivers/perf/Kconfig
@@ -105,6 +105,19 @@ config RISCV_PMU_SBI
full perf feature support i.e. counter overflow, privilege mode
filtering, counter configuration.
+config RISCV_PDIS_CSR
+ bool "Experimental RISC-V PDIS CSR-output perf PMU"
+ depends on RISCV_PMU_SBI && 64BIT && MMU && !RISCV_M_MODE && EXPERT
+ help
+ Build an experimental per-CPU PDIS sampling PMU. This requires a
+ matching test model and firmware with the temporary CSR encodings.
+ Firmware must report Sspdis on CPUs authorized to use PDIS.
+ The riscv_pdis_test=1 boot parameter explicitly authorizes probing.
+ Task sampling, mixed privilege and KVM are not supported. Do not
+ enable the boot parameter on existing production hardware.
+
+ If unsure, say N.
+
config STARFIVE_STARLINK_PMU
depends on ARCH_STARFIVE || COMPILE_TEST
depends on 64BIT
diff --git a/drivers/perf/Makefile b/drivers/perf/Makefile
index eb8a022dad9a..203e6227dc6b 100644
--- a/drivers/perf/Makefile
+++ b/drivers/perf/Makefile
@@ -20,6 +20,7 @@ obj-$(CONFIG_QCOM_L3_PMU) += qcom_l3_pmu.o
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_RISCV_PDIS_CSR) += riscv_pdis.o
obj-$(CONFIG_STARFIVE_STARLINK_PMU) += starfive_starlink_pmu.o
obj-$(CONFIG_THUNDERX2_PMU) += thunderx2_pmu.o
obj-$(CONFIG_XGENE_PMU) += xgene_pmu.o
diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
new file mode 100644
index 000000000000..b7671e5ad63e
--- /dev/null
+++ b/drivers/perf/riscv_pdis.c
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Experimental RV64 PDIS CSR-output PMU for an explicitly matching model. */
+
+#include <linux/bits.h>
+#include <linux/cpu.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/of.h>
+#include <linux/percpu.h>
+
+#include <asm/asm-extable.h>
+#include <asm/cpufeature.h>
+#include <asm/csr.h>
+
+#include "riscv_pdis.h"
+
+/* Draft spdis.adoc at 2de60e18118ce78e2da82ec33296345c94d8560c. */
+#define RISCV_PDIS_COUNTER_BIT BIT(1)
+/* Local experimental encodings, not architectural CSR allocations. */
+#define RISCV_PDIS_CSR_CTL 0x5e0
+#define RISCV_PDIS_CSR_COUNTER 0x5e1
+#define RISCV_PDIS_CSR_EVMASK 0x5e2
+#define RISCV_PDIS_CSR_EVMATCH 0x5e3
+#define RISCV_PDIS_CSR_FILTER 0x5e4
+#define RISCV_PDIS_CSR_VSCTL 0x6d0
+#define RISCV_PDIS_STATEEN BIT_ULL(55)
+#define RISCV_PDIS_CTL_ACC BIT_ULL(33)
+
+struct pdis_cpu {
+ bool ready;
+};
+
+static DEFINE_PER_CPU(struct pdis_cpu, pdis_cpu);
+static cpumask_t pdis_cpus;
+static bool pdis_test;
+
+static int __init pdis_test_setup(char *str)
+{
+ return kstrtobool(str, &pdis_test);
+}
+early_param("riscv_pdis_test", pdis_test_setup);
+
+/* Private test DT marker, not a binding for architectural PDIS. */
+static bool pdis_test_model_matches(unsigned int cpu)
+{
+ struct device_node *node = of_cpu_device_node_get(cpu);
+ const char *model;
+ bool matches;
+
+ matches = !of_property_read_string(node, "riscv,pdis-test-model", &model) &&
+ !strcmp(model, "csr-v1");
+ of_node_put(node);
+ return matches;
+}
+
+/* Used only during opt-in probing; missing/unauthorized CSRs must not oops. */
+static __always_inline int pdis_read_csr(unsigned int csr, unsigned long *value)
+{
+ asm goto("1: csrr %[value], %[csr]\n"
+ _ASM_EXTABLE(1b, %l[fault])
+ : [value] "=r" (*value) : [csr] "i" (csr) : "memory" : fault);
+ return 0;
+fault:
+ *value = 0;
+ return -ENODEV;
+}
+
+static __always_inline int pdis_write_csr(unsigned int csr, unsigned long value)
+{
+ asm goto("1: csrw %[csr], %[value]\n"
+ _ASM_EXTABLE(1b, %l[fault])
+ : : [value] "r" (value), [csr] "i" (csr) : "memory" : fault);
+ return 0;
+fault:
+ return -ENODEV;
+}
+
+void riscv_pdis_cpu_starting(void)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ unsigned long ctl, inhibit, value;
+ unsigned int cpu = smp_processor_id();
+
+ pcpu->ready = false;
+ cpumask_clear_cpu(cpu, &pdis_cpus);
+ if (!pdis_test || IS_ENABLED(CONFIG_KVM) ||
+ !riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_SSPDIS) ||
+ !riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_SSCOFPMF) ||
+ !riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_SSCSRIND) ||
+ !pdis_test_model_matches(cpu))
+ return;
+
+ if (pdis_read_csr(RISCV_PDIS_CSR_CTL, &ctl) ||
+ ctl & ~RISCV_PDIS_CTL_ACC ||
+ pdis_read_csr(CSR_SCOUNTINHIBIT, &inhibit) ||
+ !(inhibit & RISCV_PDIS_COUNTER_BIT))
+ return;
+ if (pdis_write_csr(RISCV_PDIS_CSR_CTL, 0) ||
+ pdis_write_csr(RISCV_PDIS_CSR_EVMASK, 0) ||
+ pdis_write_csr(RISCV_PDIS_CSR_EVMATCH, 0) ||
+ pdis_write_csr(RISCV_PDIS_CSR_FILTER, 0))
+ return;
+ if (pdis_read_csr(RISCV_PDIS_CSR_EVMASK, &value) || value ||
+ pdis_read_csr(RISCV_PDIS_CSR_EVMATCH, &value) || value ||
+ pdis_read_csr(RISCV_PDIS_CSR_FILTER, &value) || value)
+ return;
+ /* The first version requires all 32 COUNT and INITVAL bits. */
+ if (pdis_write_csr(RISCV_PDIS_CSR_COUNTER, ~0UL) ||
+ pdis_read_csr(RISCV_PDIS_CSR_COUNTER, &value) || value != ~0UL ||
+ pdis_write_csr(RISCV_PDIS_CSR_COUNTER, 0))
+ return;
+ if (riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_H)) {
+ if (pdis_read_csr(RISCV_PDIS_CSR_VSCTL, &value) ||
+ value & ~RISCV_PDIS_CTL_ACC ||
+ pdis_read_csr(CSR_HSTATEEN0, &value) || value & RISCV_PDIS_STATEEN)
+ return;
+ }
+ pcpu->ready = true;
+ cpumask_set_cpu(cpu, &pdis_cpus);
+}
+
+static void pdis_quiesce(void)
+{
+ csr_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ /* The test-model contract requires this to cancel in-flight selections. */
+ csr_write(RISCV_PDIS_CSR_CTL, 0);
+}
+
+void riscv_pdis_cpu_dying(void)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ if (pcpu->ready)
+ pdis_quiesce();
+ pcpu->ready = false;
+ cpumask_clear_cpu(smp_processor_id(), &pdis_cpus);
+}
diff --git a/drivers/perf/riscv_pdis.h b/drivers/perf/riscv_pdis.h
new file mode 100644
index 000000000000..b1ffe7d16ce0
--- /dev/null
+++ b/drivers/perf/riscv_pdis.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __RISCV_PDIS_H
+#define __RISCV_PDIS_H
+
+#ifdef CONFIG_RISCV_PDIS_CSR
+void riscv_pdis_cpu_starting(void);
+void riscv_pdis_cpu_dying(void);
+#else
+static inline void riscv_pdis_cpu_starting(void) { }
+static inline void riscv_pdis_cpu_dying(void) { }
+#endif
+
+#endif /* __RISCV_PDIS_H */
diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index 21a2078eab98..545b8becf90a 100644
--- a/drivers/perf/riscv_pmu_sbi.c
+++ b/drivers/perf/riscv_pmu_sbi.c
@@ -27,6 +27,8 @@
#include <asm/vendor_extensions.h>
#include <asm/vendor_extensions/andes.h>
+#include "riscv_pdis.h"
+
#define ALT_SBI_PMU_OVERFLOW(__ovl) \
asm volatile(ALTERNATIVE_2( \
"csrr %0, " __stringify(CSR_SCOUNTOVF), \
@@ -1204,6 +1206,8 @@ static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
if (riscv_pmu_use_irq) {
cpu_hw_evt->irq = riscv_pmu_irq;
+ if (riscv_pmu_irq_num == RV_IRQ_PMU)
+ riscv_pdis_cpu_starting();
ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE);
}
@@ -1216,6 +1220,7 @@ static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node)
{
+ riscv_pdis_cpu_dying();
if (riscv_pmu_use_irq) {
disable_percpu_irq(riscv_pmu_irq);
}
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs
2026-11-18 8:13 ` [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs Zhanpeng Zhang
@ 2026-11-18 8:39 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-11-18 8:39 UTC (permalink / raw)
To: Zhanpeng Zhang, linux-perf-users; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf: riscv: unguarded non-volatile asm goto output in
pdis_read_csr()
- [Low] perf: riscv: riscv_pdis_cpu_starting() leaves counter CSR dirty
on error
--
commit 5b0b1f9bcdc0e64d6c714a4382c017740354c7f9
Author: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs
This commit adds opt-in CPU bringup probing and teardown quiescing for the
experimental RISC-V PDIS CSR layout behind CONFIG_RISCV_PDIS_CSR and the
riscv_pdis_test boot parameter.
> diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
> new file mode 100644
> index 0000000000000..b7671e5ad63eb
> --- /dev/null
> +++ b/drivers/perf/riscv_pdis.c
> @@ -0,0 +1,137 @@
[ ... ]
> +/* Used only during opt-in probing; missing/unauthorized CSRs must not oops. */
> +static __always_inline int pdis_read_csr(unsigned int csr, unsigned long *value)
> +{
> + asm goto("1: csrr %[value], %[csr]\n"
> + _ASM_EXTABLE(1b, %l[fault])
> + : [value] "=r" (*value) : [csr] "i" (csr) : "memory" : fault);
> + return 0;
> +fault:
> + *value = 0;
> + return -ENODEV;
> +}
[Severity: Medium]
Does pdis_read_csr() break compilation or risk miscompilation on older GCC
versions by using raw asm goto with an output operand without checking
CONFIG_CC_HAS_ASM_GOTO_OUTPUT?
GCC only added syntax support for asm goto output operands in GCC 11, and
scripts/Kconfig.toolchain disables CONFIG_CC_HAS_ASM_GOTO_OUTPUT on GCC
versions affected by GCC PR 113921 (< 11.5, 12.0..12.3, and 13.0..13.2),
where inlining asm goto with outputs can miscompile the fault label edge.
In addition, asm goto with outputs is not implicitly volatile in GCC
(GCC PR 103979), so callers use asm_goto_output() from
include/linux/compiler_types.h to include the volatile qualifier.
Could RISCV_PDIS_CSR depend on CC_HAS_ASM_GOTO_OUTPUT (or provide a
!CONFIG_CC_HAS_ASM_GOTO_OUTPUT fallback) and use asm_goto_output() here?
[ ... ]
> +void riscv_pdis_cpu_starting(void)
> +{
[ ... ]
> + /* The first version requires all 32 COUNT and INITVAL bits. */
> + if (pdis_write_csr(RISCV_PDIS_CSR_COUNTER, ~0UL) ||
> + pdis_read_csr(RISCV_PDIS_CSR_COUNTER, &value) || value != ~0UL ||
> + pdis_write_csr(RISCV_PDIS_CSR_COUNTER, 0))
> + return;
[Severity: Low]
Does riscv_pdis_cpu_starting() leave RISCV_PDIS_CSR_COUNTER set to non-zero
when value != ~0UL?
If the write and read succeed but the register implements fewer than 64
writable bits, short-circuit evaluation of value != ~0UL skips the
pdis_write_csr(RISCV_PDIS_CSR_COUNTER, 0) call and returns with all
implemented counter and initval bits left set to 1.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118081327.10684-7-zhangzhanpeng.jasper@bytedance.com?part=3
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
` (2 preceding siblings ...)
2026-11-18 8:13 ` [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs Zhanpeng Zhang
@ 2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:49 ` sashiko-bot
2026-11-18 8:13 ` [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt Zhanpeng Zhang
4 siblings, 1 reply; 10+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
Manage the independent PDIS counter as a CPU-wide, fixed-period perf
event. Track partial periods on read and stop, preserve the remaining
period across resumes and claim each observed delta once on nested
reads. COUNT and sticky OF cannot report multiple unobserved reloads,
so the event count is an estimate.
Quiesce PDIS at task boundaries to keep task metadata with its sample.
Keep the remaining interval across a switch. The model must cancel
in-flight selections when software inhibits PDIS and clears spdisctl.
Keep the PMU unregistered until overflow delivery is wired up.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
drivers/perf/riscv_pdis.c | 315 +++++++++++++++++++++++++++++++++++++-
1 file changed, 312 insertions(+), 3 deletions(-)
diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
index b7671e5ad63e..9a8fe5e4ab82 100644
--- a/drivers/perf/riscv_pdis.c
+++ b/drivers/perf/riscv_pdis.c
@@ -1,11 +1,13 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Experimental RV64 PDIS CSR-output PMU for an explicitly matching model. */
-#include <linux/bits.h>
+#include <linux/bitfield.h>
#include <linux/cpu.h>
+#include <linux/device.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/of.h>
+#include <linux/perf_event.h>
#include <linux/percpu.h>
#include <asm/asm-extable.h>
@@ -24,15 +26,41 @@
#define RISCV_PDIS_CSR_FILTER 0x5e4
#define RISCV_PDIS_CSR_VSCTL 0x6d0
#define RISCV_PDIS_STATEEN BIT_ULL(55)
+#define RISCV_PDIS_CTL_SEL GENMASK_ULL(2, 0)
#define RISCV_PDIS_CTL_ACC BIT_ULL(33)
+#define RISCV_PDIS_CTL_U BIT_ULL(60)
+#define RISCV_PDIS_CTL_S BIT_ULL(61)
+
+enum riscv_pdis_select {
+ RISCV_PDIS_SELECT_ALL,
+ RISCV_PDIS_SELECT_LOAD,
+ RISCV_PDIS_SELECT_STORE,
+ RISCV_PDIS_SELECT_LOAD_STORE,
+ RISCV_PDIS_SELECT_CONTROL,
+};
+
+#define RISCV_PDIS_SAMPLE_TYPES (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
+ PERF_SAMPLE_TIME | PERF_SAMPLE_ID | \
+ PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_CPU | \
+ PERF_SAMPLE_PERIOD | PERF_SAMPLE_IDENTIFIER)
+
+/* A policy limit for the experiment, not an architectural minimum period. */
+#define PDIS_MIN_PERIOD 4096
+
+/* prev_count holds the 32-bit position and the overflow accounting bit. */
+#define PDIS_OF_ACCOUNTED BIT_ULL(32)
struct pdis_cpu {
+ struct perf_event *event;
bool ready;
+ bool counter_valid;
+ bool switching;
};
static DEFINE_PER_CPU(struct pdis_cpu, pdis_cpu);
static cpumask_t pdis_cpus;
static bool pdis_test;
+static struct pmu pdis_pmu;
static int __init pdis_test_setup(char *str)
{
@@ -82,6 +110,7 @@ void riscv_pdis_cpu_starting(void)
unsigned int cpu = smp_processor_id();
pcpu->ready = false;
+ pcpu->counter_valid = false;
cpumask_clear_cpu(cpu, &pdis_cpus);
if (!pdis_test || IS_ENABLED(CONFIG_KVM) ||
!riscv_cpu_has_extension_unlikely(cpu, RISCV_ISA_EXT_SSPDIS) ||
@@ -119,19 +148,299 @@ void riscv_pdis_cpu_starting(void)
cpumask_set_cpu(cpu, &pdis_cpus);
}
+static void pdis_clear_accounted(struct perf_event *event)
+{
+ local64_t *prev_count;
+ s64 prev;
+
+ if (!event)
+ return;
+ prev_count = &event->hw.prev_count;
+ prev = local64_read(prev_count);
+ while (prev & PDIS_OF_ACCOUNTED) {
+ if (local64_try_cmpxchg(prev_count, &prev, (u32)prev))
+ break;
+ }
+}
+
static void pdis_quiesce(void)
{
csr_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
/* The test-model contract requires this to cancel in-flight selections. */
csr_write(RISCV_PDIS_CSR_CTL, 0);
+ pdis_clear_accounted(this_cpu_ptr(&pdis_cpu)->event);
}
-void riscv_pdis_cpu_dying(void)
+static int pdis_check_period(struct perf_event *event, u64 period)
+{
+ return period >= PDIS_MIN_PERIOD && period <= U32_MAX ? 0 : -EINVAL;
+}
+
+static int pdis_event_init(struct perf_event *event)
+{
+ struct perf_event *sibling;
+ u64 mode;
+ int ret;
+
+ if (event->attr.type != pdis_pmu.type)
+ return -ENOENT;
+ if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK ||
+ event->attr.inherit || event->attr.freq || event->attr.exclusive ||
+ event->attr.aux_output ||
+ !is_sampling_event(event))
+ return -EOPNOTSUPP;
+ if (!cpumask_test_cpu(event->cpu, &pdis_cpus))
+ return -ENODEV;
+ if (event->attr.config > RISCV_PDIS_SELECT_CONTROL ||
+ event->attr.config1 || event->attr.config2)
+ return -EINVAL;
+ /* Discovery requires host-only execution; an explicit H is optional. */
+ if (event->attr.sample_type & ~RISCV_PDIS_SAMPLE_TYPES ||
+ event->attr.exclude_host ||
+ event->attr.exclude_idle || event->attr.precise_ip ||
+ event->attr.exclude_user == event->attr.exclude_kernel)
+ return -EOPNOTSUPP;
+ if (event->group_leader != event && event->group_leader->pmu == event->pmu)
+ return -EINVAL;
+ for_each_sibling_event(sibling, event->group_leader) {
+ if (sibling != event && sibling->pmu == event->pmu)
+ return -EINVAL;
+ }
+ ret = pdis_check_period(event, event->attr.sample_period);
+ if (ret)
+ return ret;
+ mode = event->attr.exclude_kernel ? RISCV_PDIS_CTL_U : RISCV_PDIS_CTL_S;
+ event->hw.config = FIELD_PREP(RISCV_PDIS_CTL_SEL, event->attr.config) | mode;
+ event->hw.idx = -1;
+ event->attach_state |= PERF_ATTACH_SCHED_CB;
+ return 0;
+}
+
+/* COUNT is frozen by the caller; OF is sticky until acknowledged. */
+static void pdis_event_update(struct perf_event *event)
+{
+ struct hw_perf_event *hwc = &event->hw;
+ u64 period = hwc->last_period;
+ s64 prev = local64_read(&hwc->prev_count);
+ s64 delta, next, step;
+ u32 progress;
+
+ do {
+ u32 count = csr_read(RISCV_PDIS_CSR_COUNTER);
+ bool pending = csr_read(CSR_SCOUNTOVF) & RISCV_PDIS_COUNTER_BIT;
+ bool accounted = prev & PDIS_OF_ACCOUNTED;
+
+ progress = count + (u32)period;
+ /* COUNT may have overflowed without reloading yet. */
+ if (progress >= period)
+ return;
+
+ /* OF can reveal one wrap; multiple unseen wraps cannot be recovered. */
+ step = (s64)progress - (u32)prev;
+ delta = step;
+ if (step < 0 || (pending && !accounted))
+ delta += period;
+ next = progress;
+ if (accounted || pending || step < 0)
+ next |= PDIS_OF_ACCOUNTED;
+ } while (!local64_try_cmpxchg(&hwc->prev_count, &prev, next));
+
+ local64_add(delta, &event->count);
+ /* Apply the position change without overwriting a nested update. */
+ local64_sub(step, &hwc->period_left);
+}
+
+static void pdis_update_and_quiesce(struct perf_event *event)
+{
+ csr_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ pdis_event_update(event);
+ pdis_quiesce();
+}
+
+static int pdis_program(struct perf_event *event, bool reload)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ struct hw_perf_event *hwc = &event->hw;
+ u64 period = hwc->sample_period;
+ u64 left = local64_read(&hwc->period_left);
+ u64 counter = 0;
+ unsigned long actual;
+
+ pdis_quiesce();
+ if (reload) {
+ u32 initval = -(u32)period;
+ u32 count;
+
+ if (!left || left > period)
+ left = period;
+ count = -(u32)left;
+ counter = (u64)initval << 32 | count;
+ csr_write(RISCV_PDIS_CSR_COUNTER, counter);
+ }
+ csr_write(RISCV_PDIS_CSR_CTL, hwc->config);
+ actual = csr_read(RISCV_PDIS_CSR_CTL);
+ if ((actual & ~RISCV_PDIS_CTL_ACC) != hwc->config ||
+ (reload && csr_read(RISCV_PDIS_CSR_COUNTER) != counter)) {
+ pcpu->counter_valid = false;
+ pdis_quiesce();
+ return -EOPNOTSUPP;
+ }
+ if (reload) {
+ hwc->last_period = period;
+ local64_set(&hwc->prev_count, period - left);
+ local64_set(&hwc->period_left, left);
+ pcpu->counter_valid = true;
+ }
+ return 0;
+}
+
+static void pdis_enable(struct perf_event *event)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ event->hw.state = 0;
+ if (!pcpu->switching)
+ csr_clear(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ perf_event_update_userpage(event);
+}
+
+static void pdis_start(struct perf_event *event, int flags)
{
struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ bool reload = (flags & PERF_EF_RELOAD) || !pcpu->counter_valid;
- if (pcpu->ready)
+ if (!pcpu->ready || pcpu->event != event ||
+ !(event->hw.state & PERF_HES_STOPPED))
+ return;
+ if (!pdis_program(event, reload))
+ pdis_enable(event);
+}
+
+static void pdis_stop(struct perf_event *event, int flags)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ if (event->hw.state & PERF_HES_STOPPED)
+ return;
+ /* Save progress even on a stop for throttling. */
+ if (pcpu->ready && pcpu->counter_valid)
+ pdis_update_and_quiesce(event);
+ event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
+ perf_event_update_userpage(event);
+}
+
+static int pdis_add(struct perf_event *event, int flags)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ int ret;
+
+ if (!pcpu->ready)
+ return -ENODEV;
+ if (pcpu->event)
+ return -EAGAIN;
+ ret = pdis_program(event, true);
+ if (ret)
+ return ret;
+ pcpu->event = event;
+ event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
+ perf_sched_cb_inc(event->pmu);
+ if (flags & PERF_EF_START) {
+ pdis_enable(event);
+ } else {
pdis_quiesce();
+ perf_event_update_userpage(event);
+ }
+ return 0;
+}
+
+static void pdis_del(struct perf_event *event, int flags)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ pdis_stop(event, PERF_EF_UPDATE);
+ pcpu->event = NULL;
+ pcpu->counter_valid = false;
+ perf_sched_cb_dec(event->pmu);
+}
+
+static void pdis_read(struct perf_event *event)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ unsigned long inhibit;
+
+ if (!pcpu->ready || !pcpu->counter_valid || pcpu->event != event ||
+ (event->hw.state & PERF_HES_STOPPED))
+ return;
+ inhibit = csr_read_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ pdis_event_update(event);
+ if (!(inhibit & RISCV_PDIS_COUNTER_BIT))
+ csr_clear(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+}
+
+/* Drop pending selections before changing the task used for sample metadata. */
+static void pdis_sched_task(struct perf_event_pmu_context *ctx,
+ struct task_struct *task, bool sched_in)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ struct perf_event *event = pcpu->event;
+
+ pcpu->switching = !sched_in;
+ if (!pcpu->ready || !event || (event->hw.state & PERF_HES_STOPPED))
+ return;
+ if (!sched_in) {
+ pdis_update_and_quiesce(event);
+ return;
+ }
+
+ /* Restore selection without resetting the counter. */
+ if (pdis_program(event, false))
+ pdis_stop(event, PERF_EF_UPDATE);
+ else
+ csr_clear(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+}
+
+void riscv_pdis_cpu_dying(void)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ if (pcpu->ready) {
+ if (pcpu->event)
+ pdis_stop(pcpu->event, PERF_EF_UPDATE);
+ else
+ pdis_quiesce();
+ }
pcpu->ready = false;
cpumask_clear_cpu(smp_processor_id(), &pdis_cpus);
}
+
+PMU_FORMAT_ATTR(select, "config:0-2");
+static struct attribute *pdis_format_attrs[] = { &format_attr_select.attr, NULL };
+static const struct attribute_group pdis_format_group = {
+ .name = "format",
+ .attrs = pdis_format_attrs,
+};
+
+static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(&pdis_cpus));
+}
+static DEVICE_ATTR_RO(cpumask);
+static struct attribute *pdis_attrs[] = { &dev_attr_cpumask.attr, NULL };
+static const struct attribute_group pdis_group = { .attrs = pdis_attrs };
+static const struct attribute_group *pdis_groups[] = {
+ &pdis_format_group, &pdis_group, NULL,
+};
+
+static struct pmu pdis_pmu = {
+ .task_ctx_nr = perf_invalid_context,
+ .capabilities = PERF_PMU_CAP_NO_NMI,
+ .attr_groups = pdis_groups,
+ .event_init = pdis_event_init,
+ .add = pdis_add,
+ .del = pdis_del,
+ .start = pdis_start,
+ .stop = pdis_stop,
+ .read = pdis_read,
+ .check_period = pdis_check_period,
+ .sched_task = pdis_sched_task,
+};
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks
2026-11-18 8:13 ` [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks Zhanpeng Zhang
@ 2026-11-18 8:49 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-11-18 8:49 UTC (permalink / raw)
To: Zhanpeng Zhang, linux-perf-users; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] perf: riscv: stale pcpu->switching in pdis_del() inhibits
subsequent events
- [Medium] perf: riscv: pdis_event_update() drops period and corrupts
state on wrap
- [Medium] perf: riscv: unguarded for_each_sibling_event() in
pdis_event_init()
--
commit ae54dcd2ba8108979f94818a492fed1b6ab87dcc
Author: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
perf: riscv: Add PDIS event and counter callbacks
This commit implements perf event initialization, counter programming,
read/start/stop callbacks, and task context-switch handling for the
experimental RISC-V PDIS PMU.
> diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
> index b7671e5ad63eb..9a8fe5e4ab828 100644
> --- a/drivers/perf/riscv_pdis.c
> +++ b/drivers/perf/riscv_pdis.c
[ ... ]
> @@ -119,19 +148,299 @@ void riscv_pdis_cpu_starting(void)
> cpumask_set_cpu(cpu, &pdis_cpus);
> }
[ ... ]
> +static int pdis_event_init(struct perf_event *event)
> +{
> + struct perf_event *sibling;
> + u64 mode;
> + int ret;
> +
> + if (event->attr.type != pdis_pmu.type)
> + return -ENOENT;
> + if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK ||
> + event->attr.inherit || event->attr.freq || event->attr.exclusive ||
> + event->attr.aux_output ||
> + !is_sampling_event(event))
> + return -EOPNOTSUPP;
> + if (!cpumask_test_cpu(event->cpu, &pdis_cpus))
> + return -ENODEV;
> + if (event->attr.config > RISCV_PDIS_SELECT_CONTROL ||
> + event->attr.config1 || event->attr.config2)
> + return -EINVAL;
> + /* Discovery requires host-only execution; an explicit H is optional. */
> + if (event->attr.sample_type & ~RISCV_PDIS_SAMPLE_TYPES ||
> + event->attr.exclude_host ||
> + event->attr.exclude_idle || event->attr.precise_ip ||
> + event->attr.exclude_user == event->attr.exclude_kernel)
> + return -EOPNOTSUPP;
> + if (event->group_leader != event && event->group_leader->pmu == event->pmu)
> + return -EINVAL;
> + for_each_sibling_event(sibling, event->group_leader) {
> + if (sibling != event && sibling->pmu == event->pmu)
> + return -EINVAL;
> + }
[Severity: Medium]
Does calling for_each_sibling_event() when event->group_leader == event
trigger a lockdep splat under CONFIG_PROVE_LOCKING=y?
For a newly allocated standalone event or group leader, event->ctx is still
NULL during pmu->event_init(), and perf_try_init_event() only locks
group_leader->ctx->mutex when event->group_leader != event.
for_each_sibling_event() unconditionally runs lockdep_assert_event_ctx(),
which checks lockdep_is_held(&event->ctx->mutex) with event->ctx == NULL
and fires WARN_ON_ONCE. Guarding the loop with
if (event->group_leader != event) avoids this.
[ ... ]
> +/* COUNT is frozen by the caller; OF is sticky until acknowledged. */
> +static void pdis_event_update(struct perf_event *event)
> +{
> + struct hw_perf_event *hwc = &event->hw;
> + u64 period = hwc->last_period;
> + s64 prev = local64_read(&hwc->prev_count);
> + s64 delta, next, step;
> + u32 progress;
> +
> + do {
> + u32 count = csr_read(RISCV_PDIS_CSR_COUNTER);
> + bool pending = csr_read(CSR_SCOUNTOVF) & RISCV_PDIS_COUNTER_BIT;
> + bool accounted = prev & PDIS_OF_ACCOUNTED;
> +
> + progress = count + (u32)period;
> + /* COUNT may have overflowed without reloading yet. */
> + if (progress >= period)
> + return;
> +
> + /* OF can reveal one wrap; multiple unseen wraps cannot be recovered. */
> + step = (s64)progress - (u32)prev;
> + delta = step;
> + if (step < 0 || (pending && !accounted))
> + delta += period;
> + next = progress;
> + if (accounted || pending || step < 0)
> + next |= PDIS_OF_ACCOUNTED;
> + } while (!local64_try_cmpxchg(&hwc->prev_count, &prev, next));
> +
> + local64_add(delta, &event->count);
> + /* Apply the position change without overwriting a nested update. */
> + local64_sub(step, &hwc->period_left);
> +}
> +
> +static void pdis_update_and_quiesce(struct perf_event *event)
> +{
> + csr_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
> + pdis_event_update(event);
> + pdis_quiesce();
> +}
[Severity: Medium]
Can returning early on progress >= period in pdis_event_update() lose the
elapsed period and leave the counter state out of sync when called from
pdis_update_and_quiesce()?
If COUNT has wrapped past 0xffffffff and is waiting for an in-flight
selection to retire and reload INITVAL when pdis_stop() or
pdis_sched_task() calls pdis_update_and_quiesce(), pdis_event_update()
returns without updating event->count, hwc->prev_count, or
hwc->period_left, and pcpu->counter_valid remains true. pdis_quiesce()
then writes 0 to RISCV_PDIS_CSR_CTL (cancelling the in-flight selection)
and clears PDIS_OF_ACCOUNTED.
Because pcpu->counter_valid is still true, a subsequent sched-in or
non-reloading pdis_start() calls pdis_program(event, false) without
reloading RISCV_PDIS_CSR_COUNTER. The counts up to the wrap are lost from
event->count, hwc->period_left keeps its stale value across a stop, and if
cancelling the selection leaves COUNT at 0, every subsequent
pdis_event_update() continues to see progress >= period until COUNT
increments through 2^32 - period events.
[ ... ]
> +static void pdis_del(struct perf_event *event, int flags)
> +{
> + struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
> +
> + pdis_stop(event, PERF_EF_UPDATE);
> + pcpu->event = NULL;
> + pcpu->counter_valid = false;
> + perf_sched_cb_dec(event->pmu);
> +}
[ ... ]
> +/* Drop pending selections before changing the task used for sample metadata. */
> +static void pdis_sched_task(struct perf_event_pmu_context *ctx,
> + struct task_struct *task, bool sched_in)
> +{
> + struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
> + struct perf_event *event = pcpu->event;
> +
> + pcpu->switching = !sched_in;
> + if (!pcpu->ready || !event || (event->hw.state & PERF_HES_STOPPED))
> + return;
[Severity: Medium]
Can pdis_del() leave pcpu->switching stuck at true if the event is removed
during a context switch?
On task switch-out, __perf_event_task_sched_out() calls
perf_pmu_sched_task(task, next, false), which runs pdis_sched_task() and
sets pcpu->switching = true. Before perf_pmu_sched_task(prev, task, true)
runs on switch-in, perf_event_context_sched_in() can schedule out a
flexible CPU-wide PDIS event when the incoming task has a pinned task
event:
__perf_event_task_sched_out()
perf_pmu_sched_task(..., false)
pdis_sched_task(..., false)
pcpu->switching = true
__perf_event_task_sched_in()
perf_event_context_sched_in()
ctx_sched_out(&cpuctx->ctx, NULL, EVENT_FLEXIBLE)
event_sched_out()
pdis_del()
perf_sched_cb_dec(event->pmu)
If the PDIS event is not re-added in cpu_ctx_sched_in() (for example,
because event->pending_disable transitioned it to PERF_EVENT_STATE_OFF in
event_sched_out()), perf_sched_cb_dec() has already removed pdis_pmu from
sched_cb_list, so perf_pmu_sched_task(prev, task, true) never calls
pdis_sched_task() to clear pcpu->switching. Any subsequent PDIS event
added or started on that CPU outside a context switch will have
pdis_enable() skip clearing CSR_SCOUNTINHIBIT, leaving the counter
inhibited.
In addition, pdis_event_init() does not reject
is_cgroup_event(event->group_leader), so a PDIS event grouped under a
cgroup software leader can be scheduled out by perf_cgroup_switch() at the
end of __perf_event_task_sched_out() after pdis_sched_task(..., false) has
already set pcpu->switching = true.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118081327.10684-7-zhangzhanpeng.jasper@bytedance.com?part=4
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt
2026-11-18 8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
` (3 preceding siblings ...)
2026-11-18 8:13 ` [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks Zhanpeng Zhang
@ 2026-11-18 8:13 ` Zhanpeng Zhang
2026-11-18 8:47 ` sashiko-bot
4 siblings, 1 reply; 10+ messages in thread
From: Zhanpeng Zhang @ 2026-11-18 8:13 UTC (permalink / raw)
To: linux-riscv, linux-perf-users
Cc: linux-kernel, devicetree, andrew.jones, linux-doc, corbet,
atish.patra, anup, pjw, palmer, aou, alex, yuanzhu, cuiyunhui,
peterz, mingo, acme, namhyung, mark.rutland, will, robh, krzk+dt,
conor+dt
Use the recorded PC and the single enabled host privilege to construct
perf samples. Only read the record header and PC from the indirect
window. Account for rejected records when applying perf throttling.
Collect PDIS before acknowledging the shared pending interrupt. Restart
ordinary counters even on a PDIS-only interrupt, then re-enable PDIS
if perf has kept the event running.
Register the PMU after probing and installing the interrupt handler.
Save the remaining period on CPU PM entry and restore it on resume.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
drivers/perf/riscv_pdis.c | 112 +++++++++++++++++++++++++++++++++++
drivers/perf/riscv_pdis.h | 10 ++++
drivers/perf/riscv_pmu_sbi.c | 38 ++++++++----
3 files changed, 148 insertions(+), 12 deletions(-)
diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
index 9a8fe5e4ab82..60c9ece22dfc 100644
--- a/drivers/perf/riscv_pdis.c
+++ b/drivers/perf/riscv_pdis.c
@@ -3,6 +3,7 @@
#include <linux/bitfield.h>
#include <linux/cpu.h>
+#include <linux/cpu_pm.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/init.h>
@@ -13,10 +14,12 @@
#include <asm/asm-extable.h>
#include <asm/cpufeature.h>
#include <asm/csr.h>
+#include <asm/ptrace.h>
#include "riscv_pdis.h"
/* Draft spdis.adoc at 2de60e18118ce78e2da82ec33296345c94d8560c. */
+#define RISCV_PDIS_ISELECT 0x60
#define RISCV_PDIS_COUNTER_BIT BIT(1)
/* Local experimental encodings, not architectural CSR allocations. */
#define RISCV_PDIS_CSR_CTL 0x5e0
@@ -26,6 +29,7 @@
#define RISCV_PDIS_CSR_FILTER 0x5e4
#define RISCV_PDIS_CSR_VSCTL 0x6d0
#define RISCV_PDIS_STATEEN BIT_ULL(55)
+#define RISCV_PDIS_CTL_OF BIT_ULL(63)
#define RISCV_PDIS_CTL_SEL GENMASK_ULL(2, 0)
#define RISCV_PDIS_CTL_ACC BIT_ULL(33)
#define RISCV_PDIS_CTL_U BIT_ULL(60)
@@ -39,6 +43,9 @@ enum riscv_pdis_select {
RISCV_PDIS_SELECT_CONTROL,
};
+#define RISCV_PDIS_HDR_TYPE GENMASK_ULL(2, 0)
+#define RISCV_PDIS_HDR_FMT GENMASK_ULL(63, 61)
+
#define RISCV_PDIS_SAMPLE_TYPES (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
PERF_SAMPLE_TIME | PERF_SAMPLE_ID | \
PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_CPU | \
@@ -54,7 +61,9 @@ struct pdis_cpu {
struct perf_event *event;
bool ready;
bool counter_valid;
+ bool irq_running;
bool switching;
+ bool pm_resume;
};
static DEFINE_PER_CPU(struct pdis_cpu, pdis_cpu);
@@ -399,6 +408,64 @@ static void pdis_sched_task(struct perf_event_pmu_context *ctx,
csr_clear(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
}
+/* Called with local IRQs disabled and the PDIS counter inhibited. */
+static int pdis_overflow(struct perf_event *event)
+{
+ struct perf_sample_data data;
+ struct pt_regs regs = {};
+ unsigned long selector, header, pc;
+
+ selector = csr_swap(CSR_SISELECT, RISCV_PDIS_ISELECT);
+ header = csr_read(CSR_SIREG);
+ pc = csr_read(CSR_SIREG2);
+ csr_write(CSR_SISELECT, selector);
+
+ if (FIELD_GET(RISCV_PDIS_HDR_FMT, header) ||
+ FIELD_GET(RISCV_PDIS_HDR_TYPE, header) > 4)
+ return perf_event_account_interrupt(event);
+
+ /* The draft record has no privilege field. Only one mode is enabled. */
+ regs.epc = pc;
+ if (event->hw.config & RISCV_PDIS_CTL_S)
+ regs.status = SR_SPP;
+ perf_sample_data_init(&data, 0, event->hw.last_period);
+ data.ip = pc;
+ data.sample_flags |= PERF_SAMPLE_IP;
+ return perf_event_overflow(event, &data, ®s);
+}
+
+bool riscv_pdis_irq_prepare(void)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ struct perf_event *event = pcpu->event;
+ bool pending;
+
+ if (!pcpu->ready)
+ return false;
+ pcpu->irq_running = event && !(event->hw.state & PERF_HES_STOPPED);
+ csr_set(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ pending = csr_read(CSR_SCOUNTOVF) & RISCV_PDIS_COUNTER_BIT;
+ if (pending && pcpu->irq_running && !pcpu->switching) {
+ pdis_event_update(event);
+ if (pdis_overflow(event))
+ pdis_stop(event, PERF_EF_UPDATE);
+ }
+ csr_clear(RISCV_PDIS_CSR_CTL, RISCV_PDIS_CTL_OF);
+ if (pending)
+ pdis_clear_accounted(event);
+ return pending;
+}
+
+void riscv_pdis_irq_finish(void)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+
+ if (pcpu->ready && pcpu->irq_running && pcpu->event &&
+ !pcpu->switching && !(pcpu->event->hw.state & PERF_HES_STOPPED))
+ csr_clear(CSR_SCOUNTINHIBIT, RISCV_PDIS_COUNTER_BIT);
+ pcpu->irq_running = false;
+}
+
void riscv_pdis_cpu_dying(void)
{
struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
@@ -413,6 +480,32 @@ void riscv_pdis_cpu_dying(void)
cpumask_clear_cpu(smp_processor_id(), &pdis_cpus);
}
+static int pdis_cpu_pm_notify(struct notifier_block *nb, unsigned long cmd, void *v)
+{
+ struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
+ struct perf_event *event = pcpu->event;
+
+ if (!pcpu->ready)
+ return NOTIFY_DONE;
+ if (cmd == CPU_PM_ENTER) {
+ pcpu->pm_resume = event && !(event->hw.state & PERF_HES_STOPPED);
+ if (event)
+ pdis_stop(event, PERF_EF_UPDATE);
+ else
+ pdis_quiesce();
+ } else if (cmd == CPU_PM_EXIT || cmd == CPU_PM_ENTER_FAILED) {
+ riscv_pdis_cpu_starting();
+ if (pcpu->pm_resume && pcpu->ready && event)
+ pdis_start(event, PERF_EF_RELOAD);
+ pcpu->pm_resume = false;
+ }
+ return NOTIFY_OK;
+}
+
+static struct notifier_block pdis_cpu_pm_nb = {
+ .notifier_call = pdis_cpu_pm_notify,
+};
+
PMU_FORMAT_ATTR(select, "config:0-2");
static struct attribute *pdis_format_attrs[] = { &format_attr_select.attr, NULL };
static const struct attribute_group pdis_format_group = {
@@ -444,3 +537,22 @@ static struct pmu pdis_pmu = {
.check_period = pdis_check_period,
.sched_task = pdis_sched_task,
};
+
+int riscv_pdis_register(struct device *parent)
+{
+ int ret;
+
+ if (cpumask_empty(&pdis_cpus)) {
+ if (pdis_test)
+ dev_info(parent, "PDIS test PMU unavailable on all online CPUs\n");
+ return -ENODEV;
+ }
+ ret = cpu_pm_register_notifier(&pdis_cpu_pm_nb);
+ if (ret)
+ return ret;
+ pdis_pmu.parent = parent;
+ ret = perf_pmu_register(&pdis_pmu, "riscv_pdis_test", -1);
+ if (ret)
+ cpu_pm_unregister_notifier(&pdis_cpu_pm_nb);
+ return ret;
+}
diff --git a/drivers/perf/riscv_pdis.h b/drivers/perf/riscv_pdis.h
index b1ffe7d16ce0..8a9cf7afe51e 100644
--- a/drivers/perf/riscv_pdis.h
+++ b/drivers/perf/riscv_pdis.h
@@ -2,12 +2,22 @@
#ifndef __RISCV_PDIS_H
#define __RISCV_PDIS_H
+#include <linux/types.h>
+
+struct device;
+
#ifdef CONFIG_RISCV_PDIS_CSR
void riscv_pdis_cpu_starting(void);
void riscv_pdis_cpu_dying(void);
+bool riscv_pdis_irq_prepare(void);
+void riscv_pdis_irq_finish(void);
+int riscv_pdis_register(struct device *parent);
#else
static inline void riscv_pdis_cpu_starting(void) { }
static inline void riscv_pdis_cpu_dying(void) { }
+static inline bool riscv_pdis_irq_prepare(void) { return false; }
+static inline void riscv_pdis_irq_finish(void) { }
+static inline int riscv_pdis_register(struct device *parent) { return 0; }
#endif
#endif /* __RISCV_PDIS_H */
diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index 545b8becf90a..af86a30aae83 100644
--- a/drivers/perf/riscv_pmu_sbi.c
+++ b/drivers/perf/riscv_pmu_sbi.c
@@ -1092,24 +1092,23 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
struct cpu_hw_events *cpu_hw_evt = dev;
u64 start_clock = sched_clock();
struct riscv_pmu_snapshot_data *sdata;
+ bool handled;
if (WARN_ON_ONCE(!cpu_hw_evt))
return IRQ_NONE;
sdata = cpu_hw_evt->snapshot_addr;
+ /* Inhibit and collect PDIS before any shared pending bit is cleared. */
+ handled = riscv_pdis_irq_prepare();
/* Firmware counter don't support overflow yet */
fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
- if (fidx == RISCV_MAX_COUNTERS) {
- csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
- return IRQ_NONE;
- }
+ if (fidx == RISCV_MAX_COUNTERS)
+ goto out_clear;
event = cpu_hw_evt->events[fidx];
- if (!event) {
- ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
- return IRQ_NONE;
- }
+ if (!event)
+ goto out_clear;
pmu = to_riscv_pmu(event->pmu);
pmu_sbi_stop_hw_ctrs(pmu);
@@ -1128,7 +1127,7 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
/* No overflow bit is set */
if (!overflow)
- return IRQ_NONE;
+ goto restart;
regs = get_irq_regs();
@@ -1181,10 +1180,20 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
hw_evt->state = 0;
}
- pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
- perf_sample_event_took(sched_clock() - start_clock);
+ handled = true;
- return IRQ_HANDLED;
+restart:
+ /* A PDIS-only interrupt must also resume the ordinary HPM counters. */
+ pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
+out:
+ /* PDIS can restart only after all sources and shared pending are serviced. */
+ riscv_pdis_irq_finish();
+ if (handled)
+ perf_sample_event_took(sched_clock() - start_clock);
+ return handled ? IRQ_HANDLED : IRQ_NONE;
+out_clear:
+ ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
+ goto out;
}
static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
@@ -1563,6 +1572,11 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)
ret = cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
if (ret)
goto out_unregister;
+ if (irq_requested && riscv_pmu_irq_num == RV_IRQ_PMU) {
+ ret = riscv_pdis_register(&pdev->dev);
+ if (ret && ret != -ENODEV)
+ pr_warn("PDIS test PMU registration failed: %d\n", ret);
+ }
/* Asynchronously check which standard events are available */
schedule_work(&check_std_events_work);
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt
2026-11-18 8:13 ` [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt Zhanpeng Zhang
@ 2026-11-18 8:47 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-11-18 8:47 UTC (permalink / raw)
To: Zhanpeng Zhang, linux-perf-users; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] perf: riscv: transient pdis_cpus clear in
pdis_cpu_pm_notify() on idle exit
- [Medium] perf: riscv: pmu_sbi_ovf_handler() restarts stopped counters
on PDIS IRQ
- [Medium] perf: riscv: pcpu->ready left enabled on failure in
riscv_pdis_register()
--
commit cd24c079596cb117f90d17389f647a0a1991c1b3
Author: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
perf: riscv: Deliver PDIS samples through the SBI PMU interrupt
This commit wires up PDIS overflow handling into the shared SBI PMU
interrupt handler, registers a CPU PM notifier to preserve PDIS state
across idle states, and registers the PDIS test PMU from the SBI PMU
probe path.
> diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
> index 9a8fe5e4ab828..60c9ece22dfce 100644
> --- a/drivers/perf/riscv_pdis.c
> +++ b/drivers/perf/riscv_pdis.c
[ ... ]
> @@ -413,6 +480,32 @@ void riscv_pdis_cpu_dying(void)
> cpumask_clear_cpu(smp_processor_id(), &pdis_cpus);
> }
>
> +static int pdis_cpu_pm_notify(struct notifier_block *nb, unsigned long cmd, void *v)
> +{
> + struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
> + struct perf_event *event = pcpu->event;
> +
> + if (!pcpu->ready)
> + return NOTIFY_DONE;
> + if (cmd == CPU_PM_ENTER) {
> + pcpu->pm_resume = event && !(event->hw.state & PERF_HES_STOPPED);
> + if (event)
> + pdis_stop(event, PERF_EF_UPDATE);
> + else
> + pdis_quiesce();
> + } else if (cmd == CPU_PM_EXIT || cmd == CPU_PM_ENTER_FAILED) {
> + riscv_pdis_cpu_starting();
> + if (pcpu->pm_resume && pcpu->ready && event)
> + pdis_start(event, PERF_EF_RELOAD);
> + pcpu->pm_resume = false;
> + }
> + return NOTIFY_OK;
> +}
[Severity: Medium]
Can calling riscv_pdis_cpu_starting() on CPU_PM_EXIT or
CPU_PM_ENTER_FAILED cause concurrent perf_event_open() calls targeting the
waking CPU to fail spuriously with -ENODEV?
riscv_pdis_cpu_starting() clears the CPU from pdis_cpus before re-probing
device tree properties and CSRs:
drivers/perf/riscv_pdis.c:riscv_pdis_cpu_starting() {
pcpu->ready = false;
pcpu->counter_valid = false;
cpumask_clear_cpu(cpu, &pdis_cpus);
...
pcpu->ready = true;
cpumask_set_cpu(cpu, &pdis_cpus);
}
Because CPU_PM_EXIT runs on every deep cpuidle wakeup without serializing
against pdis_event_init() or cpumask_show() on other CPUs, a concurrent
reader can observe the online CPU missing from pdis_cpus:
CPU0 CPU1
pdis_cpu_pm_notify(CPU_PM_EXIT)
riscv_pdis_cpu_starting()
cpumask_clear_cpu(0, &pdis_cpus)
pdis_test_model_matches(0)
perf_event_open(..., cpu=0)
pdis_event_init()
!cpumask_test_cpu(0, &pdis_cpus)
--> returns -ENODEV
cpumask_set_cpu(0, &pdis_cpus)
[ ... ]
> @@ -444,3 +537,22 @@ static struct pmu pdis_pmu = {
> .check_period = pdis_check_period,
> .sched_task = pdis_sched_task,
> };
> +
> +int riscv_pdis_register(struct device *parent)
> +{
> + int ret;
> +
> + if (cpumask_empty(&pdis_cpus)) {
> + if (pdis_test)
> + dev_info(parent, "PDIS test PMU unavailable on all online CPUs\n");
> + return -ENODEV;
> + }
> + ret = cpu_pm_register_notifier(&pdis_cpu_pm_nb);
> + if (ret)
> + return ret;
> + pdis_pmu.parent = parent;
> + ret = perf_pmu_register(&pdis_pmu, "riscv_pdis_test", -1);
> + if (ret)
> + cpu_pm_unregister_notifier(&pdis_cpu_pm_nb);
> + return ret;
> +}
[Severity: Medium]
If riscv_pdis_register() fails, does it leave pcpu->ready and pdis_test
enabled while the SBI PMU continues running without pdis_cpu_pm_nb
registered?
pmu_sbi_device_probe() calls cpuhp_state_add_instance() before
riscv_pdis_register(), which sets pcpu->ready = true on online CPUs via
pmu_sbi_starting_cpu() -> riscv_pdis_cpu_starting(), and ignores
registration errors from riscv_pdis_register(). If
cpu_pm_register_notifier() or perf_pmu_register() fails (or if
riscv_pdis_register() returns -ENODEV and a PDIS-capable CPU is brought
online later), riscv_pdis_irq_prepare() still runs on every SBI PMU
interrupt and executes an unguarded csr_clear(RISCV_PDIS_CSR_CTL,
RISCV_PDIS_CTL_OF) without pdis_cpu_pm_notify() registered to quiesce or
re-probe PDIS CSRs across non-retentive CPU idle transitions.
> diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
> index 545b8becf90ae..af86a30aae835 100644
> --- a/drivers/perf/riscv_pmu_sbi.c
> +++ b/drivers/perf/riscv_pmu_sbi.c
> @@ -1092,24 +1092,23 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
[ ... ]
> @@ -1128,7 +1127,7 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
>
> /* No overflow bit is set */
> if (!overflow)
> - return IRQ_NONE;
> + goto restart;
>
> regs = get_irq_regs();
>
> @@ -1181,10 +1180,20 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
> hw_evt->state = 0;
> }
>
> - pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
> - perf_sample_event_took(sched_clock() - start_clock);
> + handled = true;
>
> - return IRQ_HANDLED;
> +restart:
> + /* A PDIS-only interrupt must also resume the ordinary HPM counters. */
> + pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
[Severity: Medium]
Does jumping to restart on a PDIS-only interrupt start SBI hardware
counters that perf has stopped or throttled?
When an SBI PMU event is stopped via riscv_pmu_stop() (for example by
PERF_EVENT_IOC_DISABLE or perf_event_throttle()), event->hw.state has
PERF_HES_STOPPED set, but its bit in cpu_hw_evt->used_hw_ctrs remains set
until riscv_pmu_del(). pmu_sbi_start_overflow_mask() starts every counter
in used_hw_ctrs & ~ctr_ovf_mask without checking PERF_HES_STOPPED:
drivers/perf/riscv_pmu_sbi.c:pmu_sbi_start_ovf_ctrs_sbi() {
for (i = 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) {
ctr_start_mask = cpu_hw_evt->used_hw_ctrs[i] & ~ctr_ovf_mask;
/* Start all the counters that did not overflow in a single shot */
if (ctr_start_mask) {
sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, i * BITS_PER_LONG,
ctr_start_mask, 0);
}
}
}
When a PDIS-only interrupt fires on a CPU with a stopped SBI HPM event,
overflow is 0 and overflowed_ctrs is 0, so pmu_sbi_start_overflow_mask()
restarts the stopped hardware counter behind perf's back.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118081327.10684-7-zhangzhanpeng.jasper@bytedance.com?part=5
^ permalink raw reply [flat|nested] 10+ messages in thread