Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ns: move ns type into struct ns_common

It's misplaced in struct proc_ns_operations and ns->ops might be NULL if
the namespace is compiled out but we still want to know the type of the
namespace for the initial namespace struct.

Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Christian Brauner <brauner@kernel.org>

+51 -34
+3 -3
fs/namespace.c
··· 4927 4927 return -EINVAL; 4928 4928 4929 4929 ns = get_proc_ns(file_inode(fd_file(f))); 4930 - if (ns->ops->type != CLONE_NEWUSER) 4930 + if (ns->ns_type != CLONE_NEWUSER) 4931 4931 return -EINVAL; 4932 4932 4933 4933 /* ··· 5830 5830 return ERR_PTR(-EINVAL); 5831 5831 5832 5832 ns = get_proc_ns(file_inode(fd_file(f))); 5833 - if (ns->ops->type != CLONE_NEWNS) 5833 + if (ns->ns_type != CLONE_NEWNS) 5834 5834 return ERR_PTR(-EINVAL); 5835 5835 5836 5836 mnt_ns = to_mnt_ns(ns); ··· 6016 6016 .ns.ops = &mntns_operations, 6017 6017 .user_ns = &init_user_ns, 6018 6018 .ns.__ns_ref = REFCOUNT_INIT(1), 6019 + .ns.ns_type = ns_common_type(&init_mnt_ns), 6019 6020 .passive = REFCOUNT_INIT(1), 6020 6021 .mounts = RB_ROOT, 6021 6022 .poll = __WAIT_QUEUE_HEAD_INITIALIZER(init_mnt_ns.poll), ··· 6334 6333 6335 6334 const struct proc_ns_operations mntns_operations = { 6336 6335 .name = "mnt", 6337 - .type = CLONE_NEWNS, 6338 6336 .get = mntns_get, 6339 6337 .put = mntns_put, 6340 6338 .install = mntns_install,
+9 -9
fs/nsfs.c
··· 219 219 return -EINVAL; 220 220 return open_related_ns(ns, ns->ops->get_parent); 221 221 case NS_GET_NSTYPE: 222 - return ns->ops->type; 222 + return ns->ns_type; 223 223 case NS_GET_OWNER_UID: 224 - if (ns->ops->type != CLONE_NEWUSER) 224 + if (ns->ns_type != CLONE_NEWUSER) 225 225 return -EINVAL; 226 226 user_ns = container_of(ns, struct user_namespace, ns); 227 227 argp = (uid_t __user *) arg; ··· 234 234 case NS_GET_PID_IN_PIDNS: 235 235 fallthrough; 236 236 case NS_GET_TGID_IN_PIDNS: { 237 - if (ns->ops->type != CLONE_NEWPID) 237 + if (ns->ns_type != CLONE_NEWPID) 238 238 return -EINVAL; 239 239 240 240 ret = -ESRCH; ··· 273 273 return ret; 274 274 } 275 275 case NS_GET_MNTNS_ID: 276 - if (ns->ops->type != CLONE_NEWNS) 276 + if (ns->ns_type != CLONE_NEWNS) 277 277 return -EINVAL; 278 278 fallthrough; 279 279 case NS_GET_ID: { ··· 293 293 struct mnt_ns_info __user *uinfo = (struct mnt_ns_info __user *)arg; 294 294 size_t usize = _IOC_SIZE(ioctl); 295 295 296 - if (ns->ops->type != CLONE_NEWNS) 296 + if (ns->ns_type != CLONE_NEWNS) 297 297 return -EINVAL; 298 298 299 299 if (!uinfo) ··· 314 314 struct file *f __free(fput) = NULL; 315 315 size_t usize = _IOC_SIZE(ioctl); 316 316 317 - if (ns->ops->type != CLONE_NEWNS) 317 + if (ns->ns_type != CLONE_NEWNS) 318 318 return -EINVAL; 319 319 320 320 if (usize < MNT_NS_INFO_SIZE_VER0) ··· 453 453 } 454 454 455 455 fid->ns_id = ns->ns_id; 456 - fid->ns_type = ns->ops->type; 456 + fid->ns_type = ns->ns_type; 457 457 fid->ns_inum = inode->i_ino; 458 458 return FILEID_NSFS; 459 459 } ··· 489 489 return NULL; 490 490 491 491 VFS_WARN_ON_ONCE(ns->ns_id != fid->ns_id); 492 - VFS_WARN_ON_ONCE(ns->ops->type != fid->ns_type); 492 + VFS_WARN_ON_ONCE(ns->ns_type != fid->ns_type); 493 493 VFS_WARN_ON_ONCE(ns->inum != fid->ns_inum); 494 494 495 495 if (!__ns_ref_get(ns)) 496 496 return NULL; 497 497 } 498 498 499 - switch (ns->ops->type) { 499 + switch (ns->ns_type) { 500 500 #ifdef CONFIG_CGROUPS 501 501 case CLONE_NEWCGROUP: 502 502 if (!current_in_namespace(to_cg_ns(ns)))
+24 -4
include/linux/ns_common.h
··· 4 4 5 5 #include <linux/refcount.h> 6 6 #include <linux/rbtree.h> 7 + #include <uapi/linux/sched.h> 7 8 8 9 struct proc_ns_operations; 9 10 ··· 38 37 extern const struct proc_ns_operations timens_for_children_operations; 39 38 40 39 struct ns_common { 40 + u32 ns_type; 41 41 struct dentry *stashed; 42 42 const struct proc_ns_operations *ops; 43 43 unsigned int inum; ··· 53 51 }; 54 52 }; 55 53 56 - int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum); 54 + int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct proc_ns_operations *ops, int inum); 57 55 void __ns_common_free(struct ns_common *ns); 58 56 59 57 #define to_ns_common(__ns) \ ··· 108 106 struct user_namespace *: (IS_ENABLED(CONFIG_USER_NS) ? &userns_operations : NULL), \ 109 107 struct uts_namespace *: (IS_ENABLED(CONFIG_UTS_NS) ? &utsns_operations : NULL)) 110 108 111 - #define ns_common_init(__ns) \ 112 - __ns_common_init(to_ns_common(__ns), to_ns_operations(__ns), (((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0)) 109 + #define ns_common_type(__ns) \ 110 + _Generic((__ns), \ 111 + struct cgroup_namespace *: CLONE_NEWCGROUP, \ 112 + struct ipc_namespace *: CLONE_NEWIPC, \ 113 + struct mnt_namespace *: CLONE_NEWNS, \ 114 + struct net *: CLONE_NEWNET, \ 115 + struct pid_namespace *: CLONE_NEWPID, \ 116 + struct time_namespace *: CLONE_NEWTIME, \ 117 + struct user_namespace *: CLONE_NEWUSER, \ 118 + struct uts_namespace *: CLONE_NEWUTS) 113 119 114 - #define ns_common_init_inum(__ns, __inum) __ns_common_init(to_ns_common(__ns), to_ns_operations(__ns), __inum) 120 + #define ns_common_init(__ns) \ 121 + __ns_common_init(to_ns_common(__ns), \ 122 + ns_common_type(__ns), \ 123 + to_ns_operations(__ns), \ 124 + (((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0)) 125 + 126 + #define ns_common_init_inum(__ns, __inum) \ 127 + __ns_common_init(to_ns_common(__ns), \ 128 + ns_common_type(__ns), \ 129 + to_ns_operations(__ns), \ 130 + __inum) 115 131 116 132 #define ns_common_free(__ns) __ns_common_free(to_ns_common((__ns))) 117 133
-1
include/linux/proc_ns.h
··· 17 17 struct proc_ns_operations { 18 18 const char *name; 19 19 const char *real_ns_name; 20 - int type; 21 20 struct ns_common *(*get)(struct task_struct *task); 22 21 void (*put)(struct ns_common *ns); 23 22 int (*install)(struct nsset *nsset, struct ns_common *ns);
+1
init/version-timestamp.c
··· 8 8 #include <linux/utsname.h> 9 9 10 10 struct uts_namespace init_uts_ns = { 11 + .ns.ns_type = ns_common_type(&init_uts_ns), 11 12 .ns.__ns_ref = REFCOUNT_INIT(2), 12 13 .name = { 13 14 .sysname = UTS_SYSNAME,
+1
ipc/msgutil.c
··· 33 33 #ifdef CONFIG_IPC_NS 34 34 .ns.ops = &ipcns_operations, 35 35 #endif 36 + .ns.ns_type = ns_common_type(&init_ipc_ns), 36 37 }; 37 38 38 39 struct msg_msgseg {
-1
ipc/namespace.c
··· 248 248 249 249 const struct proc_ns_operations ipcns_operations = { 250 250 .name = "ipc", 251 - .type = CLONE_NEWIPC, 252 251 .get = ipcns_get, 253 252 .put = ipcns_put, 254 253 .install = ipcns_install,
+1
kernel/cgroup/cgroup.c
··· 224 224 .ns.ops = &cgroupns_operations, 225 225 .ns.inum = ns_init_inum(&init_cgroup_ns), 226 226 .root_cset = &init_css_set, 227 + .ns.ns_type = ns_common_type(&init_cgroup_ns), 227 228 }; 228 229 229 230 static struct file_system_type cgroup2_fs_type;
-1
kernel/cgroup/namespace.c
··· 137 137 138 138 const struct proc_ns_operations cgroupns_operations = { 139 139 .name = "cgroup", 140 - .type = CLONE_NEWCGROUP, 141 140 .get = cgroupns_get, 142 141 .put = cgroupns_put, 143 142 .install = cgroupns_install,
+3 -2
kernel/nscommon.c
··· 7 7 #ifdef CONFIG_DEBUG_VFS 8 8 static void ns_debug(struct ns_common *ns, const struct proc_ns_operations *ops) 9 9 { 10 - switch (ns->ops->type) { 10 + switch (ns->ns_type) { 11 11 #ifdef CONFIG_CGROUPS 12 12 case CLONE_NEWCGROUP: 13 13 VFS_WARN_ON_ONCE(ops != &cgroupns_operations); ··· 52 52 } 53 53 #endif 54 54 55 - int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum) 55 + int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct proc_ns_operations *ops, int inum) 56 56 { 57 57 refcount_set(&ns->__ns_ref, 1); 58 58 ns->stashed = NULL; 59 59 ns->ops = ops; 60 60 ns->ns_id = 0; 61 + ns->ns_type = ns_type; 61 62 RB_CLEAR_NODE(&ns->ns_tree_node); 62 63 INIT_LIST_HEAD(&ns->ns_list_node); 63 64
+2 -2
kernel/nsproxy.c
··· 545 545 546 546 if (proc_ns_file(fd_file(f))) { 547 547 ns = get_proc_ns(file_inode(fd_file(f))); 548 - if (flags && (ns->ops->type != flags)) 548 + if (flags && (ns->ns_type != flags)) 549 549 err = -EINVAL; 550 - flags = ns->ops->type; 550 + flags = ns->ns_type; 551 551 } else if (!IS_ERR(pidfd_pid(fd_file(f)))) { 552 552 err = check_setns_flags(flags); 553 553 } else {
+4 -4
kernel/nstree.c
··· 106 106 107 107 write_seqlock(&ns_tree->ns_tree_lock); 108 108 109 - VFS_WARN_ON_ONCE(ns->ops->type != ns_tree->type); 109 + VFS_WARN_ON_ONCE(ns->ns_type != ns_tree->type); 110 110 111 111 node = rb_find_add_rcu(&ns->ns_tree_node, &ns_tree->ns_tree, ns_cmp); 112 112 /* ··· 128 128 { 129 129 VFS_WARN_ON_ONCE(RB_EMPTY_NODE(&ns->ns_tree_node)); 130 130 VFS_WARN_ON_ONCE(list_empty(&ns->ns_list_node)); 131 - VFS_WARN_ON_ONCE(ns->ops->type != ns_tree->type); 131 + VFS_WARN_ON_ONCE(ns->ns_type != ns_tree->type); 132 132 133 133 write_seqlock(&ns_tree->ns_tree_lock); 134 134 rb_erase(&ns->ns_tree_node, &ns_tree->ns_tree); ··· 197 197 if (!node) 198 198 return NULL; 199 199 200 - VFS_WARN_ON_ONCE(node_to_ns(node)->ops->type != ns_type); 200 + VFS_WARN_ON_ONCE(node_to_ns(node)->ns_type != ns_type); 201 201 202 202 return node_to_ns(node); 203 203 } ··· 225 225 if (list_is_head(list, &ns_tree->ns_list)) 226 226 return ERR_PTR(-ENOENT); 227 227 228 - VFS_WARN_ON_ONCE(list_entry_rcu(list, struct ns_common, ns_list_node)->ops->type != ns_tree->type); 228 + VFS_WARN_ON_ONCE(list_entry_rcu(list, struct ns_common, ns_list_node)->ns_type != ns_tree->type); 229 229 230 230 return list_entry_rcu(list, struct ns_common, ns_list_node); 231 231 }
+1
kernel/pid.c
··· 85 85 #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) 86 86 .memfd_noexec_scope = MEMFD_NOEXEC_SCOPE_EXEC, 87 87 #endif 88 + .ns.ns_type = ns_common_type(&init_pid_ns), 88 89 }; 89 90 EXPORT_SYMBOL_GPL(init_pid_ns); 90 91
-2
kernel/pid_namespace.c
··· 443 443 444 444 const struct proc_ns_operations pidns_operations = { 445 445 .name = "pid", 446 - .type = CLONE_NEWPID, 447 446 .get = pidns_get, 448 447 .put = pidns_put, 449 448 .install = pidns_install, ··· 453 454 const struct proc_ns_operations pidns_for_children_operations = { 454 455 .name = "pid_for_children", 455 456 .real_ns_name = "pid", 456 - .type = CLONE_NEWPID, 457 457 .get = pidns_for_children_get, 458 458 .put = pidns_put, 459 459 .install = pidns_install,
+1 -2
kernel/time/namespace.c
··· 462 462 463 463 const struct proc_ns_operations timens_operations = { 464 464 .name = "time", 465 - .type = CLONE_NEWTIME, 466 465 .get = timens_get, 467 466 .put = timens_put, 468 467 .install = timens_install, ··· 471 472 const struct proc_ns_operations timens_for_children_operations = { 472 473 .name = "time_for_children", 473 474 .real_ns_name = "time", 474 - .type = CLONE_NEWTIME, 475 475 .get = timens_for_children_get, 476 476 .put = timens_put, 477 477 .install = timens_install, ··· 478 480 }; 479 481 480 482 struct time_namespace init_time_ns = { 483 + .ns.ns_type = ns_common_type(&init_time_ns), 481 484 .ns.__ns_ref = REFCOUNT_INIT(3), 482 485 .user_ns = &init_user_ns, 483 486 .ns.inum = ns_init_inum(&init_time_ns),
+1
kernel/user.c
··· 65 65 .nr_extents = 1, 66 66 }, 67 67 }, 68 + .ns.ns_type = ns_common_type(&init_user_ns), 68 69 .ns.__ns_ref = REFCOUNT_INIT(3), 69 70 .owner = GLOBAL_ROOT_UID, 70 71 .group = GLOBAL_ROOT_GID,
-1
kernel/user_namespace.c
··· 1400 1400 1401 1401 const struct proc_ns_operations userns_operations = { 1402 1402 .name = "user", 1403 - .type = CLONE_NEWUSER, 1404 1403 .get = userns_get, 1405 1404 .put = userns_put, 1406 1405 .install = userns_install,
-1
kernel/utsname.c
··· 146 146 147 147 const struct proc_ns_operations utsns_operations = { 148 148 .name = "uts", 149 - .type = CLONE_NEWUTS, 150 149 .get = utsns_get, 151 150 .put = utsns_put, 152 151 .install = utsns_install,
-1
net/core/net_namespace.c
··· 1543 1543 1544 1544 const struct proc_ns_operations netns_operations = { 1545 1545 .name = "net", 1546 - .type = CLONE_NEWNET, 1547 1546 .get = netns_get, 1548 1547 .put = netns_put, 1549 1548 .install = netns_install,