Skip to contentSkip to conversation

KVM: Kconfig fixes for the VM-wide private memory attribute

PATCH In review Paolo Bonzini 4 participants 12 messages Oct 20 – Oct 21

Subsystems (4)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

cover KVM: Kconfig fixes for the VM-wide private memory attribute

A late review of the guest_memfd attributes changes...

With Linux 7.4 the "private" memory attribute was moved from the VM to the individual guest_memfd file descriptor. The old semantics are available for backwards compatibility via CONFIG_KVM_VM_MEMORY_ATTRIBUTES, and disabled by default.

This change has two problems: first, it ties support for VM memory attributes entirely to a backwards compatibility symbol for distros. Second, it is incompatible with all released versions of QEMU (and perhaps most other VMMs?), thus the default is arguably not the right one.

Fix the first problem, by making CONFIG_KVM_VM_MEMORY_ATTRIBUTES an internal configuration symbol that is the equivalent of the old CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES, and use a more descriptive name for the private memory attribute. Fix the second by changing the default to y if either SEV or TDX are enabled; leaving it n for CONFIG_KVM_SW_PROTECTED_VM as it is only useful for selftests.

The last patch is a simple code cleanup to remove the static call and module parameter overhead when the VM-level attribute is absent.

Thanks,

Paolo

Paolo Bonzini (3):
KVM: split CONFIG_KVM_PRIVATE_MEMATTR_VM out of
KVM_VM_MEMORY_ATTRIBUTES
KVM: x86: leave the legacy private attribute in for TDX and SEV
KVM: compile out all !gmem_in_place_conversion code unless
CONFIG_KVM_PRIVATE_MEMATTR_VM

 Documentation/admin-guide/kernel-parameters.txt |  4 ++--
 arch/x86/kvm/Kconfig                            |  6 ++++--
 include/linux/kvm_host.h                        | 13 ++++++-------
 virt/kvm/Kconfig                                |  3 +++
 virt/kvm/kvm_main.c                             | 15 ++++++++-------
 5 files changed, 23 insertions(+), 18 deletions(-)
Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH 1/3 KVM: split CONFIG_KVM_PRIVATE_MEMATTR_VM out of KVM_VM_MEMORY_ATTRIBUTES

With Linux 7.4 the "private" memory attribute was moved from the VM to the individual guest_memfd file descriptor. The old semantics are available for backwards compatibility via CONFIG_KVM_VM_MEMORY_ATTRIBUTES.

However, this ties support for VM memory attributes entirely to a backwards compatibility symbol, which is a problem when adding VM memory attributes other than PRIVATE. Thus, make CONFIG_KVM_VM_MEMORY_ATTRIBUTES the equivalent of the old CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES, and use a more descriptive name for the private memory attribute. Do this now, instead of waiting for more attributes to come, before distros start adjusting their build configuration.

---
 Documentation/admin-guide/kernel-parameters.txt | 4 ++--
 arch/x86/kvm/Kconfig                            | 3 ++-
 include/linux/kvm_host.h                        | 4 +++-
 virt/kvm/Kconfig                                | 3 +++
 virt/kvm/kvm_main.c                             | 6 +++---
 5 files changed, 13 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +2 -2
index 410a3eed3a3d..42ff19defe16 100644
@@ -3178,8 +3178,8 @@ Kernel parameters
 			userspace VMM design.
 
 			Note, this parameter is only available when
-			CONFIG_KVM_VM_MEMORY_ATTRIBUTES=y. When
-			CONFIG_KVM_VM_MEMORY_ATTRIBUTES is not set, in-place
+			CONFIG_KVM_PRIVATE_MEMATTR_VM=y. When
+			CONFIG_KVM_PRIVATE_MEMATTR_VM is not set, in-place
 			conversion is enabled (and cannot be disabled) if KVM
 			is built with private memory support.
 
arch/x86/kvm/Kconfig +2 -1
index 796d33f629d3..01a683a9042a 100644
@@ -76,9 +76,10 @@ config KVM_WERROR
 
 	  If in doubt, say "N".
 
-config KVM_VM_MEMORY_ATTRIBUTES
+config KVM_PRIVATE_MEMATTR_VM
 	bool "Enable per-VM PRIVATE vs. SHARED attributes (for CoCo VMs)"
 	depends on KVM_SW_PROTECTED_VM || KVM_INTEL_TDX || KVM_AMD_SEV
+	select KVM_VM_MEMORY_ATTRIBUTES
 	help
 	  Enable support for tracking PRIVATE vs. SHARED memory using per-VM
 	  memory attributes.  Using per-VM attributes is deprecated in favor of
include/linux/kvm_host.h +3 -1
index 4d4e76a50d77..09c57d31d153 100644
@@ -2582,12 +2582,14 @@ bool kvm_arch_pre_set_vm_memory_attributes(struct kvm *kvm,
 					   struct kvm_gfn_range *range);
 bool kvm_arch_post_set_vm_memory_attributes(struct kvm *kvm,
 					    struct kvm_gfn_range *range);
+#endif  /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
 
+#ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
 static inline bool kvm_vm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
 {
 	return kvm_get_vm_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
 }
-#endif  /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
+#endif  /* CONFIG_KVM_PRIVATE_MEMATTR_VM */
 
 #ifdef kvm_arch_has_private_mem
 extern bool gmem_in_place_conversion;
virt/kvm/Kconfig +3 -0
index c5bfaa1f7749..99c9969e77a4 100644
@@ -101,3 +101,6 @@ config HAVE_KVM_ARCH_GMEM_INVALIDATE
 config HAVE_KVM_ARCH_GMEM_POPULATE
        bool
        depends on KVM_GUEST_MEMFD
+
+config KVM_VM_MEMORY_ATTRIBUTES
+       bool
virt/kvm/kvm_main.c +3 -3
index a97aa5b92616..3e35766f5c7e 100644
@@ -105,8 +105,8 @@ static bool __ro_after_init allow_unsafe_mappings;
 module_param(allow_unsafe_mappings, bool, 0444);
 
 #ifdef kvm_arch_has_private_mem
-bool __ro_after_init gmem_in_place_conversion = !IS_ENABLED(CONFIG_KVM_VM_MEMORY_ATTRIBUTES);
-#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES
+bool __ro_after_init gmem_in_place_conversion = !IS_ENABLED(CONFIG_KVM_PRIVATE_MEMATTR_VM);
+#ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
 module_param(gmem_in_place_conversion, bool, 0444);
 #endif
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(gmem_in_place_conversion);
@@ -2713,7 +2713,7 @@ static void kvm_init_memory_attributes(void)
 {
 	if (gmem_in_place_conversion)
 		static_call_update(__kvm_is_private_gfn, kvm_gmem_is_private_gfn);
-#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES
+#ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
 	else
 		static_call_update(__kvm_is_private_gfn, kvm_vm_is_private_gfn);
 #endif
Signature · 1 line
2.52.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] KVM: split CONFIG_KVM_PRIVATE_MEMATTR_VM out of KVM_VM_MEMORY_ATTRIBUTES

Quoted 13 lines · Paolo Bonzini

With Linux 7.4 the "private" memory attribute was moved from the VM to the individual guest_memfd file descriptor. The old semantics are available for backwards compatibility via CONFIG_KVM_VM_MEMORY_ATTRIBUTES.

However, this ties support for VM memory attributes entirely to a backwards compatibility symbol, which is a problem when adding VM memory attributes other than PRIVATE. Thus, make CONFIG_KVM_VM_MEMORY_ATTRIBUTES the equivalent of the old CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES, and use a more descriptive name for the private memory attribute. Do this now, instead of waiting for more attributes to come, before distros start adjusting their build configuration.

Quoted 1 line
Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] KVM: split CONFIG_KVM_PRIVATE_MEMATTR_VM out of KVM_VM_MEMORY_ATTRIBUTES

In reply to Paolo Bonzini
Quoted 10 lines · Paolo Bonzini

With Linux 7.4 the "private" memory attribute was moved from the VM to the individual guest_memfd file descriptor. The old semantics are available for backwards compatibility via CONFIG_KVM_VM_MEMORY_ATTRIBUTES.

However, this ties support for VM memory attributes entirely to a backwards compatibility symbol, which is a problem when adding VM memory attributes other than PRIVATE. Thus, make CONFIG_KVM_VM_MEMORY_ATTRIBUTES the equivalent of the old CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES, and use a more descriptive name for the private memory attribute.

I see this fits into your other work on adding other VM attributes. I thought the plan was to do something like this when the other VM attributes get added back.

Quoted 3 lines

Do this now, instead of waiting
for more attributes to come, before distros start adjusting their build configuration.

This is a good point though.

Quoted 5 lines

[...snip...]

author maintainer
#4 lore raw

PATCH 2/3 KVM: x86: leave the legacy private attribute in for TDX and SEV

In reply to Paolo Bonzini

As of today, and presumably as of Linux 7.4's release date, there is hardly any VMM that supports guest_memfd attributes. Support the legacy VM attribute for private memory if SEV or TDX are requested; default it to disabled if only the software-protected VM is requested, since it is only used by guest_memfd selftests.

While in-place conversion is the best option, it is way too early to enable it by default.

---
 arch/x86/kvm/Kconfig | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/Kconfig +2 -1
index 01a683a9042a..4f7d75ec8c40 100644
@@ -80,6 +80,7 @@ config KVM_PRIVATE_MEMATTR_VM
 	bool "Enable per-VM PRIVATE vs. SHARED attributes (for CoCo VMs)"
 	depends on KVM_SW_PROTECTED_VM || KVM_INTEL_TDX || KVM_AMD_SEV
 	select KVM_VM_MEMORY_ATTRIBUTES
+	default KVM_INTEL_TDX || KVM_AMD_SEV
 	help
 	  Enable support for tracking PRIVATE vs. SHARED memory using per-VM
 	  memory attributes.  Using per-VM attributes is deprecated in favor of
@@ -87,7 +88,7 @@ config KVM_PRIVATE_MEMATTR_VM
 	  CoCo VMs using a VMM that doesn't support guest_memfd memory
 	  attributes.
 
-	  If unsure, say N.
+	  If unsure, say "Y".
 
 config KVM_SW_PROTECTED_VM
 	bool "Enable support for KVM software-protected VMs"
Signature · 1 line
2.52.0
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] KVM: x86: leave the legacy private attribute in for TDX and SEV

Quoted 10 lines · Paolo Bonzini

As of today, and presumably as of Linux 7.4's release date, there is hardly any VMM that supports guest_memfd attributes. Support the legacy VM attribute for private memory if SEV or TDX are requested; default it to disabled if only the software-protected VM is requested, since it is only used by guest_memfd selftests.

While in-place conversion is the best option, it is way too early to enable it by default.

Quoted 17 lines
---
 arch/x86/kvm/Kconfig | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/Kconfig +1 -0
index 01a683a9042a..4f7d75ec8c40 100644
@@ -80,6 +80,7 @@ config KVM_PRIVATE_MEMATTR_VM
 	bool "Enable per-VM PRIVATE vs. SHARED attributes (for CoCo VMs)"
 	depends on KVM_SW_PROTECTED_VM || KVM_INTEL_TDX || KVM_AMD_SEV
 	select KVM_VM_MEMORY_ATTRIBUTES
+	default KVM_INTEL_TDX || KVM_AMD_SEV
 	help
 	  Enable support for tracking PRIVATE vs. SHARED memory using per-VM
 	  memory attributes.  Using per-VM attributes is deprecated in favor of

arch/x86/kvm/Kconfig config KVM_PRIVATE_MEMATTR_VM · line 91

Quoted 5 lines
arch/x86/kvm/Kconfig +1 -1
@@ -87,7 +88,7 @@ config KVM_PRIVATE_MEMATTR_VM
 	  CoCo VMs using a VMM that doesn't support guest_memfd memory
 	  attributes.
 
-	  If unsure, say N.
+	  If unsure, say "Y".

It looks the no-quote version is more popular.

If unsure, say Y.

Quoted 3 lines
arch/x86/kvm/Kconfig +0 -0
 config KVM_SW_PROTECTED_VM
 	bool "Enable support for KVM software-protected VMs"
Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] KVM: x86: leave the legacy private attribute in for TDX and SEV

In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini

As of today, and presumably as of Linux 7.4's release date, there is hardly any VMM that supports guest_memfd attributes. Support the legacy VM attribute for private memory if SEV or TDX are requested; default it to disabled if only the software-protected VM is requested, since it is only used by guest_memfd selftests.

While in-place conversion is the best option, it is way too early to enable it by default.

Quoted 28 lines
---
 arch/x86/kvm/Kconfig | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/Kconfig +2 -2
index 01a683a9042a..4f7d75ec8c40 100644
@@ -80,6 +80,7 @@ config KVM_PRIVATE_MEMATTR_VM
 	bool "Enable per-VM PRIVATE vs. SHARED attributes (for CoCo VMs)"
 	depends on KVM_SW_PROTECTED_VM || KVM_INTEL_TDX || KVM_AMD_SEV
 	select KVM_VM_MEMORY_ATTRIBUTES
+	default KVM_INTEL_TDX || KVM_AMD_SEV
 	help
 	  Enable support for tracking PRIVATE vs. SHARED memory using per-VM
 	  memory attributes.  Using per-VM attributes is deprecated in favor of
@@ -87,7 +88,7 @@ config KVM_PRIVATE_MEMATTR_VM
 	  CoCo VMs using a VMM that doesn't support guest_memfd memory
 	  attributes.
 
-	  If unsure, say N.
+	  If unsure, say "Y".
 
 config KVM_SW_PROTECTED_VM
 	bool "Enable support for KVM software-protected VMs"
--

2.52.0

author maintainer
#7 lore raw

PATCH 3/3 KVM: always make in-place conversion a compile-time constant when possible

In reply to Paolo Bonzini

Remove the unnecessary static call indirection and make gmem_in_place_conversion a constant if CONFIG_KVM_PRIVATE_MEMATTR_VM is false.

Require that CONFIG_KVM_PRIVATE_MEMATTR_VM is y only if kvm_arch_has_private_mem is also defined. This way, the three possible cases can be expressed without nested #ifs.

---
 include/linux/kvm_host.h | 13 +++++--------
 virt/kvm/kvm_main.c      | 13 +++++++------
 2 files changed, 12 insertions(+), 14 deletions(-)
include/linux/kvm_host.h +5 -8
index 09c57d31d153..e7fc94e2910a 100644
@@ -2585,15 +2585,7 @@ bool kvm_arch_post_set_vm_memory_attributes(struct kvm *kvm,
 #endif  /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
 
 #ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
-static inline bool kvm_vm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
-{
-	return kvm_get_vm_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
-}
-#endif  /* CONFIG_KVM_PRIVATE_MEMATTR_VM */
-
-#ifdef kvm_arch_has_private_mem
 extern bool gmem_in_place_conversion;
-
 typedef bool (kvm_is_private_gfn_t)(struct kvm *kvm, gfn_t gfn);
 DECLARE_STATIC_CALL(__kvm_is_private_gfn, kvm_is_private_gfn_t);
 
@@ -2601,6 +2593,11 @@ static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
 {
 	return static_call(__kvm_is_private_gfn)(kvm, gfn);
 }
+
+#elif defined kvm_arch_has_private_mem
+#define gmem_in_place_conversion true
+#define kvm_is_private_gfn kvm_gmem_is_private_gfn
+
 #else
 #define gmem_in_place_conversion false
 
virt/kvm/kvm_main.c +7 -6
index 3e35766f5c7e..a356a29be985 100644
@@ -104,11 +104,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(halt_poll_ns_shrink);
 static bool __ro_after_init allow_unsafe_mappings;
 module_param(allow_unsafe_mappings, bool, 0444);
 
-#ifdef kvm_arch_has_private_mem
-bool __ro_after_init gmem_in_place_conversion = !IS_ENABLED(CONFIG_KVM_PRIVATE_MEMATTR_VM);
 #ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
+bool __ro_after_init gmem_in_place_conversion = false;
 module_param(gmem_in_place_conversion, bool, 0444);
-#endif
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(gmem_in_place_conversion);
 #endif
 
@@ -2705,18 +2703,21 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
 }
 #endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
 
-#ifdef kvm_arch_has_private_mem
+#ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
 DEFINE_STATIC_CALL_RET0(__kvm_is_private_gfn, kvm_is_private_gfn_t);
 EXPORT_STATIC_CALL_GPL(__kvm_is_private_gfn);
 
+static bool kvm_vm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
+{
+	return kvm_get_vm_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
+}
+
 static void kvm_init_memory_attributes(void)
 {
 	if (gmem_in_place_conversion)
 		static_call_update(__kvm_is_private_gfn, kvm_gmem_is_private_gfn);
-#ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
 	else
 		static_call_update(__kvm_is_private_gfn, kvm_vm_is_private_gfn);
-#endif
 }
 #else
 static void kvm_init_memory_attributes(void) { }
Signature · 1 line
2.52.0
Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] KVM: always make in-place conversion a compile-time constant when possible

Quoted 6 lines · Paolo Bonzini

Remove the unnecessary static call indirection and make gmem_in_place_conversion a constant if CONFIG_KVM_PRIVATE_MEMATTR_VM is false.

Require that CONFIG_KVM_PRIVATE_MEMATTR_VM is y only if kvm_arch_has_private_mem is also defined.

This reads like the patch is going to make such a requirement. However, it doesn't.

Quoted 4 lines

This way, the three
possible cases can be expressed without nested #ifs.

Quoted 79 lines
---
 include/linux/kvm_host.h | 13 +++++--------
 virt/kvm/kvm_main.c      | 13 +++++++------
 2 files changed, 12 insertions(+), 14 deletions(-)
include/linux/kvm_host.h +5 -8
index 09c57d31d153..e7fc94e2910a 100644
@@ -2585,15 +2585,7 @@ bool kvm_arch_post_set_vm_memory_attributes(struct kvm *kvm,
 #endif  /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
 
 #ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
-static inline bool kvm_vm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
-{
-	return kvm_get_vm_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
-}
-#endif  /* CONFIG_KVM_PRIVATE_MEMATTR_VM */
-
-#ifdef kvm_arch_has_private_mem
 extern bool gmem_in_place_conversion;
-
 typedef bool (kvm_is_private_gfn_t)(struct kvm *kvm, gfn_t gfn);
 DECLARE_STATIC_CALL(__kvm_is_private_gfn, kvm_is_private_gfn_t);
 
@@ -2601,6 +2593,11 @@ static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
 {
 	return static_call(__kvm_is_private_gfn)(kvm, gfn);
 }
+
+#elif defined kvm_arch_has_private_mem
+#define gmem_in_place_conversion true
+#define kvm_is_private_gfn kvm_gmem_is_private_gfn
+
 #else
 #define gmem_in_place_conversion false
 
virt/kvm/kvm_main.c +7 -6
index 3e35766f5c7e..a356a29be985 100644
@@ -104,11 +104,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(halt_poll_ns_shrink);
 static bool __ro_after_init allow_unsafe_mappings;
 module_param(allow_unsafe_mappings, bool, 0444);
 
-#ifdef kvm_arch_has_private_mem
-bool __ro_after_init gmem_in_place_conversion = !IS_ENABLED(CONFIG_KVM_PRIVATE_MEMATTR_VM);
 #ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
+bool __ro_after_init gmem_in_place_conversion = false;
 module_param(gmem_in_place_conversion, bool, 0444);
-#endif
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(gmem_in_place_conversion);
 #endif
 
@@ -2705,18 +2703,21 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
 }
 #endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
 
-#ifdef kvm_arch_has_private_mem
+#ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
 DEFINE_STATIC_CALL_RET0(__kvm_is_private_gfn, kvm_is_private_gfn_t);
 EXPORT_STATIC_CALL_GPL(__kvm_is_private_gfn);
 
+static bool kvm_vm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
+{
+	return kvm_get_vm_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
+}
+
 static void kvm_init_memory_attributes(void)
 {
 	if (gmem_in_place_conversion)
 		static_call_update(__kvm_is_private_gfn, kvm_gmem_is_private_gfn);
-#ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
 	else
 		static_call_update(__kvm_is_private_gfn, kvm_vm_is_private_gfn);
-#endif
 }
 #else
 static void kvm_init_memory_attributes(void) { }
author maintainer
#9 lore raw

Re: [PATCH 3/3] KVM: always make in-place conversion a compile-time constant when possible

Quoted 9 lines · Xiaoyao Li
Quoted 6 lines · Paolo Bonzini

Remove the unnecessary static call indirection and make gmem_in_place_conversion a constant if CONFIG_KVM_PRIVATE_MEMATTR_VM is false.

Require that CONFIG_KVM_PRIVATE_MEMATTR_VM is y only if kvm_arch_has_private_mem is also defined.

This reads like the patch is going to make such a requirement. However, it doesn't.

It does in the sense that, if you set CONFIG_KVM_PRIVATE_MEMATTR_VM=y without defining kvm_arch_has_private_mem, you'll get a useless gmem_in_place_conversion module parameter.

Also, if CONFIG_KVM_GUEST_MEMFD=n, you'll get an undefined symbol kvm_gmem_is_private_gfn(). Previously, you wouldn't get the undefined symbol if kvm_arch_has_private_mem was not defined.

Paolo

Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] KVM: always make in-place conversion a compile-time constant when possible

In reply to Paolo Bonzini
Quoted 36 lines · Paolo Bonzini

Remove the unnecessary static call indirection and make gmem_in_place_conversion a constant if CONFIG_KVM_PRIVATE_MEMATTR_VM is false.

Require that CONFIG_KVM_PRIVATE_MEMATTR_VM is y only if kvm_arch_has_private_mem is also defined. This way, the three possible cases can be expressed without nested #ifs.

---
 include/linux/kvm_host.h | 13 +++++--------
 virt/kvm/kvm_main.c      | 13 +++++++------
 2 files changed, 12 insertions(+), 14 deletions(-)
include/linux/kvm_host.h +0 -8
index 09c57d31d153..e7fc94e2910a 100644
@@ -2585,15 +2585,7 @@ bool kvm_arch_post_set_vm_memory_attributes(struct kvm *kvm,
 #endif  /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
 
 #ifdef CONFIG_KVM_PRIVATE_MEMATTR_VM
-static inline bool kvm_vm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
-{
-	return kvm_get_vm_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
-}
-#endif  /* CONFIG_KVM_PRIVATE_MEMATTR_VM */
-
-#ifdef kvm_arch_has_private_mem
 extern bool gmem_in_place_conversion;
-
 typedef bool (kvm_is_private_gfn_t)(struct kvm *kvm, gfn_t gfn);
 DECLARE_STATIC_CALL(__kvm_is_private_gfn, kvm_is_private_gfn_t);
 
@@ -2601,6 +2593,11 @@ static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
 {

include/linux/kvm_host.h static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn) · line 2603

Quoted 10 lines
include/linux/kvm_host.h +5 -0
 	return static_call(__kvm_is_private_gfn)(kvm, gfn);
 }
+
+#elif defined kvm_arch_has_private_mem
+#define gmem_in_place_conversion true
+#define kvm_is_private_gfn kvm_gmem_is_private_gfn
+
 #else
 #define gmem_in_place_conversion false
 

There's a #endif just below here where the comment says

  #endif /* kvm_arch_has_private_mem */

Not sure if the convention is to align with the first #if (CONFIG_KVM_PRIVATE_MEMATTR_VM) or the #elif (kvm_arch_has_private_mem) or something in-between.

Quoted 3 lines

[...snip...]

#11 lore raw
In reply to Paolo Bonzini
Quoted 7 lines · Paolo Bonzini

A late review of the guest_memfd attributes changes...

With Linux 7.4 the "private" memory attribute was moved from the VM to the individual guest_memfd file descriptor. The old semantics are available for backwards compatibility via CONFIG_KVM_VM_MEMORY_ATTRIBUTES, and disabled by default.

Quoted 5 lines

This change has two problems: first, it ties support for VM memory attributes entirely to a backwards compatibility symbol for distros. Second, it is incompatible with all released versions of QEMU (and perhaps most other VMMs?), thus the default is arguably not the right one.

I was hoping we could go with a stick approach, but given how painful double-allocating is, hopefully a carrot will work too. :-)