drm/amdgpu: fix a race in kfd_mem_export_dmabuf()
authorAl Viro <viro@zeniv.linux.org.uk>
Tue, 4 Jun 2024 01:37:49 +0000 (21:37 -0400)
committerAlex Deucher <alexander.deucher@amd.com>
Tue, 10 Sep 2024 17:44:30 +0000 (13:44 -0400)
Using drm_gem_prime_handle_to_fd() to set dmabuf up and insert it into
descriptor table, only to have it looked up by file descriptor and
remove it from descriptor table is not just too convoluted - it's
racy; another thread might have modified the descriptor table while
we'd been going through that song and dance.

Switch kfd_mem_export_dmabuf() to using drm_gem_prime_handle_to_dmabuf()
and leave the descriptor table alone...

Reviewed-by: Felix Kuehling <felix.kuehling@amd.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

index 6d5fd371d5ce86b476c9597fdbba9ed48edaf940..4afef5b46c7d53199cddf3319bcc0995608f5aeb 100644 (file)
@@ -25,7 +25,6 @@
 #include <linux/pagemap.h>
 #include <linux/sched/mm.h>
 #include <linux/sched/task.h>
-#include <linux/fdtable.h>
 #include <drm/ttm/ttm_tt.h>
 
 #include <drm/drm_exec.h>
@@ -818,18 +817,13 @@ static int kfd_mem_export_dmabuf(struct kgd_mem *mem)
        if (!mem->dmabuf) {
                struct amdgpu_device *bo_adev;
                struct dma_buf *dmabuf;
-               int r, fd;
 
                bo_adev = amdgpu_ttm_adev(mem->bo->tbo.bdev);
-               r = drm_gem_prime_handle_to_fd(&bo_adev->ddev, bo_adev->kfd.client.file,
+               dmabuf = drm_gem_prime_handle_to_dmabuf(&bo_adev->ddev, bo_adev->kfd.client.file,
                                               mem->gem_handle,
                        mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ?
-                                              DRM_RDWR : 0, &fd);
-               if (r)
-                       return r;
-               dmabuf = dma_buf_get(fd);
-               close_fd(fd);
-               if (WARN_ON_ONCE(IS_ERR(dmabuf)))
+                                              DRM_RDWR : 0);
+               if (IS_ERR(dmabuf))
                        return PTR_ERR(dmabuf);
                mem->dmabuf = dmabuf;
        }