Unless we want to test with THP, then we shouldn't require it to be configured by the host kernel. Unfortunately, even advising with MADV_NOHUGEPAGE does require it, so check for THP first in order to avoid madvise failing with EINVAL.
Unified · Split
| @@ -14,6 +14,7 @@ · 13 unmodified lines | ||
| 14 | 14 | #include <sys/mman.h> |
| 15 | 15 | #include <sys/types.h> |
| 16 | 16 | #include <sys/stat.h> |
| 17 | +#include <unistd.h> | |
| 17 | 18 | #include <linux/kernel.h> |
| 18 | 19 | |
| 19 | 20 | #define KVM_UTIL_PGS_PER_HUGEPG 512 |
| @@ -664,13 +665,21 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, · 644 unmodified lines | ||
| 664 | 665 | |
| 665 | 666 | /* As needed perform madvise */ |
| 666 | 667 | if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) { |
| 667 | − ret = madvise(region->host_mem, npages * vm->page_size, | |
| 668 | − src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE); | |
| 669 | − TEST_ASSERT(ret == 0, "madvise failed,\n" | |
| 670 | − " addr: %p\n" | |
| 671 | − " length: 0x%lx\n" | |
| 672 | − " src_type: %x", | |
| 673 | − region->host_mem, npages * vm->page_size, src_type); | |
| 668 | + struct stat statbuf; | |
| 669 | + | |
| 670 | + ret = stat("/sys/kernel/mm/transparent_hugepage", &statbuf); | |
| 671 | + TEST_ASSERT(ret == 0 || (ret == -1 && errno == ENOENT), | |
| 672 | + "stat /sys/kernel/mm/transparent_hugepage"); | |
| 673 | + | |
| 674 | + TEST_ASSERT(ret == 0 || src_type != VM_MEM_SRC_ANONYMOUS_THP, | |
| 675 | + "VM_MEM_SRC_ANONYMOUS_THP requires THP to be configured in the host kernel"); | |
| 676 | + | |
| 677 | + if (ret == 0) { | |
| 678 | + ret = madvise(region->host_mem, npages * vm->page_size, | |
| 679 | + src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE); | |
| 680 | + TEST_ASSERT(ret == 0, "madvise failed, addr: %p length: 0x%lx src_type: %x", | |
| 681 | + region->host_mem, npages * vm->page_size, src_type); | |
| 682 | + } | |
| 674 | 683 | } |
| 675 | 684 | |
| 676 | 685 | region->unused_phy_pages = sparsebit_alloc(); |