All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Chen, Xiaogang" <xiaogang.chen@amd.com>
To: "Christian König" <ckoenig.leichtzumerken@gmail.com>,
	amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH 1/2] drm/amdkfd: Get foreign dma-buf info
Date: Tue, 27 Oct 2026 11:38:37 -0500	[thread overview]
Message-ID: <5b602827-6633-4ee2-a19e-30455243ab7b@amd.com> (raw)
In-Reply-To: <b48ff49c-e645-423f-b8a3-7d4b156c2732@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 5629 bytes --]


On 10/27/2026 4:02 AM, Christian König wrote:
>
> On 10/23/26 16:48, Xiaogang.Chen wrote:
>> 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.
> 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.
>
> 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

>
> Regards,
> Christian.
>
>> 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;

[-- Attachment #2: Type: text/html, Size: 6844 bytes --]

      reply	other threads:[~2026-10-27 16:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5b602827-6633-4ee2-a19e-30455243ab7b@amd.com \
    --to=xiaogang.chen@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=ckoenig.leichtzumerken@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.