Skip to content

Instantly share code, notes, and snippets.

@cwshu
Created May 13, 2015 06:39
Show Gist options
  • Save cwshu/7d52bc993525c1bb7df1 to your computer and use it in GitHub Desktop.
Save cwshu/7d52bc993525c1bb7df1 to your computer and use it in GitHub Desktop.

fast live VM cloning

related work

  1. paper: Fast Live Cloning of Virtual Machine Based on Xen

    • High Performance Computing and Communications, 2009. HPCC '09. 11th IEEE International Conference
    • COW based live VM cloning, x86 platform
    • 除了實作方式以外, 他們的 evaluation 我覺得也可以參考. 包含不同 VM memory size 的 COW clone 時間比較, 一些常見 application COW 後共享的 memory 比例.
  2. technical report: How to enable Live Cloning of Virtual Machines using the Xen Hypervisor

linux kernel COW mechanism

fork COW in linux kernel

  1. fork 階段, 複製 4-level page table 的底端 copy_one_pte() 會呼叫 ptep_set_wrprotect() 把 process 的 page 加上 write protected
  2. page fault handler 階段, handle_pte_fault() 呼叫 do_wp_page() 處理 write protected page

detail tracing

  • fork() -> clone()

  • fork(), vfork(), __clone() -> clone()

    • clone() with different flags mean copying process but parent and child share different resource.
  • fork() -> clone() -> do_fork() -> copy_process()

    • copy_process() -> copy_mm() -> dup_mm() -> dup_mmap()
  • dup_mmap() in kernel/fork.c

    • copy_page_range() in mm/memory.c: copy page all tables, until copy_one_pte() is called.

      • from pud to pte, pgd?
      • copy_one_pte() -> ptep_set_wrprotect(): set pte write-protected
  • ref

    • process creation in Linux Kernel Development: http://www.makelinux.net/books/lkd2/ch03lev1sec2

    • trace code (Linux Kernel 4.0)

      ptep_set_wrprotect() implementation, mm/pgtable.c:317 (http://lxr.free-electrons.com/source/arch/tile/mm/pgtable.c#L317)
      ptep_set_wrprotect() in copy_one_pte(), mm/memory.c:861 (http://lxr.free-electrons.com/source/mm/memory.c#L861)
      

KSM COW

  1. 一旦某個區域被定義為 mergeable, KSM 將把該區域加到它的工作記憶體列表.

    啟動 KSM 時, 它將搜尋相同的 page, 以 write protected 的 COW 方式保留一個 page, 並釋放另一個 page 以供它用.

  2. 當 KSM 在處理單一 page 時, 如果該候選 page 位於 stable tree 裡, 則該 page 被合併, 候選 page 被釋放.

    相關程式碼位於 ksm.c/stable_tree_search() in ksm.c/cmp_and_merge_page()

  3. 由於 stable tree 的所有 page 都是 write protected 的, 因此當一個 page 被試圖寫入時, 將生成 page fault, 從而允許 COW process 取消 page 合併.

    ksm.c/break_cow()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment