All of lore.kernel.org
 help / color / mirror / Atom feed
From: Geert Uytterhoeven <geert@linux-m68k.org>
To: Dan Carpenter <dan.carpenter@oracle.com>
Cc: kbuild@lists.01.org, Drew Fustini <drew@beagleboard.org>,
	Linus Walleij <linus.walleij@linaro.org>,
	"open list:GPIO SUBSYSTEM" <linux-gpio@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Tony Lindgren <tony@atomide.com>,
	Andy Shevchenko <andy.shevchenko@gmail.com>,
	Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Pantelis Antoniou <pantelis.antoniou@konsulko.com>,
	Jason Kridner <jkridner@beagleboard.org>,
	Robert Nelson <robertcnelson@beagleboard.org>,
	kbuild test robot <lkp@intel.com>,
	kbuild-all@lists.01.org
Subject: Re: [PATCH v2 2/2] pinctrl: pinmux: Add pinmux-select debugfs file
Date: Wed, 10 Feb 2021 19:39:16 +0100	[thread overview]
Message-ID: <CAMuHMdUTG-0PMsP--i4KE2RA_zOaQgpUDksvtU8dLPW9dSpoug@mail.gmail.com> (raw)
In-Reply-To: <20210210182044.GY2696@kadam>

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

WARNING: multiple messages have this Message-ID (diff)
From: Geert Uytterhoeven <geert@linux-m68k.org>
To: kbuild-all@lists.01.org
Subject: Re: [PATCH v2 2/2] pinctrl: pinmux: Add pinmux-select debugfs file
Date: Wed, 10 Feb 2021 19:39:16 +0100	[thread overview]
Message-ID: <CAMuHMdUTG-0PMsP--i4KE2RA_zOaQgpUDksvtU8dLPW9dSpoug@mail.gmail.com> (raw)
In-Reply-To: <20210210182044.GY2696@kadam>

[-- Attachment #1: Type: text/plain, Size: 1840 bytes --]

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(a)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

  reply	other threads:[~2021-02-10 18:43 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-10  7:49 [PATCH v2 0/2] pinctrl: pinmux: Add pinmux-select debugfs file Drew Fustini
2021-02-10  7:49 ` [PATCH v2 1/2] pinctrl: use to octal permissions for debugfs files Drew Fustini
2021-02-10  8:30   ` Joe Perches
2021-02-10 10:18     ` Andy Shevchenko
2021-02-10 12:36       ` Joe Perches
2021-02-10 21:21         ` Drew Fustini
2021-02-10 23:12           ` Joe Perches
2021-02-10  8:31   ` Geert Uytterhoeven
2021-02-10 10:14     ` Andy Shevchenko
2021-02-10 10:16   ` Andy Shevchenko
2021-02-10  7:49 ` [PATCH v2 2/2] pinctrl: pinmux: Add pinmux-select debugfs file Drew Fustini
2021-02-10  8:35   ` Geert Uytterhoeven
2021-02-10  9:56   ` Andy Shevchenko
2021-02-10 17:31     ` Drew Fustini
2021-02-10 18:20   ` Dan Carpenter
2021-02-10 18:20     ` Dan Carpenter
2021-02-10 18:20     ` Dan Carpenter
2021-02-10 18:39     ` Geert Uytterhoeven [this message]
2021-02-10 18:39       ` Geert Uytterhoeven
2021-02-10 19:05       ` Dan Carpenter
2021-02-10 19:05         ` Dan Carpenter
2021-02-10 19:05         ` Dan Carpenter
2021-02-10 19:14         ` Drew Fustini
2021-02-10 19:04     ` Drew Fustini
2021-02-10 20:33       ` Dan Carpenter
2021-02-10 20:33         ` Dan Carpenter
2021-02-10 20:33         ` Dan Carpenter
2021-02-10 15:29 kernel test robot

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=CAMuHMdUTG-0PMsP--i4KE2RA_zOaQgpUDksvtU8dLPW9dSpoug@mail.gmail.com \
    --to=geert@linux-m68k.org \
    --cc=alexandre.belloni@bootlin.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=dan.carpenter@oracle.com \
    --cc=drew@beagleboard.org \
    --cc=jkridner@beagleboard.org \
    --cc=kbuild-all@lists.01.org \
    --cc=kbuild@lists.01.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=pantelis.antoniou@konsulko.com \
    --cc=robertcnelson@beagleboard.org \
    --cc=tony@atomide.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.