[PATCH 10/12] pinctrl: cirrus: cs42l43: Fix firmware node reference count underflow
From: Dmitry Torokhov
Date: Sat Dec 05 2026 - 01:39:59 EST
cs42l43_pin_probe() mishandles firmware node attachment and reference
counting across parent configurations. When an OF "pinctrl" child node
is present, commit 9b07cdf86a0b ("pinctrl: cirrus: Fix fwnode leak in
cs42l43_pin_probe()") drops the child reference via devres while the
platform device still holds it, causing platform_device_release() to
put it a second time (with a use-after-free in device_del() or
platform_device_release() if a dynamic OF node was freed early, while
unbinding without deleting the device leaves child->dev set). When an
OF parent lacks the optional "pinctrl" child node, that same commit
unintentionally falls back to attaching the parent codec node instead of
leaving the node unset as introduced by commit d5282a539297 ("pinctrl:
cs42l43: Add support for the cs42l43"). For non-OF parents,
device_set_node() attaches the parent MFD device's firmware node without
taking a reference, causing platform_device_release() to underflow its
reference count and prematurely tear down any attached software node.
Drop device_set_node() and supply the parent device's (or OF "pinctrl"
child's) firmware node via a per-instance pinctrl_desc.fwnode and
gpio_chip.fwnode instead. Look up only the "pinctrl" child node on OF
parents, leaving both fields NULL when it is absent, and release the
probe's firmware node reference when cs42l43_pin_probe() returns instead
of via devres, because pinctrl core and gpiolib (with the companion
gpiolib reference-counting fix) each hold their own reference. Because
pinctrl is registered before gpiochip, devres LIFO teardown also keeps
the node referenced through gpiochip_remove(). Also drop the manual
fwnode->dev assignment so gdev->dev (registered with gpio_chip.fwnode)
manages fw_devlink ownership and clears fwnode->dev on unbind without
violating fw_devlink invariants.
Fixes: d5282a539297 ("pinctrl: cs42l43: Add support for the cs42l43")
Fixes: 9b07cdf86a0b ("pinctrl: cirrus: Fix fwnode leak in cs42l43_pin_probe()")
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
drivers/pinctrl/cirrus/pinctrl-cs42l43.c | 36 +++++++++++++-------------------
1 file changed, 15 insertions(+), 21 deletions(-)
diff --git a/drivers/pinctrl/cirrus/pinctrl-cs42l43.c b/drivers/pinctrl/cirrus/pinctrl-cs42l43.c
index a1c839b35557..97754e028118 100644
--- a/drivers/pinctrl/cirrus/pinctrl-cs42l43.c
+++ b/drivers/pinctrl/cirrus/pinctrl-cs42l43.c
@@ -16,6 +16,7 @@
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
+#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/string_choices.h>
@@ -31,6 +32,7 @@
struct cs42l43_pin {
struct gpio_chip gpio_chip;
+ struct pinctrl_desc pin_desc;
struct device *dev;
struct regmap *regmap;
@@ -530,9 +532,14 @@ static int cs42l43_gpio_add_pin_ranges(struct gpio_chip *chip)
return ret;
}
-static void cs42l43_fwnode_put(void *data)
+static struct fwnode_handle *cs42l43_pin_get_fwnode(struct device *parent)
{
- fwnode_handle_put(data);
+ struct fwnode_handle *fwnode = dev_fwnode(parent);
+
+ if (is_of_node(fwnode))
+ return fwnode_get_named_child_node(fwnode, "pinctrl");
+
+ return fwnode_handle_get(fwnode);
}
static int cs42l43_pin_probe(struct platform_device *pdev)
@@ -540,7 +547,6 @@ static int cs42l43_pin_probe(struct platform_device *pdev)
struct cs42l43 *cs42l43 = dev_get_drvdata(pdev->dev.parent);
struct cs42l43_pin *priv;
struct pinctrl_dev *pctldev;
- struct fwnode_handle *fwnode = dev_fwnode(cs42l43->dev);
int ret;
priv = devm_kzalloc_obj(&pdev->dev, *priv);
@@ -552,6 +558,8 @@ static int cs42l43_pin_probe(struct platform_device *pdev)
priv->shutters_locked = cs42l43->hw_lock;
+ priv->pin_desc = cs42l43_pin_desc;
+
priv->gpio_chip.request = gpiochip_generic_request;
priv->gpio_chip.free = gpiochip_generic_free;
priv->gpio_chip.direction_input = pinctrl_gpio_direction_input;
@@ -565,30 +573,16 @@ static int cs42l43_pin_probe(struct platform_device *pdev)
priv->gpio_chip.base = -1;
priv->gpio_chip.ngpio = CS42L43_NUM_GPIOS;
- if (is_of_node(fwnode)) {
- struct fwnode_handle *child;
-
- child = fwnode_get_named_child_node(fwnode, "pinctrl");
- if (child) {
- ret = devm_add_action_or_reset(&pdev->dev,
- cs42l43_fwnode_put, child);
- if (ret)
- return ret;
-
- if (!child->dev)
- child->dev = priv->dev;
- fwnode = child;
- }
- }
+ struct fwnode_handle *fwnode __free(fwnode_handle) =
+ cs42l43_pin_get_fwnode(cs42l43->dev);
+ priv->pin_desc.fwnode = fwnode;
priv->gpio_chip.fwnode = fwnode;
- device_set_node(priv->dev, fwnode);
-
devm_pm_runtime_enable(priv->dev);
pm_runtime_idle(priv->dev);
- pctldev = devm_pinctrl_register(priv->dev, &cs42l43_pin_desc, priv);
+ pctldev = devm_pinctrl_register(priv->dev, &priv->pin_desc, priv);
if (IS_ERR(pctldev))
return dev_err_probe(priv->dev, PTR_ERR(pctldev),
"Failed to register pinctrl\n");
--
2.56.1.987.g2ec0ab4260-goog