天天看點

KVM髒頁統計的硬體基礎--Inter x86

SPTE

shadow pte:影子頁表項,也就是EPT頁表項,指向EPT中下一級頁表或者頁的hpa,以及相應的權限位;

KVM在還沒有EPT硬體支援的時候,采用的是影子頁表(shadow page table)機制,為了和之前的代碼相容,在目前的實作中,EPT機制是在影子頁表機制代碼的基礎上實作的,是以EPT裡面的pte和之前一樣被叫做 shadow pte;

使用SPTE機制的時候,主要是靠SPTE的D狀态位跟蹤髒頁。

Bit Position(s) Contents Descript
0 (

P

)
Present; must be 1 to map a 1-GByte page 映射1G page必須設定為1
1 (

R/W

)
Read/write; if 0, writes may not be allowed to the 1-GByte page referenced by this entry (see Section 4.6) 1G page的寫權限控制
2 (

U/S

)
User/supervisor; if 0, user-mode accesses are not allowed to the 1-GByte page referenced by this entry (see Section 4.6) 1G page的使用者模式通路允許
3 (

PWT

)
Page-level write-through; indirectly determines the memory type used to access the 1-GByte page referenced by this entry (see Section 4.9.2) page級的write-through屬性
4 (

PCD

)
Page-level cache disable; indirectly determines the memory type used to access the 1-GByte page referenced by this entry (see Section 4.9.2) page級的cache disable屬性
5 (

A

)
Accessed; indicates whether software has accessed the 1-GByte page referenced by this entry (see Section 4.8) 訓示軟體是否通路過目前entry對應的1G page區域
6 (

D

)
Dirty; indicates whether software has written to the 1-GByte page referenced by this entry (see Section 4.8) 訓示軟體是否寫入過目前entry對應的1G page區域
7 (

PS

)
Page size; must be 1 (otherwise, this entry references a page directory; see Table 4-17) 必須置1
8 (

G

)
Global; if CR4.PGE = 1, determines whether the translation is global (see Section 4.10); ignored otherwise 如果CR4.PGE = 1,定義位址轉換是否是全局的
11:9 Ignored -
12 (

PAT

)
Indirectly determines the memory type used to access the 1-GByte page referenced by this entry (see Section 4.9.2)1 間接決定1G page的memory type
29:13 Reserved (must be 0) -
(M–1):30 Physical address of the 1-GByte page referenced by this entry 1G對齊的page實體位址
51:M Reserved (must be 0) -
58:52 Ignored -
62:59

Protection key

if CR4.PKE = 1 or CR4.PKS = 1, this may control the page’s access rights (see Section 4.6.2); otherwise, it is not used to control access rights.
Protection key,如果CR4.PKE = 1 or CR4.PKS = 1,控制page的通路權限
63 (

XD

)
If IA32_EFER.NXE = 1, execute-disable (if 1, instruction fetches are not allowed from the 1-GByte page controlled by this entry; see Section 4.6); otherwise, reserved (must be 0) 如果IA32_EFER.NXE = 1,1G page的可執行權限disable配置

EPT頁表項:

為簡化記憶體虛拟化的實作,提升記憶體虛拟化的性能,Inter推出了EPT(Enhanced Page Table)技術,在原有的頁表基礎上新增了EPT頁表實作另一次映射。這樣,GVA-GPA-HPA兩次位址轉換都由CPU硬體自動完成。

KVM髒頁統計的硬體基礎--Inter x86
通過EPT的GVA和HPA的翻譯過程如下:
  1. 處于非根模式的CPU加載guest程序的gCR3;
  2. gCR3是GPA,cpu需要通過查詢EPT頁表來實作GPA->HPA;
  3. 如果沒有,CPU觸發EPT Violation, 由Hypervisor截獲處理;
  4. 假設客戶機有m級頁表,主控端EPT有n級,在TLB均miss的最壞情況下,會産生MxN次記憶體通路,完成一次客戶機的位址翻譯;

PML

PML是Intel為支援虛拟化場景下髒頁跟蹤而開發的硬體特性,使用該特性涉及到Intel的幾個硬體元素:

  • Accessed and Dirty flags:這是EPTP字段中位于bit 6的一個标志位,當設定此标志位後,它告訴CPU每當使用EPT查詢HPA時,将頁結構存放的表項中的Accessed位(bit 8)置1,對于指向實體頁的頁表項,當往指向的實體頁中寫資料時,将它的Dirty位(bit 9)置1。
  • PML flag:這是VMCS VM-Exection Controls Field區域的一個标志位,用于使能PML特性。隻有在Accessed and Dirty flags位開啟時,才可以使能PML。
  • PML Buffer:PML位址指向一個4KB對齊的實體記憶體頁,稱為PML日志緩沖區。緩沖區由512個64位條目組成,這些條目存儲了CPU寫過的實體頁的位址(GPAs),KVM就是通過這個區域來跟蹤記憶體髒頁。
  • PML Address:用來儲存PML Buffer的記憶體實體位址,它是VMCS VM-Execution control field的一個字段。
  • PML index:PML索引是日志緩沖區下一個條目的索引,由于緩沖區包含了512個條目,PML索引通常是0~511範圍内的值(長度為16 bit,從511開始),當啟用PML時,每個寫指令都會周遊EPT設定其GPA髒标志,PML索引在每次日志操作之後遞減。每當PML日志緩沖區滿了,處理器就引發VMExit,之後hypervisor便開始介入處理。PML索引重置後,日志記錄程序将重新啟動。hypervisor響應該VMExit所采取的操作取決于其目标(例如VM熱遷移)
KVM髒頁統計的硬體基礎--Inter x86

上圖是關于使用PML來改進虛拟化的操作(例如動态遷移)的機器的過程。

綠色的VM表示目标使用者的虛拟機,執行虛拟化運作的系統運作在特權虛拟機(pVM)上

  1. 系統的執行通常從為目标使用者VM激活PML(設定Accessed and Dirty flags和PML flag标志)開始,使其可以記錄GPA(記錄髒頁)
  2. 當PML日志緩沖區滿時,産生page-modification log-full事件,CPU抛出一個VMExit,該VMExit會被内部的hypervisor所捕獲
  3. MExit的處理程式執行特定的任務(例如,将PML日志緩沖區的内容複制到與pVM共享的更大的緩沖區(圖1,))。PML索引重置為511,虛拟機恢複(VMEnter)
  4. PML Buffer的内容,就是CPU最近寫過的記憶體頁位址,它就是記憶體的髒資料,對于記憶體遷移來說,該資料可以用來評估虛機的記憶體變化量。如果PML Buffer中内容較少,那麼Qemu可以将虛機暫停然後一次性拷貝完。
  5. 一旦虛拟化操作(例如熱遷)結束,PML将被禁用。
以上是PML的流程,但是這并不意味着所有場景都是如此,在KVM當中,會保持PML特性一直開啟,在KVM初始化的時候就開啟,即使是遷移結束也不會結束PML,在vcpu建立的時候就開始配置設定PML Buffer

參考資料:

[1]. The x86 kvm shadow mmu (https://www.kernel.org/doc/Documentation/virtual/kvm/mmu.txt)

[2]. Intel Page Modification Logging, a hardware virtualization feature: study and improvement for virtual machine working set estimation

[3]. KVM同步髒頁原理 (https://blog.csdn.net/huang987246510/article/details/108348207)

繼續閱讀