All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] drm/amdkfd: Get foreign dma-buf info
@ 2026-10-23 14:48 Xiaogang.Chen
  2026-10-23 14:48 ` [PATCH 2/2] drm/amdgpu: Redirect imported dma-buf mmap request to exporter Xiaogang.Chen
  2026-10-27  9:02 ` [PATCH 1/2] drm/amdkfd: Get foreign dma-buf info Christian König
  0 siblings, 2 replies; 5+ messages in thread
From: Xiaogang.Chen @ 2026-10-23 14:48 UTC (permalink / raw)
  To: amd-gfx; +Cc: Xiaogang Chen

From: Xiaogang Chen <xiaogang.chen@amd.com>

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.

Signed-off-by: Xiaogang Chen <xiaogang.chen@amd.com>
---
 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(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
index e3da382ceb3c..170f0ea0b78a 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
@@ -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);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
index a5d1d83ee5d3..b7fea3a2f5ca 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
@@ -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,
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 1d86ddfd6bde..9ca20331e815 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -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;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-10-27 16:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-23 14:48 [PATCH 1/2] drm/amdkfd: Get foreign dma-buf info Xiaogang.Chen
2026-10-23 14:48 ` [PATCH 2/2] drm/amdgpu: Redirect imported dma-buf mmap request to exporter Xiaogang.Chen
2026-10-27  9:00   ` Christian König
2026-10-27  9:02 ` [PATCH 1/2] drm/amdkfd: Get foreign dma-buf info Christian König
2026-10-27 16:38   ` Chen, Xiaogang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.