xref: /freebsd/sys/kern/subr_eventhandler.c (revision fdafd315)
1fcb893a8SMike Smith /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 
29fcb893a8SMike Smith #include <sys/param.h>
30fcb893a8SMike Smith #include <sys/kernel.h>
31daec9284SConrad Meyer #include <sys/ktr.h>
32f34fa851SJohn Baldwin #include <sys/lock.h>
33fcb893a8SMike Smith #include <sys/malloc.h>
3406592dd1SJohn Baldwin #include <sys/mutex.h>
35fb919e4dSMark Murray #include <sys/proc.h>
36fcb893a8SMike Smith #include <sys/systm.h>
37fcb893a8SMike Smith #include <sys/eventhandler.h>
38fcb893a8SMike Smith 
39959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
40fcb893a8SMike Smith 
412ca45184SMatt Joras /* List of all eventhandler lists */
42e3975643SJake Burkholder static TAILQ_HEAD(, eventhandler_list)	eventhandler_lists;
43fcb893a8SMike Smith static int				eventhandler_lists_initted = 0;
44d543796fSJohn Baldwin static struct mtx			eventhandler_mutex;
45fcb893a8SMike Smith 
46fcb893a8SMike Smith struct eventhandler_entry_generic
47fcb893a8SMike Smith {
48fcb893a8SMike Smith     struct eventhandler_entry	ee;
49fcb893a8SMike Smith     void			(* func)(void);
50fcb893a8SMike Smith };
51fcb893a8SMike Smith 
52fdf20233SJoseph Koshy static struct eventhandler_list *_eventhandler_find_list(const char *name);
53ecdf4409SJohn Baldwin 
54fcb893a8SMike Smith /*
5506592dd1SJohn Baldwin  * Initialize the eventhandler mutex and list.
5606592dd1SJohn Baldwin  */
5706592dd1SJohn Baldwin static void
eventhandler_init(void * dummy __unused)5806592dd1SJohn Baldwin eventhandler_init(void *dummy __unused)
5906592dd1SJohn Baldwin {
6006592dd1SJohn Baldwin     TAILQ_INIT(&eventhandler_lists);
61ecdf4409SJohn Baldwin     mtx_init(&eventhandler_mutex, "eventhandler", NULL, MTX_DEF);
62ecdf4409SJohn Baldwin     atomic_store_rel_int(&eventhandler_lists_initted, 1);
6306592dd1SJohn Baldwin }
6406592dd1SJohn Baldwin SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
65237fdd78SRobert Watson     NULL);
6606592dd1SJohn Baldwin 
672ca45184SMatt Joras static struct eventhandler_list *
eventhandler_find_or_create_list(const char * name)682ca45184SMatt Joras eventhandler_find_or_create_list(const char *name)
69fcb893a8SMike Smith {
702ca45184SMatt Joras 	struct eventhandler_list *list, *new_list;
71fcb893a8SMike Smith 
72fcb893a8SMike Smith 	/* look for a matching, existing list */
73ecdf4409SJohn Baldwin 	list = _eventhandler_find_list(name);
74fcb893a8SMike Smith 
75fcb893a8SMike Smith 	/* Do we need to create the list? */
76fcb893a8SMike Smith 	if (list == NULL) {
779ed346baSBosko Milekic 	    mtx_unlock(&eventhandler_mutex);
78ecdf4409SJohn Baldwin 
792ca45184SMatt Joras 	    new_list = malloc(sizeof(*new_list) + strlen(name) + 1,
802ca45184SMatt Joras 		M_EVENTHANDLER, M_WAITOK | M_ZERO);
81ecdf4409SJohn Baldwin 
82ecdf4409SJohn Baldwin 	    /* If someone else created it already, then use that one. */
83ecdf4409SJohn Baldwin 	    mtx_lock(&eventhandler_mutex);
84ecdf4409SJohn Baldwin 	    list = _eventhandler_find_list(name);
85ecdf4409SJohn Baldwin 	    if (list != NULL) {
86ecdf4409SJohn Baldwin 		free(new_list, M_EVENTHANDLER);
87ecdf4409SJohn Baldwin 	    } else {
88ecdf4409SJohn Baldwin 		CTR2(KTR_EVH, "%s: creating list \"%s\"", __func__, name);
89ecdf4409SJohn Baldwin 		list = new_list;
902ca45184SMatt Joras 		TAILQ_INIT(&list->el_entries);
912ca45184SMatt Joras 		list->el_name = (char *)(list + 1);
92fcb893a8SMike Smith 		strcpy(list->el_name, name);
93d6160f60SMatt Joras 		mtx_init(&list->el_lock, list->el_name, "eventhandler list",
94d6160f60SMatt Joras 		    MTX_DEF);
95fcb893a8SMike Smith 		TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
96fcb893a8SMike Smith 	    }
97fcb893a8SMike Smith 	}
982ca45184SMatt Joras 	return (list);
99ecdf4409SJohn Baldwin }
1002ca45184SMatt Joras 
1012ca45184SMatt Joras /*
1022ca45184SMatt Joras  * Insertion is O(n) due to the priority scan, but optimises to O(1)
1032ca45184SMatt Joras  * if all priorities are identical.
1042ca45184SMatt Joras  */
1052ca45184SMatt Joras static eventhandler_tag
eventhandler_register_internal(struct eventhandler_list * list,const char * name,eventhandler_tag epn)1062ca45184SMatt Joras eventhandler_register_internal(struct eventhandler_list *list,
1072ca45184SMatt Joras     const char *name, eventhandler_tag epn)
1082ca45184SMatt Joras {
1092ca45184SMatt Joras     struct eventhandler_entry		*ep;
1102ca45184SMatt Joras 
1112ca45184SMatt Joras     KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
1122ca45184SMatt Joras     KASSERT(epn != NULL, ("%s: cannot register NULL event", __func__));
1132ca45184SMatt Joras 
1142ca45184SMatt Joras     /* Do we need to find/create the list? */
1152ca45184SMatt Joras     if (list == NULL) {
1162ca45184SMatt Joras 	    mtx_lock(&eventhandler_mutex);
1172ca45184SMatt Joras 	    list = eventhandler_find_or_create_list(name);
11819a0f7e1SAlfred Perlstein 	    mtx_unlock(&eventhandler_mutex);
1192ca45184SMatt Joras     }
120fcb893a8SMike Smith 
12142eedeacSBjoern A. Zeeb     KASSERT(epn->ee_priority != EHE_DEAD_PRIORITY,
12242eedeacSBjoern A. Zeeb 	("%s: handler for %s registered with dead priority", __func__, name));
12342eedeacSBjoern A. Zeeb 
12442eedeacSBjoern A. Zeeb     /* sort it into the list */
12542eedeacSBjoern A. Zeeb     CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, epn,
12642eedeacSBjoern A. Zeeb 	((struct eventhandler_entry_generic *)epn)->func, name);
12742eedeacSBjoern A. Zeeb     EHL_LOCK(list);
12842eedeacSBjoern A. Zeeb     TAILQ_FOREACH(ep, &list->el_entries, ee_link) {
12942eedeacSBjoern A. Zeeb 	if (ep->ee_priority != EHE_DEAD_PRIORITY &&
13042eedeacSBjoern A. Zeeb 	    epn->ee_priority < ep->ee_priority) {
13142eedeacSBjoern A. Zeeb 	    TAILQ_INSERT_BEFORE(ep, epn, ee_link);
13242eedeacSBjoern A. Zeeb 	    break;
13342eedeacSBjoern A. Zeeb 	}
13442eedeacSBjoern A. Zeeb     }
13542eedeacSBjoern A. Zeeb     if (ep == NULL)
13642eedeacSBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&list->el_entries, epn, ee_link);
13742eedeacSBjoern A. Zeeb     EHL_UNLOCK(list);
13842eedeacSBjoern A. Zeeb     return(epn);
13942eedeacSBjoern A. Zeeb }
14042eedeacSBjoern A. Zeeb 
14142eedeacSBjoern A. Zeeb eventhandler_tag
eventhandler_register(struct eventhandler_list * list,const char * name,void * func,void * arg,int priority)14242eedeacSBjoern A. Zeeb eventhandler_register(struct eventhandler_list *list, const char *name,
14342eedeacSBjoern A. Zeeb 		      void *func, void *arg, int priority)
14442eedeacSBjoern A. Zeeb {
14542eedeacSBjoern A. Zeeb     struct eventhandler_entry_generic	*eg;
14642eedeacSBjoern A. Zeeb 
147fcb893a8SMike Smith     /* allocate an entry for this handler, populate it */
148ecdf4409SJohn Baldwin     eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER,
149ecdf4409SJohn Baldwin 	M_WAITOK | M_ZERO);
150fcb893a8SMike Smith     eg->func = func;
151fcb893a8SMike Smith     eg->ee.ee_arg = arg;
152fcb893a8SMike Smith     eg->ee.ee_priority = priority;
153fcb893a8SMike Smith 
15442eedeacSBjoern A. Zeeb     return (eventhandler_register_internal(list, name, &eg->ee));
155fcb893a8SMike Smith }
15642eedeacSBjoern A. Zeeb 
15742eedeacSBjoern A. Zeeb #ifdef VIMAGE
15842eedeacSBjoern A. Zeeb struct eventhandler_entry_generic_vimage
15942eedeacSBjoern A. Zeeb {
16042eedeacSBjoern A. Zeeb     struct eventhandler_entry		ee;
16142eedeacSBjoern A. Zeeb     vimage_iterator_func_t		func;		/* Vimage iterator function. */
16242eedeacSBjoern A. Zeeb     struct eventhandler_entry_vimage	v_ee;		/* Original func, arg. */
16342eedeacSBjoern A. Zeeb };
16442eedeacSBjoern A. Zeeb 
16542eedeacSBjoern A. Zeeb eventhandler_tag
vimage_eventhandler_register(struct eventhandler_list * list,const char * name,void * func,void * arg,int priority,vimage_iterator_func_t iterfunc)16642eedeacSBjoern A. Zeeb vimage_eventhandler_register(struct eventhandler_list *list, const char *name,
16742eedeacSBjoern A. Zeeb     void *func, void *arg, int priority, vimage_iterator_func_t iterfunc)
16842eedeacSBjoern A. Zeeb {
16942eedeacSBjoern A. Zeeb     struct eventhandler_entry_generic_vimage	*eg;
17042eedeacSBjoern A. Zeeb 
17142eedeacSBjoern A. Zeeb     /* allocate an entry for this handler, populate it */
17242eedeacSBjoern A. Zeeb     eg = malloc(sizeof(struct eventhandler_entry_generic_vimage),
17342eedeacSBjoern A. Zeeb 	M_EVENTHANDLER, M_WAITOK | M_ZERO);
17442eedeacSBjoern A. Zeeb     eg->func = iterfunc;
17542eedeacSBjoern A. Zeeb     eg->v_ee.func = func;
17642eedeacSBjoern A. Zeeb     eg->v_ee.ee_arg = arg;
17742eedeacSBjoern A. Zeeb     eg->ee.ee_arg = &eg->v_ee;
17842eedeacSBjoern A. Zeeb     eg->ee.ee_priority = priority;
17942eedeacSBjoern A. Zeeb 
18042eedeacSBjoern A. Zeeb     return (eventhandler_register_internal(list, name, &eg->ee));
181fcb893a8SMike Smith }
18242eedeacSBjoern A. Zeeb #endif
183fcb893a8SMike Smith 
184fc091646SIan Lepore static void
_eventhandler_deregister(struct eventhandler_list * list,eventhandler_tag tag,bool wait)185fc091646SIan Lepore _eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag,
186fc091646SIan Lepore     bool wait)
187fcb893a8SMike Smith {
188fcb893a8SMike Smith     struct eventhandler_entry	*ep = tag;
189fcb893a8SMike Smith 
190ecdf4409SJohn Baldwin     EHL_LOCK_ASSERT(list, MA_OWNED);
191fcb893a8SMike Smith     if (ep != NULL) {
192fcb893a8SMike Smith 	/* remove just this entry */
193ecdf4409SJohn Baldwin 	if (list->el_runcount == 0) {
194ecdf4409SJohn Baldwin 	    CTR3(KTR_EVH, "%s: removing item %p from \"%s\"", __func__, ep,
195ecdf4409SJohn Baldwin 		list->el_name);
196fcb893a8SMike Smith 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
197fcb893a8SMike Smith 	    free(ep, M_EVENTHANDLER);
198fcb893a8SMike Smith 	} else {
199ecdf4409SJohn Baldwin 	    CTR3(KTR_EVH, "%s: marking item %p from \"%s\" as dead", __func__,
200ecdf4409SJohn Baldwin 		ep, list->el_name);
201ecdf4409SJohn Baldwin 	    ep->ee_priority = EHE_DEAD_PRIORITY;
202ecdf4409SJohn Baldwin 	}
203ecdf4409SJohn Baldwin     } else {
204fcb893a8SMike Smith 	/* remove entire list */
205ecdf4409SJohn Baldwin 	if (list->el_runcount == 0) {
206ecdf4409SJohn Baldwin 	    CTR2(KTR_EVH, "%s: removing all items from \"%s\"", __func__,
207ecdf4409SJohn Baldwin 		list->el_name);
208fcb893a8SMike Smith 	    while (!TAILQ_EMPTY(&list->el_entries)) {
209fcb893a8SMike Smith 		ep = TAILQ_FIRST(&list->el_entries);
2101b727751SPoul-Henning Kamp 		TAILQ_REMOVE(&list->el_entries, ep, ee_link);
211fcb893a8SMike Smith 		free(ep, M_EVENTHANDLER);
212fcb893a8SMike Smith 	    }
213ecdf4409SJohn Baldwin 	} else {
214ecdf4409SJohn Baldwin 	    CTR2(KTR_EVH, "%s: marking all items from \"%s\" as dead",
215ecdf4409SJohn Baldwin 		__func__, list->el_name);
216ecdf4409SJohn Baldwin 	    TAILQ_FOREACH(ep, &list->el_entries, ee_link)
217ecdf4409SJohn Baldwin 		ep->ee_priority = EHE_DEAD_PRIORITY;
218fcb893a8SMike Smith 	}
219ecdf4409SJohn Baldwin     }
220fc091646SIan Lepore     while (wait && list->el_runcount > 0)
2212b543150SAndrew Thompson 	    mtx_sleep(list, &list->el_lock, 0, "evhrm", 0);
222ecdf4409SJohn Baldwin     EHL_UNLOCK(list);
223fcb893a8SMike Smith }
224fcb893a8SMike Smith 
225fc091646SIan Lepore void
eventhandler_deregister(struct eventhandler_list * list,eventhandler_tag tag)226fc091646SIan Lepore eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
227fc091646SIan Lepore {
228fc091646SIan Lepore 
229fc091646SIan Lepore 	_eventhandler_deregister(list, tag, true);
230fc091646SIan Lepore }
231fc091646SIan Lepore 
232fc091646SIan Lepore void
eventhandler_deregister_nowait(struct eventhandler_list * list,eventhandler_tag tag)233fc091646SIan Lepore eventhandler_deregister_nowait(struct eventhandler_list *list,
234fc091646SIan Lepore     eventhandler_tag tag)
235fc091646SIan Lepore {
236fc091646SIan Lepore 
237fc091646SIan Lepore 	_eventhandler_deregister(list, tag, false);
238fc091646SIan Lepore }
239fc091646SIan Lepore 
240ecdf4409SJohn Baldwin /*
241ecdf4409SJohn Baldwin  * Internal version for use when eventhandler list is already locked.
242ecdf4409SJohn Baldwin  */
243ecdf4409SJohn Baldwin static struct eventhandler_list *
_eventhandler_find_list(const char * name)244fdf20233SJoseph Koshy _eventhandler_find_list(const char *name)
245ecdf4409SJohn Baldwin {
246ecdf4409SJohn Baldwin     struct eventhandler_list	*list;
247ecdf4409SJohn Baldwin 
248ecdf4409SJohn Baldwin     mtx_assert(&eventhandler_mutex, MA_OWNED);
249ecdf4409SJohn Baldwin     TAILQ_FOREACH(list, &eventhandler_lists, el_link) {
250ecdf4409SJohn Baldwin 	if (!strcmp(name, list->el_name))
251ecdf4409SJohn Baldwin 	    break;
252ecdf4409SJohn Baldwin     }
253ecdf4409SJohn Baldwin     return (list);
254ecdf4409SJohn Baldwin }
255ecdf4409SJohn Baldwin 
256ecdf4409SJohn Baldwin /*
257ecdf4409SJohn Baldwin  * Lookup a "slow" list by name.  Returns with the list locked.
258ecdf4409SJohn Baldwin  */
259fcb893a8SMike Smith struct eventhandler_list *
eventhandler_find_list(const char * name)260fdf20233SJoseph Koshy eventhandler_find_list(const char *name)
261fcb893a8SMike Smith {
262fcb893a8SMike Smith     struct eventhandler_list	*list;
263146be906SJake Burkholder 
264146be906SJake Burkholder     if (!eventhandler_lists_initted)
265146be906SJake Burkholder 	return(NULL);
266fcb893a8SMike Smith 
267fcb893a8SMike Smith     /* scan looking for the requested list */
2689ed346baSBosko Milekic     mtx_lock(&eventhandler_mutex);
269ecdf4409SJohn Baldwin     list = _eventhandler_find_list(name);
270ecdf4409SJohn Baldwin     if (list != NULL)
271ecdf4409SJohn Baldwin 	EHL_LOCK(list);
2729ed346baSBosko Milekic     mtx_unlock(&eventhandler_mutex);
2736595c331SMike Smith 
274fcb893a8SMike Smith     return(list);
275fcb893a8SMike Smith }
2766595c331SMike Smith 
277ecdf4409SJohn Baldwin /*
278ecdf4409SJohn Baldwin  * Prune "dead" entries from an eventhandler list.
279ecdf4409SJohn Baldwin  */
280ecdf4409SJohn Baldwin void
eventhandler_prune_list(struct eventhandler_list * list)281ecdf4409SJohn Baldwin eventhandler_prune_list(struct eventhandler_list *list)
282ecdf4409SJohn Baldwin {
283ecdf4409SJohn Baldwin     struct eventhandler_entry *ep, *en;
2842b543150SAndrew Thompson     int pruned = 0;
285ecdf4409SJohn Baldwin 
286ecdf4409SJohn Baldwin     CTR2(KTR_EVH, "%s: pruning list \"%s\"", __func__, list->el_name);
287ecdf4409SJohn Baldwin     EHL_LOCK_ASSERT(list, MA_OWNED);
2882b543150SAndrew Thompson     TAILQ_FOREACH_SAFE(ep, &list->el_entries, ee_link, en) {
289ecdf4409SJohn Baldwin 	if (ep->ee_priority == EHE_DEAD_PRIORITY) {
290ecdf4409SJohn Baldwin 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
291ecdf4409SJohn Baldwin 	    free(ep, M_EVENTHANDLER);
2922b543150SAndrew Thompson 	    pruned++;
293ecdf4409SJohn Baldwin 	}
294ecdf4409SJohn Baldwin     }
2952b543150SAndrew Thompson     if (pruned > 0)
2962b543150SAndrew Thompson 	    wakeup(list);
297ecdf4409SJohn Baldwin }
2982ca45184SMatt Joras 
2992ca45184SMatt Joras /*
3002ca45184SMatt Joras  * Create (or get the existing) list so the pointer can be stored by
3012ca45184SMatt Joras  * EVENTHANDLER_LIST_DEFINE.
3022ca45184SMatt Joras  */
3032ca45184SMatt Joras struct eventhandler_list *
eventhandler_create_list(const char * name)3042ca45184SMatt Joras eventhandler_create_list(const char *name)
3052ca45184SMatt Joras {
3062ca45184SMatt Joras 	struct eventhandler_list *list;
3072ca45184SMatt Joras 
3082ca45184SMatt Joras 	KASSERT(eventhandler_lists_initted,
3092ca45184SMatt Joras 	    ("eventhandler list created too early"));
3102ca45184SMatt Joras 
3112ca45184SMatt Joras 	mtx_lock(&eventhandler_mutex);
3122ca45184SMatt Joras 	list = eventhandler_find_or_create_list(name);
3132ca45184SMatt Joras 	mtx_unlock(&eventhandler_mutex);
3142ca45184SMatt Joras 
3152ca45184SMatt Joras 	return (list);
3162ca45184SMatt Joras }
317