kernel - lwkt_token revamp
[dragonfly.git] / sys / kern / lwkt_token.c
CommitLineData
c31b1324 1/*
c6fbe95a 2 * Copyright (c) 2003,2004,2009 The DragonFly Project. All rights reserved.
8c10bfcf
MD
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
c31b1324
MD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
8c10bfcf 10 *
c31b1324
MD
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
8c10bfcf
MD
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
c31b1324 32 * SUCH DAMAGE.
c31b1324
MD
33 */
34
c6fbe95a
MD
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 */
c31b1324
MD
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>
c31b1324 54#include <sys/sysctl.h>
4883dbe9 55#include <sys/ktr.h>
c31b1324
MD
56#include <sys/kthread.h>
57#include <machine/cpu.h>
58#include <sys/lock.h>
59#include <sys/caps.h>
9d265729
MD
60#include <sys/spinlock.h>
61
62#include <sys/thread2.h>
63#include <sys/spinlock2.h>
3b998fa9 64#include <sys/mplock2.h>
c31b1324
MD
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>
c31b1324
MD
77#include <machine/smp.h>
78
41a01a4d
MD
79#ifndef LWKT_NUM_POOL_TOKENS
80#define LWKT_NUM_POOL_TOKENS 1024 /* power of 2 */
81#endif
82#define LWKT_MASK_POOL_TOKENS (LWKT_NUM_POOL_TOKENS - 1)
83
c31b1324
MD
84#ifdef INVARIANTS
85static int token_debug = 0;
86#endif
87
41a01a4d
MD
88static lwkt_token pool_tokens[LWKT_NUM_POOL_TOKENS];
89
f917e9bc 90#define TOKEN_STRING "REF=%p TOK=%p TD=%p"
38717797
HP
91#define CONTENDED_STRING "REF=%p TOK=%p TD=%p (contention started)"
92#define UNCONTENDED_STRING "REF=%p TOK=%p TD=%p (contention stopped)"
4883dbe9
MD
93#if !defined(KTR_TOKENS)
94#define KTR_TOKENS KTR_ALL
95#endif
790e4db7 96
4883dbe9 97KTR_INFO_MASTER(tokens);
c6fbe95a
MD
98KTR_INFO(KTR_TOKENS, tokens, fail, 0, TOKEN_STRING, sizeof(void *) * 3);
99KTR_INFO(KTR_TOKENS, tokens, succ, 1, TOKEN_STRING, sizeof(void *) * 3);
7cd8d145 100#if 0
c6fbe95a 101KTR_INFO(KTR_TOKENS, tokens, release, 2, TOKEN_STRING, sizeof(void *) * 3);
f917e9bc
MD
102KTR_INFO(KTR_TOKENS, tokens, remote, 3, TOKEN_STRING, sizeof(void *) * 3);
103KTR_INFO(KTR_TOKENS, tokens, reqremote, 4, TOKEN_STRING, sizeof(void *) * 3);
104KTR_INFO(KTR_TOKENS, tokens, reqfail, 5, TOKEN_STRING, sizeof(void *) * 3);
105KTR_INFO(KTR_TOKENS, tokens, drain, 6, TOKEN_STRING, sizeof(void *) * 3);
38717797
HP
106KTR_INFO(KTR_TOKENS, tokens, contention_start, 7, CONTENDED_STRING, sizeof(void *) * 3);
107KTR_INFO(KTR_TOKENS, tokens, contention_stop, 7, UNCONTENDED_STRING, sizeof(void *) * 3);
790e4db7
MD
108#endif
109
f917e9bc
MD
110#define logtoken(name, ref) \
111 KTR_LOG(tokens_ ## name, ref, ref->tr_tok, curthread)
4883dbe9 112
c31b1324
MD
113#ifdef INVARIANTS
114SYSCTL_INT(_lwkt, OID_AUTO, token_debug, CTLFLAG_RW, &token_debug, 0, "");
115#endif
116
c31b1324 117/*
c6fbe95a
MD
118 * Return a pool token given an address
119 */
120static __inline
121lwkt_token_t
122_lwkt_token_pool_lookup(void *ptr)
123{
124 int i;
125
126 i = ((int)(intptr_t)ptr >> 2) ^ ((int)(intptr_t)ptr >> 12);
127 return(&pool_tokens[i & LWKT_MASK_POOL_TOKENS]);
128}
129
3b998fa9
MD
130/*
131 * Initialize a tokref_t prior to making it visible in the thread's
132 * token array.
133 */
134static __inline
135void
136_lwkt_tokref_init(lwkt_tokref_t ref, lwkt_token_t tok, thread_t td)
137{
138 ref->tr_tok = tok;
139 ref->tr_owner = td;
140 ref->tr_flags = tok->t_flags;
141}
c6fbe95a
MD
142
143/*
9d265729 144 * Obtain all the tokens required by the specified thread on the current
1fe5fad2
MD
145 * cpu, return 0 on failure and non-zero on success. If a failure occurs
146 * any partially acquired tokens will be released prior to return.
dd55d707 147 *
7eb611ef 148 * lwkt_getalltokens is called by the LWKT scheduler to acquire all
24c80b2b 149 * tokens that the thread had acquired prior to going to sleep.
7eb611ef 150 *
3b998fa9
MD
151 * The scheduler is responsible for maintaining the MP lock count, so
152 * we don't need to deal with tr_flags here.
153 *
7eb611ef 154 * Called from a critical section.
c31b1324 155 */
41a01a4d 156int
9d265729 157lwkt_getalltokens(thread_t td)
41a01a4d 158{
3b998fa9 159 lwkt_tokref_t scan;
c6fbe95a
MD
160 lwkt_tokref_t ref;
161 lwkt_token_t tok;
162
3b998fa9
MD
163 /*
164 * Acquire tokens in forward order, assign or validate tok->t_ref.
165 */
166 for (scan = &td->td_toks_base; scan < td->td_toks_stop; ++scan) {
167 tok = scan->tr_tok;
c6fbe95a
MD
168 for (;;) {
169 /*
170 * Try to acquire the token if we do not already have
171 * it.
172 *
173 * NOTE: If atomic_cmpset_ptr() fails we have to
174 * loop and try again. It just means we
175 * lost a cpu race.
176 */
177 ref = tok->t_ref;
c6fbe95a 178 if (ref == NULL) {
3b998fa9 179 if (atomic_cmpset_ptr(&tok->t_ref, NULL, scan))
c6fbe95a
MD
180 break;
181 continue;
182 }
183
184 /*
3b998fa9
MD
185 * Test if ref is already recursively held by this
186 * thread. We cannot safely dereference tok->t_ref
187 * (it might belong to another thread and is thus
188 * unstable), but we don't have to. We can simply
189 * range-check it.
c6fbe95a 190 */
3b998fa9
MD
191 if (ref >= &td->td_toks_base && ref < td->td_toks_stop)
192 break;
193
194 /*
195 * Otherwise we failed to acquire all the tokens.
196 * Undo and return.
197 */
198 lwkt_relalltokens(td);
199 return(FALSE);
38717797 200 }
41a01a4d 201 }
c6fbe95a 202 return (TRUE);
c31b1324
MD
203}
204
41a01a4d 205/*
9d265729 206 * Release all tokens owned by the specified thread on the current cpu.
c6fbe95a
MD
207 *
208 * This code is really simple. Even in cases where we own all the tokens
209 * note that t_ref may not match the scan for recursively held tokens,
210 * or for the case where a lwkt_getalltokens() failed.
3b998fa9
MD
211 *
212 * The scheduler is responsible for maintaining the MP lock count, so
213 * we don't need to deal with tr_flags here.
7eb611ef
MD
214 *
215 * Called from a critical section.
41a01a4d 216 */
9d265729
MD
217void
218lwkt_relalltokens(thread_t td)
41a01a4d 219{
3b998fa9 220 lwkt_tokref_t scan;
c6fbe95a
MD
221 lwkt_token_t tok;
222
3b998fa9
MD
223 for (scan = &td->td_toks_base; scan < td->td_toks_stop; ++scan) {
224 tok = scan->tr_tok;
225 if (tok->t_ref == scan)
c6fbe95a 226 tok->t_ref = NULL;
41a01a4d 227 }
41a01a4d
MD
228}
229
41a01a4d 230/*
3b998fa9
MD
231 * Token acquisition helper function. The caller must have already
232 * made nref visible by adjusting td_toks_stop and will be responsible
233 * for the disposition of nref on either success or failure.
dd55d707 234 *
3b998fa9
MD
235 * When acquiring tokens recursively we want tok->t_ref to point to
236 * the outer (first) acquisition so it gets cleared only on the last
237 * release.
41a01a4d 238 */
41a01a4d 239static __inline
7eb611ef 240int
c6fbe95a 241_lwkt_trytokref2(lwkt_tokref_t nref, thread_t td)
41a01a4d 242{
c6fbe95a 243 lwkt_token_t tok;
3b998fa9 244 lwkt_tokref_t ref;
c6fbe95a
MD
245
246 KKASSERT(td->td_gd->gd_intr_nesting_level == 0);
247
7eb611ef 248 /*
3b998fa9
MD
249 * Make sure the compiler does not reorder prior instructions
250 * beyond this demark.
7eb611ef 251 */
c6fbe95a
MD
252 cpu_ccfence();
253
7eb611ef 254 /*
c6fbe95a 255 * Attempt to gain ownership
7eb611ef 256 */
c6fbe95a
MD
257 tok = nref->tr_tok;
258 for (;;) {
259 /*
260 * Try to acquire the token if we do not already have
261 * it.
262 */
263 ref = tok->t_ref;
c6fbe95a
MD
264 if (ref == NULL) {
265 /*
266 * NOTE: If atomic_cmpset_ptr() fails we have to
267 * loop and try again. It just means we
268 * lost a cpu race.
269 */
270 if (atomic_cmpset_ptr(&tok->t_ref, NULL, nref))
271 return (TRUE);
272 continue;
273 }
274
275 /*
3b998fa9
MD
276 * Test if ref is already recursively held by this
277 * thread. We cannot safely dereference tok->t_ref
278 * (it might belong to another thread and is thus
279 * unstable), but we don't have to. We can simply
280 * range-check it.
281 */
282 if (ref >= &td->td_toks_base && ref < td->td_toks_stop)
283 return(TRUE);
284
285 /*
286 * Otherwise we failed.
c6fbe95a 287 */
c6fbe95a 288 return(FALSE);
dd55d707 289 }
c31b1324
MD
290}
291
c6fbe95a
MD
292/*
293 * Acquire a serializing token. This routine does not block.
294 */
41a01a4d 295static __inline
c31b1324 296int
c6fbe95a 297_lwkt_trytokref(lwkt_tokref_t ref, thread_t td)
c31b1324 298{
3b998fa9
MD
299 if ((ref->tr_flags & LWKT_TOKEN_MPSAFE) == 0) {
300 if (try_mplock() == 0)
301 return (FALSE);
302 }
c6fbe95a
MD
303 if (_lwkt_trytokref2(ref, td) == FALSE) {
304 /*
3b998fa9 305 * Cleanup, deactivate the failed token.
c6fbe95a 306 */
3b998fa9
MD
307 --td->td_toks_stop;
308 if ((ref->tr_flags & LWKT_TOKEN_MPSAFE) == 0)
309 rel_mplock();
c6fbe95a
MD
310 return (FALSE);
311 }
312 return (TRUE);
c31b1324
MD
313}
314
7eb611ef
MD
315/*
316 * Acquire a serializing token. This routine can block.
7eb611ef
MD
317 */
318static __inline
319void
c6fbe95a 320_lwkt_gettokref(lwkt_tokref_t ref, thread_t td)
7eb611ef 321{
3b998fa9
MD
322 if ((ref->tr_flags & LWKT_TOKEN_MPSAFE) == 0)
323 get_mplock();
c6fbe95a
MD
324 if (_lwkt_trytokref2(ref, td) == FALSE) {
325 /*
326 * Give up running if we can't acquire the token right now.
c6fbe95a 327 *
3b998fa9
MD
328 * Since the tokref is already active the scheduler now
329 * takes care of acquisition, so we need only call
330 * lwkt_yield().
331 *
332 * Since we failed this was not a recursive token so upon
c6fbe95a
MD
333 * return tr_tok->t_ref should be assigned to this specific
334 * ref.
335 */
336 logtoken(fail, ref);
337 lwkt_yield();
338 logtoken(succ, ref);
c6fbe95a
MD
339 KKASSERT(ref->tr_tok->t_ref == ref);
340 }
7eb611ef
MD
341}
342
41a01a4d 343void
3b998fa9 344lwkt_gettoken(lwkt_token_t tok)
c31b1324 345{
c6fbe95a 346 thread_t td = curthread;
3b998fa9 347 lwkt_tokref_t ref;
c6fbe95a 348
3b998fa9
MD
349 ref = td->td_toks_stop;
350 KKASSERT(ref < &td->td_toks_end);
351 _lwkt_tokref_init(ref, tok, td);
352 ++td->td_toks_stop;
c6fbe95a
MD
353 _lwkt_gettokref(ref, td);
354}
355
3b998fa9
MD
356lwkt_token_t
357lwkt_getpooltoken(void *ptr)
c6fbe95a
MD
358{
359 thread_t td = curthread;
3b998fa9
MD
360 lwkt_token_t tok;
361 lwkt_tokref_t ref;
c6fbe95a 362
3b998fa9
MD
363 ref = td->td_toks_stop;
364 KKASSERT(ref < &td->td_toks_end);
365 tok = _lwkt_token_pool_lookup(ptr);
366 _lwkt_tokref_init(ref, tok, td);
367 ++td->td_toks_stop;
c6fbe95a 368 _lwkt_gettokref(ref, td);
3b998fa9 369 return(tok);
c31b1324
MD
370}
371
c31b1324 372int
3b998fa9 373lwkt_trytoken(lwkt_token_t tok)
c31b1324 374{
c6fbe95a 375 thread_t td = curthread;
3b998fa9 376 lwkt_tokref_t ref;
c6fbe95a 377
3b998fa9
MD
378 ref = td->td_toks_stop;
379 KKASSERT(ref < &td->td_toks_end);
380 _lwkt_tokref_init(ref, tok, td);
381 ++td->td_toks_stop;
c6fbe95a 382 return(_lwkt_trytokref(ref, td));
41a01a4d
MD
383}
384
c31b1324 385/*
c6fbe95a
MD
386 * Release a serializing token.
387 *
3b998fa9
MD
388 * WARNING! All tokens must be released in reverse order. This will be
389 * asserted.
c31b1324 390 */
41a01a4d 391void
3b998fa9 392lwkt_reltoken(lwkt_token_t tok)
c31b1324 393{
3b998fa9
MD
394 thread_t td = curthread;
395 lwkt_tokref_t ref;
c6fbe95a 396
3b998fa9
MD
397 /*
398 * Remove ref from thread token list and assert that it matches
399 * the token passed in. Tokens must be released in reverse order.
400 */
401 ref = td->td_toks_stop - 1;
402 KKASSERT(ref >= &td->td_toks_base && ref->tr_tok == tok);
403 td->td_toks_stop = ref;
404
405 /*
406 * If the token was not MPSAFE release the MP lock.
407 */
408 if ((ref->tr_flags & LWKT_TOKEN_MPSAFE) == 0)
409 rel_mplock();
c6fbe95a
MD
410
411 /*
3b998fa9
MD
412 * Make sure the compiler does not reorder the clearing of
413 * tok->t_ref.
c6fbe95a 414 */
c6fbe95a
MD
415 cpu_ccfence();
416
417 /*
418 * Only clear the token if it matches ref. If ref was a recursively
419 * acquired token it may not match.
420 */
421 if (tok->t_ref == ref)
422 tok->t_ref = NULL;
c31b1324
MD
423}
424
41a01a4d
MD
425/*
426 * Pool tokens are used to provide a type-stable serializing token
427 * pointer that does not race against disappearing data structures.
428 *
429 * This routine is called in early boot just after we setup the BSP's
430 * globaldata structure.
431 */
432void
433lwkt_token_pool_init(void)
434{
c6fbe95a 435 int i;
41a01a4d 436
c6fbe95a 437 for (i = 0; i < LWKT_NUM_POOL_TOKENS; ++i)
3b998fa9 438 lwkt_token_init(&pool_tokens[i], 1);
41a01a4d
MD
439}
440
441lwkt_token_t
c6fbe95a 442lwkt_token_pool_lookup(void *ptr)
41a01a4d 443{
c6fbe95a 444 return (_lwkt_token_pool_lookup(ptr));
41a01a4d
MD
445}
446
41a01a4d
MD
447/*
448 * Initialize the owner and release-to cpu to the current cpu
449 * and reset the generation count.
450 */
451void
3b998fa9 452lwkt_token_init(lwkt_token_t tok, int mpsafe)
41a01a4d 453{
c6fbe95a 454 tok->t_ref = NULL;
3b998fa9 455 tok->t_flags = mpsafe ? LWKT_TOKEN_MPSAFE : 0;
c31b1324
MD
456}
457
41a01a4d
MD
458void
459lwkt_token_uninit(lwkt_token_t tok)
460{
c6fbe95a 461 /* empty */
41a01a4d 462}
7eb611ef 463
c6fbe95a 464#if 0
7eb611ef
MD
465int
466lwkt_token_is_stale(lwkt_tokref_t ref)
467{
c6fbe95a
MD
468 lwkt_token_t tok = ref->tr_tok;
469
470 KKASSERT(tok->t_owner == curthread && ref->tr_state == 1 &&
471 tok->t_count > 0);
472
473 /* Token is not stale */
474 if (tok->t_lastowner == tok->t_owner)
475 return (FALSE);
476
477 /*
478 * The token is stale. Reset to not stale so that the next call to
479 * lwkt_token_is_stale will return "not stale" unless the token
480 * was acquired in-between by another thread.
481 */
482 tok->t_lastowner = tok->t_owner;
483 return (TRUE);
7eb611ef 484}
c6fbe95a 485#endif