Huge pages in Linux

Abdun Nihaal
https://nihaal.me
13 February 2021 at ILUGC
Creative Commons License

A primer on Virtual memory

Virtual Memory

  • One of the core functions of an Operating System
  • Hides the complexities of memory from the programmer
  • Provides isolation between processes
  • Allows enforcing permissions
  • Allows execution of programs which require large memory

Paging

  • Technique used by modern OSes to implement virtual memory
  • Physical main memory is divided into frames
  • Address space of the process is divided into pages
  • Pages are mapped onto frames
  • Needs address translation from page number to frame number

Paging

Translation Lookaside Buffer (TLB)

  • We need to do address translation fast (as it is needed for every memory access)
  • TLB is a small cache of address translation
  • Problem is that it is small
  • TLB misses are costly (due to page walks)

Huge pages

What are huge pages?

  • x86 archtecture supports 4KB, 2MB and 1GB pages
  • But 4KB is the default page size
  • Increases TLB reach and Reduces TLB misses
  • Gives performance benefit for memory intensive applications

Challenges of supporting huge pages

  • Fragmentation
  • Permission changes
  • Need to identify the regions that benefits from using huge pages

Huge pages in Linux

Linux gives two ways to use huge pages

  1. Libhugetlbfs
  2. Transparent Huge Pages

libhugetlbfs

  • Allows us to reserve some space for huge pages
  • Program can explicitly request for huge pages using MAP_HUGETLB flag in mmap syscall
  • To view reserved space
    
    cat /proc/meminfo | grep Huge
    				
  • To reserve space for hugepages
    
    echo 20 > /sys/kernel/mm/hugepages/hugepages-{size}/nr_hugepages
    				

Small demo of libhugetlbfs

Demo code

Transparent Huge pages (THP)

  • Kernel tries to allocate Huge pages whenever possible
  • A daemon called khugepaged does defragmentation in the background
  • Challenge here is to identify address range that benefits from hugepages
  • To get more information about THP
    
    cd /sys/kernel/mm/transparent_hugepage/
    				

Huge pages problems

Inspite of the performance benefits, many blogs recommend turning THP off, on systems running production applications

  • This is to avoid tail latencies due to page migration
  • Huge pages also makes side and covert channels easy to create

Thank you

Questions?

References