Skip to contentSkip to conversation

drm/msm: Destroy the VM_BIND entity when its queue is closed

PATCH New Jiwoo Kim 2 participants 3 messages Nov 14

Subsystems (5)
lore
Share & tools
lore

Jiwoo Kim

author
#0 lore raw

PATCH 1/2 drm/msm: Destroy the VM_BIND entity when its queue is closed

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 itself is only destroyed on its final put, though, and every VM_BIND job holds a reference to it. msm_submitqueue_close() only flushes the entity, and a queue that was closed earlier with MSM_SUBMITQUEUE_CLOSE is not flushed at all.

The flush gives up after a timeout when the process exits, or on a fatal signal, and it only kills the queued jobs when the process exits because of SIGKILL. In these cases, and after MSM_SUBMITQUEUE_CLOSE, the entity can still have a callback armed on the in-fence of a VM_BIND job when drm_sched_fini() runs. When that fence signals, drm_sched_entity_wakeup() dereferences entity->rq->sched, and so reads a run queue that drm_sched_fini() has freed. If the jobs were killed, the final put of the queue then calls drm_sched_entity_destroy(), which reads the same freed run queue in drm_sched_entity_flush(). If they were not, they never run, and keep the queue, its context and the VM alive.

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

As a result, 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 process was killed while exiting, instead of being left on the entity.

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")

---
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 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.

Not changed here: for a job that never ran, msm_vma_job_free() neither unpins the BOs that msm_ioctl_vm_bind() pinned nor drops the GEM references that vm_op_enqueue() took. That already happens to VM_BIND jobs killed when the process exits, and with this patch also to the ones cancelled by a fatal signal during MSM_SUBMITQUEUE_CLOSE.

Deferring drm_sched_fini() of the VM scheduler to msm_gem_vm_free() was considered instead. It does not help: the stranded jobs keep the queue, its context and the VM alive, so the entities would still have to be destroyed at close.

 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 8b702422030a..0cf518420656 100644
@@ -1499,6 +1499,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

base-commit: 1e59855738454b14021b64115f0090eafc7526ae

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 2/2 drm/msm: Put the VM_BIND queue last in msm_vma_job_free()

msm_vma_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 cancelled 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 msm_vma_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")

---
Independent of 1/2.

 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 0cf518420656..b4b284ca5b38 100644
@@ -775,7 +775,6 @@ msm_vma_job_free(struct drm_sched_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: */
@@ -788,6 +787,12 @@ msm_vma_job_free(struct drm_sched_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
#2 lore raw
In reply to Jiwoo Kim
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/msm: BO pin leak and VM state corruption in msm_vma_job_free() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/msm: BO pin leak and VM state corruption in msm_vma_job_free()
--

commit 805b414859330e06db0b87fe27330012423f8dcc
Author: Jiwoo Kim <[email protected]>

drm/msm: Destroy the VM_BIND entity when its queue is closed

This commit destroys the drm_sched_entity of a VM_BIND submitqueue when userspace closes the queue via MSM_SUBMITQUEUE_CLOSE or file close instead of on the final submitqueue reference drop, and rejects new VM_BIND jobs on closed queues.

Quoted 4 lines
drivers/gpu/drm/msm/msm_submitqueue.c +0 -0
index 8d550d281704e..2dbe42551bd8f 100644

[ ... ]

Quoted 12 lines
drivers/gpu/drm/msm/msm_submitqueue.c +8 -0
@@ -107,6 +104,25 @@ struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_context *ctx,
 	return NULL;
 }
 
+/*
+ * The VM_BIND scheduler is torn down by msm_gem_vm_close() when the file is
+ * closed, but a VM_BIND queue can outlive that, as long as a job still holds
+ * a reference to it.  So destroy the VM_BIND entity here, when userspace
+ * closes the queue, rather than on the final put.  Both ways a queue is taken
+ * off ctx->submitqueues come through here.
+ */
+static void msm_submitqueue_close_vm_bind(struct msm_gpu_submitqueue *queue)

drivers/gpu/drm/msm/msm_submitqueue.c struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_context *ctx, · line 124

Quoted 10 lines
drivers/gpu/drm/msm/msm_submitqueue.c +10 -0
+{
+	if (queue->entity != &queue->_vm_bind_entity[0])
+		return;
+
+	mutex_lock(&queue->lock);
+	queue->vm_bind_closed = true;
+	mutex_unlock(&queue->lock);
+
+	drm_sched_entity_destroy(queue->entity);
+}

[Severity: High]
Does cancelling queued VM_BIND jobs in drm_sched_entity_destroy() leak pinned GEM objects and leave job->vm out of sync with the IOMMU page tables?

Previously, msm_submitqueue_remove() did not touch the entity, and msm_submitqueue_close() only called drm_sched_entity_flush(), which left remaining jobs on the entity unless the process exited via SIGKILL. Calling drm_sched_entity_destroy() runs drm_sched_entity_fini() -> drm_sched_entity_kill() whenever the flush times out on file close (for example, while a job waits on an unsignaled in-fence) or is interrupted by a fatal signal during MSM_SUBMITQUEUE_CLOSE.

Killed jobs bypass msm_vma_job_run() and go straight to msm_vma_job_free(), which neither unpins job->bos_pinned BOs nor drops the op->obj reference acquired by vm_op_enqueue():

drivers/gpu/drm/msm/msm_gem_vma.c:msm_vma_job_free() {
	...
	/* In error paths, we could have unexecuted ops: */
	while (!list_empty(&job->vm_ops)) {
		struct msm_vm_op *op =
			list_first_entry(&job->vm_ops, struct msm_vm_op, node);
		list_del(&op->node);
		kfree(op);
	}
	...
}

In addition, vm_bind_job_prepare() has already updated the VMA state in job->vm (marking new VMAs as mapped and closing unmapped ones), so discarding job->vm_ops without applying the page-table updates or calling msm_gem_vm_unusable(job->vm) leaves the VM state inconsistent while the context can still be used after MSM_SUBMITQUEUE_CLOSE.

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