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

* Re: [PATCH] pinctrl: mcp23s08: Protect temporary masking from concurrent enable/disable
  2026-11-27 10:53 [PATCH] pinctrl: mcp23s08: Protect temporary masking from concurrent enable/disable Francesco Lavra
@ 2026-11-27 11:06 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-27 11:06 UTC (permalink / raw)
  To: Francesco Lavra, linux-gpio

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] pinctrl: mcp23s08: unlocked read of mcp->irq_enable in
  mcp23s08_irq()
--

commit 0d9b07d612ce466ee6965e986ffa3c3137b81336
Author: Francesco Lavra <flavra@baylibre.com>

pinctrl: mcp23s08: Protect temporary masking from concurrent enable/disable

This commit adds an irq_enable field to struct mcp23s08 to track enabled
interrupts under mcp->lock, replacing the local gpinten snapshot in
mcp23s08_irq() so that unmasking after level-interrupt handling does not
overwrite concurrent mask or unmask updates.

> diff --git a/drivers/pinctrl/pinctrl-mcp23s08.c b/drivers/pinctrl/pinctrl-mcp23s08.c
> index cc668f84bf8bc..856cba4fa5b9f 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) {

[Severity: Medium]
Does reading mcp->irq_enable into enabled_interrupts after releasing
mcp->lock introduce a data race with mcp23s08_irq_mask() and
mcp23s08_irq_unmask()?

Previously, gpinten was read while holding mcp->lock and copied to
enabled_interrupts after unlocking.  Now mcp->irq_enable is read after
mutex_unlock(&mcp->lock):

drivers/pinctrl/pinctrl-mcp23s08.c:mcp23s08_irq() {
    ...
	gpio_orig = mcp->cached_gpio;
	mcp->cached_gpio = gpio;
	mutex_unlock(&mcp->lock);

	dev_dbg(mcp->chip.parent,
		"intcap 0x%04X intf 0x%04X gpio_orig 0x%04X gpio 0x%04X\n",
		intcap, intf, gpio_orig, gpio);

	enabled_interrupts = mcp->irq_enable;
    ...
}

If another CPU unmasks a pin right after mcp23s08_irq() drops mcp->lock,
mcp23s08_irq() can observe the new bit in enabled_interrupts and evaluate
that pin against the intf, intcon, defval, intcap, gpio, and gpio_orig
registers sampled before the pin's interrupt was configured and enabled:

CPU0                              CPU1
mcp23s08_irq()
  mutex_lock(&mcp->lock)
  mcp_read(MCP_INTF / MCP_GPIO / ...)
  mutex_unlock(&mcp->lock)
                                  mcp23s08_irq_bus_lock()
                                    mutex_lock(&mcp->lock)
                                  mcp23s08_irq_unmask()
                                    mcp->irq_enable |= BIT(pos)
  enabled_interrupts = mcp->irq_enable
  for_each_set_bit(i, &enabled_interrupts, ...)
    handle_nested_irq(child_irq)  <-- spurious IRQ on newly unmasked pin

Assigning enabled_interrupts = mcp->irq_enable before
mutex_unlock(&mcp->lock) would keep all accesses under mcp->lock and
preserve a consistent snapshot.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127105346.729300-1-flavra@baylibre.com?part=1

^ permalink raw reply	[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