[RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS

From: Zhanpeng Zhang

Date: Wed Nov 18 2026 - 03:13:44 EST


This RFC exposes Precise Decoded Instruction Sampling (PDIS) as a
per-CPU perf event on RV64. It targets the Smpdis/Sspdis draft at
2de60e18118ce78e2da82ec33296345c94d8560c:

https://github.com/riscv/riscv-performance-event-sampling/blob/2de60e18118ce78e2da82ec33296345c94d8560c/modules/ROOT/pages/spdis.adoc

The draft defines two destinations for sample records. With MEM=0,
hardware records samples in indirect supervisor control and status
registers (CSRs). With MEM=1, hardware writes records to the PDIS Memory
Buffer. This series implements the MEM=0 path and reads records from
the sample data CSRs.

PDIS selects instructions with its own counter and delivers records on
the local counter overflow interrupt shared with the SBI PMU. The
driver samples at a fixed period and reports the recorded PC. Each
event selects either host user mode or host supervisor mode. CSR
numbers and the state-enable bit are provisional. Probing requires a
matching CPU node and explicit boot opt-in.

Open questions
--------------

- The driver registers a dedicated perf PMU for per-CPU sampling. Is
this a reasonable starting point for PDIS support in perf?

- The model cancels in-flight selections when software sets inhibit and
clears spdisctl. What sequence guarantees that an old selection can no
longer publish a record before Linux switches tasks or events?

- The CSR record lacks a privilege field. If user and supervisor
sampling are enabled together, how should perf determine the mode of
the sampled instruction? Would it make sense to include the mode in
the record itself?

- The driver reports the recorded PC but rejects perf's precise_ip
requests. What guarantees about instruction selection and the recorded
PC would justify advertising a precise_ip level for PDIS?

- COUNT can reload several times before software reads it, while OF is
a single sticky bit. What should read() on a PDIS perf event report
when the driver can only estimate the number of matching instructions?

On firmware supporting SBI PMU snapshots, restarting ordinary counters
also requires the existing counter_idx_base fix posted by liutong:

https://lore.kernel.org/r/20260823044337.517125-1-liutong@xxxxxxxxxxx/

The reference OpenSBI firmware uses the non-snapshot path.

Reproduction
------------

The matching experimental trees are:

QEMU: https://github.com/bows7ring/qemu/tree/4ce5dec939149ccef78358d68ddc15df1fc0c55d
OpenSBI: https://github.com/bows7ring/opensbi/tree/1cdbbd84f83d80fdc6d62ea3347a4417b388784c

Build generic OpenSBI with CONFIG_XPDIS_TEST=y and Linux with
CONFIG_RISCV_PDIS_CSR=y. Boot virt with the resulting fw_dynamic.bin,
Image and a guest initramfs containing perf and the workload.

Use these QEMU options:

-machine virt -accel tcg -smp 2 -m 512M -icount shift=0,sleep=off
-cpu rv64,smstateen=on,smcsrind=on,sscsrind=on,sscofpmf=on,smcdeleg=off,ssccfg=off,x-pdis-regs=on,x-pdis-sample=on

In the test DT, append sspdis to each CPU's riscv,isa-extensions and
set riscv,pdis-test-model = "csr-v1". Add riscv_pdis_test=1 to the
kernel command line. This setup uses provisional CSR encodings.

The test ran the following from a CPU 1-pinned init. The /workload
binary pins itself to CPU 0 and loops over a labelled load instruction
400,000 times (one load and one store per iteration).

/perf record --no-inherit -m 8 -a -C 0 \
-e 'riscv_pdis_test/select=1/u' -c 4099 \
-o /tmp/pdis-u.data -- /workload
/perf script -i /tmp/pdis-u.data -F ip,event

On the QEMU model, perf received the expected PDIS samples in both
user-mode and kernel-mode runs.

Zhanpeng Zhang (5):
dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions
riscv: Detect Smpdis and Sspdis ISA extensions
perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs
perf: riscv: Add PDIS event and counter callbacks
perf: riscv: Deliver PDIS samples through the SBI PMU interrupt

.../admin-guide/kernel-parameters.txt | 10 +
.../devicetree/bindings/riscv/extensions.yaml | 19 +
MAINTAINERS | 1 +
arch/riscv/include/asm/hwcap.h | 2 +
arch/riscv/kernel/cpufeature.c | 2 +
drivers/perf/Kconfig | 13 +
drivers/perf/Makefile | 1 +
drivers/perf/riscv_pdis.c | 558 ++++++++++++++++++
drivers/perf/riscv_pdis.h | 23 +
drivers/perf/riscv_pmu_sbi.c | 43 +-
10 files changed, 660 insertions(+), 12 deletions(-)
create mode 100644 drivers/perf/riscv_pdis.c
create mode 100644 drivers/perf/riscv_pdis.h


base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.50.1 (Apple Git-155)