All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4] dma-buf: consolidate dma_fence subclass checking
@ 2022-01-19 13:43 ` Christian König
  0 siblings, 0 replies; 20+ messages in thread
From: Christian König @ 2022-01-19 13:43 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel.vetter
  Cc: linux-media, dri-devel, linaro-mm-sig

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.

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 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(-)

diff --git a/include/linux/dma-fence-array.h b/include/linux/dma-fence-array.h
index 303dd712220f..fec374f69e12 100644
--- a/include/linux/dma-fence-array.h
+++ b/include/linux/dma-fence-array.h
@@ -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);
diff --git a/include/linux/dma-fence-chain.h b/include/linux/dma-fence-chain.h
index 54fe3443fd2c..ee906b659694 100644
--- a/include/linux/dma-fence-chain.h
+++ b/include/linux/dma-fence-chain.h
@@ -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);
diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
index 1ea691753bd3..775cdc0b4f24 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -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 */
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 1/4] dma-buf: consolidate dma_fence subclass checking
@ 2022-01-19 13:43 ` Christian König
  0 siblings, 0 replies; 20+ messages in thread
From: Christian König @ 2022-01-19 13:43 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel.vetter
  Cc: linaro-mm-sig, dri-devel, linux-media

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.

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 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(-)

diff --git a/include/linux/dma-fence-array.h b/include/linux/dma-fence-array.h
index 303dd712220f..fec374f69e12 100644
--- a/include/linux/dma-fence-array.h
+++ b/include/linux/dma-fence-array.h
@@ -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);
diff --git a/include/linux/dma-fence-chain.h b/include/linux/dma-fence-chain.h
index 54fe3443fd2c..ee906b659694 100644
--- a/include/linux/dma-fence-chain.h
+++ b/include/linux/dma-fence-chain.h
@@ -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);
diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
index 1ea691753bd3..775cdc0b4f24 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -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 */
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 2/4] dma-buf: warn about dma_fence_array container rules
  2022-01-19 13:43 ` Christian König
@ 2022-01-19 13:43   ` Christian König
  -1 siblings, 0 replies; 20+ messages in thread
From: Christian König @ 2022-01-19 13:43 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel.vetter
  Cc: linux-media, dri-devel, linaro-mm-sig

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.

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/dma-fence-array.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/drivers/dma-buf/dma-fence-array.c b/drivers/dma-buf/dma-fence-array.c
index 3e07f961e2f3..4bfbcb885bbc 100644
--- a/drivers/dma-buf/dma-fence-array.c
+++ b/drivers/dma-buf/dma-fence-array.c
@@ -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);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 2/4] dma-buf: warn about dma_fence_array container rules
@ 2022-01-19 13:43   ` Christian König
  0 siblings, 0 replies; 20+ messages in thread
From: Christian König @ 2022-01-19 13:43 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel.vetter
  Cc: linaro-mm-sig, dri-devel, linux-media

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.

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/dma-fence-array.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/drivers/dma-buf/dma-fence-array.c b/drivers/dma-buf/dma-fence-array.c
index 3e07f961e2f3..4bfbcb885bbc 100644
--- a/drivers/dma-buf/dma-fence-array.c
+++ b/drivers/dma-buf/dma-fence-array.c
@@ -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);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 3/4] dma-buf: Warn about dma_fence_chain container rules
  2022-01-19 13:43 ` Christian König
@ 2022-01-19 13:43   ` Christian König
  -1 siblings, 0 replies; 20+ messages in thread
From: Christian König @ 2022-01-19 13:43 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel.vetter
  Cc: linux-media, dri-devel, linaro-mm-sig

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.

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/dma-fence-chain.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/dma-buf/dma-fence-chain.c b/drivers/dma-buf/dma-fence-chain.c
index 1b4cb3e5cec9..fa33f6b7f77b 100644
--- a/drivers/dma-buf/dma-fence-chain.c
+++ b/drivers/dma-buf/dma-fence-chain.c
@@ -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);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 3/4] dma-buf: Warn about dma_fence_chain container rules
@ 2022-01-19 13:43   ` Christian König
  0 siblings, 0 replies; 20+ messages in thread
From: Christian König @ 2022-01-19 13:43 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel.vetter
  Cc: linaro-mm-sig, dri-devel, linux-media

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.

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/dma-fence-chain.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/dma-buf/dma-fence-chain.c b/drivers/dma-buf/dma-fence-chain.c
index 1b4cb3e5cec9..fa33f6b7f77b 100644
--- a/drivers/dma-buf/dma-fence-chain.c
+++ b/drivers/dma-buf/dma-fence-chain.c
@@ -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);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 4/4] dma-buf: warn about containers in dma_resv object
  2022-01-19 13:43 ` Christian König
@ 2022-01-19 13:43   ` Christian König
  -1 siblings, 0 replies; 20+ messages in thread
From: Christian König @ 2022-01-19 13:43 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel.vetter
  Cc: linux-media, dri-devel, linaro-mm-sig

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

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/dma-resv.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c
index 6dd9a40b55d4..e8a0c1d51da2 100644
--- a/drivers/dma-buf/dma-resv.c
+++ b/drivers/dma-buf/dma-resv.c
@@ -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;
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 4/4] dma-buf: warn about containers in dma_resv object
@ 2022-01-19 13:43   ` Christian König
  0 siblings, 0 replies; 20+ messages in thread
From: Christian König @ 2022-01-19 13:43 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel.vetter
  Cc: linaro-mm-sig, dri-devel, linux-media

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

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/dma-resv.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c
index 6dd9a40b55d4..e8a0c1d51da2 100644
--- a/drivers/dma-buf/dma-resv.c
+++ b/drivers/dma-buf/dma-resv.c
@@ -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;
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/4] dma-buf: consolidate dma_fence subclass checking
  2022-01-19 13:43 ` Christian König
@ 2022-01-19 17:16   ` Daniel Vetter
  -1 siblings, 0 replies; 20+ messages in thread
From: Daniel Vetter @ 2022-01-19 17:16 UTC (permalink / raw)
  To: Christian König
  Cc: sumit.semwal, gustavo, daniel.vetter, linux-media, dri-devel,
	linaro-mm-sig

On Wed, Jan 19, 2022 at 02:43:36PM +0100, Christian König wrote:
> 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.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
>  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(-)
> 
> diff --git a/include/linux/dma-fence-array.h b/include/linux/dma-fence-array.h
> index 303dd712220f..fec374f69e12 100644
> --- a/include/linux/dma-fence-array.h
> +++ b/include/linux/dma-fence-array.h
> @@ -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);
> diff --git a/include/linux/dma-fence-chain.h b/include/linux/dma-fence-chain.h
> index 54fe3443fd2c..ee906b659694 100644
> --- a/include/linux/dma-fence-chain.h
> +++ b/include/linux/dma-fence-chain.h
> @@ -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);
> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index 1ea691753bd3..775cdc0b4f24 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -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)

Code looks all good, but I'm not super enthusiastic about exporting the
ops to drivers and letting them do random nonsense. At least i915 does
pretty enormous amounts of stuff with that instead of having pushed
priority boosting into dma-fence as a proper concept. And maybe a few
other things.

Now i915-gem team having gone off the rails of good upstream conduct is
another thing maybe, but I'd like to not encourage that.

So could we perhaps do this all in header which is entirely private to
drivers/dma-buf, like dma-fence-internal or so? And maybe whack a big
fixme onto the current abuse in drivers (of which __dma_fence_is_chain()
gets a special price for "not how upstream should be done" *sigh*).

Cheers, Daniel

> +{
> +	return dma_fence_is_array(fence) || dma_fence_is_chain(fence);
> +}
> +
>  #endif /* __LINUX_DMA_FENCE_H */
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/4] dma-buf: consolidate dma_fence subclass checking
@ 2022-01-19 17:16   ` Daniel Vetter
  0 siblings, 0 replies; 20+ messages in thread
From: Daniel Vetter @ 2022-01-19 17:16 UTC (permalink / raw)
  To: Christian König
  Cc: gustavo, dri-devel, linaro-mm-sig, daniel.vetter, linux-media

On Wed, Jan 19, 2022 at 02:43:36PM +0100, Christian König wrote:
> 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.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
>  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(-)
> 
> diff --git a/include/linux/dma-fence-array.h b/include/linux/dma-fence-array.h
> index 303dd712220f..fec374f69e12 100644
> --- a/include/linux/dma-fence-array.h
> +++ b/include/linux/dma-fence-array.h
> @@ -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);
> diff --git a/include/linux/dma-fence-chain.h b/include/linux/dma-fence-chain.h
> index 54fe3443fd2c..ee906b659694 100644
> --- a/include/linux/dma-fence-chain.h
> +++ b/include/linux/dma-fence-chain.h
> @@ -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);
> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index 1ea691753bd3..775cdc0b4f24 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -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)

Code looks all good, but I'm not super enthusiastic about exporting the
ops to drivers and letting them do random nonsense. At least i915 does
pretty enormous amounts of stuff with that instead of having pushed
priority boosting into dma-fence as a proper concept. And maybe a few
other things.

Now i915-gem team having gone off the rails of good upstream conduct is
another thing maybe, but I'd like to not encourage that.

So could we perhaps do this all in header which is entirely private to
drivers/dma-buf, like dma-fence-internal or so? And maybe whack a big
fixme onto the current abuse in drivers (of which __dma_fence_is_chain()
gets a special price for "not how upstream should be done" *sigh*).

Cheers, Daniel

> +{
> +	return dma_fence_is_array(fence) || dma_fence_is_chain(fence);
> +}
> +
>  #endif /* __LINUX_DMA_FENCE_H */
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 2/4] dma-buf: warn about dma_fence_array container rules
  2022-01-19 13:43   ` Christian König
@ 2022-01-19 17:18     ` Daniel Vetter
  -1 siblings, 0 replies; 20+ messages in thread
From: Daniel Vetter @ 2022-01-19 17:18 UTC (permalink / raw)
  To: Christian König
  Cc: sumit.semwal, gustavo, daniel.vetter, linux-media, dri-devel,
	linaro-mm-sig

On Wed, Jan 19, 2022 at 02:43:37PM +0100, Christian König wrote:
> 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.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
>  drivers/dma-buf/dma-fence-array.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/drivers/dma-buf/dma-fence-array.c b/drivers/dma-buf/dma-fence-array.c
> index 3e07f961e2f3..4bfbcb885bbc 100644
> --- a/drivers/dma-buf/dma-fence-array.c
> +++ b/drivers/dma-buf/dma-fence-array.c
> @@ -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]));

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

> +
>  	return array;
>  }
>  EXPORT_SYMBOL(dma_fence_array_create);
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 2/4] dma-buf: warn about dma_fence_array container rules
@ 2022-01-19 17:18     ` Daniel Vetter
  0 siblings, 0 replies; 20+ messages in thread
From: Daniel Vetter @ 2022-01-19 17:18 UTC (permalink / raw)
  To: Christian König
  Cc: gustavo, dri-devel, linaro-mm-sig, daniel.vetter, linux-media

On Wed, Jan 19, 2022 at 02:43:37PM +0100, Christian König wrote:
> 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.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
>  drivers/dma-buf/dma-fence-array.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/drivers/dma-buf/dma-fence-array.c b/drivers/dma-buf/dma-fence-array.c
> index 3e07f961e2f3..4bfbcb885bbc 100644
> --- a/drivers/dma-buf/dma-fence-array.c
> +++ b/drivers/dma-buf/dma-fence-array.c
> @@ -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]));

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

> +
>  	return array;
>  }
>  EXPORT_SYMBOL(dma_fence_array_create);
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 3/4] dma-buf: Warn about dma_fence_chain container rules
  2022-01-19 13:43   ` Christian König
@ 2022-01-19 17:18     ` Daniel Vetter
  -1 siblings, 0 replies; 20+ messages in thread
From: Daniel Vetter @ 2022-01-19 17:18 UTC (permalink / raw)
  To: Christian König
  Cc: sumit.semwal, gustavo, daniel.vetter, linux-media, dri-devel,
	linaro-mm-sig

On Wed, Jan 19, 2022 at 02:43:38PM +0100, Christian König wrote:
> 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.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
>  drivers/dma-buf/dma-fence-chain.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/dma-buf/dma-fence-chain.c b/drivers/dma-buf/dma-fence-chain.c
> index 1b4cb3e5cec9..fa33f6b7f77b 100644
> --- a/drivers/dma-buf/dma-fence-chain.c
> +++ b/drivers/dma-buf/dma-fence-chain.c
> @@ -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));

At first I was worried that you'd leave a chain fence in the array fence
as an option, but we exclude that with the previous patch.

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

>  }
>  EXPORT_SYMBOL(dma_fence_chain_init);
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 3/4] dma-buf: Warn about dma_fence_chain container rules
@ 2022-01-19 17:18     ` Daniel Vetter
  0 siblings, 0 replies; 20+ messages in thread
From: Daniel Vetter @ 2022-01-19 17:18 UTC (permalink / raw)
  To: Christian König
  Cc: gustavo, dri-devel, linaro-mm-sig, daniel.vetter, linux-media

On Wed, Jan 19, 2022 at 02:43:38PM +0100, Christian König wrote:
> 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.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
>  drivers/dma-buf/dma-fence-chain.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/dma-buf/dma-fence-chain.c b/drivers/dma-buf/dma-fence-chain.c
> index 1b4cb3e5cec9..fa33f6b7f77b 100644
> --- a/drivers/dma-buf/dma-fence-chain.c
> +++ b/drivers/dma-buf/dma-fence-chain.c
> @@ -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));

At first I was worried that you'd leave a chain fence in the array fence
as an option, but we exclude that with the previous patch.

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

>  }
>  EXPORT_SYMBOL(dma_fence_chain_init);
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 4/4] dma-buf: warn about containers in dma_resv object
  2022-01-19 13:43   ` Christian König
@ 2022-01-19 17:19     ` Daniel Vetter
  -1 siblings, 0 replies; 20+ messages in thread
From: Daniel Vetter @ 2022-01-19 17:19 UTC (permalink / raw)
  To: Christian König
  Cc: sumit.semwal, gustavo, daniel.vetter, linux-media, dri-devel,
	linaro-mm-sig

On Wed, Jan 19, 2022 at 02:43:39PM +0100, Christian König wrote:
> Drivers should not add containers as shared fences to the dma_resv
> object, instead each fence should be added individually.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
>  drivers/dma-buf/dma-resv.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c
> index 6dd9a40b55d4..e8a0c1d51da2 100644
> --- a/drivers/dma-buf/dma-resv.c
> +++ b/drivers/dma-buf/dma-resv.c
> @@ -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));

I'm honestly not sure whether this could go boom, so good if we push this
asap and let it soak in linux-next for the entire release cycle.

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

On the change itself, we'll see what it brings.
-Daniel

> +
>  	fobj = dma_resv_shared_list(obj);
>  	count = fobj->shared_count;
>  
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 4/4] dma-buf: warn about containers in dma_resv object
@ 2022-01-19 17:19     ` Daniel Vetter
  0 siblings, 0 replies; 20+ messages in thread
From: Daniel Vetter @ 2022-01-19 17:19 UTC (permalink / raw)
  To: Christian König
  Cc: gustavo, dri-devel, linaro-mm-sig, daniel.vetter, linux-media

On Wed, Jan 19, 2022 at 02:43:39PM +0100, Christian König wrote:
> Drivers should not add containers as shared fences to the dma_resv
> object, instead each fence should be added individually.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
>  drivers/dma-buf/dma-resv.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c
> index 6dd9a40b55d4..e8a0c1d51da2 100644
> --- a/drivers/dma-buf/dma-resv.c
> +++ b/drivers/dma-buf/dma-resv.c
> @@ -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));

I'm honestly not sure whether this could go boom, so good if we push this
asap and let it soak in linux-next for the entire release cycle.

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

On the change itself, we'll see what it brings.
-Daniel

> +
>  	fobj = dma_resv_shared_list(obj);
>  	count = fobj->shared_count;
>  
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/4] dma-buf: consolidate dma_fence subclass checking
  2022-01-19 17:16   ` Daniel Vetter
@ 2022-01-20  8:50     ` Christian König
  -1 siblings, 0 replies; 20+ messages in thread
From: Christian König @ 2022-01-20  8:50 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: sumit.semwal, gustavo, daniel.vetter, linux-media, dri-devel,
	linaro-mm-sig

Am 19.01.22 um 18:16 schrieb Daniel Vetter:
> On Wed, Jan 19, 2022 at 02:43:36PM +0100, Christian König wrote:
>> 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.
>>
>> Signed-off-by: Christian König <christian.koenig@amd.com>
>> ---
>>   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(-)
>>
>> diff --git a/include/linux/dma-fence-array.h b/include/linux/dma-fence-array.h
>> index 303dd712220f..fec374f69e12 100644
>> --- a/include/linux/dma-fence-array.h
>> +++ b/include/linux/dma-fence-array.h
>> @@ -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);
>> diff --git a/include/linux/dma-fence-chain.h b/include/linux/dma-fence-chain.h
>> index 54fe3443fd2c..ee906b659694 100644
>> --- a/include/linux/dma-fence-chain.h
>> +++ b/include/linux/dma-fence-chain.h
>> @@ -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);
>> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
>> index 1ea691753bd3..775cdc0b4f24 100644
>> --- a/include/linux/dma-fence.h
>> +++ b/include/linux/dma-fence.h
>> @@ -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)
> Code looks all good, but I'm not super enthusiastic about exporting the
> ops to drivers and letting them do random nonsense. At least i915 does
> pretty enormous amounts of stuff with that instead of having pushed
> priority boosting into dma-fence as a proper concept. And maybe a few
> other things.
>
> Now i915-gem team having gone off the rails of good upstream conduct is
> another thing maybe, but I'd like to not encourage that.
>
> So could we perhaps do this all in header which is entirely private to
> drivers/dma-buf, like dma-fence-internal or so? And maybe whack a big
> fixme onto the current abuse in drivers (of which __dma_fence_is_chain()
> gets a special price for "not how upstream should be done" *sigh*).

WTF is __dma_fence_is_chain? Seeing that for the first time now.

And yes even if you do priority boosting manually that code in i915 is 
just way to complicated.

I'm sure you don't have any objections that I clean up that mess now you 
pointed it out :)

Thanks,
Christian.

>
> Cheers, Daniel
>
>> +{
>> +	return dma_fence_is_array(fence) || dma_fence_is_chain(fence);
>> +}
>> +
>>   #endif /* __LINUX_DMA_FENCE_H */
>> -- 
>> 2.25.1
>>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/4] dma-buf: consolidate dma_fence subclass checking
@ 2022-01-20  8:50     ` Christian König
  0 siblings, 0 replies; 20+ messages in thread
From: Christian König @ 2022-01-20  8:50 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: gustavo, dri-devel, linaro-mm-sig, daniel.vetter, linux-media

Am 19.01.22 um 18:16 schrieb Daniel Vetter:
> On Wed, Jan 19, 2022 at 02:43:36PM +0100, Christian König wrote:
>> 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.
>>
>> Signed-off-by: Christian König <christian.koenig@amd.com>
>> ---
>>   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(-)
>>
>> diff --git a/include/linux/dma-fence-array.h b/include/linux/dma-fence-array.h
>> index 303dd712220f..fec374f69e12 100644
>> --- a/include/linux/dma-fence-array.h
>> +++ b/include/linux/dma-fence-array.h
>> @@ -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);
>> diff --git a/include/linux/dma-fence-chain.h b/include/linux/dma-fence-chain.h
>> index 54fe3443fd2c..ee906b659694 100644
>> --- a/include/linux/dma-fence-chain.h
>> +++ b/include/linux/dma-fence-chain.h
>> @@ -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);
>> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
>> index 1ea691753bd3..775cdc0b4f24 100644
>> --- a/include/linux/dma-fence.h
>> +++ b/include/linux/dma-fence.h
>> @@ -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)
> Code looks all good, but I'm not super enthusiastic about exporting the
> ops to drivers and letting them do random nonsense. At least i915 does
> pretty enormous amounts of stuff with that instead of having pushed
> priority boosting into dma-fence as a proper concept. And maybe a few
> other things.
>
> Now i915-gem team having gone off the rails of good upstream conduct is
> another thing maybe, but I'd like to not encourage that.
>
> So could we perhaps do this all in header which is entirely private to
> drivers/dma-buf, like dma-fence-internal or so? And maybe whack a big
> fixme onto the current abuse in drivers (of which __dma_fence_is_chain()
> gets a special price for "not how upstream should be done" *sigh*).

WTF is __dma_fence_is_chain? Seeing that for the first time now.

And yes even if you do priority boosting manually that code in i915 is 
just way to complicated.

I'm sure you don't have any objections that I clean up that mess now you 
pointed it out :)

Thanks,
Christian.

>
> Cheers, Daniel
>
>> +{
>> +	return dma_fence_is_array(fence) || dma_fence_is_chain(fence);
>> +}
>> +
>>   #endif /* __LINUX_DMA_FENCE_H */
>> -- 
>> 2.25.1
>>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/4] dma-buf: consolidate dma_fence subclass checking
  2022-01-20  8:50     ` Christian König
@ 2022-01-20 11:28       ` Daniel Vetter
  -1 siblings, 0 replies; 20+ messages in thread
From: Daniel Vetter @ 2022-01-20 11:28 UTC (permalink / raw)
  To: Christian König
  Cc: sumit.semwal, gustavo, linux-media, dri-devel, linaro-mm-sig

On Thu, Jan 20, 2022 at 9:50 AM Christian König
<ckoenig.leichtzumerken@gmail.com> wrote:
>
> Am 19.01.22 um 18:16 schrieb Daniel Vetter:
> > On Wed, Jan 19, 2022 at 02:43:36PM +0100, Christian König wrote:
> >> 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.
> >>
> >> Signed-off-by: Christian König <christian.koenig@amd.com>
> >> ---
> >>   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(-)
> >>
> >> diff --git a/include/linux/dma-fence-array.h b/include/linux/dma-fence-array.h
> >> index 303dd712220f..fec374f69e12 100644
> >> --- a/include/linux/dma-fence-array.h
> >> +++ b/include/linux/dma-fence-array.h
> >> @@ -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);
> >> diff --git a/include/linux/dma-fence-chain.h b/include/linux/dma-fence-chain.h
> >> index 54fe3443fd2c..ee906b659694 100644
> >> --- a/include/linux/dma-fence-chain.h
> >> +++ b/include/linux/dma-fence-chain.h
> >> @@ -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);
> >> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> >> index 1ea691753bd3..775cdc0b4f24 100644
> >> --- a/include/linux/dma-fence.h
> >> +++ b/include/linux/dma-fence.h
> >> @@ -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)
> > Code looks all good, but I'm not super enthusiastic about exporting the
> > ops to drivers and letting them do random nonsense. At least i915 does
> > pretty enormous amounts of stuff with that instead of having pushed
> > priority boosting into dma-fence as a proper concept. And maybe a few
> > other things.
> >
> > Now i915-gem team having gone off the rails of good upstream conduct is
> > another thing maybe, but I'd like to not encourage that.
> >
> > So could we perhaps do this all in header which is entirely private to
> > drivers/dma-buf, like dma-fence-internal or so? And maybe whack a big
> > fixme onto the current abuse in drivers (of which __dma_fence_is_chain()
> > gets a special price for "not how upstream should be done" *sigh*).
>
> WTF is __dma_fence_is_chain? Seeing that for the first time now.

Yes :-/

> And yes even if you do priority boosting manually that code in i915 is
> just way to complicated.
>
> I'm sure you don't have any objections that I clean up that mess now you
> pointed it out :)

Go for it. I think ideally we could get rid of the dma_fence_chain_ops
export. Btw similar situation exists for dma_fence_is_array, so maybe
check those out too.

I think if we do want an interface for drivers then really the only
thing that should be accessible is a dma_fence_is_container and a
dma_fence_container_for_each_fence. Really no one's business to dig
into deeper details (at least once your dma_resv series has landed).

Thanks, Daniel

> Thanks,
> Christian.
>
> >
> > Cheers, Daniel
> >
> >> +{
> >> +    return dma_fence_is_array(fence) || dma_fence_is_chain(fence);
> >> +}
> >> +
> >>   #endif /* __LINUX_DMA_FENCE_H */
> >> --
> >> 2.25.1
> >>
>


-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/4] dma-buf: consolidate dma_fence subclass checking
@ 2022-01-20 11:28       ` Daniel Vetter
  0 siblings, 0 replies; 20+ messages in thread
From: Daniel Vetter @ 2022-01-20 11:28 UTC (permalink / raw)
  To: Christian König; +Cc: linaro-mm-sig, gustavo, dri-devel, linux-media

On Thu, Jan 20, 2022 at 9:50 AM Christian König
<ckoenig.leichtzumerken@gmail.com> wrote:
>
> Am 19.01.22 um 18:16 schrieb Daniel Vetter:
> > On Wed, Jan 19, 2022 at 02:43:36PM +0100, Christian König wrote:
> >> 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.
> >>
> >> Signed-off-by: Christian König <christian.koenig@amd.com>
> >> ---
> >>   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(-)
> >>
> >> diff --git a/include/linux/dma-fence-array.h b/include/linux/dma-fence-array.h
> >> index 303dd712220f..fec374f69e12 100644
> >> --- a/include/linux/dma-fence-array.h
> >> +++ b/include/linux/dma-fence-array.h
> >> @@ -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);
> >> diff --git a/include/linux/dma-fence-chain.h b/include/linux/dma-fence-chain.h
> >> index 54fe3443fd2c..ee906b659694 100644
> >> --- a/include/linux/dma-fence-chain.h
> >> +++ b/include/linux/dma-fence-chain.h
> >> @@ -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);
> >> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> >> index 1ea691753bd3..775cdc0b4f24 100644
> >> --- a/include/linux/dma-fence.h
> >> +++ b/include/linux/dma-fence.h
> >> @@ -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)
> > Code looks all good, but I'm not super enthusiastic about exporting the
> > ops to drivers and letting them do random nonsense. At least i915 does
> > pretty enormous amounts of stuff with that instead of having pushed
> > priority boosting into dma-fence as a proper concept. And maybe a few
> > other things.
> >
> > Now i915-gem team having gone off the rails of good upstream conduct is
> > another thing maybe, but I'd like to not encourage that.
> >
> > So could we perhaps do this all in header which is entirely private to
> > drivers/dma-buf, like dma-fence-internal or so? And maybe whack a big
> > fixme onto the current abuse in drivers (of which __dma_fence_is_chain()
> > gets a special price for "not how upstream should be done" *sigh*).
>
> WTF is __dma_fence_is_chain? Seeing that for the first time now.

Yes :-/

> And yes even if you do priority boosting manually that code in i915 is
> just way to complicated.
>
> I'm sure you don't have any objections that I clean up that mess now you
> pointed it out :)

Go for it. I think ideally we could get rid of the dma_fence_chain_ops
export. Btw similar situation exists for dma_fence_is_array, so maybe
check those out too.

I think if we do want an interface for drivers then really the only
thing that should be accessible is a dma_fence_is_container and a
dma_fence_container_for_each_fence. Really no one's business to dig
into deeper details (at least once your dma_resv series has landed).

Thanks, Daniel

> Thanks,
> Christian.
>
> >
> > Cheers, Daniel
> >
> >> +{
> >> +    return dma_fence_is_array(fence) || dma_fence_is_chain(fence);
> >> +}
> >> +
> >>   #endif /* __LINUX_DMA_FENCE_H */
> >> --
> >> 2.25.1
> >>
>


-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2022-01-20 11:28 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-19 13:43 [PATCH 1/4] dma-buf: consolidate dma_fence subclass checking Christian König
2022-01-19 13:43 ` Christian König
2022-01-19 13:43 ` [PATCH 2/4] dma-buf: warn about dma_fence_array container rules Christian König
2022-01-19 13:43   ` Christian König
2022-01-19 17:18   ` Daniel Vetter
2022-01-19 17:18     ` Daniel Vetter
2022-01-19 13:43 ` [PATCH 3/4] dma-buf: Warn about dma_fence_chain " Christian König
2022-01-19 13:43   ` Christian König
2022-01-19 17:18   ` Daniel Vetter
2022-01-19 17:18     ` Daniel Vetter
2022-01-19 13:43 ` [PATCH 4/4] dma-buf: warn about containers in dma_resv object Christian König
2022-01-19 13:43   ` Christian König
2022-01-19 17:19   ` Daniel Vetter
2022-01-19 17:19     ` Daniel Vetter
2022-01-19 17:16 ` [PATCH 1/4] dma-buf: consolidate dma_fence subclass checking Daniel Vetter
2022-01-19 17:16   ` Daniel Vetter
2022-01-20  8:50   ` Christian König
2022-01-20  8:50     ` Christian König
2022-01-20 11:28     ` Daniel Vetter
2022-01-20 11:28       ` Daniel Vetter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.