This patch fixes two problems of memory-backend-file: 1. If user adds a memory-backend-file object using object_add command, specifying a non-existing directory for property mem-path, qemu will core dump with message:
/nonexistingdir: No such file or directory Bad ram offset fffffffffffff000 Aborted (core dumped) 2. If user adds a memory-backend-file object using object_add command, specifying a size that is less than huge page size, qemu will core dump with message: Bad ram offset fffffffffffff000 Aborted (core dumped) Signed-off-by: Hu Tao <hu...@cn.fujitsu.com> --- exec.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/exec.c b/exec.c index 8705cc5..a6afb4d 100644 --- a/exec.c +++ b/exec.c @@ -997,7 +997,7 @@ void qemu_mutex_unlock_ramlist(void) #define HUGETLBFS_MAGIC 0x958458f6 -static long gethugepagesize(const char *path) +static long gethugepagesize(const char *path, Error **errp) { struct statfs fs; int ret; @@ -1007,7 +1007,7 @@ static long gethugepagesize(const char *path) } while (ret != 0 && errno == EINTR); if (ret != 0) { - perror(path); + error_setg_errno(errp, errno, "failed to stat file %s", path); return 0; } @@ -1025,17 +1025,19 @@ static void *file_ram_alloc(RAMBlock *block, char *filename; char *sanitized_name; char *c; - void *area; + void *area = NULL; int fd; unsigned long hpagesize; - hpagesize = gethugepagesize(path); + hpagesize = gethugepagesize(path, errp); if (!hpagesize) { goto error; } if (memory < hpagesize) { - return NULL; + error_setg(errp, "memory size " RAM_ADDR_FMT " should be larger " + "than huge page size %" PRIx64, memory, hpagesize); + goto error; } if (kvm_enabled() && !kvm_has_sync_mmu()) { @@ -1095,8 +1097,8 @@ static void *file_ram_alloc(RAMBlock *block, return area; error: - if (mem_prealloc) { - exit(1); + if (area && area != MAP_FAILED) { + munmap(area, memory); } return NULL; } @@ -1279,6 +1281,7 @@ ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, Error **errp) { RAMBlock *new_block; + ram_addr_t addr; if (xen_enabled()) { error_setg(errp, "-mem-path not supported with Xen\n"); @@ -1308,7 +1311,14 @@ ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, return -1; } - return ram_block_add(new_block); + addr = ram_block_add(new_block); + if (addr == -1) { + g_free(new_block); + error_setg(errp, "failed to allocate memory\n"); + return -1; + } + + return addr; } #endif -- 1.9.3