[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] hostmem: Add clear option to file backend
From: |
Fam Zheng |
Subject: |
[PATCH] hostmem: Add clear option to file backend |
Date: |
Thu, 2 Mar 2023 11:09:25 +0000 |
This adds a memset to clear the backing memory. This is useful in the
case of PMEM DAX to drop dirty data, if the backing memory is handed
over from a previous application or firmware which didn't clean up
before exiting.
Signed-off-by: Fam Zheng <fam.zheng@bytedance.com>
---
backends/hostmem-file.c | 20 ++++++++++++++++++++
include/exec/memory.h | 3 +++
qapi/qom.json | 3 +++
qemu-options.hx | 4 +++-
softmmu/physmem.c | 6 +++++-
5 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
index 25141283c4..f7468d24ce 100644
--- a/backends/hostmem-file.c
+++ b/backends/hostmem-file.c
@@ -30,6 +30,7 @@ struct HostMemoryBackendFile {
bool discard_data;
bool is_pmem;
bool readonly;
+ bool clear;
};
static void
@@ -56,6 +57,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error
**errp)
ram_flags = backend->share ? RAM_SHARED : 0;
ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
+ ram_flags |= fb->clear ? RAM_CLEAR : 0;
memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
backend->size, fb->align, ram_flags,
fb->mem_path, fb->readonly, errp);
@@ -168,6 +170,21 @@ static void file_memory_backend_set_readonly(Object *obj,
bool value,
fb->readonly = value;
}
+static bool file_memory_backend_get_clear(Object *obj, Error **errp)
+{
+ HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
+
+ return fb->clear;
+}
+
+static void file_memory_backend_set_clear(Object *obj, bool value,
+ Error **errp)
+{
+ HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
+
+ fb->clear = value;
+}
+
static void file_backend_unparent(Object *obj)
{
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
@@ -204,6 +221,9 @@ file_backend_class_init(ObjectClass *oc, void *data)
object_class_property_add_bool(oc, "readonly",
file_memory_backend_get_readonly,
file_memory_backend_set_readonly);
+ object_class_property_add_bool(oc, "clear",
+ file_memory_backend_get_clear,
+ file_memory_backend_set_clear);
}
static void file_backend_instance_finalize(Object *o)
diff --git a/include/exec/memory.h b/include/exec/memory.h
index 2e602a2fad..3345db5241 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -232,6 +232,9 @@ typedef struct IOMMUTLBEvent {
/* RAM that isn't accessible through normal means. */
#define RAM_PROTECTED (1 << 8)
+/* Clear these pages when mapping */
+#define RAM_CLEAR (1 << 9)
+
static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
IOMMUNotifierFlag flags,
hwaddr start, hwaddr end,
diff --git a/qapi/qom.json b/qapi/qom.json
index 30e76653ad..2c4aa5b0d5 100644
--- a/qapi/qom.json
+++ b/qapi/qom.json
@@ -629,6 +629,8 @@
# specify the required alignment via this option.
# 0 selects a default alignment (currently the page size). (default: 0)
#
+# @clear: if true, the memory is memset to 0 during init. (default: false)
+#
# @discard-data: if true, the file contents can be destroyed when QEMU exits,
# to avoid unnecessarily flushing data to the backing file. Note
# that ``discard-data`` is only an optimization, and QEMU might
@@ -649,6 +651,7 @@
{ 'struct': 'MemoryBackendFileProperties',
'base': 'MemoryBackendProperties',
'data': { '*align': 'size',
+ '*clear': 'bool',
'*discard-data': 'bool',
'mem-path': 'str',
'*pmem': { 'type': 'bool', 'if': 'CONFIG_LIBPMEM' },
diff --git a/qemu-options.hx b/qemu-options.hx
index beeb4475ba..6c8345c62e 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -4859,7 +4859,7 @@ SRST
they are specified. Note that the 'id' property must be set. These
objects are placed in the '/objects' path.
- ``-object
memory-backend-file,id=id,size=size,mem-path=dir,share=on|off,discard-data=on|off,merge=on|off,dump=on|off,prealloc=on|off,host-nodes=host-nodes,policy=default|preferred|bind|interleave,align=align,readonly=on|off``
+ ``-object
memory-backend-file,id=id,size=size,mem-path=dir,share=on|off,clear=on|off,discard-data=on|off,merge=on|off,dump=on|off,prealloc=on|off,host-nodes=host-nodes,policy=default|preferred|bind|interleave,align=align,readonly=on|off``
Creates a memory file backend object, which can be used to back
the guest RAM with huge pages.
@@ -4886,6 +4886,8 @@ SRST
Documentation/vm/numa\_memory\_policy.txt on the Linux kernel
source tree for additional details.
+ Setting clear=on will make QEMU memset the backend to 0.
+
Setting the ``discard-data`` boolean option to on indicates that
file contents can be destroyed when QEMU exits, to avoid
unnecessarily flushing data to the backing file. Note that
diff --git a/softmmu/physmem.c b/softmmu/physmem.c
index df54b917a9..573c686fd1 100644
--- a/softmmu/physmem.c
+++ b/softmmu/physmem.c
@@ -1409,6 +1409,10 @@ static void *file_ram_alloc(RAMBlock *block,
return NULL;
}
+ if (block->flags & RAM_CLEAR) {
+ memset(area, 0, memory);
+ }
+
block->fd = fd;
return area;
}
@@ -1868,7 +1872,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size,
MemoryRegion *mr,
/* Just support these ram flags by now. */
assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_NORESERVE |
- RAM_PROTECTED)) == 0);
+ RAM_PROTECTED | RAM_CLEAR)) == 0);
if (xen_enabled()) {
error_setg(errp, "-mem-path not supported with Xen");
--
2.20.1
- [PATCH] hostmem: Add clear option to file backend,
Fam Zheng <=
Re: [PATCH] hostmem: Add clear option to file backend, Daniel P . Berrangé, 2023/03/02