1 /* $NetBSD: kern_timeout.c,v 1.47 2013/09/14 20:53:48 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2003, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe, and by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 2001 Thomas Nordin <nordin@openbsd.org> 34 * Copyright (c) 2000-2001 Artur Grabowski <art@openbsd.org> 35 * All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. The name of the author may not be used to endorse or promote products 47 * derived from this software without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 50 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 51 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 52 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 55 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 57 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 58 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 61 #include <sys/cdefs.h> 62 __KERNEL_RCSID(0, "$NetBSD: kern_timeout.c,v 1.47 2013/09/14 20:53:48 martin Exp $"); 63 64 /* 65 * Timeouts are kept in a hierarchical timing wheel. The c_time is the 66 * value of c_cpu->cc_ticks when the timeout should be called. There are 67 * four levels with 256 buckets each. See 'Scheme 7' in "Hashed and 68 * Hierarchical Timing Wheels: Efficient Data Structures for Implementing 69 * a Timer Facility" by George Varghese and Tony Lauck. 70 * 71 * Some of the "math" in here is a bit tricky. We have to beware of 72 * wrapping ints. 73 * 74 * We use the fact that any element added to the queue must be added with 75 * a positive time. That means that any element `to' on the queue cannot 76 * be scheduled to timeout further in time than INT_MAX, but c->c_time can 77 * be positive or negative so comparing it with anything is dangerous. 78 * The only way we can use the c->c_time value in any predictable way is 79 * when we calculate how far in the future `to' will timeout - "c->c_time 80 * - c->c_cpu->cc_ticks". The result will always be positive for future 81 * timeouts and 0 or negative for due timeouts. 82 */ 83 84 #define _CALLOUT_PRIVATE 85 86 #include <sys/param.h> 87 #include <sys/systm.h> 88 #include <sys/kernel.h> 89 #include <sys/callout.h> 90 #include <sys/lwp.h> 91 #include <sys/mutex.h> 92 #include <sys/proc.h> 93 #include <sys/sleepq.h> 94 #include <sys/syncobj.h> 95 #include <sys/evcnt.h> 96 #include <sys/intr.h> 97 #include <sys/cpu.h> 98 #include <sys/kmem.h> 99 100 #ifdef DDB 101 #include <machine/db_machdep.h> 102 #include <ddb/db_interface.h> 103 #include <ddb/db_access.h> 104 #include <ddb/db_sym.h> 105 #include <ddb/db_output.h> 106 #endif 107 108 #define BUCKETS 1024 109 #define WHEELSIZE 256 110 #define WHEELMASK 255 111 #define WHEELBITS 8 112 113 #define MASKWHEEL(wheel, time) (((time) >> ((wheel)*WHEELBITS)) & WHEELMASK) 114 115 #define BUCKET(cc, rel, abs) \ 116 (((rel) <= (1 << (2*WHEELBITS))) \ 117 ? ((rel) <= (1 << WHEELBITS)) \ 118 ? &(cc)->cc_wheel[MASKWHEEL(0, (abs))] \ 119 : &(cc)->cc_wheel[MASKWHEEL(1, (abs)) + WHEELSIZE] \ 120 : ((rel) <= (1 << (3*WHEELBITS))) \ 121 ? &(cc)->cc_wheel[MASKWHEEL(2, (abs)) + 2*WHEELSIZE] \ 122 : &(cc)->cc_wheel[MASKWHEEL(3, (abs)) + 3*WHEELSIZE]) 123 124 #define MOVEBUCKET(cc, wheel, time) \ 125 CIRCQ_APPEND(&(cc)->cc_todo, \ 126 &(cc)->cc_wheel[MASKWHEEL((wheel), (time)) + (wheel)*WHEELSIZE]) 127 128 /* 129 * Circular queue definitions. 130 */ 131 132 #define CIRCQ_INIT(list) \ 133 do { \ 134 (list)->cq_next_l = (list); \ 135 (list)->cq_prev_l = (list); \ 136 } while (/*CONSTCOND*/0) 137 138 #define CIRCQ_INSERT(elem, list) \ 139 do { \ 140 (elem)->cq_prev_e = (list)->cq_prev_e; \ 141 (elem)->cq_next_l = (list); \ 142 (list)->cq_prev_l->cq_next_l = (elem); \ 143 (list)->cq_prev_l = (elem); \ 144 } while (/*CONSTCOND*/0) 145 146 #define CIRCQ_APPEND(fst, snd) \ 147 do { \ 148 if (!CIRCQ_EMPTY(snd)) { \ 149 (fst)->cq_prev_l->cq_next_l = (snd)->cq_next_l; \ 150 (snd)->cq_next_l->cq_prev_l = (fst)->cq_prev_l; \ 151 (snd)->cq_prev_l->cq_next_l = (fst); \ 152 (fst)->cq_prev_l = (snd)->cq_prev_l; \ 153 CIRCQ_INIT(snd); \ 154 } \ 155 } while (/*CONSTCOND*/0) 156 157 #define CIRCQ_REMOVE(elem) \ 158 do { \ 159 (elem)->cq_next_l->cq_prev_e = (elem)->cq_prev_e; \ 160 (elem)->cq_prev_l->cq_next_e = (elem)->cq_next_e; \ 161 } while (/*CONSTCOND*/0) 162 163 #define CIRCQ_FIRST(list) ((list)->cq_next_e) 164 #define CIRCQ_NEXT(elem) ((elem)->cq_next_e) 165 #define CIRCQ_LAST(elem,list) ((elem)->cq_next_l == (list)) 166 #define CIRCQ_EMPTY(list) ((list)->cq_next_l == (list)) 167 168 static void callout_softclock(void *); 169 170 struct callout_cpu { 171 kmutex_t *cc_lock; 172 sleepq_t cc_sleepq; 173 u_int cc_nwait; 174 u_int cc_ticks; 175 lwp_t *cc_lwp; 176 callout_impl_t *cc_active; 177 callout_impl_t *cc_cancel; 178 struct evcnt cc_ev_late; 179 struct evcnt cc_ev_block; 180 struct callout_circq cc_todo; /* Worklist */ 181 struct callout_circq cc_wheel[BUCKETS]; /* Queues of timeouts */ 182 char cc_name1[12]; 183 char cc_name2[12]; 184 }; 185 186 static struct callout_cpu callout_cpu0; 187 static void *callout_sih; 188 189 static inline kmutex_t * 190 callout_lock(callout_impl_t *c) 191 { 192 struct callout_cpu *cc; 193 kmutex_t *lock; 194 195 for (;;) { 196 cc = c->c_cpu; 197 lock = cc->cc_lock; 198 mutex_spin_enter(lock); 199 if (__predict_true(cc == c->c_cpu)) 200 return lock; 201 mutex_spin_exit(lock); 202 } 203 } 204 205 /* 206 * callout_startup: 207 * 208 * Initialize the callout facility, called at system startup time. 209 * Do just enough to allow callouts to be safely registered. 210 */ 211 void 212 callout_startup(void) 213 { 214 struct callout_cpu *cc; 215 int b; 216 217 KASSERT(curcpu()->ci_data.cpu_callout == NULL); 218 219 cc = &callout_cpu0; 220 cc->cc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED); 221 CIRCQ_INIT(&cc->cc_todo); 222 for (b = 0; b < BUCKETS; b++) 223 CIRCQ_INIT(&cc->cc_wheel[b]); 224 curcpu()->ci_data.cpu_callout = cc; 225 } 226 227 /* 228 * callout_init_cpu: 229 * 230 * Per-CPU initialization. 231 */ 232 CTASSERT(sizeof(callout_impl_t) <= sizeof(callout_t)); 233 234 void 235 callout_init_cpu(struct cpu_info *ci) 236 { 237 struct callout_cpu *cc; 238 int b; 239 240 if ((cc = ci->ci_data.cpu_callout) == NULL) { 241 cc = kmem_zalloc(sizeof(*cc), KM_SLEEP); 242 if (cc == NULL) 243 panic("callout_init_cpu (1)"); 244 cc->cc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED); 245 CIRCQ_INIT(&cc->cc_todo); 246 for (b = 0; b < BUCKETS; b++) 247 CIRCQ_INIT(&cc->cc_wheel[b]); 248 } else { 249 /* Boot CPU, one time only. */ 250 callout_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE, 251 callout_softclock, NULL); 252 if (callout_sih == NULL) 253 panic("callout_init_cpu (2)"); 254 } 255 256 sleepq_init(&cc->cc_sleepq); 257 258 snprintf(cc->cc_name1, sizeof(cc->cc_name1), "late/%u", 259 cpu_index(ci)); 260 evcnt_attach_dynamic(&cc->cc_ev_late, EVCNT_TYPE_MISC, 261 NULL, "callout", cc->cc_name1); 262 263 snprintf(cc->cc_name2, sizeof(cc->cc_name2), "wait/%u", 264 cpu_index(ci)); 265 evcnt_attach_dynamic(&cc->cc_ev_block, EVCNT_TYPE_MISC, 266 NULL, "callout", cc->cc_name2); 267 268 ci->ci_data.cpu_callout = cc; 269 } 270 271 /* 272 * callout_init: 273 * 274 * Initialize a callout structure. This must be quick, so we fill 275 * only the minimum number of fields. 276 */ 277 void 278 callout_init(callout_t *cs, u_int flags) 279 { 280 callout_impl_t *c = (callout_impl_t *)cs; 281 struct callout_cpu *cc; 282 283 KASSERT((flags & ~CALLOUT_FLAGMASK) == 0); 284 285 cc = curcpu()->ci_data.cpu_callout; 286 c->c_func = NULL; 287 c->c_magic = CALLOUT_MAGIC; 288 if (__predict_true((flags & CALLOUT_MPSAFE) != 0 && cc != NULL)) { 289 c->c_flags = flags; 290 c->c_cpu = cc; 291 return; 292 } 293 c->c_flags = flags | CALLOUT_BOUND; 294 c->c_cpu = &callout_cpu0; 295 } 296 297 /* 298 * callout_destroy: 299 * 300 * Destroy a callout structure. The callout must be stopped. 301 */ 302 void 303 callout_destroy(callout_t *cs) 304 { 305 callout_impl_t *c = (callout_impl_t *)cs; 306 307 /* 308 * It's not necessary to lock in order to see the correct value 309 * of c->c_flags. If the callout could potentially have been 310 * running, the current thread should have stopped it. 311 */ 312 KASSERT((c->c_flags & CALLOUT_PENDING) == 0); 313 KASSERT(c->c_cpu->cc_lwp == curlwp || c->c_cpu->cc_active != c); 314 KASSERTMSG(c->c_magic == CALLOUT_MAGIC, 315 "callout %p: c_magic (%#x) != CALLOUT_MAGIC (%#x)", 316 c, c->c_magic, CALLOUT_MAGIC); 317 c->c_magic = 0; 318 } 319 320 /* 321 * callout_schedule_locked: 322 * 323 * Schedule a callout to run. The function and argument must 324 * already be set in the callout structure. Must be called with 325 * callout_lock. 326 */ 327 static void 328 callout_schedule_locked(callout_impl_t *c, kmutex_t *lock, int to_ticks) 329 { 330 struct callout_cpu *cc, *occ; 331 int old_time; 332 333 KASSERT(to_ticks >= 0); 334 KASSERT(c->c_func != NULL); 335 336 /* Initialize the time here, it won't change. */ 337 occ = c->c_cpu; 338 c->c_flags &= ~(CALLOUT_FIRED | CALLOUT_INVOKING); 339 340 /* 341 * If this timeout is already scheduled and now is moved 342 * earlier, reschedule it now. Otherwise leave it in place 343 * and let it be rescheduled later. 344 */ 345 if ((c->c_flags & CALLOUT_PENDING) != 0) { 346 /* Leave on existing CPU. */ 347 old_time = c->c_time; 348 c->c_time = to_ticks + occ->cc_ticks; 349 if (c->c_time - old_time < 0) { 350 CIRCQ_REMOVE(&c->c_list); 351 CIRCQ_INSERT(&c->c_list, &occ->cc_todo); 352 } 353 mutex_spin_exit(lock); 354 return; 355 } 356 357 cc = curcpu()->ci_data.cpu_callout; 358 if ((c->c_flags & CALLOUT_BOUND) != 0 || cc == occ || 359 !mutex_tryenter(cc->cc_lock)) { 360 /* Leave on existing CPU. */ 361 c->c_time = to_ticks + occ->cc_ticks; 362 c->c_flags |= CALLOUT_PENDING; 363 CIRCQ_INSERT(&c->c_list, &occ->cc_todo); 364 } else { 365 /* Move to this CPU. */ 366 c->c_cpu = cc; 367 c->c_time = to_ticks + cc->cc_ticks; 368 c->c_flags |= CALLOUT_PENDING; 369 CIRCQ_INSERT(&c->c_list, &cc->cc_todo); 370 mutex_spin_exit(cc->cc_lock); 371 } 372 mutex_spin_exit(lock); 373 } 374 375 /* 376 * callout_reset: 377 * 378 * Reset a callout structure with a new function and argument, and 379 * schedule it to run. 380 */ 381 void 382 callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg) 383 { 384 callout_impl_t *c = (callout_impl_t *)cs; 385 kmutex_t *lock; 386 387 KASSERT(c->c_magic == CALLOUT_MAGIC); 388 KASSERT(func != NULL); 389 390 lock = callout_lock(c); 391 c->c_func = func; 392 c->c_arg = arg; 393 callout_schedule_locked(c, lock, to_ticks); 394 } 395 396 /* 397 * callout_schedule: 398 * 399 * Schedule a callout to run. The function and argument must 400 * already be set in the callout structure. 401 */ 402 void 403 callout_schedule(callout_t *cs, int to_ticks) 404 { 405 callout_impl_t *c = (callout_impl_t *)cs; 406 kmutex_t *lock; 407 408 KASSERT(c->c_magic == CALLOUT_MAGIC); 409 410 lock = callout_lock(c); 411 callout_schedule_locked(c, lock, to_ticks); 412 } 413 414 /* 415 * callout_stop: 416 * 417 * Try to cancel a pending callout. It may be too late: the callout 418 * could be running on another CPU. If called from interrupt context, 419 * the callout could already be in progress at a lower priority. 420 */ 421 bool 422 callout_stop(callout_t *cs) 423 { 424 callout_impl_t *c = (callout_impl_t *)cs; 425 struct callout_cpu *cc; 426 kmutex_t *lock; 427 bool expired; 428 429 KASSERT(c->c_magic == CALLOUT_MAGIC); 430 431 lock = callout_lock(c); 432 433 if ((c->c_flags & CALLOUT_PENDING) != 0) 434 CIRCQ_REMOVE(&c->c_list); 435 expired = ((c->c_flags & CALLOUT_FIRED) != 0); 436 c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED); 437 438 cc = c->c_cpu; 439 if (cc->cc_active == c) { 440 /* 441 * This is for non-MPSAFE callouts only. To synchronize 442 * effectively we must be called with kernel_lock held. 443 * It's also taken in callout_softclock. 444 */ 445 cc->cc_cancel = c; 446 } 447 448 mutex_spin_exit(lock); 449 450 return expired; 451 } 452 453 /* 454 * callout_halt: 455 * 456 * Cancel a pending callout. If in-flight, block until it completes. 457 * May not be called from a hard interrupt handler. If the callout 458 * can take locks, the caller of callout_halt() must not hold any of 459 * those locks, otherwise the two could deadlock. If 'interlock' is 460 * non-NULL and we must wait for the callout to complete, it will be 461 * released and re-acquired before returning. 462 */ 463 bool 464 callout_halt(callout_t *cs, void *interlock) 465 { 466 callout_impl_t *c = (callout_impl_t *)cs; 467 struct callout_cpu *cc; 468 struct lwp *l; 469 kmutex_t *lock, *relock; 470 bool expired; 471 472 KASSERT(c->c_magic == CALLOUT_MAGIC); 473 KASSERT(!cpu_intr_p()); 474 475 lock = callout_lock(c); 476 relock = NULL; 477 478 expired = ((c->c_flags & CALLOUT_FIRED) != 0); 479 if ((c->c_flags & CALLOUT_PENDING) != 0) 480 CIRCQ_REMOVE(&c->c_list); 481 c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED); 482 483 l = curlwp; 484 for (;;) { 485 cc = c->c_cpu; 486 if (__predict_true(cc->cc_active != c || cc->cc_lwp == l)) 487 break; 488 if (interlock != NULL) { 489 /* 490 * Avoid potential scheduler lock order problems by 491 * dropping the interlock without the callout lock 492 * held. 493 */ 494 mutex_spin_exit(lock); 495 mutex_exit(interlock); 496 relock = interlock; 497 interlock = NULL; 498 } else { 499 /* XXX Better to do priority inheritance. */ 500 KASSERT(l->l_wchan == NULL); 501 cc->cc_nwait++; 502 cc->cc_ev_block.ev_count++; 503 l->l_kpriority = true; 504 sleepq_enter(&cc->cc_sleepq, l, cc->cc_lock); 505 sleepq_enqueue(&cc->cc_sleepq, cc, "callout", 506 &sleep_syncobj); 507 sleepq_block(0, false); 508 } 509 lock = callout_lock(c); 510 } 511 512 mutex_spin_exit(lock); 513 if (__predict_false(relock != NULL)) 514 mutex_enter(relock); 515 516 return expired; 517 } 518 519 #ifdef notyet 520 /* 521 * callout_bind: 522 * 523 * Bind a callout so that it will only execute on one CPU. 524 * The callout must be stopped, and must be MPSAFE. 525 * 526 * XXX Disabled for now until it is decided how to handle 527 * offlined CPUs. We may want weak+strong binding. 528 */ 529 void 530 callout_bind(callout_t *cs, struct cpu_info *ci) 531 { 532 callout_impl_t *c = (callout_impl_t *)cs; 533 struct callout_cpu *cc; 534 kmutex_t *lock; 535 536 KASSERT((c->c_flags & CALLOUT_PENDING) == 0); 537 KASSERT(c->c_cpu->cc_active != c); 538 KASSERT(c->c_magic == CALLOUT_MAGIC); 539 KASSERT((c->c_flags & CALLOUT_MPSAFE) != 0); 540 541 lock = callout_lock(c); 542 cc = ci->ci_data.cpu_callout; 543 c->c_flags |= CALLOUT_BOUND; 544 if (c->c_cpu != cc) { 545 /* 546 * Assigning c_cpu effectively unlocks the callout 547 * structure, as we don't hold the new CPU's lock. 548 * Issue memory barrier to prevent accesses being 549 * reordered. 550 */ 551 membar_exit(); 552 c->c_cpu = cc; 553 } 554 mutex_spin_exit(lock); 555 } 556 #endif 557 558 void 559 callout_setfunc(callout_t *cs, void (*func)(void *), void *arg) 560 { 561 callout_impl_t *c = (callout_impl_t *)cs; 562 kmutex_t *lock; 563 564 KASSERT(c->c_magic == CALLOUT_MAGIC); 565 KASSERT(func != NULL); 566 567 lock = callout_lock(c); 568 c->c_func = func; 569 c->c_arg = arg; 570 mutex_spin_exit(lock); 571 } 572 573 bool 574 callout_expired(callout_t *cs) 575 { 576 callout_impl_t *c = (callout_impl_t *)cs; 577 kmutex_t *lock; 578 bool rv; 579 580 KASSERT(c->c_magic == CALLOUT_MAGIC); 581 582 lock = callout_lock(c); 583 rv = ((c->c_flags & CALLOUT_FIRED) != 0); 584 mutex_spin_exit(lock); 585 586 return rv; 587 } 588 589 bool 590 callout_active(callout_t *cs) 591 { 592 callout_impl_t *c = (callout_impl_t *)cs; 593 kmutex_t *lock; 594 bool rv; 595 596 KASSERT(c->c_magic == CALLOUT_MAGIC); 597 598 lock = callout_lock(c); 599 rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0); 600 mutex_spin_exit(lock); 601 602 return rv; 603 } 604 605 bool 606 callout_pending(callout_t *cs) 607 { 608 callout_impl_t *c = (callout_impl_t *)cs; 609 kmutex_t *lock; 610 bool rv; 611 612 KASSERT(c->c_magic == CALLOUT_MAGIC); 613 614 lock = callout_lock(c); 615 rv = ((c->c_flags & CALLOUT_PENDING) != 0); 616 mutex_spin_exit(lock); 617 618 return rv; 619 } 620 621 bool 622 callout_invoking(callout_t *cs) 623 { 624 callout_impl_t *c = (callout_impl_t *)cs; 625 kmutex_t *lock; 626 bool rv; 627 628 KASSERT(c->c_magic == CALLOUT_MAGIC); 629 630 lock = callout_lock(c); 631 rv = ((c->c_flags & CALLOUT_INVOKING) != 0); 632 mutex_spin_exit(lock); 633 634 return rv; 635 } 636 637 void 638 callout_ack(callout_t *cs) 639 { 640 callout_impl_t *c = (callout_impl_t *)cs; 641 kmutex_t *lock; 642 643 KASSERT(c->c_magic == CALLOUT_MAGIC); 644 645 lock = callout_lock(c); 646 c->c_flags &= ~CALLOUT_INVOKING; 647 mutex_spin_exit(lock); 648 } 649 650 /* 651 * callout_hardclock: 652 * 653 * Called from hardclock() once every tick. We schedule a soft 654 * interrupt if there is work to be done. 655 */ 656 void 657 callout_hardclock(void) 658 { 659 struct callout_cpu *cc; 660 int needsoftclock, ticks; 661 662 cc = curcpu()->ci_data.cpu_callout; 663 mutex_spin_enter(cc->cc_lock); 664 665 ticks = ++cc->cc_ticks; 666 667 MOVEBUCKET(cc, 0, ticks); 668 if (MASKWHEEL(0, ticks) == 0) { 669 MOVEBUCKET(cc, 1, ticks); 670 if (MASKWHEEL(1, ticks) == 0) { 671 MOVEBUCKET(cc, 2, ticks); 672 if (MASKWHEEL(2, ticks) == 0) 673 MOVEBUCKET(cc, 3, ticks); 674 } 675 } 676 677 needsoftclock = !CIRCQ_EMPTY(&cc->cc_todo); 678 mutex_spin_exit(cc->cc_lock); 679 680 if (needsoftclock) 681 softint_schedule(callout_sih); 682 } 683 684 /* 685 * callout_softclock: 686 * 687 * Soft interrupt handler, scheduled above if there is work to 688 * be done. Callouts are made in soft interrupt context. 689 */ 690 static void 691 callout_softclock(void *v) 692 { 693 callout_impl_t *c; 694 struct callout_cpu *cc; 695 void (*func)(void *); 696 void *arg; 697 int mpsafe, count, ticks, delta; 698 lwp_t *l; 699 700 l = curlwp; 701 KASSERT(l->l_cpu == curcpu()); 702 cc = l->l_cpu->ci_data.cpu_callout; 703 704 mutex_spin_enter(cc->cc_lock); 705 cc->cc_lwp = l; 706 while (!CIRCQ_EMPTY(&cc->cc_todo)) { 707 c = CIRCQ_FIRST(&cc->cc_todo); 708 KASSERT(c->c_magic == CALLOUT_MAGIC); 709 KASSERT(c->c_func != NULL); 710 KASSERT(c->c_cpu == cc); 711 KASSERT((c->c_flags & CALLOUT_PENDING) != 0); 712 KASSERT((c->c_flags & CALLOUT_FIRED) == 0); 713 CIRCQ_REMOVE(&c->c_list); 714 715 /* If due run it, otherwise insert it into the right bucket. */ 716 ticks = cc->cc_ticks; 717 delta = c->c_time - ticks; 718 if (delta > 0) { 719 CIRCQ_INSERT(&c->c_list, BUCKET(cc, delta, c->c_time)); 720 continue; 721 } 722 if (delta < 0) 723 cc->cc_ev_late.ev_count++; 724 725 c->c_flags = (c->c_flags & ~CALLOUT_PENDING) | 726 (CALLOUT_FIRED | CALLOUT_INVOKING); 727 mpsafe = (c->c_flags & CALLOUT_MPSAFE); 728 func = c->c_func; 729 arg = c->c_arg; 730 cc->cc_active = c; 731 732 mutex_spin_exit(cc->cc_lock); 733 KASSERT(func != NULL); 734 if (__predict_false(!mpsafe)) { 735 KERNEL_LOCK(1, NULL); 736 (*func)(arg); 737 KERNEL_UNLOCK_ONE(NULL); 738 } else 739 (*func)(arg); 740 mutex_spin_enter(cc->cc_lock); 741 742 /* 743 * We can't touch 'c' here because it might be 744 * freed already. If LWPs waiting for callout 745 * to complete, awaken them. 746 */ 747 cc->cc_active = NULL; 748 if ((count = cc->cc_nwait) != 0) { 749 cc->cc_nwait = 0; 750 /* sleepq_wake() drops the lock. */ 751 sleepq_wake(&cc->cc_sleepq, cc, count, cc->cc_lock); 752 mutex_spin_enter(cc->cc_lock); 753 } 754 } 755 cc->cc_lwp = NULL; 756 mutex_spin_exit(cc->cc_lock); 757 } 758 759 #ifdef DDB 760 static void 761 db_show_callout_bucket(struct callout_cpu *cc, struct callout_circq *bucket) 762 { 763 callout_impl_t *c; 764 db_expr_t offset; 765 const char *name; 766 static char question[] = "?"; 767 int b; 768 769 if (CIRCQ_EMPTY(bucket)) 770 return; 771 772 for (c = CIRCQ_FIRST(bucket); /*nothing*/; c = CIRCQ_NEXT(&c->c_list)) { 773 db_find_sym_and_offset((db_addr_t)(intptr_t)c->c_func, &name, 774 &offset); 775 name = name ? name : question; 776 b = (bucket - cc->cc_wheel); 777 if (b < 0) 778 b = -WHEELSIZE; 779 db_printf("%9d %2d/%-4d %16lx %s\n", 780 c->c_time - cc->cc_ticks, b / WHEELSIZE, b, 781 (u_long)c->c_arg, name); 782 if (CIRCQ_LAST(&c->c_list, bucket)) 783 break; 784 } 785 } 786 787 void 788 db_show_callout(db_expr_t addr, bool haddr, db_expr_t count, const char *modif) 789 { 790 CPU_INFO_ITERATOR cii; 791 struct callout_cpu *cc; 792 struct cpu_info *ci; 793 int b; 794 795 db_printf("hardclock_ticks now: %d\n", hardclock_ticks); 796 db_printf(" ticks wheel arg func\n"); 797 798 /* 799 * Don't lock the callwheel; all the other CPUs are paused 800 * anyhow, and we might be called in a circumstance where 801 * some other CPU was paused while holding the lock. 802 */ 803 for (CPU_INFO_FOREACH(cii, ci)) { 804 cc = ci->ci_data.cpu_callout; 805 db_show_callout_bucket(cc, &cc->cc_todo); 806 } 807 for (b = 0; b < BUCKETS; b++) { 808 for (CPU_INFO_FOREACH(cii, ci)) { 809 cc = ci->ci_data.cpu_callout; 810 db_show_callout_bucket(cc, &cc->cc_wheel[b]); 811 } 812 } 813 } 814 #endif /* DDB */ 815