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

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