Skip to contentSkip to conversation

amdgpu: prevent an integer wraparound of cpu_map_count

PATCH In review Marek Olšák 5 participants 20 messages Oct 23, 2018 – Nov 2, 2018 14 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 amdgpu: prevent an integer wraparound of cpu_map_count

From: Marek Olšák <[email protected]>

---
 amdgpu/amdgpu_bo.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
amdgpu/amdgpu_bo.c +17 -2
index c0f42e81..81f8a5f7 100644
@@ -22,20 +22,21 @@
  *
  */
 
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <limits.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/time.h>
 
 #include "libdrm_macros.h"
 #include "xf86drm.h"
 #include "amdgpu_drm.h"
 #include "amdgpu_internal.h"
 #include "util_math.h"
 
@@ -442,21 +443,29 @@ drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)
 {
 	union drm_amdgpu_gem_mmap args;
 	void *ptr;
 	int r;
 
 	pthread_mutex_lock(&bo->cpu_access_mutex);
 
 	if (bo->cpu_ptr) {
 		/* already mapped */
 		assert(bo->cpu_map_count > 0);
-		bo->cpu_map_count++;
+
+		/* If the counter has already reached INT_MAX, don't increment
+		 * it and assume that the buffer will be mapped indefinitely.
+		 * The buffer is pretty unlikely to get unmapped by the user
+		 * at this point.
+		 */
+		if (bo->cpu_map_count != INT_MAX)
+			bo->cpu_map_count++;
+
 		*cpu = bo->cpu_ptr;
 		pthread_mutex_unlock(&bo->cpu_access_mutex);
 		return 0;
 	}
 
 	assert(bo->cpu_map_count == 0);
 
 	memset(&args, 0, sizeof(args));
 
 	/* Query the buffer address (args.addr_ptr).
@@ -492,21 +501,27 @@ drm_public int amdgpu_bo_cpu_unmap(amdgpu_bo_handle bo)
 
 	pthread_mutex_lock(&bo->cpu_access_mutex);
 	assert(bo->cpu_map_count >= 0);
 
 	if (bo->cpu_map_count == 0) {
 		/* not mapped */
 		pthread_mutex_unlock(&bo->cpu_access_mutex);
 		return -EINVAL;
 	}
 
-	bo->cpu_map_count--;
+	/* If the counter has already reached INT_MAX, don't decrement it.
+	 * This is because amdgpu_bo_cpu_map doesn't increment it past
+	 * INT_MAX.
+	 */
+	if (bo->cpu_map_count != INT_MAX)
+		bo->cpu_map_count--;
+
 	if (bo->cpu_map_count > 0) {
 		/* mapped multiple times */
 		pthread_mutex_unlock(&bo->cpu_access_mutex);
 		return 0;
 	}
 
 	r = drm_munmap(bo->cpu_ptr, bo->alloc_size) == 0 ? 0 : -errno;
 	bo->cpu_ptr = NULL;
 	pthread_mutex_unlock(&bo->cpu_access_mutex);
 	return r;
Signature · 6 lines
2.17.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): 20181023190733.15251-1-maraeo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH 2/2 amdgpu: don't track handles for non-memory allocations

From: Marek Olšák <[email protected]>

---
 amdgpu/amdgpu_bo.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
amdgpu/amdgpu_bo.c +9 -6
index 81f8a5f7..00b9b54a 100644
@@ -91,26 +91,29 @@ drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev,
 	if (r)
 		goto out;
 
 	r = amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
 			     buf_handle);
 	if (r) {
 		amdgpu_close_kms_handle(dev, 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);
+	if (alloc_buffer->preferred_heap &
+	    (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) {
+		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;
 }
 
 drm_public int amdgpu_bo_set_metadata(amdgpu_bo_handle bo,
 				      struct amdgpu_bo_metadata *info)
 {
 	struct drm_amdgpu_gem_metadata args = {};
 
 	args.handle = bo->handle;
Signature · 6 lines
2.17.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): 20181023190733.15251-2-maraeo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw

Re: [PATCH libdrm 2/2] amdgpu: don't track handles for non-memory allocations

Quoted 1 line · Marek Olšák

From: Marek Olšák <[email protected]>

commit log and sign-off here as well.
And any reason for that?

Regards,
Jerry

Quoted 35 lines
---
  amdgpu/amdgpu_bo.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
amdgpu/amdgpu_bo.c +9 -6
index 81f8a5f7..00b9b54a 100644
@@ -91,26 +91,29 @@ drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev,
  	if (r)
  		goto out;
 
  	r = amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
  			     buf_handle);
  	if (r) {
  		amdgpu_close_kms_handle(dev, 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);
+	if (alloc_buffer->preferred_heap &
+	    (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) {
+		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);
+	}

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev, · line 119

Quoted 10 lines
amdgpu/amdgpu_bo.c +0 -0
  out:
  	return r;
  }
 
  drm_public int amdgpu_bo_set_metadata(amdgpu_bo_handle bo,
  				      struct amdgpu_bo_metadata *info)
  {
  	struct drm_amdgpu_gem_metadata args = {};
 
  	args.handle = bo->handle;

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

#5 lore raw

Re: [PATCH libdrm 2/2] amdgpu: don't track handles for non-memory allocations

In reply to a message not in the archive
Quoted 19 lines · Marek Olšák

From: Marek Olšák <[email protected]>

---
 amdgpu/amdgpu_bo.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
amdgpu/amdgpu_bo.c +0 -0
index 81f8a5f7..00b9b54a 100644
@@ -91,26 +91,29 @@ drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev,
 	if (r)
 		goto out;
 
 	r = amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
 			     buf_handle);
 	if (r) {
 		amdgpu_close_kms_handle(dev, args.out.handle);
 		goto out;

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev, · line 102

Quoted 10 lines
amdgpu/amdgpu_bo.c +2 -6
 	}
 
-	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);
+	if (alloc_buffer->preferred_heap &
+	    (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) {

What about AMDGPU_GEM_DOMAIN_CPU? I mean, that's unlikely to actually be used here, but if it were, exporting and importing the resulting BO should work fine?

Instead of white-listing the domains which can be shared, it might be better to black-list those which can't, i.e. GDS/GWS/OA.

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): ea6c7b39-82e6-cbe0-15b3-071f0e8964d2-otUistvHUpPR7s880joybQ@public.gmane.org

#7 lore raw

Re: [PATCH libdrm 2/2] amdgpu: don't track handles for non-memory allocations

Am 24.10.18 um 10:04 schrieb Michel Dänzer:

Quoted 34 lines · Marek Olšák
Quoted 19 lines

From: Marek Olšák <[email protected]>

---
  amdgpu/amdgpu_bo.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
amdgpu/amdgpu_bo.c +0 -0
index 81f8a5f7..00b9b54a 100644
@@ -91,26 +91,29 @@ drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev,
  	if (r)
  		goto out;
 
  	r = amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
  			     buf_handle);
  	if (r) {
  		amdgpu_close_kms_handle(dev, args.out.handle);
  		goto out;

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev, · line 102

Quoted 10 lines
amdgpu/amdgpu_bo.c +2 -6
  	}
 
-	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);
+	if (alloc_buffer->preferred_heap &
+	    (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) {

What about AMDGPU_GEM_DOMAIN_CPU? I mean, that's unlikely to actually be used here, but if it were, exporting and importing the resulting BO should work fine?

Quoted 2 lines

Instead of white-listing the domains which can be shared, it might be better to black-list those which can't, i.e. GDS/GWS/OA.

Well first of all GDS can be shared between applications.

Then adding a BO to the tracking doesn't add much overhead (only 8 bytes and only if it was the last allocated).

So I don't really see a reason why we should do this?

Christian.
_______________________________________________
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): 5d944204-e524-7ada-0188-f68cedbedf1c-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#9 lore raw

Re: [PATCH libdrm 2/2] amdgpu: don't track handles for non-memory allocations

OK. I'll drop this patch.

Marek

Quoted 40 lines · Christian König

Am 24.10.18 um 10:04 schrieb Michel Dänzer:

Quoted 7 lines · Marek Olšák
Quoted 6 lines

From: Marek Olšák <[email protected]>

---
  amdgpu/amdgpu_bo.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
Quoted 5 lines

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_alloc(amdgpu_device_handle

Quoted 5 lines
amdgpu/amdgpu_bo.c +0 -0
index 81f8a5f7..00b9b54a 100644
@@ -91,26 +91,29 @@ drm_public int amdgpu_bo_alloc(amdgpu_device_handle

dev,

Quoted 4 lines

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev,

Quoted 4 lines
amdgpu/amdgpu_bo.c +0 -0
     if (r)
             goto out;
 
     r = amdgpu_bo_create(dev, alloc_buffer->alloc_size,

args.out.handle,

Quoted 14 lines
Quoted 4 lines
amdgpu/amdgpu_bo.c +0 -0
                          buf_handle);
     if (r) {
             amdgpu_close_kms_handle(dev, args.out.handle);
             goto out;

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev, · line 102

Quoted 10 lines
amdgpu/amdgpu_bo.c +2 -6
     }
 
-    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);
+    if (alloc_buffer->preferred_heap &
+        (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) {
Quoted 6 lines

What about AMDGPU_GEM_DOMAIN_CPU? I mean, that's unlikely to actually be used here, but if it were, exporting and importing the resulting BO should work fine?

Instead of white-listing the domains which can be shared, it might be better to black-list those which can't, i.e. GDS/GWS/OA.

Quoted 9 lines

Well first of all GDS can be shared between applications.

Then adding a BO to the tracking doesn't add much overhead (only 8 bytes and only if it was the last allocated).

So I don't really see a reason why we should do this?

Christian.

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

#10 lore raw
In reply to a message not in the archive
Quoted 1 line · Marek Olšák

From: Marek Olšák <[email protected]>

We need commit log and sign-off here.

BTW, have you encounter any issue about that?

Quoted 41 lines
---
  amdgpu/amdgpu_bo.c | 19 +++++++++++++++++--
  1 file changed, 17 insertions(+), 2 deletions(-)
amdgpu/amdgpu_bo.c +1 -0
index c0f42e81..81f8a5f7 100644
@@ -22,20 +22,21 @@
   *
   */
 
  #include <stdlib.h>
  #include <stdio.h>
  #include <stdint.h>
  #include <string.h>
  #include <errno.h>
  #include <fcntl.h>
  #include <unistd.h>
+#include <limits.h>
  #include <sys/ioctl.h>
  #include <sys/mman.h>
  #include <sys/time.h>
 
  #include "libdrm_macros.h"
  #include "xf86drm.h"
  #include "amdgpu_drm.h"
  #include "amdgpu_internal.h"
  #include "util_math.h"
 
@@ -442,21 +443,29 @@ drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)
  {
  	union drm_amdgpu_gem_mmap args;
  	void *ptr;
  	int r;
 
  	pthread_mutex_lock(&bo->cpu_access_mutex);
 
  	if (bo->cpu_ptr) {
  		/* already mapped */

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu) · line 460

Quoted 10 lines
amdgpu/amdgpu_bo.c +8 -1
  		assert(bo->cpu_map_count > 0);
-		bo->cpu_map_count++;
+
+		/* If the counter has already reached INT_MAX, don't increment
+		 * it and assume that the buffer will be mapped indefinitely.
+		 * The buffer is pretty unlikely to get unmapped by the user
+		 * at this point.
+		 */
+		if (bo->cpu_map_count != INT_MAX)
+			bo->cpu_map_count++;

If so, shall we print some error here to notice that indefinite mappings come up.

Regards,
Jerry

Quoted 30 lines
amdgpu/amdgpu_bo.c +8 -1
+
  		*cpu = bo->cpu_ptr;
  		pthread_mutex_unlock(&bo->cpu_access_mutex);
  		return 0;
  	}
 
  	assert(bo->cpu_map_count == 0);
 
  	memset(&args, 0, sizeof(args));
 
  	/* Query the buffer address (args.addr_ptr).
@@ -492,21 +501,27 @@ drm_public int amdgpu_bo_cpu_unmap(amdgpu_bo_handle bo)
 
  	pthread_mutex_lock(&bo->cpu_access_mutex);
  	assert(bo->cpu_map_count >= 0);
 
  	if (bo->cpu_map_count == 0) {
  		/* not mapped */
  		pthread_mutex_unlock(&bo->cpu_access_mutex);
  		return -EINVAL;
  	}
 
-	bo->cpu_map_count--;
+	/* If the counter has already reached INT_MAX, don't decrement it.
+	 * This is because amdgpu_bo_cpu_map doesn't increment it past
+	 * INT_MAX.
+	 */
+	if (bo->cpu_map_count != INT_MAX)
+		bo->cpu_map_count--;
+

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_unmap(amdgpu_bo_handle bo) · line 527

Quoted 10 lines
amdgpu/amdgpu_bo.c +0 -0
  	if (bo->cpu_map_count > 0) {
  		/* mapped multiple times */
  		pthread_mutex_unlock(&bo->cpu_access_mutex);
  		return 0;
  	}
 
  	r = drm_munmap(bo->cpu_ptr, bo->alloc_size) == 0 ? 0 : -errno;
  	bo->cpu_ptr = NULL;
  	pthread_mutex_unlock(&bo->cpu_access_mutex);
  	return r;

_______________________________________________
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): 63dd9ca5-dbfc-ad21-4a3b-6c1f48f03a03-5C7GfCeVMHo@public.gmane.org

#12 lore raw
Quoted 7 lines · Jerry(Junwei)
Quoted 1 line · Marek Olšák

From: Marek Olšák <[email protected]>

We need commit log and sign-off here.

BTW, have you encounter any issue about that?

I don't know what you mean. I'm pretty sure that a sign-off is not needed for libdrm.

Quoted 51 lines

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle

Quoted 33 lines

---
  amdgpu/amdgpu_bo.c | 19 +++++++++++++++++--
  1 file changed, 17 insertions(+), 2 deletions(-)
amdgpu/amdgpu_bo.c +1 -0
index c0f42e81..81f8a5f7 100644
@@ -22,20 +22,21 @@
   *
   */
 
  #include <stdlib.h>
  #include <stdio.h>
  #include <stdint.h>
  #include <string.h>
  #include <errno.h>
  #include <fcntl.h>
  #include <unistd.h>
+#include <limits.h>
  #include <sys/ioctl.h>
  #include <sys/mman.h>
  #include <sys/time.h>
 
  #include "libdrm_macros.h"
  #include "xf86drm.h"
  #include "amdgpu_drm.h"
  #include "amdgpu_internal.h"
  #include "util_math.h"
 
@@ -442,21 +443,29 @@ drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle

bo, void **cpu)

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)

Quoted 13 lines
amdgpu/amdgpu_bo.c +2 -1
  {
      union drm_amdgpu_gem_mmap args;
      void *ptr;
      int r;
 
      pthread_mutex_lock(&bo->cpu_access_mutex);
 
      if (bo->cpu_ptr) {
              /* already mapped */
              assert(bo->cpu_map_count > 0);
-             bo->cpu_map_count++;
+
+             /* If the counter has already reached INT_MAX, don't

increment

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle · line 455

Quoted 1 line
amdgpu/amdgpu_bo.c +1 -0
+              * it and assume that the buffer will be mapped

indefinitely.

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle · line 456

Quoted 1 line
amdgpu/amdgpu_bo.c +1 -0
+              * The buffer is pretty unlikely to get unmapped by the
Quoted 9 lines

user

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu) · line 460

Quoted 4 lines
amdgpu/amdgpu_bo.c +4 -0
+              * at this point.
+              */
+             if (bo->cpu_map_count != INT_MAX)
+                     bo->cpu_map_count++;

If so, shall we print some error here to notice that indefinite mappings come up.

No error. This is expected usage.

Marek

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

#13 lore raw
In reply to a message not in the archive

That looks really ugly to me. Mapping the same BO so often is illegal and should be handled as error.

Otherwise we will never be able to cleanly recover from a GPU lockup with lost state by reloading the client library.

Christian.

Am 23.10.18 um 21:07 schrieb Marek Olšák:

Quoted 82 lines

From: Marek Olšák <[email protected]>

---
  amdgpu/amdgpu_bo.c | 19 +++++++++++++++++--
  1 file changed, 17 insertions(+), 2 deletions(-)
amdgpu/amdgpu_bo.c +17 -2
index c0f42e81..81f8a5f7 100644
@@ -22,20 +22,21 @@
   *
   */
 
  #include <stdlib.h>
  #include <stdio.h>
  #include <stdint.h>
  #include <string.h>
  #include <errno.h>
  #include <fcntl.h>
  #include <unistd.h>
+#include <limits.h>
  #include <sys/ioctl.h>
  #include <sys/mman.h>
  #include <sys/time.h>
 
  #include "libdrm_macros.h"
  #include "xf86drm.h"
  #include "amdgpu_drm.h"
  #include "amdgpu_internal.h"
  #include "util_math.h"
 
@@ -442,21 +443,29 @@ drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)
  {
  	union drm_amdgpu_gem_mmap args;
  	void *ptr;
  	int r;
 
  	pthread_mutex_lock(&bo->cpu_access_mutex);
 
  	if (bo->cpu_ptr) {
  		/* already mapped */
  		assert(bo->cpu_map_count > 0);
-		bo->cpu_map_count++;
+
+		/* If the counter has already reached INT_MAX, don't increment
+		 * it and assume that the buffer will be mapped indefinitely.
+		 * The buffer is pretty unlikely to get unmapped by the user
+		 * at this point.
+		 */
+		if (bo->cpu_map_count != INT_MAX)
+			bo->cpu_map_count++;
+
  		*cpu = bo->cpu_ptr;
  		pthread_mutex_unlock(&bo->cpu_access_mutex);
  		return 0;
  	}
 
  	assert(bo->cpu_map_count == 0);
 
  	memset(&args, 0, sizeof(args));
 
  	/* Query the buffer address (args.addr_ptr).
@@ -492,21 +501,27 @@ drm_public int amdgpu_bo_cpu_unmap(amdgpu_bo_handle bo)
 
  	pthread_mutex_lock(&bo->cpu_access_mutex);
  	assert(bo->cpu_map_count >= 0);
 
  	if (bo->cpu_map_count == 0) {
  		/* not mapped */
  		pthread_mutex_unlock(&bo->cpu_access_mutex);
  		return -EINVAL;
  	}
 
-	bo->cpu_map_count--;
+	/* If the counter has already reached INT_MAX, don't decrement it.
+	 * This is because amdgpu_bo_cpu_map doesn't increment it past
+	 * INT_MAX.
+	 */
+	if (bo->cpu_map_count != INT_MAX)
+		bo->cpu_map_count--;
+

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_unmap(amdgpu_bo_handle bo) · line 527

Quoted 10 lines
amdgpu/amdgpu_bo.c +0 -0
  	if (bo->cpu_map_count > 0) {
  		/* mapped multiple times */
  		pthread_mutex_unlock(&bo->cpu_access_mutex);
  		return 0;
  	}
 
  	r = drm_munmap(bo->cpu_ptr, bo->alloc_size) == 0 ? 0 : -errno;
  	bo->cpu_ptr = NULL;
  	pthread_mutex_unlock(&bo->cpu_access_mutex);
  	return r;

_______________________________________________
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): 7690cf4c-5eff-85e4-885c-2ecc9dc01c25-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#15 lore raw

You and I discussed this extensively internally a while ago. It's expected and correct behavior. Mesa doesn't unmap some buffers and never will.

Marek

Quoted 71 lines · Christian König

That looks really ugly to me. Mapping the same BO so often is illegal and should be handled as error.

Otherwise we will never be able to cleanly recover from a GPU lockup with lost state by reloading the client library.

Christian.

Am 23.10.18 um 21:07 schrieb Marek Olšák:

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle

Quoted 33 lines

From: Marek Olšák <[email protected]>

---
  amdgpu/amdgpu_bo.c | 19 +++++++++++++++++--
  1 file changed, 17 insertions(+), 2 deletions(-)
amdgpu/amdgpu_bo.c +1 -0
index c0f42e81..81f8a5f7 100644
@@ -22,20 +22,21 @@
   *
   */
 
  #include <stdlib.h>
  #include <stdio.h>
  #include <stdint.h>
  #include <string.h>
  #include <errno.h>
  #include <fcntl.h>
  #include <unistd.h>
+#include <limits.h>
  #include <sys/ioctl.h>
  #include <sys/mman.h>
  #include <sys/time.h>
 
  #include "libdrm_macros.h"
  #include "xf86drm.h"
  #include "amdgpu_drm.h"
  #include "amdgpu_internal.h"
  #include "util_math.h"
 
@@ -442,21 +443,29 @@ drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle

bo, void **cpu)

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)

Quoted 13 lines
amdgpu/amdgpu_bo.c +2 -1
  {
      union drm_amdgpu_gem_mmap args;
      void *ptr;
      int r;
 
      pthread_mutex_lock(&bo->cpu_access_mutex);
 
      if (bo->cpu_ptr) {
              /* already mapped */
              assert(bo->cpu_map_count > 0);
-             bo->cpu_map_count++;
+
+             /* If the counter has already reached INT_MAX, don't

increment

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle · line 455

Quoted 1 line
amdgpu/amdgpu_bo.c +1 -0
+              * it and assume that the buffer will be mapped

indefinitely.

amdgpu/amdgpu_bo.c drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle · line 456

Quoted 1 line
amdgpu/amdgpu_bo.c +1 -0
+              * The buffer is pretty unlikely to get unmapped by the

user

Quoted 10 lines
amdgpu/amdgpu_bo.c +5 -0
+              * at this point.
+              */
+             if (bo->cpu_map_count != INT_MAX)
+                     bo->cpu_map_count++;
+
              *cpu = bo->cpu_ptr;
              pthread_mutex_unlock(&bo->cpu_access_mutex);
              return 0;
      }
 
Quoted 37 lines

amdgpu/amdgpu_bo.c drm_public int

Quoted 6 lines
amdgpu/amdgpu_bo.c +0 -0
      assert(bo->cpu_map_count == 0);
 
      memset(&args, 0, sizeof(args));
 
      /* Query the buffer address (args.addr_ptr).
@@ -492,21 +501,27 @@ drm_public int

amdgpu_bo_cpu_unmap(amdgpu_bo_handle bo)

Quoted 28 lines
amdgpu/amdgpu_bo.c +7 -1
      pthread_mutex_lock(&bo->cpu_access_mutex);
      assert(bo->cpu_map_count >= 0);
 
      if (bo->cpu_map_count == 0) {
              /* not mapped */
              pthread_mutex_unlock(&bo->cpu_access_mutex);
              return -EINVAL;
      }
 
-     bo->cpu_map_count--;
+     /* If the counter has already reached INT_MAX, don't decrement it.
+      * This is because amdgpu_bo_cpu_map doesn't increment it past
+      * INT_MAX.
+      */
+     if (bo->cpu_map_count != INT_MAX)
+             bo->cpu_map_count--;
+
      if (bo->cpu_map_count > 0) {
              /* mapped multiple times */
              pthread_mutex_unlock(&bo->cpu_access_mutex);
              return 0;
      }
 
      r = drm_munmap(bo->cpu_ptr, bo->alloc_size) == 0 ? 0 : -errno;
      bo->cpu_ptr = NULL;
      pthread_mutex_unlock(&bo->cpu_access_mutex);
      return r;

_______________________________________________
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): CAAxE2A50X-XGvyx7GcZnnAC4p-bRnXrQpjQZ1D-8pD5ir3QdMQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#17 lore raw
Quoted 2 lines · Marek Olšák

You and I discussed this extensively internally a while ago. It's expected and correct behavior. Mesa doesn't unmap some buffers and never will.

It doesn't need to keep mapping the same buffer over and over again though, does it?

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): e8cf4264-f211-70b3-01ec-34d0eb5e7edb-otUistvHUpPR7s880joybQ@public.gmane.org

#19 lore raw

Am 30.10.18 um 09:20 schrieb Michel Dänzer:

Quoted 3 lines · Marek Olšák
Quoted 2 lines

You and I discussed this extensively internally a while ago. It's expected and correct behavior. Mesa doesn't unmap some buffers and never will.

Ah! Now I at least remember what issue that one was good for.

Quoted 2 lines

It doesn't need to keep mapping the same buffer over and over again though, does it?

Yeah, that's a bit unclear to me as well.

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

#20 lore raw
In reply to a message not in the archive
Quoted 8 lines · Michel Dänzer
Quoted 1 line · Marek Olšák

You and I discussed this extensively internally a while ago. It's

expected

Quoted 1 line

and correct behavior. Mesa doesn't unmap some buffers and never will.

It doesn't need to keep mapping the same buffer over and over again though, does it?

It doesnt map it again. It just doesnt unmap. So the next map call just returns the pointer. It's correct to stop the counter wraparound.

Marek

Quoted 5 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): CAAxE2A59+J7-jdYobFMMFDhS0oPLS8MgJXzV-Z=N7jhMr9rcdg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#22 lore raw
Quoted 7 lines · Marek Olšák
Quoted 3 lines · Michel Dänzer
Quoted 1 line · Marek Olšák

You and I discussed this extensively internally a while ago. It's

expected

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

and correct behavior. Mesa doesn't unmap some buffers and never will.

It doesn't need to keep mapping the same buffer over and over again though, does it?

It doesnt map it again. It just doesnt unmap. So the next map call just returns the pointer. It's correct to stop the counter wraparound.

Mesa doesn't track whether a buffer is already mapped. Libdrm tracks that. It's a feature of libdrm to return the same pointer and expect infinite number of map calls.

Marek

Quoted 9 lines

Marek

Quoted 5 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): CAAxE2A7kA_W3xeK01F7jthfMyEij3TFy_G7=A5_vsduNVrKpbQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#24 lore raw
Quoted 17 lines · Marek Olšák
Quoted 13 lines · Marek Olšák
Quoted 8 lines · Michel Dänzer
Quoted 1 line · Marek Olšák

You and I discussed this extensively internally a while ago. It's

expected

Quoted 1 line

and correct behavior. Mesa doesn't unmap some buffers and never will.

It doesn't need to keep mapping the same buffer over and over again though, does it?

Quoted 8 lines
Quoted 3 lines

It doesnt map it again. It just doesnt unmap. So the next map call just returns the pointer. It's correct to stop the counter wraparound.

Mesa doesn't track whether a buffer is already mapped. Libdrm tracks that. It's a feature of libdrm to return the same pointer and expect infinite number of map calls.

Mesa has been having this optimization for 8 years. (since the radeon winsys). It's surprising that it surprises you now.

Marek

Quoted 12 lines

Marek

Quoted 9 lines

Marek

Quoted 5 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

#25 lore raw
In reply to a message not in the archive
Quoted 12 lines · Marek Olšák
Quoted 11 lines · Marek Olšák
Quoted 8 lines · Michel Dänzer
Quoted 1 line · Marek Olšák

You and I discussed this extensively internally a while ago. It's

expected

Quoted 1 line

and correct behavior. Mesa doesn't unmap some buffers and never will.

It doesn't need to keep mapping the same buffer over and over again though, does it?

Quoted 7 lines
Quoted 3 lines

It doesnt map it again. It just doesnt unmap. So the next map call just returns the pointer. It's correct to stop the counter wraparound.

Mesa doesn't track whether a buffer is already mapped. Libdrm tracks that. It's a feature of libdrm to return the same pointer and expect infinite number of map calls.

That's not what the reference counting in libdrm is intended for. It's for keeping track of how many independent callers have mapped the buffer. Mesa should remember that it mapped a buffer and not map it again.

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): 79fe6959-6f43-5c3b-8fa1-69d43498cdd4-otUistvHUpPR7s880joybQ@public.gmane.org

#27 lore raw

Am 30.10.18 um 16:59 schrieb Michel Dänzer:

Quoted 14 lines · Marek Olšák
Quoted 13 lines · Marek Olšák
Quoted 12 lines · Michel Dänzer
Quoted 8 lines · Marek Olšák
Quoted 1 line

You and I discussed this extensively internally a while ago. It's

expected

Quoted 1 line

and correct behavior. Mesa doesn't unmap some buffers and never will.

It doesn't need to keep mapping the same buffer over and over again though, does it?

It doesnt map it again. It just doesnt unmap. So the next map call just returns the pointer. It's correct to stop the counter wraparound.

Quoted 6 lines
Quoted 3 lines

Mesa doesn't track whether a buffer is already mapped. Libdrm tracks that. It's a feature of libdrm to return the same pointer and expect infinite number of map calls.

That's not what the reference counting in libdrm is intended for. It's for keeping track of how many independent callers have mapped the buffer. Mesa should remember that it mapped a buffer and not map it again.

Well if Mesa just wants to query the existing mapping then why not add a amdgpu_bo_get_cpu_ptr() which just queries if a CPU mapping exists and if yes returns the appropriate pointer or NULL otherwise?

I mean when we want to abstract everything in libdrm then we just need to add the functions we need to use this abstraction.

Christian.
_______________________________________________
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): e434a695-a6c4-14f7-deff-5420d08311f7-5C7GfCeVMHo@public.gmane.org

#29 lore raw
Quoted 23 lines · Christian

Am 30.10.18 um 16:59 schrieb Michel Dänzer:

Quoted 3 lines · Marek Olšák
Quoted 2 lines · Marek Olšák
Quoted 1 line

On Tue, Oct 30, 2018, 4:20 AM Michel Dänzer <[email protected]>

Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Marek Olšák
Quoted 1 line

You and I discussed this extensively internally a while ago. It's

expected

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

and correct behavior. Mesa doesn't unmap some buffers and never will.

It doesn't need to keep mapping the same buffer over and over again though, does it?

It doesnt map it again. It just doesnt unmap. So the next map call just returns the pointer. It's correct to stop the counter wraparound.

Mesa doesn't track whether a buffer is already mapped. Libdrm tracks

that.

Quoted 5 lines
Quoted 2 lines

It's a feature of libdrm to return the same pointer and expect infinite number of map calls.

That's not what the reference counting in libdrm is intended for. It's for keeping track of how many independent callers have mapped the buffer. Mesa should remember that it mapped a buffer and not map it

Quoted 9 lines

again.

Well if Mesa just wants to query the existing mapping then why not add a amdgpu_bo_get_cpu_ptr() which just queries if a CPU mapping exists and if yes returns the appropriate pointer or NULL otherwise?

I mean when we want to abstract everything in libdrm then we just need to add the functions we need to use this abstraction.

That can be future work for the sake of cleanliness and clarity, but it would be a waste of time and wouldn't help old Mesa.

Marek

_______________________________________________
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): CAAxE2A7aZ2YiJN0P8KD1F=ncj17ds1x2Dmkka-tHR7Y5vrOOBQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#31 lore raw

Am 31.10.18 um 23:12 schrieb Marek Olšák:
On Wed, Oct 31, 2018 at 3:59 AM Koenig, Christian <[email protected]<mailto:[email protected]>> wrote:
Am 30.10.18 um 16:59 schrieb Michel Dänzer:

Quoted 14 lines · Marek Olšák
Quoted 13 lines · On Tue, Oct 30, 2018, 11:49 AM Marek Olšák…
Quoted 12 lines · On Tue, Oct 30, 2018, 4:20 AM Michel Dänzer…
Quoted 8 lines · Marek Olšák
Quoted 1 line

You and I discussed this extensively internally a while ago. It's

expected

Quoted 1 line

and correct behavior. Mesa doesn't unmap some buffers and never will.

It doesn't need to keep mapping the same buffer over and over again though, does it?

It doesnt map it again. It just doesnt unmap. So the next map call just returns the pointer. It's correct to stop the counter wraparound.

Quoted 6 lines
Quoted 3 lines

Mesa doesn't track whether a buffer is already mapped. Libdrm tracks that. It's a feature of libdrm to return the same pointer and expect infinite number of map calls.

That's not what the reference counting in libdrm is intended for. It's for keeping track of how many independent callers have mapped the buffer. Mesa should remember that it mapped a buffer and not map it again.

Well if Mesa just wants to query the existing mapping then why not add a amdgpu_bo_get_cpu_ptr() which just queries if a CPU mapping exists and if yes returns the appropriate pointer or NULL otherwise?

I mean when we want to abstract everything in libdrm then we just need to add the functions we need to use this abstraction.

That can be future work for the sake of cleanliness and clarity, but it would be a waste of time and wouldn't help old Mesa.

That it doesn't help old Mesa is unfortunate, but this is clearly a bug in Mesa.

If old Mesa is broken then we should fix it by updating it and not add workarounds for specific clients in libdrm.

Regards,
Christian.

Marek

_______________________________________________
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): 7ca0e170-5a2b-a6ed-4d2e-13d9b32011a7-5C7GfCeVMHo@public.gmane.org

#33 lore raw
Quoted 44 lines · Christian

Am 31.10.18 um 23:12 schrieb Marek Olšák:

Quoted 25 lines · Christian

Am 30.10.18 um 16:59 schrieb Michel Dänzer:

Quoted 3 lines · Marek Olšák
Quoted 2 lines · Marek Olšák
Quoted 1 line

On Tue, Oct 30, 2018, 4:20 AM Michel Dänzer <[email protected]>

Quoted 5 lines · wrote:
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Marek Olšák
Quoted 1 line

You and I discussed this extensively internally a while ago. It's

expected

Quoted 1 line

and correct behavior. Mesa doesn't unmap some buffers and never

will.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

It doesn't need to keep mapping the same buffer over and over again though, does it?

It doesnt map it again. It just doesnt unmap. So the next map call

just

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

returns the pointer. It's correct to stop the counter wraparound.

Mesa doesn't track whether a buffer is already mapped. Libdrm tracks

that.

Quoted 5 lines
Quoted 2 lines

It's a feature of libdrm to return the same pointer and expect infinite number of map calls.

That's not what the reference counting in libdrm is intended for. It's for keeping track of how many independent callers have mapped the buffer. Mesa should remember that it mapped a buffer and not map it

Quoted 9 lines

again.

Well if Mesa just wants to query the existing mapping then why not add a amdgpu_bo_get_cpu_ptr() which just queries if a CPU mapping exists and if yes returns the appropriate pointer or NULL otherwise?

I mean when we want to abstract everything in libdrm then we just need to add the functions we need to use this abstraction.

That can be future work for the sake of cleanliness and clarity, but it would be a waste of time and wouldn't help old Mesa.

Quoted 6 lines

That it doesn't help old Mesa is unfortunate, but this is clearly a bug in Mesa.

If old Mesa is broken then we should fix it by updating it and not add workarounds for specific clients in libdrm.

It's not a workaround. We made a decision with amdgpu to share code by moving portions of the Mesa winsys into libdrm. The map_count is part of that. It's highly desirable to continue with code sharing. There is nothing broken with Mesa. Mesa won't check whether a buffer is already mapped. That's the responsibility of libdrm as part of code sharing and we don't want to duplicate the same logic in Mesa. It's all part of the intended design.

Marek

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