drm/amdkfd: Get foreign dma-buf info
Subsystems (3)
PATCH 1/2 drm/amdkfd: Get foreign dma-buf info
From: Xiaogang Chen <[email protected]>
When import a dma-buf into kfd driver the dma-buf can be exported from amdgpu driver or another driver/component(foreign).
Current kfd driver can't handle foreign dma-buf at AMDKFD_IOC_GET_DMABUF_INFO. When a foreign dma-buf is imported into kfd driver(ex udmabuf) it becomes a normal kfd buf that can be exported, then imported in same or another amdgpu device. That requires kfd drive to get new exported dma-buf info before it is imported.
Extend kfd_ioctl_get_dmabuf_info to handle args->dmabuf_fd that was originally exported by foreign component.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 3 ++ .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 47 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 11 ++++- 3 files changed, 60 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +3 -0
@@ -347,6 +347,9 @@ int amdgpu_amdkfd_gpuvm_import_dmabuf_fd(struct amdgpu_device *adev, int fd, uint64_t va, void *drm_priv, struct kgd_mem **mem, uint64_t *size, uint64_t *mmap_offset); +int amdgpu_amdkfd_gpuvm_imported_dmabuf_info (void *kgd_info,int fd, +uint64_t *size,uint32_t *flags, +uint32_t *gpu_id);int amdgpu_amdkfd_gpuvm_export_dmabuf (struct kgd_mem *mem,struct dma_buf **dmabuf);void amdgpu_amdkfd_debug_mem_fence (struct amdgpu_device *adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +47 -0
53 lines not on this page · raw message
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +10 -1
@@ -1595,8 +1595,17 @@ static int kfd_ioctl_get_dmabuf_info(struct file *filep, &dmabuf_adev, &args->size, &metadata_buffer, args->metadata_size, &args->metadata_size, &flags, &xcp_id); -if (r) +if (r) { +/* Foreign dma-buf: report it when this process imported it. */ + r =amdgpu_amdkfd_gpuvm_imported_dmabuf_info (p->kgd_process_info, + args->dmabuf_fd, + &args->size, + &args->flags, + &args->gpu_id); +if (!r) + args->metadata_size =0 ; goto exit; + }if (xcp_id >=0 ) args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;
Signature · 1 line
2.34.1
PATCH 2/2 drm/amdgpu: Redirect imported dma-buf mmap request to exporter
From: Xiaogang Chen <[email protected]>
The imported GEM object with ttm->page_flags TTM_TT_FLAG_EXTERNAL set and without TTM_TT_FLAG_EXTERNAL_MAPPABLE set,is only a GPU-side handle for memory that belongs to the exporter. TTM refuses to build a CPU mapping from it because those page-table entries would point to pages drm driver does not own.
amdgpu_dma_buf_create_obj creates GEM object for foreign exported buf(not generated by amdgpu driver) belonging to this catagory. The pages, when GPU needs them, come from dma_buf_map_attachment on the exporter's scatter/gather table. The exporter can move or revoke that mapping through move_notify. A CPU page-table entry installed by the importer would keep the old PFN, and the importer also does not know the exporter's caching mode or whether the memory is CPU-visible.
This patch redirects foreign imported dma-buf mmap request to exporter. The CPU mapping is created by dma_buf_mmap, which points the VMA to the exporter's file and calls dma_buf->ops->mmap. The exporter's fault handler will fill page table.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +20 -0
@@ -369,6 +369,26 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,static int amdgpu_gem_object_mmap (struct drm_gem_object *obj,struct vm_area_struct *vma) {struct amdgpu_bo *bo =gem_to_amdgpu_bo (obj); +struct dma_buf *dmabuf; +int ret; + +/* +* A foreign import (udmabuf, for example) does not own the pages. +* Point the VMA at the exporter so the later CPU fault uses its +* vm_ops. dma_buf_mmap() resets vm_pgoff and holds the dma-buf file. +* An amdgpu dma-buf stays on the TTM path below. +*/ + dmabuf = obj->import_attach ? obj->import_attach->dmabuf :NULL ; +if (dmabuf && dmabuf->ops != &amdgpu_dmabuf_ops) { + ret =dma_buf_mmap (dmabuf, vma,0 ); +if (ret) +return ret; + +/* Exporter vm_ops will not drop the mapping reference. */ +drm_gem_object_put (obj); +return 0 ; + } +if (amdgpu_ttm_tt_get_usermm (bo->tbo.ttm))return -EPERM ;
Signature · 1 line
2.34.1
Re: [PATCH 2/2] drm/amdgpu: Redirect imported dma-buf mmap request to exporter
Quoted 15 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
The imported GEM object with
ttm->page_flagsTTM_TT_FLAG_EXTERNALset and withoutTTM_TT_FLAG_EXTERNAL_MAPPABLEset,is only a GPU-side handle for memory that belongs to the exporter. TTM refuses to build a CPU mapping from it because those page-table entries would point to pages drm driver does not own.
amdgpu_dma_buf_create_objcreates GEM object for foreign exported buf(not generated by amdgpu driver) belonging to this catagory. The pages, when GPU needs them, come fromdma_buf_map_attachmenton the exporter's scatter/gather table. The exporter can move or revoke that mapping throughmove_notify. A CPU page-table entry installed by the importer would keep the old PFN, and the importer also does not know the exporter's caching mode or whether the memory is CPU-visible.
Quoted 4 lines
This patch redirects foreign imported dma-buf mmap request to exporter. The CPU mapping is created by
dma_buf_mmap, which points the VMA to the exporter's file and callsdma_buf->ops->mmap. The exporter's fault handler will fill page table.
We had that suggestion many times before and it is only allowed as a backward compatibility hack for a handful of DRM drivers to do this.
If the imported DMA-buf needs to be mmaped() that *must* be done either on the exporter or the DMA-buf file descriptor itself.
So absolutely clear NAK to that!
Regards,
Christian.
Quoted 37 lines
Signed-off-by Xiaogang Chen <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +20
-0@@ -369,6 +369,26 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,static int amdgpu_gem_object_mmap (struct drm_gem_object *obj,struct vm_area_struct *vma) {struct amdgpu_bo *bo =gem_to_amdgpu_bo (obj); +struct dma_buf *dmabuf; +int ret; + +/* +* A foreign import (udmabuf, for example) does not own the pages. +* Point the VMA at the exporter so the later CPU fault uses its +* vm_ops. dma_buf_mmap() resets vm_pgoff and holds the dma-buf file. +* An amdgpu dma-buf stays on the TTM path below. +*/ + dmabuf = obj->import_attach ? obj->import_attach->dmabuf :NULL ; +if (dmabuf && dmabuf->ops != &amdgpu_dmabuf_ops) { + ret =dma_buf_mmap (dmabuf, vma,0 ); +if (ret) +return ret; + +/* Exporter vm_ops will not drop the mapping reference. */ +drm_gem_object_put (obj); +return 0 ; + } +if (amdgpu_ttm_tt_get_usermm (bo->tbo.ttm))return -EPERM ;
Quoted 10 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
When import a dma-buf into kfd driver the dma-buf can be exported from amdgpu driver or another driver/component(foreign).
Current kfd driver can't handle foreign dma-buf at
AMDKFD_IOC_GET_DMABUF_INFO. When a foreign dma-buf is imported into kfd driver(ex udmabuf) it becomes a normal kfd buf that can be exported, then imported in same or another amdgpu device. That requires kfd drive to get new exported dma-buf info before it is imported.
Yeah again absolutely clear NAK to that!
Re-exporting the imported DMA-buf this way is *clearly* not allowed!
What you can do is to re-export the original DMA-buf so that you get the same fd you would get from the original exporter.
In other words you can then also ask the original exporter for that information.
Regards,
Christian.
Quoted 106 lines
Extend
kfd_ioctl_get_dmabuf_infoto handleargs->dmabuf_fdthat was originally exported by foreign component.Signed-off-by Xiaogang Chen <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 3 ++ .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 47 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 11 ++++- 3 files changed, 60 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +3
-0@@ -347,6 +347,9 @@ int amdgpu_amdkfd_gpuvm_import_dmabuf_fd(struct amdgpu_device *adev, int fd, uint64_t va, void *drm_priv, struct kgd_mem **mem, uint64_t *size, uint64_t *mmap_offset); +int amdgpu_amdkfd_gpuvm_imported_dmabuf_info (void *kgd_info,int fd, +uint64_t *size,uint32_t *flags, +uint32_t *gpu_id);int amdgpu_amdkfd_gpuvm_export_dmabuf (struct kgd_mem *mem,struct dma_buf **dmabuf);void amdgpu_amdkfd_debug_mem_fence (struct amdgpu_device *adev);drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +47
-0@@ -2452,6 +2452,53 @@ static int import_obj_create(struct amdgpu_device *adev,return ret; } +int amdgpu_amdkfd_gpuvm_imported_dmabuf_info (void *kgd_info,int fd, +uint64_t *size,uint32_t *flags, +uint32_t *gpu_id) +{ +struct amdkfd_process_info *info = kgd_info; +struct dma_buf *dma_buf; +struct kgd_mem *mem; +int ret = -EINVAL ; + +if (!info) +return -EINVAL ; + + dma_buf =dma_buf_get (fd); +if (IS_ERR (dma_buf)) +return PTR_ERR (dma_buf); + +mutex_lock (&info->lock); +list_for_each_entry (mem, &info->kfd_bo_list, validate_list) { +struct amdgpu_device *bo_adev; +struct kfd_dev *kfd; +int8_t xcp_id; + +if (!mem->is_imported || mem->dmabuf != dma_buf) +continue ; + +if (size) + *size =amdgpu_bo_size (mem->bo); +if (flags) + *flags = mem->alloc_flags; +if (gpu_id) { + bo_adev =amdgpu_ttm_adev (mem->bo->tbo.bdev); + kfd = bo_adev->kfd.dev; + xcp_id = mem->bo->xcp_id; +if (kfd && xcp_id >=0 && xcp_id < kfd->num_nodes && + kfd->nodes[xcp_id]) + *gpu_id = kfd->nodes[xcp_id]->id; +else if (kfd && kfd->num_nodes && kfd->nodes[0 ]) + *gpu_id = kfd->nodes[0 ]->id; + } + ret =0 ; +break ; + } +mutex_unlock (&info->lock); +dma_buf_put (dma_buf); +return ret; +} + int amdgpu_amdkfd_gpuvm_import_dmabuf_fd(struct amdgpu_device *adev, int fd, uint64_t va, void *drm_priv, struct kgd_mem **mem, uint64_t *size,drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +10
-1@@ -1595,8 +1595,17 @@ static int kfd_ioctl_get_dmabuf_info(struct file *filep, &dmabuf_adev, &args->size, &metadata_buffer, args->metadata_size, &args->metadata_size, &flags, &xcp_id); -if (r) +if (r) { +/* Foreign dma-buf: report it when this process imported it. */ + r =amdgpu_amdkfd_gpuvm_imported_dmabuf_info (p->kgd_process_info, + args->dmabuf_fd, + &args->size, + &args->flags, + &args->gpu_id); +if (!r) + args->metadata_size =0 ; goto exit; + }if (xcp_id >=0 ) args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;
Quoted 7 lines · Christian König
Quoted 5 lines · Xiaogang.Chen
From: Xiaogang Chen<[email protected]>
When import a dma-buf into kfd driver the dma-buf can be exported from amdgpu driver or another driver/component(foreign).
Quoted 8 lines
Quoted 5 lines
Current kfd driver can't handle foreign dma-buf at
AMDKFD_IOC_GET_DMABUF_INFO. When a foreign dma-buf is imported into kfd driver(ex udmabuf) it becomes a normal kfd buf that can be exported, then imported in same or another amdgpu device. That requires kfd drive to get new exported dma-buf info before it is imported.Yeah again absolutely clear NAK to that!
Re-exporting the imported DMA-buf this way is *clearly* not allowed!
This "Re-exporting" is from driver point view. App exports only once. The original buf was generated by Thunk using foreign driver/component exported buf when app allocates buf. Driver sees "exporting the imported DMA-buf", app does not. App uses same routine to handle it as for normal kfd buf.
Quoted 4 lines
What you can do is to re-export the original DMA-buf so that you get the same fd you would get from the original exporter.
In other words you can then also ask the original exporter for that information.
During the procedure there is only one dma-buf instance which was generated by foreign driver/component. kfd does not generate new dma-buf instance. It saves this dma-buf instance. kfd generates a "struct kgd_mem" to wrap this dma-buf instance and record its metadata. Then dma-buf info is obtained from this "struct kgd_mem".
Regards
Xiaogang
Quoted 109 lines
Regards,
Christian.Quoted 105 lines
Extend
kfd_ioctl_get_dmabuf_infoto handleargs->dmabuf_fdthat was originally exported by foreign component.Signed-off-by Xiaogang Chen<[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 3 ++ .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 47 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 11 ++++- 3 files changed, 60 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +3
-0@@ -347,6 +347,9 @@ int amdgpu_amdkfd_gpuvm_import_dmabuf_fd(struct amdgpu_device *adev, int fd, uint64_t va, void *drm_priv, struct kgd_mem **mem, uint64_t *size, uint64_t *mmap_offset); +int amdgpu_amdkfd_gpuvm_imported_dmabuf_info (void *kgd_info,int fd, +uint64_t *size,uint32_t *flags, +uint32_t *gpu_id);int amdgpu_amdkfd_gpuvm_export_dmabuf (struct kgd_mem *mem,struct dma_buf **dmabuf);void amdgpu_amdkfd_debug_mem_fence (struct amdgpu_device *adev);drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +47
-0@@ -2452,6 +2452,53 @@ static int import_obj_create(struct amdgpu_device *adev,return ret; } +int amdgpu_amdkfd_gpuvm_imported_dmabuf_info (void *kgd_info,int fd, +uint64_t *size,uint32_t *flags, +uint32_t *gpu_id) +{ +struct amdkfd_process_info *info = kgd_info; +struct dma_buf *dma_buf; +struct kgd_mem *mem; +int ret = -EINVAL ; + +if (!info) +return -EINVAL ; + + dma_buf =dma_buf_get (fd); +if (IS_ERR (dma_buf)) +return PTR_ERR (dma_buf); + +mutex_lock (&info->lock); +list_for_each_entry (mem, &info->kfd_bo_list, validate_list) { +struct amdgpu_device *bo_adev; +struct kfd_dev *kfd; +int8_t xcp_id; + +if (!mem->is_imported || mem->dmabuf != dma_buf) +continue ; + +if (size) + *size =amdgpu_bo_size (mem->bo); +if (flags) + *flags = mem->alloc_flags; +if (gpu_id) { + bo_adev =amdgpu_ttm_adev (mem->bo->tbo.bdev); + kfd = bo_adev->kfd.dev; + xcp_id = mem->bo->xcp_id; +if (kfd && xcp_id >=0 && xcp_id < kfd->num_nodes && + kfd->nodes[xcp_id]) + *gpu_id = kfd->nodes[xcp_id]->id; +else if (kfd && kfd->num_nodes && kfd->nodes[0 ]) + *gpu_id = kfd->nodes[0 ]->id; + } + ret =0 ; +break ; + } +mutex_unlock (&info->lock); +dma_buf_put (dma_buf); +return ret; +} + int amdgpu_amdkfd_gpuvm_import_dmabuf_fd(struct amdgpu_device *adev, int fd, uint64_t va, void *drm_priv, struct kgd_mem **mem, uint64_t *size,drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +10
-1@@ -1595,8 +1595,17 @@ static int kfd_ioctl_get_dmabuf_info(struct file *filep, &dmabuf_adev, &args->size, &metadata_buffer, args->metadata_size, &args->metadata_size, &flags, &xcp_id); -if (r) +if (r) { +/* Foreign dma-buf: report it when this process imported it. */ + r =amdgpu_amdkfd_gpuvm_imported_dmabuf_info (p->kgd_process_info, + args->dmabuf_fd, + &args->size, + &args->flags, + &args->gpu_id); +if (!r) + args->metadata_size =0 ; goto exit; + }if (xcp_id >=0 ) args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;