xref: /freebsd/sys/kern/subr_eventhandler.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/ktr.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <sys/systm.h>
38 #include <sys/eventhandler.h>
39 
40 static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
41 
42 /* List of all eventhandler lists */
43 static TAILQ_HEAD(, eventhandler_list)	eventhandler_lists;
44 static int				eventhandler_lists_initted = 0;
45 static struct mtx			eventhandler_mutex;
46 
47 struct eventhandler_entry_generic
48 {
49     struct eventhandler_entry	ee;
50     void			(* func)(void);
51 };
52 
53 static struct eventhandler_list *_eventhandler_find_list(const char *name);
54 
55 /*
56  * Initialize the eventhandler mutex and list.
57  */
58 static void
59 eventhandler_init(void *dummy __unused)
60 {
61     TAILQ_INIT(&eventhandler_lists);
62     mtx_init(&eventhandler_mutex, "eventhandler", NULL, MTX_DEF);
63     atomic_store_rel_int(&eventhandler_lists_initted, 1);
64 }
65 SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
66     NULL);
67 
68 static struct eventhandler_list *
69 eventhandler_find_or_create_list(const char *name)
70 {
71 	struct eventhandler_list *list, *new_list;
72 
73 	/* look for a matching, existing list */
74 	list = _eventhandler_find_list(name);
75 
76 	/* Do we need to create the list? */
77 	if (list == NULL) {
78 	    mtx_unlock(&eventhandler_mutex);
79 
80 	    new_list = malloc(sizeof(*new_list) + strlen(name) + 1,
81 		M_EVENTHANDLER, M_WAITOK | M_ZERO);
82 
83 	    /* If someone else created it already, then use that one. */
84 	    mtx_lock(&eventhandler_mutex);
85 	    list = _eventhandler_find_list(name);
86 	    if (list != NULL) {
87 		free(new_list, M_EVENTHANDLER);
88 	    } else {
89 		CTR2(KTR_EVH, "%s: creating list \"%s\"", __func__, name);
90 		list = new_list;
91 		TAILQ_INIT(&list->el_entries);
92 		list->el_name = (char *)(list + 1);
93 		strcpy(list->el_name, name);
94 		mtx_init(&list->el_lock, list->el_name, "eventhandler list",
95 		    MTX_DEF);
96 		TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
97 	    }
98 	}
99 	return (list);
100 }
101 
102 /*
103  * Insertion is O(n) due to the priority scan, but optimises to O(1)
104  * if all priorities are identical.
105  */
106 static eventhandler_tag
107 eventhandler_register_internal(struct eventhandler_list *list,
108     const char *name, eventhandler_tag epn)
109 {
110     struct eventhandler_entry		*ep;
111 
112     KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
113     KASSERT(epn != NULL, ("%s: cannot register NULL event", __func__));
114 
115     /* Do we need to find/create the list? */
116     if (list == NULL) {
117 	    mtx_lock(&eventhandler_mutex);
118 	    list = eventhandler_find_or_create_list(name);
119 	    mtx_unlock(&eventhandler_mutex);
120     }
121 
122     KASSERT(epn->ee_priority != EHE_DEAD_PRIORITY,
123 	("%s: handler for %s registered with dead priority", __func__, name));
124 
125     /* sort it into the list */
126     CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, epn,
127 	((struct eventhandler_entry_generic *)epn)->func, name);
128     EHL_LOCK(list);
129     TAILQ_FOREACH(ep, &list->el_entries, ee_link) {
130 	if (ep->ee_priority != EHE_DEAD_PRIORITY &&
131 	    epn->ee_priority < ep->ee_priority) {
132 	    TAILQ_INSERT_BEFORE(ep, epn, ee_link);
133 	    break;
134 	}
135     }
136     if (ep == NULL)
137 	TAILQ_INSERT_TAIL(&list->el_entries, epn, ee_link);
138     EHL_UNLOCK(list);
139     return(epn);
140 }
141 
142 eventhandler_tag
143 eventhandler_register(struct eventhandler_list *list, const char *name,
144 		      void *func, void *arg, int priority)
145 {
146     struct eventhandler_entry_generic	*eg;
147 
148     /* allocate an entry for this handler, populate it */
149     eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER,
150 	M_WAITOK | M_ZERO);
151     eg->func = func;
152     eg->ee.ee_arg = arg;
153     eg->ee.ee_priority = priority;
154 
155     return (eventhandler_register_internal(list, name, &eg->ee));
156 }
157 
158 #ifdef VIMAGE
159 struct eventhandler_entry_generic_vimage
160 {
161     struct eventhandler_entry		ee;
162     vimage_iterator_func_t		func;		/* Vimage iterator function. */
163     struct eventhandler_entry_vimage	v_ee;		/* Original func, arg. */
164 };
165 
166 eventhandler_tag
167 vimage_eventhandler_register(struct eventhandler_list *list, const char *name,
168     void *func, void *arg, int priority, vimage_iterator_func_t iterfunc)
169 {
170     struct eventhandler_entry_generic_vimage	*eg;
171 
172     /* allocate an entry for this handler, populate it */
173     eg = malloc(sizeof(struct eventhandler_entry_generic_vimage),
174 	M_EVENTHANDLER, M_WAITOK | M_ZERO);
175     eg->func = iterfunc;
176     eg->v_ee.func = func;
177     eg->v_ee.ee_arg = arg;
178     eg->ee.ee_arg = &eg->v_ee;
179     eg->ee.ee_priority = priority;
180 
181     return (eventhandler_register_internal(list, name, &eg->ee));
182 }
183 #endif
184 
185 static void
186 _eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag,
187     bool wait)
188 {
189     struct eventhandler_entry	*ep = tag;
190 
191     EHL_LOCK_ASSERT(list, MA_OWNED);
192     if (ep != NULL) {
193 	/* remove just this entry */
194 	if (list->el_runcount == 0) {
195 	    CTR3(KTR_EVH, "%s: removing item %p from \"%s\"", __func__, ep,
196 		list->el_name);
197 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
198 	    free(ep, M_EVENTHANDLER);
199 	} else {
200 	    CTR3(KTR_EVH, "%s: marking item %p from \"%s\" as dead", __func__,
201 		ep, list->el_name);
202 	    ep->ee_priority = EHE_DEAD_PRIORITY;
203 	}
204     } else {
205 	/* remove entire list */
206 	if (list->el_runcount == 0) {
207 	    CTR2(KTR_EVH, "%s: removing all items from \"%s\"", __func__,
208 		list->el_name);
209 	    while (!TAILQ_EMPTY(&list->el_entries)) {
210 		ep = TAILQ_FIRST(&list->el_entries);
211 		TAILQ_REMOVE(&list->el_entries, ep, ee_link);
212 		free(ep, M_EVENTHANDLER);
213 	    }
214 	} else {
215 	    CTR2(KTR_EVH, "%s: marking all items from \"%s\" as dead",
216 		__func__, list->el_name);
217 	    TAILQ_FOREACH(ep, &list->el_entries, ee_link)
218 		ep->ee_priority = EHE_DEAD_PRIORITY;
219 	}
220     }
221     while (wait && list->el_runcount > 0)
222 	    mtx_sleep(list, &list->el_lock, 0, "evhrm", 0);
223     EHL_UNLOCK(list);
224 }
225 
226 void
227 eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
228 {
229 
230 	_eventhandler_deregister(list, tag, true);
231 }
232 
233 void
234 eventhandler_deregister_nowait(struct eventhandler_list *list,
235     eventhandler_tag tag)
236 {
237 
238 	_eventhandler_deregister(list, tag, false);
239 }
240 
241 /*
242  * Internal version for use when eventhandler list is already locked.
243  */
244 static struct eventhandler_list *
245 _eventhandler_find_list(const char *name)
246 {
247     struct eventhandler_list	*list;
248 
249     mtx_assert(&eventhandler_mutex, MA_OWNED);
250     TAILQ_FOREACH(list, &eventhandler_lists, el_link) {
251 	if (!strcmp(name, list->el_name))
252 	    break;
253     }
254     return (list);
255 }
256 
257 /*
258  * Lookup a "slow" list by name.  Returns with the list locked.
259  */
260 struct eventhandler_list *
261 eventhandler_find_list(const char *name)
262 {
263     struct eventhandler_list	*list;
264 
265     if (!eventhandler_lists_initted)
266 	return(NULL);
267 
268     /* scan looking for the requested list */
269     mtx_lock(&eventhandler_mutex);
270     list = _eventhandler_find_list(name);
271     if (list != NULL)
272 	EHL_LOCK(list);
273     mtx_unlock(&eventhandler_mutex);
274 
275     return(list);
276 }
277 
278 /*
279  * Prune "dead" entries from an eventhandler list.
280  */
281 void
282 eventhandler_prune_list(struct eventhandler_list *list)
283 {
284     struct eventhandler_entry *ep, *en;
285     int pruned = 0;
286 
287     CTR2(KTR_EVH, "%s: pruning list \"%s\"", __func__, list->el_name);
288     EHL_LOCK_ASSERT(list, MA_OWNED);
289     TAILQ_FOREACH_SAFE(ep, &list->el_entries, ee_link, en) {
290 	if (ep->ee_priority == EHE_DEAD_PRIORITY) {
291 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
292 	    free(ep, M_EVENTHANDLER);
293 	    pruned++;
294 	}
295     }
296     if (pruned > 0)
297 	    wakeup(list);
298 }
299 
300 /*
301  * Create (or get the existing) list so the pointer can be stored by
302  * EVENTHANDLER_LIST_DEFINE.
303  */
304 struct eventhandler_list *
305 eventhandler_create_list(const char *name)
306 {
307 	struct eventhandler_list *list;
308 
309 	KASSERT(eventhandler_lists_initted,
310 	    ("eventhandler list created too early"));
311 
312 	mtx_lock(&eventhandler_mutex);
313 	list = eventhandler_find_or_create_list(name);
314 	mtx_unlock(&eventhandler_mutex);
315 
316 	return (list);
317 }
318