Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Francesco Lavra <flavra@baylibre.com>
To: Linus Walleij <linusw@kernel.org>,
	Dmitry Mastykin <mastichi@gmail.com>,
	linux-gpio@vger.kernel.org
Subject: [PATCH] pinctrl: mcp23s08: Protect temporary masking from concurrent enable/disable
Date: Fri, 27 Nov 2026 11:53:46 +0100	[thread overview]
Message-ID: <20261127105346.729300-1-flavra@baylibre.com> (raw)

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


                 reply	other threads:[~2026-11-27 10:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127105346.729300-1-flavra@baylibre.com \
    --to=flavra@baylibre.com \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=mastichi@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox