xref: /freebsd/sys/kern/kern_timeout.c (revision c74c7b73a005e689b922dfcfe5b94804669b595b)
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/bus.h>
43 #include <sys/callout.h>
44 #include <sys/condvar.h>
45 #include <sys/interrupt.h>
46 #include <sys/kernel.h>
47 #include <sys/ktr.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
51 #include <sys/proc.h>
52 #include <sys/sleepqueue.h>
53 #include <sys/sysctl.h>
54 #include <sys/smp.h>
55 
56 static int avg_depth;
57 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0,
58     "Average number of items examined per softclock call. Units = 1/1000");
59 static int avg_gcalls;
60 SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0,
61     "Average number of Giant callouts made per softclock call. Units = 1/1000");
62 static int avg_lockcalls;
63 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls, CTLFLAG_RD, &avg_lockcalls, 0,
64     "Average number of lock callouts made per softclock call. Units = 1/1000");
65 static int avg_mpcalls;
66 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0,
67     "Average number of MP callouts made per softclock call. Units = 1/1000");
68 /*
69  * TODO:
70  *	allocate more timeout table slots when table overflows.
71  */
72 int callwheelsize, callwheelbits, callwheelmask;
73 
74 struct callout_cpu {
75 	struct mtx		cc_lock;
76 	struct callout		*cc_callout;
77 	struct callout_tailq	*cc_callwheel;
78 	struct callout_list	cc_callfree;
79 	struct callout		*cc_next;
80 	struct callout		*cc_curr;
81 	void			*cc_cookie;
82 	int 			cc_softticks;
83 	int			cc_cancel;
84 	int			cc_waiting;
85 };
86 
87 #ifdef SMP
88 struct callout_cpu cc_cpu[MAXCPU];
89 #define	CC_CPU(cpu)	(&cc_cpu[(cpu)])
90 #define	CC_SELF()	CC_CPU(PCPU_GET(cpuid))
91 #else
92 struct callout_cpu cc_cpu;
93 #define	CC_CPU(cpu)	&cc_cpu
94 #define	CC_SELF()	&cc_cpu
95 #endif
96 #define	CC_LOCK(cc)	mtx_lock_spin(&(cc)->cc_lock)
97 #define	CC_UNLOCK(cc)	mtx_unlock_spin(&(cc)->cc_lock)
98 
99 static int timeout_cpu;
100 
101 MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures");
102 
103 /**
104  * Locked by cc_lock:
105  *   cc_curr         - If a callout is in progress, it is curr_callout.
106  *                     If curr_callout is non-NULL, threads waiting in
107  *                     callout_drain() will be woken up as soon as the
108  *                     relevant callout completes.
109  *   cc_cancel       - Changing to 1 with both callout_lock and c_lock held
110  *                     guarantees that the current callout will not run.
111  *                     The softclock() function sets this to 0 before it
112  *                     drops callout_lock to acquire c_lock, and it calls
113  *                     the handler only if curr_cancelled is still 0 after
114  *                     c_lock is successfully acquired.
115  *   cc_waiting      - If a thread is waiting in callout_drain(), then
116  *                     callout_wait is nonzero.  Set only when
117  *                     curr_callout is non-NULL.
118  */
119 
120 /*
121  * kern_timeout_callwheel_alloc() - kernel low level callwheel initialization
122  *
123  *	This code is called very early in the kernel initialization sequence,
124  *	and may be called more then once.
125  */
126 caddr_t
127 kern_timeout_callwheel_alloc(caddr_t v)
128 {
129 	struct callout_cpu *cc;
130 
131 	timeout_cpu = PCPU_GET(cpuid);
132 	cc = CC_CPU(timeout_cpu);
133 	/*
134 	 * Calculate callout wheel size
135 	 */
136 	for (callwheelsize = 1, callwheelbits = 0;
137 	     callwheelsize < ncallout;
138 	     callwheelsize <<= 1, ++callwheelbits)
139 		;
140 	callwheelmask = callwheelsize - 1;
141 
142 	cc->cc_callout = (struct callout *)v;
143 	v = (caddr_t)(cc->cc_callout + ncallout);
144 	cc->cc_callwheel = (struct callout_tailq *)v;
145 	v = (caddr_t)(cc->cc_callwheel + callwheelsize);
146 	return(v);
147 }
148 
149 static void
150 callout_cpu_init(struct callout_cpu *cc)
151 {
152 	struct callout *c;
153 	int i;
154 
155 	mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
156 	SLIST_INIT(&cc->cc_callfree);
157 	for (i = 0; i < callwheelsize; i++) {
158 		TAILQ_INIT(&cc->cc_callwheel[i]);
159 	}
160 	if (cc->cc_callout == NULL)
161 		return;
162 	for (i = 0; i < ncallout; i++) {
163 		c = &cc->cc_callout[i];
164 		callout_init(c, 0);
165 		c->c_flags = CALLOUT_LOCAL_ALLOC;
166 		SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
167 	}
168 }
169 
170 /*
171  * kern_timeout_callwheel_init() - initialize previously reserved callwheel
172  *				   space.
173  *
174  *	This code is called just once, after the space reserved for the
175  *	callout wheel has been finalized.
176  */
177 void
178 kern_timeout_callwheel_init(void)
179 {
180 	callout_cpu_init(CC_CPU(timeout_cpu));
181 }
182 
183 /*
184  * Start standard softclock thread.
185  */
186 void    *softclock_ih;
187 
188 static void
189 start_softclock(void *dummy)
190 {
191 	struct callout_cpu *cc;
192 #ifdef SMP
193 	int cpu;
194 #endif
195 
196 	cc = CC_CPU(timeout_cpu);
197 	if (swi_add(&clk_intr_event, "clock", softclock, cc, SWI_CLOCK,
198 	    INTR_MPSAFE, &softclock_ih))
199 		panic("died while creating standard software ithreads");
200 	cc->cc_cookie = softclock_ih;
201 #ifdef SMP
202 	for (cpu = 0; cpu <= mp_maxid; cpu++) {
203 		if (cpu == timeout_cpu)
204 			continue;
205 		if (CPU_ABSENT(cpu))
206 			continue;
207 		cc = CC_CPU(cpu);
208 		if (swi_add(NULL, "clock", softclock, cc, SWI_CLOCK,
209 		    INTR_MPSAFE, &cc->cc_cookie))
210 			panic("died while creating standard software ithreads");
211 		cc->cc_callout = NULL;	/* Only cpu0 handles timeout(). */
212 		cc->cc_callwheel = malloc(
213 		    sizeof(struct callout_tailq) * callwheelsize, M_CALLOUT,
214 		    M_WAITOK);
215 		callout_cpu_init(cc);
216 	}
217 #endif
218 }
219 
220 SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL);
221 
222 void
223 callout_tick(void)
224 {
225 	struct callout_cpu *cc;
226 	int need_softclock;
227 	int bucket;
228 
229 	/*
230 	 * Process callouts at a very low cpu priority, so we don't keep the
231 	 * relatively high clock interrupt priority any longer than necessary.
232 	 */
233 	need_softclock = 0;
234 	cc = CC_SELF();
235 	mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
236 	for (; (cc->cc_softticks - ticks) < 0; cc->cc_softticks++) {
237 		bucket = cc->cc_softticks & callwheelmask;
238 		if (!TAILQ_EMPTY(&cc->cc_callwheel[bucket])) {
239 			need_softclock = 1;
240 			break;
241 		}
242 	}
243 	mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
244 	/*
245 	 * swi_sched acquires the thread lock, so we don't want to call it
246 	 * with cc_lock held; incorrect locking order.
247 	 */
248 	if (need_softclock)
249 		swi_sched(cc->cc_cookie, 0);
250 }
251 
252 static struct callout_cpu *
253 callout_lock(struct callout *c)
254 {
255 	struct callout_cpu *cc;
256 	int cpu;
257 
258 	for (;;) {
259 		cpu = c->c_cpu;
260 		cc = CC_CPU(cpu);
261 		CC_LOCK(cc);
262 		if (cpu == c->c_cpu)
263 			break;
264 		CC_UNLOCK(cc);
265 	}
266 	return (cc);
267 }
268 
269 /*
270  * The callout mechanism is based on the work of Adam M. Costello and
271  * George Varghese, published in a technical report entitled "Redesigning
272  * the BSD Callout and Timer Facilities" and modified slightly for inclusion
273  * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
274  * used in this implementation was published by G. Varghese and T. Lauck in
275  * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
276  * the Efficient Implementation of a Timer Facility" in the Proceedings of
277  * the 11th ACM Annual Symposium on Operating Systems Principles,
278  * Austin, Texas Nov 1987.
279  */
280 
281 /*
282  * Software (low priority) clock interrupt.
283  * Run periodic events from timeout queue.
284  */
285 void
286 softclock(void *arg)
287 {
288 	struct callout_cpu *cc;
289 	struct callout *c;
290 	struct callout_tailq *bucket;
291 	int curticks;
292 	int steps;	/* #steps since we last allowed interrupts */
293 	int depth;
294 	int mpcalls;
295 	int lockcalls;
296 	int gcalls;
297 #ifdef DIAGNOSTIC
298 	struct bintime bt1, bt2;
299 	struct timespec ts2;
300 	static uint64_t maxdt = 36893488147419102LL;	/* 2 msec */
301 	static timeout_t *lastfunc;
302 #endif
303 
304 #ifndef MAX_SOFTCLOCK_STEPS
305 #define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
306 #endif /* MAX_SOFTCLOCK_STEPS */
307 
308 	mpcalls = 0;
309 	lockcalls = 0;
310 	gcalls = 0;
311 	depth = 0;
312 	steps = 0;
313 	cc = (struct callout_cpu *)arg;
314 	CC_LOCK(cc);
315 	while (cc->cc_softticks != ticks) {
316 		/*
317 		 * cc_softticks may be modified by hard clock, so cache
318 		 * it while we work on a given bucket.
319 		 */
320 		curticks = cc->cc_softticks;
321 		cc->cc_softticks++;
322 		bucket = &cc->cc_callwheel[curticks & callwheelmask];
323 		c = TAILQ_FIRST(bucket);
324 		while (c) {
325 			depth++;
326 			if (c->c_time != curticks) {
327 				c = TAILQ_NEXT(c, c_links.tqe);
328 				++steps;
329 				if (steps >= MAX_SOFTCLOCK_STEPS) {
330 					cc->cc_next = c;
331 					/* Give interrupts a chance. */
332 					CC_UNLOCK(cc);
333 					;	/* nothing */
334 					CC_LOCK(cc);
335 					c = cc->cc_next;
336 					steps = 0;
337 				}
338 			} else {
339 				void (*c_func)(void *);
340 				void *c_arg;
341 				struct lock_class *class;
342 				struct lock_object *c_lock;
343 				int c_flags, sharedlock;
344 
345 				cc->cc_next = TAILQ_NEXT(c, c_links.tqe);
346 				TAILQ_REMOVE(bucket, c, c_links.tqe);
347 				class = (c->c_lock != NULL) ?
348 				    LOCK_CLASS(c->c_lock) : NULL;
349 				sharedlock = (c->c_flags & CALLOUT_SHAREDLOCK) ?
350 				    0 : 1;
351 				c_lock = c->c_lock;
352 				c_func = c->c_func;
353 				c_arg = c->c_arg;
354 				c_flags = c->c_flags;
355 				if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
356 					c->c_flags = CALLOUT_LOCAL_ALLOC;
357 				} else {
358 					c->c_flags =
359 					    (c->c_flags & ~CALLOUT_PENDING);
360 				}
361 				cc->cc_curr = c;
362 				cc->cc_cancel = 0;
363 				CC_UNLOCK(cc);
364 				if (c_lock != NULL) {
365 					class->lc_lock(c_lock, sharedlock);
366 					/*
367 					 * The callout may have been cancelled
368 					 * while we switched locks.
369 					 */
370 					if (cc->cc_cancel) {
371 						class->lc_unlock(c_lock);
372 						goto skip;
373 					}
374 					/* The callout cannot be stopped now. */
375 					cc->cc_cancel = 1;
376 
377 					if (c_lock == &Giant.lock_object) {
378 						gcalls++;
379 						CTR3(KTR_CALLOUT,
380 						    "callout %p func %p arg %p",
381 						    c, c_func, c_arg);
382 					} else {
383 						lockcalls++;
384 						CTR3(KTR_CALLOUT, "callout lock"
385 						    " %p func %p arg %p",
386 						    c, c_func, c_arg);
387 					}
388 				} else {
389 					mpcalls++;
390 					CTR3(KTR_CALLOUT,
391 					    "callout mpsafe %p func %p arg %p",
392 					    c, c_func, c_arg);
393 				}
394 #ifdef DIAGNOSTIC
395 				binuptime(&bt1);
396 #endif
397 				THREAD_NO_SLEEPING();
398 				c_func(c_arg);
399 				THREAD_SLEEPING_OK();
400 #ifdef DIAGNOSTIC
401 				binuptime(&bt2);
402 				bintime_sub(&bt2, &bt1);
403 				if (bt2.frac > maxdt) {
404 					if (lastfunc != c_func ||
405 					    bt2.frac > maxdt * 2) {
406 						bintime2timespec(&bt2, &ts2);
407 						printf(
408 			"Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
409 						    c_func, c_arg,
410 						    (intmax_t)ts2.tv_sec,
411 						    ts2.tv_nsec);
412 					}
413 					maxdt = bt2.frac;
414 					lastfunc = c_func;
415 				}
416 #endif
417 				CTR1(KTR_CALLOUT, "callout %p finished", c);
418 				if ((c_flags & CALLOUT_RETURNUNLOCKED) == 0)
419 					class->lc_unlock(c_lock);
420 			skip:
421 				CC_LOCK(cc);
422 				/*
423 				 * If the current callout is locally
424 				 * allocated (from timeout(9))
425 				 * then put it on the freelist.
426 				 *
427 				 * Note: we need to check the cached
428 				 * copy of c_flags because if it was not
429 				 * local, then it's not safe to deref the
430 				 * callout pointer.
431 				 */
432 				if (c_flags & CALLOUT_LOCAL_ALLOC) {
433 					KASSERT(c->c_flags ==
434 					    CALLOUT_LOCAL_ALLOC,
435 					    ("corrupted callout"));
436 					c->c_func = NULL;
437 					SLIST_INSERT_HEAD(&cc->cc_callfree, c,
438 					    c_links.sle);
439 				}
440 				cc->cc_curr = NULL;
441 				if (cc->cc_waiting) {
442 					/*
443 					 * There is someone waiting
444 					 * for the callout to complete.
445 					 */
446 					cc->cc_waiting = 0;
447 					CC_UNLOCK(cc);
448 					wakeup(&cc->cc_waiting);
449 					CC_LOCK(cc);
450 				}
451 				steps = 0;
452 				c = cc->cc_next;
453 			}
454 		}
455 	}
456 	avg_depth += (depth * 1000 - avg_depth) >> 8;
457 	avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
458 	avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8;
459 	avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
460 	cc->cc_next = NULL;
461 	CC_UNLOCK(cc);
462 }
463 
464 /*
465  * timeout --
466  *	Execute a function after a specified length of time.
467  *
468  * untimeout --
469  *	Cancel previous timeout function call.
470  *
471  * callout_handle_init --
472  *	Initialize a handle so that using it with untimeout is benign.
473  *
474  *	See AT&T BCI Driver Reference Manual for specification.  This
475  *	implementation differs from that one in that although an
476  *	identification value is returned from timeout, the original
477  *	arguments to timeout as well as the identifier are used to
478  *	identify entries for untimeout.
479  */
480 struct callout_handle
481 timeout(ftn, arg, to_ticks)
482 	timeout_t *ftn;
483 	void *arg;
484 	int to_ticks;
485 {
486 	struct callout_cpu *cc;
487 	struct callout *new;
488 	struct callout_handle handle;
489 
490 	cc = CC_CPU(timeout_cpu);
491 	CC_LOCK(cc);
492 	/* Fill in the next free callout structure. */
493 	new = SLIST_FIRST(&cc->cc_callfree);
494 	if (new == NULL)
495 		/* XXX Attempt to malloc first */
496 		panic("timeout table full");
497 	SLIST_REMOVE_HEAD(&cc->cc_callfree, c_links.sle);
498 	callout_reset(new, to_ticks, ftn, arg);
499 	handle.callout = new;
500 	CC_UNLOCK(cc);
501 
502 	return (handle);
503 }
504 
505 void
506 untimeout(ftn, arg, handle)
507 	timeout_t *ftn;
508 	void *arg;
509 	struct callout_handle handle;
510 {
511 	struct callout_cpu *cc;
512 
513 	/*
514 	 * Check for a handle that was initialized
515 	 * by callout_handle_init, but never used
516 	 * for a real timeout.
517 	 */
518 	if (handle.callout == NULL)
519 		return;
520 
521 	cc = callout_lock(handle.callout);
522 	if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
523 		callout_stop(handle.callout);
524 	CC_UNLOCK(cc);
525 }
526 
527 void
528 callout_handle_init(struct callout_handle *handle)
529 {
530 	handle->callout = NULL;
531 }
532 
533 /*
534  * New interface; clients allocate their own callout structures.
535  *
536  * callout_reset() - establish or change a timeout
537  * callout_stop() - disestablish a timeout
538  * callout_init() - initialize a callout structure so that it can
539  *	safely be passed to callout_reset() and callout_stop()
540  *
541  * <sys/callout.h> defines three convenience macros:
542  *
543  * callout_active() - returns truth if callout has not been stopped,
544  *	drained, or deactivated since the last time the callout was
545  *	reset.
546  * callout_pending() - returns truth if callout is still waiting for timeout
547  * callout_deactivate() - marks the callout as having been serviced
548  */
549 int
550 callout_reset_on(struct callout *c, int to_ticks, void (*ftn)(void *),
551     void *arg, int cpu)
552 {
553 	struct callout_cpu *cc;
554 	int cancelled = 0;
555 
556 	/*
557 	 * Don't allow migration of pre-allocated callouts lest they
558 	 * become unbalanced.
559 	 */
560 	if (c->c_flags & CALLOUT_LOCAL_ALLOC)
561 		cpu = c->c_cpu;
562 retry:
563 	cc = callout_lock(c);
564 	if (cc->cc_curr == c) {
565 		/*
566 		 * We're being asked to reschedule a callout which is
567 		 * currently in progress.  If there is a lock then we
568 		 * can cancel the callout if it has not really started.
569 		 */
570 		if (c->c_lock != NULL && !cc->cc_cancel)
571 			cancelled = cc->cc_cancel = 1;
572 		if (cc->cc_waiting) {
573 			/*
574 			 * Someone has called callout_drain to kill this
575 			 * callout.  Don't reschedule.
576 			 */
577 			CTR4(KTR_CALLOUT, "%s %p func %p arg %p",
578 			    cancelled ? "cancelled" : "failed to cancel",
579 			    c, c->c_func, c->c_arg);
580 			CC_UNLOCK(cc);
581 			return (cancelled);
582 		}
583 	}
584 	if (c->c_flags & CALLOUT_PENDING) {
585 		if (cc->cc_next == c) {
586 			cc->cc_next = TAILQ_NEXT(c, c_links.tqe);
587 		}
588 		TAILQ_REMOVE(&cc->cc_callwheel[c->c_time & callwheelmask], c,
589 		    c_links.tqe);
590 
591 		cancelled = 1;
592 		c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
593 	}
594 	/*
595 	 * If the lock must migrate we have to check the state again as
596 	 * we can't hold both the new and old locks simultaneously.
597 	 */
598 	if (c->c_cpu != cpu) {
599 		c->c_cpu = cpu;
600 		CC_UNLOCK(cc);
601 		goto retry;
602 	}
603 
604 	if (to_ticks <= 0)
605 		to_ticks = 1;
606 
607 	c->c_arg = arg;
608 	c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
609 	c->c_func = ftn;
610 	c->c_time = ticks + to_ticks;
611 	TAILQ_INSERT_TAIL(&cc->cc_callwheel[c->c_time & callwheelmask],
612 			  c, c_links.tqe);
613 	CTR5(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d",
614 	    cancelled ? "re" : "", c, c->c_func, c->c_arg, to_ticks);
615 	CC_UNLOCK(cc);
616 
617 	return (cancelled);
618 }
619 
620 /*
621  * Common idioms that can be optimized in the future.
622  */
623 int
624 callout_schedule_on(struct callout *c, int to_ticks, int cpu)
625 {
626 	return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu);
627 }
628 
629 int
630 callout_schedule(struct callout *c, int to_ticks)
631 {
632 	return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu);
633 }
634 
635 int
636 _callout_stop_safe(c, safe)
637 	struct	callout *c;
638 	int	safe;
639 {
640 	struct callout_cpu *cc;
641 	struct lock_class *class;
642 	int use_lock, sq_locked;
643 
644 	/*
645 	 * Some old subsystems don't hold Giant while running a callout_stop(),
646 	 * so just discard this check for the moment.
647 	 */
648 	if (!safe && c->c_lock != NULL) {
649 		if (c->c_lock == &Giant.lock_object)
650 			use_lock = mtx_owned(&Giant);
651 		else {
652 			use_lock = 1;
653 			class = LOCK_CLASS(c->c_lock);
654 			class->lc_assert(c->c_lock, LA_XLOCKED);
655 		}
656 	} else
657 		use_lock = 0;
658 
659 	sq_locked = 0;
660 again:
661 	cc = callout_lock(c);
662 	/*
663 	 * If the callout isn't pending, it's not on the queue, so
664 	 * don't attempt to remove it from the queue.  We can try to
665 	 * stop it by other means however.
666 	 */
667 	if (!(c->c_flags & CALLOUT_PENDING)) {
668 		c->c_flags &= ~CALLOUT_ACTIVE;
669 
670 		/*
671 		 * If it wasn't on the queue and it isn't the current
672 		 * callout, then we can't stop it, so just bail.
673 		 */
674 		if (cc->cc_curr != c) {
675 			CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
676 			    c, c->c_func, c->c_arg);
677 			CC_UNLOCK(cc);
678 			if (sq_locked)
679 				sleepq_release(&cc->cc_waiting);
680 			return (0);
681 		}
682 
683 		if (safe) {
684 			/*
685 			 * The current callout is running (or just
686 			 * about to run) and blocking is allowed, so
687 			 * just wait for the current invocation to
688 			 * finish.
689 			 */
690 			while (cc->cc_curr == c) {
691 
692 				/*
693 				 * Use direct calls to sleepqueue interface
694 				 * instead of cv/msleep in order to avoid
695 				 * a LOR between cc_lock and sleepqueue
696 				 * chain spinlocks.  This piece of code
697 				 * emulates a msleep_spin() call actually.
698 				 *
699 				 * If we already have the sleepqueue chain
700 				 * locked, then we can safely block.  If we
701 				 * don't already have it locked, however,
702 				 * we have to drop the cc_lock to lock
703 				 * it.  This opens several races, so we
704 				 * restart at the beginning once we have
705 				 * both locks.  If nothing has changed, then
706 				 * we will end up back here with sq_locked
707 				 * set.
708 				 */
709 				if (!sq_locked) {
710 					CC_UNLOCK(cc);
711 					sleepq_lock(&cc->cc_waiting);
712 					sq_locked = 1;
713 					goto again;
714 				}
715 				cc->cc_waiting = 1;
716 				DROP_GIANT();
717 				CC_UNLOCK(cc);
718 				sleepq_add(&cc->cc_waiting,
719 				    &cc->cc_lock.lock_object, "codrain",
720 				    SLEEPQ_SLEEP, 0);
721 				sleepq_wait(&cc->cc_waiting, 0);
722 				sq_locked = 0;
723 
724 				/* Reacquire locks previously released. */
725 				PICKUP_GIANT();
726 				CC_LOCK(cc);
727 			}
728 		} else if (use_lock && !cc->cc_cancel) {
729 			/*
730 			 * The current callout is waiting for its
731 			 * lock which we hold.  Cancel the callout
732 			 * and return.  After our caller drops the
733 			 * lock, the callout will be skipped in
734 			 * softclock().
735 			 */
736 			cc->cc_cancel = 1;
737 			CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
738 			    c, c->c_func, c->c_arg);
739 			CC_UNLOCK(cc);
740 			KASSERT(!sq_locked, ("sleepqueue chain locked"));
741 			return (1);
742 		}
743 		CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
744 		    c, c->c_func, c->c_arg);
745 		CC_UNLOCK(cc);
746 		KASSERT(!sq_locked, ("sleepqueue chain still locked"));
747 		return (0);
748 	}
749 	if (sq_locked)
750 		sleepq_release(&cc->cc_waiting);
751 
752 	c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
753 
754 	if (cc->cc_next == c) {
755 		cc->cc_next = TAILQ_NEXT(c, c_links.tqe);
756 	}
757 	TAILQ_REMOVE(&cc->cc_callwheel[c->c_time & callwheelmask], c,
758 	    c_links.tqe);
759 
760 	CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
761 	    c, c->c_func, c->c_arg);
762 
763 	if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
764 		c->c_func = NULL;
765 		SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
766 	}
767 	CC_UNLOCK(cc);
768 	return (1);
769 }
770 
771 void
772 callout_init(c, mpsafe)
773 	struct	callout *c;
774 	int mpsafe;
775 {
776 	bzero(c, sizeof *c);
777 	if (mpsafe) {
778 		c->c_lock = NULL;
779 		c->c_flags = CALLOUT_RETURNUNLOCKED;
780 	} else {
781 		c->c_lock = &Giant.lock_object;
782 		c->c_flags = 0;
783 	}
784 	c->c_cpu = timeout_cpu;
785 }
786 
787 void
788 _callout_init_lock(c, lock, flags)
789 	struct	callout *c;
790 	struct	lock_object *lock;
791 	int flags;
792 {
793 	bzero(c, sizeof *c);
794 	c->c_lock = lock;
795 	KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0,
796 	    ("callout_init_lock: bad flags %d", flags));
797 	KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
798 	    ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock"));
799 	KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags &
800 	    (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class",
801 	    __func__));
802 	c->c_flags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK);
803 	c->c_cpu = timeout_cpu;
804 }
805 
806 #ifdef APM_FIXUP_CALLTODO
807 /*
808  * Adjust the kernel calltodo timeout list.  This routine is used after
809  * an APM resume to recalculate the calltodo timer list values with the
810  * number of hz's we have been sleeping.  The next hardclock() will detect
811  * that there are fired timers and run softclock() to execute them.
812  *
813  * Please note, I have not done an exhaustive analysis of what code this
814  * might break.  I am motivated to have my select()'s and alarm()'s that
815  * have expired during suspend firing upon resume so that the applications
816  * which set the timer can do the maintanence the timer was for as close
817  * as possible to the originally intended time.  Testing this code for a
818  * week showed that resuming from a suspend resulted in 22 to 25 timers
819  * firing, which seemed independant on whether the suspend was 2 hours or
820  * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
821  */
822 void
823 adjust_timeout_calltodo(time_change)
824     struct timeval *time_change;
825 {
826 	register struct callout *p;
827 	unsigned long delta_ticks;
828 
829 	/*
830 	 * How many ticks were we asleep?
831 	 * (stolen from tvtohz()).
832 	 */
833 
834 	/* Don't do anything */
835 	if (time_change->tv_sec < 0)
836 		return;
837 	else if (time_change->tv_sec <= LONG_MAX / 1000000)
838 		delta_ticks = (time_change->tv_sec * 1000000 +
839 			       time_change->tv_usec + (tick - 1)) / tick + 1;
840 	else if (time_change->tv_sec <= LONG_MAX / hz)
841 		delta_ticks = time_change->tv_sec * hz +
842 			      (time_change->tv_usec + (tick - 1)) / tick + 1;
843 	else
844 		delta_ticks = LONG_MAX;
845 
846 	if (delta_ticks > INT_MAX)
847 		delta_ticks = INT_MAX;
848 
849 	/*
850 	 * Now rip through the timer calltodo list looking for timers
851 	 * to expire.
852 	 */
853 
854 	/* don't collide with softclock() */
855 	CC_LOCK(cc);
856 	for (p = calltodo.c_next; p != NULL; p = p->c_next) {
857 		p->c_time -= delta_ticks;
858 
859 		/* Break if the timer had more time on it than delta_ticks */
860 		if (p->c_time > 0)
861 			break;
862 
863 		/* take back the ticks the timer didn't use (p->c_time <= 0) */
864 		delta_ticks = -p->c_time;
865 	}
866 	CC_UNLOCK(cc);
867 
868 	return;
869 }
870 #endif /* APM_FIXUP_CALLTODO */
871