Add two tests for an arena map's mmap()ed region: arena_split checks that a partial munmap() that would split the mapping fails with -EINVAL, and arena_fork checks that a forked child does not inherit the mapping (mincore() reports the range as unmapped). Signed-off-by: Ruslan Valiyev --- v2: - Drop the regression-test framing and per-callback details; use short one-line comments. - Split into two separate tests, arena_split (partial unmap) and arena_fork. - Unpack the mincore() child check into explicit statements. .../selftests/bpf/prog_tests/arena_fork.c | 53 +++++++++++++++++++ .../selftests/bpf/prog_tests/arena_split.c | 34 ++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_fork.c create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_split.c diff --git a/tools/testing/selftests/bpf/prog_tests/arena_fork.c b/tools/testing/selftests/bpf/prog_tests/arena_fork.c new file mode 100644 index 0000000000000..bb134e9adb607 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/arena_fork.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include + +/* Make sure an arena mapping is not inherited across fork(). */ + +#define NR_PAGES 3 + +void test_arena_fork(void) +{ + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE); + long ps = sysconf(_SC_PAGESIZE); + size_t sz = (size_t)NR_PAGES * ps; + void *area; + int fd, ret, status; + pid_t pid; + + fd = bpf_map_create(BPF_MAP_TYPE_ARENA, "arena_fork", 0, 0, NR_PAGES, &opts); + if (!ASSERT_OK_FD(fd, "arena map create")) + return; + + area = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (!ASSERT_NEQ(area, MAP_FAILED, "mmap arena")) + goto close_fd; + + pid = fork(); + if (pid == 0) { + unsigned char vec; + int rc; + + /* If the mapping was not inherited the range is unmapped in + * the child, so mincore() fails with ENOMEM. A success means + * the child wrongly inherited the mapping. + */ + rc = mincore(area, ps, &vec); + if (rc == 0) + _exit(1); + _exit(errno == ENOMEM ? 0 : 2); + } + if (ASSERT_GE(pid, 0, "fork")) { + while ((ret = waitpid(pid, &status, 0)) < 0 && errno == EINTR) + ; + if (ASSERT_EQ(ret, pid, "waitpid")) + ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0, + "child must not inherit arena mapping"); + } + + munmap(area, sz); +close_fd: + close(fd); +} diff --git a/tools/testing/selftests/bpf/prog_tests/arena_split.c b/tools/testing/selftests/bpf/prog_tests/arena_split.c new file mode 100644 index 0000000000000..8a59cfdb2ef89 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/arena_split.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +/* Make sure an arena mapping cannot be split by a partial munmap(). */ + +#define NR_PAGES 3 + +void test_arena_split(void) +{ + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE); + long ps = sysconf(_SC_PAGESIZE); + size_t sz = (size_t)NR_PAGES * ps; + void *area; + int fd, ret, err; + + fd = bpf_map_create(BPF_MAP_TYPE_ARENA, "arena_split", 0, 0, NR_PAGES, &opts); + if (!ASSERT_OK_FD(fd, "arena map create")) + return; + + area = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (!ASSERT_NEQ(area, MAP_FAILED, "mmap arena")) + goto close_fd; + + ret = munmap((char *)area + ps, ps); + err = errno; + if (ASSERT_ERR(ret, "split munmap")) + ASSERT_EQ(err, EINVAL, "split munmap errno"); + + munmap(area, sz); +close_fd: + close(fd); +} base-commit: 174914ea551314c52a61713b9c4bde9e42d48073 -- 2.43.0