Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / sys / kern / subr_taskqueue.c
blob050d7131fb16f4675770b5a62205a46d2e37bc62
1 /*-
2 * Copyright (c) 2000 Doug Rabson
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/sched.h>
41 #include <sys/taskqueue.h>
42 #include <sys/unistd.h>
43 #include <machine/stdarg.h>
45 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
46 static void *taskqueue_giant_ih;
47 static void *taskqueue_ih;
48 static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
49 static struct mtx taskqueue_queues_mutex;
51 struct taskqueue {
52 STAILQ_ENTRY(taskqueue) tq_link;
53 STAILQ_HEAD(, task) tq_queue;
54 const char *tq_name;
55 taskqueue_enqueue_fn tq_enqueue;
56 void *tq_context;
57 struct task *tq_running;
58 struct mtx tq_mutex;
59 struct thread **tq_threads;
60 int tq_tcount;
61 int tq_spin;
62 int tq_flags;
65 #define TQ_FLAGS_ACTIVE (1 << 0)
66 #define TQ_FLAGS_BLOCKED (1 << 1)
67 #define TQ_FLAGS_PENDING (1 << 2)
69 static __inline void
70 TQ_LOCK(struct taskqueue *tq)
72 if (tq->tq_spin)
73 mtx_lock_spin(&tq->tq_mutex);
74 else
75 mtx_lock(&tq->tq_mutex);
78 static __inline void
79 TQ_UNLOCK(struct taskqueue *tq)
81 if (tq->tq_spin)
82 mtx_unlock_spin(&tq->tq_mutex);
83 else
84 mtx_unlock(&tq->tq_mutex);
87 static void init_taskqueue_list(void *data);
89 static __inline int
90 TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
91 int t)
93 if (tq->tq_spin)
94 return (msleep_spin(p, m, wm, t));
95 return (msleep(p, m, pri, wm, t));
98 static void
99 init_taskqueue_list(void *data __unused)
102 mtx_init(&taskqueue_queues_mutex, "taskqueue list", NULL, MTX_DEF);
103 STAILQ_INIT(&taskqueue_queues);
105 SYSINIT(taskqueue_list, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_taskqueue_list,
106 NULL);
108 static struct taskqueue *
109 _taskqueue_create(const char *name, int mflags,
110 taskqueue_enqueue_fn enqueue, void *context,
111 int mtxflags, const char *mtxname)
113 struct taskqueue *queue;
115 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
116 if (!queue)
117 return 0;
119 STAILQ_INIT(&queue->tq_queue);
120 queue->tq_name = name;
121 queue->tq_enqueue = enqueue;
122 queue->tq_context = context;
123 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
124 queue->tq_flags |= TQ_FLAGS_ACTIVE;
125 mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
127 mtx_lock(&taskqueue_queues_mutex);
128 STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
129 mtx_unlock(&taskqueue_queues_mutex);
131 return queue;
134 struct taskqueue *
135 taskqueue_create(const char *name, int mflags,
136 taskqueue_enqueue_fn enqueue, void *context)
138 return _taskqueue_create(name, mflags, enqueue, context,
139 MTX_DEF, "taskqueue");
143 * Signal a taskqueue thread to terminate.
145 static void
146 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
149 while (tq->tq_tcount > 0) {
150 wakeup(tq);
151 TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
155 void
156 taskqueue_free(struct taskqueue *queue)
159 mtx_lock(&taskqueue_queues_mutex);
160 STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
161 mtx_unlock(&taskqueue_queues_mutex);
163 TQ_LOCK(queue);
164 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
165 taskqueue_run(queue);
166 taskqueue_terminate(queue->tq_threads, queue);
167 mtx_destroy(&queue->tq_mutex);
168 free(queue->tq_threads, M_TASKQUEUE);
169 free(queue, M_TASKQUEUE);
173 * Returns with the taskqueue locked.
175 struct taskqueue *
176 taskqueue_find(const char *name)
178 struct taskqueue *queue;
180 mtx_lock(&taskqueue_queues_mutex);
181 STAILQ_FOREACH(queue, &taskqueue_queues, tq_link) {
182 if (strcmp(queue->tq_name, name) == 0) {
183 TQ_LOCK(queue);
184 mtx_unlock(&taskqueue_queues_mutex);
185 return queue;
188 mtx_unlock(&taskqueue_queues_mutex);
189 return NULL;
193 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
195 struct task *ins;
196 struct task *prev;
198 TQ_LOCK(queue);
201 * Count multiple enqueues.
203 if (task->ta_pending) {
204 task->ta_pending++;
205 TQ_UNLOCK(queue);
206 return 0;
210 * Optimise the case when all tasks have the same priority.
212 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
213 if (!prev || prev->ta_priority >= task->ta_priority) {
214 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
215 } else {
216 prev = 0;
217 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
218 prev = ins, ins = STAILQ_NEXT(ins, ta_link))
219 if (ins->ta_priority < task->ta_priority)
220 break;
222 if (prev)
223 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
224 else
225 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
228 task->ta_pending = 1;
229 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
230 queue->tq_enqueue(queue->tq_context);
231 else
232 queue->tq_flags |= TQ_FLAGS_PENDING;
234 TQ_UNLOCK(queue);
236 return 0;
239 void
240 taskqueue_block(struct taskqueue *queue)
243 TQ_LOCK(queue);
244 queue->tq_flags |= TQ_FLAGS_BLOCKED;
245 TQ_UNLOCK(queue);
248 void
249 taskqueue_unblock(struct taskqueue *queue)
252 TQ_LOCK(queue);
253 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
254 if (queue->tq_flags & TQ_FLAGS_PENDING) {
255 queue->tq_flags &= ~TQ_FLAGS_PENDING;
256 queue->tq_enqueue(queue->tq_context);
258 TQ_UNLOCK(queue);
261 void
262 taskqueue_run(struct taskqueue *queue)
264 struct task *task;
265 int owned, pending;
267 owned = mtx_owned(&queue->tq_mutex);
268 if (!owned)
269 TQ_LOCK(queue);
270 while (STAILQ_FIRST(&queue->tq_queue)) {
272 * Carefully remove the first task from the queue and
273 * zero its pending count.
275 task = STAILQ_FIRST(&queue->tq_queue);
276 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
277 pending = task->ta_pending;
278 task->ta_pending = 0;
279 queue->tq_running = task;
280 TQ_UNLOCK(queue);
282 task->ta_func(task->ta_context, pending);
284 TQ_LOCK(queue);
285 queue->tq_running = NULL;
286 wakeup(task);
290 * For compatibility, unlock on return if the queue was not locked
291 * on entry, although this opens a race window.
293 if (!owned)
294 TQ_UNLOCK(queue);
297 void
298 taskqueue_drain(struct taskqueue *queue, struct task *task)
300 if (queue->tq_spin) { /* XXX */
301 mtx_lock_spin(&queue->tq_mutex);
302 while (task->ta_pending != 0 || task == queue->tq_running)
303 msleep_spin(task, &queue->tq_mutex, "-", 0);
304 mtx_unlock_spin(&queue->tq_mutex);
305 } else {
306 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
308 mtx_lock(&queue->tq_mutex);
309 while (task->ta_pending != 0 || task == queue->tq_running)
310 msleep(task, &queue->tq_mutex, PWAIT, "-", 0);
311 mtx_unlock(&queue->tq_mutex);
315 static void
316 taskqueue_swi_enqueue(void *context)
318 swi_sched(taskqueue_ih, 0);
321 static void
322 taskqueue_swi_run(void *dummy)
324 taskqueue_run(taskqueue_swi);
327 static void
328 taskqueue_swi_giant_enqueue(void *context)
330 swi_sched(taskqueue_giant_ih, 0);
333 static void
334 taskqueue_swi_giant_run(void *dummy)
336 taskqueue_run(taskqueue_swi_giant);
340 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
341 const char *name, ...)
343 va_list ap;
344 struct thread *td;
345 struct taskqueue *tq;
346 int i, error;
347 char ktname[MAXCOMLEN];
349 if (count <= 0)
350 return (EINVAL);
352 tq = *tqp;
354 va_start(ap, name);
355 vsnprintf(ktname, MAXCOMLEN, name, ap);
356 va_end(ap);
358 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
359 M_NOWAIT | M_ZERO);
360 if (tq->tq_threads == NULL) {
361 printf("%s: no memory for %s threads\n", __func__, ktname);
362 return (ENOMEM);
365 for (i = 0; i < count; i++) {
366 if (count == 1)
367 error = kthread_add(taskqueue_thread_loop, tqp, NULL,
368 &tq->tq_threads[i], RFSTOPPED, 0, ktname);
369 else
370 error = kthread_add(taskqueue_thread_loop, tqp, NULL,
371 &tq->tq_threads[i], RFSTOPPED, 0,
372 "%s_%d", ktname, i);
373 if (error) {
374 /* should be ok to continue, taskqueue_free will dtrt */
375 printf("%s: kthread_add(%s): error %d", __func__,
376 ktname, error);
377 tq->tq_threads[i] = NULL; /* paranoid */
378 } else
379 tq->tq_tcount++;
381 for (i = 0; i < count; i++) {
382 if (tq->tq_threads[i] == NULL)
383 continue;
384 td = tq->tq_threads[i];
385 thread_lock(td);
386 sched_prio(td, pri);
387 sched_add(td, SRQ_BORING);
388 thread_unlock(td);
391 return (0);
394 void
395 taskqueue_thread_loop(void *arg)
397 struct taskqueue **tqp, *tq;
399 tqp = arg;
400 tq = *tqp;
401 TQ_LOCK(tq);
402 do {
403 taskqueue_run(tq);
404 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
405 } while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0);
407 /* rendezvous with thread that asked us to terminate */
408 tq->tq_tcount--;
409 wakeup_one(tq->tq_threads);
410 TQ_UNLOCK(tq);
411 kthread_exit();
414 void
415 taskqueue_thread_enqueue(void *context)
417 struct taskqueue **tqp, *tq;
419 tqp = context;
420 tq = *tqp;
422 mtx_assert(&tq->tq_mutex, MA_OWNED);
423 wakeup_one(tq);
426 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
427 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
428 INTR_MPSAFE, &taskqueue_ih));
430 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, 0,
431 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
432 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
434 TASKQUEUE_DEFINE_THREAD(thread);
436 struct taskqueue *
437 taskqueue_create_fast(const char *name, int mflags,
438 taskqueue_enqueue_fn enqueue, void *context)
440 return _taskqueue_create(name, mflags, enqueue, context,
441 MTX_SPIN, "fast_taskqueue");
444 /* NB: for backwards compatibility */
446 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
448 return taskqueue_enqueue(queue, task);
451 static void *taskqueue_fast_ih;
453 static void
454 taskqueue_fast_enqueue(void *context)
456 swi_sched(taskqueue_fast_ih, 0);
459 static void
460 taskqueue_fast_run(void *dummy)
462 taskqueue_run(taskqueue_fast);
465 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, 0,
466 swi_add(NULL, "Fast task queue", taskqueue_fast_run, NULL,
467 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));