From: Haozhong Zhang <haozhong.zhang@intel.com>
To: qemu-devel@nongnu.org, xen-devel@lists.xen.org
Cc: Haozhong Zhang <haozhong.zhang@intel.com>,
Eduardo Habkost <ehabkost@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
Igor Mammedov <imammedo@redhat.com>,
Konrad Rzeszutek Wilk <konrad@darnok.org>,
Dan Williams <dan.j.williams@intel.com>
Subject: [RFC QEMU PATCH v2 08/10] hostmem: add a host memory backend for Xen
Date: Mon, 20 Mar 2017 08:12:47 +0800 [thread overview]
Message-ID: <20170320001249.25521-9-haozhong.zhang@intel.com> (raw)
In-Reply-To: <20170320001249.25521-1-haozhong.zhang@intel.com>
Some virtual devices (e.g. NVDIMM) use the host memory backend to map
its backend resources to the guest. When those devices are used on Xen,
the mapping has to be managed out of QEMU. In order to reuse other parts
of the implementation of those devices, we introduce a host memory
backend for Xen (memory-backend-xen) which is mostly a placeholder
that only creates an empty memory region and does not actually
allocate and map the resource.
Following is an example of a vNVDIMM device backed by a host pmem
namespace,
-object memory-backend-xen,id=mem1,mem-path=/dev/pmem0,size=4G
-device nvdimm,id=nvdimm1,memdev=mem1
Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
---
Cc: Eduardo Habkost <ehabkost@redhat.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>
---
backends/Makefile.objs | 1 +
backends/hostmem-xen.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++
backends/hostmem.c | 8 ++++
hw/mem/pc-dimm.c | 5 +-
4 files changed, 134 insertions(+), 1 deletion(-)
create mode 100644 backends/hostmem-xen.c
diff --git a/backends/Makefile.objs b/backends/Makefile.objs
index 0e0f1567b2..11dcaa4d0e 100644
--- a/backends/Makefile.objs
+++ b/backends/Makefile.objs
@@ -9,6 +9,7 @@ common-obj-$(CONFIG_TPM) += tpm.o
common-obj-y += hostmem.o hostmem-ram.o
common-obj-$(CONFIG_LINUX) += hostmem-file.o
+common-obj-${CONFIG_XEN_BACKEND} += hostmem-xen.o
common-obj-y += cryptodev.o
common-obj-y += cryptodev-builtin.o
diff --git a/backends/hostmem-xen.c b/backends/hostmem-xen.c
new file mode 100644
index 0000000000..b1b446766d
--- /dev/null
+++ b/backends/hostmem-xen.c
@@ -0,0 +1,121 @@
+/*
+ * QEMU Host Memory Backend for Xen
+ *
+ * Copyright(C) 2017 Intel Corporation.
+ *
+ * Author:
+ * Haozhong Zhang <haozhong.zhang@intel.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/mmap-alloc.h"
+#include "sysemu/hostmem.h"
+#include "qapi/error.h"
+#include "qom/object_interfaces.h"
+
+
+#define TYPE_MEMORY_BACKEND_XEN "memory-backend-xen"
+
+#define MEMORY_BACKEND_XEN(obj) \
+ OBJECT_CHECK(HostMemoryBackendXen, (obj), TYPE_MEMORY_BACKEND_XEN)
+
+typedef struct HostMemoryBackendXen HostMemoryBackendXen;
+
+struct HostMemoryBackendXen {
+ HostMemoryBackend parent_obj;
+
+ char *mem_path;
+};
+
+static void xen_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
+{
+ HostMemoryBackendXen *db = MEMORY_BACKEND_XEN(backend);
+ int fd;
+ size_t page_size;
+
+ if (!backend->size) {
+ error_setg(errp, "can't create backend with size 0");
+ return;
+ }
+ memory_region_init(&backend->mr,
+ OBJECT(backend), db->mem_path, backend->size);
+
+ fd = open(db->mem_path, O_RDONLY);
+ if (!fd) {
+ error_setg(errp, "can't open file %s, err %d", db->mem_path, errno);
+ return;
+ }
+ page_size = qemu_fd_getpagesize(fd);
+ backend->mr.align = MAX(page_size, QEMU_VMALLOC_ALIGN);
+ close(fd);
+}
+
+static void xen_backend_class_init(ObjectClass *oc, void *data)
+{
+ HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
+
+ bc->alloc = xen_backend_memory_alloc;
+}
+
+static char *get_mem_path(Object *o, Error **errp)
+{
+ HostMemoryBackendXen *db = MEMORY_BACKEND_XEN(o);
+
+ return g_strdup(db->mem_path);
+}
+
+static void set_mem_path(Object *o, const char *str, Error **errp)
+{
+ HostMemoryBackend *backend = MEMORY_BACKEND(o);
+ HostMemoryBackendXen *db = MEMORY_BACKEND_XEN(o);
+
+ if (memory_region_size(&backend->mr)) {
+ error_setg(errp, "cannot change property value");
+ return;
+ }
+ g_free(db->mem_path);
+ db->mem_path = g_strdup(str);
+}
+
+static void
+xen_backend_instance_init(Object *o)
+{
+ object_property_add_str(o, "mem-path", get_mem_path,
+ set_mem_path, NULL);
+}
+
+static void xen_backend_instance_finalize(Object *o)
+{
+ HostMemoryBackendXen *db = MEMORY_BACKEND_XEN(o);
+
+ g_free(db->mem_path);
+}
+
+static const TypeInfo xen_backend_info = {
+ .name = TYPE_MEMORY_BACKEND_XEN,
+ .parent = TYPE_MEMORY_BACKEND,
+ .class_init = xen_backend_class_init,
+ .instance_init = xen_backend_instance_init,
+ .instance_finalize = xen_backend_instance_finalize,
+ .instance_size = sizeof(HostMemoryBackendXen),
+};
+
+static void register_types(void)
+{
+ type_register_static(&xen_backend_info);
+}
+
+type_init(register_types);
diff --git a/backends/hostmem.c b/backends/hostmem.c
index 7f5de70609..ee5e59c133 100644
--- a/backends/hostmem.c
+++ b/backends/hostmem.c
@@ -18,6 +18,7 @@
#include "qapi-visit.h"
#include "qemu/config-file.h"
#include "qom/object_interfaces.h"
+#include "hw/xen/xen.h"
#ifdef CONFIG_NUMA
#include <numaif.h>
@@ -274,6 +275,13 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
goto out;
}
+ /* The backend storage of MEMORY_BACKEND_XEN is managed by Xen,
+ * so no further work in this function is needed.
+ */
+ if (xen_enabled() && !backend->mr.ram_block) {
+ goto out;
+ }
+
ptr = memory_region_get_ram_ptr(&backend->mr);
sz = memory_region_size(&backend->mr);
diff --git a/hw/mem/pc-dimm.c b/hw/mem/pc-dimm.c
index 9e8dab0e89..69c5784252 100644
--- a/hw/mem/pc-dimm.c
+++ b/hw/mem/pc-dimm.c
@@ -28,6 +28,7 @@
#include "sysemu/kvm.h"
#include "trace.h"
#include "hw/virtio/vhost.h"
+#include "hw/xen/xen.h"
typedef struct pc_dimms_capacity {
uint64_t size;
@@ -107,7 +108,9 @@ void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplugState *hpms,
}
memory_region_add_subregion(&hpms->mr, addr - hpms->base, mr);
- vmstate_register_ram(vmstate_mr, dev);
+ if (!xen_enabled()) {
+ vmstate_register_ram(vmstate_mr, dev);
+ }
numa_set_mem_node_id(addr, memory_region_size(mr), dimm->node);
out:
--
2.12.0
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel
next prev parent reply other threads:[~2017-03-20 0:12 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-20 0:12 [RFC QEMU PATCH v2 00/10] Implement vNVDIMM for Xen HVM guest Haozhong Zhang
2017-03-20 0:12 ` [RFC QEMU PATCH v2 01/10] nvdimm xen: disable label support on Xen Haozhong Zhang
2017-04-01 12:25 ` Konrad Rzeszutek Wilk
2017-03-20 0:12 ` [RFC QEMU PATCH v2 02/10] xen-hvm: initialize DM ACPI Haozhong Zhang
2017-03-20 0:12 ` [RFC QEMU PATCH v2 03/10] xen-hvm: support copying ACPI to guest memory Haozhong Zhang
2017-03-20 0:12 ` [RFC QEMU PATCH v2 04/10] nvdimm acpi: do not use fw_cfg on Xen Haozhong Zhang
2017-03-20 0:12 ` [RFC QEMU PATCH v2 05/10] nvdimm acpi: copy NFIT to Xen guest Haozhong Zhang
2017-03-20 0:12 ` [RFC QEMU PATCH v2 06/10] nvdimm acpi: build and copy NVDIMM namespace devices to guest on Xen Haozhong Zhang
2017-03-20 0:12 ` [RFC QEMU PATCH v2 07/10] xen-hvm: initiate building DM ACPI on i386 machine Haozhong Zhang
2017-03-20 0:12 ` Haozhong Zhang [this message]
2017-03-20 0:12 ` [RFC QEMU PATCH v2 09/10] xen-hvm: create hotplug memory region on Xen Haozhong Zhang
2017-03-20 0:12 ` [RFC QEMU PATCH v2 10/10] qapi: extend 'query-memory-devices' to list devices of specified type Haozhong Zhang
2017-04-11 8:56 ` [Qemu-devel] " Markus Armbruster
2017-03-20 0:26 ` [Qemu-devel] [RFC QEMU PATCH v2 00/10] Implement vNVDIMM for Xen HVM guest no-reply
2017-03-28 13:18 ` no-reply
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=20170320001249.25521-9-haozhong.zhang@intel.com \
--to=haozhong.zhang@intel.com \
--cc=dan.j.williams@intel.com \
--cc=ehabkost@redhat.com \
--cc=imammedo@redhat.com \
--cc=konrad@darnok.org \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=xen-devel@lists.xen.org \
/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).