linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] gpio/grgpio: fix deadlock in grgpio_irq_unmap()
@ 2015-08-17  8:23 Alexandre Courbot
  2015-08-17  8:56 ` Linus Walleij
  0 siblings, 1 reply; 2+ messages in thread
From: Alexandre Courbot @ 2015-08-17  8:23 UTC (permalink / raw)
  To: Linus Walleij
  Cc: linux-gpio, linux-kernel, ldv-project, gnurou, Alexandre Courbot,
	Alexey Khoroshilov, Andreas Larsson

As reported by Alexey Khoroshilov:

    grgpio_irq_unmap() code looks quite suspicious regarding usage of
    priv->bgc.lock spinlock.

    It locks the spinlock in line 310:

	spin_lock_irqsave(&priv->bgc.lock, flags);

    and then it can call grgpio_set_imask() in line 317:

	grgpio_set_imask(priv, i, 0);

    But grgpio_set_imask() unconditionally locks the spinlock by itself.

Fix this by moving the spinlock acquisition outside of
grgpio_set_imask().

Found by Linux Driver Verification project (linuxtesting.org).

Reported-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
CC: Alexey Khoroshilov <khoroshilov@ispras.ru>
CC: Andreas Larsson <andreas@gaisler.com>
---
 drivers/gpio/gpio-grgpio.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/gpio/gpio-grgpio.c b/drivers/gpio/gpio-grgpio.c
index 77053d61466e..801423fe8143 100644
--- a/drivers/gpio/gpio-grgpio.c
+++ b/drivers/gpio/gpio-grgpio.c
@@ -104,17 +104,12 @@ static void grgpio_set_imask(struct grgpio_priv *priv, unsigned int offset,
 {
 	struct bgpio_chip *bgc = &priv->bgc;
 	unsigned long mask = bgc->pin2mask(bgc, offset);
-	unsigned long flags;
-
-	spin_lock_irqsave(&bgc->lock, flags);
 
 	if (val)
 		priv->imask |= mask;
 	else
 		priv->imask &= ~mask;
 	bgc->write_reg(priv->regs + GRGPIO_IMASK, priv->imask);
-
-	spin_unlock_irqrestore(&bgc->lock, flags);
 }
 
 static int grgpio_to_irq(struct gpio_chip *gc, unsigned offset)
@@ -180,16 +175,26 @@ static void grgpio_irq_mask(struct irq_data *d)
 {
 	struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
 	int offset = d->hwirq;
+	unsigned long flags;
+
+	spin_lock_irqsave(&priv->bgc.lock, flags);
 
 	grgpio_set_imask(priv, offset, 0);
+
+	spin_unlock_irqrestore(&priv->bgc.lock, flags);
 }
 
 static void grgpio_irq_unmask(struct irq_data *d)
 {
 	struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
 	int offset = d->hwirq;
+	unsigned long flags;
+
+	spin_lock_irqsave(&priv->bgc.lock, flags);
 
 	grgpio_set_imask(priv, offset, 1);
+
+	spin_unlock_irqrestore(&priv->bgc.lock, flags);
 }
 
 static struct irq_chip grgpio_irq_chip = {
-- 
2.5.0


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

* Re: [PATCH] gpio/grgpio: fix deadlock in grgpio_irq_unmap()
  2015-08-17  8:23 [PATCH] gpio/grgpio: fix deadlock in grgpio_irq_unmap() Alexandre Courbot
@ 2015-08-17  8:56 ` Linus Walleij
  0 siblings, 0 replies; 2+ messages in thread
From: Linus Walleij @ 2015-08-17  8:56 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
	ldv-project, Alexandre Courbot, Alexey Khoroshilov,
	Andreas Larsson

On Mon, Aug 17, 2015 at 10:23 AM, Alexandre Courbot <acourbot@nvidia.com> wrote:

> As reported by Alexey Khoroshilov:
>
>     grgpio_irq_unmap() code looks quite suspicious regarding usage of
>     priv->bgc.lock spinlock.
>
>     It locks the spinlock in line 310:
>
>         spin_lock_irqsave(&priv->bgc.lock, flags);
>
>     and then it can call grgpio_set_imask() in line 317:
>
>         grgpio_set_imask(priv, i, 0);
>
>     But grgpio_set_imask() unconditionally locks the spinlock by itself.
>
> Fix this by moving the spinlock acquisition outside of
> grgpio_set_imask().
>
> Found by Linux Driver Verification project (linuxtesting.org).
>
> Reported-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> CC: Alexey Khoroshilov <khoroshilov@ispras.ru>
> CC: Andreas Larsson <andreas@gaisler.com>

Patch applied.

Yours,
Linus Walleij

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

end of thread, other threads:[~2015-08-17  8:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-17  8:23 [PATCH] gpio/grgpio: fix deadlock in grgpio_irq_unmap() Alexandre Courbot
2015-08-17  8:56 ` Linus Walleij

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).