From patchwork Mon Nov 7 05:08:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haozhong Zhang X-Patchwork-Id: 9414271 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 782DD60585 for ; Mon, 7 Nov 2016 05:10:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6CB6F28CD4 for ; Mon, 7 Nov 2016 05:10:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 613A628CE6; Mon, 7 Nov 2016 05:10:13 +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 0CB2D28CD4 for ; Mon, 7 Nov 2016 05:10:13 +0000 (UTC) Received: from localhost ([::1]:51179 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c3cCC-00067b-9j for patchwork-qemu-devel@patchwork.kernel.org; Mon, 07 Nov 2016 00:10:12 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51004) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c3cBk-00065G-S1 for qemu-devel@nongnu.org; Mon, 07 Nov 2016 00:09:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c3cBj-0001an-Qz for qemu-devel@nongnu.org; Mon, 07 Nov 2016 00:09:44 -0500 Received: from mga02.intel.com ([134.134.136.20]:6606) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c3cBj-0001Ul-Iq for qemu-devel@nongnu.org; Mon, 07 Nov 2016 00:09:43 -0500 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga101.jf.intel.com with ESMTP; 06 Nov 2016 21:09:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,604,1473145200"; d="scan'208";a="458369" Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.148]) by orsmga002.jf.intel.com with ESMTP; 06 Nov 2016 21:09:41 -0800 From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Mon, 7 Nov 2016 13:08:59 +0800 Message-Id: <20161107050859.31058-4-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161107050859.31058-1-haozhong.zhang@intel.com> References: <20161107050859.31058-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.20 Subject: [Qemu-devel] [PATCH 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: Haozhong Zhang , Eduardo Habkost , Peter Crosthwaite , Paolo Bonzini , Igor Mammedov , Richard Henderson 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 specified, 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 | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index 42efb2f..56cc96b 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); + } + } + + if (!backend->size) { + error_setg(&local_err, "can't create backend with size 0"); } #endif + + out: + error_propagate(errp, local_err); } static char *get_mem_path(Object *o, Error **errp) diff --git a/exec.c b/exec.c index 42045aa..b81c9a8 100644 --- a/exec.c +++ b/exec.c @@ -1676,6 +1676,7 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, int fd = -1; bool unlink_on_error = false; int64_t file_size; + uint64_t mr_size; Error *local_err = NULL; if (xen_enabled()) { @@ -1704,6 +1705,32 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, */ file_size = get_file_size(fd); + if (!size && file_size >= 0) { + if (!file_size) { + error_setg(&local_err, + "%s is empty or a directory, 'size' option must be specified", + mem_path); + goto out; + } + + mr_size = memory_region_size(mr); + if (mr_size && mr_size != size) { + error_setg(&local_err, "cannot resize non-empty memory region"); + goto out; + } + if (!mr_size) { + memory_region_set_size(mr, file_size); + } + size = file_size; + } + + if (!size) { + error_setg(&local_err, + "cannot get size of %s, 'size' option must be specified", + mem_path); + goto out; + } + size = HOST_PAGE_ALIGN(size); if (file_size > 0 && file_size < size) { error_setg(&local_err, "backing store %s size 0x%"PRIx64