hw/usb/hcd-ohci: Fix memory leak in ohci_service_iso_td()
PATCH 1/1 hw/usb/hcd-ohci: Fix memory leak in ohci_service_iso_td()
From: heropd <[email protected]>
Fix a memory leak in ohci_service_iso_td() by calling usb_packet_cleanup() before freeing USBPacket.
--- hw/usb/hcd-ohci.c | 2 ++ 1 file changed, 2 insertions(+)
hw/usb/hcd-ohci.c +2 -0
@@ -748,6 +748,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed)usb_handle_packet (dev, pkt);if (pkt->status ==USB_RET_ASYNC ) {usb_device_flush_ep_queue (dev, ep); +usb_packet_cleanup (pkt);g_free (pkt);return 1 ; }
@@ -756,6 +757,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) }else { ret = pkt->status; } +usb_packet_cleanup (pkt);g_free (pkt); trace_usb_ohci_iso_td_so(start_offset, end_offset, start_addr, end_addr,
Signature · 1 line
2.50.1 (Apple Git-155)
+BALATON Zoltan
Quoted 7 lines · On Tue, May 5, 2026 at 3:01 PM <[email protected]> wrote:
From: heropd <[email protected]>
Fix a memory leak in
ohci_service_iso_td()by callingusb_packet_cleanup()before freeing USBPacket.Signed-off-by Munkhbaatar Enkhbaatar <[email protected]>
Quoted 28 lines
--- hw/usb/hcd-ohci.c | 2 ++ 1 file changed, 2 insertions(+)hw/usb/hcd-ohci.c +2
-1@@ -748,6 +748,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed)usb_handle_packet (dev, pkt);if (pkt->status ==USB_RET_ASYNC ) {usb_device_flush_ep_queue (dev, ep); +usb_packet_cleanup (pkt);g_free (pkt);return 1 ; }@@ -756,6 +757,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) }else { ret = pkt->status; } +usb_packet_cleanup (pkt);g_free (pkt); trace_usb_ohci_iso_td_so(start_offset, end_offset, start_addr, end_addr, --2.50.1 (Apple Git-155)
Signature · 1 line
Marc-André Lureau
Quoted 7 lines · On Tue, 5 May 2026 at 12:01, <[email protected]> wrote:
From: heropd <[email protected]>
Fix a memory leak in
ohci_service_iso_td()by callingusb_packet_cleanup()before freeing USBPacket.Signed-off-by Munkhbaatar Enkhbaatar <[email protected]>
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3463
Quoted 9 lines
--- hw/usb/hcd-ohci.c | 2 ++ 1 file changed, 2 insertions(+)hw/usb/hcd-ohci.c +0
-0
hw/usb/hcd-ohci.c static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) · line 754
Quoted 7 lines
hw/usb/hcd-ohci.c +1
-0@@ -748,6 +748,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed)usb_handle_packet (dev, pkt);if (pkt->status ==USB_RET_ASYNC ) {usb_device_flush_ep_queue (dev, ep); +usb_packet_cleanup (pkt);g_free (pkt);return 1 ; }
I don't really understand the USB subsystem, but something seems odd here. The case when usb_handle_packet() can set the p->status to USB_RET_ASYNC is when it has added it to a queue, either directly ("QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);") or by calling usb_queue_one(). The usb_device_flush_ep_queue() does not (as far as I can tell) remove the queued packet. So it seems wrong to me that this code path frees the packet, and also if we call usb_packet_cleanup() it will do
assert(!usb_packet_is_inflight(p));
which I think will fire.
It's possible that we can't ever actually get to this code -- the usb_handle_packet() has a claim "hcd drivers cannot handle async for isoc" and some assertions about that, which suggests maybe that the intent is that we won't ever return USB_RET_ASYNC for an iso TD. But if so, we should be asserting that.
hw/usb/hcd-ohci.c static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) · line 761
Quoted 6 lines
hw/usb/hcd-ohci.c +1
-0@@ -756,6 +757,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) }else { ret = pkt->status; } +usb_packet_cleanup (pkt);g_free (pkt);
This one's probably OK -- is it the one that fixes the repro case in the bug report?
thanks
-- PMM
Quoted 44 lines · Peter Maydell
Quoted 7 lines · On Tue, 5 May 2026 at 12:01, <[email protected]…
From: heropd <[email protected] <mailto:[email protected]>>
Fix a memory leak in
ohci_service_iso_td()by callingusb_packet_cleanup()before freeing USBPacket.Signed-off-by Munkhbaatar Enkhbaatar <[email protected] <mailto:[email protected]>>Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3463
Quoted 9 lines
---
hw/usb/hcd-ohci.c| 2 ++1 file changed, 2 insertions(+)hw/usb/hcd-ohci.c +0
-0hw/usb/hcd-ohci.c static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) · line 754
Quoted 7 lines
hw/usb/hcd-ohci.c +1
-0@@ -748,6 +748,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed)usb_handle_packet (dev, pkt);if (pkt->status ==USB_RET_ASYNC ) {usb_device_flush_ep_queue (dev, ep); +usb_packet_cleanup (pkt);g_free (pkt);return 1 ; }I don't really understand the USB subsystem, but something seems odd here. The case when
usb_handle_packet()can set thep->statustoUSB_RET_ASYNCis when it has added it to a queue, either directly ("QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);") or by callingusb_queue_one(). Theusb_device_flush_ep_queue()does not (as far as I can tell) remove the queued packet. So it seems wrong to me that this code path frees the packet, and also if we callusb_packet_cleanup()it will do
assert(!usb_packet_is_inflight(p));
which I think will fire.
Quoted 6 lines
It's possible that we can't ever actually get to this code -- the
usb_handle_packet()has a claim "hcd drivers cannot handle async for isoc" and some assertions about that, which suggests maybe that the intent is that we won't ever returnUSB_RET_ASYNCfor an iso TD. But if so, we should be asserting that.
Thanks that makes sense,
I tested the USB_RET_ASYNC path locally and usb_packet_cleanup() does indeed hit the inflight assertion there
OHCI ISO ASYNC branch reached
Assertion failed: (!usb_packet_is_inflight(p)), function usb_packet_cleanup, file core.c, line 657.
Quoted 9 lines
hw/usb/hcd-ohci.c static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) · line 761
Quoted 6 lines
hw/usb/hcd-ohci.c +1
-0@@ -756,6 +757,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) }else { ret = pkt->status; } +usb_packet_cleanup (pkt);g_free (pkt);This one's probably OK -- is it the one that fixes the repro case in the bug report?
Yes this one should fix repro bug in report
Quoted 3 lines
thanks
-- PMM
For the async path, I can leave it unchanged for this fix, or add an assertion since ISO TDs are not expected to complete asynchronously.
Thanks,
Munkhbaatar
Quoted 73 lines · Мөнхбаатар Энхбаатар
On 5 May 2026, at 21:14, Peter Maydell <[email protected]> wrote:
On Tue, 5 May 2026 at 12:01, <[email protected]> wrote:
From: heropd <[email protected]>
Fix a memory leak in
ohci_service_iso_td()by callingusb_packet_cleanup()before freeing USBPacket.Signed-off-by Munkhbaatar Enkhbaatar <[email protected]>Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3463
---
hw/usb/hcd-ohci.c| 2 ++1 file changed, 2 insertions(+)hw/usb/hcd-ohci.c +1
-0@@ -748,6 +748,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed)usb_handle_packet (dev, pkt);if (pkt->status ==USB_RET_ASYNC ) {usb_device_flush_ep_queue (dev, ep); +usb_packet_cleanup (pkt);g_free (pkt);return 1 ; }I don't really understand the USB subsystem, but something seems odd here. The case when
usb_handle_packet()can set thep->statustoUSB_RET_ASYNCis when it has added it to a queue, either directly ("QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);") or by callingusb_queue_one(). Theusb_device_flush_ep_queue()does not (as far as I can tell) remove the queued packet. So it seems wrong to me that this code path frees the packet, and also if we callusb_packet_cleanup()it will do
assert(!usb_packet_is_inflight(p));
which I think will fire.It's possible that we can't ever actually get to this code -- the
usb_handle_packet()has a claim "hcd drivers cannot handle async for isoc" and some assertions about that, which suggests maybe that the intent is that we won't ever returnUSB_RET_ASYNCfor an iso TD. But if so, we should be asserting that.Thanks that makes sense,
I tested theUSB_RET_ASYNCpath locally andusb_packet_cleanup()does indeed hit the inflight assertion there
OHCI ISO ASYNC branch reached
Assertion failed: (!usb_packet_is_inflight(p)), functionusb_packet_cleanup, filecore.c, line 657.hw/usb/hcd-ohci.c +1
-0@@ -756,6 +757,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) }else { ret = pkt->status; } +usb_packet_cleanup (pkt);g_free (pkt);This one's probably OK -- is it the one that fixes the repro case in the bug report?
Quoted 4 lines
Yes this one should fix repro bug in report
For the async path, I can leave it unchanged for this fix, or add an assertion since ISO TDs are not expected to complete asynchronously.
I think we should do this as two patches:
(1) change the "if (pkt->status == USB_RET_ASYNC) { ... }" to something like:
/* The USB core guarantees to never defer ISO TDs */ assert(pkt->status != USB_RET_ASYNC);
(2) fix the leak with the usb_packet_cleanup() call that's currently the second hunk in this patch
That way if I'm wrong about the USB subsystem's guarantees we'll be able to just revert patch 1 without undoing the fix for the leak.
thanks
-- PMM
v2 cover hw/usb/hcd-ohci: Fix ISO TD USBPacket handling
This series updates OHCI ISO TD USBPacket handling.
Changes since v1:
- Split the original patch into two patches.
- Replace the USB_RET_ASYNC handling with an assertion.
- Keep usb_packet_cleanup() only on the synchronous completion path.
Munkhbaatar Enkhbaatar (2):
hw/usb/hcd-ohci: Assert isochronous TDs are never deferred hw/usb/hcd-ohci: Clean up USBPacket before freeing ISO TD packet
hw/usb/hcd-ohci.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 1/2 hw/usb/hcd-ohci: Assert isochronous TDs are never deferred
--- hw/usb/hcd-ohci.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
hw/usb/hcd-ohci.c +4 -5
@@ -746,11 +746,10 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed)usb_packet_setup (pkt, pid, ep,0 , addr, false, int_req);usb_packet_addbuf (pkt, buf, len);usb_handle_packet (dev, pkt); -if (pkt->status ==USB_RET_ASYNC ) { -usb_device_flush_ep_queue (dev, ep); -g_free (pkt); -return 1 ; - } + +/* The USB core guarantees to never defer ISO TDs. */ +assert (pkt->status !=USB_RET_ASYNC ); +if (pkt->status ==USB_RET_SUCCESS ) { ret = pkt->actual_length; }else {
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v2 1/2] hw/usb/hcd-ohci: Assert isochronous TDs are never deferred
Quoted 14 lines · Munkhbaatar Enkhbaatar
Suggested-by Peter Maydell <[email protected]>Signed-off-by Munkhbaatar Enkhbaatar <[email protected]>--- hw/usb/hcd-ohci.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)hw/usb/hcd-ohci.c +0
-0@@ -746,11 +746,10 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed)usb_packet_setup (pkt, pid, ep,0 , addr, false, int_req);
hw/usb/hcd-ohci.c static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) · line 751
Quoted 10 lines
hw/usb/hcd-ohci.c +3
-5usb_packet_addbuf (pkt, buf, len);usb_handle_packet (dev, pkt); -if (pkt->status ==USB_RET_ASYNC ) { -usb_device_flush_ep_queue (dev, ep); -g_free (pkt); -return 1 ; - } + +/* The USB core guarantees to never defer ISO TDs. */ +assert (pkt->status !=USB_RET_ASYNC );
thanks
-- PMM
PATCH v2 2/2 hw/usb/hcd-ohci: Clean up USBPacket before freeing ISO TD packet
In reply to Munkhbaatar Enkhbaatarohci_service_iso_td() allocates a USBPacket and frees it after synchronous completion, but it does not call usb_packet_cleanup() first.
Call usb_packet_cleanup() before g_free() so resources owned by USBPacket are released.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3463
--- hw/usb/hcd-ohci.c | 1 + 1 file changed, 1 insertion(+)
hw/usb/hcd-ohci.c +1 -0
@@ -755,6 +755,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) }else { ret = pkt->status; } +usb_packet_cleanup (pkt);g_free (pkt); trace_usb_ohci_iso_td_so(start_offset, end_offset, start_addr, end_addr,
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v2 2/2] hw/usb/hcd-ohci: Clean up USBPacket before freeing ISO TD packet
Quoted 27 lines · Munkhbaatar Enkhbaatar
ohci_service_iso_td()allocates a USBPacket and frees it after synchronous completion, but it does not callusb_packet_cleanup()first.Call
usb_packet_cleanup()beforeg_free()so resources owned by USBPacket are released.Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3463
Signed-off-by Munkhbaatar Enkhbaatar <[email protected]>--- hw/usb/hcd-ohci.c | 1 + 1 file changed, 1 insertion(+)hw/usb/hcd-ohci.c +1
-1@@ -755,6 +755,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed) }else { ret = pkt->status; } +usb_packet_cleanup (pkt);g_free (pkt); trace_usb_ohci_iso_td_so(start_offset, end_offset, start_addr, end_addr, --
Quoted 1 line
2.50.1 (Apple Git-155)
thanks
-- PMM
Re: [PATCH v2 0/2] hw/usb/hcd-ohci: Fix ISO TD USBPacket handling
In reply to Munkhbaatar EnkhbaatarQuoted 11 lines · Munkhbaatar Enkhbaatar
This series updates OHCI ISO TD USBPacket handling.
Changes since v1:
- Split the original patch into two patches.
- Replace theUSB_RET_ASYNChandling with an assertion.
- Keepusb_packet_cleanup()only on the synchronous completion path.Munkhbaatar Enkhbaatar (2):
hw/usb/hcd-ohci: Assert isochronous TDs are never deferred hw/usb/hcd-ohci: Clean up USBPacket before freeing ISO TD packet
Since nobody else has picked these up and the main user of OHCI USB is arm boards, I've applied these to target-arm.next.
thanks
-- PMM
Re: [PATCH v2 0/2] hw/usb/hcd-ohci: Fix ISO TD USBPacket handling
Hi PMM,
Thank you for applying patches.
BR,
Munkhbaatar
Quoted 20 lines · Peter Maydell
Quoted 11 lines · Munkhbaatar Enkhbaatar
This series updates OHCI ISO TD USBPacket handling.
Changes since v1:
- Split the original patch into two patches.
- Replace theUSB_RET_ASYNChandling with an assertion.
- Keepusb_packet_cleanup()only on the synchronous completion path.Munkhbaatar Enkhbaatar (2):
hw/usb/hcd-ohci: Assert isochronous TDs are never deferred hw/usb/hcd-ohci: Clean up USBPacket before freeing ISO TD packetSince nobody else has picked these up and the main user of OHCI USB is arm boards, I've applied these to target-arm.next.
thanks
-- PMM