Skip to contentSkip to conversation

drm/xe: Make page-table updates using the default engine

PATCH New Matthew Brost 2 participants 3 messages Jul 5, 2023

Subsystems (2)
lore
Share & tools
lore

Matthew Brost

author maintainer
#0 lore raw

PATCH 1/2 drm/xe: Make page-table updates using the default engine

From: Thomas Hellström <[email protected]>

If the default engine m->eng was used, there is no check for idle and a cpu page-table update may thus happen in parallel with a gpu one. Don't allow CPU page-table updates with the default engine until the engine is idle.

---
 drivers/gpu/drm/xe/xe_migrate.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/xe/xe_migrate.c +3 -2
index 55e113dd7e82..b2fa2cb5f537 100644
@@ -1160,9 +1160,10 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 	int err = 0;
 	bool usm = !eng && xe->info.supports_usm;
 	bool first_munmap_rebind = vma && vma->first_munmap_rebind;
+	struct xe_engine *eng_override = !eng ? m->eng : eng;
 
 	/* Use the CPU if no in syncs and engine is idle */
-	if (no_in_syncs(syncs, num_syncs) && (!eng || xe_engine_is_idle(eng))) {
+	if (no_in_syncs(syncs, num_syncs) && xe_engine_is_idle(eng_override)) {
 		fence =  xe_migrate_update_pgtables_cpu(m, vm, bo, updates,
 							num_updates,
 							first_munmap_rebind,
@@ -1259,7 +1260,7 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 	if (!eng)
 		mutex_lock(&m->job_mutex);
 
-	job = xe_bb_create_migration_job(eng ?: m->eng, bb,
+	job = xe_bb_create_migration_job(eng_override, bb,
 					 xe_migrate_batch_base(m, usm),
 					 update_idx);
 	if (IS_ERR(job)) {
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 2/2 drm/xe: Make bind engines safe

We currently have a race between bind engines which can result in corrupted page tables leading to faults.

A simple example:
bind A 0x0000-0x1000, engine A, has unsatisfied in-fence bind B 0x1000-0x2000, engine B, no in-fences exec A uses 0x1000-0x2000

Bind B will pass bind A and exec A will fault. This occurs as bind A programs the root of the page table in a bind job which is held up by an in-fence. Bind B in this case just programs a leaf entry of the structure.

To fix this introduce a per VM interval tree to track cross engine conflicts. In the above example bind A would insert an dependency in the interval tree with a key of 0x0-0x7fffffffff, bind B would find that dependency and its bind job would scheduled behind the unsatisfied in-fence and bind A's job.

v2: Use interval tree, proper cleanup, fix build

Cc Thomas Hellström <[email protected]>
---
 drivers/gpu/drm/xe/xe_guc_submit.c      |  3 +
 drivers/gpu/drm/xe/xe_migrate.c         | 93 +++++++++++++++++++++++--
 drivers/gpu/drm/xe/xe_sched_job.h       | 14 ++++
 drivers/gpu/drm/xe/xe_sched_job_types.h | 15 ++++
 drivers/gpu/drm/xe/xe_vm.c              |  2 +
 drivers/gpu/drm/xe/xe_vm_types.h        |  5 ++
 6 files changed, 128 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/xe_guc_submit.c +3 -0
index 86c445903560..1a57d22df99b 100644
@@ -682,6 +682,9 @@ static void guc_engine_free_job(struct drm_sched_job *drm_job)
 	struct xe_sched_job *job = to_xe_sched_job(drm_job);
 
 	trace_xe_sched_job_free(job);
+
+	if (xe_sched_job_is_bind(job))
+		xe_sched_job_trigger_bind_cleanup(job);
 	xe_sched_job_put(job);
 }
 
drivers/gpu/drm/xe/xe_migrate.c +89 -4

135 lines not on this page · raw message

drivers/gpu/drm/xe/xe_sched_job.h +14 -0
index 5315ad8656c2..dec2e07e524b 100644
@@ -6,6 +6,7 @@
 #ifndef _XE_SCHED_JOB_H_
 #define _XE_SCHED_JOB_H_
 
+#include "xe_macros.h"
 #include "xe_sched_job_types.h"
 
 #define XE_SCHED_HANG_LIMIT 1
@@ -73,4 +74,17 @@ xe_sched_job_add_migrate_flush(struct xe_sched_job *job, u32 flags)
 
 bool xe_sched_job_is_migration(struct xe_engine *e);
 
+static inline bool
+xe_sched_job_is_bind(struct xe_sched_job *job)
+{
+	return !!job->bind_args.last;
+}
+
+static inline void
+xe_sched_job_trigger_bind_cleanup(struct xe_sched_job *job)
+{
+	XE_WARN_ON(!xe_sched_job_is_bind(job));
+	queue_work(system_wq, &job->bind_args.cleanup_work);
+}
+
 #endif
drivers/gpu/drm/xe/xe_sched_job_types.h +15 -0
index 5534bfacaa16..1e2581efd9f4 100644
@@ -37,6 +37,21 @@ struct xe_sched_job {
 		/** @value: write back value */
 		u64 value;
 	} user_fence;
+	/** @bind_args: if a bind job, the bind arguments */
+	struct {
+		/** @start: start of a bind job stored in a itree */
+		u64 start;
+		/** @last: last of a bind job stored in a itree */
+		u64 last;
+		/** @rb: RB node for itree to track deps for bind jobs */
+		struct rb_node rb;
+		/** @subtree_last: needed for itree lookup */
+		u64 subtree_last;
+		/** @cleanup_work: worker to cleanup bind job */
+		struct work_struct cleanup_work;
+		/** @tile_id: tile id of bind job */
+		u8 tile_id;
+	} bind_args;
 	/** @migrate_flush_flags: Additional flush flags for migration jobs */
 	u32 migrate_flush_flags;
 	/** @batch_addr: batch buffer address of job */
drivers/gpu/drm/xe/xe_vm.c +2 -0
index dbff75a9087b..382c896ac250 100644
@@ -1233,6 +1233,8 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
 		    tile->id != XE_VM_FLAG_GT_ID(flags))
 			continue;
 
+		vm->itree[id] = RB_ROOT_CACHED;
+
 		vm->pt_root[id] = xe_pt_create(vm, tile, xe->info.vm_max_level);
 		if (IS_ERR(vm->pt_root[id])) {
 			err = PTR_ERR(vm->pt_root[id]);
drivers/gpu/drm/xe/xe_vm_types.h +5 -0
index c148dd49a6ca..97151e4fd314 100644
@@ -160,6 +160,11 @@ struct xe_vm {
 
 	struct kref refcount;
 
+	/**
+	 * @itree: the interval tree to track of conflicts between bind engines
+	 */
+	struct rb_root_cached itree[XE_MAX_TILES_PER_DEVICE];
+
 	/* engine used for (un)binding vma's */
 	struct xe_engine *eng[XE_MAX_TILES_PER_DEVICE];
 
Signature · 1 line
2.34.1
#2 lore raw

[Intel-xe] ✗ CI.Patch_applied: failure for series starting with [1/2] drm/xe: Make page-table updates using the default engine

In reply to Matthew Brost
== Series Details ==

== Series Details ==

Series: series starting with [1/2] drm/xe: Make page-table updates using the default engine
URL : https://patchwork.freedesktop.org/series/120207/
State : failure

== Summary ==

=== Applying kernel patches on branch 'drm-xe-next' with base: ===
Base commit: cd26cb9c9 drm/xe: Make page-table updates using the default engine happen in order === git am output follows ===
error: patch failed: drivers/gpu/drm/xe/xe_migrate.c:1160
error: drivers/gpu/drm/xe/xe_migrate.c: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch
Applying: drm/xe: Make page-table updates using the default engine
Patch failed at 0001 drm/xe: Make page-table updates using the default engine When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".