diff --git a/sys/kern/subr_eventhandler.c b/sys/kern/subr_eventhandler.c index 27f21f02da34..a56f1e4f3c6f 100644 --- a/sys/kern/subr_eventhandler.c +++ b/sys/kern/subr_eventhandler.c @@ -1,173 +1,240 @@ /*- * Copyright (c) 1999 Michael Smith * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records"); /* List of 'slow' lists */ static TAILQ_HEAD(, eventhandler_list) eventhandler_lists; static int eventhandler_lists_initted = 0; static struct mtx eventhandler_mutex; struct eventhandler_entry_generic { struct eventhandler_entry ee; void (* func)(void); }; +static struct eventhandler_list *_eventhandler_find_list(char *name); + /* * Initialize the eventhandler mutex and list. */ static void eventhandler_init(void *dummy __unused) { TAILQ_INIT(&eventhandler_lists); - mtx_init(&eventhandler_mutex, "eventhandler", NULL, MTX_DEF | MTX_RECURSE); - eventhandler_lists_initted = 1; + mtx_init(&eventhandler_mutex, "eventhandler", NULL, MTX_DEF); + atomic_store_rel_int(&eventhandler_lists_initted, 1); } SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init, NULL) /* * Insertion is O(n) due to the priority scan, but optimises to O(1) * if all priorities are identical. */ eventhandler_tag eventhandler_register(struct eventhandler_list *list, char *name, void *func, void *arg, int priority) { + struct eventhandler_list *new_list; struct eventhandler_entry_generic *eg; struct eventhandler_entry *ep; KASSERT(eventhandler_lists_initted, ("eventhandler registered too early")); /* lock the eventhandler lists */ mtx_lock(&eventhandler_mutex); /* Do we need to find/create the (slow) list? */ if (list == NULL) { /* look for a matching, existing list */ - list = eventhandler_find_list(name); + list = _eventhandler_find_list(name); /* Do we need to create the list? */ if (list == NULL) { - if ((list = malloc(sizeof(struct eventhandler_list) + strlen(name) - + 1, M_EVENTHANDLER, M_NOWAIT)) == NULL) { - mtx_unlock(&eventhandler_mutex); - return(NULL); + mtx_unlock(&eventhandler_mutex); + + new_list = malloc(sizeof(struct eventhandler_list) + + strlen(name) + 1, M_EVENTHANDLER, M_WAITOK); + + /* If someone else created it already, then use that one. */ + mtx_lock(&eventhandler_mutex); + list = _eventhandler_find_list(name); + if (list != NULL) { + free(new_list, M_EVENTHANDLER); + } else { + CTR2(KTR_EVH, "%s: creating list \"%s\"", __func__, name); + list = new_list; + list->el_flags = 0; + list->el_runcount = 0; + bzero(&list->el_lock, sizeof(list->el_lock)); + list->el_name = (char *)list + sizeof(struct eventhandler_list); + strcpy(list->el_name, name); + TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link); } - list->el_flags = 0; - bzero(&list->el_lock, sizeof(list->el_lock)); - list->el_name = (char *)list + sizeof(struct eventhandler_list); - strcpy(list->el_name, name); - TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link); } } - if (!(list->el_flags & EHE_INITTED)) { + if (!(list->el_flags & EHL_INITTED)) { TAILQ_INIT(&list->el_entries); - sx_init(&list->el_lock, name); - list->el_flags = EHE_INITTED; + mtx_init(&list->el_lock, name, "eventhandler list", MTX_DEF); + atomic_store_rel_int(&list->el_flags, EHL_INITTED); } mtx_unlock(&eventhandler_mutex); - + /* allocate an entry for this handler, populate it */ - if ((eg = malloc(sizeof(struct eventhandler_entry_generic), - M_EVENTHANDLER, M_NOWAIT)) == NULL) { - return(NULL); - } + eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER, + M_WAITOK | M_ZERO); eg->func = func; eg->ee.ee_arg = arg; eg->ee.ee_priority = priority; - + KASSERT(priority != EHE_DEAD_PRIORITY, + ("%s: handler for %s registered with dead priority", __func__, name)); + /* sort it into the list */ - EHE_LOCK(list); - for (ep = TAILQ_FIRST(&list->el_entries); - ep != NULL; - ep = TAILQ_NEXT(ep, ee_link)) { - if (eg->ee.ee_priority < ep->ee_priority) { + CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, eg, + func, name); + EHL_LOCK(list); + TAILQ_FOREACH(ep, &list->el_entries, ee_link) { + if (ep->ee_priority != EHE_DEAD_PRIORITY && + eg->ee.ee_priority < ep->ee_priority) { TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link); break; } } if (ep == NULL) TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link); - EHE_UNLOCK(list); + EHL_UNLOCK(list); return(&eg->ee); } void eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag) { struct eventhandler_entry *ep = tag; - /* XXX insert diagnostic check here? */ - EHE_LOCK(list); + EHL_LOCK_ASSERT(list, MA_OWNED); if (ep != NULL) { /* remove just this entry */ - TAILQ_REMOVE(&list->el_entries, ep, ee_link); - free(ep, M_EVENTHANDLER); + if (list->el_runcount == 0) { + CTR3(KTR_EVH, "%s: removing item %p from \"%s\"", __func__, ep, + list->el_name); + TAILQ_REMOVE(&list->el_entries, ep, ee_link); + free(ep, M_EVENTHANDLER); + } else { + CTR3(KTR_EVH, "%s: marking item %p from \"%s\" as dead", __func__, + ep, list->el_name); + ep->ee_priority = EHE_DEAD_PRIORITY; + } } else { /* remove entire list */ - while (!TAILQ_EMPTY(&list->el_entries)) { - ep = TAILQ_FIRST(&list->el_entries); + if (list->el_runcount == 0) { + CTR2(KTR_EVH, "%s: removing all items from \"%s\"", __func__, + list->el_name); TAILQ_REMOVE(&list->el_entries, ep, ee_link); - free(ep, M_EVENTHANDLER); + while (!TAILQ_EMPTY(&list->el_entries)) { + ep = TAILQ_FIRST(&list->el_entries); + TAILQ_REMOVE(&list->el_entries, ep, ee_link); + free(ep, M_EVENTHANDLER); + } + } else { + CTR2(KTR_EVH, "%s: marking all items from \"%s\" as dead", + __func__, list->el_name); + TAILQ_FOREACH(ep, &list->el_entries, ee_link) + ep->ee_priority = EHE_DEAD_PRIORITY; } } - EHE_UNLOCK(list); + EHL_UNLOCK(list); } +/* + * Internal version for use when eventhandler list is already locked. + */ +static struct eventhandler_list * +_eventhandler_find_list(char *name) +{ + struct eventhandler_list *list; + + mtx_assert(&eventhandler_mutex, MA_OWNED); + TAILQ_FOREACH(list, &eventhandler_lists, el_link) { + if (!strcmp(name, list->el_name)) + break; + } + return (list); +} + +/* + * Lookup a "slow" list by name. Returns with the list locked. + */ struct eventhandler_list * eventhandler_find_list(char *name) { struct eventhandler_list *list; if (!eventhandler_lists_initted) return(NULL); /* scan looking for the requested list */ mtx_lock(&eventhandler_mutex); - for (list = TAILQ_FIRST(&eventhandler_lists); - list != NULL; - list = TAILQ_NEXT(list, el_link)) { - if (!strcmp(name, list->el_name)) - break; - } + list = _eventhandler_find_list(name); + if (list != NULL) + EHL_LOCK(list); mtx_unlock(&eventhandler_mutex); return(list); } +/* + * Prune "dead" entries from an eventhandler list. + */ +void +eventhandler_prune_list(struct eventhandler_list *list) +{ + struct eventhandler_entry *ep, *en; + + CTR2(KTR_EVH, "%s: pruning list \"%s\"", __func__, list->el_name); + EHL_LOCK_ASSERT(list, MA_OWNED); + ep = TAILQ_FIRST(&list->el_entries); + while (ep != NULL) { + en = TAILQ_NEXT(ep, ee_link); + if (ep->ee_priority == EHE_DEAD_PRIORITY) { + TAILQ_REMOVE(&list->el_entries, ep, ee_link); + free(ep, M_EVENTHANDLER); + } + ep = en; + } +} diff --git a/sys/sys/eventhandler.h b/sys/sys/eventhandler.h index 13782179d733..a1689287a52a 100644 --- a/sys/sys/eventhandler.h +++ b/sys/sys/eventhandler.h @@ -1,187 +1,206 @@ /*- * Copyright (c) 1999 Michael Smith * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef SYS_EVENTHANDLER_H #define SYS_EVENTHANDLER_H #include -#include +#include +#include #include struct eventhandler_entry { TAILQ_ENTRY(eventhandler_entry) ee_link; int ee_priority; +#define EHE_DEAD_PRIORITY (-1) void *ee_arg; }; struct eventhandler_list { char *el_name; int el_flags; -#define EHE_INITTED (1<<0) - struct sx el_lock; +#define EHL_INITTED (1<<0) + u_int el_runcount; + struct mtx el_lock; TAILQ_ENTRY(eventhandler_list) el_link; TAILQ_HEAD(,eventhandler_entry) el_entries; }; typedef struct eventhandler_entry *eventhandler_tag; -#define EHE_LOCK(p) sx_xlock(&(p)->el_lock) -#define EHE_UNLOCK(p) sx_xunlock(&(p)->el_lock) +#define EHL_LOCK(p) mtx_lock(&(p)->el_lock) +#define EHL_UNLOCK(p) mtx_unlock(&(p)->el_lock) +#define EHL_LOCK_ASSERT(p, x) mtx_assert(&(p)->el_lock, x) + +/* + * Macro to invoke the handlers for a given event. + */ +#define _EVENTHANDLER_INVOKE(name, list, ...) do { \ + struct eventhandler_entry *_ep; \ + struct eventhandler_entry_ ## name *_t; \ + \ + KASSERT((list)->el_flags & EHL_INITTED, \ + ("eventhandler_invoke: running non-inited list")); \ + EHL_LOCK_ASSERT((list), MA_OWNED); \ + (list)->el_runcount++; \ + KASSERT((list)->el_runcount > 0, \ + ("eventhandler_invoke: runcount overflow")); \ + CTR0(KTR_EVH, "eventhandler_invoke(\"" __STRING(name) "\")"); \ + TAILQ_FOREACH(_ep, &((list)->el_entries), ee_link) { \ + if (_ep->ee_priority != EHE_DEAD_PRIORITY) { \ + EHL_UNLOCK((list)); \ + _t = (struct eventhandler_entry_ ## name *)_ep; \ + CTR1(KTR_EVH, "eventhandler_invoke: executing %p", \ + (void *)_t->eh_func); \ + _t->eh_func(_ep->ee_arg , ## __VA_ARGS__); \ + EHL_LOCK((list)); \ + } \ + } \ + KASSERT((list)->el_runcount > 0, \ + ("eventhandler_invoke: runcount underflow")); \ + (list)->el_runcount--; \ + if ((list)->el_runcount == 0) \ + eventhandler_prune_list(list); \ + EHL_UNLOCK((list)); \ +} while (0) + /* * Fast handler lists require the eventhandler list be present * at link time. They don't allow addition of entries to * unknown eventhandler lists, ie. each list must have an * "owner". * * Fast handler lists must be defined once by the owner * of the eventhandler list, and the declaration must be in * scope at any point the list is manipulated. */ #define EVENTHANDLER_FAST_DECLARE(name, type) \ extern struct eventhandler_list Xeventhandler_list_ ## name ; \ struct eventhandler_entry_ ## name { \ struct eventhandler_entry ee; \ type eh_func; \ }; \ struct __hack #define EVENTHANDLER_FAST_DEFINE(name, type) \ struct eventhandler_list Xeventhandler_list_ ## name = { #name }; \ struct __hack -#define EVENTHANDLER_FAST_INVOKE(name, ...) \ -do { \ +#define EVENTHANDLER_FAST_INVOKE(name, ...) do { \ struct eventhandler_list *_el = &Xeventhandler_list_ ## name ; \ - struct eventhandler_entry *_ep, *_en; \ - struct eventhandler_entry_ ## name *_t; \ \ - if (_el->el_flags & EHE_INITTED) { \ - EHE_LOCK(_el); \ - _ep = TAILQ_FIRST(&(_el->el_entries)); \ - while (_ep != NULL) { \ - _en = TAILQ_NEXT(_ep, ee_link); \ - _t = (struct eventhandler_entry_ ## name *)_ep; \ - _t->eh_func(_ep->ee_arg , __VA_ARGS__); \ - _ep = _en; \ - } \ - EHE_UNLOCK(_el); \ + if (_el->el_flags & EHL_INITTED) { \ + EHL_LOCK(_el); \ + _EVENTHANDLER_INVOKE(name, _el , ## __VA_ARGS__); \ } \ } while (0) #define EVENTHANDLER_FAST_REGISTER(name, func, arg, priority) \ eventhandler_register(&Xeventhandler_list_ ## name, \ #name, func, arg, priority) -#define EVENTHANDLER_FAST_DEREGISTER(name, tag) \ - eventhandler_deregister(&Xeventhandler_list_ ## name, tag) +#define EVENTHANDLER_FAST_DEREGISTER(name, tag) do { \ + struct eventhandler_list *_el = &Xeventhandler_list_ ## name ; \ + \ + KASSERT(_el->el_flags & EHL_INITTED, \ + ("eventhandler_fast_deregister on un-inited list %s", ## name)); \ + EHL_LOCK(_el); \ + eventhandler_deregister(_el, tag); \ +} while (0) /* * Slow handlers are entirely dynamic; lists are created * when entries are added to them, and thus have no concept of "owner", * * Slow handlers need to be declared, but do not need to be defined. The * declaration must be in scope wherever the handler is to be invoked. */ #define EVENTHANDLER_DECLARE(name, type) \ struct eventhandler_entry_ ## name \ { \ struct eventhandler_entry ee; \ type eh_func; \ }; \ struct __hack #define EVENTHANDLER_INVOKE(name, ...) \ do { \ struct eventhandler_list *_el; \ - struct eventhandler_entry *_ep, *_en; \ - struct eventhandler_entry_ ## name *_t; \ \ - if (((_el = eventhandler_find_list(#name)) != NULL) && \ - (_el->el_flags & EHE_INITTED)) { \ - EHE_LOCK(_el); \ - _ep = TAILQ_FIRST(&(_el->el_entries)); \ - while (_ep != NULL) { \ - _en = TAILQ_NEXT(_ep, ee_link); \ - _t = (struct eventhandler_entry_ ## name *)_ep; \ - _t->eh_func(_ep->ee_arg , __VA_ARGS__); \ - _ep = _en; \ - } \ - EHE_UNLOCK(_el); \ - } \ + if ((_el = eventhandler_find_list(#name)) != NULL) \ + _EVENTHANDLER_INVOKE(name, _el , ## __VA_ARGS__); \ } while (0) #define EVENTHANDLER_REGISTER(name, func, arg, priority) \ eventhandler_register(NULL, #name, func, arg, priority) #define EVENTHANDLER_DEREGISTER(name, tag) \ do { \ struct eventhandler_list *_el; \ \ if ((_el = eventhandler_find_list(#name)) != NULL) \ eventhandler_deregister(_el, tag); \ } while(0) -extern eventhandler_tag eventhandler_register(struct eventhandler_list *list, - char *name, - void *func, - void *arg, - int priority); -extern void eventhandler_deregister(struct eventhandler_list *list, - eventhandler_tag tag); -extern struct eventhandler_list *eventhandler_find_list(char *name); +eventhandler_tag eventhandler_register(struct eventhandler_list *list, + char *name, void *func, void *arg, int priority); +void eventhandler_deregister(struct eventhandler_list *list, + eventhandler_tag tag); +struct eventhandler_list *eventhandler_find_list(char *name); +void eventhandler_prune_list(struct eventhandler_list *list); /* * Standard system event queues. */ /* Shutdown events */ typedef void (*shutdown_fn)(void *, int); #define SHUTDOWN_PRI_FIRST 0 #define SHUTDOWN_PRI_DEFAULT 10000 #define SHUTDOWN_PRI_LAST 20000 EVENTHANDLER_DECLARE(shutdown_pre_sync, shutdown_fn); /* before fs sync */ EVENTHANDLER_DECLARE(shutdown_post_sync, shutdown_fn); /* after fs sync */ EVENTHANDLER_DECLARE(shutdown_final, shutdown_fn); /* Idle process event */ typedef void (*idle_eventhandler_t)(void *, int); #define IDLE_PRI_FIRST 10000 #define IDLE_PRI_LAST 20000 EVENTHANDLER_FAST_DECLARE(idle_event, idle_eventhandler_t); /* Low memory event */ typedef void (*vm_lowmem_handler_t)(void *, int); #define LOWMEM_PRI_DEFAULT 0 EVENTHANDLER_DECLARE(vm_lowmem, vm_lowmem_handler_t); #endif /* SYS_EVENTHANDLER_H */