Home | History | Annotate | Line # | Download | only in kern
kern_timeout.c revision 1.28
      1 /*	$NetBSD: kern_timeout.c,v 1.28 2007/11/06 00:42:43 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.28 2007/11/06 00:42:43 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 #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 /*
    331  * callout_reset:
    332  *
    333  *	Reset a callout structure with a new function and argument, and
    334  *	schedule it to run.
    335  */
    336 void
    337 callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg)
    338 {
    339 	callout_impl_t *c = (callout_impl_t *)cs;
    340 	int old_time;
    341 
    342 	KASSERT(to_ticks >= 0);
    343 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    344 	KASSERT(func != NULL);
    345 
    346 	mutex_spin_enter(&callout_lock);
    347 
    348 	/* Initialize the time here, it won't change. */
    349 	old_time = c->c_time;
    350 	c->c_time = to_ticks + hardclock_ticks;
    351 	c->c_flags &= ~CALLOUT_FIRED;
    352 
    353 	c->c_func = func;
    354 	c->c_arg = arg;
    355 
    356 	/*
    357 	 * If this timeout is already scheduled and now is moved
    358 	 * earlier, reschedule it now. Otherwise leave it in place
    359 	 * and let it be rescheduled later.
    360 	 */
    361 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    362 		if (c->c_time - old_time < 0) {
    363 			CIRCQ_REMOVE(&c->c_list);
    364 			CIRCQ_INSERT(&c->c_list, &timeout_todo);
    365 		}
    366 	} else {
    367 		c->c_flags |= CALLOUT_PENDING;
    368 		CIRCQ_INSERT(&c->c_list, &timeout_todo);
    369 	}
    370 
    371 	mutex_spin_exit(&callout_lock);
    372 }
    373 
    374 /*
    375  * callout_schedule:
    376  *
    377  *	Schedule a callout to run.  The function and argument must
    378  *	already be set in the callout structure.
    379  */
    380 void
    381 callout_schedule(callout_t *cs, int to_ticks)
    382 {
    383 	callout_impl_t *c = (callout_impl_t *)cs;
    384 	int old_time;
    385 
    386 	KASSERT(to_ticks >= 0);
    387 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    388 	KASSERT(c->c_func != NULL);
    389 
    390 	mutex_spin_enter(&callout_lock);
    391 
    392 	/* Initialize the time here, it won't change. */
    393 	old_time = c->c_time;
    394 	c->c_time = to_ticks + hardclock_ticks;
    395 	c->c_flags &= ~CALLOUT_FIRED;
    396 
    397 	/*
    398 	 * If this timeout is already scheduled and now is moved
    399 	 * earlier, reschedule it now. Otherwise leave it in place
    400 	 * and let it be rescheduled later.
    401 	 */
    402 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    403 		if (c->c_time - old_time < 0) {
    404 			CIRCQ_REMOVE(&c->c_list);
    405 			CIRCQ_INSERT(&c->c_list, &timeout_todo);
    406 		}
    407 	} else {
    408 		c->c_flags |= CALLOUT_PENDING;
    409 		CIRCQ_INSERT(&c->c_list, &timeout_todo);
    410 	}
    411 
    412 	mutex_spin_exit(&callout_lock);
    413 }
    414 
    415 /*
    416  * callout_stop:
    417  *
    418  *	Cancel a pending callout.
    419  */
    420 bool
    421 callout_stop(callout_t *cs)
    422 {
    423 	callout_impl_t *c = (callout_impl_t *)cs;
    424 	bool expired;
    425 
    426 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    427 
    428 	mutex_spin_enter(&callout_lock);
    429 
    430 	if (callout_running(c))
    431 		callout_barrier(c);
    432 
    433 	if ((c->c_flags & CALLOUT_PENDING) != 0)
    434 		CIRCQ_REMOVE(&c->c_list);
    435 
    436 	expired = ((c->c_flags & CALLOUT_FIRED) != 0);
    437 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    438 
    439 	mutex_spin_exit(&callout_lock);
    440 
    441 	return expired;
    442 }
    443 
    444 void
    445 callout_setfunc(callout_t *cs, void (*func)(void *), void *arg)
    446 {
    447 	callout_impl_t *c = (callout_impl_t *)cs;
    448 
    449 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    450 
    451 	mutex_spin_enter(&callout_lock);
    452 	c->c_func = func;
    453 	c->c_arg = arg;
    454 	mutex_spin_exit(&callout_lock);
    455 }
    456 
    457 bool
    458 callout_expired(callout_t *cs)
    459 {
    460 	callout_impl_t *c = (callout_impl_t *)cs;
    461 	bool rv;
    462 
    463 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    464 
    465 	mutex_spin_enter(&callout_lock);
    466 	rv = ((c->c_flags & CALLOUT_FIRED) != 0);
    467 	mutex_spin_exit(&callout_lock);
    468 
    469 	return rv;
    470 }
    471 
    472 bool
    473 callout_active(callout_t *cs)
    474 {
    475 	callout_impl_t *c = (callout_impl_t *)cs;
    476 	bool rv;
    477 
    478 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    479 
    480 	mutex_spin_enter(&callout_lock);
    481 	rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0);
    482 	mutex_spin_exit(&callout_lock);
    483 
    484 	return rv;
    485 }
    486 
    487 bool
    488 callout_pending(callout_t *cs)
    489 {
    490 	callout_impl_t *c = (callout_impl_t *)cs;
    491 	bool rv;
    492 
    493 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    494 
    495 	mutex_spin_enter(&callout_lock);
    496 	rv = ((c->c_flags & CALLOUT_PENDING) != 0);
    497 	mutex_spin_exit(&callout_lock);
    498 
    499 	return rv;
    500 }
    501 
    502 bool
    503 callout_invoking(callout_t *cs)
    504 {
    505 	callout_impl_t *c = (callout_impl_t *)cs;
    506 	bool rv;
    507 
    508 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    509 
    510 	mutex_spin_enter(&callout_lock);
    511 	rv = ((c->c_flags & CALLOUT_INVOKING) != 0);
    512 	mutex_spin_exit(&callout_lock);
    513 
    514 	return rv;
    515 }
    516 
    517 void
    518 callout_ack(callout_t *cs)
    519 {
    520 	callout_impl_t *c = (callout_impl_t *)cs;
    521 
    522 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    523 
    524 	mutex_spin_enter(&callout_lock);
    525 	c->c_flags &= ~CALLOUT_INVOKING;
    526 	mutex_spin_exit(&callout_lock);
    527 }
    528 
    529 /*
    530  * This is called from hardclock() once every tick.
    531  * We schedule callout_softclock() if there is work
    532  * to be done.
    533  */
    534 void
    535 callout_hardclock(void)
    536 {
    537 	int needsoftclock;
    538 
    539 	mutex_spin_enter(&callout_lock);
    540 
    541 	MOVEBUCKET(0, hardclock_ticks);
    542 	if (MASKWHEEL(0, hardclock_ticks) == 0) {
    543 		MOVEBUCKET(1, hardclock_ticks);
    544 		if (MASKWHEEL(1, hardclock_ticks) == 0) {
    545 			MOVEBUCKET(2, hardclock_ticks);
    546 			if (MASKWHEEL(2, hardclock_ticks) == 0)
    547 				MOVEBUCKET(3, hardclock_ticks);
    548 		}
    549 	}
    550 
    551 	needsoftclock = !CIRCQ_EMPTY(&timeout_todo);
    552 	mutex_spin_exit(&callout_lock);
    553 
    554 	if (needsoftclock)
    555 		softint_schedule(callout_si);
    556 }
    557 
    558 /* ARGSUSED */
    559 static void
    560 callout_softclock(void *v)
    561 {
    562 	callout_impl_t *c;
    563 	struct cpu_info *ci;
    564 	void (*func)(void *);
    565 	void *arg;
    566 	u_int mpsafe, count;
    567 	lwp_t *l;
    568 
    569 	l = curlwp;
    570 	ci = l->l_cpu;
    571 
    572 	mutex_spin_enter(&callout_lock);
    573 
    574 	while (!CIRCQ_EMPTY(&timeout_todo)) {
    575 		c = CIRCQ_FIRST(&timeout_todo);
    576 		KASSERT(c->c_magic == CALLOUT_MAGIC);
    577 		KASSERT(c->c_func != NULL);
    578 		KASSERT((c->c_flags & CALLOUT_PENDING) != 0);
    579 		KASSERT((c->c_flags & CALLOUT_FIRED) == 0);
    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