From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8C34C606A69C for ; Sat, 28 Nov 2026 23:59:27 +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 kanga.kvack.org (Postfix) id B727E6B05C5; Sat, 28 Nov 2026 18:59:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B228A6B05C6; Sat, 28 Nov 2026 18:59:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A60166B05C7; Sat, 28 Nov 2026 18:59:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6F48C6B05C5 for ; Sat, 28 Nov 2026 18:59:13 -0500 (EST) Received: from smtpin10.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E91C216079C for ; Sat, 28 Nov 2026 23:59:12 +0000 (UTC) X-FDA: 85486196544.10.F62925C Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf10.hostedemail.com (Postfix) with ESMTP id 405CAC0008 for ; Sat, 28 Nov 2026 23:59:11 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=eLaGQC1t; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1795910351; b=WMfSbovDrUuzn5++dzwrf1HWOp92u2ECtXXiuV3+idB9NxW6hWOGwkmxv/mF/m7LglAE1J DlTGmr4opY8duCCFcRSKiUQPpn5LcCxxQzqP8wnabajWocX6NqfmneIwBtc+zGOR5rG+qH fAQSw7w8y+b2KU5nQoXKvhv+5KB5eiY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795910351; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0lGnMzERnsiMlT4QYXratYlxAZnBUu58H+tVdA7/PHw=; b=IaQJe6QzFY+0jZrdhyyyED+hDhm9cVtVQslZaL6nm548uhVBUsP/fFrCaxXAVRo33m0+6r j70Wu92UwZ5u0NlThMu5eKVYmBGykiRT2loewMT2Q8EY+XiV7CTYlHu4xtB6ZlUSda30YZ Zqhur9QlJARZXtYCbgsSJ4m3nFBIZ1A= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=eLaGQC1t; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with UTF8SMTP id 86CC243BCA; Sat, 28 Nov 2026 23:59:10 +0000 (UTC) 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 405CAC0008 X-Stat-Signature: 17ed56cfiwg48o8wy4q4g9k7htfqbbrz X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1795910351-597227 X-HE-Meta: U2FsdGVkX19LchSs1yIuHKnlQiA/7j4vqpuGQDcZgj6uNalZDv2eECcU55NpVC2ohT7jjBi0CNaux3BypCybxaKFK8U6rlC4zdKDM0h8kD8HdD6c5D4cqB9cL5G7U389nA/VJ0IT+U3CNA63LmGctYMlZxNjfnqDEz51l4D+RFJdpRAjmc4Oy8eriA6ak2sQU871hB+OG85vJYQksl/ZOI129JfCtz3TydkpPXqkY63W7rZPESxfW/9U/n0a9WPqbm3I3A+Dl0BT0Kr9fI5PNfZtSqKV7UrpQkstjMR8EKLXnisOvC73ZoSMOwQx5Mcn1FCXoJqpL3XQJjKpkazjfZFk2mokw2PqSMZsKs6M/CnzhJA5xVNHB6DbE9J0RlpW4S/RisW5mYsKl4mIBGzrVNazE0jT8WInIX2VjSzlbNmeDxMN/NbFjtPbGT104dDpDiteGrv8+AZyszC6+WTxMdWo7WZTbCafFZ1qOUsdlZAhmIT85AFYHAaqRyrEDtIbNeXF9T1kFamjScF3aWkWRWk37DUKY/1d6y2KzZ3i/BcWpoO7nGRwF2ei1u/iUFaxWNXXGiU84oEGB3FLlPEGL08gRVBDXzXvyGloJVVjbqgQ/j1i1m8siwA2deOclYHShrgakxZh2iPta+ncW9kDQVH4Qc4p2kmit/9cjoNASvGgplt5XdGXDA0E/wGI1OGJeMk9mKhWTtE9ytbB7eNsPII/liJV9hmlxPfnOS4MsRAqbWqd4LSIB9mydIT7vaVWHoeOWFmQzjK2Sx6ey0pMPBV5CG3sZ3qMTQXzNzV0seJWdR0O7qXzQKOOm6WdR0lrvGsJKmFAFCAwuW+ewQSL4O8CNSsmm03KbPrlC7XJGHeEHUf8WMDChLGWgXEwl9/hviXLRmVBH2nzwWpcJirmXS/ejEjkOtbyikumUmyGsR4gfpl/xCYJhsKElRTPZbXFWM08NnaT6Easj+qobvT A/Rt4ree Jnkh3gIksuSD7iUPWPFW4SIRzUxnIW2FV7Jzz6jvHhvmkekQRvy5k8/vx86oJFsqhzy7LxNwdxJ09YG1BPWztcsB3xBuAekH6xCMXkiAiNTaGGjVLj8NMj++wfS6Ezib+G+q+y3f3bKWznbfKJGz4jdvnpc8nEa+cmVPtMTuDFUGu/78D88y9JMcMlINJDoen/vVxCnZ3w/lgAyQErR3MPyAHqVHM0I7wQMxeZWj2XuAQ4j0zcqohX6QHBVcrDhOseF7SI3QNr8AaYJRFJ2yuOR08IVopb9e/xaaCxwTCHDu3T5FZVa8RenE8gb4p4k1CGs9eRCEmXBbnic5GoZQ2ofwO5uxBWInUGtwTkmR8Ld81hwIKaZUq9LqeHzDhCLkikB37p5dDagEGEoPZfKqgTcmnYfGcHNKSUktqXJH2BJ2a6do18upcwqK3rcoLyaXrvM/WaBwPIkaZE8rSlhtYi/rNR/1DB7oWA9Ba8i3T0NdQoHlWnHmX1SHeaz93WshXtnxicu9y0QZEteS9nwcSjvLkxrlQKnDGRBqzQojzSj/iKJFUEE7tytEEDdm83eXS8UyP Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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