cr: deal with pid/pidns
Subsystems (4)
PATCH 21/30 cr: deal with pid/pidns
C/R struct pid and struct pid_namespace.
Userspace should see same pids as before.
->last_pid is visible though /proc/loadavg, so it needs to be dumped too, sigh.
FIXME: restoration of pidns is recursive.
Signed-off-by Alexey Dobriyan <[email protected]>
---
include/linux/cr.h | 26 +++ include/linux/pid.h | 2 include/linux/pid_namespace.h | 2 kernel/cr/Kconfig | 1 kernel/cr/Makefile | 1 kernel/cr/cpt-sys.c | 12 + kernel/cr/cr-file.c | 10 + kernel/cr/cr-nsproxy.c | 15 + kernel/cr/cr-pid.c | 363 ++++++++++++++++++++++++++++++++++++++++++ kernel/cr/cr-task.c | 18 ++ kernel/cr/cr.h | 8 kernel/fork.c | 2 kernel/pid.c | 39 ++++ kernel/pid_namespace.c | 4 14 files changed, 496 insertions(+), 7 deletions(-)
include/linux/cr.h +26 -0
@@ -37,6 +37,8 @@ struct cr_object_header {#define CR_OBJ_VMA_VDSO 6#define CR_OBJ_NSPROXY 7#define CR_OBJ_UTS_NS 8 +#define CR_OBJ_PID_NS 9 +#define CR_OBJ_PID 10__u32 cr_type;/* object type */ __u32 cr_len;/* object length in bytes including header */ } __packed;
@@ -56,6 +58,7 @@ struct cr_image_task_struct {cr_pos_t cr_pos_real_parent;cr_pos_t cr_pos_mm; +cr_pos_t cr_pos_pids[3 ];cr_pos_t cr_pos_nsproxy;__u8 cr_comm[16 ];
@@ -153,6 +156,7 @@ struct cr_image_nsproxy {struct cr_object_header cr_hdr;cr_pos_t cr_pos_uts_ns; +cr_pos_t cr_pos_pid_ns; } __packed;struct cr_image_uts_ns {
@@ -166,6 +170,13 @@ struct cr_image_uts_ns {__u8 cr_domainname[64 ]; } __packed; +struct cr_image_pid_ns { +struct cr_object_header cr_hdr; + +cr_pos_t cr_pos_parent;/* CR_POS_UNDEF if root pid_ns */ +__u32 cr_last_pid; +} __packed; +struct cr_image_mm_struct {struct cr_object_header cr_hdr;
@@ -223,7 +234,22 @@ struct cr_image_file {__u32 cr_i_mode;__u32 cr_f_flags;__u64 cr_f_pos; +struct { +cr_pos_t cr_pos_pid; +__u32 cr_pid_type; +__u32 cr_uid; +__u32 cr_euid; +__u32 cr_signum; + } cr_f_owner;__u32 cr_name_len; /* __u8 cr_name[cr_name_len] */ } __packed; + +struct cr_image_pid { +struct cr_object_header cr_hdr; + +cr_pos_t cr_pos_pid_ns;/* position of last pid_ns */ +__u32 cr_level; +__u32 cr_nr[1 ];/* cr_nr[cr_level + 1] */ +} __packed;#endif
include/linux/pid.h +1 -1
@@ -119,7 +119,7 @@ extern struct pid *find_get_pid(int nr);extern struct pid *find_ge_pid (int nr,struct pid_namespace *);int next_pidmap (struct pid_namespace *pid_ns,int last); -extern struct pid *alloc_pid (struct pid_namespace *ns); +extern struct pid *alloc_pid (struct pid_namespace *ns,int *cr_nr,unsigned int cr_level);extern void free_pid (struct pid *pid);/*
include/linux/pid_namespace.h +2 -0
@@ -42,6 +42,8 @@ static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)return ns; } +struct pid_namespace *create_pid_namespace (unsigned int level); +void destroy_pid_namespace (struct pid_namespace *pid_ns);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);
kernel/cr/Kconfig +1 -0
@@ -1,5 +1,6 @@ config CR bool "Container checkpoint/restart" + depends on PID_NS depends on UTS_NS select FREEZER help
kernel/cr/Makefile +1 -0
@@ -4,6 +4,7 @@ cr-y += cr-context.o cr-y += cr-file.o cr-y += cr-mm.o cr-y += cr-nsproxy.o +cr-y += cr-pid.o cr-y += cr-task.o cr-y += cr-uts.o cr-$(CONFIG_X86_32) += cr-x86_32.o
kernel/cr/cpt-sys.c +12 -0
@@ -71,12 +71,18 @@ static int cr_collect(struct cr_context *ctx) rv =cr_collect_all_uts_ns (ctx);if (rv <0 )return rv; + rv =cr_collect_all_pid_ns (ctx); +if (rv <0 ) +return rv; rv =cr_collect_all_mm_struct (ctx);if (rv <0 )return rv; rv =cr_collect_all_file (ctx);if (rv <0 )return rv; + rv =cr_collect_all_pid (ctx); +if (rv <0 ) +return rv;return 0 ; }
@@ -110,6 +116,12 @@ static int cr_dump(struct cr_context *ctx) rv =cr_dump_image_header (ctx);if (rv <0 )return rv; + rv =cr_dump_all_pid_ns (ctx); +if (rv <0 ) +return rv; + rv =cr_dump_all_pid (ctx); +if (rv <0 ) +return rv; rv =cr_dump_all_file (ctx);if (rv <0 )return rv;
kernel/cr/cr-file.c +10 -0
@@ -92,6 +92,7 @@ int generic_file_checkpoint(struct file *file, struct cr_context *ctx) {struct cr_object *obj;struct cr_image_file *i; +struct cr_object *tmp;struct kstat stat;char *buf, *name;int rv;
@@ -109,6 +110,15 @@ int generic_file_checkpoint(struct file *file, struct cr_context *ctx) i->cr_i_mode = stat.mode; i->cr_f_flags = file->f_flags; i->cr_f_pos = file->f_pos; +if (file->f_owner.pid) { + tmp =cr_find_obj_by_ptr (ctx, file->f_owner.pid,CR_CTX_PID ); + i->cr_f_owner.cr_pos_pid = tmp->o_pos; + }else + i->cr_f_owner.cr_pos_pid =CR_POS_UNDEF ; + i->cr_f_owner.cr_pid_type = file->f_owner.pid_type; + i->cr_f_owner.cr_uid = file->f_owner.uid; + i->cr_f_owner.cr_euid = file->f_owner.euid; + i->cr_f_owner.cr_signum = file->f_owner.signum; buf =kmalloc (PAGE_SIZE ,GFP_KERNEL );if (!buf) {
kernel/cr/cr-nsproxy.c +14 -1
@@ -59,6 +59,8 @@ static int cr_dump_nsproxy(struct cr_context *ctx, struct cr_object *obj) tmp =cr_find_obj_by_ptr (ctx, nsproxy->uts_ns,CR_CTX_UTS_NS ); i->cr_pos_uts_ns = tmp->o_pos; + tmp =cr_find_obj_by_ptr (ctx, nsproxy->pid_ns,CR_CTX_PID_NS ); + i->cr_pos_pid_ns = tmp->o_pos; obj->o_pos = ctx->cr_dump_file->f_pos; rv =cr_write (ctx, i,sizeof (*i));
@@ -84,6 +86,7 @@ static int __cr_restore_nsproxy(struct cr_context *ctx, loff_t pos)struct cr_image_nsproxy *i;struct nsproxy *nsproxy;struct uts_namespace *uts_ns; +struct pid_namespace *pid_ns;struct cr_object *obj, *tmp;int rv;
@@ -127,7 +130,17 @@ static int __cr_restore_nsproxy(struct cr_context *ctx, loff_t pos)get_mnt_ns (init_nsproxy.mnt_ns); nsproxy->mnt_ns = init_nsproxy.mnt_ns; - nsproxy->pid_ns =get_pid_ns (&init_pid_ns); + tmp =cr_find_obj_by_pos (ctx, i->cr_pos_pid_ns,CR_CTX_PID_NS ); +if (!tmp) { + rv =cr_restore_pid_ns (ctx, i->cr_pos_pid_ns); +if (rv <0 ) { +kfree (i); +return rv; + } + tmp =cr_find_obj_by_pos (ctx, i->cr_pos_pid_ns,CR_CTX_PID_NS ); + } + pid_ns = tmp->o_obj; + nsproxy->pid_ns =get_pid_ns (pid_ns);#ifdef CONFIG_NET nsproxy->net_ns =get_net (&init_net);
new file mode 100644
kernel/cr/cr-pid.c new file +363 -0
363 lines not on this page · raw message
kernel/cr/cr-task.c +18 -0
@@ -2,6 +2,7 @@#include <linux/fs.h> #include <linux/kthread.h> #include <linux/nsproxy.h> +#include <linux/pid.h> #include <linux/pid_namespace.h> #include <linux/sched.h> #include <linux/tty.h>
@@ -113,6 +114,20 @@ static int cr_dump_task_struct(struct cr_context *ctx, struct cr_object *obj) tmp =cr_find_obj_by_ptr (ctx, tsk->nsproxy,CR_CTX_NSPROXY ); i->cr_pos_nsproxy = tmp->o_pos; +BUILD_BUG_ON (PIDTYPE_MAX !=3 ); + tmp =cr_find_obj_by_ptr (ctx, tsk->pids[PIDTYPE_PID ].pid,CR_CTX_PID ); + i->cr_pos_pids[0 ] = tmp->o_pos; + tmp =cr_find_obj_by_ptr (ctx, tsk->pids[PIDTYPE_PGID ].pid,CR_CTX_PID ); +if (tmp) + i->cr_pos_pids[1 ] = tmp->o_pos; +else + i->cr_pos_pids[1 ] =CR_POS_UNDEF ; + tmp =cr_find_obj_by_ptr (ctx, tsk->pids[PIDTYPE_SID ].pid,CR_CTX_PID ); +if (tmp) + i->cr_pos_pids[2 ] = tmp->o_pos; +else + i->cr_pos_pids[2 ] =CR_POS_UNDEF ; +BUILD_BUG_ON (TASK_COMM_LEN !=16 );strlcpy ((char *)i->cr_comm, (const char *)tsk->comm,sizeof (i->cr_comm));
@@ -171,6 +186,9 @@ static int task_struct_restorer(void *_tsk_ctx) rv =cr_restore_nsproxy (ctx, i->cr_pos_nsproxy);if (rv <0 ) goto out; + rv =cr_restore_pid (ctx, i); +if (rv <0 ) + goto out; rv =0 ; out:
kernel/cr/cr.h +8 -0
@@ -25,6 +25,8 @@ enum cr_context_obj_type {CR_CTX_FILE ,CR_CTX_MM_STRUCT ,CR_CTX_NSPROXY , +CR_CTX_PID , +CR_CTX_PID_NS ,CR_CTX_TASK_STRUCT ,CR_CTX_UTS_NS ,NR_CR_CTX_TYPES
@@ -71,18 +73,24 @@ static inline void __user *cr_restore_ptr(__u64 ptr)int cr_collect_all_file (struct cr_context *ctx);int cr_collect_all_mm_struct (struct cr_context *ctx);int cr_collect_all_nsproxy (struct cr_context *ctx); +int cr_collect_all_pid_ns (struct cr_context *ctx); +int cr_collect_all_pid (struct cr_context *ctx);int cr_collect_all_task_struct (struct cr_context *ctx);int cr_collect_all_uts_ns (struct cr_context *ctx);int cr_dump_all_file (struct cr_context *ctx);int cr_dump_all_mm_struct (struct cr_context *ctx);int cr_dump_all_nsproxy (struct cr_context *ctx); +int cr_dump_all_pid_ns (struct cr_context *ctx); +int cr_dump_all_pid (struct cr_context *ctx);int cr_dump_all_task_struct (struct cr_context *ctx);int cr_dump_all_uts_ns (struct cr_context *ctx);int cr_restore_file (struct cr_context *ctx,loff_t pos);int cr_restore_mm_struct (struct cr_context *ctx,loff_t pos);int cr_restore_nsproxy (struct cr_context *ctx,loff_t pos); +int cr_restore_pid_ns (struct cr_context *ctx,loff_t pos); +int cr_restore_pid (struct cr_context *ctx,struct cr_image_task_struct *i);int cr_restore_task_struct (struct cr_context *ctx,loff_t pos);int cr_restore_uts_ns (struct cr_context *ctx,loff_t pos);
kernel/fork.c +1 -1
@@ -1112,7 +1112,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,if (pid != &init_struct_pid) { retval = -ENOMEM ; - pid =alloc_pid (p->nsproxy->pid_ns); + pid =alloc_pid (p->nsproxy->pid_ns,NULL ,0 );if (!pid) goto bad_fork_cleanup_io;
kernel/pid.c +37 -2
@@ -182,6 +182,36 @@ static int alloc_pidmap(struct pid_namespace *pid_ns)return -1 ; } +#ifdef CONFIG_CR +static int set_pidmap (struct pid_namespace *pid_ns,pid_t pid) +{ +int offset; +struct pidmap *map; + + offset = pid &BITS_PER_PAGE_MASK ; + map = &pid_ns->pidmap[pid/BITS_PER_PAGE ]; +if (!map->page) { +void *page =kzalloc (PAGE_SIZE ,GFP_KERNEL ); +/* +* Free the page if someone raced with us +* installing it: +*/ +spin_lock_irq (&pidmap_lock); +if (map->page) +kfree (page); +else + map->page = page; +spin_unlock_irq (&pidmap_lock); +if (unlikely (!map->page)) +return -ENOMEM ; + } +if (test_and_set_bit (offset, map->page)) +return -EBUSY ; +atomic_dec (&map->nr_free); +return pid; +} +#endif +int next_pidmap (struct pid_namespace *pid_ns,int last) {int offset;
@@ -239,7 +269,7 @@ void free_pid(struct pid *pid)call_rcu (&pid->rcu, delayed_put_pid); } -struct pid *alloc_pid (struct pid_namespace *ns) +struct pid *alloc_pid (struct pid_namespace *ns,int *cr_nr,unsigned int cr_level) {struct pid *pid;enum pid_type type;
@@ -253,7 +283,12 @@ struct pid *alloc_pid(struct pid_namespace *ns) tmp = ns;for (i = ns->level; i >=0 ; i--) { - nr =alloc_pidmap (tmp); +#ifdef CONFIG_CR +if (cr_nr && ns->level - i <= cr_level) + nr =set_pidmap (tmp, cr_nr[ns->level - i]); + else +#endif + nr =alloc_pidmap (tmp);if (nr <0 ) goto out_free;
kernel/pid_namespace.c +2 -2
@@ -67,7 +67,7 @@ err_alloc:return NULL ; } -static struct pid_namespace *create_pid_namespace (unsigned int level) +struct pid_namespace *create_pid_namespace (unsigned int level) {struct pid_namespace *ns;int i;
@@ -103,7 +103,7 @@ out:return ERR_PTR (-ENOMEM ); } -static void destroy_pid_namespace (struct pid_namespace *ns) +void destroy_pid_namespace (struct pid_namespace *ns) {int i;