Download presentation
Presentation is loading. Please wait.
1
Virtual Memory – Paging Techniques
CS 5204 Operating Systems Virtual Memory – Paging Techniques Godmar Back
2
Demand paging Idea: only keep data in memory that’s being used
Needed for virtualization – don’t use up physical memory for data processes don’t access Requires that actual allocation of physical page frames be delayed until first access Many variations Lazy loading of text & data, mmapped pages & newly allocated heap pages Copy-on-write CS 5204 Fall 2015
3
Lazy Loading P1 1 GB 3 GB FFFFFFFF Pintos loads the first process …
Pintos then starts the first process … 1 GB Process faults because code page is not present … kheap Process faults when touching address in data segment … kbss kdata free C kcode ustack (1) user (1) stack page was allocated eagerly data + code pages are noted in page table, but no physical frame has been allocated user (1) user (1) kernel 3 GB kernel used kernel kernel udata (1) ucode (1) CS 5204 Fall 2015
4
Stack Growth P1 1 GB 3 GB FFFFFFFF Pintos loads the first process …
Pintos then starts the first process … 1 GB Process faults because code page is not present … kheap Process calls recursive function or allocates large local variable kbss kdata free C kcode ustack (1) user (1) ustack (2) user (1) page fault at about here user (1) kernel 3 GB kernel used kernel kernel udata (1) ucode (1) CS 5204 Fall 2015
5
Microscopic View of Stack Growth
push $ebp sub $20, $esp push $eax push $ebx esp = 0x8004 esp = 0x8000 Page Fault! esp = 0x7FEC esp = 0x7FE8 esp = 0x7FE4 0x8000 intr0e_stub: … call page_fault() iret void page_fault() { get fault addr determine if it’s close to user $esp Yes: allocate page frame install page in page table No: terminate process } Can resume after page fault (and unless feip is changed) this will retry the faulting instruction (here: push $eax) MMU will walk hardware page table again CS 5204 Fall 2015
6
Fault Resumption Requires that faulting CPU instruction be restartable
Most CPUs are designed this way Very powerful technique Entirely transparent to user program: user program is frozen in time until OS decides what to do Can be used to emulate lots of things Programs that just ignore segmentation violations (!?) (here: resume with next instruction – retrying would fault again) Subpage protection (protect entire page, take fault on access, check if address was to an valid subpage region) Virtual machines (original IBM/360 design was fully virtualizable; vmware, qemu – run entire OS on top of another OS) Garbage collection (detect how recently objects have been accessed) Distributed Shared Memory CS 5204 Fall 2015
7
Distributed Shared Memory
Idea: allows accessing other machine’s memory as if it were local Augment page table to be able to keep track of network locations: local virtual address (remote machine, remote address) On page fault, send request for data to owning machine, receive data, allocate & write to local page, map local page, and resume Process will be able to just use pointers to access all memory distributed across machines – fully transparent Q.: how do you guarantee consistency? Lots of options CS 5204 Fall 2015
8
Heap Growth P1 1 GB 3 GB FFFFFFFF Pintos loads the first process …
Pintos then starts the first process … 1 GB Process faults because code page is not present … kheap Process needs memory to place malloc() objects in kbss kdata free C kcode Process faults when touching new memory ustack (1) user (1) user (1) user (1) kernel 3 GB kernel used Process calls sbrk(addr) kernel udata (2) kernel udata (1) ucode (1) CS 5204 Fall 2015
9
mmap() P1 1 GB 3 GB FFFFFFFF Pintos loads the first process … C0400000
Pintos then starts the first process … 1 GB Process faults because code page is not present … kheap Process opens file, calls mmap(fd, addr) kbss kdata free C kcode Process faults when touching mapped file ustack (1) user (1) user (1) user (1) Page fault handler allocs page, maps it, reads data from disk: ummap (1) kernel 3 GB kernel used kernel kernel udata (1) ucode (1) CS 5204 Fall 2015
10
Copy-On-Write Sometimes, want to create a copy of a page:
Example: Unix fork() creates copies of all parent’s pages in the child Optimization: Don’t copy pages, copy PTEs – now have 2 PTEs pointing to frame Set all PTEs read-only Read accesses succeed On Write access, copy the page into new frame, update PTEs to point to new & old frame Looks like each have their own copy, but postpone actual copying until one is writing the data Hope is at most one will ever touch the data – never have to make actual copy CS 5204 Fall 2015
11
Lazy Loading & Prefetching
Typically want to do some prefetching when faulting in page Reduces latency on subsequent faults Q.: how many pages? which pages? Too much: waste time & space fetching unused pages Too little: pay (relatively large) page fault latency too often Predict which pages the program will access next (how?) Let applications give hints to OS If applications knows Example: madvise(2) Usual conflict: what’s best for application vs what’s best for system as a whole CS 5204 Fall 2015
12
kernel space maps to page-in user space mmap sbrk(POS) page (frame) of
physical DRAM kernel space mapping in currently active page table (1 set per CPU for current process) maps to user virtual page in a process‘s address space, page is present/resident munmap sbrk(NEG) page-out page-in user space user virtual page in a process‘s address space, page is not present; OS will page-in on demand mapping in currently inactive page table (1 set per process) mmap sbrk(POS) unused virtual address space accesses here lead to SIGSEGV Process 1 vaddr kernel virtual address space; accesses here lead to SIGSEGV CS 5204 Fall 2015
13
kernel space user space Process 1 vaddr Physical DRAM Process 3 vaddr
CS 5204 Fall 2015
14
kernel space user space Process 1 vaddr Physical DRAM Process 3 vaddr
CS 5204 Fall 2015
15
kernel space user space Process 1 vaddr Physical DRAM Process 3 vaddr
CS 5204 Fall 2015
16
kernel space user space On-demand Paging Process 1 vaddr Physical DRAM
CS 5204 Fall 2015
17
kernel space user space mmap() Process 1 vaddr Physical DRAM Process 3
CS 5204 Fall 2015
18
kernel space user space read from file evicted to swap Process 1 vaddr
Physical DRAM Process 3 vaddr Process 2 vaddr CS 5204 Fall 2015
19
Page Eviction Suppose page fault occurs, but no free physical frame is there to allocate Must evict frame Find victim frame (how – later) Find & change old page table entry pointing to the victim frame If data in it isn’t already somewhere on disk, write to special area on disk (“swap space”) Install in new page table entry Resume Requires check on page fault if page has been swapped out – fault in if so Some subtleties with locking: How do you prevent a process from writing to a page some other process has chosen to evict from its frame? What do you do if a process faults on a page that another process is in the middle of paging out? CS 5204 Fall 2015
20
Page Eviction Example Process A needs a frame
PTE: process id = ? (if appl.), virtual addr = ?, dirty bit = ?, accessed bit = ?, Process A needs a frame decides it wants this frame Q.: how will it find the PTE, if any, that points to it? victim frame: phys addr = … Linux uses a so-called “rmap” for that that links frames to PTE CS 5204 Fall 2015
21
Managing Swap Space Continuous region on disk
Preferably on separate disk, but typically a partition on same disk Different allocation strategies are possible Simplest: when page must be evicted, allocate swap space for page; deallocate when page is paged back in Or: allocate swap space upfront Should page’s position in swap space change? What if same page is paged out multiple times? Can be managed via bitmap Free/used bits for each page that can be stored Pintos: note 1 page == 8 sectors CS 5204 Fall 2015
22
Locking Frames Aka “pinned” or “wired” pages or frames
If another device outside the CPU (e.g., DMA by network controller) accesses a frame, it cannot be paged out Device driver must tell VM subsystem about this Also required if you want to avoid a page fault while kernel code is accessing a user address, such as during a system call. CS 5204 Fall 2015
23
Accessing User Pointers & Paging
Kernel must check that user pointers are valid P2: easy, just check range & page table Harder when swapping: validity of a pointer may change between check & access (if another process sneaks in and selects frame mapped to an already checked page for eviction) Possible solution: verify & lock, then access, then unlock (Alternative is to certain handle page faults on user addresses in kernel mode) Read carefully. if (verify_user(addr)) process_terminate(); // what if addr’s frame is just now // swapped out by another process? *addr = value; CS 5204 Fall 2015
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.