mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] selftests: mincore: use default huge page size for hugetlb test
@ 2026-06-26 12:59 Yijia Wang
  2026-07-06  1:18 ` Andrew Morton
  0 siblings, 1 reply; 2+ messages in thread
From: Yijia Wang @ 2026-06-26 12:59 UTC (permalink / raw)
  To: linux-kselftest; +Cc: linux-kernel, linux-mm, shuah, Yijia Wang

The hugetlb mincore test passes the base page size as the MAP_HUGETLB
mapping length.  This works on systems where the default huge page size
is the same as the base page size, but mmap() can fail with EINVAL before
mincore() is exercised when the default huge page size is larger, such as
on arm64 systems with 64K base pages and 2M huge pages.

Use the default huge page size from /proc/meminfo for the hugetlb mapping
and unmap length.  Keep the mincore() check scoped to one base page since
the test only needs to verify the residency state before and after the
mapping is touched.  This also avoids changing the mincore() length to the
whole huge page, which would require a larger residency vector because
mincore() reports one byte per base page.

Signed-off-by: Yijia Wang <wangyijia.yeah@bytedance.com>
---
 tools/testing/selftests/mincore/mincore_selftest.c | 29 ++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/mincore/mincore_selftest.c b/tools/testing/selftests/mincore/mincore_selftest.c
index cdd022c1c..cd51420d5 100644
--- a/tools/testing/selftests/mincore/mincore_selftest.c
+++ b/tools/testing/selftests/mincore/mincore_selftest.c
@@ -22,6 +22,27 @@
 #define MB (1UL << 20)
 #define FILE_SIZE (4 * MB)
 
+static unsigned long default_huge_page_size(void)
+{
+	FILE *f = fopen("/proc/meminfo", "r");
+	unsigned long hps = 0;
+	size_t linelen = 0;
+	char *line = NULL;
+
+	if (!f)
+		return 0;
+	while (getline(&line, &linelen, f) > 0) {
+		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
+			hps <<= 10;
+			break;
+		}
+	}
+
+	free(line);
+	fclose(f);
+	return hps;
+}
+
 
 /*
  * Tests the user interface. This test triggers most of the documented
@@ -139,14 +160,18 @@ TEST(check_anonymous_locked_pages)
 TEST(check_huge_pages)
 {
 	unsigned char vec[1];
+	unsigned long hpage_size;
 	char *addr;
 	int retval;
 	int page_size;
 
 	page_size = sysconf(_SC_PAGESIZE);
+	hpage_size = default_huge_page_size();
+	if (!hpage_size)
+		hpage_size = page_size;
 
 	errno = 0;
-	addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+	addr = mmap(NULL, hpage_size, PROT_READ | PROT_WRITE,
 		MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
 		-1, 0);
 	if (addr == MAP_FAILED) {
@@ -170,7 +195,7 @@ TEST(check_huge_pages)
 	}
 
 	munlock(addr, page_size);
-	munmap(addr, page_size);
+	munmap(addr, hpage_size);
 }
 
 

---
base-commit: 1d5dcaa3bd65f2e8c9baa14a393d3a2dc5db7524
change-id: 20260626-b4-mincore-preview-2ae0c3d65eb1

Best regards,
-- 
Yijia Wang <wangyijia.yeah@bytedance.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] selftests: mincore: use default huge page size for hugetlb test
  2026-06-26 12:59 [PATCH] selftests: mincore: use default huge page size for hugetlb test Yijia Wang
@ 2026-07-06  1:18 ` Andrew Morton
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2026-07-06  1:18 UTC (permalink / raw)
  To: Yijia Wang; +Cc: linux-kselftest, linux-kernel, linux-mm, shuah

On Fri, 26 Jun 2026 20:59:02 +0800 "Yijia Wang" <wangyijia.yeah@bytedance.com> wrote:

> The hugetlb mincore test passes the base page size as the MAP_HUGETLB
> mapping length.  This works on systems where the default huge page size
> is the same as the base page size, but mmap() can fail with EINVAL before
> mincore() is exercised when the default huge page size is larger, such as
> on arm64 systems with 64K base pages and 2M huge pages.
> 
> Use the default huge page size from /proc/meminfo for the hugetlb mapping
> and unmap length.  Keep the mincore() check scoped to one base page since
> the test only needs to verify the residency state before and after the
> mapping is touched.  This also avoids changing the mincore() length to the
> whole huge page, which would require a larger residency vector because
> mincore() reports one byte per base page.

Thanks.

AI review flagged a couple of possible issues - can you please take a
look?

https://sashiko.dev/#/patchset/20260626-b4-mincore-preview-v1-1-5f07aa02ab32@bytedance.com

> --- a/tools/testing/selftests/mincore/mincore_selftest.c
> +++ b/tools/testing/selftests/mincore/mincore_selftest.c
> @@ -22,6 +22,27 @@
>  #define MB (1UL << 20)
>  #define FILE_SIZE (4 * MB)
>  
> +static unsigned long default_huge_page_size(void)

This would be our fifth(?) implementation of default_huge_page_size()
in tools/testing/selftests.  There's a project for someone!


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-06  1:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-26 12:59 [PATCH] selftests: mincore: use default huge page size for hugetlb test Yijia Wang
2026-07-06  1:18 ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox