Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kvm: selftests: ucall: fix exit mmio address guessing

Fix two more bugs in the exit_mmio address guessing.
The first bug was that the start and step calculations were
wrong since they were dividing the number of address bits instead
of the address space. The second other bug was that the guessing
algorithm wasn't considering the valid physical and virtual address
ranges correctly for an identity map.

Signed-off-by: Andrew Jones <drjones@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Andrew Jones and committed by
Paolo Bonzini
57d5edfe 2bcbd406

+19 -11
+19 -11
tools/testing/selftests/kvm/lib/ucall.c
··· 35 35 36 36 if (type == UCALL_MMIO) { 37 37 vm_paddr_t gpa, start, end, step, offset; 38 + unsigned bits; 38 39 bool ret; 39 40 40 41 if (arg) { ··· 46 45 } 47 46 48 47 /* 49 - * Find an address within the allowed virtual address space, 50 - * that does _not_ have a KVM memory region associated with it. 51 - * Identity mapping an address like this allows the guest to 48 + * Find an address within the allowed physical and virtual address 49 + * spaces, that does _not_ have a KVM memory region associated with 50 + * it. Identity mapping an address like this allows the guest to 52 51 * access it, but as KVM doesn't know what to do with it, it 53 52 * will assume it's something userspace handles and exit with 54 53 * KVM_EXIT_MMIO. Well, at least that's how it works for AArch64. 55 - * Here we start with a guess that the addresses around two 56 - * thirds of the VA space are unmapped and then work both down 57 - * and up from there in 1/12 VA space sized steps. 54 + * Here we start with a guess that the addresses around 5/8th 55 + * of the allowed space are unmapped and then work both down and 56 + * up from there in 1/16th allowed space sized steps. 57 + * 58 + * Note, we need to use VA-bits - 1 when calculating the allowed 59 + * virtual address space for an identity mapping because the upper 60 + * half of the virtual address space is the two's complement of the 61 + * lower and won't match physical addresses. 58 62 */ 59 - start = 1ul << (vm->va_bits * 2 / 3); 60 - end = 1ul << vm->va_bits; 61 - step = 1ul << (vm->va_bits / 12); 63 + bits = vm->va_bits - 1; 64 + bits = vm->pa_bits < bits ? vm->pa_bits : bits; 65 + end = 1ul << bits; 66 + start = end * 5 / 8; 67 + step = end / 16; 62 68 for (offset = 0; offset < end - start; offset += step) { 63 - if (ucall_mmio_init(vm, (gpa - offset) & ~(vm->page_size - 1))) 69 + if (ucall_mmio_init(vm, start - offset)) 64 70 return; 65 - if (ucall_mmio_init(vm, (gpa + offset) & ~(vm->page_size - 1))) 71 + if (ucall_mmio_init(vm, start + offset)) 66 72 return; 67 73 } 68 74 TEST_ASSERT(false, "Can't find a ucall mmio address");