Home | History | Annotate | Line # | Download | only in kern
kern_timeout.c revision 1.27.4.3
      1 /*	$NetBSD: kern_timeout.c,v 1.27.4.3 2008/02/18 21:06:46 mjf 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.27.4.3 2008/02/18 21:06:46 mjf 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/callout.h>
     97 #include <sys/mutex.h>
     98 #include <sys/proc.h>
     99 #include <sys/sleepq.h>
    100 #include <sys/syncobj.h>
    101 #include <sys/evcnt.h>
    102 #include <sys/intr.h>
    103 
    104 #ifdef DDB
    105 #include <machine/db_machdep.h>
    106 #include <ddb/db_interface.h>
    107 #include <ddb/db_access.h>
    108 #include <ddb/db_sym.h>
    109 #include <ddb/db_output.h>
    110 #endif
    111 
    112 #define BUCKETS		1024
    113 #define WHEELSIZE	256
    114 #define WHEELMASK	255
    115 #define WHEELBITS	8
    116 
    117 static struct callout_circq timeout_wheel[BUCKETS];	/* Queues of timeouts */
    118 static struct callout_circq timeout_todo;		/* Worklist */
    119 
    120 #define MASKWHEEL(wheel, time) (((time) >> ((wheel)*WHEELBITS)) & WHEELMASK)
    121 
    122 #define BUCKET(rel, abs)						\
    123     (((rel) <= (1 << (2*WHEELBITS)))					\
    124     	? ((rel) <= (1 << WHEELBITS))					\
    125             ? &timeout_wheel[MASKWHEEL(0, (abs))]			\
    126             : &timeout_wheel[MASKWHEEL(1, (abs)) + WHEELSIZE]		\
    127         : ((rel) <= (1 << (3*WHEELBITS)))				\
    128             ? &timeout_wheel[MASKWHEEL(2, (abs)) + 2*WHEELSIZE]		\
    129             : &timeout_wheel[MASKWHEEL(3, (abs)) + 3*WHEELSIZE])
    130 
    131 #define MOVEBUCKET(wheel, time)						\
    132     CIRCQ_APPEND(&timeout_todo,						\
    133         &timeout_wheel[MASKWHEEL((wheel), (time)) + (wheel)*WHEELSIZE])
    134 
    135 /*
    136  * Circular queue definitions.
    137  */
    138 
    139 #define CIRCQ_INIT(list)						\
    140 do {									\
    141         (list)->cq_next_l = (list);					\
    142         (list)->cq_prev_l = (list);					\
    143 } while (/*CONSTCOND*/0)
    144 
    145 #define CIRCQ_INSERT(elem, list)					\
    146 do {									\
    147         (elem)->cq_prev_e = (list)->cq_prev_e;				\
    148         (elem)->cq_next_l = (list);					\
    149         (list)->cq_prev_l->cq_next_l = (elem);				\
    150         (list)->cq_prev_l = (elem);					\
    151 } while (/*CONSTCOND*/0)
    152 
    153 #define CIRCQ_APPEND(fst, snd)						\
    154 do {									\
    155         if (!CIRCQ_EMPTY(snd)) {					\
    156                 (fst)->cq_prev_l->cq_next_l = (snd)->cq_next_l;		\
    157                 (snd)->cq_next_l->cq_prev_l = (fst)->cq_prev_l;		\
    158                 (snd)->cq_prev_l->cq_next_l = (fst);			\
    159                 (fst)->cq_prev_l = (snd)->cq_prev_l;			\
    160                 CIRCQ_INIT(snd);					\
    161         }								\
    162 } while (/*CONSTCOND*/0)
    163 
    164 #define CIRCQ_REMOVE(elem)						\
    165 do {									\
    166         (elem)->cq_next_l->cq_prev_e = (elem)->cq_prev_e;		\
    167         (elem)->cq_prev_l->cq_next_e = (elem)->cq_next_e;		\
    168 } while (/*CONSTCOND*/0)
    169 
    170 #define CIRCQ_FIRST(list)	((list)->cq_next_e)
    171 #define CIRCQ_NEXT(elem)	((elem)->cq_next_e)
    172 #define CIRCQ_LAST(elem,list)	((elem)->cq_next_l == (list))
    173 #define CIRCQ_EMPTY(list)	((list)->cq_next_l == (list))
    174 
    175 static void	callout_softclock(void *);
    176 
    177 /*
    178  * All wheels are locked with the same lock (which must also block out
    179  * all interrupts).  Eventually this should become per-CPU.
    180  */
    181 kmutex_t callout_lock;
    182 sleepq_t callout_sleepq;
    183 void	*callout_si;
    184 
    185 static struct evcnt callout_ev_late;
    186 static struct evcnt callout_ev_block;
    187 
    188 /*
    189  * callout_barrier:
    190  *
    191  *	If the callout is already running, wait until it completes.
    192  *	XXX This should do priority inheritance.
    193  */
    194 static void
    195 callout_barrier(callout_impl_t *c)
    196 {
    197 	extern syncobj_t sleep_syncobj;
    198 	struct cpu_info *ci;
    199 	struct lwp *l;
    200 
    201 	l = curlwp;
    202 
    203 	if ((c->c_flags & CALLOUT_MPSAFE) == 0) {
    204 		/*
    205 		 * Note: we must be called with the kernel lock held,
    206 		 * as we use it to synchronize with callout_softclock().
    207 		 */
    208 		ci = c->c_oncpu;
    209 		ci->ci_data.cpu_callout_cancel = c;
    210 		return;
    211 	}
    212 
    213 	while ((ci = c->c_oncpu) != NULL && ci->ci_data.cpu_callout == c) {
    214 		KASSERT(l->l_wchan == NULL);
    215 
    216 		ci->ci_data.cpu_callout_nwait++;
    217 		callout_ev_block.ev_count++;
    218 
    219 		l->l_kpriority = true;
    220 		sleepq_enter(&callout_sleepq, l);
    221 		sleepq_enqueue(&callout_sleepq, ci, "callout", &sleep_syncobj);
    222 		sleepq_block(0, false);
    223 		mutex_spin_enter(&callout_lock);
    224 	}
    225 }
    226 
    227 /*
    228  * callout_running:
    229  *
    230  *	Return non-zero if callout 'c' is currently executing.
    231  */
    232 static inline bool
    233 callout_running(callout_impl_t *c)
    234 {
    235 	struct cpu_info *ci;
    236 
    237 	if ((ci = c->c_oncpu) == NULL)
    238 		return false;
    239 	if (ci->ci_data.cpu_callout != c)
    240 		return false;
    241 	if (c->c_onlwp == curlwp)
    242 		return false;
    243 	return true;
    244 }
    245 
    246 /*
    247  * callout_startup:
    248  *
    249  *	Initialize the callout facility, called at system startup time.
    250  */
    251 void
    252 callout_startup(void)
    253 {
    254 	int b;
    255 
    256 	KASSERT(sizeof(callout_impl_t) <= sizeof(callout_t));
    257 
    258 	CIRCQ_INIT(&timeout_todo);
    259 	for (b = 0; b < BUCKETS; b++)
    260 		CIRCQ_INIT(&timeout_wheel[b]);
    261 
    262 	mutex_init(&callout_lock, MUTEX_DEFAULT, IPL_SCHED);
    263 	sleepq_init(&callout_sleepq, &callout_lock);
    264 
    265 	evcnt_attach_dynamic(&callout_ev_late, EVCNT_TYPE_MISC,
    266 	    NULL, "callout", "late");
    267 	evcnt_attach_dynamic(&callout_ev_block, EVCNT_TYPE_MISC,
    268 	    NULL, "callout", "block waiting");
    269 }
    270 
    271 /*
    272  * callout_startup2:
    273  *
    274  *	Complete initialization once soft interrupts are available.
    275  */
    276 void
    277 callout_startup2(void)
    278 {
    279 
    280 	callout_si = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    281 	    callout_softclock, NULL);
    282 	if (callout_si == NULL)
    283 		panic("callout_startup2: unable to register softclock intr");
    284 }
    285 
    286 /*
    287  * callout_init:
    288  *
    289  *	Initialize a callout structure.
    290  */
    291 void
    292 callout_init(callout_t *cs, u_int flags)
    293 {
    294 	callout_impl_t *c = (callout_impl_t *)cs;
    295 
    296 	KASSERT((flags & ~CALLOUT_FLAGMASK) == 0);
    297 
    298 	memset(c, 0, sizeof(*c));
    299 	c->c_flags = flags;
    300 	c->c_magic = CALLOUT_MAGIC;
    301 }
    302 
    303 /*
    304  * callout_destroy:
    305  *
    306  *	Destroy a callout structure.  The callout must be stopped.
    307  */
    308 void
    309 callout_destroy(callout_t *cs)
    310 {
    311 	callout_impl_t *c = (callout_impl_t *)cs;
    312 
    313 	/*
    314 	 * It's not necessary to lock in order to see the correct value
    315 	 * of c->c_flags.  If the callout could potentially have been
    316 	 * running, the current thread should have stopped it.
    317 	 */
    318 	KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
    319 	if (c->c_oncpu != NULL) {
    320 		KASSERT(
    321 		    ((struct cpu_info *)c->c_oncpu)->ci_data.cpu_callout != c);
    322 	}
    323 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    324 
    325 	c->c_magic = 0;
    326 }
    327 
    328 /*
    329  * callout_schedule_locked:
    330  *
    331  *	Schedule a callout to run.  The function and argument must
    332  *	already be set in the callout structure.  Must be called with
    333  *	callout_lock.
    334  */
    335 static void
    336 callout_schedule_locked(callout_impl_t *c, int to_ticks)
    337 {
    338 	int old_time;
    339 
    340 	KASSERT(to_ticks >= 0);
    341 	KASSERT(c->c_func != NULL);
    342 
    343 	/* Initialize the time here, it won't change. */
    344 	old_time = c->c_time;
    345 	c->c_time = to_ticks + hardclock_ticks;
    346 	c->c_flags &= ~CALLOUT_FIRED;
    347 
    348 	/*
    349 	 * If this timeout is already scheduled and now is moved
    350 	 * earlier, reschedule it now. Otherwise leave it in place
    351 	 * and let it be rescheduled later.
    352 	 */
    353 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    354 		if (c->c_time - old_time < 0) {
    355 			CIRCQ_REMOVE(&c->c_list);
    356 			CIRCQ_INSERT(&c->c_list, &timeout_todo);
    357 		}
    358 	} else {
    359 		c->c_flags |= CALLOUT_PENDING;
    360 		CIRCQ_INSERT(&c->c_list, &timeout_todo);
    361 	}
    362 }
    363 
    364 /*
    365  * callout_reset:
    366  *
    367  *	Reset a callout structure with a new function and argument, and
    368  *	schedule it to run.
    369  */
    370 void
    371 callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg)
    372 {
    373 	callout_impl_t *c = (callout_impl_t *)cs;
    374 
    375 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    376 
    377 	mutex_spin_enter(&callout_lock);
    378 
    379 	c->c_func = func;
    380 	c->c_arg = arg;
    381 
    382 	callout_schedule_locked(c, to_ticks);
    383 
    384 	mutex_spin_exit(&callout_lock);
    385 }
    386 
    387 /*
    388  * callout_schedule:
    389  *
    390  *	Schedule a callout to run.  The function and argument must
    391  *	already be set in the callout structure.
    392  */
    393 void
    394 callout_schedule(callout_t *cs, int to_ticks)
    395 {
    396 	callout_impl_t *c = (callout_impl_t *)cs;
    397 
    398 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    399 
    400 	mutex_spin_enter(&callout_lock);
    401 	callout_schedule_locked(c, to_ticks);
    402 	mutex_spin_exit(&callout_lock);
    403 }
    404 
    405 /*
    406  * callout_stop:
    407  *
    408  *	Cancel a pending callout.
    409  */
    410 bool
    411 callout_stop(callout_t *cs)
    412 {
    413 	callout_impl_t *c = (callout_impl_t *)cs;
    414 	bool expired;
    415 
    416 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    417 
    418 	mutex_spin_enter(&callout_lock);
    419 
    420 	if (callout_running(c))
    421 		callout_barrier(c);
    422 
    423 	if ((c->c_flags & CALLOUT_PENDING) != 0)
    424 		CIRCQ_REMOVE(&c->c_list);
    425 
    426 	expired = ((c->c_flags & CALLOUT_FIRED) != 0);
    427 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    428 
    429 	mutex_spin_exit(&callout_lock);
    430 
    431 	return expired;
    432 }
    433 
    434 void
    435 callout_setfunc(callout_t *cs, void (*func)(void *), void *arg)
    436 {
    437 	callout_impl_t *c = (callout_impl_t *)cs;
    438 
    439 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    440 
    441 	mutex_spin_enter(&callout_lock);
    442 	c->c_func = func;
    443 	c->c_arg = arg;
    444 	mutex_spin_exit(&callout_lock);
    445 }
    446 
    447 bool
    448 callout_expired(callout_t *cs)
    449 {
    450 	callout_impl_t *c = (callout_impl_t *)cs;
    451 	bool rv;
    452 
    453 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    454 
    455 	mutex_spin_enter(&callout_lock);
    456 	rv = ((c->c_flags & CALLOUT_FIRED) != 0);
    457 	mutex_spin_exit(&callout_lock);
    458 
    459 	return rv;
    460 }
    461 
    462 bool
    463 callout_active(callout_t *cs)
    464 {
    465 	callout_impl_t *c = (callout_impl_t *)cs;
    466 	bool rv;
    467 
    468 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    469 
    470 	mutex_spin_enter(&callout_lock);
    471 	rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0);
    472 	mutex_spin_exit(&callout_lock);
    473 
    474 	return rv;
    475 }
    476 
    477 bool
    478 callout_pending(callout_t *cs)
    479 {
    480 	callout_impl_t *c = (callout_impl_t *)cs;
    481 	bool rv;
    482 
    483 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    484 
    485 	mutex_spin_enter(&callout_lock);
    486 	rv = ((c->c_flags & CALLOUT_PENDING) != 0);
    487 	mutex_spin_exit(&callout_lock);
    488 
    489 	return rv;
    490 }
    491 
    492 bool
    493 callout_invoking(callout_t *cs)
    494 {
    495 	callout_impl_t *c = (callout_impl_t *)cs;
    496 	bool rv;
    497 
    498 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    499 
    500 	mutex_spin_enter(&callout_lock);
    501 	rv = ((c->c_flags & CALLOUT_INVOKING) != 0);
    502 	mutex_spin_exit(&callout_lock);
    503 
    504 	return rv;
    505 }
    506 
    507 void
    508 callout_ack(callout_t *cs)
    509 {
    510 	callout_impl_t *c = (callout_impl_t *)cs;
    511 
    512 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    513 
    514 	mutex_spin_enter(&callout_lock);
    515 	c->c_flags &= ~CALLOUT_INVOKING;
    516 	mutex_spin_exit(&callout_lock);
    517 }
    518 
    519 /*
    520  * This is called from hardclock() once every tick.
    521  * We schedule callout_softclock() if there is work
    522  * to be done.
    523  */
    524 void
    525 callout_hardclock(void)
    526 {
    527 	int needsoftclock;
    528 
    529 	mutex_spin_enter(&callout_lock);
    530 
    531 	MOVEBUCKET(0, hardclock_ticks);
    532 	if (MASKWHEEL(0, hardclock_ticks) == 0) {
    533 		MOVEBUCKET(1, hardclock_ticks);
    534 		if (MASKWHEEL(1, hardclock_ticks) == 0) {
    535 			MOVEBUCKET(2, hardclock_ticks);
    536 			if (MASKWHEEL(2, hardclock_ticks) == 0)
    537 				MOVEBUCKET(3, hardclock_ticks);
    538 		}
    539 	}
    540 
    541 	needsoftclock = !CIRCQ_EMPTY(&timeout_todo);
    542 	mutex_spin_exit(&callout_lock);
    543 
    544 	if (needsoftclock)
    545 		softint_schedule(callout_si);
    546 }
    547 
    548 /* ARGSUSED */
    549 static void
    550 callout_softclock(void *v)
    551 {
    552 	callout_impl_t *c;
    553 	struct cpu_info *ci;
    554 	void (*func)(void *);
    555 	void *arg;
    556 	u_int mpsafe, count;
    557 	lwp_t *l;
    558 
    559 	l = curlwp;
    560 	ci = l->l_cpu;
    561 
    562 	mutex_spin_enter(&callout_lock);
    563 
    564 	while (!CIRCQ_EMPTY(&timeout_todo)) {
    565 		c = CIRCQ_FIRST(&timeout_todo);
    566 		KASSERT(c->c_magic == CALLOUT_MAGIC);
    567 		KASSERT(c->c_func != NULL);
    568 		KASSERT((c->c_flags & CALLOUT_PENDING) != 0);
    569 		KASSERT((c->c_flags & CALLOUT_FIRED) == 0);
    570 		CIRCQ_REMOVE(&c->c_list);
    571 
    572 		/* If due run it, otherwise insert it into the right bucket. */
    573 		if (c->c_time - hardclock_ticks > 0) {
    574 			CIRCQ_INSERT(&c->c_list,
    575 			    BUCKET((c->c_time - hardclock_ticks), c->c_time));
    576 		} else {
    577 			if (c->c_time - hardclock_ticks < 0)
    578 				callout_ev_late.ev_count++;
    579 
    580 			c->c_flags ^= (CALLOUT_PENDING | CALLOUT_FIRED);
    581 			mpsafe = (c->c_flags & CALLOUT_MPSAFE);
    582 			func = c->c_func;
    583 			arg = c->c_arg;
    584 			c->c_oncpu = ci;
    585 			c->c_onlwp = l;
    586 
    587 			mutex_spin_exit(&callout_lock);
    588 			if (!mpsafe) {
    589 				KERNEL_LOCK(1, curlwp);
    590 				if (ci->ci_data.cpu_callout_cancel != c)
    591 					(*func)(arg);
    592 				KERNEL_UNLOCK_ONE(curlwp);
    593 			} else
    594 					(*func)(arg);
    595 			mutex_spin_enter(&callout_lock);
    596 
    597 			/*
    598 			 * We can't touch 'c' here because it might be
    599 			 * freed already.  If LWPs waiting for callout
    600 			 * to complete, awaken them.
    601 			 */
    602 			ci->ci_data.cpu_callout_cancel = NULL;
    603 			ci->ci_data.cpu_callout = NULL;
    604 			if ((count = ci->ci_data.cpu_callout_nwait) != 0) {
    605 				ci->ci_data.cpu_callout_nwait = 0;
    606 				/* sleepq_wake() drops the lock. */
    607 				sleepq_wake(&callout_sleepq, ci, count);
    608 				mutex_spin_enter(&callout_lock);
    609 			}
    610 		}
    611 	}
    612 
    613 	mutex_spin_exit(&callout_lock);
    614 }
    615 
    616 #ifdef DDB
    617 static void
    618 db_show_callout_bucket(struct callout_circq *bucket)
    619 {
    620 	callout_impl_t *c;
    621 	db_expr_t offset;
    622 	const char *name;
    623 	static char question[] = "?";
    624 
    625 	if (CIRCQ_EMPTY(bucket))
    626 		return;
    627 
    628 	for (c = CIRCQ_FIRST(bucket); /*nothing*/; c = CIRCQ_NEXT(&c->c_list)) {
    629 		db_find_sym_and_offset((db_addr_t)(intptr_t)c->c_func, &name,
    630 		    &offset);
    631 		name = name ? name : question;
    632 #ifdef _LP64
    633 #define	POINTER_WIDTH	"%16lx"
    634 #else
    635 #define	POINTER_WIDTH	"%8lx"
    636 #endif
    637 		db_printf("%9d %2d/%-4d " POINTER_WIDTH "  %s\n",
    638 		    c->c_time - hardclock_ticks,
    639 		    (int)((bucket - timeout_wheel) / WHEELSIZE),
    640 		    (int)(bucket - timeout_wheel), (u_long) c->c_arg, name);
    641 
    642 		if (CIRCQ_LAST(&c->c_list, bucket))
    643 			break;
    644 	}
    645 }
    646 
    647 void
    648 db_show_callout(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    649 {
    650 	int b;
    651 
    652 	db_printf("hardclock_ticks now: %d\n", hardclock_ticks);
    653 #ifdef _LP64
    654 	db_printf("    ticks  wheel               arg  func\n");
    655 #else
    656 	db_printf("    ticks  wheel       arg  func\n");
    657 #endif
    658 
    659 	/*
    660 	 * Don't lock the callwheel; all the other CPUs are paused
    661 	 * anyhow, and we might be called in a circumstance where
    662 	 * some other CPU was paused while holding the lock.
    663 	 */
    664 
    665 	db_show_callout_bucket(&timeout_todo);
    666 	for (b = 0; b < BUCKETS; b++)
    667 		db_show_callout_bucket(&timeout_wheel[b]);
    668 }
    669 #endif /* DDB */
    670