[PATCH 8/9] nscommon: simplify initialization

Christian Brauner posted 9 patches 2 weeks, 1 day ago
[PATCH 8/9] nscommon: simplify initialization
Posted by Christian Brauner 2 weeks, 1 day ago
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
Re: [PATCH 8/9] nscommon: simplify initialization
Posted by Jan Kara 2 weeks ago
On Wed 17-09-25 12:28:07, Christian Brauner wrote:
> 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>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  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
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR