Skip to content

KVM: selftests: Actually test PV_UNHALT

Hemanth Selam kvm linux-kselftest lkml 2 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest2 patches 0 repliesNew
  2. v22 patches 1 replySuperseded

Changes in v3 from the cover letter

- Handle x2APIC in the new helper. v2 always applied the xAPIC shift and mask, so it returned 0 for a vCPU in x2APIC mode on a VM that enabled KVM_X2APIC_API_USE_32BIT_IDS, and truncated IDs above 255. Reported by the Sashiko AI reviewer.

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2KVM: selftests: Add a helper to read a vCPU's APIC IDtools/testing/selftests/kvm/include/x86/apic.h+20 -00 replies
2/2KVM: selftests: Test the PV_UNHALT feature, not just its CPUID bittools/testing/selftests/kvm/x86/kvm_pv_test.c+96 -10 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v3 yet.

Read the v3 thread

Cover letter [PATCH v3 0/2] KVM: selftests: Actually test PV_UNHALT

test_pv_unhalt() only checks that KVM clears KVM_FEATURE_PV_UNHALT from guest CPUID when HLT-exiting is disabled; the feature itself has never been exercised, hence the FIXME. Patch 2 tests it by halting one vCPU with interrupts disabled and kicking it from another, so that reaching the instruction after HLT is proof that KVM_HC_KICK_CPU was delivered.

Patch 1 adds the helper that patch 2 needs to learn a vCPU's APIC ID from the host, rather than open coding KVM_GET_LAPIC as a few tests already do.

Changes in v3:
- Handle x2APIC in the new helper. v2 always applied the xAPIC shift and mask, so it returned 0 for a vCPU in x2APIC mode on a VM that enabled KVM_X2APIC_API_USE_32BIT_IDS, and truncated IDs above 255. Reported by the Sashiko AI reviewer.

Changes in v2:
- Pass the APIC ID to kick in a1, not a0. KVM reads it from a1, as the in-kernel guest does in kvm_kick_cpu(), so v1 asked KVM to kick APIC ID 0 and only passed because the halting vCPU happened to be vCPU 0. Also reported by the Sashiko AI reviewer.
- Halt on a vCPU with a non-zero APIC ID, so that a kick sent to the wrong vCPU can no longer pass by accident, and enable that vCPU's APIC, as a guest using PV spinlocks would: KVM only routes the kick once the vCPU is in the APIC map, which is also why xapic_ipi_test enables it.
- Move the APIC ID helper into apic.h instead of keeping it private to the test (new patch 1).
- Report the return value of pthread_create()/pthread_join() rather than errno; they return the error directly and do not set errno.

Built and run on x86_64 (AMD). Untested on Intel, though the kick is handled in common code and delivered through the generic LAPIC path.

The helper was checked against every way KVM reports an APIC ID, comparing it with what the v2 version returned:

xAPIC vcpu 0..3 want 0..3 got 0..3 v2: 0..3

  x2APIC vcpu 0..3, legacy ID format   want 0..3   got 0..3    v2: 0..3
  x2APIC vcpu 0..3, 32-bit ID format   want 0..3   got 0..3    v2: 0 0 0 0
  x2APIC vcpu 300                      want 300    got 300     v2: 0
  guest renamed its xAPIC ID to 0x2a   want 42     got 42      v2: 42

For the test itself:

- On kvm-x86/next, the whole selftest suite builds warning-free and kvm_pv_test passed 10 of 10 runs.

- Also run inside a VM booted on a kernel built from kvm-x86/next, i.e. against the KVM this targets rather than the host's.

- Whole x86 suite with the series applied: 61 passed, 24 skipped, and set_sregs_test failed with "KVM allowed invalid efer bit (0x100)". That one fails identically without the series, i.e. it is the host kernel.

The test was checked against four deliberate breakages, to make sure it can only pass when the kick really works:

- pass the APIC ID in a0, i.e. the v1 bug: the kick goes to the wrong vCPU and the test times out, so this version does catch it;

- drop the KVM_HC_KICK_CPU call: the halted vCPU is never resumed and the test times out;

- clear PV_UNHALT from the kicking vCPU's CPUID while enforcement is on:
the hypercall returns -KVM_ENOSYS and the test fails with

     0xfffffffffffffc18 != 0x0 (kvm_hypercall(KVM_HC_KICK_CPU, ...) != 0)

- remove the halt: the bounded wait trips and the test fails with "vCPU never halted" rather than hanging.

Hemanth Selam (2):
KVM: selftests: Add a helper to read a vCPU's APIC ID
KVM: selftests: Test the PV_UNHALT feature, not just its CPUID bit

 tools/testing/selftests/kvm/include/x86/apic.h | 20 +++++
 tools/testing/selftests/kvm/x86/kvm_pv_test.c  | 97 +++++++++++++++++++++-
 2 files changed, 116 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.7