Commit e2268aa3 authored by Adrian Reber's avatar Adrian Reber Committed by Andrei Vagin

Try to include userfaultfd with criu (part 1)

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 is part 1 of the userfaultfd integration which provides the
'lazy-pages' server implementation.

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

v5:
    * split the userfaultfd patch in multiple smaller patches
    * introduced vma_can_be_lazy() function to check if a page
      can be handled by uffd
    * moved uffd related code from cr-restore.c to uffd.c
    * handle failure to register a memory page of the restored process
      with userfaultfd

v6:
    * get PID of to be restored process from the 'criu restore' process;
      first the PID is transferred and then the UFFD
    * code has been re-ordered to be better prepared for lazy-restore
      from remote host
    * compile test for UFFD availability only once
Signed-off-by: 's avatarAdrian Reber <areber@redhat.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
parent 27e60179
...@@ -35,6 +35,10 @@ endif ...@@ -35,6 +35,10 @@ endif
export DEFINES += $(FEATURE_DEFINES) export DEFINES += $(FEATURE_DEFINES)
export CFLAGS += $(FEATURE_DEFINES) export CFLAGS += $(FEATURE_DEFINES)
ifeq ($(call try-cc,$(FEATURE_TEST_UFFD)),y)
export UFFD := 1
endif
FEATURES_LIST := TCP_REPAIR STRLCPY STRLCAT PTRACE_PEEKSIGINFO \ FEATURES_LIST := TCP_REPAIR STRLCPY STRLCAT PTRACE_PEEKSIGINFO \
SETPROCTITLE_INIT MEMFD TCP_REPAIR_WINDOW SETPROCTITLE_INIT MEMFD TCP_REPAIR_WINDOW
...@@ -57,6 +61,10 @@ $(call map,gen-feature-test,$(FEATURES_LIST)) ...@@ -57,6 +61,10 @@ $(call map,gen-feature-test,$(FEATURES_LIST))
ifeq ($$(VDSO),y) ifeq ($$(VDSO),y)
$(Q) @echo '#define CONFIG_VDSO' >> $$@ $(Q) @echo '#define CONFIG_VDSO' >> $$@
$(Q) @echo '' >> $$@ $(Q) @echo '' >> $$@
endif
ifeq ($$(UFFD),1)
$(Q) @echo '#define CONFIG_HAS_UFFD' >> $$@
$(Q) @echo '' >> $$@
endif endif
$(Q) @echo '#endif /* __CR_CONFIG_H__ */' >> $$@ $(Q) @echo '#endif /* __CR_CONFIG_H__ */' >> $$@
endef endef
......
...@@ -92,6 +92,10 @@ obj-$(CONFIG_COMPAT) += vdso-compat.o ...@@ -92,6 +92,10 @@ obj-$(CONFIG_COMPAT) += vdso-compat.o
CFLAGS_REMOVE_vdso-compat.o += $(CFLAGS-ASAN) $(CFLAGS-GCOV) CFLAGS_REMOVE_vdso-compat.o += $(CFLAGS-ASAN) $(CFLAGS-GCOV)
endif endif
ifeq ($(UFFD),1)
obj-y += uffd.o
endif
PROTOBUF_GEN := scripts/protobuf-gen.sh PROTOBUF_GEN := scripts/protobuf-gen.sh
$(obj)/protobuf-desc.d: $(obj)/protobuf-desc-gen.h $(obj)/protobuf-desc.d: $(obj)/protobuf-desc-gen.h
......
...@@ -733,6 +733,9 @@ int main(int argc, char *argv[], char *envp[]) ...@@ -733,6 +733,9 @@ int main(int argc, char *argv[], char *envp[])
return -1; return -1;
} }
if (!strcmp(argv[optind], "lazy-pages"))
return uffd_listen() != 0;
if (!strcmp(argv[optind], "check")) if (!strcmp(argv[optind], "check"))
return cr_check() != 0; return cr_check() != 0;
...@@ -766,6 +769,9 @@ usage: ...@@ -766,6 +769,9 @@ usage:
" criu page-server\n" " criu page-server\n"
" criu service [<options>]\n" " criu service [<options>]\n"
" criu dedup\n" " criu dedup\n"
#ifdef CONFIG_HAS_UFFD
" criu lazy-pages -D DIR [<options>]\n"
#endif
"\n" "\n"
"Commands:\n" "Commands:\n"
" dump checkpoint a process/tree identified by pid\n" " dump checkpoint a process/tree identified by pid\n"
......
...@@ -25,6 +25,11 @@ extern int cr_restore_tasks(void); ...@@ -25,6 +25,11 @@ extern int cr_restore_tasks(void);
extern int convert_to_elf(char *elf_path, int fd_core); extern int convert_to_elf(char *elf_path, int fd_core);
extern int cr_check(void); extern int cr_check(void);
extern int cr_dedup(void); 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 check_add_feature(char *arg); extern int check_add_feature(char *arg);
extern void pr_check_features(const char *offset, const char *sep, int width); extern void pr_check_features(const char *offset, const char *sep, int width);
......
#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_ */
This diff is collapsed.
...@@ -123,3 +123,18 @@ ENTRY(main) ...@@ -123,3 +123,18 @@ ENTRY(main)
nop nop
END(main) END(main)
endef 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment