diff mbox

[v2,7/9] gpio: 104-idi-48: make use of raw_spinlock variants

Message ID a9b84439c28b96a9b6a4cbfca85f1e13a457d25d.1490135047.git.julia@ni.com
State New
Headers show

Commit Message

Julia Cartwright March 21, 2017, 10:43 p.m. UTC
The 104-idi-48 gpio driver currently implements an irq_chip for handling
interrupts; due to how irq_chip handling is done, it's necessary for the
irq_chip methods to be invoked from hardirq context, even on a a
real-time kernel.  Because the spinlock_t type becomes a "sleeping"
spinlock w/ RT kernels, it is not suitable to be used with irq_chips.

A quick audit of the operations under the lock reveal that they do only
minimal, bounded work, and are therefore safe to do under a raw spinlock.

Signed-off-by: Julia Cartwright <julia@ni.com>
---
New patch as of v2 of series.

 drivers/gpio/gpio-104-idi-48.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

Comments

William Breathitt Gray March 22, 2017, 12:44 p.m. UTC | #1
On Tue, Mar 21, 2017 at 05:43:07PM -0500, Julia Cartwright wrote:
>The 104-idi-48 gpio driver currently implements an irq_chip for handling
>interrupts; due to how irq_chip handling is done, it's necessary for the
>irq_chip methods to be invoked from hardirq context, even on a a
>real-time kernel.  Because the spinlock_t type becomes a "sleeping"
>spinlock w/ RT kernels, it is not suitable to be used with irq_chips.
>
>A quick audit of the operations under the lock reveal that they do only
>minimal, bounded work, and are therefore safe to do under a raw spinlock.
>
>Signed-off-by: Julia Cartwright <julia@ni.com>

Hi Julia,

This driver also uses a second spinlock_t, called ack_lock, to prevent
reentrance into the idi_48_irq_handler function. Should ack_lock also be
implemented as a raw_spinlock_t?

Thanks,

William Breathitt Gray

>---
>New patch as of v2 of series.
>
> drivers/gpio/gpio-104-idi-48.c | 18 ++++++++++--------
> 1 file changed, 10 insertions(+), 8 deletions(-)
>
>diff --git a/drivers/gpio/gpio-104-idi-48.c b/drivers/gpio/gpio-104-idi-48.c
>index 568375a7ebc2..337c048168d8 100644
>--- a/drivers/gpio/gpio-104-idi-48.c
>+++ b/drivers/gpio/gpio-104-idi-48.c
>@@ -51,7 +51,7 @@ MODULE_PARM_DESC(irq, "ACCES 104-IDI-48 interrupt line numbers");
>  */
> struct idi_48_gpio {
> 	struct gpio_chip chip;
>-	spinlock_t lock;
>+	raw_spinlock_t lock;
> 	spinlock_t ack_lock;
> 	unsigned char irq_mask[6];
> 	unsigned base;
>@@ -112,11 +112,12 @@ static void idi_48_irq_mask(struct irq_data *data)
> 			if (!idi48gpio->irq_mask[boundary]) {
> 				idi48gpio->cos_enb &= ~BIT(boundary);
> 
>-				spin_lock_irqsave(&idi48gpio->lock, flags);
>+				raw_spin_lock_irqsave(&idi48gpio->lock, flags);
> 
> 				outb(idi48gpio->cos_enb, idi48gpio->base + 7);
> 
>-				spin_unlock_irqrestore(&idi48gpio->lock, flags);
>+				raw_spin_unlock_irqrestore(&idi48gpio->lock,
>+						           flags);
> 			}
> 
> 			return;
>@@ -145,11 +146,12 @@ static void idi_48_irq_unmask(struct irq_data *data)
> 			if (!prev_irq_mask) {
> 				idi48gpio->cos_enb |= BIT(boundary);
> 
>-				spin_lock_irqsave(&idi48gpio->lock, flags);
>+				raw_spin_lock_irqsave(&idi48gpio->lock, flags);
> 
> 				outb(idi48gpio->cos_enb, idi48gpio->base + 7);
> 
>-				spin_unlock_irqrestore(&idi48gpio->lock, flags);
>+				raw_spin_unlock_irqrestore(&idi48gpio->lock,
>+						           flags);
> 			}
> 
> 			return;
>@@ -186,11 +188,11 @@ static irqreturn_t idi_48_irq_handler(int irq, void *dev_id)
> 
> 	spin_lock(&idi48gpio->ack_lock);
> 
>-	spin_lock(&idi48gpio->lock);
>+	raw_spin_lock(&idi48gpio->lock);
> 
> 	cos_status = inb(idi48gpio->base + 7);
> 
>-	spin_unlock(&idi48gpio->lock);
>+	raw_spin_unlock(&idi48gpio->lock);
> 
> 	/* IRQ Status (bit 6) is active low (0 = IRQ generated by device) */
> 	if (cos_status & BIT(6)) {
>@@ -256,7 +258,7 @@ static int idi_48_probe(struct device *dev, unsigned int id)
> 	idi48gpio->chip.get = idi_48_gpio_get;
> 	idi48gpio->base = base[id];
> 
>-	spin_lock_init(&idi48gpio->lock);
>+	raw_spin_lock_init(&idi48gpio->lock);
> 	spin_lock_init(&idi48gpio->ack_lock);
> 
> 	err = devm_gpiochip_add_data(dev, &idi48gpio->chip, idi48gpio);
>-- 
>2.12.0
>
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Julia Cartwright March 22, 2017, 4:11 p.m. UTC | #2
On Wed, Mar 22, 2017 at 08:44:14AM -0400, William Breathitt Gray wrote:
> On Tue, Mar 21, 2017 at 05:43:07PM -0500, Julia Cartwright wrote:
> >The 104-idi-48 gpio driver currently implements an irq_chip for handling
> >interrupts; due to how irq_chip handling is done, it's necessary for the
> >irq_chip methods to be invoked from hardirq context, even on a a
> >real-time kernel.  Because the spinlock_t type becomes a "sleeping"
> >spinlock w/ RT kernels, it is not suitable to be used with irq_chips.
> >
> >A quick audit of the operations under the lock reveal that they do only
> >minimal, bounded work, and are therefore safe to do under a raw spinlock.
> >
> >Signed-off-by: Julia Cartwright <julia@ni.com>
> 
> Hi Julia,
> 
> This driver also uses a second spinlock_t, called ack_lock, to prevent
> reentrance into the idi_48_irq_handler function. Should ack_lock also be
> implemented as a raw_spinlock_t?

I saw this lock, and I don't even understand it's purpose.

However, I think I convinced myself that it's harmless.  Why?  It's only
ever acquired in a handler registered with request_irq(), which, on RT,
is invoked in a context which can sleep.

Thanks for taking a closer look!

   Julia
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Linus Walleij March 28, 2017, 9:11 a.m. UTC | #3
On Wed, Mar 22, 2017 at 1:44 PM, William Breathitt Gray
<vilhelm.gray@gmail.com> wrote:
> On Tue, Mar 21, 2017 at 05:43:07PM -0500, Julia Cartwright wrote:
>>The 104-idi-48 gpio driver currently implements an irq_chip for handling
>>interrupts; due to how irq_chip handling is done, it's necessary for the
>>irq_chip methods to be invoked from hardirq context, even on a a
>>real-time kernel.  Because the spinlock_t type becomes a "sleeping"
>>spinlock w/ RT kernels, it is not suitable to be used with irq_chips.
>>
>>A quick audit of the operations under the lock reveal that they do only
>>minimal, bounded work, and are therefore safe to do under a raw spinlock.
>>
>>Signed-off-by: Julia Cartwright <julia@ni.com>
>
> Hi Julia,
>
> This driver also uses a second spinlock_t, called ack_lock, to prevent
> reentrance into the idi_48_irq_handler function. Should ack_lock also be
> implemented as a raw_spinlock_t?

Hm, can I apply this one patch or not?

Linus Walleij
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
William Breathitt Gray March 28, 2017, 11:40 a.m. UTC | #4
On Tue, Mar 28, 2017 at 11:11:59AM +0200, Linus Walleij wrote:
>On Wed, Mar 22, 2017 at 1:44 PM, William Breathitt Gray
><vilhelm.gray@gmail.com> wrote:
>> On Tue, Mar 21, 2017 at 05:43:07PM -0500, Julia Cartwright wrote:
>>>The 104-idi-48 gpio driver currently implements an irq_chip for handling
>>>interrupts; due to how irq_chip handling is done, it's necessary for the
>>>irq_chip methods to be invoked from hardirq context, even on a a
>>>real-time kernel.  Because the spinlock_t type becomes a "sleeping"
>>>spinlock w/ RT kernels, it is not suitable to be used with irq_chips.
>>>
>>>A quick audit of the operations under the lock reveal that they do only
>>>minimal, bounded work, and are therefore safe to do under a raw spinlock.
>>>
>>>Signed-off-by: Julia Cartwright <julia@ni.com>
>>
>> Hi Julia,
>>
>> This driver also uses a second spinlock_t, called ack_lock, to prevent
>> reentrance into the idi_48_irq_handler function. Should ack_lock also be
>> implemented as a raw_spinlock_t?
>
>Hm, can I apply this one patch or not?
>
>Linus Walleij

Oops, sorry for missing this reply. Julia is correct that ack_lock does
not need to be implemented as raw_spinlock_t. For reference, ack_lock is
used to prevent a race condition on the device hardware itself related
to how the 104-IDI-48 acknowledges IRQ (check out the commit description
for it for a more in-depth explanation if you're curious).

Long story short: Julia's patch is prefectly acceptable as is.

Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>

William Breathitt Gray
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Linus Walleij March 28, 2017, 12:55 p.m. UTC | #5
On Tue, Mar 21, 2017 at 11:43 PM, Julia Cartwright <julia@ni.com> wrote:

> The 104-idi-48 gpio driver currently implements an irq_chip for handling
> interrupts; due to how irq_chip handling is done, it's necessary for the
> irq_chip methods to be invoked from hardirq context, even on a a
> real-time kernel.  Because the spinlock_t type becomes a "sleeping"
> spinlock w/ RT kernels, it is not suitable to be used with irq_chips.
>
> A quick audit of the operations under the lock reveal that they do only
> minimal, bounded work, and are therefore safe to do under a raw spinlock.
>
> Signed-off-by: Julia Cartwright <julia@ni.com>
> ---
> New patch as of v2 of series.

Patch applied with William's ACK.

Yours,
Linus Walleij
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/gpio/gpio-104-idi-48.c b/drivers/gpio/gpio-104-idi-48.c
index 568375a7ebc2..337c048168d8 100644
--- a/drivers/gpio/gpio-104-idi-48.c
+++ b/drivers/gpio/gpio-104-idi-48.c
@@ -51,7 +51,7 @@  MODULE_PARM_DESC(irq, "ACCES 104-IDI-48 interrupt line numbers");
  */
 struct idi_48_gpio {
 	struct gpio_chip chip;
-	spinlock_t lock;
+	raw_spinlock_t lock;
 	spinlock_t ack_lock;
 	unsigned char irq_mask[6];
 	unsigned base;
@@ -112,11 +112,12 @@  static void idi_48_irq_mask(struct irq_data *data)
 			if (!idi48gpio->irq_mask[boundary]) {
 				idi48gpio->cos_enb &= ~BIT(boundary);
 
-				spin_lock_irqsave(&idi48gpio->lock, flags);
+				raw_spin_lock_irqsave(&idi48gpio->lock, flags);
 
 				outb(idi48gpio->cos_enb, idi48gpio->base + 7);
 
-				spin_unlock_irqrestore(&idi48gpio->lock, flags);
+				raw_spin_unlock_irqrestore(&idi48gpio->lock,
+						           flags);
 			}
 
 			return;
@@ -145,11 +146,12 @@  static void idi_48_irq_unmask(struct irq_data *data)
 			if (!prev_irq_mask) {
 				idi48gpio->cos_enb |= BIT(boundary);
 
-				spin_lock_irqsave(&idi48gpio->lock, flags);
+				raw_spin_lock_irqsave(&idi48gpio->lock, flags);
 
 				outb(idi48gpio->cos_enb, idi48gpio->base + 7);
 
-				spin_unlock_irqrestore(&idi48gpio->lock, flags);
+				raw_spin_unlock_irqrestore(&idi48gpio->lock,
+						           flags);
 			}
 
 			return;
@@ -186,11 +188,11 @@  static irqreturn_t idi_48_irq_handler(int irq, void *dev_id)
 
 	spin_lock(&idi48gpio->ack_lock);
 
-	spin_lock(&idi48gpio->lock);
+	raw_spin_lock(&idi48gpio->lock);
 
 	cos_status = inb(idi48gpio->base + 7);
 
-	spin_unlock(&idi48gpio->lock);
+	raw_spin_unlock(&idi48gpio->lock);
 
 	/* IRQ Status (bit 6) is active low (0 = IRQ generated by device) */
 	if (cos_status & BIT(6)) {
@@ -256,7 +258,7 @@  static int idi_48_probe(struct device *dev, unsigned int id)
 	idi48gpio->chip.get = idi_48_gpio_get;
 	idi48gpio->base = base[id];
 
-	spin_lock_init(&idi48gpio->lock);
+	raw_spin_lock_init(&idi48gpio->lock);
 	spin_lock_init(&idi48gpio->ack_lock);
 
 	err = devm_gpiochip_add_data(dev, &idi48gpio->chip, idi48gpio);