drm/panthor: Performance counter implementation with single manual client support
Subsystems (5)
v8 cover drm/panthor: Performance counter implementation with single manual client support
Hello,
This patch set implements initial support for performance counter sampling in Panthor, as a follow-up for Adrián Larumbe's patch set [1]. In response to follow-up comments on v6, uAPI features that were not being populated in the original series are now dropped, leaving only the features necessary for the initial implementation.
The Mali performance counters are controlled via a global firmware interface to point to a buffer that contains samples of either 64 or 128 32-bit counters per block instance. Each of the block types has its own 32-bit enable mask, allowing for finer-grained control of which counters are emitted. The size of the sample is dictated by the sum of the FIRMWARE_SIZE and the HARDWARE_SIZE fields, but neither the FW nor the HW provide sufficient information about what block types are present at what offsets. This information is architectural, and may change between different major versions, as well as different firmware revisions.
To work around the instability of the counter block layout, the kernel maintains its own offset table per GPU, and provides that via additional block headers inserted into the data. This enables old userspace clients to interpret a buffer copied over directly from hardware, and lets users like libGPUCounters[2] embedded in an executable retain some level of support for counter sampling without being updated.
The following performance counter implementation supports a single userspace explicitly requesting samples via ioctl, and managing the consumed samples via a ring buffer and a set of control indices. The user client passes in the resources to create a session, at which point they can explicitly enable or disable sampling via the PERF_SESSION_START/PERF_SESSION_STOP ioctls.
Samples are requested via an explicit PERF_SESSION_SAMPLE ioctl. When the request round trips back to the KMD, an eventfd is signaled for the user indicating that the sample is complete and can be found in the shared user/kernel ring buffer.
Existing performance counter workflows, such as those in game engines, and user-space power models/governor implementations require the ability to simultaneously obtain counter data on the same driver. Though this is not enabled out of the box, handling for multiple sessions sampling at different cadences is added via the sample accumulation handling.
Mesa MR: https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/35024
Changes in v8:
- Consolidated the series into three patches.
- Converted bitmaps to use DECLARE_BITMAP directly.
- Documented session_try_start and session_try_stop return values.
- Added overflow checks for the user_sample_size.
- Reworked session destruction order.
- Reset session state on start.
- Added graceful failure for perf registration.
- Documented interface use and memory barriers
- Fixed ring buffer size wording in the uAPI docs, and relax user buffer
size requirements
- Link to v7: https://patch.msgid.link/[email protected]
Changes in v7:
- Move the perf commands into their own ioctls
- Moved session tracking to the pfile
- Fixed padding on uAPI structs
- Removed the sampler abstraction
- Moved new registers to a separate commit
- Drop the offset argument for session creation
- Drop the count of enabled clients, using the session_list directly
- Removed the r-b's due to significant changes in the patches
- Added check for CAP_PERFMON on session setup
- Fixed architectural handling of prfcnt_en
- Link to v6: https://lore.kernel.org/dri-devel/[email protected]/
Changes in v6:
- xa_erase -> xa_delete to fix up dangling session issues
- Move setting user data into request callback to avoid setting it on error
- Embed the enable mask into the sampler
- Added helper for sample_enable and sampler_disable to eliminate duplicate checks
- Drop PM reference on session enable failure
- Drop the panthor_perf_em_zero function
- Added R-b's for the first several patches
- Added reading of the PRFCNT_FEATURES register on supporting architectures
- link to v5: https://lore.kernel.org/dri-devel/[email protected]/
Changes in v5:
- Started re-using perf info size fields instead of
recomputing the size where needed
- Removed panthor_file pointer to the drm_file
- Fixed ordering of subsystem unplug on init failure
- Using the kernel struct size to allocate memory for user-passed
uAPI struct.
- Inlined panthor_perf_sampler_{suspend,resume} into panthor_perf_{suspend,resume}
- Inlined
- Updated all callers of CIRC_SPACE_TO_END to use CIRC_SPACE
for correct ring buffer wraparound.
- Free the session and sampler enable maps on termination
- Drop the return values from panthor_perf_{suspend,resume}
- Update userdata and end timestamp on accumulation
- Removed the ptdev checks on suspend and resume paths
- Link to v4: https://lore.kernel.org/dri-devel/[email protected]/
Changes in v4:
- Added sample size to the uAPI.
- Clarified the bit-to-counter mapping for enable masks.
- Fixed IRQ handling: the PERFCNT_THRESHOLD and PERFCNT_OVERFLOW
interrupts can be handled by checking the difference between the
REQ and ACK bits, whereas PERFCNT_SAMPLE needs external data to
validate.
- FW ring buffer indices are now only wrapped when reading the buffer
and are otherwise left in their pre-wrapped form.
- Accumulation index is now bumped after the first copy.
- All insert and extract index reads now use the proper, full-width
type.
- L2 slices are now computed via a macro to extract the relevant
bits from the MEM_FEATURES register. This macro was moved from
the uAPI due to changes in the register making it unstable.
- Consistently take the sampler lock to check if a sample has been
requested.
- Link to v3: https://lore.kernel.org/dri-devel/[email protected]/
Changes in v3:
- Fixed offset issues into FW ring buffer
- Fixed sparse shader core handling
- Added pre- and post- reset handlers
- Added module param to control size of FW ring buffer
- Clarified naming on sampler functions
- Added error logging for PERF_SETUP
- Link to v2: https://lore.kernel.org/dri-devel/[email protected]/
RFC v2:
- Link to v1: https://lore.kernel.org/lkml/[email protected]/T/#m67d1f89614fe35dc0560e8304d6731eb1a6942b6
[1]: https://lore.kernel.org/lkml/[email protected]/T/#m67d1f89614fe35dc0560e8304d6731eb1a6942b6
[2]: https://github.com/ARM-software/libGPUCounters
---
Lukas Zapolskas (3):
drm/panthor: Add perf setup and session management drm/panthor: Implement performance counter firmware sampling drm/panthor: Expose performance counter queries and ioctls
drivers/gpu/drm/panthor/Makefile | 1 + drivers/gpu/drm/panthor/panthor_device.c | 15 +- drivers/gpu/drm/panthor/panthor_device.h | 12 +- drivers/gpu/drm/panthor/panthor_drv.c | 137 +- drivers/gpu/drm/panthor/panthor_fw.c | 7 + drivers/gpu/drm/panthor/panthor_fw.h | 7 + drivers/gpu/drm/panthor/panthor_gpu.c | 5 + drivers/gpu/drm/panthor/panthor_gpu.h | 2 + drivers/gpu/drm/panthor/panthor_gpu_regs.h | 4 + drivers/gpu/drm/panthor/panthor_perf.c | 2030 ++++++++++++++++++++++++++++ drivers/gpu/drm/panthor/panthor_perf.h | 39 + include/uapi/drm/panthor_drm.h | 479 +++++++ 12 files changed, 2734 insertions(+), 4 deletions(-)
---
base-commit: 97429bac371273886238bfcf63d78e7405cacb46
change-id: 20260529-panthor-performance-counters-594dea247c2b
Best regards,
Signature · 1 line
Lukas Zapolskas <[email protected]>
PATCH v8 1/3 drm/panthor: Add perf setup and session management
Add the panthor_perf system initialization and unplug code for subsystem-level handling in subsequent patches, as well as user session handling.
The user is permitted five primitive operations:
- SESSION_CREATE to create a new session with a static counter set and enable mask configuration.
- SESSION_START activates the sample and ensures that only new data is presented to the user.
- SESSION_SAMPLE requests a new sample if one is not outstanding, and signals completion via the eventfd.
- SESSION_STOP stops the session from the requesting any new samples
- SESSION_DESTROY releases the session handle back to the per-pfile pool and stops refcounting any of the user resources on the kernel side.
The current implementation allows combining the counter requests from multiple user sessions, with each session using the operations above to sample counters and manage their session's lifecycle independently.
--- drivers/gpu/drm/panthor/Makefile | 1 + drivers/gpu/drm/panthor/panthor_device.c | 7 + drivers/gpu/drm/panthor/panthor_device.h | 12 +- drivers/gpu/drm/panthor/panthor_drv.c | 9 + drivers/gpu/drm/panthor/panthor_fw.c | 3 + drivers/gpu/drm/panthor/panthor_fw.h | 1 + drivers/gpu/drm/panthor/panthor_gpu.c | 5 + drivers/gpu/drm/panthor/panthor_gpu.h | 2 + drivers/gpu/drm/panthor/panthor_gpu_regs.h | 4 + drivers/gpu/drm/panthor/panthor_perf.c | 897 +++++++++++++++++++++++++++++ drivers/gpu/drm/panthor/panthor_perf.h | 31 + include/uapi/drm/panthor_drm.h | 263 +++++++++ 12 files changed, 1234 insertions(+), 1 deletion(-)
drivers/gpu/drm/panthor/Makefile +1 -0
@@ -10,6 +10,7 @@ panthor-y := \ panthor_heap.o \ panthor_hw.o \ panthor_mmu.o \ + panthor_perf.o \ panthor_pwr.o \ panthor_sched.o
drivers/gpu/drm/panthor/panthor_device.c +7 -0
@@ -24,6 +24,7 @@#include "panthor_gpu.h" #include "panthor_hw.h" #include "panthor_mmu.h" +#include "panthor_perf.h" #include "panthor_pwr.h" #include "panthor_sched.h"
@@ -287,6 +288,10 @@ int panthor_device_init(struct panthor_device *ptdev)if (ret) goto err_unplug_fw; + ret =panthor_perf_init (ptdev); +if (ret) + goto err_unplug_sched; +panthor_gem_init (ptdev);/* Now that everything is initialized, we can enable the reset work. */
@@ -305,6 +310,8 @@ int panthor_device_init(struct panthor_device *ptdev) err_disable_autosuspend:pm_runtime_dont_use_autosuspend (ptdev->base.dev); + +err_unplug_sched:panthor_sched_unplug (ptdev); err_unplug_fw:
drivers/gpu/drm/panthor/panthor_device.h +11 -1
@@ -29,10 +29,11 @@ struct panthor_hw;struct panthor_job ;struct panthor_mmu ;struct panthor_fw ; -struct panthor_perfcnt ;struct panthor_pwr ; +struct panthor_perf ;struct panthor_vm ;struct panthor_vm_pool ; +struct panthor_perf_pool ;/** * struct panthor_soc_data - Panthor SoC Data
@@ -174,6 +175,9 @@ struct panthor_device {/** @pwr: Power control management data. */ struct panthor_pwr *pwr; +/** @perf_info: Performance counter interface information. */ +struct drm_panthor_perf_info perf_info; +/** @gpu: GPU management data. */ struct panthor_gpu *gpu;
@@ -320,6 +324,9 @@ struct panthor_device {atomic_t recovery_needed; } pm; +/** @perf: Performance counter management data. */ +struct panthor_perf *perf; +/** @profile_mask: User-set profiling flags for job accounting. */ u32 profile_mask;
@@ -374,6 +381,9 @@ struct panthor_file {/** @groups: Scheduling group pool attached to this file. */ struct panthor_group_pool *groups; +/** @sessions: Perf session pool attached to this file. */ +struct panthor_perf_pool *sessions; +/** @stats: cycle and timestamp measures for job execution. */ struct panthor_gpu_usage stats; };
drivers/gpu/drm/panthor/panthor_drv.c +9 -0
@@ -36,6 +36,7 @@#include "panthor_gpu.h" #include "panthor_heap.h" #include "panthor_mmu.h" +#include "panthor_perf.h" #include "panthor_sched.h" /**
@@ -1622,9 +1623,16 @@ panthor_open(struct drm_device *ddev, struct drm_file *file)if (ret) goto err_destroy_vm_pool; + ret =panthor_perf_pool_create (pfile); +if (ret) + goto err_destroy_group_pool; + file->driver_priv = pfile;return 0 ; +err_destroy_group_pool: +panthor_group_pool_destroy (pfile); + err_destroy_vm_pool:panthor_vm_pool_destroy (pfile);
@@ -1638,6 +1646,7 @@ panthor_postclose(struct drm_device *ddev, struct drm_file *file) {struct panthor_file *pfile = file->driver_priv; +panthor_perf_pool_destroy (pfile);panthor_group_pool_destroy (pfile);panthor_vm_pool_destroy (pfile);
drivers/gpu/drm/panthor/panthor_fw.c +3 -0
@@ -25,6 +25,7 @@#include "panthor_gpu.h" #include "panthor_hw.h" #include "panthor_mmu.h" +#include "panthor_perf.h" #include "panthor_sched.h" #include "panthor_trace.h"
@@ -1319,6 +1320,8 @@ void panthor_fw_unplug(struct panthor_device *ptdev)panthor_fw_stop (ptdev); } +panthor_perf_unplug (ptdev); +list_for_each_entry (section, &ptdev->fw->sections, node)panthor_kernel_bo_destroy (section->mem);
drivers/gpu/drm/panthor/panthor_fw.h +1 -0
@@ -198,6 +198,7 @@ struct panthor_fw_global_control_iface {u32 output_va;u32 group_num;u32 group_stride; +#define GLB_PERFCNT_FW_SIZE (x) ((((x) >> 16) << 8))u32 perfcnt_size;u32 instr_features; };
drivers/gpu/drm/panthor/panthor_gpu.c +5 -0
@@ -512,3 +512,8 @@ int panthor_gpu_coherency_init(struct panthor_device *ptdev)drm_err (&ptdev->base,"Coherency not supported by the device" );return -ENOTSUPP ; } + +u32 panthor_gpu_get_prfcnt_features (struct panthor_device *ptdev) +{ +return gpu_read (ptdev->gpu->iomem,GPU_PRFCNT_FEATURES ); +}
drivers/gpu/drm/panthor/panthor_gpu.h +2 -0
@@ -60,4 +60,6 @@ u64 panthor_gpu_get_cycle_count(struct panthor_device *ptdev);int panthor_gpu_coherency_init (struct panthor_device *ptdev); +u32 panthor_gpu_get_prfcnt_features (struct panthor_device *ptdev); +#endif
drivers/gpu/drm/panthor/panthor_gpu_regs.h +4 -0
@@ -23,6 +23,7 @@#define GPU_TILER_FEATURES 0xC#define GPU_MEM_FEATURES 0x10#define GROUPS_L2_COHERENT BIT(0) +#define GPU_MEM_FEATURES_L2_SLICES (x) ((((x) & GENMASK(11, 8)) >> 8) + 1)#define GPU_MMU_FEATURES 0x14#define GPU_MMU_FEATURES_VA_BITS (x) ((x) & GENMASK(7, 0))
@@ -70,6 +71,9 @@#define GPU_FEATURES_NEURAL_ENGINE BIT(4)#define GPU_FEATURES_RAY_TRAVERSAL BIT(5) +#define GPU_PRFCNT_FEATURES 0x68 +#define PRFCNT_FEATURES_COUNTER_BLOCK_SIZE (x) (((x) & GENMASK(7, 0)) << 8) +#define GPU_TIMESTAMP_OFFSET 0x88#define GPU_CYCLE_COUNT 0x90#define GPU_TIMESTAMP 0x98
drivers/gpu/drm/panthor/panthor_perf.c new file +897 -0
897 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_perf.h new file +31 -0
@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 or MIT */ +/* Copyright 2025 Collabora Ltd */ +/* Copyright 2025 Arm ltd. */ + +#ifndef __PANTHOR_PERF_H__ +#define __PANTHOR_PERF_H__ + +#include <linux/types.h> + +struct drm_file ; +struct drm_panthor_perf_session_create ; +struct panthor_device ; +struct panthor_file ; +struct panthor_perf ; +struct panthor_perf_pool ; + +int panthor_perf_pool_create (struct panthor_file *pfile); +void panthor_perf_pool_destroy (struct panthor_file *pfile); + +int panthor_perf_init (struct panthor_device *ptdev); +void panthor_perf_unplug (struct panthor_device *ptdev); + +int panthor_perf_session_create (struct drm_file *file, +struct drm_panthor_perf_session_create *setup_args, +unsigned long *sid); +int panthor_perf_session_destroy (struct panthor_file *pfile,unsigned int handle); +int panthor_perf_session_start (struct panthor_file *pfile,u32 handle,u64 user_data); +int panthor_perf_session_stop (struct panthor_file *pfile,unsigned int sid,u64 user_data); +int panthor_perf_session_sample (struct panthor_file *pfile,unsigned int handle,u64 user_data); + +#endif /* __PANTHOR_PERF_H__ */
include/uapi/drm/panthor_drm.h +263 -0
275 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v8 1/3] drm/panthor: Add perf setup and session management
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] drm/panthor: 64-bit smp_load_acquire() in session_read_extract_idx() - [High] drm/panthor: missing…
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [High] drm/panthor: 64-bit smp_load_acquire() in session_read_extract_idx()
- [High] drm/panthor: missing output handle field in struct drm_panthor_perf_session_create
- [High] drm/panthor: panthor_perf_bo_get() discards iosys_map is_iomem flag
- [Medium] drm/panthor: unreachable stopping check in session_try_sample()
- [Low] drm/panthor: format type mismatch for size_t in panthor_perf_bo_get()
- [Low] drm/panthor: split declaration of __free() vars in panthor_perf.c
--
commit e36bb7b1917c06a9ff609cb883067fc62562c67b
Author: Lukas Zapolskas <[email protected]>
drm/panthor: Add perf setup and session management
This commit adds performance counter subsystem initialization and unplug handling, along with user session lifecycle management and the associated UAPI structures.
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_perf.c new file +0
-0
[ ... ]
Quoted 15 lines
drivers/gpu/drm/panthor/panthor_perf.c new file +15
-0+static int +panthor_perf_bo_get (struct drm_file *file,u32 handle,size_t min_size, +struct panthor_perf_bo *bo) +{ +struct drm_device *ddev = file->minor->dev; +struct drm_gem_object *obj; +struct iosys_map map; +int ret; + +if (WARN_ON_ONCE (!bo)) +return -EINVAL ; + + obj =drm_gem_object_lookup (file, handle); +if (!obj) { +drm_err (ddev,"Could not find handle %d!\n" , handle);
drivers/gpu/drm/panthor/panthor_perf.c line 259
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c new file +10
-0+return -EINVAL ; + } + +if (obj->size < min_size) { +drm_err (ddev, +"Perf BO expected min size %lu, passed BO with size %lu\n" , + min_size, obj->size); + ret = -ENOMEM ; + goto err_put; + }
[Severity: Low]
Both min_size and obj->size have type size_t, so formatting them with %lu causes a -Wformat warning on 32-bit architectures. Should this use %zu instead?
Quoted 15 lines
drivers/gpu/drm/panthor/panthor_perf.c new file +15
-0+ + ret =drm_gem_vmap (obj, &map); +if (ret) + goto err_put; + + bo->obj = obj; + bo->vaddr = map.vaddr; + +return 0 ; + +err_put: +drm_gem_object_put (obj); +return ret; +} +
drivers/gpu/drm/panthor/panthor_perf.c line 284
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c new file +10
-0+static void panthor_perf_bo_put (struct panthor_perf_bo *bo) +{ +if (!bo->obj) +return ; + +if (bo->vaddr) { +struct iosys_map map =IOSYS_MAP_INIT_VADDR (bo->vaddr); + +drm_gem_vunmap (bo->obj, &map); + }
[Severity: High]
What happens if userspace passes a PRIME-imported GEM handle backed by IOMEM to panthor_perf_bo_get()?
For imported objects, drm_gem_vmap() delegates to dma_buf_vmap(), which can return an IOMEM mapping with map.is_iomem set to true. Because panthor_perf_bo_get() discards map.is_iomem and stores only map.vaddr, the driver dereferences session->control and session->samples directly as regular system memory, and panthor_perf_bo_put() passes an iosys_map with is_iomem = false to drm_gem_vunmap(), triggering BUG_ON(!iosys_map_is_equal(&dmabuf->vmap_ptr, map)) in dma_buf_vunmap().
[ ... ]
Quoted 25 lines
drivers/gpu/drm/panthor/panthor_perf.c new file +25
-0+#define DEFINE_SESSION_STATE_CHECK (name, _state) \ + static bool session_is_## name(struct panthor_perf_session *session) \ + { \ + lockdep_assert_held(&session->state_lock); \ + \ + return session->state == (_state); \ + } + +DEFINE_SESSION_STATE_CHECK (active,PANTHOR_PERF_SESSION_ACTIVE ); +DEFINE_SESSION_STATE_CHECK (starting,PANTHOR_PERF_SESSION_STARTING ); +DEFINE_SESSION_STATE_CHECK (stopping,PANTHOR_PERF_SESSION_STOPPING ); +DEFINE_SESSION_STATE_CHECK (destroy,PANTHOR_PERF_SESSION_DESTROY ); + +static bool session_has_overflow (struct panthor_perf_session *session) +{ +lockdep_assert_held (&session->state_lock); + +return session->overflow; +} + +static void session_set_state (struct panthor_perf_session *session, +enum panthor_perf_session_state state) +{ +lockdep_assert_held (&session->state_lock); +
drivers/gpu/drm/panthor/panthor_perf.c line 379
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c new file +10
-0+ session->state = state; +} + +static u64 session_read_extract_idx (struct panthor_perf_session *session) +{ +/* Userspace will update their own extract index to indicate that a sample is consumed +* from the ringbuffer, and we must ensure we read the latest value. +*/ +return smp_load_acquire (&session->control->extract_idx); +}
[Severity: High]
Does smp_load_acquire(&session->control->extract_idx) compile on 32-bit SMP architectures?
session->control->extract_idx is a __u64, and on 32-bit SMP targets smp_load_acquire() triggers compiletime_assert_atomic_type() because sizeof(__u64) exceeds sizeof(long).
[ ... ]
drivers/gpu/drm/panthor/panthor_perf.c line 672
Quoted 9 lines
drivers/gpu/drm/panthor/panthor_perf.c new file +9
-0+static int session_try_sample (struct panthor_perf_session *session,u64 user_data) +{ +guard (spinlock_irqsave)(&session->state_lock); + +if (!session_is_active (session)) +return -EINVAL ; + +if (session_is_stopping (session)) +return -EBUSY ;
[Severity: Medium]
Can session_is_stopping(session) ever be reached here?
session->state holds a single enum panthor_perf_session_state value, so when the state is PANTHOR_PERF_SESSION_STOPPING, !session_is_active() is
already true and session_try_sample() returns -EINVAL instead of -EBUSY.
[ ... ]
drivers/gpu/drm/panthor/panthor_perf.c line 744
Quoted 11 lines
drivers/gpu/drm/panthor/panthor_perf.c new file +11
-0+int panthor_perf_session_destroy (struct panthor_file *pfile,u32 handle) +{ + struct panthor_perf_session *session __free(session_put) = NULL; +struct panthor_perf *perf = pfile->ptdev->perf; +struct panthor_perf_pool *pool = pfile->sessions; +int ret = -EINVAL ; + +if (!perf) +return -EOPNOTSUPP ; + + session =session_from_handle (pool, handle);
[Severity: Low]
This isn't a bug, but cleanup.h guidelines prefer defining and initializing __free() variables in a single statement rather than initializing them to NULL at the top of the function and assigning them later. The same pattern appears in panthor_perf_session_start(), panthor_perf_session_stop(), and panthor_perf_session_sample().
Quoted 4 lines
include/uapi/drm/panthor_drm.h +0
-0
[ ... ]
Quoted 28 lines
include/uapi/drm/panthor_drm.h +28
-0+struct drm_panthor_perf_session_create { +/** +* @block_set: Set of performance counter blocks, member of +* enum drm_panthor_perf_counter_set. +* +* This is a global configuration and only one set can be active at a time. If +* another client has already requested a counter set, any further requests +* for a different counter set will fail and return an -EBUSY. +* +* If the requested set does not exist, the request will fail and return an -EINVAL. +* +* Some sets have additional requirements to be enabled, and the setup request will +* fail with an -EACCES if these requirements are not satisfied. +*/ +__u32 block_set; + +/** @fd: eventfd for signalling the availability of a new sample. */ +__u32 fd; + +/** @ringbuf_handle: Handle to the BO to write perf counter sample to. */ +__u32 ringbuf_handle; + +/** +* @control_handle: Handle to the BO containing a contiguous 16 byte range, used for the +* insert and extract indices for the ringbuffer. +*/ +__u32 control_handle; +
include/uapi/drm/panthor_drm.h struct drm_panthor_bo_query_info { · line 1498
Quoted 10 lines
include/uapi/drm/panthor_drm.h +10
-0+/** +* @sample_slots: The number of slots available in the userspace-provided BO. Must be +* a power of 2 and at least 2, to leave space for a sample and the final STOP sample. +* +* If sample_slots * sample_size is smaller than the ringbuf BO, the setup will fail. +*/ +__u32 sample_slots; + +/** @pad: MBZ. */ +__u32 pad;
[Severity: High]
Should struct drm_panthor_perf_session_create have an output field for the allocated session handle instead of returning the handle via *sid as the ioctl return value?
drm_ioctl() expects driver ioctl handlers to return 0 on success and a negative errno on failure, and logs an error whenever the return code is non-zero. Returning a positive session ID from the ioctl breaks standard DRM userspace wrappers that check for a zero return value on success, and differs from all other object-creation ioctls in panthor (VM_CREATE, BO_CREATE, GROUP_CREATE, TILER_HEAP_CREATE).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v8 2/3 drm/panthor: Implement performance counter firmware sampling
In reply to Lukas Zapolskaspanthor_perf aggregates counter and set requests coming from userspace and mediates interactions with the FW interface, to ensure that user sessions cannot override the global configuration.
Upon receipt of a relevant interrupt, corresponding to one of the three relevant bits of the GLB_ACK register, panthor_perf takes any samples that occurred, and, based on the insert and extract indices, accumulates them to an internal storage buffer after zero-extending the counters from the 32-bit counters emitted by the hardware to 64-bit counters for internal accumulation.
When the performance counters are enabled, the FW ensures no counter data is lost when entering and leaving non-counting regions by producing automatic samples that do not correspond to a GLB_REQ.PRFCNT_SAMPLE request. Such regions may be per hardware unit, such as when a shader core powers down, or global. Most of these events do not directly correspond to session sample requests, so any intermediary counter data must be stored into a temporary accumulation buffer.
If there are sessions waiting for a sample, this accumulated buffer will be taken, and emitted for each waiting client. During this phase, information like the timestamps of sample request and sample emission, type of the counter block and block index annotations are added to the sample header and block headers. If no sessions are waiting for a sample, this accumulation buffer is kept until the next time a sample is requested.
Special handling is needed for the PRFCNT_OVERFLOW interrupt, which is an indication that the internal sample handling rate was insufficient.
A semi-static table maintains the mappings between the offsets from the base of a FW sample to the corresponding block type. Since the counter blocks themselves do not contain this information, this needs to be done in the kernel. This also handles sparseness in the shader core buffer region, which depends on SHADER_PRESENT and architecture revision.
The worker handling samples also drives session state transitions, taking sessions from any transition state (STARTING, STOPPING, DESTROY) to a stable state (ACTIVE, STOPPED, or freed).
The number of ring buffer slots is configurable through module param to allow for a lower memory footprint on memory constrained systems.
--- drivers/gpu/drm/panthor/panthor_device.c | 8 +- drivers/gpu/drm/panthor/panthor_fw.c | 4 + drivers/gpu/drm/panthor/panthor_fw.h | 8 +- drivers/gpu/drm/panthor/panthor_perf.c | 1163 +++++++++++++++++++++++++++++- drivers/gpu/drm/panthor/panthor_perf.h | 8 + 5 files changed, 1174 insertions(+), 17 deletions(-)
drivers/gpu/drm/panthor/panthor_device.c +7 -1
@@ -137,6 +137,7 @@ static void panthor_device_reset_work(struct work_struct *work)if (!drm_dev_enter (&ptdev->base, &cookie))return ; +panthor_perf_pre_reset (ptdev);panthor_sched_pre_reset (ptdev);panthor_fw_pre_reset (ptdev, true);panthor_mmu_pre_reset (ptdev);
@@ -146,6 +147,8 @@ static void panthor_device_reset_work(struct work_struct *work) ret =panthor_fw_post_reset (ptdev);atomic_set (&ptdev->reset.pending,0 );panthor_sched_post_reset (ptdev, ret !=0 ); +if (!ret) +panthor_perf_post_reset (ptdev);drm_dev_exit (cookie);if (ret) {
@@ -537,8 +540,10 @@ int panthor_device_resume(struct device *dev) ret =panthor_device_resume_hw_components (ptdev); } -if (!ret) +if (!ret) {panthor_sched_resume (ptdev); +panthor_perf_resume (ptdev); + }drm_dev_exit (cookie);
@@ -602,6 +607,7 @@ int panthor_device_suspend(struct device *dev)/* We prepare everything as if we were resetting the GPU. * The end of the reset will happen in the resume path though. */ +panthor_perf_suspend (ptdev);panthor_sched_suspend (ptdev);panthor_fw_suspend (ptdev);panthor_mmu_suspend (ptdev);
drivers/gpu/drm/panthor/panthor_fw.c +4 -0
@@ -1073,9 +1073,12 @@ static void panthor_fw_init_global_iface(struct panthor_device *ptdev)/* Enable interrupts we care about. */ glb_iface->input->ack_irq_mask =GLB_CFG_ALLOC_EN | +GLB_PERFCNT_SAMPLE |GLB_PING |GLB_CFG_PROGRESS_TIMER |GLB_CFG_POWEROFF_TIMER | +GLB_PERFCNT_THRESHOLD | +GLB_PERFCNT_OVERFLOW |GLB_IDLE_EN |GLB_IDLE ;
@@ -1123,6 +1126,7 @@ static void panthor_job_irq_handler(struct panthor_irq *pirq, u32 status) duration =U32_MAX ;trace_gpu_job_irq (ptdev->base.dev, status, duration); } +panthor_perf_report_irq (ptdev, status); }static irqreturn_t panthor_job_irq_threaded_handler (int irq,void *data)
drivers/gpu/drm/panthor/panthor_fw.h +7 -1
@@ -198,9 +198,12 @@ struct panthor_fw_global_control_iface {u32 output_va;u32 group_num;u32 group_stride; -#define GLB_PERFCNT_FW_SIZE (x) ((((x) >> 16) << 8)) +#define GLB_PERFCNT_FW_SIZE (x) (((x) & GENMASK(32, 16)) >> 8) +#define GLB_PERFCNT_HW_SIZE (x) (((x) & GENMASK(15, 0)) << 8)u32 perfcnt_size;u32 instr_features; +#define PERFCNT_FEATURES_MD_SIZE (x) (((x) & GENMASK(3, 0)) << 8) +u32 perfcnt_features; };struct panthor_fw_global_input_iface {
@@ -249,6 +252,9 @@ struct panthor_fw_global_input_iface {u64 perfcnt_base;u32 perfcnt_extract;u32 reserved3[3 ]; +#define GLB_PERFCNT_CONFIG_SIZE (x) ((x) & GENMASK(7, 0)) +#define GLB_PERFCNT_CONFIG_SET (x) (((x) & GENMASK(1, 0)) << 8) +#define GLB_PERFCNT_METADATA_ENABLE BIT(10)u32 perfcnt_config;u32 perfcnt_csg_select;u32 perfcnt_fw_enable;
drivers/gpu/drm/panthor/panthor_perf.c +1148 -15
1,379 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_perf.h +8 -0
@@ -18,6 +18,8 @@ int panthor_perf_pool_create(struct panthor_file *pfile);void panthor_perf_pool_destroy (struct panthor_file *pfile);int panthor_perf_init (struct panthor_device *ptdev); +void panthor_perf_suspend (struct panthor_device *ptdev); +void panthor_perf_resume (struct panthor_device *ptdev);void panthor_perf_unplug (struct panthor_device *ptdev); int panthor_perf_session_create(struct drm_file *file,
@@ -28,4 +30,10 @@ int panthor_perf_session_start(struct panthor_file *pfile, u32 handle, u64 user_int panthor_perf_session_stop (struct panthor_file *pfile,unsigned int sid,u64 user_data);int panthor_perf_session_sample (struct panthor_file *pfile,unsigned int handle,u64 user_data); +void panthor_perf_report_irq (struct panthor_device *ptdev,u32 status); + +void panthor_perf_pre_reset (struct panthor_device *ptdev); + +void panthor_perf_post_reset (struct panthor_device *ptdev); +#endif /* __PANTHOR_PERF_H__ */
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v8 2/3] drm/panthor: Implement performance counter firmware sampling
Thank you for your contribution! Sashiko AI review found 20 potential issues to consider: - [High] drm/panthor: out-of-range GENMASK(32, 16) in GLB_PERFCNT_FW_SIZE() - [High] drm/panthor: WQ_HIGHPRI…
Thank you for your contribution! Sashiko AI review found 20 potential issues to consider:
- [High] drm/panthor: out-of-range GENMASK(32, 16) in GLB_PERFCNT_FW_SIZE()
- [High] drm/panthor: WQ_HIGHPRI busy-loop in panthor_perf_handle_fw_events()
- [High] drm/panthor: missing sample_work flush in perf suspend and pre-reset
- [High] drm/panthor: unsynchronized teardown in panthor_perf_unplug()
- [High] drm/panthor: panthor_perf_bo_get() drops iosys_map.is_iomem flag
- [High] drm/panthor: 64-bit modulo in session_copy_sample() breaks 32-bit build
- [Medium] drm/panthor: leaked runtime PM refs in panthor_perf_unplug()
- [Medium] drm/panthor: raw pm_runtime_resume_and_get() in session create
- [Medium] drm/panthor: sample_start_ns clobbered in session_try_sample/stop()
- [Medium] drm/panthor: premature sample emission in panthor_perf_handle_fw_events()
- [Medium] drm/panthor: unsynchronized state and index access in session_copy_sample()
- [Medium] drm/panthor: sample request race in panthor_perf_report_irq()
- [Medium] drm/panthor: zeroed block timestamps in session_accumulate_sample()
- [Medium] drm/panthor: enable mask overwrite in panthor_perf_handle_fw_samples()
- [Medium] drm/panthor: pending perf sample lost across GPU reset in panthor_perf_post_reset()
- [Medium] drm/panthor: lost pending perf sample across reset and suspend/resume
- [Medium] drm/panthor: perf reset and suspend lose samples and wedge sessions
- [Medium] drm/panthor: race between panthor_perf_unplug() and panthor_perf_pool_destroy()
- [Low] drm/panthor: unused ringbuf slot macros disagree with perf_ringbuf_slots_set()
- [Low] drm/panthor: unreachable stopping check in session_try_sample() --
commit af99224f8a37db1bdbdbaef8c01eeb9aff21b441
Author: Lukas Zapolskas <[email protected]>
drm/panthor: Implement performance counter firmware sampling
This commit implements performance counter firmware ringbuffer setup, interrupt handling, sample accumulation, and session state transitions for Panthor.
drivers/gpu/drm/panthor/panthor_fw.h struct panthor_fw_global_control_iface { · line 203
Quoted 12 lines
drivers/gpu/drm/panthor/panthor_fw.h +2
-1@@ -198,9 +198,12 @@ struct panthor_fw_global_control_iface {u32 output_va;u32 group_num;u32 group_stride; -#define GLB_PERFCNT_FW_SIZE (x) ((((x) >> 16) << 8)) +#define GLB_PERFCNT_FW_SIZE (x) (((x) & GENMASK(32, 16)) >> 8) +#define GLB_PERFCNT_HW_SIZE (x) (((x) & GENMASK(15, 0)) << 8)u32 perfcnt_size;
[Severity: High]
Should GLB_PERFCNT_FW_SIZE() use GENMASK(31, 16) instead of GENMASK(32, 16)? perfcnt_size is a u32, and on 32-bit builds GENMASK(32, 16) shifts by BITS_PER_LONG - 1 - 32 (-1), failing the build with -Wshift-count-negative.
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_perf.c +0
-0
[ ... ]
Quoted 17 lines
drivers/gpu/drm/panthor/panthor_perf.c +17
-0+/** +* PANTHOR_PERF_RINGBUF_SLOTS_MIN - The minimum permitted number of slots in the Panthor perf +* ring buffer. +*/ +#define PANTHOR_PERF_RINGBUF_SLOTS_MIN (16) + +/** +* PANTHOR_PERF_RINGBUF_SLOTS_MAX - The maximum permitted number of slots in the Panthor perf +* ring buffer. +*/ +#define PANTHOR_PERF_RINGBUF_SLOTS_MAX (256) + +static unsigned int perf_ringbuf_slots =32 ; + +static int perf_ringbuf_slots_set (const char *val,const struct kernel_param *kp) +{ +unsigned int slots;
drivers/gpu/drm/panthor/panthor_perf.c line 106
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+int ret =kstrtouint (val,0 , &slots); + +if (ret) +return ret; + +if (!is_power_of_2 (slots)) +return -EINVAL ; + +return param_set_uint_minmax (val, kp,16 ,128 ); +}
[Severity: Low]PANTHOR_PERF_RINGBUF_SLOTS_MIN and PANTHOR_PERF_RINGBUF_SLOTS_MAX are unused, and PANTHOR_PERF_RINGBUF_SLOTS_MAX is 256 (which would truncate to 0 in the 8-bit GLB_PERFCNT_CONFIG_SIZE() field) while perf_ringbuf_slots_set() hardcodes 16 and 128.
[ ... ]
Quoted 39 lines
drivers/gpu/drm/panthor/panthor_perf.c +39
-0+static void panthor_perf_handle_fw_samples (struct panthor_device *ptdev, +u32 extract_idx,u32 insert_idx) +{ +struct panthor_perf *perf = ptdev->perf; +const size_t ann_block_size = +get_annotated_block_size (ptdev->perf_info.counters_per_block); +u32 i; + +for (i = extract_idx; i != insert_idx; i++) { +u32 slot = i % perf->sample_slots; +u8 *fw_sample = (u8 *)perf->rb->kmap + slot * perf->fw_sample_size; +struct panthor_perf_fw_block_iter fw_iter = { .perf_ctx = perf }; +size_t ann_off = ptdev->perf_info.sample_header_size; + +for (;panthor_perf_fw_block_iter_valid (&fw_iter); + fw_iter.offset += fw_iter.perf_ctx->fw_block_size, fw_iter.idx++) { +struct panthor_perf_counter_block *blk = + (typeof (blk))(perf->sample + ann_off); +u32 *const block = (u32 *)(fw_sample + fw_iter.offset); +const u32 prfcnt_en = block[PANTHOR_CTR_PRFCNT_EN ]; + +if (fw_iter.type ==DRM_PANTHOR_PERF_BLOCK_METADATA ) { +/* +* The host must clear the SAMPLE_REASON to acknowledge it has +* consumed the sample. +*/ + block[PANTHOR_CTR_SAMPLE_REASON ] =0 ; +continue ; + } + +if (!(perf->blocks[fw_iter.type].phys_mask &BIT_ULL (fw_iter.idx))) +continue ; + + blk->header = (struct drm_panthor_perf_block_header ) { + .block_idx = fw_iter.idx, + .block_type = fw_iter.type, + }; + +if (prfcnt_en ==0 )
drivers/gpu/drm/panthor/panthor_perf.c session_from_handle(struct panthor_perf_pool *pool, unsigned long handle) · line 656
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+ goto advance_block; + +/* +* The four header counters must be treated differently, because they are +* not additive. For the fourth, the assignment does not matter, as it +* is reserved and should be zero. +*/ + blk->counters[PANTHOR_CTR_TIMESTAMP_LO ] = block[PANTHOR_CTR_TIMESTAMP_LO ]; + blk->counters[PANTHOR_CTR_TIMESTAMP_HI ] = block[PANTHOR_CTR_TIMESTAMP_HI ]; + blk->counters[PANTHOR_CTR_PRFCNT_EN ] = block[PANTHOR_CTR_PRFCNT_EN ];
[Severity: Medium]
When multiple firmware sample slots are processed in one panthor_perf_handle_fw_samples() call, does assigning blk->counters[PANTHOR_CTR_PRFCNT_EN] overwrite enable bits from earlier slots in the batch instead of OR-ing them?
In session_populate_sample(), expand_enable_mask() uses iter.src_blk->counters[PANTHOR_CTR_PRFCNT_EN] to find which counters in perf->sample need to be zeroed if they were not requested by the session. If an earlier slot in the batch had counter bits set in prfcnt_en and a later slot had prfcnt_en == 1 (or a smaller subset), overwriting PANTHOR_CTR_PRFCNT_EN drops the earlier bits and leaves those unrequested counters unzeroed in the session's sample buffer.
[ ... ]
Quoted 14 lines
drivers/gpu/drm/panthor/panthor_perf.c +14
-0+static void session_accumulate_sample (struct panthor_device *ptdev, +struct panthor_perf_session *session, +u8 *session_sample,u8 *perf_sample) +{ +const struct drm_panthor_perf_info *const perf_info = &ptdev->perf_info; +struct panthor_perf_sample_block_iter iter; + +sample_block_iter_init (perf_info, &session->enabled_counters, perf_sample, + session_sample, &iter); + +while (sample_block_iter_next (&iter)) { +const struct panthor_perf_counter_block *src_blk = iter.src_blk; +struct panthor_perf_counter_block *dst_blk = iter.dst_blk; +unsigned int i =PANTHOR_HEADER_COUNTERS ;
drivers/gpu/drm/panthor/panthor_perf.c static bool session_has_space(struct panthor_perf_session *session, u32 needed) · line 937
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+ + dst_blk->counters[PANTHOR_CTR_TIMESTAMP_HI ] = + src_blk->counters[PANTHOR_CTR_TIMESTAMP_HI ]; + dst_blk->counters[PANTHOR_CTR_TIMESTAMP_LO ] = + src_blk->counters[PANTHOR_CTR_TIMESTAMP_LO ]; + +for_each_set_bit_from (i, iter.requested_mask, iter.counters) + dst_blk->counters[i] += src_blk->counters[i]; + } +}
[Severity: Medium]
Can session_accumulate_sample() overwrite valid block timestamps with zero when accumulating multiple sample batches?
panthor_perf_handle_fw_events() zeroes perf->sample after each run, and panthor_perf_handle_fw_samples() skips writing PANTHOR_CTR_TIMESTAMP_LO, PANTHOR_CTR_TIMESTAMP_HI, and PANTHOR_CTR_PRFCNT_EN when prfcnt_en == 0. When a later batch has prfcnt_en == 0 for a block (for example after an automatic sample on a single powered-down shader core), session_accumulate_sample() unconditionally copies the zeroed timestamps from src_blk over dst_blk. In addition, session_populate_sample() sets blk->counters[PANTHOR_CTR_PRFCNT_EN] solely from iter.requested_mask without masking against src_blk->counters[PANTHOR_CTR_PRFCNT_EN], and session_accumulate_sample() never updates dst_blk->counters[PANTHOR_CTR_PRFCNT_EN].
[ ... ]
Quoted 16 lines
drivers/gpu/drm/panthor/panthor_perf.c +16
-0+static void session_copy_sample (struct panthor_device *ptdev, +struct panthor_perf_session *session,u32 flags) +{ +struct panthor_perf *perf = ptdev->perf; +const size_t sample_size = ptdev->perf_info.sample_size; +const u64 insert_idx = session->insert_idx; +u8 *new_sample; + + scoped_guard(spinlock_irqsave, &session->state_lock) { +if (!session_is_active (session) && !session_is_stopping (session)) +return ; + +if (flags &DRM_PANTHOR_PERF_SAMPLE_OVERFLOW ) + session->overflow = true; + session->sample_flags |= flags; + }
drivers/gpu/drm/panthor/panthor_perf.c static bool session_has_space(struct panthor_perf_session *session, u32 needed) · line 1025
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+ + new_sample = session->samples + (insert_idx % session->ringbuf_slots) * sample_size; + +if (session->accum_idx != insert_idx) { +session_populate_sample (ptdev, session, new_sample, perf->sample); + session->accum_idx = insert_idx; + }else { +session_accumulate_sample (ptdev, session, new_sample, perf->sample); + } +}
[Severity: Medium]
Can session_copy_sample() and session_emit_sample() race with session_try_start(), session_try_stop(), or session_try_sample()?
CPU0 (sample_work) CPU1 (ioctl)panthor_perf_handle_fw_events()
panthor_perf_handle_fw_samples()
lock(&perf->session_lock)
session_copy_sample()
/* sees STOPPED, returns */
session_start()
lock(&perf->op_lock)
session_try_start()
lock(&session->state_lock)
state = STARTING
accum_idx = insert_idx - 1
unlock(&session->state_lock)
session_emit_sample()
lock(&session->state_lock)
/* sees STARTING, sets ACTIVE */
unlock(&session->state_lock)panthor_perf_handle_fw_events() holds perf->session_lock, while session_start(), session_stop(), and session_sample() hold perf->op_lock. session_copy_sample() also reads session->insert_idx and updates session->accum_idx outside session->state_lock, whereas session_try_start() updates session->accum_idx under session->state_lock. If a session transition occurs after panthor_perf_handle_fw_samples() (or between session_copy_sample() and session_emit_sample()) before perf_fw_request_sample() is called, session_emit_sample() completes the transition using the pre-request automatic sample, which can leave timestamp_start_ns > timestamp_end_ns or leak pre-start counters into an active session.
drivers/gpu/drm/panthor/panthor_perf.c static bool session_has_space(struct panthor_perf_session *session, u32 needed) · line 1032
Quoted 7 lines
drivers/gpu/drm/panthor/panthor_perf.c +7
-0+ +static void session_publish_sample (struct panthor_perf *perf, +struct panthor_perf_session *session) +{ +const struct drm_panthor_perf_info *const info = &perf->ptdev->perf_info; +u8 *sample = session->samples + + (session->insert_idx % session->ringbuf_slots) * info->sample_size;
[Severity: High]
Does (insert_idx % session->ringbuf_slots) in session_copy_sample() and session_publish_sample() fail to link on 32-bit architectures?
insert_idx is a u64 and session->ringbuf_slots is a size_t, so the % operator emits a 64-bit unsigned modulo (__aeabi_uldivmod / __umoddi3) on 32-bit builds where CONFIG_DRM_PANTHOR is enabled (ARM || COMPILE_TEST). Since panthor_perf_session_create() requires ringbuf_slots to be a power of two, this can use (insert_idx & (session->ringbuf_slots - 1)).
[ ... ]
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_perf.c +5
-0+static void panthor_perf_handle_fw_events (struct panthor_perf *perf) +{ +struct panthor_device *const ptdev = perf->ptdev; +struct panthor_fw_global_iface *glb_iface =panthor_fw_get_glb_iface (ptdev); +const u32 extract_idx =READ_ONCE (glb_iface->input->perfcnt_extract);
drivers/gpu/drm/panthor/panthor_perf.c static bool session_has_space(struct panthor_perf_session *session, u32 needed) · line 1101
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+const u32 insert_idx =READ_ONCE (glb_iface->output->perfcnt_insert); +const u32 ack =READ_ONCE (glb_iface->output->ack); +const u32 req =READ_ONCE (glb_iface->input->req); +const u32 events = (req ^ ack) & (GLB_PERFCNT_THRESHOLD |GLB_PERFCNT_OVERFLOW ); +const u32 flags = events &GLB_PERFCNT_OVERFLOW ?DRM_PANTHOR_PERF_SAMPLE_OVERFLOW :0 ; + +if (panthor_device_reset_is_pending (ptdev)) { +queue_work (perf->sample_workq, &perf->sample_work); +return ; + }
[Severity: High]
Does re-queueing perf->sample_work when panthor_device_reset_is_pending() is true cause a 100% CPU busy-loop on the WQ_HIGHPRI workqueue?
When panthor_perf_sample_worker() runs, WORK_STRUCT_PENDING_BIT on perf->sample_work has already been cleared. Because perf->sample_work is a work_struct rather than a delayed_work, calling queue_work() from inside panthor_perf_handle_fw_events() immediately marks it runnable again with no delay, so the WQ_HIGHPRI worker spins calling panthor_perf_handle_fw_events() and perf_detach_sessions() until ptdev->reset.pending is cleared (or across the entire suspend duration if a reset was scheduled when panthor_device_reset_work() bailed out for pm.state != PANTHOR_DEVICE_PM_STATE_ACTIVE). In addition, panthor_device_reset_work() clears ptdev->reset.pending before calling panthor_perf_post_reset(), allowing the spinning worker to read perfcnt_extract and perfcnt_insert before panthor_perf_post_reset() re-initializes sampling.
Quoted 11 lines
drivers/gpu/drm/panthor/panthor_perf.c +11
-0+ +if ((u32)(insert_idx - extract_idx) > perf->sample_slots) { +drm_warn_once (&ptdev->base,"Perf FW ring distance exceeds capacity\n" ); +return ; + } + +if (extract_idx == insert_idx) + goto ack_events; + +panthor_perf_handle_fw_samples (ptdev, extract_idx, insert_idx); +WRITE_ONCE (glb_iface->input->perfcnt_extract, insert_idx);
drivers/gpu/drm/panthor/panthor_perf.c static bool session_has_space(struct panthor_perf_session *session, u32 needed) · line 1122
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+ + scoped_guard(mutex, &perf->session_lock) { +struct panthor_perf_session *session; + +list_for_each_entry (session, &perf->session_list, sessions) +session_copy_sample (ptdev, session, flags); + +list_for_each_entry (session, &perf->session_list, sessions) +session_emit_sample (perf, session); + }
[Severity: Medium]
Can automatic firmware samples cause panthor_perf_handle_fw_events() to call session_emit_sample() before an explicit GLB_PERFCNT_SAMPLE request has been acknowledged?
Firmware emits automatic samples on shader core power transitions and GLB_PERFCNT_THRESHOLD interrupts. Because panthor_perf_handle_fw_events() calls session_emit_sample() unconditionally without checking whether GLB_PERFCNT_SAMPLE was acknowledged (or checking the metadata block sample reason), an automatic sample arriving while a GLB_PERFCNT_SAMPLE request is still in flight will prematurely transition a STARTING session to ACTIVE (so the subsequent clearing sample is accumulated into the active session instead of discarded) or prematurely publish a partial sample for a STOPPING or pending_sample session.
Quoted 25 lines
drivers/gpu/drm/panthor/panthor_perf.c +25
-0+ +memset (perf->sample,0 , ptdev->perf_info.sample_size); + +ack_events: +if (events) +panthor_fw_update_reqs (glb_iface, req, ack, events); +} + +void panthor_perf_report_irq (struct panthor_device *ptdev,u32 status) +{ +struct panthor_fw_global_iface *glb_iface =panthor_fw_get_glb_iface (ptdev); +struct panthor_perf *const perf = ptdev->perf; + +if (!(status &JOB_INT_GLOBAL_IF )) +return ; + +if (!perf) +return ; + +const u32 ack =READ_ONCE (glb_iface->output->ack); +const u32 req =READ_ONCE (glb_iface->input->req); +const u32 events = (req ^ ack) & (GLB_PERFCNT_THRESHOLD |GLB_PERFCNT_OVERFLOW ); +const bool sample_acked = ((req &GLB_PERFCNT_SAMPLE ) == (ack &GLB_PERFCNT_SAMPLE )); +const u32 extract_idx =READ_ONCE (glb_iface->input->perfcnt_extract); +const u32 insert_idx =READ_ONCE (glb_iface->output->perfcnt_insert);
drivers/gpu/drm/panthor/panthor_perf.c static bool session_has_space(struct panthor_perf_session *session, u32 needed) · line 1157
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+const bool has_sample = sample_acked && insert_idx != extract_idx; + +if (sample_acked) { +guard (spinlock_irqsave)(&perf->pend_lock); + perf->fw_sample_requested = false; + } + +if (events || has_sample) +queue_work (perf->sample_workq, &perf->sample_work); +}
[Severity: Medium]
Can panthor_perf_report_irq() race with perf_fw_request_sample() and clear perf->fw_sample_requested before GLB_PERFCNT_SAMPLE is toggled, or lose a sample request when the worker has already advanced perfcnt_extract?
CPU0 (ioctl) CPU1 (IRQ)perf_fw_request_sample()
lock(&perf->pend_lock)
perf->fw_sample_requested = true
unlock(&perf->pend_lock)
panthor_perf_report_irq()
/* GLB_PING / unrelated IRQ */
/* req & SAMPLE == ack & SAMPLE */
lock(&perf->pend_lock)
perf->fw_sample_requested = false
unlock(&perf->pend_lock) panthor_fw_toggle_reqs(...,
GLB_PERFCNT_SAMPLE)
Because perf_fw_request_sample() drops perf->pend_lock before calling panthor_fw_toggle_reqs(), any unrelated JOB_INT_GLOBAL_IF interrupt firing in that window sees sample_acked == true and clears perf->fw_sample_requested while the request is still in flight; a second perf_fw_request_sample() then toggles GLB_PERFCNT_SAMPLE back to match ack, dropping the request. Conversely, if an already-running panthor_perf_handle_fw_events() worker consumes the new sample, advances perfcnt_extract to insert_idx, and wakes session_start() before panthor_perf_report_irq() runs, an immediate follow-up session_stop() or session_sample() sees perf->fw_sample_requested == true and skips toggling GLB_PERFCNT_SAMPLE, after which panthor_perf_report_irq() sees insert_idx == extract_idx, clears perf->fw_sample_requested, and never queues sample_work.
[ ... ]
drivers/gpu/drm/panthor/panthor_perf.c int panthor_perf_session_create(struct drm_file *file, · line 1498
Quoted 11 lines
drivers/gpu/drm/panthor/panthor_perf.c +1
-0@@ -497,6 +1486,7 @@ int panthor_perf_session_create(struct drm_file *file,kref_init (&session->ref);INIT_LIST_HEAD (&session->sessions); +init_waitqueue_head (&session->state_wq);spin_lock_init (&session->state_lock); session->control = control.vaddr; *session->control = (struct drm_panthor_perf_ringbuf_control ) {0 };@@ -507,6 +1497,14 @@ int panthor_perf_session_create(struct drm_file *file, session->control_buf = control; session->accum_idx =U64_MAX ;
[Severity: High]
What happens if userspace passes a PRIME-imported GEM buffer backed by device IOMEM for control_handle or ringbuf_handle?
panthor_perf_bo_get() maps the GEM object with drm_gem_vmap() without rejecting imported buffers or checking map.is_iomem, and stores only bo->vaddr = map.vaddr:
drivers/gpu/drm/panthor/panthor_perf.c:panthor_perf_bo_get() {
...
ret = drm_gem_vmap(obj, &map);
if (ret)
goto err_put;
bo->obj = obj;
bo->vaddr = map.vaddr;
...
}The sampling path then performs direct CPU memory accesses and smp_load_acquire() / smp_store_release() on session->control and session->samples, and on teardown panthor_perf_bo_put() reconstructs the map with IOSYS_MAP_INIT_VADDR(bo->vaddr) (is_iomem = false), which triggers BUG_ON(!iosys_map_is_equal(&dmabuf->vmap_ptr, map)) in dma_buf_vunmap().
drivers/gpu/drm/panthor/panthor_perf.c int panthor_perf_session_create(struct drm_file *file, · line 1502
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_perf.c +4
-0+ + ret =pm_runtime_resume_and_get (ptdev->base.dev); +if (ret) + goto cleanup_obj;
[Severity: Medium]
Should panthor_perf_session_create() callpanthor_device_resume_and_get(ptdev) instead ofpm_runtime_resume_and_get(ptdev->base.dev)?
When panthor_device_resume() fails, it sets ptdev->pm.recovery_needed = 1; calling pm_runtime_resume_and_get() directly bypasses the pm_runtime_set_suspended() recovery handling in panthor_device_resume_and_get() and leaves dev->power.runtime_error latched.
[ ... ]
Quoted 11 lines
drivers/gpu/drm/panthor/panthor_perf.c +1
-2@@ -657,8 +1674,7 @@ static int session_start(struct panthor_perf *perf, struct panthor_perf_session* stop the session and restart sampling. * * Return: -* - 0 to request a new sample and wait, -* - 1 to skip the re-request and go directly to wait +* - 1 if the caller should wait for the sample to complete, * - negative error code on invalid transitions or state */ static int session_try_sample (struct panthor_perf_session *session,u64 user_data)
drivers/gpu/drm/panthor/panthor_perf.c static int session_sample(struct panthor_perf *perf, struct panthor_perf_session · line 1725
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_perf.c +2
-1@@ -703,7 +1719,8 @@ static int session_sample(struct panthor_perf *perf, struct panthor_perf_sessionif (ret <=0 )return ret; -/* TODO Calls to the FW interface will go here in later patches. */ +perf_fw_request_sample (perf); +return 0 ; }
[Severity: Low]
In session_try_sample(), the initial !session_is_active(session) check returns -EINVAL for all non-active states, making the immediately following if (session_is_stopping(session)) return -EBUSY; check unreachable:
drivers/gpu/drm/panthor/panthor_perf.c:session_try_sample() {
guard(spinlock_irqsave)(&session->state_lock);
if (!session_is_active(session))
return -EINVAL;
if (session_is_stopping(session))
return -EBUSY;
...
}[Severity: Medium]
Does overwriting session->sample_start_ns = ktime_get_raw_ns() in session_try_sample() and session_try_stop() clobber the interval start timestamp recorded in session_emit_sample()?
In session_emit_sample(), the update_session label sets session->sample_start_ns = perf->last_fw_sample_time when the STARTING clearing sample completes or when a pending sample is published. Because session_publish_sample() is only called when session_is_stopping(session) or session->pending_sample is true, and both session_try_stop() and session_try_sample() unconditionally overwrite session->sample_start_ns with ktime_get_raw_ns(), the published sample header's timestamp_start_ns always reflects when the SAMPLE or STOP ioctl was issued rather than the start of the accumulation window.
[ ... ]
Quoted 29 lines
drivers/gpu/drm/panthor/panthor_perf.c +29
-0+void panthor_perf_suspend (struct panthor_device *ptdev) +{ +struct panthor_perf *perf = ptdev->perf; +int ret; + +if (!perf) +return ; + +guard (mutex)(&perf->op_lock); + + ret =perf_disable (perf); + +if (ret) +drm_warn (&ptdev->base,"Could not stop sampling before suspend, err = %d" , ret); +} + +/** +* panthor_perf_resume - Resume the performance counter subsystem after system resumption. +* @ptdev: Panthor device. +* +* Indicate to the performance counters that the system has resumed. This must not be used +* to handle MCU state transitions, for the same reasons as detailed in the kerneldoc for +* @panthor_perf_suspend. +*/ +void panthor_perf_resume (struct panthor_device *ptdev) +{ +struct panthor_perf *perf = ptdev->perf; +int ret; +
drivers/gpu/drm/panthor/panthor_perf.c int panthor_perf_session_sample(struct panthor_file *pfile, u32 handle, u64 user · line 1921
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+if (!perf) +return ; + +guard (mutex)(&perf->op_lock); + + ret =perf_enable (perf); + +if (ret) +drm_warn (&ptdev->base,"Could not resume sampling, err = %d" , ret); +}
[Severity: High]
Does panthor_perf_suspend() (and panthor_perf_pre_reset()) need to flush perf->sample_work after perf_disable(perf), the way panthor_perf_session_destroy() and panthor_perf_pool_destroy() call perf_sync_work(perf)?
CPU0 (reset / suspend-resume) CPU1 (sample_work)panthor_perf_handle_fw_events()
extract_idx = 1, insert_idx = 3panthor_perf_pre_reset()
perf_disable()
...panthor_perf_post_reset()
perf_enable()
panthor_perf_fw_start_sampling()
perfcnt_extract = 0
panthor_perf_handle_fw_samples(1, 3) perfcnt_extract = 3 <-- stale index
Because panthor_perf_sample_worker() does not take perf->op_lock, a pending sample_work can run concurrently with suspend/reset or with panthor_perf_fw_start_sampling() on resume/post-reset and overwrite perfcnt_extract = 0 with a stale pre-reset insert_idx. When the next FW sample arrives at perfcnt_insert = 1, (u32)(insert_idx - extract_idx) wraps around past perf->sample_slots, triggering "Perf FW ring distance exceeds capacity" and dropping all subsequent samples.
[Severity: Medium]
Without draining perf->rb in panthor_perf_suspend() and panthor_perf_pre_reset(), any firmware samples written before perf_disable() completes are also discarded when panthor_perf_fw_start_sampling() resets perfcnt_extract to 0, leaving stale non-zero PANTHOR_CTR_PRFCNT_EN and PANTHOR_CTR_SAMPLE_REASON words in those ringbuffer slots. In addition, if panthor_perf_request_sync() times out because the firmware never acknowledged GLB_PERFCNT_SAMPLE, perf->fw_sample_requested remains stuck at true and the session stays in STARTING or STOPPING, causing retries to skip requesting a sample and panthor_perf_session_destroy() to fail with -EBUSY.
Quoted 21 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+/** * panthor_perf_unplug - Terminate the performance counter subsystem. * @ptdev: Panthor device. @@ -856,10 +1930,27 @@ int panthor_perf_session_sample(struct panthor_file *pfile, u32 handle, u64 uservoid panthor_perf_unplug (struct panthor_device *ptdev) {struct panthor_perf *perf = ptdev->perf; +struct panthor_perf_session *session, *tmp;if (!perf)return ; +disable_work_sync (&perf->sample_work); + + scoped_guard(mutex, &perf->session_lock) { +list_for_each_entry_safe (session, tmp, &perf->session_list, sessions) { + scoped_guard(spinlock_irqsave, &session->state_lock) +session_set_state (session,PANTHOR_PERF_SESSION_DESTROY ); + +list_del_init (&session->sessions);
drivers/gpu/drm/panthor/panthor_perf.c int panthor_perf_session_sample(struct panthor_file *pfile, u32 handle, u64 user · line 1955
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +8
-0+session_put (session); + } + } + +panthor_kernel_bo_vunmap (perf->rb); +panthor_kernel_bo_destroy (perf->rb); + + perf->rb =NULL ; ptdev->perf =NULL ; }
[Severity: High]
Can panthor_perf_unplug() race with concurrent session operations or panthor_perf_pool_destroy() to cause a use-after-free or NULL pointer dereference?
CPU0 CPU1panthor_perf_pool_destroy()
perf = pfile->ptdev->perf
panthor_fw_unplug()
panthor_perf_unplug()
panthor_kernel_bo_destroy(perf->rb)
perf->rb = NULL
ptdev->perf = NULL
/* destroys fw->sections, fw->vm = NULL */
lock(&perf->op_lock)
perf_disable(perf)
/* accesses destroyed glb_iface */
perf_enable(perf)
panthor_perf_fw_start_sampling()
panthor_perf_fw_write_config(ptdev->perf) <-- NULL derefpanthor_perf_unplug() destroys perf->rb and clears ptdev->perf without acquiring perf->op_lock, and panthor_fw_unplug() immediately unmaps all firmware interface sections and clears ptdev->fw->vm. Meanwhile, panthor_perf_session_create() reloads ptdev->perf after blocking allocations, and panthor_perf_pool_destroy() reads pfile->ptdev->perf before taking perf->op_lock. Furthermore, panthor_perf_unplug() sets sessions to PANTHOR_PERF_SESSION_DESTROY without calling wake_up_all(&session->state_wq) (and session_is_active_locked() does not check session_is_destroy(session)), leaving any thread in panthor_perf_request_sync() blocked until timeout.
[Severity: Medium]
Does panthor_perf_unplug() also leak the per-session runtime PM references acquired in panthor_perf_session_create()?
panthor_perf_session_create() calls pm_runtime_resume_and_get() for each session. When panthor_device_unplug() calls panthor_fw_unplug() -> panthor_perf_unplug(), sessions are removed from perf->session_list and ptdev->perf is set to NULL without dropping their runtime PM references. As a result, pm_runtime_put_sync_suspend() at the end of panthor_device_unplug() cannot suspend the device before driver removal finishes, and any subsequent panthor_perf_session_destroy() ioctl returns -EOPNOTSUPP because ptdev->perf is NULL, leaving the session and its runtime PM reference pinned until the DRM file is closed.
Quoted 28 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-1@@ -867,6 +1958,9 @@ int panthor_perf_pool_create(struct panthor_file *pfile) {struct panthor_perf_pool *pool; +if (!pfile->ptdev->perf) +return 0 ; + pool =kzalloc_obj (*pool);if (!pool)return -ENOMEM ;@@ -880,14 +1974,33 @@ int panthor_perf_pool_create(struct panthor_file *pfile)void panthor_perf_pool_destroy (struct panthor_file *pfile) {struct panthor_perf_pool *pool = pfile->sessions; +struct panthor_perf *perf = pfile->ptdev->perf;struct panthor_perf_session *session;unsigned long handle; +bool sync_work = false; + +if (!pool) +return ;xa_for_each_marked (&pool->xa, handle, session,SESSION_REGISTERED ) { - scoped_guard(spinlock_irqsave, &session->state_lock) + scoped_guard(spinlock_irqsave, &session->state_lock) {session_set_state (session,PANTHOR_PERF_SESSION_DESTROY ); + sync_work = true;
drivers/gpu/drm/panthor/panthor_perf.c int panthor_perf_pool_create(struct panthor_file *pfile) · line 1998
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+ } + } + +if (perf && sync_work) { +guard (mutex)(&perf->op_lock); + +perf_disable (perf); +perf_sync_work (perf); +perf_enable (perf); + }
[Severity: Medium]
Because panthor_perf_pool_destroy() is called from panthor_postclose() without drm_dev_enter(), can it race with panthor_device_unplug() -> panthor_fw_unplug() -> panthor_perf_unplug() and touch MMIO doorbells and destroyed firmware sections in perf_disable() and perf_enable() after the device has been unplugged?
Quoted 22 lines
drivers/gpu/drm/panthor/panthor_perf.c +13
-0+ +xa_for_each_marked (&pool->xa, handle, session,SESSION_REGISTERED ) {xa_erase (&pool->xa, handle);session_put (session); +pm_runtime_put_autosuspend (pfile->ptdev->base.dev); }xa_destroy (&pool->xa);@@ -895,3 +2008,23 @@ void panthor_perf_pool_destroy(struct panthor_file *pfile) pfile->sessions =NULL ; } + +void panthor_perf_pre_reset (struct panthor_device *ptdev) +{ +if (!ptdev->perf) +return ; + +guard (mutex)(&ptdev->perf->op_lock); + +perf_disable (ptdev->perf); +}
drivers/gpu/drm/panthor/panthor_perf.c void panthor_perf_pool_destroy(struct panthor_file *pfile) · line 2030
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_perf.c +10
-0+ +void panthor_perf_post_reset (struct panthor_device *ptdev) +{ +if (!ptdev->perf) +return ; + +guard (mutex)(&ptdev->perf->op_lock); + +perf_enable (ptdev->perf); +}
[Severity: Medium]
If a GPU reset occurs while a session has a sample request in flight (session->pending_sample == true, or the session is in STARTING or STOPPING), is that sample request lost across the reset?
During panthor_device_reset_work(), panthor_fw_post_reset() reloads and zeroes the firmware interface sections (clearing GLB_PERFCNT_SAMPLE in req/ack and resetting perfcnt_insert and perfcnt_extract to 0). When the MCU boot JOB_INT_GLOBAL_IF interrupt fires, panthor_perf_report_irq() sees (req & GLB_PERFCNT_SAMPLE) == (ack & GLB_PERFCNT_SAMPLE) with insert_idx == extract_idx == 0 and clears perf->fw_sample_requested = false without queueing sample_work. panthor_perf_post_reset() then calls perf_enable(), which re-enables GLB_PERFCNT_EN but never re-issues perf_fw_request_sample() for sessions with pending requests, leaving session->pending_sample stuck at true so future SAMPLE and STOP calls fail with -EBUSY and DESTROY fails with -EINVAL.
[Severity: Medium]
The same loss of an in-flight sample request happens across system suspend/resume in panthor_perf_resume(): session_sample() releases perf->op_lock immediately after calling perf_fw_request_sample(), and if panthor_perf_suspend() and panthor_perf_resume() run before the sample is delivered, panthor_perf_resume() calls perf_enable() (resetting perfcnt_extract to 0) without re-requesting a firmware sample for sessions with session->pending_sample == true, wedging the session and blocking new PERF_SESSION_CREATE calls with -EBUSY until the file descriptor is closed.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v8 3/3 drm/panthor: Expose performance counter queries and ioctls
In reply to Lukas ZapolskasExpose DEV_QUERY.PERF_INFO and the five performance counter session ioctls with documentation. Introduce a small scope-based wrapper around drm_dev_enter() and drm_dev_exit() to ensure proper cleanup.
--- drivers/gpu/drm/panthor/panthor_drv.c | 128 ++++++++++++++++- include/uapi/drm/panthor_drm.h | 254 +++++++++++++++++++++++++++++++--- 2 files changed, 361 insertions(+), 21 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +126 -2
@@ -7,6 +7,7 @@#include <asm/arch_timer.h> #endif +#include <linux/cleanup.h> #include <linux/list.h> #include <linux/module.h> #include <linux/of_platform.h>
@@ -182,7 +183,8 @@ panthor_get_uobj_array(const struct drm_panthor_obj_array *in, u32 min_stride, PANTHOR_UOBJ_DECL(struct drm_panthor_queue_submit, syncs), \PANTHOR_UOBJ_DECL (struct drm_panthor_queue_create , ringbuf_size), \PANTHOR_UOBJ_DECL (struct drm_panthor_vm_bind_op , syncs), \ - PANTHOR_UOBJ_DECL(struct drm_panthor_bo_sync_op, size)) +PANTHOR_UOBJ_DECL (struct drm_panthor_bo_sync_op , size), \ + PANTHOR_UOBJ_DECL(struct drm_panthor_perf_info, shader_blocks))/** * PANTHOR_UOBJ_SET() - Copy a kernel object to a user object.
@@ -959,6 +961,10 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d args->size =sizeof (ptdev->mmu_info);return 0 ; +case DRM_PANTHOR_DEV_QUERY_PERF_INFO : + args->size =sizeof (ptdev->perf_info); +return 0 ; +default :return -EINVAL ; }
@@ -992,6 +998,9 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct dcase DRM_PANTHOR_DEV_QUERY_MMU_INFO :return PANTHOR_UOBJ_SET (args->pointer, args->size, ptdev->mmu_info); +case DRM_PANTHOR_DEV_QUERY_PERF_INFO : +return PANTHOR_UOBJ_SET (args->pointer, args->size, ptdev->perf_info); +default :return -EINVAL ; }
@@ -1590,6 +1599,110 @@ static int panthor_ioctl_bo_query_info(struct drm_device *ddev, void *data,return 0 ; } +static int panthor_drm_dev_enter (struct drm_device *ddev) +{ +int cookie; + +if (!drm_dev_enter (ddev, &cookie)) +return -ENODEV ; + +return cookie; +} + +static void panthor_drm_dev_exit (int cookie) +{ +if (cookie >=0 ) +drm_dev_exit (cookie); +} + +DEFINE_CLASS (panthor_drm_dev, int,panthor_drm_dev_exit (_T), + panthor_drm_dev_enter(ddev), struct drm_device *ddev) + +static int panthor_ioctl_perf_session_create (struct drm_device *ddev,void *data, +struct drm_file *file) +{ +int ret; +unsigned long sid =0 ; +struct drm_panthor_perf_session_create *args = data; + +CLASS (panthor_drm_dev, cookie)(ddev); +if (cookie <0 ) +return cookie; + +if (args->pad) +return -EINVAL ; + + ret =panthor_perf_session_create (file, args, &sid); +if (ret) +return ret; + +return sid; +} + +static int panthor_ioctl_perf_session_start (struct drm_device *ddev,void *data, +struct drm_file *file) +{ +struct panthor_file *pfile = file->driver_priv; +struct drm_panthor_perf_session_start *args = data; + +CLASS (panthor_drm_dev, cookie)(ddev); +if (cookie <0 ) +return cookie; + +if (args->pad0) +return -EINVAL ; + +return panthor_perf_session_start (pfile, args->handle, args->user_data); +} + +static int panthor_ioctl_perf_session_sample (struct drm_device *ddev,void *data, +struct drm_file *file) +{ +struct panthor_file *pfile = file->driver_priv; +struct drm_panthor_perf_session_sample *args = data; + +CLASS (panthor_drm_dev, cookie)(ddev); +if (cookie <0 ) +return cookie; + +if (args->pad0) +return -EINVAL ; + +return panthor_perf_session_sample (pfile, args->handle, args->user_data); +} + +static int panthor_ioctl_perf_session_stop (struct drm_device *ddev,void *data, +struct drm_file *file) +{ +struct panthor_file *pfile = file->driver_priv; +struct drm_panthor_perf_session_stop *args = data; + +CLASS (panthor_drm_dev, cookie)(ddev); +if (cookie <0 ) +return cookie; + +if (args->pad0) +return -EINVAL ; + +return panthor_perf_session_stop (pfile, args->handle, args->user_data); +} + +static int panthor_ioctl_perf_session_destroy (struct drm_device *ddev,void *data, +struct drm_file *file) +{ +struct panthor_file *pfile = file->driver_priv; +struct drm_panthor_perf_session_destroy *args = data; + +CLASS (panthor_drm_dev, cookie)(ddev); +if (cookie <0 ) +return cookie; + +if (args->pad0) +return -EINVAL ; + +return panthor_perf_session_destroy (pfile, args->handle); +} +static int panthor_open (struct drm_device *ddev,struct drm_file *file) {
@@ -1674,6 +1787,11 @@ static const struct drm_ioctl_desc panthor_drm_driver_ioctls[] = {PANTHOR_IOCTL (SET_USER_MMIO_OFFSET , set_user_mmio_offset,DRM_RENDER_ALLOW ),PANTHOR_IOCTL (BO_SYNC , bo_sync,DRM_RENDER_ALLOW ),PANTHOR_IOCTL (BO_QUERY_INFO , bo_query_info,DRM_RENDER_ALLOW ), +PANTHOR_IOCTL (PERF_SESSION_CREATE , perf_session_create,DRM_RENDER_ALLOW ), +PANTHOR_IOCTL (PERF_SESSION_START , perf_session_start,DRM_RENDER_ALLOW ), +PANTHOR_IOCTL (PERF_SESSION_SAMPLE , perf_session_sample,DRM_RENDER_ALLOW ), +PANTHOR_IOCTL (PERF_SESSION_STOP , perf_session_stop,DRM_RENDER_ALLOW ), + PANTHOR_IOCTL(PERF_SESSION_DESTROY, perf_session_destroy, DRM_RENDER_ALLOW), };static int panthor_mmap (struct file *filp,struct vm_area_struct *vma)
@@ -1794,6 +1912,12 @@ static void panthor_debugfs_init(struct drm_minor *minor)* - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags * - 1.9 - adds DRM_PANTHOR_DEV_QUERY_MMU_INFO query * - adds DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE flag +* - 1.10 - add DRM_PANTHOR_DEV_QUERY_PERF_INFO query +* - adds PERF_SESSION_CREATE ioctl +* - adds PERF_SESSION_START ioctl +* - adds PERF_SESSION_SAMPLE ioctl +* - adds PERF_SESSION_STOP ioctl +* - adds PERF_SESSION_DESTROY ioctl */ static const struct drm_driver panthor_drm_driver = { .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1807,7 +1931,7 @@ static const struct drm_driver panthor_drm_driver = { .name = "panthor", .desc = "Panthor DRM driver", .major = 1, - .minor = 9, + .minor = 10, .gem_prime_import_sg_table = panthor_gem_prime_import_sg_table, .gem_prime_import = panthor_gem_prime_import,
include/uapi/drm/panthor_drm.h +235 -19
341 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v8 3/3] drm/panthor: Expose performance counter queries and ioctls
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/panthor: infinite busy-loop in panthor_perf_handle_fw_events() - [Medium] drm/panthor: race…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/panthor: infinite busy-loop in panthor_perf_handle_fw_events()
- [Medium] drm/panthor: race between panthor_perf_pool_destroy() and unplug
- [Medium] drm/panthor: PRFCNT_EN overwrite in panthor_perf_handle_fw_samples()
--
commit b28f9d76443cda266099b6a812924734c3213c0a
Author: Lukas Zapolskas <[email protected]>
drm/panthor: Expose performance counter queries and ioctls
This commit exposes the DRM_PANTHOR_DEV_QUERY_PERF_INFO device query and the five performance counter session ioctls, and wraps the session ioctls with a drm_dev_enter()/drm_dev_exit() cleanup class.
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_drv.c +0
-0
[ ... ]
Quoted 33 lines
drivers/gpu/drm/panthor/panthor_drv.c +29
-0@@ -1590,6 +1599,110 @@ static int panthor_ioctl_bo_query_info(struct drm_device *ddev, void *data,return 0 ; } +static int panthor_drm_dev_enter (struct drm_device *ddev) +{ +int cookie; + +if (!drm_dev_enter (ddev, &cookie)) +return -ENODEV ; + +return cookie; +} + +static void panthor_drm_dev_exit (int cookie) +{ +if (cookie >=0 ) +drm_dev_exit (cookie); +} + +DEFINE_CLASS (panthor_drm_dev, int,panthor_drm_dev_exit (_T), + panthor_drm_dev_enter(ddev), struct drm_device *ddev) + +static int panthor_ioctl_perf_session_create (struct drm_device *ddev,void *data, +struct drm_file *file) +{ +int ret; +unsigned long sid =0 ; +struct drm_panthor_perf_session_create *args = data; + +CLASS (panthor_drm_dev, cookie)(ddev); +if (cookie <0 ) +return cookie;
drivers/gpu/drm/panthor/panthor_drv.c static int panthor_ioctl_bo_query_info(struct drm_device *ddev, void *data, · line 1640
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10
-0+ +if (args->pad) +return -EINVAL ; + + ret =panthor_perf_session_create (file, args, &sid); +if (ret) +return ret; + +return sid; +}
[Severity: Medium]
Once panthor_ioctl_perf_session_create() registers a session in pfile->sessions, can closing the DRM file race with panthor_device_unplug() and cause a use-after-free or NULL pointer dereference in panthor_perf_pool_destroy()?
The session ioctls guard against device unplug with panthor_drm_dev, but panthor_postclose() -> panthor_perf_pool_destroy() does not call drm_dev_enter(), and panthor_perf_unplug() does not take perf->op_lock:
CPU0 CPU1panthor_postclose()
panthor_perf_pool_destroy()
perf = pfile->ptdev->perf
panthor_device_unplug()
panthor_fw_unplug()
panthor_perf_unplug()
panthor_kernel_bo_destroy(perf->rb)
perf->rb = NULL
ptdev->perf = NULL
/* destroys fw section BOs */
guard(mutex)(&perf->op_lock)
perf_disable(perf)
panthor_perf_fw_stop_sampling()
/* accesses destroyed glb_iface BO */ <-- use-after-free
perf_enable(perf)
panthor_perf_fw_start_sampling()
panthor_perf_fw_write_config(ptdev->perf)
<-- NULL pointer dereferenceQuoted 6 lines
drivers/gpu/drm/panthor/panthor_drv.c +6
-0+ +static int panthor_ioctl_perf_session_start (struct drm_device *ddev,void *data, +struct drm_file *file) +{ +struct panthor_file *pfile = file->driver_priv; +struct drm_panthor_perf_session_start *args = data;
drivers/gpu/drm/panthor/panthor_drv.c static int panthor_ioctl_bo_query_info(struct drm_device *ddev, void *data, · line 1656
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10
-0+ +CLASS (panthor_drm_dev, cookie)(ddev); +if (cookie <0 ) +return cookie; + +if (args->pad0) +return -EINVAL ; + +return panthor_perf_session_start (pfile, args->handle, args->user_data); +}
[Severity: High]
When a session is active or transitioning while a GPU reset is pending, does panthor_perf_handle_fw_events() spin in an unthrottled busy-loop on the WQ_HIGHPRI perf->sample_workq workqueue?
drivers/gpu/drm/panthor/panthor_perf.c:panthor_perf_handle_fw_events() {
...
if (panthor_device_reset_is_pending(ptdev)) {
queue_work(perf->sample_workq, &perf->sample_work);
return;
}
...
}Because perf->sample_work is a non-delayed work item executing on a WQ_HIGHPRI | WQ_UNBOUND workqueue, re-queueing it from inside its own handler while ptdev->reset.pending is set causes it to re-run immediately at high priority without yielding, starving normal-priority workers such as ptdev->reset.wq or spinning until resume if reset.pending was left set by an aborted reset during suspend.
Quoted 6 lines
drivers/gpu/drm/panthor/panthor_drv.c +6
-0+ +static int panthor_ioctl_perf_session_sample (struct drm_device *ddev,void *data, +struct drm_file *file) +{ +struct panthor_file *pfile = file->driver_priv; +struct drm_panthor_perf_session_sample *args = data;
drivers/gpu/drm/panthor/panthor_drv.c static int panthor_ioctl_bo_query_info(struct drm_device *ddev, void *data, · line 1672
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10
-0+ +CLASS (panthor_drm_dev, cookie)(ddev); +if (cookie <0 ) +return cookie; + +if (args->pad0) +return -EINVAL ; + +return panthor_perf_session_sample (pfile, args->handle, args->user_data); +}
[Severity: Medium]
When panthor_perf_handle_fw_samples() accumulates multiple firmware samples in one pass (insert_idx - extract_idx > 1), does overwriting blk->counters[PANTHOR_CTR_PRFCNT_EN] cause session_populate_sample() to leave unrequested counter values in the user ringbuffer?
drivers/gpu/drm/panthor/panthor_perf.c:panthor_perf_handle_fw_samples() {
...
blk->counters[PANTHOR_CTR_TIMESTAMP_LO] = block[PANTHOR_CTR_TIMESTAMP_LO];
blk->counters[PANTHOR_CTR_TIMESTAMP_HI] = block[PANTHOR_CTR_TIMESTAMP_HI];
blk->counters[PANTHOR_CTR_PRFCNT_EN] = block[PANTHOR_CTR_PRFCNT_EN];
...
if (prfcnt_en == 1)
goto advance_block;
for (size_t k = PANTHOR_HEADER_COUNTERS;
k < ptdev->perf_info.counters_per_block; k++)
blk->counters[k] += block[k];
...
}The counter values are summed across all samples in [extract_idx, insert_idx), but PANTHOR_CTR_PRFCNT_EN is overwritten with the last non-zero prfcnt_en instead of being bitwise-ORed. Because session_populate_sample() copies the entire accumulated perf_sample into the user-mapped session BO with memcpy() and only zeroes out unrequested counters that are set in the final PANTHOR_CTR_PRFCNT_EN mask, any counter group enabled in an earlier sample of the batch but not the last one is not cleared.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3