1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2022 Google LLC
15 #include "../kselftest.h"
17 #ifndef __NR_pidfd_open
18 #define __NR_pidfd_open -1
21 #ifndef __NR_process_mrelease
22 #define __NR_process_mrelease -1
25 #define MB(x) (x << 20)
26 #define MAX_SIZE_MB 1024
28 static int alloc_noexit(unsigned long nr_pages, int pipefd)
31 int timeout = 10; /* 10sec timeout to get killed */
35 buf = (char *)mmap(NULL, nr_pages * PAGE_SIZE, PROT_READ | PROT_WRITE,
36 MAP_PRIVATE | MAP_ANON, 0, 0);
37 if (buf == MAP_FAILED) {
38 perror("mmap failed, halting the test");
42 for (i = 0; i < nr_pages; i++)
43 *((unsigned long *)(buf + (i * PAGE_SIZE))) = i;
45 /* Signal the parent that the child is ready */
46 if (write(pipefd, "", 1) < 0) {
51 /* Wait to be killed (when reparenting happens) */
52 while (getppid() == ppid && timeout > 0) {
57 munmap(buf, nr_pages * PAGE_SIZE);
59 return (timeout > 0) ? KSFT_PASS : KSFT_FAIL;
62 /* The process_mrelease calls in this test are expected to fail */
63 static void run_negative_tests(int pidfd)
66 /* Test invalid flags. Expect to fail with EINVAL error code. */
67 if (!syscall(__NR_process_mrelease, pidfd, (unsigned int)-1) ||
69 res = (errno == ENOSYS ? KSFT_SKIP : KSFT_FAIL);
70 perror("process_mrelease with wrong flags");
74 * Test reaping while process is alive with no pending SIGKILL.
75 * Expect to fail with EINVAL error code.
77 if (!syscall(__NR_process_mrelease, pidfd, 0) || errno != EINVAL) {
78 res = (errno == ENOSYS ? KSFT_SKIP : KSFT_FAIL);
79 perror("process_mrelease on a live process");
84 static int child_main(int pipefd[], size_t size)
88 /* Allocate and fault-in memory and wait to be killed */
90 res = alloc_noexit(MB(size) / PAGE_SIZE, pipefd[1]);
104 /* Test a wrong pidfd */
105 if (!syscall(__NR_process_mrelease, -1, 0) || errno != EBADF) {
106 res = (errno == ENOSYS ? KSFT_SKIP : KSFT_FAIL);
107 perror("process_mrelease with wrong pidfd");
111 /* Start the test with 1MB child memory allocation */
115 * Pipe for the child to signal when it's done allocating
131 /* Child main routine */
132 res = child_main(pipefd, size);
137 * Parent main routine:
138 * Wait for the child to finish allocations, then kill and reap
141 /* Block until the child is ready */
142 res = read(pipefd[0], &byte, 1);
146 if (!kill(pid, SIGKILL))
147 waitpid(pid, NULL, 0);
151 pidfd = syscall(__NR_pidfd_open, pid, 0);
153 perror("pidfd_open");
154 if (!kill(pid, SIGKILL))
155 waitpid(pid, NULL, 0);
159 /* Run negative tests which require a live child */
160 run_negative_tests(pidfd);
162 if (kill(pid, SIGKILL)) {
163 res = (errno == ENOSYS ? KSFT_SKIP : KSFT_FAIL);
168 success = (syscall(__NR_process_mrelease, pidfd, 0) == 0);
171 * If we failed to reap because the child exited too soon,
172 * before we could call process_mrelease. Double child's memory
173 * which causes it to spend more time on cleanup and increases
174 * our chances of reaping its memory before it exits.
175 * Retry until we succeed or reach MAX_SIZE_MB.
177 if (errno == ESRCH) {
178 retry = (size <= MAX_SIZE_MB);
180 res = (errno == ENOSYS ? KSFT_SKIP : KSFT_FAIL);
181 perror("process_mrelease");
182 waitpid(pid, NULL, 0);
187 /* Cleanup to prevent zombies */
188 if (waitpid(pid, NULL, 0) < 0) {
199 printf("All process_mrelease attempts failed!\n");
203 printf("Success reaping a child with %zuMB of memory allocations\n",