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

From: Rob Clark

Date: Fri Dec 04 2026 - 12:19:38 EST


On Sun, Nov 29, 2026 at 1:00 AM Vladimir Bely <vlwwwwww@xxxxxxxxx> wrote:
>
> 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@xxxxxxxxxxxxxxx
> Signed-off-by: Vladimir Bely <vlwwwwww@xxxxxxxxx>
> ---
> 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).

guard(gem_lock) should be in linux-next now.. but if you want
something easier to backport, maybe make this a two-patch series with
the 2nd patch converting to guard()

> - 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(-)
>
> diff --git a/drivers/gpu/drm/msm/msm_gem.c b/drivers/gpu/drm/msm/msm_gem.c
> index 6ee0e7d14..d224082cd 100644
> --- a/drivers/gpu/drm/msm/msm_gem.c
> +++ b/drivers/gpu/drm/msm/msm_gem.c
> @@ -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;
> + }
> + }

At the cost of an extra get/put, this could just use drm_gpuvm_bo_find()

BR,
-R

> + 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
> --
> 2.43.0
>