>> + pgarr = cr_pgarr_alloc(ctx);
>> + return pgarr;
>> +}
>> +
>> +/*
>> + * Checkpoint is outside the context of the checkpointee, so one cannot
>> + * simply read pages from user-space. Instead, we scan the address space
>> + * of the target to cherry-pick pages of interest. Selected pages are
>> + * enlisted in a page-array chain (attached to the checkpoint context).
>> + * To save their contents, each page is mapped to kernel memory and then
>> + * dumped to the file descriptor.
>> + */
>> +
>> +/**
>> + * cr_vma_fill_pgarr - fill a page-array with addr/page tuples for a vma
>> + * @ctx - checkpoint context
>> + * @pgarr - page-array to fill
>> + * @vma - vma to scan
>> + * @start - start address (updated)
>> + */
>> +static int cr_vma_fill_pgarr(struct cr_ctx *ctx, struct cr_pgarr *pgarr,
>> + struct vm_area_struct *vma, unsigned long *start)
>> +{
>> + unsigned long end = vma->vm_end;
>> + unsigned long addr = *start;
>> + struct page **pagep;
>> + unsigned long *addrp;
>> + int cow, nr, ret = 0;
>> +
>> + nr = pgarr->nr_free;
>> + pagep = &pgarr->pages[pgarr->nr_used];
>> + addrp = &pgarr->vaddrs[pgarr->nr_used];
>> + cow = !!vma->vm_file;
>> +
>> + while (addr < end) {
>> + struct page *page;
>> +
>> + /*
>> + * simplified version of get_user_pages(): already have vma,
>> + * only need FOLL_TOUCH, and (for now) ignore fault stats.
>> + *
>> + * FIXME: consolidate with get_user_pages()
>> + */
>> +
>> + cond_resched();
>> + while (!(page = follow_page(vma, addr, FOLL_TOUCH))) {
>> + ret = handle_mm_fault(vma->vm_mm, vma, addr, 0);
>> + if (ret & VM_FAULT_ERROR) {
>> + if (ret & VM_FAULT_OOM)
>> + ret = -ENOMEM;
>> + else if (ret & VM_FAULT_SIGBUS)
>> + ret = -EFAULT;
>> + else
>> + BUG();
>> + break;
>> + }
>> + cond_resched();
>> + ret = 0;
>> + }
>> +
>> + if (IS_ERR(page))
>> + ret = PTR_ERR(page);
>> +
>> + if (ret < 0)
>> + break;
>> +
>> + if (page == ZERO_PAGE(0)) {
>> + page = NULL; /* zero page: ignore */
>> + } else if (cow && page_mapping(page) != NULL) {
>> + page = NULL; /* clean cow: ignore */
>> + } else {
>> + get_page(page);
>> + *(addrp++) = addr;
>> + *(pagep++) = page;
>> + if (--nr == 0) {
>> + addr += PAGE_SIZE;
>> + break;
>> + }
>> + }
>> +
>> + addr += PAGE_SIZE;
>> + }
>> +
>> + if (unlikely(ret < 0)) {
>> + nr = pgarr->nr_free - nr;
>> + while (nr--)
>> + page_cache_release(*(--pagep));
>> + return ret;
>> + }
>> +
>> + *start = addr;
>> + return pgarr->nr_free - nr;
>> +}
>> +
>> +/**
>> + * cr_vma_scan_pages - scan vma for pages that will need to be dumped
>> + * @ctx - checkpoint context
>> + * @vma - vma to scan
>> + *
>> + * lists of page pointes and corresponding virtual addresses are tracked
>> + * inside ctx->pgarr page-array chain
>> + */
>> +static int cr_vma_scan_pages(struct cr_ctx *ctx, struct vm_area_struct *vma)
>> +{
>> + unsigned long addr = vma->vm_start;
>> + unsigned long end = vma->vm_end;
>> + struct cr_pgarr *pgarr;
>> + int nr, total = 0;
>> +
>> + while (addr < end) {
>> + pgarr = cr_pgarr_prep(ctx);
>> + if (!pgarr)
>> + return -ENOMEM;
>> + nr = cr_vma_fill_pgarr(ctx, pgarr, vma, &addr);
>> + if (nr < 0)
>> + return nr;
>> + pgarr->nr_free -= nr;
>> + pgarr->nr_used += nr;
>> + total += nr;
>> + }
>> +
>> + cr_debug("total %d\n", total);
>> + return total;
>> +}
>> +
>> +static int cr_page_write(struct cr_ctx *ctx, struct page *page, char *buf)
>> +{
>> + void *ptr;
>> +
>> + ptr = kmap_atomic(page, KM_USER1);
>> + memcpy(buf, ptr, PAGE_SIZE);
>> + kunmap_atomic(page, KM_USER1);
>> +
>> + return cr_kwrite(ctx, buf, PAGE_SIZE);
>> +}
>> +
>> +/**
>> + * cr_vma_dump_pages - dump pages listed in the ctx page-array chain
>> + * @ctx - checkpoint context
>> + * @total - total number of pages
>> + *
>> + * First dump all virtual addresses, followed by the contents of all pages
>> + */
>> +static int cr_vma_dump_pages(struct cr_ctx *ctx, int total)
>> +{
>> + struct cr_pgarr *pgarr;
>> + char *buf;
>> + int i, ret = 0;
>> +
>> + if (!total)
>> + return 0;
>> +
>> + list_for_each_entry(pgarr, &ctx->pgarr, list) {
>> + ret = cr_kwrite(ctx, pgarr->vaddrs,
>> + pgarr->nr_used * sizeof(*pgarr->vaddrs));
>> + if (ret < 0)
>> + return ret;
>> + }
>> +
>> + buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
>> + if (!buf)
>> + return -ENOMEM;
>> +
>> + list_for_each_entry(pgarr, &ctx->pgarr, list) {
>> + for (i = 0; i < pgarr->nr_used; i++) {
>> + ret = cr_page_write(ctx, pgarr->pages[i], buf);
>> + if (ret < 0)
>> + goto out;
>> + }
>> + }
>> +
>> + out:
>> + kfree(buf);
>> + return ret;
>> +}
>> +
>> +static int cr_write_vma(struct cr_ctx *ctx, struct vm_area_struct *vma)
>> +{
>> + struct cr_hdr h;
>> + struct cr_hdr_vma *hh = cr_hbuf_get(ctx, sizeof(*hh));
>> + int vma_type, nr, ret;
>> +
>> + h.type = CR_HDR_VMA;
>> + h.len = sizeof(*hh);
>> + h.parent = 0;
>> +
>> + hh->vm_start = vma->vm_start;
>> + hh->vm_end = vma->vm_end;
>> + hh->vm_page_prot = vma->vm_page_prot.pgprot;
>> + hh->vm_flags = vma->vm_flags;
>> + hh->vm_pgoff = vma->vm_pgoff;
>> +
>> + if (vma->vm_flags & (VM_SHARED | VM_IO | VM_HUGETLB | VM_NONLINEAR)) {
>> + pr_warning("CR: unsupported VMA %#lx\n", vma->vm_flags);
>> + return -ETXTBSY;
>> + }
>> +
>> + /* by default assume anon memory */
>> + vma_type = CR_VMA_ANON;
>> +
>> + /* if there is a backing file, assume private-mapped */
>> + /* (FIX: check if the file is unlinked) */
>> + if (vma->vm_file)
>> + vma_type = CR_VMA_FILE;
>> +
>> + hh->vma_type = vma_type;
>> +
>> + /*
>> + * it seems redundant now, but we do it in 3 steps for because:
>> + * first, the logic is simpler when we how many pages before
>> + * dumping them; second, a future optimization will defer the
>> + * writeout (dump, and free) to a later step; in which case all
>> + * the pages to be dumped will be aggregated on the checkpoint ctx
>> + */
>> +
>> + /* (1) scan: scan through the PTEs of the vma to count the pages
>> + * to dump (and later make those pages COW), and keep the list of
>> + * pages (and a reference to each page) on the checkpoint ctx */
>> + nr = cr_vma_scan_pages(ctx, vma);
>> + if (nr < 0)
>> + return nr;
>> +
>> + hh->nr_pages = nr;
>> + ret = cr_write_obj(ctx, &h, hh);
>> + cr_hbuf_put(ctx, sizeof(*hh));
>> + if (ret < 0)
>> + return ret;
>> + /* save the file name, if relevant */
>> + if (vma->vm_file)
>> + ret = cr_write_fname(ctx, &vma->vm_file->f_path, ctx->vfsroot);
>> +
>> + if (ret < 0)
>> + return ret;
>> +
>> + /* (2) dump: write out the addresses of all pages in the list (on
>> + * the checkpoint ctx) followed by the contents of all pages */
>> + ret = cr_vma_dump_pages(ctx, nr);
>> +
>> + /* (3) free: release the extra references to the pages in the list */
>> + cr_pgarr_reset(ctx);
>> +
>> + return ret;
>> +}
>> +
>> +int cr_write_mm(struct cr_ctx *ctx, struct task_struct *t)
>> +{
>> + struct cr_hdr h;
>> + struct cr_hdr_mm *hh = cr_hbuf_get(ctx, sizeof(*hh));
>> + struct mm_struct *mm;
>> + struct vm_area_struct *vma;
>> + int objref, ret;
>> +
>> + h.type = CR_HDR_MM;
>> + h.len = sizeof(*hh);
>> + h.parent = task_pid_vnr(t);
>> +
>> + mm = get_task_mm(t);
>> +
>> + objref = 0; /* will be meaningful with multiple processes */
>> + hh->objref = objref;
>> +
>> + down_read(&mm->mmap_sem);
>> +
>> + hh->start_code = mm->start_code;
>> + hh->end_code = mm->end_code;
>> + hh->start_data = mm->start_data;
>> + hh->end_data = mm->end_data;
>> + hh->start_brk = mm->start_brk;
>> + hh->brk = mm->brk;
>> + hh->start_stack = mm->start_stack;
>> + hh->arg_start = mm->arg_start;
>> + hh->arg_end = mm->arg_end;
>> + hh->env_start = mm->env_start;
>> + hh->env_end = mm->env_end;
>> +
>> + hh->map_count = mm->map_count;
>> +
>> + /* FIX: need also mm->flags */
>> +
>> + ret = cr_write_obj(ctx, &h, hh);
>> + cr_hbuf_put(ctx, sizeof(*hh));
>> + if (ret < 0)
>> + goto out;
>> +
>> + /* write the vma's */
>> + for (vma = mm->mmap; vma; vma = vma->vm_next) {
>> + ret = cr_write_vma(ctx, vma);
>> + if (ret < 0)
>> + goto out;
>> + }
>> +
>> + ret = cr_write_mm_context(ctx, mm, objref);
>> +
>> + out:
>> + up_read(&mm->mmap_sem);
>> + mmput(mm);
>> + return ret;
>> +}
>> diff --git a/checkpoint/ckpt_mem.h b/checkpoint/ckpt_mem.h
>> new file mode 100644
>> index 0000000..8ee211d
>> --- /dev/null
>> +++ b/checkpoint/ckpt_mem.h
>> @@ -0,0 +1,35 @@
>> +#ifndef _CHECKPOINT_CKPT_MEM_H_
>> +#define _CHECKPOINT_CKPT_MEM_H_
>> +/*
>> + * Generic container checkpoint-restart
>> + *
>> + * Copyright (C) 2008 Oren Laadan
>> + *
>> + * This file is subject to the terms and conditions of the GNU General Public
>> + * License. See the file COPYING in the main directory of the Linux
>> + * distribution for more details.
>> + */
>> +
>> +#include <linux/mm_types.h>
>> +
>> +/*
>> + * page-array chains: each cr_pgarr describes a set of <strcut page *,vaddr>
>> + * tuples (where vaddr is the virtual address of a page in a particular mm).
>> + * Specifically, we use separate arrays so that all vaddrs can be written
>> + * and read at once.
>> + */
>> +
>> +struct cr_pgarr {
>> + unsigned long *vaddrs;
>> + struct page **pages;
>> + unsigned int nr_used; /* how many entries already used */
>> + unsigned int nr_free; /* how many entries still free */
>> + struct list_head list;
>> +};
>> +
>> +void cr_pgarr_reset(struct cr_ctx *ctx);
>> +void cr_pgarr_free(struct cr_ctx *ctx);
>> +struct cr_pgarr *cr_pgarr_alloc(struct cr_ctx *ctx);
>> +struct cr_pgarr *cr_pgarr_prep(struct cr_ctx *ctx);
>> +
>> +#endif /* _CHECKPOINT_CKPT_MEM_H_ */
>> diff --git a/checkpoint/sys.c b/checkpoint/sys.c
>> index 113e0df..8141161 100644
>> --- a/checkpoint/sys.c
>> +++ b/checkpoint/sys.c
>> @@ -16,6 +16,8 @@
>> #include <linux/capability.h>
>> #include <linux/ckpt.h>
>>
>> +#include "ckpt_mem.h"
>> +
>> /*
>> * helpers to write/read to/from the image file descriptor
>> *
>> @@ -110,7 +112,6 @@ int cr_kread(struct cr_ctx *ctx, void *buf, int count)
>> return ret;
>> }
>>
>> -
>> /*
>> * helpers to manage CR contexts: allocated for each checkpoint and/or
>> * restart operation, and persists until the operation is completed.
>> @@ -126,6 +127,11 @@ void cr_ctx_free(struct cr_ctx *ctx)
>>
>> free_pages((unsigned long) ctx->hbuf, CR_HBUF_ORDER);
>>
>> + if (ctx->vfsroot)
>> + path_put(ctx->vfsroot);
>> +
>> + cr_pgarr_free(ctx);
>> +
>> kfree(ctx);
>> }
>>
>> @@ -145,10 +151,13 @@ struct cr_ctx *cr_ctx_alloc(pid_t pid, int fd, unsigned long flags)
>> get_file(ctx->file);
>>
>> ctx->hbuf = (void *) __get_free_pages(GFP_KERNEL, CR_HBUF_ORDER);
>> - if (!ctx->hbuf) {
>> - cr_ctx_free(ctx);
>> - return ERR_PTR(-ENOMEM);
>> - }
>> + if (!ctx->hbuf)
>> + goto nomem;
>> +
>> + /* assume checkpointer is in container's root vfs */
>> + /* FIXME: this works for now, but will change with real containers */
>> + ctx->vfsroot = ¤t->fs->root;
>> + path_get(ctx->vfsroot);
>>
>> ctx->pid = pid;
>> ctx->flags = flags;
>> @@ -156,6 +165,10 @@ struct cr_ctx *cr_ctx_alloc(pid_t pid, int fd, unsigned long flags)
>> ctx->crid = atomic_inc_return(&cr_ctx_count);
>>
>> return ctx;
>> +
>> + nomem:
>> + cr_ctx_free(ctx);
>> + return ERR_PTR(-ENOMEM);
>> }
>>
>> /*
>> diff --git a/include/asm-x86/ckpt_hdr.h b/include/asm-x86/ckpt_hdr.h
>> index 44a903c..6bc61ac 100644
>> --- a/include/asm-x86/ckpt_hdr.h
>> +++ b/include/asm-x86/ckpt_hdr.h
>> @@ -69,4 +69,9 @@ struct cr_hdr_cpu {
>>
>> } __attribute__((aligned(8)));
>>
>> +struct cr_hdr_mm_context {
>> + __s16 ldt_entry_size;
>> + __s16 nldt;
>> +} __attribute__((aligned(8)));
>> +
>> #endif /* __ASM_X86_CKPT_HDR__H */
>> diff --git a/include/linux/ckpt.h b/include/linux/ckpt.h
>> index 91f4998..5c62a90 100644
>> --- a/include/linux/ckpt.h
>> +++ b/include/linux/ckpt.h
>> @@ -10,6 +10,9 @@
>> * distribution for more details.
>> */
>>
>> +#include <linux/path.h>
>> +#include <linux/fs.h>
>> +
>> #define CR_VERSION 1
>>
>> struct cr_ctx {
>> @@ -24,6 +27,11 @@ struct cr_ctx {
>>
>> void *hbuf; /* temporary buffer for headers */
>> int hpos; /* position in headers buffer */
>> +
>> + struct list_head pgarr; /* page array for dumping VMA contents */
>> + struct cr_pgarr *pgcur; /* current position in page array */
>> +
>> + struct path *vfsroot; /* container root (FIXME) */
>> };
>>
>> /* cr_ctx: flags */
>> @@ -46,11 +54,15 @@ struct cr_hdr;
>>
>> int cr_write_obj(struct cr_ctx *ctx, struct cr_hdr *h, void *buf);
>> int cr_write_string(struct cr_ctx *ctx, char *str, int len);
>> +int cr_write_fname(struct cr_ctx *ctx, struct path *path, struct path *root);
>>
>> int cr_read_obj(struct cr_ctx *ctx, struct cr_hdr *h, void *buf, int n);
>> int cr_read_obj_type(struct cr_ctx *ctx, void *buf, int n, int type);
>> int cr_read_string(struct cr_ctx *ctx, void *str, int len);
>>
>> +int cr_write_mm(struct cr_ctx *ctx, struct task_struct *t);
>> +int cr_read_mm(struct cr_ctx *ctx);
>> +
>> int do_checkpoint(struct cr_ctx *ctx);
>> int do_restart(struct cr_ctx *ctx);
>>
>> diff --git a/include/linux/ckpt_hdr.h b/include/linux/ckpt_hdr.h
>> index e66f322..ac77d7d 100644
>> --- a/include/linux/ckpt_hdr.h
>> +++ b/include/linux/ckpt_hdr.h
>> @@ -32,6 +32,7 @@ struct cr_hdr {
>> enum {
>> CR_HDR_HEAD = 1,
>> CR_HDR_STRING,
>> + CR_HDR_FNAME,
>>
>> CR_HDR_TASK = 101,
>> CR_HDR_THREAD,
>> @@ -82,4 +83,33 @@ struct cr_hdr_task {
>> __s32 task_comm_len;
>> } __attribute__((aligned(8)));
>>
>> +struct cr_hdr_mm {
>> + __u32 objref; /* identifier for shared objects */
>> + __u32 map_count;
>> +
>> + __u64 start_code, end_code, start_data, end_data;
>> + __u64 start_brk, brk, start_stack;
>> + __u64 arg_start, arg_end, env_start, env_end;
>> +
>> +} __attribute__((aligned(8)));
>> +
>> +/* vma subtypes */
>> +enum vm_type {
>> + CR_VMA_ANON = 1,
>> + CR_VMA_FILE
>> +};
>> +
>> +struct cr_hdr_vma {
>> + __u32 vma_type;
>> + __u32 _padding;
>> + __s64 nr_pages;
>> +
>> + __u64 vm_start;
>> + __u64 vm_end;
>> + __u64 vm_page_prot;
>> + __u64 vm_flags;
>> + __u64 vm_pgoff;
>> +
>> +} __attribute__((aligned(8)));
>> +
>> #endif /* _CHECKPOINT_CKPT_HDR_H_ */
>> --
>> 1.5.4.3
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
>> the body of a message to
majordomo@vger.kernel.org
>> More majordomo info at
http://vger.kernel.org/majordomo-info.html
>> Please read the FAQ at
http://www.tux.org/lkml/
>>
>
>
>
> --
> Kinds regards,
> MinChan Kim
>