Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexey Charkov" <alchark@flipper.net>
Cc: linux-gpio@vger.kernel.org
Subject: Re: [PATCH] gpio: pca953x: Re-check level-triggered lines that stay active
Date: Mon, 30 Nov 2026 10:29:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-240860@kernel.org> (raw)
In-Reply-To: <20261130-pca953x-level-reenable-v1-1-a989d2505da4@flipper.net>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] gpio: pca953x: deadlock with disable_irq() on irq_wake_thread()
- [Medium] gpio: pca953x: spurious recheck in pca953x_irq_set_type()
  when disabled
--

commit 8dd2b0c1488595f3c519717f339414a171ce7d99
Author: Alexey Charkov <alchark@flipper.net>

gpio: pca953x: Re-check level-triggered lines that stay active

This commit wakes the expander's IRQ thread to re-scan input registers
after handling a level-triggered interrupt, enabling one, or switching an
enabled interrupt to level triggering.

> diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
> index 270cea8b4b492..e8ff948f392ca 100644
> --- a/drivers/gpio/gpio-pca953x.c
> +++ b/drivers/gpio/gpio-pca953x.c
[ ... ]
> @@ -899,6 +913,15 @@ static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
>  		pca953x_gpio_direction_input_unlocked(&chip->gpio_chip, level);
>  
>  out:
> +	/*
> +	 * The chip only interrupts on input changes, so a level-triggered line
> +	 * that is already active when enabled would go unnoticed.
> +	 */
> +	if (chip->irq_level_recheck) {
> +		chip->irq_level_recheck = false;
> +		irq_wake_thread(chip->client->irq, chip);
> +	}
> +
>  	mutex_unlock(&chip->irq_lock);
>  }
>  
> @@ -919,6 +942,9 @@ static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
>  	assign_bit(hwirq, chip->irq_trig_level_low, type & IRQ_TYPE_LEVEL_LOW);
>  	assign_bit(hwirq, chip->irq_trig_level_high, type & IRQ_TYPE_LEVEL_HIGH);
>  
> +	if ((type & IRQ_TYPE_LEVEL_MASK) && test_bit(hwirq, chip->irq_mask))
> +		chip->irq_level_recheck = true;
> +
>  	return 0;
>  }

[Severity: Medium]
Does checking test_bit(hwirq, chip->irq_mask) here wake the IRQ thread
even when the interrupt is disabled?

Because pca953x_irq_chip does not implement .irq_disable, disable_irq()
disables the line lazily without calling pca953x_irq_mask(), leaving
hwirq set in chip->irq_mask.  Calling irq_set_irq_type() on a lazily
disabled line therefore sets chip->irq_level_recheck and triggers an I2C
input scan (clearing the hardware interrupt condition on the expander and
updating chip->irq_stat) while the consumer still has the interrupt
disabled.  Should this check !irqd_irq_disabled(d) instead?

[ ... ]

> @@ -1055,9 +1083,25 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid)
>  
>  			handle_nested_irq(nested_irq);
>  			ret = 1;
> +
> +			/*
> +			 * The chip interrupts again only once an input changes,
> +			 * so a level still active after its handler, as when a
> +			 * new event came in while the handler cleared the last
> +			 * one, would go unnoticed: read the inputs again. Not
> +			 * while the consumer keeps the interrupt disabled, which
> +			 * would only spin, as enabling it re-checks anyway.
> +			 */
> +			if ((test_bit(level, chip->irq_trig_level_low) ||
> +			     test_bit(level, chip->irq_trig_level_high)) &&
> +			    !irqd_irq_disabled(irq_get_irq_data(nested_irq)))
> +				recheck = true;
>  		}
>  	}
>  
> +	if (recheck)
> +		irq_wake_thread(chip->client->irq, chip);
> +
>  	return IRQ_RETVAL(ret);
>  }

[Severity: High]
Can calling irq_wake_thread(chip->client->irq, chip) deadlock with
pca953x_save_context() during suspend?

pca953x_save_context() holds chip->i2c_lock while calling
disable_irq(chip->client->irq):

drivers/gpio/gpio-pca953x.c:pca953x_save_context() {
	guard(mutex)(&chip->i2c_lock);

	/* Disable IRQ to prevent early triggering while regmap "cache only" is on */
	if (chip->client->irq > 0)
		disable_irq(chip->client->irq);
	regcache_cache_only(chip->regmap, true);
}

Neither irq_wake_thread() nor __irq_wake_thread() checks whether
chip->client->irq has IRQD_IRQ_DISABLED set, so waking the parent IRQ
thread increments desc->threads_active and schedules another run of
pca953x_irq_handler():

CPU0                              CPU1 (irq/client->irq)
pca953x_irq_handler()
  pca953x_irq_pending()
    lock(&chip->i2c_lock)
    unlock(&chip->i2c_lock)
                                  pca953x_save_context()
                                    lock(&chip->i2c_lock)
                                    disable_irq(chip->client->irq)
                                      __disable_irq_nosync()
                                      synchronize_irq()
                                        /* waits for threads_active == 0 */
  handle_nested_irq(nested_irq)
  irq_wake_thread(client->irq, chip)
    /* sets IRQTF_RUNTHREAD,
       increments threads_active */
pca953x_irq_handler()
  pca953x_irq_pending()
    lock(&chip->i2c_lock)         <-- deadlock

In addition, pca953x_irq_bus_sync_unlock() can call
irq_wake_thread(chip->client->irq, chip) after pca953x_save_context() has
disabled chip->client->irq and placed regmap into cache-only mode.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-pca953x-level-reenable-v1-1-a989d2505da4@flipper.net?part=1

  reply	other threads:[~2026-11-30 10:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 10:10 [PATCH] gpio: pca953x: Re-check level-triggered lines that stay active Alexey Charkov
2026-11-30 10:29 ` sashiko-bot [this message]
2026-11-30 10:40   ` Alexey Charkov

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-240860@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alchark@flipper.net \
    --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