| Message ID | 20260616060449.42225-1-zenghongling@kylinos.cn (mailing list archive) |
|---|---|
| State | New |
| Headers |
Return-Path: <linux-sunxi+bounces-23845-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 AE9211C2B02 for <noreply@patchwork.local>; Tue, 16 Jun 2026 08:05:55 +0200 (CEST) Authentication-Results: mxe881; spf=pass (sender IP is 172.234.253.10) smtp.mailfrom=linux-sunxi+bounces-23845-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-23845-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 9EAE330F804F for <noreply@patchwork.local>; Tue, 16 Jun 2026 06:05:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 13AA83DA7CD; Tue, 16 Jun 2026 06:05:03 +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 8891A3093B5 for <linux-sunxi@lists.linux.dev>; Tue, 16 Jun 2026 06:04:58 +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=1781589903; cv=none; b=aANtOpZ55QX9bv16ezwrIPPdHGqOSM0NG9Z3NbbFL9y//ydSfMXgGxR+dcMiNpk/voRoLeI6+J0JtauDNbV9PMQ5Vk8vd0DgiRzlNXGi5Z3jFSHI2Xc7Yk9csD1qM4SRFepf84QxsMSRbcO7Fs808j4NKEiSk4NE+5YGuxb/z9Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781589903; c=relaxed/simple; bh=EJYxFV26UgIohNCX98r/zv4iSx9WwinKKVrDv7Loo7k=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=rerv/SjYYQVum+HcDAcY/3vEOMNt86QBKnjb4QJPoiWvpDbJhgqGvDiQG0pp4hpXgDpixXSSvU7V+mwQB3cXLocinfHZdZ6s4dafsMfc4r88pcYelxug0I2FyMCd1R5iTOr6xVw0E46Wy4psdHAcghUxjdLGyYgTSNmYsK+em90= 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: 4b335554694911f1aa26b74ffac11d73-20260616 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.12,REQID:f8c07405-8f65-4cc3-93c2-90861c38714d,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:8d830cddd8d0be4aff8ba95ffb23005c,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: 4b335554694911f1aa26b74ffac11d73-20260616 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 1123411217; Tue, 16 Jun 2026 14:04:54 +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> Subject: [PATCH v2] dmaengine: sun6i-dma: Fix memory leak in sun6i_dma_terminate_all Date: Tue, 16 Jun 2026 14:04:49 +0800 Message-Id: <20260616060449.42225-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-Rspamd-Server: rspamd-worker-8404 X-Spamd-Result: default: False [-0.66 / 15.00]; BAYES_HAM(-5.50)[99.99%]; RBL_SENDERSCORE(2.00)[172.234.253.10:from]; SUSPICIOUS_RECIPS(1.50)[]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; MAILLIST(-0.15)[generic]; BAD_REP_POLICIES(0.10)[]; MIME_GOOD(-0.10)[text/plain]; HAS_LIST_UNSUB(-0.01)[]; DMARC_NA(0.00)[kylinos.cn]; RCPT_COUNT_TWELVE(0.00)[13]; DBL_BLOCKED_OPENRESOLVER(0.00)[kylinos.cn:email,sea.lore.kernel.org:rdns,sea.lore.kernel.org:helo]; TAGGED_RCPT(0.00)[]; FREEMAIL_CC(0.00)[vger.kernel.org,lists.infradead.org,lists.linux.dev,126.com,kylinos.cn]; FUZZY_BLOCKED(0.00)[rspamd.com]; R_SPF_ALLOW(0.00)[+ip4:172.234.253.10]; PRECEDENCE_BULK(0.00)[]; FORGED_SENDER_MAILLIST(0.00)[]; RCVD_TLS_LAST(0.00)[]; R_DKIM_NA(0.00)[]; FORGED_RECIPIENTS_MAILLIST(0.00)[]; FREEMAIL_TO(0.00)[kernel.org,gmail.com,sholland.org,arndb.de]; FROM_HAS_DN(0.00)[]; ARC_ALLOW(0.00)[subspace.kernel.org:s=arc-20240116:i=1]; TAGGED_FROM(0.00)[bounces-23845-noreply=patchwork.local]; RCVD_COUNT_FIVE(0.00)[5]; MIME_TRACE(0.00)[0:+]; FROM_NEQ_ENVFROM(0.00)[zenghongling@kylinos.cn,linux-sunxi@lists.linux.dev]; ASN(0.00)[asn:63949, ipnet:172.234.224.0/19, country:SG]; TO_DN_SOME(0.00)[] X-Rspamd-Queue-Id: AE9211C2B02 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 |
[v2] dmaengine: sun6i-dma: Fix memory leak in sun6i_dma_terminate_all
|
|
Commit Message
Hongling Zeng
June 16, 2026, 6:04 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>
---
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
---
drivers/dma/sun6i-dma.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
Comments
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: Pre-existing issues: - [Critical] Lockless read of `vchan->phy` in `sun6i_dma_terminate_all` leads to a TOCTOU race condition that can hijack and corrupt another channel's physical state. - [High] Concurrent execution of `sun6i_dma_terminate_all` and `sun6i_dma_interrupt` can lead to a fatal NULL pointer dereference. - [High] Terminating an active cyclic transfer frees the descriptor without clearing the virt-dma cyclic pointer, leading to a Use-After-Free. - [High] Plain `spin_lock` on `sdev->lock` in process context deadlocks with softirq tasklet. -- commit f08a1b576e146ed2d00cd844a90cf04a64b43397 Author: Hongling Zeng <zenghongling@kylinos.cn> dmaengine: sun6i-dma: Fix memory leak in sun6i_dma_terminate_all This commit addresses a memory leak for non-cyclic DMA transfers. It ensures both cyclic and non-cyclic active descriptors are added to the desc_completed list before cleanup to prevent them from being permanently leaked. > diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c > index a9a254dbf8cb9..46c837e87e6e4 100644 > --- a/drivers/dma/sun6i-dma.c > +++ b/drivers/dma/sun6i-dma.c > @@ -945,16 +945,14 @@ static int sun6i_dma_terminate_all(struct dma_chan *chan) > > spin_lock_irqsave(&vchan->vc.lock, flags); [Severity: High] This is a pre-existing issue, but does acquiring sdev->lock earlier in this function create a deadlock risk? sun6i_dma_terminate_all() calls spin_lock(&sdev->lock) without disabling softirqs. If a hardware interrupt fires on the same CPU while the lock is held, the kernel processes pending softirqs upon returning from the interrupt. This runs sun6i_dma_tasklet(), which attempts to acquire the lock using spin_lock_irq(&sdev->lock). Will this cause a self-deadlock since the interrupted process context already holds the lock? > > - 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) { [Severity: Critical] This isn't a bug introduced by this patch, but does this code have a race condition that can corrupt another channel? At the top of sun6i_dma_terminate_all(), the pchan pointer is cached locklessly from vchan->phy before taking any locks. Concurrently, sun6i_dma_tasklet() can complete a transfer, set vchan->phy = NULL, and reassign this exact pchan to a different virtual channel. When this code subsequently acquires vchan->vc.lock, wouldn't it use the stale pchan pointer to inappropriately access descriptors and halt hardware belonging to the newly assigned channel? > + struct virt_dma_desc *vd = &pchan->desc->vd; > + struct virt_dma_chan *vc = &vchan->vc; > > - list_add_tail(&vd->node, &vc->desc_completed); > - } > + list_add_tail(&vd->node, &vc->desc_completed); [Severity: High] This is a pre-existing issue, but does terminating an active cyclic transfer leave a dangling pointer here? The active descriptor is added to desc_completed so it can be freed by vchan_dma_desc_free_list(), but the virt-dma pointer vc->cyclic is never cleared. If vchan_complete() runs asynchronously from the tasklet, couldn't it see a non-NULL vc->cyclic and dereference the freed descriptor? Standard virt-dma drivers typically use vchan_terminate_vdesc() to clear this. > } > > + vchan->cyclic = false; > vchan_get_all_descriptors(&vchan->vc, &head); > > if (pchan) { [ ... ] > vchan->phy = NULL; > pchan->vchan = NULL; > pchan->desc = NULL; [Severity: High] This isn't a bug introduced by this patch, but can clearing pchan->desc here race with the interrupt handler and cause a NULL pointer dereference? In sun6i_dma_interrupt(), vchan->cyclic is read locklessly. If it evaluates to false, the handler blocks on spin_lock(&vchan->vc.lock): sun6i_dma_interrupt() { ... if (!vchan->cyclic) { spin_lock(&vchan->vc.lock); vchan_cookie_complete(&pchan->desc->vd); ... } If sun6i_dma_terminate_all() concurrently acquires the lock and sets pchan->desc = NULL, when the interrupt handler unblocks, wouldn't evaluating &pchan->desc->vd crash the kernel? > pchan->done = NULL; > }
Dne torek, 16. junij 2026 ob 08:04:49 Srednjeevropski poletni čas je Hongling Zeng napisal(a): > 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> Best regards, Jernej > > --- > 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 > --- > drivers/dma/sun6i-dma.c | 12 +++++------- > 1 file changed, 5 insertions(+), 7 deletions(-) > > diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c > index 7a79f346250a..12d038ef5f2e 100644 > --- a/drivers/dma/sun6i-dma.c > +++ b/drivers/dma/sun6i-dma.c > @@ -946,16 +946,14 @@ 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; > + if (pchan && pchan->desc && pchan->desc != pchan->done) { > + struct virt_dma_desc *vd = &pchan->desc->vd; > + struct virt_dma_chan *vc = &vchan->vc; > > - list_add_tail(&vd->node, &vc->desc_completed); > - } > + list_add_tail(&vd->node, &vc->desc_completed); > } > > + vchan->cyclic = false; > vchan_get_all_descriptors(&vchan->vc, &head); > > if (pchan) { >
On Tue, Jun 16, 2026 at 02:04:49PM +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. > > Fixes: 555859308723 ("dmaengine: sun6i: Add driver for the Allwinner A31 DMA controller") > Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn> > > --- > 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 > --- > drivers/dma/sun6i-dma.c | 12 +++++------- > 1 file changed, 5 insertions(+), 7 deletions(-) > > diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c > index 7a79f346250a..12d038ef5f2e 100644 > --- a/drivers/dma/sun6i-dma.c > +++ b/drivers/dma/sun6i-dma.c > @@ -946,16 +946,14 @@ 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; > + if (pchan && pchan->desc && pchan->desc != pchan->done) { > + struct virt_dma_desc *vd = &pchan->desc->vd; > + struct virt_dma_chan *vc = &vchan->vc; > > - list_add_tail(&vd->node, &vc->desc_completed); > - } > + list_add_tail(&vd->node, &vc->desc_completed); should It be in desc_terminated queue? ref: https://lore.kernel.org/dmaengine/ajETw7uwVx_U9o5F@ryzen/T/#m541c24b45fb425c6a8a81d800db225b58411447e Frank > } > > + vchan->cyclic = false; > vchan_get_all_descriptors(&vchan->vc, &head); > > if (pchan) { > -- > 2.25.1 >
Hi Frank and Jernej Skrabec:
Thanks for all the review!
You're absolutely right.After checking virt-dma.h, I see that
desc_terminated is the correct queue for actively terminated transfers.
I'll update to use:
- vchan_terminate_vdesc(vd);
I'll send v3 ,Please help to review.
Best regards,
Hongling Zeng
在 2026年06月17日 04:47, Frank Li 写道:
> On Tue, Jun 16, 2026 at 02:04:49PM +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.
>>
>> Fixes: 555859308723 ("dmaengine: sun6i: Add driver for the Allwinner A31 DMA controller")
>> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
>>
>> ---
>> 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
>> ---
>> drivers/dma/sun6i-dma.c | 12 +++++-------
>> 1 file changed, 5 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c
>> index 7a79f346250a..12d038ef5f2e 100644
>> --- a/drivers/dma/sun6i-dma.c
>> +++ b/drivers/dma/sun6i-dma.c
>> @@ -946,16 +946,14 @@ 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;
>> + if (pchan && pchan->desc && pchan->desc != pchan->done) {
>> + struct virt_dma_desc *vd = &pchan->desc->vd;
>> + struct virt_dma_chan *vc = &vchan->vc;
>>
>> - list_add_tail(&vd->node, &vc->desc_completed);
>> - }
>> + list_add_tail(&vd->node, &vc->desc_completed);
> should It be in desc_terminated queue?
>
> ref: https://lore.kernel.org/dmaengine/ajETw7uwVx_U9o5F@ryzen/T/#m541c24b45fb425c6a8a81d800db225b58411447e
>
> Frank
>> }
>>
>> + 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..12d038ef5f2e 100644 --- a/drivers/dma/sun6i-dma.c +++ b/drivers/dma/sun6i-dma.c @@ -946,16 +946,14 @@ 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; + if (pchan && pchan->desc && pchan->desc != pchan->done) { + struct virt_dma_desc *vd = &pchan->desc->vd; + struct virt_dma_chan *vc = &vchan->vc; - list_add_tail(&vd->node, &vc->desc_completed); - } + list_add_tail(&vd->node, &vc->desc_completed); } + vchan->cyclic = false; vchan_get_all_descriptors(&vchan->vc, &head); if (pchan) {