From: kr494167@gmail.com
To: linusw@kernel.org
Cc: Frank.Li@nxp.com, conor.dooley@microchip.com,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
Surendra Singh Chouhan <kr494167@gmail.com>
Subject: [PATCH v3] pinctrl: generic: free maps on pinctrl_generic_to_map() failure
Date: Wed, 29 Jul 2026 13:24:37 +0530 [thread overview]
Message-ID: <20260729075437.101025-1-kr494167@gmail.com> (raw)
From: Surendra Singh Chouhan <kr494167@gmail.com>
pinctrl_generic_to_map() parses DT configuration and allocates pinctrl
maps via pinctrl_utils_reserve_map().
If subsequent steps (such as pinctrl_utils_add_map_mux(),
pinctrl_generic_add_group(), pinconf_generic_parse_dt_config(), or
pinctrl_utils_add_map_configs()) return an error, *maps may contain
partially allocated map entries. Returning the error directly without
freeing *maps leaks the allocated mapping memory across all drivers
that rely on pinctrl_generic_to_map().
Fix this by calling pinctrl_utils_free_map() and resetting *maps,
*num_maps, and *num_reserved_maps in the error path of
pinctrl_generic_to_map().
Fixes: aaaf31be0426 ("pinctrl: extract pinctrl_generic_to_map() from pinctrl_generic_pins_function_dt_node_to_map()")
Signed-off-by: Surendra Singh Chouhan <kr494167@gmail.com>
---
v3:
- Clean up pinctrl-generic.c changes cleanly to apply cleanly on linusw/pinctrl.git devel branch.
v2:
- Move map cleanup inside pinctrl_generic_to_map() helper in pinctrl-generic.c
so all callers are protected from memory leaks, as suggested by Frank Li.
drivers/pinctrl/pinctrl-generic.c | 25 ++++++++++++++++++-------
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/drivers/pinctrl/pinctrl-generic.c b/drivers/pinctrl/pinctrl-generic.c
index 9759b0186bcc..fd6bdb74028a 100644
--- a/drivers/pinctrl/pinctrl-generic.c
+++ b/drivers/pinctrl/pinctrl-generic.c
@@ -42,33 +42,44 @@ int pinctrl_generic_to_map(struct pinctrl_dev *pctldev, struct device_node *pare
ret = pinctrl_utils_add_map_mux(pctldev, maps, num_reserved_maps, num_maps, group_name,
parent->name);
if (ret < 0)
- return ret;
+ goto err_free_map;
ret = pinctrl_generic_add_group(pctldev, group_name, pins, npins, data);
- if (ret < 0)
- return dev_err_probe(dev, ret, "failed to add group %s: %d\n",
+ if (ret < 0) {
+ dev_err_probe(dev, ret, "failed to add group %s: %d\n",
group_name, ret);
+ goto err_free_map;
+ }
ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &num_configs);
- if (ret)
- return dev_err_probe(dev, ret, "failed to parse pin config of group %s\n",
+ if (ret) {
+ dev_err_probe(dev, ret, "failed to parse pin config of group %s\n",
group_name);
+ goto err_free_map;
+ }
if (num_configs == 0)
return 0;
ret = pinctrl_utils_reserve_map(pctldev, maps, num_reserved_maps, num_maps, reserve);
if (ret)
- return ret;
+ goto err_free_map;
ret = pinctrl_utils_add_map_configs(pctldev, maps, num_reserved_maps, num_maps, group_name,
configs,
num_configs, PIN_MAP_TYPE_CONFIGS_GROUP);
kfree(configs);
if (ret)
- return ret;
+ goto err_free_map;
return 0;
+
+err_free_map:
+ pinctrl_utils_free_map(pctldev, *maps, *num_maps);
+ *maps = NULL;
+ *num_maps = 0;
+ *num_reserved_maps = 0;
+ return ret;
};
EXPORT_SYMBOL_GPL(pinctrl_generic_to_map);
--
2.55.0
reply other threads:[~2026-07-29 7:54 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260729075437.101025-1-kr494167@gmail.com \
--to=kr494167@gmail.com \
--cc=Frank.Li@nxp.com \
--cc=conor.dooley@microchip.com \
--cc=linusw@kernel.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox