From: Suren Baghdasaryan <surenb@google.com>
To: akpm@linux-foundation.org
Cc: mhocko@suse.com, rientjes@google.com, willy@infradead.org,
hannes@cmpxchg.org, guro@fb.com, minchan@kernel.org,
kirill@shutemov.name, aarcange@redhat.com, brauner@kernel.org,
hch@infradead.org, oleg@redhat.com, david@redhat.com,
jannh@google.com, shakeelb@google.com, peterx@redhat.com,
jhubbard@nvidia.com, shuah@kernel.org,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-kselftest@vger.kernel.org, kernel-team@android.com,
surenb@google.com
Subject: [PATCH 1/3] selftests: vm: add process_mrelease tests
Date: Mon, 9 May 2022 20:00:12 -0700 [thread overview]
Message-ID: <20220510030014.3842475-1-surenb@google.com> (raw)
Introduce process_mrelease syscall sanity tests. They include tests of
invalid pidfd and flags inputs, attempting to call process_mrelease
with a live process and a valid usage of process_mrelease. Because
process_mrelease has to be used against a process with a pending SIGKILL,
it's possible that the process exits before process_mrelease gets called.
In such cases we retry the test with a victim that allocates twice more
memory up to 1GB. This would require the victim process to spend more
time during exit and process_mrelease has a better chance of catching
the process before it exits.
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
---
tools/testing/selftests/vm/Makefile | 1 +
tools/testing/selftests/vm/mrelease_test.c | 176 +++++++++++++++++++++
tools/testing/selftests/vm/run_vmtests.sh | 16 ++
3 files changed, 193 insertions(+)
create mode 100644 tools/testing/selftests/vm/mrelease_test.c
diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
index 04a49e876a46..733fccbff0ef 100644
--- a/tools/testing/selftests/vm/Makefile
+++ b/tools/testing/selftests/vm/Makefile
@@ -43,6 +43,7 @@ TEST_GEN_FILES += map_populate
TEST_GEN_FILES += memfd_secret
TEST_GEN_FILES += mlock-random-test
TEST_GEN_FILES += mlock2-tests
+TEST_GEN_FILES += mrelease_test
TEST_GEN_FILES += mremap_dontunmap
TEST_GEN_FILES += mremap_test
TEST_GEN_FILES += on-fault-limit
diff --git a/tools/testing/selftests/vm/mrelease_test.c b/tools/testing/selftests/vm/mrelease_test.c
new file mode 100644
index 000000000000..a61061bf8433
--- /dev/null
+++ b/tools/testing/selftests/vm/mrelease_test.c
@@ -0,0 +1,176 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2022 Google LLC
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "util.h"
+
+static inline int pidfd_open(pid_t pid, unsigned int flags)
+{
+#ifdef __NR_pidfd_open
+ return syscall(__NR_pidfd_open, pid, flags);
+#else
+ errno = ENOSYS;
+ return -1;
+#endif
+}
+
+static inline int process_mrelease(int pidfd, unsigned int flags)
+{
+#ifdef __NR_process_mrelease
+ return syscall(__NR_process_mrelease, pidfd, flags);
+#else
+ errno = ENOSYS;
+ return -1;
+#endif
+}
+
+static void write_fault_pages(char *addr, unsigned long nr_pages)
+{
+ unsigned long i;
+
+ for (i = 0; i < nr_pages; i++)
+ *((unsigned long *)(addr + (i * PAGE_SIZE))) = i;
+}
+
+static int alloc_noexit(unsigned long nr_pages, int pipefd)
+{
+ int ppid = getppid();
+ void *buf;
+
+ buf = mmap(NULL, nr_pages * PAGE_SIZE, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANON, 0, 0);
+ if (buf == MAP_FAILED) {
+ perror("mmap");
+ return 1;
+ }
+
+ write_fault_pages((char *)buf, nr_pages);
+
+ /* Signal the parent that the child is ready */
+ if (write(pipefd, "", 1) < 0) {
+ perror("write");
+ return 1;
+ }
+
+ /* Wait to be killed (when reparenting happens) */
+ while (getppid() == ppid)
+ sleep(1);
+
+ munmap(buf, nr_pages * PAGE_SIZE);
+
+ return 0;
+}
+
+
+#define MB(x) (x << 20)
+#define MAX_SIZE_MB 1024
+
+int main(void)
+{
+ int res;
+ int pipefd[2], pidfd;
+ pid_t pid;
+ char byte;
+ size_t size;
+ int negative_tests_done = 0;
+
+ /* Test a wrong pidfd */
+ if (!process_mrelease(-1, 0) || errno != EBADF) {
+ perror("process_mrelease with wring pidfd");
+ exit(1);
+ }
+
+ /*
+ * Start the test with 1MB allocation and double every time
+ * process_mrelease fails
+ */
+ for (size = 1; size <= MAX_SIZE_MB; size *= 2) {
+ /*
+ * Pipe for the child to signal when it's done allocating
+ * memory
+ */
+ if (pipe(pipefd)) {
+ perror("pipe");
+ exit(1);
+ }
+ pid = fork();
+ if (pid < 0) {
+ perror("fork");
+ exit(1);
+ }
+
+ if (pid == 0) {
+ close(pipefd[0]);
+ res = alloc_noexit(MB(size) / PAGE_SIZE, pipefd[1]);
+ close(pipefd[1]);
+ exit(res);
+ }
+
+ close(pipefd[1]);
+ /* Block until the child is ready */
+ res = read(pipefd[0], &byte, 1);
+ close(pipefd[0]);
+ if (res < 0) {
+ perror("read");
+ exit(1);
+ }
+
+ pidfd = pidfd_open(pid, 0);
+ if (pidfd < 0) {
+ perror("pidfd_open");
+ exit(1);
+ }
+
+ /* Run negative tests which require a valid child only once */
+ if (!negative_tests_done) {
+ /* Test invalid flags */
+ if (!process_mrelease(pidfd, (unsigned int)-1) ||
+ errno != EINVAL) {
+ perror("process_mrelease with wrong flags");
+ exit(1);
+ }
+ /* Test reapling while process is still alive */
+ if (!process_mrelease(pidfd, 0) ||
+ errno != EINVAL) {
+ perror("process_mrelease on a live process");
+ exit(1);
+ }
+ negative_tests_done = 1;
+ }
+
+ if (kill(pid, SIGKILL)) {
+ perror("kill");
+ exit(1);
+ }
+
+ if (!process_mrelease(pidfd, 0)) {
+ /* Terminate the test once process_mrelease succeeds */
+ return 0;
+ }
+
+ /*
+ * Ignore the failure if the child exited before mrelease got
+ * called, increase allocation size and retry the test
+ */
+ if (errno != ESRCH) {
+ perror("process_mrelease");
+ exit(1);
+ }
+
+ if (waitpid(pid, NULL, 0) < 0) {
+ perror("waitpid");
+ exit(1);
+ }
+ close(pidfd);
+ }
+
+ printf("All process_mrelease attempts failed!\n");
+ exit(1);
+}
diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh
index 352ba00cf26b..1986162fea39 100755
--- a/tools/testing/selftests/vm/run_vmtests.sh
+++ b/tools/testing/selftests/vm/run_vmtests.sh
@@ -287,6 +287,22 @@ else
echo "[PASS]"
fi
+echo "---------------------"
+echo "running mrelease_test"
+echo "---------------------"
+./mrelease_test
+ret_val=$?
+
+if [ $ret_val -eq 0 ]; then
+ echo "[PASS]"
+elif [ $ret_val -eq $ksft_skip ]; then
+ echo "[SKIP]"
+ exitcode=$ksft_skip
+else
+ echo "[FAIL]"
+ exitcode=1
+fi
+
echo "-------------------"
echo "running mremap_test"
echo "-------------------"
--
2.36.0.512.ge40c2bad7a-goog
next reply other threads:[~2022-05-10 3:00 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-10 3:00 Suren Baghdasaryan [this message]
2022-05-10 3:00 ` [PATCH 2/3] mm: drop oom code from exit_mmap Suren Baghdasaryan
2022-05-10 13:05 ` Michal Hocko
2022-05-10 16:31 ` Suren Baghdasaryan
2022-05-10 20:53 ` Michal Hocko
2022-05-10 20:59 ` Suren Baghdasaryan
2022-05-10 15:46 ` Shuah Khan
2022-05-10 16:35 ` Suren Baghdasaryan
2022-05-10 3:00 ` [PATCH 3/3] mm: delete unused MMF_OOM_VICTIM flag Suren Baghdasaryan
2022-05-10 13:08 ` Michal Hocko
2022-05-16 2:46 ` Suren Baghdasaryan
2022-05-10 15:51 ` Shuah Khan
2022-05-10 16:10 ` Suren Baghdasaryan
2022-05-10 15:43 ` [PATCH 1/3] selftests: vm: add process_mrelease tests Shuah Khan
2022-05-10 16:29 ` Suren Baghdasaryan
2022-05-10 16:35 ` Shuah Khan
2022-05-10 16:42 ` Suren Baghdasaryan
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=20220510030014.3842475-1-surenb@google.com \
--to=surenb@google.com \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=david@redhat.com \
--cc=guro@fb.com \
--cc=hannes@cmpxchg.org \
--cc=hch@infradead.org \
--cc=jannh@google.com \
--cc=jhubbard@nvidia.com \
--cc=kernel-team@android.com \
--cc=kirill@shutemov.name \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=minchan@kernel.org \
--cc=oleg@redhat.com \
--cc=peterx@redhat.com \
--cc=rientjes@google.com \
--cc=shakeelb@google.com \
--cc=shuah@kernel.org \
--cc=willy@infradead.org \
/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