Skip to contentSkip to conversation

KVM: Mapping guest_memfd backed memory at the host for software protected VMs

RFC PATCH Superseded Fuad Tabba 4 participants 26 messages Jan 29, 2025 – Feb 10, 2025

Subsystems (11)

v2 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Fuad Tabba

author
#0 lore raw

v2 cover KVM: Mapping guest_memfd backed memory at the host for software protected VMs

Main changes since v1 [1]:
- Added x86 support for mapping guest_memfd at the host, enabled only for the KVM_X86_SW_PROTECTED_VM type.
- Require setting memslot userspace_addr for guest_memfd slots even if shared, and remove patches that worked around that.
- Brought in more of the infrastructure from the patch series that allows restricted mapping of guest_memfd backed memory.
- Renamed references to "mappable" -> "shared".
- Expanded the selftests.
- Added instructions to test on x86 and arm64 (below).
- Rebased on Linux 6.13.

The purpose of this series is to serve as a base for _restricted_ mmap() support for guest_memfd backed memory at the host [2]. It would allow experimentation with what that support would be like in the safe environment of the software VM types, which are meant for testing and experimentation.

This series adds a new VM type for arm64, KVM_VM_TYPE_ARM_SW_PROTECTED, analogous to the x86 KVM_X86_SW_PROTECTED_VM. This type is to serve as a development and testing vehicle for Confidential (CoCo) VMs.

Similar to its x86 counterpart, SW_PROTECTED is meant only for development and testing. It's not meant to be used for "real" VMs, and especially not in production. The behavior and effective ABI for software-protected VMs is unstable.

This series enables mmap() and fault() support for guest_memfd backed memory specifically for the software-protected VM types (in x86 and arm64), only when explicitly enabled in the config.

The series is based on Linux 6.13 and much of the code within is a subset of the latest series I sent [2], with the addition of the new software protected vm type.

To test this series, I've pushed a kvmtool branch with support for guest_memfd for x86 and arm64 and the new runtime options of --guest_memfd and --sw_protected, which marks the VM as software protected [3]. I plan on upstreaming this branch once I've tested it more and tidied it up a bit (or a lot).

To test this patch series on x86 (I use a standard Debian image):

Build:

- Build the kernel with the following config options enabled:
defconfigs:

	x86_64_defconfig
	kvm_guest.config

config options:
KVM
KVM_INTEL
KVM_PRIVATE_MEM
KVM_SW_PROTECTED_VM
KVM_GMEM_SHARED_MEM

- Build the kernel kvm selftest tools/testing/selftests/kvm, you only need guest_memfd_test, e.g.:
make EXTRA_CFLAGS="-static -DDEBUG" -C tools/testing/selftests/kvm

- Build kvmtool [3] lkvm-static (I build it on a different machine). make lkvm-static

Run:
Boot your Linux image with the kernel you built above.

The selftest you can run as it is:
./guest_memfd_test

For kvmtool, where bzImage is the same as the host's:

	./lkvm-static run -c 2 -m 512 -p "break=mount" --kernel bzImage --debug --guest_memfd --sw_protected

To test this patch series on arm64 (I use a standard Debian image):

Build:

- Build the kernel with defconfig

- Build the kernel kvm selftest tools/testing/selftests/kvm, you only need guest_memfd_test.

- Build kvmtool [3] lkvm-static (I cross compile it on a different machine). You are likely to need libfdt as well.

For libfdt (in the same directory as kvmtool):

	git clone git://git.kernel.org/pub/scm/utils/dtc/dtc.git
	cd dtc
	export CC=aarch64-linux-gnu-gcc
	make
	cd ..

Then for kvmtool:
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- LIBFDT_DIR=./dtc/libfdt/ lkvm-static

Run:
Boot your Linux image with the kernel you built above.

The selftest you can run as it is:
./guest_memfd_test

For kvmtool, where Image is the same as the host's, and rootfs is your rootfs image (in case kvmtool can't figure it out):

	./lkvm-static run -c 2 -m 512 -d rootfs --kernel Image --force-pci --irqchip gicv3 --debug --guest_memfd --sw_protected

You can find (potentially slightly outdated) instructions on how to a full arm64 system stack under QEMU here [4].

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://android-kvm.googlesource.com/kvmtool/+/refs/heads/tabba/guestmem-6.13
[4] https://mirrors.edge.kernel.org/pub/linux/kernel/people/will/docs/qemu/qemu-arm64-howto.html

Fuad Tabba (11):
mm: Consolidate freeing of typed folios on final folio_put()
KVM: guest_memfd: Handle final folio_put() of guest_memfd pages
KVM: guest_memfd: Allow host to map guest_memfd() pages
KVM: guest_memfd: Add KVM capability to check if guest_memfd is shared
KVM: guest_memfd: Handle in-place shared memory as guest_memfd backed
memory
KVM: x86: Mark KVM_X86_SW_PROTECTED_VM as supporting guest_memfd
shared memory
KVM: arm64: Refactor user_mem_abort() calculation of force_pte
KVM: arm64: Handle guest_memfd()-backed guest page faults
KVM: arm64: Introduce KVM_VM_TYPE_ARM_SW_PROTECTED machine type
KVM: arm64: Enable mapping guest_memfd in arm64
KVM: guest_memfd: selftests: guest_memfd mmap() test when mapping is
allowed

 Documentation/virt/kvm/api.rst                |  5 +
 arch/arm64/include/asm/kvm_host.h             | 10 ++
 arch/arm64/kvm/Kconfig                        |  1 +
 arch/arm64/kvm/arm.c                          |  5 +
 arch/arm64/kvm/mmu.c                          | 91 ++++++++++++-------
 arch/x86/include/asm/kvm_host.h               |  5 +
 arch/x86/kvm/Kconfig                          |  3 +-
 include/linux/kvm_host.h                      | 19 +++-
 include/linux/page-flags.h                    | 22 +++++
 include/uapi/linux/kvm.h                      |  7 ++
 mm/debug.c                                    |  1 +
 mm/swap.c                                     | 27 +++++-
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../testing/selftests/kvm/guest_memfd_test.c  | 75 +++++++++++++--
 tools/testing/selftests/kvm/lib/kvm_util.c    |  3 +-
 virt/kvm/Kconfig                              |  4 +
 virt/kvm/guest_memfd.c                        | 90 ++++++++++++++++++
 virt/kvm/kvm_main.c                           |  9 +-
 18 files changed, 326 insertions(+), 52 deletions(-)

base-commit: ffd294d346d185b70e28b1a28abe367bbfe53c04

Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
author
#1 lore raw

RFC PATCH v2 1/11 mm: Consolidate freeing of typed folios on final folio_put()

Some folio types, such as hugetlb, handle freeing their own folios. Moreover, guest_memfd will require being notified once a folio's reference count reaches 0 to facilitate shared to private folio conversion, without the folio actually being freed at that point.

As a first step towards that, this patch consolidates freeing folios that have a type. The first user is hugetlb folios. Later in this patch series, guest_memfd will become the second user of this.

---
 include/linux/page-flags.h | 15 +++++++++++++++
 mm/swap.c                  | 22 +++++++++++++++++-----
 2 files changed, 32 insertions(+), 5 deletions(-)
include/linux/page-flags.h +15 -0
index 691506bdf2c5..6615f2f59144 100644
@@ -962,6 +962,21 @@ static inline bool page_has_type(const struct page *page)
 	return page_mapcount_is_type(data_race(page->page_type));
 }
 
+static inline int page_get_type(const struct page *page)
+{
+	return page->page_type >> 24;
+}
+
+static inline bool folio_has_type(const struct folio *folio)
+{
+	return page_has_type(&folio->page);
+}
+
+static inline int folio_get_type(const struct folio *folio)
+{
+	return page_get_type(&folio->page);
+}
+
 #define FOLIO_TYPE_OPS(lname, fname)					\
 static __always_inline bool folio_test_##fname(const struct folio *folio) \
 {									\
mm/swap.c +17 -5
index 10decd9dffa1..8a66cd9cb9da 100644
@@ -94,6 +94,18 @@ static void page_cache_release(struct folio *folio)
 		unlock_page_lruvec_irqrestore(lruvec, flags);
 }
 
+static void free_typed_folio(struct folio *folio)
+{
+	switch (folio_get_type(folio)) {
+	case PGTY_hugetlb:
+		if (IS_ENABLED(CONFIG_HUGETLBFS))
+			free_huge_folio(folio);
+		return;
+	default:
+		WARN_ON_ONCE(1);
+	}
+}
+
 void __folio_put(struct folio *folio)
 {
 	if (unlikely(folio_is_zone_device(folio))) {
@@ -101,8 +113,8 @@ void __folio_put(struct folio *folio)
 		return;
 	}
 
-	if (folio_test_hugetlb(folio)) {
-		free_huge_folio(folio);
+	if (unlikely(folio_has_type(folio))) {
+		free_typed_folio(folio);
 		return;
 	}
 
@@ -934,13 +946,13 @@ void folios_put_refs(struct folio_batch *folios, unsigned int *refs)
 		if (!folio_ref_sub_and_test(folio, nr_refs))
 			continue;
 
-		/* hugetlb has its own memcg */
-		if (folio_test_hugetlb(folio)) {
+		if (unlikely(folio_has_type(folio))) {
+			/* typed folios have their own memcg, if any */
 			if (lruvec) {
 				unlock_page_lruvec_irqrestore(lruvec, flags);
 				lruvec = NULL;
 			}
-			free_huge_folio(folio);
+			free_typed_folio(folio);
 			continue;
 		}
 		folio_unqueue_deferred_split(folio);
Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
author
#2 lore raw

RFC PATCH v2 2/11 KVM: guest_memfd: Handle final folio_put() of guest_memfd pages

In reply to Fuad Tabba

Before transitioning a guest_memfd folio to unshared, thereby disallowing access by the host and allowing the hypervisor to transition its view of the guest page as private, we need to be sure that the host doesn't have any references to the folio.

This patch introduces a new type for guest_memfd folios, which isn't activated in this series but is here as a placeholder and to facilitate the code in the next patch. This will be used in the future to register a callback that informs the guest_memfd subsystem when the last reference is dropped, therefore knowing that the host doesn't have any remaining references.

---
 include/linux/page-flags.h | 7 +++++++
 mm/debug.c                 | 1 +
 mm/swap.c                  | 5 +++++
 3 files changed, 13 insertions(+)
include/linux/page-flags.h +7 -0
index 6615f2f59144..bab3cac1f93b 100644
@@ -942,6 +942,7 @@ enum pagetype {
 	PGTY_slab	= 0xf5,
 	PGTY_zsmalloc	= 0xf6,
 	PGTY_unaccepted	= 0xf7,
+	PGTY_guestmem	= 0xf8,
 
 	PGTY_mapcount_underflow = 0xff
 };
@@ -1091,6 +1092,12 @@ FOLIO_TYPE_OPS(hugetlb, hugetlb)
 FOLIO_TEST_FLAG_FALSE(hugetlb)
 #endif
 
+#ifdef CONFIG_KVM_GMEM_MAPPABLE
+FOLIO_TYPE_OPS(guestmem, guestmem)
+#else
+FOLIO_TEST_FLAG_FALSE(guestmem)
+#endif
+
 PAGE_TYPE_OPS(Zsmalloc, zsmalloc, zsmalloc)
 
 /*
mm/debug.c +1 -0
index 95b6ab809c0e..db93be385ed9 100644
@@ -56,6 +56,7 @@ static const char *page_type_names[] = {
 	DEF_PAGETYPE_NAME(table),
 	DEF_PAGETYPE_NAME(buddy),
 	DEF_PAGETYPE_NAME(unaccepted),
+	DEF_PAGETYPE_NAME(guestmem),
 };
 
 static const char *page_type_name(unsigned int page_type)
mm/swap.c +5 -0
index 8a66cd9cb9da..73d61c7f8edd 100644
@@ -37,6 +37,7 @@
 #include <linux/page_idle.h>
 #include <linux/local_lock.h>
 #include <linux/buffer_head.h>
+#include <linux/kvm_host.h>
 
 #include "internal.h"
 
@@ -101,6 +102,10 @@ static void free_typed_folio(struct folio *folio)
 		if (IS_ENABLED(CONFIG_HUGETLBFS))
 			free_huge_folio(folio);
 		return;
+	case PGTY_guestmem:
+		if (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM))
+			WARN_ONCE(1, "A placeholder that shouldn't trigger.");
+		return;
 	default:
 		WARN_ON_ONCE(1);
 	}
Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
#3 lore raw

Re: [RFC PATCH v2 02/11] KVM: guest_memfd: Handle final folio_put() of guest_memfd pages

kernel test robot noticed the following build errors:

Hi Fuad,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on ffd294d346d185b70e28b1a28abe367bbfe53c04]

url:    https://github.com/intel-lab-lkp/linux/commits/Fuad-Tabba/mm-Consolidate-freeing-of-typed-folios-on-final-folio_put/20250130-012603
base:   ffd294d346d185b70e28b1a28abe367bbfe53c04
patch link:    https://lore.kernel.org/r/20250129172320.950523-3-tabba%40google.com
patch subject: [RFC PATCH v2 02/11] KVM: guest_memfd: Handle final folio_put() of guest_memfd pages
config: um-allnoconfig (https://download.01.org/0day-ci/archive/20250130/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250130/[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 >>):

In file included from mm/swap.c:40:
In file included from include/linux/kvm_host.h:45:

>> arch/x86/include/asm/kvm_host.h:569:33: error: use of undeclared identifier 'X86_PMC_IDX_MAX'
     569 |                 DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
         |                                               ^
   arch/x86/include/asm/kvm_host.h:572:36: error: use of undeclared identifier 'X86_PMC_IDX_MAX'
     572 |         DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);
         |                                           ^
   arch/x86/include/asm/kvm_host.h:573:29: error: use of undeclared identifier 'X86_PMC_IDX_MAX'
     573 |         DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);
         |                                    ^
>> arch/x86/include/asm/kvm_host.h:828:19: error: field has incomplete type 'struct fpu_guest'
     828 |         struct fpu_guest guest_fpu;
         |                          ^
   arch/x86/include/asm/kvm_host.h:828:9: note: forward declaration of 'struct fpu_guest'
     828 |         struct fpu_guest guest_fpu;
         |                ^
>> arch/x86/include/asm/kvm_host.h:1051:29: error: use of undeclared identifier 'PG_LEVEL_1G'
    1051 |         struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES];
         |                                    ^
   arch/x86/include/asm/kvm_host.h:151:28: note: expanded from macro 'KVM_NR_PAGE_SIZES'
     151 | #define KVM_NR_PAGE_SIZES       (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
         |                                  ^
   arch/x86/include/asm/kvm_host.h:150:32: note: expanded from macro 'KVM_MAX_HUGEPAGE_LEVEL'
     150 | #define KVM_MAX_HUGEPAGE_LEVEL  PG_LEVEL_1G
         |                                 ^
>> arch/x86/include/asm/kvm_host.h:1051:29: error: use of undeclared identifier 'PG_LEVEL_4K'
   arch/x86/include/asm/kvm_host.h:151:53: note: expanded from macro 'KVM_NR_PAGE_SIZES'
     151 | #define KVM_NR_PAGE_SIZES       (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
         |                                                           ^
   arch/x86/include/asm/kvm_host.h:1052:36: error: use of undeclared identifier 'PG_LEVEL_1G'
    1052 |         struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
         |                                           ^
   arch/x86/include/asm/kvm_host.h:151:28: note: expanded from macro 'KVM_NR_PAGE_SIZES'
     151 | #define KVM_NR_PAGE_SIZES       (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
         |                                  ^
   arch/x86/include/asm/kvm_host.h:150:32: note: expanded from macro 'KVM_MAX_HUGEPAGE_LEVEL'
     150 | #define KVM_MAX_HUGEPAGE_LEVEL  PG_LEVEL_1G
         |                                 ^
   arch/x86/include/asm/kvm_host.h:1052:36: error: use of undeclared identifier 'PG_LEVEL_4K'
   arch/x86/include/asm/kvm_host.h:151:53: note: expanded from macro 'KVM_NR_PAGE_SIZES'
     151 | #define KVM_NR_PAGE_SIZES       (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
         |                                                           ^
   arch/x86/include/asm/kvm_host.h:1556:20: error: use of undeclared identifier 'PG_LEVEL_1G'
    1556 |                 atomic64_t pages[KVM_NR_PAGE_SIZES];
         |                                  ^
   arch/x86/include/asm/kvm_host.h:151:28: note: expanded from macro 'KVM_NR_PAGE_SIZES'
     151 | #define KVM_NR_PAGE_SIZES       (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
         |                                  ^
   arch/x86/include/asm/kvm_host.h:150:32: note: expanded from macro 'KVM_MAX_HUGEPAGE_LEVEL'
     150 | #define KVM_MAX_HUGEPAGE_LEVEL  PG_LEVEL_1G
         |                                 ^
   arch/x86/include/asm/kvm_host.h:1556:20: error: use of undeclared identifier 'PG_LEVEL_4K'
   arch/x86/include/asm/kvm_host.h:151:53: note: expanded from macro 'KVM_NR_PAGE_SIZES'
     151 | #define KVM_NR_PAGE_SIZES       (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
         |                                                           ^
>> arch/x86/include/asm/kvm_host.h:1623:29: error: use of undeclared identifier 'APIC_DEST_LOGICAL'
    1623 |         return dest_mode_logical ? APIC_DEST_LOGICAL : APIC_DEST_PHYSICAL;
         |                                    ^
>> arch/x86/include/asm/kvm_host.h:1623:49: error: use of undeclared identifier 'APIC_DEST_PHYSICAL'
    1623 |         return dest_mode_logical ? APIC_DEST_LOGICAL : APIC_DEST_PHYSICAL;
         |                                                        ^
>> arch/x86/include/asm/kvm_host.h:1670:48: warning: declaration of 'struct desc_ptr' will not be visible outside of this function [-Wvisibility]
    1670 |         void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^
   arch/x86/include/asm/kvm_host.h:1671:48: warning: declaration of 'struct desc_ptr' will not be visible outside of this function [-Wvisibility]
    1671 |         void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^
   arch/x86/include/asm/kvm_host.h:1672:48: warning: declaration of 'struct desc_ptr' will not be visible outside of this function [-Wvisibility]
    1672 |         void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^
   arch/x86/include/asm/kvm_host.h:1673:48: warning: declaration of 'struct desc_ptr' will not be visible outside of this function [-Wvisibility]
    1673 |         void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^
>> arch/x86/include/asm/kvm_host.h:2327:32: error: use of undeclared identifier 'APIC_DM_FIXED'
    2327 |         return (irq->delivery_mode == APIC_DM_FIXED ||
         |                                       ^
>> arch/x86/include/asm/kvm_host.h:2328:25: error: use of undeclared identifier 'APIC_DM_LOWEST'
    2328 |                 irq->delivery_mode == APIC_DM_LOWEST);
         |                                       ^
>> arch/x86/include/asm/kvm_host.h:2347:9: error: use of undeclared identifier 'BAD_APICID'
    2347 |         return BAD_APICID;
         |                ^

4 warnings and 15 errors generated.

vim +/X86_PMC_IDX_MAX +569 arch/x86/include/asm/kvm_host.h

f5132b01386b5a Gleb Natapov        2011-11-10  533  
4f1fa2a1bbeb2f Like Xu             2022-09-19  534  /* More counters may conflict with other existing Architectural MSRs */
f287bef6ddc208 Dapeng Mi           2024-06-27  535  #define KVM_MAX(a, b)	((a) >= (b) ? (a) : (b))
f287bef6ddc208 Dapeng Mi           2024-06-27  536  #define KVM_MAX_NR_INTEL_GP_COUNTERS	8
f287bef6ddc208 Dapeng Mi           2024-06-27  537  #define KVM_MAX_NR_AMD_GP_COUNTERS	6
f287bef6ddc208 Dapeng Mi           2024-06-27  538  #define KVM_MAX_NR_GP_COUNTERS		KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \
f287bef6ddc208 Dapeng Mi           2024-06-27  539  						KVM_MAX_NR_AMD_GP_COUNTERS)
f287bef6ddc208 Dapeng Mi           2024-06-27  540  
f287bef6ddc208 Dapeng Mi           2024-06-27  541  #define KVM_MAX_NR_INTEL_FIXED_COUTNERS	3
f287bef6ddc208 Dapeng Mi           2024-06-27  542  #define KVM_MAX_NR_AMD_FIXED_COUTNERS	0
f287bef6ddc208 Dapeng Mi           2024-06-27  543  #define KVM_MAX_NR_FIXED_COUNTERS	KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUTNERS, \
f287bef6ddc208 Dapeng Mi           2024-06-27  544  						KVM_MAX_NR_AMD_FIXED_COUTNERS)
004a0aa56edea3 Sean Christopherson 2023-11-09  545  
f5132b01386b5a Gleb Natapov        2011-11-10  546  struct kvm_pmu {
12aad9164763bb Mathias Krause      2023-02-17  547  	u8 version;
f5132b01386b5a Gleb Natapov        2011-11-10  548  	unsigned nr_arch_gp_counters;
f5132b01386b5a Gleb Natapov        2011-11-10  549  	unsigned nr_arch_fixed_counters;
f5132b01386b5a Gleb Natapov        2011-11-10  550  	unsigned available_event_types;
f5132b01386b5a Gleb Natapov        2011-11-10  551  	u64 fixed_ctr_ctrl;
0e102ce3d41331 Dapeng Mi           2024-04-30  552  	u64 fixed_ctr_ctrl_rsvd;
f5132b01386b5a Gleb Natapov        2011-11-10  553  	u64 global_ctrl;
f5132b01386b5a Gleb Natapov        2011-11-10  554  	u64 global_status;
f5132b01386b5a Gleb Natapov        2011-11-10  555  	u64 counter_bitmask[2];
0e102ce3d41331 Dapeng Mi           2024-04-30  556  	u64 global_ctrl_rsvd;
0e102ce3d41331 Dapeng Mi           2024-04-30  557  	u64 global_status_rsvd;
103af0a9878859 Andi Kleen          2013-07-18  558  	u64 reserved_bits;
95b065bf5c431c Jim Mattson         2022-03-07  559  	u64 raw_event_mask;
f287bef6ddc208 Dapeng Mi           2024-06-27  560  	struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
f287bef6ddc208 Dapeng Mi           2024-06-27  561  	struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
f1c5651fda43e0 Sean Christopherson 2022-09-23  562  
f1c5651fda43e0 Sean Christopherson 2022-09-23  563  	/*
f1c5651fda43e0 Sean Christopherson 2022-09-23  564  	 * Overlay the bitmap with a 64-bit atomic so that all bits can be
f1c5651fda43e0 Sean Christopherson 2022-09-23  565  	 * set in a single access, e.g. to reprogram all counters when the PMU
f1c5651fda43e0 Sean Christopherson 2022-09-23  566  	 * filter changes.
f1c5651fda43e0 Sean Christopherson 2022-09-23  567  	 */
f1c5651fda43e0 Sean Christopherson 2022-09-23  568  	union {
4be946728f65c1 Like Xu             2019-10-21 @569  		DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
f1c5651fda43e0 Sean Christopherson 2022-09-23  570  		atomic64_t __reprogram_pmi;
f1c5651fda43e0 Sean Christopherson 2022-09-23  571  	};
b35e5548b41131 Like Xu             2019-10-27  572  	DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);
b35e5548b41131 Like Xu             2019-10-27  573  	DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);
b35e5548b41131 Like Xu             2019-10-27  574  
8183a538cd95f7 Like Xu             2022-04-11  575  	u64 ds_area;
c59a1f106f5cd4 Like Xu             2022-04-11  576  	u64 pebs_enable;
0e102ce3d41331 Dapeng Mi           2024-04-30  577  	u64 pebs_enable_rsvd;
902caeb6841a64 Like Xu             2022-04-11  578  	u64 pebs_data_cfg;
0e102ce3d41331 Dapeng Mi           2024-04-30  579  	u64 pebs_data_cfg_rsvd;
c59a1f106f5cd4 Like Xu             2022-04-11  580  
854250329c02c0 Like Xu             2022-04-11  581  	/*
854250329c02c0 Like Xu             2022-04-11  582  	 * If a guest counter is cross-mapped to host counter with different
854250329c02c0 Like Xu             2022-04-11  583  	 * index, its PEBS capability will be temporarily disabled.
854250329c02c0 Like Xu             2022-04-11  584  	 *
854250329c02c0 Like Xu             2022-04-11  585  	 * The user should make sure that this mask is updated
854250329c02c0 Like Xu             2022-04-11  586  	 * after disabling interrupts and before perf_guest_get_msrs();
854250329c02c0 Like Xu             2022-04-11  587  	 */
854250329c02c0 Like Xu             2022-04-11  588  	u64 host_cross_mapped_mask;
854250329c02c0 Like Xu             2022-04-11  589  
b35e5548b41131 Like Xu             2019-10-27  590  	/*
b35e5548b41131 Like Xu             2019-10-27  591  	 * The gate to release perf_events not marked in
b35e5548b41131 Like Xu             2019-10-27  592  	 * pmc_in_use only once in a vcpu time slice.
b35e5548b41131 Like Xu             2019-10-27  593  	 */
b35e5548b41131 Like Xu             2019-10-27  594  	bool need_cleanup;
b35e5548b41131 Like Xu             2019-10-27  595  
b35e5548b41131 Like Xu             2019-10-27  596  	/*
b35e5548b41131 Like Xu             2019-10-27  597  	 * The total number of programmed perf_events and it helps to avoid
b35e5548b41131 Like Xu             2019-10-27  598  	 * redundant check before cleanup if guest don't use vPMU at all.
b35e5548b41131 Like Xu             2019-10-27  599  	 */
b35e5548b41131 Like Xu             2019-10-27  600  	u8 event_count;
f5132b01386b5a Gleb Natapov        2011-11-10  601  };
f5132b01386b5a Gleb Natapov        2011-11-10  602  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

Re: [RFC PATCH v2 02/11] KVM: guest_memfd: Handle final folio_put() of guest_memfd pages

In reply to Fuad Tabba
kernel test robot noticed the following build warnings:

Hi Fuad,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on ffd294d346d185b70e28b1a28abe367bbfe53c04]

url:    https://github.com/intel-lab-lkp/linux/commits/Fuad-Tabba/mm-Consolidate-freeing-of-typed-folios-on-final-folio_put/20250130-012603
base:   ffd294d346d185b70e28b1a28abe367bbfe53c04
patch link:    https://lore.kernel.org/r/20250129172320.950523-3-tabba%40google.com
patch subject: [RFC PATCH v2 02/11] KVM: guest_memfd: Handle final folio_put() of guest_memfd pages
config: um-allyesconfig (https://download.01.org/0day-ci/archive/20250130/[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/20250130/[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 warnings (new ones prefixed by >>):

In file included from include/vdso/const.h:5,

                    from include/linux/const.h:4,
                    from include/linux/bits.h:5,
                    from include/linux/ratelimit_types.h:5,
                    from include/linux/printk.h:9,
                    from include/asm-generic/bug.h:22,
                    from ./arch/um/include/generated/asm/bug.h:1,
                    from include/linux/bug.h:5,
                    from include/linux/mmdebug.h:5,
                    from include/linux/mm.h:6,
                    from mm/swap.c:17:
   arch/x86/include/asm/kvm_host.h:569:47: error: 'X86_PMC_IDX_MAX' undeclared here (not in a function)
     569 |                 DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
         |                                               ^~~~~~~~~~~~~~~
   include/uapi/linux/const.h:51:40: note: in definition of macro '__KERNEL_DIV_ROUND_UP'
      51 | #define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
         |                                        ^
   include/linux/types.h:11:28: note: in expansion of macro 'BITS_TO_LONGS'
      11 |         unsigned long name[BITS_TO_LONGS(bits)]
         |                            ^~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:569:17: note: in expansion of macro 'DECLARE_BITMAP'
     569 |                 DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
         |                 ^~~~~~~~~~~~~~
   In file included from include/linux/kvm_host.h:45,
                    from mm/swap.c:40:
   arch/x86/include/asm/kvm_host.h:828:26: error: field 'guest_fpu' has incomplete type
     828 |         struct fpu_guest guest_fpu;
         |                          ^~~~~~~~~
   arch/x86/include/asm/kvm_host.h:150:33: error: 'PG_LEVEL_1G' undeclared here (not in a function)
     150 | #define KVM_MAX_HUGEPAGE_LEVEL  PG_LEVEL_1G
         |                                 ^~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:151:34: note: in expansion of macro 'KVM_MAX_HUGEPAGE_LEVEL'
     151 | #define KVM_NR_PAGE_SIZES       (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
         |                                  ^~~~~~~~~~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:1051:36: note: in expansion of macro 'KVM_NR_PAGE_SIZES'
    1051 |         struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES];
         |                                    ^~~~~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:151:59: error: 'PG_LEVEL_4K' undeclared here (not in a function)
     151 | #define KVM_NR_PAGE_SIZES       (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
         |                                                           ^~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:1051:36: note: in expansion of macro 'KVM_NR_PAGE_SIZES'
    1051 |         struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES];
         |                                    ^~~~~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h: In function 'kvm_lapic_irq_dest_mode':
   arch/x86/include/asm/kvm_host.h:1623:36: error: 'APIC_DEST_LOGICAL' undeclared (first use in this function)
    1623 |         return dest_mode_logical ? APIC_DEST_LOGICAL : APIC_DEST_PHYSICAL;
         |                                    ^~~~~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:1623:36: note: each undeclared identifier is reported only once for each function it appears in
   arch/x86/include/asm/kvm_host.h:1623:56: error: 'APIC_DEST_PHYSICAL' undeclared (first use in this function)
    1623 |         return dest_mode_logical ? APIC_DEST_LOGICAL : APIC_DEST_PHYSICAL;
         |                                                        ^~~~~~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h: At top level:
>> arch/x86/include/asm/kvm_host.h:1670:55: warning: 'struct desc_ptr' declared inside parameter list will not be visible outside of this definition or declaration
    1670 |         void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^~~~~~~~
   arch/x86/include/asm/kvm_host.h:1671:55: warning: 'struct desc_ptr' declared inside parameter list will not be visible outside of this definition or declaration
    1671 |         void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^~~~~~~~
   arch/x86/include/asm/kvm_host.h:1672:55: warning: 'struct desc_ptr' declared inside parameter list will not be visible outside of this definition or declaration
    1672 |         void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^~~~~~~~
   arch/x86/include/asm/kvm_host.h:1673:55: warning: 'struct desc_ptr' declared inside parameter list will not be visible outside of this definition or declaration
    1673 |         void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^~~~~~~~
   arch/x86/include/asm/kvm_host.h: In function 'read_msr':
   arch/x86/include/asm/kvm_host.h:2228:9: error: implicit declaration of function 'rdmsrl' [-Werror=implicit-function-declaration]
    2228 |         rdmsrl(msr, value);
         |         ^~~~~~
   arch/x86/include/asm/kvm_host.h: In function 'kvm_irq_is_postable':
   arch/x86/include/asm/kvm_host.h:2327:39: error: 'APIC_DM_FIXED' undeclared (first use in this function)
    2327 |         return (irq->delivery_mode == APIC_DM_FIXED ||
         |                                       ^~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:2328:39: error: 'APIC_DM_LOWEST' undeclared (first use in this function)
    2328 |                 irq->delivery_mode == APIC_DM_LOWEST);
         |                                       ^~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h: In function 'kvm_cpu_get_apicid':
   arch/x86/include/asm/kvm_host.h:2347:16: error: 'BAD_APICID' undeclared (first use in this function)
    2347 |         return BAD_APICID;
         |                ^~~~~~~~~~

cc1: some warnings being treated as errors

vim +1670 arch/x86/include/asm/kvm_host.h

c96001c5702e66 arch/x86/include/asm/kvm_host.h Peter Xu              2019-12-04  1625  
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1626  struct kvm_x86_ops {
9dadfc4a6145e1 arch/x86/include/asm/kvm_host.h Sean Christopherson   2021-10-18  1627  	const char *name;
9dadfc4a6145e1 arch/x86/include/asm/kvm_host.h Sean Christopherson   2021-10-18  1628  
d83420c2d74e66 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-11-30  1629  	int (*check_processor_compatibility)(void);
d83420c2d74e66 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-11-30  1630  
0617a769ce16b8 arch/x86/include/asm/kvm_host.h Sean Christopherson   2024-08-29  1631  	int (*enable_virtualization_cpu)(void);
0617a769ce16b8 arch/x86/include/asm/kvm_host.h Sean Christopherson   2024-08-29  1632  	void (*disable_virtualization_cpu)(void);
590b09b1d88e18 arch/x86/include/asm/kvm_host.h Sean Christopherson   2024-08-29  1633  	cpu_emergency_virt_cb *emergency_disable_virtualization_cpu;
590b09b1d88e18 arch/x86/include/asm/kvm_host.h Sean Christopherson   2024-08-29  1634  
6e4fd06f3ee1b1 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-03-21  1635  	void (*hardware_unsetup)(void);
5719455fbd952a arch/x86/include/asm/kvm_host.h Tom Lendacky          2020-12-10  1636  	bool (*has_emulated_msr)(struct kvm *kvm, u32 index);
7c1b761be029da arch/x86/include/asm/kvm_host.h Xiaoyao Li            2020-07-09  1637  	void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1638  
562b6b089d6472 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-01-26  1639  	unsigned int vm_size;
03543133cea646 arch/x86/include/asm/kvm_host.h Suravee Suthikulpanit 2016-05-04  1640  	int (*vm_init)(struct kvm *kvm);
03543133cea646 arch/x86/include/asm/kvm_host.h Suravee Suthikulpanit 2016-05-04  1641  	void (*vm_destroy)(struct kvm *kvm);
03543133cea646 arch/x86/include/asm/kvm_host.h Suravee Suthikulpanit 2016-05-04  1642  
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1643  	/* Create, but do not attach this VCPU */
d588bb9be1da6a arch/x86/include/asm/kvm_host.h Chao Gao              2022-04-19  1644  	int (*vcpu_precreate)(struct kvm *kvm);
987b2594ed5d12 arch/x86/include/asm/kvm_host.h Sean Christopherson   2019-12-18  1645  	int (*vcpu_create)(struct kvm_vcpu *vcpu);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1646  	void (*vcpu_free)(struct kvm_vcpu *vcpu);
d28bc9dd25ce02 arch/x86/include/asm/kvm_host.h Nadav Amit            2015-04-13  1647  	void (*vcpu_reset)(struct kvm_vcpu *vcpu, bool init_event);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1648  
e27bc0440ebd14 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-01-28  1649  	void (*prepare_switch_to_guest)(struct kvm_vcpu *vcpu);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1650  	void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1651  	void (*vcpu_put)(struct kvm_vcpu *vcpu);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1652  
6986982fef86ae arch/x86/include/asm/kvm_host.h Paolo Bonzini         2020-07-10  1653  	void (*update_exception_bitmap)(struct kvm_vcpu *vcpu);
609e36d372ad93 arch/x86/include/asm/kvm_host.h Paolo Bonzini         2015-04-08  1654  	int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
8fe8ab46be06fc arch/x86/include/asm/kvm_host.h Will Auld             2012-11-29  1655  	int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1656  	u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1657  	void (*get_segment)(struct kvm_vcpu *vcpu,
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1658  			    struct kvm_segment *var, int seg);
2e4d2653497856 include/asm-x86/kvm_host.h      Izik Eidus            2008-03-24  1659  	int (*get_cpl)(struct kvm_vcpu *vcpu);
f0e7012c4b9386 arch/x86/include/asm/kvm_host.h Sean Christopherson   2024-10-09  1660  	int (*get_cpl_no_cache)(struct kvm_vcpu *vcpu);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1661  	void (*set_segment)(struct kvm_vcpu *vcpu,
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1662  			    struct kvm_segment *var, int seg);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1663  	void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
26a0652cb453c7 arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-06-13  1664  	bool (*is_valid_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1665  	void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
405329fc9aeef1 arch/x86/include/asm/kvm_host.h Michael Roth          2021-12-16  1666  	void (*post_set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
26a0652cb453c7 arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-06-13  1667  	bool (*is_valid_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
c2fe3cd4604ac8 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-10-06  1668  	void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
72f211ecaa80a0 arch/x86/include/asm/kvm_host.h Maxim Levitsky        2020-10-01  1669  	int (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
89a27f4d0e042a arch/x86/include/asm/kvm_host.h Gleb Natapov          2010-02-16 @1670  	void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
89a27f4d0e042a arch/x86/include/asm/kvm_host.h Gleb Natapov          2010-02-16  1671  	void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
89a27f4d0e042a arch/x86/include/asm/kvm_host.h Gleb Natapov          2010-02-16  1672  	void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
89a27f4d0e042a arch/x86/include/asm/kvm_host.h Gleb Natapov          2010-02-16  1673  	void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
c77fb5fe6f031b arch/x86/include/asm/kvm_host.h Paolo Bonzini         2014-02-21  1674  	void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu);
020df0794f5764 arch/x86/include/asm/kvm_host.h Gleb Natapov          2010-04-13  1675  	void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
5fdbf9765b7ba6 include/asm-x86/kvm_host.h      Marcelo Tosatti       2008-06-27  1676  	void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1677  	unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1678  	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
c5063551bfcae4 arch/x86/include/asm/kvm_host.h Marc Orr              2021-12-09  1679  	bool (*get_if_flag)(struct kvm_vcpu *vcpu);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1680  
e27bc0440ebd14 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-01-28  1681  	void (*flush_tlb_all)(struct kvm_vcpu *vcpu);
e27bc0440ebd14 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-01-28  1682  	void (*flush_tlb_current)(struct kvm_vcpu *vcpu);
0277022a77a56f arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-10-18  1683  #if IS_ENABLED(CONFIG_HYPERV)
8a1300ff95185b arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-04-04  1684  	int  (*flush_remote_tlbs)(struct kvm *kvm);
9ed3bf411226f4 arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-04-04  1685  	int  (*flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn,
9ed3bf411226f4 arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-04-04  1686  					gfn_t nr_pages);
0277022a77a56f arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-10-18  1687  #endif
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1688  
faff87588d8bfd arch/x86/include/asm/kvm_host.h Junaid Shahid         2018-06-29  1689  	/*
faff87588d8bfd arch/x86/include/asm/kvm_host.h Junaid Shahid         2018-06-29  1690  	 * Flush any TLB entries associated with the given GVA.
faff87588d8bfd arch/x86/include/asm/kvm_host.h Junaid Shahid         2018-06-29  1691  	 * Does not need to flush GPA->HPA mappings.
faff87588d8bfd arch/x86/include/asm/kvm_host.h Junaid Shahid         2018-06-29  1692  	 * Can potentially get non-canonical addresses through INVLPGs, which
faff87588d8bfd arch/x86/include/asm/kvm_host.h Junaid Shahid         2018-06-29  1693  	 * the implementation may choose to ignore if appropriate.
faff87588d8bfd arch/x86/include/asm/kvm_host.h Junaid Shahid         2018-06-29  1694  	 */
e27bc0440ebd14 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-01-28  1695  	void (*flush_tlb_gva)(struct kvm_vcpu *vcpu, gva_t addr);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1696  
e64419d991ea21 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-03-20  1697  	/*
e64419d991ea21 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-03-20  1698  	 * Flush any TLB entries created by the guest.  Like tlb_flush_gva(),
e64419d991ea21 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-03-20  1699  	 * does not need to flush GPA->HPA mappings.
e64419d991ea21 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-03-20  1700  	 */
e27bc0440ebd14 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-01-28  1701  	void (*flush_tlb_guest)(struct kvm_vcpu *vcpu);
e64419d991ea21 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-03-20  1702  
fc4fad79fc3d88 arch/x86/include/asm/kvm_host.h Sean Christopherson   2021-12-28  1703  	int (*vcpu_pre_run)(struct kvm_vcpu *vcpu);
9c9025ea003a03 arch/x86/include/asm/kvm_host.h Sean Christopherson   2024-01-09  1704  	enum exit_fastpath_completion (*vcpu_run)(struct kvm_vcpu *vcpu,
9c9025ea003a03 arch/x86/include/asm/kvm_host.h Sean Christopherson   2024-01-09  1705  						  bool force_immediate_exit);
1e9e2622a149e8 arch/x86/include/asm/kvm_host.h Wanpeng Li            2019-11-21  1706  	int (*handle_exit)(struct kvm_vcpu *vcpu,
1e9e2622a149e8 arch/x86/include/asm/kvm_host.h Wanpeng Li            2019-11-21  1707  		enum exit_fastpath_completion exit_fastpath);
f8ea7c6049d5d0 arch/x86/include/asm/kvm_host.h Vitaly Kuznetsov      2019-08-13  1708  	int (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
5ef8acbdd687c9 arch/x86/include/asm/kvm_host.h Oliver Upton          2020-02-07  1709  	void (*update_emulated_instruction)(struct kvm_vcpu *vcpu);
2809f5d2c4cfad arch/x86/include/asm/kvm_host.h Glauber Costa         2009-05-12  1710  	void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask);
37ccdcbe075719 arch/x86/include/asm/kvm_host.h Paolo Bonzini         2014-05-20  1711  	u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1712  	void (*patch_hypercall)(struct kvm_vcpu *vcpu,
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1713  				unsigned char *hypercall_addr);
2d61391270a3ce arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-05-02  1714  	void (*inject_irq)(struct kvm_vcpu *vcpu, bool reinjected);
e27bc0440ebd14 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-01-28  1715  	void (*inject_nmi)(struct kvm_vcpu *vcpu);
6ad75c5c99f78e arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-08-30  1716  	void (*inject_exception)(struct kvm_vcpu *vcpu);
b463a6f744a263 arch/x86/include/asm/kvm_host.h Avi Kivity            2010-07-20  1717  	void (*cancel_injection)(struct kvm_vcpu *vcpu);
c9d40913ac5a21 arch/x86/include/asm/kvm_host.h Paolo Bonzini         2020-05-22  1718  	int (*interrupt_allowed)(struct kvm_vcpu *vcpu, bool for_injection);
c9d40913ac5a21 arch/x86/include/asm/kvm_host.h Paolo Bonzini         2020-05-22  1719  	int (*nmi_allowed)(struct kvm_vcpu *vcpu, bool for_injection);
3cfc3092f40bc3 arch/x86/include/asm/kvm_host.h Jan Kiszka            2009-11-12  1720  	bool (*get_nmi_mask)(struct kvm_vcpu *vcpu);
3cfc3092f40bc3 arch/x86/include/asm/kvm_host.h Jan Kiszka            2009-11-12  1721  	void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked);
fa4c027a7956f5 arch/x86/include/asm/kvm_host.h Santosh Shukla        2023-02-27  1722  	/* Whether or not a virtual NMI is pending in hardware. */
fa4c027a7956f5 arch/x86/include/asm/kvm_host.h Santosh Shukla        2023-02-27  1723  	bool (*is_vnmi_pending)(struct kvm_vcpu *vcpu);
fa4c027a7956f5 arch/x86/include/asm/kvm_host.h Santosh Shukla        2023-02-27  1724  	/*
54aa699e8094ef arch/x86/include/asm/kvm_host.h Bjorn Helgaas         2024-01-02  1725  	 * Attempt to pend a virtual NMI in hardware.  Returns %true on success
fa4c027a7956f5 arch/x86/include/asm/kvm_host.h Santosh Shukla        2023-02-27  1726  	 * to allow using static_call_ret0 as the fallback.
fa4c027a7956f5 arch/x86/include/asm/kvm_host.h Santosh Shukla        2023-02-27  1727  	 */
fa4c027a7956f5 arch/x86/include/asm/kvm_host.h Santosh Shukla        2023-02-27  1728  	bool (*set_vnmi_pending)(struct kvm_vcpu *vcpu);
c9a7953f09bbe2 arch/x86/include/asm/kvm_host.h Jan Kiszka            2014-03-07  1729  	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
c9a7953f09bbe2 arch/x86/include/asm/kvm_host.h Jan Kiszka            2014-03-07  1730  	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
95ba82731374eb arch/x86/include/asm/kvm_host.h Gleb Natapov          2009-04-21  1731  	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
73b42dc69be856 arch/x86/include/asm/kvm_host.h Sean Christopherson   2024-07-19  1732  
73b42dc69be856 arch/x86/include/asm/kvm_host.h Sean Christopherson   2024-07-19  1733  	const bool x2apic_icr_is_split;
b3f257a846960d arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-01-06  1734  	const unsigned long required_apicv_inhibits;
2008fab3453019 arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-01-06  1735  	bool allow_apicv_in_x2apic_without_x2apic_virtualization;
d62caabb41f33d arch/x86/include/asm/kvm_host.h Andrey Smetanin       2015-11-10  1736  	void (*refresh_apicv_exec_ctrl)(struct kvm_vcpu *vcpu);
c7c9c56ca26f7b arch/x86/include/asm/kvm_host.h Yang Zhang            2013-01-25  1737  	void (*hwapic_irr_update)(struct kvm_vcpu *vcpu, int max_irr);
d39850f57d2102 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-06-14  1738  	void (*hwapic_isr_update)(int isr);
6308630bd3dbb6 arch/x86/include/asm/kvm_host.h Andrey Smetanin       2015-11-10  1739  	void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
8d860bbeedef97 arch/x86/include/asm/kvm_host.h Jim Mattson           2018-05-09  1740  	void (*set_virtual_apic_mode)(struct kvm_vcpu *vcpu);
a4148b7ca2a5af arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-03-20  1741  	void (*set_apic_access_page_addr)(struct kvm_vcpu *vcpu);
57dfd7b53dec74 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-01-28  1742  	void (*deliver_interrupt)(struct kvm_lapic *apic, int delivery_mode,
57dfd7b53dec74 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-01-28  1743  				  int trig_mode, int vector);
76dfafd536730e arch/x86/include/asm/kvm_host.h Paolo Bonzini         2016-12-19  1744  	int (*sync_pir_to_irr)(struct kvm_vcpu *vcpu);
ea4a5ff80c24a2 drivers/kvm/x86.h               Zhang Xiantao         2007-11-19  1745  	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
2ac52ab861b920 arch/x86/include/asm/kvm_host.h Sean Christopherson   2018-03-20  1746  	int (*set_identity_map_addr)(struct kvm *kvm, u64 ident_addr);
ba28401bb93e5c arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-07-14  1747  	u8 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
344f414fa0f162 arch/x86/include/asm/kvm_host.h Joerg Roedel          2009-07-27  1748  
e83bc09caf1671 arch/x86/include/asm/kvm_host.h Sean Christopherson   2021-03-05  1749  	void (*load_mmu_pgd)(struct kvm_vcpu *vcpu, hpa_t root_hpa,
e83bc09caf1671 arch/x86/include/asm/kvm_host.h Sean Christopherson   2021-03-05  1750  			     int root_level);
727a7e27cf88a2 arch/x86/include/asm/kvm_host.h Paolo Bonzini         2020-03-05  1751  
f5f48ee15c2ee3 arch/x86/include/asm/kvm_host.h Sheng Yang            2010-06-30  1752  	bool (*has_wbinvd_exit)(void);
f5f48ee15c2ee3 arch/x86/include/asm/kvm_host.h Sheng Yang            2010-06-30  1753  
307a94c721fed1 arch/x86/include/asm/kvm_host.h Ilias Stamatis        2021-05-26  1754  	u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);
307a94c721fed1 arch/x86/include/asm/kvm_host.h Ilias Stamatis        2021-05-26  1755  	u64 (*get_l2_tsc_multiplier)(struct kvm_vcpu *vcpu);
2d63699099ac1f arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-07-28  1756  	void (*write_tsc_offset)(struct kvm_vcpu *vcpu);
2d63699099ac1f arch/x86/include/asm/kvm_host.h Sean Christopherson   2023-07-28  1757  	void (*write_tsc_multiplier)(struct kvm_vcpu *vcpu);
99e3e30aee1a32 arch/x86/include/asm/kvm_host.h Zachary Amsden        2010-08-19  1758  
235ba74f008d2e arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-09-23  1759  	/*
0a62a0319abb92 arch/x86/include/asm/kvm_host.h David Edmondson       2021-09-20  1760  	 * Retrieve somewhat arbitrary exit information.  Intended to
0a62a0319abb92 arch/x86/include/asm/kvm_host.h David Edmondson       2021-09-20  1761  	 * be used only from within tracepoints or error paths.
235ba74f008d2e arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-09-23  1762  	 */
0a62a0319abb92 arch/x86/include/asm/kvm_host.h David Edmondson       2021-09-20  1763  	void (*get_exit_info)(struct kvm_vcpu *vcpu, u32 *reason,
0a62a0319abb92 arch/x86/include/asm/kvm_host.h David Edmondson       2021-09-20  1764  			      u64 *info1, u64 *info2,
235ba74f008d2e arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-09-23  1765  			      u32 *exit_int_info, u32 *exit_int_info_err_code);
8a76d7f25f8f24 arch/x86/include/asm/kvm_host.h Joerg Roedel          2011-04-04  1766  
8a76d7f25f8f24 arch/x86/include/asm/kvm_host.h Joerg Roedel          2011-04-04  1767  	int (*check_intercept)(struct kvm_vcpu *vcpu,
8a76d7f25f8f24 arch/x86/include/asm/kvm_host.h Joerg Roedel          2011-04-04  1768  			       struct x86_instruction_info *info,
21f1b8f29ea5b2 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-02-18  1769  			       enum x86_intercept_stage stage,
21f1b8f29ea5b2 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-02-18  1770  			       struct x86_exception *exception);
a9ab13ff6e844a arch/x86/include/asm/kvm_host.h Wanpeng Li            2020-04-10  1771  	void (*handle_exit_irqoff)(struct kvm_vcpu *vcpu);
7f5581f5929849 arch/x86/include/asm/kvm_host.h Sean Christopherson   2020-03-02  1772  
88178fd4f7187b arch/x86/include/asm/kvm_host.h Kai Huang             2015-01-28  1773  	/*
a018eba53870aa arch/x86/include/asm/kvm_host.h Sean Christopherson   2021-02-12  1774  	 * Size of the CPU's dirty log buffer, i.e. VMX's PML buffer.  A zero
a018eba53870aa arch/x86/include/asm/kvm_host.h Sean Christopherson   2021-02-12  1775  	 * value indicates CPU dirty logging is unsupported or disabled.
88178fd4f7187b arch/x86/include/asm/kvm_host.h Kai Huang             2015-01-28  1776  	 */
6dd03800b1afe4 arch/x86/include/asm/kvm_host.h Sean Christopherson   2021-02-12  1777  	int cpu_dirty_log_size;
a85863c2ec55ed arch/x86/include/asm/kvm_host.h Makarand Sonare       2021-02-12  1778  	void (*update_cpu_dirty_logging)(struct kvm_vcpu *vcpu);
bab4165e2f0319 arch/x86/include/asm/kvm_host.h Bandan Das            2017-05-05  1779  
33b22172452f05 arch/x86/include/asm/kvm_host.h Paolo Bonzini         2020-04-17  1780  	const struct kvm_x86_nested_ops *nested_ops;
efc644048ecde5 arch/x86/include/asm/kvm_host.h Feng Wu               2015-09-18  1781  
d1ed092f77ef56 arch/x86/include/asm/kvm_host.h Suravee Suthikulpanit 2016-05-04  1782  	void (*vcpu_blocking)(struct kvm_vcpu *vcpu);
d1ed092f77ef56 arch/x86/include/asm/kvm_host.h Suravee Suthikulpanit 2016-05-04  1783  	void (*vcpu_unblocking)(struct kvm_vcpu *vcpu);
d1ed092f77ef56 arch/x86/include/asm/kvm_host.h Suravee Suthikulpanit 2016-05-04  1784  
e27bc0440ebd14 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-01-28  1785  	int (*pi_update_irte)(struct kvm *kvm, unsigned int host_irq,
efc644048ecde5 arch/x86/include/asm/kvm_host.h Feng Wu               2015-09-18  1786  			      uint32_t guest_irq, bool set);
e27bc0440ebd14 arch/x86/include/asm/kvm_host.h Sean Christopherson   2022-01-28  1787  	void (*pi_start_assignment)(struct kvm *kvm);
9cfec6d097c607 arch/x86/include/asm/kvm_host.h Haitao Shan           2023-09-12  1788  	void (*apicv_pre_state_restore)(struct kvm_vcpu *vcpu);
be8ca170edf342 arch/x86/include/asm/kvm_host.h Suravee Suthikulpanit 2016-05-04  1789  	void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);
17e433b54393a6 arch/x86/include/asm/kvm_host.h Wanpeng Li            2019-08-05  1790  	bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu);
ce7a058a2117f0 arch/x86/include/asm/kvm_host.h Yunhong Jiang         2016-06-13  1791  
f99279825ee30b arch/x86/include/asm/kvm_host.h Sean Christopherson   2019-04-16  1792  	int (*set_hv_timer)(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
f99279825ee30b arch/x86/include/asm/kvm_host.h Sean Christopherson   2019-04-16  1793  			    bool *expired);
ce7a058a2117f0 arch/x86/include/asm/kvm_host.h Yunhong Jiang         2016-06-13  1794  	void (*cancel_hv_timer)(struct kvm_vcpu *vcpu);
c45dcc71b794b5 arch/x86/include/asm/kvm_host.h Ashok Raj             2016-06-22  1795  
c45dcc71b794b5 arch/x86/include/asm/kvm_host.h Ashok Raj             2016-06-22  1796  	void (*setup_mce)(struct kvm_vcpu *vcpu);
0234bf885236a4 arch/x86/include/asm/kvm_host.h Ladi Prosek           2017-10-11  1797  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw

Re: [RFC PATCH v2 02/11] KVM: guest_memfd: Handle final folio_put() of guest_memfd pages

In reply to Fuad Tabba
Quoted 32 lines · Fuad Tabba

Before transitioning a guest_memfd folio to unshared, thereby disallowing access by the host and allowing the hypervisor to transition its view of the guest page as private, we need to be sure that the host doesn't have any references to the folio.

This patch introduces a new type for guest_memfd folios, which isn't activated in this series but is here as a placeholder and to facilitate the code in the next patch. This will be used in the future to register a callback that informs the guest_memfd subsystem when the last reference is dropped, therefore knowing that the host doesn't have any remaining references.

---
  include/linux/page-flags.h | 7 +++++++
  mm/debug.c                 | 1 +
  mm/swap.c                  | 5 +++++
  3 files changed, 13 insertions(+)
include/linux/page-flags.h +1 -0
index 6615f2f59144..bab3cac1f93b 100644
@@ -942,6 +942,7 @@ enum pagetype {
  	PGTY_slab	= 0xf5,
  	PGTY_zsmalloc	= 0xf6,
  	PGTY_unaccepted	= 0xf7,
+	PGTY_guestmem	= 0xf8,
 
  	PGTY_mapcount_underflow = 0xff
  };

include/linux/page-flags.h FOLIO_TYPE_OPS(hugetlb, hugetlb) · line 1094

Quoted 3 lines
include/linux/page-flags.h +0 -0
@@ -1091,6 +1092,12 @@ FOLIO_TYPE_OPS(hugetlb, hugetlb)
  FOLIO_TEST_FLAG_FALSE(hugetlb)
  #endif
 

Some short doc would be nice, to at least hint that this is related to guest_memfd, and that these are otherwise folios.

/*
* guestmem folios are folios that are used to back VM memory as managed
* guest_memfd. Once the last reference is put, instead of freeing these
* folios back to the page allocator, they are returned to guest_memfd. *
* For now, guestmem will only be set on these folios as long as they
* cannot be mapped to user space ("private state"), with the plan of
* always setting that type once typed folios can be mapped to user
* space cleanly.
*/

Quoted 33 lines
include/linux/page-flags.h +6 -0
+#ifdef CONFIG_KVM_GMEM_MAPPABLE
+FOLIO_TYPE_OPS(guestmem, guestmem)
+#else
+FOLIO_TEST_FLAG_FALSE(guestmem)
+#endif
+
  PAGE_TYPE_OPS(Zsmalloc, zsmalloc, zsmalloc)
 
  /*
mm/debug.c +1 -0
index 95b6ab809c0e..db93be385ed9 100644
@@ -56,6 +56,7 @@ static const char *page_type_names[] = {
  	DEF_PAGETYPE_NAME(table),
  	DEF_PAGETYPE_NAME(buddy),
  	DEF_PAGETYPE_NAME(unaccepted),
+	DEF_PAGETYPE_NAME(guestmem),
  };>
  static const char *page_type_name(unsigned int page_type)
mm/swap.c +1 -0
index 8a66cd9cb9da..73d61c7f8edd 100644
@@ -37,6 +37,7 @@
  #include <linux/page_idle.h>
  #include <linux/local_lock.h>
  #include <linux/buffer_head.h>
+#include <linux/kvm_host.h>
 
  #include "internal.h"
 

mm/swap.c static void free_typed_folio(struct folio *folio) · line 107

Quoted 6 lines
mm/swap.c +3 -0
@@ -101,6 +102,10 @@ static void free_typed_folio(struct folio *folio)
  		if (IS_ENABLED(CONFIG_HUGETLBFS))
  			free_huge_folio(folio);
  		return;
+	case PGTY_guestmem:
+		if (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM))
+			WARN_ONCE(1, "A placeholder that shouldn't trigger.");

Does it make sense to directly introduce the callback into guest_memfd and handle the WARN_ONCE() in there? Then, we don't have tot ouch this core code later again.

Signature · 3 lines
Cheers,

David / dhildenb
author
#6 lore raw

Re: [RFC PATCH v2 02/11] KVM: guest_memfd: Handle final folio_put() of guest_memfd pages

Hi David,

Quoted 42 lines · David Hildenbrand
Quoted 32 lines · Fuad Tabba

Before transitioning a guest_memfd folio to unshared, thereby disallowing access by the host and allowing the hypervisor to transition its view of the guest page as private, we need to be sure that the host doesn't have any references to the folio.

This patch introduces a new type for guest_memfd folios, which isn't activated in this series but is here as a placeholder and to facilitate the code in the next patch. This will be used in the future to register a callback that informs the guest_memfd subsystem when the last reference is dropped, therefore knowing that the host doesn't have any remaining references.

---
  include/linux/page-flags.h | 7 +++++++
  mm/debug.c                 | 1 +
  mm/swap.c                  | 5 +++++
  3 files changed, 13 insertions(+)
include/linux/page-flags.h +1 -0
index 6615f2f59144..bab3cac1f93b 100644
@@ -942,6 +942,7 @@ enum pagetype {
      PGTY_slab       = 0xf5,
      PGTY_zsmalloc   = 0xf6,
      PGTY_unaccepted = 0xf7,
+     PGTY_guestmem   = 0xf8,
 
      PGTY_mapcount_underflow = 0xff
  };

include/linux/page-flags.h FOLIO_TYPE_OPS(hugetlb, hugetlb) · line 1094

Quoted 3 lines
include/linux/page-flags.h +0 -0
@@ -1091,6 +1092,12 @@ FOLIO_TYPE_OPS(hugetlb, hugetlb)
  FOLIO_TEST_FLAG_FALSE(hugetlb)
  #endif
 

Some short doc would be nice, to at least hint that this is related to guest_memfd, and that these are otherwise folios.

Quoted 10 lines

/*
* guestmem folios are folios that are used to back VM memory as managed
* guest_memfd. Once the last reference is put, instead of freeing these
* folios back to the page allocator, they are returned to guest_memfd. *
* For now, guestmem will only be set on these folios as long as they
* cannot be mapped to user space ("private state"), with the plan of
* always setting that type once typed folios can be mapped to user
* space cleanly.
*/

Will add this.

Quoted 32 lines
Quoted 32 lines
include/linux/page-flags.h +6 -0
+#ifdef CONFIG_KVM_GMEM_MAPPABLE
+FOLIO_TYPE_OPS(guestmem, guestmem)
+#else
+FOLIO_TEST_FLAG_FALSE(guestmem)
+#endif
+
  PAGE_TYPE_OPS(Zsmalloc, zsmalloc, zsmalloc)
 
  /*
mm/debug.c +1 -0
index 95b6ab809c0e..db93be385ed9 100644
@@ -56,6 +56,7 @@ static const char *page_type_names[] = {
      DEF_PAGETYPE_NAME(table),
      DEF_PAGETYPE_NAME(buddy),
      DEF_PAGETYPE_NAME(unaccepted),
+     DEF_PAGETYPE_NAME(guestmem),
  };>
  static const char *page_type_name(unsigned int page_type)
mm/swap.c +1 -0
index 8a66cd9cb9da..73d61c7f8edd 100644
@@ -37,6 +37,7 @@
  #include <linux/page_idle.h>
  #include <linux/local_lock.h>
  #include <linux/buffer_head.h>
+#include <linux/kvm_host.h>
 
  #include "internal.h"
 
Quoted 12 lines

mm/swap.c static void free_typed_folio(struct folio *folio) · line 107

Quoted 7 lines
mm/swap.c +3 -0
@@ -101,6 +102,10 @@ static void free_typed_folio(struct folio *folio)
              if (IS_ENABLED(CONFIG_HUGETLBFS))
                      free_huge_folio(folio);
              return;
+     case PGTY_guestmem:
+             if (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM))
+                     WARN_ONCE(1, "A placeholder that shouldn't trigger.");

Does it make sense to directly introduce the callback into guest_memfd and handle the WARN_ONCE() in there? Then, we don't have tot ouch this core code later again.

Yes. That would simplify things.

Thanks,
/fuad

Quoted 5 lines

--
Cheers,

David / dhildenb

#7 lore raw

Re: [RFC PATCH v2 02/11] KVM: guest_memfd: Handle final folio_put() of guest_memfd pages

In reply to Fuad Tabba
kernel test robot noticed the following build errors:

Hi Fuad,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on ffd294d346d185b70e28b1a28abe367bbfe53c04]

url:    https://github.com/intel-lab-lkp/linux/commits/Fuad-Tabba/mm-Consolidate-freeing-of-typed-folios-on-final-folio_put/20250130-012603
base:   ffd294d346d185b70e28b1a28abe367bbfe53c04
patch link:    https://lore.kernel.org/r/20250129172320.950523-3-tabba%40google.com
patch subject: [RFC PATCH v2 02/11] KVM: guest_memfd: Handle final folio_put() of guest_memfd pages
config: um-allyesconfig (https://download.01.org/0day-ci/archive/20250131/[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/20250131/[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 >>):

In file included from include/vdso/const.h:5,

                    from include/linux/const.h:4,
                    from include/linux/bits.h:5,
                    from include/linux/ratelimit_types.h:5,
                    from include/linux/printk.h:9,
                    from include/asm-generic/bug.h:22,
                    from ./arch/um/include/generated/asm/bug.h:1,
                    from include/linux/bug.h:5,
                    from include/linux/mmdebug.h:5,
                    from include/linux/mm.h:6,
                    from mm/swap.c:17:
>> arch/x86/include/asm/kvm_host.h:569:47: error: 'X86_PMC_IDX_MAX' undeclared here (not in a function)
     569 |                 DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
         |                                               ^~~~~~~~~~~~~~~
   include/uapi/linux/const.h:51:40: note: in definition of macro '__KERNEL_DIV_ROUND_UP'
      51 | #define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
         |                                        ^
   include/linux/types.h:11:28: note: in expansion of macro 'BITS_TO_LONGS'
      11 |         unsigned long name[BITS_TO_LONGS(bits)]
         |                            ^~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:569:17: note: in expansion of macro 'DECLARE_BITMAP'
     569 |                 DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
         |                 ^~~~~~~~~~~~~~
   In file included from include/linux/kvm_host.h:45,
                    from mm/swap.c:40:
>> arch/x86/include/asm/kvm_host.h:828:26: error: field 'guest_fpu' has incomplete type
     828 |         struct fpu_guest guest_fpu;
         |                          ^~~~~~~~~
>> arch/x86/include/asm/kvm_host.h:150:33: error: 'PG_LEVEL_1G' undeclared here (not in a function)
     150 | #define KVM_MAX_HUGEPAGE_LEVEL  PG_LEVEL_1G
         |                                 ^~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:151:34: note: in expansion of macro 'KVM_MAX_HUGEPAGE_LEVEL'
     151 | #define KVM_NR_PAGE_SIZES       (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
         |                                  ^~~~~~~~~~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:1051:36: note: in expansion of macro 'KVM_NR_PAGE_SIZES'
    1051 |         struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES];
         |                                    ^~~~~~~~~~~~~~~~~
>> arch/x86/include/asm/kvm_host.h:151:59: error: 'PG_LEVEL_4K' undeclared here (not in a function)
     151 | #define KVM_NR_PAGE_SIZES       (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
         |                                                           ^~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:1051:36: note: in expansion of macro 'KVM_NR_PAGE_SIZES'
    1051 |         struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES];
         |                                    ^~~~~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h: In function 'kvm_lapic_irq_dest_mode':
>> arch/x86/include/asm/kvm_host.h:1623:36: error: 'APIC_DEST_LOGICAL' undeclared (first use in this function)
    1623 |         return dest_mode_logical ? APIC_DEST_LOGICAL : APIC_DEST_PHYSICAL;
         |                                    ^~~~~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h:1623:36: note: each undeclared identifier is reported only once for each function it appears in
>> arch/x86/include/asm/kvm_host.h:1623:56: error: 'APIC_DEST_PHYSICAL' undeclared (first use in this function)
    1623 |         return dest_mode_logical ? APIC_DEST_LOGICAL : APIC_DEST_PHYSICAL;
         |                                                        ^~~~~~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h: At top level:
   arch/x86/include/asm/kvm_host.h:1670:55: warning: 'struct desc_ptr' declared inside parameter list will not be visible outside of this definition or declaration
    1670 |         void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^~~~~~~~
   arch/x86/include/asm/kvm_host.h:1671:55: warning: 'struct desc_ptr' declared inside parameter list will not be visible outside of this definition or declaration
    1671 |         void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^~~~~~~~
   arch/x86/include/asm/kvm_host.h:1672:55: warning: 'struct desc_ptr' declared inside parameter list will not be visible outside of this definition or declaration
    1672 |         void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^~~~~~~~
   arch/x86/include/asm/kvm_host.h:1673:55: warning: 'struct desc_ptr' declared inside parameter list will not be visible outside of this definition or declaration
    1673 |         void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
         |                                                       ^~~~~~~~
   arch/x86/include/asm/kvm_host.h: In function 'read_msr':
>> arch/x86/include/asm/kvm_host.h:2228:9: error: implicit declaration of function 'rdmsrl' [-Werror=implicit-function-declaration]
    2228 |         rdmsrl(msr, value);
         |         ^~~~~~
   arch/x86/include/asm/kvm_host.h: In function 'kvm_irq_is_postable':
>> arch/x86/include/asm/kvm_host.h:2327:39: error: 'APIC_DM_FIXED' undeclared (first use in this function)
    2327 |         return (irq->delivery_mode == APIC_DM_FIXED ||
         |                                       ^~~~~~~~~~~~~
>> arch/x86/include/asm/kvm_host.h:2328:39: error: 'APIC_DM_LOWEST' undeclared (first use in this function)
    2328 |                 irq->delivery_mode == APIC_DM_LOWEST);
         |                                       ^~~~~~~~~~~~~~
   arch/x86/include/asm/kvm_host.h: In function 'kvm_cpu_get_apicid':
>> arch/x86/include/asm/kvm_host.h:2347:16: error: 'BAD_APICID' undeclared (first use in this function)
    2347 |         return BAD_APICID;
         |                ^~~~~~~~~~

cc1: some warnings being treated as errors

vim +/X86_PMC_IDX_MAX +569 arch/x86/include/asm/kvm_host.h

f5132b01386b5a Gleb Natapov        2011-11-10  533  
4f1fa2a1bbeb2f Like Xu             2022-09-19  534  /* More counters may conflict with other existing Architectural MSRs */
f287bef6ddc208 Dapeng Mi           2024-06-27  535  #define KVM_MAX(a, b)	((a) >= (b) ? (a) : (b))
f287bef6ddc208 Dapeng Mi           2024-06-27  536  #define KVM_MAX_NR_INTEL_GP_COUNTERS	8
f287bef6ddc208 Dapeng Mi           2024-06-27  537  #define KVM_MAX_NR_AMD_GP_COUNTERS	6
f287bef6ddc208 Dapeng Mi           2024-06-27  538  #define KVM_MAX_NR_GP_COUNTERS		KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \
f287bef6ddc208 Dapeng Mi           2024-06-27  539  						KVM_MAX_NR_AMD_GP_COUNTERS)
f287bef6ddc208 Dapeng Mi           2024-06-27  540  
f287bef6ddc208 Dapeng Mi           2024-06-27  541  #define KVM_MAX_NR_INTEL_FIXED_COUTNERS	3
f287bef6ddc208 Dapeng Mi           2024-06-27  542  #define KVM_MAX_NR_AMD_FIXED_COUTNERS	0
f287bef6ddc208 Dapeng Mi           2024-06-27  543  #define KVM_MAX_NR_FIXED_COUNTERS	KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUTNERS, \
f287bef6ddc208 Dapeng Mi           2024-06-27  544  						KVM_MAX_NR_AMD_FIXED_COUTNERS)
004a0aa56edea3 Sean Christopherson 2023-11-09  545  
f5132b01386b5a Gleb Natapov        2011-11-10  546  struct kvm_pmu {
12aad9164763bb Mathias Krause      2023-02-17  547  	u8 version;
f5132b01386b5a Gleb Natapov        2011-11-10  548  	unsigned nr_arch_gp_counters;
f5132b01386b5a Gleb Natapov        2011-11-10  549  	unsigned nr_arch_fixed_counters;
f5132b01386b5a Gleb Natapov        2011-11-10  550  	unsigned available_event_types;
f5132b01386b5a Gleb Natapov        2011-11-10  551  	u64 fixed_ctr_ctrl;
0e102ce3d41331 Dapeng Mi           2024-04-30  552  	u64 fixed_ctr_ctrl_rsvd;
f5132b01386b5a Gleb Natapov        2011-11-10  553  	u64 global_ctrl;
f5132b01386b5a Gleb Natapov        2011-11-10  554  	u64 global_status;
f5132b01386b5a Gleb Natapov        2011-11-10  555  	u64 counter_bitmask[2];
0e102ce3d41331 Dapeng Mi           2024-04-30  556  	u64 global_ctrl_rsvd;
0e102ce3d41331 Dapeng Mi           2024-04-30  557  	u64 global_status_rsvd;
103af0a9878859 Andi Kleen          2013-07-18  558  	u64 reserved_bits;
95b065bf5c431c Jim Mattson         2022-03-07  559  	u64 raw_event_mask;
f287bef6ddc208 Dapeng Mi           2024-06-27  560  	struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
f287bef6ddc208 Dapeng Mi           2024-06-27  561  	struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
f1c5651fda43e0 Sean Christopherson 2022-09-23  562  
f1c5651fda43e0 Sean Christopherson 2022-09-23  563  	/*
f1c5651fda43e0 Sean Christopherson 2022-09-23  564  	 * Overlay the bitmap with a 64-bit atomic so that all bits can be
f1c5651fda43e0 Sean Christopherson 2022-09-23  565  	 * set in a single access, e.g. to reprogram all counters when the PMU
f1c5651fda43e0 Sean Christopherson 2022-09-23  566  	 * filter changes.
f1c5651fda43e0 Sean Christopherson 2022-09-23  567  	 */
f1c5651fda43e0 Sean Christopherson 2022-09-23  568  	union {
4be946728f65c1 Like Xu             2019-10-21 @569  		DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
f1c5651fda43e0 Sean Christopherson 2022-09-23  570  		atomic64_t __reprogram_pmi;
f1c5651fda43e0 Sean Christopherson 2022-09-23  571  	};
b35e5548b41131 Like Xu             2019-10-27  572  	DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);
b35e5548b41131 Like Xu             2019-10-27  573  	DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);
b35e5548b41131 Like Xu             2019-10-27  574  
8183a538cd95f7 Like Xu             2022-04-11  575  	u64 ds_area;
c59a1f106f5cd4 Like Xu             2022-04-11  576  	u64 pebs_enable;
0e102ce3d41331 Dapeng Mi           2024-04-30  577  	u64 pebs_enable_rsvd;
902caeb6841a64 Like Xu             2022-04-11  578  	u64 pebs_data_cfg;
0e102ce3d41331 Dapeng Mi           2024-04-30  579  	u64 pebs_data_cfg_rsvd;
c59a1f106f5cd4 Like Xu             2022-04-11  580  
854250329c02c0 Like Xu             2022-04-11  581  	/*
854250329c02c0 Like Xu             2022-04-11  582  	 * If a guest counter is cross-mapped to host counter with different
854250329c02c0 Like Xu             2022-04-11  583  	 * index, its PEBS capability will be temporarily disabled.
854250329c02c0 Like Xu             2022-04-11  584  	 *
854250329c02c0 Like Xu             2022-04-11  585  	 * The user should make sure that this mask is updated
854250329c02c0 Like Xu             2022-04-11  586  	 * after disabling interrupts and before perf_guest_get_msrs();
854250329c02c0 Like Xu             2022-04-11  587  	 */
854250329c02c0 Like Xu             2022-04-11  588  	u64 host_cross_mapped_mask;
854250329c02c0 Like Xu             2022-04-11  589  
b35e5548b41131 Like Xu             2019-10-27  590  	/*
b35e5548b41131 Like Xu             2019-10-27  591  	 * The gate to release perf_events not marked in
b35e5548b41131 Like Xu             2019-10-27  592  	 * pmc_in_use only once in a vcpu time slice.
b35e5548b41131 Like Xu             2019-10-27  593  	 */
b35e5548b41131 Like Xu             2019-10-27  594  	bool need_cleanup;
b35e5548b41131 Like Xu             2019-10-27  595  
b35e5548b41131 Like Xu             2019-10-27  596  	/*
b35e5548b41131 Like Xu             2019-10-27  597  	 * The total number of programmed perf_events and it helps to avoid
b35e5548b41131 Like Xu             2019-10-27  598  	 * redundant check before cleanup if guest don't use vPMU at all.
b35e5548b41131 Like Xu             2019-10-27  599  	 */
b35e5548b41131 Like Xu             2019-10-27  600  	u8 event_count;
f5132b01386b5a Gleb Natapov        2011-11-10  601  };
f5132b01386b5a Gleb Natapov        2011-11-10  602  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#8 lore raw

RFC PATCH v2 3/11 KVM: guest_memfd: Allow host to map guest_memfd() pages

In reply to Fuad Tabba

Add support for mmap() and fault() for guest_memfd backed memory in the host for VMs that support in-place conversion between shared and private (shared memory). To that end, this patch adds the ability to check whether the VM type has that support, and only allows mapping its memory if that's the case.

Additionally, this behavior is gated with a new configuration option, CONFIG_KVM_GMEM_SHARED_MEM.

---

This patch series will allow shared memory support for software VMs in x86. It will also introduce a similar VM type for arm64 and allow shared memory support for that. In the future, pKVM will also support shared memory.

---
 include/linux/kvm_host.h | 11 ++++++
 virt/kvm/Kconfig         |  4 +++
 virt/kvm/guest_memfd.c   | 77 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+)
include/linux/kvm_host.h +11 -0
index 401439bb21e3..408429f13bf4 100644
@@ -717,6 +717,17 @@ static inline bool kvm_arch_has_private_mem(struct kvm *kvm)
 }
 #endif
 
+/*
+ * Arch code must define kvm_arch_gmem_supports_shared_mem if support for
+ * private memory is enabled and it supports in-place shared/private conversion.
+ */
+#if !defined(kvm_arch_gmem_supports_shared_mem) && !IS_ENABLED(CONFIG_KVM_PRIVATE_MEM)
+static inline bool kvm_arch_gmem_supports_shared_mem(struct kvm *kvm)
+{
+	return false;
+}
+#endif
+
 #ifndef kvm_arch_has_readonly_mem
 static inline bool kvm_arch_has_readonly_mem(struct kvm *kvm)
 {
virt/kvm/Kconfig +4 -0
index 54e959e7d68f..4e759e8020c5 100644
@@ -124,3 +124,7 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
 config HAVE_KVM_ARCH_GMEM_INVALIDATE
        bool
        depends on KVM_PRIVATE_MEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_PRIVATE_MEM
+       bool
virt/kvm/guest_memfd.c +77 -0

84 lines not on this page · raw message

Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
#9 lore raw

Re: [RFC PATCH v2 03/11] KVM: guest_memfd: Allow host to map guest_memfd() pages

Quoted 75 lines · Fuad Tabba

Add support for mmap() and fault() for guest_memfd backed memory in the host for VMs that support in-place conversion between shared and private (shared memory). To that end, this patch adds the ability to check whether the VM type has that support, and only allows mapping its memory if that's the case.

Additionally, this behavior is gated with a new configuration option, CONFIG_KVM_GMEM_SHARED_MEM.

---

This patch series will allow shared memory support for software VMs in x86. It will also introduce a similar VM type for arm64 and allow shared memory support for that. In the future, pKVM will also support shared memory.

---
  include/linux/kvm_host.h | 11 ++++++
  virt/kvm/Kconfig         |  4 +++
  virt/kvm/guest_memfd.c   | 77 ++++++++++++++++++++++++++++++++++++++++
  3 files changed, 92 insertions(+)
include/linux/kvm_host.h +11 -0
index 401439bb21e3..408429f13bf4 100644
@@ -717,6 +717,17 @@ static inline bool kvm_arch_has_private_mem(struct kvm *kvm)
  }
  #endif
 
+/*
+ * Arch code must define kvm_arch_gmem_supports_shared_mem if support for
+ * private memory is enabled and it supports in-place shared/private conversion.
+ */
+#if !defined(kvm_arch_gmem_supports_shared_mem) && !IS_ENABLED(CONFIG_KVM_PRIVATE_MEM)
+static inline bool kvm_arch_gmem_supports_shared_mem(struct kvm *kvm)
+{
+	return false;
+}
+#endif
+
  #ifndef kvm_arch_has_readonly_mem
  static inline bool kvm_arch_has_readonly_mem(struct kvm *kvm)
  {
virt/kvm/Kconfig +4 -0
index 54e959e7d68f..4e759e8020c5 100644
@@ -124,3 +124,7 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
  config HAVE_KVM_ARCH_GMEM_INVALIDATE
         bool
         depends on KVM_PRIVATE_MEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_PRIVATE_MEM
+       bool
virt/kvm/guest_memfd.c +10 -0
index 47a9f68f7b24..86441581c9ae 100644
@@ -307,7 +307,84 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)
  	return gfn - slot->base_gfn + slot->gmem.pgoff;
  }
 
+#ifdef CONFIG_KVM_GMEM_SHARED_MEM
+static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf)
+{
+	struct inode *inode = file_inode(vmf->vma->vm_file);
+	struct folio *folio;
+	vm_fault_t ret = VM_FAULT_LOCKED;
+
+	filemap_invalidate_lock_shared(inode->i_mapping);
+
+	folio = kvm_gmem_get_folio(inode, vmf->pgoff);

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 329

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+	if (IS_ERR(folio)) {
+		ret = VM_FAULT_SIGBUS;
+		goto out_filemap;
+	}
+
+	if (folio_test_hwpoison(folio)) {
+		ret = VM_FAULT_HWPOISON;
+		goto out_folio;
+	}
+

Worth adding a comment, something like

/*
* Only private folios are marked as "guestmem" so far, and we never
* expect private folios at this point.
*/

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 340

Quoted 11 lines
virt/kvm/guest_memfd.c +11 -0
+	if (WARN_ON_ONCE(folio_test_guestmem(folio)))  {
+		ret = VM_FAULT_SIGBUS;
+		goto out_folio;
+	}
+
+	/* No support for huge pages. */
+	if (WARN_ON_ONCE(folio_nr_pages(folio) > 1)) {
+		ret = VM_FAULT_SIGBUS;
+		goto out_folio;
+	}
+

/* We only support mmap of small folios. */
VM_WARN_ON_ONCE(folio_test_large(folio));

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 347

Quoted 7 lines
virt/kvm/guest_memfd.c +7 -0
+	if (!folio_test_uptodate(folio)) {
+		clear_highpage(folio_page(folio, 0));
+		folio_mark_uptodate(folio);
+	}
+
+	vmf->page = folio_file_page(folio, vmf->pgoff);
+

Apart from that LGTM.

Signature · 3 lines
Cheers,

David / dhildenb
author
#10 lore raw

Re: [RFC PATCH v2 03/11] KVM: guest_memfd: Allow host to map guest_memfd() pages

Hi David,

Quoted 94 lines · David Hildenbrand
Quoted 75 lines · Fuad Tabba

Add support for mmap() and fault() for guest_memfd backed memory in the host for VMs that support in-place conversion between shared and private (shared memory). To that end, this patch adds the ability to check whether the VM type has that support, and only allows mapping its memory if that's the case.

Additionally, this behavior is gated with a new configuration option, CONFIG_KVM_GMEM_SHARED_MEM.

---

This patch series will allow shared memory support for software VMs in x86. It will also introduce a similar VM type for arm64 and allow shared memory support for that. In the future, pKVM will also support shared memory.

---
  include/linux/kvm_host.h | 11 ++++++
  virt/kvm/Kconfig         |  4 +++
  virt/kvm/guest_memfd.c   | 77 ++++++++++++++++++++++++++++++++++++++++
  3 files changed, 92 insertions(+)
include/linux/kvm_host.h +11 -0
index 401439bb21e3..408429f13bf4 100644
@@ -717,6 +717,17 @@ static inline bool kvm_arch_has_private_mem(struct kvm *kvm)
  }
  #endif
 
+/*
+ * Arch code must define kvm_arch_gmem_supports_shared_mem if support for
+ * private memory is enabled and it supports in-place shared/private conversion.
+ */
+#if !defined(kvm_arch_gmem_supports_shared_mem) && !IS_ENABLED(CONFIG_KVM_PRIVATE_MEM)
+static inline bool kvm_arch_gmem_supports_shared_mem(struct kvm *kvm)
+{
+     return false;
+}
+#endif
+
  #ifndef kvm_arch_has_readonly_mem
  static inline bool kvm_arch_has_readonly_mem(struct kvm *kvm)
  {
virt/kvm/Kconfig +4 -0
index 54e959e7d68f..4e759e8020c5 100644
@@ -124,3 +124,7 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
  config HAVE_KVM_ARCH_GMEM_INVALIDATE
         bool
         depends on KVM_PRIVATE_MEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_PRIVATE_MEM
+       bool
virt/kvm/guest_memfd.c +10 -0
index 47a9f68f7b24..86441581c9ae 100644
@@ -307,7 +307,84 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)
      return gfn - slot->base_gfn + slot->gmem.pgoff;
  }
 
+#ifdef CONFIG_KVM_GMEM_SHARED_MEM
+static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf)
+{
+     struct inode *inode = file_inode(vmf->vma->vm_file);
+     struct folio *folio;
+     vm_fault_t ret = VM_FAULT_LOCKED;
+
+     filemap_invalidate_lock_shared(inode->i_mapping);
+
+     folio = kvm_gmem_get_folio(inode, vmf->pgoff);

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 329

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+     if (IS_ERR(folio)) {
+             ret = VM_FAULT_SIGBUS;
+             goto out_filemap;
+     }
+
+     if (folio_test_hwpoison(folio)) {
+             ret = VM_FAULT_HWPOISON;
+             goto out_folio;
+     }
+

Worth adding a comment, something like

/*
* Only private folios are marked as "guestmem" so far, and we never
* expect private folios at this point.
*/

Quoted 14 lines

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 340

Quoted 11 lines
virt/kvm/guest_memfd.c +11 -0
+     if (WARN_ON_ONCE(folio_test_guestmem(folio)))  {
+             ret = VM_FAULT_SIGBUS;
+             goto out_folio;
+     }
+
+     /* No support for huge pages. */
+     if (WARN_ON_ONCE(folio_nr_pages(folio) > 1)) {
+             ret = VM_FAULT_SIGBUS;
+             goto out_folio;
+     }
+

/* We only support mmap of small folios. */
VM_WARN_ON_ONCE(folio_test_large(folio));

Will do. Thanks.

/fuad

Quoted 16 lines

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 347

Quoted 8 lines
virt/kvm/guest_memfd.c +7 -0
+     if (!folio_test_uptodate(folio)) {
+             clear_highpage(folio_page(folio, 0));
+             folio_mark_uptodate(folio);
+     }
+
+     vmf->page = folio_file_page(folio, vmf->pgoff);
+

Apart from that LGTM.

--
Cheers,

David / dhildenb

#11 lore raw

Re: [RFC PATCH v2 03/11] KVM: guest_memfd: Allow host to map guest_memfd() pages

In reply to Fuad Tabba

Hi Fuad!

Quoted 57 lines · Fuad Tabba

Add support for mmap() and fault() for guest_memfd backed memory in the host for VMs that support in-place conversion between shared and private (shared memory). To that end, this patch adds the ability to check whether the VM type has that support, and only allows mapping its memory if that's the case.

Additionally, this behavior is gated with a new configuration option, CONFIG_KVM_GMEM_SHARED_MEM.

---

This patch series will allow shared memory support for software VMs in x86. It will also introduce a similar VM type for arm64 and allow shared memory support for that. In the future, pKVM will also support shared memory.

---
 include/linux/kvm_host.h | 11 ++++++
 virt/kvm/Kconfig         |  4 +++
 virt/kvm/guest_memfd.c   | 77 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+)
(quoted diff) +0 -1
-snip-
virt/kvm/guest_memfd.c +24 -0
index 47a9f68f7b24..86441581c9ae 100644
@@ -307,7 +307,84 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)
        return gfn - slot->base_gfn + slot->gmem.pgoff;
 }
 
+#ifdef CONFIG_KVM_GMEM_SHARED_MEM
+static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf)
+{
+       struct inode *inode = file_inode(vmf->vma->vm_file);
+       struct folio *folio;
+       vm_fault_t ret = VM_FAULT_LOCKED;
+
+       filemap_invalidate_lock_shared(inode->i_mapping);
+
+       folio = kvm_gmem_get_folio(inode, vmf->pgoff);
+       if (IS_ERR(folio)) {
+               ret = VM_FAULT_SIGBUS;
+               goto out_filemap;
+       }
+
+       if (folio_test_hwpoison(folio)) {
+               ret = VM_FAULT_HWPOISON;
+               goto out_folio;
+       }
+
+       if (WARN_ON_ONCE(folio_test_guestmem(folio)))  {
+               ret = VM_FAULT_SIGBUS;
+               goto out_folio;
+       }

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 343

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+
+       /* No support for huge pages. */
+       if (WARN_ON_ONCE(folio_nr_pages(folio) > 1)) {
+               ret = VM_FAULT_SIGBUS;
+               goto out_folio;
+       }
+
+       if (!folio_test_uptodate(folio)) {
+               clear_highpage(folio_page(folio, 0));
+               folio_mark_uptodate(folio);

kvm_gmem_mark_prepared() instead of direct folio_mark_uptodate() here, I think (in preparation of things like [1])? Noticed this while rebasing my direct map removal series on top of this and wondering why mmap'd folios sometimes didn't get removed (since it hooks mark_prepared()).

Best,
Patrick

[1]: https://lore.kernel.org/kvm/[email protected]/

author
#12 lore raw

Re: [RFC PATCH v2 03/11] KVM: guest_memfd: Allow host to map guest_memfd() pages

Quoted 68 lines · Patrick Roy

Hi Fuad!

Quoted 64 lines · Fuad Tabba

Add support for mmap() and fault() for guest_memfd backed memory in the host for VMs that support in-place conversion between shared and private (shared memory). To that end, this patch adds the ability to check whether the VM type has that support, and only allows mapping its memory if that's the case.

Additionally, this behavior is gated with a new configuration option, CONFIG_KVM_GMEM_SHARED_MEM.

---

This patch series will allow shared memory support for software VMs in x86. It will also introduce a similar VM type for arm64 and allow shared memory support for that. In the future, pKVM will also support shared memory.

---
 include/linux/kvm_host.h | 11 ++++++
 virt/kvm/Kconfig         |  4 +++
 virt/kvm/guest_memfd.c   | 77 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+)
(quoted diff) +0 -1
-snip-
virt/kvm/guest_memfd.c +31 -0
index 47a9f68f7b24..86441581c9ae 100644
@@ -307,7 +307,84 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)
        return gfn - slot->base_gfn + slot->gmem.pgoff;
 }
 
+#ifdef CONFIG_KVM_GMEM_SHARED_MEM
+static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf)
+{
+       struct inode *inode = file_inode(vmf->vma->vm_file);
+       struct folio *folio;
+       vm_fault_t ret = VM_FAULT_LOCKED;
+
+       filemap_invalidate_lock_shared(inode->i_mapping);
+
+       folio = kvm_gmem_get_folio(inode, vmf->pgoff);
+       if (IS_ERR(folio)) {
+               ret = VM_FAULT_SIGBUS;
+               goto out_filemap;
+       }
+
+       if (folio_test_hwpoison(folio)) {
+               ret = VM_FAULT_HWPOISON;
+               goto out_folio;
+       }
+
+       if (WARN_ON_ONCE(folio_test_guestmem(folio)))  {
+               ret = VM_FAULT_SIGBUS;
+               goto out_folio;
+       }
+
+       /* No support for huge pages. */
+       if (WARN_ON_ONCE(folio_nr_pages(folio) > 1)) {
+               ret = VM_FAULT_SIGBUS;
+               goto out_folio;
+       }
+
Quoted 8 lines

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 343

Quoted 3 lines
virt/kvm/guest_memfd.c +3 -0
+       if (!folio_test_uptodate(folio)) {
+               clear_highpage(folio_page(folio, 0));
+               folio_mark_uptodate(folio);

kvm_gmem_mark_prepared() instead of direct folio_mark_uptodate() here, I think (in preparation of things like [1])? Noticed this while rebasing my direct map removal series on top of this and wondering why mmap'd folios sometimes didn't get removed (since it hooks mark_prepared()).

Thanks for pointing that out. Will fix.
/fuad

Quoted 4 lines

Best,
Patrick

[1]: https://lore.kernel.org/kvm/[email protected]/

author
#13 lore raw

RFC PATCH v2 4/11 KVM: guest_memfd: Add KVM capability to check if guest_memfd is shared

In reply to Fuad Tabba

Add the KVM capability KVM_CAP_GMEM_SHARED_MEM, which indicates that the VM supports shared memory in guest_memfd, or that the host can create VMs that support shared memory. Supporting shared memory implies that memory can be mapped when shared with the host.

For now, this checks only whether the VM type supports sharing guest_memfd backed memory. In the future, it will be expanded to check whether the specific memory address is shared with the host.

---
 include/uapi/linux/kvm.h |  1 +
 virt/kvm/guest_memfd.c   | 13 +++++++++++++
 virt/kvm/kvm_main.c      |  4 ++++
 3 files changed, 18 insertions(+)
include/uapi/linux/kvm.h +1 -0
index 502ea63b5d2e..3ac805c5abf1 100644
@@ -933,6 +933,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_PRE_FAULT_MEMORY 236
 #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
 #define KVM_CAP_X86_GUEST_MODE 238
+#define KVM_CAP_GMEM_SHARED_MEM 239
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
virt/kvm/guest_memfd.c +13 -0
index 86441581c9ae..4e1144ed3446 100644
@@ -308,6 +308,13 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)
 }
 
 #ifdef CONFIG_KVM_GMEM_SHARED_MEM
+static bool kvm_gmem_is_shared(struct file *file, pgoff_t pgoff)
+{
+	struct kvm_gmem *gmem = file->private_data;
+
+	return kvm_arch_gmem_supports_shared_mem(gmem->kvm);
+}
+
 static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf)
 {
 	struct inode *inode = file_inode(vmf->vma->vm_file);
@@ -327,6 +334,12 @@ static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf)
 		goto out_folio;
 	}
 
+	/* Must be called with folio lock held, i.e., after kvm_gmem_get_folio() */
+	if (!kvm_gmem_is_shared(vmf->vma->vm_file, vmf->pgoff)) {
+		ret = VM_FAULT_SIGBUS;
+		goto out_folio;
+	}
+
 	if (WARN_ON_ONCE(folio_test_guestmem(folio)))  {
 		ret = VM_FAULT_SIGBUS;
 		goto out_folio;
virt/kvm/kvm_main.c +4 -0
index de2c11dae231..40e4ed512923 100644
@@ -4792,6 +4792,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 #ifdef CONFIG_KVM_PRIVATE_MEM
 	case KVM_CAP_GUEST_MEMFD:
 		return !kvm || kvm_arch_has_private_mem(kvm);
+#endif
+#ifdef CONFIG_KVM_GMEM_SHARED_MEM
+	case KVM_CAP_GMEM_SHARED_MEM:
+		return !kvm || kvm_arch_gmem_supports_shared_mem(kvm);
 #endif
 	default:
 		break;
Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
#14 lore raw

Re: [RFC PATCH v2 04/11] KVM: guest_memfd: Add KVM capability to check if guest_memfd is shared

Quoted 35 lines · Fuad Tabba

Add the KVM capability KVM_CAP_GMEM_SHARED_MEM, which indicates that the VM supports shared memory in guest_memfd, or that the host can create VMs that support shared memory. Supporting shared memory implies that memory can be mapped when shared with the host.

For now, this checks only whether the VM type supports sharing guest_memfd backed memory. In the future, it will be expanded to check whether the specific memory address is shared with the host.

---
  include/uapi/linux/kvm.h |  1 +
  virt/kvm/guest_memfd.c   | 13 +++++++++++++
  virt/kvm/kvm_main.c      |  4 ++++
  3 files changed, 18 insertions(+)
include/uapi/linux/kvm.h +1 -0
index 502ea63b5d2e..3ac805c5abf1 100644
@@ -933,6 +933,7 @@ struct kvm_enable_cap {
  #define KVM_CAP_PRE_FAULT_MEMORY 236
  #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
  #define KVM_CAP_X86_GUEST_MODE 238
+#define KVM_CAP_GMEM_SHARED_MEM 239
 
  struct kvm_irq_routing_irqchip {
  	__u32 irqchip;
virt/kvm/guest_memfd.c +0 -0
index 86441581c9ae..4e1144ed3446 100644

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 311

Quoted 4 lines
virt/kvm/guest_memfd.c +1 -0
@@ -308,6 +308,13 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)
  }
 
  #ifdef CONFIG_KVM_GMEM_SHARED_MEM
+static bool kvm_gmem_is_shared(struct file *file, pgoff_t pgoff)

I assume you want to call this something like:

kvm_gmem_folio_is_shared
kvm_gmem_offset_is_shared
kvm_gmem_range_is_shared
...

To make it clearer that you are only checking one piece and not the whole thing.

But then, I wonder if that could be handled in kvm_gmem_get_folio(), and e.g., specified via a flag?

kvm_gmem_get_folio(inode, vmf->pgoff, KVM_GMEM_GF_SHARED);

Maybe existing callers would want to pass KVM_GMEM_GF_PRIVATE, and the ones that "don't care" don't pas anything?

Signature · 3 lines
Cheers,

David / dhildenb
author
#15 lore raw

Re: [RFC PATCH v2 04/11] KVM: guest_memfd: Add KVM capability to check if guest_memfd is shared

Hi David,

Quoted 52 lines · David Hildenbrand
Quoted 35 lines · Fuad Tabba

Add the KVM capability KVM_CAP_GMEM_SHARED_MEM, which indicates that the VM supports shared memory in guest_memfd, or that the host can create VMs that support shared memory. Supporting shared memory implies that memory can be mapped when shared with the host.

For now, this checks only whether the VM type supports sharing guest_memfd backed memory. In the future, it will be expanded to check whether the specific memory address is shared with the host.

---
  include/uapi/linux/kvm.h |  1 +
  virt/kvm/guest_memfd.c   | 13 +++++++++++++
  virt/kvm/kvm_main.c      |  4 ++++
  3 files changed, 18 insertions(+)
include/uapi/linux/kvm.h +1 -0
index 502ea63b5d2e..3ac805c5abf1 100644
@@ -933,6 +933,7 @@ struct kvm_enable_cap {
  #define KVM_CAP_PRE_FAULT_MEMORY 236
  #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
  #define KVM_CAP_X86_GUEST_MODE 238
+#define KVM_CAP_GMEM_SHARED_MEM 239
 
  struct kvm_irq_routing_irqchip {
      __u32 irqchip;
virt/kvm/guest_memfd.c +0 -0
index 86441581c9ae..4e1144ed3446 100644

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 311

Quoted 4 lines
virt/kvm/guest_memfd.c +1 -0
@@ -308,6 +308,13 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)
  }
 
  #ifdef CONFIG_KVM_GMEM_SHARED_MEM
+static bool kvm_gmem_is_shared(struct file *file, pgoff_t pgoff)

I assume you want to call this something like:

kvm_gmem_folio_is_shared
kvm_gmem_offset_is_shared
kvm_gmem_range_is_shared
...

To make it clearer that you are only checking one piece and not the whole thing.

Quoted 7 lines

But then, I wonder if that could be handled in kvm_gmem_get_folio(), and e.g., specified via a flag?

kvm_gmem_get_folio(inode, vmf->pgoff, KVM_GMEM_GF_SHARED);

Maybe existing callers would want to pass KVM_GMEM_GF_PRIVATE, and the ones that "don't care" don't pas anything?

I agree that naming this function to make it clearer would be a good idea. That said, I think with the patches I removed, it doesn't even need to be exposed at all, even looking at future patches --- it has no callers other than kvm_gmem_fault(). Therefore, I don't think we need to add a flag to kvm_gmem_get_folio().

I'll have a closer look while preparing the respin and fix it either way.

Thanks,
/fuad

Quoted 5 lines

--
Cheers,

David / dhildenb

#16 lore raw

Re: [RFC PATCH v2 04/11] KVM: guest_memfd: Add KVM capability to check if guest_memfd is shared

Quoted 63 lines · Fuad Tabba

Hi David,

Quoted 52 lines · David Hildenbrand
Quoted 35 lines · Fuad Tabba

Add the KVM capability KVM_CAP_GMEM_SHARED_MEM, which indicates that the VM supports shared memory in guest_memfd, or that the host can create VMs that support shared memory. Supporting shared memory implies that memory can be mapped when shared with the host.

For now, this checks only whether the VM type supports sharing guest_memfd backed memory. In the future, it will be expanded to check whether the specific memory address is shared with the host.

---
   include/uapi/linux/kvm.h |  1 +
   virt/kvm/guest_memfd.c   | 13 +++++++++++++
   virt/kvm/kvm_main.c      |  4 ++++
   3 files changed, 18 insertions(+)
include/uapi/linux/kvm.h +1 -0
index 502ea63b5d2e..3ac805c5abf1 100644
@@ -933,6 +933,7 @@ struct kvm_enable_cap {
   #define KVM_CAP_PRE_FAULT_MEMORY 236
   #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
   #define KVM_CAP_X86_GUEST_MODE 238
+#define KVM_CAP_GMEM_SHARED_MEM 239
 
   struct kvm_irq_routing_irqchip {
       __u32 irqchip;
virt/kvm/guest_memfd.c +0 -0
index 86441581c9ae..4e1144ed3446 100644

virt/kvm/guest_memfd.c static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) · line 311

Quoted 4 lines
virt/kvm/guest_memfd.c +1 -0
@@ -308,6 +308,13 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)
   }
 
   #ifdef CONFIG_KVM_GMEM_SHARED_MEM
+static bool kvm_gmem_is_shared(struct file *file, pgoff_t pgoff)

I assume you want to call this something like:

kvm_gmem_folio_is_shared
kvm_gmem_offset_is_shared
kvm_gmem_range_is_shared
...

To make it clearer that you are only checking one piece and not the whole thing.

Quoted 7 lines

But then, I wonder if that could be handled in kvm_gmem_get_folio(), and e.g., specified via a flag?

kvm_gmem_get_folio(inode, vmf->pgoff, KVM_GMEM_GF_SHARED);

Maybe existing callers would want to pass KVM_GMEM_GF_PRIVATE, and the ones that "don't care" don't pas anything?

Quoted 7 lines

I agree that naming this function to make it clearer would be a good idea. That said, I think with the patches I removed, it doesn't even need to be exposed at all, even looking at future patches --- it has no callers other than kvm_gmem_fault(). Therefore, I don't think we need to add a flag to kvm_gmem_get_folio().

I'll have a closer look while preparing the respin and fix it either way.

Okay, having some indication in the name that we only want a shared folio etc. might make sense.

Signature · 3 lines
Cheers,

David / dhildenb
author
#17 lore raw

RFC PATCH v2 5/11 KVM: guest_memfd: Handle in-place shared memory as guest_memfd backed memory

In reply to Fuad Tabba

For VMs that allow sharing guest_memfd backed memory in-place, handle that memory the same as "private" guest_memfd memory. This means that faulting that memory in the host or in the guest will go through the guest_memfd subsystem.

Note that the word "private" in the name of the function kvm_mem_is_private() doesn't necessarily indicate that the memory isn't shared, but is due to the history and evolution of guest_memfd and the various names it has received. In effect, this function is used to multiplex between the path of a normal page fault and the path of a guest_memfd backed page fault.

---
 include/linux/kvm_host.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/kvm_host.h +2 -1
index 408429f13bf4..e57cdf4e3f3f 100644
@@ -2503,7 +2503,8 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
 #else
 static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
 {
-	return false;
+	return kvm_arch_gmem_supports_shared_mem(kvm) &&
+	       kvm_slot_can_be_private(gfn_to_memslot(kvm, gfn));
 }
 #endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */
 
Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
author
#18 lore raw

RFC PATCH v2 6/11 KVM: x86: Mark KVM_X86_SW_PROTECTED_VM as supporting guest_memfd shared memory

In reply to Fuad Tabba

The KVM_X86_SW_PROTECTED_VM type is meant for experimentation and does not have any underlying support for protected guests. This makes it a good candidate to use for testing this patch series. Therefore, when the kconfig option is enabled, mark KVM_X86_SW_PROTECTED_VM as supporting shared memory.

This means that this memory is considered by guest_memfd to be shared with the host, with in-place conversion. This allows the host to map and fault in guest_memfd memory belonging to this VM type.

---
 arch/x86/include/asm/kvm_host.h | 5 +++++
 arch/x86/kvm/Kconfig            | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +5 -0
index e159e44a6a1b..35d5995350da 100644
@@ -2202,8 +2202,13 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
 #ifdef CONFIG_KVM_PRIVATE_MEM
 #define kvm_arch_has_private_mem(kvm) ((kvm)->arch.has_private_mem)
+
+#define kvm_arch_gmem_supports_shared_mem(kvm)         \
+	(IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) &&      \
+	 ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM))
 #else
 #define kvm_arch_has_private_mem(kvm) false
+#define kvm_arch_gmem_supports_shared_mem(kvm) false
 #endif
 
 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state)
arch/x86/kvm/Kconfig +2 -1
index ea2c4f21c1ca..22d1bcdaad58 100644
@@ -45,7 +45,8 @@ config KVM_X86
 	select HAVE_KVM_PM_NOTIFIER if PM
 	select KVM_GENERIC_HARDWARE_ENABLING
 	select KVM_GENERIC_PRE_FAULT_MEMORY
-	select KVM_GENERIC_PRIVATE_MEM if KVM_SW_PROTECTED_VM
+	select KVM_PRIVATE_MEM if KVM_SW_PROTECTED_VM
+	select KVM_GMEM_SHARED_MEM if KVM_SW_PROTECTED_VM
 	select KVM_WERROR if WERROR
 
 config KVM
Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
author
#19 lore raw

RFC PATCH v2 7/11 KVM: arm64: Refactor user_mem_abort() calculation of force_pte

In reply to Fuad Tabba

To simplify the code and to make the assumptions clearer, refactor user_mem_abort() by immediately setting force_pte to true if logging_active is true. Also, add a check to ensure that the assumption that logging_active is guaranteed to never be true for VM_PFNMAP memslot is true.

---
 arch/arm64/kvm/mmu.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/arm64/kvm/mmu.c +7 -5
index c9d46ad57e52..1ec362d0d093 100644
@@ -1436,7 +1436,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			  bool fault_is_perm)
 {
 	int ret = 0;
-	bool write_fault, writable, force_pte = false;
+	bool write_fault, writable;
 	bool exec_fault, mte_allowed;
 	bool device = false, vfio_allow_any_uc = false;
 	unsigned long mmu_seq;
@@ -1448,6 +1448,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	gfn_t gfn;
 	kvm_pfn_t pfn;
 	bool logging_active = memslot_is_logging(memslot);
+	bool force_pte = logging_active;
 	long vma_pagesize, fault_granule;
 	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
 	struct kvm_pgtable *pgt;
@@ -1493,12 +1494,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	 * logging_active is guaranteed to never be true for VM_PFNMAP
 	 * memslots.
 	 */
-	if (logging_active) {
-		force_pte = true;
+	if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP)))
+		return -EFAULT;
+
+	if (force_pte)
 		vma_shift = PAGE_SHIFT;
-	} else {
+	else
 		vma_shift = get_vma_page_shift(vma, hva);
-	}
 
 	switch (vma_shift) {
 #ifndef __PAGETABLE_PMD_FOLDED
Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
author
#20 lore raw

RFC PATCH v2 8/11 KVM: arm64: Handle guest_memfd()-backed guest page faults

In reply to Fuad Tabba

Add arm64 support for handling guest page faults on guest_memfd backed memslots.

For now, the fault granule is restricted to PAGE_SIZE.

---
 arch/arm64/kvm/mmu.c     | 84 ++++++++++++++++++++++++++--------------
 include/linux/kvm_host.h |  5 +++
 virt/kvm/kvm_main.c      |  5 ---
 3 files changed, 61 insertions(+), 33 deletions(-)
arch/arm64/kvm/mmu.c +56 -28
index 1ec362d0d093..c1f3ddb88cb9 100644
@@ -1430,6 +1430,33 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)
 	return vma->vm_flags & VM_MTE_ALLOWED;
 }
 
+static kvm_pfn_t faultin_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
+			     gfn_t gfn, bool write_fault, bool *writable,
+			     struct page **page, bool is_private)
+{
+	kvm_pfn_t pfn;
+	int ret;
+
+	if (!is_private)
+		return __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page);
+
+	*writable = false;
+
+	if (WARN_ON_ONCE(write_fault && memslot_is_readonly(slot)))
+		return KVM_PFN_ERR_NOSLOT_MASK;
+
+	ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, page, NULL);
+	if (!ret) {
+		*writable = write_fault;
+		return pfn;
+	}
+
+	if (ret == -EHWPOISON)
+		return KVM_PFN_ERR_HWPOISON;
+
+	return KVM_PFN_ERR_NOSLOT_MASK;
+}
+
 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			  struct kvm_s2_trans *nested,
 			  struct kvm_memory_slot *memslot, unsigned long hva,
@@ -1437,24 +1464,25 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 {
 	int ret = 0;
 	bool write_fault, writable;
-	bool exec_fault, mte_allowed;
+	bool exec_fault, mte_allowed = false;
 	bool device = false, vfio_allow_any_uc = false;
 	unsigned long mmu_seq;
 	phys_addr_t ipa = fault_ipa;
 	struct kvm *kvm = vcpu->kvm;
 	struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
-	struct vm_area_struct *vma;
+	struct vm_area_struct *vma = NULL;
 	short vma_shift;
-	gfn_t gfn;
+	gfn_t gfn = ipa >> PAGE_SHIFT;
 	kvm_pfn_t pfn;
 	bool logging_active = memslot_is_logging(memslot);
-	bool force_pte = logging_active;
-	long vma_pagesize, fault_granule;
+	bool is_private = kvm_mem_is_private(kvm, gfn);
+	bool force_pte = logging_active || is_private;
+	long vma_pagesize, fault_granule = PAGE_SIZE;
 	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
 	struct kvm_pgtable *pgt;
 	struct page *page;
 
-	if (fault_is_perm)
+	if (fault_is_perm && !is_private)
 		fault_granule = kvm_vcpu_trap_get_perm_fault_granule(vcpu);
 	write_fault = kvm_is_write_fault(vcpu);
 	exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu);
@@ -1478,24 +1506,30 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			return ret;
 	}
 
+	mmap_read_lock(current->mm);
+
 	/*
 	 * Let's check if we will get back a huge page backed by hugetlbfs, or
 	 * get block mapping for device MMIO region.
 	 */
-	mmap_read_lock(current->mm);
-	vma = vma_lookup(current->mm, hva);
-	if (unlikely(!vma)) {
-		kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
-		mmap_read_unlock(current->mm);
-		return -EFAULT;
-	}
+	if (!is_private) {
+		vma = vma_lookup(current->mm, hva);
+		if (unlikely(!vma)) {
+			kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
+			mmap_read_unlock(current->mm);
+			return -EFAULT;
+		}
 
-	/*
-	 * logging_active is guaranteed to never be true for VM_PFNMAP
-	 * memslots.
-	 */
-	if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP)))
-		return -EFAULT;
+		/*
+		 * logging_active is guaranteed to never be true for VM_PFNMAP
+		 * memslots.
+		 */
+		if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP)))
+			return -EFAULT;
+
+		vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
+		mte_allowed = kvm_vma_mte_allowed(vma);
+	}
 
 	if (force_pte)
 		vma_shift = PAGE_SHIFT;
@@ -1565,18 +1599,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		ipa &= ~(vma_pagesize - 1);
 	}
 
-	gfn = ipa >> PAGE_SHIFT;
-	mte_allowed = kvm_vma_mte_allowed(vma);
-
-	vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
-
 	/* Don't use the VMA after the unlock -- it may have vanished */
 	vma = NULL;
 
 	/*
 	 * Read mmu_invalidate_seq so that KVM can detect if the results of
-	 * vma_lookup() or __kvm_faultin_pfn() become stale prior to
-	 * acquiring kvm->mmu_lock.
+	 * vma_lookup() or faultin_pfn() become stale prior to acquiring
+	 * kvm->mmu_lock.
 	 *
 	 * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs
 	 * with the smp_wmb() in kvm_mmu_invalidate_end().
@@ -1584,8 +1613,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	mmu_seq = vcpu->kvm->mmu_invalidate_seq;
 	mmap_read_unlock(current->mm);
 
-	pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0,
-				&writable, &page);
+	pfn = faultin_pfn(kvm, memslot, gfn, write_fault, &writable, &page, is_private);
 	if (pfn == KVM_PFN_ERR_HWPOISON) {
 		kvm_send_hwpoison_signal(hva, vma_shift);
 		return 0;
include/linux/kvm_host.h +5 -0
index e57cdf4e3f3f..42397fc4a1fb 100644
@@ -1864,6 +1864,11 @@ static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
 	return gfn_to_memslot(kvm, gfn)->id;
 }
 
+static inline bool memslot_is_readonly(const struct kvm_memory_slot *slot)
+{
+	return slot->flags & KVM_MEM_READONLY;
+}
+
 static inline gfn_t
 hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
 {
virt/kvm/kvm_main.c +0 -5
index 40e4ed512923..9f913a7fc44b 100644
@@ -2622,11 +2622,6 @@ unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn)
 	return size;
 }
 
-static bool memslot_is_readonly(const struct kvm_memory_slot *slot)
-{
-	return slot->flags & KVM_MEM_READONLY;
-}
-
 static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t gfn,
 				       gfn_t *nr_pages, bool write)
 {
Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
author
#21 lore raw

RFC PATCH v2 9/11 KVM: arm64: Introduce KVM_VM_TYPE_ARM_SW_PROTECTED machine type

In reply to Fuad Tabba

Introduce a new virtual machine type, KVM_VM_TYPE_ARM_SW_PROTECTED, to serve as a development and testing vehicle for Confidential (CoCo) VMs, similar to the x86 KVM_X86_SW_PROTECTED_VM type.

Initially, this is used to test guest_memfd without needing any underlying protection.

Similar to the x86 type, this is currently only for development and testing. Do not use KVM_VM_TYPE_ARM_SW_PROTECTED for "real" VMs, and especially not in production. The behavior and effective ABI for software-protected VMs is unstable.

---
 Documentation/virt/kvm/api.rst    |  5 +++++
 arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
 arch/arm64/kvm/arm.c              |  5 +++++
 arch/arm64/kvm/mmu.c              |  3 ---
 include/uapi/linux/kvm.h          |  6 ++++++
 5 files changed, 26 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +5 -0
index f15b61317aad..7953b07c8c2b 100644
@@ -214,6 +214,11 @@ exposed by the guest CPUs in ID_AA64MMFR0_EL1[PARange]. It only affects
 size of the address translated by the stage2 level (guest physical to
 host physical address translations).
 
+KVM_VM_TYPE_ARM_SW_PROTECTED is currently only for development and testing of
+confidential VMs without having underlying support. Do not use
+KVM_VM_TYPE_ARM_SW_PROTECTED for "real" VMs, and especially not in production.
+The behavior and effective ABI for software-protected VMs is unstable.
+
 
 4.3 KVM_GET_MSR_INDEX_LIST, KVM_GET_MSR_FEATURE_INDEX_LIST
 ----------------------------------------------------------
arch/arm64/include/asm/kvm_host.h +10 -0
index e18e9244d17a..e8a0db2ac4fa 100644
@@ -380,6 +380,8 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */
 	struct kvm_protected_vm pkvm;
+
+	unsigned long vm_type;
 };
 
 struct kvm_vcpu_fault_info {
@@ -1529,4 +1531,12 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
 #define kvm_has_s1poe(k)				\
 	(kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP))
 
+#define kvm_arch_has_private_mem(kvm)			\
+	(IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) &&		\
+	 ((kvm)->arch.vm_type & KVM_VM_TYPE_ARM_SW_PROTECTED))
+
+#define kvm_arch_gmem_supports_shared_mem(kvm)		\
+	(IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) &&	\
+	 ((kvm)->arch.vm_type & KVM_VM_TYPE_ARM_SW_PROTECTED))
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/arm.c +5 -0
index a102c3aebdbc..ecdb8db619d8 100644
@@ -171,6 +171,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 {
 	int ret;
 
+	if (type & ~KVM_VM_TYPE_MASK)
+		return -EINVAL;
+
 	mutex_init(&kvm->arch.config_lock);
 
 #ifdef CONFIG_LOCKDEP
@@ -212,6 +215,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
 
+	kvm->arch.vm_type = type;
+
 	return 0;
 
 err_free_cpumask:
arch/arm64/kvm/mmu.c +0 -3
index c1f3ddb88cb9..8e19248533f1 100644
@@ -869,9 +869,6 @@ static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)
 	u64 mmfr0, mmfr1;
 	u32 phys_shift;
 
-	if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
-		return -EINVAL;
-
 	phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
 	if (is_protected_kvm_enabled()) {
 		phys_shift = kvm_ipa_limit;
include/uapi/linux/kvm.h +6 -0
index 3ac805c5abf1..a3973d2b1a69 100644
@@ -656,6 +656,12 @@ struct kvm_enable_cap {
 #define KVM_VM_TYPE_ARM_IPA_SIZE_MASK	0xffULL
 #define KVM_VM_TYPE_ARM_IPA_SIZE(x)		\
 	((x) & KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
+
+#define KVM_VM_TYPE_ARM_SW_PROTECTED	(1UL << 9)
+
+#define KVM_VM_TYPE_MASK	(KVM_VM_TYPE_ARM_IPA_SIZE_MASK | \
+				 KVM_VM_TYPE_ARM_SW_PROTECTED)
+
 /*
  * ioctls for /dev/kvm fds:
  */
Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
author
#22 lore raw

RFC PATCH v2 10/11 KVM: arm64: Enable mapping guest_memfd in arm64

In reply to Fuad Tabba

Enable mapping guest_memfd in arm64, which would only apply to VMs with the type KVM_VM_TYPE_ARM_SW_PROTECTED.

---
 arch/arm64/kvm/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/kvm/Kconfig +1 -0
index ead632ad01b4..4830d8805bed 100644
@@ -38,6 +38,7 @@ menuconfig KVM
 	select HAVE_KVM_VCPU_RUN_PID_CHANGE
 	select SCHED_INFO
 	select GUEST_PERF_EVENTS if PERF_EVENTS
+	select KVM_GMEM_SHARED_MEM
 	help
 	  Support hosting virtualized guest machines.
 
Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
author
#23 lore raw

RFC PATCH v2 11/11 KVM: guest_memfd: selftests: guest_memfd mmap() test when mapping is allowed

In reply to Fuad Tabba

Expand the guest_memfd selftests to include testing mapping guest memory for VM types that support it.

Also, build the guest_memfd selftest for aarch64.

---
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../testing/selftests/kvm/guest_memfd_test.c  | 75 +++++++++++++++++--
 tools/testing/selftests/kvm/lib/kvm_util.c    |  3 +-
 3 files changed, 71 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/Makefile +1 -0
index 41593d2e7de9..c998eb3c3b77 100644
@@ -174,6 +174,7 @@ TEST_GEN_PROGS_aarch64 += coalesced_io_test
 TEST_GEN_PROGS_aarch64 += demand_paging_test
 TEST_GEN_PROGS_aarch64 += dirty_log_test
 TEST_GEN_PROGS_aarch64 += dirty_log_perf_test
+TEST_GEN_PROGS_aarch64 += guest_memfd_test
 TEST_GEN_PROGS_aarch64 += guest_print_test
 TEST_GEN_PROGS_aarch64 += get-reg-list
 TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
tools/testing/selftests/kvm/guest_memfd_test.c +69 -6

110 lines not on this page · raw message

tools/testing/selftests/kvm/lib/kvm_util.c +1 -2
index 480e3a40d197..098ea04ec099 100644
@@ -347,9 +347,8 @@ struct kvm_vm *____vm_create(struct vm_shape shape)
 	}
 
 #ifdef __aarch64__
-	TEST_ASSERT(!vm->type, "ARM doesn't support test-provided types");
 	if (vm->pa_bits != 40)
-		vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
+		vm->type |= KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
 #endif
 
 	vm_open(vm);
Signature · 1 line
2.48.1.262.g85cc9f2d1e-goog
#24 lore raw
In reply to Fuad Tabba

On 29.01.25 18:23, Fuad Tabba wrote:

Thanks for the new version

Quoted 3 lines

Main changes since v1 [1]:
- Added x86 support for mapping guest_memfd at the host, enabled only for the KVM_X86_SW_PROTECTED_VM type.

Nice!

Quoted 4 lines

- Require setting memslot userspace_addr for guest_memfd slots even if shared, and remove patches that worked around that.
- Brought in more of the infrastructure from the patch series that allows restricted mapping of guest_memfd backed memory.

Ah, that explains why we see the page_type stuff in here now :)

Quoted 3 lines

- Renamed references to "mappable" -> "shared".
- Expanded the selftests.
- Added instructions to test on x86 and arm64 (below).

Very nice!

I assume there is still no page conversion happening -- or is there now that the page_stuff thing is in here?

Would be good to spell out what's supported and what's still TBD regarding mmap support.

Signature · 3 lines
Cheers,

David / dhildenb
author
#25 lore raw

Hi David,

Quoted 22 lines · David Hildenbrand

On 29.01.25 18:23, Fuad Tabba wrote:

Thanks for the new version

Quoted 3 lines

Main changes since v1 [1]:
- Added x86 support for mapping guest_memfd at the host, enabled only for the KVM_X86_SW_PROTECTED_VM type.

Nice!

Quoted 4 lines

- Require setting memslot userspace_addr for guest_memfd slots even if shared, and remove patches that worked around that.
- Brought in more of the infrastructure from the patch series that allows restricted mapping of guest_memfd backed memory.

Ah, that explains why we see the page_type stuff in here now :)

Quoted 3 lines

- Renamed references to "mappable" -> "shared".
- Expanded the selftests.
- Added instructions to test on x86 and arm64 (below).

Quoted 8 lines

Very nice!

I assume there is still no page conversion happening -- or is there now that the page_stuff thing is in here?

Would be good to spell out what's supported and what's still TBD regarding mmap support.

Thanks! No page conversion happening yet. I'm rebasing the other series, the one with the conversions, on top of this one, as well as fixing it based on the feedback that I got.

What this is missing is the infrastructure that tracks the mappability/shareability at the host and the guest, as well as the implementation of the callbacks themselves. I thought I'd send this one out now, while I work on the larger one, since this one is easier to test, and serves as a base for the coming part.

Cheers,
/fuad

Quoted 5 lines

--
Cheers,

David / dhildenb