mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Linus Walleij <linusw@kernel.org>,
	Chen-Yu Tsai <wens@kernel.org>,
	Jernej Skrabec <jernej.skrabec@gmail.com>,
	Samuel Holland <samuel@sholland.org>,
	Andre Przywara <andre.przywara@arm.com>,
	Maxime Ripard <mripard@kernel.org>,
	linux-gpio@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 10/12] pinctrl: sunxi: Serialise per-bank regulator request and free
Date: Wed,  2 Dec 2026 09:30:04 +0000	[thread overview]
Message-ID: <9d72e998077256a13f53fa28658a7bc7f6afa84b.1796203691.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796203691.git.lee@kernel.org>

The pinmux core invokes pmxops->request() and pmxops->free() without
holding a controller-wide lock.  When two pins in the same 32-pin bank
are requested or freed concurrently, sunxi_pmx_request() and
sunxi_pmx_free() race on s_reg->regulator and s_reg->refcount, which can
result in duplicate regulator_get()/regulator_enable() calls or
addition-on-zero refcount warnings.

Serialise per-bank regulator acquisition and release in
sunxi_pmx_request() and sunxi_pmx_free() with a mutex.

Fixes: 9a2a566adb00 ("pinctrl: sunxi: Deal with per-bank regulators")
Closes: https://sashiko.dev/#/bug/linux-8e59d9d1-607f-4eba-a596-fa524b707d0e
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/pinctrl/sunxi/pinctrl-sunxi.c | 17 ++++++++++-------
 drivers/pinctrl/sunxi/pinctrl-sunxi.h |  3 +++
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.c b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
index bf4ec4665e2b..d20415b885a9 100644
--- a/drivers/pinctrl/sunxi/pinctrl-sunxi.c
+++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
@@ -10,6 +10,7 @@
  * warranty of any kind, whether express or implied.
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/export.h>
 #include <linux/gpio/driver.h>
@@ -913,13 +914,16 @@ static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset)
 	unsigned short bank_offset = bank - pctl->desc->pin_base /
 					    PINS_PER_BANK;
 	struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank_offset];
-	struct regulator *reg = s_reg->regulator;
+	struct regulator *reg;
 	char supply[16];
 	int ret;
 
 	if (WARN_ON_ONCE(bank_offset >= ARRAY_SIZE(pctl->regulators)))
 		return -EINVAL;
 
+	guard(mutex)(&pctl->reg_lock);
+
+	reg = s_reg->regulator;
 	if (reg) {
 		refcount_inc(&s_reg->refcount);
 		return 0;
@@ -936,7 +940,8 @@ static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset)
 	if (ret) {
 		dev_err(pctl->dev,
 			"Couldn't enable bank P%c regulator\n", 'A' + bank);
-		goto out;
+		regulator_put(reg);
+		return ret;
 	}
 
 	sunxi_pinctrl_set_io_bias_cfg(pctl, offset, reg);
@@ -945,11 +950,6 @@ static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset)
 	refcount_set(&s_reg->refcount, 1);
 
 	return 0;
-
-out:
-	regulator_put(reg);
-
-	return ret;
 }
 
 static int sunxi_pmx_free(struct pinctrl_dev *pctldev, unsigned offset)
@@ -960,6 +960,8 @@ static int sunxi_pmx_free(struct pinctrl_dev *pctldev, unsigned offset)
 					    PINS_PER_BANK;
 	struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank_offset];
 
+	guard(mutex)(&pctl->reg_lock);
+
 	if (!refcount_dec_and_test(&s_reg->refcount))
 		return 0;
 
@@ -1609,6 +1611,7 @@ int sunxi_pinctrl_init_with_flags(struct platform_device *pdev,
 	platform_set_drvdata(pdev, pctl);
 
 	raw_spin_lock_init(&pctl->lock);
+	mutex_init(&pctl->reg_lock);
 
 	pctl->membase = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(pctl->membase))
diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.h b/drivers/pinctrl/sunxi/pinctrl-sunxi.h
index 7c2f29e75519..1d8547e9492e 100644
--- a/drivers/pinctrl/sunxi/pinctrl-sunxi.h
+++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.h
@@ -14,6 +14,7 @@
 #define __PINCTRL_SUNXI_H
 
 #include <linux/kernel.h>
+#include <linux/mutex.h>
 #include <linux/spinlock.h>
 
 #define PA_BASE	0
@@ -187,6 +188,8 @@ struct sunxi_pinctrl {
 	int				*irq;
 	unsigned			*irq_array;
 	raw_spinlock_t			lock;
+	/* Protects @regulators */
+	struct mutex			reg_lock;
 	/*
 	 * Output latch shadow, one word per bank.  Seeded lockless at
 	 * probe before the pinctrl device registers, protected by @lock
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-02  9:33 UTC|newest]

Thread overview: 15+ 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  9:29 ` [PATCH 02/12] pinctrl: imx: Validate pin_id in imx_pinctrl_parse_pin_mmio() Lee Jones
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  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  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 ` Lee Jones [this message]
2026-12-02  9:30 ` [PATCH 11/12] pinctrl: stm32: Protect irqmux restore with hwspinlock during resume Lee Jones
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=9d72e998077256a13f53fa28658a7bc7f6afa84b.1796203691.git.lee@kernel.org \
    --to=lee@kernel.org \
    --cc=andre.przywara@arm.com \
    --cc=jernej.skrabec@gmail.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-sunxi@lists.linux.dev \
    --cc=mripard@kernel.org \
    --cc=samuel@sholland.org \
    --cc=stable@vger.kernel.org \
    --cc=wens@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

all inboxes | Powered by JetHome®