Introduction to Memory Management 黃偉修 Linux Kernel.

Slides:



Advertisements
Similar presentations
Memory Management in Linux Anand Sivasubramaniam.
Advertisements

Tutorial 8 March 9, 2012 TA: Europa Shang
CSCC69: Operating Systems
The Linux Kernel: Memory Management
Memory management.
16.317: Microprocessor System Design I
CMPT 300: Final Review Chapters 8 – Memory Management: Ch. 8, 9 Address spaces Logical (virtual): generated by the CPU Physical: seen by the memory.
Memory Management (II)
CE6105 Linux 作業系統 Linux Operating System 許 富 皓. Chapter 2 Memory Addressing.
CMPT 300: Final Review Chapters 8 – Memory Management: Ch. 8, 9 Address spaces Logical (virtual): generated by the CPU Physical: seen by the memory.
Computer Organization and Architecture
03/22/2004CSCI 315 Operating Systems Design1 Virtual Memory Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Main Memory. Background Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only.
Basics of Operating Systems March 4, 2001 Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard.
1 Special Topics on Operating System R. C. Chang.
Memory Management in Windows and Linux &. Windows Memory Management Virtual memory manager (VMM) –Executive component responsible for managing memory.
Memory Management -1 Background Swapping Memory Management Schemes
Review of Memory Management, Virtual Memory CS448.
Operating Systems Chapter 8
8.4 paging Paging is a memory-management scheme that permits the physical address space of a process to be non-contiguous. The basic method for implementation.
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
Lecture 9: Memory Hierarchy Virtual Memory Kai Bu
1 Linux Operating System 許 富 皓. 2 Memory Addressing.
8.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Paging Physical address space of a process can be noncontiguous Avoids.
CS 149: Operating Systems March 3 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations.
University of Amsterdam Computer Systems – virtual memory Arnoud Visser 1 Computer Systems Virtual Memory.
Virtual Memory Additional Slides Slide Source: Topics Address translation Accelerating translation with TLBs class12.ppt.
Review °Apply Principle of Locality Recursively °Manage memory to disk? Treat as cache Included protection as bonus, now critical Use Page Table of mappings.
Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles.
Processes and Virtual Memory
Virtual Memory Review Goal: give illusion of a large memory Allow many processes to share single memory Strategy Break physical memory up into blocks (pages)
What is a Process ? A program in execution.
LECTURE 12 Virtual Memory. VIRTUAL MEMORY Just as a cache can provide fast, easy access to recently-used code and data, main memory acts as a “cache”
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Why Kernel Works? kernel function gets dynamic memory in a fairly straightforward manner _get_free_pages( ) or alloc_pages( ) The simple approaches work.
Memory Management 백 일 우
Chapter 2: The Linux System Part 4
Virtual Memory: Systems
Modeling Page Replacement Algorithms
Virtual Memory Chapter 8.
Lecture 28: Virtual Memory-Address Translation
Virtual Memory: Systems
Memory Management 11/17/2018 A. Berrached:CS4315:UHD.
Chapter 9: Virtual-Memory Management
Virtual Memory: Systems
Operating Systems Lecture November 2018.
Pentium/Linux Memory System
CSC 660: Advanced Operating Systems
Memory Management-I 1.
Main Memory Background Swapping Contiguous Allocation Paging
Modeling Page Replacement Algorithms
Pentium III / Linux Memory System April 4, 2000
Virtual Memory.
Translation Buffers (TLB’s)
Chapter 8: Memory Management strategies
Lecture 37 Syed Mansoor Sarwar
CSE451 Virtual Memory Paging Autumn 2002
Virtual Memory: Systems CSCI 380: Operating Systems
Translation Buffers (TLB’s)
CSE 451 Autumn 2003 November 13 Section.
Special Topics on Operating System
CSCI 380: Operating Systems William Killian
Memory Management in Linux
Translation Buffers (TLBs)
Review What are the advantages/disadvantages of pages versus segments?
Virtual Memory.
CSE 542: Operating Systems
Presentation transcript:

Introduction to Memory Management 黃偉修 Linux Kernel

Outline 4 Virtual memory model 4 Caches 4 Page allocation and Deallcation 4 Swapping Out and Discarding pages

Virtual memory model 4 Why virtual memory ?. Large address Space. Protection. Shared memory 4 how to virtual memory ?. Page. Linear address transform. Demand paging

Virtual memory model 4 Linux Page.There level page Tables. Independent to processor - X86 2-level - Alpha 3-level each platform must provide translation macros that allow the kernel to traverse the page tables for a particular process

Virtual memory model 4 Address Transfer. Ex: 386 processor A 32-bit Linear address is divided as follows: DIR TABLE OFFSET

Physical address is then computed (in hardware) as: CR3 + DIR points to the table_base. table_base + TABLE points to the page_base. physical_address page_base + OFFSET

Format for Page directory and Page table entry: ADDRESS OS 0 0 D A 0 0 U/S R/W P D 1 means page is dirty (undefined for page directory entry). R/W 0 means readonly for user. U/S 1 means user page. P 1 means page is present in memory. A 1 means page has been accessed (set to 0 by aging). OS bits can be used for LRU etc, and are defined by the OS. I.e :When a page is swapped, bits 1-31 are used to mark where a page is stored in swap (bit 0 must be 0).

Virtual memory model 4 Memory Mapping. The link of an image into a processes virtual address space is known as mapping. Every process virtual memory is represent by an mm_struct data structure - information about image - points to a number of vm_area_struct data struct. Vm_area_struct

mm_struct the mm_struct data structure is used to describe the virtual memory of a task or process. struct mm_struct { int count; pgd_t * pgd; unsigned long context; unsigned long start_code, end_code, start_data, end_data; unsigned long start_brk, brk, start_stack, start_mmap; unsigned long arg_start, arg_end, env_start, env_end; unsigned long rss, total_vm, locked_vm; unsigned long def_flags; struct vm_area_struct * mmap; struct vm_area_struct * mmap_avl; struct semaphore mmap_sem; };

vm_area_struct Each vm_area_struct data structure describes an area of virtual memory for a process. struct vm_area_struct { struct mm_struct * vm_mm; /* VM area parameters */ unsigned long vm_start; unsigned long vm_end; pgprot_t vm_page_prot; 保護屬性 unsigned short vm_flags; 存取權限和哪些保護屬性可設定 /* AVL tree of VM areas per task, sorted by address */ short vm_avl_height; struct vm_area_struct * vm_avl_left; struct vm_area_struct * vm_avl_right; /* linked list of VM areas per task, sorted by address */ struct vm_area_struct * vm_next;

/* for areas with inode, the circular list inode->i_mmap */ /* for shm areas, the circular list of attaches */ /* otherwise unused */ struct vm_area_struct * vm_nextt_share; struct vm_area_struct * vm_prev_share; /* more */ struct vm_operations_struct * vm_ops; 運算函數位址 unsigned long vm_offset; 作 memory mapping struct inode * vm_inode; unsigned long vm_pte; /* shared mem */ };

Virtual memory model 4 Demand paging. Access not valid page table entry. Page fault report address and access type. Search vm_area_struct in a AVL tree to check illegal or legal virtual address (send SIGSEGV signal to process). If legal virtual address then check type. Else the page fault is legal case note: differentiate between swap file and somewhere on disk

Virtual memory model 4 How about O.S ?. Physical mode V.S Virtual addressing mode. Physical mode : no page tables / addr transfer. Linux OS run in physical mode

Caches 4 Buffer Cache. the buffer cache is indexed via the device identifier and desire block number 4 Page Cache. Used to speed up access to images and data on disk. Bring page not through file system. Page read ahead

Caches 4 Swap Cache. Only dirty pages are saved in swap file. no need to write it to the swap file if the page is already in the swap file

Caches 4 Hardware Caches. TLB. To avoid three times of memory reference

Page Allocation and Deallocation 4 Some data structure. Mem_map - all of physical pages in system are described by - a list of mem_map_t. Mem_map_t - describes a single physical page in system - field : count ->number of used the page age map_nr ->physical frame number

Page Allocation and Deallocation 4 Some data structure. free _area vector - each element contains information about “block” of page - the block size upwards in power of two i.e. Free_area[0]=1 page per block Free_area[0]=2 page per block Free_area[0]=4 page per block

Map is a point to bitmap to which keeps to track of allocated groups of page of this size bit N is set if the N’th block is free

Page Allocation and Deallocation 4 Buddy algorithm.Allocation - allocation code search free_area for requested size - follow the chain and check the map to find the free block - if no free block, search the twice size chain - break down this block,then free blocks are queue on appropriate queue

Page Allocation and Deallocation 4 Buddy algorithm.Deallocation - whenever a block of pages is freed,the adjacent or buddy block of same size is checked to see if free. - if free then recombine into a bigger block free size.

Page Allocation and Deallocation 4 Buddy algorithm..Conclude allocation tends to fragment memory to ”small” one deallocation tends to recombines pages into “bigger” one

Swapping Out and Discarding pages 4 Kernel swap daemon (kswapd). a special type of process,a kernel thread. Make sure that there enough pages in physical memory. Waiting for the kernel swap “timer” to periodically expire. Two scale : free_pages_high, free_page_low

Swapping Out and Discarding pages 4 when to do ?.num of free pages fallen below free_page_high worse still free_page_low - below free_page_high ->swap free 3 pages - below free_page_low ->swap free 6 page.Timer

Swapping Out and Discarding pages 4 Method.reducing the size of the buffer and page cache - discarding these pages dose not have too many harmful side effect because “caches”!! - check mem_map to see if some page is cached - shared pages are not considered and page can’t in both cache - if not enough cached page then consider shared page 4 Method. Swapping Out System V shared Memorypages

Swapping Out and Discarding pages 4 Method.swapping out and discarding pages - look at each “process” in the system in turn to see if it is a good candidate for swapping - not swap or discard “shared” or “locked” page - not swap out all of the swappable pages of the process - decide by “age” in the mem_map_t (old)

Swapping Out and Discarding pages 4 Method. Swapping Out System V shared Memorypages i.e. kswapd remember which method that it used last time successfully