Topics / Linux Kernel & Device Drivers / Q28
What do GFP_KERNEL and GFP_ATOMIC mean?
Short answer: They are allocation flags that tell the allocator what it is allowed to do. GFP_KERNEL may sleep to reclaim memory (write back pages, swap, compact), so it may only be used in process context with no spinlocks held; it is the normal choice and rarely fails. GFP_ATOMIC never sleeps and may use emergency reserves, so it can be used in interrupt handlers and under spinlocks, but it fails more often and callers must handle NULL.
| Flag | Sleeps | Use |
|---|---|---|
GFP_KERNEL |
yes | normal kernel allocations in process context |
GFP_ATOMIC |
no | IRQ/softirq context, under spinlocks |
GFP_NOWAIT |
no | like ATOMIC but without emergency reserves; fail fast |
GFP_NOIO |
yes, but no I/O | inside block I/O paths (avoid recursion into the I/O you are servicing) |
GFP_NOFS |
yes, but no filesystem calls | inside filesystem code |
GFP_DMA / GFP_DMA32 |
modifier | memory below 16 MB / 4 GB for devices with limited addressing (prefer the DMA API) |
__GFP_ZERO |
modifier | zeroed memory (kzalloc) |
__GFP_NOWARN, __GFP_RETRY_MAYFAIL, __GFP_NOFAIL |
modifiers | control warnings and retry behaviour |
Best practice: avoid allocations in atomic context altogether where possible: preallocate buffers in probe or open, or use mempools; memalloc_noio_save()/memalloc_nofs_save() scopes are preferred over passing NOIO/NOFS flags everywhere.