Skip to contentSkip to conversation

dma-buf/sw_sync: hold a fence reference when check if it signaled

PATCH In review Gustavo Padovan 3 participants 5 messages Jul 27, 2017 – Jul 28, 2017

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

PATCH 1/1 dma-buf/sw_sync: hold a fence reference when check if it signaled

From: Gustavo Padovan <[email protected]>

If userspace already dropped its own reference by closing the sw_sync fence fd we might end up in a deadlock where dma_fence_is_signaled_locked() will trigger the release of the fence a thus try to hold the lock to remove the fence from the list.

We need to grab a reference to the fence before calling into this chain if we want to avoid this issue.

---
 drivers/dma-buf/sw_sync.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/dma-buf/sw_sync.c +6 -1
index af1bc84..8291434 100644
@@ -144,11 +144,16 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
 	obj->value += inc;
 
 	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
-		if (!dma_fence_is_signaled_locked(&pt->base))
+		dma_fence_get(&pt->base);
+		if (!dma_fence_is_signaled_locked(&pt->base)) {
+			dma_fence_put(&pt->base);
 			break;
+		}
 
 		list_del_init(&pt->link);
 		rb_erase(&pt->node, &obj->pt_tree);
+
+		dma_fence_put(&pt->base);
 	}
 
 	spin_unlock_irq(&obj->lock);
Signature · 6 lines
2.9.4

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

Quoting Gustavo Padovan (2017-07-27 20:03:53)

Quoted 6 lines

From: Gustavo Padovan <[email protected]>

If userspace already dropped its own reference by closing the sw_sync fence fd we might end up in a deadlock where dma_fence_is_signaled_locked() will trigger the release of the fence a thus try to hold the lock to remove the fence from the list.

So the issue here is that call to dma_fence_is_signaled_lock() is triggering the unreference?

Quoted 14 lines

We need to grab a reference to the fence before calling into this chain if we want to avoid this issue.

---
 drivers/dma-buf/sw_sync.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/dma-buf/sw_sync.c +0 -0
index af1bc84..8291434 100644

drivers/dma-buf/sw_sync.c static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) · line 147

Quoted 5 lines
drivers/dma-buf/sw_sync.c +1 -1
@@ -144,11 +144,16 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
        obj->value += inc;
 
        list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
-               if (!dma_fence_is_signaled_locked(&pt->base))
+               dma_fence_get(&pt->base);

This would need to be dma_fence_get_rcu() to avoid grabbing the fence when its refcount has hit 0.

drivers/dma-buf/sw_sync.c static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) · line 154

Quoted 7 lines
drivers/dma-buf/sw_sync.c +3 -0
+               if (!dma_fence_is_signaled_locked(&pt->base)) {
+                       dma_fence_put(&pt->base);
                        break;
+               }
 
                list_del_init(&pt->link);
                rb_erase(&pt->node, &obj->pt_tree);

But if I understand correctly, we just need to unlink first, then signal.

list_for_each_entry_safe() {
	if (!timeline_fence_signaled(&pt->base))
		break;

	list_del_init(&pt->link);
	rb_erase(&pt->node, &obj->pt_tree);

	dma_fence_signal_locked(&pt->base);
}

The challenge is in writing the comment to explain the open-coding. -Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw

2017-07-27 Chris Wilson <[email protected]>:

Quoted 10 lines

Quoting Gustavo Padovan (2017-07-27 20:03:53)

Quoted 6 lines

From: Gustavo Padovan <[email protected]>

If userspace already dropped its own reference by closing the sw_sync fence fd we might end up in a deadlock where dma_fence_is_signaled_locked() will trigger the release of the fence a thus try to hold the lock to remove the fence from the list.

So the issue here is that call to dma_fence_is_signaled_lock() is triggering the unreference?

Exactly. I'll say that explicitely in the commit message.

Quoted 45 lines
Quoted 15 lines

We need to grab a reference to the fence before calling into this chain if we want to avoid this issue.

---
 drivers/dma-buf/sw_sync.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/dma-buf/sw_sync.c +0 -0
index af1bc84..8291434 100644

drivers/dma-buf/sw_sync.c static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) · line 147

Quoted 5 lines
drivers/dma-buf/sw_sync.c +1 -1
@@ -144,11 +144,16 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
        obj->value += inc;
 
        list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
-               if (!dma_fence_is_signaled_locked(&pt->base))
+               dma_fence_get(&pt->base);

This would need to be dma_fence_get_rcu() to avoid grabbing the fence when its refcount has hit 0.

drivers/dma-buf/sw_sync.c static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) · line 154

Quoted 7 lines
drivers/dma-buf/sw_sync.c +3 -0
+               if (!dma_fence_is_signaled_locked(&pt->base)) {
+                       dma_fence_put(&pt->base);
                        break;
+               }
 
                list_del_init(&pt->link);
                rb_erase(&pt->node, &obj->pt_tree);

But if I understand correctly, we just need to unlink first, then signal.

list_for_each_entry_safe() {
	if (!timeline_fence_signaled(&pt->base))
		break;

	list_del_init(&pt->link);
	rb_erase(&pt->node, &obj->pt_tree);

	dma_fence_signal_locked(&pt->base);
}
Quoted 1 line

The challenge is in writing the comment to explain the open-coding.

That is cleaner and doesn't need the get/put dance. I'll come up with a comment to explain it.

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

#3 lore raw

Quoting Gustavo Padovan (2017-07-28 02:57:25)

Quoted 10 lines

2017-07-27 Chris Wilson <[email protected]>:

Quoted 8 lines

Quoting Gustavo Padovan (2017-07-27 20:03:53)

Quoted 6 lines

From: Gustavo Padovan <[email protected]>

If userspace already dropped its own reference by closing the sw_sync fence fd we might end up in a deadlock where dma_fence_is_signaled_locked() will trigger the release of the fence a thus try to hold the lock to remove the fence from the list.

Quoted 4 lines
Quoted 2 lines

So the issue here is that call to dma_fence_is_signaled_lock() is triggering the unreference?

Exactly. I'll say that explicitely in the commit message.

:) It was more of a rhetorical question making sure that I understood correctly.

Quoted 10 lines
Quoted 10 lines

But if I understand correctly, we just need to unlink first, then signal.

list_for_each_entry_safe() {
      if (!timeline_fence_signaled(&pt->base))
              break;

      list_del_init(&pt->link);
      rb_erase(&pt->node, &obj->pt_tree);
Quoted 7 lines
Quoted 4 lines
      dma_fence_signal_locked(&pt->base);
}

The challenge is in writing the comment to explain the open-coding.

That is cleaner and doesn't need the get/put dance. I'll come up with a comment to explain it.

...

/*
* A signal callback may release the last reference to this fence,
* causing it to be freed. That operation has to be last to avoid
* a use after free inside this loop, and must be after we remove
* the fence from the timeline in order to prevent deadlocking on
* timeline->lock inside timeline_fence_release().
*/
dma_fence_signal_locked().
-Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
In reply to Gustavo Padovan
Quoted 7 lines · Gustavo Padovan

From: Gustavo Padovan <[email protected]>

If userspace already dropped its own reference by closing the sw_sync fence fd we might end up in a deadlock where dma_fence_is_signaled_locked() will trigger the release of the fence a thus try to hold the lock to remove the fence from the list.

Quoted 5 lines

We need to grab a reference to the fence before calling into this chain if we want to avoid this issue.

Do we have a testcase for this?
-Daniel

Quoted 33 lines
---
 drivers/dma-buf/sw_sync.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/dma-buf/sw_sync.c +6 -1
index af1bc84..8291434 100644
@@ -144,11 +144,16 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
 	obj->value += inc;
 
 	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
-		if (!dma_fence_is_signaled_locked(&pt->base))
+		dma_fence_get(&pt->base);
+		if (!dma_fence_is_signaled_locked(&pt->base)) {
+			dma_fence_put(&pt->base);
 			break;
+		}
 
 		list_del_init(&pt->link);
 		rb_erase(&pt->node, &obj->pt_tree);
+
+		dma_fence_put(&pt->base);
 	}
 
 	spin_unlock_irq(&obj->lock);
Signature · 6 lines
2.9.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel