From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795910351; b=UaQb3//dqNFqQEg7zm2g6vD/9rpHnwSaqOXLtlyhCYiVZOaowpP9L7lF5WRPzkVfG3z5 XGHpfvBY9MEL0nnymNm7foieZjzBzFTr4eoETY766dflld+4NfTqqxc9MLL3o+HrgAUFX fmwm3yAvF/JuPGsxnfxapUFkM0w3LZmSk/mSsfKr7j5ij4BkdmXXcCnwP6V3iZoHH02zQ 5bHzLWaBP6XM1K3mjURDgHPVAq+xR5Mh+hgIO+tiHUkTmH5dHrBCi8U1NvHVn+xvkHP1K g5J+qfJEwgfGRZc/7j8oJ0VZGG1UrYsdxb56qG/QpCW2I20D2YCPs9STSdaCcy+IzKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795910351; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=0lGnMzERnsiMlT4QYXratYlxAZnBUu58H+tVdA7/PHw=; b=s8aTTyW0F+NuwOc4Hc/+cVSqCFOK5lboaN0NO2mIWu15BvUI9CeNxrmaAJUcxBSNCjyJ +oJ43960Q05SebesCYg4UdxL5uQrbdFtEAu5FsKxE7OgaxzrbBnZ8QKyZOYrSPq3rfwac cA93Gzvh1xcI/hfutSq9nVO8GZ5YoA/pVMKCBNBFiQ3rJJxlnaU1FO2r5+JnNUH2v0Z78 glOq1LjyGMvb+/rHWksgTCSpZlOw+GUC5tKxjKWZqQc8sQ63hNw6uT0W4t+Fkgm9fwwH8 p+Wm+R2mCCxLIkg4VoWYER7YFDfoACn2wjeDthPnh8WqgFeNaotc83qghTgEVS/dLIA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=eLaGQC1t; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id CA8F3606A324; Sat, 28 Nov 2026 23:59:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=eLaGQC1t Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 822681F0092A; Sat, 28 Nov 2026 23:59:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795910350; bh=0lGnMzERnsiMlT4QYXratYlxAZnBUu58H+tVdA7/PHw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=eLaGQC1t73r7YGM2v/SYrykLF1f9TQzFjZ75t3KAVnz8+zMNFMSfVvz1lMxQOXOYM zR1KQpkJ7QDiF/LiNr6FYjrVCsp1tp2rj/stwQzHL17tzBD/UgTYA799scGFr79V5T LsMab3Icid6F0MQtinyAW5Gfn6w4xhSllDXQBI7qw8INcFvzielx+yTKP1yWf1jeOL cAqwdjlH1KuB8IH8gSq8Ujkc6tFwTrVGteA4FFUqD6QdWrBr5k6NC5wO3zVTVngsVm 2xABpZVtO4oHz/54Zts2euYePqAfVVdDxFAYA9DxAwfqmGce3bw2mVcQMXACn/NN9t n9/upHG0ZG3Gw== From: Jarkko Sakkinen To: fsdevel@vger.kernel.org Cc: Jarkko Sakkinen , bpf@vger.kernel.org, containers@lists.linux.dev, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Aleksa Sarai , Alexander Mikhalitsyn , Alexander Viro , Andy Lutomirski , Arnd Bergmann , Christian Brauner , David Howells , Eric Biggers , Georgia Garcia , Giuseppe Scrivano , Hugh Dickins , James Morris , Jan Kara , Jeff Layton , John Johansen , Jonathan Corbet , Josef Bacik , Kees Cook , KP Singh , Lennart Poettering , Matt Bobrowski , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Neal Gompa , Oleg Nesterov , Paul Moore , Pavel Tikhomirov , Serge Hallyn , Shuah Khan , Stephen Smalley , Zygmunt Krynicki Subject: [RFC PATCH v8 13/29] userland: Track init lifecycle via copy_process() and pid_namespace Date: Sun, 29 Nov 2026 01:57:52 +0200 Message-ID: <20261128235808.2369970-14-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261128235808.2369970-1-jarkko@kernel.org> References: <20261128235808.2369970-1-jarkko@kernel.org> X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A userland needs to record its init thread group's PID when bootstrapped and capture its exit status and UID once all processes in the userland have terminated, without serializing normal fork() and exit() paths inside the userland. Commit the bootstrap child reaper and bind pid_ns->userland in userland_copy() after copy_process() attaches PIDs and drops tasklist_lock, and invoke userland_exit() from zap_pid_ns_processes() after the userland's PID namespace has killed and reaped all member tasks. Co-developed-by: David Howells Signed-off-by: David Howells Signed-off-by: Jarkko Sakkinen --- include/linux/pid_namespace.h | 4 ++++ include/linux/userland.h | 15 +++++++++++++ kernel/fork.c | 4 ++++ kernel/pid_namespace.c | 2 ++ kernel/userland.c | 41 +++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+) diff --git a/include/linux/pid_namespace.h b/include/linux/pid_namespace.h index b20baaa7e62b..ef703a3b8ed5 100644 --- a/include/linux/pid_namespace.h +++ b/include/linux/pid_namespace.h @@ -15,6 +15,7 @@ #define MAX_PID_NS_LEVEL 32 struct fs_pin; +struct userland; #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) /* modes for vm.memfd_noexec sysctl */ @@ -45,6 +46,9 @@ struct pid_namespace { struct user_namespace *user_ns; struct ucounts *ucounts; int reboot; /* group exit code if this pidns was rebooted */ +#ifdef CONFIG_USERLAND + struct userland *userland; +#endif struct ns_common ns; struct work_struct work; } __randomize_layout; diff --git a/include/linux/userland.h b/include/linux/userland.h index 364dd27f0eb0..277a0568d8e9 100644 --- a/include/linux/userland.h +++ b/include/linux/userland.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -17,6 +18,8 @@ struct file; struct nsproxy; struct pid; +struct pid_namespace; +struct task_struct; struct user_namespace; /** @@ -60,6 +63,7 @@ struct userland { struct user_namespace *user_ns; struct nsproxy *ns; struct pid *init_pid; + kuid_t init_uid; spinlock_t lock; /* Guards userland lifecycle state */ wait_queue_head_t waitq; unsigned int flags; @@ -69,6 +73,8 @@ struct userland { #ifdef CONFIG_USERLAND void userland_put(struct userland *c); bool userland_is_file(struct file *file); +void userland_copy(struct pid *pid, struct userland *userland); +void userland_exit(struct pid_namespace *pid_ns, struct task_struct *tsk); #else static inline void userland_put(struct userland *c) { @@ -78,6 +84,15 @@ static inline bool userland_is_file(struct file *file) { return false; } + +static inline void userland_copy(struct pid *pid, struct userland *userland) +{ +} + +static inline void userland_exit(struct pid_namespace *pid_ns, + struct task_struct *tsk) +{ +} #endif /* CONFIG_USERLAND */ #endif /* _LINUX_USERLAND_H */ diff --git a/kernel/fork.c b/kernel/fork.c index 77d6c8fc38c6..47900d577cd1 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -83,6 +83,7 @@ #include #include #include +#include #include #include #include @@ -2584,6 +2585,9 @@ __latent_entropy struct task_struct *copy_process( syscall_tracepoint_update(p); write_unlock_irq(&tasklist_lock); + if (likely(p->pid) && thread_group_leader(p)) + userland_copy(pid, NULL); + if (pidfile) fd_install(pidfd, pidfile); diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index 127a5fd23e70..70606d9c5b52 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "pid_sysctl.h" @@ -280,6 +281,7 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns) current->signal->group_exit_code = pid_ns->reboot; acct_exit_ns(pid_ns); + userland_exit(pid_ns, me); return; } diff --git a/kernel/userland.c b/kernel/userland.c index 7be0c4b5385b..b7aa6caecafd 100644 --- a/kernel/userland.c +++ b/kernel/userland.c @@ -92,3 +92,44 @@ bool userland_is_file(struct file *file) { return file->f_op == &userland_fops; } + +void userland_copy(struct pid *pid, struct userland *c) +{ + if (likely(!c || !is_child_reaper(pid))) + return; + + scoped_guard(spinlock, &c->lock) { + if (c->state != USERLAND_FORKING) + return; + refcount_inc(&c->usage); + ns_of_pid(pid)->userland = c; + WRITE_ONCE(c->init_pid, get_pid(pid)); + WRITE_ONCE(c->state, USERLAND_RUNNING); + } +} + +void userland_exit(struct pid_namespace *pid_ns, struct task_struct *tsk) +{ + struct userland *c = pid_ns->userland; + struct task_struct *leader; + kuid_t uid; + int status; + + if (!c) + return; + + leader = tsk->group_leader; + uid = task_uid(leader); + status = (tsk->signal->flags & SIGNAL_GROUP_EXIT) ? + tsk->signal->group_exit_code : leader->exit_code; + + scoped_guard(spinlock, &c->lock) { + c->exit_code = status; + c->init_uid = uid; + WRITE_ONCE(c->state, USERLAND_DEAD); + } + + pid_ns->userland = NULL; + __wake_up(&c->waitq, TASK_NORMAL, 0, poll_to_key(EPOLLHUP)); + userland_put(c); +} -- 2.47.3