* [PATCH v4 1/2] drm/msm/gem: defer shared GPU VM teardown until last VMA user
2026-12-04 23:10 [PATCH v4 0/2] drm/msm/gem: defer shared GPU VM teardown until last VMA user Vladimir Bely
@ 2026-12-04 23:10 ` Vladimir Bely
2026-12-04 23:10 ` [PATCH v4 2/2] drm/msm/gem: use guard(gem_lock) in has_vma() Vladimir Bely
1 sibling, 0 replies; 3+ messages in thread
From: Vladimir Bely @ 2026-12-04 23:10 UTC (permalink / raw)
To: Rob Clark, Dmitry Baryshkov
Cc: Abhinav Kumar, Jessica Zhang, Sean Paul, Marijn Suijten,
David Airlie, Simona Vetter, linux-arm-msm, dri-devel, freedreno,
linux-kernel
On GPUs without per-process pagetables (a2xx-a5xx), every context shares
priv->gpu->vm. When a buffer is shared between two processes (for example,
an app and a compositor via dma-buf), closing the handle in one process
calls msm_gem_close() -> put_iova_spaces(obj, ctx->vm, ...), which tears
down the single shared GPU VMA while the other process is still rendering
with it. On Adreno 306 (MSM8916), this triggers continuous GPU IOMMU
faults and display corruption.
Skip the per-handle teardown in msm_gem_close() when ctx->vm is the shared
priv->gpu->vm, and instead tear down that mapping in msm_gem_vma_put() once
vma_ref drops to zero -- matching how the shared priv->kms->vm is already
handled. Check has_vma() before waiting on fences or locking priv->gpu->vm
so GPUs with per-process pagetables pay no extra cost.
After waiting unlocked for fences, re-check both vma_ref and fence
signaling under msm_gem_lock_vm_and_obj() before calling put_iova_spaces()
so a concurrent msm_gem_vma_get() (or a get/submit/put cycle) cannot race
with teardown. Also re-check vma_ref under the lock for priv->kms->vm.
Fixes: 111fdd2198e6 ("drm/msm: drm_gpuvm conversion")
Cc: stable@vger.kernel.org
Signed-off-by: Vladimir Bely <vlwwwwww@gmail.com>
---
drivers/gpu/drm/msm/msm_gem.c | 58 ++++++++++++++++++++++++++++++++++-
1 file changed, 57 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/msm/msm_gem.c b/drivers/gpu/drm/msm/msm_gem.c
index 6ee0e7d14..aac5926fb 100644
--- a/drivers/gpu/drm/msm/msm_gem.c
+++ b/drivers/gpu/drm/msm/msm_gem.c
@@ -53,8 +53,21 @@ static int msm_gem_open(struct drm_gem_object *obj, struct drm_file *file)
static void put_iova_spaces(struct drm_gem_object *obj, struct drm_gpuvm *vm,
bool close, const char *reason);
+static bool has_vma(struct drm_gem_object *obj, struct drm_gpuvm *vm)
+{
+ struct drm_gpuvm_bo *vm_bo;
+
+ msm_gem_lock(obj);
+ vm_bo = drm_gpuvm_bo_find(vm, obj);
+ drm_gpuvm_bo_put(vm_bo);
+ msm_gem_unlock(obj);
+
+ return !!vm_bo;
+}
+
static void msm_gem_close(struct drm_gem_object *obj, struct drm_file *file)
{
+ struct msm_drm_private *priv = obj->dev->dev_private;
struct msm_context *ctx = file->driver_priv;
struct drm_exec exec;
@@ -69,6 +82,15 @@ static void msm_gem_close(struct drm_gem_object *obj, struct drm_file *file)
if (!ctx->vm)
return;
+ /*
+ * Without per-process pagetables, all contexts share priv->gpu->vm,
+ * so tearing down the VMA on handle close would break other users of
+ * an imported/shared buffer. Leave it to msm_gem_vma_put() once the
+ * last user is gone.
+ */
+ if (priv->gpu && ctx->vm == priv->gpu->vm)
+ return;
+
/*
* VM_BIND does not depend on implicit teardown of VMAs on handle
* close, but instead on implicit teardown of the VM when the device
@@ -105,6 +127,39 @@ void msm_gem_vma_put(struct drm_gem_object *obj)
if (atomic_dec_return(&to_msm_bo(obj)->vma_ref))
return;
+ /*
+ * Without per-process pagetables, every context maps the object in
+ * the shared global GPU VM, see msm_gem_close(). Drop that mapping,
+ * and the reference it holds on the object, once the last user is
+ * gone. Check has_vma() first so GPUs with per-process pagetables
+ * don't wait on fences or lock the global GPU VM.
+ */
+ if (priv->gpu && priv->gpu->vm && has_vma(obj, priv->gpu->vm)) {
+ struct drm_exec exec;
+
+ for (;;) {
+ dma_resv_wait_timeout(obj->resv,
+ DMA_RESV_USAGE_BOOKKEEP, false,
+ MAX_SCHEDULE_TIMEOUT);
+ if (atomic_read(&to_msm_bo(obj)->vma_ref))
+ return;
+
+ msm_gem_lock_vm_and_obj(&exec, obj, priv->gpu->vm);
+ if (atomic_read(&to_msm_bo(obj)->vma_ref)) {
+ drm_exec_fini(&exec);
+ return;
+ }
+ if (dma_resv_test_signaled(obj->resv,
+ DMA_RESV_USAGE_BOOKKEEP)) {
+ put_iova_spaces(obj, priv->gpu->vm, true,
+ "vma_put");
+ drm_exec_fini(&exec);
+ break;
+ }
+ drm_exec_fini(&exec);
+ }
+ }
+
if (!priv->kms)
return;
@@ -112,7 +167,8 @@ void msm_gem_vma_put(struct drm_gem_object *obj)
struct drm_exec exec;
msm_gem_lock_vm_and_obj(&exec, obj, priv->kms->vm);
- put_iova_spaces(obj, priv->kms->vm, true, "vma_put");
+ if (!atomic_read(&to_msm_bo(obj)->vma_ref))
+ put_iova_spaces(obj, priv->kms->vm, true, "vma_put");
drm_exec_fini(&exec); /* drop locks */
#endif
}
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v4 2/2] drm/msm/gem: use guard(gem_lock) in has_vma()
2026-12-04 23:10 [PATCH v4 0/2] drm/msm/gem: defer shared GPU VM teardown until last VMA user Vladimir Bely
2026-12-04 23:10 ` [PATCH v4 1/2] " Vladimir Bely
@ 2026-12-04 23:10 ` Vladimir Bely
1 sibling, 0 replies; 3+ messages in thread
From: Vladimir Bely @ 2026-12-04 23:10 UTC (permalink / raw)
To: Rob Clark, Dmitry Baryshkov
Cc: Abhinav Kumar, Jessica Zhang, Sean Paul, Marijn Suijten,
David Airlie, Simona Vetter, linux-arm-msm, dri-devel, freedreno,
linux-kernel
Convert has_vma() from explicit msm_gem_lock()/msm_gem_unlock() to
guard(gem_lock)(obj) to match the rest of msm_gem.c while keeping the
preceding fix easy to backport to stable trees without guard(gem_lock).
Signed-off-by: Vladimir Bely <vlwwwwww@gmail.com>
---
drivers/gpu/drm/msm/msm_gem.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/msm/msm_gem.c b/drivers/gpu/drm/msm/msm_gem.c
index aac5926fb..06b8bc44b 100644
--- a/drivers/gpu/drm/msm/msm_gem.c
+++ b/drivers/gpu/drm/msm/msm_gem.c
@@ -57,10 +57,10 @@ static bool has_vma(struct drm_gem_object *obj, struct drm_gpuvm *vm)
{
struct drm_gpuvm_bo *vm_bo;
- msm_gem_lock(obj);
+ guard(gem_lock)(obj);
+
vm_bo = drm_gpuvm_bo_find(vm, obj);
drm_gpuvm_bo_put(vm_bo);
- msm_gem_unlock(obj);
return !!vm_bo;
}
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread