Home | History | Annotate | Line # | Download | only in kern
kern_timeout.c revision 1.32
      1 /*	$NetBSD: kern_timeout.c,v 1.32 2008/03/28 20:44:39 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.32 2008/03/28 20:44:39 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 
    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_startup:
    190  *
    191  *	Initialize the callout facility, called at system startup time.
    192  */
    193 void
    194 callout_startup(void)
    195 {
    196 	int b;
    197 
    198 	KASSERT(sizeof(callout_impl_t) <= sizeof(callout_t));
    199 
    200 	CIRCQ_INIT(&timeout_todo);
    201 	for (b = 0; b < BUCKETS; b++)
    202 		CIRCQ_INIT(&timeout_wheel[b]);
    203 
    204 	mutex_init(&callout_lock, MUTEX_DEFAULT, IPL_SCHED);
    205 	sleepq_init(&callout_sleepq, &callout_lock);
    206 
    207 	evcnt_attach_dynamic(&callout_ev_late, EVCNT_TYPE_MISC,
    208 	    NULL, "callout", "late");
    209 	evcnt_attach_dynamic(&callout_ev_block, EVCNT_TYPE_MISC,
    210 	    NULL, "callout", "wait for completion");
    211 }
    212 
    213 /*
    214  * callout_startup2:
    215  *
    216  *	Complete initialization once soft interrupts are available.
    217  */
    218 void
    219 callout_startup2(void)
    220 {
    221 
    222 	callout_si = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    223 	    callout_softclock, NULL);
    224 	if (callout_si == NULL)
    225 		panic("callout_startup2: unable to register softclock intr");
    226 }
    227 
    228 /*
    229  * callout_init:
    230  *
    231  *	Initialize a callout structure.
    232  */
    233 void
    234 callout_init(callout_t *cs, u_int flags)
    235 {
    236 	callout_impl_t *c = (callout_impl_t *)cs;
    237 
    238 	KASSERT((flags & ~CALLOUT_FLAGMASK) == 0);
    239 
    240 	memset(c, 0, sizeof(*c));
    241 	c->c_flags = flags;
    242 	c->c_magic = CALLOUT_MAGIC;
    243 }
    244 
    245 /*
    246  * callout_destroy:
    247  *
    248  *	Destroy a callout structure.  The callout must be stopped.
    249  */
    250 void
    251 callout_destroy(callout_t *cs)
    252 {
    253 	callout_impl_t *c = (callout_impl_t *)cs;
    254 
    255 	/*
    256 	 * It's not necessary to lock in order to see the correct value
    257 	 * of c->c_flags.  If the callout could potentially have been
    258 	 * running, the current thread should have stopped it.
    259 	 */
    260 	KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
    261 	if (c->c_oncpu != NULL) {
    262 		KASSERT(
    263 		    ((struct cpu_info *)c->c_oncpu)->ci_data.cpu_callout != c);
    264 	}
    265 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    266 
    267 	c->c_magic = 0;
    268 }
    269 
    270 /*
    271  * callout_schedule_locked:
    272  *
    273  *	Schedule a callout to run.  The function and argument must
    274  *	already be set in the callout structure.  Must be called with
    275  *	callout_lock.
    276  */
    277 static void
    278 callout_schedule_locked(callout_impl_t *c, int to_ticks)
    279 {
    280 	int old_time;
    281 
    282 	KASSERT(to_ticks >= 0);
    283 	KASSERT(c->c_func != NULL);
    284 
    285 	/* Initialize the time here, it won't change. */
    286 	old_time = c->c_time;
    287 	c->c_time = to_ticks + hardclock_ticks;
    288 	c->c_flags &= ~CALLOUT_FIRED;
    289 
    290 	/*
    291 	 * If this timeout is already scheduled and now is moved
    292 	 * earlier, reschedule it now. Otherwise leave it in place
    293 	 * and let it be rescheduled later.
    294 	 */
    295 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    296 		if (c->c_time - old_time < 0) {
    297 			CIRCQ_REMOVE(&c->c_list);
    298 			CIRCQ_INSERT(&c->c_list, &timeout_todo);
    299 		}
    300 	} else {
    301 		c->c_flags |= CALLOUT_PENDING;
    302 		CIRCQ_INSERT(&c->c_list, &timeout_todo);
    303 	}
    304 }
    305 
    306 /*
    307  * callout_reset:
    308  *
    309  *	Reset a callout structure with a new function and argument, and
    310  *	schedule it to run.
    311  */
    312 void
    313 callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg)
    314 {
    315 	callout_impl_t *c = (callout_impl_t *)cs;
    316 
    317 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    318 
    319 	mutex_spin_enter(&callout_lock);
    320 
    321 	c->c_func = func;
    322 	c->c_arg = arg;
    323 
    324 	callout_schedule_locked(c, to_ticks);
    325 
    326 	mutex_spin_exit(&callout_lock);
    327 }
    328 
    329 /*
    330  * callout_schedule:
    331  *
    332  *	Schedule a callout to run.  The function and argument must
    333  *	already be set in the callout structure.
    334  */
    335 void
    336 callout_schedule(callout_t *cs, int to_ticks)
    337 {
    338 	callout_impl_t *c = (callout_impl_t *)cs;
    339 
    340 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    341 
    342 	mutex_spin_enter(&callout_lock);
    343 	callout_schedule_locked(c, to_ticks);
    344 	mutex_spin_exit(&callout_lock);
    345 }
    346 
    347 
    348 /*
    349  * callout_stop:
    350  *
    351  *	Try to cancel a pending callout.
    352  */
    353 bool
    354 callout_stop(callout_t *cs)
    355 {
    356 	callout_impl_t *c = (callout_impl_t *)cs;
    357 	struct cpu_info *ci;
    358 	bool expired;
    359 
    360 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    361 
    362 	mutex_spin_enter(&callout_lock);
    363 
    364 	if ((c->c_flags & CALLOUT_PENDING) != 0)
    365 		CIRCQ_REMOVE(&c->c_list);
    366 	expired = ((c->c_flags & CALLOUT_FIRED) != 0);
    367 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    368 
    369 	if ((ci = c->c_oncpu) != NULL && ci->ci_data.cpu_callout == c) {
    370 		/*
    371 		 * This is for non-MPSAFE callouts only.  To synchronize
    372 		 * effectively we must be called with kernel_lock held.
    373 		 * It's also taken in callout_softclock.
    374 		 */
    375 		ci = c->c_oncpu;
    376 		ci->ci_data.cpu_callout_cancel = c;
    377 	}
    378 
    379 	mutex_spin_exit(&callout_lock);
    380 
    381 	return expired;
    382 }
    383 
    384 /*
    385  * callout_halt:
    386  *
    387  *	Cancel a pending callout.  If in-flight, block until it completes.
    388  *	May not be called from a hard interrupt handler.
    389  */
    390 bool
    391 callout_halt(callout_t *cs)
    392 {
    393 	callout_impl_t *c = (callout_impl_t *)cs;
    394 	extern syncobj_t sleep_syncobj;
    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