设为首页 收藏本站
查看: 678|回复: 0

[经验分享] KVM 内存管理概述

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-10-10 10:06:33 | 显示全部楼层 |阅读模式
  This was written in February 2010, during the era of qemu-kvm 0.12.
  The qemu/kvm process runs mostly like a normal Linux program.  It allocates its memory with normal malloc() or mmap() calls.  If a guest is going to have 1GB of physical memory, qemu/kvm will effectively do a malloc(1<<30), allocating 1GB of host virtual space.  However, just like a normal program doing a malloc(), there is no actual physicalmemory allocated at the time of the malloc().  It will not be actually allocated until the first time it is touched.
  Once the guest is running, it sees that malloc()'d memory area asbeing its physical memory.  If the guest's kernel were to access what it sees as physical address 0x0, it will see the first page of that malloc() done by the qemu/kvm process.
  It used to be that every time a KVM guest changed its page tables, the host had to be involved.  The host would validate that the entries the guest put in its page tables were valid and that they did not accessany memory which was not allowed.  It did this with two mechanisms.
  One was that the actual set of page tables being used by the virtualization hardware are separate from the page tables that the guest*thought* were being used.  The guest first makes a change in its page tables.  Later, the host notices this change, verifies it, and then makes a real page table which is accessed by the hardware.  The guest software is not allowed to directly manipulate the page tables accessed by the hardware.  This concept is called shadow page tables and it is a very common technique in virtualization.
  The second part was that the VMX/AMD-V extensions allowed the host to trap whenever the guest tried to set the register pointing to the base page table (CR3).
  This technique works fine.  But, it has some serious performance implications.  A single access to a guest page can take up to 25 memory accesses to complete, which gets very costly. See this paper: http://developer.amd.com/assets/NPT-WP-1%201-final-TM.pdffor more information.  The basic problem is that every access to memorymust go through both the page tables of the guest and then the page tables of the host.  The two-dimensional part comes in because the page tables of the guest must *themselves* go through the page tables of the host.
  It can also be very costly for the host to verify and maintain the shadow page tables.  
  Both AMD and Intel sought solutions to these problems and came up with similar answers called EPT and NPT.  They specify a set of structures recognized by the hardware which can quickly translate guest physical addresses to host physical addresses *without* going through the host page tables.  This shortcut removes the costly two-dimensional page table walks.
  The problem with this is that the host page tables are what we use to enforce things like process separation.  If a page was to be unmapped from the host (when it is swapped, for instance), it then we *must* coordinate that change with these new hardware EPT/NPT structures.
  The solution in software is something Linux calls mmu_notifiers.  Since the qemu/kvm memory is normal Linux memory (from the host Linux kernel's perspective) the kernel may try to swap it, replace it, or evenfree it just like normal memory.
  But, before the pages are actually given back to the host kernel forother use, the kvm/qemu guest is notified of the host's intentions. Thekvm/qemu guest can then remove the page from the shadow page tables orthe NPT/EPT structures. After the kvm/qemu guest has done this, the hostkernel is then free to do what it wishes with the page.
  A day in the life of a KVM guest physical page:

  
Fault-in path

  • QEMU calls malloc() and allocates virtual space for the page, but no backing physical page
  • The guest process touches what it thinks is a physical address, but this traps into the host since the memory is unallocated
  • The host kernel sees a page fault, calls do_page_fault() in the area that was malloc()'d, and if all goes well, allocates some memory to back it.
  • The host kernel creates a pte_t to connect the malloc()'d virtual address to a host physical address, makes rmap entries, puts it on the LRU, etc...
  • mmu_notifier change_pte()?? is called, which allows KVM to create an NPT/EPT entry for the new page. (and an spte entry??)
  • Host returns from page fault, guest execution resumes

  
Swap-out path
  Now, let's say the host is under memory pressure.  The page from above has gone through the Linux LRU and has found itself on the inactive list.  The kernel decides that it wants the page back:

  • The host kernel uses rmap structures to find out in which VMA (vm_area_struct) the page is mapped.
  • The host kernel looks up the mm_struct associated with that VMA, and walks down the Linux page tables to find the host hardware pagetable entry (pte_t) for the page.
  • The host kernel swaps out the page and clears out the pte_t (let's assume that this page was only used in a single place). But, before freeing the page:
  • The host kernel calls the mmu_notifier invalidate_page(). Thislooks up the page's entry in the NPT/EPT structures and removes it.
  • Now, any subsequent access to the page will trap into the host((2) in the fault-in path above)
  

  
Memory Overcommit
  Given all of the above, it should be apparent that just like normal processes on linux, the host memory allocated to the host processes representing kvm guests may be overcommitted.  One distro's discussion of this appears here.
             版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-124977-1-1.html 上篇帖子: KVM在嵌入式μCLinux操作系统上的移植 下篇帖子: qemu-kvm 准备知识
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表