Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: Ju Nan <junan76@163.com>
Cc: Antonio Borneo <antonio.borneo@foss.st.com>,
	Linus Walleij <linusw@kernel.org>,
	Maxime Coquelin <mcoquelin.stm32@gmail.com>,
	Alexandre Torgue <alexandre.torgue@foss.st.com>,
	linux-gpio@vger.kernel.org,
	linux-stm32@st-md-mailman.stormreply.com,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH 11/12] pinctrl: stm32: Protect irqmux restore with hwspinlock during resume
Date: Thu, 3 Dec 2026 08:18:32 +0000	[thread overview]
Message-ID: <20261203081832.GI98551@kernel.org> (raw)
In-Reply-To: <5b7dec9d.16c0.1a23994d4ed.Coremail.junan76@163.com>

> At 2026-12-02 17:30:05, "Lee Jones" <lee@kernel.org> 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@vger.kernel.org
> >Signed-off-by: Lee Jones <lee@kernel.org>
> >---
> > 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@163.com>

Thank you.

-- 
Lee Jones

  reply	other threads:[~2026-12-03  8:18 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:29 [PATCH 00/12] pinctrl: Fix Critical and High issues reported by Sashiko (batch 3) Lee Jones
2026-12-02  9:29 ` [PATCH 01/12] pinctrl: stm32: Validate npins against STM32_GPIO_PINS_PER_BANK Lee Jones
2026-12-02 16:13   ` Antonio Borneo
2026-12-02  9:29 ` [PATCH 02/12] pinctrl: imx: Validate pin_id in imx_pinctrl_parse_pin_mmio() Lee Jones
2026-12-02  9:50   ` sashiko-bot
2026-12-02 17:17   ` Frank Li
2026-12-02  9:29 ` [PATCH 03/12] pinctrl: single: Protect group and function updates with pctldev->mutex Lee Jones
2026-12-02 14:08   ` Bartosz Golaszewski
2026-12-02  9:29 ` [PATCH 04/12] pinctrl: imx: Propagate DT parsing errors in imx_pinctrl_probe_dt() Lee Jones
2026-12-02 17:20   ` Frank Li
2026-12-02  9:29 ` [PATCH 05/12] pinctrl: tegra: Require at least one register bank in tegra_pinctrl_probe() Lee Jones
2026-12-02  9:30 ` [PATCH 06/12] pinctrl: imx: Serialise MMIO register updates with a spinlock Lee Jones
2026-12-02 17:22   ` Frank Li
2026-12-02 20:08     ` Lee Jones
2026-12-03  4:10       ` Frank Li
2026-12-02  9:30 ` [PATCH 07/12] pinctrl: nuvoton: ma35: Avoid double free of shared pin configs Lee Jones
2026-12-02  9:30 ` [PATCH 08/12] pinctrl: keembay: Check src before calling keembay_find_free_slot() Lee Jones
2026-12-02  9:30 ` [PATCH 09/12] pinctrl: ti: iodelay: Protect generic group addition with pctldev->mutex Lee Jones
2026-12-02  9:30 ` [PATCH 10/12] pinctrl: sunxi: Serialise per-bank regulator request and free Lee Jones
2026-12-02  9:30 ` [PATCH 11/12] pinctrl: stm32: Protect irqmux restore with hwspinlock during resume Lee Jones
2026-12-02 15:28   ` Antonio Borneo
2026-12-03  1:46   ` Ju Nan
2026-12-03  8:18     ` Lee Jones [this message]
2026-12-02  9:30 ` [PATCH 12/12] pinctrl: sx150x: Register threaded IRQ after adding GPIO chip Lee Jones
2026-12-02  9:35   ` Neil Armstrong

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=20261203081832.GI98551@kernel.org \
    --to=lee@kernel.org \
    --cc=alexandre.torgue@foss.st.com \
    --cc=antonio.borneo@foss.st.com \
    --cc=junan76@163.com \
    --cc=linusw@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=stable@vger.kernel.org \
    /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