Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Francesco Lavra" <flavra@baylibre.com>, linux-gpio@vger.kernel.org
Subject: Re: [PATCH] pinctrl: mcp23s08: Protect temporary masking from concurrent enable/disable
Date: Fri, 27 Nov 2026 11:06:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-237215@kernel.org> (raw)
In-Reply-To: <20261127105346.729300-1-flavra@baylibre.com>

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

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

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=sashiko-outbox-237215@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=flavra@baylibre.com \
    --cc=linux-gpio@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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