Skip to contentSkip to conversation

amdgpu: add bo from user memory to handle table

PATCH In review Junwei Zhang 4 participants 13 messages Aug 8, 2018 9 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 amdgpu: add bo from user memory to handle table

When create bo from user memory, add it to handle table for future query.

---
 amdgpu/amdgpu_bo.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
amdgpu/amdgpu_bo.c +10 -1
index 422c7c9..b24e698 100644
@@ -556,7 +556,16 @@ int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
 	bo->alloc_size = size;
 	bo->handle = args.handle;
 
-	*buf_handle = bo;
+	pthread_mutex_lock(&bo->dev->bo_table_mutex);
+	r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
+	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
+
+	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
+
+	if (r)
+		amdgpu_bo_free(bo);
+	else
+		*buf_handle = bo;
 
 	return r;
 }
Signature · 6 lines
1.9.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1533701320-23661-1-git-send-email-Jerry.Zhang-5C7GfCeVMHo@public.gmane.org

#2 lore raw

PATCH 2/4 amdgpu: add a function to find bo by cpu mapping (v2)

Userspace needs to know if the user memory is from BO or malloc.

v2: update mutex range and rebase

---
 amdgpu/amdgpu.h    | 23 +++++++++++++++++++++++
 amdgpu/amdgpu_bo.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
amdgpu/amdgpu.h +23 -0
index be83b45..a8c353c 100644
@@ -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()
amdgpu/amdgpu_bo.c +34 -0
index b24e698..a7f0662 100644
@@ -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++) {
+		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,
Signature · 6 lines
1.9.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1533701320-23661-2-git-send-email-Jerry.Zhang-5C7GfCeVMHo@public.gmane.org

#4 lore raw

Re: [PATCH libdrm 2/4] amdgpu: add a function to find bo by cpu mapping (v2)

Quoted 56 lines · Junwei Zhang

Userspace needs to know if the user memory is from BO or malloc.

v2: update mutex range and rebase

---
  amdgpu/amdgpu.h    | 23 +++++++++++++++++++++++
  amdgpu/amdgpu_bo.c | 34 ++++++++++++++++++++++++++++++++++
  2 files changed, 57 insertions(+)
amdgpu/amdgpu.h +23 -0
index be83b45..a8c353c 100644
@@ -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()
amdgpu/amdgpu_bo.c +4 -0
index b24e698..a7f0662 100644
@@ -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,

amdgpu/amdgpu_bo.c int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo, · line 545

Quoted 10 lines
amdgpu/amdgpu_bo.c +10 -0
+				  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.

Regards,
David Zhou

Quoted 13 lines
amdgpu/amdgpu_bo.c +13 -0
+		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;

amdgpu/amdgpu_bo.c int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo, · line 568

Quoted 10 lines
amdgpu/amdgpu_bo.c +7 -0
+		*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
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): b40df073-fc49-0e32-901d-2193f38700ea-5C7GfCeVMHo@public.gmane.org

#6 lore raw

Re: [PATCH libdrm 2/4] amdgpu: add a function to find bo by cpu mapping (v2)

Am 08.08.2018 um 06:23 schrieb zhoucm1:

Quoted 70 lines · Junwei Zhang
Quoted 55 lines

Userspace needs to know if the user memory is from BO or malloc.

v2: update mutex range and rebase

---
  amdgpu/amdgpu.h    | 23 +++++++++++++++++++++++ amdgpu/amdgpu_bo.c | 34 ++++++++++++++++++++++++++++++++++

  2 files changed, 57 insertions(+)
amdgpu/amdgpu.h +23 -0
index be83b45..a8c353c 100644
@@ -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()
amdgpu/amdgpu_bo.c +3 -0
index b24e698..a7f0662 100644
@@ -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,

amdgpu/amdgpu_bo.c int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo, · line 544

Quoted 10 lines
amdgpu/amdgpu_bo.c +10 -0
+                  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,

Quoted 2 lines

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.

If we find a wider use we can still start to use the kernel implementation again.

Regards,
Christian.

Quoted 18 lines

Regards,
David Zhou

Quoted 15 lines
amdgpu/amdgpu_bo.c +15 -0
+        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;
+    }
Quoted 9 lines

amdgpu/amdgpu_bo.c int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo, · line 568

Quoted 8 lines
amdgpu/amdgpu_bo.c +5 -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
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 2f043d23-5266-4dfc-a703-119a5e9acb5f-5C7GfCeVMHo@public.gmane.org

#8 lore raw

Re: [PATCH libdrm 2/4] amdgpu: add a function to find bo by cpu mapping (v2)

Quoted 70 lines · Christian König

Am 08.08.2018 um 06:23 schrieb zhoucm1:

Quoted 69 lines · Junwei Zhang
Quoted 54 lines

Userspace needs to know if the user memory is from BO or malloc.

v2: update mutex range and rebase

---
  amdgpu/amdgpu.h    | 23 +++++++++++++++++++++++
  amdgpu/amdgpu_bo.c | 34 ++++++++++++++++++++++++++++++++++
  2 files changed, 57 insertions(+)
amdgpu/amdgpu.h +23 -0
index be83b45..a8c353c 100644
@@ -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()
amdgpu/amdgpu_bo.c +3 -0
index b24e698..a7f0662 100644
@@ -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,

amdgpu/amdgpu_bo.c int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo, · line 544

Quoted 10 lines
amdgpu/amdgpu_bo.c +10 -0
+                  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,

Quoted 8 lines
Quoted 2 lines

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.

If we find a wider use we can still start to use the kernel implementation again.

Thanks you all to clarify that.

May I get RB for this patch?
Then I could work on the patch 4 only.
Thanks.

Regards,
Jerry

Quoted 32 lines

Regards,
Christian.

Quoted 27 lines

Regards,
David Zhou

Quoted 23 lines
amdgpu/amdgpu_bo.c +20 -0
+        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
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#9 lore raw

Re: [PATCH libdrm 2/4] amdgpu: add a function to find bo by cpu mapping (v2)

In reply to a message not in the archive
Quoted 71 lines · Christian König

Am 08.08.2018 um 06:23 schrieb zhoucm1:

Quoted 70 lines · Junwei Zhang
Quoted 55 lines

Userspace needs to know if the user memory is from BO or malloc.

v2: update mutex range and rebase

---
  amdgpu/amdgpu.h    | 23 +++++++++++++++++++++++ amdgpu/amdgpu_bo.c | 34 ++++++++++++++++++++++++++++++++++

  2 files changed, 57 insertions(+)
amdgpu/amdgpu.h +23 -0
index be83b45..a8c353c 100644
@@ -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()
amdgpu/amdgpu_bo.c +3 -0
index b24e698..a7f0662 100644
@@ -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,

amdgpu/amdgpu_bo.c int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo, · line 544

Quoted 10 lines
amdgpu/amdgpu_bo.c +10 -0
+                  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,

Quoted 6 lines
Quoted 2 lines

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.

Regards,
David Zhou

Quoted 35 lines

If we find a wider use we can still start to use the kernel implementation again.

Regards,
Christian.

Quoted 28 lines

Regards,
David Zhou

Quoted 24 lines
amdgpu/amdgpu_bo.c +20 -0
+        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
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 5ddfa3f1-c71e-0dd8-3ec8-42bc646e3d69-5C7GfCeVMHo@public.gmane.org

#11 lore raw

Re: [PATCH libdrm 2/4] amdgpu: add a function to find bo by cpu mapping (v2)

Am 08.08.2018 um 10:43 schrieb zhoucm1:

Quoted 74 lines · Christian König
Quoted 71 lines

Am 08.08.2018 um 06:23 schrieb zhoucm1:

Quoted 70 lines · Junwei Zhang
Quoted 55 lines

Userspace needs to know if the user memory is from BO or malloc.

v2: update mutex range and rebase

---
  amdgpu/amdgpu.h    | 23 +++++++++++++++++++++++ amdgpu/amdgpu_bo.c | 34 ++++++++++++++++++++++++++++++++++

  2 files changed, 57 insertions(+)
amdgpu/amdgpu.h +23 -0
index be83b45..a8c353c 100644
@@ -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()
amdgpu/amdgpu_bo.c +3 -0
index b24e698..a7f0662 100644
@@ -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,

amdgpu/amdgpu_bo.c int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo, · line 544

Quoted 10 lines
amdgpu/amdgpu_bo.c +10 -0
+                  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,

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

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 <[email protected]>.

Regards,
Christian.

Quoted 39 lines

Regards,
David Zhou

Quoted 35 lines

If we find a wider use we can still start to use the kernel implementation again.

Regards,
Christian.

Quoted 28 lines

Regards,
David Zhou

Quoted 24 lines
amdgpu/amdgpu_bo.c +20 -0
+        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,

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 7858ed2f-e504-f477-09a7-cd0b5ce90c58-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#13 lore raw

Re: [PATCH libdrm 2/4] amdgpu: add a function to find bo by cpu mapping (v2)

Quoted 82 lines · Christian König

Am 08.08.2018 um 10:43 schrieb zhoucm1:

Quoted 73 lines · Christian König
Quoted 70 lines

Am 08.08.2018 um 06:23 schrieb zhoucm1:

Quoted 69 lines · Junwei Zhang
Quoted 54 lines

Userspace needs to know if the user memory is from BO or malloc.

v2: update mutex range and rebase

---
  amdgpu/amdgpu.h    | 23 +++++++++++++++++++++++
  amdgpu/amdgpu_bo.c | 34 ++++++++++++++++++++++++++++++++++
  2 files changed, 57 insertions(+)
amdgpu/amdgpu.h +23 -0
index be83b45..a8c353c 100644
@@ -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()
amdgpu/amdgpu_bo.c +3 -0
index b24e698..a7f0662 100644
@@ -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,

amdgpu/amdgpu_bo.c int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo, · line 544

Quoted 10 lines
amdgpu/amdgpu_bo.c +10 -0
+                  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,

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

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.

Quoted 7 lines

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 <[email protected]>.

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

Quoted 47 lines

Regards,
Christian.

Quoted 42 lines

Regards,
David Zhou

Quoted 34 lines

If we find a wider use we can still start to use the kernel implementation again.

Regards,
Christian.

Quoted 27 lines

Regards,
David Zhou

Quoted 23 lines
amdgpu/amdgpu_bo.c +20 -0
+        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
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#14 lore raw

PATCH 3/4 tests/amdgpu: add test for finding bo by CPU mapping

In reply to a message not in the archive

Add a test for API to query bo by CPU mapping

---
 tests/amdgpu/bo_tests.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
tests/amdgpu/bo_tests.c +33 -0
index 9d4da4a..dc2de9b 100644
@@ -27,6 +27,7 @@
 
 #include "amdgpu_test.h"
 #include "amdgpu_drm.h"
+#include "amdgpu_internal.h"
 
 #define BUFFER_SIZE (4*1024)
 #define BUFFER_ALIGN (4*1024)
@@ -44,6 +45,7 @@ static void amdgpu_bo_metadata(void);
 static void amdgpu_bo_map_unmap(void);
 static void amdgpu_memory_alloc(void);
 static void amdgpu_mem_fail_alloc(void);
+static void amdgpu_bo_find_by_cpu_mapping(void);
 
 CU_TestInfo bo_tests[] = {
 	{ "Export/Import",  amdgpu_bo_export_import },
@@ -51,6 +53,7 @@ CU_TestInfo bo_tests[] = {
 	{ "CPU map/unmap",  amdgpu_bo_map_unmap },
 	{ "Memory alloc Test",  amdgpu_memory_alloc },
 	{ "Memory fail alloc Test",  amdgpu_mem_fail_alloc },
+	{ "Find bo by CPU mapping",  amdgpu_bo_find_by_cpu_mapping },
 	CU_TEST_INFO_NULL,
 };
 
@@ -262,3 +265,33 @@ static void amdgpu_mem_fail_alloc(void)
 		CU_ASSERT_EQUAL(r, 0);
 	}
 }
+
+static void amdgpu_bo_find_by_cpu_mapping(void)
+{
+	amdgpu_bo_handle bo_handle, find_bo_handle;
+	amdgpu_va_handle va_handle;
+	void *bo_cpu;
+	uint64_t bo_mc_address;
+	uint64_t offset;
+	int r;
+
+	r = amdgpu_bo_alloc_and_map(device_handle, 4096, 4096,
+				    AMDGPU_GEM_DOMAIN_GTT, 0,
+				    &bo_handle, &bo_cpu,
+				    &bo_mc_address, &va_handle);
+	CU_ASSERT_EQUAL(r, 0);
+
+	r = amdgpu_find_bo_by_cpu_mapping(device_handle,
+					  bo_cpu,
+					  4096,
+					  &find_bo_handle,
+					  &offset);
+	CU_ASSERT_EQUAL(r, 0);
+	CU_ASSERT_EQUAL(offset, 0);
+	CU_ASSERT_EQUAL(bo_handle->handle, find_bo_handle->handle);
+
+	atomic_dec(&find_bo_handle->refcount, 1);
+	r = amdgpu_bo_unmap_and_free(bo_handle, va_handle,
+				     bo_mc_address, 4096);
+	CU_ASSERT_EQUAL(r, 0);
+}
Signature · 6 lines
1.9.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#15 lore raw

PATCH 4/4 amdgpu: add a function to create amdgpu bo internally

In reply to a message not in the archive

a helper function to create and initialize amdgpu bo

---
 amdgpu/amdgpu_bo.c | 81 ++++++++++++++++++++++--------------------------------
 1 file changed, 33 insertions(+), 48 deletions(-)
amdgpu/amdgpu_bo.c +33 -48
index a7f0662..59cba69 100644
@@ -48,11 +48,39 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
 	drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &args);
 }
 
+static int amdgpu_bo_create(amdgpu_device_handle dev,
+			    uint64_t size,
+			    uint32_t handle,
+			    amdgpu_bo_handle *buf_handle)
+{
+	struct amdgpu_bo *bo;
+	int r = 0;
+
+	bo = calloc(1, sizeof(struct amdgpu_bo));
+	if (!bo)
+		return -ENOMEM;
+
+	atomic_set(&bo->refcount, 1);
+	bo->dev = dev;
+	bo->alloc_size = size;
+	bo->handle = handle;
+	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
+
+	pthread_mutex_lock(&bo->dev->bo_table_mutex);
+	r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
+	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
+	if (r)
+		amdgpu_bo_free(bo);
+	else
+		*buf_handle = bo;
+
+	return r;
+}
+
 int amdgpu_bo_alloc(amdgpu_device_handle dev,
 		    struct amdgpu_bo_alloc_request *alloc_buffer,
 		    amdgpu_bo_handle *buf_handle)
 {
-	struct amdgpu_bo *bo;
 	union drm_amdgpu_gem_create args;
 	unsigned heap = alloc_buffer->preferred_heap;
 	int r = 0;
@@ -61,14 +89,6 @@ int amdgpu_bo_alloc(amdgpu_device_handle dev,
 	if (!(heap & (AMDGPU_GEM_DOMAIN_GTT | AMDGPU_GEM_DOMAIN_VRAM)))
 		return -EINVAL;
 
-	bo = calloc(1, sizeof(struct amdgpu_bo));
-	if (!bo)
-		return -ENOMEM;
-
-	atomic_set(&bo->refcount, 1);
-	bo->dev = dev;
-	bo->alloc_size = alloc_buffer->alloc_size;
-
 	memset(&args, 0, sizeof(args));
 	args.in.bo_size = alloc_buffer->alloc_size;
 	args.in.alignment = alloc_buffer->phys_alignment;
@@ -80,25 +100,11 @@ int amdgpu_bo_alloc(amdgpu_device_handle dev,
 	/* Allocate the buffer with the preferred heap. */
 	r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_CREATE,
 				&args, sizeof(args));
-	if (r) {
-		free(bo);
-		return r;
-	}
-
-	bo->handle = args.out.handle;
-
-	pthread_mutex_lock(&bo->dev->bo_table_mutex);
-	r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
-	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
-
-	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
-
 	if (r)
-		amdgpu_bo_free(bo);
-	else
-		*buf_handle = bo;
+		return r;
 
-	return r;
+	return amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
+				buf_handle);
 }
 
 int amdgpu_bo_set_metadata(amdgpu_bo_handle bo,
@@ -569,7 +575,6 @@ int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
 				    amdgpu_bo_handle *buf_handle)
 {
 	int r;
-	struct amdgpu_bo *bo;
 	struct drm_amdgpu_gem_userptr args;
 
 	args.addr = (uintptr_t)cpu;
@@ -581,27 +586,7 @@ int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
 	if (r)
 		return r;
 
-	bo = calloc(1, sizeof(struct amdgpu_bo));
-	if (!bo)
-		return -ENOMEM;
-
-	atomic_set(&bo->refcount, 1);
-	bo->dev = dev;
-	bo->alloc_size = size;
-	bo->handle = args.handle;
-
-	pthread_mutex_lock(&bo->dev->bo_table_mutex);
-	r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
-	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
-
-	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
-
-	if (r)
-		amdgpu_bo_free(bo);
-	else
-		*buf_handle = bo;
-
-	return r;
+	return amdgpu_bo_create(dev, size, args.handle, buf_handle);
 }
 
 int amdgpu_bo_list_create(amdgpu_device_handle dev,
Signature · 6 lines
1.9.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1533701320-23661-4-git-send-email-Jerry.Zhang-5C7GfCeVMHo@public.gmane.org

#17 lore raw

Re: [PATCH libdrm 4/4] amdgpu: add a function to create amdgpu bo internally

Am 08.08.2018 um 06:08 schrieb Junwei Zhang:

Quoted 1 line

a helper function to create and initialize amdgpu bo

Can the new function be also used to initialize a BO structure during import?

Apart from that it look like a nice cleanup to me, Christian.

Quoted 122 lines
---
  amdgpu/amdgpu_bo.c | 81 ++++++++++++++++++++++--------------------------------
  1 file changed, 33 insertions(+), 48 deletions(-)
amdgpu/amdgpu_bo.c +32 -42
index a7f0662..59cba69 100644
@@ -48,11 +48,39 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
  	drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &args);
  }
 
+static int amdgpu_bo_create(amdgpu_device_handle dev,
+			    uint64_t size,
+			    uint32_t handle,
+			    amdgpu_bo_handle *buf_handle)
+{
+	struct amdgpu_bo *bo;
+	int r = 0;
+
+	bo = calloc(1, sizeof(struct amdgpu_bo));
+	if (!bo)
+		return -ENOMEM;
+
+	atomic_set(&bo->refcount, 1);
+	bo->dev = dev;
+	bo->alloc_size = size;
+	bo->handle = handle;
+	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
+
+	pthread_mutex_lock(&bo->dev->bo_table_mutex);
+	r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
+	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
+	if (r)
+		amdgpu_bo_free(bo);
+	else
+		*buf_handle = bo;
+
+	return r;
+}
+
  int amdgpu_bo_alloc(amdgpu_device_handle dev,
  		    struct amdgpu_bo_alloc_request *alloc_buffer,
  		    amdgpu_bo_handle *buf_handle)
  {
-	struct amdgpu_bo *bo;
  	union drm_amdgpu_gem_create args;
  	unsigned heap = alloc_buffer->preferred_heap;
  	int r = 0;
@@ -61,14 +89,6 @@ int amdgpu_bo_alloc(amdgpu_device_handle dev,
  	if (!(heap & (AMDGPU_GEM_DOMAIN_GTT | AMDGPU_GEM_DOMAIN_VRAM)))
  		return -EINVAL;
 
-	bo = calloc(1, sizeof(struct amdgpu_bo));
-	if (!bo)
-		return -ENOMEM;
-
-	atomic_set(&bo->refcount, 1);
-	bo->dev = dev;
-	bo->alloc_size = alloc_buffer->alloc_size;
-
  	memset(&args, 0, sizeof(args));
  	args.in.bo_size = alloc_buffer->alloc_size;
  	args.in.alignment = alloc_buffer->phys_alignment;
@@ -80,25 +100,11 @@ int amdgpu_bo_alloc(amdgpu_device_handle dev,
  	/* Allocate the buffer with the preferred heap. */
  	r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_CREATE,
  				&args, sizeof(args));
-	if (r) {
-		free(bo);
-		return r;
-	}
-
-	bo->handle = args.out.handle;
-
-	pthread_mutex_lock(&bo->dev->bo_table_mutex);
-	r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
-	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
-
-	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
-
  	if (r)
-		amdgpu_bo_free(bo);
-	else
-		*buf_handle = bo;
+		return r;
 
-	return r;
+	return amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
+				buf_handle);
  }
 
  int amdgpu_bo_set_metadata(amdgpu_bo_handle bo,
@@ -569,7 +575,6 @@ int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
  				    amdgpu_bo_handle *buf_handle)
  {
  	int r;
-	struct amdgpu_bo *bo;
  	struct drm_amdgpu_gem_userptr args;
 
  	args.addr = (uintptr_t)cpu;
@@ -581,27 +586,7 @@ int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
  	if (r)
  		return r;
 
-	bo = calloc(1, sizeof(struct amdgpu_bo));
-	if (!bo)
-		return -ENOMEM;
-
-	atomic_set(&bo->refcount, 1);
-	bo->dev = dev;
-	bo->alloc_size = size;
-	bo->handle = args.handle;
-
-	pthread_mutex_lock(&bo->dev->bo_table_mutex);
-	r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
-	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
-
-	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
-

amdgpu/amdgpu_bo.c int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev, · line 592

Quoted 10 lines
amdgpu/amdgpu_bo.c +1 -6
-	if (r)
-		amdgpu_bo_free(bo);
-	else
-		*buf_handle = bo;
-
-	return r;
+	return amdgpu_bo_create(dev, size, args.handle, buf_handle);
  }
 
  int amdgpu_bo_list_create(amdgpu_device_handle dev,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1371bf42-2f74-f45b-d1f4-113cd44b806a-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#19 lore raw

Re: [PATCH libdrm 4/4] amdgpu: add a function to create amdgpu bo internally

Quoted 4 lines · Christian König

Am 08.08.2018 um 06:08 schrieb Junwei Zhang:

Quoted 1 line

a helper function to create and initialize amdgpu bo

Can the new function be also used to initialize a BO structure during import?

Yeah, that's what I'm going to talk a bit more in this patch. (actually it's a RFC patch)

When I'm working on it, find amdgpu_bo_import() holds the table lock through the function. Wonder if it involves any potential issue, if I add the amdgpu_bo_create() at the end of function out of the table lock?

If so, I would provide a amdgpu_bo_create_locked() function to insert the range of holding the lock.

Any background/insight could be shared?

Regards,
Jerry

Quoted 121 lines

Apart from that it look like a nice cleanup to me, Christian.

Quoted 117 lines
---
  amdgpu/amdgpu_bo.c | 81 ++++++++++++++++++++++--------------------------------
  1 file changed, 33 insertions(+), 48 deletions(-)
amdgpu/amdgpu_bo.c +32 -43
index a7f0662..59cba69 100644
@@ -48,11 +48,39 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
      drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &args);
  }
+static int amdgpu_bo_create(amdgpu_device_handle dev,
+                uint64_t size,
+                uint32_t handle,
+                amdgpu_bo_handle *buf_handle)
+{
+    struct amdgpu_bo *bo;
+    int r = 0;
+
+    bo = calloc(1, sizeof(struct amdgpu_bo));
+    if (!bo)
+        return -ENOMEM;
+
+    atomic_set(&bo->refcount, 1);
+    bo->dev = dev;
+    bo->alloc_size = size;
+    bo->handle = handle;
+    pthread_mutex_init(&bo->cpu_access_mutex, NULL);
+
+    pthread_mutex_lock(&bo->dev->bo_table_mutex);
+    r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
+    pthread_mutex_unlock(&bo->dev->bo_table_mutex);
+    if (r)
+        amdgpu_bo_free(bo);
+    else
+        *buf_handle = bo;
+
+    return r;
+}
+
  int amdgpu_bo_alloc(amdgpu_device_handle dev,
              struct amdgpu_bo_alloc_request *alloc_buffer,
              amdgpu_bo_handle *buf_handle)
  {
-    struct amdgpu_bo *bo;
      union drm_amdgpu_gem_create args;
      unsigned heap = alloc_buffer->preferred_heap;
      int r = 0;
@@ -61,14 +89,6 @@ int amdgpu_bo_alloc(amdgpu_device_handle dev,
      if (!(heap & (AMDGPU_GEM_DOMAIN_GTT | AMDGPU_GEM_DOMAIN_VRAM)))
          return -EINVAL;
-    bo = calloc(1, sizeof(struct amdgpu_bo));
-    if (!bo)
-        return -ENOMEM;
-
-    atomic_set(&bo->refcount, 1);
-    bo->dev = dev;
-    bo->alloc_size = alloc_buffer->alloc_size;
-
      memset(&args, 0, sizeof(args));
      args.in.bo_size = alloc_buffer->alloc_size;
      args.in.alignment = alloc_buffer->phys_alignment;
@@ -80,25 +100,11 @@ int amdgpu_bo_alloc(amdgpu_device_handle dev,
      /* Allocate the buffer with the preferred heap. */
      r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_CREATE,
                  &args, sizeof(args));
-    if (r) {
-        free(bo);
-        return r;
-    }
-
-    bo->handle = args.out.handle;
-
-    pthread_mutex_lock(&bo->dev->bo_table_mutex);
-    r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
-    pthread_mutex_unlock(&bo->dev->bo_table_mutex);
-
-    pthread_mutex_init(&bo->cpu_access_mutex, NULL);
-
      if (r)
-        amdgpu_bo_free(bo);
-    else
-        *buf_handle = bo;
+        return r;
-    return r;
+    return amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
+                buf_handle);
  }
  int amdgpu_bo_set_metadata(amdgpu_bo_handle bo,
@@ -569,7 +575,6 @@ int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
                      amdgpu_bo_handle *buf_handle)
  {
      int r;
-    struct amdgpu_bo *bo;
      struct drm_amdgpu_gem_userptr args;
      args.addr = (uintptr_t)cpu;
@@ -581,27 +586,7 @@ int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
      if (r)
          return r;
-    bo = calloc(1, sizeof(struct amdgpu_bo));
-    if (!bo)
-        return -ENOMEM;
-
-    atomic_set(&bo->refcount, 1);
-    bo->dev = dev;
-    bo->alloc_size = size;
-    bo->handle = args.handle;
-
-    pthread_mutex_lock(&bo->dev->bo_table_mutex);
-    r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
-    pthread_mutex_unlock(&bo->dev->bo_table_mutex);
-
-    pthread_mutex_init(&bo->cpu_access_mutex, NULL);
-
-    if (r)
Quoted 9 lines

amdgpu/amdgpu_bo.c int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev, · line 592

Quoted 8 lines
amdgpu/amdgpu_bo.c +1 -5
-        amdgpu_bo_free(bo);
-    else
-        *buf_handle = bo;
-
-    return r;
+    return amdgpu_bo_create(dev, size, args.handle, buf_handle);
  }
  int amdgpu_bo_list_create(amdgpu_device_handle dev,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): [email protected]

#21 lore raw

Re: [PATCH libdrm 4/4] amdgpu: add a function to create amdgpu bo internally

Am 08.08.2018 um 09:12 schrieb Zhang, Jerry (Junwei):

Quoted 9 lines · Christian König
Quoted 4 lines

Am 08.08.2018 um 06:08 schrieb Junwei Zhang:

Quoted 1 line

a helper function to create and initialize amdgpu bo

Can the new function be also used to initialize a BO structure during import?

Yeah, that's what I'm going to talk a bit more in this patch. (actually it's a RFC patch)

Quoted 7 lines

When I'm working on it, find amdgpu_bo_import() holds the table lock through the function. Wonder if it involves any potential issue, if I add the amdgpu_bo_create() at the end of function out of the table lock?

If so, I would provide a amdgpu_bo_create_locked() function to insert the range of holding the lock.

Any background/insight could be shared?

Ah, yes of course. We need to hold the lock during import to make sure that we don't import things twice.

I would just move adding the handle to the table out of the function into the callers.

It's still a nice cleanup if we just have the structure initialization in one place.

Christian.

Quoted 139 lines

Regards,
Jerry

Quoted 135 lines

Apart from that it look like a nice cleanup to me, Christian.

Quoted 130 lines

---
  amdgpu/amdgpu_bo.c | 81
++++++++++++++++++++++--------------------------------

  1 file changed, 33 insertions(+), 48 deletions(-)
amdgpu/amdgpu_bo.c +33 -48
index a7f0662..59cba69 100644
@@ -48,11 +48,39 @@ static void 
 amdgpu_close_kms_handle(amdgpu_device_handle dev,
       drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &args);
   }
+static int amdgpu_bo_create(amdgpu_device_handle dev,
+                uint64_t size,
+                uint32_t handle,
+                amdgpu_bo_handle *buf_handle)
+{
+    struct amdgpu_bo *bo;
+    int r = 0;
+
+    bo = calloc(1, sizeof(struct amdgpu_bo));
+    if (!bo)
+        return -ENOMEM;
+
+    atomic_set(&bo->refcount, 1);
+    bo->dev = dev;
+    bo->alloc_size = size;
+    bo->handle = handle;
+    pthread_mutex_init(&bo->cpu_access_mutex, NULL);
+
+    pthread_mutex_lock(&bo->dev->bo_table_mutex);
+    r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
+    pthread_mutex_unlock(&bo->dev->bo_table_mutex);
+    if (r)
+        amdgpu_bo_free(bo);
+    else
+        *buf_handle = bo;
+
+    return r;
+}
+
   int amdgpu_bo_alloc(amdgpu_device_handle dev,
               struct amdgpu_bo_alloc_request *alloc_buffer,
               amdgpu_bo_handle *buf_handle)
   {
-    struct amdgpu_bo *bo;
       union drm_amdgpu_gem_create args;
       unsigned heap = alloc_buffer->preferred_heap;
       int r = 0;
@@ -61,14 +89,6 @@ int amdgpu_bo_alloc(amdgpu_device_handle dev,
       if (!(heap & (AMDGPU_GEM_DOMAIN_GTT | AMDGPU_GEM_DOMAIN_VRAM)))
           return -EINVAL;
-    bo = calloc(1, sizeof(struct amdgpu_bo));
-    if (!bo)
-        return -ENOMEM;
-
-    atomic_set(&bo->refcount, 1);
-    bo->dev = dev;
-    bo->alloc_size = alloc_buffer->alloc_size;
-
       memset(&args, 0, sizeof(args));
       args.in.bo_size = alloc_buffer->alloc_size;
       args.in.alignment = alloc_buffer->phys_alignment;
@@ -80,25 +100,11 @@ int amdgpu_bo_alloc(amdgpu_device_handle dev,
       /* Allocate the buffer with the preferred heap. */
       r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_CREATE,
                   &args, sizeof(args));
-    if (r) {
-        free(bo);
-        return r;
-    }
-
-    bo->handle = args.out.handle;
-
-    pthread_mutex_lock(&bo->dev->bo_table_mutex);
-    r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
-    pthread_mutex_unlock(&bo->dev->bo_table_mutex);
-
-    pthread_mutex_init(&bo->cpu_access_mutex, NULL);
-
       if (r)
-        amdgpu_bo_free(bo);
-    else
-        *buf_handle = bo;
+        return r;
-    return r;
+    return amdgpu_bo_create(dev, alloc_buffer->alloc_size, 
 args.out.handle,
+                buf_handle);
   }
   int amdgpu_bo_set_metadata(amdgpu_bo_handle bo,
@@ -569,7 +575,6 @@ int 
 amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
                       amdgpu_bo_handle *buf_handle)
   {
       int r;
-    struct amdgpu_bo *bo;
       struct drm_amdgpu_gem_userptr args;
       args.addr = (uintptr_t)cpu;
@@ -581,27 +586,7 @@ int 
 amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
       if (r)
           return r;
-    bo = calloc(1, sizeof(struct amdgpu_bo));
-    if (!bo)
-        return -ENOMEM;
-
-    atomic_set(&bo->refcount, 1);
-    bo->dev = dev;
-    bo->alloc_size = size;
-    bo->handle = args.handle;
-
-    pthread_mutex_lock(&bo->dev->bo_table_mutex);
-    r = handle_table_insert(&bo->dev->bo_handles, bo->handle, bo);
-    pthread_mutex_unlock(&bo->dev->bo_table_mutex);
-
-    pthread_mutex_init(&bo->cpu_access_mutex, NULL);
-
-    if (r)
-        amdgpu_bo_free(bo);
-    else
-        *buf_handle = bo;
-
-    return r;
+    return amdgpu_bo_create(dev, size, args.handle, buf_handle);
   }
   int amdgpu_bo_list_create(amdgpu_device_handle dev,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx