linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] gpio: timberdale: Fix potential deadlock on &tgpio->lock
@ 2023-09-26 10:29 Chengfeng Ye
  2023-09-26 12:58 ` Andy Shevchenko
  2023-09-27  6:52 ` Bartosz Golaszewski
  0 siblings, 2 replies; 4+ messages in thread
From: Chengfeng Ye @ 2023-09-26 10:29 UTC (permalink / raw)
  To: linus.walleij, brgl, andy, alex, aboutphysycs
  Cc: linux-gpio, linux-kernel, Chengfeng Ye

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)
<interrupt>
   --> 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 <dg573847474@gmail.com>
---
 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 bbd9e9191199..fad979797486 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.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] gpio: timberdale: Fix potential deadlock on &tgpio->lock
  2023-09-26 10:29 [PATCH] gpio: timberdale: Fix potential deadlock on &tgpio->lock Chengfeng Ye
@ 2023-09-26 12:58 ` Andy Shevchenko
  2023-09-27  6:52 ` Bartosz Golaszewski
  1 sibling, 0 replies; 4+ messages in thread
From: Andy Shevchenko @ 2023-09-26 12:58 UTC (permalink / raw)
  To: Chengfeng Ye
  Cc: linus.walleij, brgl, alex, aboutphysycs, linux-gpio, linux-kernel

On Tue, Sep 26, 2023 at 10:29:14AM +0000, Chengfeng Ye wrote:
> 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)
> <interrupt>
>    --> 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.

Okay, makes sense.
Reviewed-by: Andy Shevchenko <andy@kernel.org>

Question to the users of this hardware if they ever want to have this IRQ chip
in the RT environment. In that case the locking type needs to be raw.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gpio: timberdale: Fix potential deadlock on &tgpio->lock
  2023-09-26 10:29 [PATCH] gpio: timberdale: Fix potential deadlock on &tgpio->lock Chengfeng Ye
  2023-09-26 12:58 ` Andy Shevchenko
@ 2023-09-27  6:52 ` Bartosz Golaszewski
  2023-09-27  6:54   ` Chengfeng Ye
  1 sibling, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2023-09-27  6:52 UTC (permalink / raw)
  To: Chengfeng Ye
  Cc: linus.walleij, andy, alex, aboutphysycs, linux-gpio, linux-kernel

On Tue, Sep 26, 2023 at 12:29 PM Chengfeng Ye <dg573847474@gmail.com> wrote:
>
> 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)
> <interrupt>
>    --> 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 <dg573847474@gmail.com>
> ---
>  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 bbd9e9191199..fad979797486 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.17.1
>

Applied, thanks!

Bart

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gpio: timberdale: Fix potential deadlock on &tgpio->lock
  2023-09-27  6:52 ` Bartosz Golaszewski
@ 2023-09-27  6:54   ` Chengfeng Ye
  0 siblings, 0 replies; 4+ messages in thread
From: Chengfeng Ye @ 2023-09-27  6:54 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: linus.walleij, andy, alex, aboutphysycs, linux-gpio, linux-kernel

Thanks much for the review!

Best,
Chengfeng

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-09-27  6:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-26 10:29 [PATCH] gpio: timberdale: Fix potential deadlock on &tgpio->lock Chengfeng Ye
2023-09-26 12:58 ` Andy Shevchenko
2023-09-27  6:52 ` Bartosz Golaszewski
2023-09-27  6:54   ` Chengfeng Ye

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).