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