KVM: GUEST_MEMFD fixes/restrictions
Subsystems (5)
Sean Christopherson
maintainercover 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
Sean Christopherson
maintainerPATCH 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.
--- virt/kvm/kvm_main.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +7 -1
@@ -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
Sean Christopherson
maintainerPATCH 2/5 KVM: x86: Update KVM_SW_PROTECTED_VM docs to make it clear they're a WIP
In reply to Sean ChristophersonRewrite 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.
--- 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
@@ -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
@@ -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
Sean Christopherson
maintainerPATCH 3/5 KVM: x86/mmu: Restrict KVM_SW_PROTECTED_VM to the TDP MMU
In reply to Sean ChristophersonAdvertise 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.
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -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
Sean Christopherson
maintainerPATCH 4/5 KVM: selftests: Create GUEST_MEMFD for relevant invalid flags testcases
In reply to Sean ChristophersonActually 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.
--- 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
@@ -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
Sean Christopherson
maintainerPATCH 5/5 KVM: selftests: Add a testcase to verify GUEST_MEMFD and READONLY are exclusive
In reply to Sean ChristophersonExtend 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
@@ -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
Sean Christopherson
maintainerQuoted 13 lines · Sean Christopherson
A few minor-ish fixes related to
GUEST_MEMFDthat I am hoping to squeeze into 6.8 as the they affect KVM's ABI (especially patch 1).Sean Christopherson (5):
KVM: MakeKVM_MEM_GUEST_MEMFDmutually exclusive withKVM_MEM_READONLY
KVM: x86: UpdateKVM_SW_PROTECTED_VMdocs to make it clear they're a
WIP
KVM: x86/mmu: RestrictKVM_SW_PROTECTED_VMto the TDP MMU
KVM: selftests: CreateGUEST_MEMFDfor relevant invalid flags
testcases
KVM: selftests: Add a testcase to verifyGUEST_MEMFDand 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