Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 0B69B28689A for ; Fri, 5 Sep 2025 21:02:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757106132; cv=none; b=g3PKMgxuQJ6X5cOXzDggGAnke3GKr3hcy+Cu70oVP8HLVKjy9PvPjW1icxpMdqBqrP9S67pKDpZV39lilyRMfMXG5Sg954z460OXWpe8cklHGQHAJMDaR2R4D0Auqq1GBVF1F5l/UYKrAFe+2OrYjHVJFHmSkBBkkqkBJOaZmf4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757106132; c=relaxed/simple; bh=Ys29k4EWeUZeA0xVOoxB+f8d3rS7WkJY0wgdHoo/t+4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=I54XfMe4XbTU237VxbELw1jZSZrDIPFn7YxWEtWfa2JIPww6VDSmEK7do3OzJwHttpbTGM3hO9sNN8CfBpZ/S8tvXQ7ZPZ3ukXRiKuXztv+uAr4VCbWz95wVTYsOlcY9fX2pXv5xMDABndww17/iZf79HnfUHwEWUJPoKGTlgXw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ib1Z51jF; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ib1Z51jF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2015C4CEF1; Fri, 5 Sep 2025 21:02:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1757106131; bh=Ys29k4EWeUZeA0xVOoxB+f8d3rS7WkJY0wgdHoo/t+4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ib1Z51jFf912m06DDkEvDJxAO0N7sdvoDjVG+zP2mPkTBZt+VTkOwg1uNtCK3aYug LxleSJCX20dMkemq+wpc4YNgFIG3yKXPzCaf1yUlofpBRNtu9jZXWXlxYeXps86LeO jBeV4tkBelFGTNVKIKPzWV0Vv8pJjuhvCup1jKMFQ95Z65LSEGoCuitxAY9i2/ErXb /7qYHkhyYcAG9eJJ6EQGwRY/PrP7J0kJuLxrrPHAt2IkeioZ2Ifma1/7bSoc6a0ABM xNTSGaqEYjkll8La1DnlbXQUmGEM6d5RKztNobJYOrgkdjrJv6fYl825O3baoIN9GO fEO9tpu0ptw7g== From: Sasha Levin To: stable@vger.kernel.org Cc: Qiu-ji Chen , AngeloGioacchino Del Regno , Vinod Koul , Sasha Levin Subject: [PATCH 5.15.y] dmaengine: mediatek: Fix a possible deadlock error in mtk_cqdma_tx_status() Date: Fri, 5 Sep 2025 17:02:08 -0400 Message-ID: <20250905210208.3417069-1-sashal@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <2025051905-paced-scrounger-571a@gregkh> References: <2025051905-paced-scrounger-571a@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Qiu-ji Chen [ Upstream commit 157ae5ffd76a2857ccb4b7ce40bc5a344ca00395 ] Fix a potential deadlock bug. Observe that in the mtk-cqdma.c file, functions like mtk_cqdma_issue_pending() and mtk_cqdma_free_active_desc() properly acquire the pc lock before the vc lock when handling pc and vc fields. However, mtk_cqdma_tx_status() violates this order by first acquiring the vc lock before invoking mtk_cqdma_find_active_desc(), which subsequently takes the pc lock. This reversed locking sequence (vc → pc) contradicts the established pc → vc order and creates deadlock risks. Fix the issue by moving the vc lock acquisition code from mtk_cqdma_find_active_desc() to mtk_cqdma_tx_status(). Ensure the pc lock is acquired before the vc lock in the calling function to maintain correct locking hierarchy. Note that since mtk_cqdma_find_active_desc() is a static function with only one caller (mtk_cqdma_tx_status()), this modification safely eliminates the deadlock possibility without affecting other components. This possible bug is found by an experimental static analysis tool developed by our team. This tool analyzes the locking APIs to extract function pairs that can be concurrently executed, and then analyzes the instructions in the paired functions to identify possible concurrency bugs including deadlocks, data races and atomicity violations. Fixes: b1f01e48df5a ("dmaengine: mediatek: Add MediaTek Command-Queue DMA controller for MT6765 SoC") Cc: stable@vger.kernel.org Signed-off-by: Qiu-ji Chen Reviewed-by: AngeloGioacchino Del Regno Link: https://lore.kernel.org/r/20250508073634.3719-1-chenqiuji666@gmail.com Signed-off-by: Vinod Koul Signed-off-by: Sasha Levin --- drivers/dma/mediatek/mtk-cqdma.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/dma/mediatek/mtk-cqdma.c b/drivers/dma/mediatek/mtk-cqdma.c index 41ef9f15d3d59..39e902b279e64 100644 --- a/drivers/dma/mediatek/mtk-cqdma.c +++ b/drivers/dma/mediatek/mtk-cqdma.c @@ -421,15 +421,11 @@ static struct virt_dma_desc *mtk_cqdma_find_active_desc(struct dma_chan *c, { struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c); struct virt_dma_desc *vd; - unsigned long flags; - spin_lock_irqsave(&cvc->pc->lock, flags); list_for_each_entry(vd, &cvc->pc->queue, node) if (vd->tx.cookie == cookie) { - spin_unlock_irqrestore(&cvc->pc->lock, flags); return vd; } - spin_unlock_irqrestore(&cvc->pc->lock, flags); list_for_each_entry(vd, &cvc->vc.desc_issued, node) if (vd->tx.cookie == cookie) @@ -453,9 +449,11 @@ static enum dma_status mtk_cqdma_tx_status(struct dma_chan *c, if (ret == DMA_COMPLETE || !txstate) return ret; + spin_lock_irqsave(&cvc->pc->lock, flags); spin_lock_irqsave(&cvc->vc.lock, flags); vd = mtk_cqdma_find_active_desc(c, cookie); spin_unlock_irqrestore(&cvc->vc.lock, flags); + spin_unlock_irqrestore(&cvc->pc->lock, flags); if (vd) { cvd = to_cqdma_vdesc(vd); -- 2.50.1