From: Lionel Landwerlin via dri-devel <dri-devel@lists.freedesktop.org>
To: Chunming Zhou <david1.zhou@amd.com>,
Christian.Koenig@amd.com, dri-devel@lists.freedesktop.org,
amd-gfx@lists.freedesktop.org
Cc: "Christian König" <ckoenig.leichtzumerken@gmail.com>
Subject: Re: [PATCH 02/11] dma-buf: add new dma_fence_chain container v4
Date: Fri, 15 Feb 2019 14:23:39 +0000 [thread overview]
Message-ID: <6c2adaf5-6871-20be-a26d-182f8ca8ab8a@intel.com> (raw)
In-Reply-To: <20181207095601.2058-2-david1.zhou@amd.com>
Hi Christian, David,
For timeline semaphore we need points to signaled in order.
I'm struggling to understand how this fence-chain implementation
preserves ordering of the seqnos.
One of the scenario I can see an issue happening is when you have a
timeline with points 1 & 2 and userspace submits for 2 different engines :
- first with let's say a blitter style engine on point 2
- then a 3d style engine on point 1
Another scenario would be signaling a timeline with points 1 & 2 with
those points in reverse order in the submission array.
-Lionel
On 07/12/2018 09:55, Chunming Zhou wrote:
> From: Christian König <ckoenig.leichtzumerken@gmail.com>
>
> Lockless container implementation similar to a dma_fence_array, but with
> only two elements per node and automatic garbage collection.
>
> v2: properly document dma_fence_chain_for_each, add dma_fence_chain_find_seqno,
> drop prev reference during garbage collection if it's not a chain fence.
> v3: use head and iterator for dma_fence_chain_for_each
> v4: fix reference count in dma_fence_chain_enable_signaling
>
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
> drivers/dma-buf/Makefile | 3 +-
> drivers/dma-buf/dma-fence-chain.c | 241 ++++++++++++++++++++++++++++++
> include/linux/dma-fence-chain.h | 81 ++++++++++
> 3 files changed, 324 insertions(+), 1 deletion(-)
> create mode 100644 drivers/dma-buf/dma-fence-chain.c
> create mode 100644 include/linux/dma-fence-chain.h
>
> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> index 0913a6ccab5a..1f006e083eb9 100644
> --- a/drivers/dma-buf/Makefile
> +++ b/drivers/dma-buf/Makefile
> @@ -1,4 +1,5 @@
> -obj-y := dma-buf.o dma-fence.o dma-fence-array.o reservation.o seqno-fence.o
> +obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
> + reservation.o seqno-fence.o
> obj-$(CONFIG_SYNC_FILE) += sync_file.o
> obj-$(CONFIG_SW_SYNC) += sw_sync.o sync_debug.o
> obj-$(CONFIG_UDMABUF) += udmabuf.o
> diff --git a/drivers/dma-buf/dma-fence-chain.c b/drivers/dma-buf/dma-fence-chain.c
> new file mode 100644
> index 000000000000..0c5e3c902fa0
> --- /dev/null
> +++ b/drivers/dma-buf/dma-fence-chain.c
> @@ -0,0 +1,241 @@
> +/*
> + * fence-chain: chain fences together in a timeline
> + *
> + * Copyright (C) 2018 Advanced Micro Devices, Inc.
> + * Authors:
> + * Christian König <christian.koenig@amd.com>
> + *
> + * 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 <linux/dma-fence-chain.h>
> +
> +static bool dma_fence_chain_enable_signaling(struct dma_fence *fence);
> +
> +/**
> + * dma_fence_chain_get_prev - use RCU to get a reference to the previous fence
> + * @chain: chain node to get the previous node from
> + *
> + * Use dma_fence_get_rcu_safe to get a reference to the previous fence of the
> + * chain node.
> + */
> +static struct dma_fence *dma_fence_chain_get_prev(struct dma_fence_chain *chain)
> +{
> + struct dma_fence *prev;
> +
> + rcu_read_lock();
> + prev = dma_fence_get_rcu_safe(&chain->prev);
> + rcu_read_unlock();
> + return prev;
> +}
> +
> +/**
> + * dma_fence_chain_walk - chain walking function
> + * @fence: current chain node
> + *
> + * Walk the chain to the next node. Returns the next fence or NULL if we are at
> + * the end of the chain. Garbage collects chain nodes which are already
> + * signaled.
> + */
> +struct dma_fence *dma_fence_chain_walk(struct dma_fence *fence)
> +{
> + struct dma_fence_chain *chain, *prev_chain;
> + struct dma_fence *prev, *replacement, *tmp;
> +
> + chain = to_dma_fence_chain(fence);
> + if (!chain) {
> + dma_fence_put(fence);
> + return NULL;
> + }
> +
> + while ((prev = dma_fence_chain_get_prev(chain))) {
> +
> + prev_chain = to_dma_fence_chain(prev);
> + if (prev_chain) {
> + if (!dma_fence_is_signaled(prev_chain->fence))
> + break;
> +
> + replacement = dma_fence_chain_get_prev(prev_chain);
> + } else {
> + if (!dma_fence_is_signaled(prev))
> + break;
> +
> + replacement = NULL;
> + }
> +
> + tmp = cmpxchg(&chain->prev, prev, replacement);
> + if (tmp == prev)
> + dma_fence_put(tmp);
> + else
> + dma_fence_put(replacement);
> + dma_fence_put(prev);
> + }
> +
> + dma_fence_put(fence);
> + return prev;
> +}
> +EXPORT_SYMBOL(dma_fence_chain_walk);
> +
> +/**
> + * dma_fence_chain_find_seqno - find fence chain node by seqno
> + * @pfence: pointer to the chain node where to start
> + * @seqno: the sequence number to search for
> + *
> + * Advance the fence pointer to the chain node which will signal this sequence
> + * number. If no sequence number is provided then this is a no-op.
> + *
> + * Returns EINVAL if the fence is not a chain node or the sequence number has
> + * not yet advanced far enough.
> + */
> +int dma_fence_chain_find_seqno(struct dma_fence **pfence, uint64_t seqno)
> +{
> + struct dma_fence_chain *chain;
> +
> + if (!seqno)
> + return 0;
> +
> + chain = to_dma_fence_chain(*pfence);
> + if (!chain || chain->base.seqno < seqno)
> + return -EINVAL;
> +
> + dma_fence_chain_for_each(*pfence, &chain->base) {
> + if ((*pfence)->context != chain->base.context ||
> + to_dma_fence_chain(*pfence)->prev_seqno < seqno)
> + break;
> + }
> + dma_fence_put(&chain->base);
> +
> + return 0;
> +}
> +EXPORT_SYMBOL(dma_fence_chain_find_seqno);
> +
> +static const char *dma_fence_chain_get_driver_name(struct dma_fence *fence)
> +{
> + return "dma_fence_chain";
> +}
> +
> +static const char *dma_fence_chain_get_timeline_name(struct dma_fence *fence)
> +{
> + return "unbound";
> +}
> +
> +static void dma_fence_chain_irq_work(struct irq_work *work)
> +{
> + struct dma_fence_chain *chain;
> +
> + chain = container_of(work, typeof(*chain), work);
> +
> + /* Try to rearm the callback */
> + if (!dma_fence_chain_enable_signaling(&chain->base))
> + /* Ok, we are done. No more unsignaled fences left */
> + dma_fence_signal(&chain->base);
> + dma_fence_put(&chain->base);
> +}
> +
> +static void dma_fence_chain_cb(struct dma_fence *f, struct dma_fence_cb *cb)
> +{
> + struct dma_fence_chain *chain;
> +
> + chain = container_of(cb, typeof(*chain), cb);
> + irq_work_queue(&chain->work);
> + dma_fence_put(f);
> +}
> +
> +static bool dma_fence_chain_enable_signaling(struct dma_fence *fence)
> +{
> + struct dma_fence_chain *head = to_dma_fence_chain(fence);
> +
> + dma_fence_get(&head->base);
> + dma_fence_chain_for_each(fence, &head->base) {
> + struct dma_fence_chain *chain = to_dma_fence_chain(fence);
> + struct dma_fence *f = chain ? chain->fence : fence;
> +
> + dma_fence_get(f);
> + if (!dma_fence_add_callback(f, &head->cb, dma_fence_chain_cb)) {
> + dma_fence_put(fence);
> + return true;
> + }
> + dma_fence_put(f);
> + }
> + dma_fence_put(&head->base);
> + return false;
> +}
> +
> +static bool dma_fence_chain_signaled(struct dma_fence *fence)
> +{
> + dma_fence_chain_for_each(fence, fence) {
> + struct dma_fence_chain *chain = to_dma_fence_chain(fence);
> + struct dma_fence *f = chain ? chain->fence : fence;
> +
> + if (!dma_fence_is_signaled(f)) {
> + dma_fence_put(fence);
> + return false;
> + }
> + }
> +
> + return true;
> +}
> +
> +static void dma_fence_chain_release(struct dma_fence *fence)
> +{
> + struct dma_fence_chain *chain = to_dma_fence_chain(fence);
> +
> + dma_fence_put(chain->prev);
> + dma_fence_put(chain->fence);
> + dma_fence_free(fence);
> +}
> +
> +const struct dma_fence_ops dma_fence_chain_ops = {
> + .get_driver_name = dma_fence_chain_get_driver_name,
> + .get_timeline_name = dma_fence_chain_get_timeline_name,
> + .enable_signaling = dma_fence_chain_enable_signaling,
> + .signaled = dma_fence_chain_signaled,
> + .release = dma_fence_chain_release,
> +};
> +EXPORT_SYMBOL(dma_fence_chain_ops);
> +
> +/**
> + * dma_fence_chain_init - initialize a fence chain
> + * @chain: the chain node to initialize
> + * @prev: the previous fence
> + * @fence: the current fence
> + *
> + * Initialize a new chain node and either start a new chain or add the node to
> + * the existing chain of the previous fence.
> + */
> +void dma_fence_chain_init(struct dma_fence_chain *chain,
> + struct dma_fence *prev,
> + struct dma_fence *fence,
> + uint64_t seqno)
> +{
> + struct dma_fence_chain *prev_chain = to_dma_fence_chain(prev);
> + uint64_t context;
> +
> + spin_lock_init(&chain->lock);
> + chain->prev = prev;
> + chain->fence = fence;
> + chain->prev_seqno = 0;
> + init_irq_work(&chain->work, dma_fence_chain_irq_work);
> +
> + /* Try to reuse the context of the previous chain node. */
> + if (prev_chain && __dma_fence_is_later(seqno, prev->seqno)) {
> + context = prev->context;
> + chain->prev_seqno = prev->seqno;
> + } else {
> + context = dma_fence_context_alloc(1);
> + /* Make sure that we always have a valid sequence number. */
> + if (prev_chain)
> + seqno = max(prev->seqno, seqno);
> + }
> +
> + dma_fence_init(&chain->base, &dma_fence_chain_ops,
> + &chain->lock, context, seqno);
> +}
> +EXPORT_SYMBOL(dma_fence_chain_init);
> diff --git a/include/linux/dma-fence-chain.h b/include/linux/dma-fence-chain.h
> new file mode 100644
> index 000000000000..a5c2e8c6915c
> --- /dev/null
> +++ b/include/linux/dma-fence-chain.h
> @@ -0,0 +1,81 @@
> +/*
> + * fence-chain: chain fences together in a timeline
> + *
> + * Copyright (C) 2018 Advanced Micro Devices, Inc.
> + * Authors:
> + * Christian König <christian.koenig@amd.com>
> + *
> + * 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.
> + */
> +
> +#ifndef __LINUX_DMA_FENCE_CHAIN_H
> +#define __LINUX_DMA_FENCE_CHAIN_H
> +
> +#include <linux/dma-fence.h>
> +#include <linux/irq_work.h>
> +
> +/**
> + * struct dma_fence_chain - fence to represent an node of a fence chain
> + * @base: fence base class
> + * @lock: spinlock for fence handling
> + * @prev: previous fence of the chain
> + * @prev_seqno: original previous seqno before garbage collection
> + * @fence: encapsulated fence
> + * @cb: callback structure for signaling
> + * @work: irq work item for signaling
> + */
> +struct dma_fence_chain {
> + struct dma_fence base;
> + spinlock_t lock;
> + struct dma_fence *prev;
> + u64 prev_seqno;
> + struct dma_fence *fence;
> + struct dma_fence_cb cb;
> + struct irq_work work;
> +};
> +
> +extern const struct dma_fence_ops dma_fence_chain_ops;
> +
> +/**
> + * to_dma_fence_chain - cast a fence to a dma_fence_chain
> + * @fence: fence to cast to a dma_fence_array
> + *
> + * Returns NULL if the fence is not a dma_fence_chain,
> + * or the dma_fence_chain otherwise.
> + */
> +static inline struct dma_fence_chain *
> +to_dma_fence_chain(struct dma_fence *fence)
> +{
> + if (!fence || fence->ops != &dma_fence_chain_ops)
> + return NULL;
> +
> + return container_of(fence, struct dma_fence_chain, base);
> +}
> +
> +/**
> + * dma_fence_chain_for_each - iterate over all fences in chain
> + * @iter: current fence
> + * @head: starting point
> + *
> + * Iterate over all fences in the chain. We keep a reference to the current
> + * fence while inside the loop which must be dropped when breaking out.
> + */
> +#define dma_fence_chain_for_each(iter, head) \
> + for (iter = dma_fence_get(head); iter; \
> + iter = dma_fence_chain_walk(head))
> +
> +struct dma_fence *dma_fence_chain_walk(struct dma_fence *fence);
> +int dma_fence_chain_find_seqno(struct dma_fence **pfence, uint64_t seqno);
> +void dma_fence_chain_init(struct dma_fence_chain *chain,
> + struct dma_fence *prev,
> + struct dma_fence *fence,
> + uint64_t seqno);
> +
> +#endif /* __LINUX_DMA_FENCE_CHAIN_H */
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel
next prev parent reply other threads:[~2019-02-15 14:23 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-07 9:55 [PATCH 01/11] dma-buf: make fence sequence numbers 64 bit v2 Chunming Zhou
2018-12-07 9:55 ` [PATCH 02/11] dma-buf: add new dma_fence_chain container v4 Chunming Zhou
2019-02-15 14:23 ` Lionel Landwerlin via dri-devel [this message]
[not found] ` <6c2adaf5-6871-20be-a26d-182f8ca8ab8a-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2019-02-15 14:32 ` Koenig, Christian
[not found] ` <e170ceed-fdb7-8b4a-93d7-e565641390b3-5C7GfCeVMHo@public.gmane.org>
2019-02-15 15:52 ` Lionel Landwerlin via amd-gfx
[not found] ` <bbae2023-8dee-692e-9549-40779a202587-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2019-02-15 16:39 ` Christian König via amd-gfx
2019-02-15 16:49 ` Jason Ekstrand
[not found] ` <CAOFGe96HUkzHPJKYT-07X3vMvCRD-=Hba1=Ke24qt_PY2vn0YQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-02-15 17:51 ` Christian König via amd-gfx
[not found] ` <a0b27d87-50f2-56ce-1db7-5a1dc005a798-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2019-02-15 18:16 ` Jason Ekstrand
[not found] ` <CAOFGe9611MqmsvdvZS4_vuJjrrUAmjK5-41Z6tpaxTHJsB8CwA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-02-15 18:33 ` Koenig, Christian
[not found] ` <f933f9ec-6e69-f9df-f12f-5f1844a2ad37-5C7GfCeVMHo@public.gmane.org>
2019-02-15 19:11 ` Jason Ekstrand
2018-12-07 9:55 ` [PATCH 03/11] drm/syncobj: remove drm_syncobj_cb and cleanup Chunming Zhou
[not found] ` <20181207095601.2058-1-david1.zhou-5C7GfCeVMHo@public.gmane.org>
2018-12-07 9:55 ` [PATCH 04/11] drm/syncobj: add new drm_syncobj_add_point interface v2 Chunming Zhou
2018-12-07 9:55 ` [PATCH 06/11] drm/syncobj: add timeline payload query ioctl v4 Chunming Zhou
[not found] ` <20181207095601.2058-6-david1.zhou-5C7GfCeVMHo@public.gmane.org>
2019-02-15 19:31 ` Lionel Landwerlin via amd-gfx
[not found] ` <157f8231-57e2-0492-de5d-f9ba4761c4c9-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2019-02-16 19:22 ` Christian König via amd-gfx
[not found] ` <a24728a8-5b80-e746-a1f2-6555cd817e99-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2019-02-18 3:10 ` zhoucm1
[not found] ` <eae060f6-6493-ef71-ed9d-52d7dd768b03-5C7GfCeVMHo@public.gmane.org>
2019-02-18 7:28 ` Koenig, Christian
[not found] ` <4becddef-3bb3-5a66-34d4-95cced896939-5C7GfCeVMHo@public.gmane.org>
2019-02-18 11:40 ` Lionel Landwerlin
2018-12-07 9:55 ` [PATCH 07/11] drm/syncobj: use the timeline point in drm_syncobj_find_fence v3 Chunming Zhou
2018-12-07 9:55 ` [PATCH 09/11] drm/syncobj: add transition iotcls between binary and timeline Chunming Zhou
2018-12-07 11:28 ` Koenig, Christian
[not found] ` <20181207095601.2058-9-david1.zhou-5C7GfCeVMHo@public.gmane.org>
2019-02-15 14:28 ` Lionel Landwerlin via amd-gfx
2019-02-18 10:35 ` zhoucm1
2019-02-18 11:01 ` Koenig, Christian
[not found] ` <27a38e11-0c77-4340-aac9-b02e816c6f58-2ueSQiBKiTY7tOexoI0I+QC/G2K4zDHf@public.gmane.org>
2019-02-18 12:07 ` Lionel Landwerlin
[not found] ` <83890a08-769a-b52a-f2f6-9fe425f2562c-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2019-02-18 17:01 ` Koenig, Christian
[not found] ` <64c548d0-b062-f937-30a5-5a4d3f296f91-5C7GfCeVMHo@public.gmane.org>
2019-02-19 10:46 ` zhoucm1
[not found] ` <c2c12849-d26b-3212-40ca-682d6f8006fa-5C7GfCeVMHo@public.gmane.org>
2019-02-19 11:29 ` Lionel Landwerlin
2019-02-19 11:32 ` Koenig, Christian
2019-02-20 4:53 ` zhoucm1
2019-02-20 7:59 ` Koenig, Christian
[not found] ` <976d7032-1cde-0427-ce56-38c2ac8881ec-5C7GfCeVMHo@public.gmane.org>
2019-02-20 8:10 ` zhoucm1
[not found] ` <730eaa42-d852-e9d8-7756-43fb256a466f-5C7GfCeVMHo@public.gmane.org>
2019-02-20 8:24 ` Koenig, Christian
2018-12-07 9:56 ` [PATCH 11/11] drm/amdgpu: update version for timeline syncobj support in amdgpu Chunming Zhou
2018-12-07 9:55 ` [PATCH 05/11] drm/syncobj: add support for timeline point wait v8 Chunming Zhou
2018-12-07 9:55 ` [PATCH 08/11] drm/amdgpu: add timeline support in amdgpu CS v2 Chunming Zhou
2018-12-07 9:56 ` [PATCH 10/11] drm/syncobj: add timeline signal ioctl for syncobj Chunming Zhou
[not found] ` <20181207095601.2058-10-david1.zhou-5C7GfCeVMHo@public.gmane.org>
2018-12-07 11:31 ` Christian König
2018-12-07 13:09 ` Chunming Zhou
[not found] ` <8c34aaf0-13b3-4070-f4ef-076fe1ab3197-5C7GfCeVMHo@public.gmane.org>
2018-12-07 13:14 ` Koenig, Christian
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6c2adaf5-6871-20be-a26d-182f8ca8ab8a@intel.com \
--to=dri-devel@lists.freedesktop.org \
--cc=Christian.Koenig@amd.com \
--cc=amd-gfx@lists.freedesktop.org \
--cc=ckoenig.leichtzumerken@gmail.com \
--cc=david1.zhou@amd.com \
--cc=lionel.g.landwerlin@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox