Thursday, 15 January 2015

Memory Managment in kernel

I am reading Linux Kernel Development for undertsanding memory managment in kernel

-kernel cannot easily deal with memory allocation errors, and the kernel often cannot sleep.

Pages
-The kernel treats physical pages as the basic unit of memory management.
-MMU smallest unit is pages
-Most 32-bit architectures have 4KB pages.
-64-bit architectures have 8KB pages.
-kernel represents every physical page on the system with a struct page structure.
<linux/mm_types.h>.
structpage {
              unsigned long  flags;
              atomic_t   _count;  //stores the usage count of the page(-1 no one use the
              atomic_t _mapcount;                                                                     page)
              unsigned long private;
              struct address_space *mapping;//page cache
              pgoff_t index;
              struct list_head lru;
              void *virtual;(//pointer to virtual address space//NULL if not permanently    mapped)
};

 page_count()
-this structure to keep track of all the pages in the system, because the kernel needs to know whether a page is free
-If page is not free kernel must know who owns the page.
- Possible owners include
user-space processes, dynamically allocated kernel data, static kernel code, the page cache, and so on.

Zones-Because of hardware limitations, the kernel cannot treat all pages as identical. Some pages,because of their physical address in memory, cannot be used for certain tasks.
-to overcome this limitation,the kernel divides pages into different zones(similar properties).

Linux has four primary memory zones:
ZONE_DMA—This zone contains pages that can undergo DMA.
ZONE_DMA32—Like ZOME_DMA, this zone contains pages that can undergo DMA.Unlike ZONE_DMA, these pages are accessible only by 32-bit devices. On some architectures, this zone is a larger subset of memory.
ZONE_NORMAL—This zone contains normal, regularly mapped, pages.
ZONE_HIGHMEM—This zone contains “high memory,” which are pages not permanently mapped into the kernel’s address space.
<linux/mmzone.h>.
-actual use and layout of the memory zones is architecture-dependent.
- 32-bit x86 systems, ZONE_HIGHMEM is all memory above the physical 896MB mark.
-The memory contained in ZONE_HIGHMEM is called high memory. The rest
of the system’s memory is called low memory.
- On x86, for example, ZONE_NORMAL is all physical memory from 16MB to 896MB.
-

No comments:

Post a Comment