[CRIU] [PATCH v4] Try to include userfaultfd with criu

Adrian Reber adrian at lisas.de
Thu Mar 3 11:50:42 PST 2016


From: Adrian Reber <areber at redhat.com>

This is a first try to include userfaultfd with criu. Right now it
still requires a "normal" checkpoint. After checkpointing the application
it can be restored with the help of userfaultfd.

All restored pages with MAP_ANONYMOUS and MAP_PRIVATE set are marked as
being handled by userfaultfd.

As soon as the process is restored it blocks on the first memory access
and waits for pages being transferred by userfaultfd.

To handle the required pages a new criu command has been added. For a
userfaultfd supported restore the first step is to start the
'lazy-pages' server:

  criu lazy-pages -v4 -D /tmp/3/ --address /tmp/userfault.socket

This waits on a unix domain socket (defined using the --address option)
to receive a userfaultfd file descriptor from a '--lazy-pages' enabled
'criu restore':

  criu restore -D /tmp/3 -j -v4 --lazy-pages \
  --address /tmp/userfault.socket

In the first step the VDSO pages are pushed from the lazy-pages server
into the restored process. After that the lazy-pages server waits on the
UFFD FD for a UFFD requested page. If there are no requests received
during a period of 5 seconds the lazy-pages server switches into a mode
where the remaining, non-transferred pages are copied into the
destination process. After all remaining pages have been copied the
lazy-pages server exits.

The first page that usually is requested is a VDSO page. The process
currently used for restoring has two VDSO pages, but only one is requested
via userfaultfd. In the second part where the remaining pages are copied
into the process, the second VDSO page is also copied into the process
as it has not been requested previously. Unfortunately, even as this
page has not been requested before, it is not accepted by userfaultfd.
EINVAL is returned. The reason for EINVAL is not understood and therefore
the VDSO pages are copied first into the process, then switching to request
mode and copying the pages which are requested via userfaultfd. To
decide at which point the VDSO pages can be copied into the process, the
lazy-pages server is currently waiting for the first page requested via
userfaultfd. This is one of the VDSO pages. To not copy a page a second
time, which is unnecessary and not possible, there is now a check to see
if the page has been transferred previously.

The use case to use usefaultfd with a checkpointed process on a remote
machine will probably benefit from the current work related to
image-cache and image-proxy.

For the final implementation it would be nice to have a restore running
in uffd mode on one system which requests the memory pages over the
network from another system which is running 'criu checkpoint' also in
uffd mode. This way the pages need to be copied only 'once' from the
checkpoint process to the uffd restore process.

TODO:
    * Contains still many debug outputs which need to be cleaned up.
    * Maybe transfer the dump directory FD also via unix domain sockets
      so that the 'uffd'/'lazy-pages' server can keep running without
      the need to specify the dump directory with '-D'
    * Keep the lazy-pages server running after all pages have been
      transferred and start waiting for new connections to serve.
    * Resurrect the non-cooperative patch set, as once the restored task
      fork()'s or calls mremap() the whole thing becomes broken.
    * Figure out if current VDSO handling is correct.
    * Figure out when and how zero pages need to be inserted via uffd.

v2:
    * provide option '--lazy-pages' to enable uffd style restore
    * use send_fd()/recv_fd() provided by criu (instead of own
      implementation)
    * do not install the uffd as service_fd
    * use named constants for MAP_ANONYMOUS
    * do not restore memory pages and then later mark them as uffd
      handled
    * remove function find_pages() to search in pages-<id>.img;
      now using criu functions to find the necessary pages;
      for each new page search the pages-<id>.img file is opened
    * only check the UFFDIO_API once
    * trying to protect uffd code by CONFIG_UFFD;
      use make UFFD=1 to compile criu with this patch

v3:
   * renamed the server mode from 'uffd' -> 'lazy-pages'
   * switched client and server roles transferring the UFFD FD
     * the criu part running in lazy-pages server mode is now
       waiting for connections
     * the criu restore process connects to the lazy-pages server
       to pass the UFFD FD
   * before UFFD copying anything else the VDSO pages are copied
     as it fails to copy unused VDSO pages once the process is running.
     this was necessary to be able to copy all pages.
   * if there are no more UFFD messages for 5 seconds the lazy-pages
     server switches in copy mode to copy all remaining pages, which
     have not been requested yet, into the restored process
   * check the UFFDIO_API at the correct place
   * close UFFD FD in the restorer to remove open UFFD FD in the
     restored process

v4:
    * removed unnecessary madvise() calls ; it seemed necessary when
      first running tests with uffd; it actually is not necessary
    * auto-detect if build-system provides linux/userfaultfd.h
      header.
    * simplify unix domain socket setup and communication.
    * use --address to specify the location of the used
      unix domain socket.

Signed-off-by: Adrian Reber <areber at redhat.com>
---
 criu/Makefile.config      |   6 +-
 criu/Makefile.crtools     |   4 +
 criu/cr-restore.c         |  95 +++++++++-
 criu/crtools.c            |  20 +++
 criu/include/cr_options.h |   1 +
 criu/include/crtools.h    |   6 +
 criu/include/page-read.h  |   1 +
 criu/include/restorer.h   |   2 +
 criu/include/uffd.h       |  16 ++
 criu/page-read.c          |   2 +
 criu/pie/restorer.c       |  69 +++++++-
 criu/uffd.c               | 439 ++++++++++++++++++++++++++++++++++++++++++++++
 scripts/feature-tests.mak |  15 ++
 13 files changed, 671 insertions(+), 5 deletions(-)
 create mode 100644 criu/include/uffd.h
 create mode 100644 criu/uffd.c

diff --git a/criu/Makefile.config b/criu/Makefile.config
index aaaca1f..c3841b9 100644
--- a/criu/Makefile.config
+++ b/criu/Makefile.config
@@ -14,8 +14,12 @@ ifeq ($(call pkg-config-check,libselinux),y)
         DEFINES	+= -DCONFIG_HAS_SELINUX
 endif
 
+ifeq ($(call try-cc,$(FEATURE_TEST_UFFD)),y)
+	export UFFD := 1
+endif
+
 FEATURES_LIST	:= TCP_REPAIR PRLIMIT STRLCPY STRLCAT PTRACE_PEEKSIGINFO \
-	SETPROCTITLE_INIT MEMFD
+	SETPROCTITLE_INIT MEMFD UFFD
 
 # $1 - config name
 define gen-feature-test
diff --git a/criu/Makefile.crtools b/criu/Makefile.crtools
index 4448047..f1573d1 100644
--- a/criu/Makefile.crtools
+++ b/criu/Makefile.crtools
@@ -81,6 +81,10 @@ obj-y			+= pie-util-vdso.o
 obj-y			+= vdso.o
 endif
 
+ifeq ($(UFFD),1)
+obj-y	+= uffd.o
+endif
+
 PROTOBUF_GEN := $(SRC_DIR)/scripts/protobuf-gen.sh
 
 protobuf-desc.c: protobuf-desc-gen.h
diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index 8c752d1..d572db4 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -19,6 +19,7 @@
 #include <sys/shm.h>
 #include <sys/mount.h>
 #include <sys/prctl.h>
+#include <sys/syscall.h>
 
 #include <sched.h>
 
@@ -76,6 +77,8 @@
 #include "seccomp.h"
 #include "bitmap.h"
 #include "fault-injection.h"
+#include "uffd.h"
+
 #include "parasite-syscall.h"
 
 #include "protobuf.h"
@@ -469,6 +472,17 @@ static int restore_priv_vma_content(void)
 			p = decode_pointer((off) * PAGE_SIZE +
 					vma->premmaped_addr);
 
+			/*
+			 * This means that userfaultfd is used to load the pages
+			 * on demand.
+			 */
+			if (opts.lazy_pages && (vma->e->flags & MAP_ANONYMOUS) &&
+					(vma->e->flags & MAP_PRIVATE)) {
+				pr_debug("Lazy restore skips %lx\n", vma->e->start);
+				pr.skip_pages(&pr, PAGE_SIZE);
+				continue;
+			}
+
 			set_bit(off, vma->page_bitmap);
 			if (vma->ppage_bitmap) { /* inherited vma */
 				clear_bit(off, vma->ppage_bitmap);
@@ -2131,7 +2145,7 @@ out:
 	return -1;
 }
 
-static int prepare_task_entries(void)
+int prepare_task_entries(void)
 {
 	task_entries_pos = rst_mem_align_cpos(RM_SHREMAP);
 	task_entries = rst_mem_alloc(sizeof(*task_entries), RM_SHREMAP);
@@ -2964,6 +2978,54 @@ static int rst_prep_creds(pid_t pid, CoreEntry *core, unsigned long *creds_pos)
 	return 0;
 }
 
+#ifdef CONFIG_HAS_UFFD
+
+static int send_uffd(int sendfd)
+{
+	int fd;
+	int len;
+	int ret = -1;
+	struct sockaddr_un sun;
+
+	if (!opts.addr) {
+		pr_info("Please specify a file name for the unix domain socket\n");
+		pr_info("used to communicate between the lazy-pages server\n");
+		pr_info("and the restore process. Use the --address option like\n");
+		pr_info("criu restore --lazy-pages --address /tmp/userfault.socket\n");
+		return -1;
+	}
+
+	if (sendfd < 0)
+		return -1;
+
+	if (strlen(opts.addr) >= sizeof(sun.sun_path)) {
+		return -1;
+	}
+
+	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
+		return -1;
+
+	memset(&sun, 0, sizeof(sun));
+	sun.sun_family = AF_UNIX;
+	strcpy(sun.sun_path, opts.addr);
+	len = offsetof(struct sockaddr_un, sun_path) + strlen(opts.addr);
+	if (connect(fd, (struct sockaddr *) &sun, len) < 0) {
+		pr_perror("connect to %s failed", opts.addr);
+		goto out;
+	}
+
+	if (send_fd(fd, NULL, 0, sendfd) < 0) {
+		pr_perror("send_fd error:");
+		goto out;
+	}
+	ret = 0;
+out:
+	close(fd);
+	return ret;
+}
+
+#endif /* CONFIG_HAS_UFFD */
+
 static int sigreturn_restore(pid_t pid, CoreEntry *core)
 {
 	void *mem = MAP_FAILED;
@@ -3219,6 +3281,37 @@ static int sigreturn_restore(pid_t pid, CoreEntry *core)
 
 	strncpy(task_args->comm, core->tc->comm, sizeof(task_args->comm));
 
+	if (!opts.lazy_pages)
+		task_args->uffd = -1;
+#ifdef CONFIG_HAS_UFFD
+	else {
+		struct uffdio_api uffdio_api;
+		/*
+		 * Open userfaulfd FD which is passed to the restorer blob and
+		 * to a second process handling the userfaultfd page faults.
+		 */
+		task_args->uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
+
+		/*
+		 * Check if the UFFD_API is the one which is expected
+		 */
+		uffdio_api.api = UFFD_API;
+		uffdio_api.features = 0;
+		if (ioctl(task_args->uffd, UFFDIO_API, &uffdio_api)) {
+			pr_err("Checking for UFFDIO_API failed.\n");
+			goto err;
+		}
+		if (uffdio_api.api != UFFD_API) {
+			pr_err("Result of looking up UFFDIO_API does not match: %Lu\n", uffdio_api.api);
+			goto err;
+		}
+
+		if (send_uffd(task_args->uffd) < 0) {
+			close(task_args->uffd);
+			goto err;
+		}
+	}
+#endif
 
 	/*
 	 * Fill up per-thread data.
diff --git a/criu/crtools.c b/criu/crtools.c
index d6e8672..30329a1 100644
--- a/criu/crtools.c
+++ b/criu/crtools.c
@@ -275,6 +275,9 @@ int main(int argc, char *argv[], char *envp[])
 		{ "timeout",			required_argument,	0, 1072 },
 		{ "external",			required_argument,	0, 1073	},
 		{ "empty-ns",			required_argument,	0, 1074	},
+#ifdef CONFIG_HAS_UFFD
+		{ "lazy-pages",			no_argument,		0, 1075 },
+#endif
 		{ },
 	};
 
@@ -524,6 +527,11 @@ int main(int argc, char *argv[], char *envp[])
 		case 1072:
 			opts.timeout = atoi(optarg);
 			break;
+#ifdef CONFIG_HAS_UFFD
+		case 1075:
+			opts.lazy_pages = true;
+			break;
+#endif
 		case 'M':
 			{
 				char *aux;
@@ -680,6 +688,9 @@ int main(int argc, char *argv[], char *envp[])
 		return -1;
 	}
 
+	if (!strcmp(argv[optind], "lazy-pages"))
+		return uffd_listen() != 0;
+
 	if (!strcmp(argv[optind], "check"))
 		return cr_check() != 0;
 
@@ -720,6 +731,9 @@ usage:
 "  criu page-server\n"
 "  criu service [<options>]\n"
 "  criu dedup\n"
+#ifdef CONFIG_HAS_UFFD
+"  criu lazy-pages -D DIR [<options>]\n"
+#endif
 "\n"
 "Commands:\n"
 "  dump           checkpoint a process/tree identified by pid\n"
@@ -758,6 +772,12 @@ usage:
 "                        restore making it the parent of the restored process\n"
 "  --freeze-cgroup\n"
 "                        use cgroup freezer to collect processes\n"
+#ifdef CONFIG_HAS_UFFD
+"  --lazy-pages          restore pages on demand\n"
+"                        this requires running a second instance of criu\n"
+"                        in lazy-pages mode: 'criu lazy-pages -D DIR'\n"
+"                        --lazy-pages and lazy-pages mode require userfaultfd\n"
+#endif
 "\n"
 "* Special resources support:\n"
 "  -x|--" USK_EXT_PARAM "inode,.." "      allow external unix connections (optionally can be assign socket's inode that allows one-sided dump)\n"
diff --git a/criu/include/cr_options.h b/criu/include/cr_options.h
index a6f0b3e..d07c104 100644
--- a/criu/include/cr_options.h
+++ b/criu/include/cr_options.h
@@ -107,6 +107,7 @@ struct cr_options {
 	char			*lsm_profile;
 	unsigned int		timeout;
 	unsigned int		empty_ns;
+	bool			lazy_pages;
 };
 
 extern struct cr_options opts;
diff --git a/criu/include/crtools.h b/criu/include/crtools.h
index b369a02..49ba0cc 100644
--- a/criu/include/crtools.h
+++ b/criu/include/crtools.h
@@ -25,6 +25,12 @@ extern int convert_to_elf(char *elf_path, int fd_core);
 extern int cr_check(void);
 extern int cr_exec(int pid, char **opts);
 extern int cr_dedup(void);
+#ifdef CONFIG_HAS_UFFD
+extern int uffd_listen(void);
+#else
+static inline int uffd_listen() { return 0; };
+#endif /* CONFIG_HAS_UFFD */
+extern int prepare_task_entries(void);
 
 extern int check_add_feature(char *arg);
 
diff --git a/criu/include/page-read.h b/criu/include/page-read.h
index 8951a72..3ba1ee9 100644
--- a/criu/include/page-read.h
+++ b/criu/include/page-read.h
@@ -51,6 +51,7 @@ struct page_read {
 	/* stop working on current pagemap */
 	void (*put_pagemap)(struct page_read *);
 	void (*close)(struct page_read *);
+	void (*skip_pages)(struct page_read *, unsigned long len);
 
 	/* Private data of reader */
 	struct cr_img *pmi;
diff --git a/criu/include/restorer.h b/criu/include/restorer.h
index 9896aa1..86236b0 100644
--- a/criu/include/restorer.h
+++ b/criu/include/restorer.h
@@ -124,6 +124,8 @@ struct task_restore_args {
 	int				logfd;
 	unsigned int			loglevel;
 
+	int				uffd;
+
 	/* threads restoration */
 	int				nr_threads;		/* number of threads */
 	thread_restore_fcall_t		clone_restore_fn;	/* helper address for clone() call */
diff --git a/criu/include/uffd.h b/criu/include/uffd.h
new file mode 100644
index 0000000..d5a043b
--- /dev/null
+++ b/criu/include/uffd.h
@@ -0,0 +1,16 @@
+#ifndef __CR_UFFD_H_
+#define __CR_UFFD_H_
+
+#include "config.h"
+
+#ifdef CONFIG_HAS_UFFD
+
+#include <syscall.h>
+#include <linux/userfaultfd.h>
+
+#ifndef __NR_userfaultfd
+#error "missing __NR_userfaultfd definition"
+#endif
+#endif /* CONFIG_HAS_UFFD */
+
+#endif /* __CR_UFFD_H_ */
diff --git a/criu/page-read.c b/criu/page-read.c
index a185265..e5ec76a 100644
--- a/criu/page-read.c
+++ b/criu/page-read.c
@@ -328,6 +328,7 @@ int open_page_read_at(int dfd, int pid, struct page_read *pr, int pr_flags)
 	pr->put_pagemap = put_pagemap;
 	pr->read_pages = read_pagemap_page;
 	pr->close = close_page_read;
+	pr->skip_pages = skip_pagemap_pages;
 	pr->id = ids++;
 
 	pr_debug("Opened page read %u (parent %u)\n",
@@ -350,6 +351,7 @@ open_old:
 	pr->read_pages = read_page;
 	pr->pi = NULL;
 	pr->close = close_page_read;
+	pr->skip_pages = NULL;
 
 	return 1;
 }
diff --git a/criu/pie/restorer.c b/criu/pie/restorer.c
index f7bde75..617b2f6 100644
--- a/criu/pie/restorer.c
+++ b/criu/pie/restorer.c
@@ -25,6 +25,7 @@
 #include "image.h"
 #include "sk-inet.h"
 #include "vma.h"
+#include "uffd.h"
 
 #include "crtools.h"
 #include "lock.h"
@@ -567,7 +568,44 @@ static void rst_tcp_socks_all(struct task_restore_args *ta)
 		rst_tcp_repair_off(&ta->tcp_socks[i]);
 }
 
-static int vma_remap(unsigned long src, unsigned long dst, unsigned long len)
+
+
+
+static void enable_uffd(int uffd, unsigned long addr, unsigned long len)
+{
+	/*
+	 * If uffd == -1, this means that userfaultfd is not enabled
+	 * or it is not available.
+	 */
+	if (uffd == -1)
+		return;
+#ifdef CONFIG_HAS_UFFD
+	int rc;
+	struct uffdio_register uffdio_register;
+	unsigned long expected_ioctls;
+
+	uffdio_register.range.start = addr;
+	uffdio_register.range.len = len;
+	uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
+	pr_info("lazy-pages: uffdio_register.range.start 0x%lx\n", (unsigned long) uffdio_register.range.start);
+	pr_info("lazy-pages: uffdio_register.len 0x%llx\n", uffdio_register.range.len);
+	rc = sys_ioctl(uffd, UFFDIO_REGISTER, &uffdio_register);
+	pr_info("lazy-pages: ioctl UFFDIO_REGISTER rc %d\n", rc);
+	pr_info("lazy-pages: uffdio_register.range.start 0x%lx\n", (unsigned long) uffdio_register.range.start);
+	pr_info("lazy-pages: uffdio_register.len 0x%llx\n", uffdio_register.range.len);
+
+	expected_ioctls = (1 << _UFFDIO_WAKE) | (1 << _UFFDIO_COPY) | (1 << _UFFDIO_ZEROPAGE);
+
+	if ((uffdio_register.ioctls & expected_ioctls) != expected_ioctls) {
+		pr_err("lazy-pages: unexpected missing uffd ioctl for anon memory\n");
+	}
+
+#endif
+
+}
+
+
+static int vma_remap(unsigned long src, unsigned long dst, unsigned long len, int uffd, int flags)
 {
 	unsigned long guard = 0, tmp;
 
@@ -640,6 +678,17 @@ static int vma_remap(unsigned long src, unsigned long dst, unsigned long len)
 		return -1;
 	}
 
+	/*
+	 * If running in userfaultfd/lazy-pages mode pages with
+	 * MAP_ANONYMOUS and MAP_PRIVATE are remapped but without the
+	 * real content.
+	 * The function enable_uffd() marks the page(s) as userfaultfd
+	 * pages, so that the processes will hang until the memory is
+	 * injected via userfaultfd.
+	 */
+	if ((flags & MAP_PRIVATE) && (flags & MAP_ANONYMOUS))
+		enable_uffd(uffd, dst, len);
+
 	return 0;
 }
 
@@ -898,6 +947,10 @@ long __export_restore_task(struct task_restore_args *args)
 
 	pr_info("Switched to the restorer %d\n", my_pid);
 
+	if (args->uffd > -1) {
+		pr_debug("lazy-pages: uffd %d\n", args->uffd);
+	}
+
 	if (vdso_do_park(&args->vdso_sym_rt, args->vdso_rt_parked_at, vdso_rt_size))
 		goto core_restore_end;
 
@@ -919,7 +972,7 @@ long __export_restore_task(struct task_restore_args *args)
 			break;
 
 		if (vma_remap(vma_premmaped_start(vma_entry),
-				vma_entry->start, vma_entry_len(vma_entry)))
+				vma_entry->start, vma_entry_len(vma_entry), args->uffd, vma_entry->flags))
 			goto core_restore_end;
 	}
 
@@ -937,10 +990,20 @@ long __export_restore_task(struct task_restore_args *args)
 			break;
 
 		if (vma_remap(vma_premmaped_start(vma_entry),
-				vma_entry->start, vma_entry_len(vma_entry)))
+				vma_entry->start, vma_entry_len(vma_entry), args->uffd, vma_entry->flags))
 			goto core_restore_end;
 	}
 
+	if (args->uffd > -1) {
+		pr_debug("lazy-pages: closing uffd %d\n", args->uffd);
+		/*
+		 * All userfaultfd configuration has finished at this point.
+		 * Let's close the UFFD file descriptor, so that the restored
+		 * process does not have an opened UFFD FD for ever.
+		 */
+		sys_close(args->uffd);
+	}
+
 	/*
 	 * OK, lets try to map new one.
 	 */
diff --git a/criu/uffd.c b/criu/uffd.c
new file mode 100644
index 0000000..68fcf3d
--- /dev/null
+++ b/criu/uffd.c
@@ -0,0 +1,439 @@
+#include <stddef.h>
+#include <stdio.h>
+#include <errno.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <string.h>
+#include <time.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <sys/ioctl.h>
+#include <sys/un.h>
+#include <sys/socket.h>
+
+#include "asm/page.h"
+#include "include/log.h"
+#include "include/criu-plugin.h"
+#include "include/page-read.h"
+#include "include/files-reg.h"
+#include "include/mem.h"
+#include "include/uffd.h"
+#include "include/util-pie.h"
+#include "include/pstree.h"
+#include "include/crtools.h"
+#include "include/cr_options.h"
+#include "xmalloc.h"
+
+#undef  LOG_PREFIX
+#define LOG_PREFIX "lazy-pages: "
+
+static int server_listen(struct sockaddr_un *saddr)
+{
+	int fd;
+	int len;
+
+	if (strlen(opts.addr) >= sizeof(saddr->sun_path)) {
+		return -1;
+	}
+
+	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
+		return -1;
+
+	unlink(opts.addr);
+
+	memset(saddr, 0, sizeof(struct sockaddr_un));
+	saddr->sun_family = AF_UNIX;
+	strcpy(saddr->sun_path, opts.addr);
+	len = offsetof(struct sockaddr_un, sun_path) + strlen(opts.addr);
+
+	if (bind(fd, (struct sockaddr *) saddr, len) < 0) {
+		goto out;
+	}
+
+	if (listen(fd, 10) < 0) {
+		goto out;
+	}
+
+	return fd;
+
+out:
+	close(fd);
+	return -1;
+}
+
+static int ud_open()
+{
+	int client;
+	int listen;
+	int newfd;
+	int ret = -1;
+	struct sockaddr_un saddr;
+	socklen_t len;
+
+	if ((listen = server_listen(&saddr)) < 0) {
+		pr_perror("server_listen error");
+		return -1;
+	}
+
+	/* accept new client request */
+	len = sizeof(struct sockaddr_un);
+	if ((client = accept(listen, &saddr, &len)) < 0) {
+		pr_perror("server_accept error: %d", client);
+		close(listen);
+		return -1;
+	}
+
+	pr_debug("client fd %d\n", client);
+	newfd = recv_fd(client);
+	if (newfd < 0) {
+		pr_perror("recv_fd error:");
+		goto out;
+	}
+	pr_debug("newfd %d\n", newfd);
+	close(client);
+
+	return newfd;
+out:
+	close(listen);
+	close(client);
+	return ret;
+}
+
+static void get_page(unsigned long addr, void *dest, struct page_read *pr)
+{
+	struct iovec iov;
+	int ret;
+	unsigned char buf[PAGE_SIZE];
+
+	ret = open_page_read(root_item->pid.virt, pr, PR_TASK | PR_MOD);
+	pr_debug("ret %d\n", ret);
+
+	/* TODO: return code checking */
+	ret = pr->get_pagemap(pr, &iov);
+	pr_debug("get_pagemap  ret %d\n", ret);
+	ret = seek_pagemap_page(pr, addr, true);
+	pr_debug("seek_pagemap_page %x\n", ret);
+	ret = pr->read_pages(pr, addr, 1, buf);
+	pr_debug("read_pages ret %d\n", ret);
+	memcpy(dest, buf, PAGE_SIZE);
+	pr->close(pr);
+}
+
+#define UFFD_FLAG_SENT	0x1
+#define UFFD_FLAG_VDSO	0x2
+
+struct uffd_pages_struct {
+        struct list_head                list;
+        unsigned long                   addr;
+        int                             flags;
+};
+
+static int uffd_copy_page(int uffd, struct page_read *pr, __u64 address,
+			  void *dest)
+{
+	struct uffdio_copy uffdio_copy;
+	int rc;
+
+	get_page(address, dest, pr);
+
+	uffdio_copy.dst = address;
+	uffdio_copy.src = (unsigned long) dest;
+	uffdio_copy.len = page_size();
+	uffdio_copy.mode = 0;
+	uffdio_copy.copy = 0;
+
+	pr_debug("uffdio_copy.dst 0x%llx\n", uffdio_copy.dst);
+	rc = ioctl(uffd, UFFDIO_COPY, &uffdio_copy);
+	pr_debug("ioctl UFFDIO_COPY rc 0x%x\n", rc);
+	pr_debug("uffdio_copy.copy 0x%llx\n", uffdio_copy.copy);
+	if (rc) {
+		/* real retval in ufdio_copy.copy */
+		if (uffdio_copy.copy != -EEXIST) {
+			pr_err("UFFDIO_COPY error %Ld\n", uffdio_copy.copy);
+			return -1;
+		}
+	} else if (uffdio_copy.copy != page_size()) {
+		pr_err("UFFDIO_COPY unexpected size %Ld\n", uffdio_copy.copy);
+		return -1;
+	}
+
+
+	return uffdio_copy.copy;
+
+}
+
+static int collect_uffd_pages(struct page_read *pr, struct list_head * uffd_list, unsigned long *vma_size)
+{
+	unsigned long base;
+	int i;
+	struct iovec iov;
+	unsigned long nr_pages;
+	unsigned long ps;
+	int rc;
+	struct uffd_pages_struct *uffd_pages;
+	struct vma_area *vma;
+	struct vm_area_list *vmas = &rsti(root_item)->vmas;
+
+	rc = pr->get_pagemap(pr, &iov);
+	if (rc <= 0)
+		return 0;
+
+	ps = page_size();
+	nr_pages = iov.iov_len / ps;
+	base = (unsigned long) iov.iov_base;
+	pr_debug("iov.iov_base 0x%lx (%ld pages)\n", base, nr_pages);
+
+	if (pr->put_pagemap)
+		pr->put_pagemap(pr);
+
+	for (i = 0; i < nr_pages; i++) {
+		bool uffd_page = false;
+		bool uffd_vdso = false;
+		base = (unsigned long) iov.iov_base + (i * ps);
+		/*
+		 * Only pages which are MAP_ANONYMOUS and MAP_PRIVATE
+		 * are relevant for userfaultfd handling.
+		 * Loop over all VMAs to see if the flags matching.
+		 */
+		list_for_each_entry(vma, &vmas->h, list) {
+			/*
+			 * This loop assumes that base can actually be found
+			 * in the VMA list.
+			 */
+			if (base >= vma->e->start && base < vma->e->end) {
+				if ((vma->e->flags & MAP_ANONYMOUS) &&
+				    (vma->e->flags & MAP_PRIVATE) &&
+				    !(vma_area_is(vma, VMA_AREA_VSYSCALL))) {
+					uffd_page = true;
+					if (vma_area_is(vma, VMA_AREA_VDSO))
+						uffd_vdso = true;
+					break;
+				}
+			}
+		}
+
+		/* This is not a page we are looking for. Move along */
+		if (!uffd_page)
+			continue;
+
+		pr_debug("Adding 0x%lx to our list\n", base);
+
+		*vma_size += ps;
+		uffd_pages = xzalloc(sizeof(struct uffd_pages_struct));
+		if (!uffd_pages)
+			return -1;
+		uffd_pages->addr = base;
+		if (uffd_vdso)
+			uffd_pages->flags |= UFFD_FLAG_VDSO;
+		list_add(&uffd_pages->list, uffd_list);
+	}
+
+	return 1;
+}
+
+/*
+ *  Setting up criu infrastructure to easily
+ *  access the dump results.
+ */
+static void criu_init()
+{
+	/* TODO: return code checking */
+	check_img_inventory();
+	prepare_task_entries();
+	prepare_pstree();
+	collect_remaps_and_regfiles();
+	prepare_shared_reg_files();
+	prepare_remaps();
+	prepare_mm_pid(root_item);
+
+	/* We found a PID */
+	pr_debug("root_item->pid.virt %d\n", root_item->pid.virt);
+	pr_debug("root_item->pid.real %d\n", root_item->pid.real);
+}
+
+int uffd_listen()
+{
+	__u64 address;
+	void *dest;
+	__u64 flags;
+	struct uffd_msg msg;
+	struct page_read pr;
+	unsigned long ps;
+	int rc;
+	fd_set set;
+	struct timeval timeout;
+	int uffd;
+	unsigned long uffd_copied_pages = 0;
+	int uffd_flags;
+	struct uffd_pages_struct *uffd_pages;
+	bool vdso_sent = false;
+	unsigned long vma_size = 0;
+
+	LIST_HEAD(uffd_list);
+
+	if (!opts.addr) {
+		pr_info("Please specify a file name for the unix domain socket\n");
+		pr_info("used to communicate between the lazy-pages server\n");
+		pr_info("and the restore process. Use the --address option like\n");
+		pr_info("criu --lazy-pages --address /tmp/userfault.socket\n");
+		return -1;
+	}
+
+	pr_debug("Waiting for incoming connections on %s\n", opts.addr);
+	if ((uffd = ud_open()) < 0)
+		exit(0);
+
+	pr_debug("uffd is 0x%d\n", uffd);
+	uffd_flags = fcntl(uffd, F_GETFD, NULL);
+	pr_debug("uffd_flags are 0x%x\n", uffd_flags);
+
+	/* Setting up criu infrastructure to easily access the dump results */
+	criu_init();
+
+	/* Initialize FD sets for read() with timeouts (using select()) */
+	FD_ZERO(&set);
+	FD_SET(uffd, &set);
+
+	/* All operations will be done on page size */
+	ps = page_size();
+	dest = malloc(ps);
+
+	rc = open_page_read(root_item->pid.virt, &pr, PR_TASK);
+	if (rc <= 0)
+		return 1;
+	/*
+	 * This puts all pages which should be handled by userfaultfd
+	 * in the list uffd_list. This list is later used to detect if
+	 * a page has already been transferred or if it needs to be
+	 * pushed into the process using userfaultfd.
+	 */
+	do {
+		rc = collect_uffd_pages(&pr, &uffd_list, &vma_size);
+		if (rc == -1)
+			return 1;
+	} while (rc);
+
+	if (pr.close)
+		pr.close(&pr);
+
+	while (1) {
+		bool page_sent = false;
+		/*
+		 * Setting the timeout to 5 seconds. If after this time
+		 * no uffd pages are requested the code switches to
+		 * copying the remaining pages.
+		 *
+		 * Timeout is re-defined every time select() is run as
+		 * select(2) says:
+		 *  Consider timeout to be undefined after select() returns.
+		 */
+		timeout.tv_sec = 5;
+		timeout.tv_usec = 0;
+		rc = select(uffd + 1, &set, NULL, NULL, &timeout);
+		pr_debug("select() rc: 0x%x\n", rc);
+		if (rc == 0) {
+			pr_debug("read timeout\n");
+			pr_debug("switching from request to copy mode\n");
+			break;
+		}
+		rc = read(uffd, &msg, sizeof(msg));
+		pr_debug("read() rc: 0x%x\n", rc);
+
+		if (rc != sizeof(msg)) {
+			if (rc < 0)
+				pr_perror("read error");
+			else
+				pr_debug("short read\n");
+			continue;
+		}
+
+		/* Align requested address to the next page boundary */
+		address = msg.arg.pagefault.address & ~(ps - 1);
+		pr_debug("msg.arg.pagefault.address 0x%llx\n", address);
+
+		/*
+		 * At this point the process on the other side waits for the first page.
+		 * In the first step we will force the vdso pages into the new process.
+		 */
+		if (!vdso_sent) {
+			pr_debug("Pushing VDSO pages once\n");
+			list_for_each_entry(uffd_pages, &uffd_list, list) {
+				if (!(uffd_pages->flags & UFFD_FLAG_VDSO))
+					continue;
+				rc = uffd_copy_page(uffd, &pr, uffd_pages->addr,
+						    dest);
+				if (rc < 0) {
+					pr_err("Error during UFFD copy\n");
+					return 1;
+				}
+				vma_size -= rc;
+				uffd_copied_pages++;
+				uffd_pages->flags |= UFFD_FLAG_SENT;
+			}
+			vdso_sent = true;
+		}
+
+		/* Make sure to not transfer a page twice */
+		list_for_each_entry(uffd_pages, &uffd_list, list) {
+			if ((uffd_pages->addr == address) &&
+			    (uffd_pages->flags & UFFD_FLAG_SENT)) {
+				page_sent = true;
+				break;
+			}
+		}
+
+		if (page_sent)
+			continue;
+
+		/* Now handle the pages actually requested. */
+
+		flags = msg.arg.pagefault.flags;
+		pr_debug("msg.arg.pagefault.flags 0x%llx\n", flags);
+
+		if (msg.event != UFFD_EVENT_PAGEFAULT) {
+			pr_err("unexpected msg event %u\n", msg.event);
+			return 1;
+		}
+
+		rc = uffd_copy_page(uffd, &pr, address, dest);
+		if (rc < 0) {
+			pr_err("Error during UFFD copy\n");
+			return 1;
+		}
+		vma_size -= rc;
+		uffd_copied_pages++;
+
+		/*
+		 * Mark this page as having been already transferred, so
+		 * that it has not to be copied again later.
+		 */
+		list_for_each_entry(uffd_pages, &uffd_list, list) {
+			if (uffd_pages->addr == address)
+				uffd_pages->flags |= UFFD_FLAG_SENT;
+		}
+	}
+	pr_debug("remaining vma_size: 0x%lx\n", vma_size);
+	pr_debug("uffd_copied_pages:    %ld\n", uffd_copied_pages);
+	list_for_each_entry(uffd_pages, &uffd_list, list) {
+		pr_debug("Checking remaining pages 0x%lx (flags 0x%x)\n",
+			 uffd_pages->addr, uffd_pages->flags);
+		if (uffd_pages->flags & UFFD_FLAG_SENT)
+			continue;
+
+		rc = uffd_copy_page(uffd, &pr, uffd_pages->addr, dest);
+		if (rc < 0) {
+			pr_err("Error during UFFD copy\n");
+			return 1;
+		}
+		vma_size -= rc;
+
+		pr_debug("remaining vma_size: 0x%lx\n", vma_size);
+		pr_debug("uffd_copied_pages:    %ld\n", ++uffd_copied_pages);
+		uffd_pages->flags |= UFFD_FLAG_SENT;
+	}
+	close(uffd);
+	return 0;
+}
diff --git a/scripts/feature-tests.mak b/scripts/feature-tests.mak
index 525c73e..51a54de 100644
--- a/scripts/feature-tests.mak
+++ b/scripts/feature-tests.mak
@@ -108,3 +108,18 @@ int main(void)
 }
 
 endef
+
+define FEATURE_TEST_UFFD
+
+#include <syscall.h>
+#include <linux/userfaultfd.h>
+
+int main(void)
+{
+#ifndef __NR_userfaultfd
+#error "missing __NR_userfaultfd definition"
+#endif
+	return 0;
+}
+
+endef
-- 
1.8.3.1



More information about the CRIU mailing list