Sunday, May 19, 2019

Linux: When to use kmalloc vs kmem_cache_alloc vs Vmalloc?

Read the quick comparison here: 
Linux Memory Management API Quick Primer  [Download PDF]
Gautam Bhanage | www.Bhanage.com | Pub2: GDB2019-001 June 2019

At a high level:
1. kmalloc for all generic memory allocation
2. kmem_cache_alloc for repeatetive structs that need allocations. These structures typically need to be accessed frequently (and are L1 & L2 cache aligned by the kernel).This is done much more efficiently through the slab allocator.
3. vmalloc() - allocates virtually contiguous memory. Not really useful from a linux kernel driver perspective.
Citation info:
Gautam Bhanage, "Linux Memory Management API Quick Primer ",  GDB2019-001 published at www.bhanage.com  June 2019  [Download PDF]