Skip to contentSkip to conversation

pinctrl: Fix resource leaks, out-of-bounds accesses, and error handling (batch 2)

PATCH In review Lee Jones 2 participants 15 messages Nov 19

Subsystems (8)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

v2 cover pinctrl: Fix resource leaks, out-of-bounds accesses, and error handling (batch 2)

These issues were reported by Sashiko (batch 2).

v1 => v2:
- Rebase onto pinctrl for-next, dropping the manual fwnode_handle_put() in npcm7xx_gpio_of() now covered by __free(fwnode_handle)
- ma35: Explicitly include the I/O mapping header for iounmap()
- ma35: Retain a reference on bank->fwnode in ma35_pinctrl_get_soc_data() (Sashiko)
- ma35: Correct the description of commit 3fd976afe974 in the dt_node_to_map commit message (Sashiko)
- npcm7xx, npcm8xx: Release GPIO bank fwnode_iomap() mappings via devres on error (Sashiko)
- single: Switch *map and settings in pcs_dt_node_to_map() and pcs_parse_pinconf() to unmanaged kcalloc()/kfree() so devres teardown cannot free *map before pcs_dt_free_map() runs

v1: https://lore.kernel.org/all/[email protected]/

Cc Alexander Sverdlin <[email protected]>
Cc "Andreas Färber" <[email protected]>
Cc Andrew Gaylard <[email protected]>
Cc Andrew Jeffery <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Avi Fishman <[email protected]>
Cc Benjamin Fair <[email protected]>
Cc Dvorkin Dmitry <[email protected]>
Cc Haojian Zhuang <[email protected]>
Cc Harshit Mogalapalli <[email protected]>
Cc Jacky Huang <[email protected]>
Cc James Tai <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Nancy Yuen <[email protected]>
Cc Patrick Venture <[email protected]>
Cc Shan-Chun Hung <[email protected]>
Cc Tali Perry <[email protected]>
Cc Tomer Maimon <[email protected]>
Cc Tony Lindgren <[email protected]>
Cc Tzuyi Chang <[email protected]>
Cc Wells Lu <[email protected]>
Cc Yu-Chun Lin <[email protected]>

Lee Jones (12):
pinctrl: nuvoton: ma35: Unmap reg_base and release clock on probe
error
pinctrl: nuvoton: ma35: Prevent out-of-bounds write in

    ma35_pinctrl_get_soc_data()
  pinctrl: nuvoton: ma35: Retain child firmware node reference in
    ma35_pinctrl_get_soc_data()
  pinctrl: nuvoton: ma35: Assign fwnode to gpio_chip in
    ma35_gpiolib_register()
  pinctrl: nuvoton: ma35: Avoid leaking map when parent node is missing
  pinctrl: nuvoton: Unmap GPIO bank registers on error in NPCM7xx and
    NPCM8xx
  pinctrl: nuvoton: npcm8xx: Propagate npcm8xx_gpio_register() error in
    probe
  pinctrl: nuvoton: npcm7xx: Fix fwnode_irq_get() error check in
    npcm7xx_gpio_of()

pinctrl: single: Reject invalid register width in pcs_probe()
pinctrl: single: Free pinconf settings array in pcs_dt_free_map()
pinctrl: sunplus: Validate pin_func index in sppctl_dt_node_to_map()
pinctrl: realtek: rtd1625: Set dcycle_offset to NA for pins without
duty-cycle support

 drivers/pinctrl/nuvoton/pinctrl-ma35.c    | 70 +++++++++++++++++++----
 drivers/pinctrl/nuvoton/pinctrl-npcm7xx.c | 20 +++++--
 drivers/pinctrl/nuvoton/pinctrl-npcm8xx.c | 15 ++++-
 drivers/pinctrl/pinctrl-single.c          | 49 ++++++++--------
 drivers/pinctrl/realtek/pinctrl-rtd1625.c | 12 ++--
 drivers/pinctrl/sunplus/sppctl.c          |  7 ++-
 6 files changed, 126 insertions(+), 47 deletions(-)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#1 lore raw

PATCH v2 1/12 pinctrl: nuvoton: ma35: Unmap reg_base and release clock on probe error

ma35_get_bank_data() maps bank->reg_base via fwnode_iomap() and acquires and enables bank->clk via of_clk_get() and clk_prepare_enable(), neither of which is devres-managed. If of_clk_get() or clk_prepare_enable() fails, bank->reg_base is leaked, and ma35_pinctrl_get_soc_data() ignores the error and continues probing with an uninitialised bank.

Register devm cleanup actions for bank->reg_base and bank->clk, and propagate errors from ma35_get_bank_data() in ma35_pinctrl_get_soc_data().

Fixes f805e356313b ("pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver")

---

v1 => v2: Explicitly include the I/O mapping header for iounmap()

 drivers/pinctrl/nuvoton/pinctrl-ma35.c | 37 ++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 5 deletions(-)
drivers/pinctrl/nuvoton/pinctrl-ma35.c +32 -5
index a79ebf4be2c3..bfa743db8b13 100644
@@ -11,6 +11,7 @@
 #include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/gpio/driver.h>
+#include <linux/io.h>
 #include <linux/mfd/syscon.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
@@ -555,6 +556,22 @@ static int ma35_gpiolib_register(struct platform_device *pdev, struct ma35_pinct
 	return 0;
 }
 
+static void ma35_bank_iounmap(void *data)
+{
+	struct ma35_pin_bank *bank = data;
+
+	iounmap(bank->reg_base);
+	bank->reg_base = NULL;
+}
+
+static void ma35_bank_clk_release(void *data)
+{
+	struct ma35_pin_bank *bank = data;
+
+	clk_disable_unprepare(bank->clk);
+	clk_put(bank->clk);
+}
+
 static int ma35_get_bank_data(struct ma35_pin_bank *bank)
 {
 	int ret;
@@ -563,6 +580,10 @@ static int ma35_get_bank_data(struct ma35_pin_bank *bank)
 	if (!bank->reg_base)
 		return -ENOMEM;
 
+	ret = devm_add_action_or_reset(bank->dev, ma35_bank_iounmap, bank);
+	if (ret)
+		return ret;
+
 	bank->irq = fwnode_irq_get(bank->fwnode, 0);
 
 	bank->nr_pins = MA35_GPIO_PORT_MAX;
@@ -572,10 +593,12 @@ static int ma35_get_bank_data(struct ma35_pin_bank *bank)
 		return PTR_ERR(bank->clk);
 
 	ret = clk_prepare_enable(bank->clk);
-	if (ret)
+	if (ret) {
 		clk_put(bank->clk);
+		return ret;
+	}
 
-	return ret;
+	return devm_add_action_or_reset(bank->dev, ma35_bank_clk_release, bank);
 }
 
 static int ma35_pinctrl_get_soc_data(struct ma35_pinctrl *pctl, struct platform_device *pdev)
@@ -583,7 +606,7 @@ static int ma35_pinctrl_get_soc_data(struct ma35_pinctrl *pctl, struct platform_
 	struct fwnode_handle *child;
 	struct ma35_pin_ctrl *ctrl;
 	struct ma35_pin_bank *bank;
-	int i, id = 0;
+	int i, id = 0, ret;
 
 	ctrl = pctl->ctrl;
 	ctrl->nr_banks = MA35_GPIO_BANK_MAX;
@@ -603,8 +626,12 @@ static int ma35_pinctrl_get_soc_data(struct ma35_pinctrl *pctl, struct platform_
 		bank->fwnode = child;
 		bank->regmap = pctl->regmap;
 		bank->dev = &pdev->dev;
-		if (!ma35_get_bank_data(bank))
-			bank->valid = true;
+		ret = ma35_get_bank_data(bank);
+		if (ret) {
+			fwnode_handle_put(child);
+			return ret;
+		}
+		bank->valid = true;
 		id++;
 	}
 	return 0;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#2 lore raw

PATCH v2 2/12 pinctrl: nuvoton: ma35: Prevent out-of-bounds write in ma35_pinctrl_get_soc_data()

In reply to Lee Jones

ma35_pinctrl_get_soc_data() allocates ctrl->pin_banks with ctrl->nr_banks (MA35_GPIO_BANK_MAX) entries and populates them while iterating over child GPIO controller nodes with for_each_gpiochip_node(). If the Device Tree contains more than MA35_GPIO_BANK_MAX GPIO controller child nodes, id exceeds ctrl->nr_banks and writes past the end of ctrl->pin_banks.

Check that id is within ctrl->nr_banks before indexing ctrl->pin_banks.

Fixes f805e356313b ("pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver")

---

v1 => v2: No changes

 drivers/pinctrl/nuvoton/pinctrl-ma35.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/pinctrl/nuvoton/pinctrl-ma35.c +6 -0
index bfa743db8b13..aff2212c6278 100644
@@ -622,6 +622,12 @@ static int ma35_pinctrl_get_soc_data(struct ma35_pinctrl *pctl, struct platform_
 	}
 
 	for_each_gpiochip_node(&pdev->dev, child) {
+		if (id >= ctrl->nr_banks) {
+			dev_err(&pdev->dev, "Exceeded max GPIO banks (%u)\n",
+				ctrl->nr_banks);
+			fwnode_handle_put(child);
+			return -EINVAL;
+		}
 		bank = &ctrl->pin_banks[id];
 		bank->fwnode = child;
 		bank->regmap = pctl->regmap;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#3 lore raw

PATCH v2 3/12 pinctrl: nuvoton: ma35: Retain child firmware node reference in ma35_pinctrl_get_soc_data()

In reply to Lee Jones

for_each_gpiochip_node() drops its reference on each child firmware node as the loop advances. Storing child directly in bank->fwnode without acquiring a reference leaves bank->fwnode dangling once the iteration moves on, leading to a use-after-free when ma35_gpiolib_register() accesses bank->fwnode.

Acquire a reference on child with fwnode_handle_get() and register a devres action to release it on probe failure or device teardown.

Fixes f805e356313b ("pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver")

---

v1 => v2: New patch, retaining a reference on bank->fwnode (Sashiko)

 drivers/pinctrl/nuvoton/pinctrl-ma35.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/pinctrl/nuvoton/pinctrl-ma35.c +13 -1
index aff2212c6278..c9e09c14a101 100644
@@ -556,6 +556,11 @@ static int ma35_gpiolib_register(struct platform_device *pdev, struct ma35_pinct
 	return 0;
 }
 
+static void ma35_bank_fwnode_release(void *data)
+{
+	fwnode_handle_put(data);
+}
+
 static void ma35_bank_iounmap(void *data)
 {
 	struct ma35_pin_bank *bank = data;
@@ -629,7 +634,14 @@ static int ma35_pinctrl_get_soc_data(struct ma35_pinctrl *pctl, struct platform_
 			return -EINVAL;
 		}
 		bank = &ctrl->pin_banks[id];
-		bank->fwnode = child;
+		bank->fwnode = fwnode_handle_get(child);
+		ret = devm_add_action_or_reset(&pdev->dev,
+					       ma35_bank_fwnode_release,
+					       bank->fwnode);
+		if (ret) {
+			fwnode_handle_put(child);
+			return ret;
+		}
 		bank->regmap = pctl->regmap;
 		bank->dev = &pdev->dev;
 		ret = ma35_get_bank_data(bank);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#4 lore raw

PATCH v2 4/12 pinctrl: nuvoton: ma35: Assign fwnode to gpio_chip in ma35_gpiolib_register()

In reply to Lee Jones

ma35_gpiolib_register() registers all 14 GPIO banks under the same parent platform device without setting bank->chip.fwnode to the child bank node. Consequently, all GPIO banks share the parent pinctrl node for OF/fwnode lookups, preventing consumer drivers from resolving phandles to individual GPIO bank child nodes (&gpiob, &gpioc, etc.).

Set bank->chip.fwnode to bank->fwnode before registering each GPIO chip.

Fixes f805e356313b ("pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver")

---

v1 => v2: No changes

 drivers/pinctrl/nuvoton/pinctrl-ma35.c | 1 +
 1 file changed, 1 insertion(+)
drivers/pinctrl/nuvoton/pinctrl-ma35.c +1 -0
index c9e09c14a101..a92945b7821f 100644
@@ -519,6 +519,7 @@ static int ma35_gpiolib_register(struct platform_device *pdev, struct ma35_pinct
 		bank->irqinten = 0;
 		bank->chip.label = bank->name;
 		bank->chip.parent = &pdev->dev;
+		bank->chip.fwnode = bank->fwnode;
 		bank->chip.request = ma35_gpio_core_to_request;
 		bank->chip.direction_input = ma35_gpio_core_direction_in;
 		bank->chip.direction_output = ma35_gpio_core_direction_out;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#5 lore raw

PATCH v2 5/12 pinctrl: nuvoton: ma35: Avoid leaking map when parent node is missing

In reply to Lee Jones

Commit 3fd976afe974 ("pinctrl: nuvoton: fix a double free in ma35_pinctrl_dt_node_to_map_func()") switched new_map from devm_kcalloc() to unmanaged kcalloc() without freeing new_map on the !parent error path. Because pinctrl_dt_to_map() does not invoke dt_free_map() when dt_node_to_map() returns an error, new_map is leaked if of_get_parent() returns NULL.

Look up the parent node before allocating new_map, and release the parent node reference if the allocation fails.

Fixes 3fd976afe974 ("pinctrl: nuvoton: fix a double free in ma35_pinctrl_dt_node_to_map_func()")

---

v1 => v2:
- Correct description of commit 3fd976afe974 in commit message (Sashiko)

 drivers/pinctrl/nuvoton/pinctrl-ma35.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/pinctrl/nuvoton/pinctrl-ma35.c +7 -5
index a92945b7821f..92c1c40339f1 100644
@@ -202,17 +202,19 @@ static int ma35_pinctrl_dt_node_to_map_func(struct pinctrl_dev *pctldev,
 		return -EINVAL;
 	}
 
+	parent = of_get_parent(np);
+	if (!parent)
+		return -EINVAL;
+
 	map_num += grp->grp.npins;
 	new_map = kzalloc_objs(*new_map, map_num);
-	if (!new_map)
+	if (!new_map) {
+		of_node_put(parent);
 		return -ENOMEM;
+	}
 
 	*map = new_map;
 	*num_maps = map_num;
-	/* create mux map */
-	parent = of_get_parent(np);
-	if (!parent)
-		return -EINVAL;
 
 	setting = grp->data;
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#6 lore raw

PATCH v2 6/12 pinctrl: nuvoton: Unmap GPIO bank registers on error in NPCM7xx and NPCM8xx

In reply to Lee Jones

npcm7xx_gpio_of() and npcm8xx_gpio_fw() map each GPIO bank's registers via unmanaged fwnode_iomap() without unmapping them if a subsequent step in the loop, pinctrl registration, or GPIO registration fails.

Register a devres action after each fwnode_iomap() call so the bank mappings are released automatically on probe failure or device teardown.

Fixes 3b588e43ee5c ("pinctrl: nuvoton: add NPCM7xx pinctrl and GPIO driver")
Fixes acf4884a5717 ("pinctrl: nuvoton: add NPCM8XX pinctrl and GPIO driver")

---

v1 => v2:
- New patch, releasing fwnode_iomap() mappings via devres (Sashiko)

 drivers/pinctrl/nuvoton/pinctrl-npcm7xx.c | 13 +++++++++++++
 drivers/pinctrl/nuvoton/pinctrl-npcm8xx.c | 13 +++++++++++++
 2 files changed, 26 insertions(+)
drivers/pinctrl/nuvoton/pinctrl-npcm7xx.c +13 -0
index 3a80948ac70f..3e763a097b91 100644
@@ -7,6 +7,7 @@
 #include <linux/gpio/driver.h>
 #include <linux/gpio/generic.h>
 #include <linux/interrupt.h>
+#include <linux/io.h>
 #include <linux/irq.h>
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
@@ -1821,6 +1822,13 @@ static const struct pinctrl_desc npcm7xx_pinctrl_desc = {
 	.owner = THIS_MODULE,
 };
 
+static void npcm7xx_gpio_iounmap(void *data)
+{
+	struct npcm7xx_gpio *bank = data;
+
+	iounmap(bank->base);
+}
+
 static int npcm7xx_gpio_of(struct npcm7xx_pinctrl *pctrl)
 {
 	struct gpio_generic_chip_config config;
@@ -1835,6 +1843,11 @@ static int npcm7xx_gpio_of(struct npcm7xx_pinctrl *pctrl)
 		if (!pctrl->gpio_bank[id].base)
 			return -EINVAL;
 
+		ret = devm_add_action_or_reset(dev, npcm7xx_gpio_iounmap,
+					       &pctrl->gpio_bank[id]);
+		if (ret)
+			return ret;
+
 		config = (struct gpio_generic_chip_config) {
 			.dev = dev,
 			.sz = 4,
drivers/pinctrl/nuvoton/pinctrl-npcm8xx.c +13 -0
index 690ba8473bcc..f300c3cd924a 100644
@@ -7,6 +7,7 @@
 #include <linux/gpio/driver.h>
 #include <linux/gpio/generic.h>
 #include <linux/interrupt.h>
+#include <linux/io.h>
 #include <linux/irq.h>
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
@@ -2453,6 +2454,13 @@ static int npcmgpio_add_pin_ranges(struct gpio_chip *chip)
 				      bank->chip.gc.ngpio);
 }
 
+static void npcm8xx_gpio_iounmap(void *data)
+{
+	struct npcm8xx_gpio *bank = data;
+
+	iounmap(bank->base);
+}
+
 static int npcm8xx_gpio_fw(struct npcm8xx_pinctrl *pctrl)
 {
 	struct gpio_generic_chip_config config;
@@ -2467,6 +2475,11 @@ static int npcm8xx_gpio_fw(struct npcm8xx_pinctrl *pctrl)
 		if (!pctrl->gpio_bank[id].base)
 			return dev_err_probe(dev, -ENXIO, "fwnode_iomap id %d failed\n", id);
 
+		ret = devm_add_action_or_reset(dev, npcm8xx_gpio_iounmap,
+					       &pctrl->gpio_bank[id]);
+		if (ret)
+			return ret;
+
 		config = (struct gpio_generic_chip_config) {
 			.dev = dev,
 			.sz = 4,
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#7 lore raw

PATCH v2 7/12 pinctrl: nuvoton: npcm8xx: Propagate npcm8xx_gpio_register() error in probe

In reply to Lee Jones

npcm8xx_pinctrl_probe() logs an error with dev_err_probe() when npcm8xx_gpio_register() fails, but omits the return statement and returns 0 instead. Propagate the error returned by dev_err_probe().

Fixes acf4884a5717 ("pinctrl: nuvoton: add NPCM8XX pinctrl and GPIO driver")

---

v1 => v2: No changes

 drivers/pinctrl/nuvoton/pinctrl-npcm8xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pinctrl/nuvoton/pinctrl-npcm8xx.c +1 -1
index f300c3cd924a..6c124d8129bb 100644
@@ -2598,7 +2598,7 @@ static int npcm8xx_pinctrl_probe(struct platform_device *pdev)
 
 	ret = npcm8xx_gpio_register(pctrl);
 	if (ret < 0)
-		dev_err_probe(dev, ret, "Failed to register gpio\n");
+		return dev_err_probe(dev, ret, "Failed to register gpio\n");
 
 	return 0;
 }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#8 lore raw

PATCH v2 8/12 pinctrl: nuvoton: npcm7xx: Fix fwnode_irq_get() error check in npcm7xx_gpio_of()

In reply to Lee Jones

Commit 693c9ecd8326 ("pinctrl: nuvoton: Reduce use of OF-specific APIs") replaced irq_of_parse_and_map(), which returns 0 on failure, with fwnode_irq_get(), which returns a negative errno on failure (including -EPROBE_DEFER), while leaving the !ret check unchanged. As a result, negative error codes are stored in pctrl->gpio_bank[id].irq and passed as large unsigned IRQ numbers when registering the GPIO chip instead of aborting or deferring probe.

Check for ret < 0 and propagate the error code, matching the NPCM8XX driver.

Fixes 693c9ecd8326 ("pinctrl: nuvoton: Reduce use of OF-specific APIs")

---

v1 => v2:
- Rebase onto pinctrl for-next, dropping manual fwnode_handle_put() now handled by __free(fwnode_handle)

 drivers/pinctrl/nuvoton/pinctrl-npcm7xx.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/pinctrl/nuvoton/pinctrl-npcm7xx.c +3 -4
index 3e763a097b91..72480603ce6b 100644
@@ -1871,10 +1871,9 @@ static int npcm7xx_gpio_of(struct npcm7xx_pinctrl *pctrl)
 		fwnode_handle_put(args.fwnode);
 
 		ret = fwnode_irq_get(child, 0);
-		if (!ret) {
-			dev_err(dev, "No IRQ for GPIO bank %u\n", id);
-			return -EINVAL;
-		}
+		if (ret < 0)
+			return dev_err_probe(dev, ret,
+					     "No IRQ for GPIO bank %u\n", id);
 		pctrl->gpio_bank[id].irq = ret;
 		pctrl->gpio_bank[id].irqbase = id * NPCM7XX_GPIO_PER_BANK;
 		pctrl->gpio_bank[id].pinctrl_id = args.args[0];
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#9 lore raw

PATCH v2 9/12 pinctrl: single: Reject invalid register width in pcs_probe()

In reply to Lee Jones

If the "pinctrl-single,register-width" property specifies an unsupported width (anything other than 8, 16, or 32), pcs_probe() hits the default branch in the switch statement and continues without initialising pcs->read or pcs->write, leading to a divide-by-zero in pcs_allocate_pin_table() or a NULL function pointer dereference when accessing registers.

Validate pcs->width immediately after reading the property and return -EINVAL from the default branch in pcs_probe().

Fixes 8b8b091bf07f ("pinctrl: Add one-register-per-pin type device tree based pinctrl driver")

---

v1 => v2: No changes

 drivers/pinctrl/pinctrl-single.c | 34 ++++++++++++++++----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/pinctrl/pinctrl-single.c +17 -17
index d72ebb186b89..1779ffbc1181 100644
@@ -1832,6 +1832,23 @@ static int pcs_probe(struct platform_device *pdev)
 		return ret;
 	}
 
+	switch (pcs->width) {
+	case 8:
+		pcs->read = pcs_readb;
+		pcs->write = pcs_writeb;
+		break;
+	case 16:
+		pcs->read = pcs_readw;
+		pcs->write = pcs_writew;
+		break;
+	case 32:
+		pcs->read = pcs_readl;
+		pcs->write = pcs_writel;
+		break;
+	default:
+		return -EINVAL;
+	}
+
 	ret = of_property_read_u32(np, "pinctrl-single,function-mask",
 				   &pcs->fmask);
 	if (!ret) {
@@ -1881,23 +1898,6 @@ static int pcs_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, pcs);
 
-	switch (pcs->width) {
-	case 8:
-		pcs->read = pcs_readb;
-		pcs->write = pcs_writeb;
-		break;
-	case 16:
-		pcs->read = pcs_readw;
-		pcs->write = pcs_writew;
-		break;
-	case 32:
-		pcs->read = pcs_readl;
-		pcs->write = pcs_writel;
-		break;
-	default:
-		break;
-	}
-
 	pcs->desc.name = DRIVER_NAME;
 	pcs->desc.pctlops = &pcs_pinctrl_ops;
 	pcs->desc.pmxops = &pcs_pinmux_ops;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Reviewed-by on v2 9/12
#10 lore raw

Re: [PATCH v2 09/12] pinctrl: single: Reject invalid register width in pcs_probe()

Hi Lee!

Quoted 7 lines · Lee Jones

If the "pinctrl-single,register-width" property specifies an unsupported width (anything other than 8, 16, or 32), pcs_probe() hits the default branch in the switch statement and continues without initialising pcs->read or pcs->write, leading to a divide-by-zero in pcs_allocate_pin_table() or a NULL function pointer dereference when accessing registers.

Quoted 7 lines

Validate pcs->width immediately after reading the property and return -EINVAL from the default branch in pcs_probe().

Fixes 8b8b091bf07f ("pinctrl: Add one-register-per-pin type device tree based pinctrl driver")
Quoted 6 lines

---

v1 => v2: No changes

 drivers/pinctrl/pinctrl-single.c | 34 ++++++++++++++++----------------

 1 file changed, 17 insertions(+), 17 deletions(-)
Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
author
#11 lore raw

PATCH v2 10/12 pinctrl: single: Free pinconf settings array in pcs_dt_free_map()

In reply to Lee Jones

pcs_parse_pinconf() allocates a per-map settings array and attaches it to map[i].data.configs.configs, while pcs_dt_node_to_map() allocates *map itself, both via devm_kcalloc() on pcs->dev.

When a consumer device unbinds and re-probes, pcs_dt_free_map() only frees map and leaks the settings array on pcs->dev. Moreover, because pcs_probe() registers the pinctrl device with devres before pinctrl hogs or consumers call pcs_dt_node_to_map(), devres teardown on pcs->dev unbind frees *map and settings before devm_pinctrl_dev_release() invokes pcs_dt_free_map().

Allocate *map and settings with unmanaged kcalloc() and free both with kfree() in pcs_dt_free_map().

Fixes 8b8b091bf07f ("pinctrl: Add one-register-per-pin type device tree based pinctrl driver")
Fixes 9dddb4df90d1 ("pinctrl: single: support generic pinconf")

---

v1 => v2:
- Switch *map and settings to unmanaged kcalloc()/kfree() so devres cannot free *map before pcs_dt_free_map() runs

 drivers/pinctrl/pinctrl-single.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/pinctrl/pinctrl-single.c +8 -7
index 1779ffbc1181..1ba692a7ecac 100644
@@ -311,10 +311,12 @@ static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev,
 static void pcs_dt_free_map(struct pinctrl_dev *pctldev,
 				struct pinctrl_map *map, unsigned num_maps)
 {
-	struct pcs_device *pcs;
+	for (unsigned int i = 0; i < num_maps; i++) {
+		if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
+			kfree(map[i].data.configs.configs);
+	}
 
-	pcs = pinctrl_dev_get_drvdata(pctldev);
-	devm_kfree(pcs->dev, map);
+	kfree(map);
 }
 
 static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
@@ -964,8 +966,7 @@ static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np,
 	func->nconfs = nconfs;
 	conf = &(func->conf[0]);
 	m++;
-	settings = devm_kcalloc(pcs->dev, nconfs, sizeof(unsigned long),
-				GFP_KERNEL);
+	settings = kcalloc(nconfs, sizeof(unsigned long), GFP_KERNEL);
 	if (!settings)
 		return -ENOMEM;
 	s = &settings[0];
@@ -1289,7 +1290,7 @@ static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
 	pcs = pinctrl_dev_get_drvdata(pctldev);
 
 	/* create 2 maps. One is for pinmux, and the other is for pinconf. */
-	*map = devm_kcalloc(pcs->dev, 2, sizeof(**map), GFP_KERNEL);
+	*map = kcalloc(2, sizeof(**map), GFP_KERNEL);
 	if (!*map)
 		return -ENOMEM;
 
@@ -1324,7 +1325,7 @@ static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
 free_pgnames:
 	devm_kfree(pcs->dev, pgnames);
 free_map:
-	devm_kfree(pcs->dev, *map);
+	kfree(*map);
 
 	return ret;
 }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#12 lore raw

Re: [PATCH v2 10/12] pinctrl: single: Free pinconf settings array in pcs_dt_free_map()

Hi Lee,

Quoted 11 lines · Lee Jones

pcs_parse_pinconf() allocates a per-map settings array and attaches it to map[i].data.configs.configs, while pcs_dt_node_to_map() allocates *map itself, both via devm_kcalloc() on pcs->dev.

When a consumer device unbinds and re-probes, pcs_dt_free_map() only frees map and leaks the settings array on pcs->dev. Moreover, because pcs_probe() registers the pinctrl device with devres before pinctrl hogs or consumers call pcs_dt_node_to_map(), devres teardown on pcs->dev unbind frees *map and settings before devm_pinctrl_dev_release() invokes pcs_dt_free_map().

Quoted 4 lines

Allocate *map and settings with unmanaged kcalloc() and free both with kfree() in pcs_dt_free_map().

Fixes 8b8b091bf07f ("pinctrl: Add one-register-per-pin type device tree based pinctrl driver")

Should this have been 8f773bfbdd42
("pinctrl: single: fix possible memory leak when pinctrl_enable() fails") instead?

Quoted 21 lines
Fixes 9dddb4df90d1 ("pinctrl: single: support generic pinconf")

---

v1 => v2:
- Switch *map and settings to unmanaged kcalloc()/kfree() so devres cannot free *map before pcs_dt_free_map() runs

 drivers/pinctrl/pinctrl-single.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/pinctrl/pinctrl-single.c +0 -0
index 1779ffbc1181..1ba692a7ecac 100644
@@ -311,10 +311,12 @@ static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev,
 static void pcs_dt_free_map(struct pinctrl_dev *pctldev,
 				struct pinctrl_map *map, unsigned num_maps)
 {

drivers/pinctrl/pinctrl-single.c static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev, · line 320

Quoted 10 lines
drivers/pinctrl/pinctrl-single.c +5 -3
-	struct pcs_device *pcs;
+	for (unsigned int i = 0; i < num_maps; i++) {
+		if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
+			kfree(map[i].data.configs.configs);
+	}
 
-	pcs = pinctrl_dev_get_drvdata(pctldev);
-	devm_kfree(pcs->dev, map);
+	kfree(map);
 }

So the above pcs_dt_free_map() turns into pinctrl_utils_free_map(), given that the driver never creates CONFIGS_PIN maps.

Would ".dt_free_map = pinctrl_utils_free_map" make sense instead?

drivers/pinctrl/pinctrl-single.c static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np, · line 969

Quoted 8 lines
drivers/pinctrl/pinctrl-single.c +1 -2
 static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
@@ -964,8 +966,7 @@ static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np,
 	func->nconfs = nconfs;
 	conf = &(func->conf[0]);
 	m++;
-	settings = devm_kcalloc(pcs->dev, nconfs, sizeof(unsigned long),
-				GFP_KERNEL);
+	settings = kcalloc(nconfs, sizeof(unsigned long), GFP_KERNEL);

The patch will require a rebase here starting from v7.4-rc2

Quoted 21 lines
drivers/pinctrl/pinctrl-single.c +2 -2
 	if (!settings)
 		return -ENOMEM;
 	s = &settings[0];
@@ -1289,7 +1290,7 @@ static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
 	pcs = pinctrl_dev_get_drvdata(pctldev);
 
 	/* create 2 maps. One is for pinmux, and the other is for pinconf. */
-	*map = devm_kcalloc(pcs->dev, 2, sizeof(**map), GFP_KERNEL);
+	*map = kcalloc(2, sizeof(**map), GFP_KERNEL);
 	if (!*map)
 		return -ENOMEM;
 
@@ -1324,7 +1325,7 @@ static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
 free_pgnames:
 	devm_kfree(pcs->dev, pgnames);
 free_map:
-	devm_kfree(pcs->dev, *map);
+	kfree(*map);
 
 	return ret;
 }
Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
author
#13 lore raw

PATCH v2 11/12 pinctrl: sunplus: Validate pin_func index in sppctl_dt_node_to_map()

In reply to Lee Jones

In sppctl_dt_node_to_map(), the validation loop checks that pin_num is within sppctl_pins_all_sz, but does not validate pin_func (bits 15:8) before indexing sppctl_list_funcs[pin_func] for fully-pinmuxed pins. Check that pin_func is less than sppctl_list_funcs_sz when pin_type selects a mux function.

---

v1 => v2: Collect Andrew's Reviewed-by and Tested-by

 drivers/pinctrl/sunplus/sppctl.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/pinctrl/sunplus/sppctl.c +6 -1
index 8a3fb9b56597..843641528e4e 100644
@@ -867,8 +867,13 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 	for (i = 0; i < (*num_maps); i++) {
 		dt_pin = be32_to_cpu(list[i]);
 		pin_num = FIELD_GET(GENMASK(31, 24), dt_pin);
+		pin_type = FIELD_GET(GENMASK(23, 16), dt_pin);
+		pin_func = FIELD_GET(GENMASK(15, 8), dt_pin);
 
-		if (pin_num >= sppctl_pins_all_sz) {
+		if (pin_num >= sppctl_pins_all_sz ||
+		    (pin_type != SPPCTL_PCTL_G_GPIO &&
+		     pin_type != SPPCTL_PCTL_G_IOPP &&
+		     pin_func >= sppctl_list_funcs_sz)) {
 			dev_err(pctldev->dev, "Invalid pin property at index %d (0x%08x)\n",
 				i, dt_pin);
 			return -EINVAL;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#14 lore raw

PATCH v2 12/12 pinctrl: realtek: rtd1625: Set dcycle_offset to NA for pins without duty-cycle support

In reply to Lee Jones

In rtd1625_main2_sconfigs[], pins gpio_86 through gpio_91 do not support clock duty-cycle adjustment (dcycle_maskbits is 0), but specify 0 instead of NA for dcycle_offset. Because rtd_pconf_parse_conf() tests sconfig_desc->dcycle_offset == NA to determine whether duty-cycle configuration is supported, an offset of 0 causes it to compute GENMASK(0 - 1, 0) and corrupt the pad control register.

Set dcycle_offset to NA for gpio_86 through gpio_91.

Fixes e309dbd523b8 ("pinctrl: realtek: add rtd1625 pinctrl driver")

---

v1 => v2: Collect Yu-Chun's Reviewed-by

 drivers/pinctrl/realtek/pinctrl-rtd1625.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/pinctrl/realtek/pinctrl-rtd1625.c +6 -6
index 88f2be37f743..f6e038e4bbdd 100644
@@ -2993,12 +2993,12 @@ static const struct rtd_pin_sconfig_desc rtd1625_main2_sconfigs[] = {
 	RTK_PIN_SCONFIG(gpio_65, 0x34, 16, 3, 19, 3, 22, 3),
 	RTK_PIN_SCONFIG(gpio_66, 0x38, 3, 3, 6, 3, 9, 3),
 	RTK_PIN_SCONFIG(gpio_67, 0x38, 16, 3, 19, 3, 22, 3),
-	RTK_PIN_SCONFIG(gpio_86, 0x3c, 0, 0, 14, 4, 18, 4),
-	RTK_PIN_SCONFIG(gpio_87, 0x3c, 0, 0, 24, 4, 28, 4),
-	RTK_PIN_SCONFIG(gpio_88, 0x40, 0, 0, 2, 4, 6, 4),
-	RTK_PIN_SCONFIG(gpio_89, 0x40, 0, 0, 12, 4, 16, 4),
-	RTK_PIN_SCONFIG(gpio_90, 0x40, 0, 0, 22, 4, 26, 4),
-	RTK_PIN_SCONFIG(gpio_91, 0x44, 0, 0, 2, 4, 6, 4),
+	RTK_PIN_SCONFIG(gpio_86, 0x3c, NA, 0, 14, 4, 18, 4),
+	RTK_PIN_SCONFIG(gpio_87, 0x3c, NA, 0, 24, 4, 28, 4),
+	RTK_PIN_SCONFIG(gpio_88, 0x40, NA, 0, 2, 4, 6, 4),
+	RTK_PIN_SCONFIG(gpio_89, 0x40, NA, 0, 12, 4, 16, 4),
+	RTK_PIN_SCONFIG(gpio_90, 0x40, NA, 0, 22, 4, 26, 4),
+	RTK_PIN_SCONFIG(gpio_91, 0x44, NA, 0, 2, 4, 6, 4),
 	RTK_PIN_SCONFIG(hif_clk, 0x44, 13, 3, 16, 3, 19, 3),
 	RTK_PIN_SCONFIG(hif_data, 0x48, 3, 3, 6, 3, 9, 3),
 	RTK_PIN_SCONFIG(hif_en, 0x48, 16, 3, 19, 3, 22, 3),
Signature · 1 line
2.56.1.750.gf70dc682cf-goog