Re: mem leak in mii ?

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Mon, 22 Nov 2004 16:44:57 -0500
On Friday 19 November 2004 06:49 pm, Bjoern A. Zeeb wrote:
> Hi,
>
> in sys/dev/mii/mii.c there are two calls to malloc for ivars;
> see for example mii_phy_probe:
>
>         v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT);
>         if (v == 0) {
>                 return (ENOMEM);
>         }
>         v[0] = ifmedia_upd;
>         v[1] = ifmedia_sts;
>         *child = device_add_child(dev, "miibus", -1);
>         device_set_ivars(*child, v);
>
> Where is the free for this malloc ? I cannot find it.
>
> analogous: miibus_probe ?

It's a leak.  It should be free'd when the miibus device is destroyed.  Here's 
a possible fix:

Index: dev/mii/mii.c
===================================================================
RCS file: /usr/cvs/src/sys/dev/mii/mii.c,v
retrieving revision 1.20
diff -u -r1.20 mii.c
--- dev/mii/mii.c	15 Aug 2004 06:24:40 -0000	1.20
+++ dev/mii/mii.c	22 Nov 2004 21:43:40 -0000
_at__at_ -186,11 +186,15 _at__at_
 	device_t		dev;
 {
 	struct mii_data		*mii;
+	void			**v;
 
 	bus_generic_detach(dev);
 	mii = device_get_softc(dev);
 	ifmedia_removeall(&mii->mii_media);
 	mii->mii_ifp = NULL;
+	v = device_get_ivars(dev);
+	device_set_ivars(dev, NULL);
+	free(v, M_DEVBUF);
 
 	return(0);
 }
_at__at_ -325,6 +329,7 _at__at_
 
 	if (i == MII_NPHY) {
 		device_delete_child(dev, *child);
+		free(v, M_DEVBUF);
 		*child = NULL;
 		return(ENXIO);
 	}


-- 
John Baldwin <jhb_at_FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
"Power Users Use the Power to Serve"  =  http://www.FreeBSD.org
Received on Mon Nov 22 2004 - 21:00:00 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:23 UTC