Skip to contentSkip to conversation

drm/xe: Use run_ticks instead of runtime for client stats

PATCH In review Umesh Nerlige Ramappa 2 participants 3 messages May 24, 2024 – May 28, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/xe: Use run_ticks instead of runtime for client stats

Note that runtime is also used in the pm context, so it is confusing to use the same name to denote run time of the drm client. Use a more appropriate name for the client utilization.

While at it, drop the incorrect multi-lrc comment in the helper description

v2: s/show_runtime/show_run_ticks/ (Rodrigo)

---
 drivers/gpu/drm/xe/xe_device_types.h |  4 ++--
 drivers/gpu/drm/xe/xe_drm_client.c   |  8 ++++----
 drivers/gpu/drm/xe/xe_exec_queue.c   | 12 ++++++------
 drivers/gpu/drm/xe/xe_exec_queue.h   |  2 +-
 drivers/gpu/drm/xe/xe_execlist.c     |  2 +-
 drivers/gpu/drm/xe/xe_guc_submit.c   |  2 +-
 6 files changed, 15 insertions(+), 15 deletions(-)
drivers/gpu/drm/xe/xe_device_types.h +2 -2
index 03bedc33b21a..d0936d82cc1d 100644
@@ -556,8 +556,8 @@ struct xe_file {
 		struct mutex lock;
 	} exec_queue;
 
-	/** @runtime: hw engine class runtime in ticks for this drm client */
-	u64 runtime[XE_ENGINE_CLASS_MAX];
+	/** @run_ticks: hw engine class run time in ticks for this drm client */
+	u64 run_ticks[XE_ENGINE_CLASS_MAX];
 
 	/** @client: drm client */
 	struct xe_drm_client *client;
drivers/gpu/drm/xe/xe_drm_client.c +4 -4
index af404c9e5cc0..1dcae0e6d66d 100644
@@ -237,7 +237,7 @@ static void show_meminfo(struct drm_printer *p, struct drm_file *file)
 	}
 }
 
-static void show_runtime(struct drm_printer *p, struct drm_file *file)
+static void show_run_ticks(struct drm_printer *p, struct drm_file *file)
 {
 	unsigned long class, i, gt_id, capacity[XE_ENGINE_CLASS_MAX] = { };
 	struct xe_file *xef = file->driver_priv;
@@ -252,7 +252,7 @@ static void show_runtime(struct drm_printer *p, struct drm_file *file)
 	/* Accumulate all the exec queues from this client */
 	mutex_lock(&xef->exec_queue.lock);
 	xa_for_each(&xef->exec_queue.xa, i, q)
-		xe_exec_queue_update_runtime(q);
+		xe_exec_queue_update_run_ticks(q);
 	mutex_unlock(&xef->exec_queue.lock);
 
 	/* Get the total GPU cycles */
@@ -287,7 +287,7 @@ static void show_runtime(struct drm_printer *p, struct drm_file *file)
 
 		class_name = xe_hw_engine_class_to_str(class);
 		drm_printf(p, "drm-cycles-%s:\t%llu\n",
-			   class_name, xef->runtime[class]);
+			   class_name, xef->run_ticks[class]);
 		drm_printf(p, "drm-total-cycles-%s:\t%llu\n",
 			   class_name, gpu_timestamp);
 
@@ -310,6 +310,6 @@ static void show_runtime(struct drm_printer *p, struct drm_file *file)
 void xe_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file)
 {
 	show_meminfo(p, file);
-	show_runtime(p, file);
+	show_run_ticks(p, file);
 }
 #endif
drivers/gpu/drm/xe/xe_exec_queue.c +6 -6
index 0fd61fb4d104..841d3ea71e0d 100644
@@ -756,14 +756,14 @@ bool xe_exec_queue_is_idle(struct xe_exec_queue *q)
 }
 
 /**
- * xe_exec_queue_update_runtime() - Update runtime for this exec queue from hw
+ * xe_exec_queue_update_run_ticks() - Update run time in ticks for this exec queue
+ * from hw
  * @q: The exec queue
  *
- * Update the timestamp saved by HW for this exec queue and save runtime
- * calculated by using the delta from last update. On multi-lrc case, only the
- * first is considered.
+ * Update the timestamp saved by HW for this exec queue and save run ticks
+ * calculated by using the delta from last update.
  */
-void xe_exec_queue_update_runtime(struct xe_exec_queue *q)
+void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
 {
 	struct xe_file *xef;
 	struct xe_lrc *lrc;
@@ -789,7 +789,7 @@ void xe_exec_queue_update_runtime(struct xe_exec_queue *q)
 	 */
 	lrc = &q->lrc[0];
 	new_ts = xe_lrc_update_timestamp(lrc, &old_ts);
-	xef->runtime[q->class] += (new_ts - old_ts) * q->width;
+	xef->run_ticks[q->class] += (new_ts - old_ts) * q->width;
 }
 
 void xe_exec_queue_kill(struct xe_exec_queue *q)
drivers/gpu/drm/xe/xe_exec_queue.h +1 -1
index e0f07d28ee1a..289a3a51d2a2 100644
@@ -75,6 +75,6 @@ struct dma_fence *xe_exec_queue_last_fence_get(struct xe_exec_queue *e,
 					       struct xe_vm *vm);
 void xe_exec_queue_last_fence_set(struct xe_exec_queue *e, struct xe_vm *vm,
 				  struct dma_fence *fence);
-void xe_exec_queue_update_runtime(struct xe_exec_queue *q);
+void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q);
 
 #endif
drivers/gpu/drm/xe/xe_execlist.c +1 -1
index bd7f27efe0e0..8e5c591fcecd 100644
@@ -306,7 +306,7 @@ static void execlist_job_free(struct drm_sched_job *drm_job)
 {
 	struct xe_sched_job *job = to_xe_sched_job(drm_job);
 
-	xe_exec_queue_update_runtime(job->q);
+	xe_exec_queue_update_run_ticks(job->q);
 	xe_sched_job_put(job);
 }
 
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
index 54778189cfd5..ee078a6d174a 100644
@@ -763,7 +763,7 @@ static void guc_exec_queue_free_job(struct drm_sched_job *drm_job)
 {
 	struct xe_sched_job *job = to_xe_sched_job(drm_job);
 
-	xe_exec_queue_update_runtime(job->q);
+	xe_exec_queue_update_run_ticks(job->q);
 
 	trace_xe_sched_job_free(job);
 	xe_sched_job_put(job);
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm/xe: Do not access xe file when updating exec queue run_ticks

The current code is running into a use after free case where xe file is closed before the exec queue run_ticks can be updated. This is occurring in the xe_file_close path. To fix that, do not access xe file when updating the exec queue run_ticks. Instead store the exec queue run_ticks locally in the exec queue object and accumulate it when the user dumps the drm client stats. We know that the xe file is valid when user is dumping the run_ticks for the drm client, so this effectively removes the dependency on xe file object in xe_exec_queue_update_run_ticks().

v2:
- Fix the accumulation of q->run_ticks delta into xe file run_ticks
- s/runtime/run_ticks/ (Rodrigo)

Fixes 6109f24f87d7 ("drm/xe: Add helper to accumulate exec queue runtime")
---
 drivers/gpu/drm/xe/xe_drm_client.c       | 5 ++++-
 drivers/gpu/drm/xe/xe_exec_queue.c       | 5 +----
 drivers/gpu/drm/xe/xe_exec_queue_types.h | 4 ++++
 3 files changed, 9 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_drm_client.c +4 -1
index 1dcae0e6d66d..4a19b771e3a0 100644
@@ -251,8 +251,11 @@ static void show_run_ticks(struct drm_printer *p, struct drm_file *file)
 
 	/* Accumulate all the exec queues from this client */
 	mutex_lock(&xef->exec_queue.lock);
-	xa_for_each(&xef->exec_queue.xa, i, q)
+	xa_for_each(&xef->exec_queue.xa, i, q) {
 		xe_exec_queue_update_run_ticks(q);
+		xef->run_ticks[q->class] += q->run_ticks - q->old_run_ticks;
+		q->old_run_ticks = q->run_ticks;
+	}
 	mutex_unlock(&xef->exec_queue.lock);
 
 	/* Get the total GPU cycles */
drivers/gpu/drm/xe/xe_exec_queue.c +1 -4
index 841d3ea71e0d..4e9cee088f25 100644
@@ -765,7 +765,6 @@ bool xe_exec_queue_is_idle(struct xe_exec_queue *q)
  */
 void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
 {
-	struct xe_file *xef;
 	struct xe_lrc *lrc;
 	u32 old_ts, new_ts;
 
@@ -777,8 +776,6 @@ void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
 	if (!q->vm || !q->vm->xef)
 		return;
 
-	xef = q->vm->xef;
-
 	/*
 	 * Only sample the first LRC. For parallel submission, all of them are
 	 * scheduled together and we compensate that below by multiplying by
@@ -789,7 +786,7 @@ void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
 	 */
 	lrc = &q->lrc[0];
 	new_ts = xe_lrc_update_timestamp(lrc, &old_ts);
-	xef->run_ticks[q->class] += (new_ts - old_ts) * q->width;
+	q->run_ticks += (new_ts - old_ts) * q->width;
 }
 
 void xe_exec_queue_kill(struct xe_exec_queue *q)
drivers/gpu/drm/xe/xe_exec_queue_types.h +4 -0
index ee78d497d838..6551e45933a3 100644
@@ -151,6 +151,10 @@ struct xe_exec_queue {
 	 * Protected by @vm's resv. Unused if @vm == NULL.
 	 */
 	u64 tlb_flush_seqno;
+	/** @old_run_ticks: prior hw engine class run time in ticks for this exec queue */
+	u64 old_run_ticks;
+	/** @run_ticks: hw engine class run time in ticks for this exec queue */
+	u64 run_ticks;
 	/** @lrc: logical ring context for this exec queue */
 	struct xe_lrc lrc[];
 };
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 2/2] drm/xe: Do not access xe file when updating exec queue run_ticks

Quoted 29 lines · Umesh Nerlige Ramappa

The current code is running into a use after free case where xe file is closed before the exec queue run_ticks can be updated. This is occurring in the xe_file_close path. To fix that, do not access xe file when updating the exec queue run_ticks. Instead store the exec queue run_ticks locally in the exec queue object and accumulate it when the user dumps the drm client stats. We know that the xe file is valid when user is dumping the run_ticks for the drm client, so this effectively removes the dependency on xe file object in xe_exec_queue_update_run_ticks().

v2:
- Fix the accumulation of q->run_ticks delta into xe file run_ticks
- s/runtime/run_ticks/ (Rodrigo)

Fixes 6109f24f87d7 ("drm/xe: Add helper to accumulate exec queue runtime")

---

drivers/gpu/drm/xe/xe_drm_client.c       | 5 ++++-
drivers/gpu/drm/xe/xe_exec_queue.c       | 5 +----
drivers/gpu/drm/xe/xe_exec_queue_types.h | 4 ++++
3 files changed, 9 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_drm_client.c +0 -0
index 1dcae0e6d66d..4a19b771e3a0 100644

drivers/gpu/drm/xe/xe_drm_client.c static void show_run_ticks(struct drm_printer *p, struct drm_file *file) · line 257

Quoted 8 lines
drivers/gpu/drm/xe/xe_drm_client.c +3 -1
@@ -251,8 +251,11 @@ static void show_run_ticks(struct drm_printer *p, struct drm_file *file)
 
 	/* Accumulate all the exec queues from this client */
 	mutex_lock(&xef->exec_queue.lock);
-	xa_for_each(&xef->exec_queue.xa, i, q)
+	xa_for_each(&xef->exec_queue.xa, i, q) {
 		xe_exec_queue_update_run_ticks(q);
+		xef->run_ticks[q->class] += q->run_ticks - q->old_run_ticks;
+		q->old_run_ticks = q->run_ticks;

these 2 lines are kind of a layer violation... we shouldn't be setting something in xe_exec_queue from xe_drm_client. Since we can't return the delta from xe_exec_queue_update_run_ticks() since it's called from places other than the query by userspace, we should probably move these 2 lines code to a function like.... xe_exec_queue_get_delta_run_ticks() or s/get/flush/ or something else.

other than that, lgtm. Thanks for the fix.

Lucas De Marchi