mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vladimir Bely <vlwwwwww@gmail.com>
To: Rob Clark <robin.clark@oss.qualcomm.com>,
	Dmitry Baryshkov <lumag@kernel.org>
Cc: Abhinav Kumar <abhinav.kumar@linux.dev>,
	Jessica Zhang <jesszhan0024@gmail.com>,
	Sean Paul <sean@poorly.run>,
	Marijn Suijten <marijn.suijten@somainline.org>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
	linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org,
	freedreno@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	Vladimir Bely <vlwwwwww@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH v2] drm/msm/gem: defer shared GPU VM teardown until last VMA user
Date: Sun, 29 Nov 2026 04:17:58 +0300	[thread overview]
Message-ID: <20261129011759.1025516-1-vlwwwwww@gmail.com> (raw)
In-Reply-To: <20261129000944.986285-1-vlwwwwww@gmail.com>

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: Add drm_gpuvm support")
Cc: stable@vger.kernel.org
Signed-off-by: Vladimir Bely <vlwwwwww@gmail.com>
---
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
  to close the TOCTOU race with concurrent msm_gem_vma_get() (reported by
  Sashiko AI review).
- Re-check dma_resv_test_signaled() under the lock and loop if a new fence
  was added during an ABA vma_get/submit/vma_put cycle.

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

diff --git a/drivers/gpu/drm/msm/msm_gem.c b/drivers/gpu/drm/msm/msm_gem.c
index 6ee0e7d14..191089304 100644
--- a/drivers/gpu/drm/msm/msm_gem.c
+++ b/drivers/gpu/drm/msm/msm_gem.c
@@ -53,8 +53,23 @@ 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;
+
+	guard(gem_lock)(obj);
+
+	drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
+		if (vm_bo->vm == vm)
+			return true;
+	}
+
+	return false;
+}
+
 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 +84,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 +129,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 +169,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


      reply	other threads:[~2026-11-29  1:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  0:09 [PATCH] " Vladimir Bely
2026-11-29  1:17 ` Vladimir Bely [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129011759.1025516-1-vlwwwwww@gmail.com \
    --to=vlwwwwww@gmail.com \
    --cc=abhinav.kumar@linux.dev \
    --cc=airlied@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=freedreno@lists.freedesktop.org \
    --cc=jesszhan0024@gmail.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lumag@kernel.org \
    --cc=marijn.suijten@somainline.org \
    --cc=robin.clark@oss.qualcomm.com \
    --cc=sean@poorly.run \
    --cc=simona@ffwll.ch \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®