Commit c9aaf9f3 authored by Pavel Emelyanov's avatar Pavel Emelyanov

rst: Introduce an engine to allocate memory on restore

On restore we need differetn types of memory allocation.
Here's an engine that tries to generalize them all. The
main difference is in how the buffer with objects is being
grown up.

There are 3 types of memory allocations:

1. shared memory -- objects, that will be used by all criu
   children, but will not reach the restorer
2. shared remapable -- the same, but restorer would need
   access to them, i.e. -- buffer with objects will get
   remapped into restorer
3. private -- the same, but allocatedby each task for itself
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 64720771
obj-y += parasite-syscall.o obj-y += parasite-syscall.o
obj-y += mem.o obj-y += mem.o
obj-y += rst-malloc.o
obj-y += cr-restore.o obj-y += cr-restore.o
obj-y += crtools.o obj-y += crtools.o
obj-y += security.o obj-y += security.o
......
#ifndef __CR_RST_MALLOC__H__
#define __CR_RST_MALLOC__H__
/*
* On restore we need differetn types of memory allocation.
* Here's an engine that tries to generalize them all. The
* main difference is in how the buffer with objects is being
* grown up.
*
* Buffers, that are to be used by restorer will be remapped
* into restorer address space with rst_mem_remap() call. Thus
* we have to either keep track of all the buffers and objects,
* or keep objects one-by-one in a plain linear buffer. The
* engine uses the 2nd approach.
*/
enum {
/*
* Shared non-remapable allocations. These can happen only
* in "global" context, i.e. when objects are allocated to
* be used by any process to be restored. The objects are
* not going to be used in restorer blob, thus allocation
* engine grows buffers in a simple manner.
*/
RM_SHARED,
/*
* Shared objects, that are about to be used in restorer
* blob. For these the *_remap_* stuff below is used to get
* the actual pointer on any object. Growing a buffer is
* done with mremap, so that we don't have to keep track
* of all the buffer chunks and can remap them in restorer
* in one call.
*/
RM_SHREMAP,
/*
* Privately used objects. Buffer grow and remap is the
* same as for SHREMAP, but memory regions are MAP_PRIVATE.
*/
RM_PRIVATE,
RST_MEM_TYPES,
};
/*
* Disables SHARED and SHREMAP allocations, turns on PRIVATE
*/
void rst_mem_switch_to_private(void);
/*
* Reports a cookie of a current shared buffer position, that
* can later be used in rst_mem_cpos() to find out the object
* pointer.
*/
unsigned long rst_mem_cpos(int type);
void *rst_mem_remap_ptr(unsigned long pos, int type);
/*
* Allocate and free objects. We don't need to free arbitrary
* object, thus allocation is simple (linear) and only the
* last object can be freed (pop-ed from buffer).
*/
void *rst_mem_alloc(unsigned long size, int type);
void rst_mem_free_last(int type);
/*
* Routines to remap SHREMAP and PRIVATE into restorer address space
*/
unsigned long rst_mem_remap_size(void);
int rst_mem_remap(void *to);
#endif
#include <stdio.h>
#include <stdbool.h>
#include <sys/mman.h>
#include "rst-malloc.h"
#include "bug.h"
#include "asm/types.h"
struct rst_mem_type_s {
bool remapable;
bool enabled;
unsigned long free_bytes;
void *free_mem;
int (*grow)(struct rst_mem_type_s *);
unsigned long last;
void *buf;
unsigned long size;
};
#define RST_MEM_BATCH (2 * PAGE_SIZE)
static int grow_shared(struct rst_mem_type_s *t)
{
void *aux;
/*
* This buffer will not get remapped into
* restorer, thus we can just forget the
* previous chunk location and allocate a
* new one
*/
aux = mmap(NULL, RST_MEM_BATCH, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, 0, 0);
if (aux == MAP_FAILED)
return -1;
t->free_mem = aux;
t->free_bytes = RST_MEM_BATCH;
t->last = 0;
return 0;
}
static int grow_remap(struct rst_mem_type_s *t, int flag)
{
void *aux;
if (!t->buf)
/*
* Can't call mremap with NULL address :(
*/
aux = mmap(NULL, RST_MEM_BATCH, PROT_READ | PROT_WRITE,
flag | MAP_ANON, 0, 0);
else
/*
* We'll have to remap all objects into restorer
* address space and get their new addresses. Since
* we allocate many objects as one linear array, it's
* simpler just to grow the buffer and let callers
* find out new array addresses, rather than allocate
* a completely new one and force callers use objects'
* cpos-s.
*/
aux = mremap(t->buf, t->size,
t->size + RST_MEM_BATCH, MREMAP_MAYMOVE);
if (aux == MAP_FAILED)
return -1;
t->free_mem += (aux - t->buf);
t->free_bytes += RST_MEM_BATCH;
t->size += RST_MEM_BATCH;
t->buf = aux;
return 0;
}
static int grow_shremap(struct rst_mem_type_s *t)
{
return grow_remap(t, MAP_SHARED);
}
static int grow_private(struct rst_mem_type_s *t)
{
return grow_remap(t, MAP_PRIVATE);
}
static struct rst_mem_type_s rst_mems[RST_MEM_TYPES] = {
[RM_SHARED] = {
.grow = grow_shared,
.remapable = false,
.enabled = true,
},
[RM_SHREMAP] = {
.grow = grow_shremap,
.remapable = true,
.enabled = true,
},
[RM_PRIVATE] = {
.grow = grow_private,
.remapable = true,
.enabled = false,
},
};
void rst_mem_switch_to_private(void)
{
rst_mems[RM_SHARED].enabled = false;
rst_mems[RM_SHREMAP].enabled = false;
rst_mems[RM_PRIVATE].enabled = true;
}
unsigned long rst_mem_cpos(int type)
{
struct rst_mem_type_s *t = &rst_mems[type];
BUG_ON(!t->remapable || !t->enabled);
return t->free_mem - t->buf;
}
void *rst_mem_remap_ptr(unsigned long pos, int type)
{
struct rst_mem_type_s *t = &rst_mems[type];
BUG_ON(!t->remapable);
return t->buf + pos;
}
void *rst_mem_alloc(unsigned long size, int type)
{
struct rst_mem_type_s *t = &rst_mems[type];
void *ret;
BUG_ON(!t->enabled);
if ((t->free_bytes < size) && t->grow(t)) {
pr_perror("Can't grow rst mem");
return NULL;
}
ret = t->free_mem;
t->free_mem += size;
t->free_bytes -= size;
t->last = size;
return ret;
}
void rst_mem_free_last(int type)
{
struct rst_mem_type_s *t = &rst_mems[type];
BUG_ON(!t->enabled);
t->free_mem -= t->last;
t->free_bytes += t->last;
t->last = 0; /* next free_last would be no-op */
}
unsigned long rst_mem_remap_size(void)
{
return rst_mems[RM_PRIVATE].size + rst_mems[RM_SHREMAP].size;
}
static int rst_mem_remap_one(struct rst_mem_type_s *t, void *to)
{
void *aux;
BUG_ON(!t->remapable);
if (!t->buf)
/*
* No allocations happenned from this buffer.
* It's safe just to do nothing.
*/
return 0;
pr_debug("\tcall mremap(%p, %lu, %lu, MAYMOVE | FIXED, %p)\n",
t->buf, t->size, t->size, to);
aux = mremap(t->buf, t->size, t->size, MREMAP_MAYMOVE | MREMAP_FIXED, to);
if (aux == MAP_FAILED) {
pr_perror("Can't mremap rst mem");
return -1;
}
t->buf = aux;
t->enabled = false;
return 0;
}
int rst_mem_remap(void *to)
{
int ret;
ret = rst_mem_remap_one(&rst_mems[RM_PRIVATE], to);
if (!ret) {
to += rst_mems[RM_PRIVATE].size;
ret = rst_mem_remap_one(&rst_mems[RM_SHREMAP], to);
}
return ret;
}
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