| Message ID | 20260617023411.574488-1-zenghongling@kylinos.cn (mailing list archive) |
|---|---|
| State | New |
| Headers |
Return-Path: <linux-sunxi+bounces-23852-sunxi=pue.re@lists.linux.dev> X-Original-To: noreply@patchwork.local Delivered-To: noreply@patchwork.local Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mxe881.netcup.net (Postfix) with ESMTPS id 4C9031C007A for <noreply@patchwork.local>; Wed, 17 Jun 2026 04:34:43 +0200 (CEST) Authentication-Results: mxe881; spf=pass (sender IP is 172.234.253.10) smtp.mailfrom=linux-sunxi+bounces-23852-noreply=patchwork.local@lists.linux.dev smtp.helo=sea.lore.kernel.org Received-SPF: pass (mxe881: domain of lists.linux.dev designates 172.234.253.10 as permitted sender) client-ip=172.234.253.10; envelope-from=linux-sunxi+bounces-23852-noreply=patchwork.local@lists.linux.dev; helo=sea.lore.kernel.org; Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1]) by sea.lore.kernel.org (Postfix) with ESMTP id 4A76830DFA69 for <noreply@patchwork.local>; Wed, 17 Jun 2026 02:34:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 92B3F2FB969; Wed, 17 Jun 2026 02:34:32 +0000 (UTC) X-Original-To: linux-sunxi@lists.linux.dev Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BDC42DB7AE for <linux-sunxi@lists.linux.dev>; Wed, 17 Jun 2026 02:34:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781663672; cv=none; b=gF0Z+sBXfT9zuTO2iVY6cKiCggjH8Sbn5zDMNSgl4OiGeig9hevVebBcOGfmdjHniWtUQTL6F2h8cxNqGlbb0S9hPqfn24kbVTsed2H2tnPeB9GcmaMmQH5+ZplsFoJOAMplIDLlOgLfu8k5Xe0J6XxTR91dfZsB42kGULOAdb0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781663672; c=relaxed/simple; bh=9tpthZ546URBjjWAsL/0GB29wqScr9CUo826UVyIc2Q=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=qs0zMlU3iovcYjv08EYffVOFZF0tCNiGZa1UiVPbWZPEqLPYwM4nIolnlbXtiu+QyXbPQyBCOUI84k/hpuog3JQYJMjvIkbKvjmSK77MqtHYmGIvLnRk3o4L3bivXWGAHVPqx2lGhBY2VfP2f+BGP8ji1FXoP+d1pw+AlnD0gVE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 088dc55c69f511f1aa26b74ffac11d73-20260617 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.12,REQID:3509e3fe-e98e-43f4-8322-c8e72fabe82c,IP:0,U RL:0,TC:0,Content:-25,EDM:-25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACT ION:release,TS:-50 X-CID-META: VersionHash:e7bac3a,CLOUDID:cfbce81a19c81799c41c83c0864cf5ba,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:102|850|865|898,TC:nil,Content:0|15|50,E DM:2,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA: 0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 088dc55c69f511f1aa26b74ffac11d73-20260617 X-User: zenghongling@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from <zenghongling@kylinos.cn>) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 106712783; Wed, 17 Jun 2026 10:34:15 +0800 From: Hongling Zeng <zenghongling@kylinos.cn> To: vkoul@kernel.org, Frank.Li@kernel.org, wens@kernel.org, jernej.skrabec@gmail.com, samuel@sholland.org, mripard@kernel.org, arnd@arndb.de Cc: dmaengine@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, zhongling0719@126.com, Hongling Zeng <zenghongling@kylinos.cn>, Frank Li <Frank.li@oss.nxp.com> Subject: [PATCH v3] dmaengine: sun6i-dma: Fix memory leak in sun6i_dma_terminate_all Date: Wed, 17 Jun 2026 10:34:11 +0800 Message-Id: <20260617023411.574488-1-zenghongling@kylinos.cn> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-sunxi@lists.linux.dev List-Id: <linux-sunxi.lists.linux.dev> List-Subscribe: <mailto:linux-sunxi+subscribe@lists.linux.dev> List-Unsubscribe: <mailto:linux-sunxi+unsubscribe@lists.linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-MORS-Enabled: yes X-MORS-DOMAIN: patchwork.local X-MORS-HOSTING: hosting172546 X-MORS-USER: hosting172546 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= |
| Series |
[v3] dmaengine: sun6i-dma: Fix memory leak in sun6i_dma_terminate_all
|
|
Commit Message
Hongling Zeng
June 17, 2026, 2:34 a.m. UTC
When terminating a non-cyclic DMA transfer, the active descriptor
is not properly reclaimed. The descriptor is removed from the
desc_issued list in sun6i_dma_start_desc(), but in
sun6i_dma_terminate_all(), only cyclic transfer descriptors are
added to the desc_completed list before cleanup.
For non-cyclic transfers, pchan->desc is set to NULL without first
adding the descriptor back to a list that vchan_get_all_descriptors()
can collect. This causes the descriptor and its associated LLI chain
to be permanently leaked.
Fix by ensuring both cyclic and non-cyclic active descriptors are
added to the desc_completed list before setting pchan->desc to NULL.
Fixes: 555859308723 ("dmaengine: sun6i: Add driver for the Allwinner A31 DMA controller")
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
Acked-by: Jernej Skrabec <jernej.skrabec@gmail.com>
Suggested-by: Frank Li <Frank.li@oss.nxp.com>
---
Change in v2;
-Add pchan->desc != pchan->done check to prevent race condition
where completed descriptors could be double-added to desc_completed
list, causing list corruption
---
Change in v3:
-Fix by using vchan_terminate_vdesc() as suggested by Frank Li
---
drivers/dma/sun6i-dma.c | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)
Comments
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Pre-existing issues:
- [Critical] A concurrency race condition between `sun6i_dma_interrupt` and `sun6i_dma_terminate_all` leads to a NULL pointer dereference or Use-After-Free.
- [High] Time-Of-Check to Time-Of-Use (TOCTOU) race condition on `vchan->phy` in `sun6i_dma_terminate_all` leads to severe state and linked list corruption.
- [High] Use-After-Free in the `err_lli_free` error path of `sun6i_dma_prep_slave_sg` and `sun6i_dma_prep_dma_cyclic`.
- [High] Deadlock due to acquiring `sdev->lock` without disabling softirqs in process context.
--
commit ef76fa0808e0c4c9df78bcb34c318f781e0ae211
Author: Hongling Zeng <zenghongling@kylinos.cn>
dmaengine: sun6i-dma: Fix memory leak in sun6i_dma_terminate_all
When terminating a non-cyclic DMA transfer, the active descriptor
is not properly reclaimed because only cyclic transfer descriptors
are added to the desc_completed list before cleanup. This causes
the descriptor and its associated LLI chain to be permanently leaked.
This fix ensures both cyclic and non-cyclic active descriptors are
added to the list before setting pchan->desc to NULL.
> diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c
> index a9a254dbf8cb9..f47a326dd7ffa 100644
> --- a/drivers/dma/sun6i-dma.c
> +++ b/drivers/dma/sun6i-dma.c
> @@ -945,16 +945,13 @@ static int sun6i_dma_terminate_all(struct dma_chan *chan)
[Severity: High]
This is a pre-existing issue, but can acquiring sdev->lock cause a deadlock
here?
Earlier in this function:
spin_lock(&sdev->lock);
list_del_init(&vchan->node);
spin_unlock(&sdev->lock);
Because spin_lock() does not disable softirqs, could a hardware interrupt
occur while this lock is held, schedule sun6i_dma_tasklet(), and cause the
tasklet to spin forever on sdev->lock in softirq context on the same CPU?
This exact same locking pattern also appears in sun6i_dma_pause().
[Severity: High]
This is a pre-existing issue, but is there a TOCTOU race on vchan->phy?
At the beginning of sun6i_dma_terminate_all(), pchan is read locklessly:
struct sun6i_pchan *pchan = vchan->phy;
Could sun6i_dma_tasklet() free this physical channel and reallocate it to a
different virtual channel before we acquire the lock? If so, does mutating
pchan->desc and calling vchan_terminate_vdesc(vd) below corrupt the
descriptor lists of an unrelated channel?
>
> spin_lock_irqsave(&vchan->vc.lock, flags);
>
> - if (vchan->cyclic) {
> - vchan->cyclic = false;
> - if (pchan && pchan->desc) {
> - struct virt_dma_desc *vd = &pchan->desc->vd;
> - struct virt_dma_chan *vc = &vchan->vc;
> + if (pchan && pchan->desc && pchan->desc != pchan->done) {
> + struct virt_dma_desc *vd = &pchan->desc->vd;
>
> - list_add_tail(&vd->node, &vc->desc_completed);
> - }
> + vchan_terminate_vdesc(vd);
> }
>
> + vchan->cyclic = false;
> vchan_get_all_descriptors(&vchan->vc, &head);
>
> if (pchan) {
[Severity: Critical]
This is a pre-existing issue, but does sun6i_dma_terminate_all() race with
sun6i_dma_interrupt()?
Later in this function, we set pchan->desc = NULL under vchan->vc.lock:
if (pchan) {
writel(DMA_CHAN_ENABLE_STOP, pchan->base + DMA_CHAN_ENABLE);
writel(DMA_CHAN_PAUSE_RESUME, pchan->base + DMA_CHAN_PAUSE);
vchan->phy = NULL;
pchan->vchan = NULL;
pchan->desc = NULL;
pchan->done = NULL;
}
If sun6i_dma_interrupt() is currently executing on another CPU, it evaluates
pchan->desc->vd either locklessly (for cyclic) or blocks on vchan->vc.lock
(for non-cyclic):
if (vchan->cyclic) {
vchan_cyclic_callback(&pchan->desc->vd);
} else {
spin_lock(&vchan->vc.lock);
vchan_cookie_complete(&pchan->desc->vd);
...
Once sun6i_dma_terminate_all() drops the lock, sun6i_dma_interrupt() will
dereference the invalidated pointer. Can this cause a NULL pointer dereference
or a Use-After-Free?
[Severity: High]
This isn't a bug introduced by this patch, but is there a Use-After-Free in
the error path of sun6i_dma_prep_slave_sg() and sun6i_dma_prep_dma_cyclic()?
In the err_lli_free label of both functions:
err_lli_free:
for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli;
p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next)
dma_pool_free(sdev->pool, v_lli, p_lli);
dma_pool_free() frees v_lli, but the loop update expression subsequently
evaluates v_lli = v_lli->v_lli_next. Does this dereference the freed memory?
On Wed, Jun 17, 2026 at 10:34:11AM +0800, Hongling Zeng wrote: > When terminating a non-cyclic DMA transfer, the active descriptor > is not properly reclaimed. The descriptor is removed from the > desc_issued list in sun6i_dma_start_desc(), but in > sun6i_dma_terminate_all(), only cyclic transfer descriptors are > added to the desc_completed list before cleanup. > > For non-cyclic transfers, pchan->desc is set to NULL without first > adding the descriptor back to a list that vchan_get_all_descriptors() > can collect. This causes the descriptor and its associated LLI chain > to be permanently leaked. > > Fix by ensuring both cyclic and non-cyclic active descriptors are > added to the desc_completed list before setting pchan->desc to NULL. Can you update this to match your change. Frank > > Fixes: 555859308723 ("dmaengine: sun6i: Add driver for the Allwinner A31 DMA controller") > Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn> > Acked-by: Jernej Skrabec <jernej.skrabec@gmail.com> > Suggested-by: Frank Li <Frank.li@oss.nxp.com> > > --- > Change in v2; > -Add pchan->desc != pchan->done check to prevent race condition > where completed descriptors could be double-added to desc_completed > list, causing list corruption > --- > Change in v3: > -Fix by using vchan_terminate_vdesc() as suggested by Frank Li > --- > drivers/dma/sun6i-dma.c | 13 +++++-------- > 1 file changed, 5 insertions(+), 8 deletions(-) > > diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c > index 7a79f346250a..134ae840f176 100644 > --- a/drivers/dma/sun6i-dma.c > +++ b/drivers/dma/sun6i-dma.c > @@ -946,16 +946,13 @@ static int sun6i_dma_terminate_all(struct dma_chan *chan) > > spin_lock_irqsave(&vchan->vc.lock, flags); > > - if (vchan->cyclic) { > - vchan->cyclic = false; > - if (pchan && pchan->desc) { > - struct virt_dma_desc *vd = &pchan->desc->vd; > - struct virt_dma_chan *vc = &vchan->vc; > - > - list_add_tail(&vd->node, &vc->desc_completed); > - } > + if (pchan && pchan->desc && pchan->desc != pchan->done) { > + struct virt_dma_desc *vd = &pchan->desc->vd; > + > + vchan_terminate_vdesc(vd); > } > > + vchan->cyclic = false; > vchan_get_all_descriptors(&vchan->vc, &head); > > if (pchan) { > -- > 2.25.1 >
diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c index 7a79f346250a..134ae840f176 100644 --- a/drivers/dma/sun6i-dma.c +++ b/drivers/dma/sun6i-dma.c @@ -946,16 +946,13 @@ static int sun6i_dma_terminate_all(struct dma_chan *chan) spin_lock_irqsave(&vchan->vc.lock, flags); - if (vchan->cyclic) { - vchan->cyclic = false; - if (pchan && pchan->desc) { - struct virt_dma_desc *vd = &pchan->desc->vd; - struct virt_dma_chan *vc = &vchan->vc; - - list_add_tail(&vd->node, &vc->desc_completed); - } + if (pchan && pchan->desc && pchan->desc != pchan->done) { + struct virt_dma_desc *vd = &pchan->desc->vd; + + vchan_terminate_vdesc(vd); } + vchan->cyclic = false; vchan_get_all_descriptors(&vchan->vc, &head); if (pchan) {