Linux Memory


Published on

Published in: Business, Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Linux Memory

    1. 1. Principles of Virtual Memory Virtual Memory, Paging, Segmentation
    2. 2. Overview <ul><li>Virtual Memory </li></ul><ul><li>Paging </li></ul><ul><li>Virtual Memory a nd Linux </li></ul>
    3. 3. 1. Virtual Memory <ul><li>1.1 Why Virtual Memory (VM)? </li></ul><ul><li>1.2 What is VM ? </li></ul><ul><li>1.3 The Mapping Process </li></ul><ul><li>1.4 VM: Features </li></ul><ul><li>1.5 VM: Advantages </li></ul><ul><li>1.6 VM: Disadvantages </li></ul><ul><li>1.7 VM: Implementation </li></ul>
    4. 4. 1.1 Why Virtual Memory (VM)? <ul><li>S hortage of memory </li></ul><ul><ul><li>Efficient memory management needed </li></ul></ul>OS Process 3 Process 1 Process 2 Process 4 Memory <ul><ul><li>Process may be too big for physical memory </li></ul></ul><ul><ul><li>More active processes than physical memory can hold </li></ul></ul><ul><li>Requirements of m ulti programming </li></ul><ul><ul><li>Efficient p rotection scheme </li></ul></ul><ul><ul><li>Simple way of s haring </li></ul></ul>
    5. 5. 1.2 What i s VM? <ul><li>Program: </li></ul><ul><ul><li>.... </li></ul></ul><ul><ul><li>Mov AX, 0xA0F4 </li></ul></ul><ul><ul><li>.... </li></ul></ul>Virtual Memory Physical Memory Virtual Address Physical Address „ Piece“ of Virtual Memory „ Piece“ of Physical Memory Note: It does not matter at which physical address a „piece“ of VM is placed, since the corresponding addresses are mapped by the mapping unit. 0xA0F4 0xC0F4 Mapping Unit (MMU) Table (one per Process)
    6. 6. 1.3 The Mapping Process <ul><li>Usually every process has its own mapping table  own virtual address space (assumed from now on) </li></ul>virtual address check using mapping table <ul><li>Not every „piece“ of VM has to be present in PM </li></ul><ul><ul><li>„ Pieces“ may be loaded from HDD as they are referenced </li></ul></ul><ul><ul><li>Rarely used „pieces“ may be discarded or written out to disk (  swapping) </li></ul></ul>MMU piece in physical memory? memory access fault OS brings „ piece“ in from HDD physical address OS adjusts mapping table translate address yes
    7. 7. 1.4 VM: Features Swapping <ul><li>Danger: Thrashing: </li></ul><ul><ul><li>„ Piece“ just swapped out is immediately requested again </li></ul></ul><ul><ul><li>System swaps in / out all the time , no real work is done </li></ul></ul><ul><li>Thus: „piece“ for swap out has to be chosen carefully </li></ul><ul><ul><li>K eep track of „piece“ usage („age of piece “) </li></ul></ul><ul><ul><li>Hopefully „piece“ used frequently lately will be used again in near future ( principle of locality !) </li></ul></ul>lack of memory no need to swap out complete process!!! find rarely used „piece&quot; adjust mapping table „ piece“ modified? save HDD location of „piece“ discard „ piece“ no write „piece“ out to disk yes
    8. 8. 1.4 VM: Features Protection <ul><li>Each process has its own virtual address space </li></ul><ul><ul><li>Processes invisible to each other </li></ul></ul><ul><ul><li>Process cannot access another processes memory </li></ul></ul><ul><li>MMU checks protection bits on memory access (during address mapping) </li></ul><ul><ul><li>„ Pieces“ can be protected from being written to or being executed or even being read </li></ul></ul><ul><ul><li>System can distinguish different protection levels (user / kernel mode) </li></ul></ul><ul><li>Write protection can be used to implement copy on write (  Sharing) </li></ul>
    9. 9. 1.4 VM: Features Sharing <ul><li>„ Pieces“ of different processes mapped to one single „piece“ of physical memory </li></ul><ul><ul><li>Allows sharing of code (saves memory), e. g . libraries </li></ul></ul><ul><ul><li>Copy on write: „piece“ may be used by several processes until one writ es to it (then that process gets its own copy) </li></ul></ul><ul><ul><li>Simplifies interprocess-communication (IPC) </li></ul></ul>shared memory P iece 2 P iece 1 Virtual memory Process 1 P iece 0 Piece 1 Piece 2 Piece 0 Physical memory Virtual memory Process 2 P iece 1 P iece 0 P iece 2
    10. 10. 1.5 VM: Advantages (1) <ul><li>VM supports </li></ul><ul><ul><li>Swapping </li></ul></ul><ul><ul><ul><li>Rarely used „pieces“ can be discarded or swapped out </li></ul></ul></ul><ul><ul><ul><li>„ Piece“ can be swapped back in to any free piece of physical memory large enough, mapping unit translates addresses </li></ul></ul></ul><ul><ul><li>Protection </li></ul></ul><ul><ul><li>Sharing </li></ul></ul><ul><ul><ul><li>Common data or code may be shared to save memory </li></ul></ul></ul><ul><li>Process need not be in memory as a whole </li></ul><ul><ul><li>No need for complicated overlay techniques (OS does job) </li></ul></ul><ul><ul><li>Process may even be larger than all of physical memory </li></ul></ul><ul><ul><li>Data / code can be read from disk as needed </li></ul></ul>
    11. 11. <ul><li>Code can be placed anywhere in physical memory without relocation (adresses are mapped!) </li></ul><ul><li>Increase d cpu utilization </li></ul><ul><ul><li>more processes can be held in memory (in part)  more processes in ready state (consider: 80% HDD I/O wait time not uncommon) </li></ul></ul>1.5 VM: Advantages (2)
    12. 12. 1.6 VM: Disadvantages <ul><li>Memory requirements (mapping tables) </li></ul><ul><li>Longer memory access times ( mapping table lookup) </li></ul><ul><ul><li>Can be improved using TLB </li></ul></ul>
    13. 13. 1.7 VM: Implementation <ul><li>VM may be implemented using </li></ul><ul><ul><li>Paging </li></ul></ul><ul><ul><li>Segmentation (no) </li></ul></ul><ul><ul><li>Combination of both (no) </li></ul></ul>
    14. 14. 2. Paging <ul><li>2.1 What i s Paging? </li></ul><ul><li>2.2 Paging: Implementation </li></ul><ul><li>2.3 Paging: Features </li></ul><ul><li>2. 4 Paging : Advantage s </li></ul><ul><li>2. 5 Paging : Disadvantages </li></ul><ul><li>2. 6 Summary: Conversion o f a Virtual Address </li></ul>
    15. 15. Valid-Invalid Bit <ul><li>With each page table entry a valid–invalid bit is associated ( v  in-memory, i  not-in-memory) </li></ul><ul><li>Initially valid–invalid bit is set to i on all entries </li></ul><ul><li>Example of a page table snapshot: </li></ul>v v v v i i i … . Frame # valid-invalid bit page table
    16. 16. 2.1 What i s Paging? Page 7 Page 5 Page 4 Page 3 Page 2 Page 1 Page 6 Virtual memory (divided into equal size pages) Page 0 0x00 Page Table (one per process, one entry per page maintained by OS) Page 7 Page 5 Page 4 Page 3 Page 2 Page 1 Page 6 Page 0 v v v Frame 0 Frame 1 Frame 3 Frame 0 Frame 1 Frame 2 Frame 3 Physical memory (divided into equal size page frames) 0x00 v v v
    17. 17. 2.2 Paging: Implementation Typical Page Table Entry other Page Frame # execute x write w read r r w x valid v r w x v referenced re v re modified m re m shared s m s caching disabled c s c super - page su c su process id pid su pid guard data (extended) guard gd g pid g gd g gd other
    18. 18. 2.2 Paging: Implementation Singlelevel Page Tables Problem: Page tables can get very large , e.g. 32 bit address space, 4KB pages  2^20 entries per process  4MB at 4B per entry 64 bit  16777216 GB page table!!!! one entry per page one table per process 0x14 0x 2 Virtual address Page # Offset Physical address 0x14 Offset 0x8 Frame # Page Table Base Register (PTBR) Page Table 0x8 ... 0x0 * L 0x1 * L 0x2 * L L : size of entry
    19. 19. 2.2 Paging: Implementation Multilevel Page Tables not all need be present saves memory table size can be restricted to one page Offset Page #1 Page #2 Page #3 Page Directory Page Middle Directory Page Table Page Frame # Offset Offset Frame # Oversized Super -P age v=0
    20. 20. Page Fault <ul><li>If there is a reference to a page, first reference to that page will trap to operating system: </li></ul><ul><li>page fault </li></ul><ul><li>Operating system looks at another table to decide: </li></ul><ul><ul><li>Invalid reference  abort </li></ul></ul><ul><ul><li>Just not in memory </li></ul></ul><ul><li>Get empty frame </li></ul><ul><li>Swap page into frame </li></ul><ul><li>Reset tables </li></ul><ul><li>Set validation bit = v </li></ul><ul><li>Restart the instruction that caused the page fault </li></ul>
    21. 21. Steps in Handling a Page Fault
    22. 22. 2. 3 Paging: Features Prepaging <ul><li>Process requests consecutive pages (or just one)  OS loads following pages into memory as well (expecting they will also be needed) </li></ul><ul><li>Saves time when large contiguous structures are used (e.g. huge arrays) </li></ul><ul><li>Wastes memory and time case pages not needed </li></ul>VM referenced by process prepaged by OS
    23. 23. 2. 3 Paging: Features Demand Paging <ul><li>On process startup only first page is loaded into physical memory </li></ul><ul><li>Pages are then loaded as referenced </li></ul><ul><li>Saves memory </li></ul><ul><li>But: may cause frequent page faults until process has its working set in physical memory. </li></ul><ul><li>OS may adjust its policy (demand / prepaging) dependent on </li></ul><ul><ul><li>Available free physical memory </li></ul></ul><ul><ul><li>Process types and history </li></ul></ul>
    24. 24. 2. 3 Paging: Features Simplified Swapping <ul><li>Process requires 3 frames </li></ul><ul><li>swap out 3 most seldomly used pages </li></ul>Swapping out the 3 most seldomly used „pieces“ will not work  Swap algo must try to create free pieces as big as possible (costly!) rarely used Process requires memory Paging VM system Non-Paging VM system 1 „piece“ 3 pages PM PM
    25. 25. 2. 4 Paging : Advantages <ul><li>Allocating memory is easy and cheap </li></ul><ul><ul><li>Any free page is ok, OS can take first one out of list it keeps </li></ul></ul><ul><li>Eliminates external fragmentation </li></ul><ul><li>Data (page frames) can be scattered all over PM  pages are mapped appropriately anyway </li></ul><ul><li>Allows demand paging and prepaging </li></ul><ul><li>More efficient swapping </li></ul><ul><ul><li>No need for considerations about fragmentation </li></ul></ul><ul><ul><li>Just swap out page least likely to be used </li></ul></ul>
    26. 26. 2. 5 Paging : Disadvantages <ul><li>Longer memory access times (page table lookup) </li></ul><ul><ul><li>Can be improved using </li></ul></ul><ul><ul><ul><li>TLB </li></ul></ul></ul><ul><ul><ul><li>Guarded page tables </li></ul></ul></ul><ul><ul><ul><li>Inverted page tables </li></ul></ul></ul><ul><li>Memory requirements (one entry per VM page) </li></ul><ul><ul><li>Improve using </li></ul></ul><ul><ul><ul><li>Multilevel page tables and variable page sizes (super - pages) </li></ul></ul></ul><ul><ul><ul><li>Guarded page tables </li></ul></ul></ul><ul><ul><ul><li>Page Table Length Register (PTLR) to limit virtual memory size </li></ul></ul></ul><ul><li>Internal fragmentation </li></ul><ul><ul><li>Yet only an average of about ½ page per contiguous address range </li></ul></ul>
    27. 27. Translation Lookaside Buffer <ul><li>Each virtual memory reference can cause two physical memory accesses </li></ul><ul><ul><li>one to fetch the page table </li></ul></ul><ul><ul><li>one to fetch the data </li></ul></ul><ul><li>To overcome this problem a high-speed cache is set up for page table entries </li></ul><ul><ul><li>called the TLB - Translation Lookaside Buffer </li></ul></ul>
    28. 28. Translation Lookaside Buffer <ul><li>Contains page table entries that have been most recently used </li></ul><ul><li>Functions same way as a memory cache </li></ul>
    29. 29. Translation Lookaside Buffer <ul><li>Given a virtual address, processor examines the TLB </li></ul><ul><li>If page table entry is present (a hit), the frame number is retrieved and the real address is formed </li></ul><ul><li>If page table entry is not found in the TLB (a miss), the page number is used to index the process page table </li></ul>
    30. 30. Translation Lookaside Buffer <ul><li>First checks if page is already in main memory </li></ul><ul><ul><li>if not in main memory a page fault is issued </li></ul></ul><ul><li>The TLB is updated to include the new page entry </li></ul>
    31. 31. 2. 6 Summary: Conversion o f a Virtual Address Virtual address Physical address OS Hard ware no protection fault reference legal? copy on write? HDD I/O complete: interrupt memory full? update page table process into ready state copy page process into blocking state yes page fault no exception to process no TLB page table miss page in mem? hit access rights? yes update TLB yes exception to process no swap out a page yes HDD I/O read req. no yes bring in page from HDD ! process into blocking state
    32. 32. 5. Virtual memory a nd Linux <ul><li>5.1 Why VM under Linux? </li></ul><ul><li>5.2 The Linux VM System </li></ul><ul><li>5.3 The Linux Protection Scheme </li></ul><ul><li>5.4 The Linux Paging System </li></ul>
    33. 33. 5.1 Why VM under Linux? <ul><li>Linux is a multitasking, multiuser OS. It requires: </li></ul><ul><ul><li>Protection </li></ul></ul><ul><ul><li>Ability to ensure pseudo-parallel execution (even if cumulated process size greater than physical memory) </li></ul></ul><ul><ul><li>Efficient IPC methods (sharing) </li></ul></ul><ul><li>Good solution: virtual memory </li></ul>
    34. 34. 5.2 The Linux VM System <ul><li>Kernel runs in physical addressing mode, maintains VM system </li></ul><ul><li>Basically a paging system </li></ul><ul><li>Some remainders of a CoSP scheme present: </li></ul><ul><ul><li>Process memory segmented into kernel/user memory </li></ul></ul><ul><ul><li>Process in user mode (  5.3) may not access kernel memory </li></ul></ul><ul><ul><li>V 2.0 defined code and data segments for each kernel and user mode </li></ul></ul><ul><ul><li>V 2.2 still defines those segments but for complete virtual address space </li></ul></ul>
    35. 35. 5.3 The Linux Protection Scheme <ul><li>Linux uses two modes: kernel and user mode </li></ul><ul><ul><li>Makes no use of elaborate protection scheme x86 processors provide (only uses ring 0 (kernel) and ring 3 (user)) </li></ul></ul><ul><li>Programs are all started in user mode </li></ul><ul><li>Program needs to use system resources  must make system call (per software interrupt)  kernel code is executed on behalf of process </li></ul><ul><li>Kernel processes permanently run in kernel mode </li></ul>
    36. 36. 5.4 The Linux Paging System <ul><li>Linux employs architecture independent memory management code </li></ul><ul><li>Linux uses 3-level paging system </li></ul><ul><ul><li>Intel x86 system: only 2-level paging </li></ul></ul><ul><ul><ul><li>Entry in page directory is treated as page middle directory with only one entry </li></ul></ul></ul><ul><ul><ul><li>4 Mb pages on some Intel systems are used (e.g. for graphics memory) </li></ul></ul></ul><ul><li>Linux uses valid, protection, referenced, modified bits </li></ul><ul><li>Employs copy on write and demand paging </li></ul>
    37. 37. Windows Paging Policy <ul><li>Demand paging without pre-paging </li></ul><ul><li>Maintain a certain number of free page frames </li></ul><ul><li>For 32-bit machine, each process has 4 GB of virtual address space </li></ul><ul><li>Backing store – disk space is not assigned to page until it is paged out </li></ul><ul><li>Uses working sets (per process) </li></ul><ul><ul><li>Consists of pages mapped into memory and can be accessed without page fault </li></ul></ul><ul><ul><li>Has min/max size range that changes over time </li></ul></ul><ul><ul><ul><li>If page fault occurs and working set < min, add page </li></ul></ul></ul><ul><ul><ul><li>If page fault occurs and working set > max, evict page from working set and add new page </li></ul></ul></ul><ul><ul><ul><li>If too many page faults, then increase size of working set </li></ul></ul></ul><ul><li>When evicting pages, </li></ul><ul><ul><li>Evict from large processes that have been idle for a long time before small active processes </li></ul></ul><ul><ul><li>Consider foreground process last </li></ul></ul>
    38. 38. Virtual-address Space
    39. 39. Shared Library Using Virtual Memory
    40. 40. Caches <ul><li>If you were to implement a system using the above theoretical model then it would work, but not particularly efficiently. Both operating system and processor designers try hard to extract more performance from the system. Apart from making the processors, memory and so on faster the best approach is to maintain caches of useful information and data that make some operations faster. Linux uses a number of memory management related caches: </li></ul>
    41. 41. <ul><li>Buffer Cache </li></ul><ul><ul><li>The buffer cache contains data buffers that are used by the block device drivers. </li></ul></ul><ul><li>Page Cache </li></ul><ul><ul><li>This is used to speed up access to images and data on disk. </li></ul></ul><ul><li>Swap Cache </li></ul><ul><ul><li>Only modified (or dirty ) pages are saved in the swap file. </li></ul></ul><ul><li>Hardware Caches </li></ul>Hardware Caches
    42. 42. Linux Page Tables <ul><li>Linux assumes that there are three levels of page tables </li></ul>