Re: [PATCH v12 4/7] dmaengine: dw: use virt-dma for descriptor and callback ownership
From: James Hilliard
Date: Mon Nov 30 2026 - 13:10:48 EST
On Wed, Nov 25, 2026 at 1:45 PM Frank Li <Frank.li@xxxxxxxxxxx> wrote:
>
> On Sat, Nov 21, 2026 at 02:03:19PM -0700, James Hilliard wrote:
> > The status getter can invoke a client's completion callback while polling
> > the hardware. An 8250 RX timeout calls it with the port lock held, and the
> > callback takes that same lock. Termination also lacks a synchronization
> > hook to wait for callbacks already detached from the channel's queues.
> >
> > Serge Semin proposed leaving transfer completion to the controller
> > tasklet [1]. Clients such as catpt poll with the controller interrupt
> > masked, so the status getter still needs to process RAW.XFER and
> > advance software-LLP blocks.
> >
> > Use virt-dma for transaction queues, cookie completion and callback
> > delivery. Keep the active transaction and hardware state under vc.lock.
> > Run client callbacks only from the per-channel virt-dma tasklet.
> >
> > An 8250 RX timeout consumes the transfer length minus the reported
> > residue. On a bus error, source reads do not establish how many bytes
> > reached the destination. Credit only whole blocks whose completion the
> > driver observed while advancing a software-linked transfer. For a
> > hardware-linked transfer, treat the whole buffer as unconfirmed.
> >
> > Report the abort and the same residue to result callbacks and status
> > pollers, even after another transfer completes. Reserve the error record
> > during preparation, when clients can still handle allocation failure.
> > Use the normal virt-dma submission path; neither submission nor fault
> > reporting needs to allocate. Keep only failed cookies after descriptor
> > release, until the cookie is reused or the channel is released.
> >
> > Submit only the first block of each transaction to virt-dma. Its child
> > LLIs remain on the driver's hardware-chain list and are freed together
> > after the callback returns. Keep the controller tasklet for hardware
> > event processing and error recovery.
> >
> > Complete cookies under vc.lock before handing descriptors to the callback
> > tasklet. Complete terminated cookies in submission order without invoking
> > their callbacks, so a concurrent termination cannot advance the completed
> > cookie past an older completion that later moves it backwards. Newly
> > submitted transactions become eligible to start when issue_pending()
> > moves them to the issued queue.
> >
> > Implement device_synchronize with vchan_synchronize(), which drains only
> > the selected channel's callbacks. DMAengine calls it before releasing
> > channel resources. Exclude the controller tasklet while clearing channel
> > configuration and powering down, and let a pending run return without
> > register access after the last channel has been released.
> >
> > Fixes: 3bfb1d20b547 ("dmaengine: Driver for the Synopsys DesignWare DMA controller")
> > Fixes: 69cea5a00d31 ("dmaengine/dw_dmac: Replace spin_lock* with irqsave variants and enable submission from callback")
> > Fixes: b36f09c3c441 ("dmaengine: Add transfer termination synchronization support")
> > Suggested-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
> > Link: https://lore.kernel.org/r/20240911184710.4207-2-fancer.lancer@xxxxxxxxx [1]
> > Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
> > ---
> > drivers/dma/dw/Kconfig | 1 +
> > drivers/dma/dw/core.c | 502 +++++++++++++++++++++++++------------------------
> > drivers/dma/dw/regs.h | 26 ++-
> > 3 files changed, 273 insertions(+), 256 deletions(-)
> >
> > diff --git a/drivers/dma/dw/Kconfig b/drivers/dma/dw/Kconfig
> > index a9828ddd6d06..444702c4a059 100644
> > --- a/drivers/dma/dw/Kconfig
> > +++ b/drivers/dma/dw/Kconfig
> > @@ -7,6 +7,7 @@
> > config DW_DMAC_CORE
> > tristate
> > select DMA_ENGINE
> > + select DMA_VIRTUAL_CHANNELS
> >
> > config DW_DMAC
> > tristate "Synopsys DesignWare AHB DMA platform driver"
> > diff --git a/drivers/dma/dw/core.c b/drivers/dma/dw/core.c
> > index 3df696e41ff2..d00d0d53f6ba 100644
> > --- a/drivers/dma/dw/core.c
> > +++ b/drivers/dma/dw/core.c
> > @@ -8,6 +8,7 @@
> > */
> >
> > #include <linux/bitops.h>
> > +#include <linux/cleanup.h>
> > #include <linux/delay.h>
> > #include <linux/dmaengine.h>
> > #include <linux/dma-mapping.h>
> > @@ -41,35 +42,6 @@
> >
> > /*----------------------------------------------------------------------*/
> >
> > -static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
> > -{
> > - return to_dw_desc(dwc->active_list.next);
> > -}
> > -
> > -static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
> > -{
> > - struct dw_desc *desc = txd_to_dw_desc(tx);
> > - struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
> > - dma_cookie_t cookie;
> > - unsigned long flags;
> > -
> > - spin_lock_irqsave(&dwc->vc.lock, flags);
> > - cookie = dma_cookie_assign(tx);
> > -
> > - /*
> > - * REVISIT: We should attempt to chain as many descriptors as
> > - * possible, perhaps even appending to those already submitted
> > - * for DMA. But this is hard to do in a race-free manner.
> > - */
> > -
> > - list_add_tail(&desc->desc_node, &dwc->queue);
> > - spin_unlock_irqrestore(&dwc->vc.lock, flags);
> > - dev_vdbg(dmaengine_chan_dev(tx->chan), "%s: queued %u\n",
> > - __func__, desc->txd.cookie);
> > -
> > - return cookie;
> > -}
> > -
> > static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
> > {
> > struct dw_dma *dw = to_dw_dma(dwc->vc.chan.device);
> > @@ -80,12 +52,8 @@ static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
> > if (!desc)
> > return NULL;
> >
> > - dwc->descs_allocated++;
> > INIT_LIST_HEAD(&desc->tx_list);
> > - dma_async_tx_descriptor_init(&desc->txd, &dwc->vc.chan);
> > - desc->txd.tx_submit = dwc_tx_submit;
> > - desc->txd.flags = DMA_CTRL_ACK;
> > - desc->txd.phys = phys;
> > + desc->vd.tx.phys = phys;
> > return desc;
> > }
> >
> > @@ -97,14 +65,86 @@ static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
> > if (unlikely(!desc))
> > return;
> >
> > - list_for_each_entry_safe(child, _next, &desc->tx_list, desc_node) {
> > - list_del(&child->desc_node);
> > - dma_pool_free(dw->desc_pool, child, child->txd.phys);
> > - dwc->descs_allocated--;
> > + list_for_each_entry_safe(child, _next, &desc->tx_list, vd.node) {
> > + list_del(&child->vd.node);
> > + dma_pool_free(dw->desc_pool, child, child->vd.tx.phys);
> > }
> >
> > - dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
> > - dwc->descs_allocated--;
> > + kfree(desc->error);
> > + dma_pool_free(dw->desc_pool, desc, desc->vd.tx.phys);
> > +}
> > +
> > +static void dwc_desc_free(struct virt_dma_desc *vd)
> > +{
> > + struct dw_dma_chan *dwc = to_dw_dma_chan(vd->tx.chan);
> > +
> > + dwc_desc_put(dwc, vd_to_dw_desc(vd));
> > +}
> > +
> > +static struct dma_async_tx_descriptor *
> > +dwc_tx_prep(struct dw_dma_chan *dwc, struct dw_desc *desc, unsigned long flags)
> > +{
> > + if (flags & DMA_CTRL_REUSE)
> > + goto err;
> > +
> > + /* Reserve a record now so neither submission nor error completion can fail. */
> > + desc->error = kmalloc_obj(*desc->error, GFP_ATOMIC);
> > + if (desc->error)
> > + return vchan_tx_prep(&dwc->vc, &desc->vd, flags);
> > +err:
> > + dwc_desc_put(dwc, desc);
> > + return NULL;
> > +}
> > +
> > +static int dwc_error_cmp(const void *key, const struct rb_node *node)
> > +{
> > + const struct dw_dma_error *error = rb_entry(node, struct dw_dma_error, node);
> > +
> > + return *(const dma_cookie_t *)key - error->cookie;
> > +}
> > +
> > +static int dwc_error_cmp_nodes(struct rb_node *a, const struct rb_node *b)
> > +{
> > + return dwc_error_cmp(&rb_entry(a, struct dw_dma_error, node)->cookie, b);
> > +}
> > +
> > +static void dwc_retire_cookie(struct dw_dma_chan *dwc, struct virt_dma_desc *vd)
> > +{
> > + struct dw_desc *desc = vd_to_dw_desc(vd);
> > + struct dw_dma_error *error = desc->error;
> > + struct rb_node *old;
> > +
> > + lockdep_assert_held(&dwc->vc.lock);
> > + /* A numeric cookie may still have an error from before it wrapped. */
> > + if (vd->tx_result.result == DMA_TRANS_ABORTED) {
> > + error->cookie = vd->tx.cookie;
> > + error->residue = vd->tx_result.residue;
> > + old = rb_find_add(&error->node, &dwc->errors, dwc_error_cmp_nodes);
> > + if (!old) {
> > + desc->error = NULL;
> > + return;
> > + }
> > + rb_entry(old, struct dw_dma_error, node)->residue = error->residue;
> > + } else {
> > + old = rb_find(&vd->tx.cookie, &dwc->errors, dwc_error_cmp);
> > + if (old) {
> > + rb_erase(old, &dwc->errors);
> > + kfree(rb_entry(old, struct dw_dma_error, node));
> > + }
> > + }
> > +}
> > +
> > +static void dwc_free_errors(struct dw_dma_chan *dwc)
> > +{
> > + struct dw_dma_error *error, *next;
> > + struct rb_root errors;
> > +
> > + scoped_guard(spinlock_irqsave, &dwc->vc.lock) {
> > + errors = dwc->errors;
> > + dwc->errors = RB_ROOT;
> > + }
> > + rbtree_postorder_for_each_entry_safe(error, next, &errors, node)
> > + kfree(error);
> > }
>
> I can't understand why need rb tree for errors? if error happen, whole
> desc should return error.
The whole transaction does fail. The tree does not determine whether
the descriptor failed; it retains the failed cookie and residue after
virt-dma has completed and freed that descriptor. A later tx_status()
query would otherwise see the completed cookie and report DMA_COMPLETE,
even though the transfer aborted.
More than one failed cookie can remain queryable after newer transfers
complete. The tree provides per-cookie lookup for those records; a
different per-cookie container could serve the same purpose.
>
> Frank