Refer to: https://libvirt.org/kbase/virtiofs.html#other-options-for-vhost-user-memory-setup
几乎所有的virtio设备(使用virtqueue的)都需要访问至少一部分guest的内存(DMA需要)。设备使用virtqueue来实现guest内存和qemu进程的信息交换。而virtiofsd, 跟vhost-user的virtio设备一样, 需要被其他的用户空间的进程使用到,所以意味着qemu需要为整个guest设置使用共享内存。从qemu 4.2开始,我们可以在numa拓扑里具体地为每个node设置memory backend。然而这种方法只对支持numa的machine type生效。qemu 5.0.0和libvirt 6.9.0以后,我们可以在没有numa设置的情况下去设置memorybackend(使用 memobject 的接口)。
- host的设置,设置memory backend:
-
Use memfd memory 不需要任何设置
-
Use file-backed memory 在qemu.conf 中设置 memory_backing_dir = "/dev/shm/"
-
Use hugepage-backed memory 设置足够的 hugepages,使用命令:
# virsh allocpages 2M 1024
- Guest xml,设置numa拓扑(可选的)
...
<cpu ...>
<numa>
<cell id='0' cpus='0-7' memory='2' unit='GiB' memAccess='shared'/>
</numa>
</cpu>
...
</domain>
- Guest xml,设置memory backend。(备注:memory backend一共有三种: file,anonymous,memfd。默认是anonymous,如果设置了<access mode='shared'/>也会变成file backend;即使设置了<access mode='shared'/> 和 <source type="anonymous"/>,也会使用file backend。)
<memoryBacking>
...
<source type="file|anonymous|memfd"/>
</memoryBacking>
下面这些xml的例子都是sharedmemory的简单实例,都没有使用numa node的设置(当然要加numa node的设置也是可以的): Example 1: memfd memory
<domain>
...
<memoryBacking>
<source type='memfd'/>
<access mode='shared'/>
</memoryBacking>
...
</domain>
Example 2: file-backed memory without hugepage 备注:需要在qemu.conf中设置 memory_backing_dir = "/dev/shm/"才得到完整的功能; 如果不设置的话使用默认的memory_backing_dir = "/var/lib/libvirt/qemu/ram"也是有效的设置,但是有一些限制,比如不支持postcopy迁移。参考: https://bugzilla.redhat.com/show_bug.cgi?id=2057267#c13
<domain>
...
<memoryBacking>
<access mode='shared'/>
</memoryBacking>
...
</domain>
Example 3: file-backed memory with hugepage
<domain>
...
<memoryBacking>
<hugepages>
<page size='2' unit='M'/>
</hugepages>
<access mode='shared'/>
</memoryBacking>
...
</domain>