xref: /freebsd/sys/kern/subr_eventhandler.c (revision fc091646)
1fcb893a8SMike Smith /*-
2fcb893a8SMike Smith  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
3fcb893a8SMike Smith  * All rights reserved.
4fcb893a8SMike Smith  *
5fcb893a8SMike Smith  * Redistribution and use in source and binary forms, with or without
6fcb893a8SMike Smith  * modification, are permitted provided that the following conditions
7fcb893a8SMike Smith  * are met:
8fcb893a8SMike Smith  * 1. Redistributions of source code must retain the above copyright
9fcb893a8SMike Smith  *    notice, this list of conditions and the following disclaimer.
10fcb893a8SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
11fcb893a8SMike Smith  *    notice, this list of conditions and the following disclaimer in the
12fcb893a8SMike Smith  *    documentation and/or other materials provided with the distribution.
13fcb893a8SMike Smith  *
14fcb893a8SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15fcb893a8SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16fcb893a8SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17fcb893a8SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18fcb893a8SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19fcb893a8SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20fcb893a8SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21fcb893a8SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22fcb893a8SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23fcb893a8SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24fcb893a8SMike Smith  * SUCH DAMAGE.
25fcb893a8SMike Smith  */
26fcb893a8SMike Smith 
27677b542eSDavid E. O'Brien #include <sys/cdefs.h>
28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
29677b542eSDavid E. O'Brien 
30fcb893a8SMike Smith #include <sys/param.h>
31fcb893a8SMike Smith #include <sys/kernel.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 
41fcb893a8SMike Smith /* List of 'slow' 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
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 
6706592dd1SJohn Baldwin /*
68fcb893a8SMike Smith  * Insertion is O(n) due to the priority scan, but optimises to O(1)
69fcb893a8SMike Smith  * if all priorities are identical.
70fcb893a8SMike Smith  */
7142eedeacSBjoern A. Zeeb static eventhandler_tag
7242eedeacSBjoern A. Zeeb eventhandler_register_internal(struct eventhandler_list *list,
7342eedeacSBjoern A. Zeeb     const char *name, eventhandler_tag epn)
74fcb893a8SMike Smith {
75ecdf4409SJohn Baldwin     struct eventhandler_list		*new_list;
76fcb893a8SMike Smith     struct eventhandler_entry		*ep;
77fcb893a8SMike Smith 
7806592dd1SJohn Baldwin     KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
7942eedeacSBjoern A. Zeeb     KASSERT(epn != NULL, ("%s: cannot register NULL event", __func__));
80fcb893a8SMike Smith 
816595c331SMike Smith     /* lock the eventhandler lists */
829ed346baSBosko Milekic     mtx_lock(&eventhandler_mutex);
836595c331SMike Smith 
84fcb893a8SMike Smith     /* Do we need to find/create the (slow) list? */
85fcb893a8SMike Smith     if (list == NULL) {
86fcb893a8SMike Smith 	/* look for a matching, existing list */
87ecdf4409SJohn Baldwin 	list = _eventhandler_find_list(name);
88fcb893a8SMike Smith 
89fcb893a8SMike Smith 	/* Do we need to create the list? */
90fcb893a8SMike Smith 	if (list == NULL) {
919ed346baSBosko Milekic 	    mtx_unlock(&eventhandler_mutex);
92ecdf4409SJohn Baldwin 
93ecdf4409SJohn Baldwin 	    new_list = malloc(sizeof(struct eventhandler_list) +
94ecdf4409SJohn Baldwin 		strlen(name) + 1, M_EVENTHANDLER, M_WAITOK);
95ecdf4409SJohn Baldwin 
96ecdf4409SJohn Baldwin 	    /* If someone else created it already, then use that one. */
97ecdf4409SJohn Baldwin 	    mtx_lock(&eventhandler_mutex);
98ecdf4409SJohn Baldwin 	    list = _eventhandler_find_list(name);
99ecdf4409SJohn Baldwin 	    if (list != NULL) {
100ecdf4409SJohn Baldwin 		free(new_list, M_EVENTHANDLER);
101ecdf4409SJohn Baldwin 	    } else {
102ecdf4409SJohn Baldwin 		CTR2(KTR_EVH, "%s: creating list \"%s\"", __func__, name);
103ecdf4409SJohn Baldwin 		list = new_list;
104fcb893a8SMike Smith 		list->el_flags = 0;
105ecdf4409SJohn Baldwin 		list->el_runcount = 0;
10628fe1a71SAlfred Perlstein 		bzero(&list->el_lock, sizeof(list->el_lock));
107fcb893a8SMike Smith 		list->el_name = (char *)list + sizeof(struct eventhandler_list);
108fcb893a8SMike Smith 		strcpy(list->el_name, name);
109fcb893a8SMike Smith 		TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
110fcb893a8SMike Smith 	    }
111fcb893a8SMike Smith 	}
112ecdf4409SJohn Baldwin     }
113ecdf4409SJohn Baldwin     if (!(list->el_flags & EHL_INITTED)) {
114fcb893a8SMike Smith 	TAILQ_INIT(&list->el_entries);
115ecdf4409SJohn Baldwin 	mtx_init(&list->el_lock, name, "eventhandler list", MTX_DEF);
116ecdf4409SJohn Baldwin 	atomic_store_rel_int(&list->el_flags, EHL_INITTED);
117fcb893a8SMike Smith     }
11819a0f7e1SAlfred Perlstein     mtx_unlock(&eventhandler_mutex);
119fcb893a8SMike Smith 
12042eedeacSBjoern A. Zeeb     KASSERT(epn->ee_priority != EHE_DEAD_PRIORITY,
12142eedeacSBjoern A. Zeeb 	("%s: handler for %s registered with dead priority", __func__, name));
12242eedeacSBjoern A. Zeeb 
12342eedeacSBjoern A. Zeeb     /* sort it into the list */
12442eedeacSBjoern A. Zeeb     CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, epn,
12542eedeacSBjoern A. Zeeb 	((struct eventhandler_entry_generic *)epn)->func, name);
12642eedeacSBjoern A. Zeeb     EHL_LOCK(list);
12742eedeacSBjoern A. Zeeb     TAILQ_FOREACH(ep, &list->el_entries, ee_link) {
12842eedeacSBjoern A. Zeeb 	if (ep->ee_priority != EHE_DEAD_PRIORITY &&
12942eedeacSBjoern A. Zeeb 	    epn->ee_priority < ep->ee_priority) {
13042eedeacSBjoern A. Zeeb 	    TAILQ_INSERT_BEFORE(ep, epn, ee_link);
13142eedeacSBjoern A. Zeeb 	    break;
13242eedeacSBjoern A. Zeeb 	}
13342eedeacSBjoern A. Zeeb     }
13442eedeacSBjoern A. Zeeb     if (ep == NULL)
13542eedeacSBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&list->el_entries, epn, ee_link);
13642eedeacSBjoern A. Zeeb     EHL_UNLOCK(list);
13742eedeacSBjoern A. Zeeb     return(epn);
13842eedeacSBjoern A. Zeeb }
13942eedeacSBjoern A. Zeeb 
14042eedeacSBjoern A. Zeeb eventhandler_tag
14142eedeacSBjoern A. Zeeb eventhandler_register(struct eventhandler_list *list, const char *name,
14242eedeacSBjoern A. Zeeb 		      void *func, void *arg, int priority)
14342eedeacSBjoern A. Zeeb {
14442eedeacSBjoern A. Zeeb     struct eventhandler_entry_generic	*eg;
14542eedeacSBjoern A. Zeeb 
146fcb893a8SMike Smith     /* allocate an entry for this handler, populate it */
147ecdf4409SJohn Baldwin     eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER,
148ecdf4409SJohn Baldwin 	M_WAITOK | M_ZERO);
149fcb893a8SMike Smith     eg->func = func;
150fcb893a8SMike Smith     eg->ee.ee_arg = arg;
151fcb893a8SMike Smith     eg->ee.ee_priority = priority;
152fcb893a8SMike Smith 
15342eedeacSBjoern A. Zeeb     return (eventhandler_register_internal(list, name, &eg->ee));
154fcb893a8SMike Smith }
15542eedeacSBjoern A. Zeeb 
15642eedeacSBjoern A. Zeeb #ifdef VIMAGE
15742eedeacSBjoern A. Zeeb struct eventhandler_entry_generic_vimage
15842eedeacSBjoern A. Zeeb {
15942eedeacSBjoern A. Zeeb     struct eventhandler_entry		ee;
16042eedeacSBjoern A. Zeeb     vimage_iterator_func_t		func;		/* Vimage iterator function. */
16142eedeacSBjoern A. Zeeb     struct eventhandler_entry_vimage	v_ee;		/* Original func, arg. */
16242eedeacSBjoern A. Zeeb };
16342eedeacSBjoern A. Zeeb 
16442eedeacSBjoern A. Zeeb eventhandler_tag
16542eedeacSBjoern A. Zeeb vimage_eventhandler_register(struct eventhandler_list *list, const char *name,
16642eedeacSBjoern A. Zeeb     void *func, void *arg, int priority, vimage_iterator_func_t iterfunc)
16742eedeacSBjoern A. Zeeb {
16842eedeacSBjoern A. Zeeb     struct eventhandler_entry_generic_vimage	*eg;
16942eedeacSBjoern A. Zeeb 
17042eedeacSBjoern A. Zeeb     /* allocate an entry for this handler, populate it */
17142eedeacSBjoern A. Zeeb     eg = malloc(sizeof(struct eventhandler_entry_generic_vimage),
17242eedeacSBjoern A. Zeeb 	M_EVENTHANDLER, M_WAITOK | M_ZERO);
17342eedeacSBjoern A. Zeeb     eg->func = iterfunc;
17442eedeacSBjoern A. Zeeb     eg->v_ee.func = func;
17542eedeacSBjoern A. Zeeb     eg->v_ee.ee_arg = arg;
17642eedeacSBjoern A. Zeeb     eg->ee.ee_arg = &eg->v_ee;
17742eedeacSBjoern A. Zeeb     eg->ee.ee_priority = priority;
17842eedeacSBjoern A. Zeeb 
17942eedeacSBjoern A. Zeeb     return (eventhandler_register_internal(list, name, &eg->ee));
180fcb893a8SMike Smith }
18142eedeacSBjoern A. Zeeb #endif
182fcb893a8SMike Smith 
183fc091646SIan Lepore static void
184fc091646SIan Lepore _eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag,
185fc091646SIan Lepore     bool wait)
186fcb893a8SMike Smith {
187fcb893a8SMike Smith     struct eventhandler_entry	*ep = tag;
188fcb893a8SMike Smith 
189ecdf4409SJohn Baldwin     EHL_LOCK_ASSERT(list, MA_OWNED);
190fcb893a8SMike Smith     if (ep != NULL) {
191fcb893a8SMike Smith 	/* remove just this entry */
192ecdf4409SJohn Baldwin 	if (list->el_runcount == 0) {
193ecdf4409SJohn Baldwin 	    CTR3(KTR_EVH, "%s: removing item %p from \"%s\"", __func__, ep,
194ecdf4409SJohn Baldwin 		list->el_name);
195fcb893a8SMike Smith 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
196fcb893a8SMike Smith 	    free(ep, M_EVENTHANDLER);
197fcb893a8SMike Smith 	} else {
198ecdf4409SJohn Baldwin 	    CTR3(KTR_EVH, "%s: marking item %p from \"%s\" as dead", __func__,
199ecdf4409SJohn Baldwin 		ep, list->el_name);
200ecdf4409SJohn Baldwin 	    ep->ee_priority = EHE_DEAD_PRIORITY;
201ecdf4409SJohn Baldwin 	}
202ecdf4409SJohn Baldwin     } else {
203fcb893a8SMike Smith 	/* remove entire list */
204ecdf4409SJohn Baldwin 	if (list->el_runcount == 0) {
205ecdf4409SJohn Baldwin 	    CTR2(KTR_EVH, "%s: removing all items from \"%s\"", __func__,
206ecdf4409SJohn Baldwin 		list->el_name);
207fcb893a8SMike Smith 	    while (!TAILQ_EMPTY(&list->el_entries)) {
208fcb893a8SMike Smith 		ep = TAILQ_FIRST(&list->el_entries);
2091b727751SPoul-Henning Kamp 		TAILQ_REMOVE(&list->el_entries, ep, ee_link);
210fcb893a8SMike Smith 		free(ep, M_EVENTHANDLER);
211fcb893a8SMike Smith 	    }
212ecdf4409SJohn Baldwin 	} else {
213ecdf4409SJohn Baldwin 	    CTR2(KTR_EVH, "%s: marking all items from \"%s\" as dead",
214ecdf4409SJohn Baldwin 		__func__, list->el_name);
215ecdf4409SJohn Baldwin 	    TAILQ_FOREACH(ep, &list->el_entries, ee_link)
216ecdf4409SJohn Baldwin 		ep->ee_priority = EHE_DEAD_PRIORITY;
217fcb893a8SMike Smith 	}
218ecdf4409SJohn Baldwin     }
219fc091646SIan Lepore     while (wait && list->el_runcount > 0)
2202b543150SAndrew Thompson 	    mtx_sleep(list, &list->el_lock, 0, "evhrm", 0);
221ecdf4409SJohn Baldwin     EHL_UNLOCK(list);
222fcb893a8SMike Smith }
223fcb893a8SMike Smith 
224fc091646SIan Lepore void
225fc091646SIan Lepore eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
226fc091646SIan Lepore {
227fc091646SIan Lepore 
228fc091646SIan Lepore 	_eventhandler_deregister(list, tag, true);
229fc091646SIan Lepore }
230fc091646SIan Lepore 
231fc091646SIan Lepore void
232fc091646SIan Lepore eventhandler_deregister_nowait(struct eventhandler_list *list,
233fc091646SIan Lepore     eventhandler_tag tag)
234fc091646SIan Lepore {
235fc091646SIan Lepore 
236fc091646SIan Lepore 	_eventhandler_deregister(list, tag, false);
237fc091646SIan Lepore }
238fc091646SIan Lepore 
239ecdf4409SJohn Baldwin /*
240ecdf4409SJohn Baldwin  * Internal version for use when eventhandler list is already locked.
241ecdf4409SJohn Baldwin  */
242ecdf4409SJohn Baldwin static struct eventhandler_list *
243fdf20233SJoseph Koshy _eventhandler_find_list(const char *name)
244ecdf4409SJohn Baldwin {
245ecdf4409SJohn Baldwin     struct eventhandler_list	*list;
246ecdf4409SJohn Baldwin 
247ecdf4409SJohn Baldwin     mtx_assert(&eventhandler_mutex, MA_OWNED);
248ecdf4409SJohn Baldwin     TAILQ_FOREACH(list, &eventhandler_lists, el_link) {
249ecdf4409SJohn Baldwin 	if (!strcmp(name, list->el_name))
250ecdf4409SJohn Baldwin 	    break;
251ecdf4409SJohn Baldwin     }
252ecdf4409SJohn Baldwin     return (list);
253ecdf4409SJohn Baldwin }
254ecdf4409SJohn Baldwin 
255ecdf4409SJohn Baldwin /*
256ecdf4409SJohn Baldwin  * Lookup a "slow" list by name.  Returns with the list locked.
257ecdf4409SJohn Baldwin  */
258fcb893a8SMike Smith struct eventhandler_list *
259fdf20233SJoseph Koshy eventhandler_find_list(const char *name)
260fcb893a8SMike Smith {
261fcb893a8SMike Smith     struct eventhandler_list	*list;
262146be906SJake Burkholder 
263146be906SJake Burkholder     if (!eventhandler_lists_initted)
264146be906SJake Burkholder 	return(NULL);
265fcb893a8SMike Smith 
266fcb893a8SMike Smith     /* scan looking for the requested list */
2679ed346baSBosko Milekic     mtx_lock(&eventhandler_mutex);
268ecdf4409SJohn Baldwin     list = _eventhandler_find_list(name);
269ecdf4409SJohn Baldwin     if (list != NULL)
270ecdf4409SJohn Baldwin 	EHL_LOCK(list);
2719ed346baSBosko Milekic     mtx_unlock(&eventhandler_mutex);
2726595c331SMike Smith 
273fcb893a8SMike Smith     return(list);
274fcb893a8SMike Smith }
2756595c331SMike Smith 
276ecdf4409SJohn Baldwin /*
277ecdf4409SJohn Baldwin  * Prune "dead" entries from an eventhandler list.
278ecdf4409SJohn Baldwin  */
279ecdf4409SJohn Baldwin void
280ecdf4409SJohn Baldwin eventhandler_prune_list(struct eventhandler_list *list)
281ecdf4409SJohn Baldwin {
282ecdf4409SJohn Baldwin     struct eventhandler_entry *ep, *en;
2832b543150SAndrew Thompson     int pruned = 0;
284ecdf4409SJohn Baldwin 
285ecdf4409SJohn Baldwin     CTR2(KTR_EVH, "%s: pruning list \"%s\"", __func__, list->el_name);
286ecdf4409SJohn Baldwin     EHL_LOCK_ASSERT(list, MA_OWNED);
2872b543150SAndrew Thompson     TAILQ_FOREACH_SAFE(ep, &list->el_entries, ee_link, en) {
288ecdf4409SJohn Baldwin 	if (ep->ee_priority == EHE_DEAD_PRIORITY) {
289ecdf4409SJohn Baldwin 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
290ecdf4409SJohn Baldwin 	    free(ep, M_EVENTHANDLER);
2912b543150SAndrew Thompson 	    pruned++;
292ecdf4409SJohn Baldwin 	}
293ecdf4409SJohn Baldwin     }
2942b543150SAndrew Thompson     if (pruned > 0)
2952b543150SAndrew Thompson 	    wakeup(list);
296ecdf4409SJohn Baldwin }
297