| Message ID | 20260701045733.33654-1-zenghongling@kylinos.cn (mailing list archive) |
|---|---|
| State | New |
| Headers |
Return-Path: <linux-sunxi+bounces-24050-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 D46551C0792 for <noreply@patchwork.local>; Wed, 1 Jul 2026 06:58:36 +0200 (CEST) Authentication-Results: mxe881; spf=pass (sender IP is 172.234.253.10) smtp.mailfrom=linux-sunxi+bounces-24050-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-24050-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 94B333012D0C for <noreply@patchwork.local>; Wed, 1 Jul 2026 04:57:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3DB7A38B7A1; Wed, 1 Jul 2026 04:57:54 +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 9B636381AEB for <linux-sunxi@lists.linux.dev>; Wed, 1 Jul 2026 04:57:51 +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=1782881874; cv=none; b=N8u76QAGtAhRGfx7TK+66mN5TuNs9LsCXCNPz48wHFbkrpx2DsZy3yMaPV2/3M+owodZRkTGH2rDCBIb4hkCe6qVXwI5F13yasOm9tx4nc3uTy3R6DfqYUjBbzOkil2x/dfOFLdZxLB1GNti6hYWBgAXRK1ccG5Nf27iiRhh/yA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782881874; c=relaxed/simple; bh=mveOLRV9TX3+36ENzTwHzA3tDMfCci7eSqRRlrbJ0Kw=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=qLnTl9DKNVnRA4OxBsPn3mYHK1XpUmz3RIh6wLeSAP2tPxfXQB19qvrvPrhXN0aEDbDACb3DY64cZtg9JlNZaoZDybsuZgzuZQHYSUWT5AXJdBUwb25CK5rw2RMOPDSNbXpm6zQmc1GOJulnvkVcEproClFv/G4oK3IpIC3Xaxs= 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: 61711196750911f1aa26b74ffac11d73-20260701 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.12,REQID:0b727f38-f9ab-4716-96c7-ed9e6adbaf25,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:31543b51669c0d188af52ef103980411,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: 61711196750911f1aa26b74ffac11d73-20260701 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 2047955673; Wed, 01 Jul 2026 12:57:37 +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>, Frank Li <Frank.Li@nxp.com> Subject: [PATCH v5] dmaengine: sun6i-dma: Fix reclaim descriptors while terminating DMA Date: Wed, 1 Jul 2026 12:57:33 +0800 Message-Id: <20260701045733.33654-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 |
[v5] dmaengine: sun6i-dma: Fix reclaim descriptors while terminating DMA
|
|
Commit Message
Hongling Zeng
July 1, 2026, 4:57 a.m. UTC
When terminating DMA transfers, active descriptors are not properly
reclaimed. Only cyclic descriptors were handled, leaving non-cyclic
descriptors and their LLI chains to be permanently leaked.
Fix by using vchan_terminate_vdesc() which handles both cyclic and
non-cyclic descriptors by adding them to desc_terminated queue for
proper cleanup.
Add pchan->desc != pchan->done check to prevent double-adding completed
descriptors, which would corrupt the list.
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>
Reviewed-by: Frank Li <Frank.Li@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
---
Change in v4:
-Correct the commit message
---
Chaneg in v5:
-Correct the subject
-Add reviewed-by
---
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: - [High] Unprotected out-of-lock read of `vchan->phy` leads to a severe TOCTOU race causing list corruption and wrong channel teardown. - [High] TOCTOU race in hardware interrupt handler leads to a fatal NULL pointer dereference when terminating DMA. - [High] Missing IRQ masking when acquiring `sdev->lock` in process context causes a deadlock with the softirq tasklet. - [High] Incorrect array indexing in `sun6i_dma_interrupt` incorrectly routes upper hardware channels to lower channel states. -- commit 494d0d1002ddd0015efbf5a3a6ef1f6894fb2914 Author: Hongling Zeng <zenghongling@kylinos.cn> dmaengine: sun6i-dma: Fix reclaim descriptors while terminating DMA This commit updates sun6i_dma_terminate_all() to use vchan_terminate_vdesc() so that both cyclic and non-cyclic descriptors are properly reclaimed. It also adds a check to prevent double-adding completed descriptors to the list. > 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 just above this hunk, the function acquires sdev->lock using a plain spin_lock(): sun6i_dma_terminate_all() { ... spin_lock(&sdev->lock); list_del_init(&vchan->node); spin_unlock(&sdev->lock); Could this plain spin_lock cause a deadlock? Since this is called from process context, hardware interrupts remain enabled. If a DMA interrupt fires on this CPU while the lock is held, the softirq can execute sun6i_dma_tasklet() which attempts to acquire the same lock with spin_lock_irq(): sun6i_dma_tasklet() { spin_lock_irq(&sdev->lock); } Could this cause the tasklet to spin forever waiting for the interrupted process to release it? The same pattern appears in sun6i_dma_pause(). > 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); [Severity: High] This is a pre-existing issue, but is it safe to trust the pchan pointer here? The pchan pointer is read locklessly at the start of the function (struct sun6i_pchan *pchan = vchan->phy;). If a tasklet runs concurrently before the locks are acquired, couldn't it complete the current descriptor, free this physical channel, and reallocate it to a different virtual channel? If that happens, this code would evaluate the stale pointer inside the critical section and mistakenly call vchan_terminate_vdesc() on another channel's descriptors. > } > > + vchan->cyclic = false; > vchan_get_all_descriptors(&vchan->vc, &head); > > if (pchan) { [Severity: High] This is a pre-existing issue, but inside this if block, the descriptor pointer is cleared: sun6i_dma_terminate_all() { ... if (pchan) { ... pchan->desc = NULL; } } Could clearing the descriptor pointer here race with the interrupt handler? If another CPU is actively processing a hardware interrupt when this channel is terminated: CPU1: sun6i_dma_interrupt() validates vchan locklessly CPU2: sun6i_dma_terminate_all() sets pchan->desc = NULL; CPU1: sun6i_dma_interrupt() calls vchan_cyclic_callback(&pchan->desc->vd); Would this lead to a NULL pointer dereference when evaluating the descriptor on CPU1? While reviewing the driver, I noticed another pre-existing issue in sun6i_dma_interrupt(): drivers/dma/sun6i-dma.c:sun6i_dma_interrupt() { ... for (i = 0; i < sdev->num_pchans / DMA_IRQ_CHAN_NR; i++) { ... for (j = 0; j < DMA_IRQ_CHAN_NR; j++) { ... pchan = sdev->pchans + j; [Severity: High] This is a pre-existing issue, but does this inner loop incorrectly route upper-bank interrupts to the lower physical channels? For SoCs with more than 8 DMA channels, the outer loop checks register banks (i = 0, 1). However, the physical channel lookup uses sdev->pchans + j instead of including the offset for the current bank (sdev->pchans + i * DMA_IRQ_CHAN_NR + j). Could an interrupt from physical channel 8 or above incorrectly process the virtual channel attached to physical channel 0, potentially causing descriptors to complete for the wrong channel?
On 01-07-26, 12:57, Hongling Zeng wrote: > When terminating DMA transfers, active descriptors are not properly > reclaimed. Only cyclic descriptors were handled, leaving non-cyclic > descriptors and their LLI chains to be permanently leaked. > > Fix by using vchan_terminate_vdesc() which handles both cyclic and > non-cyclic descriptors by adding them to desc_terminated queue for > proper cleanup. > > Add pchan->desc != pchan->done check to prevent double-adding completed > descriptors, which would corrupt the list. You should run checkpatch to fix the formatting issues. ERROR: trailing whitespace #65: FILE: drivers/dma/sun6i-dma.c:95 For now, I have fixed and applied
On Wed, 01 Jul 2026 12:57:33 +0800, Hongling Zeng wrote: > When terminating DMA transfers, active descriptors are not properly > reclaimed. Only cyclic descriptors were handled, leaving non-cyclic > descriptors and their LLI chains to be permanently leaked. > > Fix by using vchan_terminate_vdesc() which handles both cyclic and > non-cyclic descriptors by adding them to desc_terminated queue for > proper cleanup. > > [...] Applied, thanks! [1/1] dmaengine: sun6i-dma: Fix reclaim descriptors while terminating DMA commit: ab1150115e68a46b687eb38c1ab92782018c9f2c Best regards,
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) {