Fix error handling for async_pf
Christian Borntraeger
reviewerv2 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
Christian Borntraeger
reviewerPATCH 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.
--- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +2 -1
@@ -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
[email protected]
AI reviewRe: [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 aFOLL_NOWAITgup()for EINTR, EAGAIN and EHWPOISON. There might be others like-EFAULTforVM_FAULT_SIGBUS. For those other errors, reportingKVM_PFN_ERR_NEEDS_IOwould be wrong as it triggers anasync_pfresulting in endless retries.Return
KVM_PFN_ERR_NEEDS_IOonly ifgup()returned 0 pages and report the error otherwise.Fixes 0857b9e95c1a ("KVM: Enable async page fault processing")Signed-off-by Christian Borntraeger <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2 1/2] KVM: Set up async page faults only for faults that need to wait
In reply to Christian BorntraegerQuoted 11 lines · Christian Borntraeger
hva_to_pfn()checks aFOLL_NOWAITgup()for EINTR, EAGAIN and EHWPOISON. There might be others like-EFAULTforVM_FAULT_SIGBUS. For those other errors, reportingKVM_PFN_ERR_NEEDS_IOwould be wrong as it triggers anasync_pfresulting in endless retries.Return
KVM_PFN_ERR_NEEDS_IOonly ifgup()returned 0 pages and report the error otherwise.Fixes 0857b9e95c1a ("KVM: Enable async page fault processing")Signed-off-by Christian Borntraeger <[email protected]>---
Christian Borntraeger
reviewerPATCH v2 2/2 KVM: selftests: Test that KVM_RUN fails for unresolvable host faults
In reply to Christian BorntraegerAdd 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
@@ -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
[email protected]
AI reviewRe: [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_RUNmust 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.
Signed-off-by Christian Borntraeger <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Sean Christopherson
reviewerRe: [PATCH v2 2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults
In reply to Christian BorntraegerQuoted 8 lines · Christian Borntraeger
Add a test in which the guest writes to a shmem-backed memslot beyond the end of the file.
KVM_RUNmust 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.
Signed-off-by Christian Borntraeger <[email protected]>--- 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
@@ -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));
Christian Borntraeger
reviewerRe: [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_RUNmust 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
Signed-off-by Christian Borntraeger <[email protected]>--- 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()explicitlyTEST_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.
Christian Borntraeger
reviewerPATCH 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 proposalv2->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
@@ -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
Sean Christopherson
reviewerRe: [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_RUNmust 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.
Signed-off-by Christian Borntraeger <[email protected]>---
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 :-)
Christian Borntraeger
reviewerRe: [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_RUNmust 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
Signed-off-by Christian Borntraeger <[email protected]>---
I went with Seans name proposalAww, 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?
Sean Christopherson
reviewerRe: [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_RUNmust 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.
Signed-off-by Christian Borntraeger <[email protected]>---
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.
Christian Borntraeger
reviewerRe: [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_RUNmust 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.
Signed-off-by Christian Borntraeger <[email protected]>---
I went with Seans name proposalAww, 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.
Christian Borntraeger
reviewerPing?
Am 20.10.26 um 19:58 schrieb Christian Borntraeger:
Quoted 18 lines
Return mapping errors also for
async_pfcases. 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-rcChristian Borntraeger (2):
KVM: Set up async page faults only for faults that need to wait
KVM: selftests: Test thatKVM_RUNfails for unresolvable host faultstools/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
Quoted 9 lines · Christian Borntraeger
Return mapping errors also for
async_pfcases. 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