[PATCH 1/9] hw/mem: Add tag support to generic host memory backends

Alireza Sanaee via qemu development posted 9 patches 1 week, 1 day ago
[PATCH 1/9] hw/mem: Add tag support to generic host memory backends
Posted by Alireza Sanaee via qemu development 1 week, 1 day ago
Add a string tag property to HostMemoryBackend so that backends can be
identified by a user-assigned name at runtime. Expose the property through
QOM and add a host_memory_backend_find_by_tag() helper that walks the
object tree to locate a backend by its tag.

This is used by the following CXL patches to resolve a named backend for a
pending DC extent and attach it to the accepted extent when the host
acknowledges the add request.

Signed-off-by: Alireza Sanaee <alireza.sanaee@huawei.com>
---
 backends/hostmem.c       | 72 ++++++++++++++++++++++++++++++++++++++++
 include/system/hostmem.h |  2 ++
 qapi/qom.json            |  3 ++
 tests/qtest/qom-test.c   |  8 +++--
 4 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/backends/hostmem.c b/backends/hostmem.c
index 15d4365b69..270cae9569 100644
--- a/backends/hostmem.c
+++ b/backends/hostmem.c
@@ -47,6 +47,50 @@ host_memory_backend_get_name(HostMemoryBackend *backend)
     return object_get_canonical_path(OBJECT(backend));
 }
 
+typedef struct HostMemoryBackendTagSearch {
+    const char *tag;
+    HostMemoryBackend *backend;
+} HostMemoryBackendTagSearch;
+
+static int host_memory_backend_find_by_tag_cb(Object *obj, void *opaque)
+{
+    HostMemoryBackendTagSearch *ctx = opaque;
+    HostMemoryBackend *backend;
+
+    if (!object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
+        return 0;
+    }
+
+    backend = MEMORY_BACKEND(obj);
+    if (!backend->tag) {
+        return 0;
+    }
+
+    if (strcmp(backend->tag, ctx->tag) == 0) {
+        ctx->backend = backend;
+        return 1;
+    }
+
+    return 0;
+}
+
+HostMemoryBackend *host_memory_backend_find_by_tag(const char *tag)
+{
+    HostMemoryBackendTagSearch ctx = {
+        .tag = tag,
+        .backend = NULL,
+    };
+
+    if (!tag) {
+        return NULL;
+    }
+
+    object_child_foreach_recursive(object_get_objects_root(),
+                                   host_memory_backend_find_by_tag_cb, &ctx);
+
+    return ctx.backend;
+}
+
 static void
 host_memory_backend_get_size(Object *obj, Visitor *v, const char *name,
                              void *opaque, Error **errp)
@@ -460,6 +504,22 @@ static void host_memory_backend_set_share(Object *o, bool value, Error **errp)
     backend->share = value;
 }
 
+static void
+host_memory_backend_set_tag(Object *obj, const char *value, Error **errp)
+{
+    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
+
+    g_free(backend->tag);
+    backend->tag = g_strdup(value);
+}
+
+static char *host_memory_backend_get_tag(Object *obj, Error **errp)
+{
+    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
+
+    return g_strdup(backend->tag);
+}
+
 #ifdef CONFIG_LINUX
 static bool host_memory_backend_get_reserve(Object *o, Error **errp)
 {
@@ -501,6 +561,13 @@ host_memory_backend_set_use_canonical_path(Object *obj, bool value,
     backend->use_canonical_path = value;
 }
 
+static void host_memory_backend_finalize(Object *obj)
+{
+    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
+
+    g_free(backend->tag);
+}
+
 static void
 host_memory_backend_class_init(ObjectClass *oc, const void *data)
 {
@@ -557,6 +624,10 @@ host_memory_backend_class_init(ObjectClass *oc, const void *data)
         host_memory_backend_get_share, host_memory_backend_set_share);
     object_class_property_set_description(oc, "share",
         "Mark the memory as private to QEMU or shared");
+    object_class_property_add_str(oc, "tag",
+        host_memory_backend_get_tag, host_memory_backend_set_tag);
+    object_class_property_set_description(oc, "tag",
+        "A user-defined tag to identify this memory backend");
 #ifdef CONFIG_LINUX
     object_class_property_add_bool(oc, "reserve",
         host_memory_backend_get_reserve, host_memory_backend_set_reserve);
@@ -586,6 +657,7 @@ static const TypeInfo host_memory_backend_info = {
     .class_init = host_memory_backend_class_init,
     .instance_size = sizeof(HostMemoryBackend),
     .instance_init = host_memory_backend_init,
+    .instance_finalize = host_memory_backend_finalize,
     .instance_post_init = host_memory_backend_post_init,
     .interfaces = (const InterfaceInfo[]) {
         { TYPE_USER_CREATABLE },
diff --git a/include/system/hostmem.h b/include/system/hostmem.h
index 88fa791ac7..a02e173d48 100644
--- a/include/system/hostmem.h
+++ b/include/system/hostmem.h
@@ -81,12 +81,14 @@ struct HostMemoryBackend {
     ThreadContext *prealloc_context;
     DECLARE_BITMAP(host_nodes, MAX_NODES + 1);
     HostMemPolicy policy;
+    char *tag;
 
     MemoryRegion mr;
 };
 
 bool host_memory_backend_mr_inited(HostMemoryBackend *backend);
 MemoryRegion *host_memory_backend_get_memory(HostMemoryBackend *backend);
+HostMemoryBackend *host_memory_backend_find_by_tag(const char *tag);
 
 void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped);
 bool host_memory_backend_is_mapped(HostMemoryBackend *backend);
diff --git a/qapi/qom.json b/qapi/qom.json
index c653248f85..e6feeb324a 100644
--- a/qapi/qom.json
+++ b/qapi/qom.json
@@ -658,6 +658,8 @@
 # @reserve: if true, reserve swap space (or huge pages) if applicable
 #     (default: true) (since 6.1)
 #
+# @tag: user-defined memory backend tag (since 11.1)
+#
 # @size: size of the memory region in bytes
 #
 # @x-use-canonical-path-for-ramblock-id: if true, the canonical path
@@ -683,6 +685,7 @@
             '*prealloc-context': 'str',
             '*share': 'bool',
             '*reserve': 'bool',
+            '*tag': 'str',
             'size': 'size',
             '*x-use-canonical-path-for-ramblock-id': 'bool' } }
 
diff --git a/tests/qtest/qom-test.c b/tests/qtest/qom-test.c
index 6421f2d9d9..3109b47cca 100644
--- a/tests/qtest/qom-test.c
+++ b/tests/qtest/qom-test.c
@@ -17,6 +17,7 @@
 
 #define RAM_NAME "node0"
 #define RAM_SIZE 65536
+#define RAM_TAG "ramtag0"
 
 static int verbosity_level;
 
@@ -59,6 +60,8 @@ static void test_list_get_value(QTestState *qts)
 
         } else if (!strcmp(prop_name, "size")) {
             g_assert_cmpint(qdict_get_int(prop, "value"), ==, RAM_SIZE);
+        } else if (!strcmp(prop_name, "tag")) {
+            g_assert_cmpstr(qdict_get_str(prop, "value"), ==, RAM_TAG);
         }
     }
 }
@@ -195,8 +198,9 @@ static void test_machine(gconstpointer data)
     QTestState *qts;
     g_autoptr(QList) paths = qlist_new();
 
-    qts = qtest_initf("-machine %s -object memory-backend-ram,id=%s,size=%d",
-                      machine, RAM_NAME, RAM_SIZE);
+    qts = qtest_initf("-machine %s -object memory-backend-ram,id=%s,size=%d"
+                      ",tag=%s",
+                      machine, RAM_NAME, RAM_SIZE, RAM_TAG);
 
     if (g_test_slow()) {
         /* Make sure we can get the machine class properties: */
-- 
2.50.1 (Apple Git-155)
Re: [PATCH 1/9] hw/mem: Add tag support to generic host memory backends
Posted by Markus Armbruster 1 week ago
Alireza Sanaee <alireza.sanaee@huawei.com> writes:

> Add a string tag property to HostMemoryBackend so that backends can be
> identified by a user-assigned name at runtime. Expose the property through
> QOM and add a host_memory_backend_find_by_tag() helper that walks the
> object tree to locate a backend by its tag.

Why can't you use the QOM ID?

$ qemu-system-x86_64 -S -display none -monitor stdio -object memory-backend-ram,id=fred,size=1M
QEMU 10.2.90 monitor - type 'help' for more information
(qemu) info qom-tree /objects/fred
/fred (memory-backend-ram)
  /fred[0] (memory-region)
Re: [PATCH 1/9] hw/mem: Add tag support to generic host memory backends
Posted by Alireza Sanaee via qemu development 1 week ago
On Thu, 26 Mar 2026 11:42:17 +0100
Markus Armbruster <armbru@redhat.com> wrote:

Hi Markus,

> Alireza Sanaee <alireza.sanaee@huawei.com> writes:
> 
> > Add a string tag property to HostMemoryBackend so that backends can be
> > identified by a user-assigned name at runtime. Expose the property through
> > QOM and add a host_memory_backend_find_by_tag() helper that walks the
> > object tree to locate a backend by its tag.  
> 
> Why can't you use the QOM ID?
> 
> $ qemu-system-x86_64 -S -display none -monitor stdio -object memory-backend-ram,id=fred,size=1M
> QEMU 10.2.90 monitor - type 'help' for more information
> (qemu) info qom-tree /objects/fred
> /fred (memory-backend-ram)
>   /fred[0] (memory-region)
> 
>  

Fair question. The ID would have been a solution if it had accepted GUIDs. CXL spec 
requires GUIDs for extents as identification, hence new tag property.

alireza@blue-ocean:~/Downloads$ qemu-system-x86_64 -s -display none -monitor std
io -object memory-backend-ram,id=02bb2436-e87d-4aa1-b2be-aaffdd76bb79,size=1M
qemu-system-x86_64: -object memory-backend-ram,id=02bb2436-e87d-4aa1-b2be-aaffdd
76bb79,size=1M: Parameter 'id' expects an identifier
Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.

Thanks,
Ali
Re: [PATCH 1/9] hw/mem: Add tag support to generic host memory backends
Posted by Markus Armbruster 1 week ago
Alireza Sanaee <alireza.sanaee@huawei.com> writes:

> On Thu, 26 Mar 2026 11:42:17 +0100
> Markus Armbruster <armbru@redhat.com> wrote:
>
> Hi Markus,
>
>> Alireza Sanaee <alireza.sanaee@huawei.com> writes:
>> 
>> > Add a string tag property to HostMemoryBackend so that backends can be
>> > identified by a user-assigned name at runtime. Expose the property through
>> > QOM and add a host_memory_backend_find_by_tag() helper that walks the
>> > object tree to locate a backend by its tag.  
>> 
>> Why can't you use the QOM ID?
>> 
>> $ qemu-system-x86_64 -S -display none -monitor stdio -object memory-backend-ram,id=fred,size=1M
>> QEMU 10.2.90 monitor - type 'help' for more information
>> (qemu) info qom-tree /objects/fred
>> /fred (memory-backend-ram)
>>   /fred[0] (memory-region)
>> 
>>  
>
> Fair question. The ID would have been a solution if it had accepted GUIDs. CXL spec 
> requires GUIDs for extents as identification, hence new tag property.

Your commit message should state that this needs to be a GUID and why.

> alireza@blue-ocean:~/Downloads$ qemu-system-x86_64 -s -display none -monitor std
> io -object memory-backend-ram,id=02bb2436-e87d-4aa1-b2be-aaffdd76bb79,size=1M
> qemu-system-x86_64: -object memory-backend-ram,id=02bb2436-e87d-4aa1-b2be-aaffdd
> 76bb79,size=1M: Parameter 'id' expects an identifier
> Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.
>
> Thanks,
> Ali
Re: [PATCH 1/9] hw/mem: Add tag support to generic host memory backends
Posted by Alireza Sanaee via qemu development 1 week ago
On Thu, 26 Mar 2026 14:01:06 +0100
Markus Armbruster <armbru@redhat.com> wrote:

Hi Markus,

Sure, absolutely.
> Alireza Sanaee <alireza.sanaee@huawei.com> writes:
> 
> > On Thu, 26 Mar 2026 11:42:17 +0100
> > Markus Armbruster <armbru@redhat.com> wrote:
> >
> > Hi Markus,
> >  
> >> Alireza Sanaee <alireza.sanaee@huawei.com> writes:
> >>   
> >> > Add a string tag property to HostMemoryBackend so that backends can be
> >> > identified by a user-assigned name at runtime. Expose the property through
> >> > QOM and add a host_memory_backend_find_by_tag() helper that walks the
> >> > object tree to locate a backend by its tag.    
> >> 
> >> Why can't you use the QOM ID?
> >> 
> >> $ qemu-system-x86_64 -S -display none -monitor stdio -object memory-backend-ram,id=fred,size=1M
> >> QEMU 10.2.90 monitor - type 'help' for more information
> >> (qemu) info qom-tree /objects/fred
> >> /fred (memory-backend-ram)
> >>   /fred[0] (memory-region)
> >> 
> >>    
> >
> > Fair question. The ID would have been a solution if it had accepted GUIDs. CXL spec 
> > requires GUIDs for extents as identification, hence new tag property.  
> 
> Your commit message should state that this needs to be a GUID and why.
> 
> > alireza@blue-ocean:~/Downloads$ qemu-system-x86_64 -s -display none -monitor std
> > io -object memory-backend-ram,id=02bb2436-e87d-4aa1-b2be-aaffdd76bb79,size=1M
> > qemu-system-x86_64: -object memory-backend-ram,id=02bb2436-e87d-4aa1-b2be-aaffdd
> > 76bb79,size=1M: Parameter 'id' expects an identifier
> > Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.
> >
> > Thanks,
> > Ali  
> 
>