From patchwork Wed Jun 28 16:47:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengfeng Ye X-Patchwork-Id: 13296064 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 CD8FEEB64D7 for ; Wed, 28 Jun 2023 16:48:54 +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=jMwDu66264qbfE9LKdpbAL8pdOYDsE4NoGc//vHrj2o=; b=pUtAWm/Ij/Jk1e OcFYzd5pPHmZb03CQuapGovrXKce+StSzzsO8nzCJ0PmxP0SlcXgOITYlocj5v4ywRIGbJ6Adl70U /iNE4hPJ9g6P1YvFfNfqu97mQ3j9DTUkULjXJjeOUFpxH6llKeiWYHyLcQ01h/EXicZSkGWwzdEpK ARMWQV8jVameIRva6nRvYhQHekUIPLR43WE6m9HBUFl4gelf3SNmkViYmILvJwst28Ioye/xLA5fx fKXKpzq51Pk1wpqJ3/5CEuEWKXX1RM2E3n2Z6MbjQnE/c+7UZhPA+VM0NgSzD3vYnTFTdFzeCP1Dr dxpVGbEhxdR3AjvqMSgg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qEYLB-00GBhl-2A; Wed, 28 Jun 2023 16:48:25 +0000 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qEYL8-00GBg6-10 for linux-arm-kernel@lists.infradead.org; Wed, 28 Jun 2023 16:48:23 +0000 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-66767d628e2so3676066b3a.2 for ; Wed, 28 Jun 2023 09:48:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687970898; x=1690562898; h=message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wp97cGGpKr64bUZG2HsIrRL1H4FjGhOC/kMVFyCMzf8=; b=FSGDU7NlG8PeyFJEJEEBv6tE/GDYvudPVH7ysSaPc/LdPy9v0r0hxtd3TMbIkGkjCB 9WDL7pA6Gvd166G2iHQNMGmHcsROaQul3p68o6dnGZIdJl7K6H9Ps7wpsoywYF+ZCp/f GKgShm/+vT3YalEz52rY+g2TqEu/XrJrBzXab0IJccy7W2CxvwSnpRPxrSTdjWOy1JQC +sYAz7WYDe5H7R+xlWQ6OF9rt1C+2U6Q5T49TosC0l+qpZMrnOWy+c6reDxC+ZR7dZ9F esoTtAaLJBoahP5T2b6RY/hqzTlPu31YMfuTK1TKnkyC0LMZEBPnNiKIj3W9LP75hfxl ItVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687970898; x=1690562898; h=message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wp97cGGpKr64bUZG2HsIrRL1H4FjGhOC/kMVFyCMzf8=; b=YR0LR2sEasDSyEsE7+eRBxeLOZ/Hmbw1C7PjxLDZpH+Cw127u2Be+BzHGnHmpM8NGZ K0z1bdF5SsBZ4y5U6j/OpuWDIR5fk6Fb9DRlImAAt04K3an7blQJTrCeSSZ6INsglPi4 n2qtyYXO1H/rgrJ8f7o1p9N3POuqnaImEdNpBKoZwllWxd9hPr9OJQZRl8xDpkeX6QS0 WwtgprxycVgFuUe9ErPkE0nVITDnIBPT2VRf/fcvDFYa8GCE7wtKEK/v3UktvKVapJmj DIhq6YcJSarjJPz5H1B0YF0MutfpTrqwahXwjll3G7BqGsnOewhiGmsAPLvlHueTBgaA 5r7g== X-Gm-Message-State: AC+VfDyUks1JM8mzL6OqZqB47m5GUWXxBoEaaJDEhVzl1O6U8Q8Lk9H+ +a3uTBy1eGsrz+bYeUS5K3w= X-Google-Smtp-Source: ACHHUZ5yIpKRZwCdR+tbijRQabMXEhbZXBA+ET3pD1NIQ5tWmMWdS5ewR9m0gZpDIpBRIhrcdegezA== X-Received: by 2002:a17:90a:1999:b0:25e:ad19:5f46 with SMTP id 25-20020a17090a199900b0025ead195f46mr23301757pji.12.1687970898452; Wed, 28 Jun 2023 09:48:18 -0700 (PDT) Received: from 377044c6c369.cse.ust.hk (191host097.mobilenet.cse.ust.hk. [143.89.191.97]) by smtp.gmail.com with ESMTPSA id ob11-20020a17090b390b00b00262dc783974sm6733914pjb.31.2023.06.28.09.48.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jun 2023 09:48:17 -0700 (PDT) From: Chengfeng Ye To: krzysztof.kozlowski@linaro.org, alim.akhtar@samsung.com, wim@linux-watchdog.org, linux@roeck-us.net Cc: linux-arm-kernel@lists.infradead.org, linux-samsung-soc@vger.kernel.org, linux-watchdog@vger.kernel.org, linux-kernel@vger.kernel.org, Chengfeng Ye Subject: [PATCH] watchdog: s3c2410: Fix potential deadlock on &wdt->lock Date: Wed, 28 Jun 2023 16:47:59 +0000 Message-Id: <20230628164759.67594-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-20230628_094822_371756_316452CF X-CRM114-Status: GOOD ( 12.41 ) 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 &wdt->lock is acquired by hard irq s3c2410wdt_irq(), other acquisition of the same lock under process context should disable irq, otherwise deadlock could happen if the irq preempt the execution while the lock is held in process context on the same CPU. [Interrupt]: s3c2410wdt_irq -->/root/linux/drivers/watchdog/s3c2410_wdt.c:547 -->/root/linux/drivers/watchdog/s3c2410_wdt.c:383 -->spin_lock(&wdt->lock); [Process Context]: s3c2410wdt_suspend -->/root/linux/drivers/watchdog/s3c2410_wdt.c:764 -->/root/linux/drivers/watchdog/s3c2410_wdt.c:403 -->spin_lock(&wdt->lock); [Process Context]: s3c2410wdt_probe -->/root/linux/drivers/watchdog/s3c2410_wdt.c:710 -->/root/linux/drivers/watchdog/s3c2410_wdt.c:415 -->spin_lock(&wdt->lock); This flaw was found by an experimental static analysis tool I am developing for irq-related deadlock, which reported the above warning when analyzing the linux kernel 6.4-rc7 release. The tentative patch fix the potential deadlock by spin_lock_irqsave(). Signed-off-by: Chengfeng Ye --- drivers/watchdog/s3c2410_wdt.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/watchdog/s3c2410_wdt.c b/drivers/watchdog/s3c2410_wdt.c index 95416a9bdd4b..2dfc0d6a3004 100644 --- a/drivers/watchdog/s3c2410_wdt.c +++ b/drivers/watchdog/s3c2410_wdt.c @@ -399,10 +399,11 @@ static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt) static int s3c2410wdt_stop(struct watchdog_device *wdd) { struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd); + unsigned long flags; - spin_lock(&wdt->lock); + spin_lock_irqsave(&wdt->lock, flags); __s3c2410wdt_stop(wdt); - spin_unlock(&wdt->lock); + spin_unlock_irqrestore(&wdt->lock, flags); return 0; } @@ -411,8 +412,9 @@ static int s3c2410wdt_start(struct watchdog_device *wdd) { unsigned long wtcon; struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd); + unsigned long flags; - spin_lock(&wdt->lock); + spin_lock_irqsave(&wdt->lock, flags); __s3c2410wdt_stop(wdt); @@ -433,7 +435,7 @@ static int s3c2410wdt_start(struct watchdog_device *wdd) writel(wdt->count, wdt->reg_base + S3C2410_WTDAT); writel(wdt->count, wdt->reg_base + S3C2410_WTCNT); writel(wtcon, wdt->reg_base + S3C2410_WTCON); - spin_unlock(&wdt->lock); + spin_unlock_irqrestore(&wdt->lock, flags); return 0; }