User-level Memory Chris Gill, David Ferry, Brian Kocoloski

Slides:



Advertisements
Similar presentations
Jaishankar Sundararaman
Advertisements

Memory Management in Linux David Chang OS Lab., NCTU,Taiwan.
Memory Mapping Sarah Diesburg COP5641.
CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging.
Linux Virtual Memory for Intel Processor
Operating Systems ECE344 Ding Yuan Paging Lecture 8: Paging.
System calls for Process management
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 9: Virtual Memory.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
University of Amsterdam Computer Systems – virtual memory Arnoud Visser 1 Computer Systems Virtual Memory.
Processes and Virtual Memory
LINUX 15 : Memory Management
Carnegie Mellon Introduction to Computer Systems / Spring 2009 March 23, 2009 Virtual Memory.
What is a Process ? A program in execution.
System calls for Process management Process creation, termination, waiting.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 4.
W4118 Operating Systems Instructor: Junfeng Yang.
Why Kernel Works? kernel function gets dynamic memory in a fairly straightforward manner _get_free_pages( ) or alloc_pages( ) The simple approaches work.
W4118 Operating Systems Instructor: Junfeng Yang.
Processes David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
CS703 – Advanced Operating Systems By Mr. Farhan Zaidi.
Introduction to Kernel
CS 140 Lecture Notes: Virtual Memory
Paging.
Chapter 2: The Linux System Part 4
Virtual Memory CSSE 332 Operating Systems
Final Review David Ferry, Chris Gill
Chapter 9: Virtual Memory – Part I
Virtual Memory: Systems
CS703 - Advanced Operating Systems
Chapter 9: Virtual Memory
Linux Pipes and FIFOs David Ferry, Chris Gill
Processes David Ferry, Chris Gill
Semester Review Chris Gill CSE 422S - Operating Systems Organization
CSE 120 Principles of Operating
Linux Virtual Filesystem
CSE 153 Design of Operating Systems Winter 2018
Virtual Memory: Systems /18-213/14-513/15-513: Introduction to Computer Systems 18th Lecture, October 25, 2018.
CSE 153 Design of Operating Systems Winter 2018
CSCI206 - Computer Organization & Programming
CS 140 Lecture Notes: Virtual Memory
Virtual Memory: Systems
Linux kernel: Processes, threads and scheduling
Virtual Memory: Systems
Making Virtual Memory Real: The Linux-x86-64 way
Module IV Memory Organization.
Pentium/Linux Memory System
CS 140 Lecture Notes: Virtual Memory
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Pentium III / Linux Memory System April 4, 2000
Virtual Memory.
Instructor: Phil Gibbons
Top Half / Bottom Half Processing
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
How & When The Kernel Runs
Semester Review Brian Kocoloski
Virtual Memory: Systems CSCI 380: Operating Systems
CSE 451 Autumn 2003 November 13 Section.
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Kernel Memory Chris Gill, David Ferry, Brian Kocoloski
CSCI 380: Operating Systems William Killian
Lecture 7: Flexible Address Translation
CSE 153 Design of Operating Systems Winter 2019
CSE 451: Operating Systems Lecture 10 Paging & TLBs
CSE 153 Design of Operating Systems Winter 2019
CS 140 Lecture Notes: Virtual Memory
Processes David Ferry, Chris Gill, Brian Kocoloski
Virtual Memory and Paging
Shared Memory David Ferry, Chris Gill
Dirty COW Race Condition Attack
Presentation transcript:

User-level Memory Chris Gill, David Ferry, Brian Kocoloski CSE 422S - Operating Systems Organization Washington University in St. Louis St. Louis, MO 63130

CSE 422S – Operating Systems Organization Virtual Memory Areas Recall virtual/physical memory relationship Pages are mapped to virtual memory Virtual addresses require translation Virtual Address Translation: Process A Process B Virtual Address Page # Offset 4KB 4KB 4KB 4KB 4KB 4KB Physical Page Phys. Addr CSE 422S – Operating Systems Organization

/proc/<pid>/maps The valid virtual addresses in a process $ cat /proc/self/maps CSE 422S – Operating Systems Organization

Process Address Spaces Each user space process has an address space Pointer to mm_struct is non-NULL in user processes only (i.e., 0 for kernel threads) Stores the list of available memory areas (vm_area_struct , or “VMA”) Identifies code, data, heap, and stack segments Is reference counted (mm_count is 2+ if shared) Address space has a flat range of addresses Segmentation fault occurs if the process accesses an address outside its allowed address segments CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Paging Review PML Linux terminology for 4-level page tables PML: Page Map Level PGD: Page Global Directory PMD: Page Middle Directory PTE: Page Table Entry PGD PMD PTE Physical Page (4KB) CSE 422S – Operating Systems Organization

Forking and Copy-on-Write Recall that in our discussion of process creation, we saw that Linux relies on a technique called copy-on-write (CoW) CoW is used to make process creation fast All virtual memory segments in the parent process can be accessed by the child process But as soon as either the parent or child writes to a memory location, their address spaces point to different physical pages Relies on the virtual memory subsystem (including paging on paged-systems) to CSE 422S – Operating Systems Organization

Metadata in Page Table Entries Index MetaData (r/w/x) (present) Page Address for next level of page table … 342 P: 0 R: 1 W:0 X: 0 0x1234…. 511 Recall the metadata bits within page table entries Present (P): is the next level of page table (or the physical page address, if we’re at the last level) present? R/W/X: three separate bits indicating whether the memory is readable, writable, and/or executable CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Fork + CoW PML PML Process A (“parent”) Process B (“child”) PGD PGD PMD PMD Set PTEs as “unwritable” PTE PTE Physical Page (4KB) CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Fork + CoW PML PML Process A (“parent”) Process B (“child”) PGD PGD PMD PMD Physical Page (4KB) As soon as either A or B writes to the page Page fault (because PTE unwritable) Allocate new physical page Update both PTEs to be writable PTE New Physical Page (4KB) PTE CSE 422S – Operating Systems Organization

Kernel Source Pointers kernel/fork.c _do_fork() call copy_process() copy_process call copy_mm() copy_mm call dup_mm() dup_mm call dup_mmap() dup_mmap call copy_page_range() mm/memory.c copy_page_range Recursively copies all page table entries over this address range CSE 422S – Operating Systems Organization

Shared Memory use is Efficient Shared Memory refers to situations where different processes access same physical pages Same address translation as usual Virtual addresses map to same physical page(s) Page(s) can be readable/ writable by both processes, writable by one process / readable by the other, etc. Process A Virtual Address Page # Offset Process B Virtual Address Page # Offset Physical Page CSE 422S – Operating Systems Organization

Shared Memory Concepts Processes may share physical memory region Communicate by modifying memory values Kernel only invoked at creation & deletion Reads/writes incur standard memory use costs Physical Address (e.g. 0x1000) Process A shm_ptr 1 2 3 5 8 13 Process B shm_ptr CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Shared Memory PML Process A PML Process B Some virtual address in process A maps to the same physical address as some virtual address in process B Do the virtual addresses have to be the same if the physical addresses are? PGD PGD PMD PMD Physical Page (4KB) PTE PTE CSE 422S – Operating Systems Organization

Shared Memory Uses: (v)Forking/Cloning Fork(), vfork(), pthread_create(), etc.: All use sys_clone() system call sys_clone flags (LKD pp. 35) Creating “threads”: CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND i.e., share address space, filesystem metadata, open files, and signal handlers Creating processes (fork): No flags needed Creating processes (vfork): CLONE_VFORK | CLONE_VM CSE 422S – Operating Systems Organization

“vFork” and thread creation PML PML Process A (“parent”) Process B (“child”) PGD Any virtual address maps to the same physical address in both processes A and B Main benefit of vfork() Page tables do not need to be copied in new process/ thread What types of programs would benefit from this? PMD PTE Physical Page (4KB) CSE 422S – Operating Systems Organization

Memory Mapping and Unmapping Kernel creates a new address interval The do_mmap function specifies access permissions, may back it with a file (or may leave it anonymous) The do_munmap function removes the interval from the process address space May use vm_area_cachep (slab allocation) User processes also may initiate memory mapping Via the mmap() C library function Useful for shared memory based communication between processes: see man 7 shm_overview CSE 422S – Operating Systems Organization

Today’s Studio: Using Shared Memory in Linux Three steps to use shared memory Create a shared file descriptor via shm_create() Resize shared region via ftruncate() Map region into process via mmap() Shared regions are identified by name Creator can specify user-level permissions CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Shared Memory Caveats Basic interface: void* (untyped pointer) Programmer must impose organization on shared memory region, manipulate the pointer accordingly E.g., casting pointer to specific type according to how sharing processes have laid out memory use Provides multi-threaded-like environment But concurrency libraries (and others) may or may not work when shared across processes E.g. pthreads’ PTHREAD_PROCESS_SHARED etc. May need to write your own concurrency code! CSE 422S – Operating Systems Organization