On Tue, Nov 06, 2012 at 11:32:19AM +0100, Mike Belopuhov wrote: > attach fails early in case there's no firmware, but > athn_detach does ieee80211_ifdetach and if_detach > regardless of whether ifnet part got setup correctly > leading to a free of an unallocated memory and a > panic. > > the following diff follows an established practice > in the other drivers and fixes the problem here. > tested on this device: > > athn0 at uhub0 port 3 "NETGEAR WNA WNA1100" rev 2.00/1.08 addr 2 > athn0: AR9271 rev 1 (1T1R), ROM rev 15, address 20:4e:7f:d9:65:2f > > ok?
It seems more appropriate to handle this problem entirely within the USB-specific layer of the driver, rather than within dev/ic/athn.c. If loading the firmware fails, athn_usb_attachhook() doesn't even call athn_attach(). This is what your fix is compensating for. I believe athn_usb_detach() shouldn't be calling athn_detach() unconditionally. I fixed a similar problem for acx(4) cardbus cards once, where ejecting a card that had failed to attach half way through paniced the kernel. This was in r1.12 of dev/cardbus/if_acx_cardbus.c, which looked like this: Index: if_acx_cardbus.c =================================================================== RCS file: /cvs/src/sys/dev/cardbus/if_acx_cardbus.c,v retrieving revision 1.11 retrieving revision 1.12 diff -u -p -r1.11 -r1.12 --- if_acx_cardbus.c 10 Nov 2006 20:20:04 -0000 1.11 +++ if_acx_cardbus.c 26 Feb 2009 23:03:05 -0000 1.12 @@ -1,4 +1,4 @@ -/* $OpenBSD: if_acx_cardbus.c,v 1.11 2006/11/10 20:20:04 damien Exp $ */ +/* $OpenBSD: if_acx_cardbus.c,v 1.12 2009/02/26 23:03:05 stsp Exp $ */ /* * Copyright (c) 2006 Claudio Jeker <[email protected]> @@ -77,6 +77,8 @@ struct acx_cardbus_softc { bus_space_tag_t sc_io_bt; bus_space_handle_t sc_io_bh; bus_size_t sc_iomapsize; + + int sc_acx_attached; }; int acx_cardbus_match(struct device *, void *, void *); @@ -171,7 +173,8 @@ acx_cardbus_attach(struct device *parent else acx100_set_param(sc); - acx_attach(sc); + error = acx_attach(sc); + csc->sc_acx_attached = error == 0; Cardbus_function_disable(ct); } @@ -186,9 +189,11 @@ acx_cardbus_detach(struct device *self, cardbus_function_tag_t cf = ct->ct_cf; int error, b1 = CARDBUS_BASE0_REG, b2 = CARDBUS_BASE1_REG; - error = acx_detach(sc); - if (error != 0) - return (error); + if (csc->sc_acx_attached) { + error = acx_detach(sc); + if (error != 0) + return (error); + } /* unhook the interrupt handler */ if (csc->sc_ih != NULL) { > > Index: athn.c > =================================================================== > RCS file: /home/cvs/src/sys/dev/ic/athn.c,v > retrieving revision 1.74 > diff -u -p -u -p -r1.74 athn.c > --- athn.c 20 Oct 2012 09:54:20 -0000 1.74 > +++ athn.c 1 Nov 2012 16:33:53 -0000 > @@ -396,8 +396,10 @@ athn_detach(struct athn_softc *sc) > if (sc->eep != NULL) > free(sc->eep, M_DEVBUF); > > - ieee80211_ifdetach(ifp); > - if_detach(ifp); > + if (ifp->if_softc != NULL) { > + ieee80211_ifdetach(ifp); > + if_detach(ifp); > + } > } > > #if NBPFILTER > 0
