where to find the kmalloc implementation

Okash Khawaja okash.khawaja at gmail.com
Wed Jan 9 11:51:40 EST 2019

On Wed, Jan 9, 2019 at 3:17 PM Carter Cheng <cartercheng at gmail.com> wrote:
> Hi,
> I am curious where in the kernel sources would I find the kmalloc implementation? I am curious how GFP_ATOMIC option is implemented.
> Thanks,
> Carter.

Hi Carter,

kmalloc() implementation can be found in include/linux/slab.h.
Interesting part of kmalloc() is the use of kmem_cache. kmem_cache is
basically a pool of memory. kmalloc() maintains a system of
kmem_cache's and allocates memory from those caches to the caller. How
it allocates that memory is determined by type of request (e.g.
GFP_ATOMIC) and "buddy system" algorithm, which is an algorithm
designed to minimise fragmentation and be cache efficient.

>From what I remember, kmalloc system sets aside memory for "emergency"
uses, like kmalloc calls with GFP_ATOMIC. So for normal memory
allocation calls, execution may sleep until memory becomes available,
even though there may be memory available in the emergence cache.

Hope it helps :)


> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

More information about the Kernelnewbies mailing list