Skip to contentSkip to conversation

virt: pvmemcontrol: control guest physical memory properties

PATCH In review Yuanchu Xie 2 participants 6 messages Dec 3, 2024 – Jan 4, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v5 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

---
PATCH v4 -> v5
- use drvdata and friends to enable multiple devices

PATCH v3 -> v4
- changed dev_info to dev_dbg so the driver is quiet when it works properly.
- Edited the changelog section to be included in the diffstat.

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]/
v3:
https://lore.kernel.org/linux-mm/[email protected]/
v4:
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      | 499 ++++++++++++++++++
 include/uapi/linux/pvmemcontrol.h             |  76 +++
 mm/percpu.c                                   |   1 +
 8 files changed, 593 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
index 243f1f1b554a..09788e8e4f9f 100644
@@ -378,6 +378,8 @@ Code  Seq#    Include File                                           Comments
 0xCD  01     linux/reiserfs_fs.h                                     Dead since 6.13
 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
index d8c848cf09a6..454e347a90cf 100644
@@ -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
index f29901bd7820..3a1fd6e076ad 100644
@@ -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
new file mode 100644
index 000000000000..9fe16da23bd8
@@ -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
new file mode 100644
index 000000000000..2fc087ef3ef5
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_PVMEMCONTROL)	:= pvmemcontrol.o
drivers/virt/pvmemcontrol/pvmemcontrol.c new file +499 -0

499 lines not on this page · raw message

include/uapi/linux/pvmemcontrol.h new file +76 -0

76 lines not on this page · raw message

mm/percpu.c +1 -0
index d8dd31a2e407..87d69b17d979 100644
@@ -2384,6 +2384,7 @@ phys_addr_t per_cpu_ptr_to_phys(void *addr)
 		return page_to_phys(pcpu_addr_to_page(addr)) +
 		       offset_in_page(addr);
 }
+EXPORT_SYMBOL_GPL(per_cpu_ptr_to_phys);
 
 /**
  * pcpu_alloc_alloc_info - allocate percpu allocation info
Signature · 1 line
2.46.0
#1 lore raw

PATCH v5 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
index 1e930c7a58b1..92db5dcf0212 100644
@@ -18894,6 +18894,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
#2 lore raw
In reply to Yuanchu Xie
Quoted 5 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.

Hello Greg,

I addressed the feedback from you earlier at [1]. Let me know how the v5 goes.

Thanks,
Yuanchu

[1] https://lore.kernel.org/linux-mm/2024102220-creed-darkening-aac0@gregkh/

#3 lore raw
Quoted 6 lines · Yuanchu Xie
Quoted 5 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.

Quoted 8 lines

Hello Greg,

I addressed the feedback from you earlier at [1]. Let me know how the v5 goes.

Thanks,
Yuanchu

[1] https://lore.kernel.org/linux-mm/2024102220-creed-darkening-aac0@gregkh/

Right now my review queue looks like this:

	$ mdfrm -c ~/mail/todo/
	2474 messages in /home/gregkh/mail/todo/

I recommend taking the time to review other patches from other people to help reduce the maintainer review load.

thanks!

greg k-h

#4 lore raw
In reply to Yuanchu Xie
Quoted 9 lines · Yuanchu Xie
Quoted 5 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.

Hello Greg,

I addressed the feedback from you earlier at [1]. Let me know how the v5 goes.

Why isn't this being reviewed by the KVM and mm developers? THat's the subsytems you are interacting with, right?

thanks,

greg k-h

#5 lore raw
In reply to Yuanchu Xie
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_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

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 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

Quoted 5 lines

---
PATCH v4 -> v5
- use drvdata and friends to enable multiple devices

And now you are "burning" a whole major number for this, right? Why not just use a misc device for every individual one? That should be much simpler and take away a lot of the generic code you have added here (your class structure, your major/minor number handling, etc.)

Quoted 28 lines

PATCH v3 -> v4
- changed dev_info to dev_dbg so the driver is quiet when it works properly.
- Edited the changelog section to be included in the diffstat.

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]/
v3:
https://lore.kernel.org/linux-mm/[email protected]/
v4:
https://lore.kernel.org/linux-mm/[email protected]/

Quoted 6 lines
 .../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      | 499 ++++++++++++++++++

Why a whole subdirectory for just one .c file? Why not put it in drivers/virt/ ?

thanks,

greg k-h