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