From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Xiao Guangrong" <xiaoguangrong.eric@gmail.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Julio Montes" <julio.montes@intel.com>,
"Eduardo Habkost" <ehabkost@redhat.com>,
eric.g.ernst@gmail.com, "Stefan Hajnoczi" <stefanha@redhat.com>,
"Igor Mammedov" <imammedo@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>,
"Richard Henderson" <rth@twiddle.net>
Subject: [PATCH v3 2/3] hostmem-file: add readonly=on|off option
Date: Mon, 4 Jan 2021 17:13:19 +0000 [thread overview]
Message-ID: <20210104171320.575838-3-stefanha@redhat.com> (raw)
In-Reply-To: <20210104171320.575838-1-stefanha@redhat.com>
Let -object memory-backend-file work on read-only files when the
readonly=on option is given. This can be used to share the contents of a
file between multiple guests while preventing them from consuming
Copy-on-Write memory if guests dirty the pages, for example.
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Liam Merwick <liam.merwick@oracle.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
v3:
* Use Object *obj instead of Object *o [Igor]
* Do not dereference MEMORY_BACKEND_FILE(o)->readonly directly, use a
local variable to hold the HostMemoryBackendFile pointer. [Igor]
---
backends/hostmem-file.c | 28 +++++++++++++++++++++++++++-
qemu-options.hx | 5 ++++-
2 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
index e827692124..733408e076 100644
--- a/backends/hostmem-file.c
+++ b/backends/hostmem-file.c
@@ -29,6 +29,7 @@ struct HostMemoryBackendFile {
uint64_t align;
bool discard_data;
bool is_pmem;
+ bool readonly;
};
static void
@@ -56,7 +57,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
backend->size, fb->align,
(backend->share ? RAM_SHARED : 0) |
(fb->is_pmem ? RAM_PMEM : 0),
- fb->mem_path, false, errp);
+ fb->mem_path, fb->readonly, errp);
g_free(name);
#endif
}
@@ -151,6 +152,28 @@ static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
fb->is_pmem = value;
}
+static bool file_memory_backend_get_readonly(Object *obj, Error **errp)
+{
+ HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
+
+ return fb->readonly;
+}
+
+static void file_memory_backend_set_readonly(Object *obj, bool value,
+ Error **errp)
+{
+ HostMemoryBackend *backend = MEMORY_BACKEND(obj);
+ HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
+
+ if (host_memory_backend_mr_inited(backend)) {
+ error_setg(errp, "cannot change property 'readonly' of %s.",
+ object_get_typename(obj));
+ return;
+ }
+
+ fb->readonly = value;
+}
+
static void file_backend_unparent(Object *obj)
{
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
@@ -182,6 +205,9 @@ file_backend_class_init(ObjectClass *oc, void *data)
NULL, NULL);
object_class_property_add_bool(oc, "pmem",
file_memory_backend_get_pmem, file_memory_backend_set_pmem);
+ object_class_property_add_bool(oc, "readonly",
+ file_memory_backend_get_readonly,
+ file_memory_backend_set_readonly);
}
static void file_backend_instance_finalize(Object *o)
diff --git a/qemu-options.hx b/qemu-options.hx
index 459c916d3d..4732c6b45e 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -4407,7 +4407,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``
+ ``-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``
Creates a memory file backend object, which can be used to back
the guest RAM with huge pages.
@@ -4490,6 +4490,9 @@ SRST
4.15) and the filesystem of ``mem-path`` mounted with DAX
option.
+ The ``readonly`` option specifies whether the backing file is opened
+ read-only or read-write (default).
+
``-object memory-backend-ram,id=id,merge=on|off,dump=on|off,share=on|off,prealloc=on|off,size=size,host-nodes=host-nodes,policy=default|preferred|bind|interleave``
Creates a memory backend object, which can be used to back the
guest RAM. Memory backend objects offer more control than the
--
2.29.2
next prev parent reply other threads:[~2021-01-04 17:14 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-04 17:13 [PATCH v3 0/3] nvdimm: read-only file support Stefan Hajnoczi
2021-01-04 17:13 ` [PATCH v3 1/3] memory: add readonly support to memory_region_init_ram_from_file() Stefan Hajnoczi
2021-01-04 17:13 ` Stefan Hajnoczi [this message]
2021-01-04 17:13 ` [PATCH v3 3/3] nvdimm: check -object memory-backend-file, readonly=on option Stefan Hajnoczi
2021-01-04 21:02 ` [PATCH v3 0/3] nvdimm: read-only file support Eduardo Habkost
2021-01-14 14:05 ` Stefan Hajnoczi
2021-01-21 20:41 ` Eduardo Habkost
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210104171320.575838-3-stefanha@redhat.com \
--to=stefanha@redhat.com \
--cc=ehabkost@redhat.com \
--cc=eric.g.ernst@gmail.com \
--cc=imammedo@redhat.com \
--cc=julio.montes@intel.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=philmd@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=rth@twiddle.net \
--cc=xiaoguangrong.eric@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).