[CRIU] [PATCH 05/10] rst: Rework the rst_info referencing

Pavel Emelyanov xemul at parallels.com
Mon Sep 29 11:04:39 PDT 2014


Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 cgroup.c         | 10 +++++-----
 cr-restore.c     | 56 ++++++++++++++++++++++++++++----------------------------
 files.c          | 34 +++++++++++++++++-----------------
 include/pstree.h |  8 ++++++--
 mem.c            |  2 +-
 pstree.c         | 22 +++++++++++++---------
 tty.c            |  2 +-
 7 files changed, 71 insertions(+), 63 deletions(-)

diff --git a/cgroup.c b/cgroup.c
index 3b14773..faeaeb8 100644
--- a/cgroup.c
+++ b/cgroup.c
@@ -968,22 +968,22 @@ int prepare_task_cgroup(struct pstree_item *me)
 	CgSetEntry *se;
 	u32 current_cgset;
 
-	if (!me->rst->cg_set)
+	if (!rsti(me)->cg_set)
 		return 0;
 
 	if (me->parent)
-		current_cgset = me->parent->rst->cg_set;
+		current_cgset = rsti(me->parent)->cg_set;
 	else
 		current_cgset = root_cg_set;
 
-	if (me->rst->cg_set == current_cgset) {
+	if (rsti(me)->cg_set == current_cgset) {
 		pr_info("Cgroups %d inherited from parent\n", current_cgset);
 		return 0;
 	}
 
-	se = find_rst_set_by_id(me->rst->cg_set);
+	se = find_rst_set_by_id(rsti(me)->cg_set);
 	if (!se) {
-		pr_err("No set %d found\n", me->rst->cg_set);
+		pr_err("No set %d found\n", rsti(me)->cg_set);
 		return -1;
 	}
 
diff --git a/cr-restore.c b/cr-restore.c
index 4c9308c..7c597b3 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -339,7 +339,7 @@ static int restore_priv_vma_content(pid_t pid)
 {
 	struct vma_area *vma;
 	int ret = 0;
-	struct list_head *vmas = &current->rst->vmas.h;
+	struct list_head *vmas = &rsti(current)->vmas.h;
 
 	unsigned int nr_restored = 0;
 	unsigned int nr_shared = 0;
@@ -491,7 +491,7 @@ static int prepare_mappings(int pid)
 	void *old_premmapped_addr = NULL;
 	unsigned long old_premmapped_len, pstart = 0;
 
-	vmas = &current->rst->vmas;
+	vmas = &rsti(current)->vmas;
 	if (vmas->nr == 0) /* Zombie */
 		goto out;
 
@@ -500,7 +500,7 @@ static int prepare_mappings(int pid)
 	 * See comments in map_private_vma.
 	 */
 	if (current->parent)
-		parent_vmas = &current->parent->rst->vmas.h;
+		parent_vmas = &rsti(current->parent)->vmas.h;
 	else
 		parent_vmas = &empty;
 
@@ -511,10 +511,10 @@ static int prepare_mappings(int pid)
 		return -1;
 	}
 
-	old_premmapped_addr = current->rst->premmapped_addr;
-	old_premmapped_len = current->rst->premmapped_len;
-	current->rst->premmapped_addr = addr;
-	current->rst->premmapped_len = vmas->priv_size;
+	old_premmapped_addr = rsti(current)->premmapped_addr;
+	old_premmapped_len = rsti(current)->premmapped_len;
+	rsti(current)->premmapped_addr = addr;
+	rsti(current)->premmapped_len = vmas->priv_size;
 
 	pvma = list_first_entry(parent_vmas, struct vma_area, list);
 
@@ -556,7 +556,7 @@ out:
 static int unmap_guard_pages()
 {
 	struct vma_area *vma;
-	struct list_head *vmas = &current->rst->vmas.h;
+	struct list_head *vmas = &rsti(current)->vmas.h;
 
 	list_for_each_entry(vma, vmas, list) {
 		if (!vma_priv(vma->e))
@@ -579,7 +579,7 @@ static int open_vmas(int pid)
 {
 	struct vma_area *vma;
 	int ret = 0;
-	struct list_head *vmas = &current->rst->vmas.h;
+	struct list_head *vmas = &rsti(current)->vmas.h;
 
 	list_for_each_entry(vma, vmas, list) {
 		if (!(vma_area_is(vma, VMA_AREA_REGULAR)))
@@ -989,9 +989,9 @@ static void maybe_clone_parent(struct pstree_item *item,
 		 * versions of the kernels, but we treat 3.11 as a base, so at
 		 * least warn a user about potential problems.
 		 */
-		item->rst->clone_flags |= CLONE_PARENT;
+		rsti(item)->clone_flags |= CLONE_PARENT;
 		root_as_sibling = 1;
-		if (item->rst->clone_flags & CLONE_NEWPID)
+		if (rsti(item)->clone_flags & CLONE_NEWPID)
 			pr_warn("Set CLONE_PARENT | CLONE_NEWPID but it might cause restore problem,"
 				"because not all kernels support such clone flags combinations!\n");
 	} else if (opts.restore_detach) {
@@ -1024,10 +1024,10 @@ static inline int fork_with_pid(struct pstree_item *item)
 			return -1;
 
 		item->state = ca.core->tc->task_state;
-		item->rst->cg_set = ca.core->tc->cg_set;
+		rsti(item)->cg_set = ca.core->tc->cg_set;
 
 		if (item->state == TASK_DEAD)
-			item->parent->rst->nr_zombies++;
+			rsti(item->parent)->nr_zombies++;
 		else if (!task_alive(item)) {
 			pr_err("Unknown task state %d\n", item->state);
 			return -1;
@@ -1040,14 +1040,14 @@ static inline int fork_with_pid(struct pstree_item *item)
 		 * Helper entry will not get moved around and thus
 		 * will live in the parent's cgset.
 		 */
-		item->rst->cg_set = item->parent->rst->cg_set;
+		rsti(item)->cg_set = rsti(item->parent)->cg_set;
 		ca.core = NULL;
 	}
 
 	ret = -1;
 
 	ca.item = item;
-	ca.clone_flags = item->rst->clone_flags;
+	ca.clone_flags = rsti(item)->clone_flags;
 
 	BUG_ON(ca.clone_flags & CLONE_VM);
 
@@ -1282,10 +1282,10 @@ static void restore_pgid(void)
 		 * group (-j option).
 		 */
 
-		leader = current->rst->pgrp_leader;
+		leader = rsti(current)->pgrp_leader;
 		if (leader) {
 			BUG_ON(my_pgid != leader->pid.virt);
-			futex_wait_until(&leader->rst->pgrp_set, 1);
+			futex_wait_until(&rsti(leader)->pgrp_set, 1);
 		}
 	}
 
@@ -1296,7 +1296,7 @@ static void restore_pgid(void)
 	}
 
 	if (my_pgid == current->pid.virt)
-		futex_set_and_wake(&current->rst->pgrp_set, 1);
+		futex_set_and_wake(&rsti(current)->pgrp_set, 1);
 }
 
 static int mount_proc(void)
@@ -1396,7 +1396,7 @@ static int restore_task_with_children(void *_arg)
 		close_safe(&ca->fd);
 
 	if (current->state != TASK_HELPER) {
-		ret = clone_service_fd(current->rst->service_fd_id);
+		ret = clone_service_fd(rsti(current)->service_fd_id);
 		if (ret)
 			goto err;
 	}
@@ -1585,7 +1585,7 @@ static int attach_to_tasks(bool root_seized, enum trace_flags *flag)
 				return -1;
 			}
 
-			ret = ptrace_stop_pie(pid, item->rst->breakpoint, flag);
+			ret = ptrace_stop_pie(pid, rsti(item)->breakpoint, flag);
 			if (ret < 0)
 				return -1;
 		}
@@ -1626,7 +1626,7 @@ static void finalize_restore(int status)
 		if (ctl == NULL)
 			goto detach;
 
-		parasite_unmap(ctl, (unsigned long) item->rst->munmap_restorer);
+		parasite_unmap(ctl, (unsigned long)rsti(item)->munmap_restorer);
 
 		xfree(ctl);
 
@@ -2236,7 +2236,7 @@ static int prepare_creds(int pid, struct task_restore_args *args)
 static int prepare_mm(pid_t pid, struct task_restore_args *args)
 {
 	int exe_fd, i, ret = -1;
-	MmEntry *mm = current->rst->mm;
+	MmEntry *mm = rsti(current)->mm;
 
 	args->mm = *mm;
 	args->mm.n_mm_saved_auxv = 0;
@@ -2568,7 +2568,7 @@ static int sigreturn_restore(pid_t pid, CoreEntry *core)
 #endif
 
 	struct vm_area_list self_vmas;
-	struct vm_area_list *vmas = &current->rst->vmas;
+	struct vm_area_list *vmas = &rsti(current)->vmas;
 	int i;
 
 	pr_info("Restore via sigreturn\n");
@@ -2683,7 +2683,7 @@ static int sigreturn_restore(pid_t pid, CoreEntry *core)
 	 */
 	restore_thread_exec_start	= restorer_sym(exec_mem_hint, __export_restore_thread);
 	restore_task_exec_start		= restorer_sym(exec_mem_hint, __export_restore_task);
-	current->rst->munmap_restorer	= restorer_sym(exec_mem_hint, __export_unmap);
+	rsti(current)->munmap_restorer	= restorer_sym(exec_mem_hint, __export_unmap);
 
 	exec_mem_hint += restorer_len;
 
@@ -2717,7 +2717,7 @@ static int sigreturn_restore(pid_t pid, CoreEntry *core)
 	if (rst_mem_remap(mem))
 		goto err;
 
-	task_args->breakpoint = &current->rst->breakpoint;
+	task_args->breakpoint = &rsti(current)->breakpoint;
 	task_args->task_entries = rst_mem_remap_ptr(task_entries_pos, RM_SHREMAP);
 
 	task_args->rst_mem = mem;
@@ -2729,8 +2729,8 @@ static int sigreturn_restore(pid_t pid, CoreEntry *core)
 	task_args->vdso_rt_size = vdso_rt_size;
 #endif
 
-	task_args->premmapped_addr = (unsigned long) current->rst->premmapped_addr;
-	task_args->premmapped_len = current->rst->premmapped_len;
+	task_args->premmapped_addr = (unsigned long)rsti(current)->premmapped_addr;
+	task_args->premmapped_len = rsti(current)->premmapped_len;
 
 	task_args->shmems = rst_mem_remap_ptr(rst_shmems, RM_SHREMAP);
 	task_args->nr_shmems = nr_shmems;
@@ -2882,7 +2882,7 @@ static int sigreturn_restore(pid_t pid, CoreEntry *core)
 	 * Now prepare run-time data for threads restore.
 	 */
 	task_args->nr_threads		= current->nr_threads;
-	task_args->nr_zombies		= current->rst->nr_zombies;
+	task_args->nr_zombies		= rsti(current)->nr_zombies;
 	task_args->clone_restore_fn	= (void *)restore_thread_exec_start;
 	task_args->thread_args		= thread_args;
 
diff --git a/files.c b/files.c
index 42425a2..b975cd4 100644
--- a/files.c
+++ b/files.c
@@ -591,7 +591,7 @@ int prepare_fd_pid(struct pstree_item *item)
 	int ret = 0;
 	struct cr_img *img;
 	pid_t pid = item->pid.virt;
-	struct rst_info *rst_info = item->rst;
+	struct rst_info *rst_info = rsti(item);
 
 	INIT_LIST_HEAD(&rst_info->fds);
 	INIT_LIST_HEAD(&rst_info->eventpoll);
@@ -608,7 +608,7 @@ int prepare_fd_pid(struct pstree_item *item)
 		if (item->ids == NULL) /* zombie */
 			return 0;
 
-		if (item->rst->fdt && item->rst->fdt->pid != item->pid.virt)
+		if (rsti(item)->fdt && rsti(item)->fdt->pid != item->pid.virt)
 			return 0;
 
 		img = open_image(CR_FD_FDINFO, O_RSTR, item->ids->files_id);
@@ -951,8 +951,8 @@ int prepare_fds(struct pstree_item *me)
 
 	close_pid_proc(); /* flush any proc cached fds we may have */
 
-	if (me->rst->fdt) {
-		struct fdt *fdt = me->rst->fdt;
+	if (rsti(me)->fdt) {
+		struct fdt *fdt = rsti(me)->fdt;
 
 		/*
 		 * Wait all tasks, who share a current fd table.
@@ -975,7 +975,7 @@ int prepare_fds(struct pstree_item *me)
 			continue;
 		}
 
-		ret = open_fdinfos(me->pid.virt, &me->rst->fds, state);
+		ret = open_fdinfos(me->pid.virt, &rsti(me)->fds, state);
 		if (ret)
 			break;
 
@@ -983,7 +983,7 @@ int prepare_fds(struct pstree_item *me)
 		 * Now handle TTYs. Slaves are delayed to be sure masters
 		 * are already opened.
 		 */
-		ret = open_fdinfos(me->pid.virt, &me->rst->tty_slaves, state);
+		ret = open_fdinfos(me->pid.virt, &rsti(me)->tty_slaves, state);
 		if (ret)
 			break;
 
@@ -992,13 +992,13 @@ int prepare_fds(struct pstree_item *me)
 		 * to be already restored, thus we store them in a separate
 		 * list and restore at the very end.
 		 */
-		ret = open_fdinfos(me->pid.virt, &me->rst->eventpoll, state);
+		ret = open_fdinfos(me->pid.virt, &rsti(me)->eventpoll, state);
 		if (ret)
 			break;
 	}
 
-	if (me->rst->fdt)
-		futex_inc_and_wake(&me->rst->fdt->fdt_lock);
+	if (rsti(me)->fdt)
+		futex_inc_and_wake(&rsti(me)->fdt->fdt_lock);
 out:
 	close_service_fd(CR_PROC_FD_OFF);
 	tty_fini_fds();
@@ -1032,7 +1032,7 @@ static int fchroot(int fd)
 int restore_fs(struct pstree_item *me)
 {
 	int dd_root, dd_cwd, ret, err = -1;
-	struct rst_info *ri = me->rst;
+	struct rst_info *ri = rsti(me);
 
 	/*
 	 * First -- open both descriptors. We will not
@@ -1084,7 +1084,7 @@ out:
 int prepare_fs_pid(struct pstree_item *item)
 {
 	pid_t pid = item->pid.virt;
-	struct rst_info *ri = item->rst;
+	struct rst_info *ri = rsti(item);
 	struct cr_img *img;
 	FsEntry *fe;
 
@@ -1135,21 +1135,21 @@ int shared_fdt_prepare(struct pstree_item *item)
 	struct pstree_item *parent = item->parent;
 	struct fdt *fdt;
 
-	if (!parent->rst->fdt) {
-		fdt = shmalloc(sizeof(*item->rst->fdt));
+	if (!rsti(parent)->fdt) {
+		fdt = shmalloc(sizeof(*rsti(item)->fdt));
 		if (fdt == NULL)
 			return -1;
 
-		parent->rst->fdt = fdt;
+		rsti(parent)->fdt = fdt;
 
 		futex_init(&fdt->fdt_lock);
 		fdt->nr = 1;
 		fdt->pid = parent->pid.virt;
 	} else
-		fdt = parent->rst->fdt;
+		fdt = rsti(parent)->fdt;
 
-	item->rst->fdt = fdt;
-	item->rst->service_fd_id = fdt->nr;
+	rsti(item)->fdt = fdt;
+	rsti(item)->service_fd_id = fdt->nr;
 	fdt->nr++;
 	if (pid_rst_prio(item->pid.virt, fdt->pid))
 		fdt->pid = item->pid.virt;
diff --git a/include/pstree.h b/include/pstree.h
index d639cfc..090f272 100644
--- a/include/pstree.h
+++ b/include/pstree.h
@@ -28,10 +28,14 @@ struct pstree_item {
 	struct pid		*threads;	/* array of threads */
 	CoreEntry		**core;
 	TaskKobjIdsEntry	*ids;
-
-	struct rst_info		rst[0];
 };
 
+/* See alloc_pstree_item() for details */
+static inline struct rst_info *rsti(struct pstree_item *i)
+{
+	return (struct rst_info *)(i + 1);
+}
+
 /* ids is alocated and initialized for all alive tasks */
 static inline int shared_fdtable(struct pstree_item *item)
 {
diff --git a/mem.c b/mem.c
index 2ef9bbb..c6ff03b 100644
--- a/mem.c
+++ b/mem.c
@@ -386,7 +386,7 @@ int prepare_mm_pid(struct pstree_item *i)
 	pid_t pid = i->pid.virt;
 	int ret = -1, vn = 0;
 	struct cr_img *img;
-	struct rst_info *ri = i->rst;
+	struct rst_info *ri = rsti(i);
 
 	img = open_image(CR_FD_MM, O_RSTR | O_OPT, pid);
 	if (!img) {
diff --git a/pstree.c b/pstree.c
index 29d59f2..0754ed9 100644
--- a/pstree.c
+++ b/pstree.c
@@ -168,11 +168,15 @@ struct pstree_item *__alloc_pstree_item(bool rst)
 		if (!item)
 			return NULL;
 	} else {
-		item = shmalloc(sizeof(*item) + sizeof(item->rst[0]));
+		int sz;
+
+		sz = sizeof(*item) + sizeof(struct rst_info);
+		item = shmalloc(sz);
 		if (!item)
 			return NULL;
-		memset(item, 0, sizeof(*item) + sizeof(item->rst[0]));
-		vm_area_list_init(&item->rst[0].vmas);
+
+		memset(item, 0, sz);
+		vm_area_list_init(&rsti(item)->vmas);
 	}
 
 	INIT_LIST_HEAD(&item->children);
@@ -461,7 +465,7 @@ static int prepare_pstree_ids(void)
 		helper->pid.virt = item->sid;
 		helper->state = TASK_HELPER;
 		helper->parent = root_item;
-		helper->rst->clone_flags = CLONE_FILES | CLONE_FS;
+		rsti(helper)->clone_flags = CLONE_FILES | CLONE_FS;
 		list_add_tail(&helper->sibling, &helpers);
 		task_entries->nr_helpers++;
 
@@ -562,7 +566,7 @@ static int prepare_pstree_ids(void)
 		}
 
 		if (gleader) {
-			item->rst->pgrp_leader = gleader;
+			rsti(item)->pgrp_leader = gleader;
 			continue;
 		}
 
@@ -582,10 +586,10 @@ static int prepare_pstree_ids(void)
 		helper->pid.virt = item->pgid;
 		helper->state = TASK_HELPER;
 		helper->parent = item;
-		helper->rst->clone_flags = CLONE_FILES | CLONE_FS;
+		rsti(helper)->clone_flags = CLONE_FILES | CLONE_FS;
 		list_add(&helper->sibling, &item->children);
 		task_entries->nr_helpers++;
-		item->rst->pgrp_leader = helper;
+		rsti(item)->pgrp_leader = helper;
 
 		pr_info("Add a helper %d for restoring PGID %d\n",
 				helper->pid.virt, helper->pgid);
@@ -669,7 +673,7 @@ static int prepare_pstree_kobj_ids(void)
 		}
 
 set_mask:
-		item->rst->clone_flags = cflags;
+		rsti(item)->clone_flags = cflags;
 		if (parent)
 			/*
 			 * Mount namespaces are setns()-ed at
@@ -677,7 +681,7 @@ set_mask:
 			 * no need in creating it with its own
 			 * temporary namespace
 			 */
-			item->rst->clone_flags &= ~CLONE_NEWNS;
+			rsti(item)->clone_flags &= ~CLONE_NEWNS;
 
 		cflags &= CLONE_ALLNS;
 
diff --git a/tty.c b/tty.c
index 462f023..18707fd 100644
--- a/tty.c
+++ b/tty.c
@@ -764,7 +764,7 @@ static int tty_find_restoring_task(struct tty_info *info)
 			pr_info("Set a control terminal %x to %d\n",
 				info->tfe->id, info->tie->sid);
 			return prepare_ctl_tty(item->pid.virt,
-					       item->rst,
+					       rsti(item),
 					       info->tfe->id);
 		}
 
-- 
1.8.4.2




More information about the CRIU mailing list