Home | History | Annotate | Line # | Download | only in kern
kern_timeout.c revision 1.65
      1 /*	$NetBSD: kern_timeout.c,v 1.65 2020/06/02 02:04:35 rin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003, 2006, 2007, 2008, 2009, 2019 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 2001 Thomas Nordin <nordin (at) openbsd.org>
     34  * Copyright (c) 2000-2001 Artur Grabowski <art (at) openbsd.org>
     35  * All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  *
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. The name of the author may not be used to endorse or promote products
     47  *    derived from this software without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     50  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     51  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     52  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     53  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     55  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     56  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     57  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     58  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 __KERNEL_RCSID(0, "$NetBSD: kern_timeout.c,v 1.65 2020/06/02 02:04:35 rin Exp $");
     63 
     64 /*
     65  * Timeouts are kept in a hierarchical timing wheel.  The c_time is the
     66  * value of c_cpu->cc_ticks when the timeout should be called.  There are
     67  * four levels with 256 buckets each. See 'Scheme 7' in "Hashed and
     68  * Hierarchical Timing Wheels: Efficient Data Structures for Implementing
     69  * a Timer Facility" by George Varghese and Tony Lauck.
     70  *
     71  * Some of the "math" in here is a bit tricky.  We have to beware of
     72  * wrapping ints.
     73  *
     74  * We use the fact that any element added to the queue must be added with
     75  * a positive time.  That means that any element `to' on the queue cannot
     76  * be scheduled to timeout further in time than INT_MAX, but c->c_time can
     77  * be positive or negative so comparing it with anything is dangerous.
     78  * The only way we can use the c->c_time value in any predictable way is
     79  * when we calculate how far in the future `to' will timeout - "c->c_time
     80  * - c->c_cpu->cc_ticks".  The result will always be positive for future
     81  * timeouts and 0 or negative for due timeouts.
     82  */
     83 
     84 #define	_CALLOUT_PRIVATE
     85 
     86 #include <sys/param.h>
     87 #include <sys/systm.h>
     88 #include <sys/kernel.h>
     89 #include <sys/callout.h>
     90 #include <sys/lwp.h>
     91 #include <sys/mutex.h>
     92 #include <sys/proc.h>
     93 #include <sys/sleepq.h>
     94 #include <sys/syncobj.h>
     95 #include <sys/evcnt.h>
     96 #include <sys/intr.h>
     97 #include <sys/cpu.h>
     98 #include <sys/kmem.h>
     99 
    100 #ifdef DDB
    101 #include <machine/db_machdep.h>
    102 #include <ddb/db_interface.h>
    103 #include <ddb/db_access.h>
    104 #include <ddb/db_cpu.h>
    105 #include <ddb/db_sym.h>
    106 #include <ddb/db_output.h>
    107 #endif
    108 
    109 #define BUCKETS		1024
    110 #define WHEELSIZE	256
    111 #define WHEELMASK	255
    112 #define WHEELBITS	8
    113 
    114 #define MASKWHEEL(wheel, time) (((time) >> ((wheel)*WHEELBITS)) & WHEELMASK)
    115 
    116 #define BUCKET(cc, rel, abs)						\
    117     (((rel) <= (1 << (2*WHEELBITS)))					\
    118     	? ((rel) <= (1 << WHEELBITS))					\
    119             ? &(cc)->cc_wheel[MASKWHEEL(0, (abs))]			\
    120             : &(cc)->cc_wheel[MASKWHEEL(1, (abs)) + WHEELSIZE]		\
    121         : ((rel) <= (1 << (3*WHEELBITS)))				\
    122             ? &(cc)->cc_wheel[MASKWHEEL(2, (abs)) + 2*WHEELSIZE]	\
    123             : &(cc)->cc_wheel[MASKWHEEL(3, (abs)) + 3*WHEELSIZE])
    124 
    125 #define MOVEBUCKET(cc, wheel, time)					\
    126     CIRCQ_APPEND(&(cc)->cc_todo,					\
    127         &(cc)->cc_wheel[MASKWHEEL((wheel), (time)) + (wheel)*WHEELSIZE])
    128 
    129 /*
    130  * Circular queue definitions.
    131  */
    132 
    133 #define CIRCQ_INIT(list)						\
    134 do {									\
    135         (list)->cq_next_l = (list);					\
    136         (list)->cq_prev_l = (list);					\
    137 } while (/*CONSTCOND*/0)
    138 
    139 #define CIRCQ_INSERT(elem, list)					\
    140 do {									\
    141         (elem)->cq_prev_e = (list)->cq_prev_e;				\
    142         (elem)->cq_next_l = (list);					\
    143         (list)->cq_prev_l->cq_next_l = (elem);				\
    144         (list)->cq_prev_l = (elem);					\
    145 } while (/*CONSTCOND*/0)
    146 
    147 #define CIRCQ_APPEND(fst, snd)						\
    148 do {									\
    149         if (!CIRCQ_EMPTY(snd)) {					\
    150                 (fst)->cq_prev_l->cq_next_l = (snd)->cq_next_l;		\
    151                 (snd)->cq_next_l->cq_prev_l = (fst)->cq_prev_l;		\
    152                 (snd)->cq_prev_l->cq_next_l = (fst);			\
    153                 (fst)->cq_prev_l = (snd)->cq_prev_l;			\
    154                 CIRCQ_INIT(snd);					\
    155         }								\
    156 } while (/*CONSTCOND*/0)
    157 
    158 #define CIRCQ_REMOVE(elem)						\
    159 do {									\
    160         (elem)->cq_next_l->cq_prev_e = (elem)->cq_prev_e;		\
    161         (elem)->cq_prev_l->cq_next_e = (elem)->cq_next_e;		\
    162 } while (/*CONSTCOND*/0)
    163 
    164 #define CIRCQ_FIRST(list)	((list)->cq_next_e)
    165 #define CIRCQ_NEXT(elem)	((elem)->cq_next_e)
    166 #define CIRCQ_LAST(elem,list)	((elem)->cq_next_l == (list))
    167 #define CIRCQ_EMPTY(list)	((list)->cq_next_l == (list))
    168 
    169 struct callout_cpu {
    170 	kmutex_t	*cc_lock;
    171 	sleepq_t	cc_sleepq;
    172 	u_int		cc_nwait;
    173 	u_int		cc_ticks;
    174 	lwp_t		*cc_lwp;
    175 	callout_impl_t	*cc_active;
    176 	callout_impl_t	*cc_cancel;
    177 	struct evcnt	cc_ev_late;
    178 	struct evcnt	cc_ev_block;
    179 	struct callout_circq cc_todo;		/* Worklist */
    180 	struct callout_circq cc_wheel[BUCKETS];	/* Queues of timeouts */
    181 	char		cc_name1[12];
    182 	char		cc_name2[12];
    183 };
    184 
    185 #ifdef DDB
    186 static struct callout_cpu ccb;
    187 static struct cpu_info cib;
    188 #endif
    189 
    190 #ifndef CRASH /* _KERNEL */
    191 static void	callout_softclock(void *);
    192 static void	callout_wait(callout_impl_t *, void *, kmutex_t *);
    193 
    194 static struct callout_cpu callout_cpu0 __cacheline_aligned;
    195 static void *callout_sih __read_mostly;
    196 
    197 static inline kmutex_t *
    198 callout_lock(callout_impl_t *c)
    199 {
    200 	struct callout_cpu *cc;
    201 	kmutex_t *lock;
    202 
    203 	for (;;) {
    204 		cc = c->c_cpu;
    205 		lock = cc->cc_lock;
    206 		mutex_spin_enter(lock);
    207 		if (__predict_true(cc == c->c_cpu))
    208 			return lock;
    209 		mutex_spin_exit(lock);
    210 	}
    211 }
    212 
    213 /*
    214  * callout_startup:
    215  *
    216  *	Initialize the callout facility, called at system startup time.
    217  *	Do just enough to allow callouts to be safely registered.
    218  */
    219 void
    220 callout_startup(void)
    221 {
    222 	struct callout_cpu *cc;
    223 	int b;
    224 
    225 	KASSERT(curcpu()->ci_data.cpu_callout == NULL);
    226 
    227 	cc = &callout_cpu0;
    228 	cc->cc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
    229 	CIRCQ_INIT(&cc->cc_todo);
    230 	for (b = 0; b < BUCKETS; b++)
    231 		CIRCQ_INIT(&cc->cc_wheel[b]);
    232 	curcpu()->ci_data.cpu_callout = cc;
    233 }
    234 
    235 /*
    236  * callout_init_cpu:
    237  *
    238  *	Per-CPU initialization.
    239  */
    240 CTASSERT(sizeof(callout_impl_t) <= sizeof(callout_t));
    241 
    242 void
    243 callout_init_cpu(struct cpu_info *ci)
    244 {
    245 	struct callout_cpu *cc;
    246 	int b;
    247 
    248 	if ((cc = ci->ci_data.cpu_callout) == NULL) {
    249 		cc = kmem_zalloc(sizeof(*cc), KM_SLEEP);
    250 		cc->cc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
    251 		CIRCQ_INIT(&cc->cc_todo);
    252 		for (b = 0; b < BUCKETS; b++)
    253 			CIRCQ_INIT(&cc->cc_wheel[b]);
    254 	} else {
    255 		/* Boot CPU, one time only. */
    256 		callout_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    257 		    callout_softclock, NULL);
    258 		if (callout_sih == NULL)
    259 			panic("callout_init_cpu (2)");
    260 	}
    261 
    262 	sleepq_init(&cc->cc_sleepq);
    263 
    264 	snprintf(cc->cc_name1, sizeof(cc->cc_name1), "late/%u",
    265 	    cpu_index(ci));
    266 	evcnt_attach_dynamic(&cc->cc_ev_late, EVCNT_TYPE_MISC,
    267 	    NULL, "callout", cc->cc_name1);
    268 
    269 	snprintf(cc->cc_name2, sizeof(cc->cc_name2), "wait/%u",
    270 	    cpu_index(ci));
    271 	evcnt_attach_dynamic(&cc->cc_ev_block, EVCNT_TYPE_MISC,
    272 	    NULL, "callout", cc->cc_name2);
    273 
    274 	ci->ci_data.cpu_callout = cc;
    275 }
    276 
    277 /*
    278  * callout_init:
    279  *
    280  *	Initialize a callout structure.  This must be quick, so we fill
    281  *	only the minimum number of fields.
    282  */
    283 void
    284 callout_init(callout_t *cs, u_int flags)
    285 {
    286 	callout_impl_t *c = (callout_impl_t *)cs;
    287 	struct callout_cpu *cc;
    288 
    289 	KASSERT((flags & ~CALLOUT_FLAGMASK) == 0);
    290 
    291 	cc = curcpu()->ci_data.cpu_callout;
    292 	c->c_func = NULL;
    293 	c->c_magic = CALLOUT_MAGIC;
    294 	if (__predict_true((flags & CALLOUT_MPSAFE) != 0 && cc != NULL)) {
    295 		c->c_flags = flags;
    296 		c->c_cpu = cc;
    297 		return;
    298 	}
    299 	c->c_flags = flags | CALLOUT_BOUND;
    300 	c->c_cpu = &callout_cpu0;
    301 }
    302 
    303 /*
    304  * callout_destroy:
    305  *
    306  *	Destroy a callout structure.  The callout must be stopped.
    307  */
    308 void
    309 callout_destroy(callout_t *cs)
    310 {
    311 	callout_impl_t *c = (callout_impl_t *)cs;
    312 
    313 	KASSERTMSG(c->c_magic == CALLOUT_MAGIC,
    314 	    "callout %p: c_magic (%#x) != CALLOUT_MAGIC (%#x)",
    315 	    c, c->c_magic, CALLOUT_MAGIC);
    316 	/*
    317 	 * It's not necessary to lock in order to see the correct value
    318 	 * of c->c_flags.  If the callout could potentially have been
    319 	 * running, the current thread should have stopped it.
    320 	 */
    321 	KASSERTMSG((c->c_flags & CALLOUT_PENDING) == 0,
    322 	    "pending callout %p: c_func (%p) c_flags (%#x) destroyed from %p",
    323 	    c, c->c_func, c->c_flags, __builtin_return_address(0));
    324 	KASSERTMSG(c->c_cpu->cc_lwp == curlwp || c->c_cpu->cc_active != c,
    325 	    "running callout %p: c_func (%p) c_flags (%#x) destroyed from %p",
    326 	    c, c->c_func, c->c_flags, __builtin_return_address(0));
    327 	c->c_magic = 0;
    328 }
    329 
    330 /*
    331  * callout_schedule_locked:
    332  *
    333  *	Schedule a callout to run.  The function and argument must
    334  *	already be set in the callout structure.  Must be called with
    335  *	callout_lock.
    336  */
    337 static void
    338 callout_schedule_locked(callout_impl_t *c, kmutex_t *lock, int to_ticks)
    339 {
    340 	struct callout_cpu *cc, *occ;
    341 	int old_time;
    342 
    343 	KASSERT(to_ticks >= 0);
    344 	KASSERT(c->c_func != NULL);
    345 
    346 	/* Initialize the time here, it won't change. */
    347 	occ = c->c_cpu;
    348 	c->c_flags &= ~(CALLOUT_FIRED | CALLOUT_INVOKING);
    349 
    350 	/*
    351 	 * If this timeout is already scheduled and now is moved
    352 	 * earlier, reschedule it now.  Otherwise leave it in place
    353 	 * and let it be rescheduled later.
    354 	 */
    355 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    356 		/* Leave on existing CPU. */
    357 		old_time = c->c_time;
    358 		c->c_time = to_ticks + occ->cc_ticks;
    359 		if (c->c_time - old_time < 0) {
    360 			CIRCQ_REMOVE(&c->c_list);
    361 			CIRCQ_INSERT(&c->c_list, &occ->cc_todo);
    362 		}
    363 		mutex_spin_exit(lock);
    364 		return;
    365 	}
    366 
    367 	cc = curcpu()->ci_data.cpu_callout;
    368 	if ((c->c_flags & CALLOUT_BOUND) != 0 || cc == occ ||
    369 	    !mutex_tryenter(cc->cc_lock)) {
    370 		/* Leave on existing CPU. */
    371 		c->c_time = to_ticks + occ->cc_ticks;
    372 		c->c_flags |= CALLOUT_PENDING;
    373 		CIRCQ_INSERT(&c->c_list, &occ->cc_todo);
    374 	} else {
    375 		/* Move to this CPU. */
    376 		c->c_cpu = cc;
    377 		c->c_time = to_ticks + cc->cc_ticks;
    378 		c->c_flags |= CALLOUT_PENDING;
    379 		CIRCQ_INSERT(&c->c_list, &cc->cc_todo);
    380 		mutex_spin_exit(cc->cc_lock);
    381 	}
    382 	mutex_spin_exit(lock);
    383 }
    384 
    385 /*
    386  * callout_reset:
    387  *
    388  *	Reset a callout structure with a new function and argument, and
    389  *	schedule it to run.
    390  */
    391 void
    392 callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg)
    393 {
    394 	callout_impl_t *c = (callout_impl_t *)cs;
    395 	kmutex_t *lock;
    396 
    397 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    398 	KASSERT(func != NULL);
    399 
    400 	lock = callout_lock(c);
    401 	c->c_func = func;
    402 	c->c_arg = arg;
    403 	callout_schedule_locked(c, lock, to_ticks);
    404 }
    405 
    406 /*
    407  * callout_schedule:
    408  *
    409  *	Schedule a callout to run.  The function and argument must
    410  *	already be set in the callout structure.
    411  */
    412 void
    413 callout_schedule(callout_t *cs, int to_ticks)
    414 {
    415 	callout_impl_t *c = (callout_impl_t *)cs;
    416 	kmutex_t *lock;
    417 
    418 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    419 
    420 	lock = callout_lock(c);
    421 	callout_schedule_locked(c, lock, to_ticks);
    422 }
    423 
    424 /*
    425  * callout_stop:
    426  *
    427  *	Try to cancel a pending callout.  It may be too late: the callout
    428  *	could be running on another CPU.  If called from interrupt context,
    429  *	the callout could already be in progress at a lower priority.
    430  */
    431 bool
    432 callout_stop(callout_t *cs)
    433 {
    434 	callout_impl_t *c = (callout_impl_t *)cs;
    435 	struct callout_cpu *cc;
    436 	kmutex_t *lock;
    437 	bool expired;
    438 
    439 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    440 
    441 	lock = callout_lock(c);
    442 
    443 	if ((c->c_flags & CALLOUT_PENDING) != 0)
    444 		CIRCQ_REMOVE(&c->c_list);
    445 	expired = ((c->c_flags & CALLOUT_FIRED) != 0);
    446 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    447 
    448 	cc = c->c_cpu;
    449 	if (cc->cc_active == c) {
    450 		/*
    451 		 * This is for non-MPSAFE callouts only.  To synchronize
    452 		 * effectively we must be called with kernel_lock held.
    453 		 * It's also taken in callout_softclock.
    454 		 */
    455 		cc->cc_cancel = c;
    456 	}
    457 
    458 	mutex_spin_exit(lock);
    459 
    460 	return expired;
    461 }
    462 
    463 /*
    464  * callout_halt:
    465  *
    466  *	Cancel a pending callout.  If in-flight, block until it completes.
    467  *	May not be called from a hard interrupt handler.  If the callout
    468  * 	can take locks, the caller of callout_halt() must not hold any of
    469  *	those locks, otherwise the two could deadlock.  If 'interlock' is
    470  *	non-NULL and we must wait for the callout to complete, it will be
    471  *	released and re-acquired before returning.
    472  */
    473 bool
    474 callout_halt(callout_t *cs, void *interlock)
    475 {
    476 	callout_impl_t *c = (callout_impl_t *)cs;
    477 	kmutex_t *lock;
    478 	int flags;
    479 
    480 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    481 	KASSERT(!cpu_intr_p());
    482 	KASSERT(interlock == NULL || mutex_owned(interlock));
    483 
    484 	/* Fast path. */
    485 	lock = callout_lock(c);
    486 	flags = c->c_flags;
    487 	if ((flags & CALLOUT_PENDING) != 0)
    488 		CIRCQ_REMOVE(&c->c_list);
    489 	c->c_flags = flags & ~(CALLOUT_PENDING|CALLOUT_FIRED);
    490 	if (__predict_false(flags & CALLOUT_FIRED)) {
    491 		callout_wait(c, interlock, lock);
    492 		return true;
    493 	}
    494 	mutex_spin_exit(lock);
    495 	return false;
    496 }
    497 
    498 /*
    499  * callout_wait:
    500  *
    501  *	Slow path for callout_halt().  Deliberately marked __noinline to
    502  *	prevent unneeded overhead in the caller.
    503  */
    504 static void __noinline
    505 callout_wait(callout_impl_t *c, void *interlock, kmutex_t *lock)
    506 {
    507 	struct callout_cpu *cc;
    508 	struct lwp *l;
    509 	kmutex_t *relock;
    510 
    511 	l = curlwp;
    512 	relock = NULL;
    513 	for (;;) {
    514 		/*
    515 		 * At this point we know the callout is not pending, but it
    516 		 * could be running on a CPU somewhere.  That can be curcpu
    517 		 * in a few cases:
    518 		 *
    519 		 * - curlwp is a higher priority soft interrupt
    520 		 * - the callout blocked on a lock and is currently asleep
    521 		 * - the callout itself has called callout_halt() (nice!)
    522 		 */
    523 		cc = c->c_cpu;
    524 		if (__predict_true(cc->cc_active != c || cc->cc_lwp == l))
    525 			break;
    526 
    527 		/* It's running - need to wait for it to complete. */
    528 		if (interlock != NULL) {
    529 			/*
    530 			 * Avoid potential scheduler lock order problems by
    531 			 * dropping the interlock without the callout lock
    532 			 * held; then retry.
    533 			 */
    534 			mutex_spin_exit(lock);
    535 			mutex_exit(interlock);
    536 			relock = interlock;
    537 			interlock = NULL;
    538 		} else {
    539 			/* XXX Better to do priority inheritance. */
    540 			KASSERT(l->l_wchan == NULL);
    541 			cc->cc_nwait++;
    542 			cc->cc_ev_block.ev_count++;
    543 			l->l_kpriority = true;
    544 			sleepq_enter(&cc->cc_sleepq, l, cc->cc_lock);
    545 			sleepq_enqueue(&cc->cc_sleepq, cc, "callout",
    546 			    &sleep_syncobj, false);
    547 			sleepq_block(0, false);
    548 		}
    549 
    550 		/*
    551 		 * Re-lock the callout and check the state of play again.
    552 		 * It's a common design pattern for callouts to re-schedule
    553 		 * themselves so put a stop to it again if needed.
    554 		 */
    555 		lock = callout_lock(c);
    556 		if ((c->c_flags & CALLOUT_PENDING) != 0)
    557 			CIRCQ_REMOVE(&c->c_list);
    558 		c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    559 	}
    560 
    561 	mutex_spin_exit(lock);
    562 	if (__predict_false(relock != NULL))
    563 		mutex_enter(relock);
    564 }
    565 
    566 #ifdef notyet
    567 /*
    568  * callout_bind:
    569  *
    570  *	Bind a callout so that it will only execute on one CPU.
    571  *	The callout must be stopped, and must be MPSAFE.
    572  *
    573  *	XXX Disabled for now until it is decided how to handle
    574  *	offlined CPUs.  We may want weak+strong binding.
    575  */
    576 void
    577 callout_bind(callout_t *cs, struct cpu_info *ci)
    578 {
    579 	callout_impl_t *c = (callout_impl_t *)cs;
    580 	struct callout_cpu *cc;
    581 	kmutex_t *lock;
    582 
    583 	KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
    584 	KASSERT(c->c_cpu->cc_active != c);
    585 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    586 	KASSERT((c->c_flags & CALLOUT_MPSAFE) != 0);
    587 
    588 	lock = callout_lock(c);
    589 	cc = ci->ci_data.cpu_callout;
    590 	c->c_flags |= CALLOUT_BOUND;
    591 	if (c->c_cpu != cc) {
    592 		/*
    593 		 * Assigning c_cpu effectively unlocks the callout
    594 		 * structure, as we don't hold the new CPU's lock.
    595 		 * Issue memory barrier to prevent accesses being
    596 		 * reordered.
    597 		 */
    598 		membar_exit();
    599 		c->c_cpu = cc;
    600 	}
    601 	mutex_spin_exit(lock);
    602 }
    603 #endif
    604 
    605 void
    606 callout_setfunc(callout_t *cs, void (*func)(void *), void *arg)
    607 {
    608 	callout_impl_t *c = (callout_impl_t *)cs;
    609 	kmutex_t *lock;
    610 
    611 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    612 	KASSERT(func != NULL);
    613 
    614 	lock = callout_lock(c);
    615 	c->c_func = func;
    616 	c->c_arg = arg;
    617 	mutex_spin_exit(lock);
    618 }
    619 
    620 bool
    621 callout_expired(callout_t *cs)
    622 {
    623 	callout_impl_t *c = (callout_impl_t *)cs;
    624 	kmutex_t *lock;
    625 	bool rv;
    626 
    627 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    628 
    629 	lock = callout_lock(c);
    630 	rv = ((c->c_flags & CALLOUT_FIRED) != 0);
    631 	mutex_spin_exit(lock);
    632 
    633 	return rv;
    634 }
    635 
    636 bool
    637 callout_active(callout_t *cs)
    638 {
    639 	callout_impl_t *c = (callout_impl_t *)cs;
    640 	kmutex_t *lock;
    641 	bool rv;
    642 
    643 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    644 
    645 	lock = callout_lock(c);
    646 	rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0);
    647 	mutex_spin_exit(lock);
    648 
    649 	return rv;
    650 }
    651 
    652 bool
    653 callout_pending(callout_t *cs)
    654 {
    655 	callout_impl_t *c = (callout_impl_t *)cs;
    656 	kmutex_t *lock;
    657 	bool rv;
    658 
    659 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    660 
    661 	lock = callout_lock(c);
    662 	rv = ((c->c_flags & CALLOUT_PENDING) != 0);
    663 	mutex_spin_exit(lock);
    664 
    665 	return rv;
    666 }
    667 
    668 bool
    669 callout_invoking(callout_t *cs)
    670 {
    671 	callout_impl_t *c = (callout_impl_t *)cs;
    672 	kmutex_t *lock;
    673 	bool rv;
    674 
    675 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    676 
    677 	lock = callout_lock(c);
    678 	rv = ((c->c_flags & CALLOUT_INVOKING) != 0);
    679 	mutex_spin_exit(lock);
    680 
    681 	return rv;
    682 }
    683 
    684 void
    685 callout_ack(callout_t *cs)
    686 {
    687 	callout_impl_t *c = (callout_impl_t *)cs;
    688 	kmutex_t *lock;
    689 
    690 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    691 
    692 	lock = callout_lock(c);
    693 	c->c_flags &= ~CALLOUT_INVOKING;
    694 	mutex_spin_exit(lock);
    695 }
    696 
    697 /*
    698  * callout_hardclock:
    699  *
    700  *	Called from hardclock() once every tick.  We schedule a soft
    701  *	interrupt if there is work to be done.
    702  */
    703 void
    704 callout_hardclock(void)
    705 {
    706 	struct callout_cpu *cc;
    707 	int needsoftclock, ticks;
    708 
    709 	cc = curcpu()->ci_data.cpu_callout;
    710 	mutex_spin_enter(cc->cc_lock);
    711 
    712 	ticks = ++cc->cc_ticks;
    713 
    714 	MOVEBUCKET(cc, 0, ticks);
    715 	if (MASKWHEEL(0, ticks) == 0) {
    716 		MOVEBUCKET(cc, 1, ticks);
    717 		if (MASKWHEEL(1, ticks) == 0) {
    718 			MOVEBUCKET(cc, 2, ticks);
    719 			if (MASKWHEEL(2, ticks) == 0)
    720 				MOVEBUCKET(cc, 3, ticks);
    721 		}
    722 	}
    723 
    724 	needsoftclock = !CIRCQ_EMPTY(&cc->cc_todo);
    725 	mutex_spin_exit(cc->cc_lock);
    726 
    727 	if (needsoftclock)
    728 		softint_schedule(callout_sih);
    729 }
    730 
    731 /*
    732  * callout_softclock:
    733  *
    734  *	Soft interrupt handler, scheduled above if there is work to
    735  * 	be done.  Callouts are made in soft interrupt context.
    736  */
    737 static void
    738 callout_softclock(void *v)
    739 {
    740 	callout_impl_t *c;
    741 	struct callout_cpu *cc;
    742 	void (*func)(void *);
    743 	void *arg;
    744 	int mpsafe, count, ticks, delta;
    745 	lwp_t *l;
    746 
    747 	l = curlwp;
    748 	KASSERT(l->l_cpu == curcpu());
    749 	cc = l->l_cpu->ci_data.cpu_callout;
    750 
    751 	mutex_spin_enter(cc->cc_lock);
    752 	cc->cc_lwp = l;
    753 	while (!CIRCQ_EMPTY(&cc->cc_todo)) {
    754 		c = CIRCQ_FIRST(&cc->cc_todo);
    755 		KASSERT(c->c_magic == CALLOUT_MAGIC);
    756 		KASSERT(c->c_func != NULL);
    757 		KASSERT(c->c_cpu == cc);
    758 		KASSERT((c->c_flags & CALLOUT_PENDING) != 0);
    759 		KASSERT((c->c_flags & CALLOUT_FIRED) == 0);
    760 		CIRCQ_REMOVE(&c->c_list);
    761 
    762 		/* If due run it, otherwise insert it into the right bucket. */
    763 		ticks = cc->cc_ticks;
    764 		delta = (int)((unsigned)c->c_time - (unsigned)ticks);
    765 		if (delta > 0) {
    766 			CIRCQ_INSERT(&c->c_list, BUCKET(cc, delta, c->c_time));
    767 			continue;
    768 		}
    769 		if (delta < 0)
    770 			cc->cc_ev_late.ev_count++;
    771 
    772 		c->c_flags = (c->c_flags & ~CALLOUT_PENDING) |
    773 		    (CALLOUT_FIRED | CALLOUT_INVOKING);
    774 		mpsafe = (c->c_flags & CALLOUT_MPSAFE);
    775 		func = c->c_func;
    776 		arg = c->c_arg;
    777 		cc->cc_active = c;
    778 
    779 		mutex_spin_exit(cc->cc_lock);
    780 		KASSERT(func != NULL);
    781 		if (__predict_false(!mpsafe)) {
    782 			KERNEL_LOCK(1, NULL);
    783 			(*func)(arg);
    784 			KERNEL_UNLOCK_ONE(NULL);
    785 		} else
    786 			(*func)(arg);
    787 		mutex_spin_enter(cc->cc_lock);
    788 
    789 		/*
    790 		 * We can't touch 'c' here because it might be
    791 		 * freed already.  If LWPs waiting for callout
    792 		 * to complete, awaken them.
    793 		 */
    794 		cc->cc_active = NULL;
    795 		if ((count = cc->cc_nwait) != 0) {
    796 			cc->cc_nwait = 0;
    797 			/* sleepq_wake() drops the lock. */
    798 			sleepq_wake(&cc->cc_sleepq, cc, count, cc->cc_lock);
    799 			mutex_spin_enter(cc->cc_lock);
    800 		}
    801 	}
    802 	cc->cc_lwp = NULL;
    803 	mutex_spin_exit(cc->cc_lock);
    804 }
    805 #endif /* !CRASH */
    806 
    807 #ifdef DDB
    808 static void
    809 db_show_callout_bucket(struct callout_cpu *cc, struct callout_circq *kbucket,
    810     struct callout_circq *bucket)
    811 {
    812 	callout_impl_t *c, ci;
    813 	db_expr_t offset;
    814 	const char *name;
    815 	static char question[] = "?";
    816 	int b;
    817 
    818 	if (CIRCQ_LAST(bucket, kbucket))
    819 		return;
    820 
    821 	for (c = CIRCQ_FIRST(bucket); /*nothing*/; c = CIRCQ_NEXT(&c->c_list)) {
    822 		db_read_bytes((db_addr_t)c, sizeof(ci), (char *)&ci);
    823 		c = &ci;
    824 		db_find_sym_and_offset((db_addr_t)(intptr_t)c->c_func, &name,
    825 		    &offset);
    826 		name = name ? name : question;
    827 		b = (bucket - cc->cc_wheel);
    828 		if (b < 0)
    829 			b = -WHEELSIZE;
    830 		db_printf("%9d %2d/%-4d %16lx  %s\n",
    831 		    c->c_time - cc->cc_ticks, b / WHEELSIZE, b,
    832 		    (u_long)c->c_arg, name);
    833 		if (CIRCQ_LAST(&c->c_list, kbucket))
    834 			break;
    835 	}
    836 }
    837 
    838 void
    839 db_show_callout(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    840 {
    841 	struct callout_cpu *cc;
    842 	struct cpu_info *ci;
    843 	int b;
    844 
    845 #ifndef CRASH
    846 	db_printf("hardclock_ticks now: %d\n", getticks());
    847 #endif
    848 	db_printf("    ticks  wheel               arg  func\n");
    849 
    850 	/*
    851 	 * Don't lock the callwheel; all the other CPUs are paused
    852 	 * anyhow, and we might be called in a circumstance where
    853 	 * some other CPU was paused while holding the lock.
    854 	 */
    855 	for (ci = db_cpu_first(); ci != NULL; ci = db_cpu_next(ci)) {
    856 		db_read_bytes((db_addr_t)ci, sizeof(cib), (char *)&cib);
    857 		cc = cib.ci_data.cpu_callout;
    858 		db_read_bytes((db_addr_t)cc, sizeof(ccb), (char *)&ccb);
    859 		db_show_callout_bucket(&ccb, &cc->cc_todo, &ccb.cc_todo);
    860 	}
    861 	for (b = 0; b < BUCKETS; b++) {
    862 		for (ci = db_cpu_first(); ci != NULL; ci = db_cpu_next(ci)) {
    863 			db_read_bytes((db_addr_t)ci, sizeof(cib), (char *)&cib);
    864 			cc = cib.ci_data.cpu_callout;
    865 			db_read_bytes((db_addr_t)cc, sizeof(ccb), (char *)&ccb);
    866 			db_show_callout_bucket(&ccb, &cc->cc_wheel[b],
    867 			    &ccb.cc_wheel[b]);
    868 		}
    869 	}
    870 }
    871 #endif /* DDB */
    872