Home | History | Annotate | Line # | Download | only in kern
kern_timeout.c revision 1.24
      1 /*	$NetBSD: kern_timeout.c,v 1.24 2007/07/10 21:26:00 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003, 2006, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe, and by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 2001 Thomas Nordin <nordin (at) openbsd.org>
     41  * Copyright (c) 2000-2001 Artur Grabowski <art (at) openbsd.org>
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  *
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. The name of the author may not be used to endorse or promote products
     54  *    derived from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     57  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     58  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     59  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     60  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     61  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     62  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     63  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     64  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     65  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: kern_timeout.c,v 1.24 2007/07/10 21:26:00 ad Exp $");
     70 
     71 /*
     72  * Timeouts are kept in a hierarchical timing wheel.  The c_time is the
     73  * value of the global variable "hardclock_ticks" when the timeout should
     74  * be called.  There are four levels with 256 buckets each. See 'Scheme 7'
     75  * in "Hashed and Hierarchical Timing Wheels: Efficient Data Structures
     76  * for Implementing a Timer Facility" by George Varghese and Tony Lauck.
     77  *
     78  * Some of the "math" in here is a bit tricky.  We have to beware of
     79  * wrapping ints.
     80  *
     81  * We use the fact that any element added to the queue must be added with
     82  * a positive time.  That means that any element `to' on the queue cannot
     83  * be scheduled to timeout further in time than INT_MAX, but c->c_time can
     84  * be positive or negative so comparing it with anything is dangerous.
     85  * The only way we can use the c->c_time value in any predictable way is
     86  * when we calculate how far in the future `to' will timeout - "c->c_time
     87  * - hardclock_ticks".  The result will always be positive for future
     88  * timeouts and 0 or negative for due timeouts.
     89  */
     90 
     91 #define	_CALLOUT_PRIVATE
     92 
     93 #include <sys/param.h>
     94 #include <sys/systm.h>
     95 #include <sys/kernel.h>
     96 #include <sys/lock.h>
     97 #include <sys/callout.h>
     98 #include <sys/mutex.h>
     99 #include <sys/proc.h>
    100 #include <sys/sleepq.h>
    101 #include <sys/syncobj.h>
    102 #include <sys/evcnt.h>
    103 
    104 #include <machine/intr.h>
    105 
    106 #ifdef DDB
    107 #include <machine/db_machdep.h>
    108 #include <ddb/db_interface.h>
    109 #include <ddb/db_access.h>
    110 #include <ddb/db_sym.h>
    111 #include <ddb/db_output.h>
    112 #endif
    113 
    114 #define BUCKETS		1024
    115 #define WHEELSIZE	256
    116 #define WHEELMASK	255
    117 #define WHEELBITS	8
    118 
    119 static struct callout_circq timeout_wheel[BUCKETS];	/* Queues of timeouts */
    120 static struct callout_circq timeout_todo;		/* Worklist */
    121 
    122 #define MASKWHEEL(wheel, time) (((time) >> ((wheel)*WHEELBITS)) & WHEELMASK)
    123 
    124 #define BUCKET(rel, abs)						\
    125     (((rel) <= (1 << (2*WHEELBITS)))					\
    126     	? ((rel) <= (1 << WHEELBITS))					\
    127             ? &timeout_wheel[MASKWHEEL(0, (abs))]			\
    128             : &timeout_wheel[MASKWHEEL(1, (abs)) + WHEELSIZE]		\
    129         : ((rel) <= (1 << (3*WHEELBITS)))				\
    130             ? &timeout_wheel[MASKWHEEL(2, (abs)) + 2*WHEELSIZE]		\
    131             : &timeout_wheel[MASKWHEEL(3, (abs)) + 3*WHEELSIZE])
    132 
    133 #define MOVEBUCKET(wheel, time)						\
    134     CIRCQ_APPEND(&timeout_todo,						\
    135         &timeout_wheel[MASKWHEEL((wheel), (time)) + (wheel)*WHEELSIZE])
    136 
    137 /*
    138  * Circular queue definitions.
    139  */
    140 
    141 #define CIRCQ_INIT(list)						\
    142 do {									\
    143         (list)->cq_next_l = (list);					\
    144         (list)->cq_prev_l = (list);					\
    145 } while (/*CONSTCOND*/0)
    146 
    147 #define CIRCQ_INSERT(elem, list)					\
    148 do {									\
    149         (elem)->cq_prev_e = (list)->cq_prev_e;				\
    150         (elem)->cq_next_l = (list);					\
    151         (list)->cq_prev_l->cq_next_l = (elem);				\
    152         (list)->cq_prev_l = (elem);					\
    153 } while (/*CONSTCOND*/0)
    154 
    155 #define CIRCQ_APPEND(fst, snd)						\
    156 do {									\
    157         if (!CIRCQ_EMPTY(snd)) {					\
    158                 (fst)->cq_prev_l->cq_next_l = (snd)->cq_next_l;		\
    159                 (snd)->cq_next_l->cq_prev_l = (fst)->cq_prev_l;		\
    160                 (snd)->cq_prev_l->cq_next_l = (fst);			\
    161                 (fst)->cq_prev_l = (snd)->cq_prev_l;			\
    162                 CIRCQ_INIT(snd);					\
    163         }								\
    164 } while (/*CONSTCOND*/0)
    165 
    166 #define CIRCQ_REMOVE(elem)						\
    167 do {									\
    168         (elem)->cq_next_l->cq_prev_e = (elem)->cq_prev_e;		\
    169         (elem)->cq_prev_l->cq_next_e = (elem)->cq_next_e;		\
    170 } while (/*CONSTCOND*/0)
    171 
    172 #define CIRCQ_FIRST(list)	((list)->cq_next_e)
    173 #define CIRCQ_NEXT(elem)	((elem)->cq_next_e)
    174 #define CIRCQ_LAST(elem,list)	((elem)->cq_next_l == (list))
    175 #define CIRCQ_EMPTY(list)	((list)->cq_next_l == (list))
    176 
    177 static void	callout_softclock(void *);
    178 
    179 /*
    180  * All wheels are locked with the same lock (which must also block out
    181  * all interrupts).  Eventually this should become per-CPU.
    182  */
    183 kmutex_t callout_lock;
    184 sleepq_t callout_sleepq;
    185 void	*callout_si;
    186 
    187 static struct evcnt callout_ev_late;
    188 static struct evcnt callout_ev_block;
    189 
    190 /*
    191  * callout_barrier:
    192  *
    193  *	If the callout is already running, wait until it completes.
    194  *	XXX This should do priority inheritance.
    195  */
    196 static void
    197 callout_barrier(callout_impl_t *c)
    198 {
    199 	extern syncobj_t sleep_syncobj;
    200 	struct cpu_info *ci;
    201 	struct lwp *l;
    202 
    203 	l = curlwp;
    204 
    205 	if ((c->c_flags & CALLOUT_MPSAFE) == 0) {
    206 		/*
    207 		 * Note: we must be called with the kernel lock held,
    208 		 * as we use it to synchronize with callout_softclock().
    209 		 */
    210 		ci = c->c_oncpu;
    211 		ci->ci_data.cpu_callout_cancel = c;
    212 		return;
    213 	}
    214 
    215 	while ((ci = c->c_oncpu) != NULL && ci->ci_data.cpu_callout == c) {
    216 		KASSERT(l->l_wchan == NULL);
    217 
    218 		ci->ci_data.cpu_callout_nwait++;
    219 		callout_ev_block.ev_count++;
    220 
    221 		lwp_lock(l);
    222 		lwp_unlock_to(l, &callout_lock);
    223 		sleepq_enqueue(&callout_sleepq, sched_kpri(l), ci,
    224 		    "callout", &sleep_syncobj);
    225 		sleepq_block(0, false);
    226 		mutex_spin_enter(&callout_lock);
    227 	}
    228 }
    229 
    230 /*
    231  * callout_running:
    232  *
    233  *	Return non-zero if callout 'c' is currently executing.
    234  */
    235 static inline bool
    236 callout_running(callout_impl_t *c)
    237 {
    238 	struct cpu_info *ci;
    239 
    240 	if ((ci = c->c_oncpu) == NULL)
    241 		return false;
    242 	if (ci->ci_data.cpu_callout != c)
    243 		return false;
    244 	if (c->c_onlwp == curlwp)
    245 		return false;
    246 	return true;
    247 }
    248 
    249 /*
    250  * callout_startup:
    251  *
    252  *	Initialize the callout facility, called at system startup time.
    253  */
    254 void
    255 callout_startup(void)
    256 {
    257 	int b;
    258 
    259 	KASSERT(sizeof(callout_impl_t) <= sizeof(callout_t));
    260 
    261 	CIRCQ_INIT(&timeout_todo);
    262 	for (b = 0; b < BUCKETS; b++)
    263 		CIRCQ_INIT(&timeout_wheel[b]);
    264 
    265 	mutex_init(&callout_lock, MUTEX_SPIN, IPL_SCHED);
    266 	sleepq_init(&callout_sleepq, &callout_lock);
    267 
    268 	evcnt_attach_dynamic(&callout_ev_late, EVCNT_TYPE_MISC,
    269 	    NULL, "callout", "late");
    270 	evcnt_attach_dynamic(&callout_ev_block, EVCNT_TYPE_MISC,
    271 	    NULL, "callout", "block waiting");
    272 }
    273 
    274 /*
    275  * callout_startup2:
    276  *
    277  *	Complete initialization once soft interrupts are available.
    278  */
    279 void
    280 callout_startup2(void)
    281 {
    282 
    283 	callout_si = softintr_establish(IPL_SOFTCLOCK,
    284 	    callout_softclock, NULL);
    285 	if (callout_si == NULL)
    286 		panic("callout_startup2: unable to register softclock intr");
    287 }
    288 
    289 /*
    290  * callout_init:
    291  *
    292  *	Initialize a callout structure.
    293  */
    294 void
    295 callout_init(callout_t *cs, u_int flags)
    296 {
    297 	callout_impl_t *c = (callout_impl_t *)cs;
    298 
    299 	KASSERT((flags & ~CALLOUT_FLAGMASK) == 0);
    300 
    301 	memset(c, 0, sizeof(*c));
    302 	c->c_flags = flags;
    303 	c->c_magic = CALLOUT_MAGIC;
    304 }
    305 
    306 /*
    307  * callout_destroy:
    308  *
    309  *	Destroy a callout structure.  The callout must be stopped.
    310  */
    311 void
    312 callout_destroy(callout_t *cs)
    313 {
    314 	callout_impl_t *c = (callout_impl_t *)cs;
    315 
    316 	/*
    317 	 * It's not necessary to lock in order to see the correct value
    318 	 * of c->c_flags.  If the callout could potentially have been
    319 	 * running, the current thread should have stopped it.
    320 	 */
    321 	KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
    322 	if (c->c_oncpu != NULL) {
    323 		KASSERT(
    324 		    ((struct cpu_info *)c->c_oncpu)->ci_data.cpu_callout != c);
    325 	}
    326 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    327 
    328 	c->c_magic = 0;
    329 }
    330 
    331 
    332 /*
    333  * callout_reset:
    334  *
    335  *	Reset a callout structure with a new function and argument, and
    336  *	schedule it to run.
    337  */
    338 void
    339 callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg)
    340 {
    341 	callout_impl_t *c = (callout_impl_t *)cs;
    342 	int old_time;
    343 
    344 	KASSERT(to_ticks >= 0);
    345 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    346 	KASSERT(func != NULL);
    347 
    348 	mutex_spin_enter(&callout_lock);
    349 
    350 	/* Initialize the time here, it won't change. */
    351 	old_time = c->c_time;
    352 	c->c_time = to_ticks + hardclock_ticks;
    353 	c->c_flags &= ~CALLOUT_FIRED;
    354 
    355 	c->c_func = func;
    356 	c->c_arg = arg;
    357 
    358 	/*
    359 	 * If this timeout is already scheduled and now is moved
    360 	 * earlier, reschedule it now. Otherwise leave it in place
    361 	 * and let it be rescheduled later.
    362 	 */
    363 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    364 		if (c->c_time - old_time < 0) {
    365 			CIRCQ_REMOVE(&c->c_list);
    366 			CIRCQ_INSERT(&c->c_list, &timeout_todo);
    367 		}
    368 	} else {
    369 		c->c_flags |= CALLOUT_PENDING;
    370 		CIRCQ_INSERT(&c->c_list, &timeout_todo);
    371 	}
    372 
    373 	mutex_spin_exit(&callout_lock);
    374 }
    375 
    376 /*
    377  * callout_schedule:
    378  *
    379  *	Schedule a callout to run.  The function and argument must
    380  *	already be set in the callout structure.
    381  */
    382 void
    383 callout_schedule(callout_t *cs, int to_ticks)
    384 {
    385 	callout_impl_t *c = (callout_impl_t *)cs;
    386 	int old_time;
    387 
    388 	KASSERT(to_ticks >= 0);
    389 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    390 	KASSERT(c->c_func != NULL);
    391 
    392 	mutex_spin_enter(&callout_lock);
    393 
    394 	/* Initialize the time here, it won't change. */
    395 	old_time = c->c_time;
    396 	c->c_time = to_ticks + hardclock_ticks;
    397 	c->c_flags &= ~CALLOUT_FIRED;
    398 
    399 	/*
    400 	 * If this timeout is already scheduled and now is moved
    401 	 * earlier, reschedule it now. Otherwise leave it in place
    402 	 * and let it be rescheduled later.
    403 	 */
    404 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    405 		if (c->c_time - old_time < 0) {
    406 			CIRCQ_REMOVE(&c->c_list);
    407 			CIRCQ_INSERT(&c->c_list, &timeout_todo);
    408 		}
    409 	} else {
    410 		c->c_flags |= CALLOUT_PENDING;
    411 		CIRCQ_INSERT(&c->c_list, &timeout_todo);
    412 	}
    413 
    414 	mutex_spin_exit(&callout_lock);
    415 }
    416 
    417 /*
    418  * callout_stop:
    419  *
    420  *	Cancel a pending callout.
    421  */
    422 bool
    423 callout_stop(callout_t *cs)
    424 {
    425 	callout_impl_t *c = (callout_impl_t *)cs;
    426 	bool expired;
    427 
    428 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    429 
    430 	mutex_spin_enter(&callout_lock);
    431 
    432 	if (callout_running(c))
    433 		callout_barrier(c);
    434 
    435 	if ((c->c_flags & CALLOUT_PENDING) != 0)
    436 		CIRCQ_REMOVE(&c->c_list);
    437 
    438 	expired = ((c->c_flags & CALLOUT_FIRED) != 0);
    439 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    440 
    441 	mutex_spin_exit(&callout_lock);
    442 
    443 	return expired;
    444 }
    445 
    446 void
    447 callout_setfunc(callout_t *cs, void (*func)(void *), void *arg)
    448 {
    449 	callout_impl_t *c = (callout_impl_t *)cs;
    450 
    451 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    452 
    453 	mutex_spin_enter(&callout_lock);
    454 	c->c_func = func;
    455 	c->c_arg = arg;
    456 	mutex_spin_exit(&callout_lock);
    457 }
    458 
    459 bool
    460 callout_expired(callout_t *cs)
    461 {
    462 	callout_impl_t *c = (callout_impl_t *)cs;
    463 	bool rv;
    464 
    465 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    466 
    467 	mutex_spin_enter(&callout_lock);
    468 	rv = ((c->c_flags & CALLOUT_FIRED) != 0);
    469 	mutex_spin_exit(&callout_lock);
    470 
    471 	return rv;
    472 }
    473 
    474 bool
    475 callout_active(callout_t *cs)
    476 {
    477 	callout_impl_t *c = (callout_impl_t *)cs;
    478 	bool rv;
    479 
    480 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    481 
    482 	mutex_spin_enter(&callout_lock);
    483 	rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0);
    484 	mutex_spin_exit(&callout_lock);
    485 
    486 	return rv;
    487 }
    488 
    489 bool
    490 callout_pending(callout_t *cs)
    491 {
    492 	callout_impl_t *c = (callout_impl_t *)cs;
    493 	bool rv;
    494 
    495 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    496 
    497 	mutex_spin_enter(&callout_lock);
    498 	rv = ((c->c_flags & CALLOUT_PENDING) != 0);
    499 	mutex_spin_exit(&callout_lock);
    500 
    501 	return rv;
    502 }
    503 
    504 bool
    505 callout_invoking(callout_t *cs)
    506 {
    507 	callout_impl_t *c = (callout_impl_t *)cs;
    508 	bool rv;
    509 
    510 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    511 
    512 	mutex_spin_enter(&callout_lock);
    513 	rv = ((c->c_flags & CALLOUT_INVOKING) != 0);
    514 	mutex_spin_exit(&callout_lock);
    515 
    516 	return rv;
    517 }
    518 
    519 void
    520 callout_ack(callout_t *cs)
    521 {
    522 	callout_impl_t *c = (callout_impl_t *)cs;
    523 
    524 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    525 
    526 	mutex_spin_enter(&callout_lock);
    527 	c->c_flags &= ~CALLOUT_INVOKING;
    528 	mutex_spin_exit(&callout_lock);
    529 }
    530 
    531 /*
    532  * This is called from hardclock() once every tick.
    533  * We schedule callout_softclock() if there is work
    534  * to be done.
    535  */
    536 void
    537 callout_hardclock(void)
    538 {
    539 	int needsoftclock;
    540 
    541 	mutex_spin_enter(&callout_lock);
    542 
    543 	MOVEBUCKET(0, hardclock_ticks);
    544 	if (MASKWHEEL(0, hardclock_ticks) == 0) {
    545 		MOVEBUCKET(1, hardclock_ticks);
    546 		if (MASKWHEEL(1, hardclock_ticks) == 0) {
    547 			MOVEBUCKET(2, hardclock_ticks);
    548 			if (MASKWHEEL(2, hardclock_ticks) == 0)
    549 				MOVEBUCKET(3, hardclock_ticks);
    550 		}
    551 	}
    552 
    553 	needsoftclock = !CIRCQ_EMPTY(&timeout_todo);
    554 	mutex_spin_exit(&callout_lock);
    555 
    556 	if (needsoftclock)
    557 		softintr_schedule(callout_si);
    558 }
    559 
    560 /* ARGSUSED */
    561 static void
    562 callout_softclock(void *v)
    563 {
    564 	callout_impl_t *c;
    565 	struct cpu_info *ci;
    566 	void (*func)(void *);
    567 	void *arg;
    568 	u_int mpsafe, count;
    569 	lwp_t *l;
    570 
    571 	l = curlwp;
    572 	ci = l->l_cpu;
    573 
    574 	mutex_spin_enter(&callout_lock);
    575 
    576 	while (!CIRCQ_EMPTY(&timeout_todo)) {
    577 		c = CIRCQ_FIRST(&timeout_todo);
    578 		KASSERT(c->c_magic == CALLOUT_MAGIC);
    579 		KASSERT(c->c_func != NULL);
    580 		CIRCQ_REMOVE(&c->c_list);
    581 
    582 		/* If due run it, otherwise insert it into the right bucket. */
    583 		if (c->c_time - hardclock_ticks > 0) {
    584 			CIRCQ_INSERT(&c->c_list,
    585 			    BUCKET((c->c_time - hardclock_ticks), c->c_time));
    586 		} else {
    587 			if (c->c_time - hardclock_ticks < 0)
    588 				callout_ev_late.ev_count++;
    589 
    590 			c->c_flags ^= (CALLOUT_PENDING | CALLOUT_FIRED);
    591 			mpsafe = (c->c_flags & CALLOUT_MPSAFE);
    592 			func = c->c_func;
    593 			arg = c->c_arg;
    594 			c->c_oncpu = ci;
    595 			c->c_onlwp = l;
    596 
    597 			mutex_spin_exit(&callout_lock);
    598 			if (!mpsafe) {
    599 				KERNEL_LOCK(1, curlwp);
    600 				if (ci->ci_data.cpu_callout_cancel != c)
    601 					(*func)(arg);
    602 				KERNEL_UNLOCK_ONE(curlwp);
    603 			} else
    604 					(*func)(arg);
    605 			mutex_spin_enter(&callout_lock);
    606 
    607 			/*
    608 			 * We can't touch 'c' here because it might be
    609 			 * freed already.  If LWPs waiting for callout
    610 			 * to complete, awaken them.
    611 			 */
    612 			ci->ci_data.cpu_callout_cancel = NULL;
    613 			ci->ci_data.cpu_callout = NULL;
    614 			if ((count = ci->ci_data.cpu_callout_nwait) != 0) {
    615 				ci->ci_data.cpu_callout_nwait = 0;
    616 				/* sleepq_wake() drops the lock. */
    617 				sleepq_wake(&callout_sleepq, ci, count);
    618 				mutex_spin_enter(&callout_lock);
    619 			}
    620 		}
    621 	}
    622 
    623 	mutex_spin_exit(&callout_lock);
    624 }
    625 
    626 #ifdef DDB
    627 static void
    628 db_show_callout_bucket(struct callout_circq *bucket)
    629 {
    630 	callout_impl_t *c;
    631 	db_expr_t offset;
    632 	const char *name;
    633 	static char question[] = "?";
    634 
    635 	if (CIRCQ_EMPTY(bucket))
    636 		return;
    637 
    638 	for (c = CIRCQ_FIRST(bucket); /*nothing*/; c = CIRCQ_NEXT(&c->c_list)) {
    639 		db_find_sym_and_offset((db_addr_t)(intptr_t)c->c_func, &name,
    640 		    &offset);
    641 		name = name ? name : question;
    642 #ifdef _LP64
    643 #define	POINTER_WIDTH	"%16lx"
    644 #else
    645 #define	POINTER_WIDTH	"%8lx"
    646 #endif
    647 		db_printf("%9d %2d/%-4d " POINTER_WIDTH "  %s\n",
    648 		    c->c_time - hardclock_ticks,
    649 		    (int)((bucket - timeout_wheel) / WHEELSIZE),
    650 		    (int)(bucket - timeout_wheel), (u_long) c->c_arg, name);
    651 
    652 		if (CIRCQ_LAST(&c->c_list, bucket))
    653 			break;
    654 	}
    655 }
    656 
    657 void
    658 db_show_callout(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    659 {
    660 	int b;
    661 
    662 	db_printf("hardclock_ticks now: %d\n", hardclock_ticks);
    663 #ifdef _LP64
    664 	db_printf("    ticks  wheel               arg  func\n");
    665 #else
    666 	db_printf("    ticks  wheel       arg  func\n");
    667 #endif
    668 
    669 	/*
    670 	 * Don't lock the callwheel; all the other CPUs are paused
    671 	 * anyhow, and we might be called in a circumstance where
    672 	 * some other CPU was paused while holding the lock.
    673 	 */
    674 
    675 	db_show_callout_bucket(&timeout_todo);
    676 	for (b = 0; b < BUCKETS; b++)
    677 		db_show_callout_bucket(&timeout_wheel[b]);
    678 }
    679 #endif /* DDB */
    680