summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonk Liu <monk.liu@amd.com>2017-08-07 22:27:10 +0800
committerMonk Liu <Monk.Liu@amd.com>2017-08-10 13:17:32 +0800
commite53d1bf32e1f449383939fb89e2771eff5fda98a (patch)
tree0ee79b0c58a4da2173d2a60bdcd3b64eaff128a1
parenta9c6b74fcb4494a391ec6e0e29ec5505cabd16cc (diff)
drm:fix race issue between two bo functions(v2)
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 Signed-off-by: Monk Liu <monk.liu@amd.com> Reviewed-by: Christian K├Ânig <christian.koenig@amd.com> Acked-by: Qiang Yu <Qiang.Yu@amd.com>
-rw-r--r--amdgpu/amdgpu_bo.c5
-rw-r--r--amdgpu/amdgpu_internal.h13
2 files changed, 12 insertions, 6 deletions
diff --git a/amdgpu/amdgpu_bo.c b/amdgpu/amdgpu_bo.c
index 76126a2c..09028c73 100644
--- a/amdgpu/amdgpu_bo.c
+++ b/amdgpu/amdgpu_bo.c
@@ -57,14 +57,12 @@ static void amdgpu_close_kms_handle(amdgpu_device_handle dev,
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) {
@@ -344,10 +342,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 d99e4c0d..640300f5 100644
--- a/amdgpu/amdgpu_internal.h
+++ b/amdgpu/amdgpu_internal.h
@@ -239,8 +239,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;
}