Commit f13ec96e authored by Andrew Vagin's avatar Andrew Vagin Committed by Pavel Emelyanov

restore: fix race in calculation of a number of zombies

Currently each task subtracts number of zombies from
task_entries->nr_threads without locks, so if two tasks will do this
operation concurrently, the result may be unpredictable.

https://github.com/xemul/criu/issues/13

Cc: Tycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: 's avatarAndrew Vagin <avagin@openvz.org>
Acked-by: 's avatarTycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 9d9518b8
......@@ -1838,6 +1838,12 @@ static int restore_root_task(struct pstree_item *init)
if (ret < 0)
goto out_kill;
/*
* The task_entries->nr_zombies is updated in the
* CR_STATE_RESTORE_SIGCHLD in pie code.
*/
task_entries->nr_threads -= atomic_read(&task_entries->nr_zombies);
ret = stop_usernsd();
if (ret < 0)
goto out_kill;
......@@ -1940,6 +1946,7 @@ static int prepare_task_entries(void)
task_entries->nr_threads = 0;
task_entries->nr_tasks = 0;
task_entries->nr_helpers = 0;
atomic_set(&task_entries->nr_zombies, 0);
futex_set(&task_entries->start, CR_STATE_RESTORE_NS);
mutex_init(&task_entries->userns_sync_lock);
......
......@@ -7,6 +7,7 @@
struct task_entries {
int nr_threads, nr_tasks, nr_helpers;
atomic_t nr_zombies;
futex_t nr_in_progress;
futex_t start;
atomic_t cr_err;
......
......@@ -803,8 +803,7 @@ static int wait_zombies(struct task_restore_args *task_args)
{
int i;
task_entries->nr_threads -= task_args->zombies_n;
task_entries->nr_tasks -= task_args->zombies_n;
atomic_add(task_args->zombies_n, &task_entries->nr_zombies);
for (i = 0; i < task_args->zombies_n; i++) {
if (sys_waitid(P_PID, task_args->zombies[i], NULL, WNOWAIT | WEXITED, NULL) < 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment