Blame SOURCES/kvm-hostmem-file-Add-discard-data-option.patch

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