mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Sverdlin, Alexander" <alexander.sverdlin@siemens.com>
To: "linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>,
	"lee@kernel.org" <lee@kernel.org>,
	"linux-gpio@vger.kernel.org" <linux-gpio@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-omap@vger.kernel.org" <linux-omap@vger.kernel.org>
Subject: Re: [PATCH v2 10/12] pinctrl: single: Free pinconf settings array in pcs_dt_free_map()
Date: Thu, 19 Nov 2026 22:38:03 +0000	[thread overview]
Message-ID: <bf6de7dda99ddb51e98db7cea5f48e77e45e7082.camel@siemens.com> (raw)
In-Reply-To: <20261119205013.2563811-11-lee@kernel.org>

Hi Lee,

On Thu, 2026-11-19 at 20:49 +0000, Lee Jones wrote:
> 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")

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

> Fixes: 9dddb4df90d1 ("pinctrl: single: support generic pinconf")
> Closes: https://sashiko.dev/#/bug/linux-fcedec46-4487-4392-a882-3d3bef07e56f
> Cc: stable@vger.kernel.org
> Signed-off-by: Lee Jones <lee@kernel.org>
> ---
> 
> 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(-)
> 
> diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
> index 1779ffbc1181..1ba692a7ecac 100644
> --- a/drivers/pinctrl/pinctrl-single.c
> +++ b/drivers/pinctrl/pinctrl-single.c
> @@ -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);
>  }

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?
 
>  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

>  	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;
>  }

-- 
Alexander Sverdlin
Siemens AG
www.siemens.com

  reply	other threads:[~2026-11-19 22:38 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 20:48 [PATCH v2 00/12] pinctrl: Fix resource leaks, out-of-bounds accesses, and error handling (batch 2) Lee Jones
2026-11-19 20:48 ` [PATCH v2 01/12] pinctrl: nuvoton: ma35: Unmap reg_base and release clock on probe error Lee Jones
2026-11-19 20:48 ` [PATCH v2 02/12] pinctrl: nuvoton: ma35: Prevent out-of-bounds write in ma35_pinctrl_get_soc_data() Lee Jones
2026-11-19 20:48 ` [PATCH v2 03/12] pinctrl: nuvoton: ma35: Retain child firmware node reference " Lee Jones
2026-11-19 20:49 ` [PATCH v2 04/12] pinctrl: nuvoton: ma35: Assign fwnode to gpio_chip in ma35_gpiolib_register() Lee Jones
2026-11-19 20:49 ` [PATCH v2 05/12] pinctrl: nuvoton: ma35: Avoid leaking map when parent node is missing Lee Jones
2026-11-19 20:49 ` [PATCH v2 06/12] pinctrl: nuvoton: Unmap GPIO bank registers on error in NPCM7xx and NPCM8xx Lee Jones
2026-11-19 20:49 ` [PATCH v2 07/12] pinctrl: nuvoton: npcm8xx: Propagate npcm8xx_gpio_register() error in probe Lee Jones
2026-11-19 20:49 ` [PATCH v2 08/12] pinctrl: nuvoton: npcm7xx: Fix fwnode_irq_get() error check in npcm7xx_gpio_of() Lee Jones
2026-11-19 20:49 ` [PATCH v2 09/12] pinctrl: single: Reject invalid register width in pcs_probe() Lee Jones
2026-11-19 22:38   ` Sverdlin, Alexander
2026-11-19 20:49 ` [PATCH v2 10/12] pinctrl: single: Free pinconf settings array in pcs_dt_free_map() Lee Jones
2026-11-19 22:38   ` Sverdlin, Alexander [this message]
2026-11-19 20:49 ` [PATCH v2 11/12] pinctrl: sunplus: Validate pin_func index in sppctl_dt_node_to_map() Lee Jones
2026-11-19 20:49 ` [PATCH v2 12/12] pinctrl: realtek: rtd1625: Set dcycle_offset to NA for pins without duty-cycle support Lee Jones

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=bf6de7dda99ddb51e98db7cea5f48e77e45e7082.camel@siemens.com \
    --to=alexander.sverdlin@siemens.com \
    --cc=lee@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-omap@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

all inboxes | Powered by JetHome®