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