Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between kmalloc and kmem_cache_alloc

Tags:

linux-kernel

What is difference between kmem_cache_alloc and kmalloc() in kernel memory allocation? which one is used when?

like image 284
debonair Avatar asked Mar 13 '14 05:03

debonair


People also ask

What is the difference between Kmalloc and Vmalloc?

The kmalloc() function guarantees that the pages are physically contiguous (and virtually contiguous). The vmalloc() function works in a similar fashion to kmalloc() , except it allocates memory that is only virtually contiguous and not necessarily physically contiguous.

When should I use Kmalloc?

It takes address returned by kmalloc while allocation memory as an argument. If memory required in driver needs to be contiguous in physical memory then we have to use kmalloc for allocation of memory. vmalloc is the other call to allocate memory in kernel space as like kmalloc.

What is Linux Kmalloc?

kmalloc is the normal method of allocating memory for objects smaller than page size in the kernel. The flags argument may be one of: GFP_USER - Allocate memory on behalf of user. May sleep. GFP_KERNEL - Allocate normal kernel ram.

Does Kmalloc zero memory?

void *kzalloc(size_t size, gfp_t flags); works like kmalloc, but also zero the memory. kmalloc() will return a memory chunk with size of power of 2 that matches or exceeds len and will return NULL upon failure. The maximum size allocatable by kmalloc() is 1024 pages, or 4MB on x86.


2 Answers

Kmalloc - allocates contiguous region from the physical memory. But keep in mind, allocating and free'ing memory is a lot of work.

Kmem_cache_alloc - Here, your process keeps some copies of the some pre-defined size objects pre-allocated. Say you have struct that you know you will be requiring very frequently, so instead of allocating it from the main memory (kmalloc) when you need it, you already keep multiple copies of it allocated & when you want it, it returns the address of the block already allocated (saves a lot of time). Similarly, when you free it, you don't give it back, it actually isn't free'd, it goes back to the allocated pool so that if some process again asks for it, you can return this address of the already allocated struct.

like image 52
brokenfoot Avatar answered Oct 15 '22 21:10

brokenfoot


kmalloc: It uses the generic slab caches available to any kernel code. so your module will share slab cache with other components in kernel.

kmem_cache_alloc: It will allocate objects from a dedicated slab cache created by kmem_cache_create. If you specifically want a better slab cache management dedicated to your module only, that too for a specific type of objects, use kmem_cache_create followed by kmem_cache_alloc. USB/SCSI drivers use this. kmem_cache_create takes sizeof your object you want to create slab of, a name which appears in /proc/slabinfo and flags to govern behavior of your slab cache.

Ref: https://www.mail-archive.com/[email protected]/msg13191.html & LDD

like image 4
alex Avatar answered Oct 15 '22 21:10

alex