Fix error handling for async_pf
Christian Borntraeger
reviewercover 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.
can certainly wait after the merge window, but if anyone has time to review..
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 | 168 ++++++++++++++++++ virt/kvm/kvm_main.c | 1 + 3 files changed, 170 insertions(+) create mode 100644 tools/testing/selftests/kvm/faultin_sigbus_test.c
Signature · 1 line
2.53.0
Christian Borntraeger
reviewerPATCH 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 | 1 + 1 file changed, 1 insertion(+)
virt/kvm/kvm_main.c +1 -0
@@ -3048,6 +3048,7 @@ kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp) pfn =KVM_PFN_ERR_FAULT ; }else {if ((kfp->flags &FOLL_NOWAIT ) && + npages ==0 &&/* no PFN_ERR_NEEDS_IO on errors */ 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 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
Sean Christopherson
reviewerRe: [PATCH 1/2] KVM: Set up async page faults only for faults that need to wait
In reply to Christian BorntraegerQuoted 19 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]>--- virt/kvm/kvm_main.c | 1 + 1 file changed, 1 insertion(+)virt/kvm/kvm_main.c +0
-0
virt/kvm/kvm_main.c kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp) · line 3051
Quoted 4 lines
virt/kvm/kvm_main.c +1
-0@@ -3048,6 +3048,7 @@ kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp) pfn =KVM_PFN_ERR_FAULT ; }else { if ((kfp->flags & FOLL_NOWAIT) && + npages == 0 && /* no PFN_ERR_NEEDS_IO on errors */
!npages is generally preferred over "== 0".
virt/kvm/kvm_main.c kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp) · line 3054
Quoted 3 lines
virt/kvm/kvm_main.c +0
-0vma_is_valid(vma, kfp->flags & FOLL_WRITE)) pfn =KVM_PFN_ERR_NEEDS_IO ; else
Maybe instead of calling out "on errors", explicitly call out that GUP returns '0' when the fault needs to be retried? And if we move the npages check first, then IMO we can use a block comment instead of a trailing comment, e.g.
/* 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 pfn = KVM_PFN_ERR_FAULT;
For anyone else that found this surprising, while faultin_page() returns -EBUSY or -EAGAIN, __get_user_pages() converts those to '0':
ret = faultin_page(vma, start, gup_flags,
PTR_ERR(page) == -EMLINK, locked);
switch (ret) {
case 0:
goto retry;
case -EBUSY:
case -EAGAIN:
ret = 0;
fallthrough;
case -EFAULT:
case -ENOMEM:
case -EHWPOISON:
goto out;
}It'd be loved if GUP return EWOULDBLOCK for this case, but I can only imagine the pain that could cause for the rest of the kernel.
Christian Borntraeger
reviewerPATCH 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 | 168 ++++++++++++++++++ 2 files changed, 169 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 +168 -0
168 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] KVM: selftests: missing gmem_flags argument to vm_mem_add() in main() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM: selftests: missing gmem_flags argument to vm_mem_add() in main() --
commit 3a1c0f90404979b6638dbd3c2769d7b78633b104
Author: Christian Borntraeger <[email protected]>
KVM: selftests: Test that KVM_RUN fails for unresolvable host faults
This commit adds a selftest in which a guest writes to a shmem-backed memslot beyond the end of the file to verify that KVM_RUN fails with -EFAULT rather than retrying an asynchronous page fault indefinitely.
Quoted 6 lines
tools/testing/selftests/kvm/faultin_sigbus_test.c new file +0
-0@@ -0,0 +1,168 @@
[ ... ]
Quoted 11 lines
tools/testing/selftests/kvm/faultin_sigbus_test.c new file +11
-0+int main (void ) +{ +struct sigaction sa = { .sa_handler = sig_alarm };/* no SA_RESTART */ +struct userspace_mem_region *region; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +gpa_t gpa; +gva_t gva; +u64 npages; +int r;
tools/testing/selftests/kvm/faultin_sigbus_test.c line 131
Quoted 10 lines
tools/testing/selftests/kvm/faultin_sigbus_test.c new file +10
-0+ + vm =vm_create_with_one_vcpu (&vcpu, guest_code); + +/* After slot 0 and the page that some architectures use for ucalls. */ + region =memslot2region (vm,0 ); + gpa =align_up (region->region.guest_phys_addr + + region->region.memory_size + vm->page_size,TEST_SIZE ); + npages =TEST_SIZE / vm->page_size; + +vm_mem_add (vm,VM_MEM_SRC_SHMEM , gpa,TEST_SLOT , npages,0 ,-1 ,0 );
[Severity: Medium]
Does this fail to build when compiling KVM selftests?
In tools/testing/selftests/kvm/include/kvm_util.h (and tools/testing/selftests/kvm/lib/kvm_util.c), vm_mem_add() takes 9 arguments following commit ee455183c92be7:
void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
gpa_t gpa, u32 slot, u64 npages, u32 flags, int gmem_fd, u64 gmem_offset, u64 gmem_flags);
Calling vm_mem_add() in main() with only 8 arguments omits the final gmem_flags parameter and fails compilation with too few arguments to function vm_mem_add. Should this pass the 9th argument (0), or use the 6-argument helper vm_userspace_mem_region_add(vm, VM_MEM_SRC_SHMEM, gpa, TEST_SLOT, npages, 0) instead?
Quoted 3 lines
tools/testing/selftests/kvm/faultin_sigbus_test.c new file +3
-0+ region =memslot2region (vm,TEST_SLOT ); +/* Only the first host page is backed, everything behind it is SIGBUS. */ +TEST_ASSERT (!ftruncate (region->fd,getpagesize ()),"ftruncate() failed" );
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Christian Borntraeger
reviewerRe: [PATCH 2/2] KVM: selftests: Test that KVM_RUN fails for unresolvable host faults
Am 20.10.26 um 16:40 schrieb [email protected]:
Quoted 52 lines
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM: selftests: missinggmem_flagsargument tovm_mem_add()inmain()--commit
3a1c0f90404979b6638dbd3c2769d7b78633b104
Author: Christian Borntraeger <[email protected]>KVM: selftests: Test that
KVM_RUNfails for unresolvable host faultsThis commit adds a selftest in which a guest writes to a shmem-backed memslot beyond the end of the file to verify that
KVM_RUNfails with-EFAULTrather than retrying an asynchronous page fault indefinitely.Quoted 6 lines
tools/testing/selftests/kvm/faultin_sigbus_test.c new file +0
-0@@ -0,0 +1,168 @@[ ... ]
Quoted 11 lines
tools/testing/selftests/kvm/faultin_sigbus_test.c new file +11
-0+int main (void ) +{ +struct sigaction sa = { .sa_handler = sig_alarm };/* no SA_RESTART */ +struct userspace_mem_region *region; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +gpa_t gpa; +gva_t gva; +u64 npages; +int r;tools/testing/selftests/kvm/faultin_sigbus_test.c line 131
Quoted 10 lines
tools/testing/selftests/kvm/faultin_sigbus_test.c new file +10
-0+ + vm =vm_create_with_one_vcpu (&vcpu, guest_code); + +/* After slot 0 and the page that some architectures use for ucalls. */ + region =memslot2region (vm,0 ); + gpa =align_up (region->region.guest_phys_addr + + region->region.memory_size + vm->page_size,TEST_SIZE ); + npages =TEST_SIZE / vm->page_size; + +vm_mem_add (vm,VM_MEM_SRC_SHMEM , gpa,TEST_SLOT , npages,0 ,-1 ,0 );[Severity: Medium]
Does this fail to build when compiling KVM selftests?In
tools/testing/selftests/kvm/include/kvm_util.h(andtools/testing/selftests/kvm/lib/kvm_util.c),vm_mem_add()takes 9 arguments following commitee455183c92be7:void vm_mem_add(struct
kvm_vm*vm, enumvm_mem_backing_src_typesrc_type,
Quoted 8 lines
gpa_t gpa, u32 slot, u64 npages, u32 flags, int gmem_fd, u64 gmem_offset, u64 gmem_flags);Calling
vm_mem_add()inmain()with only 8 arguments omits the finalgmem_flagsparameter and fails compilation with too few arguments to functionvm_mem_add. Should this pass the 9th argument (0), or use the 6-argument helpervm_userspace_mem_region_add(vm, VM_MEM_SRC_SHMEM, gpa, TEST_SLOT, npages, 0)instead?
Yes, this works on 7.3 but fails on next