Skip to contentSkip to conversation

Enforce dma_fence container rules

PATCH In review Christian König 3 participants 15 messages Jan 20, 2022 – Jan 21, 2022

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

Hi Daniel,

second version of this set.

I've kept the fence ops exported for now since there are indeed valid uses in the drm_syncobj implementation which needs a more wider rework.

But quite a bunch of cases in i915, one in amdgpu and another one in vmwgfx are cleaned up at the end of this series now.

Please review and comment,
Christian.

#1 lore raw

PATCH 1/9 dma-buf: consolidate dma_fence subclass checking

Consolidate the wrapper functions to check for dma_fence subclasses in the dma_fence header.

This makes it easier to document and also check the different requirements for fence containers in the subclasses.

---
 include/linux/dma-fence-array.h | 15 +------------
 include/linux/dma-fence-chain.h |  3 +--
 include/linux/dma-fence.h       | 38 +++++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 16 deletions(-)
include/linux/dma-fence-array.h +1 -14
index 303dd712220f..fec374f69e12 100644
@@ -45,19 +45,6 @@ struct dma_fence_array {
 	struct irq_work work;
 };
 
-extern const struct dma_fence_ops dma_fence_array_ops;
-
-/**
- * dma_fence_is_array - check if a fence is from the array subsclass
- * @fence: fence to test
- *
- * Return true if it is a dma_fence_array and false otherwise.
- */
-static inline bool dma_fence_is_array(struct dma_fence *fence)
-{
-	return fence->ops == &dma_fence_array_ops;
-}
-
 /**
  * to_dma_fence_array - cast a fence to a dma_fence_array
  * @fence: fence to cast to a dma_fence_array
@@ -68,7 +55,7 @@ static inline bool dma_fence_is_array(struct dma_fence *fence)
 static inline struct dma_fence_array *
 to_dma_fence_array(struct dma_fence *fence)
 {
-	if (fence->ops != &dma_fence_array_ops)
+	if (!fence || !dma_fence_is_array(fence))
 		return NULL;
 
 	return container_of(fence, struct dma_fence_array, base);
include/linux/dma-fence-chain.h +1 -2
index 54fe3443fd2c..ee906b659694 100644
@@ -49,7 +49,6 @@ struct dma_fence_chain {
 	spinlock_t lock;
 };
 
-extern const struct dma_fence_ops dma_fence_chain_ops;
 
 /**
  * to_dma_fence_chain - cast a fence to a dma_fence_chain
@@ -61,7 +60,7 @@ extern const struct dma_fence_ops dma_fence_chain_ops;
 static inline struct dma_fence_chain *
 to_dma_fence_chain(struct dma_fence *fence)
 {
-	if (!fence || fence->ops != &dma_fence_chain_ops)
+	if (!fence || !dma_fence_is_chain(fence))
 		return NULL;
 
 	return container_of(fence, struct dma_fence_chain, base);
include/linux/dma-fence.h +38 -0
index 1ea691753bd3..775cdc0b4f24 100644
@@ -587,4 +587,42 @@ struct dma_fence *dma_fence_get_stub(void);
 struct dma_fence *dma_fence_allocate_private_stub(void);
 u64 dma_fence_context_alloc(unsigned num);
 
+extern const struct dma_fence_ops dma_fence_array_ops;
+extern const struct dma_fence_ops dma_fence_chain_ops;
+
+/**
+ * dma_fence_is_array - check if a fence is from the array subclass
+ * @fence: the fence to test
+ *
+ * Return true if it is a dma_fence_array and false otherwise.
+ */
+static inline bool dma_fence_is_array(struct dma_fence *fence)
+{
+	return fence->ops == &dma_fence_array_ops;
+}
+
+/**
+ * dma_fence_is_chain - check if a fence is from the chain subclass
+ * @fence: the fence to test
+ *
+ * Return true if it is a dma_fence_chain and false otherwise.
+ */
+static inline bool dma_fence_is_chain(struct dma_fence *fence)
+{
+	return fence->ops == &dma_fence_chain_ops;
+}
+
+/**
+ * dma_fence_is_container - check if a fence is a container for other fences
+ * @fence: the fence to test
+ *
+ * Return true if this fence is a container for other fences, false otherwise.
+ * This is important since we can't build up large fence structure or otherwise
+ * we run into recursion during operation on those fences.
+ */
+static inline bool dma_fence_is_container(struct dma_fence *fence)
+{
+	return dma_fence_is_array(fence) || dma_fence_is_chain(fence);
+}
+
 #endif /* __LINUX_DMA_FENCE_H */
Signature · 1 line
2.25.1
#2 lore raw

Re: [Linaro-mm-sig] [PATCH 1/9] dma-buf: consolidate dma_fence subclass checking

Quoted 102 lines · Christian König

Consolidate the wrapper functions to check for dma_fence subclasses in the dma_fence header.

This makes it easier to document and also check the different requirements for fence containers in the subclasses.

---
  include/linux/dma-fence-array.h | 15 +------------
  include/linux/dma-fence-chain.h |  3 +--
  include/linux/dma-fence.h       | 38 +++++++++++++++++++++++++++++++++
  3 files changed, 40 insertions(+), 16 deletions(-)
include/linux/dma-fence-array.h +1 -14
index 303dd712220f..fec374f69e12 100644
@@ -45,19 +45,6 @@ struct dma_fence_array {
  	struct irq_work work;
  };
 
-extern const struct dma_fence_ops dma_fence_array_ops;
-
-/**
- * dma_fence_is_array - check if a fence is from the array subsclass
- * @fence: fence to test
- *
- * Return true if it is a dma_fence_array and false otherwise.
- */
-static inline bool dma_fence_is_array(struct dma_fence *fence)
-{
-	return fence->ops == &dma_fence_array_ops;
-}
-
  /**
   * to_dma_fence_array - cast a fence to a dma_fence_array
   * @fence: fence to cast to a dma_fence_array
@@ -68,7 +55,7 @@ static inline bool dma_fence_is_array(struct dma_fence *fence)
  static inline struct dma_fence_array *
  to_dma_fence_array(struct dma_fence *fence)
  {
-	if (fence->ops != &dma_fence_array_ops)
+	if (!fence || !dma_fence_is_array(fence))
  		return NULL;
 
  	return container_of(fence, struct dma_fence_array, base);
include/linux/dma-fence-chain.h +1 -2
index 54fe3443fd2c..ee906b659694 100644
@@ -49,7 +49,6 @@ struct dma_fence_chain {
  	spinlock_t lock;
  };
 
-extern const struct dma_fence_ops dma_fence_chain_ops;
 
  /**
   * to_dma_fence_chain - cast a fence to a dma_fence_chain
@@ -61,7 +60,7 @@ extern const struct dma_fence_ops dma_fence_chain_ops;
  static inline struct dma_fence_chain *
  to_dma_fence_chain(struct dma_fence *fence)
  {
-	if (!fence || fence->ops != &dma_fence_chain_ops)
+	if (!fence || !dma_fence_is_chain(fence))
  		return NULL;
 
  	return container_of(fence, struct dma_fence_chain, base);
include/linux/dma-fence.h +27 -0
index 1ea691753bd3..775cdc0b4f24 100644
@@ -587,4 +587,42 @@ struct dma_fence *dma_fence_get_stub(void);
  struct dma_fence *dma_fence_allocate_private_stub(void);
  u64 dma_fence_context_alloc(unsigned num);
 
+extern const struct dma_fence_ops dma_fence_array_ops;
+extern const struct dma_fence_ops dma_fence_chain_ops;
+
+/**
+ * dma_fence_is_array - check if a fence is from the array subclass
+ * @fence: the fence to test
+ *
+ * Return true if it is a dma_fence_array and false otherwise.
+ */
+static inline bool dma_fence_is_array(struct dma_fence *fence)
+{
+	return fence->ops == &dma_fence_array_ops;
+}
+
+/**
+ * dma_fence_is_chain - check if a fence is from the chain subclass
+ * @fence: the fence to test
+ *
+ * Return true if it is a dma_fence_chain and false otherwise.
+ */
+static inline bool dma_fence_is_chain(struct dma_fence *fence)
+{
+	return fence->ops == &dma_fence_chain_ops;
+}
+
+/**
+ * dma_fence_is_container - check if a fence is a container for other fences

include/linux/dma-fence.h struct dma_fence *dma_fence_get_stub(void); · line 626

Quoted 10 lines
include/linux/dma-fence.h +10 -0
+ * @fence: the fence to test
+ *
+ * Return true if this fence is a container for other fences, false otherwise.
+ * This is important since we can't build up large fence structure or otherwise
+ * we run into recursion during operation on those fences.
+ */
+static inline bool dma_fence_is_container(struct dma_fence *fence)
+{
+	return dma_fence_is_array(fence) || dma_fence_is_chain(fence);
+}

What's the strategy here moving forward if we add more dma_resv containers, or if a driver adds a container that similarly has risc of recursion? Should we perhaps add an ops bool for this, or require that all dma_resv containers that has this limitation be part of the dma-buf subsystem rather than driver-specific?

Thanks,
/Thomas

Quoted 2 lines
include/linux/dma-fence.h +1 -0
+
  #endif /* __LINUX_DMA_FENCE_H */
#3 lore raw

Re: [Linaro-mm-sig] [PATCH 1/9] dma-buf: consolidate dma_fence subclass checking

Am 21.01.22 um 08:41 schrieb Thomas Hellström (Intel):

Quoted 106 lines · Christian König
Quoted 104 lines

Consolidate the wrapper functions to check for dma_fence subclasses in the dma_fence header.

This makes it easier to document and also check the different requirements for fence containers in the subclasses.

---
  include/linux/dma-fence-array.h | 15 +------------ include/linux/dma-fence-chain.h |  3 +-- include/linux/dma-fence.h       | 38 +++++++++++++++++++++++++++++++++

  3 files changed, 40 insertions(+), 16 deletions(-)
include/linux/dma-fence-array.h +0 -12
index 303dd712220f..fec374f69e12 100644
@@ -45,19 +45,6 @@ struct dma_fence_array {
       struct irq_work work;
   };
   -extern const struct dma_fence_ops dma_fence_array_ops;
-
-/**
- * dma_fence_is_array - check if a fence is from the array subsclass
- * @fence: fence to test
- *
- * Return true if it is a dma_fence_array and false otherwise.
- */
-static inline bool dma_fence_is_array(struct dma_fence *fence)
-{
-    return fence->ops == &dma_fence_array_ops;
-}
-
   /**
    * to_dma_fence_array - cast a fence to a dma_fence_array
    * @fence: fence to cast to a dma_fence_array
@@ -68,7 +55,7 @@ static inline bool dma_fence_is_array(struct 
 dma_fence *fence)

  static inline struct dma_fence_array * to_dma_fence_array(struct dma_fence *fence)
  {
-    if (fence->ops != &dma_fence_array_ops)
+    if (!fence || !dma_fence_is_array(fence))
          return NULL;
        return container_of(fence, struct dma_fence_array, base);

include/linux/dma-fence-chain.h +0 -0
index 54fe3443fd2c..ee906b659694 100644
@@ -49,7 +49,6 @@ struct dma_fence_chain {
       spinlock_t lock;
   };
   -extern const struct dma_fence_ops dma_fence_chain_ops;
     /**
    * to_dma_fence_chain - cast a fence to a dma_fence_chain
@@ -61,7 +60,7 @@ extern const struct dma_fence_ops dma_fence_chain_ops;

  static inline struct dma_fence_chain *
  to_dma_fence_chain(struct dma_fence *fence)
  {

-    if (!fence || fence->ops != &dma_fence_chain_ops)
+    if (!fence || !dma_fence_is_chain(fence))

          return NULL;
        return container_of(fence, struct dma_fence_chain, base);

include/linux/dma-fence.h +26 -0
index 1ea691753bd3..775cdc0b4f24 100644
@@ -587,4 +587,42 @@ struct dma_fence *dma_fence_get_stub(void);
   struct dma_fence *dma_fence_allocate_private_stub(void);
   u64 dma_fence_context_alloc(unsigned num);
   +extern const struct dma_fence_ops dma_fence_array_ops;
+extern const struct dma_fence_ops dma_fence_chain_ops;
+
+/**
+ * dma_fence_is_array - check if a fence is from the array subclass
+ * @fence: the fence to test
+ *
+ * Return true if it is a dma_fence_array and false otherwise.
+ */
+static inline bool dma_fence_is_array(struct dma_fence *fence)
+{
+    return fence->ops == &dma_fence_array_ops;
+}
+
+/**
+ * dma_fence_is_chain - check if a fence is from the chain subclass
+ * @fence: the fence to test
+ *
+ * Return true if it is a dma_fence_chain and false otherwise.
+ */
+static inline bool dma_fence_is_chain(struct dma_fence *fence)
+{
+    return fence->ops == &dma_fence_chain_ops;
+}
+
+/**
+ * dma_fence_is_container - check if a fence is a container for 

other fences
+ * @fence: the fence to test
+ *
+ * Return true if this fence is a container for other fences, false otherwise.
+ * This is important since we can't build up large fence structure or otherwise

Quoted 8 lines

include/linux/dma-fence.h struct dma_fence *dma_fence_get_stub(void); · line 626

Quoted 6 lines

+ * we run into recursion during operation on those fences.
+ */

include/linux/dma-fence.h +4 -0
+static inline bool dma_fence_is_container(struct dma_fence *fence)
+{
+    return dma_fence_is_array(fence) || dma_fence_is_chain(fence);
+}

What's the strategy here moving forward if we add more dma_resv containers, or if a driver adds a container that similarly has risc of recursion? Should we perhaps add an ops bool for this, or require that all dma_resv containers that has this limitation be part of the dma-buf subsystem rather than driver-specific?

Good question.

I think that all containers which also implement the dma_fence interface should be part of the DMA-buf subsystem and not driver specific. Drivers just tend to reinvent something incorrectly.

Where/How should we document that?

Regards,
Christian.

Quoted 7 lines

Thanks,
/Thomas

Quoted 2 lines

+
  #endif /* __LINUX_DMA_FENCE_H */

#4 lore raw

PATCH 2/9 dma-buf: warn about dma_fence_array container rules

In reply to Christian König

It's not allowed to nest another dma_fence container into a dma_fence_array or otherwise we can run into recursion.

Warn about that when we create a dma_fence_array.

---
 drivers/dma-buf/dma-fence-array.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/dma-buf/dma-fence-array.c +13 -0
index 3e07f961e2f3..4bfbcb885bbc 100644
@@ -176,6 +176,19 @@ struct dma_fence_array *dma_fence_array_create(int num_fences,
 
 	array->base.error = PENDING_ERROR;
 
+	/* dma_fence_array objects should never contain any other fence
+	 * containers or otherwise we run into recursion and potential kernel
+	 * stack overflow on operations on the dma_fence_array.
+	 *
+	 * The correct way of handling this is to flatten out the array by the
+	 * caller instead.
+	 *
+	 * Enforce this here by checking that we don't create a dma_fence_array
+	 * with any container inside.
+	 */
+	while (seqno--)
+		WARN_ON(dma_fence_is_container(fences[seqno]));
+
 	return array;
 }
 EXPORT_SYMBOL(dma_fence_array_create);
Signature · 1 line
2.25.1
#5 lore raw

Re: [Linaro-mm-sig] [PATCH 2/9] dma-buf: warn about dma_fence_array container rules

Quoted 16 lines · Christian König

It's not allowed to nest another dma_fence container into a dma_fence_array or otherwise we can run into recursion.

Warn about that when we create a dma_fence_array.

---
  drivers/dma-buf/dma-fence-array.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
drivers/dma-buf/dma-fence-array.c +0 -0
index 3e07f961e2f3..4bfbcb885bbc 100644

drivers/dma-buf/dma-fence-array.c struct dma_fence_array *dma_fence_array_create(int num_fences, · line 179

Quoted 4 lines
drivers/dma-buf/dma-fence-array.c +1 -0
@@ -176,6 +176,19 @@ struct dma_fence_array *dma_fence_array_create(int num_fences,
 
  	array->base.error = PENDING_ERROR;
 
+	/* dma_fence_array objects should never contain any other fence

Nit: First comment line of multi-line comments shouldn't contain text.

drivers/dma-buf/dma-fence-array.c struct dma_fence_array *dma_fence_array_create(int num_fences, · line 191

Quoted 12 lines
drivers/dma-buf/dma-fence-array.c +12 -0
+	 * containers or otherwise we run into recursion and potential kernel
+	 * stack overflow on operations on the dma_fence_array.
+	 *
+	 * The correct way of handling this is to flatten out the array by the
+	 * caller instead.
+	 *
+	 * Enforce this here by checking that we don't create a dma_fence_array
+	 * with any container inside.
+	 */
+	while (seqno--)
+		WARN_ON(dma_fence_is_container(fences[seqno]));
+

s/seqno/num_fences/g ?

Thanks,

Thomas

Quoted 3 lines
drivers/dma-buf/dma-fence-array.c +0 -0
  	return array;
  }
  EXPORT_SYMBOL(dma_fence_array_create);
#6 lore raw

Re: [Linaro-mm-sig] [PATCH 2/9] dma-buf: warn about dma_fence_array container rules

Am 21.01.22 um 08:31 schrieb Thomas Hellström:

Quoted 38 lines · Christian König
Quoted 18 lines

It's not allowed to nest another dma_fence container into a dma_fence_array or otherwise we can run into recursion.

Warn about that when we create a dma_fence_array.

---
  drivers/dma-buf/dma-fence-array.c | 13 +++++++++++++

  1 file changed, 13 insertions(+)
drivers/dma-buf/dma-fence-array.c +0 -0
index 3e07f961e2f3..4bfbcb885bbc 100644
@@ -176,6 +176,19 @@ struct dma_fence_array 
 *dma_fence_array_create(int num_fences,
         array->base.error = PENDING_ERROR;
Quoted 1 line

  +    /* dma_fence_array objects should never contain any other fence

Nit: First comment line of multi-line comments shouldn't contain text.

Quoted 7 lines

+     * containers or otherwise we run into recursion and potential kernel
+     * stack overflow on operations on the dma_fence_array. +     *
+     * The correct way of handling this is to flatten out the array by the
+     * caller instead.

Quoted 8 lines

+     *
+     * Enforce this here by checking that we don't create a dma_fence_array
+     * with any container inside.
+     */
+    while (seqno--)
+        WARN_ON(dma_fence_is_container(fences[seqno])); +

Quoted 1 line

s/seqno/num_fences/g ?

Ah, of course! Typing to fast.

Thanks,
Christian.

Quoted 11 lines

Thanks,

Thomas

Quoted 3 lines

      return array;
  }
  EXPORT_SYMBOL(dma_fence_array_create);

#7 lore raw

PATCH 3/9 dma-buf: Warn about dma_fence_chain container rules

In reply to Christian König

Chaining of dma_fence_chain objects is only allowed through the prev fence and not through the contained fence.

Warn about that when we create a dma_fence_chain.

---
 drivers/dma-buf/dma-fence-chain.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/dma-buf/dma-fence-chain.c +8 -0
index 1b4cb3e5cec9..fa33f6b7f77b 100644
@@ -254,5 +254,13 @@ void dma_fence_chain_init(struct dma_fence_chain *chain,
 
 	dma_fence_init(&chain->base, &dma_fence_chain_ops,
 		       &chain->lock, context, seqno);
+
+	/* Chaining dma_fence_chain container together is only allowed through
+	 * the prev fence and not through the contained fence.
+	 *
+	 * The correct way of handling this is to flatten out the fence
+	 * structure into a dma_fence_array by the caller instead.
+	 */
+	WARN_ON(dma_fence_is_chain(fence));
 }
 EXPORT_SYMBOL(dma_fence_chain_init);
Signature · 1 line
2.25.1
#8 lore raw

Re: [Linaro-mm-sig] [PATCH 3/9] dma-buf: Warn about dma_fence_chain container rules

Quoted 16 lines · Christian König

Chaining of dma_fence_chain objects is only allowed through the prev fence and not through the contained fence.

Warn about that when we create a dma_fence_chain.

---
  drivers/dma-buf/dma-fence-chain.c | 8 ++++++++
  1 file changed, 8 insertions(+)
drivers/dma-buf/dma-fence-chain.c +0 -0
index 1b4cb3e5cec9..fa33f6b7f77b 100644

drivers/dma-buf/dma-fence-chain.c void dma_fence_chain_init(struct dma_fence_chain *chain, · line 258

Quoted 5 lines
drivers/dma-buf/dma-fence-chain.c +2 -0
@@ -254,5 +254,13 @@ void dma_fence_chain_init(struct dma_fence_chain *chain,
 
  	dma_fence_init(&chain->base, &dma_fence_chain_ops,
  		       &chain->lock, context, seqno);
+
+	/* Chaining dma_fence_chain container together is only allowed through

Nit: Multi-line comment.

Otherwise, Reviewed-by: Thomas Hellström <[email protected]>

Quoted 8 lines
drivers/dma-buf/dma-fence-chain.c +6 -0
+	 * the prev fence and not through the contained fence.
+	 *
+	 * The correct way of handling this is to flatten out the fence
+	 * structure into a dma_fence_array by the caller instead.
+	 */
+	WARN_ON(dma_fence_is_chain(fence));
  }
  EXPORT_SYMBOL(dma_fence_chain_init);
#9 lore raw

PATCH 4/9 dma-buf: warn about containers in dma_resv object

In reply to Christian König

Drivers should not add containers as shared fences to the dma_resv object, instead each fence should be added individually.

---
 drivers/dma-buf/dma-resv.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/dma-buf/dma-resv.c +5 -0
index 6dd9a40b55d4..e8a0c1d51da2 100644
@@ -256,6 +256,11 @@ void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence)
 
 	dma_resv_assert_held(obj);
 
+	/* Drivers should not add containers here, instead add each fence
+	 * individually.
+	 */
+	WARN_ON(dma_fence_is_container(fence));
+
 	fobj = dma_resv_shared_list(obj);
 	count = fobj->shared_count;
 
Signature · 1 line
2.25.1
#10 lore raw

PATCH 5/9 dma-buf: Add dma_fence_array_for_each (v2)

In reply to Christian König

From: Christian König <[email protected]>

Add a helper to iterate over all fences in a dma_fence_array object.

v2 (Jason Ekstrand)
- Return NULL from dma_fence_array_first if head == NULL. This matches the iterator behavior of dma_fence_chain_for_each in that it iterates zero times if head == NULL.
- Return NULL from dma_fence_array_next if index > array->num_fences.

---
 drivers/dma-buf/dma-fence-array.c | 27 +++++++++++++++++++++++++++
 include/linux/dma-fence-array.h   | 17 +++++++++++++++++
 2 files changed, 44 insertions(+)
drivers/dma-buf/dma-fence-array.c +27 -0
index 4bfbcb885bbc..eb517b3914f7 100644
@@ -218,3 +218,30 @@ bool dma_fence_match_context(struct dma_fence *fence, u64 context)
 	return true;
 }
 EXPORT_SYMBOL(dma_fence_match_context);
+
+struct dma_fence *dma_fence_array_first(struct dma_fence *head)
+{
+	struct dma_fence_array *array;
+
+	if (!head)
+		return NULL;
+
+	array = to_dma_fence_array(head);
+	if (!array)
+		return head;
+
+	return array->fences[0];
+}
+EXPORT_SYMBOL(dma_fence_array_first);
+
+struct dma_fence *dma_fence_array_next(struct dma_fence *head,
+				       unsigned int index)
+{
+	struct dma_fence_array *array = to_dma_fence_array(head);
+
+	if (!array || index >= array->num_fences)
+		return NULL;
+
+	return array->fences[index];
+}
+EXPORT_SYMBOL(dma_fence_array_next);
include/linux/dma-fence-array.h +17 -0
index fec374f69e12..e34dcb0bb462 100644
@@ -61,6 +61,19 @@ to_dma_fence_array(struct dma_fence *fence)
 	return container_of(fence, struct dma_fence_array, base);
 }
 
+/**
+ * dma_fence_array_for_each - iterate over all fences in array
+ * @fence: current fence
+ * @index: index into the array
+ * @head: potential dma_fence_array object
+ *
+ * Test if @array is a dma_fence_array object and if yes iterate over all fences
+ * in the array. If not just iterate over the fence in @array itself.
+ */
+#define dma_fence_array_for_each(fence, index, head)			\
+	for (index = 0, fence = dma_fence_array_first(head); fence;	\
+	     ++(index), fence = dma_fence_array_next(head, index))
+
 struct dma_fence_array *dma_fence_array_create(int num_fences,
 					       struct dma_fence **fences,
 					       u64 context, unsigned seqno,
@@ -68,4 +81,8 @@ struct dma_fence_array *dma_fence_array_create(int num_fences,
 
 bool dma_fence_match_context(struct dma_fence *fence, u64 context);
 
+struct dma_fence *dma_fence_array_first(struct dma_fence *head);
+struct dma_fence *dma_fence_array_next(struct dma_fence *head,
+				       unsigned int index);
+
 #endif /* __LINUX_DMA_FENCE_ARRAY_H */
Signature · 1 line
2.25.1
#11 lore raw

PATCH 6/9 dma-buf: add dma_fence_chain_contained helper

In reply to Christian König

It's a reoccurring pattern that we need to extract the fence from a dma_fence_chain object. Add a helper for this.

---
 drivers/dma-buf/dma-fence-chain.c |  6 ++----
 include/linux/dma-fence-chain.h   | 15 +++++++++++++++
 2 files changed, 17 insertions(+), 4 deletions(-)
drivers/dma-buf/dma-fence-chain.c +2 -4
index fa33f6b7f77b..2f497ffb70d8 100644
@@ -148,8 +148,7 @@ static bool dma_fence_chain_enable_signaling(struct dma_fence *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;
+		struct dma_fence *f = dma_fence_chain_contained(fence);
 
 		dma_fence_get(f);
 		if (!dma_fence_add_callback(f, &head->cb, dma_fence_chain_cb)) {
@@ -165,8 +164,7 @@ 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_contained(fence);
 
 		if (!dma_fence_is_signaled(f)) {
 			dma_fence_put(fence);
include/linux/dma-fence-chain.h +15 -0
index ee906b659694..10d51bcdf7b7 100644
@@ -66,6 +66,21 @@ to_dma_fence_chain(struct dma_fence *fence)
 	return container_of(fence, struct dma_fence_chain, base);
 }
 
+/**
+ * dma_fence_chain_contained - return the contained fence
+ * @fence: the fence to test
+ *
+ * If the fence is a dma_fence_chain the function returns the fence contained
+ * inside the chain object, otherwise it returns the fence itself.
+ */
+static inline struct dma_fence *
+dma_fence_chain_contained(struct dma_fence *fence)
+{
+	struct dma_fence_chain *chain = to_dma_fence_chain(fence);
+
+	return chain ? chain->fence : fence;
+}
+
 /**
  * dma_fence_chain_alloc
  *
Signature · 1 line
2.25.1
#12 lore raw

PATCH 7/9 drm/amdgpu: use dma_fence_chain_contained

In reply to Christian König

Instead of manually extracting the fence.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c +2 -3
index f7d8487799b2..40e06745fae9 100644
@@ -261,10 +261,9 @@ int amdgpu_sync_resv(struct amdgpu_device *adev, struct amdgpu_sync *sync,
 
 	dma_resv_for_each_fence(&cursor, resv, true, f) {
 		dma_fence_chain_for_each(f, f) {
-			struct dma_fence_chain *chain = to_dma_fence_chain(f);
+			struct dma_fence *tmp = dma_fence_chain_contained(f);
 
-			if (amdgpu_sync_test_fence(adev, mode, owner, chain ?
-						   chain->fence : f)) {
+			if (amdgpu_sync_test_fence(adev, mode, owner, tmp)) {
 				r = amdgpu_sync_fence(sync, f);
 				dma_fence_put(f);
 				if (r)
Signature · 1 line
2.25.1
#13 lore raw

PATCH 8/9 drm/i915: use dma_fence extractor functions

In reply to Christian König

Instead of manually messing with the data structures use the iterators and extraction helpers provided by the framework.

This whole handling should essentially go away when boost handling is implemented in the core dma-buf framework.

---
 drivers/gpu/drm/i915/gem/i915_gem_busy.c | 40 ++++++--------------
 drivers/gpu/drm/i915/gem/i915_gem_wait.c | 31 ++++------------
 drivers/gpu/drm/i915/i915_request.c      | 47 +++++++-----------------
 3 files changed, 33 insertions(+), 85 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_busy.c +12 -28
index 470fdfd61a0f..4ea7a5b26405 100644
@@ -41,6 +41,8 @@ static __always_inline unsigned int
 __busy_set_if_active(struct dma_fence *fence, u32 (*flag)(u16 id))
 {
 	const struct i915_request *rq;
+	struct dma_fence *current_fence;
+	unsigned int i;
 
 	/*
 	 * We have to check the current hw status of the fence as the uABI
@@ -58,40 +60,22 @@ __busy_set_if_active(struct dma_fence *fence, u32 (*flag)(u16 id))
 	 *
 	 * 2. A single i915 request.
 	 */
-	if (dma_fence_is_array(fence)) {
-		struct dma_fence_array *array = to_dma_fence_array(fence);
-		struct dma_fence **child = array->fences;
-		unsigned int nchild = array->num_fences;
-
-		do {
-			struct dma_fence *current_fence = *child++;
-
-			/* Not an i915 fence, can't be busy per above */
-			if (!dma_fence_is_i915(current_fence) ||
-			    !test_bit(I915_FENCE_FLAG_COMPOSITE,
-				      &current_fence->flags)) {
-				return 0;
-			}
-
-			rq = to_request(current_fence);
-			if (!i915_request_completed(rq))
-				return flag(rq->engine->uabi_class);
-		} while (--nchild);
-
-		/* All requests in array complete, not busy */
-		return 0;
-	} else {
-		if (!dma_fence_is_i915(fence))
-			return 0;
+	dma_fence_array_for_each(current_fence, i, fence) {
 
-		rq = to_request(fence);
-		if (i915_request_completed(rq))
+		/* Not an i915 fence, can't be busy per above */
+		if (!dma_fence_is_i915(current_fence) ||
+		    !test_bit(I915_FENCE_FLAG_COMPOSITE, &current_fence->flags))
 			return 0;
 
+		rq = to_request(current_fence);
 		/* Beware type-expansion follies! */
 		BUILD_BUG_ON(!typecheck(u16, rq->engine->uabi_class));
-		return flag(rq->engine->uabi_class);
+		if (!i915_request_completed(rq))
+			return flag(rq->engine->uabi_class);
 	}
+
+	/* All requests in array complete, not busy */
+	return 0;
 }
 
 static __always_inline unsigned int
drivers/gpu/drm/i915/gem/i915_gem_wait.c +8 -23
index dab3d30c09a0..13f37b6aedf7 100644
@@ -72,11 +72,6 @@ static void fence_set_priority(struct dma_fence *fence,
 	rcu_read_unlock();
 }
 
-static inline bool __dma_fence_is_chain(const struct dma_fence *fence)
-{
-	return fence->ops == &dma_fence_chain_ops;
-}
-
 void i915_gem_fence_wait_priority(struct dma_fence *fence,
 				  const struct i915_sched_attr *attr)
 {
@@ -85,25 +80,15 @@ void i915_gem_fence_wait_priority(struct dma_fence *fence,
 
 	local_bh_disable();
 
-	/* Recurse once into a fence-array */
-	if (dma_fence_is_array(fence)) {
-		struct dma_fence_array *array = to_dma_fence_array(fence);
-		int i;
+	/* The chain is ordered; if we boost the last, we boost all */
+	dma_fence_chain_for_each(fence, fence) {
+		struct dma_fence *array, *element;
+		unsigned int i;
 
-		for (i = 0; i < array->num_fences; i++)
-			fence_set_priority(array->fences[i], attr);
-	} else if (__dma_fence_is_chain(fence)) {
-		struct dma_fence *iter;
-
-		/* The chain is ordered; if we boost the last, we boost all */
-		dma_fence_chain_for_each(iter, fence) {
-			fence_set_priority(to_dma_fence_chain(iter)->fence,
-					   attr);
-			break;
-		}
-		dma_fence_put(iter);
-	} else {
-		fence_set_priority(fence, attr);
+		/* Recurse once into a fence-array */
+		array = dma_fence_chain_contained(fence);
+		dma_fence_array_for_each(element, i, array)
+			fence_set_priority(element, attr);
 	}
 
 	local_bh_enable(); /* kick the tasklets if queues were reprioritised */
drivers/gpu/drm/i915/i915_request.c +13 -34

95 lines not on this page · raw message

Signature · 1 line
2.25.1
#14 lore raw

PATCH 9/9 drm/vmwgfx: remove vmw_wait_dma_fence

In reply to Christian König

Decomposing fence containers don't seem to make any sense here.

So just remove the function entirely and call dma_fence_wait() directly.

Cc VMware Graphics <[email protected]>
Cc Zack Rusin <[email protected]>
---
 drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c |  2 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_fence.c   | 46 -------------------------
 drivers/gpu/drm/vmwgfx/vmwgfx_fence.h   |  3 --
 3 files changed, 1 insertion(+), 50 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c +1 -1
index 44ca23b0ea4e..0ff28f0e3eb4 100644
@@ -4500,7 +4500,7 @@ int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
 			goto mksstats_out;
 		}
 
-		ret = vmw_wait_dma_fence(dev_priv->fman, in_fence);
+		ret = dma_fence_wait(in_fence, true);
 		if (ret)
 			goto out;
 	}
drivers/gpu/drm/vmwgfx/vmwgfx_fence.c +0 -46

52 lines not on this page · raw message

drivers/gpu/drm/vmwgfx/vmwgfx_fence.h +0 -3
index 079ab4f3ba51..a7eee579c76a 100644
@@ -104,9 +104,6 @@ extern int vmw_user_fence_create(struct drm_file *file_priv,
 				 struct vmw_fence_obj **p_fence,
 				 uint32_t *p_handle);
 
-extern int vmw_wait_dma_fence(struct vmw_fence_manager *fman,
-			      struct dma_fence *fence);
-
 extern void vmw_fence_fifo_up(struct vmw_fence_manager *fman);
 
 extern void vmw_fence_fifo_down(struct vmw_fence_manager *fman);
Signature · 1 line
2.25.1