mirror of
https://github.com/qemu/qemu.git
synced 2024-11-25 20:03:37 +08:00
98bfaac788
-----BEGIN PGP SIGNATURE----- iQIcBAABAgAGBQJZrTlXAAoJEDhwtADrkYZT570P/20EQATHuRMGcO9QIParHqZL dh3yyBMEoSPNJx/dK2M1psT9vQgfcQ9+W6ouzW8nmu70bmY+NpwwlJfr9DadXE/q C9ht8ULL6pNfJ4bfLB8SqWqG6WUuyZJEWc3x3dCVuU4921hprwyAkWFEXiw73ThN Gay4zRooxZSR0pHsuh5wrlUl7iF7UHy/UO1yRvIUWvYMQ4PLyMzF/+o2hP4MvPNe l/b5B2XYiioMlJOjmi2/kRABOUywt4YWSXTxRi7f9VxWmKa5B8rbeMmxaX5zH3zB b4Ik8UdYAvW47iKilGQa1JlLqK24tgnA6monzMfQwhCdUtBTHu3rf8jJrvRai1fl R8vitLy1JJ93GPzVUAknSNOdkNBD5QnEvqn/ZGxvKpEQIHaPURoyOD50i9/NFR0n 6d8WSv2403zZsE6c+JkmeOm0zRhoE3Bkhcg8G9QYhGJu8meCsOVmR+afoBtzrOB9 ziB7dpWyAMwCioY25vHVD2vtMT/YlZX7Ic+vuWKtagFLt6lHROnSraon/iLdXYj3 Dmeb+sdBXL8vUXBK1c5P+ifKqgF6asz918wJgc5aucPWY3xY6lEN9wCGXqJuVw5Q B3FSIMCIAJhW8KpzmHObRJU1Enj2StkWBymWWNIu5f323v1u7hK57TXYw9HqDETj cL599N7VasD5azXLjlxk =eUZW -----END PGP SIGNATURE----- Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2017-09-01-v3' into staging QAPI patches for 2017-09-01 # gpg: Signature made Mon 04 Sep 2017 12:30:31 BST # gpg: using RSA key 0x3870B400EB918653 # gpg: Good signature from "Markus Armbruster <armbru@redhat.com>" # gpg: aka "Markus Armbruster <armbru@pond.sub.org>" # Primary key fingerprint: 354B C8B3 D7EB 2A6B 6867 4E5F 3870 B400 EB91 8653 * remotes/armbru/tags/pull-qapi-2017-09-01-v3: (47 commits) qapi: drop the sentinel in enum array qapi: Change data type of the FOO_lookup generated for enum FOO qapi: Convert indirect uses of FOO_lookup[...] to qapi_enum_lookup() qapi: Mechanically convert FOO_lookup[...] to FOO_str(...) qapi: Generate FOO_str() macro for QAPI enum FOO qapi: Avoid unnecessary use of enum lookup table's sentinel qapi: Use qapi_enum_parse() in input_type_enum() crypto: Use qapi_enum_parse() in qcrypto_block_luks_name_lookup() quorum: Use qapi_enum_parse() in quorum_open() block: Use qemu_enum_parse() in blkdebug_debug_breakpoint() hmp: Use qapi_enum_parse() in hmp_migrate_set_parameter() hmp: Use qapi_enum_parse() in hmp_migrate_set_capability() tpm: Clean up model registration & lookup tpm: Clean up driver registration & lookup qapi: Drop superfluous qapi_enum_parse() parameter max qapi: Update qapi-code-gen.txt examples to match current code qapi-schema: Improve section headings qapi-schema: Move queries from common.json to qapi-schema.json qapi-schema: Make block-core.json self-contained qapi-schema: Fold event.json back into qapi-schema.json ... Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
431 lines
12 KiB
C
431 lines
12 KiB
C
/*
|
|
* QEMU Host Memory Backend
|
|
*
|
|
* Copyright (C) 2013-2014 Red Hat Inc
|
|
*
|
|
* Authors:
|
|
* Igor Mammedov <imammedo@redhat.com>
|
|
*
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
* See the COPYING file in the top-level directory.
|
|
*/
|
|
#include "qemu/osdep.h"
|
|
#include "sysemu/hostmem.h"
|
|
#include "hw/boards.h"
|
|
#include "qapi/error.h"
|
|
#include "qapi/visitor.h"
|
|
#include "qapi-types.h"
|
|
#include "qapi-visit.h"
|
|
#include "qemu/config-file.h"
|
|
#include "qom/object_interfaces.h"
|
|
|
|
#ifdef CONFIG_NUMA
|
|
#include <numaif.h>
|
|
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT != MPOL_DEFAULT);
|
|
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED != MPOL_PREFERRED);
|
|
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND != MPOL_BIND);
|
|
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE != MPOL_INTERLEAVE);
|
|
#endif
|
|
|
|
static void
|
|
host_memory_backend_get_size(Object *obj, Visitor *v, const char *name,
|
|
void *opaque, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
uint64_t value = backend->size;
|
|
|
|
visit_type_size(v, name, &value, errp);
|
|
}
|
|
|
|
static void
|
|
host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
|
|
void *opaque, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
Error *local_err = NULL;
|
|
uint64_t value;
|
|
|
|
if (host_memory_backend_mr_inited(backend)) {
|
|
error_setg(&local_err, "cannot change property value");
|
|
goto out;
|
|
}
|
|
|
|
visit_type_size(v, name, &value, &local_err);
|
|
if (local_err) {
|
|
goto out;
|
|
}
|
|
if (!value) {
|
|
error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
|
|
PRIu64 "'", object_get_typename(obj), name, value);
|
|
goto out;
|
|
}
|
|
backend->size = value;
|
|
out:
|
|
error_propagate(errp, local_err);
|
|
}
|
|
|
|
static void
|
|
host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name,
|
|
void *opaque, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
uint16List *host_nodes = NULL;
|
|
uint16List **node = &host_nodes;
|
|
unsigned long value;
|
|
|
|
value = find_first_bit(backend->host_nodes, MAX_NODES);
|
|
if (value == MAX_NODES) {
|
|
return;
|
|
}
|
|
|
|
*node = g_malloc0(sizeof(**node));
|
|
(*node)->value = value;
|
|
node = &(*node)->next;
|
|
|
|
do {
|
|
value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
|
|
if (value == MAX_NODES) {
|
|
break;
|
|
}
|
|
|
|
*node = g_malloc0(sizeof(**node));
|
|
(*node)->value = value;
|
|
node = &(*node)->next;
|
|
} while (true);
|
|
|
|
visit_type_uint16List(v, name, &host_nodes, errp);
|
|
}
|
|
|
|
static void
|
|
host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
|
|
void *opaque, Error **errp)
|
|
{
|
|
#ifdef CONFIG_NUMA
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
uint16List *l = NULL;
|
|
|
|
visit_type_uint16List(v, name, &l, errp);
|
|
|
|
while (l) {
|
|
bitmap_set(backend->host_nodes, l->value, 1);
|
|
l = l->next;
|
|
}
|
|
#else
|
|
error_setg(errp, "NUMA node binding are not supported by this QEMU");
|
|
#endif
|
|
}
|
|
|
|
static int
|
|
host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
return backend->policy;
|
|
}
|
|
|
|
static void
|
|
host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
backend->policy = policy;
|
|
|
|
#ifndef CONFIG_NUMA
|
|
if (policy != HOST_MEM_POLICY_DEFAULT) {
|
|
error_setg(errp, "NUMA policies are not supported by this QEMU");
|
|
}
|
|
#endif
|
|
}
|
|
|
|
static bool host_memory_backend_get_merge(Object *obj, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
|
|
return backend->merge;
|
|
}
|
|
|
|
static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
|
|
if (!host_memory_backend_mr_inited(backend)) {
|
|
backend->merge = value;
|
|
return;
|
|
}
|
|
|
|
if (value != backend->merge) {
|
|
void *ptr = memory_region_get_ram_ptr(&backend->mr);
|
|
uint64_t sz = memory_region_size(&backend->mr);
|
|
|
|
qemu_madvise(ptr, sz,
|
|
value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE);
|
|
backend->merge = value;
|
|
}
|
|
}
|
|
|
|
static bool host_memory_backend_get_dump(Object *obj, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
|
|
return backend->dump;
|
|
}
|
|
|
|
static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
|
|
if (!host_memory_backend_mr_inited(backend)) {
|
|
backend->dump = value;
|
|
return;
|
|
}
|
|
|
|
if (value != backend->dump) {
|
|
void *ptr = memory_region_get_ram_ptr(&backend->mr);
|
|
uint64_t sz = memory_region_size(&backend->mr);
|
|
|
|
qemu_madvise(ptr, sz,
|
|
value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP);
|
|
backend->dump = value;
|
|
}
|
|
}
|
|
|
|
static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
|
|
return backend->prealloc || backend->force_prealloc;
|
|
}
|
|
|
|
static void host_memory_backend_set_prealloc(Object *obj, bool value,
|
|
Error **errp)
|
|
{
|
|
Error *local_err = NULL;
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
|
|
if (backend->force_prealloc) {
|
|
if (value) {
|
|
error_setg(errp,
|
|
"remove -mem-prealloc to use the prealloc property");
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!host_memory_backend_mr_inited(backend)) {
|
|
backend->prealloc = value;
|
|
return;
|
|
}
|
|
|
|
if (value && !backend->prealloc) {
|
|
int fd = memory_region_get_fd(&backend->mr);
|
|
void *ptr = memory_region_get_ram_ptr(&backend->mr);
|
|
uint64_t sz = memory_region_size(&backend->mr);
|
|
|
|
os_mem_prealloc(fd, ptr, sz, smp_cpus, &local_err);
|
|
if (local_err) {
|
|
error_propagate(errp, local_err);
|
|
return;
|
|
}
|
|
backend->prealloc = true;
|
|
}
|
|
}
|
|
|
|
static void host_memory_backend_init(Object *obj)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
MachineState *machine = MACHINE(qdev_get_machine());
|
|
|
|
backend->merge = machine_mem_merge(machine);
|
|
backend->dump = machine_dump_guest_core(machine);
|
|
backend->prealloc = mem_prealloc;
|
|
}
|
|
|
|
bool host_memory_backend_mr_inited(HostMemoryBackend *backend)
|
|
{
|
|
/*
|
|
* NOTE: We forbid zero-length memory backend, so here zero means
|
|
* "we haven't inited the backend memory region yet".
|
|
*/
|
|
return memory_region_size(&backend->mr) != 0;
|
|
}
|
|
|
|
MemoryRegion *
|
|
host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
|
|
{
|
|
return host_memory_backend_mr_inited(backend) ? &backend->mr : NULL;
|
|
}
|
|
|
|
void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped)
|
|
{
|
|
backend->is_mapped = mapped;
|
|
}
|
|
|
|
bool host_memory_backend_is_mapped(HostMemoryBackend *backend)
|
|
{
|
|
return backend->is_mapped;
|
|
}
|
|
|
|
static void
|
|
host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(uc);
|
|
HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
|
|
Error *local_err = NULL;
|
|
void *ptr;
|
|
uint64_t sz;
|
|
|
|
if (bc->alloc) {
|
|
bc->alloc(backend, &local_err);
|
|
if (local_err) {
|
|
goto out;
|
|
}
|
|
|
|
ptr = memory_region_get_ram_ptr(&backend->mr);
|
|
sz = memory_region_size(&backend->mr);
|
|
|
|
if (backend->merge) {
|
|
qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
|
|
}
|
|
if (!backend->dump) {
|
|
qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
|
|
}
|
|
#ifdef CONFIG_NUMA
|
|
unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
|
|
/* lastbit == MAX_NODES means maxnode = 0 */
|
|
unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
|
|
/* ensure policy won't be ignored in case memory is preallocated
|
|
* before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
|
|
* this doesn't catch hugepage case. */
|
|
unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
|
|
|
|
/* check for invalid host-nodes and policies and give more verbose
|
|
* error messages than mbind(). */
|
|
if (maxnode && backend->policy == MPOL_DEFAULT) {
|
|
error_setg(errp, "host-nodes must be empty for policy default,"
|
|
" or you should explicitly specify a policy other"
|
|
" than default");
|
|
return;
|
|
} else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
|
|
error_setg(errp, "host-nodes must be set for policy %s",
|
|
HostMemPolicy_str(backend->policy));
|
|
return;
|
|
}
|
|
|
|
/* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
|
|
* as argument to mbind() due to an old Linux bug (feature?) which
|
|
* cuts off the last specified node. This means backend->host_nodes
|
|
* must have MAX_NODES+1 bits available.
|
|
*/
|
|
assert(sizeof(backend->host_nodes) >=
|
|
BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
|
|
assert(maxnode <= MAX_NODES);
|
|
if (mbind(ptr, sz, backend->policy,
|
|
maxnode ? backend->host_nodes : NULL, maxnode + 1, flags)) {
|
|
if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
|
|
error_setg_errno(errp, errno,
|
|
"cannot bind memory to host NUMA nodes");
|
|
return;
|
|
}
|
|
}
|
|
#endif
|
|
/* Preallocate memory after the NUMA policy has been instantiated.
|
|
* This is necessary to guarantee memory is allocated with
|
|
* specified NUMA policy in place.
|
|
*/
|
|
if (backend->prealloc) {
|
|
os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
|
|
smp_cpus, &local_err);
|
|
if (local_err) {
|
|
goto out;
|
|
}
|
|
}
|
|
}
|
|
out:
|
|
error_propagate(errp, local_err);
|
|
}
|
|
|
|
static bool
|
|
host_memory_backend_can_be_deleted(UserCreatable *uc)
|
|
{
|
|
if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc))) {
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
static char *get_id(Object *o, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(o);
|
|
|
|
return g_strdup(backend->id);
|
|
}
|
|
|
|
static void set_id(Object *o, const char *str, Error **errp)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(o);
|
|
|
|
if (backend->id) {
|
|
error_setg(errp, "cannot change property value");
|
|
return;
|
|
}
|
|
backend->id = g_strdup(str);
|
|
}
|
|
|
|
static void
|
|
host_memory_backend_class_init(ObjectClass *oc, void *data)
|
|
{
|
|
UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
|
|
|
|
ucc->complete = host_memory_backend_memory_complete;
|
|
ucc->can_be_deleted = host_memory_backend_can_be_deleted;
|
|
|
|
object_class_property_add_bool(oc, "merge",
|
|
host_memory_backend_get_merge,
|
|
host_memory_backend_set_merge, &error_abort);
|
|
object_class_property_add_bool(oc, "dump",
|
|
host_memory_backend_get_dump,
|
|
host_memory_backend_set_dump, &error_abort);
|
|
object_class_property_add_bool(oc, "prealloc",
|
|
host_memory_backend_get_prealloc,
|
|
host_memory_backend_set_prealloc, &error_abort);
|
|
object_class_property_add(oc, "size", "int",
|
|
host_memory_backend_get_size,
|
|
host_memory_backend_set_size,
|
|
NULL, NULL, &error_abort);
|
|
object_class_property_add(oc, "host-nodes", "int",
|
|
host_memory_backend_get_host_nodes,
|
|
host_memory_backend_set_host_nodes,
|
|
NULL, NULL, &error_abort);
|
|
object_class_property_add_enum(oc, "policy", "HostMemPolicy",
|
|
&HostMemPolicy_lookup,
|
|
host_memory_backend_get_policy,
|
|
host_memory_backend_set_policy, &error_abort);
|
|
object_class_property_add_str(oc, "id", get_id, set_id, &error_abort);
|
|
}
|
|
|
|
static void host_memory_backend_finalize(Object *o)
|
|
{
|
|
HostMemoryBackend *backend = MEMORY_BACKEND(o);
|
|
g_free(backend->id);
|
|
}
|
|
|
|
static const TypeInfo host_memory_backend_info = {
|
|
.name = TYPE_MEMORY_BACKEND,
|
|
.parent = TYPE_OBJECT,
|
|
.abstract = true,
|
|
.class_size = sizeof(HostMemoryBackendClass),
|
|
.class_init = host_memory_backend_class_init,
|
|
.instance_size = sizeof(HostMemoryBackend),
|
|
.instance_init = host_memory_backend_init,
|
|
.instance_finalize = host_memory_backend_finalize,
|
|
.interfaces = (InterfaceInfo[]) {
|
|
{ TYPE_USER_CREATABLE },
|
|
{ }
|
|
}
|
|
};
|
|
|
|
static void register_types(void)
|
|
{
|
|
type_register_static(&host_memory_backend_info);
|
|
}
|
|
|
|
type_init(register_types);
|