W4118 Operating Systems Instructor: Junfeng Yang.

Slides:



Advertisements
Similar presentations
Memory.
Advertisements

Memory Management Unit
Operating Systems Lecture 10 Issues in Paging and Virtual Memory Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing.
The Linux Kernel: Memory Management
16.317: Microprocessor System Design I
4/14/2017 Discussed Earlier segmentation - the process address space is divided into logical pieces called segments. The following are the example of types.
OS Memory Addressing.
CS 153 Design of Operating Systems Spring 2015
CS 153 Design of Operating Systems Spring 2015
Memory Management (II)
Linux Vs. Windows NT Memory Management Hitesh Kumar
Memory Management and Paging CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CE6105 Linux 作業系統 Linux Operating System 許 富 皓. Chapter 2 Memory Addressing.
CS 104 Introduction to Computer Science and Graphics Problems
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
Memory Management April 28, 2000 Instructor: Gary Kimura.
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.
Computer Organization and Architecture Operating System Support Chapter 8.
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
Memory Management in Windows and Linux &. Windows Memory Management Virtual memory manager (VMM) –Executive component responsible for managing memory.
CS 346 – Chapter 8 Main memory –Addressing –Swapping –Allocation and fragmentation –Paging –Segmentation Commitment –Please finish chapter 8.
Review of Memory Management, Virtual Memory CS448.
Memory Addressing in Linux  Logical Address machine language instruction location  Linear address (virtual address) a single 32 but unsigned integer.
Operating Systems Chapter 8
CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging.
Linux Virtual Memory for Intel Processor
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.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
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.
1 Linux Operating System 許 富 皓. 2 Memory Addressing.
CS 149: Operating Systems March 3 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Virtual Memory 1 1.
1 Memory Management. 2 Fixed Partitions Legend Free Space 0k 4k 16k 64k 128k Internal fragmentation (cannot be reallocated) Divide memory into n (possible.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Hardware.
Paging (continued) & Caching CS-3013 A-term Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Introduction: Memory Management 2 Ideally programmers want memory that is large fast non volatile Memory hierarchy small amount of fast, expensive memory.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Demand Paging.
Processes and Virtual Memory
Memory Management Continued Questions answered in this lecture: What is paging? How can segmentation and paging be combined? How can one speed up address.
OS Memory Addressing. Architecture CPU – Processing units – Caches – Interrupt controllers – MMU Memory Interconnect North bridge South bridge PCI, etc.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 4.
External fragmentation in a paging system Use paging circuitry to map groups of noncontiguous free pages into logically contiguous addresses (remap your.
W4118 Operating Systems Instructor: Junfeng Yang.
Last lecture: VM Implementation
Logistics Homework 5 will be out this evening, due 3:09pm 4/14
Chapter 2: The Linux System Part 4
CSE 120 Principles of Operating
UNIT–IV: Memory Management
Memory Caches & TLB Virtual Memory
Instructor: Junfeng Yang
CSE 153 Design of Operating Systems Winter 2018
Chapter 8: Main Memory.
Main Memory Background Swapping Contiguous Allocation Paging
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
CSE 451: Operating Systems Autumn 2005 Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 451: Operating Systems Autumn 2004 Page Tables, TLBs, and Other Pragmatics Hank Levy 1.
CSE 451 Autumn 2003 November 13 Section.
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 451: Operating Systems Lecture 10 Paging & TLBs
CS703 - Advanced Operating Systems
CSE 451: Operating Systems Winter 2005 Page Tables, TLBs, and Other Pragmatics Steve Gribble 1.
CSE 153 Design of Operating Systems Winter 2019
Chapter 8 & 9 Main Memory and Virtual Memory
Paging Andrew Whitaker CSE451.
CSE 542: Operating Systems
Virtual Memory 1 1.
Presentation transcript:

W4118 Operating Systems Instructor: Junfeng Yang

Logistics  Homework 5 due 3:09pm this Thursday 1

Last lecture: LRU page replacement  Least-Recently-Used: throw out a page that has not been referenced for the longest time  With locality, approximate OPT  However, difficult to implement efficiently  Clock: efficient, approximate LRU  Clock extension: bias toward replacing clean pages  Problem with LRU  Doesn’t work well repeated scans with data set larger than memory  Doesn’t consider frequency LFU LRU-k and LRU-2Q 2

3 Today  Linux Memory Management  Address Space Translation How does Linux translate address?  Address Space Layout What does a process Address Space look like? What does the kernel Address Space look like? How to implement Copy-on-Write (COW)?  Dynamic Memory Allocation How to allocate pages? How to allocate objects?  Virtual Memory How to replace pages? How to load page from disk? 3

Address Translation  Linux uses paging to translate logical addresses to physical addresses. Linux does not use segmentation  More portable since some RISC architectures don’t support segmentation  Hierarchical paging is flexible enough 4

Recall: x86 segmentation and paging hardware  CPU generates logical address (seg, offset)  Given to segmentation unit Which produces linear addresses  Linear address given to paging unit Which generates physical address in main memory Paging units form equivalent of MMU 5

Segmentation  Since x86 segmentation hardware cannot be disabled, Linux just uses NULL mappings  Set segment base to 0x , limit to 0xffffffff  segment offset == linear addresses  arch/i386/kernel/head.S  Linux defines four segments  User code (__USER_CS)  User data (__USER_DS)  Kernel code (__KERNEL_CS)  Kernel data (__KERNEL_DATA)

Segment Protection  Current Privilege level (CPL) specifies privileged mode or user mode  Stored in current code segment descriptor  User code segment: CPL = 3  Kernel code segment: CPL = 0  Descriptor Privilege Level (DPL) specifies protection  Only accessible if CPL <= DPL  Switch between user mode and kernel mode (e.g. system call and return)  Hardware load the corresponding segment selector (__USER_CS or __KERNEL_CS) into register cs 7

Paging  Linux uses 4-level hierarchical paging  A linear address is split into five parts, to seamlessly handle a range of different addressing modes  Page Global Dir  Page Upper Dir  Page Middle Dir  Page Table  Page Offset  Example: 32-bit address space, 4KB page without physical address extension  Page Global dir: 10 bits  Page Upper dir and Page Middle dir are not used  Page Table: 10 bits  Page Offset: 12 bits 8

Page Table Operations  Linux provides data structures and operations to create, delete, read and write page directories  include/asm-i386/pgtable.h  arch/i386/mm/hugetlbpage.c  Naming convention  pgd: Page Global Directory  pmd: Page Middle Directory  pud: Page Upper Directory  pte: Page Table Entry  Example: mk_pte(p, prot) 9

TLB Operations  x86 uses hardware TLB  OS does not manage TLB  Only operation: flush TLB entries  include/asm-i386/tlbflush.h  load cr3: flush all TLB entries  invlpg addr: flush a single TLB entry Much more efficient than flushing all TLB entries 10

11 Today  Linux Memory Management  Address Space Translation How does Linux translate address?  Address Space Layout What does a process Address Space look like? What does the kernel Address Space look like? How to implement Copy-on-Write (COW)?  Dynamic Memory Allocation How to allocate pages? How to allocate objects?  Virtual Memory How to replace pages? How to load page from disk? 11

Recall: Linux Process Address Space User space Kernel space User-mode stack-area Task’s code and data kernel mode user mode Kernel space is also mapped into user space  from user mode to kernel mode, no need to switch address spaces Shared runtime-libraries 0 0xC Kernel data structures 0xFFFFFFFF

Kernel Address Space  Kernel Address Space [3G, 4G)  [0, 896 MB) Physical Memory Mapping Kernel uses this mapping to manage physical memory physical address = logical address – 3G  The rest 128 MB are used for Accessing High Memory Non-contiguous pages Fix-mapped Linear Addresses 13 Physical Memory Mapping vmalloc area vmalloc area Persistent High Memory Mappings Fix-mapped Linear addresses … 0xC xFFFFFFFF

A Cool Trick: Copy-on-Write  In fork(), parent and child often share significant amount of memory  Expensive to do copy all pages  COW Idea: exploit VA to PA indirection  Instead of copying all pages, share parent pages in child address space  If either process writes to shared pages, only then is the page copied  How to detect page write? Mark pages as read-only in both parent and child address space On write, page fault occurs 14

Sharing Pages  copy_process() in kernel/fork.c  copy_mm()  dup_mmap() // copy page tables  copy_page_range() in mm/memory.c  copy_pud_range()  copy_pmd_range()  copy_pte_range()  copy_one_pte() // mark readonly 15

Copy Page on Page Fault  do_page_fault in arch/i386/mm/fault.c  cr2 stores faulting virtual address  handle_mm_fault in mm/memory.c  handle_pte_fault in mm/memory.c  if(write_access)  do_wp_page() 16

17 Today  Linux Memory Management  Address Space Translation How does Linux translate address?  Address Space Layout What does a process Address Space look like? What does the kernel Address Space look like? How to implement Copy-on-Write (COW)?  Dynamic Memory Allocation How to allocate pages? How to allocate objects?  Virtual Memory How to replace pages? How to load page from disk? 17

Dynamic Memory Allocation  How to allocate pages?  Data structures for page allocation  Buddy algorithm for page allocation  How to allocate objects?  Slab allocation 18

Page Descriptor  Keep track of the status of each page frame  struct page, include/linux/mm.h  All stored in mem_map array  Simple mapping between a page and its descriptor  Nth page’s descriptor is mem_map[N]  virt_to_page  page_to_pfn

Memory Zone  Keep track of pages in different zones  struct zone, include/linux/mmzone.h  ZONE_DMA: <16MB  ZONE_NORMAL: 16MB-896MB  ZONE_HIGHMEM: >896MB

Page Allocator  Linux use a buddy allocator for page allocation  Buddy Allocator: Fast, simple allocation for blocks that are 2^n bytes [Knuth 1968]  Data structure: free lists of blocks of pages of size 2^0, 2^1, …  Allocation restrictions: 2^n pages  Allocation of 2^n pages:  Search free lists (n, n+1, n+2, …) for appropriate size Recursively divide larger blocks until reach block of correct size Insert “buddy” blocks into free lists  Free  Recursively coalesce block with buddy if buddy free  Example: allocate a 256-page block  mm/page_alloc.c

Advantages and Disadvantages of Buddy Allocator  Advantages  Fast and simple compared to general dynamic memory allocation  Avoid external fragmentation by keeping free physical pages contiguous Can use paging, but three problems: –DMA bypasses paging –Modifying page table leads to TLB flush –Cannot use “super page” to increase TLB coverage  Disadvantages  Internal fragmentation Allocation of block of k pages when k != 2^n

Slab Allocator  For objects smaller than a page  Implemented on top of page allocator  Each memory region is called a cache  Two types of slab allocator  Fixed-size slab allocator: cache contains objects of same size for frequently allocated objects  General-purpose slab allocator: caches contain objects of size 2^n for less frequently allocated objects For allocation of object with size k, round to nearest 2^n  mm/slab.c

Advantages and Disadvantages of slab allocator  Advantages  Reduce internal fragmentation: many objects in one page  Fast: no need to allocate and free page frames Allocation: no search of objects with the right size for fixed-size allocator; simple search for general- purpose allocator Free: no merge with adjacent free blocks  Disadvantages  Memory overhead for bookkeeping  Internal fragmentation for general-purpose slab allocator

25 Today  Linux Memory Management  Address Space Translation How does Linux translate address?  Address Space Layout What does a process Address Space look like? What does the kernel Address Space look like? How to implement Copy-on-Write (COW)?  Dynamic Memory Allocation How to allocate pages? How to allocate objects?  Virtual Memory How to replace pages? How to load page from disk? 25

Data Structures for Page Replacement  Two lists in struct zone  active_list: hot pages  inactive_list: cold pages  Two bits in struct page  PG_active: is page on active list?  PG_referenced: has page been referenced recently? 26

Functions for Page Replacement  lru_cache_add*(): add to inactive or active list  mark_page_accessed(): called twice to move a page from inactive to active  page_referenced(): test if a page is referenced  refill_inactive_zone(): move pages from active to inactive

When to Replace Pages?  Usually when free_more_memory() in fs/buffer.c called  try_to_free_pages in mm/vmscan.c  shrink_caches  shrink_zone  refill_inactive_zone  shrink_cache  shrink_list  pageout() 28

How to Load Page?  do_page_fault() in arch/i386/mm/fault.c  cr2 stores faulting virtual address  handle_mm_fault() in mm/memory.c  handle_pte_fault()  if(!pte_present(entry))  do_no_page() // demand paging  do_file_page() // file mapped page  do_swap_page() // swapped out page 29