dma-buf: cleanup dma_fence_unwrap selftest v2
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/5 dma-buf: cleanup dma_fence_unwrap selftest v2
The selftests, fix the error handling, remove unused functions and stop leaking memory in failed tests.
v2: fix the memory leak correctly.
--- drivers/dma-buf/st-dma-fence-unwrap.c | 48 +++++++++++---------------- 1 file changed, 19 insertions(+), 29 deletions(-)
drivers/dma-buf/st-dma-fence-unwrap.c +19 -29
@@ -4,27 +4,19 @@* Copyright (C) 2022 Advanced Micro Devices, Inc. */ +#include <linux/dma-fence.h> +#include <linux/dma-fence-array.h> +#include <linux/dma-fence-chain.h> #include <linux/dma-fence-unwrap.h> -#if 0 -#include <linux/kernel.h> -#include <linux/kthread.h> -#include <linux/mm.h> -#include <linux/sched/signal.h> -#include <linux/slab.h> -#include <linux/spinlock.h> -#include <linux/random.h> -#endif #include "selftest.h" #define CHAIN_SZ (4 << 10) -static inline struct mock_fence { +struct mock_fence {struct dma_fence base;spinlock_t lock; -} *to_mock_fence (struct dma_fence *f) { - return container_of(f, struct mock_fence, base); -} +};static const char *mock_name (struct dma_fence *f) {
@@ -45,7 +37,8 @@ static struct dma_fence *mock_fence(void)return NULL ;spin_lock_init (&f->lock); -dma_fence_init (&f->base, &mock_ops, &f->lock,0 ,0 ); +dma_fence_init (&f->base, &mock_ops, &f->lock, +dma_fence_context_alloc (1 ),1 );return &f->base; }
@@ -59,7 +52,7 @@ static struct dma_fence *mock_array(unsigned int num_fences, ...) fences =kcalloc (num_fences,sizeof (*fences),GFP_KERNEL );if (!fences) -return NULL ; + goto error_put;va_start (valist, num_fences);for (i =0 ; i < num_fences; ++i)
@@ -70,13 +63,17 @@ static struct dma_fence *mock_array(unsigned int num_fences, ...)dma_fence_context_alloc (1 ), 1, false);if (!array) - goto cleanup; + goto error_free;return &array->base; -cleanup: -for (i =0 ; i < num_fences; ++i) -dma_fence_put (fences[i]); +error_free:kfree (fences); + +error_put: +va_start (valist, num_fences); +for (i =0 ; i < num_fences; ++i) +dma_fence_put (va_arg (valist,typeof (*fences))); +va_end (valist);return NULL ; }
@@ -113,7 +110,6 @@ static int sanitycheck(void *arg)if (!chain)return -ENOMEM ; -dma_fence_signal (f);dma_fence_put (chain);return err; }
@@ -154,10 +150,8 @@ static int unwrap_array(void *arg) err = -EINVAL ; } -dma_fence_signal (f1); -dma_fence_signal (f2);dma_fence_put (array); -return 0 ; +return err; }static int unwrap_chain (void *arg)
@@ -196,10 +190,8 @@ static int unwrap_chain(void *arg) err = -EINVAL ; } -dma_fence_signal (f1); -dma_fence_signal (f2);dma_fence_put (chain); -return 0 ; +return err; }static int unwrap_chain_array (void *arg)
@@ -242,10 +234,8 @@ static int unwrap_chain_array(void *arg) err = -EINVAL ; } -dma_fence_signal (f1); -dma_fence_signal (f2);dma_fence_put (chain); -return 0 ; +return err; }int dma_fence_unwrap (void )
Signature · 1 line
2.25.1
PATCH 2/5 dma-buf: cleanup dma_fence_unwrap implementation
In reply to a message not in the archiveMove the code from the inline functions into exported functions.
--- drivers/dma-buf/Makefile | 2 +- drivers/dma-buf/dma-fence-unwrap.c | 59 ++++++++++++++++++++++++++++++ include/linux/dma-fence-unwrap.h | 52 ++------------------------ 3 files changed, 64 insertions(+), 49 deletions(-) create mode 100644 drivers/dma-buf/dma-fence-unwrap.c
drivers/dma-buf/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \ - dma-resv.o + dma-fence-unwrap.o dma-resv.o obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o obj-$(CONFIG_DMABUF_HEAPS) += heaps/ obj-$(CONFIG_SYNC_FILE) += sync_file.o
drivers/dma-buf/dma-fence-unwrap.c new file +59 -0
59 lines not on this page · raw message
include/linux/dma-fence-unwrap.h +4 -48
69 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 3/5 dma-buf: return only unsignaled fences in dma_fence_unwrap_for_each v3
In reply to a message not in the archivedma_fence_chain containers cleanup signaled fences automatically, so filter those out from arrays as well.
v2: fix missing walk over the array
v3: massively simplify the patch and actually update the description.
--- include/linux/dma-fence-unwrap.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
include/linux/dma-fence-unwrap.h +5 -1
@@ -43,9 +43,13 @@ struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor);* Unwrap dma_fence_chain and dma_fence_array containers and deep dive into all * potential fences in them. If @head is just a normal fence only that one is * returned. +* +* Note that signalled fences are opportunistically filtered out, which +* means the iteration is potentially over no fence at all. */ #define dma_fence_unwrap_for_each (fence, cursor, head) \ for (fence = dma_fence_unwrap_first(head, cursor); fence; \ - fence = dma_fence_unwrap_next(cursor)) + fence = dma_fence_unwrap_next(cursor)) \ + if (!dma_fence_is_signaled(fence))#endif
Signature · 1 line
2.25.1
PATCH 4/5 dma-buf: generalize dma_fence unwrap & merging v3
In reply to a message not in the archiveIntroduce a dma_fence_unwrap_merge() macro which allows to unwrap fences which potentially can be containers as well and then merge them back together into a flat dma_fence_array.
v2: rename the function, add some more comments about how the wrapper is
used, move filtering of signaled fences into the unwrap iterator, add complex selftest which covers more cases.
v3: fix signaled fence filtering once more
--- drivers/dma-buf/dma-fence-unwrap.c | 103 ++++++++++++++++++++++ drivers/dma-buf/st-dma-fence-unwrap.c | 109 +++++++++++++++++++++++ drivers/dma-buf/sync_file.c | 119 ++------------------------ include/linux/dma-fence-unwrap.h | 24 ++++++ 4 files changed, 242 insertions(+), 113 deletions(-)
drivers/dma-buf/dma-fence-unwrap.c +103 -0
@@ -11,6 +11,7 @@#include <linux/dma-fence-array.h> #include <linux/dma-fence-chain.h> #include <linux/dma-fence-unwrap.h> +#include <linux/slab.h> /* Internal helper to start new array iteration, don't use directly */ static struct dma_fence *
@@ -57,3 +58,105 @@ struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor)return __dma_fence_unwrap_array (cursor); }EXPORT_SYMBOL_GPL (dma_fence_unwrap_next); + +/* Implementation for the dma_fence_merge() marco, don't use directly */ +struct dma_fence *__dma_fence_unwrap_merge (unsigned int num_fences, +struct dma_fence **fences, +struct dma_fence_unwrap *iter) +{ +struct dma_fence_array *result; +struct dma_fence *tmp, **array; +unsigned int i; +size_t count; + + count =0 ; +for (i =0 ; i < num_fences; ++i) { +dma_fence_unwrap_for_each (tmp, &iter[i], fences[i]) + ++count; + } + +if (count ==0 ) +return dma_fence_get_stub (); + + array =kmalloc_array (count,sizeof (*array),GFP_KERNEL ); +if (!array) +return NULL ; + +/* +* This trashes the input fence array and uses it as position for the +* following merge loop. This works because the dma_fence_merge() +* wrapper macro is creating this temporary array on the stack together +* with the iterators. +*/ +for (i =0 ; i < num_fences; ++i) + fences[i] =dma_fence_unwrap_first (fences[i], &iter[i]); + + count =0 ; +do { +unsigned int sel; + +restart: + tmp =NULL ; +for (i =0 ; i < num_fences; ++i) { +struct dma_fence *next; + +while (fences[i] &&dma_fence_is_signaled (fences[i])) + fences[i] =dma_fence_unwrap_next (&iter[i]); + + next = fences[i]; +if (!next) +continue ; + +/* +* We can't guarantee that inpute fences are ordered by +* context, but it is still quite likely when this +* function is used multiple times. So attempt to order +* the fences by context as we pass over them and merge +* fences with the same context. +*/ +if (!tmp || tmp->context > next->context) { + tmp = next; + sel = i; + + }else if (tmp->context < next->context) { +continue ; + + }else if (dma_fence_is_later (tmp, next)) { + fences[i] =dma_fence_unwrap_next (&iter[i]); + goto restart; + }else { + fences[sel] =dma_fence_unwrap_next (&iter[sel]); + goto restart; + } + } + +if (tmp) { + array[count++] =dma_fence_get (tmp); + fences[sel] =dma_fence_unwrap_next (&iter[sel]); + } + }while (tmp); + +if (count ==0 ) { + tmp =dma_fence_get_stub (); + goto return_tmp; + } + +if (count ==1 ) { + tmp = array[0 ]; + goto return_tmp; + } + + result =dma_fence_array_create (count, array, +dma_fence_context_alloc (1 ), +1 , false); +if (!result) { + tmp =NULL ; + goto return_tmp; + } +return &result->base; + +return_tmp: +kfree (array); +return tmp; +} +EXPORT_SYMBOL_GPL (__dma_fence_unwrap_merge);
drivers/dma-buf/st-dma-fence-unwrap.c +109 -0
121 lines not on this page · raw message
drivers/dma-buf/sync_file.c +6 -113
140 lines not on this page · raw message
include/linux/dma-fence-unwrap.h +24 -0
@@ -52,4 +52,28 @@ struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor); fence = dma_fence_unwrap_next(cursor)) \if (!dma_fence_is_signaled (fence)) +struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences, +struct dma_fence **fences, +struct dma_fence_unwrap *cursors); + +/** +* dma_fence_unwrap_merge - unwrap and merge fences +* +* All fences given as parameters are unwrapped and merged back together as flat +* dma_fence_array. Useful if multiple containers need to be merged together. +* +* Implemented as a macro to allocate the necessary arrays on the stack and +* account the stack frame size to the caller. +* +* Returns NULL on memory allocation failure, a dma_fence object representing +* all the given fences otherwise. +*/ +#define dma_fence_unwrap_merge (...) \ + ({ \ + struct dma_fence *__f[] = { __VA_ARGS__ }; \ + struct dma_fence_unwrap __c[ARRAY_SIZE(__f)]; \ + \ + __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f, __c); \ + }) +#endif
Signature · 1 line
2.25.1
PATCH 5/5 drm: use dma_fence_unwrap_merge() in drm_syncobj
In reply to a message not in the archiveThe unwrap merge function is now intended for this use case.
--- drivers/gpu/drm/drm_syncobj.c | 57 +++++------------------------------ 1 file changed, 7 insertions(+), 50 deletions(-)
drivers/gpu/drm/drm_syncobj.c +7 -50
@@ -184,6 +184,7 @@*/ #include <linux/anon_inodes.h> +#include <linux/dma-fence-unwrap.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/sched/signal.h>
@@ -853,57 +854,12 @@ drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data, &args->handle); } - -/* -* Try to flatten a dma_fence_chain into a dma_fence_array so that it can be -* added as timeline fence to a chain again. -*/ -static int drm_syncobj_flatten_chain (struct dma_fence **f) -{ -struct dma_fence_chain *chain =to_dma_fence_chain (*f); -struct dma_fence *tmp, **fences; -struct dma_fence_array *array; -unsigned int count; - -if (!chain) -return 0 ; - - count =0 ; -dma_fence_chain_for_each (tmp, &chain->base) - ++count; - - fences =kmalloc_array (count,sizeof (*fences),GFP_KERNEL ); -if (!fences) -return -ENOMEM ; - - count =0 ; -dma_fence_chain_for_each (tmp, &chain->base) - fences[count++] =dma_fence_get (tmp); - - array =dma_fence_array_create (count, fences, -dma_fence_context_alloc (1 ), -1 , false); -if (!array) - goto free_fences; - -dma_fence_put (*f); - *f = &array->base; -return 0 ; - -free_fences: -while (count--) -dma_fence_put (fences[count]); - -kfree (fences); -return -ENOMEM ; -} -static int drm_syncobj_transfer_to_timeline (struct drm_file *file_private,struct drm_syncobj_transfer *args) {struct drm_syncobj *timeline_syncobj =NULL ; +struct dma_fence *fence, *tmp;struct dma_fence_chain *chain; -struct dma_fence *fence;int ret; timeline_syncobj =drm_syncobj_find (file_private, args->dst_handle);
@@ -912,13 +868,14 @@ static int drm_syncobj_transfer_to_timeline(struct drm_file *file_private, } ret =drm_syncobj_find_fence (file_private, args->src_handle, args->src_point, args->flags, - &fence); + &tmp);if (ret) goto err_put_timeline; - ret =drm_syncobj_flatten_chain (&fence); -if (ret) - goto err_free_fence; + fence =dma_fence_unwrap_merge (tmp); +dma_fence_put (tmp); +if (!fence) + goto err_put_timeline; chain =dma_fence_chain_alloc ();if (!chain) {
Signature · 1 line
2.25.1