1 1.23 riastrad /* $NetBSD: sysmon_taskq.c,v 1.23 2021/12/31 14:29:14 riastradh Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /* 4 1.1 thorpej * Copyright (c) 2001, 2003 Wasabi Systems, Inc. 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 1.1 thorpej * 9 1.1 thorpej * Redistribution and use in source and binary forms, with or without 10 1.1 thorpej * modification, are permitted provided that the following conditions 11 1.1 thorpej * are met: 12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 13 1.1 thorpej * notice, this list of conditions and the following disclaimer. 14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 16 1.1 thorpej * documentation and/or other materials provided with the distribution. 17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software 18 1.1 thorpej * must display the following acknowledgement: 19 1.1 thorpej * This product includes software developed for the NetBSD Project by 20 1.1 thorpej * Wasabi Systems, Inc. 21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 1.1 thorpej * or promote products derived from this software without specific prior 23 1.1 thorpej * written permission. 24 1.1 thorpej * 25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 36 1.1 thorpej */ 37 1.1 thorpej 38 1.1 thorpej /* 39 1.1 thorpej * General purpose task queue for sysmon back-ends. This can be 40 1.1 thorpej * used to run callbacks that require thread context. 41 1.1 thorpej */ 42 1.2 lukem 43 1.2 lukem #include <sys/cdefs.h> 44 1.23 riastrad __KERNEL_RCSID(0, "$NetBSD: sysmon_taskq.c,v 1.23 2021/12/31 14:29:14 riastradh Exp $"); 45 1.1 thorpej 46 1.1 thorpej #include <sys/param.h> 47 1.1 thorpej #include <sys/malloc.h> 48 1.1 thorpej #include <sys/queue.h> 49 1.1 thorpej #include <sys/proc.h> 50 1.1 thorpej #include <sys/kthread.h> 51 1.1 thorpej #include <sys/systm.h> 52 1.15 pgoyette #include <sys/module.h> 53 1.18 pgoyette #include <sys/once.h> 54 1.1 thorpej 55 1.1 thorpej #include <dev/sysmon/sysmon_taskq.h> 56 1.1 thorpej 57 1.1 thorpej struct sysmon_task { 58 1.1 thorpej TAILQ_ENTRY(sysmon_task) st_list; 59 1.1 thorpej void (*st_func)(void *); 60 1.1 thorpej void *st_arg; 61 1.1 thorpej u_int st_pri; 62 1.1 thorpej }; 63 1.1 thorpej 64 1.1 thorpej static TAILQ_HEAD(, sysmon_task) sysmon_task_queue = 65 1.1 thorpej TAILQ_HEAD_INITIALIZER(sysmon_task_queue); 66 1.1 thorpej 67 1.10 xtraeme static kmutex_t sysmon_task_queue_mtx; 68 1.10 xtraeme static kmutex_t sysmon_task_queue_init_mtx; 69 1.10 xtraeme static kcondvar_t sysmon_task_queue_cv; 70 1.1 thorpej 71 1.10 xtraeme static int sysmon_task_queue_initialized; 72 1.4 christos static int sysmon_task_queue_cleanup_sem; 73 1.9 ad static struct lwp *sysmon_task_queue_lwp; 74 1.10 xtraeme static void sysmon_task_queue_thread(void *); 75 1.1 thorpej 76 1.15 pgoyette MODULE(MODULE_CLASS_MISC, sysmon_taskq, NULL); 77 1.15 pgoyette 78 1.15 pgoyette /* 79 1.15 pgoyette * XXX Normally, all initialization would be handled as part of 80 1.15 pgoyette * the module(9) framework. However, there are a number of 81 1.15 pgoyette * users of the sysmon_taskq facility that are not modular, 82 1.15 pgoyette * and these can directly call sysmon_task_queue_init() 83 1.20 dholland * directly. To accommodate these non-standard users, we 84 1.15 pgoyette * make sure that sysmon_task_queue_init() handles multiple 85 1.15 pgoyette * invocations. And we also ensure that, if any non-module 86 1.15 pgoyette * user exists, we don't allow the module to be unloaded. 87 1.15 pgoyette * (We can't use module_hold() for this, since the module(9) 88 1.15 pgoyette * framework itself isn't necessarily initialized yet.) 89 1.15 pgoyette */ 90 1.15 pgoyette 91 1.15 pgoyette /* 92 1.18 pgoyette * tq_preinit: 93 1.15 pgoyette * 94 1.15 pgoyette * Early one-time initialization of task-queue 95 1.15 pgoyette */ 96 1.18 pgoyette 97 1.18 pgoyette ONCE_DECL(once_tq); 98 1.18 pgoyette 99 1.18 pgoyette static int 100 1.18 pgoyette tq_preinit(void) 101 1.10 xtraeme { 102 1.15 pgoyette 103 1.11 ad mutex_init(&sysmon_task_queue_mtx, MUTEX_DEFAULT, IPL_VM); 104 1.10 xtraeme mutex_init(&sysmon_task_queue_init_mtx, MUTEX_DEFAULT, IPL_NONE); 105 1.10 xtraeme cv_init(&sysmon_task_queue_cv, "smtaskq"); 106 1.15 pgoyette sysmon_task_queue_initialized = 0; 107 1.18 pgoyette 108 1.18 pgoyette return 0; 109 1.10 xtraeme } 110 1.1 thorpej 111 1.1 thorpej /* 112 1.1 thorpej * sysmon_task_queue_init: 113 1.1 thorpej * 114 1.1 thorpej * Initialize the sysmon task queue. 115 1.1 thorpej */ 116 1.1 thorpej void 117 1.1 thorpej sysmon_task_queue_init(void) 118 1.1 thorpej { 119 1.9 ad int error; 120 1.1 thorpej 121 1.18 pgoyette (void)RUN_ONCE(&once_tq, tq_preinit); 122 1.18 pgoyette 123 1.10 xtraeme mutex_enter(&sysmon_task_queue_init_mtx); 124 1.15 pgoyette if (sysmon_task_queue_initialized++) { 125 1.10 xtraeme mutex_exit(&sysmon_task_queue_init_mtx); 126 1.1 thorpej return; 127 1.1 thorpej } 128 1.1 thorpej 129 1.10 xtraeme mutex_exit(&sysmon_task_queue_init_mtx); 130 1.1 thorpej 131 1.13 xtraeme error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, 132 1.13 xtraeme sysmon_task_queue_thread, NULL, &sysmon_task_queue_lwp, "sysmon"); 133 1.9 ad if (error) { 134 1.9 ad printf("Unable to create sysmon task queue thread, " 135 1.9 ad "error = %d\n", error); 136 1.9 ad panic("sysmon_task_queue_init"); 137 1.9 ad } 138 1.1 thorpej } 139 1.1 thorpej 140 1.1 thorpej /* 141 1.1 thorpej * sysmon_task_queue_fini: 142 1.1 thorpej * 143 1.1 thorpej * Tear town the sysmon task queue. 144 1.1 thorpej */ 145 1.17 pgoyette int 146 1.1 thorpej sysmon_task_queue_fini(void) 147 1.1 thorpej { 148 1.1 thorpej 149 1.15 pgoyette if (sysmon_task_queue_initialized > 1) 150 1.17 pgoyette return EBUSY; 151 1.15 pgoyette 152 1.10 xtraeme mutex_enter(&sysmon_task_queue_mtx); 153 1.1 thorpej 154 1.1 thorpej sysmon_task_queue_cleanup_sem = 1; 155 1.10 xtraeme cv_signal(&sysmon_task_queue_cv); 156 1.1 thorpej 157 1.10 xtraeme while (sysmon_task_queue_cleanup_sem != 0) 158 1.10 xtraeme cv_wait(&sysmon_task_queue_cv, 159 1.10 xtraeme &sysmon_task_queue_mtx); 160 1.1 thorpej 161 1.10 xtraeme mutex_exit(&sysmon_task_queue_mtx); 162 1.17 pgoyette 163 1.17 pgoyette return 0; 164 1.1 thorpej } 165 1.1 thorpej 166 1.1 thorpej /* 167 1.1 thorpej * sysmon_task_queue_thread: 168 1.1 thorpej * 169 1.1 thorpej * The sysmon task queue execution thread. We execute callbacks that 170 1.1 thorpej * have been queued for us. 171 1.1 thorpej */ 172 1.1 thorpej static void 173 1.7 christos sysmon_task_queue_thread(void *arg) 174 1.1 thorpej { 175 1.1 thorpej struct sysmon_task *st; 176 1.1 thorpej 177 1.1 thorpej /* 178 1.1 thorpej * Run through all the tasks before we check for the exit 179 1.1 thorpej * condition; it's probably more important to actually run 180 1.1 thorpej * all the tasks before we exit. 181 1.1 thorpej */ 182 1.14 gmcgarry mutex_enter(&sysmon_task_queue_mtx); 183 1.1 thorpej for (;;) { 184 1.1 thorpej st = TAILQ_FIRST(&sysmon_task_queue); 185 1.14 gmcgarry if (st != NULL) { 186 1.14 gmcgarry TAILQ_REMOVE(&sysmon_task_queue, st, st_list); 187 1.14 gmcgarry mutex_exit(&sysmon_task_queue_mtx); 188 1.14 gmcgarry (*st->st_func)(st->st_arg); 189 1.14 gmcgarry free(st, M_TEMP); 190 1.14 gmcgarry mutex_enter(&sysmon_task_queue_mtx); 191 1.14 gmcgarry } else { 192 1.1 thorpej /* Check for the exit condition. */ 193 1.14 gmcgarry if (sysmon_task_queue_cleanup_sem != 0) 194 1.14 gmcgarry break; 195 1.10 xtraeme cv_wait(&sysmon_task_queue_cv, &sysmon_task_queue_mtx); 196 1.1 thorpej } 197 1.1 thorpej } 198 1.14 gmcgarry /* Time to die. */ 199 1.14 gmcgarry sysmon_task_queue_cleanup_sem = 0; 200 1.14 gmcgarry cv_broadcast(&sysmon_task_queue_cv); 201 1.14 gmcgarry mutex_exit(&sysmon_task_queue_mtx); 202 1.14 gmcgarry kthread_exit(0); 203 1.1 thorpej } 204 1.1 thorpej 205 1.22 riastrad static void 206 1.22 riastrad sysmon_task_queue_sched_task(struct sysmon_task *st) 207 1.22 riastrad { 208 1.22 riastrad struct sysmon_task *lst; 209 1.22 riastrad 210 1.22 riastrad mutex_enter(&sysmon_task_queue_mtx); 211 1.22 riastrad TAILQ_FOREACH(lst, &sysmon_task_queue, st_list) { 212 1.22 riastrad if (st->st_pri > lst->st_pri) { 213 1.22 riastrad TAILQ_INSERT_BEFORE(lst, st, st_list); 214 1.22 riastrad break; 215 1.22 riastrad } 216 1.22 riastrad } 217 1.22 riastrad 218 1.22 riastrad if (lst == NULL) 219 1.22 riastrad TAILQ_INSERT_TAIL(&sysmon_task_queue, st, st_list); 220 1.22 riastrad 221 1.22 riastrad cv_broadcast(&sysmon_task_queue_cv); 222 1.22 riastrad mutex_exit(&sysmon_task_queue_mtx); 223 1.22 riastrad } 224 1.22 riastrad 225 1.1 thorpej /* 226 1.1 thorpej * sysmon_task_queue_sched: 227 1.1 thorpej * 228 1.1 thorpej * Schedule a task for deferred execution. 229 1.1 thorpej */ 230 1.1 thorpej int 231 1.1 thorpej sysmon_task_queue_sched(u_int pri, void (*func)(void *), void *arg) 232 1.1 thorpej { 233 1.22 riastrad struct sysmon_task *st; 234 1.1 thorpej 235 1.19 martin (void)RUN_ONCE(&once_tq, tq_preinit); 236 1.19 martin 237 1.9 ad if (sysmon_task_queue_lwp == NULL) 238 1.8 xtraeme aprint_debug("WARNING: Callback scheduled before sysmon " 239 1.8 xtraeme "task queue thread present\n"); 240 1.1 thorpej 241 1.1 thorpej if (func == NULL) 242 1.10 xtraeme return EINVAL; 243 1.1 thorpej 244 1.1 thorpej st = malloc(sizeof(*st), M_TEMP, M_NOWAIT); 245 1.1 thorpej if (st == NULL) 246 1.10 xtraeme return ENOMEM; 247 1.1 thorpej 248 1.1 thorpej st->st_func = func; 249 1.1 thorpej st->st_arg = arg; 250 1.1 thorpej st->st_pri = pri; 251 1.1 thorpej 252 1.22 riastrad sysmon_task_queue_sched_task(st); 253 1.22 riastrad 254 1.22 riastrad return 0; 255 1.22 riastrad } 256 1.22 riastrad 257 1.22 riastrad struct tqbarrier { 258 1.22 riastrad kmutex_t lock; 259 1.22 riastrad kcondvar_t cv; 260 1.22 riastrad bool done; 261 1.22 riastrad }; 262 1.22 riastrad 263 1.22 riastrad static void 264 1.22 riastrad tqbarrier_task(void *cookie) 265 1.22 riastrad { 266 1.22 riastrad struct tqbarrier *bar = cookie; 267 1.22 riastrad 268 1.22 riastrad mutex_enter(&bar->lock); 269 1.22 riastrad bar->done = true; 270 1.22 riastrad cv_broadcast(&bar->cv); 271 1.22 riastrad mutex_exit(&bar->lock); 272 1.22 riastrad } 273 1.22 riastrad 274 1.22 riastrad /* 275 1.22 riastrad * sysmon_task_queue_barrier: 276 1.22 riastrad * 277 1.22 riastrad * Wait for the completion of all tasks at priority pri or lower 278 1.22 riastrad * currently queued at the time of the call. 279 1.22 riastrad */ 280 1.22 riastrad void 281 1.22 riastrad sysmon_task_queue_barrier(u_int pri) 282 1.22 riastrad { 283 1.23 riastrad struct sysmon_task *st; 284 1.22 riastrad struct tqbarrier bar; 285 1.22 riastrad 286 1.22 riastrad (void)RUN_ONCE(&once_tq, tq_preinit); 287 1.10 xtraeme 288 1.22 riastrad KASSERT(sysmon_task_queue_lwp); 289 1.22 riastrad KASSERT(curlwp != sysmon_task_queue_lwp); 290 1.1 thorpej 291 1.22 riastrad mutex_init(&bar.lock, MUTEX_DEFAULT, IPL_NONE); 292 1.22 riastrad cv_init(&bar.cv, "sysmontq"); 293 1.22 riastrad bar.done = false; 294 1.22 riastrad 295 1.23 riastrad st = malloc(sizeof(*st), M_TEMP, M_WAITOK); 296 1.23 riastrad st->st_func = &tqbarrier_task; 297 1.23 riastrad st->st_arg = &bar; 298 1.23 riastrad st->st_pri = pri; 299 1.22 riastrad 300 1.23 riastrad sysmon_task_queue_sched_task(st); 301 1.22 riastrad 302 1.22 riastrad mutex_enter(&bar.lock); 303 1.22 riastrad while (!bar.done) 304 1.22 riastrad cv_wait(&bar.cv, &bar.lock); 305 1.22 riastrad mutex_exit(&bar.lock); 306 1.10 xtraeme 307 1.22 riastrad cv_destroy(&bar.cv); 308 1.22 riastrad mutex_destroy(&bar.lock); 309 1.1 thorpej } 310 1.15 pgoyette 311 1.21 riastrad static int 312 1.15 pgoyette sysmon_taskq_modcmd(modcmd_t cmd, void *arg) 313 1.15 pgoyette { 314 1.15 pgoyette int ret; 315 1.21 riastrad 316 1.21 riastrad switch (cmd) { 317 1.15 pgoyette case MODULE_CMD_INIT: 318 1.15 pgoyette sysmon_task_queue_init(); 319 1.15 pgoyette ret = 0; 320 1.15 pgoyette break; 321 1.21 riastrad case MODULE_CMD_FINI: 322 1.17 pgoyette ret = sysmon_task_queue_fini(); 323 1.15 pgoyette break; 324 1.15 pgoyette case MODULE_CMD_STAT: 325 1.21 riastrad default: 326 1.15 pgoyette ret = ENOTTY; 327 1.15 pgoyette } 328 1.21 riastrad 329 1.15 pgoyette return ret; 330 1.15 pgoyette } 331