Skip to contentSkip to conversation

drm: fix deadlock of syncobj

PATCH In review Chunming Zhou 5 participants 7 messages Oct 19, 2018

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

PATCH 1/1 drm: fix deadlock of syncobj

Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Christian König <[email protected]>
---
 drivers/gpu/drm/drm_syncobj.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_syncobj.c +5 -2
index 57bf6006394d..2f3c14cb5156 100644
@@ -344,13 +344,16 @@ 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;
+		struct list_head cb_list;
 
+		INIT_LIST_HEAD(&cb_list);
 		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);
 	}
 }
 EXPORT_SYMBOL(drm_syncobj_replace_fence);
Signature · 6 lines
2.17.1

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

Quoting Chunming Zhou (2018-10-19 11:26:41)

Quoted 13 lines
Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Christian König <[email protected]>
---
 drivers/gpu/drm/drm_syncobj.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_syncobj.c +0 -0
index 57bf6006394d..2f3c14cb5156 100644

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 348

Quoted 5 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
@@ -344,13 +344,16 @@ 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;
+               struct list_head cb_list;
+               INIT_LIST_HEAD(&cb_list);

LIST_HEAD(cb_list); // does both in one

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 351

Quoted 3 lines
drivers/gpu/drm/drm_syncobj.c +1 -1
                spin_lock(&syncobj->lock);
-               list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
+               list_splice_init(&syncobj->cb_list, &cb_list);

Steal the snapshot of the list under the lock, ok.

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 354

Quoted 3 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
+               spin_unlock(&syncobj->lock);
+               list_for_each_entry_safe(cur, tmp, &cb_list, node) {
                        list_del_init(&cur->node);

Races against external caller of drm_syncobj_remove_callback(). However, it looks like that race is just fine, but we don't guard against the struct drm_syncobj_cb itself being freed, leading to all sort of fun for an interrupted drm_syncobj_array_wait_timeout.

That kfree seems to undermine the validity of stealing the list. -Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw
Quoted 26 lines · Chris Wilson

Quoting Chunming Zhou (2018-10-19 11:26:41)

Quoted 13 lines
Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Christian König <[email protected]>
---
  drivers/gpu/drm/drm_syncobj.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_syncobj.c +0 -0
index 57bf6006394d..2f3c14cb5156 100644

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 348

Quoted 5 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
@@ -344,13 +344,16 @@ 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;
+               struct list_head cb_list;
+               INIT_LIST_HEAD(&cb_list);

LIST_HEAD(cb_list); // does both in one

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 351

Quoted 3 lines
drivers/gpu/drm/drm_syncobj.c +1 -1
                 spin_lock(&syncobj->lock);
-               list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
+               list_splice_init(&syncobj->cb_list, &cb_list);

Steal the snapshot of the list under the lock, ok.

Quoted 7 lines

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 354

Quoted 3 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
+               spin_unlock(&syncobj->lock);
+               list_for_each_entry_safe(cur, tmp, &cb_list, node) {
                         list_del_init(&cur->node);

Races against external caller of drm_syncobj_remove_callback(). However, it looks like that race is just fine, but we don't guard against the struct drm_syncobj_cb itself being freed, leading to all sort of fun for an interrupted drm_syncobj_array_wait_timeout.

Thanks quick review, I will use "while (!list_empty()) { e = list_first_entry(); list_del(e)" to avoid deadlock.

will send v2 in one minute.

Regards,
David Zhou

Quoted 3 lines

That kfree seems to undermine the validity of stealing the list. -Chris

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

#3 lore raw
Quoted 31 lines · On 2018年10月19日 19:26, zhoucm1 wrote:
Quoted 28 lines · Chris Wilson

Quoting Chunming Zhou (2018-10-19 11:26:41)

Quoted 13 lines
Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Christian König <[email protected]>

---
  drivers/gpu/drm/drm_syncobj.c | 7 +++++--

  1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_syncobj.c +0 -0
index 57bf6006394d..2f3c14cb5156 100644

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct · line 349

Quoted 6 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
@@ -344,13 +344,16 @@ 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;
+               struct list_head cb_list;
+               INIT_LIST_HEAD(&cb_list);

LIST_HEAD(cb_list); // does both in one

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 351

Quoted 4 lines
drivers/gpu/drm/drm_syncobj.c +1 -1
 spin_lock(&syncobj->lock);
-               list_for_each_entry_safe(cur, tmp, 
 &syncobj->cb_list, node) {
+               list_splice_init(&syncobj->cb_list, &cb_list);

Steal the snapshot of the list under the lock, ok.

Quoted 8 lines
Quoted 7 lines

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 354

Quoted 3 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
+ spin_unlock(&syncobj->lock);
+               list_for_each_entry_safe(cur, tmp, &cb_list, node) {
                          list_del_init(&cur->node);

Races against external caller of drm_syncobj_remove_callback(). However, it looks like that race is just fine, but we don't guard against the struct drm_syncobj_cb itself being freed, leading to all sort of fun for an interrupted drm_syncobj_array_wait_timeout.

Thanks quick review, I will use "while (!list_empty()) { e = list_first_entry(); list_del(e)" to avoid deadlock.

this still cannot resolve freeing problem,  do you mind I change spinlock to mutex?

Thanks,
David Zhou

Quoted 9 lines

will send v2 in one minute.

Regards,
David Zhou

Quoted 3 lines

That kfree seems to undermine the validity of stealing the list. -Chris

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

#4 lore raw

Am 19.10.18 um 14:01 schrieb zhoucm1:

Quoted 44 lines · On 2018年10月19日 19:26, zhoucm1 wrote:
Quoted 41 lines · Chris Wilson
Quoted 38 lines

Quoting Chunming Zhou (2018-10-19 11:26:41)

Quoted 14 lines
Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Christian König <[email protected]>

---
  drivers/gpu/drm/drm_syncobj.c | 7 +++++--

  1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_syncobj.c +0 -0
index 57bf6006394d..2f3c14cb5156 100644

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct · line 349

Quoted 6 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
@@ -344,13 +344,16 @@ 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;
+               struct list_head cb_list;
+               INIT_LIST_HEAD(&cb_list);

LIST_HEAD(cb_list); // does both in one

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 351

Quoted 4 lines
drivers/gpu/drm/drm_syncobj.c +1 -1
 spin_lock(&syncobj->lock);
-               list_for_each_entry_safe(cur, tmp, 
 &syncobj->cb_list, node) {
+               list_splice_init(&syncobj->cb_list, &cb_list);

Steal the snapshot of the list under the lock, ok.

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 354

Quoted 3 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
+ spin_unlock(&syncobj->lock);
+               list_for_each_entry_safe(cur, tmp, &cb_list, node) {
                          list_del_init(&cur->node);

Races against external caller of drm_syncobj_remove_callback(). However,
it looks like that race is just fine, but we don't guard against the struct drm_syncobj_cb itself being freed, leading to all sort of fun for
an interrupted drm_syncobj_array_wait_timeout.

Quoted 4 lines
Quoted 2 lines

Thanks quick review, I will use "while (!list_empty()) { e = list_first_entry(); list_del(e)" to avoid deadlock.

this still cannot resolve freeing problem,  do you mind I change spinlock to mutex?

How does that help?

What you could do is to merge the array of fences into the beginning of the signal_pt, e.g. something like this:

struct drm_syncobj_signal_pt {
        struct dma_fence fences[2];
        struct dma_fence_array *fence_array;
        u64    value;
        struct list_head list;
};

This way the drm_syncobj_signal_pt is freed when the fence_array is freed. That should be sufficient if we correctly reference count the fence_array.

Christian.

Quoted 13 lines

Thanks,
David Zhou

Quoted 9 lines

will send v2 in one minute.

Regards,
David Zhou

Quoted 3 lines

That kfree seems to undermine the validity of stealing the list. -Chris

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

#5 lore raw
Quoted 59 lines · Christian

Am 19.10.18 um 14:01 schrieb zhoucm1:

Quoted 42 lines · On 2018年10月19日 19:26, zhoucm1 wrote:
Quoted 40 lines · Chris Wilson
Quoted 38 lines

Quoting Chunming Zhou (2018-10-19 11:26:41)

Quoted 14 lines
Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Christian König <[email protected]>
---
   drivers/gpu/drm/drm_syncobj.c | 7 +++++--
   1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_syncobj.c +0 -0
index 57bf6006394d..2f3c14cb5156 100644

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct · line 349

Quoted 6 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
@@ -344,13 +344,16 @@ 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;
+               struct list_head cb_list;
+               INIT_LIST_HEAD(&cb_list);

LIST_HEAD(cb_list); // does both in one

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 351

Quoted 4 lines
drivers/gpu/drm/drm_syncobj.c +1 -1
 spin_lock(&syncobj->lock);
-               list_for_each_entry_safe(cur, tmp,
 &syncobj->cb_list, node) {
+               list_splice_init(&syncobj->cb_list, &cb_list);

Steal the snapshot of the list under the lock, ok.

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 354

Quoted 3 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
+ spin_unlock(&syncobj->lock);
+               list_for_each_entry_safe(cur, tmp, &cb_list, node) {
                          list_del_init(&cur->node);

Races against external caller of drm_syncobj_remove_callback(). However,
it looks like that race is just fine, but we don't guard against the struct drm_syncobj_cb itself being freed, leading to all sort of fun for
an interrupted drm_syncobj_array_wait_timeout.

Quoted 4 lines
Quoted 2 lines

Thanks quick review, I will use "while (!list_empty()) { e = list_first_entry(); list_del(e)" to avoid deadlock.

this still cannot resolve freeing problem,  do you mind I change spinlock to mutex?

How does that help?

What you could do is to merge the array of fences into the beginning of the signal_pt, e.g. something like this:

struct drm_syncobj_signal_pt {
        struct dma_fence fences[2];
        struct dma_fence_array *fence_array;
        u64    value;
        struct list_head list;
};

Quoted 3 lines

This way the drm_syncobj_signal_pt is freed when the fence_array is freed. That should be sufficient if we correctly reference count the fence_array.

I'm not sure what problem you said, the deadlock reason is :
Cb func will call drm_syncobj_search_fence, which will need to grab the lock, otherwise deadlock.

But when we steal list or use "while (!list_empty()) { e = list_first_entry(); list_del(e)", both will encounter another freeing problem, that is syncobj_cb could be freed when wait timeout.

If we change to use mutex, then we can move lock inside of _search_fence out. another way is we add a separate spinlock for signal_pt_list, not share one lock with cb_list.

Regards,
David

Quoted 11 lines

Christian.

Quoted 8 lines

Thanks,
David Zhou

Quoted 6 lines

will send v2 in one minute.

Regards,
David Zhou

Quoted 2 lines

That kfree seems to undermine the validity of stealing the list. -Chris

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

#6 lore raw

Am 19.10.18 um 14:19 schrieb zhoucm1:

Quoted 65 lines · Christian
Quoted 59 lines

Am 19.10.18 um 14:01 schrieb zhoucm1:

Quoted 42 lines · On 2018年10月19日 19:26, zhoucm1 wrote:
Quoted 40 lines · Chris Wilson
Quoted 38 lines

Quoting Chunming Zhou (2018-10-19 11:26:41)

Quoted 14 lines
Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Christian König <[email protected]>

---
   drivers/gpu/drm/drm_syncobj.c | 7 +++++--

   1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_syncobj.c +0 -0
index 57bf6006394d..2f3c14cb5156 100644

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct · line 349

Quoted 6 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
@@ -344,13 +344,16 @@ 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;
+               struct list_head cb_list;
+               INIT_LIST_HEAD(&cb_list);

LIST_HEAD(cb_list); // does both in one

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 351

Quoted 4 lines
drivers/gpu/drm/drm_syncobj.c +1 -1
 spin_lock(&syncobj->lock);
-               list_for_each_entry_safe(cur, tmp,
 &syncobj->cb_list, node) {
+ list_splice_init(&syncobj->cb_list, &cb_list);

Steal the snapshot of the list under the lock, ok.

drivers/gpu/drm/drm_syncobj.c void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, · line 354

Quoted 3 lines
drivers/gpu/drm/drm_syncobj.c +2 -0
+ spin_unlock(&syncobj->lock);
+               list_for_each_entry_safe(cur, tmp, &cb_list, node) {
 list_del_init(&cur->node);

Races against external caller of drm_syncobj_remove_callback(). However,
it looks like that race is just fine, but we don't guard against the struct drm_syncobj_cb itself being freed, leading to all sort of fun for
an interrupted drm_syncobj_array_wait_timeout.

Quoted 4 lines
Quoted 2 lines

Thanks quick review, I will use "while (!list_empty()) { e = list_first_entry(); list_del(e)" to avoid deadlock.

this still cannot resolve freeing problem,  do you mind I change spinlock to mutex?

How does that help?

What you could do is to merge the array of fences into the beginning of the signal_pt, e.g. something like this:

struct drm_syncobj_signal_pt {
          struct dma_fence fences[2];
          struct dma_fence_array *fence_array;
          u64    value;
          struct list_head list;
};

Quoted 3 lines

This way the drm_syncobj_signal_pt is freed when the fence_array is freed. That should be sufficient if we correctly reference count the fence_array.

Quoted 8 lines

I'm not sure what problem you said, the deadlock reason is :
Cb func will call drm_syncobj_search_fence, which will need to grab the lock, otherwise deadlock.

But when we steal list or use "while (!list_empty()) { e = list_first_entry(); list_del(e)", both will encounter another freeing problem, that is syncobj_cb could be freed when wait timeout.

If we change to use mutex, then we can move lock inside of _search_fence out.

another way is we add a separate spinlock for signal_pt_list, not share one lock with cb_list.

Well of hand that sounds like the cleanest approach to me.

Christian.

Quoted 12 lines

Regards,
David

Quoted 9 lines

Christian.

Quoted 6 lines

Thanks,
David Zhou

Quoted 4 lines

will send v2 in one minute.

Regards,
David Zhou

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

That kfree seems to undermine the validity of stealing the list. -Chris

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

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