Commit 85819fa0 authored by Sean Christopherson's avatar Sean Christopherson
Browse files

KVM: selftests: Drop "vaddr_" from APIs that allocate memory for a given VM

Now that KVM selftests use gva_t instead of vm_vaddr_t, drop "vaddr_" from
the core memory allocation APIs as the information is extraneous and does
more harm than good.  E.g. the APIs don't _just_ allocate virtual memory,
they allocate backing physical memory and install mappings in the guest
page tables.  And as proven by kmalloc() and malloc(), developers generally
expect that allocations come with a working virtual address.

Opportunistically clean up the function comment for vm_alloc(), and drop
the misleading and superfluous comments for its wrappers.

No functional change intended.

Link: https://patch.msgid.link/20260420212004.3938325-12-seanjc@google.com


Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
parent 6ec982b5
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -771,7 +771,7 @@ static void test_vgic(u32 nr_irqs, bool level_sensitive, bool eoi_split)
	vcpu_init_descriptor_tables(vcpu);

	/* Setup the guest args page (so it gets the args). */
	args_gva = vm_vaddr_alloc_page(vm);
	args_gva = vm_alloc_page(vm);
	memcpy(addr_gva2hva(vm, args_gva), &args, sizeof(args));
	vcpu_args_set(vcpu, 1, args_gva);

@@ -997,7 +997,7 @@ static void test_vgic_two_cpus(void *gcode)
	vcpu_init_descriptor_tables(vcpus[1]);

	/* Setup the guest args page (so it gets the args). */
	args_gva = vm_vaddr_alloc_page(vm);
	args_gva = vm_alloc_page(vm);
	memcpy(addr_gva2hva(vm, args_gva), &args, sizeof(args));
	vcpu_args_set(vcpus[0], 2, args_gva, 0);
	vcpu_args_set(vcpus[1], 2, args_gva, 1);
+8 −8
Original line number Diff line number Diff line
@@ -716,14 +716,14 @@ void vm_mem_region_delete(struct kvm_vm *vm, u32 slot);
struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, u32 vcpu_id);
void vm_populate_vaddr_bitmap(struct kvm_vm *vm);
gva_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, gva_t vaddr_min);
gva_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min);
gva_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
gva_t vm_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min);
gva_t __vm_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
		 enum kvm_mem_region_type type);
gva_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
gva_t vm_alloc_shared(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
		      enum kvm_mem_region_type type);
gva_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages);
gva_t __vm_vaddr_alloc_page(struct kvm_vm *vm, enum kvm_mem_region_type type);
gva_t vm_vaddr_alloc_page(struct kvm_vm *vm);
gva_t vm_alloc_pages(struct kvm_vm *vm, int nr_pages);
gva_t __vm_alloc_page(struct kvm_vm *vm, enum kvm_mem_region_type type);
gva_t vm_alloc_page(struct kvm_vm *vm);

void virt_map(struct kvm_vm *vm, u64 vaddr, u64 paddr,
	      unsigned int npages);
+5 −5
Original line number Diff line number Diff line
@@ -422,7 +422,7 @@ static struct kvm_vcpu *__aarch64_vcpu_add(struct kvm_vm *vm, u32 vcpu_id,

	stack_size = vm->page_size == 4096 ? DEFAULT_STACK_PGS * vm->page_size :
					     vm->page_size;
	stack_vaddr = __vm_vaddr_alloc(vm, stack_size,
	stack_vaddr = __vm_alloc(vm, stack_size,
				 DEFAULT_ARM64_GUEST_STACK_VADDR_MIN,
				 MEM_REGION_DATA);

@@ -536,8 +536,8 @@ void route_exception(struct ex_regs *regs, int vector)

void vm_init_descriptor_tables(struct kvm_vm *vm)
{
	vm->handlers = __vm_vaddr_alloc(vm, sizeof(struct handlers),
					vm->page_size, MEM_REGION_DATA);
	vm->handlers = __vm_alloc(vm, sizeof(struct handlers), vm->page_size,
				  MEM_REGION_DATA);

	*(gva_t *)addr_gva2hva(vm, (gva_t)(&exception_handlers)) = vm->handlers;
}
+1 −2
Original line number Diff line number Diff line
@@ -162,8 +162,7 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)
		seg_vend |= vm->page_size - 1;
		size_t seg_size = seg_vend - seg_vstart + 1;

		gva_t vaddr = __vm_vaddr_alloc(vm, seg_size, seg_vstart,
						    MEM_REGION_CODE);
		gva_t vaddr = __vm_alloc(vm, seg_size, seg_vstart, MEM_REGION_CODE);
		TEST_ASSERT(vaddr == seg_vstart, "Unable to allocate "
			"virtual memory for segment at requested min addr,\n"
			"  segment idx: %u\n"
+22 −62
Original line number Diff line number Diff line
@@ -1450,7 +1450,7 @@ gva_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, gva_t vaddr_min)
	return pgidx_start * vm->page_size;
}

static gva_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
static gva_t ____vm_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
			  enum kvm_mem_region_type type, bool protected)
{
	u64 pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
@@ -1476,84 +1476,44 @@ static gva_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
	return vaddr_start;
}

gva_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
gva_t __vm_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
		 enum kvm_mem_region_type type)
{
	return ____vm_vaddr_alloc(vm, sz, vaddr_min, type,
	return ____vm_alloc(vm, sz, vaddr_min, type,
			    vm_arch_has_protected_memory(vm));
}

gva_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
gva_t vm_alloc_shared(struct kvm_vm *vm, size_t sz, gva_t vaddr_min,
		      enum kvm_mem_region_type type)
{
	return ____vm_vaddr_alloc(vm, sz, vaddr_min, type, false);
	return ____vm_alloc(vm, sz, vaddr_min, type, false);
}

/*
 * VM Virtual Address Allocate
 *
 * Input Args:
 *   vm - Virtual Machine
 *   sz - Size in bytes
 *   vaddr_min - Minimum starting virtual address
 *
 * Output Args: None
 *
 * Return:
 *   Starting guest virtual address
 *
 * Allocates at least sz bytes within the virtual address space of the vm
 * given by vm.  The allocated bytes are mapped to a virtual address >=
 * the address given by vaddr_min.  Note that each allocation uses a
 * a unique set of pages, with the minimum real allocation being at least
 * a page. The allocated physical space comes from the TEST_DATA memory region.
 * Allocates at least sz bytes within the virtual address space of the VM
 * given by @vm.  The allocated bytes are mapped to a virtual address >= the
 * address given by @vaddr_min.  Note that each allocation uses a a unique set
 * of pages, with the minimum real allocation being at least a page. The
 * allocated physical space comes from the TEST_DATA memory region.
 */
gva_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min)
gva_t vm_alloc(struct kvm_vm *vm, size_t sz, gva_t vaddr_min)
{
	return __vm_vaddr_alloc(vm, sz, vaddr_min, MEM_REGION_TEST_DATA);
	return __vm_alloc(vm, sz, vaddr_min, MEM_REGION_TEST_DATA);
}

/*
 * VM Virtual Address Allocate Pages
 *
 * Input Args:
 *   vm - Virtual Machine
 *
 * Output Args: None
 *
 * Return:
 *   Starting guest virtual address
 *
 * Allocates at least N system pages worth of bytes within the virtual address
 * space of the vm.
 */
gva_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages)
gva_t vm_alloc_pages(struct kvm_vm *vm, int nr_pages)
{
	return vm_vaddr_alloc(vm, nr_pages * getpagesize(), KVM_UTIL_MIN_VADDR);
	return vm_alloc(vm, nr_pages * getpagesize(), KVM_UTIL_MIN_VADDR);
}

gva_t __vm_vaddr_alloc_page(struct kvm_vm *vm, enum kvm_mem_region_type type)
gva_t __vm_alloc_page(struct kvm_vm *vm, enum kvm_mem_region_type type)
{
	return __vm_vaddr_alloc(vm, getpagesize(), KVM_UTIL_MIN_VADDR, type);
	return __vm_alloc(vm, getpagesize(), KVM_UTIL_MIN_VADDR, type);
}

/*
 * VM Virtual Address Allocate Page
 *
 * Input Args:
 *   vm - Virtual Machine
 *
 * Output Args: None
 *
 * Return:
 *   Starting guest virtual address
 *
 * Allocates at least one system page worth of bytes within the virtual address
 * space of the vm.
 */
gva_t vm_vaddr_alloc_page(struct kvm_vm *vm)
gva_t vm_alloc_page(struct kvm_vm *vm)
{
	return vm_vaddr_alloc_pages(vm, 1);
	return vm_alloc_pages(vm, 1);
}

/*
Loading