Pull namespace update from Christian Brauner: "Add two simple helper macros for the namespace infrastructure" * tag 'namespaces-7.1-rc1.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: nsproxy: Add FOR_EACH_NS_TYPE() X-macro and CLONE_NS_ALL
Unified · Split
| @@ -7,6 +7,7 @@ · 6 unmodified lines | |||
| 7 | #include <linux/rbtree.h> | 7 | #include <linux/rbtree.h> |
| 8 | #include <linux/refcount.h> | 8 | #include <linux/refcount.h> |
| 9 | #include <linux/types.h> | 9 | #include <linux/types.h> |
| 10 | #include <uapi/linux/sched.h> | ||
| 10 | 11 | ||
| 11 | struct cgroup_namespace; | 12 | struct cgroup_namespace; |
| 12 | struct dentry; | 13 | struct dentry; |
| @@ -184,15 +185,38 @@ struct ns_common { · 171 unmodified lines | |||
| 184 | struct user_namespace *: (IS_ENABLED(CONFIG_USER_NS) ? &userns_operations : NULL), \ | 185 | struct user_namespace *: (IS_ENABLED(CONFIG_USER_NS) ? &userns_operations : NULL), \ |
| 185 | struct uts_namespace *: (IS_ENABLED(CONFIG_UTS_NS) ? &utsns_operations : NULL)) | 186 | struct uts_namespace *: (IS_ENABLED(CONFIG_UTS_NS) ? &utsns_operations : NULL)) |
| 186 | 187 | ||
| 187 | #define ns_common_type(__ns) \ | 188 | /* |
| 188 | _Generic((__ns), \ | 189 | * FOR_EACH_NS_TYPE - Canonical list of namespace types |
| 189 | struct cgroup_namespace *: CLONE_NEWCGROUP, \ | 190 | * |
| 190 | struct ipc_namespace *: CLONE_NEWIPC, \ | 191 | * Enumerates all (struct type, CLONE_NEW* flag) pairs. This is the |
| 191 | struct mnt_namespace *: CLONE_NEWNS, \ | 192 | * single source of truth used to derive ns_common_type() and |
| 192 | struct net *: CLONE_NEWNET, \ | 193 | * CLONE_NS_ALL. When adding a new namespace type, add a single entry |
| 193 | struct pid_namespace *: CLONE_NEWPID, \ | 194 | * here; all consumers update automatically. |
| 194 | struct time_namespace *: CLONE_NEWTIME, \ | 195 | * |
| 195 | struct user_namespace *: CLONE_NEWUSER, \ | 196 | * @X: Callback macro taking (struct_name, clone_flag) as arguments. |
| 196 | struct uts_namespace *: CLONE_NEWUTS) | 197 | */ |
| 198 | #define FOR_EACH_NS_TYPE(X) \ | ||
| 199 | X(cgroup_namespace, CLONE_NEWCGROUP) \ | ||
| 200 | X(ipc_namespace, CLONE_NEWIPC) \ | ||
| 201 | X(mnt_namespace, CLONE_NEWNS) \ | ||
| 202 | X(net, CLONE_NEWNET) \ | ||
| 203 | X(pid_namespace, CLONE_NEWPID) \ | ||
| 204 | X(time_namespace, CLONE_NEWTIME) \ | ||
| 205 | X(user_namespace, CLONE_NEWUSER) \ | ||
| 206 | X(uts_namespace, CLONE_NEWUTS) | ||
| 207 | |||
| 208 | /* Bitmask of all known CLONE_NEW* flags. */ | ||
| 209 | #define _NS_TYPE_FLAG_OR(struct_name, flag) | (flag) | ||
| 210 | #define CLONE_NS_ALL (0 FOR_EACH_NS_TYPE(_NS_TYPE_FLAG_OR)) | ||
| 211 | |||
| 212 | /* | ||
| 213 | * ns_common_type - Map a namespace struct pointer to its CLONE_NEW* flag | ||
| 214 | * | ||
| 215 | * Uses a leading-comma pattern so the FOR_EACH_NS_TYPE expansion | ||
| 216 | * produces ", struct foo *: FLAG" entries without a trailing comma. | ||
| 217 | */ | ||
| 218 | #define _NS_TYPE_ASSOC(struct_name, flag) , struct struct_name *: (flag) | ||
| 219 | |||
| 220 | #define ns_common_type(__ns) _Generic((__ns)FOR_EACH_NS_TYPE(_NS_TYPE_ASSOC)) | ||
| 197 | 221 | ||
| 198 | #endif /* _LINUX_NS_COMMON_TYPES_H */ | 222 | #endif /* _LINUX_NS_COMMON_TYPES_H */ |
| @@ -46,6 +46,7 @@ · 45 unmodified lines | |||
| 46 | #include <linux/mm_inline.h> | 46 | #include <linux/mm_inline.h> |
| 47 | #include <linux/memblock.h> | 47 | #include <linux/memblock.h> |
| 48 | #include <linux/nsproxy.h> | 48 | #include <linux/nsproxy.h> |
| 49 | #include <linux/ns/ns_common_types.h> | ||
| 49 | #include <linux/capability.h> | 50 | #include <linux/capability.h> |
| 50 | #include <linux/cpu.h> | 51 | #include <linux/cpu.h> |
| 51 | #include <linux/cgroup.h> | 52 | #include <linux/cgroup.h> |
| @@ -3045,11 +3046,9 @@ void __init proc_caches_init(void) · 2993 unmodified lines | |||
| 3045 | */ | 3046 | */ |
| 3046 | static int check_unshare_flags(unsigned long unshare_flags) | 3047 | static int check_unshare_flags(unsigned long unshare_flags) |
| 3047 | { | 3048 | { |
| 3048 | if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| | 3049 | if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_SIGHAND| |
| 3049 | CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| | 3050 | CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| |
| 3050 | CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| | 3051 | CLONE_NS_ALL)) |
| 3051 | CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP| | ||
| 3052 | CLONE_NEWTIME)) | ||
| 3053 | return -EINVAL; | 3052 | return -EINVAL; |
| 3054 | /* | 3053 | /* |
| 3055 | * Not implemented, but pretend it works if there is nothing | 3054 | * Not implemented, but pretend it works if there is nothing |
| @@ -12,6 +12,7 @@ · 11 unmodified lines | |||
| 12 | #include <linux/slab.h> | 12 | #include <linux/slab.h> |
| 13 | #include <linux/export.h> | 13 | #include <linux/export.h> |
| 14 | #include <linux/nsproxy.h> | 14 | #include <linux/nsproxy.h> |
| 15 | #include <linux/ns/ns_common_types.h> | ||
| 15 | #include <linux/init_task.h> | 16 | #include <linux/init_task.h> |
| 16 | #include <linux/mnt_namespace.h> | 17 | #include <linux/mnt_namespace.h> |
| 17 | #include <linux/utsname.h> | 18 | #include <linux/utsname.h> |
| @@ -170,9 +171,7 @@ int copy_namespaces(u64 flags, struct task_struct *tsk) · 152 unmodified lines | |||
| 170 | struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns); | 171 | struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns); |
| 171 | struct nsproxy *new_ns; | 172 | struct nsproxy *new_ns; |
| 172 | 173 | ||
| 173 | if (likely(!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | | 174 | if (likely(!(flags & (CLONE_NS_ALL & ~CLONE_NEWUSER)))) { |
| 174 | CLONE_NEWPID | CLONE_NEWNET | | ||
| 175 | CLONE_NEWCGROUP | CLONE_NEWTIME)))) { | ||
| 176 | if ((flags & CLONE_VM) || | 175 | if ((flags & CLONE_VM) || |
| 177 | likely(old_ns->time_ns_for_children == old_ns->time_ns)) { | 176 | likely(old_ns->time_ns_for_children == old_ns->time_ns)) { |
| 178 | get_nsproxy(old_ns); | 177 | get_nsproxy(old_ns); |
| @@ -214,9 +213,7 @@ int unshare_nsproxy_namespaces(unsigned long unshare_flags, · 35 unmodified lines | |||
| 214 | struct user_namespace *user_ns; | 213 | struct user_namespace *user_ns; |
| 215 | int err = 0; | 214 | int err = 0; |
| 216 | 215 | ||
| 217 | if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | | 216 | if (!(unshare_flags & (CLONE_NS_ALL & ~CLONE_NEWUSER))) |
| 218 | CLONE_NEWNET | CLONE_NEWPID | CLONE_NEWCGROUP | | ||
| 219 | CLONE_NEWTIME))) | ||
| 220 | return 0; | 217 | return 0; |
| 221 | 218 | ||
| 222 | user_ns = new_cred ? new_cred->user_ns : current_user_ns(); | 219 | user_ns = new_cred ? new_cred->user_ns : current_user_ns(); |
| @@ -292,9 +289,7 @@ int exec_task_namespaces(void) · 69 unmodified lines | |||
| 292 | 289 | ||
| 293 | static int check_setns_flags(unsigned long flags) | 290 | static int check_setns_flags(unsigned long flags) |
| 294 | { | 291 | { |
| 295 | if (!flags || (flags & ~(CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | | 292 | if (!flags || (flags & ~CLONE_NS_ALL)) |
| 296 | CLONE_NEWNET | CLONE_NEWTIME | CLONE_NEWUSER | | ||
| 297 | CLONE_NEWPID | CLONE_NEWCGROUP))) | ||
| 298 | return -EINVAL; | 293 | return -EINVAL; |
| 299 | 294 | ||
| 300 | #ifndef CONFIG_USER_NS | 295 | #ifndef CONFIG_USER_NS |