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=1794957270; b=Jqdk8OZ0AfrW153O0gbEFLMEUs6NYSkXbdphJK12G/i3iYV1J/DBVNcn/qNFrIkHeRHG epteohpTSIHBgMKk2Hrwmhh50/lnccWuDX/t7Pdeclz5hgJZYre8aG73J57QRt+zkIpTH PGxlUwo2CViVatzph/MWTN00bRDHAV3dFmAwlWEQgwEOqFU6bZaEKaAoMrthXdHJBu8ri 47mPdGrZyUtskEFWrZU9CbemqzmTDplPeQP9k3Y7yzwV7Tw9sER/oxjJ5Ir2D/Ush4rVC YZ/Fpl0cFV7DDLfPtSWPap09N+g9gacnQxA6Ync8iGCW3KhApm9afA/afePfJokvAvA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794957270; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=zwDa1R5hTozvkPRWD0eMYwP+MniaC6Knzv1Xuv/zVws=; b=gNltTvXMtVM3zYppCQwYZx+tY4zMurXGKstqycYfiKWbN8l2wxr1hRk46M2HPlfwfjSw NZUmlo0zgzaYNm7OE/I/BEKAzcLQpsZLCTjrSvu/Jm95tc2Uxo6b58caHz4hAEoPkXEUL rzK4Fp2im/cRdEGmOC2pxN3NtM3x82qolHKqPz1ayCteGB9JzSgs7HHEAK7WvL+VdMqO7 yyL0YfElYvSwFWIvbA9ssQr22//6PHJ0+Yk/Fj/S0vVsSgTmWeXRmVoTyFqdDkg7H4NbW oLq+1OVHzUDX3xs3GY332XQTWFGhsCVQqVxUxBkudHUV5aLN5PF/Y1KbFDjWDwE6/Mw== 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=TSbA+he0; 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 8DB36606A334; Tue, 17 Nov 2026 23:14:30 +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=TSbA+he0 Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 670F21F000C5; Tue, 17 Nov 2026 23:14:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794957270; bh=zwDa1R5hTozvkPRWD0eMYwP+MniaC6Knzv1Xuv/zVws=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TSbA+he0G7OYrax9FVe2/eY1mXOMJeANzHnN6Hu84Hg8aXZThc50BP7+5n7RWJkCB V+LglBYUl7bgg+neNkoktPveKlVpOsfwe/VLpFYM8mbeJP3Yt7K069EjsGbt5pSaj4 DUFWRsuKzuRxXfmWyFT6zPm4lY06RGQwTkx7ILROx51IKYI1v4UlLMkbLVAkns9S/0 4lpeBoMuf2EC5iT4ziqBwPyU7Rrkg8p+qwL+lWK9C2IgTJ/+CZjSz8OA5m4tIoNMxL XEbkizXDiykvLs/tCoAHVE6hoMlh3xiiwHEtxN32p9Nk23EiknnUoNqQ4A5/LPp8Cf 4oA1UYAYAKidQ== From: Jarkko Sakkinen To: linux-fsdevel@vger.kernel.org Cc: Jarkko Sakkinen , linux-api@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Alexander Viro , Andrew Morton , Andy Lutomirski , Ben Segall , Christian Brauner , David Hildenbrand , David Howells , Davidlohr Bueso , Dietmar Eggemann , Ingo Molnar , Jan Kara , Jonathan Corbet , John Johansen , Juri Lelli , Kees Cook , K Prateek Nayak , "Liam R. Howlett" , Lorenzo Stoakes , Manfred Spraul , Mel Gorman , Michal Hocko , Mike Rapoport , Neal Gompa , Paul Moore , Pavel Tikhomirov , Peter Zijlstra , Randy Dunlap , Serge Hallyn , Shuah Khan , Steven Rostedt , Suren Baghdasaryan , Valentin Schneider , Vincent Guittot , Vlastimil Babka , Zygmunt Krynicki Subject: [RFC PATCH v2 05/13] rootns: Add rootns_wait() system call Date: Wed, 18 Nov 2026 01:13:44 +0200 Message-ID: <20261117231352.3816603-6-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261117231352.3816603-1-jarkko@kernel.org> References: <20261117231352.3816603-1-jarkko@kernel.org> X-Mailing-List: linux-doc@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 wait4() and waitid() only allow a parent or subreaper to collect exit status, so an external manager holding a rootns file descriptor cannot wait on a rootns init process forked by another process or observe exit status multiple times across observers. Add the rootns_wait() system call and its compat entry point to wait on a rootns file descriptor until its init thread group exits and report the recorded wait status and rusage. Return the init TGID translated into the caller's active PID namespace, 0 for WNOHANG while still running, or -ECHILD when the PID is not visible to the caller. Co-developed-by: David Howells Signed-off-by: David Howells Signed-off-by: Jarkko Sakkinen --- include/linux/compat.h | 4 ++ include/linux/syscalls.h | 2 + kernel/rootns.c | 105 +++++++++++++++++++++++++++++++++++++++ kernel/sys_ni.c | 2 + 4 files changed, 113 insertions(+) diff --git a/include/linux/compat.h b/include/linux/compat.h index ad37ca617e65..3a9ac9dbb24c 100644 --- a/include/linux/compat.h +++ b/include/linux/compat.h @@ -779,6 +779,10 @@ asmlinkage long compat_sys_recvmmsg_time32(int fd, struct compat_mmsghdr __user asmlinkage long compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options, struct compat_rusage __user *ru); +asmlinkage long compat_sys_rootns_wait(int rootnsfd, + compat_int_t __user *status, + unsigned int options, + struct compat_rusage __user *ru); asmlinkage long compat_sys_fanotify_mark(int, unsigned int, __u32, __u32, int, const char __user *); asmlinkage long compat_sys_open_by_handle_at(int mountdirfd, diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index 272d54bddbe2..60e9c4ff71e7 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -1007,6 +1007,8 @@ asmlinkage long sys_lsm_set_self_attr(unsigned int attr, struct lsm_ctx __user * u32 size, u32 flags); asmlinkage long sys_lsm_list_modules(u64 __user *ids, u32 __user *size, u32 flags); asmlinkage long sys_rootns_create(unsigned int flags); +asmlinkage long sys_rootns_wait(int rootnsfd, int __user *status, + unsigned int options, struct rusage __user *ru); /* * Architecture-specific system calls diff --git a/kernel/rootns.c b/kernel/rootns.c index caa810b65d0d..83ab759a31ec 100644 --- a/kernel/rootns.c +++ b/kernel/rootns.c @@ -5,6 +5,8 @@ #include #include +#include +#include #include #include #include @@ -284,3 +286,106 @@ SYSCALL_DEFINE1(rootns_create, unsigned int, flags) return fd; } + +/* + * Wait for rootns init to exit and report results for the caller. + * + * Returns caller-visible init TGID on success, 0 for WNOHANG when still + * running, and -ECHILD when init pid is not visible in caller pidns. + */ +static long rootns_wait_common(int rootnsfd, unsigned int options, + int *status, struct rusage *ru) +{ + CLASS(fd, f)(rootnsfd); + struct pid_namespace *pid_ns = task_active_pid_ns(current); + struct rootns *rootns; + long ret; + + if (fd_empty(f)) + return -EBADF; + if (!is_rootns_file(fd_file(f)) || (options & ~WNOHANG)) + return -EINVAL; + + rootns = fd_file(f)->private_data; + if (!(options & WNOHANG)) { + ret = wait_event_killable(rootns->waitq, + READ_ONCE(rootns->state) == ROOTNS_DEAD); + if (ret) + return ret; + } + + scoped_guard(spinlock, &rootns->lock) { + if (rootns->state != ROOTNS_DEAD) + return 0; + if (!rootns->init_pid) + return -ECHILD; + ret = pid_nr_ns(rootns->init_pid, pid_ns); + if (ret <= 0) + return -ECHILD; + if (status) + *status = rootns->exit_code; + if (ru) + *ru = rootns->rusage; + } + + return ret; +} + +/** + * sys_rootns_wait - Wait for root namespace termination and collect init status + * @rootnsfd: Root namespace control file descriptor + * @status: Optional user pointer to receive wait status of init + * @options: Wait options (0 or %WNOHANG) + * @ru: Optional user pointer to receive resource usage of init + * + * Wait killably (unless %WNOHANG is set in @options) until @rootnsfd reaches + * %ROOTNS_DEAD, then copy out the recorded exit status and resource usage of + * the rootns init thread group. + * + * Return: Caller-visible init TGID on success, 0 if %WNOHANG is set and the + * rootns has not terminated, %-ECHILD if init was never started or is not + * visible in the caller's active PID namespace, or negative error code. + */ +SYSCALL_DEFINE4(rootns_wait, int, rootnsfd, int __user *, status, + unsigned int, options, struct rusage __user *, ru) +{ + int code; + long ret; + struct rusage rusage; + + ret = rootns_wait_common(rootnsfd, options, + status ? &code : NULL, + ru ? &rusage : NULL); + if (ret <= 0) + return ret; + + if (status && put_user(code, status)) + return -EFAULT; + if (ru && copy_to_user(ru, &rusage, sizeof(rusage))) + return -EFAULT; + return ret; +} + +#ifdef CONFIG_COMPAT +COMPAT_SYSCALL_DEFINE4(rootns_wait, int, rootnsfd, + compat_int_t __user *, status, + unsigned int, options, + struct compat_rusage __user *, ru) +{ + int code; + long ret; + struct rusage rusage; + + ret = rootns_wait_common(rootnsfd, options, + status ? &code : NULL, + ru ? &rusage : NULL); + if (ret <= 0) + return ret; + + if (status && put_user(code, status)) + return -EFAULT; + if (ru && put_compat_rusage(&rusage, ru)) + return -EFAULT; + return ret; +} +#endif diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c index 4a6c4337cc74..f9918d38107b 100644 --- a/kernel/sys_ni.c +++ b/kernel/sys_ni.c @@ -401,3 +401,5 @@ COND_SYSCALL(uprobe); /* kernel/rootns.c */ COND_SYSCALL(rootns_create); +COND_SYSCALL(rootns_wait); +COND_SYSCALL_COMPAT(rootns_wait); -- 2.47.3