[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 24/47] memory: do explicit cleanup when remove listen
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PULL 24/47] memory: do explicit cleanup when remove listeners |
Date: |
Mon, 5 Feb 2018 20:28:38 +0100 |
From: Peter Xu <address@hidden>
When unregister memory listeners, we should call, e.g.,
region_del() (and possibly other undo operations) on every existing
memory region sections there, otherwise we may leak resources that are
held during the region_add(). This patch undo the stuff for the
listeners, which emulates the case when the address space is set from
current to an empty state.
I found this problem when debugging a refcount leak issue that leads to
a device unplug event lost (please see the "Bug:" line below). In that
case, the leakage of resource is the PCI BAR memory region refcount.
And since memory regions are not keeping their own refcount but onto
their owners, so the vfio-pci device's (who is the owner of the PCI BAR
memory regions) refcount is leaked, and event missing.
We had encountered similar issues before and fixed in other
way (ee4c112846, "vhost: Release memory references on cleanup"). This
patch can be seen as a more high-level fix of similar problems that are
caused by the resource leaks from memory listeners. So now we can remove
the explicit unref of memory regions since that'll be done altogether
during unregistering of listeners now.
Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1531393
Signed-off-by: Peter Xu <address@hidden>
Message-Id: <address@hidden>
Reviewed-by: Paolo Bonzini <address@hidden>
Signed-off-by: Paolo Bonzini <address@hidden>
---
hw/virtio/vhost.c | 4 ----
memory.c | 27 +++++++++++++++++++++++++++
2 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
index c4f654c..d16c0c8 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -1368,10 +1368,6 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
if (hdev->mem) {
/* those are only safe after successful init */
memory_listener_unregister(&hdev->memory_listener);
- for (i = 0; i < hdev->n_mem_sections; ++i) {
- MemoryRegionSection *section = &hdev->mem_sections[i];
- memory_region_unref(section->mr);
- }
QLIST_REMOVE(hdev, entry);
}
if (hdev->migration_blocker) {
diff --git a/memory.c b/memory.c
index 9e83496..5686698 100644
--- a/memory.c
+++ b/memory.c
@@ -2612,6 +2612,32 @@ static void listener_add_address_space(MemoryListener
*listener,
flatview_unref(view);
}
+static void listener_del_address_space(MemoryListener *listener,
+ AddressSpace *as)
+{
+ FlatView *view;
+ FlatRange *fr;
+
+ if (listener->begin) {
+ listener->begin(listener);
+ }
+ view = address_space_get_flatview(as);
+ FOR_EACH_FLAT_RANGE(fr, view) {
+ MemoryRegionSection section = section_from_flat_range(fr, view);
+
+ if (fr->dirty_log_mask && listener->log_stop) {
+ listener->log_stop(listener, §ion, fr->dirty_log_mask, 0);
+ }
+ if (listener->region_del) {
+ listener->region_del(listener, §ion);
+ }
+ }
+ if (listener->commit) {
+ listener->commit(listener);
+ }
+ flatview_unref(view);
+}
+
void memory_listener_register(MemoryListener *listener, AddressSpace *as)
{
MemoryListener *other = NULL;
@@ -2652,6 +2678,7 @@ void memory_listener_unregister(MemoryListener *listener)
return;
}
+ listener_del_address_space(listener, listener->address_space);
QTAILQ_REMOVE(&memory_listeners, listener, link);
QTAILQ_REMOVE(&listener->address_space->listeners, listener, link_as);
listener->address_space = NULL;
--
1.8.3.1
- [Qemu-devel] [PULL 15/47] hvf: ept_emulation_fault() needs NetApp BSD attribution, (continued)
- [Qemu-devel] [PULL 15/47] hvf: ept_emulation_fault() needs NetApp BSD attribution, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 18/47] exynos4210: workaround UBSAN compilation error, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 16/47] Drop remaining bits of ia64 host support, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 19/47] build-sys: add --enable-sanitizers, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 21/47] vhost: add traces for memory listeners, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 20/47] ucontext: annotate coroutine stack for ASAN, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 17/47] sockets: fix parsing of ipv4/ipv6 opts in parse_socket_addr, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 23/47] vfio: listener unregister before unset container, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 22/47] arm: postpone device listener unregister, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 25/47] chardev/char-socket: add POLLHUP handler, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 24/47] memory: do explicit cleanup when remove listeners,
Paolo Bonzini <=
- [Qemu-devel] [PULL 28/47] ivshmem: Improve MSI irqfd error handling, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 29/47] ivshmem: Disable irqfd on device reset, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 27/47] ivshmem: Always remove irqfd notifiers, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 32/47] cpus: dummy: unregister thread with RCU, exit loop on unplug, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 30/47] cpus: hax: register/unregister thread with RCU, exit loop on unplug, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 31/47] cpus: kvm: unregister thread with RCU, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 26/47] ivshmem: Don't update non-existent MSI routes, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 35/47] cpus: join thread when removing a vCPU, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 34/47] cpus: hvf: unregister thread with RCU, Paolo Bonzini, 2018/02/05
- [Qemu-devel] [PULL 33/47] cpus: tcg: unregister thread with RCU, fix exiting of loop on unplug, Paolo Bonzini, 2018/02/05