From patchwork Thu Oct 27 04:23:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haozhong Zhang X-Patchwork-Id: 9398875 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9389460231 for ; Thu, 27 Oct 2016 04:28:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5D19029F62 for ; Thu, 27 Oct 2016 04:28:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4F7AD29F66; Thu, 27 Oct 2016 04:28:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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 mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 873B029F62 for ; Thu, 27 Oct 2016 04:28:01 +0000 (UTC) Received: from localhost ([::1]:38852 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bzcIK-0002Hp-0s for patchwork-qemu-devel@patchwork.kernel.org; Thu, 27 Oct 2016 00:28:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34838) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bzcEP-0007gM-Ig for qemu-devel@nongnu.org; Thu, 27 Oct 2016 00:23:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bzcEO-0006Rb-Fo for qemu-devel@nongnu.org; Thu, 27 Oct 2016 00:23:57 -0400 Received: from mga09.intel.com ([134.134.136.24]:33340) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1bzcEO-0006Ld-3w for qemu-devel@nongnu.org; Thu, 27 Oct 2016 00:23:56 -0400 Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga102.jf.intel.com with ESMTP; 26 Oct 2016 21:23:56 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,404,1473145200"; d="scan'208";a="24248228" Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.148]) by orsmga005.jf.intel.com with ESMTP; 26 Oct 2016 21:23:54 -0700 From: Haozhong Zhang To: qemu-devel@nongnu.org, Eduardo Habkost , Igor Mammedov Date: Thu, 27 Oct 2016 12:23:00 +0800 Message-Id: <20161027042300.5929-4-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161027042300.5929-1-haozhong.zhang@intel.com> References: <20161027042300.5929-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.24 Subject: [Qemu-devel] [PATCH v2 3/3] hostmem-file: make option 'size' optional X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Richard Henderson , Haozhong Zhang , Peter Crosthwaite Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP If 'size' option is not given, Qemu will use the file size of 'mem-path' instead. If an empty file, a non-existing file or a directory is specified by option 'mem-path', a non-zero option 'size' is still needed. Signed-off-by: Haozhong Zhang --- backends/hostmem-file.c | 28 ++++++++++++++++++++-------- exec.c | 33 ++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index 42efb2f..6ee4352 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -39,17 +39,14 @@ static void file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) { HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend); + Error *local_err = NULL; - if (!backend->size) { - error_setg(errp, "can't create backend with size 0"); - return; - } if (!fb->mem_path) { - error_setg(errp, "mem-path property not set"); - return; + error_setg(&local_err, "mem-path property not set"); + goto out; } #ifndef CONFIG_LINUX - error_setg(errp, "-mem-path not supported on this host"); + error_setg(&local_err, "-mem-path not supported on this host"); #else if (!memory_region_size(&backend->mr)) { gchar *path; @@ -58,10 +55,25 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), path, backend->size, fb->share, - fb->mem_path, errp); + fb->mem_path, &local_err); g_free(path); + + if (local_err) { + goto out; + } + + if (!backend->size) { + backend->size = memory_region_size(&backend->mr); + } } #endif + + if (!backend->size) { + error_setg(&local_err, "can't create backend with size 0"); + } + + out: + error_propagate(errp, local_err); } static char *get_mem_path(Object *o, Error **errp) diff --git a/exec.c b/exec.c index 264a25f..89065bd 100644 --- a/exec.c +++ b/exec.c @@ -1234,7 +1234,7 @@ static int64_t get_file_size(int fd) } static void *file_ram_alloc(RAMBlock *block, - ram_addr_t memory, + ram_addr_t *memory, const char *path, Error **errp) { @@ -1245,6 +1245,7 @@ static void *file_ram_alloc(RAMBlock *block, void *area = MAP_FAILED; int fd = -1; int64_t file_size; + ram_addr_t mem_size = *memory; if (kvm_enabled() && !kvm_has_sync_mmu()) { error_setg(errp, @@ -1309,21 +1310,27 @@ static void *file_ram_alloc(RAMBlock *block, file_size = get_file_size(fd); - if (memory < block->page_size) { + if (!mem_size && file_size > 0) { + mem_size = file_size; + memory_region_set_size(block->mr, mem_size); + } + + if (mem_size < block->page_size) { error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to " "or larger than page size 0x%zx", - memory, block->page_size); + mem_size, block->page_size); goto error; } - if (file_size > 0 && file_size < memory) { + if (file_size > 0 && file_size < mem_size) { error_setg(errp, "backing store %s size %"PRId64 " does not match 'size' option %"PRIu64, - path, file_size, memory); + path, file_size, mem_size); goto error; } - memory = ROUND_UP(memory, block->page_size); + mem_size = ROUND_UP(mem_size, block->page_size); + *memory = mem_size; /* * ftruncate is not supported by hugetlbfs in older @@ -1339,11 +1346,11 @@ static void *file_ram_alloc(RAMBlock *block, * those labels. Therefore, extending the non-empty backend file * is disabled as well. */ - if (!file_size && ftruncate(fd, memory)) { + if (!file_size && ftruncate(fd, mem_size)) { perror("ftruncate"); } - area = qemu_ram_mmap(fd, memory, block->mr->align, + area = qemu_ram_mmap(fd, mem_size, block->mr->align, block->flags & RAM_SHARED); if (area == MAP_FAILED) { error_setg_errno(errp, errno, @@ -1352,7 +1359,7 @@ static void *file_ram_alloc(RAMBlock *block, } if (mem_prealloc) { - os_mem_prealloc(fd, area, memory, errp); + os_mem_prealloc(fd, area, mem_size, errp); if (errp && *errp) { goto error; } @@ -1363,7 +1370,7 @@ static void *file_ram_alloc(RAMBlock *block, error: if (area != MAP_FAILED) { - qemu_ram_munmap(area, memory); + qemu_ram_munmap(area, mem_size); } if (unlink_on_error) { unlink(path); @@ -1690,15 +1697,15 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, size = HOST_PAGE_ALIGN(size); new_block = g_malloc0(sizeof(*new_block)); new_block->mr = mr; - new_block->used_length = size; - new_block->max_length = size; new_block->flags = share ? RAM_SHARED : 0; - new_block->host = file_ram_alloc(new_block, size, + new_block->host = file_ram_alloc(new_block, &size, mem_path, errp); if (!new_block->host) { g_free(new_block); return NULL; } + new_block->used_length = size; + new_block->max_length = size; ram_block_add(new_block, &local_err); if (local_err) {