[CRIU] [PATCH v2 2/5] uffd.c: move code into subfunctions

Adrian Reber adrian at lisas.de
Thu Mar 24 08:52:51 PDT 2016


From: Adrian Reber <areber at redhat.com>

uffd_listen() is a rather large function and this starts to move code
into subfunctions.

Signed-off-by: Adrian Reber <areber at redhat.com>
---
 criu/uffd.c | 67 ++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 33 insertions(+), 34 deletions(-)

diff --git a/criu/uffd.c b/criu/uffd.c
index cdda985..88748bb 100644
--- a/criu/uffd.c
+++ b/criu/uffd.c
@@ -418,15 +418,16 @@ static int handle_vdso_pages(int uffd, struct list_head *uffd_list, void *dest)
 /*
  *  Setting up criu infrastructure and scan for VMAs.
  */
-static int find_vmas()
+static int find_vmas(struct list_head *uffd_list)
 {
 	struct cr_img *img;
 	int ret;
 	struct vm_area_list vmas;
 	int vn = 0;
 	struct rst_info *ri;
+	struct page_read pr;
+	struct uffd_pages_struct *uffd_pages;
 
-	LIST_HEAD(uffd_list);
 
 	if (check_img_inventory() == -1)
 		return -1;
@@ -461,7 +462,7 @@ static int find_vmas()
 		ret = -1;
 		vma = alloc_vma_area();
 		if (!vma)
-			break;
+			goto out;
 
 		ret = 0;
 		ri->vmas.nr++;
@@ -478,6 +479,34 @@ static int find_vmas()
 		pr_info("vma 0x%"PRIx64" 0x%"PRIx64"\n", vma->e->start, vma->e->end);
 	}
 
+	ret = open_page_read(pid, &pr, PR_TASK);
+	if (ret <= 0) {
+		ret = -1;
+		goto out;
+	}
+	/*
+	 * 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 {
+		ret = collect_uffd_pages(&pr, uffd_list);
+		if (ret == -1) {
+			goto out;
+		}
+	} while (ret);
+
+	if (pr.close)
+		pr.close(&pr);
+
+	/* Count detected pages */
+	list_for_each_entry(uffd_pages, uffd_list, list)
+	    ret++;
+
+	pr_debug("Found %d pages to be handled by UFFD\n", ret);
+
+out:
 	return ret;
 }
 
@@ -487,7 +516,6 @@ int uffd_listen()
 	void *dest;
 	__u64 flags;
 	struct uffd_msg msg;
-	struct page_read pr;
 	unsigned long ps;
 	int rc;
 	fd_set set;
@@ -521,7 +549,7 @@ int uffd_listen()
 	 * Find the memory pages belonging to the restored process
 	 * so that it is trackable when all pages have been transferred.
 	 */
-	if (find_vmas() == -1)
+	if ((total_pages = find_vmas(&uffd_list)) == -1)
 		return -1;
 
 	/* Initialize FD sets for read() with timeouts (using select()) */
@@ -532,35 +560,6 @@ int uffd_listen()
 	ps = page_size();
 	dest = malloc(ps);
 
-	rc = open_page_read(pid, &pr, PR_TASK);
-	if (rc <= 0) {
-		rc = 1;
-		goto out;
-	}
-	/*
-	 * 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);
-		if (rc == -1) {
-			rc = 1;
-			goto out;
-		}
-	} while (rc);
-
-	if (pr.close)
-		pr.close(&pr);
-
-
-	/* Count detected pages */
-	list_for_each_entry(uffd_pages, &uffd_list, list)
-	    total_pages++;
-
-	pr_debug("Found %ld pages to be handled by UFFD\n", total_pages);
-
 	while (1) {
 		bool page_sent = false;
 		/*
-- 
1.8.3.1



More information about the CRIU mailing list