Skip to contentSkip to conversation

Register accel-$cpu_type for all targets

PATCH Superseded Yonggang Luo 2 participants 6 messages Oct 27 – Oct 28

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Register accel-$cpu_type for all targets

In multi-target binary, the target list is more than one, so we need register them all by iterating target list for system-emu. The user-emu still register the accel-$cpu_Type in the old way as it's stil contains only a single target, and it do not have the target list in QOM way.

This is an alternative way to

doesn't means this is a blocker of them, they can co-exist.

Yonggang Luo (2):
target-info: cache the QOM target list before MODULE_INIT_QOM
accel: register CPU parent for each target

 accel/accel-common.c           |  6 ++----
 accel/accel-internal.h         |  1 +
 accel/accel-system.c           | 24 +++++++++++++++++++++++
 accel/accel-user.c             |  7 +++++++
 include/qemu/target-info-qom.h |  4 ++++
 system/vl.c                    |  2 +-
 target-info-qom.c              | 26 ++++++++++++++++++++++++-
 tests/qtest/fuzz/fuzz.c        |  2 +-
 tests/unit/test-multi-target.c | 35 +++++++++++++++++++++++++++-------
 9 files changed, 93 insertions(+), 14 deletions(-)
Signature · 1 line
2.55.0.windows.3
#1 lore raw

PATCH 1/2 target-info: cache the QOM target list before MODULE_INIT_QOM

We can not retrieve target list when do call to MODULE_INIT_QOM
In latter commit, we need iterate target list inside type_init referenced functions for generate all QOM variants depends on target list(without select the target). So we have to prepare the QOM target list before MODULE_INIT_QOM

---
 include/qemu/target-info-qom.h |  4 ++++
 system/vl.c                    |  2 +-
 target-info-qom.c              | 26 +++++++++++++++++++++++++-
 tests/qtest/fuzz/fuzz.c        |  2 +-
 tests/unit/test-multi-target.c | 20 +++++++++++++-------
 5 files changed, 44 insertions(+), 10 deletions(-)
include/qemu/target-info-qom.h +4 -0
index ba2e4370afb..031d9ce32a5 100644
@@ -25,6 +25,10 @@ typedef struct TargetInfoQomClass {
 
 OBJECT_DECLARE_TYPE(TargetInfoQom, TargetInfoQomClass, TARGET_INFO)
 
+/* Initialize before MODULE_INIT_QOM; cache is owned by this module. */
+void target_info_qom_init(void);
+GSList *target_info_qom_get_targets(void);
+
 void target_info_qom_set_target(void);
 void target_info_qom_set_target_info(const TargetInfo *ti);
 
system/vl.c +1 -1
index 468a9fc247a..47d5f09d2d7 100644
@@ -2914,7 +2914,7 @@ void qemu_init(int argc, char **argv)
 
     os_setup_limits();
 
-    module_call_init(MODULE_INIT_TARGET_INFO);
+    target_info_qom_init();
     target_info_qom_set_target();
 
     module_init_info(qemu_modinfo);
target-info-qom.c +25 -1
index aeeedb7e818..8d3f2b7280b 100644
@@ -8,6 +8,7 @@
 
 #include "qemu/osdep.h"
 #include "qapi/error.h"
+#include "qemu/module.h"
 #include "qom/object.h"
 #include "qemu/target-info-def.h"
 #include "qemu/target-info-init.h"
@@ -34,6 +35,29 @@ static const TypeInfo target_info_parent_type = {
 DEFINE_TARGET_INFO_TYPE(target_info_parent_type)
 
 static const TargetInfo *target_info_ptr;
+static GSList *target_info_classes;
+static bool target_info_initialized;
+
+void target_info_qom_init(void)
+{
+    if (target_info_initialized) {
+        return;
+    }
+
+    module_call_init(MODULE_INIT_TARGET_INFO);
+    /*
+     * Avoid enumerating target-info classes during QOM registration,
+     * which can leave unrelated types incomplete.
+     */
+    target_info_classes = object_class_get_list(TYPE_TARGET_INFO, false);
+    target_info_initialized = true;
+}
+
+GSList *target_info_qom_get_targets(void)
+{
+    assert(target_info_initialized);
+    return target_info_classes;
+}
 
 const TargetInfo *target_info(void)
 {
@@ -42,7 +66,7 @@ const TargetInfo *target_info(void)
 
 void target_info_qom_set_target(void)
 {
-    g_autoptr(GSList) targets = object_class_get_list(TYPE_TARGET_INFO, false);
+    GSList *targets = target_info_qom_get_targets();
 
     size_t num_found = g_slist_length(targets);
     if (num_found != 1) {
tests/qtest/fuzz/fuzz.c +1 -1
index a3a131c80f8..570fc0b45b2 100644
@@ -173,7 +173,7 @@ int LLVMFuzzerInitialize(int *argc, char ***argv, char ***envp)
 
     /* Initialize qgraph and modules */
     qos_graph_init();
-    module_call_init(MODULE_INIT_TARGET_INFO);
+    target_info_qom_init();
     target_info_qom_set_target();
     module_call_init(MODULE_INIT_FUZZ_TARGET);
     module_call_init(MODULE_INIT_QOM);
tests/unit/test-multi-target.c +13 -7
index 89b55bb1632..a91fe8e6d07 100644
@@ -30,23 +30,29 @@
 /* stub for system/main.c */
 int (*qemu_main)(void);
 
+static GSList *targets;
+
 static void test_qom_types_are_unique(void)
 {
-    module_call_init(MODULE_INIT_TARGET_INFO);
-    g_autoptr(GSList) targets = object_class_get_list(TYPE_TARGET_INFO, false);
     g_assert(targets);
-    const TargetInfo *first_ti = TARGET_INFO_CLASS(targets->data)->target_info;
-    target_info_qom_set_target_info(first_ti);
-
-    /* register all types */
-    module_call_init(MODULE_INIT_QOM);
     /* trigger type_initialize for all types */
     object_class_get_list("", false);
+
+    g_assert_true(targets == target_info_qom_get_targets());
 }
 
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
+    target_info_qom_init();
+    targets = target_info_qom_get_targets();
+
+    const TargetInfo *first_ti = TARGET_INFO_CLASS(targets->data)->target_info;
+    target_info_qom_set_target_info(first_ti);
+
+    /* register all types */
+    module_call_init(MODULE_INIT_QOM);
+
     g_test_add_func("/multi-targets/qom-types-are-unique",
                    test_qom_types_are_unique);
     return g_test_run();
Signature · 1 line
2.55.0.windows.3
#2 lore raw

PATCH 2/2 accel: register CPU parent for each target

In reply to Yonggang Luo

Now we need use the prepared target-list to register different accel-$cpu_type parent QOM type. So that multi-target won't error missing parent for different accel for different arch.

Also add a test for it in test-multi-target.c

---
 accel/accel-common.c           |  6 ++----
 accel/accel-internal.h         |  1 +
 accel/accel-system.c           | 24 ++++++++++++++++++++++++
 accel/accel-user.c             |  7 +++++++
 tests/unit/test-multi-target.c | 15 +++++++++++++++
 5 files changed, 49 insertions(+), 4 deletions(-)
accel/accel-common.c +2 -4
index 00a400243f0..0f4e202d8f8 100644
@@ -130,9 +130,9 @@ static const TypeInfo accel_types[] = {
 
 DEFINE_TYPES(accel_types)
 
-static void register_accel_target_type(void)
+void accel_register_cpu_type(const char *cpu_type)
 {
-    g_autofree char *name = g_strconcat("accel-", target_cpu_type(), NULL);
+    g_autofree char *name = g_strconcat("accel-", cpu_type, NULL);
     const TypeInfo accel_cpu_type = {
         .name = name,
         .parent = TYPE_OBJECT,
@@ -142,5 +142,3 @@ static void register_accel_target_type(void)
 
     type_register_static(&accel_cpu_type);
 }
-
-type_init(register_accel_target_type);
accel/accel-internal.h +1 -0
index d3a4422cbf7..2b6f58526d3 100644
@@ -13,5 +13,6 @@
 #include "qemu/accel.h"
 
 void accel_init_ops_interfaces(AccelClass *ac);
+void accel_register_cpu_type(const char *cpu_type);
 
 #endif /* ACCEL_SYSTEM_H */
accel/accel-system.c +24 -0
index 1325b23864c..fdfa85179ee 100644
@@ -25,6 +25,8 @@
 
 #include "qemu/osdep.h"
 #include "qemu/accel.h"
+#include "qemu/target-info-def.h"
+#include "qemu/target-info-qom.h"
 #include "qom/compat-properties.h"
 #include "qapi/qapi-commands-accelerator.h"
 #include "monitor/monitor.h"
@@ -32,6 +34,7 @@
 #include "hw/core/boards.h"
 #include "hw/core/cpu.h"
 #include "accel/accel-ops.h"
+#include "accel/accel-cpu.h"
 #include "accel/accel-cpu-ops.h"
 #include "system/cpus.h"
 #include "qemu/error-report.h"
@@ -124,6 +127,27 @@ static const TypeInfo accel_ops_type_info = {
 
 static void accel_system_register_types(void)
 {
+    GSList *targets = target_info_qom_get_targets();
+    const TargetInfo *ti, *prev_ti;
+
     type_register_static(&accel_ops_type_info);
+
+    for (GSList *l = targets; l; l = l->next) {
+        bool duplicate = false;
+
+        ti = TARGET_INFO_CLASS(l->data)->target_info;
+        for (GSList *prev = targets; prev != l; prev = prev->next) {
+            prev_ti = TARGET_INFO_CLASS(prev->data)->target_info;
+            if (g_str_equal(prev_ti->cpu_type, ti->cpu_type)) {
+                duplicate = true;
+                break;
+            }
+        }
+        if (duplicate) {
+            continue;
+        }
+
+        accel_register_cpu_type(ti->cpu_type);
+    }
 }
 type_init(accel_system_register_types);
accel/accel-user.c +7 -0
index 7d192306a65..85ede70e5bd 100644
@@ -9,8 +9,15 @@
 
 #include "qemu/osdep.h"
 #include "qemu/accel.h"
+#include "qemu/target-info.h"
 #include "accel-internal.h"
 
+static void register_accel_target_type(void)
+{
+    accel_register_cpu_type(target_cpu_type());
+}
+type_init(register_accel_target_type);
+
 void accel_init_ops_interfaces(AccelClass *ac)
 {
     /* nothing */
tests/unit/test-multi-target.c +15 -0
index a91fe8e6d07..9ace553ca05 100644
@@ -15,6 +15,7 @@
 #include "qemu/osdep.h"
 #include "qemu/module.h"
 #include "qemu/target-info.h"
+#include "qemu/target-info-def.h"
 #include "qemu/target-info-qom.h"
 #include "qom/object.h"
 
@@ -41,6 +42,18 @@ static void test_qom_types_are_unique(void)
     g_assert_true(targets == target_info_qom_get_targets());
 }
 
+static void test_accel_cpu_parent_per_target(void)
+{
+    g_assert(targets);
+
+    for (GSList *l = targets; l; l = l->next) {
+        const TargetInfo *ti = TARGET_INFO_CLASS(l->data)->target_info;
+        g_autofree char *name = g_strconcat("accel-", ti->cpu_type, NULL);
+
+        g_assert_nonnull(object_class_by_name(name));
+    }
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -55,5 +68,7 @@ int main(int argc, char **argv)
 
     g_test_add_func("/multi-targets/qom-types-are-unique",
                    test_qom_types_are_unique);
+    g_test_add_func("/multi-targets/accel-cpu-parent-per-target",
+                    test_accel_cpu_parent_per_target);
     return g_test_run();
 }
Signature · 1 line
2.55.0.windows.3
#3 lore raw

Re: [PATCH 2/2] accel: register CPU parent for each target

Quoted 6 lines · Yonggang Luo

Now we need use the prepared target-list to register different accel-$cpu_type parent QOM type. So that multi-target won't error missing parent for different accel for different arch.

Also add a test for it in test-multi-target.c

Quoted 9 lines
---
 accel/accel-common.c           |  6 ++----
 accel/accel-internal.h         |  1 +
 accel/accel-system.c           | 24 ++++++++++++++++++++++++
 accel/accel-user.c             |  7 +++++++
 tests/unit/test-multi-target.c | 15 +++++++++++++++
 5 files changed, 49 insertions(+), 4 deletions(-)

An alternative would be to keep a target specific accel file (accel-target.c), which only contains code registration for accel type, and no global symbols.

This way, the type is correctly present for all targets, without needing to iterate list of targets explicitly, as build system includes what is needed directly.

IMHO, it would be simpler and better.

What do you think?

Regards,
Pierrick

#4 lore raw

Re: [PATCH 2/2] accel: register CPU parent for each target

Quoted 22 lines · Pierrick Bouvier
Quoted 6 lines · Yonggang Luo

Now we need use the prepared target-list to register different accel-$cpu_type parent QOM type. So that multi-target won't error missing parent for different accel for different arch.

Also add a test for it in test-multi-target.c

Quoted 9 lines
---
 accel/accel-common.c           |  6 ++----
 accel/accel-internal.h         |  1 +
 accel/accel-system.c           | 24 ++++++++++++++++++++++++
 accel/accel-user.c             |  7 +++++++
 tests/unit/test-multi-target.c | 15 +++++++++++++++
 5 files changed, 49 insertions(+), 4 deletions(-)

An alternative would be to keep a target specific accel file (accel-target.c), which only contains code registration for accel type, and no global symbols.

Quoted 7 lines

This way, the type is correctly present for all targets, without needing to iterate list of targets explicitly, as build system includes what is needed directly.

IMHO, it would be simpler and better.

What do you think?

I am fine with this, I also consider this, I have avoided this because I am thinking
there won't any target-specific files except target-info-def.c in the remote future.
Now I think a small set of this type(target-specific) of files is also acceptable,
I'll go this way, much simpler.

Quoted 4 lines

Regards,
Pierrick

Signature · 6 lines
         此致
礼
罗勇刚
Yours
    sincerely,
Yonggang Luo
#5 lore raw

Re: [PATCH 2/2] accel: register CPU parent for each target

Quoted 32 lines · Yonggang Luo
Quoted 22 lines · Pierrick Bouvier
Quoted 6 lines · Yonggang Luo

Now we need use the prepared target-list to register different accel-$cpu_type parent QOM type. So that multi-target won't error missing parent for different accel for different arch.

Also add a test for it in test-multi-target.c

Quoted 9 lines
---
 accel/accel-common.c           |  6 ++----
 accel/accel-internal.h         |  1 +
 accel/accel-system.c           | 24 ++++++++++++++++++++++++
 accel/accel-user.c             |  7 +++++++
 tests/unit/test-multi-target.c | 15 +++++++++++++++
 5 files changed, 49 insertions(+), 4 deletions(-)

An alternative would be to keep a target specific accel file (accel-target.c), which only contains code registration for accel type, and no global symbols.

Quoted 7 lines

This way, the type is correctly present for all targets, without needing to iterate list of targets explicitly, as build system includes what is needed directly.

IMHO, it would be simpler and better.

What do you think?

Quoted 8 lines

I am fine with this, I also consider this, I have avoided this because I am thinking
there won't any target-specific files except target-info-def.c in the remote future.
Now I think a small set of this type(target-specific) of files is also acceptable,
I'll go this way, much simpler.

Sounds good.
As long as a file does not introduce global symbols, it's not really a problem to have target-specific files.

Quoted 7 lines
Quoted 4 lines

Regards,
Pierrick