From patchwork Tue Jun 27 16:43:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengfeng Ye X-Patchwork-Id: 13294813 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5C8A5EB64DC for ; Tue, 27 Jun 2023 16:44:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date:Subject:Cc:To :From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=uL9Ba0vHjFDGeIYCGa8hrt/shorfA8rhKWPnn2jwHo0=; b=P1c3yNAk0Hwwkf xKA6BnZ+KE16GSh+c57TZ+5gM9YHr7mBKwFKowbCnPFNJexuixrwFuAyqY6TKfMGu06p+cCS8KzN5 AhkQJIgSUZxu7I58Kekq3yqeSdCuLB/6dmvaQbqOlW36XpZ3oNPGpuiWwnGEnEnLW8R6dj3+AAXYx weOsdyf2RAbd09c8M14LQKzWwqiiuqWeoA0FbpzL+elncymau0DSi5AQqqFsEGT2Wka+7zIpu7WTD MFosEli1BOTGm4v+dqhXwNzOHu4Xc8pYuXWNDDhFIdIHTZyAaJ8ifjINm4mduFdSlW/h1v8o020KA Se+dxvJ44wWY1rFLd6WQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qEBmw-00DfAE-2x; Tue, 27 Jun 2023 16:43:34 +0000 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qEBmu-00Df9H-03; Tue, 27 Jun 2023 16:43:33 +0000 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-668709767b1so3018807b3a.2; Tue, 27 Jun 2023 09:43:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687884208; x=1690476208; h=message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qy8/BA41lOJ9tHciVEFoPKVGOra2xL33Ff1AOugAL5g=; b=FwCVNSPx8LXb6QTh7PdSqwrtSvAIf/zDW4EARi28pobQc75y5eN8Z2hpzA14/sX/k4 sP1RLiYE0RZL/qd4NtLiC///7VDecwKeu7J3hyW0X6zsCgkx5nd0Tt6Rp82j7uNFvXcj XEwNwxjKLmYgCJg441CQSefrlqxkjYdO3j2nG1aX4vps+Qhw2Veho45XIKEOY0fap8PR 3lykX7x3kIQmyuDUm8gu3kt1zdFjl+y/Nv72Cv9eXS9QJ9Jp5hDYpa7lshd7S3exgus0 pPXWmaDnsdtCbxW7a5UM3bt6lzkbwyLASqBXQ4OIO0FV11ChvuhnuOs0gUDYHBInN2xq DbcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687884208; x=1690476208; h=message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qy8/BA41lOJ9tHciVEFoPKVGOra2xL33Ff1AOugAL5g=; b=aSe+o8VSQ759orMxNS7n0+j1XEHQz8yE+3MC6Ta/elHcOEUKbOsvcpt3uJNkWTJq3k eRRz5cBrqO1bq0g6FqSy373migZfGCxyynRe3MxZO1acdOj8D9I/9+jYpymWJfALoDyK 0qk2QP7cD1dKOIGXVxNrbwlXwgPV+W0ynhereidY2q+/00aHcjJxM9CPHA6PUZCkEC7D HbKDHnN2ZWZzxIg6eXucVjErJpY2WjSUkyzaJO7O4ty5UVsimhJH5QocEOBx+xf5waK3 Q0FovuQi7mmw43DkYqz1jOddSmT8ytEMoUUQ+MWZbfQptBpkP9IVrCrEXjkgFEXFfhof KgkQ== X-Gm-Message-State: AC+VfDzIPhCjjjDHOiMYAlinFHIvB9aH7Vg6Oh4eadUEdFx0CqGdkSQk UEQBinDaObXDIBvyS+SO26w= X-Google-Smtp-Source: ACHHUZ5nR3SzvcwXIaA2STdJUbtdQU1KzmmjZQuZPBMKm/Wdu30ozJAOtD5F3e9gZWX4UsiH2KBDoA== X-Received: by 2002:a05:6a20:7f82:b0:11a:efaa:eb43 with SMTP id d2-20020a056a207f8200b0011aefaaeb43mr33140157pzj.3.1687884208460; Tue, 27 Jun 2023 09:43:28 -0700 (PDT) Received: from 377044c6c369.cse.ust.hk (191host097.mobilenet.cse.ust.hk. [143.89.191.97]) by smtp.gmail.com with ESMTPSA id u5-20020aa78485000000b00671eb039b23sm4410638pfn.58.2023.06.27.09.43.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jun 2023 09:43:27 -0700 (PDT) From: Chengfeng Ye To: sean.wang@mediatek.com, vkoul@kernel.org, matthias.bgg@gmail.com, angelogioacchino.delregno@collabora.com Cc: dmaengine@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, Chengfeng Ye Subject: [PATCH] dmaengine: mediatek: mtk-hsdma: fix potential deadlock on &vc->lock Date: Tue, 27 Jun 2023 16:43:09 +0000 Message-Id: <20230627164309.59922-1-dg573847474@gmail.com> X-Mailer: git-send-email 2.17.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230627_094332_073436_88286A03 X-CRM114-Status: GOOD ( 11.71 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org As &vc->lock is acquired by the irq mtk_hsdma_irq(), other process context code acquiring the lock should disable irq. The terminate callback mtk_hsdma_terminate_all() acquires the same lock without closing irq. Possible deadlock scenario: mtk_hsdma_free_active_desc() -> spin_lock(&hvc->vc.lock) -> mtk_hsdma_irq() -> mtk_hsdma_free_rooms_in_ring() -> spin_lock(&hvc->vc.lock) (deadlock here) This flaw was found using an experimental static analysis tool we are developing for irq-related deadlock. The tentative patch fix the potential deadlock by spin_lock_irqsave(). Signed-off-by: Chengfeng Ye --- drivers/dma/mediatek/mtk-hsdma.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/dma/mediatek/mtk-hsdma.c b/drivers/dma/mediatek/mtk-hsdma.c index 69cc61c0b262..6b6773575893 100644 --- a/drivers/dma/mediatek/mtk-hsdma.c +++ b/drivers/dma/mediatek/mtk-hsdma.c @@ -757,18 +757,19 @@ static void mtk_hsdma_free_active_desc(struct dma_chan *c) { struct mtk_hsdma_vchan *hvc = to_hsdma_vchan(c); bool sync_needed = false; + unsigned long flags; /* * Once issue_synchronize is being set, which means once the hardware * consumes all descriptors for the channel in the ring, the * synchronization must be notified immediately it is completed. */ - spin_lock(&hvc->vc.lock); + spin_lock_irqsave(&hvc->vc.lock, flags); if (!list_empty(&hvc->desc_hw_processing)) { hvc->issue_synchronize = true; sync_needed = true; } - spin_unlock(&hvc->vc.lock); + spin_unlock_irqrestore(&hvc->vc.lock, flags); if (sync_needed) wait_for_completion(&hvc->issue_completion);