1 / 28

Ch 11 Memory Management

제 47 강 : Memory Management. Ch 11 Memory Management. Background. Pages basic unit of memory management Each architecture supports its own page size eg Most 32-bit architectures have 4KB pages The kernel represents every physical page with a struct page. <include/linux/mm.h.

mya
Download Presentation

Ch 11 Memory Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 제47강 : Memory Management Ch 11 Memory Management

  2. Background • Pages • basic unit of memory management • Each architecture supports its own page size eg Most 32-bit architectures have 4KB pages • The kernel represents every physical page with a struct page <include/linux/mm.h status of the page struct page { unsigned long flags; atomoc_t count; struct list_head list; struct list_head lru; struct address_space *mapping; unsigned long index;; union { struct pte_chain *chain; pte_addr_t direct; } pte; unsigned long private; void *virtual; }; usage count of the page inode list, file list, free list LRU list for replacement page’s virtual address

  3. inodeB taskA inodeA IPCA page page page page Page Cache Set of data structures that contains pages backed by files, devices, … page page page page LRU page page page free page page page page <include/linux/mm.h struct page { unsigned long flags; atomoc_t count; struct list_head list; struct list_head lru; struct address_space *mapping; unsigned long index;; union { struct pte_chain *chain; pte_addr_t direct; } pte; unsigned long private; void *virtual; }; usage count of the page inode list, file list, free list LRU list for replacement

  4. Background • Zones • kernel cannot treat all pages as identical. • because some pages cannot be used for certain task • eg early DMA devices could only access 1st 16MB of memory • kernel divides pages into different zones • 3 memory zones in Linux • Each zone is represented by struct zone, • which is defined in <include/linux/mmzone.h>

  5. Smarter Space ManagementStatic Dynamic Allocation • Before: static allocation (allocation size at coding time) • Now: space allocated dynamically on-demand kernel manages space pool Allocated Not Allocated proc[N] file[M] inode[I] proc file Central Space Pool (Heap Storage) inode - static allocation - arrays - not flexible • - dynamic allocation (kernel) • - linked list • flexible [Lions] [Linux]

  6. Heap Storage

  7. Heap - Variable size allocation Heap Storage allocator malloc() deallocator free() • Allows to allocate/free • any size block, • in any order

  8. malloc/free example Kernel Heap Storage allocator struct task_struct *t; struct inode *i; process_create() { /* create a new process. Need a new space for task */ /* allocate a block of task_struct from heap storage */ t = (task_struct) malloc(sizeof(task_struct)) /* link this struct to task list */ } More malloc(sizeof(task_struct) … malloc() sizeof(inode) deallocator inode free() Kernel Heap Storage allocator task malloc() sizeof(t) deallocator inode free() Kernel Heap Storage allocator task malloc() file deallocator inode free()

  9. malloc/free example process_remove() { /* deleting this process. Do not need the task_struct space return t to available memory pool */ free(t) } inode_remove() { /* deleting this file. Do not need the inode space return f to available memory pool */ free(i) } Heap Storage Kernel allocator task malloc() file deallocator inode free() Heap Storage Kernel allocator task malloc() file deallocator inode free()

  10. Heap Storage space pool Allocated Not Allocated proc file variable size any order variable size inode

  11. Problems of HeapExternal Fragmentation free(2) use (3) use (2) use (4) free(2) mfree( ) free(2) use (3) free(2) use (4) free(2) malloc(3) use (3) Enough aggregate heap storage. But no single free block of size 3 is available

  12. Problems of HeapCoalesce free(2) use (3) use (2) use (4) free(2) free(2) use (3) free(2) use (4) free(2) free(2) use (3) free(2) free(4) free(2) 3 independent free (small) blocks?

  13. Problems of HeapCoalesce free(2) use (3) use (2) use (4) free(2) free(2) use (3) free(2) use (4) free(2) free(2) use (3) free(2) free(4) free(2) free(2) use (3) coalesced large free block Join with next (previous) block if they are free & adjacent Make it into single (large) free block Complex data structure & algorithm

  14. Compaction free(2) use (3) free(2) use (4) free(2) free(2) use (4) free(2) free(2) use (3) shift many words Very big overhead though

  15. CPU 1 CPU 2 CPU 3 CPU 0 inode inode file task Heap Storage Performance Enhancement of Heap storage • External fragmentation problem • Coalesce • Compaction • Large overhead for solving external fragmentation! • All SMP CPU’s access, every kernel data structure • Must lock/unlock on every access • Central Bottleneck

  16. Smarter Space ManagementStatic Dynamic Allocation Allocated Not Allocated proc[N] file[M] inode[I] proc file Central Space Pool (Heap Storage) inode [Method 1-Array] [Method 2-Heap] • dynamic allocation (kernel) • Heap becomes a bottleneck • External Fragmentation Overhead - static allocation - not flexible

  17. [Method 3] [Allocate in Slab units -- Big Size, Fixed Size] inode file Heap Allocated Not Allocated Allocated malloc (file) malloc (inode) malloc (file) malloc (inode) malloc (file) malloc (inode) slab unit slab unit slab slab slab

  18. inode object, inode cache file inode Slab Layer Allocated Not Allocated Allocated <inodeCache> <file Cache> inode object slab unit slab unit file object slab slab slab slab free list free list Fixed Size  Simple Space Management Requests for free space  Distributed among Many Caches

  19. emptypartial/full slabfree inode list inode file Slab Layer Allocated Not Allocated Allocated <inodeCache> <file Cache> inode object partial empty slab unit slab unit file object slab slab slab slab free list full free list Fixed Size  Simple Space Management Requests for free space  Distributed among Many Caches

  20. Terminology • Terminologies: “inode“ “page” “inode-table” inode-object slab inode-cache Slab Layer Allocated Not Allocated <inodeCache> slabs slab unit inode object space pool partial full empty slab free space for inode free list

  21. Terminology • Inode Cache has to manage its private free-list “Allocated” to inode cache “Deallocated” back to central storage pool “Occupied” used for file A’s inode “Free” allocated, but not used for any allocate/deallocate Allocated Not Allocated <inodeCache> slab unit occupy/free space pool slab free list allocated but free

  22. Allocated <inodeCache> slab free list Slab layer – how it works • request space for a new inode struct • <case> free list “has” space • there is space in partial (or empty) slabs • allocate from unused struct from slab • <case> free list is empty • does not exist --- any empty or partial slab • invokes page(slab) allocation function • release -- finished using inode struct • marks object as free • If system-wide memory is low • deallocate empty slabs

  23. Allocated <inodeCache> slab free list Slab Layer • Cache Data Structure • each cache is represented by a kmem_cache_s structure • defined in <mm/slab.c> • eg) inode_cachep struct kmem_cache_s { … struct kmem_list3 lists; … } struct kmem_list3 { struct list_head slabs_partial struct list_head slabs_full struct list_head slabs_free … } • each list contains • partial slabs, • full slabs, • free slabs

  24. Slab Layer • Slab Data Structure • each slab is represented by slab structure • defined in <mm/slab.c> Slab Layer struct slab { struct list_head list; unsigned long colouroff; void *s_mem; unsigned int inuse; kmem_bufctl_t free; }; Not Allocated slab unit slab unit slab slab

  25. Allocated <inodeCache> slab free list Slab Layer • Create / Destroy Cache • kmem_cache_t * kmem_cache_create(const char *name, size_t size, size_t offset, unsigned long flags, void (*ctor)(void*, kmem_cache_t *, unsigned long), void (*dtor)(void*, kmem_cache_t *, unsigned long)) • create new cache • int kmem_cache_destroy(kmem_cache_t *cachep) • destroy a cache • must ensure that all slabs in the cache are empty • must ensure that no one access the cache during a call to kmem_cache_destroy()

  26. Allocated <inodeCache> slab free list Slab Layer • Object in a Cache • void * kmem_cache_alloc(kmem_cache_t *cachep, int flags) • returns a pointer to an object for the given cache • void kmem_cache_free(kmem_cache_t *cachep, void *objp) • marks the object in cache as free

  27. Allocated <inodeCache> slab free list • Allocating a Slab to a Cache • allocate slabs only when • no partial/empty slabs exits in cache (1) there are no free object in cache (2) obtain page frame(s) kmem_cache_alloc kmem_cache_grow() kmem_getpages() cache_slabmgmt() cache_init_objs() (3) get a new slab descriptor (4) adds slab descriptor at the end of free slab list

  28. Allocated <inodeCache> slab free list • Releasing a Slab from Cache Slab is released only when (1) available memory grows low (& free some memory) (2) or when a cache is explicitly destroyed • static voidkmem_freepages(kmem_cache_t *cachep, …) • returns all the contiguous page frames used by the slab • static void slab_destroy(kmem_cache_t *cachep, ..) • checks whether this cache has destructor method • for its objects

More Related