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 6A029606A6B4 for ; Sat, 5 Dec 2026 03:28:52 +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=F2hNhS/R Received: by kanga.kvack.org (Postfix) id 5B44F6B05CB; Fri, 4 Dec 2026 22:27:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 58B9D6B05CC; Fri, 4 Dec 2026 22:27:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4533F6B05CD; Fri, 4 Dec 2026 22:27:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D2A406B05CB for ; Fri, 4 Dec 2026 22:27:40 -0500 (EST) Received: from smtpin03.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 360B380589 for ; Sat, 5 Dec 2026 03:27:35 +0000 (UTC) X-FDA: 85508494470.03.07C674F Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf20.hostedemail.com (Postfix) with ESMTP id 8436B1C0008 for ; Sat, 5 Dec 2026 03:27:33 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="F2hNhS/R"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf20.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=1796441253; b=dEv4WUxlJEysGklrlqud0W33BCIGUJonInpRZLNSokb+eDWG9RfkdIHXgkQYbF+8JgU0pb 85A4gXFqIthO2imvGm1ximTEQlhPTLT4uU4hFwifr9kspjtfOWaeYFA6vaQUw0+rcuaxtB ir/pOSyQE0GPy8h2H4g9caDdd5eblqE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796441253; 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=E1QENmcA9GnJGnsxOUI6hkzBKO4OwRHA0vfyKmuBtnQ=; b=FquprA0YndfZMiIvIzCqOGFBoHiiQkSd2ZC9W/1ogR3c2cYJsm3YotxAU7OOO9+wZREO5I a/pDF4dEauQ/+VanGL6Hk5tjb5gVu71S1eDk2ROC8nx5VXQQ1/pX4DyIiHpvR1Z9lxZAh+ /kKchIgUSQoMQwYlfJdgd2ndyJOK7jI= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="F2hNhS/R"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf20.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 C985241FE5; Sat, 5 Dec 2026 03:27:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 04CE01F00A1C; Sat, 5 Dec 2026 03:27:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796441252; bh=E1QENmcA9GnJGnsxOUI6hkzBKO4OwRHA0vfyKmuBtnQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=F2hNhS/RjElem+l/wZSgkaPopOsjXrpE9xroDROjaEeMA2SxZosIf6Xdye3k7sXvz XNQ2deiOJiKM0texLEC0v0tRZM5naVpI0YBWL+mdMrgAynTZMTAA45dMDGyBO8GGk7 FKwoyFXCeamsiki8463Q55C8fryyNSlwfCyXB7id62kTPqszF8W3nbdQkIJERoy1NT piacZJnL82CFhAceTahCZBggJQ5/kcrXga5DUymoNo0SWusCvjA0YM/vWzCsDeu6lX rh7aMDKvvCpuoavVtSZMycx+PaNSoZOpuhuVYQ8uWMH23ScKr0xhf5766qPoJt51fE hndohJxTTEwZg== 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-integrity@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 , Alexei Starovoitov , Andrew Morton , Andrii Nakryiko , Andy Lutomirski , Arnd Bergmann , Baolin Wang , Ben Segall , Christian Brauner , Daniel Borkmann , David Hildenbrand , David Howells , David Woodhouse , Davidlohr Bueso , Dietmar Eggemann , Eduard Zingerman , Emil Tsalapatis , Eric Biggers , Georgia Garcia , Giuseppe Scrivano , =?UTF-8?q?G=C3=BCnther=20Noack?= , Hugh Dickins , Ihor Solodrai , Ingo Molnar , James Morris , Jan Kara , Jason Gunthorpe , Jeff Layton , Jiri Olsa , John Johansen , Jonathan Corbet , Josef Bacik , Juri Lelli , Kees Cook , K Prateek Nayak , KP Singh , Kumar Kartikeya Dwivedi , Lennart Poettering , "Liam R. Howlett" , Manfred Spraul , Martin KaFai Lau , Matt Bobrowski , Mel Gorman , Michal Hocko , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Mike Rapoport , Neal Gompa , Oleg Nesterov , Paul Moore , Pavel Tikhomirov , Peter Zijlstra , Randy Dunlap , "Serge E. Hallyn" , Serge Hallyn , Shuah Khan , Song Liu , Stephen Smalley , Steven Rostedt , Valentin Schneider , Vincent Guittot , Yonghong Song , Zygmunt Krynicki Subject: [RFC PATCH v10 11/28] runfd: Add core infrastructure Date: Sat, 5 Dec 2026 05:26:16 +0200 Message-ID: <20261205032633.1702772-12-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261205032633.1702772-1-jarkko@kernel.org> References: <20261205032633.1702772-1-jarkko@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: jbj4tagm7hhc3quyq5dzngfmo9fepfp8 X-Spam-Status: No, score=-19.1 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-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 8436B1C0008 X-HE-Tag: 1796441253-138369 X-HE-Meta: U2FsdGVkX1/EW3hnIfAm7CMyguHx1ahWWpTLrgRFr2dXJIYQR+QMQp0LaiRiFLEx87JU82EBspTI0ouvSUp18R5RBC7expH0CYeq7tdV5vJqlhn0b1ISjo4nqT5IJ1MQhbJdaykOddDKnOZiQ24ohxrNNlf9nVwpB/Q7VqkTLyx2l7sw/xIQAeKn1zbE8am8K/PeE6jUR+zLmzzXHtRe/Dp9nR2EyNFkNWfA+6r4VvI7iv/VVFRsk8QE+QxINfiPKcCAjTnAa8AP91K2ZV9hDXbU909Stk/r7OXVACgeft1pJH8M5p8C/dc6GpEG2KUshWm3uoDnzFz8z9LwgDeS6E5f6pZUKaUFPKRAQOYugHaQ8WnVCP4EAN7R/4MdRFJkHjtYQlNtfT9Waq5szpBqkV8nFMW9wk6Ko0adgEHHlax3ctro1LokzvbJdDZZTa9sIaZtDn01TzKje/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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 Linux namespaces can be created and entered individually, but userspace orchestrators lack a single control object that binds a full detached namespace set and an init process lifecycle behind a pollable file descriptor. Because this object manages process lifecycle, wait and signal delivery, VFS mount delegation, and LSM state rather than representing an nsfs namespace entity, model it as a distinct run abstraction. Add struct run, its lifecycle states, reference counting, uAPI flags, and anonymous inode file operations under CONFIG_RUNFD. Drop active and passive namespace references on final release, send SIGKILL to the run init thread group on close when RUNFD_AUTOKILL is set, and report EPOLLHUP from poll() once the run reaches RUNFD_DEAD. Co-developed-by: David Howells Signed-off-by: David Howells Signed-off-by: Jarkko Sakkinen --- include/linux/runfd.h | 83 ++++++++++++++++++++++++++++++++ include/uapi/linux/runfd.h | 22 +++++++++ init/Kconfig | 10 ++++ kernel/Makefile | 2 + kernel/runfd.c | 96 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 213 insertions(+) create mode 100644 include/linux/runfd.h create mode 100644 include/uapi/linux/runfd.h create mode 100644 kernel/runfd.c diff --git a/include/linux/runfd.h b/include/linux/runfd.h new file mode 100644 index 000000000000..d7faba4da949 --- /dev/null +++ b/include/linux/runfd.h @@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2017-2019 Red Hat, Inc. All Rights Reserved. + * Copyright (C) 2026 Opinsys Oy + */ + +#ifndef _LINUX_RUNFD_H +#define _LINUX_RUNFD_H + +#include +#include +#include +#include + +#define RUNFD_ALL_FLAGS RUNFD_AUTOKILL + +struct file; +struct nsproxy; +struct pid; +struct user_namespace; + +/** + * enum runfd_state - run lifecycle states + * @RUNFD_INIT: Build phase after ``runfd()`` before any + * process has entered. ``RUNFD_SHARE_NSFD`` is + * permitted, ID mappings may be configured on the owning + * user namespace, and ``"/"`` must be mounted in the + * mount namespace before entry. + * @RUNFD_FORKING: Bootstrap phase while the first + * ``clone3(CLONE_INTO_RUN)`` call creates PID 1. + * Concurrent callers wait killably; reverts to + * %RUNFD_INIT if ``copy_process()`` fails, or advances + * to %RUNFD_RUNNING once PID 1 is attached. + * @RUNFD_RUNNING: Live phase while PID 1 is attached. Subsequent + * ``clone3(CLONE_INTO_RUN)`` calls join the PID + * namespace until teardown begins, + * ``RUNFD_GET_NSFD(CLONE_NEWPID)`` and + * ``pidfd_send_signal()`` are enabled, and + * ``RUNFD_SHARE_NSFD`` returns ``-EBUSY``. + * @RUNFD_DEAD: Terminal phase entered after PID 1 exits and the PID + * namespace has killed and reaped all member processes. + * ``poll()`` reports ``EPOLLHUP`` and + * ``waitid(P_RUNFD, ...)`` reports PID 1's exit status + * without consuming it. + * + * State transitions advance from %RUNFD_INIT through %RUNFD_FORKING and + * %RUNFD_RUNNING to %RUNFD_DEAD, reverting from %RUNFD_FORKING to + * %RUNFD_INIT only if the initial bootstrap fork fails. + */ +enum runfd_state { + RUNFD_INIT = 0, + RUNFD_FORKING = 1, + RUNFD_RUNNING = 2, + RUNFD_DEAD = 3, +}; + +struct run { + refcount_t usage; + int exit_code; + struct user_namespace *user_ns; + struct nsproxy *ns; + struct pid *init_pid; + spinlock_t lock; /* Guards run lifecycle state */ + wait_queue_head_t waitq; + unsigned int flags; + enum runfd_state state; +}; + +#ifdef CONFIG_RUNFD +void runfd_put(struct run *c); +bool runfd_is_file(struct file *file); +#else +static inline void runfd_put(struct run *c) +{ +} + +static inline bool runfd_is_file(struct file *file) +{ + return false; +} +#endif /* CONFIG_RUNFD */ + +#endif /* _LINUX_RUNFD_H */ diff --git a/include/uapi/linux/runfd.h b/include/uapi/linux/runfd.h new file mode 100644 index 000000000000..f46a757b1159 --- /dev/null +++ b/include/uapi/linux/runfd.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Copyright (C) 2017-2019 Red Hat, Inc. All Rights Reserved. + * Copyright (C) 2026 Opinsys Oy + */ + +#ifndef _UAPI_LINUX_RUNFD_H +#define _UAPI_LINUX_RUNFD_H + +/** + * enum runfd_flags - Flags for runfd() + * @RUNFD_AUTOKILL: Send %SIGKILL to run init on final fd release. + * + * Every run created by runfd() allocates a full set of + * detached namespaces owned by a fresh user namespace and sets %O_CLOEXEC + * on the returned control file descriptor. + */ +enum runfd_flags { + RUNFD_AUTOKILL = 0x00000001, +}; + +#endif /* _UAPI_LINUX_RUNFD_H */ diff --git a/init/Kconfig b/init/Kconfig index c8a7cfa60a6e..ffd0f1c19c4c 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1281,6 +1281,16 @@ config NET_NS Allow user space to create what appear to be multiple instances of the network stack. +config RUNFD + bool "Container run descriptors" + depends on USER_NS && PID_NS && UTS_NS && IPC_NS && NET_NS && CGROUPS + help + Allow user space to open and manage isolated runs and their init + process lifecycle through an anonymous-inode control file descriptor + via runfd(), waitid(P_RUNFD), pidfd_send_signal(), + RUNFD_GET_NSFD, RUNFD_SHARE_NSFD, FSCONFIG_SET_RUN, and + clone3() with CLONE_INTO_RUN. + endif # NAMESPACES config CHECKPOINT_RESTORE diff --git a/kernel/Makefile b/kernel/Makefile index 4ff92e24680c..eb39bd150903 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -152,6 +152,8 @@ KCOV_INSTRUMENT_kstack_erase.o := n obj-$(CONFIG_SCF_TORTURE_TEST) += scftorture.o +obj-$(CONFIG_RUNFD) += runfd.o + $(obj)/configs.o: $(obj)/config_data.gz # .config is small, so it is compressed with gzip rather than the image compressor. diff --git a/kernel/runfd.c b/kernel/runfd.c new file mode 100644 index 000000000000..019e90e8bf2f --- /dev/null +++ b/kernel/runfd.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2017-2019 Red Hat, Inc. All Rights Reserved. + * Copyright (C) 2026 Opinsys Oy + */ + +/** + * DOC: Overview + * + * A run is one instance of a container, from setup through init exit. + * ``runfd()`` creates a run and returns its control file descriptor before + * any process starts. Namespaces can be configured through file descriptors + * before ``clone3()`` starts init. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void runfd_put(struct run *c) +{ + if (!c || !refcount_dec_and_test(&c->usage)) + return; + + if (c->ns) + put_nsproxy(c->ns); + if (c->init_pid) + put_pid(c->init_pid); + if (c->user_ns) { + ns_ref_active_put(c->user_ns); + put_user_ns(c->user_ns); + } + kfree(c); +} + +static __poll_t runfd_poll(struct file *file, poll_table *wait) +{ + struct run *run = file->private_data; + __poll_t mask = 0; + + poll_wait(file, &run->waitq, wait); + + if (READ_ONCE(run->state) == RUNFD_DEAD) + mask |= EPOLLHUP; + + return mask; +} + +static int runfd_release(struct inode *inode, struct file *file) +{ + struct run *run = file->private_data; + + if (run->flags & RUNFD_AUTOKILL) { + struct pid *pid = NULL; + + scoped_guard(spinlock, &run->lock) { + if (run->state == RUNFD_RUNNING) + pid = get_pid(run->init_pid); + } + if (pid) { + kill_pid(pid, SIGKILL, true); + put_pid(pid); + } + } + runfd_put(run); + return 0; +} + +static const struct file_operations runfd_fops = { + .poll = runfd_poll, + .release = runfd_release, +}; + +bool runfd_is_file(struct file *file) +{ + return file->f_op == &runfd_fops; +} -- 2.47.3