source: rtems-libbsd/freebsd/sys/kern/kern_timeout.c @ 1372502

55-freebsd-126-freebsd-12
Last change on this file since 1372502 was 1372502, checked in by Sebastian Huber <sebastian.huber@…>, on 07/26/18 at 12:49:35

Make sure CALLOUT_DFRMIGRATION is not used

This flag supports the callout migration in FreeBSD. This feature is
not supported by libbsd.

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