Skip to contentSkip to conversation

drm/msm: Fix VM_BIND job, queue and entity lifetimes

PATCH New Jiwoo Kim 2 participants 7 messages Nov 18

Subsystems (5)
lore
Share & tools
lore

Jiwoo Kim

author
#0 lore raw

v4 cover drm/msm: Fix VM_BIND job, queue and entity lifetimes

These fix what happens to VM_BIND jobs, queues and entities when the file or a VM_BIND queue is closed while jobs are still pending, and make the VM unusable when VM_BIND jobs of different queues apply an unmap before the map that it undoes.

Patch 1 makes msm_gem_vm_close() wait for VM_BIND jobs, as it does for submits. Patch 2 handles an unmap that finds part of its range not mapped, or that comes before the map it undoes, like a failed map. Patch 3 makes a VM_BIND job that the scheduler kills before it runs apply its queued unmaps before it drops its references to the unmapped objects, release its BO pins, and mark the VM unusable. Patch 4 destroys the VM_BIND entity when its queue is closed, instead of on the final put, which can come after the VM's scheduler has been torn down. Patch 5 puts the queue last when a job is freed, as the queue may hold the last reference to the VM.

Changes in v4:
- New patch 1, as Rob suggested: VM_BIND jobs add their fences to vm->last_fence, keeping the previous fence if the merge fails. Patch 4 is still needed for killed jobs, which the scheduler frees only after it has signalled their fences.
- New patch 2, for the ordering problem across VM_BIND queues that Sashiko found in v3 patch 2, and that v3 patch 1 also made reachable through killed jobs. Unmap errors are now handled like map errors, as Rob suggested, and so is an unmap that comes before the map it undoes. A job that fails in the ioctl after updating the VMAs now marks the VM unusable as well.
- Patch 3 (v3 patch 1): uses the checks of patch 2, and describes the window that Sashiko pointed out in v3, where a job that waits for a killed job applies its updates before the killed job's unmaps.
- Patch 4 (v3 patch 2): changelog updated for patch 1, and a second Fixes tag, for the commit that added VM_BIND jobs.

Changes in v3:
- Patch 1: only apply the queued ops of a job that the scheduler frees after killing it, once its dependencies and the previous job of its entity are done. The error path of msm_ioctl_vm_bind() frees the job right away, so v2 could apply its unmaps too early and out of order (Sashiko). That path is left as it is in mainline, and the title now says which jobs the patch is about.
- Patch 3: rebased on patch 1, title adjusted.

Changes in v2:
- New patch 1, for an issue that Sashiko reported on v1 patch 1, which that patch makes easier to hit.
- Patch 2: changelog refers to patch 1.

Review of v2 by Rob:
https://lore.kernel.org/r/CACSVV00ZppfBKwsHJ+NBYBB7a2f3g1O=6J4Gt1m_A74fudZQoQ@mail.gmail.com

v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]

Jiwoo Kim (5):
drm/msm: Wait for VM_BIND jobs in msm_gem_vm_close() drm/msm: Mark the VM unusable on out-of-order VM_BIND unmaps drm/msm: Release the pins and ops of a killed VM_BIND job drm/msm: Destroy the VM_BIND entity when its queue is closed drm/msm: Put the VM_BIND queue last when freeing a VM_BIND job

 drivers/gpu/drm/msm/msm_gem.h         |   7 +
 drivers/gpu/drm/msm/msm_gem_vma.c     | 242 ++++++++++++++++++++++++--
 drivers/gpu/drm/msm/msm_gpu.h         |   6 +
 drivers/gpu/drm/msm/msm_submitqueue.c |  50 ++++--
 4 files changed, 272 insertions(+), 33 deletions(-)

base-commit: 1e59855738454b14021b64115f0090eafc7526ae

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v4 1/5 drm/msm: Wait for VM_BIND jobs in msm_gem_vm_close()

msm_gem_vm_close() waits for vm->last_fence before it tears down the VM's scheduler and its remaining mappings, but only submits add their fences to it. msm_submitqueue_close() flushes the VM_BIND entities first, but drm_sched_entity_flush() gives up after MAX_WAIT_SCHED_ENTITY_Q_EMPTY when the process is exiting, or on a fatal signal, and a queue that was closed earlier with DRM_IOCTL_MSM_SUBMITQUEUE_CLOSE is not flushed at all. A VM_BIND job that is then still waiting for its dependencies is not waited for, so its pgtable updates can still be pending, and the job still queued on its entity, when the scheduler is torn down.

Add the fences of VM_BIND jobs to vm->last_fence as well, as submit_attach_object_fences() does for submits, but keep the previous fence if dma_fence_unwrap_merge() fails. As for submits, msm_gem_vm_close() waits for these fences without a timeout and uninterruptibly, so closing the file now also waits for the dependencies of the queued VM_BIND jobs.

This does not cover a job that is killed, e.g. on SIGKILL while the process exits: the scheduler signals the fence of such a job before it frees the job, and the free can drop the last reference to the queue. That is fixed by "drm/msm: Destroy the VM_BIND entity when its queue is closed".

This was found with an AI coding assistant, which also drafted this patch and its changelog. It has not been reproduced, and the change has only been compile-tested.

---
As for submits, a VM_BIND job whose in-fence does not signal now keeps msm_gem_vm_close() waiting, uninterruptibly.

submit_attach_object_fences() drops vm->last_fence when dma_fence_unwrap_merge() fails. This patch keeps the previous fence for VM_BIND jobs instead, and leaves the submit path as it is.

 drivers/gpu/drm/msm/msm_gem_vma.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/msm/msm_gem_vma.c +13 -1
index 8b702422030a..2f0698df1a9a 100644
@@ -6,6 +6,7 @@
 
 #include "drm/drm_file.h"
 #include "drm/msm_drm.h"
+#include "linux/dma-fence-unwrap.h"
 #include "linux/file.h"
 #include "linux/sync_file.h"
 
@@ -1443,7 +1444,7 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
 	struct msm_syncobj_post_dep *post_deps = NULL;
 	struct drm_syncobj **syncobjs_to_reset = NULL;
 	struct sync_file *sync_file = NULL;
-	struct dma_fence *fence;
+	struct dma_fence *fence, *last_fence;
 	int out_fence_fd = -1;
 	int ret, nr_bos = 0;
 	unsigned i;
@@ -1586,6 +1587,17 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
 	msm_syncobj_reset(syncobjs_to_reset, args->nr_in_syncobjs);
 	msm_syncobj_process_post_deps(post_deps, args->nr_out_syncobjs, fence);
 
+	/*
+	 * Let msm_gem_vm_close() wait for the job, as it does for submits.
+	 * The VM's resv, which protects last_fence, is still locked here.
+	 * If the merge fails, keep waiting for the previous fence at least.
+	 */
+	last_fence = dma_fence_unwrap_merge(fence, to_msm_vm(vm)->last_fence);
+	if (last_fence) {
+		dma_fence_put(to_msm_vm(vm)->last_fence);
+		to_msm_vm(vm)->last_fence = last_fence;
+	}
+
 	dma_fence_put(fence);
 
 out:
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v4 2/5 drm/msm: Mark the VM unusable on out-of-order VM_BIND unmaps

In reply to Jiwoo Kim

msm_ioctl_vm_bind() updates the VMAs of the VM right away, and queues the matching pgtable updates on the job, but the VM_BIND jobs of different queues are not ordered against each other. A job can thus unmap a VMA before the job of another queue that maps it has run. The unmap then finds nothing mapped, and drops the reference that its op holds on the object. When the map runs after it, the pages it maps are neither tracked by a VMA nor kept alive by a reference once that job is done.

Userspace is expected to order VM_BIND jobs that touch the same range, but the kernel must not end up with freed pages mapped when it does not. Treat an unmap that finds part of its range not mapped like a failed map: skip the remaining maps of the job and mark the VM unusable, after which SUBMIT and VM_BIND fail with -EPIPE. VM_BIND pgtables only use single-page granules, so such an unmap error only means that part of the range was not mapped.

That alone does not catch an unmap that runs while the range still holds the pages of an older mapping, whose own unmap is still queued on a third queue. So also have the VMAs that a job maps, and the unmap ops that undo them, refer to a small refcounted object that tells whether the job has applied its updates, and handle an unmap that comes before the map it undoes the same way. In msm_vma_job_run(), read vm->unusable, and set it on such an error, under mmu_lock, which also protects the done state, so that the result does not depend on the jobs of a VM being run one at a time.

A job that fails in msm_ioctl_vm_bind() after it queued pgtable updates is freed without applying them, so the VM no longer matches its pgtables, and a later unmap of one of its VMAs would be taken for an out-of-order one. Mark the VM unusable in that case too.

The patch and its changelog were drafted with an AI coding assistant. The issue has not been reproduced, and the change has only been compile-tested.

---
VMAs that the shrinker unmapped are no longer marked mapped, so no unmap is queued for them, and a VMA whose map has been applied is fully mapped, as VM_BIND pgtables only use single-page granules. So the new errors should not trigger for userspace that orders its VM_BIND jobs.

Not addressed here: a queued unmap only holds a reference on its object, which does not keep the shrinker from purging or evicting the pages before the unmap is applied. Julius Bairaktaris' "drm/msm: Keep objects of queued VM_BIND unmaps busy" addresses that:
https://lore.kernel.org/r/[email protected]

 drivers/gpu/drm/msm/msm_gem.h     |   7 ++
 drivers/gpu/drm/msm/msm_gem_vma.c | 126 ++++++++++++++++++++++++++++--
 2 files changed, 127 insertions(+), 6 deletions(-)
drivers/gpu/drm/msm/msm_gem.h +7 -0
index d8ec696d89d7..3174460def2e 100644
@@ -164,6 +164,7 @@ void msm_gem_vm_close(struct drm_gpuvm *gpuvm);
 void msm_gem_vm_unusable(struct drm_gpuvm *gpuvm);
 
 struct msm_fence_context;
+struct msm_vm_bind_done;
 
 #define MSM_VMA_DUMP (DRM_GPUVA_USERBITS << 0)
 
@@ -185,6 +186,12 @@ struct msm_gem_vma {
 
 	/** @mapped: Is this VMA mapped? */
 	bool mapped;
+
+	/**
+	 * @map_done: For a VMA created by a VM_BIND job, tells whether that
+	 * job has applied its pgtable updates.
+	 */
+	struct msm_vm_bind_done *map_done;
 };
 #define to_msm_vma(x) container_of(x, struct msm_gem_vma, base)
 
drivers/gpu/drm/msm/msm_gem_vma.c +120 -6

282 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v4 2/5] drm/msm: Mark the VM unusable on out-of-order VM_BIND unmaps

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/msm: unmapped VMAs left in pgtables on msm_ioctl_vm_bind() failure - [Medium] drm/msm: missed…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/msm: unmapped VMAs left in pgtables on msm_ioctl_vm_bind() failure
- [Medium] drm/msm: missed msm_gem_vm_unusable() when vm_ops is empty on error
--

commit eeb2166c9690535d4ade679e940123eb63eece51
Author: Jiwoo Kim <[email protected]>

drm/msm: Mark the VM unusable on out-of-order VM_BIND unmaps

This commit tracks whether a VM_BIND job's map operations have been applied to the page tables and marks the VM unusable when an unmap runs out of order or when msm_ioctl_vm_bind() fails after queuing page table updates.

Quoted 4 lines
drivers/gpu/drm/msm/msm_gem_vma.c +0 -0
index 2f0698df1a9a0..228c16a3d9e49 100644

[ ... ]

Quoted 5 lines
drivers/gpu/drm/msm/msm_gem_vma.c +1 -0
@@ -783,10 +876,14 @@ msm_vma_job_free(struct drm_sched_job *_job)
 	while (!list_empty(&job->vm_ops)) {
 		struct msm_vm_op *op =
 			list_first_entry(&job->vm_ops, struct msm_vm_op, node);
+		if (op->op == MSM_VM_OP_UNMAP)

drivers/gpu/drm/msm/msm_gem_vma.c msm_vma_job_free(struct drm_sched_job *_job) · line 889

Quoted 10 lines
drivers/gpu/drm/msm/msm_gem_vma.c +3 -0
+			vm_bind_done_put(op->unmap.map_done);
 		list_del(&op->node);
 		kfree(op);
 	}
 
+	vm_bind_done_put(job->bind_done);
+
 	wake_up(&vm->prealloc_throttle.wait);
 
 	kfree(job);

[Severity: High]
When msm_ioctl_vm_bind() fails after msm_gem_vm_sm_step_unmap() or msm_gem_vm_sm_step_remap() has queued an MSM_VM_OP_UNMAP, does discarding the unexecuted unmap ops here leave the closed VMA's pages mapped in vm->mmu?

Both step functions set msm_vma->mapped = false and call msm_gem_vma_close(), which removes the VMA from job->vm and unlinks it from the GEM object's vm_bo before the job is pushed. If a later operation in vm_bind_job_prepare() or sync_file_create() fails, msm_vma_job_free() frees the queued MSM_VM_OP_UNMAP without unmapping its range from vm->mmu. If that range was already mapped by an earlier job, its PTEs remain active in vm->mmu while no VMA remains on obj->gpuva.list, so when the shrinker later evicts or purges the unpinned object via msm_gem_evict() or msm_gem_purge(), put_iova_spaces() does not unmap the range before put_pages() frees the backing pages.

In addition, msm_gem_vm_unusable() writes vm->unusable = true outside vm->mmu_lock (and returns before taking vm->mmu_lock when vm->log is NULL or empty), while msm_vma_job_run() does not hold the VM dma_resv lock. An earlier job that maps the closed VMA can therefore run msm_vma_job_run() concurrently, observe vm->unusable == false, and map the pages into vm->mmu even if the VMA was not yet mapped when msm_ioctl_vm_bind() failed.

[ ... ]

Quoted 6 lines
drivers/gpu/drm/msm/msm_gem_vma.c +2 -0
@@ -1604,6 +1709,15 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
 	if (ret)
 		vm_bind_job_unpin_objects(job);
 
+	/*
+	 * A job that fails after it queued pgtable updates is freed without

drivers/gpu/drm/msm/msm_gem_vma.c msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file) · line 1723

Quoted 10 lines
drivers/gpu/drm/msm/msm_gem_vma.c +7 -0
+	 * applying them, so the VM no longer matches its pgtables, and a
+	 * later unmap of one of its VMAs would be taken for an out of order
+	 * one.
+	 */
+	if (ret && !list_empty(&job->vm_ops))
+		msm_gem_vm_unusable(job->vm);
+
 	drm_exec_fini(&exec);
 out_unlock:
 	mutex_unlock(&queue->lock);

[Severity: Medium]
Can msm_ioctl_vm_bind() still fail after mutating job->vm while job->vm_ops is empty, leaving the modified VM usable?

In msm_gem_vm_sm_step_map(), vma_from_op() inserts the new VMA into job->vm with mapped = false before vm_op_enqueue() is called:

drivers/gpu/drm/msm/msm_gem_vma.c:msm_gem_vm_sm_step_map() {
	vma = vma_from_op(arg, &op->map);
	if (WARN_ON(IS_ERR(vma)))
		return PTR_ERR(vma);
	...
	ret = vm_op_enqueue(arg, (struct msm_vm_op){
	...
	});

	if (ret)
		return ret;
}

If vm_op_enqueue() fails with -ENOMEM on the first step of op 0, both i == 0 in vm_bind_job_prepare() and list_empty(&job->vm_ops) are true, so neither check calls msm_gem_vm_unusable(). If op 0 was MSM_VM_BIND_OP_MAP_NULL, the unmapped VMA stays in job->vm with vm_bo == NULL; a subsequent ioctl that remaps that range will then reach msm_gem_vm_sm_step_remap() with !mapped and call drm_gpuvm_bo_evict(vm_bo, true), dereferencing NULL.

The same check also misses failures in op 0 or sync_file_create() after op 0 modified job->vm without queueing a vm_op (such as closing an already-unmapped VMA or updating flags in place via arg->kept). Conversely, when vm_bind_job_prepare() fails at i > 0 with a non-empty job->vm_ops, both vm_bind_job_prepare() and msm_ioctl_vm_bind() call msm_gem_vm_unusable(), dumping the VM log twice.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#4 lore raw

PATCH v4 3/5 drm/msm: Release the pins and ops of a killed VM_BIND job

In reply to Jiwoo Kim

msm_vma_job_run() applies the pgtable updates that vm_bind_job_prepare() queued for a VM_BIND job, drops the GEM object references that vm_op_enqueue() took for them, and unpins the BOs of the job. When the entity of a job is killed, e.g. on SIGKILL while the process exits, the scheduler frees the job without running it. msm_vma_job_free() then frees the queued ops without applying them and without dropping their references, and leaves the BOs pinned. Besides the leaks, the VM no longer matches its pgtables: for example, the pages of a VMA that was unmapped from the VM are still mapped.

The scheduler only frees a killed job once its dependencies have signalled and the job queued before it on the same entity has run or been killed, so the jobs that it depends on, and those queued before it on its entity, have run and applied their updates by then, unless they were killed themselves. In msm_vma_job_free(), apply the queued updates the way msm_vma_job_run() does on error: skip the maps, but apply the unmaps, so that the references that the ops hold are only dropped once their pages are unmapped, and mark the VM unusable. Killed jobs can be freed concurrently, but as they only unmap, their order does not matter. If one of the unmaps comes before the map it undoes, which a job of another queue has not applied yet, that map is skipped, as the killed job marks the VM unusable under mmu_lock before it drops the lock. Move that part of msm_vma_job_run() into a helper for this. Also clear bos_pinned once msm_vma_job_run() has unpinned the BOs, as msm_job_run() does for submits, and unpin the BOs of a job that still has them pinned, taking the object lock, as the job's fence does not keep them busy anymore.

The scheduler signals the fence of a killed job before it frees the job, so a job that waits for it can apply its updates first. A map into a range that the killed job still has to unmap then fails, as the pages are still mapped, and marks the VM unusable.

msm_ioctl_vm_bind() frees the job when the ioctl fails, without waiting for its dependencies or the previous jobs of its queue, so the queued updates of such a job must not be applied. Move the freeing itself into vm_bind_job_free(), and call that directly from there.

The patch and its changelog were drafted with an AI coding assistant. The issue has not been reproduced, and the change has only been compile-tested.

Fixes 2e6a8a1fe2b2 ("drm/msm: Add VM_BIND ioctl")

---
At commit 2e6a8a1fe2b2, msm_vma_job_run() still unpinned the BOs under the object lock, and msm_gem_vm_unusable() did not exist yet, so older stable trees need an adapted backport.

Julius Bairaktaris' "drm/msm: Keep objects of queued VM_BIND unmaps busy" keeps the objects of queued unmaps busy with the job's fence:
https://lore.kernel.org/r/[email protected]
For a killed job, the scheduler signals that fence before it calls free_job(), so with both patches, these objects are no longer busy for the short time until this patch applies the unmaps. Without this patch, the unmaps of a killed job are never applied. Closing that window would need the objects to stay pinned until their unmaps are applied. The two patches apply in either order.

Not addressed here, as before this patch:
- The error path of msm_ioctl_vm_bind() still frees the queued ops of a failed job without applying them or dropping their references, but patch 2 now marks the VM unusable then. Their unmaps could only be applied after the job's dependencies and the previous jobs of its queue, i.e. by queuing the job anyway.
- The issues in the error handling of vm_bind_job_prepare() that Sashiko listed for v2: a VMA left in the VM when vm_op_enqueue() fails, which a later remap of a MAP_NULL area turns into a NULL dereference, and the vm_bo reference in msm_gem_vm_sm_step_remap().

 drivers/gpu/drm/msm/msm_gem_vma.c | 94 ++++++++++++++++++++++++++-----
 1 file changed, 81 insertions(+), 13 deletions(-)
drivers/gpu/drm/msm/msm_gem_vma.c +81 -13
index 228c16a3d9e4..fd104e2cee35 100644
@@ -772,22 +772,23 @@ vm_bind_map_done(struct msm_vm_bind_job *job, struct msm_vm_bind_done *map_done)
 	return !map_done || map_done == job->bind_done || map_done->done;
 }
 
-static struct dma_fence *
-msm_vma_job_run(struct drm_sched_job *_job)
+/*
+ * Apply the pgtable updates queued for a job, and drop the references that
+ * the ops hold.  If ret is an error, or one occurs, the remaining maps are
+ * skipped, but the unmaps are still applied, as the pages they unmap must
+ * not be freed while they are still mapped.
+ */
+static int
+vm_bind_job_apply_ops(struct msm_vm_bind_job *job, int ret)
 {
-	struct msm_vm_bind_job *job = to_msm_vm_bind_job(_job);
-	struct drm_device *dev = job->vm->drm;
 	struct msm_gem_vm *vm = to_msm_vm(job->vm);
-	struct drm_gem_object *obj;
-	int ret;
-
-	vm_dbg("");
 
 	mutex_lock(&vm->mmu_lock);
 	vm->mmu->prealloc = &job->prealloc;
 
 	/* vm->unusable is checked and set under mmu_lock, see below: */
-	ret = vm->unusable ? -EINVAL : 0;
+	if (vm->unusable)
+		ret = ret ?: -EINVAL;
 
 	while (!list_empty(&job->vm_ops)) {
 		struct msm_vm_op *op =
@@ -834,6 +835,21 @@ msm_vma_job_run(struct drm_sched_job *_job)
 	vm->mmu->prealloc = NULL;
 	mutex_unlock(&vm->mmu_lock);
 
+	return ret;
+}
+
+static struct dma_fence *
+msm_vma_job_run(struct drm_sched_job *_job)
+{
+	struct msm_vm_bind_job *job = to_msm_vm_bind_job(_job);
+	struct drm_device *dev = job->vm->drm;
+	struct drm_gem_object *obj;
+	int ret;
+
+	vm_dbg("");
+
+	ret = vm_bind_job_apply_ops(job, 0);
+
 	/*
 	 * We failed to perform at least _some_ of the pgtable updates, so
 	 * now the VM is in an undefined state.  Game over!
@@ -847,16 +863,22 @@ msm_vma_job_run(struct drm_sched_job *_job)
 		msm_gem_unpin_active(obj);
 	}
 
+	job->bos_pinned = false;
+
 	mutex_unlock(&dev->gem_lru_mutex);
 
 	/* VM_BIND ops are synchronous, so no fence to wait on: */
 	return NULL;
 }
 
+/*
+ * Free a job, whether it ran or not.  Ops that are still queued are freed
+ * without being applied, and without dropping their object references, as
+ * the pages of their unmaps may still be mapped.
+ */
 static void
-msm_vma_job_free(struct drm_sched_job *_job)
+vm_bind_job_free(struct msm_vm_bind_job *job)
 {
-	struct msm_vm_bind_job *job = to_msm_vm_bind_job(_job);
 	struct msm_gem_vm *vm = to_msm_vm(job->vm);
 	struct drm_gem_object *obj;
 
@@ -864,7 +886,20 @@ msm_vma_job_free(struct drm_sched_job *_job)
 
 	atomic_sub(job->prealloc.count, &vm->prealloc_throttle.in_flight);
 
-	drm_sched_job_cleanup(_job);
+	drm_sched_job_cleanup(&job->base);
+
+	/*
+	 * A job that was killed before it ran still has its BOs pinned.  The
+	 * job's fence does not keep them busy anymore, so take the object lock
+	 * to unpin them.
+	 */
+	if (job->bos_pinned) {
+		job_foreach_bo(obj, job) {
+			msm_gem_lock(obj);
+			msm_gem_unpin_locked(obj);
+			msm_gem_unlock(obj);
+		}
+	}
 
 	job_foreach_bo (obj, job)
 		drm_gem_object_put(obj);
@@ -889,6 +924,34 @@ msm_vma_job_free(struct drm_sched_job *_job)
 	kfree(job);
 }
 
+static void
+msm_vma_job_free(struct drm_sched_job *_job)
+{
+	struct msm_vm_bind_job *job = to_msm_vm_bind_job(_job);
+
+	/*
+	 * A job that the scheduler frees without having run it was killed,
+	 * e.g. because its entity was destroyed.  Its queued updates were
+	 * already applied to the VM's address space, but not to its pgtables.
+	 * The scheduler has waited for the job's dependencies, and for the job
+	 * before it on the entity to run or be killed, so apply its unmaps now,
+	 * before the references to the unmapped objects are dropped, and give
+	 * up on the VM, as its maps are skipped.  Killed jobs only unmap, so
+	 * the order in which they are freed does not matter.
+	 *
+	 * The scheduler signals the fence of a killed job before it frees the
+	 * job, so a job that waits for it can apply its updates first.  A map
+	 * into a range that this job still has to unmap then fails, and marks
+	 * the VM unusable.
+	 */
+	if (!list_empty(&job->vm_ops)) {
+		vm_bind_job_apply_ops(job, -ECANCELED);
+		msm_gem_vm_unusable(job->vm);
+	}
+
+	vm_bind_job_free(job);
+}
+
 static const struct drm_sched_backend_ops msm_vm_bind_ops = {
 	.run_job = msm_vma_job_run,
 	.free_job = msm_vma_job_free
@@ -1733,8 +1796,13 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
 	}
 
 	if (!IS_ERR_OR_NULL(job)) {
+		/*
+		 * Not msm_vma_job_free(): the job's dependencies and the
+		 * previous jobs of the queue may not have completed, so its
+		 * queued updates must not be applied here.
+		 */
 		if (ret)
-			msm_vma_job_free(&job->base);
+			vm_bind_job_free(job);
 	} else {
 		/*
 		 * If the submit hasn't yet taken ownership of the queue
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH v4 4/5 drm/msm: Destroy the VM_BIND entity when its queue is closed

In reply to Jiwoo Kim

The entity of a VM_BIND submitqueue is attached to the per-VM scheduler, and msm_gem_vm_close() calls drm_sched_fini() on that scheduler when the file is closed, which frees its run queues. The submitqueue, and with it the entity, is only destroyed on its final put, though, and every VM_BIND job holds a reference to it.

msm_gem_vm_close() waits for the fences of VM_BIND jobs first, but when a job is killed, e.g. on SIGKILL while the process exits, the scheduler signals the fence of the job before it frees the job. The free can then drop the last reference to the queue after drm_sched_fini() has run, and the final put calls drm_sched_entity_destroy(), which reads the freed run queue in drm_sched_entity_flush().

Destroy the VM_BIND entity when userspace closes the queue, on file close or with DRM_IOCTL_MSM_SUBMITQUEUE_CLOSE, instead of on the final put, and refuse new VM_BIND jobs on a closed queue. In the DRM_IOCTL_MSM_SUBMITQUEUE_CLOSE path, do it after dropping ctxlock, as destroying the entity waits for the jobs queued on it.

As a result, DRM_IOCTL_MSM_SUBMITQUEUE_CLOSE on a VM_BIND queue now waits (killably) for the jobs already queued on it, and a fatal signal during that wait cancels the remaining ones, where they used to still run. On file close, the jobs still queued when the wait gives up are now always cancelled, as they already were when the exiting process had been killed by SIGKILL, instead of being left on the entity. Cancelled jobs apply their unmaps, release their pins and references, and mark the VM unusable.

This was found with an AI coding assistant, which also drafted this patch and its changelog. It has not been reproduced, and the change has only been compile-tested.

Fixes 92395af63a99 ("drm/msm: Add VM_BIND submitqueue")
Fixes 2e6a8a1fe2b2 ("drm/msm: Add VM_BIND ioctl")

---
The scoped_guard() in msm_submitqueue_remove() relies on commit ea69d489d3a6 ("drm/msm: Rework queuelock"), so trees older than v7.3 need a manual backport.

If DRM_IOCTL_MSM_SUBMITQUEUE_CLOSE should rather not block, the destroy of closed VM_BIND queues could instead be deferred to file close, e.g. with a per-context list of them. That would also keep the ioctl out of a killable wait for the queued jobs, which the freezer cannot interrupt.

 drivers/gpu/drm/msm/msm_gem_vma.c     |  5 +++
 drivers/gpu/drm/msm/msm_gpu.h         |  6 ++++
 drivers/gpu/drm/msm/msm_submitqueue.c | 50 +++++++++++++++++++--------
 3 files changed, 47 insertions(+), 14 deletions(-)
drivers/gpu/drm/msm/msm_gem_vma.c +5 -0
index fd104e2cee35..c526409f451d 100644
@@ -1668,6 +1668,11 @@ msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file)
 	if (ret)
 		goto out_post_unlock;
 
+	if (queue->vm_bind_closed) {
+		ret = -ENOENT;
+		goto out_unlock;
+	}
+
 	if (args->flags & MSM_VM_BIND_FENCE_FD_IN) {
 		struct dma_fence *in_fence;
 
drivers/gpu/drm/msm/msm_gpu.h +6 -0
index bc5b31eb1eca..561f2d998e99 100644
@@ -596,6 +596,12 @@ struct msm_gpu_submitqueue {
 	struct kref ref;
 	struct drm_sched_entity *entity;
 
+	/**
+	 * @vm_bind_closed: the queue was closed by userspace, and its VM_BIND
+	 * entity is being or has been destroyed.  Protected by @lock.
+	 */
+	bool vm_bind_closed;
+
 	/** @_vm_bind_entity: used for @entity pointer for VM_BIND queues */
 	struct drm_sched_entity _vm_bind_entity[0];
 };
drivers/gpu/drm/msm/msm_submitqueue.c +36 -14

82 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#6 lore raw

PATCH v4 5/5 drm/msm: Put the VM_BIND queue last when freeing a VM_BIND job

In reply to Jiwoo Kim

vm_bind_job_free() puts the job's submitqueue and then wakes up vm->prealloc_throttle.wait. The job does not hold a reference to the VM of its own: the VM is kept alive by the context, which the queue holds a reference to. When the job holds the last reference to the queue, as it can once the file has been closed, for example when a killed job is freed, the put can drop the last reference to the VM, and the wakeup then uses a VM that msm_gem_vm_free() has released.

Since commit 01c8d1f385f7 ("drm/msm: RCU-free the scheduler-containing ring and VM objects"), msm_gem_vm_free() frees the VM with kfree_rcu(), but vm_bind_job_free() is not in an RCU read-side critical section, so a grace period can still end before the wakeup, e.g. if the task is preempted in between. Before that commit, the VM was freed right away.

Put the queue only after the last use of the VM.

This was found with an AI coding assistant, which also drafted this patch and its changelog. It has not been reproduced, and the change has only been compile-tested.

Fixes b74fae5492d1 ("drm/msm: Add VM_BIND throttling")
---
 drivers/gpu/drm/msm/msm_gem_vma.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/msm/msm_gem_vma.c +6 -1
index c526409f451d..3e9657982291 100644
@@ -904,7 +904,6 @@ vm_bind_job_free(struct msm_vm_bind_job *job)
 	job_foreach_bo (obj, job)
 		drm_gem_object_put(obj);
 
-	msm_submitqueue_put(job->queue);
 	dma_fence_put(job->fence);
 
 	/* In error paths, we could have unexecuted ops: */
@@ -921,6 +920,12 @@ vm_bind_job_free(struct msm_vm_bind_job *job)
 
 	wake_up(&vm->prealloc_throttle.wait);
 
+	/*
+	 * The queue may hold the last reference to the context, and so to the
+	 * VM, so put it only once we are done with the VM.
+	 */
+	msm_submitqueue_put(job->queue);
+
 	kfree(job);
 }
 
Signature · 1 line
2.43.0