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