Skip to contentSkip to conversation

dma-buf: add dma_fence_chain_for_each_unwrap helper

PATCH In review Christian König 2 participants 3 messages Jul 30, 2019

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 dma-buf: add dma_fence_chain_for_each_unwrap helper

Add another for_each helper to iterate over all the fences in a chain with unwrapping each chain node.

---
 drivers/dma-buf/dma-fence-chain.c | 11 ++++------
 include/linux/dma-fence-chain.h   | 34 +++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 7 deletions(-)
drivers/dma-buf/dma-fence-chain.c +4 -7
index 93c42078cb57..6c3a615731b4 100644
@@ -151,12 +151,10 @@ static void dma_fence_chain_cb(struct dma_fence *f, struct dma_fence_cb *cb)
 static bool dma_fence_chain_enable_signaling(struct dma_fence *fence)
 {
 	struct dma_fence_chain *head = to_dma_fence_chain(fence);
+	struct dma_fence *f;
 
 	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_chain_for_each_unwrap(f, fence, &head->base) {
 		dma_fence_get(f);
 		if (!dma_fence_add_callback(f, &head->cb, dma_fence_chain_cb)) {
 			dma_fence_put(fence);
@@ -170,10 +168,9 @@ static bool dma_fence_chain_enable_signaling(struct dma_fence *fence)
 
 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;
+	struct dma_fence *f;
 
+	dma_fence_chain_for_each_unwrap(f, fence, fence) {
 		if (!dma_fence_is_signaled(f)) {
 			dma_fence_put(fence);
 			return false;
include/linux/dma-fence-chain.h +34 -0
index 934a442db8ac..7466d7b8837e 100644
@@ -59,6 +59,24 @@ to_dma_fence_chain(struct dma_fence *fence)
 	return container_of(fence, struct dma_fence_chain, base);
 }
 
+/**
+ * dma_fence_chain_unwrap - unwrap chain node
+ * @fence: fence which could be a chain node
+ *
+ * If the paramter is a chain node return the cotained fence, otherwise return
+ * the parameter itself.
+ */
+static inline struct dma_fence *
+dma_fence_chain_unwrap(struct dma_fence *fence)
+{
+	struct dma_fence_chain *chain = to_dma_fence_chain(fence);
+
+	if (!chain)
+		return fence;
+
+	return chain->fence;
+}
+
 /**
  * dma_fence_chain_for_each - iterate over all fences in chain
  * @iter: current fence
@@ -71,6 +89,22 @@ to_dma_fence_chain(struct dma_fence *fence)
 	for (iter = dma_fence_get(head); iter; \
 	     iter = dma_fence_chain_walk(iter))
 
+/**
+ * dma_fence_chain_for_each_unwrap - iterate over all unwrapped fences in chain
+ * @fence: the unwrapped fence
+ * @iter: current fence
+ * @head: starting point
+ *
+ * Iterate over all fences in the chain with unwrapping. 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_unwrap(fence, iter, head)	\
+	for (iter = dma_fence_get(head),			\
+	     fence = dma_fence_chain_unwrap(iter);		\
+	     iter; iter = dma_fence_chain_walk(iter),		\
+	     fence = dma_fence_chain_unwrap(iter))
+
 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,
Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH 2/2 dma-buf: add dma_fence_chain_(alloc|free)

Wrap kmalloc/kfree to allow switching to a slab allocator later on.

---
 drivers/dma-buf/dma-fence-chain.c      |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c |  6 +++---
 drivers/gpu/drm/drm_syncobj.c          |  2 +-
 include/linux/dma-fence-chain.h        | 24 ++++++++++++++++++++++++
 4 files changed, 29 insertions(+), 5 deletions(-)
drivers/dma-buf/dma-fence-chain.c +1 -1
index 6c3a615731b4..56183dc9f787 100644
@@ -186,7 +186,7 @@ static void dma_fence_chain_release(struct dma_fence *fence)
 
 	dma_fence_put(rcu_dereference_protected(chain->prev, true));
 	dma_fence_put(chain->fence);
-	dma_fence_free(fence);
+	dma_fence_chain_free(chain);
 }
 
 const struct dma_fence_ops dma_fence_chain_ops = {
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -3
index def029ab5657..4b1e4b321999 100644
@@ -758,7 +758,7 @@ static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,
 
 	for (i = 0; i < parser->num_post_deps; i++) {
 		drm_syncobj_put(parser->post_deps[i].syncobj);
-		kfree(parser->post_deps[i].chain);
+		dma_fence_chain_free(parser->post_deps[i].chain);
 	}
 	kfree(parser->post_deps);
 
@@ -1187,7 +1187,7 @@ static int amdgpu_cs_process_syncobj_timeline_out_dep(struct amdgpu_cs_parser *p
 
 		dep->chain = NULL;
 		if (syncobj_deps[i].point) {
-			dep->chain = kmalloc(sizeof(*dep->chain), GFP_KERNEL);
+			dep->chain = dma_fence_chain_alloc();
 			if (!dep->chain)
 				return -ENOMEM;
 		}
@@ -1195,7 +1195,7 @@ static int amdgpu_cs_process_syncobj_timeline_out_dep(struct amdgpu_cs_parser *p
 		dep->syncobj = drm_syncobj_find(p->filp,
 						syncobj_deps[i].handle);
 		if (!dep->syncobj) {
-			kfree(dep->chain);
+			dma_fence_chain_free(dep->chain);
 			return -EINVAL;
 		}
 		dep->point = syncobj_deps[i].point;
drivers/gpu/drm/drm_syncobj.c +1 -1
index cecff2e447b1..fc73f398e738 100644
@@ -696,7 +696,7 @@ static int drm_syncobj_transfer_to_timeline(struct drm_file *file_private,
 				     &fence);
 	if (ret)
 		goto err;
-	chain = kzalloc(sizeof(struct dma_fence_chain), GFP_KERNEL);
+	chain = dma_fence_chain_alloc();
 	if (!chain) {
 		ret = -ENOMEM;
 		goto err1;
include/linux/dma-fence-chain.h +24 -0
index 7466d7b8837e..b84e28661d48 100644
@@ -18,6 +18,7 @@
 #ifndef __LINUX_DMA_FENCE_CHAIN_H
 #define __LINUX_DMA_FENCE_CHAIN_H
 
+#include <linux/slab.h>
 #include <linux/dma-fence.h>
 #include <linux/irq_work.h>
 
@@ -43,6 +44,29 @@ struct dma_fence_chain {
 
 extern const struct dma_fence_ops dma_fence_chain_ops;
 
+/**
+ * dma_fence_chain_alloc - wrapper to allocate a dma_fence_chain
+ *
+ * Wrap the kmalloc for now to easier switch to a slub allocator.
+ *
+ * Returns an allocated dma_fence_chain or NULL.
+ */
+static inline struct dma_fence_chain *dma_fence_chain_alloc(void)
+{
+	return kmalloc(sizeof(struct dma_fence_chain), GFP_KERNEL);
+}
+
+/**
+ * dma_fence_chain_free - wrapper to free a dma_fence_chain
+ * @chain: the chain we need to free.
+ *
+ * Wrap the dma_fence_free for now to easier switch to a slub allocator.
+ */
+static inline void dma_fence_chain_free(struct dma_fence_chain *chain)
+{
+	dma_fence_free(&chain->base);
+}
+
 /**
  * to_dma_fence_chain - cast a fence to a dma_fence_chain
  * @fence: fence to cast to a dma_fence_array
Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw
In reply to Christian König

Quoting Christian König (2019-07-30 13:15:53)

Quoted 7 lines

+/**
+ * dma_fence_chain_unwrap - unwrap chain node
+ * @fence: fence which could be a chain node
+ *
+ * If the paramter is a chain node return the cotained fence, otherwise return
+ * the parameter itself.
+ */

s/paramter/parameter/
s/cotained/contained/

Quoted 16 lines
(quoted diff) +15 -0
+static inline struct dma_fence *
+dma_fence_chain_unwrap(struct dma_fence *fence)
+{
+       struct dma_fence_chain *chain = to_dma_fence_chain(fence);
+
+       if (!chain)
+               return fence;
+
+       return chain->fence;
+}
 
+/**
+ * dma_fence_chain_for_each_unwrap - iterate over all unwrapped fences in chain
+ * @fence: the unwrapped fence
+ * @iter: current fence
+ * @head: starting point

Review

Quoted 10 lines
(quoted diff) +10 -0
+ *
+ * Iterate over all fences in the chain with unwrapping. 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_unwrap(fence, iter, head)     \
+       for (iter = dma_fence_get(head),                        \
+            fence = dma_fence_chain_unwrap(iter);              \
+            iter; iter = dma_fence_chain_walk(iter),           \
+            fence = dma_fence_chain_unwrap(iter))

Why not

for (iter = dma_fence_get(head);
	(fence = dma_fence_chain_unwrap(iter));
	iter = dma_fence_chain_walk(iter))

? It doesn't look like dma_fence_chain_unwrap() can legally return NULL, but if you pass NULL to unwrap it returns NULL.

I'd vote for dma_fence_chain_for_each_fence() in the spirit of list_for_each_entry().
-Chris
_______________________________________________
dri-devel mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel