|
@@ -48,6 +48,7 @@
|
|
|
#include "qemu/qemu-print.h"
|
|
|
#include "qemu/log.h"
|
|
|
#include "qemu/memalign.h"
|
|
|
+#include "qemu/memfd.h"
|
|
|
#include "exec/memory.h"
|
|
|
#include "exec/ioport.h"
|
|
|
#include "system/dma.h"
|
|
@@ -1948,6 +1949,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, ram_addr_t max_size,
|
|
|
bool grow,
|
|
|
Error **errp)
|
|
|
{
|
|
|
+ ERRP_GUARD();
|
|
|
RAMBlock *new_block;
|
|
|
Error *local_err = NULL;
|
|
|
int64_t file_size, file_align;
|
|
@@ -2068,6 +2070,25 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
+#ifdef CONFIG_POSIX
|
|
|
+/*
|
|
|
+ * Create MAP_SHARED RAMBlocks by mmap'ing a file descriptor, so it can be
|
|
|
+ * shared with another process if CPR is being used. Use memfd if available
|
|
|
+ * because it has no size limits, else use POSIX shm.
|
|
|
+ */
|
|
|
+static int qemu_ram_get_shared_fd(const char *name, Error **errp)
|
|
|
+{
|
|
|
+ int fd;
|
|
|
+
|
|
|
+ if (qemu_memfd_check(0)) {
|
|
|
+ fd = qemu_memfd_create(name, 0, 0, 0, 0, errp);
|
|
|
+ } else {
|
|
|
+ fd = qemu_shm_alloc(0, errp);
|
|
|
+ }
|
|
|
+ return fd;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
static
|
|
|
RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
|
|
|
qemu_ram_resize_cb resized,
|
|
@@ -2081,6 +2102,41 @@ RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
|
|
|
assert((ram_flags & ~(RAM_SHARED | RAM_RESIZEABLE | RAM_PREALLOC |
|
|
|
RAM_NORESERVE | RAM_GUEST_MEMFD)) == 0);
|
|
|
assert(!host ^ (ram_flags & RAM_PREALLOC));
|
|
|
+ assert(max_size >= size);
|
|
|
+
|
|
|
+#ifdef CONFIG_POSIX /* ignore RAM_SHARED for Windows */
|
|
|
+ if (!host) {
|
|
|
+ if (ram_flags & RAM_SHARED) {
|
|
|
+ const char *name = memory_region_name(mr);
|
|
|
+ int fd = qemu_ram_get_shared_fd(name, errp);
|
|
|
+
|
|
|
+ if (fd < 0) {
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Use same alignment as qemu_anon_ram_alloc */
|
|
|
+ mr->align = QEMU_VMALLOC_ALIGN;
|
|
|
+
|
|
|
+ /*
|
|
|
+ * This can fail if the shm mount size is too small, or alloc from
|
|
|
+ * fd is not supported, but previous QEMU versions that called
|
|
|
+ * qemu_anon_ram_alloc for anonymous shared memory could have
|
|
|
+ * succeeded. Quietly fail and fall back.
|
|
|
+ */
|
|
|
+ new_block = qemu_ram_alloc_from_fd(size, max_size, resized, mr,
|
|
|
+ ram_flags, fd, 0, false, NULL);
|
|
|
+ if (new_block) {
|
|
|
+ trace_qemu_ram_alloc_shared(name, new_block->used_length,
|
|
|
+ new_block->max_length, fd,
|
|
|
+ new_block->host);
|
|
|
+ return new_block;
|
|
|
+ }
|
|
|
+
|
|
|
+ close(fd);
|
|
|
+ /* fall back to anon allocation */
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
|
|
|
align = qemu_real_host_page_size();
|
|
|
align = MAX(align, TARGET_PAGE_SIZE);
|
|
@@ -2092,7 +2148,6 @@ RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
|
|
|
new_block->resized = resized;
|
|
|
new_block->used_length = size;
|
|
|
new_block->max_length = max_size;
|
|
|
- assert(max_size >= size);
|
|
|
new_block->fd = -1;
|
|
|
new_block->guest_memfd = -1;
|
|
|
new_block->page_size = qemu_real_host_page_size();
|