Skip to contentSkip to conversation

drm/vc4: Split and shrink job_lock

PATCH Applied Maíra Canal 2 participants 20 messages Oct 4 – Nov 9

Subsystems (3)
lore
Share & tools
lore

Maíra Canal

author reviewer
#0 lore raw

cover drm/vc4: Split and shrink job_lock

vc4->job_lock is a device-wide spinlock taken by the V3D interrupt handler, so everything under it runs with interrupts disabled. It currently protects several unrelated things: job queues, the binner slot pool, the saved hang state, the active perfmon, the attachment of the job fence to every BO, and more.

This series aims to (1) split vc4->job_lock into multiple independent locks and (2) shrinks its critical sections when possible.

In particular, the series address four current issues:

1. drm_syncobj_replace_fence() runs under job_lock, which lockdep reports as an IRQ lock inversion.

2. drm_exec_fini() runs under it as well and reaches kvfree(), which asks for preemptible task context.

3. VC4_SUBMIT_CL re-reads vc4->emit_seqno after dropping the lock, so a concurrent submission makes the ioctl return another job's seqno.

4. The perfmon counters are copied to userspace with no lock held.

The rest of series does a series of changes to move work out of job_lock and reduce contention.

On a Raspberry Pi 3B+ running glmark2-wayland --off-screen, lock_stat shows job_lock contentions reduced by 90% (2093 -> 207 per run) and total wait time by 96% (14.7 ms -> 0.6 ms). The longest job_lock hold, during which interrupts are disabled, drops from 747 us to 27 us, as the render done handler no longer signals fences under the lock. The workload is GPU-bound, so the glmark2 score is unchanged (239.6 vs 240.2) [1].

About 99k acquisitions per run move off job_lock onto the per-fence locks, and 185k more onto bin_alloc_lock, with little contention on either.

Considering that glmark2 is a GPU-bound client, it's important to notice that contention improvements might be higher with several clients or a CPU-bound workload, which I did not measure.

This series applies on top of "drm/vc4: Fix binner slot allocation failing on an idle GPU" [2].

[1] glmark2-wayland --off-screen scores are the mean of 5 runs per kernel without CONFIG_LOCK_STAT enabled. The lock data is the mean of 3 runs from CONFIG_LOCK_STAT builds.

[2] https://lore.kernel.org/dri-devel/[email protected]/T/

Best regards,
- Maíra

---
Maíra Canal (11):
drm/vc4: Publish the output fence outside of job_lock drm/vc4: Drop the BO reservations outside of job_lock drm/vc4: Refcount vc4_exec_info
drm/vc4: Return the seqno of the submitted job drm/vc4: Attach the job fences outside of job_lock drm/vc4: Use the inline fence lock
drm/vc4: Signal the render fence outside of job_lock drm/vc4: Take job_lock only once to drain job_done_list drm/vc4: Protect perfmon state with a dedicated lock drm/vc4: Protect the hang state with a dedicated lock drm/vc4: Split out bin_alloc_lock

 drivers/gpu/drm/vc4/vc4_drv.h     |  20 +++++--
 drivers/gpu/drm/vc4/vc4_gem.c     | 106 +++++++++++++++++++-------------------
 drivers/gpu/drm/vc4/vc4_irq.c     |  80 ++++++++++++++++------------
 drivers/gpu/drm/vc4/vc4_perfmon.c |  40 +++++++++-----
 drivers/gpu/drm/vc4/vc4_v3d.c     |   2 +-
 5 files changed, 146 insertions(+), 102 deletions(-)

---
base-commit: 64eded63d88ea70466c57cf433761bb1efea37a4
change-id: 20261002-vc4-misc-fixes-2b313622f570

author reviewer
#1 lore raw

PATCH 1/11 drm/vc4: Publish the output fence outside of job_lock

job_lock is taken by the V3D interrupt handler, so nesting an IRQ-unsafe lock under it creates an inversion. drm_syncobj_replace_fence() takes drm_syncobj::lock, which other paths acquire with interrupts enabled. For example, a thread interrupted while holding it blocks the handler on job_lock, and the handler can only make progress once the submit path releases job_lock, which it cannot do while waiting for drm_syncobj::lock.

This is reported by lockdep:

[  188.139076] ========================================================
[  188.139086] WARNING: possible irq lock inversion dependency detected
[  188.139096] 7.3.0-rc3-00067-gc4f3feeaf60f #67 Tainted: G         C
[  188.139108] --------------------------------------------------------
[  188.139116] swapper/0/0 just changed the state of lock:
[  188.139125] ffff0000054f8ff8 (&vc4->job_lock){-...}-{3:3}, at: vc4_irq+0xa8/0x28c [vc4]
[  188.139234] but this lock took another, HARDIRQ-unsafe lock in the past:
[  188.139242]  (&syncobj->lock){+.+.}-{3:3}
[  188.139256] and interrupts could create inverse lock ordering between them.
[  188.139267] other info that might help us debug this:
[  188.139275]  Possible interrupt unsafe locking scenario:
[  188.139284]        CPU0                    CPU1
[  188.139291]        ----                    ----
[  188.139299]   lock(&syncobj->lock);
[  188.139312]                                local_irq_disable();
[  188.139320]                                lock(&vc4->job_lock);
[  188.139335]                                lock(&syncobj->lock);
[  188.139350]   <Interrupt>
[  188.139358]     lock(&vc4->job_lock);
[  188.139372] *** DEADLOCK ***

Hand the fence over after job_lock is dropped. The job can complete and drop exec->fence in that window, so take a reference first and publish through the local pointer, as exec may already be gone.

Fixes e84fcb95e074 ("drm/vc4: Export fence through syncobj")
---
 drivers/gpu/drm/vc4/vc4_gem.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/gpu/drm/vc4/vc4_gem.c +9 -1
index 6ee4bd77d433..f4be154d4eb0 100644
@@ -639,8 +639,11 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 	fence->seqno = exec->seqno;
 	exec->fence = &fence->base;
 
+	/* The job can complete and drop exec->fence as soon as job_lock is
+	 * released, so hold our own reference.
+	 */
 	if (out_sync)
-		drm_syncobj_replace_fence(out_sync, exec->fence);
+		dma_fence_get(&fence->base);
 
 	vc4_attach_fences(exec);
 
@@ -662,6 +665,11 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 
 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
 
+	if (out_sync) {
+		drm_syncobj_replace_fence(out_sync, &fence->base);
+		dma_fence_put(&fence->base);
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 01/11] drm/vc4: Publish the output fence outside of job_lock

Quoted 76 lines · Maíra Canal

job_lock is taken by the V3D interrupt handler, so nesting an IRQ- unsafe
lock under it creates an inversion. drm_syncobj_replace_fence() takes drm_syncobj::lock, which other paths acquire with interrupts enabled. For
example, a thread interrupted while holding it blocks the handler on job_lock, and the handler can only make progress once the submit path releases job_lock, which it cannot do while waiting for drm_syncobj::lock.

This is reported by lockdep:

[  188.139076]
========================================================
[  188.139086] WARNING: possible irq lock inversion dependency detected
[  188.139096] 7.3.0-rc3-00067-gc4f3feeaf60f #67 Tainted: G         C [  188.139108] ------------------------------------------------------ --
[  188.139116] swapper/0/0 just changed the state of lock:
[  188.139125] ffff0000054f8ff8 (&vc4->job_lock){-...}-{3:3}, at:
vc4_irq+0xa8/0x28c [vc4]
[  188.139234] but this lock took another, HARDIRQ-unsafe lock in the past:
[  188.139242]  (&syncobj->lock){+.+.}-{3:3}
[  188.139256] and interrupts could create inverse lock ordering between them.
[  188.139267] other info that might help us debug this:
[  188.139275]  Possible interrupt unsafe locking scenario:
[  188.139284]        CPU0                    CPU1
[  188.139291]        ----                    ----
[  188.139299]   lock(&syncobj->lock);
[  188.139312]                                local_irq_disable(); [  188.139320]                                lock(&vc4->job_lock); [  188.139335]                                lock(&syncobj->lock); [  188.139350]   <Interrupt>
[  188.139358]     lock(&vc4->job_lock);
[  188.139372] *** DEADLOCK ***

Hand the fence over after job_lock is dropped. The job can complete and
drop exec->fence in that window, so take a reference first and publish
through the local pointer, as exec may already be gone.

Fixes e84fcb95e074 ("drm/vc4: Export fence through syncobj")

---
 drivers/gpu/drm/vc4/vc4_gem.c | 10 +++++++++-

 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/gpu/drm/vc4/vc4_gem.c +0 -0
index 6ee4bd77d433..f4be154d4eb0 100644
@@ -639,8 +639,11 @@ vc4_queue_submit(struct drm_device *dev, struct
 vc4_exec_info *exec,
  	fence->seqno = exec->seqno;
  	exec->fence = &fence->base;

drivers/gpu/drm/vc4/vc4_gem.c +1 -0
+	/* The job can complete and drop exec->fence as soon as

job_lock is

drivers/gpu/drm/vc4/vc4_gem.c +2 -0
+	 * released, so hold our own reference.
+	 */

  if (out_sync)

drivers/gpu/drm/vc4/vc4_gem.c +1 -1
-		drm_syncobj_replace_fence(out_sync, exec->fence);
+		dma_fence_get(&fence->base);


  vc4_attach_fences(exec);

drivers/gpu/drm/vc4/vc4_gem.c +0 -0
@@ -662,6 +665,11 @@ vc4_queue_submit(struct drm_device *dev, struct
 vc4_exec_info *exec,


  spin_unlock_irqrestore(&vc4->job_lock, irqflags);

drivers/gpu/drm/vc4/vc4_gem.c vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec, · line 672

Quoted 5 lines
drivers/gpu/drm/vc4/vc4_gem.c +5 -0
+	if (out_sync) {
+		drm_syncobj_replace_fence(out_sync, &fence->base);
+		dma_fence_put(&fence->base);
+	}
+

Doesn't this mean that it is theoretically possible that a very small job completes and its FLDONE/FRDONE interrupts trigger before we had a chance to install the fence?

Iago

Quoted 4 lines

  return 0;
 }

author reviewer
#3 lore raw

Re: [PATCH 01/11] drm/vc4: Publish the output fence outside of job_lock

Hi Iago,

Quoted 77 lines · Iago Toral
Quoted 76 lines · Maíra Canal

job_lock is taken by the V3D interrupt handler, so nesting an IRQ- unsafe
lock under it creates an inversion. drm_syncobj_replace_fence() takes drm_syncobj::lock, which other paths acquire with interrupts enabled. For
example, a thread interrupted while holding it blocks the handler on job_lock, and the handler can only make progress once the submit path releases job_lock, which it cannot do while waiting for drm_syncobj::lock.

This is reported by lockdep:

[  188.139076]
========================================================
[  188.139086] WARNING: possible irq lock inversion dependency detected
[  188.139096] 7.3.0-rc3-00067-gc4f3feeaf60f #67 Tainted: G         C [  188.139108] ------------------------------------------------------ --
[  188.139116] swapper/0/0 just changed the state of lock:
[  188.139125] ffff0000054f8ff8 (&vc4->job_lock){-...}-{3:3}, at:
vc4_irq+0xa8/0x28c [vc4]
[  188.139234] but this lock took another, HARDIRQ-unsafe lock in the past:
[  188.139242]  (&syncobj->lock){+.+.}-{3:3}
[  188.139256] and interrupts could create inverse lock ordering between them.
[  188.139267] other info that might help us debug this:
[  188.139275]  Possible interrupt unsafe locking scenario:
[  188.139284]        CPU0                    CPU1
[  188.139291]        ----                    ----
[  188.139299]   lock(&syncobj->lock);
[  188.139312]                                local_irq_disable(); [  188.139320]                                lock(&vc4->job_lock); [  188.139335]                                lock(&syncobj->lock); [  188.139350]   <Interrupt>
[  188.139358]     lock(&vc4->job_lock);
[  188.139372] *** DEADLOCK ***

Hand the fence over after job_lock is dropped. The job can complete and
drop exec->fence in that window, so take a reference first and publish
through the local pointer, as exec may already be gone.

Fixes e84fcb95e074 ("drm/vc4: Export fence through syncobj")
---
  drivers/gpu/drm/vc4/vc4_gem.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
drivers/gpu/drm/vc4/vc4_gem.c +1 -0
index 6ee4bd77d433..f4be154d4eb0 100644
@@ -639,8 +639,11 @@ vc4_queue_submit(struct drm_device *dev, struct
 vc4_exec_info *exec,
  	fence->seqno = exec->seqno;
  	exec->fence = &fence->base;
 
+	/* The job can complete and drop exec->fence as soon as

job_lock is

drivers/gpu/drm/vc4/vc4_gem.c +3 -1
+	 * released, so hold our own reference.
+	 */
  	if (out_sync)
-		drm_syncobj_replace_fence(out_sync, exec->fence);
+		dma_fence_get(&fence->base);
 
  	vc4_attach_fences(exec);
@@ -662,6 +665,11 @@ vc4_queue_submit(struct drm_device *dev, struct
 vc4_exec_info *exec,
 
  	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
 
Quoted 9 lines

drivers/gpu/drm/vc4/vc4_gem.c vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec, · line 672

Quoted 5 lines
drivers/gpu/drm/vc4/vc4_gem.c +5 -0
+	if (out_sync) {
+		drm_syncobj_replace_fence(out_sync, &fence->base);
+		dma_fence_put(&fence->base);
+	}
+

Doesn't this mean that it is theoretically possible that a very small job completes and its FLDONE/FRDONE interrupts trigger before we had a chance to install the fence?

Yes, the job can signal before the out_sync fence is installed, but that's harmless. Installing an already-signalled fence is valid and the syncobj only becomes visible to userspace once the ioctl returns, which happens after the replace. Therefore, in this scenario, userspace would see a valid syncobj (already-signalled) anyway.

Best regards,
- Maíra

author reviewer
#4 lore raw

PATCH 2/11 drm/vc4: Drop the BO reservations outside of job_lock

In reply to Maíra Canal

vc4_queue_submit() calls drm_exec_fini() with job_lock held and interrupts disabled. Besides unlocking the reservations, drm_exec_fini() frees the object array with kvfree(), which asks for preemptible task context or a non-NMI interrupt, and may reach might_sleep() through vfree().

Considering that an array that exceeds a page can take the vmalloc fallback, call drm_exec_fini() once job_lock is dropped. The reservations are then held across the job kick, which is harmless as nothing in the completion path takes a BO reservation.

Note that vc4_attach_fences() has to stay inside the critical section, as it walks exec, which the job done worker is free to release as soon as job_lock is dropped.

Fixes 04630796c437 ("drm/vc4: Use DRM Execution Contexts")
---
 drivers/gpu/drm/vc4/vc4_gem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/vc4/vc4_gem.c +2 -2
index f4be154d4eb0..0fef9b48d3c6 100644
@@ -647,8 +647,6 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 
 	vc4_attach_fences(exec);
 
-	drm_exec_fini(exec_ctx);
-
 	list_add_tail(&exec->head, &vc4->bin_job_list);
 
 	/* If no bin job was executing and if the render job (if any) has the
@@ -665,6 +663,8 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 
 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
 
+	drm_exec_fini(exec_ctx);
+
 	if (out_sync) {
 		drm_syncobj_replace_fence(out_sync, &fence->base);
 		dma_fence_put(&fence->base);
Signature · 1 line
2.55.0
author reviewer
#5 lore raw

PATCH 3/11 drm/vc4: Refcount vc4_exec_info

In reply to Maíra Canal

A queued job is owned by whoever reaches it last: the submit ioctl hands it to the job lists and the job done worker frees it. This means that the ioctl cannot touch vc4_exec_info once job_lock is dropped. That forces everything that touches exec to sit inside the critical section, even work that has nothing to do with the job queues.

Give the vc4_exec_info a refcount. The submit path holds one until it is done with the job and the job lists hold another from the moment the job is queued. The teardown happens when the last one goes away.

---
 drivers/gpu/drm/vc4/vc4_drv.h |  2 ++
 drivers/gpu/drm/vc4/vc4_gem.c | 30 ++++++++++++++++++++++--------
 2 files changed, 24 insertions(+), 8 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +2 -0
index b0e82c2dc1ba..d18a0fece93c 100644
@@ -671,6 +671,8 @@ struct vc4_crtc_state {
 struct vc4_exec_info {
 	struct vc4_dev *dev;
 
+	struct kref refcount;
+
 	/* Sequence number for this bin/render job. */
 	uint64_t seqno;
 
drivers/gpu/drm/vc4/vc4_gem.c +22 -8
index 0fef9b48d3c6..b56952bb6b97 100644
@@ -639,6 +639,8 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 	fence->seqno = exec->seqno;
 	exec->fence = &fence->base;
 
+	kref_get(&exec->refcount);
+
 	/* The job can complete and drop exec->fence as soon as job_lock is
 	 * released, so hold our own reference.
 	 */
@@ -719,11 +721,11 @@ vc4_cl_lookup_bos(struct drm_device *dev,
 
 fail_dec_usecnt:
 	/* Decrease usecnt on acquired objects.
-	 * We cannot rely on  vc4_complete_exec() to release resources here,
-	 * because vc4_complete_exec() has no information about which BO has
-	 * had its ->usecnt incremented.
+	 * We cannot rely on vc4_release_exec() to release resources here,
+	 * because it has no information about which BO has had its ->usecnt
+	 * incremented.
 	 * To make things easier we just free everything explicitly and set
-	 * exec->bo to NULL so that vc4_complete_exec() skips the 'BO release'
+	 * exec->bo to NULL so that vc4_release_exec() skips the 'BO release'
 	 * step.
 	 */
 	for (i-- ; i >= 0; i--)
@@ -854,9 +856,11 @@ vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
 }
 
 static void
-vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
+vc4_release_exec(struct kref *ref)
 {
-	struct vc4_dev *vc4 = to_vc4_dev(dev);
+	struct vc4_exec_info *exec = container_of(ref, struct vc4_exec_info,
+						  refcount);
+	struct vc4_dev *vc4 = exec->dev;
 	unsigned long irqflags;
 	unsigned i;
 
@@ -908,6 +912,12 @@ vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
 	kfree(exec);
 }
 
+static void
+vc4_exec_put(struct vc4_exec_info *exec)
+{
+	kref_put(&exec->refcount, vc4_release_exec);
+}
+
 void
 vc4_job_handle_completed(struct vc4_dev *vc4)
 {
@@ -924,7 +934,7 @@ vc4_job_handle_completed(struct vc4_dev *vc4)
 		list_del(&exec->head);
 
 		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
-		vc4_complete_exec(&vc4->base, exec);
+		vc4_exec_put(exec);
 		spin_lock_irqsave(&vc4->job_lock, irqflags);
 	}
 
@@ -1071,6 +1081,8 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
 		return ret;
 	}
 
+	kref_init(&exec->refcount);
+
 	exec->args = args;
 	INIT_LIST_HEAD(&exec->unref_list);
 
@@ -1160,12 +1172,14 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
 	/* Return the seqno for our job. */
 	args->seqno = vc4->emit_seqno;
 
+	vc4_exec_put(exec);
+
 	return 0;
 
 fail_unreserve:
 	drm_exec_fini(&exec_ctx);
 fail:
-	vc4_complete_exec(&vc4->base, exec);
+	vc4_exec_put(exec);
 
 	return ret;
 }
Signature · 1 line
2.55.0
author reviewer
#6 lore raw

PATCH 4/11 drm/vc4: Return the seqno of the submitted job

In reply to Maíra Canal

VC4_SUBMIT_CL reports the seqno of the job it queued by re-reading vc4->emit_seqno once vc4_queue_submit() has returned. That counter is shared by every submission on the device, so a job queued by another thread in the meantime bumps it first and the ioctl hands userspace a seqno belonging to a job it did not submit. Waiting on that seqno means waiting for unrelated work.

Now that the submit path holds a reference to exec until it is done with the job, report the seqno recorded in it.

Fixes d5b1a78a772f ("drm/vc4: Add support for drawing 3D frames.")
---
 drivers/gpu/drm/vc4/vc4_gem.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/gpu/drm/vc4/vc4_gem.c +2 -4
index b56952bb6b97..9cf343297f83 100644
@@ -620,7 +620,6 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 {
 	struct vc4_dev *vc4 = to_vc4_dev(dev);
 	struct vc4_exec_info *renderjob;
-	uint64_t seqno;
 	unsigned long irqflags;
 	struct vc4_fence *fence;
 
@@ -631,8 +630,7 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 
 	spin_lock_irqsave(&vc4->job_lock, irqflags);
 
-	seqno = ++vc4->emit_seqno;
-	exec->seqno = seqno;
+	exec->seqno = ++vc4->emit_seqno;
 
 	dma_fence_init(&fence->base, &vc4_fence_ops, &vc4->job_lock,
 		       vc4->dma_fence_context, exec->seqno);
@@ -1170,7 +1168,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
 		goto fail_unreserve;
 
 	/* Return the seqno for our job. */
-	args->seqno = vc4->emit_seqno;
+	args->seqno = exec->seqno;
 
 	vc4_exec_put(exec);
 
Signature · 1 line
2.55.0
author reviewer
#7 lore raw

PATCH 5/11 drm/vc4: Attach the job fences outside of job_lock

In reply to Maíra Canal

vc4_attach_fences() adds the job fence to the reservation of every BO the job touches, which is a O(bo_count) walk under job_lock. It was there because vc4_exec_info could be freed by the job done worker the moment the lock was dropped.

As vc4_exec_info is refcounted now, hand the fences over after the unlock.

Note that the render done handler dropped the fence as soon as the job completed, so the walk could find exec->fence already cleared. Leave the fence to be dropped by the release path. The submit path no longer needs a reference of its own to publish the fence through the sync object either.

---
 drivers/gpu/drm/vc4/vc4_gem.c | 23 ++++++-----------------
 drivers/gpu/drm/vc4/vc4_irq.c |  5 +----
 2 files changed, 7 insertions(+), 21 deletions(-)
drivers/gpu/drm/vc4/vc4_gem.c +6 -17
index 9cf343297f83..ae76868663ea 100644
@@ -639,14 +639,6 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 
 	kref_get(&exec->refcount);
 
-	/* The job can complete and drop exec->fence as soon as job_lock is
-	 * released, so hold our own reference.
-	 */
-	if (out_sync)
-		dma_fence_get(&fence->base);
-
-	vc4_attach_fences(exec);
-
 	list_add_tail(&exec->head, &vc4->bin_job_list);
 
 	/* If no bin job was executing and if the render job (if any) has the
@@ -663,12 +655,12 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 
 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
 
+	vc4_attach_fences(exec);
+
 	drm_exec_fini(exec_ctx);
 
-	if (out_sync) {
+	if (out_sync)
 		drm_syncobj_replace_fence(out_sync, &fence->base);
-		dma_fence_put(&fence->base);
-	}
 
 	return 0;
 }
@@ -862,13 +854,10 @@ vc4_release_exec(struct kref *ref)
 	unsigned long irqflags;
 	unsigned i;
 
-	/* If we got force-completed because of GPU reset rather than
-	 * through our IRQ handler, signal the fence now.
+	/* The render done handler signals the fence, so only drop the
+	 * reference here.
 	 */
-	if (exec->fence) {
-		dma_fence_signal(exec->fence);
-		dma_fence_put(exec->fence);
-	}
+	dma_fence_put(exec->fence);
 
 	if (exec->bo) {
 		for (i = 0; i < exec->bo_count; i++) {
drivers/gpu/drm/vc4/vc4_irq.c +1 -4
index 6ced2835dfdc..241249b6582b 100644
@@ -193,11 +193,8 @@ vc4_irq_finish_render_job(struct drm_device *dev)
 	else if (nextbin && nextbin->perfmon != exec->perfmon)
 		vc4_submit_next_bin_job(dev);
 
-	if (exec->fence) {
+	if (exec->fence)
 		dma_fence_signal_locked(exec->fence);
-		dma_fence_put(exec->fence);
-		exec->fence = NULL;
-	}
 
 	wake_up_all(&vc4->job_wait_queue);
 	schedule_work(&vc4->job_done_work);
Signature · 1 line
2.55.0
#8 lore raw

Re: [PATCH 05/11] drm/vc4: Attach the job fences outside of job_lock

Quoted 53 lines · Maíra Canal

vc4_attach_fences() adds the job fence to the reservation of every BO the
job touches, which is a O(bo_count) walk under job_lock. It was there because vc4_exec_info could be freed by the job done worker the moment the
lock was dropped.

As vc4_exec_info is refcounted now, hand the fences over after the unlock.

Note that the render done handler dropped the fence as soon as the job
completed, so the walk could find exec->fence already cleared. Leave the
fence to be dropped by the release path. The submit path no longer needs
a reference of its own to publish the fence through the sync object either.

---
 drivers/gpu/drm/vc4/vc4_gem.c | 23 ++++++----------------- drivers/gpu/drm/vc4/vc4_irq.c |  5 +----

 2 files changed, 7 insertions(+), 21 deletions(-)
drivers/gpu/drm/vc4/vc4_gem.c +0 -0
index 9cf343297f83..ae76868663ea 100644
@@ -639,14 +639,6 @@ vc4_queue_submit(struct drm_device *dev, struct
 vc4_exec_info *exec,


  kref_get(&exec->refcount);

drivers/gpu/drm/vc4/vc4_gem.c +0 -1
-	/* The job can complete and drop exec->fence as soon as

job_lock is

drivers/gpu/drm/vc4/vc4_gem.c +0 -7
-	 * released, so hold our own reference.
-	 */
-	if (out_sync)
-		dma_fence_get(&fence->base);
-
-	vc4_attach_fences(exec);
-

  list_add_tail(&exec->head, &vc4->bin_job_list);
  /* If no bin job was executing and if the render job (if any) has the

drivers/gpu/drm/vc4/vc4_gem.c +0 -0
@@ -663,12 +655,12 @@ vc4_queue_submit(struct drm_device *dev, struct
 vc4_exec_info *exec,


  spin_unlock_irqrestore(&vc4->job_lock, irqflags);

drivers/gpu/drm/vc4/vc4_gem.c vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec, · line 659

Quoted 2 lines
drivers/gpu/drm/vc4/vc4_gem.c +2 -0
+	vc4_attach_fences(exec);
+

Similar question as before: doesn't this mean we may submit this job before we have installed the fences in the BO reservations? Wouldn't that mean there is a small window where the BOs look idle to other users when they are in fact being used by the GPU?

Iago

Quoted 49 lines

  drm_exec_fini(exec_ctx);

drivers/gpu/drm/vc4/vc4_gem.c +1 -1
-	if (out_sync) {
+	if (out_sync)

  drm_syncobj_replace_fence(out_sync, &fence->base);

drivers/gpu/drm/vc4/vc4_gem.c +0 -2
-		dma_fence_put(&fence->base);
-	}


  return 0;
 }

drivers/gpu/drm/vc4/vc4_gem.c +0 -0
@@ -862,13 +854,10 @@ vc4_release_exec(struct kref *ref)
  	unsigned long irqflags;
  	unsigned i;

drivers/gpu/drm/vc4/vc4_gem.c +0 -1
-	/* If we got force-completed because of GPU reset rather

than

drivers/gpu/drm/vc4/vc4_gem.c +1 -1
-	 * through our IRQ handler, signal the fence now.
+	/* The render done handler signals the fence, so only drop

the

drivers/gpu/drm/vc4/vc4_gem.c +1 -0
+	 * reference here.

  */

drivers/gpu/drm/vc4/vc4_gem.c +1 -4
-	if (exec->fence) {
-		dma_fence_signal(exec->fence);
-		dma_fence_put(exec->fence);
-	}
+	dma_fence_put(exec->fence);


  if (exec->bo) {
  for (i = 0; i < exec->bo_count; i++) {

drivers/gpu/drm/vc4/vc4_irq.c +0 -0
index 6ced2835dfdc..241249b6582b 100644
@@ -193,11 +193,8 @@ vc4_irq_finish_render_job(struct drm_device
 *dev)
  	else if (nextbin && nextbin->perfmon != exec->perfmon)
  		vc4_submit_next_bin_job(dev);

drivers/gpu/drm/vc4/vc4_irq.c +1 -1
-	if (exec->fence) {
+	if (exec->fence)

  dma_fence_signal_locked(exec->fence);

drivers/gpu/drm/vc4/vc4_irq.c +0 -3
-		dma_fence_put(exec->fence);
-		exec->fence = NULL;
-	}


  wake_up_all(&vc4->job_wait_queue);
  schedule_work(&vc4->job_done_work);

author reviewer
#9 lore raw

Re: [PATCH 05/11] drm/vc4: Attach the job fences outside of job_lock

Hi Iago,

Quoted 54 lines · Iago Toral
Quoted 53 lines · Maíra Canal

vc4_attach_fences() adds the job fence to the reservation of every BO the
job touches, which is a O(bo_count) walk under job_lock. It was there because vc4_exec_info could be freed by the job done worker the moment the
lock was dropped.

As vc4_exec_info is refcounted now, hand the fences over after the unlock.

Note that the render done handler dropped the fence as soon as the job
completed, so the walk could find exec->fence already cleared. Leave the
fence to be dropped by the release path. The submit path no longer needs
a reference of its own to publish the fence through the sync object either.

---
  drivers/gpu/drm/vc4/vc4_gem.c | 23 ++++++-----------------
  drivers/gpu/drm/vc4/vc4_irq.c |  5 +----
  2 files changed, 7 insertions(+), 21 deletions(-)
drivers/gpu/drm/vc4/vc4_gem.c +0 -1
index 9cf343297f83..ae76868663ea 100644
@@ -639,14 +639,6 @@ vc4_queue_submit(struct drm_device *dev, struct
 vc4_exec_info *exec,
 
  	kref_get(&exec->refcount);
 
-	/* The job can complete and drop exec->fence as soon as

job_lock is

drivers/gpu/drm/vc4/vc4_gem.c +0 -7
-	 * released, so hold our own reference.
-	 */
-	if (out_sync)
-		dma_fence_get(&fence->base);
-
-	vc4_attach_fences(exec);
-
  	list_add_tail(&exec->head, &vc4->bin_job_list);
 
  	/* If no bin job was executing and if the render job (if

any) has the

drivers/gpu/drm/vc4/vc4_gem.c +0 -0
@@ -663,12 +655,12 @@ vc4_queue_submit(struct drm_device *dev, struct
 vc4_exec_info *exec,
 
  	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
 
Quoted 8 lines

drivers/gpu/drm/vc4/vc4_gem.c vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec, · line 659

Quoted 2 lines
drivers/gpu/drm/vc4/vc4_gem.c +2 -0
+	vc4_attach_fences(exec);
+

Similar question as before: doesn't this mean we may submit this job before we have installed the fences in the BO reservations? Wouldn't that mean there is a small window where the BOs look idle to other users when they are in fact being used by the GPU?

In this case, the window exists, but it isn't observable in a way that actually matters. The reservations stay locked until drm_exec_fini(), which runs after the fences are attached, so anyone taking the resv lock, including another submit touching the same BOs, waits for them. Other drivers, such as V3D and Panfrost, follow the same pattern: push jobs -> attach fences -> replace the out syncobjs fences.

Best regards,
- Maíra

#10 lore raw

Re: [PATCH 05/11] drm/vc4: Attach the job fences outside of job_lock

Quoted 77 lines · Maíra Canal

Hi Iago,

Quoted 64 lines · Iago Toral
Quoted 63 lines · Maíra Canal

vc4_attach_fences() adds the job fence to the reservation of every BO
the
job touches, which is a O(bo_count) walk under job_lock. It was there
because vc4_exec_info could be freed by the job done worker the moment the
lock was dropped.

As vc4_exec_info is refcounted now, hand the fences over after the
unlock.

Note that the render done handler dropped the fence as soon as the
job
completed, so the walk could find exec->fence already cleared. Leave
the
fence to be dropped by the release path. The submit path no longer
needs
a reference of its own to publish the fence through the sync object
either.

---
  drivers/gpu/drm/vc4/vc4_gem.c | 23 ++++++----------------- drivers/gpu/drm/vc4/vc4_irq.c |  5 +----

  2 files changed, 7 insertions(+), 21 deletions(-)
drivers/gpu/drm/vc4/vc4_gem.c +0 -0
index 9cf343297f83..ae76868663ea 100644
@@ -639,14 +639,6 @@ vc4_queue_submit(struct drm_device *dev,

struct
vc4_exec_info *exec,

   kref_get(&exec->refcount);

drivers/gpu/drm/vc4/vc4_gem.c +0 -1
-	/* The job can complete and drop exec->fence as soon as

job_lock is

drivers/gpu/drm/vc4/vc4_gem.c +0 -7
-	 * released, so hold our own reference.
-	 */
-	if (out_sync)
-		dma_fence_get(&fence->base);
-
-	vc4_attach_fences(exec);
-

   list_add_tail(&exec->head, &vc4->bin_job_list);
   /* If no bin job was executing and if the render job (if any) has the

drivers/gpu/drm/vc4/vc4_gem.c +0 -0
@@ -663,12 +655,12 @@ vc4_queue_submit(struct drm_device *dev,

struct
vc4_exec_info *exec,

   spin_unlock_irqrestore(&vc4->job_lock, irqflags);

drivers/gpu/drm/vc4/vc4_gem.c +1 -0
+	vc4_attach_fences(exec);
Quoted 9 lines
Quoted 1 line

+

Similar question as before: doesn't this mean we may submit this job
before we have installed the fences in the BO reservations? Wouldn't
that mean there is a small window where the BOs look idle to other users when they are in fact being used by the GPU?

Quoted 9 lines

In this case, the window exists, but it isn't observable in a way that
actually matters. The reservations stay locked until drm_exec_fini(), which runs after the fences are attached, so anyone taking the resv lock, including another submit touching the same BOs, waits for them. Other drivers, such as V3D and Panfrost, follow the same pattern:
push
jobs -> attach fences -> replace the out syncobjs fences.

Great, thanks, we may want to add small comments in both patches to make these things explicit since I don't think they are immediately obvious.

With that, you can add my R-B to both patches too.

Iago

Quoted 4 lines

Best regards,
- Maíra

author reviewer
#11 lore raw

PATCH 6/11 drm/vc4: Use the inline fence lock

In reply to Maíra Canal

Passing job_lock to dma_fence_init() ties every fence to a lock embedded in vc4_dev. A fence attached to an exported BO's reservation can be held by another driver after vc4 is gone, leaving fence->lock dangling, and it pulls DRM core paths such as dma_fence_add_callback() into the lock that serialises the job queues.

Take a NULL lock in dma_fence_init() and give it a lock of its own, which is what the external lock has been deprecated in favour of.

---
 drivers/gpu/drm/vc4/vc4_gem.c | 2 +-
 drivers/gpu/drm/vc4/vc4_irq.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/vc4/vc4_gem.c +1 -1
index ae76868663ea..b65b3f62b098 100644
@@ -632,7 +632,7 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 
 	exec->seqno = ++vc4->emit_seqno;
 
-	dma_fence_init(&fence->base, &vc4_fence_ops, &vc4->job_lock,
+	dma_fence_init(&fence->base, &vc4_fence_ops, NULL,
 		       vc4->dma_fence_context, exec->seqno);
 	fence->seqno = exec->seqno;
 	exec->fence = &fence->base;
drivers/gpu/drm/vc4/vc4_irq.c +1 -1
index 241249b6582b..f87550f3e55d 100644
@@ -194,7 +194,7 @@ vc4_irq_finish_render_job(struct drm_device *dev)
 		vc4_submit_next_bin_job(dev);
 
 	if (exec->fence)
-		dma_fence_signal_locked(exec->fence);
+		dma_fence_signal(exec->fence);
 
 	wake_up_all(&vc4->job_wait_queue);
 	schedule_work(&vc4->job_done_work);
Signature · 1 line
2.55.0
author reviewer
#12 lore raw

PATCH 7/11 drm/vc4: Signal the render fence outside of job_lock

In reply to Maíra Canal

vc4_irq_finish_render_job() signals the job's fence, wakes the waiters, and queues the job done work while holding job_lock. This increases the job_lock contention as dma_fence_signal() takes the fence lock and runs every callback attached to the fence. So, job_lock is held, with interrupts disabled, for as long as those callbacks take.

Considering that these functions are unrelated to the job lists, hand the fence out of the critical section and signal it, wake the waiters and queue the work once job_lock is dropped.

In order to split the end of the render job and the fence signalling, take a reference before handing it over, as the job done worker is free to release the exec as soon as the lock is dropped.

On a Raspberry Pi 3, LOCK_STAT shows this section holding job_lock in about three quarters of its contentions, showing the relevance of reducing this critical section.

---
 drivers/gpu/drm/vc4/vc4_irq.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
drivers/gpu/drm/vc4/vc4_irq.c +22 -6
index f87550f3e55d..dcee4a1e17ad 100644
@@ -152,7 +152,7 @@ vc4_cancel_bin_job(struct drm_device *dev)
 	vc4_submit_next_bin_job(dev);
 }
 
-static void
+static struct dma_fence *
 vc4_irq_finish_render_job(struct drm_device *dev)
 {
 	struct vc4_dev *vc4 = to_vc4_dev(dev);
@@ -160,7 +160,7 @@ vc4_irq_finish_render_job(struct drm_device *dev)
 	struct vc4_exec_info *nextbin, *nextrender;
 
 	if (!exec)
-		return;
+		return NULL;
 
 	trace_vc4_rcl_end_irq(dev, exec->seqno);
 
@@ -193,8 +193,17 @@ vc4_irq_finish_render_job(struct drm_device *dev)
 	else if (nextbin && nextbin->perfmon != exec->perfmon)
 		vc4_submit_next_bin_job(dev);
 
-	if (exec->fence)
-		dma_fence_signal(exec->fence);
+	return dma_fence_get(exec->fence);
+}
+
+static void
+vc4_irq_render_job_done(struct vc4_dev *vc4, struct dma_fence *fence)
+{
+	if (!fence)
+		return;
+
+	dma_fence_signal(fence);
+	dma_fence_put(fence);
 
 	wake_up_all(&vc4->job_wait_queue);
 	schedule_work(&vc4->job_done_work);
@@ -233,9 +242,13 @@ vc4_irq(int irq, void *arg)
 	}
 
 	if (intctl & V3D_INT_FRDONE) {
+		struct dma_fence *fence;
+
 		spin_lock(&vc4->job_lock);
-		vc4_irq_finish_render_job(dev);
+		fence = vc4_irq_finish_render_job(dev);
 		spin_unlock(&vc4->job_lock);
+
+		vc4_irq_render_job_done(vc4, fence);
 		status = IRQ_HANDLED;
 	}
 
@@ -328,6 +341,7 @@ void vc4_irq_uninstall(struct drm_device *dev)
 void vc4_irq_reset(struct drm_device *dev)
 {
 	struct vc4_dev *vc4 = to_vc4_dev(dev);
+	struct dma_fence *fence;
 	unsigned long irqflags;
 
 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
@@ -346,6 +360,8 @@ void vc4_irq_reset(struct drm_device *dev)
 
 	spin_lock_irqsave(&vc4->job_lock, irqflags);
 	vc4_cancel_bin_job(dev);
-	vc4_irq_finish_render_job(dev);
+	fence = vc4_irq_finish_render_job(dev);
 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
+
+	vc4_irq_render_job_done(vc4, fence);
 }
Signature · 1 line
2.55.0
author reviewer
#13 lore raw

PATCH 8/11 drm/vc4: Take job_lock only once to drain job_done_list

In reply to Maíra Canal

vc4_job_handle_completed() drops and retakes job_lock around every job it completes, so a batch of N finished jobs costs N + 1 acquisitions of a lock the interrupt handler takes on every bin and render done. Each retake can contend with the handler.

Splice job_done_list onto a local list under a single acquisition and complete the jobs from there. Jobs finishing meanwhile requeue the done work, which picks them up on its next run.

---
 drivers/gpu/drm/vc4/vc4_gem.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/gpu/drm/vc4/vc4_gem.c +6 -11
index b65b3f62b098..b61f9b0a7c09 100644
@@ -908,24 +908,19 @@ vc4_exec_put(struct vc4_exec_info *exec)
 void
 vc4_job_handle_completed(struct vc4_dev *vc4)
 {
-	unsigned long irqflags;
+	struct vc4_exec_info *exec, *next;
+	LIST_HEAD(done);
 
 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
 		return;
 
-	spin_lock_irqsave(&vc4->job_lock, irqflags);
-	while (!list_empty(&vc4->job_done_list)) {
-		struct vc4_exec_info *exec =
-			list_first_entry(&vc4->job_done_list,
-					 struct vc4_exec_info, head);
+	scoped_guard(spinlock_irqsave, &vc4->job_lock)
+		list_splice_init(&vc4->job_done_list, &done);
+
+	list_for_each_entry_safe(exec, next, &done, head) {
 		list_del(&exec->head);
-
-		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
 		vc4_exec_put(exec);
-		spin_lock_irqsave(&vc4->job_lock, irqflags);
 	}
-
-	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
 }
 
 /* Scheduled when any job has been completed, this walks the list of
Signature · 1 line
2.55.0
author reviewer
#14 lore raw

PATCH 9/11 drm/vc4: Protect perfmon state with a dedicated lock

In reply to Maíra Canal

The V3D block exposes a single set of performance counters, so at most one perfmon can be programmed into the hardware at a time. Until now, job_lock serialises the access to the active perfmon, but not every access was being serialized. For example, vc4_perfmon_get_values_ioctl() copies perfmon->counters out while vc4_perfmon_stop() accumulates into the same array from the interrupt handler, so userspace can read a half-updated array. Also, vc4_perfmon_delete() tests the pointer before taking any lock.

Extending job_lock over those two paths would put a userspace read and the destroy path on the lock the interrupt handler uses to drive the job queues. The perfmon state has no invariant in common with those queues.

Introduce a dedicated spinlock next to the active perfmon pointer and take it in vc4_perfmon_start() and vc4_perfmon_stop(). Snapshot the counter values under the same lock in vc4_perfmon_get_values_ioctl() so that a concurrent stop cannot expose a half-updated array to userspace.

Fixes 65101d8c9108 ("drm/vc4: Expose performance counters to userspace")
---
 drivers/gpu/drm/vc4/vc4_drv.h     | 13 ++++++++++---
 drivers/gpu/drm/vc4/vc4_gem.c     |  7 ++-----
 drivers/gpu/drm/vc4/vc4_irq.c     |  3 +--
 drivers/gpu/drm/vc4/vc4_perfmon.c | 40 +++++++++++++++++++++++++++------------
 4 files changed, 41 insertions(+), 22 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +10 -3
index d18a0fece93c..2fad8a489558 100644
@@ -181,10 +181,17 @@ struct vc4_dev {
 	wait_queue_head_t job_wait_queue;
 	struct work_struct job_done_work;
 
-	/* Used to track the active perfmon if any. Access to this field is
-	 * protected by job_lock.
+	/* Tracks the performance monitor state. The V3D block exposes a single
+	 * set of performance counters, so at most one perfmon can be active at
+	 * any moment.
 	 */
-	struct vc4_perfmon *active_perfmon;
+	struct {
+		/* Protects @active. */
+		spinlock_t lock;
+
+		/* Perfmon currently programmed in HW (or NULL if none). */
+		struct vc4_perfmon *active;
+	} perfmon_state;
 
 	/* The memory used for storing binner tile alloc, tile state,
 	 * and overflow memory allocations.  This is freed when V3D
drivers/gpu/drm/vc4/vc4_gem.c +2 -5
index b61f9b0a7c09..1f3cb3c2c7fc 100644
@@ -488,11 +488,7 @@ vc4_submit_next_bin_job(struct drm_device *dev)
 
 	vc4_flush_caches(dev);
 
-	/* Only start the perfmon if it was not already started by a previous
-	 * job.
-	 */
-	if (exec->perfmon && vc4->active_perfmon != exec->perfmon)
-		vc4_perfmon_start(vc4, exec->perfmon);
+	vc4_perfmon_start(vc4, exec->perfmon);
 
 	/* Either put the job in the binner if it uses the binner, or
 	 * immediately move it to the to-be-rendered queue.
@@ -1181,6 +1177,7 @@ int vc4_gem_init(struct drm_device *dev)
 	INIT_LIST_HEAD(&vc4->render_job_list);
 	INIT_LIST_HEAD(&vc4->job_done_list);
 	spin_lock_init(&vc4->job_lock);
+	spin_lock_init(&vc4->perfmon_state.lock);
 
 	INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
 	timer_setup(&vc4->hangcheck.timer, vc4_hangcheck_elapsed, 0);
drivers/gpu/drm/vc4/vc4_irq.c +1 -2
index dcee4a1e17ad..f8787d4169ef 100644
@@ -145,8 +145,7 @@ vc4_cancel_bin_job(struct drm_device *dev)
 		return;
 
 	/* Stop the perfmon so that the next bin job can be started. */
-	if (exec->perfmon)
-		vc4_perfmon_stop(vc4, exec->perfmon, false);
+	vc4_perfmon_stop(vc4, exec->perfmon, false);
 
 	list_move_tail(&exec->head, &vc4->bin_job_list);
 	vc4_submit_next_bin_job(dev);
drivers/gpu/drm/vc4/vc4_perfmon.c +28 -12
index f75dfd156756..dd3414157d61 100644
@@ -51,7 +51,15 @@ void vc4_perfmon_start(struct vc4_dev *vc4, struct vc4_perfmon *perfmon)
 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
 		return;
 
-	if (WARN_ON_ONCE(!perfmon || vc4->active_perfmon))
+	if (!perfmon)
+		return;
+
+	guard(spinlock_irqsave)(&vc4->perfmon_state.lock);
+
+	/* Only start the perfmon if it was not already started by a
+	 * previous job.
+	 */
+	if (vc4->perfmon_state.active == perfmon)
 		return;
 
 	for (i = 0; i < perfmon->ncounters; i++)
@@ -60,7 +68,7 @@ void vc4_perfmon_start(struct vc4_dev *vc4, struct vc4_perfmon *perfmon)
 	mask = GENMASK(perfmon->ncounters - 1, 0);
 	V3D_WRITE(V3D_PCTRC, mask);
 	V3D_WRITE(V3D_PCTRE, V3D_PCTRE_EN | mask);
-	vc4->active_perfmon = perfmon;
+	vc4->perfmon_state.active = perfmon;
 }
 
 void vc4_perfmon_stop(struct vc4_dev *vc4, struct vc4_perfmon *perfmon,
@@ -71,8 +79,12 @@ void vc4_perfmon_stop(struct vc4_dev *vc4, struct vc4_perfmon *perfmon,
 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
 		return;
 
-	if (WARN_ON_ONCE(!vc4->active_perfmon ||
-			 perfmon != vc4->active_perfmon))
+	if (!perfmon)
+		return;
+
+	guard(spinlock_irqsave)(&vc4->perfmon_state.lock);
+
+	if (perfmon != vc4->perfmon_state.active)
 		return;
 
 	if (capture) {
@@ -81,7 +93,7 @@ void vc4_perfmon_stop(struct vc4_dev *vc4, struct vc4_perfmon *perfmon,
 	}
 
 	V3D_WRITE(V3D_PCTRE, 0);
-	vc4->active_perfmon = NULL;
+	vc4->perfmon_state.active = NULL;
 }
 
 struct vc4_perfmon *vc4_perfmon_find(struct vc4_file *vc4file, int id)
@@ -116,8 +128,7 @@ static void vc4_perfmon_delete(struct vc4_file *vc4file,
 	struct vc4_dev *vc4 = vc4file->dev;
 
 	/* If the active perfmon is being destroyed, stop it first */
-	if (perfmon == vc4->active_perfmon)
-		vc4_perfmon_stop(vc4, perfmon, false);
+	vc4_perfmon_stop(vc4, perfmon, false);
 
 	vc4_perfmon_put(perfmon);
 }
@@ -222,8 +233,10 @@ int vc4_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
 	struct vc4_dev *vc4 = to_vc4_dev(dev);
 	struct vc4_file *vc4file = file_priv->driver_priv;
 	struct drm_vc4_perfmon_get_values *req = data;
+	u64 values[DRM_VC4_MAX_PERF_COUNTERS];
 	struct vc4_perfmon *perfmon;
-	int ret;
+	size_t size;
+	int ret = 0;
 
 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
 		return -ENODEV;
@@ -237,11 +250,14 @@ int vc4_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
 	if (!perfmon)
 		return -EINVAL;
 
-	if (copy_to_user(u64_to_user_ptr(req->values_ptr), perfmon->counters,
-			 perfmon->ncounters * sizeof(u64)))
+	size = perfmon->ncounters * sizeof(u64);
+
+	/* Snapshot the counters under the perfmon lock */
+	scoped_guard(spinlock_irqsave, &vc4->perfmon_state.lock)
+		memcpy(values, perfmon->counters, size);
+
+	if (copy_to_user(u64_to_user_ptr(req->values_ptr), values, size))
 		ret = -EFAULT;
-	else
-		ret = 0;
 
 	vc4_perfmon_put(perfmon);
 	return ret;
Signature · 1 line
2.55.0
author reviewer
#15 lore raw

PATCH 10/11 drm/vc4: Protect the hang state with a dedicated lock

In reply to Maíra Canal

The saved GPU hang state is published by vc4_save_hang_state() and claimed by VC4_GET_HANG_STATE, and access to the pointer was serialized by job_lock. However, job_lock already has other attributions related with the jobs currently executed on hardware.

The hang state has no invariant in common with the jobs programmed into the hardware. vc4_save_hang_state() already drops job_lock between reading those jobs and publishing the dump, so the two critical sections are independent and nothing is made less atomic by separating them.

Give the hang state its own spinlock. The two locks are never nested, so this introduces no ordering requirement, and neither hang state path has to disable interrupts any more.

---
 drivers/gpu/drm/vc4/vc4_drv.h |  2 ++
 drivers/gpu/drm/vc4/vc4_gem.c | 16 ++++++++--------
 2 files changed, 10 insertions(+), 8 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +2 -0
index 2fad8a489558..c6ae54d2e8b8 100644
@@ -99,6 +99,8 @@ struct vc4_dev {
 	struct vc4_hvs *hvs;
 	struct vc4_v3d *v3d;
 
+	/* Protects @hang_state. */
+	spinlock_t hang_state_lock;
 	struct vc4_hang_state *hang_state;
 
 	/* The kernel-space BO cache.  Tracks buffers that have been
drivers/gpu/drm/vc4/vc4_gem.c +8 -8
index 1f3cb3c2c7fc..230558b3f684 100644
@@ -74,7 +74,6 @@ vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
 	struct vc4_hang_state *kernel_state;
 	struct drm_vc4_get_hang_state *state;
 	struct vc4_dev *vc4 = to_vc4_dev(dev);
-	unsigned long irqflags;
 	u32 i;
 	int ret = 0;
 
@@ -86,10 +85,10 @@ vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
 		return -ENODEV;
 	}
 
-	spin_lock_irqsave(&vc4->job_lock, irqflags);
+	spin_lock(&vc4->hang_state_lock);
 	kernel_state = vc4->hang_state;
 	if (!kernel_state) {
-		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
+		spin_unlock(&vc4->hang_state_lock);
 		return -ENOENT;
 	}
 	state = &kernel_state->user_state;
@@ -99,12 +98,12 @@ vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
 	 */
 	if (get_state->bo_count < state->bo_count) {
 		get_state->bo_count = state->bo_count;
-		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
+		spin_unlock(&vc4->hang_state_lock);
 		return 0;
 	}
 
 	vc4->hang_state = NULL;
-	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
+	spin_unlock(&vc4->hang_state_lock);
 
 	/* Save the user's BO pointer, so we don't stomp it with the memcpy. */
 	state->bo = get_state->bo;
@@ -273,13 +272,13 @@ vc4_save_hang_state(struct drm_device *dev)
 		mutex_unlock(&bo->madv_lock);
 	}
 
-	spin_lock_irqsave(&vc4->job_lock, irqflags);
+	spin_lock(&vc4->hang_state_lock);
 	if (vc4->hang_state) {
-		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
+		spin_unlock(&vc4->hang_state_lock);
 		vc4_free_hang_state(dev, kernel_state);
 	} else {
 		vc4->hang_state = kernel_state;
-		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
+		spin_unlock(&vc4->hang_state_lock);
 	}
 
 	return;
@@ -1177,6 +1176,7 @@ int vc4_gem_init(struct drm_device *dev)
 	INIT_LIST_HEAD(&vc4->render_job_list);
 	INIT_LIST_HEAD(&vc4->job_done_list);
 	spin_lock_init(&vc4->job_lock);
+	spin_lock_init(&vc4->hang_state_lock);
 	spin_lock_init(&vc4->perfmon_state.lock);
 
 	INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
Signature · 1 line
2.55.0
author reviewer
#16 lore raw

PATCH 11/11 drm/vc4: Split out bin_alloc_lock

In reply to Maíra Canal

The binner slot pool, tracked by bin_alloc_used and bin_alloc_overflow, is serialized by job_lock. The pool has a lifecycle of its own: slots are claimed by bin_slot_try_alloc(), which the submit path retries from a wait loop, and released from the render done handler, from vc4_release_exec() and from the overflow worker. Each of those takes the device-wide job lock to update the binner slot pool, contending the job queues.

Give the pool its own spinlock. Note that, as vc4_irq_finish_render_job() frees the finished job's slots and vc4_overflow_mem_work() hand slots over to a queued job, they hold both locks, nesting bin_alloc_lock inside job_lock. However, nothing takes them in the other order.

That leaves vc4_overflow_mem_work() holding job_lock over the binner overflow registers, which it never needed, as the only other writers of V3D_BPOA and V3D_BPOS are the bind and unbind paths, which run with the interrupt uninstalled. Write to them once the lock is dropped.

---
 drivers/gpu/drm/vc4/vc4_drv.h |  3 +++
 drivers/gpu/drm/vc4/vc4_gem.c |  7 +++----
 drivers/gpu/drm/vc4/vc4_irq.c | 46 ++++++++++++++++++++++---------------------
 drivers/gpu/drm/vc4/vc4_v3d.c |  2 +-
 4 files changed, 31 insertions(+), 27 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +3 -0
index c6ae54d2e8b8..e2cc5bb78791 100644
@@ -204,6 +204,9 @@ struct vc4_dev {
 	/* Size of blocks allocated within bin_bo. */
 	uint32_t bin_alloc_size;
 
+	/* Protects @bin_alloc_used and @bin_alloc_overflow. */
+	spinlock_t bin_alloc_lock;
+
 	/* Bitmask of the bin_alloc_size chunks in bin_bo that are
 	 * used.
 	 */
drivers/gpu/drm/vc4/vc4_gem.c +3 -4
index 230558b3f684..09aaa1b796ad 100644
@@ -846,7 +846,6 @@ vc4_release_exec(struct kref *ref)
 	struct vc4_exec_info *exec = container_of(ref, struct vc4_exec_info,
 						  refcount);
 	struct vc4_dev *vc4 = exec->dev;
-	unsigned long irqflags;
 	unsigned i;
 
 	/* The render done handler signals the fence, so only drop the
@@ -875,9 +874,8 @@ vc4_release_exec(struct kref *ref)
 	 * completion had their slots released in vc4_irq_finish_render_job().
 	 * Only jobs that never completed still have slots to be released here.
 	 */
-	spin_lock_irqsave(&vc4->job_lock, irqflags);
-	vc4->bin_alloc_used &= ~exec->bin_slots;
-	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
+	scoped_guard(spinlock_irqsave, &vc4->bin_alloc_lock)
+		vc4->bin_alloc_used &= ~exec->bin_slots;
 
 	/* Let anyone waiting on the binner pool retry. */
 	wake_up_all(&vc4->job_wait_queue);
@@ -1176,6 +1174,7 @@ int vc4_gem_init(struct drm_device *dev)
 	INIT_LIST_HEAD(&vc4->render_job_list);
 	INIT_LIST_HEAD(&vc4->job_done_list);
 	spin_lock_init(&vc4->job_lock);
+	spin_lock_init(&vc4->bin_alloc_lock);
 	spin_lock_init(&vc4->hang_state_lock);
 	spin_lock_init(&vc4->perfmon_state.lock);
 
drivers/gpu/drm/vc4/vc4_irq.c +24 -22

73 lines not on this page · raw message

drivers/gpu/drm/vc4/vc4_v3d.c +1 -1
index 8a3c90136506..b4bb90765795 100644
@@ -177,7 +177,7 @@ static int bin_slot_try_alloc(struct vc4_dev *vc4)
 {
 	int slot;
 
-	guard(spinlock_irqsave)(&vc4->job_lock);
+	guard(spinlock_irqsave)(&vc4->bin_alloc_lock);
 
 	slot = ffs(~vc4->bin_alloc_used);
 	if (slot != 0) {
Signature · 1 line
2.55.0
Reviewed-by on the v1 series
#17 lore raw
In reply to Maíra Canal

This looks good to me, I asked a couple of questions about patches 1 and 5, everything else is:

Quoted 1 line · Maíra Canal

(...)

Quoted 10 lines

On a Raspberry Pi 3B+ running glmark2-wayland --off-screen, lock_stat
shows job_lock contentions reduced by 90% (2093 -> 207 per run) and total
wait time by 96% (14.7 ms -> 0.6 ms). The longest job_lock hold, during
which interrupts are disabled, drops from 747 us to 27 us, as the render
done handler no longer signals fences under the lock. The workload is GPU-bound, so the glmark2 score is unchanged (239.6 vs 240.2) [1].

If you want to force a CPU bound scenario you may be able to achieve that by dropping output resolution to a minimum (passing something like -s 32x32) . That would make GPU-side work very small and CPU work might become dominant.

Iago

Quoted 53 lines

About 99k acquisitions per run move off job_lock onto the per-fence locks,
and 185k more onto bin_alloc_lock, with little contention on either.

Considering that glmark2 is a GPU-bound client, it's important to notice
that contention improvements might be higher with several clients or a
CPU-bound workload, which I did not measure.

This series applies on top of "drm/vc4: Fix binner slot allocation failing
on an idle GPU" [2].

[1] glmark2-wayland --off-screen scores are the mean of 5 runs per kernel
without CONFIG_LOCK_STAT enabled. The lock data is the mean of 3 runs from
CONFIG_LOCK_STAT builds.

[2]
https://lore.kernel.org/dri-devel/[email protected]/T/

Best regards,
- Maíra

---
Maíra Canal (11):
      drm/vc4: Publish the output fence outside of job_lock drm/vc4: Drop the BO reservations outside of job_lock drm/vc4: Refcount vc4_exec_info
      drm/vc4: Return the seqno of the submitted job drm/vc4: Attach the job fences outside of job_lock drm/vc4: Use the inline fence lock
      drm/vc4: Signal the render fence outside of job_lock drm/vc4: Take job_lock only once to drain job_done_list drm/vc4: Protect perfmon state with a dedicated lock drm/vc4: Protect the hang state with a dedicated lock drm/vc4: Split out bin_alloc_lock

 drivers/gpu/drm/vc4/vc4_drv.h     |  20 +++++-- drivers/gpu/drm/vc4/vc4_gem.c     | 106 +++++++++++++++++++---------
----------
 drivers/gpu/drm/vc4/vc4_irq.c     |  80 ++++++++++++++++------------ drivers/gpu/drm/vc4/vc4_perfmon.c |  40 +++++++++----- drivers/gpu/drm/vc4/vc4_v3d.c     |   2 +-

 5 files changed, 146 insertions(+), 102 deletions(-)

---
base-commit: 64eded63d88ea70466c57cf433761bb1efea37a4
change-id: 20261002-vc4-misc-fixes-2b313622f570

author reviewer Applied
#18 lore raw
In reply to Maíra Canal
Quoted 10 lines · Maíra Canal

vc4->job_lock is a device-wide spinlock taken by the V3D interrupt handler, so everything under it runs with interrupts disabled. It currently protects several unrelated things: job queues, the binner slot pool, the saved hang state, the active perfmon, the attachment of the job fence to every BO, and more.

This series aims to (1) split vc4->job_lock into multiple independent locks and (2) shrinks its critical sections when possible.

[...]

Applied to drm-misc-fixes, thanks!

[01/11] drm/vc4: Publish the output fence outside of job_lock

        commit: 9ee552a42c3905b4547e83173d46413137ec329a
[02/11] drm/vc4: Drop the BO reservations outside of job_lock
        commit: 8ef66514d7b993c9fb96a5886f001e3556610c1c

I'll apply the rest of the patches to drm-misc-next after the merge window.

Best regards,
- Maíra

author reviewer Applied
#19 lore raw
In reply to Maíra Canal
Quoted 10 lines · Maíra Canal

vc4->job_lock is a device-wide spinlock taken by the V3D interrupt handler, so everything under it runs with interrupts disabled. It currently protects several unrelated things: job queues, the binner slot pool, the saved hang state, the active perfmon, the attachment of the job fence to every BO, and more.

This series aims to (1) split vc4->job_lock into multiple independent locks and (2) shrinks its critical sections when possible.

[...]

Applied, thanks!

[03/11] drm/vc4: Refcount vc4_exec_info

        commit: 66190d52ce5100320b29b2237e83b7b8d7cc56ee
[04/11] drm/vc4: Return the seqno of the submitted job
        commit: d5e814a3fa2579bb69058f1e88f521f89d5beca1
[05/11] drm/vc4: Attach the job fences outside of job_lock
        commit: 0abc50c2c69ff6cf7dc0f28d0b43c1eb0e63bef6
[06/11] drm/vc4: Use the inline fence lock
        commit: 3e7768d8a8ad9d916cf19b1379602c9d20a9ac40
[07/11] drm/vc4: Signal the render fence outside of job_lock
        commit: 4f0f809738e8d801d283518340da8d128e775c8c
[08/11] drm/vc4: Take job_lock only once to drain job_done_list
        commit: d88f725d8bac6a66c96a12f98afe406252294ff7
[09/11] drm/vc4: Protect perfmon state with a dedicated lock
        commit: 2898d618761173cb27787dbe51e6baca2fef268e
[10/11] drm/vc4: Protect the hang state with a dedicated lock
        commit: fed35e1ce274e0c81122f6869d8ff45f41efb586
[11/11] drm/vc4: Split out bin_alloc_lock
        commit: 851a478fa1ef6e63f5260d8fa6b228d9ca893952

Best regards,
- Maíra