[CRIU] [PATCH 5/6] pstree: try to find a free pid between busy pids (v2)

Andrey Vagin avagin at openvz.org
Thu Mar 10 11:24:42 PST 2016


From: Andrew Vagin <avagin at virtuozzo.com>

Currently our pid allocator returns max_pid++ and it can return a pid
which is bigger than kernel.max_pid.

(00.821430)   5506: Error (cr-restore.c:1540): Pid 300 do not match expected 32768

https://jira.sw.ru/browse/PSBM-42202

v2: handle error code from insert_item()
Signed-off-by: Andrew Vagin <avagin at virtuozzo.com>
---
 criu/pstree.c | 41 +++++++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 8 deletions(-)

diff --git a/criu/pstree.c b/criu/pstree.c
index 1fa3f96..00dc174 100644
--- a/criu/pstree.c
+++ b/criu/pstree.c
@@ -334,8 +334,6 @@ err:
 	return ret;
 }
 
-static int max_pid = 0;
-
 static int prepare_pstree_for_shell_job(void)
 {
 	pid_t current_sid = getsid(getpid());
@@ -382,8 +380,10 @@ static int prepare_pstree_for_shell_job(void)
 			pi->sid = current_sid;
 	}
 
-	max_pid = max((int)current_sid, max_pid);
-	max_pid = max((int)current_gid, max_pid);
+	if (insert_item(current_sid))
+		return -1;
+	if (insert_item(current_gid))
+		return -1;
 
 	return 0;
 }
@@ -474,11 +474,8 @@ static int read_pstree_image(void)
 			break;
 
 		pi->pid.virt = e->pid;
-		max_pid = max((int)e->pid, max_pid);
 		pi->pgid = e->pgid;
-		max_pid = max((int)e->pgid, max_pid);
 		pi->sid = e->sid;
-		max_pid = max((int)e->sid, max_pid);
 		pi->pid.state = TASK_ALIVE;
 
 		if (e->ppid == 0) {
@@ -567,11 +564,34 @@ static int read_pstree_image(void)
 				goto err;
 		}
 	}
+
 err:
 	close_image(img);
 	return ret;
 }
 
+#define RESERVED_PIDS           300
+static int get_free_pid()
+{
+	static struct pstree_item *prev, *next;
+
+	if (prev == NULL)
+		prev = rb_entry(rb_first(&pid_root_rb), struct pstree_item, pid.node);
+
+	while (1) {
+		pid_t pid;
+		pid = prev->pid.virt + 1;
+		pid = pid < RESERVED_PIDS ? RESERVED_PIDS + 1 : pid;
+
+		next = rb_entry(rb_next(&prev->pid.node), struct pstree_item, pid.node);
+		if (&next->pid.node == NULL || next->pid.virt > pid)
+			return pid;
+		prev = next;
+	}
+
+	return -1;
+}
+
 static int prepare_pstree_ids(void)
 {
 	struct pstree_item *item, *child, *helper, *tmp;
@@ -598,7 +618,12 @@ static int prepare_pstree_ids(void)
 
 		leader = insert_item(item->sid);
 		if (leader->pid.state != TASK_UNDEF) {
-			helper = insert_item(++max_pid);
+			pid_t pid;
+
+			pid = get_free_pid();
+			if (pid < 0)
+				break;
+			helper = insert_item(pid);
 			if (helper == NULL)
 				return -1;
 
-- 
2.5.0



More information about the CRIU mailing list