xref: /netbsd-src/sys/kern/kern_timeout.c (revision 32a89764db97084253bb1e4fd0cf5b396c7886c1)
1*32a89764Sad /*	$NetBSD: kern_timeout.c,v 1.79 2023/10/08 13:23:05 ad Exp $	*/
21b84adbeSthorpej 
31b84adbeSthorpej /*-
40a6ca13bSad  * Copyright (c) 2003, 2006, 2007, 2008, 2009, 2019, 2023
50a6ca13bSad  *     The NetBSD Foundation, Inc.
61b84adbeSthorpej  * All rights reserved.
71b84adbeSthorpej  *
81b84adbeSthorpej  * This code is derived from software contributed to The NetBSD Foundation
988ab7da9Sad  * by Jason R. Thorpe, and by Andrew Doran.
101b84adbeSthorpej  *
111b84adbeSthorpej  * Redistribution and use in source and binary forms, with or without
121b84adbeSthorpej  * modification, are permitted provided that the following conditions
131b84adbeSthorpej  * are met:
141b84adbeSthorpej  * 1. Redistributions of source code must retain the above copyright
151b84adbeSthorpej  *    notice, this list of conditions and the following disclaimer.
161b84adbeSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
171b84adbeSthorpej  *    notice, this list of conditions and the following disclaimer in the
181b84adbeSthorpej  *    documentation and/or other materials provided with the distribution.
191b84adbeSthorpej  *
201b84adbeSthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
211b84adbeSthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
221b84adbeSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
231b84adbeSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
241b84adbeSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
251b84adbeSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
261b84adbeSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
271b84adbeSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
281b84adbeSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
291b84adbeSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
301b84adbeSthorpej  * POSSIBILITY OF SUCH DAMAGE.
311b84adbeSthorpej  */
321b84adbeSthorpej 
331b84adbeSthorpej /*
341b84adbeSthorpej  * Copyright (c) 2001 Thomas Nordin <nordin@openbsd.org>
351b84adbeSthorpej  * Copyright (c) 2000-2001 Artur Grabowski <art@openbsd.org>
361b84adbeSthorpej  * All rights reserved.
371b84adbeSthorpej  *
381b84adbeSthorpej  * Redistribution and use in source and binary forms, with or without
391b84adbeSthorpej  * modification, are permitted provided that the following conditions
401b84adbeSthorpej  * are met:
411b84adbeSthorpej  *
421b84adbeSthorpej  * 1. Redistributions of source code must retain the above copyright
431b84adbeSthorpej  *    notice, this list of conditions and the following disclaimer.
441b84adbeSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
451b84adbeSthorpej  *    notice, this list of conditions and the following disclaimer in the
461b84adbeSthorpej  *    documentation and/or other materials provided with the distribution.
471b84adbeSthorpej  * 3. The name of the author may not be used to endorse or promote products
481b84adbeSthorpej  *    derived from this software without specific prior written permission.
491b84adbeSthorpej  *
501b84adbeSthorpej  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
511b84adbeSthorpej  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
521b84adbeSthorpej  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
531b84adbeSthorpej  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
541b84adbeSthorpej  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
551b84adbeSthorpej  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
561b84adbeSthorpej  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
571b84adbeSthorpej  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
581b84adbeSthorpej  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
591b84adbeSthorpej  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
601b84adbeSthorpej  */
611b84adbeSthorpej 
6209b31914Slukem #include <sys/cdefs.h>
63*32a89764Sad __KERNEL_RCSID(0, "$NetBSD: kern_timeout.c,v 1.79 2023/10/08 13:23:05 ad Exp $");
6409b31914Slukem 
651b84adbeSthorpej /*
6688ab7da9Sad  * Timeouts are kept in a hierarchical timing wheel.  The c_time is the
67ecebc8b4Sad  * value of c_cpu->cc_ticks when the timeout should be called.  There are
68ecebc8b4Sad  * four levels with 256 buckets each. See 'Scheme 7' in "Hashed and
69ecebc8b4Sad  * Hierarchical Timing Wheels: Efficient Data Structures for Implementing
70ecebc8b4Sad  * a Timer Facility" by George Varghese and Tony Lauck.
7188ab7da9Sad  *
7288ab7da9Sad  * Some of the "math" in here is a bit tricky.  We have to beware of
7388ab7da9Sad  * wrapping ints.
7488ab7da9Sad  *
7588ab7da9Sad  * We use the fact that any element added to the queue must be added with
7688ab7da9Sad  * a positive time.  That means that any element `to' on the queue cannot
7788ab7da9Sad  * be scheduled to timeout further in time than INT_MAX, but c->c_time can
7888ab7da9Sad  * be positive or negative so comparing it with anything is dangerous.
7988ab7da9Sad  * The only way we can use the c->c_time value in any predictable way is
8088ab7da9Sad  * when we calculate how far in the future `to' will timeout - "c->c_time
81ecebc8b4Sad  * - c->c_cpu->cc_ticks".  The result will always be positive for future
8288ab7da9Sad  * timeouts and 0 or negative for due timeouts.
831b84adbeSthorpej  */
841b84adbeSthorpej 
8556161147Sad #define	_CALLOUT_PRIVATE
8656161147Sad 
871b84adbeSthorpej #include <sys/param.h>
881b84adbeSthorpej #include <sys/systm.h>
891b84adbeSthorpej #include <sys/kernel.h>
901b84adbeSthorpej #include <sys/callout.h>
9111a35aedSrmind #include <sys/lwp.h>
92b07ec3fcSad #include <sys/mutex.h>
9388ab7da9Sad #include <sys/proc.h>
9488ab7da9Sad #include <sys/sleepq.h>
9588ab7da9Sad #include <sys/syncobj.h>
9688ab7da9Sad #include <sys/evcnt.h>
9746ed8f7dSad #include <sys/intr.h>
986c180fd4Sad #include <sys/cpu.h>
99ecebc8b4Sad #include <sys/kmem.h>
100fb5b3d05Sriastradh #include <sys/sdt.h>
1011b84adbeSthorpej 
1021b84adbeSthorpej #ifdef DDB
1031b84adbeSthorpej #include <machine/db_machdep.h>
1041b84adbeSthorpej #include <ddb/db_interface.h>
1051b84adbeSthorpej #include <ddb/db_access.h>
1067d5216c0Schristos #include <ddb/db_cpu.h>
1071b84adbeSthorpej #include <ddb/db_sym.h>
1081b84adbeSthorpej #include <ddb/db_output.h>
1091b84adbeSthorpej #endif
1101b84adbeSthorpej 
1111b84adbeSthorpej #define BUCKETS		1024
1121b84adbeSthorpej #define WHEELSIZE	256
1131b84adbeSthorpej #define WHEELMASK	255
1141b84adbeSthorpej #define WHEELBITS	8
1151b84adbeSthorpej 
1161b84adbeSthorpej #define MASKWHEEL(wheel, time) (((time) >> ((wheel)*WHEELBITS)) & WHEELMASK)
1171b84adbeSthorpej 
118ecebc8b4Sad #define BUCKET(cc, rel, abs)						\
1191b84adbeSthorpej     (((rel) <= (1 << (2*WHEELBITS)))					\
1201b84adbeSthorpej     	? ((rel) <= (1 << WHEELBITS))					\
121ecebc8b4Sad             ? &(cc)->cc_wheel[MASKWHEEL(0, (abs))]			\
122ecebc8b4Sad             : &(cc)->cc_wheel[MASKWHEEL(1, (abs)) + WHEELSIZE]		\
1231b84adbeSthorpej         : ((rel) <= (1 << (3*WHEELBITS)))				\
124ecebc8b4Sad             ? &(cc)->cc_wheel[MASKWHEEL(2, (abs)) + 2*WHEELSIZE]	\
125ecebc8b4Sad             : &(cc)->cc_wheel[MASKWHEEL(3, (abs)) + 3*WHEELSIZE])
1261b84adbeSthorpej 
127ecebc8b4Sad #define MOVEBUCKET(cc, wheel, time)					\
128ecebc8b4Sad     CIRCQ_APPEND(&(cc)->cc_todo,					\
129ecebc8b4Sad         &(cc)->cc_wheel[MASKWHEEL((wheel), (time)) + (wheel)*WHEELSIZE])
1301b84adbeSthorpej 
1311b84adbeSthorpej /*
1321b84adbeSthorpej  * Circular queue definitions.
1331b84adbeSthorpej  */
1341b84adbeSthorpej 
135a3b1a08dSscw #define CIRCQ_INIT(list)						\
1361b84adbeSthorpej do {									\
137a3b1a08dSscw         (list)->cq_next_l = (list);					\
138a3b1a08dSscw         (list)->cq_prev_l = (list);					\
1391b84adbeSthorpej } while (/*CONSTCOND*/0)
1401b84adbeSthorpej 
1411b84adbeSthorpej #define CIRCQ_INSERT(elem, list)					\
1421b84adbeSthorpej do {									\
143a3b1a08dSscw         (elem)->cq_prev_e = (list)->cq_prev_e;				\
144a3b1a08dSscw         (elem)->cq_next_l = (list);					\
145a3b1a08dSscw         (list)->cq_prev_l->cq_next_l = (elem);				\
146a3b1a08dSscw         (list)->cq_prev_l = (elem);					\
1471b84adbeSthorpej } while (/*CONSTCOND*/0)
1481b84adbeSthorpej 
1491b84adbeSthorpej #define CIRCQ_APPEND(fst, snd)						\
1501b84adbeSthorpej do {									\
1511b84adbeSthorpej         if (!CIRCQ_EMPTY(snd)) {					\
152a3b1a08dSscw                 (fst)->cq_prev_l->cq_next_l = (snd)->cq_next_l;		\
153a3b1a08dSscw                 (snd)->cq_next_l->cq_prev_l = (fst)->cq_prev_l;		\
154a3b1a08dSscw                 (snd)->cq_prev_l->cq_next_l = (fst);			\
155a3b1a08dSscw                 (fst)->cq_prev_l = (snd)->cq_prev_l;			\
1561b84adbeSthorpej                 CIRCQ_INIT(snd);					\
1571b84adbeSthorpej         }								\
1581b84adbeSthorpej } while (/*CONSTCOND*/0)
1591b84adbeSthorpej 
1601b84adbeSthorpej #define CIRCQ_REMOVE(elem)						\
1611b84adbeSthorpej do {									\
162a3b1a08dSscw         (elem)->cq_next_l->cq_prev_e = (elem)->cq_prev_e;		\
163a3b1a08dSscw         (elem)->cq_prev_l->cq_next_e = (elem)->cq_next_e;		\
1641b84adbeSthorpej } while (/*CONSTCOND*/0)
1651b84adbeSthorpej 
166a3b1a08dSscw #define CIRCQ_FIRST(list)	((list)->cq_next_e)
167a3b1a08dSscw #define CIRCQ_NEXT(elem)	((elem)->cq_next_e)
168a3b1a08dSscw #define CIRCQ_LAST(elem,list)	((elem)->cq_next_l == (list))
169a3b1a08dSscw #define CIRCQ_EMPTY(list)	((list)->cq_next_l == (list))
1701b84adbeSthorpej 
171ecebc8b4Sad struct callout_cpu {
1727364cd36Sad 	kmutex_t	*cc_lock;
173ecebc8b4Sad 	sleepq_t	cc_sleepq;
174ecebc8b4Sad 	u_int		cc_nwait;
175ecebc8b4Sad 	u_int		cc_ticks;
176ecebc8b4Sad 	lwp_t		*cc_lwp;
177ecebc8b4Sad 	callout_impl_t	*cc_active;
178ecebc8b4Sad 	struct evcnt	cc_ev_late;
179ecebc8b4Sad 	struct evcnt	cc_ev_block;
180ecebc8b4Sad 	struct callout_circq cc_todo;		/* Worklist */
181ecebc8b4Sad 	struct callout_circq cc_wheel[BUCKETS];	/* Queues of timeouts */
182ecebc8b4Sad 	char		cc_name1[12];
183ecebc8b4Sad 	char		cc_name2[12];
184fb5b3d05Sriastradh 	struct cpu_info	*cc_cpu;
185ecebc8b4Sad };
18688ab7da9Sad 
1875125993eSrin #ifdef DDB
1885125993eSrin static struct callout_cpu ccb;
1895125993eSrin #endif
1907d5216c0Schristos 
1915125993eSrin #ifndef CRASH /* _KERNEL */
1927d5216c0Schristos static void	callout_softclock(void *);
193298a9247Sad static void	callout_wait(callout_impl_t *, void *, kmutex_t *);
194298a9247Sad 
195298a9247Sad static struct callout_cpu callout_cpu0 __cacheline_aligned;
196298a9247Sad static void *callout_sih __read_mostly;
197ecebc8b4Sad 
198fb5b3d05Sriastradh SDT_PROBE_DEFINE2(sdt, kernel, callout, init,
199fb5b3d05Sriastradh     "struct callout *"/*ch*/,
200fb5b3d05Sriastradh     "unsigned"/*flags*/);
201fb5b3d05Sriastradh SDT_PROBE_DEFINE1(sdt, kernel, callout, destroy,
202fb5b3d05Sriastradh     "struct callout *"/*ch*/);
203fb5b3d05Sriastradh SDT_PROBE_DEFINE4(sdt, kernel, callout, setfunc,
204fb5b3d05Sriastradh     "struct callout *"/*ch*/,
205fb5b3d05Sriastradh     "void (*)(void *)"/*func*/,
206fb5b3d05Sriastradh     "void *"/*arg*/,
207fb5b3d05Sriastradh     "unsigned"/*flags*/);
208fb5b3d05Sriastradh SDT_PROBE_DEFINE5(sdt, kernel, callout, schedule,
209fb5b3d05Sriastradh     "struct callout *"/*ch*/,
210fb5b3d05Sriastradh     "void (*)(void *)"/*func*/,
211fb5b3d05Sriastradh     "void *"/*arg*/,
212fb5b3d05Sriastradh     "unsigned"/*flags*/,
213fb5b3d05Sriastradh     "int"/*ticks*/);
214fb5b3d05Sriastradh SDT_PROBE_DEFINE6(sdt, kernel, callout, migrate,
215fb5b3d05Sriastradh     "struct callout *"/*ch*/,
216fb5b3d05Sriastradh     "void (*)(void *)"/*func*/,
217fb5b3d05Sriastradh     "void *"/*arg*/,
218fb5b3d05Sriastradh     "unsigned"/*flags*/,
219fb5b3d05Sriastradh     "struct cpu_info *"/*ocpu*/,
220fb5b3d05Sriastradh     "struct cpu_info *"/*ncpu*/);
221fb5b3d05Sriastradh SDT_PROBE_DEFINE4(sdt, kernel, callout, entry,
222fb5b3d05Sriastradh     "struct callout *"/*ch*/,
223fb5b3d05Sriastradh     "void (*)(void *)"/*func*/,
224fb5b3d05Sriastradh     "void *"/*arg*/,
225fb5b3d05Sriastradh     "unsigned"/*flags*/);
226fb5b3d05Sriastradh SDT_PROBE_DEFINE4(sdt, kernel, callout, return,
227fb5b3d05Sriastradh     "struct callout *"/*ch*/,
228fb5b3d05Sriastradh     "void (*)(void *)"/*func*/,
229fb5b3d05Sriastradh     "void *"/*arg*/,
230fb5b3d05Sriastradh     "unsigned"/*flags*/);
231fb5b3d05Sriastradh SDT_PROBE_DEFINE5(sdt, kernel, callout, stop,
232fb5b3d05Sriastradh     "struct callout *"/*ch*/,
233fb5b3d05Sriastradh     "void (*)(void *)"/*func*/,
234fb5b3d05Sriastradh     "void *"/*arg*/,
235fb5b3d05Sriastradh     "unsigned"/*flags*/,
236fb5b3d05Sriastradh     "bool"/*expired*/);
237fb5b3d05Sriastradh SDT_PROBE_DEFINE4(sdt, kernel, callout, halt,
238fb5b3d05Sriastradh     "struct callout *"/*ch*/,
239fb5b3d05Sriastradh     "void (*)(void *)"/*func*/,
240fb5b3d05Sriastradh     "void *"/*arg*/,
241fb5b3d05Sriastradh     "unsigned"/*flags*/);
242fb5b3d05Sriastradh SDT_PROBE_DEFINE5(sdt, kernel, callout, halt__done,
243fb5b3d05Sriastradh     "struct callout *"/*ch*/,
244fb5b3d05Sriastradh     "void (*)(void *)"/*func*/,
245fb5b3d05Sriastradh     "void *"/*arg*/,
246fb5b3d05Sriastradh     "unsigned"/*flags*/,
247fb5b3d05Sriastradh     "bool"/*expired*/);
248fb5b3d05Sriastradh 
249*32a89764Sad syncobj_t callout_syncobj = {
250*32a89764Sad 	.sobj_name	= "callout",
251*32a89764Sad 	.sobj_flag	= SOBJ_SLEEPQ_SORTED,
252*32a89764Sad 	.sobj_boostpri  = PRI_KERNEL,
253*32a89764Sad 	.sobj_unsleep	= sleepq_unsleep,
254*32a89764Sad 	.sobj_changepri	= sleepq_changepri,
255*32a89764Sad 	.sobj_lendpri	= sleepq_lendpri,
256*32a89764Sad 	.sobj_owner	= syncobj_noowner,
257*32a89764Sad };
258*32a89764Sad 
259ecebc8b4Sad static inline kmutex_t *
callout_lock(callout_impl_t * c)260ecebc8b4Sad callout_lock(callout_impl_t *c)
261ecebc8b4Sad {
2627364cd36Sad 	struct callout_cpu *cc;
263ecebc8b4Sad 	kmutex_t *lock;
264ecebc8b4Sad 
265ecebc8b4Sad 	for (;;) {
2667364cd36Sad 		cc = c->c_cpu;
2677364cd36Sad 		lock = cc->cc_lock;
268ecebc8b4Sad 		mutex_spin_enter(lock);
2697364cd36Sad 		if (__predict_true(cc == c->c_cpu))
270ecebc8b4Sad 			return lock;
271ecebc8b4Sad 		mutex_spin_exit(lock);
272ecebc8b4Sad 	}
273ecebc8b4Sad }
274b9d81d9cSthorpej 
2751b84adbeSthorpej /*
276964811d3Spho  * Check if the callout is currently running on an LWP that isn't curlwp.
277964811d3Spho  */
278964811d3Spho static inline bool
callout_running_somewhere_else(callout_impl_t * c,struct callout_cpu * cc)279964811d3Spho callout_running_somewhere_else(callout_impl_t *c, struct callout_cpu *cc)
280964811d3Spho {
281964811d3Spho 	KASSERT(c->c_cpu == cc);
282964811d3Spho 
283964811d3Spho 	return cc->cc_active == c && cc->cc_lwp != curlwp;
284964811d3Spho }
285964811d3Spho 
286964811d3Spho /*
2871b84adbeSthorpej  * callout_startup:
2881b84adbeSthorpej  *
2891b84adbeSthorpej  *	Initialize the callout facility, called at system startup time.
290ecebc8b4Sad  *	Do just enough to allow callouts to be safely registered.
2911b84adbeSthorpej  */
2921b84adbeSthorpej void
callout_startup(void)2931b84adbeSthorpej callout_startup(void)
2941b84adbeSthorpej {
295ecebc8b4Sad 	struct callout_cpu *cc;
296ecebc8b4Sad 	int b;
297ecebc8b4Sad 
298ecebc8b4Sad 	KASSERT(curcpu()->ci_data.cpu_callout == NULL);
299ecebc8b4Sad 
300ecebc8b4Sad 	cc = &callout_cpu0;
3017364cd36Sad 	cc->cc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
302ecebc8b4Sad 	CIRCQ_INIT(&cc->cc_todo);
303ecebc8b4Sad 	for (b = 0; b < BUCKETS; b++)
304ecebc8b4Sad 		CIRCQ_INIT(&cc->cc_wheel[b]);
305ecebc8b4Sad 	curcpu()->ci_data.cpu_callout = cc;
306ecebc8b4Sad }
307ecebc8b4Sad 
308ecebc8b4Sad /*
309ecebc8b4Sad  * callout_init_cpu:
310ecebc8b4Sad  *
311ecebc8b4Sad  *	Per-CPU initialization.
312ecebc8b4Sad  */
313243edbb1Smartin CTASSERT(sizeof(callout_impl_t) <= sizeof(callout_t));
314243edbb1Smartin 
315ecebc8b4Sad void
callout_init_cpu(struct cpu_info * ci)316ecebc8b4Sad callout_init_cpu(struct cpu_info *ci)
317ecebc8b4Sad {
318ecebc8b4Sad 	struct callout_cpu *cc;
3191b84adbeSthorpej 	int b;
3201b84adbeSthorpej 
321ecebc8b4Sad 	if ((cc = ci->ci_data.cpu_callout) == NULL) {
322ecebc8b4Sad 		cc = kmem_zalloc(sizeof(*cc), KM_SLEEP);
3237364cd36Sad 		cc->cc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
324ecebc8b4Sad 		CIRCQ_INIT(&cc->cc_todo);
3251b84adbeSthorpej 		for (b = 0; b < BUCKETS; b++)
326ecebc8b4Sad 			CIRCQ_INIT(&cc->cc_wheel[b]);
327ecebc8b4Sad 	} else {
328ecebc8b4Sad 		/* Boot CPU, one time only. */
329ecebc8b4Sad 		callout_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
330ecebc8b4Sad 		    callout_softclock, NULL);
331ecebc8b4Sad 		if (callout_sih == NULL)
332ecebc8b4Sad 			panic("callout_init_cpu (2)");
33388ab7da9Sad 	}
33488ab7da9Sad 
33593e0e983Sad 	sleepq_init(&cc->cc_sleepq);
33688ab7da9Sad 
337ecebc8b4Sad 	snprintf(cc->cc_name1, sizeof(cc->cc_name1), "late/%u",
338ecebc8b4Sad 	    cpu_index(ci));
339ecebc8b4Sad 	evcnt_attach_dynamic(&cc->cc_ev_late, EVCNT_TYPE_MISC,
340ecebc8b4Sad 	    NULL, "callout", cc->cc_name1);
341ecebc8b4Sad 
342ecebc8b4Sad 	snprintf(cc->cc_name2, sizeof(cc->cc_name2), "wait/%u",
343ecebc8b4Sad 	    cpu_index(ci));
344ecebc8b4Sad 	evcnt_attach_dynamic(&cc->cc_ev_block, EVCNT_TYPE_MISC,
345ecebc8b4Sad 	    NULL, "callout", cc->cc_name2);
346ecebc8b4Sad 
347fb5b3d05Sriastradh 	cc->cc_cpu = ci;
348ecebc8b4Sad 	ci->ci_data.cpu_callout = cc;
3491b84adbeSthorpej }
3501b84adbeSthorpej 
3511b84adbeSthorpej /*
3521b84adbeSthorpej  * callout_init:
3531b84adbeSthorpej  *
354ecebc8b4Sad  *	Initialize a callout structure.  This must be quick, so we fill
355ecebc8b4Sad  *	only the minimum number of fields.
3561b84adbeSthorpej  */
3571b84adbeSthorpej void
callout_init(callout_t * cs,u_int flags)35888ab7da9Sad callout_init(callout_t *cs, u_int flags)
3591b84adbeSthorpej {
36088ab7da9Sad 	callout_impl_t *c = (callout_impl_t *)cs;
361ecebc8b4Sad 	struct callout_cpu *cc;
36288ab7da9Sad 
36388ab7da9Sad 	KASSERT((flags & ~CALLOUT_FLAGMASK) == 0);
3641b84adbeSthorpej 
365fb5b3d05Sriastradh 	SDT_PROBE2(sdt, kernel, callout, init,  cs, flags);
366fb5b3d05Sriastradh 
367ecebc8b4Sad 	cc = curcpu()->ci_data.cpu_callout;
368ecebc8b4Sad 	c->c_func = NULL;
36988ab7da9Sad 	c->c_magic = CALLOUT_MAGIC;
370ecebc8b4Sad 	if (__predict_true((flags & CALLOUT_MPSAFE) != 0 && cc != NULL)) {
371ecebc8b4Sad 		c->c_flags = flags;
372ecebc8b4Sad 		c->c_cpu = cc;
373ecebc8b4Sad 		return;
374ecebc8b4Sad 	}
375ecebc8b4Sad 	c->c_flags = flags | CALLOUT_BOUND;
376ecebc8b4Sad 	c->c_cpu = &callout_cpu0;
3771b84adbeSthorpej }
3781b84adbeSthorpej 
3791b84adbeSthorpej /*
38088ab7da9Sad  * callout_destroy:
38188ab7da9Sad  *
38288ab7da9Sad  *	Destroy a callout structure.  The callout must be stopped.
38388ab7da9Sad  */
38488ab7da9Sad void
callout_destroy(callout_t * cs)38588ab7da9Sad callout_destroy(callout_t *cs)
38688ab7da9Sad {
38788ab7da9Sad 	callout_impl_t *c = (callout_impl_t *)cs;
38888ab7da9Sad 
389fb5b3d05Sriastradh 	SDT_PROBE1(sdt, kernel, callout, destroy,  cs);
390fb5b3d05Sriastradh 
391cfe95f14Schristos 	KASSERTMSG(c->c_magic == CALLOUT_MAGIC,
392cfe95f14Schristos 	    "callout %p: c_magic (%#x) != CALLOUT_MAGIC (%#x)",
393cfe95f14Schristos 	    c, c->c_magic, CALLOUT_MAGIC);
39488ab7da9Sad 	/*
39588ab7da9Sad 	 * It's not necessary to lock in order to see the correct value
39688ab7da9Sad 	 * of c->c_flags.  If the callout could potentially have been
39788ab7da9Sad 	 * running, the current thread should have stopped it.
39888ab7da9Sad 	 */
3996a293bc9Smartin 	KASSERTMSG((c->c_flags & CALLOUT_PENDING) == 0,
4006ec02221Sad 	    "pending callout %p: c_func (%p) c_flags (%#x) destroyed from %p",
4016a293bc9Smartin 	    c, c->c_func, c->c_flags, __builtin_return_address(0));
402964811d3Spho 	KASSERTMSG(!callout_running_somewhere_else(c, c->c_cpu),
4036ec02221Sad 	    "running callout %p: c_func (%p) c_flags (%#x) destroyed from %p",
4046ec02221Sad 	    c, c->c_func, c->c_flags, __builtin_return_address(0));
40588ab7da9Sad 	c->c_magic = 0;
40688ab7da9Sad }
40788ab7da9Sad 
40888ab7da9Sad /*
4098c65b9a8Sjoerg  * callout_schedule_locked:
4101b84adbeSthorpej  *
4118c65b9a8Sjoerg  *	Schedule a callout to run.  The function and argument must
4128c65b9a8Sjoerg  *	already be set in the callout structure.  Must be called with
4138c65b9a8Sjoerg  *	callout_lock.
4141b84adbeSthorpej  */
4158c65b9a8Sjoerg static void
callout_schedule_locked(callout_impl_t * c,kmutex_t * lock,int to_ticks)416ecebc8b4Sad callout_schedule_locked(callout_impl_t *c, kmutex_t *lock, int to_ticks)
4171b84adbeSthorpej {
418ecebc8b4Sad 	struct callout_cpu *cc, *occ;
419b07ec3fcSad 	int old_time;
4201b84adbeSthorpej 
421fb5b3d05Sriastradh 	SDT_PROBE5(sdt, kernel, callout, schedule,
422fb5b3d05Sriastradh 	    c, c->c_func, c->c_arg, c->c_flags, to_ticks);
423fb5b3d05Sriastradh 
4241b84adbeSthorpej 	KASSERT(to_ticks >= 0);
4258c65b9a8Sjoerg 	KASSERT(c->c_func != NULL);
4261b84adbeSthorpej 
4271b84adbeSthorpej 	/* Initialize the time here, it won't change. */
428ecebc8b4Sad 	occ = c->c_cpu;
42922191248Sad 	c->c_flags &= ~(CALLOUT_FIRED | CALLOUT_INVOKING);
4301b84adbeSthorpej 
4311b84adbeSthorpej 	/*
4321b84adbeSthorpej 	 * If this timeout is already scheduled and now is moved
4331b84adbeSthorpej 	 * earlier, reschedule it now.  Otherwise leave it in place
4341b84adbeSthorpej 	 * and let it be rescheduled later.
4351b84adbeSthorpej 	 */
43688ab7da9Sad 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
437ecebc8b4Sad 		/* Leave on existing CPU. */
438ecebc8b4Sad 		old_time = c->c_time;
439ecebc8b4Sad 		c->c_time = to_ticks + occ->cc_ticks;
440f4585f06Syamt 		if (c->c_time - old_time < 0) {
4411b84adbeSthorpej 			CIRCQ_REMOVE(&c->c_list);
442ecebc8b4Sad 			CIRCQ_INSERT(&c->c_list, &occ->cc_todo);
4431b84adbeSthorpej 		}
444ecebc8b4Sad 		mutex_spin_exit(lock);
445ecebc8b4Sad 		return;
446ecebc8b4Sad 	}
447ecebc8b4Sad 
448ecebc8b4Sad 	cc = curcpu()->ci_data.cpu_callout;
449ecebc8b4Sad 	if ((c->c_flags & CALLOUT_BOUND) != 0 || cc == occ ||
4507364cd36Sad 	    !mutex_tryenter(cc->cc_lock)) {
451ecebc8b4Sad 		/* Leave on existing CPU. */
452ecebc8b4Sad 		c->c_time = to_ticks + occ->cc_ticks;
4531b84adbeSthorpej 		c->c_flags |= CALLOUT_PENDING;
454ecebc8b4Sad 		CIRCQ_INSERT(&c->c_list, &occ->cc_todo);
455ecebc8b4Sad 	} else {
456ecebc8b4Sad 		/* Move to this CPU. */
457ecebc8b4Sad 		c->c_cpu = cc;
458ecebc8b4Sad 		c->c_time = to_ticks + cc->cc_ticks;
459ecebc8b4Sad 		c->c_flags |= CALLOUT_PENDING;
460ecebc8b4Sad 		CIRCQ_INSERT(&c->c_list, &cc->cc_todo);
4617364cd36Sad 		mutex_spin_exit(cc->cc_lock);
462fb5b3d05Sriastradh 		SDT_PROBE6(sdt, kernel, callout, migrate,
463fb5b3d05Sriastradh 		    c, c->c_func, c->c_arg, c->c_flags,
464fb5b3d05Sriastradh 		    occ->cc_cpu, cc->cc_cpu);
4651b84adbeSthorpej 	}
466ecebc8b4Sad 	mutex_spin_exit(lock);
4678c65b9a8Sjoerg }
4688c65b9a8Sjoerg 
4698c65b9a8Sjoerg /*
4708c65b9a8Sjoerg  * callout_reset:
4718c65b9a8Sjoerg  *
4728c65b9a8Sjoerg  *	Reset a callout structure with a new function and argument, and
4738c65b9a8Sjoerg  *	schedule it to run.
4748c65b9a8Sjoerg  */
4758c65b9a8Sjoerg void
callout_reset(callout_t * cs,int to_ticks,void (* func)(void *),void * arg)4768c65b9a8Sjoerg callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg)
4778c65b9a8Sjoerg {
4788c65b9a8Sjoerg 	callout_impl_t *c = (callout_impl_t *)cs;
479ecebc8b4Sad 	kmutex_t *lock;
4808c65b9a8Sjoerg 
4818c65b9a8Sjoerg 	KASSERT(c->c_magic == CALLOUT_MAGIC);
482b37999b4Srmind 	KASSERT(func != NULL);
4838c65b9a8Sjoerg 
484ecebc8b4Sad 	lock = callout_lock(c);
485fb5b3d05Sriastradh 	SDT_PROBE4(sdt, kernel, callout, setfunc,  cs, func, arg, c->c_flags);
4868c65b9a8Sjoerg 	c->c_func = func;
4878c65b9a8Sjoerg 	c->c_arg = arg;
488ecebc8b4Sad 	callout_schedule_locked(c, lock, to_ticks);
4891b84adbeSthorpej }
4901b84adbeSthorpej 
4911b84adbeSthorpej /*
4921b84adbeSthorpej  * callout_schedule:
4931b84adbeSthorpej  *
4941b84adbeSthorpej  *	Schedule a callout to run.  The function and argument must
4951b84adbeSthorpej  *	already be set in the callout structure.
4961b84adbeSthorpej  */
4971b84adbeSthorpej void
callout_schedule(callout_t * cs,int to_ticks)49888ab7da9Sad callout_schedule(callout_t *cs, int to_ticks)
4991b84adbeSthorpej {
50088ab7da9Sad 	callout_impl_t *c = (callout_impl_t *)cs;
501ecebc8b4Sad 	kmutex_t *lock;
5021b84adbeSthorpej 
50388ab7da9Sad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
5041b84adbeSthorpej 
505ecebc8b4Sad 	lock = callout_lock(c);
506ecebc8b4Sad 	callout_schedule_locked(c, lock, to_ticks);
5071b84adbeSthorpej }
5081b84adbeSthorpej 
5091b84adbeSthorpej /*
5101b84adbeSthorpej  * callout_stop:
5111b84adbeSthorpej  *
512ecebc8b4Sad  *	Try to cancel a pending callout.  It may be too late: the callout
513ecebc8b4Sad  *	could be running on another CPU.  If called from interrupt context,
514ecebc8b4Sad  *	the callout could already be in progress at a lower priority.
5151b84adbeSthorpej  */
51688ab7da9Sad bool
callout_stop(callout_t * cs)51788ab7da9Sad callout_stop(callout_t *cs)
5181b84adbeSthorpej {
51988ab7da9Sad 	callout_impl_t *c = (callout_impl_t *)cs;
520ecebc8b4Sad 	kmutex_t *lock;
52188ab7da9Sad 	bool expired;
5221b84adbeSthorpej 
52388ab7da9Sad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
524b07ec3fcSad 
525ecebc8b4Sad 	lock = callout_lock(c);
52688ab7da9Sad 
52788ab7da9Sad 	if ((c->c_flags & CALLOUT_PENDING) != 0)
5281b84adbeSthorpej 		CIRCQ_REMOVE(&c->c_list);
52988ab7da9Sad 	expired = ((c->c_flags & CALLOUT_FIRED) != 0);
53069f1e707She 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
5311b84adbeSthorpej 
532fb5b3d05Sriastradh 	SDT_PROBE5(sdt, kernel, callout, stop,
533fb5b3d05Sriastradh 	    c, c->c_func, c->c_arg, c->c_flags, expired);
534fb5b3d05Sriastradh 
535ecebc8b4Sad 	mutex_spin_exit(lock);
536c3338aabSad 
537c3338aabSad 	return expired;
538c3338aabSad }
539c3338aabSad 
540c3338aabSad /*
541c3338aabSad  * callout_halt:
542c3338aabSad  *
543c3338aabSad  *	Cancel a pending callout.  If in-flight, block until it completes.
544ecebc8b4Sad  *	May not be called from a hard interrupt handler.  If the callout
545ecebc8b4Sad  * 	can take locks, the caller of callout_halt() must not hold any of
54643d8bae9Sad  *	those locks, otherwise the two could deadlock.  If 'interlock' is
54743d8bae9Sad  *	non-NULL and we must wait for the callout to complete, it will be
54843d8bae9Sad  *	released and re-acquired before returning.
549c3338aabSad  */
550c3338aabSad bool
callout_halt(callout_t * cs,void * interlock)551c8ff5c0cSad callout_halt(callout_t *cs, void *interlock)
552c3338aabSad {
553c3338aabSad 	callout_impl_t *c = (callout_impl_t *)cs;
554298a9247Sad 	kmutex_t *lock;
555c3338aabSad 
556c3338aabSad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
557c3338aabSad 	KASSERT(!cpu_intr_p());
558d56bf90bSozaki-r 	KASSERT(interlock == NULL || mutex_owned(interlock));
559c3338aabSad 
560298a9247Sad 	/* Fast path. */
561ecebc8b4Sad 	lock = callout_lock(c);
562fb5b3d05Sriastradh 	SDT_PROBE4(sdt, kernel, callout, halt,
563fb5b3d05Sriastradh 	    c, c->c_func, c->c_arg, c->c_flags);
564eef95bfbSpho 	if ((c->c_flags & CALLOUT_PENDING) != 0)
565c3338aabSad 		CIRCQ_REMOVE(&c->c_list);
566eef95bfbSpho 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
567964811d3Spho 	if (__predict_false(callout_running_somewhere_else(c, c->c_cpu))) {
568298a9247Sad 		callout_wait(c, interlock, lock);
569298a9247Sad 		return true;
570298a9247Sad 	}
571fb5b3d05Sriastradh 	SDT_PROBE5(sdt, kernel, callout, halt__done,
572fb5b3d05Sriastradh 	    c, c->c_func, c->c_arg, c->c_flags, /*expired*/false);
573298a9247Sad 	mutex_spin_exit(lock);
574298a9247Sad 	return false;
575298a9247Sad }
576298a9247Sad 
577298a9247Sad /*
578298a9247Sad  * callout_wait:
579298a9247Sad  *
580298a9247Sad  *	Slow path for callout_halt().  Deliberately marked __noinline to
581298a9247Sad  *	prevent unneeded overhead in the caller.
582298a9247Sad  */
583298a9247Sad static void __noinline
callout_wait(callout_impl_t * c,void * interlock,kmutex_t * lock)584298a9247Sad callout_wait(callout_impl_t *c, void *interlock, kmutex_t *lock)
585298a9247Sad {
586298a9247Sad 	struct callout_cpu *cc;
587298a9247Sad 	struct lwp *l;
588298a9247Sad 	kmutex_t *relock;
5890a6ca13bSad 	int nlocks;
590c3338aabSad 
591c3338aabSad 	l = curlwp;
592298a9247Sad 	relock = NULL;
593ecebc8b4Sad 	for (;;) {
5945f882f34Sad 		/*
5955f882f34Sad 		 * At this point we know the callout is not pending, but it
5965f882f34Sad 		 * could be running on a CPU somewhere.  That can be curcpu
5975f882f34Sad 		 * in a few cases:
5985f882f34Sad 		 *
5995f882f34Sad 		 * - curlwp is a higher priority soft interrupt
6005f882f34Sad 		 * - the callout blocked on a lock and is currently asleep
6015f882f34Sad 		 * - the callout itself has called callout_halt() (nice!)
6025f882f34Sad 		 */
603ecebc8b4Sad 		cc = c->c_cpu;
604964811d3Spho 		if (__predict_true(!callout_running_somewhere_else(c, cc)))
605ecebc8b4Sad 			break;
6065f882f34Sad 
6075f882f34Sad 		/* It's running - need to wait for it to complete. */
60843d8bae9Sad 		if (interlock != NULL) {
60943d8bae9Sad 			/*
61043d8bae9Sad 			 * Avoid potential scheduler lock order problems by
61143d8bae9Sad 			 * dropping the interlock without the callout lock
6125f882f34Sad 			 * held; then retry.
61343d8bae9Sad 			 */
61443d8bae9Sad 			mutex_spin_exit(lock);
61543d8bae9Sad 			mutex_exit(interlock);
61643d8bae9Sad 			relock = interlock;
61743d8bae9Sad 			interlock = NULL;
61843d8bae9Sad 		} else {
61943d8bae9Sad 			/* XXX Better to do priority inheritance. */
620c3338aabSad 			KASSERT(l->l_wchan == NULL);
621ecebc8b4Sad 			cc->cc_nwait++;
622ecebc8b4Sad 			cc->cc_ev_block.ev_count++;
6230a6ca13bSad 			nlocks = sleepq_enter(&cc->cc_sleepq, l, cc->cc_lock);
62443d8bae9Sad 			sleepq_enqueue(&cc->cc_sleepq, cc, "callout",
625*32a89764Sad 			    &callout_syncobj, false);
626*32a89764Sad 			sleepq_block(0, false, &callout_syncobj, nlocks);
62743d8bae9Sad 		}
6285f882f34Sad 
6295f882f34Sad 		/*
6305f882f34Sad 		 * Re-lock the callout and check the state of play again.
6315f882f34Sad 		 * It's a common design pattern for callouts to re-schedule
6325f882f34Sad 		 * themselves so put a stop to it again if needed.
6335f882f34Sad 		 */
634ecebc8b4Sad 		lock = callout_lock(c);
6355f882f34Sad 		if ((c->c_flags & CALLOUT_PENDING) != 0)
6365f882f34Sad 			CIRCQ_REMOVE(&c->c_list);
6375f882f34Sad 		c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
638c3338aabSad 	}
639c3338aabSad 
640fb5b3d05Sriastradh 	SDT_PROBE5(sdt, kernel, callout, halt__done,
641fb5b3d05Sriastradh 	    c, c->c_func, c->c_arg, c->c_flags, /*expired*/true);
642fb5b3d05Sriastradh 
643ecebc8b4Sad 	mutex_spin_exit(lock);
64443d8bae9Sad 	if (__predict_false(relock != NULL))
64543d8bae9Sad 		mutex_enter(relock);
64688ab7da9Sad }
64788ab7da9Sad 
648ecebc8b4Sad #ifdef notyet
649ecebc8b4Sad /*
650ecebc8b4Sad  * callout_bind:
651ecebc8b4Sad  *
652ecebc8b4Sad  *	Bind a callout so that it will only execute on one CPU.
653ecebc8b4Sad  *	The callout must be stopped, and must be MPSAFE.
654ecebc8b4Sad  *
655ecebc8b4Sad  *	XXX Disabled for now until it is decided how to handle
656ecebc8b4Sad  *	offlined CPUs.  We may want weak+strong binding.
657ecebc8b4Sad  */
658ecebc8b4Sad void
callout_bind(callout_t * cs,struct cpu_info * ci)659ecebc8b4Sad callout_bind(callout_t *cs, struct cpu_info *ci)
660ecebc8b4Sad {
661ecebc8b4Sad 	callout_impl_t *c = (callout_impl_t *)cs;
662ecebc8b4Sad 	struct callout_cpu *cc;
663ecebc8b4Sad 	kmutex_t *lock;
664ecebc8b4Sad 
665ecebc8b4Sad 	KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
666ecebc8b4Sad 	KASSERT(c->c_cpu->cc_active != c);
667ecebc8b4Sad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
668ecebc8b4Sad 	KASSERT((c->c_flags & CALLOUT_MPSAFE) != 0);
669ecebc8b4Sad 
670ecebc8b4Sad 	lock = callout_lock(c);
671ecebc8b4Sad 	cc = ci->ci_data.cpu_callout;
672ecebc8b4Sad 	c->c_flags |= CALLOUT_BOUND;
673ecebc8b4Sad 	if (c->c_cpu != cc) {
674ecebc8b4Sad 		/*
675ecebc8b4Sad 		 * Assigning c_cpu effectively unlocks the callout
676ecebc8b4Sad 		 * structure, as we don't hold the new CPU's lock.
677ecebc8b4Sad 		 * Issue memory barrier to prevent accesses being
678ecebc8b4Sad 		 * reordered.
679ecebc8b4Sad 		 */
680ecebc8b4Sad 		membar_exit();
681ecebc8b4Sad 		c->c_cpu = cc;
682ecebc8b4Sad 	}
683ecebc8b4Sad 	mutex_spin_exit(lock);
684ecebc8b4Sad }
685ecebc8b4Sad #endif
686ecebc8b4Sad 
68788ab7da9Sad void
callout_setfunc(callout_t * cs,void (* func)(void *),void * arg)68888ab7da9Sad callout_setfunc(callout_t *cs, void (*func)(void *), void *arg)
68988ab7da9Sad {
69088ab7da9Sad 	callout_impl_t *c = (callout_impl_t *)cs;
691ecebc8b4Sad 	kmutex_t *lock;
69288ab7da9Sad 
69388ab7da9Sad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
694b37999b4Srmind 	KASSERT(func != NULL);
69588ab7da9Sad 
696ecebc8b4Sad 	lock = callout_lock(c);
697fb5b3d05Sriastradh 	SDT_PROBE4(sdt, kernel, callout, setfunc,  cs, func, arg, c->c_flags);
69888ab7da9Sad 	c->c_func = func;
69988ab7da9Sad 	c->c_arg = arg;
700ecebc8b4Sad 	mutex_spin_exit(lock);
70188ab7da9Sad }
70288ab7da9Sad 
70388ab7da9Sad bool
callout_expired(callout_t * cs)70488ab7da9Sad callout_expired(callout_t *cs)
70588ab7da9Sad {
70688ab7da9Sad 	callout_impl_t *c = (callout_impl_t *)cs;
707ecebc8b4Sad 	kmutex_t *lock;
70888ab7da9Sad 	bool rv;
70988ab7da9Sad 
71088ab7da9Sad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
71188ab7da9Sad 
712ecebc8b4Sad 	lock = callout_lock(c);
71388ab7da9Sad 	rv = ((c->c_flags & CALLOUT_FIRED) != 0);
714ecebc8b4Sad 	mutex_spin_exit(lock);
71588ab7da9Sad 
71688ab7da9Sad 	return rv;
71788ab7da9Sad }
71888ab7da9Sad 
71988ab7da9Sad bool
callout_active(callout_t * cs)72088ab7da9Sad callout_active(callout_t *cs)
72188ab7da9Sad {
72288ab7da9Sad 	callout_impl_t *c = (callout_impl_t *)cs;
723ecebc8b4Sad 	kmutex_t *lock;
72488ab7da9Sad 	bool rv;
72588ab7da9Sad 
72688ab7da9Sad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
72788ab7da9Sad 
728ecebc8b4Sad 	lock = callout_lock(c);
72988ab7da9Sad 	rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0);
730ecebc8b4Sad 	mutex_spin_exit(lock);
73188ab7da9Sad 
73288ab7da9Sad 	return rv;
73388ab7da9Sad }
73488ab7da9Sad 
73588ab7da9Sad bool
callout_pending(callout_t * cs)73688ab7da9Sad callout_pending(callout_t *cs)
73788ab7da9Sad {
73888ab7da9Sad 	callout_impl_t *c = (callout_impl_t *)cs;
739ecebc8b4Sad 	kmutex_t *lock;
74088ab7da9Sad 	bool rv;
74188ab7da9Sad 
74288ab7da9Sad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
74388ab7da9Sad 
744ecebc8b4Sad 	lock = callout_lock(c);
74588ab7da9Sad 	rv = ((c->c_flags & CALLOUT_PENDING) != 0);
746ecebc8b4Sad 	mutex_spin_exit(lock);
74788ab7da9Sad 
74888ab7da9Sad 	return rv;
74988ab7da9Sad }
75088ab7da9Sad 
75188ab7da9Sad bool
callout_invoking(callout_t * cs)75288ab7da9Sad callout_invoking(callout_t *cs)
75388ab7da9Sad {
75488ab7da9Sad 	callout_impl_t *c = (callout_impl_t *)cs;
755ecebc8b4Sad 	kmutex_t *lock;
75688ab7da9Sad 	bool rv;
75788ab7da9Sad 
75888ab7da9Sad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
75988ab7da9Sad 
760ecebc8b4Sad 	lock = callout_lock(c);
76188ab7da9Sad 	rv = ((c->c_flags & CALLOUT_INVOKING) != 0);
762ecebc8b4Sad 	mutex_spin_exit(lock);
76388ab7da9Sad 
76488ab7da9Sad 	return rv;
76588ab7da9Sad }
76688ab7da9Sad 
76788ab7da9Sad void
callout_ack(callout_t * cs)76888ab7da9Sad callout_ack(callout_t *cs)
76988ab7da9Sad {
77088ab7da9Sad 	callout_impl_t *c = (callout_impl_t *)cs;
771ecebc8b4Sad 	kmutex_t *lock;
77288ab7da9Sad 
77388ab7da9Sad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
77488ab7da9Sad 
775ecebc8b4Sad 	lock = callout_lock(c);
77688ab7da9Sad 	c->c_flags &= ~CALLOUT_INVOKING;
777ecebc8b4Sad 	mutex_spin_exit(lock);
7781b84adbeSthorpej }
7791b84adbeSthorpej 
7801b84adbeSthorpej /*
781ecebc8b4Sad  * callout_hardclock:
782ecebc8b4Sad  *
783ecebc8b4Sad  *	Called from hardclock() once every tick.  We schedule a soft
784ecebc8b4Sad  *	interrupt if there is work to be done.
7851b84adbeSthorpej  */
78688ab7da9Sad void
callout_hardclock(void)7871b84adbeSthorpej callout_hardclock(void)
7881b84adbeSthorpej {
789ecebc8b4Sad 	struct callout_cpu *cc;
790ecebc8b4Sad 	int needsoftclock, ticks;
7911b84adbeSthorpej 
792ecebc8b4Sad 	cc = curcpu()->ci_data.cpu_callout;
7937364cd36Sad 	mutex_spin_enter(cc->cc_lock);
7941b84adbeSthorpej 
795ecebc8b4Sad 	ticks = ++cc->cc_ticks;
796ecebc8b4Sad 
797ecebc8b4Sad 	MOVEBUCKET(cc, 0, ticks);
798ecebc8b4Sad 	if (MASKWHEEL(0, ticks) == 0) {
799ecebc8b4Sad 		MOVEBUCKET(cc, 1, ticks);
800ecebc8b4Sad 		if (MASKWHEEL(1, ticks) == 0) {
801ecebc8b4Sad 			MOVEBUCKET(cc, 2, ticks);
802ecebc8b4Sad 			if (MASKWHEEL(2, ticks) == 0)
803ecebc8b4Sad 				MOVEBUCKET(cc, 3, ticks);
8041b84adbeSthorpej 		}
8051b84adbeSthorpej 	}
8061b84adbeSthorpej 
807ecebc8b4Sad 	needsoftclock = !CIRCQ_EMPTY(&cc->cc_todo);
8087364cd36Sad 	mutex_spin_exit(cc->cc_lock);
8091b84adbeSthorpej 
81088ab7da9Sad 	if (needsoftclock)
811ecebc8b4Sad 		softint_schedule(callout_sih);
8121b84adbeSthorpej }
8131b84adbeSthorpej 
814ecebc8b4Sad /*
815ecebc8b4Sad  * callout_softclock:
816ecebc8b4Sad  *
817ecebc8b4Sad  *	Soft interrupt handler, scheduled above if there is work to
818ecebc8b4Sad  * 	be done.  Callouts are made in soft interrupt context.
819ecebc8b4Sad  */
82088ab7da9Sad static void
callout_softclock(void * v)82188ab7da9Sad callout_softclock(void *v)
8221b84adbeSthorpej {
82388ab7da9Sad 	callout_impl_t *c;
824ecebc8b4Sad 	struct callout_cpu *cc;
8251b84adbeSthorpej 	void (*func)(void *);
8261b84adbeSthorpej 	void *arg;
8270fa3d1b3Sriastradh 	int mpsafe, count, ticks, delta;
82868020631Sriastradh 	u_int flags __unused;
82988ab7da9Sad 	lwp_t *l;
8301b84adbeSthorpej 
83188ab7da9Sad 	l = curlwp;
832ecebc8b4Sad 	KASSERT(l->l_cpu == curcpu());
833ecebc8b4Sad 	cc = l->l_cpu->ci_data.cpu_callout;
83488ab7da9Sad 
8357364cd36Sad 	mutex_spin_enter(cc->cc_lock);
836ecebc8b4Sad 	cc->cc_lwp = l;
837ecebc8b4Sad 	while (!CIRCQ_EMPTY(&cc->cc_todo)) {
838ecebc8b4Sad 		c = CIRCQ_FIRST(&cc->cc_todo);
83988ab7da9Sad 		KASSERT(c->c_magic == CALLOUT_MAGIC);
84088ab7da9Sad 		KASSERT(c->c_func != NULL);
841ecebc8b4Sad 		KASSERT(c->c_cpu == cc);
8427fecd4deSad 		KASSERT((c->c_flags & CALLOUT_PENDING) != 0);
8437fecd4deSad 		KASSERT((c->c_flags & CALLOUT_FIRED) == 0);
8441b84adbeSthorpej 		CIRCQ_REMOVE(&c->c_list);
8451b84adbeSthorpej 
8461b84adbeSthorpej 		/* If due run it, otherwise insert it into the right bucket. */
847ecebc8b4Sad 		ticks = cc->cc_ticks;
848dd8f5519Skre 		delta = (int)((unsigned)c->c_time - (unsigned)ticks);
849dd8f5519Skre 		if (delta > 0) {
850ecebc8b4Sad 			CIRCQ_INSERT(&c->c_list, BUCKET(cc, delta, c->c_time));
851ecebc8b4Sad 			continue;
852ecebc8b4Sad 		}
853dd8f5519Skre 		if (delta < 0)
854ecebc8b4Sad 			cc->cc_ev_late.ev_count++;
8551b84adbeSthorpej 
85622191248Sad 		c->c_flags = (c->c_flags & ~CALLOUT_PENDING) |
85722191248Sad 		    (CALLOUT_FIRED | CALLOUT_INVOKING);
85888ab7da9Sad 		mpsafe = (c->c_flags & CALLOUT_MPSAFE);
8591b84adbeSthorpej 		func = c->c_func;
8601b84adbeSthorpej 		arg = c->c_arg;
861ecebc8b4Sad 		cc->cc_active = c;
862fb5b3d05Sriastradh 		flags = c->c_flags;
86388ab7da9Sad 
8647364cd36Sad 		mutex_spin_exit(cc->cc_lock);
865b37999b4Srmind 		KASSERT(func != NULL);
866fb5b3d05Sriastradh 		SDT_PROBE4(sdt, kernel, callout, entry,  c, func, arg, flags);
8677364cd36Sad 		if (__predict_false(!mpsafe)) {
868ecebc8b4Sad 			KERNEL_LOCK(1, NULL);
8691b84adbeSthorpej 			(*func)(arg);
870ecebc8b4Sad 			KERNEL_UNLOCK_ONE(NULL);
87188ab7da9Sad 		} else
87288ab7da9Sad 			(*func)(arg);
873fb5b3d05Sriastradh 		SDT_PROBE4(sdt, kernel, callout, return,  c, func, arg, flags);
874fb698d22Sriastradh 		KASSERTMSG(l->l_blcnt == 0,
875fb698d22Sriastradh 		    "callout %p func %p leaked %d biglocks",
876fb698d22Sriastradh 		    c, func, l->l_blcnt);
8777364cd36Sad 		mutex_spin_enter(cc->cc_lock);
87888ab7da9Sad 
879b07ec3fcSad 		/*
88088ab7da9Sad 		 * We can't touch 'c' here because it might be
88188ab7da9Sad 		 * freed already.  If LWPs waiting for callout
88288ab7da9Sad 		 * to complete, awaken them.
883b07ec3fcSad 		 */
884ecebc8b4Sad 		cc->cc_active = NULL;
885ecebc8b4Sad 		if ((count = cc->cc_nwait) != 0) {
886ecebc8b4Sad 			cc->cc_nwait = 0;
88788ab7da9Sad 			/* sleepq_wake() drops the lock. */
8887364cd36Sad 			sleepq_wake(&cc->cc_sleepq, cc, count, cc->cc_lock);
8897364cd36Sad 			mutex_spin_enter(cc->cc_lock);
89088ab7da9Sad 		}
8911b84adbeSthorpej 	}
892ecebc8b4Sad 	cc->cc_lwp = NULL;
8937364cd36Sad 	mutex_spin_exit(cc->cc_lock);
8941b84adbeSthorpej }
895a97ea118Srin #endif /* !CRASH */
8961b84adbeSthorpej 
8971b84adbeSthorpej #ifdef DDB
8981b84adbeSthorpej static void
db_show_callout_bucket(struct callout_cpu * cc,struct callout_circq * kbucket,struct callout_circq * bucket)8992e0dfabbSchristos db_show_callout_bucket(struct callout_cpu *cc, struct callout_circq *kbucket,
9002e0dfabbSchristos     struct callout_circq *bucket)
9011b84adbeSthorpej {
9027d5216c0Schristos 	callout_impl_t *c, ci;
9031b84adbeSthorpej 	db_expr_t offset;
904efb69433Schristos 	const char *name;
905efb69433Schristos 	static char question[] = "?";
906ecebc8b4Sad 	int b;
9071b84adbeSthorpej 
9082e0dfabbSchristos 	if (CIRCQ_LAST(bucket, kbucket))
909a3b1a08dSscw 		return;
910a3b1a08dSscw 
9112e0dfabbSchristos 	for (c = CIRCQ_FIRST(bucket); /*nothing*/; c = CIRCQ_NEXT(&c->c_list)) {
9127d5216c0Schristos 		db_read_bytes((db_addr_t)c, sizeof(ci), (char *)&ci);
9137d5216c0Schristos 		c = &ci;
91420fb5a9fSscw 		db_find_sym_and_offset((db_addr_t)(intptr_t)c->c_func, &name,
91520fb5a9fSscw 		    &offset);
916efb69433Schristos 		name = name ? name : question;
917ecebc8b4Sad 		b = (bucket - cc->cc_wheel);
918ecebc8b4Sad 		if (b < 0)
919ecebc8b4Sad 			b = -WHEELSIZE;
920ecebc8b4Sad 		db_printf("%9d %2d/%-4d %16lx  %s\n",
921ecebc8b4Sad 		    c->c_time - cc->cc_ticks, b / WHEELSIZE, b,
922ecebc8b4Sad 		    (u_long)c->c_arg, name);
9232e0dfabbSchristos 		if (CIRCQ_LAST(&c->c_list, kbucket))
924a3b1a08dSscw 			break;
9251b84adbeSthorpej 	}
9261b84adbeSthorpej }
9271b84adbeSthorpej 
9281b84adbeSthorpej void
db_show_callout(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)92993feeb12Smatt db_show_callout(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
9301b84adbeSthorpej {
931fbbf6b42Srin 	struct callout_cpu *cc;
932fbbf6b42Srin 	struct cpu_info *ci;
9331b84adbeSthorpej 	int b;
9341b84adbeSthorpej 
9357d5216c0Schristos #ifndef CRASH
936983fd9ccSmaxv 	db_printf("hardclock_ticks now: %d\n", getticks());
9377d5216c0Schristos #endif
9381b84adbeSthorpej 	db_printf("    ticks  wheel               arg  func\n");
9391b84adbeSthorpej 
9401b84adbeSthorpej 	/*
9411b84adbeSthorpej 	 * Don't lock the callwheel; all the other CPUs are paused
9421b84adbeSthorpej 	 * anyhow, and we might be called in a circumstance where
9431b84adbeSthorpej 	 * some other CPU was paused while holding the lock.
9441b84adbeSthorpej 	 */
9457d5216c0Schristos 	for (ci = db_cpu_first(); ci != NULL; ci = db_cpu_next(ci)) {
946d82fa5bdSrin 		db_read_bytes((db_addr_t)ci +
947d82fa5bdSrin 		    offsetof(struct cpu_info, ci_data.cpu_callout),
948d82fa5bdSrin 		    sizeof(cc), (char *)&cc);
949fbbf6b42Srin 		db_read_bytes((db_addr_t)cc, sizeof(ccb), (char *)&ccb);
950fbbf6b42Srin 		db_show_callout_bucket(&ccb, &cc->cc_todo, &ccb.cc_todo);
951ecebc8b4Sad 	}
952ecebc8b4Sad 	for (b = 0; b < BUCKETS; b++) {
9537d5216c0Schristos 		for (ci = db_cpu_first(); ci != NULL; ci = db_cpu_next(ci)) {
954d82fa5bdSrin 			db_read_bytes((db_addr_t)ci +
955d82fa5bdSrin 			    offsetof(struct cpu_info, ci_data.cpu_callout),
956d82fa5bdSrin 			    sizeof(cc), (char *)&cc);
957fbbf6b42Srin 			db_read_bytes((db_addr_t)cc, sizeof(ccb), (char *)&ccb);
958fbbf6b42Srin 			db_show_callout_bucket(&ccb, &cc->cc_wheel[b],
959fbbf6b42Srin 			    &ccb.cc_wheel[b]);
960ecebc8b4Sad 		}
961ecebc8b4Sad 	}
9621b84adbeSthorpej }
9631b84adbeSthorpej #endif /* DDB */
964