| Message ID | 20260616033116.23490-1-zenghongling@kylinos.cn (mailing list archive) |
|---|---|
| State | New |
| Headers |
Return-Path: <linux-sunxi+bounces-23842-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 246121C0135 for <noreply@patchwork.local>; Tue, 16 Jun 2026 05:33:25 +0200 (CEST) Authentication-Results: mxe881; spf=pass (sender IP is 172.234.253.10) smtp.mailfrom=linux-sunxi+bounces-23842-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-23842-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 6C9C3310ECCD for <noreply@patchwork.local>; Tue, 16 Jun 2026 03:31:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F97132BF52; Tue, 16 Jun 2026 03:31:31 +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 2DE5A3C1F5C for <linux-sunxi@lists.linux.dev>; Tue, 16 Jun 2026 03:31:28 +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=1781580691; cv=none; b=L/K10SXx5OhPpIg6B2pkE+S/c5zcplGU2Q7DH+7camwY1tAAXOjlSkqYrh4EDdtWiJ0kH/KPS1XB9lXEPHRUv9I7zn3IBLW4ppAwrwgDNeIxxX2HAP7PD5V+sFBJsaR8k3m4dM3wTnIaCPHJ1jwg/kb7bwY572JgV+X3miVXvOM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781580691; c=relaxed/simple; bh=Y2KI/8DPDFkNPzODRUnUuZ5On4n0HvY0aZ9FK9/iZ54=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=ski+ZUktmJ+gBxQyfPOzGdXSTpcn6hX9wlzVKHtleHvqwfHvPD4UVEJNYxAP4uxVtuzrpWGRweNjTqeB2ya0I5UrMtzklPvF/fXH3EagJKLf2eyl4enQnQ6XWpSB4MlV1IMRn6uNcoJhqjzD09DLDRnTQaCbJ7Jl63ej34gLzQQ= 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: d922ccde693311f1aa26b74ffac11d73-20260616 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.12,REQID:a41df261-ddae-454f-8e05-8bdf533db092,IP:0,U RL:0,TC:0,Content:-5,EDM:-25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:-30 X-CID-META: VersionHash:e7bac3a,CLOUDID:dd2cfc02ff709e3f057f61824139d4d7,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: d922ccde693311f1aa26b74ffac11d73-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 404894445; Tue, 16 Jun 2026 11:31:23 +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] dmaengine: sun6i-dma: Fix memory leak in sun6i_dma_terminate_all Date: Tue, 16 Jun 2026 11:31:16 +0800 Message-Id: <20260616033116.23490-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]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; HAS_LIST_UNSUB(-0.01)[]; RCPT_COUNT_TWELVE(0.00)[13]; TAGGED_RCPT(0.00)[]; FREEMAIL_CC(0.00)[vger.kernel.org,lists.infradead.org,lists.linux.dev,126.com,kylinos.cn]; DMARC_NA(0.00)[kylinos.cn]; PRECEDENCE_BULK(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; R_SPF_ALLOW(0.00)[+ip4:172.234.253.10]; FROM_HAS_DN(0.00)[]; RCVD_COUNT_FIVE(0.00)[5]; 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]; FORGED_SENDER_MAILLIST(0.00)[]; ARC_ALLOW(0.00)[subspace.kernel.org:s=arc-20240116:i=1]; TAGGED_FROM(0.00)[bounces-23842-noreply=patchwork.local]; 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: 246121C0135 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 |
dmaengine: sun6i-dma: Fix memory leak in sun6i_dma_terminate_all
|
|
Commit Message
Hongling Zeng
June 16, 2026, 3:31 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>
---
drivers/dma/sun6i-dma.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
Comments
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: New issues: - [High] The patch unconditionally adds the active descriptor `pchan->desc` to `vc->desc_completed` in `sun6i_dma_terminate_all()`, causing list corruption and a potential use-after-free for non-cyclic transfers. Pre-existing issues: - [High] A TOCTOU race condition between `sun6i_dma_interrupt()` and `sun6i_dma_terminate_all()` allows the interrupt handler to dereference a NULL `pchan->desc` pointer. - [High] Use-after-free when calculating residue for a recently completed transfer in `sun6i_dma_tx_status()`. - [High] Deadlock risk due to acquiring `sdev->lock` with plain `spin_lock()` in process context, which is vulnerable to softirq preemption. - [High] The interrupt handler locklessly reads `pchan->vchan`, which races with channel reallocation and can prematurely complete a newly started transfer. -- commit c1c2a26ec23500f38a915dc234fa3b40c6cbad7d Author: Hongling Zeng <zenghongling@kylinos.cn> dmaengine: sun6i-dma: Fix memory leak in sun6i_dma_terminate_all This commit fixes a memory leak in sun6i_dma_terminate_all() for non-cyclic DMA transfers. It ensures that both cyclic and non-cyclic active descriptors are added to the desc_completed list before setting pchan->desc to NULL. > diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c > index a9a254dbf8cb9..7f9414e3a3814 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 isn't a bug introduced by this patch, but just above this line in sun6i_dma_terminate_all(), there is a plain spin_lock() used in process context: spin_lock(&sdev->lock); list_del_init(&vchan->node); spin_unlock(&sdev->lock); Does this risk an infinite spinlock deadlock? Since sun6i_dma_terminate_all() is callable from process context, a hardirq can preempt execution while sdev->lock is held. When the hardirq finishes, the pending softirqs would run sun6i_dma_tasklet on the same CPU, which attempts to acquire the lock using spin_lock_irqsave(&sdev->lock, flags), potentially deadlocking. > - 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) { > + 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] Does unconditionally adding the descriptor to desc_completed risk list corruption for non-cyclic transfers? When a non-cyclic transfer completes, sun6i_dma_interrupt() calls vchan_cookie_complete(), which places the descriptor on vc->desc_completed, sets pchan->done = pchan->desc, and schedules the vchan_complete tasklet. However, pchan->desc isn't cleared until sun6i_dma_tasklet runs. If sun6i_dma_terminate_all() executes during this window, it observes a non-NULL pchan->desc and executes list_add_tail() again. Since the descriptor is already on the list (or has been spliced/freed by the completion tasklet), this double-add could corrupt the linked list pointers. Should this check if (pchan->desc != pchan->done) first? [ ... ] [Severity: High] This is a pre-existing issue, but is there a TOCTOU race condition between sun6i_dma_interrupt() and sun6i_dma_terminate_all()? In sun6i_dma_interrupt(), pchan->vchan is read locklessly. If sun6i_dma_terminate_all() runs concurrently on another CPU, it can acquire the channel lock, set pchan->desc = NULL, and release the lock: CPU1 (sun6i_dma_terminate_all): if (pchan) { ... pchan->desc = NULL; } CPU2 (sun6i_dma_interrupt): spin_lock(&vchan->vc.lock); vchan_cookie_complete(&pchan->desc->vd); Could this cause a fatal NULL pointer dereference when &pchan->desc->vd evaluates to NULL inside vchan_cookie_complete()? [Severity: High] This is a pre-existing issue, but does sun6i_dma_tx_status() have a use-after-free risk when calculating residue? When a transfer finishes, the vchan_complete tasklet completes the callback and frees the descriptor memory. However, pchan->desc is not cleared until the separate sun6i_dma_tasklet runs. If dmaengine_tx_status() is called in this window, vchan_find_desc() returns NULL, and the code falls back to checking pchan && pchan->desc. It then calls sun6i_get_chan_size(pchan), which accesses the descriptor: drivers/dma/sun6i-dma.c:sun6i_get_chan_size() { ... for (lli = txd->v_lli; lli; lli = lli->v_lli_next) { ... } Will this dereference the already-freed descriptor memory? [Severity: High] This is a pre-existing issue, but does the interrupt handler risk prematurely completing a newly started transfer? sun6i_dma_terminate_all() stops the physical channel but leaves pending interrupt bits set in the hardware DMA_IRQ_STAT register. The sun6i_dma_tasklet can immediately reallocate this physical channel to a new virtual channel and start it. If the delayed hardware interrupt from the old transfer fires, sun6i_dma_interrupt() reads pchan->vchan locklessly: drivers/dma/sun6i-dma.c:sun6i_dma_interrupt() { ... vchan = pchan->vchan; if (vchan && (status & vchan->irq_type)) { ... } If it evaluates as the newly assigned vchan, and non-cyclic transfers share the same irq_type bitmask, could the handler incorrectly acquire the new vchan->vc.lock and prematurely complete the new transfer's descriptor?
diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c index 7a79f346250a..97730ba6c874 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) { + 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) {