From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ABF15E95A85 for ; Sun, 8 Oct 2023 00:51:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234346AbjJHAvM (ORCPT ); Sat, 7 Oct 2023 20:51:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234458AbjJHAus (ORCPT ); Sat, 7 Oct 2023 20:50:48 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8293CD48; Sat, 7 Oct 2023 17:50:07 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E760C433CC; Sun, 8 Oct 2023 00:50:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696726203; bh=ZxLTp2cbLb9eo1xQ5FrKJgFWIe79ItBd51kd3aNbBl8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d5TC818npIwT/2pHDAqnqjIZnXiYQa49wvh4p5VhmHACilT5kBfJevHSkwqXQC92W /Nd9cv6RbDxbKtRrPVT6EazZoIa7yT/bR4ACNaCbnXzh9j1c9U4cTWKKJ65i8Ch1WE yTNkjl6esecyZoeHL4jR60AD/a/C8rdC7nZUd92se/qw/yh4sdtmlXtt10F8CrKFpT SNy97J/xwgBJMk5f8ACl9EHgf31ueEX8oD3hzg260WzsJ8AcbRPOojbovxY//F8b3/ W2Y51rW/oAlhXxG3QGAKY7u+rG0x88vlv/aR11LOuXix6HJqiiUBlTHIBn88b40sw5 yw+qsLeI3v4Qw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Chengfeng Ye , Andy Shevchenko , Bartosz Golaszewski , Sasha Levin , linus.walleij@linaro.org, brgl@bgdev.pl, linux-gpio@vger.kernel.org Subject: [PATCH AUTOSEL 5.15 07/10] gpio: timberdale: Fix potential deadlock on &tgpio->lock Date: Sat, 7 Oct 2023 20:49:46 -0400 Message-Id: <20231008004950.3768189-7-sashal@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231008004950.3768189-1-sashal@kernel.org> References: <20231008004950.3768189-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 5.15.134 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Chengfeng Ye [ Upstream commit 9e8bc2dda5a7a8e2babc9975f4b11c9a6196e490 ] As timbgpio_irq_enable()/timbgpio_irq_disable() callback could be executed under irq context, it could introduce double locks on &tgpio->lock if it preempts other execution units requiring the same locks. timbgpio_gpio_set() --> timbgpio_update_bit() --> spin_lock(&tgpio->lock) --> timbgpio_irq_disable() --> spin_lock_irqsave(&tgpio->lock) This flaw was found by an experimental static analysis tool I am developing for irq-related deadlock. To prevent the potential deadlock, the patch uses spin_lock_irqsave() on &tgpio->lock inside timbgpio_gpio_set() to prevent the possible deadlock scenario. Signed-off-by: Chengfeng Ye Reviewed-by: Andy Shevchenko Signed-off-by: Bartosz Golaszewski Signed-off-by: Sasha Levin --- drivers/gpio/gpio-timberdale.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/gpio/gpio-timberdale.c b/drivers/gpio/gpio-timberdale.c index de14949a3fe5a..92c1f2baa4bff 100644 --- a/drivers/gpio/gpio-timberdale.c +++ b/drivers/gpio/gpio-timberdale.c @@ -43,9 +43,10 @@ static int timbgpio_update_bit(struct gpio_chip *gpio, unsigned index, unsigned offset, bool enabled) { struct timbgpio *tgpio = gpiochip_get_data(gpio); + unsigned long flags; u32 reg; - spin_lock(&tgpio->lock); + spin_lock_irqsave(&tgpio->lock, flags); reg = ioread32(tgpio->membase + offset); if (enabled) @@ -54,7 +55,7 @@ static int timbgpio_update_bit(struct gpio_chip *gpio, unsigned index, reg &= ~(1 << index); iowrite32(reg, tgpio->membase + offset); - spin_unlock(&tgpio->lock); + spin_unlock_irqrestore(&tgpio->lock, flags); return 0; } -- 2.40.1