kernel - Major signal path adjustments to fix races, tsleep race fixes, +more
[dragonfly.git] / sys / kern / subr_taskqueue.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 2000 Doug Rabson
3 * All rights reserved.
4 *
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.
13 *
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.
25 *
62ade751 26 * $FreeBSD: src/sys/kern/subr_taskqueue.c,v 1.1.2.3 2003/09/10 00:40:39 ken Exp $
984263bc
MD
27 */
28
29#include <sys/param.h>
30#include <sys/queue.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/taskqueue.h>
34#include <sys/interrupt.h>
b3504e03 35#include <sys/lock.h>
984263bc 36#include <sys/malloc.h>
62ade751 37#include <sys/kthread.h>
e43a034f 38#include <sys/thread2.h>
8619d09d
AH
39#include <sys/spinlock.h>
40#include <sys/spinlock2.h>
41#include <sys/serialize.h>
42#include <sys/proc.h>
43#include <machine/varargs.h>
62ade751 44
984263bc
MD
45MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
46
47static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
8619d09d 48static struct lock taskqueue_queues_lock;
984263bc
MD
49
50struct taskqueue {
51 STAILQ_ENTRY(taskqueue) tq_link;
52 STAILQ_HEAD(, task) tq_queue;
53 const char *tq_name;
54 taskqueue_enqueue_fn tq_enqueue;
55 void *tq_context;
8619d09d
AH
56
57 struct task *tq_running;
58 struct spinlock tq_lock;
59 struct thread **tq_threads;
60 int tq_tcount;
61 int tq_flags;
984263bc
MD
62};
63
8619d09d
AH
64#define TQ_FLAGS_ACTIVE (1 << 0)
65#define TQ_FLAGS_BLOCKED (1 << 1)
66#define TQ_FLAGS_PENDING (1 << 2)
67
3bb42951
MD
68static void taskqueue_run(struct taskqueue *queue, int lock_held);
69
8619d09d
AH
70static __inline void
71TQ_LOCK_INIT(struct taskqueue *tq)
72{
73 spin_init(&tq->tq_lock);
74}
75
76static __inline void
77TQ_LOCK_UNINIT(struct taskqueue *tq)
78{
79 spin_uninit(&tq->tq_lock);
80}
81
82static __inline void
83TQ_LOCK(struct taskqueue *tq)
84{
287a8577 85 spin_lock(&tq->tq_lock);
8619d09d
AH
86}
87
88static __inline void
89TQ_UNLOCK(struct taskqueue *tq)
90{
287a8577 91 spin_unlock(&tq->tq_lock);
8619d09d
AH
92}
93
94static __inline void
95TQ_SLEEP(struct taskqueue *tq, void *ident, const char *wmesg)
96{
97 ssleep(ident, &tq->tq_lock, 0, wmesg, 0);
98}
99
984263bc
MD
100struct taskqueue *
101taskqueue_create(const char *name, int mflags,
102 taskqueue_enqueue_fn enqueue, void *context)
103{
104 struct taskqueue *queue;
984263bc 105
fb525024 106 queue = kmalloc(sizeof(*queue), M_TASKQUEUE, mflags | M_ZERO);
984263bc 107 if (!queue)
8e6d56f3 108 return NULL;
984263bc
MD
109 STAILQ_INIT(&queue->tq_queue);
110 queue->tq_name = name;
111 queue->tq_enqueue = enqueue;
112 queue->tq_context = context;
8619d09d
AH
113 queue->tq_flags |= TQ_FLAGS_ACTIVE;
114 TQ_LOCK_INIT(queue);
984263bc 115
8619d09d 116 lockmgr(&taskqueue_queues_lock, LK_EXCLUSIVE);
984263bc 117 STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
8619d09d 118 lockmgr(&taskqueue_queues_lock, LK_RELEASE);
984263bc
MD
119
120 return queue;
121}
122
8619d09d
AH
123static void
124taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
125{
126 while(tq->tq_tcount > 0) {
127 wakeup(tq);
128 TQ_SLEEP(tq, pp, "taskqueue_terminate");
129 }
130}
131
984263bc
MD
132void
133taskqueue_free(struct taskqueue *queue)
134{
8619d09d
AH
135 TQ_LOCK(queue);
136 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
3bb42951 137 taskqueue_run(queue, 1);
8619d09d
AH
138 taskqueue_terminate(queue->tq_threads, queue);
139 TQ_UNLOCK(queue);
140
141 lockmgr(&taskqueue_queues_lock, LK_EXCLUSIVE);
984263bc 142 STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
8619d09d
AH
143 lockmgr(&taskqueue_queues_lock, LK_RELEASE);
144
145 TQ_LOCK_UNINIT(queue);
984263bc 146
efda3bd0 147 kfree(queue, M_TASKQUEUE);
984263bc
MD
148}
149
150struct taskqueue *
151taskqueue_find(const char *name)
152{
153 struct taskqueue *queue;
984263bc 154
8619d09d 155 lockmgr(&taskqueue_queues_lock, LK_EXCLUSIVE);
e43a034f 156 STAILQ_FOREACH(queue, &taskqueue_queues, tq_link) {
984263bc 157 if (!strcmp(queue->tq_name, name)) {
8619d09d 158 lockmgr(&taskqueue_queues_lock, LK_RELEASE);
984263bc
MD
159 return queue;
160 }
e43a034f 161 }
8619d09d 162 lockmgr(&taskqueue_queues_lock, LK_RELEASE);
8e6d56f3 163 return NULL;
984263bc
MD
164}
165
0bb018c6
SS
166/*
167 * NOTE! If using the per-cpu taskqueues ``taskqueue_thread[mycpuid]'',
168 * be sure NOT TO SHARE the ``task'' between CPUs. TASKS ARE NOT LOCKED.
169 * So either use a throwaway task which will only be enqueued once, or
170 * use one task per CPU!
171 */
984263bc
MD
172int
173taskqueue_enqueue(struct taskqueue *queue, struct task *task)
174{
175 struct task *ins;
176 struct task *prev;
177
8619d09d 178 TQ_LOCK(queue);
984263bc
MD
179
180 /*
181 * Don't allow new tasks on a queue which is being freed.
182 */
8619d09d
AH
183 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0) {
184 TQ_UNLOCK(queue);
984263bc
MD
185 return EPIPE;
186 }
187
188 /*
189 * Count multiple enqueues.
190 */
191 if (task->ta_pending) {
192 task->ta_pending++;
8619d09d 193 TQ_UNLOCK(queue);
984263bc
MD
194 return 0;
195 }
196
197 /*
198 * Optimise the case when all tasks have the same priority.
199 */
200 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
201 if (!prev || prev->ta_priority >= task->ta_priority) {
202 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
203 } else {
8619d09d 204 prev = NULL;
984263bc
MD
205 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
206 prev = ins, ins = STAILQ_NEXT(ins, ta_link))
207 if (ins->ta_priority < task->ta_priority)
208 break;
209
210 if (prev)
211 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
212 else
213 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
214 }
215
216 task->ta_pending = 1;
8619d09d
AH
217 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0) {
218 if (queue->tq_enqueue)
219 queue->tq_enqueue(queue->tq_context);
220 } else {
221 queue->tq_flags |= TQ_FLAGS_PENDING;
222 }
984263bc 223
8619d09d 224 TQ_UNLOCK(queue);
984263bc
MD
225
226 return 0;
227}
228
229void
8619d09d
AH
230taskqueue_block(struct taskqueue *queue)
231{
232 TQ_LOCK(queue);
233 queue->tq_flags |= TQ_FLAGS_BLOCKED;
234 TQ_UNLOCK(queue);
235}
236
237void
238taskqueue_unblock(struct taskqueue *queue)
239{
240 TQ_LOCK(queue);
241 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
242 if (queue->tq_flags & TQ_FLAGS_PENDING) {
243 queue->tq_flags &= ~TQ_FLAGS_PENDING;
244 if (queue->tq_enqueue)
245 queue->tq_enqueue(queue->tq_context);
246 }
247 TQ_UNLOCK(queue);
248}
249
250void
3bb42951 251taskqueue_run(struct taskqueue *queue, int lock_held)
984263bc 252{
984263bc
MD
253 struct task *task;
254 int pending;
255
3bb42951
MD
256 if (lock_held == 0)
257 TQ_LOCK(queue);
984263bc
MD
258 while (STAILQ_FIRST(&queue->tq_queue)) {
259 /*
260 * Carefully remove the first task from the queue and
261 * zero its pending count.
262 */
263 task = STAILQ_FIRST(&queue->tq_queue);
264 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
265 pending = task->ta_pending;
266 task->ta_pending = 0;
8619d09d
AH
267 queue->tq_running = task;
268 TQ_UNLOCK(queue);
984263bc
MD
269
270 task->ta_func(task->ta_context, pending);
271
8619d09d
AH
272 TQ_LOCK(queue);
273 queue->tq_running = NULL;
274 wakeup(task);
984263bc 275 }
3bb42951
MD
276 if (lock_held == 0)
277 TQ_UNLOCK(queue);
8619d09d
AH
278}
279
280void
281taskqueue_drain(struct taskqueue *queue, struct task *task)
282{
283 TQ_LOCK(queue);
284 while (task->ta_pending != 0 || task == queue->tq_running)
285 TQ_SLEEP(queue, task, "-");
286 TQ_UNLOCK(queue);
984263bc
MD
287}
288
289static void
290taskqueue_swi_enqueue(void *context)
291{
292 setsofttq();
293}
294
295static void
477d3c1c 296taskqueue_swi_run(void *arg, void *frame)
984263bc 297{
3bb42951 298 taskqueue_run(taskqueue_swi, 0);
984263bc
MD
299}
300
8619d09d
AH
301static void
302taskqueue_swi_mp_run(void *arg, void *frame)
303{
3bb42951 304 taskqueue_run(taskqueue_swi_mp, 0);
8619d09d
AH
305}
306
307int
6b26ab14 308taskqueue_start_threads(struct taskqueue **tqp, int count, int pri, int ncpu,
8619d09d
AH
309 const char *fmt, ...)
310{
311 __va_list ap;
312 struct thread *td;
313 struct taskqueue *tq;
6b26ab14 314 int i, error, cpu;
8619d09d
AH
315 char ktname[MAXCOMLEN];
316
317 if (count <= 0)
318 return EINVAL;
319
320 tq = *tqp;
6b26ab14 321 cpu = ncpu;
8619d09d
AH
322
323 __va_start(ap, fmt);
324 kvsnprintf(ktname, MAXCOMLEN, fmt, ap);
325 __va_end(ap);
326
327 tq->tq_threads = kmalloc(sizeof(struct thread *) * count, M_TASKQUEUE,
328 M_WAITOK | M_ZERO);
329
330 for (i = 0; i < count; i++) {
6b26ab14
AH
331 /*
332 * If no specific cpu was specified and more than one thread
333 * is to be created, we distribute the threads amongst all
334 * cpus.
335 */
336 if ((ncpu <= -1) && (count > 1))
337 cpu = i%ncpus;
338
da5ac978
AH
339 if (count == 1) {
340 error = lwkt_create(taskqueue_thread_loop, tqp,
fdce8919 341 &tq->tq_threads[i], NULL,
4643740a 342 TDF_NOSTART, cpu,
fdce8919 343 "%s", ktname);
da5ac978
AH
344 } else {
345 error = lwkt_create(taskqueue_thread_loop, tqp,
fdce8919 346 &tq->tq_threads[i], NULL,
4643740a 347 TDF_NOSTART, cpu,
fdce8919 348 "%s_%d", ktname, i);
da5ac978 349 }
8619d09d 350 if (error) {
98a14266 351 kprintf("%s: lwkt_create(%s): error %d", __func__,
8619d09d
AH
352 ktname, error);
353 tq->tq_threads[i] = NULL;
354 } else {
355 td = tq->tq_threads[i];
3723ced5 356 lwkt_setpri_initial(td, pri);
8619d09d
AH
357 lwkt_schedule(td);
358 tq->tq_tcount++;
359 }
360 }
361
362 return 0;
363}
364
365void
366taskqueue_thread_loop(void *arg)
367{
368 struct taskqueue **tqp, *tq;
369
370 tqp = arg;
371 tq = *tqp;
372 TQ_LOCK(tq);
373 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
3bb42951 374 taskqueue_run(tq, 1);
6b26ab14 375 TQ_SLEEP(tq, tq, "tqthr");
8619d09d
AH
376 }
377
378 /* rendezvous with thread that asked us to terminate */
379 tq->tq_tcount--;
380 wakeup_one(tq->tq_threads);
381 TQ_UNLOCK(tq);
382 lwkt_exit();
383}
384
385void
386taskqueue_thread_enqueue(void *context)
387{
388 struct taskqueue **tqp, *tq;
389
390 tqp = context;
391 tq = *tqp;
392
393 wakeup_one(tq);
394}
395
b3504e03 396TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
1da8d52f 397 register_swi(SWI_TQ, taskqueue_swi_run, NULL, "swi_taskq", NULL, -1));
8619d09d
AH
398/*
399 * XXX: possibly use a different SWI_TQ_MP or so.
400 * related: sys/interrupt.h
401 * related: platform/XXX/isa/ipl_funcs.c
402 */
403TASKQUEUE_DEFINE(swi_mp, taskqueue_swi_enqueue, 0,
1da8d52f 404 register_swi(SWI_TQ, taskqueue_swi_mp_run, NULL, "swi_mp_taskq", NULL, -1));
b3504e03 405
b3504e03 406struct taskqueue *taskqueue_thread[MAXCPU];
b3504e03
JH
407
408static void
409taskqueue_init(void)
410{
411 int cpu;
412
8619d09d
AH
413 lockinit(&taskqueue_queues_lock, "tqqueues", 0, 0);
414 STAILQ_INIT(&taskqueue_queues);
415
b3504e03
JH
416 for (cpu = 0; cpu < ncpus; cpu++) {
417 taskqueue_thread[cpu] = taskqueue_create("thread", M_INTWAIT,
6b26ab14
AH
418 taskqueue_thread_enqueue, &taskqueue_thread[cpu]);
419 taskqueue_start_threads(&taskqueue_thread[cpu], 1,
da5ac978 420 TDPRI_KERN_DAEMON, cpu, "taskq_cpu %d", cpu);
b3504e03
JH
421 }
422}
423
8619d09d 424SYSINIT(taskqueueinit, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, taskqueue_init, NULL);