Skip to contentSkip to conversation

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

PATCH In review Maíra Canal 3 participants 10 messages Apr 20, 2024 – Apr 23, 2024

Subsystems (3)
lore
Share & tools
lore

Maíra Canal

author maintainer
#0 lore raw

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

The first version of this series had the intention to fix two major issues with the GPU stats:

1. We were incrementing enabled_ns twice by the end of each job.
2. There is a race-condition between the IRQ handler and the users

The first of the issues was already addressed and the fix was applied to drm-misc-fixes. Now, what is left, addresses the second issue.

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

v1 -> v2: https://lore.kernel.org/dri-devel/[email protected]/T/

- As the first patch was a bugfix, it was pushed to drm-misc-fixes.
- [1/4] Add Chema Casanova's R-b
- [2/4] s/jobs_sent/jobs_completed and add the reasoning in the commit message (Chema Casanova)
- [2/4] Add Chema Casanova's and Tvrtko Ursulin's R-b
- [3/4] Call local_clock() only once, by adding a new parameter to the v3d_stats_update function (Chema Casanova)
- [4/4] Move new line to the correct patch [2/4] (Tvrtko Ursulin)
- [4/4] Use seqcount_t as locking primitive instead of a rw_lock (Tvrtko Ursulin)

v2 -> v3: https://lore.kernel.org/dri-devel/[email protected]/T/

- [4/5] New patch: separates the code refactor from the race-condition fix (Tvrtko Ursulin)
- [5/5] s/interruption/interrupt (Tvrtko Ursulin)
- [5/5] s/matches/match (Tvrtko Ursulin)
- [5/5] Add Tvrtko Ursulin's R-b

Best Regards,
- Maíra

Maíra Canal (5):
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: Decouple stats calculation from printing
drm/v3d: Fix race-condition between sysfs/fdinfo and interrupt handler

 drivers/gpu/drm/v3d/v3d_drv.c   | 33 ++++++++----
 drivers/gpu/drm/v3d/v3d_drv.h   | 30 ++++++++---
 drivers/gpu/drm/v3d/v3d_gem.c   |  9 ++--
 drivers/gpu/drm/v3d/v3d_irq.c   | 48 ++---------------
 drivers/gpu/drm/v3d/v3d_sched.c | 94 +++++++++++++++++----------------
 drivers/gpu/drm/v3d/v3d_sysfs.c | 13 ++---
 6 files changed, 109 insertions(+), 118 deletions(-)
Signature · 1 line
2.44.0
author maintainer
#1 lore raw

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

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 commit 35f4f8c9fc97 ("drm/v3d: Don't increment enabled_ns twice").

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
author maintainer
#2 lore raw

PATCH v3 2/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.

Note that, when we created the struct v3d_stats, we renamed jobs_sent to jobs_completed. This better express the semantics of the variable, as we are only accounting jobs that have been completed.

---
 drivers/gpu/drm/v3d/v3d_drv.c   | 15 +++++++--------
 drivers/gpu/drm/v3d/v3d_drv.h   | 18 ++++++++++--------
 drivers/gpu/drm/v3d/v3d_gem.c   |  8 ++++----
 drivers/gpu/drm/v3d/v3d_sched.c | 20 ++++++++++++--------
 drivers/gpu/drm/v3d/v3d_sysfs.c | 10 ++++++----
 5 files changed, 39 insertions(+), 32 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.c +7 -8
index 3debf37e7d9b..52e3ba9df46f 100644
@@ -115,14 +115,12 @@ 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 +149,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_completed);
 	}
 }
 
drivers/gpu/drm/v3d/v3d_drv.h +10 -8
index ee3545226d7f..5a198924d568 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_completed;
+};
+
 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 +4 -4
index afc565078c78..0086081a9261 100644
@@ -247,10 +247,10 @@ 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);
-		v3d->queue[i].start_ns = 0;
-		v3d->queue[i].enabled_ns = 0;
-		v3d->queue[i].jobs_sent = 0;
+		struct v3d_queue_state *queue = &v3d->queue[i];
+
+		queue->fence_context = dma_fence_context_alloc(1);
+		memset(&queue->stats, 0, sizeof(queue->stats));
 	}
 
 	spin_lock_init(&v3d->mm_lock);
drivers/gpu/drm/v3d/v3d_sched.c +12 -8
index 8ca61bcd4b1c..b6b5542c3fcf 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_completed++;
+	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_completed++;
+	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..6a8e7acc8b82 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_completed,
+				     stats->enabled_ns + active_runtime);
 	}
 
 	return len;
Signature · 1 line
2.44.0
author maintainer
#3 lore raw

PATCH v3 3/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 this set of GPU stats.

---
 drivers/gpu/drm/v3d/v3d_sched.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/gpu/drm/v3d/v3d_sched.c +10 -7
index b6b5542c3fcf..b9614944931c 100644
@@ -118,6 +118,14 @@ 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)
+{
+	stats->enabled_ns += now - stats->start_ns;
+	stats->jobs_completed++;
+	stats->start_ns = 0;
+}
+
 void
 v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
 {
@@ -127,13 +135,8 @@ v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue)
 	struct v3d_stats *local_stats = &file->stats[queue];
 	u64 now = local_clock();
 
-	local_stats->enabled_ns += now - local_stats->start_ns;
-	local_stats->jobs_completed++;
-	local_stats->start_ns = 0;
-
-	global_stats->enabled_ns += now - global_stats->start_ns;
-	global_stats->jobs_completed++;
-	global_stats->start_ns = 0;
+	v3d_stats_update(local_stats, now);
+	v3d_stats_update(global_stats, now);
 }
 
 static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
Signature · 1 line
2.44.0
author maintainer
#4 lore raw

PATCH v3 4/5 drm/v3d: Decouple stats calculation from printing

In reply to Maíra Canal

Create a function to decouple the stats calculation from the printing. This will be useful in the next step when we add a seqcount to protect the stats.

---
 drivers/gpu/drm/v3d/v3d_drv.c   | 18 ++++++++++++++----
 drivers/gpu/drm/v3d/v3d_drv.h   |  4 ++++
 drivers/gpu/drm/v3d/v3d_sysfs.c | 11 +++--------
 3 files changed, 21 insertions(+), 12 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.c +14 -4
index 52e3ba9df46f..2ec359ed2def 100644
@@ -142,6 +142,15 @@ v3d_postclose(struct drm_device *dev, struct drm_file *file)
 	kfree(v3d_priv);
 }
 
+void v3d_get_stats(const struct v3d_stats *stats, u64 timestamp,
+		   u64 *active_runtime, u64 *jobs_completed)
+{
+	*active_runtime = stats->enabled_ns;
+	if (stats->start_ns)
+		*active_runtime += timestamp - stats->start_ns;
+	*jobs_completed = stats->jobs_completed;
+}
+
 static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 {
 	struct v3d_file_priv *file_priv = file->driver_priv;
@@ -150,20 +159,21 @@ 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_runtime, jobs_completed;
+
+		v3d_get_stats(stats, timestamp, &active_runtime, &jobs_completed);
 
 		/* 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_runtime);
 
 		/* 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_completed);
+			   v3d_queue_to_string(queue), jobs_completed);
 	}
 }
 
drivers/gpu/drm/v3d/v3d_drv.h +4 -0
index 5a198924d568..ff06dc1cc078 100644
@@ -510,6 +510,10 @@ struct drm_gem_object *v3d_prime_import_sg_table(struct drm_device *dev,
 /* v3d_debugfs.c */
 void v3d_debugfs_init(struct drm_minor *minor);
 
+/* v3d_drv.c */
+void v3d_get_stats(const struct v3d_stats *stats, u64 timestamp,
+		   u64 *active_runtime, u64 *jobs_completed);
+
 /* v3d_fence.c */
 extern const struct dma_fence_ops v3d_fence_ops;
 struct dma_fence *v3d_fence_create(struct v3d_dev *v3d, enum v3d_queue queue);
drivers/gpu/drm/v3d/v3d_sysfs.c +3 -8
index 6a8e7acc8b82..d610e355964f 100644
@@ -15,18 +15,15 @@ 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;
 
 	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_completed;
 
-		if (stats->start_ns)
-			active_runtime = timestamp - stats->start_ns;
-		else
-			active_runtime = 0;
+		v3d_get_stats(stats, timestamp, &active_runtime, &jobs_completed);
 
 		/* 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 +37,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_completed,
-				     stats->enabled_ns + active_runtime);
+				     timestamp, jobs_completed, active_runtime);
 	}
 
 	return len;
Signature · 1 line
2.44.0
Reviewed-by on v3 4/5
#5 lore raw

Re: [PATCH v3 4/5] drm/v3d: Decouple stats calculation from printing

Quoted 98 lines · Maíra Canal

Create a function to decouple the stats calculation from the printing. This will be useful in the next step when we add a seqcount to protect the stats.

---
  drivers/gpu/drm/v3d/v3d_drv.c   | 18 ++++++++++++++----
  drivers/gpu/drm/v3d/v3d_drv.h   |  4 ++++
  drivers/gpu/drm/v3d/v3d_sysfs.c | 11 +++--------
  3 files changed, 21 insertions(+), 12 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.c +14 -4
index 52e3ba9df46f..2ec359ed2def 100644
@@ -142,6 +142,15 @@ v3d_postclose(struct drm_device *dev, struct drm_file *file)
  	kfree(v3d_priv);
  }
 
+void v3d_get_stats(const struct v3d_stats *stats, u64 timestamp,
+		   u64 *active_runtime, u64 *jobs_completed)
+{
+	*active_runtime = stats->enabled_ns;
+	if (stats->start_ns)
+		*active_runtime += timestamp - stats->start_ns;
+	*jobs_completed = stats->jobs_completed;
+}
+
  static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  {
  	struct v3d_file_priv *file_priv = file->driver_priv;
@@ -150,20 +159,21 @@ 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_runtime, jobs_completed;
+
+		v3d_get_stats(stats, timestamp, &active_runtime, &jobs_completed);
 
  		/* 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_runtime);
 
  		/* 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_completed);
+			   v3d_queue_to_string(queue), jobs_completed);
  	}
  }
 
drivers/gpu/drm/v3d/v3d_drv.h +4 -0
index 5a198924d568..ff06dc1cc078 100644
@@ -510,6 +510,10 @@ struct drm_gem_object *v3d_prime_import_sg_table(struct drm_device *dev,
  /* v3d_debugfs.c */
  void v3d_debugfs_init(struct drm_minor *minor);
 
+/* v3d_drv.c */
+void v3d_get_stats(const struct v3d_stats *stats, u64 timestamp,
+		   u64 *active_runtime, u64 *jobs_completed);
+
  /* v3d_fence.c */
  extern const struct dma_fence_ops v3d_fence_ops;
  struct dma_fence *v3d_fence_create(struct v3d_dev *v3d, enum v3d_queue queue);
drivers/gpu/drm/v3d/v3d_sysfs.c +2 -5
index 6a8e7acc8b82..d610e355964f 100644
@@ -15,18 +15,15 @@ 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;
 
  	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_completed;
 
-		if (stats->start_ns)
-			active_runtime = timestamp - stats->start_ns;
-		else
-			active_runtime = 0;
+		v3d_get_stats(stats, timestamp, &active_runtime, &jobs_completed);
 
  		/* Each line will display the queue name, timestamp, the number
  		 * of jobs sent to that queue and the runtime, as can be seem here:

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

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_sysfs.c +1 -3
@@ -40,9 +37,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_completed,
-				     stats->enabled_ns + active_runtime);
+				     timestamp, jobs_completed, active_runtime);
  	}
 
  	return len;

Regards,

Tvrtko

author maintainer
#6 lore raw

PATCH v3 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 interrupt 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 match reality.

In order to address this issue properly, use a seqcount 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   | 14 ++++++++++----
 drivers/gpu/drm/v3d/v3d_drv.h   |  7 +++++++
 drivers/gpu/drm/v3d/v3d_gem.c   |  1 +
 drivers/gpu/drm/v3d/v3d_sched.c |  7 +++++++
 4 files changed, 25 insertions(+), 4 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.c +10 -4
index 2ec359ed2def..28b7ddce7747 100644
@@ -121,6 +121,7 @@ v3d_open(struct drm_device *dev, struct drm_file *file)
 				      1, NULL);
 
 		memset(&v3d_priv->stats[i], 0, sizeof(v3d_priv->stats[i]));
+		seqcount_init(&v3d_priv->stats[i].lock);
 	}
 
 	v3d_perfmon_open_file(v3d_priv);
@@ -145,10 +146,15 @@ v3d_postclose(struct drm_device *dev, struct drm_file *file)
 void v3d_get_stats(const struct v3d_stats *stats, u64 timestamp,
 		   u64 *active_runtime, u64 *jobs_completed)
 {
-	*active_runtime = stats->enabled_ns;
-	if (stats->start_ns)
-		*active_runtime += timestamp - stats->start_ns;
-	*jobs_completed = stats->jobs_completed;
+	unsigned int seq;
+
+	do {
+		seq = read_seqcount_begin(&stats->lock);
+		*active_runtime = stats->enabled_ns;
+		if (stats->start_ns)
+			*active_runtime += timestamp - stats->start_ns;
+		*jobs_completed = stats->jobs_completed;
+	} while (read_seqcount_retry(&stats->lock, seq));
 }
 
 static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file)
drivers/gpu/drm/v3d/v3d_drv.h +7 -0
index ff06dc1cc078..a2c516fe6d79 100644
@@ -40,6 +40,13 @@ struct v3d_stats {
 	u64 start_ns;
 	u64 enabled_ns;
 	u64 jobs_completed;
+
+	/*
+	 * This seqcount 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 can be updated.
+	 */
+	seqcount_t lock;
 };
 
 struct v3d_queue_state {
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
index 0086081a9261..da8faf3b9011 100644
@@ -251,6 +251,7 @@ v3d_gem_init(struct drm_device *dev)
 
 		queue->fence_context = dma_fence_context_alloc(1);
 		memset(&queue->stats, 0, sizeof(queue->stats));
+		seqcount_init(&queue->stats.lock);
 	}
 
 	spin_lock_init(&v3d->mm_lock);
drivers/gpu/drm/v3d/v3d_sched.c +7 -0
index b9614944931c..7cd8c335cd9b 100644
@@ -114,16 +114,23 @@ v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
 	struct v3d_stats *local_stats = &file->stats[queue];
 	u64 now = local_clock();
 
+	write_seqcount_begin(&local_stats->lock);
 	local_stats->start_ns = now;
+	write_seqcount_end(&local_stats->lock);
+
+	write_seqcount_begin(&global_stats->lock);
 	global_stats->start_ns = now;
+	write_seqcount_end(&global_stats->lock);
 }
 
 static void
 v3d_stats_update(struct v3d_stats *stats, u64 now)
 {
+	write_seqcount_begin(&stats->lock);
 	stats->enabled_ns += now - stats->start_ns;
 	stats->jobs_completed++;
 	stats->start_ns = 0;
+	write_seqcount_end(&stats->lock);
 }
 
 void
Signature · 1 line
2.44.0
author maintainer
#7 lore raw
In reply to Maíra Canal

Hi,

@drm-misc maintainers, is there any chance you could backport commit 35f4f8c9fc97 ("drm/v3d: Don't increment enabled_ns twice") [1] to drm- misc-next?

I would like to apply this series to drm-misc-next because it fixes another issue with the GPU stats, but this series depends on commit 35f4f8c9fc97, as it has plenty of refactors on the GPU stats code.

Although I could theoretically apply this series in drm-misc-fixes, I don't believe it would be ideal, as discussed in #dri-devel earlier today.

[1] https://gitlab.freedesktop.org/drm/misc/kernel/-/commit/35f4f8c9fc972248055096d63b782060e473311b

Best Regards,
- Maíra

Quoted 50 lines · Maíra Canal

The first version of this series had the intention to fix two major issues with the GPU stats:

1. We were incrementing enabled_ns twice by the end of each job.
2. There is a race-condition between the IRQ handler and the users

The first of the issues was already addressed and the fix was applied to drm-misc-fixes. Now, what is left, addresses the second issue.

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

v1 -> v2: https://lore.kernel.org/dri-devel/[email protected]/T/

- As the first patch was a bugfix, it was pushed to drm-misc-fixes.
- [1/4] Add Chema Casanova's R-b
- [2/4] s/jobs_sent/jobs_completed and add the reasoning in the commit message (Chema Casanova)
- [2/4] Add Chema Casanova's and Tvrtko Ursulin's R-b
- [3/4] Call local_clock() only once, by adding a new parameter to the v3d_stats_update function (Chema Casanova)
- [4/4] Move new line to the correct patch [2/4] (Tvrtko Ursulin)
- [4/4] Use seqcount_t as locking primitive instead of a rw_lock (Tvrtko Ursulin)

v2 -> v3: https://lore.kernel.org/dri-devel/[email protected]/T/

- [4/5] New patch: separates the code refactor from the race-condition fix (Tvrtko Ursulin)
- [5/5] s/interruption/interrupt (Tvrtko Ursulin)
- [5/5] s/matches/match (Tvrtko Ursulin)
- [5/5] Add Tvrtko Ursulin's R-b

Best Regards,
- Maíra

Maíra Canal (5):
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: Decouple stats calculation from printing
drm/v3d: Fix race-condition between sysfs/fdinfo and interrupt handler

  drivers/gpu/drm/v3d/v3d_drv.c   | 33 ++++++++----
  drivers/gpu/drm/v3d/v3d_drv.h   | 30 ++++++++---
  drivers/gpu/drm/v3d/v3d_gem.c   |  9 ++--
  drivers/gpu/drm/v3d/v3d_irq.c   | 48 ++---------------
  drivers/gpu/drm/v3d/v3d_sched.c | 94 +++++++++++++++++----------------
  drivers/gpu/drm/v3d/v3d_sysfs.c | 13 ++---
  6 files changed, 109 insertions(+), 118 deletions(-)
maintainer
#8 lore raw

Hi,

Quoted 8 lines · Maíra Canal

@drm-misc maintainers, is there any chance you could backport commit 35f4f8c9fc97 ("drm/v3d: Don't increment enabled_ns twice") [1] to drm- misc-next?

I would like to apply this series to drm-misc-next because it fixes another issue with the GPU stats, but this series depends on commit 35f4f8c9fc97, as it has plenty of refactors on the GPU stats code.

Quoted 5 lines

Although I could theoretically apply this series in drm-misc-fixes, I don't believe it would be ideal, as discussed in #dri-devel earlier today.

[1] https://gitlab.freedesktop.org/drm/misc/kernel/-/commit/35f4f8c9fc972248055096d63b782060e473311b

I just did the backmerge

Maxime

author maintainer
#9 lore raw
Quoted 11 lines · Maxime Ripard

Hi,

Quoted 8 lines · Maíra Canal

@drm-misc maintainers, is there any chance you could backport commit 35f4f8c9fc97 ("drm/v3d: Don't increment enabled_ns twice") [1] to drm- misc-next?

I would like to apply this series to drm-misc-next because it fixes another issue with the GPU stats, but this series depends on commit 35f4f8c9fc97, as it has plenty of refactors on the GPU stats code.

Quoted 7 lines
Quoted 5 lines

Although I could theoretically apply this series in drm-misc-fixes, I don't believe it would be ideal, as discussed in #dri-devel earlier today.

[1] https://gitlab.freedesktop.org/drm/misc/kernel/-/commit/35f4f8c9fc972248055096d63b782060e473311b

I just did the backmerge

Thanks Maxime! I just applied the series to drm-misc/drm-misc-next.

Thanks for drm-misc maintainers for the quick action!

Best Regards,
- Maíra

Quoted 2 lines

Maxime