Skip to contentSkip to conversation

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

PATCH Superseded Vladimir Bely 3 participants 6 messages Nov 29

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

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

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")
---
 drivers/gpu/drm/msm/msm_gem.c | 45 ++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
drivers/gpu/drm/msm/msm_gem.c +44 -1
index 6ee0e7d14..88a46ec3c 100644
@@ -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

Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/msm: TOCTOU race on vma_ref across unlocked wait in msm_gem_vma_put() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/msm: TOCTOU race on vma_ref across unlocked wait in msm_gem_vma_put()
--

commit 465909c901566442dba60b64068077eeb1425e62
Author: Vladimir Bely <[email protected]>

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

This commit skips tearing down VMAs in the shared global GPU VM during msm_gem_close() and instead defers the teardown to msm_gem_vma_put() when vma_ref reaches zero.

Quoted 4 lines
drivers/gpu/drm/msm/msm_gem.c +0 -0
index 6ee0e7d146157..88a46ec3caad1 100644

[ ... ]

Quoted 13 lines
drivers/gpu/drm/msm/msm_gem.c +9 -0
@@ -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;

drivers/gpu/drm/msm/msm_gem.c void msm_gem_vma_put(struct drm_gem_object *obj) · line 152

Quoted 10 lines
drivers/gpu/drm/msm/msm_gem.c +8 -0
+
+		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;

[Severity: Medium]
Can msm_gem_vma_put() tear down active VMAs in priv->gpu->vm and priv->kms->vm if vma_ref is incremented again while dma_resv_wait_timeout() is sleeping unlocked?

For example, an existing drm_framebuffer holds a GEM object reference without holding vma_ref while unprepared, and a concurrent atomic commit can prepare it while msm_gem_vma_put() is waiting on fences:

CPU0 CPU1
msm_gem_close()
msm_gem_vma_put()

    atomic_dec_return(&vma_ref) -> 0
    dma_resv_wait_timeout(...)
                                      msm_framebuffer_prepare()
                                        msm_gem_vma_get()
                                          atomic_inc(&vma_ref) -> 1
                                        msm_gem_get_and_pin_iova(...)
    msm_gem_lock_vm_and_obj(...)
    put_iova_spaces(..., gpu->vm, ...)
    drm_exec_fini(&exec)
    msm_gem_lock_vm_and_obj(...)
    put_iova_spaces(..., kms->vm, ...)  <-- unmaps active VMA

Because vma_ref is not re-checked after locking the VM and object, put_iova_spaces() unmaps and closes VMAs that are back in use, causing IOMMU faults and causing the later msm_framebuffer_cleanup() -> msm_gem_unpin_iova() call to miss the closed VMA and leak pin_count.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

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

In reply to Vladimir Bely

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")

---
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(-)
drivers/gpu/drm/msm/msm_gem.c +59 -1
index 6ee0e7d14..191089304 100644
@@ -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
 }
Signature · 1 line
2.43.0
#3 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/msm: undefined gem_lock guard class in has_vma() breaks build - [Low] drm/msm: inaccurate commit…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/msm: undefined gem_lock guard class in has_vma() breaks build
- [Low] drm/msm: inaccurate commit subject in Fixes: trailer for 111fdd2198e6
--

commit 2d67e62e55be07ff8942dc714f5cd6ae5d9e884c
Author: Vladimir Bely <[email protected]>

Quoted 16 lines

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

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.

Quoted 8 lines

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")

[Severity: Low]
The quoted subject in the Fixes: tag does not match the commit in git history. Should this be:

Fixes 111fdd2198e6 ("drm/msm: drm_gpuvm conversion")

drivers/gpu/drm/msm/msm_gem.c static int msm_gem_open(struct drm_gem_object *obj, struct drm_file *file) · line 59

Quoted 13 lines
drivers/gpu/drm/msm/msm_gem.c +5 -0
index 130ae27ee00be..a150038265a88 100644
@@ -52,8 +52,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);

[Severity: High]
Does this compile? Neither drivers/gpu/drm/msm/msm_gem.h nor include/drm/drm_gem.h defines a gem_lock cleanup guard class, so guard(gem_lock)(obj) fails to build with undeclared class_gem_lock_t, class_gem_lock_constructor and class_gem_lock_destructor identifiers. Either DEFINE_GUARD(gem_lock, struct drm_gem_object *, msm_gem_lock(_T), msm_gem_unlock(_T)) needs to be added to msm_gem.h, or has_vma() should call msm_gem_lock() and msm_gem_unlock() directly.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw
In reply to Vladimir Bely
kernel test robot noticed the following build errors:

Hi Vladimir,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm-misc/drm-misc-next]
[also build test ERROR on linus/master v7.4-rc4 next-20261127] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Vladimir-Bely/drm-msm-gem-defer-shared-GPU-VM-teardown-until-last-VMA-user/20261129-041758
base:   https://gitlab.freedesktop.org/drm/misc/kernel.git drm-misc-next
patch link:    https://lore.kernel.org/r/20261129011759.1025516-1-vlwwwwww%40gmail.com
patch subject: [PATCH v2] drm/msm/gem: defer shared GPU VM teardown until last VMA user
config: arc-randconfig-1001-20261129 (https://download.01.org/0day-ci/archive/20261129/[email protected]/config)
compiler: arc-linux-gcc (GCC) 13.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261129/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from include/linux/preempt.h:11,
from include/linux/sched.h:15, from include/linux/ratelimit.h:6, from include/linux/dev_printk.h:16, from include/linux/device.h:15, from include/linux/dma-mapping.h:5, from include/linux/dma-map-ops.h:9, from drivers/gpu/drm/msm/msm_gem.c:7:
drivers/gpu/drm/msm/msm_gem.c: In function 'has_vma':

>> include/linux/cleanup.h:298:9: error: unknown type name 'class_gem_lock_t'; did you mean 'class_read_lock_t'?
     298 |         class_##_name##_t var __cleanup(class_##_name##_destructor) =   \
         |         ^~~~~~
   include/linux/cleanup.h:416:9: note: in expansion of macro 'CLASS'
     416 |         CLASS(_name, __UNIQUE_ID(guard))
         |         ^~~~~
   drivers/gpu/drm/msm/msm_gem.c:59:9: note: in expansion of macro 'guard'
      59 |         guard(gem_lock)(obj);
         |         ^~~~~
>> drivers/gpu/drm/msm/msm_gem.c:59:9: error: cleanup argument not a function
>> include/linux/cleanup.h:299:17: error: implicit declaration of function 'class_gem_lock_constructor'; did you mean 'class_read_lock_constructor'? [-Werror=implicit-function-declaration]
     299 |                 class_##_name##_constructor
         |                 ^~~~~~
   include/linux/cleanup.h:416:9: note: in expansion of macro 'CLASS'
     416 |         CLASS(_name, __UNIQUE_ID(guard))
         |         ^~~~~
   drivers/gpu/drm/msm/msm_gem.c:59:9: note: in expansion of macro 'guard'
      59 |         guard(gem_lock)(obj);
         |         ^~~~~
   include/linux/compiler.h:165:17: warning: unused variable '__UNIQUE_ID_guard_549' [-Wunused-variable]
     165 |         __PASTE(__UNIQUE_ID_,                                   \
         |                 ^~~~~~~~~~~~
   include/linux/cleanup.h:298:27: note: in definition of macro 'CLASS'
     298 |         class_##_name##_t var __cleanup(class_##_name##_destructor) =   \
         |                           ^~~
   include/linux/compiler_types.h:16:23: note: in expansion of macro '___PASTE'
      16 | #define __PASTE(a, b) ___PASTE(a, b)
         |                       ^~~~~~~~
   include/linux/compiler.h:165:9: note: in expansion of macro '__PASTE'
     165 |         __PASTE(__UNIQUE_ID_,                                   \
         |         ^~~~~~~
   include/linux/cleanup.h:416:22: note: in expansion of macro '__UNIQUE_ID'
     416 |         CLASS(_name, __UNIQUE_ID(guard))
         |                      ^~~~~~~~~~~
   drivers/gpu/drm/msm/msm_gem.c:59:9: note: in expansion of macro 'guard'
      59 |         guard(gem_lock)(obj);
         |         ^~~~~

cc1: some warnings being treated as errors

vim +59 drivers/gpu/drm/msm/msm_gem.c

    51	
    52	static void put_iova_spaces(struct drm_gem_object *obj, struct drm_gpuvm *vm,
    53				    bool close, const char *reason);
    54	
    55	static bool has_vma(struct drm_gem_object *obj, struct drm_gpuvm *vm)
    56	{
    57		struct drm_gpuvm_bo *vm_bo;
    58	
  > 59		guard(gem_lock)(obj);
    60	
    61		drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
    62			if (vm_bo->vm == vm)
    63				return true;
    64		}
    65	
    66		return false;
    67	}
    68	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw

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

In reply to Vladimir Bely

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")

---
Changes in v3:
- Use msm_gem_lock()/msm_gem_unlock() directly in has_vma() instead of guard(gem_lock) so the patch builds on v7.4/drm-misc-next/stable trees as well as msm-next (reported by kernel test robot and Sashiko AI).
- 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 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 | 63 ++++++++++++++++++++++++++++++++++-
 1 file changed, 62 insertions(+), 1 deletion(-)
drivers/gpu/drm/msm/msm_gem.c +62 -1
index 6ee0e7d14..d224082cd 100644
@@ -53,8 +53,26 @@ 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;
+	bool found = false;
+
+	msm_gem_lock(obj);
+	drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
+		if (vm_bo->vm == vm) {
+			found = true;
+			break;
+		}
+	}
+	msm_gem_unlock(obj);
+
+	return found;
+}
+
 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 +87,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 +132,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 +172,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
 }

base-commit: dca2f1b25860f8caa0406f858e76a3a21671a012

Signature · 1 line
2.43.0