gmirror: Pre-allocate the timeout event structure

We can't call malloc(M_WAITOK) in a callout handler.

Reviewed by:	imp
Reported by:	pho
Tested by:	pho
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D29223
This commit is contained in:
Mark Johnston 2021-03-11 15:43:04 -05:00
parent d28cbb7944
commit 2f1cfb7f63
2 changed files with 35 additions and 9 deletions

View File

@ -115,6 +115,7 @@ static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
static void g_mirror_update_device(struct g_mirror_softc *sc, bool force); static void g_mirror_update_device(struct g_mirror_softc *sc, bool force);
static void g_mirror_dumpconf(struct sbuf *sb, const char *indent, static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp); struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
static void g_mirror_timeout_drain(struct g_mirror_softc *sc);
static int g_mirror_refresh_device(struct g_mirror_softc *sc, static int g_mirror_refresh_device(struct g_mirror_softc *sc,
const struct g_provider *pp, const struct g_mirror_metadata *md); const struct g_provider *pp, const struct g_mirror_metadata *md);
static void g_mirror_sync_reinit(const struct g_mirror_disk *disk, static void g_mirror_sync_reinit(const struct g_mirror_disk *disk,
@ -183,15 +184,14 @@ g_mirror_event_free(struct g_mirror_event *ep)
free(ep, M_MIRROR); free(ep, M_MIRROR);
} }
int static int
g_mirror_event_send(void *arg, int state, int flags) g_mirror_event_dispatch(struct g_mirror_event *ep, void *arg, int state,
int flags)
{ {
struct g_mirror_softc *sc; struct g_mirror_softc *sc;
struct g_mirror_disk *disk; struct g_mirror_disk *disk;
struct g_mirror_event *ep;
int error; int error;
ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep); G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
if ((flags & G_MIRROR_EVENT_DEVICE) != 0) { if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
disk = NULL; disk = NULL;
@ -226,6 +226,15 @@ g_mirror_event_send(void *arg, int state, int flags)
return (error); return (error);
} }
int
g_mirror_event_send(void *arg, int state, int flags)
{
struct g_mirror_event *ep;
ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
return (g_mirror_event_dispatch(ep, arg, state, flags));
}
static struct g_mirror_event * static struct g_mirror_event *
g_mirror_event_first(struct g_mirror_softc *sc) g_mirror_event_first(struct g_mirror_softc *sc)
{ {
@ -582,7 +591,7 @@ g_mirror_destroy_device(struct g_mirror_softc *sc)
mtx_unlock(&sc->sc_events_mtx); mtx_unlock(&sc->sc_events_mtx);
} }
} }
callout_drain(&sc->sc_callout); g_mirror_timeout_drain(sc);
g_topology_lock(); g_topology_lock();
LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) { LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
@ -2291,13 +2300,26 @@ static void
g_mirror_go(void *arg) g_mirror_go(void *arg)
{ {
struct g_mirror_softc *sc; struct g_mirror_softc *sc;
struct g_mirror_event *ep;
sc = arg; sc = arg;
G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name); G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
g_mirror_event_send(sc, 0, ep = sc->sc_timeout_event;
sc->sc_timeout_event = NULL;
g_mirror_event_dispatch(ep, sc, 0,
G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE); G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
} }
static void
g_mirror_timeout_drain(struct g_mirror_softc *sc)
{
sx_assert(&sc->sc_lock, SX_XLOCKED);
callout_drain(&sc->sc_callout);
g_mirror_event_free(sc->sc_timeout_event);
sc->sc_timeout_event = NULL;
}
static u_int static u_int
g_mirror_determine_state(struct g_mirror_disk *disk) g_mirror_determine_state(struct g_mirror_disk *disk)
{ {
@ -2454,7 +2476,7 @@ g_mirror_update_device(struct g_mirror_softc *sc, bool force)
* Disks went down in starting phase, so destroy * Disks went down in starting phase, so destroy
* device. * device.
*/ */
callout_drain(&sc->sc_callout); g_mirror_timeout_drain(sc);
sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY; sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__, G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
sc->sc_rootmount); sc->sc_rootmount);
@ -2491,7 +2513,7 @@ g_mirror_update_device(struct g_mirror_softc *sc, bool force)
} }
} else { } else {
/* Cancel timeout. */ /* Cancel timeout. */
callout_drain(&sc->sc_callout); g_mirror_timeout_drain(sc);
} }
/* /*
@ -3153,10 +3175,13 @@ g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md,
sc->sc_rootmount = root_mount_hold("GMIRROR"); sc->sc_rootmount = root_mount_hold("GMIRROR");
G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount); G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
/* /*
* Run timeout. * Schedule startup timeout.
*/ */
timeout = g_mirror_timeout * hz; timeout = g_mirror_timeout * hz;
sc->sc_timeout_event = malloc(sizeof(struct g_mirror_event), M_MIRROR,
M_WAITOK);
callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc); callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
return (sc->sc_geom); return (sc->sc_geom);
} }

View File

@ -207,6 +207,7 @@ struct g_mirror_softc {
TAILQ_HEAD(, g_mirror_event) sc_events; TAILQ_HEAD(, g_mirror_event) sc_events;
struct mtx sc_events_mtx; struct mtx sc_events_mtx;
struct g_mirror_event *sc_timeout_event;
struct callout sc_callout; struct callout sc_callout;