kernel - Fix excessive call stack depth on stuck interrupt
[dragonfly.git] / sys / kern / lwkt_token.c
blob8e35887b3e38c7c13ae939142bbfcc99bb08fcb2
1 /*
2 * Copyright (c) 2003,2004,2009 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 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
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.
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
32 * SUCH DAMAGE.
36 * lwkt_token - Implement soft token locks.
38 * Tokens are locks which serialize a thread only while the thread is
39 * running. If the thread blocks all tokens are released, then reacquired
40 * when the thread resumes.
42 * This implementation requires no critical sections or spin locks, but
43 * does use atomic_cmpset_ptr().
45 * Tokens may be recursively acquired by the same thread. However the
46 * caller must be sure to release such tokens in reverse order.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h>
52 #include <sys/rtprio.h>
53 #include <sys/queue.h>
54 #include <sys/sysctl.h>
55 #include <sys/ktr.h>
56 #include <sys/kthread.h>
57 #include <machine/cpu.h>
58 #include <sys/lock.h>
59 #include <sys/spinlock.h>
61 #include <sys/thread2.h>
62 #include <sys/spinlock2.h>
63 #include <sys/mplock2.h>
65 #include <vm/vm.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_pager.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_zone.h>
75 #include <machine/stdarg.h>
76 #include <machine/smp.h>
78 #include "opt_ddb.h"
79 #ifdef DDB
80 #include <ddb/ddb.h>
81 #endif
83 extern int lwkt_sched_debug;
85 #ifndef LWKT_NUM_POOL_TOKENS
86 #define LWKT_NUM_POOL_TOKENS 4001 /* prime number */
87 #endif
89 struct lwkt_pool_token {
90 struct lwkt_token token;
91 } __cachealign;
93 static struct lwkt_pool_token pool_tokens[LWKT_NUM_POOL_TOKENS];
94 struct spinlock tok_debug_spin = SPINLOCK_INITIALIZER(&tok_debug_spin, "tok_debug_spin");
96 #define TOKEN_STRING "REF=%p TOK=%p TD=%p"
97 #define TOKEN_ARGS lwkt_tokref_t ref, lwkt_token_t tok, struct thread *td
98 #define CONTENDED_STRING TOKEN_STRING " (contention started)"
99 #define UNCONTENDED_STRING TOKEN_STRING " (contention stopped)"
100 #if !defined(KTR_TOKENS)
101 #define KTR_TOKENS KTR_ALL
102 #endif
104 KTR_INFO_MASTER(tokens);
105 KTR_INFO(KTR_TOKENS, tokens, fail, 0, TOKEN_STRING, TOKEN_ARGS);
106 KTR_INFO(KTR_TOKENS, tokens, succ, 1, TOKEN_STRING, TOKEN_ARGS);
107 #if 0
108 KTR_INFO(KTR_TOKENS, tokens, release, 2, TOKEN_STRING, TOKEN_ARGS);
109 KTR_INFO(KTR_TOKENS, tokens, remote, 3, TOKEN_STRING, TOKEN_ARGS);
110 KTR_INFO(KTR_TOKENS, tokens, reqremote, 4, TOKEN_STRING, TOKEN_ARGS);
111 KTR_INFO(KTR_TOKENS, tokens, reqfail, 5, TOKEN_STRING, TOKEN_ARGS);
112 KTR_INFO(KTR_TOKENS, tokens, drain, 6, TOKEN_STRING, TOKEN_ARGS);
113 KTR_INFO(KTR_TOKENS, tokens, contention_start, 7, CONTENDED_STRING, TOKEN_ARGS);
114 KTR_INFO(KTR_TOKENS, tokens, contention_stop, 7, UNCONTENDED_STRING, TOKEN_ARGS);
115 #endif
117 #define logtoken(name, ref) \
118 KTR_LOG(tokens_ ## name, ref, ref->tr_tok, curthread)
121 * Global tokens. These replace the MP lock for major subsystem locking.
122 * These tokens are initially used to lockup both global and individual
123 * operations.
125 * Once individual structures get their own locks these tokens are used
126 * only to protect global lists & other variables and to interlock
127 * allocations and teardowns and such.
129 * The UP initializer causes token acquisition to also acquire the MP lock
130 * for maximum compatibility. The feature may be enabled and disabled at
131 * any time, the MP state is copied to the tokref when the token is acquired
132 * and will not race against sysctl changes.
134 struct lwkt_token mp_token = LWKT_TOKEN_INITIALIZER(mp_token);
135 struct lwkt_token pmap_token = LWKT_TOKEN_INITIALIZER(pmap_token);
136 struct lwkt_token dev_token = LWKT_TOKEN_INITIALIZER(dev_token);
137 struct lwkt_token vm_token = LWKT_TOKEN_INITIALIZER(vm_token);
138 struct lwkt_token vmspace_token = LWKT_TOKEN_INITIALIZER(vmspace_token);
139 struct lwkt_token kvm_token = LWKT_TOKEN_INITIALIZER(kvm_token);
140 struct lwkt_token sigio_token = LWKT_TOKEN_INITIALIZER(sigio_token);
141 struct lwkt_token tty_token = LWKT_TOKEN_INITIALIZER(tty_token);
142 struct lwkt_token vnode_token = LWKT_TOKEN_INITIALIZER(vnode_token);
144 static int lwkt_token_spin = 5;
145 SYSCTL_INT(_lwkt, OID_AUTO, token_spin, CTLFLAG_RW,
146 &lwkt_token_spin, 0, "Decontention spin loops");
147 static int lwkt_token_delay = 0;
148 SYSCTL_INT(_lwkt, OID_AUTO, token_delay, CTLFLAG_RW,
149 &lwkt_token_delay, 0, "Decontention spin delay in ns");
152 * The collision count is bumped every time the LWKT scheduler fails
153 * to acquire needed tokens in addition to a normal lwkt_gettoken()
154 * stall.
156 SYSCTL_LONG(_lwkt, OID_AUTO, mp_collisions, CTLFLAG_RW,
157 &mp_token.t_collisions, 0, "Collision counter of mp_token");
158 SYSCTL_LONG(_lwkt, OID_AUTO, pmap_collisions, CTLFLAG_RW,
159 &pmap_token.t_collisions, 0, "Collision counter of pmap_token");
160 SYSCTL_LONG(_lwkt, OID_AUTO, dev_collisions, CTLFLAG_RW,
161 &dev_token.t_collisions, 0, "Collision counter of dev_token");
162 SYSCTL_LONG(_lwkt, OID_AUTO, vm_collisions, CTLFLAG_RW,
163 &vm_token.t_collisions, 0, "Collision counter of vm_token");
164 SYSCTL_LONG(_lwkt, OID_AUTO, vmspace_collisions, CTLFLAG_RW,
165 &vmspace_token.t_collisions, 0, "Collision counter of vmspace_token");
166 SYSCTL_LONG(_lwkt, OID_AUTO, kvm_collisions, CTLFLAG_RW,
167 &kvm_token.t_collisions, 0, "Collision counter of kvm_token");
168 SYSCTL_LONG(_lwkt, OID_AUTO, sigio_collisions, CTLFLAG_RW,
169 &sigio_token.t_collisions, 0, "Collision counter of sigio_token");
170 SYSCTL_LONG(_lwkt, OID_AUTO, tty_collisions, CTLFLAG_RW,
171 &tty_token.t_collisions, 0, "Collision counter of tty_token");
172 SYSCTL_LONG(_lwkt, OID_AUTO, vnode_collisions, CTLFLAG_RW,
173 &vnode_token.t_collisions, 0, "Collision counter of vnode_token");
175 int tokens_debug_output;
176 SYSCTL_INT(_lwkt, OID_AUTO, tokens_debug_output, CTLFLAG_RW,
177 &tokens_debug_output, 0, "Generate stack trace N times");
180 #ifdef DEBUG_LOCKS_LATENCY
182 static long tokens_add_latency;
183 SYSCTL_LONG(_debug, OID_AUTO, tokens_add_latency, CTLFLAG_RW,
184 &tokens_add_latency, 0,
185 "Add spinlock latency");
187 #endif
190 static int _lwkt_getalltokens_sorted(thread_t td);
193 * Acquire the initial mplock
195 * (low level boot only)
197 void
198 cpu_get_initial_mplock(void)
200 KKASSERT(mp_token.t_ref == NULL);
201 if (lwkt_trytoken(&mp_token) == FALSE)
202 panic("cpu_get_initial_mplock");
206 * Return a pool token given an address. Use a prime number to reduce
207 * overlaps.
209 static __inline
210 lwkt_token_t
211 _lwkt_token_pool_lookup(void *ptr)
213 u_int i;
215 i = (u_int)(uintptr_t)ptr % LWKT_NUM_POOL_TOKENS;
216 return (&pool_tokens[i].token);
220 * Initialize a tokref_t prior to making it visible in the thread's
221 * token array.
223 static __inline
224 void
225 _lwkt_tokref_init(lwkt_tokref_t ref, lwkt_token_t tok, thread_t td, long excl)
227 ref->tr_tok = tok;
228 ref->tr_count = excl;
229 ref->tr_owner = td;
233 * Attempt to acquire a shared or exclusive token. Returns TRUE on success,
234 * FALSE on failure.
236 * If TOK_EXCLUSIVE is set in mode we are attempting to get an exclusive
237 * token, otherwise are attempting to get a shared token.
239 * If TOK_EXCLREQ is set in mode this is a blocking operation, otherwise
240 * it is a non-blocking operation (for both exclusive or shared acquisions).
242 static __inline
244 _lwkt_trytokref(lwkt_tokref_t ref, thread_t td, long mode)
246 lwkt_token_t tok;
247 lwkt_tokref_t oref;
248 long count;
250 tok = ref->tr_tok;
251 KASSERT(((mode & TOK_EXCLREQ) == 0 || /* non blocking */
252 td->td_gd->gd_intr_nesting_level == 0 ||
253 panic_cpu_gd == mycpu),
254 ("Attempt to acquire token %p not already "
255 "held in hard code section", tok));
257 if (mode & TOK_EXCLUSIVE) {
259 * Attempt to get an exclusive token
261 for (;;) {
262 count = tok->t_count;
263 oref = tok->t_ref; /* can be NULL */
264 cpu_ccfence();
265 if ((count & ~TOK_EXCLREQ) == 0) {
267 * It is possible to get the exclusive bit.
268 * We must clear TOK_EXCLREQ on successful
269 * acquisition.
271 if (atomic_cmpset_long(&tok->t_count, count,
272 (count & ~TOK_EXCLREQ) |
273 TOK_EXCLUSIVE)) {
274 KKASSERT(tok->t_ref == NULL);
275 tok->t_ref = ref;
276 return TRUE;
278 /* retry */
279 } else if ((count & TOK_EXCLUSIVE) &&
280 oref >= &td->td_toks_base &&
281 oref < td->td_toks_stop) {
283 * Our thread already holds the exclusive
284 * bit, we treat this tokref as a shared
285 * token (sorta) to make the token release
286 * code easier.
288 * NOTE: oref cannot race above if it
289 * happens to be ours, so we're good.
290 * But we must still have a stable
291 * variable for both parts of the
292 * comparison.
294 * NOTE: Since we already have an exclusive
295 * lock and don't need to check EXCLREQ
296 * we can just use an atomic_add here
298 atomic_add_long(&tok->t_count, TOK_INCR);
299 ref->tr_count &= ~TOK_EXCLUSIVE;
300 return TRUE;
301 } else if ((mode & TOK_EXCLREQ) &&
302 (count & TOK_EXCLREQ) == 0) {
304 * Unable to get the exclusive bit but being
305 * asked to set the exclusive-request bit.
306 * Since we are going to retry anyway just
307 * set the bit unconditionally.
309 atomic_set_long(&tok->t_count, TOK_EXCLREQ);
310 return FALSE;
311 } else {
313 * Unable to get the exclusive bit and not
314 * being asked to set the exclusive-request
315 * (aka lwkt_trytoken()), or EXCLREQ was
316 * already set.
318 cpu_pause();
319 return FALSE;
321 /* retry */
323 } else {
325 * Attempt to get a shared token. Note that TOK_EXCLREQ
326 * for shared tokens simply means the caller intends to
327 * block. We never actually set the bit in tok->t_count.
329 for (;;) {
330 count = tok->t_count;
331 oref = tok->t_ref; /* can be NULL */
332 cpu_ccfence();
333 if ((count & (TOK_EXCLUSIVE/*|TOK_EXCLREQ*/)) == 0) {
335 * It may be possible to get the token shared.
337 if ((atomic_fetchadd_long(&tok->t_count, TOK_INCR) & TOK_EXCLUSIVE) == 0) {
338 return TRUE;
340 atomic_fetchadd_long(&tok->t_count, -TOK_INCR);
341 /* retry */
342 } else if ((count & TOK_EXCLUSIVE) &&
343 oref >= &td->td_toks_base &&
344 oref < td->td_toks_stop) {
346 * We own the exclusive bit on the token so
347 * we can in fact also get it shared.
349 atomic_add_long(&tok->t_count, TOK_INCR);
350 return TRUE;
351 } else {
353 * We failed to get the token shared
355 return FALSE;
357 /* retry */
362 static __inline
364 _lwkt_trytokref_spin(lwkt_tokref_t ref, thread_t td, long mode)
366 int spin;
368 if (_lwkt_trytokref(ref, td, mode)) {
369 #ifdef DEBUG_LOCKS_LATENCY
370 long j;
371 for (j = tokens_add_latency; j > 0; --j)
372 cpu_ccfence();
373 #endif
374 return TRUE;
376 for (spin = lwkt_token_spin; spin > 0; --spin) {
377 if (lwkt_token_delay)
378 tsc_delay(lwkt_token_delay);
379 else
380 cpu_pause();
381 if (_lwkt_trytokref(ref, td, mode)) {
382 #ifdef DEBUG_LOCKS_LATENCY
383 long j;
384 for (j = tokens_add_latency; j > 0; --j)
385 cpu_ccfence();
386 #endif
387 return TRUE;
390 return FALSE;
394 * Release a token that we hold.
396 static __inline
397 void
398 _lwkt_reltokref(lwkt_tokref_t ref, thread_t td)
400 lwkt_token_t tok;
401 long count;
403 tok = ref->tr_tok;
404 for (;;) {
405 count = tok->t_count;
406 cpu_ccfence();
407 if (tok->t_ref == ref) {
409 * We are an exclusive holder. We must clear tr_ref
410 * before we clear the TOK_EXCLUSIVE bit. If we are
411 * unable to clear the bit we must restore
412 * tok->t_ref.
414 KKASSERT(count & TOK_EXCLUSIVE);
415 tok->t_ref = NULL;
416 if (atomic_cmpset_long(&tok->t_count, count,
417 count & ~TOK_EXCLUSIVE)) {
418 return;
420 tok->t_ref = ref;
421 /* retry */
422 } else {
424 * We are a shared holder
426 KKASSERT(count & TOK_COUNTMASK);
427 if (atomic_cmpset_long(&tok->t_count, count,
428 count - TOK_INCR)) {
429 return;
431 /* retry */
433 /* retry */
438 * Obtain all the tokens required by the specified thread on the current
439 * cpu, return 0 on failure and non-zero on success. If a failure occurs
440 * any partially acquired tokens will be released prior to return.
442 * lwkt_getalltokens is called by the LWKT scheduler to re-acquire all
443 * tokens that the thread had to release when it switched away.
445 * If spinning is non-zero this function acquires the tokens in a particular
446 * order to deal with potential deadlocks. We simply use address order for
447 * the case.
449 * Called from a critical section.
452 lwkt_getalltokens(thread_t td, int spinning)
454 lwkt_tokref_t scan;
455 lwkt_token_t tok;
457 if (spinning)
458 return(_lwkt_getalltokens_sorted(td));
461 * Acquire tokens in forward order, assign or validate tok->t_ref.
463 for (scan = &td->td_toks_base; scan < td->td_toks_stop; ++scan) {
464 tok = scan->tr_tok;
465 for (;;) {
467 * Only try really hard on the last token
469 if (scan == td->td_toks_stop - 1) {
470 if (_lwkt_trytokref_spin(scan, td, scan->tr_count))
471 break;
472 } else {
473 if (_lwkt_trytokref(scan, td, scan->tr_count))
474 break;
478 * Otherwise we failed to acquire all the tokens.
479 * Release whatever we did get.
481 KASSERT(tok->t_desc,
482 ("token %p is not initialized", tok));
483 strncpy(td->td_gd->gd_cnt.v_lock_name,
484 tok->t_desc,
485 sizeof(td->td_gd->gd_cnt.v_lock_name) - 1);
487 if (lwkt_sched_debug > 0) {
488 --lwkt_sched_debug;
489 kprintf("toka %p %s %s\n",
490 tok, tok->t_desc, td->td_comm);
492 td->td_wmesg = tok->t_desc;
493 ++tok->t_collisions;
494 while (--scan >= &td->td_toks_base)
495 _lwkt_reltokref(scan, td);
496 return(FALSE);
499 return (TRUE);
503 * Release all tokens owned by the specified thread on the current cpu.
505 * This code is really simple. Even in cases where we own all the tokens
506 * note that t_ref may not match the scan for recursively held tokens which
507 * are held deeper in the stack, or for the case where a lwkt_getalltokens()
508 * failed.
510 * Tokens are released in reverse order to reduce chasing race failures.
512 * Called from a critical section.
514 void
515 lwkt_relalltokens(thread_t td)
517 lwkt_tokref_t scan;
520 * Weird order is to try to avoid a panic loop
522 if (td->td_toks_have) {
523 scan = td->td_toks_have;
524 td->td_toks_have = NULL;
525 } else {
526 scan = td->td_toks_stop;
528 while (--scan >= &td->td_toks_base)
529 _lwkt_reltokref(scan, td);
533 * This is the decontention version of lwkt_getalltokens(). The tokens are
534 * acquired in address-sorted order to deal with any deadlocks. Ultimately
535 * token failures will spin into the scheduler and get here.
537 * Called from critical section
539 static
541 _lwkt_getalltokens_sorted(thread_t td)
543 lwkt_tokref_t sort_array[LWKT_MAXTOKENS];
544 lwkt_tokref_t scan;
545 lwkt_token_t tok;
546 int i;
547 int j;
548 int n;
551 * Sort the token array. Yah yah, I know this isn't fun.
553 * NOTE: Recursively acquired tokens are ordered the same as in the
554 * td_toks_array so we can always get the earliest one first.
556 i = 0;
557 scan = &td->td_toks_base;
558 while (scan < td->td_toks_stop) {
559 for (j = 0; j < i; ++j) {
560 if (scan->tr_tok < sort_array[j]->tr_tok)
561 break;
563 if (j != i) {
564 bcopy(sort_array + j, sort_array + j + 1,
565 (i - j) * sizeof(lwkt_tokref_t));
567 sort_array[j] = scan;
568 ++scan;
569 ++i;
571 n = i;
574 * Acquire tokens in forward order, assign or validate tok->t_ref.
576 for (i = 0; i < n; ++i) {
577 scan = sort_array[i];
578 tok = scan->tr_tok;
579 for (;;) {
581 * Only try really hard on the last token
583 if (scan == td->td_toks_stop - 1) {
584 if (_lwkt_trytokref_spin(scan, td, scan->tr_count))
585 break;
586 } else {
587 if (_lwkt_trytokref(scan, td, scan->tr_count))
588 break;
592 * Otherwise we failed to acquire all the tokens.
593 * Release whatever we did get.
595 if (lwkt_sched_debug > 0) {
596 --lwkt_sched_debug;
597 kprintf("tokb %p %s %s\n",
598 tok, tok->t_desc, td->td_comm);
600 td->td_wmesg = tok->t_desc;
601 ++tok->t_collisions;
602 while (--i >= 0) {
603 scan = sort_array[i];
604 _lwkt_reltokref(scan, td);
606 return(FALSE);
611 * We were successful, there is no need for another core to signal
612 * us.
614 return (TRUE);
618 * Get a serializing token. This routine can block.
620 void
621 lwkt_gettoken(lwkt_token_t tok)
623 thread_t td = curthread;
624 lwkt_tokref_t ref;
626 ref = td->td_toks_stop;
627 KKASSERT(ref < &td->td_toks_end);
628 ++td->td_toks_stop;
629 cpu_ccfence();
630 _lwkt_tokref_init(ref, tok, td, TOK_EXCLUSIVE|TOK_EXCLREQ);
632 #ifdef DEBUG_LOCKS
634 * Taking an exclusive token after holding it shared will
635 * livelock. Scan for that case and assert.
637 lwkt_tokref_t tk;
638 int found = 0;
639 for (tk = &td->td_toks_base; tk < ref; tk++) {
640 if (tk->tr_tok != tok)
641 continue;
643 found++;
644 if (tk->tr_count & TOK_EXCLUSIVE)
645 goto good;
647 /* We found only shared instances of this token if found >0 here */
648 KASSERT((found == 0), ("Token %p s/x livelock", tok));
649 good:
650 #endif
652 if (_lwkt_trytokref_spin(ref, td, TOK_EXCLUSIVE|TOK_EXCLREQ))
653 return;
656 * Give up running if we can't acquire the token right now.
658 * Since the tokref is already active the scheduler now
659 * takes care of acquisition, so we need only call
660 * lwkt_switch().
662 * Since we failed this was not a recursive token so upon
663 * return tr_tok->t_ref should be assigned to this specific
664 * ref.
666 td->td_wmesg = tok->t_desc;
667 ++tok->t_collisions;
668 logtoken(fail, ref);
669 td->td_toks_have = td->td_toks_stop - 1;
671 if (tokens_debug_output > 0) {
672 --tokens_debug_output;
673 spin_lock(&tok_debug_spin);
674 kprintf("Excl Token thread %p %s %s\n",
675 td, tok->t_desc, td->td_comm);
676 print_backtrace(6);
677 kprintf("\n");
678 spin_unlock(&tok_debug_spin);
681 lwkt_switch();
682 logtoken(succ, ref);
683 KKASSERT(tok->t_ref == ref);
687 * Similar to gettoken but we acquire a shared token instead of an exclusive
688 * token.
690 void
691 lwkt_gettoken_shared(lwkt_token_t tok)
693 thread_t td = curthread;
694 lwkt_tokref_t ref;
696 ref = td->td_toks_stop;
697 KKASSERT(ref < &td->td_toks_end);
698 ++td->td_toks_stop;
699 cpu_ccfence();
700 _lwkt_tokref_init(ref, tok, td, TOK_EXCLREQ);
702 #ifdef DEBUG_LOCKS
704 * Taking a pool token in shared mode is a bad idea; other
705 * addresses deeper in the call stack may hash to the same pool
706 * token and you may end up with an exclusive-shared livelock.
707 * Warn in this condition.
709 if ((tok >= &pool_tokens[0].token) &&
710 (tok < &pool_tokens[LWKT_NUM_POOL_TOKENS].token))
711 kprintf("Warning! Taking pool token %p in shared mode\n", tok);
712 #endif
715 if (_lwkt_trytokref_spin(ref, td, TOK_EXCLREQ))
716 return;
719 * Give up running if we can't acquire the token right now.
721 * Since the tokref is already active the scheduler now
722 * takes care of acquisition, so we need only call
723 * lwkt_switch().
725 * Since we failed this was not a recursive token so upon
726 * return tr_tok->t_ref should be assigned to this specific
727 * ref.
729 td->td_wmesg = tok->t_desc;
730 ++tok->t_collisions;
731 logtoken(fail, ref);
732 td->td_toks_have = td->td_toks_stop - 1;
734 if (tokens_debug_output > 0) {
735 --tokens_debug_output;
736 spin_lock(&tok_debug_spin);
737 kprintf("Shar Token thread %p %s %s\n",
738 td, tok->t_desc, td->td_comm);
739 print_backtrace(6);
740 kprintf("\n");
741 spin_unlock(&tok_debug_spin);
744 lwkt_switch();
745 logtoken(succ, ref);
749 * Attempt to acquire a token, return TRUE on success, FALSE on failure.
751 * We setup the tokref in case we actually get the token (if we switch later
752 * it becomes mandatory so we set TOK_EXCLREQ), but we call trytokref without
753 * TOK_EXCLREQ in case we fail.
756 lwkt_trytoken(lwkt_token_t tok)
758 thread_t td = curthread;
759 lwkt_tokref_t ref;
761 ref = td->td_toks_stop;
762 KKASSERT(ref < &td->td_toks_end);
763 ++td->td_toks_stop;
764 cpu_ccfence();
765 _lwkt_tokref_init(ref, tok, td, TOK_EXCLUSIVE|TOK_EXCLREQ);
767 if (_lwkt_trytokref(ref, td, TOK_EXCLUSIVE))
768 return TRUE;
771 * Failed, unpend the request
773 cpu_ccfence();
774 --td->td_toks_stop;
775 ++tok->t_collisions;
776 return FALSE;
779 lwkt_token_t
780 lwkt_getpooltoken(void *ptr)
782 lwkt_token_t tok;
784 tok = _lwkt_token_pool_lookup(ptr);
785 lwkt_gettoken(tok);
786 return (tok);
790 * Release a serializing token.
792 * WARNING! All tokens must be released in reverse order. This will be
793 * asserted.
795 void
796 lwkt_reltoken(lwkt_token_t tok)
798 thread_t td = curthread;
799 lwkt_tokref_t ref;
802 * Remove ref from thread token list and assert that it matches
803 * the token passed in. Tokens must be released in reverse order.
805 ref = td->td_toks_stop - 1;
806 KKASSERT(ref >= &td->td_toks_base && ref->tr_tok == tok);
807 _lwkt_reltokref(ref, td);
808 cpu_sfence();
809 td->td_toks_stop = ref;
813 * It is faster for users of lwkt_getpooltoken() to use the returned
814 * token and just call lwkt_reltoken(), but for convenience we provide
815 * this function which looks the token up based on the ident.
817 void
818 lwkt_relpooltoken(void *ptr)
820 lwkt_token_t tok = _lwkt_token_pool_lookup(ptr);
821 lwkt_reltoken(tok);
825 * Return a count of the number of token refs the thread has to the
826 * specified token, whether it currently owns the token or not.
829 lwkt_cnttoken(lwkt_token_t tok, thread_t td)
831 lwkt_tokref_t scan;
832 int count = 0;
834 for (scan = &td->td_toks_base; scan < td->td_toks_stop; ++scan) {
835 if (scan->tr_tok == tok)
836 ++count;
838 return(count);
842 * Pool tokens are used to provide a type-stable serializing token
843 * pointer that does not race against disappearing data structures.
845 * This routine is called in early boot just after we setup the BSP's
846 * globaldata structure.
848 void
849 lwkt_token_pool_init(void)
851 int i;
853 for (i = 0; i < LWKT_NUM_POOL_TOKENS; ++i)
854 lwkt_token_init(&pool_tokens[i].token, "pool");
857 lwkt_token_t
858 lwkt_token_pool_lookup(void *ptr)
860 return (_lwkt_token_pool_lookup(ptr));
864 * Initialize a token.
866 void
867 lwkt_token_init(lwkt_token_t tok, const char *desc)
869 tok->t_count = 0;
870 tok->t_ref = NULL;
871 tok->t_collisions = 0;
872 tok->t_desc = desc;
875 void
876 lwkt_token_uninit(lwkt_token_t tok)
878 /* empty */
882 * Exchange the two most recent tokens on the tokref stack. This allows
883 * you to release a token out of order.
885 * We have to be careful about the case where the top two tokens are
886 * the same token. In this case tok->t_ref will point to the deeper
887 * ref and must remain pointing to the deeper ref. If we were to swap
888 * it the first release would clear the token even though a second
889 * ref is still present.
891 * Only exclusively held tokens contain a reference to the tokref which
892 * has to be flipped along with the swap.
894 void
895 lwkt_token_swap(void)
897 lwkt_tokref_t ref1, ref2;
898 lwkt_token_t tok1, tok2;
899 long count1, count2;
900 thread_t td = curthread;
902 crit_enter();
904 ref1 = td->td_toks_stop - 1;
905 ref2 = td->td_toks_stop - 2;
906 KKASSERT(ref1 >= &td->td_toks_base);
907 KKASSERT(ref2 >= &td->td_toks_base);
909 tok1 = ref1->tr_tok;
910 tok2 = ref2->tr_tok;
911 count1 = ref1->tr_count;
912 count2 = ref2->tr_count;
914 if (tok1 != tok2) {
915 ref1->tr_tok = tok2;
916 ref1->tr_count = count2;
917 ref2->tr_tok = tok1;
918 ref2->tr_count = count1;
919 if (tok1->t_ref == ref1)
920 tok1->t_ref = ref2;
921 if (tok2->t_ref == ref2)
922 tok2->t_ref = ref1;
925 crit_exit();
928 #ifdef DDB
929 DB_SHOW_COMMAND(tokens, db_tok_all)
931 struct lwkt_token *tok, **ptr;
932 struct lwkt_token *toklist[16] = {
933 &mp_token,
934 &pmap_token,
935 &dev_token,
936 &vm_token,
937 &vmspace_token,
938 &kvm_token,
939 &sigio_token,
940 &tty_token,
941 &vnode_token,
942 NULL
945 ptr = toklist;
946 for (tok = *ptr; tok; tok = *(++ptr)) {
947 db_printf("tok=%p tr_owner=%p t_colissions=%ld t_desc=%s\n", tok,
948 (tok->t_ref ? tok->t_ref->tr_owner : NULL),
949 tok->t_collisions, tok->t_desc);
952 #endif /* DDB */