Fix blocking races in various *_locate() functions within softupdates.
[dragonfly.git] / sys / kern / lwkt_thread.c
CommitLineData
8ad65e08 1/*
8c10bfcf
MD
2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
8ad65e08
MD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
8c10bfcf 10 *
8ad65e08
MD
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
8c10bfcf
MD
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8ad65e08 32 * SUCH DAMAGE.
8c10bfcf 33 *
bbb31c5d 34 * $DragonFly: src/sys/kern/lwkt_thread.c,v 1.100 2006/06/01 19:02:38 dillon Exp $
75cdbe6c
MD
35 */
36
37/*
38 * Each cpu in a system has its own self-contained light weight kernel
39 * thread scheduler, which means that generally speaking we only need
40 * to use a critical section to avoid problems. Foreign thread
41 * scheduling is queued via (async) IPIs.
8ad65e08
MD
42 */
43
05220613
MD
44#ifdef _KERNEL
45
8ad65e08
MD
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/proc.h>
50#include <sys/rtprio.h>
51#include <sys/queue.h>
7d0bac62 52#include <sys/sysctl.h>
99df837e 53#include <sys/kthread.h>
f1d1c3fa 54#include <machine/cpu.h>
99df837e 55#include <sys/lock.h>
f6bf3af1 56#include <sys/caps.h>
9d265729 57#include <sys/spinlock.h>
57aa743c 58#include <sys/ktr.h>
9d265729
MD
59
60#include <sys/thread2.h>
61#include <sys/spinlock2.h>
f1d1c3fa 62
7d0bac62
MD
63#include <vm/vm.h>
64#include <vm/vm_param.h>
65#include <vm/vm_kern.h>
66#include <vm/vm_object.h>
67#include <vm/vm_page.h>
68#include <vm/vm_map.h>
69#include <vm/vm_pager.h>
70#include <vm/vm_extern.h>
71#include <vm/vm_zone.h>
72
99df837e 73#include <machine/stdarg.h>
57c254db 74#include <machine/ipl.h>
96728c05 75#include <machine/smp.h>
99df837e 76
05220613
MD
77#else
78
79#include <sys/stdint.h>
fb04f4fd 80#include <libcaps/thread.h>
05220613
MD
81#include <sys/thread.h>
82#include <sys/msgport.h>
83#include <sys/errno.h>
fb04f4fd 84#include <libcaps/globaldata.h>
7e8303ad 85#include <machine/cpufunc.h>
05220613
MD
86#include <sys/thread2.h>
87#include <sys/msgport2.h>
709799ea 88#include <stdio.h>
05220613 89#include <stdlib.h>
709799ea 90#include <string.h>
709799ea 91#include <machine/lock.h>
b950d4b8
EN
92#include <machine/atomic.h>
93#include <machine/cpu.h>
05220613
MD
94
95#endif
96
7d0bac62 97static int untimely_switch = 0;
0f7a3396
MD
98#ifdef INVARIANTS
99static int panic_on_cscount = 0;
100#endif
05220613
MD
101static __int64_t switch_count = 0;
102static __int64_t preempt_hit = 0;
103static __int64_t preempt_miss = 0;
104static __int64_t preempt_weird = 0;
38717797
HP
105static __int64_t token_contention_count = 0;
106static __int64_t mplock_contention_count = 0;
05220613
MD
107
108#ifdef _KERNEL
109
110SYSCTL_INT(_lwkt, OID_AUTO, untimely_switch, CTLFLAG_RW, &untimely_switch, 0, "");
0f7a3396
MD
111#ifdef INVARIANTS
112SYSCTL_INT(_lwkt, OID_AUTO, panic_on_cscount, CTLFLAG_RW, &panic_on_cscount, 0, "");
113#endif
4b5f931b 114SYSCTL_QUAD(_lwkt, OID_AUTO, switch_count, CTLFLAG_RW, &switch_count, 0, "");
4b5f931b 115SYSCTL_QUAD(_lwkt, OID_AUTO, preempt_hit, CTLFLAG_RW, &preempt_hit, 0, "");
4b5f931b 116SYSCTL_QUAD(_lwkt, OID_AUTO, preempt_miss, CTLFLAG_RW, &preempt_miss, 0, "");
26a0694b 117SYSCTL_QUAD(_lwkt, OID_AUTO, preempt_weird, CTLFLAG_RW, &preempt_weird, 0, "");
38717797
HP
118#ifdef INVARIANTS
119SYSCTL_QUAD(_lwkt, OID_AUTO, token_contention_count, CTLFLAG_RW,
120 &token_contention_count, 0, "spinning due to token contention");
121SYSCTL_QUAD(_lwkt, OID_AUTO, mplock_contention_count, CTLFLAG_RW,
122 &mplock_contention_count, 0, "spinning due to MPLOCK contention");
123#endif
05220613
MD
124#endif
125
57aa743c
MD
126/*
127 * Kernel Trace
128 */
129#ifdef _KERNEL
130
131#if !defined(KTR_GIANT_CONTENTION)
132#define KTR_GIANT_CONTENTION KTR_ALL
133#endif
134
135KTR_INFO_MASTER(giant);
136KTR_INFO(KTR_GIANT_CONTENTION, giant, beg, 0, "thread=%p", sizeof(void *));
137KTR_INFO(KTR_GIANT_CONTENTION, giant, end, 1, "thread=%p", sizeof(void *));
138
139#define loggiant(name) KTR_LOG(giant_ ## name, curthread)
140
141#endif
142
4b5f931b
MD
143/*
144 * These helper procedures handle the runq, they can only be called from
145 * within a critical section.
75cdbe6c
MD
146 *
147 * WARNING! Prior to SMP being brought up it is possible to enqueue and
148 * dequeue threads belonging to other cpus, so be sure to use td->td_gd
149 * instead of 'mycpu' when referencing the globaldata structure. Once
150 * SMP live enqueuing and dequeueing only occurs on the current cpu.
4b5f931b 151 */
f1d1c3fa
MD
152static __inline
153void
154_lwkt_dequeue(thread_t td)
155{
156 if (td->td_flags & TDF_RUNQ) {
4b5f931b 157 int nq = td->td_pri & TDPRI_MASK;
75cdbe6c 158 struct globaldata *gd = td->td_gd;
4b5f931b 159
f1d1c3fa 160 td->td_flags &= ~TDF_RUNQ;
4b5f931b
MD
161 TAILQ_REMOVE(&gd->gd_tdrunq[nq], td, td_threadq);
162 /* runqmask is passively cleaned up by the switcher */
f1d1c3fa
MD
163 }
164}
165
166static __inline
167void
168_lwkt_enqueue(thread_t td)
169{
344ad853 170 if ((td->td_flags & (TDF_RUNQ|TDF_MIGRATING|TDF_TSLEEPQ|TDF_BLOCKQ)) == 0) {
4b5f931b 171 int nq = td->td_pri & TDPRI_MASK;
75cdbe6c 172 struct globaldata *gd = td->td_gd;
4b5f931b 173
f1d1c3fa 174 td->td_flags |= TDF_RUNQ;
4b5f931b
MD
175 TAILQ_INSERT_TAIL(&gd->gd_tdrunq[nq], td, td_threadq);
176 gd->gd_runqmask |= 1 << nq;
f1d1c3fa
MD
177 }
178}
8ad65e08 179
37af14fe
MD
180/*
181 * Schedule a thread to run. As the current thread we can always safely
182 * schedule ourselves, and a shortcut procedure is provided for that
183 * function.
184 *
185 * (non-blocking, self contained on a per cpu basis)
186 */
187void
188lwkt_schedule_self(thread_t td)
189{
190 crit_enter_quick(td);
191 KASSERT(td->td_wait == NULL, ("lwkt_schedule_self(): td_wait not NULL!"));
192 KASSERT(td != &td->td_gd->gd_idlethread, ("lwkt_schedule_self(): scheduling gd_idlethread is illegal!"));
344ad853 193 KKASSERT(td->td_proc == NULL || (td->td_proc->p_flag & P_ONRUNQ) == 0);
37af14fe 194 _lwkt_enqueue(td);
37af14fe
MD
195 crit_exit_quick(td);
196}
197
198/*
199 * Deschedule a thread.
200 *
201 * (non-blocking, self contained on a per cpu basis)
202 */
203void
204lwkt_deschedule_self(thread_t td)
205{
206 crit_enter_quick(td);
207 KASSERT(td->td_wait == NULL, ("lwkt_schedule_self(): td_wait not NULL!"));
208 _lwkt_dequeue(td);
209 crit_exit_quick(td);
210}
211
2d93b37a
MD
212#ifdef _KERNEL
213
8ad65e08
MD
214/*
215 * LWKTs operate on a per-cpu basis
216 *
73e4f7b9 217 * WARNING! Called from early boot, 'mycpu' may not work yet.
8ad65e08
MD
218 */
219void
220lwkt_gdinit(struct globaldata *gd)
221{
4b5f931b
MD
222 int i;
223
224 for (i = 0; i < sizeof(gd->gd_tdrunq)/sizeof(gd->gd_tdrunq[0]); ++i)
225 TAILQ_INIT(&gd->gd_tdrunq[i]);
226 gd->gd_runqmask = 0;
73e4f7b9 227 TAILQ_INIT(&gd->gd_tdallq);
8ad65e08
MD
228}
229
2d93b37a
MD
230#endif /* _KERNEL */
231
7d0bac62
MD
232/*
233 * Initialize a thread wait structure prior to first use.
234 *
235 * NOTE! called from low level boot code, we cannot do anything fancy!
236 */
237void
41a01a4d 238lwkt_wait_init(lwkt_wait_t w)
7d0bac62 239{
9d265729 240 spin_init(&w->wa_spinlock);
7d0bac62 241 TAILQ_INIT(&w->wa_waitq);
41a01a4d
MD
242 w->wa_gen = 0;
243 w->wa_count = 0;
7d0bac62
MD
244}
245
246/*
247 * Create a new thread. The thread must be associated with a process context
75cdbe6c
MD
248 * or LWKT start address before it can be scheduled. If the target cpu is
249 * -1 the thread will be created on the current cpu.
0cfcada1
MD
250 *
251 * If you intend to create a thread without a process context this function
252 * does everything except load the startup and switcher function.
7d0bac62
MD
253 */
254thread_t
d3d32139 255lwkt_alloc_thread(struct thread *td, int stksize, int cpu, int flags)
7d0bac62 256{
99df837e 257 void *stack;
37af14fe 258 globaldata_t gd = mycpu;
7d0bac62 259
ef0fdad1 260 if (td == NULL) {
37af14fe
MD
261 crit_enter_gd(gd);
262 if (gd->gd_tdfreecount > 0) {
263 --gd->gd_tdfreecount;
264 td = TAILQ_FIRST(&gd->gd_tdfreeq);
d9eea1a5 265 KASSERT(td != NULL && (td->td_flags & TDF_RUNNING) == 0,
ef0fdad1 266 ("lwkt_alloc_thread: unexpected NULL or corrupted td"));
37af14fe
MD
267 TAILQ_REMOVE(&gd->gd_tdfreeq, td, td_threadq);
268 crit_exit_gd(gd);
d3d32139 269 flags |= td->td_flags & (TDF_ALLOCATED_STACK|TDF_ALLOCATED_THREAD);
ef0fdad1 270 } else {
37af14fe 271 crit_exit_gd(gd);
05220613 272#ifdef _KERNEL
ef0fdad1 273 td = zalloc(thread_zone);
05220613
MD
274#else
275 td = malloc(sizeof(struct thread));
276#endif
ef0fdad1 277 td->td_kstack = NULL;
f470d0c8 278 td->td_kstack_size = 0;
ef0fdad1
MD
279 flags |= TDF_ALLOCATED_THREAD;
280 }
281 }
f470d0c8
MD
282 if ((stack = td->td_kstack) != NULL && td->td_kstack_size != stksize) {
283 if (flags & TDF_ALLOCATED_STACK) {
a9e5ae82 284#ifdef _KERNEL
f470d0c8 285 kmem_free(kernel_map, (vm_offset_t)stack, td->td_kstack_size);
a9e5ae82
JS
286#else
287 libcaps_free_stack(stack, td->td_kstack_size);
288#endif
f470d0c8
MD
289 stack = NULL;
290 }
291 }
292 if (stack == NULL) {
05220613 293#ifdef _KERNEL
f470d0c8 294 stack = (void *)kmem_alloc(kernel_map, stksize);
05220613 295#else
f470d0c8 296 stack = libcaps_alloc_stack(stksize);
05220613 297#endif
ef0fdad1 298 flags |= TDF_ALLOCATED_STACK;
99df837e 299 }
75cdbe6c 300 if (cpu < 0)
f470d0c8 301 lwkt_init_thread(td, stack, stksize, flags, mycpu);
75cdbe6c 302 else
f470d0c8 303 lwkt_init_thread(td, stack, stksize, flags, globaldata_find(cpu));
99df837e 304 return(td);
7d0bac62
MD
305}
306
709799ea
MD
307#ifdef _KERNEL
308
7d0bac62
MD
309/*
310 * Initialize a preexisting thread structure. This function is used by
311 * lwkt_alloc_thread() and also used to initialize the per-cpu idlethread.
312 *
f8c3996b
MD
313 * All threads start out in a critical section at a priority of
314 * TDPRI_KERN_DAEMON. Higher level code will modify the priority as
75cdbe6c
MD
315 * appropriate. This function may send an IPI message when the
316 * requested cpu is not the current cpu and consequently gd_tdallq may
317 * not be initialized synchronously from the point of view of the originating
318 * cpu.
319 *
320 * NOTE! we have to be careful in regards to creating threads for other cpus
321 * if SMP has not yet been activated.
7d0bac62 322 */
41a01a4d
MD
323#ifdef SMP
324
75cdbe6c
MD
325static void
326lwkt_init_thread_remote(void *arg)
327{
328 thread_t td = arg;
329
52eedfb5
MD
330 /*
331 * Protected by critical section held by IPI dispatch
332 */
75cdbe6c
MD
333 TAILQ_INSERT_TAIL(&td->td_gd->gd_tdallq, td, td_allq);
334}
335
41a01a4d
MD
336#endif
337
7d0bac62 338void
f470d0c8
MD
339lwkt_init_thread(thread_t td, void *stack, int stksize, int flags,
340 struct globaldata *gd)
7d0bac62 341{
37af14fe
MD
342 globaldata_t mygd = mycpu;
343
99df837e
MD
344 bzero(td, sizeof(struct thread));
345 td->td_kstack = stack;
f470d0c8 346 td->td_kstack_size = stksize;
d3d32139 347 td->td_flags = flags;
26a0694b 348 td->td_gd = gd;
f8c3996b 349 td->td_pri = TDPRI_KERN_DAEMON + TDPRI_CRIT;
d3d32139
MD
350#ifdef SMP
351 if ((flags & TDF_MPSAFE) == 0)
352 td->td_mpcount = 1;
353#endif
c95cd171 354 lwkt_initport(&td->td_msgport, td);
99df837e 355 pmap_init_thread(td);
0f7a3396 356#ifdef SMP
5d21b981
MD
357 /*
358 * Normally initializing a thread for a remote cpu requires sending an
359 * IPI. However, the idlethread is setup before the other cpus are
360 * activated so we have to treat it as a special case. XXX manipulation
361 * of gd_tdallq requires the BGL.
362 */
363 if (gd == mygd || td == &gd->gd_idlethread) {
37af14fe 364 crit_enter_gd(mygd);
75cdbe6c 365 TAILQ_INSERT_TAIL(&gd->gd_tdallq, td, td_allq);
37af14fe 366 crit_exit_gd(mygd);
75cdbe6c 367 } else {
2db3b277 368 lwkt_send_ipiq(gd, lwkt_init_thread_remote, td);
75cdbe6c 369 }
0f7a3396 370#else
37af14fe 371 crit_enter_gd(mygd);
0f7a3396 372 TAILQ_INSERT_TAIL(&gd->gd_tdallq, td, td_allq);
37af14fe 373 crit_exit_gd(mygd);
0f7a3396 374#endif
73e4f7b9
MD
375}
376
2d93b37a
MD
377#endif /* _KERNEL */
378
73e4f7b9
MD
379void
380lwkt_set_comm(thread_t td, const char *ctl, ...)
381{
e2565a42 382 __va_list va;
73e4f7b9 383
e2565a42 384 __va_start(va, ctl);
73e4f7b9 385 vsnprintf(td->td_comm, sizeof(td->td_comm), ctl, va);
e2565a42 386 __va_end(va);
7d0bac62
MD
387}
388
99df837e 389void
73e4f7b9 390lwkt_hold(thread_t td)
99df837e 391{
73e4f7b9
MD
392 ++td->td_refs;
393}
394
395void
396lwkt_rele(thread_t td)
397{
398 KKASSERT(td->td_refs > 0);
399 --td->td_refs;
400}
401
c95cd171
MD
402#ifdef _KERNEL
403
73e4f7b9
MD
404void
405lwkt_wait_free(thread_t td)
406{
407 while (td->td_refs)
377d4740 408 tsleep(td, 0, "tdreap", hz);
73e4f7b9
MD
409}
410
c95cd171
MD
411#endif
412
73e4f7b9
MD
413void
414lwkt_free_thread(thread_t td)
415{
416 struct globaldata *gd = mycpu;
417
d9eea1a5 418 KASSERT((td->td_flags & TDF_RUNNING) == 0,
99df837e
MD
419 ("lwkt_free_thread: did not exit! %p", td));
420
37af14fe 421 crit_enter_gd(gd);
73e4f7b9 422 if (gd->gd_tdfreecount < CACHE_NTHREADS &&
99df837e
MD
423 (td->td_flags & TDF_ALLOCATED_THREAD)
424 ) {
73e4f7b9
MD
425 ++gd->gd_tdfreecount;
426 TAILQ_INSERT_HEAD(&gd->gd_tdfreeq, td, td_threadq);
37af14fe 427 crit_exit_gd(gd);
99df837e 428 } else {
37af14fe 429 crit_exit_gd(gd);
99df837e 430 if (td->td_kstack && (td->td_flags & TDF_ALLOCATED_STACK)) {
05220613 431#ifdef _KERNEL
f470d0c8 432 kmem_free(kernel_map, (vm_offset_t)td->td_kstack, td->td_kstack_size);
05220613 433#else
f470d0c8 434 libcaps_free_stack(td->td_kstack, td->td_kstack_size);
05220613 435#endif
73e4f7b9 436 /* gd invalid */
99df837e 437 td->td_kstack = NULL;
f470d0c8 438 td->td_kstack_size = 0;
99df837e 439 }
05220613
MD
440 if (td->td_flags & TDF_ALLOCATED_THREAD) {
441#ifdef _KERNEL
99df837e 442 zfree(thread_zone, td);
05220613
MD
443#else
444 free(td);
445#endif
446 }
99df837e
MD
447 }
448}
449
450
8ad65e08
MD
451/*
452 * Switch to the next runnable lwkt. If no LWKTs are runnable then
f1d1c3fa
MD
453 * switch to the idlethread. Switching must occur within a critical
454 * section to avoid races with the scheduling queue.
455 *
456 * We always have full control over our cpu's run queue. Other cpus
457 * that wish to manipulate our queue must use the cpu_*msg() calls to
458 * talk to our cpu, so a critical section is all that is needed and
459 * the result is very, very fast thread switching.
460 *
96728c05
MD
461 * The LWKT scheduler uses a fixed priority model and round-robins at
462 * each priority level. User process scheduling is a totally
463 * different beast and LWKT priorities should not be confused with
464 * user process priorities.
f1d1c3fa 465 *
96728c05
MD
466 * The MP lock may be out of sync with the thread's td_mpcount. lwkt_switch()
467 * cleans it up. Note that the td_switch() function cannot do anything that
468 * requires the MP lock since the MP lock will have already been setup for
71ef2f5c
MD
469 * the target thread (not the current thread). It's nice to have a scheduler
470 * that does not need the MP lock to work because it allows us to do some
471 * really cool high-performance MP lock optimizations.
69d78e99
MD
472 *
473 * PREEMPTION NOTE: Preemption occurs via lwkt_preempt(). lwkt_switch()
474 * is not called by the current thread in the preemption case, only when
475 * the preempting thread blocks (in order to return to the original thread).
8ad65e08
MD
476 */
477void
478lwkt_switch(void)
479{
37af14fe
MD
480 globaldata_t gd = mycpu;
481 thread_t td = gd->gd_curthread;
8ad65e08 482 thread_t ntd;
8a8d5d85
MD
483#ifdef SMP
484 int mpheld;
485#endif
8ad65e08 486
46a3f46d 487 /*
27e88a6e
MD
488 * Switching from within a 'fast' (non thread switched) interrupt or IPI
489 * is illegal. However, we may have to do it anyway if we hit a fatal
490 * kernel trap or we have paniced.
491 *
492 * If this case occurs save and restore the interrupt nesting level.
46a3f46d 493 */
27e88a6e
MD
494 if (gd->gd_intr_nesting_level) {
495 int savegdnest;
496 int savegdtrap;
497
498 if (gd->gd_trap_nesting_level == 0 && panicstr == NULL) {
499 panic("lwkt_switch: cannot switch from within "
500 "a fast interrupt, yet, td %p\n", td);
501 } else {
502 savegdnest = gd->gd_intr_nesting_level;
503 savegdtrap = gd->gd_trap_nesting_level;
504 gd->gd_intr_nesting_level = 0;
505 gd->gd_trap_nesting_level = 0;
a7422615
MD
506 if ((td->td_flags & TDF_PANICWARN) == 0) {
507 td->td_flags |= TDF_PANICWARN;
508 printf("Warning: thread switch from interrupt or IPI, "
509 "thread %p (%s)\n", td, td->td_comm);
510#ifdef DDB
511 db_print_backtrace();
512#endif
513 }
27e88a6e
MD
514 lwkt_switch();
515 gd->gd_intr_nesting_level = savegdnest;
516 gd->gd_trap_nesting_level = savegdtrap;
517 return;
518 }
96728c05 519 }
ef0fdad1 520
cb973d15
MD
521 /*
522 * Passive release (used to transition from user to kernel mode
523 * when we block or switch rather then when we enter the kernel).
524 * This function is NOT called if we are switching into a preemption
525 * or returning from a preemption. Typically this causes us to lose
0a3f9b47
MD
526 * our current process designation (if we have one) and become a true
527 * LWKT thread, and may also hand the current process designation to
528 * another process and schedule thread.
cb973d15
MD
529 */
530 if (td->td_release)
531 td->td_release(td);
532
37af14fe 533 crit_enter_gd(gd);
dd55d707 534#ifdef SMP
9d265729
MD
535 if (td->td_toks)
536 lwkt_relalltokens(td);
dd55d707 537#endif
9d265729
MD
538
539 /*
b02926de
MD
540 * We had better not be holding any spin locks, but don't get into an
541 * endless panic loop.
9d265729 542 */
bbb31c5d
MD
543 KASSERT(gd->gd_spinlock_rd == NULL || panicstr != NULL,
544 ("lwkt_switch: still holding a shared spinlock %p!",
545 gd->gd_spinlock_rd));
d666840a
MD
546 KASSERT(gd->gd_spinlocks_wr == 0 || panicstr != NULL,
547 ("lwkt_switch: still holding %d exclusive spinlocks!",
548 gd->gd_spinlocks_wr));
9d265729 549
8a8d5d85
MD
550
551#ifdef SMP
552 /*
553 * td_mpcount cannot be used to determine if we currently hold the
554 * MP lock because get_mplock() will increment it prior to attempting
71ef2f5c
MD
555 * to get the lock, and switch out if it can't. Our ownership of
556 * the actual lock will remain stable while we are in a critical section
557 * (but, of course, another cpu may own or release the lock so the
558 * actual value of mp_lock is not stable).
8a8d5d85
MD
559 */
560 mpheld = MP_LOCK_HELD();
0f7a3396
MD
561#ifdef INVARIANTS
562 if (td->td_cscount) {
563 printf("Diagnostic: attempt to switch while mastering cpusync: %p\n",
564 td);
565 if (panic_on_cscount)
566 panic("switching while mastering cpusync");
567 }
568#endif
8a8d5d85 569#endif
99df837e
MD
570 if ((ntd = td->td_preempted) != NULL) {
571 /*
572 * We had preempted another thread on this cpu, resume the preempted
26a0694b
MD
573 * thread. This occurs transparently, whether the preempted thread
574 * was scheduled or not (it may have been preempted after descheduling
8a8d5d85
MD
575 * itself).
576 *
577 * We have to setup the MP lock for the original thread after backing
578 * out the adjustment that was made to curthread when the original
579 * was preempted.
99df837e 580 */
26a0694b 581 KKASSERT(ntd->td_flags & TDF_PREEMPT_LOCK);
8a8d5d85 582#ifdef SMP
96728c05 583 if (ntd->td_mpcount && mpheld == 0) {
fc92d4aa 584 panic("MPLOCK NOT HELD ON RETURN: %p %p %d %d",
96728c05
MD
585 td, ntd, td->td_mpcount, ntd->td_mpcount);
586 }
8a8d5d85
MD
587 if (ntd->td_mpcount) {
588 td->td_mpcount -= ntd->td_mpcount;
589 KKASSERT(td->td_mpcount >= 0);
590 }
591#endif
26a0694b 592 ntd->td_flags |= TDF_PREEMPT_DONE;
8ec60c3f
MD
593
594 /*
595 * XXX. The interrupt may have woken a thread up, we need to properly
596 * set the reschedule flag if the originally interrupted thread is at
597 * a lower priority.
598 */
599 if (gd->gd_runqmask > (2 << (ntd->td_pri & TDPRI_MASK)) - 1)
600 need_lwkt_resched();
8a8d5d85 601 /* YYY release mp lock on switchback if original doesn't need it */
8ad65e08 602 } else {
4b5f931b
MD
603 /*
604 * Priority queue / round-robin at each priority. Note that user
605 * processes run at a fixed, low priority and the user process
606 * scheduler deals with interactions between user processes
607 * by scheduling and descheduling them from the LWKT queue as
608 * necessary.
8a8d5d85
MD
609 *
610 * We have to adjust the MP lock for the target thread. If we
611 * need the MP lock and cannot obtain it we try to locate a
41a01a4d
MD
612 * thread that does not need the MP lock. If we cannot, we spin
613 * instead of HLT.
614 *
615 * A similar issue exists for the tokens held by the target thread.
616 * If we cannot obtain ownership of the tokens we cannot immediately
617 * schedule the thread.
618 */
619
8ec60c3f
MD
620 /*
621 * If an LWKT reschedule was requested, well that is what we are
622 * doing now so clear it.
623 */
624 clear_lwkt_resched();
4b5f931b
MD
625again:
626 if (gd->gd_runqmask) {
627 int nq = bsrl(gd->gd_runqmask);
628 if ((ntd = TAILQ_FIRST(&gd->gd_tdrunq[nq])) == NULL) {
629 gd->gd_runqmask &= ~(1 << nq);
630 goto again;
631 }
8a8d5d85 632#ifdef SMP
41a01a4d 633 /*
df6b8ba0
MD
634 * THREAD SELECTION FOR AN SMP MACHINE BUILD
635 *
41a01a4d
MD
636 * If the target needs the MP lock and we couldn't get it,
637 * or if the target is holding tokens and we could not
638 * gain ownership of the tokens, continue looking for a
639 * thread to schedule and spin instead of HLT if we can't.
a453459d
MD
640 *
641 * NOTE: the mpheld variable invalid after this conditional, it
642 * can change due to both cpu_try_mplock() returning success
9d265729 643 * AND interactions in lwkt_getalltokens() due to the fact that
a453459d
MD
644 * we are trying to check the mpcount of a thread other then
645 * the current thread. Because of this, if the current thread
646 * is not holding td_mpcount, an IPI indirectly run via
9d265729 647 * lwkt_getalltokens() can obtain and release the MP lock and
a453459d 648 * cause the core MP lock to be released.
41a01a4d
MD
649 */
650 if ((ntd->td_mpcount && mpheld == 0 && !cpu_try_mplock()) ||
9d265729 651 (ntd->td_toks && lwkt_getalltokens(ntd) == 0)
41a01a4d 652 ) {
8a8d5d85 653 u_int32_t rqmask = gd->gd_runqmask;
a453459d
MD
654
655 mpheld = MP_LOCK_HELD();
656 ntd = NULL;
8a8d5d85
MD
657 while (rqmask) {
658 TAILQ_FOREACH(ntd, &gd->gd_tdrunq[nq], td_threadq) {
38717797 659 if (ntd->td_mpcount && !mpheld && !cpu_try_mplock()) {
a453459d 660 /* spinning due to MP lock being held */
38717797 661#ifdef INVARIANTS
a453459d 662 ++mplock_contention_count;
38717797 663#endif
a453459d 664 /* mplock still not held, 'mpheld' still valid */
41a01a4d 665 continue;
38717797 666 }
a453459d
MD
667
668 /*
9d265729 669 * mpheld state invalid after getalltokens call returns
a453459d
MD
670 * failure, but the variable is only needed for
671 * the loop.
672 */
9d265729 673 if (ntd->td_toks && !lwkt_getalltokens(ntd)) {
a453459d 674 /* spinning due to token contention */
38717797 675#ifdef INVARIANTS
a453459d 676 ++token_contention_count;
38717797 677#endif
a453459d 678 mpheld = MP_LOCK_HELD();
41a01a4d 679 continue;
38717797 680 }
41a01a4d 681 break;
8a8d5d85
MD
682 }
683 if (ntd)
684 break;
685 rqmask &= ~(1 << nq);
686 nq = bsrl(rqmask);
687 }
688 if (ntd == NULL) {
a2a5ad0d
MD
689 ntd = &gd->gd_idlethread;
690 ntd->td_flags |= TDF_IDLE_NOHLT;
df6b8ba0 691 goto using_idle_thread;
8a8d5d85 692 } else {
344ad853 693 ++gd->gd_cnt.v_swtch;
8a8d5d85
MD
694 TAILQ_REMOVE(&gd->gd_tdrunq[nq], ntd, td_threadq);
695 TAILQ_INSERT_TAIL(&gd->gd_tdrunq[nq], ntd, td_threadq);
696 }
697 } else {
344ad853 698 ++gd->gd_cnt.v_swtch;
8a8d5d85
MD
699 TAILQ_REMOVE(&gd->gd_tdrunq[nq], ntd, td_threadq);
700 TAILQ_INSERT_TAIL(&gd->gd_tdrunq[nq], ntd, td_threadq);
701 }
702#else
df6b8ba0
MD
703 /*
704 * THREAD SELECTION FOR A UP MACHINE BUILD. We don't have to
705 * worry about tokens or the BGL.
706 */
344ad853 707 ++gd->gd_cnt.v_swtch;
4b5f931b
MD
708 TAILQ_REMOVE(&gd->gd_tdrunq[nq], ntd, td_threadq);
709 TAILQ_INSERT_TAIL(&gd->gd_tdrunq[nq], ntd, td_threadq);
8a8d5d85 710#endif
4b5f931b 711 } else {
3c23a41a 712 /*
60f945af
MD
713 * We have nothing to run but only let the idle loop halt
714 * the cpu if there are no pending interrupts.
3c23a41a 715 */
a2a5ad0d 716 ntd = &gd->gd_idlethread;
60f945af 717 if (gd->gd_reqflags & RQF_IDLECHECK_MASK)
3c23a41a 718 ntd->td_flags |= TDF_IDLE_NOHLT;
a453459d 719#ifdef SMP
df6b8ba0
MD
720using_idle_thread:
721 /*
722 * The idle thread should not be holding the MP lock unless we
723 * are trapping in the kernel or in a panic. Since we select the
724 * idle thread unconditionally when no other thread is available,
725 * if the MP lock is desired during a panic or kernel trap, we
726 * have to loop in the scheduler until we get it.
727 */
728 if (ntd->td_mpcount) {
729 mpheld = MP_LOCK_HELD();
730 if (gd->gd_trap_nesting_level == 0 && panicstr == NULL)
731 panic("Idle thread %p was holding the BGL!", ntd);
732 else if (mpheld == 0)
733 goto again;
734 }
a453459d 735#endif
4b5f931b 736 }
f1d1c3fa 737 }
26a0694b
MD
738 KASSERT(ntd->td_pri >= TDPRI_CRIT,
739 ("priority problem in lwkt_switch %d %d", td->td_pri, ntd->td_pri));
8a8d5d85
MD
740
741 /*
742 * Do the actual switch. If the new target does not need the MP lock
743 * and we are holding it, release the MP lock. If the new target requires
744 * the MP lock we have already acquired it for the target.
745 */
746#ifdef SMP
747 if (ntd->td_mpcount == 0 ) {
748 if (MP_LOCK_HELD())
749 cpu_rel_mplock();
750 } else {
a453459d 751 ASSERT_MP_LOCK_HELD(ntd);
8a8d5d85
MD
752 }
753#endif
94f6d86e
MD
754 if (td != ntd) {
755 ++switch_count;
f1d1c3fa 756 td->td_switch(ntd);
94f6d86e 757 }
37af14fe
MD
758 /* NOTE: current cpu may have changed after switch */
759 crit_exit_quick(td);
8ad65e08
MD
760}
761
b68b7282 762/*
96728c05
MD
763 * Request that the target thread preempt the current thread. Preemption
764 * only works under a specific set of conditions:
b68b7282 765 *
96728c05
MD
766 * - We are not preempting ourselves
767 * - The target thread is owned by the current cpu
768 * - We are not currently being preempted
769 * - The target is not currently being preempted
770 * - We are able to satisfy the target's MP lock requirements (if any).
771 *
772 * THE CALLER OF LWKT_PREEMPT() MUST BE IN A CRITICAL SECTION. Typically
773 * this is called via lwkt_schedule() through the td_preemptable callback.
774 * critpri is the managed critical priority that we should ignore in order
775 * to determine whether preemption is possible (aka usually just the crit
776 * priority of lwkt_schedule() itself).
b68b7282 777 *
26a0694b
MD
778 * XXX at the moment we run the target thread in a critical section during
779 * the preemption in order to prevent the target from taking interrupts
780 * that *WE* can't. Preemption is strictly limited to interrupt threads
781 * and interrupt-like threads, outside of a critical section, and the
782 * preempted source thread will be resumed the instant the target blocks
783 * whether or not the source is scheduled (i.e. preemption is supposed to
784 * be as transparent as possible).
4b5f931b 785 *
8a8d5d85
MD
786 * The target thread inherits our MP count (added to its own) for the
787 * duration of the preemption in order to preserve the atomicy of the
96728c05
MD
788 * MP lock during the preemption. Therefore, any preempting targets must be
789 * careful in regards to MP assertions. Note that the MP count may be
71ef2f5c
MD
790 * out of sync with the physical mp_lock, but we do not have to preserve
791 * the original ownership of the lock if it was out of synch (that is, we
792 * can leave it synchronized on return).
b68b7282
MD
793 */
794void
96728c05 795lwkt_preempt(thread_t ntd, int critpri)
b68b7282 796{
46a3f46d 797 struct globaldata *gd = mycpu;
0a3f9b47 798 thread_t td;
8a8d5d85
MD
799#ifdef SMP
800 int mpheld;
57c254db 801 int savecnt;
8a8d5d85 802#endif
b68b7282 803
26a0694b 804 /*
96728c05
MD
805 * The caller has put us in a critical section. We can only preempt
806 * if the caller of the caller was not in a critical section (basically
d666840a
MD
807 * a local interrupt), as determined by the 'critpri' parameter. We
808 * also acn't preempt if the caller is holding any spinlocks (even if
809 * he isn't in a critical section). This also handles the tokens test.
96728c05
MD
810 *
811 * YYY The target thread must be in a critical section (else it must
812 * inherit our critical section? I dunno yet).
41a01a4d 813 *
0a3f9b47 814 * Set need_lwkt_resched() unconditionally for now YYY.
26a0694b
MD
815 */
816 KASSERT(ntd->td_pri >= TDPRI_CRIT, ("BADCRIT0 %d", ntd->td_pri));
26a0694b 817
0a3f9b47 818 td = gd->gd_curthread;
0a3f9b47 819 if ((ntd->td_pri & TDPRI_MASK) <= (td->td_pri & TDPRI_MASK)) {
57c254db
MD
820 ++preempt_miss;
821 return;
822 }
96728c05
MD
823 if ((td->td_pri & ~TDPRI_MASK) > critpri) {
824 ++preempt_miss;
8ec60c3f 825 need_lwkt_resched();
96728c05
MD
826 return;
827 }
828#ifdef SMP
46a3f46d 829 if (ntd->td_gd != gd) {
96728c05 830 ++preempt_miss;
8ec60c3f 831 need_lwkt_resched();
96728c05
MD
832 return;
833 }
834#endif
41a01a4d
MD
835 /*
836 * Take the easy way out and do not preempt if the target is holding
d666840a 837 * any spinlocks. We could test whether the thread(s) being
41a01a4d
MD
838 * preempted interlock against the target thread's tokens and whether
839 * we can get all the target thread's tokens, but this situation
840 * should not occur very often so its easier to simply not preempt.
d666840a
MD
841 * Also, plain spinlocks are impossible to figure out at this point so
842 * just don't preempt.
41a01a4d 843 */
bbb31c5d 844 if (gd->gd_spinlock_rd || gd->gd_spinlocks_wr) {
41a01a4d 845 ++preempt_miss;
8ec60c3f 846 need_lwkt_resched();
41a01a4d
MD
847 return;
848 }
26a0694b
MD
849 if (td == ntd || ((td->td_flags | ntd->td_flags) & TDF_PREEMPT_LOCK)) {
850 ++preempt_weird;
8ec60c3f 851 need_lwkt_resched();
26a0694b
MD
852 return;
853 }
854 if (ntd->td_preempted) {
4b5f931b 855 ++preempt_hit;
8ec60c3f 856 need_lwkt_resched();
26a0694b 857 return;
b68b7282 858 }
8a8d5d85 859#ifdef SMP
a2a5ad0d
MD
860 /*
861 * note: an interrupt might have occured just as we were transitioning
71ef2f5c
MD
862 * to or from the MP lock. In this case td_mpcount will be pre-disposed
863 * (non-zero) but not actually synchronized with the actual state of the
864 * lock. We can use it to imply an MP lock requirement for the
865 * preemption but we cannot use it to test whether we hold the MP lock
866 * or not.
a2a5ad0d 867 */
96728c05 868 savecnt = td->td_mpcount;
71ef2f5c 869 mpheld = MP_LOCK_HELD();
8a8d5d85
MD
870 ntd->td_mpcount += td->td_mpcount;
871 if (mpheld == 0 && ntd->td_mpcount && !cpu_try_mplock()) {
872 ntd->td_mpcount -= td->td_mpcount;
873 ++preempt_miss;
8ec60c3f 874 need_lwkt_resched();
8a8d5d85
MD
875 return;
876 }
877#endif
26a0694b 878
8ec60c3f
MD
879 /*
880 * Since we are able to preempt the current thread, there is no need to
881 * call need_lwkt_resched().
882 */
26a0694b
MD
883 ++preempt_hit;
884 ntd->td_preempted = td;
885 td->td_flags |= TDF_PREEMPT_LOCK;
886 td->td_switch(ntd);
887 KKASSERT(ntd->td_preempted && (td->td_flags & TDF_PREEMPT_DONE));
96728c05
MD
888#ifdef SMP
889 KKASSERT(savecnt == td->td_mpcount);
71ef2f5c
MD
890 mpheld = MP_LOCK_HELD();
891 if (mpheld && td->td_mpcount == 0)
96728c05 892 cpu_rel_mplock();
71ef2f5c 893 else if (mpheld == 0 && td->td_mpcount)
96728c05
MD
894 panic("lwkt_preempt(): MP lock was not held through");
895#endif
26a0694b
MD
896 ntd->td_preempted = NULL;
897 td->td_flags &= ~(TDF_PREEMPT_LOCK|TDF_PREEMPT_DONE);
b68b7282
MD
898}
899
f1d1c3fa
MD
900/*
901 * Yield our thread while higher priority threads are pending. This is
902 * typically called when we leave a critical section but it can be safely
903 * called while we are in a critical section.
904 *
905 * This function will not generally yield to equal priority threads but it
906 * can occur as a side effect. Note that lwkt_switch() is called from
46a3f46d 907 * inside the critical section to prevent its own crit_exit() from reentering
f1d1c3fa
MD
908 * lwkt_yield_quick().
909 *
235957ed 910 * gd_reqflags indicates that *something* changed, e.g. an interrupt or softint
ef0fdad1
MD
911 * came along but was blocked and made pending.
912 *
f1d1c3fa
MD
913 * (self contained on a per cpu basis)
914 */
915void
916lwkt_yield_quick(void)
917{
7966cb69
MD
918 globaldata_t gd = mycpu;
919 thread_t td = gd->gd_curthread;
ef0fdad1 920
a2a5ad0d 921 /*
235957ed 922 * gd_reqflags is cleared in splz if the cpl is 0. If we were to clear
a2a5ad0d
MD
923 * it with a non-zero cpl then we might not wind up calling splz after
924 * a task switch when the critical section is exited even though the
46a3f46d 925 * new task could accept the interrupt.
a2a5ad0d
MD
926 *
927 * XXX from crit_exit() only called after last crit section is released.
928 * If called directly will run splz() even if in a critical section.
46a3f46d
MD
929 *
930 * td_nest_count prevent deep nesting via splz() or doreti(). Note that
931 * except for this special case, we MUST call splz() here to handle any
932 * pending ints, particularly after we switch, or we might accidently
933 * halt the cpu with interrupts pending.
a2a5ad0d 934 */
46a3f46d 935 if (gd->gd_reqflags && td->td_nest_count < 2)
f1d1c3fa 936 splz();
f1d1c3fa
MD
937
938 /*
939 * YYY enabling will cause wakeup() to task-switch, which really
940 * confused the old 4.x code. This is a good way to simulate
7d0bac62
MD
941 * preemption and MP without actually doing preemption or MP, because a
942 * lot of code assumes that wakeup() does not block.
f1d1c3fa 943 */
46a3f46d
MD
944 if (untimely_switch && td->td_nest_count == 0 &&
945 gd->gd_intr_nesting_level == 0
946 ) {
37af14fe 947 crit_enter_quick(td);
f1d1c3fa
MD
948 /*
949 * YYY temporary hacks until we disassociate the userland scheduler
950 * from the LWKT scheduler.
951 */
952 if (td->td_flags & TDF_RUNQ) {
953 lwkt_switch(); /* will not reenter yield function */
954 } else {
37af14fe 955 lwkt_schedule_self(td); /* make sure we are scheduled */
f1d1c3fa 956 lwkt_switch(); /* will not reenter yield function */
37af14fe 957 lwkt_deschedule_self(td); /* make sure we are descheduled */
f1d1c3fa 958 }
7966cb69 959 crit_exit_noyield(td);
f1d1c3fa 960 }
f1d1c3fa
MD
961}
962
8ad65e08 963/*
f1d1c3fa 964 * This implements a normal yield which, unlike _quick, will yield to equal
235957ed 965 * priority threads as well. Note that gd_reqflags tests will be handled by
f1d1c3fa
MD
966 * the crit_exit() call in lwkt_switch().
967 *
968 * (self contained on a per cpu basis)
8ad65e08
MD
969 */
970void
f1d1c3fa 971lwkt_yield(void)
8ad65e08 972{
37af14fe 973 lwkt_schedule_self(curthread);
f1d1c3fa
MD
974 lwkt_switch();
975}
976
8ad65e08 977/*
f1d1c3fa
MD
978 * Generic schedule. Possibly schedule threads belonging to other cpus and
979 * deal with threads that might be blocked on a wait queue.
980 *
0a3f9b47
MD
981 * We have a little helper inline function which does additional work after
982 * the thread has been enqueued, including dealing with preemption and
983 * setting need_lwkt_resched() (which prevents the kernel from returning
984 * to userland until it has processed higher priority threads).
6330a558
MD
985 *
986 * It is possible for this routine to be called after a failed _enqueue
987 * (due to the target thread migrating, sleeping, or otherwise blocked).
988 * We have to check that the thread is actually on the run queue!
8ad65e08 989 */
0a3f9b47
MD
990static __inline
991void
8ec60c3f 992_lwkt_schedule_post(globaldata_t gd, thread_t ntd, int cpri)
0a3f9b47 993{
6330a558
MD
994 if (ntd->td_flags & TDF_RUNQ) {
995 if (ntd->td_preemptable) {
996 ntd->td_preemptable(ntd, cpri); /* YYY +token */
997 } else if ((ntd->td_flags & TDF_NORESCHED) == 0 &&
998 (ntd->td_pri & TDPRI_MASK) > (gd->gd_curthread->td_pri & TDPRI_MASK)
999 ) {
1000 need_lwkt_resched();
1001 }
0a3f9b47
MD
1002 }
1003}
1004
8ad65e08
MD
1005void
1006lwkt_schedule(thread_t td)
1007{
37af14fe
MD
1008 globaldata_t mygd = mycpu;
1009
41a01a4d 1010 KASSERT(td != &td->td_gd->gd_idlethread, ("lwkt_schedule(): scheduling gd_idlethread is illegal!"));
37af14fe 1011 crit_enter_gd(mygd);
344ad853 1012 KKASSERT(td->td_proc == NULL || (td->td_proc->p_flag & P_ONRUNQ) == 0);
37af14fe 1013 if (td == mygd->gd_curthread) {
f1d1c3fa
MD
1014 _lwkt_enqueue(td);
1015 } else {
1016 lwkt_wait_t w;
1017
1018 /*
1019 * If the thread is on a wait list we have to send our scheduling
1020 * request to the owner of the wait structure. Otherwise we send
1021 * the scheduling request to the cpu owning the thread. Races
1022 * are ok, the target will forward the message as necessary (the
1023 * message may chase the thread around before it finally gets
1024 * acted upon).
1025 *
1026 * (remember, wait structures use stable storage)
0a3f9b47 1027 *
0c453950
MD
1028 * NOTE: we have to account for the number of critical sections
1029 * under our control when calling _lwkt_schedule_post() so it
1030 * can figure out whether preemption is allowed.
1031 *
1032 * NOTE: The wait structure algorithms are a mess and need to be
1033 * rewritten.
1034 *
1035 * NOTE: We cannot safely acquire or release a token, even
1036 * non-blocking, because this routine may be called in the context
1037 * of a thread already holding the token and thus not provide any
1038 * interlock protection. We cannot safely manipulate the td_toks
1039 * list for the same reason. Instead we depend on our critical
1040 * section if the token is owned by our cpu.
f1d1c3fa
MD
1041 */
1042 if ((w = td->td_wait) != NULL) {
d666840a 1043 spin_lock_wr(&w->wa_spinlock);
9d265729
MD
1044 TAILQ_REMOVE(&w->wa_waitq, td, td_threadq);
1045 --w->wa_count;
1046 td->td_wait = NULL;
d666840a 1047 spin_unlock_wr(&w->wa_spinlock);
0f7a3396 1048#ifdef SMP
9d265729 1049 if (td->td_gd == mygd) {
0f7a3396 1050 _lwkt_enqueue(td);
8ec60c3f 1051 _lwkt_schedule_post(mygd, td, TDPRI_CRIT);
f1d1c3fa 1052 } else {
9d265729
MD
1053 lwkt_send_ipiq(td->td_gd, (ipifunc1_t)lwkt_schedule, td);
1054 }
b8a98473 1055#else
9d265729
MD
1056 _lwkt_enqueue(td);
1057 _lwkt_schedule_post(mygd, td, TDPRI_CRIT);
b8a98473 1058#endif
f1d1c3fa
MD
1059 } else {
1060 /*
1061 * If the wait structure is NULL and we own the thread, there
1062 * is no race (since we are in a critical section). If we
1063 * do not own the thread there might be a race but the
1064 * target cpu will deal with it.
1065 */
0f7a3396 1066#ifdef SMP
37af14fe 1067 if (td->td_gd == mygd) {
f1d1c3fa 1068 _lwkt_enqueue(td);
8ec60c3f 1069 _lwkt_schedule_post(mygd, td, TDPRI_CRIT);
f1d1c3fa 1070 } else {
b8a98473 1071 lwkt_send_ipiq(td->td_gd, (ipifunc1_t)lwkt_schedule, td);
f1d1c3fa 1072 }
0f7a3396
MD
1073#else
1074 _lwkt_enqueue(td);
8ec60c3f 1075 _lwkt_schedule_post(mygd, td, TDPRI_CRIT);
0f7a3396 1076#endif
f1d1c3fa 1077 }
8ad65e08 1078 }
37af14fe 1079 crit_exit_gd(mygd);
8ad65e08
MD
1080}
1081
52eedfb5
MD
1082#ifdef SMP
1083
d9eea1a5 1084/*
52eedfb5
MD
1085 * Thread migration using a 'Pull' method. The thread may or may not be
1086 * the current thread. It MUST be descheduled and in a stable state.
1087 * lwkt_giveaway() must be called on the cpu owning the thread.
1088 *
1089 * At any point after lwkt_giveaway() is called, the target cpu may
1090 * 'pull' the thread by calling lwkt_acquire().
1091 *
1092 * MPSAFE - must be called under very specific conditions.
d9eea1a5 1093 */
52eedfb5
MD
1094void
1095lwkt_giveaway(thread_t td)
1096{
1097 globaldata_t gd = mycpu;
1098
1099 crit_enter_gd(gd);
1100 KKASSERT(td->td_gd == gd);
1101 TAILQ_REMOVE(&gd->gd_tdallq, td, td_allq);
1102 td->td_flags |= TDF_MIGRATING;
1103 crit_exit_gd(gd);
1104}
1105
a2a5ad0d
MD
1106void
1107lwkt_acquire(thread_t td)
1108{
37af14fe
MD
1109 globaldata_t gd;
1110 globaldata_t mygd;
a2a5ad0d 1111
52eedfb5 1112 KKASSERT(td->td_flags & TDF_MIGRATING);
a2a5ad0d 1113 gd = td->td_gd;
37af14fe 1114 mygd = mycpu;
52eedfb5 1115 if (gd != mycpu) {
35238fa5 1116 cpu_lfence();
52eedfb5 1117 KKASSERT((td->td_flags & TDF_RUNQ) == 0);
37af14fe 1118 crit_enter_gd(mygd);
52eedfb5
MD
1119 while (td->td_flags & (TDF_RUNNING|TDF_PREEMPT_LOCK))
1120 cpu_lfence();
37af14fe 1121 td->td_gd = mygd;
52eedfb5
MD
1122 TAILQ_INSERT_TAIL(&mygd->gd_tdallq, td, td_allq);
1123 td->td_flags &= ~TDF_MIGRATING;
1124 crit_exit_gd(mygd);
1125 } else {
1126 crit_enter_gd(mygd);
1127 TAILQ_INSERT_TAIL(&mygd->gd_tdallq, td, td_allq);
1128 td->td_flags &= ~TDF_MIGRATING;
37af14fe 1129 crit_exit_gd(mygd);
a2a5ad0d
MD
1130 }
1131}
1132
52eedfb5
MD
1133#endif
1134
f1d1c3fa
MD
1135/*
1136 * Generic deschedule. Descheduling threads other then your own should be
1137 * done only in carefully controlled circumstances. Descheduling is
1138 * asynchronous.
1139 *
1140 * This function may block if the cpu has run out of messages.
8ad65e08
MD
1141 */
1142void
1143lwkt_deschedule(thread_t td)
1144{
f1d1c3fa 1145 crit_enter();
b8a98473 1146#ifdef SMP
f1d1c3fa
MD
1147 if (td == curthread) {
1148 _lwkt_dequeue(td);
1149 } else {
a72187e9 1150 if (td->td_gd == mycpu) {
f1d1c3fa
MD
1151 _lwkt_dequeue(td);
1152 } else {
b8a98473 1153 lwkt_send_ipiq(td->td_gd, (ipifunc1_t)lwkt_deschedule, td);
f1d1c3fa
MD
1154 }
1155 }
b8a98473
MD
1156#else
1157 _lwkt_dequeue(td);
1158#endif
f1d1c3fa
MD
1159 crit_exit();
1160}
1161
4b5f931b
MD
1162/*
1163 * Set the target thread's priority. This routine does not automatically
1164 * switch to a higher priority thread, LWKT threads are not designed for
1165 * continuous priority changes. Yield if you want to switch.
1166 *
1167 * We have to retain the critical section count which uses the high bits
26a0694b
MD
1168 * of the td_pri field. The specified priority may also indicate zero or
1169 * more critical sections by adding TDPRI_CRIT*N.
18bbe476
MD
1170 *
1171 * Note that we requeue the thread whether it winds up on a different runq
1172 * or not. uio_yield() depends on this and the routine is not normally
1173 * called with the same priority otherwise.
4b5f931b
MD
1174 */
1175void
1176lwkt_setpri(thread_t td, int pri)
1177{
26a0694b 1178 KKASSERT(pri >= 0);
a72187e9 1179 KKASSERT(td->td_gd == mycpu);
26a0694b
MD
1180 crit_enter();
1181 if (td->td_flags & TDF_RUNQ) {
1182 _lwkt_dequeue(td);
1183 td->td_pri = (td->td_pri & ~TDPRI_MASK) + pri;
1184 _lwkt_enqueue(td);
1185 } else {
1186 td->td_pri = (td->td_pri & ~TDPRI_MASK) + pri;
1187 }
1188 crit_exit();
1189}
1190
1191void
1192lwkt_setpri_self(int pri)
1193{
1194 thread_t td = curthread;
1195
4b5f931b
MD
1196 KKASSERT(pri >= 0 && pri <= TDPRI_MAX);
1197 crit_enter();
1198 if (td->td_flags & TDF_RUNQ) {
1199 _lwkt_dequeue(td);
1200 td->td_pri = (td->td_pri & ~TDPRI_MASK) + pri;
1201 _lwkt_enqueue(td);
1202 } else {
1203 td->td_pri = (td->td_pri & ~TDPRI_MASK) + pri;
1204 }
1205 crit_exit();
1206}
1207
8ec60c3f
MD
1208/*
1209 * Determine if there is a runnable thread at a higher priority then
1210 * the current thread. lwkt_setpri() does not check this automatically.
1211 * Return 1 if there is, 0 if there isn't.
1212 *
1213 * Example: if bit 31 of runqmask is set and the current thread is priority
1214 * 30, then we wind up checking the mask: 0x80000000 against 0x7fffffff.
1215 *
1216 * If nq reaches 31 the shift operation will overflow to 0 and we will wind
1217 * up comparing against 0xffffffff, a comparison that will always be false.
1218 */
1219int
1220lwkt_checkpri_self(void)
1221{
1222 globaldata_t gd = mycpu;
1223 thread_t td = gd->gd_curthread;
1224 int nq = td->td_pri & TDPRI_MASK;
1225
1226 while (gd->gd_runqmask > (__uint32_t)(2 << nq) - 1) {
1227 if (TAILQ_FIRST(&gd->gd_tdrunq[nq + 1]))
1228 return(1);
1229 ++nq;
1230 }
1231 return(0);
1232}
1233
5d21b981 1234/*
52eedfb5
MD
1235 * Migrate the current thread to the specified cpu.
1236 *
1237 * This is accomplished by descheduling ourselves from the current cpu,
1238 * moving our thread to the tdallq of the target cpu, IPI messaging the
1239 * target cpu, and switching out. TDF_MIGRATING prevents scheduling
1240 * races while the thread is being migrated.
5d21b981 1241 */
3d28ff59 1242#ifdef SMP
5d21b981 1243static void lwkt_setcpu_remote(void *arg);
3d28ff59 1244#endif
5d21b981
MD
1245
1246void
1247lwkt_setcpu_self(globaldata_t rgd)
1248{
1249#ifdef SMP
1250 thread_t td = curthread;
1251
1252 if (td->td_gd != rgd) {
1253 crit_enter_quick(td);
1254 td->td_flags |= TDF_MIGRATING;
1255 lwkt_deschedule_self(td);
52eedfb5 1256 TAILQ_REMOVE(&td->td_gd->gd_tdallq, td, td_allq);
b8a98473 1257 lwkt_send_ipiq(rgd, (ipifunc1_t)lwkt_setcpu_remote, td);
5d21b981
MD
1258 lwkt_switch();
1259 /* we are now on the target cpu */
52eedfb5 1260 TAILQ_INSERT_TAIL(&rgd->gd_tdallq, td, td_allq);
5d21b981
MD
1261 crit_exit_quick(td);
1262 }
1263#endif
1264}
1265
ecdefdda
MD
1266void
1267lwkt_migratecpu(int cpuid)
1268{
1269#ifdef SMP
1270 globaldata_t rgd;
1271
1272 rgd = globaldata_find(cpuid);
1273 lwkt_setcpu_self(rgd);
1274#endif
1275}
1276
5d21b981
MD
1277/*
1278 * Remote IPI for cpu migration (called while in a critical section so we
1279 * do not have to enter another one). The thread has already been moved to
1280 * our cpu's allq, but we must wait for the thread to be completely switched
1281 * out on the originating cpu before we schedule it on ours or the stack
1282 * state may be corrupt. We clear TDF_MIGRATING after flushing the GD
1283 * change to main memory.
1284 *
1285 * XXX The use of TDF_MIGRATING might not be sufficient to avoid races
1286 * against wakeups. It is best if this interface is used only when there
1287 * are no pending events that might try to schedule the thread.
1288 */
3d28ff59 1289#ifdef SMP
5d21b981
MD
1290static void
1291lwkt_setcpu_remote(void *arg)
1292{
1293 thread_t td = arg;
1294 globaldata_t gd = mycpu;
1295
c1102e9f 1296 while (td->td_flags & (TDF_RUNNING|TDF_PREEMPT_LOCK))
35238fa5 1297 cpu_lfence();
5d21b981 1298 td->td_gd = gd;
35238fa5 1299 cpu_sfence();
5d21b981 1300 td->td_flags &= ~TDF_MIGRATING;
344ad853 1301 KKASSERT(td->td_proc == NULL || (td->td_proc->p_flag & P_ONRUNQ) == 0);
5d21b981
MD
1302 _lwkt_enqueue(td);
1303}
3d28ff59 1304#endif
5d21b981 1305
553ea3c8 1306struct lwp *
4b5f931b
MD
1307lwkt_preempted_proc(void)
1308{
73e4f7b9 1309 thread_t td = curthread;
4b5f931b
MD
1310 while (td->td_preempted)
1311 td = td->td_preempted;
553ea3c8 1312 return(td->td_lwp);
4b5f931b
MD
1313}
1314
f1d1c3fa 1315/*
41a01a4d
MD
1316 * Block on the specified wait queue until signaled. A generation number
1317 * must be supplied to interlock the wait queue. The function will
1318 * return immediately if the generation number does not match the wait
1319 * structure's generation number.
f1d1c3fa
MD
1320 */
1321void
ae8050a4 1322lwkt_block(lwkt_wait_t w, const char *wmesg, int *gen)
f1d1c3fa
MD
1323{
1324 thread_t td = curthread;
f1d1c3fa 1325
d666840a 1326 spin_lock_wr(&w->wa_spinlock);
ae8050a4 1327 if (w->wa_gen == *gen) {
f1d1c3fa 1328 _lwkt_dequeue(td);
344ad853 1329 td->td_flags |= TDF_BLOCKQ;
f1d1c3fa
MD
1330 TAILQ_INSERT_TAIL(&w->wa_waitq, td, td_threadq);
1331 ++w->wa_count;
1332 td->td_wait = w;
ae8050a4 1333 td->td_wmesg = wmesg;
d666840a 1334 spin_unlock_wr(&w->wa_spinlock);
f1d1c3fa 1335 lwkt_switch();
344ad853
MD
1336 KKASSERT((td->td_flags & TDF_BLOCKQ) == 0);
1337 td->td_wmesg = NULL;
9d265729
MD
1338 *gen = w->wa_gen;
1339 } else {
1340 *gen = w->wa_gen;
d666840a 1341 spin_unlock_wr(&w->wa_spinlock);
8ad65e08 1342 }
f1d1c3fa
MD
1343}
1344
1345/*
1346 * Signal a wait queue. We gain ownership of the wait queue in order to
1347 * signal it. Once a thread is removed from the wait queue we have to
1348 * deal with the cpu owning the thread.
1349 *
1350 * Note: alternatively we could message the target cpu owning the wait
1351 * queue. YYY implement as sysctl.
1352 */
1353void
ece04fd0 1354lwkt_signal(lwkt_wait_t w, int count)
f1d1c3fa
MD
1355{
1356 thread_t td;
f1d1c3fa 1357
d666840a 1358 spin_lock_wr(&w->wa_spinlock);
f1d1c3fa 1359 ++w->wa_gen;
ece04fd0
MD
1360 if (count < 0)
1361 count = w->wa_count;
f1d1c3fa
MD
1362 while ((td = TAILQ_FIRST(&w->wa_waitq)) != NULL && count) {
1363 --count;
1364 --w->wa_count;
344ad853 1365 KKASSERT(td->td_flags & TDF_BLOCKQ);
f1d1c3fa 1366 TAILQ_REMOVE(&w->wa_waitq, td, td_threadq);
344ad853 1367 td->td_flags &= ~TDF_BLOCKQ;
f1d1c3fa 1368 td->td_wait = NULL;
d666840a 1369 spin_unlock_wr(&w->wa_spinlock);
344ad853 1370 KKASSERT(td->td_proc == NULL || (td->td_proc->p_flag & P_ONRUNQ) == 0);
b8a98473 1371#ifdef SMP
a72187e9 1372 if (td->td_gd == mycpu) {
f1d1c3fa
MD
1373 _lwkt_enqueue(td);
1374 } else {
b8a98473 1375 lwkt_send_ipiq(td->td_gd, (ipifunc1_t)lwkt_schedule, td);
f1d1c3fa 1376 }
b8a98473
MD
1377#else
1378 _lwkt_enqueue(td);
1379#endif
d666840a 1380 spin_lock_wr(&w->wa_spinlock);
f1d1c3fa 1381 }
d666840a 1382 spin_unlock_wr(&w->wa_spinlock);
f1d1c3fa
MD
1383}
1384
99df837e
MD
1385/*
1386 * Create a kernel process/thread/whatever. It shares it's address space
1387 * with proc0 - ie: kernel only.
1388 *
365fa13f
MD
1389 * NOTE! By default new threads are created with the MP lock held. A
1390 * thread which does not require the MP lock should release it by calling
1391 * rel_mplock() at the start of the new thread.
99df837e
MD
1392 */
1393int
1394lwkt_create(void (*func)(void *), void *arg,
75cdbe6c 1395 struct thread **tdp, thread_t template, int tdflags, int cpu,
ef0fdad1 1396 const char *fmt, ...)
99df837e 1397{
73e4f7b9 1398 thread_t td;
e2565a42 1399 __va_list ap;
99df837e 1400
d3d32139
MD
1401 td = lwkt_alloc_thread(template, LWKT_THREAD_STACK, cpu,
1402 tdflags | TDF_VERBOSE);
a2a5ad0d
MD
1403 if (tdp)
1404 *tdp = td;
709799ea 1405 cpu_set_thread_handler(td, lwkt_exit, func, arg);
99df837e
MD
1406
1407 /*
1408 * Set up arg0 for 'ps' etc
1409 */
e2565a42 1410 __va_start(ap, fmt);
99df837e 1411 vsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
e2565a42 1412 __va_end(ap);
99df837e
MD
1413
1414 /*
1415 * Schedule the thread to run
1416 */
ef0fdad1
MD
1417 if ((td->td_flags & TDF_STOPREQ) == 0)
1418 lwkt_schedule(td);
1419 else
1420 td->td_flags &= ~TDF_STOPREQ;
99df837e
MD
1421 return 0;
1422}
1423
2d93b37a 1424/*
2d93b37a
MD
1425 * kthread_* is specific to the kernel and is not needed by userland.
1426 */
1427#ifdef _KERNEL
1428
99df837e
MD
1429/*
1430 * Destroy an LWKT thread. Warning! This function is not called when
1431 * a process exits, cpu_proc_exit() directly calls cpu_thread_exit() and
1432 * uses a different reaping mechanism.
1433 */
1434void
1435lwkt_exit(void)
1436{
1437 thread_t td = curthread;
8826f33a 1438 globaldata_t gd;
99df837e
MD
1439
1440 if (td->td_flags & TDF_VERBOSE)
1441 printf("kthread %p %s has exited\n", td, td->td_comm);
f6bf3af1 1442 caps_exit(td);
37af14fe
MD
1443 crit_enter_quick(td);
1444 lwkt_deschedule_self(td);
8826f33a 1445 gd = mycpu;
e56e4dea 1446 lwkt_remove_tdallq(td);
8826f33a
MD
1447 if (td->td_flags & TDF_ALLOCATED_THREAD) {
1448 ++gd->gd_tdfreecount;
1449 TAILQ_INSERT_TAIL(&gd->gd_tdfreeq, td, td_threadq);
1450 }
99df837e
MD
1451 cpu_thread_exit();
1452}
1453
e56e4dea
MD
1454void
1455lwkt_remove_tdallq(thread_t td)
1456{
1457 KKASSERT(td->td_gd == mycpu);
1458 TAILQ_REMOVE(&td->td_gd->gd_tdallq, td, td_allq);
1459}
1460
2d93b37a
MD
1461#endif /* _KERNEL */
1462
1463void
1464crit_panic(void)
1465{
1466 thread_t td = curthread;
1467 int lpri = td->td_pri;
1468
1469 td->td_pri = 0;
1470 panic("td_pri is/would-go negative! %p %d", td, lpri);
1471}
1472
d165e668
MD
1473#ifdef SMP
1474
bd8015ca
MD
1475/*
1476 * Called from debugger/panic on cpus which have been stopped. We must still
1477 * process the IPIQ while stopped, even if we were stopped while in a critical
1478 * section (XXX).
1479 *
1480 * If we are dumping also try to process any pending interrupts. This may
1481 * or may not work depending on the state of the cpu at the point it was
1482 * stopped.
1483 */
1484void
1485lwkt_smp_stopped(void)
1486{
1487 globaldata_t gd = mycpu;
1488
1489 crit_enter_gd(gd);
1490 if (dumping) {
1491 lwkt_process_ipiq();
1492 splz();
1493 } else {
1494 lwkt_process_ipiq();
1495 }
1496 crit_exit_gd(gd);
1497}
1498
57aa743c
MD
1499/*
1500 * get_mplock() calls this routine if it is unable to obtain the MP lock.
1501 * get_mplock() has already incremented td_mpcount. We must block and
1502 * not return until giant is held.
1503 *
1504 * All we have to do is lwkt_switch() away. The LWKT scheduler will not
1505 * reschedule the thread until it can obtain the giant lock for it.
1506 */
1507void
1508lwkt_mp_lock_contested(void)
1509{
1510#ifdef _KERNEL
1511 loggiant(beg);
1512#endif
1513 lwkt_switch();
1514#ifdef _KERNEL
1515 loggiant(end);
1516#endif
1517}
1518
d165e668 1519#endif