A v3d core is able to expose a single set of HW performance counters, so at any moment at most one perfmon can be programmed in HW. Currently, the driver tracks the active perfmon with v3d_dev->active_perfmon, but three long-standing issues makes perfmon handling unreliable:
1. The active_perfmon pointer is accessed lock-free from scheduler callbacks, the GPU-reset path and the perfmon ioctls. Note that the v3d_perfmon->lock mutex serialized start/stop of one perfmon object against itself, but the invariant that needs protection is device-wide. 2. perfmon start/stop is hooked exclusively to run_job() callbacks via v3d_switch_perfmon(). If nothing is queued behind a perfmon-carrying job, the perfmon is never actually stopped. 3. A non-global perfmon should count events generated by a specific submission, but the scheduler can run jobs from different queues concurrently. Without explicit cross-queue serialization, an unrelated job running in parallel pollutes the counters and produces unusable results. This series aims to address all three issues. PATCH 1 moves the locking to where the invariant actually lives (fixing issue #1) and replaces the sleeping mutex with a spinlock, which allows us to stop the perfmon from the IRQ handler at job-completion time (the natural boundary for "active perfmon follows the active job") and fixes issue #2. PATCH 2 addresses issue #3 by building on the new locking to enforce cross-queue serialization when a non-global perfmon is attached, by adding scheduler fence dependencies during submission. The fence dependencies allow us to enforce two rules: 1. A job that carries a non-global perfmon waits for every job currently in-flight across all HW queues to finish. 2. While such a job is in-flight, any subsequently submitted job waits for it. This allows us to ensure cross-queue isolation and the reliability of the performance counter values. To make sure that this series actually produces the expected results and improves the overall reliability of v3d's performance monitors, this series is accompanied by a IGT series [1], which was already merged. [1] https://lore.kernel.org/igt-dev/[email protected]/T/ Best regards, - Maíra --- v1 -> v2: https://lore.kernel.org/r/[email protected] - Rebased on top of "[PATCH v2 00/14] drm/v3d: Scheduler and submission fixes and refactoring" - [1/4] NEW PATCH: "drm/v3d: Fix global performance monitor reference counting" - Minimal patch for stable branches only fixing the reference leaks in global perfmons. - [2/4] Start/stop the global perfmon inside the set_global_perfmon ioctl and simplify global perfmon management across the helpers (Iago Toral) - In the reset path, before stopping the perfmon for the HW reset, v3d_reset() now re-arms the global perfmon with v3d_perfmon_resume(), as the global perfmon's start/stop points live only in the IOCTL. - v3d_perfmon_get_values_ioctl() no longer stops the perfmon, it only captures the values. Lifecycle management is left to the job (per-job perfmons) or the SET_GLOBAL ioctl (global perfmon). - [2/4] In v3d_perfmon_delete(), first, stop the perfmon and then, check if it's a global perfmon (Iago Toral) - [2/4] Add some comments to explain the refcount logic for global perfmons (Iago Toral) - [3/4] Move the job->queue introduction to this patch instead of the previous one. - [4/4] NEW PATCH: "drm/v3d: Drop the queue argument from v3d_job_add_syncobjs()" v2 -> v3: https://lore.kernel.org/r/[email protected] - Rebased on top of drm-misc-next. - "[PATCH v2 1/4] drm/v3d: Fix global performance monitor reference counting" was applied to drm-misc-fixes. - "[PATCH v2 4/4] drm/v3d: Drop the queue argument from v3d_job_add_syncobjs()" was no longer needed after "[PATCH v4 00/12] drm/v3d: Scheduler and submission fixes and refactoring" was applied to drm-misc-next. - [1/2, 2/2] Add Iago's R-b (Iago Toral) --- Maíra Canal (2): drm/v3d: Refactor perfmon locking drm/v3d: Serialize jobs across queues when a perfmon is attached drivers/gpu/drm/v3d/v3d_drv.h | 47 ++++++++-- drivers/gpu/drm/v3d/v3d_gem.c | 7 +- drivers/gpu/drm/v3d/v3d_irq.c | 7 +- drivers/gpu/drm/v3d/v3d_perfmon.c | 189 ++++++++++++++++++++++++++++---------- drivers/gpu/drm/v3d/v3d_power.c | 4 + drivers/gpu/drm/v3d/v3d_sched.c | 26 +----- drivers/gpu/drm/v3d/v3d_submit.c | 68 +++++++++++++- 7 files changed, 261 insertions(+), 87 deletions(-) --- base-commit: 903f8773ee96c5dc5fb9aec65227f39fd3e7a1dc change-id: 20260505-v3d-perfmon-lifetime-48c9ded1091b
