* [PATCH v4 0/5] drm/msm: Fix VM_BIND job, queue and entity lifetimes
@ 2026-11-18 2:07 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
` (4 more replies)
0 siblings, 5 replies; 7+ 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
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/20261114141851.134290-1-arpe1618@gmail.com
v2: https://lore.kernel.org/r/20261114081902.1669-1-arpe1618@gmail.com
v1: https://lore.kernel.org/r/20261114062541.2001-1-arpe1618@gmail.com
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
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [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; 7+ 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] 7+ 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:45 ` sashiko-bot
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, 1 reply; 7+ 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] 7+ 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; 7+ 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] 7+ 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; 7+ 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] 7+ 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; 7+ 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] 7+ messages in thread
* Re: [PATCH v4 2/5] drm/msm: Mark the VM unusable on out-of-order VM_BIND unmaps
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:45 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-18 2:45 UTC (permalink / raw)
To: Jiwoo Kim; +Cc: dri-devel
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 <arpe1618@gmail.com>
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.
Link: https://lore.kernel.org/r/CACSVV00ZppfBKwsHJ+NBYBB7a2f3g1O=6J4Gt1m_A74fudZQoQ@mail.gmail.com
> diff --git a/drivers/gpu/drm/msm/msm_gem_vma.c b/drivers/gpu/drm/msm/msm_gem_vma.c
> index 2f0698df1a9a0..228c16a3d9e49 100644
> --- a/drivers/gpu/drm/msm/msm_gem_vma.c
> +++ b/drivers/gpu/drm/msm/msm_gem_vma.c
[ ... ]
> @@ -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);
[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.
[ ... ]
> @@ -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);
[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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118020716.1698-1-arpe1618@gmail.com?part=2
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-18 2:46 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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:45 ` sashiko-bot
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 ` [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
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.