A config option to compile out some namespaces code (v2)
cover A config option to compile out some namespaces code (v2)
Changes from v1:
* make the NAMESPACES_EXPERIMENTAL selectable even in !EMBEDDED case
There were some questions like "do I need this on my cellphone" in reply to different namespaces patches. Indeed, the namespaces are not useful for most of the embedded systems, but the code creating and releasing them weights a lot.
So I propose to add a config option which will help embedded people to reduce the vmlinux size. This option simply compiles out the namespaces cloning and releasing code *only*, but keeps all the other logic untouched (e.g. the notion of init_ns).
When someone tries to clone some namespace with their support turned off, he will receive an EINVAL error.
This patchset can save more than 2KB from the vmlinux when turning the config option "NAMESPACES" to "n":
$ scripts/bloat-o-meter vmlinux-no-ns vmlinux-with-ns add/remove: 27/0 grow/shrink: 11/7 up/down: 2477/-340 (2137) function old new delta copy_pid_ns - 537 +537 copy_user_ns - 181 +181 copy_ipcs - 149 +149 zap_pid_ns_processes - 130 +130 copy_utsname - 120 +120 shm_exit_ns - 106 +106 sem_exit_ns - 106 +106 msg_exit_ns - 106 +106 freeary - 100 +100 release_uids - 95 +95 freeque - 92 +92 free_nsproxy 48 123 +75 create_new_namespaces 300 358 +58 free_pid_ns - 56 +56 pid_namespaces_init - 48 +48 __sem_init_ns - 45 +45 shm_init_ns - 42 +42 sem_init_ns - 42 +42 msg_init_ns - 42 +42 __shm_init_ns - 38 +38 __msg_init_ns - 31 +31 sysvipc_proc_release 5 35 +30 proc_kill_sb 5 35 +30 free_ipc_ns - 30 +30 do_shm_rmid - 29 +29 proc_set_super 13 38 +25 shm_release 18 39 +21 put_pid 75 95 +20 alloc_pid 687 706 +19 pid_caches_mutex - 16 +16 free_user_ns - 16 +16 sysvipc_proc_open 100 111 +11 do_shmat 778 787 +9 pid_caches_lh - 8 +8 free_uts_ns - 5 +5 pid_ns_cachep - 4 +4 __initcall_pid_namespaces_init6 - 4 +4 do_exit 1855 1856 +1 show_stat 1665 1661 -4 sys_shmctl 1934 1907 -27 msg_init 82 47 -35 shm_init 92 47 -45 sem_init 99 44 -55 sys_msgctl 1394 1311 -83 sys_semctl 2123 2032 -91
PATCH 1/5 The config option itself
The option is called NAMESPACES. It can be selectable only if EMBEDDED is chosen (this was Eric's requisition). When the EMBEDDED is off namespaces will be on automatically.
One more option (NAMESPACES_EXPERIMENTAL) was added by Serge's request to move there all the namespaces that are not finished yet. Currently only the user and the network namespaces are such. This option is selectable all the time NAMESPACES are "y" regardless of EMBEDDED one.
---
init/Kconfig +17 -0
@@ -390,6 +381,23 @@ config RELAY If unsure, say N. +config NAMESPACES + bool "Namespaces support" if EMBEDDED + default !EMBEDDED + help + Provides the way to make tasks work with different objects using + the same id. For example same IPC id may refer to different objects + or same user id or pid may refer to different tasks when used in + different namespaces. + +config NAMESPACES_EXPERIMENTAL + bool "Add experimantal namespaces support" + depends on NAMESPACES && EXPERIMENTAL + default n + help + Also include the support for the namespaces that are not fnished + or well developed yet + config BLK_DEV_INITRD bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support" depends on BROKEN || !FRV
Re: [PATCH 1/5] The config option itself
Quoted 4 lines · Pavel Emelyanov
The option is called NAMESPACES. It can be selectable only if EMBEDDED is chosen (this was Eric's requisition). When the EMBEDDED is off namespaces will be on automatically.
Quoted 8 lines
One more option (
NAMESPACES_EXPERIMENTAL) was added by Serge's request to move there all the namespaces that are not finished yet. Currently only the user and the network namespaces are such. This option is selectable all the time NAMESPACES are "y" regardless of EMBEDDED one.Signed-off-by Pavel Emelyanov <[email protected]>Acked-by Cedric Le Goater <[email protected]>
Looks good. Thanks.
(for the whole set, just to save the list from a few emails)
-serge
Quoted 31 lines
---
init/Kconfig +17
-0@@ -390,6 +381,23 @@ config RELAY If unsure, say N. +config NAMESPACES + bool "Namespaces support" if EMBEDDED + default !EMBEDDED + help + Provides the way to make tasks work with different objects using + the same id. For example same IPC id may refer to different objects + or same user id or pid may refer to different tasks when used in + different namespaces. + +config NAMESPACES_EXPERIMENTAL + bool "Add experimantal namespaces support" + depends on NAMESPACES && EXPERIMENTAL + default n + help + Also include the support for the namespaces that are not fnished + or well developed yet + config BLK_DEV_INITRD bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support" depends on BROKEN || !FRV
Re: [PATCH 1/5] The config option itself
In reply to Pavel EmelyanovQuoted 9 lines · On Mon, 01 Oct 2007 19:33:27 +0400 Pavel Emelyanov wrote:
init/Kconfig +1
-0@@ -390,6 +381,23 @@ config RELAY If unsure, say N. +config NAMESPACES
init/Kconfig config RELAY · line 394
Quoted 10 lines
init/Kconfig +10
-0+ bool "Namespaces support" if EMBEDDED + default !EMBEDDED + help + Provides the way to make tasks work with different objects using + the same id. For example same IPC id may refer to different objects + or same user id or pid may refer to different tasks when used in + different namespaces. + +config NAMESPACES_EXPERIMENTAL + bool "Add experimantal namespaces support"
experimental
init/Kconfig config RELAY · line 398
Quoted 4 lines
init/Kconfig +4
-0+ depends on NAMESPACES && EXPERIMENTAL + default n + help + Also include the support for the namespaces that are not fnished
finished
init/Kconfig config RELAY · line 403
Quoted 5 lines
init/Kconfig +2
-0+ or well developed yet + config BLK_DEV_INITRD bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support" depends on BROKEN || !FRV
---
~Randy
PATCH 2/5 Move the UST namespace under the option
In reply to Pavel EmelyanovCurrently all the namespace management code is in the kernel/utsname.c file, so just compile it out and make stub in .h file.
The init namespace itself is in init/version.c and is left in the kernel.
---
include/linux/utsname.h +21 -0
@@ -35,6 +35,7 @@ struct new_utsname {#include <linux/sched.h> #include <linux/kref.h> #include <linux/nsproxy.h> +#include <linux/err.h> #include <asm/atomic.h> struct uts_namespace {
@@ -43,6 +44,7 @@ struct uts_namespace { };extern struct uts_namespace init_uts_ns; +#ifdef CONFIG_NAMESPACES static inline void get_uts_ns (struct uts_namespace *ns) {kref_get (&ns->kref);
@@ -56,6 +58,25 @@ static inline void put_uts_ns(struct uts {kref_put (&ns->kref, free_uts_ns); } +#else +static inline void get_uts_ns (struct uts_namespace *ns) +{ +} + +static inline void put_uts_ns (struct uts_namespace *ns) +{ +} + +static inline struct uts_namespace *copy_utsname (unsigned long flags, +struct uts_namespace *ns) +{ +if (flags &CLONE_NEWUTS ) +return ERR_PTR (-EINVAL ); + +return ns; +} +#endif +static inline struct new_utsname *utsname (void ) {return ¤t->nsproxy->uts_ns->name;
kernel/Makefile +2 -2
@@ -4,8 +4,7 @@ signal.o sys.o kmod.o workqueue.o pid.o \ rcupdate.o extable.o params.o posix-timers.o \ kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o \ - hrtimer.o rwsem.o latency.o nsproxy.o srcu.o \ - utsname.o notifier.o sysctl.o + hrtimer.o rwsem.o latency.o nsproxy.o srcu.o notifier.o sysctl.o obj-$(CONFIG_SYSCTL) += sysctl_check.o obj-$(CONFIG_STACKTRACE) += stacktrace.o
@@ -50,6 +49,7 @@ obj-$(CONFIG_AUDITSYSCALL) += auditsc.o obj-$(CONFIG_AUDIT_TREE) += audit_tree.o obj-$(CONFIG_KPROBES) += kprobes.o obj-$(CONFIG_KGDB) += kgdb.o +obj-$(CONFIG_NAMESPACES) += utsname.o obj-$(CONFIG_SYSFS) += ksysfs.o obj-$(CONFIG_DETECT_SOFTLOCKUP) += softlockup.o obj-$(CONFIG_GENERIC_HARDIRQS) += irq/
PATCH 3/5 Move the IPC namespace under the option
In reply to Pavel EmelyanovCurrently all the IPC namespace management code is in ipc/util.c. I moved this code into ipc/namespace.c file which is compiled out when needed.
The linux/ipc_namespace.h file is used to store the prototypes of the functions in namespace.c and the stubs for NAMESPACES=n case. This is done so, because the stub for copy_ipc_namespace requires the knoweledge of the CLONE_NEWIPC flag, which is in sched.h. But the linux/ipc.h file itself in included into many many .c files via the sys.h->sem.h sequence so adding the sched.h into it will make all these .c depend on sched.h which is not that good. On the other hand the knowledge about the namespaces stuff is required in 4 .c files only.
Besides, this patch compiles out some auxiliary functions from ipc/sem.c, msg.c and shm.c files. It turned out that moving these functions into namespaces.c is not that easy because they use many other calls and macros from the original file. Moving them would make this patch complicated. On the other hand all these functions can be consolidated, so I will make it separately a bit later.
---
include/linux/ipc.h +0 -50
@@ -100,56 +100,6 @@ struct kern_ipc_permvoid *security; }; -struct ipc_ids ; -struct ipc_namespace { -struct kref kref; -struct ipc_ids *ids[3 ]; - -int sem_ctls[4 ]; -int used_sems; - -int msg_ctlmax; -int msg_ctlmnb; -int msg_ctlmni; - -size_t shm_ctlmax; -size_t shm_ctlall; -int shm_ctlmni; -int shm_tot; -}; - -extern struct ipc_namespace init_ipc_ns; - -#ifdef CONFIG_SYSVIPC -#define INIT_IPC_NS (ns) .ns = &init_ipc_ns, -extern void free_ipc_ns (struct kref *kref); -extern struct ipc_namespace *copy_ipcs (unsigned long flags, -struct ipc_namespace *ns); -#else -#define INIT_IPC_NS (ns) -static inline struct ipc_namespace *copy_ipcs (unsigned long flags, -struct ipc_namespace *ns) -{ -return ns; -} -#endif - -static inline struct ipc_namespace *get_ipc_ns (struct ipc_namespace *ns) -{ -#ifdef CONFIG_SYSVIPC -if (ns) -kref_get (&ns->kref); -#endif -return ns; -} - -static inline void put_ipc_ns (struct ipc_namespace *ns) -{ -#ifdef CONFIG_SYSVIPC -kref_put (&ns->kref, free_ipc_ns); -#endif -} -#endif /* __KERNEL__ */ #endif /* _LINUX_IPC_H */
include/linux/ipc_namespace.h new file +67 -0
67 lines not on this page · raw message
ipc/Makefile +1 -0
@@ -7,4 +7,5 @@ obj-$(CONFIG_SYSVIPC) += util.o msgutil. obj-$(CONFIG_SYSVIPC_SYSCTL) += ipc_sysctl.o obj_mq-$(CONFIG_COMPAT) += compat_mq.o obj-$(CONFIG_POSIX_MQUEUE) += mqueue.o msgutil.o $(obj_mq-y) +obj-$(CONFIG_NAMESPACES) += namespace.o
ipc/ipc_sysctl.c +1 -0
@@ -14,6 +14,7 @@#include <linux/nsproxy.h> #include <linux/sysctl.h> #include <linux/uaccess.h> +#include <linux/ipc_namespace.h> static void *get_ipc (ctl_table *table) {
ipc/msg.c +3 -0
@@ -36,6 +36,7 @@#include <linux/seq_file.h> #include <linux/mutex.h> #include <linux/nsproxy.h> +#include <linux/ipc_namespace.h> #include <asm/current.h> #include <asm/uaccess.h>
@@ -92,6 +93,7 @@ static void __msg_init_ns(struct ipc_namipc_init_ids (ids); } +#ifdef CONFIG_NAMESPACES int msg_init_ns (struct ipc_namespace *ns) {struct ipc_ids *ids;
@@ -127,6 +129,7 @@ void msg_exit_ns(struct ipc_namespace *nkfree (ns->ids[IPC_MSG_IDS ]); ns->ids[IPC_MSG_IDS] = NULL; } +#endif void __init msg_init (void ) {
ipc/namespace.c new file +73 -0
73 lines not on this page · raw message
ipc/sem.c +3 -0
@@ -82,6 +82,7 @@#include <linux/seq_file.h> #include <linux/mutex.h> #include <linux/nsproxy.h> +#include <linux/ipc_namespace.h> #include <asm/uaccess.h> #include "util.h"
@@ -130,6 +131,7 @@ static void __sem_init_ns(struct ipc_namipc_init_ids (ids); } +#ifdef CONFIG_NAMESPACES int sem_init_ns (struct ipc_namespace *ns) {struct ipc_ids *ids;
@@ -165,6 +167,7 @@ void sem_exit_ns(struct ipc_namespace *nkfree (ns->ids[IPC_SEM_IDS ]); ns->ids[IPC_SEM_IDS] = NULL; } +#endif void __init sem_init (void ) {
ipc/shm.c +3 -0
@@ -38,6 +38,7 @@#include <linux/mutex.h> #include <linux/nsproxy.h> #include <linux/mount.h> +#include <linux/ipc_namespace.h> #include <asm/uaccess.h>
@@ -97,6 +98,7 @@ static void do_shm_rmid(struct ipc_namesshm_destroy (ns, shp); } +#ifdef CONFIG_NAMESPACES int shm_init_ns (struct ipc_namespace *ns) {struct ipc_ids *ids;
@@ -132,6 +134,7 @@ void shm_exit_ns(struct ipc_namespace *nkfree (ns->ids[IPC_SHM_IDS ]); ns->ids[IPC_SHM_IDS] = NULL; } +#endif void __init shm_init (void ) {
ipc/util.c +1 -60
73 lines not on this page · raw message
ipc/util.h +2 -0
@@ -20,6 +20,8 @@ void sem_init (void);void msg_init (void );void shm_init (void ); +struct ipc_namespace ; +int sem_init_ns (struct ipc_namespace *ns);int msg_init_ns (struct ipc_namespace *ns);int shm_init_ns (struct ipc_namespace *ns);
kernel/nsproxy.c +1 -0
@@ -20,6 +20,7 @@#include <linux/mnt_namespace.h> #include <linux/utsname.h> #include <linux/pid_namespace.h> +#include <linux/ipc_namespace.h> static struct kmem_cache *nsproxy_cachep;
Re: [PATCH 3/5] Move the IPC namespace under the option
Quoted 23 lines · Pavel Emelyanov
Currently all the IPC namespace management code is in
ipc/util.c. I moved this code intoipc/namespace.cfile which is compiled out when needed.The
linux/ipc_namespace.hfile is used to store the prototypes of the functions innamespace.cand the stubs for NAMESPACES=n case. This is done so, because the stub forcopy_ipc_namespacerequires the knoweledge of theCLONE_NEWIPCflag, which is insched.h. But thelinux/ipc.hfile itself in included into many many .c files via thesys.h->sem.hsequence so adding thesched.hinto it will make all these .c depend onsched.hwhich is not that good. On the other hand the knowledge about the namespaces stuff is required in 4 .c files only.Besides, this patch compiles out some auxiliary functions from
ipc/sem.c,msg.candshm.cfiles. It turned out that moving these functions intonamespaces.cis not that easy because they use many other calls and macros from the original file. Moving them would make this patch complicated. On the other hand all these functions can be consolidated, so I will make it separately a bit later.
Quoted 1 line
Signed-off-by Pavel Emelyanov <[email protected]>
Fine with me.
Thanks !
C.
PATCH 4/5 Move the user namespace under the option
In reply to Pavel EmelyanovWe currently have a CONFIG_USER_NS option. Just rename it into CONFIG_NAMESPACES_EXPERIMENTAL and move the init_user_ns into user.c file to make the kernel compile and work without the namespaces support.
---
include/linux/user_namespace.h +1 -1
@@ -17,7 +17,7 @@ struct user_namespace {extern struct user_namespace init_user_ns; -#ifdef CONFIG_USER_NS +#ifdef CONFIG_NAMESPACES_EXPERIMENTAL static inline struct user_namespace *get_user_ns (struct user_namespace *ns) {
init/Kconfig +0 -9
@@ -206,15 +206,6 @@ config TASK_IO_ACCOUNTING Say N if unsure. -config USER_NS - bool "User Namespaces (EXPERIMENTAL)" - default n - depends on EXPERIMENTAL - help - Support user namespaces. This allows containers, i.e. - vservers, to use user namespaces to provide different - user info for different servers. If unsure, say N. - config AUDIT bool "Auditing support" depends on NET
kernel/Makefile +2 -1
@@ -4,7 +4,7 @@ obj-y = sched.o fork.o exec_domain.o panic.o printk.o profile.o \ exit.o itimer.o time.o softirq.o resource.o \ - sysctl.o capability.o ptrace.o timer.o user.o user_namespace.o \ + sysctl.o capability.o ptrace.o timer.o user.o \ signal.o sys.o kmod.o workqueue.o pid.o \ rcupdate.o extable.o params.o posix-timers.o \ kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o \
@@ -50,6 +49,7 @@ obj-$(CONFIG_AUDITSYSCALL) += auditsc.o obj-$(CONFIG_KPROBES) += kprobes.o obj-$(CONFIG_KGDB) += kgdb.o obj-$(CONFIG_NAMESPACES) += utsname.o +obj-$(CONFIG_NAMESPACES_EXPERIMENTAL) += user_namespace.o obj-$(CONFIG_SYSFS) += ksysfs.o obj-$(CONFIG_DETECT_SOFTLOCKUP) += softlockup.o obj-$(CONFIG_GENERIC_HARDIRQS) += irq/
kernel/user.c +11 -0
@@ -17,6 +17,15 @@#include <linux/module.h> #include <linux/user_namespace.h> +struct user_namespace init_user_ns = { + .kref = { + .refcount =ATOMIC_INIT (2 ), + }, + .root_user = &root_user, +}; + +EXPORT_SYMBOL_GPL (init_user_ns); +/* * UID task count cache, to get fast user lookup in "alloc_uid" * when changing user ID's (ie setuid() and friends).
@@ -242,6 +251,7 @@ void switch_uid(struct user_struct *new_suid_keys (current); } +#ifdef CONFIG_NAMESPACES_EXPERIMENTAL void release_uids (struct user_namespace *ns) {int i;
@@ -266,6 +276,7 @@ void release_uids(struct user_namespace free_uid(ns->root_user); } +#endif static int __init uid_cache_init (void ) {
kernel/user_namespace.c +0 -13
@@ -10,17 +10,6 @@#include <linux/nsproxy.h> #include <linux/user_namespace.h> -struct user_namespace init_user_ns = { - .kref = { - .refcount =ATOMIC_INIT (2 ), - }, - .root_user = &root_user, -}; - -EXPORT_SYMBOL_GPL (init_user_ns); - -#ifdef CONFIG_USER_NS -/* * Clone a new ns copying an original user ns, setting refcount to 1 * @old_ns: namespace to clone
@@ -84,5 +73,3 @@ void free_user_ns(struct kref *kref)release_uids (ns);kfree (ns); } - -#endif /* CONFIG_USER_NS */
PATCH 5/5 Move the PID namespace under the option
In reply to Pavel EmelyanovFor the same reasons as with the IPC namespaces, all the prototypes and stuns go to the pid_namespace.h file. The namespace management code itself is moved to the pid_namespace.c file.
The pid_namespace cache is created inside an initcall, i.e. a bit later than the pid hash is initialized. This is OK for now - no code in kernel tries to clone new pid namespaces before boot.
The zap_pid_namespace() function is expanded into a BUG() when NAMESPACES is "n". This is normal as exiting the init namespace (the only namespace in this case) causes a panic() in exit_child_reaper() function anyway.
---
include/linux/pid.h +2 -1
@@ -121,7 +121,8 @@ extern struct pid *find_ge_pid(int nr, sextern struct pid *alloc_pid (struct pid_namespace *ns);extern void FASTCALL (free_pid (struct pid *pid)); -extern void zap_pid_ns_processes (struct pid_namespace *pid_ns); + +int next_pidmap (struct pid_namespace *pid_ns,int last);/* * the helpers to get the pid's id seen from different namespaces
include/linux/pid_namespace.h +27 -0
@@ -6,6 +6,7 @@#include <linux/threads.h> #include <linux/nsproxy.h> #include <linux/kref.h> +#include <linux/err.h> struct pidmap {atomic_t nr_free;
@@ -29,6 +30,7 @@ struct pid_namespace {extern struct pid_namespace init_pid_ns; +#ifdef CONFIG_NAMESPACES static inline struct pid_namespace *get_pid_ns (struct pid_namespace *ns) {if (ns != &init_pid_ns)
@@ -38,12 +40,37 @@ static inline struct pid_namespace *get_extern struct pid_namespace *copy_pid_ns (unsigned long flags,struct pid_namespace *ns);extern void free_pid_ns (struct kref *kref); +extern void zap_pid_ns_processes (struct pid_namespace *pid_ns);static inline void put_pid_ns (struct pid_namespace *ns) {if (ns != &init_pid_ns)kref_put (&ns->kref, free_pid_ns); } +#else +static inline struct pid_namespace *get_pid_ns (struct pid_namespace *ns) +{ +return ns; +} + +static inline void put_pid_ns (struct pid_namespace *ns) +{ +} + +static inline struct pid_namespace *copy_pid_ns (unsigned long flags, +struct pid_namespace *ns) +{ +if (flags &CLONE_NEWPID ) +return ERR_PTR (-EINVAL ); + +return ns; +} + +static inline void zap_pid_ns_processes (struct pid_namespace *ns) +{ +BUG (); +} +#endif static inline struct pid_namespace *task_active_pid_ns (struct task_struct *tsk) {
kernel/Makefile +1 -0
@@ -50,6 +49,7 @@ obj-$(CONFIG_AUDITSYSCALL) += auditsc.o obj-$(CONFIG_KPROBES) += kprobes.o obj-$(CONFIG_KGDB) += kgdb.o obj-$(CONFIG_NAMESPACES) += utsname.o +obj-$(CONFIG_NAMESPACES) += pid_namespace.o obj-$(CONFIG_NAMESPACES_EXPERIMENTAL) += user_namespace.o obj-$(CONFIG_SYSFS) += ksysfs.o obj-$(CONFIG_DETECT_SOFTLOCKUP) += softlockup.o
kernel/pid.c +3 -186
223 lines not on this page · raw message
kernel/pid_namespace.c new file +196 -0
196 lines not on this page · raw message