[PATCH v2 20/20] KVM: selftests: Add Xen evtchn vs mmu_notifier race test for HVA caches
From: David Woodhouse
Date: Wed Nov 04 2026 - 21:27:59 EST
From: David Woodhouse <dwmw@xxxxxxxxxxxx>
Race a guest EVTCHNOP_send loop against a host thread hammering
MADV_DONTNEED on the userspace page backing HVA-based shared_info and
vcpu_info caches. Every madvise zaps the PTE and invalidates both
gfn_to_pfn_caches; every delivery must refresh through the window
(hva_to_pfn_retry() retries until the invalidation has finished, then
faults the page back in) and must succeed. The caches are HVA-based,
so no memslot semantics excuse a drop: unlike
xen_evtchn_memslot_race_test, which only pins the no-deadlock claim,
this test asserts every one of the 20000 sends delivers, and any
hypercall bounce to userspace fails the test.
This exercises the pfncache invalidation protocol itself — the
invalidate_start walk, the reader draining for a cache mid-teardown,
and the handshake between a refresh publishing a new mapping and a
concurrent invalidation vetoing it — which is where a lifetime bug
in the cache would live; on a debug kernel one is expected to
surface as a splat rather than a dropped event.
Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../kvm/x86/xen_evtchn_hva_notify_race_test.c | 188 ++++++++++++++++++
2 files changed, 189 insertions(+)
create mode 100644 tools/testing/selftests/kvm/x86/xen_evtchn_hva_notify_race_test.c
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index c17408f8d61f..19dc0744f25f 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -150,6 +150,7 @@ TEST_GEN_PROGS_x86 += x86/debug_regs
TEST_GEN_PROGS_x86 += x86/tsc_msrs_test
TEST_GEN_PROGS_x86 += x86/vmx_pmu_caps_test
TEST_GEN_PROGS_x86 += x86/xen_evtchn_memslot_race_test
+TEST_GEN_PROGS_x86 += x86/xen_evtchn_hva_notify_race_test
TEST_GEN_PROGS_x86 += x86/xen_shinfo_test
TEST_GEN_PROGS_x86 += x86/xen_vmcall_test
TEST_GEN_PROGS_x86 += x86/sev_dbg_test
diff --git a/tools/testing/selftests/kvm/x86/xen_evtchn_hva_notify_race_test.c b/tools/testing/selftests/kvm/x86/xen_evtchn_hva_notify_race_test.c
new file mode 100644
index 000000000000..c105435f6357
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/xen_evtchn_hva_notify_race_test.c
@@ -0,0 +1,188 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * xen_evtchn_hva_notify_race_test
+ *
+ * A guest EVTCHNOP_send loop racing continuous mmu_notifier
+ * invalidation of the HVA-based shared_info and vcpu_info caches: a
+ * host thread hammers MADV_DONTNEED on the userspace page backing
+ * both, zapping the PTE and invalidating the gfn_to_pfn_caches on
+ * every pass. Delivery must refresh through each invalidation window
+ * (hva_to_pfn_retry() retries until the notifier has finished, then
+ * faults the page back in) and must never fail: the caches are
+ * HVA-based, so no memslot semantics excuse a drop.
+ *
+ * This is the stress for the pfncache invalidation protocol itself —
+ * the invalidate_start walk, the GPC_INVALIDATING grace-period
+ * draining, and the announce/veto handshake between a refresh's
+ * publish and a concurrent invalidation. Unlike the sibling
+ * xen_evtchn_memslot_race_test, every send is asserted to succeed;
+ * any -EFAULT bounce to userspace, or any guest-visible failure, is
+ * a test failure, and a lifetime bug is expected to show up as a
+ * crash or splat on a debug kernel.
+ */
+#include <pthread.h>
+#include <stdatomic.h>
+#include <sys/mman.h>
+
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+
+#include <stdint.h>
+
+#define EVTCHN_PORT 15
+#define TEST_ITERATIONS 5000
+
+#define EVTCHNOP_send 4
+#define __HYPERVISOR_event_channel_op 32
+
+#define XEN_HYPERCALL_MSR 0x40000000
+
+#define EVTCHNSTAT_ipi 5
+
+struct evtchn_send {
+ uint32_t port;
+};
+
+static void guest_code(void)
+{
+ struct evtchn_send s = { .port = EVTCHN_PORT };
+ uint64_t rc;
+ int i;
+
+ for (i = 0; i < TEST_ITERATIONS; i++) {
+ rc = __xen_hypercall(__HYPERVISOR_event_channel_op,
+ EVTCHNOP_send, &s);
+ __GUEST_ASSERT(!rc,
+ "EVTCHNOP_send failed with %ld on iteration %d",
+ (long)rc, i);
+ GUEST_SYNC(i);
+ }
+
+ GUEST_DONE();
+}
+
+static atomic_bool churn_stop;
+
+static void *backing_churn(void *arg)
+{
+ void *backing = arg;
+
+ while (!atomic_load(&churn_stop))
+ madvise(backing, PAGE_SIZE, MADV_DONTNEED);
+
+ return NULL;
+}
+
+int main(int argc, char *argv[])
+{
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ pthread_t thread;
+ struct ucall uc;
+ void *backing;
+ int done = 0;
+
+ int xen_caps = kvm_check_cap(KVM_CAP_XEN_HVM);
+
+ TEST_REQUIRE(xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA);
+ TEST_REQUIRE(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_SEND);
+
+ vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+
+ /*
+ * The shared_info page and the vcpu_info within it live in plain
+ * anonymous VMM memory, not in any memslot: nothing but the
+ * mmu_notifier churn below can perturb the caches.
+ */
+ backing = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ TEST_ASSERT(backing != MAP_FAILED, "mmap of backing page failed");
+
+ struct kvm_xen_hvm_config hvmc = {
+ .flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL |
+ KVM_XEN_HVM_CONFIG_EVTCHN_SEND,
+ .msr = XEN_HYPERCALL_MSR,
+ };
+ vm_ioctl(vm, KVM_XEN_HVM_CONFIG, &hvmc);
+
+ struct kvm_xen_hvm_attr lm = {
+ .type = KVM_XEN_ATTR_TYPE_LONG_MODE,
+ .u.long_mode = 1,
+ };
+ vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &lm);
+
+ struct kvm_xen_hvm_attr ha = {
+ .type = KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA,
+ .u.shared_info.hva = (unsigned long)backing,
+ };
+ vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &ha);
+
+ struct kvm_xen_vcpu_attr vi = {
+ .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA,
+ .u.hva = (unsigned long)backing + 0x40,
+ };
+ vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &vi);
+
+ /* Loop the port back to this vCPU as an IPI. */
+ struct kvm_xen_hvm_attr evt = {
+ .type = KVM_XEN_ATTR_TYPE_EVTCHN,
+ .u.evtchn.send_port = EVTCHN_PORT,
+ .u.evtchn.type = EVTCHNSTAT_ipi,
+ .u.evtchn.flags = 0,
+ .u.evtchn.deliver.port.port = EVTCHN_PORT,
+ .u.evtchn.deliver.port.vcpu = vcpu->id,
+ .u.evtchn.deliver.port.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL,
+ };
+ vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &evt);
+
+ kvm_pthread_create(&thread, NULL, backing_churn, backing);
+
+ /*
+ * Self-guard against a kernel which livelocks a refresh under
+ * continuous invalidation. Deliberately no SIGALRM handler, as in
+ * xen_evtchn_memslot_race_test: only the default disposition can
+ * kill a process whose vCPU thread never returns to userspace.
+ * Empirically ~100-170 iterations/sec on a c6a.metal host: nearly
+ * every send pays a full refresh (fault the page back in and remap
+ * it), so budget generously; the healthy runtime is ~30-50s.
+ */
+ alarm(240);
+
+ while (!done) {
+ vcpu_run(vcpu);
+
+ /*
+ * An HVA-based cache has no memslot to be mid-update:
+ * there is no legitimate reason for delivery to fail, so
+ * unlike the memslot race test, a hypercall bouncing to
+ * userspace is a straight failure.
+ */
+ TEST_ASSERT(vcpu->run->exit_reason != KVM_EXIT_XEN,
+ "EVTCHNOP_send bounced to userspace: delivery failed");
+
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_SYNC:
+ if (uc.args[1] && !(uc.args[1] % 1000))
+ pr_info("progress: %lu iterations\n", uc.args[1]);
+ break;
+ case UCALL_DONE:
+ done = 1;
+ break;
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ /* NOT REACHED */
+ default:
+ TEST_FAIL("Unexpected ucall %lu", uc.cmd);
+ }
+ }
+
+ atomic_store(&churn_stop, true);
+ kvm_pthread_join(thread, NULL);
+ alarm(0);
+
+ kvm_vm_free(vm);
+ return 0;
+}
--
2.43.0