| Message ID | 20260706031721.24415-1-zenghongling@kylinos.cn (mailing list archive) |
|---|---|
| State | New |
| Headers |
Return-Path: <linux-sunxi+bounces-24190-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 C248E1C00CB for <noreply@patchwork.local>; Mon, 6 Jul 2026 05:17:35 +0200 (CEST) Authentication-Results: mxe881; spf=pass (sender IP is 172.234.253.10) smtp.mailfrom=linux-sunxi+bounces-24190-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-24190-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 8975C300577E for <noreply@patchwork.local>; Mon, 6 Jul 2026 03:17:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F3DFA2E0938; Mon, 6 Jul 2026 03:17: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 28151355F5F for <linux-sunxi@lists.linux.dev>; Mon, 6 Jul 2026 03:17:30 +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=1783307852; cv=none; b=brrEtjtlJHWte8a/0bekU0g2PuabjNoAKH4sV4LAAO6/L19B5SgERGpNt0Q8Kl+dwIy16D1FkpDiJpxWpNXWpDDw9MR/gYOhIBPx8tVEPf9ioAnAd0KxL7SeduA1CTCh/ykwXyNWIu3Avyb8HiY0IFnIsNsr4ZE6PyaQ6etLqd4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783307852; c=relaxed/simple; bh=hJFYERrq9ysnSX4Idrn+S2/hZq8iIzdh3p+3w4JhzHA=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=V+7brsXrcVMKys/ltBrB2TPiMVAeXG2eq9vbC5L2NuEZIETaJy7odP6QTweZGAhVFbG2ZZRb1sThrByWun0WpuXquro8BpwWbqw5w3lH0VlV4BpwSWoIH3av0QsYz/W483JcgcmUMHXFaNmVhbGN5L4RygsQDBN+oBVLhPlesU4= 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: 361154e678e911f1aa26b74ffac11d73-20260706 X-CID-CACHE: Type:Local,Time:202607061103+08,HitQuantity:1 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.12,REQID:6e982504-d7ca-45e5-a725-04a7d47906b9,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:e7bac3a,CLOUDID:8be11dd4efe86fb9ab476a1e2276960f,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:102|850|865|898,TC:nil,Content:0|15|50,E DM:-3,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: 361154e678e911f1aa26b74ffac11d73-20260706 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 424335274; Mon, 06 Jul 2026 11:17:25 +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>, stable@vger.kernel.org Subject: [PATCH v2] dmaengine: sun6i: Fix potential deadlock on sdev->lock Date: Mon, 6 Jul 2026 11:17:21 +0800 Message-Id: <20260706031721.24415-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 |
[v2] dmaengine: sun6i: Fix potential deadlock on sdev->lock
|
|
Commit Message
Hongling Zeng
July 6, 2026, 3:17 a.m. UTC
sun6i_dma_terminate_all() and sun6i_dma_pause() acquire sdev->lock
with plain spin_lock() from process context. If a DMA interrupt fires
on the same CPU while the lock is held, the interrupt handler schedules
sun6i_dma_tasklet(), which runs in softirq context and attempts to
acquire the same lock with spin_lock_irq(), causing a deadlock.
Fix by using spin_lock_irq() to disable interrupts while holding
sdev->lock, consistent with other call sites.
Fixes: ba489fd46ab6 ("dmaengine: sun6i: Add support for Allwinner A31 DMA controller")
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
---
Change in v2:
-Fix by using spin_lock_irqsave() and spin_unlock_irqrestore() to
disable interrupts while holding sdev->lock and properly save/restore
interrupt state.
---
drivers/dma/sun6i-dma.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
Comments
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: Pre-existing issues: - [Critical] The DMA interrupt handler erroneously uses the relative inner loop index `j` to resolve physical channels, causing false transfer completions and silent memory corruption for channels >= 8. - [High] Unprotected read of `vchan->phy` before acquiring locks creates severe TOCTOU races, risking memory corruption and crashes. - [High] Concurrent execution of `sun6i_dma_interrupt` and `sun6i_dma_terminate_all` causes a NULL pointer dereference on `pchan->desc`. - [High] Calling `sun6i_dma_resume` on an already pending channel corrupts the `sdev->pending` list. - [High] `sun6i_dma_terminate_all()` permanently leaks the active descriptor and its DMA pool entries for non-cyclic transfers. - [High] Use-after-free when traversing the LLI linked list in the error handling paths of `sun6i_dma_prep_slave_sg()` and `sun6i_dma_prep_dma_cyclic()`. -- commit b092144f027cb09b10f743f59c347030a2c17b8c Author: Hongling Zeng <zenghongling@kylinos.cn> dmaengine: sun6i: Fix potential deadlock on sdev->lock sun6i_dma_terminate_all() and sun6i_dma_pause() acquire sdev->lock with plain spin_lock() from process context. If a DMA interrupt fires on the same CPU while the lock is held, the interrupt handler schedules sun6i_dma_tasklet(), which runs in softirq context and attempts to acquire the same lock with spin_lock_irq(), causing a deadlock. Cc: stable@vger.kernel.org > diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c > index a9a254dbf8cb9..a6487971ad09e 100644 > --- a/drivers/dma/sun6i-dma.c > +++ b/drivers/dma/sun6i-dma.c > @@ -891,6 +891,7 @@ static int sun6i_dma_pause(struct dma_chan *chan) > struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device); > struct sun6i_vchan *vchan = to_sun6i_vchan(chan); > struct sun6i_pchan *pchan = vchan->phy; [Severity: High] This isn't a bug introduced by this patch, but vchan->phy is read without holding vchan->vc.lock. Can this allow pchan to become stale if the tasklet completes the transfer and reassigns the physical channel to a different virtual channel before the locks are acquired? > + unsigned long flags; > > dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc); > > @@ -898,9 +899,9 @@ static int sun6i_dma_pause(struct dma_chan *chan) > writel(DMA_CHAN_PAUSE_PAUSE, > pchan->base + DMA_CHAN_PAUSE); > } else { > - spin_lock(&sdev->lock); > + spin_lock_irqsave(&sdev->lock, flags); > list_del_init(&vchan->node); > - spin_unlock(&sdev->lock); > + spin_unlock_irqrestore(&sdev->lock, flags); > } > > return 0; [ ... ] > @@ -939,9 +940,9 @@ static int sun6i_dma_terminate_all(struct dma_chan *chan) [Severity: High] This is a pre-existing issue, but vchan->phy is read unlocked in this function as well. Could the stale pchan be used to modify hardware registers and descriptor lists for the wrong transfer inside the lock? > unsigned long flags; > LIST_HEAD(head); > > - spin_lock(&sdev->lock); > + spin_lock_irqsave(&sdev->lock, flags); > list_del_init(&vchan->node); > - spin_unlock(&sdev->lock); > + spin_unlock_irqrestore(&sdev->lock, flags); > > spin_lock_irqsave(&vchan->vc.lock, flags); [Severity: High] This isn't a bug introduced by this patch, but does sun6i_dma_terminate_all() permanently leak the active descriptor and its DMA pool entries for non-cyclic transfers? When a transfer is aborted, the code clears pchan->desc: drivers/dma/sun6i-dma.c:sun6i_dma_terminate_all() { ... if (vchan->cyclic) { vchan->cyclic = false; if (pchan && pchan->desc) { ... list_add_tail(&vd->node, &vc->desc_completed); } } vchan_get_all_descriptors(&vchan->vc, &head); if (pchan) { ... pchan->desc = NULL; } ... } It doesn't append the active descriptor to any cleanup list for non-cyclic transfers before clearing it, which means vchan_get_all_descriptors() fails to capture it. [Severity: Critical] This is a pre-existing issue, but does the DMA interrupt handler erroneously use the relative inner loop index 'j' to resolve physical channels? 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) && status; j++) { pchan = sdev->pchans + j; ... } On SoCs with more than 8 channels, shouldn't this use the absolute index (i * DMA_IRQ_CHAN_NR + j)? Using 'j' always resolves to channels 0-7, which could falsely complete unrelated channels and cause silent memory corruption for channels >= 8. [Severity: High] This isn't a bug introduced by this patch, but can concurrent execution of sun6i_dma_interrupt() and sun6i_dma_terminate_all() cause a NULL pointer dereference? In the hard IRQ handler, sun6i_dma_interrupt() acquires the lock and unconditionally dereferences pchan->desc: drivers/dma/sun6i-dma.c:sun6i_dma_interrupt() { ... if (vchan && (status & vchan->irq_type)) { if (vchan->cyclic) { vchan_cyclic_callback(&pchan->desc->vd); } else { spin_lock(&vchan->vc.lock); vchan_cookie_complete(&pchan->desc->vd); pchan->done = pchan->desc; spin_unlock(&vchan->vc.lock); } } ... } If sun6i_dma_terminate_all() runs concurrently, it acquires vchan->vc.lock, forcefully stops the channel, and sets pchan->desc = NULL. When the interrupt handler subsequently acquires the lock, wouldn't it dereference the NULL pchan->desc? [Severity: High] This is a pre-existing issue, but does calling sun6i_dma_resume() on an already pending channel corrupt the sdev->pending list? drivers/dma/sun6i-dma.c:sun6i_dma_resume() { ... } else if (!list_empty(&vchan->vc.desc_issued)) { spin_lock(&sdev->lock); list_add_tail(&vchan->node, &sdev->pending); spin_unlock(&sdev->lock); } ... } Unlike sun6i_dma_issue_pending(), this fails to check !list_empty(&vchan->node) before adding the node. If a channel is already waiting in the sdev->pending list, couldn't this re-addition corrupt the list pointers? [Severity: High] This isn't a bug introduced by this patch, but is there a use-after-free when traversing the LLI linked list in the error handling paths? drivers/dma/sun6i-dma.c:sun6i_dma_prep_slave_sg() { ... 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); ... } The loop body frees v_lli immediately, but the loop's update expression then reads v_lli->p_lli_next and v_lli->v_lli_next from the just-freed memory. This same pattern also appears in sun6i_dma_prep_dma_cyclic().
diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c index a9a254dbf8cb..a6487971ad09 100644 --- a/drivers/dma/sun6i-dma.c +++ b/drivers/dma/sun6i-dma.c @@ -891,6 +891,7 @@ static int sun6i_dma_pause(struct dma_chan *chan) struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device); struct sun6i_vchan *vchan = to_sun6i_vchan(chan); struct sun6i_pchan *pchan = vchan->phy; + unsigned long flags; dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc); @@ -898,9 +899,9 @@ static int sun6i_dma_pause(struct dma_chan *chan) writel(DMA_CHAN_PAUSE_PAUSE, pchan->base + DMA_CHAN_PAUSE); } else { - spin_lock(&sdev->lock); + spin_lock_irqsave(&sdev->lock, flags); list_del_init(&vchan->node); - spin_unlock(&sdev->lock); + spin_unlock_irqrestore(&sdev->lock, flags); } return 0; @@ -939,9 +940,9 @@ static int sun6i_dma_terminate_all(struct dma_chan *chan) unsigned long flags; LIST_HEAD(head); - spin_lock(&sdev->lock); + spin_lock_irqsave(&sdev->lock, flags); list_del_init(&vchan->node); - spin_unlock(&sdev->lock); + spin_unlock_irqrestore(&sdev->lock, flags); spin_lock_irqsave(&vchan->vc.lock, flags);