Skip to contentSkip to conversation

gpio: dwapb: Add robust error handling in interrupt handler

PATCH Superseded Liang Hao 1 participant 2 messages Jul 3 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Liang Hao

author
#1 lore raw

PATCH v2 1/1 gpio: dwapb: Add robust error handling in interrupt handler

In reply to a message not in the archive

The current interrupt handler silently continues if an interrupt handling fails, which may lead to interrupt storms. Add proper error handling to gracefully recover from failed interrupt handling.

When generic_handle_irq() fails, the following recovery actions are taken:
- Write EOI to clear the pending interrupt
- Mask the interrupt to prevent immediate re-triggering
- Disable the interrupt to stop further interrupts on this line

Use scoped_guard(gpio_generic_lock_irqsave) to protect the read- modify-write sequences on shared registers (GPIO_INTMASK and GPIO_INTEN). This prevents race conditions with irq_chip callbacks (dwapb_irq_mask, dwapb_irq_enable, etc.) that access the same registers from different contexts.

These measures prevent the system from being overwhelmed by repeated unhandled interrupts while logging a rate-limited warning for debugging purposes.

---
v1 -> v2:
- Add spinlock protection for register access in error path
- Protect against race with irq_chip callbacks accessing shared registers

---
 drivers/gpio/gpio-dwapb.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
drivers/gpio/gpio-dwapb.c +17 -2
index 7b92b233fafe..dc9676eb1429 100644
@@ -209,8 +209,23 @@ static u32 dwapb_do_irq(struct dwapb_gpio *gpio)
 	for_each_set_bit(hwirq, &irq_status, DWAPB_MAX_GPIOS) {
 		int gpio_irq = irq_find_mapping(gen_gc->gc.irq.domain, hwirq);
 		u32 irq_type = irq_get_trigger_type(gpio_irq);
-
-		generic_handle_irq(gpio_irq);
+		int ret;
+		u32 val_intmask, val_inten;
+
+		ret = generic_handle_irq(gpio_irq);
+		if (ret) {
+			dev_warn_ratelimited(gpio->dev, "Failed to handle irq %d\n", gpio_irq);
+			/* Clear the interrupt */
+			scoped_guard(gpio_generic_lock_irqsave, gen_gc) {
+				/* Clear the interrupt */
+				dwapb_write(gpio, GPIO_PORTA_EOI, BIT(hwirq));
+				val_intmask = dwapb_read(gpio, GPIO_INTMASK);
+				dwapb_write(gpio, GPIO_INTMASK, val_intmask | BIT(hwirq));
+				val_inten = dwapb_read(gpio, GPIO_INTEN);
+				dwapb_write(gpio, GPIO_INTEN, val_inten & ~BIT(hwirq));
+			}
+			continue;
+		}
 
 		if ((irq_type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
 			dwapb_toggle_trigger(gpio, hwirq);
Signature · 1 line
2.50.1 (Apple Git-155)
author
#2 lore raw

PATCH v3 1/1 gpio: dwapb: Add robust error handling in interrupt handler

The current interrupt handler silently continues if an interrupt handling fails, which may lead to interrupt storms. Add proper error handling to gracefully recover from failed interrupt handling.

When generic_handle_irq() fails, the following recovery actions are taken:
- Write EOI to clear the pending interrupt
- Mask the interrupt to prevent immediate re-triggering
- Disable the interrupt to stop further interrupts on this line

Use scoped_guard(gpio_generic_lock_irqsave) to protect the read- modify-write sequences on shared registers (GPIO_INTMASK and GPIO_INTEN). This prevents race conditions with irq_chip callbacks (dwapb_irq_mask, dwapb_irq_enable, etc.) that access the same registers from different contexts.

These measures prevent the system from being overwhelmed by repeated unhandled interrupts while logging a rate-limited warning for debugging purposes.

---
v2 -> v3:
- Remove duplicate comment

v1 -> v2:
- Add spinlock protection for register access in error path
- Protect against race with irq_chip callbacks accessing shared registers

---
 drivers/gpio/gpio-dwapb.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
drivers/gpio/gpio-dwapb.c +17 -2
index 7b92b233fafe..dc9676eb1429 100644
@@ -209,8 +209,23 @@ static u32 dwapb_do_irq(struct dwapb_gpio *gpio)
 	for_each_set_bit(hwirq, &irq_status, DWAPB_MAX_GPIOS) {
 		int gpio_irq = irq_find_mapping(gen_gc->gc.irq.domain, hwirq);
 		u32 irq_type = irq_get_trigger_type(gpio_irq);
-
-		generic_handle_irq(gpio_irq);
+		int ret;
+		u32 val_intmask, val_inten;
+
+		ret = generic_handle_irq(gpio_irq);
+		if (ret) {
+			dev_warn_ratelimited(gpio->dev, "Failed to handle irq %d\n", gpio_irq);
+			/* Clear the interrupt */
+			scoped_guard(gpio_generic_lock_irqsave, gen_gc) {
+				/* Clear the interrupt */
+				dwapb_write(gpio, GPIO_PORTA_EOI, BIT(hwirq));
+				val_intmask = dwapb_read(gpio, GPIO_INTMASK);
+				dwapb_write(gpio, GPIO_INTMASK, val_intmask | BIT(hwirq));
+				val_inten = dwapb_read(gpio, GPIO_INTEN);
+				dwapb_write(gpio, GPIO_INTEN, val_inten & ~BIT(hwirq));
+			}
+			continue;
+		}
 
 		if ((irq_type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
 			dwapb_toggle_trigger(gpio, hwirq);
Signature · 1 line
2.50.1 (Apple Git-155)