NetBSD Problem Report #39322

From www@NetBSD.org  Fri Aug  8 08:53:37 2008
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by narn.NetBSD.org (Postfix) with ESMTP id E225263BC30
	for <gnats-bugs@gnats.netbsd.org>; Fri,  8 Aug 2008 08:53:37 +0000 (UTC)
Message-Id: <20080808085337.6C00B63BB81@narn.NetBSD.org>
Date: Fri,  8 Aug 2008 08:53:37 +0000 (UTC)
From: andreas.jacobs@lancom.de
Reply-To: andreas.jacobs@lancom.de
To: gnats-bugs@NetBSD.org
Subject: Memoryleaks in Ohci Driver (USB)
X-Send-Pr-Version: www-1.0

>Number:         39322
>Category:       kern
>Synopsis:       Memoryleaks in Ohci Driver (USB)
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kern-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Aug 08 08:55:00 +0000 2008
>Closed-Date:    Wed Aug 13 09:45:22 +0000 2008
>Last-Modified:  Wed Aug 13 10:50:03 +0000 2008
>Originator:     Andreas Jacobs
>Release:        CVS Head from 2008-08-07
>Organization:
LANCOM Systems GmbH
>Environment:
I do not use the NetBSD operating system itself but only its USB-Stack ported to another OS.
>Description:
If you attach an Ohci and later detach it again, it does not free all the memory it has allocated:

1) In ohci.c:715 init_ohci() calls usb_allocmem to allocate a memory block to which a reference is stored in sc->sc_hccadma. This block is never deallocated

2) In ohci.c:706 init_ohci() creates an empty SIMPLEQ sc->sc_free_xfers to hold a reserve of unused xfers. ohci_allocx() later tries to take new xfers from that queue or creates new xfers via malloc if that queue is emtpy. ohci_freex() returns xfers back to that queue. This is fine as long as the ohci is attached, but when the ohci vanishes, the xfers should be deallocated, because there isn't any longer a way to use them again.

Thank you for fixing this
Andreas Jacobs
>How-To-Repeat:
Attach and detach an Ohci over and over again. This is hard to do, if that chip is part of your PC, but is possible, if you use a UMTS/HSPA modem attached via PCMCIA.
>Fix:
1) Deallocate the sc->sc_hccadma block in ohci_detach() via usb_freemem()
2) Deallocate all xfers in sc->sc_free_xfers in ohci_detach()

Here is a patch for that, in universal diff format with 3 lines of context:

Index: ohci.c
===================================================================
RCS file: /cvsroot/src/sys/dev/usb/ohci.c,v
retrieving revision 1.195
diff -U3 -r1.195 ohci.c
--- ohci.c      28 Jun 2008 17:42:53 -0000      1.195
+++ ohci.c      8 Aug 2008 08:29:39 -0000
@@ -385,6 +385,7 @@
 ohci_detach(struct ohci_softc *sc, int flags)
 {
        int rv = 0;
+       usbd_xfer_handle xfer;

        if (sc->sc_child != NULL)
                rv = config_detach(sc->sc_child, flags);
@@ -397,6 +398,11 @@
        usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */

        /* free data structures XXX */
+       usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
+       while((xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers)) != NULL) {
+               SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
+               k_free(xfer, M_USB);
+       }

        return (rv);
 }

>Release-Note:

>Audit-Trail:
From: Matthias Drochner <M.Drochner@fz-juelich.de>
To: gnats-bugs@NetBSD.org
Cc: kern-bug-people@NetBSD.org, gnats-admin@NetBSD.org,
	netbsd-bugs@NetBSD.org
Subject: Re: kern/39322: Memoryleaks in Ohci Driver (USB) 
Date: Tue, 12 Aug 2008 20:38:24 +0200

 This is a multipart MIME message.

 --==_Exmh_35652100181460
 Content-Type: text/plain; charset=us-ascii


 andreas.jacobs@lancom.de said:
 > Here is a patch for that

 Thanks -- looks OK, but the memory is allocated by malloc(),
 so it should be freed by free()... could you check whether
 the appended patch works for you?

 best regards
 Matthias





 -------------------------------------------------------------------
 -------------------------------------------------------------------
 Forschungszentrum Juelich GmbH
 52425 Juelich

 Sitz der Gesellschaft: Juelich
 Eingetragen im Handelsregister des Amtsgerichts Dueren Nr. HR B 3498
 Vorsitzende des Aufsichtsrats: MinDir'in Baerbel Brumme-Bothe
 Geschaeftsfuehrung: Prof. Dr. Achim Bachem (Vorsitzender),
 Dr. Ulrich Krafft (stellv. Vorsitzender), Prof. Dr. Harald Bolt,
 Dr. Sebastian M. Schmidt
 -------------------------------------------------------------------
 -------------------------------------------------------------------

 --==_Exmh_35652100181460
 Content-Type: text/plain ; name="ohci.txt"; charset=us-ascii
 Content-Description: ohci.txt
 Content-Disposition: attachment; filename="ohci.txt"

 #
 # old_revision [05483fc8817ab109a28dae727d55611ef093c9cf]
 #
 # patch "sys/dev/usb/ohci.c"
 #  from [66ec729b074362214149b22651b8d37d0658f6b6]
 #    to [961ba09736eaeb4045e7500fd0fd467129e262c4]
 #
 ============================================================
 --- sys/dev/usb/ohci.c	66ec729b074362214149b22651b8d37d0658f6b6
 +++ sys/dev/usb/ohci.c	961ba09736eaeb4045e7500fd0fd467129e262c4
 @@ -385,6 +385,7 @@ ohci_detach(struct ohci_softc *sc, int f
  ohci_detach(struct ohci_softc *sc, int flags)
  {
  	int rv = 0;
 +	usbd_xfer_handle xfer;

  	if (sc->sc_child != NULL)
  		rv = config_detach(sc->sc_child, flags);
 @@ -396,7 +397,11 @@ ohci_detach(struct ohci_softc *sc, int f

  	usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */

 -	/* free data structures XXX */
 +	usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
 +	while((xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers)) != NULL) {
 +		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
 +		free(xfer, M_USB);
 +	}

  	return (rv);
  }

 --==_Exmh_35652100181460--

State-Changed-From-To: open->closed
State-Changed-By: drochner@NetBSD.org
State-Changed-When: Wed, 13 Aug 2008 09:45:22 +0000
State-Changed-Why:
patch applied, thanks


From: Matthias Drochner <drochner@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/39322 CVS commit: src/sys/dev/usb
Date: Wed, 13 Aug 2008 09:43:56 +0000 (UTC)

 Module Name:	src
 Committed By:	drochner
 Date:		Wed Aug 13 09:43:56 UTC 2008

 Modified Files:
 	src/sys/dev/usb: ohci.c

 Log Message:
 free memory on detach, fixes memory leak, from Andreas Jacobs per
 PR kern/39322


 To generate a diff of this commit:
 cvs rdiff -r1.195 -r1.196 src/sys/dev/usb/ohci.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: Andreas.Jacobs@lancom.de
To: gnats-bugs@NetBSD.org,
	M.Drochner@fz-juelich.de
Cc: 
Subject: Re: kern/39322: Memoryleaks in Ohci Driver (USB)
Date: Wed, 13 Aug 2008 10:43:12 +0200

 Dies ist eine mehrteilige Nachricht im MIME-Format.
 --=_alternative 002FE68AC12574A4_=
 Content-Type: text/plain; charset="ISO-8859-1"
 Content-Transfer-Encoding: quoted-printable

 Hello,

 > Thanks -- looks OK, but the memory is allocated by malloc(),
 > so it should be freed by free()... could you check whether
 > the appended patch works for you?

 yes, you are perfectly right, it must be free, not k=5Ffree. A tiny mistake=
 =20
 on my side. Besides that you didn't change much in my patch, so I am=20
 satisfied with that.

 best regards
 Andreas

 Andreas Jacobs
 E-Mail: andreas.jacobs@lancom.de

 LANCOM Systems GmbH
 Adenauerstra=DFe 20 / B2, 52146 Wuerselen, Germany
 http://www.lancom.de
 =5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=
 =5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=20
 Amtsgericht Aachen, HRB 8889
 Gesch=E4ftsf=FChrer Ralf Koenzen

 --=_alternative 002FE68AC12574A4_=
 Content-Type: text/html; charset="ISO-8859-1"
 Content-Transfer-Encoding: quoted-printable


 <br><font size=3D2 face=3D"sans-serif">Hello,</font>
 <br>
 <br><font size=3D2>&gt; Thanks -- looks OK, but the memory is allocated
 by malloc(),<br>
 &gt; so it should be freed by free()... could you check whether<br>
 &gt; the appended patch works for you?</font>
 <br>
 <br><font size=3D2 face=3D"sans-serif">yes, you are perfectly right, it must
 be free, not k=5Ffree. A tiny mistake on my side. Besides that you didn't
 change much in my patch, so I am satisfied with that.</font>
 <br>
 <br><font size=3D2 face=3D"sans-serif">best regards</font>
 <br><font size=3D2 face=3D"sans-serif">Andreas</font>
 <br>
 <br><font size=3D2 face=3D"sans-serif">Andreas Jacobs<br>
 E-Mail: andreas.jacobs@lancom.de<br>
 <br>
 LANCOM Systems GmbH<br>
 Adenauerstra=DFe 20 / B2, 52146 Wuerselen, Germany<br>
 </font><a href=3Dhttp://www.lancom.de/><font size=3D2 face=3D"sans-serif">h=
 ttp://www.lancom.de<br>
 =5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=
 =5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F <br>
 Amtsgericht Aachen, HRB 8889<br>
 Gesch=E4ftsf=FChrer Ralf Koenzen<br>
 </font></a>
 --=_alternative 002FE68AC12574A4_=--

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.