Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4D053CD3424 for ; Wed, 6 May 2026 04:14:28 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wKTdu-0002Pe-Pg; Wed, 06 May 2026 00:13:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wKTdt-0002PH-Bk for qemu-devel@nongnu.org; Wed, 06 May 2026 00:13:49 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1wKTdr-000699-6A for qemu-devel@nongnu.org; Wed, 06 May 2026 00:13:49 -0400 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-3650a628473so3968123a91.3 for ; Tue, 05 May 2026 21:13:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778040826; x=1778645626; darn=nongnu.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=AK3zMwVO3WE3Y00R1JNOuZCh4QPSwgne75XPkrfDBak=; b=Vg99/qz+D8WcZxXBZYZTrXNbnTSD1c+sew8RIKeeirTXp1kjFCqMVB/2ILzYm12Xvp mQTs8eieCv+kk0up3CnymXdNp+l5Hqk5VGWuTn2/kDBGbuvkQvDVgZxCDeevmd+7alfo LazQKPr36o/ufWkpo7I9RuO6Wun2bk/qxYFreFe+saTjbvZVFJvg+v2kXdtMCc787ZhR k7xx8u3gwUFw+O5QB4wAkP5at1wxosM4YU2XRywUANxO2x+TWJMJYhx/aFpuaW6urF+a xVEJlA+VKn0fwO8i1we7cXSBYA62vhxN3CNOaKmuYKzfXojIaBaCfm5643sawnY59+1P pveg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778040826; x=1778645626; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AK3zMwVO3WE3Y00R1JNOuZCh4QPSwgne75XPkrfDBak=; b=ExZg8kc+4WsnknydqVAcuoGeqc61eeQ31BeF6FhkU7pMIyszqgeKvBalInBmk4U9tD 2E+zOtUjOcuhWxWRCQzbZTWe97YTi7CtivUqm9wyFqJVc6rFeoH8mE/2qoT4+SeMKL5/ w2358idgVzhLaWFK2JWHcBAu1YTIdVbazC5ifAkGwZdhDQmjCZmwM0g84jbgdvrI/Vpa xmkDLW33ThxLgqLhMblVc+qT5XAmgI+IPHtsFusyfr47aNiDES680ZpEpoQlgmlyi1pC VQ1EEILHQe9F/2FVSEpIOHaEFqcN+LMSqZT3AORvutUUnWV42ZtjR9jKWcym581AkN2+ cd/g== X-Gm-Message-State: AOJu0YzIDl6ZJSkmZF6+L/1EWihcPQyxm6pDq6cX6QLyVbb4D0DP+pVb QXmJWjEzibv5cDL/D/swcd2xrSt4r9IKMpgSNMYJWxziU74aGvnX4trH X-Gm-Gg: AeBDiet8hDq4ZD6CXr0h46n7fOaWL0BMSNkZf31ITyk6g4r1JAvQoCWSS7Zgv8sb+AW c0VdIzUyuQW7el7Cvx8mQxebIpaSHLnol62qxh3LPUqQuoS0NiLoBkgztBXODBuZ0TGoA6d93jZ 34P9y7OQwb1mwzEBgAz3oczbMJ6xlxejaB6y8eWf/iVshKdjlEx9DOqUdkS7X6ymbhfnSgFAzGf R2F1T9RlnnjJ4LEzS82mIvyX/dpPd29qyyqY1RCn9E2Ayxt6GPT2+Hd3aPsfB2KYs00zeUf+p6l irIUpsxHtxD66JZgJGSIgoyBw820mma6bo/Vs/KSDyc04CBsKPpNjzukFOtAbXbesGY8vdZNxye uzXEmjY6MST1bBo32cCnQan8fO1Z4+Ta1WD2P6TU9rWM0nKowZbwB7AqDlr0R0WWJvNdFoNWnL0 k+GDYDtxVjDDP9ez2ZqQ62K0Sc/b1x4jxF6Q4TjkZuFKPUGIpwqy/tCZ6gvg== X-Received: by 2002:a17:90b:4e87:b0:362:ded3:bbcb with SMTP id 98e67ed59e1d1-365ac77546emr1614461a91.25.1778040825448; Tue, 05 May 2026 21:13:45 -0700 (PDT) Received: from smtpclient.apple ([202.55.186.124]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-365b0560fa8sm329934a91.1.2026.05.05.21.13.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 May 2026 21:13:44 -0700 (PDT) From: =?utf-8?B?0JzTqdC90YXQsdCw0LDRgtCw0YAg0K3QvdGF0LHQsNCw0YLQsNGA?= Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_DCFE8011-BCAA-4ADD-8C82-5914F24140FA" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.500.181\)) Subject: Re: [PATCH] hw/usb/hcd-ohci: Fix memory leak in ohci_service_iso_td() Date: Wed, 6 May 2026 12:13:25 +0800 In-Reply-To: Cc: qemu-devel@nongnu.org To: Peter Maydell References: <20260505110036.47574-1-munkhuu0825@gmail.com> X-Mailer: Apple Mail (2.3864.500.181) Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=munkhuu0825@gmail.com; helo=mail-pj1-x102e.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org --Apple-Mail=_DCFE8011-BCAA-4ADD-8C82-5914F24140FA Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On 5 May 2026, at 21:14, Peter Maydell = wrote: >=20 > On Tue, 5 May 2026 at 12:01, > wrote: >>=20 >> From: heropd > >>=20 >> Fix a memory leak in ohci_service_iso_td() by calling >> usb_packet_cleanup() before freeing USBPacket. >>=20 >> Signed-off-by: Munkhbaatar Enkhbaatar > >=20 > Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3463 >=20 >=20 >> --- >> hw/usb/hcd-ohci.c | 2 ++ >> 1 file changed, 2 insertions(+) >>=20 >> diff --git a/hw/usb/hcd-ohci.c b/hw/usb/hcd-ohci.c >> index 6ed8046fc2..43c49a42ec 100644 >> --- a/hw/usb/hcd-ohci.c >> +++ b/hw/usb/hcd-ohci.c >> @@ -748,6 +748,7 @@ static int ohci_service_iso_td(OHCIState *ohci, = struct ohci_ed *ed) >> usb_handle_packet(dev, pkt); >> if (pkt->status =3D=3D USB_RET_ASYNC) { >> usb_device_flush_ep_queue(dev, ep); >> + usb_packet_cleanup(pkt); >> g_free(pkt); >> return 1; >> } >=20 > 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. >=20 > 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. >=20 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. >> @@ -756,6 +757,7 @@ static int ohci_service_iso_td(OHCIState *ohci, = struct ohci_ed *ed) >> } else { >> ret =3D pkt->status; >> } >> + usb_packet_cleanup(pkt); >> g_free(pkt); >=20 > 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 >=20 > 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 --Apple-Mail=_DCFE8011-BCAA-4ADD-8C82-5914F24140FA Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii

On 5 May 2026, at 21:14, Peter Maydell = <peter.maydell@linaro.org> wrote:

On Tue, 5 May 2026 at 12:01, <munkhuu0825@gmail.com> wrote:

From: heropd <munkhuu0825@gmail.com>
Fix a memory leak in ohci_service_iso_td() by = calling
usb_packet_cleanup() before freeing = USBPacket.

Signed-off-by: Munkhbaatar Enkhbaatar <munkhuu0825@gmail.com>

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3463


---
hw/usb/hcd-ohci.c | 2 ++
1 file changed, 2 = insertions(+)

diff --git a/hw/usb/hcd-ohci.c = b/hw/usb/hcd-ohci.c
index 6ed8046fc2..43c49a42ec 100644
--- = a/hw/usb/hcd-ohci.c
+++ b/hw/usb/hcd-ohci.c
@@ -748,6 +748,7 @@ = static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed = *ed)
    usb_handle_packet(dev, = pkt);
    if (pkt->status =3D=3D = USB_RET_ASYNC) = {
        usb_device_flush_ep_q= ueue(dev, ep);
+ =        usb_packet_cleanup(pkt);
&nbs= p;       g_free(pkt);
  &n= bsp;     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));<= br style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; = font-size: 12px; font-style: normal; font-variant-caps: normal; = font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; = text-indent: 0px; text-transform: none; white-space: normal; widows: 2; = word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-line: = none; text-decoration-thickness: auto; text-decoration-style: = solid;">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.


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.

@@ = -756,6 +757,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct = ohci_ed *ed)
    } else = {
        ret =3D = pkt->status;
    }
+ =    usb_packet_cleanup(pkt);
    g_fr= ee(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


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

= --Apple-Mail=_DCFE8011-BCAA-4ADD-8C82-5914F24140FA--