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