summaryrefslogtreecommitdiff
path: root/sys/dev/sysmon/sysmon_taskq.c
blob: 559987ddc2a19b32a309bdfee846f950e529f9c8 (plain)
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
/*	$NetBSD: sysmon_taskq.c,v 1.23 2021/12/31 14:29:14 riastradh Exp $	*/

/*
 * Copyright (c) 2001, 2003 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed for the NetBSD Project by
 *	Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
 * 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.
 */

/*
 * General purpose task queue for sysmon back-ends.  This can be
 * used to run callbacks that require thread context.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sysmon_taskq.c,v 1.23 2021/12/31 14:29:14 riastradh Exp $");

#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/once.h>

#include <dev/sysmon/sysmon_taskq.h>

struct sysmon_task {
	TAILQ_ENTRY(sysmon_task) st_list;
	void (*st_func)(void *);
	void *st_arg;
	u_int st_pri;
};

static TAILQ_HEAD(, sysmon_task) sysmon_task_queue =
    TAILQ_HEAD_INITIALIZER(sysmon_task_queue);

static kmutex_t sysmon_task_queue_mtx;
static kmutex_t sysmon_task_queue_init_mtx;
static kcondvar_t sysmon_task_queue_cv;

static int sysmon_task_queue_initialized;
static int sysmon_task_queue_cleanup_sem;
static struct lwp *sysmon_task_queue_lwp;
static void sysmon_task_queue_thread(void *);

MODULE(MODULE_CLASS_MISC, sysmon_taskq, NULL);

/*
 * XXX	Normally, all initialization would be handled as part of
 *	the module(9) framework.  However, there are a number of
 *	users of the sysmon_taskq facility that are not modular,
 *	and these can directly call sysmon_task_queue_init()
 *	directly.  To accommodate these non-standard users, we
 *	make sure that sysmon_task_queue_init() handles multiple
 *	invocations.  And we also ensure that, if any non-module
 *	user exists, we don't allow the module to be unloaded.
 *	(We can't use module_hold() for this, since the module(9)
 *	framework itself isn't necessarily initialized yet.)
 */

/*
 * tq_preinit:
 *
 *	Early one-time initialization of task-queue
 */

ONCE_DECL(once_tq);

static int
tq_preinit(void)
{

	mutex_init(&sysmon_task_queue_mtx, MUTEX_DEFAULT, IPL_VM);
	mutex_init(&sysmon_task_queue_init_mtx, MUTEX_DEFAULT, IPL_NONE);
	cv_init(&sysmon_task_queue_cv, "smtaskq");
	sysmon_task_queue_initialized = 0;

	return 0;
}

/*
 * sysmon_task_queue_init:
 *
 *	Initialize the sysmon task queue.
 */
void
sysmon_task_queue_init(void)
{
	int error;

	(void)RUN_ONCE(&once_tq, tq_preinit);

	mutex_enter(&sysmon_task_queue_init_mtx);
	if (sysmon_task_queue_initialized++) {
		mutex_exit(&sysmon_task_queue_init_mtx);
		return;
	}

	mutex_exit(&sysmon_task_queue_init_mtx);

	error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
	    sysmon_task_queue_thread, NULL, &sysmon_task_queue_lwp, "sysmon");
	if (error) {
		printf("Unable to create sysmon task queue thread, "
		    "error = %d\n", error);
		panic("sysmon_task_queue_init");
	}
}

/*
 * sysmon_task_queue_fini:
 *
 *	Tear town the sysmon task queue.
 */
int
sysmon_task_queue_fini(void)
{

	if (sysmon_task_queue_initialized > 1)
		return EBUSY;

	mutex_enter(&sysmon_task_queue_mtx);

	sysmon_task_queue_cleanup_sem = 1;
	cv_signal(&sysmon_task_queue_cv);

	while (sysmon_task_queue_cleanup_sem != 0)
		cv_wait(&sysmon_task_queue_cv,
			&sysmon_task_queue_mtx);

	mutex_exit(&sysmon_task_queue_mtx);

	return 0;
}

/*
 * sysmon_task_queue_thread:
 *
 *	The sysmon task queue execution thread.  We execute callbacks that
 *	have been queued for us.
 */
static void
sysmon_task_queue_thread(void *arg)
{
	struct sysmon_task *st;

	/*
	 * Run through all the tasks before we check for the exit
	 * condition; it's probably more important to actually run
	 * all the tasks before we exit.
	 */
	mutex_enter(&sysmon_task_queue_mtx);
	for (;;) {
		st = TAILQ_FIRST(&sysmon_task_queue);
		if (st != NULL) {
			TAILQ_REMOVE(&sysmon_task_queue, st, st_list);
			mutex_exit(&sysmon_task_queue_mtx);
			(*st->st_func)(st->st_arg);
			free(st, M_TEMP);
			mutex_enter(&sysmon_task_queue_mtx);
		} else {
			/* Check for the exit condition. */
			if (sysmon_task_queue_cleanup_sem != 0)
				break;
			cv_wait(&sysmon_task_queue_cv, &sysmon_task_queue_mtx);
		}
	}
	/* Time to die. */
	sysmon_task_queue_cleanup_sem = 0;
	cv_broadcast(&sysmon_task_queue_cv);
	mutex_exit(&sysmon_task_queue_mtx);
	kthread_exit(0);
}

static void
sysmon_task_queue_sched_task(struct sysmon_task *st)
{
	struct sysmon_task *lst;

	mutex_enter(&sysmon_task_queue_mtx);
	TAILQ_FOREACH(lst, &sysmon_task_queue, st_list) {
		if (st->st_pri > lst->st_pri) {
			TAILQ_INSERT_BEFORE(lst, st, st_list);
			break;
		}
	}

	if (lst == NULL)
		TAILQ_INSERT_TAIL(&sysmon_task_queue, st, st_list);

	cv_broadcast(&sysmon_task_queue_cv);
	mutex_exit(&sysmon_task_queue_mtx);
}

/*
 * sysmon_task_queue_sched:
 *
 *	Schedule a task for deferred execution.
 */
int
sysmon_task_queue_sched(u_int pri, void (*func)(void *), void *arg)
{
	struct sysmon_task *st;

	(void)RUN_ONCE(&once_tq, tq_preinit);

	if (sysmon_task_queue_lwp == NULL)
		aprint_debug("WARNING: Callback scheduled before sysmon "
		    "task queue thread present\n");

	if (func == NULL)
		return EINVAL;

	st = malloc(sizeof(*st), M_TEMP, M_NOWAIT);
	if (st == NULL)
		return ENOMEM;

	st->st_func = func;
	st->st_arg = arg;
	st->st_pri = pri;

	sysmon_task_queue_sched_task(st);

	return 0;
}

struct tqbarrier {
	kmutex_t	lock;
	kcondvar_t	cv;
	bool		done;
};

static void
tqbarrier_task(void *cookie)
{
	struct tqbarrier *bar = cookie;

	mutex_enter(&bar->lock);
	bar->done = true;
	cv_broadcast(&bar->cv);
	mutex_exit(&bar->lock);
}

/*
 * sysmon_task_queue_barrier:
 *
 *	Wait for the completion of all tasks at priority pri or lower
 *	currently queued at the time of the call.
 */
void
sysmon_task_queue_barrier(u_int pri)
{
	struct sysmon_task *st;
	struct tqbarrier bar;

	(void)RUN_ONCE(&once_tq, tq_preinit);

	KASSERT(sysmon_task_queue_lwp);
	KASSERT(curlwp != sysmon_task_queue_lwp);

	mutex_init(&bar.lock, MUTEX_DEFAULT, IPL_NONE);
	cv_init(&bar.cv, "sysmontq");
	bar.done = false;

	st = malloc(sizeof(*st), M_TEMP, M_WAITOK);
	st->st_func = &tqbarrier_task;
	st->st_arg = &bar;
	st->st_pri = pri;

	sysmon_task_queue_sched_task(st);

	mutex_enter(&bar.lock);
	while (!bar.done)
		cv_wait(&bar.cv, &bar.lock);
	mutex_exit(&bar.lock);

	cv_destroy(&bar.cv);
	mutex_destroy(&bar.lock);
}

static int
sysmon_taskq_modcmd(modcmd_t cmd, void *arg)
{
	int ret;

	switch (cmd) {
	case MODULE_CMD_INIT:
		sysmon_task_queue_init();
		ret = 0;
		break;
	case MODULE_CMD_FINI:
		ret = sysmon_task_queue_fini();
		break;
	case MODULE_CMD_STAT:
	default:
		ret = ENOTTY;
	}

	return ret;
}