dma-fence: dma-buf synchronization (v7)
Subsystems (3)
PATCH 1/3 dma-fence: dma-buf synchronization (v7)
A dma-fence can be attached to a buffer which is being filled or consumed by hw, to allow userspace to pass the buffer without waiting to another device. For example, userspace can call page_flip ioctl to display the next frame of graphics after kicking the GPU but while the GPU is still rendering. The display device sharing the buffer with the GPU would attach a callback to get notified when the GPU's rendering-complete IRQ fires, to update the scan-out address of the display, without having to wake up userspace.
A dma-fence is transient, one-shot deal. It is allocated and attached to one or more dma-buf's. When the one that attached it is done, with the pending operation, it can signal the fence.
+ dma_fence_signal()
The dma-buf-mgr handles tracking, and waiting on, the fences associated with a dma-buf.
TODO maybe need some helper fxn for simple devices, like a display- only drm/kms device which simply wants to wait for exclusive fence to be signaled, and then attach a non-exclusive fence while scanout is in progress.
The one pending on the fence can add an async callback:
+ dma_fence_add_callback()
The callback can optionally be cancelled with remove_wait_queue()
Or wait synchronously (optionally with timeout or interruptible):
+ dma_fence_wait()
A default software-only implementation is provided, which can be used by drivers attaching a fence to a buffer when they have no other means for hw sync. But a memory backed fence is also envisioned, because it is common that GPU's can write to, or poll on some memory location for synchronization. For example:
fence = dma_buf_get_fence(dmabuf);
if (fence->ops == &bikeshed_fence_ops) {
dma_buf *fence_buf;
dma_bikeshed_fence_get_buf(fence, &fence_buf, &offset);
... tell the hw the memory location to wait on ...
} else {
/* fall-back to sw sync * /
dma_fence_add_callback(fence, my_cb);
}On SoC platforms, if some other hw mechanism is provided for synchronizing between IP blocks, it could be supported as an alternate implementation with it's own fence ops in a similar way.
To facilitate other non-sw implementations, the enable_signaling callback can be used to keep track if a device not supporting hw sync is waiting on the fence, and in this case should arrange to call dma_fence_signal() at some point after the condition has changed, to notify other devices waiting on the fence. If there are no sw waiters, this can be skipped to avoid waking the CPU unnecessarily. The handler of the enable_signaling op should take a refcount until the fence is signaled, then release its ref.
The intention is to provide a userspace interface (presumably via eventfd) later, to be used in conjunction with dma-buf's mmap support for sw access to buffers (or for userspace apps that would prefer to do their own synchronization).
v1: Original
v2: After discussion w/ danvet and mlankhorst on #dri-devel, we decided
that dma-fence didn't need to care about the sw->hw signaling path (it can be handled same as sw->sw case), and therefore the fence->ops can be simplified and more handled in the core. So remove the signal, add_callback, cancel_callback, and wait ops, and replace with a simple enable_signaling() op which can be used to inform a fence supporting hw->hw signaling that one or more devices which do not support hw signaling are waiting (and therefore it should enable an irq or do whatever is necessary in order that the CPU is notified when the fence is passed).
v3: Fix locking fail in attach_fence() and get_fence()
v4: Remove tie-in w/ dma-buf.. after discussion w/ danvet and mlankorst
we decided that we need to be able to attach one fence to N dma-buf's, so using the list_head in dma-fence struct would be problematic.
v5: [ Maarten Lankhorst ] Updated for dma-bikeshed-fence and dma-buf-manager.
v6: [ Maarten Lankhorst ] I removed dma_fence_cancel_callback and some comments
about checking if fence fired or not. This is broken by design. waitqueue_active during destruction is now fatal, since the signaller should be holding a reference in enable_signalling until it signalled the fence. Pass the original dma_fence_cb along, and call __remove_wait in the dma_fence_callback handler, so that no cleanup needs to be performed.
v7: [ Maarten Lankhorst ] Set cb->func and only enable sw signaling if
fence wasn't signaled yet, for example for hardware fences that may choose to signal blindly.
--- drivers/base/Makefile | 2 drivers/base/dma-fence.c | 287 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/dma-fence.h | 96 +++++++++++++++ 3 files changed, 384 insertions(+), 1 deletion(-) create mode 100644 drivers/base/dma-fence.c create mode 100644 include/linux/dma-fence.h
drivers/base/Makefile +1 -1
@@ -10,7 +10,7 @@ obj-$(CONFIG_CMA) += dma-contiguous.o obj-y += power/ obj-$(CONFIG_HAS_DMA) += dma-mapping.o obj-$(CONFIG_HAVE_GENERIC_DMA_COHERENT) += dma-coherent.o -obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o +obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o dma-fence.o obj-$(CONFIG_ISA) += isa.o obj-$(CONFIG_FW_LOADER) += firmware_class.o obj-$(CONFIG_NUMA) += node.o
drivers/base/dma-fence.c new file +287 -0
287 lines not on this page · raw message
include/linux/dma-fence.h new file +96 -0
96 lines not on this page · raw message
PATCH 2/3 dma-bikeshed-fence: Hardware dma-buf implementation of fencing
This type of fence can be used with hardware synchronization for simple hardware that can block execution until the condition dma_buf[offset] >= value has been met, accounting for wraparound.
A software fallback still has to be provided in case the fence is used with a device that doesn't support this mechanism. It is useful to expose this for graphics cards that have an op to support this.
Some cards like i915 can export those, but don't have an option to wait, so they need the software fallback.
I extended the original patch by Rob Clark.
--- drivers/base/Makefile | 2 - drivers/base/dma-bikeshed-fence.c | 44 +++++++++++++++++ include/linux/dma-bikeshed-fence.h | 92 ++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 drivers/base/dma-bikeshed-fence.c create mode 100644 include/linux/dma-bikeshed-fence.h
drivers/base/Makefile +1 -1
@@ -10,7 +10,7 @@ obj-$(CONFIG_CMA) += dma-contiguous.o obj-y += power/ obj-$(CONFIG_HAS_DMA) += dma-mapping.o obj-$(CONFIG_HAVE_GENERIC_DMA_COHERENT) += dma-coherent.o -obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o dma-fence.o +obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o dma-fence.o dma-bikeshed-fence.o obj-$(CONFIG_ISA) += isa.o obj-$(CONFIG_FW_LOADER) += firmware_class.o obj-$(CONFIG_NUMA) += node.o
drivers/base/dma-bikeshed-fence.c new file +44 -0
44 lines not on this page · raw message
include/linux/dma-bikeshed-fence.h new file +92 -0
92 lines not on this page · raw message
PATCH 3/3 dma-buf-mgr: multiple dma-buf synchronization (v3)
In reply to Maarten Lankhorstdma-buf-mgr handles the case of reserving single or multiple dma-bufs while trying to prevent deadlocks from buffers being reserved simultaneously. For this to happen extra functions have been introduced:
+ dma_buf_reserve()
+ dma_buf_unreserve()
+ dma_buf_wait_unreserved()
Reserve a single buffer, optionally with a sequence to indicate this is part of a multi-dmabuf reservation. This function will return -EDEADLK and return immediately if reserving would cause a deadlock. In case a single buffer is being reserved, no sequence is needed, otherwise please use the dmabufmgr calls.
If you want to attach a exclusive dma-fence, you have to wait until all shared fences have signalled completion. If there are none, or if a shared fence has to be attached, wait until last exclusive fence has signalled completion.
The new fence has to be attached before unreserving the buffer, and in exclusive mode all previous fences will have be removed from the buffer, and unreffed when done with it.
dmabufmgr methods:
+ dmabufmgr_validate_init()
This function inits a dmabufmgr_validate structure and appends it to the tail of the list, with refcount set to 1.
+ dmabufmgr_validate_put()
Convenience function to unref and free a dmabufmgr_validate structure. However if it's used for custom callback signalling, a custom function should be implemented.
+ dmabufmgr_reserve_buffers()
This function takes a linked list of dmabufmgr_validate's, each one requires the following members to be set by the caller:
- validate->head, list head
- validate->bo, must be set to the dma-buf to reserve.
- validate->shared, set to true if opened in shared mode.
- validate->priv, can be used by the caller to identify this buffer.
This function will then set the following members on succesful completion:
- validate->num_fences, amount of valid fences to wait on before this buffer can be accessed. This can be 0.
- validate->fences[0...num_fences-1] fences to wait on
+ dmabufmgr_backoff_reservation()
This can be used when the caller encounters an error between reservation and usage. No new fence will be attached and all reservations will be undone without side effects.
+ dmabufmgr_fence_buffer_objects
Upon successful completion a new fence will have to be attached. This function releases old fences and attaches the new one.
+ dmabufmgr_wait_completed_cpu
A simple cpu waiter convenience function. Waits until all fences have signalled completion before returning.
The rationale of refcounting dmabufmgr_validate lies in the wait dma_fence_cb wait member. Before calling dma_fence_add_callback you should increase the refcount on dmabufmgr_validate with dmabufmgr_validate_get, and on signal completion you should call kref_put(&val->refcount, custom_free_signal); after all callbacks have added you drop the refcount by 1 also, when refcount drops to 0 all callbacks have been signalled, and dmabufmgr_validate has been waited on and can be freed. Since this will require atomic spinlocks to unlink the list and signal completion, a deadlock could occur if you try to call add_callback otherwise, so the refcount is used as a means of preventing this from occuring by having your custom free function take a device specific lock, removing from list and freeing the data. The nice/evil part about this is that this will also guarantee no memory leaks can occur behind your back. This allows delays completion by moving the dmabufmgr_validate list to be a part of the committed reservation.
--- drivers/base/Makefile | 3 - drivers/base/dma-buf-mgr.c | 240 +++++++++++++++++++++++++++++++++++++++++++ drivers/base/dma-buf.c | 113 ++++++++++++++++++++ drivers/base/dma-fence.c | 1 include/linux/dma-buf-mgr.h | 123 ++++++++++++++++++++++ include/linux/dma-buf.h | 31 ++++++ include/linux/dma-fence.h | 2 7 files changed, 511 insertions(+), 2 deletions(-) create mode 100644 drivers/base/dma-buf-mgr.c create mode 100644 include/linux/dma-buf-mgr.h
drivers/base/Makefile +2 -1
@@ -10,7 +10,8 @@ obj-$(CONFIG_CMA) += dma-contiguous.o obj-y += power/ obj-$(CONFIG_HAS_DMA) += dma-mapping.o obj-$(CONFIG_HAVE_GENERIC_DMA_COHERENT) += dma-coherent.o -obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o dma-fence.o dma-bikeshed-fence.o +obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o dma-fence.o dma-buf-mgr.o \ + dma-bikeshed-fence.o obj-$(CONFIG_ISA) += isa.o obj-$(CONFIG_FW_LOADER) += firmware_class.o obj-$(CONFIG_NUMA) += node.o
drivers/base/dma-buf-mgr.c new file +240 -0
240 lines not on this page · raw message
drivers/base/dma-buf.c +113 -0
140 lines not on this page · raw message
drivers/base/dma-fence.c +1 -0
@@ -20,6 +20,7 @@#include <linux/slab.h> #include <linux/sched.h> #include <linux/export.h> +#include <linux/dma-buf.h> #include <linux/dma-fence.h> /**
include/linux/dma-buf-mgr.h new file +123 -0
123 lines not on this page · raw message
include/linux/dma-buf.h +31 -0
@@ -35,6 +35,13 @@ struct device;struct dma_buf ;struct dma_buf_attachment ; +#include <linux/dma-fence.h> + +extern atomic_t dma_buf_reserve_counter; +extern spinlock_t dma_buf_reserve_lock; + +#define DMA_BUF_MAX_SHARED_FENCE 8 +/** * struct dma_buf_ops - operations possible on struct dma_buf * @attach: [optional] allows different devices to 'attach' themselves to the
@@ -113,6 +120,8 @@ struct dma_buf_ops {* @attachments: list of dma_buf_attachment that denotes all devices attached. * @ops: dma_buf_ops associated with this buffer object. * @priv: exporter specific private data for this buffer object. +* @bufmgr_entry: used by dmabufmgr +* @bufdev: used by dmabufmgr */ struct dma_buf {size_t size;
@@ -122,6 +131,18 @@ struct dma_buf {/* mutex to serialize list manipulation and attach/detach */ struct mutex lock;void *priv; + +/** event queue for waking up when this dmabuf becomes unreserved */ +wait_queue_head_t event_queue; + +atomic_t reserved; + +/** These require dma_buf_reserve to be called before modification */ +bool seq_valid; +u32 val_seq; +struct dma_fence *fence_excl; +struct dma_fence *fence_shared[DMA_BUF_MAX_SHARED_FENCE ]; +u32 fence_shared_count; };/**
@@ -188,6 +209,14 @@ int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *, unsigned long);void *dma_buf_vmap (struct dma_buf *);void dma_buf_vunmap (struct dma_buf *,void *vaddr); +int dma_buf_reserve_locked (struct dma_buf *,bool intr,bool no_wait, +bool use_seq,u32 seq); +int dma_buf_reserve (struct dma_buf *,bool intr,bool no_wait, +bool use_seq,u32 seq); +int dma_buf_wait_unreserved (struct dma_buf *,bool interruptible); +void dma_buf_unreserve_locked (struct dma_buf *); +void dma_buf_unreserve (struct dma_buf *); +#else static inline struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
@@ -300,6 +329,8 @@ static inline void *dma_buf_vmap(struct dma_buf *dmabuf)static inline void dma_buf_vunmap (struct dma_buf *dmabuf,void *vaddr) { } + +// TODO #endif /* CONFIG_DMA_SHARED_BUFFER */ #endif /* __DMA_BUF_H__ */
include/linux/dma-fence.h +1 -1
@@ -24,7 +24,7 @@#include <linux/list.h> #include <linux/wait.h> #include <linux/list.h> -#include <linux/dma-buf.h> +#include <linux/kref.h> struct dma_fence ;struct dma_fence_ops ;
Op 07-08-12 19:53, Maarten Lankhorst schreef:
Quoted 8 lines
A dma-fence can be attached to a buffer which is being filled or consumed by hw, to allow userspace to pass the buffer without waiting to another device. For example, userspace can call
page_flipioctl to display the next frame of graphics after kicking the GPU but while the GPU is still rendering. The display device sharing the buffer with the GPU would attach a callback to get notified when the GPU's rendering-complete IRQ fires, to update the scan-out address of the display, without having to wake up userspace.
I implemented this for intel and debugged it with intel <-> nouveau interaction. Unfortunately the nouveau patches aren't ready at this point, but the git repo I'm using is available at:
http://cgit.freedesktop.org/~mlankhorst/linux/
It has the patch series and a sample implementation for intel, based on drm-intel-next tree.
I tried to keep it deadlock and race condition free as much as possible, but locking gets complicated enough that if I'm unlucky something might have slipped through regardless.
Especially the locking in i915_gem_reset_requests, is screwed up. This shows what a real PITA it is to abort callbacks prematurely while keeping everything stable. As such, aborting requests should only be done in exceptional circumstances, in this case hardware died and things are already locked up anyhow..
~Maarten
Sumit Semwal
maintainerHi Maarten,
Quoted 9 lines · Maarten Lankhorst
Op 07-08-12 19:53, Maarten Lankhorst schreef:
Quoted 8 lines
A dma-fence can be attached to a buffer which is being filled or consumed by hw, to allow userspace to pass the buffer without waiting to another device. For example, userspace can call
page_flipioctl to display the next frame of graphics after kicking the GPU but while the GPU is still rendering. The display device sharing the buffer with the GPU would attach a callback to get notified when the GPU's rendering-complete IRQ fires, to update the scan-out address of the display, without having to wake up userspace.
Thanks for this patchset; Could you please also fill up Documentation/dma-buf-sharing.txt, to include the relevant bits?
We've tried to make sure the Documentation corresponding is kept up-to-date as the framework has grown, and new features are added to it - and I think features as important as dma-fence and dmabufmgr do warrant a healthy update.
Quoted 22 lines
I implemented this for intel and debugged it with intel <-> nouveau interaction. Unfortunately the nouveau patches aren't ready at this point, but the git repo I'm using is available at:
http://cgit.freedesktop.org/~mlankhorst/linux/
It has the patch series and a sample implementation for intel, based on drm-intel-next tree.
I tried to keep it deadlock and race condition free as much as possible, but locking gets complicated enough that if I'm unlucky something might have slipped through regardless.
Especially the locking in
i915_gem_reset_requests, is screwed up. This shows what a real PITA it is to abort callbacks prematurely while keeping everything stable. As such, aborting requests should only be done in exceptional circumstances, in this case hardware died and things are already locked up anyhow..~Maarten
Signature · 2 lines
Thanks and best regards, Sumit Semwal
Hey Sumit,
Op 08-08-12 08:35, Sumit Semwal schreef:
Quoted 13 lines
Hi Maarten,
Quoted 9 lines · Maarten Lankhorst
Op 07-08-12 19:53, Maarten Lankhorst schreef:
Quoted 8 lines
A dma-fence can be attached to a buffer which is being filled or consumed by hw, to allow userspace to pass the buffer without waiting to another device. For example, userspace can call
page_flipioctl to display the next frame of graphics after kicking the GPU but while the GPU is still rendering. The display device sharing the buffer with the GPU would attach a callback to get notified when the GPU's rendering-complete IRQ fires, to update the scan-out address of the display, without having to wake up userspace.
Quoted 7 lines
Thanks for this patchset; Could you please also fill up
Documentation/dma-buf-sharing.txt, to include the relevant bits?We've tried to make sure the Documentation corresponding is kept up-to-date as the framework has grown, and new features are added to it - and I think features as important as dma-fence and dmabufmgr do warrant a healthy update.
Ok I'll clean it up and add the documentation, one other question. If code that requires dmabuf needs to select CONFIG_DMA_SHARED_BUFFER, why does dma-buf.h have fallbacks for !CONFIG_DMA_SHARED_BUFFER? This seems weird, would you have any objection if I removed those?
~Maarten