From: Cyril Hrubis <chrubis@suse.cz>
To: Wei Gao <wegao@suse.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v2] mmap21.c: Test for new MAP_DROPPABLE flag for mmap
Date: Mon, 10 Mar 2025 17:18:57 +0100 [thread overview]
Message-ID: <Z88Q8TV13_PMKAIT@yuki.lan> (raw)
In-Reply-To: <20250103135626.28672-1-wegao@suse.com>
Hi!
> diff --git a/testcases/kernel/syscalls/mmap/mmap21.c b/testcases/kernel/syscalls/mmap/mmap21.c
> new file mode 100644
> index 000000000..46f3ac7c5
> --- /dev/null
> +++ b/testcases/kernel/syscalls/mmap/mmap21.c
> @@ -0,0 +1,102 @@
> +// 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
> + */
> +
> +#define _GNU_SOURCE
> +#include <errno.h>
> +#include <stdio.h>
> +#include <sys/types.h>
> +#include "tst_test.h"
> +#include "lapi/mmap.h"
> +
> +#define MEM_LIMIT (256 * TST_MB)
> +#define ALLOC_SIZE (128 * TST_MB)
> +
> +static struct tst_cg_group *cg_child;
> +
> +static void test_mmap(void)
> +{
> + size_t alloc_size = ALLOC_SIZE;
> + size_t page_size = getpagesize();
> + void *alloc;
> + pid_t child;
> +
> + cg_child = tst_cg_group_mk(tst_cg, "child");
> + SAFE_CG_PRINTF(tst_cg, "memory.max", "%d", MEM_LIMIT);
> + SAFE_CG_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
> +
> + 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);
> + }
> + }
> +
> + while (!(*shared_var)) {
> + for (size_t i = 0; i < alloc_size; i += page_size) {
> + if (!*(uint8_t *)(alloc + i)) {
> + *shared_var = 1;
Why do we need the shared_var at all? Why can't we just SIGKILL the
child at this point the same way the selftest does it?
> + break;
> + }
> + }
> + }
> +
> + TST_EXP_EQ_LI((*shared_var), 1);
> +
> + SAFE_WAITPID(child, NULL, 0);
> +
> + SAFE_MUNMAP(alloc, alloc_size);
> + SAFE_MUNMAP(shared_var, sizeof(int));
> +}
> +
> +static void setup(void)
> +{
> + void *addr = mmap(0, 1, PROT_READ | PROT_WRITE,
> + MAP_ANONYMOUS | MAP_DROPPABLE, -1, 0);
> + if (addr == MAP_FAILED && errno == EINVAL)
> + tst_brk(TCONF, "MAP_DROPPABLE not support");
> +}
> +
> +static void cleanup(void)
> +{
> + if (cg_child) {
> + SAFE_CG_PRINTF(tst_cg_drain, "cgroup.procs", "%d", getpid());
> + cg_child = tst_cg_group_rm(cg_child);
> + }
> +}
> +
> +static struct tst_test test = {
> + .test_all = test_mmap,
> + .needs_tmpdir = 1,
> + .forks_child = 1,
> + .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
> + .needs_root = 1,
> + .cleanup = cleanup,
> + .setup = setup,
> + .min_mem_avail = 300,
> +};
> --
> 2.35.3
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2025-03-10 16:19 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-28 13:32 [LTP] [PATCH v1] mmap21.c: Test for new MAP_DROPPABLE flag for mmap Wei Gao via ltp
2024-12-31 10:04 ` Li Wang
2025-01-02 9:38 ` Cyril Hrubis
2025-01-03 1:42 ` Wei Gao via ltp
2025-01-03 13:54 ` Wei Gao via ltp
2025-01-03 13:56 ` [LTP] [PATCH v2] " Wei Gao via ltp
2025-03-10 16:18 ` Cyril Hrubis [this message]
2025-04-16 19:16 ` [LTP] [PATCH v3] mmap22.c: " Wei Gao via ltp
2025-04-25 11:40 ` Cyril Hrubis
2025-04-28 16:04 ` [LTP] [PATCH v4] " Wei Gao via ltp
2025-05-07 15:25 ` Cyril Hrubis
2025-05-12 8:43 ` Jan Stancek via ltp
2025-05-13 15:44 ` Wei Gao via ltp
2025-05-14 8:51 ` Jan Stancek via ltp
2025-05-14 9:04 ` Li Wang via ltp
2025-05-14 9:10 ` Jan Stancek via ltp
2025-05-14 9:14 ` Li Wang via ltp
2025-05-14 11:15 ` Jan Stancek via ltp
2025-05-14 12:44 ` Li Wang via ltp
2025-05-14 12:54 ` Jan Stancek via ltp
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z88Q8TV13_PMKAIT@yuki.lan \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
--cc=wegao@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox