Skip to contentSkip to conversation

amdgpu:

PATCH In review Michel Dänzer 5 participants 15 messages Jun 24, 2019 – Jun 28, 2019 3 messages not in the archive

lore
Share & tools
lore
#0 lore raw

cover amdgpu:

From: Michel Dänzer <[email protected]>

The motivation for these patches is https://bugs.freedesktop.org/110903 .

Patches 1-3 are preparatory.

Patches 4 & 5 are the core patches allowing the issues discussed in the bug report to be fixed.

Patches 6-8 are further optimizations / cleanups.

Patch 9 is the Mesa patch making use of the new amdgpu_bo_handle_type_kms_user API to fix the user visible problem.

Note that the libdrm patches need to land first, a libdrm release needs to be made, and the Mesa patch needs to bump the libdrm_amdgpu version requirement to that release's version before it can land.

Michel Dänzer (9):
amdgpu: Pass file descriptor directly to amdgpu_close_kms_handle
amdgpu: Add BO handle to table in amdgpu_bo_create
amdgpu: Rename fd_mutex/list to dev_mutex/list
amdgpu: Add struct amdgpu_core_device and amdgpu_core_bo
amdgpu: Add amdgpu_bo_handle_type_kms_user
amdgpu: Re-use an existing amdgpu_device when possible
amdgpu: Use normal integers for device / core BO reference counting
amdgpu: Drop refcount member from struct amdgpu_core_bo/device winsys/amdgpu: Use amdgpu_bo_handle_type_kms_user for API KMS handles

 amdgpu/amdgpu.h               |  14 +-
 amdgpu/amdgpu_asic_id.c       |   4 +-
 amdgpu/amdgpu_bo.c            | 367 ++++++++++++++++++++++------------
 amdgpu/amdgpu_cs.c            |  64 +++---
 amdgpu/amdgpu_device.c        | 281 +++++++++++++-------------
 amdgpu/amdgpu_gpu_info.c      |  35 ++--
 amdgpu/amdgpu_internal.h      |  31 ++-
 amdgpu/amdgpu_vamgr.c         |   9 +-
 amdgpu/amdgpu_vm.c            |   4 +-
 tests/amdgpu/amdgpu_test.c    |   2 +-
 tests/amdgpu/bo_tests.c       |   2 +-
 tests/amdgpu/cs_tests.c       |   8 +-
 tests/amdgpu/deadlock_tests.c |   8 +-
 tests/amdgpu/uvd_enc_tests.c  |   2 +-
 tests/amdgpu/vce_tests.c      |  12 +-
 tests/amdgpu/vcn_tests.c      |   4 +-
 tests/amdgpu/vm_tests.c       |   2 +-
 17 files changed, 500 insertions(+), 349 deletions(-)
Signature · 6 lines
2.20.1

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

A message that is not in the archive (sent off-list or not yet received): 20190624165406.13682-1-michel-otUistvHUpPR7s880joybQ@public.gmane.org

#2 lore raw

PATCH 1/9 amdgpu: Pass file descriptor directly to amdgpu_close_kms_handle

From: Michel Dänzer <[email protected]>

And propagate drmIoctl's return value.

This allows replacing all remaining open-coded DRM_IOCTL_GEM_CLOSE ioctl calls with amdgpu_close_kms_handle calls.

---
 amdgpu/amdgpu_bo.c | 35 +++++++++++++++--------------------
 1 file changed, 15 insertions(+), 20 deletions(-)
amdgpu/amdgpu_bo.c +15 -20
index 32ee358f..6c0b8517 100644
@@ -39,13 +39,12 @@
 #include "amdgpu_internal.h"
 #include "util_math.h"
 
-static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
-				     uint32_t handle)
+static int amdgpu_close_kms_handle(int fd, uint32_t handle)
 {
 	struct drm_gem_close args = {};
 
 	args.handle = handle;
-	drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &args);
+	return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &args);
 }
 
 static int amdgpu_bo_create(amdgpu_device_handle dev,
@@ -93,7 +92,7 @@ drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev,
 	r = amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
 			     buf_handle);
 	if (r) {
-		amdgpu_close_kms_handle(dev, args.out.handle);
+		amdgpu_close_kms_handle(dev->fd, args.out.handle);
 		goto out;
 	}
 
@@ -214,11 +213,8 @@ static int amdgpu_bo_export_flink(amdgpu_bo_handle bo)
 
 	bo->flink_name = flink.name;
 
-	if (bo->dev->flink_fd != bo->dev->fd) {
-		struct drm_gem_close args = {};
-		args.handle = handle;
-		drmIoctl(bo->dev->flink_fd, DRM_IOCTL_GEM_CLOSE, &args);
-	}
+	if (bo->dev->flink_fd != bo->dev->fd)
+		amdgpu_close_kms_handle(bo->dev->flink_fd, handle);
 
 	pthread_mutex_lock(&bo->dev->bo_table_mutex);
 	r = handle_table_insert(&bo->dev->bo_flink_names, bo->flink_name, bo);
@@ -261,7 +257,6 @@ drm_public int amdgpu_bo_import(amdgpu_device_handle dev,
 		     struct amdgpu_bo_import_result *output)
 {
 	struct drm_gem_open open_arg = {};
-	struct drm_gem_close close_arg = {};
 	struct amdgpu_bo *bo = NULL;
 	uint32_t handle = 0, flink_name = 0;
 	uint64_t alloc_size = 0;
@@ -345,12 +340,12 @@ drm_public int amdgpu_bo_import(amdgpu_device_handle dev,
 			close(dma_fd);
 			if (r)
 				goto free_bo_handle;
-			close_arg.handle = open_arg.handle;
-			r = drmIoctl(dev->flink_fd, DRM_IOCTL_GEM_CLOSE,
-				     &close_arg);
+			r = amdgpu_close_kms_handle(dev->flink_fd,
+						    open_arg.handle);
 			if (r)
 				goto free_bo_handle;
 		}
+		open_arg.handle = 0;
 		break;
 
 	case amdgpu_bo_handle_type_dma_buf_fd:
@@ -388,14 +383,13 @@ drm_public int amdgpu_bo_import(amdgpu_device_handle dev,
 remove_handle:
 	handle_table_remove(&dev->bo_handles, bo->handle);
 free_bo_handle:
-	if (flink_name && !close_arg.handle && open_arg.handle) {
-		close_arg.handle = open_arg.handle;
-		drmIoctl(dev->flink_fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
-	}
+	if (flink_name && open_arg.handle)
+		amdgpu_close_kms_handle(dev->flink_fd, open_arg.handle);
+
 	if (bo)
 		amdgpu_bo_free(bo);
 	else
-		amdgpu_close_kms_handle(dev, handle);
+		amdgpu_close_kms_handle(dev->fd, handle);
 unlock:
 	pthread_mutex_unlock(&dev->bo_table_mutex);
 	return r;
@@ -424,12 +418,13 @@ drm_public int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
 			amdgpu_bo_cpu_unmap(bo);
 		}
 
-		amdgpu_close_kms_handle(dev, bo->handle);
+		amdgpu_close_kms_handle(dev->fd, bo->handle);
 		pthread_mutex_destroy(&bo->cpu_access_mutex);
 		free(bo);
 	}
 
 	pthread_mutex_unlock(&dev->bo_table_mutex);
+
 	return 0;
 }
 
@@ -604,7 +599,7 @@ drm_public int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
 
 	r = amdgpu_bo_create(dev, size, args.handle, buf_handle);
 	if (r) {
-		amdgpu_close_kms_handle(dev, args.handle);
+		amdgpu_close_kms_handle(dev->fd, args.handle);
 		goto out;
 	}
 
Signature · 6 lines
2.20.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Reviewed-by on v1 1/9
#3 lore raw

Re: [PATCH libdrm 1/9] amdgpu: Pass file descriptor directly to amdgpu_close_kms_handle

Quoted 5 lines · Michel Dänzer

From: Michel Dänzer <[email protected]>

And propagate drmIoctl's return value.

Quoted 8 lines

This allows replacing all remaining open-coded DRM_IOCTL_GEM_CLOSE ioctl calls with amdgpu_close_kms_handle calls.

---
 amdgpu/amdgpu_bo.c | 35 +++++++++++++++--------------------
 1 file changed, 15 insertions(+), 20 deletions(-)

Fwiw patches 1-3 are:

-Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw

PATCH 2/9 amdgpu: Add BO handle to table in amdgpu_bo_create

In reply to a message not in the archive

From: Michel Dänzer <[email protected]>

Simplifies its callers.

dev->bo_table_mutex is now always held when amdgpu_bo_create is called (this was already the case in amdgpu_bo_import).

---
 amdgpu/amdgpu_bo.c | 32 ++++++++++++--------------------
 1 file changed, 12 insertions(+), 20 deletions(-)
amdgpu/amdgpu_bo.c +12 -20
index 6c0b8517..5bdb8fe8 100644
@@ -53,11 +53,18 @@ static int amdgpu_bo_create(amdgpu_device_handle dev,
 			    amdgpu_bo_handle *buf_handle)
 {
 	struct amdgpu_bo *bo;
+	int r;
 
 	bo = calloc(1, sizeof(struct amdgpu_bo));
 	if (!bo)
 		return -ENOMEM;
 
+	r = handle_table_insert(&dev->bo_handles, handle, bo);
+	if (r) {
+		free(bo);
+		return r;
+	}
+
 	atomic_set(&bo->refcount, 1);
 	bo->dev = dev;
 	bo->alloc_size = size;
@@ -89,19 +96,14 @@ drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev,
 	if (r)
 		goto out;
 
+	pthread_mutex_lock(&dev->bo_table_mutex);
 	r = amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
 			     buf_handle);
+	pthread_mutex_unlock(&dev->bo_table_mutex);
 	if (r) {
 		amdgpu_close_kms_handle(dev->fd, args.out.handle);
-		goto out;
 	}
 
-	pthread_mutex_lock(&dev->bo_table_mutex);
-	r = handle_table_insert(&dev->bo_handles, (*buf_handle)->handle,
-				*buf_handle);
-	pthread_mutex_unlock(&dev->bo_table_mutex);
-	if (r)
-		amdgpu_bo_free(*buf_handle);
 out:
 	return r;
 }
@@ -363,15 +365,12 @@ drm_public int amdgpu_bo_import(amdgpu_device_handle dev,
 	if (r)
 		goto free_bo_handle;
 
-	r = handle_table_insert(&dev->bo_handles, bo->handle, bo);
-	if (r)
-		goto free_bo_handle;
 	if (flink_name) {
 		bo->flink_name = flink_name;
 		r = handle_table_insert(&dev->bo_flink_names, flink_name,
 					bo);
 		if (r)
-			goto remove_handle;
+			goto free_bo_handle;
 
 	}
 
@@ -380,8 +379,6 @@ drm_public int amdgpu_bo_import(amdgpu_device_handle dev,
 	pthread_mutex_unlock(&dev->bo_table_mutex);
 	return 0;
 
-remove_handle:
-	handle_table_remove(&dev->bo_handles, bo->handle);
 free_bo_handle:
 	if (flink_name && open_arg.handle)
 		amdgpu_close_kms_handle(dev->flink_fd, open_arg.handle);
@@ -597,18 +594,13 @@ drm_public int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
 	if (r)
 		goto out;
 
+	pthread_mutex_lock(&dev->bo_table_mutex);
 	r = amdgpu_bo_create(dev, size, args.handle, buf_handle);
+	pthread_mutex_unlock(&dev->bo_table_mutex);
 	if (r) {
 		amdgpu_close_kms_handle(dev->fd, args.handle);
-		goto out;
 	}
 
-	pthread_mutex_lock(&dev->bo_table_mutex);
-	r = handle_table_insert(&dev->bo_handles, (*buf_handle)->handle,
-				*buf_handle);
-	pthread_mutex_unlock(&dev->bo_table_mutex);
-	if (r)
-		amdgpu_bo_free(*buf_handle);
 out:
 	return r;
 }
Signature · 6 lines
2.20.1

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

PATCH 3/9 amdgpu: Rename fd_mutex/list to dev_mutex/list

In reply to a message not in the archive

From: Michel Dänzer <[email protected]>

Seems to better reflect what they're for.

---
 amdgpu/amdgpu_device.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
amdgpu/amdgpu_device.c +15 -15
index 362494b1..76b4e5eb 100644
@@ -43,8 +43,8 @@
 
 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
 
-static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
-static amdgpu_device_handle fd_list;
+static pthread_mutex_t dev_mutex = PTHREAD_MUTEX_INITIALIZER;
+static amdgpu_device_handle dev_list;
 
 static int fd_compare(int fd1, int fd2)
 {
@@ -95,13 +95,13 @@ 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;
+	amdgpu_device_handle *node = &dev_list;
 
-	pthread_mutex_lock(&fd_mutex);
+	pthread_mutex_lock(&dev_mutex);
 	while (*node != dev && (*node)->next)
 		node = &(*node)->next;
 	*node = (*node)->next;
-	pthread_mutex_unlock(&fd_mutex);
+	pthread_mutex_unlock(&dev_mutex);
 
 	close(dev->fd);
 	if ((dev->flink_fd >= 0) && (dev->fd != dev->flink_fd))
@@ -155,16 +155,16 @@ drm_public int amdgpu_device_initialize(int fd,
 
 	*device_handle = NULL;
 
-	pthread_mutex_lock(&fd_mutex);
+	pthread_mutex_lock(&dev_mutex);
 	r = amdgpu_get_auth(fd, &flag_auth);
 	if (r) {
 		fprintf(stderr, "%s: amdgpu_get_auth (1) failed (%i)\n",
 			__func__, r);
-		pthread_mutex_unlock(&fd_mutex);
+		pthread_mutex_unlock(&dev_mutex);
 		return r;
 	}
 
-	for (dev = fd_list; dev; dev = dev->next)
+	for (dev = dev_list; dev; dev = dev->next)
 		if (fd_compare(dev->fd, fd) == 0)
 			break;
 
@@ -173,7 +173,7 @@ drm_public int amdgpu_device_initialize(int fd,
 		if (r) {
 			fprintf(stderr, "%s: amdgpu_get_auth (2) failed (%i)\n",
 				__func__, r);
-			pthread_mutex_unlock(&fd_mutex);
+			pthread_mutex_unlock(&dev_mutex);
 			return r;
 		}
 		if ((flag_auth) && (!flag_authexist)) {
@@ -182,14 +182,14 @@ drm_public int amdgpu_device_initialize(int fd,
 		*major_version = dev->major_version;
 		*minor_version = dev->minor_version;
 		amdgpu_device_reference(device_handle, dev);
-		pthread_mutex_unlock(&fd_mutex);
+		pthread_mutex_unlock(&dev_mutex);
 		return 0;
 	}
 
 	dev = calloc(1, sizeof(struct amdgpu_device));
 	if (!dev) {
 		fprintf(stderr, "%s: calloc failed\n", __func__);
-		pthread_mutex_unlock(&fd_mutex);
+		pthread_mutex_unlock(&dev_mutex);
 		return -ENOMEM;
 	}
 
@@ -265,9 +265,9 @@ drm_public int amdgpu_device_initialize(int fd,
 	*major_version = dev->major_version;
 	*minor_version = dev->minor_version;
 	*device_handle = dev;
-	dev->next = fd_list;
-	fd_list = dev;
-	pthread_mutex_unlock(&fd_mutex);
+	dev->next = dev_list;
+	dev_list = dev;
+	pthread_mutex_unlock(&dev_mutex);
 
 	return 0;
 
@@ -275,7 +275,7 @@ cleanup:
 	if (dev->fd >= 0)
 		close(dev->fd);
 	free(dev);
-	pthread_mutex_unlock(&fd_mutex);
+	pthread_mutex_unlock(&dev_mutex);
 	return r;
 }
 
Signature · 6 lines
2.20.1

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

PATCH 8/9 amdgpu: Drop refcount member from struct amdgpu_core_bo/device

In reply to a message not in the archive

From: Michel Dänzer <[email protected]>

We can just walk the lists of struct amdgpu_bo/device to find out if there are any references left.

---
 amdgpu/amdgpu_bo.c       | 14 +++++++++++---
 amdgpu/amdgpu_device.c   | 30 ++++++++++++++++++++++--------
 amdgpu/amdgpu_internal.h |  2 --
 3 files changed, 33 insertions(+), 13 deletions(-)
amdgpu/amdgpu_bo.c +11 -3
index f40df484..332ce4be 100644
@@ -65,7 +65,6 @@ static int amdgpu_core_bo_create(struct amdgpu_core_device *dev,
 		return r;
 	}
 
-	bo->refcount = 1;
 	bo->alloc_size = size;
 	bo->handle = handle;
 	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
@@ -95,7 +94,6 @@ static int amdgpu_bo_create(amdgpu_device_handle user_dev,
 				goto out;
 			}
 		}
-		bo->refcount++;
 	} else {
 		r = amdgpu_core_bo_create(dev, size, handle, &bo);
 		if (r)
@@ -466,8 +464,18 @@ static void amdgpu_core_bo_free(struct amdgpu_bo *user_bo)
 {
 	struct amdgpu_core_device *dev = user_bo->dev->core;
 	struct amdgpu_core_bo *bo = user_bo->core;
+	struct amdgpu_bo *iter_bo;
 
-	if (--bo->refcount == 0) {
+	/* Are there any other user BOs left referencing the same core BO? */
+	for (iter_bo = bo->user_bos; iter_bo; iter_bo = iter_bo->next) {
+		if (iter_bo == user_bo)
+			continue;
+
+		if (iter_bo->core == bo)
+			break;
+	}
+
+	if (!iter_bo) {
 		/* Remove the buffer from the hash tables. */
 		handle_table_remove(&dev->bo_handles, bo->handle);
 
amdgpu/amdgpu_device.c +22 -8
index 8a99b8bb..1709099e 100644
@@ -72,9 +72,6 @@ static int fd_compare(int fd1, int fd2)
 
 static void amdgpu_device_free(struct amdgpu_core_device *dev)
 {
-	if (--dev->refcount > 0)
-		return;
-
 	close(dev->fd);
 
 	amdgpu_vamgr_deinit(&dev->vamgr_32);
@@ -102,6 +99,26 @@ static bool same_file_description(int fd1, int fd2)
 	return fd1 == fd2;
 }
 
+static void amdgpu_device_unreference(amdgpu_device_handle user_dev)
+{
+	struct amdgpu_device *dev;
+
+	if (!user_dev->core)
+		return;
+
+	/* Are there any other user devices left referencing the core device? */
+	for (dev = dev_list; dev; dev = dev->next) {
+		if (dev == user_dev)
+			continue;
+
+		if (dev->core == user_dev->core)
+			break;
+	}
+
+	if (!dev)
+		amdgpu_device_free(user_dev->core);
+}
+
 static int amdgpu_device_init(amdgpu_device_handle user_dev)
 {
 	struct amdgpu_device *dev_iter;
@@ -115,7 +132,6 @@ static int amdgpu_device_init(amdgpu_device_handle user_dev)
 			break;
 
 	if (dev_iter) {
-		dev_iter->core->refcount++;
 		user_dev->core = dev_iter->core;
 		return 0;
 	}
@@ -126,7 +142,6 @@ static int amdgpu_device_init(amdgpu_device_handle user_dev)
 		return -ENOMEM;
 	}
 
-	dev->refcount = 1;
 	pthread_mutex_init(&dev->bo_table_mutex, NULL);
 
 	dev->fd = user_dev->user_fd;
@@ -251,8 +266,7 @@ out:
 cleanup:
 	if (!user_dev->core || user_dev->user_fd != user_dev->core->fd)
 		close(user_dev->user_fd);
-	if (user_dev->core)
-		amdgpu_device_free(user_dev->core);
+	amdgpu_device_unreference(user_dev);
 	free(user_dev);
 	pthread_mutex_unlock(&dev_mutex);
 	return r;
@@ -274,7 +288,7 @@ drm_public int amdgpu_device_deinitialize(amdgpu_device_handle user_dev)
 		if (user_dev->user_fd != dev->fd)
 			close(user_dev->user_fd);
 
-		amdgpu_device_free(dev);
+		amdgpu_device_unreference(user_dev);
 
 		free(user_dev);
 	}
amdgpu/amdgpu_internal.h +0 -2
index 70566f98..7110e30c 100644
@@ -65,7 +65,6 @@ struct amdgpu_va {
 };
 
 struct amdgpu_core_device {
-	int refcount;
 	int fd;
 	unsigned major_version;
 	unsigned minor_version;
@@ -97,7 +96,6 @@ struct amdgpu_device {
 };
 
 struct amdgpu_core_bo {
-	int refcount;
 	amdgpu_bo_handle user_bos;
 
 	uint64_t alloc_size;
Signature · 6 lines
2.20.1

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

PATCH 9/9 winsys/amdgpu: Use amdgpu_bo_handle_type_kms_user for API KMS handles

In reply to a message not in the archive

From: Michel Dänzer <[email protected]>

Gallium API callers expect the returned handles to be valid for the DRM file descriptor passed to the driver during initialization, which may not be the case with amdgpu_bo_handle_type_kms.

Bugzilla: https://bugs.freedesktop.org/110903

---
 src/gallium/winsys/amdgpu/drm/amdgpu_bo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/gallium/winsys/amdgpu/drm/amdgpu_bo.c +1 -1
index 37098ab305f..d4f7e1c7a95 100644
@@ -1544,7 +1544,7 @@ static bool amdgpu_bo_get_handle(struct pb_buffer *buffer,
       type = amdgpu_bo_handle_type_dma_buf_fd;
       break;
    case WINSYS_HANDLE_TYPE_KMS:
-      type = amdgpu_bo_handle_type_kms;
+      type = amdgpu_bo_handle_type_kms_user;
       break;
    default:
       return false;
Signature · 6 lines
2.20.1

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

PATCH 4/9 amdgpu: Add struct amdgpu_core_device and amdgpu_core_bo

In reply to Michel Dänzer

From: Michel Dänzer <[email protected]>

They can be referenced by any number of struct amdgpu_device/bo, which are used for amdgpu_device/bo_handle in the public API.

This allows keeping track of the DRM file descriptor passed to amdgpu_device_initialize and the one used for CS submission etc. separately. The core structs hold the information relevant for the latter.

Because we now always keep a duplicate of the file descriptor passed to amdgpu_device_initialize, we can use that for flink, and we no longer need to check its authentication status (flink could never be expected to work after passing an unauthenticated file descriptor to amdgpu_device_initialize).

---
 amdgpu/amdgpu_asic_id.c       |   4 +-
 amdgpu/amdgpu_bo.c            | 251 ++++++++++++++++++++++------------
 amdgpu/amdgpu_cs.c            |  64 +++++----
 amdgpu/amdgpu_device.c        | 219 ++++++++++++-----------------
 amdgpu/amdgpu_gpu_info.c      |  35 ++---
 amdgpu/amdgpu_internal.h      |  27 ++--
 amdgpu/amdgpu_vamgr.c         |   9 +-
 amdgpu/amdgpu_vm.c            |   4 +-
 tests/amdgpu/amdgpu_test.c    |   2 +-
 tests/amdgpu/bo_tests.c       |   2 +-
 tests/amdgpu/cs_tests.c       |   8 +-
 tests/amdgpu/deadlock_tests.c |   8 +-
 tests/amdgpu/uvd_enc_tests.c  |   2 +-
 tests/amdgpu/vce_tests.c      |  12 +-
 tests/amdgpu/vcn_tests.c      |   4 +-
 tests/amdgpu/vm_tests.c       |   2 +-
 16 files changed, 360 insertions(+), 293 deletions(-)
amdgpu/amdgpu_asic_id.c +2 -2
index a5007ffc..356c8a59 100644
@@ -34,7 +34,7 @@
 #include "amdgpu_drm.h"
 #include "amdgpu_internal.h"
 
-static int parse_one_line(struct amdgpu_device *dev, const char *line)
+static int parse_one_line(struct amdgpu_core_device *dev, const char *line)
 {
 	char *buf, *saveptr;
 	char *s_did;
@@ -104,7 +104,7 @@ out:
 	return r;
 }
 
-void amdgpu_parse_asic_ids(struct amdgpu_device *dev)
+void amdgpu_parse_asic_ids(struct amdgpu_core_device *dev)
 {
 	FILE *fp;
 	char *line = NULL;
amdgpu/amdgpu_bo.c +167 -84

578 lines not on this page · raw message

amdgpu/amdgpu_cs.c +36 -28

244 lines not on this page · raw message

amdgpu/amdgpu_device.c +90 -129

298 lines not on this page · raw message

amdgpu/amdgpu_gpu_info.c +18 -17
index 777087f2..7253fbea 100644
@@ -40,7 +40,7 @@ drm_public int amdgpu_query_info(amdgpu_device_handle dev, unsigned info_id,
 	request.return_size = size;
 	request.query = info_id;
 
-	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
+	return drmCommandWrite(dev->core->fd, DRM_AMDGPU_INFO, &request,
 			       sizeof(struct drm_amdgpu_info));
 }
 
@@ -55,7 +55,7 @@ drm_public int amdgpu_query_crtc_from_id(amdgpu_device_handle dev, unsigned id,
 	request.query = AMDGPU_INFO_CRTC_FROM_ID;
 	request.mode_crtc.id = id;
 
-	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
+	return drmCommandWrite(dev->core->fd, DRM_AMDGPU_INFO, &request,
 			       sizeof(struct drm_amdgpu_info));
 }
 
@@ -74,7 +74,7 @@ drm_public int amdgpu_read_mm_registers(amdgpu_device_handle dev,
 	request.read_mmr_reg.instance = instance;
 	request.read_mmr_reg.flags = flags;
 
-	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
+	return drmCommandWrite(dev->core->fd, DRM_AMDGPU_INFO, &request,
 			       sizeof(struct drm_amdgpu_info));
 }
 
@@ -90,7 +90,7 @@ drm_public int amdgpu_query_hw_ip_count(amdgpu_device_handle dev,
 	request.query = AMDGPU_INFO_HW_IP_COUNT;
 	request.query_hw_ip.type = type;
 
-	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
+	return drmCommandWrite(dev->core->fd, DRM_AMDGPU_INFO, &request,
 			       sizeof(struct drm_amdgpu_info));
 }
 
@@ -107,7 +107,7 @@ drm_public int amdgpu_query_hw_ip_info(amdgpu_device_handle dev, unsigned type,
 	request.query_hw_ip.type = type;
 	request.query_hw_ip.ip_instance = ip_instance;
 
-	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
+	return drmCommandWrite(dev->core->fd, DRM_AMDGPU_INFO, &request,
 			       sizeof(struct drm_amdgpu_info));
 }
 
@@ -127,7 +127,7 @@ drm_public int amdgpu_query_firmware_version(amdgpu_device_handle dev,
 	request.query_fw.ip_instance = ip_instance;
 	request.query_fw.index = index;
 
-	r = drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
+	r = drmCommandWrite(dev->core->fd, DRM_AMDGPU_INFO, &request,
 			    sizeof(struct drm_amdgpu_info));
 	if (r)
 		return r;
@@ -137,11 +137,12 @@ drm_public int amdgpu_query_firmware_version(amdgpu_device_handle dev,
 	return 0;
 }
 
-drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev)
+drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle user_dev)
 {
+	struct amdgpu_core_device *dev = user_dev->core;
 	int r, i;
 
-	r = amdgpu_query_info(dev, AMDGPU_INFO_DEV_INFO, sizeof(dev->dev_info),
+	r = amdgpu_query_info(user_dev, AMDGPU_INFO_DEV_INFO, sizeof(dev->dev_info),
 			      &dev->dev_info);
 	if (r)
 		return r;
@@ -172,7 +173,7 @@ drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev)
 					    (AMDGPU_INFO_MMR_SH_INDEX_MASK <<
 					     AMDGPU_INFO_MMR_SH_INDEX_SHIFT);
 
-			r = amdgpu_read_mm_registers(dev, 0x263d, 1, instance, 0,
+			r = amdgpu_read_mm_registers(user_dev, 0x263d, 1, instance, 0,
 						     &dev->info.backend_disable[i]);
 			if (r)
 				return r;
@@ -180,13 +181,13 @@ drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev)
 			dev->info.backend_disable[i] =
 				(dev->info.backend_disable[i] >> 16) & 0xff;
 
-			r = amdgpu_read_mm_registers(dev, 0xa0d4, 1, instance, 0,
+			r = amdgpu_read_mm_registers(user_dev, 0xa0d4, 1, instance, 0,
 						     &dev->info.pa_sc_raster_cfg[i]);
 			if (r)
 				return r;
 
 			if (dev->info.family_id >= AMDGPU_FAMILY_CI) {
-				r = amdgpu_read_mm_registers(dev, 0xa0d5, 1, instance, 0,
+				r = amdgpu_read_mm_registers(user_dev, 0xa0d5, 1, instance, 0,
 						     &dev->info.pa_sc_raster_cfg1[i]);
 				if (r)
 					return r;
@@ -194,25 +195,25 @@ drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev)
 		}
 	}
 
-	r = amdgpu_read_mm_registers(dev, 0x263e, 1, 0xffffffff, 0,
+	r = amdgpu_read_mm_registers(user_dev, 0x263e, 1, 0xffffffff, 0,
 					     &dev->info.gb_addr_cfg);
 	if (r)
 		return r;
 
 	if (dev->info.family_id < AMDGPU_FAMILY_AI) {
-		r = amdgpu_read_mm_registers(dev, 0x2644, 32, 0xffffffff, 0,
+		r = amdgpu_read_mm_registers(user_dev, 0x2644, 32, 0xffffffff, 0,
 					     dev->info.gb_tile_mode);
 		if (r)
 			return r;
 
 		if (dev->info.family_id >= AMDGPU_FAMILY_CI) {
-			r = amdgpu_read_mm_registers(dev, 0x2664, 16, 0xffffffff, 0,
+			r = amdgpu_read_mm_registers(user_dev, 0x2664, 16, 0xffffffff, 0,
 						     dev->info.gb_macro_tile_mode);
 			if (r)
 				return r;
 		}
 
-		r = amdgpu_read_mm_registers(dev, 0x9d8, 1, 0xffffffff, 0,
+		r = amdgpu_read_mm_registers(user_dev, 0x9d8, 1, 0xffffffff, 0,
 					     &dev->info.mc_arb_ramcfg);
 		if (r)
 			return r;
@@ -235,7 +236,7 @@ drm_public int amdgpu_query_gpu_info(amdgpu_device_handle dev,
 		return -EINVAL;
 
 	/* Get ASIC info*/
-	*info = dev->info;
+	*info = dev->core->info;
 
 	return 0;
 }
@@ -328,6 +329,6 @@ drm_public int amdgpu_query_sensor_info(amdgpu_device_handle dev, unsigned senso
 	request.query = AMDGPU_INFO_SENSOR;
 	request.sensor_info.type = sensor_type;
 
-	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
+	return drmCommandWrite(dev->core->fd, DRM_AMDGPU_INFO, &request,
 			       sizeof(struct drm_amdgpu_info));
 }
amdgpu/amdgpu_internal.h +19 -8
index a340abbd..3a2ab74c 100644
@@ -64,14 +64,13 @@ struct amdgpu_va {
 	struct amdgpu_bo_va_mgr *vamgr;
 };
 
-struct amdgpu_device {
+struct amdgpu_core_device {
 	atomic_t refcount;
-	struct amdgpu_device *next;
 	int fd;
-	int flink_fd;
 	unsigned major_version;
 	unsigned minor_version;
 
+	struct amdgpu_core_device *next;
 	char *marketing_name;
 	/** List of buffer handles. Protected by bo_table_mutex. */
 	struct handle_table bo_handles;
@@ -91,9 +90,14 @@ struct amdgpu_device {
 	struct amdgpu_bo_va_mgr vamgr_high_32;
 };
 
-struct amdgpu_bo {
+struct amdgpu_device {
+	int user_fd;
+	struct amdgpu_core_device *core;
+};
+
+struct amdgpu_core_bo {
 	atomic_t refcount;
-	struct amdgpu_device *dev;
+	amdgpu_bo_handle user_bos;
 
 	uint64_t alloc_size;
 
@@ -105,14 +109,21 @@ struct amdgpu_bo {
 	int cpu_map_count;
 };
 
-struct amdgpu_bo_list {
+struct amdgpu_bo {
+	atomic_t refcount;
+	struct amdgpu_bo *next;
+	struct amdgpu_core_bo *core;
 	struct amdgpu_device *dev;
+};
+
+struct amdgpu_bo_list {
+	struct amdgpu_core_device *dev;
 
 	uint32_t handle;
 };
 
 struct amdgpu_context {
-	struct amdgpu_device *dev;
+	struct amdgpu_core_device *dev;
 	/** Mutex for accessing fences and to maintain command submissions
 	    in good sequence. */
 	pthread_mutex_t sequence_mutex;
@@ -141,7 +152,7 @@ drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
 
 drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
 
-drm_private void amdgpu_parse_asic_ids(struct amdgpu_device *dev);
+drm_private void amdgpu_parse_asic_ids(struct amdgpu_core_device *dev);
 
 drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
 
amdgpu/amdgpu_vamgr.c +6 -3
index d25d4216..560b84e7 100644
@@ -29,10 +29,12 @@
 #include "amdgpu_internal.h"
 #include "util_math.h"
 
-drm_public int amdgpu_va_range_query(amdgpu_device_handle dev,
+drm_public int amdgpu_va_range_query(amdgpu_device_handle user_dev,
 				     enum amdgpu_gpu_va_range type,
 				     uint64_t *start, uint64_t *end)
 {
+	struct amdgpu_core_device *dev = user_dev->core;
+
 	if (type != amdgpu_gpu_va_range_general)
 		return -EINVAL;
 
@@ -186,7 +188,7 @@ out:
 	pthread_mutex_unlock(&mgr->bo_va_mutex);
 }
 
-drm_public int amdgpu_va_range_alloc(amdgpu_device_handle dev,
+drm_public int amdgpu_va_range_alloc(amdgpu_device_handle user_dev,
 				     enum amdgpu_gpu_va_range va_range_type,
 				     uint64_t size,
 				     uint64_t va_base_alignment,
@@ -195,6 +197,7 @@ drm_public int amdgpu_va_range_alloc(amdgpu_device_handle dev,
 				     amdgpu_va_handle *va_range_handle,
 				     uint64_t flags)
 {
+	struct amdgpu_core_device *dev = user_dev->core;
 	struct amdgpu_bo_va_mgr *vamgr;
 
 	/* Clear the flag when the high VA manager is not initialized */
@@ -237,7 +240,7 @@ drm_public int amdgpu_va_range_alloc(amdgpu_device_handle dev,
 			amdgpu_vamgr_free_va(vamgr, *va_base_allocated, size);
 			return -ENOMEM;
 		}
-		va->dev = dev;
+		va->dev = user_dev;
 		va->address = *va_base_allocated;
 		va->size = size;
 		va->range = va_range_type;
amdgpu/amdgpu_vm.c +2 -2
index 7e6e28f0..de44e6c2 100644
@@ -33,7 +33,7 @@ drm_public int amdgpu_vm_reserve_vmid(amdgpu_device_handle dev, uint32_t flags)
 	vm.in.op = AMDGPU_VM_OP_RESERVE_VMID;
 	vm.in.flags = flags;
 
-	return drmCommandWriteRead(dev->fd, DRM_AMDGPU_VM,
+	return drmCommandWriteRead(dev->core->fd, DRM_AMDGPU_VM,
 				   &vm, sizeof(vm));
 }
 
@@ -45,6 +45,6 @@ drm_public int amdgpu_vm_unreserve_vmid(amdgpu_device_handle dev,
 	vm.in.op = AMDGPU_VM_OP_UNRESERVE_VMID;
 	vm.in.flags = flags;
 
-	return drmCommandWriteRead(dev->fd, DRM_AMDGPU_VM,
+	return drmCommandWriteRead(dev->core->fd, DRM_AMDGPU_VM,
 				   &vm, sizeof(vm));
 }
tests/amdgpu/amdgpu_test.c +1 -1
index 73403fb4..7095c4e4 100644
@@ -428,7 +428,7 @@ static void amdgpu_disable_suites()
 				   &minor_version, &device_handle))
 		return;
 
-	family_id = device_handle->info.family_id;
+	family_id = device_handle->core->info.family_id;
 
 	if (amdgpu_device_deinitialize(device_handle))
 		return;
tests/amdgpu/bo_tests.c +1 -1
index 7cff4cf7..d89c944d 100644
@@ -309,7 +309,7 @@ static void amdgpu_bo_find_by_cpu_mapping(void)
 					  &offset);
 	CU_ASSERT_EQUAL(r, 0);
 	CU_ASSERT_EQUAL(offset, 0);
-	CU_ASSERT_EQUAL(bo_handle->handle, find_bo_handle->handle);
+	CU_ASSERT_EQUAL(bo_handle->core->handle, find_bo_handle->core->handle);
 
 	atomic_dec(&find_bo_handle->refcount, 1);
 	r = amdgpu_bo_unmap_and_free(bo_handle, va_handle,
tests/amdgpu/cs_tests.c +4 -4
index 7ad0f0dc..8a5f6ed3 100644
@@ -68,7 +68,7 @@ CU_BOOL suite_cs_tests_enable(void)
 					     &minor_version, &device_handle))
 		return CU_FALSE;
 
-	family_id = device_handle->info.family_id;
+	family_id = device_handle->core->info.family_id;
 
 	if (amdgpu_device_deinitialize(device_handle))
 		return CU_FALSE;
@@ -101,10 +101,10 @@ int suite_cs_tests_init(void)
 		return CUE_SINIT_FAILED;
 	}
 
-	family_id = device_handle->info.family_id;
+	family_id = device_handle->core->info.family_id;
 	/* VI asic POLARIS10/11 have specific external_rev_id */
-	chip_rev = device_handle->info.chip_rev;
-	chip_id = device_handle->info.chip_external_rev;
+	chip_rev = device_handle->core->info.chip_rev;
+	chip_id = device_handle->core->info.chip_external_rev;
 
 	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
 	if (r)
tests/amdgpu/deadlock_tests.c +4 -4
index 91368c15..8526bae7 100644
@@ -127,14 +127,14 @@ CU_BOOL suite_deadlock_tests_enable(void)
 	 * Only enable for ASICs supporting GPU reset and for which it's enabled
 	 * by default (currently GFX8/9 dGPUS)
 	 */
-	if (device_handle->info.family_id != AMDGPU_FAMILY_VI &&
-	    device_handle->info.family_id != AMDGPU_FAMILY_AI &&
-	    device_handle->info.family_id != AMDGPU_FAMILY_CI) {
+	if (device_handle->core->info.family_id != AMDGPU_FAMILY_VI &&
+	    device_handle->core->info.family_id != AMDGPU_FAMILY_AI &&
+	    device_handle->core->info.family_id != AMDGPU_FAMILY_CI) {
 		printf("\n\nGPU reset is not enabled for the ASIC, deadlock suite disabled\n");
 		enable = CU_FALSE;
 	}
 
-	if (device_handle->info.family_id >= AMDGPU_FAMILY_AI)
+	if (device_handle->core->info.family_id >= AMDGPU_FAMILY_AI)
 		use_uc_mtype = 1;
 
 	if (amdgpu_device_deinitialize(device_handle))
tests/amdgpu/uvd_enc_tests.c +1 -1
index b4251bcf..856e7ae1 100644
@@ -114,7 +114,7 @@ int suite_uvd_enc_tests_init(void)
 	if (r)
 		return CUE_SINIT_FAILED;
 
-	family_id = device_handle->info.family_id;
+	family_id = device_handle->core->info.family_id;
 
 	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
 	if (r)
tests/amdgpu/vce_tests.c +6 -6
index 0026826e..05d9ef57 100644
@@ -103,10 +103,10 @@ CU_BOOL suite_vce_tests_enable(void)
 					     &minor_version, &device_handle))
 		return CU_FALSE;
 
-	family_id = device_handle->info.family_id;
-	chip_rev = device_handle->info.chip_rev;
-	chip_id = device_handle->info.chip_external_rev;
-	ids_flags = device_handle->info.ids_flags;
+	family_id = device_handle->core->info.family_id;
+	chip_rev = device_handle->core->info.chip_rev;
+	chip_id = device_handle->core->info.chip_external_rev;
+	ids_flags = device_handle->core->info.ids_flags;
 
 	amdgpu_query_firmware_version(device_handle, AMDGPU_INFO_FW_VCE, 0,
 					  0, &version, &feature);
@@ -153,8 +153,8 @@ int suite_vce_tests_init(void)
 		return CUE_SINIT_FAILED;
 	}
 
-	family_id = device_handle->info.family_id;
-	vce_harvest_config = device_handle->info.vce_harvest_config;
+	family_id = device_handle->core->info.family_id;
+	vce_harvest_config = device_handle->core->info.vce_harvest_config;
 
 	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
 	if (r)
tests/amdgpu/vcn_tests.c +2 -2
index ad438f35..17248444 100644
@@ -94,7 +94,7 @@ CU_BOOL suite_vcn_tests_enable(void)
 				   &minor_version, &device_handle))
 		return CU_FALSE;
 
-	family_id = device_handle->info.family_id;
+	family_id = device_handle->core->info.family_id;
 
 	if (amdgpu_device_deinitialize(device_handle))
 			return CU_FALSE;
@@ -132,7 +132,7 @@ int suite_vcn_tests_init(void)
 	if (r)
 		return CUE_SINIT_FAILED;
 
-	family_id = device_handle->info.family_id;
+	family_id = device_handle->core->info.family_id;
 
 	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
 	if (r)
tests/amdgpu/vm_tests.c +1 -1
index 69bc4683..e52aef25 100644
@@ -43,7 +43,7 @@ CU_BOOL suite_vm_tests_enable(void)
 				     &minor_version, &device_handle))
 		return CU_FALSE;
 
-	if (device_handle->info.family_id == AMDGPU_FAMILY_SI) {
+	if (device_handle->core->info.family_id == AMDGPU_FAMILY_SI) {
 		printf("\n\nCurrently hangs the CP on this ASIC, VM suite disabled\n");
 		enable = CU_FALSE;
 	}
Signature · 6 lines
2.20.1

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

PATCH 5/9 amdgpu: Add amdgpu_bo_handle_type_kms_user

In reply to Michel Dänzer

From: Michel Dänzer <[email protected]>

amdgpu_bo_handle_type_kms returns the handle valid for the DRM file descriptor (fd) used for CS submission etc. This is also valid for the fd passed to amdgpu_device_initialize the first time for a specific GPU, but in general not for fds passed to amdgpu_device_initialize later for the same GPU.

Because some use-cases require a handle valid for the fd passed to amdgpu_device_initialize, amdgpu_bo_handle_type_kms_user is added for this purpose.

---
 amdgpu/amdgpu.h          | 14 ++++++++-
 amdgpu/amdgpu_bo.c       | 65 +++++++++++++++++++++++++++-------------
 amdgpu/amdgpu_internal.h |  1 +
 3 files changed, 59 insertions(+), 21 deletions(-)
amdgpu/amdgpu.h +13 -1
index 66e45f73..f95c0a36 100644
@@ -81,7 +81,14 @@ enum amdgpu_bo_handle_type {
 	/** GEM flink name (needs DRM authentication, used by DRI2) */
 	amdgpu_bo_handle_type_gem_flink_name = 0,
 
-	/** KMS handle which is used by all driver ioctls */
+	/** KMS handle which is used by all driver ioctls
+	 *
+	 * NOTE: The returned handle is valid for the DRM file description
+	 * used for command submission, which may be different from the one
+	 * referenced by the file descriptor passed to
+	 * amdgpu_device_initialize. Use amdgpu_bo_handle_type_kms_user to
+	 * get a handle valid for the latter.
+	 */
 	amdgpu_bo_handle_type_kms = 1,
 
 	/** DMA-buf fd handle */
@@ -91,6 +98,11 @@ enum amdgpu_bo_handle_type {
 	 * amdgpu_bo_handle_type_kms, use that instead of this
 	 */
 	amdgpu_bo_handle_type_kms_noimport = 3,
+
+	/** KMS handle valid for the DRM file description referenced by the
+	 * file descriptor passed to amdgpu_device_initialize.
+	 */
+	amdgpu_bo_handle_type_kms_user = 4,
 };
 
 /** Define known types of GPU VM VA ranges */
amdgpu/amdgpu_bo.c +45 -20

111 lines not on this page · raw message

amdgpu/amdgpu_internal.h +1 -0
index 3a2ab74c..a08a4ae8 100644
@@ -111,6 +111,7 @@ struct amdgpu_core_bo {
 
 struct amdgpu_bo {
 	atomic_t refcount;
+	uint32_t user_handle;
 	struct amdgpu_bo *next;
 	struct amdgpu_core_bo *core;
 	struct amdgpu_device *dev;
Signature · 6 lines
2.20.1

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

PATCH 6/9 amdgpu: Re-use an existing amdgpu_device when possible

In reply to Michel Dänzer

From: Michel Dänzer <[email protected]>

It's possible if amdgpu_device's user_fd references the same file description as the fd passed to amdgpu_device_initialize.

---
 amdgpu/amdgpu_device.c   | 76 +++++++++++++++++++++++++++++-----------
 amdgpu/amdgpu_internal.h |  3 +-
 2 files changed, 58 insertions(+), 21 deletions(-)
amdgpu/amdgpu_device.c +56 -20
index abf5f942..8d9a85c2 100644
@@ -28,6 +28,11 @@
  *
  */
 
+#ifdef __linux__
+#include <linux/kcmp.h>
+#include <sys/syscall.h>
+#endif
+
 #include <sys/stat.h>
 #include <errno.h>
 #include <string.h>
@@ -44,7 +49,7 @@
 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
 
 static pthread_mutex_t dev_mutex = PTHREAD_MUTEX_INITIALIZER;
-static struct amdgpu_core_device *dev_list;
+static amdgpu_device_handle dev_list;
 
 static int fd_compare(int fd1, int fd2)
 {
@@ -67,12 +72,6 @@ static int fd_compare(int fd1, int fd2)
 
 static void amdgpu_device_free(struct amdgpu_core_device *dev)
 {
-	struct amdgpu_core_device **node = &dev_list;
-
-	while (*node != dev && (*node)->next)
-		node = &(*node)->next;
-	*node = (*node)->next;
-
 	close(dev->fd);
 
 	amdgpu_vamgr_deinit(&dev->vamgr_32);
@@ -86,20 +85,35 @@ static void amdgpu_device_free(struct amdgpu_core_device *dev)
 	free(dev);
 }
 
+static bool same_file_description(int fd1, int fd2)
+{
+#ifdef __linux__
+	pid_t pid = getpid();
+
+	return syscall(SYS_kcmp, pid, pid, KCMP_FILE, fd1, fd2) == 0;
+#endif
+
+	/* NOTE: This is never true at this point, since we always duplicate the
+	 * fd passed to amdgpu_device_initialize
+	 */
+	return fd1 == fd2;
+}
+
 static int amdgpu_device_init(amdgpu_device_handle user_dev)
 {
+	struct amdgpu_device *dev_iter;
 	struct amdgpu_core_device *dev;
 	drmVersionPtr version;
 	uint64_t start, max;
 	int r;
 
-	for (dev = dev_list; dev; dev = dev->next)
-		if (fd_compare(dev->fd, user_dev->user_fd) == 0)
+	for (dev_iter = dev_list; dev_iter; dev_iter = dev_iter->next)
+		if (fd_compare(dev_iter->core->fd, user_dev->user_fd) == 0)
 			break;
 
-	if (dev) {
-		atomic_inc(&dev->refcount);
-		user_dev->core = dev;
+	if (dev_iter) {
+		atomic_inc(&dev_iter->core->refcount);
+		user_dev->core = dev_iter->core;
 		return 0;
 	}
 
@@ -115,9 +129,6 @@ static int amdgpu_device_init(amdgpu_device_handle user_dev)
 	dev->fd = user_dev->user_fd;
 	user_dev->core = dev;
 
-	dev->next = dev_list;
-	dev_list = dev;
-
 	version = drmGetVersion(dev->fd);
 	if (version->version_major != 3) {
 		fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
@@ -184,6 +195,17 @@ drm_public int amdgpu_device_initialize(int fd,
 
 	*device_handle = NULL;
 
+	pthread_mutex_lock(&dev_mutex);
+
+	for (user_dev = dev_list; user_dev; user_dev = user_dev->next) {
+		if (same_file_description(user_dev->user_fd, fd)) {
+			atomic_inc(&user_dev->refcount);
+			goto out;
+		}
+	}
+
+	pthread_mutex_unlock(&dev_mutex);
+
 	user_dev = calloc(1, sizeof(struct amdgpu_device));
 	if (!user_dev) {
 		fprintf(stderr, "%s: calloc failed\n", __func__);
@@ -211,6 +233,11 @@ drm_public int amdgpu_device_initialize(int fd,
 		goto cleanup;
 	}
 
+	atomic_set(&user_dev->refcount, 1);
+	user_dev->next = dev_list;
+	dev_list = user_dev;
+
+out:
 	*major_version = user_dev->core->major_version;
 	*minor_version = user_dev->core->minor_version;
 	*device_handle = user_dev;
@@ -234,14 +261,23 @@ drm_public int amdgpu_device_deinitialize(amdgpu_device_handle user_dev)
 
 	pthread_mutex_lock(&dev_mutex);
 
-	if (user_dev->user_fd != dev->fd)
-		close(user_dev->user_fd);
+	if (update_references(&user_dev->refcount, NULL)) {
+		struct amdgpu_device **node = &dev_list;
 
-	if (update_references(&dev->refcount, NULL))
-		amdgpu_device_free(dev);
+		while (*node != user_dev && (*node)->next)
+			node = &(*node)->next;
+		*node = (*node)->next;
+
+		if (user_dev->user_fd != dev->fd)
+			close(user_dev->user_fd);
+
+		if (update_references(&dev->refcount, NULL))
+			amdgpu_device_free(dev);
+
+		free(user_dev);
+	}
 
 	pthread_mutex_unlock(&dev_mutex);
-	free(user_dev);
 	return 0;
 }
 
amdgpu/amdgpu_internal.h +2 -1
index a08a4ae8..686d50ec 100644
@@ -70,7 +70,6 @@ struct amdgpu_core_device {
 	unsigned major_version;
 	unsigned minor_version;
 
-	struct amdgpu_core_device *next;
 	char *marketing_name;
 	/** List of buffer handles. Protected by bo_table_mutex. */
 	struct handle_table bo_handles;
@@ -91,8 +90,10 @@ struct amdgpu_core_device {
 };
 
 struct amdgpu_device {
+	atomic_t refcount;
 	int user_fd;
 	struct amdgpu_core_device *core;
+	struct amdgpu_device *next;
 };
 
 struct amdgpu_core_bo {
Signature · 6 lines
2.20.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#11 lore raw

PATCH 7/9 amdgpu: Use normal integers for device / core BO reference counting

In reply to Michel Dänzer

From: Michel Dänzer <[email protected]>

The dev_mutex / dev->bo_table_mutex is always locked when accessing the reference counts, so no need for atomic operations.

---
 amdgpu/amdgpu_bo.c       |  6 +++---
 amdgpu/amdgpu_device.c   | 18 ++++++++++--------
 amdgpu/amdgpu_internal.h |  6 +++---
 3 files changed, 16 insertions(+), 14 deletions(-)
amdgpu/amdgpu_bo.c +3 -3
index 8d42db90..f40df484 100644
@@ -65,7 +65,7 @@ static int amdgpu_core_bo_create(struct amdgpu_core_device *dev,
 		return r;
 	}
 
-	atomic_set(&bo->refcount, 1);
+	bo->refcount = 1;
 	bo->alloc_size = size;
 	bo->handle = handle;
 	pthread_mutex_init(&bo->cpu_access_mutex, NULL);
@@ -95,7 +95,7 @@ static int amdgpu_bo_create(amdgpu_device_handle user_dev,
 				goto out;
 			}
 		}
-		atomic_inc(&bo->refcount);
+		bo->refcount++;
 	} else {
 		r = amdgpu_core_bo_create(dev, size, handle, &bo);
 		if (r)
@@ -467,7 +467,7 @@ static void amdgpu_core_bo_free(struct amdgpu_bo *user_bo)
 	struct amdgpu_core_device *dev = user_bo->dev->core;
 	struct amdgpu_core_bo *bo = user_bo->core;
 
-	if (update_references(&bo->refcount, NULL)) {
+	if (--bo->refcount == 0) {
 		/* Remove the buffer from the hash tables. */
 		handle_table_remove(&dev->bo_handles, bo->handle);
 
amdgpu/amdgpu_device.c +10 -8
index 8d9a85c2..8a99b8bb 100644
@@ -72,6 +72,9 @@ static int fd_compare(int fd1, int fd2)
 
 static void amdgpu_device_free(struct amdgpu_core_device *dev)
 {
+	if (--dev->refcount > 0)
+		return;
+
 	close(dev->fd);
 
 	amdgpu_vamgr_deinit(&dev->vamgr_32);
@@ -112,7 +115,7 @@ static int amdgpu_device_init(amdgpu_device_handle user_dev)
 			break;
 
 	if (dev_iter) {
-		atomic_inc(&dev_iter->core->refcount);
+		dev_iter->core->refcount++;
 		user_dev->core = dev_iter->core;
 		return 0;
 	}
@@ -123,7 +126,7 @@ static int amdgpu_device_init(amdgpu_device_handle user_dev)
 		return -ENOMEM;
 	}
 
-	atomic_set(&dev->refcount, 1);
+	dev->refcount = 1;
 	pthread_mutex_init(&dev->bo_table_mutex, NULL);
 
 	dev->fd = user_dev->user_fd;
@@ -199,7 +202,7 @@ drm_public int amdgpu_device_initialize(int fd,
 
 	for (user_dev = dev_list; user_dev; user_dev = user_dev->next) {
 		if (same_file_description(user_dev->user_fd, fd)) {
-			atomic_inc(&user_dev->refcount);
+			user_dev->refcount++;
 			goto out;
 		}
 	}
@@ -233,7 +236,7 @@ drm_public int amdgpu_device_initialize(int fd,
 		goto cleanup;
 	}
 
-	atomic_set(&user_dev->refcount, 1);
+	user_dev->refcount = 1;
 	user_dev->next = dev_list;
 	dev_list = user_dev;
 
@@ -248,7 +251,7 @@ out:
 cleanup:
 	if (!user_dev->core || user_dev->user_fd != user_dev->core->fd)
 		close(user_dev->user_fd);
-	if (user_dev->core && update_references(&user_dev->core->refcount, NULL))
+	if (user_dev->core)
 		amdgpu_device_free(user_dev->core);
 	free(user_dev);
 	pthread_mutex_unlock(&dev_mutex);
@@ -261,7 +264,7 @@ drm_public int amdgpu_device_deinitialize(amdgpu_device_handle user_dev)
 
 	pthread_mutex_lock(&dev_mutex);
 
-	if (update_references(&user_dev->refcount, NULL)) {
+	if (--user_dev->refcount == 0) {
 		struct amdgpu_device **node = &dev_list;
 
 		while (*node != user_dev && (*node)->next)
@@ -271,8 +274,7 @@ drm_public int amdgpu_device_deinitialize(amdgpu_device_handle user_dev)
 		if (user_dev->user_fd != dev->fd)
 			close(user_dev->user_fd);
 
-		if (update_references(&dev->refcount, NULL))
-			amdgpu_device_free(dev);
+		amdgpu_device_free(dev);
 
 		free(user_dev);
 	}
amdgpu/amdgpu_internal.h +3 -3
index 686d50ec..70566f98 100644
@@ -65,7 +65,7 @@ struct amdgpu_va {
 };
 
 struct amdgpu_core_device {
-	atomic_t refcount;
+	int refcount;
 	int fd;
 	unsigned major_version;
 	unsigned minor_version;
@@ -90,14 +90,14 @@ struct amdgpu_core_device {
 };
 
 struct amdgpu_device {
-	atomic_t refcount;
+	int refcount;
 	int user_fd;
 	struct amdgpu_core_device *core;
 	struct amdgpu_device *next;
 };
 
 struct amdgpu_core_bo {
-	atomic_t refcount;
+	int refcount;
 	amdgpu_bo_handle user_bos;
 
 	uint64_t alloc_size;
Signature · 6 lines
2.20.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#12 lore raw
In reply to Michel Dänzer

Patches #1 - #3 look good to me, but I'm not sure if the rest is such a good idea.

Basically you not only want to use the same FD for CS, but also for basically all buffer functions and as far as I can see we break that here.

I would rather add a new function to export the KMS handle for a certain BO/FD pair. Exporting the handle based on a type was also like trying to squeeze a round pig through a square hole in the first place.

KMS, flink and DMA-buf fd are fundamentally different and shouldn't be handled by the same function in the first place.

The only tricky part in this scenario is to know when to close the KMS handle again.

Christian.

Am 24.06.19 um 18:53 schrieb Michel Dänzer:

Quoted 39 lines

From: Michel Dänzer <[email protected]>

The motivation for these patches is https://bugs.freedesktop.org/110903 .

Patches 1-3 are preparatory.

Patches 4 & 5 are the core patches allowing the issues discussed in the bug report to be fixed.

Patches 6-8 are further optimizations / cleanups.

Patch 9 is the Mesa patch making use of the new amdgpu_bo_handle_type_kms_user API to fix the user visible problem.

Note that the libdrm patches need to land first, a libdrm release needs to be made, and the Mesa patch needs to bump the libdrm_amdgpu version requirement to that release's version before it can land.

Michel Dänzer (9):
amdgpu: Pass file descriptor directly to amdgpu_close_kms_handle
amdgpu: Add BO handle to table in amdgpu_bo_create
amdgpu: Rename fd_mutex/list to dev_mutex/list
amdgpu: Add struct amdgpu_core_device and amdgpu_core_bo
amdgpu: Add amdgpu_bo_handle_type_kms_user
amdgpu: Re-use an existing amdgpu_device when possible
amdgpu: Use normal integers for device / core BO reference counting
amdgpu: Drop refcount member from struct amdgpu_core_bo/device winsys/amdgpu: Use amdgpu_bo_handle_type_kms_user for API KMS handles

  amdgpu/amdgpu.h               |  14 +-
  amdgpu/amdgpu_asic_id.c       |   4 +-
  amdgpu/amdgpu_bo.c            | 367 ++++++++++++++++++++++------------
  amdgpu/amdgpu_cs.c            |  64 +++---
  amdgpu/amdgpu_device.c        | 281 +++++++++++++-------------
  amdgpu/amdgpu_gpu_info.c      |  35 ++--
  amdgpu/amdgpu_internal.h      |  31 ++-
  amdgpu/amdgpu_vamgr.c         |   9 +-
  amdgpu/amdgpu_vm.c            |   4 +-
  tests/amdgpu/amdgpu_test.c    |   2 +-
Quoted 9 lines
  tests/amdgpu/bo_tests.c       |   2 +-
  tests/amdgpu/cs_tests.c       |   8 +-
  tests/amdgpu/deadlock_tests.c |   8 +-
  tests/amdgpu/uvd_enc_tests.c  |   2 +-
  tests/amdgpu/vce_tests.c      |  12 +-
  tests/amdgpu/vcn_tests.c      |   4 +-
  tests/amdgpu/vm_tests.c       |   2 +-
  17 files changed, 500 insertions(+), 349 deletions(-)

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

A message that is not in the archive (sent off-list or not yet received): b48aae10-c1db-b76b-ddde-9c0a47028633-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#14 lore raw
Quoted 5 lines · Christian König

Patches #1 - #3 look good to me, but I'm not sure if the rest is such a good idea.

Basically you not only want to use the same FD for CS, but also for basically all buffer functions and as far as I can see we break that here.

How so? The core FD is used for everything except flink and amdgpu_bo_handle_type_kms_user.

Quoted 6 lines

I would rather add a new function to export the KMS handle for a certain BO/FD pair. Exporting the handle based on a type was also like trying to squeeze a round pig through a square hole in the first place.

KMS, flink and DMA-buf fd are fundamentally different and shouldn't be handled by the same function in the first place.

I don't really see the problem. radeonsi & RADV are using amdgpu_bo_handle_type_kms to get handles for command stream submission, so those handles have to be valid for the core FD. Now there's amdgpu_bo_handle_type_kms_user to get a handle valid for the FD passed to amdgpu_device_initialize. Can you describe a scenario where a handle valid for yet another file description is needed?

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

Am 25.06.19 um 10:02 schrieb Michel Dänzer:

Quoted 8 lines · Christian König
Quoted 5 lines

Patches #1 - #3 look good to me, but I'm not sure if the rest is such a good idea.

Basically you not only want to use the same FD for CS, but also for basically all buffer functions and as far as I can see we break that here.

How so? The core FD is used for everything except flink and amdgpu_bo_handle_type_kms_user.

IIRC in the Mesa winsys we compare the amdgpu_device and amdgpu_bo pointers to figure out if an opened device or imported BO is the same as one we already know.

With this patch that won't work any more and for example OpenGL and VA-API could potentially use separate amdgpu_bo pointers for the same underlying buffer. That in turn would break synchronization.

Additional to that we potentially could keep a bunch of file descriptors around which we don't necessary want. In other words we previously closed the extra file descriptors, but now we keep them open as well. But that should probably be only a minor problem.

Quoted 4 lines
Quoted 4 lines

I would rather add a new function to export the KMS handle for a certain BO/FD pair. Exporting the handle based on a type was also like trying to squeeze a round pig through a square hole in the first place.

Quoted 8 lines
Quoted 2 lines

KMS, flink and DMA-buf fd are fundamentally different and shouldn't be handled by the same function in the first place.

I don't really see the problem. radeonsi & RADV are using amdgpu_bo_handle_type_kms to get handles for command stream submission, so those handles have to be valid for the core FD. Now there's amdgpu_bo_handle_type_kms_user to get a handle valid for the FD passed to amdgpu_device_initialize. Can you describe a scenario where a handle valid for yet another file description is needed?

Not yet another file descriptor, but the underlying problem here seems to be that we don't tell libdrm in which domain/file descriptor the KMS should actually be valid in.

Regards,
Christian.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): 78d5a7c1-8534-7917-58bc-1827dff106c8-5C7GfCeVMHo@public.gmane.org

#17 lore raw
Quoted 10 lines · Christian

Am 25.06.19 um 10:02 schrieb Michel Dänzer:

Quoted 8 lines · Christian König
Quoted 5 lines

Patches #1 - #3 look good to me, but I'm not sure if the rest is such a good idea.

Basically you not only want to use the same FD for CS, but also for basically all buffer functions and as far as I can see we break that here.

How so? The core FD is used for everything except flink and amdgpu_bo_handle_type_kms_user.

Quoted 7 lines

IIRC in the Mesa winsys we compare the amdgpu_device and amdgpu_bo pointers to figure out if an opened device or imported BO is the same as one we already know.

With this patch that won't work any more and for example OpenGL and VA-API could potentially use separate amdgpu_bo pointers for the same underlying buffer. That in turn would break synchronization.

Hmm, I was hoping patch 6 would cover this, but it looks like OpenGL and VA-API pass file descriptors referencing different file descriptions to amdgpu_device_initialize, so you're right. :(

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I would rather add a new function to export the KMS handle for a certain BO/FD pair.

I'll try this approach then.

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