Received: from eggs.gnu.org ([2001:4830:134:3::10]:58894) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bz7k3-0000wi-Ub for qemu-devel@nongnu.org; Tue, 25 Oct 2016 15:50:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bz7k0-0004qq-Kv for qemu-devel@nongnu.org; Tue, 25 Oct 2016 15:50:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47444) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1bz7k0-0004qc-Ds for qemu-devel@nongnu.org; Tue, 25 Oct 2016 15:50:32 -0400 Date: Tue, 25 Oct 2016 17:50:28 -0200 From: Eduardo Habkost Message-ID: <20161025195028.GX5057@thinpad.lan.raisama.net> References: <20161024092151.32386-1-haozhong.zhang@intel.com> <20161024092151.32386-3-haozhong.zhang@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20161024092151.32386-3-haozhong.zhang@intel.com> Subject: Re: [Qemu-devel] [PATCH 2/2] hostmem-file: allow option 'size' optional List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Haozhong Zhang Cc: qemu-devel@nongnu.org, Igor Mammedov , Paolo Bonzini , Peter Crosthwaite , Richard Henderson , Xiao Guangrong On Mon, Oct 24, 2016 at 05:21:51PM +0800, Haozhong Zhang wrote: > If 'size' option of hostmem-file is not given, QEMU will use the file > size of 'mem-path' instead. For an empty file, a non-zero size must be > specified by the option 'size'. > > Signed-off-by: Haozhong Zhang > --- > backends/hostmem-file.c | 10 ++++++---- > exec.c | 8 ++++++++ > 2 files changed, 14 insertions(+), 4 deletions(-) > > diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c > index 42efb2f..f94d2f7 100644 > --- a/backends/hostmem-file.c > +++ b/backends/hostmem-file.c > @@ -40,10 +40,6 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) > { > HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend); > > - 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; > @@ -62,6 +58,12 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) > g_free(path); > } > #endif > + if (!errp && !backend->size) { This condition is always false because non-NULL errp is always provided by the only caller (host_memory_backend_memory_complete()). To simplify error checking, I suggest moving the error path to a label at the end of the function, e.g.: static void file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) { Error *local_err = NULL; /* ... */ memory_region_init_ram_from_file(..., &local_err); if (local_err) { goto out; } /* ... */ if (!backend->size) { backend->size = memory_region_size(&backend->mr); } /* ... */ out: error_propagate(errp, local_err); } > + backend->size = memory_region_size(&backend->mr); > + if (!backend->size) { > + error_setg(errp, "can't create backend with size 0"); > + } > + } > } > > static char *get_mem_path(Object *o, Error **errp) > diff --git a/exec.c b/exec.c > index 95983c9..91adc62 100644 > --- a/exec.c > +++ b/exec.c > @@ -1274,6 +1274,14 @@ static void *file_ram_alloc(RAMBlock *block, > goto error; > } > > + if (memory) { > + memory = memory ?: file_size; This doesn't make sense to me. You already checked if memory is zero above, and now you are checking if it's zero again. file_size is never going to be used here. > + memory_region_set_size(block->mr, memory); > + memory = HOST_PAGE_ALIGN(memory); > + block->used_length = memory; > + block->max_length = memory; This is fragile: it duplicates the logic that initializes used_length and max_length in qemu_ram_alloc_*(). Maybe it's better to keep the file-size-probing magic inside hostmem-file.c, and always give a non-zero size to memory_region_init_ram_from_file(). > + } > + > if (memory < block->page_size) { > error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to " > "or larger than page size 0x%zx", > -- > 2.10.1 > -- Eduardo