Skip to contentSkip to conversation

virt: pvmemcontrol: control guest physical memory properties

PATCH Superseded Yuanchu Xie 3 participants 5 messages Oct 21, 2024 – Oct 23, 2024

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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 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]/

 .../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
index e4be1378ba26..bde53044b53c 100644
@@ -378,6 +378,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
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 +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
#1 lore raw

PATCH v4 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 7ad507f49324..c0afabab8b71 100644
@@ -18716,6 +18716,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 1 line · Yuanchu Xie

+static DEFINE_RWLOCK(pvmemcontrol_lock);

What does this lock control?

Quoted 1 line

+static struct pvmemcontrol *pvmemcontrol __read_mostly;

Having a single static device should not be needed, please tie this properly to the pci device that the driver core gives you. With this design, you have limited yourself to a fixed number of devices in the system at once (i.e. 1). This isn't the 1990's anymore, drivers should be able to handle any number of devices at once.

thanks,

greg k-h

#3 lore raw
In reply to Yuanchu Xie
kernel test robot noticed the following build errors:

Hi Yuanchu,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.12-rc4 next-20241023]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Yuanchu-Xie/virt-pvmemcontrol-add-Yuanchu-and-Pasha-as-maintainers/20241022-045035
base:   linus/master
patch link:    https://lore.kernel.org/r/20241021204849.1580384-1-yuanchu%40google.com
patch subject: [PATCH v4 1/2] virt: pvmemcontrol: control guest physical memory properties
config: i386-allmodconfig (https://download.01.org/0day-ci/archive/20241023/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241023/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "per_cpu_ptr_to_phys" [drivers/virt/pvmemcontrol/pvmemcontrol.ko] undefined!

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for GET_FREE_REGION Depends on [n]: SPARSEMEM [=n]
Selected by [m]:
- RESOURCE_KUNIT_TEST [=m] && RUNTIME_TESTING_MENU [=y] && KUNIT [=m]

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw
In reply to Yuanchu Xie
kernel test robot noticed the following build errors:

Hi Yuanchu,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.12-rc4 next-20241023]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Yuanchu-Xie/virt-pvmemcontrol-add-Yuanchu-and-Pasha-as-maintainers/20241022-045035
base:   linus/master
patch link:    https://lore.kernel.org/r/20241021204849.1580384-1-yuanchu%40google.com
patch subject: [PATCH v4 1/2] virt: pvmemcontrol: control guest physical memory properties
config: powerpc-randconfig-002-20241024 (https://download.01.org/0day-ci/archive/20241024/[email protected]/config)
compiler: powerpc-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241024/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

>> drivers/virt/pvmemcontrol/pvmemcontrol.c:455:1: warning: data definition has no type or storage class
     455 | module_pci_driver(pvmemcontrol_pci_driver);
         | ^~~~~~~~~~~~~~~~~
>> drivers/virt/pvmemcontrol/pvmemcontrol.c:455:1: error: type defaults to 'int' in declaration of 'module_pci_driver' [-Wimplicit-int]
>> drivers/virt/pvmemcontrol/pvmemcontrol.c:455:1: error: parameter names (without types) in function declaration [-Wdeclaration-missing-parameter-type]
>> drivers/virt/pvmemcontrol/pvmemcontrol.c:449:26: warning: 'pvmemcontrol_pci_driver' defined but not used [-Wunused-variable]
     449 | static struct pci_driver pvmemcontrol_pci_driver = {
         |                          ^~~~~~~~~~~~~~~~~~~~~~~

vim +455 drivers/virt/pvmemcontrol/pvmemcontrol.c

448

 > 449	static struct pci_driver pvmemcontrol_pci_driver = {
   450		.name = "pvmemcontrol",
   451		.id_table = pvmemcontrol_pci_id_tbl,
   452		.probe = pvmemcontrol_pci_probe,
   453		.remove = pvmemcontrol_pci_remove,
   454	};
 > 455	module_pci_driver(pvmemcontrol_pci_driver);

456

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki