* [PATCH v3 0/2] KVM: selftests: Actually test PV_UNHALT
@ 2026-08-27 4:45 Hemanth Selam
2026-08-27 4:45 ` [PATCH v3 1/2] KVM: selftests: Add a helper to read a vCPU's APIC ID Hemanth Selam
2026-08-27 4:45 ` [PATCH v3 2/2] KVM: selftests: Test the PV_UNHALT feature, not just its CPUID bit Hemanth Selam
0 siblings, 2 replies; 3+ messages in thread
From: Hemanth Selam @ 2026-08-27 4:45 UTC (permalink / raw)
To: seanjc, pbonzini, shuah; +Cc: kvm, linux-kselftest, linux-kernel
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(-)
--
2.43.7
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v3 1/2] KVM: selftests: Add a helper to read a vCPU's APIC ID
2026-08-27 4:45 [PATCH v3 0/2] KVM: selftests: Actually test PV_UNHALT Hemanth Selam
@ 2026-08-27 4:45 ` Hemanth Selam
2026-08-27 4:45 ` [PATCH v3 2/2] KVM: selftests: Test the PV_UNHALT feature, not just its CPUID bit Hemanth Selam
1 sibling, 0 replies; 3+ messages in thread
From: Hemanth Selam @ 2026-08-27 4:45 UTC (permalink / raw)
To: seanjc, pbonzini, shuah; +Cc: kvm, linux-kselftest, linux-kernel
Reading a vCPU's APIC ID from the host means open coding KVM_GET_LAPIC and
picking the field out of the register block, which several tests already
do. Add a helper next to the other APIC definitions so that a test that
needs to target a vCPU, e.g. to send it an IPI, can just ask for its ID.
Handle both APIC modes, since where KVM reports the ID depends on the mode
and, for x2APIC, on whether userspace enabled KVM_X2APIC_API_USE_32BIT_IDS:
read APIC_ID for xAPIC, where the guest can rename itself, and use the vCPU
ID for x2APIC, which KVM keeps equal to it.
Assisted-by: Cursor:claude-opus-5
Signed-off-by: Hemanth Selam <hemanth.selam@gmail.com>
---
.../testing/selftests/kvm/include/x86/apic.h | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/x86/apic.h b/tools/testing/selftests/kvm/include/x86/apic.h
index 31887bdc3d6c..df8815bd57af 100644
--- a/tools/testing/selftests/kvm/include/x86/apic.h
+++ b/tools/testing/selftests/kvm/include/x86/apic.h
@@ -79,6 +79,26 @@ void apic_disable(void);
void xapic_enable(void);
void x2apic_enable(void);
+/*
+ * Reads the APIC ID of a vCPU from the host, e.g. to target an IPI at it.
+ *
+ * In xAPIC mode the guest can change its own ID, so read it back from KVM,
+ * where it lives in bits 31:24 of APIC_ID. In x2APIC mode KVM ties the ID to
+ * the vCPU ID, and reports it in APIC_ID either as-is or shifted left by 24
+ * depending on KVM_X2APIC_API_USE_32BIT_IDS, so use the vCPU ID instead of
+ * having to know which format the VM opted into.
+ */
+static inline u32 vcpu_get_apic_id(struct kvm_vcpu *vcpu)
+{
+ struct kvm_lapic_state lapic;
+
+ if (vcpu_get_msr(vcpu, MSR_IA32_APICBASE) & MSR_IA32_APICBASE_EXTD)
+ return vcpu->id;
+
+ vcpu_ioctl(vcpu, KVM_GET_LAPIC, &lapic);
+ return GET_APIC_ID_FIELD(*(u32 *)&lapic.regs[APIC_ID]);
+}
+
static inline u32 get_bsp_flag(void)
{
return rdmsr(MSR_IA32_APICBASE) & MSR_IA32_APICBASE_BSP;
--
2.43.7
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v3 2/2] KVM: selftests: Test the PV_UNHALT feature, not just its CPUID bit
2026-08-27 4:45 [PATCH v3 0/2] KVM: selftests: Actually test PV_UNHALT Hemanth Selam
2026-08-27 4:45 ` [PATCH v3 1/2] KVM: selftests: Add a helper to read a vCPU's APIC ID Hemanth Selam
@ 2026-08-27 4:45 ` Hemanth Selam
1 sibling, 0 replies; 3+ messages in thread
From: Hemanth Selam @ 2026-08-27 4:45 UTC (permalink / raw)
To: seanjc, pbonzini, shuah; +Cc: kvm, linux-kselftest, linux-kernel
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.
Add a two vCPU test for it. One vCPU halts with interrupts disabled, so
nothing except the KVM_HC_KICK_CPU issued by the other can resume it: KVM
delivers the kick as APIC_DM_REMRD, which sets pv_unhalted and makes the
vCPU runnable without injecting an interrupt. Reaching the instruction
after HLT is therefore proof that the kick arrived.
Give the halting vCPU a non-zero APIC ID, so that a kick aimed at the
wrong vCPU fails the test instead of hitting the halter by chance, and
enable its APIC as a guest using PV spinlocks would, since KVM only routes
the kick once the vCPU's APIC is in the map.
Wait for the halter's halt_exits to tick before kicking so that the kick
lands on a vCPU that has actually halted, and bound the wait so a vCPU
that never halts fails the test instead of hanging it.
The kicking vCPU runs with KVM_CAP_ENFORCE_PV_FEATURE_CPUID enabled and
PV_UNHALT advertised, so that KVM services the hypercall because the
feature is exposed rather than because enforcement is off.
Assisted-by: Cursor:claude-opus-5
Signed-off-by: Hemanth Selam <hemanth.selam@gmail.com>
---
tools/testing/selftests/kvm/x86/kvm_pv_test.c | 97 ++++++++++++++++++-
1 file changed, 96 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kvm/x86/kvm_pv_test.c b/tools/testing/selftests/kvm/x86/kvm_pv_test.c
index 8ed5fa635021..120d08ac25ce 100644
--- a/tools/testing/selftests/kvm/x86/kvm_pv_test.c
+++ b/tools/testing/selftests/kvm/x86/kvm_pv_test.c
@@ -6,8 +6,10 @@
*/
#include <asm/kvm_para.h>
#include <linux/kvm_para.h>
+#include <pthread.h>
#include <stdint.h>
+#include "apic.h"
#include "test_util.h"
#include "kvm_util.h"
#include "processor.h"
@@ -193,7 +195,99 @@ static void test_pv_unhalt(void)
TEST_ASSERT(!vcpu_cpuid_has(vcpu, X86_FEATURE_KVM_PV_UNHALT),
"PV_UNHALT set in guest CPUID when HLT-exiting is disabled");
- /* FIXME: actually test KVM_FEATURE_PV_UNHALT feature */
+ kvm_vm_free(vm);
+}
+
+static void pv_unhalt_halter_guest_code(void)
+{
+ /*
+ * Enable the local APIC, as a guest that uses PV spinlocks would. KVM
+ * only routes the kick to this vCPU once its APIC is in the map.
+ */
+ xapic_enable();
+
+ /*
+ * Interrupts are disabled, so nothing except the KVM_HC_KICK_CPU from
+ * the other vCPU can end the halt, i.e. reaching GUEST_DONE() proves
+ * the kick was delivered.
+ */
+ asm volatile("cli; hlt");
+
+ GUEST_DONE();
+}
+
+static void pv_unhalt_kicker_guest_code(u32 halter_apic_id)
+{
+ /* KVM takes flags in a0 and the APIC ID to kick in a1. */
+ GUEST_ASSERT_EQ(kvm_hypercall(KVM_HC_KICK_CPU, 0, halter_apic_id, 0, 0), 0);
+ GUEST_DONE();
+}
+
+static void run_guest_to_done(struct kvm_vcpu *vcpu)
+{
+ struct ucall uc;
+ u64 cmd;
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+ cmd = get_ucall(vcpu, &uc);
+ if (cmd == UCALL_ABORT)
+ REPORT_GUEST_ASSERT(uc);
+ TEST_ASSERT_EQ(cmd, UCALL_DONE);
+}
+
+static void *pv_unhalt_halter_thread(void *vcpu)
+{
+ run_guest_to_done(vcpu);
+ return NULL;
+}
+
+static void test_pv_unhalt_kick(void)
+{
+ struct kvm_vcpu *halter, *kicker;
+ struct timespec start;
+ struct kvm_vm *vm;
+ pthread_t thread;
+ int r;
+
+ pr_info("testing KVM_HC_KICK_CPU\n");
+
+ /*
+ * Give the halter a non-zero APIC ID, so that a kick sent to the wrong
+ * vCPU fails the test instead of hitting the halter by chance.
+ */
+ vm = vm_create_with_one_vcpu(&kicker, pv_unhalt_kicker_guest_code);
+ halter = vm_vcpu_add(vm, 1, pv_unhalt_halter_guest_code);
+ virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA);
+
+ /*
+ * Enforce the PV CPUID so that KVM services the hypercall because
+ * PV_UNHALT is advertised to the kicker, and not because enforcement
+ * is off. KVM advertises PV_UNHALT by default while HLT-exiting is
+ * enabled; set it explicitly so that the test keeps testing the
+ * feature if that ever changes.
+ */
+ vcpu_enable_cap(kicker, KVM_CAP_ENFORCE_PV_FEATURE_CPUID, 1);
+ vcpu_set_cpuid_feature(kicker, X86_FEATURE_KVM_PV_UNHALT);
+ vcpu_args_set(kicker, 1, vcpu_get_apic_id(halter));
+
+ r = pthread_create(&thread, NULL, pv_unhalt_halter_thread, halter);
+ TEST_ASSERT(!r, "pthread_create halter failed, error=%d", r);
+
+ /* Kick only once the halter has taken its HLT exit. */
+ clock_gettime(CLOCK_MONOTONIC, &start);
+ while (!vcpu_get_stat(halter, halt_exits)) {
+ TEST_ASSERT(timespec_elapsed(start).tv_sec < 10,
+ "vCPU never halted");
+ usleep(100);
+ }
+
+ run_guest_to_done(kicker);
+
+ /* Nothing except the kick can get the halter to GUEST_DONE(). */
+ r = pthread_join(thread, NULL);
+ TEST_ASSERT(!r, "pthread_join halter failed, error=%d", r);
kvm_vm_free(vm);
}
@@ -215,4 +309,5 @@ int main(void)
kvm_vm_free(vm);
test_pv_unhalt();
+ test_pv_unhalt_kick();
}
--
2.43.7
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-27 4:46 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-27 4:45 [PATCH v3 0/2] KVM: selftests: Actually test PV_UNHALT Hemanth Selam
2026-08-27 4:45 ` [PATCH v3 1/2] KVM: selftests: Add a helper to read a vCPU's APIC ID Hemanth Selam
2026-08-27 4:45 ` [PATCH v3 2/2] KVM: selftests: Test the PV_UNHALT feature, not just its CPUID bit Hemanth Selam
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox