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