Skip to contentSkip to conversation

Fix check-qtest-ppc64 sanitizer errors

PATCH Superseded Akihiko Odaki 2 participants 11 messages Jul 8, 2024

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v3 cover Fix check-qtest-ppc64 sanitizer errors

Based-on: <3ad18bc590ef28e1526e8053568086b453e7ffde.1718211878.git.quic_mathbern@quicinc.com> ("[PATCH] cpu: fix memleak of 'halt_cond' and 'thread'")

I saw various sanitizer errors when running check-qtest-ppc64. While I could just turn off sanitizers, I decided to tackle them this time.

Unfortunately, GLib does not free test data in some cases so some sanitizer errors remain. All sanitizer errors will be gone with this patch series combined with the following change for GLib:
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4120

---
Changes in v3:
- Added patch "memory: Clarify that we use owner's reference count".
- Added patch "memory: Refer to docs/devel/memory.rst for 'owner'".
- Fixed the message of patch
"memory: Do not create circular reference with subregion".
- Dropped patch "cpu: Free cpu_ases" in favor of:
https://lore.kernel.org/r/[email protected]/
("[PATCH V13 6/8] physmem: Add helper function to destroy CPU AddressSpace")
- Dropped patches "hw/ide: Convert macio ide_irq into GPIO line" and "hw/ide: Remove internal DMA qemu_irq" in favor of commit efb359346c7a ("hw/ide/macio: switch from using qemu_allocate_irq() to qdev input GPIOs")
- Dropped patch "hw/isa/vt82c686: Define a GPIO line between vt82c686 and i8259" in favor of:
https://patchew.org/QEMU/[email protected]/
("[PATCH 0/3] Resolve vt82c686 and piix4 qemu_irq memory leaks")
- Dropped pulled patches.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Rebased to "[PATCH] cpu: fix memleak of 'halt_cond' and 'thread'". (Philippe Mathieu-Daudé)
- Converted IRQs into GPIO lines and removed one qemu_irq usage. (Peter Maydell)
- s/suppresses/fixes/ (Michael S. Tsirkin)
- Corrected title of patch "hw/virtio: Free vqs after vhost_dev_cleanup()" (was "hw/virtio: Free vqs before vhost_dev_cleanup()")
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Akihiko Odaki (9):
spapr: Free stdout path
ppc/vof: Fix unaligned FDT property access
migration: Free removed SaveStateEntry
memory: Do not refer to "memory region's reference count"
memory: Refer to docs/devel/memory.rst for "owner"
memory: Clarify that owner may be missing
memory: Clarify owner must not call memory_region_ref()
memory: Do not create circular reference with subregion tests/qtest: Delete previous boot file

 include/exec/memory.h        | 22 +++++++---------------
 hw/ppc/spapr_vof.c           |  2 +-
 hw/ppc/vof.c                 |  2 +-
 migration/savevm.c           |  2 ++
 system/memory.c              | 11 +++++++++--
 tests/qtest/migration-test.c | 18 +++++++++++-------
 6 files changed, 31 insertions(+), 26 deletions(-)

---
base-commit: f2cb4026fccfe073f84a4b440e41d3ed0c3134f6
change-id: 20240625-san-097afaf4f1c2

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw

PATCH v3 1/9 spapr: Free stdout path

This fixes LeakSanitizer warnings.

---
 hw/ppc/spapr_vof.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/spapr_vof.c +1 -1
index 09f29be0b9de..c02eaacfed0b 100644
@@ -28,7 +28,7 @@ target_ulong spapr_h_vof_client(PowerPCCPU *cpu, SpaprMachineState *spapr,
 
 void spapr_vof_client_dt_finalize(SpaprMachineState *spapr, void *fdt)
 {
-    char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
+    g_autofree char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
 
     vof_build_dt(fdt, spapr->vof);
 
Signature · 1 line
2.45.2
#2 lore raw

PATCH v3 2/9 ppc/vof: Fix unaligned FDT property access

In reply to Akihiko Odaki

FDT properties are aligned by 4 bytes, not 8 bytes.

---
 hw/ppc/vof.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/vof.c +1 -1
index e3b430a81f4f..b5b6514d79fc 100644
@@ -646,7 +646,7 @@ static void vof_dt_memory_available(void *fdt, GArray *claimed, uint64_t base)
     mem0_reg = fdt_getprop(fdt, offset, "reg", &proplen);
     g_assert(mem0_reg && proplen == sizeof(uint32_t) * (ac + sc));
     if (sc == 2) {
-        mem0_end = be64_to_cpu(*(uint64_t *)(mem0_reg + sizeof(uint32_t) * ac));
+        mem0_end = ldq_be_p(mem0_reg + sizeof(uint32_t) * ac);
     } else {
         mem0_end = be32_to_cpu(*(uint32_t *)(mem0_reg + sizeof(uint32_t) * ac));
     }
Signature · 1 line
2.45.2
#3 lore raw

PATCH v3 3/9 migration: Free removed SaveStateEntry

In reply to Akihiko Odaki

This fixes LeakSanitizer warnings.

---
 migration/savevm.c | 2 ++
 1 file changed, 2 insertions(+)
migration/savevm.c +2 -0
index deb57833f8a8..85958d7b09cd 100644
@@ -874,6 +874,8 @@ int vmstate_replace_hack_for_ppc(VMStateIf *obj, int instance_id,
 
     if (se) {
         savevm_state_handler_remove(se);
+        g_free(se->compat);
+        g_free(se);
     }
     return vmstate_register(obj, instance_id, vmsd, opaque);
 }
Signature · 1 line
2.45.2
#4 lore raw

PATCH v3 4/9 memory: Do not refer to "memory region's reference count"

In reply to Akihiko Odaki

Now MemoryRegions do have their own reference counts, but they will not be used when their owners are not themselves. However, the documentation of memory_region_ref() says it adds "1 to a memory region's reference count", which is confusing. Avoid referring to "memory region's reference count" and just say: "Add a reference to a memory region". Make a similar change to memory_region_unref() too.

---
 include/exec/memory.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/exec/memory.h +2 -2
index c26ede33d21e..a5af05864274 100644
@@ -1243,7 +1243,7 @@ void memory_region_init(MemoryRegion *mr,
                         uint64_t size);
 
 /**
- * memory_region_ref: Add 1 to a memory region's reference count
+ * memory_region_ref: Add a reference to a memory region
  *
  * Whenever memory regions are accessed outside the BQL, they need to be
  * preserved against hot-unplug.  MemoryRegions actually do not have their
@@ -1260,7 +1260,7 @@ void memory_region_init(MemoryRegion *mr,
 void memory_region_ref(MemoryRegion *mr);
 
 /**
- * memory_region_unref: Remove 1 to a memory region's reference count
+ * memory_region_unref: Remove a reference to a memory region
  *
  * Whenever memory regions are accessed outside the BQL, they need to be
  * preserved against hot-unplug.  MemoryRegions actually do not have their
Signature · 1 line
2.45.2
#5 lore raw

PATCH v3 5/9 memory: Refer to docs/devel/memory.rst for "owner"

In reply to Akihiko Odaki

memory_region_ref() and memory_region_unref() used to have their own descriptions of "owner", but they are somewhat out-of-date and misleading.

In particular, they say "whenever memory regions are accessed outside the BQL, they need to be preserved against hot-unplug", but protecting against hot-unplug is not mandatory if it is known that they will never be hot-unplugged. They also say "MemoryRegions actually do not have their own reference count", but they actually do. They just will not be used unless their owners are not themselves.

Refer to docs/devel/memory.rst as the single source of truth instead of maintaining duplicate descriptions of "owner".

---
 include/exec/memory.h | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
include/exec/memory.h +2 -11
index a5af05864274..e1bd29550c15 100644
@@ -1245,15 +1245,8 @@ void memory_region_init(MemoryRegion *mr,
 /**
  * memory_region_ref: Add a reference to a memory region
  *
- * Whenever memory regions are accessed outside the BQL, they need to be
- * preserved against hot-unplug.  MemoryRegions actually do not have their
- * own reference count; they piggyback on a QOM object, their "owner".
  * This function adds a reference to the owner.
- *
- * All MemoryRegions must have an owner if they can disappear, even if the
- * device they belong to operates exclusively under the BQL.  This is because
- * the region could be returned at any time by memory_region_find, and this
- * is usually under guest control.
+ * See docs/devel/memory.rst to know about owner.
  *
  * @mr: the #MemoryRegion
  */
@@ -1262,10 +1255,8 @@ void memory_region_ref(MemoryRegion *mr);
 /**
  * memory_region_unref: Remove a reference to a memory region
  *
- * Whenever memory regions are accessed outside the BQL, they need to be
- * preserved against hot-unplug.  MemoryRegions actually do not have their
- * own reference count; they piggyback on a QOM object, their "owner".
  * This function removes a reference to the owner and possibly destroys it.
+ * See docs/devel/memory.rst to know about owner.
  *
  * @mr: the #MemoryRegion
  */
Signature · 1 line
2.45.2
#6 lore raw

PATCH v3 6/9 memory: Clarify that owner may be missing

In reply to Akihiko Odaki

A memory region may not have an owner, and memory_region_ref() and memory_region_unref() do nothing for such.

---
 include/exec/memory.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/exec/memory.h +3 -3
index e1bd29550c15..847c84c86db0 100644
@@ -1245,7 +1245,7 @@ void memory_region_init(MemoryRegion *mr,
 /**
  * memory_region_ref: Add a reference to a memory region
  *
- * This function adds a reference to the owner.
+ * This function adds a reference to the owner if present.
  * See docs/devel/memory.rst to know about owner.
  *
  * @mr: the #MemoryRegion
@@ -1255,8 +1255,8 @@ void memory_region_ref(MemoryRegion *mr);
 /**
  * memory_region_unref: Remove a reference to a memory region
  *
- * This function removes a reference to the owner and possibly destroys it.
- * See docs/devel/memory.rst to know about owner.
+ * This function removes a reference to the owner and possibly destroys it if
+ * present. See docs/devel/memory.rst to know about owner.
  *
  * @mr: the #MemoryRegion
  */
Signature · 1 line
2.45.2
#7 lore raw

PATCH v3 7/9 memory: Clarify owner must not call memory_region_ref()

In reply to Akihiko Odaki
---
 include/exec/memory.h | 1 +
 1 file changed, 1 insertion(+)
include/exec/memory.h +1 -0
index 847c84c86db0..32bb430acdc4 100644
@@ -1246,6 +1246,7 @@ void memory_region_init(MemoryRegion *mr,
  * memory_region_ref: Add a reference to a memory region
  *
  * This function adds a reference to the owner if present.
+ * The owner must not call this function as it results in a circular reference.
  * See docs/devel/memory.rst to know about owner.
  *
  * @mr: the #MemoryRegion
Signature · 1 line
2.45.2
#8 lore raw

PATCH v3 8/9 memory: Do not create circular reference with subregion

In reply to Akihiko Odaki

memory_region_update_container_subregions() calls memory_region_ref() on behalf of the owner of the container. memory_region_ref() must not be called if the owner of the container also owns the subregion.

---
 system/memory.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
system/memory.c +9 -2
index 2d6952136066..09c98042e443 100644
@@ -2625,7 +2625,10 @@ static void memory_region_update_container_subregions(MemoryRegion *subregion)
 
     memory_region_transaction_begin();
 
-    memory_region_ref(subregion);
+    if (mr->owner != subregion->owner) {
+        memory_region_ref(subregion);
+    }
+
     QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
         if (subregion->priority >= other->priority) {
             QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
@@ -2683,7 +2686,11 @@ void memory_region_del_subregion(MemoryRegion *mr,
         assert(alias->mapped_via_alias >= 0);
     }
     QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
-    memory_region_unref(subregion);
+
+    if (mr->owner != subregion->owner) {
+        memory_region_unref(subregion);
+    }
+
     memory_region_update_pending |= mr->enabled && subregion->enabled;
     memory_region_transaction_commit();
 }
Signature · 1 line
2.45.2
#9 lore raw

PATCH v3 9/9 tests/qtest: Delete previous boot file

In reply to Akihiko Odaki

A test run may create boot files several times. Delete the previous boot file before creating a new one.

---
 tests/qtest/migration-test.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
tests/qtest/migration-test.c +11 -7
index 70b606b88864..6c06100d91e2 100644
@@ -144,12 +144,23 @@ static char *bootpath;
 #include "tests/migration/ppc64/a-b-kernel.h"
 #include "tests/migration/s390x/a-b-bios.h"
 
+static void bootfile_delete(void)
+{
+    unlink(bootpath);
+    g_free(bootpath);
+    bootpath = NULL;
+}
+
 static void bootfile_create(char *dir, bool suspend_me)
 {
     const char *arch = qtest_get_arch();
     unsigned char *content;
     size_t len;
 
+    if (bootpath) {
+        bootfile_delete();
+    }
+
     bootpath = g_strdup_printf("%s/bootsect", dir);
     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
         /* the assembled x86 boot sector should be exactly one sector large */
@@ -177,13 +188,6 @@ static void bootfile_create(char *dir, bool suspend_me)
     fclose(bootfile);
 }
 
-static void bootfile_delete(void)
-{
-    unlink(bootpath);
-    g_free(bootpath);
-    bootpath = NULL;
-}
-
 /*
  * Wait for some output in the serial output file,
  * we get an 'A' followed by an endless string of 'B's
Signature · 1 line
2.45.2
Acked-by on v3 9/9
#10 lore raw

Re: [PATCH v3 9/9] tests/qtest: Delete previous boot file

Quoted 8 lines · Akihiko Odaki

A test run may create boot files several times. Delete the previous boot file before creating a new one.

---
  tests/qtest/migration-test.c | 18 +++++++++++-------
  1 file changed, 11 insertions(+), 7 deletions(-)