* [PATCH v4 1/5] drm/msm: Wait for VM_BIND jobs in msm_gem_vm_close()
2026-11-18 2:07 [PATCH v4 0/5] drm/msm: Fix VM_BIND job, queue and entity lifetimes Jiwoo Kim
@ 2026-11-18 2:07 ` Jiwoo Kim
2026-11-18 2:07 ` [PATCH v4 2/5] drm/msm: Mark the VM unusable on out-of-order VM_BIND unmaps Jiwoo Kim
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Jiwoo Kim @ 2026-11-18 2:07 UTC (permalink / raw)
To: Rob Clark, Dmitry Baryshkov
Cc: Akhil P Oommen, Konrad Dybcio, Anna Maniscalco,
Julius Bairaktaris, Abhinav Kumar, Jessica Zhang, Sean Paul,
Marijn Suijten, David Airlie, Simona Vetter, linux-arm-msm,
dri-devel, freedreno, linux-kernel, stable
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.
Fixes: 2e6a8a1fe2b2 ("drm/msm: Add VM_BIND ioctl")
Cc: stable@vger.kernel.org
Suggested-by: Rob Clark <robin.clark@oss.qualcomm.com>
Link: https://lore.kernel.org/r/CACSVV00ZppfBKwsHJ+NBYBB7a2f3g1O=6J4Gt1m_A74fudZQoQ@mail.gmail.com
Signed-off-by: Jiwoo Kim <arpe1618@gmail.com>
---
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(-)
diff --git a/drivers/gpu/drm/msm/msm_gem_vma.c b/drivers/gpu/drm/msm/msm_gem_vma.c
index 8b702422030a..2f0698df1a9a 100644
--- a/drivers/gpu/drm/msm/msm_gem_vma.c
+++ b/drivers/gpu/drm/msm/msm_gem_vma.c
@@ -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:
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v4 2/5] drm/msm: Mark the VM unusable on out-of-order VM_BIND unmaps
2026-11-18 2:07 [PATCH v4 0/5] drm/msm: Fix VM_BIND job, queue and entity lifetimes Jiwoo Kim
2026-11-18 2:07 ` [PATCH v4 1/5] drm/msm: Wait for VM_BIND jobs in msm_gem_vm_close() Jiwoo Kim
@ 2026-11-18 2:07 ` Jiwoo Kim
2026-11-18 2:07 ` [PATCH v4 3/5] drm/msm: Release the pins and ops of a killed VM_BIND job Jiwoo Kim
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Jiwoo Kim @ 2026-11-18 2:07 UTC (permalink / raw)
To: Rob Clark, Dmitry Baryshkov
Cc: Akhil P Oommen, Konrad Dybcio, Anna Maniscalco,
Julius Bairaktaris, Abhinav Kumar, Jessica Zhang, Sean Paul,
Marijn Suijten, David Airlie, Simona Vetter, linux-arm-msm,
dri-devel, freedreno, linux-kernel, stable
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.
Fixes: 2e6a8a1fe2b2 ("drm/msm: Add VM_BIND ioctl")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/r/sashiko-outbox-214650@kernel.org
Suggested-by: Rob Clark <robin.clark@oss.qualcomm.com>
Link: https://lore.kernel.org/r/CACSVV00ZppfBKwsHJ+NBYBB7a2f3g1O=6J4Gt1m_A74fudZQoQ@mail.gmail.com
Signed-off-by: Jiwoo Kim <arpe1618@gmail.com>
---
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/20261111080619.244138-1-julius@bairaktaris.de
drivers/gpu/drm/msm/msm_gem.h | 7 ++
drivers/gpu/drm/msm/msm_gem_vma.c | 126 ++++++++++++++++++++++++++++--
2 files changed, 127 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/msm/msm_gem.h b/drivers/gpu/drm/msm/msm_gem.h
index d8ec696d89d7..3174460def2e 100644
--- a/drivers/gpu/drm/msm/msm_gem.h
+++ b/drivers/gpu/drm/msm/msm_gem.h
@@ -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)
diff --git a/drivers/gpu/drm/msm/msm_gem_vma.c b/drivers/gpu/drm/msm/msm_gem_vma.c
index 2f0698df1a9a..228c16a3d9e4 100644
--- a/drivers/gpu/drm/msm/msm_gem_vma.c
+++ b/drivers/gpu/drm/msm/msm_gem_vma.c
@@ -63,6 +63,12 @@ struct msm_vm_unmap_op {
* a submitqueue (ie. process cleanup)
*/
int queue_id;
+
+ /**
+ * @map_done: For an unmap queued by a VM_BIND job, tells whether the
+ * map that it undoes has been applied.
+ */
+ struct msm_vm_bind_done *map_done;
};
/**
@@ -97,6 +103,20 @@ struct msm_vm_op {
struct drm_gem_object *obj;
};
+/**
+ * struct msm_vm_bind_done - Whether a VM_BIND job has applied its updates
+ *
+ * The VMAs that a VM_BIND job maps, and the unmap ops that undo them later,
+ * hold a reference to it, so that such an unmap can tell whether the map it
+ * undoes has been applied to the pgtables yet.
+ */
+struct msm_vm_bind_done {
+ /** @ref: reference count */
+ struct kref ref;
+ /** @done: the job has applied its updates, protected by vm->mmu_lock */
+ bool done;
+};
+
/**
* struct msm_vm_bind_job - Tracking for a VM_BIND ioctl
*
@@ -119,6 +139,8 @@ struct msm_vm_bind_job {
struct msm_mmu_prealloc prealloc;
/** @vm_ops: a list of struct msm_vm_op */
struct list_head vm_ops;
+ /** @bind_done: whether the job has applied its updates to the pgtables */
+ struct msm_vm_bind_done *bind_done;
/** @bos_pinned: are the GEM objects being bound pinned? */
bool bos_pinned;
/** @nr_ops: the number of userspace requested ops */
@@ -156,6 +178,27 @@ static inline struct msm_vm_bind_job *to_msm_vm_bind_job(struct drm_sched_job *j
return container_of(job, struct msm_vm_bind_job, base);
}
+static struct msm_vm_bind_done *
+vm_bind_done_get(struct msm_vm_bind_done *done)
+{
+ if (done)
+ kref_get(&done->ref);
+ return done;
+}
+
+static void
+vm_bind_done_release(struct kref *ref)
+{
+ kfree(container_of(ref, struct msm_vm_bind_done, ref));
+}
+
+static void
+vm_bind_done_put(struct msm_vm_bind_done *done)
+{
+ if (done)
+ kref_put(&done->ref, vm_bind_done_release);
+}
+
static void
msm_gem_vm_free(struct drm_gpuvm *gpuvm)
{
@@ -243,7 +286,7 @@ vm_log(struct msm_gem_vm *vm, const char *op, uint64_t iova, uint64_t range, int
vm->log_idx = (vm->log_idx + 1) & ((1 << vm->log_shift) - 1);
}
-static void
+static int
vm_unmap_op(struct msm_gem_vm *vm, const struct msm_vm_unmap_op *op)
{
const char *reason = op->reason;
@@ -253,7 +296,7 @@ vm_unmap_op(struct msm_gem_vm *vm, const struct msm_vm_unmap_op *op)
vm_log(vm, reason, op->iova, op->range, op->queue_id);
- vm->mmu->funcs->unmap(vm->mmu, op->iova, op->range);
+ return vm->mmu->funcs->unmap(vm->mmu, op->iova, op->range);
}
static int
@@ -361,6 +404,7 @@ void msm_gem_vma_close(struct drm_gpuva *vma)
drm_gpuva_remove(vma);
drm_gpuva_unlink(vma);
+ vm_bind_done_put(msm_vma->map_done);
kfree(vma);
}
@@ -484,6 +528,9 @@ vm_op_enqueue(struct op_arg *arg, struct msm_vm_op _op)
if (op->obj)
drm_gem_object_get(op->obj);
+ if (op->op == MSM_VM_OP_UNMAP)
+ vm_bind_done_get(op->unmap.map_done);
+
return 0;
}
@@ -541,6 +588,7 @@ msm_gem_vm_sm_step_map(struct drm_gpuva_op *op, void *_arg)
vma->flags = ((struct op_arg *)arg)->flags;
to_msm_vma(vma)->mapped = true;
+ to_msm_vma(vma)->map_done = vm_bind_done_get(job->bind_done);
return 0;
}
@@ -554,6 +602,7 @@ msm_gem_vm_sm_step_remap(struct drm_gpuva_op *op, void *arg)
struct drm_gpuva *prev_vma = NULL, *next_vma = NULL;
struct drm_gpuvm_bo *vm_bo = orig_vma->vm_bo;
bool mapped = to_msm_vma(orig_vma)->mapped;
+ struct msm_vm_bind_done *map_done;
unsigned flags;
int ret;
@@ -571,6 +620,7 @@ msm_gem_vm_sm_step_remap(struct drm_gpuva_op *op, void *arg)
.iova = unmap_start,
.range = unmap_range,
.queue_id = job->queue->id,
+ .map_done = to_msm_vma(orig_vma)->map_done,
},
.obj = orig_vma->gem.obj,
});
@@ -601,25 +651,34 @@ msm_gem_vm_sm_step_remap(struct drm_gpuva_op *op, void *arg)
*/
flags = orig_vma->flags;
+ /* The pages of prev_vma and next_vma were mapped with orig_vma: */
+ map_done = vm_bind_done_get(to_msm_vma(orig_vma)->map_done);
+
msm_gem_vma_close(orig_vma);
if (op->remap.prev) {
prev_vma = vma_from_op(arg, op->remap.prev);
- if (WARN_ON(IS_ERR(prev_vma)))
+ if (WARN_ON(IS_ERR(prev_vma))) {
+ vm_bind_done_put(map_done);
return PTR_ERR(prev_vma);
+ }
vm_dbg("prev_vma: %p:%p: %016llx %016llx", vm, prev_vma, prev_vma->va.addr, prev_vma->va.range);
to_msm_vma(prev_vma)->mapped = mapped;
+ to_msm_vma(prev_vma)->map_done = vm_bind_done_get(map_done);
prev_vma->flags = flags;
}
if (op->remap.next) {
next_vma = vma_from_op(arg, op->remap.next);
- if (WARN_ON(IS_ERR(next_vma)))
+ if (WARN_ON(IS_ERR(next_vma))) {
+ vm_bind_done_put(map_done);
return PTR_ERR(next_vma);
+ }
vm_dbg("next_vma: %p:%p: %016llx %016llx", vm, next_vma, next_vma->va.addr, next_vma->va.range);
to_msm_vma(next_vma)->mapped = mapped;
+ to_msm_vma(next_vma)->map_done = vm_bind_done_get(map_done);
next_vma->flags = flags;
}
@@ -628,6 +687,7 @@ msm_gem_vm_sm_step_remap(struct drm_gpuva_op *op, void *arg)
/* Drop the previous ref: */
drm_gpuvm_bo_put(vm_bo);
+ vm_bind_done_put(map_done);
return 0;
}
@@ -678,6 +738,7 @@ msm_gem_vm_sm_step_unmap(struct drm_gpuva_op *op, void *_arg)
.iova = vma->va.addr,
.range = vma->va.range,
.queue_id = job->queue->id,
+ .map_done = msm_vma->map_done,
},
.obj = vma->gem.obj,
});
@@ -701,6 +762,16 @@ static const struct drm_gpuvm_ops msm_gpuvm_ops = {
.sm_step_unmap = msm_gem_vm_sm_step_unmap,
};
+/*
+ * Whether the map that an unmap op of @job undoes has been applied. The ops
+ * of @job itself are applied in order, so its own maps come first.
+ */
+static bool
+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)
{
@@ -708,13 +779,16 @@ msm_vma_job_run(struct drm_sched_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->unusable ? -EINVAL : 0;
+ 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;
+
while (!list_empty(&job->vm_ops)) {
struct msm_vm_op *op =
list_first_entry(&job->vm_ops, struct msm_vm_op, node);
@@ -730,7 +804,17 @@ msm_vma_job_run(struct drm_sched_job *_job)
ret = vm_map_op(vm, &op->map);
break;
case MSM_VM_OP_UNMAP:
- vm_unmap_op(vm, &op->unmap);
+ /*
+ * An unmap that finds part of its range not mapped, or
+ * that comes before the map it undoes, means that the
+ * jobs that touch this range were not ordered. If the
+ * map was applied after the unmap, nothing would track
+ * its pages, so handle this like a failed map.
+ */
+ if (vm_unmap_op(vm, &op->unmap) ||
+ !vm_bind_map_done(job, op->unmap.map_done))
+ ret = ret ?: -EINVAL;
+ vm_bind_done_put(op->unmap.map_done);
break;
}
drm_gem_object_put(op->obj);
@@ -738,6 +822,15 @@ msm_vma_job_run(struct drm_sched_job *_job)
kfree(op);
}
+ /*
+ * Mark the VM unusable before dropping mmu_lock, so that the maps of
+ * a job that applies its updates after an out of order unmap are
+ * skipped.
+ */
+ if (ret)
+ vm->unusable = true;
+ job->bind_done->done = true;
+
vm->mmu->prealloc = NULL;
mutex_unlock(&vm->mmu_lock);
@@ -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)
+ 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);
@@ -968,9 +1065,17 @@ vm_bind_job_create(struct drm_device *dev, struct drm_file *file, struct drm_gpu
if (!job)
return ERR_PTR(-ENOMEM);
+ job->bind_done = kzalloc_obj(*job->bind_done);
+ if (!job->bind_done) {
+ kfree(job);
+ return ERR_PTR(-ENOMEM);
+ }
+ kref_init(&job->bind_done->ref);
+
ret = drm_sched_job_init(&job->base, queue->entity, 1, queue,
file->client_id);
if (ret) {
+ kfree(job->bind_done);
kfree(job);
return ERR_PTR(ret);
}
@@ -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
+ * 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);
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v4 3/5] drm/msm: Release the pins and ops of a killed VM_BIND job
2026-11-18 2:07 [PATCH v4 0/5] drm/msm: Fix VM_BIND job, queue and entity lifetimes Jiwoo Kim
2026-11-18 2:07 ` [PATCH v4 1/5] drm/msm: Wait for VM_BIND jobs in msm_gem_vm_close() Jiwoo Kim
2026-11-18 2:07 ` [PATCH v4 2/5] drm/msm: Mark the VM unusable on out-of-order VM_BIND unmaps Jiwoo Kim
@ 2026-11-18 2:07 ` Jiwoo Kim
2026-11-18 2:07 ` [PATCH v4 4/5] drm/msm: Destroy the VM_BIND entity when its queue is closed Jiwoo Kim
2026-11-18 2:07 ` [PATCH v4 5/5] drm/msm: Put the VM_BIND queue last when freeing a VM_BIND job Jiwoo Kim
4 siblings, 0 replies; 6+ messages in thread
From: Jiwoo Kim @ 2026-11-18 2:07 UTC (permalink / raw)
To: Rob Clark, Dmitry Baryshkov
Cc: Akhil P Oommen, Konrad Dybcio, Anna Maniscalco,
Julius Bairaktaris, Abhinav Kumar, Jessica Zhang, Sean Paul,
Marijn Suijten, David Airlie, Simona Vetter, linux-arm-msm,
dri-devel, freedreno, linux-kernel, stable
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")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/r/sashiko-outbox-214234@kernel.org
Signed-off-by: Jiwoo Kim <arpe1618@gmail.com>
---
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/20261111080619.244138-1-julius@bairaktaris.de
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(-)
diff --git a/drivers/gpu/drm/msm/msm_gem_vma.c b/drivers/gpu/drm/msm/msm_gem_vma.c
index 228c16a3d9e4..fd104e2cee35 100644
--- a/drivers/gpu/drm/msm/msm_gem_vma.c
+++ b/drivers/gpu/drm/msm/msm_gem_vma.c
@@ -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
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v4 4/5] drm/msm: Destroy the VM_BIND entity when its queue is closed
2026-11-18 2:07 [PATCH v4 0/5] drm/msm: Fix VM_BIND job, queue and entity lifetimes Jiwoo Kim
` (2 preceding siblings ...)
2026-11-18 2:07 ` [PATCH v4 3/5] drm/msm: Release the pins and ops of a killed VM_BIND job Jiwoo Kim
@ 2026-11-18 2:07 ` Jiwoo Kim
2026-11-18 2:07 ` [PATCH v4 5/5] drm/msm: Put the VM_BIND queue last when freeing a VM_BIND job Jiwoo Kim
4 siblings, 0 replies; 6+ messages in thread
From: Jiwoo Kim @ 2026-11-18 2:07 UTC (permalink / raw)
To: Rob Clark, Dmitry Baryshkov
Cc: Akhil P Oommen, Konrad Dybcio, Anna Maniscalco,
Julius Bairaktaris, Abhinav Kumar, Jessica Zhang, Sean Paul,
Marijn Suijten, David Airlie, Simona Vetter, linux-arm-msm,
dri-devel, freedreno, linux-kernel, stable
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")
Cc: stable@vger.kernel.org
Signed-off-by: Jiwoo Kim <arpe1618@gmail.com>
---
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(-)
diff --git a/drivers/gpu/drm/msm/msm_gem_vma.c b/drivers/gpu/drm/msm/msm_gem_vma.c
index fd104e2cee35..c526409f451d 100644
--- a/drivers/gpu/drm/msm/msm_gem_vma.c
+++ b/drivers/gpu/drm/msm/msm_gem_vma.c
@@ -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;
diff --git a/drivers/gpu/drm/msm/msm_gpu.h b/drivers/gpu/drm/msm/msm_gpu.h
index bc5b31eb1eca..561f2d998e99 100644
--- a/drivers/gpu/drm/msm/msm_gpu.h
+++ b/drivers/gpu/drm/msm/msm_gpu.h
@@ -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];
};
diff --git a/drivers/gpu/drm/msm/msm_submitqueue.c b/drivers/gpu/drm/msm/msm_submitqueue.c
index 8d550d281704..2dbe42551bd8 100644
--- a/drivers/gpu/drm/msm/msm_submitqueue.c
+++ b/drivers/gpu/drm/msm/msm_submitqueue.c
@@ -79,9 +79,6 @@ void msm_submitqueue_destroy(struct kref *kref)
idr_destroy(&queue->fence_idr);
- if (queue->entity == &queue->_vm_bind_entity[0])
- drm_sched_entity_destroy(queue->entity);
-
msm_context_put(queue->ctx);
kfree(queue);
@@ -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)
+{
+ 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);
+}
+
void msm_submitqueue_close(struct msm_context *ctx)
{
struct msm_gpu_submitqueue *queue, *tmp;
@@ -119,8 +135,7 @@ void msm_submitqueue_close(struct msm_context *ctx)
* be any more user ioctls coming our way
*/
list_for_each_entry_safe(queue, tmp, &ctx->submitqueues, node) {
- if (queue->entity == &queue->_vm_bind_entity[0])
- drm_sched_entity_flush(queue->entity, MAX_WAIT_SCHED_ENTITY_Q_EMPTY);
+ msm_submitqueue_close_vm_bind(queue);
list_del(&queue->node);
msm_submitqueue_put(queue);
}
@@ -326,7 +341,7 @@ int msm_submitqueue_query(struct drm_device *drm, struct msm_context *ctx,
int msm_submitqueue_remove(struct msm_context *ctx, u32 id)
{
- struct msm_gpu_submitqueue *entry;
+ struct msm_gpu_submitqueue *entry, *queue = NULL;
if (!ctx)
return 0;
@@ -338,16 +353,23 @@ int msm_submitqueue_remove(struct msm_context *ctx, u32 id)
if (!id)
return -ENOENT;
- guard(rwsem_write)(&ctx->ctxlock);
-
- list_for_each_entry(entry, &ctx->submitqueues, node) {
- if (entry->id == id) {
- list_del(&entry->node);
- msm_submitqueue_put(entry);
- return 0;
+ scoped_guard(rwsem_write, &ctx->ctxlock) {
+ list_for_each_entry(entry, &ctx->submitqueues, node) {
+ if (entry->id == id) {
+ list_del(&entry->node);
+ queue = entry;
+ break;
+ }
}
}
- return -ENOENT;
+ if (!queue)
+ return -ENOENT;
+
+ /* Not under ctxlock: destroying the entity can wait for its jobs */
+ msm_submitqueue_close_vm_bind(queue);
+ msm_submitqueue_put(queue);
+
+ return 0;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v4 5/5] drm/msm: Put the VM_BIND queue last when freeing a VM_BIND job
2026-11-18 2:07 [PATCH v4 0/5] drm/msm: Fix VM_BIND job, queue and entity lifetimes Jiwoo Kim
` (3 preceding siblings ...)
2026-11-18 2:07 ` [PATCH v4 4/5] drm/msm: Destroy the VM_BIND entity when its queue is closed Jiwoo Kim
@ 2026-11-18 2:07 ` Jiwoo Kim
4 siblings, 0 replies; 6+ messages in thread
From: Jiwoo Kim @ 2026-11-18 2:07 UTC (permalink / raw)
To: Rob Clark, Dmitry Baryshkov
Cc: Akhil P Oommen, Konrad Dybcio, Anna Maniscalco,
Julius Bairaktaris, Abhinav Kumar, Jessica Zhang, Sean Paul,
Marijn Suijten, David Airlie, Simona Vetter, linux-arm-msm,
dri-devel, freedreno, linux-kernel, stable
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")
Cc: stable@vger.kernel.org
Signed-off-by: Jiwoo Kim <arpe1618@gmail.com>
---
drivers/gpu/drm/msm/msm_gem_vma.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/msm/msm_gem_vma.c b/drivers/gpu/drm/msm/msm_gem_vma.c
index c526409f451d..3e9657982291 100644
--- a/drivers/gpu/drm/msm/msm_gem_vma.c
+++ b/drivers/gpu/drm/msm/msm_gem_vma.c
@@ -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);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread