Skip to contentSkip to conversation

Fix Memory Region Name bugs

PATCH In review Peter Crosthwaite 5 participants 11 messages Aug 20, 2014 – Aug 21, 2014 Arm STM32

lore
Share & tools
lore
#0 lore raw

cover Fix Memory Region Name bugs

Hi Peter,

Here is an attempt at a proper fix to the memory region naming bugs you reported. The Xen compile bug and the memory leak.

Regards,
Peter

Peter Crosthwaite (3):
xen: hvm: Abstract away memory region name ref
qom: return const for object_get_canon_path component
qom: object.h: Update object_get_canon_path* doc

 backends/hostmem-ram.c | 3 +--
 hw/mem/pc-dimm.c       | 3 +--
 include/qom/object.h   | 8 +++++---
 iothread.c             | 2 +-
 numa.c                 | 4 +++-
 qom/object.c           | 9 ++++-----
 xen-hvm.c              | 9 ++++++---
 7 files changed, 21 insertions(+), 17 deletions(-)
Signature · 1 line
2.0.1.1.gfbfc394
#1 lore raw

PATCH 1/3 xen: hvm: Abstract away memory region name ref

The mr->name field is removed. This slipped through compile testing. Fix.

---

 xen-hvm.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
xen-hvm.c +6 -3
index 91de2e2..9fd9d6e 100644
@@ -291,6 +291,7 @@ static int xen_add_to_physmap(XenIOState *state,
     hwaddr pfn, start_gpfn;
     hwaddr phys_offset = memory_region_get_ram_addr(mr);
     char path[80], value[17];
+    const char *mr_name;
 
     if (get_physmapping(state, start_addr, size)) {
         return 0;
@@ -326,11 +327,13 @@ go_physmap:
         }
     }
 
+    mr_name = memory_region_name(mr);
+
     physmap = g_malloc(sizeof (XenPhysmap));
 
     physmap->start_addr = start_addr;
     physmap->size = size;
-    physmap->name = (char *)mr->name;
+    physmap->name = (char *)mr_name;
     physmap->phys_offset = phys_offset;
 
     QLIST_INSERT_HEAD(&state->physmap, physmap, list);
@@ -354,11 +357,11 @@ go_physmap:
     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
         return -1;
     }
-    if (mr->name) {
+    if (mr_name) {
         snprintf(path, sizeof(path),
                 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
                 xen_domid, (uint64_t)phys_offset);
-        if (!xs_write(state->xenstore, 0, path, mr->name, strlen(mr->name))) {
+        if (!xs_write(state->xenstore, 0, path, mr_name, strlen(mr_name))) {
             return -1;
         }
     }
Signature · 1 line
2.0.1.1.gfbfc394
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH bugfix v1 1/3] xen: hvm: Abstract away memory region name ref (fix broken build)

Am 20.08.2014 um 07:07 schrieb Peter Crosthwaite:

Quoted 23 lines

The mr->name field is removed. This slipped through compile testing. Fix.

---

 xen-hvm.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
xen-hvm.c +1 -0
index 91de2e2..9fd9d6e 100644
@@ -291,6 +291,7 @@ static int xen_add_to_physmap(XenIOState *state,
     hwaddr pfn, start_gpfn;
     hwaddr phys_offset = memory_region_get_ram_addr(mr);
     char path[80], value[17];
+    const char *mr_name;
 
     if (get_physmapping(state, start_addr, size)) {
         return 0;
@@ -326,11 +327,13 @@ go_physmap:
         }

xen-hvm.c go_physmap: · line 336

Quoted 10 lines
xen-hvm.c +3 -1
     }
 
+    mr_name = memory_region_name(mr);
+
     physmap = g_malloc(sizeof (XenPhysmap));
 
     physmap->start_addr = start_addr;
     physmap->size = size;
-    physmap->name = (char *)mr->name;
+    physmap->name = (char *)mr_name;

This type cast can be removed. Just add the const in XenPhysmap's name declaration.

Quoted 8 lines
xen-hvm.c +0 -0
     physmap->phys_offset = phys_offset;
 
     QLIST_INSERT_HEAD(&state->physmap, physmap, list);
@@ -354,11 +357,11 @@ go_physmap:
     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
         return -1;
     }

xen-hvm.c go_physmap: · line 367

Quoted 10 lines
xen-hvm.c +2 -2
-    if (mr->name) {
+    if (mr_name) {
         snprintf(path, sizeof(path),
                 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
                 xen_domid, (uint64_t)phys_offset);
-        if (!xs_write(state->xenstore, 0, path, mr->name, strlen(mr->name))) {
+        if (!xs_write(state->xenstore, 0, path, mr_name, strlen(mr_name))) {
             return -1;
         }
     }

Otherwise ok. Below is my own variant which I tested before I noticed your patch.

@@ -69,11 +69,11 @@ static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
#define BUFFER_IO_MAX_DELAY 100

 typedef struct XenPhysmap {
     hwaddr start_addr;
     ram_addr_t size;
-    char *name;
+    const char *name;
     hwaddr phys_offset;

     QLIST_ENTRY(XenPhysmap) list;

} XenPhysmap;

@@ -328,11 +328,11 @@ go_physmap:

     physmap = g_malloc(sizeof (XenPhysmap));

     physmap->start_addr = start_addr;
     physmap->size = size;
-    physmap->name = (char *)mr->name;
+    physmap->name = memory_region_name(mr);
     physmap->phys_offset = phys_offset;

     QLIST_INSERT_HEAD(&state->physmap, physmap, list);

     xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
@@ -352,15 +352,16 @@ go_physmap:
             xen_domid, (uint64_t)phys_offset);
     snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)size);
     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
         return -1;
     }
-    if (mr->name) {
+    if (physmap->name) {
         snprintf(path, sizeof(path),
                 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
                 xen_domid, (uint64_t)phys_offset);
-        if (!xs_write(state->xenstore, 0, path, mr->name,
strlen(mr->name))) {
+        if (!xs_write(state->xenstore, 0, path, physmap->name,
+                      strlen(physmap->name))) {
             return -1;
         }
     }

     return 0;
#3 lore raw

Re: [Qemu-devel] [PATCH bugfix v1 1/3] xen: hvm: Abstract away memory region name ref (fix broken build)

Quoted 30 lines · Stefan Weil

Am 20.08.2014 um 07:07 schrieb Peter Crosthwaite:

Quoted 29 lines

The mr->name field is removed. This slipped through compile testing. Fix.

---

 xen-hvm.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
xen-hvm.c +3 -0
index 91de2e2..9fd9d6e 100644
@@ -291,6 +291,7 @@ static int xen_add_to_physmap(XenIOState *state,
     hwaddr pfn, start_gpfn;
     hwaddr phys_offset = memory_region_get_ram_addr(mr);
     char path[80], value[17];
+    const char *mr_name;
 
     if (get_physmapping(state, start_addr, size)) {
         return 0;
@@ -326,11 +327,13 @@ go_physmap:
         }
     }
 
+    mr_name = memory_region_name(mr);
+
     physmap = g_malloc(sizeof (XenPhysmap));
 
Quoted 7 lines

xen-hvm.c go_physmap: · line 336

Quoted 4 lines
xen-hvm.c +1 -1
     physmap->start_addr = start_addr;
     physmap->size = size;
-    physmap->name = (char *)mr->name;
+    physmap->name = (char *)mr_name;

This type cast can be removed. Just add the const in XenPhysmap's name declaration.

Yes, I noticed that dodgy cast too. Stefano, is there a reason why the XenPhysmap field isn't just a const pointer?

thanks
-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH bugfix v1 1/3] xen: hvm: Abstract away memory region name ref (fix broken build)

Quoted 39 lines · Peter Maydell
Quoted 30 lines · Stefan Weil

Am 20.08.2014 um 07:07 schrieb Peter Crosthwaite:

Quoted 29 lines

The mr->name field is removed. This slipped through compile testing. Fix.

---

 xen-hvm.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
xen-hvm.c +3 -0
index 91de2e2..9fd9d6e 100644
@@ -291,6 +291,7 @@ static int xen_add_to_physmap(XenIOState *state,
     hwaddr pfn, start_gpfn;
     hwaddr phys_offset = memory_region_get_ram_addr(mr);
     char path[80], value[17];
+    const char *mr_name;
 
     if (get_physmapping(state, start_addr, size)) {
         return 0;
@@ -326,11 +327,13 @@ go_physmap:
         }
     }
 
+    mr_name = memory_region_name(mr);
+
     physmap = g_malloc(sizeof (XenPhysmap));
 
Quoted 7 lines

xen-hvm.c go_physmap: · line 336

Quoted 4 lines
xen-hvm.c +1 -1
     physmap->start_addr = start_addr;
     physmap->size = size;
-    physmap->name = (char *)mr->name;
+    physmap->name = (char *)mr_name;

This type cast can be removed. Just add the const in XenPhysmap's name declaration.

Quoted 3 lines

Yes, I noticed that dodgy cast too. Stefano, is there a reason why the XenPhysmap field isn't just a const pointer?

No reason, we could just switch to const char*. I would be happy to take a patch for that.

Sorry for the late reply, I am attending XenSummit/LinuxCon.

#5 lore raw

PATCH 2/3 qom: return const for object_get_canon_path component

In reply to Peter Crosthwaite

Most (if not all) calls sites of object_get_canonical_path component use it to get a read-only version for usage as an object name. Avoid the need for the callers to explicitly free the returned string and mark it const accordingly.

Frees are removed from call sites accordingly.

---

 backends/hostmem-ram.c | 3 +--
 hw/mem/pc-dimm.c       | 3 +--
 include/qom/object.h   | 2 +-
 iothread.c             | 2 +-
 numa.c                 | 4 +++-
 qom/object.c           | 9 ++++-----
 6 files changed, 11 insertions(+), 12 deletions(-)
backends/hostmem-ram.c +1 -2
index d9a8290..a13ec23 100644
@@ -18,7 +18,7 @@
 static void
 ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
-    char *path;
+    const char *path;
 
     if (!backend->size) {
         error_setg(errp, "can't create backend with size 0");
@@ -28,7 +28,6 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     path = object_get_canonical_path_component(OBJECT(backend));
     memory_region_init_ram(&backend->mr, OBJECT(backend), path,
                            backend->size);
-    g_free(path);
 }
 
 static void
hw/mem/pc-dimm.c +1 -2
index 5bfc5b7..bc0ef18 100644
@@ -223,9 +223,8 @@ static void pc_dimm_check_memdev_is_busy(Object *obj, const char *name,
 
     mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), errp);
     if (memory_region_is_mapped(mr)) {
-        char *path = object_get_canonical_path_component(val);
+        const char *path = object_get_canonical_path_component(val);
         error_setg(errp, "can't use already busy memdev: %s", path);
-        g_free(path);
     } else {
         qdev_prop_allow_set_link_before_realize(obj, name, val, errp);
     }
include/qom/object.h +1 -1
index 8a05a81..8618e49 100644
@@ -1031,7 +1031,7 @@ Object *object_get_root(void);
  * Returns: The final component in the object's canonical path.  The canonical
  * path is the path within the composition tree starting from the root.
  */
-gchar *object_get_canonical_path_component(Object *obj);
+const gchar *object_get_canonical_path_component(Object *obj);
 
 /**
  * object_get_canonical_path:
iothread.c +1 -1
index d9403cf..37cb0af 100644
@@ -126,7 +126,7 @@ IOThread *iothread_find(const char *id)
 
 char *iothread_get_id(IOThread *iothread)
 {
-    return object_get_canonical_path_component(OBJECT(iothread));
+    return g_strdup(object_get_canonical_path_component(OBJECT(iothread)));
 }
 
 AioContext *iothread_get_aio_context(IOThread *iothread)
numa.c +3 -1
index c78cec9..3e05ac5 100644
@@ -302,7 +302,9 @@ void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
         }
 
         if (memory_region_is_mapped(seg)) {
-            char *path = object_get_canonical_path_component(OBJECT(backend));
+            const char *path;
+
+            path = object_get_canonical_path_component(OBJECT(backend));
             error_report("memory backend %s is used multiple times. Each "
                          "-numa option must use a different memdev value.",
                          path);
qom/object.c +4 -5
index 1b00831..a7fd480 100644
@@ -1219,7 +1219,7 @@ out:
     g_free(full_type);
 }
 
-gchar *object_get_canonical_path_component(Object *obj)
+const gchar *object_get_canonical_path_component(Object *obj)
 {
     ObjectProperty *prop = NULL;
 
@@ -1232,7 +1232,7 @@ gchar *object_get_canonical_path_component(Object *obj)
         }
 
         if (prop->opaque == obj) {
-            return g_strdup(prop->name);
+            return prop->name;
         }
     }
 
@@ -1247,15 +1247,14 @@ gchar *object_get_canonical_path(Object *obj)
     char *newpath, *path = NULL;
 
     while (obj != root) {
-        char *component = object_get_canonical_path_component(obj);
+        const char *component = object_get_canonical_path_component(obj);
 
         if (path) {
             newpath = g_strdup_printf("%s/%s", component, path);
-            g_free(component);
             g_free(path);
             path = newpath;
         } else {
-            path = component;
+            path = g_strdup(component);
         }
 
         obj = obj->parent;
Signature · 1 line
2.0.1.1.gfbfc394
#6 lore raw

PATCH 3/3 qom: object.h: Update object_get_canon_path* doc

In reply to Peter Crosthwaite

With information about return value ownership.

---

 include/qom/object.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/qom/object.h +4 -2
index 8618e49..87de889 100644
@@ -1029,7 +1029,8 @@ Object *object_get_root(void);
  * object_get_canonical_path_component:
  *
  * Returns: The final component in the object's canonical path.  The canonical
- * path is the path within the composition tree starting from the root.
+ * path is the path within the composition tree starting from the root. The
+ * returned value may not be modified.
  */
 const gchar *object_get_canonical_path_component(Object *obj);
 
@@ -1037,7 +1038,8 @@ const gchar *object_get_canonical_path_component(Object *obj);
  * object_get_canonical_path:
  *
  * Returns: The canonical path for a object.  This is the path within the
- * composition tree starting from the root.
+ * composition tree starting from the root.  The returned value is allocated
+ * memory and the client is responsible for freeing the returned value.
  */
 gchar *object_get_canonical_path(Object *obj);
 
Signature · 1 line
2.0.1.1.gfbfc394
#7 lore raw

Re: [Qemu-devel] [PATCH bugfix v1 3/3] qom: object.h: Update object_get_canon_path* doc

Quoted 13 lines · Peter Crosthwaite

With information about return value ownership.

---

 include/qom/object.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/qom/object.h +0 -0
index 8618e49..87de889 100644

include/qom/object.h Object *object_get_root(void); · line 1035

Quoted 8 lines
include/qom/object.h +2 -1
@@ -1029,7 +1029,8 @@ Object *object_get_root(void);
  * object_get_canonical_path_component:
  *
  * Returns: The final component in the object's canonical path.  The canonical
- * path is the path within the composition tree starting from the root.
+ * path is the path within the composition tree starting from the root. The
+ * returned value may not be modified.
  */
 const gchar *object_get_canonical_path_component(Object *obj);

The other thing you need to say is that the returned string is only valid for as long as the object remains a child property of its parent. (Is that right? I'm not clear. It also sounds like a really awkward lifetime management requirement, which suggests to me that really the "return-a-copy" semantics are nicer.)

Having object_get_canonical_path_component() and object_get_canonical_path() having different return value ownership semantics is likely to be a bit confusing I think.

If we do do this I think I'd put the doc comment change in the same patch that changes the semantics.

thanks
-- PMM

#8 lore raw

Re: [Qemu-devel] [PATCH bugfix v1 3/3] qom: object.h: Update object_get_canon_path* doc

Quoted 21 lines · Peter Maydell
Quoted 20 lines · Peter Crosthwaite

With information about return value ownership.

---

 include/qom/object.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/qom/object.h +2 -1
index 8618e49..87de889 100644
@@ -1029,7 +1029,8 @@ Object *object_get_root(void);
  * object_get_canonical_path_component:
  *
  * Returns: The final component in the object's canonical path.  The canonical
- * path is the path within the composition tree starting from the root.
+ * path is the path within the composition tree starting from the root. The
+ * returned value may not be modified.
  */
Quoted 8 lines

include/qom/object.h Object *object_get_root(void); · line 1035

Quoted 1 line
include/qom/object.h +0 -0
 const gchar *object_get_canonical_path_component(Object *obj);

The other thing you need to say is that the returned string is only valid for as long as the object remains a child property of its parent. (Is that right? I'm not clear. It also sounds like a really awkward lifetime management requirement, which suggests to me that really the "return-a-copy" semantics are nicer.)

It is. But in QOM that is the naming scheme. An object is uniquely identified by it's full canonical path. If you don't have a parent, you don't have a name. If you have a dup of the name, but then the object gets reparented or destroyed your name is stale and I think the caller should have to deal with that.

Quoted 5 lines

Having object_get_canonical_path_component() and object_get_canonical_path() having different return value ownership semantics is likely to be a bit confusing I think.

Alternatives I can think of include,

1: All memory_region_name() callsites have to do a free. 2: Memory API keeps a single copy of the name (lazily inited in memory_region_name perhaps).
3: Patch struct object with a char instance_name[] field and have QOM ensure the fixed location is always a valid name ("\0" for an unparented object).
4: This

Please let me know your preference.

Regards,
Peter

Quoted 6 lines

If we do do this I think I'd put the doc comment change in the same patch that changes the semantics.

thanks
-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH bugfix v1 3/3] qom: object.h: Update object_get_canon_path* doc

In reply to Peter Maydell

Il 20/08/2014 11:07, Peter Maydell ha scritto:

Quoted 6 lines

The other thing you need to say is that the returned string is only valid for as long as the object remains a child property of its parent. (Is that right? I'm not clear. It also sounds like a really awkward lifetime management requirement, which suggests to me that really the "return-a-copy" semantics are nicer.)

They are indeed nicer, but harder to use in C.

Unparenting is (currently) always done under the BQL, so guaranteeing that the returned string stays alive as long as you use it is currently not a problem. This might change in the future, but I don't see many reasons to get rid completely of the BQL (as opposed to the surgical moving of stuff outside it that we've been doing so far).

Quoted 3 lines

Having object_get_canonical_path_component() and object_get_canonical_path() having different return value ownership semantics is likely to be a bit confusing I think.

Right, though that could be solved by renaming the _component() function.

Paolo

#10 lore raw

Re: [Qemu-devel] [PATCH bugfix v1 3/3] qom: object.h: Update object_get_canon_path* doc

Quoted 16 lines · Paolo Bonzini

Il 20/08/2014 11:07, Peter Maydell ha scritto:

Quoted 6 lines

The other thing you need to say is that the returned string is only valid for as long as the object remains a child property of its parent. (Is that right? I'm not clear. It also sounds like a really awkward lifetime management requirement, which suggests to me that really the "return-a-copy" semantics are nicer.)

They are indeed nicer, but harder to use in C.

Unparenting is (currently) always done under the BQL, so guaranteeing that the returned string stays alive as long as you use it is currently not a problem. This might change in the future, but I don't see many reasons to get rid completely of the BQL (as opposed to the surgical moving of stuff outside it that we've been doing so far).

Quoted 6 lines
Quoted 3 lines

Having object_get_canonical_path_component() and object_get_canonical_path() having different return value ownership semantics is likely to be a bit confusing I think.

Right, though that could be solved by renaming the _component() function.

Yes that had crossed my mind too. object_get_canonical_path_component is a mouthful and it's usages suggest object_get_name or object_get_instance_name would be more fitting.

The fact that it is a canonical_path_component is more of an implementation detail rather than its externally useful functionality.

Regards,
Peter

Quoted 2 lines

Paolo