Skip to contentSkip to conversation

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

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

lore
Share & tools
lore

Monk Liu

author
#0 lore raw

PATCH 1/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): 1502177661-1430-1-git-send-email-Monk.Liu-5C7GfCeVMHo@public.gmane.org

author
#2 lore raw

PATCH 2/2 drm: merge and cleanup amdgpu_bo_free

From: Monk Liu <[email protected]>

since bo_reference and bo_internal_free are
all only used by bo_free, so we just merge them
together

Change-Id I72ebc9c8bcfcd23f4d52a1385db871a95c23859e
---
 amdgpu/amdgpu_bo.c       | 52 +++++++++++++++++++++++++++---------------------
 amdgpu/amdgpu_internal.h | 33 ------------------------------
 2 files changed, 29 insertions(+), 56 deletions(-)
amdgpu/amdgpu_bo.c +29 -23
index 82f38c0..901673a 100644
@@ -52,27 +52,6 @@ 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)
-{
-	/* Remove the buffer from the hash tables. */
-	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);
-	}
-
-	/* 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,
 		    struct amdgpu_bo_alloc_request *alloc_buffer,
 		    amdgpu_bo_handle *buf_handle)
@@ -415,8 +394,35 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
 
 int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
 {
-	/* Just drop the reference. */
-	amdgpu_bo_reference(&buf_handle, NULL);
+	struct amdgpu_device *dev;
+	struct amdgpu_bo *bo = buf_handle;
+
+	assert(bo != NULL);
+	dev = bo->dev;
+	pthread_mutex_lock(&dev->bo_table_mutex);
+
+	if (update_references(&bo->refcount, NULL)) {
+		/* Remove the buffer from the hash tables. */
+		util_hash_table_remove(dev->bo_handles,
+					(void*)(uintptr_t)bo->handle);
+
+		if (bo->flink_name) {
+			util_hash_table_remove(dev->bo_flink_names,
+						(void*)(uintptr_t)bo->flink_name);
+		}
+
+		/* Release CPU access. */
+		if (bo->cpu_map_count > 0) {
+			bo->cpu_map_count = 1;
+			amdgpu_bo_cpu_unmap(bo);
+		}
+
+		amdgpu_close_kms_handle(dev, bo->handle);
+		pthread_mutex_destroy(&bo->cpu_access_mutex);
+		free(bo);
+	}
+
+	pthread_mutex_unlock(&dev->bo_table_mutex);
 	return 0;
 }
 
amdgpu/amdgpu_internal.h +0 -33
index 79da0e7..a6bf831 100644
@@ -133,8 +133,6 @@ struct amdgpu_semaphore {
 
 void amdgpu_device_free_internal(amdgpu_device_handle dev);
 
-void amdgpu_bo_free_internal(amdgpu_bo_handle bo);
-
 void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
 		       uint64_t max, uint64_t alignment);
 
@@ -177,37 +175,6 @@ static inline bool update_references(atomic_t *dst, atomic_t *src)
 }
 
 /**
- * Assignment between two amdgpu_bo pointers with reference counting.
- *
- * Usage:
- *    struct amdgpu_bo *dst = ... , *src = ...;
- *
- *    dst = src;
- *    // No reference counting. Only use this when you need to move
- *    // a reference from one pointer to another.
- *
- *    amdgpu_bo_reference(&dst, src);
- *    // Reference counters are updated. dst is decremented and src is
- *    // incremented. dst is freed if its reference counter is 0.
- */
-static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
-					struct amdgpu_bo *src)
-{
-	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;
-}
-
-/**
  * Assignment between two amdgpu_device pointers with reference counting.
  *
  * Usage:
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): 1502177661-1430-2-git-send-email-Monk.Liu-5C7GfCeVMHo@public.gmane.org

Reviewed-by
#4 lore raw

Re: [PATCH libdrm 2/2] drm: merge and cleanup amdgpu_bo_free

Am 08.08.2017 um 09:34 schrieb Monk Liu:

Quoted 8 lines

From: Monk Liu <[email protected]>

since bo_reference and bo_internal_free are
all only used by bo_free, so we just merge them
together

Change-Id I72ebc9c8bcfcd23f4d52a1385db871a95c23859e
Quoted 116 lines
---
  amdgpu/amdgpu_bo.c       | 52 +++++++++++++++++++++++++++---------------------
  amdgpu/amdgpu_internal.h | 33 ------------------------------
  2 files changed, 29 insertions(+), 56 deletions(-)
amdgpu/amdgpu_bo.c +29 -23
index 82f38c0..901673a 100644
@@ -52,27 +52,6 @@ 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)
-{
-	/* Remove the buffer from the hash tables. */
-	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);
-	}
-
-	/* 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,
  		    struct amdgpu_bo_alloc_request *alloc_buffer,
  		    amdgpu_bo_handle *buf_handle)
@@ -415,8 +394,35 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
 
  int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
  {
-	/* Just drop the reference. */
-	amdgpu_bo_reference(&buf_handle, NULL);
+	struct amdgpu_device *dev;
+	struct amdgpu_bo *bo = buf_handle;
+
+	assert(bo != NULL);
+	dev = bo->dev;
+	pthread_mutex_lock(&dev->bo_table_mutex);
+
+	if (update_references(&bo->refcount, NULL)) {
+		/* Remove the buffer from the hash tables. */
+		util_hash_table_remove(dev->bo_handles,
+					(void*)(uintptr_t)bo->handle);
+
+		if (bo->flink_name) {
+			util_hash_table_remove(dev->bo_flink_names,
+						(void*)(uintptr_t)bo->flink_name);
+		}
+
+		/* Release CPU access. */
+		if (bo->cpu_map_count > 0) {
+			bo->cpu_map_count = 1;
+			amdgpu_bo_cpu_unmap(bo);
+		}
+
+		amdgpu_close_kms_handle(dev, bo->handle);
+		pthread_mutex_destroy(&bo->cpu_access_mutex);
+		free(bo);
+	}
+
+	pthread_mutex_unlock(&dev->bo_table_mutex);
  	return 0;
  }
 
amdgpu/amdgpu_internal.h +0 -26
index 79da0e7..a6bf831 100644
@@ -133,8 +133,6 @@ struct amdgpu_semaphore {
 
  void amdgpu_device_free_internal(amdgpu_device_handle dev);
 
-void amdgpu_bo_free_internal(amdgpu_bo_handle bo);
-
  void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
  		       uint64_t max, uint64_t alignment);
 
@@ -177,37 +175,6 @@ static inline bool update_references(atomic_t *dst, atomic_t *src)
  }
 
  /**
- * Assignment between two amdgpu_bo pointers with reference counting.
- *
- * Usage:
- *    struct amdgpu_bo *dst = ... , *src = ...;
- *
- *    dst = src;
- *    // No reference counting. Only use this when you need to move
- *    // a reference from one pointer to another.
- *
- *    amdgpu_bo_reference(&dst, src);
- *    // Reference counters are updated. dst is decremented and src is
- *    // incremented. dst is freed if its reference counter is 0.
- */
-static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
-					struct amdgpu_bo *src)
-{
-	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/amdgpu_internal.h static inline bool update_references(atomic_t *dst, atomic_t *src) · line 180

Quoted 10 lines
amdgpu/amdgpu_internal.h +0 -7
-		amdgpu_bo_free_internal(bo);
-
-	pthread_mutex_unlock(mlock);
-	*dst = src;
-}
-
-/**
   * Assignment between two amdgpu_device pointers with reference counting.
   *
   * Usage:

_______________________________________________
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): bbcaecea-f2a1-7dec-5fca-69bad4f7176d-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#6 lore raw

Re: [PATCH libdrm 2/2] drm: merge and cleanup amdgpu_bo_free

Quoted 8 lines · Christian König

Am 08.08.2017 um 09:34 schrieb Monk Liu:

Quoted 7 lines

From: Monk Liu <[email protected]>

since bo_reference and bo_internal_free are
all only used by bo_free, so we just merge them
together

Quoted 5 lines
Quoted 2 lines
Change-Id I72ebc9c8bcfcd23f4d52a1385db871a95c23859e

Pushed both upstream. Thanks!

Alex

Quoted 125 lines
Quoted 125 lines

---
  amdgpu/amdgpu_bo.c       | 52
+++++++++++++++++++++++++++---------------------
  amdgpu/amdgpu_internal.h | 33 ------------------------------
  2 files changed, 29 insertions(+), 56 deletions(-)
amdgpu/amdgpu_bo.c +29 -22
index 82f38c0..901673a 100644
@@ -52,27 +52,6 @@ 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)
-{
-       /* Remove the buffer from the hash tables. */
-       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);
-       }
-
-       /* 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,
                    struct amdgpu_bo_alloc_request *alloc_buffer,
                    amdgpu_bo_handle *buf_handle)
@@ -415,8 +394,35 @@ int amdgpu_bo_import(amdgpu_device_handle dev,
    int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
  {
-       /* Just drop the reference. */
-       amdgpu_bo_reference(&buf_handle, NULL);
+       struct amdgpu_device *dev;
+       struct amdgpu_bo *bo = buf_handle;
+
+       assert(bo != NULL);
+       dev = bo->dev;
+       pthread_mutex_lock(&dev->bo_table_mutex);
+
+       if (update_references(&bo->refcount, NULL)) {
+               /* Remove the buffer from the hash tables. */
+               util_hash_table_remove(dev->bo_handles,
+                                       (void*)(uintptr_t)bo->handle);
+
+               if (bo->flink_name) {
+                       util_hash_table_remove(dev->bo_flink_names,
+
 (void*)(uintptr_t)bo->flink_name);
+               }
+
+               /* Release CPU access. */
+               if (bo->cpu_map_count > 0) {
+                       bo->cpu_map_count = 1;
+                       amdgpu_bo_cpu_unmap(bo);
+               }
+
+               amdgpu_close_kms_handle(dev, bo->handle);
+               pthread_mutex_destroy(&bo->cpu_access_mutex);
+               free(bo);
+       }
+
+       pthread_mutex_unlock(&dev->bo_table_mutex);
        return 0;
  }
  diff --git a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h

index 79da0e7..a6bf831 100644

amdgpu/amdgpu_internal.h +0 -32
@@ -133,8 +133,6 @@ struct amdgpu_semaphore {
    void amdgpu_device_free_internal(amdgpu_device_handle dev);
  -void amdgpu_bo_free_internal(amdgpu_bo_handle bo);
-
  void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
                       uint64_t max, uint64_t alignment);
  @@ -177,37 +175,6 @@ static inline bool update_references(atomic_t *dst,
 atomic_t *src)
  }
    /**
- * Assignment between two amdgpu_bo pointers with reference counting.
- *
- * Usage:
- *    struct amdgpu_bo *dst = ... , *src = ...;
- *
- *    dst = src;
- *    // No reference counting. Only use this when you need to move
- *    // a reference from one pointer to another.
- *
- *    amdgpu_bo_reference(&dst, src);
- *    // Reference counters are updated. dst is decremented and src is
- *    // incremented. dst is freed if its reference counter is 0.
- */
-static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
-                                       struct amdgpu_bo *src)
-{
-       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;
-}
-
-/**
   * Assignment between two amdgpu_device pointers with reference

counting.
*

Quoted 8 lines
Quoted 1 line

* Usage:

_______________________________________________
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

A message that is not in the archive (sent off-list or not yet received): CADnq5_MgXHaccrGbq3ttv_nQ6E+51o1E2GRRBiHAmDvWGSPdTA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

author
#8 lore raw

RE: [PATCH libdrm 2/2] drm: merge and cleanup amdgpu_bo_free

I pushed it to amd-staging-upstream

What else branch did you refer to ?

BR Monk

-----Original Message-----
From: Alex Deucher [mailto:[email protected]]
Sent: Wednesday, August 9, 2017 12:19 AM
To: Christian König <[email protected]>
Cc: Liu, Monk <[email protected]>; amd-gfx list <[email protected]>
Subject: Re: [PATCH libdrm 2/2] drm: merge and cleanup amdgpu_bo_free

Quoted 4 lines · Christian König

Am 08.08.2017 um 09:34 schrieb Monk Liu:

Quoted 3 lines

From: Monk Liu <[email protected]>

Quoted 8 lines
Quoted 5 lines

since bo_reference and bo_internal_free are all only used by bo_free, so we just merge them together

Change-Id I72ebc9c8bcfcd23f4d52a1385db871a95c23859e

Pushed both upstream. Thanks!

Alex

Quoted 125 lines
Quoted 125 lines

---
  amdgpu/amdgpu_bo.c       | 52
+++++++++++++++++++++++++++---------------------
  amdgpu/amdgpu_internal.h | 33 ------------------------------
  2 files changed, 29 insertions(+), 56 deletions(-)
amdgpu/amdgpu_bo.c index renamed from amdgpu/amdgpu_bo.c +0 -0

82f38c0..901673a 100644

amdgpu/amdgpu_bo.c +0 -19
@@ -52,27 +52,6 @@ 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) -{
-       /* Remove the buffer from the hash tables. */
-       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);
-       }
-
-       /* 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,
                    struct amdgpu_bo_alloc_request *alloc_buffer,
                    amdgpu_bo_handle *buf_handle) @@ -415,8 +394,35 

@@ int amdgpu_bo_import(amdgpu_device_handle dev,
int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
{

amdgpu/amdgpu_bo.c +16 -2
-       /* Just drop the reference. */
-       amdgpu_bo_reference(&buf_handle, NULL);
+       struct amdgpu_device *dev;
+       struct amdgpu_bo *bo = buf_handle;
+
+       assert(bo != NULL);
+       dev = bo->dev;
+       pthread_mutex_lock(&dev->bo_table_mutex);
+
+       if (update_references(&bo->refcount, NULL)) {
+               /* Remove the buffer from the hash tables. */
+               util_hash_table_remove(dev->bo_handles,
+                                       
+ (void*)(uintptr_t)bo->handle);
+
+               if (bo->flink_name) {
+                       util_hash_table_remove(dev->bo_flink_names,
+

(void*)(uintptr_t)bo->flink_name);

amdgpu/amdgpu_bo.c +14 -0
+               }
+
+               /* Release CPU access. */
+               if (bo->cpu_map_count > 0) {
+                       bo->cpu_map_count = 1;
+                       amdgpu_bo_cpu_unmap(bo);
+               }
+
+               amdgpu_close_kms_handle(dev, bo->handle);
+               pthread_mutex_destroy(&bo->cpu_access_mutex);
+               free(bo);
+       }
+
+       pthread_mutex_unlock(&dev->bo_table_mutex);
        return 0;
  }
  diff --git a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h 

index 79da0e7..a6bf831 100644

amdgpu/amdgpu_internal.h +0 -32
@@ -133,8 +133,6 @@ struct amdgpu_semaphore {
    void amdgpu_device_free_internal(amdgpu_device_handle dev);
  -void amdgpu_bo_free_internal(amdgpu_bo_handle bo);
-
  void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
                       uint64_t max, uint64_t alignment);
  @@ -177,37 +175,6 @@ static inline bool update_references(atomic_t 
 *dst, atomic_t *src)
  }
    /**
- * Assignment between two amdgpu_bo pointers with reference counting.
- *
- * Usage:
- *    struct amdgpu_bo *dst = ... , *src = ...;
- *
- *    dst = src;
- *    // No reference counting. Only use this when you need to move
- *    // a reference from one pointer to another.
- *
- *    amdgpu_bo_reference(&dst, src);
- *    // Reference counters are updated. dst is decremented and src is
- *    // incremented. dst is freed if its reference counter is 0.
- */
-static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
-                                       struct amdgpu_bo *src)
-{
-       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;
-}
-
-/**
   * Assignment between two amdgpu_device pointers with reference 

counting.
*

Quoted 8 lines
Quoted 1 line

* Usage:

_______________________________________________
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

A message that is not in the archive (sent off-list or not yet received): BLUPR12MB044917CC806D591517489D71848B0-7LeqcoF/[email protected]

#10 lore raw

Re: [PATCH libdrm 2/2] drm: merge and cleanup amdgpu_bo_free

Quoted 1 line · Monk

I pushed it to amd-staging-upstream

that branch is long dead. master tracks upstream now in internal git IIRC.

Quoted 2 lines

What else branch did you refer to ?

The upstream tree on freedesktop.org:
https://cgit.freedesktop.org/mesa/drm

Alex

Quoted 23 lines

BR Monk

-----Original Message-----
From: Alex Deucher [mailto:[email protected]]
Sent: Wednesday, August 9, 2017 12:19 AM
To: Christian König <[email protected]>
Cc: Liu, Monk <[email protected]>; amd-gfx list <[email protected]>
Subject: Re: [PATCH libdrm 2/2] drm: merge and cleanup amdgpu_bo_free

Quoted 11 lines · Christian König

Am 08.08.2017 um 09:34 schrieb Monk Liu:

Quoted 8 lines

From: Monk Liu <[email protected]>

since bo_reference and bo_internal_free are all only used by bo_free, so we just merge them together

Change-Id I72ebc9c8bcfcd23f4d52a1385db871a95c23859e
Quoted 139 lines
Quoted 1 line

Pushed both upstream. Thanks!

Alex

Quoted 133 lines
Quoted 123 lines

---
  amdgpu/amdgpu_bo.c       | 52
+++++++++++++++++++++++++++---------------------
  amdgpu/amdgpu_internal.h | 33 ------------------------------
  2 files changed, 29 insertions(+), 56 deletions(-)
amdgpu/amdgpu_bo.c index renamed from amdgpu/amdgpu_bo.c +0 -0

82f38c0..901673a 100644

amdgpu/amdgpu_bo.c +0 -19
@@ -52,27 +52,6 @@ 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) -{
-       /* Remove the buffer from the hash tables. */
-       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);
-       }
-
-       /* 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,
                    struct amdgpu_bo_alloc_request *alloc_buffer,
                    amdgpu_bo_handle *buf_handle) @@ -415,8 +394,35

@@ int amdgpu_bo_import(amdgpu_device_handle dev,
int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
{

amdgpu/amdgpu_bo.c +16 -2
-       /* Just drop the reference. */
-       amdgpu_bo_reference(&buf_handle, NULL);
+       struct amdgpu_device *dev;
+       struct amdgpu_bo *bo = buf_handle;
+
+       assert(bo != NULL);
+       dev = bo->dev;
+       pthread_mutex_lock(&dev->bo_table_mutex);
+
+       if (update_references(&bo->refcount, NULL)) {
+               /* Remove the buffer from the hash tables. */
+               util_hash_table_remove(dev->bo_handles,
+
+ (void*)(uintptr_t)bo->handle);
+
+               if (bo->flink_name) {
+                       util_hash_table_remove(dev->bo_flink_names,
+

(void*)(uintptr_t)bo->flink_name);

amdgpu/amdgpu_bo.c +14 -0
+               }
+
+               /* Release CPU access. */
+               if (bo->cpu_map_count > 0) {
+                       bo->cpu_map_count = 1;
+                       amdgpu_bo_cpu_unmap(bo);
+               }
+
+               amdgpu_close_kms_handle(dev, bo->handle);
+               pthread_mutex_destroy(&bo->cpu_access_mutex);
+               free(bo);
+       }
+
+       pthread_mutex_unlock(&dev->bo_table_mutex);
        return 0;
  }
  diff --git a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h

index 79da0e7..a6bf831 100644

amdgpu/amdgpu_internal.h +0 -32
@@ -133,8 +133,6 @@ struct amdgpu_semaphore {
    void amdgpu_device_free_internal(amdgpu_device_handle dev);
  -void amdgpu_bo_free_internal(amdgpu_bo_handle bo);
-
  void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
                       uint64_t max, uint64_t alignment);
  @@ -177,37 +175,6 @@ static inline bool update_references(atomic_t
 *dst, atomic_t *src)
  }
    /**
- * Assignment between two amdgpu_bo pointers with reference counting.
- *
- * Usage:
- *    struct amdgpu_bo *dst = ... , *src = ...;
- *
- *    dst = src;
- *    // No reference counting. Only use this when you need to move
- *    // a reference from one pointer to another.
- *
- *    amdgpu_bo_reference(&dst, src);
- *    // Reference counters are updated. dst is decremented and src is
- *    // incremented. dst is freed if its reference counter is 0.
- */
-static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
-                                       struct amdgpu_bo *src)
-{
-       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;
-}
-
-/**
   * Assignment between two amdgpu_device pointers with reference
Quoted 3 lines

counting.
*
* Usage:

_______________________________________________
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

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

Am 08.08.2017 um 09:34 schrieb Monk Liu:

Quoted 65 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 +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 +1 -1
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)
  {

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

Quoted 10 lines
amdgpu/amdgpu_internal.h +8 -2
-	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))

Please add some spaces in "src?&src->refcount:NULL" after each operator.

With that fixed the patch is Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

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

Quoted 6 lines
amdgpu/amdgpu_internal.h +3 -0
+		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): af7e8c05-4924-f2b5-e2e1-f7ea3fd4bf59-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

author
#13 lore raw
Quoted 3 lines

Please add some spaces in "src?&src->refcount:NULL" after each operator.

With that fixed the patch is Reviewed-by: Christian König <[email protected]>.

Actually this " src?&src->refcount:NULL" is totally removed in second cleanup patch ...

BR Monk

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

Am 08.08.2017 um 09:34 schrieb Monk Liu:

Quoted 65 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 +1 -4
@@ -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; diff --git 

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

amdgpu/amdgpu_internal.h +2 -1
@@ -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)
  {

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

Quoted 10 lines
amdgpu/amdgpu_internal.h +8 -2
-	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))

Please add some spaces in "src?&src->refcount:NULL" after each operator.

With that fixed the patch is Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

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

Quoted 6 lines
amdgpu/amdgpu_internal.h +3 -0
+		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): BLUPR12MB04499480361852F178A38899848A0-7LeqcoF/[email protected]

#15 lore raw

Am 08.08.2017 um 09:49 schrieb Liu, Monk:

Quoted 3 lines
Quoted 2 lines

Please add some spaces in "src?&src->refcount:NULL" after each operator. With that fixed the patch is Reviewed-by: Christian König <[email protected]>.

Actually this " src?&src->refcount:NULL" is totally removed in second cleanup patch ...

Yeah, that obviously works as well :)

Christian.

Quoted 92 lines

BR Monk

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

Am 08.08.2017 um 09:34 schrieb Monk Liu:

Quoted 65 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 +1 -4
@@ -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; diff --git

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

amdgpu/amdgpu_internal.h +2 -1
@@ -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)
   {

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

Quoted 10 lines
amdgpu/amdgpu_internal.h +8 -2
-	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))

Please add some spaces in "src?&src->refcount:NULL" after each operator.

With that fixed the patch is Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

Quoted 11 lines

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

Quoted 6 lines
amdgpu/amdgpu_internal.h +3 -0
+		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

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