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