LWN.net Logo

Some new memory allocation flags

Dealing with memory allocation failures is a requirement for all kernel code (and user-space code as well). But there are some places in the kernel where failures cannot be allowed to happen. So it is not uncommon to see kernel code which doesn't take "no" for an answer. As Andrew Morton put it:

There are quite a lot of places in the kernel which will infinitely retry a memory allocation. Generally, they get it wrong.

As a way of helping kernel code get it right, Andrew has created a patch - since merged for 2.5.69 - which adds a new set of __GFP flags for get_free_page() and the other memory allocation functions. These flags are:

__GFP_REPEAT
This flag tells the page allocater to "try harder," repeating failed allocation attempts if need be. Allocations can still fail, but failure should be less likely.

__GFP_NOFAIL
Try even harder; allocations with this flag must not fail. Needless to say, such an allocation could take a long time to satisfy.

__GFP_NORETRY
Failed allocations should not be retried; instead, a failure status will be returned to the caller immediately.

These flags should make memory allocation operations a little more predictable. There is a moral hazard here, however, that programmers will start simply supplying __GFP_NOFAIL instead of making the extra effort to deal with failed allocations. __GFP_NOFAIL has its place, but, in most cases, it is probably better to be able to deal with low-memory situations directly.


(Log in to post comments)

Some new memory allocation flags

Posted Apr 24, 2003 16:46 UTC (Thu) by cpeterso (guest, #305) [Link]


Which flag is the default? I imagine probably __GFP_NORETRY matches the previous behvaior, but Andrew's post says __GFP_NORETRY "isn't used at present (although perhaps it should be, in swapoff). It is mainly for completeness."

So is __GFP_REPEAT the default?

Default behavior

Posted Apr 24, 2003 16:48 UTC (Thu) by corbet (editor, #1) [Link]

The current Linus kernels default to __GFP_REPEAT. Part of Andrew's reasoning for adding these flags, however, is that some other kernels (-aa, for example) behave differently. By specifying the behavior they want, memory allocations can be expected to behave more consistently regardless of the current default behavior.

Copyright © 2003, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds