Skip to contentSkip to conversation

drm/v3d: Fix GPU stats inconsistancies and race-condition

PATCH Superseded Maíra Canal 3 participants 16 messages Apr 3, 2024 – Apr 16, 2024

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Maíra Canal

author maintainer
#0 lore raw

cover drm/v3d: Fix GPU stats inconsistancies and race-condition

This series addresses two major issues on the GPU stats:

1. Currently, we are incrementing enabled_ns twice by the end of each job.
2. There is a race-condition between the IRQ handler and the users

Apart from addressing this issues, this series improved the GPU stats code as a hole. We reduced code repetition as a hole, creating functions to start and update the GPU stats. This will likely reduce the odds of issue #1 happen again.

Note that I incrementally improved the code, creating small atomics commits to ease the reviewing process. Also, I separated the first patch, that has the fix to issue #1, in order to keep the fix separated from code improvements.

The issue #1 is addressed on the first patch, while issue #2 is addressed in the last patch. Patches #2 to #4 are code improvements.

Best Regards,
- Maíra

Maíra Canal (5):
drm/v3d: Don't increment enabled_ns twice
drm/v3d: Create two functions to update all GPU stats variables drm/v3d: Create a struct to store the GPU stats
drm/v3d: Create function to update a set of GPU stats drm/v3d: Fix race-condition between sysfs/fdinfo and interrupt handler

 drivers/gpu/drm/v3d/v3d_drv.c   | 24 +++++---
 drivers/gpu/drm/v3d/v3d_drv.h   | 26 ++++++---
 drivers/gpu/drm/v3d/v3d_gem.c   |  9 +--
 drivers/gpu/drm/v3d/v3d_irq.c   | 52 ++----------------
 drivers/gpu/drm/v3d/v3d_sched.c | 97 ++++++++++++++++++---------------
 drivers/gpu/drm/v3d/v3d_sysfs.c | 18 +++---
 6 files changed, 104 insertions(+), 122 deletions(-)

--
2.44.0

author maintainer
#1 lore raw

PATCH 1/5 drm/v3d: Don't increment `enabled_ns` twice

The commit 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs") introduced the calculation of global GPU stats. For the regards, it used the already existing infrastructure provided by commit 09a93cc4f7d1 ("drm/v3d:
Implement show_fdinfo() callback for GPU usage stats"). While adding global GPU stats calculation ability, the author forgot to delete the existing one.

Currently, the value of enabled_ns is incremented twice by the end of the job, when it should be added just once. Therefore, delete the leftovers from commit 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs").

Fixes 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs")
---
 drivers/gpu/drm/v3d/v3d_irq.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/gpu/drm/v3d/v3d_irq.c +0 -4
index 2e04f6cb661e..ce6b2fb341d1 100644
@@ -105,7 +105,6 @@ v3d_irq(int irq, void *arg)
 		struct v3d_file_priv *file = v3d->bin_job->base.file->driver_priv;
 		u64 runtime = local_clock() - file->start_ns[V3D_BIN];
 
-		file->enabled_ns[V3D_BIN] += local_clock() - file->start_ns[V3D_BIN];
 		file->jobs_sent[V3D_BIN]++;
 		v3d->queue[V3D_BIN].jobs_sent++;
 
@@ -126,7 +125,6 @@ v3d_irq(int irq, void *arg)
 		struct v3d_file_priv *file = v3d->render_job->base.file->driver_priv;
 		u64 runtime = local_clock() - file->start_ns[V3D_RENDER];
 
-		file->enabled_ns[V3D_RENDER] += local_clock() - file->start_ns[V3D_RENDER];
 		file->jobs_sent[V3D_RENDER]++;
 		v3d->queue[V3D_RENDER].jobs_sent++;
 
@@ -147,7 +145,6 @@ v3d_irq(int irq, void *arg)
 		struct v3d_file_priv *file = v3d->csd_job->base.file->driver_priv;
 		u64 runtime = local_clock() - file->start_ns[V3D_CSD];
 
-		file->enabled_ns[V3D_CSD] += local_clock() - file->start_ns[V3D_CSD];
 		file->jobs_sent[V3D_CSD]++;
 		v3d->queue[V3D_CSD].jobs_sent++;
 
@@ -195,7 +192,6 @@ v3d_hub_irq(int irq, void *arg)
 		struct v3d_file_priv *file = v3d->tfu_job->base.file->driver_priv;
 		u64 runtime = local_clock() - file->start_ns[V3D_TFU];
 
-		file->enabled_ns[V3D_TFU] += local_clock() - file->start_ns[V3D_TFU];
 		file->jobs_sent[V3D_TFU]++;
 		v3d->queue[V3D_TFU].jobs_sent++;
 
Signature · 1 line
2.44.0
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] drm/v3d: Don't increment `enabled_ns` twice

Quoted 48 lines · Maíra Canal

The commit 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs") introduced the calculation of global GPU stats. For the regards, it used the already existing infrastructure provided by commit 09a93cc4f7d1 ("drm/v3d:
Implement show_fdinfo() callback for GPU usage stats"). While adding global GPU stats calculation ability, the author forgot to delete the existing one.

Currently, the value of enabled_ns is incremented twice by the end of the job, when it should be added just once. Therefore, delete the leftovers from commit 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs").

Fixes 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs")
---
  drivers/gpu/drm/v3d/v3d_irq.c | 4 ----
  1 file changed, 4 deletions(-)
drivers/gpu/drm/v3d/v3d_irq.c +0 -3
index 2e04f6cb661e..ce6b2fb341d1 100644
@@ -105,7 +105,6 @@ v3d_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->bin_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_BIN];
 
-		file->enabled_ns[V3D_BIN] += local_clock() - file->start_ns[V3D_BIN];
  		file->jobs_sent[V3D_BIN]++;
  		v3d->queue[V3D_BIN].jobs_sent++;
 
@@ -126,7 +125,6 @@ v3d_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->render_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_RENDER];
 
-		file->enabled_ns[V3D_RENDER] += local_clock() - file->start_ns[V3D_RENDER];
  		file->jobs_sent[V3D_RENDER]++;
  		v3d->queue[V3D_RENDER].jobs_sent++;
 
@@ -147,7 +145,6 @@ v3d_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->csd_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_CSD];
 
-		file->enabled_ns[V3D_CSD] += local_clock() - file->start_ns[V3D_CSD];
  		file->jobs_sent[V3D_CSD]++;
  		v3d->queue[V3D_CSD].jobs_sent++;
 

drivers/gpu/drm/v3d/v3d_irq.c v3d_hub_irq(int irq, void *arg) · line 197

Quoted 7 lines
drivers/gpu/drm/v3d/v3d_irq.c +0 -1
@@ -195,7 +192,6 @@ v3d_hub_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->tfu_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_TFU];
 
-		file->enabled_ns[V3D_TFU] += local_clock() - file->start_ns[V3D_TFU];
  		file->jobs_sent[V3D_TFU]++;
  		v3d->queue[V3D_TFU].jobs_sent++;
 

Regards,

Tvrtko

Reviewed-by on v1 1/5
#3 lore raw

Re: [PATCH 1/5] drm/v3d: Don't increment `enabled_ns` twice

In reply to Maíra Canal
Quoted 48 lines · Maíra Canal

The commit 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs") introduced the calculation of global GPU stats. For the regards, it used the already existing infrastructure provided by commit 09a93cc4f7d1 ("drm/v3d:
Implement show_fdinfo() callback for GPU usage stats"). While adding global GPU stats calculation ability, the author forgot to delete the existing one.

Currently, the value of enabled_ns is incremented twice by the end of the job, when it should be added just once. Therefore, delete the leftovers from commit 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs").

Fixes 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs")
---
  drivers/gpu/drm/v3d/v3d_irq.c | 4 ----
  1 file changed, 4 deletions(-)
drivers/gpu/drm/v3d/v3d_irq.c +0 -3
index 2e04f6cb661e..ce6b2fb341d1 100644
@@ -105,7 +105,6 @@ v3d_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->bin_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_BIN];
 
-		file->enabled_ns[V3D_BIN] += local_clock() - file->start_ns[V3D_BIN];
  		file->jobs_sent[V3D_BIN]++;
  		v3d->queue[V3D_BIN].jobs_sent++;
 
@@ -126,7 +125,6 @@ v3d_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->render_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_RENDER];
 
-		file->enabled_ns[V3D_RENDER] += local_clock() - file->start_ns[V3D_RENDER];
  		file->jobs_sent[V3D_RENDER]++;
  		v3d->queue[V3D_RENDER].jobs_sent++;
 
@@ -147,7 +145,6 @@ v3d_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->csd_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_CSD];
 
-		file->enabled_ns[V3D_CSD] += local_clock() - file->start_ns[V3D_CSD];
  		file->jobs_sent[V3D_CSD]++;
  		v3d->queue[V3D_CSD].jobs_sent++;
 

drivers/gpu/drm/v3d/v3d_irq.c v3d_hub_irq(int irq, void *arg) · line 197

Quoted 7 lines
drivers/gpu/drm/v3d/v3d_irq.c +0 -1
@@ -195,7 +192,6 @@ v3d_hub_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->tfu_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_TFU];
 
-		file->enabled_ns[V3D_TFU] += local_clock() - file->start_ns[V3D_TFU];
  		file->jobs_sent[V3D_TFU]++;
  		v3d->queue[V3D_TFU].jobs_sent++;
 

Thanks for fixing this. I see that I included this error in my first refactoring of the original patch.

#4 lore raw

Re: [PATCH 1/5] drm/v3d: Don't increment `enabled_ns` twice

Hi,

Quoted 47 lines · Chema Casanova
Quoted 46 lines · Maíra Canal

The commit 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs") introduced the calculation of global GPU stats. For the regards, it used the already existing infrastructure provided by commit 09a93cc4f7d1 ("drm/v3d:
Implement show_fdinfo() callback for GPU usage stats"). While adding global GPU stats calculation ability, the author forgot to delete the existing one.

Currently, the value of enabled_ns is incremented twice by the end of the job, when it should be added just once. Therefore, delete the leftovers from commit 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs").

Fixes 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs")

---
  drivers/gpu/drm/v3d/v3d_irq.c | 4 ----

  1 file changed, 4 deletions(-)
drivers/gpu/drm/v3d/v3d_irq.c +0 -3
index 2e04f6cb661e..ce6b2fb341d1 100644
@@ -105,7 +105,6 @@ v3d_irq(int irq, void *arg)
           struct v3d_file_priv *file = v3d->bin_job->base.file->driver_priv;
           u64 runtime = local_clock() - file->start_ns[V3D_BIN];
-        file->enabled_ns[V3D_BIN] += local_clock() - 
 file->start_ns[V3D_BIN];
           file->jobs_sent[V3D_BIN]++;
           v3d->queue[V3D_BIN].jobs_sent++;
@@ -126,7 +125,6 @@ v3d_irq(int irq, void *arg)
           struct v3d_file_priv *file = v3d->render_job->base.file->driver_priv;
           u64 runtime = local_clock() - file->start_ns[V3D_RENDER];
-        file->enabled_ns[V3D_RENDER] += local_clock() - 
 file->start_ns[V3D_RENDER];
           file->jobs_sent[V3D_RENDER]++;
           v3d->queue[V3D_RENDER].jobs_sent++;
@@ -147,7 +145,6 @@ v3d_irq(int irq, void *arg)
           struct v3d_file_priv *file = v3d->csd_job->base.file->driver_priv;
           u64 runtime = local_clock() - file->start_ns[V3D_CSD];
-        file->enabled_ns[V3D_CSD] += local_clock() - 
 file->start_ns[V3D_CSD];
           file->jobs_sent[V3D_CSD]++;
           v3d->queue[V3D_CSD].jobs_sent++;
@@ -195,7 +192,6 @@ v3d_hub_irq(int irq, void *arg)
           struct v3d_file_priv *file = v3d->tfu_job->base.file->driver_priv;
Quoted 8 lines

drivers/gpu/drm/v3d/v3d_irq.c v3d_hub_irq(int irq, void *arg) · line 196

Quoted 5 lines
drivers/gpu/drm/v3d/v3d_irq.c +0 -1
           u64 runtime = local_clock() - file->start_ns[V3D_TFU];
-        file->enabled_ns[V3D_TFU] += local_clock() - 
 file->start_ns[V3D_TFU];
           file->jobs_sent[V3D_TFU]++;
           v3d->queue[V3D_TFU].jobs_sent++;

Thanks for fixing this. I see that I included this error in my first refactoring of the original patch.

Not sure if it would be worth creating a simple test like https://gitlab.freedesktop.org/drm/igt-gpu-tools/-/commit/2f81ed3aed873c7cc2f6d0e1117fa4fb02033246 for i915? Just a thought.

Regards,

Tvrtko

author maintainer
#5 lore raw

Re: [PATCH 1/5] drm/v3d: Don't increment `enabled_ns` twice

In reply to Maíra Canal
Quoted 7 lines · Maíra Canal

The commit 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs") introduced the calculation of global GPU stats. For the regards, it used the already existing infrastructure provided by commit 09a93cc4f7d1 ("drm/v3d:
Implement show_fdinfo() callback for GPU usage stats"). While adding global GPU stats calculation ability, the author forgot to delete the existing one.

Quoted 8 lines

Currently, the value of enabled_ns is incremented twice by the end of the job, when it should be added just once. Therefore, delete the leftovers from commit 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs").

Fixes 509433d8146c ("drm/v3d: Expose the total GPU usage stats on sysfs")

As this patch is a isolated bugfix and it was reviewed by two developers, I'm applying it to drm-misc/drm-misc-fixes.

I'll address the feedback for the rest of the series later and send a v2.

Best Regards,
- Maíra

Quoted 40 lines
---
  drivers/gpu/drm/v3d/v3d_irq.c | 4 ----
  1 file changed, 4 deletions(-)
drivers/gpu/drm/v3d/v3d_irq.c +0 -4
index 2e04f6cb661e..ce6b2fb341d1 100644
@@ -105,7 +105,6 @@ v3d_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->bin_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_BIN];
 
-		file->enabled_ns[V3D_BIN] += local_clock() - file->start_ns[V3D_BIN];
  		file->jobs_sent[V3D_BIN]++;
  		v3d->queue[V3D_BIN].jobs_sent++;
 
@@ -126,7 +125,6 @@ v3d_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->render_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_RENDER];
 
-		file->enabled_ns[V3D_RENDER] += local_clock() - file->start_ns[V3D_RENDER];
  		file->jobs_sent[V3D_RENDER]++;
  		v3d->queue[V3D_RENDER].jobs_sent++;
 
@@ -147,7 +145,6 @@ v3d_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->csd_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_CSD];
 
-		file->enabled_ns[V3D_CSD] += local_clock() - file->start_ns[V3D_CSD];
  		file->jobs_sent[V3D_CSD]++;
  		v3d->queue[V3D_CSD].jobs_sent++;
 
@@ -195,7 +192,6 @@ v3d_hub_irq(int irq, void *arg)
  		struct v3d_file_priv *file = v3d->tfu_job->base.file->driver_priv;
  		u64 runtime = local_clock() - file->start_ns[V3D_TFU];
 
-		file->enabled_ns[V3D_TFU] += local_clock() - file->start_ns[V3D_TFU];
  		file->jobs_sent[V3D_TFU]++;
  		v3d->queue[V3D_TFU].jobs_sent++;
 
author maintainer
#6 lore raw

PATCH 2/5 drm/v3d: Create two functions to update all GPU stats variables

In reply to Maíra Canal

Currently, we manually perform all operations to update the GPU stats variables. Apart from the code repetition, this is very prone to errors, as we can see on the previous commit.

Therefore, create two functions to manage updating all GPU stats variables. Now, the jobs only need to call for v3d_job_update_stats() when the job is done and v3d_job_start_stats() when starting the job.

---
 drivers/gpu/drm/v3d/v3d_drv.h   |  1 +
 drivers/gpu/drm/v3d/v3d_irq.c   | 48 ++------------------
 drivers/gpu/drm/v3d/v3d_sched.c | 80 +++++++++++++++------------------
 3 files changed, 40 insertions(+), 89 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.h +1 -0
index 1950c723dde1..ee3545226d7f 100644
@@ -543,6 +543,7 @@ void v3d_mmu_insert_ptes(struct v3d_bo *bo);
 void v3d_mmu_remove_ptes(struct v3d_bo *bo);
 
 /* v3d_sched.c */
+void v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue);
 int v3d_sched_init(struct v3d_dev *v3d);
 void v3d_sched_fini(struct v3d_dev *v3d);
 
drivers/gpu/drm/v3d/v3d_irq.c +4 -44

76 lines not on this page · raw message

drivers/gpu/drm/v3d/v3d_sched.c +35 -45

159 lines not on this page · raw message

Signature · 1 line
2.44.0
Reviewed-by on v1 2/5
#7 lore raw

Re: [PATCH 2/5] drm/v3d: Create two functions to update all GPU stats variables

Quoted 277 lines · Maíra Canal

Currently, we manually perform all operations to update the GPU stats variables. Apart from the code repetition, this is very prone to errors, as we can see on the previous commit.

Therefore, create two functions to manage updating all GPU stats variables. Now, the jobs only need to call for v3d_job_update_stats() when the job is done and v3d_job_start_stats() when starting the job.

---
  drivers/gpu/drm/v3d/v3d_drv.h   |  1 +
  drivers/gpu/drm/v3d/v3d_irq.c   | 48 ++------------------
  drivers/gpu/drm/v3d/v3d_sched.c | 80 +++++++++++++++------------------
  3 files changed, 40 insertions(+), 89 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.h +1 -0
index 1950c723dde1..ee3545226d7f 100644
@@ -543,6 +543,7 @@ void v3d_mmu_insert_ptes(struct v3d_bo *bo);
  void v3d_mmu_remove_ptes(struct v3d_bo *bo);
 
  /* v3d_sched.c */
+void v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue);
  int v3d_sched_init(struct v3d_dev *v3d);
  void v3d_sched_fini(struct v3d_dev *v3d);
 
drivers/gpu/drm/v3d/v3d_irq.c +4 -44
index ce6b2fb341d1..d469bda52c1a 100644
@@ -102,18 +102,8 @@ v3d_irq(int irq, void *arg)
  	if (intsts & V3D_INT_FLDONE) {
  		struct v3d_fence *fence =
  			to_v3d_fence(v3d->bin_job->base.irq_fence);
-		struct v3d_file_priv *file = v3d->bin_job->base.file->driver_priv;
-		u64 runtime = local_clock() - file->start_ns[V3D_BIN];
-
-		file->jobs_sent[V3D_BIN]++;
-		v3d->queue[V3D_BIN].jobs_sent++;
-
-		file->start_ns[V3D_BIN] = 0;
-		v3d->queue[V3D_BIN].start_ns = 0;
-
-		file->enabled_ns[V3D_BIN] += runtime;
-		v3d->queue[V3D_BIN].enabled_ns += runtime;
 
+		v3d_job_update_stats(&v3d->bin_job->base, V3D_BIN);
  		trace_v3d_bcl_irq(&v3d->drm, fence->seqno);
  		dma_fence_signal(&fence->base);
  		status = IRQ_HANDLED;
@@ -122,18 +112,8 @@ v3d_irq(int irq, void *arg)
  	if (intsts & V3D_INT_FRDONE) {
  		struct v3d_fence *fence =
  			to_v3d_fence(v3d->render_job->base.irq_fence);
-		struct v3d_file_priv *file = v3d->render_job->base.file->driver_priv;
-		u64 runtime = local_clock() - file->start_ns[V3D_RENDER];
-
-		file->jobs_sent[V3D_RENDER]++;
-		v3d->queue[V3D_RENDER].jobs_sent++;
-
-		file->start_ns[V3D_RENDER] = 0;
-		v3d->queue[V3D_RENDER].start_ns = 0;
-
-		file->enabled_ns[V3D_RENDER] += runtime;
-		v3d->queue[V3D_RENDER].enabled_ns += runtime;
 
+		v3d_job_update_stats(&v3d->render_job->base, V3D_RENDER);
  		trace_v3d_rcl_irq(&v3d->drm, fence->seqno);
  		dma_fence_signal(&fence->base);
  		status = IRQ_HANDLED;
@@ -142,18 +122,8 @@ v3d_irq(int irq, void *arg)
  	if (intsts & V3D_INT_CSDDONE(v3d->ver)) {
  		struct v3d_fence *fence =
  			to_v3d_fence(v3d->csd_job->base.irq_fence);
-		struct v3d_file_priv *file = v3d->csd_job->base.file->driver_priv;
-		u64 runtime = local_clock() - file->start_ns[V3D_CSD];
-
-		file->jobs_sent[V3D_CSD]++;
-		v3d->queue[V3D_CSD].jobs_sent++;
-
-		file->start_ns[V3D_CSD] = 0;
-		v3d->queue[V3D_CSD].start_ns = 0;
-
-		file->enabled_ns[V3D_CSD] += runtime;
-		v3d->queue[V3D_CSD].enabled_ns += runtime;
 
+		v3d_job_update_stats(&v3d->csd_job->base, V3D_CSD);
  		trace_v3d_csd_irq(&v3d->drm, fence->seqno);
  		dma_fence_signal(&fence->base);
  		status = IRQ_HANDLED;
@@ -189,18 +159,8 @@ v3d_hub_irq(int irq, void *arg)
  	if (intsts & V3D_HUB_INT_TFUC) {
  		struct v3d_fence *fence =
  			to_v3d_fence(v3d->tfu_job->base.irq_fence);
-		struct v3d_file_priv *file = v3d->tfu_job->base.file->driver_priv;
-		u64 runtime = local_clock() - file->start_ns[V3D_TFU];
-
-		file->jobs_sent[V3D_TFU]++;
-		v3d->queue[V3D_TFU].jobs_sent++;
-
-		file->start_ns[V3D_TFU] = 0;
-		v3d->queue[V3D_TFU].start_ns = 0;
-
-		file->enabled_ns[V3D_TFU] += runtime;
-		v3d->queue[V3D_TFU].enabled_ns += runtime;
 
+		v3d_job_update_stats(&v3d->tfu_job->base, V3D_TFU);
  		trace_v3d_tfu_irq(&v3d->drm, fence->seqno);
  		dma_fence_signal(&fence->base);
  		status = IRQ_HANDLED;
drivers/gpu/drm/v3d/v3d_sched.c +34 -39
index 54015ad765c7..8ca61bcd4b1c 100644
@@ -105,11 +105,37 @@ v3d_switch_perfmon(struct v3d_dev *v3d, struct v3d_job *job)
  		v3d_perfmon_start(v3d, job->perfmon);
  }
 
+static void
+v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
+{
+	struct v3d_dev *v3d = job->v3d;
+	struct v3d_file_priv *file = job->file->driver_priv;
+	u64 now = local_clock();
+
+	file->start_ns[queue] = now;
+	v3d->queue[queue].start_ns = now;
+}
+
+void
+v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
+{
+	struct v3d_dev *v3d = job->v3d;
+	struct v3d_file_priv *file = job->file->driver_priv;
+	u64 now = local_clock();
+
+	file->enabled_ns[queue] += now - file->start_ns[queue];
+	file->jobs_sent[queue]++;
+	file->start_ns[queue] = 0;
+
+	v3d->queue[queue].enabled_ns += now - v3d->queue[queue].start_ns;
+	v3d->queue[queue].jobs_sent++;
+	v3d->queue[queue].start_ns = 0;
+}
+
  static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
  {
  	struct v3d_bin_job *job = to_bin_job(sched_job);
  	struct v3d_dev *v3d = job->base.v3d;
-	struct v3d_file_priv *file = job->base.file->driver_priv;
  	struct drm_device *dev = &v3d->drm;
  	struct dma_fence *fence;
  	unsigned long irqflags;
@@ -141,9 +167,7 @@ static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
  	trace_v3d_submit_cl(dev, false, to_v3d_fence(fence)->seqno,
  			    job->start, job->end);
 
-	file->start_ns[V3D_BIN] = local_clock();
-	v3d->queue[V3D_BIN].start_ns = file->start_ns[V3D_BIN];
-
+	v3d_job_start_stats(&job->base, V3D_BIN);
  	v3d_switch_perfmon(v3d, &job->base);
 
  	/* Set the current and end address of the control list.
@@ -168,7 +192,6 @@ static struct dma_fence *v3d_render_job_run(struct drm_sched_job *sched_job)
  {
  	struct v3d_render_job *job = to_render_job(sched_job);
  	struct v3d_dev *v3d = job->base.v3d;
-	struct v3d_file_priv *file = job->base.file->driver_priv;
  	struct drm_device *dev = &v3d->drm;
  	struct dma_fence *fence;
 
@@ -196,9 +219,7 @@ static struct dma_fence *v3d_render_job_run(struct drm_sched_job *sched_job)
  	trace_v3d_submit_cl(dev, true, to_v3d_fence(fence)->seqno,
  			    job->start, job->end);
 
-	file->start_ns[V3D_RENDER] = local_clock();
-	v3d->queue[V3D_RENDER].start_ns = file->start_ns[V3D_RENDER];
-
+	v3d_job_start_stats(&job->base, V3D_RENDER);
  	v3d_switch_perfmon(v3d, &job->base);
 
  	/* XXX: Set the QCFG */
@@ -217,7 +238,6 @@ v3d_tfu_job_run(struct drm_sched_job *sched_job)
  {
  	struct v3d_tfu_job *job = to_tfu_job(sched_job);
  	struct v3d_dev *v3d = job->base.v3d;
-	struct v3d_file_priv *file = job->base.file->driver_priv;
  	struct drm_device *dev = &v3d->drm;
  	struct dma_fence *fence;
 
@@ -232,8 +252,7 @@ v3d_tfu_job_run(struct drm_sched_job *sched_job)
 
  	trace_v3d_submit_tfu(dev, to_v3d_fence(fence)->seqno);
 
-	file->start_ns[V3D_TFU] = local_clock();
-	v3d->queue[V3D_TFU].start_ns = file->start_ns[V3D_TFU];
+	v3d_job_start_stats(&job->base, V3D_TFU);
 
  	V3D_WRITE(V3D_TFU_IIA(v3d->ver), job->args.iia);
  	V3D_WRITE(V3D_TFU_IIS(v3d->ver), job->args.iis);
@@ -260,7 +279,6 @@ v3d_csd_job_run(struct drm_sched_job *sched_job)
  {
  	struct v3d_csd_job *job = to_csd_job(sched_job);
  	struct v3d_dev *v3d = job->base.v3d;
-	struct v3d_file_priv *file = job->base.file->driver_priv;
  	struct drm_device *dev = &v3d->drm;
  	struct dma_fence *fence;
  	int i, csd_cfg0_reg, csd_cfg_reg_count;
@@ -279,9 +297,7 @@ v3d_csd_job_run(struct drm_sched_job *sched_job)
 
  	trace_v3d_submit_csd(dev, to_v3d_fence(fence)->seqno);
 
-	file->start_ns[V3D_CSD] = local_clock();
-	v3d->queue[V3D_CSD].start_ns = file->start_ns[V3D_CSD];
-
+	v3d_job_start_stats(&job->base, V3D_CSD);
  	v3d_switch_perfmon(v3d, &job->base);
 
  	csd_cfg0_reg = V3D_CSD_QUEUED_CFG0(v3d->ver);
@@ -530,8 +546,6 @@ v3d_cpu_job_run(struct drm_sched_job *sched_job)
  {
  	struct v3d_cpu_job *job = to_cpu_job(sched_job);
  	struct v3d_dev *v3d = job->base.v3d;
-	struct v3d_file_priv *file = job->base.file->driver_priv;
-	u64 runtime;
 
  	v3d->cpu_job = job;
 
@@ -540,25 +554,13 @@ v3d_cpu_job_run(struct drm_sched_job *sched_job)
  		return NULL;
  	}
 
-	file->start_ns[V3D_CPU] = local_clock();
-	v3d->queue[V3D_CPU].start_ns = file->start_ns[V3D_CPU];
-
+	v3d_job_start_stats(&job->base, V3D_CPU);
  	trace_v3d_cpu_job_begin(&v3d->drm, job->job_type);
 
  	cpu_job_function[job->job_type](job);
 
  	trace_v3d_cpu_job_end(&v3d->drm, job->job_type);
-
-	runtime = local_clock() - file->start_ns[V3D_CPU];
-
-	file->enabled_ns[V3D_CPU] += runtime;
-	v3d->queue[V3D_CPU].enabled_ns += runtime;
-
-	file->jobs_sent[V3D_CPU]++;
-	v3d->queue[V3D_CPU].jobs_sent++;
-
-	file->start_ns[V3D_CPU] = 0;
-	v3d->queue[V3D_CPU].start_ns = 0;
+	v3d_job_update_stats(&job->base, V3D_CPU);
 
  	return NULL;
  }
@@ -568,24 +570,12 @@ v3d_cache_clean_job_run(struct drm_sched_job *sched_job)
  {
  	struct v3d_job *job = to_v3d_job(sched_job);
  	struct v3d_dev *v3d = job->v3d;
-	struct v3d_file_priv *file = job->file->driver_priv;
-	u64 runtime;
 
-	file->start_ns[V3D_CACHE_CLEAN] = local_clock();
-	v3d->queue[V3D_CACHE_CLEAN].start_ns = file->start_ns[V3D_CACHE_CLEAN];
+	v3d_job_start_stats(job, V3D_CACHE_CLEAN);
 
  	v3d_clean_caches(v3d);
 
-	runtime = local_clock() - file->start_ns[V3D_CACHE_CLEAN];
-
-	file->enabled_ns[V3D_CACHE_CLEAN] += runtime;
-	v3d->queue[V3D_CACHE_CLEAN].enabled_ns += runtime;

drivers/gpu/drm/v3d/v3d_sched.c v3d_cache_clean_job_run(struct drm_sched_job *sched_job) · line 581

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_sched.c +1 -6
-
-	file->jobs_sent[V3D_CACHE_CLEAN]++;
-	v3d->queue[V3D_CACHE_CLEAN].jobs_sent++;
-
-	file->start_ns[V3D_CACHE_CLEAN] = 0;
-	v3d->queue[V3D_CACHE_CLEAN].start_ns = 0;
+	v3d_job_update_stats(job, V3D_CACHE_CLEAN);
 
  	return NULL;
  }
author maintainer
#8 lore raw

PATCH 3/5 drm/v3d: Create a struct to store the GPU stats

In reply to Maíra Canal

This will make it easier to instantiate the GPU stats variables and it will create a structure where we can store all the variables that refer to GPU stats.

---
 drivers/gpu/drm/v3d/v3d_drv.c   | 14 ++++++--------
 drivers/gpu/drm/v3d/v3d_drv.h   | 18 ++++++++++--------
 drivers/gpu/drm/v3d/v3d_gem.c   |  4 +---
 drivers/gpu/drm/v3d/v3d_sched.c | 20 ++++++++++++--------
 drivers/gpu/drm/v3d/v3d_sysfs.c | 10 ++++++----
 5 files changed, 35 insertions(+), 31 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.c +6 -8
index 3debf37e7d9b..cbb62be18aa5 100644
@@ -115,14 +115,11 @@ v3d_open(struct drm_device *dev, struct drm_file *file)
 	v3d_priv->v3d = v3d;
 
 	for (i = 0; i < V3D_MAX_QUEUES; i++) {
-		v3d_priv->enabled_ns[i] = 0;
-		v3d_priv->start_ns[i] = 0;
-		v3d_priv->jobs_sent[i] = 0;
-
 		sched = &v3d->queue[i].sched;
 		drm_sched_entity_init(&v3d_priv->sched_entity[i],
 				      DRM_SCHED_PRIORITY_NORMAL, &sched,
 				      1, NULL);
+		memset(&v3d_priv->stats[i], 0, sizeof(v3d_priv->stats[i]));
 	}
 
 	v3d_perfmon_open_file(v3d_priv);
@@ -151,20 +148,21 @@ static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 	enum v3d_queue queue;
 
 	for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
+		struct v3d_stats *stats = &file_priv->stats[queue];
+
 		/* Note that, in case of a GPU reset, the time spent during an
 		 * attempt of executing the job is not computed in the runtime.
 		 */
 		drm_printf(p, "drm-engine-%s: \t%llu ns\n",
 			   v3d_queue_to_string(queue),
-			   file_priv->start_ns[queue] ? file_priv->enabled_ns[queue]
-						      + timestamp - file_priv->start_ns[queue]
-						      : file_priv->enabled_ns[queue]);
+			   stats->start_ns ? stats->enabled_ns + timestamp - stats->start_ns
+					   : stats->enabled_ns);
 
 		/* Note that we only count jobs that completed. Therefore, jobs
 		 * that were resubmitted due to a GPU reset are not computed.
 		 */
 		drm_printf(p, "v3d-jobs-%s: \t%llu jobs\n",
-			   v3d_queue_to_string(queue), file_priv->jobs_sent[queue]);
+			   v3d_queue_to_string(queue), stats->jobs_sent);
 	}
 }
 
drivers/gpu/drm/v3d/v3d_drv.h +10 -8
index ee3545226d7f..0117593976ed 100644
@@ -36,15 +36,20 @@ static inline char *v3d_queue_to_string(enum v3d_queue queue)
 	return "UNKNOWN";
 }
 
+struct v3d_stats {
+	u64 start_ns;
+	u64 enabled_ns;
+	u64 jobs_sent;
+};
+
 struct v3d_queue_state {
 	struct drm_gpu_scheduler sched;
 
 	u64 fence_context;
 	u64 emit_seqno;
 
-	u64 start_ns;
-	u64 enabled_ns;
-	u64 jobs_sent;
+	/* Stores the GPU stats for this queue in the global context. */
+	struct v3d_stats stats;
 };
 
 /* Performance monitor object. The perform lifetime is controlled by userspace
@@ -188,11 +193,8 @@ struct v3d_file_priv {
 
 	struct drm_sched_entity sched_entity[V3D_MAX_QUEUES];
 
-	u64 start_ns[V3D_MAX_QUEUES];
-
-	u64 enabled_ns[V3D_MAX_QUEUES];
-
-	u64 jobs_sent[V3D_MAX_QUEUES];
+	/* Stores the GPU stats for a specific queue for this fd. */
+	struct v3d_stats stats[V3D_MAX_QUEUES];
 };
 
 struct v3d_bo {
drivers/gpu/drm/v3d/v3d_gem.c +1 -3
index afc565078c78..d14589d3ae6c 100644
@@ -248,9 +248,7 @@ v3d_gem_init(struct drm_device *dev)
 
 	for (i = 0; i < V3D_MAX_QUEUES; i++) {
 		v3d->queue[i].fence_context = dma_fence_context_alloc(1);
-		v3d->queue[i].start_ns = 0;
-		v3d->queue[i].enabled_ns = 0;
-		v3d->queue[i].jobs_sent = 0;
+		memset(&v3d->queue[i].stats, 0, sizeof(v3d->queue[i].stats));
 	}
 
 	spin_lock_init(&v3d->mm_lock);
drivers/gpu/drm/v3d/v3d_sched.c +12 -8
index 8ca61bcd4b1c..ea5f5a84b55b 100644
@@ -110,10 +110,12 @@ v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
 {
 	struct v3d_dev *v3d = job->v3d;
 	struct v3d_file_priv *file = job->file->driver_priv;
+	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
+	struct v3d_stats *local_stats = &file->stats[queue];
 	u64 now = local_clock();
 
-	file->start_ns[queue] = now;
-	v3d->queue[queue].start_ns = now;
+	local_stats->start_ns = now;
+	global_stats->start_ns = now;
 }
 
 void
@@ -121,15 +123,17 @@ v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
 {
 	struct v3d_dev *v3d = job->v3d;
 	struct v3d_file_priv *file = job->file->driver_priv;
+	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
+	struct v3d_stats *local_stats = &file->stats[queue];
 	u64 now = local_clock();
 
-	file->enabled_ns[queue] += now - file->start_ns[queue];
-	file->jobs_sent[queue]++;
-	file->start_ns[queue] = 0;
+	local_stats->enabled_ns += now - local_stats->start_ns;
+	local_stats->jobs_sent++;
+	local_stats->start_ns = 0;
 
-	v3d->queue[queue].enabled_ns += now - v3d->queue[queue].start_ns;
-	v3d->queue[queue].jobs_sent++;
-	v3d->queue[queue].start_ns = 0;
+	global_stats->enabled_ns += now - global_stats->start_ns;
+	global_stats->jobs_sent++;
+	global_stats->start_ns = 0;
 }
 
 static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
drivers/gpu/drm/v3d/v3d_sysfs.c +6 -4
index d106845ba890..1eb5f3de6937 100644
@@ -21,8 +21,10 @@ gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf)
 	len += sysfs_emit(buf, "queue\ttimestamp\tjobs\truntime\n");
 
 	for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
-		if (v3d->queue[queue].start_ns)
-			active_runtime = timestamp - v3d->queue[queue].start_ns;
+		struct v3d_stats *stats = &v3d->queue[queue].stats;
+
+		if (stats->start_ns)
+			active_runtime = timestamp - stats->start_ns;
 		else
 			active_runtime = 0;
 
@@ -39,8 +41,8 @@ gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf)
 		len += sysfs_emit_at(buf, len, "%s\t%llu\t%llu\t%llu\n",
 				     v3d_queue_to_string(queue),
 				     timestamp,
-				     v3d->queue[queue].jobs_sent,
-				     v3d->queue[queue].enabled_ns + active_runtime);
+				     stats->jobs_sent,
+				     stats->enabled_ns + active_runtime);
 	}
 
 	return len;
Signature · 1 line
2.44.0
Reviewed-by on v1 3/5
#9 lore raw

Re: [PATCH 3/5] drm/v3d: Create a struct to store the GPU stats

Quoted 177 lines · Maíra Canal

This will make it easier to instantiate the GPU stats variables and it will create a structure where we can store all the variables that refer to GPU stats.

---
  drivers/gpu/drm/v3d/v3d_drv.c   | 14 ++++++--------
  drivers/gpu/drm/v3d/v3d_drv.h   | 18 ++++++++++--------
  drivers/gpu/drm/v3d/v3d_gem.c   |  4 +---
  drivers/gpu/drm/v3d/v3d_sched.c | 20 ++++++++++++--------
  drivers/gpu/drm/v3d/v3d_sysfs.c | 10 ++++++----
  5 files changed, 35 insertions(+), 31 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.c +6 -8
index 3debf37e7d9b..cbb62be18aa5 100644
@@ -115,14 +115,11 @@ v3d_open(struct drm_device *dev, struct drm_file *file)
  	v3d_priv->v3d = v3d;
 
  	for (i = 0; i < V3D_MAX_QUEUES; i++) {
-		v3d_priv->enabled_ns[i] = 0;
-		v3d_priv->start_ns[i] = 0;
-		v3d_priv->jobs_sent[i] = 0;
-
  		sched = &v3d->queue[i].sched;
  		drm_sched_entity_init(&v3d_priv->sched_entity[i],
  				      DRM_SCHED_PRIORITY_NORMAL, &sched,
  				      1, NULL);
+		memset(&v3d_priv->stats[i], 0, sizeof(v3d_priv->stats[i]));
  	}
 
  	v3d_perfmon_open_file(v3d_priv);
@@ -151,20 +148,21 @@ static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  	enum v3d_queue queue;
 
  	for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
+		struct v3d_stats *stats = &file_priv->stats[queue];
+
  		/* Note that, in case of a GPU reset, the time spent during an
  		 * attempt of executing the job is not computed in the runtime.
  		 */
  		drm_printf(p, "drm-engine-%s: \t%llu ns\n",
  			   v3d_queue_to_string(queue),
-			   file_priv->start_ns[queue] ? file_priv->enabled_ns[queue]
-						      + timestamp - file_priv->start_ns[queue]
-						      : file_priv->enabled_ns[queue]);
+			   stats->start_ns ? stats->enabled_ns + timestamp - stats->start_ns
+					   : stats->enabled_ns);
 
  		/* Note that we only count jobs that completed. Therefore, jobs
  		 * that were resubmitted due to a GPU reset are not computed.
  		 */
  		drm_printf(p, "v3d-jobs-%s: \t%llu jobs\n",
-			   v3d_queue_to_string(queue), file_priv->jobs_sent[queue]);
+			   v3d_queue_to_string(queue), stats->jobs_sent);
  	}
  }
 
drivers/gpu/drm/v3d/v3d_drv.h +10 -8
index ee3545226d7f..0117593976ed 100644
@@ -36,15 +36,20 @@ static inline char *v3d_queue_to_string(enum v3d_queue queue)
  	return "UNKNOWN";
  }
 
+struct v3d_stats {
+	u64 start_ns;
+	u64 enabled_ns;
+	u64 jobs_sent;
+};
+
  struct v3d_queue_state {
  	struct drm_gpu_scheduler sched;
 
  	u64 fence_context;
  	u64 emit_seqno;
 
-	u64 start_ns;
-	u64 enabled_ns;
-	u64 jobs_sent;
+	/* Stores the GPU stats for this queue in the global context. */
+	struct v3d_stats stats;
  };
 
  /* Performance monitor object. The perform lifetime is controlled by userspace
@@ -188,11 +193,8 @@ struct v3d_file_priv {
 
  	struct drm_sched_entity sched_entity[V3D_MAX_QUEUES];
 
-	u64 start_ns[V3D_MAX_QUEUES];
-
-	u64 enabled_ns[V3D_MAX_QUEUES];
-
-	u64 jobs_sent[V3D_MAX_QUEUES];
+	/* Stores the GPU stats for a specific queue for this fd. */
+	struct v3d_stats stats[V3D_MAX_QUEUES];
  };
 
  struct v3d_bo {
drivers/gpu/drm/v3d/v3d_gem.c +1 -3
index afc565078c78..d14589d3ae6c 100644
@@ -248,9 +248,7 @@ v3d_gem_init(struct drm_device *dev)
 
  	for (i = 0; i < V3D_MAX_QUEUES; i++) {
  		v3d->queue[i].fence_context = dma_fence_context_alloc(1);
-		v3d->queue[i].start_ns = 0;
-		v3d->queue[i].enabled_ns = 0;
-		v3d->queue[i].jobs_sent = 0;
+		memset(&v3d->queue[i].stats, 0, sizeof(v3d->queue[i].stats));
  	}
 
  	spin_lock_init(&v3d->mm_lock);
drivers/gpu/drm/v3d/v3d_sched.c +12 -8
index 8ca61bcd4b1c..ea5f5a84b55b 100644
@@ -110,10 +110,12 @@ v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
  {
  	struct v3d_dev *v3d = job->v3d;
  	struct v3d_file_priv *file = job->file->driver_priv;
+	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
+	struct v3d_stats *local_stats = &file->stats[queue];
  	u64 now = local_clock();
 
-	file->start_ns[queue] = now;
-	v3d->queue[queue].start_ns = now;
+	local_stats->start_ns = now;
+	global_stats->start_ns = now;
  }
 
  void
@@ -121,15 +123,17 @@ v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
  {
  	struct v3d_dev *v3d = job->v3d;
  	struct v3d_file_priv *file = job->file->driver_priv;
+	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
+	struct v3d_stats *local_stats = &file->stats[queue];
  	u64 now = local_clock();
 
-	file->enabled_ns[queue] += now - file->start_ns[queue];
-	file->jobs_sent[queue]++;
-	file->start_ns[queue] = 0;
+	local_stats->enabled_ns += now - local_stats->start_ns;
+	local_stats->jobs_sent++;
+	local_stats->start_ns = 0;
 
-	v3d->queue[queue].enabled_ns += now - v3d->queue[queue].start_ns;
-	v3d->queue[queue].jobs_sent++;
-	v3d->queue[queue].start_ns = 0;
+	global_stats->enabled_ns += now - global_stats->start_ns;
+	global_stats->jobs_sent++;
+	global_stats->start_ns = 0;
  }
 
  static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
drivers/gpu/drm/v3d/v3d_sysfs.c +4 -2
index d106845ba890..1eb5f3de6937 100644
@@ -21,8 +21,10 @@ gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf)
  	len += sysfs_emit(buf, "queue\ttimestamp\tjobs\truntime\n");
 
  	for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
-		if (v3d->queue[queue].start_ns)
-			active_runtime = timestamp - v3d->queue[queue].start_ns;
+		struct v3d_stats *stats = &v3d->queue[queue].stats;
+
+		if (stats->start_ns)
+			active_runtime = timestamp - stats->start_ns;
  		else
  			active_runtime = 0;
 

drivers/gpu/drm/v3d/v3d_sysfs.c gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf) · line 48

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_sysfs.c +2 -2
@@ -39,8 +41,8 @@ gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf)
  		len += sysfs_emit_at(buf, len, "%s\t%llu\t%llu\t%llu\n",
  				     v3d_queue_to_string(queue),
  				     timestamp,
-				     v3d->queue[queue].jobs_sent,
-				     v3d->queue[queue].enabled_ns + active_runtime);
+				     stats->jobs_sent,
+				     stats->enabled_ns + active_runtime);
  	}
 
  	return len;

Regards,

Tvrtko

Reviewed-by on v1 3/5
#10 lore raw

Re: [PATCH 3/5] drm/v3d: Create a struct to store the GPU stats

In reply to Maíra Canal
Quoted 64 lines · Maíra Canal

This will make it easier to instantiate the GPU stats variables and it will create a structure where we can store all the variables that refer to GPU stats.

---
  drivers/gpu/drm/v3d/v3d_drv.c   | 14 ++++++--------
  drivers/gpu/drm/v3d/v3d_drv.h   | 18 ++++++++++--------
  drivers/gpu/drm/v3d/v3d_gem.c   |  4 +---
  drivers/gpu/drm/v3d/v3d_sched.c | 20 ++++++++++++--------
  drivers/gpu/drm/v3d/v3d_sysfs.c | 10 ++++++----
  5 files changed, 35 insertions(+), 31 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.c +6 -8
index 3debf37e7d9b..cbb62be18aa5 100644
@@ -115,14 +115,11 @@ v3d_open(struct drm_device *dev, struct drm_file *file)
  	v3d_priv->v3d = v3d;
 
  	for (i = 0; i < V3D_MAX_QUEUES; i++) {
-		v3d_priv->enabled_ns[i] = 0;
-		v3d_priv->start_ns[i] = 0;
-		v3d_priv->jobs_sent[i] = 0;
-
  		sched = &v3d->queue[i].sched;
  		drm_sched_entity_init(&v3d_priv->sched_entity[i],
  				      DRM_SCHED_PRIORITY_NORMAL, &sched,
  				      1, NULL);
+		memset(&v3d_priv->stats[i], 0, sizeof(v3d_priv->stats[i]));
  	}
 
  	v3d_perfmon_open_file(v3d_priv);
@@ -151,20 +148,21 @@ static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  	enum v3d_queue queue;
 
  	for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
+		struct v3d_stats *stats = &file_priv->stats[queue];
+
  		/* Note that, in case of a GPU reset, the time spent during an
  		 * attempt of executing the job is not computed in the runtime.
  		 */
  		drm_printf(p, "drm-engine-%s: \t%llu ns\n",
  			   v3d_queue_to_string(queue),
-			   file_priv->start_ns[queue] ? file_priv->enabled_ns[queue]
-						      + timestamp - file_priv->start_ns[queue]
-						      : file_priv->enabled_ns[queue]);
+			   stats->start_ns ? stats->enabled_ns + timestamp - stats->start_ns
+					   : stats->enabled_ns);
 
  		/* Note that we only count jobs that completed. Therefore, jobs
  		 * that were resubmitted due to a GPU reset are not computed.
  		 */
  		drm_printf(p, "v3d-jobs-%s: \t%llu jobs\n",
-			   v3d_queue_to_string(queue), file_priv->jobs_sent[queue]);
+			   v3d_queue_to_string(queue), stats->jobs_sent);
  	}
  }
 
drivers/gpu/drm/v3d/v3d_drv.h +0 -0
index ee3545226d7f..0117593976ed 100644

drivers/gpu/drm/v3d/v3d_drv.h static inline char *v3d_queue_to_string(enum v3d_queue queue) · line 42

Quoted 7 lines
drivers/gpu/drm/v3d/v3d_drv.h +4 -0
@@ -36,15 +36,20 @@ static inline char *v3d_queue_to_string(enum v3d_queue queue)
  	return "UNKNOWN";
  }
 
+struct v3d_stats {
+	u64 start_ns;
+	u64 enabled_ns;
+	u64 jobs_sent;

As currently, we are only accounting jobs that have been completed. What do you think of renaming jobs_sent to jobs_completed?

Quoted 106 lines
drivers/gpu/drm/v3d/v3d_drv.h +6 -8
+};
+
  struct v3d_queue_state {
  	struct drm_gpu_scheduler sched;
 
  	u64 fence_context;
  	u64 emit_seqno;
 
-	u64 start_ns;
-	u64 enabled_ns;
-	u64 jobs_sent;
+	/* Stores the GPU stats for this queue in the global context. */
+	struct v3d_stats stats;
  };
 
  /* Performance monitor object. The perform lifetime is controlled by userspace
@@ -188,11 +193,8 @@ struct v3d_file_priv {
 
  	struct drm_sched_entity sched_entity[V3D_MAX_QUEUES];
 
-	u64 start_ns[V3D_MAX_QUEUES];
-
-	u64 enabled_ns[V3D_MAX_QUEUES];
-
-	u64 jobs_sent[V3D_MAX_QUEUES];
+	/* Stores the GPU stats for a specific queue for this fd. */
+	struct v3d_stats stats[V3D_MAX_QUEUES];
  };
 
  struct v3d_bo {
drivers/gpu/drm/v3d/v3d_gem.c +1 -3
index afc565078c78..d14589d3ae6c 100644
@@ -248,9 +248,7 @@ v3d_gem_init(struct drm_device *dev)
 
  	for (i = 0; i < V3D_MAX_QUEUES; i++) {
  		v3d->queue[i].fence_context = dma_fence_context_alloc(1);
-		v3d->queue[i].start_ns = 0;
-		v3d->queue[i].enabled_ns = 0;
-		v3d->queue[i].jobs_sent = 0;
+		memset(&v3d->queue[i].stats, 0, sizeof(v3d->queue[i].stats));
  	}
 
  	spin_lock_init(&v3d->mm_lock);
drivers/gpu/drm/v3d/v3d_sched.c +12 -8
index 8ca61bcd4b1c..ea5f5a84b55b 100644
@@ -110,10 +110,12 @@ v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
  {
  	struct v3d_dev *v3d = job->v3d;
  	struct v3d_file_priv *file = job->file->driver_priv;
+	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
+	struct v3d_stats *local_stats = &file->stats[queue];
  	u64 now = local_clock();
 
-	file->start_ns[queue] = now;
-	v3d->queue[queue].start_ns = now;
+	local_stats->start_ns = now;
+	global_stats->start_ns = now;
  }
 
  void
@@ -121,15 +123,17 @@ v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
  {
  	struct v3d_dev *v3d = job->v3d;
  	struct v3d_file_priv *file = job->file->driver_priv;
+	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
+	struct v3d_stats *local_stats = &file->stats[queue];
  	u64 now = local_clock();
 
-	file->enabled_ns[queue] += now - file->start_ns[queue];
-	file->jobs_sent[queue]++;
-	file->start_ns[queue] = 0;
+	local_stats->enabled_ns += now - local_stats->start_ns;
+	local_stats->jobs_sent++;
+	local_stats->start_ns = 0;
 
-	v3d->queue[queue].enabled_ns += now - v3d->queue[queue].start_ns;
-	v3d->queue[queue].jobs_sent++;
-	v3d->queue[queue].start_ns = 0;
+	global_stats->enabled_ns += now - global_stats->start_ns;
+	global_stats->jobs_sent++;
+	global_stats->start_ns = 0;
  }
 
  static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
drivers/gpu/drm/v3d/v3d_sysfs.c +4 -2
index d106845ba890..1eb5f3de6937 100644
@@ -21,8 +21,10 @@ gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf)
  	len += sysfs_emit(buf, "queue\ttimestamp\tjobs\truntime\n");
 
  	for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
-		if (v3d->queue[queue].start_ns)
-			active_runtime = timestamp - v3d->queue[queue].start_ns;
+		struct v3d_stats *stats = &v3d->queue[queue].stats;
+
+		if (stats->start_ns)
+			active_runtime = timestamp - stats->start_ns;
  		else
  			active_runtime = 0;
 

drivers/gpu/drm/v3d/v3d_sysfs.c gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf) · line 48

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_sysfs.c +2 -2
@@ -39,8 +41,8 @@ gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf)
  		len += sysfs_emit_at(buf, len, "%s\t%llu\t%llu\t%llu\n",
  				     v3d_queue_to_string(queue),
  				     timestamp,
-				     v3d->queue[queue].jobs_sent,
-				     v3d->queue[queue].enabled_ns + active_runtime);
+				     stats->jobs_sent,
+				     stats->enabled_ns + active_runtime);
  	}
 
  	return len;

Not related to this review, but as a general doubt, I miss with our current implementation the possibility of the user checking if a queue is running or not when the stats are checked. That information is stored in the struct as (start_ns !=0) but is not exposed, and it could be interesting information.

It would explain why you have running time (enabled_ns>0) but jobs_sent/completed is still 0, that is possible as you have one job running. It would be useful also if you want to check the average enabled/time per job. As we should consider the number of jobs completed + currently running to calculate the average.

In any case, this patch is:

author maintainer
#11 lore raw

PATCH 4/5 drm/v3d: Create function to update a set of GPU stats

In reply to Maíra Canal

Given a set of GPU stats, that is, a struct v3d_stats related to a queue in a given context, create a function that can update all this set of GPU stats.

---
 drivers/gpu/drm/v3d/v3d_sched.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
drivers/gpu/drm/v3d/v3d_sched.c +12 -8
index ea5f5a84b55b..754107b80f67 100644
@@ -118,6 +118,16 @@ v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
 	global_stats->start_ns = now;
 }
 
+static void
+v3d_stats_update(struct v3d_stats *stats)
+{
+	u64 now = local_clock();
+
+	stats->enabled_ns += now - stats->start_ns;
+	stats->jobs_sent++;
+	stats->start_ns = 0;
+}
+
 void
 v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
 {
@@ -125,15 +135,9 @@ v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
 	struct v3d_file_priv *file = job->file->driver_priv;
 	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
 	struct v3d_stats *local_stats = &file->stats[queue];
-	u64 now = local_clock();
-
-	local_stats->enabled_ns += now - local_stats->start_ns;
-	local_stats->jobs_sent++;
-	local_stats->start_ns = 0;
 
-	global_stats->enabled_ns += now - global_stats->start_ns;
-	global_stats->jobs_sent++;
-	global_stats->start_ns = 0;
+	v3d_stats_update(local_stats);
+	v3d_stats_update(global_stats);
 }
 
 static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
Signature · 1 line
2.44.0
Reviewed-by on v1 4/5
#12 lore raw

Re: [PATCH 4/5] drm/v3d: Create function to update a set of GPU stats

Quoted 38 lines · Maíra Canal

Given a set of GPU stats, that is, a struct v3d_stats related to a queue in a given context, create a function that can update all this set of GPU stats.

---
  drivers/gpu/drm/v3d/v3d_sched.c | 20 ++++++++++++--------
  1 file changed, 12 insertions(+), 8 deletions(-)
drivers/gpu/drm/v3d/v3d_sched.c +10 -4
index ea5f5a84b55b..754107b80f67 100644
@@ -118,6 +118,16 @@ v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
  	global_stats->start_ns = now;
  }
 
+static void
+v3d_stats_update(struct v3d_stats *stats)
+{
+	u64 now = local_clock();
+
+	stats->enabled_ns += now - stats->start_ns;
+	stats->jobs_sent++;
+	stats->start_ns = 0;
+}
+
  void
  v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
  {
@@ -125,15 +135,9 @@ v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
  	struct v3d_file_priv *file = job->file->driver_priv;
  	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
  	struct v3d_stats *local_stats = &file->stats[queue];
-	u64 now = local_clock();
-
-	local_stats->enabled_ns += now - local_stats->start_ns;
-	local_stats->jobs_sent++;

drivers/gpu/drm/v3d/v3d_sched.c v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue) · line 143

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_sched.c +2 -4
-	local_stats->start_ns = 0;
 
-	global_stats->enabled_ns += now - global_stats->start_ns;
-	global_stats->jobs_sent++;
-	global_stats->start_ns = 0;
+	v3d_stats_update(local_stats);
+	v3d_stats_update(global_stats);
  }
 
  static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)

Regards,

Tvrtko

#13 lore raw

Re: [PATCH 4/5] drm/v3d: Create function to update a set of GPU stats

In reply to Maíra Canal
Quoted 14 lines · Maíra Canal

Given a set of GPU stats, that is, a struct v3d_stats related to a queue in a given context, create a function that can update all this set of GPU stats.

---
  drivers/gpu/drm/v3d/v3d_sched.c | 20 ++++++++++++--------
  1 file changed, 12 insertions(+), 8 deletions(-)
drivers/gpu/drm/v3d/v3d_sched.c +0 -0
index ea5f5a84b55b..754107b80f67 100644

drivers/gpu/drm/v3d/v3d_sched.c v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue) · line 124

Quoted 7 lines
drivers/gpu/drm/v3d/v3d_sched.c +4 -0
@@ -118,6 +118,16 @@ v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
  	global_stats->start_ns = now;
  }
 
+static void
+v3d_stats_update(struct v3d_stats *stats)
+{
+	u64 now = local_clock();

I understand that with this change, we would be calling twice local_clock() for each stat update, once for local and one for global stats. I don't know the performance impact of the extra local_clock(), but I understand as you are always updating global_stats after local_stats we would be losing the correspondence that global_stats is the sum of all local_stats for all process. With this approach, it will be always greater or equal.

Maybe it makes sense to pass an extra parameter now so save one local_clock() and the sum of global and local stats is the same when you only have one process running.

Quoted 27 lines
drivers/gpu/drm/v3d/v3d_sched.c +8 -8
+
+	stats->enabled_ns += now - stats->start_ns;
+	stats->jobs_sent++;
+	stats->start_ns = 0;
+}
+
  void
  v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
  {
@@ -125,15 +135,9 @@ v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
  	struct v3d_file_priv *file = job->file->driver_priv;
  	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
  	struct v3d_stats *local_stats = &file->stats[queue];
-	u64 now = local_clock();
-
-	local_stats->enabled_ns += now - local_stats->start_ns;
-	local_stats->jobs_sent++;
-	local_stats->start_ns = 0;
 
-	global_stats->enabled_ns += now - global_stats->start_ns;
-	global_stats->jobs_sent++;
-	global_stats->start_ns = 0;
+	v3d_stats_update(local_stats);
+	v3d_stats_update(global_stats);
  }
 
  static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
author maintainer
#14 lore raw

PATCH 5/5 drm/v3d: Fix race-condition between sysfs/fdinfo and interrupt handler

In reply to Maíra Canal

In V3D, the conclusion of a job is indicated by a IRQ. When a job finishes, then we update the local and the global GPU stats of that queue. But, while the GPU stats are being updated, a user might be reading the stats from sysfs or fdinfo.

For example, on gpu_stats_show(), we could think about a scenario where v3d->queue[queue].start_ns != 0, then an interruption happens, we update the value of v3d->queue[queue].start_ns to 0, we come back to gpu_stats_show() to calculate active_runtime and now, active_runtime = timestamp.

In this simple example, the user would see a spike in the queue usage, that didn't matches reality.

In order to address this issue properly, use rw-locks to protect read and write sections of the code.

Fixes 09a93cc4f7d1 ("drm/v3d: Implement show_fdinfo() callback for GPU usage stats")
---
 drivers/gpu/drm/v3d/v3d_drv.c   | 16 ++++++++++++----
 drivers/gpu/drm/v3d/v3d_drv.h   |  7 +++++++
 drivers/gpu/drm/v3d/v3d_gem.c   |  7 +++++--
 drivers/gpu/drm/v3d/v3d_sched.c |  9 +++++++++
 drivers/gpu/drm/v3d/v3d_sysfs.c | 16 ++++++++--------
 5 files changed, 41 insertions(+), 14 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.c +12 -4
index cbb62be18aa5..60437718786c 100644
@@ -119,7 +119,9 @@ v3d_open(struct drm_device *dev, struct drm_file *file)
 		drm_sched_entity_init(&v3d_priv->sched_entity[i],
 				      DRM_SCHED_PRIORITY_NORMAL, &sched,
 				      1, NULL);
+
 		memset(&v3d_priv->stats[i], 0, sizeof(v3d_priv->stats[i]));
+		rwlock_init(&v3d_priv->stats[i].rw_lock);
 	}
 
 	v3d_perfmon_open_file(v3d_priv);
@@ -149,20 +151,26 @@ static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 
 	for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
 		struct v3d_stats *stats = &file_priv->stats[queue];
+		u64 active_time, jobs_sent;
+		unsigned long flags;
+
+		read_lock_irqsave(&stats->rw_lock, flags);
+		active_time = stats->start_ns ? stats->enabled_ns + timestamp - stats->start_ns
+					      : stats->enabled_ns;
+		jobs_sent = stats->jobs_sent;
+		read_unlock_irqrestore(&stats->rw_lock, flags);
 
 		/* Note that, in case of a GPU reset, the time spent during an
 		 * attempt of executing the job is not computed in the runtime.
 		 */
 		drm_printf(p, "drm-engine-%s: \t%llu ns\n",
-			   v3d_queue_to_string(queue),
-			   stats->start_ns ? stats->enabled_ns + timestamp - stats->start_ns
-					   : stats->enabled_ns);
+			   v3d_queue_to_string(queue), active_time);
 
 		/* Note that we only count jobs that completed. Therefore, jobs
 		 * that were resubmitted due to a GPU reset are not computed.
 		 */
 		drm_printf(p, "v3d-jobs-%s: \t%llu jobs\n",
-			   v3d_queue_to_string(queue), stats->jobs_sent);
+			   v3d_queue_to_string(queue), jobs_sent);
 	}
 }
 
drivers/gpu/drm/v3d/v3d_drv.h +7 -0
index 0117593976ed..8fde2623f763 100644
@@ -40,6 +40,13 @@ struct v3d_stats {
 	u64 start_ns;
 	u64 enabled_ns;
 	u64 jobs_sent;
+
+	/*
+	 * This lock is used to protect the access to the GPU stats variables.
+	 * It must be used as, while we are reading the stats, IRQs can happen
+	 * and the stats would be updated.
+	 */
+	rwlock_t rw_lock;
 };
 
 struct v3d_queue_state {
drivers/gpu/drm/v3d/v3d_gem.c +5 -2
index d14589d3ae6c..439088724a51 100644
@@ -247,8 +247,11 @@ v3d_gem_init(struct drm_device *dev)
 	int ret, i;
 
 	for (i = 0; i < V3D_MAX_QUEUES; i++) {
-		v3d->queue[i].fence_context = dma_fence_context_alloc(1);
-		memset(&v3d->queue[i].stats, 0, sizeof(v3d->queue[i].stats));
+		struct v3d_queue_state *queue = &v3d->queue[i];
+
+		queue->fence_context = dma_fence_context_alloc(1);
+		memset(&queue->stats, 0, sizeof(queue->stats));
+		rwlock_init(&queue->stats.rw_lock);
 	}
 
 	spin_lock_init(&v3d->mm_lock);
drivers/gpu/drm/v3d/v3d_sched.c +9 -0
index 754107b80f67..640de6768b15 100644
@@ -113,19 +113,28 @@ v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
 	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
 	struct v3d_stats *local_stats = &file->stats[queue];
 	u64 now = local_clock();
+	unsigned long flags;
 
+	write_lock_irqsave(&local_stats->rw_lock, flags);
 	local_stats->start_ns = now;
+	write_unlock_irqrestore(&local_stats->rw_lock, flags);
+
+	write_lock_irqsave(&global_stats->rw_lock, flags);
 	global_stats->start_ns = now;
+	write_unlock_irqrestore(&global_stats->rw_lock, flags);
 }
 
 static void
 v3d_stats_update(struct v3d_stats *stats)
 {
 	u64 now = local_clock();
+	unsigned long flags;
 
+	write_lock_irqsave(&stats->rw_lock, flags);
 	stats->enabled_ns += now - stats->start_ns;
 	stats->jobs_sent++;
 	stats->start_ns = 0;
+	write_unlock_irqrestore(&stats->rw_lock, flags);
 }
 
 void
drivers/gpu/drm/v3d/v3d_sysfs.c +8 -8
index 1eb5f3de6937..0f739a034cee 100644
@@ -15,18 +15,20 @@ gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf)
 	struct v3d_dev *v3d = to_v3d_dev(drm);
 	enum v3d_queue queue;
 	u64 timestamp = local_clock();
-	u64 active_runtime;
 	ssize_t len = 0;
+	unsigned long flags;
 
 	len += sysfs_emit(buf, "queue\ttimestamp\tjobs\truntime\n");
 
 	for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
 		struct v3d_stats *stats = &v3d->queue[queue].stats;
+		u64 active_runtime, jobs_sent;
 
-		if (stats->start_ns)
-			active_runtime = timestamp - stats->start_ns;
-		else
-			active_runtime = 0;
+		read_lock_irqsave(&stats->rw_lock, flags);
+		active_runtime = stats->start_ns ? stats->enabled_ns + timestamp - stats->start_ns
+						 : stats->enabled_ns;
+		jobs_sent = stats->jobs_sent;
+		read_unlock_irqrestore(&stats->rw_lock, flags);
 
 		/* Each line will display the queue name, timestamp, the number
 		 * of jobs sent to that queue and the runtime, as can be seem here:
@@ -40,9 +42,7 @@ gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf)
 		 */
 		len += sysfs_emit_at(buf, len, "%s\t%llu\t%llu\t%llu\n",
 				     v3d_queue_to_string(queue),
-				     timestamp,
-				     stats->jobs_sent,
-				     stats->enabled_ns + active_runtime);
+				     timestamp, jobs_sent, active_runtime);
 	}
 
 	return len;

--
2.44.0

#15 lore raw

Re: [PATCH 5/5] drm/v3d: Fix race-condition between sysfs/fdinfo and interrupt handler

Quoted 32 lines · Maíra Canal

In V3D, the conclusion of a job is indicated by a IRQ. When a job finishes, then we update the local and the global GPU stats of that queue. But, while the GPU stats are being updated, a user might be reading the stats from sysfs or fdinfo.

For example, on gpu_stats_show(), we could think about a scenario where v3d->queue[queue].start_ns != 0, then an interruption happens, we update the value of v3d->queue[queue].start_ns to 0, we come back to gpu_stats_show() to calculate active_runtime and now, active_runtime = timestamp.

In this simple example, the user would see a spike in the queue usage, that didn't matches reality.

In order to address this issue properly, use rw-locks to protect read and write sections of the code.

Fixes 09a93cc4f7d1 ("drm/v3d: Implement show_fdinfo() callback for GPU usage stats")
---
  drivers/gpu/drm/v3d/v3d_drv.c   | 16 ++++++++++++----
  drivers/gpu/drm/v3d/v3d_drv.h   |  7 +++++++
  drivers/gpu/drm/v3d/v3d_gem.c   |  7 +++++--
  drivers/gpu/drm/v3d/v3d_sched.c |  9 +++++++++
  drivers/gpu/drm/v3d/v3d_sysfs.c | 16 ++++++++--------
  5 files changed, 41 insertions(+), 14 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.c +0 -0
index cbb62be18aa5..60437718786c 100644

drivers/gpu/drm/v3d/v3d_drv.c v3d_open(struct drm_device *dev, struct drm_file *file) · line 122

Quoted 4 lines
drivers/gpu/drm/v3d/v3d_drv.c +1 -0
@@ -119,7 +119,9 @@ v3d_open(struct drm_device *dev, struct drm_file *file)
  		drm_sched_entity_init(&v3d_priv->sched_entity[i],
  				      DRM_SCHED_PRIORITY_NORMAL, &sched,
  				      1, NULL);
+

Nitpick - if you want a blank line here probably add it in the patch which added the below memset.

drivers/gpu/drm/v3d/v3d_drv.c static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file) · line 157

Quoted 13 lines
drivers/gpu/drm/v3d/v3d_drv.c +5 -0
  		memset(&v3d_priv->stats[i], 0, sizeof(v3d_priv->stats[i]));
+		rwlock_init(&v3d_priv->stats[i].rw_lock);
  	}
 
  	v3d_perfmon_open_file(v3d_priv);
@@ -149,20 +151,26 @@ static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 
  	for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
  		struct v3d_stats *stats = &file_priv->stats[queue];
+		u64 active_time, jobs_sent;
+		unsigned long flags;
+
+		read_lock_irqsave(&stats->rw_lock, flags);

The context is never irq/bh here so you can you read_lock_irq.

However on the topic of lock type chosen, I think sort of established wisdom is that rwlocks are overkill for such short locked sections. More so, optimizing for multiple concurrent readers is not a huge use case for fdinfo reads. I would go for a plain spinlock, or potentially even read/write_seqcount. Just because the latter has no atomics in the irq handler. Readers might retry now and then, but unless v3d typically sees thousands of interrupts per second it should not be a problem.

Quoted 80 lines
drivers/gpu/drm/v3d/v3d_drv.c +6 -4
+		active_time = stats->start_ns ? stats->enabled_ns + timestamp - stats->start_ns
+					      : stats->enabled_ns;
+		jobs_sent = stats->jobs_sent;
+		read_unlock_irqrestore(&stats->rw_lock, flags);
 
  		/* Note that, in case of a GPU reset, the time spent during an
  		 * attempt of executing the job is not computed in the runtime.
  		 */
  		drm_printf(p, "drm-engine-%s: \t%llu ns\n",
-			   v3d_queue_to_string(queue),
-			   stats->start_ns ? stats->enabled_ns + timestamp - stats->start_ns
-					   : stats->enabled_ns);
+			   v3d_queue_to_string(queue), active_time);
 
  		/* Note that we only count jobs that completed. Therefore, jobs
  		 * that were resubmitted due to a GPU reset are not computed.
  		 */
  		drm_printf(p, "v3d-jobs-%s: \t%llu jobs\n",
-			   v3d_queue_to_string(queue), stats->jobs_sent);
+			   v3d_queue_to_string(queue), jobs_sent);
  	}
  }
 
drivers/gpu/drm/v3d/v3d_drv.h +7 -0
index 0117593976ed..8fde2623f763 100644
@@ -40,6 +40,13 @@ struct v3d_stats {
  	u64 start_ns;
  	u64 enabled_ns;
  	u64 jobs_sent;
+
+	/*
+	 * This lock is used to protect the access to the GPU stats variables.
+	 * It must be used as, while we are reading the stats, IRQs can happen
+	 * and the stats would be updated.
+	 */
+	rwlock_t rw_lock;
  };
 
  struct v3d_queue_state {
drivers/gpu/drm/v3d/v3d_gem.c +5 -2
index d14589d3ae6c..439088724a51 100644
@@ -247,8 +247,11 @@ v3d_gem_init(struct drm_device *dev)
  	int ret, i;
 
  	for (i = 0; i < V3D_MAX_QUEUES; i++) {
-		v3d->queue[i].fence_context = dma_fence_context_alloc(1);
-		memset(&v3d->queue[i].stats, 0, sizeof(v3d->queue[i].stats));
+		struct v3d_queue_state *queue = &v3d->queue[i];
+
+		queue->fence_context = dma_fence_context_alloc(1);
+		memset(&queue->stats, 0, sizeof(queue->stats));
+		rwlock_init(&queue->stats.rw_lock);
  	}
 
  	spin_lock_init(&v3d->mm_lock);
drivers/gpu/drm/v3d/v3d_sched.c +6 -0
index 754107b80f67..640de6768b15 100644
@@ -113,19 +113,28 @@ v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
  	struct v3d_stats *global_stats = &v3d->queue[queue].stats;
  	struct v3d_stats *local_stats = &file->stats[queue];
  	u64 now = local_clock();
+	unsigned long flags;
 
+	write_lock_irqsave(&local_stats->rw_lock, flags);
  	local_stats->start_ns = now;
+	write_unlock_irqrestore(&local_stats->rw_lock, flags);
+
+	write_lock_irqsave(&global_stats->rw_lock, flags);
  	global_stats->start_ns = now;
+	write_unlock_irqrestore(&global_stats->rw_lock, flags);
  }
 
  static void
  v3d_stats_update(struct v3d_stats *stats)

drivers/gpu/drm/v3d/v3d_sched.c v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue) · line 138

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_sched.c +3 -0
  {
  	u64 now = local_clock();
+	unsigned long flags;
 
+	write_lock_irqsave(&stats->rw_lock, flags);
  	stats->enabled_ns += now - stats->start_ns;
  	stats->jobs_sent++;
  	stats->start_ns = 0;
+	write_unlock_irqrestore(&stats->rw_lock, flags);
  }

I was wondering if plain write_lock would be more efficient but I suspect the two helpers are called from both the irq handler and the worker in case of CPU jobs?

Quoted 15 lines
drivers/gpu/drm/v3d/v3d_sched.c +0 -0
  void
drivers/gpu/drm/v3d/v3d_sysfs.c +1 -1
index 1eb5f3de6937..0f739a034cee 100644
@@ -15,18 +15,20 @@ gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf)
  	struct v3d_dev *v3d = to_v3d_dev(drm);
  	enum v3d_queue queue;
  	u64 timestamp = local_clock();
-	u64 active_runtime;
  	ssize_t len = 0;
+	unsigned long flags;
 
  	len += sysfs_emit(buf, "queue\ttimestamp\tjobs\truntime\n");

drivers/gpu/drm/v3d/v3d_sysfs.c gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf) · line 27

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_sysfs.c +2 -4
 
  	for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
  		struct v3d_stats *stats = &v3d->queue[queue].stats;
+		u64 active_runtime, jobs_sent;
 
-		if (stats->start_ns)
-			active_runtime = timestamp - stats->start_ns;
-		else
-			active_runtime = 0;
+		read_lock_irqsave(&stats->rw_lock, flags);

This one is also always process context so does not need irqsave.

Quoted 19 lines
drivers/gpu/drm/v3d/v3d_sysfs.c +5 -4
+		active_runtime = stats->start_ns ? stats->enabled_ns + timestamp - stats->start_ns
+						 : stats->enabled_ns;
+		jobs_sent = stats->jobs_sent;
+		read_unlock_irqrestore(&stats->rw_lock, flags);
 
  		/* Each line will display the queue name, timestamp, the number
  		 * of jobs sent to that queue and the runtime, as can be seem here:
@@ -40,9 +42,7 @@ gpu_stats_show(struct device *dev, struct device_attribute *attr, char *buf)
  		 */
  		len += sysfs_emit_at(buf, len, "%s\t%llu\t%llu\t%llu\n",
  				     v3d_queue_to_string(queue),
-				     timestamp,
-				     stats->jobs_sent,
-				     stats->enabled_ns + active_runtime);
+				     timestamp, jobs_sent, active_runtime);
  	}
 
  	return len;
--
Quoted 3 lines

2.44.0

Regards,

Tvrtko