[CRIU] [PATCH v4 4/8] memory: don't use parent memdump if detected possible pid reuse

Pavel Tikhomirov ptikhomirov at virtuozzo.com
Thu Feb 15 13:06:51 MSK 2018


From: ptikhomirov <ptikhomirov at virtuozzo.com>

We have a problem when a pid is reused between consequent dumps we can't
understand if pagemap and pages from images of parent dump are invalid
to restore these pid already. That can lead even to wrong memory
restored for these pid, see the test in last patch.

So these is a try do separate processes with (likely) invalid previous
memory dump from processes with 100% valid previous dump.

For that we use the value of /proc/<pid>/stat's start_time and also the
timestamp of each (pre)dump. If the start time is strictly less than the
timestamp, that means that the pagemap for these pid from previous dump
is valid - was done for exactly the same process.

Creation time is in centiseconds by default so if predump is really fast
(<1csec) we can have false negative decisions for some processes, but in
case of long running processes we are fine.

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

v2: remove __maybe_unused for get_parent_stats; fix get_parent_stats to
have static typing; print warning only if unsure; check has_dump_uptime
v3: read parent stats from image only once; reuse stat from previous
parse_pid_stat call on dump
v4: move code to function; use unsigned long long for ticks; put
proc_pid_stat on mem_dump_ctl; print warning on all pid-reuse cases

Signed-off-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
---
 criu/cr-dump.c     |  2 ++
 criu/include/mem.h |  7 +++++--
 criu/mem.c         | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 criu/stats.c       |  2 +-
 4 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/criu/cr-dump.c b/criu/cr-dump.c
index 9e8425504..c6cc0bfe5 100644
--- a/criu/cr-dump.c
+++ b/criu/cr-dump.c
@@ -1289,6 +1289,7 @@ static int pre_dump_one_task(struct pstree_item *item)
 
 	mdc.pre_dump = true;
 	mdc.lazy = false;
+	mdc.stat = NULL;
 
 	ret = parasite_dump_pages_seized(item, &vmas, &mdc, parasite_ctl);
 	if (ret)
@@ -1448,6 +1449,7 @@ static int dump_one_task(struct pstree_item *item)
 
 	mdc.pre_dump = false;
 	mdc.lazy = opts.lazy_pages;
+	mdc.stat = &pps_buf;
 
 	ret = parasite_dump_pages_seized(item, &vmas, &mdc, parasite_ctl);
 	if (ret)
diff --git a/criu/include/mem.h b/criu/include/mem.h
index bb897c599..99d62f726 100644
--- a/criu/include/mem.h
+++ b/criu/include/mem.h
@@ -4,6 +4,8 @@
 #include <stdbool.h>
 #include "int.h"
 #include "vma.pb-c.h"
+#include "pid.h"
+#include "proc_parse.h"
 
 struct parasite_ctl;
 struct vm_area_list;
@@ -12,8 +14,9 @@ struct pstree_item;
 struct vma_area;
 
 struct mem_dump_ctl {
-	bool	pre_dump;
-	bool	lazy;
+	bool			pre_dump;
+	bool			lazy;
+	struct proc_pid_stat	*stat;
 };
 
 extern bool vma_has_guard_gap_hidden(struct vma_area *vma);
diff --git a/criu/mem.c b/criu/mem.c
index 4c6942a11..caf012759 100644
--- a/criu/mem.c
+++ b/criu/mem.c
@@ -30,9 +30,11 @@
 #include "fault-injection.h"
 #include "prctl.h"
 #include <compel/compel.h>
+#include "proc_parse.h"
 
 #include "protobuf.h"
 #include "images/pagemap.pb-c.h"
+#include "images/stats.pb-c.h"
 
 static int task_reset_dirty_track(int pid)
 {
@@ -290,6 +292,50 @@ static int xfer_pages(struct page_pipe *pp, struct page_xfer *xfer)
 	return ret;
 }
 
+static int detect_pid_reuse(struct pstree_item *item, struct proc_pid_stat* pps)
+{
+	struct proc_pid_stat pps_buf;
+	static StatsEntry *se;
+	unsigned long long tps; /* ticks per second */
+	int ret;
+
+	tps = sysconf(_SC_CLK_TCK);
+	if (tps == -1) {
+		pr_perror("Failed to get clock ticks via sysconf");
+		return -1;
+	}
+
+	if (!pps) {
+		pps = &pps_buf;
+		ret = parse_pid_stat(item->pid->real, pps);
+		if (ret < 0)
+			return -1;
+	}
+
+	if (!se) {
+		se = get_parent_stats();
+		if (!se)
+			return -1;
+	}
+
+	if (se->dump->has_dump_uptime) {
+		unsigned long long dump_ticks;
+
+		dump_ticks = se->dump->dump_uptime/(USEC_PER_SEC/tps);
+
+		if (pps->start_time >= dump_ticks) {
+			/* Print "*" if unsure */
+			pr_warn("Pid reuse%s detected for pid %d\n",
+				pps_buf.start_time == dump_ticks ? "*" : "",
+				item->pid->real);
+			return 1;
+		}
+	} else
+		pr_warn_once("Parent image has no dump timestamp, " \
+			     "pid reuse detection is OFF!\n");
+	return 0;
+}
+
 static int __parasite_dump_pages_seized(struct pstree_item *item,
 		struct parasite_dump_pages_args *args,
 		struct vm_area_list *vma_area_list,
@@ -303,6 +349,7 @@ static int __parasite_dump_pages_seized(struct pstree_item *item,
 	int ret = -1;
 	unsigned cpp_flags = 0;
 	unsigned long pmc_size;
+	int possible_pid_reuse = 0;
 
 	if (opts.check_only)
 		return 0;
@@ -360,6 +407,13 @@ static int __parasite_dump_pages_seized(struct pstree_item *item,
 			xfer.parent = NULL + 1;
 	}
 
+	if (xfer.parent) {
+		possible_pid_reuse = detect_pid_reuse(item, mdc->stat);
+		if (possible_pid_reuse == -1)
+			goto out_xfer;
+	}
+
+
 	/*
 	 * Step 1 -- generate the pagemap
 	 */
@@ -386,7 +440,7 @@ static int __parasite_dump_pages_seized(struct pstree_item *item,
 		else {
 again:
 			ret = generate_iovs(vma_area, pp, map, &off,
-				has_parent);
+				has_parent && !possible_pid_reuse);
 			if (ret == -EAGAIN) {
 				BUG_ON(!(pp->flags & PP_CHUNK_MODE));
 
diff --git a/criu/stats.c b/criu/stats.c
index 4823e48de..4d646c104 100644
--- a/criu/stats.c
+++ b/criu/stats.c
@@ -213,7 +213,7 @@ void write_stats(int what)
 		display_stats(what, &stats);
 }
 
-__maybe_unused StatsEntry *get_parent_stats(void)
+StatsEntry *get_parent_stats(void)
 {
 	struct cr_img *img;
 	StatsEntry *se;
-- 
2.14.3



More information about the CRIU mailing list