Skip to contentSkip to conversation

pinctrl: Fix Critical and High issues reported by Sashiko (batch 3)

PATCH In review Lee Jones 6 participants 21 messages Dec 2

Subsystems (12)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

cover pinctrl: Fix Critical and High issues reported by Sashiko (batch 3)

These issues were reported by Sashiko.

This is the third batch of Critical and High severity pinctrl fixes, rebased onto pinctrl/for-next. It addresses out-of-bounds array and MMIO accesses, missing locking around generic group/function trees and shared hardware registers, a shared pin configuration double-free, DT error propagation, a zero-bank dereference, and IRQ registration ordering.

Cc Alexandre Torgue <[email protected]>
Cc Andre Przywara <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Antonio Borneo <[email protected]>
Cc Bartosz Golaszewski <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Chen-Yu Tsai <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Dong Aisheng <[email protected]>
Cc Fabien Dessenne <[email protected]>
Cc Fabio Estevam <[email protected]>
Cc Frank Li <[email protected]>
Cc Gary Bisson <[email protected]>
Cc Gregory Bean <[email protected]>
Cc Haibo Chen <[email protected]>
Cc Haojian Zhuang <[email protected]>
Cc Husaini Zulkifli <[email protected]>
Cc Jacky Bai <[email protected]>
Cc Jacky Huang <[email protected]>
Cc Jernej Skrabec <[email protected]>
Cc Jonathan Hunter <[email protected]>
Cc Ju Nan <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Lokesh Vutla <[email protected]>
Cc Mark Gross <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Mehmet Fide <[email protected]>
Cc Neil Armstrong <[email protected]>
Cc Nishanth Menon <[email protected]>
Cc NXP S32 Linux Team <[email protected]>
Cc Pengutronix Kernel Team <[email protected]>
Cc Rob Herring <[email protected]>
Cc Rosen Penev <[email protected]>
Cc Samuel Holland <[email protected]>
Cc Sascha Hauer <[email protected]>
Cc Shan-Chun Hung <[email protected]>
Cc Shawn Guo <[email protected]>
Cc Sowjanya D <[email protected]>
Cc Sowjanya Komatineni <[email protected]>
Cc Takashi Sakamoto <[email protected]>
Cc Thierry Reding <[email protected]>
Cc Tony Lindgren <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Vijayakannan Ayyathurai <[email protected]>
Cc "Vineetha G. Jaya Kumaran" <[email protected]>

Lee Jones (12):
pinctrl: stm32: Validate npins against STM32_GPIO_PINS_PER_BANK
pinctrl: imx: Validate pin_id in imx_pinctrl_parse_pin_mmio()
pinctrl: single: Protect group and function updates with

    pctldev->mutex
  pinctrl: imx: Propagate DT parsing errors in imx_pinctrl_probe_dt()
  pinctrl: tegra: Require at least one register bank in
    tegra_pinctrl_probe()
  pinctrl: imx: Serialise MMIO register updates with a spinlock
  pinctrl: nuvoton: ma35: Avoid double free of shared pin configs
  pinctrl: keembay: Check src before calling keembay_find_free_slot()
  pinctrl: ti: iodelay: Protect generic group addition with
    pctldev->mutex

pinctrl: sunxi: Serialise per-bank regulator request and free
pinctrl: stm32: Protect irqmux restore with hwspinlock during resume
pinctrl: sx150x: Register threaded IRQ after adding GPIO chip

 drivers/pinctrl/freescale/pinctrl-imx.c     | 64 +++++++++++++++------
 drivers/pinctrl/freescale/pinctrl-imx.h     |  2 +
 drivers/pinctrl/freescale/pinctrl-imx7ulp.c |  3 +
 drivers/pinctrl/freescale/pinctrl-imx8ulp.c |  3 +
 drivers/pinctrl/freescale/pinctrl-vf610.c   |  3 +
 drivers/pinctrl/nuvoton/pinctrl-ma35.c      | 21 ++++++-
 drivers/pinctrl/pinctrl-keembay.c           |  6 +-
 drivers/pinctrl/pinctrl-single.c            | 12 ++--
 drivers/pinctrl/pinctrl-sx150x.c            | 12 ++--
 drivers/pinctrl/stm32/pinctrl-stm32.c       | 33 ++++++++++-
 drivers/pinctrl/sunxi/pinctrl-sunxi.c       | 17 +++---
 drivers/pinctrl/sunxi/pinctrl-sunxi.h       |  3 +
 drivers/pinctrl/tegra/pinctrl-tegra.c       |  2 +
 drivers/pinctrl/ti/pinctrl-ti-iodelay.c     |  7 ++-
 14 files changed, 149 insertions(+), 39 deletions(-)
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#1 lore raw

PATCH 1/12 pinctrl: stm32: Validate npins against STM32_GPIO_PINS_PER_BANK

stm32_gpiolib_register_bank() computes npins from the device tree "gpio-ranges" property as args.args[0] + args.args[2] without checking that the resulting value fits within STM32_GPIO_PINS_PER_BANK (16). Because bank->pin_backup[] is a fixed-size array of STM32_GPIO_PINS_PER_BANK elements and the hardware registers only have 16 lines per bank, a larger npins value leads to out-of-bounds array accesses and shift overflows.

Validate that npins is within 1..STM32_GPIO_PINS_PER_BANK.

Fixes b67210cc217f ("pinctrl: stm32: consider the GPIO offset to expose all the GPIO lines")
---
 drivers/pinctrl/stm32/pinctrl-stm32.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/pinctrl/stm32/pinctrl-stm32.c +13 -0
index bf16e5814bf8..01c8a646d9a0 100644
@@ -1657,13 +1657,26 @@ static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl, struct fwnode
 		bank->gpio_chip.base = args.args[1];
 
 		/* get the last defined gpio line (offset + nb of pins) */
+		if (args.args[0] >= STM32_GPIO_PINS_PER_BANK ||
+		    args.args[2] > STM32_GPIO_PINS_PER_BANK - args.args[0]) {
+			fwnode_handle_put(args.fwnode);
+			return -EINVAL;
+		}
 		npins = args.args[0] + args.args[2];
 		fwnode_handle_put(args.fwnode);
 
 		while (!fwnode_property_get_reference_args(fwnode, "gpio-ranges", NULL, 3, ++i, &args)) {
+			if (args.args[0] >= STM32_GPIO_PINS_PER_BANK ||
+			    args.args[2] > STM32_GPIO_PINS_PER_BANK - args.args[0]) {
+				fwnode_handle_put(args.fwnode);
+				return -EINVAL;
+			}
 			npins = max(npins, (int)(args.args[0] + args.args[2]));
 			fwnode_handle_put(args.fwnode);
 		}
+
+		if (!npins)
+			return -EINVAL;
 	} else {
 		bank_nr = pctl->nbanks;
 		bank->gpio_chip.base = bank_nr * STM32_GPIO_PINS_PER_BANK;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#3 lore raw

PATCH 2/12 pinctrl: imx: Validate pin_id in imx_pinctrl_parse_pin_mmio()

In reply to Lee Jones

imx_pinctrl_parse_pin_mmio() derives *pin_id from mux_reg / 4 or conf_reg / 4 and uses it directly to index ipctl->pin_regs[*pin_id] and info->pins[*pin_id] without bounds checking against info->npins. If a device tree entry specifies an out-of-range register offset or sets both mux_reg and conf_reg to 0 on a SoC without ZERO_OFFSET_VALID (making conf_reg == -1 and *pin_id == 0x3fffffff), this results in an out-of-bounds memory access.

Validate that *pin_id is less than info->npins and return -EINVAL on invalid entries.

Fixes e16415313c9b ("pinctrl: imx: move hard-coding data into device tree")
---
 drivers/pinctrl/freescale/pinctrl-imx.c | 32 ++++++++++++++++++-------
 1 file changed, 24 insertions(+), 8 deletions(-)
drivers/pinctrl/freescale/pinctrl-imx.c +24 -8
index ebcce9aeac8c..0a825f58448f 100644
@@ -502,15 +502,16 @@ static const struct pinconf_ops imx_pinconf_ops = {
 #define FSL_PIN_SHARE_SIZE 20
 #define FSL_SCU_PIN_SIZE 12
 
-static void imx_pinctrl_parse_pin_mmio(struct imx_pinctrl *ipctl,
-				       unsigned int *pin_id, struct imx_pin *pin,
-				       const __be32 **list_p,
-				       struct device_node *np)
+static int imx_pinctrl_parse_pin_mmio(struct imx_pinctrl *ipctl,
+				      unsigned int *pin_id, struct imx_pin *pin,
+				      const __be32 **list_p,
+				      struct device_node *np)
 {
 	const struct imx_pinctrl_soc_info *info = ipctl->info;
 	struct imx_pin_mmio *pin_mmio = &pin->conf.mmio;
 	struct imx_pin_reg *pin_reg;
 	const __be32 *list = *list_p;
+	unsigned int id;
 	u32 mux_reg, conf_reg;
 	u32 config;
 
@@ -527,7 +528,14 @@ static void imx_pinctrl_parse_pin_mmio(struct imx_pinctrl *ipctl,
 			conf_reg = -1;
 	}
 
-	*pin_id = (mux_reg != -1) ? mux_reg / 4 : conf_reg / 4;
+	id = (mux_reg != -1) ? mux_reg / 4 : conf_reg / 4;
+	if (id >= info->npins) {
+		dev_err(ipctl->dev, "Invalid pin id %u in node %pOF\n",
+			id, np);
+		return -EINVAL;
+	}
+
+	*pin_id = id;
 	pin_reg = &ipctl->pin_regs[*pin_id];
 	pin->pin = *pin_id;
 	pin_reg->mux_reg = mux_reg;
@@ -547,6 +555,8 @@ static void imx_pinctrl_parse_pin_mmio(struct imx_pinctrl *ipctl,
 
 	dev_dbg(ipctl->dev, "%s: 0x%x 0x%08lx", info->pins[*pin_id].name,
 			     pin_mmio->mux_mode, pin_mmio->config);
+
+	return 0;
 }
 
 static int imx_pinctrl_parse_groups(struct device_node *np,
@@ -611,10 +621,16 @@ static int imx_pinctrl_parse_groups(struct device_node *np,
 
 	for (i = 0; i < grp->grp.npins; i++) {
 		pin = &((struct imx_pin *)(grp->data))[i];
-		if (info->flags & IMX_USE_SCU)
+		if (info->flags & IMX_USE_SCU) {
 			info->imx_pinctrl_parse_pin(ipctl, &pins[i], pin, &list);
-		else
-			imx_pinctrl_parse_pin_mmio(ipctl, &pins[i], pin, &list, np);
+		} else {
+			int ret;
+
+			ret = imx_pinctrl_parse_pin_mmio(ipctl, &pins[i], pin,
+							 &list, np);
+			if (ret)
+				return ret;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#6 lore raw

PATCH 3/12 pinctrl: single: Protect group and function updates with pctldev->mutex

In reply to Lee Jones

pcs_parse_one_pinctrl_entry() and pcs_parse_bits_in_pinctrl_entry() mutate pcs->pctl->pin_function_tree and pcs->pctl->pin_group_tree via pcs_add_function(), pinctrl_generic_add_group(), and their removal helpers while holding only the driver-private pcs->mutex. However, the pinctrl core debugfs readers and teardown paths traverse and modify those radix trees under pctldev->mutex, leaving concurrent readers and writers racing without a shared lock.

Acquire pcs->pctl->mutex instead of pcs->mutex in both parsing functions so all generic group and function tree operations are serialised against the pinctrl core.

Fixes a4ab10860723 ("pinctrl: single: Fix group and function selector use")
---
 drivers/pinctrl/pinctrl-single.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/pinctrl/pinctrl-single.c +6 -6
index e351cdf50669..618f8fc6ee6c 100644
@@ -1059,7 +1059,7 @@ static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
 	}
 
 	pgnames[0] = np->name;
-	mutex_lock(&pcs->mutex);
+	mutex_lock(&pcs->pctl->mutex);
 	fsel = pcs_add_function(pcs, &function, np->name, vals, found,
 				pgnames, 1);
 	if (fsel < 0) {
@@ -1102,7 +1102,7 @@ static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
 		devm_kfree(pcs->dev, function);
 		devm_kfree(pcs->dev, pgnames);
 	}
-	mutex_unlock(&pcs->mutex);
+	mutex_unlock(&pcs->pctl->mutex);
 
 	return 0;
 
@@ -1118,7 +1118,7 @@ static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
 	if (pinmux_generic_get_function(pcs->pctl, fsel)->data == function)
 		pinmux_generic_remove_function(pcs->pctl, fsel);
 free_pins:
-	mutex_unlock(&pcs->mutex);
+	mutex_unlock(&pcs->pctl->mutex);
 	devm_kfree(pcs->dev, pins);
 
 free_vals:
@@ -1229,7 +1229,7 @@ static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
 	}
 
 	pgnames[0] = np->name;
-	mutex_lock(&pcs->mutex);
+	mutex_lock(&pcs->pctl->mutex);
 	fsel = pcs_add_function(pcs, &function, np->name, vals, found,
 				pgnames, 1);
 	if (fsel < 0) {
@@ -1254,7 +1254,7 @@ static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
 		devm_kfree(pcs->dev, function);
 		devm_kfree(pcs->dev, pgnames);
 	}
-	mutex_unlock(&pcs->mutex);
+	mutex_unlock(&pcs->pctl->mutex);
 
 	return 0;
 
@@ -1263,7 +1263,7 @@ static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
 	if (pinmux_generic_get_function(pcs->pctl, fsel)->data == function)
 		pinmux_generic_remove_function(pcs->pctl, fsel);
 free_pins:
-	mutex_unlock(&pcs->mutex);
+	mutex_unlock(&pcs->pctl->mutex);
 	devm_kfree(pcs->dev, pins);
 
 free_vals:
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#8 lore raw

PATCH 4/12 pinctrl: imx: Propagate DT parsing errors in imx_pinctrl_probe_dt()

In reply to Lee Jones

imx_pinctrl_parse_functions() inserts each group descriptor into pctl->pin_group_tree before calling imx_pinctrl_parse_groups() and ignores the return value of imx_pinctrl_parse_groups(). Likewise, imx_pinctrl_probe_dt() ignores the return value of imx_pinctrl_parse_functions(). If a device tree group node is malformed or a memory allocation fails, partially initialised group descriptors with NULL data pointers remain registered in the radix tree and cause a NULL pointer dereference when the pinctrl core or debugfs accesses the group.

Parse each group before inserting it into pctl->pin_group_tree and propagate errors from imx_pinctrl_parse_groups() and imx_pinctrl_parse_functions(). Return 0 when a function node has no groups so boards with empty controller nodes continue to probe as before.

Fixes e566fc11ea76 ("pinctrl: imx: use generic pinctrl helpers for managing groups")
---
 drivers/pinctrl/freescale/pinctrl-imx.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
drivers/pinctrl/freescale/pinctrl-imx.c +14 -8
index 0a825f58448f..b1114dce3485 100644
@@ -658,7 +658,7 @@ static int imx_pinctrl_parse_functions(struct device_node *np,
 	func->ngroups = of_get_child_count(np);
 	if (func->ngroups == 0) {
 		dev_info(ipctl->dev, "no groups defined in %pOF\n", np);
-		return -EINVAL;
+		return 0;
 	}
 
 	group_names = devm_kzalloc_objs(ipctl->dev, *group_names, func->ngroups);
@@ -679,6 +679,10 @@ static int imx_pinctrl_parse_functions(struct device_node *np,
 		if (!grp)
 			return -ENOMEM;
 
+		ret = imx_pinctrl_parse_groups(child, grp, ipctl, i++);
+		if (ret)
+			return ret;
+
 		mutex_lock(&ipctl->mutex);
 		/*
 		 * FIXME: This should use pinctrl_generic_add_group() and not
@@ -687,8 +691,6 @@ static int imx_pinctrl_parse_functions(struct device_node *np,
 		radix_tree_insert(&pctl->pin_group_tree,
 				  ipctl->group_index++, grp);
 		mutex_unlock(&ipctl->mutex);
-
-		imx_pinctrl_parse_groups(child, grp, ipctl, i++);
 	}
 
 	return 0;
@@ -718,7 +720,6 @@ static int imx_pinctrl_probe_dt(struct platform_device *pdev,
 				struct imx_pinctrl *ipctl)
 {
 	struct device_node *np = pdev->dev.of_node;
-	struct device_node *child;
 	struct pinctrl_dev *pctl = ipctl->pctl;
 	u32 nfuncs = 0;
 	u32 i = 0;
@@ -743,16 +744,21 @@ static int imx_pinctrl_probe_dt(struct platform_device *pdev,
 		pctl->num_groups = of_get_child_count(np);
 	} else {
 		pctl->num_groups = 0;
-		for_each_child_of_node(np, child)
+		for_each_child_of_node_scoped(np, child)
 			pctl->num_groups += of_get_child_count(child);
 	}
 
 	if (flat_funcs) {
-		imx_pinctrl_parse_functions(np, ipctl, 0);
+		return imx_pinctrl_parse_functions(np, ipctl, 0);
 	} else {
+		int ret;
+
 		i = 0;
-		for_each_child_of_node(np, child)
-			imx_pinctrl_parse_functions(child, ipctl, i++);
+		for_each_child_of_node_scoped(np, child) {
+			ret = imx_pinctrl_parse_functions(child, ipctl, i++);
+			if (ret)
+				return ret;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#10 lore raw

PATCH 5/12 pinctrl: tegra: Require at least one register bank in tegra_pinctrl_probe()

In reply to Lee Jones

tegra_pinctrl_probe() counts IORESOURCE_MEM resources without checking that at least one memory bank is present. If a device tree node omits the reg property, pmx->nbanks is 0, pmx->regs is allocated as ZERO_SIZE_PTR, and tegra_pinctrl_resume() unconditionally dereferences pmx->regs[0] to flush writes.

Return -ENODEV from tegra_pinctrl_probe() if pmx->nbanks is 0.

Fixes a4873accca05 ("pinctrl: tegra: Flush pinctrl writes during resume")
---
 drivers/pinctrl/tegra/pinctrl-tegra.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pinctrl/tegra/pinctrl-tegra.c +2 -0
index 51684c5dda34..62a19c6eda3a 100644
@@ -903,6 +903,8 @@ int tegra_pinctrl_probe(struct platform_device *pdev,
 		backup_regs_size += resource_size(res);
 	}
 	pmx->nbanks = i;
+	if (!pmx->nbanks)
+		return -ENODEV;
 
 	pmx->regs = devm_kzalloc_objs(&pdev->dev, *pmx->regs, pmx->nbanks);
 	if (!pmx->regs)
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#11 lore raw

PATCH 6/12 pinctrl: imx: Serialise MMIO register updates with a spinlock

In reply to Lee Jones

The pinctrl core does not serialise .set_mux() and .pin_config_set() callbacks against one another or against concurrent .set_mux() calls. On i.MX platforms that share mux and configuration registers via SHARE_MUX_CONF_REG or share general-purpose select-input registers, concurrent read-modify-write sequences in imx_pmx_set_one_pin_mmio(), imx_pinconf_set_mmio(), and the GPIO direction callbacks can race and overwrite each other's updates.

Add a raw_spinlock_t to struct imx_pinctrl and hold it across MMIO read-modify-write register updates.

Fixes 94176faf279b ("pinctrl: imx: work around select input quirk")
---
 drivers/pinctrl/freescale/pinctrl-imx.c     | 10 ++++++++++
 drivers/pinctrl/freescale/pinctrl-imx.h     |  2 ++
 drivers/pinctrl/freescale/pinctrl-imx7ulp.c |  3 +++
 drivers/pinctrl/freescale/pinctrl-imx8ulp.c |  3 +++
 drivers/pinctrl/freescale/pinctrl-vf610.c   |  3 +++
 5 files changed, 21 insertions(+)
drivers/pinctrl/freescale/pinctrl-imx.c +10 -0
index b1114dce3485..807f72b8b6ab 100644
@@ -168,6 +168,7 @@ static int imx_pmx_set_one_pin_mmio(struct imx_pinctrl *ipctl,
 	const struct imx_pinctrl_soc_info *info = ipctl->info;
 	struct imx_pin_mmio *pin_mmio = &pin->conf.mmio;
 	const struct imx_pin_reg *pin_reg;
+	unsigned long flags;
 	unsigned int pin_id;
 
 	pin_id = pin->pin;
@@ -182,10 +183,12 @@ static int imx_pmx_set_one_pin_mmio(struct imx_pinctrl *ipctl,
 	if (info->flags & SHARE_MUX_CONF_REG) {
 		u32 reg;
 
+		raw_spin_lock_irqsave(&ipctl->lock, flags);
 		reg = readl(ipctl->base + pin_reg->mux_reg);
 		reg &= ~info->mux_mask;
 		reg |= (pin_mmio->mux_mode << info->mux_shift);
 		writel(reg, ipctl->base + pin_reg->mux_reg);
+		raw_spin_unlock_irqrestore(&ipctl->lock, flags);
 		dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n",
 			pin_reg->mux_reg, reg);
 	} else {
@@ -217,10 +220,12 @@ static int imx_pmx_set_one_pin_mmio(struct imx_pinctrl *ipctl,
 		 * The input_reg[i] here is actually some IOMUXC general
 		 * purpose register, not regular select input register.
 		 */
+		raw_spin_lock_irqsave(&ipctl->lock, flags);
 		val = readl(ipctl->base + pin_mmio->input_reg);
 		val &= ~mask;
 		val |= select << shift;
 		writel(val, ipctl->base + pin_mmio->input_reg);
+		raw_spin_unlock_irqrestore(&ipctl->lock, flags);
 	} else if (pin_mmio->input_reg) {
 		/*
 		 * Regular select input register can never be at offset
@@ -390,11 +395,15 @@ static int imx_pinconf_set_mmio(struct pinctrl_dev *pctldev,
 
 	for (i = 0; i < num_configs; i++) {
 		if (info->flags & SHARE_MUX_CONF_REG) {
+			unsigned long flags;
 			u32 reg;
+
+			raw_spin_lock_irqsave(&ipctl->lock, flags);
 			reg = readl(ipctl->base + pin_reg->conf_reg);
 			reg &= info->mux_mask;
 			reg |= configs[i];
 			writel(reg, ipctl->base + pin_reg->conf_reg);
+			raw_spin_unlock_irqrestore(&ipctl->lock, flags);
 			dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n",
 				pin_reg->conf_reg, reg);
 		} else {
@@ -840,6 +849,7 @@ int imx_pinctrl_probe(struct platform_device *pdev,
 	imx_pmx_ops.gpio_set_direction = info->gpio_set_direction;
 
 	mutex_init(&ipctl->mutex);
+	raw_spin_lock_init(&ipctl->lock);
 
 	ipctl->info = info;
 	ipctl->dev = &pdev->dev;
drivers/pinctrl/freescale/pinctrl-imx.h +2 -0
index 8fa7e1e2521d..0d33584fa710 100644
@@ -12,6 +12,7 @@
 #define __DRIVERS_PINCTRL_IMX_H
 
 #include <linux/pinctrl/pinmux.h>
+#include <linux/spinlock.h>
 
 struct platform_device;
 
@@ -79,6 +80,7 @@ struct imx_pinctrl {
 	struct imx_pin_reg *pin_regs;
 	unsigned int group_index;
 	struct mutex mutex;
+	raw_spinlock_t lock;
 };
 
 struct imx_pinctrl_soc_info {
drivers/pinctrl/freescale/pinctrl-imx7ulp.c +3 -0
index 063805daee03..b44bbc77ab4a 100644
@@ -263,18 +263,21 @@ static int imx7ulp_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
 {
 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
 	const struct imx_pin_reg *pin_reg;
+	unsigned long flags;
 	u32 reg;
 
 	pin_reg = &ipctl->pin_regs[offset];
 	if (pin_reg->mux_reg == -1)
 		return -EINVAL;
 
+	raw_spin_lock_irqsave(&ipctl->lock, flags);
 	reg = readl(ipctl->base + pin_reg->mux_reg);
 	if (input)
 		reg = (reg & ~BM_OBE_ENABLED) | BM_IBE_ENABLED;
 	else
 		reg = (reg & ~BM_IBE_ENABLED) | BM_OBE_ENABLED;
 	writel(reg, ipctl->base + pin_reg->mux_reg);
+	raw_spin_unlock_irqrestore(&ipctl->lock, flags);
 
 	return 0;
 }
drivers/pinctrl/freescale/pinctrl-imx8ulp.c +3 -0
index 88af25b0d48e..a869de92aeba 100644
@@ -222,18 +222,21 @@ static int imx8ulp_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
 {
 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
 	const struct imx_pin_reg *pin_reg;
+	unsigned long flags;
 	u32 reg;
 
 	pin_reg = &ipctl->pin_regs[offset];
 	if (pin_reg->mux_reg == -1)
 		return -EINVAL;
 
+	raw_spin_lock_irqsave(&ipctl->lock, flags);
 	reg = readl(ipctl->base + pin_reg->mux_reg);
 	if (input)
 		reg = (reg & ~BM_OBE_ENABLED) | BM_IBE_ENABLED;
 	else
 		reg = (reg & ~BM_IBE_ENABLED) | BM_OBE_ENABLED;
 	writel(reg, ipctl->base + pin_reg->mux_reg);
+	raw_spin_unlock_irqrestore(&ipctl->lock, flags);
 
 	return 0;
 }
drivers/pinctrl/freescale/pinctrl-vf610.c +3 -0
index 77d077618782..930e414ca8d7 100644
@@ -295,6 +295,7 @@ static int vf610_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
 {
 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
 	const struct imx_pin_reg *pin_reg;
+	unsigned long flags;
 	u32 reg;
 
 	pin_reg = &ipctl->pin_regs[offset];
@@ -302,12 +303,14 @@ static int vf610_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
 		return -EINVAL;
 
 	/* IBE always enabled allows us to read the value "on the wire" */
+	raw_spin_lock_irqsave(&ipctl->lock, flags);
 	reg = readl(ipctl->base + pin_reg->mux_reg);
 	if (input)
 		reg &= ~0x2;
 	else
 		reg |= 0x2;
 	writel(reg, ipctl->base + pin_reg->mux_reg);
+	raw_spin_unlock_irqrestore(&ipctl->lock, flags);
 
 	return 0;
 }
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#13 lore raw

PATCH 7/12 pinctrl: nuvoton: ma35: Avoid double free of shared pin configs

In reply to Lee Jones

ma35_pinctrl_parse_groups() allocates a single configs array via pinconf_generic_parse_dt_config() and assigns the same pointer to every pin in the group. ma35_pinctrl_dt_node_to_map_func() then copies that shared pointer into every PIN_MAP_TYPE_CONFIGS_PIN map entry. Because ma35_pctrl_ops uses pinconf_generic_dt_free_map(), tearing down the pinctrl mapping frees the same configs pointer once per pin, causing an immediate double free and leaving dangling pointers in the group data for subsequent mappings. The configs array is also leaked on error paths in ma35_pinctrl_parse_groups().

Manage the lifetime of the shared configs array with a devm action registered right after pinconf_generic_parse_dt_config(), and use a dedicated dt_free_map callback that frees only the map array.

Fixes f805e356313b ("pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver")
---
 drivers/pinctrl/nuvoton/pinctrl-ma35.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/pinctrl/nuvoton/pinctrl-ma35.c +20 -1
index c20350bc1cdb..b9b34b8b84c6 100644
@@ -17,6 +17,7 @@
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/regmap.h>
+#include <linux/slab.h>
 
 #include <linux/pinctrl/pinconf.h>
 #include <linux/pinctrl/pinctrl.h>
@@ -236,12 +237,18 @@ static int ma35_pinctrl_dt_node_to_map_func(struct pinctrl_dev *pctldev,
 	return 0;
 }
 
+static void ma35_dt_free_map(struct pinctrl_dev *pctldev,
+			     struct pinctrl_map *map, unsigned int num_maps)
+{
+	kfree(map);
+}
+
 static const struct pinctrl_ops ma35_pctrl_ops = {
 	.get_groups_count = ma35_get_groups_count,
 	.get_group_name = ma35_get_group_name,
 	.get_group_pins = ma35_get_group_pins,
 	.dt_node_to_map = ma35_pinctrl_dt_node_to_map_func,
-	.dt_free_map = pinconf_generic_dt_free_map,
+	.dt_free_map = ma35_dt_free_map,
 };
 
 static int ma35_pinmux_get_func_count(struct pinctrl_dev *pctldev)
@@ -1028,6 +1035,11 @@ static const struct pinconf_ops ma35_pinconf_ops = {
 	.is_generic = true,
 };
 
+static void ma35_free_configs(void *data)
+{
+	kfree(data);
+}
+
 static int ma35_pinctrl_parse_groups(struct fwnode_handle *fwnode, struct group_desc *grp,
 				     struct ma35_pinctrl *npctl, u32 index)
 {
@@ -1043,6 +1055,13 @@ static int ma35_pinctrl_parse_groups(struct fwnode_handle *fwnode, struct group_
 	if (ret)
 		return ret;
 
+	if (configs) {
+		ret = devm_add_action_or_reset(npctl->dev, ma35_free_configs,
+					       configs);
+		if (ret)
+			return ret;
+	}
+
 	count = fwnode_property_count_u32(fwnode, "nuvoton,pins");
 	if (!count || count % 3)
 		return -EINVAL;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#14 lore raw

PATCH 8/12 pinctrl: keembay: Check src before calling keembay_find_free_slot()

In reply to Lee Jones

keembay_gpio_irq_enable() passes the return value of keembay_find_free_src() straight into keembay_find_free_slot() before checking whether src is negative. When all interrupt sources are busy, keembay_find_free_src() returns -EBUSY, which is implicitly converted to a large unsigned int in keembay_find_free_slot() and causes an out-of-bounds MMIO read.

Return early when src is negative before calling
keembay_find_free_slot().

Fixes ffd4e739358b ("pinctrl: Add Intel Keem Bay pinctrl driver")
---
 drivers/pinctrl/pinctrl-keembay.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pinctrl/pinctrl-keembay.c +4 -2
index cf2a81c3c2cc..78f02693b4fa 100644
@@ -1375,9 +1375,11 @@ static void keembay_gpio_irq_enable(struct irq_data *data)
 
 	/* Check which Interrupt source and slot is available */
 	src = keembay_find_free_src(kpc, trig);
+	if (src < 0)
+		return;
+
 	slot = keembay_find_free_slot(kpc, src);
-
-	if (src < 0 || slot < 0)
+	if (slot < 0)
 		return;
 
 	gpiochip_enable_irq(gc, pin);
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#15 lore raw

PATCH 9/12 pinctrl: ti: iodelay: Protect generic group addition with pctldev->mutex

In reply to Lee Jones

ti_iodelay_dt_node_to_map() calls pinctrl_generic_add_group() and pinctrl_generic_get_group() without holding iod->pctl->mutex. Both helpers expect the caller to hold pctldev->mutex to serialise radix tree mutations against concurrent consumer probes and pinctrl core debugfs readers.

Acquire iod->pctl->mutex around pinctrl_generic_add_group() and the subsequent pinctrl_generic_get_group() deduplication check.

Fixes 003910ebc83b ("pinctrl: Introduce TI IOdelay configuration driver")
---
 drivers/pinctrl/ti/pinctrl-ti-iodelay.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/pinctrl/ti/pinctrl-ti-iodelay.c +6 -1
index 4a48cebbf2b3..f52d0af0794d 100644
@@ -13,6 +13,7 @@
 #include <linux/init.h>
 #include <linux/io.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
@@ -550,9 +551,12 @@ static int ti_iodelay_dt_node_to_map(struct pinctrl_dev *pctldev,
 	g->ncfg = i;
 	g->config = PIN_CONFIG_END;
 
+	mutex_lock(&iod->pctl->mutex);
 	error = pinctrl_generic_add_group(iod->pctl, np->name, pins, i, g);
-	if (error < 0)
+	if (error < 0) {
+		mutex_unlock(&iod->pctl->mutex);
 		goto free_data;
+	}
 
 	/*
 	 * The core deduplicates groups by name and keeps the first for the
@@ -566,6 +570,7 @@ static int ti_iodelay_dt_node_to_map(struct pinctrl_dev *pctldev,
 		devm_kfree(iod->dev, g);
 		g = grp->data;
 	}
+	mutex_unlock(&iod->pctl->mutex);
 
 	(*map)->type = PIN_MAP_TYPE_CONFIGS_GROUP;
 	(*map)->data.configs.group_or_pin = np->name;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#16 lore raw

PATCH 10/12 pinctrl: sunxi: Serialise per-bank regulator request and free

In reply to Lee Jones

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")
---
 drivers/pinctrl/sunxi/pinctrl-sunxi.c | 17 ++++++++++-------
 drivers/pinctrl/sunxi/pinctrl-sunxi.h |  3 +++
 2 files changed, 13 insertions(+), 7 deletions(-)
drivers/pinctrl/sunxi/pinctrl-sunxi.c +10 -7
index bf4ec4665e2b..d20415b885a9 100644
@@ -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))
drivers/pinctrl/sunxi/pinctrl-sunxi.h +3 -0
index 7c2f29e75519..1d8547e9492e 100644
@@ -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
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#17 lore raw

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

In reply to Lee Jones

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")
---
 drivers/pinctrl/stm32/pinctrl-stm32.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/pinctrl/stm32/pinctrl-stm32.c +19 -1
index 01c8a646d9a0..897da5fafaf4 100644
@@ -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;
 }
 
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#19 lore raw

PATCH 12/12 pinctrl: sx150x: Register threaded IRQ after adding GPIO chip

In reply to Lee Jones

sx150x_probe() calls devm_request_threaded_irq() with IRQF_SHARED before devm_gpiochip_add_data(). If a shared or pending interrupt fires before the GPIO chip is registered, sx150x_irq_thread_fn() passes a NULL pctl->gpio.irq.domain to irq_find_mapping(). Because devres tears down resources in LIFO order, this ordering also removes the GPIO chip and its IRQ domain while the threaded IRQ handler is still active during probe failure or device unbind, causing a use-after-free.

Move devm_request_threaded_irq() after devm_gpiochip_add_data() so the IRQ domain is created before the handler can run and torn down after the IRQ is freed.

Fixes 0a04d767af8c ("pinctrl: sx150x: Use irqchip template")
---
 drivers/pinctrl/pinctrl-sx150x.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/pinctrl/pinctrl-sx150x.c +7 -5
index 0cf68fe22f89..bbc8c4f1f44a 100644
@@ -1164,7 +1164,7 @@ static int sx150x_probe(struct i2c_client *client)
 				     I2C_FUNC_SMBUS_WRITE_WORD_DATA;
 	struct device *dev = &client->dev;
 	struct sx150x_pinctrl *pctl;
-	u32 irq_type;
+	u32 irq_type = 0;
 	int ret;
 
 	if (!i2c_check_functionality(client->adapter, i2c_funcs))
@@ -1288,7 +1288,13 @@ static int sx150x_probe(struct i2c_client *client)
 					"unsupported irq trigger type %x\n",
 					irq_type);
 		}
+	}
 
+	ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl);
+	if (ret)
+		return ret;
+
+	if (client->irq > 0) {
 		ret = devm_request_threaded_irq(dev, client->irq, NULL,
 						sx150x_irq_thread_fn,
 						IRQF_ONESHOT | IRQF_SHARED |
@@ -1298,10 +1304,6 @@ static int sx150x_probe(struct i2c_client *client)
 			return ret;
 	}
 
-	ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl);
-	if (ret)
-		return ret;
-
 	/*
 	 * Pin control functions need to be enabled AFTER registering the
 	 * GPIO chip because sx150x_pinconf_set() calls
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
Reviewed-by on v1 12/12
#20 lore raw

Re: [PATCH 12/12] pinctrl: sx150x: Register threaded IRQ after adding GPIO chip

Quoted 48 lines · Lee Jones

sx150x_probe() calls devm_request_threaded_irq() with IRQF_SHARED before devm_gpiochip_add_data(). If a shared or pending interrupt fires before the GPIO chip is registered, sx150x_irq_thread_fn() passes a NULL pctl->gpio.irq.domain to irq_find_mapping(). Because devres tears down resources in LIFO order, this ordering also removes the GPIO chip and its IRQ domain while the threaded IRQ handler is still active during probe failure or device unbind, causing a use-after-free.

Move devm_request_threaded_irq() after devm_gpiochip_add_data() so the IRQ domain is created before the handler can run and torn down after the IRQ is freed.

Fixes 0a04d767af8c ("pinctrl: sx150x: Use irqchip template")
---
  drivers/pinctrl/pinctrl-sx150x.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
drivers/pinctrl/pinctrl-sx150x.c +7 -1
index 0cf68fe22f89..bbc8c4f1f44a 100644
@@ -1164,7 +1164,7 @@ static int sx150x_probe(struct i2c_client *client)
  				     I2C_FUNC_SMBUS_WRITE_WORD_DATA;
  	struct device *dev = &client->dev;
  	struct sx150x_pinctrl *pctl;
-	u32 irq_type;
+	u32 irq_type = 0;
  	int ret;
 
  	if (!i2c_check_functionality(client->adapter, i2c_funcs))
@@ -1288,7 +1288,13 @@ static int sx150x_probe(struct i2c_client *client)
  					"unsupported irq trigger type %x\n",
  					irq_type);
  		}
+	}
 
+	ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl);
+	if (ret)
+		return ret;
+
+	if (client->irq > 0) {
  		ret = devm_request_threaded_irq(dev, client->irq, NULL,
  						sx150x_irq_thread_fn,
  						IRQF_ONESHOT | IRQF_SHARED |

drivers/pinctrl/pinctrl-sx150x.c static int sx150x_probe(struct i2c_client *client) · line 1309

Quoted 10 lines
drivers/pinctrl/pinctrl-sx150x.c +0 -4
@@ -1298,10 +1304,6 @@ static int sx150x_probe(struct i2c_client *client)
  			return ret;
  	}
 
-	ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl);
-	if (ret)
-		return ret;
-
  	/*
  	 * Pin control functions need to be enabled AFTER registering the
  	 * GPIO chip because sx150x_pinconf_set() calls

Thanks,
Neil

#4 lore raw

Re: [PATCH 02/12] pinctrl: imx: Validate pin_id in imx_pinctrl_parse_pin_mmio()

In reply to Lee Jones
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] pinctrl: imx: unhandled errors in…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] pinctrl: imx: unhandled errors in imx_pinctrl_probe_dt() [1] --

commit 6b0819d145656fd0c5efa904a33b1bacd1462e80
Author: Lee Jones <[email protected]>

pinctrl: imx: Validate pin_id in imx_pinctrl_parse_pin_mmio()

This commit bounds-checks the derived pin_id against info->npins in imx_pinctrl_parse_pin_mmio() and propagates -EINVAL to imx_pinctrl_parse_groups().

Quoted 4 lines
drivers/pinctrl/freescale/pinctrl-imx.c +0 -0
index ebcce9aeac8c3..0a825f58448f9 100644

[ ... ]

Quoted 10 lines
drivers/pinctrl/freescale/pinctrl-imx.c +2 -3
@@ -611,10 +621,16 @@ static int imx_pinctrl_parse_groups(struct device_node *np,
 
 	for (i = 0; i < grp->grp.npins; i++) {
 		pin = &((struct imx_pin *)(grp->data))[i];
-		if (info->flags & IMX_USE_SCU)
+		if (info->flags & IMX_USE_SCU) {
 			info->imx_pinctrl_parse_pin(ipctl, &pins[i], pin, &list);
-		else
-			imx_pinctrl_parse_pin_mmio(ipctl, &pins[i], pin, &list, np);
+		} else {

drivers/pinctrl/freescale/pinctrl-imx.c static int imx_pinctrl_parse_groups(struct device_node *np, · line 636

Quoted 10 lines
drivers/pinctrl/freescale/pinctrl-imx.c +7 -0
+			int ret;
+
+			ret = imx_pinctrl_parse_pin_mmio(ipctl, &pins[i], pin,
+							 &list, np);
+			if (ret)
+				return ret;
+		}
 	}
 
 	return 0;

[Severity: High]
This problem wasn't introduced by this patch, but can returning an error from imx_pinctrl_parse_groups() leave a partially initialized group registered in pin_group_tree [1]?

imx_pinctrl_parse_functions() inserts grp into pctl->pin_group_tree and then calls imx_pinctrl_parse_groups() without checking its return value, and imx_pinctrl_probe_dt() likewise ignores the return value of imx_pinctrl_parse_functions():

drivers/pinctrl/freescale/pinctrl-imx.c:imx_pinctrl_parse_functions() {
	...
		radix_tree_insert(&pctl->pin_group_tree,
				  ipctl->group_index++, grp);
		mutex_unlock(&ipctl->mutex);

		imx_pinctrl_parse_groups(child, grp, ipctl, i++);
	}
}

If imx_pinctrl_parse_groups() fails before allocating grp->data or grp->grp.pins (or if a later pin entry fails after only part of the group is populated), probing still succeeds and later group activation such as imx_pmx_set() can dereference a NULL or unpopulated pin array.

[1] https://sashiko.dev/#/bug/linux-70c60bc4-5868-4c94-b1bb-7073f7a3316c

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Reviewed-by on v1 3/12
#7 lore raw

Re: [PATCH 03/12] pinctrl: single: Protect group and function updates with pctldev->mutex

In reply to Lee Jones
Quoted 12 lines · Lee Jones

pcs_parse_one_pinctrl_entry() and pcs_parse_bits_in_pinctrl_entry() mutate pcs->pctl->pin_function_tree and pcs->pctl->pin_group_tree via pcs_add_function(), pinctrl_generic_add_group(), and their removal helpers while holding only the driver-private pcs->mutex. However, the pinctrl core debugfs readers and teardown paths traverse and modify those radix trees under pctldev->mutex, leaving concurrent readers and writers racing without a shared lock.

Acquire pcs->pctl->mutex instead of pcs->mutex in both parsing functions so all generic group and function tree operations are serialised against the pinctrl core.

Quoted 5 lines
Fixes a4ab10860723 ("pinctrl: single: Fix group and function selector use")

---

maintainer Reviewed-by on v1 11/12
#18 lore raw

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

In reply to Lee Jones
Quoted 44 lines · Lee Jones

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")

---
 drivers/pinctrl/stm32/pinctrl-stm32.c | 20 +++++++++++++++++++-

 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/pinctrl/stm32/pinctrl-stm32.c +0 -0
index 01c8a646d9a0..897da5fafaf4 100644
@@ -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;

drivers/pinctrl/stm32/pinctrl-stm32.c +12 -0
+
+               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);

Quoted 8 lines

+               if (pctl->hwlock)
+                       hwspin_unlock_in_atomic(pctl->hwlock); +               spin_unlock_irqrestore(&pctl->irqmux_lock, flags); +       }
+
        return 0;
 }

Thanks,
Antonio

maintainer Reviewed-by on v1 1/12
#2 lore raw

Re: [PATCH 01/12] pinctrl: stm32: Validate npins against STM32_GPIO_PINS_PER_BANK

In reply to Lee Jones
Quoted 41 lines · Lee Jones

stm32_gpiolib_register_bank() computes npins from the device tree "gpio-ranges" property as args.args[0] + args.args[2] without checking that the resulting value fits within STM32_GPIO_PINS_PER_BANK (16). Because bank->pin_backup[] is a fixed-size array of STM32_GPIO_PINS_PER_BANK elements and the hardware registers only have 16 lines per bank, a larger npins value leads to out-of-bounds array accesses and shift overflows.

Validate that npins is within 1..STM32_GPIO_PINS_PER_BANK.

Fixes b67210cc217f ("pinctrl: stm32: consider the GPIO offset to expose all the GPIO lines")

---
 drivers/pinctrl/stm32/pinctrl-stm32.c | 13 +++++++++++++

 1 file changed, 13 insertions(+)
drivers/pinctrl/stm32/pinctrl-stm32.c +0 -0
index bf16e5814bf8..01c8a646d9a0 100644
@@ -1657,13 +1657,26 @@ static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl, struct fwnode
                 bank->gpio_chip.base = args.args[1];


                /* get the last defined gpio line (offset + nb of pins) */
+               if (args.args[0] >= STM32_GPIO_PINS_PER_BANK ||
+                   args.args[2] > STM32_GPIO_PINS_PER_BANK - args.args[0]) {
+                       fwnode_handle_put(args.fwnode);
+                       return -EINVAL;
+               }
                npins = args.args[0] + args.args[2];
                fwnode_handle_put(args.fwnode);

                while (!fwnode_property_get_reference_args(fwnode, "gpio-ranges", NULL, 3, ++i, &args)) { +                       if (args.args[0] >= STM32_GPIO_PINS_PER_BANK ||
+                           args.args[2] > STM32_GPIO_PINS_PER_BANK - args.args[0]) {
+                               fwnode_handle_put(args.fwnode);
+                               return -EINVAL;
+                       }
                        npins = max(npins, (int)(args.args[0] + args.args[2]));

Quoted 8 lines

                        fwnode_handle_put(args.fwnode);
                }
+
+               if (!npins)
+                       return -EINVAL;
        } else {
                bank_nr = pctl->nbanks;
                bank->gpio_chip.base = bank_nr * STM32_GPIO_PINS_PER_BANK;

Hi Lee,

I agree, this change fixes the case of incorrect values for the DT property "gpio-ranges", values not checked by 'make dtbs_check'.

I thought we should trust the DT as correct, avoiding too many checks in the drivers. Is there any news I have missed?
Or is this a special case, as it involves a potential out-of-bounds array access?

Anyway,

Thanks,
Antonio

Reviewed-by on v1 2/12
#5 lore raw

Re: [PATCH 02/12] pinctrl: imx: Validate pin_id in imx_pinctrl_parse_pin_mmio()

In reply to Lee Jones
Quoted 8 lines · Lee Jones

imx_pinctrl_parse_pin_mmio() derives *pin_id from mux_reg / 4 or conf_reg / 4 and uses it directly to index ipctl->pin_regs[*pin_id] and info->pins[*pin_id] without bounds checking against info->npins. If a device tree entry specifies an out-of-range register offset or sets both mux_reg and conf_reg to 0 on a SoC without ZERO_OFFSET_VALID (making conf_reg == -1 and *pin_id == 0x3fffffff), this results in an out-of-bounds memory access.

Quoted 8 lines

Validate that *pin_id is less than info->npins and return -EINVAL on invalid entries.

Fixes e16415313c9b ("pinctrl: imx: move hard-coding data into device tree")

---

Quoted 77 lines
 drivers/pinctrl/freescale/pinctrl-imx.c | 32 ++++++++++++++++++-------
 1 file changed, 24 insertions(+), 8 deletions(-)
drivers/pinctrl/freescale/pinctrl-imx.c +24 -9
index ebcce9aeac8c..0a825f58448f 100644
@@ -502,15 +502,16 @@ static const struct pinconf_ops imx_pinconf_ops = {
 #define FSL_PIN_SHARE_SIZE 20
 #define FSL_SCU_PIN_SIZE 12
 
-static void imx_pinctrl_parse_pin_mmio(struct imx_pinctrl *ipctl,
-				       unsigned int *pin_id, struct imx_pin *pin,
-				       const __be32 **list_p,
-				       struct device_node *np)
+static int imx_pinctrl_parse_pin_mmio(struct imx_pinctrl *ipctl,
+				      unsigned int *pin_id, struct imx_pin *pin,
+				      const __be32 **list_p,
+				      struct device_node *np)
 {
 	const struct imx_pinctrl_soc_info *info = ipctl->info;
 	struct imx_pin_mmio *pin_mmio = &pin->conf.mmio;
 	struct imx_pin_reg *pin_reg;
 	const __be32 *list = *list_p;
+	unsigned int id;
 	u32 mux_reg, conf_reg;
 	u32 config;
 
@@ -527,7 +528,14 @@ static void imx_pinctrl_parse_pin_mmio(struct imx_pinctrl *ipctl,
 			conf_reg = -1;
 	}
 
-	*pin_id = (mux_reg != -1) ? mux_reg / 4 : conf_reg / 4;
+	id = (mux_reg != -1) ? mux_reg / 4 : conf_reg / 4;
+	if (id >= info->npins) {
+		dev_err(ipctl->dev, "Invalid pin id %u in node %pOF\n",
+			id, np);
+		return -EINVAL;
+	}
+
+	*pin_id = id;
 	pin_reg = &ipctl->pin_regs[*pin_id];
 	pin->pin = *pin_id;
 	pin_reg->mux_reg = mux_reg;
@@ -547,6 +555,8 @@ static void imx_pinctrl_parse_pin_mmio(struct imx_pinctrl *ipctl,
 
 	dev_dbg(ipctl->dev, "%s: 0x%x 0x%08lx", info->pins[*pin_id].name,
 			     pin_mmio->mux_mode, pin_mmio->config);
+
+	return 0;
 }
 
 static int imx_pinctrl_parse_groups(struct device_node *np,
@@ -611,10 +621,16 @@ static int imx_pinctrl_parse_groups(struct device_node *np,
 
 	for (i = 0; i < grp->grp.npins; i++) {
 		pin = &((struct imx_pin *)(grp->data))[i];
-		if (info->flags & IMX_USE_SCU)
+		if (info->flags & IMX_USE_SCU) {
 			info->imx_pinctrl_parse_pin(ipctl, &pins[i], pin, &list);
-		else
-			imx_pinctrl_parse_pin_mmio(ipctl, &pins[i], pin, &list, np);
+		} else {
+			int ret;
+
+			ret = imx_pinctrl_parse_pin_mmio(ipctl, &pins[i], pin,
+							 &list, np);
+			if (ret)
+				return ret;
+		}
 	}
 
 	return 0;
--

2.56.1.911.g883d8bcb20-goog

#9 lore raw

Re: [PATCH 04/12] pinctrl: imx: Propagate DT parsing errors in imx_pinctrl_probe_dt()

In reply to Lee Jones
Quoted 29 lines · Lee Jones

imx_pinctrl_parse_functions() inserts each group descriptor into pctl->pin_group_tree before calling imx_pinctrl_parse_groups() and ignores the return value of imx_pinctrl_parse_groups(). Likewise, imx_pinctrl_probe_dt() ignores the return value of imx_pinctrl_parse_functions(). If a device tree group node is malformed or a memory allocation fails, partially initialised group descriptors with NULL data pointers remain registered in the radix tree and cause a NULL pointer dereference when the pinctrl core or debugfs accesses the group.

Parse each group before inserting it into pctl->pin_group_tree and propagate errors from imx_pinctrl_parse_groups() and imx_pinctrl_parse_functions(). Return 0 when a function node has no groups so boards with empty controller nodes continue to probe as before.

Fixes e566fc11ea76 ("pinctrl: imx: use generic pinctrl helpers for managing groups")
---
 drivers/pinctrl/freescale/pinctrl-imx.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
drivers/pinctrl/freescale/pinctrl-imx.c +0 -0
index 0a825f58448f..b1114dce3485 100644

drivers/pinctrl/freescale/pinctrl-imx.c static int imx_pinctrl_parse_functions(struct device_node *np, · line 661

Quoted 5 lines
drivers/pinctrl/freescale/pinctrl-imx.c +1 -1
@@ -658,7 +658,7 @@ static int imx_pinctrl_parse_functions(struct device_node *np,
 	func->ngroups = of_get_child_count(np);
 	if (func->ngroups == 0) {
 		dev_info(ipctl->dev, "no groups defined in %pOF\n", np);
-		return -EINVAL;
+		return 0;

I think it is correct to allow empty groups, but need seperate patch for it.

Quoted 32 lines
drivers/pinctrl/freescale/pinctrl-imx.c +4 -3
 	}
 
 	group_names = devm_kzalloc_objs(ipctl->dev, *group_names, func->ngroups);
@@ -679,6 +679,10 @@ static int imx_pinctrl_parse_functions(struct device_node *np,
 		if (!grp)
 			return -ENOMEM;
 
+		ret = imx_pinctrl_parse_groups(child, grp, ipctl, i++);
+		if (ret)
+			return ret;
+
 		mutex_lock(&ipctl->mutex);
 		/*
 		 * FIXME: This should use pinctrl_generic_add_group() and not
@@ -687,8 +691,6 @@ static int imx_pinctrl_parse_functions(struct device_node *np,
 		radix_tree_insert(&pctl->pin_group_tree,
 				  ipctl->group_index++, grp);
 		mutex_unlock(&ipctl->mutex);
-
-		imx_pinctrl_parse_groups(child, grp, ipctl, i++);
 	}
 
 	return 0;
@@ -718,7 +720,6 @@ static int imx_pinctrl_probe_dt(struct platform_device *pdev,
 				struct imx_pinctrl *ipctl)
 {
 	struct device_node *np = pdev->dev.of_node;
-	struct device_node *child;
 	struct pinctrl_dev *pctl = ipctl->pctl;
 	u32 nfuncs = 0;
 	u32 i = 0;

drivers/pinctrl/freescale/pinctrl-imx.c static int imx_pinctrl_probe_dt(struct platform_device *pdev, · line 747

Quoted 5 lines
drivers/pinctrl/freescale/pinctrl-imx.c +1 -1
@@ -743,16 +744,21 @@ static int imx_pinctrl_probe_dt(struct platform_device *pdev,
 		pctl->num_groups = of_get_child_count(np);
 	} else {
 		pctl->num_groups = 0;
-		for_each_child_of_node(np, child)
+		for_each_child_of_node_scoped(np, child)

This one fix difference problem, need seperate patch to fix it

Frank

Quoted 24 lines
drivers/pinctrl/freescale/pinctrl-imx.c +8 -4
 			pctl->num_groups += of_get_child_count(child);
 	}
 
 	if (flat_funcs) {
-		imx_pinctrl_parse_functions(np, ipctl, 0);
+		return imx_pinctrl_parse_functions(np, ipctl, 0);
 	} else {
+		int ret;
+
 		i = 0;
-		for_each_child_of_node(np, child)
-			imx_pinctrl_parse_functions(child, ipctl, i++);
+		for_each_child_of_node_scoped(np, child) {
+			ret = imx_pinctrl_parse_functions(child, ipctl, i++);
+			if (ret)
+				return ret;
+		}
 	}
 
 	return 0;
--

2.56.1.911.g883d8bcb20-goog

#12 lore raw

Re: [PATCH 06/12] pinctrl: imx: Serialise MMIO register updates with a spinlock

In reply to Lee Jones
Quoted 9 lines · Lee Jones

The pinctrl core does not serialise .set_mux() and .pin_config_set() callbacks against one another or against concurrent .set_mux() calls. On i.MX platforms that share mux and configuration registers via SHARE_MUX_CONF_REG or share general-purpose select-input registers, concurrent read-modify-write sequences in imx_pmx_set_one_pin_mmio(), imx_pinconf_set_mmio(), and the GPIO direction callbacks can race and overwrite each other's updates.

Add a raw_spinlock_t to struct imx_pinctrl and hold it across MMIO

why choose raw_spinlock_t instead of spinlock_t?

Frank