Commit 18192001 authored by Felix Kuehling's avatar Felix Kuehling Committed by Alex Deucher

drm/amdkfd: Export DMABufs from KFD using GEM handles

Create GEM handles for exporting DMABufs using GEM-Prime APIs. The GEM
handles are created in a drm_client_dev context to avoid exposing them
in user mode contexts through a DMABuf import.
Signed-off-by: default avatarFelix Kuehling <Felix.Kuehling@amd.com>
Reviewed-by: default avatarRamesh Errabolu <Ramesh.Errabolu@amd.com>
Signed-off-by: default avatarAlex Deucher <alexander.deucher@amd.com>
parent 4e95669e
...@@ -142,6 +142,7 @@ void amdgpu_amdkfd_device_init(struct amdgpu_device *adev) ...@@ -142,6 +142,7 @@ void amdgpu_amdkfd_device_init(struct amdgpu_device *adev)
{ {
int i; int i;
int last_valid_bit; int last_valid_bit;
int ret;
amdgpu_amdkfd_gpuvm_init_mem_limits(); amdgpu_amdkfd_gpuvm_init_mem_limits();
...@@ -160,6 +161,12 @@ void amdgpu_amdkfd_device_init(struct amdgpu_device *adev) ...@@ -160,6 +161,12 @@ void amdgpu_amdkfd_device_init(struct amdgpu_device *adev)
.enable_mes = adev->enable_mes, .enable_mes = adev->enable_mes,
}; };
ret = drm_client_init(&adev->ddev, &adev->kfd.client, "kfd", NULL);
if (ret) {
dev_err(adev->dev, "Failed to init DRM client: %d\n", ret);
return;
}
/* this is going to have a few of the MSBs set that we need to /* this is going to have a few of the MSBs set that we need to
* clear * clear
*/ */
...@@ -198,6 +205,10 @@ void amdgpu_amdkfd_device_init(struct amdgpu_device *adev) ...@@ -198,6 +205,10 @@ void amdgpu_amdkfd_device_init(struct amdgpu_device *adev)
adev->kfd.init_complete = kgd2kfd_device_init(adev->kfd.dev, adev->kfd.init_complete = kgd2kfd_device_init(adev->kfd.dev,
&gpu_resources); &gpu_resources);
if (adev->kfd.init_complete)
drm_client_register(&adev->kfd.client);
else
drm_client_release(&adev->kfd.client);
amdgpu_amdkfd_total_mem_size += adev->gmc.real_vram_size; amdgpu_amdkfd_total_mem_size += adev->gmc.real_vram_size;
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <linux/mmu_notifier.h> #include <linux/mmu_notifier.h>
#include <linux/memremap.h> #include <linux/memremap.h>
#include <kgd_kfd_interface.h> #include <kgd_kfd_interface.h>
#include <drm/drm_client.h>
#include "amdgpu_sync.h" #include "amdgpu_sync.h"
#include "amdgpu_vm.h" #include "amdgpu_vm.h"
#include "amdgpu_xcp.h" #include "amdgpu_xcp.h"
...@@ -83,6 +84,7 @@ struct kgd_mem { ...@@ -83,6 +84,7 @@ struct kgd_mem {
struct amdgpu_sync sync; struct amdgpu_sync sync;
uint32_t gem_handle;
bool aql_queue; bool aql_queue;
bool is_imported; bool is_imported;
}; };
...@@ -105,6 +107,9 @@ struct amdgpu_kfd_dev { ...@@ -105,6 +107,9 @@ struct amdgpu_kfd_dev {
/* HMM page migration MEMORY_DEVICE_PRIVATE mapping */ /* HMM page migration MEMORY_DEVICE_PRIVATE mapping */
struct dev_pagemap pgmap; struct dev_pagemap pgmap;
/* Client for KFD BO GEM handle allocations */
struct drm_client_dev client;
}; };
enum kgd_engine_type { enum kgd_engine_type {
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/sched/mm.h> #include <linux/sched/mm.h>
#include <linux/sched/task.h> #include <linux/sched/task.h>
#include <linux/fdtable.h>
#include <drm/ttm/ttm_tt.h> #include <drm/ttm/ttm_tt.h>
#include <drm/drm_exec.h> #include <drm/drm_exec.h>
...@@ -806,13 +807,22 @@ kfd_mem_dmaunmap_attachment(struct kgd_mem *mem, ...@@ -806,13 +807,22 @@ kfd_mem_dmaunmap_attachment(struct kgd_mem *mem,
static int kfd_mem_export_dmabuf(struct kgd_mem *mem) static int kfd_mem_export_dmabuf(struct kgd_mem *mem)
{ {
if (!mem->dmabuf) { if (!mem->dmabuf) {
struct dma_buf *ret = amdgpu_gem_prime_export( struct amdgpu_device *bo_adev;
&mem->bo->tbo.base, 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,
mem->gem_handle,
mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ? mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ?
DRM_RDWR : 0); DRM_RDWR : 0, &fd);
if (IS_ERR(ret)) if (r)
return PTR_ERR(ret); return r;
mem->dmabuf = ret; dmabuf = dma_buf_get(fd);
close_fd(fd);
if (WARN_ON_ONCE(IS_ERR(dmabuf)))
return PTR_ERR(dmabuf);
mem->dmabuf = dmabuf;
} }
return 0; return 0;
...@@ -1778,6 +1788,9 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( ...@@ -1778,6 +1788,9 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
pr_debug("Failed to allow vma node access. ret %d\n", ret); pr_debug("Failed to allow vma node access. ret %d\n", ret);
goto err_node_allow; goto err_node_allow;
} }
ret = drm_gem_handle_create(adev->kfd.client.file, gobj, &(*mem)->gem_handle);
if (ret)
goto err_gem_handle_create;
bo = gem_to_amdgpu_bo(gobj); bo = gem_to_amdgpu_bo(gobj);
if (bo_type == ttm_bo_type_sg) { if (bo_type == ttm_bo_type_sg) {
bo->tbo.sg = sg; bo->tbo.sg = sg;
...@@ -1829,6 +1842,8 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( ...@@ -1829,6 +1842,8 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
err_pin_bo: err_pin_bo:
err_validate_bo: err_validate_bo:
remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info); remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info);
drm_gem_handle_delete(adev->kfd.client.file, (*mem)->gem_handle);
err_gem_handle_create:
drm_vma_node_revoke(&gobj->vma_node, drm_priv); drm_vma_node_revoke(&gobj->vma_node, drm_priv);
err_node_allow: err_node_allow:
/* Don't unreserve system mem limit twice */ /* Don't unreserve system mem limit twice */
...@@ -1941,8 +1956,12 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( ...@@ -1941,8 +1956,12 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
/* Free the BO*/ /* Free the BO*/
drm_vma_node_revoke(&mem->bo->tbo.base.vma_node, drm_priv); drm_vma_node_revoke(&mem->bo->tbo.base.vma_node, drm_priv);
if (mem->dmabuf) if (!mem->is_imported)
drm_gem_handle_delete(adev->kfd.client.file, mem->gem_handle);
if (mem->dmabuf) {
dma_buf_put(mem->dmabuf); dma_buf_put(mem->dmabuf);
mem->dmabuf = NULL;
}
mutex_destroy(&mem->lock); mutex_destroy(&mem->lock);
/* If this releases the last reference, it will end up calling /* If this releases the last reference, it will end up calling
......
...@@ -1855,8 +1855,8 @@ static uint32_t get_process_num_bos(struct kfd_process *p) ...@@ -1855,8 +1855,8 @@ static uint32_t get_process_num_bos(struct kfd_process *p)
return num_of_bos; return num_of_bos;
} }
static int criu_get_prime_handle(struct kgd_mem *mem, int flags, static int criu_get_prime_handle(struct kgd_mem *mem,
u32 *shared_fd) int flags, u32 *shared_fd)
{ {
struct dma_buf *dmabuf; struct dma_buf *dmabuf;
int ret; int ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment