From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: "Amir Goldstein" <amir73il@gmail.com>,
"Josef Bacik" <josef@toxicpanda.com>,
"Jeff Layton" <jlayton@kernel.org>, "Mike Yuan" <me@yhndnzj.com>,
"Zbigniew Jędrzejewski-Szmek" <zbyszek@in.waw.pl>,
"Lennart Poettering" <mzxreary@0pointer.de>,
"Daan De Meyer" <daan.j.demeyer@gmail.com>,
"Aleksa Sarai" <cyphar@cyphar.com>,
"Alexander Viro" <viro@zeniv.linux.org.uk>,
"Jan Kara" <jack@suse.cz>, "Tejun Heo" <tj@kernel.org>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Michal Koutný" <mkoutny@suse.com>,
"Jakub Kicinski" <kuba@kernel.org>,
"Anna-Maria Behnsen" <anna-maria@linutronix.de>,
"Frederic Weisbecker" <frederic@kernel.org>,
"Thomas Gleixner" <tglx@linutronix.de>,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
netdev@vger.kernel.org, "Christian Brauner" <brauner@kernel.org>
Subject: [PATCH 8/9] nscommon: simplify initialization
Date: Wed, 17 Sep 2025 12:28:07 +0200 [thread overview]
Message-ID: <20250917-work-namespace-ns_common-v1-8-1b3bda8ef8f2@kernel.org> (raw)
In-Reply-To: <20250917-work-namespace-ns_common-v1-0-1b3bda8ef8f2@kernel.org>
There's a lot of information that namespace implementers don't need to
know about at all. Encapsulate this all in the initialization helper.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/namespace.c | 5 +++--
include/linux/ns_common.h | 41 +++++++++++++++++++++++++++++++++++++++--
ipc/namespace.c | 2 +-
kernel/cgroup/namespace.c | 2 +-
kernel/nscommon.c | 17 ++++++++---------
kernel/pid_namespace.c | 2 +-
kernel/time/namespace.c | 2 +-
kernel/user_namespace.c | 2 +-
kernel/utsname.c | 2 +-
net/core/net_namespace.c | 2 +-
10 files changed, 57 insertions(+), 20 deletions(-)
diff --git a/fs/namespace.c b/fs/namespace.c
index 09e4ecd44972..31eb0e8f21eb 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4105,8 +4105,9 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
}
if (anon)
- new_ns->ns.inum = MNT_NS_ANON_INO;
- ret = ns_common_init(&new_ns->ns, &mntns_operations, !anon);
+ ret = ns_common_init_inum(new_ns, &mntns_operations, MNT_NS_ANON_INO);
+ else
+ ret = ns_common_init(new_ns, &mntns_operations);
if (ret) {
kfree(new_ns);
dec_mnt_namespaces(ucounts);
diff --git a/include/linux/ns_common.h b/include/linux/ns_common.h
index 78b17fe80b62..284bba2b7c43 100644
--- a/include/linux/ns_common.h
+++ b/include/linux/ns_common.h
@@ -16,6 +16,15 @@ struct time_namespace;
struct user_namespace;
struct uts_namespace;
+extern struct cgroup_namespace init_cgroup_ns;
+extern struct ipc_namespace init_ipc_ns;
+extern struct mnt_namespace *init_mnt_ns;
+extern struct net init_net;
+extern struct pid_namespace init_pid_ns;
+extern struct time_namespace init_time_ns;
+extern struct user_namespace init_user_ns;
+extern struct uts_namespace init_uts_ns;
+
struct ns_common {
struct dentry *stashed;
const struct proc_ns_operations *ops;
@@ -31,8 +40,7 @@ struct ns_common {
};
};
-int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
- bool alloc_inum);
+int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum);
#define to_ns_common(__ns) \
_Generic((__ns), \
@@ -45,4 +53,33 @@ int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
struct user_namespace *: &(__ns)->ns, \
struct uts_namespace *: &(__ns)->ns)
+#define ns_init_inum(__ns) \
+ _Generic((__ns), \
+ struct cgroup_namespace *: CGROUP_NS_INIT_INO, \
+ struct ipc_namespace *: IPC_NS_INIT_INO, \
+ struct mnt_namespace *: MNT_NS_INIT_INO, \
+ struct net *: NET_NS_INIT_INO, \
+ struct pid_namespace *: PID_NS_INIT_INO, \
+ struct time_namespace *: TIME_NS_INIT_INO, \
+ struct user_namespace *: USER_NS_INIT_INO, \
+ struct uts_namespace *: UTS_NS_INIT_INO)
+
+#define ns_init_ns(__ns) \
+ _Generic((__ns), \
+ struct cgroup_namespace *: &init_cgroup_ns, \
+ struct ipc_namespace *: &init_ipc_ns, \
+ struct mnt_namespace *: init_mnt_ns, \
+ struct net *: &init_net, \
+ struct pid_namespace *: &init_pid_ns, \
+ struct time_namespace *: &init_time_ns, \
+ struct user_namespace *: &init_user_ns, \
+ struct uts_namespace *: &init_uts_ns)
+
+#define ns_common_init(__ns, __ops) \
+ __ns_common_init(&(__ns)->ns, __ops, \
+ (((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0))
+
+#define ns_common_init_inum(__ns, __ops, __inum) \
+ __ns_common_init(&(__ns)->ns, __ops, __inum)
+
#endif
diff --git a/ipc/namespace.c b/ipc/namespace.c
index 89588819956b..0f8bbd18a475 100644
--- a/ipc/namespace.c
+++ b/ipc/namespace.c
@@ -62,7 +62,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
if (ns == NULL)
goto fail_dec;
- err = ns_common_init(&ns->ns, &ipcns_operations, true);
+ err = ns_common_init(ns, &ipcns_operations);
if (err)
goto fail_free;
diff --git a/kernel/cgroup/namespace.c b/kernel/cgroup/namespace.c
index 5a327914b565..d928c557e28b 100644
--- a/kernel/cgroup/namespace.c
+++ b/kernel/cgroup/namespace.c
@@ -27,7 +27,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
if (!new_ns)
return ERR_PTR(-ENOMEM);
- ret = ns_common_init(&new_ns->ns, &cgroupns_operations, true);
+ ret = ns_common_init(new_ns, &cgroupns_operations);
if (ret)
return ERR_PTR(ret);
ns_tree_add(new_ns);
diff --git a/kernel/nscommon.c b/kernel/nscommon.c
index e10fad8afe61..c3a90bb665ad 100644
--- a/kernel/nscommon.c
+++ b/kernel/nscommon.c
@@ -1,21 +1,20 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/ns_common.h>
+#include <linux/proc_ns.h>
-int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
- bool alloc_inum)
+int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum)
{
- if (alloc_inum && !ns->inum) {
- int ret;
- ret = proc_alloc_inum(&ns->inum);
- if (ret)
- return ret;
- }
refcount_set(&ns->count, 1);
ns->stashed = NULL;
ns->ops = ops;
ns->ns_id = 0;
RB_CLEAR_NODE(&ns->ns_tree_node);
INIT_LIST_HEAD(&ns->ns_list_node);
- return 0;
+
+ if (inum) {
+ ns->inum = inum;
+ return 0;
+ }
+ return proc_alloc_inum(&ns->inum);
}
diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c
index 9b327420309e..170757c265c2 100644
--- a/kernel/pid_namespace.c
+++ b/kernel/pid_namespace.c
@@ -103,7 +103,7 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
if (ns->pid_cachep == NULL)
goto out_free_idr;
- err = ns_common_init(&ns->ns, &pidns_operations, true);
+ err = ns_common_init(ns, &pidns_operations);
if (err)
goto out_free_idr;
diff --git a/kernel/time/namespace.c b/kernel/time/namespace.c
index 20b65f90549e..ce8e952104a7 100644
--- a/kernel/time/namespace.c
+++ b/kernel/time/namespace.c
@@ -97,7 +97,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
if (!ns->vvar_page)
goto fail_free;
- err = ns_common_init(&ns->ns, &timens_operations, true);
+ err = ns_common_init(ns, &timens_operations);
if (err)
goto fail_free_page;
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index cfb0e28f2779..db9f0463219c 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -126,7 +126,7 @@ int create_user_ns(struct cred *new)
ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);
- ret = ns_common_init(&ns->ns, &userns_operations, true);
+ ret = ns_common_init(ns, &userns_operations);
if (ret)
goto fail_free;
diff --git a/kernel/utsname.c b/kernel/utsname.c
index a682830742d3..399888be66bd 100644
--- a/kernel/utsname.c
+++ b/kernel/utsname.c
@@ -50,7 +50,7 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
if (!ns)
goto fail_dec;
- err = ns_common_init(&ns->ns, &utsns_operations, true);
+ err = ns_common_init(ns, &utsns_operations);
if (err)
goto fail_free;
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index 897f4927df9e..fdb266bbdf93 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -409,7 +409,7 @@ static __net_init int preinit_net(struct net *net, struct user_namespace *user_n
ns_ops = NULL;
#endif
- ret = ns_common_init(&net->ns, ns_ops, true);
+ ret = ns_common_init(net, ns_ops);
if (ret)
return ret;
--
2.47.3
next prev parent reply other threads:[~2025-09-17 10:29 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-17 10:27 [PATCH 0/9] ns: rework common initialization Christian Brauner
2025-09-17 10:28 ` [PATCH 1/9] uts: split namespace into separate header Christian Brauner
2025-09-17 16:28 ` Jan Kara
2025-09-17 10:28 ` [PATCH 2/9] mnt: expose pointer to init_mnt_ns Christian Brauner
2025-09-17 16:28 ` Jan Kara
2025-09-19 10:05 ` Christian Brauner
2025-09-22 10:19 ` Jan Kara
2025-09-23 10:44 ` Christian Brauner
2025-09-17 10:28 ` [PATCH 3/9] nscommon: move to separate file Christian Brauner
2025-09-17 16:30 ` Jan Kara
2025-09-17 10:28 ` [PATCH 4/9] cgroup: split namespace into separate header Christian Brauner
2025-09-17 16:30 ` Tejun Heo
2025-09-17 16:30 ` Jan Kara
2025-09-17 10:28 ` [PATCH 5/9] nsfs: add inode number for anon namespace Christian Brauner
2025-09-17 16:31 ` Jan Kara
2025-09-17 10:28 ` [PATCH 6/9] mnt: simplify ns_common_init() handling Christian Brauner
2025-09-17 16:45 ` Jan Kara
2025-09-18 8:15 ` Christian Brauner
2025-09-18 9:12 ` Jan Kara
2025-09-17 10:28 ` [PATCH 7/9] net: centralize ns_common initialization Christian Brauner
2025-09-18 9:42 ` Jan Kara
2025-09-19 8:08 ` Christian Brauner
2025-09-22 10:19 ` Jan Kara
2025-09-17 10:28 ` Christian Brauner [this message]
2025-09-18 9:45 ` [PATCH 8/9] nscommon: simplify initialization Jan Kara
2025-09-17 10:28 ` [PATCH 9/9] ns: add ns_common_free() Christian Brauner
2025-09-18 9:11 ` Jan Kara
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=20250917-work-namespace-ns_common-v1-8-1b3bda8ef8f2@kernel.org \
--to=brauner@kernel.org \
--cc=amir73il@gmail.com \
--cc=anna-maria@linutronix.de \
--cc=cgroups@vger.kernel.org \
--cc=cyphar@cyphar.com \
--cc=daan.j.demeyer@gmail.com \
--cc=frederic@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=jack@suse.cz \
--cc=jlayton@kernel.org \
--cc=josef@toxicpanda.com \
--cc=kuba@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=me@yhndnzj.com \
--cc=mkoutny@suse.com \
--cc=mzxreary@0pointer.de \
--cc=netdev@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
--cc=viro@zeniv.linux.org.uk \
--cc=zbyszek@in.waw.pl \
/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;
as well as URLs for NNTP newsgroup(s).