Rework panthor's cache flush and soft reset locking
Subsystems (4)
v2 cover Rework panthor's cache flush and soft reset locking
There are two problems in panthor's cache flushing/soft reset code related to locking.
The first problem is that the spinlocks are taken with _irqsave, even though the contended lock is never acquired in a raw interrupt handler. Only a threaded handler locks it.
The second problem is that the wait_event condition references pending_reqs without holding the required lock. This means there's a data race.
While resolving the first problem is easy, the second problem requires adding some more macro variants to wait.h, which is done in the first patch.
New tracepoints to debug cache flushing duration without initial locking waits is thrown in for good measure as well, to complement what's in lock_stat and what the function tracer can already do.
---
Changes in v2:
- Use trace event template for the two tracepoints (ty Steven Rostedt)
- Link to v1: https://patch.msgid.link/[email protected]
---
Nicolas Frattaroli (3):
wait: Introduce non-irq variants of wait_event_lock_timeout drm/panthor: Revisit reqs_lock handling in flush/reset paths drm/panthor: Add tracepoints for cache flushing
drivers/gpu/drm/panthor/panthor_gpu.c | 28 +++++------ drivers/gpu/drm/panthor/panthor_trace.h | 49 +++++++++++++++++++ include/linux/wait.h | 84 +++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 14 deletions(-)
---
base-commit: 5e24f68d311764bf343f9def49b752b509dfd5fd
change-id: 20260728-panthor-cache-flush-fix-b36cb15f92c3
Best regards,
Signature · 1 line
Nicolas Frattaroli <[email protected]>
PATCH v2 1/3 wait: Introduce non-irq variants of wait_event_lock_timeout
For locks that are never acquired in an interrupt context, the non-irq variants for spin_lock()/spin_unlock() are more efficient. However, so far, wait.h only provided wait_event_lock_irq_timeout and wait_event_interruptible_lock_irq_timeout, both of which use spin_lock_irq()/spin_unlock_irq().
Rectify this by adding new non-irq variants of these two macros.
--- include/linux/wait.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+)
include/linux/wait.h +84 -0
@@ -1174,6 +1174,13 @@ do { \ __ret =schedule_timeout (__ret); \ spin_lock_irq(&lock)); +#define __wait_event_lock_timeout (wq_head, condition, lock, timeout, state) \ + ___wait_event(wq_head, ___wait_cond_timeout(condition), \ + state, 0, timeout, \ + spin_unlock(&lock); \ + __ret = schedule_timeout(__ret); \ + spin_lock(&lock)) +/** * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets * true or a timeout elapses. The condition is checked under
@@ -1219,6 +1226,83 @@ do { \ __ret; \ }) +/** +* wait_event_interruptible_lock_timeout - sleep until a condition gets +* true or a timeout elapses. The condition is checked under +* the lock with IRQs enabled. This is expected to be called +* with the lock taken. +* @wq_head: the waitqueue to wait on +* @condition: a C expression for the event to wait for +* @lock: a spinlock_t that has been locked with spin_lock(), which will be +* released before schedule() and reacquired afterwards. +* @timeout: timeout, in jiffies +* +* The process is put to sleep (TASK_INTERRUPTIBLE) until the +* @condition evaluates to true or signal is received. The @condition is +* checked each time the waitqueue @wq_head is woken up. +* +* wake_up() has to be called after changing any variable that could +* change the result of the wait condition. +* +* This is supposed to be called while holding the lock. The lock is +* dropped before going to sleep using spin_unlock() and is reacquired +* using spin_lock() afterwards, but without globally disabling IRQs. +* This is in contrast to wait_event_interruptible_lock_irq_timeout(), +* which uses the spin_lock_irq() and spin_unlock_irq() functions instead. +* +* The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it +* was interrupted by a signal, and the remaining jiffies otherwise +* if the condition evaluated to true before the timeout elapsed. +*/ +#define wait_event_interruptible_lock_timeout (wq_head, condition, lock, \ + timeout) \ +({ \ + long __ret = timeout; \ + if (!___wait_cond_timeout(condition)) \ + __ret = __wait_event_lock_timeout( \ + wq_head, condition, lock, timeout, \ + TASK_INTERRUPTIBLE); \ + __ret; \ +}) + +/** +* wait_event_lock_timeout - sleep until a condition gets true or a timeout +* elapses. The condition is checked under the lock with IRQs +* enabled. This is expected to be called with the lock taken. +* @wq_head: the waitqueue to wait on +* @condition: a C expression for the event to wait for +* @lock: a spinlock_t that has been locked with spin_lock(), which will be +* released before schedule() and reacquired afterwards. +* @timeout: timeout, in jiffies +* +* The process is put to sleep (TASK_UNINTERRUPTIBLE) until the +* @condition evaluates to true or signal is received. The @condition is +* checked each time the waitqueue @wq_head is woken up. +* +* wake_up() has to be called after changing any variable that could +* change the result of the wait condition. +* +* This is supposed to be called while holding the lock. The lock is +* dropped before going to sleep using spin_unlock() and is reacquired +* using spin_lock() afterwards, but without globally disabling IRQs. +* This is in contrast to wait_event_lock_irq_timeout(), which uses the +* spin_lock_irq() and spin_unlock_irq() functions instead. Compared to +* wait_event_interruptible_lock_timeout(), it uses the %TASK_UNINTERRUPTIBLE +* instead of allowing the task to be interrupted. +* +* The function returns 0 if the @timeout elapsed, or the remaining jiffies +* otherwise if the condition evaluated to true before the timeout elapsed. +*/ +#define wait_event_lock_timeout (wq_head, condition, lock, timeout) \ +({ \ + long __ret = timeout; \ + if (!___wait_cond_timeout(condition)) \ + __ret = __wait_event_lock_timeout( \ + wq_head, condition, lock, timeout, \ + TASK_UNINTERRUPTIBLE); \ + __ret; \ +}) +/* * Waitqueues which are removed from the waitqueue_head at wakeup time */
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v2 1/3] wait: Introduce non-irq variants of wait_event_lock_timeout
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The kerneldoc comment for wait_event_lock_timeout incorrectly states that the process can be…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The kerneldoc comment for wait_event_lock_timeout incorrectly states that the process can be awakened by a signal. --
--- Patch [1]: [PATCH v2 1/3] wait: Introduce non-irq variants of wait_event_lock_timeout --- commit a625303b88e13551964ad465fcf368350aa20d46
Author: Nicolas Frattaroli <[email protected]>
wait: Introduce non-irq variants of wait_event_lock_timeout
This commit introduces non-irq variants of wait_event_lock_timeout and wait_event_interruptible_lock_timeout. These macros are optimized for spinlocks that are never acquired in an interrupt context, avoiding the overhead of globally disabling IRQs.
Quoted 4 lines
include/linux/wait.h +0
-0
[ ... ]
Quoted 5 lines
+/**
+ *wait_event_lock_timeout- sleep until a condition gets true or a timeout
+ * elapses. The condition is checked under the lock with IRQs
+ * enabled. This is expected to be called with the lock taken.
+ * @wq_head: the waitqueue to wait on
Quoted 8 lines
+ * @condition: a C expression for the event to wait for
+ * @lock: aspinlock_tthat has been locked withspin_lock(), which will be
+ * released beforeschedule()and reacquired afterwards.
+ * @timeout: timeout, in jiffies
+ *
+ * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
+ * @condition evaluates to true or signal is received. The @condition is
+ * checked each time the waitqueue @wq_head is woken up.
[Severity: Low]
This isn't a bug, but is the mention of a signal being received accurate here?
Since wait_event_lock_timeout places the task in TASK_UNINTERRUPTIBLE state, it should not be awakened by signals. It looks like this sentence might have been copied from the interruptible variant above it.
Quoted 3 lines
+ *
+ *wake_up()has to be called after changing any variable that could
+ * change the result of the wait condition.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2 1/3] wait: Introduce non-irq variants of wait_event_lock_timeout
In reply to Nicolas FrattaroliQuoted 9 lines · Nicolas Frattaroli
For locks that are never acquired in an interrupt context, the non-irq variants for spin_lock()/spin_unlock() are more efficient. However, so far,
wait.honly providedwait_event_lock_irq_timeoutandwait_event_interruptible_lock_irq_timeout, both of which use spin_lock_irq()/spin_unlock_irq().Rectify this by adding new non-irq variants of these two macros.
Signed-off-by Nicolas Frattaroli <[email protected]>
Best regards,
Liviu
Quoted 110 lines
--- include/linux/wait.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+)include/linux/wait.h +84
-0@@ -1174,6 +1174,13 @@ do { \ __ret =schedule_timeout (__ret); \ spin_lock_irq(&lock)); +#define __wait_event_lock_timeout (wq_head, condition, lock, timeout, state) \ + ___wait_event(wq_head, ___wait_cond_timeout(condition), \ + state, 0, timeout, \ + spin_unlock(&lock); \ + __ret = schedule_timeout(__ret); \ + spin_lock(&lock)) +/** * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets * true or a timeout elapses. The condition is checked under @@ -1219,6 +1226,83 @@ do { \ __ret; \ }) +/** +* wait_event_interruptible_lock_timeout - sleep until a condition gets +* true or a timeout elapses. The condition is checked under +* the lock with IRQs enabled. This is expected to be called +* with the lock taken. +* @wq_head: the waitqueue to wait on +* @condition: a C expression for the event to wait for +* @lock: a spinlock_t that has been locked with spin_lock(), which will be +* released before schedule() and reacquired afterwards. +* @timeout: timeout, in jiffies +* +* The process is put to sleep (TASK_INTERRUPTIBLE) until the +* @condition evaluates to true or signal is received. The @condition is +* checked each time the waitqueue @wq_head is woken up. +* +* wake_up() has to be called after changing any variable that could +* change the result of the wait condition. +* +* This is supposed to be called while holding the lock. The lock is +* dropped before going to sleep using spin_unlock() and is reacquired +* using spin_lock() afterwards, but without globally disabling IRQs. +* This is in contrast to wait_event_interruptible_lock_irq_timeout(), +* which uses the spin_lock_irq() and spin_unlock_irq() functions instead. +* +* The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it +* was interrupted by a signal, and the remaining jiffies otherwise +* if the condition evaluated to true before the timeout elapsed. +*/ +#define wait_event_interruptible_lock_timeout (wq_head, condition, lock, \ + timeout) \ +({ \ + long __ret = timeout; \ + if (!___wait_cond_timeout(condition)) \ + __ret = __wait_event_lock_timeout( \ + wq_head, condition, lock, timeout, \ + TASK_INTERRUPTIBLE); \ + __ret; \ +}) + +/** +* wait_event_lock_timeout - sleep until a condition gets true or a timeout +* elapses. The condition is checked under the lock with IRQs +* enabled. This is expected to be called with the lock taken. +* @wq_head: the waitqueue to wait on +* @condition: a C expression for the event to wait for +* @lock: a spinlock_t that has been locked with spin_lock(), which will be +* released before schedule() and reacquired afterwards. +* @timeout: timeout, in jiffies +* +* The process is put to sleep (TASK_UNINTERRUPTIBLE) until the +* @condition evaluates to true or signal is received. The @condition is +* checked each time the waitqueue @wq_head is woken up. +* +* wake_up() has to be called after changing any variable that could +* change the result of the wait condition. +* +* This is supposed to be called while holding the lock. The lock is +* dropped before going to sleep using spin_unlock() and is reacquired +* using spin_lock() afterwards, but without globally disabling IRQs. +* This is in contrast to wait_event_lock_irq_timeout(), which uses the +* spin_lock_irq() and spin_unlock_irq() functions instead. Compared to +* wait_event_interruptible_lock_timeout(), it uses the %TASK_UNINTERRUPTIBLE +* instead of allowing the task to be interrupted. +* +* The function returns 0 if the @timeout elapsed, or the remaining jiffies +* otherwise if the condition evaluated to true before the timeout elapsed. +*/ +#define wait_event_lock_timeout (wq_head, condition, lock, timeout) \ +({ \ + long __ret = timeout; \ + if (!___wait_cond_timeout(condition)) \ + __ret = __wait_event_lock_timeout( \ + wq_head, condition, lock, timeout, \ + TASK_UNINTERRUPTIBLE); \ + __ret; \ +}) +/* * Waitqueues which are removed from the waitqueue_head at wakeup time */ Signature · 2 lines
-- 2.55.0
Signature · 8 lines
====================
| I would like to |
| fix the world, |
| but they're not |
| giving me the |
\ source code! /
---------------
¯\_(ツ)_/¯Re: [PATCH v2 1/3] wait: Introduce non-irq variants of wait_event_lock_timeout
In reply to Nicolas FrattaroliQuoted 9 lines · Nicolas Frattaroli
For locks that are never acquired in an interrupt context, the non-irq variants for spin_lock()/spin_unlock() are more efficient. However, so far,
wait.honly providedwait_event_lock_irq_timeoutandwait_event_interruptible_lock_irq_timeout, both of which use spin_lock_irq()/spin_unlock_irq().Rectify this by adding new non-irq variants of these two macros.
Signed-off-by Nicolas Frattaroli <[email protected]>
There's a documentation bug below...
Quoted 67 lines
--- include/linux/wait.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+)include/linux/wait.h +48
-0@@ -1174,6 +1174,13 @@ do { \ __ret =schedule_timeout (__ret); \ spin_lock_irq(&lock)); +#define __wait_event_lock_timeout (wq_head, condition, lock, timeout, state) \ + ___wait_event(wq_head, ___wait_cond_timeout(condition), \ + state, 0, timeout, \ + spin_unlock(&lock); \ + __ret = schedule_timeout(__ret); \ + spin_lock(&lock)) +/** * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets * true or a timeout elapses. The condition is checked under @@ -1219,6 +1226,83 @@ do { \ __ret; \ }) +/** +* wait_event_interruptible_lock_timeout - sleep until a condition gets +* true or a timeout elapses. The condition is checked under +* the lock with IRQs enabled. This is expected to be called +* with the lock taken. +* @wq_head: the waitqueue to wait on +* @condition: a C expression for the event to wait for +* @lock: a spinlock_t that has been locked with spin_lock(), which will be +* released before schedule() and reacquired afterwards. +* @timeout: timeout, in jiffies +* +* The process is put to sleep (TASK_INTERRUPTIBLE) until the +* @condition evaluates to true or signal is received. The @condition is +* checked each time the waitqueue @wq_head is woken up. +* +* wake_up() has to be called after changing any variable that could +* change the result of the wait condition. +* +* This is supposed to be called while holding the lock. The lock is +* dropped before going to sleep using spin_unlock() and is reacquired +* using spin_lock() afterwards, but without globally disabling IRQs. +* This is in contrast to wait_event_interruptible_lock_irq_timeout(), +* which uses the spin_lock_irq() and spin_unlock_irq() functions instead. +* +* The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it +* was interrupted by a signal, and the remaining jiffies otherwise +* if the condition evaluated to true before the timeout elapsed. +*/ +#define wait_event_interruptible_lock_timeout (wq_head, condition, lock, \ + timeout) \ +({ \ + long __ret = timeout; \ + if (!___wait_cond_timeout(condition)) \ + __ret = __wait_event_lock_timeout( \ + wq_head, condition, lock, timeout, \ + TASK_INTERRUPTIBLE); \ + __ret; \ +}) + +/** +* wait_event_lock_timeout - sleep until a condition gets true or a timeout
include/linux/wait.h do { \ · line 1279
Quoted 10 lines
include/linux/wait.h +10
-0+* elapses. The condition is checked under the lock with IRQs +* enabled. This is expected to be called with the lock taken. +* @wq_head: the waitqueue to wait on +* @condition: a C expression for the event to wait for +* @lock: a spinlock_t that has been locked with spin_lock(), which will be +* released before schedule() and reacquired afterwards. +* @timeout: timeout, in jiffies +* +* The process is put to sleep (TASK_UNINTERRUPTIBLE) until the +* @condition evaluates to true or signal is received. The @condition is
^^^^^^^^^^^^^^^^^^^^^
UNINTERRUPTIBLE means that a signal doesn't wake it - that text should be removed.
With that fixed:
Thanks,
Steve
Quoted 30 lines
include/linux/wait.h +26
-0+* checked each time the waitqueue @wq_head is woken up. +* +* wake_up() has to be called after changing any variable that could +* change the result of the wait condition. +* +* This is supposed to be called while holding the lock. The lock is +* dropped before going to sleep using spin_unlock() and is reacquired +* using spin_lock() afterwards, but without globally disabling IRQs. +* This is in contrast to wait_event_lock_irq_timeout(), which uses the +* spin_lock_irq() and spin_unlock_irq() functions instead. Compared to +* wait_event_interruptible_lock_timeout(), it uses the %TASK_UNINTERRUPTIBLE +* instead of allowing the task to be interrupted. +* +* The function returns 0 if the @timeout elapsed, or the remaining jiffies +* otherwise if the condition evaluated to true before the timeout elapsed. +*/ +#define wait_event_lock_timeout (wq_head, condition, lock, timeout) \ +({ \ + long __ret = timeout; \ + if (!___wait_cond_timeout(condition)) \ + __ret = __wait_event_lock_timeout( \ + wq_head, condition, lock, timeout, \ + TASK_UNINTERRUPTIBLE); \ + __ret; \ +}) +/* * Waitqueues which are removed from the waitqueue_head at wakeup time */
PATCH v2 2/3 drm/panthor: Revisit reqs_lock handling in flush/reset paths
In reply to Nicolas Frattarolipanthor_gpu_flush_caches() and panthor_gpu_soft_reset() would read (and even reset) the contents of the pending_reqs register outside of holding the reqs_lock. Additionally, when it did hold the lock, it did so with the irqsave/irqrestore variants, even though the spinlock was never acquired in an atomic context, just the threaded handler.
Use the new wait_event_lock_timeout() macro to check pending_reqs under the lock, and only do so without disabling interrupts.
--- drivers/gpu/drm/panthor/panthor_gpu.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
drivers/gpu/drm/panthor/panthor_gpu.c +11 -14
@@ -330,35 +330,34 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, u32 l2, u32 lsc, u32 other) {struct panthor_gpu *gpu = ptdev->gpu; -unsigned long flags;int ret =0 ;/* Serialize cache flush operations. */ guard (mutex)(&ptdev->gpu->cache_flush_lock); -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); +spin_lock (&ptdev->gpu->reqs_lock);if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other)); }else { ret = -EIO ; } -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); -if (ret) +if (ret) { +spin_unlock (&ptdev->gpu->reqs_lock);return ret; + } -if (!wait_event_timeout (ptdev->gpu->reqs_acked, +if (!wait_event_lock_timeout (ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED ), -msecs_to_jiffies (100 ))) { -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); + ptdev->gpu->reqs_lock,msecs_to_jiffies (100 ))) {if ((ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED ) !=0 && !(gpu_read (gpu->irq.iomem,INT_RAWSTAT ) &GPU_IRQ_CLEAN_CACHES_COMPLETED )) ret = -ETIMEDOUT ;else ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED ; -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); } +spin_unlock (&ptdev->gpu->reqs_lock);if (ret) {panthor_device_schedule_reset (ptdev);
@@ -378,27 +377,25 @@ int panthor_gpu_soft_reset(struct panthor_device *ptdev) {struct panthor_gpu *gpu = ptdev->gpu;bool timedout = false; -unsigned long flags; -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); +guard (spinlock)(&ptdev->gpu->reqs_lock); +if (!drm_WARN_ON (&ptdev->base, ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_RESET_COMPLETED ;gpu_write (gpu->irq.iomem,INT_CLEAR ,GPU_IRQ_RESET_COMPLETED );gpu_write (gpu->iomem,GPU_CMD ,GPU_SOFT_RESET ); } -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); -if (!wait_event_timeout (ptdev->gpu->reqs_acked, +if (!wait_event_lock_timeout (ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED ), + ptdev->gpu->reqs_lock,msecs_to_jiffies (100 ))) { -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags);if ((ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED ) !=0 && !(gpu_read (gpu->irq.iomem,INT_RAWSTAT ) &GPU_IRQ_RESET_COMPLETED )) timedout = true;else ptdev->gpu->pending_reqs &= ~GPU_IRQ_RESET_COMPLETED ; -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); }if (timedout) {
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v2 2/3] drm/panthor: Revisit reqs_lock handling in flush/reset paths
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Race condition between the threaded IRQ handler and the wait timeout logic causes spurious GPU resets. --
--- Patch [2]: [PATCH v2 2/3] drm/panthor: Revisit reqs_lock handling in flush/reset paths --- commit ec14154f1ff35bca85396f0f6f71d7d7b1d6eb6d
Author: Nicolas Frattaroli <[email protected]>
drm/panthor: Revisit reqs_lock handling in flush/reset paths
The patch updates the handling of reqs_lock in panthor_gpu_flush_caches() and panthor_gpu_soft_reset(). It replaces wait_event_timeout() with wait_event_lock_timeout(), and uses spin_lock() instead of spin_lock_irqsave() since the lock is not acquired in an atomic context.
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_gpu.c +0
-0@@ -329,35 +329,34 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,
[ ... ]
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 356
Quoted 9 lines
drivers/gpu/drm/panthor/panthor_gpu.c +2
-3-if (!wait_event_timeout (ptdev->gpu->reqs_acked, +if (!wait_event_lock_timeout (ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED ), -msecs_to_jiffies (100 ))) { -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); + ptdev->gpu->reqs_lock,msecs_to_jiffies (100 ))) {if ((ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED ) !=0 && !(gpu_read (gpu->irq.iomem,INT_RAWSTAT ) &GPU_IRQ_CLEAN_CACHES_COMPLETED )) ret = -ETIMEDOUT ;
[Severity: High]
This isn't a bug introduced by this patch, but is there a race condition here with the threaded IRQ handler?
If panthor_gpu_irq_handler() clears the hardware interrupt status via INT_CLEAR before acquiring reqs_lock, it can be preempted:
gpu_write(gpu->irq.iomem, INT_CLEAR, status);
// ... preemptible window ...
spin_lock(&ptdev->gpu->reqs_lock);
if (status & ptdev->gpu->pending_reqs) {If the 100ms timeout expires during this precise window, the fallback condition above evaluates under reqs_lock.
It finds that pending_reqs still indicates the request is pending, but checking INT_RAWSTAT shows the interrupt is not pending (because it was just cleared by the preempted thread).
Could this cause the wait logic to falsely conclude the hardware timed out, return -ETIMEDOUT, and schedule a spurious full GPU reset?
[ ... ]
Quoted 1 line
drivers/gpu/drm/panthor/panthor_gpu.c +0
-0@@ -377,27 +376,25 @@ int panthor_gpu_soft_reset(struct panthor_device *ptdev)
[ ... ]
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_soft_reset(struct panthor_device *ptdev) · line 396
Quoted 9 lines
drivers/gpu/drm/panthor/panthor_gpu.c +2
-2-if (!wait_event_timeout (ptdev->gpu->reqs_acked, +if (!wait_event_lock_timeout (ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED ), + ptdev->gpu->reqs_lock,msecs_to_jiffies (100 ))) { -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags);if ((ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED ) !=0 && !(gpu_read (gpu->irq.iomem,INT_RAWSTAT ) &GPU_IRQ_RESET_COMPLETED )) timedout = true;
[Severity: High]
This is a pre-existing issue, but does this suffer from the same race condition with the threaded IRQ handler as the flush caches timeout logic above?
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_gpu.c +0
-1else ptdev->gpu->pending_reqs &= ~GPU_IRQ_RESET_COMPLETED ; -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v2 2/3] drm/panthor: Revisit reqs_lock handling in flush/reset paths
In reply to Nicolas FrattaroliQuoted 11 lines · Nicolas Frattaroli
panthor_gpu_flush_caches()andpanthor_gpu_soft_reset()would read (and even reset) the contents of thepending_reqsregister outside of holding thereqs_lock. Additionally, when it did hold the lock, it did so with the irqsave/irqrestore variants, even though the spinlock was never acquired in an atomic context, just the threaded handler.Use the new
wait_event_lock_timeout()macro to checkpending_reqsunder the lock, and only do so without disabling interrupts.Fixes 5cd894e258c4 ("drm/panthor: Add the GPU logical block")Signed-off-by Nicolas Frattaroli <[email protected]>
Best regards,
Liviu
Quoted 87 lines
--- drivers/gpu/drm/panthor/panthor_gpu.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)drivers/gpu/drm/panthor/panthor_gpu.c +11
-14@@ -330,35 +330,34 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, u32 l2, u32 lsc, u32 other) {struct panthor_gpu *gpu = ptdev->gpu; -unsigned long flags;int ret =0 ;/* Serialize cache flush operations. */ guard (mutex)(&ptdev->gpu->cache_flush_lock); -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); +spin_lock (&ptdev->gpu->reqs_lock);if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other)); }else { ret = -EIO ; } -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); -if (ret) +if (ret) { +spin_unlock (&ptdev->gpu->reqs_lock);return ret; + } -if (!wait_event_timeout (ptdev->gpu->reqs_acked, +if (!wait_event_lock_timeout (ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED ), -msecs_to_jiffies (100 ))) { -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); + ptdev->gpu->reqs_lock,msecs_to_jiffies (100 ))) {if ((ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED ) !=0 && !(gpu_read (gpu->irq.iomem,INT_RAWSTAT ) &GPU_IRQ_CLEAN_CACHES_COMPLETED )) ret = -ETIMEDOUT ;else ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED ; -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); } +spin_unlock (&ptdev->gpu->reqs_lock);if (ret) {panthor_device_schedule_reset (ptdev);@@ -378,27 +377,25 @@ int panthor_gpu_soft_reset(struct panthor_device *ptdev) {struct panthor_gpu *gpu = ptdev->gpu;bool timedout = false; -unsigned long flags; -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); +guard (spinlock)(&ptdev->gpu->reqs_lock); +if (!drm_WARN_ON (&ptdev->base, ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_RESET_COMPLETED ;gpu_write (gpu->irq.iomem,INT_CLEAR ,GPU_IRQ_RESET_COMPLETED );gpu_write (gpu->iomem,GPU_CMD ,GPU_SOFT_RESET ); } -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); -if (!wait_event_timeout (ptdev->gpu->reqs_acked, +if (!wait_event_lock_timeout (ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED ), + ptdev->gpu->reqs_lock,msecs_to_jiffies (100 ))) { -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags);if ((ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED ) !=0 && !(gpu_read (gpu->irq.iomem,INT_RAWSTAT ) &GPU_IRQ_RESET_COMPLETED )) timedout = true;else ptdev->gpu->pending_reqs &= ~GPU_IRQ_RESET_COMPLETED ; -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); }if (timedout) {Signature · 2 lines
-- 2.55.0
Signature · 8 lines
====================
| I would like to |
| fix the world, |
| but they're not |
| giving me the |
\ source code! /
---------------
¯\_(ツ)_/¯Re: [PATCH v2 2/3] drm/panthor: Revisit reqs_lock handling in flush/reset paths
In reply to Nicolas FrattaroliQuoted 11 lines · Nicolas Frattaroli
panthor_gpu_flush_caches()andpanthor_gpu_soft_reset()would read (and even reset) the contents of thepending_reqsregister outside of holding thereqs_lock. Additionally, when it did hold the lock, it did so with the irqsave/irqrestore variants, even though the spinlock was never acquired in an atomic context, just the threaded handler.Use the new
wait_event_lock_timeout()macro to checkpending_reqsunder the lock, and only do so without disabling interrupts.Fixes 5cd894e258c4 ("drm/panthor: Add the GPU logical block")Signed-off-by Nicolas Frattaroli <[email protected]>
Quoted 84 lines
--- drivers/gpu/drm/panthor/panthor_gpu.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)drivers/gpu/drm/panthor/panthor_gpu.c +11
-14@@ -330,35 +330,34 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, u32 l2, u32 lsc, u32 other) {struct panthor_gpu *gpu = ptdev->gpu; -unsigned long flags;int ret =0 ;/* Serialize cache flush operations. */ guard (mutex)(&ptdev->gpu->cache_flush_lock); -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); +spin_lock (&ptdev->gpu->reqs_lock);if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other)); }else { ret = -EIO ; } -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); -if (ret) +if (ret) { +spin_unlock (&ptdev->gpu->reqs_lock);return ret; + } -if (!wait_event_timeout (ptdev->gpu->reqs_acked, +if (!wait_event_lock_timeout (ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED ), -msecs_to_jiffies (100 ))) { -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); + ptdev->gpu->reqs_lock,msecs_to_jiffies (100 ))) {if ((ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED ) !=0 && !(gpu_read (gpu->irq.iomem,INT_RAWSTAT ) &GPU_IRQ_CLEAN_CACHES_COMPLETED )) ret = -ETIMEDOUT ;else ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED ; -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); } +spin_unlock (&ptdev->gpu->reqs_lock);if (ret) {panthor_device_schedule_reset (ptdev);@@ -378,27 +377,25 @@ int panthor_gpu_soft_reset(struct panthor_device *ptdev) {struct panthor_gpu *gpu = ptdev->gpu;bool timedout = false; -unsigned long flags; -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); +guard (spinlock)(&ptdev->gpu->reqs_lock); +if (!drm_WARN_ON (&ptdev->base, ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_RESET_COMPLETED ;gpu_write (gpu->irq.iomem,INT_CLEAR ,GPU_IRQ_RESET_COMPLETED );gpu_write (gpu->iomem,GPU_CMD ,GPU_SOFT_RESET ); } -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); -if (!wait_event_timeout (ptdev->gpu->reqs_acked, +if (!wait_event_lock_timeout (ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED ), + ptdev->gpu->reqs_lock,msecs_to_jiffies (100 ))) { -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags);if ((ptdev->gpu->pending_reqs &GPU_IRQ_RESET_COMPLETED ) !=0 && !(gpu_read (gpu->irq.iomem,INT_RAWSTAT ) &GPU_IRQ_RESET_COMPLETED )) timedout = true;else ptdev->gpu->pending_reqs &= ~GPU_IRQ_RESET_COMPLETED ; -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); }if (timedout) {
Boris Brezillon
maintainerRe: [PATCH v2 2/3] drm/panthor: Revisit reqs_lock handling in flush/reset paths
In reply to Nicolas FrattaroliHello Nicolas,
Quoted 3 lines · On Thu, 30 Jul 2026 13:45:15 +0200 Nicolas Frattaroli…
panthor_gpu_flush_caches()andpanthor_gpu_soft_reset()would read (and even reset) the contents of thepending_reqsregister outside of holding thereqs_lock.
Can you elaborate a bit on the race being fixed here? If pending_reqs bits are truly cleared before the wake_up_all() call (which would require a WRITE_ONCE() to be enforced, admittedly), there's no risk for the wait_event() call to do a test before the bits have been updated, and this holds even if the test is done without the lock held.
The other race I could think of is two threads calling panthor_gpu_flush_caches() concurrently, and the second one stealing the FLUSH_COMPLETED event the first thread waits on and re-issuing a second flush on top, thus delaying the completion for the first thread. But that should be covered by the cache_flush_lock.
Quoted 19 lines
Additionally, when it did hold the lock, it did so with the irqsave/irqrestore variants, even though the spinlock was never acquired in an atomic context, just the threaded handler.
Use the new
wait_event_lock_timeout()macro to checkpending_reqsunder the lock, and only do so without disabling interrupts.Fixes 5cd894e258c4 ("drm/panthor: Add the GPU logical block")Signed-off-by Nicolas Frattaroli <[email protected]>--- drivers/gpu/drm/panthor/panthor_gpu.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)drivers/gpu/drm/panthor/panthor_gpu.c +0
-0@@ -330,35 +330,34 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, u32 l2, u32 lsc, u32 other)
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 338
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_gpu.c +1
-2{struct panthor_gpu *gpu = ptdev->gpu; -unsigned long flags;int ret =0 ;/* Serialize cache flush operations. */ guard (mutex)(&ptdev->gpu->cache_flush_lock); -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); +spin_lock (&ptdev->gpu->reqs_lock);
Can we make the _irq{save,restore}-drop its own patch?
Quoted 7 lines
drivers/gpu/drm/panthor/panthor_gpu.c +0
-1if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other)); }else { ret = -EIO ; } -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags);
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 352
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_gpu.c +4
-2-if (ret) +if (ret) { +spin_unlock (&ptdev->gpu->reqs_lock);return ret; + } - if (!wait_event_timeout(ptdev->gpu->reqs_acked, + if (!wait_event_lock_timeout(ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED),
Assuming we really need to do the test with the lock held, could we add a patch at the beginning of the series that fixes the race without depending on the new wait macro, so that we have a version that can easily be backported?
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 360
Quoted 11 lines
drivers/gpu/drm/panthor/panthor_gpu.c +2
-3- msecs_to_jiffies(100))) { -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); + ptdev->gpu->reqs_lock, msecs_to_jiffies(100))) {if ((ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED ) !=0 && !(gpu_read (gpu->irq.iomem,INT_RAWSTAT ) &GPU_IRQ_CLEAN_CACHES_COMPLETED )) ret = -ETIMEDOUT ;else ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED ; -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); } +spin_unlock (&ptdev->gpu->reqs_lock);
I think a scoped_guard() could make things a bit cleaner, and given you already turn the regular lock/unlock sequence into a guard in panthor_gpu_soft_reset(), I'd do that here as well.
Regards,
Boris
[1]https://elixir.bootlin.com/linux/v7.2-rc5/source/drivers/gpu/drm/panthor/panthor_gpu.c#L114
Re: [PATCH v2 2/3] drm/panthor: Revisit reqs_lock handling in flush/reset paths
Quoted 15 lines · On Monday, 3 August 2026 10:53:24 Central European Summer Time Boris Brezillon…
Hello Nicolas,
Quoted 3 lines · On Thu, 30 Jul 2026 13:45:15 +0200 Nicolas Frattaroli…
panthor_gpu_flush_caches()andpanthor_gpu_soft_reset()would read (and even reset) the contents of thepending_reqsregister outside of holding thereqs_lock.Can you elaborate a bit on the race being fixed here? If
pending_reqsbits are truly cleared before thewake_up_all()call (which would require aWRITE_ONCE()to be enforced, admittedly), there's no risk for thewait_event()call to do a test before the bits have been updated, and this holds even if the test is done without the lock held.
Quoted 5 lines
The other race I could think of is two threads calling
panthor_gpu_flush_caches()concurrently, and the second one stealing theFLUSH_COMPLETEDevent the first thread waits on and re-issuing a second flush on top, thus delaying the completion for the first thread. But that should be covered by thecache_flush_lock.
panthor_gpu_flush_caches() is not the only thing that sets/gets pending_reqs. Notably, the threaded interrupt handler does, as well as any other functionality using the same member for reqs tracking (e.g. the soft reset).
Consider the following serialisation of events:
1. T1 asks to flush caches by writing GPU_CMD and setting pending_reqs
2. T1 drops reqs_lock.
3. T2 enters IRQ handler for flush complete, spins lock waiting for reqs_lock
4. T1 sleeps at wait_event_timeout
5. T2 updates pending_reqs and wakes up the waiter in any order, since the effects of those two can't consistently be observed as sequential logic without the outer reqs_lock being held by the observer
6. T1 wakes up, checks pending_reqs, but since pending_reqs is checked without holding any lock, so we implictly depend on the synchronisation point that is the waitqueue's lock rather than the reqs_lock spinlock, which says nothing about whether the pending_reqs change materialised on T1's side yet as far as I can tell?
7. T1 sees that pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED is still != 0, so goes back to sleep for some future wake-up of reqs_acked or a timeout.
I'm not 100% sure, but I think 6. means that the memory model would permit T1 to re-use the pending_reqs it previously set, rather than the updated one set by T2, since there's nothing stopping us from being woken up before the pending_reqs change has made itself known to observers not serialising with the reqs_lock being released by T2 in panthor_gpu_irq_handler after.
If you check lock_stat before the change, you see that the reqs_lock is actually never contended. This isn't a good sign because it means whatever situation it exists to protect against never occurs, so either the lock is pointless or the lock is non-functional.
Quoted 24 lines
Quoted 24 lines
Additionally, when it did hold the lock, it did so with the irqsave/irqrestore variants, even though the spinlock was never acquired in an atomic context, just the threaded handler.
Use the new
wait_event_lock_timeout()macro to checkpending_reqsunder the lock, and only do so without disabling interrupts.Fixes 5cd894e258c4 ("drm/panthor: Add the GPU logical block")Signed-off-by Nicolas Frattaroli <[email protected]>--- drivers/gpu/drm/panthor/panthor_gpu.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)drivers/gpu/drm/panthor/panthor_gpu.c +0
-1@@ -330,35 +330,34 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, u32 l2, u32 lsc, u32 other) {struct panthor_gpu *gpu = ptdev->gpu; -unsigned long flags;int ret =0 ;
Quoted 7 lines
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 338
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_gpu.c +1
-1/* Serialize cache flush operations. */ guard (mutex)(&ptdev->gpu->cache_flush_lock); -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); +spin_lock (&ptdev->gpu->reqs_lock);Can we make the _irq{save,restore}-drop its own patch?
I'm not sure it's fine to drop the IRQ disabling without fixing the read of pending_reqs outside its lock.
Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/panthor/panthor_gpu.c +3
-2if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other)); }else { ret = -EIO ; } -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); -if (ret) +if (ret) { +spin_unlock (&ptdev->gpu->reqs_lock);return ret; + }
Quoted 7 lines
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 352
Quoted 3 lines
drivers/gpu/drm/panthor/panthor_gpu.c +1
-1- if (!wait_event_timeout(ptdev->gpu->reqs_acked, + if (!wait_event_lock_timeout(ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED),Assuming we really need to do the test with the lock held, could we add a patch at the beginning of the series that fixes the race without depending on the new wait macro, so that we have a version that can easily be backported?
It's either backporting the prerequisite new macro or still doing this all with IRQs disabled using the pre-existing wait_event_lock_irq_timeout macro, and the IRQ disabled thing is what caused problems.
Quoted 16 lines
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 360
Quoted 12 lines
drivers/gpu/drm/panthor/panthor_gpu.c +2
-3- msecs_to_jiffies(100))) { -spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); + ptdev->gpu->reqs_lock, msecs_to_jiffies(100))) {if ((ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED ) !=0 && !(gpu_read (gpu->irq.iomem,INT_RAWSTAT ) &GPU_IRQ_CLEAN_CACHES_COMPLETED )) ret = -ETIMEDOUT ;else ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED ; -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); } +spin_unlock (&ptdev->gpu->reqs_lock);I think a
scoped_guard()could make things a bit cleaner, and given you already turn the regular lock/unlock sequence into a guard inpanthor_gpu_soft_reset(), I'd do that here as well.
That would add an additional layer of indentation, which I'm wary of. We can't use a non-scoped guard due to the reset at the end of the function.
I'll see if I can reshuffle the code to make it not as ugly to use a scoped_guard here. Will also slightly change the semantics of the _end tracepoint, since it'll then fire it before dropping the lock, but that's not much of a change.
Kind regards,
Nicolas Frattaroli
Quoted 7 lines
Regards,
Boris
[1]https://elixir.bootlin.com/linux/v7.2-rc5/source/drivers/gpu/drm/panthor/panthor_gpu.c#L114
Re: [PATCH v2 2/3] drm/panthor: Revisit reqs_lock handling in flush/reset paths
Quoted 27 lines · On Monday, 3 August 2026 15:13:25 Central European Summer Time Nicolas…
Quoted 15 lines · On Monday, 3 August 2026 10:53:24 Central European Summer Time Boris Brezillon…
Hello Nicolas,
Quoted 3 lines · On Thu, 30 Jul 2026 13:45:15 +0200 Nicolas Frattaroli…
panthor_gpu_flush_caches()andpanthor_gpu_soft_reset()would read (and even reset) the contents of thepending_reqsregister outside of holding thereqs_lock.Can you elaborate a bit on the race being fixed here? If
pending_reqsbits are truly cleared before thewake_up_all()call (which would require aWRITE_ONCE()to be enforced, admittedly), there's no risk for thewait_event()call to do a test before the bits have been updated, and this holds even if the test is done without the lock held.Quoted 5 lines
The other race I could think of is two threads calling
panthor_gpu_flush_caches()concurrently, and the second one stealing theFLUSH_COMPLETEDevent the first thread waits on and re-issuing a second flush on top, thus delaying the completion for the first thread. But that should be covered by thecache_flush_lock.
panthor_gpu_flush_caches()is not the only thing that sets/getspending_reqs. Notably, the threaded interrupt handler does, as well as any other functionality using the same member for reqs tracking (e.g. the soft reset).
Quoted 5 lines
Consider the following serialisation of events:
1. T1 asks to flush caches by writingGPU_CMDand settingpending_reqs
2. T1 dropsreqs_lock.
3. T2 enters IRQ handler for flush complete, spins lock waiting forreqs_lock
Minor correction: Imagine 2 and 3 reversed here in a non-IRQ-disabling, variant, otherwise "spins lock" does not make sense. With an IRQ-disabling variant, ignore the "spins lock waiting for reqs_lock" part.
Quoted 12 lines
4. T1 sleeps at
wait_event_timeout
5. T2 updatespending_reqsand wakes up the waiter in any order, since the effects of those two can't consistently be observed as sequential logic without the outerreqs_lockbeing held by the observer
6. T1 wakes up, checkspending_reqs, but sincepending_reqsis checked without holding any lock, so we implictly depend on the synchronisation point that is the waitqueue's lock rather than thereqs_lockspinlock, which says nothing about whether thepending_reqschange materialised on T1's side yet as far as I can tell?
7. T1 sees thatpending_reqs&GPU_IRQ_CLEAN_CACHES_COMPLETEDis still != 0, so goes back to sleep for some future wake-up ofreqs_ackedor a timeout.
Boris Brezillon
maintainerRe: [PATCH v2 2/3] drm/panthor: Revisit reqs_lock handling in flush/reset paths
In reply to Nicolas FrattaroliQuoted 44 lines · On Mon, 03 Aug 2026 15:13:25 +0200 Nicolas Frattaroli…
Quoted 15 lines · On Monday, 3 August 2026 10:53:24 Central European Summer Time Boris Brezillon…
Hello Nicolas,
Quoted 3 lines · On Thu, 30 Jul 2026 13:45:15 +0200 Nicolas Frattaroli…
panthor_gpu_flush_caches()andpanthor_gpu_soft_reset()would read (and even reset) the contents of thepending_reqsregister outside of holding thereqs_lock.Can you elaborate a bit on the race being fixed here? If
pending_reqsbits are truly cleared before thewake_up_all()call (which would require aWRITE_ONCE()to be enforced, admittedly), there's no risk for thewait_event()call to do a test before the bits have been updated, and this holds even if the test is done without the lock held.Quoted 5 lines
The other race I could think of is two threads calling
panthor_gpu_flush_caches()concurrently, and the second one stealing theFLUSH_COMPLETEDevent the first thread waits on and re-issuing a second flush on top, thus delaying the completion for the first thread. But that should be covered by thecache_flush_lock.
panthor_gpu_flush_caches()is not the only thing that sets/getspending_reqs. Notably, the threaded interrupt handler does, as well as any other functionality using the same member for reqs tracking (e.g. the soft reset).Consider the following serialisation of events:
1. T1 asks to flush caches by writingGPU_CMDand settingpending_reqs
2. T1 dropsreqs_lock.
3. T2 enters IRQ handler for flush complete, spins lock waiting forreqs_lock
4. T1 sleeps atwait_event_timeout
5. T2 updatespending_reqsand wakes up the waiter in any order, since the effects of those two can't consistently be observed as sequential logic without the outerreqs_lockbeing held by the observer
6. T1 wakes up, checkspending_reqs, but sincepending_reqsis checked without holding any lock, so we implictly depend on the synchronisation point that is the waitqueue's lock rather than thereqs_lockspinlock, which says nothing about whether thepending_reqschange materialised on T1's side yet as far as I can tell?
7. T1 sees thatpending_reqs&GPU_IRQ_CLEAN_CACHES_COMPLETEDis still != 0, so goes back to sleep for some future wake-up ofreqs_ackedor a timeout.
Quoted 5 lines
I'm not 100% sure, but I think 6. means that the memory model would permit T1 to re-use the
pending_reqsit previously set, rather than the updated one set by T2, since there's nothing stopping us from being woken up before thepending_reqschange has made itself known to observers not serialising with thereqs_lockbeing released by T2 inpanthor_gpu_irq_handlerafter.
If I read the "SLEEP AND WAKE-UP FUNCTIONS" section in [1] correctly, I think we're covered by the "general memory barrier" here.
Quoted 5 lines
If you check
lock_statbefore the change, you see that thereqs_lockis actually never contended. This isn't a good sign because it means whatever situation it exists to protect against never occurs, so either the lock is pointless or the lock is non-functional.
Or we're just in a situation where there's almost never two threads updating the pending_reqs concurrently, which is not the same as saying this can't happen at all.
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Additionally, when it did hold the lock, it did so with the irqsave/irqrestore variants, even though the spinlock was never acquired in an atomic context, just the threaded handler.
Use the new
wait_event_lock_timeout()macro to checkpending_reqsunder the lock, and only do so without disabling interrupts.Fixes 5cd894e258c4 ("drm/panthor: Add the GPU logical block")Signed-off-by Nicolas Frattaroli <[email protected]>--- drivers/gpu/drm/panthor/panthor_gpu.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)drivers/gpu/drm/panthor/panthor_gpu.c +0
-1@@ -330,35 +330,34 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, u32 l2, u32 lsc, u32 other) {struct panthor_gpu *gpu = ptdev->gpu; -unsigned long flags;int ret =0 ;/* Serialize cache flush operations. */ guard (mutex)(&ptdev->gpu->cache_flush_lock);
Quoted 7 lines
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 338
Quoted 2 lines
drivers/gpu/drm/panthor/panthor_gpu.c +1
-1-spin_lock_irqsave (&ptdev->gpu->reqs_lock, flags); +spin_lock (&ptdev->gpu->reqs_lock);Can we make the _irq{save,restore}-drop its own patch?
I'm not sure it's fine to drop the IRQ disabling without fixing the read of
pending_reqsoutside its lock.
Can be done after fixing the race, if it's really making the race more likely. Although, as I said above, I'm not yet convinced the race we're chasing is the one we've been discussing here.
Quoted 24 lines
Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/panthor/panthor_gpu.c +3
-2if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other)); }else { ret = -EIO ; } -spin_unlock_irqrestore (&ptdev->gpu->reqs_lock, flags); -if (ret) +if (ret) { +spin_unlock (&ptdev->gpu->reqs_lock);return ret; + }Quoted 7 lines
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 352
Quoted 3 lines
drivers/gpu/drm/panthor/panthor_gpu.c +1
-1- if (!wait_event_timeout(ptdev->gpu->reqs_acked, + if (!wait_event_lock_timeout(ptdev->gpu->reqs_acked, !(ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED),Assuming we really need to do the test with the lock held, could we add a patch at the beginning of the series that fixes the race without depending on the new wait macro, so that we have a version that can easily be backported?
Quoted 3 lines
It's either backporting the prerequisite new macro or still doing this all with IRQs disabled using the pre-existing
wait_event_lock_irq_timeoutmacro, and the IRQ disabled thing is what caused problems.
Can't we have the following helper function?
static bool cache_flush_done(struct panthor_device *ptdev)
{
guard(spinlock_irqsave)(&ptdev->gpu->reqs_lock);
return ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED;
}and then:
if (!wait_event_timeout(ptdev->gpu->reqs_acked,
cache_flush_done(ptdev),
msecs_to_jiffies(100))) {....
[1]https://docs.kernel.org/core-api/wrappers/memory-barriers.html#
PATCH v2 3/3 drm/panthor: Add tracepoints for cache flushing
In reply to Nicolas FrattaroliAdd two new event tracepoints: gpu_cache_flush_start to be emitted after acquiring the flush mutex and reqs spinlock, and gpu_cache_flush_end to be emitted when leaving the function.
This allows debugging the duration a flush takes irrespective of initial function entry lock contention by subtracting the start tracepoint's timestamp from the end tracepoint timestamp, and additionally contains information such as which caches were flushed.
--- drivers/gpu/drm/panthor/panthor_gpu.c | 3 ++ drivers/gpu/drm/panthor/panthor_trace.h | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+)
drivers/gpu/drm/panthor/panthor_gpu.c +3 -0
@@ -336,6 +336,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,guard (mutex)(&ptdev->gpu->cache_flush_lock);spin_lock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_start (ptdev->base.dev, l2, lsc, other);if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other));
@@ -345,6 +346,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,if (ret) {spin_unlock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_end (ptdev->base.dev, l2, lsc, other);return ret; }
@@ -358,6 +360,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED ; }spin_unlock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_end (ptdev->base.dev, l2, lsc, other);if (ret) {panthor_device_schedule_reset (ptdev);
drivers/gpu/drm/panthor/panthor_trace.h +49 -0
55 lines not on this page · raw message
Signature · 1 line
2.55.0
Re: [PATCH v2 3/3] drm/panthor: Add tracepoints for cache flushing
Quoted 9 lines · On Thu, 30 Jul 2026 13:45:16 +0200 Nicolas Frattaroli…
Add two new event tracepoints:
gpu_cache_flush_startto be emitted after acquiring the flush mutex and reqs spinlock, andgpu_cache_flush_endto be emitted when leaving the function.This allows debugging the duration a flush takes irrespective of initial function entry lock contention by subtracting the start tracepoint's timestamp from the end tracepoint timestamp, and additionally contains information such as which caches were flushed.
Quoted 5 lines
Signed-off-by Nicolas Frattaroli <[email protected]>--- drivers/gpu/drm/panthor/panthor_gpu.c | 3 ++ drivers/gpu/drm/panthor/panthor_trace.h | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+)
From the tracing point of view:
-- Steve
Re: [PATCH v2 3/3] drm/panthor: Add tracepoints for cache flushing
In reply to Nicolas FrattaroliQuoted 10 lines · Nicolas Frattaroli
Add two new event tracepoints:
gpu_cache_flush_startto be emitted after acquiring the flush mutex and reqs spinlock, andgpu_cache_flush_endto be emitted when leaving the function.This allows debugging the duration a flush takes irrespective of initial function entry lock contention by subtracting the start tracepoint's timestamp from the end tracepoint timestamp, and additionally contains information such as which caches were flushed.
Signed-off-by Nicolas Frattaroli <[email protected]>
Best regards,
Liviu
Quoted 97 lines
--- drivers/gpu/drm/panthor/panthor_gpu.c | 3 ++ drivers/gpu/drm/panthor/panthor_trace.h | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+)drivers/gpu/drm/panthor/panthor_gpu.c +3
-0@@ -336,6 +336,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,guard (mutex)(&ptdev->gpu->cache_flush_lock);spin_lock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_start (ptdev->base.dev, l2, lsc, other);if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other));@@ -345,6 +346,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,if (ret) {spin_unlock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_end (ptdev->base.dev, l2, lsc, other);return ret; }@@ -358,6 +360,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED ; }spin_unlock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_end (ptdev->base.dev, l2, lsc, other);if (ret) {panthor_device_schedule_reset (ptdev);drivers/gpu/drm/panthor/panthor_trace.h +49
-0@@ -76,6 +76,55 @@ TRACE_EVENT(gpu_job_irq, __entry->events, __entry->duration_ns) ); +DECLARE_EVENT_CLASS (gpu_cache_flush_template, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other), +TP_STRUCT__entry ( + __string(dev_name, dev_name(dev)) + __field(u32, l2) + __field(u32, lsc) +__field (u32, other) + ), +TP_fast_assign ( + __assign_str(dev_name); + __entry->l2 = l2; + __entry->lsc = lsc; + __entry->other = other; + ), +TP_printk ("%s: l2=0x%x lsc=0x%x other=0x%x" ,__get_str (dev_name), + __entry->l2, __entry->lsc, __entry->other) +); + +/** +* gpu_cache_flush_start - called after cache flush locks taken, before flush +* @dev: pointer to the &struct device, for printing the device name +* @l2: "l2" flush flags +* @lsc: "lsc" flush flags +* @other: "other" flush flags +* +* Fires after any initial lock contention around the locks needed for flushing +* caches, but before the actual cache flush is requested. +*/ +DEFINE_EVENT (gpu_cache_flush_template, gpu_cache_flush_start, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other) +); + +/** +* gpu_cache_flush_end - called after cache flush +* @dev: pointer to the &struct device, for printing the device name +* @l2: "l2" flush flags +* @lsc: "lsc" flush flags +* @other: "other" flush flags +* +* Fires after either the cache flush is complete, or has failed. Can be used +* together with gpu_cache_flush_start to get how long the flush has taken. +*/ +DEFINE_EVENT (gpu_cache_flush_template, gpu_cache_flush_end, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other) +); +#endif /* __PANTHOR_TRACE_H__ */ #undef TRACE_INCLUDE_PATHSignature · 2 lines
-- 2.55.0
Signature · 8 lines
====================
| I would like to |
| fix the world, |
| but they're not |
| giving me the |
\ source code! /
---------------
¯\_(ツ)_/¯Re: [PATCH v2 3/3] drm/panthor: Add tracepoints for cache flushing
In reply to Nicolas FrattaroliQuoted 10 lines · Nicolas Frattaroli
Add two new event tracepoints:
gpu_cache_flush_startto be emitted after acquiring the flush mutex and reqs spinlock, andgpu_cache_flush_endto be emitted when leaving the function.This allows debugging the duration a flush takes irrespective of initial function entry lock contention by subtracting the start tracepoint's timestamp from the end tracepoint timestamp, and additionally contains information such as which caches were flushed.
Signed-off-by Nicolas Frattaroli <[email protected]>
Thanks,
Steve
Quoted 94 lines
--- drivers/gpu/drm/panthor/panthor_gpu.c | 3 ++ drivers/gpu/drm/panthor/panthor_trace.h | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+)drivers/gpu/drm/panthor/panthor_gpu.c +3
-0@@ -336,6 +336,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,guard (mutex)(&ptdev->gpu->cache_flush_lock);spin_lock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_start (ptdev->base.dev, l2, lsc, other);if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other));@@ -345,6 +346,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,if (ret) {spin_unlock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_end (ptdev->base.dev, l2, lsc, other);return ret; }@@ -358,6 +360,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED ; }spin_unlock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_end (ptdev->base.dev, l2, lsc, other);if (ret) {panthor_device_schedule_reset (ptdev);drivers/gpu/drm/panthor/panthor_trace.h +49
-0@@ -76,6 +76,55 @@ TRACE_EVENT(gpu_job_irq, __entry->events, __entry->duration_ns) ); +DECLARE_EVENT_CLASS (gpu_cache_flush_template, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other), +TP_STRUCT__entry ( + __string(dev_name, dev_name(dev)) + __field(u32, l2) + __field(u32, lsc) +__field (u32, other) + ), +TP_fast_assign ( + __assign_str(dev_name); + __entry->l2 = l2; + __entry->lsc = lsc; + __entry->other = other; + ), +TP_printk ("%s: l2=0x%x lsc=0x%x other=0x%x" ,__get_str (dev_name), + __entry->l2, __entry->lsc, __entry->other) +); + +/** +* gpu_cache_flush_start - called after cache flush locks taken, before flush +* @dev: pointer to the &struct device, for printing the device name +* @l2: "l2" flush flags +* @lsc: "lsc" flush flags +* @other: "other" flush flags +* +* Fires after any initial lock contention around the locks needed for flushing +* caches, but before the actual cache flush is requested. +*/ +DEFINE_EVENT (gpu_cache_flush_template, gpu_cache_flush_start, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other) +); + +/** +* gpu_cache_flush_end - called after cache flush +* @dev: pointer to the &struct device, for printing the device name +* @l2: "l2" flush flags +* @lsc: "lsc" flush flags +* @other: "other" flush flags +* +* Fires after either the cache flush is complete, or has failed. Can be used +* together with gpu_cache_flush_start to get how long the flush has taken. +*/ +DEFINE_EVENT (gpu_cache_flush_template, gpu_cache_flush_end, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other) +); +#endif /* __PANTHOR_TRACE_H__ */ #undef TRACE_INCLUDE_PATH
Boris Brezillon
maintainerRe: [PATCH v2 3/3] drm/panthor: Add tracepoints for cache flushing
In reply to Nicolas FrattaroliQuoted 28 lines · On Thu, 30 Jul 2026 13:45:16 +0200 Nicolas Frattaroli…
Add two new event tracepoints:
gpu_cache_flush_startto be emitted after acquiring the flush mutex and reqs spinlock, andgpu_cache_flush_endto be emitted when leaving the function.This allows debugging the duration a flush takes irrespective of initial function entry lock contention by subtracting the start tracepoint's timestamp from the end tracepoint timestamp, and additionally contains information such as which caches were flushed.
Signed-off-by Nicolas Frattaroli <[email protected]>--- drivers/gpu/drm/panthor/panthor_gpu.c | 3 ++ drivers/gpu/drm/panthor/panthor_trace.h | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+)drivers/gpu/drm/panthor/panthor_gpu.c +1
-0@@ -336,6 +336,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,guard (mutex)(&ptdev->gpu->cache_flush_lock);spin_lock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_start (ptdev->base.dev, l2, lsc, other);if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other));
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 349
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_gpu.c +1
-0@@ -345,6 +346,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,if (ret) {spin_unlock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_end (ptdev->base.dev, l2, lsc, other);
Should we add a status to the end event, so that faulty flushes can be filtered out (those can be immediate, or timeout=100ms depending on where the failure happens, but they are not reflecting anything useful, and would pollute the stats)? Actually, if what we care about is the time it takes to do a flush, do we even need those start/end events, can't we pass the time as an argument and do the math in the function like we do in the job IRQ handler?
Quoted 72 lines
drivers/gpu/drm/panthor/panthor_gpu.c +1
-0return ret; }@@ -358,6 +360,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED ; }spin_unlock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_end (ptdev->base.dev, l2, lsc, other);if (ret) {panthor_device_schedule_reset (ptdev);drivers/gpu/drm/panthor/panthor_trace.h +49
-0@@ -76,6 +76,55 @@ TRACE_EVENT(gpu_job_irq, __entry->events, __entry->duration_ns) ); +DECLARE_EVENT_CLASS (gpu_cache_flush_template, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other), +TP_STRUCT__entry ( + __string(dev_name, dev_name(dev)) + __field(u32, l2) + __field(u32, lsc) +__field (u32, other) + ), +TP_fast_assign ( + __assign_str(dev_name); + __entry->l2 = l2; + __entry->lsc = lsc; + __entry->other = other; + ), +TP_printk ("%s: l2=0x%x lsc=0x%x other=0x%x" ,__get_str (dev_name), + __entry->l2, __entry->lsc, __entry->other) +); + +/** +* gpu_cache_flush_start - called after cache flush locks taken, before flush +* @dev: pointer to the &struct device, for printing the device name +* @l2: "l2" flush flags +* @lsc: "lsc" flush flags +* @other: "other" flush flags +* +* Fires after any initial lock contention around the locks needed for flushing +* caches, but before the actual cache flush is requested. +*/ +DEFINE_EVENT (gpu_cache_flush_template, gpu_cache_flush_start, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other) +); + +/** +* gpu_cache_flush_end - called after cache flush +* @dev: pointer to the &struct device, for printing the device name +* @l2: "l2" flush flags +* @lsc: "lsc" flush flags +* @other: "other" flush flags +* +* Fires after either the cache flush is complete, or has failed. Can be used +* together with gpu_cache_flush_start to get how long the flush has taken. +*/ +DEFINE_EVENT (gpu_cache_flush_template, gpu_cache_flush_end, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other) +); +#endif /* __PANTHOR_TRACE_H__ */ #undef TRACE_INCLUDE_PATH
Re: [PATCH v2 3/3] drm/panthor: Add tracepoints for cache flushing
Quoted 32 lines · On Monday, 3 August 2026 11:03:58 Central European Summer Time Boris Brezillon…
Quoted 29 lines · On Thu, 30 Jul 2026 13:45:16 +0200 Nicolas Frattaroli…
Add two new event tracepoints:
gpu_cache_flush_startto be emitted after acquiring the flush mutex and reqs spinlock, andgpu_cache_flush_endto be emitted when leaving the function.This allows debugging the duration a flush takes irrespective of initial function entry lock contention by subtracting the start tracepoint's timestamp from the end tracepoint timestamp, and additionally contains information such as which caches were flushed.
Signed-off-by Nicolas Frattaroli <[email protected]>--- drivers/gpu/drm/panthor/panthor_gpu.c | 3 ++ drivers/gpu/drm/panthor/panthor_trace.h | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+)drivers/gpu/drm/panthor/panthor_gpu.c +1
-0@@ -336,6 +336,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,guard (mutex)(&ptdev->gpu->cache_flush_lock);spin_lock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_start (ptdev->base.dev, l2, lsc, other);if (!(ptdev->gpu->pending_reqs &GPU_IRQ_CLEAN_CACHES_COMPLETED )) { ptdev->gpu->pending_reqs |=GPU_IRQ_CLEAN_CACHES_COMPLETED ;gpu_write (gpu->iomem,GPU_CMD ,GPU_FLUSH_CACHES (l2, lsc, other));@@ -345,6 +346,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev,
Quoted 11 lines
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_flush_caches(struct panthor_device *ptdev, · line 349
Quoted 3 lines
drivers/gpu/drm/panthor/panthor_gpu.c +1
-0if (ret) {spin_unlock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_end (ptdev->base.dev, l2, lsc, other);Should we add a status to the end event, so that faulty flushes can be filtered out (those can be immediate, or timeout=100ms depending on where the failure happens, but they are not reflecting anything useful, and would pollute the stats)? Actually, if what we care about is the time it takes to do a flush, do we even need those start/end events, can't we pass the time as an argument and do the math in the function like we do in the job IRQ handler?
The start/end pattern seems more common across the kernel. Since there is no doubt as to which start correlates with which end due to the mutex, I chose to go with that.
Quoted 75 lines
Quoted 73 lines
drivers/gpu/drm/panthor/panthor_gpu.c +1
-0return ret; }@@ -358,6 +360,7 @@ int panthor_gpu_flush_caches(struct panthor_device *ptdev, ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED ; }spin_unlock (&ptdev->gpu->reqs_lock); +trace_gpu_cache_flush_end (ptdev->base.dev, l2, lsc, other);if (ret) {panthor_device_schedule_reset (ptdev);drivers/gpu/drm/panthor/panthor_trace.h +49
-0@@ -76,6 +76,55 @@ TRACE_EVENT(gpu_job_irq, __entry->events, __entry->duration_ns) ); +DECLARE_EVENT_CLASS (gpu_cache_flush_template, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other), +TP_STRUCT__entry ( + __string(dev_name, dev_name(dev)) + __field(u32, l2) + __field(u32, lsc) +__field (u32, other) + ), +TP_fast_assign ( + __assign_str(dev_name); + __entry->l2 = l2; + __entry->lsc = lsc; + __entry->other = other; + ), +TP_printk ("%s: l2=0x%x lsc=0x%x other=0x%x" ,__get_str (dev_name), + __entry->l2, __entry->lsc, __entry->other) +); + +/** +* gpu_cache_flush_start - called after cache flush locks taken, before flush +* @dev: pointer to the &struct device, for printing the device name +* @l2: "l2" flush flags +* @lsc: "lsc" flush flags +* @other: "other" flush flags +* +* Fires after any initial lock contention around the locks needed for flushing +* caches, but before the actual cache flush is requested. +*/ +DEFINE_EVENT (gpu_cache_flush_template, gpu_cache_flush_start, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other) +); + +/** +* gpu_cache_flush_end - called after cache flush +* @dev: pointer to the &struct device, for printing the device name +* @l2: "l2" flush flags +* @lsc: "lsc" flush flags +* @other: "other" flush flags +* +* Fires after either the cache flush is complete, or has failed. Can be used +* together with gpu_cache_flush_start to get how long the flush has taken. +*/ +DEFINE_EVENT (gpu_cache_flush_template, gpu_cache_flush_end, + TP_PROTO(const struct device *dev, u32 l2, u32 lsc, u32 other), +TP_ARGS (dev, l2, lsc, other) +); +#endif /* __PANTHOR_TRACE_H__ */ #undef TRACE_INCLUDE_PATH