Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | /* * Copyright (c) 2016-2019 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /*- * Copyright (c) 1999 Michael Smith <msmith@freebsd.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <sys/cdefs.h> #include <sys/param.h> #include <sys/kernel.h> #include <kern/queue.h> #include <kern/locks.h> #include <sys/malloc.h> #include <sys/proc.h> #include <sys/systm.h> #include <sys/mcache.h> #include <sys/eventhandler.h> #include <sys/sysctl.h> int evh_debug = 0; SYSCTL_NODE(_kern, OID_AUTO, eventhandler, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Eventhandler"); SYSCTL_INT(_kern_eventhandler, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_LOCKED, &evh_debug, 0, "Eventhandler debug mode"); struct eventhandler_entry_arg eventhandler_entry_dummy_arg = { .ee_fm_uuid = { 0 }, .ee_fr_uuid = { 0 } }; /* List of 'slow' lists */ static struct eventhandler_lists_ctxt evthdlr_lists_ctxt_glb; static LCK_GRP_DECLARE(eventhandler_mutex_grp, "eventhandler"); static unsigned int eg_size; /* size of eventhandler_entry_generic */ static struct mcache *eg_cache; /* mcache for eventhandler_entry_generic */ static unsigned int el_size; /* size of eventhandler_list */ static struct mcache *el_cache; /* mcache for eventhandler_list */ LCK_GRP_DECLARE(el_lock_grp, "eventhandler list"); LCK_ATTR_DECLARE(el_lock_attr, 0, 0); struct eventhandler_entry_generic { struct eventhandler_entry ee; void *func; }; static struct eventhandler_list *_eventhandler_find_list( struct eventhandler_lists_ctxt *evthdlr_lists_ctxt, const char *name); void eventhandler_lists_ctxt_init(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt) { VERIFY(evthdlr_lists_ctxt != NULL); TAILQ_INIT(&evthdlr_lists_ctxt->eventhandler_lists); evthdlr_lists_ctxt->eventhandler_lists_initted = 1; lck_mtx_init(&evthdlr_lists_ctxt->eventhandler_mutex, &eventhandler_mutex_grp, LCK_ATTR_NULL); } /* * Initialize the eventhandler mutex and list. */ void eventhandler_init(void) { eventhandler_lists_ctxt_init(&evthdlr_lists_ctxt_glb); eg_size = sizeof(struct eventhandler_entry_generic); eg_cache = mcache_create("eventhdlr_generic", eg_size, sizeof(uint64_t), 0, MCR_SLEEP); el_size = sizeof(struct eventhandler_list); el_cache = mcache_create("eventhdlr_list", el_size, sizeof(uint64_t), 0, MCR_SLEEP); } void eventhandler_reap_caches(boolean_t purge) { mcache_reap_now(eg_cache, purge); mcache_reap_now(el_cache, purge); } /* * Insertion is O(n) due to the priority scan, but optimises to O(1) * if all priorities are identical. */ static eventhandler_tag eventhandler_register_internal( struct eventhandler_lists_ctxt *evthdlr_lists_ctxt, struct eventhandler_list *list, const char *name, eventhandler_tag epn) { struct eventhandler_list *new_list; struct eventhandler_entry *ep; VERIFY(strlen(name) <= (sizeof(new_list->el_name) - 1)); if (evthdlr_lists_ctxt == NULL) { evthdlr_lists_ctxt = &evthdlr_lists_ctxt_glb; } VERIFY(evthdlr_lists_ctxt->eventhandler_lists_initted); /* eventhandler registered too early */ VERIFY(epn != NULL); /* cannot register NULL event */ /* lock the eventhandler lists */ lck_mtx_lock_spin(&evthdlr_lists_ctxt->eventhandler_mutex); /* Do we need to find/create the (slow) list? */ if (list == NULL) { /* look for a matching, existing list */ list = _eventhandler_find_list(evthdlr_lists_ctxt, name); /* Do we need to create the list? */ if (list == NULL) { lck_mtx_convert_spin(&evthdlr_lists_ctxt->eventhandler_mutex); new_list = mcache_alloc(el_cache, MCR_SLEEP); if (new_list == NULL) { evhlog((LOG_DEBUG, "%s: Can't allocate list \"%s\"", __func__, name)); lck_mtx_unlock(&evthdlr_lists_ctxt->eventhandler_mutex); return NULL; } bzero(new_list, el_size); evhlog((LOG_DEBUG, "%s: creating list \"%s\"", __func__, name)); list = new_list; list->el_flags = 0; list->el_runcount = 0; bzero(&list->el_lock, sizeof(list->el_lock)); (void) snprintf(list->el_name, sizeof(list->el_name), "%s", name); TAILQ_INSERT_HEAD(&evthdlr_lists_ctxt->eventhandler_lists, list, el_link); } } if (!(list->el_flags & EHL_INITTED)) { TAILQ_INIT(&list->el_entries); EHL_LOCK_INIT(list); list->el_flags |= EHL_INITTED; } lck_mtx_unlock(&evthdlr_lists_ctxt->eventhandler_mutex); KASSERT(epn->ee_priority != EHE_DEAD_PRIORITY, ("%s: handler for %s registered with dead priority", __func__, name)); /* sort it into the list */ evhlog((LOG_DEBUG, "%s: adding item %p (function %p to \"%s\"", __func__, (void *)VM_KERNEL_ADDRPERM(epn), (void *)VM_KERNEL_UNSLIDE(((struct eventhandler_entry_generic *)epn)->func), name)); EHL_LOCK(list); TAILQ_FOREACH(ep, &list->el_entries, ee_link) { if (ep->ee_priority != EHE_DEAD_PRIORITY && epn->ee_priority < ep->ee_priority) { TAILQ_INSERT_BEFORE(ep, epn, ee_link); break; } } if (ep == NULL) { TAILQ_INSERT_TAIL(&list->el_entries, epn, ee_link); } EHL_UNLOCK(list); return epn; } eventhandler_tag eventhandler_register(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt, struct eventhandler_list *list, const char *name, void *func, struct eventhandler_entry_arg arg, int priority) { struct eventhandler_entry_generic *eg; /* allocate an entry for this handler, populate it */ eg = mcache_alloc(eg_cache, MCR_SLEEP); if (eg == NULL) { evhlog((LOG_DEBUG, "%s: Can't allocate entry to register for event list " "\"%s\"", __func__, name)); return NULL; } bzero(eg, eg_size); eg->func = func; eg->ee.ee_arg = arg; eg->ee.ee_priority = priority; return eventhandler_register_internal(evthdlr_lists_ctxt, list, name, &eg->ee); } void eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag) { struct eventhandler_entry *ep = tag; EHL_LOCK_ASSERT(list, LCK_MTX_ASSERT_OWNED); if (ep != NULL) { /* remove just this entry */ if (list->el_runcount == 0) { evhlog((LOG_DEBUG, "%s: removing item %p from \"%s\"", __func__, (void *)VM_KERNEL_ADDRPERM(ep), list->el_name)); /* * We may have purged the list because of certain events. * Make sure that is not the case when a specific entry * is being removed. */ if (!TAILQ_EMPTY(&list->el_entries)) { TAILQ_REMOVE(&list->el_entries, ep, ee_link); } EHL_LOCK_CONVERT(list); mcache_free(eg_cache, ep); } else { evhlog((LOG_DEBUG, "%s: marking item %p from \"%s\" as dead", __func__, (void *)VM_KERNEL_ADDRPERM(ep), list->el_name)); ep->ee_priority = EHE_DEAD_PRIORITY; } } else { /* remove entire list */ if (list->el_runcount == 0) { evhlog((LOG_DEBUG, "%s: removing all items from \"%s\"", __func__, list->el_name)); EHL_LOCK_CONVERT(list); while (!TAILQ_EMPTY(&list->el_entries)) { ep = TAILQ_FIRST(&list->el_entries); TAILQ_REMOVE(&list->el_entries, ep, ee_link); mcache_free(eg_cache, ep); } } else { evhlog((LOG_DEBUG, "%s: marking all items from \"%s\" as dead", __func__, list->el_name)); TAILQ_FOREACH(ep, &list->el_entries, ee_link) ep->ee_priority = EHE_DEAD_PRIORITY; } } while (list->el_runcount > 0) { msleep((caddr_t)list, &list->el_lock, PSPIN, "evhrm", 0); } EHL_UNLOCK(list); } /* * Internal version for use when eventhandler list is already locked. */ static struct eventhandler_list * _eventhandler_find_list(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt, const char *name) { struct eventhandler_list *list; VERIFY(evthdlr_lists_ctxt != NULL); LCK_MTX_ASSERT(&evthdlr_lists_ctxt->eventhandler_mutex, LCK_MTX_ASSERT_OWNED); TAILQ_FOREACH(list, &evthdlr_lists_ctxt->eventhandler_lists, el_link) { if (!strcmp(name, list->el_name)) { break; } } return list; } /* * Lookup a "slow" list by name. Returns with the list locked. */ struct eventhandler_list * eventhandler_find_list(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt, const char *name) { struct eventhandler_list *list; if (evthdlr_lists_ctxt == NULL) { evthdlr_lists_ctxt = &evthdlr_lists_ctxt_glb; } if (!evthdlr_lists_ctxt->eventhandler_lists_initted) { return NULL; } /* scan looking for the requested list */ lck_mtx_lock_spin(&evthdlr_lists_ctxt->eventhandler_mutex); list = _eventhandler_find_list(evthdlr_lists_ctxt, name); if (list != NULL) { lck_mtx_convert_spin(&evthdlr_lists_ctxt->eventhandler_mutex); EHL_LOCK_SPIN(list); } lck_mtx_unlock(&evthdlr_lists_ctxt->eventhandler_mutex); return list; } /* * Prune "dead" entries from an eventhandler list. */ void eventhandler_prune_list(struct eventhandler_list *list) { struct eventhandler_entry *ep, *en; int pruned = 0; evhlog((LOG_DEBUG, "%s: pruning list \"%s\"", __func__, list->el_name)); EHL_LOCK_ASSERT(list, LCK_MTX_ASSERT_OWNED); TAILQ_FOREACH_SAFE(ep, &list->el_entries, ee_link, en) { if (ep->ee_priority == EHE_DEAD_PRIORITY) { TAILQ_REMOVE(&list->el_entries, ep, ee_link); mcache_free(eg_cache, ep); pruned++; } } if (pruned > 0) { wakeup(list); } } /* * This should be called when last reference to an object * is being released. * The individual event type lists must be purged when the object * becomes defunct. */ void eventhandler_lists_ctxt_destroy(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt) { struct eventhandler_list *list = NULL; struct eventhandler_list *list_next = NULL; lck_mtx_lock(&evthdlr_lists_ctxt->eventhandler_mutex); TAILQ_FOREACH_SAFE(list, &evthdlr_lists_ctxt->eventhandler_lists, el_link, list_next) { VERIFY(TAILQ_EMPTY(&list->el_entries)); EHL_LOCK_DESTROY(list); mcache_free(el_cache, list); } lck_mtx_unlock(&evthdlr_lists_ctxt->eventhandler_mutex); lck_mtx_destroy(&evthdlr_lists_ctxt->eventhandler_mutex, &eventhandler_mutex_grp); return; } |