* [LTP] [PATCH] [PATCH] Add hugemmap37, migrated task-size-overrun.c from libhugetlbfs v2
@ 2025-09-25 6:13 Pavithra
2026-03-03 15:10 ` Cyril Hrubis
0 siblings, 1 reply; 2+ messages in thread
From: Pavithra @ 2025-09-25 6:13 UTC (permalink / raw)
To: ltp; +Cc: pavrampu, Pavithra
From: Pavithra <pavrampu@linux.vnet.ibm.com>
This test is migrated from :
https://github.com/libhugetlbfs/libhugetlbfs/blob/master/tests/task-size-overrun.c
This test verifies the behavior of mmap across the TASK_SIZE boundary.
It checks whether mmap with and without MAP_FIXED correctly handles
mappings that straddle the TASK_SIZE boundary.
Verified that test runs with multiple iterations
Signed-off-by: Pavithra <pavrampu@linux.vnet.ibm.com>
---
runtest/hugetlb | 1 +
testcases/kernel/mem/.gitignore | 1 +
.../kernel/mem/hugetlb/hugemmap/hugemmap37.c | 141 ++++++++++++++++++
3 files changed, 143 insertions(+)
create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c
diff --git a/runtest/hugetlb b/runtest/hugetlb
index 0896d3c94..8aaafeee3 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -36,6 +36,7 @@ hugemmap30 hugemmap30
hugemmap31 hugemmap31
hugemmap32 hugemmap32
hugemmap34 hugemmap34
+hugemmap37 hugemmap37
hugemmap05_1 hugemmap05 -m
hugemmap05_2 hugemmap05 -s
hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index b4455de51..38d428fe8 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -36,6 +36,7 @@
/hugetlb/hugemmap/hugemmap31
/hugetlb/hugemmap/hugemmap32
/hugetlb/hugemmap/hugemmap34
+/hugetlb/hugemmap/hugemmap37
/hugetlb/hugeshmat/hugeshmat01
/hugetlb/hugeshmat/hugeshmat02
/hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c
new file mode 100644
index 000000000..3fd6631d4
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
+ */
+
+/*\
+ *[Descripiton]
+ *
+ * Origin: https://github.com/libhugetlbfs/libhugetlbfs/blob/master/tests/task-size-overrun.c
+ *
+ * This test verifies the behavior of mmap across the TASK_SIZE boundary.
+ * It checks whether mmap with and without MAP_FIXED correctly handles
+ * mappings that straddle the TASK_SIZE boundary.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "hugetlb.h"
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
+#include "tst_safe_macros.h"
+
+#define MAPS_BUF_SZ 4096
+#define _LARGEFILE64_SOURCE
+#define MNTPOINT "hugetlbfs/"
+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
+
+static long hpage_size;
+static int fd;
+
+static unsigned long find_last_mapped(void)
+{
+ char line[MAPS_BUF_SZ];
+ char *tmp;
+ unsigned long start, end, off, ino;
+ FILE *f;
+
+ f = SAFE_FOPEN("/proc/self/maps", "r");
+ do {
+ tmp = fgets(line, MAPS_BUF_SZ, f);
+ } while (tmp);
+ SAFE_FCLOSE(f);
+
+ tst_res(TINFO, "Last map: %s", line);
+ SAFE_SSCANF(line, "%lx-%lx %*s %lx %*s %ld %*s", &start, &end, &off, &ino);
+ tst_res(TINFO, "Last map: at 0x%lx-0x%lx\n", start, end);
+ return end;
+}
+
+static unsigned long find_task_size(void)
+{
+ unsigned long low, high; /* PFNs */
+ void *p;
+
+ low = find_last_mapped();
+ if (!low || ((low % getpagesize()) != 0))
+ tst_brk(TBROK, "Bogus stack end address, 0x%lx!?", low);
+ low = low / getpagesize();
+
+ /* This sum should get us (2^(wordsize) - 2 pages) */
+ high = (unsigned long)(-2 * getpagesize()) / tst_get_hugepage_size();
+ tst_res(TINFO, "Binary searching for task size PFNs 0x%lx..0x%lx\n", low, high);
+
+ while (high > low + 1) {
+ unsigned long pfn = (low + high) / 2;
+ unsigned long addr = pfn * getpagesize();
+
+ assert((pfn >= low) && (pfn <= high));
+
+ p = mmap((void *)addr, getpagesize(), PROT_READ,
+ MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
+ if (p == MAP_FAILED) {
+ tst_res(TINFO | TERRNO, "Map failed at 0x%lx", addr);
+ high = pfn;
+ } else {
+ tst_res(TINFO, "Map succeeded at 0x%lx\n", addr);
+ SAFE_MUNMAP(p, getpagesize());
+ low = pfn;
+ }
+ }
+
+ return low * getpagesize();
+}
+
+static void run_test(void)
+{
+ void *p;
+ unsigned long task_size;
+ unsigned long straddle_addr;
+
+ task_size = find_task_size();
+ tst_res(TINFO, "TASK_SIZE = 0x%lx\n", task_size);
+
+ straddle_addr = task_size - hpage_size;
+ straddle_addr = ALIGN(straddle_addr, hpage_size);
+
+ tst_res(TINFO, "Mapping without MAP_FIXED at %lx...", straddle_addr);
+ errno = 0;
+ p = mmap((void *)straddle_addr, 2*hpage_size, PROT_READ|PROT_WRITE,
+ MAP_SHARED, fd, 0);
+ if (p == (void *)straddle_addr)
+ tst_res(TFAIL, "Apparently suceeded in mapping across TASK_SIZE boundary");
+
+ tst_res(TINFO, "Mapping with MAP_FIXED at %lx...", straddle_addr);
+ errno = 0;
+ p = mmap((void *)straddle_addr, 2*hpage_size, PROT_READ|PROT_WRITE,
+ MAP_SHARED|MAP_FIXED, fd, 0);
+ if (p != MAP_FAILED)
+ tst_res(TFAIL, "Apparently suceeded in mapping across TASK_SIZE boundary");
+
+ tst_res(TPASS, "Test passed!");
+}
+
+static void setup(void)
+{
+ hpage_size = getpagesize();
+ fd = tst_creat_unlinked(MNTPOINT, 0, 0600);
+}
+
+static void cleanup(void)
+{
+ if (fd > 0)
+ SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+ .needs_root = 1,
+ .mntpoint = MNTPOINT,
+ .needs_hugetlbfs = 1,
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run_test,
+ .hugepages = {3, TST_NEEDS},
+};
--
2.43.5
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [LTP] [PATCH] [PATCH] Add hugemmap37, migrated task-size-overrun.c from libhugetlbfs v2
2025-09-25 6:13 [LTP] [PATCH] [PATCH] Add hugemmap37, migrated task-size-overrun.c from libhugetlbfs v2 Pavithra
@ 2026-03-03 15:10 ` Cyril Hrubis
0 siblings, 0 replies; 2+ messages in thread
From: Cyril Hrubis @ 2026-03-03 15:10 UTC (permalink / raw)
To: Pavithra; +Cc: ltp, Pavithra
Hi!
> +/*\
> + *[Descripiton]
> + *
> + * Origin: https://github.com/libhugetlbfs/libhugetlbfs/blob/master/tests/task-size-overrun.c
> + *
> + * This test verifies the behavior of mmap across the TASK_SIZE boundary.
> + * It checks whether mmap with and without MAP_FIXED correctly handles
> + * mappings that straddle the TASK_SIZE boundary.
> + *
> + */
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <sys/mman.h>
> +#include <errno.h>
> +#include <assert.h>
> +
> +#include "hugetlb.h"
> +#include "tst_test.h"
> +#include "tst_safe_stdio.h"
> +#include "tst_safe_macros.h"
> +
> +#define MAPS_BUF_SZ 4096
> +#define _LARGEFILE64_SOURCE
> +#define MNTPOINT "hugetlbfs/"
> +#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
We have LTP_ALIGN() macro in the test library.
> +static long hpage_size;
> +static int fd;
> +
> +static unsigned long find_last_mapped(void)
> +{
> + char line[MAPS_BUF_SZ];
> + char *tmp;
> + unsigned long start, end, off, ino;
> + FILE *f;
> +
> + f = SAFE_FOPEN("/proc/self/maps", "r");
> + do {
> + tmp = fgets(line, MAPS_BUF_SZ, f);
> + } while (tmp);
> + SAFE_FCLOSE(f);
> +
> + tst_res(TINFO, "Last map: %s", line);
> + SAFE_SSCANF(line, "%lx-%lx %*s %lx %*s %ld %*s", &start, &end, &off, &ino);
> + tst_res(TINFO, "Last map: at 0x%lx-0x%lx\n", start, end);
> + return end;
> +}
> +
> +static unsigned long find_task_size(void)
> +{
> + unsigned long low, high; /* PFNs */
> + void *p;
> +
> + low = find_last_mapped();
> + if (!low || ((low % getpagesize()) != 0))
> + tst_brk(TBROK, "Bogus stack end address, 0x%lx!?", low);
The last mapping in /proc/self/maps on my systems is [vsyscall] which is
mapped at the end of the address space. I guess that we need to take
last line from the file that is not [vsyscall] (and perhaps there are
other special cases on some architectures but that can be fixed later).
> + low = low / getpagesize();
> +
> + /* This sum should get us (2^(wordsize) - 2 pages) */
> + high = (unsigned long)(-2 * getpagesize()) / tst_get_hugepage_size();
I do not get this at all. We are trying to figure out TASK_SIZE how
exactly do we figure out the upper bound from hugepage size?
> + tst_res(TINFO, "Binary searching for task size PFNs 0x%lx..0x%lx\n", low, high);
> +
> + while (high > low + 1) {
> + unsigned long pfn = (low + high) / 2;
> + unsigned long addr = pfn * getpagesize();
> +
> + assert((pfn >= low) && (pfn <= high));
As long as I can tell this assert is never triggered.
> + p = mmap((void *)addr, getpagesize(), PROT_READ,
> + MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
> + if (p == MAP_FAILED) {
> + tst_res(TINFO | TERRNO, "Map failed at 0x%lx", addr);
> + high = pfn;
> + } else {
> + tst_res(TINFO, "Map succeeded at 0x%lx\n", addr);
> + SAFE_MUNMAP(p, getpagesize());
> + low = pfn;
> + }
> + }
> +
> + return low * getpagesize();
> +}
> +
> +static void run_test(void)
> +{
> + void *p;
> + unsigned long task_size;
> + unsigned long straddle_addr;
> +
> + task_size = find_task_size();
> + tst_res(TINFO, "TASK_SIZE = 0x%lx\n", task_size);
> +
> + straddle_addr = task_size - hpage_size;
> + straddle_addr = ALIGN(straddle_addr, hpage_size);
> +
> + tst_res(TINFO, "Mapping without MAP_FIXED at %lx...", straddle_addr);
> + errno = 0;
> + p = mmap((void *)straddle_addr, 2*hpage_size, PROT_READ|PROT_WRITE,
> + MAP_SHARED, fd, 0);
> + if (p == (void *)straddle_addr)
> + tst_res(TFAIL, "Apparently suceeded in mapping across TASK_SIZE boundary");
> +
> + tst_res(TINFO, "Mapping with MAP_FIXED at %lx...", straddle_addr);
> + errno = 0;
> + p = mmap((void *)straddle_addr, 2*hpage_size, PROT_READ|PROT_WRITE,
> + MAP_SHARED|MAP_FIXED, fd, 0);
> + if (p != MAP_FAILED)
> + tst_res(TFAIL, "Apparently suceeded in mapping across TASK_SIZE boundary");
> +
> + tst_res(TPASS, "Test passed!");
> +}
> +
> +static void setup(void)
> +{
> + hpage_size = getpagesize();
> + fd = tst_creat_unlinked(MNTPOINT, 0, 0600);
> +}
> +
> +static void cleanup(void)
> +{
> + if (fd > 0)
> + SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> + .needs_root = 1,
> + .mntpoint = MNTPOINT,
> + .needs_hugetlbfs = 1,
> + .setup = setup,
> + .cleanup = cleanup,
> + .test_all = run_test,
> + .hugepages = {3, TST_NEEDS},
> +};
> --
> 2.43.5
>
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-03-03 15:11 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-25 6:13 [LTP] [PATCH] [PATCH] Add hugemmap37, migrated task-size-overrun.c from libhugetlbfs v2 Pavithra
2026-03-03 15:10 ` Cyril Hrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox