Skip to contentSkip to conversation

KVM: x86: Fix lost nested APF VM-Exits

PATCH In review Loc Nguyen 2 participants 4 messages Oct 2

Subsystems (4)
lore
Share & tools
lore

Loc Nguyen

author
#0 lore raw

cover KVM: x86: Fix lost nested APF VM-Exits

A nested asynchronous page fault delivered to L1 as a synthetic #PF VM-Exit can be lost after KVM queues the exception. Most callers of kvm_queue_exception_vmexit() arrive through kvm_multiple_exception(), which requests event processing, but the nested APF path calls the helper directly.

Without KVM_REQ_EVENT, vcpu_enter_guest() can skip kvm_check_and_inject_events() and re-enter L2. A later VM-Exit can then clear the pending exception while reconstructing vectoring state. The APF reason remains PAGE_NOT_PRESENT, allowing a later regular #PF to consume the stale reason.

Patch 1 requests event processing in kvm_queue_exception_vmexit() so that all queued exception VM-Exits are processed before re-entering the guest.

Patch 2 adds a regression test that holds an L2 backing page missing with userfaultfd. The test verifies that L1 receives the synthetic #PF VM-Exit with the expected APF token and PAGE_NOT_PRESENT reason.

Testing was performed in a nested VMX environment:

- x86_64 kernel build with KVM_WERROR=y
- nested_apf_event_test fails on the base kernel with a lost nested APF event and passes with the fix
- full KVM selftest suite passes
- kvm-unit-tests VMX suite passes with TIMEOUT=900

Loc Nguyen (2):
KVM: x86: Request event processing for exception VM-Exits
KVM: selftests: Add a test for lost nested APF VM-Exits

 arch/x86/kvm/x86.c                            |   2 +
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/x86/nested_apf_event_test.c | 306 ++++++++++++++++++
 3 files changed, 309 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/nested_apf_event_test.c

base-commit: b378201ccd5280d0fff89bbe55e1eb00620ec0d5

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 KVM: x86: Request event processing for exception VM-Exits

Request event processing whenever kvm_queue_exception_vmexit() queues an exception VM-Exit. Most callers reach the helper through kvm_multiple_exception(), which already sets KVM_REQ_EVENT, but nested asynchronous page faults call it directly.

Without another event request, vcpu_enter_guest() skips kvm_check_and_inject_events() and re-enters L2. A subsequent VM-Exit can clear the pending exception while reconstructing vectoring state, losing the synthetic #PF VM-Exit. The APF reason remains PAGE_NOT_PRESENT and a later regular #PF can consume the stale value.

Setting KVM_REQ_EVENT in the common helper ensures that L1 receives the queued VM-Exit before KVM re-enters L2.

Fixes 7709aba8f716 ("KVM: x86: Morph pending exceptions to pending VM-Exits at queue time")
---
 arch/x86/kvm/x86.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/x86.c +2 -0
index cf3fcdfd8ad2..376341a1ba04 100644
@@ -450,6 +450,8 @@ static void kvm_queue_exception_vmexit(struct kvm_vcpu *vcpu, unsigned int vecto
 {
 	struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
 
+	kvm_make_request(KVM_REQ_EVENT, vcpu);
+
 	ex->vector = vector;
 	ex->injected = false;
 	ex->pending = true;
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/2 KVM: selftests: Add a test for lost nested APF VM-Exits

In reply to Loc Nguyen

Exercise delivery of an asynchronous page fault from L2 to L1 as a synthetic #PF VM-Exit. Hold L2's backing page missing with userfaultfd and bound the critical KVM_RUN with a signal-based timeout.

On an affected kernel, KVM queues the nested VM-Exit without requesting event processing, re-enters L2, and blocks on the same missing page. The test detects the timeout and verifies that L1's APF record still contains KVM_PV_REASON_PAGE_NOT_PRESENT.

On a fixed kernel, verify that L1 receives an exception VM-Exit with a #PF, error code zero, a nonzero APF token, and a matching page-not-present reason.

Skip the test when VMX, EPT, the nested APF features, or userfaultfd fault handling are unavailable.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/x86/nested_apf_event_test.c | 306 ++++++++++++++++++
 2 files changed, 307 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/nested_apf_event_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index b31ee61af284..a882c1f8a5fb 100644
@@ -92,6 +92,7 @@ TEST_GEN_PROGS_x86 += x86/kvm_pv_test
 TEST_GEN_PROGS_x86 += x86/kvm_buslock_test
 TEST_GEN_PROGS_x86 += x86/monitor_mwait_test
 TEST_GEN_PROGS_x86 += x86/msrs_test
+TEST_GEN_PROGS_x86 += x86/nested_apf_event_test
 TEST_GEN_PROGS_x86 += x86/nested_close_kvm_test
 TEST_GEN_PROGS_x86 += x86/nested_dirty_log_test
 TEST_GEN_PROGS_x86 += x86/nested_emulation_test
tools/testing/selftests/kvm/x86/nested_apf_event_test.c new file +306 -0

306 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw
In reply to Loc Nguyen
Quoted 6 lines · Loc Nguyen

A nested asynchronous page fault delivered to L1 as a synthetic #PF VM-Exit can be lost after KVM queues the exception. Most callers of kvm_queue_exception_vmexit() arrive through kvm_multiple_exception(), which requests event processing, but the nested APF path calls the helper directly.

Quoted 7 lines

Without KVM_REQ_EVENT, vcpu_enter_guest() can skip kvm_check_and_inject_events() and re-enter L2. A later VM-Exit can then clear the pending exception while reconstructing vectoring state. The APF reason remains PAGE_NOT_PRESENT, allowing a later regular #PF to consume the stale reason.

[...]

Applied the fix to kvm-x86 misc. I'm going to hold off on the selftest for the moment, I'd like to go straight to having it support both AMD and Intel.

Thanks!

[1/2] KVM: x86: Request event processing for exception VM-Exits

      https://github.com/kvm-x86/linux/commit/88b23a2e8286

[2/2] KVM: selftests: Add a test for lost nested APF VM-Exits
[SKIP, for now]

--
https://github.com/kvm-x86/linux/tree/next