[CRIU] Re: [PATCH] protobuf: Convert vma_entry to PB format v2

Cyrill Gorcunov gorcunov at openvz.org
Thu Jul 19 04:23:26 EDT 2012


On Thu, Jul 19, 2012 at 12:18:46PM +0400, Cyrill Gorcunov wrote:
> > > 
> > > Huh? Why not just use tmp below?

Updated

	Cyrill
-------------- next part --------------
>From cad67ee9a47a2745262511d4359c78327888df8b Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Thu, 19 Jul 2012 11:54:22 +0400
Subject: [PATCH] protobuf: Convert vma_entry to PB format v3

v2:
 - Use regular uint types in message proto
 - Use PB engine for "show"
v3:
 - drop usage of temp. variable in prepare_shmem_pid

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 cr-dump.c          |    6 +++---
 cr-restore.c       |   15 ++++++++++-----
 cr-show.c          |   20 ++------------------
 files.c            |    2 +-
 include/crtools.h  |    4 +++-
 include/files.h    |    3 ++-
 include/image.h    |   11 -----------
 include/parasite.h |    4 +++-
 include/restorer.h |    5 +++--
 include/shmem.h    |    8 +++++---
 include/util.h     |    3 +++
 parasite.c         |    2 +-
 protobuf/Makefile  |    1 +
 protobuf/vma.proto |   10 ++++++++++
 restorer.c         |    4 ++--
 shmem.c            |   24 ++++++++++++++----------
 16 files changed, 63 insertions(+), 59 deletions(-)
 create mode 100644 protobuf/vma.proto

diff --git a/cr-dump.c b/cr-dump.c
index 6d641c4..e8e69c5 100644
--- a/cr-dump.c
+++ b/cr-dump.c
@@ -390,7 +390,7 @@ static int dump_task_fs(pid_t pid, struct cr_fdset *fdset)
 	return pb_write(fdset_fd(fdset, CR_FD_FS), &fe, fs_entry);
 }
 
-static int dump_filemap(pid_t pid, struct vma_entry *vma, int file_fd,
+static int dump_filemap(pid_t pid, VmaEntry *vma, int file_fd,
 		const struct cr_fdset *fdset)
 {
 	struct fd_parms p = FD_PARMS_INIT;
@@ -422,7 +422,7 @@ static int dump_task_mappings(pid_t pid, const struct list_head *vma_area_list,
 	fd = fdset_fd(cr_fdset, CR_FD_VMAS);
 
 	list_for_each_entry(vma_area, vma_area_list, list) {
-		struct vma_entry *vma = &vma_area->vma;
+		VmaEntry *vma = &vma_area->vma;
 
 		pr_info_vma(vma_area);
 
@@ -438,7 +438,7 @@ static int dump_task_mappings(pid_t pid, const struct list_head *vma_area_list,
 			ret = 0;
 
 		if (!ret)
-			ret = write_img(fd, vma);
+			ret = pb_write(fd, vma, vma_entry);
 		if (ret)
 			goto err;
 	}
diff --git a/cr-restore.c b/cr-restore.c
index a0eea4b..9a034e8 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -52,6 +52,7 @@
 #include "protobuf.h"
 #include "protobuf/sa.pb-c.h"
 #include "protobuf/itimer.pb-c.h"
+#include "protobuf/vma.pb-c.h"
 
 static struct pstree_item *me;
 
@@ -141,6 +142,7 @@ static int read_and_open_vmas(int pid, struct list_head *vmas, int *nr_vmas)
 	*nr_vmas = 0;
 	while (1) {
 		struct vma_area *vma;
+		VmaEntry *e;
 
 		ret = -1;
 		vma = alloc_vma_area();
@@ -149,10 +151,13 @@ static int read_and_open_vmas(int pid, struct list_head *vmas, int *nr_vmas)
 
 		(*nr_vmas)++;
 		list_add_tail(&vma->list, vmas);
-		ret = read_img_eof(fd, &vma->vma);
+		ret = pb_read_eof(fd, &e, vma_entry);
 		if (ret <= 0)
 			break;
 
+		vma->vma = *e;
+		vma_entry__free_unpacked(e, NULL);
+
 		if (!(vma_entry_is(&vma->vma, VMA_AREA_REGULAR)))
 			continue;
 
@@ -1032,9 +1037,9 @@ static int prepare_creds(int pid, struct task_restore_core_args *args)
 	return ret > 0 ? 0 : -1;
 }
 
-static struct vma_entry *vma_list_remap(void *addr, unsigned long len, struct list_head *vmas)
+static VmaEntry *vma_list_remap(void *addr, unsigned long len, struct list_head *vmas)
 {
-	struct vma_entry *vma, *ret;
+	VmaEntry *vma, *ret;
 	struct vma_area *vma_area;
 
 	ret = vma = mmap(addr, len, PROT_READ | PROT_WRITE,
@@ -1123,8 +1128,8 @@ static int sigreturn_restore(pid_t pid, struct list_head *tgt_vmas, int nr_vmas)
 	if (ret < 0)
 		goto err;
 
-	self_vmas_len = round_up((ret + 1) * sizeof(struct vma_entry), PAGE_SIZE);
-	vmas_len = round_up((nr_vmas + 1) * sizeof(struct vma_entry), PAGE_SIZE);
+	self_vmas_len = round_up((ret + 1) * sizeof(VmaEntry), PAGE_SIZE);
+	vmas_len = round_up((nr_vmas + 1) * sizeof(VmaEntry), PAGE_SIZE);
 
 	/* pr_info_vma_list(&self_vma_list); */
 
diff --git a/cr-show.c b/cr-show.c
index b7d1fd4..3a3a60f 100644
--- a/cr-show.c
+++ b/cr-show.c
@@ -38,6 +38,7 @@
 #include "protobuf/sa.pb-c.h"
 #include "protobuf/itimer.pb-c.h"
 #include "protobuf/mm.pb-c.h"
+#include "protobuf/vma.pb-c.h"
 
 #define DEF_PAGES_PER_LINE	6
 
@@ -165,24 +166,7 @@ void show_fs(int fd_fs, struct cr_options *o)
 
 void show_vmas(int fd_vma, struct cr_options *o)
 {
-	struct vma_area vma_area = {};
-	struct vma_entry ve;
-
-	pr_img_head(CR_FD_VMAS);
-
-	while (1) {
-		int ret;
-
-		ret = read_img_eof(fd_vma, &ve);
-		if (ret <= 0)
-			break;
-
-		/* Simply in a sake of fancy printing */
-		vma_area.vma = ve;
-		pr_msg_vma(&vma_area);
-	}
-
-	pr_img_tail(CR_FD_VMAS);
+	pb_show_plain(fd_vma, vma_entry);
 }
 
 static int nice_width_for(unsigned long addr)
diff --git a/files.c b/files.c
index 2270127..f5b6d6b 100644
--- a/files.c
+++ b/files.c
@@ -531,7 +531,7 @@ err:
 	return ret;
 }
 
-int get_filemap_fd(int pid, struct vma_entry *vma_entry)
+int get_filemap_fd(int pid, VmaEntry *vma_entry)
 {
 	return open_reg_by_id(vma_entry->shmid);
 }
diff --git a/include/crtools.h b/include/crtools.h
index ed79df6..c7fe75b 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -9,6 +9,8 @@
 #include "util.h"
 #include "image.h"
 
+#include "../protobuf/vma.pb-c.h"
+
 #define CR_FD_PERM		(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
 #define CR_FD_PERM_DUMP		(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
 
@@ -168,7 +170,7 @@ void free_mappings(struct list_head *vma_area_list);
 
 struct vma_area {
 	struct list_head	list;
-	struct vma_entry	vma;
+	VmaEntry		vma;
 	int			vm_file_fd;
 };
 
diff --git a/include/files.h b/include/files.h
index 3598a0b..5178da3 100644
--- a/include/files.h
+++ b/include/files.h
@@ -9,6 +9,7 @@
 
 #include "../protobuf/fdinfo.pb-c.h"
 #include "../protobuf/fown.pb-c.h"
+#include "../protobuf/vma.pb-c.h"
 
 struct pstree_item;
 struct file_desc;
@@ -87,7 +88,7 @@ extern void show_saved_files(void);
 extern int prepare_fds(struct pstree_item *me);
 extern int prepare_fd_pid(int pid, struct rst_info *rst_info);
 extern int prepare_shared_fdinfo(void);
-extern int get_filemap_fd(int pid, struct vma_entry *vma_entry);
+extern int get_filemap_fd(int pid, VmaEntry *vma_entry);
 extern int prepare_fs(int pid);
 extern int set_fd_flags(int fd, int flags);
 
diff --git a/include/image.h b/include/image.h
index 0dc23ad..7ee2dde 100644
--- a/include/image.h
+++ b/include/image.h
@@ -55,17 +55,6 @@
 
 #define USK_EXTERN	(1 << 0)
 
-struct vma_entry {
-	u64	start;
-	u64	end;
-	u64	pgoff;
-	u64	shmid;
-	u32	prot;
-	u32	flags;
-	u32	status;
-	s64	fd;
-} __packed;
-
 #define VMA_AREA_NONE		(0 <<  0)
 #define VMA_AREA_REGULAR	(1 <<  0)	/* Dumpable area */
 #define VMA_AREA_STACK		(1 <<  1)
diff --git a/include/parasite.h b/include/parasite.h
index 0e0c87c..35d3406 100644
--- a/include/parasite.h
+++ b/include/parasite.h
@@ -15,6 +15,8 @@
 
 #include "util-net.h"
 
+#include "../protobuf/vma.pb-c.h"
+
 #define __head __used __section(.head.text)
 
 enum {
@@ -42,7 +44,7 @@ struct parasite_init_args {
 };
 
 struct parasite_dump_pages_args {
-	struct vma_entry	vma_entry;
+	VmaEntry		vma_entry;
 	unsigned long		nrpages_dumped;	/* how many pages are dumped */
 	unsigned long		nrpages_skipped;
 	unsigned long		nrpages_total;
diff --git a/include/restorer.h b/include/restorer.h
index 39367b2..eb45f66 100644
--- a/include/restorer.h
+++ b/include/restorer.h
@@ -12,6 +12,7 @@
 #include "crtools.h"
 
 #include "../protobuf/mm.pb-c.h"
+#include "../protobuf/vma.pb-c.h"
 
 #ifndef CONFIG_X86_64
 # error Only x86-64 is supported
@@ -79,8 +80,8 @@ struct task_restore_core_args {
 	struct thread_restore_args	*thread_args;		/* array of thread arguments */
 	struct shmems			*shmems;
 	struct task_entries		*task_entries;
-	struct vma_entry		*self_vmas;
-	struct vma_entry		*tgt_vmas;
+	VmaEntry			*self_vmas;
+	VmaEntry			*tgt_vmas;
 	rt_sigaction_t			sigchld_act;
 
 	struct itimerval		itimers[3];
diff --git a/include/shmem.h b/include/shmem.h
index f8907c8..f109d46 100644
--- a/include/shmem.h
+++ b/include/shmem.h
@@ -1,16 +1,18 @@
 #ifndef __CR_SHMEM_H__
 #define __CR_SHMEM_H__
+
+#include "../protobuf/vma.pb-c.h"
+
 int prepare_shmem_pid(int pid);
 int prepare_shmem_restore(void);
 void show_saved_shmems(void);
-struct vma_entry;
-int get_shmem_fd(int pid, struct vma_entry *vi);
+int get_shmem_fd(int pid, VmaEntry *vi);
 
 struct shmems;
 extern struct shmems *rst_shmems;
 
 int cr_dump_shmem(void);
-int add_shmem_area(pid_t pid, struct vma_entry *vma);
+int add_shmem_area(pid_t pid, VmaEntry *vma);
 int init_shmem_dump(void);
 void fini_shmem_dump(void);
 #endif
diff --git a/include/util.h b/include/util.h
index a8b9cc9..18c821f 100644
--- a/include/util.h
+++ b/include/util.h
@@ -16,6 +16,8 @@
 #include "types.h"
 #include "log.h"
 
+#include "../protobuf/vma.pb-c.h"
+
 #define PREF_SHIFT_OP(pref, op, size)	((size) op (pref ##BYTES_SHIFT))
 #define KBYTES_SHIFT	10
 #define MBYTES_SHIFT	20
@@ -155,6 +157,7 @@ extern void pr_vma(unsigned int loglevel, const struct vma_area *vma_area);
 	({							\
 		struct vma_area *p__ = xzalloc(sizeof(*p__));	\
 		if (p__) {					\
+			vma_entry__init(&p__->vma);		\
 			p__->vm_file_fd = -1;			\
 			p__->vma.fd	= -1;			\
 		}						\
diff --git a/parasite.c b/parasite.c
index b70110a..29a3e85 100644
--- a/parasite.c
+++ b/parasite.c
@@ -102,7 +102,7 @@ static void sys_write_msg(const char *msg)
 #define PME_SWAP	(1ULL << 62)
 #define PME_FILE	(1ULL << 61)
 
-static inline int should_dump_page(struct vma_entry *vmae, u64 pme)
+static inline int should_dump_page(VmaEntry *vmae, u64 pme)
 {
 	return (pme & (PME_PRESENT | PME_SWAP)) &&
 		/*
diff --git a/protobuf/Makefile b/protobuf/Makefile
index 8e4b87f..008778b 100644
--- a/protobuf/Makefile
+++ b/protobuf/Makefile
@@ -47,6 +47,7 @@ PROTO_FILES	+= ipc-desc.proto
 PROTO_FILES	+= ipc-shm.proto
 PROTO_FILES	+= ipc-msg.proto
 PROTO_FILES	+= ipc-sem.proto
+PROTO_FILES	+= vma.proto
 
 HDRS	:= $(patsubst %.proto,%.pb-c.h,$(PROTO_FILES))
 SRCS	:= $(patsubst %.proto,%.pb-c.c,$(PROTO_FILES))
diff --git a/protobuf/vma.proto b/protobuf/vma.proto
new file mode 100644
index 0000000..5535c36
--- /dev/null
+++ b/protobuf/vma.proto
@@ -0,0 +1,10 @@
+message vma_entry {
+	required uint64		start	= 1;
+	required uint64		end	= 2;
+	required uint64		pgoff	= 3;
+	required uint64		shmid	= 4;
+	required uint32		prot	= 5;
+	required uint32		flags	= 6;
+	required uint32		status	= 7;
+	required sint64		fd	= 8;
+}
diff --git a/restorer.c b/restorer.c
index eb41e97..3b527eb 100644
--- a/restorer.c
+++ b/restorer.c
@@ -242,7 +242,7 @@ static long restore_self_exe_late(struct task_restore_core_args *args)
 	return 0;
 }
 
-static u64 restore_mapping(const struct vma_entry *vma_entry)
+static u64 restore_mapping(const VmaEntry *vma_entry)
 {
 	int prot	= vma_entry->prot;
 	int flags	= vma_entry->flags | MAP_FIXED;
@@ -293,7 +293,7 @@ long __export_restore_task(struct task_restore_core_args *args)
 {
 	long ret = -1;
 	struct core_entry *core_entry;
-	struct vma_entry *vma_entry;
+	VmaEntry *vma_entry;
 	u64 va;
 
 	struct rt_sigframe *rt_sigframe;
diff --git a/shmem.c b/shmem.c
index 38f04b5..cc1dcdb 100644
--- a/shmem.c
+++ b/shmem.c
@@ -7,6 +7,8 @@
 #include "crtools.h"
 #include "restorer.h"
 
+#include "protobuf.h"
+
 struct shmems *rst_shmems;
 
 void show_saved_shmems(void)
@@ -22,7 +24,7 @@ void show_saved_shmems(void)
 			rst_shmems->entries[i].pid);
 }
 
-static int collect_shmem(int pid, struct vma_entry *vi)
+static int collect_shmem(int pid, VmaEntry *vi)
 {
 	int nr_shmems = rst_shmems->nr_shmems;
 	unsigned long size = vi->pgoff + vi->end - vi->start;
@@ -77,7 +79,7 @@ static int collect_shmem(int pid, struct vma_entry *vi)
 int prepare_shmem_pid(int pid)
 {
 	int fd, ret = -1;
-	struct vma_entry vi;
+	VmaEntry *vi;
 
 	fd = open_image_ro(CR_FD_VMAS, pid);
 	if (fd < 0) {
@@ -88,19 +90,21 @@ int prepare_shmem_pid(int pid)
 	}
 
 	while (1) {
-		ret = read_img_eof(fd, &vi);
+		ret = pb_read_eof(fd, &vi, vma_entry);
 		if (ret <= 0)
 			break;
 
-		pr_info("vma 0x%lx 0x%lx\n", vi.start, vi.end);
+		pr_info("vma 0x%lx 0x%lx\n", vi->start, vi->end);
 
-		if (!vma_entry_is(&vi, VMA_ANON_SHARED))
+		if (!vma_entry_is(vi, VMA_ANON_SHARED) ||
+		    vma_entry_is(vi, VMA_AREA_SYSVIPC)) {
+			vma_entry__free_unpacked(vi, NULL);
 			continue;
+		}
 
-		if (vma_entry_is(&vi, VMA_AREA_SYSVIPC))
-			continue;
+		ret = collect_shmem(pid, vi);
+		vma_entry__free_unpacked(vi, NULL);
 
-		ret = collect_shmem(pid, &vi);
 		if (ret)
 			break;
 	}
@@ -156,7 +160,7 @@ static int restore_shmem_content(void *addr, struct shmem_info *si)
 	return ret;
 }
 
-int get_shmem_fd(int pid, struct vma_entry *vi)
+int get_shmem_fd(int pid, VmaEntry *vi)
 {
 	struct shmem_info *si;
 	void *addr;
@@ -241,7 +245,7 @@ static struct shmem_info_dump* shmem_find(unsigned long shmid)
 	return NULL;
 }
 
-int add_shmem_area(pid_t pid, struct vma_entry *vma)
+int add_shmem_area(pid_t pid, VmaEntry *vma)
 {
 	struct shmem_info_dump *si;
 	unsigned long size = vma->pgoff + (vma->end - vma->start);
-- 
1.7.7.6



More information about the CRIU mailing list