[v3,1/3] dmaengine: virt-dma: share deferred descriptor retirement

Message ID 20261119-submit-sun4i-dma-callback-lifetime-v1-v3-1-3935aef58db9@gmail.com (mailing list archive)
State New
Headers
Series dmaengine: sun4i: retain cyclic descriptors until callbacks drain |

Commit Message

James Hilliard Nov. 19, 2026, 6:02 p.m. UTC
A cyclic callback can retain its descriptor after the tasklet drops the
virtual-channel lock. Collecting and freeing descriptors during atomic
termination does not wait for that callback, and clearing vc->cyclic alone
only cancels a callback which has not started.

Provide vchan_terminate_all() to clear the pending cyclic callback and
move submitted, issued and completed descriptors to desc_terminated. A
cyclic descriptor moved to the completed list by an error path is still
retained; completed descriptors already detached by the tasklet remain
its responsibility.

Leave prepared descriptors on desc_allocated: the client still owns them
and may submit them after terminating earlier work. Freeing channel
resources, unlike termination, still releases these descriptors.

vchan_synchronize() drains callbacks before freeing or recycling retired
descriptors. Repeated termination leaves them on the terminated list.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 drivers/dma/virt-dma.h | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
  

Patch

diff --git a/drivers/dma/virt-dma.h b/drivers/dma/virt-dma.h
index abf2ae737c30..f753c6b62d6b 100644
--- a/drivers/dma/virt-dma.h
+++ b/drivers/dma/virt-dma.h
@@ -199,6 +199,25 @@  static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
 	list_splice_tail_init(&vc->desc_terminated, head);
 }
 
+/**
+ * vchan_terminate_all - retire descriptors until callbacks have drained
+ * @vc: virtual channel to terminate
+ *
+ * The caller must hold vc.lock and prevent further callback publication.
+ * Submitted, issued and completed descriptors stay on desc_terminated until
+ * vchan_synchronize() drains callbacks. Prepared descriptors remain available
+ * for submission. The completion tasklet owns descriptors it already detached.
+ */
+static inline void vchan_terminate_all(struct virt_dma_chan *vc)
+{
+	lockdep_assert_held(&vc->lock);
+
+	vc->cyclic = NULL;
+	list_splice_tail_init(&vc->desc_submitted, &vc->desc_terminated);
+	list_splice_tail_init(&vc->desc_issued, &vc->desc_terminated);
+	list_splice_tail_init(&vc->desc_completed, &vc->desc_terminated);
+}
+
 static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
 {
 	struct virt_dma_desc *vd;
@@ -232,7 +251,7 @@  static inline const char *vchan_chan_name(struct virt_dma_chan *vc)
  * Makes sure that all scheduled or active callbacks have finished running. For
  * proper operation the caller has to ensure that no new callbacks are scheduled
  * after the invocation of this function started.
- * Free up the terminated cyclic descriptor to prevent memory leakage.
+ * Free or recycle terminated descriptors after their callbacks have drained.
  */
 static inline void vchan_synchronize(struct virt_dma_chan *vc)
 {