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

Geert Uytterhoeven
2021-02-10T18:48:36Z

Hi Dan,

On Wed, Feb 10, 2021 at 7:21 PM Dan Carpenter <dan.carpenter@oracle.com> wrote:
> 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.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds