hw/s390x: Fix memory leaks after removing elements with QTAILQ_REMOVE
PATCH 1/1 hw/s390x: Fix memory leaks after removing elements with QTAILQ_REMOVE
From: Thomas Huth <[email protected]>
The elements that get removed with QTAILQ_REMOVE are never referenced afterwards anymore, so the corresponding memory should get freed.
--- hw/s390x/s390-pci-bus.c | 1 + hw/s390x/s390-pci-vfio.c | 1 + 2 files changed, 2 insertions(+)
hw/s390x/s390-pci-bus.c +1 -0
@@ -911,6 +911,7 @@ static void s390_pcihost_unrealize(DeviceState *dev)while (!QTAILQ_EMPTY (&s->zpci_groups)) { group =QTAILQ_FIRST (&s->zpci_groups);QTAILQ_REMOVE (&s->zpci_groups, group, link); +g_free (group); } }
hw/s390x/s390-pci-vfio.c +1 -0
@@ -101,6 +101,7 @@ void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt) cnt->users--;if (cnt->users ==0 ) {QTAILQ_REMOVE (&s->zpci_dma_limit, cnt, link); +g_free (cnt); } }
Signature · 1 line
2.54.0
Quoted 11 lines · Thomas Huth
From: Thomas Huth <[email protected]>
The elements that get removed with
QTAILQ_REMOVEare never referenced afterwards anymore, so the corresponding memory should get freed.Signed-off-by Thomas Huth <[email protected]>--- hw/s390x/s390-pci-bus.c | 1 + hw/s390x/s390-pci-vfio.c | 1 + 2 files changed, 2 insertions(+)
Thanks Thomas! Code looks good and I did some regression testing with this patch applied:
Should we also consider adding:
? Neither are commonly-driven paths, but s390_pci_end_dma_count can be
driven N times via unplug of N vfio-pci devices.
Also out of curiosity, did you just stumble on this or was there some tooling or testing that was being employed here that found this?
Thanks,
Matt
Quoted 16 lines · Matthew Rosato
Quoted 11 lines · Thomas Huth
From: Thomas Huth <[email protected]>
The elements that get removed with
QTAILQ_REMOVEare never referenced afterwards anymore, so the corresponding memory should get freed.Signed-off-by Thomas Huth <[email protected]>--- hw/s390x/s390-pci-bus.c | 1 + hw/s390x/s390-pci-vfio.c | 1 + 2 files changed, 2 insertions(+)Thanks Thomas! Code looks good and I did some regression testing with this patch applied:
Quoted 7 lines
Reviewed-by Matthew Rosato <[email protected]>Tested-by Matthew Rosato <[email protected]>Should we also consider adding:
Fixes b354d5d804 ("s390x/pci: clean up s390 PCI groups")Fixes 37fa32de70 ("s390x/pci: Honor DMA limits set by vfio")
Yes, that makes sense.
Actually, looking at the commit description of b354d5d804, I think this code has been added with a wrong assumption: The unrealize method is not called during system reset. So to free the memory, this should likely be added to a reset handler instead? Could you maybe have a look?
Quoted 2 lines
Also out of curiosity, did you just stumble on this or was there some tooling or testing that was being employed here that found this?
I asked Claude Code to find bugs in the QEMU sources, and this was something that looked reasonable to me. (I only used Claude to find bugs, the patch has been 100% written by myself of course)
Thomas
Quoted 22 lines · Thomas Huth
Quoted 21 lines · Matthew Rosato
Quoted 11 lines · Thomas Huth
From: Thomas Huth <[email protected]>
The elements that get removed with
QTAILQ_REMOVEare never referenced afterwards anymore, so the corresponding memory should get freed.Signed-off-by Thomas Huth <[email protected]>---
hw/s390x/s390-pci-bus.c | 1 +
hw/s390x/s390-pci-vfio.c| 1 +2 files changed, 2 insertions(+)Thanks Thomas! Code looks good and I did some regression testing with this patch applied:
Reviewed-by Matthew Rosato <[email protected]>Tested-by Matthew Rosato <[email protected]>Should we also consider adding:
Quoted 8 lines
Quoted 2 lines
Fixes b354d5d804 ("s390x/pci: clean up s390 PCI groups")Fixes 37fa32de70 ("s390x/pci: Honor DMA limits set by vfio")Yes, that makes sense.
Actually, looking at the commit description of
b354d5d804, I think this code has been added with a wrong assumption: The unrealize method is not called during system reset.
So, yes, you're right that this isn't doing what was intended per the commit message...
Quoted 2 lines
So to free the memory, this should likely be added to a reset handler instead? Could you maybe have a look?
But I also don't think that we really want to arbitrarily clear this list on a machine reset.
As written, it's only OK to free this list arbitrarily if we are 1) ending the process anyway or 2) can expect another call to s390_pcihost_realize() + device plug (s390_pcihost_plug) events for all devices in order to re-establish new group pointers.
When we do, say, a reboot or kexec in the guest, because the associated devices did not go through an unplug they continue to use the reference to their pci_group which should be OK -- the underlying vfio device will still give the same group data post-reset. (Or, if it's an emulated device, it will today always use the same QEMU-defined group)
I think the only issue is that when we do actually unplug vfio-pci devices, we can potentially orphan groups in the list that are no longer used. Fortunately there is an upper-limit of 256 entries; while the S390PCIGroup supports a full int, s390-pci-vfio.c will base all group IDs from a uint8_t.
But it does leave a theoretical issue of what if the device is also unplugged from the host, and a new device later comes along that re-uses the host group ID with new group info, and then you pass that device through to this guest.
So: I wonder if the proper thing to do is to refcount the groups during plug and remove them from the list during unplug when refcount hits 0 (and of course, add the missing g_free here) -- with special handling for the default group that should never go away.
Which is probably more than you signed on for with this patch ;) If you'd like, I can instead sort this out and give you a reported-by?
Meanwhile you could still split out the fix to s390_pci_end_dma_count() and send that one again by itself (w/ Fixes: 37fa32de70)
Thanks,
Matt
Quoted 7 lines · Matthew Rosato
Quoted 6 lines · Thomas Huth
Quoted 5 lines · Matthew Rosato
Quoted 4 lines · Thomas Huth
From: Thomas Huth <[email protected]>
The elements that get removed with
QTAILQ_REMOVEare never referenced afterwards anymore, so the corresponding memory should get freed.
...
Quoted 16 lines
Quoted 16 lines
Quoted 10 lines
Thanks Thomas! Code looks good and I did some regression testing with this patch applied:
Reviewed-by Matthew Rosato <[email protected]>Tested-by Matthew Rosato <[email protected]>Should we also consider adding:
Fixes b354d5d804 ("s390x/pci: clean up s390 PCI groups")Fixes 37fa32de70 ("s390x/pci: Honor DMA limits set by vfio")Yes, that makes sense.
Actually, looking at the commit description of
b354d5d804, I think this code has been added with a wrong assumption: The unrealize method is not called during system reset.
...
Quoted 7 lines
So: I wonder if the proper thing to do is to refcount the groups during plug and remove them from the list during unplug when refcount hits 0 (and of course, add the missing
g_freehere) -- with special handling for the default group that should never go away.Which is probably more than you signed on for with this patch ;) If you'd like, I can instead sort this out and give you a reported-by?
Yes, sounds good!
Quoted 2 lines
Meanwhile you could still split out the fix to
s390_pci_end_dma_count()and send that one again by itself (w/ Fixes:37fa32de70)
Sure, I'll send a v2 that contains only the other change.
Thomas
Quoted 28 lines · Thomas Huth
From: Thomas Huth <[email protected]>
The elements that get removed with
QTAILQ_REMOVEare never referenced afterwards anymore, so the corresponding memory should get freed.Signed-off-by Thomas Huth <[email protected]>--- hw/s390x/s390-pci-bus.c | 1 + hw/s390x/s390-pci-vfio.c | 1 + 2 files changed, 2 insertions(+)hw/s390x/s390-pci-bus.c +1
-0@@ -911,6 +911,7 @@ static void s390_pcihost_unrealize(DeviceState *dev)while (!QTAILQ_EMPTY (&s->zpci_groups)) { group =QTAILQ_FIRST (&s->zpci_groups);QTAILQ_REMOVE (&s->zpci_groups, group, link); +g_free (group); } }hw/s390x/s390-pci-vfio.c +0
-0
hw/s390x/s390-pci-vfio.c void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt) · line 107
Quoted 7 lines
hw/s390x/s390-pci-vfio.c +1
-0@@ -101,6 +101,7 @@ void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt) cnt->users--;if (cnt->users ==0 ) {QTAILQ_REMOVE (&s->zpci_dma_limit, cnt, link); +g_free (cnt); } }