Linux RISC-V trace framework and drivers
Subsystems (4)
v7 cover Linux RISC-V trace framework and drivers
This series adds 'gtrace' which is an architecture-neutral hardware trace framework with RISC-V E-trace as its first backend. The 'gtrace' core framework has the minimal shared plumbing (component registration, path create/destroy, path start/stop, etc) and the RISC-V trace drivers are its first consumer. It should be possible for the required ARM components to be adapted to the same framework in a future.
This series also adds initial support for RISC-V trace component drivers. The RISC-V trace v1.0 specification is already ratified and can be found at:
https://github.com/riscv-non-isa/e-trace-encap/releases/tag/v1.0.0-ratified
https://github.com/riscv-non-isa/tg-nexus-trace/releases/tag/1.0_Ratified
The drivers use the common trace control interface. E-Trace remains the preferred format for the generic RISC-V compatible string; the encoder now reads back the accepted hardware format and updates the component's format.
In the future, there will be subsequent series adding:
1) Sysfs support
2) ACPI support
3) More trace drivers (such as funnel, ATB, etc)
4) Support for upcoming self-hosted trace specification
5) Arm component support under the gtrace framework
6) ... and more ...
These patches can also be found in the riscv_trace_support_v7 branch at:
https://github.com/mdchitale/linux.git
To test the patches, we need QEMU virt machine with RISC-V trace support which can be found in rv-etrace_v4 branch at:
https://gitlab.com/danielhb/qemu.git
To capture gtrace data using perf on QEMU virt machine do the following:
1) Launch QEMU virt machine
$ qemu-system-riscv64 -nographic -M virt -smp 2 -bios fw_dynamic.bin \
-kernel Image -append "root=/dev/vda rw console=ttyS0 earlycon=sbi" \
-drive file=rootfs.img,id=disk1,if=none,format=raw \
-device virtio-blk-device,drive=disk1
2) Run perf record to capture gtrace data
$ perf record --all-cpus -e gtrace/event=0x1/ <command>
3) The step2 would create a perf.data file which has the gtrace data.
Now run perf report -D and look for PERF_RECORD_AUXTRACE event
section(s) which point(s) to the actual gtrace data offset.Changes since v6:
- Rebased on latest upstream
- Add a gtrace component type of NONE version-zero components can match
- Hold gtrace component references for firmware-node and CPU-source lookups,
- Detach incoming/outgoing connection pointers before removing components
- Balance temporary OF node references and outgoing references on parser and platform-probe failures; fix the CPU-node diagnostic lifetime and the OF module device table declaration.
- Use a spinlock to guard trace-ID and component ready callbacks
- Probe SRCBITS using all possible allowed widths and select the final allowed width
- Read back the trace format (e-trace/n-trace) supported by hardware and update platform driver state accordingly
- Make ramsink register-pair helper functions generic to gtrace
- Copy AUX data from the actual path sink and not the first component that implemented the aux copy callback.
- Pass the available AUX byte limit to the core and sink callbacks
whereas sink driver returns the actual byte count and AUX flags.
- Release AUX mappings data directly in free_aux() instead of deferring it via workqueue
- Set the AUX_OUTPUT_HW_ID to the encoder's hardware source ID
- Other fixes for issues reported by Sashiko bot
Changes since v5:
- Moved the DT connection parsing into the gtrace core as gtrace-of.c.
- Added start_count and a lock to the core's private component data to serialize start/stop/copyto_auxbuf operations. Dropped perf_buf_lock.
- Added handling of partially enabled path by disabling all enabled components
- Added cycle detection when walking the trace component graph
- Added trace source ID configuration in the encoder driver
- Added trace format (E-Trace/N-Trace) selection via the platform driver compatible string. It is used by encoder and perf drivers to set it into hardware and perf data header respectively
- Modified the ramsink buffer setup to read back the trRamStart and trRamLimit registers, and program the sync frequency, memory format and stop-on-wrap settings at start time
- Improved error handling and added path owner in the perf driver
- Fixed several issues reported by sashiko-bot but some of its findings are deferred to a later version.
Changes since v4:
- Introduced a new architecture-neutral 'gtrace' (Generic Trace) framework and moved the RISC-V trace drivers on top of it instead of a RISC-V-only 'rvtrace' core.
- Added new PATCH3 "gtrace: Add RISC-V platform driver for the gtrace framework" to split out the RISC-V platform/DT code from the core framework patch.
- Moved all rvtrace driver sources from drivers/hwtracing/rvtrace/ to drivers/hwtracing/gtrace/, with single Kconfig, Makefile using
- Made the perf driver (previously rvtrace-perf.c) architecture-neutral and moved it into the gtrace framework as gtrace-perf.c; PMU name changed from "rvtrace" to "gtrace"
- Added new PATCH8 "perf: Add gtrace AUX buffer trace format type" to define PERF_AUX_FLAG_GTRACE_FORMAT_ETRACE which is used in later patches.
- copyto_auxbuf() and gtrace_path_copyto_auxbuf() now take an output parameter so the AUX data format can be reported by the sink driver by setting PERF_AUX_FLAG_GTRACE_FORMAT_ETRACE.
- Renamed the generic side of the perf tools support from rvtrace* to gtrace*. perf.data ABI is unchanged.
- Fixed gtrace ramsink start/stop to call gtrace_enable_component()/ gtrace_disable_component() to remove duplication with code in gtrace-core.c
- Updated the MAINTAINERS patch to refer to gtrace framework instead of rvtrace
Changes since v3:
- Rebased on Linux-7.1-rc1
- Use kzalloc_obj() in-place of kzalloc() in PATCH2, PATCH3 and PATCH9
- Improved PATCH7 to save the previous WP value
Changes since v2:
- Rebased on Linux-7.0-rc1
- Addressed Rob's comments on DT bindings in PATCH1
- Addressed ref-count related issues in rvtrace_of_parse_outconns() of PATCH2
- Made RVtrace framework more generic by avoiding implicit access to component registers in PATCH2
- More improvements in trRamStart/Limit/WP programming and other improvments in PATCH7
- Removed RVTRACE_BUF_LEN from PATCH9
- Removed redundant page_size from PATCH10
- Renamed found_etm in PATCH10
- Removed rvtrace_recording_init() declaration from header in PATCH11
Changes since v1:
- Rebased on Linux-6.18-rc3
- Addressed Rob's comments in dt-bindings added by PATCH1
- Get reference of conn->dest_fwnode and add missing break in rvtrace_of_parse_outconns() of rvtrace-platform drivers added by PATCH2
- Added new inline function rvtrace_comp_is_empty() in PATCH2 and used it in rvtrace_encoder_stop() added by PATCH5
- Fixed trRamWPLow usage in PATCH7
- Determine RAM sink buffer size based on component implementation ID and reduce default RAM sink buffer size to 1MB
- Add new PATCH8 to enable DMA_RESTRICTED_POOL in RISC-V defconfig so that implementations with RAM sink address restrictions can be handled.
Anup Patel (7):
dt-bindings: Add RISC-V trace component bindings
hwtracing: gtrace: Initial implementation of gtrace framework
gtrace: Add RISC-V platform driver for the gtrace framework
gtrace: Add functions to create/destroy a trace component path
gtrace: Add function to copy into perf AUX buffer
riscv: Enable DMA_RESTRICTED_POOL in defconfig
MAINTAINERS: Add entry for RISC-V trace framework
Mayuresh Chitale (7):
gtrace: Add functions to start/stop tracing on a component path
gtrace: Add RISC-V Trace encoder driver
perf: Add gtrace AUX buffer trace format type
gtrace: Add RISC-V Trace ramsink driver
gtrace: Add perf driver for tracing using perf tool perf tools: Add RISC-V trace PMU record capabilities perf tools: Initial support for gtrace decoder
.../bindings/riscv/riscv,trace-component.yaml | 120 +++ MAINTAINERS | 11 + arch/riscv/configs/defconfig | 1 + drivers/Makefile | 1 + drivers/hwtracing/Kconfig | 2 + drivers/hwtracing/gtrace/Kconfig | 59 ++ drivers/hwtracing/gtrace/Makefile | 9 + drivers/hwtracing/gtrace/gtrace-core.c | 962 ++++++++++++++++++ drivers/hwtracing/gtrace/gtrace-of.c | 133 +++ drivers/hwtracing/gtrace/gtrace-perf.c | 368 +++++++ drivers/hwtracing/gtrace/rvtrace-encoder.c | 274 +++++ drivers/hwtracing/gtrace/rvtrace-platform.c | 194 ++++ drivers/hwtracing/gtrace/rvtrace-ramsink.c | 366 +++++++ drivers/hwtracing/gtrace/rvtrace.h | 121 +++ include/linux/gtrace.h | 382 +++++++ include/uapi/linux/perf_event.h | 5 + tools/include/uapi/linux/perf_event.h | 5 + tools/perf/arch/riscv/util/Build | 1 + tools/perf/arch/riscv/util/auxtrace.c | 220 ++++ tools/perf/util/Build | 1 + tools/perf/util/auxtrace.c | 4 + tools/perf/util/auxtrace.h | 1 + tools/perf/util/gtrace-decoder.c | 99 ++ tools/perf/util/gtrace.h | 17 + 24 files changed, 3356 insertions(+) create mode 100644 Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml create mode 100644 drivers/hwtracing/gtrace/Kconfig create mode 100644 drivers/hwtracing/gtrace/Makefile create mode 100644 drivers/hwtracing/gtrace/gtrace-core.c create mode 100644 drivers/hwtracing/gtrace/gtrace-of.c create mode 100644 drivers/hwtracing/gtrace/gtrace-perf.c create mode 100644 drivers/hwtracing/gtrace/rvtrace-encoder.c create mode 100644 drivers/hwtracing/gtrace/rvtrace-platform.c create mode 100644 drivers/hwtracing/gtrace/rvtrace-ramsink.c create mode 100644 drivers/hwtracing/gtrace/rvtrace.h create mode 100644 include/linux/gtrace.h create mode 100644 tools/perf/arch/riscv/util/auxtrace.c create mode 100644 tools/perf/util/gtrace-decoder.c create mode 100644 tools/perf/util/gtrace.h
base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
Signature · 1 line
2.43.0
PATCH v7 1/14 dt-bindings: Add RISC-V trace component bindings
From: Anup Patel <[email protected]>
Add device tree bindings for the memory mapped RISC-V trace components which support both the RISC-V efficient trace (E-trace) protocol and the RISC-V Nexus-based trace (N-trace) protocol.
The RISC-V trace components are defined by the RISC-V trace control interface specification.
--- .../bindings/riscv/riscv,trace-component.yaml | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml
Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml new file +120 -0
@@ -0,0 +1,120 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/riscv/riscv,trace-component.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: RISC-V Trace Component + +maintainers: + - Anup Patel <[email protected]> + +description: + The RISC-V trace control interface specification standard memory mapped + components (or devices) which support both the RISC-V efficient trace + (E-trace) protocol and the RISC-V Nexus-based trace (N-trace) protocol. + The RISC-V trace components have implementation specific directed acyclic + graph style interdependency where output of one component serves as input + to another component and certain components (such as funnel) can take inputs + from multiple components. The type and version of a RISC-V trace component + can be discovered from it's IMPL memory mapped register hence component + specific compatible strings are not needed. + +properties: + compatible: + items: + - enum: + - qemu,trace-component + - const: riscv,trace-component + + reg: + maxItems: 1 + + cpus: + maxItems: 1 + description: + phandle to the cpu to which the RISC-V trace component is bound. + + in-ports: + $ref: /schemas/graph.yaml#/properties/ports + patternProperties: + '^port(@[0-7])?$': + description: Input connections from RISC-V trace component + $ref: /schemas/graph.yaml#/properties/port + + out-ports: + $ref: /schemas/graph.yaml#/properties/ports + patternProperties: + '^port(@[0-7])?$': + description: Output connections from RISC-V trace component + $ref: /schemas/graph.yaml#/properties/port + +required: + - compatible + - reg + +anyOf: + - required: [ in-ports ] + - required: [ out-ports ] + +unevaluatedProperties: false + +examples: + - | + // Example 1 (Per-hart encoder and ramsink components): + + trace@c000000 { + compatible = "qemu,trace-component", "riscv,trace-component"; + reg = <0xc000000 0x1000>; + cpus = <&CPU0>; + + out-ports { + port { + CPU0_ENCODER_OUTPUT: endpoint { + remote-endpoint = <&CPU0_RAMSINK_INPUT>; + }; + }; + }; + }; + + trace@c001000 { + compatible = "qemu,trace-component", "riscv,trace-component"; + reg = <0xc001000 0x1000>; + cpus = <&CPU0>; + + in-ports { + port { + CPU0_RAMSINK_INPUT: endpoint { + }; + }; + }; + }; + + trace@c002000 { + compatible = "qemu,trace-component", "riscv,trace-component"; + reg = <0xc002000 0x1000>; + cpus = <&CPU1>; + + out-ports { + port { + CPU1_ENCODER_OUTPUT: endpoint { + remote-endpoint = <&CPU1_RAMSINK_INPUT>; + }; + }; + }; + }; + + trace@c003000 { + compatible = "qemu,trace-component", "riscv,trace-component"; + reg = <0xc003000 0x1000>; + cpus = <&CPU1>; + + in-ports { + port { + CPU1_RAMSINK_INPUT: endpoint { + }; + }; + }; + }; + +...
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 01/14] dt-bindings: Add RISC-V trace component bindings
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Low] dt-bindings: riscv: redundant bindings word in commit subject - [Low] dt-bindings: riscv: arbitrary…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Low] dt-bindings: riscv: redundant bindings word in commit subject
- [Low] dt-bindings: riscv: arbitrary 8-port limit in in-ports and out-ports
- [Low] dt-bindings: riscv: missing reciprocal remote-endpoint in example
--
commit 1257d7f086d130afccb10391f20f195c41e67ce3
Author: Anup Patel <[email protected]>
dt-bindings: Add RISC-V trace component bindings
This commit adds a Devicetree binding schema for memory-mapped RISC-V trace components supporting E-trace and N-trace protocols.
Quoted 1 line
dt-bindings: Add RISC-V trace component bindings
[Severity: Low]
The dt-bindings: prefix already indicates that this patch adds a binding. Could the subject drop the redundant trailing word "bindings" and include the riscv: subsystem prefix, such as "dt-bindings: riscv: Add RISC-V trace component"?
Quoted 6 lines
Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml new file +0
-0@@ -0,0 +1,120 @@
[ ... ]
Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml line 50
Quoted 13 lines
Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml new file +13
-0+ in-ports: + $ref: /schemas/graph.yaml#/properties/ports + patternProperties: + '^port(@[0-7])?$': + description: Input connections from RISC-V trace component + $ref: /schemas/graph.yaml#/properties/port + + out-ports: + $ref: /schemas/graph.yaml#/properties/ports + patternProperties: + '^port(@[0-7])?$': + description: Output connections from RISC-V trace component + $ref: /schemas/graph.yaml#/properties/port
[Severity: Low]
Why are in-ports and out-ports restricted to unit addresses 0 through 7? The RISC-V Trace Control Interface specification supports up to 16 funnel inputs (trTfDisInput bits 0..15) and arbitrary implementation-specific graph topologies, so components with 9 or more ports would fail schema validation unless standard hexadecimal port addresses such as
^port(@[0-9a-f]+)?$ are allowed.
[ ... ]
Quoted 47 lines
Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml new file +47
-0+examples: + - | + // Example 1 (Per-hart encoder and ramsink components): + + trace@c000000 { + compatible = "qemu,trace-component", "riscv,trace-component"; + reg = <0xc000000 0x1000>; + cpus = <&CPU0>; + + out-ports { + port { + CPU0_ENCODER_OUTPUT: endpoint { + remote-endpoint = <&CPU0_RAMSINK_INPUT>; + }; + }; + }; + }; + + trace@c001000 { + compatible = "qemu,trace-component", "riscv,trace-component"; + reg = <0xc001000 0x1000>; + cpus = <&CPU0>; + + in-ports { + port { + CPU0_RAMSINK_INPUT: endpoint { + }; + }; + }; + }; + + trace@c002000 { + compatible = "qemu,trace-component", "riscv,trace-component"; + reg = <0xc002000 0x1000>; + cpus = <&CPU1>; + + out-ports { + port { + CPU1_ENCODER_OUTPUT: endpoint { + remote-endpoint = <&CPU1_RAMSINK_INPUT>; + }; + }; + }; + }; + + trace@c003000 { + compatible = "qemu,trace-component", "riscv,trace-component";
Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml line 118
Quoted 10 lines
Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml new file +10
-0+ reg = <0xc003000 0x1000>; + cpus = <&CPU1>; + + in-ports { + port { + CPU1_RAMSINK_INPUT: endpoint { + }; + }; + }; + };
[Severity: Low]
Should CPU0_RAMSINK_INPUT and CPU1_RAMSINK_INPUT include reciprocal remote-endpoint properties pointing back to <&CPU0_ENCODER_OUTPUT> and <&CPU1_ENCODER_OUTPUT>? Omitting them leaves the OF graph connections unidirectional and the encoder endpoint labels unreferenced, which triggers dtc graph_endpoint and dt-check-style unused-label warnings.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v7 2/14 hwtracing: gtrace: Initial implementation of gtrace framework
In reply to Mayuresh ChitaleFrom: Anup Patel <[email protected]>
Implement a generic and architecture neutral trace framework (gtrace) in which trace components are organized in a graph-like topology. The framework provides a bus for the trace components along with helpers to register components, build the topology and access the hardware registers.
--- drivers/Makefile | 1 + drivers/hwtracing/Kconfig | 2 + drivers/hwtracing/gtrace/Kconfig | 15 + drivers/hwtracing/gtrace/Makefile | 4 + drivers/hwtracing/gtrace/gtrace-core.c | 524 +++++++++++++++++++++++++ include/linux/gtrace.h | 313 +++++++++++++++ 6 files changed, 859 insertions(+) create mode 100644 drivers/hwtracing/gtrace/Kconfig create mode 100644 drivers/hwtracing/gtrace/Makefile create mode 100644 drivers/hwtracing/gtrace/gtrace-core.c create mode 100644 include/linux/gtrace.h
drivers/Makefile +1 -0
@@ -178,6 +178,7 @@ obj-$(CONFIG_CORESIGHT) += hwtracing/coresight/ obj-y += hwtracing/intel_th/ obj-$(CONFIG_STM) += hwtracing/stm/ obj-$(CONFIG_HISI_PTT) += hwtracing/ptt/ +obj-$(CONFIG_GTRACE) += hwtracing/gtrace/ obj-y += android/ obj-$(CONFIG_NVMEM) += nvmem/ obj-$(CONFIG_FPGA) += fpga/
drivers/hwtracing/Kconfig +2 -0
@@ -7,4 +7,6 @@ source "drivers/hwtracing/intel_th/Kconfig" source "drivers/hwtracing/ptt/Kconfig" +source "drivers/hwtracing/gtrace/Kconfig" + endmenu
drivers/hwtracing/gtrace/Kconfig new file +15 -0
@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only + +menuconfig GTRACE + tristate "Generic Hardware Trace Support" + depends on OF + help + This framework provides an architecture-neutral kernel interface + for hardware trace drivers. It builds a topological view of the + trace components and configures the correct series of components + when trace is enabled. It is intended to be shared across RISC-V + and ARM trace components so that a single trace path may span + components of different architectures. + + To compile this driver as a module, choose M here: the module + will be called gtrace.
drivers/hwtracing/gtrace/Makefile new file +4 -0
@@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_GTRACE) += gtrace.o +gtrace-y := gtrace-core.o
drivers/hwtracing/gtrace/gtrace-core.c new file +524 -0
524 lines not on this page · raw message
include/linux/gtrace.h new file +313 -0
313 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 02/14] hwtracing: gtrace: Initial implementation of gtrace framework
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] hwtracing: gtrace: unchecked 0 return in gtrace_setup_inconns_from_outconns() - [Medium] hwtracing…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] hwtracing: gtrace: unchecked 0 return in gtrace_setup_inconns_from_outconns()
- [Medium] hwtracing: gtrace: refcount leak overwriting gtrace_cpu_source_comp
--
commit 8c67f8e6b67201b62b509964fb9df143def486d0
Author: Anup Patel <[email protected]>
hwtracing: gtrace: Initial implementation of gtrace framework
This commit introduces the core generic hardware trace (gtrace) bus and framework for registering trace components, building their connection topology, and accessing hardware registers.
Quoted 6 lines
drivers/hwtracing/gtrace/gtrace-core.c new file +0
-0@@ -0,0 +1,524 @@
[ ... ]
Quoted 29 lines
drivers/hwtracing/gtrace/gtrace-core.c new file +29
-0+static int gtrace_setup_inconn (struct device *dev,void *data) +{ +struct gtrace_component *comp =to_gtrace_component (dev); +struct gtrace_platform_data *pdata = comp->pdata; +struct gtrace_connection *conn = data; +int i; + +if (device_match_fwnode (&comp->dev, conn->dest_fwnode)) { +for (i =0 ; i < pdata->nr_inconns; i++) { +if (pdata->inconns[i]) +continue ; + pdata->inconns[i] = conn; +return 1 ; + } +return -ENOSPC ; + } + +return 0 ; +} + +static int gtrace_setup_inconns_from_outconns (struct gtrace_component *comp) +{ +struct gtrace_platform_data *pdata = comp->pdata; +struct gtrace_connection *conn; +int i, ret; + +lockdep_assert_held (>race_mutex); + +for (i =0 ; i < pdata->nr_outconns; i++) {
drivers/hwtracing/gtrace/gtrace-core.c line 338
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-core.c new file +10
-0+ conn = pdata->outconns[i]; + ret =bus_for_each_dev (>race_bustype,NULL , conn, gtrace_setup_inconn); +if (ret <0 ) { +gtrace_cleanup_inconns_from_outconns (comp); +return ret; + } + } + +return 0 ; +}
[Severity: High]
Can gtrace_setup_inconns_from_outconns() silently succeed when bus_for_each_dev() returns 0 because no registered component matched conn->dest_fwnode?
The caller resolves conn->dest_comp via gtrace_find_by_fwnode() before calling gtrace_register_component(), which drops gtrace_mutex in between. If conn->dest_comp is unregistered in that window (or if conn->dest_fwnode does not match any device on gtrace_bustype), bus_for_each_dev() returns 0. Checking only ret < 0 lets registration succeed without linking conn into any destination component's inconns[] array, leaving conn->dest_comp pointing to an unregistered component whose pdata has been freed.
Also, gtrace_setup_inconn() and gtrace_cleanup_inconn() match devices only by conn->dest_fwnode without checking comp == conn->dest_comp. If the destination device was unbound and rebound in that window, conn is attached to the new component while conn->dest_comp still holds a reference to the old one; when gtrace_cleanup_inconns() later runs on the new component, it calls gtrace_put_component(comp) and underflows the new component's reference count while leaking the old conn->dest_comp reference.
[ ... ]
Quoted 17 lines
drivers/hwtracing/gtrace/gtrace-core.c new file +17
-0+if (bound_cpu >=0 && is_source) { +gtrace_get_component (comp); +per_cpu (gtrace_cpu_source_comp, bound_cpu) = comp; + } + +mutex_unlock (>race_mutex); + +return comp; + +err_out_unlock: +mutex_unlock (>race_mutex); +err_out: +return ERR_PTR (ret); +} +EXPORT_SYMBOL_GPL (gtrace_register_component); + +void gtrace_unregister_component (struct gtrace_component *comp)
drivers/hwtracing/gtrace/gtrace-core.c line 488
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-core.c new file +10
-0+{ +struct gtrace_platform_data *pdata = comp->pdata; + +mutex_lock (>race_mutex); + +if (pdata->bound_cpu >=0 && +per_cpu (gtrace_cpu_source_comp, pdata->bound_cpu) == comp) { +per_cpu (gtrace_cpu_source_comp, pdata->bound_cpu) =NULL ; +gtrace_put_component (comp); + }
[Severity: Medium]
Does gtrace_register_component() leak the existing component reference if a second source component is registered for the same bound_cpu?
Each component gets a unique type_idx device name suffix, so device_register() succeeds even if per_cpu(gtrace_cpu_source_comp, bound_cpu) is already populated. Overwriting the per-CPU pointer without checking if it is already non-NULL causes gtrace_unregister_component() on the earlier component to see per_cpu(gtrace_cpu_source_comp, pdata->bound_cpu) != comp and skip gtrace_put_component().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v7 3/14 gtrace: Add RISC-V platform driver for the gtrace framework
In reply to Mayuresh ChitaleFrom: Anup Patel <[email protected]>
The RISC-V trace components defined by the RISC-V Trace Control Interface Specification are memory mapped devices which are organized in a graph-like topology and described in the device tree.
Add the RISC-V platform driver for the gtrace framework. The driver parses the RISC-V component topology from the device tree, discovers the type and version of each component from its IMPL register, provides the RISC-V register level control ops and registers each component with the gtrace core.
--- drivers/hwtracing/gtrace/Kconfig | 15 ++ drivers/hwtracing/gtrace/Makefile | 3 + drivers/hwtracing/gtrace/gtrace-of.c | 133 ++++++++++++++ drivers/hwtracing/gtrace/rvtrace-platform.c | 194 ++++++++++++++++++++ drivers/hwtracing/gtrace/rvtrace.h | 121 ++++++++++++ include/linux/gtrace.h | 3 + 6 files changed, 469 insertions(+) create mode 100644 drivers/hwtracing/gtrace/gtrace-of.c create mode 100644 drivers/hwtracing/gtrace/rvtrace-platform.c create mode 100644 drivers/hwtracing/gtrace/rvtrace.h
drivers/hwtracing/gtrace/Kconfig +15 -0
@@ -13,3 +13,18 @@ menuconfig GTRACE To compile this driver as a module, choose M here: the module will be called gtrace. + +config RVTRACE + tristate "RISC-V Trace Support" + depends on RISCV + depends on OF + depends on GTRACE + default RISCV + help + This provides the RISC-V backend for the generic hardware trace + (gtrace) framework. It parses the RISC-V trace component topology, + provides the RISC-V register-level control ops and registers each + component with the gtrace core. + + To compile this driver as a module, choose M here: the module + will be called rvtrace.
drivers/hwtracing/gtrace/Makefile +3 -0
@@ -2,3 +2,6 @@ obj-$(CONFIG_GTRACE) += gtrace.o gtrace-y := gtrace-core.o +gtrace-$(CONFIG_OF) += gtrace-of.o + +obj-$(CONFIG_RVTRACE) += rvtrace-platform.o
drivers/hwtracing/gtrace/gtrace-of.c new file +133 -0
133 lines not on this page · raw message
drivers/hwtracing/gtrace/rvtrace-platform.c new file +194 -0
194 lines not on this page · raw message
drivers/hwtracing/gtrace/rvtrace.h new file +121 -0
121 lines not on this page · raw message
include/linux/gtrace.h +3 -0
@@ -277,6 +277,9 @@ struct gtrace_component *gtrace_register_component(struct gtrace_component_id *ivoid gtrace_unregister_component (struct gtrace_component *comp);void gtrace_release_outconns (struct gtrace_platform_data *pdata); +int gtrace_of_parse_outconns (struct gtrace_platform_data *pdata); +int gtrace_of_parse_inconns (struct gtrace_platform_data *pdata); +/** * struct gtrace_driver - Representation of a trace driver. * @id_table: Table to match components handled by the driver, terminated
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 03/14] gtrace: Add RISC-V platform driver for the gtrace framework
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] gtrace: device reference leak on dest_comp in gtrace_of_parse_outconns() - [Medium] gtrace: TOCTOU…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] gtrace: device reference leak on dest_comp in gtrace_of_parse_outconns()
- [Medium] gtrace: TOCTOU race between gtrace_of_parse_outconns() and registration
- [Low] gtrace: CONFIG_RVTRACE module name mismatch between Kconfig and Makefile
--
commit 4636c1fbe60ae0aca475723fde99c3bc6d009499
Author: Anup Patel <[email protected]>
gtrace: Add RISC-V platform driver for the gtrace framework
This commit adds device-tree connection parsing helpers and a RISC-V platform driver for the gtrace framework.
Quoted 28 lines
drivers/hwtracing/gtrace/Kconfig +15
-0@@ -13,3 +13,18 @@ menuconfig GTRACE To compile this driver as a module, choose M here: the module will be called gtrace. + +config RVTRACE + tristate "RISC-V Trace Support" + depends on RISCV + depends on OF + depends on GTRACE + default RISCV + help + This provides the RISC-V backend for the generic hardware trace + (gtrace) framework. It parses the RISC-V trace component topology, + provides the RISC-V register-level control ops and registers each + component with the gtrace core. + + To compile this driver as a module, choose M here: the module + will be called rvtrace.drivers/hwtracing/gtrace/Makefile +0
-0
drivers/hwtracing/gtrace/Makefile line 7
Quoted 6 lines
drivers/hwtracing/gtrace/Makefile +3
-0@@ -2,3 +2,6 @@ obj-$(CONFIG_GTRACE) += gtrace.o gtrace-y := gtrace-core.o +gtrace-$(CONFIG_OF) += gtrace-of.o + +obj-$(CONFIG_RVTRACE) += rvtrace-platform.o
[Severity: Low]
The Kconfig help text states that the module will be called rvtrace, but building CONFIG_RVTRACE=m produces rvtrace-platform.ko.
Quoted 6 lines
drivers/hwtracing/gtrace/gtrace-of.c new file +0
-0@@ -0,0 +1,133 @@
[ ... ]
Quoted 25 lines
drivers/hwtracing/gtrace/gtrace-of.c new file +25
-0+static struct gtrace_connection * +gtrace_of_parse_outconn (struct gtrace_platform_data *pdata, +struct device_node *ep_node) +{ +struct device_node *rep_node, *rdev_node; +struct gtrace_connection *conn; +struct of_endpoint ep, rep; +int ret; + + conn =devm_kzalloc (pdata->dev,sizeof (*conn),GFP_KERNEL ); +if (!conn) +return ERR_PTR (-ENOMEM ); + + ret =of_graph_parse_endpoint (ep_node, &ep); +if (ret) +return ERR_PTR (ret); + + rep_node =of_graph_get_remote_endpoint (ep_node); +if (!rep_node) +return ERR_PTR (-ENODEV ); + + rdev_node =of_graph_get_port_parent (rep_node); + + ret =of_graph_parse_endpoint (rep_node, &rep); +if (ret)
drivers/hwtracing/gtrace/gtrace-of.c line 47
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-of.c new file +10
-0+ goto out; + + conn->src_port = ep.port; + conn->src_fwnode =dev_fwnode (pdata->dev); +/* The 'src_comp' is set by gtrace_register_component() */ + conn->src_comp =NULL ; + conn->dest_port = rep.port; + conn->dest_fwnode =of_fwnode_handle (rdev_node); +fwnode_handle_get (conn->dest_fwnode); + conn->dest_comp =gtrace_find_by_fwnode (conn->dest_fwnode);
[Severity: Medium]
Can conn->dest_comp become stale if the destination component is unregistered before gtrace_register_component() runs?
CPU0 CPU1rvtrace_platform_probe() [src]
gtrace_of_parse_outconns()
gtrace_of_parse_outconn()
gtrace_find_by_fwnode()
mutex_lock(>race_mutex)
gtrace_get_component(dest)
mutex_unlock(>race_mutex)
rvtrace_platform_remove() [dest]
gtrace_unregister_component(dest)
mutex_lock(>race_mutex)
gtrace_cleanup_inconns(dest, NULL)
device_unregister(&dest->dev)
mutex_unlock(>race_mutex)
/* devres frees dest->pdata */
gtrace_register_component()
mutex_lock(>race_mutex)
gtrace_setup_inconns_from_outconns()
bus_for_each_dev(..., gtrace_setup_inconn)
/* dest is off bus; returns 0 */
mutex_unlock(>race_mutex)Because conn has not been installed in dest->pdata->inconns[] yet when CPU1 runs gtrace_cleanup_inconns(), conn->dest_comp is not cleared. Then in gtrace_setup_inconns_from_outconns(), bus_for_each_dev() returns 0 because dest->dev is no longer on gtrace_bustype, and checking only ret < 0 treats that as success:
drivers/hwtracing/gtrace/gtrace-core.c:gtrace_setup_inconns_from_outconns() {
...
ret = bus_for_each_dev(>race_bustype, NULL, conn, gtrace_setup_inconn);
if (ret < 0) {
gtrace_cleanup_inconns_from_outconns(comp);
return ret;
}
...
}Registration then succeeds with conn->dest_comp pointing to an unregistered component whose devm-allocated dest->pdata has already been freed, leading to a use-after-free when walking output connections.
Quoted 46 lines
drivers/hwtracing/gtrace/gtrace-of.c new file +46
-0+if (!conn->dest_comp) { +fwnode_handle_put (conn->dest_fwnode); + ret = -EPROBE_DEFER ; + } + +out: +of_node_put (rdev_node); +of_node_put (rep_node); +return ret ?ERR_PTR (ret) : conn; +} + +/* +* Parse the "out-ports" graph of the component's device tree node and fill +* pdata->outconns. +* Return: 0 on success or when there are no output ports +* -EPROBE_DEFER if a destination is not registered yet +* negative error code otherwise. +*/ +int gtrace_of_parse_outconns (struct gtrace_platform_data *pdata) +{ +struct device_node *parent, *ep_node; +struct gtrace_connection *conn; +int ret =0 , i =0 ; + + parent =of_get_child_by_name (dev_of_node (pdata->dev),"out-ports" ); +if (!parent) +return 0 ; + + pdata->nr_outconns =of_graph_get_endpoint_count (parent); + pdata->outconns =devm_kcalloc (pdata->dev, pdata->nr_outconns, +sizeof (*pdata->outconns),GFP_KERNEL ); +if (!pdata->outconns) { + ret = -ENOMEM ; + goto done; + } + +for_each_endpoint_of_node (parent, ep_node) { + conn =gtrace_of_parse_outconn (pdata, ep_node); +if (IS_ERR (conn)) { + ret =PTR_ERR (conn); +of_node_put (ep_node); +break ; + } + + pdata->outconns[i] = conn; + i++;
drivers/hwtracing/gtrace/gtrace-of.c line 103
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-of.c new file +10
-0+ } + +done: +if (ret) { +for (i =0 ; i < pdata->nr_outconns && pdata->outconns; i++) { + conn = pdata->outconns[i]; +if (conn && conn->dest_fwnode) +fwnode_handle_put (conn->dest_fwnode); + } + }
[Severity: Medium]
Does this error path leak the conn->dest_comp device reference acquired by gtrace_find_by_fwnode()?
When a component has multiple output endpoints and parsing a later endpoint fails (such as with -EPROBE_DEFER), the loop only drops conn->dest_fwnode on the earlier connections and rvtrace_platform_probe() returns directly without calling gtrace_release_outconns(pdata). Calling gtrace_release_outconns(pdata) here when pdata->outconns is non-NULL would release both conn->dest_comp and conn->dest_fwnode.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v7 4/14 gtrace: Add functions to create/destroy a trace component path
In reply to Mayuresh ChitaleFrom: Anup Patel <[email protected]>
Trace needs to be configured on a chain of trace components which are connected to each other. These chain of components is also referred to as trace component path. Add functions to create/destroy a trace component path which will be later used by gtrace perf support.
--- drivers/hwtracing/gtrace/gtrace-core.c | 257 +++++++++++++++++++++++++ include/linux/gtrace.h | 27 ++- 2 files changed, 283 insertions(+), 1 deletion(-)
drivers/hwtracing/gtrace/gtrace-core.c +257 -0
@@ -26,6 +26,7 @@ struct gtrace_comp_priv {struct gtrace_component comp;u32 type_idx;bool ready; +bool visited; };#define to_gtrace_comp_priv (__comp) \
@@ -230,6 +231,62 @@ int gtrace_reset_component(struct gtrace_component *comp) }EXPORT_SYMBOL_GPL (gtrace_reset_component); +static int __gtrace_walk_output_components (struct gtrace_component *comp, +bool *stop,void *priv, +int (*fn)(struct gtrace_component *comp,bool *stop, +struct gtrace_connection *stop_conn, +void *priv)) +{ +struct gtrace_comp_priv *cpriv =to_gtrace_comp_priv (comp); +struct gtrace_connection *conn, *stop_conn =NULL ; +struct gtrace_platform_data *pdata = comp->pdata; +int i, ret; + +if (cpriv->visited) { +dev_err (&comp->dev,"loop detected, please fix the firmware\n" ); +return -ELOOP ; + } + cpriv->visited = true; + +for (i =0 ; i < pdata->nr_outconns; i++) { + conn = pdata->outconns[i]; + ret =__gtrace_walk_output_components (conn->dest_comp, stop, priv, fn); +if (ret) { + cpriv->visited = false; +return ret; + } + +if (*stop) { + stop_conn = conn; +break ; + } + } + + ret =fn (comp, stop, stop_conn, priv); + cpriv->visited = false; + +return ret; +} + +int gtrace_walk_output_components (struct gtrace_component *comp,void *priv, +int (*fn)(struct gtrace_component *comp,bool *stop, +struct gtrace_connection *stop_conn, +void *priv)) +{ +bool stop = false; +int ret; + +if (!comp || !fn) +return -EINVAL ; + +mutex_lock (>race_mutex); + ret =__gtrace_walk_output_components (comp, &stop, priv, fn); +mutex_unlock (>race_mutex); + +return ret; +} +EXPORT_SYMBOL_GPL (gtrace_walk_output_components); +struct gtrace_component *gtrace_cpu_source (unsigned int cpu) {struct gtrace_component *comp;
@@ -496,6 +553,206 @@ void gtrace_unregister_component(struct gtrace_component *comp) }EXPORT_SYMBOL_GPL (gtrace_unregister_component); +struct gtrace_path_node { +struct list_head head; +struct gtrace_component *comp; +struct gtrace_connection *conn; +}; + +struct gtrace_component *gtrace_path_source (struct gtrace_path *path) +{ +struct gtrace_path_node *node; + + node = list_first_entry(&path->comp_list, struct gtrace_path_node, head); +return node->comp; +} +EXPORT_SYMBOL_GPL (gtrace_path_source); + +struct gtrace_component *gtrace_path_sink (struct gtrace_path *path) +{ +struct gtrace_path_node *node; + + node = list_last_entry(&path->comp_list, struct gtrace_path_node, head); +return node->comp; +} +EXPORT_SYMBOL_GPL (gtrace_path_sink); + +static int gtrace_assign_trace_id (struct gtrace_path *path) +{ +const struct gtrace_driver *gtdrv; +struct gtrace_component *comp; +struct gtrace_path_node *node; +int trace_id; + +list_for_each_entry (node, &path->comp_list, head) { + comp = node->comp; +device_lock (&comp->dev); + gtdrv =to_gtrace_driver (comp->dev.driver); + +if (!gtdrv || !gtdrv->get_trace_id) { +device_unlock (&comp->dev); +continue ; + } + + trace_id = gtdrv->get_trace_id (comp, path->mode); +device_unlock (&comp->dev); +if (trace_id == -ENODATA ) +continue ; +if (trace_id <0 ) +return trace_id; + + path->trace_id = trace_id; +return 0 ; + } + +return 0 ; +} + +static void gtrace_unassign_trace_id (struct gtrace_path *path) +{ +const struct gtrace_driver *gtdrv; +struct gtrace_component *comp; +struct gtrace_path_node *node; + +if (path->trace_id ==GTRACE_INVALID_TRACE_ID ) +return ; + +list_for_each_entry (node, &path->comp_list, head) { + comp = node->comp; +device_lock (&comp->dev); + gtdrv =to_gtrace_driver (comp->dev.driver); + +if (!gtdrv || !gtdrv->put_trace_id) { +device_unlock (&comp->dev); +continue ; + } + + gtdrv->put_trace_id (comp, path->mode, path->trace_id); +device_unlock (&comp->dev); + } +} + +static bool gtrace_path_ready (struct gtrace_path *path) +{ +struct gtrace_comp_priv *cpriv; +struct gtrace_path_node *node; + +list_for_each_entry (node, &path->comp_list, head) { +device_lock (&node->comp->dev); + cpriv =to_gtrace_comp_priv (node->comp); +if (!cpriv->ready) { +device_unlock (&node->comp->dev); +return false; + } +device_unlock (&node->comp->dev); + } + +return true; +} + +struct build_path_walk_priv { +struct gtrace_path *path; +struct gtrace_component *sink; +}; + +static int build_path_walk_fn (struct gtrace_component *comp,bool *stop, +struct gtrace_connection *stop_conn, +void *priv) +{ +struct build_path_walk_priv *ppriv = priv; +struct gtrace_path *path = ppriv->path; +struct gtrace_path_node *node; + +if ((!ppriv->sink &>race_is_sink (comp->pdata)) || + (ppriv->sink && ppriv->sink == comp)) + *stop = true; + +if (*stop) { + node =kzalloc_obj (*node); +if (!node) +return -ENOMEM ; +INIT_LIST_HEAD (&node->head); +gtrace_get_component (comp); + node->comp = comp; + node->conn = stop_conn; +list_add (&node->head, &path->comp_list); + } + +return 0 ; +} + +static void gtrace_release_path_nodes (struct gtrace_path *path) +{ +struct gtrace_path_node *node, *node1; + +list_for_each_entry_safe (node, node1, &path->comp_list, head) { +list_del (&node->head); +gtrace_put_component (node->comp); +kfree (node); + } +} + +struct gtrace_path *gtrace_create_path (struct gtrace_component *source, +struct gtrace_component *sink, +enum gtrace_component_mode mode) +{ +struct build_path_walk_priv priv; +struct gtrace_path *path; +int ret =0 ; + +if (!source || mode >=GTRACE_COMPONENT_MODE_MAX ) { + ret = -EINVAL ; + goto err_out; + } + + path =kzalloc (sizeof (*path),GFP_KERNEL ); +if (!path) { + ret = -ENOMEM ; + goto err_out; + } +INIT_LIST_HEAD (&path->comp_list); + path->mode = mode; + path->trace_id =GTRACE_INVALID_TRACE_ID ; + + priv.path = path; + priv.sink = sink; + ret =gtrace_walk_output_components (source, &priv, build_path_walk_fn); +if (ret <0 ) + goto err_release_path_nodes; + +/* Before proceeding, check that a valid path was built. */ +if (list_empty (&path->comp_list)) { + ret = -ENODEV ; + goto err_release_path_nodes; + } + +if (!gtrace_path_ready (path)) { + ret = -EOPNOTSUPP ; + goto err_release_path_nodes; + } + + ret =gtrace_assign_trace_id (path); +if (ret <0 ) + goto err_release_path_nodes; + +return path; + +err_release_path_nodes: +gtrace_release_path_nodes (path); +kfree (path); +err_out: +return ERR_PTR (ret); +} +EXPORT_SYMBOL_GPL (gtrace_create_path); + +void gtrace_destroy_path (struct gtrace_path *path) +{ +gtrace_unassign_trace_id (path); +gtrace_release_path_nodes (path); +kfree (path); +} +EXPORT_SYMBOL_GPL (gtrace_destroy_path); +int __gtrace_register_driver (struct module *owner,struct gtrace_driver *gtdrv) { gtdrv->driver.owner = owner;
include/linux/gtrace.h +26 -1
@@ -269,6 +269,10 @@ int gtrace_enable_component(struct gtrace_component *comp);int gtrace_disable_component (struct gtrace_component *comp);int gtrace_reset_component (struct gtrace_component *comp); +int gtrace_walk_output_components (struct gtrace_component *comp,void *priv, +int (*fn)(struct gtrace_component *comp,bool *stop, +struct gtrace_connection *stop_conn, +void *priv));struct gtrace_component *gtrace_cpu_source (unsigned int cpu); struct gtrace_component *gtrace_register_component(struct gtrace_component_id *id,
@@ -280,13 +284,34 @@ void gtrace_release_outconns(struct gtrace_platform_data *pdata);int gtrace_of_parse_outconns (struct gtrace_platform_data *pdata);int gtrace_of_parse_inconns (struct gtrace_platform_data *pdata); +/** +* struct gtrace_path - Representation of a trace path from source to sink. +* @comp_list: List of trace components in the path. +* @mode: Usage mode for trace components. +* @trace_id: Hardware source ID, or GTRACE_INVALID_TRACE_ID if none is supplied. +*/ +struct gtrace_path { +struct list_head comp_list; +enum gtrace_component_mode mode; +u32 trace_id; +#define GTRACE_INVALID_TRACE_ID U32_MAX +}; + +struct gtrace_component *gtrace_path_source (struct gtrace_path *path); +struct gtrace_component *gtrace_path_sink (struct gtrace_path *path); +struct gtrace_path *gtrace_create_path (struct gtrace_component *source, +struct gtrace_component *sink, +enum gtrace_component_mode mode); +void gtrace_destroy_path (struct gtrace_path *path); +/** * struct gtrace_driver - Representation of a trace driver. * @id_table: Table to match components handled by the driver, terminated * by GTRACE_COMPONENT_TYPE_NONE. * @probe: Driver probe() function. * @remove: Driver remove() function. -* @get_trace_id: Get/allocate a trace ID. +* @get_trace_id: Get/allocate a trace ID. Returns a nonnegative ID, -ENODATA +* if this component supplies no ID, or another negative error. * @put_trace_id: Put/free a trace ID. * @driver: Device driver instance. */
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 04/14] gtrace: Add functions to create/destroy a trace component path
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] gtrace: NULL pointer dereference in __gtrace_walk_output_components() - [Medium] gtrace: unpaired…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] gtrace: NULL pointer dereference in __gtrace_walk_output_components()
- [Medium] gtrace: unpaired put_trace_id() calls in gtrace_unassign_trace_id()
- [Medium] gtrace: unpinned component driver across gtrace_create_path() and path lifetime
--
commit c1254df0912d56d211b33f383984d030e23fab3a
Author: Anup Patel <[email protected]>
gtrace: Add functions to create/destroy a trace component path
This commit adds functions to walk output components and create or destroy a trace component path from a source to a sink, including trace ID assignment.
Quoted 4 lines
drivers/hwtracing/gtrace/gtrace-core.c +0
-0
[ ... ]
Quoted 14 lines
drivers/hwtracing/gtrace/gtrace-core.c +10
-0@@ -230,6 +231,62 @@ int gtrace_reset_component(struct gtrace_component *comp) }EXPORT_SYMBOL_GPL (gtrace_reset_component); +static int __gtrace_walk_output_components (struct gtrace_component *comp, +bool *stop,void *priv, +int (*fn)(struct gtrace_component *comp,bool *stop, +struct gtrace_connection *stop_conn, +void *priv)) +{ +struct gtrace_comp_priv *cpriv =to_gtrace_comp_priv (comp); +struct gtrace_connection *conn, *stop_conn =NULL ; +struct gtrace_platform_data *pdata = comp->pdata; +int i, ret;
drivers/hwtracing/gtrace/gtrace-core.c int gtrace_reset_component(struct gtrace_component *comp) · line 253
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-core.c +10
-0+ +if (cpriv->visited) { +dev_err (&comp->dev,"loop detected, please fix the firmware\n" ); +return -ELOOP ; + } + cpriv->visited = true; + +for (i =0 ; i < pdata->nr_outconns; i++) { + conn = pdata->outconns[i]; + ret =__gtrace_walk_output_components (conn->dest_comp, stop, priv, fn);
[Severity: High]
Can conn->dest_comp be NULL here, leading to a NULL pointer dereference of comp->pdata and cpriv->visited on the recursive call?
When a downstream component is unregistered, gtrace_cleanup_inconns() sets conn->dest_comp = NULL on the shared connection entry that stays in the upstream component's pdata->outconns[] array:
drivers/hwtracing/gtrace/gtrace-core.c:gtrace_cleanup_inconns() {
...
pdata->inconns[i] = NULL;
conn->dest_comp = NULL;
gtrace_put_component(comp);
...
}[ ... ]
Quoted 3 lines
drivers/hwtracing/gtrace/gtrace-core.c +0
-0@@ -496,6 +553,206 @@ void gtrace_unregister_component(struct gtrace_component *comp) }EXPORT_SYMBOL_GPL (gtrace_unregister_component);
[ ... ]
Quoted 44 lines
drivers/hwtracing/gtrace/gtrace-core.c +44
-0+static int gtrace_assign_trace_id (struct gtrace_path *path) +{ +const struct gtrace_driver *gtdrv; +struct gtrace_component *comp; +struct gtrace_path_node *node; +int trace_id; + +list_for_each_entry (node, &path->comp_list, head) { + comp = node->comp; +device_lock (&comp->dev); + gtdrv =to_gtrace_driver (comp->dev.driver); + +if (!gtdrv || !gtdrv->get_trace_id) { +device_unlock (&comp->dev); +continue ; + } + + trace_id = gtdrv->get_trace_id (comp, path->mode); +device_unlock (&comp->dev); +if (trace_id == -ENODATA ) +continue ; +if (trace_id <0 ) +return trace_id; + + path->trace_id = trace_id; +return 0 ; + } + +return 0 ; +} + +static void gtrace_unassign_trace_id (struct gtrace_path *path) +{ +const struct gtrace_driver *gtdrv; +struct gtrace_component *comp; +struct gtrace_path_node *node; + +if (path->trace_id ==GTRACE_INVALID_TRACE_ID ) +return ; + +list_for_each_entry (node, &path->comp_list, head) { + comp = node->comp;\ +device_lock (&comp->dev); + gtdrv =to_gtrace_driver (comp->dev.driver);
drivers/hwtracing/gtrace/gtrace-core.c void gtrace_unregister_component(struct gtrace_component *comp) · line 633
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-core.c +10
-0+ +if (!gtdrv || !gtdrv->put_trace_id) { +device_unlock (&comp->dev); +continue ; + } + + gtdrv->put_trace_id (comp, path->mode, path->trace_id); +device_unlock (&comp->dev); + } +}
[Severity: Medium]
Can gtrace_unassign_trace_id() call put_trace_id() on components that never allocated path->trace_id?
gtrace_assign_trace_id() skips components whose get_trace_id() returns -ENODATA and stops at the first component that returns a valid ID, whereas gtrace_unassign_trace_id() calls put_trace_id() on every component in path->comp_list that defines the callback. Any component in the path that returned -ENODATA or sat after the supplier will receive an unpaired put_trace_id() call, corrupting its trace ID accounting.
Quoted 18 lines
drivers/hwtracing/gtrace/gtrace-core.c +18
-0+ +static bool gtrace_path_ready (struct gtrace_path *path) +{ +struct gtrace_comp_priv *cpriv; +struct gtrace_path_node *node; + +list_for_each_entry (node, &path->comp_list, head) { +device_lock (&node->comp->dev); + cpriv =to_gtrace_comp_priv (node->comp); +if (!cpriv->ready) { +device_unlock (&node->comp->dev); +return false; + } +device_unlock (&node->comp->dev); + } + +return true; +}
[ ... ]
drivers/hwtracing/gtrace/gtrace-core.c void gtrace_unregister_component(struct gtrace_component *comp) · line 734
Quoted 6 lines
drivers/hwtracing/gtrace/gtrace-core.c +6
-0+if (!gtrace_path_ready (path)) { + ret = -EOPNOTSUPP ; + goto err_release_path_nodes; + } + + ret =gtrace_assign_trace_id (path);
[Severity: Medium]
What prevents a component driver from being unbound after gtrace_path_ready() drops device_lock(&node->comp->dev)?
CPU0 CPU1gtrace_create_path()
gtrace_path_ready()
device_lock(&comp->dev)
cpriv->ready == true
device_unlock(&comp->dev)
device_release_driver(&comp->dev)
gtrace_remove()
cpriv->ready = false
comp->dev.driver = NULL
gtrace_assign_trace_id()
gtdrv == NULL -> continueBecause build_path_walk_fn() only takes a struct device reference via gtrace_get_component() without pinning the bound driver, an unbind after gtrace_path_ready() causes gtrace_assign_trace_id() to see !gtdrv and silently skip the component, allowing gtrace_create_path() to succeed with an unbound component (or skip put_trace_id() if the driver is unbound before gtrace_destroy_path()).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v7 5/14 gtrace: Add functions to start/stop tracing on a component path
In reply to Mayuresh ChitaleThe perf driver framework needs to be able to start / stop all components in a trace component path during its operation. Add gtrace_path_start() and gtrace_path_stop() functions for this purpose.
--- drivers/hwtracing/gtrace/gtrace-core.c | 128 ++++++++++++++++++++++++- include/linux/gtrace.h | 7 ++ 2 files changed, 134 insertions(+), 1 deletion(-)
drivers/hwtracing/gtrace/gtrace-core.c +127 -1
@@ -10,6 +10,7 @@#include <linux/module.h> #include <linux/mutex.h> #include <linux/percpu.h> +#include <linux/spinlock.h> #include <linux/xarray.h> #include <linux/gtrace.h>
@@ -24,7 +25,16 @@ static DEFINE_PER_CPU(struct gtrace_component *, gtrace_cpu_source_comp);/* gtrace comp specific data, to be used by core functions only */ struct gtrace_comp_priv {struct gtrace_component comp; +/* +* Protects ready, start_count and owner. Serializes start/stop against other +* paths sharing this component and against driver removal. For a sink, also +* serializes against gtrace_path_copyto_auxbuf(). +* Raw because the PMU start/stop callbacks are called with the rq_lock held. +*/ +raw_spinlock_t lock;u32 type_idx; +u32 start_count; +pid_t owner;bool ready;bool visited; };
@@ -121,13 +131,17 @@ static int gtrace_probe(struct device *dev)const struct gtrace_driver *gtdrv =to_gtrace_driver (dev->driver);struct gtrace_component *comp =to_gtrace_component (dev);struct gtrace_comp_priv *cpriv =to_gtrace_comp_priv (comp); +unsigned long flags;int ret =0 ;if (gtdrv->probe) ret = gtdrv->probe (comp); -if (!ret) +if (!ret) { +raw_spin_lock_irqsave (&cpriv->lock, flags); cpriv->ready = true; +raw_spin_unlock_irqrestore (&cpriv->lock, flags); + }return ret; }
@@ -137,8 +151,11 @@ static void gtrace_remove(struct device *dev)const struct gtrace_driver *gtdrv =to_gtrace_driver (dev->driver);struct gtrace_component *comp =to_gtrace_component (dev);struct gtrace_comp_priv *cpriv =to_gtrace_comp_priv (comp); +unsigned long flags; +raw_spin_lock_irqsave (&cpriv->lock, flags); cpriv->ready = false; +raw_spin_unlock_irqrestore (&cpriv->lock, flags);if (gtdrv->remove) gtdrv->remove (comp); }
@@ -481,6 +498,7 @@ struct gtrace_component *gtrace_register_component(struct gtrace_component_id *i ret = -ENOMEM ; goto err_out; } +raw_spin_lock_init (&cpriv->lock); comp = &cpriv->comp; comp->pdata = pdata; comp->id = *id;
@@ -692,6 +710,114 @@ static void gtrace_release_path_nodes(struct gtrace_path *path) } } +static int __gtrace_comp_start (struct gtrace_component *comp,pid_t owner) +{ +struct gtrace_comp_priv *cpriv =to_gtrace_comp_priv (comp); +const struct gtrace_driver *gtdrv; +unsigned long flags; +int ret =0 ; + +raw_spin_lock_irqsave (&cpriv->lock, flags); +if (!cpriv->ready) { + ret = -ENODEV ; + goto out; + } + + gtdrv =to_gtrace_driver (comp->dev.driver); +if (!gtdrv) { + ret = -ENODEV ; + goto out; + } + +if (cpriv->start_count) { +if (cpriv->owner != owner) { + ret = -EBUSY ; + goto out; + } + }else { +if (gtdrv->start) { + ret = gtdrv->start (comp); +if (ret) + goto out; + } + cpriv->owner = owner; + } + + cpriv->start_count++; +out: +raw_spin_unlock_irqrestore (&cpriv->lock, flags); +return ret; +} + +static int __gtrace_comp_stop (struct gtrace_component *comp) +{ +struct gtrace_comp_priv *cpriv =to_gtrace_comp_priv (comp); +const struct gtrace_driver *gtdrv; +unsigned long flags; +int ret =0 ; + +raw_spin_lock_irqsave (&cpriv->lock, flags); +if (!cpriv->start_count) { + ret = -EINVAL ; + goto out; + } + + cpriv->start_count--; +if (!cpriv->start_count) { +if (!cpriv->ready) { + ret = -ENODEV ; + goto out; + } + + gtdrv =to_gtrace_driver (comp->dev.driver); +if (!gtdrv) { + ret = -ENODEV ; + goto out; + } + +if (gtdrv->stop) + ret = gtdrv->stop (comp); + } +out: +raw_spin_unlock_irqrestore (&cpriv->lock, flags); +return ret; +} + +int gtrace_path_start (struct gtrace_path *path) +{ +struct gtrace_path_node *node; +int ret =0 ; + +list_for_each_entry_reverse (node, &path->comp_list, head) { + ret =__gtrace_comp_start (node->comp, path->owner); +if (ret) +break ; + } + +/* If a component failed to start, stop all the components that were started before it */ +if (ret) +list_for_each_entry_continue (node, &path->comp_list, head) +__gtrace_comp_stop (node->comp); + +return ret; +} +EXPORT_SYMBOL_GPL (gtrace_path_start); + +int gtrace_path_stop (struct gtrace_path *path) +{ +struct gtrace_path_node *node; +int ret =0 , err; + +list_for_each_entry (node, &path->comp_list, head) { + err =__gtrace_comp_stop (node->comp); +if (err && !ret) + ret = err; + } + +return ret; +} +EXPORT_SYMBOL_GPL (gtrace_path_stop); +struct gtrace_path *gtrace_create_path (struct gtrace_component *source,struct gtrace_component *sink,enum gtrace_component_mode mode)
include/linux/gtrace.h +7 -0
@@ -295,6 +295,7 @@ struct gtrace_path {enum gtrace_component_mode mode;u32 trace_id;#define GTRACE_INVALID_TRACE_ID U32_MAX +pid_t owner; };struct gtrace_component *gtrace_path_source (struct gtrace_path *path);
@@ -303,11 +304,15 @@ struct gtrace_path *gtrace_create_path(struct gtrace_component *source,struct gtrace_component *sink, enum gtrace_component_mode mode);void gtrace_destroy_path (struct gtrace_path *path); +int gtrace_path_start (struct gtrace_path *path); +int gtrace_path_stop (struct gtrace_path *path);/** * struct gtrace_driver - Representation of a trace driver. * @id_table: Table to match components handled by the driver, terminated * by GTRACE_COMPONENT_TYPE_NONE. +* @start: Callback to start tracing. +* @stop: Callback to stop tracing. * @probe: Driver probe() function. * @remove: Driver remove() function. * @get_trace_id: Get/allocate a trace ID. Returns a nonnegative ID, -ENODATA
@@ -317,6 +322,8 @@ void gtrace_destroy_path(struct gtrace_path *path);*/ struct gtrace_driver {const struct gtrace_component_id *id_table; +int (*start)(struct gtrace_component *comp); +int (*stop)(struct gtrace_component *comp);int (*probe)(struct gtrace_component *comp);void (*remove)(struct gtrace_component *comp); int (*get_trace_id)(struct gtrace_component *comp,
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 05/14] gtrace: Add functions to start/stop tracing on a component path
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] gtrace: active component not stopped on driver removal in gtrace_remove() - [Low] gtrace: missing…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] gtrace: active component not stopped on driver removal in gtrace_remove()
- [Low] gtrace: missing @owner kernel-doc entry for struct gtrace_path --
commit 3c35cac2de4c3236ac92bfa75608dd492c2b0158
Author: Mayuresh Chitale <[email protected]>
gtrace: Add functions to start/stop tracing on a component path
This commit adds gtrace_path_start() and gtrace_path_stop() along with per-component start reference counting and driver start/stop callbacks to start and stop tracing across all components in a path.
Quoted 4 lines
drivers/hwtracing/gtrace/gtrace-core.c +0
-0
[ ... ]
drivers/hwtracing/gtrace/gtrace-core.c static void gtrace_remove(struct device *dev) · line 161
Quoted 12 lines
drivers/hwtracing/gtrace/gtrace-core.c +3
-0@@ -137,8 +151,11 @@ static void gtrace_remove(struct device *dev)const struct gtrace_driver *gtdrv =to_gtrace_driver (dev->driver);struct gtrace_component *comp =to_gtrace_component (dev);struct gtrace_comp_priv *cpriv =to_gtrace_comp_priv (comp); +unsigned long flags; +raw_spin_lock_irqsave (&cpriv->lock, flags); cpriv->ready = false; +raw_spin_unlock_irqrestore (&cpriv->lock, flags);if (gtdrv->remove) gtdrv->remove (comp); }
[Severity: Medium]
Should gtrace_remove() stop the component when cpriv->start_count is non-zero?
If a component driver is unbound while a trace path is active, gtrace_remove() clears cpriv->ready without calling gtdrv->stop(comp). When gtrace_path_stop() later calls __gtrace_comp_stop(), it decrements cpriv->start_count to 0, sees !cpriv->ready, and returns -ENODEV without calling gtdrv->stop(comp) either, leaving hardware tracing active.
Also, if the driver is re-probed before existing active paths are stopped, cpriv->ready becomes true again while cpriv->start_count is still positive from the previous binding, causing later start/stop calls to skip gtdrv->start(comp) or call gtdrv->stop(comp) on a newly probed component that was never started.
include/linux/gtrace.h struct gtrace_path { · line 299
Quoted 10 lines
include/linux/gtrace.h +1
-0@@ -295,6 +295,7 @@ struct gtrace_path {enum gtrace_component_mode mode;u32 trace_id;#define GTRACE_INVALID_TRACE_ID U32_MAX +pid_t owner; };
[Severity: Low]
struct gtrace_path has a kernel-doc comment block above it, so adding the owner member without an @owner description will trigger a kernel-doc warning under W=1 builds.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v7 6/14 gtrace: Add RISC-V Trace encoder driver
In reply to Mayuresh ChitaleAdd initial implementation of RISC-V E-Trace encoder driver. The encoder is defined in the RISC-V Trace Control Interface specification.
--- drivers/hwtracing/gtrace/Kconfig | 13 + drivers/hwtracing/gtrace/Makefile | 1 + drivers/hwtracing/gtrace/rvtrace-encoder.c | 274 +++++++++++++++++++++ 3 files changed, 288 insertions(+) create mode 100644 drivers/hwtracing/gtrace/rvtrace-encoder.c
drivers/hwtracing/gtrace/Kconfig +13 -0
@@ -28,3 +28,16 @@ config RVTRACE To compile this driver as a module, choose M here: the module will be called rvtrace. + +config RVTRACE_ENCODER + tristate "RISC-V Trace Encoder driver" + depends on RVTRACE + default y + help + This driver provides support for the RISC-V trace encoder component + which is defined by the RISC-V Trace Control Interface specification. + The encoder is bound to a CPU and generates the trace data for that + CPU. + + To compile this driver as a module, choose M here: the module + will be called rvtrace-encoder.
drivers/hwtracing/gtrace/Makefile +1 -0
@@ -5,3 +5,4 @@ gtrace-y := gtrace-core.o gtrace-$(CONFIG_OF) += gtrace-of.o obj-$(CONFIG_RVTRACE) += rvtrace-platform.o +obj-$(CONFIG_RVTRACE_ENCODER) += rvtrace-encoder.o
drivers/hwtracing/gtrace/rvtrace-encoder.c new file +274 -0
274 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 06/14] gtrace: Add RISC-V Trace encoder driver
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] gtrace: broken source ID bit-width probing in rvtrace_encoder_probe() - [Medium] gtrace: missing…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] gtrace: broken source ID bit-width probing in rvtrace_encoder_probe()
- [Medium] gtrace: missing trTeEmpty flush poll in rvtrace_encoder_stop()
--
commit 23f7809d52c4ad4dc2d6c2111a3f04ab70095757
Author: Mayuresh Chitale <[email protected]>
gtrace: Add RISC-V Trace encoder driver
This commit adds the initial driver for the RISC-V E-Trace encoder component defined by the RISC-V Trace Control Interface specification.
Quoted 5 lines
drivers/hwtracing/gtrace/rvtrace-encoder.c new file +0
-0
[ ... ]
Quoted 6 lines
drivers/hwtracing/gtrace/rvtrace-encoder.c new file +6
-0+static int rvtrace_encoder_probe (struct gtrace_component *comp) +{ +struct gtrace_platform_data *pdata = comp->pdata; +struct rvtrace_encoder_priv *priv; +u32 val, hw_srcbits, srcbits; +int ret;
[ ... ]
Quoted 16 lines
drivers/hwtracing/gtrace/rvtrace-encoder.c new file +16
-0+/* Check if trTeInhibitSrc is hardwired to 1 */ + val =gtrace_read32 (pdata,RVTRACE_COMPONENT_CTRL_OFFSET ); + val &= ~BIT (RVTRACE_COMPONENT_CTRL_INHIBITSRC_SHIFT ); +gtrace_write32 (pdata, val,RVTRACE_COMPONENT_CTRL_OFFSET ); + val =gtrace_read32 (pdata,RVTRACE_COMPONENT_CTRL_OFFSET ); +if (!(val &BIT (RVTRACE_COMPONENT_CTRL_INHIBITSRC_SHIFT ))) { +/* Probe how many bits can trTeSrcID span in the emitted Trace messages */ + val =gtrace_read32 (pdata,RVTRACE_ENCODER_INSTFEAT_OFFSET ); + hw_srcbits =RVTRACE_ENCODER_INSTFEAT_SRCBITS_MAX ; + do { + val &= ~RVTRACE_ENCODER_INSTFEAT_SRCBITS_MASK; + val |= FIELD_PREP(RVTRACE_ENCODER_INSTFEAT_SRCBITS_MASK, hw_srcbits); + gtrace_write32(pdata, val, RVTRACE_ENCODER_INSTFEAT_OFFSET); + val = gtrace_read32(pdata, RVTRACE_ENCODER_INSTFEAT_OFFSET); + if (FIELD_GET(RVTRACE_ENCODER_INSTFEAT_SRCBITS_MASK, val) == hw_srcbits) + break;
drivers/hwtracing/gtrace/rvtrace-encoder.c line 155
Quoted 10 lines
drivers/hwtracing/gtrace/rvtrace-encoder.c new file +10
-0+ }while (--hw_srcbits); +/* Determine if there are sufficient bits to hold max cpus */ + srcbits =min_t (u32, hw_srcbits,order_base_2 (nr_cpu_ids)); +if (srcbits && pdata->bound_cpu <BIT (srcbits)) { +rvtrace_encoder_set_srcid (pdata, srcbits); + val =gtrace_read32 (pdata,RVTRACE_ENCODER_INSTFEAT_OFFSET ); +if (FIELD_GET (RVTRACE_ENCODER_INSTFEAT_SRCBITS_MASK , val) == srcbits) + priv->srcbits = srcbits; + } + }
[Severity: High]
Does clamping srcbits with order_base_2(nr_cpu_ids) leave priv->srcbits unset on single-CPU systems or hardware with a fixed trTeSrcBits width?
When nr_cpu_ids is 1, order_base_2(1) evaluates to 0, so srcbits is 0 and no source ID is configured. In addition, trTeSrcBits in trTeInstFeatures is a WARL field often fixed to a synthesis-time width (e.g. 4 bits); when order_base_2(nr_cpu_ids) is smaller than that fixed width, reading back RVTRACE_ENCODER_INSTFEAT_SRCBITS_MASK returns hw_srcbits rather than srcbits, causing the equality check to fail and disabling source IDs even though hw_srcbits can hold pdata->bound_cpu.
[ ... ]
Quoted 11 lines
drivers/hwtracing/gtrace/rvtrace-encoder.c new file +11
-0+static int rvtrace_encoder_stop (struct gtrace_component *comp) +{ +struct gtrace_platform_data *pdata = comp->pdata; +int ret, err; +u32 val; + + val =gtrace_read32 (pdata,RVTRACE_COMPONENT_CTRL_OFFSET ); + val &= ~BIT (RVTRACE_COMPONENT_CTRL_ITRACE_SHIFT ); +gtrace_write32 (pdata, val,RVTRACE_COMPONENT_CTRL_OFFSET ); + ret = gtrace_poll_bit(pdata, RVTRACE_COMPONENT_CTRL_OFFSET, + RVTRACE_COMPONENT_CTRL_ITRACE_SHIFT, 0,
drivers/hwtracing/gtrace/rvtrace-encoder.c line 239
Quoted 10 lines
drivers/hwtracing/gtrace/rvtrace-encoder.c new file +10
-0+ pdata->control_poll_timeout_usecs); +if (ret) +dev_err (&comp->dev,"failed to stop tracing.\n" ); + + err =gtrace_disable_component (comp); +if (err) +dev_err (&comp->dev,"failed to disable encoder.\n" ); + +return ret ?: err; +}
[Severity: Medium]
Should rvtrace_encoder_stop() call rvtrace_comp_poll_empty(comp) before disabling the encoder?
Per the RISC-V Trace Control Interface specification, clearing trTeInstTracing causes the encoder to flush remaining queued trace packets and assert trTeEmpty when drained. Disabling the component before trTeEmpty asserts can discard buffered trace data at the end of a session.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v7 7/14 gtrace: Add function to copy into perf AUX buffer
In reply to Mayuresh ChitaleFrom: Anup Patel <[email protected]>
The RISC-V trace ramsink will need a mechanism to copy trace data into the perf AUX buffer. Add gtrace_path_copyto_auxbuf() function and corresponding trace driver callback copyto_auxbuf() for this purpose.
--- drivers/hwtracing/gtrace/gtrace-core.c | 42 ++++++++++++++++++++++++++ include/linux/gtrace.h | 31 +++++++++++++++++++ 2 files changed, 73 insertions(+)
drivers/hwtracing/gtrace/gtrace-core.c +42 -0
@@ -818,6 +818,48 @@ int gtrace_path_stop(struct gtrace_path *path) }EXPORT_SYMBOL_GPL (gtrace_path_stop); +int gtrace_path_copyto_auxbuf (struct gtrace_path *path, +struct gtrace_perf_auxbuf *buf, +size_t *size,u64 *aux_flags) +{ +struct gtrace_component *comp =gtrace_path_sink (path); +struct gtrace_comp_priv *sink =to_gtrace_comp_priv (comp); +const struct gtrace_driver *gtdrv; +int ret = -EOPNOTSUPP ; +unsigned long flags; + +/* +* Copy only after every path using the sink has stopped; otherwise the +* hardware may overwrite data being copied, or the same data may be copied +* to aux buffer twice. The last path to stop does the copy. Holding the +* sink's lock also stops another path from starting the sink before copy +* completes. +*/ +raw_spin_lock_irqsave (&sink->lock, flags); +if (sink->start_count) { + *size =0 ; + ret =0 ; + goto out; + } + +if (!sink->ready) { + ret = -ENODEV ; + goto out; + } + + gtdrv =to_gtrace_driver (comp->dev.driver); +if (gtdrv && gtdrv->copyto_auxbuf) { + *size = gtdrv->copyto_auxbuf (comp, buf, *size, aux_flags); + ret =0 ; + } +out: +if (ret) + *size =0 ; +raw_spin_unlock_irqrestore (&sink->lock, flags); +return ret; +} +EXPORT_SYMBOL_GPL (gtrace_path_copyto_auxbuf); +struct gtrace_path *gtrace_create_path (struct gtrace_component *source,struct gtrace_component *sink,enum gtrace_component_mode mode)
include/linux/gtrace.h +31 -0
@@ -307,10 +307,37 @@ void gtrace_destroy_path(struct gtrace_path *path);int gtrace_path_start (struct gtrace_path *path);int gtrace_path_stop (struct gtrace_path *path); +/** +* struct gtrace_perf_auxbuf - Representation of the perf AUX buffer. +* @length: Size of the AUX buffer. +* @nr_pages: Number of pages of the AUX buffer. +* @base: Start address of AUX buffer. +* @pos: Position in the AUX buffer to commit traced data. +*/ +struct gtrace_perf_auxbuf { +size_t length; +int nr_pages; +void *base; +long pos; +}; + +/* +* size is both, an input and an output parameter. As an input it conveys the max_bytes +* that can be copied by ramsink and as output it returns the actual count copied by ramsink. +*/ +int gtrace_path_copyto_auxbuf (struct gtrace_path *path, +struct gtrace_perf_auxbuf *buf, +size_t *size,u64 *aux_flags); +/** * struct gtrace_driver - Representation of a trace driver. * @id_table: Table to match components handled by the driver, terminated * by GTRACE_COMPONENT_TYPE_NONE. +* @copyto_auxbuf: Callback to copy data into perf AUX buffer. The size parameter +* is the maximum writable byte count, at most buf->length. +* Returns the actual copied byte count. +* Reports AUX flags via @aux_flags, including +* PERF_AUX_FLAG_TRUNCATED when data is discarded to fit. * @start: Callback to start tracing. * @stop: Callback to stop tracing. * @probe: Driver probe() function.
@@ -322,6 +349,10 @@ int gtrace_path_stop(struct gtrace_path *path);*/ struct gtrace_driver {const struct gtrace_component_id *id_table; +size_t (*copyto_auxbuf)(struct gtrace_component *comp, +struct gtrace_perf_auxbuf *buf, +size_t size, +u64 *aux_flags);int (*start)(struct gtrace_component *comp);int (*stop)(struct gtrace_component *comp);int (*probe)(struct gtrace_component *comp);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 07/14] gtrace: Add function to copy into perf AUX buffer
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] gtrace: TOCTOU race between gtrace_path_stop() and gtrace_path_copyto_auxbuf() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] gtrace: TOCTOU race between gtrace_path_stop() and gtrace_path_copyto_auxbuf()
--
commit 316c0ae2e4a3dbc8091e2e7c9a0a691103a47556
Author: Anup Patel <[email protected]>
gtrace: Add function to copy into perf AUX buffer
This commit adds gtrace_path_copyto_auxbuf() and the corresponding copyto_auxbuf() driver callback to copy trace data from a sink into a perf AUX buffer.
Quoted 32 lines
drivers/hwtracing/gtrace/gtrace-core.c +24
-0@@ -818,6 +818,48 @@ int gtrace_path_stop(struct gtrace_path *path) }EXPORT_SYMBOL_GPL (gtrace_path_stop); +int gtrace_path_copyto_auxbuf (struct gtrace_path *path, +struct gtrace_perf_auxbuf *buf, +size_t *size,u64 *aux_flags) +{ +struct gtrace_component *comp =gtrace_path_sink (path); +struct gtrace_comp_priv *sink =to_gtrace_comp_priv (comp); +const struct gtrace_driver *gtdrv; +int ret = -EOPNOTSUPP ; +unsigned long flags; + +/* +* Copy only after every path using the sink has stopped; otherwise the +* hardware may overwrite data being copied, or the same data may be copied +* to aux buffer twice. The last path to stop does the copy. Holding the +* sink's lock also stops another path from starting the sink before copy +* completes. +*/ +raw_spin_lock_irqsave (&sink->lock, flags); +if (sink->start_count) { + *size =0 ; + ret =0 ; + goto out; + } +
drivers/hwtracing/gtrace/gtrace-core.c int gtrace_path_stop(struct gtrace_path *path) · line 854
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-core.c +10
-0+if (!sink->ready) { + ret = -ENODEV ; + goto out; + } + + gtdrv =to_gtrace_driver (comp->dev.driver); +if (gtdrv && gtdrv->copyto_auxbuf) { + *size = gtdrv->copyto_auxbuf (comp, buf, *size, aux_flags); + ret =0 ; + }
[Severity: Medium]
Can two paths sharing a sink both copy the same trace data, or miss the copy entirely, because sink->lock is dropped between gtrace_path_stop() and gtrace_path_copyto_auxbuf()?
__gtrace_comp_stop() decrements sink->start_count and releases sink->lock before the caller reaches gtrace_path_copyto_auxbuf():
CPU0 CPU1gtrace_path_stop()
__gtrace_comp_stop(sink)
lock(&sink->lock)
sink->start_count-- (2 -> 1)
unlock(&sink->lock)
gtrace_path_stop()
__gtrace_comp_stop(sink)
lock(&sink->lock)
sink->start_count-- (1 -> 0)
gtdrv->stop(comp)
unlock(&sink->lock)gtrace_path_copyto_auxbuf()
lock(&sink->lock)
sink->start_count == 0
gtdrv->copyto_auxbuf(...)
unlock(&sink->lock)
gtrace_path_copyto_auxbuf()
lock(&sink->lock)
sink->start_count == 0
gtdrv->copyto_auxbuf(...)
<-- duplicate copyConversely, if another path sharing the sink calls gtrace_path_start() in that window after the last active path drops sink->lock in __gtrace_comp_stop(), sink->start_count becomes 1 and the sink restarts before gtrace_path_copyto_auxbuf() runs, so the stopped path sees sink->start_count != 0 and skips the copy.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v7 8/14 perf: Add gtrace AUX buffer trace format type
In reply to Mayuresh ChitaleThe gtrace PMU can host trace components of different architectures and trace protocols, so the recorded AUX data needs to carry its format as well. Define PERF_AUX_FLAG_GTRACE_FORMAT_ETRACE for the RISC-V E-trace format which the perf tool can use to decode the payload.
--- include/uapi/linux/perf_event.h | 5 +++++ tools/include/uapi/linux/perf_event.h | 5 +++++ 2 files changed, 10 insertions(+)
include/uapi/linux/perf_event.h +5 -0
@@ -1352,6 +1352,11 @@ enum perf_callchain_context {#define PERF_AUX_FLAG_CORESIGHT_FORMAT_CORESIGHT 0x0000/* Default for backward compatibility */ #define PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW 0x0100/* Raw format of the source */ +/* gtrace PMU AUX buffer formats */ +#define PERF_AUX_FLAG_GTRACE_FORMAT_UNKNOWN 0x0000 +#define PERF_AUX_FLAG_GTRACE_FORMAT_ETRACE 0x0100/* RISC-V E-trace format */ +#define PERF_AUX_FLAG_GTRACE_FORMAT_NTRACE 0x0200/* RISC-V N-Trace format */ +#define PERF_FLAG_FD_NO_GROUP (1UL << 0)#define PERF_FLAG_FD_OUTPUT (1UL << 1)#define PERF_FLAG_PID_CGROUP (1UL << 2)/* pid=cgroup ID, per-CPU mode only */
tools/include/uapi/linux/perf_event.h +5 -0
@@ -1352,6 +1352,11 @@ enum perf_callchain_context {#define PERF_AUX_FLAG_CORESIGHT_FORMAT_CORESIGHT 0x0000/* Default for backward compatibility */ #define PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW 0x0100/* Raw format of the source */ +/* gtrace PMU AUX buffer formats */ +#define PERF_AUX_FLAG_GTRACE_FORMAT_UNKNOWN 0x0000 +#define PERF_AUX_FLAG_GTRACE_FORMAT_ETRACE 0x0100/* RISC-V E-trace format */ +#define PERF_AUX_FLAG_GTRACE_FORMAT_NTRACE 0x0200/* RISC-V N-Trace format */ +#define PERF_FLAG_FD_NO_GROUP (1UL << 0)#define PERF_FLAG_FD_OUTPUT (1UL << 1)#define PERF_FLAG_PID_CGROUP (1UL << 2)/* pid=cgroup ID, per-CPU mode only */
Signature · 1 line
2.43.0
PATCH v7 9/14 gtrace: Add RISC-V Trace ramsink driver
In reply to Mayuresh ChitaleAdd initial implementation of RISC-V E-Trace ramsink driver. The ramsink is defined in the RISC-V Trace Control Interface specification.
--- drivers/hwtracing/gtrace/Kconfig | 15 + drivers/hwtracing/gtrace/Makefile | 1 + drivers/hwtracing/gtrace/rvtrace-ramsink.c | 366 +++++++++++++++++++++ 3 files changed, 382 insertions(+) create mode 100644 drivers/hwtracing/gtrace/rvtrace-ramsink.c
drivers/hwtracing/gtrace/Kconfig +15 -0
@@ -41,3 +41,18 @@ config RVTRACE_ENCODER To compile this driver as a module, choose M here: the module will be called rvtrace-encoder. + +config RVTRACE_RAMSINK + tristate "RISC-V Trace Ramsink driver" + depends on RVTRACE + select DMA_SHARED_BUFFER + default y + help + This driver provides support for the RISC-V trace ramsink component + which is defined by the RISC-V Trace Control Interface specification. + The ramsink is a trace sink which stores the trace data generated by + the upstream components into a system memory buffer. The buffer is + then copied to the perf AUX buffer when tracing using the perf tool. + + To compile this driver as a module, choose M here: the module + will be called rvtrace-ramsink.
drivers/hwtracing/gtrace/Makefile +1 -0
@@ -6,3 +6,4 @@ gtrace-$(CONFIG_OF) += gtrace-of.o obj-$(CONFIG_RVTRACE) += rvtrace-platform.o obj-$(CONFIG_RVTRACE_ENCODER) += rvtrace-encoder.o +obj-$(CONFIG_RVTRACE_RAMSINK) += rvtrace-ramsink.o
drivers/hwtracing/gtrace/rvtrace-ramsink.c new file +366 -0
366 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 09/14] gtrace: Add RISC-V Trace ramsink driver
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] gtrace: stale trace data copied on wrap in rvtrace_ramsink_copyto_auxbuf() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] gtrace: stale trace data copied on wrap in rvtrace_ramsink_copyto_auxbuf()
--
commit ed8acf419ff33b6e8c0e35b30a76e41399f3be04
Author: Mayuresh Chitale <[email protected]>
gtrace: Add RISC-V Trace ramsink driver
This commit adds the RISC-V E-Trace ramsink driver for the gtrace framework, storing trace data into a system memory DMA buffer and copying it to the perf AUX buffer.
Quoted 5 lines
drivers/hwtracing/gtrace/rvtrace-ramsink.c new file +0
-0
[ ... ]
Quoted 32 lines
drivers/hwtracing/gtrace/rvtrace-ramsink.c new file +32
-0+static size_t rvtrace_ramsink_copyto_auxbuf (struct gtrace_component *comp, +struct gtrace_perf_auxbuf *buf, +size_t size, +u64 *aux_flags) +{ +struct rvtrace_ramsink_priv *priv =dev_get_drvdata (&comp->dev); +struct trace_buf src, dst; +size_t bytes =0 ; +bool wrap; +u64 wp; + + dst.base = buf->base; + dst.len = buf->length; + dst.cur = buf->pos; + src.base = priv->va; + src.len = priv->size; + *aux_flags =0 ; + wp =gtrace_read_pair (comp->pdata,RVTRACE_RAMSINK_WPLOW_OFF , +RVTRACE_RAMSINK_WPHIGH_OFF ); + wrap = wp &RVTRACE_RAMSINK_WPLOW_WRAP ; + wp &= ~RVTRACE_RAMSINK_WPLOW_WRAP ; + +if (wp < priv->start || wp >= priv->end || + (!wrap && wp < priv->prev_wp)) +return 0 ; + +if (wrap) { +gtrace_write_pair (comp->pdata, priv->start,RVTRACE_RAMSINK_WPLOW_OFF , +RVTRACE_RAMSINK_WPHIGH_OFF ); + src.cur = wp - priv->start; + priv->prev_wp = priv->start; +/*
drivers/hwtracing/gtrace/rvtrace-ramsink.c line 170
Quoted 10 lines
drivers/hwtracing/gtrace/rvtrace-ramsink.c new file +10
-0+* There is no way to tell if trRamWp wrapped around more than once. As a +* result priv->prev_wp can't be used and the entire buffer must be copied +* even though some data might be duplicated. +*/ + bytes = priv->size; + }else { + src.cur = priv->prev_wp - priv->start; + bytes = wp - priv->prev_wp; + priv->prev_wp = wp; + }
[Severity: High]
As reported by Leo Yu-Chi Liang on v6 [1], can the wrap path copy stale trace data from earlier captures into the perf AUX buffer?
When a capture finishes without wrapping, the non-wrap branch advances priv->prev_wp to wp without resetting trRamWP to priv->start, and rvtrace_ramsink_start() does not reset trRamWP or priv->prev_wp either. The next capture therefore resumes writing at priv->prev_wp. If it crosses priv->end and stops at a wrapped wp < priv->prev_wp, the region [wp, priv->prev_wp) was not written during that capture and still holds data from previous captures or an earlier perf session, yet the wrap branch copies the full priv->size bytes starting at wp.
Resetting trRamWP and priv->prev_wp to priv->start in rvtrace_ramsink_start() (or after each copy in rvtrace_ramsink_copyto_auxbuf()) would ensure each capture starts at the beginning of the buffer.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH v7 10/14 riscv: Enable DMA_RESTRICTED_POOL in defconfig
In reply to Mayuresh ChitaleFrom: Anup Patel <[email protected]>
The RISC-V ramsink trace component may have implementation specific restrictions such that the component can only write trace data in particular parts of DRAM.
Enable DMA_RESTRICTED_POOL in the defconfig so that dma_alloc_*() and dma_free_*() APIs work for devices with DMA address restrictions.
--- arch/riscv/configs/defconfig | 1 + 1 file changed, 1 insertion(+)
arch/riscv/configs/defconfig +1 -0
@@ -308,6 +308,7 @@ CONFIG_SECURITY_APPARMOR=y CONFIG_DEFAULT_SECURITY_DAC=y CONFIG_CRYPTO_USER_API_HASH=y CONFIG_CRYPTO_DEV_VIRTIO=y +CONFIG_DMA_RESTRICTED_POOL=y CONFIG_PRINTK_TIME=y CONFIG_DEBUG_KERNEL=y CONFIG_DEBUG_FS=y
Signature · 1 line
2.43.0
PATCH v7 11/14 gtrace: Add perf driver for tracing using perf tool
In reply to Mayuresh ChitaleAdd perf driver for the gtrace similar to ARM Coresight and Hisilicon PTT drivers. The driver adds 'gtrace' event descriptor which can be used by the perf tool to record the trace data. The actual data format depends on type of encoder device and optionally the PMU specific trace format type.
--- drivers/hwtracing/gtrace/Kconfig | 3 +- drivers/hwtracing/gtrace/Makefile | 2 +- drivers/hwtracing/gtrace/gtrace-core.c | 15 +- drivers/hwtracing/gtrace/gtrace-perf.c | 368 +++++++++++++++++++++++++ include/linux/gtrace.h | 3 + 5 files changed, 388 insertions(+), 3 deletions(-) create mode 100644 drivers/hwtracing/gtrace/gtrace-perf.c
drivers/hwtracing/gtrace/Kconfig +2 -1
@@ -2,7 +2,8 @@ menuconfig GTRACE tristate "Generic Hardware Trace Support" - depends on OF + depends on OF && HAVE_PERF_EVENTS + select PERF_EVENTS help This framework provides an architecture-neutral kernel interface for hardware trace drivers. It builds a topological view of the
drivers/hwtracing/gtrace/Makefile +1 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_GTRACE) += gtrace.o -gtrace-y := gtrace-core.o +gtrace-y := gtrace-core.o gtrace-perf.o gtrace-$(CONFIG_OF) += gtrace-of.o obj-$(CONFIG_RVTRACE) += rvtrace-platform.o
drivers/hwtracing/gtrace/gtrace-core.c +14 -1
@@ -932,13 +932,26 @@ EXPORT_SYMBOL_GPL(__gtrace_register_driver);static int __init gtrace_init (void ) { +int ret; +gtrace_init_type_idx (); -return bus_register (>race_bustype); + ret =bus_register (>race_bustype); +if (ret) +return ret; + + ret =gtrace_perf_init (); +if (ret) { +bus_unregister (>race_bustype); +return ret; + } + +return 0 ; } static void __exit gtrace_exit(void) { +gtrace_perf_exit ();bus_unregister (>race_bustype); }
drivers/hwtracing/gtrace/gtrace-perf.c new file +368 -0
368 lines not on this page · raw message
include/linux/gtrace.h +3 -0
@@ -376,4 +376,7 @@ static inline void gtrace_unregister_driver(struct gtrace_driver *gtdrv) driver_unregister(>drv->driver); } +int gtrace_perf_init (void ); +void gtrace_perf_exit (void ); +#endif /* __LINUX_GTRACE_H__ */
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 11/14] gtrace: Add perf driver for tracing using perf tool
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [Critical] gtrace: NULL pointer dereference of event->owner in gtrace_setup_aux() - [High] gtrace…
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [Critical] gtrace: NULL pointer dereference of event->owner in gtrace_setup_aux()
- [High] gtrace: gtrace_event_init() returns -EINVAL instead of -ENOENT on type mismatch
- [High] gtrace: unclosed AUX handle and leaked refcount in gtrace_event_stop()
- [High] gtrace: missing PERF_PMU_CAP_NO_EXCLUDE allows unprivileged kernel tracing
- [Medium] gtrace: missing perf_event_itrace_started() call in gtrace_event_start()
- [Medium] gtrace: race between gtrace_path_stop() and gtrace_path_copyto_auxbuf()
--
commit 3ba5b474daf5f255e16143a11ae06c6b2b9c9dd7
Author: Mayuresh Chitale <[email protected]>
gtrace: Add perf driver for tracing using perf tool
This commit adds a perf PMU driver for the gtrace hardware tracing framework to record trace data into perf AUX buffers.
Quoted 6 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +0
-0@@ -0,0 +1,368 @@
[ ... ]
Quoted 24 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +24
-0+static void *gtrace_setup_aux (struct perf_event *event,void **pages, +int nr_pages,bool overwrite) +{ +struct gtrace_event_data *event_data =NULL ; +struct page **pagelist; +int cpu = event->cpu, i; +cpumask_t *mask; + + event_data =alloc_event_data (cpu); +if (!event_data) +return NULL ; + + mask = &event_data->mask; +/* +* Create the path for each CPU in the mask. In case of any failure skip the CPU +*/ +for_each_cpu (cpu, mask) { +struct gtrace_component *src; +struct gtrace_path *path; + + src =gtrace_cpu_source (cpu); +if (!src) { +cpumask_clear_cpu (cpu, mask); +continue ;
drivers/hwtracing/gtrace/gtrace-perf.c line 133
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +10
-0+ } + + path =gtrace_create_path (src,NULL ,GTRACE_COMPONENT_MODE_PERF ); +gtrace_put_component (src); +if (IS_ERR (path)) { +cpumask_clear_cpu (cpu, mask); +continue ; + } + + path->owner =task_pid_nr (event->owner);
[Severity: Critical]
Can task_pid_nr(event->owner) dereference a NULL or freed task_struct here?
If the thread that called perf_event_open() exits while another thread or child process still holds the event file descriptor and calls mmap() to set up the AUX buffer, perf_event_exit_task() clears event->owner to NULL before the exiting task_struct is freed via RCU. task_pid_nr() unconditionally dereferences tsk->pid, and gtrace_setup_aux() reads event->owner across sleeping calls without RCU protection or a NULL check.
[ ... ]
drivers/hwtracing/gtrace/gtrace-perf.c line 188
Quoted 7 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +7
-0+static int gtrace_event_init (struct perf_event *event) +{ +if (event->attr.type != gtrace_pmu.type) +return -EINVAL ; + +return 0 ; +}
[Severity: High]
Should gtrace_event_init() return -ENOENT instead of -EINVAL when event->attr.type does not match gtrace_pmu.type?
When perf_init_event() walks the global pmus list to resolve an event, it stops iterating as soon as any PMU callback returns an error other than -ENOENT, so returning -EINVAL here causes unrelated perf_event_open() calls to fail prematurely.
Quoted 35 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +35
-0+ +static void gtrace_event_start (struct perf_event *event,int flags) +{ +struct gtrace_ctxt *ctxt =this_cpu_ptr (>race_ctxt); +struct perf_output_handle *handle = &ctxt->handle; +struct gtrace_event_data *event_data; +int cpu =smp_processor_id (); +struct gtrace_path *path; + +if (WARN_ON (ctxt->event_data)) + goto fail; + +/* +* Deal with the ring buffer API and get a handle on the +* session's information. +*/ + event_data =perf_aux_output_begin (handle, event); +if (!event_data) + goto fail; + +if (!cpumask_test_cpu (cpu, &event_data->mask)) + goto out; + + event_data->buf.pos = handle->head % event_data->buf.length; + path = *gtrace_event_cpu_path_ptr (event_data, cpu); +if (!path) { +dev_err_ratelimited (gtrace_pmu.dev,"Error. Path not found on cpu %d\n" , cpu); + goto fail_end; + } + +if (gtrace_path_start (path)) { +dev_err_ratelimited (gtrace_pmu.dev,"Error. Tracing not started on cpu %d\n" , cpu); + goto fail_end; + } +
drivers/hwtracing/gtrace/gtrace-perf.c line 233
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +10
-0+/* Report the hardware source ID once per CPU for this AUX buffer. */ +if (path->trace_id !=GTRACE_INVALID_TRACE_ID && + !cpumask_test_cpu (cpu, &event_data->aux_hwid_done)) { +cpumask_set_cpu (cpu, &event_data->aux_hwid_done); +perf_report_aux_output_id (event, path->trace_id); + } + +out: +/* Tell the perf core the event is alive */ + event->hw.state =0 ;
[Severity: Medium]
Should gtrace_event_start() call perf_event_itrace_started(event)?
Because gtrace_pmu sets PERF_PMU_CAP_ITRACE, perf_log_itrace_start() checks PERF_ATTACH_ITRACE on every event_sched_in(). Without perf_event_itrace_started() setting PERF_ATTACH_ITRACE, perf emits a PERF_RECORD_ITRACE_START record into the main ring buffer on every context switch instead of only once when tracing starts.
Quoted 41 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +41
-0+ ctxt->event_data = event_data; +return ; +fail_end: +perf_aux_output_end (handle,0 ); +fail: + event->hw.state =PERF_HES_STOPPED ; +} + +static void gtrace_event_stop (struct perf_event *event,int mode) +{ +struct gtrace_ctxt *ctxt =this_cpu_ptr (>race_ctxt); +struct perf_output_handle *handle = &ctxt->handle; +u64 aux_flags =PERF_AUX_FLAG_GTRACE_FORMAT_UNKNOWN ; +struct gtrace_event_data *event_data; +int ret, cpu =smp_processor_id (); +struct gtrace_path *path; +size_t size =0 ; + +if (event->hw.state ==PERF_HES_STOPPED ) +return ; + +if (handle->event &&WARN_ON (perf_get_aux (handle) != ctxt->event_data)) +return ; + + event_data = ctxt->event_data; + ctxt->event_data =NULL ; + +if (WARN_ON (!event_data)) +return ; + +if (!cpumask_test_cpu (cpu, &event_data->mask)) + goto out_end; + +/* stop tracing */ + path = *gtrace_event_cpu_path_ptr (event_data, cpu); +if (!path) { +dev_err_ratelimited (gtrace_pmu.dev,"Error. Path not found on cpu %d\n" , cpu); + goto out_end; + } + +if (gtrace_path_stop (path)) {
drivers/hwtracing/gtrace/gtrace-perf.c line 284
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +10
-0+dev_err_ratelimited (gtrace_pmu.dev,"Error. Tracing not stopped on cpu %d\n" , cpu); + goto out_end; + } + + event->hw.state =PERF_HES_STOPPED ; +if (handle->event && (mode &PERF_EF_UPDATE )) { +if (WARN_ON_ONCE (handle->event != event)) +return ; + size = event_data->overwrite ? event_data->buf.length : handle->size; + ret =gtrace_path_copyto_auxbuf (path, &event_data->buf, &size, &aux_flags);
[Severity: Medium]
Can another CPU start or stop a path sharing the same sink in the window between gtrace_path_stop() and gtrace_path_copyto_auxbuf()?
CPU0 CPU1gtrace_event_stop()
gtrace_path_stop(path)
__gtrace_comp_stop(sink)
raw_spin_lock(&sink->lock)
sink->start_count-- (becomes 0)
gtdrv->stop(sink)
raw_spin_unlock(&sink->lock)
gtrace_event_start()
gtrace_path_start(path)
__gtrace_comp_start(sink)
raw_spin_lock(&sink->lock)
sink->start_count++ (becomes 1)
gtdrv->start(sink)
raw_spin_unlock(&sink->lock)
gtrace_path_copyto_auxbuf()
raw_spin_lock(&sink->lock)
if (sink->start_count)
*size = 0; <-- trace data droppedSimilarly, if two CPUs sharing a sink stop concurrently, both can finish gtrace_path_stop() and decrement sink->start_count to 0 before either enters gtrace_path_copyto_auxbuf(), causing both CPUs to see sink->start_count == 0 and copy from the sink.
Quoted 13 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +13
-0+WARN_ON_ONCE (ret); + aux_flags &= ~PERF_AUX_FLAG_PMU_FORMAT_TYPE_MASK ; + aux_flags |=gtrace_path_aux_format (path); +if (READ_ONCE (handle->event)) { +/* Snapshot overwrites are expected; TRUNCATED would disable the event. */ +if (event_data->overwrite) { + aux_flags &= ~PERF_AUX_FLAG_TRUNCATED ; + handle->head += size; + } +/* Tag the AUX data with the source's trace format. */ +perf_aux_output_flag (handle, aux_flags); +perf_aux_output_end (handle, size); + }else
drivers/hwtracing/gtrace/gtrace-perf.c line 307
Quoted 10 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +10
-0+WARN_ON (size); + } + +return ; + +out_end: + event->hw.state =PERF_HES_STOPPED ; +if (handle->event && (mode &PERF_EF_UPDATE )) +perf_aux_output_end (handle,0 ); +}
[Severity: High]
Does gtrace_event_stop() leave the AUX output handle open and leak rb->aux_refcount when called without PERF_EF_UPDATE?
When gtrace_event_stop() is called with mode == 0 (for example, when a grouped software sampling event throttles via perf_event_throttle_group()), it clears ctxt->event_data and sets event->hw.state = PERF_HES_STOPPED without calling perf_aux_output_end(). When gtrace_event_del() later calls gtrace_event_stop(event, PERF_EF_UPDATE), it returns immediately because event->hw.state is already PERF_HES_STOPPED, leaving handle->event active and leaking the AUX buffer and event_data.
[ ... ]
drivers/hwtracing/gtrace/gtrace-perf.c line 349
Quoted 3 lines
drivers/hwtracing/gtrace/gtrace-perf.c new file +3
-0+int __init gtrace_perf_init (void ) +{ + gtrace_pmu.capabilities = (PERF_PMU_CAP_EXCLUSIVE |PERF_PMU_CAP_ITRACE );
[Severity: High]
Should gtrace_pmu.capabilities include PERF_PMU_CAP_NO_EXCLUDE (or gtrace_event_init() enforce perf_allow_kernel())?
Neither gtrace nor the encoder interface implements privilege-level trace filtering. Without PERF_PMU_CAP_NO_EXCLUDE, an unprivileged user can pass attr.exclude_kernel = 1 to perf_event_open() to bypass the perf_allow_kernel() check in sys_perf_event_open() while the hardware still records kernel execution into the user's AUX buffer.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
PATCH v7 12/14 perf tools: Add RISC-V trace PMU record capabilities
In reply to Mayuresh ChitaleIntroduce the required auxiliary API functions allowing the perf core to interact with gtrace perf driver. On RISC-V systems the gtrace PMU uses the RISC-V E-Trace components and optionally an ATB Bridge component for connecting to ARM-side sinks, if it is present on the SoC.
--- tools/perf/arch/riscv/util/Build | 1 + tools/perf/arch/riscv/util/auxtrace.c | 220 ++++++++++++++++++++++++++ tools/perf/util/auxtrace.c | 1 + tools/perf/util/auxtrace.h | 1 + tools/perf/util/gtrace.h | 16 ++ 5 files changed, 239 insertions(+) create mode 100644 tools/perf/arch/riscv/util/auxtrace.c create mode 100644 tools/perf/util/gtrace.h
tools/perf/arch/riscv/util/Build +1 -0
@@ -1 +1,2 @@ perf-util-y += header.o +perf-util-y += auxtrace.o
tools/perf/arch/riscv/util/auxtrace.c new file +220 -0
220 lines not on this page · raw message
tools/perf/util/auxtrace.c +1 -0
@@ -1432,6 +1432,7 @@ int perf_event__process_auxtrace_info(const struct perf_tool *tool __maybe_unusecase PERF_AUXTRACE_VPA_DTL : err =powerpc_vpadtl_process_auxtrace_info (event, session);break ; +case PERF_AUXTRACE_GTRACE :case PERF_AUXTRACE_UNKNOWN :default :return -EINVAL ;
tools/perf/util/auxtrace.h +1 -0
@@ -46,6 +46,7 @@ enum auxtrace_type {PERF_AUXTRACE_S390_CPUMSF ,PERF_AUXTRACE_HISI_PTT ,PERF_AUXTRACE_VPA_DTL , + PERF_AUXTRACE_GTRACE, }; enum itrace_period_type {
tools/perf/util/gtrace.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2026 Qualcomm Technologies, Inc. +*/ + +#ifndef INCLUDE__UTIL_PERF_GTRACE_H__ +#define INCLUDE__UTIL_PERF_GTRACE_H__ + +#include <linux/types.h> +#include "util/event.h" + +struct perf_session ; + +#define GTRACE_AUXTRACE_PRIV_SIZE sizeof(u64) + +#endif
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 12/14] perf tools: Add RISC-V trace PMU record capabilities
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] perf riscv: gtrace PMU never sets pmu->auxtrace or pmu->selectable - [Medium] perf riscv…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] perf riscv: gtrace PMU never sets pmu->auxtrace or pmu->selectable
- [Medium] perf riscv: rvtrace_recording_options() omits PERF_SAMPLE_CPU on gtrace evsel
--
commit 0c55086ce4e6f736821fda4e6c6b8b310252b04f
Author: Mayuresh Chitale <[email protected]>
perf tools: Add RISC-V trace PMU record capabilities
This commit adds auxtrace recording callbacks and event definitions for the RISC-V gtrace PMU in perf tools.
Quoted 6 lines
tools/perf/arch/riscv/util/auxtrace.c new file +0
-0@@ -0,0 +1,220 @@
[ ... ]
Quoted 23 lines
tools/perf/arch/riscv/util/auxtrace.c new file +23
-0+static int rvtrace_recording_options (struct auxtrace_record *itr,struct evlist *evlist, +struct record_opts *opts) +{ + struct rvtrace_recording *ptr = container_of(itr, struct rvtrace_recording, itr); +struct perf_pmu *rvtrace_pmu = ptr->rvtrace_pmu; +struct evsel *evsel, *rvtrace_evsel =NULL ; +struct evsel *tracking_evsel; +int err; + + ptr->evlist = evlist; +evlist__for_each_entry (evlist, evsel) { +if (evsel->core.attr.type == rvtrace_pmu->type) { +if (rvtrace_evsel) { +pr_err ("There may be only one " GTRACE_PMU_NAME " event\n" ); +return -EINVAL ; + } + evsel->core.attr.freq =0 ; + evsel->core.attr.sample_period =1 ; + evsel->needs_auxtrace_mmap = true; + rvtrace_evsel = evsel; + opts->full_auxtrace = true; + } + }
tools/perf/arch/riscv/util/auxtrace.c line 127
Quoted 10 lines
tools/perf/arch/riscv/util/auxtrace.c new file +10
-0+ + err =rvtrace_set_auxtrace_mmap_page (opts); +if (err) +return err; +/* +* To obtain the auxtrace buffer file descriptor, the auxtrace event +* must come first. +*/ +evlist__to_front (evlist, rvtrace_evsel); +evsel__set_sample_bit (rvtrace_evsel,TIME );
[Severity: Medium]
Should rvtrace_recording_options() also set PERF_SAMPLE_CPU on rvtrace_evsel?
The gtrace kernel driver calls perf_report_aux_output_id(event, path->trace_id) per CPU in gtrace_event_start(), which relies on PERF_SAMPLE_CPU in the event's sample_id to associate each hardware trace_id with its CPU. Without evsel__set_sample_bit(rvtrace_evsel, CPU), per-thread recording (perf record -e gtrace// <workload> without -a, -C or --sample-cpu) leaves PERF_SAMPLE_CPU unset, so PERF_RECORD_AUX_OUTPUT_HW_ID and PERF_RECORD_AUX records have sample.cpu == -1 and cannot be matched to the CPU that produced the trace.
[ ... ]
tools/perf/arch/riscv/util/auxtrace.c line 209
Quoted 10 lines
tools/perf/arch/riscv/util/auxtrace.c new file +10
-0+struct auxtrace_record *auxtrace_record__init (struct evlist *evlist,int *err) +{ +struct perf_pmu *rvtrace_pmu =NULL ; +struct perf_pmu *found_pmu =NULL ; +struct evsel *evsel; + +if (!evlist) +return NULL ; + + rvtrace_pmu =perf_pmus__find (GTRACE_PMU_NAME );
[Severity: Medium]
Does RISC-V need a perf_pmu__arch_init() implementation to set pmu->auxtrace = true and pmu->selectable = true on the gtrace PMU?
evsel__is_aux_event() checks evsel->needs_auxtrace_mmap || pmu->auxtrace, and both parse_options() (for event grouping) and record__auxtrace_init() (for auxtrace_parse_sample_options() and auxtrace_parse_aux_action()) run before auxtrace_record__options() calls rvtrace_recording_options() to set needs_auxtrace_mmap. Because pmu->auxtrace and pmu->selectable are never set for gtrace, event groups led by gtrace// are split during parsing, --aux-sample and aux-action terms fail validation, and gtrace// is omitted from perf list.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
PATCH v7 13/14 perf tools: Initial support for gtrace decoder
In reply to Mayuresh ChitaleAdd bare bones support for gtrace decoder so that the data received from the hardware by the gtrace perf driver can be written to the perf record output file. Actual decoding of the trace payload is not yet implemented.
--- tools/perf/util/Build | 1 + tools/perf/util/auxtrace.c | 3 + tools/perf/util/gtrace-decoder.c | 99 ++++++++++++++++++++++++++++++++ tools/perf/util/gtrace.h | 1 + 4 files changed, 104 insertions(+) create mode 100644 tools/perf/util/gtrace-decoder.c
tools/perf/util/Build +1 -0
@@ -147,6 +147,7 @@ perf-util-y += cs-etm.o perf-util-y += cs-etm-decoder/ endif perf-util-y += cs-etm-base.o +perf-util-y += gtrace-decoder.o perf-util-y += parse-branch-options.o perf-util-y += parse-regs-options.o
tools/perf/util/auxtrace.c +3 -0
@@ -54,6 +54,7 @@#include "arm-spe.h" #include "hisi-ptt.h" #include "s390-cpumsf.h" +#include "gtrace.h" #include "util/mmap.h" #include "powerpc-vpadtl.h"
@@ -1433,6 +1434,8 @@ int perf_event__process_auxtrace_info(const struct perf_tool *tool __maybe_unuse err =powerpc_vpadtl_process_auxtrace_info (event, session);break ;case PERF_AUXTRACE_GTRACE : + err =gtrace__process_auxtrace_info (event, session); +break ;case PERF_AUXTRACE_UNKNOWN :default :return -EINVAL ;
tools/perf/util/gtrace-decoder.c new file +99 -0
99 lines not on this page · raw message
tools/perf/util/gtrace.h +1 -0
@@ -13,4 +13,5 @@ struct perf_session;#define GTRACE_AUXTRACE_PRIV_SIZE sizeof(u64) +int gtrace__process_auxtrace_info (union perf_event *event,struct perf_session *session);#endif
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v7 13/14] perf tools: Initial support for gtrace decoder
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] perf gtrace: unconsumed pipe payload in gtrace_process_auxtrace_event() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] perf gtrace: unconsumed pipe payload in gtrace_process_auxtrace_event()
--
commit aa86191c124d5417c0c0399ddfc0c607fe1d1b9d
Author: Mayuresh Chitale <[email protected]>
perf tools: Initial support for gtrace decoder
This commit adds stub auxtrace callbacks for PERF_AUXTRACE_GTRACE so gtrace trace data can be recorded into perf.data files.
Quoted 5 lines
tools/perf/util/gtrace-decoder.c new file +0
-0
[ ... ]
tools/perf/util/gtrace-decoder.c line 38
Quoted 6 lines
tools/perf/util/gtrace-decoder.c new file +6
-0+static int gtrace_process_auxtrace_event (struct perf_session *session__maybe_unused , +union perf_event *event__maybe_unused , +const struct perf_tool *tool__maybe_unused ) +{ +return 0 ; +}
[Severity: Medium]
Does gtrace_process_auxtrace_event() leave event->auxtrace.size bytes of AUX payload unread in the stream when perf_data__is_pipe(session->data) is true?
In pipe mode, __perf_session__process_pipe_events() only reads the perf_record_auxtrace header and expects tool->auxtrace to consume the trailing payload from the pipe fd (as process_event_auxtrace_stub() does with skipn()). Returning 0 without consuming those bytes causes the next loop iteration to read raw AUX payload as the next perf_event_header, corrupting and aborting piped sessions such as "perf record -o - | perf report".
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
PATCH v7 14/14 MAINTAINERS: Add entry for RISC-V trace framework
In reply to Mayuresh ChitaleFrom: Anup Patel <[email protected]>
Add Mayuresh and myself as maintainers for the gtrace framework and its RISC-V trace drivers.
--- MAINTAINERS | 11 +++++++++++ 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
@@ -24318,6 +24318,17 @@ F: include/dt-bindings/power/thead,th1520-power.h F: include/dt-bindings/reset/thead,th1520-reset.h F: include/linux/firmware/thead/thead,th1520-aon.h +GTRACE FRAMEWORK AND RISC-V TRACE DRIVERS +M: Mayuresh Chitale <[email protected]> +M: Anup Patel <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml +F: drivers/hwtracing/gtrace/* +F: include/linux/gtrace* +F: tools/perf/arch/riscv/util/auxtrace.c +F: tools/perf/util/gtrace* + RNBD BLOCK DRIVERS M: Md. Haris Iqbal <[email protected]> M: Jack Wang <[email protected]>
Signature · 1 line
2.43.0