lectures.alex.balgavy.eu

Lecture notes from university.
git clone git://git.alex.balgavy.eu/lectures.alex.balgavy.eu.git
Log | Files | Refs | Submodules

index.md (2306B)


      1 +++
      2 title = 'Paging'
      3 +++
      4 
      5 # Paging
      6 ## Page faults
      7 Special kind of exception, IV 14.
      8 
      9 Types:
     10 - invalid: access to invalid address (e.g. write to RO page)
     11 - major (slower): valid, but page not in memory (e.g. file, swapped pages)
     12 - minor (faster): valid, page in memory
     13 
     14 Kernel page fault:
     15 - invalid:
     16     - on kernel address: oops (panic/recovery)
     17     - on user address: checks (copy to/from user)
     18 - major: kernel memory is paged out (not on Linux, e.g. in Windows)
     19 - minor: lazy allocations (e.g. old vmalloc)
     20 
     21 User page fault:
     22 - could be anything
     23 
     24 Page fault information:
     25 - cr2 register has faulting virtual address
     26 - error code on stack has:
     27     - bunch of reserved bits
     28     - P: PTE present bit set (so page present, maybe fault caused by some other problem)
     29     - W: write access
     30     - U: user access (so we were in user eecution)
     31     - R: PTE had reserved bits set
     32     - I: faulted while trying to fetch instruction
     33 
     34 VMA:
     35 - contiguous virtual region with given properties (bit flags)
     36 - anonymous: no file
     37 - stored in red-black tree (for efficiency) and linked list (for traversing in order)
     38 
     39 ![VMA diagram](vma-diagram.png)
     40 
     41 Demand paging:
     42 - new pages are not mapped onto physical memory
     43 - on first access, process page faults
     44 - kernel assigns page frame to process, creates PTE, resumes execution
     45 
     46 VMA operations:
     47 - mmap: maps new region in adress space
     48     - find fitting hole in memory map, creates and links new VMA (in simplest case)
     49 - munmap: unmaps region in address space
     50     - unlinks and deletes VMA (in simplest case), removes mapped page frames, removes allocated page tables, flushes TLB
     51 - mprotect: changes region's protection bits
     52     - update VMA protection (in simplest case), update affected PTEs
     53 - madvise: give kernel advice on region, like `MADV_DONTNEED` (unmap page frames), `MADV_WILLNEED` (read ahead page frames)
     54 - find and split: when you work on a part of a VMA (might even split one into three parts)
     55 - merge: merge any adjacent VMAs with same properties (flags)
     56 
     57 Transparent Huge Pages (THP)
     58 - kernel transparently allocates THP when possible
     59 - map: allocate compound pages (physically contiguous pages) and map as THPs
     60 - collapse: replace some regular pages with THP
     61 - split: split THP into number of regular pages
     62 - compact: memory compaction to allocate compound pages