Skip to contentSkip to conversation

amdgpu: always add all BOs to lockup table

PATCH In review Christian König 4 participants 11 messages Aug 2, 2018 – Aug 3, 2018 3 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 amdgpu: always add all BOs to lockup table

This way we can always find a BO structure by its handle.

---
 amdgpu/amdgpu_bo.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
amdgpu/amdgpu_bo.c +5 -11
index d29be244..39228620 100644
@@ -88,6 +88,11 @@ int amdgpu_bo_alloc(amdgpu_device_handle dev,
 
 	bo->handle = args.out.handle;
 
+	pthread_mutex_lock(&bo->dev->bo_table_mutex);
+	util_hash_table_set(bo->dev->bo_handles,
+			    (void*)(uintptr_t)bo->handle, bo);
+	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
+
 	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
 
 	*buf_handle = bo;
@@ -168,14 +173,6 @@ int amdgpu_bo_query_info(amdgpu_bo_handle bo,
 	return 0;
 }
 
-static void amdgpu_add_handle_to_table(amdgpu_bo_handle bo)
-{
-	pthread_mutex_lock(&bo->dev->bo_table_mutex);
-	util_hash_table_set(bo->dev->bo_handles,
-			    (void*)(uintptr_t)bo->handle, bo);
-	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
-}
-
 static int amdgpu_bo_export_flink(amdgpu_bo_handle bo)
 {
 	struct drm_gem_flink flink;
@@ -240,14 +237,11 @@ int amdgpu_bo_export(amdgpu_bo_handle bo,
 		return 0;
 
 	case amdgpu_bo_handle_type_kms:
-		amdgpu_add_handle_to_table(bo);
-		/* fall through */
 	case amdgpu_bo_handle_type_kms_noimport:
 		*shared_handle = bo->handle;
 		return 0;
 
 	case amdgpu_bo_handle_type_dma_buf_fd:
-		amdgpu_add_handle_to_table(bo);
 		return drmPrimeHandleToFD(bo->dev->fd, bo->handle,
 					  DRM_CLOEXEC | DRM_RDWR,
 					  (int*)shared_handle);
Signature · 6 lines
2.14.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): 20180802140452.23894-1-christian.koenig-5C7GfCeVMHo@public.gmane.org

#2 lore raw

PATCH 2/6 amdgpu: stop using the hash table for fd_tab

We have so few devices that just walking a linked list is probably faster.

---
 amdgpu/amdgpu_device.c   | 49 ++++++++++++++++--------------------------------
 amdgpu/amdgpu_internal.h |  1 +
 2 files changed, 17 insertions(+), 33 deletions(-)
amdgpu/amdgpu_device.c +16 -33
index d7aec6a4..38fd186d 100644
@@ -43,10 +43,9 @@
 #include "util_math.h"
 
 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
-#define UINT_TO_PTR(x) ((void *)((intptr_t)(x)))
 
 static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
-static struct util_hash_table *fd_tab;
+static amdgpu_device_handle fd_list;
 
 static unsigned handle_hash(void *key)
 {
@@ -58,28 +57,8 @@ static int handle_compare(void *key1, void *key2)
 	return PTR_TO_UINT(key1) != PTR_TO_UINT(key2);
 }
 
-static unsigned fd_hash(void *key)
+static int fd_compare(int fd1, int fd2)
 {
-	int fd = PTR_TO_UINT(key);
-	char *name = drmGetPrimaryDeviceNameFromFd(fd);
-	unsigned result = 0;
-	char *c;
-
-	if (name == NULL)
-		return 0;
-
-	for (c = name; *c; ++c)
-		result += *c;
-
-	free(name);
-
-	return result;
-}
-
-static int fd_compare(void *key1, void *key2)
-{
-	int fd1 = PTR_TO_UINT(key1);
-	int fd2 = PTR_TO_UINT(key2);
 	char *name1 = drmGetPrimaryDeviceNameFromFd(fd1);
 	char *name2 = drmGetPrimaryDeviceNameFromFd(fd2);
 	int result;
@@ -127,16 +106,17 @@ static int amdgpu_get_auth(int fd, int *auth)
 
 static void amdgpu_device_free_internal(amdgpu_device_handle dev)
 {
+	amdgpu_device_handle *node = &fd_list;
+
 	pthread_mutex_lock(&fd_mutex);
-	util_hash_table_remove(fd_tab, UINT_TO_PTR(dev->fd));
-	if (util_hash_table_count(fd_tab) == 0) {
-		util_hash_table_destroy(fd_tab);
-		fd_tab = NULL;
-	}
+	while (*node != dev && (*node)->next)
+		node = &(*node)->next;
+	*node = (*node)->next;
+	pthread_mutex_unlock(&fd_mutex);
+
 	close(dev->fd);
 	if ((dev->flink_fd >= 0) && (dev->fd != dev->flink_fd))
 		close(dev->flink_fd);
-	pthread_mutex_unlock(&fd_mutex);
 
 	amdgpu_vamgr_deinit(&dev->vamgr_32);
 	amdgpu_vamgr_deinit(&dev->vamgr);
@@ -187,8 +167,6 @@ int amdgpu_device_initialize(int fd,
 	*device_handle = NULL;
 
 	pthread_mutex_lock(&fd_mutex);
-	if (!fd_tab)
-		fd_tab = util_hash_table_create(fd_hash, fd_compare);
 	r = amdgpu_get_auth(fd, &flag_auth);
 	if (r) {
 		fprintf(stderr, "%s: amdgpu_get_auth (1) failed (%i)\n",
@@ -196,7 +174,11 @@ int amdgpu_device_initialize(int fd,
 		pthread_mutex_unlock(&fd_mutex);
 		return r;
 	}
-	dev = util_hash_table_get(fd_tab, UINT_TO_PTR(fd));
+
+	for (dev = fd_list; dev; dev = dev->next)
+		if (fd_compare(dev->fd, fd) == 0)
+			break;
+
 	if (dev) {
 		r = amdgpu_get_auth(dev->fd, &flag_authexist);
 		if (r) {
@@ -297,7 +279,8 @@ int amdgpu_device_initialize(int fd,
 	*major_version = dev->major_version;
 	*minor_version = dev->minor_version;
 	*device_handle = dev;
-	util_hash_table_set(fd_tab, UINT_TO_PTR(dev->fd), dev);
+	dev->next = fd_list;
+	fd_list = dev;
 	pthread_mutex_unlock(&fd_mutex);
 
 	return 0;
amdgpu/amdgpu_internal.h +1 -0
index 99b8ce0b..83012cab 100644
@@ -65,6 +65,7 @@ struct amdgpu_va {
 
 struct amdgpu_device {
 	atomic_t refcount;
+	struct amdgpu_device *next;
 	int fd;
 	int flink_fd;
 	unsigned major_version;
Signature · 6 lines
2.14.1

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

PATCH 3/6 amdgpu: add handle table implementation

In reply to a message not in the archive

The kernel handles are dense and the kernel always tries to use the lowest free id. Use this to implement a more efficient handle table by using a resizeable array instead of a hash.

---
 amdgpu/Makefile.sources |  4 +++-
 amdgpu/handle_table.c   | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
 amdgpu/handle_table.h   | 40 +++++++++++++++++++++++++++++++++
 3 files changed, 102 insertions(+), 1 deletion(-)
 create mode 100644 amdgpu/handle_table.c
 create mode 100644 amdgpu/handle_table.h
amdgpu/Makefile.sources +3 -1
index 498b64cc..62577ba5 100644
@@ -10,7 +10,9 @@ LIBDRM_AMDGPU_FILES := \
 	util_hash.c \
 	util_hash.h \
 	util_hash_table.c \
-	util_hash_table.h
+	util_hash_table.h \
+	handle_table.c \
+	handle_table.h
 
 LIBDRM_AMDGPU_H_FILES := \
 	amdgpu.h
amdgpu/handle_table.c new file +59 -0

59 lines not on this page · raw message

amdgpu/handle_table.h new file +40 -0
new file mode 100644
index 00000000..9a170445
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2018 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef _HANDLE_TABLE_H_
+#define _HANDLE_TABLE_H_
+
+#include <stdint.h>
+#include "libdrm_macros.h"
+
+struct handle_table {
+	uint32_t	max_key;
+	void		**values;
+};
+
+drm_private int handle_table_insert(struct handle_table *table, uint32_t key,
+				    void *value);
+drm_private void handle_table_remove(struct handle_table *table, uint32_t key);
+drm_private void *handle_table_lockup(struct handle_table *table, uint32_t key);
+
+#endif /* _HANDLE_TABLE_H_ */
Signature · 6 lines
2.14.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): 20180802140452.23894-3-christian.koenig-5C7GfCeVMHo@public.gmane.org

#5 lore raw

Re: [PATCH libdrm 3/6] amdgpu: add handle table implementation

amdgpu/handle_table.c line 34

Quoted 13 lines · Christian König

The kernel handles are dense and the kernel always tries to use the lowest free id. Use this to implement a more efficient handle table by using a resizeable array instead of a hash.

[...]

amdgpu/handle_table.c new file +5 -0
+drm_private int handle_table_insert(struct handle_table *table, uint32_t key,
+				    void *value)
+{
+	if (key >= table->max_key) {
+		uint32_t max_key = ALIGN(key, 512);

The idea here is to make the array page aligned, right? However, as is it's only aligned to half the page size on 32-bit (and even worse on systems where the minimum page size is e.g. 64K). Something like

uint32_t max_key = ALIGN(key, sysconf(_SC_PAGESIZE) / sizeof(void*))

should do the trick.

Also, should handle_table_insert return an error if the table already has a different non-NULL entry for the key?

amdgpu/handle_table.c line 59

Quoted 9 lines

drm_private void handle_table_remove(struct handle_table *table, uint32_t key)

amdgpu/handle_table.c new file +8 -0
+{
+	table->values[key] = NULL;
+}
+
+drm_private void *handle_table_lockup(struct handle_table *table, uint32_t key)
+{
+	return table->values[key];
+}

Typo: Should be "lookup", not "lockup".

Maybe these should check that key < table->max_key, to prevent memory outside of the table from getting corrupted by buggy callers.

Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#6 lore raw

Re: [PATCH libdrm 3/6] amdgpu: add handle table implementation

In reply to a message not in the archive

Applies to patch #1 and patch #3, should lockup not be lookup?

Quoted 129 lines · Christian König

The kernel handles are dense and the kernel always tries to use the lowest free id. Use this to implement a more efficient handle table by using a resizeable array instead of a hash.

---
  amdgpu/Makefile.sources |  4 +++-
  amdgpu/handle_table.c   | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
  amdgpu/handle_table.h   | 40 +++++++++++++++++++++++++++++++++
  3 files changed, 102 insertions(+), 1 deletion(-)

create mode 100644 amdgpu/handle_table.c
create mode 100644 amdgpu/handle_table.h

amdgpu/Makefile.sources +3 -1
index 498b64cc..62577ba5 100644
@@ -10,7 +10,9 @@ LIBDRM_AMDGPU_FILES := \
  	util_hash.c \
  	util_hash.h \
  	util_hash_table.c \
-	util_hash_table.h
+	util_hash_table.h \
+	handle_table.c \
+	handle_table.h
 
  LIBDRM_AMDGPU_H_FILES := \
  	amdgpu.h
amdgpu/handle_table.c new file +59 -0
new file mode 100644
index 00000000..9acc44d5
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2018 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "handle_table.h"
+#include "util_math.h"
+
+drm_private int handle_table_insert(struct handle_table *table, uint32_t key,
+				    void *value)
+{
+	if (key >= table->max_key) {
+		uint32_t max_key = ALIGN(key, 512);
+		void **values;
+
+		values = realloc(table->values, max_key * sizeof(void *));
+		if (!values)
+			return -ENOMEM;
+
+		memset(values + table->max_key, 0, (max_key - table->max_key) *
+		       sizeof(void *));
+
+		table->max_key = max_key;
+		table->values = values;
+	}
+	table->values[key] = value;
+	return 0;
+}
+
+drm_private void handle_table_remove(struct handle_table *table, uint32_t key)
+{
+	table->values[key] = NULL;
+}
+
+drm_private void *handle_table_lockup(struct handle_table *table, uint32_t key)
+{
+	return table->values[key];
+}
amdgpu/handle_table.h new file +30 -0
new file mode 100644
index 00000000..9a170445
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2018 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef _HANDLE_TABLE_H_
+#define _HANDLE_TABLE_H_
+
+#include <stdint.h>
+#include "libdrm_macros.h"
+
+struct handle_table {

amdgpu/handle_table.h line 40

Quoted 10 lines
amdgpu/handle_table.h new file +10 -0
+	uint32_t	max_key;
+	void		**values;
+};
+
+drm_private int handle_table_insert(struct handle_table *table, uint32_t key,
+				    void *value);
+drm_private void handle_table_remove(struct handle_table *table, uint32_t key);
+drm_private void *handle_table_lockup(struct handle_table *table, uint32_t key);
+
+#endif /* _HANDLE_TABLE_H_ */

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

Reviewed-by Junwei Zhang
#7 lore raw

Re: [PATCH libdrm 3/6] amdgpu: add handle table implementation

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

The kernel handles are dense and the kernel always tries to use the lowest free id. Use this to implement a more efficient handle table by using a resizeable array instead of a hash.

---
  amdgpu/Makefile.sources |  4 +++-
  amdgpu/handle_table.c   | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
  amdgpu/handle_table.h   | 40 +++++++++++++++++++++++++++++++++
  3 files changed, 102 insertions(+), 1 deletion(-)

create mode 100644 amdgpu/handle_table.c
create mode 100644 amdgpu/handle_table.h

amdgpu/Makefile.sources +3 -1
index 498b64cc..62577ba5 100644
@@ -10,7 +10,9 @@ LIBDRM_AMDGPU_FILES := \
  	util_hash.c \
  	util_hash.h \
  	util_hash_table.c \
-	util_hash_table.h
+	util_hash_table.h \
+	handle_table.c \
+	handle_table.h
 
  LIBDRM_AMDGPU_H_FILES := \
  	amdgpu.h
amdgpu/handle_table.c new file +44 -0
new file mode 100644
index 00000000..9acc44d5
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2018 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "handle_table.h"
+#include "util_math.h"
+
+drm_private int handle_table_insert(struct handle_table *table, uint32_t key,
+				    void *value)
+{
+	if (key >= table->max_key) {
+		uint32_t max_key = ALIGN(key, 512);
+		void **values;
+
+		values = realloc(table->values, max_key * sizeof(void *));
+		if (!values)
+			return -ENOMEM;
+
+		memset(values + table->max_key, 0, (max_key - table->max_key) *
+		       sizeof(void *));
+
+		table->max_key = max_key;

amdgpu/handle_table.c line 54

Quoted 10 lines
amdgpu/handle_table.c new file +10 -0
+		table->values = values;
+	}
+	table->values[key] = value;
+	return 0;
+}
+
+drm_private void handle_table_remove(struct handle_table *table, uint32_t key)
+{
+	table->values[key] = NULL;
+}

Not sure if there are too many handles for all devices, since we don't free the memory. please confirm that.

Apart from that and typo, the series looks good for me.

When I try to re-implement the find bo API, the game has regression issue. I have to verify it when it's ready.
Then send out the patch again.

Jerry

Quoted 42 lines
amdgpu/handle_table.c new file +5 -0
+
+drm_private void *handle_table_lockup(struct handle_table *table, uint32_t key)
+{
+	return table->values[key];
+}
amdgpu/handle_table.h new file +30 -0
new file mode 100644
index 00000000..9a170445
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2018 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef _HANDLE_TABLE_H_
+#define _HANDLE_TABLE_H_
+
+#include <stdint.h>
+#include "libdrm_macros.h"
+
+struct handle_table {

amdgpu/handle_table.h line 40

Quoted 10 lines
amdgpu/handle_table.h new file +10 -0
+	uint32_t	max_key;
+	void		**values;
+};
+
+drm_private int handle_table_insert(struct handle_table *table, uint32_t key,
+				    void *value);
+drm_private void handle_table_remove(struct handle_table *table, uint32_t key);
+drm_private void *handle_table_lockup(struct handle_table *table, uint32_t key);
+
+#endif /* _HANDLE_TABLE_H_ */

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

#8 lore raw

PATCH 4/6 amdgpu: use handle table for KMS handles

In reply to a message not in the archive

Instead of the hash use the handle table.

---
 amdgpu/amdgpu_bo.c       | 19 ++++++++++---------
 amdgpu/amdgpu_device.c   |  2 --
 amdgpu/amdgpu_internal.h |  3 ++-
 3 files changed, 12 insertions(+), 12 deletions(-)
amdgpu/amdgpu_bo.c +10 -9
index 39228620..9a7d0b29 100644
@@ -89,14 +89,17 @@ int amdgpu_bo_alloc(amdgpu_device_handle dev,
 	bo->handle = args.out.handle;
 
 	pthread_mutex_lock(&bo->dev->bo_table_mutex);
-	util_hash_table_set(bo->dev->bo_handles,
-			    (void*)(uintptr_t)bo->handle, bo);
+	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);
 
-	*buf_handle = bo;
-	return 0;
+	if (r)
+		amdgpu_bo_free(bo);
+	else
+		*buf_handle = bo;
+
+	return r;
 }
 
 int amdgpu_bo_set_metadata(amdgpu_bo_handle bo,
@@ -297,8 +300,7 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
 		break;
 
 	case amdgpu_bo_handle_type_dma_buf_fd:
-		bo = util_hash_table_get(dev->bo_handles,
-					 (void*)(uintptr_t)shared_handle);
+		bo = handle_table_lockup(&dev->bo_handles, shared_handle);
 		break;
 
 	case amdgpu_bo_handle_type_kms:
@@ -381,7 +383,7 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
 	bo->dev = dev;
 	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
 
-	util_hash_table_set(dev->bo_handles, (void*)(uintptr_t)bo->handle, bo);
+	handle_table_insert(&dev->bo_handles, bo->handle, bo);
 	pthread_mutex_unlock(&dev->bo_table_mutex);
 
 	output->buf_handle = bo;
@@ -400,8 +402,7 @@ int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
 
 	if (update_references(&bo->refcount, NULL)) {
 		/* Remove the buffer from the hash tables. */
-		util_hash_table_remove(dev->bo_handles,
-					(void*)(uintptr_t)bo->handle);
+		handle_table_remove(&dev->bo_handles, bo->handle);
 
 		if (bo->flink_name) {
 			util_hash_table_remove(dev->bo_flink_names,
amdgpu/amdgpu_device.c +0 -2
index 38fd186d..6b07c064 100644
@@ -123,7 +123,6 @@ static void amdgpu_device_free_internal(amdgpu_device_handle dev)
 	amdgpu_vamgr_deinit(&dev->vamgr_high_32);
 	amdgpu_vamgr_deinit(&dev->vamgr_high);
 	util_hash_table_destroy(dev->bo_flink_names);
-	util_hash_table_destroy(dev->bo_handles);
 	pthread_mutex_destroy(&dev->bo_table_mutex);
 	free(dev->marketing_name);
 	free(dev);
@@ -230,7 +229,6 @@ int amdgpu_device_initialize(int fd,
 
 	dev->bo_flink_names = util_hash_table_create(handle_hash,
 						     handle_compare);
-	dev->bo_handles = util_hash_table_create(handle_hash, handle_compare);
 	pthread_mutex_init(&dev->bo_table_mutex, NULL);
 
 	/* Check if acceleration is working. */
amdgpu/amdgpu_internal.h +2 -1
index 83012cab..36ebc738 100644
@@ -32,6 +32,7 @@
 #include "xf86atomic.h"
 #include "amdgpu.h"
 #include "util_double_list.h"
+#include "handle_table.h"
 
 #define AMDGPU_CS_MAX_RINGS 8
 /* do not use below macro if b is not power of 2 aligned value */
@@ -73,7 +74,7 @@ struct amdgpu_device {
 
 	char *marketing_name;
 	/** List of buffer handles. Protected by bo_table_mutex. */
-	struct util_hash_table *bo_handles;
+	struct handle_table bo_handles;
 	/** List of buffer GEM flink names. Protected by bo_table_mutex. */
 	struct util_hash_table *bo_flink_names;
 	/** This protects all hash tables. */
Signature · 6 lines
2.14.1

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

PATCH 5/6 amdgpu: use handle table for flink names

In reply to a message not in the archive

Instead of the hash use the handle table.

---
 amdgpu/amdgpu_bo.c       | 26 +++++++++++++-------------
 amdgpu/amdgpu_device.c   | 14 --------------
 amdgpu/amdgpu_internal.h |  2 +-
 3 files changed, 14 insertions(+), 28 deletions(-)
amdgpu/amdgpu_bo.c +13 -13
index 9a7d0b29..14cccd9f 100644
@@ -37,7 +37,6 @@
 #include "xf86drm.h"
 #include "amdgpu_drm.h"
 #include "amdgpu_internal.h"
-#include "util_hash_table.h"
 #include "util_math.h"
 
 static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
@@ -216,12 +215,10 @@ static int amdgpu_bo_export_flink(amdgpu_bo_handle bo)
 	}
 
 	pthread_mutex_lock(&bo->dev->bo_table_mutex);
-	util_hash_table_set(bo->dev->bo_flink_names,
-			    (void*)(uintptr_t)bo->flink_name,
-			    bo);
+	r = handle_table_insert(&bo->dev->bo_flink_names, bo->flink_name, bo);
 	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
 
-	return 0;
+	return r;
 }
 
 int amdgpu_bo_export(amdgpu_bo_handle bo,
@@ -295,8 +292,7 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
 	/* If we have already created a buffer with this handle, find it. */
 	switch (type) {
 	case amdgpu_bo_handle_type_gem_flink_name:
-		bo = util_hash_table_get(dev->bo_flink_names,
-					 (void*)(uintptr_t)shared_handle);
+		bo = handle_table_lockup(&dev->bo_flink_names, shared_handle);
 		break;
 
 	case amdgpu_bo_handle_type_dma_buf_fd:
@@ -364,8 +360,13 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
 		}
 		bo->flink_name = shared_handle;
 		bo->alloc_size = open_arg.size;
-		util_hash_table_set(dev->bo_flink_names,
-				    (void*)(uintptr_t)bo->flink_name, bo);
+		r = handle_table_insert(&dev->bo_flink_names, shared_handle,
+					bo);
+		if (r) {
+			pthread_mutex_unlock(&dev->bo_table_mutex);
+			amdgpu_bo_free(bo);
+			return r;
+		}
 		break;
 
 	case amdgpu_bo_handle_type_dma_buf_fd:
@@ -404,10 +405,9 @@ int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
 		/* Remove the buffer from the hash tables. */
 		handle_table_remove(&dev->bo_handles, bo->handle);
 
-		if (bo->flink_name) {
-			util_hash_table_remove(dev->bo_flink_names,
-						(void*)(uintptr_t)bo->flink_name);
-		}
+		if (bo->flink_name)
+			handle_table_remove(&dev->bo_flink_names,
+					    bo->flink_name);
 
 		/* Release CPU access. */
 		if (bo->cpu_map_count > 0) {
amdgpu/amdgpu_device.c +0 -14
index 6b07c064..68ee19c9 100644
@@ -39,7 +39,6 @@
 #include "xf86drm.h"
 #include "amdgpu_drm.h"
 #include "amdgpu_internal.h"
-#include "util_hash_table.h"
 #include "util_math.h"
 
 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
@@ -47,16 +46,6 @@
 static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
 static amdgpu_device_handle fd_list;
 
-static unsigned handle_hash(void *key)
-{
-	return PTR_TO_UINT(key);
-}
-
-static int handle_compare(void *key1, void *key2)
-{
-	return PTR_TO_UINT(key1) != PTR_TO_UINT(key2);
-}
-
 static int fd_compare(int fd1, int fd2)
 {
 	char *name1 = drmGetPrimaryDeviceNameFromFd(fd1);
@@ -122,7 +111,6 @@ static void amdgpu_device_free_internal(amdgpu_device_handle dev)
 	amdgpu_vamgr_deinit(&dev->vamgr);
 	amdgpu_vamgr_deinit(&dev->vamgr_high_32);
 	amdgpu_vamgr_deinit(&dev->vamgr_high);
-	util_hash_table_destroy(dev->bo_flink_names);
 	pthread_mutex_destroy(&dev->bo_table_mutex);
 	free(dev->marketing_name);
 	free(dev);
@@ -227,8 +215,6 @@ int amdgpu_device_initialize(int fd,
 	dev->minor_version = version->version_minor;
 	drmFreeVersion(version);
 
-	dev->bo_flink_names = util_hash_table_create(handle_hash,
-						     handle_compare);
 	pthread_mutex_init(&dev->bo_table_mutex, NULL);
 
 	/* Check if acceleration is working. */
amdgpu/amdgpu_internal.h +1 -1
index 36ebc738..a340abbd 100644
@@ -76,7 +76,7 @@ struct amdgpu_device {
 	/** List of buffer handles. Protected by bo_table_mutex. */
 	struct handle_table bo_handles;
 	/** List of buffer GEM flink names. Protected by bo_table_mutex. */
-	struct util_hash_table *bo_flink_names;
+	struct handle_table bo_flink_names;
 	/** This protects all hash tables. */
 	pthread_mutex_t bo_table_mutex;
 	struct drm_amdgpu_info_device dev_info;
Signature · 6 lines
2.14.1

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

PATCH 6/6 amdgpu: remove the hash table implementation

In reply to a message not in the archive

Not used any more.

---
 amdgpu/Makefile.sources  |   4 -
 amdgpu/util_hash.c       | 383 -----------------------------------------------
 amdgpu/util_hash.h       | 103 -------------
 amdgpu/util_hash_table.c | 270 ---------------------------------
 amdgpu/util_hash_table.h |  71 ---------
 5 files changed, 831 deletions(-)
 delete mode 100644 amdgpu/util_hash.c
 delete mode 100644 amdgpu/util_hash.h
 delete mode 100644 amdgpu/util_hash_table.c
 delete mode 100644 amdgpu/util_hash_table.h
amdgpu/Makefile.sources +0 -4
index 62577ba5..d6df324a 100644
@@ -7,10 +7,6 @@ LIBDRM_AMDGPU_FILES := \
 	amdgpu_internal.h \
 	amdgpu_vamgr.c \
 	amdgpu_vm.c \
-	util_hash.c \
-	util_hash.h \
-	util_hash_table.c \
-	util_hash_table.h \
 	handle_table.c \
 	handle_table.h
 
amdgpu/util_hash.c deleted +0 -383

383 lines not on this page · raw message

amdgpu/util_hash.h deleted +0 -103

103 lines not on this page · raw message

amdgpu/util_hash_table.c deleted +0 -270

270 lines not on this page · raw message

amdgpu/util_hash_table.h deleted +0 -71

71 lines not on this page · raw message

Signature · 6 lines
2.14.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Reviewed-by
#11 lore raw
In reply to a message not in the archive
Quoted 3 lines · Christian König

This way we can always find a BO structure by its handle.

Typo in the shortlog: should be "lookup" instead of "lockup".

Also, this patch should really be after patch 4.

amdgpu/amdgpu_bo.c int amdgpu_bo_export(amdgpu_bo_handle bo, · line 242

Quoted 9 lines
amdgpu/amdgpu_bo.c +0 -2
@@ -240,14 +237,11 @@ int amdgpu_bo_export(amdgpu_bo_handle bo,
 		return 0;
 
 	case amdgpu_bo_handle_type_kms:
-		amdgpu_add_handle_to_table(bo);
-		/* fall through */
 	case amdgpu_bo_handle_type_kms_noimport:
 		*shared_handle = bo->handle;
 		return 0;

This is a bit unfortunate, amdgpu_bo_handle_type_kms_noimport only just landed this week for the 2.4.93 release, now it'll already be useless noise... A bit more coordination would have been nice. :)

Anyway, with the typo in patch 3 fixed, patches 2 & 4-6 are

Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
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): 2ede6f59-a1e6-fe9e-396f-2cc961db6df5-otUistvHUpPR7s880joybQ@public.gmane.org

#13 lore raw
Quoted 13 lines · Michel Dänzer
Quoted 3 lines · Christian König

This way we can always find a BO structure by its handle.

Typo in the shortlog: should be "lookup" instead of "lockup".

Also, this patch should really be after patch 4.

Quoted 3 lines
(quoted diff) +0 -0
@@ -240,14 +237,11 @@ int amdgpu_bo_export(amdgpu_bo_handle bo,
  		return 0;
 
Quoted 10 lines

amdgpu/amdgpu_bo.c int amdgpu_bo_export(amdgpu_bo_handle bo, · line 242

Quoted 6 lines
amdgpu/amdgpu_bo.c +0 -2
  	case amdgpu_bo_handle_type_kms:
-		amdgpu_add_handle_to_table(bo);
-		/* fall through */
  	case amdgpu_bo_handle_type_kms_noimport:
  		*shared_handle = bo->handle;
  		return 0;

This is a bit unfortunate, amdgpu_bo_handle_type_kms_noimport only just landed this week for the 2.4.93 release, now it'll already be useless noise... A bit more coordination would have been nice. :)

BTW, there is a schedule for that release?

I mean if amdgpu_bo_handle_type_kms_noimport is merged in the release at last minute. Generally we may wait for some verification or process for a release.
if the func has to go through a verification/process, that maybe not happen in a week.

Jerry

Quoted 7 lines

Anyway, with the typo in patch 3 fixed, patches 2 & 4-6 are

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