* [PATCH] selftests/mm: hugetlb_madv_vs_map: add underflow test
@ 2026-08-27 18:00 Guillaume Morin
2026-08-28 11:38 ` Breno Leitao
0 siblings, 1 reply; 2+ messages in thread
From: Guillaume Morin @ 2026-08-27 18:00 UTC (permalink / raw)
To: linux-mm; +Cc: leitao, rppt
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
Cc: Breno Leitao <leitao@debian.org>
Cc: Mike Rapoport <rppt@kernel.org>
---
.../testing/selftests/mm/hugepage_settings.c | 9 ++
.../testing/selftests/mm/hugepage_settings.h | 1 +
.../selftests/mm/hugetlb_madv_vs_map.c | 84 ++++++++++++++++++-
3 files changed, 93 insertions(+), 1 deletion(-)
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..6aa42f064279 100644
--- a/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
+++ b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
@@ -15,13 +15,20 @@
*
* Touching the first page after thread3's allocation will raise a SIGBUS
*
+ * We setup a 2nd test where 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.
+ *
* Author: Breno Leitao <leitao@debian.org>
*/
+#include <limits.h>
#include <pthread.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
+#include <sys/wait.h>
#include <unistd.h>
#include "vm_util.h"
@@ -86,7 +93,7 @@ int main(void)
int max = 10;
ksft_print_header();
- ksft_set_plan(1);
+ 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",
@@ -120,5 +127,80 @@ int main(void)
}
ksft_test_result_pass("No unexpected huge page allocations\n");
+ 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_msg("Failed to allocate huge page\n");
+
+ {
+ pid_t pid;
+ int pipe_fds[2];
+ unsigned long nr_reserved = 0;
+
+ 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_msg("pipe failed");
+
+ pid = fork();
+ if (pid < 0)
+ ksft_exit_fail_msg("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_exit_fail_msg("child read failed");
+ return 0;
+ }
+
+ /* Parent */
+ close(pipe_fds[0]);
+
+ /* First unmap, this will close the vma */
+ if (munmap(huge_ptr, mmap_size) != 0) {
+ kill(pid, SIGKILL);
+ ksft_exit_fail_msg("munmap failed");
+ }
+
+ nr_reserved = hugetlb_nr_resv_pages(default_huge_page_size());
+ if (nr_reserved == ULONG_MAX) {
+ ksft_test_result_fail("After the munmap, HugePages_Rsvd underflowed!\n");
+ } else if (nr_reserved == 0) {
+ ksft_test_result_pass("Underflow not present!\n");
+ } else {
+ ksft_exit_fail_msg("Unexpected HugePages_Rsvd=%ld after munmap, should be 0 or -1. Repeat the test\n",
+ nr_reserved);
+ }
+ /* Make the child exit, this should restore HugePages_Rsvd to 0 */
+ if (write(pipe_fds[1], &nr_reserved, 1) < 0) {
+ kill(pid, SIGKILL);
+ ksft_exit_fail_msg("write failed");
+ }
+ close(pipe_fds[1]);
+ if (waitpid(pid, NULL, 0) <= 0)
+ ksft_exit_fail_msg("write failed");
+
+ 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 or -1. Repeat the test\n",
+ nr_reserved);
+ }
+ }
ksft_finished();
}
--
2.39.1
--
Guillaume Morin <guillaume@morinfr.org>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] selftests/mm: hugetlb_madv_vs_map: add underflow test
2026-08-27 18:00 [PATCH] selftests/mm: hugetlb_madv_vs_map: add underflow test Guillaume Morin
@ 2026-08-28 11:38 ` Breno Leitao
0 siblings, 0 replies; 2+ messages in thread
From: Breno Leitao @ 2026-08-28 11:38 UTC (permalink / raw)
To: Guillaume Morin; +Cc: linux-mm, rppt
On Thu, Aug 27, 2026 at 08:00:49PM +0200, Guillaume Morin wrote:
> @@ -120,5 +127,80 @@ int main(void)
> }
>
> ksft_test_result_pass("No unexpected huge page allocations\n");
> + 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_msg("Failed to allocate huge page\n");
> +
> + {
> + pid_t pid;
> + int pipe_fds[2];
> + unsigned long nr_reserved = 0;
Why do you want to start a C block "{" here? That looks like a bit
weird.
Also, please factor our this new code.
Thanks for the new test,
--breno
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-08-28 11:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-27 18:00 [PATCH] selftests/mm: hugetlb_madv_vs_map: add underflow test Guillaume Morin
2026-08-28 11:38 ` Breno Leitao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox