From patchwork Wed Aug 16 17:00:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengfeng Ye X-Patchwork-Id: 13355509 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 D2023C001DF for ; Wed, 16 Aug 2023 17:01:05 +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=TOYIDkrv/9Ab5TxXXaWOUzSKZwsOBZqzgn93j1m2zGE=; b=c5twD9EV/6d1G1 4E0oWnPoLhPhM7kWL8guim5yUCGiRRGykOr/U5eevrZqhTAKVKJ7TFHeTqh+7v1RKDkeEy1aTS/lS xGrXSz6P+dMkBMD/JHYgHY8gvG7n1dn1hYvRPiJJ72WO58zBccQ46bN1OeJEXY/aQXk2Ai3bez7Bs f+yN8quJi3dvAN7+vI3ThNL3lKXAmNDlp+CcQ0q4S6O6CCH0QD6ZjdQUDk+QIgUnsnNBUMyKsoSvN 1ypDvYjEpVyuD6OcEnGaMHSYbSFA38IIJNNbBrQRaJTZmTU170oahFS6+kEzm8/zNsFxK9zhqdBg8 30fYylb07r0936nlYGkw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qWJss-004fO7-2m; Wed, 16 Aug 2023 17:00:38 +0000 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qWJsq-004fNY-0r for linux-arm-kernel@lists.infradead.org; Wed, 16 Aug 2023 17:00:37 +0000 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-6887918ed20so1236284b3a.2 for ; Wed, 16 Aug 2023 10:00:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692205234; x=1692810034; h=message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0PYaXMe/nlp8yVrrb6yGMJtqNU4p8cj6vJCqISzfbDQ=; b=aE+Zc0+bDMFXuytHHOEjtSFikOls6o9h64eXNmdVlBVceWkTRtD3SClPyn5NvcCHDH VdxNW84FhE49loir0qeW0lQtI+Q5jMwYRxNJyiuktIvm7ZKWN81TW5Ws54wLTWP11zez Xb9TntzGuMTXfESjdkD4yeAjM7mtHqZ+ZEEc+1NBo7f7YycOcY5BzqlFgUMxJkVUJi9k 5Utf/Rk5AEfneFqoRwwh1lJsYIU74yBULDdZ1mXJwfgbWDEC4r6jQjV5Uprt3LHSutqD pxMXg9LZ5Dujpp5Ah6+f5eFjtch7vP2VB89BKTLAZ08qanlgKrOEXxa36egphaQhqoQY RMdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692205234; x=1692810034; h=message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0PYaXMe/nlp8yVrrb6yGMJtqNU4p8cj6vJCqISzfbDQ=; b=g8BZ04hKHkrClY8vdrKjIFPre/W+2XknUBXfOo0otJNufZvjvaH0Pqln5uYA8qDGX0 tALwFbU6dhcMZIaz0awHYTSwANiR/WlNazISyK+creaLPvdELyC23y+QXqJq2f3QTdJp uqOcuF+xG+o1SwysuYJrGNQ60kDg+86RlPib8LhYfmV8VpZKZrcCRiEV/nLx78+g7SfW xIa/hZpkYa/nh51dUaLGWxwQqxCK6dXHHiiYu4E++IQmTacD3rjjyj4JevmQ7PgP/f8q xAYPRHIAjXk62gZGD3H9AFRUPT2t/2cp1XwPL/ccX23fdpysuV0oqugkVd990EM0iPq1 rQfA== X-Gm-Message-State: AOJu0Yx6PeQ5kJrKIXP5CrWgWWWuPrOA9j1bBFOpy50YDKFjyRl7B5mA I4NSId/PYXhF6sDIopr0J8o= X-Google-Smtp-Source: AGHT+IGkennMBgjBNmFDn/FXs93MyYPFOqjjMuHUvzYfQ/6a0Z5X49K9MASilFYVNVNKBZxze8QtNg== X-Received: by 2002:a17:90a:fd14:b0:268:42a2:35db with SMTP id cv20-20020a17090afd1400b0026842a235dbmr1903541pjb.48.1692205234144; Wed, 16 Aug 2023 10:00:34 -0700 (PDT) Received: from 377044c6c369.cse.ust.hk (191host097.mobilenet.cse.ust.hk. [143.89.191.97]) by smtp.gmail.com with ESMTPSA id n32-20020a17090a2ca300b00262d9b4b527sm12327087pjd.52.2023.08.16.10.00.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Aug 2023 10:00:33 -0700 (PDT) From: Chengfeng Ye To: vkoul@kernel.org, sugaya.taichi@socionext.com, orito.takao@socionext.com, len.baker@gmx.com, jaswinder.singh@linaro.org Cc: dmaengine@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Chengfeng Ye Subject: [PATCH v2] dmaengine: milbeaut-hdmac: Fix potential deadlock on &mc->vc.lock Date: Wed, 16 Aug 2023 17:00:13 +0000 Message-Id: <20230816170013.4262-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-20230816_100036_303879_C3A2D50F X-CRM114-Status: GOOD ( 13.67 ) 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 &mc->vc.lock is acquired by milbeaut_hdmac_interrupt() under irq context, other acquisition of the same lock under process context should disable irq, otherwise deadlock could happen if the irq preempts the execution of process context code while the lock is held in process context on the same CPU. milbeaut_hdmac_chan_config(), milbeaut_hdmac_chan_resume() and milbeaut_hdmac_chan_pause() are such callback functions not disable irq by default. Possible deadlock scenario: milbeaut_hdmac_chan_config() -> spin_lock(&mc->vc.lock) -> milbeaut_hdmac_interrupt() -> spin_lock(&mc->vc.lock); (deadlock here) This flaw was found by an experimental static analysis tool I am developing for irq-related deadlock. The tentative patch fixes the potential deadlock by spin_lock_irqsave() in the three callback functions to disable irq while lock is held. Signed-off-by: Chengfeng Ye --- Change in V2: - Also change &mc->vc.lock to &vc->lock for uniformity consideration --- drivers/dma/milbeaut-hdmac.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/drivers/dma/milbeaut-hdmac.c b/drivers/dma/milbeaut-hdmac.c index 1b0a95892627..5c664c8c10f5 100644 --- a/drivers/dma/milbeaut-hdmac.c +++ b/drivers/dma/milbeaut-hdmac.c @@ -214,10 +214,11 @@ milbeaut_hdmac_chan_config(struct dma_chan *chan, struct dma_slave_config *cfg) { struct virt_dma_chan *vc = to_virt_chan(chan); struct milbeaut_hdmac_chan *mc = to_milbeaut_hdmac_chan(vc); + unsigned long flags; - spin_lock(&mc->vc.lock); + spin_lock_irqsave(&vc->lock, flags); mc->cfg = *cfg; - spin_unlock(&mc->vc.lock); + spin_unlock_irqrestore(&vc->lock, flags); return 0; } @@ -226,13 +227,14 @@ static int milbeaut_hdmac_chan_pause(struct dma_chan *chan) { struct virt_dma_chan *vc = to_virt_chan(chan); struct milbeaut_hdmac_chan *mc = to_milbeaut_hdmac_chan(vc); + unsigned long flags; u32 val; - spin_lock(&mc->vc.lock); + spin_lock_irqsave(&vc->lock, flags); val = readl_relaxed(mc->reg_ch_base + MLB_HDMAC_DMACA); val |= MLB_HDMAC_PB; writel_relaxed(val, mc->reg_ch_base + MLB_HDMAC_DMACA); - spin_unlock(&mc->vc.lock); + spin_unlock_irqrestore(&vc->lock, flags); return 0; } @@ -241,13 +243,14 @@ static int milbeaut_hdmac_chan_resume(struct dma_chan *chan) { struct virt_dma_chan *vc = to_virt_chan(chan); struct milbeaut_hdmac_chan *mc = to_milbeaut_hdmac_chan(vc); + unsigned long flags; u32 val; - spin_lock(&mc->vc.lock); + spin_lock_irqsave(&vc->lock, flags); val = readl_relaxed(mc->reg_ch_base + MLB_HDMAC_DMACA); val &= ~MLB_HDMAC_PB; writel_relaxed(val, mc->reg_ch_base + MLB_HDMAC_DMACA); - spin_unlock(&mc->vc.lock); + spin_unlock_irqrestore(&vc->lock, flags); return 0; }