Skip to contentSkip to conversation

drm: fix race issue

PATCH In review Monk Liu 2 participants 3 messages Aug 7, 2017 2 messages not in the archive

lore
Share & tools
lore

Monk Liu

author
#0 lore raw

PATCH 1/1 drm: fix race issue

From: Monk Liu <[email protected]>

there is race issue between two threads on amdgpu_bo_free and amdgpu_bo_import, this patch tend to fix it by moving the pthread_mutex_lock out of bo_internal and cover the update_reference function.

besides the mutex_unlock in bo_import should also cover bo refcount increasement.

Change-Id I8ec5a2879dda0e6468864fc64e6b46059482998b
---
 amdgpu/amdgpu_bo.c       | 13 +++++--------
 amdgpu/amdgpu_internal.h | 17 +++++++++++++++--
 2 files changed, 20 insertions(+), 10 deletions(-)
amdgpu/amdgpu_bo.c +5 -8
index ec99488..10a3efe 100644
@@ -52,27 +52,22 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
 	drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &args);
 }
 
-void amdgpu_bo_free_internal(amdgpu_bo_handle bo)
+drm_private void amdgpu_bo_free_internal(amdgpu_bo_handle bo)
 {
 	/* Remove the buffer from the hash tables. */
-	pthread_mutex_lock(&bo->dev->bo_table_mutex);
 	util_hash_table_remove(bo->dev->bo_handles,
 			       (void*)(uintptr_t)bo->handle);
 	if (bo->flink_name) {
 		util_hash_table_remove(bo->dev->bo_flink_names,
 				       (void*)(uintptr_t)bo->flink_name);
 	}
-	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
 
 	/* Release CPU access. */
 	if (bo->cpu_map_count > 0) {
 		bo->cpu_map_count = 1;
 		amdgpu_bo_cpu_unmap(bo);
 	}
-
 	amdgpu_close_kms_handle(bo->dev, bo->handle);
-	pthread_mutex_destroy(&bo->cpu_access_mutex);
-	free(bo);
 }
 
 int amdgpu_bo_alloc(amdgpu_device_handle dev,
@@ -297,6 +292,7 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
 		/* Get a KMS handle. */
 		r = drmPrimeFDToHandle(dev->fd, shared_handle, &handle);
 		if (r) {
+			pthread_mutex_unlock(&dev->bo_table_mutex);
 			return r;
 		}
 
@@ -339,10 +335,9 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
 	}
 
 	if (bo) {
-		pthread_mutex_unlock(&dev->bo_table_mutex);
-
 		/* The buffer already exists, just bump the refcount. */
 		atomic_inc(&bo->refcount);
+		pthread_mutex_unlock(&dev->bo_table_mutex);
 
 		output->buf_handle = bo;
 		output->alloc_size = bo->alloc_size;
@@ -466,6 +461,8 @@ int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)
 	bo->cpu_map_count = 1;
 	pthread_mutex_unlock(&bo->cpu_access_mutex);
 
+	amdgpu_add_handle_to_table(bo);
+
 	*cpu = ptr;
 	return 0;
 }
amdgpu/amdgpu_internal.h +15 -2
index 0508131..e1a6559 100644
@@ -30,6 +30,9 @@
 
 #include <assert.h>
 #include <pthread.h>
+#include <stdlib.h>
+
+#include "libdrm_macros.h"
 #include "xf86atomic.h"
 #include "amdgpu.h"
 #include "util_double_list.h"
@@ -193,8 +196,18 @@ static inline bool update_references(atomic_t *dst, atomic_t *src)
 static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
 					struct amdgpu_bo *src)
 {
-	if (update_references(&(*dst)->refcount, &src->refcount))
-		amdgpu_bo_free_internal(*dst);
+	struct amdgpu_bo* bo = *dst;
+	assert(bo!=NULL);
+
+	pthread_mutex_lock(&bo->dev->bo_table_mutex);
+
+	if (update_references(&bo->refcount, src?&src->refcount:NULL)) {
+		amdgpu_bo_free_internal(bo);
+		pthread_mutex_destroy(&bo->cpu_access_mutex);
+		pthread_mutex_unlock(&bo->dev->bo_table_mutex);
+		free(bo);
+	} else
+		pthread_mutex_unlock(&bo->dev->bo_table_mutex);
 	*dst = src;
 }
 
Signature · 6 lines
2.7.4

_______________________________________________
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): 1502106259-11522-1-git-send-email-Monk.Liu-5C7GfCeVMHo@public.gmane.org

#2 lore raw

Am 07.08.2017 um 13:44 schrieb Monk Liu:

Quoted 9 lines

From: Monk Liu <[email protected]>

there is race issue between two threads on amdgpu_bo_free and amdgpu_bo_import, this patch tend to fix it by moving the pthread_mutex_lock out of bo_internal and cover the update_reference function.

besides the mutex_unlock in bo_import should also cover bo refcount increasement.

Nice catch, but the solution is to heavy.

Quoted 51 lines
Change-Id I8ec5a2879dda0e6468864fc64e6b46059482998b
---
  amdgpu/amdgpu_bo.c       | 13 +++++--------
  amdgpu/amdgpu_internal.h | 17 +++++++++++++++--
  2 files changed, 20 insertions(+), 10 deletions(-)
amdgpu/amdgpu_bo.c +2 -6
index ec99488..10a3efe 100644
@@ -52,27 +52,22 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
  	drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &args);
  }
 
-void amdgpu_bo_free_internal(amdgpu_bo_handle bo)
+drm_private void amdgpu_bo_free_internal(amdgpu_bo_handle bo)
  {
  	/* Remove the buffer from the hash tables. */
-	pthread_mutex_lock(&bo->dev->bo_table_mutex);
  	util_hash_table_remove(bo->dev->bo_handles,
  			       (void*)(uintptr_t)bo->handle);
  	if (bo->flink_name) {
  		util_hash_table_remove(bo->dev->bo_flink_names,
  				       (void*)(uintptr_t)bo->flink_name);
  	}
-	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
 
  	/* Release CPU access. */
  	if (bo->cpu_map_count > 0) {
  		bo->cpu_map_count = 1;
  		amdgpu_bo_cpu_unmap(bo);
  	}
-
  	amdgpu_close_kms_handle(bo->dev, bo->handle);
-	pthread_mutex_destroy(&bo->cpu_access_mutex);
-	free(bo);
  }
 
  int amdgpu_bo_alloc(amdgpu_device_handle dev,
@@ -297,6 +292,7 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
  		/* Get a KMS handle. */
  		r = drmPrimeFDToHandle(dev->fd, shared_handle, &handle);
  		if (r) {
+			pthread_mutex_unlock(&dev->bo_table_mutex);
  			return r;
  		}
 

amdgpu/amdgpu_bo.c int amdgpu_bo_import(amdgpu_device_handle dev, · line 340

Quoted 8 lines
amdgpu/amdgpu_bo.c +1 -2
@@ -339,10 +335,9 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
  	}
 
  	if (bo) {
-		pthread_mutex_unlock(&dev->bo_table_mutex);
-
  		/* The buffer already exists, just bump the refcount. */
  		atomic_inc(&bo->refcount);
+		pthread_mutex_unlock(&dev->bo_table_mutex);

Actually the problem is only here, that one needs to use atomic_inc_unless() before the mutex is dropped and return only if we could successfully acquire a reference to the BO.

All other changes are unnecessary.

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

Quoted 8 lines
amdgpu/amdgpu_bo.c +1 -0
  		output->buf_handle = bo;
  		output->alloc_size = bo->alloc_size;
@@ -466,6 +461,8 @@ int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)
  	bo->cpu_map_count = 1;
  	pthread_mutex_unlock(&bo->cpu_access_mutex);
 
+	amdgpu_add_handle_to_table(bo);

Well that is clearly a NAK. IIRC we discussed that before and came to the conclusion that it isn't a good idea.

But it looks unrelated to the other changes, so why do you want to add it now?

Regards,
Christian.

Quoted 29 lines
amdgpu/amdgpu_bo.c +1 -0
+
  	*cpu = ptr;
  	return 0;
  }
amdgpu/amdgpu_internal.h +8 -2
index 0508131..e1a6559 100644
@@ -30,6 +30,9 @@
 
  #include <assert.h>
  #include <pthread.h>
+#include <stdlib.h>
+
+#include "libdrm_macros.h"
  #include "xf86atomic.h"
  #include "amdgpu.h"
  #include "util_double_list.h"
@@ -193,8 +196,18 @@ static inline bool update_references(atomic_t *dst, atomic_t *src)
  static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
  					struct amdgpu_bo *src)
  {
-	if (update_references(&(*dst)->refcount, &src->refcount))
-		amdgpu_bo_free_internal(*dst);
+	struct amdgpu_bo* bo = *dst;
+	assert(bo!=NULL);
+
+	pthread_mutex_lock(&bo->dev->bo_table_mutex);
+

amdgpu/amdgpu_internal.h static inline bool update_references(atomic_t *dst, atomic_t *src) · line 213

Quoted 10 lines
amdgpu/amdgpu_internal.h +7 -0
+	if (update_references(&bo->refcount, src?&src->refcount:NULL)) {
+		amdgpu_bo_free_internal(bo);
+		pthread_mutex_destroy(&bo->cpu_access_mutex);
+		pthread_mutex_unlock(&bo->dev->bo_table_mutex);
+		free(bo);
+	} else
+		pthread_mutex_unlock(&bo->dev->bo_table_mutex);
  	*dst = src;
  }
 

_______________________________________________
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): 48f75205-c857-6ed0-c720-644f0a2e07a9-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

author
#4 lore raw

Christian

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

Quoted 7 lines

output->buf_handle = bo;
output->alloc_size = bo->alloc_size; @@ -466,6 +461,8 @@ int
amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)
bo->cpu_map_count = 1; pthread_mutex_unlock(&bo->cpu_access_mutex);

amdgpu/amdgpu_bo.c +1 -0
+	amdgpu_add_handle_to_table(bo);

This one is a wrong base cherry-pick issue, I didn't tend to add this line, sorry for confuse .... my bad and thanks for the catch

amdgpu/amdgpu_bo.c int amdgpu_bo_import(amdgpu_device_handle dev, · line 297

Quoted 7 lines

int amdgpu_bo_import(amdgpu_device_handle dev,
/* Get a KMS handle. */
r = drmPrimeFDToHandle(dev->fd, shared_handle, &handle); if (r) {

amdgpu/amdgpu_bo.c +1 -0
+			pthread_mutex_unlock(&dev->bo_table_mutex);
  			return r;
  		}

This is another fix, I think it can be separated with main patch, this is just an obvious mutex_unlock missing in our code

For the main patch, I rework one and will sent for review again

BR Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: Monday, August 7, 2017 9:32 PM
To: Liu, Monk <[email protected]>; [email protected]
Subject: Re: [PATCH libdrm] drm: fix race issue

Am 07.08.2017 um 13:44 schrieb Monk Liu:

Quoted 9 lines

From: Monk Liu <[email protected]>

there is race issue between two threads on amdgpu_bo_free and amdgpu_bo_import, this patch tend to fix it by moving the pthread_mutex_lock out of bo_internal and cover the update_reference function.

besides the mutex_unlock in bo_import should also cover bo refcount increasement.

Nice catch, but the solution is to heavy.

Quoted 51 lines
Change-Id I8ec5a2879dda0e6468864fc64e6b46059482998b
---
  amdgpu/amdgpu_bo.c       | 13 +++++--------
  amdgpu/amdgpu_internal.h | 17 +++++++++++++++--
  2 files changed, 20 insertions(+), 10 deletions(-)
amdgpu/amdgpu_bo.c index renamed from amdgpu/amdgpu_bo.c +0 -0

ec99488..10a3efe 100644

amdgpu/amdgpu_bo.c +1 -6
@@ -52,27 +52,22 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
  	drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &args);
  }
 
-void amdgpu_bo_free_internal(amdgpu_bo_handle bo)
+drm_private void amdgpu_bo_free_internal(amdgpu_bo_handle bo)
  {
  	/* Remove the buffer from the hash tables. */
-	pthread_mutex_lock(&bo->dev->bo_table_mutex);
  	util_hash_table_remove(bo->dev->bo_handles,
  			       (void*)(uintptr_t)bo->handle);
  	if (bo->flink_name) {
  		util_hash_table_remove(bo->dev->bo_flink_names,
  				       (void*)(uintptr_t)bo->flink_name);
  	}
-	pthread_mutex_unlock(&bo->dev->bo_table_mutex);
 
  	/* Release CPU access. */
  	if (bo->cpu_map_count > 0) {
  		bo->cpu_map_count = 1;
  		amdgpu_bo_cpu_unmap(bo);
  	}
-
  	amdgpu_close_kms_handle(bo->dev, bo->handle);
-	pthread_mutex_destroy(&bo->cpu_access_mutex);
-	free(bo);
  }
 
  int amdgpu_bo_alloc(amdgpu_device_handle dev, @@ -297,6 +292,7 @@ 

int amdgpu_bo_import(amdgpu_device_handle dev,
/* Get a KMS handle. */
r = drmPrimeFDToHandle(dev->fd, shared_handle, &handle); if (r) {

amdgpu/amdgpu_bo.c +1 -0
+			pthread_mutex_unlock(&dev->bo_table_mutex);
  			return r;
  		}

amdgpu/amdgpu_bo.c int amdgpu_bo_import(amdgpu_device_handle dev, · line 340

Quoted 8 lines
amdgpu/amdgpu_bo.c +1 -2
@@ -339,10 +335,9 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
  	}
 
  	if (bo) {
-		pthread_mutex_unlock(&dev->bo_table_mutex);
-
  		/* The buffer already exists, just bump the refcount. */
  		atomic_inc(&bo->refcount);
+		pthread_mutex_unlock(&dev->bo_table_mutex);

Actually the problem is only here, that one needs to use
atomic_inc_unless() before the mutex is dropped and return only if we could successfully acquire a reference to the BO.

All other changes are unnecessary.

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

Quoted 8 lines
amdgpu/amdgpu_bo.c +0 -0
  		output->buf_handle = bo;
  		output->alloc_size = bo->alloc_size; @@ -466,6 +461,8 @@ int 
 amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)
  	bo->cpu_map_count = 1;
  	pthread_mutex_unlock(&bo->cpu_access_mutex);
amdgpu/amdgpu_bo.c +1 -0
+	amdgpu_add_handle_to_table(bo);

Well that is clearly a NAK. IIRC we discussed that before and came to the conclusion that it isn't a good idea.

But it looks unrelated to the other changes, so why do you want to add it now?

Regards,
Christian.

Quoted 29 lines
amdgpu/amdgpu_bo.c +1 -0
+
  	*cpu = ptr;
  	return 0;
  }
amdgpu/amdgpu_internal.h index renamed from amdgpu/amdgpu_internal.h +0 -0

0508131..e1a6559 100644

amdgpu/amdgpu_internal.h +8 -2
@@ -30,6 +30,9 @@
 
  #include <assert.h>
  #include <pthread.h>
+#include <stdlib.h>
+
+#include "libdrm_macros.h"
  #include "xf86atomic.h"
  #include "amdgpu.h"
  #include "util_double_list.h"
@@ -193,8 +196,18 @@ static inline bool update_references(atomic_t *dst, atomic_t *src)
  static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
  					struct amdgpu_bo *src)
  {
-	if (update_references(&(*dst)->refcount, &src->refcount))
-		amdgpu_bo_free_internal(*dst);
+	struct amdgpu_bo* bo = *dst;
+	assert(bo!=NULL);
+
+	pthread_mutex_lock(&bo->dev->bo_table_mutex);
+

amdgpu/amdgpu_internal.h static inline bool update_references(atomic_t *dst, atomic_t *src) · line 213

Quoted 10 lines
amdgpu/amdgpu_internal.h +7 -0
+	if (update_references(&bo->refcount, src?&src->refcount:NULL)) {
+		amdgpu_bo_free_internal(bo);
+		pthread_mutex_destroy(&bo->cpu_access_mutex);
+		pthread_mutex_unlock(&bo->dev->bo_table_mutex);
+		free(bo);
+	} else
+		pthread_mutex_unlock(&bo->dev->bo_table_mutex);
  	*dst = src;
  }
 

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