[CRIU] [PATCH v4 12/41] pstree: Move thread allocation up and do cleanup

Kirill Tkhai ktkhai at virtuozzo.com
Thu May 4 09:06:43 PDT 2017


Since parse_thread_status() still does not use a passed thread,
moving the allocation up is just a refactoring.

Also make readable the nightmare with indexes and simplify BUG_ON()
using ">=".

v4: New

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 criu/seize.c |   21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/criu/seize.c b/criu/seize.c
index ffc56d8ad..6732151c0 100644
--- a/criu/seize.c
+++ b/criu/seize.c
@@ -689,7 +689,7 @@ static int collect_threads(struct pstree_item *item)
 {
 	struct pid **threads = NULL;
 	int nr_threads = 0, i = 0, ret, nr_inprogress, nr_stopped = 0;
-	int level = item->pid->level;
+	int level = item->pid->level, id;
 
 	ret = parse_threads(item->pid->real, &threads, &nr_threads);
 	if (ret < 0)
@@ -731,8 +731,17 @@ static int collect_threads(struct pstree_item *item)
 		if (!opts.freeze_cgroup && compel_interrupt_task(pid))
 			continue;
 
+		id = item->nr_threads;
+		BUG_ON(id >= nr_threads);
+		item->threads[id] = xmalloc(PID_SIZE(level));
+		if (!item->threads[id])
+			goto err;
+		item->threads[id]->real = pid;
+		item->threads[id]->item = NULL;
+		item->threads[id]->level = level;
+
 		ret = compel_wait_task(pid, item_ppid(item), parse_thread_status, NULL,
-				       &t_creds.s, &item->threads[item->nr_threads]);
+				       &t_creds.s, &item->threads[id]);
 		if (ret < 0) {
 			/*
 			 * Here is a race window between parse_threads() and seize(),
@@ -741,6 +750,7 @@ static int collect_threads(struct pstree_item *item)
 			 * of attempts is restricted, so it will exit if something
 			 * really wrong.
 			 */
+			xfree(item->threads[id]);
 			continue;
 		}
 
@@ -749,13 +759,6 @@ static int collect_threads(struct pstree_item *item)
 		else
 			processes_to_wait--;
 
-		BUG_ON(item->nr_threads + 1 > nr_threads);
-		item->threads[item->nr_threads] = xmalloc(PID_SIZE(level));
-		if (!item->threads[item->nr_threads])
-			goto err;
-		item->threads[item->nr_threads]->real = pid;
-		item->threads[item->nr_threads]->item = NULL;
-		item->threads[item->nr_threads]->level = level;
 		item->nr_threads++;
 
 		if (ret == TASK_DEAD) {



More information about the CRIU mailing list