KVM: selftests: Align memory region addresses to 1M on s390x
authorThomas Huth <thuth@redhat.com>
Thu, 23 May 2019 16:43:05 +0000 (18:43 +0200)
committerChristian Borntraeger <borntraeger@de.ibm.com>
Tue, 4 Jun 2019 12:05:38 +0000 (14:05 +0200)
On s390x, there is a constraint that memory regions have to be aligned
to 1M (or running the VM will fail). Introduce a new "alignment" variable
in the vm_userspace_mem_region_add() function which now can be used for
both, huge page and s390x alignment requirements.

Signed-off-by: Thomas Huth <thuth@redhat.com>
Message-Id: <20190523164309.13345-6-thuth@redhat.com>
Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
[prepare for THP as outlined by Andrew Jones]

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

index 9ea9ba28af1f14062d8d70e77282d4e798d4cc93..ab7fcb9acf3da7c96f283e8c4e9fb99f9319aeae 100644 (file)
@@ -557,6 +557,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
        int ret;
        struct userspace_mem_region *region;
        size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size;
+       size_t alignment;
 
        TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical "
                "address not on a page boundary.\n"
@@ -606,9 +607,20 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
        TEST_ASSERT(region != NULL, "Insufficient Memory");
        region->mmap_size = npages * vm->page_size;
 
-       /* Enough memory to align up to a huge page. */
+#ifdef __s390x__
+       /* On s390x, the host address must be aligned to 1M (due to PGSTEs) */
+       alignment = 0x100000;
+#else
+       alignment = 1;
+#endif
+
        if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
-               region->mmap_size += huge_page_size;
+               alignment = max(huge_page_size, alignment);
+
+       /* Add enough memory to align up if necessary */
+       if (alignment > 1)
+               region->mmap_size += alignment;
+
        region->mmap_start = mmap(NULL, region->mmap_size,
                                  PROT_READ | PROT_WRITE,
                                  MAP_PRIVATE | MAP_ANONYMOUS
@@ -618,9 +630,8 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
                    "test_malloc failed, mmap_start: %p errno: %i",
                    region->mmap_start, errno);
 
-       /* Align THP allocation up to start of a huge page. */
-       region->host_mem = align(region->mmap_start,
-                                src_type == VM_MEM_SRC_ANONYMOUS_THP ?  huge_page_size : 1);
+       /* Align host address */
+       region->host_mem = align(region->mmap_start, alignment);
 
        /* As needed perform madvise */
        if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) {