xref: /freebsd/sys/kern/subr_eventhandler.c (revision daec92844e8fef4d0a629c70443043691e59530c)
1fcb893a8SMike Smith /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
4fcb893a8SMike Smith  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5fcb893a8SMike Smith  * All rights reserved.
6fcb893a8SMike Smith  *
7fcb893a8SMike Smith  * Redistribution and use in source and binary forms, with or without
8fcb893a8SMike Smith  * modification, are permitted provided that the following conditions
9fcb893a8SMike Smith  * are met:
10fcb893a8SMike Smith  * 1. Redistributions of source code must retain the above copyright
11fcb893a8SMike Smith  *    notice, this list of conditions and the following disclaimer.
12fcb893a8SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
13fcb893a8SMike Smith  *    notice, this list of conditions and the following disclaimer in the
14fcb893a8SMike Smith  *    documentation and/or other materials provided with the distribution.
15fcb893a8SMike Smith  *
16fcb893a8SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17fcb893a8SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18fcb893a8SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19fcb893a8SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20fcb893a8SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21fcb893a8SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22fcb893a8SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23fcb893a8SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24fcb893a8SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25fcb893a8SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26fcb893a8SMike Smith  * SUCH DAMAGE.
27fcb893a8SMike Smith  */
28fcb893a8SMike Smith 
29677b542eSDavid E. O'Brien #include <sys/cdefs.h>
30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
31677b542eSDavid E. O'Brien 
32fcb893a8SMike Smith #include <sys/param.h>
33fcb893a8SMike Smith #include <sys/kernel.h>
34*daec9284SConrad Meyer #include <sys/ktr.h>
35f34fa851SJohn Baldwin #include <sys/lock.h>
36fcb893a8SMike Smith #include <sys/malloc.h>
3706592dd1SJohn Baldwin #include <sys/mutex.h>
38fb919e4dSMark Murray #include <sys/proc.h>
39fcb893a8SMike Smith #include <sys/systm.h>
40fcb893a8SMike Smith #include <sys/eventhandler.h>
41fcb893a8SMike Smith 
42959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
43fcb893a8SMike Smith 
442ca45184SMatt Joras /* List of all eventhandler lists */
45e3975643SJake Burkholder static TAILQ_HEAD(, eventhandler_list)	eventhandler_lists;
46fcb893a8SMike Smith static int				eventhandler_lists_initted = 0;
47d543796fSJohn Baldwin static struct mtx			eventhandler_mutex;
48fcb893a8SMike Smith 
49fcb893a8SMike Smith struct eventhandler_entry_generic
50fcb893a8SMike Smith {
51fcb893a8SMike Smith     struct eventhandler_entry	ee;
52fcb893a8SMike Smith     void			(* func)(void);
53fcb893a8SMike Smith };
54fcb893a8SMike Smith 
55fdf20233SJoseph Koshy static struct eventhandler_list *_eventhandler_find_list(const char *name);
56ecdf4409SJohn Baldwin 
57fcb893a8SMike Smith /*
5806592dd1SJohn Baldwin  * Initialize the eventhandler mutex and list.
5906592dd1SJohn Baldwin  */
6006592dd1SJohn Baldwin static void
6106592dd1SJohn Baldwin eventhandler_init(void *dummy __unused)
6206592dd1SJohn Baldwin {
6306592dd1SJohn Baldwin     TAILQ_INIT(&eventhandler_lists);
64ecdf4409SJohn Baldwin     mtx_init(&eventhandler_mutex, "eventhandler", NULL, MTX_DEF);
65ecdf4409SJohn Baldwin     atomic_store_rel_int(&eventhandler_lists_initted, 1);
6606592dd1SJohn Baldwin }
6706592dd1SJohn Baldwin SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
68237fdd78SRobert Watson     NULL);
6906592dd1SJohn Baldwin 
702ca45184SMatt Joras static struct eventhandler_list *
712ca45184SMatt Joras eventhandler_find_or_create_list(const char *name)
72fcb893a8SMike Smith {
732ca45184SMatt Joras 	struct eventhandler_list *list, *new_list;
74fcb893a8SMike Smith 
75fcb893a8SMike Smith 	/* look for a matching, existing list */
76ecdf4409SJohn Baldwin 	list = _eventhandler_find_list(name);
77fcb893a8SMike Smith 
78fcb893a8SMike Smith 	/* Do we need to create the list? */
79fcb893a8SMike Smith 	if (list == NULL) {
809ed346baSBosko Milekic 	    mtx_unlock(&eventhandler_mutex);
81ecdf4409SJohn Baldwin 
822ca45184SMatt Joras 	    new_list = malloc(sizeof(*new_list) + strlen(name) + 1,
832ca45184SMatt Joras 		M_EVENTHANDLER, M_WAITOK | M_ZERO);
84ecdf4409SJohn Baldwin 
85ecdf4409SJohn Baldwin 	    /* If someone else created it already, then use that one. */
86ecdf4409SJohn Baldwin 	    mtx_lock(&eventhandler_mutex);
87ecdf4409SJohn Baldwin 	    list = _eventhandler_find_list(name);
88ecdf4409SJohn Baldwin 	    if (list != NULL) {
89ecdf4409SJohn Baldwin 		free(new_list, M_EVENTHANDLER);
90ecdf4409SJohn Baldwin 	    } else {
91ecdf4409SJohn Baldwin 		CTR2(KTR_EVH, "%s: creating list \"%s\"", __func__, name);
92ecdf4409SJohn Baldwin 		list = new_list;
932ca45184SMatt Joras 		TAILQ_INIT(&list->el_entries);
942ca45184SMatt Joras 		list->el_name = (char *)(list + 1);
95fcb893a8SMike Smith 		strcpy(list->el_name, name);
96d6160f60SMatt Joras 		mtx_init(&list->el_lock, list->el_name, "eventhandler list",
97d6160f60SMatt Joras 		    MTX_DEF);
98fcb893a8SMike Smith 		TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
99fcb893a8SMike Smith 	    }
100fcb893a8SMike Smith 	}
1012ca45184SMatt Joras 	return (list);
102ecdf4409SJohn Baldwin }
1032ca45184SMatt Joras 
1042ca45184SMatt Joras /*
1052ca45184SMatt Joras  * Insertion is O(n) due to the priority scan, but optimises to O(1)
1062ca45184SMatt Joras  * if all priorities are identical.
1072ca45184SMatt Joras  */
1082ca45184SMatt Joras static eventhandler_tag
1092ca45184SMatt Joras eventhandler_register_internal(struct eventhandler_list *list,
1102ca45184SMatt Joras     const char *name, eventhandler_tag epn)
1112ca45184SMatt Joras {
1122ca45184SMatt Joras     struct eventhandler_entry		*ep;
1132ca45184SMatt Joras 
1142ca45184SMatt Joras     KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
1152ca45184SMatt Joras     KASSERT(epn != NULL, ("%s: cannot register NULL event", __func__));
1162ca45184SMatt Joras 
1172ca45184SMatt Joras     /* Do we need to find/create the list? */
1182ca45184SMatt Joras     if (list == NULL) {
1192ca45184SMatt Joras 	    mtx_lock(&eventhandler_mutex);
1202ca45184SMatt Joras 	    list = eventhandler_find_or_create_list(name);
12119a0f7e1SAlfred Perlstein 	    mtx_unlock(&eventhandler_mutex);
1222ca45184SMatt Joras     }
123fcb893a8SMike Smith 
12442eedeacSBjoern A. Zeeb     KASSERT(epn->ee_priority != EHE_DEAD_PRIORITY,
12542eedeacSBjoern A. Zeeb 	("%s: handler for %s registered with dead priority", __func__, name));
12642eedeacSBjoern A. Zeeb 
12742eedeacSBjoern A. Zeeb     /* sort it into the list */
12842eedeacSBjoern A. Zeeb     CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, epn,
12942eedeacSBjoern A. Zeeb 	((struct eventhandler_entry_generic *)epn)->func, name);
13042eedeacSBjoern A. Zeeb     EHL_LOCK(list);
13142eedeacSBjoern A. Zeeb     TAILQ_FOREACH(ep, &list->el_entries, ee_link) {
13242eedeacSBjoern A. Zeeb 	if (ep->ee_priority != EHE_DEAD_PRIORITY &&
13342eedeacSBjoern A. Zeeb 	    epn->ee_priority < ep->ee_priority) {
13442eedeacSBjoern A. Zeeb 	    TAILQ_INSERT_BEFORE(ep, epn, ee_link);
13542eedeacSBjoern A. Zeeb 	    break;
13642eedeacSBjoern A. Zeeb 	}
13742eedeacSBjoern A. Zeeb     }
13842eedeacSBjoern A. Zeeb     if (ep == NULL)
13942eedeacSBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&list->el_entries, epn, ee_link);
14042eedeacSBjoern A. Zeeb     EHL_UNLOCK(list);
14142eedeacSBjoern A. Zeeb     return(epn);
14242eedeacSBjoern A. Zeeb }
14342eedeacSBjoern A. Zeeb 
14442eedeacSBjoern A. Zeeb eventhandler_tag
14542eedeacSBjoern A. Zeeb eventhandler_register(struct eventhandler_list *list, const char *name,
14642eedeacSBjoern A. Zeeb 		      void *func, void *arg, int priority)
14742eedeacSBjoern A. Zeeb {
14842eedeacSBjoern A. Zeeb     struct eventhandler_entry_generic	*eg;
14942eedeacSBjoern A. Zeeb 
150fcb893a8SMike Smith     /* allocate an entry for this handler, populate it */
151ecdf4409SJohn Baldwin     eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER,
152ecdf4409SJohn Baldwin 	M_WAITOK | M_ZERO);
153fcb893a8SMike Smith     eg->func = func;
154fcb893a8SMike Smith     eg->ee.ee_arg = arg;
155fcb893a8SMike Smith     eg->ee.ee_priority = priority;
156fcb893a8SMike Smith 
15742eedeacSBjoern A. Zeeb     return (eventhandler_register_internal(list, name, &eg->ee));
158fcb893a8SMike Smith }
15942eedeacSBjoern A. Zeeb 
16042eedeacSBjoern A. Zeeb #ifdef VIMAGE
16142eedeacSBjoern A. Zeeb struct eventhandler_entry_generic_vimage
16242eedeacSBjoern A. Zeeb {
16342eedeacSBjoern A. Zeeb     struct eventhandler_entry		ee;
16442eedeacSBjoern A. Zeeb     vimage_iterator_func_t		func;		/* Vimage iterator function. */
16542eedeacSBjoern A. Zeeb     struct eventhandler_entry_vimage	v_ee;		/* Original func, arg. */
16642eedeacSBjoern A. Zeeb };
16742eedeacSBjoern A. Zeeb 
16842eedeacSBjoern A. Zeeb eventhandler_tag
16942eedeacSBjoern A. Zeeb vimage_eventhandler_register(struct eventhandler_list *list, const char *name,
17042eedeacSBjoern A. Zeeb     void *func, void *arg, int priority, vimage_iterator_func_t iterfunc)
17142eedeacSBjoern A. Zeeb {
17242eedeacSBjoern A. Zeeb     struct eventhandler_entry_generic_vimage	*eg;
17342eedeacSBjoern A. Zeeb 
17442eedeacSBjoern A. Zeeb     /* allocate an entry for this handler, populate it */
17542eedeacSBjoern A. Zeeb     eg = malloc(sizeof(struct eventhandler_entry_generic_vimage),
17642eedeacSBjoern A. Zeeb 	M_EVENTHANDLER, M_WAITOK | M_ZERO);
17742eedeacSBjoern A. Zeeb     eg->func = iterfunc;
17842eedeacSBjoern A. Zeeb     eg->v_ee.func = func;
17942eedeacSBjoern A. Zeeb     eg->v_ee.ee_arg = arg;
18042eedeacSBjoern A. Zeeb     eg->ee.ee_arg = &eg->v_ee;
18142eedeacSBjoern A. Zeeb     eg->ee.ee_priority = priority;
18242eedeacSBjoern A. Zeeb 
18342eedeacSBjoern A. Zeeb     return (eventhandler_register_internal(list, name, &eg->ee));
184fcb893a8SMike Smith }
18542eedeacSBjoern A. Zeeb #endif
186fcb893a8SMike Smith 
187fc091646SIan Lepore static void
188fc091646SIan Lepore _eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag,
189fc091646SIan Lepore     bool wait)
190fcb893a8SMike Smith {
191fcb893a8SMike Smith     struct eventhandler_entry	*ep = tag;
192fcb893a8SMike Smith 
193ecdf4409SJohn Baldwin     EHL_LOCK_ASSERT(list, MA_OWNED);
194fcb893a8SMike Smith     if (ep != NULL) {
195fcb893a8SMike Smith 	/* remove just this entry */
196ecdf4409SJohn Baldwin 	if (list->el_runcount == 0) {
197ecdf4409SJohn Baldwin 	    CTR3(KTR_EVH, "%s: removing item %p from \"%s\"", __func__, ep,
198ecdf4409SJohn Baldwin 		list->el_name);
199fcb893a8SMike Smith 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
200fcb893a8SMike Smith 	    free(ep, M_EVENTHANDLER);
201fcb893a8SMike Smith 	} else {
202ecdf4409SJohn Baldwin 	    CTR3(KTR_EVH, "%s: marking item %p from \"%s\" as dead", __func__,
203ecdf4409SJohn Baldwin 		ep, list->el_name);
204ecdf4409SJohn Baldwin 	    ep->ee_priority = EHE_DEAD_PRIORITY;
205ecdf4409SJohn Baldwin 	}
206ecdf4409SJohn Baldwin     } else {
207fcb893a8SMike Smith 	/* remove entire list */
208ecdf4409SJohn Baldwin 	if (list->el_runcount == 0) {
209ecdf4409SJohn Baldwin 	    CTR2(KTR_EVH, "%s: removing all items from \"%s\"", __func__,
210ecdf4409SJohn Baldwin 		list->el_name);
211fcb893a8SMike Smith 	    while (!TAILQ_EMPTY(&list->el_entries)) {
212fcb893a8SMike Smith 		ep = TAILQ_FIRST(&list->el_entries);
2131b727751SPoul-Henning Kamp 		TAILQ_REMOVE(&list->el_entries, ep, ee_link);
214fcb893a8SMike Smith 		free(ep, M_EVENTHANDLER);
215fcb893a8SMike Smith 	    }
216ecdf4409SJohn Baldwin 	} else {
217ecdf4409SJohn Baldwin 	    CTR2(KTR_EVH, "%s: marking all items from \"%s\" as dead",
218ecdf4409SJohn Baldwin 		__func__, list->el_name);
219ecdf4409SJohn Baldwin 	    TAILQ_FOREACH(ep, &list->el_entries, ee_link)
220ecdf4409SJohn Baldwin 		ep->ee_priority = EHE_DEAD_PRIORITY;
221fcb893a8SMike Smith 	}
222ecdf4409SJohn Baldwin     }
223fc091646SIan Lepore     while (wait && list->el_runcount > 0)
2242b543150SAndrew Thompson 	    mtx_sleep(list, &list->el_lock, 0, "evhrm", 0);
225ecdf4409SJohn Baldwin     EHL_UNLOCK(list);
226fcb893a8SMike Smith }
227fcb893a8SMike Smith 
228fc091646SIan Lepore void
229fc091646SIan Lepore eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
230fc091646SIan Lepore {
231fc091646SIan Lepore 
232fc091646SIan Lepore 	_eventhandler_deregister(list, tag, true);
233fc091646SIan Lepore }
234fc091646SIan Lepore 
235fc091646SIan Lepore void
236fc091646SIan Lepore eventhandler_deregister_nowait(struct eventhandler_list *list,
237fc091646SIan Lepore     eventhandler_tag tag)
238fc091646SIan Lepore {
239fc091646SIan Lepore 
240fc091646SIan Lepore 	_eventhandler_deregister(list, tag, false);
241fc091646SIan Lepore }
242fc091646SIan Lepore 
243ecdf4409SJohn Baldwin /*
244ecdf4409SJohn Baldwin  * Internal version for use when eventhandler list is already locked.
245ecdf4409SJohn Baldwin  */
246ecdf4409SJohn Baldwin static struct eventhandler_list *
247fdf20233SJoseph Koshy _eventhandler_find_list(const char *name)
248ecdf4409SJohn Baldwin {
249ecdf4409SJohn Baldwin     struct eventhandler_list	*list;
250ecdf4409SJohn Baldwin 
251ecdf4409SJohn Baldwin     mtx_assert(&eventhandler_mutex, MA_OWNED);
252ecdf4409SJohn Baldwin     TAILQ_FOREACH(list, &eventhandler_lists, el_link) {
253ecdf4409SJohn Baldwin 	if (!strcmp(name, list->el_name))
254ecdf4409SJohn Baldwin 	    break;
255ecdf4409SJohn Baldwin     }
256ecdf4409SJohn Baldwin     return (list);
257ecdf4409SJohn Baldwin }
258ecdf4409SJohn Baldwin 
259ecdf4409SJohn Baldwin /*
260ecdf4409SJohn Baldwin  * Lookup a "slow" list by name.  Returns with the list locked.
261ecdf4409SJohn Baldwin  */
262fcb893a8SMike Smith struct eventhandler_list *
263fdf20233SJoseph Koshy eventhandler_find_list(const char *name)
264fcb893a8SMike Smith {
265fcb893a8SMike Smith     struct eventhandler_list	*list;
266146be906SJake Burkholder 
267146be906SJake Burkholder     if (!eventhandler_lists_initted)
268146be906SJake Burkholder 	return(NULL);
269fcb893a8SMike Smith 
270fcb893a8SMike Smith     /* scan looking for the requested list */
2719ed346baSBosko Milekic     mtx_lock(&eventhandler_mutex);
272ecdf4409SJohn Baldwin     list = _eventhandler_find_list(name);
273ecdf4409SJohn Baldwin     if (list != NULL)
274ecdf4409SJohn Baldwin 	EHL_LOCK(list);
2759ed346baSBosko Milekic     mtx_unlock(&eventhandler_mutex);
2766595c331SMike Smith 
277fcb893a8SMike Smith     return(list);
278fcb893a8SMike Smith }
2796595c331SMike Smith 
280ecdf4409SJohn Baldwin /*
281ecdf4409SJohn Baldwin  * Prune "dead" entries from an eventhandler list.
282ecdf4409SJohn Baldwin  */
283ecdf4409SJohn Baldwin void
284ecdf4409SJohn Baldwin eventhandler_prune_list(struct eventhandler_list *list)
285ecdf4409SJohn Baldwin {
286ecdf4409SJohn Baldwin     struct eventhandler_entry *ep, *en;
2872b543150SAndrew Thompson     int pruned = 0;
288ecdf4409SJohn Baldwin 
289ecdf4409SJohn Baldwin     CTR2(KTR_EVH, "%s: pruning list \"%s\"", __func__, list->el_name);
290ecdf4409SJohn Baldwin     EHL_LOCK_ASSERT(list, MA_OWNED);
2912b543150SAndrew Thompson     TAILQ_FOREACH_SAFE(ep, &list->el_entries, ee_link, en) {
292ecdf4409SJohn Baldwin 	if (ep->ee_priority == EHE_DEAD_PRIORITY) {
293ecdf4409SJohn Baldwin 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
294ecdf4409SJohn Baldwin 	    free(ep, M_EVENTHANDLER);
2952b543150SAndrew Thompson 	    pruned++;
296ecdf4409SJohn Baldwin 	}
297ecdf4409SJohn Baldwin     }
2982b543150SAndrew Thompson     if (pruned > 0)
2992b543150SAndrew Thompson 	    wakeup(list);
300ecdf4409SJohn Baldwin }
3012ca45184SMatt Joras 
3022ca45184SMatt Joras /*
3032ca45184SMatt Joras  * Create (or get the existing) list so the pointer can be stored by
3042ca45184SMatt Joras  * EVENTHANDLER_LIST_DEFINE.
3052ca45184SMatt Joras  */
3062ca45184SMatt Joras struct eventhandler_list *
3072ca45184SMatt Joras eventhandler_create_list(const char *name)
3082ca45184SMatt Joras {
3092ca45184SMatt Joras 	struct eventhandler_list *list;
3102ca45184SMatt Joras 
3112ca45184SMatt Joras 	KASSERT(eventhandler_lists_initted,
3122ca45184SMatt Joras 	    ("eventhandler list created too early"));
3132ca45184SMatt Joras 
3142ca45184SMatt Joras 	mtx_lock(&eventhandler_mutex);
3152ca45184SMatt Joras 	list = eventhandler_find_or_create_list(name);
3162ca45184SMatt Joras 	mtx_unlock(&eventhandler_mutex);
3172ca45184SMatt Joras 
3182ca45184SMatt Joras 	return (list);
3192ca45184SMatt Joras }
320