From patchwork Tue May 6 09:27:37 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hu Tao X-Patchwork-Id: 346111 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id C02181412F4 for ; Tue, 6 May 2014 19:46:57 +1000 (EST) Received: from localhost ([::1]:33920 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Whbxf-0007FX-Mh for incoming@patchwork.ozlabs.org; Tue, 06 May 2014 05:46:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39083) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WhboG-0008MN-Qi for qemu-devel@nongnu.org; Tue, 06 May 2014 05:37:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WhboA-0008GV-4u for qemu-devel@nongnu.org; Tue, 06 May 2014 05:37:12 -0400 Received: from [59.151.112.132] (port=22452 helo=heian.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Whbo9-0008CI-7L for qemu-devel@nongnu.org; Tue, 06 May 2014 05:37:06 -0400 X-IronPort-AV: E=Sophos;i="4.97,996,1389715200"; d="scan'208";a="30131300" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 06 May 2014 17:27:25 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id s469Ttbv003084; Tue, 6 May 2014 17:29:55 +0800 Received: from G08FNSTD100614.fnst.cn.fujitsu.com (10.167.226.102) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.146.2; Tue, 6 May 2014 17:30:04 +0800 From: Hu Tao To: Date: Tue, 6 May 2014 17:27:37 +0800 Message-ID: <699c6836a33da9b1ff6b5a8a661d8192684d730f.1399365798.git.hutao@cn.fujitsu.com> X-Mailer: git-send-email 1.8.5.2.229.g4448466 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.167.226.102] X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 59.151.112.132 Cc: Paolo Bonzini , Igor Mammedov Subject: [Qemu-devel] [PATCH v3.1 16/31] memory: add error propagation to file-based RAM allocation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Paolo Bonzini Right now, -mem-path will fall back to RAM-based allocation in some cases. This should never happen with "-object memory-file", prepare the code by adding correct error propagation. Signed-off-by: Paolo Bonzini Signed-off-by: Hu Tao --- exec.c | 36 ++++++++++++++++++++++++------------ include/exec/memory.h | 5 ++++- include/exec/ram_addr.h | 2 +- memory.c | 5 +++-- numa.c | 13 ++++++++++++- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/exec.c b/exec.c index 379472d..1e51b2f 100644 --- a/exec.c +++ b/exec.c @@ -1021,7 +1021,8 @@ static void sigbus_handler(int signal) static void *file_ram_alloc(RAMBlock *block, ram_addr_t memory, - const char *path) + const char *path, + Error **errp) { char *filename; char *sanitized_name; @@ -1040,7 +1041,8 @@ static void *file_ram_alloc(RAMBlock *block, } if (kvm_enabled() && !kvm_has_sync_mmu()) { - fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n"); + error_setg(errp, + "host lacks kvm mmu notifiers, -mem-path unsupported\n"); goto error; } @@ -1057,7 +1059,8 @@ static void *file_ram_alloc(RAMBlock *block, fd = mkstemp(filename); if (fd < 0) { - perror("unable to create backing store for hugepages"); + error_setg_errno(errp, errno, + "unable to create backing store for hugepages"); g_free(filename); goto error; } @@ -1072,12 +1075,14 @@ static void *file_ram_alloc(RAMBlock *block, * If anything goes wrong with it under other filesystems, * mmap will fail. */ - if (ftruncate(fd, memory)) + if (ftruncate(fd, memory)) { perror("ftruncate"); + } area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); if (area == MAP_FAILED) { - perror("file_ram_alloc: can't mmap RAM pages"); + error_setg_errno(errp, errno, + "unable to map backing store for hugepages"); close(fd); goto error; } @@ -1305,13 +1310,14 @@ static ram_addr_t ram_block_add(RAMBlock *new_block) #ifdef __linux__ ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, - const char *mem_path) + const char *mem_path, + Error **errp) { RAMBlock *new_block; if (xen_enabled()) { - fprintf(stderr, "-mem-path not supported with Xen\n"); - exit(1); + error_setg(errp, "-mem-path not supported with Xen\n"); + return -1; } if (phys_mem_alloc != qemu_anon_ram_alloc) { @@ -1320,16 +1326,22 @@ ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, * phys_mem_alloc, but we haven't bothered to provide * a hook there. */ - fprintf(stderr, - "-mem-path not supported with this accelerator\n"); - exit(1); + error_setg(errp, + "-mem-path not supported with this accelerator\n"); + return -1; } size = TARGET_PAGE_ALIGN(size); new_block = g_malloc0(sizeof(*new_block)); new_block->mr = mr; new_block->length = size; - new_block->host = file_ram_alloc(new_block, size, mem_path); + new_block->host = file_ram_alloc(new_block, size, + mem_path, errp); + if (!new_block->host) { + g_free(new_block); + return -1; + } + return ram_block_add(new_block); } #endif diff --git a/include/exec/memory.h b/include/exec/memory.h index 75d4635..4e606dd 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -31,6 +31,7 @@ #include "qemu/queue.h" #include "qemu/int128.h" #include "qemu/notify.h" +#include "qapi/error.h" #define MAX_PHYS_ADDR_SPACE_BITS 62 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1) @@ -321,12 +322,14 @@ void memory_region_init_ram(MemoryRegion *mr, * @name: the name of the region. * @size: size of the region. * @path: the path in which to allocate the RAM. + * @errp: pointer to Error*, to store an error if it happens. */ void memory_region_init_ram_from_file(MemoryRegion *mr, struct Object *owner, const char *name, uint64_t size, - const char *path); + const char *path, + Error **errp); #endif /** diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index dedb258..f9518a6 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -23,7 +23,7 @@ #include "hw/xen/xen.h" ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, - const char *mem_path); + const char *mem_path, Error **errp); ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, MemoryRegion *mr); ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr); diff --git a/memory.c b/memory.c index 677b8f3..6b0c116 100644 --- a/memory.c +++ b/memory.c @@ -1025,13 +1025,14 @@ void memory_region_init_ram_from_file(MemoryRegion *mr, struct Object *owner, const char *name, uint64_t size, - const char *path) + const char *path, + Error **errp) { memory_region_init(mr, owner, name, size); mr->ram = true; mr->terminates = true; mr->destructor = memory_region_destructor_ram; - mr->ram_addr = qemu_ram_alloc_from_file(size, mr, path); + mr->ram_addr = qemu_ram_alloc_from_file(size, mr, path, errp); } #endif diff --git a/numa.c b/numa.c index e136612..966cacc 100644 --- a/numa.c +++ b/numa.c @@ -231,7 +231,18 @@ static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner, { if (mem_path) { #ifdef __linux__ - memory_region_init_ram_from_file(mr, owner, name, ram_size, mem_path); + Error *err = NULL; + memory_region_init_ram_from_file(mr, owner, name, ram_size, + mem_path, &err); + + /* Legacy behavior: if allocation failed, fall back to + * regular RAM allocation. + */ + if (!memory_region_size(mr)) { + qerror_report_err(err); + error_free(err); + memory_region_init_ram(mr, owner, name, ram_size); + } #else fprintf(stderr, "-mem-path not supported on this host\n"); exit(1);