Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Dongsheng Zhang <dongsheng.x.zhang@intel.com>
To: linux-kselftest@vger.kernel.org
Subject: [PATCH 2/2] KVM: selftests: Add assertion for mem_size in vm_mem_add()
Date: Thu, 25 Apr 2024 06:44:34 +0800	[thread overview]
Message-ID: <20240424224434.14166-2-dongsheng.x.zhang@intel.com> (raw)
In-Reply-To: <20240424224434.14166-1-dongsheng.x.zhang@intel.com>

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


  reply	other threads:[~2024-04-24 22:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2024-05-21 20:51 ` Zhang, Dongsheng X

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240424224434.14166-2-dongsheng.x.zhang@intel.com \
    --to=dongsheng.x.zhang@intel.com \
    --cc=linux-kselftest@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox