public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: brgl@bgdev.pl, Linus Walleij <linus.walleij@linaro.org>,
	Kent Gibson <warthog618@gmail.com>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	Peter Zijlstra <peterz@infradead.org>,
	akpm@linux-foundation.org, linux-gpio@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: Re: [PATCH v4] gpio: sim: fix an invalid __free() usage
Date: Mon, 25 Sep 2023 09:58:49 +0300	[thread overview]
Message-ID: <ZREvqQdQlE9OBvMC@smile.fi.intel.com> (raw)
In-Reply-To: <CAHk-=wgK+PimVukScvVjoqOdOWWG_e+xCurhP_9SvA_+7scYWw@mail.gmail.com>

On Sat, Sep 23, 2023 at 11:19:08AM -0700, Linus Torvalds wrote:
> On Tue, 19 Sept 2023 at 03:49, Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> >
> > Nope, k*alloc*() returns ZERO or NULL on failure. That's what most developers
> > are missing :-)
> 
> Absolutely not.
> 
> k*alloc() returns NULL on failure. Absolutely nothing else.
> 
> On *success*, it can return the special ZERO_SIZE_PTR. But that is
> *not* a failure at all. It's very much a successful pointer.
> 
> Now, it's a pointer that you can't actually dereference, but that's
> very much intentional. You can't dereference it, because you asked for
> a zero-sized allocation. You got a zero-sized allocation.

"A-ha" moment to me, thank you for elaboration!

> But please never *ever* think it's a failure. It's very much not a
> failure case, and it is very much intentional.
> 
> It's different from NULL exactly *because* it's successful, and
> exactly so that you can write
> 
>      ptr = kmalloc(size);
>      if (!ptr)
>           return -ENOMEM;
> 
> without having to worry about the "size is zero" case.
> 
> The standard user-space "malloc()" library is misdesigned. Surprise
> surprise. The kernel isn't.

-- 
With Best Regards,
Andy Shevchenko



      reply	other threads:[~2023-09-25  6:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-18 14:55 [PATCH v4] gpio: sim: fix an invalid __free() usage Bartosz Golaszewski
2023-09-18 15:31 ` Andy Shevchenko
2023-09-19  7:31   ` brgl
2023-09-19  7:37     ` Kent Gibson
2023-09-19  7:46       ` brgl
2023-09-19 10:49     ` Andy Shevchenko
2023-09-23 18:19       ` Linus Torvalds
2023-09-25  6:58         ` Andy Shevchenko [this message]

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=ZREvqQdQlE9OBvMC@smile.fi.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=bartosz.golaszewski@linaro.org \
    --cc=brgl@bgdev.pl \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=torvalds@linux-foundation.org \
    --cc=warthog618@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox