Skip to contentSkip to conversation

KVM: GUEST_MEMFD fixes/restrictions

PATCH In review Sean Christopherson 1 participant 7 messages Feb 22, 2024 – Feb 23, 2024

Subsystems (5)
Documentation x86 KVM Kselftest +1 more subsystems
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: GUEST_MEMFD fixes/restrictions

A few minor-ish fixes related to GUEST_MEMFD that I am hoping to squeeze into 6.8 as the they affect KVM's ABI (especially patch 1).

Sean Christopherson (5):
KVM: Make KVM_MEM_GUEST_MEMFD mutually exclusive with KVM_MEM_READONLY
KVM: x86: Update KVM_SW_PROTECTED_VM docs to make it clear they're a
WIP
KVM: x86/mmu: Restrict KVM_SW_PROTECTED_VM to the TDP MMU
KVM: selftests: Create GUEST_MEMFD for relevant invalid flags
testcases
KVM: selftests: Add a testcase to verify GUEST_MEMFD and READONLY are
exclusive

 Documentation/virt/kvm/api.rst                       |  5 +++++
 arch/x86/kvm/Kconfig                                 |  7 ++++---
 arch/x86/kvm/x86.c                                   |  2 +-
 tools/testing/selftests/kvm/set_memory_region_test.c | 12 +++++++++++-
 virt/kvm/kvm_main.c                                  |  8 +++++++-
 5 files changed, 28 insertions(+), 6 deletions(-)

base-commit: 21dbc438dde69ff630b3264c54b94923ee9fcdcf

Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
author maintainer
#1 lore raw

PATCH 1/5 KVM: Make KVM_MEM_GUEST_MEMFD mutually exclusive with KVM_MEM_READONLY

Disallow creating read-only memslots that support GUEST_MEMFD, as GUEST_MEMFD is fundamentally incompatible with KVM's semantics for read-only memslots. Read-only memslots allow the userspace VMM to emulate option ROMs by filling the backing memory with readable, executable code and data, while triggering emulated MMIO on writes. GUEST_MEMFD doesn't currently support writes from userspace and KVM doesn't support emulated MMIO on private accesses, i.e. the guest can only ever read zeros, and writes will always be treated as errors.

Cc Fuad Tabba <[email protected]>
Cc Michael Roth <[email protected]>
Cc Isaku Yamahata <[email protected]>
Cc Yu Zhang <[email protected]>
Cc Chao Peng <[email protected]>
Fixes a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory")
---
 virt/kvm/kvm_main.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +7 -1
index eb0dfcd157f4..0f9f78f4f7cf 100644
@@ -1617,7 +1617,13 @@ static int check_memory_region_flags(struct kvm *kvm,
 		valid_flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
 
 #ifdef CONFIG_HAVE_KVM_READONLY_MEM
-	valid_flags |= KVM_MEM_READONLY;
+	/*
+	 * GUEST_MEMFD is incompatible with read-only memslots, as writes to
+	 * read-only memslots have emulated MMIO, not page fault, semantics,
+	 * and KVM doesn't allow emulated MMIO for private memory.
+	 */
+	if (!(mem->flags & KVM_MEM_GUEST_MEMFD))
+		valid_flags |= KVM_MEM_READONLY;
 #endif
 
 	if (mem->flags & ~valid_flags)
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
author maintainer
#2 lore raw

PATCH 2/5 KVM: x86: Update KVM_SW_PROTECTED_VM docs to make it clear they're a WIP

In reply to Sean Christopherson

Rewrite the help message for KVM_SW_PROTECTED_VM to make it clear that software-protected VMs are a development and testing vehicle for guest_memfd(), and that attempting to use KVM_SW_PROTECTED_VM for anything remotely resembling a "real" VM will fail. E.g. any memory accesses from KVM will incorrectly access shared memory, nested TDP is wildly broken, and so on and so forth.

Update KVM's API documentation with similar warnings to discourage anyone from attempting to run anything but selftests with KVM_X86_SW_PROTECTED_VM.

Fixes 89ea60c2c7b5 ("KVM: x86: Add support for "protected VMs" that can utilize private memory")
---
 Documentation/virt/kvm/api.rst | 5 +++++
 arch/x86/kvm/Kconfig           | 7 ++++---
 2 files changed, 9 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +5 -0
index bd93cafd3e4e..0b5a33ee71ee 100644
@@ -8820,6 +8820,11 @@ means the VM type with value @n is supported.  Possible values of @n are::
   #define KVM_X86_DEFAULT_VM	0
   #define KVM_X86_SW_PROTECTED_VM	1
 
+Note, KVM_X86_SW_PROTECTED_VM is currently only for development and testing.
+Do not use KVM_X86_SW_PROTECTED_VM for "real" VMs, and especially not in
+production.  The behavior and effective ABI for software-protected VMs is
+unstable.
+
 9. Known KVM API problems
 =========================
 
arch/x86/kvm/Kconfig +4 -3
index 5895aee5dfef..4336b3fff0cf 100644
@@ -81,9 +81,10 @@ config KVM_SW_PROTECTED_VM
 	depends on KVM && X86_64
 	select KVM_GENERIC_PRIVATE_MEM
 	help
-	  Enable support for KVM software-protected VMs.  Currently "protected"
-	  means the VM can be backed with memory provided by
-	  KVM_CREATE_GUEST_MEMFD.
+	  Enable support for KVM software-protected VMs.  Currently, software-
+	  protected VMs are purely a development and testing vehicle for
+	  KVM_CREATE_GUEST_MEMFD.  Attempting to run a "real" VM workload as a
+	  software-protected VM will fail miserably.
 
 	  If unsure, say "N".
 
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
author maintainer
#3 lore raw

PATCH 3/5 KVM: x86/mmu: Restrict KVM_SW_PROTECTED_VM to the TDP MMU

In reply to Sean Christopherson

Advertise and support software-protected VMs if and only if the TDP MMU is enabled, i.e. disallow KVM_SW_PROTECTED_VM if TDP is enabled for KVM's legacy/shadow MMU. TDP support for the shadow MMU is maintenance-only, e.g. support for TDX and SNP will also be restricted to the TDP MMU.

Fixes 89ea60c2c7b5 ("KVM: x86: Add support for "protected VMs" that can utilize private memory")
---
 arch/x86/kvm/x86.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
index 86d88bc7a6d0..1e0cc1906232 100644
@@ -4580,7 +4580,7 @@ static bool kvm_is_vm_type_supported(unsigned long type)
 {
 	return type == KVM_X86_DEFAULT_VM ||
 	       (type == KVM_X86_SW_PROTECTED_VM &&
-		IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) && tdp_enabled);
+		IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) && tdp_mmu_enabled);
 }
 
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
author maintainer
#4 lore raw

PATCH 4/5 KVM: selftests: Create GUEST_MEMFD for relevant invalid flags testcases

In reply to Sean Christopherson

Actually create a GUEST_MEMFD instance and pass it to KVM when doing negative tests for KVM_SET_USER_MEMORY_REGION2 + KVM_MEM_GUEST_MEMFD. Without a valid GUEST_MEMFD file descriptor, KVM_SET_USER_MEMORY_REGION2 will always fail with -EINVAL, resulting in false passes for any and all tests of illegal combinations of KVM_MEM_GUEST_MEMFD and other flags.

Fixes 5d74316466f4 ("KVM: selftests: Add a memory region subtest to validate invalid flags")
---
 tools/testing/selftests/kvm/set_memory_region_test.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/set_memory_region_test.c +5 -1
index 40337f566eeb..9df4b61116bc 100644
@@ -367,11 +367,15 @@ static void test_invalid_memory_region_flags(void)
 	}
 
 	if (supported_flags & KVM_MEM_GUEST_MEMFD) {
+		int guest_memfd = vm_create_guest_memfd(vm, MEM_REGION_SIZE, 0);
+
 		r = __vm_set_user_memory_region2(vm, 0,
 						 KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_GUEST_MEMFD,
-						 0, MEM_REGION_SIZE, NULL, 0, 0);
+						 0, MEM_REGION_SIZE, NULL, guest_memfd, 0);
 		TEST_ASSERT(r && errno == EINVAL,
 			    "KVM_SET_USER_MEMORY_REGION2 should have failed, dirty logging private memory is unsupported");
+
+		close(guest_memfd);
 	}
 }
 
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
author maintainer
#5 lore raw

PATCH 5/5 KVM: selftests: Add a testcase to verify GUEST_MEMFD and READONLY are exclusive

In reply to Sean Christopherson

Extend set_memory_region_test's invalid flags subtest to verify that GUEST_MEMFD is incompatible with READONLY. GUEST_MEMFD doesn't currently support writes from userspace and KVM doesn't support emulated MMIO on private accesses, and so KVM is supposed to reject the GUEST_MEMFD+READONLY in order to avoid configuration that KVM can't support.

---
 tools/testing/selftests/kvm/set_memory_region_test.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/testing/selftests/kvm/set_memory_region_test.c +6 -0
index 9df4b61116bc..06b43ed23580 100644
@@ -375,6 +375,12 @@ static void test_invalid_memory_region_flags(void)
 		TEST_ASSERT(r && errno == EINVAL,
 			    "KVM_SET_USER_MEMORY_REGION2 should have failed, dirty logging private memory is unsupported");
 
+		r = __vm_set_user_memory_region2(vm, 0,
+						 KVM_MEM_READONLY | KVM_MEM_GUEST_MEMFD,
+						 0, MEM_REGION_SIZE, NULL, guest_memfd, 0);
+		TEST_ASSERT(r && errno == EINVAL,
+			    "KVM_SET_USER_MEMORY_REGION2 should have failed, read-only GUEST_MEMFD memslots are unsupported");
+
 		close(guest_memfd);
 	}
 }
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
author maintainer
#6 lore raw
In reply to Sean Christopherson
Quoted 13 lines · Sean Christopherson

A few minor-ish fixes related to GUEST_MEMFD that I am hoping to squeeze into 6.8 as the they affect KVM's ABI (especially patch 1).

Sean Christopherson (5):
KVM: Make KVM_MEM_GUEST_MEMFD mutually exclusive with KVM_MEM_READONLY
KVM: x86: Update KVM_SW_PROTECTED_VM docs to make it clear they're a
WIP
KVM: x86/mmu: Restrict KVM_SW_PROTECTED_VM to the TDP MMU
KVM: selftests: Create GUEST_MEMFD for relevant invalid flags
testcases
KVM: selftests: Add a testcase to verify GUEST_MEMFD and READONLY are
exclusive

Quoted 1 line

[...]

Applied ridiculously quickly to kvm-x86 guest_memfd_fixes, so as to get these into -next before the weekend. I purposely put these in a separate branch so that I can squash fixes, reviews, etc., or drop the branch outright if Paolo swoops in and grabs 'em.

[1/5] KVM: Make KVM_MEM_GUEST_MEMFD mutually exclusive with KVM_MEM_READONLY

      https://github.com/kvm-x86/linux/commit/e563592224e0
[2/5] KVM: x86: Update KVM_SW_PROTECTED_VM docs to make it clear they're a WIP
      https://github.com/kvm-x86/linux/commit/422692098c4c
[3/5] KVM: x86/mmu: Restrict KVM_SW_PROTECTED_VM to the TDP MMU
      https://github.com/kvm-x86/linux/commit/a1176ef5c92a
[4/5] KVM: selftests: Create GUEST_MEMFD for relevant invalid flags testcases
      https://github.com/kvm-x86/linux/commit/63e5c5a10559
[5/5] KVM: selftests: Add a testcase to verify GUEST_MEMFD and READONLY are exclusive
      https://github.com/kvm-x86/linux/commit/2dfd23830344

--
https://github.com/kvm-x86/linux/tree/next