Skip to content

Commit 5485e82

Browse files
Ricardo KollerMarc Zyngier
authored andcommitted
KVM: selftests: Fix alignment in virt_arch_pgd_alloc() and vm_vaddr_alloc()
Refactor virt_arch_pgd_alloc() and vm_vaddr_alloc() in both RISC-V and aarch64 to fix the alignment of parameters in a couple of calls. This will make it easier to fix the alignment in a future commit that adds an extra parameter (that happens to be very long). No functional change intended. Suggested-by: Sean Christopherson <[email protected]> Signed-off-by: Ricardo Koller <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 290c5b5 commit 5485e82

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

tools/testing/selftests/kvm/lib/aarch64/processor.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ static uint64_t __maybe_unused ptrs_per_pte(struct kvm_vm *vm)
7676

7777
void virt_arch_pgd_alloc(struct kvm_vm *vm)
7878
{
79-
if (!vm->pgd_created) {
80-
vm_paddr_t paddr = vm_phy_pages_alloc(vm,
81-
page_align(vm, ptrs_per_pgd(vm) * 8) / vm->page_size,
82-
KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0);
83-
vm->pgd = paddr;
84-
vm->pgd_created = true;
85-
}
79+
size_t nr_pages = page_align(vm, ptrs_per_pgd(vm) * 8) / vm->page_size;
80+
81+
if (vm->pgd_created)
82+
return;
83+
84+
vm->pgd = vm_phy_pages_alloc(vm, nr_pages,
85+
KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0);
86+
vm->pgd_created = true;
8687
}
8788

8889
static void _virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
@@ -325,13 +326,15 @@ void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcpu, uint8_t indent)
325326
struct kvm_vcpu *aarch64_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
326327
struct kvm_vcpu_init *init, void *guest_code)
327328
{
328-
size_t stack_size = vm->page_size == 4096 ?
329-
DEFAULT_STACK_PGS * vm->page_size :
330-
vm->page_size;
331-
uint64_t stack_vaddr = vm_vaddr_alloc(vm, stack_size,
332-
DEFAULT_ARM64_GUEST_STACK_VADDR_MIN);
329+
size_t stack_size;
330+
uint64_t stack_vaddr;
333331
struct kvm_vcpu *vcpu = __vm_vcpu_add(vm, vcpu_id);
334332

333+
stack_size = vm->page_size == 4096 ? DEFAULT_STACK_PGS * vm->page_size :
334+
vm->page_size;
335+
stack_vaddr = vm_vaddr_alloc(vm, stack_size,
336+
DEFAULT_ARM64_GUEST_STACK_VADDR_MIN);
337+
335338
aarch64_vcpu_setup(vcpu, init);
336339

337340
vcpu_set_reg(vcpu, ARM64_CORE_REG(sp_el1), stack_vaddr + stack_size);

tools/testing/selftests/kvm/lib/riscv/processor.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ static uint64_t pte_index(struct kvm_vm *vm, vm_vaddr_t gva, int level)
5555

5656
void virt_arch_pgd_alloc(struct kvm_vm *vm)
5757
{
58-
if (!vm->pgd_created) {
59-
vm_paddr_t paddr = vm_phy_pages_alloc(vm,
60-
page_align(vm, ptrs_per_pte(vm) * 8) / vm->page_size,
61-
KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0);
62-
vm->pgd = paddr;
63-
vm->pgd_created = true;
64-
}
58+
size_t nr_pages = page_align(vm, ptrs_per_pte(vm) * 8) / vm->page_size;
59+
60+
if (vm->pgd_created)
61+
return;
62+
63+
vm->pgd = vm_phy_pages_alloc(vm, nr_pages,
64+
KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0);
65+
vm->pgd_created = true;
6566
}
6667

6768
void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
@@ -279,15 +280,17 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
279280
void *guest_code)
280281
{
281282
int r;
282-
size_t stack_size = vm->page_size == 4096 ?
283-
DEFAULT_STACK_PGS * vm->page_size :
284-
vm->page_size;
285-
unsigned long stack_vaddr = vm_vaddr_alloc(vm, stack_size,
286-
DEFAULT_RISCV_GUEST_STACK_VADDR_MIN);
283+
size_t stack_size;
284+
unsigned long stack_vaddr;
287285
unsigned long current_gp = 0;
288286
struct kvm_mp_state mps;
289287
struct kvm_vcpu *vcpu;
290288

289+
stack_size = vm->page_size == 4096 ? DEFAULT_STACK_PGS * vm->page_size :
290+
vm->page_size;
291+
stack_vaddr = vm_vaddr_alloc(vm, stack_size,
292+
DEFAULT_RISCV_GUEST_STACK_VADDR_MIN);
293+
291294
vcpu = __vm_vcpu_add(vm, vcpu_id);
292295
riscv_vcpu_mmu_setup(vcpu);
293296

0 commit comments

Comments
 (0)