* [PATCH 1/2] KVM: selftests: Fix nx_huge_pages_test for default_hugepagesz=1G
@ 2024-04-24 22:44 Dongsheng Zhang
2024-04-24 22:44 ` [PATCH 2/2] KVM: selftests: Add assertion for mem_size in vm_mem_add() Dongsheng Zhang
2024-05-21 20:51 ` [PATCH 1/2] KVM: selftests: Fix nx_huge_pages_test for default_hugepagesz=1G Zhang, Dongsheng X
0 siblings, 2 replies; 3+ messages in thread
From: Dongsheng Zhang @ 2024-04-24 22:44 UTC (permalink / raw)
To: linux-kselftest
From: donsheng <dongsheng.x.zhang@intel.com>
If the host was booted with the "default_hugepagesz=1G" kernel command-line
parameter, running the NX hugepage test will fail with error "Invalid argument"
at the TEST_ASSERT line in kvm_util.c's __vm_mem_region_delete() function:
static void __vm_mem_region_delete(struct kvm_vm *vm,
struct userspace_mem_region *region,
bool unlink)
{
int ret;
...
ret = munmap(region->mmap_start, region->mmap_size);
TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
...
}
NX hugepage test creates a VM with a data slot of 6M size backed with huge
pages. If the default hugetlb page size is set to 1G, calling mmap() with
MAP_HUGETLB and a length of 6M will succeed but calling its matching munmap()
will fail. Documentation/admin-guide/mm/hugetlbpage.rst specifies this behavior:
"Syscalls that operate on memory backed by hugetlb pages only have their lengths
aligned to the native page size of the processor; they will normally fail with
errno set to EINVAL or exclude hugetlb pages that extend beyond the length if
not hugepage aligned. For example, munmap(2) will fail if memory is backed by
a hugetlb page and the length is smaller than the hugepage size."
Explicitly use MAP_HUGE_2MB in conjunction with MAP_HUGETLB to fix the issue.
Signed-off-by: donsheng <dongsheng.x.zhang@intel.com>
Suggested-by: Zide Chen <zide.chen@intel.com>
---
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
index 17bbb96fc4df..146e9033e206 100644
--- a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
+++ b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
@@ -129,7 +129,7 @@ void run_test(int reclaim_period_ms, bool disable_nx_huge_pages,
vcpu = vm_vcpu_add(vm, 0, guest_code);
- vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_HUGETLB,
+ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB,
HPAGE_GPA, HPAGE_SLOT,
HPAGE_SLOT_NPAGES, 0);
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] KVM: selftests: Add assertion for mem_size in vm_mem_add()
2024-04-24 22:44 [PATCH 1/2] KVM: selftests: Fix nx_huge_pages_test for default_hugepagesz=1G Dongsheng Zhang
@ 2024-04-24 22:44 ` Dongsheng Zhang
2024-05-21 20:51 ` [PATCH 1/2] KVM: selftests: Fix nx_huge_pages_test for default_hugepagesz=1G Zhang, Dongsheng X
1 sibling, 0 replies; 3+ messages in thread
From: Dongsheng Zhang @ 2024-04-24 22:44 UTC (permalink / raw)
To: linux-kselftest
From: donsheng <dongsheng.x.zhang@intel.com>
If mmap() is called with MAP_HUGETLB and the requested mapping size is
not a multiple of the underlying hugetlb page size, kernel will automatically
align the mmap size, but munmap() will fail if you specify the size you asked
for rather than the size that was assigned by Linux kernel mmap().
To avoid munmap() failure, add sanity check for mem_size in vm_mem_add() to
ensure that mem_size is aligned to page size of the underlying hugetlb backing src.
This assertion helps to ensure that vm_mem_add() is called with aligned mapping size,
especially when vm_mem_add() is called to add guest memory backed by
VM_MEM_SRC_ANONYMOUS_HUGETLB which relies on the default hugetlb page size.
Signed-off-by: donsheng <dongsheng.x.zhang@intel.com>
---
tools/testing/selftests/kvm/lib/kvm_util.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index b2262b5fad9e..827bb3d57815 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -985,6 +985,11 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
" vm->max_gfn: 0x%lx vm->page_size: 0x%x",
guest_paddr, npages, vm->max_gfn, vm->page_size);
+ TEST_ASSERT(!(is_backing_src_hugetlb(src_type) &&
+ (mem_size & (backing_src_pagesz - 1))),
+ "mem_size 0x%lx is not aligned to backing src %s's page size 0x%lx",
+ mem_size, vm_mem_backing_src_alias(src_type)->name, backing_src_pagesz);
+
/*
* Confirm a mem region with an overlapping address doesn't
* already exist.
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] KVM: selftests: Fix nx_huge_pages_test for default_hugepagesz=1G
2024-04-24 22:44 [PATCH 1/2] KVM: selftests: Fix nx_huge_pages_test for default_hugepagesz=1G Dongsheng Zhang
2024-04-24 22:44 ` [PATCH 2/2] KVM: selftests: Add assertion for mem_size in vm_mem_add() Dongsheng Zhang
@ 2024-05-21 20:51 ` Zhang, Dongsheng X
1 sibling, 0 replies; 3+ messages in thread
From: Zhang, Dongsheng X @ 2024-05-21 20:51 UTC (permalink / raw)
To: "<shuah"; +Cc: linux-kselftest
Hi, Shuah,
Any comment on this series?
Thanks,
don
On 4/24/2024 3:44 PM, Dongsheng Zhang wrote:
> From: donsheng <dongsheng.x.zhang@intel.com>
>
> If the host was booted with the "default_hugepagesz=1G" kernel command-line
> parameter, running the NX hugepage test will fail with error "Invalid argument"
> at the TEST_ASSERT line in kvm_util.c's __vm_mem_region_delete() function:
> static void __vm_mem_region_delete(struct kvm_vm *vm,
> struct userspace_mem_region *region,
> bool unlink)
> {
> int ret;
> ...
> ret = munmap(region->mmap_start, region->mmap_size);
> TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
> ...
> }
>
> NX hugepage test creates a VM with a data slot of 6M size backed with huge
> pages. If the default hugetlb page size is set to 1G, calling mmap() with
> MAP_HUGETLB and a length of 6M will succeed but calling its matching munmap()
> will fail. Documentation/admin-guide/mm/hugetlbpage.rst specifies this behavior:
>
> "Syscalls that operate on memory backed by hugetlb pages only have their lengths
> aligned to the native page size of the processor; they will normally fail with
> errno set to EINVAL or exclude hugetlb pages that extend beyond the length if
> not hugepage aligned. For example, munmap(2) will fail if memory is backed by
> a hugetlb page and the length is smaller than the hugepage size."
>
> Explicitly use MAP_HUGE_2MB in conjunction with MAP_HUGETLB to fix the issue.
>
> Signed-off-by: donsheng <dongsheng.x.zhang@intel.com>
> Suggested-by: Zide Chen <zide.chen@intel.com>
> ---
> tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
> index 17bbb96fc4df..146e9033e206 100644
> --- a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
> +++ b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
> @@ -129,7 +129,7 @@ void run_test(int reclaim_period_ms, bool disable_nx_huge_pages,
>
> vcpu = vm_vcpu_add(vm, 0, guest_code);
>
> - vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_HUGETLB,
> + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB,
> HPAGE_GPA, HPAGE_SLOT,
> HPAGE_SLOT_NPAGES, 0);
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-05-21 20:51 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-24 22:44 [PATCH 1/2] KVM: selftests: Fix nx_huge_pages_test for default_hugepagesz=1G Dongsheng Zhang
2024-04-24 22:44 ` [PATCH 2/2] KVM: selftests: Add assertion for mem_size in vm_mem_add() Dongsheng Zhang
2024-05-21 20:51 ` [PATCH 1/2] KVM: selftests: Fix nx_huge_pages_test for default_hugepagesz=1G Zhang, Dongsheng X
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox