All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/2] drm/msm/gem: defer shared GPU VM teardown until last VMA user
@ 2026-12-04 23:10 Vladimir Bely
  2026-12-04 23:10 ` [PATCH v4 1/2] " Vladimir Bely
  2026-12-04 23:10 ` [PATCH v4 2/2] drm/msm/gem: use guard(gem_lock) in has_vma() Vladimir Bely
  0 siblings, 2 replies; 4+ 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), all contexts share
priv->gpu->vm, so tearing down the GPU VMA in msm_gem_close() breaks
other users of a shared/imported buffer.

Patch 1 defers the shared priv->gpu->vm teardown to msm_gem_vma_put()
using explicit msm_gem_lock()/msm_gem_unlock() so it backports cleanly
to stable kernels.  Patch 2 converts has_vma() to guard(gem_lock) on
msm-next.

Changes in v4 (addressing Rob Clark's review):
- Patch 1: use drm_gpuvm_bo_find()/drm_gpuvm_bo_put() in has_vma()
  instead of open-coding the drm_gem_for_each_gpuvm_bo() loop.
- Patch 2: add follow-up patch converting has_vma() to guard(gem_lock)
  so Patch 1 stays easy to backport to stable trees.

Changes in v3:
- Use msm_gem_lock()/msm_gem_unlock() in has_vma() instead of
  guard(gem_lock) so the fix builds on v7.4/stable trees as well.
- Fix the commit subject in the Fixes: trailer.

Changes in v2:
- Re-check vma_ref after the unlocked dma_resv_wait_timeout() and again
  under msm_gem_lock_vm_and_obj() for both priv->gpu->vm and
  priv->kms->vm.
- Re-check dma_resv_test_signaled() under the lock and loop on an ABA
  vma_get/submit/vma_put cycle.

Link: https://lore.kernel.org/linux-arm-msm/20261129090012.183245-1-vlwwwwww@gmail.com/ [v3]

Vladimir Bely (2):
  drm/msm/gem: defer shared GPU VM teardown until last VMA user
  drm/msm/gem: use guard(gem_lock) in has_vma()

 drivers/gpu/drm/msm/msm_gem.c | 58 ++++++++++++++++++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)


base-commit: dca2f1b25860f8caa0406f858e76a3a21671a012
-- 
2.43.0


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

* [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:41   ` sashiko-bot
  2026-12-04 23:10 ` [PATCH v4 2/2] drm/msm/gem: use guard(gem_lock) in has_vma() Vladimir Bely
  1 sibling, 1 reply; 4+ 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 related	[flat|nested] 4+ 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; 4+ 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 related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 1/2] drm/msm/gem: defer shared GPU VM teardown until last VMA user
  2026-12-04 23:10 ` [PATCH v4 1/2] " Vladimir Bely
@ 2026-12-04 23:41   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-04 23:41 UTC (permalink / raw)
  To: Vladimir Bely; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] drm/msm: unbounded uninterruptible fence wait in
  msm_gem_vma_put()
- [Medium] drm/msm: has_vma() locks obj->resv on per-process pagetable
  GPUs
- [Medium] drm/msm: uninterruptible fence wait under modeset lock in
  msm_gem_vma_put()
- [Medium] drm/msm: unchecked msm_gem_lock_vm_and_obj() return in
  msm_gem_vma_put()
- [Medium] drm/msm/gem: blocking GPU fence wait and VM unmap in
  msm_gem_vma_put()
- [Low] drm/msm: stale vma_ref comments only mention kms->vm
--

commit 04b8493963f48ed195419d586c3d301c918f3f68
Author: Vladimir Bely <vlwwwwww@gmail.com>

drm/msm/gem: defer shared GPU VM teardown until last VMA user

This commit skips per-handle VMA teardown in msm_gem_close() when ctx->vm
is the shared priv->gpu->vm, and instead tears down that mapping in
msm_gem_vma_put() once vma_ref drops to zero.

> diff --git a/drivers/gpu/drm/msm/msm_gem.c b/drivers/gpu/drm/msm/msm_gem.c
> index 6ee0e7d146157..aac5926fbc904 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;
> +}

[Severity: Medium]
Does has_vma() still acquire obj->resv on GPUs with per-process pagetables?

msm_gpu_init() unconditionally creates priv->gpu->vm for kernel buffers on
a6xx/a7xx/a8xx as well, so priv->gpu && priv->gpu->vm is always true in
msm_gem_vma_put().  Every final vma_ref drop therefore takes
dma_resv_lock(obj->resv) and walks obj->gpuva.list.

For MSM_BO_NO_SHARE buffers, obj->resv aliases the process-wide ctx->vm
reservation lock used on every submit and VM_BIND ioctl, adding lock
contention on handle closes that previously took no locks.

[ ... ]

> @@ -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.
> +	 */

[Severity: Low]
The comment above msm_gem_vma_get() in msm_gem.c ("Get/put for kms->vm
VMA") and the kernel-doc for msm_gem_object::vma_ref in msm_gem.h still
only mention kms->vm, even though vma_ref now also governs priv->gpu->vm
teardown.

> +	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);

[Severity: Medium]
Can this uninterruptible fence wait now sleep while drm_modeset_lock is
held?

If a synchronous atomic commit is interrupted by a signal during
drm_atomic_helper_wait_for_fences(dev, state, true), the error path runs
drm_atomic_helper_unprepare_planes() -> msm_framebuffer_cleanup() ->
msm_gem_vma_put() while still holding drm_modeset_lock.

When the GEM handle was already closed, vma_ref drops to zero and this
re-enters an uninterruptible wait on the same unsignaled fence, turning a
signal-aborted commit into an unkillable wait that blocks other modesets
and VT switches.

> +			if (atomic_read(&to_msm_bo(obj)->vma_ref))
> +				return;
> +
> +			msm_gem_lock_vm_and_obj(&exec, obj, priv->gpu->vm);

[Severity: Medium]
What happens if msm_gem_lock_vm_and_obj() fails with -ENOMEM here?

drm_exec_lock_obj() can return -ENOMEM without holding either lock when
allocating exec->objects fails (for instance, during exit_files() of an
OOM-killed process whose ALLOC_OOM reserves are exhausted).

Ignoring the return value lets put_iova_spaces() unlink and free VMAs from
the shared priv->gpu->vm without holding obj->resv or the VM lock, risking
rb-tree and list corruption against concurrent contexts.

> +			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;
> +			}

[Severity: Medium]
Is it safe to block on GPU fences, the global priv->gpu->vm lock, and
synchronous IOMMU unmap inside msm_gem_vma_put()?

msm_atomic_commit_tail() -> msm_framebuffer_cleanup() runs before
drm_atomic_helper_commit_cleanup_done(), so an active or hung GPU job on an
old scanout BO whose handle was already closed stalls subsequent commits in
stall_checks().

Likewise, delayed_fput() -> msm_gem_dmabuf_release() drains deferred file
closes serially on a single global work item, so blocking here stalls
unrelated deferred file releases system-wide.  Deferring shared GPU VMA
teardown to a worker or fence callback would avoid blocking both paths.

> +			drm_exec_fini(&exec);
> +		}
> +	}

[Severity: High]
Can this unbounded retry loop livelock on a foreign imported dma-buf?

msm_gem_import() sets obj->resv = dmabuf->resv, while each import gets an
independent vma_ref.  Once vma_ref reaches zero, nothing can raise it
again, yet another dma-buf holder can keep attaching unsignaled fences to
dmabuf->resv via DMA_BUF_IOCTL_IMPORT_SYNC_FILE or another GPU context.

When a new fence lands while dma_resv_wait_timeout() sleeps on an earlier
one, dma_resv_test_signaled() sees the new fence under the lock and loops
indefinitely in uninterruptible sleep, hanging msm_gem_close() in D state
or wedging atomic commit_work.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204231058.699120-1-vlwwwwww@gmail.com?part=1

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

end of thread, other threads:[~2026-12-04 23:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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:41   ` sashiko-bot
2026-12-04 23:10 ` [PATCH v4 2/2] drm/msm/gem: use guard(gem_lock) in has_vma() Vladimir Bely

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.