* [PATCH] gpio: pca953x: Re-check level-triggered lines that stay active
@ 2026-11-30 10:10 Alexey Charkov
2026-11-30 10:29 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: Alexey Charkov @ 2026-11-30 10:10 UTC (permalink / raw)
To: Linus Walleij, Bartosz Golaszewski, Potin Lai
Cc: linux-gpio, linux-kernel, Alexey Charkov
The expanders only interrupt when an input changes from the value last
read, so they cannot express a level that is still active once its
handler has run, or once its consumer re-enables the interrupt. That is
the case whenever a new event comes in before the handler, or the
deferred work of a consumer that disables its interrupt meanwhile, has
cleared the last one. No input changes, the expander stays quiet, and the
event sits unnoticed until an unrelated line on the same expander happens
to change.
On a board with a FUSB302 behind a TCA6416 this loses the source's Accept
in most USB PD negotiations, so the sink keeps timing out and issuing hard
resets for tens of seconds before it gets a contract. A FUSB302 driver
that handles its interrupt in the IRQ thread rather than in deferred work
stops getting interrupts within seconds of a burst of USB PD messages
instead: its RX FIFO fills up and the chip stops acknowledging them.
Wake the expander's interrupt thread, so that it reads the inputs and
handles the line if it is still active, as a level-sensitive interrupt
controller would, once a level-triggered interrupt has been handled, and
when one is enabled, or switched to level triggering while enabled. Skip
an interrupt the consumer keeps disabled after its handler, which would
only spin until enabling it re-checks anyway. Do the scan in that thread
rather than in the caller, so nested handlers stay serialized and never
run under the locks of whoever enabled the interrupt. Hook .irq_enable
rather than .irq_unmask: the core disables interrupts lazily without
masking them, so re-enabling one does not unmask it.
Fixes: 417b0f8d08f8 ("gpio: pca953x: Add support for level-triggered interrupts")
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/gpio/gpio-pca953x.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
index 270cea8b4b49..e8ff948f392c 100644
--- a/drivers/gpio/gpio-pca953x.c
+++ b/drivers/gpio/gpio-pca953x.c
@@ -232,6 +232,7 @@ struct pca953x_chip {
DECLARE_BITMAP(irq_trig_fall, MAX_LINE);
DECLARE_BITMAP(irq_trig_level_high, MAX_LINE);
DECLARE_BITMAP(irq_trig_level_low, MAX_LINE);
+ bool irq_level_recheck;
#endif
atomic_t wakeup_path;
@@ -834,6 +835,19 @@ static void pca953x_irq_unmask(struct irq_data *d)
set_bit(hwirq, chip->irq_mask);
}
+static void pca953x_irq_enable(struct irq_data *d)
+{
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+ struct pca953x_chip *chip = gpiochip_get_data(gc);
+ irq_hw_number_t hwirq = irqd_to_hwirq(d);
+
+ pca953x_irq_unmask(d);
+
+ if (test_bit(hwirq, chip->irq_trig_level_low) ||
+ test_bit(hwirq, chip->irq_trig_level_high))
+ chip->irq_level_recheck = true;
+}
+
static int pca953x_irq_set_wake(struct irq_data *d, unsigned int on)
{
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
@@ -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;
}
@@ -946,6 +972,7 @@ static void pca953x_irq_print_chip(struct irq_data *data, struct seq_file *p)
static const struct irq_chip pca953x_irq_chip = {
.irq_mask = pca953x_irq_mask,
.irq_unmask = pca953x_irq_unmask,
+ .irq_enable = pca953x_irq_enable,
.irq_set_wake = pca953x_irq_set_wake,
.irq_bus_lock = pca953x_irq_bus_lock,
.irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
@@ -1035,6 +1062,7 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid)
struct pca953x_chip *chip = devid;
struct gpio_chip *gc = &chip->gpio_chip;
DECLARE_BITMAP(pending, MAX_LINE);
+ bool recheck = false;
int level;
bool ret;
@@ -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);
}
---
base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d
change-id: 20261015-pca953x-level-reenable-aeaa863e255b
Best regards,
--
Alexey Charkov <alchark@flipper.net>
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] gpio: pca953x: Re-check level-triggered lines that stay active
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
2026-11-30 10:40 ` Alexey Charkov
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-11-30 10:29 UTC (permalink / raw)
To: Alexey Charkov; +Cc: linux-gpio
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
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] gpio: pca953x: Re-check level-triggered lines that stay active
2026-11-30 10:29 ` sashiko-bot
@ 2026-11-30 10:40 ` Alexey Charkov
0 siblings, 0 replies; 3+ messages in thread
From: Alexey Charkov @ 2026-11-30 10:40 UTC (permalink / raw)
To: sashiko-reviews; +Cc: linux-gpio
On Mon, Nov 30, 2026 at 2:29 PM <sashiko-bot@kernel.org> wrote:
>
> 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?
Valid, albeit minor IMO. Just another I2C transaction. I can switch to
!irqd_irq_disabled(d) in v2, though.
> [ ... ]
>
> > @@ -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.
Valid, I'll fix in v2.
Best regards,
Alexey
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 10:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2026-11-30 10:40 ` Alexey Charkov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox