hostmem-file: add readonly=on|off option
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. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com> Reviewed-by: Liam Merwick <liam.merwick@oracle.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20210104171320.575838-3-stefanha@redhat.com> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
This commit is contained in:
parent
369d6dc4de
commit
86635aa4e9
|
@ -29,6 +29,7 @@ struct HostMemoryBackendFile {
|
||||||
uint64_t align;
|
uint64_t align;
|
||||||
bool discard_data;
|
bool discard_data;
|
||||||
bool is_pmem;
|
bool is_pmem;
|
||||||
|
bool readonly;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -56,7 +57,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
|
||||||
backend->size, fb->align,
|
backend->size, fb->align,
|
||||||
(backend->share ? RAM_SHARED : 0) |
|
(backend->share ? RAM_SHARED : 0) |
|
||||||
(fb->is_pmem ? RAM_PMEM : 0),
|
(fb->is_pmem ? RAM_PMEM : 0),
|
||||||
fb->mem_path, false, errp);
|
fb->mem_path, fb->readonly, errp);
|
||||||
g_free(name);
|
g_free(name);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -151,6 +152,28 @@ static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
|
||||||
fb->is_pmem = value;
|
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)
|
static void file_backend_unparent(Object *obj)
|
||||||
{
|
{
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
||||||
|
@ -182,6 +205,9 @@ file_backend_class_init(ObjectClass *oc, void *data)
|
||||||
NULL, NULL);
|
NULL, NULL);
|
||||||
object_class_property_add_bool(oc, "pmem",
|
object_class_property_add_bool(oc, "pmem",
|
||||||
file_memory_backend_get_pmem, file_memory_backend_set_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)
|
static void file_backend_instance_finalize(Object *o)
|
||||||
|
|
|
@ -4426,7 +4426,7 @@ SRST
|
||||||
they are specified. Note that the 'id' property must be set. These
|
they are specified. Note that the 'id' property must be set. These
|
||||||
objects are placed in the '/objects' path.
|
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
|
Creates a memory file backend object, which can be used to back
|
||||||
the guest RAM with huge pages.
|
the guest RAM with huge pages.
|
||||||
|
|
||||||
|
@ -4509,6 +4509,9 @@ SRST
|
||||||
4.15) and the filesystem of ``mem-path`` mounted with DAX
|
4.15) and the filesystem of ``mem-path`` mounted with DAX
|
||||||
option.
|
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``
|
``-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
|
Creates a memory backend object, which can be used to back the
|
||||||
guest RAM. Memory backend objects offer more control than the
|
guest RAM. Memory backend objects offer more control than the
|
||||||
|
|
Loading…
Reference in New Issue