Re: [patch v2 1/5] mm: add nofail variants of kmalloc kcalloc and kzalloc

David Rientjes
2010-09-05T23:01:53Z

On Thu, 2 Sep 2010, Jiri Slaby wrote:

> > @@ -334,6 +334,57 @@ static inline void *kzalloc_node(size_t size, gfp_t flags, int node)
> > return kmalloc_node(size, flags | __GFP_ZERO, node);
> > }
> >
> > +/**
> > + * kmalloc_nofail - infinitely loop until kmalloc() succeeds.
> > + * @size: how many bytes of memory are required.
> > + * @flags: the type of memory to allocate (see kmalloc).
> > + *
> > + * NOTE: no new callers of this function should be implemented!
> > + * All memory allocations should be failable whenever possible.
> > + */
> > +static inline void *kmalloc_nofail(size_t size, gfp_t flags)
> > +{
> > + void *ret;
> > +
> > + for (;;) {
> > + ret = kmalloc(size, flags);
> > + if (ret)
> > + return ret;
> > + WARN_ON_ONCE(get_order(size) > PAGE_ALLOC_COSTLY_ORDER);
>
> This doesn't work as you expect. kmalloc will warn every time it fails.

It actually does work as I expect since the WARN_ON_ONCE() never even gets
triggered here for any of kmalloc_nofail()'s callers since they all have
sizes small enough that the conditional is never true. The page allocator
implicitly loops forever if the order <= PAGE_ALLOC_COSTLY_ORDER, so this
warning is only emitted if the #define implementation of the page
allocator only changes. That's intended, we want to know the consequences
of our change.

> __GFP_NOFAIL used to disable the warning.

No, it didn't, it was unnecessary for all of the kmalloc_nofail() callers
since they already implicitly loop. No warning is emitted (these are all
GFP_NOFS or GFP_NOIO users, there are no order-4 or larger GFP_ATOMIC
allocators using this interface).

> Actually what's wrong with
> __GFP_NOFAIL? I cannot find a reason in the changelogs why the patches
> are needed.
>

Couple reasons:

- it's unnecessary in the page allocator, it can be implemented at a
higher level, which allows us to remove these branches from the
slowpath,

- it's mostly unnecessary since all users have orders that will
implicitly loop forever anyway (although __GFP_NOFAIL does guarantee
that it won't fail even if we change the looping behavior internally,
these warnings help to isolate cases where it's needed), and