l19 VM Linux
l19 VM Linux
2
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
3
Today
Linux Memory Management
Address Space Translation
• How does Linux translate address?
Virtual Memory
• How to replace pages?
• How to load page from disk?
4
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
5
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
6
Segmentation
Since x86 segmentation hardware cannot be
disabled, Linux just uses NULL mappings
Set segment base to 0x00000000, limit to 0xffffffff
segment offset == linear addresses
arch/i386/kernel/head.S
8
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
9
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)
10
TLB Operations
x86 uses hardware TLB
OS does not manage TLB
11
Today
Linux Memory Management
Address Space Translation
• How does Linux translate address?
Virtual Memory
• How to replace pages?
• How to load page from disk?
12
Recall: Linux Process Address
Space
0xFFFFFFFF Kernel data
kernel mode structures
Kernel space
0
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
Persistent
Physical Fix-mapped
vmalloc …vmalloc High
Memory Linear
area area Memory
Mapping addresses
Mappings
0xC0000000 0xFFFFFFFF
14
A Cool Trick: Copy-on-Write
In fork(), parent and child often share
significant amount of memory
Expensive to do copy all pages
15
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
16
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()
17
Today
Linux Memory Management
Address Space Translation
• How does Linux translate address?
Virtual Memory
• How to replace pages?
• How to load page from disk?
18
Dynamic Memory Allocation
How to allocate pages?
Data structures for page allocation
Buddy algorithm for page allocation
19
Page Descriptor
Keep track of the status of each page
frame
struct page, include/linux/mm.h
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
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
Today
Linux Memory Management
Address Space Translation
• How does Linux translate address?
Virtual Memory
• How to replace pages?
• How to load page from disk?
26
Data Structures for Page
Replacement
Two lists in struct zone
active_list: hot pages
inactive_list: cold pages
27
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()
29
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
30