xref: /freebsd/sys/kern/kern_timeout.c (revision 4ceaf45de53de4da24831c87d7163cd15225c137)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34acc8326dSGarrett Wollman  *	From: @(#)kern_clock.c	8.5 (Berkeley) 1/21/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
4091dd9aaeSRobert Watson #include "opt_kdtrace.h"
4191dd9aaeSRobert Watson 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
448d809d50SJeff Roberson #include <sys/bus.h>
4515b7a470SPoul-Henning Kamp #include <sys/callout.h>
462c1bb207SColin Percival #include <sys/condvar.h>
478d809d50SJeff Roberson #include <sys/interrupt.h>
48df8bae1dSRodney W. Grimes #include <sys/kernel.h>
49ff7ec58aSRobert Watson #include <sys/ktr.h>
50f34fa851SJohn Baldwin #include <sys/lock.h>
518d809d50SJeff Roberson #include <sys/malloc.h>
52cb799bfeSJohn Baldwin #include <sys/mutex.h>
5321f9e816SJohn Baldwin #include <sys/proc.h>
5491dd9aaeSRobert Watson #include <sys/sdt.h>
556a0ce57dSAttilio Rao #include <sys/sleepqueue.h>
5622ee8c4fSPoul-Henning Kamp #include <sys/sysctl.h>
578d809d50SJeff Roberson #include <sys/smp.h>
58df8bae1dSRodney W. Grimes 
591283e9cdSAttilio Rao #ifdef SMP
601283e9cdSAttilio Rao #include <machine/cpu.h>
611283e9cdSAttilio Rao #endif
621283e9cdSAttilio Rao 
6391dd9aaeSRobert Watson SDT_PROVIDER_DEFINE(callout_execute);
6479856499SRui Paulo SDT_PROBE_DEFINE(callout_execute, kernel, , callout_start, callout-start);
6591dd9aaeSRobert Watson SDT_PROBE_ARGTYPE(callout_execute, kernel, , callout_start, 0,
6691dd9aaeSRobert Watson     "struct callout *");
6779856499SRui Paulo SDT_PROBE_DEFINE(callout_execute, kernel, , callout_end, callout-end);
6891dd9aaeSRobert Watson SDT_PROBE_ARGTYPE(callout_execute, kernel, , callout_end, 0,
6991dd9aaeSRobert Watson     "struct callout *");
7091dd9aaeSRobert Watson 
7122ee8c4fSPoul-Henning Kamp static int avg_depth;
7222ee8c4fSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0,
7322ee8c4fSPoul-Henning Kamp     "Average number of items examined per softclock call. Units = 1/1000");
7422ee8c4fSPoul-Henning Kamp static int avg_gcalls;
7522ee8c4fSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0,
7622ee8c4fSPoul-Henning Kamp     "Average number of Giant callouts made per softclock call. Units = 1/1000");
7764b9ee20SAttilio Rao static int avg_lockcalls;
7864b9ee20SAttilio Rao SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls, CTLFLAG_RD, &avg_lockcalls, 0,
7964b9ee20SAttilio Rao     "Average number of lock callouts made per softclock call. Units = 1/1000");
8022ee8c4fSPoul-Henning Kamp static int avg_mpcalls;
8122ee8c4fSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0,
8222ee8c4fSPoul-Henning Kamp     "Average number of MP callouts made per softclock call. Units = 1/1000");
8315b7a470SPoul-Henning Kamp /*
8415b7a470SPoul-Henning Kamp  * TODO:
8515b7a470SPoul-Henning Kamp  *	allocate more timeout table slots when table overflows.
8615b7a470SPoul-Henning Kamp  */
87ab36c067SJustin T. Gibbs int callwheelsize, callwheelbits, callwheelmask;
88f23b4c91SGarrett Wollman 
8920c510f8SLuigi Rizzo /*
901283e9cdSAttilio Rao  * The callout cpu migration entity represents informations necessary for
911283e9cdSAttilio Rao  * describing the migrating callout to the new callout cpu.
921283e9cdSAttilio Rao  * The cached informations are very important for deferring migration when
931283e9cdSAttilio Rao  * the migrating callout is already running.
941283e9cdSAttilio Rao  */
951283e9cdSAttilio Rao struct cc_mig_ent {
961283e9cdSAttilio Rao #ifdef SMP
971283e9cdSAttilio Rao 	void	(*ce_migration_func)(void *);
981283e9cdSAttilio Rao 	void	*ce_migration_arg;
991283e9cdSAttilio Rao 	int	ce_migration_cpu;
1001283e9cdSAttilio Rao 	int	ce_migration_ticks;
1011283e9cdSAttilio Rao #endif
1021283e9cdSAttilio Rao };
1031283e9cdSAttilio Rao 
1041283e9cdSAttilio Rao /*
10520c510f8SLuigi Rizzo  * There is one struct callout_cpu per cpu, holding all relevant
10620c510f8SLuigi Rizzo  * state for the callout processing thread on the individual CPU.
10720c510f8SLuigi Rizzo  * In particular:
10820c510f8SLuigi Rizzo  *	cc_ticks is incremented once per tick in callout_cpu().
10920c510f8SLuigi Rizzo  *	It tracks the global 'ticks' but in a way that the individual
11020c510f8SLuigi Rizzo  *	threads should not worry about races in the order in which
11120c510f8SLuigi Rizzo  *	hardclock() and hardclock_cpu() run on the various CPUs.
11220c510f8SLuigi Rizzo  *	cc_softclock is advanced in callout_cpu() to point to the
11320c510f8SLuigi Rizzo  *	first entry in cc_callwheel that may need handling. In turn,
11420c510f8SLuigi Rizzo  *	a softclock() is scheduled so it can serve the various entries i
11520c510f8SLuigi Rizzo  *	such that cc_softclock <= i <= cc_ticks .
11620c510f8SLuigi Rizzo  *	XXX maybe cc_softclock and cc_ticks should be volatile ?
11720c510f8SLuigi Rizzo  *
11820c510f8SLuigi Rizzo  *	cc_ticks is also used in callout_reset_cpu() to determine
11920c510f8SLuigi Rizzo  *	when the callout should be served.
12020c510f8SLuigi Rizzo  */
1218d809d50SJeff Roberson struct callout_cpu {
122*4ceaf45dSAttilio Rao 	struct mtx_padalign	cc_lock;
123*4ceaf45dSAttilio Rao 	struct cc_mig_ent	cc_migrating_entity;
1248d809d50SJeff Roberson 	struct callout		*cc_callout;
1258d809d50SJeff Roberson 	struct callout_tailq	*cc_callwheel;
1268d809d50SJeff Roberson 	struct callout_list	cc_callfree;
1278d809d50SJeff Roberson 	struct callout		*cc_next;
1288d809d50SJeff Roberson 	struct callout		*cc_curr;
1298d809d50SJeff Roberson 	void			*cc_cookie;
13020c510f8SLuigi Rizzo 	int 			cc_ticks;
1318d809d50SJeff Roberson 	int 			cc_softticks;
1328d809d50SJeff Roberson 	int			cc_cancel;
1338d809d50SJeff Roberson 	int			cc_waiting;
134a157e425SAlexander Motin 	int 			cc_firsttick;
1358d809d50SJeff Roberson };
1368d809d50SJeff Roberson 
1378d809d50SJeff Roberson #ifdef SMP
1381283e9cdSAttilio Rao #define	cc_migration_func	cc_migrating_entity.ce_migration_func
1391283e9cdSAttilio Rao #define	cc_migration_arg	cc_migrating_entity.ce_migration_arg
1401283e9cdSAttilio Rao #define	cc_migration_cpu	cc_migrating_entity.ce_migration_cpu
1411283e9cdSAttilio Rao #define	cc_migration_ticks	cc_migrating_entity.ce_migration_ticks
1421283e9cdSAttilio Rao 
1438d809d50SJeff Roberson struct callout_cpu cc_cpu[MAXCPU];
1441283e9cdSAttilio Rao #define	CPUBLOCK	MAXCPU
1458d809d50SJeff Roberson #define	CC_CPU(cpu)	(&cc_cpu[(cpu)])
1468d809d50SJeff Roberson #define	CC_SELF()	CC_CPU(PCPU_GET(cpuid))
1478d809d50SJeff Roberson #else
1488d809d50SJeff Roberson struct callout_cpu cc_cpu;
1498d809d50SJeff Roberson #define	CC_CPU(cpu)	&cc_cpu
1508d809d50SJeff Roberson #define	CC_SELF()	&cc_cpu
1518d809d50SJeff Roberson #endif
1528d809d50SJeff Roberson #define	CC_LOCK(cc)	mtx_lock_spin(&(cc)->cc_lock)
1538d809d50SJeff Roberson #define	CC_UNLOCK(cc)	mtx_unlock_spin(&(cc)->cc_lock)
1541283e9cdSAttilio Rao #define	CC_LOCK_ASSERT(cc)	mtx_assert(&(cc)->cc_lock, MA_OWNED)
1558d809d50SJeff Roberson 
1568d809d50SJeff Roberson static int timeout_cpu;
157a157e425SAlexander Motin void (*callout_new_inserted)(int cpu, int ticks) = NULL;
1588d809d50SJeff Roberson 
159d745c852SEd Schouten static MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures");
16049a74476SColin Percival 
161e9dec2c4SColin Percival /**
1628d809d50SJeff Roberson  * Locked by cc_lock:
1638d809d50SJeff Roberson  *   cc_curr         - If a callout is in progress, it is curr_callout.
164b36f4588SJohn Baldwin  *                     If curr_callout is non-NULL, threads waiting in
165b36f4588SJohn Baldwin  *                     callout_drain() will be woken up as soon as the
1662c1bb207SColin Percival  *                     relevant callout completes.
1678d809d50SJeff Roberson  *   cc_cancel       - Changing to 1 with both callout_lock and c_lock held
16898c926b2SIan Dowse  *                     guarantees that the current callout will not run.
16998c926b2SIan Dowse  *                     The softclock() function sets this to 0 before it
17064b9ee20SAttilio Rao  *                     drops callout_lock to acquire c_lock, and it calls
171b36f4588SJohn Baldwin  *                     the handler only if curr_cancelled is still 0 after
17264b9ee20SAttilio Rao  *                     c_lock is successfully acquired.
1738d809d50SJeff Roberson  *   cc_waiting      - If a thread is waiting in callout_drain(), then
174b36f4588SJohn Baldwin  *                     callout_wait is nonzero.  Set only when
1752c1bb207SColin Percival  *                     curr_callout is non-NULL.
1762c1bb207SColin Percival  */
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes /*
1791283e9cdSAttilio Rao  * Resets the migration entity tied to a specific callout cpu.
1801283e9cdSAttilio Rao  */
1811283e9cdSAttilio Rao static void
1821283e9cdSAttilio Rao cc_cme_cleanup(struct callout_cpu *cc)
1831283e9cdSAttilio Rao {
1841283e9cdSAttilio Rao 
1851283e9cdSAttilio Rao #ifdef SMP
1861283e9cdSAttilio Rao 	cc->cc_migration_cpu = CPUBLOCK;
1871283e9cdSAttilio Rao 	cc->cc_migration_ticks = 0;
1881283e9cdSAttilio Rao 	cc->cc_migration_func = NULL;
1891283e9cdSAttilio Rao 	cc->cc_migration_arg = NULL;
1901283e9cdSAttilio Rao #endif
1911283e9cdSAttilio Rao }
1921283e9cdSAttilio Rao 
1931283e9cdSAttilio Rao /*
1941283e9cdSAttilio Rao  * Checks if migration is requested by a specific callout cpu.
1951283e9cdSAttilio Rao  */
1961283e9cdSAttilio Rao static int
1971283e9cdSAttilio Rao cc_cme_migrating(struct callout_cpu *cc)
1981283e9cdSAttilio Rao {
1991283e9cdSAttilio Rao 
2001283e9cdSAttilio Rao #ifdef SMP
2011283e9cdSAttilio Rao 	return (cc->cc_migration_cpu != CPUBLOCK);
2021283e9cdSAttilio Rao #else
2031283e9cdSAttilio Rao 	return (0);
2041283e9cdSAttilio Rao #endif
2051283e9cdSAttilio Rao }
2061283e9cdSAttilio Rao 
2071283e9cdSAttilio Rao /*
208219d632cSMatthew Dillon  * kern_timeout_callwheel_alloc() - kernel low level callwheel initialization
209219d632cSMatthew Dillon  *
210219d632cSMatthew Dillon  *	This code is called very early in the kernel initialization sequence,
211219d632cSMatthew Dillon  *	and may be called more then once.
212219d632cSMatthew Dillon  */
213219d632cSMatthew Dillon caddr_t
214219d632cSMatthew Dillon kern_timeout_callwheel_alloc(caddr_t v)
215219d632cSMatthew Dillon {
2168d809d50SJeff Roberson 	struct callout_cpu *cc;
2178d809d50SJeff Roberson 
2188d809d50SJeff Roberson 	timeout_cpu = PCPU_GET(cpuid);
2198d809d50SJeff Roberson 	cc = CC_CPU(timeout_cpu);
220219d632cSMatthew Dillon 	/*
221219d632cSMatthew Dillon 	 * Calculate callout wheel size
222219d632cSMatthew Dillon 	 */
223219d632cSMatthew Dillon 	for (callwheelsize = 1, callwheelbits = 0;
224219d632cSMatthew Dillon 	     callwheelsize < ncallout;
225219d632cSMatthew Dillon 	     callwheelsize <<= 1, ++callwheelbits)
226219d632cSMatthew Dillon 		;
227219d632cSMatthew Dillon 	callwheelmask = callwheelsize - 1;
228219d632cSMatthew Dillon 
2298d809d50SJeff Roberson 	cc->cc_callout = (struct callout *)v;
2308d809d50SJeff Roberson 	v = (caddr_t)(cc->cc_callout + ncallout);
2318d809d50SJeff Roberson 	cc->cc_callwheel = (struct callout_tailq *)v;
2328d809d50SJeff Roberson 	v = (caddr_t)(cc->cc_callwheel + callwheelsize);
233219d632cSMatthew Dillon 	return(v);
234219d632cSMatthew Dillon }
235219d632cSMatthew Dillon 
2368d809d50SJeff Roberson static void
2378d809d50SJeff Roberson callout_cpu_init(struct callout_cpu *cc)
2388d809d50SJeff Roberson {
2398d809d50SJeff Roberson 	struct callout *c;
2408d809d50SJeff Roberson 	int i;
2418d809d50SJeff Roberson 
2428d809d50SJeff Roberson 	mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
2438d809d50SJeff Roberson 	SLIST_INIT(&cc->cc_callfree);
2448d809d50SJeff Roberson 	for (i = 0; i < callwheelsize; i++) {
2458d809d50SJeff Roberson 		TAILQ_INIT(&cc->cc_callwheel[i]);
2468d809d50SJeff Roberson 	}
2471283e9cdSAttilio Rao 	cc_cme_cleanup(cc);
2488d809d50SJeff Roberson 	if (cc->cc_callout == NULL)
2498d809d50SJeff Roberson 		return;
2508d809d50SJeff Roberson 	for (i = 0; i < ncallout; i++) {
2518d809d50SJeff Roberson 		c = &cc->cc_callout[i];
2528d809d50SJeff Roberson 		callout_init(c, 0);
2538d809d50SJeff Roberson 		c->c_flags = CALLOUT_LOCAL_ALLOC;
2548d809d50SJeff Roberson 		SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
2558d809d50SJeff Roberson 	}
2568d809d50SJeff Roberson }
2578d809d50SJeff Roberson 
2581283e9cdSAttilio Rao #ifdef SMP
2591283e9cdSAttilio Rao /*
2601283e9cdSAttilio Rao  * Switches the cpu tied to a specific callout.
2611283e9cdSAttilio Rao  * The function expects a locked incoming callout cpu and returns with
2621283e9cdSAttilio Rao  * locked outcoming callout cpu.
2631283e9cdSAttilio Rao  */
2641283e9cdSAttilio Rao static struct callout_cpu *
2651283e9cdSAttilio Rao callout_cpu_switch(struct callout *c, struct callout_cpu *cc, int new_cpu)
2661283e9cdSAttilio Rao {
2671283e9cdSAttilio Rao 	struct callout_cpu *new_cc;
2681283e9cdSAttilio Rao 
2691283e9cdSAttilio Rao 	MPASS(c != NULL && cc != NULL);
2701283e9cdSAttilio Rao 	CC_LOCK_ASSERT(cc);
2711283e9cdSAttilio Rao 
272e75baa28SAttilio Rao 	/*
273e75baa28SAttilio Rao 	 * Avoid interrupts and preemption firing after the callout cpu
274e75baa28SAttilio Rao 	 * is blocked in order to avoid deadlocks as the new thread
275e75baa28SAttilio Rao 	 * may be willing to acquire the callout cpu lock.
276e75baa28SAttilio Rao 	 */
2771283e9cdSAttilio Rao 	c->c_cpu = CPUBLOCK;
278e75baa28SAttilio Rao 	spinlock_enter();
2791283e9cdSAttilio Rao 	CC_UNLOCK(cc);
2801283e9cdSAttilio Rao 	new_cc = CC_CPU(new_cpu);
2811283e9cdSAttilio Rao 	CC_LOCK(new_cc);
282e75baa28SAttilio Rao 	spinlock_exit();
2831283e9cdSAttilio Rao 	c->c_cpu = new_cpu;
2841283e9cdSAttilio Rao 	return (new_cc);
2851283e9cdSAttilio Rao }
2861283e9cdSAttilio Rao #endif
2871283e9cdSAttilio Rao 
288219d632cSMatthew Dillon /*
289219d632cSMatthew Dillon  * kern_timeout_callwheel_init() - initialize previously reserved callwheel
290219d632cSMatthew Dillon  *				   space.
291219d632cSMatthew Dillon  *
292219d632cSMatthew Dillon  *	This code is called just once, after the space reserved for the
293219d632cSMatthew Dillon  *	callout wheel has been finalized.
294219d632cSMatthew Dillon  */
295219d632cSMatthew Dillon void
296219d632cSMatthew Dillon kern_timeout_callwheel_init(void)
297219d632cSMatthew Dillon {
2988d809d50SJeff Roberson 	callout_cpu_init(CC_CPU(timeout_cpu));
2998d809d50SJeff Roberson }
300219d632cSMatthew Dillon 
3018d809d50SJeff Roberson /*
3028d809d50SJeff Roberson  * Start standard softclock thread.
3038d809d50SJeff Roberson  */
3048d809d50SJeff Roberson static void
3058d809d50SJeff Roberson start_softclock(void *dummy)
3068d809d50SJeff Roberson {
3078d809d50SJeff Roberson 	struct callout_cpu *cc;
3088d809d50SJeff Roberson #ifdef SMP
3098d809d50SJeff Roberson 	int cpu;
3108d809d50SJeff Roberson #endif
3118d809d50SJeff Roberson 
3128d809d50SJeff Roberson 	cc = CC_CPU(timeout_cpu);
3138d809d50SJeff Roberson 	if (swi_add(&clk_intr_event, "clock", softclock, cc, SWI_CLOCK,
3143350df48SJohn Baldwin 	    INTR_MPSAFE, &cc->cc_cookie))
3158d809d50SJeff Roberson 		panic("died while creating standard software ithreads");
3168d809d50SJeff Roberson #ifdef SMP
3173aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
3188d809d50SJeff Roberson 		if (cpu == timeout_cpu)
3198d809d50SJeff Roberson 			continue;
3208d809d50SJeff Roberson 		cc = CC_CPU(cpu);
3218d809d50SJeff Roberson 		if (swi_add(NULL, "clock", softclock, cc, SWI_CLOCK,
3228d809d50SJeff Roberson 		    INTR_MPSAFE, &cc->cc_cookie))
3238d809d50SJeff Roberson 			panic("died while creating standard software ithreads");
3248d809d50SJeff Roberson 		cc->cc_callout = NULL;	/* Only cpu0 handles timeout(). */
3258d809d50SJeff Roberson 		cc->cc_callwheel = malloc(
3268d809d50SJeff Roberson 		    sizeof(struct callout_tailq) * callwheelsize, M_CALLOUT,
3278d809d50SJeff Roberson 		    M_WAITOK);
3288d809d50SJeff Roberson 		callout_cpu_init(cc);
329219d632cSMatthew Dillon 	}
3308d809d50SJeff Roberson #endif
331219d632cSMatthew Dillon }
3328d809d50SJeff Roberson 
3338d809d50SJeff Roberson SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL);
3348d809d50SJeff Roberson 
3358d809d50SJeff Roberson void
3368d809d50SJeff Roberson callout_tick(void)
3378d809d50SJeff Roberson {
3388d809d50SJeff Roberson 	struct callout_cpu *cc;
3399fc51b0bSJeff Roberson 	int need_softclock;
3409fc51b0bSJeff Roberson 	int bucket;
3418d809d50SJeff Roberson 
3428d809d50SJeff Roberson 	/*
3438d809d50SJeff Roberson 	 * Process callouts at a very low cpu priority, so we don't keep the
3448d809d50SJeff Roberson 	 * relatively high clock interrupt priority any longer than necessary.
3458d809d50SJeff Roberson 	 */
3469fc51b0bSJeff Roberson 	need_softclock = 0;
3478d809d50SJeff Roberson 	cc = CC_SELF();
3488d809d50SJeff Roberson 	mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
349a157e425SAlexander Motin 	cc->cc_firsttick = cc->cc_ticks = ticks;
35020c510f8SLuigi Rizzo 	for (; (cc->cc_softticks - cc->cc_ticks) <= 0; cc->cc_softticks++) {
3519fc51b0bSJeff Roberson 		bucket = cc->cc_softticks & callwheelmask;
3529fc51b0bSJeff Roberson 		if (!TAILQ_EMPTY(&cc->cc_callwheel[bucket])) {
3538d809d50SJeff Roberson 			need_softclock = 1;
3549fc51b0bSJeff Roberson 			break;
3559fc51b0bSJeff Roberson 		}
3569fc51b0bSJeff Roberson 	}
3578d809d50SJeff Roberson 	mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
3588d809d50SJeff Roberson 	/*
3598d809d50SJeff Roberson 	 * swi_sched acquires the thread lock, so we don't want to call it
3608d809d50SJeff Roberson 	 * with cc_lock held; incorrect locking order.
3618d809d50SJeff Roberson 	 */
3628d809d50SJeff Roberson 	if (need_softclock)
3638d809d50SJeff Roberson 		swi_sched(cc->cc_cookie, 0);
3648d809d50SJeff Roberson }
3658d809d50SJeff Roberson 
366a157e425SAlexander Motin int
3670e189873SAlexander Motin callout_tickstofirst(int limit)
368a157e425SAlexander Motin {
369a157e425SAlexander Motin 	struct callout_cpu *cc;
370a157e425SAlexander Motin 	struct callout *c;
371a157e425SAlexander Motin 	struct callout_tailq *sc;
372a157e425SAlexander Motin 	int curticks;
373a157e425SAlexander Motin 	int skip = 1;
374a157e425SAlexander Motin 
375a157e425SAlexander Motin 	cc = CC_SELF();
376a157e425SAlexander Motin 	mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
377a157e425SAlexander Motin 	curticks = cc->cc_ticks;
3780e189873SAlexander Motin 	while( skip < ncallout && skip < limit ) {
379a157e425SAlexander Motin 		sc = &cc->cc_callwheel[ (curticks+skip) & callwheelmask ];
380a157e425SAlexander Motin 		/* search scanning ticks */
381a157e425SAlexander Motin 		TAILQ_FOREACH( c, sc, c_links.tqe ){
382189795feSAlexander Motin 			if (c->c_time - curticks <= ncallout)
383a157e425SAlexander Motin 				goto out;
384a157e425SAlexander Motin 		}
385a157e425SAlexander Motin 		skip++;
386a157e425SAlexander Motin 	}
387a157e425SAlexander Motin out:
388a157e425SAlexander Motin 	cc->cc_firsttick = curticks + skip;
389a157e425SAlexander Motin 	mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
390a157e425SAlexander Motin 	return (skip);
391a157e425SAlexander Motin }
392a157e425SAlexander Motin 
3938d809d50SJeff Roberson static struct callout_cpu *
3948d809d50SJeff Roberson callout_lock(struct callout *c)
3958d809d50SJeff Roberson {
3968d809d50SJeff Roberson 	struct callout_cpu *cc;
3978d809d50SJeff Roberson 	int cpu;
3988d809d50SJeff Roberson 
3998d809d50SJeff Roberson 	for (;;) {
4008d809d50SJeff Roberson 		cpu = c->c_cpu;
4011283e9cdSAttilio Rao #ifdef SMP
4021283e9cdSAttilio Rao 		if (cpu == CPUBLOCK) {
4031283e9cdSAttilio Rao 			while (c->c_cpu == CPUBLOCK)
4041283e9cdSAttilio Rao 				cpu_spinwait();
4051283e9cdSAttilio Rao 			continue;
4061283e9cdSAttilio Rao 		}
4071283e9cdSAttilio Rao #endif
4088d809d50SJeff Roberson 		cc = CC_CPU(cpu);
4098d809d50SJeff Roberson 		CC_LOCK(cc);
4108d809d50SJeff Roberson 		if (cpu == c->c_cpu)
4118d809d50SJeff Roberson 			break;
4128d809d50SJeff Roberson 		CC_UNLOCK(cc);
4138d809d50SJeff Roberson 	}
4148d809d50SJeff Roberson 	return (cc);
415219d632cSMatthew Dillon }
416219d632cSMatthew Dillon 
4171283e9cdSAttilio Rao static void
4181283e9cdSAttilio Rao callout_cc_add(struct callout *c, struct callout_cpu *cc, int to_ticks,
4191283e9cdSAttilio Rao     void (*func)(void *), void *arg, int cpu)
4201283e9cdSAttilio Rao {
4211283e9cdSAttilio Rao 
4221283e9cdSAttilio Rao 	CC_LOCK_ASSERT(cc);
4231283e9cdSAttilio Rao 
4241283e9cdSAttilio Rao 	if (to_ticks <= 0)
4251283e9cdSAttilio Rao 		to_ticks = 1;
4261283e9cdSAttilio Rao 	c->c_arg = arg;
4271283e9cdSAttilio Rao 	c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
4281283e9cdSAttilio Rao 	c->c_func = func;
4291283e9cdSAttilio Rao 	c->c_time = ticks + to_ticks;
4301283e9cdSAttilio Rao 	TAILQ_INSERT_TAIL(&cc->cc_callwheel[c->c_time & callwheelmask],
4311283e9cdSAttilio Rao 	    c, c_links.tqe);
4321283e9cdSAttilio Rao 	if ((c->c_time - cc->cc_firsttick) < 0 &&
4331283e9cdSAttilio Rao 	    callout_new_inserted != NULL) {
4341283e9cdSAttilio Rao 		cc->cc_firsttick = c->c_time;
4351283e9cdSAttilio Rao 		(*callout_new_inserted)(cpu,
4361283e9cdSAttilio Rao 		    to_ticks + (ticks - cc->cc_ticks));
4371283e9cdSAttilio Rao 	}
4381283e9cdSAttilio Rao }
4391283e9cdSAttilio Rao 
4406098e7acSKonstantin Belousov static void
4416098e7acSKonstantin Belousov callout_cc_del(struct callout *c, struct callout_cpu *cc)
4426098e7acSKonstantin Belousov {
4436098e7acSKonstantin Belousov 
4446098e7acSKonstantin Belousov 	if (cc->cc_next == c)
4456098e7acSKonstantin Belousov 		cc->cc_next = TAILQ_NEXT(c, c_links.tqe);
4466098e7acSKonstantin Belousov 	if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
4476098e7acSKonstantin Belousov 		c->c_func = NULL;
4486098e7acSKonstantin Belousov 		SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
4496098e7acSKonstantin Belousov 	}
4506098e7acSKonstantin Belousov }
4516098e7acSKonstantin Belousov 
4526098e7acSKonstantin Belousov static struct callout *
4536098e7acSKonstantin Belousov softclock_call_cc(struct callout *c, struct callout_cpu *cc, int *mpcalls,
4546098e7acSKonstantin Belousov     int *lockcalls, int *gcalls)
4556098e7acSKonstantin Belousov {
4566098e7acSKonstantin Belousov 	void (*c_func)(void *);
4576098e7acSKonstantin Belousov 	void *c_arg;
4586098e7acSKonstantin Belousov 	struct lock_class *class;
4596098e7acSKonstantin Belousov 	struct lock_object *c_lock;
4606098e7acSKonstantin Belousov 	int c_flags, sharedlock;
4616098e7acSKonstantin Belousov #ifdef SMP
4626098e7acSKonstantin Belousov 	struct callout_cpu *new_cc;
4636098e7acSKonstantin Belousov 	void (*new_func)(void *);
4646098e7acSKonstantin Belousov 	void *new_arg;
4656098e7acSKonstantin Belousov 	int new_cpu, new_ticks;
4666098e7acSKonstantin Belousov #endif
4676098e7acSKonstantin Belousov #ifdef DIAGNOSTIC
4686098e7acSKonstantin Belousov 	struct bintime bt1, bt2;
4696098e7acSKonstantin Belousov 	struct timespec ts2;
4706098e7acSKonstantin Belousov 	static uint64_t maxdt = 36893488147419102LL;	/* 2 msec */
4716098e7acSKonstantin Belousov 	static timeout_t *lastfunc;
4726098e7acSKonstantin Belousov #endif
4736098e7acSKonstantin Belousov 
4746098e7acSKonstantin Belousov 	cc->cc_next = TAILQ_NEXT(c, c_links.tqe);
4756098e7acSKonstantin Belousov 	class = (c->c_lock != NULL) ? LOCK_CLASS(c->c_lock) : NULL;
4766098e7acSKonstantin Belousov 	sharedlock = (c->c_flags & CALLOUT_SHAREDLOCK) ? 0 : 1;
4776098e7acSKonstantin Belousov 	c_lock = c->c_lock;
4786098e7acSKonstantin Belousov 	c_func = c->c_func;
4796098e7acSKonstantin Belousov 	c_arg = c->c_arg;
4806098e7acSKonstantin Belousov 	c_flags = c->c_flags;
4816098e7acSKonstantin Belousov 	if (c->c_flags & CALLOUT_LOCAL_ALLOC)
4826098e7acSKonstantin Belousov 		c->c_flags = CALLOUT_LOCAL_ALLOC;
4836098e7acSKonstantin Belousov 	else
4846098e7acSKonstantin Belousov 		c->c_flags &= ~CALLOUT_PENDING;
4856098e7acSKonstantin Belousov 	cc->cc_curr = c;
4866098e7acSKonstantin Belousov 	cc->cc_cancel = 0;
4876098e7acSKonstantin Belousov 	CC_UNLOCK(cc);
4886098e7acSKonstantin Belousov 	if (c_lock != NULL) {
4896098e7acSKonstantin Belousov 		class->lc_lock(c_lock, sharedlock);
4906098e7acSKonstantin Belousov 		/*
4916098e7acSKonstantin Belousov 		 * The callout may have been cancelled
4926098e7acSKonstantin Belousov 		 * while we switched locks.
4936098e7acSKonstantin Belousov 		 */
4946098e7acSKonstantin Belousov 		if (cc->cc_cancel) {
4956098e7acSKonstantin Belousov 			class->lc_unlock(c_lock);
4966098e7acSKonstantin Belousov 			goto skip;
4976098e7acSKonstantin Belousov 		}
4986098e7acSKonstantin Belousov 		/* The callout cannot be stopped now. */
4996098e7acSKonstantin Belousov 		cc->cc_cancel = 1;
5006098e7acSKonstantin Belousov 
5016098e7acSKonstantin Belousov 		if (c_lock == &Giant.lock_object) {
5026098e7acSKonstantin Belousov 			(*gcalls)++;
5036098e7acSKonstantin Belousov 			CTR3(KTR_CALLOUT, "callout %p func %p arg %p",
5046098e7acSKonstantin Belousov 			    c, c_func, c_arg);
5056098e7acSKonstantin Belousov 		} else {
5066098e7acSKonstantin Belousov 			(*lockcalls)++;
5076098e7acSKonstantin Belousov 			CTR3(KTR_CALLOUT, "callout lock %p func %p arg %p",
5086098e7acSKonstantin Belousov 			    c, c_func, c_arg);
5096098e7acSKonstantin Belousov 		}
5106098e7acSKonstantin Belousov 	} else {
5116098e7acSKonstantin Belousov 		(*mpcalls)++;
5126098e7acSKonstantin Belousov 		CTR3(KTR_CALLOUT, "callout mpsafe %p func %p arg %p",
5136098e7acSKonstantin Belousov 		    c, c_func, c_arg);
5146098e7acSKonstantin Belousov 	}
5156098e7acSKonstantin Belousov #ifdef DIAGNOSTIC
5166098e7acSKonstantin Belousov 	binuptime(&bt1);
5176098e7acSKonstantin Belousov #endif
5186098e7acSKonstantin Belousov 	THREAD_NO_SLEEPING();
5196098e7acSKonstantin Belousov 	SDT_PROBE(callout_execute, kernel, , callout_start, c, 0, 0, 0, 0);
5206098e7acSKonstantin Belousov 	c_func(c_arg);
5216098e7acSKonstantin Belousov 	SDT_PROBE(callout_execute, kernel, , callout_end, c, 0, 0, 0, 0);
5226098e7acSKonstantin Belousov 	THREAD_SLEEPING_OK();
5236098e7acSKonstantin Belousov #ifdef DIAGNOSTIC
5246098e7acSKonstantin Belousov 	binuptime(&bt2);
5256098e7acSKonstantin Belousov 	bintime_sub(&bt2, &bt1);
5266098e7acSKonstantin Belousov 	if (bt2.frac > maxdt) {
5276098e7acSKonstantin Belousov 		if (lastfunc != c_func || bt2.frac > maxdt * 2) {
5286098e7acSKonstantin Belousov 			bintime2timespec(&bt2, &ts2);
5296098e7acSKonstantin Belousov 			printf(
5306098e7acSKonstantin Belousov 		"Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
5316098e7acSKonstantin Belousov 			    c_func, c_arg, (intmax_t)ts2.tv_sec, ts2.tv_nsec);
5326098e7acSKonstantin Belousov 		}
5336098e7acSKonstantin Belousov 		maxdt = bt2.frac;
5346098e7acSKonstantin Belousov 		lastfunc = c_func;
5356098e7acSKonstantin Belousov 	}
5366098e7acSKonstantin Belousov #endif
5376098e7acSKonstantin Belousov 	CTR1(KTR_CALLOUT, "callout %p finished", c);
5386098e7acSKonstantin Belousov 	if ((c_flags & CALLOUT_RETURNUNLOCKED) == 0)
5396098e7acSKonstantin Belousov 		class->lc_unlock(c_lock);
5406098e7acSKonstantin Belousov skip:
5416098e7acSKonstantin Belousov 	CC_LOCK(cc);
5426098e7acSKonstantin Belousov 	/*
5436098e7acSKonstantin Belousov 	 * If the current callout is locally allocated (from
5446098e7acSKonstantin Belousov 	 * timeout(9)) then put it on the freelist.
5456098e7acSKonstantin Belousov 	 *
5466098e7acSKonstantin Belousov 	 * Note: we need to check the cached copy of c_flags because
5476098e7acSKonstantin Belousov 	 * if it was not local, then it's not safe to deref the
5486098e7acSKonstantin Belousov 	 * callout pointer.
5496098e7acSKonstantin Belousov 	 */
5506098e7acSKonstantin Belousov 	if (c_flags & CALLOUT_LOCAL_ALLOC) {
5516098e7acSKonstantin Belousov 		KASSERT(c->c_flags == CALLOUT_LOCAL_ALLOC,
5526098e7acSKonstantin Belousov 		    ("corrupted callout"));
5536098e7acSKonstantin Belousov 		c->c_func = NULL;
5546098e7acSKonstantin Belousov 		SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
5556098e7acSKonstantin Belousov 	}
5566098e7acSKonstantin Belousov 	cc->cc_curr = NULL;
5576098e7acSKonstantin Belousov 	if (cc->cc_waiting) {
5586098e7acSKonstantin Belousov 		/*
5596098e7acSKonstantin Belousov 		 * There is someone waiting for the
5606098e7acSKonstantin Belousov 		 * callout to complete.
5616098e7acSKonstantin Belousov 		 * If the callout was scheduled for
5626098e7acSKonstantin Belousov 		 * migration just cancel it.
5636098e7acSKonstantin Belousov 		 */
5646098e7acSKonstantin Belousov 		if (cc_cme_migrating(cc))
5656098e7acSKonstantin Belousov 			cc_cme_cleanup(cc);
5666098e7acSKonstantin Belousov 		cc->cc_waiting = 0;
5676098e7acSKonstantin Belousov 		CC_UNLOCK(cc);
5686098e7acSKonstantin Belousov 		wakeup(&cc->cc_waiting);
5696098e7acSKonstantin Belousov 		CC_LOCK(cc);
5706098e7acSKonstantin Belousov 	} else if (cc_cme_migrating(cc)) {
5716098e7acSKonstantin Belousov #ifdef SMP
5726098e7acSKonstantin Belousov 		/*
5736098e7acSKonstantin Belousov 		 * If the callout was scheduled for
5746098e7acSKonstantin Belousov 		 * migration just perform it now.
5756098e7acSKonstantin Belousov 		 */
5766098e7acSKonstantin Belousov 		new_cpu = cc->cc_migration_cpu;
5776098e7acSKonstantin Belousov 		new_ticks = cc->cc_migration_ticks;
5786098e7acSKonstantin Belousov 		new_func = cc->cc_migration_func;
5796098e7acSKonstantin Belousov 		new_arg = cc->cc_migration_arg;
5806098e7acSKonstantin Belousov 		cc_cme_cleanup(cc);
5816098e7acSKonstantin Belousov 
5826098e7acSKonstantin Belousov 		/*
5836098e7acSKonstantin Belousov 		 * Handle deferred callout stops
5846098e7acSKonstantin Belousov 		 */
5856098e7acSKonstantin Belousov 		if ((c->c_flags & CALLOUT_DFRMIGRATION) == 0) {
5866098e7acSKonstantin Belousov 			CTR3(KTR_CALLOUT,
5876098e7acSKonstantin Belousov 			     "deferred cancelled %p func %p arg %p",
5886098e7acSKonstantin Belousov 			     c, new_func, new_arg);
5896098e7acSKonstantin Belousov 			callout_cc_del(c, cc);
5906098e7acSKonstantin Belousov 			goto nextc;
5916098e7acSKonstantin Belousov 		}
5926098e7acSKonstantin Belousov 
5936098e7acSKonstantin Belousov 		c->c_flags &= ~CALLOUT_DFRMIGRATION;
5946098e7acSKonstantin Belousov 
5956098e7acSKonstantin Belousov 		/*
5966098e7acSKonstantin Belousov 		 * It should be assert here that the
5976098e7acSKonstantin Belousov 		 * callout is not destroyed but that
5986098e7acSKonstantin Belousov 		 * is not easy.
5996098e7acSKonstantin Belousov 		 */
6006098e7acSKonstantin Belousov 		new_cc = callout_cpu_switch(c, cc, new_cpu);
6016098e7acSKonstantin Belousov 		callout_cc_add(c, new_cc, new_ticks, new_func, new_arg,
6026098e7acSKonstantin Belousov 		    new_cpu);
6036098e7acSKonstantin Belousov 		CC_UNLOCK(new_cc);
6046098e7acSKonstantin Belousov 		CC_LOCK(cc);
6056098e7acSKonstantin Belousov #else
6066098e7acSKonstantin Belousov 		panic("migration should not happen");
6076098e7acSKonstantin Belousov #endif
6086098e7acSKonstantin Belousov 	}
6096098e7acSKonstantin Belousov #ifdef SMP
6106098e7acSKonstantin Belousov nextc:
6116098e7acSKonstantin Belousov #endif
6126098e7acSKonstantin Belousov 	return (cc->cc_next);
6136098e7acSKonstantin Belousov }
6146098e7acSKonstantin Belousov 
615219d632cSMatthew Dillon /*
616ab36c067SJustin T. Gibbs  * The callout mechanism is based on the work of Adam M. Costello and
617ab36c067SJustin T. Gibbs  * George Varghese, published in a technical report entitled "Redesigning
618ab36c067SJustin T. Gibbs  * the BSD Callout and Timer Facilities" and modified slightly for inclusion
619ab36c067SJustin T. Gibbs  * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
620024035e8SHiten Pandya  * used in this implementation was published by G. Varghese and T. Lauck in
621ab36c067SJustin T. Gibbs  * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
622ab36c067SJustin T. Gibbs  * the Efficient Implementation of a Timer Facility" in the Proceedings of
623ab36c067SJustin T. Gibbs  * the 11th ACM Annual Symposium on Operating Systems Principles,
624ab36c067SJustin T. Gibbs  * Austin, Texas Nov 1987.
625ab36c067SJustin T. Gibbs  */
626a50ec505SPoul-Henning Kamp 
627ab36c067SJustin T. Gibbs /*
628df8bae1dSRodney W. Grimes  * Software (low priority) clock interrupt.
629df8bae1dSRodney W. Grimes  * Run periodic events from timeout queue.
630df8bae1dSRodney W. Grimes  */
631df8bae1dSRodney W. Grimes void
6328d809d50SJeff Roberson softclock(void *arg)
633df8bae1dSRodney W. Grimes {
6348d809d50SJeff Roberson 	struct callout_cpu *cc;
635b336df68SPoul-Henning Kamp 	struct callout *c;
636b336df68SPoul-Henning Kamp 	struct callout_tailq *bucket;
637b336df68SPoul-Henning Kamp 	int curticks;
638b336df68SPoul-Henning Kamp 	int steps;	/* #steps since we last allowed interrupts */
63922ee8c4fSPoul-Henning Kamp 	int depth;
64022ee8c4fSPoul-Henning Kamp 	int mpcalls;
64164b9ee20SAttilio Rao 	int lockcalls;
64222ee8c4fSPoul-Henning Kamp 	int gcalls;
643df8bae1dSRodney W. Grimes 
64415b7a470SPoul-Henning Kamp #ifndef MAX_SOFTCLOCK_STEPS
64515b7a470SPoul-Henning Kamp #define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
64615b7a470SPoul-Henning Kamp #endif /* MAX_SOFTCLOCK_STEPS */
647ab36c067SJustin T. Gibbs 
64822ee8c4fSPoul-Henning Kamp 	mpcalls = 0;
64964b9ee20SAttilio Rao 	lockcalls = 0;
65022ee8c4fSPoul-Henning Kamp 	gcalls = 0;
65122ee8c4fSPoul-Henning Kamp 	depth = 0;
652ab36c067SJustin T. Gibbs 	steps = 0;
6538d809d50SJeff Roberson 	cc = (struct callout_cpu *)arg;
6548d809d50SJeff Roberson 	CC_LOCK(cc);
65520c510f8SLuigi Rizzo 	while (cc->cc_softticks - 1 != cc->cc_ticks) {
65645327611SJustin T. Gibbs 		/*
6578d809d50SJeff Roberson 		 * cc_softticks may be modified by hard clock, so cache
65845327611SJustin T. Gibbs 		 * it while we work on a given bucket.
65945327611SJustin T. Gibbs 		 */
6608d809d50SJeff Roberson 		curticks = cc->cc_softticks;
6619fc51b0bSJeff Roberson 		cc->cc_softticks++;
6628d809d50SJeff Roberson 		bucket = &cc->cc_callwheel[curticks & callwheelmask];
66345327611SJustin T. Gibbs 		c = TAILQ_FIRST(bucket);
6646098e7acSKonstantin Belousov 		while (c != NULL) {
66522ee8c4fSPoul-Henning Kamp 			depth++;
66645327611SJustin T. Gibbs 			if (c->c_time != curticks) {
667ab36c067SJustin T. Gibbs 				c = TAILQ_NEXT(c, c_links.tqe);
668ab36c067SJustin T. Gibbs 				++steps;
669ab36c067SJustin T. Gibbs 				if (steps >= MAX_SOFTCLOCK_STEPS) {
6708d809d50SJeff Roberson 					cc->cc_next = c;
67145327611SJustin T. Gibbs 					/* Give interrupts a chance. */
6728d809d50SJeff Roberson 					CC_UNLOCK(cc);
673ab32297dSJohn Baldwin 					;	/* nothing */
6748d809d50SJeff Roberson 					CC_LOCK(cc);
6758d809d50SJeff Roberson 					c = cc->cc_next;
676ab36c067SJustin T. Gibbs 					steps = 0;
677df8bae1dSRodney W. Grimes 				}
678ab36c067SJustin T. Gibbs 			} else {
67945327611SJustin T. Gibbs 				TAILQ_REMOVE(bucket, c, c_links.tqe);
6806098e7acSKonstantin Belousov 				c = softclock_call_cc(c, cc, &mpcalls,
6816098e7acSKonstantin Belousov 				    &lockcalls, &gcalls);
682ab36c067SJustin T. Gibbs 				steps = 0;
683ab36c067SJustin T. Gibbs 			}
684ab36c067SJustin T. Gibbs 		}
685ab36c067SJustin T. Gibbs 	}
68622ee8c4fSPoul-Henning Kamp 	avg_depth += (depth * 1000 - avg_depth) >> 8;
68722ee8c4fSPoul-Henning Kamp 	avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
68864b9ee20SAttilio Rao 	avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8;
68922ee8c4fSPoul-Henning Kamp 	avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
6908d809d50SJeff Roberson 	cc->cc_next = NULL;
6918d809d50SJeff Roberson 	CC_UNLOCK(cc);
692df8bae1dSRodney W. Grimes }
693df8bae1dSRodney W. Grimes 
694df8bae1dSRodney W. Grimes /*
695df8bae1dSRodney W. Grimes  * timeout --
696df8bae1dSRodney W. Grimes  *	Execute a function after a specified length of time.
697df8bae1dSRodney W. Grimes  *
698df8bae1dSRodney W. Grimes  * untimeout --
699df8bae1dSRodney W. Grimes  *	Cancel previous timeout function call.
700df8bae1dSRodney W. Grimes  *
701ab36c067SJustin T. Gibbs  * callout_handle_init --
702ab36c067SJustin T. Gibbs  *	Initialize a handle so that using it with untimeout is benign.
703ab36c067SJustin T. Gibbs  *
704df8bae1dSRodney W. Grimes  *	See AT&T BCI Driver Reference Manual for specification.  This
705ab36c067SJustin T. Gibbs  *	implementation differs from that one in that although an
706ab36c067SJustin T. Gibbs  *	identification value is returned from timeout, the original
707ab36c067SJustin T. Gibbs  *	arguments to timeout as well as the identifier are used to
708ab36c067SJustin T. Gibbs  *	identify entries for untimeout.
709df8bae1dSRodney W. Grimes  */
710ab36c067SJustin T. Gibbs struct callout_handle
711ab36c067SJustin T. Gibbs timeout(ftn, arg, to_ticks)
7128f03c6f1SBruce Evans 	timeout_t *ftn;
713df8bae1dSRodney W. Grimes 	void *arg;
714e82ac18eSJonathan Lemon 	int to_ticks;
715df8bae1dSRodney W. Grimes {
7168d809d50SJeff Roberson 	struct callout_cpu *cc;
717ab36c067SJustin T. Gibbs 	struct callout *new;
718ab36c067SJustin T. Gibbs 	struct callout_handle handle;
719df8bae1dSRodney W. Grimes 
7208d809d50SJeff Roberson 	cc = CC_CPU(timeout_cpu);
7218d809d50SJeff Roberson 	CC_LOCK(cc);
722df8bae1dSRodney W. Grimes 	/* Fill in the next free callout structure. */
7238d809d50SJeff Roberson 	new = SLIST_FIRST(&cc->cc_callfree);
724ab36c067SJustin T. Gibbs 	if (new == NULL)
725ab36c067SJustin T. Gibbs 		/* XXX Attempt to malloc first */
726df8bae1dSRodney W. Grimes 		panic("timeout table full");
7278d809d50SJeff Roberson 	SLIST_REMOVE_HEAD(&cc->cc_callfree, c_links.sle);
728acc8326dSGarrett Wollman 	callout_reset(new, to_ticks, ftn, arg);
729ab36c067SJustin T. Gibbs 	handle.callout = new;
7308d809d50SJeff Roberson 	CC_UNLOCK(cc);
7318d809d50SJeff Roberson 
732ab36c067SJustin T. Gibbs 	return (handle);
733df8bae1dSRodney W. Grimes }
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes void
736ab36c067SJustin T. Gibbs untimeout(ftn, arg, handle)
7378f03c6f1SBruce Evans 	timeout_t *ftn;
738df8bae1dSRodney W. Grimes 	void *arg;
739ab36c067SJustin T. Gibbs 	struct callout_handle handle;
740df8bae1dSRodney W. Grimes {
7418d809d50SJeff Roberson 	struct callout_cpu *cc;
742df8bae1dSRodney W. Grimes 
743ab36c067SJustin T. Gibbs 	/*
744ab36c067SJustin T. Gibbs 	 * Check for a handle that was initialized
745ab36c067SJustin T. Gibbs 	 * by callout_handle_init, but never used
746ab36c067SJustin T. Gibbs 	 * for a real timeout.
747ab36c067SJustin T. Gibbs 	 */
748ab36c067SJustin T. Gibbs 	if (handle.callout == NULL)
749ab36c067SJustin T. Gibbs 		return;
750df8bae1dSRodney W. Grimes 
7518d809d50SJeff Roberson 	cc = callout_lock(handle.callout);
752acc8326dSGarrett Wollman 	if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
753acc8326dSGarrett Wollman 		callout_stop(handle.callout);
7548d809d50SJeff Roberson 	CC_UNLOCK(cc);
755df8bae1dSRodney W. Grimes }
756df8bae1dSRodney W. Grimes 
7573c816944SBruce Evans void
758ab36c067SJustin T. Gibbs callout_handle_init(struct callout_handle *handle)
759ab36c067SJustin T. Gibbs {
760ab36c067SJustin T. Gibbs 	handle->callout = NULL;
761ab36c067SJustin T. Gibbs }
762ab36c067SJustin T. Gibbs 
763acc8326dSGarrett Wollman /*
764acc8326dSGarrett Wollman  * New interface; clients allocate their own callout structures.
765acc8326dSGarrett Wollman  *
766acc8326dSGarrett Wollman  * callout_reset() - establish or change a timeout
767acc8326dSGarrett Wollman  * callout_stop() - disestablish a timeout
768acc8326dSGarrett Wollman  * callout_init() - initialize a callout structure so that it can
769acc8326dSGarrett Wollman  *	safely be passed to callout_reset() and callout_stop()
770acc8326dSGarrett Wollman  *
7719b8b58e0SJonathan Lemon  * <sys/callout.h> defines three convenience macros:
772acc8326dSGarrett Wollman  *
77386fd19deSColin Percival  * callout_active() - returns truth if callout has not been stopped,
77486fd19deSColin Percival  *	drained, or deactivated since the last time the callout was
77586fd19deSColin Percival  *	reset.
7769b8b58e0SJonathan Lemon  * callout_pending() - returns truth if callout is still waiting for timeout
7779b8b58e0SJonathan Lemon  * callout_deactivate() - marks the callout as having been serviced
778acc8326dSGarrett Wollman  */
779d04304d1SGleb Smirnoff int
7808d809d50SJeff Roberson callout_reset_on(struct callout *c, int to_ticks, void (*ftn)(void *),
7818d809d50SJeff Roberson     void *arg, int cpu)
782acc8326dSGarrett Wollman {
7838d809d50SJeff Roberson 	struct callout_cpu *cc;
784d04304d1SGleb Smirnoff 	int cancelled = 0;
785acc8326dSGarrett Wollman 
7868d809d50SJeff Roberson 	/*
7878d809d50SJeff Roberson 	 * Don't allow migration of pre-allocated callouts lest they
7888d809d50SJeff Roberson 	 * become unbalanced.
7898d809d50SJeff Roberson 	 */
7908d809d50SJeff Roberson 	if (c->c_flags & CALLOUT_LOCAL_ALLOC)
7918d809d50SJeff Roberson 		cpu = c->c_cpu;
7928d809d50SJeff Roberson 	cc = callout_lock(c);
7938d809d50SJeff Roberson 	if (cc->cc_curr == c) {
7942c1bb207SColin Percival 		/*
7952c1bb207SColin Percival 		 * We're being asked to reschedule a callout which is
79664b9ee20SAttilio Rao 		 * currently in progress.  If there is a lock then we
79798c926b2SIan Dowse 		 * can cancel the callout if it has not really started.
79898c926b2SIan Dowse 		 */
7998d809d50SJeff Roberson 		if (c->c_lock != NULL && !cc->cc_cancel)
8008d809d50SJeff Roberson 			cancelled = cc->cc_cancel = 1;
8018d809d50SJeff Roberson 		if (cc->cc_waiting) {
80298c926b2SIan Dowse 			/*
80398c926b2SIan Dowse 			 * Someone has called callout_drain to kill this
80498c926b2SIan Dowse 			 * callout.  Don't reschedule.
8052c1bb207SColin Percival 			 */
80668a57ebfSGleb Smirnoff 			CTR4(KTR_CALLOUT, "%s %p func %p arg %p",
80768a57ebfSGleb Smirnoff 			    cancelled ? "cancelled" : "failed to cancel",
80868a57ebfSGleb Smirnoff 			    c, c->c_func, c->c_arg);
8098d809d50SJeff Roberson 			CC_UNLOCK(cc);
810d04304d1SGleb Smirnoff 			return (cancelled);
81149a74476SColin Percival 		}
81298c926b2SIan Dowse 	}
8130413bacdSColin Percival 	if (c->c_flags & CALLOUT_PENDING) {
8148d809d50SJeff Roberson 		if (cc->cc_next == c) {
8158d809d50SJeff Roberson 			cc->cc_next = TAILQ_NEXT(c, c_links.tqe);
8160413bacdSColin Percival 		}
8178d809d50SJeff Roberson 		TAILQ_REMOVE(&cc->cc_callwheel[c->c_time & callwheelmask], c,
8180413bacdSColin Percival 		    c_links.tqe);
8190413bacdSColin Percival 
820d04304d1SGleb Smirnoff 		cancelled = 1;
8218d809d50SJeff Roberson 		c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
8228d809d50SJeff Roberson 	}
8231283e9cdSAttilio Rao 
8241283e9cdSAttilio Rao #ifdef SMP
8250413bacdSColin Percival 	/*
8261283e9cdSAttilio Rao 	 * If the callout must migrate try to perform it immediately.
8271283e9cdSAttilio Rao 	 * If the callout is currently running, just defer the migration
8281283e9cdSAttilio Rao 	 * to a more appropriate moment.
8290413bacdSColin Percival 	 */
8308d809d50SJeff Roberson 	if (c->c_cpu != cpu) {
8311283e9cdSAttilio Rao 		if (cc->cc_curr == c) {
8321283e9cdSAttilio Rao 			cc->cc_migration_cpu = cpu;
8331283e9cdSAttilio Rao 			cc->cc_migration_ticks = to_ticks;
8341283e9cdSAttilio Rao 			cc->cc_migration_func = ftn;
8351283e9cdSAttilio Rao 			cc->cc_migration_arg = arg;
83657d07ca9SKonstantin Belousov 			c->c_flags |= CALLOUT_DFRMIGRATION;
8371283e9cdSAttilio Rao 			CTR5(KTR_CALLOUT,
8381283e9cdSAttilio Rao 		    "migration of %p func %p arg %p in %d to %u deferred",
8391283e9cdSAttilio Rao 			    c, c->c_func, c->c_arg, to_ticks, cpu);
84008e4ac8aSAttilio Rao 			CC_UNLOCK(cc);
8411283e9cdSAttilio Rao 			return (cancelled);
842a157e425SAlexander Motin 		}
8431283e9cdSAttilio Rao 		cc = callout_cpu_switch(c, cc, cpu);
84408e4ac8aSAttilio Rao 	}
8451283e9cdSAttilio Rao #endif
8461283e9cdSAttilio Rao 
8471283e9cdSAttilio Rao 	callout_cc_add(c, cc, to_ticks, ftn, arg, cpu);
84868a57ebfSGleb Smirnoff 	CTR5(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d",
84968a57ebfSGleb Smirnoff 	    cancelled ? "re" : "", c, c->c_func, c->c_arg, to_ticks);
8508d809d50SJeff Roberson 	CC_UNLOCK(cc);
851d04304d1SGleb Smirnoff 
852d04304d1SGleb Smirnoff 	return (cancelled);
853acc8326dSGarrett Wollman }
854acc8326dSGarrett Wollman 
8556e0186d5SSam Leffler /*
8566e0186d5SSam Leffler  * Common idioms that can be optimized in the future.
8576e0186d5SSam Leffler  */
8586e0186d5SSam Leffler int
8596e0186d5SSam Leffler callout_schedule_on(struct callout *c, int to_ticks, int cpu)
8606e0186d5SSam Leffler {
8616e0186d5SSam Leffler 	return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu);
8626e0186d5SSam Leffler }
8636e0186d5SSam Leffler 
8646e0186d5SSam Leffler int
8656e0186d5SSam Leffler callout_schedule(struct callout *c, int to_ticks)
8666e0186d5SSam Leffler {
8676e0186d5SSam Leffler 	return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu);
8686e0186d5SSam Leffler }
8696e0186d5SSam Leffler 
8702c1bb207SColin Percival int
8712c1bb207SColin Percival _callout_stop_safe(c, safe)
8722c1bb207SColin Percival 	struct	callout *c;
8732c1bb207SColin Percival 	int	safe;
8742c1bb207SColin Percival {
8751283e9cdSAttilio Rao 	struct callout_cpu *cc, *old_cc;
87664b9ee20SAttilio Rao 	struct lock_class *class;
87764b9ee20SAttilio Rao 	int use_lock, sq_locked;
87898c926b2SIan Dowse 
87964b9ee20SAttilio Rao 	/*
88064b9ee20SAttilio Rao 	 * Some old subsystems don't hold Giant while running a callout_stop(),
88164b9ee20SAttilio Rao 	 * so just discard this check for the moment.
88264b9ee20SAttilio Rao 	 */
88364b9ee20SAttilio Rao 	if (!safe && c->c_lock != NULL) {
88464b9ee20SAttilio Rao 		if (c->c_lock == &Giant.lock_object)
88564b9ee20SAttilio Rao 			use_lock = mtx_owned(&Giant);
88664b9ee20SAttilio Rao 		else {
88764b9ee20SAttilio Rao 			use_lock = 1;
88864b9ee20SAttilio Rao 			class = LOCK_CLASS(c->c_lock);
88964b9ee20SAttilio Rao 			class->lc_assert(c->c_lock, LA_XLOCKED);
89098c926b2SIan Dowse 		}
89164b9ee20SAttilio Rao 	} else
89264b9ee20SAttilio Rao 		use_lock = 0;
8932c1bb207SColin Percival 
89467b158d8SJohn Baldwin 	sq_locked = 0;
8951283e9cdSAttilio Rao 	old_cc = NULL;
89667b158d8SJohn Baldwin again:
8978d809d50SJeff Roberson 	cc = callout_lock(c);
8981283e9cdSAttilio Rao 
8991283e9cdSAttilio Rao 	/*
9001283e9cdSAttilio Rao 	 * If the callout was migrating while the callout cpu lock was
9011283e9cdSAttilio Rao 	 * dropped,  just drop the sleepqueue lock and check the states
9021283e9cdSAttilio Rao 	 * again.
9031283e9cdSAttilio Rao 	 */
9041283e9cdSAttilio Rao 	if (sq_locked != 0 && cc != old_cc) {
9051283e9cdSAttilio Rao #ifdef SMP
9061283e9cdSAttilio Rao 		CC_UNLOCK(cc);
9071283e9cdSAttilio Rao 		sleepq_release(&old_cc->cc_waiting);
9081283e9cdSAttilio Rao 		sq_locked = 0;
9091283e9cdSAttilio Rao 		old_cc = NULL;
9101283e9cdSAttilio Rao 		goto again;
9111283e9cdSAttilio Rao #else
9121283e9cdSAttilio Rao 		panic("migration should not happen");
9131283e9cdSAttilio Rao #endif
9141283e9cdSAttilio Rao 	}
9151283e9cdSAttilio Rao 
916acc8326dSGarrett Wollman 	/*
917b36f4588SJohn Baldwin 	 * If the callout isn't pending, it's not on the queue, so
918b36f4588SJohn Baldwin 	 * don't attempt to remove it from the queue.  We can try to
919b36f4588SJohn Baldwin 	 * stop it by other means however.
920acc8326dSGarrett Wollman 	 */
921acc8326dSGarrett Wollman 	if (!(c->c_flags & CALLOUT_PENDING)) {
9229b8b58e0SJonathan Lemon 		c->c_flags &= ~CALLOUT_ACTIVE;
923b36f4588SJohn Baldwin 
924b36f4588SJohn Baldwin 		/*
925b36f4588SJohn Baldwin 		 * If it wasn't on the queue and it isn't the current
926b36f4588SJohn Baldwin 		 * callout, then we can't stop it, so just bail.
927b36f4588SJohn Baldwin 		 */
9288d809d50SJeff Roberson 		if (cc->cc_curr != c) {
92968a57ebfSGleb Smirnoff 			CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
93068a57ebfSGleb Smirnoff 			    c, c->c_func, c->c_arg);
9318d809d50SJeff Roberson 			CC_UNLOCK(cc);
93267b158d8SJohn Baldwin 			if (sq_locked)
933ce62b59cSJeff Roberson 				sleepq_release(&cc->cc_waiting);
93498c926b2SIan Dowse 			return (0);
93598c926b2SIan Dowse 		}
936b36f4588SJohn Baldwin 
93798c926b2SIan Dowse 		if (safe) {
9382c1bb207SColin Percival 			/*
939b36f4588SJohn Baldwin 			 * The current callout is running (or just
940b36f4588SJohn Baldwin 			 * about to run) and blocking is allowed, so
941b36f4588SJohn Baldwin 			 * just wait for the current invocation to
942b36f4588SJohn Baldwin 			 * finish.
9432c1bb207SColin Percival 			 */
9448d809d50SJeff Roberson 			while (cc->cc_curr == c) {
9456a0ce57dSAttilio Rao 
9466a0ce57dSAttilio Rao 				/*
9476a0ce57dSAttilio Rao 				 * Use direct calls to sleepqueue interface
9486a0ce57dSAttilio Rao 				 * instead of cv/msleep in order to avoid
9498d809d50SJeff Roberson 				 * a LOR between cc_lock and sleepqueue
9506a0ce57dSAttilio Rao 				 * chain spinlocks.  This piece of code
9516a0ce57dSAttilio Rao 				 * emulates a msleep_spin() call actually.
95267b158d8SJohn Baldwin 				 *
95367b158d8SJohn Baldwin 				 * If we already have the sleepqueue chain
95467b158d8SJohn Baldwin 				 * locked, then we can safely block.  If we
95567b158d8SJohn Baldwin 				 * don't already have it locked, however,
9568d809d50SJeff Roberson 				 * we have to drop the cc_lock to lock
95767b158d8SJohn Baldwin 				 * it.  This opens several races, so we
95867b158d8SJohn Baldwin 				 * restart at the beginning once we have
95967b158d8SJohn Baldwin 				 * both locks.  If nothing has changed, then
96067b158d8SJohn Baldwin 				 * we will end up back here with sq_locked
96167b158d8SJohn Baldwin 				 * set.
9626a0ce57dSAttilio Rao 				 */
96367b158d8SJohn Baldwin 				if (!sq_locked) {
9648d809d50SJeff Roberson 					CC_UNLOCK(cc);
965ce62b59cSJeff Roberson 					sleepq_lock(&cc->cc_waiting);
96667b158d8SJohn Baldwin 					sq_locked = 1;
9671283e9cdSAttilio Rao 					old_cc = cc;
96867b158d8SJohn Baldwin 					goto again;
9696a0ce57dSAttilio Rao 				}
9701283e9cdSAttilio Rao 
9711283e9cdSAttilio Rao 				/*
9721283e9cdSAttilio Rao 				 * Migration could be cancelled here, but
9731283e9cdSAttilio Rao 				 * as long as it is still not sure when it
9741283e9cdSAttilio Rao 				 * will be packed up, just let softclock()
9751283e9cdSAttilio Rao 				 * take care of it.
9761283e9cdSAttilio Rao 				 */
9778d809d50SJeff Roberson 				cc->cc_waiting = 1;
9786a0ce57dSAttilio Rao 				DROP_GIANT();
9798d809d50SJeff Roberson 				CC_UNLOCK(cc);
980ce62b59cSJeff Roberson 				sleepq_add(&cc->cc_waiting,
9818d809d50SJeff Roberson 				    &cc->cc_lock.lock_object, "codrain",
9826a0ce57dSAttilio Rao 				    SLEEPQ_SLEEP, 0);
983ce62b59cSJeff Roberson 				sleepq_wait(&cc->cc_waiting, 0);
98467b158d8SJohn Baldwin 				sq_locked = 0;
9851283e9cdSAttilio Rao 				old_cc = NULL;
9866a0ce57dSAttilio Rao 
9876a0ce57dSAttilio Rao 				/* Reacquire locks previously released. */
9886a0ce57dSAttilio Rao 				PICKUP_GIANT();
9898d809d50SJeff Roberson 				CC_LOCK(cc);
990b36f4588SJohn Baldwin 			}
9918d809d50SJeff Roberson 		} else if (use_lock && !cc->cc_cancel) {
992b36f4588SJohn Baldwin 			/*
99364b9ee20SAttilio Rao 			 * The current callout is waiting for its
99464b9ee20SAttilio Rao 			 * lock which we hold.  Cancel the callout
995b36f4588SJohn Baldwin 			 * and return.  After our caller drops the
99664b9ee20SAttilio Rao 			 * lock, the callout will be skipped in
997b36f4588SJohn Baldwin 			 * softclock().
998b36f4588SJohn Baldwin 			 */
9998d809d50SJeff Roberson 			cc->cc_cancel = 1;
100068a57ebfSGleb Smirnoff 			CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
100168a57ebfSGleb Smirnoff 			    c, c->c_func, c->c_arg);
10021283e9cdSAttilio Rao 			KASSERT(!cc_cme_migrating(cc),
10031283e9cdSAttilio Rao 			    ("callout wrongly scheduled for migration"));
10048d809d50SJeff Roberson 			CC_UNLOCK(cc);
100567b158d8SJohn Baldwin 			KASSERT(!sq_locked, ("sleepqueue chain locked"));
100698c926b2SIan Dowse 			return (1);
100757d07ca9SKonstantin Belousov 		} else if ((c->c_flags & CALLOUT_DFRMIGRATION) != 0) {
100857d07ca9SKonstantin Belousov 			c->c_flags &= ~CALLOUT_DFRMIGRATION;
100957d07ca9SKonstantin Belousov 			CTR3(KTR_CALLOUT, "postponing stop %p func %p arg %p",
101057d07ca9SKonstantin Belousov 			    c, c->c_func, c->c_arg);
101157d07ca9SKonstantin Belousov 			CC_UNLOCK(cc);
101257d07ca9SKonstantin Belousov 			return (1);
1013b36f4588SJohn Baldwin 		}
101468a57ebfSGleb Smirnoff 		CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
101568a57ebfSGleb Smirnoff 		    c, c->c_func, c->c_arg);
10168d809d50SJeff Roberson 		CC_UNLOCK(cc);
101767b158d8SJohn Baldwin 		KASSERT(!sq_locked, ("sleepqueue chain still locked"));
1018a45982d2SJohn Baldwin 		return (0);
1019acc8326dSGarrett Wollman 	}
102067b158d8SJohn Baldwin 	if (sq_locked)
1021ce62b59cSJeff Roberson 		sleepq_release(&cc->cc_waiting);
102267b158d8SJohn Baldwin 
10239b8b58e0SJonathan Lemon 	c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
1024acc8326dSGarrett Wollman 
102568a57ebfSGleb Smirnoff 	CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
102668a57ebfSGleb Smirnoff 	    c, c->c_func, c->c_arg);
10276098e7acSKonstantin Belousov 	TAILQ_REMOVE(&cc->cc_callwheel[c->c_time & callwheelmask], c,
10286098e7acSKonstantin Belousov 	    c_links.tqe);
10296098e7acSKonstantin Belousov 	callout_cc_del(c, cc);
103068a57ebfSGleb Smirnoff 
10318d809d50SJeff Roberson 	CC_UNLOCK(cc);
1032a45982d2SJohn Baldwin 	return (1);
1033acc8326dSGarrett Wollman }
1034acc8326dSGarrett Wollman 
1035acc8326dSGarrett Wollman void
1036e82ac18eSJonathan Lemon callout_init(c, mpsafe)
1037acc8326dSGarrett Wollman 	struct	callout *c;
1038e82ac18eSJonathan Lemon 	int mpsafe;
1039acc8326dSGarrett Wollman {
10407347e1c6SGarrett Wollman 	bzero(c, sizeof *c);
104198c926b2SIan Dowse 	if (mpsafe) {
104264b9ee20SAttilio Rao 		c->c_lock = NULL;
104398c926b2SIan Dowse 		c->c_flags = CALLOUT_RETURNUNLOCKED;
104498c926b2SIan Dowse 	} else {
104564b9ee20SAttilio Rao 		c->c_lock = &Giant.lock_object;
104698c926b2SIan Dowse 		c->c_flags = 0;
104798c926b2SIan Dowse 	}
10488d809d50SJeff Roberson 	c->c_cpu = timeout_cpu;
104998c926b2SIan Dowse }
105098c926b2SIan Dowse 
105198c926b2SIan Dowse void
105264b9ee20SAttilio Rao _callout_init_lock(c, lock, flags)
105398c926b2SIan Dowse 	struct	callout *c;
105464b9ee20SAttilio Rao 	struct	lock_object *lock;
105598c926b2SIan Dowse 	int flags;
105698c926b2SIan Dowse {
105798c926b2SIan Dowse 	bzero(c, sizeof *c);
105864b9ee20SAttilio Rao 	c->c_lock = lock;
105964b9ee20SAttilio Rao 	KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0,
106064b9ee20SAttilio Rao 	    ("callout_init_lock: bad flags %d", flags));
106164b9ee20SAttilio Rao 	KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
106264b9ee20SAttilio Rao 	    ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock"));
106313ddf72dSAttilio Rao 	KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags &
106413ddf72dSAttilio Rao 	    (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class",
106564b9ee20SAttilio Rao 	    __func__));
106664b9ee20SAttilio Rao 	c->c_flags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK);
10678d809d50SJeff Roberson 	c->c_cpu = timeout_cpu;
1068acc8326dSGarrett Wollman }
1069acc8326dSGarrett Wollman 
1070e1d6dc65SNate Williams #ifdef APM_FIXUP_CALLTODO
1071e1d6dc65SNate Williams /*
1072e1d6dc65SNate Williams  * Adjust the kernel calltodo timeout list.  This routine is used after
1073e1d6dc65SNate Williams  * an APM resume to recalculate the calltodo timer list values with the
1074e1d6dc65SNate Williams  * number of hz's we have been sleeping.  The next hardclock() will detect
1075e1d6dc65SNate Williams  * that there are fired timers and run softclock() to execute them.
1076e1d6dc65SNate Williams  *
1077e1d6dc65SNate Williams  * Please note, I have not done an exhaustive analysis of what code this
1078e1d6dc65SNate Williams  * might break.  I am motivated to have my select()'s and alarm()'s that
1079e1d6dc65SNate Williams  * have expired during suspend firing upon resume so that the applications
1080e1d6dc65SNate Williams  * which set the timer can do the maintanence the timer was for as close
1081e1d6dc65SNate Williams  * as possible to the originally intended time.  Testing this code for a
1082e1d6dc65SNate Williams  * week showed that resuming from a suspend resulted in 22 to 25 timers
1083e1d6dc65SNate Williams  * firing, which seemed independant on whether the suspend was 2 hours or
1084e1d6dc65SNate Williams  * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
1085e1d6dc65SNate Williams  */
1086e1d6dc65SNate Williams void
1087e1d6dc65SNate Williams adjust_timeout_calltodo(time_change)
1088e1d6dc65SNate Williams     struct timeval *time_change;
1089e1d6dc65SNate Williams {
1090e1d6dc65SNate Williams 	register struct callout *p;
1091e1d6dc65SNate Williams 	unsigned long delta_ticks;
1092e1d6dc65SNate Williams 
1093e1d6dc65SNate Williams 	/*
1094e1d6dc65SNate Williams 	 * How many ticks were we asleep?
1095c8b47828SBruce Evans 	 * (stolen from tvtohz()).
1096e1d6dc65SNate Williams 	 */
1097e1d6dc65SNate Williams 
1098e1d6dc65SNate Williams 	/* Don't do anything */
1099e1d6dc65SNate Williams 	if (time_change->tv_sec < 0)
1100e1d6dc65SNate Williams 		return;
1101e1d6dc65SNate Williams 	else if (time_change->tv_sec <= LONG_MAX / 1000000)
1102e1d6dc65SNate Williams 		delta_ticks = (time_change->tv_sec * 1000000 +
1103e1d6dc65SNate Williams 			       time_change->tv_usec + (tick - 1)) / tick + 1;
1104e1d6dc65SNate Williams 	else if (time_change->tv_sec <= LONG_MAX / hz)
1105e1d6dc65SNate Williams 		delta_ticks = time_change->tv_sec * hz +
1106e1d6dc65SNate Williams 			      (time_change->tv_usec + (tick - 1)) / tick + 1;
1107e1d6dc65SNate Williams 	else
1108e1d6dc65SNate Williams 		delta_ticks = LONG_MAX;
1109e1d6dc65SNate Williams 
1110e1d6dc65SNate Williams 	if (delta_ticks > INT_MAX)
1111e1d6dc65SNate Williams 		delta_ticks = INT_MAX;
1112e1d6dc65SNate Williams 
1113e1d6dc65SNate Williams 	/*
1114e1d6dc65SNate Williams 	 * Now rip through the timer calltodo list looking for timers
1115e1d6dc65SNate Williams 	 * to expire.
1116e1d6dc65SNate Williams 	 */
1117e1d6dc65SNate Williams 
1118e1d6dc65SNate Williams 	/* don't collide with softclock() */
11198d809d50SJeff Roberson 	CC_LOCK(cc);
1120e1d6dc65SNate Williams 	for (p = calltodo.c_next; p != NULL; p = p->c_next) {
1121e1d6dc65SNate Williams 		p->c_time -= delta_ticks;
1122e1d6dc65SNate Williams 
1123e1d6dc65SNate Williams 		/* Break if the timer had more time on it than delta_ticks */
1124e1d6dc65SNate Williams 		if (p->c_time > 0)
1125e1d6dc65SNate Williams 			break;
1126e1d6dc65SNate Williams 
1127e1d6dc65SNate Williams 		/* take back the ticks the timer didn't use (p->c_time <= 0) */
1128e1d6dc65SNate Williams 		delta_ticks = -p->c_time;
1129e1d6dc65SNate Williams 	}
11308d809d50SJeff Roberson 	CC_UNLOCK(cc);
1131e1d6dc65SNate Williams 
1132e1d6dc65SNate Williams 	return;
1133e1d6dc65SNate Williams }
1134e1d6dc65SNate Williams #endif /* APM_FIXUP_CALLTODO */
1135