Skip to contentSkip to conversation

Fix error handling for async_pf

PATCH Superseded Christian Borntraeger 3 participants 7 messages Oct 20

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Christian Borntraeger

author reviewer
#0 lore raw

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.

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
author reviewer
#1 lore raw

PATCH 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 | 1 +
 1 file changed, 1 insertion(+)
virt/kvm/kvm_main.c +1 -0
index 5a782849521c..34b38d7512c3 100644
@@ -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
#2 lore raw

Re: [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 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
#3 lore raw

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

In reply to Christian Borntraeger
Quoted 19 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")
---
 virt/kvm/kvm_main.c | 1 +
 1 file changed, 1 insertion(+)
virt/kvm/kvm_main.c +0 -0
index 5a782849521c..34b38d7512c3 100644

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 -0
 		    vma_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.

author reviewer
#4 lore raw

PATCH 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       | 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
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 +168 -0

168 lines not on this page · raw message

Signature · 1 line
2.53.0
#5 lore raw

Re: [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
new file mode 100644
index 0000000000000..b9d673260ed5a
@@ -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
author reviewer
#6 lore raw

Re: [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: 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
new file mode 100644
index 0000000000000..b9d673260ed5a
@@ -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,

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() 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?

Yes, this works on 7.3 but fails on next