e1249cdd6ef4870593cea64ed3bbebb3fc0d9a4f
[dragonfly.git] / sys / kern / lwkt_token.c
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:
10  * 
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.
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
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * lwkt_token - Implement soft token locks.
37  *
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.
41  *
42  * This implementation requires no critical sections or spin locks, but
43  * does use atomic_cmpset_ptr().
44  *
45  * Tokens may be recursively acquired by the same thread.  However the
46  * caller must be sure to release such tokens in reverse order.
47  */
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/caps.h>
60 #include <sys/spinlock.h>
61
62 #include <sys/thread2.h>
63 #include <sys/spinlock2.h>
64 #include <sys/mplock2.h>
65
66 #include <vm/vm.h>
67 #include <vm/vm_param.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_page.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_pager.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_zone.h>
75
76 #include <machine/stdarg.h>
77 #include <machine/smp.h>
78
79 extern int lwkt_sched_debug;
80
81 #ifndef LWKT_NUM_POOL_TOKENS
82 #define LWKT_NUM_POOL_TOKENS    4001    /* prime number */
83 #endif
84
85 static lwkt_token       pool_tokens[LWKT_NUM_POOL_TOKENS];
86
87 #define TOKEN_STRING    "REF=%p TOK=%p TD=%p"
88 #define CONTENDED_STRING        "REF=%p TOK=%p TD=%p (contention started)"
89 #define UNCONTENDED_STRING      "REF=%p TOK=%p TD=%p (contention stopped)"
90 #if !defined(KTR_TOKENS)
91 #define KTR_TOKENS      KTR_ALL
92 #endif
93
94 KTR_INFO_MASTER(tokens);
95 KTR_INFO(KTR_TOKENS, tokens, fail, 0, TOKEN_STRING, sizeof(void *) * 3);
96 KTR_INFO(KTR_TOKENS, tokens, succ, 1, TOKEN_STRING, sizeof(void *) * 3);
97 #if 0
98 KTR_INFO(KTR_TOKENS, tokens, release, 2, TOKEN_STRING, sizeof(void *) * 3);
99 KTR_INFO(KTR_TOKENS, tokens, remote, 3, TOKEN_STRING, sizeof(void *) * 3);
100 KTR_INFO(KTR_TOKENS, tokens, reqremote, 4, TOKEN_STRING, sizeof(void *) * 3);
101 KTR_INFO(KTR_TOKENS, tokens, reqfail, 5, TOKEN_STRING, sizeof(void *) * 3);
102 KTR_INFO(KTR_TOKENS, tokens, drain, 6, TOKEN_STRING, sizeof(void *) * 3);
103 KTR_INFO(KTR_TOKENS, tokens, contention_start, 7, CONTENDED_STRING, sizeof(void *) * 3);
104 KTR_INFO(KTR_TOKENS, tokens, contention_stop, 7, UNCONTENDED_STRING, sizeof(void *) * 3);
105 #endif
106
107 #define logtoken(name, ref)                                             \
108         KTR_LOG(tokens_ ## name, ref, ref->tr_tok, curthread)
109
110 /*
111  * Global tokens.  These replace the MP lock for major subsystem locking.
112  * These tokens are initially used to lockup both global and individual
113  * operations.
114  *
115  * Once individual structures get their own locks these tokens are used
116  * only to protect global lists & other variables and to interlock
117  * allocations and teardowns and such.
118  *
119  * The UP initializer causes token acquisition to also acquire the MP lock
120  * for maximum compatibility.  The feature may be enabled and disabled at
121  * any time, the MP state is copied to the tokref when the token is acquired
122  * and will not race against sysctl changes.
123  */
124 struct lwkt_token mp_token = LWKT_TOKEN_INITIALIZER(mp_token);
125 struct lwkt_token pmap_token = LWKT_TOKEN_INITIALIZER(pmap_token);
126 struct lwkt_token dev_token = LWKT_TOKEN_INITIALIZER(dev_token);
127 struct lwkt_token vm_token = LWKT_TOKEN_INITIALIZER(vm_token);
128 struct lwkt_token vmspace_token = LWKT_TOKEN_INITIALIZER(vmspace_token);
129 struct lwkt_token kvm_token = LWKT_TOKEN_INITIALIZER(kvm_token);
130 struct lwkt_token proc_token = LWKT_TOKEN_INITIALIZER(proc_token);
131 struct lwkt_token tty_token = LWKT_TOKEN_INITIALIZER(tty_token);
132 struct lwkt_token vnode_token = LWKT_TOKEN_INITIALIZER(vnode_token);
133 struct lwkt_token vmobj_token = LWKT_TOKEN_INITIALIZER(vmobj_token);
134
135 static int lwkt_token_spin = 5;
136 SYSCTL_INT(_lwkt, OID_AUTO, token_spin, CTLFLAG_RW,
137     &lwkt_token_spin, 0, "Decontention spin loops");
138 static int lwkt_token_delay = 0;
139 SYSCTL_INT(_lwkt, OID_AUTO, token_delay, CTLFLAG_RW,
140     &lwkt_token_delay, 0, "Decontention spin delay in ns");
141
142 /*
143  * The collision count is bumped every time the LWKT scheduler fails
144  * to acquire needed tokens in addition to a normal lwkt_gettoken()
145  * stall.
146  */
147 SYSCTL_LONG(_lwkt, OID_AUTO, mp_collisions, CTLFLAG_RW,
148     &mp_token.t_collisions, 0, "Collision counter of mp_token");
149 SYSCTL_LONG(_lwkt, OID_AUTO, pmap_collisions, CTLFLAG_RW,
150     &pmap_token.t_collisions, 0, "Collision counter of pmap_token");
151 SYSCTL_LONG(_lwkt, OID_AUTO, dev_collisions, CTLFLAG_RW,
152     &dev_token.t_collisions, 0, "Collision counter of dev_token");
153 SYSCTL_LONG(_lwkt, OID_AUTO, vm_collisions, CTLFLAG_RW,
154     &vm_token.t_collisions, 0, "Collision counter of vm_token");
155 SYSCTL_LONG(_lwkt, OID_AUTO, vmspace_collisions, CTLFLAG_RW,
156     &vmspace_token.t_collisions, 0, "Collision counter of vmspace_token");
157 SYSCTL_LONG(_lwkt, OID_AUTO, kvm_collisions, CTLFLAG_RW,
158     &kvm_token.t_collisions, 0, "Collision counter of kvm_token");
159 SYSCTL_LONG(_lwkt, OID_AUTO, proc_collisions, CTLFLAG_RW,
160     &proc_token.t_collisions, 0, "Collision counter of proc_token");
161 SYSCTL_LONG(_lwkt, OID_AUTO, tty_collisions, CTLFLAG_RW,
162     &tty_token.t_collisions, 0, "Collision counter of tty_token");
163 SYSCTL_LONG(_lwkt, OID_AUTO, vnode_collisions, CTLFLAG_RW,
164     &vnode_token.t_collisions, 0, "Collision counter of vnode_token");
165
166 static int _lwkt_getalltokens_sorted(thread_t td);
167
168 #ifdef SMP
169 /*
170  * Acquire the initial mplock
171  *
172  * (low level boot only)
173  */
174 void
175 cpu_get_initial_mplock(void)
176 {
177         KKASSERT(mp_token.t_ref == NULL);
178         if (lwkt_trytoken(&mp_token) == FALSE)
179                 panic("cpu_get_initial_mplock");
180 }
181 #endif
182
183 /*
184  * Return a pool token given an address.  Use a prime number to reduce
185  * overlaps.
186  */
187 static __inline
188 lwkt_token_t
189 _lwkt_token_pool_lookup(void *ptr)
190 {
191         u_int i;
192
193         i = (u_int)(uintptr_t)ptr % LWKT_NUM_POOL_TOKENS;
194         return(&pool_tokens[i]);
195 }
196
197 /*
198  * Initialize a tokref_t prior to making it visible in the thread's
199  * token array.
200  */
201 static __inline
202 void
203 _lwkt_tokref_init(lwkt_tokref_t ref, lwkt_token_t tok, thread_t td)
204 {
205         ref->tr_tok = tok;
206         ref->tr_owner = td;
207 }
208
209 static
210 int
211 _lwkt_trytoken_spin(lwkt_token_t tok, lwkt_tokref_t ref)
212 {
213         int n;
214
215         for (n = 0; n < lwkt_token_spin; ++n) {
216                 if (tok->t_ref == NULL &&
217                     atomic_cmpset_ptr(&tok->t_ref, NULL, ref)) {
218                         return TRUE;
219                 }
220                 if (lwkt_token_delay) {
221                         tsc_delay(lwkt_token_delay);
222                 } else {
223                         cpu_lfence();
224                         cpu_pause();
225                 }
226         }
227         return FALSE;
228 }
229
230 static __inline
231 void
232 _lwkt_reltoken_spin(lwkt_token_t tok)
233 {
234         tok->t_ref = NULL;
235 }
236
237 #if 0
238 /*
239  * Helper function used by lwkt_getalltokens[_sorted]().
240  *
241  * Our attempt to acquire the token has failed.  To reduce cache coherency
242  * bandwidth we set our cpu bit in t_collmask then wait for a reasonable
243  * period of time for a hand-off from the current token owner.
244  */
245 static
246 int
247 _lwkt_trytoken_spin(lwkt_token_t tok, lwkt_tokref_t ref)
248 {
249         globaldata_t gd = mycpu;
250         cpumask_t mask;
251         int n;
252
253         /*
254          * Add our cpu to the collision mask and wait for the token to be
255          * handed off to us.
256          */
257         crit_enter();
258         atomic_set_cpumask(&tok->t_collmask, gd->gd_cpumask);
259         for (n = 0; n < lwkt_token_spin; ++n) {
260                 /*
261                  * Token was released before we set our collision bit.
262                  */
263                 if (tok->t_ref == NULL &&
264                     atomic_cmpset_ptr(&tok->t_ref, NULL, ref)) {
265                         KKASSERT((tok->t_collmask & gd->gd_cpumask) != 0);
266                         atomic_clear_cpumask(&tok->t_collmask, gd->gd_cpumask);
267                         crit_exit();
268                         return TRUE;
269                 }
270
271                 /*
272                  * Token was handed-off to us.
273                  */
274                 if (tok->t_ref == &gd->gd_handoff) {
275                         KKASSERT((tok->t_collmask & gd->gd_cpumask) == 0);
276                         tok->t_ref = ref;
277                         crit_exit();
278                         return TRUE;
279                 }
280                 if (lwkt_token_delay)
281                         tsc_delay(lwkt_token_delay);
282                 else
283                         cpu_pause();
284         }
285
286         /*
287          * We failed, attempt to clear our bit in the cpumask.  We may race
288          * someone handing-off to us.  If someone other than us cleared our
289          * cpu bit a handoff is incoming and we must wait for it.
290          */
291         for (;;) {
292                 mask = tok->t_collmask;
293                 cpu_ccfence();
294                 if (mask & gd->gd_cpumask) {
295                         if (atomic_cmpset_cpumask(&tok->t_collmask,
296                                                   mask,
297                                                   mask & ~gd->gd_cpumask)) {
298                                 crit_exit();
299                                 return FALSE;
300                         }
301                         continue;
302                 }
303                 if (tok->t_ref != &gd->gd_handoff) {
304                         cpu_pause();
305                         continue;
306                 }
307                 tok->t_ref = ref;
308                 crit_exit();
309                 return TRUE;
310         }
311 }
312
313 /*
314  * Release token with hand-off
315  */
316 static __inline
317 void
318 _lwkt_reltoken_spin(lwkt_token_t tok)
319 {
320         globaldata_t xgd;
321         cpumask_t sidemask;
322         cpumask_t mask;
323         int cpuid;
324
325         if (tok->t_collmask == 0) {
326                 tok->t_ref = NULL;
327                 return;
328         }
329
330         crit_enter();
331         sidemask = ~(mycpu->gd_cpumask - 1);    /* high bits >= xcpu */
332         for (;;) {
333                 mask = tok->t_collmask;
334                 cpu_ccfence();
335                 if (mask == 0) {
336                         tok->t_ref = NULL;
337                         break;
338                 }
339                 if (mask & sidemask)
340                         cpuid = BSFCPUMASK(mask & sidemask);
341                 else
342                         cpuid = BSFCPUMASK(mask);
343                 xgd = globaldata_find(cpuid);
344                 if (atomic_cmpset_cpumask(&tok->t_collmask, mask,
345                                           mask & ~CPUMASK(cpuid))) {
346                         tok->t_ref = &xgd->gd_handoff;
347                         break;
348                 }
349         }
350         crit_exit();
351 }
352
353 #endif
354
355
356 /*
357  * Obtain all the tokens required by the specified thread on the current
358  * cpu, return 0 on failure and non-zero on success.  If a failure occurs
359  * any partially acquired tokens will be released prior to return.
360  *
361  * lwkt_getalltokens is called by the LWKT scheduler to acquire all
362  * tokens that the thread had acquired prior to going to sleep.
363  *
364  * If spinning is non-zero this function acquires the tokens in a particular
365  * order to deal with potential deadlocks.  We simply use address order for
366  * the case.
367  *
368  * Called from a critical section.
369  */
370 int
371 lwkt_getalltokens(thread_t td, int spinning)
372 {
373         lwkt_tokref_t scan;
374         lwkt_tokref_t ref;
375         lwkt_token_t tok;
376
377         if (spinning)
378                 return(_lwkt_getalltokens_sorted(td));
379
380         /*
381          * Acquire tokens in forward order, assign or validate tok->t_ref.
382          */
383         for (scan = &td->td_toks_base; scan < td->td_toks_stop; ++scan) {
384                 tok = scan->tr_tok;
385                 for (;;) {
386                         /*
387                          * Try to acquire the token if we do not already have
388                          * it.
389                          *
390                          * NOTE: If atomic_cmpset_ptr() fails we have to
391                          *       loop and try again.  It just means we
392                          *       lost a cpu race.
393                          */
394                         ref = tok->t_ref;
395                         if (ref == NULL) {
396                                 if (atomic_cmpset_ptr(&tok->t_ref, NULL,scan))
397                                         break;
398                                 continue;
399                         }
400
401                         /*
402                          * Someone holds the token.
403                          *
404                          * Test if ref is already recursively held by this
405                          * thread.  We cannot safely dereference tok->t_ref
406                          * (it might belong to another thread and is thus
407                          * unstable), but we don't have to. We can simply
408                          * range-check it.
409                          */
410                         if (ref >= &td->td_toks_base && ref < td->td_toks_stop)
411                                 break;
412
413                         /*
414                          * Try hard to acquire this token before giving up
415                          * and releasing the whole lot.
416                          */
417                         if (_lwkt_trytoken_spin(tok, scan))
418                                 break;
419                         if (lwkt_sched_debug)
420                                 kprintf("toka %p %s\n", tok, tok->t_desc);
421
422                         /*
423                          * Otherwise we failed to acquire all the tokens.
424                          * Release whatever we did get.
425                          */
426                         td->td_wmesg = tok->t_desc;
427                         atomic_add_long(&tok->t_collisions, 1);
428                         lwkt_relalltokens(td);
429
430                         return(FALSE);
431                 }
432
433         }
434         return (TRUE);
435 }
436
437 /*
438  * Release all tokens owned by the specified thread on the current cpu.
439  *
440  * This code is really simple.  Even in cases where we own all the tokens
441  * note that t_ref may not match the scan for recursively held tokens which
442  * are held deeper in the stack, or for the case where a lwkt_getalltokens()
443  * failed.
444  *
445  * Tokens are released in reverse order to reduce chasing race failures.
446  * 
447  * Called from a critical section.
448  */
449 void
450 lwkt_relalltokens(thread_t td)
451 {
452         lwkt_tokref_t scan;
453         lwkt_token_t tok;
454
455         for (scan = td->td_toks_stop - 1; scan >= &td->td_toks_base; --scan) {
456         /*for (scan = &td->td_toks_base; scan < td->td_toks_stop; ++scan) {*/
457                 tok = scan->tr_tok;
458                 if (tok->t_ref == scan)
459                         _lwkt_reltoken_spin(tok);
460         }
461 }
462
463 /*
464  * This is the decontention version of lwkt_getalltokens().  The tokens are
465  * acquired in address-sorted order to deal with any deadlocks.  Ultimately
466  * token failures will spin into the scheduler and get here.
467  *
468  * In addition, to reduce hardware cache coherency contention monitor/mwait
469  * is interlocked with gd->gd_reqflags and RQF_SPINNING.  Other cores which
470  * release a contended token will clear RQF_SPINNING and cause the mwait
471  * to resume.  Any interrupt will also generally set RQF_* flags and cause
472  * mwait to resume (or be a NOP in the first place).
473  *
474  * This code is required to set up RQF_SPINNING in case of failure.  The
475  * caller may call monitor/mwait on gd->gd_reqflags on failure.  We do NOT
476  * want to call mwait here, and doubly so while we are holding tokens.
477  *
478  * Called from critical section
479  */
480 static
481 int
482 _lwkt_getalltokens_sorted(thread_t td)
483 {
484         /*globaldata_t gd = td->td_gd;*/
485         lwkt_tokref_t sort_array[LWKT_MAXTOKENS];
486         lwkt_tokref_t scan;
487         lwkt_tokref_t ref;
488         lwkt_token_t tok;
489         int i;
490         int j;
491         int n;
492
493         /*
494          * Sort the token array.  Yah yah, I know this isn't fun.
495          *
496          * NOTE: Recursively acquired tokens are ordered the same as in the
497          *       td_toks_array so we can always get the earliest one first.
498          */
499         i = 0;
500         scan = &td->td_toks_base;
501         while (scan < td->td_toks_stop) {
502                 for (j = 0; j < i; ++j) {
503                         if (scan->tr_tok < sort_array[j]->tr_tok)
504                                 break;
505                 }
506                 if (j != i) {
507                         bcopy(sort_array + j, sort_array + j + 1,
508                               (i - j) * sizeof(lwkt_tokref_t));
509                 }
510                 sort_array[j] = scan;
511                 ++scan;
512                 ++i;
513         }
514         n = i;
515
516         /*
517          * Acquire tokens in forward order, assign or validate tok->t_ref.
518          */
519         for (i = 0; i < n; ++i) {
520                 scan = sort_array[i];
521                 tok = scan->tr_tok;
522                 for (;;) {
523                         /*
524                          * Try to acquire the token if we do not already have
525                          * it.
526                          *
527                          * NOTE: If atomic_cmpset_ptr() fails we have to
528                          *       loop and try again.  It just means we
529                          *       lost a cpu race.
530                          */
531                         ref = tok->t_ref;
532                         if (ref == NULL) {
533                                 if (atomic_cmpset_ptr(&tok->t_ref, NULL, scan))
534                                         break;
535                                 continue;
536                         }
537
538                         /*
539                          * Someone holds the token.
540                          *
541                          * Test if ref is already recursively held by this
542                          * thread.  We cannot safely dereference tok->t_ref
543                          * (it might belong to another thread and is thus
544                          * unstable), but we don't have to. We can simply
545                          * range-check it.
546                          */
547                         if (ref >= &td->td_toks_base && ref < td->td_toks_stop)
548                                 break;
549
550                         /*
551                          * Try hard to acquire this token before giving up
552                          * and releasing the whole lot.
553                          */
554                         if (_lwkt_trytoken_spin(tok, scan))
555                                 break;
556                         if (lwkt_sched_debug)
557                                 kprintf("tokb %p %s\n", tok, tok->t_desc);
558
559                         /*
560                          * Tokens are released in reverse order to reduce
561                          * chasing race failures.
562                          */
563                         td->td_wmesg = tok->t_desc;
564                         atomic_add_long(&tok->t_collisions, 1);
565
566                         for (j = i - 1; j >= 0; --j) {
567                         /*for (j = 0; j < i; ++j) {*/
568                                 scan = sort_array[j];
569                                 tok = scan->tr_tok;
570                                 if (tok->t_ref == scan)
571                                         _lwkt_reltoken_spin(tok);
572                         }
573                         return (FALSE);
574                 }
575         }
576
577         /*
578          * We were successful, there is no need for another core to signal
579          * us.
580          */
581 #if 0
582         atomic_clear_int(&gd->gd_reqflags, RQF_SPINNING);
583 #endif
584         return (TRUE);
585 }
586
587 /*
588  * Token acquisition helper function.  The caller must have already
589  * made nref visible by adjusting td_toks_stop and will be responsible
590  * for the disposition of nref on either success or failure.
591  *
592  * When acquiring tokens recursively we want tok->t_ref to point to
593  * the outer (first) acquisition so it gets cleared only on the last
594  * release.
595  */
596 static __inline
597 int
598 _lwkt_trytokref2(lwkt_tokref_t nref, thread_t td, int blocking)
599 {
600         lwkt_token_t tok;
601         lwkt_tokref_t ref;
602
603         /*
604          * Make sure the compiler does not reorder prior instructions
605          * beyond this demark.
606          */
607         cpu_ccfence();
608
609         /*
610          * Attempt to gain ownership
611          */
612         tok = nref->tr_tok;
613         for (;;) {
614                 /*
615                  * Try to acquire the token if we do not already have
616                  * it.  This is not allowed if we are in a hard code
617                  * section (because it 'might' have blocked).
618                  */
619                 ref = tok->t_ref;
620                 if (ref == NULL) {
621                         KASSERT((blocking == 0 ||
622                                 td->td_gd->gd_intr_nesting_level == 0 ||
623                                 panic_cpu_gd == mycpu),
624                                 ("Attempt to acquire token %p not already "
625                                  "held in hard code section", tok));
626
627                         /*
628                          * NOTE: If atomic_cmpset_ptr() fails we have to
629                          *       loop and try again.  It just means we
630                          *       lost a cpu race.
631                          */
632                         if (atomic_cmpset_ptr(&tok->t_ref, NULL, nref))
633                                 return (TRUE);
634                         continue;
635                 }
636
637                 /*
638                  * Test if ref is already recursively held by this
639                  * thread.  We cannot safely dereference tok->t_ref
640                  * (it might belong to another thread and is thus
641                  * unstable), but we don't have to. We can simply
642                  * range-check it.
643                  *
644                  * It is ok to acquire a token that is already held
645                  * by the current thread when in a hard code section.
646                  */
647                 if (ref >= &td->td_toks_base && ref < td->td_toks_stop)
648                         return(TRUE);
649
650                 /*
651                  * Spin generously.  This is preferable to just switching
652                  * away unconditionally.
653                  */
654                 if (_lwkt_trytoken_spin(tok, nref))
655                         return(TRUE);
656
657                 /*
658                  * Otherwise we failed, and it is not ok to attempt to
659                  * acquire a token in a hard code section.
660                  */
661                 KASSERT((blocking == 0 ||
662                         td->td_gd->gd_intr_nesting_level == 0),
663                         ("Attempt to acquire token %p not already "
664                          "held in hard code section", tok));
665
666                 return(FALSE);
667         }
668 }
669
670 /*
671  * Get a serializing token.  This routine can block.
672  */
673 void
674 lwkt_gettoken(lwkt_token_t tok)
675 {
676         thread_t td = curthread;
677         lwkt_tokref_t ref;
678
679         ref = td->td_toks_stop;
680         KKASSERT(ref < &td->td_toks_end);
681         ++td->td_toks_stop;
682         cpu_ccfence();
683         _lwkt_tokref_init(ref, tok, td);
684
685         if (_lwkt_trytokref2(ref, td, 1) == FALSE) {
686                 /*
687                  * Give up running if we can't acquire the token right now.
688                  *
689                  * Since the tokref is already active the scheduler now
690                  * takes care of acquisition, so we need only call
691                  * lwkt_switch().
692                  *
693                  * Since we failed this was not a recursive token so upon
694                  * return tr_tok->t_ref should be assigned to this specific
695                  * ref.
696                  */
697                 td->td_wmesg = tok->t_desc;
698                 atomic_add_long(&tok->t_collisions, 1);
699                 logtoken(fail, ref);
700                 lwkt_switch();
701                 logtoken(succ, ref);
702                 KKASSERT(tok->t_ref == ref);
703         }
704 }
705
706 void
707 lwkt_gettoken_hard(lwkt_token_t tok)
708 {
709         thread_t td = curthread;
710         lwkt_tokref_t ref;
711
712         ref = td->td_toks_stop;
713         KKASSERT(ref < &td->td_toks_end);
714         ++td->td_toks_stop;
715         cpu_ccfence();
716         _lwkt_tokref_init(ref, tok, td);
717
718         if (_lwkt_trytokref2(ref, td, 1) == FALSE) {
719                 /*
720                  * Give up running if we can't acquire the token right now.
721                  *
722                  * Since the tokref is already active the scheduler now
723                  * takes care of acquisition, so we need only call
724                  * lwkt_switch().
725                  *
726                  * Since we failed this was not a recursive token so upon
727                  * return tr_tok->t_ref should be assigned to this specific
728                  * ref.
729                  */
730                 td->td_wmesg = tok->t_desc;
731                 atomic_add_long(&tok->t_collisions, 1);
732                 logtoken(fail, ref);
733                 lwkt_switch();
734                 logtoken(succ, ref);
735                 KKASSERT(tok->t_ref == ref);
736         }
737         crit_enter_hard_gd(td->td_gd);
738 }
739
740 lwkt_token_t
741 lwkt_getpooltoken(void *ptr)
742 {
743         thread_t td = curthread;
744         lwkt_token_t tok;
745         lwkt_tokref_t ref;
746
747         tok = _lwkt_token_pool_lookup(ptr);
748         ref = td->td_toks_stop;
749         KKASSERT(ref < &td->td_toks_end);
750         ++td->td_toks_stop;
751         cpu_ccfence();
752         _lwkt_tokref_init(ref, tok, td);
753
754         if (_lwkt_trytokref2(ref, td, 1) == FALSE) {
755                 /*
756                  * Give up running if we can't acquire the token right now.
757                  *
758                  * Since the tokref is already active the scheduler now
759                  * takes care of acquisition, so we need only call
760                  * lwkt_switch().
761                  *
762                  * Since we failed this was not a recursive token so upon
763                  * return tr_tok->t_ref should be assigned to this specific
764                  * ref.
765                  */
766                 td->td_wmesg = tok->t_desc;
767                 atomic_add_long(&tok->t_collisions, 1);
768                 logtoken(fail, ref);
769                 lwkt_switch();
770                 logtoken(succ, ref);
771                 KKASSERT(tok->t_ref == ref);
772         }
773         return(tok);
774 }
775
776 /*
777  * Attempt to acquire a token, return TRUE on success, FALSE on failure.
778  */
779 int
780 lwkt_trytoken(lwkt_token_t tok)
781 {
782         thread_t td = curthread;
783         lwkt_tokref_t ref;
784
785         ref = td->td_toks_stop;
786         KKASSERT(ref < &td->td_toks_end);
787         ++td->td_toks_stop;
788         cpu_ccfence();
789         _lwkt_tokref_init(ref, tok, td);
790
791         if (_lwkt_trytokref2(ref, td, 0) == FALSE) {
792                 /*
793                  * Cleanup, deactivate the failed token.
794                  */
795                 cpu_ccfence();
796                 --td->td_toks_stop;
797                 return (FALSE);
798         }
799         return (TRUE);
800 }
801
802 /*
803  * Release a serializing token.
804  *
805  * WARNING!  All tokens must be released in reverse order.  This will be
806  *           asserted.
807  */
808 void
809 lwkt_reltoken(lwkt_token_t tok)
810 {
811         thread_t td = curthread;
812         lwkt_tokref_t ref;
813
814         /*
815          * Remove ref from thread token list and assert that it matches
816          * the token passed in.  Tokens must be released in reverse order.
817          */
818         ref = td->td_toks_stop - 1;
819         KKASSERT(ref >= &td->td_toks_base && ref->tr_tok == tok);
820
821         /*
822          * Only clear the token if it matches ref.  If ref was a recursively
823          * acquired token it may not match.  Then adjust td_toks_stop.
824          *
825          * Some comparisons must be run prior to adjusting td_toks_stop
826          * to avoid racing against a fast interrupt/ ipi which tries to
827          * acquire a token.
828          *
829          * We must also be absolutely sure that the compiler does not
830          * reorder the clearing of t_ref and the adjustment of td_toks_stop,
831          * or reorder the adjustment of td_toks_stop against the conditional.
832          *
833          * NOTE: The mplock is a token also so sequencing is a bit complex.
834          */
835         if (tok->t_ref == ref)
836                 _lwkt_reltoken_spin(tok);
837         cpu_sfence();
838         cpu_ccfence();
839         td->td_toks_stop = ref;
840         cpu_ccfence();
841         KKASSERT(tok->t_ref != ref);
842 }
843
844 void
845 lwkt_reltoken_hard(lwkt_token_t tok)
846 {
847         lwkt_reltoken(tok);
848         crit_exit_hard();
849 }
850
851 /*
852  * It is faster for users of lwkt_getpooltoken() to use the returned
853  * token and just call lwkt_reltoken(), but for convenience we provide
854  * this function which looks the token up based on the ident.
855  */
856 void
857 lwkt_relpooltoken(void *ptr)
858 {
859         lwkt_token_t tok = _lwkt_token_pool_lookup(ptr);
860         lwkt_reltoken(tok);
861 }
862
863 /*
864  * Return a count of the number of token refs the thread has to the
865  * specified token, whether it currently owns the token or not.
866  */
867 int
868 lwkt_cnttoken(lwkt_token_t tok, thread_t td)
869 {
870         lwkt_tokref_t scan;
871         int count = 0;
872
873         for (scan = &td->td_toks_base; scan < td->td_toks_stop; ++scan) {
874                 if (scan->tr_tok == tok)
875                         ++count;
876         }
877         return(count);
878 }
879
880
881 /*
882  * Pool tokens are used to provide a type-stable serializing token
883  * pointer that does not race against disappearing data structures.
884  *
885  * This routine is called in early boot just after we setup the BSP's
886  * globaldata structure.
887  */
888 void
889 lwkt_token_pool_init(void)
890 {
891         int i;
892
893         for (i = 0; i < LWKT_NUM_POOL_TOKENS; ++i)
894                 lwkt_token_init(&pool_tokens[i], "pool");
895 }
896
897 lwkt_token_t
898 lwkt_token_pool_lookup(void *ptr)
899 {
900         return (_lwkt_token_pool_lookup(ptr));
901 }
902
903 /*
904  * Initialize a token.  
905  */
906 void
907 lwkt_token_init(lwkt_token_t tok, const char *desc)
908 {
909         tok->t_ref = NULL;
910         tok->t_collisions = 0;
911         tok->t_collmask = 0;
912         tok->t_desc = desc;
913 }
914
915 void
916 lwkt_token_uninit(lwkt_token_t tok)
917 {
918         /* empty */
919 }
920
921 /*
922  * Exchange the two most recent tokens on the tokref stack.  This allows
923  * you to release a token out of order.
924  *
925  * We have to be careful about the case where the top two tokens are
926  * the same token.  In this case tok->t_ref will point to the deeper
927  * ref and must remain pointing to the deeper ref.  If we were to swap
928  * it the first release would clear the token even though a second
929  * ref is still present.
930  */
931 void
932 lwkt_token_swap(void)
933 {
934         lwkt_tokref_t ref1, ref2;
935         lwkt_token_t tok1, tok2;
936         thread_t td = curthread;
937
938         crit_enter();
939
940         ref1 = td->td_toks_stop - 1;
941         ref2 = td->td_toks_stop - 2;
942         KKASSERT(ref1 > &td->td_toks_base);
943         KKASSERT(ref2 > &td->td_toks_base);
944
945         tok1 = ref1->tr_tok;
946         tok2 = ref2->tr_tok;
947         if (tok1 != tok2) {
948                 ref1->tr_tok = tok2;
949                 ref2->tr_tok = tok1;
950                 if (tok1->t_ref == ref1)
951                         tok1->t_ref = ref2;
952                 if (tok2->t_ref == ref2)
953                         tok2->t_ref = ref1;
954         }
955
956         crit_exit();
957 }
958
959 #if 0
960 int
961 lwkt_token_is_stale(lwkt_tokref_t ref)
962 {
963         lwkt_token_t tok = ref->tr_tok;
964
965         KKASSERT(tok->t_owner == curthread && ref->tr_state == 1 &&
966                  tok->t_count > 0);
967
968         /* Token is not stale */
969         if (tok->t_lastowner == tok->t_owner)
970                 return (FALSE);
971
972         /*
973          * The token is stale. Reset to not stale so that the next call to
974          * lwkt_token_is_stale will return "not stale" unless the token
975          * was acquired in-between by another thread.
976          */
977         tok->t_lastowner = tok->t_owner;
978         return (TRUE);
979 }
980 #endif