Skip to contentSkip to conversation

drm/xe: Fix races on fdinfo

PATCH In review Lucas De Marchi 3 participants 9 messages Oct 26, 2024 – Oct 28, 2024

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

cover drm/xe: Fix races on fdinfo

The current reading of engine utilization has same races. This should fix most of them while also drastically reducing the update rate needed on "normal apps".

I left tests/xe_drm_fdinfo running on 2 systems and saw no failures after 100 iterations.

Lucas De Marchi (3):
drm/xe: Add trace to lrc timestamp update drm/xe: Accumulate exec queue timestamp on destroy drm/xe: Stop accumulating LRC timestamp on job_free

 drivers/gpu/drm/xe/Makefile        |  1 +
 drivers/gpu/drm/xe/xe_exec_queue.c | 14 ++++++++
 drivers/gpu/drm/xe/xe_guc_submit.c |  2 --
 drivers/gpu/drm/xe/xe_lrc.c        |  3 ++
 drivers/gpu/drm/xe/xe_trace_lrc.c  |  9 ++++++
 drivers/gpu/drm/xe/xe_trace_lrc.h  | 52 ++++++++++++++++++++++++++++++
 6 files changed, 79 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/xe/xe_trace_lrc.c
 create mode 100644 drivers/gpu/drm/xe/xe_trace_lrc.h
Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/3 drm/xe: Add trace to lrc timestamp update

Help debugging when LRC timestamp is updated for a exec queue.

---
 drivers/gpu/drm/xe/Makefile       |  1 +
 drivers/gpu/drm/xe/xe_lrc.c       |  3 ++
 drivers/gpu/drm/xe/xe_trace_lrc.c |  9 ++++++
 drivers/gpu/drm/xe/xe_trace_lrc.h | 52 +++++++++++++++++++++++++++++++
 4 files changed, 65 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/xe_trace_lrc.c
 create mode 100644 drivers/gpu/drm/xe/xe_trace_lrc.h
drivers/gpu/drm/xe/Makefile +1 -0
index bc7a04ce69fd..21d69935c336 100644
@@ -101,6 +101,7 @@ xe-y += xe_bb.o \
 	xe_trace.o \
 	xe_trace_bo.o \
 	xe_trace_guc.o \
+	xe_trace_lrc.o \
 	xe_ttm_sys_mgr.o \
 	xe_ttm_stolen_mgr.o \
 	xe_ttm_vram_mgr.o \
drivers/gpu/drm/xe/xe_lrc.c +3 -0
index 4f64c7f4e68d..4b65da77c6e0 100644
@@ -25,6 +25,7 @@
 #include "xe_map.h"
 #include "xe_memirq.h"
 #include "xe_sriov.h"
+#include "xe_trace_lrc.h"
 #include "xe_vm.h"
 #include "xe_wa.h"
 
@@ -1758,5 +1759,7 @@ u32 xe_lrc_update_timestamp(struct xe_lrc *lrc, u32 *old_ts)
 
 	lrc->ctx_timestamp = xe_lrc_ctx_timestamp(lrc);
 
+	trace_xe_lrc_update_timestamp(lrc, *old_ts);
+
 	return lrc->ctx_timestamp;
 }
drivers/gpu/drm/xe/xe_trace_lrc.c new file +9 -0
new file mode 100644
index 000000000000..ab9b7e2970bc
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#ifndef __CHECKER__
+#define CREATE_TRACE_POINTS
+#include "xe_trace_lrc.h"
+#endif
drivers/gpu/drm/xe/xe_trace_lrc.h new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.47.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] drm/xe: Add trace to lrc timestamp update

Quoted 3 lines · Lucas De Marchi

Help debugging when LRC timestamp is updated for a exec queue.

Quoted 114 lines
---
 drivers/gpu/drm/xe/Makefile       |  1 +
 drivers/gpu/drm/xe/xe_lrc.c       |  3 ++
 drivers/gpu/drm/xe/xe_trace_lrc.c |  9 ++++++
 drivers/gpu/drm/xe/xe_trace_lrc.h | 52 +++++++++++++++++++++++++++++++
 4 files changed, 65 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/xe_trace_lrc.c
 create mode 100644 drivers/gpu/drm/xe/xe_trace_lrc.h
drivers/gpu/drm/xe/Makefile +1 -0
index bc7a04ce69fd..21d69935c336 100644
@@ -101,6 +101,7 @@ xe-y += xe_bb.o \
 	xe_trace.o \
 	xe_trace_bo.o \
 	xe_trace_guc.o \
+	xe_trace_lrc.o \
 	xe_ttm_sys_mgr.o \
 	xe_ttm_stolen_mgr.o \
 	xe_ttm_vram_mgr.o \
drivers/gpu/drm/xe/xe_lrc.c +3 -0
index 4f64c7f4e68d..4b65da77c6e0 100644
@@ -25,6 +25,7 @@
 #include "xe_map.h"
 #include "xe_memirq.h"
 #include "xe_sriov.h"
+#include "xe_trace_lrc.h"
 #include "xe_vm.h"
 #include "xe_wa.h"
 
@@ -1758,5 +1759,7 @@ u32 xe_lrc_update_timestamp(struct xe_lrc *lrc, u32 *old_ts)
 
 	lrc->ctx_timestamp = xe_lrc_ctx_timestamp(lrc);
 
+	trace_xe_lrc_update_timestamp(lrc, *old_ts);
+
 	return lrc->ctx_timestamp;
 }
drivers/gpu/drm/xe/xe_trace_lrc.c new file +9 -0
new file mode 100644
index 000000000000..ab9b7e2970bc
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#ifndef __CHECKER__
+#define CREATE_TRACE_POINTS
+#include "xe_trace_lrc.h"
+#endif
drivers/gpu/drm/xe/xe_trace_lrc.h new file +52 -0
new file mode 100644
index 000000000000..5c669a0b2180
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM xe
+
+#if !defined(_XE_TRACE_LRC_H_) || defined(TRACE_HEADER_MULTI_READ)
+#define _XE_TRACE_LRC_H_
+
+#include <linux/tracepoint.h>
+#include <linux/types.h>
+
+#include "xe_gt_types.h"
+#include "xe_lrc.h"
+#include "xe_lrc_types.h"
+
+#define __dev_name_lrc(lrc)	dev_name(gt_to_xe((lrc)->fence_ctx.gt)->drm.dev)
+
+TRACE_EVENT(xe_lrc_update_timestamp,
+	    TP_PROTO(struct xe_lrc *lrc, uint32_t old),
+	    TP_ARGS(lrc, old),
+	    TP_STRUCT__entry(
+		     __field(struct xe_lrc *, lrc)
+		     __field(u32, old)
+		     __field(u32, new)
+		     __string(name, lrc->fence_ctx.name)
+		     __string(device_id, __dev_name_lrc(lrc))
+	    ),
+
+	    TP_fast_assign(
+		   __entry->lrc	= lrc;
+		   __entry->old = old;
+		   __entry->new = lrc->ctx_timestamp;
+		   __assign_str(name);
+		   __assign_str(device_id);
+		   ),
+	    TP_printk("lrc=:%p lrc->name=%s old=%u new=%u device_id:%s",
+		      __entry->lrc, __get_str(name),
+		      __entry->old, __entry->new,
+		      __get_str(device_id))
+);
+
+#endif
+
+/* This part must be outside protection */
+#undef TRACE_INCLUDE_PATH
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_PATH ../../drivers/gpu/drm/xe
+#define TRACE_INCLUDE_FILE xe_trace_lrc
+#include <trace/define_trace.h>
#3 lore raw

PATCH 2/3 drm/xe: Accumulate exec queue timestamp on destroy

In reply to Lucas De Marchi

When the exec queue is destroyed, there's a race between a query to the fdinfo and the exec queue value being updated: after the destroy ioctl, if the fdinfo is queried before a call to guc_exec_queue_free_job(), the wrong utilization is reported: it's not accumulated on the query since the queue was removed from the array, and the value wasn't updated yet by the free_job().

Explicitly accumulate the engine utilization so the right value is visible after the ioctl return.

---
 drivers/gpu/drm/xe/xe_exec_queue.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/gpu/drm/xe/xe_exec_queue.c +8 -0
index d098d2dd1b2d..b15ca84b2422 100644
@@ -829,6 +829,14 @@ int xe_exec_queue_destroy_ioctl(struct drm_device *dev, void *data,
 
 	xe_exec_queue_kill(q);
 
+	/*
+	 * After killing and destroying the exec queue, make sure userspace has
+	 * an updated view of the run ticks, regardless if this was the last
+	 * ref: since the exec queue is removed from xef->exec_queue.xa, a
+	 * query to fdinfo after this returns could not account for this load.
+	 */
+	xe_exec_queue_update_run_ticks(q);
+
 	trace_xe_exec_queue_close(q);
 	xe_exec_queue_put(q);
 
Signature · 1 line
2.47.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] drm/xe: Accumulate exec queue timestamp on destroy

Quoted 7 lines · Lucas De Marchi

When the exec queue is destroyed, there's a race between a query to the fdinfo and the exec queue value being updated: after the destroy ioctl, if the fdinfo is queried before a call to guc_exec_queue_free_job(), the wrong utilization is reported: it's not accumulated on the query since the queue was removed from the array, and the value wasn't updated yet by the free_job().

Quoted 6 lines

Explicitly accumulate the engine utilization so the right value is visible after the ioctl return.

LGTM

Quoted 23 lines
---
 drivers/gpu/drm/xe/xe_exec_queue.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/gpu/drm/xe/xe_exec_queue.c +8 -0
index d098d2dd1b2d..b15ca84b2422 100644
@@ -829,6 +829,14 @@ int xe_exec_queue_destroy_ioctl(struct drm_device *dev, void *data,
 
 	xe_exec_queue_kill(q);
 
+	/*
+	 * After killing and destroying the exec queue, make sure userspace has
+	 * an updated view of the run ticks, regardless if this was the last
+	 * ref: since the exec queue is removed from xef->exec_queue.xa, a
+	 * query to fdinfo after this returns could not account for this load.
+	 */
+	xe_exec_queue_update_run_ticks(q);
+
 	trace_xe_exec_queue_close(q);
 	xe_exec_queue_put(q);
 
#5 lore raw

PATCH 3/3 drm/xe: Stop accumulating LRC timestamp on job_free

In reply to Lucas De Marchi

The exec queue timestamp is only really useful when it's being queried through the fdinfo. There's no need to update it so often, on every job_free. Tracing a simple app like vkcube running shows an update rate of ~ 120Hz.

The update on job_free() is used to cover a gap: if exec queue is created and destroyed rapidily, before a new query, the timestamp still needs to be accumulated and accounted on the xef. Initial implementation in commit 6109f24f87d7 ("drm/xe: Add helper to accumulate exec queue runtime") couldn't do it on the exec_queue_fini since the xef could be gone at that point. However since commit ce8c161cbad4 ("drm/xe: Add ref counting for xe_file") the xef is refcounted and the exec queue has a reference.

Improve the fix in commit 2149ded63079 ("drm/xe: Fix use after free when client stats are captured") by reducing the frequency in which the update is needed.

Fixes 2149ded63079 ("drm/xe: Fix use after free when client stats are captured")
---
 drivers/gpu/drm/xe/xe_exec_queue.c | 6 ++++++
 drivers/gpu/drm/xe/xe_guc_submit.c | 2 --
 2 files changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/xe/xe_exec_queue.c +6 -0
index b15ca84b2422..bc2fc917e0de 100644
@@ -260,8 +260,14 @@ void xe_exec_queue_fini(struct xe_exec_queue *q)
 {
 	int i;
 
+	/*
+	 * Before releasing our ref to lrc and xef, accumulate our run ticks
+	 */
+	xe_exec_queue_update_run_ticks(q);
+
 	for (i = 0; i < q->width; ++i)
 		xe_lrc_put(q->lrc[i]);
+
 	__xe_exec_queue_free(q);
 }
 
drivers/gpu/drm/xe/xe_guc_submit.c +0 -2
index e5d7c767a744..ebe4665d9159 100644
@@ -747,8 +747,6 @@ 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_run_ticks(job->q);
-
 	trace_xe_sched_job_free(job);
 	xe_sched_job_put(job);
 }
Signature · 1 line
2.47.0
#6 lore raw

✗ Fi.CI.CHECKPATCH: warning for drm/xe: Fix races on fdinfo

In reply to Lucas De Marchi
== Series Details ==

== Series Details ==

Series: drm/xe: Fix races on fdinfo
URL : https://patchwork.freedesktop.org/series/140538/
State : warning

== Summary ==

Error: dim checkpatch failed
be111ababfbe drm/xe: Add trace to lrc timestamp update
-:44: WARNING:FILE_PATH_CHANGES: added, moved or deleted file(s), does MAINTAINERS need updating? #44:
new file mode 100644

-:87: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #87: FILE: drivers/gpu/drm/xe/xe_trace_lrc.h:24:
+ TP_STRUCT__entry(

-:95: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #95: FILE: drivers/gpu/drm/xe/xe_trace_lrc.h:32:
+ TP_fast_assign(

-:113: CHECK:SPACING: spaces preferred around that '/' (ctx:VxV) #113: FILE: drivers/gpu/drm/xe/xe_trace_lrc.h:50:
+#define TRACE_INCLUDE_PATH ../../drivers/gpu/drm/xe

                              ^

-:113: CHECK:SPACING: spaces preferred around that '/' (ctx:VxV) #113: FILE: drivers/gpu/drm/xe/xe_trace_lrc.h:50:
+#define TRACE_INCLUDE_PATH ../../drivers/gpu/drm/xe

                                 ^

-:113: CHECK:SPACING: spaces preferred around that '/' (ctx:VxV) #113: FILE: drivers/gpu/drm/xe/xe_trace_lrc.h:50:
+#define TRACE_INCLUDE_PATH ../../drivers/gpu/drm/xe

                                         ^

-:113: CHECK:SPACING: spaces preferred around that '/' (ctx:VxV) #113: FILE: drivers/gpu/drm/xe/xe_trace_lrc.h:50:
+#define TRACE_INCLUDE_PATH ../../drivers/gpu/drm/xe

                                             ^

-:113: CHECK:SPACING: spaces preferred around that '/' (ctx:VxV) #113: FILE: drivers/gpu/drm/xe/xe_trace_lrc.h:50:
+#define TRACE_INCLUDE_PATH ../../drivers/gpu/drm/xe

                                                 ^

total: 0 errors, 1 warnings, 7 checks, 82 lines checked d88c0ee83457 drm/xe: Accumulate exec queue timestamp on destroy 8a635cead973 drm/xe: Stop accumulating LRC timestamp on job_free

#8 lore raw

✓ Fi.CI.BAT: success for drm/xe: Fix races on fdinfo

In reply to Lucas De Marchi
== Series Details ==

== Series Details ==

Series: drm/xe: Fix races on fdinfo
URL : https://patchwork.freedesktop.org/series/140538/
State : success

== Summary ==

CI Bug Log - changes from CI_DRM_15602 -> Patchwork_140538v1
====================================================

Summary
-------

**SUCCESS**

No regressions found.

External URL: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_140538v1/index.html

Participating hosts (47 -> 46)
------------------------------

Missing (1): fi-snb-2520m

Known issues
------------

Here are the changes found in Patchwork_140538v1 that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@i915_selftest@live:
- bat-mtlp-8: [PASS][1] -> [ABORT][2] ([i915#12133] / [i915#12216])
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_15602/bat-mtlp-8/igt@[email protected]
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_140538v1/bat-mtlp-8/igt@[email protected]
- bat-atsm-1: [PASS][3] -> [ABORT][4] ([i915#12133])
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_15602/bat-atsm-1/igt@[email protected]
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_140538v1/bat-atsm-1/igt@[email protected]

* igt@i915_selftest@live@guc_multi_lrc:
- bat-atsm-1: [PASS][5] -> [ABORT][6] ([i915#12305])
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_15602/bat-atsm-1/igt@i915_selftest@live@guc_multi_lrc.html
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_140538v1/bat-atsm-1/igt@i915_selftest@live@guc_multi_lrc.html

* igt@i915_selftest@live@workarounds:
- bat-mtlp-8: [PASS][7] -> [ABORT][8] ([i915#12216])
[7]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_15602/bat-mtlp-8/igt@i915_selftest@[email protected]
[8]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_140538v1/bat-mtlp-8/igt@i915_selftest@[email protected]

#### Possible fixes ####

* igt@i915_selftest@live:
- bat-dg2-8: [ABORT][9] ([i915#12133]) -> [PASS][10]
[9]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_15602/bat-dg2-8/igt@[email protected]
[10]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_140538v1/bat-dg2-8/igt@[email protected]

* igt@i915_selftest@live@guc_multi_lrc:
- bat-dg2-8: [ABORT][11] ([i915#12133] / [i915#12305]) -> [PASS][12]
[11]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_15602/bat-dg2-8/igt@i915_selftest@live@guc_multi_lrc.html
[12]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_140538v1/bat-dg2-8/igt@i915_selftest@live@guc_multi_lrc.html

[i915#12133]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/12133
[i915#12216]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/12216
[i915#12305]: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/12305

Build changes
-------------

* Linux: CI_DRM_15602 -> Patchwork_140538v1

CI-20190529: 20190529
CI_DRM_15602: 24707cf2a7c47d4cd192c97e73d58aa4011de154 @ git://anongit.freedesktop.org/gfx-ci/linux
IGT_8086: 18939acec2446c6644644186b090d16e366af8bc @ https://gitlab.freedesktop.org/drm/igt-gpu-tools.git
Patchwork_140538v1: 24707cf2a7c47d4cd192c97e73d58aa4011de154 @ git://anongit.freedesktop.org/gfx-ci/linux

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_140538v1/index.html