xref: /freebsd/sys/kern/subr_eventhandler.c (revision 8a36da99deb0e19363ec04e4d3facd869c1028f5)
1fcb893a8SMike Smith /*-
2*8a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*8a36da99SPedro 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>
34f34fa851SJohn Baldwin #include <sys/lock.h>
35fcb893a8SMike Smith #include <sys/malloc.h>
3606592dd1SJohn Baldwin #include <sys/mutex.h>
37fb919e4dSMark Murray #include <sys/proc.h>
38fcb893a8SMike Smith #include <sys/systm.h>
39fcb893a8SMike Smith #include <sys/eventhandler.h>
40fcb893a8SMike Smith 
41959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
42fcb893a8SMike Smith 
432ca45184SMatt Joras /* List of all eventhandler lists */
44e3975643SJake Burkholder static TAILQ_HEAD(, eventhandler_list)	eventhandler_lists;
45fcb893a8SMike Smith static int				eventhandler_lists_initted = 0;
46d543796fSJohn Baldwin static struct mtx			eventhandler_mutex;
47fcb893a8SMike Smith 
48fcb893a8SMike Smith struct eventhandler_entry_generic
49fcb893a8SMike Smith {
50fcb893a8SMike Smith     struct eventhandler_entry	ee;
51fcb893a8SMike Smith     void			(* func)(void);
52fcb893a8SMike Smith };
53fcb893a8SMike Smith 
54fdf20233SJoseph Koshy static struct eventhandler_list *_eventhandler_find_list(const char *name);
55ecdf4409SJohn Baldwin 
56fcb893a8SMike Smith /*
5706592dd1SJohn Baldwin  * Initialize the eventhandler mutex and list.
5806592dd1SJohn Baldwin  */
5906592dd1SJohn Baldwin static void
6006592dd1SJohn Baldwin eventhandler_init(void *dummy __unused)
6106592dd1SJohn Baldwin {
6206592dd1SJohn Baldwin     TAILQ_INIT(&eventhandler_lists);
63ecdf4409SJohn Baldwin     mtx_init(&eventhandler_mutex, "eventhandler", NULL, MTX_DEF);
64ecdf4409SJohn Baldwin     atomic_store_rel_int(&eventhandler_lists_initted, 1);
6506592dd1SJohn Baldwin }
6606592dd1SJohn Baldwin SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
67237fdd78SRobert Watson     NULL);
6806592dd1SJohn Baldwin 
692ca45184SMatt Joras static struct eventhandler_list *
702ca45184SMatt Joras eventhandler_find_or_create_list(const char *name)
71fcb893a8SMike Smith {
722ca45184SMatt Joras 	struct eventhandler_list *list, *new_list;
73fcb893a8SMike Smith 
74fcb893a8SMike Smith 	/* look for a matching, existing list */
75ecdf4409SJohn Baldwin 	list = _eventhandler_find_list(name);
76fcb893a8SMike Smith 
77fcb893a8SMike Smith 	/* Do we need to create the list? */
78fcb893a8SMike Smith 	if (list == NULL) {
799ed346baSBosko Milekic 	    mtx_unlock(&eventhandler_mutex);
80ecdf4409SJohn Baldwin 
812ca45184SMatt Joras 	    new_list = malloc(sizeof(*new_list) + strlen(name) + 1,
822ca45184SMatt Joras 		M_EVENTHANDLER, M_WAITOK | M_ZERO);
83ecdf4409SJohn Baldwin 
84ecdf4409SJohn Baldwin 	    /* If someone else created it already, then use that one. */
85ecdf4409SJohn Baldwin 	    mtx_lock(&eventhandler_mutex);
86ecdf4409SJohn Baldwin 	    list = _eventhandler_find_list(name);
87ecdf4409SJohn Baldwin 	    if (list != NULL) {
88ecdf4409SJohn Baldwin 		free(new_list, M_EVENTHANDLER);
89ecdf4409SJohn Baldwin 	    } else {
90ecdf4409SJohn Baldwin 		CTR2(KTR_EVH, "%s: creating list \"%s\"", __func__, name);
91ecdf4409SJohn Baldwin 		list = new_list;
922ca45184SMatt Joras 		TAILQ_INIT(&list->el_entries);
932ca45184SMatt Joras 		mtx_init(&list->el_lock, name, "eventhandler list", MTX_DEF);
942ca45184SMatt Joras 		list->el_name = (char *)(list + 1);
95fcb893a8SMike Smith 		strcpy(list->el_name, name);
96fcb893a8SMike Smith 		TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
97fcb893a8SMike Smith 	    }
98fcb893a8SMike Smith 	}
992ca45184SMatt Joras 	return (list);
100ecdf4409SJohn Baldwin }
1012ca45184SMatt Joras 
1022ca45184SMatt Joras /*
1032ca45184SMatt Joras  * Insertion is O(n) due to the priority scan, but optimises to O(1)
1042ca45184SMatt Joras  * if all priorities are identical.
1052ca45184SMatt Joras  */
1062ca45184SMatt Joras static eventhandler_tag
1072ca45184SMatt Joras eventhandler_register_internal(struct eventhandler_list *list,
1082ca45184SMatt Joras     const char *name, eventhandler_tag epn)
1092ca45184SMatt Joras {
1102ca45184SMatt Joras     struct eventhandler_entry		*ep;
1112ca45184SMatt Joras 
1122ca45184SMatt Joras     KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
1132ca45184SMatt Joras     KASSERT(epn != NULL, ("%s: cannot register NULL event", __func__));
1142ca45184SMatt Joras 
1152ca45184SMatt Joras     /* Do we need to find/create the list? */
1162ca45184SMatt Joras     if (list == NULL) {
1172ca45184SMatt Joras 	    mtx_lock(&eventhandler_mutex);
1182ca45184SMatt Joras 	    list = eventhandler_find_or_create_list(name);
11919a0f7e1SAlfred Perlstein 	    mtx_unlock(&eventhandler_mutex);
1202ca45184SMatt Joras     }
121fcb893a8SMike Smith 
12242eedeacSBjoern A. Zeeb     KASSERT(epn->ee_priority != EHE_DEAD_PRIORITY,
12342eedeacSBjoern A. Zeeb 	("%s: handler for %s registered with dead priority", __func__, name));
12442eedeacSBjoern A. Zeeb 
12542eedeacSBjoern A. Zeeb     /* sort it into the list */
12642eedeacSBjoern A. Zeeb     CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, epn,
12742eedeacSBjoern A. Zeeb 	((struct eventhandler_entry_generic *)epn)->func, name);
12842eedeacSBjoern A. Zeeb     EHL_LOCK(list);
12942eedeacSBjoern A. Zeeb     TAILQ_FOREACH(ep, &list->el_entries, ee_link) {
13042eedeacSBjoern A. Zeeb 	if (ep->ee_priority != EHE_DEAD_PRIORITY &&
13142eedeacSBjoern A. Zeeb 	    epn->ee_priority < ep->ee_priority) {
13242eedeacSBjoern A. Zeeb 	    TAILQ_INSERT_BEFORE(ep, epn, ee_link);
13342eedeacSBjoern A. Zeeb 	    break;
13442eedeacSBjoern A. Zeeb 	}
13542eedeacSBjoern A. Zeeb     }
13642eedeacSBjoern A. Zeeb     if (ep == NULL)
13742eedeacSBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&list->el_entries, epn, ee_link);
13842eedeacSBjoern A. Zeeb     EHL_UNLOCK(list);
13942eedeacSBjoern A. Zeeb     return(epn);
14042eedeacSBjoern A. Zeeb }
14142eedeacSBjoern A. Zeeb 
14242eedeacSBjoern A. Zeeb eventhandler_tag
14342eedeacSBjoern A. Zeeb eventhandler_register(struct eventhandler_list *list, const char *name,
14442eedeacSBjoern A. Zeeb 		      void *func, void *arg, int priority)
14542eedeacSBjoern A. Zeeb {
14642eedeacSBjoern A. Zeeb     struct eventhandler_entry_generic	*eg;
14742eedeacSBjoern A. Zeeb 
148fcb893a8SMike Smith     /* allocate an entry for this handler, populate it */
149ecdf4409SJohn Baldwin     eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER,
150ecdf4409SJohn Baldwin 	M_WAITOK | M_ZERO);
151fcb893a8SMike Smith     eg->func = func;
152fcb893a8SMike Smith     eg->ee.ee_arg = arg;
153fcb893a8SMike Smith     eg->ee.ee_priority = priority;
154fcb893a8SMike Smith 
15542eedeacSBjoern A. Zeeb     return (eventhandler_register_internal(list, name, &eg->ee));
156fcb893a8SMike Smith }
15742eedeacSBjoern A. Zeeb 
15842eedeacSBjoern A. Zeeb #ifdef VIMAGE
15942eedeacSBjoern A. Zeeb struct eventhandler_entry_generic_vimage
16042eedeacSBjoern A. Zeeb {
16142eedeacSBjoern A. Zeeb     struct eventhandler_entry		ee;
16242eedeacSBjoern A. Zeeb     vimage_iterator_func_t		func;		/* Vimage iterator function. */
16342eedeacSBjoern A. Zeeb     struct eventhandler_entry_vimage	v_ee;		/* Original func, arg. */
16442eedeacSBjoern A. Zeeb };
16542eedeacSBjoern A. Zeeb 
16642eedeacSBjoern A. Zeeb eventhandler_tag
16742eedeacSBjoern A. Zeeb vimage_eventhandler_register(struct eventhandler_list *list, const char *name,
16842eedeacSBjoern A. Zeeb     void *func, void *arg, int priority, vimage_iterator_func_t iterfunc)
16942eedeacSBjoern A. Zeeb {
17042eedeacSBjoern A. Zeeb     struct eventhandler_entry_generic_vimage	*eg;
17142eedeacSBjoern A. Zeeb 
17242eedeacSBjoern A. Zeeb     /* allocate an entry for this handler, populate it */
17342eedeacSBjoern A. Zeeb     eg = malloc(sizeof(struct eventhandler_entry_generic_vimage),
17442eedeacSBjoern A. Zeeb 	M_EVENTHANDLER, M_WAITOK | M_ZERO);
17542eedeacSBjoern A. Zeeb     eg->func = iterfunc;
17642eedeacSBjoern A. Zeeb     eg->v_ee.func = func;
17742eedeacSBjoern A. Zeeb     eg->v_ee.ee_arg = arg;
17842eedeacSBjoern A. Zeeb     eg->ee.ee_arg = &eg->v_ee;
17942eedeacSBjoern A. Zeeb     eg->ee.ee_priority = priority;
18042eedeacSBjoern A. Zeeb 
18142eedeacSBjoern A. Zeeb     return (eventhandler_register_internal(list, name, &eg->ee));
182fcb893a8SMike Smith }
18342eedeacSBjoern A. Zeeb #endif
184fcb893a8SMike Smith 
185fc091646SIan Lepore static void
186fc091646SIan Lepore _eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag,
187fc091646SIan Lepore     bool wait)
188fcb893a8SMike Smith {
189fcb893a8SMike Smith     struct eventhandler_entry	*ep = tag;
190fcb893a8SMike Smith 
191ecdf4409SJohn Baldwin     EHL_LOCK_ASSERT(list, MA_OWNED);
192fcb893a8SMike Smith     if (ep != NULL) {
193fcb893a8SMike Smith 	/* remove just this entry */
194ecdf4409SJohn Baldwin 	if (list->el_runcount == 0) {
195ecdf4409SJohn Baldwin 	    CTR3(KTR_EVH, "%s: removing item %p from \"%s\"", __func__, ep,
196ecdf4409SJohn Baldwin 		list->el_name);
197fcb893a8SMike Smith 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
198fcb893a8SMike Smith 	    free(ep, M_EVENTHANDLER);
199fcb893a8SMike Smith 	} else {
200ecdf4409SJohn Baldwin 	    CTR3(KTR_EVH, "%s: marking item %p from \"%s\" as dead", __func__,
201ecdf4409SJohn Baldwin 		ep, list->el_name);
202ecdf4409SJohn Baldwin 	    ep->ee_priority = EHE_DEAD_PRIORITY;
203ecdf4409SJohn Baldwin 	}
204ecdf4409SJohn Baldwin     } else {
205fcb893a8SMike Smith 	/* remove entire list */
206ecdf4409SJohn Baldwin 	if (list->el_runcount == 0) {
207ecdf4409SJohn Baldwin 	    CTR2(KTR_EVH, "%s: removing all items from \"%s\"", __func__,
208ecdf4409SJohn Baldwin 		list->el_name);
209fcb893a8SMike Smith 	    while (!TAILQ_EMPTY(&list->el_entries)) {
210fcb893a8SMike Smith 		ep = TAILQ_FIRST(&list->el_entries);
2111b727751SPoul-Henning Kamp 		TAILQ_REMOVE(&list->el_entries, ep, ee_link);
212fcb893a8SMike Smith 		free(ep, M_EVENTHANDLER);
213fcb893a8SMike Smith 	    }
214ecdf4409SJohn Baldwin 	} else {
215ecdf4409SJohn Baldwin 	    CTR2(KTR_EVH, "%s: marking all items from \"%s\" as dead",
216ecdf4409SJohn Baldwin 		__func__, list->el_name);
217ecdf4409SJohn Baldwin 	    TAILQ_FOREACH(ep, &list->el_entries, ee_link)
218ecdf4409SJohn Baldwin 		ep->ee_priority = EHE_DEAD_PRIORITY;
219fcb893a8SMike Smith 	}
220ecdf4409SJohn Baldwin     }
221fc091646SIan Lepore     while (wait && list->el_runcount > 0)
2222b543150SAndrew Thompson 	    mtx_sleep(list, &list->el_lock, 0, "evhrm", 0);
223ecdf4409SJohn Baldwin     EHL_UNLOCK(list);
224fcb893a8SMike Smith }
225fcb893a8SMike Smith 
226fc091646SIan Lepore void
227fc091646SIan Lepore eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
228fc091646SIan Lepore {
229fc091646SIan Lepore 
230fc091646SIan Lepore 	_eventhandler_deregister(list, tag, true);
231fc091646SIan Lepore }
232fc091646SIan Lepore 
233fc091646SIan Lepore void
234fc091646SIan Lepore eventhandler_deregister_nowait(struct eventhandler_list *list,
235fc091646SIan Lepore     eventhandler_tag tag)
236fc091646SIan Lepore {
237fc091646SIan Lepore 
238fc091646SIan Lepore 	_eventhandler_deregister(list, tag, false);
239fc091646SIan Lepore }
240fc091646SIan Lepore 
241ecdf4409SJohn Baldwin /*
242ecdf4409SJohn Baldwin  * Internal version for use when eventhandler list is already locked.
243ecdf4409SJohn Baldwin  */
244ecdf4409SJohn Baldwin static struct eventhandler_list *
245fdf20233SJoseph Koshy _eventhandler_find_list(const char *name)
246ecdf4409SJohn Baldwin {
247ecdf4409SJohn Baldwin     struct eventhandler_list	*list;
248ecdf4409SJohn Baldwin 
249ecdf4409SJohn Baldwin     mtx_assert(&eventhandler_mutex, MA_OWNED);
250ecdf4409SJohn Baldwin     TAILQ_FOREACH(list, &eventhandler_lists, el_link) {
251ecdf4409SJohn Baldwin 	if (!strcmp(name, list->el_name))
252ecdf4409SJohn Baldwin 	    break;
253ecdf4409SJohn Baldwin     }
254ecdf4409SJohn Baldwin     return (list);
255ecdf4409SJohn Baldwin }
256ecdf4409SJohn Baldwin 
257ecdf4409SJohn Baldwin /*
258ecdf4409SJohn Baldwin  * Lookup a "slow" list by name.  Returns with the list locked.
259ecdf4409SJohn Baldwin  */
260fcb893a8SMike Smith struct eventhandler_list *
261fdf20233SJoseph Koshy eventhandler_find_list(const char *name)
262fcb893a8SMike Smith {
263fcb893a8SMike Smith     struct eventhandler_list	*list;
264146be906SJake Burkholder 
265146be906SJake Burkholder     if (!eventhandler_lists_initted)
266146be906SJake Burkholder 	return(NULL);
267fcb893a8SMike Smith 
268fcb893a8SMike Smith     /* scan looking for the requested list */
2699ed346baSBosko Milekic     mtx_lock(&eventhandler_mutex);
270ecdf4409SJohn Baldwin     list = _eventhandler_find_list(name);
271ecdf4409SJohn Baldwin     if (list != NULL)
272ecdf4409SJohn Baldwin 	EHL_LOCK(list);
2739ed346baSBosko Milekic     mtx_unlock(&eventhandler_mutex);
2746595c331SMike Smith 
275fcb893a8SMike Smith     return(list);
276fcb893a8SMike Smith }
2776595c331SMike Smith 
278ecdf4409SJohn Baldwin /*
279ecdf4409SJohn Baldwin  * Prune "dead" entries from an eventhandler list.
280ecdf4409SJohn Baldwin  */
281ecdf4409SJohn Baldwin void
282ecdf4409SJohn Baldwin eventhandler_prune_list(struct eventhandler_list *list)
283ecdf4409SJohn Baldwin {
284ecdf4409SJohn Baldwin     struct eventhandler_entry *ep, *en;
2852b543150SAndrew Thompson     int pruned = 0;
286ecdf4409SJohn Baldwin 
287ecdf4409SJohn Baldwin     CTR2(KTR_EVH, "%s: pruning list \"%s\"", __func__, list->el_name);
288ecdf4409SJohn Baldwin     EHL_LOCK_ASSERT(list, MA_OWNED);
2892b543150SAndrew Thompson     TAILQ_FOREACH_SAFE(ep, &list->el_entries, ee_link, en) {
290ecdf4409SJohn Baldwin 	if (ep->ee_priority == EHE_DEAD_PRIORITY) {
291ecdf4409SJohn Baldwin 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
292ecdf4409SJohn Baldwin 	    free(ep, M_EVENTHANDLER);
2932b543150SAndrew Thompson 	    pruned++;
294ecdf4409SJohn Baldwin 	}
295ecdf4409SJohn Baldwin     }
2962b543150SAndrew Thompson     if (pruned > 0)
2972b543150SAndrew Thompson 	    wakeup(list);
298ecdf4409SJohn Baldwin }
2992ca45184SMatt Joras 
3002ca45184SMatt Joras /*
3012ca45184SMatt Joras  * Create (or get the existing) list so the pointer can be stored by
3022ca45184SMatt Joras  * EVENTHANDLER_LIST_DEFINE.
3032ca45184SMatt Joras  */
3042ca45184SMatt Joras struct eventhandler_list *
3052ca45184SMatt Joras eventhandler_create_list(const char *name)
3062ca45184SMatt Joras {
3072ca45184SMatt Joras 	struct eventhandler_list *list;
3082ca45184SMatt Joras 
3092ca45184SMatt Joras 	KASSERT(eventhandler_lists_initted,
3102ca45184SMatt Joras 	    ("eventhandler list created too early"));
3112ca45184SMatt Joras 
3122ca45184SMatt Joras 	mtx_lock(&eventhandler_mutex);
3132ca45184SMatt Joras 	list = eventhandler_find_or_create_list(name);
3142ca45184SMatt Joras 	mtx_unlock(&eventhandler_mutex);
3152ca45184SMatt Joras 
3162ca45184SMatt Joras 	return (list);
3172ca45184SMatt Joras }
318