Skip to content

Commit 94f3f2b

Browse files
CmdrMoozybonzini
authored andcommitted
KVM: selftests: create alias mappings when using shared memory
When a memory region is added with a src_type specifying that it should use some kind of shared memory, also create an alias mapping to the same underlying physical pages. And, add an API so tests can get access to these alias addresses. Basically, for a guest physical address, let us look up the analogous host *alias* address. In a future commit, we'll modify the demand paging test to take advantage of this to exercise UFFD minor faults. The idea is, we pre-fault the underlying pages *via the alias*. When the *guest* faults, it gets a "minor" fault (PTEs don't exist yet, but a page is already in the page cache). Then, the userfaultfd theads can handle the fault: they could potentially modify the underlying memory *via the alias* if they wanted to, and then they install the PTEs and let the guest carry on via a UFFDIO_CONTINUE ioctl. Reviewed-by: Ben Gardon <[email protected]> Signed-off-by: Axel Rasmussen <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent c9befd5 commit 94f3f2b

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

tools/testing/selftests/kvm/include/kvm_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
147147
void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa);
148148
void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva);
149149
vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva);
150+
void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa);
150151

151152
/*
152153
* Address Guest Virtual to Guest Physical

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,19 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
903903
vm_userspace_mem_region_gpa_insert(&vm->regions.gpa_tree, region);
904904
vm_userspace_mem_region_hva_insert(&vm->regions.hva_tree, region);
905905
hash_add(vm->regions.slot_hash, &region->slot_node, slot);
906+
907+
/* If shared memory, create an alias. */
908+
if (region->fd >= 0) {
909+
region->mmap_alias = mmap(NULL, region->mmap_size,
910+
PROT_READ | PROT_WRITE,
911+
vm_mem_backing_src_alias(src_type)->flag,
912+
region->fd, 0);
913+
TEST_ASSERT(region->mmap_alias != MAP_FAILED,
914+
"mmap of alias failed, errno: %i", errno);
915+
916+
/* Align host alias address */
917+
region->host_alias = align(region->mmap_alias, alignment);
918+
}
906919
}
907920

908921
/*
@@ -1333,6 +1346,42 @@ vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
13331346
return -1;
13341347
}
13351348

1349+
/*
1350+
* Address VM physical to Host Virtual *alias*.
1351+
*
1352+
* Input Args:
1353+
* vm - Virtual Machine
1354+
* gpa - VM physical address
1355+
*
1356+
* Output Args: None
1357+
*
1358+
* Return:
1359+
* Equivalent address within the host virtual *alias* area, or NULL
1360+
* (without failing the test) if the guest memory is not shared (so
1361+
* no alias exists).
1362+
*
1363+
* When vm_create() and related functions are called with a shared memory
1364+
* src_type, we also create a writable, shared alias mapping of the
1365+
* underlying guest memory. This allows the host to manipulate guest memory
1366+
* without mapping that memory in the guest's address space. And, for
1367+
* userfaultfd-based demand paging, we can do so without triggering userfaults.
1368+
*/
1369+
void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa)
1370+
{
1371+
struct userspace_mem_region *region;
1372+
uintptr_t offset;
1373+
1374+
region = userspace_mem_region_find(vm, gpa, gpa);
1375+
if (!region)
1376+
return NULL;
1377+
1378+
if (!region->host_alias)
1379+
return NULL;
1380+
1381+
offset = gpa - region->region.guest_phys_addr;
1382+
return (void *) ((uintptr_t) region->host_alias + offset);
1383+
}
1384+
13361385
/*
13371386
* VM Create IRQ Chip
13381387
*

tools/testing/selftests/kvm/lib/kvm_util_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ struct userspace_mem_region {
1919
int fd;
2020
off_t offset;
2121
void *host_mem;
22+
void *host_alias;
2223
void *mmap_start;
24+
void *mmap_alias;
2325
size_t mmap_size;
2426
struct rb_node gpa_node;
2527
struct rb_node hva_node;

0 commit comments

Comments
 (0)