[CRIU] [PATCH 1/2] restore: Fix deadlock when helper's child dies

Dmitry Safonov dsafonov at virtuozzo.com
Wed Jul 5 23:21:45 MSK 2017


Since commit ced9c529f687 ("restore: fix race with helpers' kids dying
too early"), we block SIGCHLD in helper tasks before CR_STATE_RESTORE.
This way we avoided default criu sighandler as it doesn't expect that
childs may die.

This is very racy as we wait on futex for another stage to be started,
but the next stage may start only when all the tasks complete previous
stage. If some children of helper dies, the helper may already have
blocked SIGCHLD and have started sleeping on the futex. Then the next
stage never comes and no one reads a pending SIGCHLD for helper.

A customer met this situation on the node, where the following
(non-related) problem has occured:
Unable to send a fin packet: libnet_write_raw_ipv6(): -1 bytes written (Network is unreachable)
Then child criu of the helper has exited with error-code and the
lockup has happened.

While we could fix it by aborting futex in the end of
restore_task_with_children() for each (non-root also) tasks,
that would be not completely correct:
1. All futex-waiting tasks will wake up after that and they
   may not expect that some tasks are on the previous stage,
   so they will spam into logs with unrelated errors and may
   also die painfully.
2. Child may die and miss aborting of the futex due to:
   o segfault
   o OOM killer
   o User-sended SIGKILL
   o Other error-path we forgot to cover with abort futex

To fix this deadlock in TASK_HELPER, introduce a new handler for
SIGCHLD which will check if the child was expected to die.
It should be as simple and plain as possible and only abort
futex there - we need reentrancy to avoid missing new signals.
No wait for helpers, nothing - only futex aborting.

Signed-off-by: Dmitry Safonov <dsafonov at virtuozzo.com>
---
 criu/cr-restore.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 50 insertions(+), 5 deletions(-)

diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index f9d9b06db731..0da54ce75d41 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -1178,6 +1178,49 @@ out:
 	return ret;
 }
 
+static bool is_child_death_expected(pid_t dead_pid)
+{
+	struct pstree_item *pi;
+
+	list_for_each_entry(pi, &current->children, sibling) {
+		if (dead_pid != vpid(pi))
+			continue;
+
+		switch (pi->pid->state) {
+		case TASK_DEAD:
+		case TASK_HELPER:
+			return true;
+		default:
+			return false;
+		}
+	}
+
+	return false;
+}
+
+/*
+ * SIGCHLD isn't blocked in this handler.
+ * Otherwise we may miss some pending SIGCHLD.
+ * Rather make the handler re-entrant.
+ */
+static void helper_sigchld_handler(int signal, siginfo_t *siginfo, void *data)
+{
+	pid_t pid = siginfo->si_pid;
+
+	if (is_child_death_expected(pid))
+		return;
+
+	/*
+	 * XXX: Not safe to print from sighandler.
+	 * Need something like pr_atomic().
+	 * Good for a while as in the worst case we'll have
+	 * corrupted messages.
+	 */
+	pr_info("Error: Unexpected death of %d\n", pid);
+
+	futex_abort_and_wake(&task_entries->nr_in_progress);
+}
+
 static int restore_one_task(int pid, CoreEntry *core)
 {
 	int i, ret;
@@ -1189,16 +1232,18 @@ static int restore_one_task(int pid, CoreEntry *core)
 	else if (current->pid->state == TASK_DEAD)
 		ret = restore_one_zombie(core);
 	else if (current->pid->state == TASK_HELPER) {
-		sigset_t blockmask, oldmask;
+		struct sigaction act;
 
 		if (prepare_fds(current))
 			return -1;
 
-		sigemptyset(&blockmask);
-		sigaddset(&blockmask, SIGCHLD);
+		sigemptyset(&act.sa_mask); /* Don't block SIGCHLD here! */
+		act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO | SA_RESTART;
+		act.sa_sigaction = helper_sigchld_handler;
 
-		if (sigprocmask(SIG_BLOCK, &blockmask, &oldmask) == -1) {
-			pr_perror("Can not set mask of blocked signals");
+		ret = sigaction(SIGCHLD, &act, NULL);
+		if (ret < 0) {
+			pr_perror("sigaction() failed");
 			return -1;
 		}
 
-- 
2.13.1



More information about the CRIU mailing list