pinctrl: core: fix possible memory leak when pinctrl_enable() fails

[ Upstream commit ae1cf47599 ]

In devm_pinctrl_register(), if pinctrl_enable() fails in pinctrl_register(),
the "pctldev" has not been added to dev resources, so devm_pinctrl_dev_release()
can not be called, it leads memory leak.

Introduce pinctrl_uninit_controller(), call it in the error path to free memory.

Fixes: 5038a66dad ("pinctrl: core: delete incorrect free in pinctrl_enable()")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org>
Link: https://lore.kernel.org/r/20240606023704.3931561-2-yangyingliang@huawei.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Yang Yingliang 2024-06-06 10:37:02 +08:00 committed by Greg Kroah-Hartman
parent dc9bb07e0c
commit 30057fb1e1

View File

@ -2062,6 +2062,14 @@ out_err:
return ERR_PTR(ret);
}
static void pinctrl_uninit_controller(struct pinctrl_dev *pctldev, struct pinctrl_desc *pctldesc)
{
pinctrl_free_pindescs(pctldev, pctldesc->pins,
pctldesc->npins);
mutex_destroy(&pctldev->mutex);
kfree(pctldev);
}
static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
{
pctldev->p = create_pinctrl(pctldev->dev, pctldev);
@ -2142,8 +2150,10 @@ struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
return pctldev;
error = pinctrl_enable(pctldev);
if (error)
if (error) {
pinctrl_uninit_controller(pctldev, pctldesc);
return ERR_PTR(error);
}
return pctldev;
}