[Qemu-devel] [PULL v6 11/12] hostmem-file: fix pmem file size check

Eduardo Habkost posted 12 patches 6 years, 5 months ago
Maintainers: Shannon Zhao <shannon.zhaosl@gmail.com>, Igor Mitsyanko <i.mitsyanko@gmail.com>, Andrew Jeffery <andrew@aj.id.au>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Eric Blake <eblake@redhat.com>, Richard Henderson <rth@twiddle.net>, "Michael S. Tsirkin" <mst@redhat.com>, Beniamino Galvani <b.galvani@gmail.com>, Andrey Smirnov <andrew.smirnov@gmail.com>, "Philippe Mathieu-Daudé" <f4bug@amsat.org>, Andrew Baumann <Andrew.Baumann@microsoft.com>, David Gibson <david@gibson.dropbear.id.au>, Eduardo Habkost <ehabkost@redhat.com>, Peter Chubb <peter.chubb@nicta.com.au>, Peter Maydell <peter.maydell@linaro.org>, Andrzej Zaborowski <balrogg@gmail.com>, Jean-Christophe Dubois <jcd@tribudubois.net>, Paolo Bonzini <pbonzini@redhat.com>, Leif Lindholm <leif.lindholm@linaro.org>, Igor Mammedov <imammedo@redhat.com>, Alistair Francis <alistair@alistair23.me>, "Cédric Le Goater" <clg@kaod.org>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Joel Stanley <joel@jms.id.au>, Markus Armbruster <armbru@redhat.com>, Jan Kiszka <jan.kiszka@web.de>, Rob Herring <robh@kernel.org>, Radoslaw Biernacki <radoslaw.biernacki@linaro.org>
[Qemu-devel] [PULL v6 11/12] hostmem-file: fix pmem file size check
Posted by Eduardo Habkost 6 years, 5 months ago
From: Stefan Hajnoczi <stefanha@redhat.com>

Commit 314aec4a6e06844937f1677f6cba21981005f389 ("hostmem-file: reject
invalid pmem file sizes") added a file size check that verifies the
hostmem object's size parameter against the actual devdax pmem file.
This is useful because getting the size wrong results in confusing
errors inside the guest.

However, the code doesn't work properly for files where struct
stat::st_size is zero.  Hostmem-file's ->alloc() function returns early
without setting an Error, causing the following assertion failure:

  qemu/memory.c:2215: memory_region_get_ram_ptr: Assertion `mr->ram_block' failed.

This patch handles the case where qemu_get_pmem_size() returns 0 but
there is no error.

Fixes: 314aec4a6e06844937f1677f6cba21981005f389
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-Id: <20190823135632.25010-1-stefanha@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 backends/hostmem-file.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
index 29e55c9195..ecc15e3eb0 100644
--- a/backends/hostmem-file.c
+++ b/backends/hostmem-file.c
@@ -67,12 +67,12 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
         uint64_t size;
 
         size = qemu_get_pmem_size(fb->mem_path, &local_err);
-        if (!size) {
+        if (local_err) {
             error_propagate(errp, local_err);
             return;
         }
 
-        if (backend->size > size) {
+        if (size && backend->size > size) {
             error_setg(errp, "size property %" PRIu64 " is larger than "
                        "pmem file \"%s\" size %" PRIu64, backend->size,
                        fb->mem_path, size);
-- 
2.21.0