mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drm/msm/gem: defer shared GPU VM teardown until last VMA user
@ 2026-11-29  0:09 Vladimir Bely
  2026-11-29  1:17 ` [PATCH v2] " Vladimir Bely
  0 siblings, 1 reply; 2+ messages in thread
From: Vladimir Bely @ 2026-11-29  0:09 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, Vladimir Bely, stable

After the conversion to drm_gpuvm, msm_gem_close() tears down the
object's VMAs in ctx->vm whenever a userspace GEM handle is closed.

On GPUs without per-process pagetables (a2xx..a5xx, or when private VM
creation falls back to the global GPU VM), every context shares
priv->gpu->vm. When a buffer is shared between processes (e.g. an
application and a compositor via dma-buf), closing the handle in one
process unmaps the buffer from the shared GPU VM while the other process
is still using it, causing continuous GPU IOMMU faults and display
corruption.

Skip tearing down ctx->vm in msm_gem_close() when ctx->vm ==
priv->gpu->vm, and instead tear down the global GPU VM mapping in
msm_gem_vma_put() once vma_ref drops to zero (the same mechanism already
used for the shared kms->vm). Otherwise the drm_gpuvm_bo keeps a
reference on the GEM object and every GPU-mapped buffer leaks.

Check has_vma() under the object's reservation lock first so GPUs with
per-process pagetables (where user objects are not mapped in
priv->gpu->vm) avoid waiting on fences or taking the global GPU VM lock.

Tested on MSM8916 / Adreno 306 (Samsung Galaxy A3, SM-A300F): eliminates
GPU IOMMU faults without leaking GEM objects.

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 | 45 ++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/msm/msm_gem.c b/drivers/gpu/drm/msm/msm_gem.c
index 6ee0e7d14..88a46ec3c 100644
--- a/drivers/gpu/drm/msm/msm_gem.c
+++ b/drivers/gpu/drm/msm/msm_gem.c
@@ -56,6 +56,7 @@ static void put_iova_spaces(struct drm_gem_object *obj, struct drm_gpuvm *vm,
 static void msm_gem_close(struct drm_gem_object *obj, struct drm_file *file)
 {
 	struct msm_context *ctx = file->driver_priv;
+	struct msm_drm_private *priv = obj->dev->dev_private;
 	struct drm_exec exec;
 
 	update_ctx_mem(file, -obj->size);
@@ -77,6 +78,17 @@ static void msm_gem_close(struct drm_gem_object *obj, struct drm_file *file)
 	if (msm_context_is_vmbind(ctx))
 		return;
 
+	/*
+	 * GPUs without per-process pagetables (a2xx..a5xx, or when private VM
+	 * creation falls back to the global GPU VM) hand every context
+	 * priv->gpu->vm.  Tearing down VMAs there on handle close unmaps
+	 * buffers that other processes still use (e.g. a dma-buf shared with
+	 * the compositor), which then fault on their stale iova.
+	 * msm_gem_vma_put() above tears them down after the last user.
+	 */
+	if (priv->gpu && ctx->vm == priv->gpu->vm)
+		return;
+
 	/*
 	 * TODO we might need to kick this to a queue to avoid blocking
 	 * in CLOSE ioctl
@@ -89,8 +101,22 @@ static void msm_gem_close(struct drm_gem_object *obj, struct drm_file *file)
 	drm_exec_fini(&exec);     /* drop locks */
 }
 
+static bool has_vma(struct drm_gem_object *obj, struct drm_gpuvm *vm)
+{
+	struct drm_gpuvm_bo *vm_bo;
+
+	guard(gem_lock)(obj);
+
+	drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
+		if (vm_bo->vm == vm)
+			return true;
+	}
+
+	return false;
+}
+
 /*
- * Get/put for kms->vm VMA
+ * Get/put for shared (kms->vm and global gpu->vm) VMAs
  */
 
 void msm_gem_vma_get(struct drm_gem_object *obj)
@@ -105,6 +131,23 @@ 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;
+
+		dma_resv_wait_timeout(obj->resv, DMA_RESV_USAGE_BOOKKEEP, false,
+				      MAX_SCHEDULE_TIMEOUT);
+		msm_gem_lock_vm_and_obj(&exec, obj, priv->gpu->vm);
+		put_iova_spaces(obj, priv->gpu->vm, true, "vma_put");
+		drm_exec_fini(&exec);     /* drop locks */
+	}
+
 	if (!priv->kms)
 		return;
 

base-commit: dca2f1b25860f8caa0406f858e76a3a21671a012
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-29  1:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29  0:09 [PATCH] drm/msm/gem: defer shared GPU VM teardown until last VMA user Vladimir Bely
2026-11-29  1:17 ` [PATCH v2] " Vladimir Bely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®