Re: [PATCH 11/12] pinctrl: stm32: Protect irqmux restore with hwspinlock during resume

From: Lee Jones

Date: Thu Dec 03 2026 - 03:18:50 EST


> At 2026-12-02 17:30:05, "Lee Jones" <lee@xxxxxxxxxx> wrote:
> >Four IRQ lines share each 32-bit EXTI mux register in SYSCFG, and on
> >heterogeneous multi-core SoCs (such as STM32MP1) those registers are
> >shared with a remote coprocessor. While stm32_gpio_domain_alloc()
> >serialises regmap_field_write(pctl->irqmux[...]) with both
> >pctl->irqmux_lock and pctl->hwlock, stm32_pinctrl_restore_gpio_regs()
> >updates pctl->irqmux[offset] during resume without acquiring either
> >lock, risking a read-modify-write race with the coprocessor.
> >
> >Acquire pctl->irqmux_lock and pctl->hwlock around the irqmux register
> >restore in stm32_pinctrl_restore_gpio_regs().
> >
> >Fixes: e2f3cf18c3e2 ("pinctrl: stm32: add suspend/resume management")
> >Closes: https://sashiko.dev/#/bug/linux-f428a25e-342d-4e81-bdba-0948cff6c22d
> >Cc: stable@xxxxxxxxxxxxxxx
> >Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
> >---
> > drivers/pinctrl/stm32/pinctrl-stm32.c | 20 +++++++++++++++++++-
> > 1 file changed, 19 insertions(+), 1 deletion(-)
> >
> >diff --git a/drivers/pinctrl/stm32/pinctrl-stm32.c b/drivers/pinctrl/stm32/pinctrl-stm32.c
> >index 01c8a646d9a0..897da5fafaf4 100644
> >--- a/drivers/pinctrl/stm32/pinctrl-stm32.c
> >+++ b/drivers/pinctrl/stm32/pinctrl-stm32.c
> >@@ -2094,9 +2094,27 @@ static int __maybe_unused stm32_pinctrl_restore_gpio_regs(
> > return ret;
> > }
> >
> >- if (pin_is_irq)
> >+ if (pin_is_irq) {
> >+ unsigned long flags;
> >+
> >+ spin_lock_irqsave(&pctl->irqmux_lock, flags);
> >+ if (pctl->hwlock) {
> >+ ret = hwspin_lock_timeout_in_atomic(pctl->hwlock,
> >+ HWSPNLCK_TIMEOUT_MS);
> >+ if (ret) {
> >+ dev_err(pctl->dev, "Can't get hwspinlock\n");
> >+ spin_unlock_irqrestore(&pctl->irqmux_lock, flags);
> >+ return ret;
> >+ }
> >+ }
> >+
> > regmap_field_write(pctl->irqmux[offset], bank->bank_ioport_nr);
> >
> >+ if (pctl->hwlock)
> >+ hwspin_unlock_in_atomic(pctl->hwlock);
> >+ spin_unlock_irqrestore(&pctl->irqmux_lock, flags);
> >+ }
> >+
> > return 0;
> > }
> >
> >--
>
> Hi Lee,
>
> Can we do this without holding `&pctl->irqmux_lock` ? since `pctl->irqmux_map` is not touched.

No, I don't think so. For two reasons.

1. hwspin_lock_timeout_in_atomic() and hwspin_unlock_in_atomic() do not
acquire the 'hwlock->lock' and do not disable preemption or interrupts
themselves. As their name implies, they require the caller to already
be in an atomic context so that the CPU cannot be preempted or
interrupted whilst holding the hardware spinlock (which could otherwise
stall the remote coprocessor past timeout).

2. In stm32_gpio_domain_alloc(), 'pctl->irqmux_lock' is the local lock
used to serialise CPUs on the Linux side before acquiring 'pctl->hwlock'
and calling regmap_field_write(). Acquiring 'pctl->irqmux_lock' here
maintains the exact same locking strategy and prevents two local CPUs
from racing on the same 'pctl->hwlock' without its internal spinlock.

> Reviewed-by: Ju Nan <junan76@xxxxxxx>

Thank you.

--
Lee Jones