Skip to contentSkip to conversation

drm: fix missing mutex unlock before return

PATCH In review Monk Liu 3 participants 9 messages Aug 7, 2017 – Aug 8, 2017 5 messages not in the archive

lore
Share & tools
lore

Monk Liu

author
#0 lore raw

PATCH 1/2 drm: fix missing mutex unlock before return

From: Monk Liu <[email protected]>

Change-Id I377dde976648d53bc9a3a2d5ba294c284910b109
---
 amdgpu/amdgpu_bo.c | 1 +
 1 file changed, 1 insertion(+)
amdgpu/amdgpu_bo.c +1 -0
index ec99488..07eb743 100644
@@ -297,6 +297,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;
 		}
 
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): 1502116512-14264-1-git-send-email-Monk.Liu-5C7GfCeVMHo@public.gmane.org

author
#2 lore raw

PATCH 2/2 drm:fix race issue between two bo functions(v2)

From: Monk Liu <[email protected]>

there is race issue between two threads on amdgpu_bo_reference and amdgpu_bo_import, this patch tends to fix it by moving the pthread_mutex_lock out of bo_free_internal and move to bo_reference to cover the update_reference part.

The mutex_unlock in bo_import should also cover bo refcount increasement.

Change-Id I1f65eacf74cd28cc0d3a71ef2f7a19b890d63c29
---
 amdgpu/amdgpu_bo.c       |  5 +----
 amdgpu/amdgpu_internal.h | 15 ++++++++++++---
 2 files changed, 13 insertions(+), 7 deletions(-)
amdgpu/amdgpu_bo.c +1 -4
index 07eb743..82f38c0 100644
@@ -55,14 +55,12 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
 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) {
@@ -340,10 +338,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;
amdgpu/amdgpu_internal.h +12 -3
index 0508131..79da0e7 100644
@@ -143,7 +143,7 @@ void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
 uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
 				uint64_t alignment, uint64_t base_required);
 
-void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, 
+void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
 				uint64_t size);
 
 int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
@@ -193,8 +193,17 @@ 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);
+	pthread_mutex_t *mlock;
+	struct amdgpu_bo* bo = *dst;
+
+	assert(bo != NULL);
+	mlock = &bo->dev->bo_table_mutex;
+	pthread_mutex_lock(mlock);
+
+	if (update_references(&bo->refcount, src?&src->refcount:NULL))
+		amdgpu_bo_free_internal(bo);
+
+	pthread_mutex_unlock(mlock);
 	*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): 1502116512-14264-2-git-send-email-Monk.Liu-5C7GfCeVMHo@public.gmane.org

#4 lore raw

Re: [PATCH libdrm 2/2] drm:fix race issue between two bo functions(v2)

Am 07.08.2017 um 16:35 schrieb Monk Liu:

Quoted 37 lines

From: Monk Liu <[email protected]>

there is race issue between two threads on amdgpu_bo_reference and amdgpu_bo_import, this patch tends to fix it by moving the pthread_mutex_lock out of bo_free_internal and move to bo_reference to cover the update_reference part.

The mutex_unlock in bo_import should also cover bo refcount increasement.

Change-Id I1f65eacf74cd28cc0d3a71ef2f7a19b890d63c29
---
  amdgpu/amdgpu_bo.c       |  5 +----
  amdgpu/amdgpu_internal.h | 15 ++++++++++++---
  2 files changed, 13 insertions(+), 7 deletions(-)
amdgpu/amdgpu_bo.c +0 -2
index 07eb743..82f38c0 100644
@@ -55,14 +55,12 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
  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) {

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

Quoted 8 lines
amdgpu/amdgpu_bo.c +1 -2
@@ -340,10 +338,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);

This could be improved a bit. Do something like the following here:

if (bo && atomic_inc_return(&bo->refcount) > 1) {
    pthread_mutex_unlock(&dev->bo_table_mutex);
...
} else {
    /* The BO is about to be destroyed, just create a new one */
    atomic_dec(&bo->refcount)
}

And then change amdgpu_bo_free_internal() to remove the key only when the BO is still the correct one.

BTW: You can completely merge amdgpu_bo_reference() and amdgpu_bo_free_internal() into amdgpu_bo_free() if you like.

Regards,
Christian.

Quoted 26 lines
amdgpu/amdgpu_bo.c +0 -0
  		output->buf_handle = bo;
  		output->alloc_size = bo->alloc_size;
amdgpu/amdgpu_internal.h +5 -3
index 0508131..79da0e7 100644
@@ -143,7 +143,7 @@ void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
  uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
  				uint64_t alignment, uint64_t base_required);
 
-void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
+void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
  				uint64_t size);
 
  int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
@@ -193,8 +193,17 @@ 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);
+	pthread_mutex_t *mlock;
+	struct amdgpu_bo* bo = *dst;
+
+	assert(bo != NULL);

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

Quoted 10 lines
amdgpu/amdgpu_internal.h +7 -0
+	mlock = &bo->dev->bo_table_mutex;
+	pthread_mutex_lock(mlock);
+
+	if (update_references(&bo->refcount, src?&src->refcount:NULL))
+		amdgpu_bo_free_internal(bo);
+
+	pthread_mutex_unlock(mlock);
  	*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): 2a753181-4ba6-1b8a-e9d1-55acd2470572-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

author
#6 lore raw

RE: [PATCH libdrm 2/2] drm:fix race issue between two bo functions(v2)

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

Quoted 9 lines
amdgpu/amdgpu_bo.c +1 -2
@@ -340,10 +338,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);

This could be improved a bit. Do something like the following here:

if (bo && atomic_inc_return(&bo->refcount) > 1) {
     pthread_mutex_unlock(&dev->bo_table_mutex);
...
} else {
     /* The BO is about to be destroyed, just create a new one */
     atomic_dec(&bo->refcount)
}

And then change amdgpu_bo_free_internal() to remove the key only when the BO is still the correct one.

I didn't get your point, can you paste your patch to show me what's your suggestion ? thanks

BR Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: Monday, August 7, 2017 11:03 PM
To: Liu, Monk <[email protected]>; [email protected]
Subject: Re: [PATCH libdrm 2/2] drm:fix race issue between two bo functions(v2)

Am 07.08.2017 um 16:35 schrieb Monk Liu:

Quoted 37 lines

From: Monk Liu <[email protected]>

there is race issue between two threads on amdgpu_bo_reference and amdgpu_bo_import, this patch tends to fix it by moving the pthread_mutex_lock out of bo_free_internal and move to bo_reference to cover the update_reference part.

The mutex_unlock in bo_import should also cover bo refcount increasement.

Change-Id I1f65eacf74cd28cc0d3a71ef2f7a19b890d63c29
---
  amdgpu/amdgpu_bo.c       |  5 +----
  amdgpu/amdgpu_internal.h | 15 ++++++++++++---
  2 files changed, 13 insertions(+), 7 deletions(-)
amdgpu/amdgpu_bo.c index renamed from amdgpu/amdgpu_bo.c +0 -0

07eb743..82f38c0 100644

amdgpu/amdgpu_bo.c +0 -2
@@ -55,14 +55,12 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
  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) {

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

Quoted 8 lines
amdgpu/amdgpu_bo.c +1 -2
@@ -340,10 +338,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);

This could be improved a bit. Do something like the following here:

if (bo && atomic_inc_return(&bo->refcount) > 1) {
     pthread_mutex_unlock(&dev->bo_table_mutex);
...
} else {
     /* The BO is about to be destroyed, just create a new one */
     atomic_dec(&bo->refcount)
}

And then change amdgpu_bo_free_internal() to remove the key only when the BO is still the correct one.

BTW: You can completely merge amdgpu_bo_reference() and amdgpu_bo_free_internal() into amdgpu_bo_free() if you like.

Regards,
Christian.

Quoted 26 lines
amdgpu/amdgpu_bo.c +0 -0
  		output->buf_handle = bo;
  		output->alloc_size = bo->alloc_size; diff --git 

a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h index 0508131..79da0e7 100644

amdgpu/amdgpu_internal.h +5 -3
@@ -143,7 +143,7 @@ void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
  uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
  				uint64_t alignment, uint64_t base_required);
 
-void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
+void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
  				uint64_t size);
 
  int amdgpu_query_gpu_info_init(amdgpu_device_handle dev); @@ -193,8 
+193,17 @@ 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);
+	pthread_mutex_t *mlock;
+	struct amdgpu_bo* bo = *dst;
+

amdgpu/amdgpu_internal.h void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);

Quoted 10 lines
amdgpu/amdgpu_internal.h +8 -0
+	assert(bo != NULL);
+	mlock = &bo->dev->bo_table_mutex;
+	pthread_mutex_lock(mlock);
+
+	if (update_references(&bo->refcount, src?&src->refcount:NULL))
+		amdgpu_bo_free_internal(bo);
+
+	pthread_mutex_unlock(mlock);
  	*dst = src;
  }

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

author
#7 lore raw

RE: [PATCH libdrm 2/2] drm:fix race issue between two bo functions(v2)

In reply to a message not in the archive

Christian

See below code (if it is what you mean) and it shows why I don't understand your point:

(1) I assume you tend to change to below logic:

	/* The buffer already exists, just bump the refcount. */
	if (bo && atomic_inc_return(&dev->bo_table_mutex) > 1) {0
		pthread_mutex_unlock(&dev->bo_table_mutex);

		output->buf_handle = bo;
		output->alloc_size = bo->alloc_size;
		return 0;
	} else {
		atomic_dec(&bo->refcount); //potential segment fault 
	}

If bo is NULL, above function will get you segment fault when CPU go to line "atomic_dec ..."

Besides, I think my original logic works fine, no need to change it at all,

(2)

Quoted 1 line

And then change amdgpu_bo_free_internal() to remove the key only when the BO is still the correct one.

I don't understand above move, can you give me the details ?

(3)

Quoted 2 lines

BTW: You can completely merge amdgpu_bo_reference() and amdgpu_bo_free_internal() into amdgpu_bo_free() if you like.

NO, really don't want, because amdgpu_Bo_reference is used widely in UMD and I don't want to change UMD at all Bo_reference have no way to merge with bo_free_internal, while bo_free_internal() can be merged with bo_free()

BR Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: Monday, August 7, 2017 11:03 PM
To: Liu, Monk <[email protected]>; [email protected]
Subject: Re: [PATCH libdrm 2/2] drm:fix race issue between two bo functions(v2)

Am 07.08.2017 um 16:35 schrieb Monk Liu:

Quoted 37 lines

From: Monk Liu <[email protected]>

there is race issue between two threads on amdgpu_bo_reference and amdgpu_bo_import, this patch tends to fix it by moving the pthread_mutex_lock out of bo_free_internal and move to bo_reference to cover the update_reference part.

The mutex_unlock in bo_import should also cover bo refcount increasement.

Change-Id I1f65eacf74cd28cc0d3a71ef2f7a19b890d63c29
---
  amdgpu/amdgpu_bo.c       |  5 +----
  amdgpu/amdgpu_internal.h | 15 ++++++++++++---
  2 files changed, 13 insertions(+), 7 deletions(-)
amdgpu/amdgpu_bo.c index renamed from amdgpu/amdgpu_bo.c +0 -0

07eb743..82f38c0 100644

amdgpu/amdgpu_bo.c +0 -2
@@ -55,14 +55,12 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
  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) {

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

Quoted 8 lines
amdgpu/amdgpu_bo.c +1 -2
@@ -340,10 +338,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);

This could be improved a bit. Do something like the following here:

if (bo && atomic_inc_return(&bo->refcount) > 1) {
     pthread_mutex_unlock(&dev->bo_table_mutex);
...
} else {
     /* The BO is about to be destroyed, just create a new one */
     atomic_dec(&bo->refcount)
}

And then change amdgpu_bo_free_internal() to remove the key only when the BO is still the correct one.

BTW: You can completely merge amdgpu_bo_reference() and amdgpu_bo_free_internal() into amdgpu_bo_free() if you like.

Regards,
Christian.

Quoted 26 lines
amdgpu/amdgpu_bo.c +0 -0
  		output->buf_handle = bo;
  		output->alloc_size = bo->alloc_size; diff --git 

a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h index 0508131..79da0e7 100644

amdgpu/amdgpu_internal.h +5 -3
@@ -143,7 +143,7 @@ void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
  uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
  				uint64_t alignment, uint64_t base_required);
 
-void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
+void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
  				uint64_t size);
 
  int amdgpu_query_gpu_info_init(amdgpu_device_handle dev); @@ -193,8 
+193,17 @@ 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);
+	pthread_mutex_t *mlock;
+	struct amdgpu_bo* bo = *dst;
+

amdgpu/amdgpu_internal.h void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);

Quoted 10 lines
amdgpu/amdgpu_internal.h +8 -0
+	assert(bo != NULL);
+	mlock = &bo->dev->bo_table_mutex;
+	pthread_mutex_lock(mlock);
+
+	if (update_references(&bo->refcount, src?&src->refcount:NULL))
+		amdgpu_bo_free_internal(bo);
+
+	pthread_mutex_unlock(mlock);
  	*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): BLUPR12MB0449EEED52A7C8DF4A965B3E848A0-7LeqcoF/[email protected]

author
#9 lore raw

RE: [PATCH libdrm 2/2] drm:fix race issue between two bo functions(v2)

One mistake from me:

Quoted 2 lines

BTW: You can completely merge amdgpu_bo_reference() and amdgpu_bo_free_internal() into amdgpu_bo_free() if you like.

Yes, they can be merged, will do...

BR Monk

-----Original Message-----
From: Liu, Monk
Sent: Tuesday, August 8, 2017 10:57 AM
To: 'Christian König' <[email protected]>; [email protected]
Subject: RE: [PATCH libdrm 2/2] drm:fix race issue between two bo functions(v2)

Christian

See below code (if it is what you mean) and it shows why I don't understand your point:

(1) I assume you tend to change to below logic:

	/* The buffer already exists, just bump the refcount. */
	if (bo && atomic_inc_return(&dev->bo_table_mutex) > 1) {0
		pthread_mutex_unlock(&dev->bo_table_mutex);

		output->buf_handle = bo;
		output->alloc_size = bo->alloc_size;
		return 0;
	} else {
		atomic_dec(&bo->refcount); //potential segment fault 
	}

If bo is NULL, above function will get you segment fault when CPU go to line "atomic_dec ..."

Besides, I think my original logic works fine, no need to change it at all,

(2)

Quoted 1 line

And then change amdgpu_bo_free_internal() to remove the key only when the BO is still the correct one.

I don't understand above move, can you give me the details ?

(3)

Quoted 2 lines

BTW: You can completely merge amdgpu_bo_reference() and amdgpu_bo_free_internal() into amdgpu_bo_free() if you like.

NO, really don't want, because amdgpu_Bo_reference is used widely in UMD and I don't want to change UMD at all Bo_reference have no way to merge with bo_free_internal, while bo_free_internal() can be merged with bo_free()

BR Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: Monday, August 7, 2017 11:03 PM
To: Liu, Monk <[email protected]>; [email protected]
Subject: Re: [PATCH libdrm 2/2] drm:fix race issue between two bo functions(v2)

Am 07.08.2017 um 16:35 schrieb Monk Liu:

Quoted 37 lines

From: Monk Liu <[email protected]>

there is race issue between two threads on amdgpu_bo_reference and amdgpu_bo_import, this patch tends to fix it by moving the pthread_mutex_lock out of bo_free_internal and move to bo_reference to cover the update_reference part.

The mutex_unlock in bo_import should also cover bo refcount increasement.

Change-Id I1f65eacf74cd28cc0d3a71ef2f7a19b890d63c29
---
  amdgpu/amdgpu_bo.c       |  5 +----
  amdgpu/amdgpu_internal.h | 15 ++++++++++++---
  2 files changed, 13 insertions(+), 7 deletions(-)
amdgpu/amdgpu_bo.c index renamed from amdgpu/amdgpu_bo.c +0 -0

07eb743..82f38c0 100644

amdgpu/amdgpu_bo.c +0 -2
@@ -55,14 +55,12 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
  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) {

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

Quoted 8 lines
amdgpu/amdgpu_bo.c +1 -2
@@ -340,10 +338,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);

This could be improved a bit. Do something like the following here:

if (bo && atomic_inc_return(&bo->refcount) > 1) {
     pthread_mutex_unlock(&dev->bo_table_mutex);
...
} else {
     /* The BO is about to be destroyed, just create a new one */
     atomic_dec(&bo->refcount)
}

And then change amdgpu_bo_free_internal() to remove the key only when the BO is still the correct one.

BTW: You can completely merge amdgpu_bo_reference() and amdgpu_bo_free_internal() into amdgpu_bo_free() if you like.

Regards,
Christian.

Quoted 27 lines
amdgpu/amdgpu_bo.c +0 -0
  		output->buf_handle = bo;
  		output->alloc_size = bo->alloc_size; diff --git 

a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h index 0508131..79da0e7 100644

amdgpu/amdgpu_internal.h +6 -3
@@ -143,7 +143,7 @@ void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
  uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
  				uint64_t alignment, uint64_t base_required);
 
-void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
+void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
  				uint64_t size);
 
  int amdgpu_query_gpu_info_init(amdgpu_device_handle dev); @@ -193,8
+193,17 @@ 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);
+	pthread_mutex_t *mlock;
+	struct amdgpu_bo* bo = *dst;
+

amdgpu/amdgpu_internal.h void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);

Quoted 10 lines
amdgpu/amdgpu_internal.h +8 -0
+	assert(bo != NULL);
+	mlock = &bo->dev->bo_table_mutex;
+	pthread_mutex_lock(mlock);
+
+	if (update_references(&bo->refcount, src?&src->refcount:NULL))
+		amdgpu_bo_free_internal(bo);
+
+	pthread_mutex_unlock(mlock);
  	*dst = src;
  }

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

#10 lore raw

Re: [PATCH libdrm 2/2] drm:fix race issue between two bo functions(v2)

In reply to a message not in the archive
Quoted 1 line

If bo is NULL, above function will get you segment fault when CPU go to line "atomic_dec ..."

Ah, yes of course that should be "else if (bo)".

Quoted 1 line

Besides, I think my original logic works fine, no need to change it at all,

Yeah, it was just and idea. But thinking more about it changing all that to only optimize the free function is probably not worth it.

Quoted 2 lines

NO, really don't want, because amdgpu_Bo_reference is used widely in UMD and I don't want to change UMD at all Bo_reference have no way to merge with bo_free_internal, while bo_free_internal() can be merged with bo_free()

Hui, what? amdgpu_Bo_reference is and internal only function, it should absolutely not be used in the UMD.

Regards,
Christian.

Am 08.08.2017 um 04:54 schrieb Liu, Monk:

Quoted 137 lines

Christian

See below code (if it is what you mean) and it shows why I don't understand your point:

(1) I assume you tend to change to below logic:

	/* The buffer already exists, just bump the refcount. */
	if (bo && atomic_inc_return(&dev->bo_table_mutex) > 1) {0
		pthread_mutex_unlock(&dev->bo_table_mutex);

		output->buf_handle = bo;
		output->alloc_size = bo->alloc_size;
		return 0;
	} else {
		atomic_dec(&bo->refcount); //potential segment fault
	}

If bo is NULL, above function will get you segment fault when CPU go to line "atomic_dec ..."

Besides, I think my original logic works fine, no need to change it at all,

(2)

Quoted 1 line

And then change amdgpu_bo_free_internal() to remove the key only when the BO is still the correct one.

I don't understand above move, can you give me the details ?

(3)

Quoted 2 lines

BTW: You can completely merge amdgpu_bo_reference() and amdgpu_bo_free_internal() into amdgpu_bo_free() if you like.

NO, really don't want, because amdgpu_Bo_reference is used widely in UMD and I don't want to change UMD at all Bo_reference have no way to merge with bo_free_internal, while bo_free_internal() can be merged with bo_free()

BR Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: Monday, August 7, 2017 11:03 PM
To: Liu, Monk <[email protected]>; [email protected]
Subject: Re: [PATCH libdrm 2/2] drm:fix race issue between two bo functions(v2)

Am 07.08.2017 um 16:35 schrieb Monk Liu:

Quoted 37 lines

From: Monk Liu <[email protected]>

there is race issue between two threads on amdgpu_bo_reference and amdgpu_bo_import, this patch tends to fix it by moving the pthread_mutex_lock out of bo_free_internal and move to bo_reference to cover the update_reference part.

The mutex_unlock in bo_import should also cover bo refcount increasement.

Change-Id I1f65eacf74cd28cc0d3a71ef2f7a19b890d63c29
---
   amdgpu/amdgpu_bo.c       |  5 +----
   amdgpu/amdgpu_internal.h | 15 ++++++++++++---
   2 files changed, 13 insertions(+), 7 deletions(-)
amdgpu/amdgpu_bo.c index renamed from amdgpu/amdgpu_bo.c +0 -0

07eb743..82f38c0 100644

amdgpu/amdgpu_bo.c +0 -2
@@ -55,14 +55,12 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
   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) {

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

Quoted 8 lines
amdgpu/amdgpu_bo.c +1 -2
@@ -340,10 +338,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);

This could be improved a bit. Do something like the following here:

if (bo && atomic_inc_return(&bo->refcount) > 1) {
      pthread_mutex_unlock(&dev->bo_table_mutex);
...
} else {
      /* The BO is about to be destroyed, just create a new one */
      atomic_dec(&bo->refcount)
}

And then change amdgpu_bo_free_internal() to remove the key only when the BO is still the correct one.

BTW: You can completely merge amdgpu_bo_reference() and amdgpu_bo_free_internal() into amdgpu_bo_free() if you like.

Regards,
Christian.

Quoted 28 lines
amdgpu/amdgpu_bo.c +0 -0
   		output->buf_handle = bo;
   		output->alloc_size = bo->alloc_size; diff --git

a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h index 0508131..79da0e7 100644

amdgpu/amdgpu_internal.h +8 -3
@@ -143,7 +143,7 @@ void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
   uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
   				uint64_t alignment, uint64_t base_required);
 
-void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
+void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
   				uint64_t size);
 
   int amdgpu_query_gpu_info_init(amdgpu_device_handle dev); @@ -193,8
+193,17 @@ 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);
+	pthread_mutex_t *mlock;
+	struct amdgpu_bo* bo = *dst;
+
+	assert(bo != NULL);
+	mlock = &bo->dev->bo_table_mutex;
+	pthread_mutex_lock(mlock);
Quoted 9 lines

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

Quoted 8 lines
amdgpu/amdgpu_internal.h +5 -0
+
+	if (update_references(&bo->refcount, src?&src->refcount:NULL))
+		amdgpu_bo_free_internal(bo);
+
+	pthread_mutex_unlock(mlock);
   	*dst = src;
   }

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

Reviewed-by
#11 lore raw
In reply to a message not in the archive

Am 07.08.2017 um 16:35 schrieb Monk Liu:

Quoted 4 lines

From: Monk Liu <[email protected]>

Change-Id I377dde976648d53bc9a3a2d5ba294c284910b109
Quoted 9 lines
---
  amdgpu/amdgpu_bo.c | 1 +
  1 file changed, 1 insertion(+)
amdgpu/amdgpu_bo.c +0 -0
index ec99488..07eb743 100644

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

Quoted 7 lines
amdgpu/amdgpu_bo.c +1 -0
@@ -297,6 +297,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;
  		}
 

_______________________________________________
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): 3fa1478e-4701-343c-0288-e8c41eb1f2c4-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#13 lore raw
Quoted 9 lines · Christian König

Am 07.08.2017 um 16:35 schrieb Monk Liu:

Quoted 5 lines

From: Monk Liu <[email protected]>

Change-Id I377dde976648d53bc9a3a2d5ba294c284910b109

Applied upstream. thanks!

Alex

Quoted 16 lines
Quoted 16 lines
---
  amdgpu/amdgpu_bo.c | 1 +
  1 file changed, 1 insertion(+)
amdgpu/amdgpu_bo.c +1 -0
index ec99488..07eb743 100644
@@ -297,6 +297,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;
                }
Quoted 8 lines
Quoted 1 line

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

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