Skip to contentSkip to conversation

Harden the dma-fence documentation a bit more

PATCH In review Christian König 2 participants 6 messages Sep 1, 2021 – Sep 3, 2021

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

Hi guys,

while it is in most cases technically possible to not have a reference to the dma_fence when adding a callback it is usually a good idea to make sure to always have a reference anyway.

Otherwise we can indeed see cases where this doesn't really work as intended like for example in the now fixed EPOLL code.

Regards,
Christian.

#1 lore raw

PATCH 1/2 dma-buf: clarify dma_fence_ops->wait documentation

This callback is pretty much deprecated and should not be used by new implementations.

Clarify that in the documentation as well.

---
 include/linux/dma-fence.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
include/linux/dma-fence.h +3 -7
index 6ffb4b2c6371..a44e42b86c2a 100644
@@ -214,19 +214,15 @@ struct dma_fence_ops {
 	 * Custom wait implementation, defaults to dma_fence_default_wait() if
 	 * not set.
 	 *
-	 * The dma_fence_default_wait implementation should work for any fence, as long
-	 * as @enable_signaling works correctly. This hook allows drivers to
-	 * have an optimized version for the case where a process context is
-	 * already available, e.g. if @enable_signaling for the general case
-	 * needs to set up a worker thread.
+	 * Deprecated and should not be used by new implementations. Only used
+	 * by existing implementations which need special handling for their
+	 * hardware reset procedure.
 	 *
 	 * Must return -ERESTARTSYS if the wait is intr = true and the wait was
 	 * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
 	 * timed out. Can also return other error values on custom implementations,
 	 * which should be treated as if the fence is signaled. For example a hardware
 	 * lockup could be reported like that.
-	 *
-	 * This callback is optional.
 	 */
 	signed long (*wait)(struct dma_fence *fence,
 			    bool intr, signed long timeout);
Signature · 1 line
2.25.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] dma-buf: clarify dma_fence_ops->wait documentation

Quoted 5 lines · Christian König

This callback is pretty much deprecated and should not be used by new implementations.

Clarify that in the documentation as well.

Quoted 34 lines
---
 include/linux/dma-fence.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
include/linux/dma-fence.h +3 -7
index 6ffb4b2c6371..a44e42b86c2a 100644
@@ -214,19 +214,15 @@ struct dma_fence_ops {
 	 * Custom wait implementation, defaults to dma_fence_default_wait() if
 	 * not set.
 	 *
-	 * The dma_fence_default_wait implementation should work for any fence, as long
-	 * as @enable_signaling works correctly. This hook allows drivers to
-	 * have an optimized version for the case where a process context is
-	 * already available, e.g. if @enable_signaling for the general case
-	 * needs to set up a worker thread.
+	 * Deprecated and should not be used by new implementations. Only used
+	 * by existing implementations which need special handling for their
+	 * hardware reset procedure.
 	 *
 	 * Must return -ERESTARTSYS if the wait is intr = true and the wait was
 	 * interrupted, and remaining jiffies if fence has signaled, or 0 if wait
 	 * timed out. Can also return other error values on custom implementations,
 	 * which should be treated as if the fence is signaled. For example a hardware
 	 * lockup could be reported like that.
-	 *
-	 * This callback is optional.
 	 */
 	signed long (*wait)(struct dma_fence *fence,
 			    bool intr, signed long timeout);
Signature · 1 line
2.25.1
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#3 lore raw

PATCH 2/2 dma-buf: clarify dma_fence_add_callback documentation

In reply to Christian König

That the caller doesn't need to keep a reference is rather risky and not defensive at all.

Especially dma_buf_poll got that horrible wrong, so better remove that sentence and also clarify that the callback might be called in atomic or interrupt context.

---
 drivers/dma-buf/dma-fence.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/dma-buf/dma-fence.c +5 -8
index ce0f5eff575d..1e82ecd443fa 100644
@@ -616,20 +616,17 @@ EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
  * @cb: the callback to register
  * @func: the function to call
  *
+ * Add a software callback to the fence. The caller should keep a reference to
+ * the fence.
+ *
  * @cb will be initialized by dma_fence_add_callback(), no initialization
  * by the caller is required. Any number of callbacks can be registered
  * to a fence, but a callback can only be registered to one fence at a time.
  *
- * Note that the callback can be called from an atomic context.  If
- * fence is already signaled, this function will return -ENOENT (and
+ * If fence is already signaled, this function will return -ENOENT (and
  * *not* call the callback).
  *
- * Add a software callback to the fence. Same restrictions apply to
- * refcount as it does to dma_fence_wait(), however the caller doesn't need to
- * keep a refcount to fence afterward dma_fence_add_callback() has returned:
- * when software access is enabled, the creator of the fence is required to keep
- * the fence alive until after it signals with dma_fence_signal(). The callback
- * itself can be called from irq context.
+ * Note that the callback can be called from an atomic context or irq context.
  *
  * Returns 0 in case of success, -ENOENT if the fence is already signaled
  * and -EINVAL in case of error.
Signature · 1 line
2.25.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] dma-buf: clarify dma_fence_add_callback documentation

Quoted 8 lines · Christian König

That the caller doesn't need to keep a reference is rather risky and not defensive at all.

Especially dma_buf_poll got that horrible wrong, so better remove that sentence and also clarify that the callback might be called in atomic or interrupt context.

Still on the fence between documenting the precise rules and documenting the safe rules, but this is tricky enough that you got me convinced. Plus shorter, simpler, clearer kerneldoc has much better chances of being read, understood and followed.

Quoted 37 lines
---
 drivers/dma-buf/dma-fence.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/dma-buf/dma-fence.c +5 -8
index ce0f5eff575d..1e82ecd443fa 100644
@@ -616,20 +616,17 @@ EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
  * @cb: the callback to register
  * @func: the function to call
  *
+ * Add a software callback to the fence. The caller should keep a reference to
+ * the fence.
+ *
  * @cb will be initialized by dma_fence_add_callback(), no initialization
  * by the caller is required. Any number of callbacks can be registered
  * to a fence, but a callback can only be registered to one fence at a time.
  *
- * Note that the callback can be called from an atomic context.  If
- * fence is already signaled, this function will return -ENOENT (and
+ * If fence is already signaled, this function will return -ENOENT (and
  * *not* call the callback).
  *
- * Add a software callback to the fence. Same restrictions apply to
- * refcount as it does to dma_fence_wait(), however the caller doesn't need to
- * keep a refcount to fence afterward dma_fence_add_callback() has returned:
- * when software access is enabled, the creator of the fence is required to keep
- * the fence alive until after it signals with dma_fence_signal(). The callback
- * itself can be called from irq context.
+ * Note that the callback can be called from an atomic context or irq context.
  *
  * Returns 0 in case of success, -ENOENT if the fence is already signaled
  * and -EINVAL in case of error.
Signature · 1 line
2.25.1
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#5 lore raw

Re: [PATCH 2/2] dma-buf: clarify dma_fence_add_callback documentation

Am 02.09.21 um 16:42 schrieb Daniel Vetter:

Quoted 8 lines · Christian König
Quoted 7 lines

That the caller doesn't need to keep a reference is rather risky and not defensive at all.

Especially dma_buf_poll got that horrible wrong, so better remove that sentence and also clarify that the callback might be called in atomic or interrupt context.

Quoted 5 lines
Quoted 1 line

Still on the fence between documenting the precise rules and documenting the safe rules, but this is tricky enough that you got me convinced. Plus shorter, simpler, clearer kerneldoc has much better chances of being read, understood and followed.

I think that for documentation we should apply the same rules we have for code.

E.g. keep it simple until you absolutely have to make it complex and keep it defensive with the least probability for something to go wrong.

Quoted 1 line

Thanks,
Christian.

Quoted 38 lines
Quoted 38 lines
---
  drivers/dma-buf/dma-fence.c | 13 +++++--------
  1 file changed, 5 insertions(+), 8 deletions(-)
drivers/dma-buf/dma-fence.c +5 -8
index ce0f5eff575d..1e82ecd443fa 100644
@@ -616,20 +616,17 @@ EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
   * @cb: the callback to register
   * @func: the function to call
   *
+ * Add a software callback to the fence. The caller should keep a reference to
+ * the fence.
+ *
   * @cb will be initialized by dma_fence_add_callback(), no initialization
   * by the caller is required. Any number of callbacks can be registered
   * to a fence, but a callback can only be registered to one fence at a time.
   *
- * Note that the callback can be called from an atomic context.  If
- * fence is already signaled, this function will return -ENOENT (and
+ * If fence is already signaled, this function will return -ENOENT (and
   * *not* call the callback).
   *
- * Add a software callback to the fence. Same restrictions apply to
- * refcount as it does to dma_fence_wait(), however the caller doesn't need to
- * keep a refcount to fence afterward dma_fence_add_callback() has returned:
- * when software access is enabled, the creator of the fence is required to keep
- * the fence alive until after it signals with dma_fence_signal(). The callback
- * itself can be called from irq context.
+ * Note that the callback can be called from an atomic context or irq context.
   *
   * Returns 0 in case of success, -ENOENT if the fence is already signaled
   * and -EINVAL in case of error.
Signature · 1 line
2.25.1