Thursday, December 15, 2011

Kernel Memory Allocation

Describe how KERNEL memory allocation is different than USER memory allocation.Discuss different kernel memory allocation methods like kmalloc() , vmalloc() & kfree().

Complications in kernel memory Allocation:
  • The kernel is limited to about 1GB of virtual and physical memory.
  • The kernel's memory is not pageable.
  • The kernel usually wants physically contiguous memory.
  • Often, the kernel must allocate the memory without sleeping.
  • Mistakes in the kernel have a much higher price than they do elsewhere. 
                                      
Physical Memory Allocation: kmalloc()
#include <linux/slab.h>
void * kmalloc(size_t size, int flags);

flags:
The flags field controls the behavior of memory allocation.
We can divide flags into three groups:
Action modifiers:tell the kernel how to allocate memory. They specify, for example,whether the kernel can sleep (that is, whether the call to kmalloc() can block)in order to satisfy the allocation
zone modifiers :tell the kernel from where the request should be satisfied. For example,some requests may need to be satisfied from memory that hardware can access through direct memory access (DMA).
Types:specify a type of allocation.They group together relevant action and zone modifiers into a single mnemonic.In general,instead of specifying multiple action and zone modifiers, we specify a single type flag.. Nearly all of your kernel memory allocations should specify one of these two flags.

The GFP_ATOMIC flag instructs the memory allocator never to block. Use this flag in situations where it cannot sleep—where it must remain atomic—such as interrupt handlers, bottom halves and process context code that is holding a lock. Because the kernel cannot block the allocation and try to free up sufficient memory to satisfy the request, an allocation specifying GFP_ATOMIC has a lesser chance of succeeding than one that does not. Nonetheless, if your current context is incapable of sleeping, it is your only choice.

Also Discuss how free memory assigned to kernel processes are managed with respect to OS

1.Buddy System
2.Slab Allocation

 

1 comment:

  1. http://www.linuxjournal.com/article/6930

    http://www.win.tue.nl/~aeb/linux/lk/lk-9.html

    https://secure.wikimedia.org/wikipedia/en/wiki/Slab_allocation

    ReplyDelete