* [PATCH v5] selftests/mm: hugetlb_madv_vs_map: add underflow test
@ 2026-09-14 15:36 Guillaume Morin
2026-10-04 23:00 ` [PATCH] selftests/mm: hugetlb_madv_vs_map: fix TAP plan mismatches Jaeyeon Lee
2026-10-06 8:15 ` [PATCH v5] selftests/mm: hugetlb_madv_vs_map: add underflow test Breno Leitao
0 siblings, 2 replies; 5+ messages in thread
From: Guillaume Morin @ 2026-09-14 15:36 UTC (permalink / raw)
To: linux-mm; +Cc: leitao, guillaume, rppt, Andrew Morton
Add a test that checks for underflows when a parent unmaps the page
first. Also check that when the child exits the reserve count is
correct.
Link: https://lore.kernel.org/all/alEJkwn5VlTTH_ZX@bender.morinfr.org/
Signed-off-by: Guillaume Morin <guillaume@morinfr.org>
Reviewed-by: Breno Leitao <leitao@debian.org>
Reviewed-by: Mike Rapoport <rppt@kernel.org>
Cc: Breno Leitao <leitao@debian.org>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
---
v5: address Mike and Breno's comments
v4: address Mike's comments
v3: address Mike's comments
v2: refactor tests into 2 functions
.../testing/selftests/mm/hugepage_settings.c | 9 ++
.../testing/selftests/mm/hugepage_settings.h | 1 +
.../selftests/mm/hugetlb_madv_vs_map.c | 141 +++++++++++++++---
3 files changed, 130 insertions(+), 21 deletions(-)
diff --git a/tools/testing/selftests/mm/hugepage_settings.c b/tools/testing/selftests/mm/hugepage_settings.c
index d7917dce3aba..584054736ce9 100644
--- a/tools/testing/selftests/mm/hugepage_settings.c
+++ b/tools/testing/selftests/mm/hugepage_settings.c
@@ -449,6 +449,15 @@ unsigned long hugetlb_free_pages(unsigned long size)
return read_num(path);
}
+unsigned long hugetlb_nr_resv_pages(unsigned long size)
+{
+ char path[PATH_MAX];
+
+ hugetlb_sysfs_path(path, sizeof(path), size, "resv_hugepages");
+
+ return read_num(path);
+}
+
static bool __hugetlb_setup(unsigned long size, unsigned long nr)
{
unsigned long free = hugetlb_free_pages(size);
diff --git a/tools/testing/selftests/mm/hugepage_settings.h b/tools/testing/selftests/mm/hugepage_settings.h
index 726c73c43c05..548e9d288d1d 100644
--- a/tools/testing/selftests/mm/hugepage_settings.h
+++ b/tools/testing/selftests/mm/hugepage_settings.h
@@ -98,6 +98,7 @@ unsigned long default_huge_page_size(void);
unsigned long hugetlb_nr_pages(unsigned long size);
void hugetlb_set_nr_pages(unsigned long size, unsigned long nr);
unsigned long hugetlb_free_pages(unsigned long size);
+unsigned long hugetlb_nr_resv_pages(unsigned long size);
static inline void hugetlb_save_settings(void)
{
diff --git a/tools/testing/selftests/mm/hugetlb_madv_vs_map.c b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
index f94549efcc6f..0f15eff1da04 100644
--- a/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
+++ b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
@@ -3,18 +3,6 @@
* A test case that must run on a system with one and only one huge page available.
* # echo 1 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
*
- * During setup, the test allocates the only available page, and starts three threads:
- * - thread1:
- * * madvise(MADV_DONTNEED) on the allocated huge page
- * - thread 2:
- * * Write to the allocated huge page
- * - thread 3:
- * * Try to allocated an extra huge page (which must not available)
- *
- * The test fails if thread3 is able to allocate a page.
- *
- * Touching the first page after thread3's allocation will raise a SIGBUS
- *
* Author: Breno Leitao <leitao@debian.org>
*/
#include <pthread.h>
@@ -22,6 +10,7 @@
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
+#include <sys/wait.h>
#include <unistd.h>
#include "vm_util.h"
@@ -74,7 +63,21 @@ void *map_extra(void *unused)
return NULL;
}
-int main(void)
+/* During setup in main, the only available page was allocated. This test then
+ * starts three threads:
+ *
+ * - thread1:
+ * * madvise(MADV_DONTNEED) on the allocated huge page
+ * - thread 2:
+ * * Write to the allocated huge page
+ * - thread 3:
+ * * Try to allocated an extra huge page (which must not available)
+ *
+ * The test fails if thread3 is able to allocate a page.
+ *
+ * Touching the first page after thread3's allocation will raise a SIGBUS
+ */
+void test_madv_vs_map(void)
{
pthread_t thread1, thread2, thread3;
void *ret;
@@ -85,13 +88,6 @@ int main(void)
*/
int max = 10;
- ksft_print_header();
- ksft_set_plan(1);
-
- if (!hugetlb_setup_default_exact(1))
- ksft_exit_skip("This test needs one and only one page to execute. Got %lu\n",
- hugetlb_free_default_pages());
-
mmap_size = default_huge_page_size();
while (max--) {
@@ -100,7 +96,7 @@ int main(void)
-1, 0);
if ((unsigned long)huge_ptr == -1)
- ksft_exit_fail_msg("Failed to allocate huge page\n");
+ ksft_exit_fail_perror("Failed to allocate huge page");
pthread_create(&thread1, NULL, madv, NULL);
pthread_create(&thread2, NULL, touch, NULL);
@@ -120,5 +116,108 @@ int main(void)
}
ksft_test_result_pass("No unexpected huge page allocations\n");
+}
+
+/* We create a child process, then unmap the page in the parent while the child
+ * waits and verify that there is no underflow of the reserved count. We also
+ * verify that after the child exits, the reserved count is properly restored.
+ */
+void test_underflow(void)
+{
+ pid_t pid;
+ int pipe_fds[2];
+ unsigned long nr_reserved = 0;
+
+ huge_ptr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
+
+ if ((unsigned long)huge_ptr == -1)
+ ksft_exit_fail_perror("Failed to allocate huge page");
+
+ nr_reserved = hugetlb_nr_resv_pages(default_huge_page_size());
+ if (nr_reserved != 1)
+ ksft_exit_fail_msg("Unexpected number of reserved pages: %lu, expected 1\n",
+ nr_reserved);
+
+ /* Force the fault to ensure the reservation is consumed */
+ *huge_ptr = 0;
+ nr_reserved = hugetlb_nr_resv_pages(default_huge_page_size());
+ if (nr_reserved != 0)
+ ksft_exit_fail_msg("Unexpected number of reserved pages: %lu, expected 0\n",
+ nr_reserved);
+
+ if (pipe(pipe_fds) != 0)
+ ksft_exit_fail_perror("pipe failed");
+
+ pid = fork();
+ if (pid < 0)
+ ksft_exit_fail_perror("fork failed");
+
+ if (pid == 0) {
+ /* Child: Simply wait for the parent */
+ char b;
+
+ close(pipe_fds[1]);
+ if (read(pipe_fds[0], &b, 1) < 0)
+ ksft_perror("child read failed");
+ /* Let the parent do the cleanup */
+ _exit(0);
+ }
+
+ /* Parent */
+ close(pipe_fds[0]);
+
+ /* First unmap, this will close the vma */
+ if (munmap(huge_ptr, mmap_size) != 0) {
+ ksft_perror("munmap failed");
+ goto err_cleanup;
+ }
+
+ nr_reserved = hugetlb_nr_resv_pages(default_huge_page_size());
+ if (nr_reserved == 0) {
+ ksft_test_result_pass("Underflow not present!\n");
+ } else {
+ ksft_test_result_fail("Unexpected HugePages_Rsvd=%ld after munmap, should be 0\n",
+ nr_reserved);
+ goto err_cleanup;
+ }
+ /* Make the child exit, this should restore HugePages_Rsvd to 0 */
+ if (write(pipe_fds[1], &nr_reserved, 1) < 0) {
+ /* If write failed, the child is likely already gone */
+ ksft_exit_fail_perror("write failed");
+ }
+ close(pipe_fds[1]);
+ if (waitpid(pid, NULL, 0) <= 0)
+ ksft_exit_fail_msg("waitpid failed\n");
+
+ nr_reserved = hugetlb_nr_resv_pages(default_huge_page_size());
+ if (nr_reserved == 0)
+ ksft_test_result_pass("After the child dies, HugePages_Rsvd is properly set to 0\n");
+ else
+ ksft_exit_fail_msg("Unexpected HugePages_Rsvd=%ld after the child termination munmap, should be 0\n", nr_reserved);
+
+ return;
+
+err_cleanup:
+ if (write(pipe_fds[1], &nr_reserved, 1) < 0)
+ ksft_exit_fail_perror("write failed");
+ if (waitpid(pid, NULL, 0) <= 0)
+ ksft_exit_fail_perror("waitpid failed");
+
+ ksft_exit_fail();
+}
+
+int main(void)
+{
+ ksft_print_header();
+ ksft_set_plan(3);
+
+ if (!hugetlb_setup_default_exact(1))
+ ksft_exit_skip("This test needs one and only one page to execute. Got %lu\n",
+ hugetlb_free_default_pages());
+
+ test_madv_vs_map();
+ test_underflow();
+
ksft_finished();
}
--
2.39.1
--
Guillaume Morin <guillaume@morinfr.org>
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH] selftests/mm: hugetlb_madv_vs_map: fix TAP plan mismatches
2026-09-14 15:36 [PATCH v5] selftests/mm: hugetlb_madv_vs_map: add underflow test Guillaume Morin
@ 2026-10-04 23:00 ` Jaeyeon Lee
2026-10-05 10:40 ` David Hildenbrand (Arm)
2026-10-05 11:12 ` Sarthak Sharma
2026-10-06 8:15 ` [PATCH v5] selftests/mm: hugetlb_madv_vs_map: add underflow test Breno Leitao
1 sibling, 2 replies; 5+ messages in thread
From: Jaeyeon Lee @ 2026-10-04 23:00 UTC (permalink / raw)
To: Andrew Morton, Guillaume Morin
Cc: David Hildenbrand, Lorenzo Stoakes, Liam R. Howlett,
Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
Shuah Khan, Breno Leitao, linux-mm, linux-kselftest, linux-kernel,
Jaeyeon Lee
The plan was raised from 1 to 3, but the HugeTLB setup check that may
call ksft_exit_skip() still runs after ksft_set_plan(), so a setup
failure reports one result against a plan of 3. Move ksft_set_plan()
below the setup check.
Also, when the underflow check fails, or munmap() fails,
test_underflow() jumps to err_cleanup and exits without reporting the
remaining results. Report the munmap() failure as a test result and
skip the final HugePages_Rsvd check in err_cleanup, so the output
always contains the planned 3 results.
Fixes: 827149aad495 ("selftests/mm: hugetlb_madv_vs_map: add underflow test")
Assisted-by: LLM
Signed-off-by: Jaeyeon Lee <jaeyeon.lee.dev@gmail.com>
---
tools/testing/selftests/mm/hugetlb_madv_vs_map.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/mm/hugetlb_madv_vs_map.c b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
index 1d111f42dd59..5dc44ff0a377 100644
--- a/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
+++ b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
@@ -169,6 +169,7 @@ void test_underflow(void)
/* First unmap, this will close the vma */
if (munmap(huge_ptr, mmap_size) != 0) {
ksft_perror("munmap failed");
+ ksft_test_result_fail("munmap failed\n");
goto err_cleanup;
}
@@ -203,18 +204,20 @@ void test_underflow(void)
if (waitpid(pid, NULL, 0) <= 0)
ksft_exit_fail_perror("waitpid failed");
+ ksft_test_result_skip("HugePages_Rsvd check after child exit\n");
ksft_exit_fail();
}
int main(void)
{
ksft_print_header();
- ksft_set_plan(3);
if (!hugetlb_setup_default_exact(1))
ksft_exit_skip("This test needs one and only one page to execute. Got %lu\n",
hugetlb_free_default_pages());
+ ksft_set_plan(3);
+
test_madv_vs_map();
test_underflow();
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] selftests/mm: hugetlb_madv_vs_map: fix TAP plan mismatches
2026-10-04 23:00 ` [PATCH] selftests/mm: hugetlb_madv_vs_map: fix TAP plan mismatches Jaeyeon Lee
@ 2026-10-05 10:40 ` David Hildenbrand (Arm)
2026-10-05 11:12 ` Sarthak Sharma
1 sibling, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-10-05 10:40 UTC (permalink / raw)
To: Jaeyeon Lee, Andrew Morton, Guillaume Morin
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Shuah Khan, Breno Leitao,
linux-mm, linux-kselftest, linux-kernel
On 10/5/26 01:00, Jaeyeon Lee wrote:
> The plan was raised from 1 to 3, but the HugeTLB setup check that may
> call ksft_exit_skip() still runs after ksft_set_plan(), so a setup
> failure reports one result against a plan of 3. Move ksft_set_plan()
> below the setup check.
>
> Also, when the underflow check fails, or munmap() fails,
> test_underflow() jumps to err_cleanup and exits without reporting the
> remaining results. Report the munmap() failure as a test result and
> skip the final HugePages_Rsvd check in err_cleanup, so the output
> always contains the planned 3 results.
>
> Fixes: 827149aad495 ("selftests/mm: hugetlb_madv_vs_map: add underflow test")
> Assisted-by: LLM
> Signed-off-by: Jaeyeon Lee <jaeyeon.lee.dev@gmail.com>
> ---
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] selftests/mm: hugetlb_madv_vs_map: fix TAP plan mismatches
2026-10-04 23:00 ` [PATCH] selftests/mm: hugetlb_madv_vs_map: fix TAP plan mismatches Jaeyeon Lee
2026-10-05 10:40 ` David Hildenbrand (Arm)
@ 2026-10-05 11:12 ` Sarthak Sharma
1 sibling, 0 replies; 5+ messages in thread
From: Sarthak Sharma @ 2026-10-05 11:12 UTC (permalink / raw)
To: Jaeyeon Lee, Andrew Morton, Guillaume Morin
Cc: David Hildenbrand, Lorenzo Stoakes, Liam R. Howlett,
Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
Shuah Khan, Breno Leitao, linux-mm, linux-kselftest, linux-kernel
Hi Jaeyeon!
On 10/5/26 4:30 AM, Jaeyeon Lee wrote:
> The plan was raised from 1 to 3, but the HugeTLB setup check that may
> call ksft_exit_skip() still runs after ksft_set_plan(), so a setup
> failure reports one result against a plan of 3. Move ksft_set_plan()
> below the setup check.
>
> Also, when the underflow check fails, or munmap() fails,
> test_underflow() jumps to err_cleanup and exits without reporting the
> remaining results. Report the munmap() failure as a test result and
> skip the final HugePages_Rsvd check in err_cleanup, so the output
> always contains the planned 3 results.
>
> Fixes: 827149aad495 ("selftests/mm: hugetlb_madv_vs_map: add underflow test")
> Assisted-by: LLM
> Signed-off-by: Jaeyeon Lee <jaeyeon.lee.dev@gmail.com>
> ---
> tools/testing/selftests/mm/hugetlb_madv_vs_map.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/mm/hugetlb_madv_vs_map.c b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
> index 1d111f42dd59..5dc44ff0a377 100644
> --- a/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
> +++ b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
> @@ -169,6 +169,7 @@ void test_underflow(void)
> /* First unmap, this will close the vma */
> if (munmap(huge_ptr, mmap_size) != 0) {
> ksft_perror("munmap failed");
> + ksft_test_result_fail("munmap failed\n");
This is duplicating the "munmap failed" message. Can we simply put a
ksft_test_result_fail("munmap failed: %s (%d)\n", strerror(errno),
errno) here?
Also, please send your patch in a new thread instead of replying on
another thread from next time onwards :)
> goto err_cleanup;
> }
>
> @@ -203,18 +204,20 @@ void test_underflow(void)
> if (waitpid(pid, NULL, 0) <= 0)
> ksft_exit_fail_perror("waitpid failed");
>
> + ksft_test_result_skip("HugePages_Rsvd check after child exit\n");
> ksft_exit_fail();
> }
>
> int main(void)
> {
> ksft_print_header();
> - ksft_set_plan(3);
>
> if (!hugetlb_setup_default_exact(1))
> ksft_exit_skip("This test needs one and only one page to execute. Got %lu\n",
> hugetlb_free_default_pages());
>
> + ksft_set_plan(3);
> +
> test_madv_vs_map();
> test_underflow();
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v5] selftests/mm: hugetlb_madv_vs_map: add underflow test
2026-09-14 15:36 [PATCH v5] selftests/mm: hugetlb_madv_vs_map: add underflow test Guillaume Morin
2026-10-04 23:00 ` [PATCH] selftests/mm: hugetlb_madv_vs_map: fix TAP plan mismatches Jaeyeon Lee
@ 2026-10-06 8:15 ` Breno Leitao
1 sibling, 0 replies; 5+ messages in thread
From: Breno Leitao @ 2026-10-06 8:15 UTC (permalink / raw)
To: Guillaume Morin; +Cc: linux-mm, rppt, Andrew Morton
On Mon, Sep 14, 2026 at 05:36:21PM +0200, Guillaume Morin wrote:
> Add a test that checks for underflows when a parent unmaps the page
> first. Also check that when the child exits the reserve count is
> correct.
>
> Link: https://lore.kernel.org/all/alEJkwn5VlTTH_ZX@bender.morinfr.org/
> Signed-off-by: Guillaume Morin <guillaume@morinfr.org>
> Reviewed-by: Breno Leitao <leitao@debian.org>
> Reviewed-by: Mike Rapoport <rppt@kernel.org>
> Cc: Breno Leitao <leitao@debian.org>
> Cc: Mike Rapoport <rppt@kernel.org>
> Cc: Andrew Morton <akpm@linux-foundation.org>
Tested-by: Breno Leitao <leitao@debian.org>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-10-06 8:15 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-14 15:36 [PATCH v5] selftests/mm: hugetlb_madv_vs_map: add underflow test Guillaume Morin
2026-10-04 23:00 ` [PATCH] selftests/mm: hugetlb_madv_vs_map: fix TAP plan mismatches Jaeyeon Lee
2026-10-05 10:40 ` David Hildenbrand (Arm)
2026-10-05 11:12 ` Sarthak Sharma
2026-10-06 8:15 ` [PATCH v5] selftests/mm: hugetlb_madv_vs_map: add underflow test Breno Leitao
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.