xref: /freebsd/sys/kern/kern_timeout.c (revision 6b806d21d144c25f4fad714e1c0cf780f5e27d7e)
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 <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/callout.h>
43 #include <sys/condvar.h>
44 #include <sys/kernel.h>
45 #include <sys/ktr.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 
50 static int avg_depth;
51 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0,
52     "Average number of items examined per softclock call. Units = 1/1000");
53 static int avg_gcalls;
54 SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0,
55     "Average number of Giant callouts made per softclock call. Units = 1/1000");
56 static int avg_mtxcalls;
57 SYSCTL_INT(_debug, OID_AUTO, to_avg_mtxcalls, CTLFLAG_RD, &avg_mtxcalls, 0,
58     "Average number of mtx callouts made per softclock call. Units = 1/1000");
59 static int avg_mpcalls;
60 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0,
61     "Average number of MP callouts made per softclock call. Units = 1/1000");
62 /*
63  * TODO:
64  *	allocate more timeout table slots when table overflows.
65  */
66 
67 /* Exported to machdep.c and/or kern_clock.c.  */
68 struct callout *callout;
69 struct callout_list callfree;
70 int callwheelsize, callwheelbits, callwheelmask;
71 struct callout_tailq *callwheel;
72 int softticks;			/* Like ticks, but for softclock(). */
73 struct mtx callout_lock;
74 #ifdef DIAGNOSTIC
75 struct mtx dont_sleep_in_callout;
76 #endif
77 
78 static struct callout *nextsoftcheck;	/* Next callout to be checked. */
79 
80 /**
81  * Locked by callout_lock:
82  *   curr_callout    - If a callout is in progress, it is curr_callout.
83  *                     If curr_callout is non-NULL, threads waiting on
84  *                     callout_wait will be woken up as soon as the
85  *                     relevant callout completes.
86  *   curr_cancelled  - Changing to 1 with both callout_lock and c_mtx held
87  *                     guarantees that the current callout will not run.
88  *                     The softclock() function sets this to 0 before it
89  *                     drops callout_lock to acquire c_mtx, and it calls
90  *                     the handler only if curr_cancelled still 0 when
91  *                     c_mtx is successfully acquired.
92  *   wakeup_ctr      - Incremented every time a thread wants to wait
93  *                     for a callout to complete.  Modified only when
94  *                     curr_callout is non-NULL.
95  *   wakeup_needed   - If a thread is waiting on callout_wait, then
96  *                     wakeup_needed is nonzero.  Increased only when
97  *                     cutt_callout is non-NULL.
98  */
99 static struct callout *curr_callout;
100 static int curr_cancelled;
101 static int wakeup_ctr;
102 static int wakeup_needed;
103 
104 /**
105  * Locked by callout_wait_lock:
106  *   callout_wait    - If wakeup_needed is set, callout_wait will be
107  *                     triggered after the current callout finishes.
108  *   wakeup_done_ctr - Set to the current value of wakeup_ctr after
109  *                     callout_wait is triggered.
110  */
111 static struct mtx callout_wait_lock;
112 static struct cv callout_wait;
113 static int wakeup_done_ctr;
114 
115 /*
116  * kern_timeout_callwheel_alloc() - kernel low level callwheel initialization
117  *
118  *	This code is called very early in the kernel initialization sequence,
119  *	and may be called more then once.
120  */
121 caddr_t
122 kern_timeout_callwheel_alloc(caddr_t v)
123 {
124 	/*
125 	 * Calculate callout wheel size
126 	 */
127 	for (callwheelsize = 1, callwheelbits = 0;
128 	     callwheelsize < ncallout;
129 	     callwheelsize <<= 1, ++callwheelbits)
130 		;
131 	callwheelmask = callwheelsize - 1;
132 
133 	callout = (struct callout *)v;
134 	v = (caddr_t)(callout + ncallout);
135 	callwheel = (struct callout_tailq *)v;
136 	v = (caddr_t)(callwheel + callwheelsize);
137 	return(v);
138 }
139 
140 /*
141  * kern_timeout_callwheel_init() - initialize previously reserved callwheel
142  *				   space.
143  *
144  *	This code is called just once, after the space reserved for the
145  *	callout wheel has been finalized.
146  */
147 void
148 kern_timeout_callwheel_init(void)
149 {
150 	int i;
151 
152 	SLIST_INIT(&callfree);
153 	for (i = 0; i < ncallout; i++) {
154 		callout_init(&callout[i], 0);
155 		callout[i].c_flags = CALLOUT_LOCAL_ALLOC;
156 		SLIST_INSERT_HEAD(&callfree, &callout[i], c_links.sle);
157 	}
158 	for (i = 0; i < callwheelsize; i++) {
159 		TAILQ_INIT(&callwheel[i]);
160 	}
161 	mtx_init(&callout_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
162 #ifdef DIAGNOSTIC
163 	mtx_init(&dont_sleep_in_callout, "dont_sleep_in_callout", NULL, MTX_DEF);
164 #endif
165 	mtx_init(&callout_wait_lock, "callout_wait_lock", NULL, MTX_DEF);
166 	cv_init(&callout_wait, "callout_wait");
167 }
168 
169 /*
170  * The callout mechanism is based on the work of Adam M. Costello and
171  * George Varghese, published in a technical report entitled "Redesigning
172  * the BSD Callout and Timer Facilities" and modified slightly for inclusion
173  * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
174  * used in this implementation was published by G. Varghese and T. Lauck in
175  * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
176  * the Efficient Implementation of a Timer Facility" in the Proceedings of
177  * the 11th ACM Annual Symposium on Operating Systems Principles,
178  * Austin, Texas Nov 1987.
179  */
180 
181 /*
182  * Software (low priority) clock interrupt.
183  * Run periodic events from timeout queue.
184  */
185 void
186 softclock(void *dummy)
187 {
188 	struct callout *c;
189 	struct callout_tailq *bucket;
190 	int curticks;
191 	int steps;	/* #steps since we last allowed interrupts */
192 	int depth;
193 	int mpcalls;
194 	int mtxcalls;
195 	int gcalls;
196 	int wakeup_cookie;
197 #ifdef DIAGNOSTIC
198 	struct bintime bt1, bt2;
199 	struct timespec ts2;
200 	static uint64_t maxdt = 36893488147419102LL;	/* 2 msec */
201 	static timeout_t *lastfunc;
202 #endif
203 
204 #ifndef MAX_SOFTCLOCK_STEPS
205 #define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
206 #endif /* MAX_SOFTCLOCK_STEPS */
207 
208 	mpcalls = 0;
209 	mtxcalls = 0;
210 	gcalls = 0;
211 	depth = 0;
212 	steps = 0;
213 	mtx_lock_spin(&callout_lock);
214 	while (softticks != ticks) {
215 		softticks++;
216 		/*
217 		 * softticks may be modified by hard clock, so cache
218 		 * it while we work on a given bucket.
219 		 */
220 		curticks = softticks;
221 		bucket = &callwheel[curticks & callwheelmask];
222 		c = TAILQ_FIRST(bucket);
223 		while (c) {
224 			depth++;
225 			if (c->c_time != curticks) {
226 				c = TAILQ_NEXT(c, c_links.tqe);
227 				++steps;
228 				if (steps >= MAX_SOFTCLOCK_STEPS) {
229 					nextsoftcheck = c;
230 					/* Give interrupts a chance. */
231 					mtx_unlock_spin(&callout_lock);
232 					;	/* nothing */
233 					mtx_lock_spin(&callout_lock);
234 					c = nextsoftcheck;
235 					steps = 0;
236 				}
237 			} else {
238 				void (*c_func)(void *);
239 				void *c_arg;
240 				struct mtx *c_mtx;
241 				int c_flags;
242 
243 				nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
244 				TAILQ_REMOVE(bucket, c, c_links.tqe);
245 				c_func = c->c_func;
246 				c_arg = c->c_arg;
247 				c_mtx = c->c_mtx;
248 				c_flags = c->c_flags;
249 				if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
250 					c->c_func = NULL;
251 					c->c_flags = CALLOUT_LOCAL_ALLOC;
252 					SLIST_INSERT_HEAD(&callfree, c,
253 							  c_links.sle);
254 				} else {
255 					c->c_flags =
256 					    (c->c_flags & ~CALLOUT_PENDING);
257 				}
258 				curr_callout = c;
259 				curr_cancelled = 0;
260 				mtx_unlock_spin(&callout_lock);
261 				if (c_mtx != NULL) {
262 					mtx_lock(c_mtx);
263 					/*
264 					 * The callout may have been cancelled
265 					 * while we switched locks.
266 					 */
267 					if (curr_cancelled) {
268 						mtx_unlock(c_mtx);
269 						mtx_lock_spin(&callout_lock);
270 						goto done_locked;
271 					}
272 					/* The callout cannot be stopped now. */
273 					curr_cancelled = 1;
274 
275 					if (c_mtx == &Giant) {
276 						gcalls++;
277 						CTR1(KTR_CALLOUT, "callout %p",
278 						    c_func);
279 					} else {
280 						mtxcalls++;
281 						CTR1(KTR_CALLOUT,
282 						    "callout mtx %p",
283 						    c_func);
284 					}
285 				} else {
286 					mpcalls++;
287 					CTR1(KTR_CALLOUT, "callout mpsafe %p",
288 					    c_func);
289 				}
290 #ifdef DIAGNOSTIC
291 				binuptime(&bt1);
292 				mtx_lock(&dont_sleep_in_callout);
293 #endif
294 				c_func(c_arg);
295 #ifdef DIAGNOSTIC
296 				mtx_unlock(&dont_sleep_in_callout);
297 				binuptime(&bt2);
298 				bintime_sub(&bt2, &bt1);
299 				if (bt2.frac > maxdt) {
300 					if (lastfunc != c_func ||
301 					    bt2.frac > maxdt * 2) {
302 						bintime2timespec(&bt2, &ts2);
303 						printf(
304 			"Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
305 						    c_func, c_arg,
306 						    (intmax_t)ts2.tv_sec,
307 						    ts2.tv_nsec);
308 					}
309 					maxdt = bt2.frac;
310 					lastfunc = c_func;
311 				}
312 #endif
313 				if ((c_flags & CALLOUT_RETURNUNLOCKED) == 0)
314 					mtx_unlock(c_mtx);
315 				mtx_lock_spin(&callout_lock);
316 done_locked:
317 				curr_callout = NULL;
318 				if (wakeup_needed) {
319 					/*
320 					 * There might be someone waiting
321 					 * for the callout to complete.
322 					 */
323 					wakeup_cookie = wakeup_ctr;
324 					mtx_unlock_spin(&callout_lock);
325 					mtx_lock(&callout_wait_lock);
326 					cv_broadcast(&callout_wait);
327 					wakeup_done_ctr = wakeup_cookie;
328 					mtx_unlock(&callout_wait_lock);
329 					mtx_lock_spin(&callout_lock);
330 					wakeup_needed = 0;
331 				}
332 				steps = 0;
333 				c = nextsoftcheck;
334 			}
335 		}
336 	}
337 	avg_depth += (depth * 1000 - avg_depth) >> 8;
338 	avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
339 	avg_mtxcalls += (mtxcalls * 1000 - avg_mtxcalls) >> 8;
340 	avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
341 	nextsoftcheck = NULL;
342 	mtx_unlock_spin(&callout_lock);
343 }
344 
345 /*
346  * timeout --
347  *	Execute a function after a specified length of time.
348  *
349  * untimeout --
350  *	Cancel previous timeout function call.
351  *
352  * callout_handle_init --
353  *	Initialize a handle so that using it with untimeout is benign.
354  *
355  *	See AT&T BCI Driver Reference Manual for specification.  This
356  *	implementation differs from that one in that although an
357  *	identification value is returned from timeout, the original
358  *	arguments to timeout as well as the identifier are used to
359  *	identify entries for untimeout.
360  */
361 struct callout_handle
362 timeout(ftn, arg, to_ticks)
363 	timeout_t *ftn;
364 	void *arg;
365 	int to_ticks;
366 {
367 	struct callout *new;
368 	struct callout_handle handle;
369 
370 	mtx_lock_spin(&callout_lock);
371 
372 	/* Fill in the next free callout structure. */
373 	new = SLIST_FIRST(&callfree);
374 	if (new == NULL)
375 		/* XXX Attempt to malloc first */
376 		panic("timeout table full");
377 	SLIST_REMOVE_HEAD(&callfree, c_links.sle);
378 
379 	callout_reset(new, to_ticks, ftn, arg);
380 
381 	handle.callout = new;
382 	mtx_unlock_spin(&callout_lock);
383 	return (handle);
384 }
385 
386 void
387 untimeout(ftn, arg, handle)
388 	timeout_t *ftn;
389 	void *arg;
390 	struct callout_handle handle;
391 {
392 
393 	/*
394 	 * Check for a handle that was initialized
395 	 * by callout_handle_init, but never used
396 	 * for a real timeout.
397 	 */
398 	if (handle.callout == NULL)
399 		return;
400 
401 	mtx_lock_spin(&callout_lock);
402 	if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
403 		callout_stop(handle.callout);
404 	mtx_unlock_spin(&callout_lock);
405 }
406 
407 void
408 callout_handle_init(struct callout_handle *handle)
409 {
410 	handle->callout = NULL;
411 }
412 
413 /*
414  * New interface; clients allocate their own callout structures.
415  *
416  * callout_reset() - establish or change a timeout
417  * callout_stop() - disestablish a timeout
418  * callout_init() - initialize a callout structure so that it can
419  *	safely be passed to callout_reset() and callout_stop()
420  *
421  * <sys/callout.h> defines three convenience macros:
422  *
423  * callout_active() - returns truth if callout has not been stopped,
424  *	drained, or deactivated since the last time the callout was
425  *	reset.
426  * callout_pending() - returns truth if callout is still waiting for timeout
427  * callout_deactivate() - marks the callout as having been serviced
428  */
429 void
430 callout_reset(c, to_ticks, ftn, arg)
431 	struct	callout *c;
432 	int	to_ticks;
433 	void	(*ftn)(void *);
434 	void	*arg;
435 {
436 
437 #ifdef notyet /* Some callers of timeout() do not hold Giant. */
438 	if (c->c_mtx != NULL)
439 		mtx_assert(c->c_mtx, MA_OWNED);
440 #endif
441 
442 	mtx_lock_spin(&callout_lock);
443 	if (c == curr_callout) {
444 		/*
445 		 * We're being asked to reschedule a callout which is
446 		 * currently in progress.  If there is a mutex then we
447 		 * can cancel the callout if it has not really started.
448 		 */
449 		if (c->c_mtx != NULL && !curr_cancelled)
450 			curr_cancelled = 1;
451 		if (wakeup_needed) {
452 			/*
453 			 * Someone has called callout_drain to kill this
454 			 * callout.  Don't reschedule.
455 			 */
456 			mtx_unlock_spin(&callout_lock);
457 			return;
458 		}
459 	}
460 	if (c->c_flags & CALLOUT_PENDING) {
461 		if (nextsoftcheck == c) {
462 			nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
463 		}
464 		TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c,
465 		    c_links.tqe);
466 
467 		/*
468 		 * Part of the normal "stop a pending callout" process
469 		 * is to clear the CALLOUT_ACTIVE and CALLOUT_PENDING
470 		 * flags.  We're not going to bother doing that here,
471 		 * because we're going to be setting those flags ten lines
472 		 * after this point, and we're holding callout_lock
473 		 * between now and then.
474 		 */
475 	}
476 
477 	/*
478 	 * We could unlock callout_lock here and lock it again before the
479 	 * TAILQ_INSERT_TAIL, but there's no point since doing this setup
480 	 * doesn't take much time.
481 	 */
482 	if (to_ticks <= 0)
483 		to_ticks = 1;
484 
485 	c->c_arg = arg;
486 	c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
487 	c->c_func = ftn;
488 	c->c_time = ticks + to_ticks;
489 	TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask],
490 			  c, c_links.tqe);
491 	mtx_unlock_spin(&callout_lock);
492 }
493 
494 int
495 _callout_stop_safe(c, safe)
496 	struct	callout *c;
497 	int	safe;
498 {
499 	int use_mtx, wakeup_cookie;
500 
501 	if (!safe && c->c_mtx != NULL) {
502 #ifdef notyet /* Some callers do not hold Giant for Giant-locked callouts. */
503 		mtx_assert(c->c_mtx, MA_OWNED);
504 		use_mtx = 1;
505 #else
506 		use_mtx = mtx_owned(c->c_mtx);
507 #endif
508 	} else {
509 		use_mtx = 0;
510 	}
511 
512 	mtx_lock_spin(&callout_lock);
513 	/*
514 	 * Don't attempt to delete a callout that's not on the queue.
515 	 */
516 	if (!(c->c_flags & CALLOUT_PENDING)) {
517 		c->c_flags &= ~CALLOUT_ACTIVE;
518 		if (c != curr_callout) {
519 			mtx_unlock_spin(&callout_lock);
520 			return (0);
521 		}
522 		if (safe) {
523 			/* We need to wait until the callout is finished. */
524 			wakeup_needed = 1;
525 			wakeup_cookie = wakeup_ctr++;
526 			mtx_unlock_spin(&callout_lock);
527 			mtx_lock(&callout_wait_lock);
528 
529 			/*
530 			 * Check to make sure that softclock() didn't
531 			 * do the wakeup in between our dropping
532 			 * callout_lock and picking up callout_wait_lock
533 			 */
534 			if (wakeup_cookie - wakeup_done_ctr > 0)
535 				cv_wait(&callout_wait, &callout_wait_lock);
536 
537 			mtx_unlock(&callout_wait_lock);
538 		} else if (use_mtx && !curr_cancelled) {
539 			/* We can stop the callout before it runs. */
540 			curr_cancelled = 1;
541 			mtx_unlock_spin(&callout_lock);
542 			return (1);
543 		} else
544 			mtx_unlock_spin(&callout_lock);
545 		return (0);
546 	}
547 	c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
548 
549 	if (nextsoftcheck == c) {
550 		nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
551 	}
552 	TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c, c_links.tqe);
553 
554 	if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
555 		c->c_func = NULL;
556 		SLIST_INSERT_HEAD(&callfree, c, c_links.sle);
557 	}
558 	mtx_unlock_spin(&callout_lock);
559 	return (1);
560 }
561 
562 void
563 callout_init(c, mpsafe)
564 	struct	callout *c;
565 	int mpsafe;
566 {
567 	bzero(c, sizeof *c);
568 	if (mpsafe) {
569 		c->c_mtx = NULL;
570 		c->c_flags = CALLOUT_RETURNUNLOCKED;
571 	} else {
572 		c->c_mtx = &Giant;
573 		c->c_flags = 0;
574 	}
575 }
576 
577 void
578 callout_init_mtx(c, mtx, flags)
579 	struct	callout *c;
580 	struct	mtx *mtx;
581 	int flags;
582 {
583 	bzero(c, sizeof *c);
584 	c->c_mtx = mtx;
585 	KASSERT((flags & ~CALLOUT_RETURNUNLOCKED) == 0,
586 	    ("callout_init_mtx: bad flags %d", flags));
587 	/* CALLOUT_RETURNUNLOCKED makes no sense without a mutex. */
588 	KASSERT(mtx != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
589 	    ("callout_init_mtx: CALLOUT_RETURNUNLOCKED with no mutex"));
590 	c->c_flags = flags & CALLOUT_RETURNUNLOCKED;
591 }
592 
593 #ifdef APM_FIXUP_CALLTODO
594 /*
595  * Adjust the kernel calltodo timeout list.  This routine is used after
596  * an APM resume to recalculate the calltodo timer list values with the
597  * number of hz's we have been sleeping.  The next hardclock() will detect
598  * that there are fired timers and run softclock() to execute them.
599  *
600  * Please note, I have not done an exhaustive analysis of what code this
601  * might break.  I am motivated to have my select()'s and alarm()'s that
602  * have expired during suspend firing upon resume so that the applications
603  * which set the timer can do the maintanence the timer was for as close
604  * as possible to the originally intended time.  Testing this code for a
605  * week showed that resuming from a suspend resulted in 22 to 25 timers
606  * firing, which seemed independant on whether the suspend was 2 hours or
607  * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
608  */
609 void
610 adjust_timeout_calltodo(time_change)
611     struct timeval *time_change;
612 {
613 	register struct callout *p;
614 	unsigned long delta_ticks;
615 
616 	/*
617 	 * How many ticks were we asleep?
618 	 * (stolen from tvtohz()).
619 	 */
620 
621 	/* Don't do anything */
622 	if (time_change->tv_sec < 0)
623 		return;
624 	else if (time_change->tv_sec <= LONG_MAX / 1000000)
625 		delta_ticks = (time_change->tv_sec * 1000000 +
626 			       time_change->tv_usec + (tick - 1)) / tick + 1;
627 	else if (time_change->tv_sec <= LONG_MAX / hz)
628 		delta_ticks = time_change->tv_sec * hz +
629 			      (time_change->tv_usec + (tick - 1)) / tick + 1;
630 	else
631 		delta_ticks = LONG_MAX;
632 
633 	if (delta_ticks > INT_MAX)
634 		delta_ticks = INT_MAX;
635 
636 	/*
637 	 * Now rip through the timer calltodo list looking for timers
638 	 * to expire.
639 	 */
640 
641 	/* don't collide with softclock() */
642 	mtx_lock_spin(&callout_lock);
643 	for (p = calltodo.c_next; p != NULL; p = p->c_next) {
644 		p->c_time -= delta_ticks;
645 
646 		/* Break if the timer had more time on it than delta_ticks */
647 		if (p->c_time > 0)
648 			break;
649 
650 		/* take back the ticks the timer didn't use (p->c_time <= 0) */
651 		delta_ticks = -p->c_time;
652 	}
653 	mtx_unlock_spin(&callout_lock);
654 
655 	return;
656 }
657 #endif /* APM_FIXUP_CALLTODO */
658