]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
drm/amdgpu: validate userq va for GEM unmap
authorPrike Liang <Prike.Liang@amd.com>
Fri, 19 Sep 2025 07:14:41 +0000 (15:14 +0800)
committerAlex Deucher <alexander.deucher@amd.com>
Mon, 13 Oct 2025 18:14:34 +0000 (14:14 -0400)
When a user unmaps a userq VA, the driver must ensure
the queue has no in-flight jobs. If there is pending work,
the kernel should wait for the attached eviction (bookkeeping)
fence to signal before deleting the mapping.

Suggested-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Prike Liang <Prike.Liang@amd.com>
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c

index c1fee1ac2c0fcf0d652f13b317bf341a5a7f118c..9ad366f2977676b9a1ceab7b78884569883e68f5 100644 (file)
@@ -1195,3 +1195,34 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
        mutex_unlock(&adev->userq_mutex);
        return ret;
 }
+
+int amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev,
+                                      struct amdgpu_bo_va_mapping *mapping,
+                                      uint64_t saddr)
+{
+       u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
+       struct amdgpu_bo_va *bo_va = mapping->bo_va;
+       struct dma_resv *resv = bo_va->base.bo->tbo.base.resv;
+       int ret = 0;
+
+       if (!ip_mask)
+               return 0;
+
+       dev_warn_once(adev->dev, "now unmapping a vital queue va:%llx\n", saddr);
+       /**
+        * The userq VA mapping reservation should include the eviction fence,
+        * if the eviction fence can't signal successfully during unmapping,
+        * then driver will warn to flag this improper unmap of the userq VA.
+        * Note: The eviction fence may be attached to different BOs, and this
+        * unmap is only for one kind of userq VAs, so at this point suppose
+        * the eviction fence is always unsignaled.
+        */
+       if (!dma_resv_test_signaled(resv, DMA_RESV_USAGE_BOOKKEEP)) {
+               ret = dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP, true,
+                                           MAX_SCHEDULE_TIMEOUT);
+               if (ret <= 0)
+                       return -EBUSY;
+       }
+
+       return 0;
+}
index ef6ea01a805376a609112983cdae9ad7bcfdd831..036d8dd585cd106be50be5f1ddd2808d52166aca 100644 (file)
@@ -146,4 +146,7 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
                                                   u32 idx);
 int amdgpu_userq_input_va_validate(struct amdgpu_usermode_queue *queue,
                                   u64 addr, u64 expected_size);
+int amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev,
+                                      struct amdgpu_bo_va_mapping *mapping,
+                                      uint64_t saddr);
 #endif
index c1a801203949a3e5f0c4648be8ccc741de9b5602..3a660ce7591338da12a56a2f3510455cc30b4a62 100644 (file)
@@ -1952,6 +1952,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
        struct amdgpu_bo_va_mapping *mapping;
        struct amdgpu_vm *vm = bo_va->base.vm;
        bool valid = true;
+       int r;
 
        saddr /= AMDGPU_GPU_PAGE_SIZE;
 
@@ -1972,6 +1973,17 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
                        return -ENOENT;
        }
 
+       /* It's unlikely to happen that the mapping userq hasn't been idled
+        * during user requests GEM unmap IOCTL except for forcing the unmap
+        * from user space.
+        */
+       if (unlikely(atomic_read(&bo_va->userq_va_mapped) > 0)) {
+               r = amdgpu_userq_gem_va_unmap_validate(adev, mapping, saddr);
+               if (unlikely(r == -EBUSY))
+                       dev_warn_once(adev->dev,
+                                     "Attempt to unmap an active userq buffer\n");
+       }
+
        list_del(&mapping->list);
        amdgpu_vm_it_remove(mapping, &vm->va);
        mapping->bo_va = NULL;