[LTP] [PATCH v1] mmap21.c: Test for new MAP_DROPPABLE flag for mmap
Li Wang
liwang@redhat.com
Tue Dec 31 11:04:54 CET 2024
On Sat, Dec 28, 2024 at 9:32 PM Wei Gao via ltp <ltp@lists.linux.it> wrote:
> Signed-off-by: Wei Gao <wegao@suse.com>
> ---
> include/lapi/mmap.h | 4 ++
> runtest/syscalls | 1 +
> testcases/kernel/syscalls/mmap/.gitignore | 1 +
> testcases/kernel/syscalls/mmap/mmap21.c | 73 +++++++++++++++++++++++
> 4 files changed, 79 insertions(+)
> create mode 100644 testcases/kernel/syscalls/mmap/mmap21.c
>
> diff --git a/include/lapi/mmap.h b/include/lapi/mmap.h
> index ea9730586..248b64564 100644
> --- a/include/lapi/mmap.h
> +++ b/include/lapi/mmap.h
> @@ -87,6 +87,10 @@
> # define MADV_PAGEOUT 21
> #endif
>
> +#ifndef MAP_DROPPABLE
> +# define MAP_DROPPABLE 0x08
> +#endif
> +
> #ifndef MAP_FIXED_NOREPLACE
>
> #ifdef __alpha__
> diff --git a/runtest/syscalls b/runtest/syscalls
> index ded035ee8..7166e39a4 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -840,6 +840,7 @@ mmap17 mmap17
> mmap18 mmap18
> mmap19 mmap19
> mmap20 mmap20
> +mmap21 mmap21
>
> modify_ldt01 modify_ldt01
> modify_ldt02 modify_ldt02
> diff --git a/testcases/kernel/syscalls/mmap/.gitignore
> b/testcases/kernel/syscalls/mmap/.gitignore
> index 4591fdbb9..87b23aaee 100644
> --- a/testcases/kernel/syscalls/mmap/.gitignore
> +++ b/testcases/kernel/syscalls/mmap/.gitignore
> @@ -18,3 +18,4 @@
> /mmap18
> /mmap19
> /mmap20
> +/mmap21
> diff --git a/testcases/kernel/syscalls/mmap/mmap21.c
> b/testcases/kernel/syscalls/mmap/mmap21.c
> new file mode 100644
> index 000000000..e2b8c4551
> --- /dev/null
> +++ b/testcases/kernel/syscalls/mmap/mmap21.c
> @@ -0,0 +1,73 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2024 Wei Gao <wegao@suse.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Test mmap(2) with MAP_DROPPABLE flag.
> + *
> + * Test base on kernel selftests/mm/droppable.c
> + */
> +
> +#include <errno.h>
> +#include <stdio.h>
> +#include <sys/types.h>
> +#include "tst_test.h"
> +#include "lapi/mmap.h"
> +
> +static void test_mmap(void)
> +{
> + size_t alloc_size = 134217728;
>
Maybe define MB as 1024 * 1024 then,
size_t alloc_size = 128*MB;
> + size_t page_size = getpagesize();
> + void *alloc;
> + pid_t child;
> +
> + alloc = SAFE_MMAP(0, alloc_size, PROT_READ | PROT_WRITE,
> + MAP_ANONYMOUS | MAP_DROPPABLE, -1, 0);
> +
> + memset(alloc, 'A', alloc_size);
> + for (size_t i = 0; i < alloc_size; i += page_size) {
> + if (*(char *)(alloc + i) != 'A')
> + tst_res(TFAIL, "memset failed");
> + }
> +
> + int *shared_var = SAFE_MMAP(NULL, sizeof(int), PROT_READ |
> PROT_WRITE,
> + MAP_SHARED | MAP_ANONYMOUS, -1, 0);
> +
> + *shared_var = 0;
> +
> + child = SAFE_FORK();
> + if (!child) {
> + for (;;) {
> + *(char *)malloc(page_size) = 'B';
> + if ((*shared_var) == 1)
> + exit(0);
>
If the parent process crashes, hangs, or fails to detect reclaimed
pages, the child process will run indefinitely, potentially consuming
system resources.
If it runs too long, add a timeout mechanism to terminate the child process.
> + }
> + }
> +
> + for (; !(*shared_var);) {
>
why not use while() loop.
+ for (size_t i = 0; i < alloc_size; i += page_size) {
> + if (!*(uint8_t *)(alloc + i)) {
> + *shared_var = 1;
> + break;
> + }
> + }
> + }
> +
> + TST_EXP_EQ_LI((*shared_var), 1);
>
The test assumes that reclaimed MAP_DROPPABLE pages will be
zeroed out when accessed. While this behavior is plausible, it depends
on how MAP_DROPPABLE is implemented in the kernel.
Do you have any clue (documents or code) that indicates this is true?
> +
> + SAFE_WAITPID(child, NULL, 0);
> +
> + SAFE_MUNMAP(alloc, alloc_size);
> + SAFE_MUNMAP(shared_var, sizeof(int));
> +}
> +
> +static struct tst_test test = {
> + .min_kver = "6.11",
>
I would suggest removing this line because some distros might backport the
syscall to their old enterprise version.
Then before running this test, the presence of MAP_DROPPABLE in the
kernel should be verified, or the test should gracefully skip if the
feature is
not supported.
> + .test_all = test_mmap,
> + .needs_tmpdir = 1,
>
add .min_mem_avail here.
> + .forks_child = 1,
> + .max_runtime = 180,
>
This max_runtime is useless if we run the test on a large RAM system
which likely takes too long to simulate the memory pressure.
We could limit the test to an CGroup and set the memory.max
to 256MB, which can complete the mem hog quickly to finish.
> +};
> --
> 2.35.3
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>
>
--
Regards,
Li Wang
More information about the ltp
mailing list