天天看点

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)

继续阅读