Skip to contentSkip to conversation

Fix check-qtest-ppc64 sanitizer errors

PATCH Superseded Akihiko Odaki 2 participants 5 messages Jan 4, 2025 – Jan 5, 2025

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v5 cover Fix check-qtest-ppc64 sanitizer errors

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 versions older than 2.81.0 do 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 v5:
- Rebased.
- Merged four patches to update inline documentation into one
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Changed to create a reference to the subregion instead of its owner when its owner equals to the container's owner.
- Dropped R-b from patch "memory: Do not create circular reference with subregion".
- Rebased.
- Link to v3: https://lore.kernel.org/r/[email protected]

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 (2):
memory: Update inline documentation
memory: Do not create circular reference with subregion

 include/exec/memory.h | 22 +++++++---------------
 system/memory.c       |  8 ++++++--
 2 files changed, 13 insertions(+), 17 deletions(-)

---
base-commit: 38d0939b86e2eef6f6a622c6f1f7befda0146595
change-id: 20240625-san-097afaf4f1c2

Best regards,

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

PATCH v5 1/2 memory: Update inline documentation

Do not refer to "memory region's reference count"
-------------------------------------------------

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.

Refer to docs/devel/memory.rst for "owner"
------------------------------------------

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".

Clarify that owner may be missing

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

memory: Clarify owner must not call memory_region_ref()
--------------------------------------------------------

The owner must not call this function as it results in a circular reference.

---
 include/exec/memory.h | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)
include/exec/memory.h +7 -15
index 9458e2801d50..cd91fe0c51cf 100644
@@ -1220,29 +1220,21 @@ 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
- * 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.
+ * 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
  */
 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
- * own reference count; they piggyback on a QOM object, their "owner".
- * This function removes a reference to the owner and possibly destroys it.
+ * 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.47.1
#2 lore raw

Re: [PATCH v5 1/2] memory: Update inline documentation

Quoted 24 lines · Akihiko Odaki

Do not refer to "memory region's reference count"
-------------------------------------------------

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.

Refer to docs/devel/memory.rst for "owner"
------------------------------------------

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.

Quoted 8 lines

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

Clarify that owner may be missing

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

The commit message is longer than the documentation it changes :-) That probably means the docs could be more detailed. For example this relation to the owner may be mentioned unless it's something to be changed in the future to clean this up.

Quoted 22 lines

memory: Clarify owner must not call memory_region_ref()
--------------------------------------------------------

The owner must not call this function as it results in a circular reference.

---
include/exec/memory.h | 22 +++++++---------------

1 file changed, 7 insertions(+), 15 deletions(-)
include/exec/memory.h +1 -1
index 9458e2801d50..cd91fe0c51cf 100644
@@ -1220,29 +1220,21 @@ 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
 *

include/exec/memory.h void memory_region_init(MemoryRegion *mr, · line 1225

Quoted 10 lines
include/exec/memory.h +1 -9
- * 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.
+ * This function adds a reference to the owner if present.

Maybe it's just not clear to me but the title says "Add a reference to a memory region" then here it says "adds a reference to the owner" and does not say what happens if there's no owner present. Maybe it's better to be explicit and say add 1 to the owner's ref count or do nothing if owner is not present.

Quoted 7 lines
include/exec/memory.h +2 -0
+ * 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
 */
 void memory_region_ref(MemoryRegion *mr);
 

include/exec/memory.h void memory_region_init(MemoryRegion *mr, · line 1237

Quoted 10 lines
include/exec/memory.h +3 -5
 /**
- * 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
- * own reference count; they piggyback on a QOM object, their "owner".
- * This function removes a reference to the owner and possibly destroys it.
+ * This function removes a reference to the owner and possibly destroys it if
+ * present. See docs/devel/memory.rst to know about owner.

In "destroys it if present" it's not clear what "it" refers to as it can either be the memory region or the owner. I guess it's the owner but better state that to avoid confusion.

Regards,
BALATON Zoltan

Quoted 8 lines
include/exec/memory.h +0 -0
 *
 * @mr: the #MemoryRegion
 */
Signature · 2 lines
-- 
2.47.1
#3 lore raw

Re: [PATCH v5 1/2] memory: Update inline documentation

Quoted 28 lines · BALATON Zoltan
Quoted 27 lines · Akihiko Odaki

Do not refer to "memory region's reference count"
-------------------------------------------------

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.

Refer to docs/devel/memory.rst for "owner"
------------------------------------------

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".

Quoted 7 lines
Quoted 5 lines

Clarify that owner may be missing

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

The commit message is longer than the documentation it changes :-) That probably means the docs could be more detailed. For example this relation to the owner may be mentioned unless it's something to be changed in the future to clean this up.

It's odd that the commit message is longer but there are a few reasons:
1. It describes why the old documentation is wrong. Some of such statements are replaced with the reference to docs/devel/memory.rst to make them shorter.

2. It contains information that is already described in the existing documentation for context.

In this particular case, the owner is already described in docs/devel/memory.rst extensively.

Quoted 31 lines
Quoted 31 lines

memory: Clarify owner must not call memory_region_ref()
--------------------------------------------------------

The owner must not call this function as it results in a circular reference.

---
include/exec/memory.h | 22 +++++++---------------

1 file changed, 7 insertions(+), 15 deletions(-)
include/exec/memory.h +1 -3
index 9458e2801d50..cd91fe0c51cf 100644
@@ -1220,29 +1220,21 @@ 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
- * 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

Quoted 8 lines
Quoted 6 lines

- * 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.
+ * This function adds a reference to the owner if present.

Maybe it's just not clear to me but the title says "Add a reference to a memory region" then here it says "adds a reference to the owner" and does not say what happens if there's no owner present. Maybe it's better to be explicit and say add 1 to the owner's ref count or do nothing if owner is not present.

I wrote the title looking at other comments that describes the owner as "the object that tracks the region's reference count", which implies the region's reference count is part of the owner.

But it is confusing to to describe the owner as "the object that tracks the region's reference count" in the first place. A memory region has its own reference count that can be used to express references internal to the owner (and actually used in the following patch).

With v6 I have just sent, I changed to describe the owner as "the object that keeps the region alive". The title of this function now says "add a reference to the owner of a memory region".

I replaced "add 1 to ~'s reference count" with "add a reference to" to make in concise (to fit into 80 columns in particular). I think it's fine not to mention the use of counter as it is an implementation detail users don't have to care.

With v6, I also noted that this function does nothing when the owner is not present in the comment body as suggested.

Quoted 23 lines
Quoted 9 lines

+ * 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
 */
void memory_region_ref(MemoryRegion *mr);

Quoted 13 lines

/**
- * 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
- * own reference count; they piggyback on a QOM object, their "owner".
- * This function removes a reference to the owner and possibly destroys it.
+ * This function removes a reference to the owner and possibly destroys it if
+ * present. See docs/devel/memory.rst to know about owner.

Quoted 1 line

In "destroys it if present" it's not clear what "it" refers to as it can either be the memory region or the owner. I guess it's the owner but better state that to avoid confusion.

I modified the comment to tell that this function may destroy the owner along with the memory region in v6.

Regards,
Akihiko Odaki

#4 lore raw

PATCH v5 2/2 memory: Do not create circular reference with subregion

In reply to Akihiko Odaki

memory_region_update_container_subregions() used to call memory_region_ref(), which creates a reference to the owner of the subregion, on behalf of the owner of the container. This results in a circular reference if the subregion and container have the same owner.

memory_region_ref() creates a reference to the owner instead of the memory region to match the lifetime of the owner and memory region. We do not need such a hack if the subregion and container have the same owner because the owner will be alive as long as the container is. Therefore, create a reference to the subregion itself instead ot its owner in such a case; the reference to the subregion is still necessary to ensure that the subregion gets finalized after the container.

---
 system/memory.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
system/memory.c +6 -2
index 78e17e0efa83..16b051249c5f 100644
@@ -2644,7 +2644,9 @@ static void memory_region_update_container_subregions(MemoryRegion *subregion)
 
     memory_region_transaction_begin();
 
-    memory_region_ref(subregion);
+    object_ref(mr->owner == subregion->owner ?
+               OBJECT(subregion) : subregion->owner);
+
     QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
         if (subregion->priority >= other->priority) {
             QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
@@ -2702,7 +2704,9 @@ void memory_region_del_subregion(MemoryRegion *mr,
         assert(alias->mapped_via_alias >= 0);
     }
     QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
-    memory_region_unref(subregion);
+    object_unref(mr->owner == subregion->owner ?
+                 OBJECT(subregion) : subregion->owner);
+
     memory_region_update_pending |= mr->enabled && subregion->enabled;
     memory_region_transaction_commit();
 }
Signature · 1 line
2.47.1