Skip to contentSkip to conversation

vl: complete memory size from memdev before creating memmap

PATCH In review Kohei Enju via 1 participant 2 messages Nov 13 – Nov 30

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vl: complete memory size from memdev before creating memmap

When -machine memory-backend=ram0 is used and '-m' option is omitted with a KVM virt machine as follows:

  qemu-system-aarch64 \
    -enable-kvm \
    -machine virt,memory-backend=ram0 \
    -object memory-backend-memfd,id=ram0,size=256G,share=on \
    ...

virt_set_memmap() creates memmap based on the default memory size ignoring the specified size property of the memory-backend-* object. This is because at this stage memory.size is not initialized by the size property specified by the memory-backend-* object.

Since the memory size is not taken into account, when the memory size is larger than about 255 GiB, this unexpectedly hides PCI ECAM space with system RAM. Consequently guests could fail to find PCI devices, failing to boot when they are dependent on those PCI devices.

When '-m' option is omitted, fix this by getting the size of the backend identified by ram_memdev_id, and completing memory.size with this value before creating the memmap. Afterwards, machine_set_mem() decides ram_size and maxram_size based on the completed memory.size.

Fixes d349f92f78d2 ("vl: allow not specifying size in -m when using -M memory-backend")
---
 system/vl.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
system/vl.c +43 -0
index e7b759d54428..d2670b85f23b 100644
@@ -34,6 +34,7 @@
 #include "hw/core/qdev-properties.h"
 #include "qapi/compat-policy.h"
 #include "qapi/error.h"
+#include "qapi/qobject-output-visitor.h"
 #include "qobject/qdict.h"
 #include "qobject/qstring.h"
 #include "qobject/qjson.h"
@@ -1810,6 +1811,47 @@ static void qemu_apply_legacy_machine_options(QDict *qdict)
     }
 }
 
+static void qemu_complete_machine_memdev_size(void)
+{
+    QDict *backend_prop, *memory_prop;
+    g_autofree char *size_str = NULL;
+    QObject *qobj = NULL;
+    ObjectOption *opt;
+    uint64_t size;
+    Visitor *v;
+
+    if (have_custom_ram_size || !ram_memdev_id) {
+        return;
+    }
+
+    QTAILQ_FOREACH(opt, &object_opts, next) {
+        ObjectOptions *options = opt->opts;
+        const char *type = ObjectType_str(options->qom_type);
+
+        if (g_str_equal(options->id, ram_memdev_id) &&
+            g_str_has_prefix(type, "memory-backend-")) {
+            v = qobject_output_visitor_new(&qobj);
+            visit_type_ObjectOptions(v, NULL, &options, &error_abort);
+            visit_complete(v, &qobj);
+            visit_free(v);
+
+            backend_prop = qobject_to(QDict, qobj);
+            size = qdict_get_uint(backend_prop, "size");
+            size_str = g_strdup_printf("%" PRIu64, size);
+
+            memory_prop = qdict_get_qdict(machine_opts_dict, "memory");
+            assert(memory_prop);
+            qdict_put_str(memory_prop, "size", size_str);
+
+            qobject_unref(qobj);
+            return;
+        }
+    }
+
+    error_report("Memory backend '%s' not found", ram_memdev_id);
+    exit(EXIT_FAILURE);
+}
+
 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
 {
     ObjectOption *opt, *next;
@@ -3821,6 +3863,7 @@ void qemu_init(int argc, char **argv)
     qemu_create_early_backends();
 
     qemu_apply_legacy_machine_options(machine_opts_dict);
+    qemu_complete_machine_memdev_size();
     qemu_apply_machine_options(machine_opts_dict);
     qobject_unref(machine_opts_dict);
     phase_advance(PHASE_MACHINE_CREATED);

---
base-commit: 29760efaf96bbe5928f8111881eddbf5ac06439e
change-id: 20261113-fix-complete-memdev-size-4dc0c807ad31

Best regards,

Signature · 1 line
Kohei Enju <[email protected]>
#1 lore raw

Gentle ping.
Any comments on this patch would be appreciated.

Thanks,
Kohei

Quoted 108 lines · Kohei Enju via

When -machine memory-backend=ram0 is used and '-m' option is omitted with a KVM virt machine as follows:

  qemu-system-aarch64 \
    -enable-kvm \
    -machine virt,memory-backend=ram0 \
    -object memory-backend-memfd,id=ram0,size=256G,share=on \
    ...

virt_set_memmap() creates memmap based on the default memory size ignoring the specified size property of the memory-backend-* object. This is because at this stage memory.size is not initialized by the size property specified by the memory-backend-* object.

Since the memory size is not taken into account, when the memory size is larger than about 255 GiB, this unexpectedly hides PCI ECAM space with system RAM. Consequently guests could fail to find PCI devices, failing to boot when they are dependent on those PCI devices.

When '-m' option is omitted, fix this by getting the size of the backend identified by ram_memdev_id, and completing memory.size with this value before creating the memmap. Afterwards, machine_set_mem() decides ram_size and maxram_size based on the completed memory.size.

Fixes d349f92f78d2 ("vl: allow not specifying size in -m when using -M memory-backend")
---
 system/vl.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
system/vl.c +43 -1
index e7b759d54428..d2670b85f23b 100644
@@ -34,6 +34,7 @@
 #include "hw/core/qdev-properties.h"
 #include "qapi/compat-policy.h"
 #include "qapi/error.h"
+#include "qapi/qobject-output-visitor.h"
 #include "qobject/qdict.h"
 #include "qobject/qstring.h"
 #include "qobject/qjson.h"
@@ -1810,6 +1811,47 @@ static void qemu_apply_legacy_machine_options(QDict *qdict)
     }
 }
 
+static void qemu_complete_machine_memdev_size(void)
+{
+    QDict *backend_prop, *memory_prop;
+    g_autofree char *size_str = NULL;
+    QObject *qobj = NULL;
+    ObjectOption *opt;
+    uint64_t size;
+    Visitor *v;
+
+    if (have_custom_ram_size || !ram_memdev_id) {
+        return;
+    }
+
+    QTAILQ_FOREACH(opt, &object_opts, next) {
+        ObjectOptions *options = opt->opts;
+        const char *type = ObjectType_str(options->qom_type);
+
+        if (g_str_equal(options->id, ram_memdev_id) &&
+            g_str_has_prefix(type, "memory-backend-")) {
+            v = qobject_output_visitor_new(&qobj);
+            visit_type_ObjectOptions(v, NULL, &options, &error_abort);
+            visit_complete(v, &qobj);
+            visit_free(v);
+
+            backend_prop = qobject_to(QDict, qobj);
+            size = qdict_get_uint(backend_prop, "size");
+            size_str = g_strdup_printf("%" PRIu64, size);
+
+            memory_prop = qdict_get_qdict(machine_opts_dict, "memory");
+            assert(memory_prop);
+            qdict_put_str(memory_prop, "size", size_str);
+
+            qobject_unref(qobj);
+            return;
+        }
+    }
+
+    error_report("Memory backend '%s' not found", ram_memdev_id);
+    exit(EXIT_FAILURE);
+}
+
 static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
 {
     ObjectOption *opt, *next;
@@ -3821,6 +3863,7 @@ void qemu_init(int argc, char **argv)
     qemu_create_early_backends();
 
     qemu_apply_legacy_machine_options(machine_opts_dict);
+    qemu_complete_machine_memdev_size();
     qemu_apply_machine_options(machine_opts_dict);
     qobject_unref(machine_opts_dict);
     phase_advance(PHASE_MACHINE_CREATED);
 
---

base-commit: 29760efaf96bbe5928f8111881eddbf5ac06439e
change-id: 20261113-fix-complete-memdev-size-4dc0c807ad31

Best regards,

Signature · 1 line
Kohei Enju <[email protected]>