Re: [PATCH v4] gpio: sim: fix an invalid __free() usage

From: Andy Shevchenko
Date: Mon Sep 25 2023 - 02:59:02 EST


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@xxxxxxxxxxxxxxx> 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