mirror_ubuntu-kernels/tools/testing/selftests/kvm/include
Sean Christopherson 1b78d474ce KVM: selftests: Add logic to detect if ioctl() failed because VM was killed
Add yet another macro to the VM/vCPU ioctl() framework to detect when an
ioctl() failed because KVM killed/bugged the VM, i.e. when there was
nothing wrong with the ioctl() itself.  If KVM kills a VM, e.g. by way of
a failed KVM_BUG_ON(), all subsequent VM and vCPU ioctl()s will fail with
-EIO, which can be quite misleading and ultimately waste user/developer
time.

Use KVM_CHECK_EXTENSION on KVM_CAP_USER_MEMORY to detect if the VM is
dead and/or bug, as KVM doesn't provide a dedicated ioctl().  Using a
heuristic is obviously less than ideal, but practically speaking the logic
is bulletproof barring a KVM change, and any such change would arguably
break userspace, e.g. if KVM returns something other than -EIO.

Without the detection, tearing down a bugged VM yields a cryptic failure
when deleting memslots:

  ==== Test Assertion Failure ====
  lib/kvm_util.c:689: !ret
  pid=45131 tid=45131 errno=5 - Input/output error
     1	0x00000000004036c3: __vm_mem_region_delete at kvm_util.c:689
     2	0x00000000004042f0: kvm_vm_free at kvm_util.c:724 (discriminator 12)
     3	0x0000000000402929: race_sync_regs at sync_regs_test.c:193
     4	0x0000000000401cab: main at sync_regs_test.c:334 (discriminator 6)
     5	0x0000000000416f13: __libc_start_call_main at libc-start.o:?
     6	0x000000000041855f: __libc_start_main_impl at ??:?
     7	0x0000000000401d40: _start at ??:?
  KVM_SET_USER_MEMORY_REGION failed, rc: -1 errno: 5 (Input/output error)

Which morphs into a more pointed error message with the detection:

  ==== Test Assertion Failure ====
  lib/kvm_util.c:689: false
  pid=80347 tid=80347 errno=5 - Input/output error
     1	0x00000000004039ab: __vm_mem_region_delete at kvm_util.c:689 (discriminator 5)
     2	0x0000000000404660: kvm_vm_free at kvm_util.c:724 (discriminator 12)
     3	0x0000000000402ac9: race_sync_regs at sync_regs_test.c:193
     4	0x0000000000401cb7: main at sync_regs_test.c:334 (discriminator 6)
     5	0x0000000000418263: __libc_start_call_main at libc-start.o:?
     6	0x00000000004198af: __libc_start_main_impl at ??:?
     7	0x0000000000401d90: _start at ??:?
  KVM killed/bugged the VM, check the kernel log for clues

Suggested-by: Michal Luczaj <mhal@rbox.co>
Cc: Oliver Upton <oliver.upton@linux.dev>
Cc: Colton Lewis <coltonlewis@google.com>
Link: https://lore.kernel.org/r/20231108010953.560824-3-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
2023-11-29 14:51:20 -08:00
..
aarch64 KVM: selftests: aarch64: vPMU register test for unimplemented counters 2023-10-24 22:59:31 +00:00
riscv KVM/riscv changes for 6.6 2023-08-31 13:25:55 -04:00
s390x KVM: selftests: Add #define of expected KVM exit reason for ucall 2023-08-02 14:42:52 -07:00
x86_64 KVM: selftests: Add helpers to do KVM_HC_MAP_GPA_RANGE hypercalls (x86) 2023-11-14 08:01:06 -05:00
guest_modes.h KVM: selftests: Factor out guest mode code 2021-01-07 18:07:29 -05:00
kvm_util_base.h KVM: selftests: Add logic to detect if ioctl() failed because VM was killed 2023-11-29 14:51:20 -08:00
kvm_util.h kvm: selftests: move ucall declarations into ucall_common.h 2022-01-07 13:25:33 -05:00
memstress.h KVM: selftests: Move dirty logging functions to memstress.(c|h) 2023-06-01 14:03:18 -07:00
numaif.h KVM: selftests: Add missing header file needed by xAPIC IPI tests 2021-02-11 08:02:08 -05:00
sparsebit.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 482 2019-06-19 17:09:52 +02:00
test_util.h Merge branch 'kvm-guestmemfd' into HEAD 2023-11-14 08:31:31 -05:00
ucall_common.h KVM: selftests: Add GUEST_SYNC[1-6] macros for synchronizing more data 2023-11-14 08:01:07 -05:00
userfaultfd_util.h KVM: selftests: Add a userfaultfd library 2022-11-10 19:10:27 +00:00