From: Daniel Vetter Subject: Re: [PATCH 2/3] dma-buf/fence: add fence_array fences v5 Date: Mon, 23 May 2016 15:53:37 +0200 Message-ID: <20160523135337.GB27098@phenom.ffwll.local> References: <1464002722-4989-1-git-send-email-deathsimple@vodafone.de> <1464002722-4989-2-git-send-email-deathsimple@vodafone.de> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Return-path: Content-Disposition: inline In-Reply-To: <1464002722-4989-2-git-send-email-deathsimple@vodafone.de> Sender: linux-kernel-owner@vger.kernel.org To: Christian =?iso-8859-1?Q?K=F6nig?= Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, daniel@ffwll.ch, gustavo@padovan.org, chris@chris-wilson.co.uk List-Id: dri-devel@lists.freedesktop.org On Mon, May 23, 2016 at 01:25:21PM +0200, Christian K=F6nig wrote: > From: Gustavo Padovan >=20 > struct fence_collection inherits from struct fence and carries a > collection of fences that needs to be waited together. >=20 > It is useful to translate a sync_file to a fence to remove the comple= xity > of dealing with sync_files on DRM drivers. So even if there are many > fences in the sync_file that needs to waited for a commit to happen, > they all get added to the fence_collection and passed for DRM use as > a standard struct fence. >=20 > That means that no changes needed to any driver besides supporting fe= nces. >=20 > fence_collection's fence doesn't belong to any timeline context, so > fence_is_later() and fence_later() are not meant to be called with > fence_collections fences. >=20 > v2: Comments by Daniel Vetter: > - merge fence_collection_init() and fence_collection_add() > - only add callbacks at ->enable_signalling() > - remove fence_collection_put() > - check for type on to_fence_collection() > - adjust fence_is_later() and fence_later() to WARN_ON() if they > are used with collection fences. >=20 > v3: - Initialize fence_cb.node at fence init. >=20 > Comments by Chris Wilson: > - return "unbound" on fence_collection_get_timeline_name() > - don't stop adding callbacks if one fails > - remove redundant !! on fence_collection_enable_signaling() > - remove redundant () on fence_collection_signaled > - use fence_default_wait() instead >=20 > v4 (chk): Rework, simplification and cleanup: > - Drop FENCE_NO_CONTEXT handling, always allocate a context. > - Rename to fence_array. > - Return fixed driver name. > - Register only one callback at a time. > - Document that create function takes ownership of array. >=20 > v5 (chk): More work and fixes: > - Avoid deadlocks by adding all callbacks at once again. > - Stop trying to remove the callbacks. > - Provide context and sequence number for the array fence. That looks like even more a step backwards - I don't see how it's used = in your amdgpu code, but now callers need to either allocate a context unconditionally (more boilerplate code) or do some fragile dance to fig= ure out whether maybe they can share the context with some other, previous fence_array. If you just want to avoid allocating a new context if all the fences in the array are from the same one, then I'd say detect that and just retu= rn that single fence directly (with an additional reference ofc). Or I'm not entirely understanding things, or probably not at all ;-) -Daniel >=20 > Signed-off-by: Gustavo Padovan > Signed-off-by: Christian K=F6nig > --- > drivers/dma-buf/Makefile | 2 +- > drivers/dma-buf/fence-array.c | 127 ++++++++++++++++++++++++++++++++= ++++++++++ > include/linux/fence-array.h | 72 ++++++++++++++++++++++++ > 3 files changed, 200 insertions(+), 1 deletion(-) > create mode 100644 drivers/dma-buf/fence-array.c > create mode 100644 include/linux/fence-array.h >=20 > diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile > index 57a675f..85f6928 100644 > --- a/drivers/dma-buf/Makefile > +++ b/drivers/dma-buf/Makefile > @@ -1 +1 @@ > -obj-y :=3D dma-buf.o fence.o reservation.o seqno-fence.o > +obj-y :=3D dma-buf.o fence.o reservation.o seqno-fence.o fence-array= =2Eo > diff --git a/drivers/dma-buf/fence-array.c b/drivers/dma-buf/fence-ar= ray.c > new file mode 100644 > index 0000000..bbcf79f > --- /dev/null > +++ b/drivers/dma-buf/fence-array.c > @@ -0,0 +1,127 @@ > +/* > + * fence-array: aggregate fences to be waited together > + * > + * Copyright (C) 2016 Collabora Ltd > + * Copyright (C) 2016 Advanced Micro Devices, Inc. > + * Authors: > + * Gustavo Padovan > + * Christian K=F6nig > + * > + * This program is free software; you can redistribute it and/or mod= ify it > + * under the terms of the GNU General Public License version 2 as pu= blished by > + * the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, b= ut WITHOUT > + * ANY WARRANTY; without even the implied warranty of MERCHANTABILIT= Y or > + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public Lic= ense for > + * more details. > + */ > + > +#include > +#include > +#include > + > +static void fence_array_cb_func(struct fence *f, struct fence_cb *cb= ); > + > +static const char *fence_array_get_driver_name(struct fence *fence) > +{ > + return "fence_array"; > +} > + > +static const char *fence_array_get_timeline_name(struct fence *fence= ) > +{ > + return "unbound"; > +} > + > +static void fence_array_cb_func(struct fence *f, struct fence_cb *cb= ) > +{ > + struct fence_array_cb *array_cb =3D > + container_of(cb, struct fence_array_cb, cb); > + struct fence_array *array =3D array_cb->array; > + > + if (atomic_dec_and_test(&array->num_pending)) > + fence_signal(&array->base); > +} > + > +static bool fence_array_enable_signaling(struct fence *fence) > +{ > + struct fence_array *array =3D to_fence_array(fence); > + struct fence_array_cb *cb =3D (void *)(&array[1]); > + unsigned i; > + > + for (i =3D 0; i < array->num_fences; ++i) { > + cb[i].array =3D array; > + if (!fence_add_callback(array->fences[i], &cb[i].cb, > + fence_array_cb_func)) > + if (atomic_dec_and_test(&array->num_pending)) > + return false; > + } > + > + return true; > +} > + > +static bool fence_array_signaled(struct fence *fence) > +{ > + struct fence_array *array =3D to_fence_array(fence); > + > + return atomic_read(&array->num_pending) =3D=3D 0; > +} > + > +static void fence_array_release(struct fence *fence) > +{ > + struct fence_array *array =3D to_fence_array(fence); > + unsigned i; > + > + for (i =3D 0; i < array->num_fences; ++i) > + fence_put(array->fences[i]); > + > + kfree(array->fences); > + fence_free(fence); > +} > + > +const struct fence_ops fence_array_ops =3D { > + .get_driver_name =3D fence_array_get_driver_name, > + .get_timeline_name =3D fence_array_get_timeline_name, > + .enable_signaling =3D fence_array_enable_signaling, > + .signaled =3D fence_array_signaled, > + .wait =3D fence_default_wait, > + .release =3D fence_array_release, > +}; > + > +/** > + * fence_array_create - Create a custom fence array > + * @num_fences: [in] number of fences to add in the array > + * @fences: [in] array containing the fences > + * @context: [in] fence context to use > + * @seqno: [in] sequence number to use > + * > + * Allocate a fence_array object and initialize the base fence with = fence_init(). > + * In case of error it returns NULL. > + * > + * The caller should allocte the fences array with num_fences size > + * and fill it with the fences it wants to add to the object. Owners= hip of this > + * array is take and fence_put() is used on each fence on release. > + */ > +struct fence_array *fence_array_create(int num_fences, struct fence = **fences, > + u64 context, unsigned seqno) > +{ > + struct fence_array *array; > + size_t size =3D sizeof(*array); > + > + /* Allocate the callback structures behind the array. */ > + size +=3D num_fences * sizeof(struct fence_array_cb); > + array =3D kzalloc(size, GFP_KERNEL); > + if (!array) > + return NULL; > + > + spin_lock_init(&array->lock); > + fence_init(&array->base, &fence_array_ops, &array->lock, > + context, seqno); > + > + array->num_fences =3D num_fences; > + atomic_set(&array->num_pending, num_fences); > + array->fences =3D fences; > + > + return array; > +} > +EXPORT_SYMBOL(fence_array_create); > diff --git a/include/linux/fence-array.h b/include/linux/fence-array.= h > new file mode 100644 > index 0000000..593ab98 > --- /dev/null > +++ b/include/linux/fence-array.h > @@ -0,0 +1,72 @@ > +/* > + * fence-array: aggregates fence to be waited together > + * > + * Copyright (C) 2016 Collabora Ltd > + * Copyright (C) 2016 Advanced Micro Devices, Inc. > + * Authors: > + * Gustavo Padovan > + * Christian K=F6nig > + * > + * This program is free software; you can redistribute it and/or mod= ify it > + * under the terms of the GNU General Public License version 2 as pu= blished by > + * the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, b= ut WITHOUT > + * ANY WARRANTY; without even the implied warranty of MERCHANTABILIT= Y or > + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public Lic= ense for > + * more details. > + */ > + > +#ifndef __LINUX_FENCE_ARRAY_H > +#define __LINUX_FENCE_ARRAY_H > + > +#include > + > +/** > + * struct fence_array_cb - callback helper for fence array > + * @cb: fence callback structure for signaling > + * @array: reference to the parent fence array object > + */ > +struct fence_array_cb { > + struct fence_cb cb; > + struct fence_array *array; > +}; > + > +/** > + * struct fence_array - fence to represent an array of fences > + * @base: fence base class > + * @lock: spinlock for fence handling > + * @num_fences: number of fences in the array > + * @num_pending: fences in the array still pending > + * @fences: array of the fences > + */ > +struct fence_array { > + struct fence base; > + > + spinlock_t lock; > + unsigned num_fences; > + atomic_t num_pending; > + struct fence **fences; > +}; > + > +extern const struct fence_ops fence_array_ops; > + > +/** > + * to_fence_array - cast a fence to a fence_array > + * @fence: fence to cast to a fence_array > + * > + * Returns NULL if the fence is not a fence_array, > + * or the fence_array otherwise. > + */ > +static inline struct fence_array *to_fence_array(struct fence *fence= ) > +{ > + if (fence->ops !=3D &fence_array_ops) > + return NULL; > + > + return container_of(fence, struct fence_array, base); > +} > + > +struct fence_array *fence_array_create(int num_fences, struct fence = **fences, > + u64 context, unsigned seqno); > + > +#endif /* __LINUX_FENCE_ARRAY_H */ > --=20 > 2.5.0 >=20 --=20 Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch