Skip to contentSkip to conversation

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

PATCH Superseded Fuad Tabba 10 participants 91 messages May 13, 2025 – May 22, 2025

Subsystems (10)

v9 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Fuad Tabba

author
#0 lore raw

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

Main changes since v8 [1]:
- Added guest_memfd flag that toggles support for in-place shared memory
- Added best-effort validation that the userspace memory address range matches the shared memory backed by guest_memfd
- Rework handling faults for shared guest_memfd memory in x86
- Fixes based on feedback from the previous series
- Rebase on Linux 6.15-rc6

The purpose of this series is to allow mapping guest_memfd backed memory at the host. This support enables VMMs like Firecracker to run guests backed completely by guest_memfd [2]. Combined with Patrick's series for direct map removal in guest_memfd [3], this would allow running VMs that offer additional hardening against Spectre-like transient execution attacks.

This series will also serve as a base for _restricted_ mmap() support for guest_memfd backed memory at the host for CoCos that allow sharing guest memory in-place with the host [4].

Patches 1 to 6 are mainly about decoupling the concept of guest memory being private vs guest memory being backed by guest_memfd. They are mostly refactoring and renaming.

Patches 7 and 8 add support for in-place shared memory, as well as the ability to map it by the host as long as it is shared, gated by a new configuration option, toggled by a new flag, and advertised to userspace by a new capability (introduced in patch 15).

Patches 9 to 14 add x86 and arm64 support for in-place shared memory.

Patch 15 introduces the capability that advertises support for in-place shared memory, and updates the documentation.

Patches 16 and 17 add new selftests for the added features.

For details on how to test this patch series, and on how to boot a guest has uses the new features, please refer to v8 [1].

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/firecracker-microvm/firecracker/tree/feature/secret-hiding
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/

Ackerley Tng (4):
KVM: guest_memfd: Check that userspace_addr and fd+offset refer to
same range
KVM: x86/mmu: Handle guest page faults for guest_memfd with shared
memory
KVM: x86: Compute max_mapping_level with input from guest_memfd
KVM: selftests: Test guest_memfd same-range validation

Fuad Tabba (13):
KVM: Rename CONFIG_KVM_PRIVATE_MEM to CONFIG_KVM_GMEM
KVM: Rename CONFIG_KVM_GENERIC_PRIVATE_MEM to
CONFIG_KVM_GENERIC_GMEM_POPULATE
KVM: Rename kvm_arch_has_private_mem() to kvm_arch_supports_gmem()
KVM: x86: Rename kvm->arch.has_private_mem to kvm->arch.supports_gmem
KVM: Rename kvm_slot_can_be_private() to kvm_slot_has_gmem()
KVM: Fix comments that refer to slots_lock
KVM: guest_memfd: Allow host to map guest_memfd() pages
KVM: arm64: Refactor user_mem_abort() calculation of force_pte
KVM: arm64: Rename variables in user_mem_abort()
KVM: arm64: Handle guest_memfd()-backed guest page faults
KVM: arm64: Enable mapping guest_memfd in arm64
KVM: Introduce the KVM capability KVM_CAP_GMEM_SHARED_MEM
KVM: selftests: guest_memfd mmap() test when mapping is allowed

 Documentation/virt/kvm/api.rst                |  18 +
 arch/arm64/include/asm/kvm_host.h             |  10 +
 arch/arm64/kvm/Kconfig                        |   1 +
 arch/arm64/kvm/mmu.c                          | 149 +++++----
 arch/x86/include/asm/kvm_host.h               |  22 +-
 arch/x86/kvm/Kconfig                          |   4 +-
 arch/x86/kvm/mmu/mmu.c                        | 135 +++++---
 arch/x86/kvm/svm/sev.c                        |   4 +-
 arch/x86/kvm/svm/svm.c                        |   4 +-
 arch/x86/kvm/x86.c                            |   3 +-
 include/linux/kvm_host.h                      |  76 ++++-
 include/uapi/linux/kvm.h                      |   2 +
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../testing/selftests/kvm/guest_memfd_test.c  | 313 ++++++++++++++++--
 virt/kvm/Kconfig                              |  15 +-
 virt/kvm/Makefile.kvm                         |   2 +-
 virt/kvm/guest_memfd.c                        | 152 ++++++++-
 virt/kvm/kvm_main.c                           |  21 +-
 virt/kvm/kvm_mm.h                             |   4 +-
 19 files changed, 753 insertions(+), 183 deletions(-)

base-commit: 82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3

Signature · 1 line
2.49.0.1045.g170613ef41-goog
author
#1 lore raw

PATCH v9 1/17 KVM: Rename CONFIG_KVM_PRIVATE_MEM to CONFIG_KVM_GMEM

The option KVM_PRIVATE_MEM enables guest_memfd in general. Subsequent patches add shared memory support to guest_memfd. Therefore, rename it to KVM_GMEM to make its purpose clearer.

---
 arch/x86/include/asm/kvm_host.h |  2 +-
 include/linux/kvm_host.h        | 10 +++++-----
 virt/kvm/Kconfig                |  8 ++++----
 virt/kvm/Makefile.kvm           |  2 +-
 virt/kvm/kvm_main.c             |  4 ++--
 virt/kvm/kvm_mm.h               |  4 ++--
 6 files changed, 15 insertions(+), 15 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index 7bc174a1f1cb..52f6f6d08558 100644
@@ -2253,7 +2253,7 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 		       int tdp_max_root_level, int tdp_huge_page_level);
 
 
-#ifdef CONFIG_KVM_PRIVATE_MEM
+#ifdef CONFIG_KVM_GMEM
 #define kvm_arch_has_private_mem(kvm) ((kvm)->arch.has_private_mem)
 #else
 #define kvm_arch_has_private_mem(kvm) false
include/linux/kvm_host.h +5 -5
index 291d49b9bf05..d6900995725d 100644
@@ -601,7 +601,7 @@ struct kvm_memory_slot {
 	short id;
 	u16 as_id;
 
-#ifdef CONFIG_KVM_PRIVATE_MEM
+#ifdef CONFIG_KVM_GMEM
 	struct {
 		/*
 		 * Writes protected by kvm->slots_lock.  Acquiring a
@@ -722,7 +722,7 @@ static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu)
  * Arch code must define kvm_arch_has_private_mem if support for private memory
  * is enabled.
  */
-#if !defined(kvm_arch_has_private_mem) && !IS_ENABLED(CONFIG_KVM_PRIVATE_MEM)
+#if !defined(kvm_arch_has_private_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
 static inline bool kvm_arch_has_private_mem(struct kvm *kvm)
 {
 	return false;
@@ -2504,7 +2504,7 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm,
 
 static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
 {
-	return IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) &&
+	return IS_ENABLED(CONFIG_KVM_GMEM) &&
 	       kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
 }
 #else
@@ -2514,7 +2514,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
 }
 #endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */
 
-#ifdef CONFIG_KVM_PRIVATE_MEM
+#ifdef CONFIG_KVM_GMEM
 int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
 		     gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
 		     int *max_order);
@@ -2527,7 +2527,7 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
 	KVM_BUG_ON(1, kvm);
 	return -EIO;
 }
-#endif /* CONFIG_KVM_PRIVATE_MEM */
+#endif /* CONFIG_KVM_GMEM */
 
 #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE
 int kvm_arch_gmem_prepare(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int max_order);
virt/kvm/Kconfig +4 -4
index 727b542074e7..49df4e32bff7 100644
@@ -112,19 +112,19 @@ config KVM_GENERIC_MEMORY_ATTRIBUTES
        depends on KVM_GENERIC_MMU_NOTIFIER
        bool
 
-config KVM_PRIVATE_MEM
+config KVM_GMEM
        select XARRAY_MULTI
        bool
 
 config KVM_GENERIC_PRIVATE_MEM
        select KVM_GENERIC_MEMORY_ATTRIBUTES
-       select KVM_PRIVATE_MEM
+       select KVM_GMEM
        bool
 
 config HAVE_KVM_ARCH_GMEM_PREPARE
        bool
-       depends on KVM_PRIVATE_MEM
+       depends on KVM_GMEM
 
 config HAVE_KVM_ARCH_GMEM_INVALIDATE
        bool
-       depends on KVM_PRIVATE_MEM
+       depends on KVM_GMEM
virt/kvm/Makefile.kvm +1 -1
index 724c89af78af..8d00918d4c8b 100644
@@ -12,4 +12,4 @@ kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
 kvm-$(CONFIG_HAVE_KVM_IRQ_ROUTING) += $(KVM)/irqchip.o
 kvm-$(CONFIG_HAVE_KVM_DIRTY_RING) += $(KVM)/dirty_ring.o
 kvm-$(CONFIG_HAVE_KVM_PFNCACHE) += $(KVM)/pfncache.o
-kvm-$(CONFIG_KVM_PRIVATE_MEM) += $(KVM)/guest_memfd.o
+kvm-$(CONFIG_KVM_GMEM) += $(KVM)/guest_memfd.o
virt/kvm/kvm_main.c +2 -2
index e85b33a92624..4996cac41a8f 100644
@@ -4842,7 +4842,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 	case KVM_CAP_MEMORY_ATTRIBUTES:
 		return kvm_supported_mem_attributes(kvm);
 #endif
-#ifdef CONFIG_KVM_PRIVATE_MEM
+#ifdef CONFIG_KVM_GMEM
 	case KVM_CAP_GUEST_MEMFD:
 		return !kvm || kvm_arch_has_private_mem(kvm);
 #endif
@@ -5276,7 +5276,7 @@ static long kvm_vm_ioctl(struct file *filp,
 	case KVM_GET_STATS_FD:
 		r = kvm_vm_ioctl_get_stats_fd(kvm);
 		break;
-#ifdef CONFIG_KVM_PRIVATE_MEM
+#ifdef CONFIG_KVM_GMEM
 	case KVM_CREATE_GUEST_MEMFD: {
 		struct kvm_create_guest_memfd guest_memfd;
 
virt/kvm/kvm_mm.h +2 -2
index acef3f5c582a..ec311c0d6718 100644
@@ -67,7 +67,7 @@ static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,
 }
 #endif /* HAVE_KVM_PFNCACHE */
 
-#ifdef CONFIG_KVM_PRIVATE_MEM
+#ifdef CONFIG_KVM_GMEM
 void kvm_gmem_init(struct module *module);
 int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args);
 int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
@@ -91,6 +91,6 @@ static inline void kvm_gmem_unbind(struct kvm_memory_slot *slot)
 {
 	WARN_ON_ONCE(1);
 }
-#endif /* CONFIG_KVM_PRIVATE_MEM */
+#endif /* CONFIG_KVM_GMEM */
 
 #endif /* __KVM_MM_H__ */
Signature · 1 line
2.49.0.1045.g170613ef41-goog
Reviewed-by on v9 1/17
#2 lore raw

Re: [PATCH v9 01/17] KVM: Rename CONFIG_KVM_PRIVATE_MEM to CONFIG_KVM_GMEM

Quoted 8 lines · Fuad Tabba

The option KVM_PRIVATE_MEM enables guest_memfd in general. Subsequent patches add shared memory support to guest_memfd. Therefore, rename it to KVM_GMEM to make its purpose clearer.

Quoted 9 lines
---
  arch/x86/include/asm/kvm_host.h |  2 +-
  include/linux/kvm_host.h        | 10 +++++-----
  virt/kvm/Kconfig                |  8 ++++----
  virt/kvm/Makefile.kvm           |  2 +-
  virt/kvm/kvm_main.c             |  4 ++--
  virt/kvm/kvm_mm.h               |  4 ++--
  6 files changed, 15 insertions(+), 15 deletions(-)
author
#3 lore raw

PATCH v9 2/17 KVM: Rename CONFIG_KVM_GENERIC_PRIVATE_MEM to CONFIG_KVM_GENERIC_GMEM_POPULATE

In reply to Fuad Tabba

The option KVM_GENERIC_PRIVATE_MEM enables populating a GPA range with guest data. Rename it to KVM_GENERIC_GMEM_POPULATE to make its purpose clearer.

---
 arch/x86/kvm/Kconfig     | 4 ++--
 include/linux/kvm_host.h | 2 +-
 virt/kvm/Kconfig         | 2 +-
 virt/kvm/guest_memfd.c   | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/Kconfig +2 -2
index fe8ea8c097de..b37258253543 100644
@@ -46,7 +46,7 @@ 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_GENERIC_GMEM_POPULATE if KVM_SW_PROTECTED_VM
 	select KVM_WERROR if WERROR
 
 config KVM
@@ -145,7 +145,7 @@ config KVM_AMD_SEV
 	depends on KVM_AMD && X86_64
 	depends on CRYPTO_DEV_SP_PSP && !(KVM_AMD=y && CRYPTO_DEV_CCP_DD=m)
 	select ARCH_HAS_CC_PLATFORM
-	select KVM_GENERIC_PRIVATE_MEM
+	select KVM_GENERIC_GMEM_POPULATE
 	select HAVE_KVM_ARCH_GMEM_PREPARE
 	select HAVE_KVM_ARCH_GMEM_INVALIDATE
 	help
include/linux/kvm_host.h +1 -1
index d6900995725d..7ca23837fa52 100644
@@ -2533,7 +2533,7 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
 int kvm_arch_gmem_prepare(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int max_order);
 #endif
 
-#ifdef CONFIG_KVM_GENERIC_PRIVATE_MEM
+#ifdef CONFIG_KVM_GENERIC_GMEM_POPULATE
 /**
  * kvm_gmem_populate() - Populate/prepare a GPA range with guest data
  *
virt/kvm/Kconfig +1 -1
index 49df4e32bff7..559c93ad90be 100644
@@ -116,7 +116,7 @@ config KVM_GMEM
        select XARRAY_MULTI
        bool
 
-config KVM_GENERIC_PRIVATE_MEM
+config KVM_GENERIC_GMEM_POPULATE
        select KVM_GENERIC_MEMORY_ATTRIBUTES
        select KVM_GMEM
        bool
virt/kvm/guest_memfd.c +1 -1
index b2aa6bf24d3a..befea51bbc75 100644
@@ -638,7 +638,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
 }
 EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn);
 
-#ifdef CONFIG_KVM_GENERIC_PRIVATE_MEM
+#ifdef CONFIG_KVM_GENERIC_GMEM_POPULATE
 long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long npages,
 		       kvm_gmem_populate_cb post_populate, void *opaque)
 {
Signature · 1 line
2.49.0.1045.g170613ef41-goog
Reviewed-by on v9 2/17
#4 lore raw

Re: [PATCH v9 02/17] KVM: Rename CONFIG_KVM_GENERIC_PRIVATE_MEM to CONFIG_KVM_GENERIC_GMEM_POPULATE

Quoted 7 lines · Fuad Tabba

The option KVM_GENERIC_PRIVATE_MEM enables populating a GPA range with guest data. Rename it to KVM_GENERIC_GMEM_POPULATE to make its purpose clearer.

Reviewed-by on v9 2/17
#5 lore raw

Re: [PATCH v9 02/17] KVM: Rename CONFIG_KVM_GENERIC_PRIVATE_MEM to CONFIG_KVM_GENERIC_GMEM_POPULATE

In reply to Fuad Tabba
Quoted 7 lines · Fuad Tabba

The option KVM_GENERIC_PRIVATE_MEM enables populating a GPA range with guest data. Rename it to KVM_GENERIC_GMEM_POPULATE to make its purpose clearer.

Quoted 7 lines
---
  arch/x86/kvm/Kconfig     | 4 ++--
  include/linux/kvm_host.h | 2 +-
  virt/kvm/Kconfig         | 2 +-
  virt/kvm/guest_memfd.c   | 2 +-
  4 files changed, 5 insertions(+), 5 deletions(-)
author
#6 lore raw

PATCH v9 3/17 KVM: Rename kvm_arch_has_private_mem() to kvm_arch_supports_gmem()

In reply to Fuad Tabba

The function kvm_arch_has_private_mem() is used to indicate whether guest_memfd is supported by the architecture, which until now implies that its private. To decouple guest_memfd support from whether the memory is private, rename this function to kvm_arch_supports_gmem().

---
 arch/x86/include/asm/kvm_host.h | 8 ++++----
 arch/x86/kvm/mmu/mmu.c          | 8 ++++----
 include/linux/kvm_host.h        | 6 +++---
 virt/kvm/kvm_main.c             | 6 +++---
 4 files changed, 14 insertions(+), 14 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -4
index 52f6f6d08558..4a83fbae7056 100644
@@ -2254,9 +2254,9 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
 
 #ifdef CONFIG_KVM_GMEM
-#define kvm_arch_has_private_mem(kvm) ((kvm)->arch.has_private_mem)
+#define kvm_arch_supports_gmem(kvm) ((kvm)->arch.has_private_mem)
 #else
-#define kvm_arch_has_private_mem(kvm) false
+#define kvm_arch_supports_gmem(kvm) false
 #endif
 
 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state)
@@ -2309,8 +2309,8 @@ enum {
 #define HF_SMM_INSIDE_NMI_MASK	(1 << 2)
 
 # define KVM_MAX_NR_ADDRESS_SPACES	2
-/* SMM is currently unsupported for guests with private memory. */
-# define kvm_arch_nr_memslot_as_ids(kvm) (kvm_arch_has_private_mem(kvm) ? 1 : 2)
+/* SMM is currently unsupported for guests with guest_memfd (esp private) memory. */
+# define kvm_arch_nr_memslot_as_ids(kvm) (kvm_arch_supports_gmem(kvm) ? 1 : 2)
 # define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)
 # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role).smm)
 #else
arch/x86/kvm/mmu/mmu.c +4 -4
index 8d1b632e33d2..b66f1bf24e06 100644
@@ -4917,7 +4917,7 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
 	if (r)
 		return r;
 
-	if (kvm_arch_has_private_mem(vcpu->kvm) &&
+	if (kvm_arch_supports_gmem(vcpu->kvm) &&
 	    kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(range->gpa)))
 		error_code |= PFERR_PRIVATE_ACCESS;
 
@@ -7705,7 +7705,7 @@ bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm,
 	 * Zapping SPTEs in this case ensures KVM will reassess whether or not
 	 * a hugepage can be used for affected ranges.
 	 */
-	if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm)))
+	if (WARN_ON_ONCE(!kvm_arch_supports_gmem(kvm)))
 		return false;
 
 	if (WARN_ON_ONCE(range->end <= range->start))
@@ -7784,7 +7784,7 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm,
 	 * a range that has PRIVATE GFNs, and conversely converting a range to
 	 * SHARED may now allow hugepages.
 	 */
-	if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm)))
+	if (WARN_ON_ONCE(!kvm_arch_supports_gmem(kvm)))
 		return false;
 
 	/*
@@ -7840,7 +7840,7 @@ void kvm_mmu_init_memslot_memory_attributes(struct kvm *kvm,
 {
 	int level;
 
-	if (!kvm_arch_has_private_mem(kvm))
+	if (!kvm_arch_supports_gmem(kvm))
 		return;
 
 	for (level = PG_LEVEL_2M; level <= KVM_MAX_HUGEPAGE_LEVEL; level++) {
include/linux/kvm_host.h +3 -3
index 7ca23837fa52..6ca7279520cf 100644
@@ -719,11 +719,11 @@ static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu)
 #endif
 
 /*
- * Arch code must define kvm_arch_has_private_mem if support for private memory
+ * Arch code must define kvm_arch_supports_gmem if support for guest_memfd
  * is enabled.
  */
-#if !defined(kvm_arch_has_private_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
-static inline bool kvm_arch_has_private_mem(struct kvm *kvm)
+#if !defined(kvm_arch_supports_gmem) && !IS_ENABLED(CONFIG_KVM_GMEM)
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
 {
 	return false;
 }
virt/kvm/kvm_main.c +3 -3
index 4996cac41a8f..2468d50a9ed4 100644
@@ -1531,7 +1531,7 @@ static int check_memory_region_flags(struct kvm *kvm,
 {
 	u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
 
-	if (kvm_arch_has_private_mem(kvm))
+	if (kvm_arch_supports_gmem(kvm))
 		valid_flags |= KVM_MEM_GUEST_MEMFD;
 
 	/* Dirty logging private memory is not currently supported. */
@@ -2362,7 +2362,7 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm,
 #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
 static u64 kvm_supported_mem_attributes(struct kvm *kvm)
 {
-	if (!kvm || kvm_arch_has_private_mem(kvm))
+	if (!kvm || kvm_arch_supports_gmem(kvm))
 		return KVM_MEMORY_ATTRIBUTE_PRIVATE;
 
 	return 0;
@@ -4844,7 +4844,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 #endif
 #ifdef CONFIG_KVM_GMEM
 	case KVM_CAP_GUEST_MEMFD:
-		return !kvm || kvm_arch_has_private_mem(kvm);
+		return !kvm || kvm_arch_supports_gmem(kvm);
 #endif
 	default:
 		break;
Signature · 1 line
2.49.0.1045.g170613ef41-goog
Reviewed-by on v9 3/17
#7 lore raw

Re: [PATCH v9 03/17] KVM: Rename kvm_arch_has_private_mem() to kvm_arch_supports_gmem()

Quoted 9 lines · Fuad Tabba

The function kvm_arch_has_private_mem() is used to indicate whether guest_memfd is supported by the architecture, which until now implies that its private. To decouple guest_memfd support from whether the memory is private, rename this function to kvm_arch_supports_gmem().

Quoted 7 lines
---
  arch/x86/include/asm/kvm_host.h | 8 ++++----
  arch/x86/kvm/mmu/mmu.c          | 8 ++++----
  include/linux/kvm_host.h        | 6 +++---
  virt/kvm/kvm_main.c             | 6 +++---
  4 files changed, 14 insertions(+), 14 deletions(-)
author
#8 lore raw

PATCH v9 4/17 KVM: x86: Rename kvm->arch.has_private_mem to kvm->arch.supports_gmem

In reply to Fuad Tabba

The bool has_private_mem is used to indicate whether guest_memfd is supported. Rename it to supports_gmem to make its meaning clearer and to decouple memory being private from guest_memfd.

---
 arch/x86/include/asm/kvm_host.h | 4 ++--
 arch/x86/kvm/mmu/mmu.c          | 2 +-
 arch/x86/kvm/svm/svm.c          | 4 ++--
 arch/x86/kvm/x86.c              | 3 +--
 4 files changed, 6 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -2
index 4a83fbae7056..709cc2a7ba66 100644
@@ -1331,7 +1331,7 @@ struct kvm_arch {
 	unsigned int indirect_shadow_pages;
 	u8 mmu_valid_gen;
 	u8 vm_type;
-	bool has_private_mem;
+	bool supports_gmem;
 	bool has_protected_state;
 	bool pre_fault_allowed;
 	struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
@@ -2254,7 +2254,7 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
 
 #ifdef CONFIG_KVM_GMEM
-#define kvm_arch_supports_gmem(kvm) ((kvm)->arch.has_private_mem)
+#define kvm_arch_supports_gmem(kvm) ((kvm)->arch.supports_gmem)
 #else
 #define kvm_arch_supports_gmem(kvm) false
 #endif
arch/x86/kvm/mmu/mmu.c +1 -1
index b66f1bf24e06..69bf2ef22ed0 100644
@@ -3486,7 +3486,7 @@ static bool page_fault_can_be_fast(struct kvm *kvm, struct kvm_page_fault *fault
 	 * on RET_PF_SPURIOUS until the update completes, or an actual spurious
 	 * case might go down the slow path. Either case will resolve itself.
 	 */
-	if (kvm->arch.has_private_mem &&
+	if (kvm->arch.supports_gmem &&
 	    fault->is_private != kvm_mem_is_private(kvm, fault->gfn))
 		return false;
 
arch/x86/kvm/svm/svm.c +2 -2
index a89c271a1951..a05b7dc7b717 100644
@@ -5110,8 +5110,8 @@ static int svm_vm_init(struct kvm *kvm)
 			(type == KVM_X86_SEV_ES_VM || type == KVM_X86_SNP_VM);
 		to_kvm_sev_info(kvm)->need_init = true;
 
-		kvm->arch.has_private_mem = (type == KVM_X86_SNP_VM);
-		kvm->arch.pre_fault_allowed = !kvm->arch.has_private_mem;
+		kvm->arch.supports_gmem = (type == KVM_X86_SNP_VM);
+		kvm->arch.pre_fault_allowed = !kvm->arch.supports_gmem;
 	}
 
 	if (!pause_filter_count || !pause_filter_thresh)
arch/x86/kvm/x86.c +1 -2
index 9896fd574bfc..12433b1e755b 100644
@@ -12716,8 +12716,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 		return -EINVAL;
 
 	kvm->arch.vm_type = type;
-	kvm->arch.has_private_mem =
-		(type == KVM_X86_SW_PROTECTED_VM);
+	kvm->arch.supports_gmem = (type == KVM_X86_SW_PROTECTED_VM);
 	/* Decided by the vendor code for other VM types.  */
 	kvm->arch.pre_fault_allowed =
 		type == KVM_X86_DEFAULT_VM || type == KVM_X86_SW_PROTECTED_VM;
Signature · 1 line
2.49.0.1045.g170613ef41-goog
Reviewed-by on v9 4/17
#9 lore raw

Re: [PATCH v9 04/17] KVM: x86: Rename kvm->arch.has_private_mem to kvm->arch.supports_gmem

Quoted 8 lines · Fuad Tabba

The bool has_private_mem is used to indicate whether guest_memfd is supported. Rename it to supports_gmem to make its meaning clearer and to decouple memory being private from guest_memfd.

Quoted 7 lines
---
  arch/x86/include/asm/kvm_host.h | 4 ++--
  arch/x86/kvm/mmu/mmu.c          | 2 +-
  arch/x86/kvm/svm/svm.c          | 4 ++--
  arch/x86/kvm/x86.c              | 3 +--
  4 files changed, 6 insertions(+), 7 deletions(-)
author
#10 lore raw

PATCH v9 5/17 KVM: Rename kvm_slot_can_be_private() to kvm_slot_has_gmem()

In reply to Fuad Tabba

The function kvm_slot_can_be_private() is used to check whether a memory slot is backed by guest_memfd. Rename it to kvm_slot_has_gmem() to make that clearer and to decouple memory being private from guest_memfd.

---
 arch/x86/kvm/mmu/mmu.c   | 4 ++--
 arch/x86/kvm/svm/sev.c   | 4 ++--
 include/linux/kvm_host.h | 2 +-
 virt/kvm/guest_memfd.c   | 2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu/mmu.c +2 -2
index 69bf2ef22ed0..2b6376986f96 100644
@@ -3283,7 +3283,7 @@ static int __kvm_mmu_max_mapping_level(struct kvm *kvm,
 int kvm_mmu_max_mapping_level(struct kvm *kvm,
 			      const struct kvm_memory_slot *slot, gfn_t gfn)
 {
-	bool is_private = kvm_slot_can_be_private(slot) &&
+	bool is_private = kvm_slot_has_gmem(slot) &&
 			  kvm_mem_is_private(kvm, gfn);
 
 	return __kvm_mmu_max_mapping_level(kvm, slot, gfn, PG_LEVEL_NUM, is_private);
@@ -4496,7 +4496,7 @@ static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu,
 {
 	int max_order, r;
 
-	if (!kvm_slot_can_be_private(fault->slot)) {
+	if (!kvm_slot_has_gmem(fault->slot)) {
 		kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
 		return -EFAULT;
 	}
arch/x86/kvm/svm/sev.c +2 -2
index a7a7dc507336..27759ca6d2f2 100644
@@ -2378,7 +2378,7 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	mutex_lock(&kvm->slots_lock);
 
 	memslot = gfn_to_memslot(kvm, params.gfn_start);
-	if (!kvm_slot_can_be_private(memslot)) {
+	if (!kvm_slot_has_gmem(memslot)) {
 		ret = -EINVAL;
 		goto out;
 	}
@@ -4688,7 +4688,7 @@ void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code)
 	}
 
 	slot = gfn_to_memslot(kvm, gfn);
-	if (!kvm_slot_can_be_private(slot)) {
+	if (!kvm_slot_has_gmem(slot)) {
 		pr_warn_ratelimited("SEV: Unexpected RMP fault, non-private slot for GPA 0x%llx\n",
 				    gpa);
 		return;
include/linux/kvm_host.h +1 -1
index 6ca7279520cf..d9616ee6acc7 100644
@@ -614,7 +614,7 @@ struct kvm_memory_slot {
 #endif
 };
 
-static inline bool kvm_slot_can_be_private(const struct kvm_memory_slot *slot)
+static inline bool kvm_slot_has_gmem(const struct kvm_memory_slot *slot)
 {
 	return slot && (slot->flags & KVM_MEM_GUEST_MEMFD);
 }
virt/kvm/guest_memfd.c +1 -1
index befea51bbc75..6db515833f61 100644
@@ -654,7 +654,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long
 		return -EINVAL;
 
 	slot = gfn_to_memslot(kvm, start_gfn);
-	if (!kvm_slot_can_be_private(slot))
+	if (!kvm_slot_has_gmem(slot))
 		return -EINVAL;
 
 	file = kvm_gmem_get_file(slot);
Signature · 1 line
2.49.0.1045.g170613ef41-goog
Reviewed-by on v9 5/17
#11 lore raw

Re: [PATCH v9 05/17] KVM: Rename kvm_slot_can_be_private() to kvm_slot_has_gmem()

Quoted 8 lines · Fuad Tabba

The function kvm_slot_can_be_private() is used to check whether a memory slot is backed by guest_memfd. Rename it to kvm_slot_has_gmem() to make that clearer and to decouple memory being private from guest_memfd.

Quoted 7 lines
---
  arch/x86/kvm/mmu/mmu.c   | 4 ++--
  arch/x86/kvm/svm/sev.c   | 4 ++--
  include/linux/kvm_host.h | 2 +-
  virt/kvm/guest_memfd.c   | 2 +-
  4 files changed, 6 insertions(+), 6 deletions(-)
author
#12 lore raw

PATCH v9 6/17 KVM: Fix comments that refer to slots_lock

In reply to Fuad Tabba

Fix comments so that they refer to slots_lock instead of slots_locks (remove trailing s).

---
 include/linux/kvm_host.h | 2 +-
 virt/kvm/kvm_main.c      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/kvm_host.h +1 -1
index d9616ee6acc7..ae70e4e19700 100644
@@ -859,7 +859,7 @@ struct kvm {
 	struct notifier_block pm_notifier;
 #endif
 #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
-	/* Protected by slots_locks (for writes) and RCU (for reads) */
+	/* Protected by slots_lock (for writes) and RCU (for reads) */
 	struct xarray mem_attr_array;
 #endif
 	char stats_id[KVM_STATS_NAME_SIZE];
virt/kvm/kvm_main.c +1 -1
index 2468d50a9ed4..6289ea1685dd 100644
@@ -333,7 +333,7 @@ void kvm_flush_remote_tlbs_memslot(struct kvm *kvm,
 	 * All current use cases for flushing the TLBs for a specific memslot
 	 * are related to dirty logging, and many do the TLB flush out of
 	 * mmu_lock. The interaction between the various operations on memslot
-	 * must be serialized by slots_locks to ensure the TLB flush from one
+	 * must be serialized by slots_lock to ensure the TLB flush from one
 	 * operation is observed by any other operation on the same memslot.
 	 */
 	lockdep_assert_held(&kvm->slots_lock);
Signature · 1 line
2.49.0.1045.g170613ef41-goog
Reviewed-by on v9 6/17
#13 lore raw

Re: [PATCH v9 06/17] KVM: Fix comments that refer to slots_lock

Quoted 11 lines · Fuad Tabba

Fix comments so that they refer to slots_lock instead of slots_locks (remove trailing s).

---
  include/linux/kvm_host.h | 2 +-
  virt/kvm/kvm_main.c      | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
author
#14 lore raw

PATCH v9 7/17 KVM: guest_memfd: Allow host to map guest_memfd() pages

In reply to Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
 arch/x86/include/asm/kvm_host.h | 10 ++++
 include/linux/kvm_host.h        | 13 +++++
 include/uapi/linux/kvm.h        |  1 +
 virt/kvm/Kconfig                |  5 ++
 virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
 5 files changed, 117 insertions(+)
arch/x86/include/asm/kvm_host.h +10 -0
index 709cc2a7ba66..f72722949cae 100644
@@ -2255,8 +2255,18 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
 #ifdef CONFIG_KVM_GMEM
 #define kvm_arch_supports_gmem(kvm) ((kvm)->arch.supports_gmem)
+
+/*
+ * CoCo VMs with hardware support that use guest_memfd only for backing private
+ * memory, e.g., TDX, cannot use guest_memfd with userspace mapping enabled.
+ */
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm)			\
+	(IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) &&			\
+	 ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM ||		\
+	  (kvm)->arch.vm_type == KVM_X86_DEFAULT_VM))
 #else
 #define kvm_arch_supports_gmem(kvm) false
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false
 #endif
 
 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state)
include/linux/kvm_host.h +13 -0
index ae70e4e19700..2ec89c214978 100644
@@ -729,6 +729,19 @@ static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
 }
 #endif
 
+/*
+ * Returns true if this VM supports shared mem in guest_memfd.
+ *
+ * Arch code must define kvm_arch_vm_supports_gmem_shared_mem if support for
+ * guest_memfd is enabled.
+ */
+#if !defined(kvm_arch_vm_supports_gmem_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
+static inline bool kvm_arch_vm_supports_gmem_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)
 {
include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..9857022a0f0c 100644
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {
 #define KVM_MEMORY_ATTRIBUTE_PRIVATE           (1ULL << 3)
 
 #define KVM_CREATE_GUEST_MEMFD	_IOWR(KVMIO,  0xd4, struct kvm_create_guest_memfd)
+#define GUEST_MEMFD_FLAG_SUPPORT_SHARED	(1UL << 0)
 
 struct kvm_create_guest_memfd {
 	__u64 size;
virt/kvm/Kconfig +5 -0
index 559c93ad90be..f4e469a62a60 100644
@@ -128,3 +128,8 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
 config HAVE_KVM_ARCH_GMEM_INVALIDATE
        bool
        depends on KVM_GMEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"
virt/kvm/guest_memfd.c +88 -0

107 lines not on this page · raw message

Signature · 1 line
2.49.0.1045.g170613ef41-goog
#15 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Quoted 141 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
 arch/x86/include/asm/kvm_host.h | 10 ++++
 include/linux/kvm_host.h        | 13 +++++
 include/uapi/linux/kvm.h        |  1 +
 virt/kvm/Kconfig                |  5 ++
 virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
 5 files changed, 117 insertions(+)
arch/x86/include/asm/kvm_host.h +10 -0
index 709cc2a7ba66..f72722949cae 100644
@@ -2255,8 +2255,18 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
 #ifdef CONFIG_KVM_GMEM
 #define kvm_arch_supports_gmem(kvm) ((kvm)->arch.supports_gmem)
+
+/*
+ * CoCo VMs with hardware support that use guest_memfd only for backing private
+ * memory, e.g., TDX, cannot use guest_memfd with userspace mapping enabled.
+ */
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm)			\
+	(IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) &&			\
+	 ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM ||		\
+	  (kvm)->arch.vm_type == KVM_X86_DEFAULT_VM))
 #else
 #define kvm_arch_supports_gmem(kvm) false
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false
 #endif
 
 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state)
include/linux/kvm_host.h +13 -0
index ae70e4e19700..2ec89c214978 100644
@@ -729,6 +729,19 @@ static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
 }
 #endif
 
+/*
+ * Returns true if this VM supports shared mem in guest_memfd.
+ *
+ * Arch code must define kvm_arch_vm_supports_gmem_shared_mem if support for
+ * guest_memfd is enabled.
+ */
+#if !defined(kvm_arch_vm_supports_gmem_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
+static inline bool kvm_arch_vm_supports_gmem_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)
 {
include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..9857022a0f0c 100644
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {
 #define KVM_MEMORY_ATTRIBUTE_PRIVATE           (1ULL << 3)
 
 #define KVM_CREATE_GUEST_MEMFD	_IOWR(KVMIO,  0xd4, struct kvm_create_guest_memfd)
+#define GUEST_MEMFD_FLAG_SUPPORT_SHARED	(1UL << 0)
 
 struct kvm_create_guest_memfd {
 	__u64 size;
virt/kvm/Kconfig +5 -0
index 559c93ad90be..f4e469a62a60 100644
@@ -128,3 +128,8 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
 config HAVE_KVM_ARCH_GMEM_INVALIDATE
        bool
        depends on KVM_GMEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"
virt/kvm/guest_memfd.c +44 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+	uint64_t flags = (uint64_t)inode->i_private;
+
+	return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+		int err = PTR_ERR(folio);
+
+		if (err == -EAGAIN)
+			ret = VM_FAULT_RETRY;
+		else
+			ret = vmf_error(err);
+
+		goto out_filemap;
+	}
+
+	if (folio_test_hwpoison(folio)) {
+		ret = VM_FAULT_HWPOISON;
+		goto out_folio;
+	}
+
+	if (WARN_ON_ONCE(folio_test_large(folio))) {
+		ret = VM_FAULT_SIGBUS;
+		goto out_folio;
+	}
+
+	if (!folio_test_uptodate(folio)) {
+		clear_highpage(folio_page(folio, 0));
+		kvm_gmem_mark_prepared(folio);
+	}
+

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

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+	vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+	if (ret != VM_FAULT_LOCKED) {
+		folio_unlock(folio);
+		folio_put(folio);
+	}
+
+out_filemap:
+	filemap_invalidate_unlock_shared(inode->i_mapping);

Do we need to hold the filemap_invalidate_lock while zeroing? Would holding the folio lock be enough?

Quoted 52 lines
virt/kvm/guest_memfd.c +34 -0
+
+	return ret;
+}
+
+static const struct vm_operations_struct kvm_gmem_vm_ops = {
+	.fault = kvm_gmem_fault_shared,
+};
+
+static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	if (!kvm_gmem_supports_shared(file_inode(file)))
+		return -ENODEV;
+
+	if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) !=
+	    (VM_SHARED | VM_MAYSHARE)) {
+		return -EINVAL;
+	}
+
+	vma->vm_ops = &kvm_gmem_vm_ops;
+
+	return 0;
+}
+#else
+#define kvm_gmem_mmap NULL
+#endif /* CONFIG_KVM_GMEM_SHARED_MEM */
+
 static struct file_operations kvm_gmem_fops = {
+	.mmap		= kvm_gmem_mmap,
 	.open		= generic_file_open,
 	.release	= kvm_gmem_release,
 	.fallocate	= kvm_gmem_fallocate,
@@ -463,6 +544,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
 	u64 flags = args->flags;
 	u64 valid_flags = 0;
 
+	if (kvm_arch_vm_supports_gmem_shared_mem(kvm))
+		valid_flags |= GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+
 	if (flags & ~valid_flags)
 		return -EINVAL;
 
@@ -501,6 +585,10 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
 	    offset + size > i_size_read(inode))
 		goto err;
 
+	if (kvm_gmem_supports_shared(inode) &&
+	    !kvm_arch_vm_supports_gmem_shared_mem(kvm))
+		goto err;
+
 	filemap_invalidate_lock(inode->i_mapping);
 
 	start = offset >> PAGE_SHIFT;
#16 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Quoted 35 lines · Ackerley Tng
Quoted 33 lines · Fuad Tabba
virt/kvm/guest_memfd.c +25 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+     uint64_t flags = (uint64_t)inode->i_private;
+
+     return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+             int err = PTR_ERR(folio);
+
+             if (err == -EAGAIN)
+                     ret = VM_FAULT_RETRY;
+             else
+                     ret = vmf_error(err);
Quoted 8 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 +8 -0
+
+             goto out_filemap;
+     }
+
+     if (folio_test_hwpoison(folio)) {
+             ret = VM_FAULT_HWPOISON;
+             goto out_folio;
+     }

nit: shmem_fault() does not include an equivalent of the above HWPOISON check, and __do_fault() already handles HWPOISON.

It's very unlikely for folio to be hwpoison and not up-to-date, and even then, writing over poison (to zero the folio) is not usually fatal.

Quoted 5 lines

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

Quoted 5 lines
virt/kvm/guest_memfd.c +5 -0
+
+     if (WARN_ON_ONCE(folio_test_large(folio))) {
+             ret = VM_FAULT_SIGBUS;
+             goto out_folio;
+     }

nit: I would prefer we remove this SIGBUS bit and change the below clearing logic to handle large folios. Up to you I suppose.

Quoted 11 lines
Quoted 11 lines
virt/kvm/guest_memfd.c +11 -0
+
+     if (!folio_test_uptodate(folio)) {
+             clear_highpage(folio_page(folio, 0));
+             kvm_gmem_mark_prepared(folio);
+     }
+
+     vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+     if (ret != VM_FAULT_LOCKED) {
+             folio_unlock(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 368

Quoted 5 lines
virt/kvm/guest_memfd.c +5 -0
+             folio_put(folio);
+     }
+
+out_filemap:
+     filemap_invalidate_unlock_shared(inode->i_mapping);

Do we need to hold the filemap_invalidate_lock while zeroing? Would holding the folio lock be enough?

Do we need to hold the filemap_invalidate_lock for reading *at all*?

I don't see why we need it. We're not checking gmem->bindings, and filemap_grab_folio() already synchronizes with filemap removal properly.

Quoted 4 lines
Quoted 4 lines
virt/kvm/guest_memfd.c +3 -0
+
+     return ret;
+}
author
#17 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Hi James,

Quoted 49 lines · James Houghton
Quoted 35 lines · Ackerley Tng
Quoted 33 lines · Fuad Tabba
virt/kvm/guest_memfd.c +25 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+     uint64_t flags = (uint64_t)inode->i_private;
+
+     return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+             int err = PTR_ERR(folio);
+
+             if (err == -EAGAIN)
+                     ret = VM_FAULT_RETRY;
+             else
+                     ret = vmf_error(err);
Quoted 8 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 +8 -0
+
+             goto out_filemap;
+     }
+
+     if (folio_test_hwpoison(folio)) {
+             ret = VM_FAULT_HWPOISON;
+             goto out_folio;
+     }

nit: shmem_fault() does not include an equivalent of the above HWPOISON check, and __do_fault() already handles HWPOISON.

Quoted 3 lines

It's very unlikely for folio to be hwpoison and not up-to-date, and even then, writing over poison (to zero the folio) is not usually fatal.

No strong preference, but the fact the it's still possible (even if unlikely) makes me lean towards keeping it.

Quoted 8 lines
Quoted 5 lines

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

Quoted 5 lines
virt/kvm/guest_memfd.c +5 -0
+
+     if (WARN_ON_ONCE(folio_test_large(folio))) {
+             ret = VM_FAULT_SIGBUS;
+             goto out_folio;
+     }

nit: I would prefer we remove this SIGBUS bit and change the below clearing logic to handle large folios. Up to you I suppose.

No strong preference here either. This is meant as a way to point out the lack of hugepage support, based on suggestions from a previous spin of this series.

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
virt/kvm/guest_memfd.c +11 -0
+
+     if (!folio_test_uptodate(folio)) {
+             clear_highpage(folio_page(folio, 0));
+             kvm_gmem_mark_prepared(folio);
+     }
+
+     vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+     if (ret != VM_FAULT_LOCKED) {
+             folio_unlock(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 368

Quoted 5 lines
virt/kvm/guest_memfd.c +5 -0
+             folio_put(folio);
+     }
+
+out_filemap:
+     filemap_invalidate_unlock_shared(inode->i_mapping);

Do we need to hold the filemap_invalidate_lock while zeroing? Would holding the folio lock be enough?

Quoted 5 lines

Do we need to hold the filemap_invalidate_lock for reading *at all*?

I don't see why we need it. We're not checking gmem->bindings, and filemap_grab_folio() already synchronizes with filemap removal properly.

Ack.

Thanks!
/fuad

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
virt/kvm/guest_memfd.c +3 -0
+
+     return ret;
+}
#18 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Quoted 52 lines · Fuad Tabba

Hi James,

Quoted 49 lines · James Houghton
Quoted 35 lines · Ackerley Tng
Quoted 33 lines · Fuad Tabba
virt/kvm/guest_memfd.c +25 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+     uint64_t flags = (uint64_t)inode->i_private;
+
+     return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+             int err = PTR_ERR(folio);
+
+             if (err == -EAGAIN)
+                     ret = VM_FAULT_RETRY;
+             else
+                     ret = vmf_error(err);
Quoted 8 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 +8 -0
+
+             goto out_filemap;
+     }
+
+     if (folio_test_hwpoison(folio)) {
+             ret = VM_FAULT_HWPOISON;
+             goto out_folio;
+     }

nit: shmem_fault() does not include an equivalent of the above HWPOISON check, and __do_fault() already handles HWPOISON.

Quoted 6 lines
Quoted 3 lines

It's very unlikely for folio to be hwpoison and not up-to-date, and even then, writing over poison (to zero the folio) is not usually fatal.

No strong preference, but the fact the it's still possible (even if unlikely) makes me lean towards keeping it.

__do_fault() indeed seems to handle it, so probably best to drop that for now.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
virt/kvm/guest_memfd.c +5 -0
+
+     if (WARN_ON_ONCE(folio_test_large(folio))) {
+             ret = VM_FAULT_SIGBUS;
+             goto out_folio;
+     }
Quoted 6 lines
Quoted 2 lines

nit: I would prefer we remove this SIGBUS bit and change the below clearing logic to handle large folios. Up to you I suppose.

No strong preference here either. This is meant as a way to point out the lack of hugepage support, based on suggestions from a previous spin of this series.

Yeah. With in-place conversion, we should never see large folios on this path. With shared-only VMs it will be different.

So for now, we can just leave it in and whoever stumbles over it can properly reason why it is OK for their use case to remove it.

Signature · 3 lines
Cheers,

David / dhildenb
#19 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

In reply to Fuad Tabba
Quoted 81 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
 arch/x86/include/asm/kvm_host.h | 10 ++++
 include/linux/kvm_host.h        | 13 +++++
 include/uapi/linux/kvm.h        |  1 +
 virt/kvm/Kconfig                |  5 ++
 virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
 5 files changed, 117 insertions(+)
arch/x86/include/asm/kvm_host.h +10 -0
index 709cc2a7ba66..f72722949cae 100644
@@ -2255,8 +2255,18 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
 #ifdef CONFIG_KVM_GMEM
 #define kvm_arch_supports_gmem(kvm) ((kvm)->arch.supports_gmem)
+
+/*
+ * CoCo VMs with hardware support that use guest_memfd only for backing private
+ * memory, e.g., TDX, cannot use guest_memfd with userspace mapping enabled.
+ */
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm)			\
+	(IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) &&			\
+	 ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM ||		\
+	  (kvm)->arch.vm_type == KVM_X86_DEFAULT_VM))
 #else
 #define kvm_arch_supports_gmem(kvm) false
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false
 #endif
 
 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state)
include/linux/kvm_host.h +13 -0
index ae70e4e19700..2ec89c214978 100644
@@ -729,6 +729,19 @@ static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
 }
 #endif
 
+/*
+ * Returns true if this VM supports shared mem in guest_memfd.
+ *
+ * Arch code must define kvm_arch_vm_supports_gmem_shared_mem if support for
+ * guest_memfd is enabled.
+ */
+#if !defined(kvm_arch_vm_supports_gmem_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
+static inline bool kvm_arch_vm_supports_gmem_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)
 {
include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..9857022a0f0c 100644
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {
 #define KVM_MEMORY_ATTRIBUTE_PRIVATE           (1ULL << 3)
 
 #define KVM_CREATE_GUEST_MEMFD	_IOWR(KVMIO,  0xd4, struct kvm_create_guest_memfd)
+#define GUEST_MEMFD_FLAG_SUPPORT_SHARED	(1UL << 0)
 
 struct kvm_create_guest_memfd {
 	__u64 size;
virt/kvm/Kconfig +0 -0
index 559c93ad90be..f4e469a62a60 100644

virt/kvm/Kconfig config HAVE_KVM_ARCH_GMEM_PREPARE · line 135

Quoted 8 lines
virt/kvm/Kconfig +5 -0
@@ -128,3 +128,8 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
 config HAVE_KVM_ARCH_GMEM_INVALIDATE
        bool
        depends on KVM_GMEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"

Hi,

I noticed following warnings with checkpatch.pl:

WARNING: Argument 'kvm' is not used in function-like macro #42: FILE: arch/x86/include/asm/kvm_host.h:2269:
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false

WARNING: please write a help paragraph that fully describes the config symbol with at least 4 lines #91: FILE: virt/kvm/Kconfig:132:
+config KVM_GMEM_SHARED_MEM

+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"

0003-KVM-Rename-kvm_arch_has_private_mem-to-kvm_arch_supp.patch
-----------------------------------------------------------------------------
WARNING: Argument 'kvm' is not used in function-like macro #35: FILE: arch/x86/include/asm/kvm_host.h:2259:
+#define kvm_arch_supports_gmem(kvm) false

total: 0 errors, 1 warnings, 91 lines checked

Please let me know if these are ignored intentionally - if so, sorry for the noise.

Best Regards,
Shivank

Quoted 114 lines
virt/kvm/guest_memfd.c +88 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+	uint64_t flags = (uint64_t)inode->i_private;
+
+	return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+		int err = PTR_ERR(folio);
+
+		if (err == -EAGAIN)
+			ret = VM_FAULT_RETRY;
+		else
+			ret = vmf_error(err);
+
+		goto out_filemap;
+	}
+
+	if (folio_test_hwpoison(folio)) {
+		ret = VM_FAULT_HWPOISON;
+		goto out_folio;
+	}
+
+	if (WARN_ON_ONCE(folio_test_large(folio))) {
+		ret = VM_FAULT_SIGBUS;
+		goto out_folio;
+	}
+
+	if (!folio_test_uptodate(folio)) {
+		clear_highpage(folio_page(folio, 0));
+		kvm_gmem_mark_prepared(folio);
+	}
+
+	vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+	if (ret != VM_FAULT_LOCKED) {
+		folio_unlock(folio);
+		folio_put(folio);
+	}
+
+out_filemap:
+	filemap_invalidate_unlock_shared(inode->i_mapping);
+
+	return ret;
+}
+
+static const struct vm_operations_struct kvm_gmem_vm_ops = {
+	.fault = kvm_gmem_fault_shared,
+};
+
+static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	if (!kvm_gmem_supports_shared(file_inode(file)))
+		return -ENODEV;
+
+	if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) !=
+	    (VM_SHARED | VM_MAYSHARE)) {
+		return -EINVAL;
+	}
+
+	vma->vm_ops = &kvm_gmem_vm_ops;
+
+	return 0;
+}
+#else
+#define kvm_gmem_mmap NULL
+#endif /* CONFIG_KVM_GMEM_SHARED_MEM */
+
 static struct file_operations kvm_gmem_fops = {
+	.mmap		= kvm_gmem_mmap,
 	.open		= generic_file_open,
 	.release	= kvm_gmem_release,
 	.fallocate	= kvm_gmem_fallocate,
@@ -463,6 +544,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
 	u64 flags = args->flags;
 	u64 valid_flags = 0;
 
+	if (kvm_arch_vm_supports_gmem_shared_mem(kvm))
+		valid_flags |= GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+
 	if (flags & ~valid_flags)
 		return -EINVAL;
 
@@ -501,6 +585,10 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
 	    offset + size > i_size_read(inode))
 		goto err;
 
+	if (kvm_gmem_supports_shared(inode) &&
+	    !kvm_arch_vm_supports_gmem_shared_mem(kvm))
+		goto err;
+
 	filemap_invalidate_lock(inode->i_mapping);
 
 	start = offset >> PAGE_SHIFT;
author
#20 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Thanks Shivank,

Quoted 113 lines · Shivank Garg
Quoted 81 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
 arch/x86/include/asm/kvm_host.h | 10 ++++
 include/linux/kvm_host.h        | 13 +++++
 include/uapi/linux/kvm.h        |  1 +
 virt/kvm/Kconfig                |  5 ++
 virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
 5 files changed, 117 insertions(+)
arch/x86/include/asm/kvm_host.h +10 -0
index 709cc2a7ba66..f72722949cae 100644
@@ -2255,8 +2255,18 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
 #ifdef CONFIG_KVM_GMEM
 #define kvm_arch_supports_gmem(kvm) ((kvm)->arch.supports_gmem)
+
+/*
+ * CoCo VMs with hardware support that use guest_memfd only for backing private
+ * memory, e.g., TDX, cannot use guest_memfd with userspace mapping enabled.
+ */
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm)                    \
+     (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) &&                      \
+      ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM ||             \
+       (kvm)->arch.vm_type == KVM_X86_DEFAULT_VM))
 #else
 #define kvm_arch_supports_gmem(kvm) false
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false
 #endif
 
 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state)
include/linux/kvm_host.h +13 -0
index ae70e4e19700..2ec89c214978 100644
@@ -729,6 +729,19 @@ static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
 }
 #endif
 
+/*
+ * Returns true if this VM supports shared mem in guest_memfd.
+ *
+ * Arch code must define kvm_arch_vm_supports_gmem_shared_mem if support for
+ * guest_memfd is enabled.
+ */
+#if !defined(kvm_arch_vm_supports_gmem_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
+static inline bool kvm_arch_vm_supports_gmem_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)
 {
include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..9857022a0f0c 100644
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {
 #define KVM_MEMORY_ATTRIBUTE_PRIVATE           (1ULL << 3)
 
 #define KVM_CREATE_GUEST_MEMFD       _IOWR(KVMIO,  0xd4, struct kvm_create_guest_memfd)
+#define GUEST_MEMFD_FLAG_SUPPORT_SHARED      (1UL << 0)
 
 struct kvm_create_guest_memfd {
      __u64 size;
virt/kvm/Kconfig +0 -0
index 559c93ad90be..f4e469a62a60 100644

virt/kvm/Kconfig config HAVE_KVM_ARCH_GMEM_PREPARE · line 135

Quoted 8 lines
virt/kvm/Kconfig +5 -0
@@ -128,3 +128,8 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
 config HAVE_KVM_ARCH_GMEM_INVALIDATE
        bool
        depends on KVM_GMEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"

Hi,

I noticed following warnings with checkpatch.pl:

WARNING: Argument 'kvm' is not used in function-like macro #42: FILE: arch/x86/include/asm/kvm_host.h:2269:
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false

WARNING: please write a help paragraph that fully describes the config symbol with at least 4 lines #91: FILE: virt/kvm/Kconfig:132:

virt/kvm/Kconfig +4 -0
+config KVM_GMEM_SHARED_MEM
+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"

0003-KVM-Rename-kvm_arch_has_private_mem-to-kvm_arch_supp.patch
-----------------------------------------------------------------------------
WARNING: Argument 'kvm' is not used in function-like macro #35: FILE: arch/x86/include/asm/kvm_host.h:2259:
+#define kvm_arch_supports_gmem(kvm) false

Quoted 3 lines

total: 0 errors, 1 warnings, 91 lines checked

Please let me know if these are ignored intentionally - if so, sorry for the noise.

Yes, I did intentionally ignore these. kvm_arch_vm_supports_gmem_shared_mem() follows the same pattern as kvm_arch_supports_gmem(). As for the comment, I couldn't think of four lines to describe the config option that's not just fluff :)

Cheers,
/fuad

Quoted 119 lines

Best Regards,
Shivank

Quoted 114 lines
virt/kvm/guest_memfd.c +88 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+     uint64_t flags = (uint64_t)inode->i_private;
+
+     return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+             int err = PTR_ERR(folio);
+
+             if (err == -EAGAIN)
+                     ret = VM_FAULT_RETRY;
+             else
+                     ret = vmf_error(err);
+
+             goto out_filemap;
+     }
+
+     if (folio_test_hwpoison(folio)) {
+             ret = VM_FAULT_HWPOISON;
+             goto out_folio;
+     }
+
+     if (WARN_ON_ONCE(folio_test_large(folio))) {
+             ret = VM_FAULT_SIGBUS;
+             goto out_folio;
+     }
+
+     if (!folio_test_uptodate(folio)) {
+             clear_highpage(folio_page(folio, 0));
+             kvm_gmem_mark_prepared(folio);
+     }
+
+     vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+     if (ret != VM_FAULT_LOCKED) {
+             folio_unlock(folio);
+             folio_put(folio);
+     }
+
+out_filemap:
+     filemap_invalidate_unlock_shared(inode->i_mapping);
+
+     return ret;
+}
+
+static const struct vm_operations_struct kvm_gmem_vm_ops = {
+     .fault = kvm_gmem_fault_shared,
+};
+
+static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
+{
+     if (!kvm_gmem_supports_shared(file_inode(file)))
+             return -ENODEV;
+
+     if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) !=
+         (VM_SHARED | VM_MAYSHARE)) {
+             return -EINVAL;
+     }
+
+     vma->vm_ops = &kvm_gmem_vm_ops;
+
+     return 0;
+}
+#else
+#define kvm_gmem_mmap NULL
+#endif /* CONFIG_KVM_GMEM_SHARED_MEM */
+
 static struct file_operations kvm_gmem_fops = {
+     .mmap           = kvm_gmem_mmap,
      .open           = generic_file_open,
      .release        = kvm_gmem_release,
      .fallocate      = kvm_gmem_fallocate,
@@ -463,6 +544,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
      u64 flags = args->flags;
      u64 valid_flags = 0;
 
+     if (kvm_arch_vm_supports_gmem_shared_mem(kvm))
+             valid_flags |= GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+
      if (flags & ~valid_flags)
              return -EINVAL;
 
@@ -501,6 +585,10 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
          offset + size > i_size_read(inode))
              goto err;
 
+     if (kvm_gmem_supports_shared(inode) &&
+         !kvm_arch_vm_supports_gmem_shared_mem(kvm))
+             goto err;
+
      filemap_invalidate_lock(inode->i_mapping);
 
      start = offset >> PAGE_SHIFT;
#21 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

In reply to Fuad Tabba
Quoted 24 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
 arch/x86/include/asm/kvm_host.h | 10 ++++
 include/linux/kvm_host.h        | 13 +++++
 include/uapi/linux/kvm.h        |  1 +
 virt/kvm/Kconfig                |  5 ++
 virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
 5 files changed, 117 insertions(+)
arch/x86/include/asm/kvm_host.h +0 -0
index 709cc2a7ba66..f72722949cae 100644
@@ -2255,8 +2255,18 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
 #ifdef CONFIG_KVM_GMEM

arch/x86/include/asm/kvm_host.h void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level, · line 2266

Quoted 10 lines
arch/x86/include/asm/kvm_host.h +9 -0
 #define kvm_arch_supports_gmem(kvm) ((kvm)->arch.supports_gmem)
+
+/*
+ * CoCo VMs with hardware support that use guest_memfd only for backing private
+ * memory, e.g., TDX, cannot use guest_memfd with userspace mapping enabled.
+ */
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm)                      \
+       (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) &&                      \
+        ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM ||             \
+         (kvm)->arch.vm_type == KVM_X86_DEFAULT_VM))

I forgot what we ended up deciding wrt "allow guest_memfd usage for default VMs on x86" in the call two weeks ago, but if we want to do that as part of this series, then this also needs

arch/x86/kvm/x86.c +1 -1
index 12433b1e755b..904b15c678d6 100644
@@ -12716,7 +12716,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
                return -EINVAL;
 
        kvm->arch.vm_type = type;
-       kvm->arch.supports_gmem = (type == KVM_X86_SW_PROTECTED_VM);
+       kvm->arch.supports_gmem = type == KVM_X86_SW_PROTECTED_VM || type == KVM_X86_DEFAULT_VM;
        /* Decided by the vendor code for other VM types.  */
        kvm->arch.pre_fault_allowed =
                type == KVM_X86_DEFAULT_VM || type == KVM_X86_SW_PROTECTED_VM;

and with that I was able to run my firecracker tests on top of this patch series with X86_DEFAULT_VM. But I did wonder about this define in x86/include/asm/kvm_host.h:

/* SMM is currently unsupported for guests with guest_memfd (esp private) memory. */

# define kvm_arch_nr_memslot_as_ids(kvm) (kvm_arch_supports_gmem(kvm) ? 1 : 2)

which I'm not really sure what to make of, but which I think means enabling guest_memfd for X86_DEFAULT_VM isn't as straight-forward as the above diff :/

Best,
Patrick

Quoted 172 lines
arch/x86/include/asm/kvm_host.h +1 -0
 #else
 #define kvm_arch_supports_gmem(kvm) false
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false
 #endif
 
 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state)
include/linux/kvm_host.h +13 -0
index ae70e4e19700..2ec89c214978 100644
@@ -729,6 +729,19 @@ static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
 }
 #endif
 
+/*
+ * Returns true if this VM supports shared mem in guest_memfd.
+ *
+ * Arch code must define kvm_arch_vm_supports_gmem_shared_mem if support for
+ * guest_memfd is enabled.
+ */
+#if !defined(kvm_arch_vm_supports_gmem_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
+static inline bool kvm_arch_vm_supports_gmem_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)
 {
include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..9857022a0f0c 100644
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {
 #define KVM_MEMORY_ATTRIBUTE_PRIVATE           (1ULL << 3)
 
 #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO,  0xd4, struct kvm_create_guest_memfd)
+#define GUEST_MEMFD_FLAG_SUPPORT_SHARED        (1UL << 0)
 
 struct kvm_create_guest_memfd {
        __u64 size;
virt/kvm/Kconfig +5 -0
index 559c93ad90be..f4e469a62a60 100644
@@ -128,3 +128,8 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
 config HAVE_KVM_ARCH_GMEM_INVALIDATE
        bool
        depends on KVM_GMEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"
virt/kvm/guest_memfd.c +88 -1
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+       uint64_t flags = (uint64_t)inode->i_private;
+
+       return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+               int err = PTR_ERR(folio);
+
+               if (err == -EAGAIN)
+                       ret = VM_FAULT_RETRY;
+               else
+                       ret = vmf_error(err);
+
+               goto out_filemap;
+       }
+
+       if (folio_test_hwpoison(folio)) {
+               ret = VM_FAULT_HWPOISON;
+               goto out_folio;
+       }
+
+       if (WARN_ON_ONCE(folio_test_large(folio))) {
+               ret = VM_FAULT_SIGBUS;
+               goto out_folio;
+       }
+
+       if (!folio_test_uptodate(folio)) {
+               clear_highpage(folio_page(folio, 0));
+               kvm_gmem_mark_prepared(folio);
+       }
+
+       vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+       if (ret != VM_FAULT_LOCKED) {
+               folio_unlock(folio);
+               folio_put(folio);
+       }
+
+out_filemap:
+       filemap_invalidate_unlock_shared(inode->i_mapping);
+
+       return ret;
+}
+
+static const struct vm_operations_struct kvm_gmem_vm_ops = {
+       .fault = kvm_gmem_fault_shared,
+};
+
+static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
+{
+       if (!kvm_gmem_supports_shared(file_inode(file)))
+               return -ENODEV;
+
+       if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) !=
+           (VM_SHARED | VM_MAYSHARE)) {
+               return -EINVAL;
+       }
+
+       vma->vm_ops = &kvm_gmem_vm_ops;
+
+       return 0;
+}
+#else
+#define kvm_gmem_mmap NULL
+#endif /* CONFIG_KVM_GMEM_SHARED_MEM */
+
 static struct file_operations kvm_gmem_fops = {
+       .mmap           = kvm_gmem_mmap,
        .open           = generic_file_open,
        .release        = kvm_gmem_release,
        .fallocate      = kvm_gmem_fallocate,
@@ -463,6 +544,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
        u64 flags = args->flags;
        u64 valid_flags = 0;
 
+       if (kvm_arch_vm_supports_gmem_shared_mem(kvm))
+               valid_flags |= GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+
        if (flags & ~valid_flags)
                return -EINVAL;
 
@@ -501,6 +585,10 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
            offset + size > i_size_read(inode))
                goto err;
 
+       if (kvm_gmem_supports_shared(inode) &&
+           !kvm_arch_vm_supports_gmem_shared_mem(kvm))
+               goto err;
+
        filemap_invalidate_lock(inode->i_mapping);
 
        start = offset >> PAGE_SHIFT;
--

2.49.0.1045.g170613ef41-goog

author
#22 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Hi Patrick,

Quoted 25 lines · Patrick
Quoted 23 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
 arch/x86/include/asm/kvm_host.h | 10 ++++
 include/linux/kvm_host.h        | 13 +++++
 include/uapi/linux/kvm.h        |  1 +
 virt/kvm/Kconfig                |  5 ++
 virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
 5 files changed, 117 insertions(+)
arch/x86/include/asm/kvm_host.h +0 -0
index 709cc2a7ba66..f72722949cae 100644
@@ -2255,8 +2255,18 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
Quoted 15 lines

arch/x86/include/asm/kvm_host.h void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level, · line 2266

Quoted 11 lines
arch/x86/include/asm/kvm_host.h +9 -0
 #ifdef CONFIG_KVM_GMEM
 #define kvm_arch_supports_gmem(kvm) ((kvm)->arch.supports_gmem)
+
+/*
+ * CoCo VMs with hardware support that use guest_memfd only for backing private
+ * memory, e.g., TDX, cannot use guest_memfd with userspace mapping enabled.
+ */
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm)                      \
+       (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) &&                      \
+        ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM ||             \
+         (kvm)->arch.vm_type == KVM_X86_DEFAULT_VM))

I forgot what we ended up deciding wrt "allow guest_memfd usage for default VMs on x86" in the call two weeks ago, but if we want to do that as part of this series, then this also needs

Yes we did. I missed it in this patch. I'll fix it.

Quoted 18 lines
arch/x86/kvm/x86.c +1 -1
index 12433b1e755b..904b15c678d6 100644
@@ -12716,7 +12716,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
                return -EINVAL;
 
        kvm->arch.vm_type = type;
-       kvm->arch.supports_gmem = (type == KVM_X86_SW_PROTECTED_VM);
+       kvm->arch.supports_gmem = type == KVM_X86_SW_PROTECTED_VM || type == KVM_X86_DEFAULT_VM;
        /* Decided by the vendor code for other VM types.  */
        kvm->arch.pre_fault_allowed =
                type == KVM_X86_DEFAULT_VM || type == KVM_X86_SW_PROTECTED_VM;

and with that I was able to run my firecracker tests on top of this patch series with X86_DEFAULT_VM. But I did wonder about this define in x86/include/asm/kvm_host.h:

Quoted 5 lines

/* SMM is currently unsupported for guests with guest_memfd (esp private) memory. */

# define kvm_arch_nr_memslot_as_ids(kvm) (kvm_arch_supports_gmem(kvm) ? 1 : 2)

which I'm not really sure what to make of, but which I think means enabling guest_memfd for X86_DEFAULT_VM isn't as straight-forward as the above diff :/

Not quite, but I'll sort it out.

Thanks,
/fuad

Quoted 176 lines

Best,
Patrick

Quoted 172 lines
arch/x86/include/asm/kvm_host.h +1 -0
 #else
 #define kvm_arch_supports_gmem(kvm) false
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false
 #endif
 
 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state)
include/linux/kvm_host.h +13 -0
index ae70e4e19700..2ec89c214978 100644
@@ -729,6 +729,19 @@ static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
 }
 #endif
 
+/*
+ * Returns true if this VM supports shared mem in guest_memfd.
+ *
+ * Arch code must define kvm_arch_vm_supports_gmem_shared_mem if support for
+ * guest_memfd is enabled.
+ */
+#if !defined(kvm_arch_vm_supports_gmem_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
+static inline bool kvm_arch_vm_supports_gmem_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)
 {
include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..9857022a0f0c 100644
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {
 #define KVM_MEMORY_ATTRIBUTE_PRIVATE           (1ULL << 3)
 
 #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO,  0xd4, struct kvm_create_guest_memfd)
+#define GUEST_MEMFD_FLAG_SUPPORT_SHARED        (1UL << 0)
 
 struct kvm_create_guest_memfd {
        __u64 size;
virt/kvm/Kconfig +5 -0
index 559c93ad90be..f4e469a62a60 100644
@@ -128,3 +128,8 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
 config HAVE_KVM_ARCH_GMEM_INVALIDATE
        bool
        depends on KVM_GMEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"
virt/kvm/guest_memfd.c +88 -1
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+       uint64_t flags = (uint64_t)inode->i_private;
+
+       return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+               int err = PTR_ERR(folio);
+
+               if (err == -EAGAIN)
+                       ret = VM_FAULT_RETRY;
+               else
+                       ret = vmf_error(err);
+
+               goto out_filemap;
+       }
+
+       if (folio_test_hwpoison(folio)) {
+               ret = VM_FAULT_HWPOISON;
+               goto out_folio;
+       }
+
+       if (WARN_ON_ONCE(folio_test_large(folio))) {
+               ret = VM_FAULT_SIGBUS;
+               goto out_folio;
+       }
+
+       if (!folio_test_uptodate(folio)) {
+               clear_highpage(folio_page(folio, 0));
+               kvm_gmem_mark_prepared(folio);
+       }
+
+       vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+       if (ret != VM_FAULT_LOCKED) {
+               folio_unlock(folio);
+               folio_put(folio);
+       }
+
+out_filemap:
+       filemap_invalidate_unlock_shared(inode->i_mapping);
+
+       return ret;
+}
+
+static const struct vm_operations_struct kvm_gmem_vm_ops = {
+       .fault = kvm_gmem_fault_shared,
+};
+
+static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
+{
+       if (!kvm_gmem_supports_shared(file_inode(file)))
+               return -ENODEV;
+
+       if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) !=
+           (VM_SHARED | VM_MAYSHARE)) {
+               return -EINVAL;
+       }
+
+       vma->vm_ops = &kvm_gmem_vm_ops;
+
+       return 0;
+}
+#else
+#define kvm_gmem_mmap NULL
+#endif /* CONFIG_KVM_GMEM_SHARED_MEM */
+
 static struct file_operations kvm_gmem_fops = {
+       .mmap           = kvm_gmem_mmap,
        .open           = generic_file_open,
        .release        = kvm_gmem_release,
        .fallocate      = kvm_gmem_fallocate,
@@ -463,6 +544,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
        u64 flags = args->flags;
        u64 valid_flags = 0;
 
+       if (kvm_arch_vm_supports_gmem_shared_mem(kvm))
+               valid_flags |= GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+
        if (flags & ~valid_flags)
                return -EINVAL;
 
@@ -501,6 +585,10 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
            offset + size > i_size_read(inode))
                goto err;
 
+       if (kvm_gmem_supports_shared(inode) &&
+           !kvm_arch_vm_supports_gmem_shared_mem(kvm))
+               goto err;
+
        filemap_invalidate_lock(inode->i_mapping);
 
        start = offset >> PAGE_SHIFT;
--

2.49.0.1045.g170613ef41-goog

#23 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

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

Hi Fuad,

kernel test robot noticed the following build errors:

[auto build test ERROR on 82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3]

url:    https://github.com/intel-lab-lkp/linux/commits/Fuad-Tabba/KVM-Rename-CONFIG_KVM_PRIVATE_MEM-to-CONFIG_KVM_GMEM/20250514-003900
base:   82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3
patch link:    https://lore.kernel.org/r/20250513163438.3942405-8-tabba%40google.com
patch subject: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages
config: x86_64-rhel-9.4 (https://download.01.org/0day-ci/archive/20250514/[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/20250514/[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 >>):

arch/x86/kvm/../../../virt/kvm/guest_memfd.c: In function 'kvm_gmem_bind':

>> arch/x86/kvm/../../../virt/kvm/guest_memfd.c:588:13: error: implicit declaration of function 'kvm_gmem_supports_shared' [-Werror=implicit-function-declaration]
     588 |         if (kvm_gmem_supports_shared(inode) &&
         |             ^~~~~~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors

vim +/kvm_gmem_supports_shared +588 arch/x86/kvm/../../../virt/kvm/guest_memfd.c

558
559 int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
560 unsigned int fd, loff_t offset)
561 {
562 loff_t size = slot->npages << PAGE_SHIFT;
563 unsigned long start, end;
564 struct kvm_gmem *gmem;
565 struct inode *inode;
566 struct file *file;
567 int r = -EINVAL;
568
569 BUILD_BUG_ON(sizeof(gfn_t) != sizeof(slot->gmem.pgoff));
570
571 file = fget(fd);
572 if (!file)
573 return -EBADF;
574
575 if (file->f_op != &kvm_gmem_fops)
576 goto err;
577
578 gmem = file->private_data;
579 if (gmem->kvm != kvm)
580 goto err;
581
582 inode = file_inode(file);
583
584 if (offset < 0 || !PAGE_ALIGNED(offset) ||
585 offset + size > i_size_read(inode))
586 goto err;
587

 > 588		if (kvm_gmem_supports_shared(inode) &&

589 !kvm_arch_vm_supports_gmem_shared_mem(kvm))
590 goto err;
591
592 filemap_invalidate_lock(inode->i_mapping);
593
594 start = offset >> PAGE_SHIFT;
595 end = start + slot->npages;
596
597 if (!xa_empty(&gmem->bindings) &&
598 xa_find(&gmem->bindings, &start, end - 1, XA_PRESENT)) {
599 filemap_invalidate_unlock(inode->i_mapping);
600 goto err;
601 }
602
603 /*
604 * memslots of flag KVM_MEM_GUEST_MEMFD are immutable to change, so
605 * kvm_gmem_bind() must occur on a new memslot. Because the memslot
606 * is not visible yet, kvm_gmem_get_pfn() is guaranteed to see the file.
607 */
608 WRITE_ONCE(slot->gmem.file, file);
609 slot->gmem.pgoff = start;
610
611 xa_store_range(&gmem->bindings, start, end - 1, slot, GFP_KERNEL);
612 filemap_invalidate_unlock(inode->i_mapping);
613
614 /*
615 * Drop the reference to the file, even on success. The file pins KVM,
616 * not the other way 'round. Active bindings are invalidated if the
617 * file is closed before memslots are destroyed.
618 */
619 r = 0;
620 err:
621 fput(file);
622 return r;
623 }
624

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

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

In reply to Fuad Tabba

,

Quoted 194 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
 arch/x86/include/asm/kvm_host.h | 10 ++++
 include/linux/kvm_host.h        | 13 +++++
 include/uapi/linux/kvm.h        |  1 +
 virt/kvm/Kconfig                |  5 ++
 virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
 5 files changed, 117 insertions(+)
arch/x86/include/asm/kvm_host.h +10 -0
index 709cc2a7ba66..f72722949cae 100644
@@ -2255,8 +2255,18 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
 #ifdef CONFIG_KVM_GMEM
 #define kvm_arch_supports_gmem(kvm) ((kvm)->arch.supports_gmem)
+
+/*
+ * CoCo VMs with hardware support that use guest_memfd only for backing private
+ * memory, e.g., TDX, cannot use guest_memfd with userspace mapping enabled.
+ */
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm)                      \
+       (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) &&                      \
+        ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM ||             \
+         (kvm)->arch.vm_type == KVM_X86_DEFAULT_VM))
 #else
 #define kvm_arch_supports_gmem(kvm) false
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false
 #endif
 
 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state)
include/linux/kvm_host.h +13 -0
index ae70e4e19700..2ec89c214978 100644
@@ -729,6 +729,19 @@ static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
 }
 #endif
 
+/*
+ * Returns true if this VM supports shared mem in guest_memfd.
+ *
+ * Arch code must define kvm_arch_vm_supports_gmem_shared_mem if support for
+ * guest_memfd is enabled.
+ */
+#if !defined(kvm_arch_vm_supports_gmem_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
+static inline bool kvm_arch_vm_supports_gmem_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)
 {
include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..9857022a0f0c 100644
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {
 #define KVM_MEMORY_ATTRIBUTE_PRIVATE           (1ULL << 3)
 
 #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO,  0xd4, struct kvm_create_guest_memfd)
+#define GUEST_MEMFD_FLAG_SUPPORT_SHARED        (1UL << 0)
 
 struct kvm_create_guest_memfd {
        __u64 size;
virt/kvm/Kconfig +5 -0
index 559c93ad90be..f4e469a62a60 100644
@@ -128,3 +128,8 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
 config HAVE_KVM_ARCH_GMEM_INVALIDATE
        bool
        depends on KVM_GMEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"
virt/kvm/guest_memfd.c +84 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+       uint64_t flags = (uint64_t)inode->i_private;
+
+       return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+               int err = PTR_ERR(folio);
+
+               if (err == -EAGAIN)
+                       ret = VM_FAULT_RETRY;
+               else
+                       ret = vmf_error(err);
+
+               goto out_filemap;
+       }
+
+       if (folio_test_hwpoison(folio)) {
+               ret = VM_FAULT_HWPOISON;
+               goto out_folio;
+       }
+
+       if (WARN_ON_ONCE(folio_test_large(folio))) {
+               ret = VM_FAULT_SIGBUS;
+               goto out_folio;
+       }
+
+       if (!folio_test_uptodate(folio)) {
+               clear_highpage(folio_page(folio, 0));
+               kvm_gmem_mark_prepared(folio);
+       }
+
+       vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+       if (ret != VM_FAULT_LOCKED) {
+               folio_unlock(folio);
+               folio_put(folio);
+       }
+
+out_filemap:
+       filemap_invalidate_unlock_shared(inode->i_mapping);
+
+       return ret;
+}
+
+static const struct vm_operations_struct kvm_gmem_vm_ops = {
+       .fault = kvm_gmem_fault_shared,
+};
+
+static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
+{
+       if (!kvm_gmem_supports_shared(file_inode(file)))
+               return -ENODEV;
+
+       if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) !=
+           (VM_SHARED | VM_MAYSHARE)) {
+               return -EINVAL;
+       }
+
+       vma->vm_ops = &kvm_gmem_vm_ops;
+
+       return 0;
+}
+#else
+#define kvm_gmem_mmap NULL
+#endif /* CONFIG_KVM_GMEM_SHARED_MEM */
+
 static struct file_operations kvm_gmem_fops = {
+       .mmap           = kvm_gmem_mmap,
        .open           = generic_file_open,
        .release        = kvm_gmem_release,
        .fallocate      = kvm_gmem_fallocate,
@@ -463,6 +544,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
        u64 flags = args->flags;
        u64 valid_flags = 0;
 
+       if (kvm_arch_vm_supports_gmem_shared_mem(kvm))
+               valid_flags |= GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+
        if (flags & ~valid_flags)
                return -EINVAL;
 

virt/kvm/guest_memfd.c int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, · line 588

Quoted 4 lines
virt/kvm/guest_memfd.c +1 -0
@@ -501,6 +585,10 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
            offset + size > i_size_read(inode))
                goto err;
 
+       if (kvm_gmem_supports_shared(inode) &&

When building without CONFIG_KVM_GMEM_SHARED_MEM, my compiler complains that kvm_gmem_supports_shared() is not defined.

Quoted 9 lines
virt/kvm/guest_memfd.c +3 -1
+           !kvm_arch_vm_supports_gmem_shared_mem(kvm))
+               goto err;
+
        filemap_invalidate_lock(inode->i_mapping);
 
        start = offset >> PAGE_SHIFT;
--

2.49.0.1045.g170613ef41-goog

author
#25 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Quoted 197 lines · James Houghton

,

Quoted 193 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
 arch/x86/include/asm/kvm_host.h | 10 ++++
 include/linux/kvm_host.h        | 13 +++++
 include/uapi/linux/kvm.h        |  1 +
 virt/kvm/Kconfig                |  5 ++
 virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
 5 files changed, 117 insertions(+)
arch/x86/include/asm/kvm_host.h +10 -0
index 709cc2a7ba66..f72722949cae 100644
@@ -2255,8 +2255,18 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
 
 #ifdef CONFIG_KVM_GMEM
 #define kvm_arch_supports_gmem(kvm) ((kvm)->arch.supports_gmem)
+
+/*
+ * CoCo VMs with hardware support that use guest_memfd only for backing private
+ * memory, e.g., TDX, cannot use guest_memfd with userspace mapping enabled.
+ */
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm)                      \
+       (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) &&                      \
+        ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM ||             \
+         (kvm)->arch.vm_type == KVM_X86_DEFAULT_VM))
 #else
 #define kvm_arch_supports_gmem(kvm) false
+#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false
 #endif
 
 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state)
include/linux/kvm_host.h +13 -0
index ae70e4e19700..2ec89c214978 100644
@@ -729,6 +729,19 @@ static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
 }
 #endif
 
+/*
+ * Returns true if this VM supports shared mem in guest_memfd.
+ *
+ * Arch code must define kvm_arch_vm_supports_gmem_shared_mem if support for
+ * guest_memfd is enabled.
+ */
+#if !defined(kvm_arch_vm_supports_gmem_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
+static inline bool kvm_arch_vm_supports_gmem_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)
 {
include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..9857022a0f0c 100644
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {
 #define KVM_MEMORY_ATTRIBUTE_PRIVATE           (1ULL << 3)
 
 #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO,  0xd4, struct kvm_create_guest_memfd)
+#define GUEST_MEMFD_FLAG_SUPPORT_SHARED        (1UL << 0)
 
 struct kvm_create_guest_memfd {
        __u64 size;
virt/kvm/Kconfig +5 -0
index 559c93ad90be..f4e469a62a60 100644
@@ -128,3 +128,8 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
 config HAVE_KVM_ARCH_GMEM_INVALIDATE
        bool
        depends on KVM_GMEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"
virt/kvm/guest_memfd.c +84 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+       uint64_t flags = (uint64_t)inode->i_private;
+
+       return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+               int err = PTR_ERR(folio);
+
+               if (err == -EAGAIN)
+                       ret = VM_FAULT_RETRY;
+               else
+                       ret = vmf_error(err);
+
+               goto out_filemap;
+       }
+
+       if (folio_test_hwpoison(folio)) {
+               ret = VM_FAULT_HWPOISON;
+               goto out_folio;
+       }
+
+       if (WARN_ON_ONCE(folio_test_large(folio))) {
+               ret = VM_FAULT_SIGBUS;
+               goto out_folio;
+       }
+
+       if (!folio_test_uptodate(folio)) {
+               clear_highpage(folio_page(folio, 0));
+               kvm_gmem_mark_prepared(folio);
+       }
+
+       vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+       if (ret != VM_FAULT_LOCKED) {
+               folio_unlock(folio);
+               folio_put(folio);
+       }
+
+out_filemap:
+       filemap_invalidate_unlock_shared(inode->i_mapping);
+
+       return ret;
+}
+
+static const struct vm_operations_struct kvm_gmem_vm_ops = {
+       .fault = kvm_gmem_fault_shared,
+};
+
+static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
+{
+       if (!kvm_gmem_supports_shared(file_inode(file)))
+               return -ENODEV;
+
+       if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) !=
+           (VM_SHARED | VM_MAYSHARE)) {
+               return -EINVAL;
+       }
+
+       vma->vm_ops = &kvm_gmem_vm_ops;
+
+       return 0;
+}
+#else
+#define kvm_gmem_mmap NULL
+#endif /* CONFIG_KVM_GMEM_SHARED_MEM */
+
 static struct file_operations kvm_gmem_fops = {
+       .mmap           = kvm_gmem_mmap,
        .open           = generic_file_open,
        .release        = kvm_gmem_release,
        .fallocate      = kvm_gmem_fallocate,
@@ -463,6 +544,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
        u64 flags = args->flags;
        u64 valid_flags = 0;
 
+       if (kvm_arch_vm_supports_gmem_shared_mem(kvm))
+               valid_flags |= GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+
        if (flags & ~valid_flags)
                return -EINVAL;
 
Quoted 8 lines

virt/kvm/guest_memfd.c int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, · line 588

Quoted 5 lines
virt/kvm/guest_memfd.c +1 -0
@@ -501,6 +585,10 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
            offset + size > i_size_read(inode))
                goto err;
 
+       if (kvm_gmem_supports_shared(inode) &&

When building without CONFIG_KVM_GMEM_SHARED_MEM, my compiler complains that kvm_gmem_supports_shared() is not defined.

Thanks for pointing that out. I'll fix this.
/fuad

Quoted 10 lines
Quoted 10 lines
virt/kvm/guest_memfd.c +3 -1
+           !kvm_arch_vm_supports_gmem_shared_mem(kvm))
+               goto err;
+
        filemap_invalidate_lock(inode->i_mapping);
 
        start = offset >> PAGE_SHIFT;
--

2.49.0.1045.g170613ef41-goog

#26 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

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

Hi Fuad,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3]

url:    https://github.com/intel-lab-lkp/linux/commits/Fuad-Tabba/KVM-Rename-CONFIG_KVM_PRIVATE_MEM-to-CONFIG_KVM_GMEM/20250514-003900
base:   82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3
patch link:    https://lore.kernel.org/r/20250513163438.3942405-8-tabba%40google.com
patch subject: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages
config: i386-allmodconfig (https://download.01.org/0day-ci/archive/20250515/[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/20250515/[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 >>):

arch/x86/kvm/../../../virt/kvm/guest_memfd.c: In function 'kvm_gmem_supports_shared':

>> arch/x86/kvm/../../../virt/kvm/guest_memfd.c:319:26: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
     319 |         uint64_t flags = (uint64_t)inode->i_private;
         |                          ^
   In file included from <command-line>:
   arch/x86/kvm/../../../virt/kvm/guest_memfd.c: In function 'kvm_gmem_bind':
   include/linux/compiler_types.h:557:45: error: call to '__compiletime_assert_672' declared with attribute error: BUILD_BUG_ON failed: sizeof(gfn_t) != sizeof(slot->gmem.pgoff)
     557 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |                                             ^
   include/linux/compiler_types.h:538:25: note: in definition of macro '__compiletime_assert'
     538 |                         prefix ## suffix();                             \
         |                         ^~~~~~
   include/linux/compiler_types.h:557:9: note: in expansion of macro '_compiletime_assert'
     557 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |         ^~~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:39:37: note: in expansion of macro 'compiletime_assert'
      39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
         |                                     ^~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:50:9: note: in expansion of macro 'BUILD_BUG_ON_MSG'
      50 |         BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
         |         ^~~~~~~~~~~~~~~~
   arch/x86/kvm/../../../virt/kvm/guest_memfd.c:569:9: note: in expansion of macro 'BUILD_BUG_ON'
     569 |         BUILD_BUG_ON(sizeof(gfn_t) != sizeof(slot->gmem.pgoff));
         |         ^~~~~~~~~~~~

vim +319 arch/x86/kvm/../../../virt/kvm/guest_memfd.c

316
317 static bool kvm_gmem_supports_shared(struct inode *inode)
318 {

 > 319		uint64_t flags = (uint64_t)inode->i_private;

320
321 return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
322 }
323

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

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

In reply to Fuad Tabba

Hi Fuad,

Quoted 17 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
  arch/x86/include/asm/kvm_host.h | 10 ++++
  include/linux/kvm_host.h        | 13 +++++
  include/uapi/linux/kvm.h        |  1 +
  virt/kvm/Kconfig                |  5 ++
  virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
  5 files changed, 117 insertions(+)

[...]

include/uapi/linux/kvm.h struct kvm_memory_attributes { · line 1570

Quoted 10 lines
include/uapi/linux/kvm.h +1 -0
index b6ae8ad8934b..9857022a0f0c 100644
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {
  #define KVM_MEMORY_ATTRIBUTE_PRIVATE           (1ULL << 3)
 
  #define KVM_CREATE_GUEST_MEMFD	_IOWR(KVMIO,  0xd4, struct kvm_create_guest_memfd)
+#define GUEST_MEMFD_FLAG_SUPPORT_SHARED	(1UL << 0)
 

This would be (1ULL << 0) to be consistent with '__u64 struct kvm_create_guest_memfd::flags'

Thanks,
Gavin

author
#28 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Hi Gavin,

Quoted 31 lines · Gavin Shan

Hi Fuad,

Quoted 17 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
  arch/x86/include/asm/kvm_host.h | 10 ++++
  include/linux/kvm_host.h        | 13 +++++
  include/uapi/linux/kvm.h        |  1 +
  virt/kvm/Kconfig                |  5 ++
  virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
  5 files changed, 117 insertions(+)

[...]

Quoted 7 lines
include/uapi/linux/kvm.h +0 -0
index b6ae8ad8934b..9857022a0f0c 100644
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {
  #define KVM_MEMORY_ATTRIBUTE_PRIVATE           (1ULL << 3)
 
Quoted 5 lines

include/uapi/linux/kvm.h struct kvm_memory_attributes { · line 1570

Quoted 3 lines
include/uapi/linux/kvm.h +1 -0
  #define KVM_CREATE_GUEST_MEMFD      _IOWR(KVMIO,  0xd4, struct kvm_create_guest_memfd)
+#define GUEST_MEMFD_FLAG_SUPPORT_SHARED      (1UL << 0)
 

This would be (1ULL << 0) to be consistent with '__u64 struct kvm_create_guest_memfd::flags'

Ack.

Thanks!
/fuad

Quoted 3 lines

Thanks,
Gavin

#29 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

In reply to Fuad Tabba

Hi Fuad,

Quoted 17 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
  arch/x86/include/asm/kvm_host.h | 10 ++++
  include/linux/kvm_host.h        | 13 +++++
  include/uapi/linux/kvm.h        |  1 +
  virt/kvm/Kconfig                |  5 ++
  virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
  5 files changed, 117 insertions(+)

[...]

Quoted 37 lines
virt/kvm/guest_memfd.c +29 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+	uint64_t flags = (uint64_t)inode->i_private;
+
+	return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+		int err = PTR_ERR(folio);
+
+		if (err == -EAGAIN)
+			ret = VM_FAULT_RETRY;
+		else
+			ret = vmf_error(err);
+
+		goto out_filemap;
+	}
+

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

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

I don't think there is a large folio involved since the max/min folio order (stored in struct address_space::flags) should have been set to 0, meaning only order-0 is possible when the folio (page) is allocated and added to the page-cache. More details can be referred to AS_FOLIO_ORDER_MASK. It's unnecessary check but not harmful. Maybe a comment is needed to mention large folio isn't around yet, but double confirm.

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

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

I must be missing some thing here. This chunk of code is out of sync to kvm_gmem_get_pfn(), where kvm_gmem_prepare_folio() and kvm_arch_gmem_prepare() are executed, and then PG_uptodate is set after that. In the latest ARM CCA series, kvm_arch_gmem_prepare() isn't used, but it would delegate the folio (page) with the prerequisite that the folio belongs to the private address space.

I guess that kvm_arch_gmem_prepare() is skipped here because we have the assumption that the folio belongs to the shared address space? However, this assumption isn't always true. We probably need to ensure the folio range is really belonging to the shared address space by poking kvm->mem_attr_array, which can be modified by VMM through ioctl KVM_SET_MEMORY_ATTRIBUTES.

Quoted 4 lines
virt/kvm/guest_memfd.c +4 -0
+	vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+	if (ret != VM_FAULT_LOCKED) {

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

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+		folio_unlock(folio);
+		folio_put(folio);
+	}
+
+out_filemap:
+	filemap_invalidate_unlock_shared(inode->i_mapping);
+
+	return ret;
+}
+

Thanks,
Gavin

author
#30 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Hi Gavin,

Quoted 70 lines · Gavin Shan

Hi Fuad,

Quoted 17 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
  arch/x86/include/asm/kvm_host.h | 10 ++++
  include/linux/kvm_host.h        | 13 +++++
  include/uapi/linux/kvm.h        |  1 +
  virt/kvm/Kconfig                |  5 ++
  virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
  5 files changed, 117 insertions(+)

[...]

Quoted 46 lines
virt/kvm/guest_memfd.c +38 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+     uint64_t flags = (uint64_t)inode->i_private;
+
+     return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+             int err = PTR_ERR(folio);
+
+             if (err == -EAGAIN)
+                     ret = VM_FAULT_RETRY;
+             else
+                     ret = vmf_error(err);
+
+             goto out_filemap;
+     }
+
+     if (folio_test_hwpoison(folio)) {
+             ret = VM_FAULT_HWPOISON;
+             goto out_folio;
+     }
+
+     if (WARN_ON_ONCE(folio_test_large(folio))) {
+             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 353

Quoted 1 line
virt/kvm/guest_memfd.c +1 -0
+

I don't think there is a large folio involved since the max/min folio order (stored in struct address_space::flags) should have been set to 0, meaning only order-0 is possible when the folio (page) is allocated and added to the page-cache. More details can be referred to AS_FOLIO_ORDER_MASK. It's unnecessary check but not harmful. Maybe a comment is needed to mention large folio isn't around yet, but double confirm.

The idea is to document the lack of hugepage support in code, but if you think it's necessary, I could add a comment.

Quoted 13 lines

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

Quoted 6 lines
virt/kvm/guest_memfd.c +5 -0
+     if (!folio_test_uptodate(folio)) {
+             clear_highpage(folio_page(folio, 0));
+             kvm_gmem_mark_prepared(folio);
+     }
+

I must be missing some thing here. This chunk of code is out of sync to kvm_gmem_get_pfn(), where kvm_gmem_prepare_folio() and kvm_arch_gmem_prepare() are executed, and then PG_uptodate is set after that. In the latest ARM CCA series, kvm_arch_gmem_prepare() isn't used, but it would delegate the folio (page) with the prerequisite that the folio belongs to the private address space.

Quoted 5 lines

I guess that kvm_arch_gmem_prepare() is skipped here because we have the assumption that the folio belongs to the shared address space? However, this assumption isn't always true. We probably need to ensure the folio range is really belonging to the shared address space by poking kvm->mem_attr_array, which can be modified by VMM through ioctl KVM_SET_MEMORY_ATTRIBUTES.

This series only supports shared memory, and the idea is not to use the attributes to check. We ensure that only certain VM types can set the flag (e.g., VM_TYPE_DEFAULT and KVM_X86_SW_PROTECTED_VM).

In the patch series that builds on it, with in-place conversion between private and shared, we do add a check that the memory faulted in is in-fact shared.

Thanks,
/fuad

Quoted 18 lines
Quoted 4 lines
virt/kvm/guest_memfd.c +4 -0
+     vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+     if (ret != VM_FAULT_LOCKED) {

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

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+             folio_unlock(folio);
+             folio_put(folio);
+     }
+
+out_filemap:
+     filemap_invalidate_unlock_shared(inode->i_mapping);
+
+     return ret;
+}
+

Thanks,
Gavin

#31 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Hi Fuad,

Quoted 77 lines · Fuad Tabba
Quoted 67 lines · Gavin Shan
Quoted 17 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
   arch/x86/include/asm/kvm_host.h | 10 ++++
   include/linux/kvm_host.h        | 13 +++++
   include/uapi/linux/kvm.h        |  1 +
   virt/kvm/Kconfig                |  5 ++
   virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
   5 files changed, 117 insertions(+)

[...]

Quoted 46 lines
virt/kvm/guest_memfd.c +38 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+     uint64_t flags = (uint64_t)inode->i_private;
+
+     return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+             int err = PTR_ERR(folio);
+
+             if (err == -EAGAIN)
+                     ret = VM_FAULT_RETRY;
+             else
+                     ret = vmf_error(err);
+
+             goto out_filemap;
+     }
+
+     if (folio_test_hwpoison(folio)) {
+             ret = VM_FAULT_HWPOISON;
+             goto out_folio;
+     }
+
+     if (WARN_ON_ONCE(folio_test_large(folio))) {
+             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 353

Quoted 1 line
virt/kvm/guest_memfd.c +1 -0
+

I don't think there is a large folio involved since the max/min folio order (stored in struct address_space::flags) should have been set to 0, meaning only order-0 is possible when the folio (page) is allocated and added to the page-cache. More details can be referred to AS_FOLIO_ORDER_MASK. It's unnecessary check but not harmful. Maybe a comment is needed to mention large folio isn't around yet, but double confirm.

Quoted 3 lines

The idea is to document the lack of hugepage support in code, but if you think it's necessary, I could add a comment.

Ok, I was actually nit-picky since we're at v9, which is close to integration, I guess. If another respin is needed, a comment wouldn't be harmful, but it's also perfectly fine without it :)

Quoted 20 lines
Quoted 14 lines

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

Quoted 7 lines
virt/kvm/guest_memfd.c +5 -0
+     if (!folio_test_uptodate(folio)) {
+             clear_highpage(folio_page(folio, 0));
+             kvm_gmem_mark_prepared(folio);
+     }
+

I must be missing some thing here. This chunk of code is out of sync to kvm_gmem_get_pfn(), where kvm_gmem_prepare_folio() and kvm_arch_gmem_prepare() are executed, and then PG_uptodate is set after that. In the latest ARM CCA series, kvm_arch_gmem_prepare() isn't used, but it would delegate the folio (page) with the prerequisite that the folio belongs to the private address space.

Quoted 5 lines

I guess that kvm_arch_gmem_prepare() is skipped here because we have the assumption that the folio belongs to the shared address space? However, this assumption isn't always true. We probably need to ensure the folio range is really belonging to the shared address space by poking kvm->mem_attr_array, which can be modified by VMM through ioctl KVM_SET_MEMORY_ATTRIBUTES.

Quoted 8 lines

This series only supports shared memory, and the idea is not to use the attributes to check. We ensure that only certain VM types can set the flag (e.g., VM_TYPE_DEFAULT and KVM_X86_SW_PROTECTED_VM).

In the patch series that builds on it, with in-place conversion between private and shared, we do add a check that the memory faulted in is in-fact shared.

Ok, thanks for your clarification. I plan to review that series, but not getting a chance yet. Right, it's sensible to limit the capability of modifying page's attribute (private vs shared) to the particular machine types since the whole feature (restricted mmap and in-place conversion) is applicable to particular machine types. I can understand KVM_X86_SW_PROTECTED_VM (similar to pKVM) needs the feature, but I don't understand why VM_TYPE_DEFAULT needs the feature. I guess we may want to use guest-memfd as to tmpfs or shmem, meaning all the address space associated with a guest-memfd is shared, but without the corresponding private space pointed by struct kvm_userspace_memory_region2 ::userspace_addr. Instead, the 'userspace_addr' will be mmap(guest-memfd) from VMM's perspective if I'm correct.

Thanks,
Gavin

Quoted 22 lines

Thanks,
/fuad

Quoted 18 lines
Quoted 4 lines
virt/kvm/guest_memfd.c +4 -0
+     vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+     if (ret != VM_FAULT_LOCKED) {

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

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+             folio_unlock(folio);
+             folio_put(folio);
+     }
+
+out_filemap:
+     filemap_invalidate_unlock_shared(inode->i_mapping);
+
+     return ret;
+}
+

Thanks,
Gavin

author
#32 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

Hi Gavin,

Quoted 118 lines · Gavin Shan

Hi Fuad,

Quoted 77 lines · Fuad Tabba
Quoted 67 lines · Gavin Shan
Quoted 17 lines · Fuad Tabba

This patch enables support for shared memory in guest_memfd, including mapping that memory at the host userspace. This support is gated by the configuration option KVM_GMEM_SHARED_MEM, and toggled by the guest_memfd flag GUEST_MEMFD_FLAG_SUPPORT_SHARED, which can be set when creating a guest_memfd instance.

---
   arch/x86/include/asm/kvm_host.h | 10 ++++
   include/linux/kvm_host.h        | 13 +++++
   include/uapi/linux/kvm.h        |  1 +
   virt/kvm/Kconfig                |  5 ++
   virt/kvm/guest_memfd.c          | 88 +++++++++++++++++++++++++++++++++
   5 files changed, 117 insertions(+)

[...]

Quoted 46 lines
virt/kvm/guest_memfd.c +38 -0
index 6db515833f61..8e6d1866b55e 100644
@@ -312,7 +312,88 @@ 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 bool kvm_gmem_supports_shared(struct inode *inode)
+{
+     uint64_t flags = (uint64_t)inode->i_private;
+
+     return flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+}
+
+static vm_fault_t kvm_gmem_fault_shared(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)) {
+             int err = PTR_ERR(folio);
+
+             if (err == -EAGAIN)
+                     ret = VM_FAULT_RETRY;
+             else
+                     ret = vmf_error(err);
+
+             goto out_filemap;
+     }
+
+     if (folio_test_hwpoison(folio)) {
+             ret = VM_FAULT_HWPOISON;
+             goto out_folio;
+     }
+
+     if (WARN_ON_ONCE(folio_test_large(folio))) {
+             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 353

Quoted 1 line
virt/kvm/guest_memfd.c +1 -0
+

I don't think there is a large folio involved since the max/min folio order (stored in struct address_space::flags) should have been set to 0, meaning only order-0 is possible when the folio (page) is allocated and added to the page-cache. More details can be referred to AS_FOLIO_ORDER_MASK. It's unnecessary check but not harmful. Maybe a comment is needed to mention large folio isn't around yet, but double confirm.

Quoted 3 lines

The idea is to document the lack of hugepage support in code, but if you think it's necessary, I could add a comment.

Ok, I was actually nit-picky since we're at v9, which is close to integration, I guess. If another respin is needed, a comment wouldn't be harmful, but it's also perfectly fine without it :)

Quoted 20 lines
Quoted 14 lines

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

Quoted 7 lines
virt/kvm/guest_memfd.c +5 -0
+     if (!folio_test_uptodate(folio)) {
+             clear_highpage(folio_page(folio, 0));
+             kvm_gmem_mark_prepared(folio);
+     }
+

I must be missing some thing here. This chunk of code is out of sync to kvm_gmem_get_pfn(), where kvm_gmem_prepare_folio() and kvm_arch_gmem_prepare() are executed, and then PG_uptodate is set after that. In the latest ARM CCA series, kvm_arch_gmem_prepare() isn't used, but it would delegate the folio (page) with the prerequisite that the folio belongs to the private address space.

Quoted 5 lines

I guess that kvm_arch_gmem_prepare() is skipped here because we have the assumption that the folio belongs to the shared address space? However, this assumption isn't always true. We probably need to ensure the folio range is really belonging to the shared address space by poking kvm->mem_attr_array, which can be modified by VMM through ioctl KVM_SET_MEMORY_ATTRIBUTES.

Quoted 8 lines

This series only supports shared memory, and the idea is not to use the attributes to check. We ensure that only certain VM types can set the flag (e.g., VM_TYPE_DEFAULT and KVM_X86_SW_PROTECTED_VM).

In the patch series that builds on it, with in-place conversion between private and shared, we do add a check that the memory faulted in is in-fact shared.

Quoted 11 lines

Ok, thanks for your clarification. I plan to review that series, but not getting a chance yet. Right, it's sensible to limit the capability of modifying page's attribute (private vs shared) to the particular machine types since the whole feature (restricted mmap and in-place conversion) is applicable to particular machine types. I can understand KVM_X86_SW_PROTECTED_VM (similar to pKVM) needs the feature, but I don't understand why VM_TYPE_DEFAULT needs the feature. I guess we may want to use guest-memfd as to tmpfs or shmem, meaning all the address space associated with a guest-memfd is shared, but without the corresponding private space pointed by struct kvm_userspace_memory_region2 ::userspace_addr. Instead, the 'userspace_addr' will be mmap(guest-memfd) from VMM's perspective if I'm correct.

There are two reasons for why we're adding this feature for VM_TYPE_DEFAULT. The first is for VMMs like Firecracker to be able to run guests backed completely by guest_memfd [1]. Combined with Patrick's series for direct map removal in guest_memfd [2], this would allow running VMs that offer additional hardening against Spectre-like transient execution attacks. The other one is that, in the long term, the hope is for guest_memfd to become the main way for backing guests, regardless of the type of guest they represent.

If you're interested to find out more, we had a discussion about this a couple of weeks ago during the bi-weekly guest_memfd upstream call (May 1) [3].

Cheers,
/fuad

[1] https://github.com/firecracker-microvm/firecracker/tree/feature/secret-hiding
[2] https://lore.kernel.org/all/[email protected]/
[3] https://docs.google.com/document/d/1M6766BzdY1Lhk7LiR5IqVR8B8mG3cr-cxTxOrAosPOk/edit?tab=t.0#heading=h.jwwteecellpo

Quoted 26 lines

Thanks,
Gavin

Quoted 22 lines

Thanks,
/fuad

Quoted 18 lines
Quoted 4 lines
virt/kvm/guest_memfd.c +4 -0
+     vmf->page = folio_file_page(folio, vmf->pgoff);
+
+out_folio:
+     if (ret != VM_FAULT_LOCKED) {

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

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+             folio_unlock(folio);
+             folio_put(folio);
+     }
+
+out_filemap:
+     filemap_invalidate_unlock_shared(inode->i_mapping);
+
+     return ret;
+}
+

Thanks,
Gavin

#33 lore raw

Re: [PATCH v9 07/17] KVM: guest_memfd: Allow host to map guest_memfd() pages

In reply to Fuad Tabba
Quoted 8 lines
  struct kvm_create_guest_memfd {
  	__u64 size;
virt/kvm/Kconfig +0 -0
index 559c93ad90be..f4e469a62a60 100644

virt/kvm/Kconfig config HAVE_KVM_ARCH_GMEM_PREPARE · line 135

Quoted 8 lines
virt/kvm/Kconfig +5 -0
@@ -128,3 +128,8 @@ config HAVE_KVM_ARCH_GMEM_PREPARE
  config HAVE_KVM_ARCH_GMEM_INVALIDATE
         bool
         depends on KVM_GMEM
+
+config KVM_GMEM_SHARED_MEM
+       select KVM_GMEM
+       bool
+       prompt "Enables in-place shared memory for guest_memfd"

Not completely accurate :)

"Enable support for non-private ("shared") memory in guest_memfd" ?

Signature · 3 lines
Cheers,

David / dhildenb
author
#34 lore raw

PATCH v9 8/17 KVM: guest_memfd: Check that userspace_addr and fd+offset refer to same range

In reply to Fuad Tabba

From: Ackerley Tng <[email protected]>

On binding of a guest_memfd with a memslot, check that the slot's userspace_addr and the requested fd and offset refer to the same memory range.

This check is best-effort: nothing prevents userspace from later mapping other memory to the same provided in slot->userspace_addr and breaking guest operation.

---
 virt/kvm/guest_memfd.c | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)
virt/kvm/guest_memfd.c +34 -3
index 8e6d1866b55e..2f499021df66 100644
@@ -556,6 +556,32 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
 	return __kvm_gmem_create(kvm, size, flags);
 }
 
+static bool kvm_gmem_is_same_range(struct kvm *kvm,
+				   struct kvm_memory_slot *slot,
+				   struct file *file, loff_t offset)
+{
+	struct mm_struct *mm = kvm->mm;
+	loff_t userspace_addr_offset;
+	struct vm_area_struct *vma;
+	bool ret = false;
+
+	mmap_read_lock(mm);
+
+	vma = vma_lookup(mm, slot->userspace_addr);
+	if (!vma)
+		goto out;
+
+	if (vma->vm_file != file)
+		goto out;
+
+	userspace_addr_offset = slot->userspace_addr - vma->vm_start;
+	ret = userspace_addr_offset + (vma->vm_pgoff << PAGE_SHIFT) == offset;
+out:
+	mmap_read_unlock(mm);
+
+	return ret;
+}
+
 int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
 		  unsigned int fd, loff_t offset)
 {
@@ -585,9 +611,14 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
 	    offset + size > i_size_read(inode))
 		goto err;
 
-	if (kvm_gmem_supports_shared(inode) &&
-	    !kvm_arch_vm_supports_gmem_shared_mem(kvm))
-		goto err;
+	if (kvm_gmem_supports_shared(inode)) {
+		if (!kvm_arch_vm_supports_gmem_shared_mem(kvm))
+			goto err;
+
+		if (slot->userspace_addr &&
+		    !kvm_gmem_is_same_range(kvm, slot, file, offset))
+			goto err;
+	}
 
 	filemap_invalidate_lock(inode->i_mapping);
 
Signature · 1 line
2.49.0.1045.g170613ef41-goog
#35 lore raw

Re: [PATCH v9 08/17] KVM: guest_memfd: Check that userspace_addr and fd+offset refer to same range

Quoted 61 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

On binding of a guest_memfd with a memslot, check that the slot's userspace_addr and the requested fd and offset refer to the same memory range.

This check is best-effort: nothing prevents userspace from later mapping other memory to the same provided in slot->userspace_addr and breaking guest operation.

---
 virt/kvm/guest_memfd.c | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)
virt/kvm/guest_memfd.c +26 -0
index 8e6d1866b55e..2f499021df66 100644
@@ -556,6 +556,32 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
        return __kvm_gmem_create(kvm, size, flags);
 }
 
+static bool kvm_gmem_is_same_range(struct kvm *kvm,
+                                  struct kvm_memory_slot *slot,
+                                  struct file *file, loff_t offset)
+{
+       struct mm_struct *mm = kvm->mm;
+       loff_t userspace_addr_offset;
+       struct vm_area_struct *vma;
+       bool ret = false;
+
+       mmap_read_lock(mm);
+
+       vma = vma_lookup(mm, slot->userspace_addr);
+       if (!vma)
+               goto out;
+
+       if (vma->vm_file != file)
+               goto out;
+
+       userspace_addr_offset = slot->userspace_addr - vma->vm_start;
+       ret = userspace_addr_offset + (vma->vm_pgoff << PAGE_SHIFT) == offset;
+out:
+       mmap_read_unlock(mm);
+
+       return ret;
+}
+
 int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
                  unsigned int fd, loff_t offset)
 {
@@ -585,9 +611,14 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
            offset + size > i_size_read(inode))
                goto err;
 

virt/kvm/guest_memfd.c int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, · line 620

Quoted 10 lines
virt/kvm/guest_memfd.c +7 -3
-       if (kvm_gmem_supports_shared(inode) &&
-           !kvm_arch_vm_supports_gmem_shared_mem(kvm))
-               goto err;
+       if (kvm_gmem_supports_shared(inode)) {
+               if (!kvm_arch_vm_supports_gmem_shared_mem(kvm))
+                       goto err;
+
+               if (slot->userspace_addr &&
+                   !kvm_gmem_is_same_range(kvm, slot, file, offset))
+                       goto err;

This is very nit-picky, but I would rather this not be -EINVAL, maybe -EIO instead? Or maybe a pr_warn_once() and let the call proceed?

The userspace_addr we got isn't invalid per se, we're just trying to give a hint to the user that their VMAs (or the userspace address they gave us) are messed up. I don't really like lumping this in with truly invalid arguments.

Quoted 7 lines
virt/kvm/guest_memfd.c +1 -1
+       }
 
        filemap_invalidate_lock(inode->i_mapping);
 
--

2.49.0.1045.g170613ef41-goog

author
#36 lore raw

Re: [PATCH v9 08/17] KVM: guest_memfd: Check that userspace_addr and fd+offset refer to same range

Hi James,

Quoted 77 lines · James Houghton
Quoted 61 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

On binding of a guest_memfd with a memslot, check that the slot's userspace_addr and the requested fd and offset refer to the same memory range.

This check is best-effort: nothing prevents userspace from later mapping other memory to the same provided in slot->userspace_addr and breaking guest operation.

---
 virt/kvm/guest_memfd.c | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)
virt/kvm/guest_memfd.c +26 -0
index 8e6d1866b55e..2f499021df66 100644
@@ -556,6 +556,32 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
        return __kvm_gmem_create(kvm, size, flags);
 }
 
+static bool kvm_gmem_is_same_range(struct kvm *kvm,
+                                  struct kvm_memory_slot *slot,
+                                  struct file *file, loff_t offset)
+{
+       struct mm_struct *mm = kvm->mm;
+       loff_t userspace_addr_offset;
+       struct vm_area_struct *vma;
+       bool ret = false;
+
+       mmap_read_lock(mm);
+
+       vma = vma_lookup(mm, slot->userspace_addr);
+       if (!vma)
+               goto out;
+
+       if (vma->vm_file != file)
+               goto out;
+
+       userspace_addr_offset = slot->userspace_addr - vma->vm_start;
+       ret = userspace_addr_offset + (vma->vm_pgoff << PAGE_SHIFT) == offset;
+out:
+       mmap_read_unlock(mm);
+
+       return ret;
+}
+
 int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
                  unsigned int fd, loff_t offset)
 {
@@ -585,9 +611,14 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
            offset + size > i_size_read(inode))
                goto err;
 

virt/kvm/guest_memfd.c int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, · line 620

Quoted 10 lines
virt/kvm/guest_memfd.c +7 -3
-       if (kvm_gmem_supports_shared(inode) &&
-           !kvm_arch_vm_supports_gmem_shared_mem(kvm))
-               goto err;
+       if (kvm_gmem_supports_shared(inode)) {
+               if (!kvm_arch_vm_supports_gmem_shared_mem(kvm))
+                       goto err;
+
+               if (slot->userspace_addr &&
+                   !kvm_gmem_is_same_range(kvm, slot, file, offset))
+                       goto err;

This is very nit-picky, but I would rather this not be -EINVAL, maybe -EIO instead? Or maybe a pr_warn_once() and let the call proceed?

Quoted 4 lines

The userspace_addr we got isn't invalid per se, we're just trying to give a hint to the user that their VMAs (or the userspace address they gave us) are messed up. I don't really like lumping this in with truly invalid arguments.

I don't mind changing the return error, but I don't think that we should have a kernel warning (pr_warn_once) for something userspace can trigger.

It's not an IO error either. I think that this is an invalid argument (EINVAL). That said, other than opposing the idea of pr_warn, I am happy to change it.

Cheers,
/fuad

Quoted 7 lines
Quoted 7 lines
virt/kvm/guest_memfd.c +1 -1
+       }
 
        filemap_invalidate_lock(inode->i_mapping);
 
--

2.49.0.1045.g170613ef41-goog

#37 lore raw

Re: [PATCH v9 08/17] KVM: guest_memfd: Check that userspace_addr and fd+offset refer to same range

Quoted 44 lines · Fuad Tabba
Quoted 43 lines · James Houghton
Quoted 41 lines · Fuad Tabba
virt/kvm/guest_memfd.c +26 -0
index 8e6d1866b55e..2f499021df66 100644
@@ -556,6 +556,32 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
        return __kvm_gmem_create(kvm, size, flags);
 }
 
+static bool kvm_gmem_is_same_range(struct kvm *kvm,
+                                  struct kvm_memory_slot *slot,
+                                  struct file *file, loff_t offset)
+{
+       struct mm_struct *mm = kvm->mm;
+       loff_t userspace_addr_offset;
+       struct vm_area_struct *vma;
+       bool ret = false;
+
+       mmap_read_lock(mm);
+
+       vma = vma_lookup(mm, slot->userspace_addr);
+       if (!vma)
+               goto out;
+
+       if (vma->vm_file != file)
+               goto out;
+
+       userspace_addr_offset = slot->userspace_addr - vma->vm_start;
+       ret = userspace_addr_offset + (vma->vm_pgoff << PAGE_SHIFT) == offset;
+out:
+       mmap_read_unlock(mm);
+
+       return ret;
+}
+
 int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
                  unsigned int fd, loff_t offset)
 {
@@ -585,9 +611,14 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
            offset + size > i_size_read(inode))
                goto err;
 
Quoted 13 lines
Quoted 11 lines

virt/kvm/guest_memfd.c int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, · line 620

Quoted 10 lines
virt/kvm/guest_memfd.c +7 -3
-       if (kvm_gmem_supports_shared(inode) &&
-           !kvm_arch_vm_supports_gmem_shared_mem(kvm))
-               goto err;
+       if (kvm_gmem_supports_shared(inode)) {
+               if (!kvm_arch_vm_supports_gmem_shared_mem(kvm))
+                       goto err;
+
+               if (slot->userspace_addr &&
+                   !kvm_gmem_is_same_range(kvm, slot, file, offset))
+                       goto err;
Quoted 2 lines

This is very nit-picky, but I would rather this not be -EINVAL, maybe -EIO instead? Or maybe a pr_warn_once() and let the call proceed?

Or just omit the check entirely. The check isn't binding (ba-dump, ching!), because the mapping/VMA can change the instant mmap_read_unlock() is called.

Quoted 8 lines
Quoted 4 lines

The userspace_addr we got isn't invalid per se, we're just trying to give a hint to the user that their VMAs (or the userspace address they gave us) are messed up. I don't really like lumping this in with truly invalid arguments.

I don't mind changing the return error, but I don't think that we should have a kernel warning (pr_warn_once) for something userspace can trigger.

This isn't a WARN, e.g. won't trip panic_on_warn. In practice, it's not meaningfully different than pr_info(). That said, I agree that printing anything is a bad approach.

Quoted 2 lines

It's not an IO error either. I think that this is an invalid argument (EINVAL).

I agree with James, this isn't an invalid argument. Having the validity of an input hinge on the ordering between a KVM ioctl() and mmap() is quite odd. I know KVM arm64 does exactly this for KVM_SET_USER_MEMORY_REGION{,2}, but I don't love the semantics. And unlike that scenario, where e.g. MTE tags are verified again at fault-time, KVM won't re-check the VMA when accessing guest memory via the userspace mapping, e.g. through uaccess.

Unless I'm forgetting something, I'm leaning toward omitting the check entirely.

Quoted 1 line

That said, other than opposing the idea of pr_warn, I am happy to change it.

#38 lore raw

Re: [PATCH v9 08/17] KVM: guest_memfd: Check that userspace_addr and fd+offset refer to same range

Quoted 78 lines · Sean Christopherson
Quoted 44 lines · Fuad Tabba
Quoted 43 lines · James Houghton
Quoted 41 lines · Fuad Tabba
virt/kvm/guest_memfd.c +26 -0
index 8e6d1866b55e..2f499021df66 100644
@@ -556,6 +556,32 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
        return __kvm_gmem_create(kvm, size, flags);
 }
 
+static bool kvm_gmem_is_same_range(struct kvm *kvm,
+                                  struct kvm_memory_slot *slot,
+                                  struct file *file, loff_t offset)
+{
+       struct mm_struct *mm = kvm->mm;
+       loff_t userspace_addr_offset;
+       struct vm_area_struct *vma;
+       bool ret = false;
+
+       mmap_read_lock(mm);
+
+       vma = vma_lookup(mm, slot->userspace_addr);
+       if (!vma)
+               goto out;
+
+       if (vma->vm_file != file)
+               goto out;
+
+       userspace_addr_offset = slot->userspace_addr - vma->vm_start;
+       ret = userspace_addr_offset + (vma->vm_pgoff << PAGE_SHIFT) == offset;
+out:
+       mmap_read_unlock(mm);
+
+       return ret;
+}
+
 int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
                  unsigned int fd, loff_t offset)
 {
@@ -585,9 +611,14 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
            offset + size > i_size_read(inode))
                goto err;
 
Quoted 13 lines
Quoted 11 lines

virt/kvm/guest_memfd.c int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, · line 620

Quoted 10 lines
virt/kvm/guest_memfd.c +7 -3
-       if (kvm_gmem_supports_shared(inode) &&
-           !kvm_arch_vm_supports_gmem_shared_mem(kvm))
-               goto err;
+       if (kvm_gmem_supports_shared(inode)) {
+               if (!kvm_arch_vm_supports_gmem_shared_mem(kvm))
+                       goto err;
+
+               if (slot->userspace_addr &&
+                   !kvm_gmem_is_same_range(kvm, slot, file, offset))
+                       goto err;
Quoted 2 lines

This is very nit-picky, but I would rather this not be -EINVAL, maybe -EIO instead? Or maybe a pr_warn_once() and let the call proceed?

Or just omit the check entirely. The check isn't binding (ba-dump, ching!), because the mapping/VMA can change the instant mmap_read_unlock() is called.

Quoted 8 lines
Quoted 4 lines

The userspace_addr we got isn't invalid per se, we're just trying to give a hint to the user that their VMAs (or the userspace address they gave us) are messed up. I don't really like lumping this in with truly invalid arguments.

I don't mind changing the return error, but I don't think that we should have a kernel warning (pr_warn_once) for something userspace can trigger.

This isn't a WARN, e.g. won't trip panic_on_warn. In practice, it's not meaningfully different than pr_info(). That said, I agree that printing anything is a bad approach.

Quoted 2 lines

It's not an IO error either. I think that this is an invalid argument (EINVAL).

Quoted 9 lines

I agree with James, this isn't an invalid argument. Having the validity of an input hinge on the ordering between a KVM ioctl() and mmap() is quite odd. I know KVM arm64 does exactly this for KVM_SET_USER_MEMORY_REGION{,2}, but I don't love the semantics. And unlike that scenario, where e.g. MTE tags are verified again at fault-time, KVM won't re-check the VMA when accessing guest memory via the userspace mapping, e.g. through uaccess.

Unless I'm forgetting something, I'm leaning toward omitting the check entirely.

I'm good with dropping this patch. I might have misunderstood the conclusion of the guest_memfd call.

Quoted 1 line
Quoted 1 line

That said, other than opposing the idea of pr_warn, I am happy to change it.

#39 lore raw

Re: [PATCH v9 08/17] KVM: guest_memfd: Check that userspace_addr and fd+offset refer to same range

Quoted 47 lines · Ackerley Tng
Quoted 46 lines · Sean Christopherson
Quoted 5 lines · Fuad Tabba
Quoted 4 lines · James Houghton
Quoted 4 lines
virt/kvm/guest_memfd.c +0 -0
@@ -585,9 +611,14 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
            offset + size > i_size_read(inode))
                goto err;
 
Quoted 13 lines
Quoted 11 lines

virt/kvm/guest_memfd.c int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, · line 620

Quoted 10 lines
virt/kvm/guest_memfd.c +7 -3
-       if (kvm_gmem_supports_shared(inode) &&
-           !kvm_arch_vm_supports_gmem_shared_mem(kvm))
-               goto err;
+       if (kvm_gmem_supports_shared(inode)) {
+               if (!kvm_arch_vm_supports_gmem_shared_mem(kvm))
+                       goto err;
+
+               if (slot->userspace_addr &&
+                   !kvm_gmem_is_same_range(kvm, slot, file, offset))
+                       goto err;
Quoted 2 lines

This is very nit-picky, but I would rather this not be -EINVAL, maybe -EIO instead? Or maybe a pr_warn_once() and let the call proceed?

Or just omit the check entirely. The check isn't binding (ba-dump, ching!), because the mapping/VMA can change the instant mmap_read_unlock() is called.

Quoted 8 lines
Quoted 4 lines

The userspace_addr we got isn't invalid per se, we're just trying to give a hint to the user that their VMAs (or the userspace address they gave us) are messed up. I don't really like lumping this in with truly invalid arguments.

I don't mind changing the return error, but I don't think that we should have a kernel warning (pr_warn_once) for something userspace can trigger.

This isn't a WARN, e.g. won't trip panic_on_warn. In practice, it's not meaningfully different than pr_info(). That said, I agree that printing anything is a bad approach.

Quoted 2 lines

It's not an IO error either. I think that this is an invalid argument (EINVAL).

I agree with James, this isn't an invalid argument. Having the validity of an input hinge on the ordering between a KVM ioctl() and mmap() is quite odd. I know KVM arm64 does exactly this for KVM_SET_USER_MEMORY_REGION{,2}, but I don't love the semantics. And unlike that scenario, where e.g. MTE tags are verified again at fault-time, KVM won't re-check the VMA when accessing guest memory via the userspace mapping, e.g. through uaccess.

Quoted 5 lines
Quoted 2 lines

Unless I'm forgetting something, I'm leaning toward omitting the check entirely.

I'm good with dropping this patch. I might have misunderstood the conclusion of the guest_memfd call.

No, I don't think you misunderstood anything. It's just that sometimes opinions different when there's actual code, versus a verbal discussion. I.e. this sounds like a good idea, but when seeing the code and thinking through the effects, it's less appealing.

#40 lore raw

Re: [PATCH v9 08/17] KVM: guest_memfd: Check that userspace_addr and fd+offset refer to same range

In reply to Fuad Tabba
Quoted 37 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

On binding of a guest_memfd with a memslot, check that the slot's userspace_addr and the requested fd and offset refer to the same memory range.

This check is best-effort: nothing prevents userspace from later mapping other memory to the same provided in slot->userspace_addr and breaking guest operation.

---
  virt/kvm/guest_memfd.c | 37 ++++++++++++++++++++++++++++++++++---
  1 file changed, 34 insertions(+), 3 deletions(-)
virt/kvm/guest_memfd.c +10 -0
index 8e6d1866b55e..2f499021df66 100644
@@ -556,6 +556,32 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
  	return __kvm_gmem_create(kvm, size, flags);
  }
 
+static bool kvm_gmem_is_same_range(struct kvm *kvm,
+				   struct kvm_memory_slot *slot,
+				   struct file *file, loff_t offset)
+{
+	struct mm_struct *mm = kvm->mm;
+	loff_t userspace_addr_offset;
+	struct vm_area_struct *vma;
+	bool ret = false;
+
+	mmap_read_lock(mm);

virt/kvm/guest_memfd.c int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) · line 578

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+
+	vma = vma_lookup(mm, slot->userspace_addr);
+	if (!vma)
+		goto out;
+
+	if (vma->vm_file != file)
+		goto out;
+
+	userspace_addr_offset = slot->userspace_addr - vma->vm_start;
+	ret = userspace_addr_offset + (vma->vm_pgoff << PAGE_SHIFT) == offset;

You'd probably have to iterate over the whole range (which might span multiple VMAs), but reading the discussion, I'm fine with dropping this patch for now.

I think it's more important to document that thoroughly: what does it mean when we use GUEST_MEMFD_FLAG_SUPPORT_SHARED and then pass that fd in a memslot.

Skimming over patch #15, I assume this is properly documented in there.

Signature · 3 lines
Cheers,

David / dhildenb
author
#41 lore raw

PATCH v9 9/17 KVM: x86/mmu: Handle guest page faults for guest_memfd with shared memory

In reply to Fuad Tabba

From: Ackerley Tng <[email protected]>

For memslots backed by guest_memfd with shared mem support, the KVM MMU always faults-in pages from guest_memfd, and not from the userspace_addr. Towards this end, this patch also introduces a new guest_memfd flag, GUEST_MEMFD_FLAG_SUPPORT_SHARED, which indicates that the guest_memfd instance supports in-place shared memory.

This flag is only supported if the VM creating the guest_memfd instance belongs to certain types determined by architecture. Only non-CoCo VMs are permitted to use guest_memfd with shared mem, for now.

Function names have also been updated for accuracy - kvm_mem_is_private() returns true only when the current private/shared state (in the CoCo sense) of the memory is private, and returns false if the current state is shared explicitly or impicitly, e.g., belongs to a non-CoCo VM.

kvm_mmu_faultin_pfn_gmem() is updated to indicate that it can be used to fault in not just private memory, but more generally, from guest_memfd.

---
 arch/x86/kvm/mmu/mmu.c   | 33 ++++++++++++++++++---------------
 include/linux/kvm_host.h | 33 +++++++++++++++++++++++++++++++--
 virt/kvm/guest_memfd.c   | 17 +++++++++++++++++
 3 files changed, 66 insertions(+), 17 deletions(-)
arch/x86/kvm/mmu/mmu.c +18 -15
index 2b6376986f96..cfbb471f7c70 100644
@@ -4465,21 +4465,25 @@ static inline u8 kvm_max_level_for_order(int order)
 	return PG_LEVEL_4K;
 }
 
-static u8 kvm_max_private_mapping_level(struct kvm *kvm, kvm_pfn_t pfn,
-					u8 max_level, int gmem_order)
+static u8 kvm_max_level_for_fault_and_order(struct kvm *kvm,
+					    struct kvm_page_fault *fault,
+					    int order)
 {
-	u8 req_max_level;
+	u8 max_level = fault->max_level;
 
 	if (max_level == PG_LEVEL_4K)
 		return PG_LEVEL_4K;
 
-	max_level = min(kvm_max_level_for_order(gmem_order), max_level);
+	max_level = min(kvm_max_level_for_order(order), max_level);
 	if (max_level == PG_LEVEL_4K)
 		return PG_LEVEL_4K;
 
-	req_max_level = kvm_x86_call(private_max_mapping_level)(kvm, pfn);
-	if (req_max_level)
-		max_level = min(max_level, req_max_level);
+	if (fault->is_private) {
+		u8 level = kvm_x86_call(private_max_mapping_level)(kvm, fault->pfn);
+
+		if (level)
+			max_level = min(max_level, level);
+	}
 
 	return max_level;
 }
@@ -4491,10 +4495,10 @@ static void kvm_mmu_finish_page_fault(struct kvm_vcpu *vcpu,
 				 r == RET_PF_RETRY, fault->map_writable);
 }
 
-static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu,
-				       struct kvm_page_fault *fault)
+static int kvm_mmu_faultin_pfn_gmem(struct kvm_vcpu *vcpu,
+				    struct kvm_page_fault *fault)
 {
-	int max_order, r;
+	int gmem_order, r;
 
 	if (!kvm_slot_has_gmem(fault->slot)) {
 		kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
@@ -4502,15 +4506,14 @@ static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu,
 	}
 
 	r = kvm_gmem_get_pfn(vcpu->kvm, fault->slot, fault->gfn, &fault->pfn,
-			     &fault->refcounted_page, &max_order);
+			     &fault->refcounted_page, &gmem_order);
 	if (r) {
 		kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
 		return r;
 	}
 
 	fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY);
-	fault->max_level = kvm_max_private_mapping_level(vcpu->kvm, fault->pfn,
-							 fault->max_level, max_order);
+	fault->max_level = kvm_max_level_for_fault_and_order(vcpu->kvm, fault, gmem_order);
 
 	return RET_PF_CONTINUE;
 }
@@ -4520,8 +4523,8 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
 {
 	unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
-	if (fault->is_private)
-		return kvm_mmu_faultin_pfn_private(vcpu, fault);
+	if (fault->is_private || kvm_gmem_memslot_supports_shared(fault->slot))
+		return kvm_mmu_faultin_pfn_gmem(vcpu, fault);
 
 	foll |= FOLL_NOWAIT;
 	fault->pfn = __kvm_faultin_pfn(fault->slot, fault->gfn, foll,
include/linux/kvm_host.h +31 -2
index 2ec89c214978..de7b46ee1762 100644
@@ -2502,6 +2502,15 @@ static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
 		vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE;
 }
 
+#ifdef CONFIG_KVM_GMEM_SHARED_MEM
+bool kvm_gmem_memslot_supports_shared(const struct kvm_memory_slot *slot);
+#else
+static inline bool kvm_gmem_memslot_supports_shared(const struct kvm_memory_slot *slot)
+{
+	return false;
+}
+#endif /* CONFIG_KVM_GMEM_SHARED_MEM */
+
 #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
 static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn)
 {
@@ -2515,10 +2524,30 @@ bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm,
 bool kvm_arch_post_set_memory_attributes(struct kvm *kvm,
 					 struct kvm_gfn_range *range);
 
+/*
+ * Returns true if the given gfn's private/shared status (in the CoCo sense) is
+ * private.
+ *
+ * A return value of false indicates that the gfn is explicitly or implicity
+ * shared (i.e., non-CoCo VMs).
+ */
 static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
 {
-	return IS_ENABLED(CONFIG_KVM_GMEM) &&
-	       kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
+	struct kvm_memory_slot *slot;
+
+	if (!IS_ENABLED(CONFIG_KVM_GMEM))
+		return false;
+
+	slot = gfn_to_memslot(kvm, gfn);
+	if (kvm_slot_has_gmem(slot) && kvm_gmem_memslot_supports_shared(slot)) {
+		/*
+		 * For now, memslots only support in-place shared memory if the
+		 * host is allowed to mmap memory (i.e., non-Coco VMs).
+		 */
+		return false;
+	}
+
+	return kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
 }
 #else
 static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
virt/kvm/guest_memfd.c +17 -0
index 2f499021df66..fe0245335c96 100644
@@ -388,6 +388,23 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
 
 	return 0;
 }
+
+bool kvm_gmem_memslot_supports_shared(const struct kvm_memory_slot *slot)
+{
+	struct file *file;
+	bool ret;
+
+	file = kvm_gmem_get_file((struct kvm_memory_slot *)slot);
+	if (!file)
+		return false;
+
+	ret = kvm_gmem_supports_shared(file_inode(file));
+
+	fput(file);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(kvm_gmem_memslot_supports_shared);
+
 #else
 #define kvm_gmem_mmap NULL
 #endif /* CONFIG_KVM_GMEM_SHARED_MEM */
Signature · 1 line
2.49.0.1045.g170613ef41-goog
#42 lore raw

Re: [PATCH v9 09/17] KVM: x86/mmu: Handle guest page faults for guest_memfd with shared memory

Quoted 28 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

For memslots backed by guest_memfd with shared mem support, the KVM MMU always faults-in pages from guest_memfd, and not from the userspace_addr. Towards this end, this patch also introduces a new guest_memfd flag, GUEST_MEMFD_FLAG_SUPPORT_SHARED, which indicates that the guest_memfd instance supports in-place shared memory.

This flag is only supported if the VM creating the guest_memfd instance belongs to certain types determined by architecture. Only non-CoCo VMs are permitted to use guest_memfd with shared mem, for now.

Function names have also been updated for accuracy - kvm_mem_is_private() returns true only when the current private/shared state (in the CoCo sense) of the memory is private, and returns false if the current state is shared explicitly or impicitly, e.g., belongs to a non-CoCo VM.

kvm_mmu_faultin_pfn_gmem() is updated to indicate that it can be used to fault in not just private memory, but more generally, from guest_memfd.

---

[...]

include/linux/kvm_host.h bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, · line 2531

Quoted 13 lines
include/linux/kvm_host.h +6 -0
+
  #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
  static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn)
  {
@@ -2515,10 +2524,30 @@ bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm,
  bool kvm_arch_post_set_memory_attributes(struct kvm *kvm,
  					 struct kvm_gfn_range *range);
 
+/*
+ * Returns true if the given gfn's private/shared status (in the CoCo sense) is
+ * private.
+ *
+ * A return value of false indicates that the gfn is explicitly or implicity

s/implicity/implicitly/

Quoted 7 lines
include/linux/kvm_host.h +3 -2
+ * shared (i.e., non-CoCo VMs).
+ */
  static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
  {
-	return IS_ENABLED(CONFIG_KVM_GMEM) &&
-	       kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
+	struct kvm_memory_slot *slot;

include/linux/kvm_host.h bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, · line 2546

Quoted 10 lines
include/linux/kvm_host.h +10 -0
+
+	if (!IS_ENABLED(CONFIG_KVM_GMEM))
+		return false;
+
+	slot = gfn_to_memslot(kvm, gfn);
+	if (kvm_slot_has_gmem(slot) && kvm_gmem_memslot_supports_shared(slot)) {
+		/*
+		 * For now, memslots only support in-place shared memory if the
+		 * host is allowed to mmap memory (i.e., non-Coco VMs).
+		 */

Not accurate: there is no in-place conversion support in this series, because there is no such itnerface. So the reason is that all memory is shared for there VM types?

Quoted 19 lines
include/linux/kvm_host.h +4 -0
+		return false;
+	}
+
+	return kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
  }
  #else
  static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
virt/kvm/guest_memfd.c +4 -0
index 2f499021df66..fe0245335c96 100644
@@ -388,6 +388,23 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
 
  	return 0;
  }
+
+bool kvm_gmem_memslot_supports_shared(const struct kvm_memory_slot *slot)
+{
+	struct file *file;

virt/kvm/guest_memfd.c static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) · line 404

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+	bool ret;
+
+	file = kvm_gmem_get_file((struct kvm_memory_slot *)slot);
+	if (!file)
+		return false;
+
+	ret = kvm_gmem_supports_shared(file_inode(file));
+
+	fput(file);
+	return ret;

Would it make sense to cache that information in the memslot, to avoid the get/put?

We could simply cache when creating the memslot I guess.

As an alternative ... could we simple get/put when managing the memslot?

Signature · 3 lines
Cheers,

David / dhildenb
#43 lore raw

Re: [PATCH v9 09/17] KVM: x86/mmu: Handle guest page faults for guest_memfd with shared memory

Quoted 41 lines · David Hildenbrand
Quoted 28 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

For memslots backed by guest_memfd with shared mem support, the KVM MMU always faults-in pages from guest_memfd, and not from the userspace_addr. Towards this end, this patch also introduces a new guest_memfd flag, GUEST_MEMFD_FLAG_SUPPORT_SHARED, which indicates that the guest_memfd instance supports in-place shared memory.

This flag is only supported if the VM creating the guest_memfd instance belongs to certain types determined by architecture. Only non-CoCo VMs are permitted to use guest_memfd with shared mem, for now.

Function names have also been updated for accuracy - kvm_mem_is_private() returns true only when the current private/shared state (in the CoCo sense) of the memory is private, and returns false if the current state is shared explicitly or impicitly, e.g., belongs to a non-CoCo VM.

kvm_mmu_faultin_pfn_gmem() is updated to indicate that it can be used to fault in not just private memory, but more generally, from guest_memfd.

---

[...]

Quoted 8 lines
include/linux/kvm_host.h +1 -0
+
  #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
  static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn)
  {
@@ -2515,10 +2524,30 @@ bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm,
  bool kvm_arch_post_set_memory_attributes(struct kvm *kvm,
  					 struct kvm_gfn_range *range);
 
Quoted 8 lines

include/linux/kvm_host.h bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, · line 2531

Quoted 5 lines
include/linux/kvm_host.h +5 -0
+/*
+ * Returns true if the given gfn's private/shared status (in the CoCo sense) is
+ * private.
+ *
+ * A return value of false indicates that the gfn is explicitly or implicity

s/implicity/implicitly/

Thanks!

Quoted 13 lines
Quoted 13 lines
include/linux/kvm_host.h +9 -2
+ * shared (i.e., non-CoCo VMs).
+ */
  static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
  {
-	return IS_ENABLED(CONFIG_KVM_GMEM) &&
-	       kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
+	struct kvm_memory_slot *slot;
+
+	if (!IS_ENABLED(CONFIG_KVM_GMEM))
+		return false;
+
+	slot = gfn_to_memslot(kvm, gfn);
+	if (kvm_slot_has_gmem(slot) && kvm_gmem_memslot_supports_shared(slot)) {
Quoted 9 lines

include/linux/kvm_host.h bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, · line 2546

Quoted 4 lines
include/linux/kvm_host.h +4 -0
+		/*
+		 * For now, memslots only support in-place shared memory if the
+		 * host is allowed to mmap memory (i.e., non-Coco VMs).
+		 */

Not accurate: there is no in-place conversion support in this series, because there is no such itnerface. So the reason is that all memory is shared for there VM types?

True that there's no in-place conversion yet.

In this patch series, guest_memfd memslots support shared memory only for specific VM types (on x86, that would be KVM_X86_DEFAULT_VM and KVM_X86_SW_PROTECTED_VMs).

How about this wording:

Without conversion support, if the guest_memfd memslot supports shared memory, all memory must be used as not private (implicitly shared).

Quoted 33 lines
Quoted 19 lines
include/linux/kvm_host.h +4 -0
+		return false;
+	}
+
+	return kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
  }
  #else
  static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
virt/kvm/guest_memfd.c +4 -0
index 2f499021df66..fe0245335c96 100644
@@ -388,6 +388,23 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
 
  	return 0;
  }
+
+bool kvm_gmem_memslot_supports_shared(const struct kvm_memory_slot *slot)
+{
+	struct file *file;

virt/kvm/guest_memfd.c static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) · line 404

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+	bool ret;
+
+	file = kvm_gmem_get_file((struct kvm_memory_slot *)slot);
+	if (!file)
+		return false;
+
+	ret = kvm_gmem_supports_shared(file_inode(file));
+
+	fput(file);
+	return ret;

Would it make sense to cache that information in the memslot, to avoid the get/put?

Quoted 2 lines

We could simply cache when creating the memslot I guess.

When I wrote it I was assuming that to ensure correctness we should check with guest memfd, like what if someone closed the gmem file in the middle of the fault path?

But I guess after the discussion at the last call, since the faulting process is long and racy, if this check passed and we go to guest memfd and the file was closed, it would just fail so I guess caching is fine.

Quoted 1 line

As an alternative ... could we simple get/put when managing the memslot?

What does a simple get/put mean here?

#44 lore raw

Re: [PATCH v9 09/17] KVM: x86/mmu: Handle guest page faults for guest_memfd with shared memory

Quoted 26 lines
Quoted 14 lines
Quoted 14 lines

+ * shared (i.e., non-CoCo VMs).
+ */
static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) {

include/linux/kvm_host.h +7 -2
-	return IS_ENABLED(CONFIG_KVM_GMEM) &&
-	       kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
+	struct kvm_memory_slot *slot;
+
+	if (!IS_ENABLED(CONFIG_KVM_GMEM))
+		return false;
+
+	slot = gfn_to_memslot(kvm, gfn);
+	if (kvm_slot_has_gmem(slot) && kvm_gmem_memslot_supports_shared(slot)) {
Quoted 9 lines

include/linux/kvm_host.h bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, · line 2546

Quoted 4 lines
include/linux/kvm_host.h +4 -0
+		/*
+		 * For now, memslots only support in-place shared memory if the
+		 * host is allowed to mmap memory (i.e., non-Coco VMs).
+		 */

Not accurate: there is no in-place conversion support in this series, because there is no such itnerface. So the reason is that all memory is shared for there VM types?

True that there's no in-place conversion yet.

Quoted 9 lines

In this patch series, guest_memfd memslots support shared memory only for specific VM types (on x86, that would be KVM_X86_DEFAULT_VM and KVM_X86_SW_PROTECTED_VMs).

How about this wording:

Without conversion support, if the guest_memfd memslot supports shared memory, all memory must be used as not private (implicitly shared).

LGTM

Quoted 36 lines
Quoted 33 lines
Quoted 19 lines
include/linux/kvm_host.h +4 -0
+		return false;
+	}
+
+	return kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
   }
   #else
   static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
virt/kvm/guest_memfd.c +4 -0
index 2f499021df66..fe0245335c96 100644
@@ -388,6 +388,23 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
 
   	return 0;
   }
+
+bool kvm_gmem_memslot_supports_shared(const struct kvm_memory_slot *slot)
+{
+	struct file *file;

virt/kvm/guest_memfd.c static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) · line 404

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+	bool ret;
+
+	file = kvm_gmem_get_file((struct kvm_memory_slot *)slot);
+	if (!file)
+		return false;
+
+	ret = kvm_gmem_supports_shared(file_inode(file));
+
+	fput(file);
+	return ret;

Would it make sense to cache that information in the memslot, to avoid the get/put?

Quoted 2 lines

We could simply cache when creating the memslot I guess.

Quoted 7 lines

When I wrote it I was assuming that to ensure correctness we should check with guest memfd, like what if someone closed the gmem file in the middle of the fault path?

But I guess after the discussion at the last call, since the faulting process is long and racy, if this check passed and we go to guest memfd and the file was closed, it would just fail so I guess caching is fine.

Yes, that would be my assumption. I mean, we also msut make sure that if the user does something stupid like that, that we won't trigger other undesired code paths (like, suddenly the guest_memfd being !shared).

Quoted 4 lines
Quoted 2 lines

As an alternative ... could we simple get/put when managing the memslot?

What does a simple get/put mean here?

s/simple/simply/

So when we create the memslot, we'd perform the get, and when we destroy the memslot, we'd do the put.

Just an idea.

Signature · 3 lines
Cheers,

David / dhildenb
author
#45 lore raw

Re: [PATCH v9 09/17] KVM: x86/mmu: Handle guest page faults for guest_memfd with shared memory

Hi David,

Quoted 90 lines · David Hildenbrand
Quoted 27 lines
Quoted 15 lines
Quoted 15 lines

+ * shared (i.e., non-CoCo VMs).
+ */
static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) {

include/linux/kvm_host.h +7 -2
-   return IS_ENABLED(CONFIG_KVM_GMEM) &&
-          kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
+   struct kvm_memory_slot *slot;
+
+   if (!IS_ENABLED(CONFIG_KVM_GMEM))
+           return false;
+
+   slot = gfn_to_memslot(kvm, gfn);
+   if (kvm_slot_has_gmem(slot) && kvm_gmem_memslot_supports_shared(slot)) {
Quoted 9 lines

include/linux/kvm_host.h bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, · line 2546

Quoted 4 lines
include/linux/kvm_host.h +4 -0
+           /*
+            * For now, memslots only support in-place shared memory if the
+            * host is allowed to mmap memory (i.e., non-Coco VMs).
+            */

Not accurate: there is no in-place conversion support in this series, because there is no such itnerface. So the reason is that all memory is shared for there VM types?

True that there's no in-place conversion yet.

Quoted 9 lines

In this patch series, guest_memfd memslots support shared memory only for specific VM types (on x86, that would be KVM_X86_DEFAULT_VM and KVM_X86_SW_PROTECTED_VMs).

How about this wording:

Without conversion support, if the guest_memfd memslot supports shared memory, all memory must be used as not private (implicitly shared).

LGTM

Quoted 36 lines
Quoted 33 lines
Quoted 19 lines
include/linux/kvm_host.h +4 -0
+           return false;
+   }
+
+   return kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
   }
   #else
   static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
virt/kvm/guest_memfd.c +4 -0
index 2f499021df66..fe0245335c96 100644
@@ -388,6 +388,23 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
 
    return 0;
   }
+
+bool kvm_gmem_memslot_supports_shared(const struct kvm_memory_slot *slot)
+{
+   struct file *file;

virt/kvm/guest_memfd.c static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) · line 404

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+   bool ret;
+
+   file = kvm_gmem_get_file((struct kvm_memory_slot *)slot);
+   if (!file)
+           return false;
+
+   ret = kvm_gmem_supports_shared(file_inode(file));
+
+   fput(file);
+   return ret;

Would it make sense to cache that information in the memslot, to avoid the get/put?

Quoted 2 lines

We could simply cache when creating the memslot I guess.

Quoted 7 lines

When I wrote it I was assuming that to ensure correctness we should check with guest memfd, like what if someone closed the gmem file in the middle of the fault path?

But I guess after the discussion at the last call, since the faulting process is long and racy, if this check passed and we go to guest memfd and the file was closed, it would just fail so I guess caching is fine.

Yes, that would be my assumption. I mean, we also msut make sure that if the user does something stupid like that, that we won't trigger other undesired code paths (like, suddenly the guest_memfd being !shared).

Quoted 3 lines
Quoted 2 lines

As an alternative ... could we simple get/put when managing the memslot?

Quoted 8 lines
Quoted 1 line

What does a simple get/put mean here?

s/simple/simply/

So when we create the memslot, we'd perform the get, and when we destroy the memslot, we'd do the put.

Just an idea.

I'm not sure we can do that. The comment in kvm_gmem_bind() on dropping the reference to the file explains why:
https://elixir.bootlin.com/linux/v6.14.7/source/virt/kvm/guest_memfd.c#L526

I think the best thing is to track whether a slot supports shared memory inside struct kvm_memory_slot::struct gmem.

Thanks,
/fuad

Quoted 5 lines

--
Cheers,

David / dhildenb

#46 lore raw

Re: [PATCH v9 09/17] KVM: x86/mmu: Handle guest page faults for guest_memfd with shared memory

Quoted 97 lines · Fuad Tabba

Hi David,

Quoted 94 lines · David Hildenbrand
Quoted 27 lines
Quoted 15 lines
Quoted 15 lines

+ * shared (i.e., non-CoCo VMs).
+ */
static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) {

include/linux/kvm_host.h +7 -2
-   return IS_ENABLED(CONFIG_KVM_GMEM) &&
-          kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
+   struct kvm_memory_slot *slot;
+
+   if (!IS_ENABLED(CONFIG_KVM_GMEM))
+           return false;
+
+   slot = gfn_to_memslot(kvm, gfn);
+   if (kvm_slot_has_gmem(slot) && kvm_gmem_memslot_supports_shared(slot)) {
Quoted 9 lines

include/linux/kvm_host.h bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, · line 2546

Quoted 4 lines
include/linux/kvm_host.h +4 -0
+           /*
+            * For now, memslots only support in-place shared memory if the
+            * host is allowed to mmap memory (i.e., non-Coco VMs).
+            */

Not accurate: there is no in-place conversion support in this series, because there is no such itnerface. So the reason is that all memory is shared for there VM types?

True that there's no in-place conversion yet.

Quoted 9 lines

In this patch series, guest_memfd memslots support shared memory only for specific VM types (on x86, that would be KVM_X86_DEFAULT_VM and KVM_X86_SW_PROTECTED_VMs).

How about this wording:

Without conversion support, if the guest_memfd memslot supports shared memory, all memory must be used as not private (implicitly shared).

LGTM

Quoted 36 lines
Quoted 33 lines
Quoted 19 lines
include/linux/kvm_host.h +4 -0
+           return false;
+   }
+
+   return kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
    }
    #else
    static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
virt/kvm/guest_memfd.c +4 -0
index 2f499021df66..fe0245335c96 100644
@@ -388,6 +388,23 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
 
     return 0;
    }
+
+bool kvm_gmem_memslot_supports_shared(const struct kvm_memory_slot *slot)
+{
+   struct file *file;

virt/kvm/guest_memfd.c static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) · line 404

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+   bool ret;
+
+   file = kvm_gmem_get_file((struct kvm_memory_slot *)slot);
+   if (!file)
+           return false;
+
+   ret = kvm_gmem_supports_shared(file_inode(file));
+
+   fput(file);
+   return ret;

Would it make sense to cache that information in the memslot, to avoid the get/put?

Quoted 2 lines

We could simply cache when creating the memslot I guess.

Quoted 7 lines

When I wrote it I was assuming that to ensure correctness we should check with guest memfd, like what if someone closed the gmem file in the middle of the fault path?

But I guess after the discussion at the last call, since the faulting process is long and racy, if this check passed and we go to guest memfd and the file was closed, it would just fail so I guess caching is fine.

Yes, that would be my assumption. I mean, we also msut make sure that if the user does something stupid like that, that we won't trigger other undesired code paths (like, suddenly the guest_memfd being !shared).

Quoted 4 lines
Quoted 2 lines

As an alternative ... could we simple get/put when managing the memslot?

What does a simple get/put mean here?

s/simple/simply/

Quoted 8 lines
Quoted 4 lines

So when we create the memslot, we'd perform the get, and when we destroy the memslot, we'd do the put.

Just an idea.

I'm not sure we can do that. The comment in kvm_gmem_bind() on dropping the reference to the file explains why:
https://elixir.bootlin.com/linux/v6.14.7/source/virt/kvm/guest_memfd.c#L526

Right, although it is rather suboptimal; we have to constantly get/put the file, even in kvm_gmem_get_pfn() right now.

Repeatedly two atomics and a bunch of checks ... for something a sane use case should never trigger.

Anyhow, that's probably something to optimize also for kvm_gmem_get_pfn() later on? Of course, the caching here is rather straight forward.

Signature · 3 lines
Cheers,

David / dhildenb
author
#47 lore raw

Re: [PATCH v9 09/17] KVM: x86/mmu: Handle guest page faults for guest_memfd with shared memory

Quoted 111 lines · David Hildenbrand
Quoted 97 lines · Fuad Tabba

Hi David,

Quoted 94 lines · David Hildenbrand
Quoted 27 lines
Quoted 15 lines
Quoted 15 lines

+ * shared (i.e., non-CoCo VMs).
+ */
static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) {

include/linux/kvm_host.h +7 -2
-   return IS_ENABLED(CONFIG_KVM_GMEM) &&
-          kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
+   struct kvm_memory_slot *slot;
+
+   if (!IS_ENABLED(CONFIG_KVM_GMEM))
+           return false;
+
+   slot = gfn_to_memslot(kvm, gfn);
+   if (kvm_slot_has_gmem(slot) && kvm_gmem_memslot_supports_shared(slot)) {
Quoted 9 lines

include/linux/kvm_host.h bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, · line 2546

Quoted 4 lines
include/linux/kvm_host.h +4 -0
+           /*
+            * For now, memslots only support in-place shared memory if the
+            * host is allowed to mmap memory (i.e., non-Coco VMs).
+            */

Not accurate: there is no in-place conversion support in this series, because there is no such itnerface. So the reason is that all memory is shared for there VM types?

True that there's no in-place conversion yet.

Quoted 9 lines

In this patch series, guest_memfd memslots support shared memory only for specific VM types (on x86, that would be KVM_X86_DEFAULT_VM and KVM_X86_SW_PROTECTED_VMs).

How about this wording:

Without conversion support, if the guest_memfd memslot supports shared memory, all memory must be used as not private (implicitly shared).

LGTM

Quoted 36 lines
Quoted 33 lines
Quoted 19 lines
include/linux/kvm_host.h +4 -0
+           return false;
+   }
+
+   return kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
    }
    #else
    static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
virt/kvm/guest_memfd.c +4 -0
index 2f499021df66..fe0245335c96 100644
@@ -388,6 +388,23 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
 
     return 0;
    }
+
+bool kvm_gmem_memslot_supports_shared(const struct kvm_memory_slot *slot)
+{
+   struct file *file;

virt/kvm/guest_memfd.c static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) · line 404

Quoted 10 lines
virt/kvm/guest_memfd.c +10 -0
+   bool ret;
+
+   file = kvm_gmem_get_file((struct kvm_memory_slot *)slot);
+   if (!file)
+           return false;
+
+   ret = kvm_gmem_supports_shared(file_inode(file));
+
+   fput(file);
+   return ret;

Would it make sense to cache that information in the memslot, to avoid the get/put?

Quoted 2 lines

We could simply cache when creating the memslot I guess.

Quoted 7 lines

When I wrote it I was assuming that to ensure correctness we should check with guest memfd, like what if someone closed the gmem file in the middle of the fault path?

But I guess after the discussion at the last call, since the faulting process is long and racy, if this check passed and we go to guest memfd and the file was closed, it would just fail so I guess caching is fine.

Yes, that would be my assumption. I mean, we also msut make sure that if the user does something stupid like that, that we won't trigger other undesired code paths (like, suddenly the guest_memfd being !shared).

Quoted 4 lines
Quoted 2 lines

As an alternative ... could we simple get/put when managing the memslot?

What does a simple get/put mean here?

s/simple/simply/

Quoted 8 lines
Quoted 4 lines

So when we create the memslot, we'd perform the get, and when we destroy the memslot, we'd do the put.

Just an idea.

I'm not sure we can do that. The comment in kvm_gmem_bind() on dropping the reference to the file explains why:
https://elixir.bootlin.com/linux/v6.14.7/source/virt/kvm/guest_memfd.c#L526

Right, although it is rather suboptimal; we have to constantly get/put the file, even in kvm_gmem_get_pfn() right now.

Quoted 6 lines

Repeatedly two atomics and a bunch of checks ... for something a sane use case should never trigger.

Anyhow, that's probably something to optimize also for kvm_gmem_get_pfn() later on? Of course, the caching here is rather straight forward.

Done.

Thanks,
/fuad

Quoted 5 lines

--
Cheers,

David / dhildenb

author
#48 lore raw

PATCH v9 10/17 KVM: x86: Compute max_mapping_level with input from guest_memfd

In reply to Fuad Tabba

From: Ackerley Tng <[email protected]>

This patch adds kvm_gmem_max_mapping_level(), which always returns PG_LEVEL_4K since guest_memfd only supports 4K pages for now.

When guest_memfd supports shared memory, max_mapping_level (especially when recovering huge pages - see call to __kvm_mmu_max_mapping_level() from recover_huge_pages_range()) should take input from guest_memfd.

Input from guest_memfd should be taken in these cases:

+ if the memslot supports shared memory (guest_memfd is used for shared memory, or in future both shared and private memory) or
+ if the memslot is only used for private memory and that gfn is private.

If the memslot doesn't use guest_memfd, figure out the max_mapping_level using the host page tables like before.

This patch also refactors and inlines the other call to __kvm_mmu_max_mapping_level().

In kvm_mmu_hugepage_adjust(), guest_memfd's input is already provided (if applicable) in fault->max_level. Hence, there is no need to query guest_memfd.

lpage_info is queried like before, and then if the fault is not from guest_memfd, adjust fault->req_level based on input from host page tables.

---
 arch/x86/kvm/mmu/mmu.c   | 92 ++++++++++++++++++++++++++--------------
 include/linux/kvm_host.h |  7 +++
 virt/kvm/guest_memfd.c   | 12 ++++++
 3 files changed, 79 insertions(+), 32 deletions(-)
arch/x86/kvm/mmu/mmu.c +60 -32
index cfbb471f7c70..9e0bc8114859 100644
@@ -3256,12 +3256,11 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
 	return level;
 }
 
-static int __kvm_mmu_max_mapping_level(struct kvm *kvm,
-				       const struct kvm_memory_slot *slot,
-				       gfn_t gfn, int max_level, bool is_private)
+static int kvm_lpage_info_max_mapping_level(struct kvm *kvm,
+					    const struct kvm_memory_slot *slot,
+					    gfn_t gfn, int max_level)
 {
 	struct kvm_lpage_info *linfo;
-	int host_level;
 
 	max_level = min(max_level, max_huge_page_level);
 	for ( ; max_level > PG_LEVEL_4K; max_level--) {
@@ -3270,23 +3269,61 @@ static int __kvm_mmu_max_mapping_level(struct kvm *kvm,
 			break;
 	}
 
-	if (is_private)
-		return max_level;
+	return max_level;
+}
+
+static inline u8 kvm_max_level_for_order(int order)
+{
+	BUILD_BUG_ON(KVM_MAX_HUGEPAGE_LEVEL > PG_LEVEL_1G);
+
+	KVM_MMU_WARN_ON(order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G) &&
+			order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M) &&
+			order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K));
+
+	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G))
+		return PG_LEVEL_1G;
+
+	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M))
+		return PG_LEVEL_2M;
+
+	return PG_LEVEL_4K;
+}
+
+static inline int kvm_gmem_max_mapping_level(const struct kvm_memory_slot *slot,
+					     gfn_t gfn, int max_level)
+{
+	int max_order;
 
 	if (max_level == PG_LEVEL_4K)
 		return PG_LEVEL_4K;
 
-	host_level = host_pfn_mapping_level(kvm, gfn, slot);
-	return min(host_level, max_level);
+	max_order = kvm_gmem_mapping_order(slot, gfn);
+	return min(max_level, kvm_max_level_for_order(max_order));
 }
 
 int kvm_mmu_max_mapping_level(struct kvm *kvm,
 			      const struct kvm_memory_slot *slot, gfn_t gfn)
 {
-	bool is_private = kvm_slot_has_gmem(slot) &&
-			  kvm_mem_is_private(kvm, gfn);
+	int max_level;
+
+	max_level = kvm_lpage_info_max_mapping_level(kvm, slot, gfn, PG_LEVEL_NUM);
+	if (max_level == PG_LEVEL_4K)
+		return PG_LEVEL_4K;
 
-	return __kvm_mmu_max_mapping_level(kvm, slot, gfn, PG_LEVEL_NUM, is_private);
+	if (kvm_slot_has_gmem(slot) &&
+	    (kvm_gmem_memslot_supports_shared(slot) ||
+	     kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE)) {
+		return kvm_gmem_max_mapping_level(slot, gfn, max_level);
+	}
+
+	return min(max_level, host_pfn_mapping_level(kvm, gfn, slot));
+}
+
+static inline bool fault_from_gmem(struct kvm_page_fault *fault)
+{
+	return fault->is_private ||
+	       (kvm_slot_has_gmem(fault->slot) &&
+		kvm_gmem_memslot_supports_shared(fault->slot));
 }
 
 void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
@@ -3309,12 +3346,20 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
 	 * Enforce the iTLB multihit workaround after capturing the requested
 	 * level, which will be used to do precise, accurate accounting.
 	 */
-	fault->req_level = __kvm_mmu_max_mapping_level(vcpu->kvm, slot,
-						       fault->gfn, fault->max_level,
-						       fault->is_private);
+	fault->req_level = kvm_lpage_info_max_mapping_level(vcpu->kvm, slot,
+							    fault->gfn, fault->max_level);
 	if (fault->req_level == PG_LEVEL_4K || fault->huge_page_disallowed)
 		return;
 
+	if (!fault_from_gmem(fault)) {
+		int host_level;
+
+		host_level = host_pfn_mapping_level(vcpu->kvm, fault->gfn, slot);
+		fault->req_level = min(fault->req_level, host_level);
+		if (fault->req_level == PG_LEVEL_4K)
+			return;
+	}
+
 	/*
 	 * mmu_invalidate_retry() was successful and mmu_lock is held, so
 	 * the pmd can't be split from under us.
@@ -4448,23 +4493,6 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
 		vcpu->stat.pf_fixed++;
 }
 
-static inline u8 kvm_max_level_for_order(int order)
-{
-	BUILD_BUG_ON(KVM_MAX_HUGEPAGE_LEVEL > PG_LEVEL_1G);
-
-	KVM_MMU_WARN_ON(order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G) &&
-			order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M) &&
-			order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K));
-
-	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G))
-		return PG_LEVEL_1G;
-
-	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M))
-		return PG_LEVEL_2M;
-
-	return PG_LEVEL_4K;
-}
-
 static u8 kvm_max_level_for_fault_and_order(struct kvm *kvm,
 					    struct kvm_page_fault *fault,
 					    int order)
@@ -4523,7 +4551,7 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
 {
 	unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
-	if (fault->is_private || kvm_gmem_memslot_supports_shared(fault->slot))
+	if (fault_from_gmem(fault))
 		return kvm_mmu_faultin_pfn_gmem(vcpu, fault);
 
 	foll |= FOLL_NOWAIT;
include/linux/kvm_host.h +7 -0
index de7b46ee1762..f9bb025327c3 100644
@@ -2560,6 +2560,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
 int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
 		     gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
 		     int *max_order);
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn);
 #else
 static inline int kvm_gmem_get_pfn(struct kvm *kvm,
 				   struct kvm_memory_slot *slot, gfn_t gfn,
@@ -2569,6 +2570,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
 	KVM_BUG_ON(1, kvm);
 	return -EIO;
 }
+static inline int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot,
+					 gfn_t gfn)
+{
+	BUG();
+	return 0;
+}
 #endif /* CONFIG_KVM_GMEM */
 
 #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE
virt/kvm/guest_memfd.c +12 -0
index fe0245335c96..b8e247063b20 100644
@@ -774,6 +774,18 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
 }
 EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn);
 
+/**
+ * Returns the mapping order for this @gfn in @slot.
+ *
+ * This is equal to max_order that would be returned if kvm_gmem_get_pfn() were
+ * called now.
+ */
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn)
+{
+	return 0;
+}
+EXPORT_SYMBOL_GPL(kvm_gmem_mapping_order);
+
 #ifdef CONFIG_KVM_GENERIC_GMEM_POPULATE
 long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long npages,
 		       kvm_gmem_populate_cb post_populate, void *opaque)
Signature · 1 line
2.49.0.1045.g170613ef41-goog
#49 lore raw

Re: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd

Quoted 215 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

This patch adds kvm_gmem_max_mapping_level(), which always returns PG_LEVEL_4K since guest_memfd only supports 4K pages for now.

When guest_memfd supports shared memory, max_mapping_level (especially when recovering huge pages - see call to __kvm_mmu_max_mapping_level() from recover_huge_pages_range()) should take input from guest_memfd.

Input from guest_memfd should be taken in these cases:

+ if the memslot supports shared memory (guest_memfd is used for shared memory, or in future both shared and private memory) or
+ if the memslot is only used for private memory and that gfn is private.

If the memslot doesn't use guest_memfd, figure out the max_mapping_level using the host page tables like before.

This patch also refactors and inlines the other call to __kvm_mmu_max_mapping_level().

In kvm_mmu_hugepage_adjust(), guest_memfd's input is already provided (if applicable) in fault->max_level. Hence, there is no need to query guest_memfd.

lpage_info is queried like before, and then if the fault is not from guest_memfd, adjust fault->req_level based on input from host page tables.

---
 arch/x86/kvm/mmu/mmu.c   | 92 ++++++++++++++++++++++++++--------------
 include/linux/kvm_host.h |  7 +++
 virt/kvm/guest_memfd.c   | 12 ++++++
 3 files changed, 79 insertions(+), 32 deletions(-)
arch/x86/kvm/mmu/mmu.c +60 -32
index cfbb471f7c70..9e0bc8114859 100644
@@ -3256,12 +3256,11 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
 	return level;
 }
 
-static int __kvm_mmu_max_mapping_level(struct kvm *kvm,
-				       const struct kvm_memory_slot *slot,
-				       gfn_t gfn, int max_level, bool is_private)
+static int kvm_lpage_info_max_mapping_level(struct kvm *kvm,
+					    const struct kvm_memory_slot *slot,
+					    gfn_t gfn, int max_level)
 {
 	struct kvm_lpage_info *linfo;
-	int host_level;
 
 	max_level = min(max_level, max_huge_page_level);
 	for ( ; max_level > PG_LEVEL_4K; max_level--) {
@@ -3270,23 +3269,61 @@ static int __kvm_mmu_max_mapping_level(struct kvm *kvm,
 			break;
 	}
 
-	if (is_private)
-		return max_level;
+	return max_level;
+}
+
+static inline u8 kvm_max_level_for_order(int order)
+{
+	BUILD_BUG_ON(KVM_MAX_HUGEPAGE_LEVEL > PG_LEVEL_1G);
+
+	KVM_MMU_WARN_ON(order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G) &&
+			order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M) &&
+			order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K));
+
+	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G))
+		return PG_LEVEL_1G;
+
+	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M))
+		return PG_LEVEL_2M;
+
+	return PG_LEVEL_4K;
+}
+
+static inline int kvm_gmem_max_mapping_level(const struct kvm_memory_slot *slot,
+					     gfn_t gfn, int max_level)
+{
+	int max_order;
 
 	if (max_level == PG_LEVEL_4K)
 		return PG_LEVEL_4K;
 
-	host_level = host_pfn_mapping_level(kvm, gfn, slot);
-	return min(host_level, max_level);
+	max_order = kvm_gmem_mapping_order(slot, gfn);
+	return min(max_level, kvm_max_level_for_order(max_order));
 }
 
 int kvm_mmu_max_mapping_level(struct kvm *kvm,
 			      const struct kvm_memory_slot *slot, gfn_t gfn)
 {
-	bool is_private = kvm_slot_has_gmem(slot) &&
-			  kvm_mem_is_private(kvm, gfn);
+	int max_level;
+
+	max_level = kvm_lpage_info_max_mapping_level(kvm, slot, gfn, PG_LEVEL_NUM);
+	if (max_level == PG_LEVEL_4K)
+		return PG_LEVEL_4K;
 
-	return __kvm_mmu_max_mapping_level(kvm, slot, gfn, PG_LEVEL_NUM, is_private);
+	if (kvm_slot_has_gmem(slot) &&
+	    (kvm_gmem_memslot_supports_shared(slot) ||
+	     kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE)) {
+		return kvm_gmem_max_mapping_level(slot, gfn, max_level);
+	}
+
+	return min(max_level, host_pfn_mapping_level(kvm, gfn, slot));
+}
+
+static inline bool fault_from_gmem(struct kvm_page_fault *fault)
+{
+	return fault->is_private ||
+	       (kvm_slot_has_gmem(fault->slot) &&
+		kvm_gmem_memslot_supports_shared(fault->slot));
 }
 
 void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
@@ -3309,12 +3346,20 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
 	 * Enforce the iTLB multihit workaround after capturing the requested
 	 * level, which will be used to do precise, accurate accounting.
 	 */
-	fault->req_level = __kvm_mmu_max_mapping_level(vcpu->kvm, slot,
-						       fault->gfn, fault->max_level,
-						       fault->is_private);
+	fault->req_level = kvm_lpage_info_max_mapping_level(vcpu->kvm, slot,
+							    fault->gfn, fault->max_level);
 	if (fault->req_level == PG_LEVEL_4K || fault->huge_page_disallowed)
 		return;
 
+	if (!fault_from_gmem(fault)) {
+		int host_level;
+
+		host_level = host_pfn_mapping_level(vcpu->kvm, fault->gfn, slot);
+		fault->req_level = min(fault->req_level, host_level);
+		if (fault->req_level == PG_LEVEL_4K)
+			return;
+	}
+
 	/*
 	 * mmu_invalidate_retry() was successful and mmu_lock is held, so
 	 * the pmd can't be split from under us.
@@ -4448,23 +4493,6 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
 		vcpu->stat.pf_fixed++;
 }
 
-static inline u8 kvm_max_level_for_order(int order)
-{
-	BUILD_BUG_ON(KVM_MAX_HUGEPAGE_LEVEL > PG_LEVEL_1G);
-
-	KVM_MMU_WARN_ON(order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G) &&
-			order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M) &&
-			order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K));
-
-	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G))
-		return PG_LEVEL_1G;
-
-	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M))
-		return PG_LEVEL_2M;
-
-	return PG_LEVEL_4K;
-}
-
 static u8 kvm_max_level_for_fault_and_order(struct kvm *kvm,
 					    struct kvm_page_fault *fault,
 					    int order)
@@ -4523,7 +4551,7 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
 {
 	unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
-	if (fault->is_private || kvm_gmem_memslot_supports_shared(fault->slot))
+	if (fault_from_gmem(fault))
 		return kvm_mmu_faultin_pfn_gmem(vcpu, fault);
 
 	foll |= FOLL_NOWAIT;
include/linux/kvm_host.h +7 -0
index de7b46ee1762..f9bb025327c3 100644
@@ -2560,6 +2560,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
 int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
 		     gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
 		     int *max_order);
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn);
 #else
 static inline int kvm_gmem_get_pfn(struct kvm *kvm,
 				   struct kvm_memory_slot *slot, gfn_t gfn,
@@ -2569,6 +2570,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
 	KVM_BUG_ON(1, kvm);
 	return -EIO;
 }
+static inline int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot,
+					 gfn_t gfn)
+{
+	BUG();
+	return 0;
+}
 #endif /* CONFIG_KVM_GMEM */
 
 #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE
virt/kvm/guest_memfd.c +0 -0
index fe0245335c96..b8e247063b20 100644

virt/kvm/guest_memfd.c int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, · line 782

Quoted 9 lines
virt/kvm/guest_memfd.c +6 -0
@@ -774,6 +774,18 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
 }
 EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn);
 
+/**
+ * Returns the mapping order for this @gfn in @slot.
+ *
+ * This is equal to max_order that would be returned if kvm_gmem_get_pfn() were
+ * called now.
+ */

make W=1 ./ -s generates following warnings-

warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst
* Returns the mapping order for this @gfn in @slot

This will fix it.

Subject: [PATCH] tmp

---
 virt/kvm/guest_memfd.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
virt/kvm/guest_memfd.c +5 -3
index b8e247063b20..d880b9098cc0 100644
@@ -775,10 +775,12 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
 EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn);
 
 /**
- * Returns the mapping order for this @gfn in @slot.
+ * kvm_gmem_mapping_order - Get the mapping order for a GFN.
+ * @slot: The KVM memory slot containing the @gfn.
+ * @gfn: The guest frame number to check.
  *
- * This is equal to max_order that would be returned if kvm_gmem_get_pfn() were
- * called now.
+ * Returns: The mapping order for a @gfn in @slot. This is equal to max_order
+ *          that kvm_gmem_get_pfn() would return for this @gfn.
  */
 int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn)
 {
Signature · 4 lines
2.34.1

Thanks,
Shivank
Quoted 9 lines
virt/kvm/guest_memfd.c +6 -0
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn)
+{
+	return 0;
+}
+EXPORT_SYMBOL_GPL(kvm_gmem_mapping_order);
+
 #ifdef CONFIG_KVM_GENERIC_GMEM_POPULATE
 long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long npages,
 		       kvm_gmem_populate_cb post_populate, void *opaque)
author
#50 lore raw

Re: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd

Quoted 228 lines · Shivank Garg
Quoted 215 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

This patch adds kvm_gmem_max_mapping_level(), which always returns PG_LEVEL_4K since guest_memfd only supports 4K pages for now.

When guest_memfd supports shared memory, max_mapping_level (especially when recovering huge pages - see call to __kvm_mmu_max_mapping_level() from recover_huge_pages_range()) should take input from guest_memfd.

Input from guest_memfd should be taken in these cases:

+ if the memslot supports shared memory (guest_memfd is used for shared memory, or in future both shared and private memory) or
+ if the memslot is only used for private memory and that gfn is private.

If the memslot doesn't use guest_memfd, figure out the max_mapping_level using the host page tables like before.

This patch also refactors and inlines the other call to __kvm_mmu_max_mapping_level().

In kvm_mmu_hugepage_adjust(), guest_memfd's input is already provided (if applicable) in fault->max_level. Hence, there is no need to query guest_memfd.

lpage_info is queried like before, and then if the fault is not from guest_memfd, adjust fault->req_level based on input from host page tables.

---
 arch/x86/kvm/mmu/mmu.c   | 92 ++++++++++++++++++++++++++--------------
 include/linux/kvm_host.h |  7 +++
 virt/kvm/guest_memfd.c   | 12 ++++++
 3 files changed, 79 insertions(+), 32 deletions(-)
arch/x86/kvm/mmu/mmu.c +60 -32
index cfbb471f7c70..9e0bc8114859 100644
@@ -3256,12 +3256,11 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
      return level;
 }
 
-static int __kvm_mmu_max_mapping_level(struct kvm *kvm,
-                                    const struct kvm_memory_slot *slot,
-                                    gfn_t gfn, int max_level, bool is_private)
+static int kvm_lpage_info_max_mapping_level(struct kvm *kvm,
+                                         const struct kvm_memory_slot *slot,
+                                         gfn_t gfn, int max_level)
 {
      struct kvm_lpage_info *linfo;
-     int host_level;
 
      max_level = min(max_level, max_huge_page_level);
      for ( ; max_level > PG_LEVEL_4K; max_level--) {
@@ -3270,23 +3269,61 @@ static int __kvm_mmu_max_mapping_level(struct kvm *kvm,
                      break;
      }
 
-     if (is_private)
-             return max_level;
+     return max_level;
+}
+
+static inline u8 kvm_max_level_for_order(int order)
+{
+     BUILD_BUG_ON(KVM_MAX_HUGEPAGE_LEVEL > PG_LEVEL_1G);
+
+     KVM_MMU_WARN_ON(order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G) &&
+                     order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M) &&
+                     order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K));
+
+     if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G))
+             return PG_LEVEL_1G;
+
+     if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M))
+             return PG_LEVEL_2M;
+
+     return PG_LEVEL_4K;
+}
+
+static inline int kvm_gmem_max_mapping_level(const struct kvm_memory_slot *slot,
+                                          gfn_t gfn, int max_level)
+{
+     int max_order;
 
      if (max_level == PG_LEVEL_4K)
              return PG_LEVEL_4K;
 
-     host_level = host_pfn_mapping_level(kvm, gfn, slot);
-     return min(host_level, max_level);
+     max_order = kvm_gmem_mapping_order(slot, gfn);
+     return min(max_level, kvm_max_level_for_order(max_order));
 }
 
 int kvm_mmu_max_mapping_level(struct kvm *kvm,
                            const struct kvm_memory_slot *slot, gfn_t gfn)
 {
-     bool is_private = kvm_slot_has_gmem(slot) &&
-                       kvm_mem_is_private(kvm, gfn);
+     int max_level;
+
+     max_level = kvm_lpage_info_max_mapping_level(kvm, slot, gfn, PG_LEVEL_NUM);
+     if (max_level == PG_LEVEL_4K)
+             return PG_LEVEL_4K;
 
-     return __kvm_mmu_max_mapping_level(kvm, slot, gfn, PG_LEVEL_NUM, is_private);
+     if (kvm_slot_has_gmem(slot) &&
+         (kvm_gmem_memslot_supports_shared(slot) ||
+          kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE)) {
+             return kvm_gmem_max_mapping_level(slot, gfn, max_level);
+     }
+
+     return min(max_level, host_pfn_mapping_level(kvm, gfn, slot));
+}
+
+static inline bool fault_from_gmem(struct kvm_page_fault *fault)
+{
+     return fault->is_private ||
+            (kvm_slot_has_gmem(fault->slot) &&
+             kvm_gmem_memslot_supports_shared(fault->slot));
 }
 
 void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
@@ -3309,12 +3346,20 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
       * Enforce the iTLB multihit workaround after capturing the requested
       * level, which will be used to do precise, accurate accounting.
       */
-     fault->req_level = __kvm_mmu_max_mapping_level(vcpu->kvm, slot,
-                                                    fault->gfn, fault->max_level,
-                                                    fault->is_private);
+     fault->req_level = kvm_lpage_info_max_mapping_level(vcpu->kvm, slot,
+                                                         fault->gfn, fault->max_level);
      if (fault->req_level == PG_LEVEL_4K || fault->huge_page_disallowed)
              return;
 
+     if (!fault_from_gmem(fault)) {
+             int host_level;
+
+             host_level = host_pfn_mapping_level(vcpu->kvm, fault->gfn, slot);
+             fault->req_level = min(fault->req_level, host_level);
+             if (fault->req_level == PG_LEVEL_4K)
+                     return;
+     }
+
      /*
       * mmu_invalidate_retry() was successful and mmu_lock is held, so
       * the pmd can't be split from under us.
@@ -4448,23 +4493,6 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
              vcpu->stat.pf_fixed++;
 }
 
-static inline u8 kvm_max_level_for_order(int order)
-{
-     BUILD_BUG_ON(KVM_MAX_HUGEPAGE_LEVEL > PG_LEVEL_1G);
-
-     KVM_MMU_WARN_ON(order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G) &&
-                     order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M) &&
-                     order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K));
-
-     if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G))
-             return PG_LEVEL_1G;
-
-     if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M))
-             return PG_LEVEL_2M;
-
-     return PG_LEVEL_4K;
-}
-
 static u8 kvm_max_level_for_fault_and_order(struct kvm *kvm,
                                          struct kvm_page_fault *fault,
                                          int order)
@@ -4523,7 +4551,7 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
 {
      unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
-     if (fault->is_private || kvm_gmem_memslot_supports_shared(fault->slot))
+     if (fault_from_gmem(fault))
              return kvm_mmu_faultin_pfn_gmem(vcpu, fault);
 
      foll |= FOLL_NOWAIT;
include/linux/kvm_host.h +7 -0
index de7b46ee1762..f9bb025327c3 100644
@@ -2560,6 +2560,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
 int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
                   gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
                   int *max_order);
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn);
 #else
 static inline int kvm_gmem_get_pfn(struct kvm *kvm,
                                 struct kvm_memory_slot *slot, gfn_t gfn,
@@ -2569,6 +2570,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
      KVM_BUG_ON(1, kvm);
      return -EIO;
 }
+static inline int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot,
+                                      gfn_t gfn)
+{
+     BUG();
+     return 0;
+}
 #endif /* CONFIG_KVM_GMEM */
 
 #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE
virt/kvm/guest_memfd.c +0 -0
index fe0245335c96..b8e247063b20 100644

virt/kvm/guest_memfd.c int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, · line 782

Quoted 9 lines
virt/kvm/guest_memfd.c +6 -0
@@ -774,6 +774,18 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
 }
 EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn);
 
+/**
+ * Returns the mapping order for this @gfn in @slot.
+ *
+ * This is equal to max_order that would be returned if kvm_gmem_get_pfn() were
+ * called now.
+ */

make W=1 ./ -s generates following warnings-

Quoted 4 lines

warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst
* Returns the mapping order for this @gfn in @slot

This will fix it.

Thank you!
/fuad

Quoted 44 lines

Subject: [PATCH] tmp

---
 virt/kvm/guest_memfd.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
virt/kvm/guest_memfd.c +5 -4
index b8e247063b20..d880b9098cc0 100644
@@ -775,10 +775,12 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
 EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn);
 
 /**
- * Returns the mapping order for this @gfn in @slot.
+ * kvm_gmem_mapping_order - Get the mapping order for a GFN.
+ * @slot: The KVM memory slot containing the @gfn.
+ * @gfn: The guest frame number to check.
  *
- * This is equal to max_order that would be returned if kvm_gmem_get_pfn() were
- * called now.
+ * Returns: The mapping order for a @gfn in @slot. This is equal to max_order
+ *          that kvm_gmem_get_pfn() would return for this @gfn.
  */
 int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn)
 {
--

2.34.1

Thanks,
Shivank

Quoted 9 lines
virt/kvm/guest_memfd.c +6 -0
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn)
+{
+     return 0;
+}
+EXPORT_SYMBOL_GPL(kvm_gmem_mapping_order);
+
 #ifdef CONFIG_KVM_GENERIC_GMEM_POPULATE
 long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long npages,
                     kvm_gmem_populate_cb post_populate, void *opaque)
#51 lore raw

Re: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd

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

Hi Fuad,

kernel test robot noticed the following build errors:

[auto build test ERROR on 82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3]

url:    https://github.com/intel-lab-lkp/linux/commits/Fuad-Tabba/KVM-Rename-CONFIG_KVM_PRIVATE_MEM-to-CONFIG_KVM_GMEM/20250514-003900
base:   82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3
patch link:    https://lore.kernel.org/r/20250513163438.3942405-11-tabba%40google.com
patch subject: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd
config: x86_64-buildonly-randconfig-002-20250514 (https://download.01.org/0day-ci/archive/20250514/[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/20250514/[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 >>):

arch/x86/kvm/mmu/mmu.c: In function 'kvm_mmu_max_mapping_level':

>> arch/x86/kvm/mmu/mmu.c:3315:14: error: implicit declaration of function 'kvm_get_memory_attributes' [-Werror=implicit-function-declaration]
    3315 |              kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE)) {
         |              ^~~~~~~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors

vim +/kvm_get_memory_attributes +3315 arch/x86/kvm/mmu/mmu.c

3303
3304 int kvm_mmu_max_mapping_level(struct kvm *kvm,
3305 const struct kvm_memory_slot *slot, gfn_t gfn)
3306 {
3307 int max_level;
3308
3309 max_level = kvm_lpage_info_max_mapping_level(kvm, slot, gfn, PG_LEVEL_NUM);
3310 if (max_level == PG_LEVEL_4K)
3311 return PG_LEVEL_4K;
3312
3313 if (kvm_slot_has_gmem(slot) &&
3314 (kvm_gmem_memslot_supports_shared(slot) ||

> 3315		     kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE)) {

3316 return kvm_gmem_max_mapping_level(slot, gfn, max_level);
3317 }
3318
3319 return min(max_level, host_pfn_mapping_level(kvm, gfn, slot));
3320 }
3321

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

Re: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd

In reply to Fuad Tabba
Quoted 46 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

This patch adds kvm_gmem_max_mapping_level(), which always returns PG_LEVEL_4K since guest_memfd only supports 4K pages for now.

When guest_memfd supports shared memory, max_mapping_level (especially when recovering huge pages - see call to __kvm_mmu_max_mapping_level() from recover_huge_pages_range()) should take input from guest_memfd.

Input from guest_memfd should be taken in these cases:

+ if the memslot supports shared memory (guest_memfd is used for shared memory, or in future both shared and private memory) or
+ if the memslot is only used for private memory and that gfn is private.

If the memslot doesn't use guest_memfd, figure out the max_mapping_level using the host page tables like before.

This patch also refactors and inlines the other call to __kvm_mmu_max_mapping_level().

In kvm_mmu_hugepage_adjust(), guest_memfd's input is already provided (if applicable) in fault->max_level. Hence, there is no need to query guest_memfd.

lpage_info is queried like before, and then if the fault is not from guest_memfd, adjust fault->req_level based on input from host page tables.

---
  arch/x86/kvm/mmu/mmu.c   | 92 ++++++++++++++++++++++++++--------------
  include/linux/kvm_host.h |  7 +++
  virt/kvm/guest_memfd.c   | 12 ++++++
  3 files changed, 79 insertions(+), 32 deletions(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index cfbb471f7c70..9e0bc8114859 100644
@@ -3256,12 +3256,11 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
  	return level;
  }

[...]

arch/x86/kvm/mmu/mmu.c static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, · line 4554

Quoted 9 lines

static u8 kvm_max_level_for_fault_and_order(struct kvm *kvm,
struct kvm_page_fault *fault, int order)

arch/x86/kvm/mmu/mmu.c +1 -1
@@ -4523,7 +4551,7 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
  {
  	unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
-	if (fault->is_private || kvm_gmem_memslot_supports_shared(fault->slot))
+	if (fault_from_gmem(fault))

Should this change rather have been done in the previous patch?

(then only adjust fault_from_gmem() in this function as required)

Quoted 16 lines
arch/x86/kvm/mmu/mmu.c +0 -0
  		return kvm_mmu_faultin_pfn_gmem(vcpu, fault);
 
  	foll |= FOLL_NOWAIT;
include/linux/kvm_host.h +1 -0
index de7b46ee1762..f9bb025327c3 100644
@@ -2560,6 +2560,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
  int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
  		     gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
  		     int *max_order);
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn);
  #else
  static inline int kvm_gmem_get_pfn(struct kvm *kvm,
  				   struct kvm_memory_slot *slot, gfn_t gfn,

include/linux/kvm_host.h static inline int kvm_gmem_get_pfn(struct kvm *kvm, · line 2574

Quoted 5 lines
include/linux/kvm_host.h +2 -0
@@ -2569,6 +2570,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
  	KVM_BUG_ON(1, kvm);
  	return -EIO;
  }
+static inline int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot,
+					 gfn_t gfn)

Probably should indent with two tabs here.

Signature · 3 lines
Cheers,

David / dhildenb
#53 lore raw

Re: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd

Quoted 56 lines · David Hildenbrand
Quoted 46 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

This patch adds kvm_gmem_max_mapping_level(), which always returns PG_LEVEL_4K since guest_memfd only supports 4K pages for now.

When guest_memfd supports shared memory, max_mapping_level (especially when recovering huge pages - see call to __kvm_mmu_max_mapping_level() from recover_huge_pages_range()) should take input from guest_memfd.

Input from guest_memfd should be taken in these cases:

+ if the memslot supports shared memory (guest_memfd is used for shared memory, or in future both shared and private memory) or
+ if the memslot is only used for private memory and that gfn is private.

If the memslot doesn't use guest_memfd, figure out the max_mapping_level using the host page tables like before.

This patch also refactors and inlines the other call to __kvm_mmu_max_mapping_level().

In kvm_mmu_hugepage_adjust(), guest_memfd's input is already provided (if applicable) in fault->max_level. Hence, there is no need to query guest_memfd.

lpage_info is queried like before, and then if the fault is not from guest_memfd, adjust fault->req_level based on input from host page tables.

---
  arch/x86/kvm/mmu/mmu.c   | 92 ++++++++++++++++++++++++++--------------
  include/linux/kvm_host.h |  7 +++
  virt/kvm/guest_memfd.c   | 12 ++++++
  3 files changed, 79 insertions(+), 32 deletions(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index cfbb471f7c70..9e0bc8114859 100644
@@ -3256,12 +3256,11 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
  	return level;
  }

[...]

Quoted 7 lines

static u8 kvm_max_level_for_fault_and_order(struct kvm *kvm,
struct kvm_page_fault *fault, int order)

arch/x86/kvm/mmu/mmu.c +0 -0
@@ -4523,7 +4551,7 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
  {
  	unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
Quoted 7 lines

arch/x86/kvm/mmu/mmu.c static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, · line 4554

Quoted 2 lines
arch/x86/kvm/mmu/mmu.c +1 -1
-	if (fault->is_private || kvm_gmem_memslot_supports_shared(fault->slot))
+	if (fault_from_gmem(fault))

Should this change rather have been done in the previous patch?

(then only adjust fault_from_gmem() in this function as required)

Yes, that is a good idea, thanks!

Quoted 15 lines
Quoted 15 lines
arch/x86/kvm/mmu/mmu.c +0 -0
  		return kvm_mmu_faultin_pfn_gmem(vcpu, fault);
 
  	foll |= FOLL_NOWAIT;
include/linux/kvm_host.h +1 -0
index de7b46ee1762..f9bb025327c3 100644
@@ -2560,6 +2560,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
  int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
  		     gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
  		     int *max_order);
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn);
  #else
  static inline int kvm_gmem_get_pfn(struct kvm *kvm,
  				   struct kvm_memory_slot *slot, gfn_t gfn,
Quoted 8 lines

include/linux/kvm_host.h static inline int kvm_gmem_get_pfn(struct kvm *kvm, · line 2574

Quoted 6 lines
include/linux/kvm_host.h +2 -0
@@ -2569,6 +2570,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
  	KVM_BUG_ON(1, kvm);
  	return -EIO;
  }
+static inline int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot,
+					 gfn_t gfn)

Probably should indent with two tabs here.

Yup!

#54 lore raw

Re: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd

Quoted 21 lines · Ackerley Tng
Quoted 12 lines
Quoted 12 lines
include/linux/kvm_host.h +1 -0
index de7b46ee1762..f9bb025327c3 100644
@@ -2560,6 +2560,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
  int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
  		     gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
  		     int *max_order);
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn);
  #else
  static inline int kvm_gmem_get_pfn(struct kvm *kvm,
  				   struct kvm_memory_slot *slot, gfn_t gfn,
Quoted 8 lines

include/linux/kvm_host.h static inline int kvm_gmem_get_pfn(struct kvm *kvm, · line 2574

Quoted 6 lines
include/linux/kvm_host.h +2 -0
@@ -2569,6 +2570,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
  	KVM_BUG_ON(1, kvm);
  	return -EIO;
  }
+static inline int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot,
+					 gfn_t gfn)

Probably should indent with two tabs here.

Quoted 1 line

Yup!

Nope! :-)

In KVM, please align the indentation as you did.

: Yeah, that way of indenting is rather bad practice. Especially for new : code we're adding or when we touch existing code, we should just use two : tabs.

: That way, we can fit more stuff into a single line, and when doing : simple changes, such as renaming the function or changing the return : type, we won't have to touch all the parameters.

At the cost of readability, IMO. The number of eyeballs that read the code is orders of magnitude greater than the number of times a function's parameters end up being shuffled around. Sacrificing readability and consistenty to avoid a small amount of rare churn isn't a good tradeoff.

#55 lore raw

Re: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd

Quoted 32 lines · Sean Christopherson
Quoted 21 lines · Ackerley Tng
Quoted 12 lines
Quoted 12 lines
include/linux/kvm_host.h +1 -0
index de7b46ee1762..f9bb025327c3 100644
@@ -2560,6 +2560,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
   int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
   		     gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
   		     int *max_order);
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn);
   #else
   static inline int kvm_gmem_get_pfn(struct kvm *kvm,
   				   struct kvm_memory_slot *slot, gfn_t gfn,
Quoted 8 lines

include/linux/kvm_host.h static inline int kvm_gmem_get_pfn(struct kvm *kvm, · line 2574

Quoted 6 lines
include/linux/kvm_host.h +2 -0
@@ -2569,6 +2570,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
   	KVM_BUG_ON(1, kvm);
   	return -EIO;
   }
+static inline int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot,
+					 gfn_t gfn)

Probably should indent with two tabs here.

Quoted 1 line

Yup!

Nope! :-)

In KVM, please align the indentation as you did.

: Yeah, that way of indenting is rather bad practice. Especially for new : code we're adding or when we touch existing code, we should just use two : tabs.

Quoted 8 lines

: That way, we can fit more stuff into a single line, and when doing : simple changes, such as renaming the function or changing the return : type, we won't have to touch all the parameters.

At the cost of readability, IMO. The number of eyeballs that read the code is orders of magnitude greater than the number of times a function's parameters end up being shuffled around. Sacrificing readability and consistenty to avoid a small amount of rare churn isn't a good tradeoff.

I new KVM wanted to be weird! :P

Signature · 3 lines
Cheers,

David / dhildenb
author
#56 lore raw

Re: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd

In reply to David Hildenbrand

Hi David,

Quoted 79 lines · David Hildenbrand
Quoted 46 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

This patch adds kvm_gmem_max_mapping_level(), which always returns PG_LEVEL_4K since guest_memfd only supports 4K pages for now.

When guest_memfd supports shared memory, max_mapping_level (especially when recovering huge pages - see call to __kvm_mmu_max_mapping_level() from recover_huge_pages_range()) should take input from guest_memfd.

Input from guest_memfd should be taken in these cases:

+ if the memslot supports shared memory (guest_memfd is used for shared memory, or in future both shared and private memory) or
+ if the memslot is only used for private memory and that gfn is private.

If the memslot doesn't use guest_memfd, figure out the max_mapping_level using the host page tables like before.

This patch also refactors and inlines the other call to __kvm_mmu_max_mapping_level().

In kvm_mmu_hugepage_adjust(), guest_memfd's input is already provided (if applicable) in fault->max_level. Hence, there is no need to query guest_memfd.

lpage_info is queried like before, and then if the fault is not from guest_memfd, adjust fault->req_level based on input from host page tables.

---
  arch/x86/kvm/mmu/mmu.c   | 92 ++++++++++++++++++++++++++--------------
  include/linux/kvm_host.h |  7 +++
  virt/kvm/guest_memfd.c   | 12 ++++++
  3 files changed, 79 insertions(+), 32 deletions(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index cfbb471f7c70..9e0bc8114859 100644
@@ -3256,12 +3256,11 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
      return level;
  }

[...]

arch/x86/kvm/mmu/mmu.c static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, · line 4554

Quoted 9 lines

static u8 kvm_max_level_for_fault_and_order(struct kvm *kvm,
struct kvm_page_fault *fault, int order)

arch/x86/kvm/mmu/mmu.c +1 -1
@@ -4523,7 +4551,7 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
  {
      unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
-     if (fault->is_private || kvm_gmem_memslot_supports_shared(fault->slot))
+     if (fault_from_gmem(fault))

Should this change rather have been done in the previous patch?

(then only adjust fault_from_gmem() in this function as required)

Quoted 15 lines
arch/x86/kvm/mmu/mmu.c +0 -0
              return kvm_mmu_faultin_pfn_gmem(vcpu, fault);
 
      foll |= FOLL_NOWAIT;
include/linux/kvm_host.h +1 -0
index de7b46ee1762..f9bb025327c3 100644
@@ -2560,6 +2560,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
  int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
                   gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
                   int *max_order);
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn);
  #else
  static inline int kvm_gmem_get_pfn(struct kvm *kvm,
                                 struct kvm_memory_slot *slot, gfn_t gfn,
Quoted 8 lines

include/linux/kvm_host.h static inline int kvm_gmem_get_pfn(struct kvm *kvm, · line 2574

Quoted 6 lines
include/linux/kvm_host.h +2 -0
@@ -2569,6 +2570,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
      KVM_BUG_ON(1, kvm);
      return -EIO;
  }
+static inline int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot,
+                                      gfn_t gfn)

Probably should indent with two tabs here.

(I'm fixing the patch before respinning, hence it's me asking)

Not sure I understand. Indentation here matches the same style as that for kvm_gmem_get_pfn() right above it in the alignment of the parameters, i.e., the parameter gfn_t gfn is aligned with the parameter const struct kvm_memory_slot *slot (four tabs and a space).

Thanks,
/fuad

Quoted 7 lines

--
Cheers,

David / dhildenb

#57 lore raw

Re: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd

Quoted 91 lines · Fuad Tabba

Hi David,

Quoted 79 lines · David Hildenbrand
Quoted 46 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

This patch adds kvm_gmem_max_mapping_level(), which always returns PG_LEVEL_4K since guest_memfd only supports 4K pages for now.

When guest_memfd supports shared memory, max_mapping_level (especially when recovering huge pages - see call to __kvm_mmu_max_mapping_level() from recover_huge_pages_range()) should take input from guest_memfd.

Input from guest_memfd should be taken in these cases:

+ if the memslot supports shared memory (guest_memfd is used for shared memory, or in future both shared and private memory) or
+ if the memslot is only used for private memory and that gfn is private.

If the memslot doesn't use guest_memfd, figure out the max_mapping_level using the host page tables like before.

This patch also refactors and inlines the other call to __kvm_mmu_max_mapping_level().

In kvm_mmu_hugepage_adjust(), guest_memfd's input is already provided (if applicable) in fault->max_level. Hence, there is no need to query guest_memfd.

lpage_info is queried like before, and then if the fault is not from guest_memfd, adjust fault->req_level based on input from host page tables.

---
   arch/x86/kvm/mmu/mmu.c   | 92 ++++++++++++++++++++++++++--------------
   include/linux/kvm_host.h |  7 +++
   virt/kvm/guest_memfd.c   | 12 ++++++
   3 files changed, 79 insertions(+), 32 deletions(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index cfbb471f7c70..9e0bc8114859 100644
@@ -3256,12 +3256,11 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
       return level;
   }

[...]

arch/x86/kvm/mmu/mmu.c static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, · line 4554

Quoted 9 lines

static u8 kvm_max_level_for_fault_and_order(struct kvm *kvm,
struct kvm_page_fault *fault, int order)

arch/x86/kvm/mmu/mmu.c +1 -1
@@ -4523,7 +4551,7 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
   {
       unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
-     if (fault->is_private || kvm_gmem_memslot_supports_shared(fault->slot))
+     if (fault_from_gmem(fault))

Should this change rather have been done in the previous patch?

(then only adjust fault_from_gmem() in this function as required)

Quoted 15 lines
arch/x86/kvm/mmu/mmu.c +0 -0
               return kvm_mmu_faultin_pfn_gmem(vcpu, fault);
 
       foll |= FOLL_NOWAIT;
include/linux/kvm_host.h +1 -0
index de7b46ee1762..f9bb025327c3 100644
@@ -2560,6 +2560,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
   int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
                    gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
                    int *max_order);
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn);
   #else
   static inline int kvm_gmem_get_pfn(struct kvm *kvm,
                                  struct kvm_memory_slot *slot, gfn_t gfn,
Quoted 8 lines

include/linux/kvm_host.h static inline int kvm_gmem_get_pfn(struct kvm *kvm, · line 2574

Quoted 6 lines
include/linux/kvm_host.h +2 -0
@@ -2569,6 +2570,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
       KVM_BUG_ON(1, kvm);
       return -EIO;
   }
+static inline int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot,
+                                      gfn_t gfn)

Probably should indent with two tabs here.

Quoted 7 lines

(I'm fixing the patch before respinning, hence it's me asking)

Not sure I understand. Indentation here matches the same style as that for kvm_gmem_get_pfn() right above it in the alignment of the parameters, i.e., the parameter gfn_t gfn is aligned with the parameter const struct kvm_memory_slot *slot (four tabs and a space).

Yeah, that way of indenting is rather bad practice. Especially for new code we're adding or when we touch existing code, we should just use two tabs.

That way, we can fit more stuff into a single line, and when doing simple changes, such as renaming the function or changing the return type, we won't have to touch all the parameters.

Maybe KVM has its own rules on that ... that's why I said "probably" :)

Signature · 3 lines
Cheers,

David / dhildenb
author
#58 lore raw

Re: [PATCH v9 10/17] KVM: x86: Compute max_mapping_level with input from guest_memfd

Quoted 105 lines · David Hildenbrand
Quoted 91 lines · Fuad Tabba

Hi David,

Quoted 79 lines · David Hildenbrand
Quoted 46 lines · Fuad Tabba

From: Ackerley Tng <[email protected]>

This patch adds kvm_gmem_max_mapping_level(), which always returns PG_LEVEL_4K since guest_memfd only supports 4K pages for now.

When guest_memfd supports shared memory, max_mapping_level (especially when recovering huge pages - see call to __kvm_mmu_max_mapping_level() from recover_huge_pages_range()) should take input from guest_memfd.

Input from guest_memfd should be taken in these cases:

+ if the memslot supports shared memory (guest_memfd is used for shared memory, or in future both shared and private memory) or
+ if the memslot is only used for private memory and that gfn is private.

If the memslot doesn't use guest_memfd, figure out the max_mapping_level using the host page tables like before.

This patch also refactors and inlines the other call to __kvm_mmu_max_mapping_level().

In kvm_mmu_hugepage_adjust(), guest_memfd's input is already provided (if applicable) in fault->max_level. Hence, there is no need to query guest_memfd.

lpage_info is queried like before, and then if the fault is not from guest_memfd, adjust fault->req_level based on input from host page tables.

---
   arch/x86/kvm/mmu/mmu.c   | 92 ++++++++++++++++++++++++++--------------
   include/linux/kvm_host.h |  7 +++
   virt/kvm/guest_memfd.c   | 12 ++++++
   3 files changed, 79 insertions(+), 32 deletions(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index cfbb471f7c70..9e0bc8114859 100644
@@ -3256,12 +3256,11 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
       return level;
   }

[...]

arch/x86/kvm/mmu/mmu.c static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, · line 4554

Quoted 9 lines

static u8 kvm_max_level_for_fault_and_order(struct kvm *kvm,
struct kvm_page_fault *fault, int order)

arch/x86/kvm/mmu/mmu.c +1 -1
@@ -4523,7 +4551,7 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
   {
       unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
-     if (fault->is_private || kvm_gmem_memslot_supports_shared(fault->slot))
+     if (fault_from_gmem(fault))

Should this change rather have been done in the previous patch?

(then only adjust fault_from_gmem() in this function as required)

Quoted 15 lines
arch/x86/kvm/mmu/mmu.c +0 -0
               return kvm_mmu_faultin_pfn_gmem(vcpu, fault);
 
       foll |= FOLL_NOWAIT;
include/linux/kvm_host.h +1 -0
index de7b46ee1762..f9bb025327c3 100644
@@ -2560,6 +2560,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)
   int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
                    gfn_t gfn, kvm_pfn_t *pfn, struct page **page,
                    int *max_order);
+int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot, gfn_t gfn);
   #else
   static inline int kvm_gmem_get_pfn(struct kvm *kvm,
                                  struct kvm_memory_slot *slot, gfn_t gfn,
Quoted 8 lines

include/linux/kvm_host.h static inline int kvm_gmem_get_pfn(struct kvm *kvm, · line 2574

Quoted 6 lines
include/linux/kvm_host.h +2 -0
@@ -2569,6 +2570,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,
       KVM_BUG_ON(1, kvm);
       return -EIO;
   }
+static inline int kvm_gmem_mapping_order(const struct kvm_memory_slot *slot,
+                                      gfn_t gfn)

Probably should indent with two tabs here.

Quoted 7 lines

(I'm fixing the patch before respinning, hence it's me asking)

Not sure I understand. Indentation here matches the same style as that for kvm_gmem_get_pfn() right above it in the alignment of the parameters, i.e., the parameter gfn_t gfn is aligned with the parameter const struct kvm_memory_slot *slot (four tabs and a space).

Yeah, that way of indenting is rather bad practice. Especially for new code we're adding or when we touch existing code, we should just use two tabs.

Quoted 5 lines

That way, we can fit more stuff into a single line, and when doing simple changes, such as renaming the function or changing the return type, we won't have to touch all the parameters.

Maybe KVM has its own rules on that ... that's why I said "probably" :)

:)

I see, although I agree with you, I'd rather that indentation be consistent within the same file.

Thanks,
/fuad

Quoted 5 lines

--
Cheers,

David / dhildenb

author
#59 lore raw

PATCH v9 11/17 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 the conditions are met. Also, remove the comment about logging_active being guaranteed to never be true for VM_PFNMAP memslots, since it's not actually correct.

No functional change intended.

---
 arch/arm64/kvm/mmu.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
arch/arm64/kvm/mmu.c +4 -9
index eeda92330ade..9865ada04a81 100644
@@ -1472,7 +1472,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;
@@ -1484,6 +1484,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 || is_protected_kvm_enabled();
 	long vma_pagesize, fault_granule;
 	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
 	struct kvm_pgtable *pgt;
@@ -1536,16 +1537,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		return -EFAULT;
 	}
 
-	/*
-	 * logging_active is guaranteed to never be true for VM_PFNMAP
-	 * memslots.
-	 */
-	if (logging_active || is_protected_kvm_enabled()) {
-		force_pte = true;
+	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.49.0.1045.g170613ef41-goog
author
#60 lore raw

PATCH v9 12/17 KVM: arm64: Rename variables in user_mem_abort()

In reply to Fuad Tabba

Guest memory can be backed by guest_memfd or by anonymous memory. Rename vma_shift to page_shift and vma_pagesize to page_size to ease readability in subsequent patches.

---
 arch/arm64/kvm/mmu.c | 54 ++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 27 deletions(-)
arch/arm64/kvm/mmu.c +27 -27
index 9865ada04a81..d756c2b5913f 100644
@@ -1479,13 +1479,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	phys_addr_t ipa = fault_ipa;
 	struct kvm *kvm = vcpu->kvm;
 	struct vm_area_struct *vma;
-	short vma_shift;
+	short page_shift;
 	void *memcache;
 	gfn_t gfn;
 	kvm_pfn_t pfn;
 	bool logging_active = memslot_is_logging(memslot);
 	bool force_pte = logging_active || is_protected_kvm_enabled();
-	long vma_pagesize, fault_granule;
+	long page_size, fault_granule;
 	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
 	struct kvm_pgtable *pgt;
 	struct page *page;
@@ -1538,11 +1538,11 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	}
 
 	if (force_pte)
-		vma_shift = PAGE_SHIFT;
+		page_shift = PAGE_SHIFT;
 	else
-		vma_shift = get_vma_page_shift(vma, hva);
+		page_shift = get_vma_page_shift(vma, hva);
 
-	switch (vma_shift) {
+	switch (page_shift) {
 #ifndef __PAGETABLE_PMD_FOLDED
 	case PUD_SHIFT:
 		if (fault_supports_stage2_huge_mapping(memslot, hva, PUD_SIZE))
@@ -1550,23 +1550,23 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		fallthrough;
 #endif
 	case CONT_PMD_SHIFT:
-		vma_shift = PMD_SHIFT;
+		page_shift = PMD_SHIFT;
 		fallthrough;
 	case PMD_SHIFT:
 		if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE))
 			break;
 		fallthrough;
 	case CONT_PTE_SHIFT:
-		vma_shift = PAGE_SHIFT;
+		page_shift = PAGE_SHIFT;
 		force_pte = true;
 		fallthrough;
 	case PAGE_SHIFT:
 		break;
 	default:
-		WARN_ONCE(1, "Unknown vma_shift %d", vma_shift);
+		WARN_ONCE(1, "Unknown page_shift %d", page_shift);
 	}
 
-	vma_pagesize = 1UL << vma_shift;
+	page_size = 1UL << page_shift;
 
 	if (nested) {
 		unsigned long max_map_size;
@@ -1592,7 +1592,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			max_map_size = PAGE_SIZE;
 
 		force_pte = (max_map_size == PAGE_SIZE);
-		vma_pagesize = min(vma_pagesize, (long)max_map_size);
+		page_size = min_t(long, page_size, max_map_size);
 	}
 
 	/*
@@ -1600,9 +1600,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	 * ensure we find the right PFN and lay down the mapping in the right
 	 * place.
 	 */
-	if (vma_pagesize == PMD_SIZE || vma_pagesize == PUD_SIZE) {
-		fault_ipa &= ~(vma_pagesize - 1);
-		ipa &= ~(vma_pagesize - 1);
+	if (page_size == PMD_SIZE || page_size == PUD_SIZE) {
+		fault_ipa &= ~(page_size - 1);
+		ipa &= ~(page_size - 1);
 	}
 
 	gfn = ipa >> PAGE_SHIFT;
@@ -1627,7 +1627,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0,
 				&writable, &page);
 	if (pfn == KVM_PFN_ERR_HWPOISON) {
-		kvm_send_hwpoison_signal(hva, vma_shift);
+		kvm_send_hwpoison_signal(hva, page_shift);
 		return 0;
 	}
 	if (is_error_noslot_pfn(pfn))
@@ -1636,9 +1636,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (kvm_is_device_pfn(pfn)) {
 		/*
 		 * If the page was identified as device early by looking at
-		 * the VMA flags, vma_pagesize is already representing the
+		 * the VMA flags, page_size is already representing the
 		 * largest quantity we can map.  If instead it was mapped
-		 * via __kvm_faultin_pfn(), vma_pagesize is set to PAGE_SIZE
+		 * via __kvm_faultin_pfn(), page_size is set to PAGE_SIZE
 		 * and must not be upgraded.
 		 *
 		 * In both cases, we don't let transparent_hugepage_adjust()
@@ -1686,16 +1686,16 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	 * If we are not forced to use page mapping, check if we are
 	 * backed by a THP and thus use block mapping if possible.
 	 */
-	if (vma_pagesize == PAGE_SIZE && !(force_pte || device)) {
+	if (page_size == PAGE_SIZE && !(force_pte || device)) {
 		if (fault_is_perm && fault_granule > PAGE_SIZE)
-			vma_pagesize = fault_granule;
+			page_size = fault_granule;
 		else
-			vma_pagesize = transparent_hugepage_adjust(kvm, memslot,
-								   hva, &pfn,
-								   &fault_ipa);
+			page_size = transparent_hugepage_adjust(kvm, memslot,
+								hva, &pfn,
+								&fault_ipa);
 
-		if (vma_pagesize < 0) {
-			ret = vma_pagesize;
+		if (page_size < 0) {
+			ret = page_size;
 			goto out_unlock;
 		}
 	}
@@ -1703,7 +1703,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (!fault_is_perm && !device && kvm_has_mte(kvm)) {
 		/* Check the VMM hasn't introduced a new disallowed VMA */
 		if (mte_allowed) {
-			sanitise_mte_tags(kvm, pfn, vma_pagesize);
+			sanitise_mte_tags(kvm, pfn, page_size);
 		} else {
 			ret = -EFAULT;
 			goto out_unlock;
@@ -1728,10 +1728,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
 	/*
 	 * Under the premise of getting a FSC_PERM fault, we just need to relax
-	 * permissions only if vma_pagesize equals fault_granule. Otherwise,
+	 * permissions only if page_size equals fault_granule. Otherwise,
 	 * kvm_pgtable_stage2_map() should be called to change block size.
 	 */
-	if (fault_is_perm && vma_pagesize == fault_granule) {
+	if (fault_is_perm && page_size == fault_granule) {
 		/*
 		 * Drop the SW bits in favour of those stored in the
 		 * PTE, which will be preserved.
@@ -1739,7 +1739,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		prot &= ~KVM_NV_GUEST_MAP_SZ;
 		ret = KVM_PGT_FN(kvm_pgtable_stage2_relax_perms)(pgt, fault_ipa, prot, flags);
 	} else {
-		ret = KVM_PGT_FN(kvm_pgtable_stage2_map)(pgt, fault_ipa, vma_pagesize,
+		ret = KVM_PGT_FN(kvm_pgtable_stage2_map)(pgt, fault_ipa, page_size,
 					     __pfn_to_phys(pfn), prot,
 					     memcache, flags);
 	}
Signature · 1 line
2.49.0.1045.g170613ef41-goog
#61 lore raw

Re: [PATCH v9 12/17] KVM: arm64: Rename variables in user_mem_abort()

Hi Fuad,

Quoted 30 lines · Fuad Tabba

Guest memory can be backed by guest_memfd or by anonymous memory. Rename vma_shift to page_shift and vma_pagesize to page_size to ease readability in subsequent patches.

---
  arch/arm64/kvm/mmu.c | 54 ++++++++++++++++++++++----------------------
  1 file changed, 27 insertions(+), 27 deletions(-)
arch/arm64/kvm/mmu.c +2 -2
index 9865ada04a81..d756c2b5913f 100644
@@ -1479,13 +1479,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	phys_addr_t ipa = fault_ipa;
  	struct kvm *kvm = vcpu->kvm;
  	struct vm_area_struct *vma;
-	short vma_shift;
+	short page_shift;
  	void *memcache;
  	gfn_t gfn;
  	kvm_pfn_t pfn;
  	bool logging_active = memslot_is_logging(memslot);
  	bool force_pte = logging_active || is_protected_kvm_enabled();
-	long vma_pagesize, fault_granule;
+	long page_size, fault_granule;
  	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
  	struct kvm_pgtable *pgt;
  	struct page *page;

[...]

Quoted 4 lines

/*

arch/arm64/kvm/mmu.c +0 -0
@@ -1600,9 +1600,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	 * ensure we find the right PFN and lay down the mapping in the right

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1607

Quoted 10 lines
arch/arm64/kvm/mmu.c +3 -3
  	 * place.
  	 */
-	if (vma_pagesize == PMD_SIZE || vma_pagesize == PUD_SIZE) {
-		fault_ipa &= ~(vma_pagesize - 1);
-		ipa &= ~(vma_pagesize - 1);
+	if (page_size == PMD_SIZE || page_size == PUD_SIZE) {
+		fault_ipa &= ~(page_size - 1);
+		ipa &= ~(page_size - 1);
  	}
 

nit: since we're here for readability, ALIGN_DOWN() may be used:

		fault_ipa = ALIGN_DOWN(fault_ipa, page_size);
		ipa = ALIGN_DOWN(ipa, page_size);

Thanks,
Gavin

author
#62 lore raw

Re: [PATCH v9 12/17] KVM: arm64: Rename variables in user_mem_abort()

Hi Gavin,

Quoted 54 lines · Gavin Shan

Hi Fuad,

Quoted 30 lines · Fuad Tabba

Guest memory can be backed by guest_memfd or by anonymous memory. Rename vma_shift to page_shift and vma_pagesize to page_size to ease readability in subsequent patches.

---
  arch/arm64/kvm/mmu.c | 54 ++++++++++++++++++++++----------------------
  1 file changed, 27 insertions(+), 27 deletions(-)
arch/arm64/kvm/mmu.c +2 -2
index 9865ada04a81..d756c2b5913f 100644
@@ -1479,13 +1479,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
      phys_addr_t ipa = fault_ipa;
      struct kvm *kvm = vcpu->kvm;
      struct vm_area_struct *vma;
-     short vma_shift;
+     short page_shift;
      void *memcache;
      gfn_t gfn;
      kvm_pfn_t pfn;
      bool logging_active = memslot_is_logging(memslot);
      bool force_pte = logging_active || is_protected_kvm_enabled();
-     long vma_pagesize, fault_granule;
+     long page_size, fault_granule;
      enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
      struct kvm_pgtable *pgt;
      struct page *page;

[...]

Quoted 4 lines
      /*
arch/arm64/kvm/mmu.c +0 -0
@@ -1600,9 +1600,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
       * ensure we find the right PFN and lay down the mapping in the right

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1607

Quoted 10 lines
arch/arm64/kvm/mmu.c +3 -3
       * place.
       */
-     if (vma_pagesize == PMD_SIZE || vma_pagesize == PUD_SIZE) {
-             fault_ipa &= ~(vma_pagesize - 1);
-             ipa &= ~(vma_pagesize - 1);
+     if (page_size == PMD_SIZE || page_size == PUD_SIZE) {
+             fault_ipa &= ~(page_size - 1);
+             ipa &= ~(page_size - 1);
      }
 

nit: since we're here for readability, ALIGN_DOWN() may be used:

Quoted 2 lines
                fault_ipa = ALIGN_DOWN(fault_ipa, page_size);
                ipa = ALIGN_DOWN(ipa, page_size);

Yes, but that would be a change beyond just renaming. I could introduce another patch to do that.

Thanks,
/fuad

Quoted 3 lines

Thanks,
Gavin

Reviewed-by on v9 12/17
#63 lore raw

Re: [PATCH v9 12/17] KVM: arm64: Rename variables in user_mem_abort()

In reply to Fuad Tabba
Quoted 7 lines · Fuad Tabba

Guest memory can be backed by guest_memfd or by anonymous memory. Rename vma_shift to page_shift and vma_pagesize to page_size to ease readability in subsequent patches.

---

Signature · 3 lines
Cheers,

David / dhildenb
author
#64 lore raw

PATCH v9 13/17 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     | 94 +++++++++++++++++++++++++---------------
 include/linux/kvm_host.h |  5 +++
 virt/kvm/kvm_main.c      |  5 ---
 3 files changed, 64 insertions(+), 40 deletions(-)
arch/arm64/kvm/mmu.c +59 -35
index d756c2b5913f..9a48ef08491d 100644
@@ -1466,6 +1466,30 @@ 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_gmem)
+{
+	kvm_pfn_t pfn;
+	int ret;
+
+	if (!is_gmem)
+		return __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page);
+
+	*writable = false;
+
+	ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, page, NULL);
+	if (!ret) {
+		*writable = !memslot_is_readonly(slot);
+		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,
@@ -1473,19 +1497,20 @@ 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 vm_area_struct *vma;
-	short page_shift;
+	struct vm_area_struct *vma = NULL;
+	short page_shift = PAGE_SHIFT;
 	void *memcache;
-	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 || is_protected_kvm_enabled();
-	long page_size, fault_granule;
+	bool is_gmem = kvm_slot_has_gmem(memslot);
+	bool force_pte = logging_active || is_gmem || is_protected_kvm_enabled();
+	long page_size, fault_granule = PAGE_SIZE;
 	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
 	struct kvm_pgtable *pgt;
 	struct page *page;
@@ -1529,17 +1554,20 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	 * 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_gmem) {
+		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;
+		}
+
+		vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
+		mte_allowed = kvm_vma_mte_allowed(vma);
 	}
 
-	if (force_pte)
-		page_shift = PAGE_SHIFT;
-	else
+	if (!force_pte)
 		page_shift = get_vma_page_shift(vma, hva);
 
 	switch (page_shift) {
@@ -1605,27 +1633,23 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		ipa &= ~(page_size - 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;
+	if (!is_gmem) {
+		/* 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.
-	 *
-	 * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs
-	 * with the smp_wmb() in kvm_mmu_invalidate_end().
-	 */
-	mmu_seq = vcpu->kvm->mmu_invalidate_seq;
-	mmap_read_unlock(current->mm);
+		/*
+		 * Read mmu_invalidate_seq so that KVM can detect if the results
+		 * of 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().
+		 */
+		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_gmem);
 	if (pfn == KVM_PFN_ERR_HWPOISON) {
 		kvm_send_hwpoison_signal(hva, page_shift);
 		return 0;
@@ -1677,7 +1701,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
 	kvm_fault_lock(kvm);
 	pgt = vcpu->arch.hw_mmu->pgt;
-	if (mmu_invalidate_retry(kvm, mmu_seq)) {
+	if (!is_gmem && mmu_invalidate_retry(kvm, mmu_seq)) {
 		ret = -EAGAIN;
 		goto out_unlock;
 	}
include/linux/kvm_host.h +5 -0
index f9bb025327c3..b317392453a5 100644
@@ -1884,6 +1884,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 6289ea1685dd..6261d8638cd2 100644
@@ -2640,11 +2640,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.49.0.1045.g170613ef41-goog
#65 lore raw

Re: [PATCH v9 13/17] KVM: arm64: Handle guest_memfd()-backed guest page faults

Quoted 33 lines · 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     | 94 +++++++++++++++++++++++++--------------- include/linux/kvm_host.h |  5 +++
 virt/kvm/kvm_main.c      |  5 ---

 3 files changed, 64 insertions(+), 40 deletions(-)
arch/arm64/kvm/mmu.c +12 -0
index d756c2b5913f..9a48ef08491d 100644
@@ -1466,6 +1466,30 @@ 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_gmem)
+{
+       kvm_pfn_t pfn;
+       int ret;
+
+       if (!is_gmem)
+               return __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page);
+
+       *writable = false;
+

arch/arm64/kvm/mmu.c static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) · line 1490

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+       ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, page, NULL);
+       if (!ret) {
+               *writable = !memslot_is_readonly(slot);
+               return pfn;
+       }
+
+       if (ret == -EHWPOISON)
+               return KVM_PFN_ERR_HWPOISON;
+
+       return KVM_PFN_ERR_NOSLOT_MASK;

I don't think the above handling for the ret != 0 case is correct. I think we should just be returning ret out to userspace.

The diff I have below is closer to what I think we must do.

Quoted 66 lines
arch/arm64/kvm/mmu.c +21 -15
+}
+
  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,
@@ -1473,19 +1497,20 @@ 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 vm_area_struct *vma;
-       short page_shift;
+       struct vm_area_struct *vma = NULL;
+       short page_shift = PAGE_SHIFT;
         void *memcache;
-       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 || is_protected_kvm_enabled();
-       long page_size, fault_granule;
+       bool is_gmem = kvm_slot_has_gmem(memslot);
+       bool force_pte = logging_active || is_gmem || is_protected_kvm_enabled();
+       long page_size, fault_granule = PAGE_SIZE;
         enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
         struct kvm_pgtable *pgt;
         struct page *page;
@@ -1529,17 +1554,20 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
          * 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_gmem) {
+               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;
+               }
+
+               vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
+               mte_allowed = kvm_vma_mte_allowed(vma);
         }
 
-       if (force_pte)
-               page_shift = PAGE_SHIFT;
-       else
+       if (!force_pte)
                 page_shift = get_vma_page_shift(vma, hva);
 
         switch (page_shift) {
@@ -1605,27 +1633,23 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
                 ipa &= ~(page_size - 1);
         }
 

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1638

Quoted 10 lines
arch/arm64/kvm/mmu.c +3 -7
-       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;
+       if (!is_gmem) {
+               /* Don't use the VMA after the unlock -- it may have vanished */
+               vma = NULL;

I think we can just move the vma declaration inside the earlier if (is_gmem) bit above. It should be really hard to accidentally attempt to use vma or hva in the is_gmem case. vma we can easily make it impossible; hva is harder.

See below for what I think this should look like.

Quoted 18 lines
arch/arm64/kvm/mmu.c +7 -10
-       /*
-        * 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.
-        *
-        * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs
-        * with the smp_wmb() in kvm_mmu_invalidate_end().
-        */
-       mmu_seq = vcpu->kvm->mmu_invalidate_seq;
-       mmap_read_unlock(current->mm);
+               /*
+                * Read mmu_invalidate_seq so that KVM can detect if the results
+                * of 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().

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1653

Quoted 10 lines
arch/arm64/kvm/mmu.c +5 -2
+                */
+               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_gmem);
         if (pfn == KVM_PFN_ERR_HWPOISON) {
                 kvm_send_hwpoison_signal(hva, page_shift);

hva is used here even for the is_gmem case, and that should be slightly concerning. And indeed it is, this is not the appropriate way to handle hwpoison for gmem (and it is different than the behavior you have for x86). x86 handles this by returning a KVM_MEMORY_FAULT_EXIT to userspace; we should do the same.

I've put what I think is more appropriate in the diff below.

And just to be clear, IMO, we *cannot* do what you have written now, especially given that we are getting rid of the userspace_addr sanity check (but that check was best-effort anyway).

Quoted 15 lines
arch/arm64/kvm/mmu.c +1 -1
                 return 0;
@@ -1677,7 +1701,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
         kvm_fault_lock(kvm);
         pgt = vcpu->arch.hw_mmu->pgt;
-       if (mmu_invalidate_retry(kvm, mmu_seq)) {
+       if (!is_gmem && mmu_invalidate_retry(kvm, mmu_seq)) {
                 ret = -EAGAIN;
                 goto out_unlock;
         }
include/linux/kvm_host.h +0 -0
index f9bb025327c3..b317392453a5 100644

include/linux/kvm_host.h static inline int memslot_id(struct kvm *kvm, gfn_t gfn) · line 1890

Quoted 7 lines
include/linux/kvm_host.h +4 -0
@@ -1884,6 +1884,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;
+}

I think if you're going to move this helper to include/linux/kvm_host.h, you might want to do so in its own patch and change all of the existing places where we check KVM_MEM_READONLY directly. *shrug*

Quoted 21 lines
include/linux/kvm_host.h +1 -0
+

 static inline gfn_t
 hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot) {

virt/kvm/kvm_main.c +0 -6
index 6289ea1685dd..6261d8638cd2 100644
@@ -2640,11 +2640,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)
  {
--
Quoted 2 lines

2.49.0.1045.g170613ef41-goog

Alright, here's the diff I have in mind:

arch/arm64/kvm/mmu.c +59 -53
index 9a48ef08491db..74eae19792373 100644
@@ -1466,28 +1466,30 @@ 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_gmem)
+static kvm_pfn_t faultin_pfn(struct kvm *kvm, struct kvm_vcpu *vcpu,
+			     struct kvm_memory_slot *slot, gfn_t gfn,
+			     bool exec_fault, bool write_fault, bool *writable,
+			     struct page **page, bool is_gmem, kvm_pfn_t *pfn)
 {
-	kvm_pfn_t pfn;
 	int ret;
 
-	if (!is_gmem)
-		return __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page);
+	if (!is_gmem) {
+		*pfn = __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page);
+		return 0;
+	}
 
 	*writable = false;
 
-	ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, page, NULL);
-	if (!ret) {
-		*writable = !memslot_is_readonly(slot);
-		return pfn;
+	ret = kvm_gmem_get_pfn(kvm, slot, gfn, pfn, page, NULL);
+	if (ret) {
+		kvm_prepare_memory_fault_exit(vcpu, gfn << PAGE_SHIFT,
+					      PAGE_SIZE, write_fault,
+					      exec_fault, false);
+		return ret;
 	}
 
-	if (ret == -EHWPOISON)
-		return KVM_PFN_ERR_HWPOISON;
-
-	return KVM_PFN_ERR_NOSLOT_MASK;
+	*writable = !memslot_is_readonly(slot);
+	return 0;
 }
 
 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
@@ -1502,7 +1504,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	unsigned long mmu_seq;
 	phys_addr_t ipa = fault_ipa;
 	struct kvm *kvm = vcpu->kvm;
-	struct vm_area_struct *vma = NULL;
 	short page_shift = PAGE_SHIFT;
 	void *memcache;
 	gfn_t gfn = ipa >> PAGE_SHIFT;
@@ -1555,6 +1556,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	 * get block mapping for device MMIO region.
 	 */
 	if (!is_gmem) {
+		struct vm_area_struct *vma = NULL;
+
 		mmap_read_lock(current->mm);
 		vma = vma_lookup(current->mm, hva);
 		if (unlikely(!vma)) {
@@ -1565,33 +1568,44 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
 		vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
 		mte_allowed = kvm_vma_mte_allowed(vma);
-	}
 
-	if (!force_pte)
-		page_shift = get_vma_page_shift(vma, hva);
+		if (!force_pte)
+			page_shift = get_vma_page_shift(vma, hva);
+
+		/*
+		 * Read mmu_invalidate_seq so that KVM can detect if the results
+		 * of 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().
+		 */
+		mmu_seq = vcpu->kvm->mmu_invalidate_seq;
+		mmap_read_unlock(current->mm);
 
-	switch (page_shift) {
+		switch (page_shift) {
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SHIFT:
-		if (fault_supports_stage2_huge_mapping(memslot, hva, PUD_SIZE))
-			break;
-		fallthrough;
+		case PUD_SHIFT:
+			if (fault_supports_stage2_huge_mapping(memslot, hva, PUD_SIZE))
+				break;
+			fallthrough;
 #endif
-	case CONT_PMD_SHIFT:
-		page_shift = PMD_SHIFT;
-		fallthrough;
-	case PMD_SHIFT:
-		if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE))
+		case CONT_PMD_SHIFT:
+			page_shift = PMD_SHIFT;
+			fallthrough;
+		case PMD_SHIFT:
+			if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE))
+				break;
+			fallthrough;
+		case CONT_PTE_SHIFT:
+			page_shift = PAGE_SHIFT;
+			force_pte = true;
+			fallthrough;
+		case PAGE_SHIFT:
 			break;
-		fallthrough;
-	case CONT_PTE_SHIFT:
-		page_shift = PAGE_SHIFT;
-		force_pte = true;
-		fallthrough;
-	case PAGE_SHIFT:
-		break;
-	default:
-		WARN_ONCE(1, "Unknown page_shift %d", page_shift);
+		default:
+			WARN_ONCE(1, "Unknown page_shift %d", page_shift);
+		}
 	}
 
 	page_size = 1UL << page_shift;
@@ -1633,24 +1647,16 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		ipa &= ~(page_size - 1);
 	}
 
-	if (!is_gmem) {
-		/* Don't use the VMA after the unlock -- it may have vanished */
-		vma = NULL;
-
+	ret = faultin_pfn(kvm, vcpu, memslot, gfn, exec_fault, write_fault,
+			  &writable, &page, is_gmem, &pfn);
+	if (ret)
+		return ret;
+	if (pfn == KVM_PFN_ERR_HWPOISON) {
 		/*
-		 * Read mmu_invalidate_seq so that KVM can detect if the results
-		 * of 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().
+		 * For gmem, hwpoison should be communicated via a memory fault
+		 * exit, not via a SIGBUS.
 		 */
-		mmu_seq = vcpu->kvm->mmu_invalidate_seq;
-		mmap_read_unlock(current->mm);
-	}
-
-	pfn = faultin_pfn(kvm, memslot, gfn, write_fault, &writable, &page, is_gmem);
-	if (pfn == KVM_PFN_ERR_HWPOISON) {
+		WARN_ON_ONCE(is_gmem);
 		kvm_send_hwpoison_signal(hva, page_shift);
 		return 0;
 	}
author
#66 lore raw

Re: [PATCH v9 13/17] KVM: arm64: Handle guest_memfd()-backed guest page faults

Hi James,

Quoted 40 lines · James Houghton
Quoted 38 lines · 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     | 94 +++++++++++++++++++++++++---------------
 include/linux/kvm_host.h |  5 +++
 virt/kvm/kvm_main.c      |  5 ---
 3 files changed, 64 insertions(+), 40 deletions(-)
arch/arm64/kvm/mmu.c +17 -0
index d756c2b5913f..9a48ef08491d 100644
@@ -1466,6 +1466,30 @@ 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_gmem)
+{
+       kvm_pfn_t pfn;
+       int ret;
+
+       if (!is_gmem)
+               return __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page);
+
+       *writable = false;
+
+       ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, page, NULL);
+       if (!ret) {
+               *writable = !memslot_is_readonly(slot);
+               return pfn;
+       }
Quoted 8 lines

arch/arm64/kvm/mmu.c static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) · line 1490

Quoted 5 lines
arch/arm64/kvm/mmu.c +5 -0
+
+       if (ret == -EHWPOISON)
+               return KVM_PFN_ERR_HWPOISON;
+
+       return KVM_PFN_ERR_NOSLOT_MASK;

I don't think the above handling for the ret != 0 case is correct. I think we should just be returning ret out to userspace.

Ack.

Quoted 69 lines

The diff I have below is closer to what I think we must do.

Quoted 66 lines
arch/arm64/kvm/mmu.c +21 -15
+}
+
 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,
@@ -1473,19 +1497,20 @@ 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 vm_area_struct *vma;
-       short page_shift;
+       struct vm_area_struct *vma = NULL;
+       short page_shift = PAGE_SHIFT;
        void *memcache;
-       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 || is_protected_kvm_enabled();
-       long page_size, fault_granule;
+       bool is_gmem = kvm_slot_has_gmem(memslot);
+       bool force_pte = logging_active || is_gmem || is_protected_kvm_enabled();
+       long page_size, fault_granule = PAGE_SIZE;
        enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
        struct kvm_pgtable *pgt;
        struct page *page;
@@ -1529,17 +1554,20 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
         * 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_gmem) {
+               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;
+               }
+
+               vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
+               mte_allowed = kvm_vma_mte_allowed(vma);
        }
 
-       if (force_pte)
-               page_shift = PAGE_SHIFT;
-       else
+       if (!force_pte)
                page_shift = get_vma_page_shift(vma, hva);
 
        switch (page_shift) {
@@ -1605,27 +1633,23 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
                ipa &= ~(page_size - 1);
        }
 
Quoted 15 lines

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1638

Quoted 10 lines
arch/arm64/kvm/mmu.c +3 -7
-       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;
+       if (!is_gmem) {
+               /* Don't use the VMA after the unlock -- it may have vanished */
+               vma = NULL;

I think we can just move the vma declaration inside the earlier if (is_gmem) bit above. It should be really hard to accidentally attempt to use vma or hva in the is_gmem case. vma we can easily make it impossible; hva is harder.

To be honest, I think we need to refactor user_mem_abort(). It's already a bit messy, and with the guest_memfd code, and in the (hopefully) soon, pkvm code, it's going to get messier. Some of the things things to keep in mind are, like you suggest, ensuring that vma and hva aren't in scope where they're not needed.

Quoted 32 lines

See below for what I think this should look like.

Quoted 18 lines
arch/arm64/kvm/mmu.c +7 -10
-       /*
-        * 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.
-        *
-        * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs
-        * with the smp_wmb() in kvm_mmu_invalidate_end().
-        */
-       mmu_seq = vcpu->kvm->mmu_invalidate_seq;
-       mmap_read_unlock(current->mm);
+               /*
+                * Read mmu_invalidate_seq so that KVM can detect if the results
+                * of 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().

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1653

Quoted 10 lines
arch/arm64/kvm/mmu.c +5 -2
+                */
+               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_gmem);
        if (pfn == KVM_PFN_ERR_HWPOISON) {
                kvm_send_hwpoison_signal(hva, page_shift);
Quoted 5 lines

hva is used here even for the is_gmem case, and that should be slightly concerning. And indeed it is, this is not the appropriate way to handle hwpoison for gmem (and it is different than the behavior you have for x86). x86 handles this by returning a KVM_MEMORY_FAULT_EXIT to userspace; we should do the same.

You're right. My initial thought was that by having a best-effort check that that would be enough, and not change the arm64 behavior all that much. Exiting to userspace is cleaner.

Quoted 24 lines

I've put what I think is more appropriate in the diff below.

And just to be clear, IMO, we *cannot* do what you have written now, especially given that we are getting rid of the userspace_addr sanity check (but that check was best-effort anyway).

Quoted 18 lines
arch/arm64/kvm/mmu.c +1 -1
                return 0;
@@ -1677,7 +1701,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
        kvm_fault_lock(kvm);
        pgt = vcpu->arch.hw_mmu->pgt;
-       if (mmu_invalidate_retry(kvm, mmu_seq)) {
+       if (!is_gmem && mmu_invalidate_retry(kvm, mmu_seq)) {
                ret = -EAGAIN;
                goto out_unlock;
        }
include/linux/kvm_host.h +0 -0
index f9bb025327c3..b317392453a5 100644
@@ -1884,6 +1884,11 @@ static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
        return gfn_to_memslot(kvm, gfn)->id;
 }
 
Quoted 8 lines

include/linux/kvm_host.h static inline int memslot_id(struct kvm *kvm, gfn_t gfn) · line 1890

Quoted 4 lines
include/linux/kvm_host.h +4 -0
+static inline bool memslot_is_readonly(const struct kvm_memory_slot *slot)
+{
+       return slot->flags & KVM_MEM_READONLY;
+}

I think if you're going to move this helper to include/linux/kvm_host.h, you might want to do so in its own patch and change all of the existing places where we check KVM_MEM_READONLY directly. *shrug*

It's a tough job, but someone's gotta do it :)

Quoted 13 lines
Quoted 13 lines
include/linux/kvm_host.h +1 -0
+
 static inline gfn_t
 hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
 {
virt/kvm/kvm_main.c +0 -0
index 6289ea1685dd..6261d8638cd2 100644
@@ -2640,11 +2640,6 @@ unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn)
        return size;
 }
 
Quoted 13 lines
Quoted 11 lines
virt/kvm/kvm_main.c +0 -6
-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)
 {
--

2.49.0.1045.g170613ef41-goog

Alright, here's the diff I have in mind:

Thank you James.

Cheers,
/fuad

Quoted 167 lines
arch/arm64/kvm/mmu.c +59 -53
index 9a48ef08491db..74eae19792373 100644
@@ -1466,28 +1466,30 @@ 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_gmem)
+static kvm_pfn_t faultin_pfn(struct kvm *kvm, struct kvm_vcpu *vcpu,
+                            struct kvm_memory_slot *slot, gfn_t gfn,
+                            bool exec_fault, bool write_fault, bool *writable,
+                            struct page **page, bool is_gmem, kvm_pfn_t *pfn)
 {
-       kvm_pfn_t pfn;
        int ret;
 
-       if (!is_gmem)
-               return __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page);
+       if (!is_gmem) {
+               *pfn = __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page);
+               return 0;
+       }
 
        *writable = false;
 
-       ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, page, NULL);
-       if (!ret) {
-               *writable = !memslot_is_readonly(slot);
-               return pfn;
+       ret = kvm_gmem_get_pfn(kvm, slot, gfn, pfn, page, NULL);
+       if (ret) {
+               kvm_prepare_memory_fault_exit(vcpu, gfn << PAGE_SHIFT,
+                                             PAGE_SIZE, write_fault,
+                                             exec_fault, false);
+               return ret;
        }
 
-       if (ret == -EHWPOISON)
-               return KVM_PFN_ERR_HWPOISON;
-
-       return KVM_PFN_ERR_NOSLOT_MASK;
+       *writable = !memslot_is_readonly(slot);
+       return 0;
 }
 
 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
@@ -1502,7 +1504,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
        unsigned long mmu_seq;
        phys_addr_t ipa = fault_ipa;
        struct kvm *kvm = vcpu->kvm;
-       struct vm_area_struct *vma = NULL;
        short page_shift = PAGE_SHIFT;
        void *memcache;
        gfn_t gfn = ipa >> PAGE_SHIFT;
@@ -1555,6 +1556,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
         * get block mapping for device MMIO region.
         */
        if (!is_gmem) {
+               struct vm_area_struct *vma = NULL;
+
                mmap_read_lock(current->mm);
                vma = vma_lookup(current->mm, hva);
                if (unlikely(!vma)) {
@@ -1565,33 +1568,44 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
                vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
                mte_allowed = kvm_vma_mte_allowed(vma);
-       }
 
-       if (!force_pte)
-               page_shift = get_vma_page_shift(vma, hva);
+               if (!force_pte)
+                       page_shift = get_vma_page_shift(vma, hva);
+
+               /*
+                * Read mmu_invalidate_seq so that KVM can detect if the results
+                * of 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().
+                */
+               mmu_seq = vcpu->kvm->mmu_invalidate_seq;
+               mmap_read_unlock(current->mm);
 
-       switch (page_shift) {
+               switch (page_shift) {
 #ifndef __PAGETABLE_PMD_FOLDED
-       case PUD_SHIFT:
-               if (fault_supports_stage2_huge_mapping(memslot, hva, PUD_SIZE))
-                       break;
-               fallthrough;
+               case PUD_SHIFT:
+                       if (fault_supports_stage2_huge_mapping(memslot, hva, PUD_SIZE))
+                               break;
+                       fallthrough;
 #endif
-       case CONT_PMD_SHIFT:
-               page_shift = PMD_SHIFT;
-               fallthrough;
-       case PMD_SHIFT:
-               if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE))
+               case CONT_PMD_SHIFT:
+                       page_shift = PMD_SHIFT;
+                       fallthrough;
+               case PMD_SHIFT:
+                       if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE))
+                               break;
+                       fallthrough;
+               case CONT_PTE_SHIFT:
+                       page_shift = PAGE_SHIFT;
+                       force_pte = true;
+                       fallthrough;
+               case PAGE_SHIFT:
                        break;
-               fallthrough;
-       case CONT_PTE_SHIFT:
-               page_shift = PAGE_SHIFT;
-               force_pte = true;
-               fallthrough;
-       case PAGE_SHIFT:
-               break;
-       default:
-               WARN_ONCE(1, "Unknown page_shift %d", page_shift);
+               default:
+                       WARN_ONCE(1, "Unknown page_shift %d", page_shift);
+               }
        }
 
        page_size = 1UL << page_shift;
@@ -1633,24 +1647,16 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
                ipa &= ~(page_size - 1);
        }
 
-       if (!is_gmem) {
-               /* Don't use the VMA after the unlock -- it may have vanished */
-               vma = NULL;
-
+       ret = faultin_pfn(kvm, vcpu, memslot, gfn, exec_fault, write_fault,
+                         &writable, &page, is_gmem, &pfn);
+       if (ret)
+               return ret;
+       if (pfn == KVM_PFN_ERR_HWPOISON) {
                /*
-                * Read mmu_invalidate_seq so that KVM can detect if the results
-                * of 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().
+                * For gmem, hwpoison should be communicated via a memory fault
+                * exit, not via a SIGBUS.
                 */
-               mmu_seq = vcpu->kvm->mmu_invalidate_seq;
-               mmap_read_unlock(current->mm);
-       }
-
-       pfn = faultin_pfn(kvm, memslot, gfn, write_fault, &writable, &page, is_gmem);
-       if (pfn == KVM_PFN_ERR_HWPOISON) {
+               WARN_ON_ONCE(is_gmem);
                kvm_send_hwpoison_signal(hva, page_shift);
                return 0;
        }
#67 lore raw

Re: [PATCH v9 13/17] KVM: arm64: Handle guest_memfd()-backed guest page faults

In reply to Fuad Tabba
Quoted 7 lines · 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.

---

[...]

Review

Quoted 1 line
(quoted diff) +1 -0
+	if (!is_gmem) {

Should we add a comment somewhere, stating that we don't support VMs with private memory, so if we have a gmem, all faults are routed through that?

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+		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;
+		}
+
+		vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
+		mte_allowed = kvm_vma_mte_allowed(vma);
Signature · 3 lines
Cheers,

David / dhildenb
author
#68 lore raw

Re: [PATCH v9 13/17] KVM: arm64: Handle guest_memfd()-backed guest page faults

Hi David,

Quoted 10 lines · David Hildenbrand
Quoted 7 lines · 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.

---

Quoted 7 lines

[...]

Quoted 1 line

+ if (!is_gmem) {

Should we add a comment somewhere, stating that we don't support VMs with private memory, so if we have a gmem, all faults are routed through that?

I guess this is related to the other thread we had. This would handle private memory correctly. It's just that for arm64 as it is, having private memory isn't that useful.

There might be a use-case where a user would create a guest_memfd-backed slot that supports private memory, and one that doesn't, which only the guest would use. I doubt that that's actually useful, but it would work and behave as expected.

Cheers,
/fuad

Quoted 16 lines

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1567

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+             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;
+             }
+
+             vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
+             mte_allowed = kvm_vma_mte_allowed(vma);

--
Cheers,

David / dhildenb

author
#69 lore raw

PATCH v9 14/17 KVM: arm64: Enable mapping guest_memfd in arm64

In reply to Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
 arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
 arch/arm64/kvm/Kconfig            |  1 +
 2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
 	return true;
 }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+	return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+	return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +1 -0
index 096e45acadb2..8c1e1964b46a 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.49.0.1045.g170613ef41-goog
#70 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

kernel test robot noticed the following build errors:

Hi Fuad,

kernel test robot noticed the following build errors:

[auto build test ERROR on 82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3]

url:    https://github.com/intel-lab-lkp/linux/commits/Fuad-Tabba/KVM-Rename-CONFIG_KVM_PRIVATE_MEM-to-CONFIG_KVM_GMEM/20250514-003900
base:   82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3
patch link:    https://lore.kernel.org/r/20250513163438.3942405-15-tabba%40google.com
patch subject: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64
config: arm64-randconfig-004-20250514 (https://download.01.org/0day-ci/archive/20250514/[email protected]/config)
compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project f819f46284f2a79790038e1f6649172789734ae8)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250514/[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 arch/arm64/kernel/asm-offsets.c:15:

>> include/linux/kvm_host.h:726:20: error: redefinition of 'kvm_arch_supports_gmem'
     726 | static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
         |                    ^
   arch/arm64/include/asm/kvm_host.h:1596:20: note: previous definition is here
    1596 | static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
         |                    ^
   In file included from arch/arm64/kernel/asm-offsets.c:15:
>> include/linux/kvm_host.h:739:20: error: redefinition of 'kvm_arch_vm_supports_gmem_shared_mem'

739 | static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
| ^
arch/arm64/include/asm/kvm_host.h:1601:20: note: previous definition is here
1601 | static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
| ^
2 errors generated.
make[3]: *** [scripts/Makefile.build:98: arch/arm64/kernel/asm-offsets.s] Error 1 shuffle=2236105568 make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1275: prepare0] Error 2 shuffle=2236105568
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:248: __sub-make] Error 2 shuffle=2236105568
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2 shuffle=2236105568
make: Target 'prepare' not remade because of errors.

vim +/kvm_arch_supports_gmem +726 include/linux/kvm_host.h

f481b069e67437 Paolo Bonzini       2015-05-17  720  
a7800aa80ea4d5 Sean Christopherson 2023-11-13  721  /*
fc8463899419fd Fuad Tabba          2025-05-13  722   * Arch code must define kvm_arch_supports_gmem if support for guest_memfd
a7800aa80ea4d5 Sean Christopherson 2023-11-13  723   * is enabled.
a7800aa80ea4d5 Sean Christopherson 2023-11-13  724   */
fc8463899419fd Fuad Tabba          2025-05-13  725  #if !defined(kvm_arch_supports_gmem) && !IS_ENABLED(CONFIG_KVM_GMEM)
fc8463899419fd Fuad Tabba          2025-05-13 @726  static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
a7800aa80ea4d5 Sean Christopherson 2023-11-13  727  {
a7800aa80ea4d5 Sean Christopherson 2023-11-13  728  	return false;
a7800aa80ea4d5 Sean Christopherson 2023-11-13  729  }
a7800aa80ea4d5 Sean Christopherson 2023-11-13  730  #endif
a7800aa80ea4d5 Sean Christopherson 2023-11-13  731  
33a8543055465e Fuad Tabba          2025-05-13  732  /*
33a8543055465e Fuad Tabba          2025-05-13  733   * Returns true if this VM supports shared mem in guest_memfd.
33a8543055465e Fuad Tabba          2025-05-13  734   *
33a8543055465e Fuad Tabba          2025-05-13  735   * Arch code must define kvm_arch_vm_supports_gmem_shared_mem if support for
33a8543055465e Fuad Tabba          2025-05-13  736   * guest_memfd is enabled.
33a8543055465e Fuad Tabba          2025-05-13  737   */
33a8543055465e Fuad Tabba          2025-05-13  738  #if !defined(kvm_arch_vm_supports_gmem_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM)
33a8543055465e Fuad Tabba          2025-05-13 @739  static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
33a8543055465e Fuad Tabba          2025-05-13  740  {
33a8543055465e Fuad Tabba          2025-05-13  741  	return false;
33a8543055465e Fuad Tabba          2025-05-13  742  }
33a8543055465e Fuad Tabba          2025-05-13  743  #endif
33a8543055465e Fuad Tabba          2025-05-13  744  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#71 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

In reply to Fuad Tabba
Quoted 16 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
 arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
 arch/arm64/kvm/Kconfig            |  1 +
 2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +0 -0
index 08ba91e6fb03..2514779f5131 100644

arch/arm64/include/asm/kvm_host.h static inline bool kvm_arch_has_irq_bypass(void) · line 1599

Quoted 7 lines
arch/arm64/include/asm/kvm_host.h +4 -0
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
        return true;
 }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+       return IS_ENABLED(CONFIG_KVM_GMEM);
+}

This is written as if it is okay for CONFIG_KVM_GMEM not to be enabled, but when disabling CONFIG_KVM_GMEM you will get an error for redefining kvm_arch_supports_gmem().

I think you either want to include:

#define kvm_arch_supports_gmem kvm_arch_supports_gmem

or just do something closer to what x86 does:

#ifdef CONFIG_KVM_GMEM
#define kvm_arch_supports_gmem(kvm) true
#endif

arch/arm64/include/asm/kvm_host.h static inline bool kvm_arch_has_irq_bypass(void) · line 1604

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +5 -0
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+       return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}

And this applies here as well.

#define kvm_arch_vm_supports_gmem_shared_mem
kvm_arch_vm_supports_gmem_shared_mem

or

#ifdef CONFIG_KVM_GMEM
#define kvm_arch_vm_supports_gmem_shared_mem(kvm)
IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
#endif

arch/arm64/kvm/Kconfig menuconfig KVM · line 41

Quoted 11 lines
arch/arm64/include/asm/kvm_host.h +1 -0
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +1 -0
index 096e45acadb2..8c1e1964b46a 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

This makes it impossible to see the error, but I think we should fix it anyway. :)

Quoted 6 lines
arch/arm64/kvm/Kconfig +0 -1
        help
          Support hosting virtualized guest machines.
 
--

2.49.0.1045.g170613ef41-goog

author
#72 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

Hi James,

Quoted 57 lines · James Houghton
Quoted 16 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
 arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
 arch/arm64/kvm/Kconfig            |  1 +
 2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +0 -0
index 08ba91e6fb03..2514779f5131 100644

arch/arm64/include/asm/kvm_host.h static inline bool kvm_arch_has_irq_bypass(void) · line 1599

Quoted 7 lines
arch/arm64/include/asm/kvm_host.h +4 -0
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
        return true;
 }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+       return IS_ENABLED(CONFIG_KVM_GMEM);
+}

This is written as if it is okay for CONFIG_KVM_GMEM not to be enabled, but when disabling CONFIG_KVM_GMEM you will get an error for redefining kvm_arch_supports_gmem().

I think you either want to include:

#define kvm_arch_supports_gmem kvm_arch_supports_gmem

or just do something closer to what x86 does:

#ifdef CONFIG_KVM_GMEM
#define kvm_arch_supports_gmem(kvm) true
#endif

arch/arm64/include/asm/kvm_host.h static inline bool kvm_arch_has_irq_bypass(void) · line 1604

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +5 -0
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+       return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}

And this applies here as well.

#define kvm_arch_vm_supports_gmem_shared_mem
kvm_arch_vm_supports_gmem_shared_mem

or

#ifdef CONFIG_KVM_GMEM
#define kvm_arch_vm_supports_gmem_shared_mem(kvm)
IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
#endif

Quoted 14 lines

arch/arm64/kvm/Kconfig menuconfig KVM · line 41

Quoted 11 lines
arch/arm64/include/asm/kvm_host.h +1 -0
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +1 -0
index 096e45acadb2..8c1e1964b46a 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

This makes it impossible to see the error, but I think we should fix it anyway. :)

Ack.

Thank you!
/fuad

Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/Kconfig +0 -1
        help
          Support hosting virtualized guest machines.
 
--

2.49.0.1045.g170613ef41-goog

#73 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

In reply to Fuad Tabba
Quoted 34 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
  arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
  arch/arm64/kvm/Kconfig            |  1 +
  2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
  	return true;
  }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+	return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+	return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
  #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +0 -0
index 096e45acadb2..8c1e1964b46a 100644

arch/arm64/kvm/Kconfig menuconfig KVM · line 44

Quoted 7 lines
arch/arm64/kvm/Kconfig +1 -0
@@ -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.
 

Do we have to reject somewhere if we are given a guest_memfd that was *not* created using the SHARED flag? Or will existing checks already reject that?

Signature · 3 lines
Cheers,

David / dhildenb
author
#74 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

Hi David,

Quoted 39 lines · David Hildenbrand
Quoted 37 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
  arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
  arch/arm64/kvm/Kconfig            |  1 +
  2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
      return true;
  }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
  #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +0 -0
index 096e45acadb2..8c1e1964b46a 100644
@@ -38,6 +38,7 @@ menuconfig KVM
      select HAVE_KVM_VCPU_RUN_PID_CHANGE
      select SCHED_INFO
      select GUEST_PERF_EVENTS if PERF_EVENTS
Quoted 8 lines

arch/arm64/kvm/Kconfig menuconfig KVM · line 44

Quoted 4 lines
arch/arm64/kvm/Kconfig +1 -0
+     select KVM_GMEM_SHARED_MEM
      help
        Support hosting virtualized guest machines.
 

Do we have to reject somewhere if we are given a guest_memfd that was *not* created using the SHARED flag? Or will existing checks already reject that?

We don't reject, but I don't think we need to. A user can create a guest_memfd that's private in arm64, it would just be useless.

Cheers,
/fuad

Quoted 5 lines

--
Cheers,

David / dhildenb

#75 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

Quoted 51 lines · Fuad Tabba

Hi David,

Quoted 39 lines · David Hildenbrand
Quoted 37 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
   arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
   arch/arm64/kvm/Kconfig            |  1 +
   2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
       return true;
   }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
   #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +0 -0
index 096e45acadb2..8c1e1964b46a 100644
@@ -38,6 +38,7 @@ menuconfig KVM
       select HAVE_KVM_VCPU_RUN_PID_CHANGE
       select SCHED_INFO
       select GUEST_PERF_EVENTS if PERF_EVENTS
Quoted 8 lines

arch/arm64/kvm/Kconfig menuconfig KVM · line 44

Quoted 4 lines
arch/arm64/kvm/Kconfig +1 -0
+     select KVM_GMEM_SHARED_MEM
       help
         Support hosting virtualized guest machines.
 

Do we have to reject somewhere if we are given a guest_memfd that was *not* created using the SHARED flag? Or will existing checks already reject that?

Quoted 2 lines

We don't reject, but I don't think we need to. A user can create a guest_memfd that's private in arm64, it would just be useless.

But the arm64 fault routine would not be able to handle that properly, no?

Signature · 3 lines
Cheers,

David / dhildenb
author
#76 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

Quoted 53 lines · David Hildenbrand
Quoted 51 lines · Fuad Tabba

Hi David,

Quoted 47 lines · David Hildenbrand
Quoted 34 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
   arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
   arch/arm64/kvm/Kconfig            |  1 +
   2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
       return true;
   }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
   #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +0 -0
index 096e45acadb2..8c1e1964b46a 100644

arch/arm64/kvm/Kconfig menuconfig KVM · line 44

Quoted 7 lines
arch/arm64/kvm/Kconfig +1 -0
@@ -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.
 

Do we have to reject somewhere if we are given a guest_memfd that was *not* created using the SHARED flag? Or will existing checks already reject that?

Quoted 4 lines
Quoted 2 lines

We don't reject, but I don't think we need to. A user can create a guest_memfd that's private in arm64, it would just be useless.

But the arm64 fault routine would not be able to handle that properly, no?

Actually it would. The function user_mem_abort() doesn't care whether it's private or shared. It would fault it into the guest correctly regardless.

Thanks,
/fuad

Quoted 5 lines

--
Cheers,

David / dhildenb

#77 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

Quoted 54 lines · Fuad Tabba
Quoted 53 lines · David Hildenbrand
Quoted 51 lines · Fuad Tabba

Hi David,

Quoted 47 lines · David Hildenbrand
Quoted 34 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
    arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
    arch/arm64/kvm/Kconfig            |  1 +
    2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
        return true;
    }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
    #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +0 -0
index 096e45acadb2..8c1e1964b46a 100644

arch/arm64/kvm/Kconfig menuconfig KVM · line 44

Quoted 7 lines
arch/arm64/kvm/Kconfig +1 -0
@@ -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.
 

Do we have to reject somewhere if we are given a guest_memfd that was *not* created using the SHARED flag? Or will existing checks already reject that?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

We don't reject, but I don't think we need to. A user can create a guest_memfd that's private in arm64, it would just be useless.

But the arm64 fault routine would not be able to handle that properly, no?

Actually it would. The function user_mem_abort() doesn't care whether it's private or shared. It would fault it into the guest correctly regardless.

I think what I meant is that: if it's !shared (private only), shared accesses (IOW all access without CoCo) should be taken from the user space mapping.

But user_mem_abort() would blindly go to kvm_gmem_get_pfn() because "is_gmem = kvm_slot_has_gmem(memslot) = true".

In other words, arm64 would have to *ignore* guest_memfd that does not support shared?

That's why I was wondering whether we should just immediately refuse such guest_memfds.

Signature · 3 lines
Cheers,

David / dhildenb
author
#78 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

Hi David,

Quoted 66 lines · David Hildenbrand
Quoted 54 lines · Fuad Tabba
Quoted 53 lines · David Hildenbrand
Quoted 51 lines · Fuad Tabba

Hi David,

Quoted 47 lines · David Hildenbrand
Quoted 34 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
    arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
    arch/arm64/kvm/Kconfig            |  1 +
    2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
        return true;
    }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
    #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +0 -0
index 096e45acadb2..8c1e1964b46a 100644

arch/arm64/kvm/Kconfig menuconfig KVM · line 44

Quoted 7 lines
arch/arm64/kvm/Kconfig +1 -0
@@ -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.
 

Do we have to reject somewhere if we are given a guest_memfd that was *not* created using the SHARED flag? Or will existing checks already reject that?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

We don't reject, but I don't think we need to. A user can create a guest_memfd that's private in arm64, it would just be useless.

But the arm64 fault routine would not be able to handle that properly, no?

Actually it would. The function user_mem_abort() doesn't care whether it's private or shared. It would fault it into the guest correctly regardless.

Quoted 6 lines

I think what I meant is that: if it's !shared (private only), shared accesses (IOW all access without CoCo) should be taken from the user space mapping.

But user_mem_abort() would blindly go to kvm_gmem_get_pfn() because "is_gmem = kvm_slot_has_gmem(memslot) = true".

Yes, since it is a gmem-backed slot.

Quoted 5 lines

In other words, arm64 would have to *ignore* guest_memfd that does not support shared?

That's why I was wondering whether we should just immediately refuse such guest_memfds.

My thinking is that if a user deliberately creates a guest_memfd-backed slot without designating it as being sharable, then either they would find out when they try to map that memory to the host userspace (mapping it would fail), or it could be that they deliberately want to set up a VM with memslots that not mappable at all by the host. Perhaps to add some layer of security (although a very flimsy one, since it's not a confidential guest).

I'm happy to a check to prevent this. The question is, how to do it exactly (I assume it would be in kvm_gmem_create())? Would it be arch-specific, i.e., prevent arm64 from creating non-shared guest_memfd backed memslots? Or do it by VM type? Even if we do it by VM-type it would need to be arch-specific, since we allow private guest_memfd slots for the default VM in x86, but we wouldn't for arm64.

We could add another function, along the lines of kvm_arch_supports_gmem_only_shared_mem(), but considering that it actually works, and (arguably) would behave as intended, I'm not sure if it's worth the complexity.

What do you think?

Cheers,
/fuad

Quoted 6 lines

--
Cheers,

David / dhildenb

#79 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

Quoted 84 lines · Fuad Tabba

Hi David,

Quoted 66 lines · David Hildenbrand
Quoted 54 lines · Fuad Tabba
Quoted 53 lines · David Hildenbrand
Quoted 51 lines · Fuad Tabba

Hi David,

Quoted 47 lines · David Hildenbrand
Quoted 34 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
     arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
     arch/arm64/kvm/Kconfig            |  1 +
     2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
         return true;
     }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
     #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +0 -0
index 096e45acadb2..8c1e1964b46a 100644

arch/arm64/kvm/Kconfig menuconfig KVM · line 44

Quoted 7 lines
arch/arm64/kvm/Kconfig +1 -0
@@ -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.
 

Do we have to reject somewhere if we are given a guest_memfd that was *not* created using the SHARED flag? Or will existing checks already reject that?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

We don't reject, but I don't think we need to. A user can create a guest_memfd that's private in arm64, it would just be useless.

But the arm64 fault routine would not be able to handle that properly, no?

Actually it would. The function user_mem_abort() doesn't care whether it's private or shared. It would fault it into the guest correctly regardless.

Quoted 6 lines

I think what I meant is that: if it's !shared (private only), shared accesses (IOW all access without CoCo) should be taken from the user space mapping.

But user_mem_abort() would blindly go to kvm_gmem_get_pfn() because "is_gmem = kvm_slot_has_gmem(memslot) = true".

Yes, since it is a gmem-backed slot.

Quoted 5 lines

In other words, arm64 would have to *ignore* guest_memfd that does not support shared?

That's why I was wondering whether we should just immediately refuse such guest_memfds.

Quoted 6 lines

My thinking is that if a user deliberately creates a guest_memfd-backed slot without designating it as being sharable, then either they would find out when they try to map that memory to the host userspace (mapping it would fail), or it could be that they deliberately want to set up a VM with memslots that not mappable at all by the host.

Hm. But that would meant that we interpret "private" memory as a concept that is not understood by the VM. Because the VM does not know what "private" memory is ...

Quoted 2 lines

Perhaps to add some layer of security (although a very flimsy one, since it's not a confidential guest).

Exactly my point. If you don't want to mmap it then ... don't mmap it :)

Quoted 9 lines

I'm happy to a check to prevent this. The question is, how to do it exactly (I assume it would be in kvm_gmem_create())? Would it be arch-specific, i.e., prevent arm64 from creating non-shared guest_memfd backed memslots? Or do it by VM type? Even if we do it by VM-type it would need to be arch-specific, since we allow private guest_memfd slots for the default VM in x86, but we wouldn't for arm64.

Quoted 6 lines

We could add another function, along the lines of kvm_arch_supports_gmem_only_shared_mem(), but considering that it actually works, and (arguably) would behave as intended, I'm not sure if it's worth the complexity.

What do you think?

My thinking was to either block this at slot creation time or at guest_memfd creation time. And we should probably block that for other VM types as well that do not support private memory?

I mean, creating guest_memfd for private memory when there is no concept of private memory for the VM is ... weird, no? :)

Signature · 3 lines
Cheers,

David / dhildenb
author
#80 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

Hi David,

Quoted 116 lines · David Hildenbrand
Quoted 84 lines · Fuad Tabba

Hi David,

Quoted 66 lines · David Hildenbrand
Quoted 54 lines · Fuad Tabba
Quoted 53 lines · David Hildenbrand
Quoted 51 lines · Fuad Tabba

Hi David,

Quoted 47 lines · David Hildenbrand
Quoted 34 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
     arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
     arch/arm64/kvm/Kconfig            |  1 +
     2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
         return true;
     }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
     #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +0 -0
index 096e45acadb2..8c1e1964b46a 100644

arch/arm64/kvm/Kconfig menuconfig KVM · line 44

Quoted 7 lines
arch/arm64/kvm/Kconfig +1 -0
@@ -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.
 

Do we have to reject somewhere if we are given a guest_memfd that was *not* created using the SHARED flag? Or will existing checks already reject that?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

We don't reject, but I don't think we need to. A user can create a guest_memfd that's private in arm64, it would just be useless.

But the arm64 fault routine would not be able to handle that properly, no?

Actually it would. The function user_mem_abort() doesn't care whether it's private or shared. It would fault it into the guest correctly regardless.

Quoted 6 lines

I think what I meant is that: if it's !shared (private only), shared accesses (IOW all access without CoCo) should be taken from the user space mapping.

But user_mem_abort() would blindly go to kvm_gmem_get_pfn() because "is_gmem = kvm_slot_has_gmem(memslot) = true".

Yes, since it is a gmem-backed slot.

Quoted 5 lines

In other words, arm64 would have to *ignore* guest_memfd that does not support shared?

That's why I was wondering whether we should just immediately refuse such guest_memfds.

Quoted 6 lines

My thinking is that if a user deliberately creates a guest_memfd-backed slot without designating it as being sharable, then either they would find out when they try to map that memory to the host userspace (mapping it would fail), or it could be that they deliberately want to set up a VM with memslots that not mappable at all by the host.

Hm. But that would meant that we interpret "private" memory as a concept that is not understood by the VM. Because the VM does not know what "private" memory is ...

Quoted 2 lines

Perhaps to add some layer of security (although a very flimsy one, since it's not a confidential guest).

Exactly my point. If you don't want to mmap it then ... don't mmap it :)

Quoted 14 lines

I'm happy to a check to prevent this. The question is, how to do it exactly (I assume it would be in kvm_gmem_create())? Would it be arch-specific, i.e., prevent arm64 from creating non-shared guest_memfd backed memslots? Or do it by VM type? Even if we do it by VM-type it would need to be arch-specific, since we allow private guest_memfd slots for the default VM in x86, but we wouldn't for arm64.

We could add another function, along the lines of kvm_arch_supports_gmem_only_shared_mem(), but considering that it actually works, and (arguably) would behave as intended, I'm not sure if it's worth the complexity.

Quoted 8 lines
Quoted 1 line

What do you think?

My thinking was to either block this at slot creation time or at guest_memfd creation time. And we should probably block that for other VM types as well that do not support private memory?

I mean, creating guest_memfd for private memory when there is no concept of private memory for the VM is ... weird, no? :)

Actually, I could add this as an arch-specific check in arch/arm64/kvm/mmu.c:kvm_arch_prepare_memory_region(). That way, core KVM/guest_memfd code doesn't need to handle this arm64-specific behavior.

Does that sound good?

Thanks,
/fuad

Quoted 5 lines

--
Cheers,

David / dhildenb

#81 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

Quoted 125 lines · Fuad Tabba

Hi David,

Quoted 122 lines · David Hildenbrand
Quoted 84 lines · Fuad Tabba

Hi David,

Quoted 66 lines · David Hildenbrand
Quoted 54 lines · Fuad Tabba
Quoted 53 lines · David Hildenbrand
Quoted 51 lines · Fuad Tabba

Hi David,

Quoted 47 lines · David Hildenbrand
Quoted 34 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
      arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
      arch/arm64/kvm/Kconfig            |  1 +
      2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
          return true;
      }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
      #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +0 -0
index 096e45acadb2..8c1e1964b46a 100644

arch/arm64/kvm/Kconfig menuconfig KVM · line 44

Quoted 7 lines
arch/arm64/kvm/Kconfig +1 -0
@@ -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.
 

Do we have to reject somewhere if we are given a guest_memfd that was *not* created using the SHARED flag? Or will existing checks already reject that?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

We don't reject, but I don't think we need to. A user can create a guest_memfd that's private in arm64, it would just be useless.

But the arm64 fault routine would not be able to handle that properly, no?

Actually it would. The function user_mem_abort() doesn't care whether it's private or shared. It would fault it into the guest correctly regardless.

Quoted 6 lines

I think what I meant is that: if it's !shared (private only), shared accesses (IOW all access without CoCo) should be taken from the user space mapping.

But user_mem_abort() would blindly go to kvm_gmem_get_pfn() because "is_gmem = kvm_slot_has_gmem(memslot) = true".

Yes, since it is a gmem-backed slot.

Quoted 5 lines

In other words, arm64 would have to *ignore* guest_memfd that does not support shared?

That's why I was wondering whether we should just immediately refuse such guest_memfds.

Quoted 6 lines

My thinking is that if a user deliberately creates a guest_memfd-backed slot without designating it as being sharable, then either they would find out when they try to map that memory to the host userspace (mapping it would fail), or it could be that they deliberately want to set up a VM with memslots that not mappable at all by the host.

Hm. But that would meant that we interpret "private" memory as a concept that is not understood by the VM. Because the VM does not know what "private" memory is ...

Quoted 2 lines

Perhaps to add some layer of security (although a very flimsy one, since it's not a confidential guest).

Exactly my point. If you don't want to mmap it then ... don't mmap it :)

Quoted 9 lines

I'm happy to a check to prevent this. The question is, how to do it exactly (I assume it would be in kvm_gmem_create())? Would it be arch-specific, i.e., prevent arm64 from creating non-shared guest_memfd backed memslots? Or do it by VM type? Even if we do it by VM-type it would need to be arch-specific, since we allow private guest_memfd slots for the default VM in x86, but we wouldn't for arm64.

Quoted 6 lines

We could add another function, along the lines of kvm_arch_supports_gmem_only_shared_mem(), but considering that it actually works, and (arguably) would behave as intended, I'm not sure if it's worth the complexity.

What do you think?

My thinking was to either block this at slot creation time or at guest_memfd creation time. And we should probably block that for other VM types as well that do not support private memory?

Quoted 8 lines
Quoted 2 lines

I mean, creating guest_memfd for private memory when there is no concept of private memory for the VM is ... weird, no? :)

Actually, I could add this as an arch-specific check in arch/arm64/kvm/mmu.c:kvm_arch_prepare_memory_region(). That way, core KVM/guest_memfd code doesn't need to handle this arm64-specific behavior.

Does that sound good?

Yes, but only do so if you agree.

Signature · 3 lines
Cheers,

David / dhildenb
author
#82 lore raw

Re: [PATCH v9 14/17] KVM: arm64: Enable mapping guest_memfd in arm64

Quoted 130 lines · David Hildenbrand
Quoted 128 lines · Fuad Tabba

Hi David,

Quoted 124 lines · David Hildenbrand
Quoted 84 lines · Fuad Tabba

Hi David,

Quoted 66 lines · David Hildenbrand
Quoted 54 lines · Fuad Tabba
Quoted 53 lines · David Hildenbrand
Quoted 51 lines · Fuad Tabba

Hi David,

Quoted 47 lines · David Hildenbrand
Quoted 34 lines · Fuad Tabba

Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem().

---
      arch/arm64/include/asm/kvm_host.h | 10 ++++++++++
      arch/arm64/kvm/Kconfig            |  1 +
      2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +10 -0
index 08ba91e6fb03..2514779f5131 100644
@@ -1593,4 +1593,14 @@ static inline bool kvm_arch_has_irq_bypass(void)
          return true;
      }
 
+static inline bool kvm_arch_supports_gmem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM);
+}
+
+static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm)
+{
+     return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM);
+}
+
      #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +0 -0
index 096e45acadb2..8c1e1964b46a 100644

arch/arm64/kvm/Kconfig menuconfig KVM · line 44

Quoted 7 lines
arch/arm64/kvm/Kconfig +1 -0
@@ -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.
 

Do we have to reject somewhere if we are given a guest_memfd that was *not* created using the SHARED flag? Or will existing checks already reject that?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

We don't reject, but I don't think we need to. A user can create a guest_memfd that's private in arm64, it would just be useless.

But the arm64 fault routine would not be able to handle that properly, no?

Actually it would. The function user_mem_abort() doesn't care whether it's private or shared. It would fault it into the guest correctly regardless.

Quoted 6 lines

I think what I meant is that: if it's !shared (private only), shared accesses (IOW all access without CoCo) should be taken from the user space mapping.

But user_mem_abort() would blindly go to kvm_gmem_get_pfn() because "is_gmem = kvm_slot_has_gmem(memslot) = true".

Yes, since it is a gmem-backed slot.

Quoted 5 lines

In other words, arm64 would have to *ignore* guest_memfd that does not support shared?

That's why I was wondering whether we should just immediately refuse such guest_memfds.

Quoted 6 lines

My thinking is that if a user deliberately creates a guest_memfd-backed slot without designating it as being sharable, then either they would find out when they try to map that memory to the host userspace (mapping it would fail), or it could be that they deliberately want to set up a VM with memslots that not mappable at all by the host.

Hm. But that would meant that we interpret "private" memory as a concept that is not understood by the VM. Because the VM does not know what "private" memory is ...

Quoted 2 lines

Perhaps to add some layer of security (although a very flimsy one, since it's not a confidential guest).

Exactly my point. If you don't want to mmap it then ... don't mmap it :)

Quoted 9 lines

I'm happy to a check to prevent this. The question is, how to do it exactly (I assume it would be in kvm_gmem_create())? Would it be arch-specific, i.e., prevent arm64 from creating non-shared guest_memfd backed memslots? Or do it by VM type? Even if we do it by VM-type it would need to be arch-specific, since we allow private guest_memfd slots for the default VM in x86, but we wouldn't for arm64.

Quoted 6 lines

We could add another function, along the lines of kvm_arch_supports_gmem_only_shared_mem(), but considering that it actually works, and (arguably) would behave as intended, I'm not sure if it's worth the complexity.

What do you think?

My thinking was to either block this at slot creation time or at guest_memfd creation time. And we should probably block that for other VM types as well that do not support private memory?

I mean, creating guest_memfd for private memory when there is no concept of private memory for the VM is ... weird, no? :)

Quoted 7 lines
Quoted 5 lines

Actually, I could add this as an arch-specific check in arch/arm64/kvm/mmu.c:kvm_arch_prepare_memory_region(). That way, core KVM/guest_memfd code doesn't need to handle this arm64-specific behavior.

Does that sound good?

Yes, but only do so if you agree.

Ack :)

/fuad

Quoted 5 lines

--
Cheers,

David / dhildenb

author
#83 lore raw

PATCH v9 15/17 KVM: Introduce the KVM capability KVM_CAP_GMEM_SHARED_MEM

In reply to Fuad Tabba

This patch introduces the KVM capability KVM_CAP_GMEM_SHARED_MEM, which indicates that guest_memfd supports shared memory (when enabled by the flag). This support is limited to certain VM types, determined per architecture.

This patch also updates the KVM documentation with details on the new capability, flag, and other information about support for shared memory in guest_memfd.

---
 Documentation/virt/kvm/api.rst | 18 ++++++++++++++++++
 include/uapi/linux/kvm.h       |  1 +
 virt/kvm/kvm_main.c            |  4 ++++
 3 files changed, 23 insertions(+)
Documentation/virt/kvm/api.rst +18 -0
index 47c7c3f92314..86f74ce7f12a 100644
@@ -6390,6 +6390,24 @@ most one mapping per page, i.e. binding multiple memory regions to a single
 guest_memfd range is not allowed (any number of memory regions can be bound to
 a single guest_memfd file, but the bound ranges must not overlap).
 
+When the capability KVM_CAP_GMEM_SHARED_MEM is supported, the 'flags' field
+supports GUEST_MEMFD_FLAG_SUPPORT_SHARED.  Setting this flag on guest_memfd
+creation enables mmap() and faulting of guest_memfd memory to host userspace.
+
+When the KVM MMU performs a PFN lookup to service a guest fault and the backing
+guest_memfd has the GUEST_MEMFD_FLAG_SUPPORT_SHARED set, then the fault will
+always be consumed from guest_memfd, regardless of whether it is a shared or a
+private fault.
+
+For these memslots, userspace_addr is checked to be the mmap()-ed view of the
+same range specified using gmem.pgoff.  Other accesses by KVM, e.g., instruction
+emulation, go via slot->userspace_addr.  The slot->userspace_addr field can be
+set to 0 to skip this check, which indicates that KVM would not access memory
+belonging to the slot via its userspace_addr.
+
+The use of GUEST_MEMFD_FLAG_SUPPORT_SHARED will not be allowed for CoCo VMs.
+This is validated when the guest_memfd instance is bound to the VM.
+
 See KVM_SET_USER_MEMORY_REGION2 for additional details.
 
 4.143 KVM_PRE_FAULT_MEMORY
include/uapi/linux/kvm.h +1 -0
index 9857022a0f0c..4cc824a3a7c9 100644
@@ -930,6 +930,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
 #define KVM_CAP_X86_GUEST_MODE 238
 #define KVM_CAP_ARM_WRITABLE_IMP_ID_REGS 239
+#define KVM_CAP_GMEM_SHARED_MEM 240
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
virt/kvm/kvm_main.c +4 -0
index 6261d8638cd2..6c75f933bfbe 100644
@@ -4840,6 +4840,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 #ifdef CONFIG_KVM_GMEM
 	case KVM_CAP_GUEST_MEMFD:
 		return !kvm || kvm_arch_supports_gmem(kvm);
+#endif
+#ifdef CONFIG_KVM_GMEM_SHARED_MEM
+	case KVM_CAP_GMEM_SHARED_MEM:
+		return !kvm || kvm_arch_vm_supports_gmem_shared_mem(kvm);
 #endif
 	default:
 		break;
Signature · 1 line
2.49.0.1045.g170613ef41-goog
#84 lore raw

Re: [PATCH v9 15/17] KVM: Introduce the KVM capability KVM_CAP_GMEM_SHARED_MEM

Hi Fuad,

Quoted 29 lines · Fuad Tabba

This patch introduces the KVM capability KVM_CAP_GMEM_SHARED_MEM, which indicates that guest_memfd supports shared memory (when enabled by the flag). This support is limited to certain VM types, determined per architecture.

This patch also updates the KVM documentation with details on the new capability, flag, and other information about support for shared memory in guest_memfd.

---
  Documentation/virt/kvm/api.rst | 18 ++++++++++++++++++
  include/uapi/linux/kvm.h       |  1 +
  virt/kvm/kvm_main.c            |  4 ++++
  3 files changed, 23 insertions(+)
Documentation/virt/kvm/api.rst +5 -0
index 47c7c3f92314..86f74ce7f12a 100644
@@ -6390,6 +6390,24 @@ most one mapping per page, i.e. binding multiple memory regions to a single
  guest_memfd range is not allowed (any number of memory regions can be bound to
  a single guest_memfd file, but the bound ranges must not overlap).
 
+When the capability KVM_CAP_GMEM_SHARED_MEM is supported, the 'flags' field
+supports GUEST_MEMFD_FLAG_SUPPORT_SHARED.  Setting this flag on guest_memfd
+creation enables mmap() and faulting of guest_memfd memory to host userspace.
+
+When the KVM MMU performs a PFN lookup to service a guest fault and the backing

Documentation/virt/kvm/api.rst most one mapping per page, i.e. binding multiple memory regions to a single · line 6407

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+guest_memfd has the GUEST_MEMFD_FLAG_SUPPORT_SHARED set, then the fault will
+always be consumed from guest_memfd, regardless of whether it is a shared or a
+private fault.
+
+For these memslots, userspace_addr is checked to be the mmap()-ed view of the
+same range specified using gmem.pgoff.  Other accesses by KVM, e.g., instruction
+emulation, go via slot->userspace_addr.  The slot->userspace_addr field can be
+set to 0 to skip this check, which indicates that KVM would not access memory
+belonging to the slot via its userspace_addr.
+

This paragraph needs to be removed if PATCH[08/17] is going to be dropped.

[PATCH v9 08/17] KVM: guest_memfd: Check that userspace_addr and fd+offset refer to same range

Documentation/virt/kvm/api.rst most one mapping per page, i.e. binding multiple memory regions to a single · line 6412

Quoted 5 lines
Documentation/virt/kvm/api.rst +3 -0
+The use of GUEST_MEMFD_FLAG_SUPPORT_SHARED will not be allowed for CoCo VMs.
+This is validated when the guest_memfd instance is bound to the VM.
+
  See KVM_SET_USER_MEMORY_REGION2 for additional details.
 

[...]

Thanks,
Gavin

author
#85 lore raw

Re: [PATCH v9 15/17] KVM: Introduce the KVM capability KVM_CAP_GMEM_SHARED_MEM

Quoted 37 lines · Gavin Shan

Hi Fuad,

Quoted 33 lines · Fuad Tabba

This patch introduces the KVM capability KVM_CAP_GMEM_SHARED_MEM, which indicates that guest_memfd supports shared memory (when enabled by the flag). This support is limited to certain VM types, determined per architecture.

This patch also updates the KVM documentation with details on the new capability, flag, and other information about support for shared memory in guest_memfd.

---
  Documentation/virt/kvm/api.rst | 18 ++++++++++++++++++
  include/uapi/linux/kvm.h       |  1 +
  virt/kvm/kvm_main.c            |  4 ++++
  3 files changed, 23 insertions(+)
Documentation/virt/kvm/api.rst +9 -0
index 47c7c3f92314..86f74ce7f12a 100644
@@ -6390,6 +6390,24 @@ most one mapping per page, i.e. binding multiple memory regions to a single
  guest_memfd range is not allowed (any number of memory regions can be bound to
  a single guest_memfd file, but the bound ranges must not overlap).
 
+When the capability KVM_CAP_GMEM_SHARED_MEM is supported, the 'flags' field
+supports GUEST_MEMFD_FLAG_SUPPORT_SHARED.  Setting this flag on guest_memfd
+creation enables mmap() and faulting of guest_memfd memory to host userspace.
+
+When the KVM MMU performs a PFN lookup to service a guest fault and the backing
+guest_memfd has the GUEST_MEMFD_FLAG_SUPPORT_SHARED set, then the fault will
+always be consumed from guest_memfd, regardless of whether it is a shared or a
+private fault.
+
Quoted 8 lines

Documentation/virt/kvm/api.rst most one mapping per page, i.e. binding multiple memory regions to a single · line 6407

Quoted 6 lines
Documentation/virt/kvm/api.rst +6 -0
+For these memslots, userspace_addr is checked to be the mmap()-ed view of the
+same range specified using gmem.pgoff.  Other accesses by KVM, e.g., instruction
+emulation, go via slot->userspace_addr.  The slot->userspace_addr field can be
+set to 0 to skip this check, which indicates that KVM would not access memory
+belonging to the slot via its userspace_addr.
+

This paragraph needs to be removed if PATCH[08/17] is going to be dropped.

Done.

Thanks,
/fuad

Quoted 14 lines

[PATCH v9 08/17] KVM: guest_memfd: Check that userspace_addr and fd+offset refer to same range

Documentation/virt/kvm/api.rst most one mapping per page, i.e. binding multiple memory regions to a single · line 6412

Quoted 5 lines
Documentation/virt/kvm/api.rst +3 -0
+The use of GUEST_MEMFD_FLAG_SUPPORT_SHARED will not be allowed for CoCo VMs.
+This is validated when the guest_memfd instance is bound to the VM.
+
  See KVM_SET_USER_MEMORY_REGION2 for additional details.
 

[...]

Thanks,
Gavin

Reviewed-by on v9 15/17
#86 lore raw

Re: [PATCH v9 15/17] KVM: Introduce the KVM capability KVM_CAP_GMEM_SHARED_MEM

In reply to Fuad Tabba
Quoted 33 lines · Fuad Tabba

This patch introduces the KVM capability KVM_CAP_GMEM_SHARED_MEM, which indicates that guest_memfd supports shared memory (when enabled by the flag). This support is limited to certain VM types, determined per architecture.

This patch also updates the KVM documentation with details on the new capability, flag, and other information about support for shared memory in guest_memfd.

---
  Documentation/virt/kvm/api.rst | 18 ++++++++++++++++++
  include/uapi/linux/kvm.h       |  1 +
  virt/kvm/kvm_main.c            |  4 ++++
  3 files changed, 23 insertions(+)
Documentation/virt/kvm/api.rst +9 -0
index 47c7c3f92314..86f74ce7f12a 100644
@@ -6390,6 +6390,24 @@ most one mapping per page, i.e. binding multiple memory regions to a single
  guest_memfd range is not allowed (any number of memory regions can be bound to
  a single guest_memfd file, but the bound ranges must not overlap).
 
+When the capability KVM_CAP_GMEM_SHARED_MEM is supported, the 'flags' field
+supports GUEST_MEMFD_FLAG_SUPPORT_SHARED.  Setting this flag on guest_memfd
+creation enables mmap() and faulting of guest_memfd memory to host userspace.
+
+When the KVM MMU performs a PFN lookup to service a guest fault and the backing
+guest_memfd has the GUEST_MEMFD_FLAG_SUPPORT_SHARED set, then the fault will
+always be consumed from guest_memfd, regardless of whether it is a shared or a
+private fault.
+

Documentation/virt/kvm/api.rst most one mapping per page, i.e. binding multiple memory regions to a single · line 6411

Quoted 10 lines
Documentation/virt/kvm/api.rst +9 -0
+For these memslots, userspace_addr is checked to be the mmap()-ed view of the
+same range specified using gmem.pgoff.  Other accesses by KVM, e.g., instruction
+emulation, go via slot->userspace_addr.  The slot->userspace_addr field can be
+set to 0 to skip this check, which indicates that KVM would not access memory
+belonging to the slot via its userspace_addr.
+
+The use of GUEST_MEMFD_FLAG_SUPPORT_SHARED will not be allowed for CoCo VMs.
+This is validated when the guest_memfd instance is bound to the VM.
+
  See KVM_SET_USER_MEMORY_REGION2 for additional details.

With Gavin's comment addressed

Signature · 3 lines
Cheers,

David / dhildenb
author
#87 lore raw

PATCH v9 16/17 KVM: 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 arm64.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../testing/selftests/kvm/guest_memfd_test.c  | 145 +++++++++++++++---
 2 files changed, 126 insertions(+), 20 deletions(-)
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index f62b0a5aba35..ccf95ed037c3 100644
@@ -163,6 +163,7 @@ TEST_GEN_PROGS_arm64 += access_tracking_perf_test
 TEST_GEN_PROGS_arm64 += arch_timer
 TEST_GEN_PROGS_arm64 += coalesced_io_test
 TEST_GEN_PROGS_arm64 += dirty_log_perf_test
+TEST_GEN_PROGS_arm64 += guest_memfd_test
 TEST_GEN_PROGS_arm64 += get-reg-list
 TEST_GEN_PROGS_arm64 += memslot_modification_stress_test
 TEST_GEN_PROGS_arm64 += memslot_perf_test
tools/testing/selftests/kvm/guest_memfd_test.c +125 -20

193 lines not on this page · raw message

Signature · 1 line
2.49.0.1045.g170613ef41-goog
#88 lore raw

Re: [PATCH v9 16/17] KVM: selftests: guest_memfd mmap() test when mapping is allowed

Hi Fuad,

Quoted 51 lines · 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 arm64.

---
  tools/testing/selftests/kvm/Makefile.kvm      |   1 +
  .../testing/selftests/kvm/guest_memfd_test.c  | 145 +++++++++++++++---
  2 files changed, 126 insertions(+), 20 deletions(-)
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index f62b0a5aba35..ccf95ed037c3 100644
@@ -163,6 +163,7 @@ TEST_GEN_PROGS_arm64 += access_tracking_perf_test
  TEST_GEN_PROGS_arm64 += arch_timer
  TEST_GEN_PROGS_arm64 += coalesced_io_test
  TEST_GEN_PROGS_arm64 += dirty_log_perf_test
+TEST_GEN_PROGS_arm64 += guest_memfd_test
  TEST_GEN_PROGS_arm64 += get-reg-list
  TEST_GEN_PROGS_arm64 += memslot_modification_stress_test
  TEST_GEN_PROGS_arm64 += memslot_perf_test
tools/testing/selftests/kvm/guest_memfd_test.c +17 -1
index ce687f8d248f..443c49185543 100644
@@ -34,12 +34,46 @@ static void test_file_read_write(int fd)
  		    "pwrite on a guest_mem fd should fail");
  }
 
-static void test_mmap(int fd, size_t page_size)
+static void test_mmap_allowed(int fd, size_t page_size, size_t total_size)
+{
+	const char val = 0xaa;
+	char *mem;
+	size_t i;
+	int ret;
+
+	mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+	TEST_ASSERT(mem != MAP_FAILED, "mmaping() guest memory should pass.");
+
+	memset(mem, val, total_size);
+	for (i = 0; i < total_size; i++)
+		TEST_ASSERT_EQ(mem[i], val);
+
+	ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0,
+			page_size);
+	TEST_ASSERT(!ret, "fallocate the first page should succeed");

tools/testing/selftests/kvm/guest_memfd_test.c static void test_file_read_write(int fd) · line 63

Quoted 10 lines
tools/testing/selftests/kvm/guest_memfd_test.c +10 -0
+
+	for (i = 0; i < page_size; i++)
+		TEST_ASSERT_EQ(mem[i], 0x00);
+	for (; i < total_size; i++)
+		TEST_ASSERT_EQ(mem[i], val);
+
+	memset(mem, val, total_size);
+	for (i = 0; i < total_size; i++)
+		TEST_ASSERT_EQ(mem[i], val);
+

The last memset() and check the resident values look redudant because same test has been covered by the first memset(). If we really want to double confirm that the page-cache is writabble, it would be enough to cover the first page. Otherwise, I guess this hunk of code can be removed :)

	memset(mem, val, page_size);
	for (i = 0; i < page_size; i++)
		TEST_ASSERT_EQ(mem[i], val);
Quoted 56 lines
tools/testing/selftests/kvm/guest_memfd_test.c +14 -13
+	ret = munmap(mem, total_size);
+	TEST_ASSERT(!ret, "munmap should succeed");
+}
+
+static void test_mmap_denied(int fd, size_t page_size, size_t total_size)
  {
  	char *mem;
 
  	mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  	TEST_ASSERT_EQ(mem, MAP_FAILED);
+
+	mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+	TEST_ASSERT_EQ(mem, MAP_FAILED);
  }
 
  static void test_file_size(int fd, size_t page_size, size_t total_size)
@@ -120,26 +154,19 @@ static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size)
  	}
  }
 
-static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
+static void test_create_guest_memfd_invalid_sizes(struct kvm_vm *vm,
+						  uint64_t guest_memfd_flags,
+						  size_t page_size)
  {
-	size_t page_size = getpagesize();
-	uint64_t flag;
  	size_t size;
  	int fd;
 
  	for (size = 1; size < page_size; size++) {
-		fd = __vm_create_guest_memfd(vm, size, 0);
+		fd = __vm_create_guest_memfd(vm, size, guest_memfd_flags);
  		TEST_ASSERT(fd == -1 && errno == EINVAL,
  			    "guest_memfd() with non-page-aligned page size '0x%lx' should fail with EINVAL",
  			    size);
  	}
-
-	for (flag = BIT(0); flag; flag <<= 1) {
-		fd = __vm_create_guest_memfd(vm, page_size, flag);
-		TEST_ASSERT(fd == -1 && errno == EINVAL,
-			    "guest_memfd() with flag '0x%lx' should fail with EINVAL",
-			    flag);
-	}
  }
 
  static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
@@ -170,30 +197,108 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
  	close(fd1);
  }
 
-int main(int argc, char *argv[])
+static void test_with_type(unsigned long vm_type, uint64_t guest_memfd_flags,
+			   bool expect_mmap_allowed)
  {
-	size_t page_size;

tools/testing/selftests/kvm/guest_memfd_test.c static void test_create_guest_memfd_multiple(struct kvm_vm *vm) · line 210

Quoted 10 lines
tools/testing/selftests/kvm/guest_memfd_test.c +4 -2
+	struct kvm_vm *vm;
  	size_t total_size;
+	size_t page_size;
  	int fd;
-	struct kvm_vm *vm;
 
-	TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD));
+	if (!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type)))
+		return;
 

The check seems incorrect for aarch64 since 0 is always returned from kvm_check_cap() there. The test is skipped for VM_TYPE_DEFAULT on aarch64. So it would be something like below:

	#define VM_TYPE_DEFAULT		0

	if (vm_type != VM_TYPE_DEFAULT &&
	    !(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type)))
		return;
Quoted 29 lines
tools/testing/selftests/kvm/guest_memfd_test.c +12 -4
  	page_size = getpagesize();
  	total_size = page_size * 4;
 
-	vm = vm_create_barebones();
+	vm = vm_create_barebones_type(vm_type);
 
-	test_create_guest_memfd_invalid(vm);
  	test_create_guest_memfd_multiple(vm);
+	test_create_guest_memfd_invalid_sizes(vm, guest_memfd_flags, page_size);
 
-	fd = vm_create_guest_memfd(vm, total_size, 0);
+	fd = vm_create_guest_memfd(vm, total_size, guest_memfd_flags);
 
  	test_file_read_write(fd);
-	test_mmap(fd, page_size);
+
+	if (expect_mmap_allowed)
+		test_mmap_allowed(fd, page_size, total_size);
+	else
+		test_mmap_denied(fd, page_size, total_size);
+
  	test_file_size(fd, page_size, total_size);
  	test_fallocate(fd, page_size, total_size);
  	test_invalid_punch_hole(fd, page_size, total_size);
 
  	close(fd);
+	kvm_vm_release(vm);
+}
+

tools/testing/selftests/kvm/guest_memfd_test.c static void test_create_guest_memfd_multiple(struct kvm_vm *vm) · line 245

Quoted 10 lines
tools/testing/selftests/kvm/guest_memfd_test.c +10 -0
+static void test_vm_type_gmem_flag_validity(unsigned long vm_type,
+					    uint64_t expected_valid_flags)
+{
+	size_t page_size = getpagesize();
+	struct kvm_vm *vm;
+	uint64_t flag = 0;
+	int fd;
+
+	if (!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type)))
+		return;

Same as above

Quoted 5 lines
tools/testing/selftests/kvm/guest_memfd_test.c +5 -0
+
+	vm = vm_create_barebones_type(vm_type);
+
+	for (flag = BIT(0); flag; flag <<= 1) {
+		fd = __vm_create_guest_memfd(vm, page_size, flag);

tools/testing/selftests/kvm/guest_memfd_test.c static void test_create_guest_memfd_multiple(struct kvm_vm *vm) · line 260

Quoted 10 lines
tools/testing/selftests/kvm/guest_memfd_test.c +10 -0
+
+		if (flag & expected_valid_flags) {
+			TEST_ASSERT(fd > 0,
+				    "guest_memfd() with flag '0x%lx' should be valid",
+				    flag);
+			close(fd);
+		} else {
+			TEST_ASSERT(fd == -1 && errno == EINVAL,
+				    "guest_memfd() with flag '0x%lx' should fail with EINVAL",
+				    flag);

It's more robust to have:

			TEST_ASSERT(fd < 0 && errno == EINVAL, ...);
Quoted 34 lines
tools/testing/selftests/kvm/guest_memfd_test.c +34 -0
+		}
+	}
+
+	kvm_vm_release(vm);
+}
+
+static void test_gmem_flag_validity(void)
+{
+	uint64_t non_coco_vm_valid_flags = 0;
+
+	if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM))
+		non_coco_vm_valid_flags = GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+
+	test_vm_type_gmem_flag_validity(VM_TYPE_DEFAULT, non_coco_vm_valid_flags);
+
+#ifdef __x86_64__
+	test_vm_type_gmem_flag_validity(KVM_X86_SW_PROTECTED_VM, non_coco_vm_valid_flags);
+	test_vm_type_gmem_flag_validity(KVM_X86_SEV_VM, 0);
+	test_vm_type_gmem_flag_validity(KVM_X86_SEV_ES_VM, 0);
+	test_vm_type_gmem_flag_validity(KVM_X86_SNP_VM, 0);
+	test_vm_type_gmem_flag_validity(KVM_X86_TDX_VM, 0);
+#endif
+}
+
+int main(int argc, char *argv[])
+{
+	TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD));
+
+	test_gmem_flag_validity();
+
+	test_with_type(VM_TYPE_DEFAULT, 0, false);
+	if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM)) {
+		test_with_type(VM_TYPE_DEFAULT, GUEST_MEMFD_FLAG_SUPPORT_SHARED,
+			       true);

tools/testing/selftests/kvm/guest_memfd_test.c static void test_create_guest_memfd_multiple(struct kvm_vm *vm) · line 304

Quoted 10 lines
tools/testing/selftests/kvm/guest_memfd_test.c +9 -0
+	}
+
+#ifdef __x86_64__
+	test_with_type(KVM_X86_SW_PROTECTED_VM, 0, false);
+	if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM)) {
+		test_with_type(KVM_X86_SW_PROTECTED_VM,
+			       GUEST_MEMFD_FLAG_SUPPORT_SHARED, true);
+	}
+#endif
  }

Thanks,
Gavin

author
#89 lore raw

Re: [PATCH v9 16/17] KVM: selftests: guest_memfd mmap() test when mapping is allowed

Hi Gavin,

Quoted 62 lines · Gavin Shan

Hi Fuad,

Quoted 58 lines · 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 arm64.

---
  tools/testing/selftests/kvm/Makefile.kvm      |   1 +
  .../testing/selftests/kvm/guest_memfd_test.c  | 145 +++++++++++++++---
  2 files changed, 126 insertions(+), 20 deletions(-)
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index f62b0a5aba35..ccf95ed037c3 100644
@@ -163,6 +163,7 @@ TEST_GEN_PROGS_arm64 += access_tracking_perf_test
  TEST_GEN_PROGS_arm64 += arch_timer
  TEST_GEN_PROGS_arm64 += coalesced_io_test
  TEST_GEN_PROGS_arm64 += dirty_log_perf_test
+TEST_GEN_PROGS_arm64 += guest_memfd_test
  TEST_GEN_PROGS_arm64 += get-reg-list
  TEST_GEN_PROGS_arm64 += memslot_modification_stress_test
  TEST_GEN_PROGS_arm64 += memslot_perf_test
tools/testing/selftests/kvm/guest_memfd_test.c +24 -1
index ce687f8d248f..443c49185543 100644
@@ -34,12 +34,46 @@ static void test_file_read_write(int fd)
                  "pwrite on a guest_mem fd should fail");
  }
 
-static void test_mmap(int fd, size_t page_size)
+static void test_mmap_allowed(int fd, size_t page_size, size_t total_size)
+{
+     const char val = 0xaa;
+     char *mem;
+     size_t i;
+     int ret;
+
+     mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+     TEST_ASSERT(mem != MAP_FAILED, "mmaping() guest memory should pass.");
+
+     memset(mem, val, total_size);
+     for (i = 0; i < total_size; i++)
+             TEST_ASSERT_EQ(mem[i], val);
+
+     ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0,
+                     page_size);
+     TEST_ASSERT(!ret, "fallocate the first page should succeed");
+
+     for (i = 0; i < page_size; i++)
+             TEST_ASSERT_EQ(mem[i], 0x00);
+     for (; i < total_size; i++)
+             TEST_ASSERT_EQ(mem[i], val);
+
+     memset(mem, val, total_size);
Quoted 8 lines

tools/testing/selftests/kvm/guest_memfd_test.c static void test_file_read_write(int fd) · line 50

Quoted 3 lines
tools/testing/selftests/kvm/guest_memfd_test.c +3 -0
+     for (i = 0; i < total_size; i++)
+             TEST_ASSERT_EQ(mem[i], val);
+

The last memset() and check the resident values look redudant because same test has been covered by the first memset(). If we really want to double confirm that the page-cache is writabble, it would be enough to cover the first page. Otherwise, I guess this hunk of code can be removed :)

My goal was to check that it is in fact writable, and that it stores the expected value, after the punch_hole. I'll limit it to the first page.

Quoted 76 lines
        memset(mem, val, page_size);
        for (i = 0; i < page_size; i++)
                TEST_ASSERT_EQ(mem[i], val);
Quoted 56 lines
tools/testing/selftests/kvm/guest_memfd_test.c +14 -13
+     ret = munmap(mem, total_size);
+     TEST_ASSERT(!ret, "munmap should succeed");
+}
+
+static void test_mmap_denied(int fd, size_t page_size, size_t total_size)
  {
      char *mem;
 
      mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
      TEST_ASSERT_EQ(mem, MAP_FAILED);
+
+     mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+     TEST_ASSERT_EQ(mem, MAP_FAILED);
  }
 
  static void test_file_size(int fd, size_t page_size, size_t total_size)
@@ -120,26 +154,19 @@ static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size)
      }
  }
 
-static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
+static void test_create_guest_memfd_invalid_sizes(struct kvm_vm *vm,
+                                               uint64_t guest_memfd_flags,
+                                               size_t page_size)
  {
-     size_t page_size = getpagesize();
-     uint64_t flag;
      size_t size;
      int fd;
 
      for (size = 1; size < page_size; size++) {
-             fd = __vm_create_guest_memfd(vm, size, 0);
+             fd = __vm_create_guest_memfd(vm, size, guest_memfd_flags);
              TEST_ASSERT(fd == -1 && errno == EINVAL,
                          "guest_memfd() with non-page-aligned page size '0x%lx' should fail with EINVAL",
                          size);
      }
-
-     for (flag = BIT(0); flag; flag <<= 1) {
-             fd = __vm_create_guest_memfd(vm, page_size, flag);
-             TEST_ASSERT(fd == -1 && errno == EINVAL,
-                         "guest_memfd() with flag '0x%lx' should fail with EINVAL",
-                         flag);
-     }
  }
 
  static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
@@ -170,30 +197,108 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
      close(fd1);
  }
 
-int main(int argc, char *argv[])
+static void test_with_type(unsigned long vm_type, uint64_t guest_memfd_flags,
+                        bool expect_mmap_allowed)
  {
-     size_t page_size;

tools/testing/selftests/kvm/guest_memfd_test.c static void test_create_guest_memfd_multiple(struct kvm_vm *vm) · line 210

Quoted 10 lines
tools/testing/selftests/kvm/guest_memfd_test.c +4 -2
+     struct kvm_vm *vm;
      size_t total_size;
+     size_t page_size;
      int fd;
-     struct kvm_vm *vm;
 
-     TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD));
+     if (!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type)))
+             return;
 

The check seems incorrect for aarch64 since 0 is always returned from kvm_check_cap() there. The test is skipped for VM_TYPE_DEFAULT on aarch64. So it would be something like below:

Quoted 5 lines
        #define VM_TYPE_DEFAULT         0

        if (vm_type != VM_TYPE_DEFAULT &&
            !(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type)))
                return;

Ack.

Thanks for this, and for all the other reviews.

Cheers,
/fuad

Quoted 25 lines
Quoted 25 lines
tools/testing/selftests/kvm/guest_memfd_test.c +9 -4
      page_size = getpagesize();
      total_size = page_size * 4;
 
-     vm = vm_create_barebones();
+     vm = vm_create_barebones_type(vm_type);
 
-     test_create_guest_memfd_invalid(vm);
      test_create_guest_memfd_multiple(vm);
+     test_create_guest_memfd_invalid_sizes(vm, guest_memfd_flags, page_size);
 
-     fd = vm_create_guest_memfd(vm, total_size, 0);
+     fd = vm_create_guest_memfd(vm, total_size, guest_memfd_flags);
 
      test_file_read_write(fd);
-     test_mmap(fd, page_size);
+
+     if (expect_mmap_allowed)
+             test_mmap_allowed(fd, page_size, total_size);
+     else
+             test_mmap_denied(fd, page_size, total_size);
+
      test_file_size(fd, page_size, total_size);
      test_fallocate(fd, page_size, total_size);
      test_invalid_punch_hole(fd, page_size, total_size);
 
Quoted 16 lines
Quoted 4 lines
tools/testing/selftests/kvm/guest_memfd_test.c +3 -0
      close(fd);
+     kvm_vm_release(vm);
+}
+

tools/testing/selftests/kvm/guest_memfd_test.c static void test_create_guest_memfd_multiple(struct kvm_vm *vm) · line 245

Quoted 10 lines
tools/testing/selftests/kvm/guest_memfd_test.c +10 -0
+static void test_vm_type_gmem_flag_validity(unsigned long vm_type,
+                                         uint64_t expected_valid_flags)
+{
+     size_t page_size = getpagesize();
+     struct kvm_vm *vm;
+     uint64_t flag = 0;
+     int fd;
+
+     if (!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type)))
+             return;

Same as above

Ack.

Quoted 18 lines
Quoted 5 lines
tools/testing/selftests/kvm/guest_memfd_test.c +5 -0
+
+     vm = vm_create_barebones_type(vm_type);
+
+     for (flag = BIT(0); flag; flag <<= 1) {
+             fd = __vm_create_guest_memfd(vm, page_size, flag);

tools/testing/selftests/kvm/guest_memfd_test.c static void test_create_guest_memfd_multiple(struct kvm_vm *vm) · line 260

Quoted 10 lines
tools/testing/selftests/kvm/guest_memfd_test.c +10 -0
+
+             if (flag & expected_valid_flags) {
+                     TEST_ASSERT(fd > 0,
+                                 "guest_memfd() with flag '0x%lx' should be valid",
+                                 flag);
+                     close(fd);
+             } else {
+                     TEST_ASSERT(fd == -1 && errno == EINVAL,
+                                 "guest_memfd() with flag '0x%lx' should fail with EINVAL",
+                                 flag);

It's more robust to have:

Quoted 1 line
                        TEST_ASSERT(fd < 0 && errno == EINVAL, ...);

Ack.

Quoted 48 lines
Quoted 34 lines
tools/testing/selftests/kvm/guest_memfd_test.c +34 -0
+             }
+     }
+
+     kvm_vm_release(vm);
+}
+
+static void test_gmem_flag_validity(void)
+{
+     uint64_t non_coco_vm_valid_flags = 0;
+
+     if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM))
+             non_coco_vm_valid_flags = GUEST_MEMFD_FLAG_SUPPORT_SHARED;
+
+     test_vm_type_gmem_flag_validity(VM_TYPE_DEFAULT, non_coco_vm_valid_flags);
+
+#ifdef __x86_64__
+     test_vm_type_gmem_flag_validity(KVM_X86_SW_PROTECTED_VM, non_coco_vm_valid_flags);
+     test_vm_type_gmem_flag_validity(KVM_X86_SEV_VM, 0);
+     test_vm_type_gmem_flag_validity(KVM_X86_SEV_ES_VM, 0);
+     test_vm_type_gmem_flag_validity(KVM_X86_SNP_VM, 0);
+     test_vm_type_gmem_flag_validity(KVM_X86_TDX_VM, 0);
+#endif
+}
+
+int main(int argc, char *argv[])
+{
+     TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD));
+
+     test_gmem_flag_validity();
+
+     test_with_type(VM_TYPE_DEFAULT, 0, false);
+     if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM)) {
+             test_with_type(VM_TYPE_DEFAULT, GUEST_MEMFD_FLAG_SUPPORT_SHARED,
+                            true);

tools/testing/selftests/kvm/guest_memfd_test.c static void test_create_guest_memfd_multiple(struct kvm_vm *vm) · line 304

Quoted 10 lines
tools/testing/selftests/kvm/guest_memfd_test.c +9 -0
+     }
+
+#ifdef __x86_64__
+     test_with_type(KVM_X86_SW_PROTECTED_VM, 0, false);
+     if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM)) {
+             test_with_type(KVM_X86_SW_PROTECTED_VM,
+                            GUEST_MEMFD_FLAG_SUPPORT_SHARED, true);
+     }
+#endif
  }

Thanks,
Gavin

author
#90 lore raw

PATCH v9 17/17 KVM: selftests: Test guest_memfd same-range validation

In reply to Fuad Tabba

From: Ackerley Tng <[email protected]>

Add some selftests for guest_memfd same-range validation, which check that the slot userspace_addr covers the same range as the memory in guest_memfd:

+ When slot->userspace_addr is set to 0, there should be no range match validation on guest_memfd binding.
+ guest_memfd binding should fail if
+ slot->userspace_addr is not from guest_memfd
+ slot->userspace_addr is mmap()ed from some other file
+ slot->userspace_addr is mmap()ed from some other guest_memfd
+ slot->userspace_addr is mmap()ed from a different range in the same guest_memfd
+ guest_memfd binding should succeed if slot->userspace_addr is mmap()ed from the same range in the same guest_memfd provided in slot->guest_memfd

---
 .../testing/selftests/kvm/guest_memfd_test.c  | 168 ++++++++++++++++++
 1 file changed, 168 insertions(+)
tools/testing/selftests/kvm/guest_memfd_test.c +168 -0
index 443c49185543..60aaba5808a5 100644
@@ -197,6 +197,173 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
 	close(fd1);
 }
 
+#define GUEST_MEMFD_TEST_SLOT 10
+#define GUEST_MEMFD_TEST_GPA 0x100000000
+
+static void
+test_bind_guest_memfd_disabling_range_match_validation(struct kvm_vm *vm,
+						       int fd)
+{
+	size_t page_size = getpagesize();
+	int ret;
+
+	ret = __vm_set_user_memory_region2(vm, GUEST_MEMFD_TEST_SLOT,
+					   KVM_MEM_GUEST_MEMFD,
+					   GUEST_MEMFD_TEST_GPA, page_size, 0,
+					   fd, 0);
+	TEST_ASSERT(!ret,
+		    "setting slot->userspace_addr to 0 should disable validation");
+	ret = __vm_set_user_memory_region2(vm, GUEST_MEMFD_TEST_SLOT,
+					   KVM_MEM_GUEST_MEMFD,
+					   GUEST_MEMFD_TEST_GPA, 0, 0,
+					   fd, 0);
+	TEST_ASSERT(!ret, "Deleting memslot should work");
+}
+
+static void
+test_bind_guest_memfd_anon_memory_in_userspace_addr(struct kvm_vm *vm, int fd)
+{
+	size_t page_size = getpagesize();
+	void *userspace_addr;
+	int ret;
+
+	userspace_addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+			      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+	ret = __vm_set_user_memory_region2(vm, GUEST_MEMFD_TEST_SLOT,
+					   KVM_MEM_GUEST_MEMFD,
+					   GUEST_MEMFD_TEST_GPA, page_size,
+					   userspace_addr, fd, 0);
+	TEST_ASSERT(ret == -1,
+		    "slot->userspace_addr is not from the guest_memfd and should fail");
+}
+
+static void test_bind_guest_memfd_shared_memory_other_file_in_userspace_addr(
+	struct kvm_vm *vm, int fd)
+{
+	size_t page_size = getpagesize();
+	void *userspace_addr;
+	int other_fd;
+	int ret;
+
+	other_fd = memfd_create("shared_memory_other_file", 0);
+	TEST_ASSERT(other_fd > 0, "Creating other file should succeed");
+
+	userspace_addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+			      MAP_SHARED, other_fd, 0);
+
+	ret = __vm_set_user_memory_region2(vm, GUEST_MEMFD_TEST_SLOT,
+					   KVM_MEM_GUEST_MEMFD,
+					   GUEST_MEMFD_TEST_GPA, page_size,
+					   userspace_addr, fd, 0);
+	TEST_ASSERT(ret == -1,
+		    "slot->userspace_addr is not from the guest_memfd and should fail");
+
+	TEST_ASSERT(!munmap(userspace_addr, page_size),
+		    "munmap() to cleanup should succeed");
+
+	close(other_fd);
+}
+
+static void
+test_bind_guest_memfd_other_guest_memfd_in_userspace_addr(struct kvm_vm *vm,
+							  int fd)
+{
+	size_t page_size = getpagesize();
+	void *userspace_addr;
+	int other_fd;
+	int ret;
+
+	other_fd = vm_create_guest_memfd(vm, page_size * 2,
+					 GUEST_MEMFD_FLAG_SUPPORT_SHARED);
+	TEST_ASSERT(other_fd > 0, "Creating other file should succeed");
+
+	userspace_addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+			      MAP_SHARED, other_fd, 0);
+
+	ret = __vm_set_user_memory_region2(vm, GUEST_MEMFD_TEST_SLOT,
+					   KVM_MEM_GUEST_MEMFD,
+					   GUEST_MEMFD_TEST_GPA, page_size,
+					   userspace_addr, fd, 0);
+	TEST_ASSERT(ret == -1,
+		    "slot->userspace_addr is not from the guest_memfd and should fail");
+
+	TEST_ASSERT(!munmap(userspace_addr, page_size),
+		    "munmap() to cleanup should succeed");
+
+	close(other_fd);
+}
+
+static void
+test_bind_guest_memfd_other_range_in_userspace_addr(struct kvm_vm *vm, int fd)
+{
+	size_t page_size = getpagesize();
+	void *userspace_addr;
+	int ret;
+
+	userspace_addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+			      MAP_SHARED, fd, page_size);
+
+	ret = __vm_set_user_memory_region2(vm, GUEST_MEMFD_TEST_SLOT,
+					   KVM_MEM_GUEST_MEMFD,
+					   GUEST_MEMFD_TEST_GPA, page_size,
+					   userspace_addr, fd, 0);
+	TEST_ASSERT(ret == -1,
+		    "slot->userspace_addr is not from the same range and should fail");
+
+	TEST_ASSERT(!munmap(userspace_addr, page_size),
+		    "munmap() to cleanup should succeed");
+}
+
+static void
+test_bind_guest_memfd_same_range_in_userspace_addr(struct kvm_vm *vm, int fd)
+{
+	size_t page_size = getpagesize();
+	void *userspace_addr;
+	int ret;
+
+	userspace_addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+			      MAP_SHARED, fd, page_size);
+
+	ret = __vm_set_user_memory_region2(vm, GUEST_MEMFD_TEST_SLOT,
+					   KVM_MEM_GUEST_MEMFD,
+					   GUEST_MEMFD_TEST_GPA, page_size,
+					   userspace_addr, fd, page_size);
+	TEST_ASSERT(!ret,
+		    "slot->userspace_addr is the same range and should succeed");
+
+	TEST_ASSERT(!munmap(userspace_addr, page_size),
+		    "munmap() to cleanup should succeed");
+
+	ret = __vm_set_user_memory_region2(vm, GUEST_MEMFD_TEST_SLOT,
+					   KVM_MEM_GUEST_MEMFD,
+					   GUEST_MEMFD_TEST_GPA, 0, 0,
+					   fd, 0);
+	TEST_ASSERT(!ret, "Deleting memslot should work");
+}
+
+static void test_bind_guest_memfd_wrt_userspace_addr(struct kvm_vm *vm)
+{
+	size_t page_size = getpagesize();
+	int fd;
+
+	if (!vm_check_cap(vm, KVM_CAP_GUEST_MEMFD) ||
+	    !vm_check_cap(vm, KVM_CAP_GMEM_SHARED_MEM))
+		return;
+
+	fd = vm_create_guest_memfd(vm, page_size * 2,
+				   GUEST_MEMFD_FLAG_SUPPORT_SHARED);
+
+	test_bind_guest_memfd_disabling_range_match_validation(vm, fd);
+	test_bind_guest_memfd_anon_memory_in_userspace_addr(vm, fd);
+	test_bind_guest_memfd_shared_memory_other_file_in_userspace_addr(vm, fd);
+	test_bind_guest_memfd_other_guest_memfd_in_userspace_addr(vm, fd);
+	test_bind_guest_memfd_other_range_in_userspace_addr(vm, fd);
+	test_bind_guest_memfd_same_range_in_userspace_addr(vm, fd);
+
+	close(fd);
+}
+
 static void test_with_type(unsigned long vm_type, uint64_t guest_memfd_flags,
 			   bool expect_mmap_allowed)
 {
@@ -214,6 +381,7 @@ static void test_with_type(unsigned long vm_type, uint64_t guest_memfd_flags,
 	vm = vm_create_barebones_type(vm_type);
 
 	test_create_guest_memfd_multiple(vm);
+	test_bind_guest_memfd_wrt_userspace_addr(vm);
 	test_create_guest_memfd_invalid_sizes(vm, guest_memfd_flags, page_size);
 
 	fd = vm_create_guest_memfd(vm, total_size, guest_memfd_flags);
Signature · 1 line
2.49.0.1045.g170613ef41-goog