[RFC 1/8] dma-buf/fence: add fence_collection fences

2016-05-18 Thread Gustavo Padovan
Hi Christian,

2016-05-18 Christian König :

> Am 15.04.2016 um 21:25 schrieb Daniel Vetter:
> > On Fri, Apr 15, 2016 at 11:27:50AM -0700, Gustavo Padovan wrote:
> > > 2016-04-15 Christian König :
> > > > Amdgpu also has an implementation for a fence collection which uses a a
> > > > hashtable to keep the fences grouped by context (e.g. only the latest 
> > > > fence
> > > > is keept for each context). See amdgpu_sync.c for reference.
> > > > 
> > > > We should either make the collection similar in a way that you can add 
> > > > as
> > > > many fences as you want (like the amdgpu implementation) or make it 
> > > > static
> > > > and only add a fixed number of fences right from the beginning.
> > > > 
> > > > I can certainly see use cases for both, but if you want to stick with a
> > > > static approach you should probably call the new object fence_array 
> > > > instead
> > > > of fence_collection and do as Daniel suggested.
> > > Maybe we can go for something in between. Have fence_collection_init()
> > > need at least two fences to create the fence_collection. Then
> > > fence_collection_add() would add more dinamically.
> > The problem with adding fences later on is that it makes it trivial to add
> > deadlocks and loops. Just add the fence collection to itself, boom. From
> > that pov it's an unsafe api, and hence something to avoid.
> > -Daniel
> 
> Any conclusion on this? Did any version of the patch made it upstream?
> 
> I'm in the need of an array based fence collection right now as well. Any
> objection that I just take the patch proposed here and fix the comments or
> are you still else working on this right now?

I have a new version of this patch that I didn't send upstream yet
because it is part of a bigger patchset. But I can split it and send
what I have for fence_collection later today.

Gustavo


[RFC 1/8] dma-buf/fence: add fence_collection fences

2016-05-18 Thread Christian König
Am 15.04.2016 um 21:25 schrieb Daniel Vetter:
> On Fri, Apr 15, 2016 at 11:27:50AM -0700, Gustavo Padovan wrote:
>> 2016-04-15 Christian König :
>>> Amdgpu also has an implementation for a fence collection which uses a a
>>> hashtable to keep the fences grouped by context (e.g. only the latest fence
>>> is keept for each context). See amdgpu_sync.c for reference.
>>>
>>> We should either make the collection similar in a way that you can add as
>>> many fences as you want (like the amdgpu implementation) or make it static
>>> and only add a fixed number of fences right from the beginning.
>>>
>>> I can certainly see use cases for both, but if you want to stick with a
>>> static approach you should probably call the new object fence_array instead
>>> of fence_collection and do as Daniel suggested.
>> Maybe we can go for something in between. Have fence_collection_init()
>> need at least two fences to create the fence_collection. Then
>> fence_collection_add() would add more dinamically.
> The problem with adding fences later on is that it makes it trivial to add
> deadlocks and loops. Just add the fence collection to itself, boom. From
> that pov it's an unsafe api, and hence something to avoid.
> -Daniel

Any conclusion on this? Did any version of the patch made it upstream?

I'm in the need of an array based fence collection right now as well. 
Any objection that I just take the patch proposed here and fix the 
comments or are you still else working on this right now?

Regards,
Christian.




[RFC 1/8] dma-buf/fence: add fence_collection fences

2016-04-15 Thread Daniel Vetter
On Fri, Apr 15, 2016 at 11:27:50AM -0700, Gustavo Padovan wrote:
> 2016-04-15 Christian König :
> > Amdgpu also has an implementation for a fence collection which uses a a
> > hashtable to keep the fences grouped by context (e.g. only the latest fence
> > is keept for each context). See amdgpu_sync.c for reference.
> > 
> > We should either make the collection similar in a way that you can add as
> > many fences as you want (like the amdgpu implementation) or make it static
> > and only add a fixed number of fences right from the beginning.
> > 
> > I can certainly see use cases for both, but if you want to stick with a
> > static approach you should probably call the new object fence_array instead
> > of fence_collection and do as Daniel suggested.
> 
> Maybe we can go for something in between. Have fence_collection_init()
> need at least two fences to create the fence_collection. Then
> fence_collection_add() would add more dinamically.

The problem with adding fences later on is that it makes it trivial to add
deadlocks and loops. Just add the fence collection to itself, boom. From
that pov it's an unsafe api, and hence something to avoid.
-Daniel
-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch


[RFC 1/8] dma-buf/fence: add fence_collection fences

2016-04-15 Thread Daniel Vetter
On Fri, Apr 15, 2016 at 11:29:34AM -0700, Gustavo Padovan wrote:
> 2016-04-15 Daniel Vetter :
> 
> > On Fri, Apr 15, 2016 at 11:03 AM, Christian König
> >  wrote:
> > > Might be that how amdgpu uses the fence context and sequence number is a 
> > > bit
> > > questionable, but this will completely break it.
> > 
> > You mean it tries to qualesce fences in the same context down to just
> > the last one? That's how it's supposed to be done, and
> > fence_collections do break this somewhat. Without fixing up
> > fence_is_later and friends. Sounds like amdgpu is a good use case to
> > make sure the changes in semantics in these functions result in
> > sensible code. In a way a fence_collection is a fence where the
> > timeline never matches with any other timeline (since it's a
> > combiation).
> > 
> > And yeah I think fence_collection should probably compress down the
> > fences to 1 per timeline. But then that's just an implementation
> > detail we can fix later on.
> 
> You mean asking for a new context for every collection?

That would be one solution, but I fear it's a bit expensive. Having a
special-case context for collections might be the better approach.
-Daniel
-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch


[RFC 1/8] dma-buf/fence: add fence_collection fences

2016-04-15 Thread Daniel Vetter
On Fri, Apr 15, 2016 at 11:03 AM, Christian König
 wrote:
> Might be that how amdgpu uses the fence context and sequence number is a bit
> questionable, but this will completely break it.

You mean it tries to qualesce fences in the same context down to just
the last one? That's how it's supposed to be done, and
fence_collections do break this somewhat. Without fixing up
fence_is_later and friends. Sounds like amdgpu is a good use case to
make sure the changes in semantics in these functions result in
sensible code. In a way a fence_collection is a fence where the
timeline never matches with any other timeline (since it's a
combiation).

And yeah I think fence_collection should probably compress down the
fences to 1 per timeline. But then that's just an implementation
detail we can fix later on.
-Daniel
-- 
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch


[RFC 1/8] dma-buf/fence: add fence_collection fences

2016-04-15 Thread Gustavo Padovan
2016-04-15 Daniel Vetter :

> On Fri, Apr 15, 2016 at 11:03 AM, Christian König
>  wrote:
> > Might be that how amdgpu uses the fence context and sequence number is a bit
> > questionable, but this will completely break it.
> 
> You mean it tries to qualesce fences in the same context down to just
> the last one? That's how it's supposed to be done, and
> fence_collections do break this somewhat. Without fixing up
> fence_is_later and friends. Sounds like amdgpu is a good use case to
> make sure the changes in semantics in these functions result in
> sensible code. In a way a fence_collection is a fence where the
> timeline never matches with any other timeline (since it's a
> combiation).
> 
> And yeah I think fence_collection should probably compress down the
> fences to 1 per timeline. But then that's just an implementation
> detail we can fix later on.

You mean asking for a new context for every collection?

Gustavo


[RFC 1/8] dma-buf/fence: add fence_collection fences

2016-04-15 Thread Gustavo Padovan
2016-04-15 Christian König :

> Am 15.04.2016 um 10:02 schrieb Daniel Vetter:
> >On Thu, Apr 14, 2016 at 06:29:34PM -0700, Gustavo Padovan wrote:
> >>From: Gustavo Padovan 
> >>
> >>struct fence_collection inherits from struct fence and carries a
> >>collection of fences that needs to be waited together.
> >>
> >>It is useful to translate a sync_file to a fence to remove the complexity
> >>of dealing with sync_files from DRM drivers. So even if there are many
> >>fences in the sync_file that needs to waited for a commit to happen
> >>drivers would only worry about a standard struct fence.That means that no
> >>changes needed to any driver besides supporting fences.
> >>
> >>fence_collection's fence doesn't belong to any timeline context.
> >>
> >>Signed-off-by: Gustavo Padovan 
> >>---
> >>  drivers/dma-buf/Makefile   |   2 +-
> >>  drivers/dma-buf/fence-collection.c | 138 
> >> +
> >>  drivers/dma-buf/fence.c|   2 +-
> >>  include/linux/fence-collection.h   |  56 +++
> >>  include/linux/fence.h  |   2 +
> >>  5 files changed, 198 insertions(+), 2 deletions(-)
> >>  create mode 100644 drivers/dma-buf/fence-collection.c
> >>  create mode 100644 include/linux/fence-collection.h
> >>
> >>diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> >>index 43325a1..30b8464 100644
> >>--- a/drivers/dma-buf/Makefile
> >>+++ b/drivers/dma-buf/Makefile
> >>@@ -1,3 +1,3 @@
> >>-obj-y := dma-buf.o fence.o reservation.o seqno-fence.o
> >>+obj-y := dma-buf.o fence.o reservation.o seqno-fence.o fence-collection.o
> >>  obj-$(CONFIG_SYNC_FILE)   += sync_file.o sync_timeline.o 
> >> sync_debug.o
> >>  obj-$(CONFIG_SW_SYNC) += sw_sync.o
> >>diff --git a/drivers/dma-buf/fence-collection.c 
> >>b/drivers/dma-buf/fence-collection.c
> >>new file mode 100644
> >>index 000..8a4ecb0
> >>--- /dev/null
> >>+++ b/drivers/dma-buf/fence-collection.c
> >>@@ -0,0 +1,138 @@
> >>+/*
> >>+ * fence-collection: aggregate fences to be waited together
> >>+ *
> >>+ * Copyright (C) 2016 Collabora Ltd
> >>+ * Authors:
> >>+ * Gustavo Padovan 
> >>+ *
> >>+ * This program is free software; you can redistribute it and/or modify it
> >>+ * under the terms of the GNU General Public License version 2 as 
> >>published by
> >>+ * the Free Software Foundation.
> >>+ *
> >>+ * This program is distributed in the hope that it will be useful, but 
> >>WITHOUT
> >>+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
> >>+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
> >>for
> >>+ * more details.
> >>+ */
> >>+
> >>+#include 
> >>+#include 
> >>+#include 
> >>+
> >>+static const char *fence_collection_get_driver_name(struct fence *fence)
> >>+{
> >>+   struct fence_collection *collection = to_fence_collection(fence);
> >>+   struct fence *f = collection->fences[0].fence;
> >>+
> >>+   return f->ops->get_driver_name(fence);
> >>+}
> 
> I would rather return some constant name here instead of relying that the
> collection already has a fence added and that all fences are from the same
> driver.

If we merge _init and _add this will not be a problem anymore and we can
return the actual driver name.

> 
> >>+
> >>+static const char *fence_collection_get_timeline_name(struct fence *fence)
> >>+{
> >>+   return "no context";
> >>+}
> >>+
> >>+static bool fence_collection_enable_signaling(struct fence *fence)
> >>+{
> >>+   struct fence_collection *collection = to_fence_collection(fence);
> >>+
> >>+   return atomic_read(&collection->num_pending_fences);
> >>+}
> >>+
> >>+static bool fence_collection_signaled(struct fence *fence)
> >>+{
> >>+   struct fence_collection *collection = to_fence_collection(fence);
> >>+
> >>+   return (atomic_read(&collection->num_pending_fences) == 0);
> >>+}
> >>+
> >>+static void fence_collection_release(struct fence *fence)
> >>+{
> >>+   struct fence_collection *collection = to_fence_collection(fence);
> >>+   int i;
> >>+
> >>+   for (i = 0 ; i < collection->num_fences ; i++) {
> >>+   fence_remove_callback(collection->fences[i].fence,
> >>+ &collection->fences[i].cb);
> >>+   fence_put(collection->fences[i].fence);
> >>+   }
> >>+
> >>+   fence_free(fence);
> >>+}
> >>+
> >>+static signed long fence_collection_wait(struct fence *fence, bool intr,
> >>+signed long timeout)
> >>+{
> >>+   struct fence_collection *collection = to_fence_collection(fence);
> >>+   int i;
> >>+
> >>+   for (i = 0 ; i < collection->num_fences ; i++) {
> >>+   timeout = fence_wait(collection->fences[i].fence, intr);
> >>+   if (timeout < 0)
> >>+   return timeout;
> >>+   }
> >>+
> >>+   return timeout;
> >>+}
> >>+
> >>+static const struct fence_ops fence_collection_ops = {
> >>+   .get_driver_name = fence_collection_get_driver_name,
> >>+   .get_timeline_name = fence_collection_ge

[RFC 1/8] dma-buf/fence: add fence_collection fences

2016-04-15 Thread Christian König
Am 15.04.2016 um 10:02 schrieb Daniel Vetter:
> On Thu, Apr 14, 2016 at 06:29:34PM -0700, Gustavo Padovan wrote:
>> From: Gustavo Padovan 
>>
>> struct fence_collection inherits from struct fence and carries a
>> collection of fences that needs to be waited together.
>>
>> It is useful to translate a sync_file to a fence to remove the complexity
>> of dealing with sync_files from DRM drivers. So even if there are many
>> fences in the sync_file that needs to waited for a commit to happen
>> drivers would only worry about a standard struct fence.That means that no
>> changes needed to any driver besides supporting fences.
>>
>> fence_collection's fence doesn't belong to any timeline context.
>>
>> Signed-off-by: Gustavo Padovan 
>> ---
>>   drivers/dma-buf/Makefile   |   2 +-
>>   drivers/dma-buf/fence-collection.c | 138 
>> +
>>   drivers/dma-buf/fence.c|   2 +-
>>   include/linux/fence-collection.h   |  56 +++
>>   include/linux/fence.h  |   2 +
>>   5 files changed, 198 insertions(+), 2 deletions(-)
>>   create mode 100644 drivers/dma-buf/fence-collection.c
>>   create mode 100644 include/linux/fence-collection.h
>>
>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
>> index 43325a1..30b8464 100644
>> --- a/drivers/dma-buf/Makefile
>> +++ b/drivers/dma-buf/Makefile
>> @@ -1,3 +1,3 @@
>> -obj-y := dma-buf.o fence.o reservation.o seqno-fence.o
>> +obj-y := dma-buf.o fence.o reservation.o seqno-fence.o fence-collection.o
>>   obj-$(CONFIG_SYNC_FILE)+= sync_file.o sync_timeline.o 
>> sync_debug.o
>>   obj-$(CONFIG_SW_SYNC)  += sw_sync.o
>> diff --git a/drivers/dma-buf/fence-collection.c 
>> b/drivers/dma-buf/fence-collection.c
>> new file mode 100644
>> index 000..8a4ecb0
>> --- /dev/null
>> +++ b/drivers/dma-buf/fence-collection.c
>> @@ -0,0 +1,138 @@
>> +/*
>> + * fence-collection: aggregate fences to be waited together
>> + *
>> + * Copyright (C) 2016 Collabora Ltd
>> + * Authors:
>> + *  Gustavo Padovan 
>> + *
>> + * This program is free software; you can redistribute it and/or modify it
>> + * under the terms of the GNU General Public License version 2 as published 
>> by
>> + * the Free Software Foundation.
>> + *
>> + * This program is distributed in the hope that it will be useful, but 
>> WITHOUT
>> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
>> + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
>> + * more details.
>> + */
>> +
>> +#include 
>> +#include 
>> +#include 
>> +
>> +static const char *fence_collection_get_driver_name(struct fence *fence)
>> +{
>> +struct fence_collection *collection = to_fence_collection(fence);
>> +struct fence *f = collection->fences[0].fence;
>> +
>> +return f->ops->get_driver_name(fence);
>> +}

I would rather return some constant name here instead of relying that 
the collection already has a fence added and that all fences are from 
the same driver.

>> +
>> +static const char *fence_collection_get_timeline_name(struct fence *fence)
>> +{
>> +return "no context";
>> +}
>> +
>> +static bool fence_collection_enable_signaling(struct fence *fence)
>> +{
>> +struct fence_collection *collection = to_fence_collection(fence);
>> +
>> +return atomic_read(&collection->num_pending_fences);
>> +}
>> +
>> +static bool fence_collection_signaled(struct fence *fence)
>> +{
>> +struct fence_collection *collection = to_fence_collection(fence);
>> +
>> +return (atomic_read(&collection->num_pending_fences) == 0);
>> +}
>> +
>> +static void fence_collection_release(struct fence *fence)
>> +{
>> +struct fence_collection *collection = to_fence_collection(fence);
>> +int i;
>> +
>> +for (i = 0 ; i < collection->num_fences ; i++) {
>> +fence_remove_callback(collection->fences[i].fence,
>> +  &collection->fences[i].cb);
>> +fence_put(collection->fences[i].fence);
>> +}
>> +
>> +fence_free(fence);
>> +}
>> +
>> +static signed long fence_collection_wait(struct fence *fence, bool intr,
>> + signed long timeout)
>> +{
>> +struct fence_collection *collection = to_fence_collection(fence);
>> +int i;
>> +
>> +for (i = 0 ; i < collection->num_fences ; i++) {
>> +timeout = fence_wait(collection->fences[i].fence, intr);
>> +if (timeout < 0)
>> +return timeout;
>> +}
>> +
>> +return timeout;
>> +}
>> +
>> +static const struct fence_ops fence_collection_ops = {
>> +.get_driver_name = fence_collection_get_driver_name,
>> +.get_timeline_name = fence_collection_get_timeline_name,
>> +.enable_signaling = fence_collection_enable_signaling,
>> +.signaled = fence_collection_signaled,
>> +.wait = fence_collection_wait,
>> +.release = fence_collection_release,
>> +};
>> +
>> +static void collection_chec

[RFC 1/8] dma-buf/fence: add fence_collection fences

2016-04-15 Thread Daniel Vetter
On Thu, Apr 14, 2016 at 06:29:34PM -0700, Gustavo Padovan wrote:
> From: Gustavo Padovan 
> 
> struct fence_collection inherits from struct fence and carries a
> collection of fences that needs to be waited together.
> 
> It is useful to translate a sync_file to a fence to remove the complexity
> of dealing with sync_files from DRM drivers. So even if there are many
> fences in the sync_file that needs to waited for a commit to happen
> drivers would only worry about a standard struct fence.That means that no
> changes needed to any driver besides supporting fences.
> 
> fence_collection's fence doesn't belong to any timeline context.
> 
> Signed-off-by: Gustavo Padovan 
> ---
>  drivers/dma-buf/Makefile   |   2 +-
>  drivers/dma-buf/fence-collection.c | 138 
> +
>  drivers/dma-buf/fence.c|   2 +-
>  include/linux/fence-collection.h   |  56 +++
>  include/linux/fence.h  |   2 +
>  5 files changed, 198 insertions(+), 2 deletions(-)
>  create mode 100644 drivers/dma-buf/fence-collection.c
>  create mode 100644 include/linux/fence-collection.h
> 
> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> index 43325a1..30b8464 100644
> --- a/drivers/dma-buf/Makefile
> +++ b/drivers/dma-buf/Makefile
> @@ -1,3 +1,3 @@
> -obj-y := dma-buf.o fence.o reservation.o seqno-fence.o
> +obj-y := dma-buf.o fence.o reservation.o seqno-fence.o fence-collection.o
>  obj-$(CONFIG_SYNC_FILE)  += sync_file.o sync_timeline.o 
> sync_debug.o
>  obj-$(CONFIG_SW_SYNC)+= sw_sync.o
> diff --git a/drivers/dma-buf/fence-collection.c 
> b/drivers/dma-buf/fence-collection.c
> new file mode 100644
> index 000..8a4ecb0
> --- /dev/null
> +++ b/drivers/dma-buf/fence-collection.c
> @@ -0,0 +1,138 @@
> +/*
> + * fence-collection: aggregate fences to be waited together
> + *
> + * Copyright (C) 2016 Collabora Ltd
> + * Authors:
> + *   Gustavo Padovan 
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU General Public License version 2 as published 
> by
> + * the Free Software Foundation.
> + *
> + * This program is distributed in the hope that it will be useful, but 
> WITHOUT
> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
> + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
> + * more details.
> + */
> +
> +#include 
> +#include 
> +#include 
> +
> +static const char *fence_collection_get_driver_name(struct fence *fence)
> +{
> + struct fence_collection *collection = to_fence_collection(fence);
> + struct fence *f = collection->fences[0].fence;
> +
> + return f->ops->get_driver_name(fence);
> +}
> +
> +static const char *fence_collection_get_timeline_name(struct fence *fence)
> +{
> + return "no context";
> +}
> +
> +static bool fence_collection_enable_signaling(struct fence *fence)
> +{
> + struct fence_collection *collection = to_fence_collection(fence);
> +
> + return atomic_read(&collection->num_pending_fences);
> +}
> +
> +static bool fence_collection_signaled(struct fence *fence)
> +{
> + struct fence_collection *collection = to_fence_collection(fence);
> +
> + return (atomic_read(&collection->num_pending_fences) == 0);
> +}
> +
> +static void fence_collection_release(struct fence *fence)
> +{
> + struct fence_collection *collection = to_fence_collection(fence);
> + int i;
> +
> + for (i = 0 ; i < collection->num_fences ; i++) {
> + fence_remove_callback(collection->fences[i].fence,
> +   &collection->fences[i].cb);
> + fence_put(collection->fences[i].fence);
> + }
> +
> + fence_free(fence);
> +}
> +
> +static signed long fence_collection_wait(struct fence *fence, bool intr,
> +  signed long timeout)
> +{
> + struct fence_collection *collection = to_fence_collection(fence);
> + int i;
> +
> + for (i = 0 ; i < collection->num_fences ; i++) {
> + timeout = fence_wait(collection->fences[i].fence, intr);
> + if (timeout < 0)
> + return timeout;
> + }
> +
> + return timeout;
> +}
> +
> +static const struct fence_ops fence_collection_ops = {
> + .get_driver_name = fence_collection_get_driver_name,
> + .get_timeline_name = fence_collection_get_timeline_name,
> + .enable_signaling = fence_collection_enable_signaling,
> + .signaled = fence_collection_signaled,
> + .wait = fence_collection_wait,
> + .release = fence_collection_release,
> +};
> +
> +static void collection_check_cb_func(struct fence *fence, struct fence_cb 
> *cb)
> +{
> + struct fence_collection_cb *f_cb;
> + struct fence_collection *collection;
> +
> + f_cb = container_of(cb, struct fence_collection_cb, cb);
> + collection = f_cb->collection;
> +
> + if (atomic_dec_and_test(&collection->num_p

[RFC 1/8] dma-buf/fence: add fence_collection fences

2016-04-14 Thread Gustavo Padovan
From: Gustavo Padovan 

struct fence_collection inherits from struct fence and carries a
collection of fences that needs to be waited together.

It is useful to translate a sync_file to a fence to remove the complexity
of dealing with sync_files from DRM drivers. So even if there are many
fences in the sync_file that needs to waited for a commit to happen
drivers would only worry about a standard struct fence.That means that no
changes needed to any driver besides supporting fences.

fence_collection's fence doesn't belong to any timeline context.

Signed-off-by: Gustavo Padovan 
---
 drivers/dma-buf/Makefile   |   2 +-
 drivers/dma-buf/fence-collection.c | 138 +
 drivers/dma-buf/fence.c|   2 +-
 include/linux/fence-collection.h   |  56 +++
 include/linux/fence.h  |   2 +
 5 files changed, 198 insertions(+), 2 deletions(-)
 create mode 100644 drivers/dma-buf/fence-collection.c
 create mode 100644 include/linux/fence-collection.h

diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
index 43325a1..30b8464 100644
--- a/drivers/dma-buf/Makefile
+++ b/drivers/dma-buf/Makefile
@@ -1,3 +1,3 @@
-obj-y := dma-buf.o fence.o reservation.o seqno-fence.o
+obj-y := dma-buf.o fence.o reservation.o seqno-fence.o fence-collection.o
 obj-$(CONFIG_SYNC_FILE)+= sync_file.o sync_timeline.o 
sync_debug.o
 obj-$(CONFIG_SW_SYNC)  += sw_sync.o
diff --git a/drivers/dma-buf/fence-collection.c 
b/drivers/dma-buf/fence-collection.c
new file mode 100644
index 000..8a4ecb0
--- /dev/null
+++ b/drivers/dma-buf/fence-collection.c
@@ -0,0 +1,138 @@
+/*
+ * fence-collection: aggregate fences to be waited together
+ *
+ * Copyright (C) 2016 Collabora Ltd
+ * Authors:
+ * Gustavo Padovan 
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#include 
+#include 
+#include 
+
+static const char *fence_collection_get_driver_name(struct fence *fence)
+{
+   struct fence_collection *collection = to_fence_collection(fence);
+   struct fence *f = collection->fences[0].fence;
+
+   return f->ops->get_driver_name(fence);
+}
+
+static const char *fence_collection_get_timeline_name(struct fence *fence)
+{
+   return "no context";
+}
+
+static bool fence_collection_enable_signaling(struct fence *fence)
+{
+   struct fence_collection *collection = to_fence_collection(fence);
+
+   return atomic_read(&collection->num_pending_fences);
+}
+
+static bool fence_collection_signaled(struct fence *fence)
+{
+   struct fence_collection *collection = to_fence_collection(fence);
+
+   return (atomic_read(&collection->num_pending_fences) == 0);
+}
+
+static void fence_collection_release(struct fence *fence)
+{
+   struct fence_collection *collection = to_fence_collection(fence);
+   int i;
+
+   for (i = 0 ; i < collection->num_fences ; i++) {
+   fence_remove_callback(collection->fences[i].fence,
+ &collection->fences[i].cb);
+   fence_put(collection->fences[i].fence);
+   }
+
+   fence_free(fence);
+}
+
+static signed long fence_collection_wait(struct fence *fence, bool intr,
+signed long timeout)
+{
+   struct fence_collection *collection = to_fence_collection(fence);
+   int i;
+
+   for (i = 0 ; i < collection->num_fences ; i++) {
+   timeout = fence_wait(collection->fences[i].fence, intr);
+   if (timeout < 0)
+   return timeout;
+   }
+
+   return timeout;
+}
+
+static const struct fence_ops fence_collection_ops = {
+   .get_driver_name = fence_collection_get_driver_name,
+   .get_timeline_name = fence_collection_get_timeline_name,
+   .enable_signaling = fence_collection_enable_signaling,
+   .signaled = fence_collection_signaled,
+   .wait = fence_collection_wait,
+   .release = fence_collection_release,
+};
+
+static void collection_check_cb_func(struct fence *fence, struct fence_cb *cb)
+{
+   struct fence_collection_cb *f_cb;
+   struct fence_collection *collection;
+
+   f_cb = container_of(cb, struct fence_collection_cb, cb);
+   collection = f_cb->collection;
+
+   if (atomic_dec_and_test(&collection->num_pending_fences))
+   fence_signal(&collection->base);
+}
+
+void fence_collection_add(struct fence_collection *collection,
+ struct fence *fence)
+{
+   int n = collection->num_fences;
+
+   collection->fences[n].collection = collection