mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	nscommon: simplify initialization
There's a lot of information that namespace implementers don't need to know about at all. Encapsulate this all in the initialization helper. Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
		
							parent
							
								
									d5b27cb8c5
								
							
						
					
					
						commit
						5612ff3ec5
					
				
					 10 changed files with 55 additions and 20 deletions
				
			
		| 
						 | 
					@ -4104,8 +4104,9 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (anon)
 | 
						if (anon)
 | 
				
			||||||
		new_ns->ns.inum = MNT_NS_ANON_INO;
 | 
							ret = ns_common_init_inum(new_ns, &mntns_operations, MNT_NS_ANON_INO);
 | 
				
			||||||
	ret = ns_common_init(&new_ns->ns, &mntns_operations, !anon);
 | 
						else
 | 
				
			||||||
 | 
							ret = ns_common_init(new_ns, &mntns_operations);
 | 
				
			||||||
	if (ret) {
 | 
						if (ret) {
 | 
				
			||||||
		kfree(new_ns);
 | 
							kfree(new_ns);
 | 
				
			||||||
		dec_mnt_namespaces(ucounts);
 | 
							dec_mnt_namespaces(ucounts);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,6 +16,15 @@ struct time_namespace;
 | 
				
			||||||
struct user_namespace;
 | 
					struct user_namespace;
 | 
				
			||||||
struct uts_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 ns_common {
 | 
				
			||||||
	struct dentry *stashed;
 | 
						struct dentry *stashed;
 | 
				
			||||||
	const struct proc_ns_operations *ops;
 | 
						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,
 | 
					int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum);
 | 
				
			||||||
		   bool alloc_inum);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define to_ns_common(__ns)                              \
 | 
					#define to_ns_common(__ns)                              \
 | 
				
			||||||
	_Generic((__ns),                                \
 | 
						_Generic((__ns),                                \
 | 
				
			||||||
| 
						 | 
					@ -45,4 +53,31 @@ int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
 | 
				
			||||||
		struct user_namespace *:   &(__ns)->ns, \
 | 
							struct user_namespace *:   &(__ns)->ns, \
 | 
				
			||||||
		struct uts_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(to_ns_common(__ns), __ops, (((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define ns_common_init_inum(__ns, __ops, __inum) __ns_common_init(to_ns_common(__ns), __ops, __inum)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -62,7 +62,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 | 
				
			||||||
	if (ns == NULL)
 | 
						if (ns == NULL)
 | 
				
			||||||
		goto fail_dec;
 | 
							goto fail_dec;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = ns_common_init(&ns->ns, &ipcns_operations, true);
 | 
						err = ns_common_init(ns, &ipcns_operations);
 | 
				
			||||||
	if (err)
 | 
						if (err)
 | 
				
			||||||
		goto fail_free;
 | 
							goto fail_free;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,7 +27,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
 | 
				
			||||||
	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
 | 
						new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
 | 
				
			||||||
	if (!new_ns)
 | 
						if (!new_ns)
 | 
				
			||||||
		return ERR_PTR(-ENOMEM);
 | 
							return ERR_PTR(-ENOMEM);
 | 
				
			||||||
	ret = ns_common_init(&new_ns->ns, &cgroupns_operations, true);
 | 
						ret = ns_common_init(new_ns, &cgroupns_operations);
 | 
				
			||||||
	if (ret)
 | 
						if (ret)
 | 
				
			||||||
		return ERR_PTR(ret);
 | 
							return ERR_PTR(ret);
 | 
				
			||||||
	ns_tree_add(new_ns);
 | 
						ns_tree_add(new_ns);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,21 +1,20 @@
 | 
				
			||||||
// SPDX-License-Identifier: GPL-2.0-only
 | 
					// SPDX-License-Identifier: GPL-2.0-only
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <linux/ns_common.h>
 | 
					#include <linux/ns_common.h>
 | 
				
			||||||
 | 
					#include <linux/proc_ns.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
 | 
					int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum)
 | 
				
			||||||
		   bool alloc_inum)
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (alloc_inum && !ns->inum) {
 | 
					 | 
				
			||||||
		int ret;
 | 
					 | 
				
			||||||
		ret = proc_alloc_inum(&ns->inum);
 | 
					 | 
				
			||||||
		if (ret)
 | 
					 | 
				
			||||||
			return ret;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	refcount_set(&ns->count, 1);
 | 
						refcount_set(&ns->count, 1);
 | 
				
			||||||
	ns->stashed = NULL;
 | 
						ns->stashed = NULL;
 | 
				
			||||||
	ns->ops = ops;
 | 
						ns->ops = ops;
 | 
				
			||||||
	ns->ns_id = 0;
 | 
						ns->ns_id = 0;
 | 
				
			||||||
	RB_CLEAR_NODE(&ns->ns_tree_node);
 | 
						RB_CLEAR_NODE(&ns->ns_tree_node);
 | 
				
			||||||
	INIT_LIST_HEAD(&ns->ns_list_node);
 | 
						INIT_LIST_HEAD(&ns->ns_list_node);
 | 
				
			||||||
	return 0;
 | 
					
 | 
				
			||||||
 | 
						if (inum) {
 | 
				
			||||||
 | 
							ns->inum = inum;
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return proc_alloc_inum(&ns->inum);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -103,7 +103,7 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
 | 
				
			||||||
	if (ns->pid_cachep == NULL)
 | 
						if (ns->pid_cachep == NULL)
 | 
				
			||||||
		goto out_free_idr;
 | 
							goto out_free_idr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = ns_common_init(&ns->ns, &pidns_operations, true);
 | 
						err = ns_common_init(ns, &pidns_operations);
 | 
				
			||||||
	if (err)
 | 
						if (err)
 | 
				
			||||||
		goto out_free_idr;
 | 
							goto out_free_idr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -97,7 +97,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 | 
				
			||||||
	if (!ns->vvar_page)
 | 
						if (!ns->vvar_page)
 | 
				
			||||||
		goto fail_free;
 | 
							goto fail_free;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = ns_common_init(&ns->ns, &timens_operations, true);
 | 
						err = ns_common_init(ns, &timens_operations);
 | 
				
			||||||
	if (err)
 | 
						if (err)
 | 
				
			||||||
		goto fail_free_page;
 | 
							goto fail_free_page;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -126,7 +126,7 @@ int create_user_ns(struct cred *new)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);
 | 
						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)
 | 
						if (ret)
 | 
				
			||||||
		goto fail_free;
 | 
							goto fail_free;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -50,7 +50,7 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
 | 
				
			||||||
	if (!ns)
 | 
						if (!ns)
 | 
				
			||||||
		goto fail_dec;
 | 
							goto fail_dec;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = ns_common_init(&ns->ns, &utsns_operations, true);
 | 
						err = ns_common_init(ns, &utsns_operations);
 | 
				
			||||||
	if (err)
 | 
						if (err)
 | 
				
			||||||
		goto fail_free;
 | 
							goto fail_free;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -409,7 +409,7 @@ static __net_init int preinit_net(struct net *net, struct user_namespace *user_n
 | 
				
			||||||
	ns_ops = NULL;
 | 
						ns_ops = NULL;
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ret = ns_common_init(&net->ns, ns_ops, true);
 | 
						ret = ns_common_init(net, ns_ops);
 | 
				
			||||||
	if (ret)
 | 
						if (ret)
 | 
				
			||||||
		return ret;
 | 
							return ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue