xref: /freebsd/sys/kern/kern_timeout.c (revision 0bf48626aaa33768078f5872b922b1487b3a9296)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	From: @(#)kern_clock.c	8.5 (Berkeley) 1/21/94
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include "opt_callout_profiling.h"
43 #include "opt_ddb.h"
44 #if defined(__arm__)
45 #include "opt_timer.h"
46 #endif
47 #include "opt_rss.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/bus.h>
52 #include <sys/callout.h>
53 #include <sys/file.h>
54 #include <sys/interrupt.h>
55 #include <sys/kernel.h>
56 #include <sys/ktr.h>
57 #include <sys/lock.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/proc.h>
61 #include <sys/sdt.h>
62 #include <sys/sleepqueue.h>
63 #include <sys/sysctl.h>
64 #include <sys/smp.h>
65 
66 #ifdef DDB
67 #include <ddb/ddb.h>
68 #include <ddb/db_sym.h>
69 #include <machine/_inttypes.h>
70 #endif
71 
72 #ifdef SMP
73 #include <machine/cpu.h>
74 #endif
75 
76 #ifndef NO_EVENTTIMERS
77 DPCPU_DECLARE(sbintime_t, hardclocktime);
78 #endif
79 
80 SDT_PROVIDER_DEFINE(callout_execute);
81 SDT_PROBE_DEFINE1(callout_execute, , , callout__start, "struct callout *");
82 SDT_PROBE_DEFINE1(callout_execute, , , callout__end, "struct callout *");
83 
84 #ifdef CALLOUT_PROFILING
85 static int avg_depth;
86 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0,
87     "Average number of items examined per softclock call. Units = 1/1000");
88 static int avg_gcalls;
89 SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0,
90     "Average number of Giant callouts made per softclock call. Units = 1/1000");
91 static int avg_lockcalls;
92 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls, CTLFLAG_RD, &avg_lockcalls, 0,
93     "Average number of lock callouts made per softclock call. Units = 1/1000");
94 static int avg_mpcalls;
95 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0,
96     "Average number of MP callouts made per softclock call. Units = 1/1000");
97 static int avg_depth_dir;
98 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth_dir, CTLFLAG_RD, &avg_depth_dir, 0,
99     "Average number of direct callouts examined per callout_process call. "
100     "Units = 1/1000");
101 static int avg_lockcalls_dir;
102 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls_dir, CTLFLAG_RD,
103     &avg_lockcalls_dir, 0, "Average number of lock direct callouts made per "
104     "callout_process call. Units = 1/1000");
105 static int avg_mpcalls_dir;
106 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls_dir, CTLFLAG_RD, &avg_mpcalls_dir,
107     0, "Average number of MP direct callouts made per callout_process call. "
108     "Units = 1/1000");
109 #endif
110 
111 static int ncallout;
112 SYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &ncallout, 0,
113     "Number of entries in callwheel and size of timeout() preallocation");
114 
115 #ifdef	RSS
116 static int pin_default_swi = 1;
117 static int pin_pcpu_swi = 1;
118 #else
119 static int pin_default_swi = 0;
120 static int pin_pcpu_swi = 0;
121 #endif
122 
123 SYSCTL_INT(_kern, OID_AUTO, pin_default_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_default_swi,
124     0, "Pin the default (non-per-cpu) swi (shared with PCPU 0 swi)");
125 SYSCTL_INT(_kern, OID_AUTO, pin_pcpu_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_pcpu_swi,
126     0, "Pin the per-CPU swis (except PCPU 0, which is also default");
127 
128 /*
129  * TODO:
130  *	allocate more timeout table slots when table overflows.
131  */
132 u_int callwheelsize, callwheelmask;
133 
134 /*
135  * The callout cpu exec entities represent informations necessary for
136  * describing the state of callouts currently running on the CPU and the ones
137  * necessary for migrating callouts to the new callout cpu. In particular,
138  * the first entry of the array cc_exec_entity holds informations for callout
139  * running in SWI thread context, while the second one holds informations
140  * for callout running directly from hardware interrupt context.
141  * The cached informations are very important for deferring migration when
142  * the migrating callout is already running.
143  */
144 struct cc_exec {
145 	struct callout		*cc_curr;
146 	void			(*cc_drain)(void *);
147 	void			*cc_last_func;
148 	void			*cc_last_arg;
149 #ifdef SMP
150 	void			(*ce_migration_func)(void *);
151 	void			*ce_migration_arg;
152 	sbintime_t		ce_migration_time;
153 	sbintime_t		ce_migration_prec;
154 	int			ce_migration_cpu;
155 #endif
156 	bool			cc_cancel;
157 	bool			cc_waiting;
158 };
159 
160 /*
161  * There is one struct callout_cpu per cpu, holding all relevant
162  * state for the callout processing thread on the individual CPU.
163  */
164 struct callout_cpu {
165 	struct mtx_padalign	cc_lock;
166 	struct cc_exec 		cc_exec_entity[2];
167 	struct callout		*cc_next;
168 	struct callout		*cc_callout;
169 	struct callout_list	*cc_callwheel;
170 	struct callout_tailq	cc_expireq;
171 	struct callout_slist	cc_callfree;
172 	sbintime_t		cc_firstevent;
173 	sbintime_t		cc_lastscan;
174 	void			*cc_cookie;
175 	u_int			cc_bucket;
176 	u_int			cc_inited;
177 	char			cc_ktr_event_name[20];
178 };
179 
180 #define	callout_migrating(c)	((c)->c_iflags & CALLOUT_DFRMIGRATION)
181 
182 #define	cc_exec_curr(cc, dir)		cc->cc_exec_entity[dir].cc_curr
183 #define	cc_exec_last_func(cc, dir)	cc->cc_exec_entity[dir].cc_last_func
184 #define	cc_exec_last_arg(cc, dir)	cc->cc_exec_entity[dir].cc_last_arg
185 #define	cc_exec_drain(cc, dir)		cc->cc_exec_entity[dir].cc_drain
186 #define	cc_exec_next(cc)		cc->cc_next
187 #define	cc_exec_cancel(cc, dir)		cc->cc_exec_entity[dir].cc_cancel
188 #define	cc_exec_waiting(cc, dir)	cc->cc_exec_entity[dir].cc_waiting
189 #ifdef SMP
190 #define	cc_migration_func(cc, dir)	cc->cc_exec_entity[dir].ce_migration_func
191 #define	cc_migration_arg(cc, dir)	cc->cc_exec_entity[dir].ce_migration_arg
192 #define	cc_migration_cpu(cc, dir)	cc->cc_exec_entity[dir].ce_migration_cpu
193 #define	cc_migration_time(cc, dir)	cc->cc_exec_entity[dir].ce_migration_time
194 #define	cc_migration_prec(cc, dir)	cc->cc_exec_entity[dir].ce_migration_prec
195 
196 struct callout_cpu cc_cpu[MAXCPU];
197 #define	CPUBLOCK	MAXCPU
198 #define	CC_CPU(cpu)	(&cc_cpu[(cpu)])
199 #define	CC_SELF()	CC_CPU(PCPU_GET(cpuid))
200 #else
201 struct callout_cpu cc_cpu;
202 #define	CC_CPU(cpu)	&cc_cpu
203 #define	CC_SELF()	&cc_cpu
204 #endif
205 #define	CC_LOCK(cc)	mtx_lock_spin(&(cc)->cc_lock)
206 #define	CC_UNLOCK(cc)	mtx_unlock_spin(&(cc)->cc_lock)
207 #define	CC_LOCK_ASSERT(cc)	mtx_assert(&(cc)->cc_lock, MA_OWNED)
208 
209 static int timeout_cpu;
210 
211 static void	callout_cpu_init(struct callout_cpu *cc, int cpu);
212 static void	softclock_call_cc(struct callout *c, struct callout_cpu *cc,
213 #ifdef CALLOUT_PROFILING
214 		    int *mpcalls, int *lockcalls, int *gcalls,
215 #endif
216 		    int direct);
217 
218 static MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures");
219 
220 /**
221  * Locked by cc_lock:
222  *   cc_curr         - If a callout is in progress, it is cc_curr.
223  *                     If cc_curr is non-NULL, threads waiting in
224  *                     callout_drain() will be woken up as soon as the
225  *                     relevant callout completes.
226  *   cc_cancel       - Changing to 1 with both callout_lock and cc_lock held
227  *                     guarantees that the current callout will not run.
228  *                     The softclock() function sets this to 0 before it
229  *                     drops callout_lock to acquire c_lock, and it calls
230  *                     the handler only if curr_cancelled is still 0 after
231  *                     cc_lock is successfully acquired.
232  *   cc_waiting      - If a thread is waiting in callout_drain(), then
233  *                     callout_wait is nonzero.  Set only when
234  *                     cc_curr is non-NULL.
235  */
236 
237 /*
238  * Resets the execution entity tied to a specific callout cpu.
239  */
240 static void
241 cc_cce_cleanup(struct callout_cpu *cc, int direct)
242 {
243 
244 	cc_exec_curr(cc, direct) = NULL;
245 	cc_exec_cancel(cc, direct) = false;
246 	cc_exec_waiting(cc, direct) = false;
247 #ifdef SMP
248 	cc_migration_cpu(cc, direct) = CPUBLOCK;
249 	cc_migration_time(cc, direct) = 0;
250 	cc_migration_prec(cc, direct) = 0;
251 	cc_migration_func(cc, direct) = NULL;
252 	cc_migration_arg(cc, direct) = NULL;
253 #endif
254 }
255 
256 /*
257  * Checks if migration is requested by a specific callout cpu.
258  */
259 static int
260 cc_cce_migrating(struct callout_cpu *cc, int direct)
261 {
262 
263 #ifdef SMP
264 	return (cc_migration_cpu(cc, direct) != CPUBLOCK);
265 #else
266 	return (0);
267 #endif
268 }
269 
270 /*
271  * Kernel low level callwheel initialization
272  * called on the BSP during kernel startup.
273  */
274 static void
275 callout_callwheel_init(void *dummy)
276 {
277 	struct callout_cpu *cc;
278 
279 	/*
280 	 * Calculate the size of the callout wheel and the preallocated
281 	 * timeout() structures.
282 	 * XXX: Clip callout to result of previous function of maxusers
283 	 * maximum 384.  This is still huge, but acceptable.
284 	 */
285 	memset(CC_CPU(curcpu), 0, sizeof(cc_cpu));
286 	ncallout = imin(16 + maxproc + maxfiles, 18508);
287 	TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
288 
289 	/*
290 	 * Calculate callout wheel size, should be next power of two higher
291 	 * than 'ncallout'.
292 	 */
293 	callwheelsize = 1 << fls(ncallout);
294 	callwheelmask = callwheelsize - 1;
295 
296 	/*
297 	 * Fetch whether we're pinning the swi's or not.
298 	 */
299 	TUNABLE_INT_FETCH("kern.pin_default_swi", &pin_default_swi);
300 	TUNABLE_INT_FETCH("kern.pin_pcpu_swi", &pin_pcpu_swi);
301 
302 	/*
303 	 * Only BSP handles timeout(9) and receives a preallocation.
304 	 *
305 	 * XXX: Once all timeout(9) consumers are converted this can
306 	 * be removed.
307 	 */
308 	timeout_cpu = PCPU_GET(cpuid);
309 	cc = CC_CPU(timeout_cpu);
310 	cc->cc_callout = malloc(ncallout * sizeof(struct callout),
311 	    M_CALLOUT, M_WAITOK);
312 	callout_cpu_init(cc, timeout_cpu);
313 }
314 SYSINIT(callwheel_init, SI_SUB_CPU, SI_ORDER_ANY, callout_callwheel_init, NULL);
315 
316 /*
317  * Initialize the per-cpu callout structures.
318  */
319 static void
320 callout_cpu_init(struct callout_cpu *cc, int cpu)
321 {
322 	struct callout *c;
323 	int i;
324 
325 	mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
326 	SLIST_INIT(&cc->cc_callfree);
327 	cc->cc_inited = 1;
328 	cc->cc_callwheel = malloc(sizeof(struct callout_list) * callwheelsize,
329 	    M_CALLOUT, M_WAITOK);
330 	for (i = 0; i < callwheelsize; i++)
331 		LIST_INIT(&cc->cc_callwheel[i]);
332 	TAILQ_INIT(&cc->cc_expireq);
333 	cc->cc_firstevent = SBT_MAX;
334 	for (i = 0; i < 2; i++)
335 		cc_cce_cleanup(cc, i);
336 	snprintf(cc->cc_ktr_event_name, sizeof(cc->cc_ktr_event_name),
337 	    "callwheel cpu %d", cpu);
338 	if (cc->cc_callout == NULL)	/* Only BSP handles timeout(9) */
339 		return;
340 	for (i = 0; i < ncallout; i++) {
341 		c = &cc->cc_callout[i];
342 		callout_init(c, 0);
343 		c->c_iflags = CALLOUT_LOCAL_ALLOC;
344 		SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
345 	}
346 }
347 
348 #ifdef SMP
349 /*
350  * Switches the cpu tied to a specific callout.
351  * The function expects a locked incoming callout cpu and returns with
352  * locked outcoming callout cpu.
353  */
354 static struct callout_cpu *
355 callout_cpu_switch(struct callout *c, struct callout_cpu *cc, int new_cpu)
356 {
357 	struct callout_cpu *new_cc;
358 
359 	MPASS(c != NULL && cc != NULL);
360 	CC_LOCK_ASSERT(cc);
361 
362 	/*
363 	 * Avoid interrupts and preemption firing after the callout cpu
364 	 * is blocked in order to avoid deadlocks as the new thread
365 	 * may be willing to acquire the callout cpu lock.
366 	 */
367 	c->c_cpu = CPUBLOCK;
368 	spinlock_enter();
369 	CC_UNLOCK(cc);
370 	new_cc = CC_CPU(new_cpu);
371 	CC_LOCK(new_cc);
372 	spinlock_exit();
373 	c->c_cpu = new_cpu;
374 	return (new_cc);
375 }
376 #endif
377 
378 /*
379  * Start standard softclock thread.
380  */
381 static void
382 start_softclock(void *dummy)
383 {
384 	struct callout_cpu *cc;
385 	char name[MAXCOMLEN];
386 #ifdef SMP
387 	int cpu;
388 	struct intr_event *ie;
389 #endif
390 
391 	cc = CC_CPU(timeout_cpu);
392 	snprintf(name, sizeof(name), "clock (%d)", timeout_cpu);
393 	if (swi_add(&clk_intr_event, name, softclock, cc, SWI_CLOCK,
394 	    INTR_MPSAFE, &cc->cc_cookie))
395 		panic("died while creating standard software ithreads");
396 	if (pin_default_swi &&
397 	    (intr_event_bind(clk_intr_event, timeout_cpu) != 0)) {
398 		printf("%s: timeout clock couldn't be pinned to cpu %d\n",
399 		    __func__,
400 		    timeout_cpu);
401 	}
402 
403 #ifdef SMP
404 	CPU_FOREACH(cpu) {
405 		if (cpu == timeout_cpu)
406 			continue;
407 		cc = CC_CPU(cpu);
408 		cc->cc_callout = NULL;	/* Only BSP handles timeout(9). */
409 		callout_cpu_init(cc, cpu);
410 		snprintf(name, sizeof(name), "clock (%d)", cpu);
411 		ie = NULL;
412 		if (swi_add(&ie, name, softclock, cc, SWI_CLOCK,
413 		    INTR_MPSAFE, &cc->cc_cookie))
414 			panic("died while creating standard software ithreads");
415 		if (pin_pcpu_swi && (intr_event_bind(ie, cpu) != 0)) {
416 			printf("%s: per-cpu clock couldn't be pinned to "
417 			    "cpu %d\n",
418 			    __func__,
419 			    cpu);
420 		}
421 	}
422 #endif
423 }
424 SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL);
425 
426 #define	CC_HASH_SHIFT	8
427 
428 static inline u_int
429 callout_hash(sbintime_t sbt)
430 {
431 
432 	return (sbt >> (32 - CC_HASH_SHIFT));
433 }
434 
435 static inline u_int
436 callout_get_bucket(sbintime_t sbt)
437 {
438 
439 	return (callout_hash(sbt) & callwheelmask);
440 }
441 
442 void
443 callout_process(sbintime_t now)
444 {
445 	struct callout *tmp, *tmpn;
446 	struct callout_cpu *cc;
447 	struct callout_list *sc;
448 	sbintime_t first, last, max, tmp_max;
449 	uint32_t lookahead;
450 	u_int firstb, lastb, nowb;
451 #ifdef CALLOUT_PROFILING
452 	int depth_dir = 0, mpcalls_dir = 0, lockcalls_dir = 0;
453 #endif
454 
455 	cc = CC_SELF();
456 	mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
457 
458 	/* Compute the buckets of the last scan and present times. */
459 	firstb = callout_hash(cc->cc_lastscan);
460 	cc->cc_lastscan = now;
461 	nowb = callout_hash(now);
462 
463 	/* Compute the last bucket and minimum time of the bucket after it. */
464 	if (nowb == firstb)
465 		lookahead = (SBT_1S / 16);
466 	else if (nowb - firstb == 1)
467 		lookahead = (SBT_1S / 8);
468 	else
469 		lookahead = (SBT_1S / 2);
470 	first = last = now;
471 	first += (lookahead / 2);
472 	last += lookahead;
473 	last &= (0xffffffffffffffffLLU << (32 - CC_HASH_SHIFT));
474 	lastb = callout_hash(last) - 1;
475 	max = last;
476 
477 	/*
478 	 * Check if we wrapped around the entire wheel from the last scan.
479 	 * In case, we need to scan entirely the wheel for pending callouts.
480 	 */
481 	if (lastb - firstb >= callwheelsize) {
482 		lastb = firstb + callwheelsize - 1;
483 		if (nowb - firstb >= callwheelsize)
484 			nowb = lastb;
485 	}
486 
487 	/* Iterate callwheel from firstb to nowb and then up to lastb. */
488 	do {
489 		sc = &cc->cc_callwheel[firstb & callwheelmask];
490 		tmp = LIST_FIRST(sc);
491 		while (tmp != NULL) {
492 			/* Run the callout if present time within allowed. */
493 			if (tmp->c_time <= now) {
494 				/*
495 				 * Consumer told us the callout may be run
496 				 * directly from hardware interrupt context.
497 				 */
498 				if (tmp->c_iflags & CALLOUT_DIRECT) {
499 #ifdef CALLOUT_PROFILING
500 					++depth_dir;
501 #endif
502 					cc_exec_next(cc) =
503 					    LIST_NEXT(tmp, c_links.le);
504 					cc->cc_bucket = firstb & callwheelmask;
505 					LIST_REMOVE(tmp, c_links.le);
506 					softclock_call_cc(tmp, cc,
507 #ifdef CALLOUT_PROFILING
508 					    &mpcalls_dir, &lockcalls_dir, NULL,
509 #endif
510 					    1);
511 					tmp = cc_exec_next(cc);
512 					cc_exec_next(cc) = NULL;
513 				} else {
514 					tmpn = LIST_NEXT(tmp, c_links.le);
515 					LIST_REMOVE(tmp, c_links.le);
516 					TAILQ_INSERT_TAIL(&cc->cc_expireq,
517 					    tmp, c_links.tqe);
518 					tmp->c_iflags |= CALLOUT_PROCESSED;
519 					tmp = tmpn;
520 				}
521 				continue;
522 			}
523 			/* Skip events from distant future. */
524 			if (tmp->c_time >= max)
525 				goto next;
526 			/*
527 			 * Event minimal time is bigger than present maximal
528 			 * time, so it cannot be aggregated.
529 			 */
530 			if (tmp->c_time > last) {
531 				lastb = nowb;
532 				goto next;
533 			}
534 			/* Update first and last time, respecting this event. */
535 			if (tmp->c_time < first)
536 				first = tmp->c_time;
537 			tmp_max = tmp->c_time + tmp->c_precision;
538 			if (tmp_max < last)
539 				last = tmp_max;
540 next:
541 			tmp = LIST_NEXT(tmp, c_links.le);
542 		}
543 		/* Proceed with the next bucket. */
544 		firstb++;
545 		/*
546 		 * Stop if we looked after present time and found
547 		 * some event we can't execute at now.
548 		 * Stop if we looked far enough into the future.
549 		 */
550 	} while (((int)(firstb - lastb)) <= 0);
551 	cc->cc_firstevent = last;
552 #ifndef NO_EVENTTIMERS
553 	cpu_new_callout(curcpu, last, first);
554 #endif
555 #ifdef CALLOUT_PROFILING
556 	avg_depth_dir += (depth_dir * 1000 - avg_depth_dir) >> 8;
557 	avg_mpcalls_dir += (mpcalls_dir * 1000 - avg_mpcalls_dir) >> 8;
558 	avg_lockcalls_dir += (lockcalls_dir * 1000 - avg_lockcalls_dir) >> 8;
559 #endif
560 	mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
561 	/*
562 	 * swi_sched acquires the thread lock, so we don't want to call it
563 	 * with cc_lock held; incorrect locking order.
564 	 */
565 	if (!TAILQ_EMPTY(&cc->cc_expireq))
566 		swi_sched(cc->cc_cookie, 0);
567 }
568 
569 static struct callout_cpu *
570 callout_lock(struct callout *c)
571 {
572 	struct callout_cpu *cc;
573 	int cpu;
574 
575 	for (;;) {
576 		cpu = c->c_cpu;
577 #ifdef SMP
578 		if (cpu == CPUBLOCK) {
579 			while (c->c_cpu == CPUBLOCK)
580 				cpu_spinwait();
581 			continue;
582 		}
583 #endif
584 		cc = CC_CPU(cpu);
585 		CC_LOCK(cc);
586 		if (cpu == c->c_cpu)
587 			break;
588 		CC_UNLOCK(cc);
589 	}
590 	return (cc);
591 }
592 
593 static void
594 callout_cc_add(struct callout *c, struct callout_cpu *cc,
595     sbintime_t sbt, sbintime_t precision, void (*func)(void *),
596     void *arg, int cpu, int flags)
597 {
598 	int bucket;
599 
600 	CC_LOCK_ASSERT(cc);
601 	if (sbt < cc->cc_lastscan)
602 		sbt = cc->cc_lastscan;
603 	c->c_arg = arg;
604 	c->c_iflags |= CALLOUT_PENDING;
605 	c->c_iflags &= ~CALLOUT_PROCESSED;
606 	c->c_flags |= CALLOUT_ACTIVE;
607 	if (flags & C_DIRECT_EXEC)
608 		c->c_iflags |= CALLOUT_DIRECT;
609 	c->c_func = func;
610 	c->c_time = sbt;
611 	c->c_precision = precision;
612 	bucket = callout_get_bucket(c->c_time);
613 	CTR3(KTR_CALLOUT, "precision set for %p: %d.%08x",
614 	    c, (int)(c->c_precision >> 32),
615 	    (u_int)(c->c_precision & 0xffffffff));
616 	LIST_INSERT_HEAD(&cc->cc_callwheel[bucket], c, c_links.le);
617 	if (cc->cc_bucket == bucket)
618 		cc_exec_next(cc) = c;
619 #ifndef NO_EVENTTIMERS
620 	/*
621 	 * Inform the eventtimers(4) subsystem there's a new callout
622 	 * that has been inserted, but only if really required.
623 	 */
624 	if (SBT_MAX - c->c_time < c->c_precision)
625 		c->c_precision = SBT_MAX - c->c_time;
626 	sbt = c->c_time + c->c_precision;
627 	if (sbt < cc->cc_firstevent) {
628 		cc->cc_firstevent = sbt;
629 		cpu_new_callout(cpu, sbt, c->c_time);
630 	}
631 #endif
632 }
633 
634 static void
635 callout_cc_del(struct callout *c, struct callout_cpu *cc)
636 {
637 
638 	if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) == 0)
639 		return;
640 	c->c_func = NULL;
641 	SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
642 }
643 
644 static void
645 softclock_call_cc(struct callout *c, struct callout_cpu *cc,
646 #ifdef CALLOUT_PROFILING
647     int *mpcalls, int *lockcalls, int *gcalls,
648 #endif
649     int direct)
650 {
651 	struct rm_priotracker tracker;
652 	void (*c_func)(void *);
653 	void *c_arg;
654 	struct lock_class *class;
655 	struct lock_object *c_lock;
656 	uintptr_t lock_status;
657 	int c_iflags;
658 #ifdef SMP
659 	struct callout_cpu *new_cc;
660 	void (*new_func)(void *);
661 	void *new_arg;
662 	int flags, new_cpu;
663 	sbintime_t new_prec, new_time;
664 #endif
665 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
666 	sbintime_t sbt1, sbt2;
667 	struct timespec ts2;
668 	static sbintime_t maxdt = 2 * SBT_1MS;	/* 2 msec */
669 	static timeout_t *lastfunc;
670 #endif
671 
672 	KASSERT((c->c_iflags & CALLOUT_PENDING) == CALLOUT_PENDING,
673 	    ("softclock_call_cc: pend %p %x", c, c->c_iflags));
674 	KASSERT((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE,
675 	    ("softclock_call_cc: act %p %x", c, c->c_flags));
676 	class = (c->c_lock != NULL) ? LOCK_CLASS(c->c_lock) : NULL;
677 	lock_status = 0;
678 	if (c->c_flags & CALLOUT_SHAREDLOCK) {
679 		if (class == &lock_class_rm)
680 			lock_status = (uintptr_t)&tracker;
681 		else
682 			lock_status = 1;
683 	}
684 	c_lock = c->c_lock;
685 	c_func = c->c_func;
686 	c_arg = c->c_arg;
687 	c_iflags = c->c_iflags;
688 	if (c->c_iflags & CALLOUT_LOCAL_ALLOC)
689 		c->c_iflags = CALLOUT_LOCAL_ALLOC;
690 	else
691 		c->c_iflags &= ~CALLOUT_PENDING;
692 
693 	cc_exec_curr(cc, direct) = c;
694 	cc_exec_last_func(cc, direct) = c_func;
695 	cc_exec_last_arg(cc, direct) = c_arg;
696 	cc_exec_cancel(cc, direct) = false;
697 	cc_exec_drain(cc, direct) = NULL;
698 	CC_UNLOCK(cc);
699 	if (c_lock != NULL) {
700 		class->lc_lock(c_lock, lock_status);
701 		/*
702 		 * The callout may have been cancelled
703 		 * while we switched locks.
704 		 */
705 		if (cc_exec_cancel(cc, direct)) {
706 			class->lc_unlock(c_lock);
707 			goto skip;
708 		}
709 		/* The callout cannot be stopped now. */
710 		cc_exec_cancel(cc, direct) = true;
711 		if (c_lock == &Giant.lock_object) {
712 #ifdef CALLOUT_PROFILING
713 			(*gcalls)++;
714 #endif
715 			CTR3(KTR_CALLOUT, "callout giant %p func %p arg %p",
716 			    c, c_func, c_arg);
717 		} else {
718 #ifdef CALLOUT_PROFILING
719 			(*lockcalls)++;
720 #endif
721 			CTR3(KTR_CALLOUT, "callout lock %p func %p arg %p",
722 			    c, c_func, c_arg);
723 		}
724 	} else {
725 #ifdef CALLOUT_PROFILING
726 		(*mpcalls)++;
727 #endif
728 		CTR3(KTR_CALLOUT, "callout %p func %p arg %p",
729 		    c, c_func, c_arg);
730 	}
731 	KTR_STATE3(KTR_SCHED, "callout", cc->cc_ktr_event_name, "running",
732 	    "func:%p", c_func, "arg:%p", c_arg, "direct:%d", direct);
733 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
734 	sbt1 = sbinuptime();
735 #endif
736 	THREAD_NO_SLEEPING();
737 	SDT_PROBE1(callout_execute, , , callout__start, c);
738 	c_func(c_arg);
739 	SDT_PROBE1(callout_execute, , , callout__end, c);
740 	THREAD_SLEEPING_OK();
741 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
742 	sbt2 = sbinuptime();
743 	sbt2 -= sbt1;
744 	if (sbt2 > maxdt) {
745 		if (lastfunc != c_func || sbt2 > maxdt * 2) {
746 			ts2 = sbttots(sbt2);
747 			printf(
748 		"Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
749 			    c_func, c_arg, (intmax_t)ts2.tv_sec, ts2.tv_nsec);
750 		}
751 		maxdt = sbt2;
752 		lastfunc = c_func;
753 	}
754 #endif
755 	KTR_STATE0(KTR_SCHED, "callout", cc->cc_ktr_event_name, "idle");
756 	CTR1(KTR_CALLOUT, "callout %p finished", c);
757 	if ((c_iflags & CALLOUT_RETURNUNLOCKED) == 0)
758 		class->lc_unlock(c_lock);
759 skip:
760 	CC_LOCK(cc);
761 	KASSERT(cc_exec_curr(cc, direct) == c, ("mishandled cc_curr"));
762 	cc_exec_curr(cc, direct) = NULL;
763 	if (cc_exec_drain(cc, direct)) {
764 		void (*drain)(void *);
765 
766 		drain = cc_exec_drain(cc, direct);
767 		cc_exec_drain(cc, direct) = NULL;
768 		CC_UNLOCK(cc);
769 		drain(c_arg);
770 		CC_LOCK(cc);
771 	}
772 	if (cc_exec_waiting(cc, direct)) {
773 		/*
774 		 * There is someone waiting for the
775 		 * callout to complete.
776 		 * If the callout was scheduled for
777 		 * migration just cancel it.
778 		 */
779 		if (cc_cce_migrating(cc, direct)) {
780 			cc_cce_cleanup(cc, direct);
781 
782 			/*
783 			 * It should be assert here that the callout is not
784 			 * destroyed but that is not easy.
785 			 */
786 			c->c_iflags &= ~CALLOUT_DFRMIGRATION;
787 		}
788 		cc_exec_waiting(cc, direct) = false;
789 		CC_UNLOCK(cc);
790 		wakeup(&cc_exec_waiting(cc, direct));
791 		CC_LOCK(cc);
792 	} else if (cc_cce_migrating(cc, direct)) {
793 		KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0,
794 		    ("Migrating legacy callout %p", c));
795 #ifdef SMP
796 		/*
797 		 * If the callout was scheduled for
798 		 * migration just perform it now.
799 		 */
800 		new_cpu = cc_migration_cpu(cc, direct);
801 		new_time = cc_migration_time(cc, direct);
802 		new_prec = cc_migration_prec(cc, direct);
803 		new_func = cc_migration_func(cc, direct);
804 		new_arg = cc_migration_arg(cc, direct);
805 		cc_cce_cleanup(cc, direct);
806 
807 		/*
808 		 * It should be assert here that the callout is not destroyed
809 		 * but that is not easy.
810 		 *
811 		 * As first thing, handle deferred callout stops.
812 		 */
813 		if (!callout_migrating(c)) {
814 			CTR3(KTR_CALLOUT,
815 			     "deferred cancelled %p func %p arg %p",
816 			     c, new_func, new_arg);
817 			callout_cc_del(c, cc);
818 			return;
819 		}
820 		c->c_iflags &= ~CALLOUT_DFRMIGRATION;
821 
822 		new_cc = callout_cpu_switch(c, cc, new_cpu);
823 		flags = (direct) ? C_DIRECT_EXEC : 0;
824 		callout_cc_add(c, new_cc, new_time, new_prec, new_func,
825 		    new_arg, new_cpu, flags);
826 		CC_UNLOCK(new_cc);
827 		CC_LOCK(cc);
828 #else
829 		panic("migration should not happen");
830 #endif
831 	}
832 	/*
833 	 * If the current callout is locally allocated (from
834 	 * timeout(9)) then put it on the freelist.
835 	 *
836 	 * Note: we need to check the cached copy of c_iflags because
837 	 * if it was not local, then it's not safe to deref the
838 	 * callout pointer.
839 	 */
840 	KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0 ||
841 	    c->c_iflags == CALLOUT_LOCAL_ALLOC,
842 	    ("corrupted callout"));
843 	if (c_iflags & CALLOUT_LOCAL_ALLOC)
844 		callout_cc_del(c, cc);
845 }
846 
847 /*
848  * The callout mechanism is based on the work of Adam M. Costello and
849  * George Varghese, published in a technical report entitled "Redesigning
850  * the BSD Callout and Timer Facilities" and modified slightly for inclusion
851  * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
852  * used in this implementation was published by G. Varghese and T. Lauck in
853  * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
854  * the Efficient Implementation of a Timer Facility" in the Proceedings of
855  * the 11th ACM Annual Symposium on Operating Systems Principles,
856  * Austin, Texas Nov 1987.
857  */
858 
859 /*
860  * Software (low priority) clock interrupt.
861  * Run periodic events from timeout queue.
862  */
863 void
864 softclock(void *arg)
865 {
866 	struct callout_cpu *cc;
867 	struct callout *c;
868 #ifdef CALLOUT_PROFILING
869 	int depth = 0, gcalls = 0, lockcalls = 0, mpcalls = 0;
870 #endif
871 
872 	cc = (struct callout_cpu *)arg;
873 	CC_LOCK(cc);
874 	while ((c = TAILQ_FIRST(&cc->cc_expireq)) != NULL) {
875 		TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
876 		softclock_call_cc(c, cc,
877 #ifdef CALLOUT_PROFILING
878 		    &mpcalls, &lockcalls, &gcalls,
879 #endif
880 		    0);
881 #ifdef CALLOUT_PROFILING
882 		++depth;
883 #endif
884 	}
885 #ifdef CALLOUT_PROFILING
886 	avg_depth += (depth * 1000 - avg_depth) >> 8;
887 	avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
888 	avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8;
889 	avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
890 #endif
891 	CC_UNLOCK(cc);
892 }
893 
894 /*
895  * timeout --
896  *	Execute a function after a specified length of time.
897  *
898  * untimeout --
899  *	Cancel previous timeout function call.
900  *
901  * callout_handle_init --
902  *	Initialize a handle so that using it with untimeout is benign.
903  *
904  *	See AT&T BCI Driver Reference Manual for specification.  This
905  *	implementation differs from that one in that although an
906  *	identification value is returned from timeout, the original
907  *	arguments to timeout as well as the identifier are used to
908  *	identify entries for untimeout.
909  */
910 struct callout_handle
911 timeout(timeout_t *ftn, void *arg, int to_ticks)
912 {
913 	struct callout_cpu *cc;
914 	struct callout *new;
915 	struct callout_handle handle;
916 
917 	cc = CC_CPU(timeout_cpu);
918 	CC_LOCK(cc);
919 	/* Fill in the next free callout structure. */
920 	new = SLIST_FIRST(&cc->cc_callfree);
921 	if (new == NULL)
922 		/* XXX Attempt to malloc first */
923 		panic("timeout table full");
924 	SLIST_REMOVE_HEAD(&cc->cc_callfree, c_links.sle);
925 	callout_reset(new, to_ticks, ftn, arg);
926 	handle.callout = new;
927 	CC_UNLOCK(cc);
928 
929 	return (handle);
930 }
931 
932 void
933 untimeout(timeout_t *ftn, void *arg, struct callout_handle handle)
934 {
935 	struct callout_cpu *cc;
936 
937 	/*
938 	 * Check for a handle that was initialized
939 	 * by callout_handle_init, but never used
940 	 * for a real timeout.
941 	 */
942 	if (handle.callout == NULL)
943 		return;
944 
945 	cc = callout_lock(handle.callout);
946 	if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
947 		callout_stop(handle.callout);
948 	CC_UNLOCK(cc);
949 }
950 
951 void
952 callout_handle_init(struct callout_handle *handle)
953 {
954 	handle->callout = NULL;
955 }
956 
957 void
958 callout_when(sbintime_t sbt, sbintime_t precision, int flags,
959     sbintime_t *res, sbintime_t *prec_res)
960 {
961 	sbintime_t to_sbt, to_pr;
962 
963 	if ((flags & (C_ABSOLUTE | C_PRECALC)) != 0) {
964 		*res = sbt;
965 		*prec_res = precision;
966 		return;
967 	}
968 	if ((flags & C_HARDCLOCK) != 0 && sbt < tick_sbt)
969 		sbt = tick_sbt;
970 	if ((flags & C_HARDCLOCK) != 0 ||
971 #ifdef NO_EVENTTIMERS
972 	    sbt >= sbt_timethreshold) {
973 		to_sbt = getsbinuptime();
974 
975 		/* Add safety belt for the case of hz > 1000. */
976 		to_sbt += tc_tick_sbt - tick_sbt;
977 #else
978 	    sbt >= sbt_tickthreshold) {
979 		/*
980 		 * Obtain the time of the last hardclock() call on
981 		 * this CPU directly from the kern_clocksource.c.
982 		 * This value is per-CPU, but it is equal for all
983 		 * active ones.
984 		 */
985 #ifdef __LP64__
986 		to_sbt = DPCPU_GET(hardclocktime);
987 #else
988 		spinlock_enter();
989 		to_sbt = DPCPU_GET(hardclocktime);
990 		spinlock_exit();
991 #endif
992 #endif
993 		if (cold && to_sbt == 0)
994 			to_sbt = sbinuptime();
995 		if ((flags & C_HARDCLOCK) == 0)
996 			to_sbt += tick_sbt;
997 	} else
998 		to_sbt = sbinuptime();
999 	if (SBT_MAX - to_sbt < sbt)
1000 		to_sbt = SBT_MAX;
1001 	else
1002 		to_sbt += sbt;
1003 	*res = to_sbt;
1004 	to_pr = ((C_PRELGET(flags) < 0) ? sbt >> tc_precexp :
1005 	    sbt >> C_PRELGET(flags));
1006 	*prec_res = to_pr > precision ? to_pr : precision;
1007 }
1008 
1009 /*
1010  * New interface; clients allocate their own callout structures.
1011  *
1012  * callout_reset() - establish or change a timeout
1013  * callout_stop() - disestablish a timeout
1014  * callout_init() - initialize a callout structure so that it can
1015  *	safely be passed to callout_reset() and callout_stop()
1016  *
1017  * <sys/callout.h> defines three convenience macros:
1018  *
1019  * callout_active() - returns truth if callout has not been stopped,
1020  *	drained, or deactivated since the last time the callout was
1021  *	reset.
1022  * callout_pending() - returns truth if callout is still waiting for timeout
1023  * callout_deactivate() - marks the callout as having been serviced
1024  */
1025 int
1026 callout_reset_sbt_on(struct callout *c, sbintime_t sbt, sbintime_t prec,
1027     void (*ftn)(void *), void *arg, int cpu, int flags)
1028 {
1029 	sbintime_t to_sbt, precision;
1030 	struct callout_cpu *cc;
1031 	int cancelled, direct;
1032 	int ignore_cpu=0;
1033 
1034 	cancelled = 0;
1035 	if (cpu == -1) {
1036 		ignore_cpu = 1;
1037 	} else if ((cpu >= MAXCPU) ||
1038 		   ((CC_CPU(cpu))->cc_inited == 0)) {
1039 		/* Invalid CPU spec */
1040 		panic("Invalid CPU in callout %d", cpu);
1041 	}
1042 	callout_when(sbt, prec, flags, &to_sbt, &precision);
1043 
1044 	/*
1045 	 * This flag used to be added by callout_cc_add, but the
1046 	 * first time you call this we could end up with the
1047 	 * wrong direct flag if we don't do it before we add.
1048 	 */
1049 	if (flags & C_DIRECT_EXEC) {
1050 		direct = 1;
1051 	} else {
1052 		direct = 0;
1053 	}
1054 	KASSERT(!direct || c->c_lock == NULL,
1055 	    ("%s: direct callout %p has lock", __func__, c));
1056 	cc = callout_lock(c);
1057 	/*
1058 	 * Don't allow migration of pre-allocated callouts lest they
1059 	 * become unbalanced or handle the case where the user does
1060 	 * not care.
1061 	 */
1062 	if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) ||
1063 	    ignore_cpu) {
1064 		cpu = c->c_cpu;
1065 	}
1066 
1067 	if (cc_exec_curr(cc, direct) == c) {
1068 		/*
1069 		 * We're being asked to reschedule a callout which is
1070 		 * currently in progress.  If there is a lock then we
1071 		 * can cancel the callout if it has not really started.
1072 		 */
1073 		if (c->c_lock != NULL && !cc_exec_cancel(cc, direct))
1074 			cancelled = cc_exec_cancel(cc, direct) = true;
1075 		if (cc_exec_waiting(cc, direct) || cc_exec_drain(cc, direct)) {
1076 			/*
1077 			 * Someone has called callout_drain to kill this
1078 			 * callout.  Don't reschedule.
1079 			 */
1080 			CTR4(KTR_CALLOUT, "%s %p func %p arg %p",
1081 			    cancelled ? "cancelled" : "failed to cancel",
1082 			    c, c->c_func, c->c_arg);
1083 			CC_UNLOCK(cc);
1084 			return (cancelled);
1085 		}
1086 #ifdef SMP
1087 		if (callout_migrating(c)) {
1088 			/*
1089 			 * This only occurs when a second callout_reset_sbt_on
1090 			 * is made after a previous one moved it into
1091 			 * deferred migration (below). Note we do *not* change
1092 			 * the prev_cpu even though the previous target may
1093 			 * be different.
1094 			 */
1095 			cc_migration_cpu(cc, direct) = cpu;
1096 			cc_migration_time(cc, direct) = to_sbt;
1097 			cc_migration_prec(cc, direct) = precision;
1098 			cc_migration_func(cc, direct) = ftn;
1099 			cc_migration_arg(cc, direct) = arg;
1100 			cancelled = 1;
1101 			CC_UNLOCK(cc);
1102 			return (cancelled);
1103 		}
1104 #endif
1105 	}
1106 	if (c->c_iflags & CALLOUT_PENDING) {
1107 		if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1108 			if (cc_exec_next(cc) == c)
1109 				cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1110 			LIST_REMOVE(c, c_links.le);
1111 		} else {
1112 			TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1113 		}
1114 		cancelled = 1;
1115 		c->c_iflags &= ~ CALLOUT_PENDING;
1116 		c->c_flags &= ~ CALLOUT_ACTIVE;
1117 	}
1118 
1119 #ifdef SMP
1120 	/*
1121 	 * If the callout must migrate try to perform it immediately.
1122 	 * If the callout is currently running, just defer the migration
1123 	 * to a more appropriate moment.
1124 	 */
1125 	if (c->c_cpu != cpu) {
1126 		if (cc_exec_curr(cc, direct) == c) {
1127 			/*
1128 			 * Pending will have been removed since we are
1129 			 * actually executing the callout on another
1130 			 * CPU. That callout should be waiting on the
1131 			 * lock the caller holds. If we set both
1132 			 * active/and/pending after we return and the
1133 			 * lock on the executing callout proceeds, it
1134 			 * will then see pending is true and return.
1135 			 * At the return from the actual callout execution
1136 			 * the migration will occur in softclock_call_cc
1137 			 * and this new callout will be placed on the
1138 			 * new CPU via a call to callout_cpu_switch() which
1139 			 * will get the lock on the right CPU followed
1140 			 * by a call callout_cc_add() which will add it there.
1141 			 * (see above in softclock_call_cc()).
1142 			 */
1143 			cc_migration_cpu(cc, direct) = cpu;
1144 			cc_migration_time(cc, direct) = to_sbt;
1145 			cc_migration_prec(cc, direct) = precision;
1146 			cc_migration_func(cc, direct) = ftn;
1147 			cc_migration_arg(cc, direct) = arg;
1148 			c->c_iflags |= (CALLOUT_DFRMIGRATION | CALLOUT_PENDING);
1149 			c->c_flags |= CALLOUT_ACTIVE;
1150 			CTR6(KTR_CALLOUT,
1151 		    "migration of %p func %p arg %p in %d.%08x to %u deferred",
1152 			    c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1153 			    (u_int)(to_sbt & 0xffffffff), cpu);
1154 			CC_UNLOCK(cc);
1155 			return (cancelled);
1156 		}
1157 		cc = callout_cpu_switch(c, cc, cpu);
1158 	}
1159 #endif
1160 
1161 	callout_cc_add(c, cc, to_sbt, precision, ftn, arg, cpu, flags);
1162 	CTR6(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d.%08x",
1163 	    cancelled ? "re" : "", c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1164 	    (u_int)(to_sbt & 0xffffffff));
1165 	CC_UNLOCK(cc);
1166 
1167 	return (cancelled);
1168 }
1169 
1170 /*
1171  * Common idioms that can be optimized in the future.
1172  */
1173 int
1174 callout_schedule_on(struct callout *c, int to_ticks, int cpu)
1175 {
1176 	return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu);
1177 }
1178 
1179 int
1180 callout_schedule(struct callout *c, int to_ticks)
1181 {
1182 	return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu);
1183 }
1184 
1185 int
1186 _callout_stop_safe(struct callout *c, int flags, void (*drain)(void *))
1187 {
1188 	struct callout_cpu *cc, *old_cc;
1189 	struct lock_class *class;
1190 	int direct, sq_locked, use_lock;
1191 	int cancelled, not_on_a_list;
1192 
1193 	if ((flags & CS_DRAIN) != 0)
1194 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, c->c_lock,
1195 		    "calling %s", __func__);
1196 
1197 	/*
1198 	 * Some old subsystems don't hold Giant while running a callout_stop(),
1199 	 * so just discard this check for the moment.
1200 	 */
1201 	if ((flags & CS_DRAIN) == 0 && c->c_lock != NULL) {
1202 		if (c->c_lock == &Giant.lock_object)
1203 			use_lock = mtx_owned(&Giant);
1204 		else {
1205 			use_lock = 1;
1206 			class = LOCK_CLASS(c->c_lock);
1207 			class->lc_assert(c->c_lock, LA_XLOCKED);
1208 		}
1209 	} else
1210 		use_lock = 0;
1211 	if (c->c_iflags & CALLOUT_DIRECT) {
1212 		direct = 1;
1213 	} else {
1214 		direct = 0;
1215 	}
1216 	sq_locked = 0;
1217 	old_cc = NULL;
1218 again:
1219 	cc = callout_lock(c);
1220 
1221 	if ((c->c_iflags & (CALLOUT_DFRMIGRATION | CALLOUT_PENDING)) ==
1222 	    (CALLOUT_DFRMIGRATION | CALLOUT_PENDING) &&
1223 	    ((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE)) {
1224 		/*
1225 		 * Special case where this slipped in while we
1226 		 * were migrating *as* the callout is about to
1227 		 * execute. The caller probably holds the lock
1228 		 * the callout wants.
1229 		 *
1230 		 * Get rid of the migration first. Then set
1231 		 * the flag that tells this code *not* to
1232 		 * try to remove it from any lists (its not
1233 		 * on one yet). When the callout wheel runs,
1234 		 * it will ignore this callout.
1235 		 */
1236 		c->c_iflags &= ~CALLOUT_PENDING;
1237 		c->c_flags &= ~CALLOUT_ACTIVE;
1238 		not_on_a_list = 1;
1239 	} else {
1240 		not_on_a_list = 0;
1241 	}
1242 
1243 	/*
1244 	 * If the callout was migrating while the callout cpu lock was
1245 	 * dropped,  just drop the sleepqueue lock and check the states
1246 	 * again.
1247 	 */
1248 	if (sq_locked != 0 && cc != old_cc) {
1249 #ifdef SMP
1250 		CC_UNLOCK(cc);
1251 		sleepq_release(&cc_exec_waiting(old_cc, direct));
1252 		sq_locked = 0;
1253 		old_cc = NULL;
1254 		goto again;
1255 #else
1256 		panic("migration should not happen");
1257 #endif
1258 	}
1259 
1260 	/*
1261 	 * If the callout is running, try to stop it or drain it.
1262 	 */
1263 	if (cc_exec_curr(cc, direct) == c) {
1264 		/*
1265 		 * Succeed we to stop it or not, we must clear the
1266 		 * active flag - this is what API users expect.  If we're
1267 		 * draining and the callout is currently executing, first wait
1268 		 * until it finishes.
1269 		 */
1270 		if ((flags & CS_DRAIN) == 0)
1271 			c->c_flags &= ~CALLOUT_ACTIVE;
1272 
1273 		if ((flags & CS_DRAIN) != 0) {
1274 			/*
1275 			 * The current callout is running (or just
1276 			 * about to run) and blocking is allowed, so
1277 			 * just wait for the current invocation to
1278 			 * finish.
1279 			 */
1280 			while (cc_exec_curr(cc, direct) == c) {
1281 				/*
1282 				 * Use direct calls to sleepqueue interface
1283 				 * instead of cv/msleep in order to avoid
1284 				 * a LOR between cc_lock and sleepqueue
1285 				 * chain spinlocks.  This piece of code
1286 				 * emulates a msleep_spin() call actually.
1287 				 *
1288 				 * If we already have the sleepqueue chain
1289 				 * locked, then we can safely block.  If we
1290 				 * don't already have it locked, however,
1291 				 * we have to drop the cc_lock to lock
1292 				 * it.  This opens several races, so we
1293 				 * restart at the beginning once we have
1294 				 * both locks.  If nothing has changed, then
1295 				 * we will end up back here with sq_locked
1296 				 * set.
1297 				 */
1298 				if (!sq_locked) {
1299 					CC_UNLOCK(cc);
1300 					sleepq_lock(
1301 					    &cc_exec_waiting(cc, direct));
1302 					sq_locked = 1;
1303 					old_cc = cc;
1304 					goto again;
1305 				}
1306 
1307 				/*
1308 				 * Migration could be cancelled here, but
1309 				 * as long as it is still not sure when it
1310 				 * will be packed up, just let softclock()
1311 				 * take care of it.
1312 				 */
1313 				cc_exec_waiting(cc, direct) = true;
1314 				DROP_GIANT();
1315 				CC_UNLOCK(cc);
1316 				sleepq_add(
1317 				    &cc_exec_waiting(cc, direct),
1318 				    &cc->cc_lock.lock_object, "codrain",
1319 				    SLEEPQ_SLEEP, 0);
1320 				sleepq_wait(
1321 				    &cc_exec_waiting(cc, direct),
1322 					     0);
1323 				sq_locked = 0;
1324 				old_cc = NULL;
1325 
1326 				/* Reacquire locks previously released. */
1327 				PICKUP_GIANT();
1328 				CC_LOCK(cc);
1329 			}
1330 			c->c_flags &= ~CALLOUT_ACTIVE;
1331 		} else if (use_lock &&
1332 			   !cc_exec_cancel(cc, direct) && (drain == NULL)) {
1333 
1334 			/*
1335 			 * The current callout is waiting for its
1336 			 * lock which we hold.  Cancel the callout
1337 			 * and return.  After our caller drops the
1338 			 * lock, the callout will be skipped in
1339 			 * softclock(). This *only* works with a
1340 			 * callout_stop() *not* callout_drain() or
1341 			 * callout_async_drain().
1342 			 */
1343 			cc_exec_cancel(cc, direct) = true;
1344 			CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1345 			    c, c->c_func, c->c_arg);
1346 			KASSERT(!cc_cce_migrating(cc, direct),
1347 			    ("callout wrongly scheduled for migration"));
1348 			if (callout_migrating(c)) {
1349 				c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1350 #ifdef SMP
1351 				cc_migration_cpu(cc, direct) = CPUBLOCK;
1352 				cc_migration_time(cc, direct) = 0;
1353 				cc_migration_prec(cc, direct) = 0;
1354 				cc_migration_func(cc, direct) = NULL;
1355 				cc_migration_arg(cc, direct) = NULL;
1356 #endif
1357 			}
1358 			CC_UNLOCK(cc);
1359 			KASSERT(!sq_locked, ("sleepqueue chain locked"));
1360 			return (1);
1361 		} else if (callout_migrating(c)) {
1362 			/*
1363 			 * The callout is currently being serviced
1364 			 * and the "next" callout is scheduled at
1365 			 * its completion with a migration. We remove
1366 			 * the migration flag so it *won't* get rescheduled,
1367 			 * but we can't stop the one thats running so
1368 			 * we return 0.
1369 			 */
1370 			c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1371 #ifdef SMP
1372 			/*
1373 			 * We can't call cc_cce_cleanup here since
1374 			 * if we do it will remove .ce_curr and
1375 			 * its still running. This will prevent a
1376 			 * reschedule of the callout when the
1377 			 * execution completes.
1378 			 */
1379 			cc_migration_cpu(cc, direct) = CPUBLOCK;
1380 			cc_migration_time(cc, direct) = 0;
1381 			cc_migration_prec(cc, direct) = 0;
1382 			cc_migration_func(cc, direct) = NULL;
1383 			cc_migration_arg(cc, direct) = NULL;
1384 #endif
1385 			CTR3(KTR_CALLOUT, "postponing stop %p func %p arg %p",
1386 			    c, c->c_func, c->c_arg);
1387  			if (drain) {
1388 				cc_exec_drain(cc, direct) = drain;
1389 			}
1390 			CC_UNLOCK(cc);
1391 			return ((flags & CS_EXECUTING) != 0);
1392 		}
1393 		CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1394 		    c, c->c_func, c->c_arg);
1395 		if (drain) {
1396 			cc_exec_drain(cc, direct) = drain;
1397 		}
1398 		KASSERT(!sq_locked, ("sleepqueue chain still locked"));
1399 		cancelled = ((flags & CS_EXECUTING) != 0);
1400 	} else
1401 		cancelled = 1;
1402 
1403 	if (sq_locked)
1404 		sleepq_release(&cc_exec_waiting(cc, direct));
1405 
1406 	if ((c->c_iflags & CALLOUT_PENDING) == 0) {
1407 		CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1408 		    c, c->c_func, c->c_arg);
1409 		/*
1410 		 * For not scheduled and not executing callout return
1411 		 * negative value.
1412 		 */
1413 		if (cc_exec_curr(cc, direct) != c)
1414 			cancelled = -1;
1415 		CC_UNLOCK(cc);
1416 		return (cancelled);
1417 	}
1418 
1419 	c->c_iflags &= ~CALLOUT_PENDING;
1420 	c->c_flags &= ~CALLOUT_ACTIVE;
1421 
1422 	CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1423 	    c, c->c_func, c->c_arg);
1424 	if (not_on_a_list == 0) {
1425 		if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1426 			if (cc_exec_next(cc) == c)
1427 				cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1428 			LIST_REMOVE(c, c_links.le);
1429 		} else {
1430 			TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1431 		}
1432 	}
1433 	callout_cc_del(c, cc);
1434 	CC_UNLOCK(cc);
1435 	return (cancelled);
1436 }
1437 
1438 void
1439 callout_init(struct callout *c, int mpsafe)
1440 {
1441 	bzero(c, sizeof *c);
1442 	if (mpsafe) {
1443 		c->c_lock = NULL;
1444 		c->c_iflags = CALLOUT_RETURNUNLOCKED;
1445 	} else {
1446 		c->c_lock = &Giant.lock_object;
1447 		c->c_iflags = 0;
1448 	}
1449 	c->c_cpu = timeout_cpu;
1450 }
1451 
1452 void
1453 _callout_init_lock(struct callout *c, struct lock_object *lock, int flags)
1454 {
1455 	bzero(c, sizeof *c);
1456 	c->c_lock = lock;
1457 	KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0,
1458 	    ("callout_init_lock: bad flags %d", flags));
1459 	KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
1460 	    ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock"));
1461 	KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags &
1462 	    (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class",
1463 	    __func__));
1464 	c->c_iflags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK);
1465 	c->c_cpu = timeout_cpu;
1466 }
1467 
1468 #ifdef APM_FIXUP_CALLTODO
1469 /*
1470  * Adjust the kernel calltodo timeout list.  This routine is used after
1471  * an APM resume to recalculate the calltodo timer list values with the
1472  * number of hz's we have been sleeping.  The next hardclock() will detect
1473  * that there are fired timers and run softclock() to execute them.
1474  *
1475  * Please note, I have not done an exhaustive analysis of what code this
1476  * might break.  I am motivated to have my select()'s and alarm()'s that
1477  * have expired during suspend firing upon resume so that the applications
1478  * which set the timer can do the maintanence the timer was for as close
1479  * as possible to the originally intended time.  Testing this code for a
1480  * week showed that resuming from a suspend resulted in 22 to 25 timers
1481  * firing, which seemed independent on whether the suspend was 2 hours or
1482  * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
1483  */
1484 void
1485 adjust_timeout_calltodo(struct timeval *time_change)
1486 {
1487 	struct callout *p;
1488 	unsigned long delta_ticks;
1489 
1490 	/*
1491 	 * How many ticks were we asleep?
1492 	 * (stolen from tvtohz()).
1493 	 */
1494 
1495 	/* Don't do anything */
1496 	if (time_change->tv_sec < 0)
1497 		return;
1498 	else if (time_change->tv_sec <= LONG_MAX / 1000000)
1499 		delta_ticks = howmany(time_change->tv_sec * 1000000 +
1500 		    time_change->tv_usec, tick) + 1;
1501 	else if (time_change->tv_sec <= LONG_MAX / hz)
1502 		delta_ticks = time_change->tv_sec * hz +
1503 		    howmany(time_change->tv_usec, tick) + 1;
1504 	else
1505 		delta_ticks = LONG_MAX;
1506 
1507 	if (delta_ticks > INT_MAX)
1508 		delta_ticks = INT_MAX;
1509 
1510 	/*
1511 	 * Now rip through the timer calltodo list looking for timers
1512 	 * to expire.
1513 	 */
1514 
1515 	/* don't collide with softclock() */
1516 	CC_LOCK(cc);
1517 	for (p = calltodo.c_next; p != NULL; p = p->c_next) {
1518 		p->c_time -= delta_ticks;
1519 
1520 		/* Break if the timer had more time on it than delta_ticks */
1521 		if (p->c_time > 0)
1522 			break;
1523 
1524 		/* take back the ticks the timer didn't use (p->c_time <= 0) */
1525 		delta_ticks = -p->c_time;
1526 	}
1527 	CC_UNLOCK(cc);
1528 
1529 	return;
1530 }
1531 #endif /* APM_FIXUP_CALLTODO */
1532 
1533 static int
1534 flssbt(sbintime_t sbt)
1535 {
1536 
1537 	sbt += (uint64_t)sbt >> 1;
1538 	if (sizeof(long) >= sizeof(sbintime_t))
1539 		return (flsl(sbt));
1540 	if (sbt >= SBT_1S)
1541 		return (flsl(((uint64_t)sbt) >> 32) + 32);
1542 	return (flsl(sbt));
1543 }
1544 
1545 /*
1546  * Dump immediate statistic snapshot of the scheduled callouts.
1547  */
1548 static int
1549 sysctl_kern_callout_stat(SYSCTL_HANDLER_ARGS)
1550 {
1551 	struct callout *tmp;
1552 	struct callout_cpu *cc;
1553 	struct callout_list *sc;
1554 	sbintime_t maxpr, maxt, medpr, medt, now, spr, st, t;
1555 	int ct[64], cpr[64], ccpbk[32];
1556 	int error, val, i, count, tcum, pcum, maxc, c, medc;
1557 #ifdef SMP
1558 	int cpu;
1559 #endif
1560 
1561 	val = 0;
1562 	error = sysctl_handle_int(oidp, &val, 0, req);
1563 	if (error != 0 || req->newptr == NULL)
1564 		return (error);
1565 	count = maxc = 0;
1566 	st = spr = maxt = maxpr = 0;
1567 	bzero(ccpbk, sizeof(ccpbk));
1568 	bzero(ct, sizeof(ct));
1569 	bzero(cpr, sizeof(cpr));
1570 	now = sbinuptime();
1571 #ifdef SMP
1572 	CPU_FOREACH(cpu) {
1573 		cc = CC_CPU(cpu);
1574 #else
1575 		cc = CC_CPU(timeout_cpu);
1576 #endif
1577 		CC_LOCK(cc);
1578 		for (i = 0; i < callwheelsize; i++) {
1579 			sc = &cc->cc_callwheel[i];
1580 			c = 0;
1581 			LIST_FOREACH(tmp, sc, c_links.le) {
1582 				c++;
1583 				t = tmp->c_time - now;
1584 				if (t < 0)
1585 					t = 0;
1586 				st += t / SBT_1US;
1587 				spr += tmp->c_precision / SBT_1US;
1588 				if (t > maxt)
1589 					maxt = t;
1590 				if (tmp->c_precision > maxpr)
1591 					maxpr = tmp->c_precision;
1592 				ct[flssbt(t)]++;
1593 				cpr[flssbt(tmp->c_precision)]++;
1594 			}
1595 			if (c > maxc)
1596 				maxc = c;
1597 			ccpbk[fls(c + c / 2)]++;
1598 			count += c;
1599 		}
1600 		CC_UNLOCK(cc);
1601 #ifdef SMP
1602 	}
1603 #endif
1604 
1605 	for (i = 0, tcum = 0; i < 64 && tcum < count / 2; i++)
1606 		tcum += ct[i];
1607 	medt = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1608 	for (i = 0, pcum = 0; i < 64 && pcum < count / 2; i++)
1609 		pcum += cpr[i];
1610 	medpr = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1611 	for (i = 0, c = 0; i < 32 && c < count / 2; i++)
1612 		c += ccpbk[i];
1613 	medc = (i >= 2) ? (1 << (i - 2)) : 0;
1614 
1615 	printf("Scheduled callouts statistic snapshot:\n");
1616 	printf("  Callouts: %6d  Buckets: %6d*%-3d  Bucket size: 0.%06ds\n",
1617 	    count, callwheelsize, mp_ncpus, 1000000 >> CC_HASH_SHIFT);
1618 	printf("  C/Bk: med %5d         avg %6d.%06jd  max %6d\n",
1619 	    medc,
1620 	    count / callwheelsize / mp_ncpus,
1621 	    (uint64_t)count * 1000000 / callwheelsize / mp_ncpus % 1000000,
1622 	    maxc);
1623 	printf("  Time: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1624 	    medt / SBT_1S, (medt & 0xffffffff) * 1000000 >> 32,
1625 	    (st / count) / 1000000, (st / count) % 1000000,
1626 	    maxt / SBT_1S, (maxt & 0xffffffff) * 1000000 >> 32);
1627 	printf("  Prec: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1628 	    medpr / SBT_1S, (medpr & 0xffffffff) * 1000000 >> 32,
1629 	    (spr / count) / 1000000, (spr / count) % 1000000,
1630 	    maxpr / SBT_1S, (maxpr & 0xffffffff) * 1000000 >> 32);
1631 	printf("  Distribution:       \tbuckets\t   time\t   tcum\t"
1632 	    "   prec\t   pcum\n");
1633 	for (i = 0, tcum = pcum = 0; i < 64; i++) {
1634 		if (ct[i] == 0 && cpr[i] == 0)
1635 			continue;
1636 		t = (i != 0) ? (((sbintime_t)1) << (i - 1)) : 0;
1637 		tcum += ct[i];
1638 		pcum += cpr[i];
1639 		printf("  %10jd.%06jds\t 2**%d\t%7d\t%7d\t%7d\t%7d\n",
1640 		    t / SBT_1S, (t & 0xffffffff) * 1000000 >> 32,
1641 		    i - 1 - (32 - CC_HASH_SHIFT),
1642 		    ct[i], tcum, cpr[i], pcum);
1643 	}
1644 	return (error);
1645 }
1646 SYSCTL_PROC(_kern, OID_AUTO, callout_stat,
1647     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1648     0, 0, sysctl_kern_callout_stat, "I",
1649     "Dump immediate statistic snapshot of the scheduled callouts");
1650 
1651 #ifdef DDB
1652 static void
1653 _show_callout(struct callout *c)
1654 {
1655 
1656 	db_printf("callout %p\n", c);
1657 #define	C_DB_PRINTF(f, e)	db_printf("   %s = " f "\n", #e, c->e);
1658 	db_printf("   &c_links = %p\n", &(c->c_links));
1659 	C_DB_PRINTF("%" PRId64,	c_time);
1660 	C_DB_PRINTF("%" PRId64,	c_precision);
1661 	C_DB_PRINTF("%p",	c_arg);
1662 	C_DB_PRINTF("%p",	c_func);
1663 	C_DB_PRINTF("%p",	c_lock);
1664 	C_DB_PRINTF("%#x",	c_flags);
1665 	C_DB_PRINTF("%#x",	c_iflags);
1666 	C_DB_PRINTF("%d",	c_cpu);
1667 #undef	C_DB_PRINTF
1668 }
1669 
1670 DB_SHOW_COMMAND(callout, db_show_callout)
1671 {
1672 
1673 	if (!have_addr) {
1674 		db_printf("usage: show callout <struct callout *>\n");
1675 		return;
1676 	}
1677 
1678 	_show_callout((struct callout *)addr);
1679 }
1680 
1681 static void
1682 _show_last_callout(int cpu, int direct, const char *dirstr)
1683 {
1684 	struct callout_cpu *cc;
1685 	void *func, *arg;
1686 
1687 	cc = CC_CPU(cpu);
1688 	func = cc_exec_last_func(cc, direct);
1689 	arg = cc_exec_last_arg(cc, direct);
1690 	db_printf("cpu %d last%s callout function: %p ", cpu, dirstr, func);
1691 	db_printsym((db_expr_t)func, DB_STGY_ANY);
1692 	db_printf("\ncpu %d last%s callout argument: %p\n", cpu, dirstr, arg);
1693 }
1694 
1695 DB_SHOW_COMMAND(callout_last, db_show_callout_last)
1696 {
1697 	int cpu, last;
1698 
1699 	if (have_addr) {
1700 		if (addr < 0 || addr > mp_maxid || CPU_ABSENT(addr)) {
1701 			db_printf("no such cpu: %d\n", (int)addr);
1702 			return;
1703 		}
1704 		cpu = last = addr;
1705 	} else {
1706 		cpu = 0;
1707 		last = mp_maxid;
1708 	}
1709 
1710 	while (cpu <= last) {
1711 		if (!CPU_ABSENT(cpu)) {
1712 			_show_last_callout(cpu, 0, "");
1713 			_show_last_callout(cpu, 1, " direct");
1714 		}
1715 		cpu++;
1716 	}
1717 }
1718 #endif /* DDB */
1719