Add strnvis, which is orthogonal to strvisx by bounding dst, not src.
[dragonfly/netmp.git] / sys / kern / lwkt_token.c
blob04f84c0a5d59305ab22bf83c4cc0c0f64ae29340
1 /*
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 *
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.
34 * $DragonFly: src/sys/kern/lwkt_token.c,v 1.22 2005/07/07 20:28:26 hmp Exp $
37 #ifdef _KERNEL
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/rtprio.h>
44 #include <sys/queue.h>
45 #include <sys/thread2.h>
46 #include <sys/sysctl.h>
47 #include <sys/ktr.h>
48 #include <sys/kthread.h>
49 #include <machine/cpu.h>
50 #include <sys/lock.h>
51 #include <sys/caps.h>
53 #include <vm/vm.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)
69 #else
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>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #include <machine/lock.h>
84 #include <machine/cpu.h>
86 #endif
88 #define MAKE_TOKENS_SPIN
89 /* #define MAKE_TOKENS_YIELD */
91 #ifndef LWKT_NUM_POOL_TOKENS
92 #define LWKT_NUM_POOL_TOKENS 1024 /* power of 2 */
93 #endif
94 #define LWKT_MASK_POOL_TOKENS (LWKT_NUM_POOL_TOKENS - 1)
96 #ifdef INVARIANTS
97 static int token_debug = 0;
98 #endif
100 #ifdef SMP
101 static void lwkt_reqtoken_remote(void *data);
102 #endif
104 static lwkt_token pool_tokens[LWKT_NUM_POOL_TOKENS];
106 #define TOKEN_STRING "REF=%p TOK=%p TD=%p"
107 #define CONTENDED_STRING "REF=%p TOK=%p TD=%p (contention started)"
108 #define UNCONTENDED_STRING "REF=%p TOK=%p TD=%p (contention stopped)"
109 #if !defined(KTR_TOKENS)
110 #define KTR_TOKENS KTR_ALL
111 #endif
113 KTR_INFO_MASTER(tokens);
114 KTR_INFO(KTR_TOKENS, tokens, try, 0, TOKEN_STRING, sizeof(void *) * 3);
115 KTR_INFO(KTR_TOKENS, tokens, get, 1, TOKEN_STRING, sizeof(void *) * 3);
116 KTR_INFO(KTR_TOKENS, tokens, release, 2, TOKEN_STRING, sizeof(void *) * 3);
117 #ifdef SMP
118 KTR_INFO(KTR_TOKENS, tokens, remote, 3, TOKEN_STRING, sizeof(void *) * 3);
119 KTR_INFO(KTR_TOKENS, tokens, reqremote, 4, TOKEN_STRING, sizeof(void *) * 3);
120 KTR_INFO(KTR_TOKENS, tokens, reqfail, 5, TOKEN_STRING, sizeof(void *) * 3);
121 KTR_INFO(KTR_TOKENS, tokens, drain, 6, TOKEN_STRING, sizeof(void *) * 3);
122 KTR_INFO(KTR_TOKENS, tokens, contention_start, 7, CONTENDED_STRING, sizeof(void *) * 3);
123 KTR_INFO(KTR_TOKENS, tokens, contention_stop, 7, UNCONTENDED_STRING, sizeof(void *) * 3);
124 #endif
126 #define logtoken(name, ref) \
127 KTR_LOG(tokens_ ## name, ref, ref->tr_tok, curthread)
129 #ifdef _KERNEL
131 #ifdef INVARIANTS
132 SYSCTL_INT(_lwkt, OID_AUTO, token_debug, CTLFLAG_RW, &token_debug, 0, "");
133 #endif
135 #endif
137 #ifdef SMP
140 * Determine if we own all the tokens in the token reference list.
141 * Return 1 on success, 0 on failure.
143 * As a side effect, queue requests for tokens we want which are owned
144 * by other cpus. The magic number is used to communicate when the
145 * target cpu has processed the request. Note, however, that the
146 * target cpu may not be able to assign the token to us which is why
147 * the scheduler must spin.
150 lwkt_chktokens(thread_t td)
152 globaldata_t gd = td->td_gd; /* mycpu */
153 lwkt_tokref_t refs;
154 globaldata_t dgd;
155 lwkt_token_t tok;
156 __uint32_t magic;
157 int r = 1;
159 KKASSERT(gd->gd_curthread->td_pri >= TDPRI_CRIT);
160 for (refs = td->td_toks; refs; refs = refs->tr_next) {
161 tok = refs->tr_tok;
162 if ((dgd = tok->t_cpu) != gd) {
163 cpu_ccfence(); /* don't let the compiler reload tok->t_cpu */
164 r = 0;
165 #ifdef INVARIANTS
166 if ((refs->tr_flags & LWKT_TOKREF_CONTENDED) == 0) {
167 refs->tr_flags |= LWKT_TOKREF_CONTENDED;
168 /* mark token contended */
169 logtoken(contention_start, refs);
171 #endif
174 * Queue a request to the target cpu, exit the loop early if
175 * we are unable to queue the IPI message. The magic number
176 * flags whether we have a pending ipi request queued or not.
177 * It can be set from MAGIC2 to MAGIC1 by a remote cpu but can
178 * only be set from MAGIC1 to MAGIC2 by our cpu.
180 magic = refs->tr_magic;
181 cpu_ccfence();
182 if (magic == LWKT_TOKREF_MAGIC1) {
183 refs->tr_magic = LWKT_TOKREF_MAGIC2; /* MP synched slowreq*/
184 refs->tr_reqgd = gd;
185 tok->t_reqcpu = gd; /* MP unsynchronized 'fast' req */
187 logtoken(reqremote, refs);
189 if (lwkt_send_ipiq_nowait(dgd, lwkt_reqtoken_remote, refs)) {
190 /* failed */
191 refs->tr_magic = LWKT_TOKREF_MAGIC1;
193 logtoken(reqfail, refs);
194 break;
196 } else if (magic != LWKT_TOKREF_MAGIC2) {
197 panic("lwkt_chktoken(): token ref %p tok %p bad magic %08x\n",
198 refs, refs->tr_tok, magic);
201 #ifdef INVARIANTS
202 if (refs->tr_flags & LWKT_TOKREF_CONTENDED) {
203 /* mark token uncontended */
204 refs->tr_flags &= ~LWKT_TOKREF_CONTENDED;
205 logtoken(contention_stop, refs);
207 #endif
209 return(r);
212 #endif
215 * Check if we already own the token. Return 1 on success, 0 on failure.
218 lwkt_havetoken(lwkt_token_t tok)
220 globaldata_t gd = mycpu;
221 thread_t td = gd->gd_curthread;
222 lwkt_tokref_t ref;
224 for (ref = td->td_toks; ref; ref = ref->tr_next) {
225 if (ref->tr_tok == tok)
226 return(1);
228 return(0);
232 lwkt_havetokref(lwkt_tokref_t xref)
234 globaldata_t gd = mycpu;
235 thread_t td = gd->gd_curthread;
236 lwkt_tokref_t ref;
238 for (ref = td->td_toks; ref; ref = ref->tr_next) {
239 if (ref == xref)
240 return(1);
242 return(0);
245 #ifdef SMP
248 * Returns 1 if it is ok to give a token away, 0 if it is not.
250 static int
251 lwkt_oktogiveaway_token(lwkt_token_t tok)
253 globaldata_t gd = mycpu;
254 lwkt_tokref_t ref;
255 thread_t td;
257 for (td = gd->gd_curthread; td; td = td->td_preempted) {
258 for (ref = td->td_toks; ref; ref = ref->tr_next) {
259 if (ref->tr_tok == tok)
260 return(0);
263 return(1);
266 #endif
269 * Acquire a serializing token
272 static __inline
273 void
274 _lwkt_gettokref(lwkt_tokref_t ref)
276 lwkt_tokref_t scan;
277 lwkt_token_t tok;
278 globaldata_t gd;
279 thread_t td;
281 gd = mycpu; /* our cpu */
282 KKASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC1);
283 KKASSERT(gd->gd_intr_nesting_level == 0);
284 td = gd->gd_curthread; /* our thread */
287 * Link the request into our thread's list. This interlocks against
288 * remote requests from other cpus, prevents the token from being
289 * given away if our cpu already owns it, and interlocks against
290 * preempting threads which may want the token. This also allows us to
291 * avoid using a critical section.
293 ref->tr_next = td->td_toks;
294 cpu_ccfence(); /* prevent compiler reordering */
295 td->td_toks = ref;
296 tok = ref->tr_tok;
299 * If we are preempting another thread which owns the token we have to
300 * yield to get out from the preemption because we cannot obtain a token
301 * owned by the thread we are preempting.
303 if (td->td_preempted) {
304 while ((td = td->td_preempted) != NULL) {
305 for (scan = td->td_toks; scan; scan = scan->tr_next) {
306 if (scan->tr_tok == tok) {
307 lwkt_yield();
308 KKASSERT(tok->t_cpu == gd);
309 goto breakout;
313 breakout: ;
314 td = gd->gd_curthread; /* our thread, again */
318 * If our cpu does not own the token then (currently) spin while we
319 * await it. XXX we should yield here but some testing is required
320 * before we do so, there could be some interlock issues with e.g.
321 * softupdates before we can yield. ZZZ
323 #ifdef SMP
324 if (tok->t_cpu != gd) {
325 #if defined(MAKE_TOKENS_SPIN)
326 int x = 40000000;
327 int y = 10;
328 crit_enter();
329 while (lwkt_chktokens(td) == 0) {
330 lwkt_process_ipiq();
331 lwkt_drain_token_requests();
332 if (--x == 0) {
333 x = 40000000;
334 printf("CHKTOKEN looping on cpu %d\n", gd->gd_cpuid);
335 #ifdef _KERNEL
336 if (--y == 0)
337 panic("CHKTOKEN looping on cpu %d", gd->gd_cpuid);
338 #endif
340 splz();
342 crit_exit();
343 #elif defined(MAKE_TOKENS_YIELD)
344 lwkt_yield();
345 #else
346 #error MAKE_TOKENS_XXX ?
347 #endif
348 KKASSERT(tok->t_cpu == gd);
350 #endif
355 * Attempt to acquire a serializing token
357 static __inline
359 _lwkt_trytokref(lwkt_tokref_t ref)
361 lwkt_token_t tok;
362 globaldata_t gd;
363 thread_t td;
365 gd = mycpu; /* our cpu */
366 KKASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC1);
367 KKASSERT(gd->gd_intr_nesting_level == 0);
368 td = gd->gd_curthread; /* our thread */
371 * Link the request into our thread's list. This interlocks against
372 * remote requests from other cpus and prevents the token from being
373 * given away if our cpu already owns it. This also allows us to
374 * avoid using a critical section.
376 * Force a panic to occur if chktokens is called while the reference
377 * is linked to td_toks but before we have resolved whether we can
378 * keep it. chktokens should never be called on our ref list
379 * preemptively.
381 ref->tr_magic = LWKT_TOKREF_MAGIC3;
382 ref->tr_next = td->td_toks;
383 cpu_ccfence(); /* prevent compiler reordering */
384 td->td_toks = ref;
387 * If our cpu does not own the token then stop now.
389 * Otherwise make sure the token is not held by a thread we are
390 * preempting. If it is, stop.
392 tok = ref->tr_tok;
393 #ifdef SMP
394 if (tok->t_cpu != gd) {
395 td->td_toks = ref->tr_next; /* remove ref */
396 ref->tr_magic = LWKT_TOKREF_MAGIC1;
397 return(0);
399 #endif
400 if (td->td_preempted) {
401 while ((td = td->td_preempted) != NULL) {
402 lwkt_tokref_t scan;
403 for (scan = td->td_toks; scan; scan = scan->tr_next) {
404 if (scan->tr_tok == tok) {
405 td = gd->gd_curthread; /* our thread */
406 td->td_toks = ref->tr_next; /* remove ref */
407 ref->tr_magic = LWKT_TOKREF_MAGIC1;
408 return(0);
415 * We own the token, legitimize the reference.
417 ref->tr_magic = LWKT_TOKREF_MAGIC1;
418 /* 'td' variable no longer valid */
419 return(1);
422 void
423 lwkt_gettoken(lwkt_tokref_t ref, lwkt_token_t tok)
425 lwkt_tokref_init(ref, tok);
426 logtoken(get, ref);
427 _lwkt_gettokref(ref);
430 void
431 lwkt_gettokref(lwkt_tokref_t ref)
433 logtoken(get, ref);
434 _lwkt_gettokref(ref);
438 lwkt_trytoken(lwkt_tokref_t ref, lwkt_token_t tok)
440 lwkt_tokref_init(ref, tok);
441 logtoken(try, ref);
442 return(_lwkt_trytokref(ref));
446 lwkt_trytokref(lwkt_tokref_t ref)
448 logtoken(try, ref);
449 return(_lwkt_trytokref(ref));
453 * Release a serializing token
455 void
456 lwkt_reltoken(lwkt_tokref *_ref)
458 lwkt_tokref_t scan;
459 lwkt_tokref *ref;
460 lwkt_tokref **pref;
461 lwkt_token_t tok;
462 globaldata_t gd;
463 thread_t td;
464 int giveaway;
466 logtoken(release, _ref);
468 * Guard check and stack check (if in the same stack page). We must
469 * also wait for any action pending on remote cpus which we do by
470 * checking the magic number and yielding in a loop.
472 ref = _ref;
473 #ifdef INVARIANTS
474 if ((((intptr_t)ref ^ (intptr_t)&_ref) & ~(intptr_t)PAGE_MASK) == 0)
475 KKASSERT((char *)ref > (char *)&_ref);
476 KKASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC1 ||
477 ref->tr_magic == LWKT_TOKREF_MAGIC2);
478 #endif
480 tok = ref->tr_tok;
481 gd = mycpu;
482 td = gd->gd_curthread;
484 KKASSERT(tok->t_cpu == gd);
485 KKASSERT(gd->gd_intr_nesting_level == 0);
488 * We can only give away the token if we aren't holding it recursively.
489 * Also use the opportunity to locate the link field for the token.
491 * We do not have to scan preempted threads since by definition we cannot
492 * be holding any token held by a thread we are preempting.
494 giveaway = 1;
495 for (pref = &td->td_toks; (ref = *pref) != _ref; pref = &ref->tr_next) {
496 KKASSERT(ref != NULL);
497 if (ref->tr_tok == tok)
498 giveaway = 0;
500 for (scan = ref->tr_next; scan; scan = scan->tr_next) {
501 if (scan->tr_tok == tok)
502 giveaway = 0;
506 * Give the token away (if we can) before removing the interlock. Once
507 * the interlock is removed, the token can be given away by an IPI.
509 if (giveaway)
510 tok->t_cpu = tok->t_reqcpu;
511 KKASSERT(*pref == ref);
512 *pref = ref->tr_next;
515 * If we had gotten the token opportunistically and it still happens to
516 * be queued to a target cpu, we have to wait for the target cpu
517 * to finish processing it. This does not happen very often and does
518 * not need to be optimal.
520 while (ref->tr_magic == LWKT_TOKREF_MAGIC2) {
521 #if defined(MAKE_TOKENS_SPIN)
522 crit_enter();
523 #ifdef SMP
524 lwkt_process_ipiq();
525 #endif
526 splz();
527 crit_exit();
528 #elif defined(MAKE_TOKENS_YIELD)
529 lwkt_yield();
530 #else
531 #error MAKE_TOKENS_XXX ?
532 #endif
534 KKASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC1);
538 * Pool tokens are used to provide a type-stable serializing token
539 * pointer that does not race against disappearing data structures.
541 * This routine is called in early boot just after we setup the BSP's
542 * globaldata structure.
544 void
545 lwkt_token_pool_init(void)
547 int i;
549 for (i = 0; i < LWKT_NUM_POOL_TOKENS; ++i)
550 lwkt_token_init(&pool_tokens[i]);
553 lwkt_token_t
554 lwkt_token_pool_get(void *ptraddr)
556 int i;
558 i = ((int)(intptr_t)ptraddr >> 2) ^ ((int)(intptr_t)ptraddr >> 12);
559 return(&pool_tokens[i & LWKT_MASK_POOL_TOKENS]);
562 #ifdef SMP
565 * This is the receiving side of a remote IPI requesting a token. If we
566 * cannot immediately hand the token off to another cpu we queue it.
568 * NOTE! we 'own' the ref structure, but we only 'own' the token if
569 * t_cpu == mycpu.
571 static void
572 lwkt_reqtoken_remote(void *data)
574 lwkt_tokref_t ref = data;
575 globaldata_t gd = mycpu;
576 lwkt_token_t tok = ref->tr_tok;
578 logtoken(remote, ref);
580 * We do not have to queue the token if we can give it away
581 * immediately. Otherwise we queue it to our globaldata structure.
583 KASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC2, ("ref %p token %p magic %08x", ref, ref->tr_tok, ref->tr_magic));
584 if (lwkt_oktogiveaway_token(tok)) {
585 if (tok->t_cpu == gd)
586 tok->t_cpu = ref->tr_reqgd;
587 cpu_ccfence(); /* prevent compiler reordering */
588 ref->tr_magic = LWKT_TOKREF_MAGIC1;
589 } else {
590 ref->tr_gdreqnext = gd->gd_tokreqbase;
591 gd->gd_tokreqbase = ref;
596 * Must be called from a critical section. Satisfy all remote token
597 * requests that are pending on our globaldata structure. The request
598 * does not have to be satisfied with a successful change of ownership
599 * but we do have to acknowledge that we have completed processing the
600 * request by setting the magic number back to MAGIC1.
602 * NOTE! we 'own' the ref structure, but we only 'own' the token if
603 * t_cpu == mycpu.
605 void
606 lwkt_drain_token_requests(void)
608 globaldata_t gd = mycpu;
609 lwkt_tokref_t ref;
611 KKASSERT(gd->gd_curthread->td_pri >= TDPRI_CRIT);
612 while ((ref = gd->gd_tokreqbase) != NULL) {
613 gd->gd_tokreqbase = ref->tr_gdreqnext;
614 logtoken(drain, ref);
615 KKASSERT(ref->tr_magic == LWKT_TOKREF_MAGIC2);
616 if (ref->tr_tok->t_cpu == gd)
617 ref->tr_tok->t_cpu = ref->tr_reqgd;
618 cpu_ccfence(); /* prevent compiler reordering */
619 ref->tr_magic = LWKT_TOKREF_MAGIC1;
623 #endif
626 * Initialize the owner and release-to cpu to the current cpu
627 * and reset the generation count.
629 void
630 lwkt_token_init(lwkt_token_t tok)
632 tok->t_cpu = tok->t_reqcpu = mycpu;
635 void
636 lwkt_token_uninit(lwkt_token_t tok)
638 /* empty */