[7/7] restore: Do not iterate over parent's files to find leftovers

Submitted by Kirill Tkhai on June 28, 2017, 3:54 p.m.

Details

Message ID 149866529178.31392.8654773320117269970.stgit@localhost.localdomain
State Accepted
Series "Skip iteration on open files to find parent's leftovers (optimize child creation)"
Commit 94ba1300e032f447f2c2ced5c522faf0f1a44795
Headers show

Commit Message

Kirill Tkhai June 28, 2017, 3:54 p.m.
This patch speeds up creation of child process by disabling
iteration over open files for the most cases. Really, we don't
need that now, as previous patches make parent files do not leak:

mnt namespace fds are stored in fdstore, pid proc files
are closed directly.

So, now we can skip closing old files for the most cases,
except some CLONE_FILES cases: we need that only if parent
have CLONE_FILES in its flags (and for root_item).

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 criu/cr-restore.c |   37 +++++++++++++++++++++++++------------
 1 file changed, 25 insertions(+), 12 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index 8d6b95c50..564bc956b 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -1147,6 +1147,30 @@  static int restore_one_zombie(CoreEntry *core)
 	return -1;
 }
 
+static int setup_newborn_fds(struct pstree_item *me)
+{
+	if (clone_service_fd(rsti(me)->service_fd_id))
+		return -1;
+
+	if (!me->parent ||
+	    (rsti(me->parent)->fdt && !(rsti(me)->clone_flags & CLONE_FILES))) {
+		/*
+		 * When our parent has shared fd table, some of the table owners
+		 * may be already created. Files, they open, will be inherited
+		 * by current process, and here we close them. Also, service fds
+		 * of parent are closed here. And root_item closes the files,
+		 * that were inherited from criu process.
+		 */
+		if (close_old_fds())
+			return -1;
+	}
+
+	if (log_init_by_pid(vpid(me)))
+		return -1;
+
+	return 0;
+}
+
 static int check_core(CoreEntry *core, struct pstree_item *me)
 {
 	int ret = -1;
@@ -1697,12 +1721,7 @@  static int restore_task_with_children(void *_arg)
 	if (current->pid->real < 0)
 		goto err;
 
-	ret = clone_service_fd(rsti(current)->service_fd_id);
-	if (ret)
-		goto err;
-
-	ret = log_init_by_pid(vpid(current));
-	if (ret < 0)
+	if (setup_newborn_fds(current))
 		goto err;
 
 	pid = getpid();
@@ -1713,12 +1732,6 @@  static int restore_task_with_children(void *_arg)
 		goto err;
 	}
 
-	if (!(ca->clone_flags & CLONE_FILES)) {
-		ret = close_old_fds();
-		if (ret)
-			goto err;
-	}
-
 	if (current->parent == NULL) {
 		/*
 		 * The root task has to be in its namespaces before executing