move the deallocation of the protocol-specific softc into its own detach hook,

so that calls to kmem_alloc() and kmem_free() would agree on the size

fixes panic on umass detach reported by Tom Ivar Helbekkmo on current-users@
This commit is contained in:
jdolecek 2019-02-10 19:23:55 +00:00
parent 72c6780ab3
commit 84351d4728
4 changed files with 51 additions and 9 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: umass.c,v 1.173 2019/02/07 13:48:27 skrll Exp $ */
/* $NetBSD: umass.c,v 1.174 2019/02/10 19:23:55 jdolecek Exp $ */
/*
* Copyright (c) 2003 The NetBSD Foundation, Inc.
@ -124,7 +124,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: umass.c,v 1.173 2019/02/07 13:48:27 skrll Exp $");
__KERNEL_RCSID(0, "$NetBSD: umass.c,v 1.174 2019/02/10 19:23:55 jdolecek Exp $");
#ifdef _KERNEL_OPT
#include "opt_usb.h"
@ -828,6 +828,24 @@ umass_detach(device_t self, int flags)
rv = config_detach(scbus->sc_child, flags);
switch (sc->sc_cmd) {
case UMASS_CPROTO_RBC:
case UMASS_CPROTO_SCSI:
#if NSCSIBUS > 0
umass_scsi_detach(sc);
#else
aprint_error_dev(self, "scsibus not configured\n");
#endif
break;
case UMASS_CPROTO_UFI:
case UMASS_CPROTO_ATAPI:
#if NATAPIBUS > 0
umass_atapi_detach(sc);
#else
aprint_error_dev(self, "atapibus not configured\n");
#endif
break;
case UMASS_CPROTO_ISD_ATA:
#if NWD > 0
umass_isdata_detach(sc);
@ -841,8 +859,8 @@ umass_detach(device_t self, int flags)
break;
}
kmem_free(scbus, sizeof(*scbus));
sc->bus = NULL;
/* protocol detach is expected to free sc->bus */
KASSERT(sc->bus == NULL);
}
if (rv != 0)

View File

@ -1,4 +1,4 @@
/* $NetBSD: umass_isdata.c,v 1.41 2019/02/07 13:48:27 skrll Exp $ */
/* $NetBSD: umass_isdata.c,v 1.42 2019/02/10 19:23:55 jdolecek Exp $ */
/*
* TODO:
@ -37,7 +37,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: umass_isdata.c,v 1.41 2019/02/07 13:48:27 skrll Exp $");
__KERNEL_RCSID(0, "$NetBSD: umass_isdata.c,v 1.42 2019/02/10 19:23:55 jdolecek Exp $");
#ifdef _KERNEL_OPT
#include "opt_usb.h"
@ -244,6 +244,9 @@ umass_isdata_detach(struct umass_softc *sc)
struct uisdata_softc *scbus = (struct uisdata_softc *)sc->bus;
ata_channel_destroy(&scbus->sc_channel);
kmem_free(scbus, sizeof(*scbus));
sc->bus = NULL;
}
void

View File

@ -1,4 +1,4 @@
/* $NetBSD: umass_scsipi.c,v 1.59 2019/02/07 13:48:27 skrll Exp $ */
/* $NetBSD: umass_scsipi.c,v 1.60 2019/02/10 19:23:55 jdolecek Exp $ */
/*
* Copyright (c) 2001, 2003, 2012 The NetBSD Foundation, Inc.
@ -31,7 +31,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: umass_scsipi.c,v 1.59 2019/02/07 13:48:27 skrll Exp $");
__KERNEL_RCSID(0, "$NetBSD: umass_scsipi.c,v 1.60 2019/02/10 19:23:55 jdolecek Exp $");
#ifdef _KERNEL_OPT
#include "opt_usb.h"
@ -151,6 +151,15 @@ umass_scsi_attach(struct umass_softc *sc)
return 0;
}
void
umass_scsi_detach(struct umass_softc *sc)
{
struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
kmem_free(scbus, sizeof(*scbus));
sc->bus = NULL;
}
#endif
#if NATAPIBUS > 0
@ -183,6 +192,15 @@ umass_atapi_attach(struct umass_softc *sc)
return 0;
}
void
umass_atapi_detach(struct umass_softc *sc)
{
struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
kmem_free(scbus, sizeof(*scbus));
sc->bus = NULL;
}
#endif
Static struct umass_scsipi_softc *

View File

@ -1,4 +1,4 @@
/* $NetBSD: umass_scsipi.h,v 1.3 2016/04/23 10:15:32 skrll Exp $ */
/* $NetBSD: umass_scsipi.h,v 1.4 2019/02/10 19:23:55 jdolecek Exp $ */
/*
* Copyright (c) 2001 The NetBSD Foundation, Inc.
@ -31,4 +31,7 @@
*/
int umass_scsi_attach(struct umass_softc *);
void umass_scsi_detach(struct umass_softc *);
int umass_atapi_attach(struct umass_softc *);
void umass_atapi_detach(struct umass_softc *);