[LTP] [PATCH 3/3] mmap24: add test for MAP_32BIT address limit
Petr Vorel
pvorel@suse.cz
Fri Sep 11 15:59:07 CEST 2026
Hi Andrea,
> Add a test verifying that mmap() with the MAP_32BIT flag restricts
> mappings to the first 2GB of address space and fails with ENOMEM
> when the 32-bit address space is exhausted without falling back to
> higher addresses.
Thanks!
...
> +++ b/testcases/kernel/syscalls/mmap/mmap24.c
...
> +
> + if ((unsigned long)addr + CHUNK_SZ > ADDR_LIMIT) {
> + tst_res(TFAIL, "mapping at %p + %zu exceeds 2GB limit",
%zu is wrong on 32 bit.
../../../../include/tst_test.h:75:55: warning: format ‘%zu’ expects argument of type ‘size_t’, but argument 6 has type ‘long unsigned int’ [-Wformat=]
75 | tst_res_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__);\
| ^~~~~~~~~
mmap24.c:63:25: note: in expansion of macro ‘tst_res’
63 | tst_res(TFAIL, "mapping at %p + %zu exceeds 2GB limit",
| ^~~~~~~
> + addr, CHUNK_SZ);
> + addrs[num_chunks++] = addr;
> + cleanup();
> + return;
> + }
> +
> + ((char *)addr)[0] = 'a';
> + ((char *)addr)[CHUNK_SZ - 1] = 'z';
> +
> + addrs[num_chunks++] = addr;
> + }
> +
> + if (!failed_with_enomem) {
> + tst_res(TFAIL, "32-bit address space was not exhausted");
> + } else if (!num_chunks) {
> + tst_res(TFAIL, "failed to map any chunk with MAP_32BIT");
> + } else {
> + tst_res(TPASS,
> + "Mapped %zu MB across %zu chunks within 2GB before ENOMEM",
And here as well.
> + (num_chunks * CHUNK_SZ) / TST_MB, num_chunks);
> + }
> +
> + cleanup();
> +}
> +
> +static struct tst_test test = {
> + .cleanup = cleanup,
> + .test_all = run,
> + .supported_archs = (const char *const []){
> + "x86_64",
> + NULL
> + },
> +};
BTW I was wondering if I can get this failing when running on VM with really
small RAM, but even with 249 MB I get get mapped 928 MB (or 960 MB when I run
with -i):
mmap24.c:81: TPASS: Mapped 928 MB across 29 chunks within 2GB before ENOMEM
What am I missing?
Anyway, LGTM.
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Kind regards,
Petr
Before merge please:
diff --git testcases/kernel/syscalls/mmap/mmap24.c testcases/kernel/syscalls/mmap/mmap24.c
index 50de620f42..94d092b809 100644
--- testcases/kernel/syscalls/mmap/mmap24.c
+++ testcases/kernel/syscalls/mmap/mmap24.c
@@ -60,7 +60,7 @@ static void run(void)
}
if ((unsigned long)addr + CHUNK_SZ > ADDR_LIMIT) {
- tst_res(TFAIL, "mapping at %p + %zu exceeds 2GB limit",
+ tst_res(TFAIL, "mapping at %p + %lu exceeds 2GB limit",
addr, CHUNK_SZ);
addrs[num_chunks++] = addr;
cleanup();
@@ -79,7 +79,7 @@ static void run(void)
tst_res(TFAIL, "failed to map any chunk with MAP_32BIT");
} else {
tst_res(TPASS,
- "Mapped %zu MB across %zu chunks within 2GB before ENOMEM",
+ "Mapped %lu MB across %zu chunks within 2GB before ENOMEM",
(num_chunks * CHUNK_SZ) / TST_MB, num_chunks);
}
More information about the ltp
mailing list