[RESEND,v1,10/55] pid: Add pid::level field and level argument for __alloc_pstree_item()

Submitted by Kirill Tkhai on March 24, 2017, 3:10 p.m.

Details

Message ID 149036823069.23093.13960138954986382885.stgit@localhost.localdomain
State New
Series "Nested pid namespaces support"
Headers show

Commit Message

Kirill Tkhai March 24, 2017, 3:10 p.m.
Pid may contain several levels, so add level field to this struct.
Currently, level is always "1".

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 criu/include/pid.h    |    1 +
 criu/include/pstree.h |    4 ++--
 criu/pstree.c         |   27 +++++++++++++++++++--------
 3 files changed, 22 insertions(+), 10 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/include/pid.h b/criu/include/pid.h
index 81786ec4..475b17a3 100644
--- a/criu/include/pid.h
+++ b/criu/include/pid.h
@@ -38,6 +38,7 @@  struct pid {
 	 * the pid value to be restored. This pid fetched from the
 	 * dumpee context, because the dumpee might have own pid namespace.
 	 */
+	int level;
 	struct {
 		pid_t virt;
 		struct rb_node node;
diff --git a/criu/include/pstree.h b/criu/include/pstree.h
index 5790943b..769dcf7d 100644
--- a/criu/include/pstree.h
+++ b/criu/include/pstree.h
@@ -86,8 +86,8 @@  static inline bool task_alive(struct pstree_item *i)
 }
 
 extern void free_pstree(struct pstree_item *root_item);
-extern struct pstree_item *__alloc_pstree_item(bool rst);
-#define alloc_pstree_item() __alloc_pstree_item(false)
+extern struct pstree_item *__alloc_pstree_item(bool rst, int level);
+#define alloc_pstree_item() __alloc_pstree_item(false, 1)
 extern void init_pstree_helper(struct pstree_item *ret);
 
 extern struct pstree_item *lookup_create_item(pid_t pid);
diff --git a/criu/pstree.c b/criu/pstree.c
index 93c90ee2..c02ddb21 100644
--- a/criu/pstree.c
+++ b/criu/pstree.c
@@ -191,31 +191,41 @@  void free_pstree(struct pstree_item *root_item)
 		list_del(&item->sibling);
 		pstree_free_cores(item);
 		xfree(item->threads);
+		xfree(item->pid);
 		xfree(item);
 		item = parent;
 	}
 }
 
-struct pstree_item *__alloc_pstree_item(bool rst)
+struct pstree_item *__alloc_pstree_item(bool rst, int level)
 {
 	struct pstree_item *item;
-	int sz;
+	int sz, p_sz;
 
+	p_sz = sizeof(struct pid) + (level - 1) * sizeof(((struct pid *)NULL)->ns[0]);
 	if (!rst) {
-		sz = sizeof(*item) + sizeof(struct dmp_info) + sizeof(struct pid);
+		sz = sizeof(*item) + sizeof(struct dmp_info);
 		item = xzalloc(sz);
 		if (!item)
 			return NULL;
-		item->pid = (void *)item + sizeof(*item) + sizeof(struct dmp_info);
+		item->pid = xmalloc(p_sz);
+		if (!item->pid) {
+			xfree(item);
+			return NULL;
+		}
 	} else {
-		sz = sizeof(*item) + sizeof(struct rst_info) + sizeof(struct pid);
+		sz = sizeof(*item) + sizeof(struct rst_info);
 		item = shmalloc(sz);
 		if (!item)
 			return NULL;
-
 		memset(item, 0, sz);
 		vm_area_list_init(&rsti(item)->vmas);
-		item->pid = (void *)item + sizeof(*item) + sizeof(struct rst_info);
+
+		item->pid = shmalloc(p_sz);
+		if (!item->pid) {
+			shfree_last(item);
+			return NULL;
+		}
 	}
 
 	INIT_LIST_HEAD(&item->children);
@@ -227,6 +237,7 @@  struct pstree_item *__alloc_pstree_item(bool rst)
 	item->born_sid = -1;
 	item->pid->item = item;
 	futex_init(&item->task_st);
+	item->pid->level = level;
 
 	return item;
 }
@@ -411,7 +422,7 @@  static struct pid *lookup_create_pid(pid_t pid, struct pid *pid_node)
 	if (!pid_node) {
 		struct pstree_item *item;
 
-		item = __alloc_pstree_item(true);
+		item = __alloc_pstree_item(true, 1);
 		if (item == NULL)
 			return NULL;