Linux GPIO subsystem development
 help / color / mirror / Atom feed
* [PATCH] pinctrl: mcp23s08: Protect temporary masking from concurrent enable/disable
@ 2026-11-27 10:53 Francesco Lavra
  2026-11-27 11:06 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Francesco Lavra @ 2026-11-27 10:53 UTC (permalink / raw)
  To: Linus Walleij, Dmitry Mastykin, linux-gpio

In mcp23s08_irq(), the code that restores the set of enabled interrupts
after temporary masking can potentially use a stale GPINTEN snapshot.
Although the save/mask and restore each happen under mcp->lock, the handler
releases that mutex before calling the nested handlers and reacquires it
afterwards. During this unlocked interval, an IRQ mask or unmask operation
can acquire the same mutex and change GPINTEN. Writing the original gpinten
here then overwrites that change.
Add a `irq_enable` field to struct mcp23s08, and always use this field
under mcp->lock when modifying the hardware mask; in mcp23s08_irq(), remove
the gpinten local variable which is made redundant by mcp->irq_enable.

Fixes: 7b0671b97f08 ("pinctrl: mcp23s08: Get rid of spurious level interrupts")
Signed-off-by: Francesco Lavra <flavra@baylibre.com>
---
 drivers/pinctrl/pinctrl-mcp23s08.c | 13 ++++++-------
 drivers/pinctrl/pinctrl-mcp23s08.h |  1 +
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/pinctrl/pinctrl-mcp23s08.c b/drivers/pinctrl/pinctrl-mcp23s08.c
index cc668f84bf8b..856cba4fa5b9 100644
--- a/drivers/pinctrl/pinctrl-mcp23s08.c
+++ b/drivers/pinctrl/pinctrl-mcp23s08.c
@@ -362,7 +362,7 @@ mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
 static irqreturn_t mcp23s08_irq(int irq, void *data)
 {
 	struct mcp23s08 *mcp = data;
-	int intcap, intcon, intf, i, gpio, gpio_orig, intcap_mask, defval, gpinten;
+	int intcap, intcon, intf, i, gpio, gpio_orig, intcap_mask, defval;
 	bool need_unmask = false;
 	unsigned long int enabled_interrupts;
 	unsigned int child_irq;
@@ -381,16 +381,13 @@ static irqreturn_t mcp23s08_irq(int irq, void *data)
 	if (mcp_read(mcp, MCP_INTCON, &intcon))
 		goto unlock;
 
-	if (mcp_read(mcp, MCP_GPINTEN, &gpinten))
-		goto unlock;
-
 	if (mcp_read(mcp, MCP_DEFVAL, &defval))
 		goto unlock;
 
 	/* Mask level interrupts to avoid their immediate reactivation after clearing */
 	if (intcon) {
 		need_unmask = true;
-		if (mcp_write(mcp, MCP_GPINTEN, gpinten & ~intcon))
+		if (mcp_write(mcp, MCP_GPINTEN, mcp->irq_enable & ~intcon))
 			goto unlock;
 	}
 
@@ -409,7 +406,7 @@ static irqreturn_t mcp23s08_irq(int irq, void *data)
 		"intcap 0x%04X intf 0x%04X gpio_orig 0x%04X gpio 0x%04X\n",
 		intcap, intf, gpio_orig, gpio);
 
-	enabled_interrupts = gpinten;
+	enabled_interrupts = mcp->irq_enable;
 	for_each_set_bit(i, &enabled_interrupts, mcp->chip.ngpio) {
 		/*
 		 * We must check all of the inputs with enabled interrupts
@@ -468,7 +465,7 @@ static irqreturn_t mcp23s08_irq(int irq, void *data)
 
 unlock:
 	if (need_unmask)
-		if (mcp_write(mcp, MCP_GPINTEN, gpinten))
+		if (mcp_write(mcp, MCP_GPINTEN, mcp->irq_enable))
 			dev_err(mcp->chip.parent, "can't unmask GPINTEN\n");
 
 	mutex_unlock(&mcp->lock);
@@ -481,6 +478,7 @@ static void mcp23s08_irq_mask(struct irq_data *data)
 	struct mcp23s08 *mcp = gpiochip_get_data(gc);
 	unsigned int pos = irqd_to_hwirq(data);
 
+	mcp->irq_enable &= ~BIT(pos);
 	mcp_set_bit(mcp, MCP_GPINTEN, pos, false);
 	gpiochip_disable_irq(gc, pos);
 }
@@ -492,6 +490,7 @@ static void mcp23s08_irq_unmask(struct irq_data *data)
 	unsigned int pos = irqd_to_hwirq(data);
 
 	gpiochip_enable_irq(gc, pos);
+	mcp->irq_enable |= BIT(pos);
 	mcp_set_bit(mcp, MCP_GPINTEN, pos, true);
 }
 
diff --git a/drivers/pinctrl/pinctrl-mcp23s08.h b/drivers/pinctrl/pinctrl-mcp23s08.h
index 105bce9e0f50..a29e67f5df46 100644
--- a/drivers/pinctrl/pinctrl-mcp23s08.h
+++ b/drivers/pinctrl/pinctrl-mcp23s08.h
@@ -37,6 +37,7 @@ struct mcp23s08 {
 
 	u16			irq_rise;
 	u16			irq_fall;
+	u16			irq_enable;
 	int			irq;
 	bool			irq_controller;
 	int			cached_gpio;
-- 
2.39.5


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

end of thread, other threads:[~2026-11-27 11:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 10:53 [PATCH] pinctrl: mcp23s08: Protect temporary masking from concurrent enable/disable Francesco Lavra
2026-11-27 11:06 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox