From: Christian Brauner <brauner@kernel.org>
To: Jann Horn <jannh@google.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
linux-fsdevel@vger.kernel.org,
Arnaldo Carvalho de Melo <acme@kernel.org>,
linux-perf-users@vger.kernel.org, bpf@vger.kernel.org,
selinux@vger.kernel.org, Kees Cook <kees@kernel.org>,
linux-mm@kvack.org, linux-security-module@vger.kernel.org,
apparmor@lists.ubuntu.com, David Hildenbrand <david@kernel.org>,
"Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH RFC 19/19] selftests/exec: test when a vfork() parent is woken up
Date: Tue, 24 Nov 2026 23:58:42 +0100 [thread overview]
Message-ID: <20261124-work-exec-update_lock-v1-19-e489035395b8@kernel.org> (raw)
In-Reply-To: <20261124-work-exec-update_lock-v1-0-e489035395b8@kernel.org>
Test that a task which called vfork() only runs again once the child has
left the mm:
- when the child execs
- when the child is killed in the middle of the exec
- when the child exits
A parent that is woken up too early only has a few instructions to catch
the child on the mm. So the parent runs in a realtime scheduling class
on the cpu of the child and preempts it as soon as it's woken up. That
takes a kernel with preemption. The tests are skipped without it and
without the right to use a realtime class.
To kill the child between the release of the old mm and the switch to
the new one it has to wait for exec_update_lock there. So there are more
readers of its /proc/<pid>/stat than the cpu they share can run and some
of them are preempted with the lock held.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
tools/testing/selftests/exec/.gitignore | 1 +
tools/testing/selftests/exec/Makefile | 1 +
tools/testing/selftests/exec/vfork-mm.c | 363 ++++++++++++++++++++++++++++++++
3 files changed, 365 insertions(+)
diff --git a/tools/testing/selftests/exec/.gitignore b/tools/testing/selftests/exec/.gitignore
index b27f4db42f87b..24f8f80543df1 100644
--- a/tools/testing/selftests/exec/.gitignore
+++ b/tools/testing/selftests/exec/.gitignore
@@ -18,6 +18,7 @@ null-argv
/set-exec
/unshare-sighand
/update-lock-fault
+/vfork-mm
xxxxxxxx*
pipe
S_I*.test
diff --git a/tools/testing/selftests/exec/Makefile b/tools/testing/selftests/exec/Makefile
index 70f090a5f7648..7d61203d6547b 100644
--- a/tools/testing/selftests/exec/Makefile
+++ b/tools/testing/selftests/exec/Makefile
@@ -22,6 +22,7 @@ TEST_GEN_PROGS += null-argv
TEST_GEN_PROGS += check-exec
TEST_GEN_PROGS += update-lock-fault
TEST_GEN_PROGS += unshare-sighand
+TEST_GEN_PROGS += vfork-mm
# binfmt_misc must not be reachable as an exec source or as a stacking layer,
# or an 'F' entry can pin the instance that owns it. Unprivileged, no bpf.
diff --git a/tools/testing/selftests/exec/vfork-mm.c b/tools/testing/selftests/exec/vfork-mm.c
new file mode 100644
index 0000000000000..43cdae8e1e7b3
--- /dev/null
+++ b/tools/testing/selftests/exec/vfork-mm.c
@@ -0,0 +1,363 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test that a task which called vfork() only runs again once the child has
+ * left the mm, i.e. once it has switched to its new mm in exec or has dropped
+ * the mm in exit, also when it is killed in the middle of an exec.
+ *
+ * The first field of /proc/<pid>/statm is the size of the mm a task is on.
+ * The parent has a mapping that is larger than anything the child ends up
+ * with: an exec'd image of this program is a lot smaller and a task that has
+ * dropped its mm shows 0.
+ *
+ * A parent that is woken up too early only has a few instructions to look
+ * before the child is gone. So it runs in a realtime scheduling class and the
+ * child, which is on the same cpu, doesn't. On a kernel with preemption the
+ * parent runs as soon as it's woken up and the child doesn't get any further
+ * meanwhile. Without preemption the child gets too far for the parent to see
+ * anything so the tests are skipped there.
+ */
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/futex.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+/* 32 bit architectures that only have the time64 system calls. */
+#if !defined(SYS_futex) && defined(SYS_futex_time64)
+#define SYS_futex SYS_futex_time64
+#endif
+
+/* Makes the exec'd image wait to be killed. */
+#define EXECED_ARG "execed"
+
+/* Exit status of a child that never made it into the exec'd image. */
+#define EXIT_NO_EXEC 127
+
+/* How often the parent looks at a child. */
+#define ROUNDS 16
+
+/* What only the mm of the parent has. */
+#define MAPPING_SIZE (1UL << 30)
+
+/* How many readers of /proc/<pid>/stat share one cpu. */
+#define READERS 8
+
+/* Size in pages of the mm @pid is on, 0 if it has none. */
+static long mm_size(pid_t pid)
+{
+ char path[64], buf[128];
+ ssize_t len;
+ int fd;
+
+ snprintf(path, sizeof(path), "/proc/%d/statm", pid);
+ fd = open(path, O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return -1;
+ len = read(fd, buf, sizeof(buf) - 1);
+ close(fd);
+ if (len <= 0)
+ return -1;
+ buf[len] = '\0';
+ return strtol(buf, NULL, 10);
+}
+
+/* A kernel that preempts kernel code says so in its version string. */
+static bool kernel_preempts(void)
+{
+ struct utsname u;
+
+ return !uname(&u) && strstr(u.version, "PREEMPT");
+}
+
+/* The child only gets to call exec and _exit(). */
+static pid_t vfork_exec(void)
+{
+ pid_t pid = vfork();
+
+ if (pid == 0) {
+ execl("/proc/self/exe", "vfork-mm", EXECED_ARG, (char *)NULL);
+ _exit(EXIT_NO_EXEC);
+ }
+ return pid;
+}
+
+/*
+ * Same, but the child tells whoever reads from @tell who it is and waits to
+ * be told on @go. The kernel clears *@tid when the child releases the mm.
+ */
+static pid_t vfork_exec_tid(int tell, int go, int *tid)
+{
+ pid_t pid = vfork();
+
+ if (pid == 0) {
+ pid_t self = getpid();
+ char c;
+
+ *tid = self;
+ syscall(SYS_set_tid_address, tid);
+ if (write(tell, &self, sizeof(self)) == sizeof(self) &&
+ read(go, &c, 1) == 1)
+ execl("/proc/self/exe", "vfork-mm", EXECED_ARG,
+ (char *)NULL);
+ _exit(EXIT_NO_EXEC);
+ }
+ return pid;
+}
+
+static pid_t vfork_exit(void)
+{
+ pid_t pid = vfork();
+
+ if (pid == 0)
+ _exit(EXIT_SUCCESS);
+ return pid;
+}
+
+FIXTURE(vfork_mm)
+{
+ char *map;
+ long pages;
+ /* The clear_child_tid word of a child, shared with the helper. */
+ int *tid;
+ /* The cpu the parent and its children are on. */
+ int cpu;
+ /* The cpus the test was started on. */
+ cpu_set_t allowed;
+};
+
+FIXTURE_SETUP(vfork_mm)
+{
+ struct sched_param param = { .sched_priority = 1 };
+ cpu_set_t cpus;
+ long size;
+
+ if (!kernel_preempts())
+ SKIP(return, "Needs a kernel with preemption");
+
+ self->pages = MAPPING_SIZE / sysconf(_SC_PAGESIZE);
+ self->map = mmap(NULL, MAPPING_SIZE, PROT_NONE,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
+ if (self->map == MAP_FAILED)
+ SKIP(return, "Can't map %lu bytes", MAPPING_SIZE);
+ size = mm_size(getpid());
+ if (size < 0)
+ SKIP(return, "Can't read /proc/self/statm");
+ ASSERT_GE(size, self->pages);
+
+ self->tid = mmap(NULL, sizeof(*self->tid), PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ ASSERT_NE(self->tid, MAP_FAILED);
+
+ /* Too many cpus for the mask: the helper takes any cpu it can get. */
+ if (sched_getaffinity(0, sizeof(self->allowed), &self->allowed))
+ memset(&self->allowed, 0xff, sizeof(self->allowed));
+
+ /* The policy isn't inherited, the cpu is. */
+ self->cpu = sched_getcpu();
+ CPU_ZERO(&cpus);
+ CPU_SET(self->cpu, &cpus);
+ if (sched_setaffinity(0, sizeof(cpus), &cpus) ||
+ sched_setscheduler(0, SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m))
+ SKIP(return, "Can't make the parent preempt the child");
+}
+
+FIXTURE_TEARDOWN(vfork_mm)
+{
+ munmap(self->map, MAPPING_SIZE);
+ munmap(self->tid, sizeof(*self->tid));
+}
+
+TEST_F(vfork_mm, exec)
+{
+ int status;
+ pid_t pid;
+ long size;
+ int i;
+
+ for (i = 0; i < ROUNDS; i++) {
+ pid = vfork_exec();
+ /* Look before anything else. */
+ size = mm_size(pid);
+ ASSERT_GT(pid, 0);
+ kill(pid, SIGKILL);
+ ASSERT_EQ(waitpid(pid, &status, 0), pid);
+ /* It didn't exit on its own so the exec didn't fail. */
+ ASSERT_TRUE(WIFSIGNALED(status));
+ ASSERT_GE(size, 0);
+ ASSERT_LT(size, self->pages) {
+ TH_LOG("round %d: vfork() returned with the child still on the mm", i);
+ }
+ }
+}
+
+/* Take exec_update_lock of @pid for reading until it's gone. */
+static void stat_reader(pid_t pid, int ready)
+{
+ char path[64], buf[1024];
+ int fd;
+
+ snprintf(path, sizeof(path), "/proc/%d/stat", pid);
+ fd = open(path, O_RDONLY | O_CLOEXEC);
+ if (fd < 0 || pread(fd, buf, sizeof(buf), 0) <= 0 ||
+ write(ready, "", 1) != 1)
+ _exit(EXIT_FAILURE);
+ while (pread(fd, buf, sizeof(buf), 0) > 0)
+ ;
+ _exit(EXIT_SUCCESS);
+}
+
+/*
+ * Kill whoever shows up on @tell right when it has released its mm in exec.
+ * It only stops there if it has to wait for exec_update_lock. So there are
+ * more readers of its /proc/<pid>/stat than the one cpu they share can run:
+ * most of them are preempted at any time, some of them with the lock held.
+ */
+static void killer(FIXTURE_DATA(vfork_mm) *self, pid_t parent, int tell,
+ int go)
+{
+ struct sched_param param = { .sched_priority = 1 };
+ pid_t helper = getpid();
+ cpu_set_t cpus;
+ int ready[2];
+ int cpu, tid;
+ pid_t pid;
+ char c;
+ int i;
+
+ prctl(PR_SET_PDEATHSIG, SIGKILL);
+ if (getppid() != parent)
+ _exit(EXIT_FAILURE);
+
+ /* Any cpu the test may use but the one of the child. */
+ for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
+ if (cpu == self->cpu || !CPU_ISSET(cpu, &self->allowed))
+ continue;
+ CPU_ZERO(&cpus);
+ CPU_SET(cpu, &cpus);
+ if (!sched_setaffinity(0, sizeof(cpus), &cpus))
+ break;
+ }
+
+ if (read(tell, &pid, sizeof(pid)) != sizeof(pid) ||
+ pipe2(ready, O_CLOEXEC))
+ _exit(EXIT_FAILURE);
+
+ for (i = 0; i < READERS; i++) {
+ pid_t reader = fork();
+
+ if (reader < 0)
+ _exit(EXIT_FAILURE);
+ if (reader == 0) {
+ prctl(PR_SET_PDEATHSIG, SIGKILL);
+ if (getppid() != helper)
+ _exit(EXIT_FAILURE);
+ stat_reader(pid, ready[1]);
+ }
+ }
+ for (i = 0; i < READERS; i++) {
+ if (read(ready[0], &c, 1) != 1)
+ _exit(EXIT_FAILURE);
+ }
+
+ /* The kernel wakes us up when it has cleared the word. */
+ if (sched_setscheduler(0, SCHED_FIFO, ¶m) || write(go, "", 1) != 1)
+ _exit(EXIT_FAILURE);
+ while ((tid = __atomic_load_n(self->tid, __ATOMIC_RELAXED)))
+ syscall(SYS_futex, self->tid, FUTEX_WAIT, tid, NULL, NULL, 0);
+ kill(pid, SIGKILL);
+
+ /* The readers go when we do. */
+ for (;;)
+ pause();
+}
+
+/*
+ * A child that is killed in exec after it has released the mm doesn't get to
+ * switch to a new one. It leaves the mm when it exits.
+ */
+TEST_F(vfork_mm, exec_killed)
+{
+ pid_t parent = getpid();
+ int tell[2], go[2];
+ pid_t pid, helper;
+ int nomm = 0;
+ int status;
+ long size;
+ int i;
+
+ for (i = 0; i < ROUNDS; i++) {
+ ASSERT_EQ(pipe2(tell, O_CLOEXEC), 0);
+ ASSERT_EQ(pipe2(go, O_CLOEXEC), 0);
+ *self->tid = 1;
+
+ helper = fork();
+ ASSERT_GE(helper, 0);
+ if (helper == 0) {
+ close(tell[1]);
+ close(go[0]);
+ killer(self, parent, tell[0], go[1]);
+ }
+ close(tell[0]);
+ close(go[1]);
+
+ pid = vfork_exec_tid(tell[1], go[0], self->tid);
+ /* Look before anything else. */
+ size = mm_size(pid);
+ ASSERT_GT(pid, 0);
+ kill(pid, SIGKILL);
+ ASSERT_EQ(waitpid(pid, &status, 0), pid);
+ kill(helper, SIGKILL);
+ ASSERT_EQ(waitpid(helper, NULL, 0), helper);
+ close(tell[1]);
+ close(go[0]);
+ ASSERT_TRUE(WIFSIGNALED(status));
+ ASSERT_GE(size, 0);
+ ASSERT_LT(size, self->pages) {
+ TH_LOG("round %d: vfork() returned with the child still on the mm", i);
+ }
+ if (!size)
+ nomm++;
+ }
+ if (!nomm)
+ SKIP(return, "The child was never killed before it got to the new mm");
+}
+
+TEST_F(vfork_mm, exit)
+{
+ int status;
+ pid_t pid;
+ long size;
+ int i;
+
+ for (i = 0; i < ROUNDS; i++) {
+ pid = vfork_exit();
+ /* Look before anything else. */
+ size = mm_size(pid);
+ ASSERT_GT(pid, 0);
+ ASSERT_EQ(waitpid(pid, &status, 0), pid);
+ ASSERT_TRUE(WIFEXITED(status));
+ ASSERT_EQ(size, 0) {
+ TH_LOG("round %d: vfork() returned with the child still on the mm", i);
+ }
+ }
+}
+
+int main(int argc, char **argv)
+{
+ if (argc == 2 && !strcmp(argv[1], EXECED_ARG)) {
+ for (;;)
+ pause();
+ }
+ return test_harness_run(argc, argv);
+}
--
2.53.0
next prev parent reply other threads:[~2026-11-24 23:00 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 01/19] proc: copy namespace link after dropping exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 02/19] kcmp: look up epoll target without exec_update_lock held Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 03/19] perf: copy counter values after dropping ctx->mutex Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 05/19] selinux: close inherited files that can't be replaced Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 06/19] exec: move unshare_sighand() before exec_mmap() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 07/19] selftests/exec: test exec with a task sleeping in a page fault Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 08/19] selftests/exec: test exec with a shared signal table Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 09/19] lsm: add security_bprm_revalidate_files() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 10/19] bpf: mark bprm_revalidate_files as sleepable Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 11/19] selinux: revalidate inherited files before taking exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 12/19] apparmor: " Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 13/19] exec: drop exec_update_lock after the credentials are committed Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 14/19] exec: switch time namespace after dropping exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 15/19] exec: don't take the mmap lock of the old mm Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 16/19] exit: don't take the mmap lock in exit_mm() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 17/19] proc: look up map_files vma without exec_update_lock held Christian Brauner
2026-11-24 23:09 ` sashiko-bot
2026-11-24 22:58 ` [PATCH RFC 18/19] vfork: wake up the parent once the child has left the mm Christian Brauner
2026-11-24 22:58 ` Christian Brauner [this message]
2026-11-24 23:15 ` [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock sashiko-bot
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=20261124-work-exec-update_lock-v1-19-e489035395b8@kernel.org \
--to=brauner@kernel.org \
--cc=acme@kernel.org \
--cc=apparmor@lists.ubuntu.com \
--cc=bpf@vger.kernel.org \
--cc=david@kernel.org \
--cc=jannh@google.com \
--cc=kees@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=selinux@vger.kernel.org \
--cc=torvalds@linux-foundation.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