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