2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
34 * $DragonFly: src/sys/kern/lwkt_token.c,v 1.23 2005/09/12 21:48:22 dillon Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/rtprio.h>
44 #include <sys/queue.h>
45 #include <sys/thread2.h>
46 #include <sys/sysctl.h>
48 #include <sys/kthread.h>
49 #include <machine/cpu.h>
54 #include <vm/vm_param.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_pager.h>
60 #include <vm/vm_extern.h>
61 #include <vm/vm_zone.h>
63 #include <machine/stdarg.h>
64 #include <machine/ipl.h>
65 #include <machine/smp.h>
67 #define THREAD_STACK (UPAGES * PAGE_SIZE)
71 #include <sys/stdint.h>
72 #include <libcaps/thread.h>
73 #include <sys/thread.h>
74 #include <sys/msgport.h>
75 #include <sys/errno.h>
76 #include <libcaps/globaldata.h>
77 #include <machine/cpufunc.h>
78 #include <sys/thread2.h>
79 #include <sys/msgport2.h>
83 #include <machine/lock.h>
84 #include <machine/cpu.h>
88 #ifndef LWKT_NUM_POOL_TOKENS
89 #define LWKT_NUM_POOL_TOKENS 1024 /* power of 2 */
91 #define LWKT_MASK_POOL_TOKENS (LWKT_NUM_POOL_TOKENS - 1)
94 static int token_debug = 0;
98 static void lwkt_reqtoken_remote(void *data);
101 static lwkt_token pool_tokens[LWKT_NUM_POOL_TOKENS];
103 #define TOKEN_STRING "REF=%p TOK=%p TD=%p"
104 #define CONTENDED_STRING "REF=%p TOK=%p TD=%p (contention started)"
105 #define UNCONTENDED_STRING "REF=%p TOK=%p TD=%p (contention stopped)"
106 #if !defined(KTR_TOKENS)
107 #define KTR_TOKENS KTR_ALL
110 KTR_INFO_MASTER(tokens);
111 KTR_INFO(KTR_TOKENS, tokens, try, 0, TOKEN_STRING, sizeof(void *) * 3);
112 KTR_INFO(KTR_TOKENS, tokens, get, 1, TOKEN_STRING, sizeof(void *) * 3);
113 KTR_INFO(KTR_TOKENS, tokens, release, 2, TOKEN_STRING, sizeof(void *) * 3);
115 KTR_INFO(KTR_TOKENS, tokens, remote, 3, TOKEN_STRING, sizeof(void *) * 3);
116 KTR_INFO(KTR_TOKENS, tokens, reqremote, 4, TOKEN_STRING, sizeof(void *) * 3);
117 KTR_INFO(KTR_TOKENS, tokens, reqfail, 5, TOKEN_STRING, sizeof(void *) * 3);
118 KTR_INFO(KTR_TOKENS, tokens, drain, 6, TOKEN_STRING, sizeof(void *) * 3);
119 KTR_INFO(KTR_TOKENS, tokens, contention_start, 7, CONTENDED_STRING, sizeof(void *) * 3);
120 KTR_INFO(KTR_TOKENS, tokens, contention_stop, 7, UNCONTENDED_STRING, sizeof(void *) * 3);
123 #define logtoken(name, ref) \
124 KTR_LOG(tokens_ ## name, ref, ref->tr_tok, curthread)
129 SYSCTL_INT(_lwkt, OID_AUTO, token_debug, CTLFLAG_RW, &token_debug, 0, "");
137 * Determine if we own all the tokens in the token reference list.
138 * Return 1 on success, 0 on failure.
140 * As a side effect, queue requests for tokens we want which are owned
141 * by other cpus. The magic number is used to communicate when the
142 * target cpu has processed the request. Note, however, that the
143 * target cpu may not be able to assign the token to us which is why
144 * the scheduler must spin.
147 lwkt_chktokens(thread_t td)
149 globaldata_t gd = td->td_gd; /* mycpu */
156 KKASSERT(gd->gd_curthread->td_pri >= TDPRI_CRIT);
157 for (refs = td->td_toks; refs; refs = refs->tr_next) {
159 if ((dgd = tok->t_cpu) != gd) {
160 cpu_ccfence(); /* don't let the compiler reload tok->t_cpu */
163 if ((refs->tr_flags & LWKT_TOKREF_CONTENDED) == 0) {
164 refs->tr_flags |= LWKT_TOKREF_CONTENDED;
165 /* mark token contended */
166 logtoken(contention_start, refs);
171 * Queue a request to the target cpu, exit the loop early if
172 * we are unable to queue the IPI message. The magic number
173 * flags whether we have a pending ipi request queued or not.
174 * It can be set from MAGIC2 to MAGIC1 by a remote cpu but can
175 * only be set from MAGIC1 to MAGIC2 by our cpu.
177 magic = refs->tr_magic;
179 if (magic == LWKT_TOKREF_MAGIC1) {
180 refs->tr_magic = LWKT_TOKREF_MAGIC2; /* MP synched slowreq*/
182 tok->t_reqcpu = gd; /* MP unsynchronized 'fast' req */
184 logtoken(reqremote, refs);
186 if (lwkt_send_ipiq_nowait(dgd, lwkt_reqtoken_remote, refs)) {
188 refs->tr_magic = LWKT_TOKREF_MAGIC1;
190 logtoken(reqfail, refs);
193 } else if (magic != LWKT_TOKREF_MAGIC2) {
194 panic("lwkt_chktoken(): token ref %p tok %p bad magic %08x\n",
195 refs, refs->tr_tok, magic);
199 if (refs->tr_flags & LWKT_TOKREF_CONTENDED) {
200 /* mark token uncontended */
201 refs->tr_flags &= ~LWKT_TOKREF_CONTENDED;
202 logtoken(contention_stop, refs);
212 * Check if we already own the token. Return 1 on success, 0 on failure.
215 lwkt_havetoken(lwkt_token_t tok)
217 globaldata_t gd = mycpu;
218 thread_t td = gd->gd_curthread;
221 for (ref = td->td_toks; ref; ref = ref->tr_next) {
222 if (ref->tr_tok == tok)
229 lwkt_havetokref(lwkt_tokref_t xref)
231 globaldata_t gd = mycpu;
232 thread_t td = gd->gd_curthread;
235 for (ref = td->td_toks; ref; ref = ref->tr_next) {
245 * Returns 1 if it is ok to give a token away, 0 if it is not.
248 lwkt_oktogiveaway_token(lwkt_token_t tok)
250 globaldata_t gd = mycpu;
254 for (td = gd->gd_curthread; td; td = td->td_preempted) {
255 for (ref = td->td_toks; ref; ref = ref->tr_next) {
256 if (ref->tr_tok == tok)
266 * Acquire a serializing token
271 _lwkt_gettokref(lwkt_tokref_t ref)
278 gd = mycpu; /* our cpu */
279 KKASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC1);
280 KKASSERT(gd->gd_intr_nesting_level == 0);
281 td = gd->gd_curthread; /* our thread */
284 * Link the request into our thread's list. This interlocks against
285 * remote requests from other cpus, prevents the token from being
286 * given away if our cpu already owns it, and interlocks against
287 * preempting threads which may want the token. This also allows us to
288 * avoid using a critical section.
290 ref->tr_next = td->td_toks;
291 cpu_ccfence(); /* prevent compiler reordering */
296 * If we are preempting another thread which owns the token we have to
297 * yield to get out from the preemption because we cannot obtain a token
298 * owned by the thread we are preempting.
300 * If we are preempting another thread and do not own the token,
301 * we must yield to get out from the preemption because we cannot
302 * safely call lwkt_drain_token_requests() and give away tokens
303 * that the thread we preempted might own.
305 * The act of yielding performs a thread switch and we will own all
306 * tokens on our td_toks list when it switches back to us and returns.
308 if (td->td_preempted) {
309 while ((td = td->td_preempted) != NULL) {
310 for (scan = td->td_toks; scan; scan = scan->tr_next) {
311 if (scan->tr_tok == tok) {
313 KKASSERT(tok->t_cpu == gd);
318 td = gd->gd_curthread; /* our thread, again */
319 if (tok->t_cpu != gd) {
321 KKASSERT(tok->t_cpu == gd);
327 * If we are not preempting another thread we can safely give
328 * away tokens while we busy loop.
330 * Currently tokens acquired by mainline threads are not assumed to
331 * break the big giant lock, so we have to spin when acquiring them.
332 * It would be nice to be able to yield here instead but we could
333 * run up against unexpected problems with e.g. softupdates or other
334 * subsystems. It's safest to spin. XXX
336 * XXX we should use some sort of time-slot synchronization and delay
337 * for these giveaways (with each cpu given a different timeslot) to
341 if (tok->t_cpu != gd) {
342 #define MAKE_TOKENS_SPIN
343 #if defined(MAKE_TOKENS_SPIN)
347 while (lwkt_chktokens(td) == 0) {
349 lwkt_drain_token_requests();
352 printf("CHKTOKEN looping on cpu %d\n", gd->gd_cpuid);
355 panic("CHKTOKEN looping on cpu %d", gd->gd_cpuid);
364 KKASSERT(tok->t_cpu == gd);
371 * Attempt to acquire a serializing token
375 _lwkt_trytokref(lwkt_tokref_t ref)
381 gd = mycpu; /* our cpu */
382 KKASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC1);
383 KKASSERT(gd->gd_intr_nesting_level == 0);
384 td = gd->gd_curthread; /* our thread */
387 * Link the request into our thread's list. This interlocks against
388 * remote requests from other cpus and prevents the token from being
389 * given away if our cpu already owns it. This also allows us to
390 * avoid using a critical section.
392 * Force a panic to occur if chktokens is called while the reference
393 * is linked to td_toks but before we have resolved whether we can
394 * keep it. chktokens should never be called on our ref list
397 ref->tr_magic = LWKT_TOKREF_MAGIC3;
398 ref->tr_next = td->td_toks;
399 cpu_ccfence(); /* prevent compiler reordering */
403 * If our cpu does not own the token then stop now.
405 * Otherwise make sure the token is not held by a thread we are
406 * preempting. If it is, stop.
410 if (tok->t_cpu != gd) {
411 td->td_toks = ref->tr_next; /* remove ref */
412 ref->tr_magic = LWKT_TOKREF_MAGIC1;
416 if (td->td_preempted) {
417 while ((td = td->td_preempted) != NULL) {
419 for (scan = td->td_toks; scan; scan = scan->tr_next) {
420 if (scan->tr_tok == tok) {
421 td = gd->gd_curthread; /* our thread */
422 td->td_toks = ref->tr_next; /* remove ref */
423 ref->tr_magic = LWKT_TOKREF_MAGIC1;
431 * We own the token, legitimize the reference.
433 ref->tr_magic = LWKT_TOKREF_MAGIC1;
434 /* 'td' variable no longer valid */
439 lwkt_gettoken(lwkt_tokref_t ref, lwkt_token_t tok)
441 lwkt_tokref_init(ref, tok);
443 _lwkt_gettokref(ref);
447 lwkt_gettokref(lwkt_tokref_t ref)
450 _lwkt_gettokref(ref);
454 lwkt_trytoken(lwkt_tokref_t ref, lwkt_token_t tok)
456 lwkt_tokref_init(ref, tok);
458 return(_lwkt_trytokref(ref));
462 lwkt_trytokref(lwkt_tokref_t ref)
465 return(_lwkt_trytokref(ref));
469 * Release a serializing token
472 lwkt_reltoken(lwkt_tokref *_ref)
482 logtoken(release, _ref);
484 * Guard check and stack check (if in the same stack page). We must
485 * also wait for any action pending on remote cpus which we do by
486 * checking the magic number and yielding in a loop.
490 if ((((intptr_t)ref ^ (intptr_t)&_ref) & ~(intptr_t)PAGE_MASK) == 0)
491 KKASSERT((char *)ref > (char *)&_ref);
492 KKASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC1 ||
493 ref->tr_magic == LWKT_TOKREF_MAGIC2);
498 td = gd->gd_curthread;
500 KKASSERT(tok->t_cpu == gd);
501 KKASSERT(gd->gd_intr_nesting_level == 0);
504 * We can only give away the token if we aren't holding it recursively.
505 * Also use the opportunity to locate the link field for the token.
507 * We do not have to scan preempted threads since by definition we cannot
508 * be holding any token held by a thread we are preempting.
511 for (pref = &td->td_toks; (ref = *pref) != _ref; pref = &ref->tr_next) {
512 KKASSERT(ref != NULL);
513 if (ref->tr_tok == tok)
516 for (scan = ref->tr_next; scan; scan = scan->tr_next) {
517 if (scan->tr_tok == tok)
522 * Give the token away (if we can) before removing the interlock. Once
523 * the interlock is removed, the token can be given away by an IPI.
526 tok->t_cpu = tok->t_reqcpu;
527 KKASSERT(*pref == ref);
528 *pref = ref->tr_next;
531 * If we had gotten the token opportunistically and it still happens to
532 * be queued to a target cpu, we have to wait for the target cpu
533 * to finish processing it. This does not happen very often and does
534 * not need to be optimal.
536 while (ref->tr_magic == LWKT_TOKREF_MAGIC2) {
537 #if defined(MAKE_TOKENS_SPIN)
548 KKASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC1);
552 * Pool tokens are used to provide a type-stable serializing token
553 * pointer that does not race against disappearing data structures.
555 * This routine is called in early boot just after we setup the BSP's
556 * globaldata structure.
559 lwkt_token_pool_init(void)
563 for (i = 0; i < LWKT_NUM_POOL_TOKENS; ++i)
564 lwkt_token_init(&pool_tokens[i]);
568 lwkt_token_pool_get(void *ptraddr)
572 i = ((int)(intptr_t)ptraddr >> 2) ^ ((int)(intptr_t)ptraddr >> 12);
573 return(&pool_tokens[i & LWKT_MASK_POOL_TOKENS]);
579 * This is the receiving side of a remote IPI requesting a token. If we
580 * cannot immediately hand the token off to another cpu we queue it.
582 * NOTE! we 'own' the ref structure, but we only 'own' the token if
586 lwkt_reqtoken_remote(void *data)
588 lwkt_tokref_t ref = data;
589 globaldata_t gd = mycpu;
590 lwkt_token_t tok = ref->tr_tok;
592 logtoken(remote, ref);
594 * We do not have to queue the token if we can give it away
595 * immediately. Otherwise we queue it to our globaldata structure.
597 KASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC2, ("ref %p token %p magic %08x", ref, ref->tr_tok, ref->tr_magic));
598 if (lwkt_oktogiveaway_token(tok)) {
599 if (tok->t_cpu == gd)
600 tok->t_cpu = ref->tr_reqgd;
601 cpu_ccfence(); /* prevent compiler reordering */
602 ref->tr_magic = LWKT_TOKREF_MAGIC1;
604 ref->tr_gdreqnext = gd->gd_tokreqbase;
605 gd->gd_tokreqbase = ref;
610 * Must be called from a critical section. Satisfy all remote token
611 * requests that are pending on our globaldata structure. The request
612 * does not have to be satisfied with a successful change of ownership
613 * but we do have to acknowledge that we have completed processing the
614 * request by setting the magic number back to MAGIC1.
616 * NOTE! we 'own' the ref structure, but we only 'own' the token if
620 lwkt_drain_token_requests(void)
622 globaldata_t gd = mycpu;
625 KKASSERT(gd->gd_curthread->td_pri >= TDPRI_CRIT);
626 while ((ref = gd->gd_tokreqbase) != NULL) {
627 gd->gd_tokreqbase = ref->tr_gdreqnext;
628 logtoken(drain, ref);
629 KKASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC2);
630 if (ref->tr_tok->t_cpu == gd)
631 ref->tr_tok->t_cpu = ref->tr_reqgd;
632 cpu_ccfence(); /* prevent compiler reordering */
633 ref->tr_magic = LWKT_TOKREF_MAGIC1;
640 * Initialize the owner and release-to cpu to the current cpu
641 * and reset the generation count.
644 lwkt_token_init(lwkt_token_t tok)
646 tok->t_cpu = tok->t_reqcpu = mycpu;
650 lwkt_token_uninit(lwkt_token_t tok)