Skip to content

Commit

Permalink
memory: alloc RAM from file at offset
Browse files Browse the repository at this point in the history
Allow RAM MemoryRegion to be created from an offset in a file, instead
of allocating at offset of 0 by default. This is needed to synchronize
RAM between QEMU & remote process.

Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
Signed-off-by: John G Johnson <john.g.johnson@oracle.com>
Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 609996697ad8617e3b01df38accc5c208c24d74e.1611938319.git.jag.raman@oracle.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
  • Loading branch information
jraman567 authored and stefanhaRH committed Feb 5, 2021
1 parent c06a16e commit 2485303
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion backends/hostmem-memfd.c
Expand Up @@ -55,7 +55,7 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
name = host_memory_backend_get_name(backend);
memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend),
name, backend->size,
backend->share, fd, errp);
backend->share, fd, 0, errp);
g_free(name);
}

Expand Down
3 changes: 2 additions & 1 deletion hw/misc/ivshmem.c
Expand Up @@ -495,7 +495,8 @@ static void process_msg_shmem(IVShmemState *s, int fd, Error **errp)

/* mmap the region and map into the BAR2 */
memory_region_init_ram_from_fd(&s->server_bar2, OBJECT(s),
"ivshmem.bar2", size, true, fd, &local_err);
"ivshmem.bar2", size, true, fd, 0,
&local_err);
if (local_err) {
error_propagate(errp, local_err);
return;
Expand Down
2 changes: 2 additions & 0 deletions include/exec/memory.h
Expand Up @@ -992,6 +992,7 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
* @size: size of the region.
* @share: %true if memory must be mmaped with the MAP_SHARED flag
* @fd: the fd to mmap.
* @offset: offset within the file referenced by fd
* @errp: pointer to Error*, to store an error if it happens.
*
* Note that this function does not do anything to cause the data in the
Expand All @@ -1003,6 +1004,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr,
uint64_t size,
bool share,
int fd,
ram_addr_t offset,
Error **errp);
#endif

Expand Down
4 changes: 2 additions & 2 deletions include/exec/ram_addr.h
Expand Up @@ -121,8 +121,8 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
uint32_t ram_flags, const char *mem_path,
bool readonly, Error **errp);
RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
uint32_t ram_flags, int fd, bool readonly,
Error **errp);
uint32_t ram_flags, int fd, off_t offset,
bool readonly, Error **errp);

RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
MemoryRegion *mr, Error **errp);
Expand Down
4 changes: 3 additions & 1 deletion include/qemu/mmap-alloc.h
Expand Up @@ -17,6 +17,7 @@ size_t qemu_mempath_getpagesize(const char *mem_path);
* @readonly: true for a read-only mapping, false for read/write.
* @shared: map has RAM_SHARED flag.
* @is_pmem: map has RAM_PMEM flag.
* @map_offset: map starts at offset of map_offset from the start of fd
*
* Return:
* On success, return a pointer to the mapped area.
Expand All @@ -27,7 +28,8 @@ void *qemu_ram_mmap(int fd,
size_t align,
bool readonly,
bool shared,
bool is_pmem);
bool is_pmem,
off_t map_offset);

void qemu_ram_munmap(int fd, void *ptr, size_t size);

Expand Down
3 changes: 2 additions & 1 deletion softmmu/memory.c
Expand Up @@ -1612,6 +1612,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr,
uint64_t size,
bool share,
int fd,
ram_addr_t offset,
Error **errp)
{
Error *err = NULL;
Expand All @@ -1621,7 +1622,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr,
mr->destructor = memory_region_destructor_ram;
mr->ram_block = qemu_ram_alloc_from_fd(size, mr,
share ? RAM_SHARED : 0,
fd, false, &err);
fd, offset, false, &err);
if (err) {
mr->size = int128_zero();
object_unparent(OBJECT(mr));
Expand Down
12 changes: 7 additions & 5 deletions softmmu/physmem.c
Expand Up @@ -1463,6 +1463,7 @@ static void *file_ram_alloc(RAMBlock *block,
int fd,
bool readonly,
bool truncate,
off_t offset,
Error **errp)
{
void *area;
Expand Down Expand Up @@ -1513,7 +1514,8 @@ static void *file_ram_alloc(RAMBlock *block,
}

area = qemu_ram_mmap(fd, memory, block->mr->align, readonly,
block->flags & RAM_SHARED, block->flags & RAM_PMEM);
block->flags & RAM_SHARED, block->flags & RAM_PMEM,
offset);
if (area == MAP_FAILED) {
error_setg_errno(errp, errno,
"unable to map backing store for guest RAM");
Expand Down Expand Up @@ -1944,8 +1946,8 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared)

#ifdef CONFIG_POSIX
RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
uint32_t ram_flags, int fd, bool readonly,
Error **errp)
uint32_t ram_flags, int fd, off_t offset,
bool readonly, Error **errp)
{
RAMBlock *new_block;
Error *local_err = NULL;
Expand Down Expand Up @@ -1999,7 +2001,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
new_block->max_length = size;
new_block->flags = ram_flags;
new_block->host = file_ram_alloc(new_block, size, fd, readonly,
!file_size, errp);
!file_size, offset, errp);
if (!new_block->host) {
g_free(new_block);
return NULL;
Expand Down Expand Up @@ -2030,7 +2032,7 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
return NULL;
}

block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, readonly, errp);
block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, 0, readonly, errp);
if (!block) {
if (created) {
unlink(mem_path);
Expand Down
8 changes: 5 additions & 3 deletions util/mmap-alloc.c
Expand Up @@ -87,7 +87,8 @@ void *qemu_ram_mmap(int fd,
size_t align,
bool readonly,
bool shared,
bool is_pmem)
bool is_pmem,
off_t map_offset)
{
int prot;
int flags;
Expand Down Expand Up @@ -150,7 +151,8 @@ void *qemu_ram_mmap(int fd,

prot = PROT_READ | (readonly ? 0 : PROT_WRITE);

ptr = mmap(guardptr + offset, size, prot, flags | map_sync_flags, fd, 0);
ptr = mmap(guardptr + offset, size, prot,
flags | map_sync_flags, fd, map_offset);

if (ptr == MAP_FAILED && map_sync_flags) {
if (errno == ENOTSUP) {
Expand All @@ -174,7 +176,7 @@ void *qemu_ram_mmap(int fd,
* if map failed with MAP_SHARED_VALIDATE | MAP_SYNC,
* we will remove these flags to handle compatibility.
*/
ptr = mmap(guardptr + offset, size, prot, flags, fd, 0);
ptr = mmap(guardptr + offset, size, prot, flags, fd, map_offset);
}

if (ptr == MAP_FAILED) {
Expand Down
2 changes: 1 addition & 1 deletion util/oslib-posix.c
Expand Up @@ -230,7 +230,7 @@ void *qemu_memalign(size_t alignment, size_t size)
void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared)
{
size_t align = QEMU_VMALLOC_ALIGN;
void *ptr = qemu_ram_mmap(-1, size, align, false, shared, false);
void *ptr = qemu_ram_mmap(-1, size, align, false, shared, false, 0);

if (ptr == MAP_FAILED) {
return NULL;
Expand Down

0 comments on commit 2485303

Please sign in to comment.