Skip to contentSkip to conversation

drm: fix deadlock of syncobj v2

PATCH In review Chunming Zhou 3 participants 4 messages Oct 21, 2018 – Oct 22, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm: fix deadlock of syncobj v2

v2:
add a mutex between sync_cb execution and free.

Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Christian König <[email protected]>
---
 drivers/gpu/drm/drm_syncobj.c | 11 +++++++++--
 include/drm/drm_syncobj.h     |  4 ++++
 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_syncobj.c +9 -2
index 57bf6006394d..c025a0b93565 100644
@@ -158,9 +158,11 @@ void drm_syncobj_add_callback(struct drm_syncobj *syncobj,
 void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
 				 struct drm_syncobj_cb *cb)
 {
+	mutex_lock(&syncobj->mutex);
 	spin_lock(&syncobj->lock);
 	list_del_init(&cb->node);
 	spin_unlock(&syncobj->lock);
+	mutex_unlock(&syncobj->mutex);
 }
 
 static void drm_syncobj_init(struct drm_syncobj *syncobj)
@@ -344,13 +346,17 @@ void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
 	drm_syncobj_create_signal_pt(syncobj, fence, pt_value);
 	if (fence) {
 		struct drm_syncobj_cb *cur, *tmp;
+		LIST_HEAD(cb_list);
 
+		mutex_lock(&syncobj->mutex);
 		spin_lock(&syncobj->lock);
-		list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
+		list_splice_init(&syncobj->cb_list, &cb_list);
+		spin_unlock(&syncobj->lock);
+		list_for_each_entry_safe(cur, tmp, &cb_list, node) {
 			list_del_init(&cur->node);
 			cur->func(syncobj, cur);
 		}
-		spin_unlock(&syncobj->lock);
+		mutex_unlock(&syncobj->mutex);
 	}
 }
 EXPORT_SYMBOL(drm_syncobj_replace_fence);
@@ -501,6 +507,7 @@ int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
 	kref_init(&syncobj->refcount);
 	INIT_LIST_HEAD(&syncobj->cb_list);
 	spin_lock_init(&syncobj->lock);
+	mutex_init(&syncobj->mutex);
 	if (flags & DRM_SYNCOBJ_CREATE_TYPE_TIMELINE)
 		syncobj->type = DRM_SYNCOBJ_TYPE_TIMELINE;
 	else
include/drm/drm_syncobj.h +4 -0
index 5e8c5c027e09..3d3c8c181bd2 100644
@@ -78,6 +78,10 @@ struct drm_syncobj {
 	 * @lock: Protects syncobj list and write-locks &fence.
 	 */
 	spinlock_t lock;
+	/**
+	 * @mutex: mutex between syncobj cb execution and free.
+	 */
+	struct mutex mutex;
 	/**
 	 * @file: A file backing for this syncobj.
 	 */
Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

Ping...
Btw:
The patch is tested by syncobj_basic and syncobj_wait of IGT.

Quoted 84 lines

-----Original Message-----
From: Chunming Zhou <[email protected]>
Sent: Sunday, October 21, 2018 7:14 PM
To: [email protected]
Cc: Zhou, David(ChunMing) <[email protected]>; Daniel Vetter <[email protected]>; Chris Wilson <[email protected]>; Koenig, Christian <[email protected]>
Subject: [PATCH] drm: fix deadlock of syncobj v2

v2:
add a mutex between sync_cb execution and free.

Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Christian König <[email protected]>
---
 drivers/gpu/drm/drm_syncobj.c | 11 +++++++++--
 include/drm/drm_syncobj.h     |  4 ++++
 2 files changed, 13 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/drm_syncobj.c index 57bf6006394d..c025a0b93565 100644

drivers/gpu/drm/drm_syncobj.c +5 -1
@@ -158,9 +158,11 @@ void drm_syncobj_add_callback(struct drm_syncobj
 *syncobj,  void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
 				 struct drm_syncobj_cb *cb)
 {
+	mutex_lock(&syncobj->mutex);
 	spin_lock(&syncobj->lock);
 	list_del_init(&cb->node);
 	spin_unlock(&syncobj->lock);
+	mutex_unlock(&syncobj->mutex);
 }
 
 static void drm_syncobj_init(struct drm_syncobj *syncobj) @@ -344,13
+346,17 @@ void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
 	drm_syncobj_create_signal_pt(syncobj, fence, pt_value);
 	if (fence) {
 		struct drm_syncobj_cb *cur, *tmp;
+		LIST_HEAD(cb_list);
 
+		mutex_lock(&syncobj->mutex);
 		spin_lock(&syncobj->lock);
-		list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node)

{

drivers/gpu/drm/drm_syncobj.c +5 -1
+		list_splice_init(&syncobj->cb_list, &cb_list);
+		spin_unlock(&syncobj->lock);
+		list_for_each_entry_safe(cur, tmp, &cb_list, node) {
 			list_del_init(&cur->node);
 			cur->func(syncobj, cur);
 		}
-		spin_unlock(&syncobj->lock);
+		mutex_unlock(&syncobj->mutex);
 	}
 }
 EXPORT_SYMBOL(drm_syncobj_replace_fence);
@@ -501,6 +507,7 @@ int drm_syncobj_create(struct drm_syncobj
 **out_syncobj, uint32_t flags,
 	kref_init(&syncobj->refcount);
 	INIT_LIST_HEAD(&syncobj->cb_list);
 	spin_lock_init(&syncobj->lock);
+	mutex_init(&syncobj->mutex);
 	if (flags & DRM_SYNCOBJ_CREATE_TYPE_TIMELINE)
 		syncobj->type = DRM_SYNCOBJ_TYPE_TIMELINE;
drivers/gpu/drm/drm_syncobj.c +0 -0
 	else
include/drm/drm_syncobj.h index renamed from include/drm/drm_syncobj.h +0 -0

5e8c5c027e09..3d3c8c181bd2 100644

include/drm/drm_syncobj.h +4 -1
@@ -78,6 +78,10 @@ struct drm_syncobj {
 	 * @lock: Protects syncobj list and write-locks &fence.
 	 */
 	spinlock_t lock;
+	/**
+	 * @mutex: mutex between syncobj cb execution and free.
+	 */
+	struct mutex mutex;
 	/**
 	 * @file: A file backing for this syncobj.
 	 */
--
Quoted 1 line

2.17.1

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

#2 lore raw
In reply to Chunming Zhou

Quoting Chunming Zhou (2018-10-21 12:14:24)

Quoted 2 lines

v2:
add a mutex between sync_cb execution and free.

The result would appear to be that syncobj->lock is relegated to protecting the pt_list and the mutex would only be needed for the syncobj->cb_list.

The patch looks correct for resolving the deadlock and avoiding introducing any new fails, but I do wonder if

	spinlock_t pt_lock;
	struct mutex cb_mutex;

and clearly separating the roles would not be a better approach. -Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw
Quoted 7 lines · Chris Wilson

Quoting Chunming Zhou (2018-10-21 12:14:24)

Quoted 2 lines

v2:
add a mutex between sync_cb execution and free.

The result would appear to be that syncobj->lock is relegated to protecting the pt_list and the mutex would only be needed for the syncobj->cb_list.

Quoted 7 lines

The patch looks correct for resolving the deadlock and avoiding introducing any new fails, but I do wonder if

	spinlock_t pt_lock;
	struct mutex cb_mutex;

and clearly separating the roles would not be a better approach.

good idea, thanks,

-David

Quoted 1 line

-Chris

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