Skip to contentSkip to conversation

Fix error handling for async_pf

PATCH Applied Christian Borntraeger 3 participants 15 messages Oct 20 – Nov 13

Subsystems (3)
lore
Share & tools
lore

Christian Borntraeger

author reviewer
#0 lore raw

v2 cover Fix error handling for async_pf

Return mapping errors also for async_pf cases. Otherwise guests will loop forever. Nothing critical just missing error handling.

v1->v2:
- !npages instead of npages == 0 (Sean)
- new block comment (Sean)
- fix testcase to build with 7.3 and 7.4-rc

Christian Borntraeger (2):
KVM: Set up async page faults only for faults that need to wait
KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/faultin_sigbus_test.c       | 169 ++++++++++++++++++
 virt/kvm/kvm_main.c                           |   3 +-
 3 files changed, 172 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/kvm/faultin_sigbus_test.c
Signature · 1 line
2.53.0
author reviewer
#1 lore raw

PATCH v2 1/2 KVM: Set up async page faults only for faults that need to wait

hva_to_pfn() checks a FOLL_NOWAIT gup() for EINTR, EAGAIN and EHWPOISON. There might be others like -EFAULT for VM_FAULT_SIGBUS. For those other errors, reporting KVM_PFN_ERR_NEEDS_IO would be wrong as it triggers an async_pf resulting in endless retries.

Return KVM_PFN_ERR_NEEDS_IO only if gup() returned 0 pages and report the error otherwise.

Fixes 0857b9e95c1a ("KVM: Enable async page fault processing")
---
 virt/kvm/kvm_main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +2 -1
index 5a782849521c..7b6172810f34 100644
@@ -3047,7 +3047,8 @@ kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp)
 		if (r < 0)
 			pfn = KVM_PFN_ERR_FAULT;
 	} else {
-		if ((kfp->flags & FOLL_NOWAIT) &&
+		/* Note, GUP returns '0' if retry is needed. */
+		if (!npages && (kfp->flags & FOLL_NOWAIT) &&
 		    vma_is_valid(vma, kfp->flags & FOLL_WRITE))
 			pfn = KVM_PFN_ERR_NEEDS_IO;
 		else
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v2 1/2] KVM: Set up async page faults only for faults that need to wait

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

hva_to_pfn() checks a FOLL_NOWAIT gup() for EINTR, EAGAIN and EHWPOISON. There might be others like -EFAULT for VM_FAULT_SIGBUS. For those other errors, reporting KVM_PFN_ERR_NEEDS_IO would be wrong as it triggers an async_pf resulting in endless retries.

Return KVM_PFN_ERR_NEEDS_IO only if gup() returned 0 pages and report the error otherwise.

Fixes 0857b9e95c1a ("KVM: Enable async page fault processing")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] KVM: Set up async page faults only for faults that need to wait

In reply to Christian Borntraeger
Quoted 11 lines · Christian Borntraeger

hva_to_pfn() checks a FOLL_NOWAIT gup() for EINTR, EAGAIN and EHWPOISON. There might be others like -EFAULT for VM_FAULT_SIGBUS. For those other errors, reporting KVM_PFN_ERR_NEEDS_IO would be wrong as it triggers an async_pf resulting in endless retries.

Return KVM_PFN_ERR_NEEDS_IO only if gup() returned 0 pages and report the error otherwise.

Fixes 0857b9e95c1a ("KVM: Enable async page fault processing")

---

author reviewer
#4 lore raw

PATCH v2 2/2 KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

In reply to Christian Borntraeger

Add a test in which the guest writes to a shmem-backed memslot beyond the end of the file. KVM_RUN must fail with -EFAULT.

On x86 the guest enables interrupts, and on s390 the test sets up pfault, so that KVM could handle the fault with an asynchronous page fault. If KVM mistakes the error for a fault that needs I/O, the vCPU retries the access forever, and the test fails after a timeout.

The test is not built for LoongArch, which handles a failed mapping as an MMIO access.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/faultin_sigbus_test.c       | 169 ++++++++++++++++++
 2 files changed, 170 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/faultin_sigbus_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 68bcec3d0070..24fdfc3e0eba 100644
@@ -59,6 +59,7 @@ TEST_PROGS_x86 += x86/nx_huge_pages_test.sh
 # Compiled test targets valid on all architectures with libkvm support
 TEST_GEN_PROGS_COMMON = demand_paging_test
 TEST_GEN_PROGS_COMMON += dirty_log_test
+TEST_GEN_PROGS_COMMON += faultin_sigbus_test
 TEST_GEN_PROGS_COMMON += guest_print_test
 TEST_GEN_PROGS_COMMON += irqfd_test
 TEST_GEN_PROGS_COMMON += kvm_binary_stats_test
tools/testing/selftests/kvm/faultin_sigbus_test.c new file +169 -0

169 lines not on this page · raw message

Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH v2 2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

Add a test in which the guest writes to a shmem-backed memslot beyond the end of the file. KVM_RUN must fail with -EFAULT.

On x86 the guest enables interrupts, and on s390 the test sets up pfault, so that KVM could handle the fault with an asynchronous page fault. If KVM mistakes the error for a fault that needs I/O, the vCPU retries the access forever, and the test fails after a timeout.

Quoted 4 lines

The test is not built for LoongArch, which handles a failed mapping as an MMIO access.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH v2 2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

In reply to Christian Borntraeger
Quoted 8 lines · Christian Borntraeger

Add a test in which the guest writes to a shmem-backed memslot beyond the end of the file. KVM_RUN must fail with -EFAULT.

On x86 the guest enables interrupts, and on s390 the test sets up pfault, so that KVM could handle the fault with an asynchronous page fault. If KVM mistakes the error for a fault that needs I/O, the vCPU retries the access forever, and the test fails after a timeout.

Quoted 7 lines

The test is not built for LoongArch, which handles a failed mapping as an MMIO access.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/faultin_sigbus_test.c       | 169 ++++++++++++++++++

The "sigbus" part of this is confusing, because there is never actually a sigbus. Kinda silly, but maybe faultin_efault_test?

Quoted 2 lines
(quoted diff) +2 -0
+static void arch_enable_async_pf(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
+{

...

Review

Quoted 4 lines
(quoted diff) +4 -0
+}
+#else
+/* x86 needs guest interrupts on, see guest_code(); nothing to do elsewhere. */
+static void arch_enable_async_pf(struct kvm_vm *vm, struct kvm_vcpu *vcpu)

Nit, maybe arch_enable_host_async_pf()? Because x86 has PV async #PFs, where instead of scheduling a worker task in the host, KVM (conditionally) reflects the #PF into the guest so that the guest can schedule in a different task. A lot of people, myself included, see "async #PF" and think of the PV version first, e.g. I initially was looking for the PV side of things here.

Quoted 20 lines
(quoted diff) +20 -0
+#endif
+
+static void guest_code(u64 gva, u64 size)
+{
+#ifdef __x86_64__
+	/* KVM does async #PF, or its halt fallback, only with IRQs enabled. */
+	sti_nop();
+#endif
+	/* Within EOF: must work, proves that the mapping is set up right. */
+	WRITE_ONCE(*(u64 *)gva, 1);
+	GUEST_SYNC(0);
+
+	/* Beyond EOF: KVM_RUN must fail with -EFAULT. */
+	WRITE_ONCE(*(u64 *)(gva + size - sizeof(u64)), 1);
+	GUEST_FAIL("Store beyond EOF succeeded");
+}
+
+static void sig_alarm(int sig)
+{
+}

...

Review

Quoted 8 lines
(quoted diff) +8 -0
+	TEST_ASSERT(!sigaction(SIGALRM, &sa, NULL), "sigaction() failed");
+	alarm(TIMEOUT_SECS);
+	r = __vcpu_run(vcpu);
+	alarm(0);
+
+	TEST_ASSERT(!(r == -1 && errno == EINTR),
+		    "KVM_RUN did not return within %d seconds: the fault on the SIGBUS page is retried forever",
+		    TIMEOUT_SECS);

Rather than assert on EINTR, what if we have sig_alarm() explicitly TEST_FAIL()? That way readers don't have to piece together that the EINTR is coming from the alarm, and there's less chance of the test somehow getting a false pass because KVM manages to morph the signal into EFAULT. That would also allow using the single-underscore _vcpu_run(), though I don't know if that's actually a good idea.

E.g.

tools/testing/selftests/kvm/faultin_sigbus_test.c +2 -4
index 9394b04efcd6..9d90c32f3c6a 100644
@@ -106,6 +106,7 @@ static void guest_code(u64 gva, u64 size)
 
 static void sig_alarm(int sig)
 {
+	TEST_FAIL("%d second timeout expired before KVM_RUN hit -EFAULT", TIMEOUT_SECS);
 }
 
 int main(void)
@@ -154,12 +155,9 @@ int main(void)
 
 	TEST_ASSERT(!sigaction(SIGALRM, &sa, NULL), "sigaction() failed");
 	alarm(TIMEOUT_SECS);
-	r = __vcpu_run(vcpu);
+	r = _vcpu_run(vcpu);
 	alarm(0);
 
-	TEST_ASSERT(!(r == -1 && errno == EINTR),
-		    "KVM_RUN did not return within %d seconds: the fault on the SIGBUS page is retried forever",
-		    TIMEOUT_SECS);
 	TEST_ASSERT(r == -1 && errno == EFAULT,
 		    "Expected KVM_RUN to fail with EFAULT, got r = %d, errno = %d (%s), exit reason %s",
 		    r, errno, strerror(errno), exit_reason_str(vcpu->run->exit_reason));
author reviewer
#7 lore raw

Re: [PATCH v2 2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

Am 22.10.26 um 20:18 schrieb Sean Christopherson:

Quoted 12 lines · Christian Borntraeger
Quoted 11 lines

Add a test in which the guest writes to a shmem-backed memslot beyond the end of the file. KVM_RUN must fail with -EFAULT.

On x86 the guest enables interrupts, and on s390 the test sets up pfault, so that KVM could handle the fault with an asynchronous page fault. If KVM mistakes the error for a fault that needs I/O, the vCPU retries the access forever, and the test fails after a timeout.

The test is not built for LoongArch, which handles a failed mapping as an MMIO access.

Quoted 7 lines
Quoted 4 lines
---
  tools/testing/selftests/kvm/Makefile.kvm      |   1 +
  .../selftests/kvm/faultin_sigbus_test.c       | 169 ++++++++++++++++++

The "sigbus" part of this is confusing, because there is never actually a sigbus. Kinda silly, but maybe faultin_efault_test?

Its Friday, so lets ask the British people and let them vote for Faulty_McFaultFace.c? ;-) Just kidding, what about async_pf_error_test.c ?

Quoted 6 lines
Quoted 3 lines
(quoted diff) +2 -0
+static void arch_enable_async_pf(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
+{

...

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+}
+#else
+/* x86 needs guest interrupts on, see guest_code(); nothing to do elsewhere. */
+static void arch_enable_async_pf(struct kvm_vm *vm, struct kvm_vcpu *vcpu)

Nit, maybe arch_enable_host_async_pf()? Because x86 has PV async #PFs, where

Can do, I will follow your guidance as you have the x86 view.

Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+	TEST_ASSERT(!(r == -1 && errno == EINTR),
+		    "KVM_RUN did not return within %d seconds: the fault on the SIGBUS page is retried forever",
+		    TIMEOUT_SECS);

Rather than assert on EINTR, what if we have sig_alarm() explicitly TEST_FAIL()? That way readers don't have to piece together that the EINTR is coming from the alarm, and there's less chance of the test somehow getting a false pass because KVM manages to morph the signal into EFAULT. That would also allow using the

I will have a try. Thanks for the feedback.

author reviewer
#8 lore raw

PATCH v3 2/2 KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

Add a test in which the guest writes to a shmem-backed memslot beyond the end of the file. KVM_RUN must fail with -EFAULT.

On x86 the guest enables interrupts, and on s390 the test sets up pfault, so that KVM could handle the fault with an asynchronous page fault. If KVM mistakes the error for a fault that needs I/O, the vCPU retries the access forever, and the test fails after a timeout.

The test is not built for LoongArch, which handles a failed mapping as an MMIO access.

---
I went with Seans name proposal
v2->v3:
- renames as suggested by Sean
- test fail on alarm

 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/faultin_efault_test.c       | 168 ++++++++++++++++++
 2 files changed, 169 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/faultin_efault_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 3cb35a6963d51..2043ee38dacc1 100644
@@ -63,6 +63,7 @@ TEST_PROGS_x86 += x86/nx_huge_pages_test.sh
 # Compiled test targets valid on all architectures with libkvm support
 TEST_GEN_PROGS_COMMON = demand_paging_test
 TEST_GEN_PROGS_COMMON += dirty_log_test
+TEST_GEN_PROGS_COMMON += faultin_efault_test
 TEST_GEN_PROGS_COMMON += guest_print_test
 TEST_GEN_PROGS_COMMON += irqfd_test
 TEST_GEN_PROGS_COMMON += kvm_binary_stats_test
tools/testing/selftests/kvm/faultin_efault_test.c new file +168 -0

168 lines not on this page · raw message

Signature · 1 line
2.56.0
#9 lore raw

Re: [PATCH v3 2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

Quoted 8 lines · Christian Borntraeger

Add a test in which the guest writes to a shmem-backed memslot beyond the end of the file. KVM_RUN must fail with -EFAULT.

On x86 the guest enables interrupts, and on s390 the test sets up pfault, so that KVM could handle the fault with an asynchronous page fault. If KVM mistakes the error for a fault that needs I/O, the vCPU retries the access forever, and the test fails after a timeout.

Quoted 6 lines

The test is not built for LoongArch, which handles a failed mapping as an MMIO access.

---
I went with Seans name proposal

Aww, bummer. If you posted a faulty_mcfault_face_test, I would absolutely apply that and force Paolo to tell me no :-)

author reviewer
#10 lore raw

Re: [PATCH v3 2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

Am 23.10.26 um 17:06 schrieb Sean Christopherson:

Quoted 12 lines · Christian Borntraeger
Quoted 11 lines

Add a test in which the guest writes to a shmem-backed memslot beyond the end of the file. KVM_RUN must fail with -EFAULT.

On x86 the guest enables interrupts, and on s390 the test sets up pfault, so that KVM could handle the fault with an asynchronous page fault. If KVM mistakes the error for a fault that needs I/O, the vCPU retries the access forever, and the test fails after a timeout.

The test is not built for LoongArch, which handles a failed mapping as an MMIO access.

Quoted 6 lines
Quoted 3 lines

---
I went with Seans name proposal

Aww, bummer. If you posted a faulty_mcfault_face_test, I would absolutely apply that and force Paolo to tell me no :-)

Sean,
Will you pick up the fix and the selftest, shall I do it or Paolo directly?

#11 lore raw

Re: [PATCH v3 2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

Quoted 17 lines · Christian Borntraeger

Am 23.10.26 um 17:06 schrieb Sean Christopherson:

Quoted 16 lines · Christian Borntraeger
Quoted 14 lines

Add a test in which the guest writes to a shmem-backed memslot beyond the end of the file. KVM_RUN must fail with -EFAULT.

On x86 the guest enables interrupts, and on s390 the test sets up pfault, so that KVM could handle the fault with an asynchronous page fault. If KVM mistakes the error for a fault that needs I/O, the vCPU retries the access forever, and the test fails after a timeout.

The test is not built for LoongArch, which handles a failed mapping as an MMIO access.

---
I went with Seans name proposal

Quoted 5 lines
Quoted 2 lines

Aww, bummer. If you posted a faulty_mcfault_face_test, I would absolutely apply that and force Paolo to tell me no :-)

Sean,
Will you pick up the fix and the selftest, shall I do it or Paolo directly?

I can grab 'em. Were you hoping to get the fix into 7.4? It doesn't seem super urgent? I was leaning towards waiting for 7.5, but tagging it for stable@. I.e. give ourselves a full cycle to make sure we didn't miss something, then get the fix backported because I wouldn't be surprised if people are hitting this in production without realizing it.

author reviewer
#12 lore raw

Re: [PATCH v3 2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

Am 05.11.26 um 02:02 schrieb Sean Christopherson:

Quoted 21 lines · Christian Borntraeger
Quoted 20 lines

Am 23.10.26 um 17:06 schrieb Sean Christopherson:

Quoted 18 lines · Christian Borntraeger
Quoted 8 lines

Add a test in which the guest writes to a shmem-backed memslot beyond the end of the file. KVM_RUN must fail with -EFAULT.

On x86 the guest enables interrupts, and on s390 the test sets up pfault, so that KVM could handle the fault with an asynchronous page fault. If KVM mistakes the error for a fault that needs I/O, the vCPU retries the access forever, and the test fails after a timeout.

Quoted 6 lines

The test is not built for LoongArch, which handles a failed mapping as an MMIO access.

---
I went with Seans name proposal

Aww, bummer. If you posted a faulty_mcfault_face_test, I would absolutely apply that and force Paolo to tell me no :-)

Quoted 8 lines
Quoted 2 lines

Sean,
Will you pick up the fix and the selftest, shall I do it or Paolo directly?

I can grab 'em. Were you hoping to get the fix into 7.4? It doesn't seem super urgent? I was leaning towards waiting for 7.5, but tagging it for stable@. I.e. give ourselves a full cycle to make sure we didn't miss something, then get the fix backported because I wouldn't be surprised if people are hitting this in production without realizing it.

No, not super urgent. Might not even qualify for stable.

author reviewer
#13 lore raw
In reply to Christian Borntraeger

Ping?

Am 20.10.26 um 19:58 schrieb Christian Borntraeger:

Quoted 18 lines

Return mapping errors also for async_pf cases. Otherwise guests will loop forever. Nothing critical just missing error handling.

v1->v2:
- !npages instead of npages == 0 (Sean)
- new block comment (Sean)
- fix testcase to build with 7.3 and 7.4-rc

Christian Borntraeger (2):
KVM: Set up async page faults only for faults that need to wait
KVM: selftests: Test that KVM_RUN fails for unresolvable host faults

  tools/testing/selftests/kvm/Makefile.kvm      |   1 +
  .../selftests/kvm/faultin_sigbus_test.c       | 169 ++++++++++++++++++
  virt/kvm/kvm_main.c                           |   3 +-
  3 files changed, 172 insertions(+), 1 deletion(-)

create mode 100644 tools/testing/selftests/kvm/faultin_sigbus_test.c

reviewer Applied
#14 lore raw
In reply to Christian Borntraeger
Quoted 9 lines · Christian Borntraeger

Return mapping errors also for async_pf cases. Otherwise guests will loop forever. Nothing critical just missing error handling.

v1->v2:
- !npages instead of npages == 0 (Sean)
- new block comment (Sean)
- fix testcase to build with 7.3 and 7.4-rc

[...]

Applied to kvm-x86 generic, thanks!

[1/2] KVM: Set up async page faults only for faults that need to wait

      https://github.com/kvm-x86/linux/commit/9e03117114b6
[2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults
      https://github.com/kvm-x86/linux/commit/05bb06acb180

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