From ebf96eb206cfc94a1db16f28b281fee0c56fef8c Mon Sep 17 00:00:00 2001
From: Eduardo Habkost <ehabkost@redhat.com>
Date: Thu, 19 Oct 2017 01:34:53 +0200
Subject: [PATCH 64/69] hostmem-file: Add "discard-data" option
RH-Author: Eduardo Habkost <ehabkost@redhat.com>
Message-id: <20171019013453.21449-5-ehabkost@redhat.com>
Patchwork-id: 77370
O-Subject: [RHV7.5 qemu-kvm-rhev PATCH 4/4] hostmem-file: Add "discard-data" option
Bugzilla: 1460848
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
The new option can be used to indicate that the file contents can
be destroyed and don't need to be flushed to disk when QEMU exits
or when the memory backend object is removed.
Internally, it will trigger a madvise(MADV_REMOVE) call when the
memory backend is removed.
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Message-Id: <20170824192315.5897-4-ehabkost@redhat.com>
[ehabkost: fixup: improved documentation]
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
Tested-by: Zack Cornelius <zack.cornelius@kove.net>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
(cherry picked from commit 11ae6ed8affdd131e735bac39b21e7d3cde66f7b)
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
---
backends/hostmem-file.c | 29 +++++++++++++++++++++++++++++
qemu-options.hx | 8 +++++++-
2 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
index fc4ef46..e44c319 100644
--- a/backends/hostmem-file.c
+++ b/backends/hostmem-file.c
@@ -32,6 +32,7 @@ struct HostMemoryBackendFile {
HostMemoryBackend parent_obj;
bool share;
+ bool discard_data;
char *mem_path;
};
@@ -103,16 +104,44 @@ static void file_memory_backend_set_share(Object *o, bool value, Error **errp)
fb->share = value;
}
+static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
+{
+ return MEMORY_BACKEND_FILE(o)->discard_data;
+}
+
+static void file_memory_backend_set_discard_data(Object *o, bool value,
+ Error **errp)
+{
+ MEMORY_BACKEND_FILE(o)->discard_data = value;
+}
+
+static void file_backend_unparent(Object *obj)
+{
+ HostMemoryBackend *backend = MEMORY_BACKEND(obj);
+ HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
+
+ if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
+ void *ptr = memory_region_get_ram_ptr(&backend->mr);
+ uint64_t sz = memory_region_size(&backend->mr);
+
+ qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
+ }
+}
+
static void
file_backend_class_init(ObjectClass *oc, void *data)
{
HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
bc->alloc = file_backend_memory_alloc;
+ oc->unparent = file_backend_unparent;
object_class_property_add_bool(oc, "share",
file_memory_backend_get_share, file_memory_backend_set_share,
&error_abort);
+ object_class_property_add_bool(oc, "discard-data",
+ file_memory_backend_get_discard_data, file_memory_backend_set_discard_data,
+ &error_abort);
object_class_property_add_str(oc, "mem-path",
get_mem_path, set_mem_path,
&error_abort);
diff --git a/qemu-options.hx b/qemu-options.hx
index 5220120..50ba50e 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -4155,7 +4155,7 @@ property must be set. These objects are placed in the
@table @option
-@item -object memory-backend-file,id=@var{id},size=@var{size},mem-path=@var{dir},share=@var{on|off}
+@item -object memory-backend-file,id=@var{id},size=@var{size},mem-path=@var{dir},share=@var{on|off},discard-data=@var{on|off}
Creates a memory file backend object, which can be used to back
the guest RAM with huge pages. The @option{id} parameter is a
@@ -4167,6 +4167,12 @@ the path to either a shared memory or huge page filesystem mount.
The @option{share} boolean option determines whether the memory
region is marked as private to QEMU, or shared. The latter allows
a co-operating external process to access the QEMU memory region.
+Setting the @option{discard-data} boolean option to @var{on}
+indicates that file contents can be destroyed when QEMU exits,
+to avoid unnecessarily flushing data to the backing file. Note
+that @option{discard-data} is only an optimization, and QEMU
+might not discard file contents if it aborts unexpectedly or is
+terminated using SIGKILL.
@item -object rng-random,id=@var{id},filename=@var{/dev/random}
--
1.8.3.1