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