Commit b3784bc2 authored by Axel Rasmussen's avatar Axel Rasmussen Committed by Paolo Bonzini

KVM: selftests: refactor vm_mem_backing_src_type flags

Each struct vm_mem_backing_src_alias has a flags field, which denotes
the flags used to mmap() an area of that type. Previously, this field
never included MAP_PRIVATE | MAP_ANONYMOUS, because
vm_userspace_mem_region_add assumed that *all* types would always use
those flags, and so it hardcoded them.

In a follow-up commit, we'll add a new type: shmem. Areas of this type
must not have MAP_PRIVATE | MAP_ANONYMOUS, and instead they must have
MAP_SHARED.

So, refactor things. Make it so that the flags field of
struct vm_mem_backing_src_alias really is a complete set of flags, and
don't add in any extras in vm_userspace_mem_region_add. This will let us
easily tack on shmem.
Signed-off-by: default avatarAxel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-7-axelrasmussen@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 0368c2c1
...@@ -849,8 +849,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, ...@@ -849,8 +849,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
region->mmap_start = mmap(NULL, region->mmap_size, region->mmap_start = mmap(NULL, region->mmap_size,
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS vm_mem_backing_src_alias(src_type)->flag,
| vm_mem_backing_src_alias(src_type)->flag,
-1, 0); -1, 0);
TEST_ASSERT(region->mmap_start != MAP_FAILED, TEST_ASSERT(region->mmap_start != MAP_FAILED,
"test_malloc failed, mmap_start: %p errno: %i", "test_malloc failed, mmap_start: %p errno: %i",
......
...@@ -168,70 +168,73 @@ size_t get_def_hugetlb_pagesz(void) ...@@ -168,70 +168,73 @@ size_t get_def_hugetlb_pagesz(void)
const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i) const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)
{ {
static const int anon_flags = MAP_PRIVATE | MAP_ANONYMOUS;
static const int anon_huge_flags = anon_flags | MAP_HUGETLB;
static const struct vm_mem_backing_src_alias aliases[] = { static const struct vm_mem_backing_src_alias aliases[] = {
[VM_MEM_SRC_ANONYMOUS] = { [VM_MEM_SRC_ANONYMOUS] = {
.name = "anonymous", .name = "anonymous",
.flag = 0, .flag = anon_flags,
}, },
[VM_MEM_SRC_ANONYMOUS_THP] = { [VM_MEM_SRC_ANONYMOUS_THP] = {
.name = "anonymous_thp", .name = "anonymous_thp",
.flag = 0, .flag = anon_flags,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB] = {
.name = "anonymous_hugetlb", .name = "anonymous_hugetlb",
.flag = MAP_HUGETLB, .flag = anon_huge_flags,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB] = {
.name = "anonymous_hugetlb_16kb", .name = "anonymous_hugetlb_16kb",
.flag = MAP_HUGETLB | MAP_HUGE_16KB, .flag = anon_huge_flags | MAP_HUGE_16KB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB] = {
.name = "anonymous_hugetlb_64kb", .name = "anonymous_hugetlb_64kb",
.flag = MAP_HUGETLB | MAP_HUGE_64KB, .flag = anon_huge_flags | MAP_HUGE_64KB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB] = {
.name = "anonymous_hugetlb_512kb", .name = "anonymous_hugetlb_512kb",
.flag = MAP_HUGETLB | MAP_HUGE_512KB, .flag = anon_huge_flags | MAP_HUGE_512KB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB] = {
.name = "anonymous_hugetlb_1mb", .name = "anonymous_hugetlb_1mb",
.flag = MAP_HUGETLB | MAP_HUGE_1MB, .flag = anon_huge_flags | MAP_HUGE_1MB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB] = {
.name = "anonymous_hugetlb_2mb", .name = "anonymous_hugetlb_2mb",
.flag = MAP_HUGETLB | MAP_HUGE_2MB, .flag = anon_huge_flags | MAP_HUGE_2MB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB] = {
.name = "anonymous_hugetlb_8mb", .name = "anonymous_hugetlb_8mb",
.flag = MAP_HUGETLB | MAP_HUGE_8MB, .flag = anon_huge_flags | MAP_HUGE_8MB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB] = {
.name = "anonymous_hugetlb_16mb", .name = "anonymous_hugetlb_16mb",
.flag = MAP_HUGETLB | MAP_HUGE_16MB, .flag = anon_huge_flags | MAP_HUGE_16MB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB] = {
.name = "anonymous_hugetlb_32mb", .name = "anonymous_hugetlb_32mb",
.flag = MAP_HUGETLB | MAP_HUGE_32MB, .flag = anon_huge_flags | MAP_HUGE_32MB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB] = {
.name = "anonymous_hugetlb_256mb", .name = "anonymous_hugetlb_256mb",
.flag = MAP_HUGETLB | MAP_HUGE_256MB, .flag = anon_huge_flags | MAP_HUGE_256MB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB] = {
.name = "anonymous_hugetlb_512mb", .name = "anonymous_hugetlb_512mb",
.flag = MAP_HUGETLB | MAP_HUGE_512MB, .flag = anon_huge_flags | MAP_HUGE_512MB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB] = {
.name = "anonymous_hugetlb_1gb", .name = "anonymous_hugetlb_1gb",
.flag = MAP_HUGETLB | MAP_HUGE_1GB, .flag = anon_huge_flags | MAP_HUGE_1GB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB] = {
.name = "anonymous_hugetlb_2gb", .name = "anonymous_hugetlb_2gb",
.flag = MAP_HUGETLB | MAP_HUGE_2GB, .flag = anon_huge_flags | MAP_HUGE_2GB,
}, },
[VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB] = { [VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB] = {
.name = "anonymous_hugetlb_16gb", .name = "anonymous_hugetlb_16gb",
.flag = MAP_HUGETLB | MAP_HUGE_16GB, .flag = anon_huge_flags | MAP_HUGE_16GB,
}, },
}; };
_Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES, _Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment