xref: /freebsd/sys/kern/subr_eventhandler.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/eventhandler.h>
40 
41 static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
42 
43 /* List of all eventhandler lists */
44 static TAILQ_HEAD(, eventhandler_list)	eventhandler_lists;
45 static int				eventhandler_lists_initted = 0;
46 static struct mtx			eventhandler_mutex;
47 
48 struct eventhandler_entry_generic
49 {
50     struct eventhandler_entry	ee;
51     void			(* func)(void);
52 };
53 
54 static struct eventhandler_list *_eventhandler_find_list(const char *name);
55 
56 /*
57  * Initialize the eventhandler mutex and list.
58  */
59 static void
60 eventhandler_init(void *dummy __unused)
61 {
62     TAILQ_INIT(&eventhandler_lists);
63     mtx_init(&eventhandler_mutex, "eventhandler", NULL, MTX_DEF);
64     atomic_store_rel_int(&eventhandler_lists_initted, 1);
65 }
66 SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
67     NULL);
68 
69 static struct eventhandler_list *
70 eventhandler_find_or_create_list(const char *name)
71 {
72 	struct eventhandler_list *list, *new_list;
73 
74 	/* look for a matching, existing list */
75 	list = _eventhandler_find_list(name);
76 
77 	/* Do we need to create the list? */
78 	if (list == NULL) {
79 	    mtx_unlock(&eventhandler_mutex);
80 
81 	    new_list = malloc(sizeof(*new_list) + strlen(name) + 1,
82 		M_EVENTHANDLER, M_WAITOK | M_ZERO);
83 
84 	    /* If someone else created it already, then use that one. */
85 	    mtx_lock(&eventhandler_mutex);
86 	    list = _eventhandler_find_list(name);
87 	    if (list != NULL) {
88 		free(new_list, M_EVENTHANDLER);
89 	    } else {
90 		CTR2(KTR_EVH, "%s: creating list \"%s\"", __func__, name);
91 		list = new_list;
92 		TAILQ_INIT(&list->el_entries);
93 		list->el_name = (char *)(list + 1);
94 		strcpy(list->el_name, name);
95 		mtx_init(&list->el_lock, list->el_name, "eventhandler list",
96 		    MTX_DEF);
97 		TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
98 	    }
99 	}
100 	return (list);
101 }
102 
103 /*
104  * Insertion is O(n) due to the priority scan, but optimises to O(1)
105  * if all priorities are identical.
106  */
107 static eventhandler_tag
108 eventhandler_register_internal(struct eventhandler_list *list,
109     const char *name, eventhandler_tag epn)
110 {
111     struct eventhandler_entry		*ep;
112 
113     KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
114     KASSERT(epn != NULL, ("%s: cannot register NULL event", __func__));
115 
116     /* Do we need to find/create the list? */
117     if (list == NULL) {
118 	    mtx_lock(&eventhandler_mutex);
119 	    list = eventhandler_find_or_create_list(name);
120 	    mtx_unlock(&eventhandler_mutex);
121     }
122 
123     KASSERT(epn->ee_priority != EHE_DEAD_PRIORITY,
124 	("%s: handler for %s registered with dead priority", __func__, name));
125 
126     /* sort it into the list */
127     CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, epn,
128 	((struct eventhandler_entry_generic *)epn)->func, name);
129     EHL_LOCK(list);
130     TAILQ_FOREACH(ep, &list->el_entries, ee_link) {
131 	if (ep->ee_priority != EHE_DEAD_PRIORITY &&
132 	    epn->ee_priority < ep->ee_priority) {
133 	    TAILQ_INSERT_BEFORE(ep, epn, ee_link);
134 	    break;
135 	}
136     }
137     if (ep == NULL)
138 	TAILQ_INSERT_TAIL(&list->el_entries, epn, ee_link);
139     EHL_UNLOCK(list);
140     return(epn);
141 }
142 
143 eventhandler_tag
144 eventhandler_register(struct eventhandler_list *list, const char *name,
145 		      void *func, void *arg, int priority)
146 {
147     struct eventhandler_entry_generic	*eg;
148 
149     /* allocate an entry for this handler, populate it */
150     eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER,
151 	M_WAITOK | M_ZERO);
152     eg->func = func;
153     eg->ee.ee_arg = arg;
154     eg->ee.ee_priority = priority;
155 
156     return (eventhandler_register_internal(list, name, &eg->ee));
157 }
158 
159 #ifdef VIMAGE
160 struct eventhandler_entry_generic_vimage
161 {
162     struct eventhandler_entry		ee;
163     vimage_iterator_func_t		func;		/* Vimage iterator function. */
164     struct eventhandler_entry_vimage	v_ee;		/* Original func, arg. */
165 };
166 
167 eventhandler_tag
168 vimage_eventhandler_register(struct eventhandler_list *list, const char *name,
169     void *func, void *arg, int priority, vimage_iterator_func_t iterfunc)
170 {
171     struct eventhandler_entry_generic_vimage	*eg;
172 
173     /* allocate an entry for this handler, populate it */
174     eg = malloc(sizeof(struct eventhandler_entry_generic_vimage),
175 	M_EVENTHANDLER, M_WAITOK | M_ZERO);
176     eg->func = iterfunc;
177     eg->v_ee.func = func;
178     eg->v_ee.ee_arg = arg;
179     eg->ee.ee_arg = &eg->v_ee;
180     eg->ee.ee_priority = priority;
181 
182     return (eventhandler_register_internal(list, name, &eg->ee));
183 }
184 #endif
185 
186 static void
187 _eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag,
188     bool wait)
189 {
190     struct eventhandler_entry	*ep = tag;
191 
192     EHL_LOCK_ASSERT(list, MA_OWNED);
193     if (ep != NULL) {
194 	/* remove just this entry */
195 	if (list->el_runcount == 0) {
196 	    CTR3(KTR_EVH, "%s: removing item %p from \"%s\"", __func__, ep,
197 		list->el_name);
198 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
199 	    free(ep, M_EVENTHANDLER);
200 	} else {
201 	    CTR3(KTR_EVH, "%s: marking item %p from \"%s\" as dead", __func__,
202 		ep, list->el_name);
203 	    ep->ee_priority = EHE_DEAD_PRIORITY;
204 	}
205     } else {
206 	/* remove entire list */
207 	if (list->el_runcount == 0) {
208 	    CTR2(KTR_EVH, "%s: removing all items from \"%s\"", __func__,
209 		list->el_name);
210 	    while (!TAILQ_EMPTY(&list->el_entries)) {
211 		ep = TAILQ_FIRST(&list->el_entries);
212 		TAILQ_REMOVE(&list->el_entries, ep, ee_link);
213 		free(ep, M_EVENTHANDLER);
214 	    }
215 	} else {
216 	    CTR2(KTR_EVH, "%s: marking all items from \"%s\" as dead",
217 		__func__, list->el_name);
218 	    TAILQ_FOREACH(ep, &list->el_entries, ee_link)
219 		ep->ee_priority = EHE_DEAD_PRIORITY;
220 	}
221     }
222     while (wait && list->el_runcount > 0)
223 	    mtx_sleep(list, &list->el_lock, 0, "evhrm", 0);
224     EHL_UNLOCK(list);
225 }
226 
227 void
228 eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
229 {
230 
231 	_eventhandler_deregister(list, tag, true);
232 }
233 
234 void
235 eventhandler_deregister_nowait(struct eventhandler_list *list,
236     eventhandler_tag tag)
237 {
238 
239 	_eventhandler_deregister(list, tag, false);
240 }
241 
242 /*
243  * Internal version for use when eventhandler list is already locked.
244  */
245 static struct eventhandler_list *
246 _eventhandler_find_list(const char *name)
247 {
248     struct eventhandler_list	*list;
249 
250     mtx_assert(&eventhandler_mutex, MA_OWNED);
251     TAILQ_FOREACH(list, &eventhandler_lists, el_link) {
252 	if (!strcmp(name, list->el_name))
253 	    break;
254     }
255     return (list);
256 }
257 
258 /*
259  * Lookup a "slow" list by name.  Returns with the list locked.
260  */
261 struct eventhandler_list *
262 eventhandler_find_list(const char *name)
263 {
264     struct eventhandler_list	*list;
265 
266     if (!eventhandler_lists_initted)
267 	return(NULL);
268 
269     /* scan looking for the requested list */
270     mtx_lock(&eventhandler_mutex);
271     list = _eventhandler_find_list(name);
272     if (list != NULL)
273 	EHL_LOCK(list);
274     mtx_unlock(&eventhandler_mutex);
275 
276     return(list);
277 }
278 
279 /*
280  * Prune "dead" entries from an eventhandler list.
281  */
282 void
283 eventhandler_prune_list(struct eventhandler_list *list)
284 {
285     struct eventhandler_entry *ep, *en;
286     int pruned = 0;
287 
288     CTR2(KTR_EVH, "%s: pruning list \"%s\"", __func__, list->el_name);
289     EHL_LOCK_ASSERT(list, MA_OWNED);
290     TAILQ_FOREACH_SAFE(ep, &list->el_entries, ee_link, en) {
291 	if (ep->ee_priority == EHE_DEAD_PRIORITY) {
292 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
293 	    free(ep, M_EVENTHANDLER);
294 	    pruned++;
295 	}
296     }
297     if (pruned > 0)
298 	    wakeup(list);
299 }
300 
301 /*
302  * Create (or get the existing) list so the pointer can be stored by
303  * EVENTHANDLER_LIST_DEFINE.
304  */
305 struct eventhandler_list *
306 eventhandler_create_list(const char *name)
307 {
308 	struct eventhandler_list *list;
309 
310 	KASSERT(eventhandler_lists_initted,
311 	    ("eventhandler list created too early"));
312 
313 	mtx_lock(&eventhandler_mutex);
314 	list = eventhandler_find_or_create_list(name);
315 	mtx_unlock(&eventhandler_mutex);
316 
317 	return (list);
318 }
319