[CRIU] [PATCH 22/22] parasite: restore parasite state via rt_sigreturn

Andrey Vagin avagin at openvz.org
Wed May 8 09:28:49 EDT 2013


This patch reduces a window, when a crtools can kill a dumped process,
because if a parasite in a deamon mode can restore the state of the
process, if crtools detached unexpectedly.

All threads are synchronized on the exit point from sys_rt_sigreturn,
for that crtools traces all syscalls.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 parasite-syscall.c | 177 ++++++++++++++++++++++++++++++++---------------------
 pie/parasite.c     |  59 +++++++++++++++---
 2 files changed, 155 insertions(+), 81 deletions(-)

diff --git a/parasite-syscall.c b/parasite-syscall.c
index 44b63bc..36af935 100644
--- a/parasite-syscall.c
+++ b/parasite-syscall.c
@@ -618,8 +618,8 @@ int parasite_init_threads_seized(struct parasite_ctl *ctl, struct pstree_item *i
 		ctl->nr_threads++;
 
 		args->real = tid;
-
-		ret = ptrace(PTRACE_GETREGS, tid, NULL, &ctl->threads[i].regs_orig);
+		args->sigframe = ctl->threads[i].rsigframe;
+		ret = ptrace(PTRACE_GETREGS, item->threads[i].real, NULL, &ctl->threads[i].regs_orig);
 		if (ret) {
 			pr_perror("Can't obtain registers (pid: %d)", tid);
 			goto err;
@@ -641,60 +641,6 @@ err:
 	return -1 ;
 }
 
-int parasite_fini_threads_seized(struct parasite_ctl *ctl)
-{
-	struct parasite_init_args *args;
-	int ret = 0, i, status;
-
-	args = parasite_args(ctl, struct parasite_init_args);
-
-	for (i = 1; i < ctl->nr_threads; i++) {
-		pid_t tid = ctl->threads[i].tid;
-
-		if (!ctl->threads[i].daemonized)
-			break;
-
-		args->real = tid;
-		ret = parasite_execute_daemon_by_pid(PARASITE_CMD_FINI_THREAD, ctl, tid);
-		/*
-		 * Note the thread's fini() can be called even when not
-		 * all threads were init()'ed, say we're rolling back from
-		 * error happened while we were init()'ing some thread, thus
-		 * -ENOENT will be returned but we should continie for the
-		 * rest of threads set.
-		 *
-		 * Strictly speaking we always init() threads in sequence thus
-		 * we could simply break the loop once first -ENOENT returned
-		 * but I prefer to be on a safe side even if some future changes
-		 * would change the code logic.
-		 */
-		if (ret && ret != -ENOENT) {
-			pr_err("Can't fini thread in parasite %d\n", tid);
-			break;
-		} else if (ret == -ENOENT)
-			continue;
-
-		pr_debug("Waiting for %d to trap\n", tid);
-		if (wait4(tid, &status, __WALL, NULL) != tid) {
-			pr_perror("Waited pid mismatch (pid: %d)", tid);
-			break;
-		}
-
-		pr_debug("Daemon %d exited trapping\n", tid);
-		if (!WIFSTOPPED(status)) {
-			pr_err("Task is still running (pid: %d)\n", tid);
-			break;
-		}
-
-		if (ptrace(PTRACE_SETREGS, tid, NULL, &ctl->threads[i].regs_orig)) {
-			pr_perror("Can't restore registers (pid: %d)", tid);
-			return -1;
-		}
-	}
-
-	return ret;
-}
-
 static int block_signals(struct pstree_item *item, struct parasite_ctl *ctl)
 {
 	int ret = 0, i;
@@ -752,23 +698,114 @@ static int unblock_signals(struct parasite_ctl *ctl)
 
 static int parasite_fini_seized(struct parasite_ctl *ctl)
 {
-	struct parasite_init_args *args;
-	int status, ret = 0;
+	int status, ret = 0, i, nr = 0, nr_dmnz = 0;
 
-	args = parasite_args(ctl, struct parasite_init_args);
-	args->real = ctl->pid.real;
+	/* Start to trace syscalls for each thread */
+	for (i = 0; i < ctl->nr_threads; i++) {
+		pid_t pid = ctl->threads[i].tid;
+
+		if (!ctl->threads[i].daemonized)
+			break;
 
-	args->real = ctl->pid.real;
-	__parasite_execute_daemon_by_pid(PARASITE_CMD_FINI, ctl, ctl->pid.real, false);
+		ptrace(PTRACE_INTERRUPT, pid, NULL, NULL);
 
-	if (wait4(ctl->pid.real, &status, __WALL, NULL) != ctl->pid.real) {
-		pr_perror("Waited pid mismatch (pid: %d)", ctl->pid.real);
-		ret = -1;
+		pr_debug("Waiting for %d to trap\n", pid);
+		if (wait4(pid, &status, __WALL, NULL) != pid) {
+			pr_perror("Waited pid mismatch (pid: %d)", pid);
+			return -1;
+		}
+
+		pr_debug("Daemon %d exited trapping\n", pid);
+		if (!WIFSTOPPED(status)) {
+			pr_err("Task is still running (pid: %d)\n", pid);
+			return -1;
+		}
+
+		ret = ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
+		if (ret) {
+			pr_perror("ptrace");
+			return -1;
+		}
+
+		nr_dmnz++;
 	}
 
-	if (!WIFSTOPPED(status)) {
-		pr_err("Task is still running (pid: %d)\n", ctl->pid.real);
-		ret = -1;
+	ret = __parasite_execute_daemon_by_pid(PARASITE_CMD_FINI, ctl, ctl->pid.real, false);
+	if (ret)
+		return -1;
+
+	/* Stop all threads on the enter point in sys_rt_sigreturn */
+	while (1) {
+		user_regs_struct_t regs;
+		pid_t pid;
+
+		pid = wait4(-1, &status, __WALL, NULL);
+		if (pid < 0) {
+			pr_perror("wait4 failed");
+			return -1;
+		}
+
+		pr_debug("%d was trapped\n", pid);
+		if (!WIFSTOPPED(status)) {
+			pr_err("%d\n", status);
+			return -1;
+		}
+		ret = ptrace(PTRACE_GETREGS, pid, NULL, &regs);
+		if (ret) {
+			pr_perror("ptrace");
+			return -1;
+		}
+
+		pr_debug("%d is going to execute the syscall %lx\n", pid, regs.orig_ax);
+		if (regs.orig_ax == __NR_rt_sigreturn) {
+			nr++;
+			pr_debug("%d was stopped\n", pid);
+			if (nr == nr_dmnz)
+				break;
+			continue;
+		}
+
+		ret = ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
+		if (ret) {
+			pr_perror("ptrace");
+			return -1;
+		}
+	}
+
+	/* Stop all threads on the exit point from sys_rt_sigreturn */
+	for (i = 0; i < ctl->nr_threads; i++) {
+		pid_t pid = ctl->threads[i].tid;
+		user_regs_struct_t regs;
+
+		if (!ctl->threads[i].daemonized)
+			break;
+
+		ret = ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
+		if (ret) {
+			pr_perror("ptrace");
+			return -1;
+		}
+
+		if (wait4(pid, &status, __WALL, NULL) != pid) {
+			pr_perror("wait4 failed");
+			return -1;
+		}
+
+		pr_debug("Trap %d\n", pid);
+		if (!WIFSTOPPED(status)) {
+			pr_err("%d\n", status);
+			return -1;
+		}
+		ret = ptrace(PTRACE_GETREGS, pid, NULL, &regs);
+		if (ret) {
+			pr_perror("ptrace");
+			return -1;
+		}
+
+		if (regs.orig_ax != __NR_rt_sigreturn) {
+			pr_debug("%d: syscall mismatch %lx (%x)\n",
+					pid, regs.orig_ax, __NR_rt_sigreturn);
+		}
 	}
 
 	return ret;
@@ -778,10 +815,8 @@ int parasite_cure_seized(struct parasite_ctl *ctl)
 {
 	int ret = 0;
 
-	if (ctl->parasite_ip) {
-		ret = parasite_fini_threads_seized(ctl);
+	if (ctl->parasite_ip)
 		parasite_fini_seized(ctl);
-	}
 
 	ctl->tsock = -1;
 
diff --git a/pie/parasite.c b/pie/parasite.c
index 55b7639..f7aa6eb 100644
--- a/pie/parasite.c
+++ b/pie/parasite.c
@@ -13,7 +13,9 @@
 
 #include <string.h>
 
+#include "asm/types.h"
 #include "asm/parasite.h"
+#include "asm/restorer.h"
 
 static int tsock = -1;
 
@@ -241,6 +243,16 @@ static int init_thread(struct parasite_init_args *args)
 
 static int fini_thread(struct tid_state_s *s)
 {
+	unsigned long new_sp;
+
+	new_sp = (long)s->sigframe + SIGFRAME_OFFSET;
+	pr_debug("%ld: new_sp=%lx ip %lx\n", sys_gettid(),
+		 new_sp, s->sigframe->uc.uc_mcontext.rip);
+
+	ARCH_RT_SIGRETURN(new_sp);
+
+	BUG();
+
 	return 0;
 }
 
@@ -411,13 +423,6 @@ static int parasite_cfg_log(struct parasite_log_args *args)
 	return ret;
 }
 
-static int fini(struct tid_state_s *s)
-{
-	log_set_fd(-1);
-
-	return fini_thread(s);
-}
-
 static int __parasite_daemon_reply_ack(unsigned int id, unsigned int cmd, int err)
 {
 	struct ctl_msg m;
@@ -523,6 +528,37 @@ static int __parasite_execute_thread(struct ctl_msg *m)
 	return s->ret;
 }
 
+static int fini(struct tid_state_s *s, bool block)
+{
+	unsigned long new_sp;
+	int i;
+
+	for (i = 1; i < next_tid_state; i++) {
+		struct ctl_msg m = {.cmd = PARASITE_CMD_FINI_THREAD, .id = tid_state[i].real};
+		__parasite_execute_thread(&m);
+	}
+
+	/*
+	 * Don't unblock signals for thread leaders, because crtools will
+	 * execute munmap for the parasite blob
+	 */
+	if (block)
+		ksigfillset(&RT_SIGFRAME_UC(s->sigframe).uc_sigmask);
+
+	new_sp = (long)s->sigframe + SIGFRAME_OFFSET;
+	pr_debug("%ld: new_sp=%lx ip %lx\n", sys_gettid(),
+		  new_sp, s->sigframe->uc.uc_mcontext.rip);
+
+	sys_close(tsock);
+	log_set_fd(-1);
+
+	ARCH_RT_SIGRETURN(new_sp);
+
+	BUG();
+
+	return -1;
+}
+
 static unsigned long noinline __used
 __parasite_daemon_thread_leader(void *args, struct tid_state_s *s, unsigned long oldstack)
 {
@@ -533,7 +569,7 @@ __parasite_daemon_thread_leader(void *args, struct tid_state_s *s, unsigned long
 
 	/* Reply we're alive */
 	if (__parasite_daemon_reply_ack(s->real, PARASITE_CMD_DAEMONIZE, 0))
-		return oldstack;
+		goto out;
 
 	while (1) {
 		if (__parasite_daemon_wait_msg(&m))
@@ -541,7 +577,7 @@ __parasite_daemon_thread_leader(void *args, struct tid_state_s *s, unsigned long
 
 		switch (m.cmd) {
 		case PARASITE_CMD_FINI:
-			ret = fini(s);
+			ret = fini(s, true);
 			sys_close(tsock);
 			/*
 			 * No ACK here since we're getting out.
@@ -549,7 +585,7 @@ __parasite_daemon_thread_leader(void *args, struct tid_state_s *s, unsigned long
 			break;
 		case PARASITE_CMD_FINI_THREAD:
 			ret = __parasite_execute_thread(&m);
-			break;
+			continue;
 		case PARASITE_CMD_DUMP_THREAD:
 			ret = __parasite_execute_thread(&m);
 			break;
@@ -590,6 +626,9 @@ __parasite_daemon_thread_leader(void *args, struct tid_state_s *s, unsigned long
 			break;
 	}
 
+out:
+	fini(&tid_state[0], false);
+
 	return oldstack;
 }
 
-- 
1.8.2



More information about the CRIU mailing list