From: "Zhang, Jerry (Junwei)" <Jerry.Zhang-5C7GfCeVMHo@public.gmane.org>
To: christian.koenig-5C7GfCeVMHo@public.gmane.org,
zhoucm1 <zhoucm1-5C7GfCeVMHo@public.gmane.org>,
amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org
Subject: Re: [PATCH libdrm 2/4] amdgpu: add a function to find bo by cpu mapping (v2)
Date: Wed, 8 Aug 2018 17:45:38 +0800 [thread overview]
Message-ID: <5B6ABBC2.7070601@amd.com> (raw)
In-Reply-To: <7858ed2f-e504-f477-09a7-cd0b5ce90c58-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
On 08/08/2018 04:51 PM, Christian König wrote:
> Am 08.08.2018 um 10:43 schrieb zhoucm1:
>>
>>
>> On 2018年08月08日 14:48, Christian König wrote:
>>> Am 08.08.2018 um 06:23 schrieb zhoucm1:
>>>>
>>>>
>>>> On 2018年08月08日 12:08, Junwei Zhang wrote:
>>>>> Userspace needs to know if the user memory is from BO or malloc.
>>>>>
>>>>> v2: update mutex range and rebase
>>>>>
>>>>> Signed-off-by: Junwei Zhang <Jerry.Zhang@amd.com>
>>>>> ---
>>>>> amdgpu/amdgpu.h | 23 +++++++++++++++++++++++
>>>>> amdgpu/amdgpu_bo.c | 34 ++++++++++++++++++++++++++++++++++
>>>>> 2 files changed, 57 insertions(+)
>>>>>
>>>>> diff --git a/amdgpu/amdgpu.h b/amdgpu/amdgpu.h
>>>>> index be83b45..a8c353c 100644
>>>>> --- a/amdgpu/amdgpu.h
>>>>> +++ b/amdgpu/amdgpu.h
>>>>> @@ -678,6 +678,29 @@ int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
>>>>> amdgpu_bo_handle *buf_handle);
>>>>> /**
>>>>> + * Validate if the user memory comes from BO
>>>>> + *
>>>>> + * \param dev - [in] Device handle. See #amdgpu_device_initialize()
>>>>> + * \param cpu - [in] CPU address of user allocated memory which we
>>>>> + * want to map to GPU address space (make GPU accessible)
>>>>> + * (This address must be correctly aligned).
>>>>> + * \param size - [in] Size of allocation (must be correctly aligned)
>>>>> + * \param buf_handle - [out] Buffer handle for the userptr memory
>>>>> + * if the user memory is not from BO, the buf_handle will be NULL.
>>>>> + * \param offset_in_bo - [out] offset in this BO for this user memory
>>>>> + *
>>>>> + *
>>>>> + * \return 0 on success\n
>>>>> + * <0 - Negative POSIX Error code
>>>>> + *
>>>>> +*/
>>>>> +int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
>>>>> + void *cpu,
>>>>> + uint64_t size,
>>>>> + amdgpu_bo_handle *buf_handle,
>>>>> + uint64_t *offset_in_bo);
>>>>> +
>>>>> +/**
>>>>> * Free previosuly allocated memory
>>>>> *
>>>>> * \param dev - \c [in] Device handle. See #amdgpu_device_initialize()
>>>>> diff --git a/amdgpu/amdgpu_bo.c b/amdgpu/amdgpu_bo.c
>>>>> index b24e698..a7f0662 100644
>>>>> --- a/amdgpu/amdgpu_bo.c
>>>>> +++ b/amdgpu/amdgpu_bo.c
>>>>> @@ -529,6 +529,40 @@ int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo,
>>>>> }
>>>>> }
>>>>> +int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
>>>>> + void *cpu,
>>>>> + uint64_t size,
>>>>> + amdgpu_bo_handle *buf_handle,
>>>>> + uint64_t *offset_in_bo)
>>>>> +{
>>>>> + int i;
>>>>> + struct amdgpu_bo *bo;
>>>>> +
>>>>> + if (cpu == NULL || size == 0)
>>>>> + return -EINVAL;
>>>>> +
>>>>> + pthread_mutex_lock(&dev->bo_table_mutex);
>>>>> + for (i = 0; i < dev->bo_handles.max_key; i++) {
>>>> Hi Jerry,
>>>>
>>>> As Christian catched before, iterating all BOs of device will introduce much CPU overhead, this isn't good direction.
>>>> Since cpu virtual address is per-process, you should go to kernel to find them from vm tree, which obviously takes less time.
>>>
>>> Yeah, but is also much more overhead to maintain.
>>>
>>> Since this is only to fix the behavior of a single buggy application at least I'm fine to keep the workaround as simple as this.
>> I like 'workaround' expression, if Jerry adds 'workaround' comments here, I'm ok as well.
>
> Yeah, agree a code comment is probably a good idea. Something like
>
> /* Workaround for a buggy application which tries to import previously exposed CPU pointers.
> * If we find a real world use case we should improve that by asking the kernel for the right handle.
> */
>
> With that added the patch is Reviewed-by: Christian König <christian.koenig@amd.com>.
Thanks you all, will add that comments.
BTW, Christian, SRDC still has no permission to get the new drm repo,
since git port is blocked by local network manager.
I could prepare those patches in gfx mail list, anyone will help to push them?
Regards,
Jerry
>
> Regards,
> Christian.
>
>>
>> Regards,
>> David Zhou
>>>
>>> If we find a wider use we can still start to use the kernel implementation again.
>>>
>>> Regards,
>>> Christian.
>>>
>>>>
>>>> Regards,
>>>> David Zhou
>>>>> + bo = handle_table_lookup(&dev->bo_handles, i);
>>>>> + if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
>>>>> + continue;
>>>>> + if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))
>>>>> + break;
>>>>> + }
>>>>> +
>>>>> + if (i < dev->bo_handles.max_key) {
>>>>> + atomic_inc(&bo->refcount);
>>>>> + *buf_handle = bo;
>>>>> + *offset_in_bo = cpu - bo->cpu_ptr;
>>>>> + } else {
>>>>> + *buf_handle = NULL;
>>>>> + *offset_in_bo = 0;
>>>>> + }
>>>>> + pthread_mutex_unlock(&dev->bo_table_mutex);
>>>>> +
>>>>> + return 0;
>>>>> +}
>>>>> +
>>>>> int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
>>>>> void *cpu,
>>>>> uint64_t size,
>>>>
>>>
>>
>> _______________________________________________
>> amd-gfx mailing list
>> amd-gfx@lists.freedesktop.org
>> https://lists.freedesktop.org/mailman/listinfo/amd-gfx
>
_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
next prev parent reply other threads:[~2018-08-08 9:45 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-08 4:08 [PATCH libdrm 1/4] amdgpu: add bo from user memory to handle table Junwei Zhang
[not found] ` <1533701320-23661-1-git-send-email-Jerry.Zhang-5C7GfCeVMHo@public.gmane.org>
2018-08-08 4:08 ` [PATCH libdrm 2/4] amdgpu: add a function to find bo by cpu mapping (v2) Junwei Zhang
[not found] ` <1533701320-23661-2-git-send-email-Jerry.Zhang-5C7GfCeVMHo@public.gmane.org>
2018-08-08 4:23 ` zhoucm1
[not found] ` <b40df073-fc49-0e32-901d-2193f38700ea-5C7GfCeVMHo@public.gmane.org>
2018-08-08 6:48 ` Christian König
[not found] ` <2f043d23-5266-4dfc-a703-119a5e9acb5f-5C7GfCeVMHo@public.gmane.org>
2018-08-08 8:11 ` Zhang, Jerry (Junwei)
2018-08-08 8:43 ` zhoucm1
[not found] ` <5ddfa3f1-c71e-0dd8-3ec8-42bc646e3d69-5C7GfCeVMHo@public.gmane.org>
2018-08-08 8:51 ` Christian König
[not found] ` <7858ed2f-e504-f477-09a7-cd0b5ce90c58-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2018-08-08 9:45 ` Zhang, Jerry (Junwei) [this message]
2018-08-08 4:08 ` [PATCH libdrm 3/4] tests/amdgpu: add test for finding bo by CPU mapping Junwei Zhang
2018-08-08 4:08 ` [PATCH libdrm 4/4] amdgpu: add a function to create amdgpu bo internally Junwei Zhang
[not found] ` <1533701320-23661-4-git-send-email-Jerry.Zhang-5C7GfCeVMHo@public.gmane.org>
2018-08-08 6:51 ` Christian König
[not found] ` <1371bf42-2f74-f45b-d1f4-113cd44b806a-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2018-08-08 7:12 ` Zhang, Jerry (Junwei)
[not found] ` <5B6A97FB.3000003-5C7GfCeVMHo@public.gmane.org>
2018-08-08 7:18 ` Christian König
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=5B6ABBC2.7070601@amd.com \
--to=jerry.zhang-5c7gfcevmho@public.gmane.org \
--cc=amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
--cc=christian.koenig-5C7GfCeVMHo@public.gmane.org \
--cc=zhoucm1-5C7GfCeVMHo@public.gmane.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox