virt: pvmemcontrol: control guest physical memory properties
Subsystems (3)
PATCH v3 1/2 virt: pvmemcontrol: control guest physical memory properties
Pvmemcontrol provides a way for the guest to control its physical memory properties and enables optimizations and security features. For example, the guest can provide information to the host where parts of a hugepage may be unbacked, or sensitive data may not be swapped out, etc.
Pvmemcontrol allows guests to manipulate its gPTE entries in the SLAT, and also some other properties of the memory mapping on the host. This is achieved by using the KVM_CAP_SYNC_MMU capability. When this capability is available, the changes in the backing of the memory region on the host are automatically reflected into the guest. For example, an mmap() or madvise() that affects the region will be made visible immediately.
There are two components of the implementation: the guest Linux driver and Virtual Machine Monitor (VMM) device. A guest-allocated shared buffer is negotiated per-cpu through a few PCI MMIO registers; the VMM device assigns a unique command for each per-cpu buffer. The guest writes its pvmemcontrol request in the per-cpu buffer, then writes the corresponding command into the command register, calling into the VMM device to perform the pvmemcontrol request.
The synchronous per-cpu shared buffer approach avoids the kick and busy waiting that the guest would have to do with virtio virtqueue transport.
User API
From the userland, the pvmemcontrol guest driver is controlled via the ioctl(2) call. It requires CAP_SYS_ADMIN.
ioctl(fd, PVMEMCONTROL_IOCTL, struct pvmemcontrol_buf *buf);
Guest userland applications can tag VMAs and guest hugepages, or advise the host on how to handle sensitive guest pages.
Supported function codes and their use cases:
PVMEMCONTROL_FREE/REMOVE/DONTNEED/PAGEOUT. For the guest. One can reduce the struct page and page table lookup overhead by using hugepages backed by smaller pages on the host. These pvmemcontrol commands can allow for partial freeing of private guest hugepages to save memory. They also allow kernel memory, such as kernel stacks and task_structs to be paravirtualized if we expose kernel APIs.
PVMEMCONTROL_MERGEABLE can inform the host KSM to deduplicate VM pages.
PVMEMCONTROL_UNMERGEABLE is useful for security, when the VM does not want to share its backing pages.
The same with PVMEMCONTROL_DONTDUMP, so sensitive pages are not included in a dump.
MLOCK/UNLOCK can advise the host that sensitive information is not swapped out on the host.
PVMEMCONTROL_MPROTECT_NONE/R/W/RW. For guest stacks backed by hugepages, stack guard pages can be handled in the host and memory can be saved in the hugepage.
PVMEMCONTROL_SET_VMA_ANON_NAME is useful for observability and debugging how guest memory is being mapped on the host.
Sample program making use of PVMEMCONTROL_DONTNEED:
https://github.com/Dummyc0m/pvmemcontrol-user
The VMM implementation is part of Cloud Hypervisor, the feature pvmemcontrol can be enabled and the VMM can then provide the device to a supporting guest.
https://github.com/cloud-hypervisor/cloud-hypervisor
-
Changelog
PATCH v2 -> v3
- added PVMEMCONTROL_MERGEABLE for memory dedupe.
- updated link to the upstream Cloud Hypervisor repo, and specify the feature required to enable the device.
PATCH v1 -> v2
- fixed byte order sparse warning. ioread/write already does little-endian.
- add include for linux/percpu.h
RFC v1 -> PATCH v1
- renamed memctl to pvmemcontrol
- defined device endianness as little endian
v1:
https://lore.kernel.org/linux-mm/[email protected]/
v2:
https://lore.kernel.org/linux-mm/[email protected]/
--- .../userspace-api/ioctl/ioctl-number.rst | 2 + drivers/virt/Kconfig | 2 + drivers/virt/Makefile | 1 + drivers/virt/pvmemcontrol/Kconfig | 10 + drivers/virt/pvmemcontrol/Makefile | 2 + drivers/virt/pvmemcontrol/pvmemcontrol.c | 459 ++++++++++++++++++ include/uapi/linux/pvmemcontrol.h | 76 +++ 7 files changed, 552 insertions(+) create mode 100644 drivers/virt/pvmemcontrol/Kconfig create mode 100644 drivers/virt/pvmemcontrol/Makefile create mode 100644 drivers/virt/pvmemcontrol/pvmemcontrol.c create mode 100644 include/uapi/linux/pvmemcontrol.h
Documentation/userspace-api/ioctl/ioctl-number.rst +2 -0
@@ -372,6 +372,8 @@ Code Seq# Include File Comments 0xCD 01 linux/reiserfs_fs.h 0xCE 01-02 uapi/linux/cxl_mem.h Compute Express Link Memory Devices 0xCF 02 fs/smb/client/cifs_ioctl.h +0xDA 00 uapi/linux/pvmemcontrol.h Pvmemcontrol Device + <mailto:[email protected]> 0xDB 00-0F drivers/char/mwave/mwavepub.h 0xDD 00-3F ZFCP device driver see drivers/s390/scsi/ <mailto:[email protected]>
drivers/virt/Kconfig +2 -0
@@ -49,4 +49,6 @@ source "drivers/virt/acrn/Kconfig" source "drivers/virt/coco/Kconfig" +source "drivers/virt/pvmemcontrol/Kconfig" + endif
drivers/virt/Makefile +1 -0
@@ -10,3 +10,4 @@ obj-y += vboxguest/ obj-$(CONFIG_NITRO_ENCLAVES) += nitro_enclaves/ obj-$(CONFIG_ACRN_HSM) += acrn/ obj-y += coco/ +obj-$(CONFIG_PVMEMCONTROL) += pvmemcontrol/
drivers/virt/pvmemcontrol/Kconfig new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 +config PVMEMCONTROL + tristate "pvmemcontrol Guest Service Module" + depends on KVM_GUEST + help + pvmemcontrol is a guest kernel module that allows to communicate + with hypervisor / VMM and control the guest memory backing. + + To compile as a module, choose M, the module will be called + pvmemcontrol. If unsure, say N.
drivers/virt/pvmemcontrol/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_PVMEMCONTROL) := pvmemcontrol.o
drivers/virt/pvmemcontrol/pvmemcontrol.c new file +459 -0
459 lines not on this page · raw message
include/uapi/linux/pvmemcontrol.h new file +76 -0
76 lines not on this page · raw message
Signature · 1 line
2.46.0
PATCH v3 2/2 virt: pvmemcontrol: add Yuanchu and Pasha as maintainers
The pvmemcontrol driver lives under drivers/virt/pvmemcontrol. We specify maintainers for the driver.
--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
@@ -18125,6 +18125,13 @@ L: [email protected] S: Supported F: drivers/net/wireless/purelifi/plfxlc/ +PVMEMCONTROL GUEST DRIVER +M: Yuanchu Xie <[email protected]> +M: Pasha Tatashin <[email protected]> +L: [email protected] +S: Supported +F: drivers/virt/pvmemcontrol/ + PVRUSB2 VIDEO4LINUX DRIVER M: Mike Isely <[email protected]> L: [email protected] (subscribers-only)
Signature · 1 line
2.46.0
Quoted 65 lines · Yuanchu Xie
Pvmemcontrol provides a way for the guest to control its physical memory properties and enables optimizations and security features. For example, the guest can provide information to the host where parts of a hugepage may be unbacked, or sensitive data may not be swapped out, etc.
Pvmemcontrol allows guests to manipulate its gPTE entries in the SLAT, and also some other properties of the memory mapping on the host. This is achieved by using the
KVM_CAP_SYNC_MMUcapability. When this capability is available, the changes in the backing of the memory region on the host are automatically reflected into the guest. For example, anmmap()ormadvise()that affects the region will be made visible immediately.There are two components of the implementation: the guest Linux driver and Virtual Machine Monitor (VMM) device. A guest-allocated shared buffer is negotiated per-cpu through a few PCI MMIO registers; the VMM device assigns a unique command for each per-cpu buffer. The guest writes its pvmemcontrol request in the per-cpu buffer, then writes the corresponding command into the command register, calling into the VMM device to perform the pvmemcontrol request.
The synchronous per-cpu shared buffer approach avoids the kick and busy waiting that the guest would have to do with virtio virtqueue transport.
User API
Quoted 1 line
From the userland, the pvmemcontrol guest driver is controlled via the
ioctl(2) call. It requires
CAP_SYS_ADMIN.
ioctl(fd, PVMEMCONTROL_IOCTL, struct pvmemcontrol_buf *buf);Guest userland applications can tag VMAs and guest hugepages, or advise the host on how to handle sensitive guest pages.
Supported function codes and their use cases:
PVMEMCONTROL_FREE/REMOVE/DONTNEED/PAGEOUT. For the guest. One can reduce the struct page and page table lookup overhead by using hugepages backed by smaller pages on the host. These pvmemcontrol commands can allow for partial freeing of private guest hugepages to save memory. They also allow kernel memory, such as kernel stacks andtask_structsto be paravirtualized if we expose kernel APIs.
PVMEMCONTROL_MERGEABLEcan inform the host KSM to deduplicate VM pages.
PVMEMCONTROL_UNMERGEABLEis useful for security, when the VM does not want to share its backing pages.
The same withPVMEMCONTROL_DONTDUMP, so sensitive pages are not included in a dump.
MLOCK/UNLOCK can advise the host that sensitive information is not swapped out on the host.PVMEMCONTROL_MPROTECT_NONE/R/W/RW. For guest stacks backed by hugepages, stack guard pages can be handled in the host and memory can be saved in the hugepage.
PVMEMCONTROL_SET_VMA_ANON_NAMEis useful for observability and debugging how guest memory is being mapped on the host.Sample program making use of
PVMEMCONTROL_DONTNEED:
https://github.com/Dummyc0m/pvmemcontrol-userThe VMM implementation is part of Cloud Hypervisor, the feature pvmemcontrol can be enabled and the VMM can then provide the device to a supporting guest.
https://github.com/cloud-hypervisor/cloud-hypervisor
Quoted 13 lines
-
Changelog
PATCH v2 -> v3
- addedPVMEMCONTROL_MERGEABLEfor memory dedupe.
- updated link to the upstream Cloud Hypervisor repo, and specify the feature required to enable the device.
PATCH v1 -> v2
- fixed byte order sparse warning. ioread/write already does little-endian.
- add include forlinux/percpu.h
RFC v1 -> PATCH v1
- renamed memctl to pvmemcontrol
- defined device endianness as little endian
As per the kernel documentation, this changelog is in the wrong place. Please put it in the correct location.
thanks,
greg k-h
drivers/virt/pvmemcontrol/pvmemcontrol.c line 194
Quoted 11 lines · Yuanchu Xie
drivers/virt/pvmemcontrol/pvmemcontrol.c new file +11
-0+dev_info (dev->device, +"pvmemcontrol_vmm_info.ret_errno = %u\n" +"pvmemcontrol_vmm_info.ret_code = %u\n" +"pvmemcontrol_vmm_info.major_version = %llu\n" +"pvmemcontrol_vmm_info.minor_version = %llu\n" +"pvmemcontrol_vmm_info.page_size = %llu\n" , + dev->pvmemcontrol_vmm_info.ret_errno, + dev->pvmemcontrol_vmm_info.ret_code, + dev->pvmemcontrol_vmm_info.arg0, + dev->pvmemcontrol_vmm_info.arg1, + dev->pvmemcontrol_vmm_info.ret_value);
When a driver works properly, it is quiet. Please do not spam the kernel log when your driver properly binds to the device like this, that's just not ok at all.
thanks,
greg k-h