Thread (20 messages) 20 messages, 5 authors, 2021-02-10

Re: [PATCH v2 2/2] pinctrl: pinmux: Add pinmux-select debugfs file

From: Dan Carpenter <hidden>
Date: 2021-02-10 19:07:24
Also in: lkml, oe-kbuild, oe-kbuild-all

On Wed, Feb 10, 2021 at 07:39:16PM +0100, Geert Uytterhoeven wrote:
Hi Dan,

On Wed, Feb 10, 2021 at 7:21 PM Dan Carpenter [off-list ref] wrote:
quoted
99b2f99aa41aa7 Drew Fustini  2021-02-09  694    buf = devm_kzalloc(pctldev->dev, PINMUX_MAX_NAME * 2, GFP_KERNEL);
99b2f99aa41aa7 Drew Fustini  2021-02-09  695    if (!buf)
99b2f99aa41aa7 Drew Fustini  2021-02-09  696            return -ENOMEM;
99b2f99aa41aa7 Drew Fustini  2021-02-09  697
99b2f99aa41aa7 Drew Fustini  2021-02-09  698    fname = devm_kzalloc(pctldev->dev, PINMUX_MAX_NAME, GFP_KERNEL);
99b2f99aa41aa7 Drew Fustini  2021-02-09  699    if (!fname) {
99b2f99aa41aa7 Drew Fustini  2021-02-09  700            ret = -ENOMEM;
99b2f99aa41aa7 Drew Fustini  2021-02-09  701            goto free_buf;

The gotos are out of order.  They should be in mirror/reverse order of
the allocations:

free_gmane:
        devm_kfree(pctldev->dev, gname);
free_fname:
        devm_kfree(pctldev->dev, fname);
free_buf:
        devm_kfree(pctldev->dev, buf);

But also why do we need to use devm_kfree() at all?  I thought the whole
point of devm_ functions was that they are garbage collected
automatically for you.  Can we not just delete all error handling and
return -ENOMEM here?
No, because the lifetime of the objects allocated here does not match the
lifetime of dev.  If they're not freed here, they will only be freed when the
device is unbound.  As the user can access the sysfs files at will, he can
OOM the system.
Then why not use vanilla kmalloc()?

regards,
dan carpenter
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help