• Sean Christopherson's avatar
    KVM: selftests: Add logic to detect if ioctl() failed because VM was killed · 1b78d474
    Sean Christopherson authored
    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: default avatarMichal 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.comSigned-off-by: default avatarSean Christopherson <seanjc@google.com>
    1b78d474
kvm_util.c 59.9 KB