1 /* $NetBSD: linux_work.c,v 1.38 2018/08/27 15:05:44 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Taylor R. Campbell. 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 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: linux_work.c,v 1.38 2018/08/27 15:05:44 riastradh Exp $"); 34 35 #include <sys/types.h> 36 #include <sys/atomic.h> 37 #include <sys/callout.h> 38 #include <sys/condvar.h> 39 #include <sys/errno.h> 40 #include <sys/kmem.h> 41 #include <sys/kthread.h> 42 #include <sys/lwp.h> 43 #include <sys/mutex.h> 44 #include <sys/queue.h> 45 46 #include <linux/workqueue.h> 47 48 struct workqueue_struct { 49 kmutex_t wq_lock; 50 kcondvar_t wq_cv; 51 TAILQ_HEAD(, delayed_work) wq_delayed; 52 TAILQ_HEAD(, work_struct) wq_queue; 53 struct work_struct *wq_current_work; 54 int wq_flags; 55 bool wq_requeued; 56 bool wq_dying; 57 uint64_t wq_gen; 58 struct lwp *wq_lwp; 59 }; 60 61 static void __dead linux_workqueue_thread(void *); 62 static void linux_workqueue_timeout(void *); 63 static struct workqueue_struct * 64 acquire_work(struct work_struct *, 65 struct workqueue_struct *); 66 static void release_work(struct work_struct *, 67 struct workqueue_struct *); 68 static void wait_for_current_work(struct work_struct *, 69 struct workqueue_struct *); 70 static void dw_callout_init(struct workqueue_struct *, 71 struct delayed_work *); 72 static void dw_callout_destroy(struct workqueue_struct *, 73 struct delayed_work *); 74 static void cancel_delayed_work_done(struct workqueue_struct *, 75 struct delayed_work *); 76 77 static specificdata_key_t workqueue_key __read_mostly; 78 79 struct workqueue_struct *system_wq __read_mostly; 80 struct workqueue_struct *system_long_wq __read_mostly; 81 struct workqueue_struct *system_power_efficient_wq __read_mostly; 82 83 /* 84 * linux_workqueue_init() 85 * 86 * Initialize the Linux workqueue subsystem. Return 0 on success, 87 * NetBSD error on failure. 88 */ 89 int 90 linux_workqueue_init(void) 91 { 92 int error; 93 94 error = lwp_specific_key_create(&workqueue_key, NULL); 95 if (error) 96 goto fail0; 97 98 system_wq = alloc_ordered_workqueue("lnxsyswq", 0); 99 if (system_wq == NULL) { 100 error = ENOMEM; 101 goto fail1; 102 } 103 104 system_long_wq = alloc_ordered_workqueue("lnxlngwq", 0); 105 if (system_long_wq == NULL) { 106 error = ENOMEM; 107 goto fail2; 108 } 109 110 system_power_efficient_wq = alloc_ordered_workqueue("lnxpwrwq", 0); 111 if (system_long_wq == NULL) { 112 error = ENOMEM; 113 goto fail3; 114 } 115 116 return 0; 117 118 fail4: __unused 119 destroy_workqueue(system_power_efficient_wq); 120 fail3: destroy_workqueue(system_long_wq); 121 fail2: destroy_workqueue(system_wq); 122 fail1: lwp_specific_key_delete(workqueue_key); 123 fail0: KASSERT(error); 124 return error; 125 } 126 127 /* 128 * linux_workqueue_fini() 129 * 130 * Destroy the Linux workqueue subsystem. Never fails. 131 */ 132 void 133 linux_workqueue_fini(void) 134 { 135 136 destroy_workqueue(system_power_efficient_wq); 137 destroy_workqueue(system_long_wq); 138 destroy_workqueue(system_wq); 139 lwp_specific_key_delete(workqueue_key); 140 } 141 142 /* 143 * Workqueues 144 */ 145 146 /* 147 * alloc_ordered_workqueue(name, flags) 148 * 149 * Create a workqueue of the given name. No flags are currently 150 * defined. Return NULL on failure, pointer to struct 151 * workqueue_struct object on success. 152 */ 153 struct workqueue_struct * 154 alloc_ordered_workqueue(const char *name, int flags) 155 { 156 struct workqueue_struct *wq; 157 int error; 158 159 KASSERT(flags == 0); 160 161 wq = kmem_zalloc(sizeof(*wq), KM_SLEEP); 162 163 mutex_init(&wq->wq_lock, MUTEX_DEFAULT, IPL_NONE); 164 cv_init(&wq->wq_cv, name); 165 TAILQ_INIT(&wq->wq_delayed); 166 TAILQ_INIT(&wq->wq_queue); 167 wq->wq_current_work = NULL; 168 wq->wq_flags = 0; 169 wq->wq_requeued = false; 170 wq->wq_dying = false; 171 wq->wq_gen = 0; 172 wq->wq_lwp = NULL; 173 174 error = kthread_create(PRI_NONE, 175 KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL, 176 &linux_workqueue_thread, wq, &wq->wq_lwp, "%s", name); 177 if (error) 178 goto fail0; 179 180 return wq; 181 182 fail0: KASSERT(TAILQ_EMPTY(&wq->wq_queue)); 183 KASSERT(TAILQ_EMPTY(&wq->wq_delayed)); 184 cv_destroy(&wq->wq_cv); 185 mutex_destroy(&wq->wq_lock); 186 kmem_free(wq, sizeof(*wq)); 187 return NULL; 188 } 189 190 /* 191 * destroy_workqueue(wq) 192 * 193 * Destroy a workqueue created with wq. Cancel any pending 194 * delayed work. Wait for all queued work to complete. 195 * 196 * May sleep. 197 */ 198 void 199 destroy_workqueue(struct workqueue_struct *wq) 200 { 201 202 /* 203 * Cancel all delayed work. We do this first because any 204 * delayed work that that has already timed out, which we can't 205 * cancel, may have queued new work. 206 */ 207 mutex_enter(&wq->wq_lock); 208 while (!TAILQ_EMPTY(&wq->wq_delayed)) { 209 struct delayed_work *const dw = TAILQ_FIRST(&wq->wq_delayed); 210 211 KASSERT(dw->work.work_queue == wq); 212 KASSERTMSG((dw->dw_state == DELAYED_WORK_SCHEDULED || 213 dw->dw_state == DELAYED_WORK_RESCHEDULED || 214 dw->dw_state == DELAYED_WORK_CANCELLED), 215 "delayed work %p in bad state: %d", 216 dw, dw->dw_state); 217 218 /* 219 * Mark it cancelled and try to stop the callout before 220 * it starts. 221 * 222 * If it's too late and the callout has already begun 223 * to execute, then it will notice that we asked to 224 * cancel it and remove itself from the queue before 225 * returning. 226 * 227 * If we stopped the callout before it started, 228 * however, then we can safely destroy the callout and 229 * dissociate it from the workqueue ourselves. 230 */ 231 dw->dw_state = DELAYED_WORK_CANCELLED; 232 if (!callout_halt(&dw->dw_callout, &wq->wq_lock)) 233 cancel_delayed_work_done(wq, dw); 234 } 235 mutex_exit(&wq->wq_lock); 236 237 /* 238 * At this point, no new work can be put on the queue. 239 */ 240 241 /* Tell the thread to exit. */ 242 mutex_enter(&wq->wq_lock); 243 wq->wq_dying = true; 244 cv_broadcast(&wq->wq_cv); 245 mutex_exit(&wq->wq_lock); 246 247 /* Wait for it to exit. */ 248 (void)kthread_join(wq->wq_lwp); 249 250 KASSERT(wq->wq_dying); 251 KASSERT(!wq->wq_requeued); 252 KASSERT(wq->wq_flags == 0); 253 KASSERT(wq->wq_current_work == NULL); 254 KASSERT(TAILQ_EMPTY(&wq->wq_queue)); 255 KASSERT(TAILQ_EMPTY(&wq->wq_delayed)); 256 cv_destroy(&wq->wq_cv); 257 mutex_destroy(&wq->wq_lock); 258 259 kmem_free(wq, sizeof(*wq)); 260 } 261 262 /* 263 * Work thread and callout 264 */ 265 266 /* 267 * linux_workqueue_thread(cookie) 268 * 269 * Main function for a workqueue's worker thread. Waits until 270 * there is work queued, grabs a batch of work off the queue, 271 * executes it all, bumps the generation number, and repeats, 272 * until dying. 273 */ 274 static void __dead 275 linux_workqueue_thread(void *cookie) 276 { 277 struct workqueue_struct *const wq = cookie; 278 TAILQ_HEAD(, work_struct) tmp; 279 280 lwp_setspecific(workqueue_key, wq); 281 282 mutex_enter(&wq->wq_lock); 283 for (;;) { 284 /* 285 * Wait until there's activity. If there's no work and 286 * we're dying, stop here. 287 */ 288 while (TAILQ_EMPTY(&wq->wq_queue) && !wq->wq_dying) 289 cv_wait(&wq->wq_cv, &wq->wq_lock); 290 if (TAILQ_EMPTY(&wq->wq_queue)) { 291 KASSERT(wq->wq_dying); 292 break; 293 } 294 295 /* Grab a batch of work off the queue. */ 296 KASSERT(!TAILQ_EMPTY(&wq->wq_queue)); 297 TAILQ_INIT(&tmp); 298 TAILQ_CONCAT(&tmp, &wq->wq_queue, work_entry); 299 300 /* Process each work item in the batch. */ 301 while (!TAILQ_EMPTY(&tmp)) { 302 struct work_struct *const work = TAILQ_FIRST(&tmp); 303 304 KASSERT(work->work_queue == wq); 305 TAILQ_REMOVE(&tmp, work, work_entry); 306 KASSERT(wq->wq_current_work == NULL); 307 wq->wq_current_work = work; 308 309 mutex_exit(&wq->wq_lock); 310 (*work->func)(work); 311 mutex_enter(&wq->wq_lock); 312 313 KASSERT(wq->wq_current_work == work); 314 KASSERT(work->work_queue == wq); 315 if (wq->wq_requeued) 316 wq->wq_requeued = false; 317 else 318 release_work(work, wq); 319 wq->wq_current_work = NULL; 320 cv_broadcast(&wq->wq_cv); 321 } 322 323 /* Notify flush that we've completed a batch of work. */ 324 wq->wq_gen++; 325 cv_broadcast(&wq->wq_cv); 326 } 327 mutex_exit(&wq->wq_lock); 328 329 kthread_exit(0); 330 } 331 332 /* 333 * linux_workqueue_timeout(cookie) 334 * 335 * Delayed work timeout callback. 336 * 337 * - If scheduled, queue it. 338 * - If rescheduled, callout_schedule ourselves again. 339 * - If cancelled, destroy the callout and release the work from 340 * the workqueue. 341 */ 342 static void 343 linux_workqueue_timeout(void *cookie) 344 { 345 struct delayed_work *const dw = cookie; 346 struct workqueue_struct *const wq = dw->work.work_queue; 347 348 KASSERT(wq != NULL); 349 350 mutex_enter(&wq->wq_lock); 351 KASSERT(dw->work.work_queue == wq); 352 switch (dw->dw_state) { 353 case DELAYED_WORK_IDLE: 354 panic("delayed work callout uninitialized: %p", dw); 355 case DELAYED_WORK_SCHEDULED: 356 dw_callout_destroy(wq, dw); 357 TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, work_entry); 358 cv_broadcast(&wq->wq_cv); 359 break; 360 case DELAYED_WORK_RESCHEDULED: 361 KASSERT(dw->dw_resched >= 0); 362 callout_schedule(&dw->dw_callout, dw->dw_resched); 363 dw->dw_state = DELAYED_WORK_SCHEDULED; 364 dw->dw_resched = -1; 365 break; 366 case DELAYED_WORK_CANCELLED: 367 cancel_delayed_work_done(wq, dw); 368 /* Can't touch dw any more. */ 369 goto out; 370 default: 371 panic("delayed work callout in bad state: %p", dw); 372 } 373 KASSERT(dw->dw_state == DELAYED_WORK_IDLE || 374 dw->dw_state == DELAYED_WORK_SCHEDULED); 375 out: mutex_exit(&wq->wq_lock); 376 } 377 378 /* 379 * current_work() 380 * 381 * If in a workqueue worker thread, return the work it is 382 * currently executing. Otherwise return NULL. 383 */ 384 struct work_struct * 385 current_work(void) 386 { 387 struct workqueue_struct *wq = lwp_getspecific(workqueue_key); 388 389 /* If we're not a workqueue thread, then there's no work. */ 390 if (wq == NULL) 391 return NULL; 392 393 /* 394 * Otherwise, this should be possible only while work is in 395 * progress. Return the current work item. 396 */ 397 KASSERT(wq->wq_current_work != NULL); 398 return wq->wq_current_work; 399 } 400 401 /* 402 * Work 403 */ 404 405 /* 406 * INIT_WORK(work, fn) 407 * 408 * Initialize work for use with a workqueue to call fn in a worker 409 * thread. There is no corresponding destruction operation. 410 */ 411 void 412 INIT_WORK(struct work_struct *work, void (*fn)(struct work_struct *)) 413 { 414 415 work->work_queue = NULL; 416 work->func = fn; 417 } 418 419 /* 420 * acquire_work(work, wq) 421 * 422 * Try to associate work with wq. If work is already on a 423 * workqueue, return that workqueue. Otherwise, set work's queue 424 * to wq, issue a memory barrier to match any prior release_work, 425 * and return NULL. 426 * 427 * Caller must hold wq's lock. 428 */ 429 static struct workqueue_struct * 430 acquire_work(struct work_struct *work, struct workqueue_struct *wq) 431 { 432 struct workqueue_struct *wq0; 433 434 KASSERT(mutex_owned(&wq->wq_lock)); 435 436 wq0 = atomic_cas_ptr(&work->work_queue, NULL, wq); 437 if (wq0 == NULL) { 438 membar_enter(); 439 KASSERT(work->work_queue == wq); 440 } 441 442 return wq0; 443 } 444 445 /* 446 * release_work(work, wq) 447 * 448 * Issue a memory barrier to match any subsequent acquire_work and 449 * dissociate work from wq. 450 * 451 * Caller must hold wq's lock and work must be associated with wq. 452 */ 453 static void 454 release_work(struct work_struct *work, struct workqueue_struct *wq) 455 { 456 457 KASSERT(work->work_queue == wq); 458 KASSERT(mutex_owned(&wq->wq_lock)); 459 460 membar_exit(); 461 work->work_queue = NULL; 462 } 463 464 /* 465 * schedule_work(work) 466 * 467 * If work is not already queued on system_wq, queue it to be run 468 * by system_wq's worker thread when it next can. True if it was 469 * newly queued, false if it was already queued. If the work was 470 * already running, queue it to run again. 471 * 472 * Caller must ensure work is not queued to run on a different 473 * workqueue. 474 */ 475 bool 476 schedule_work(struct work_struct *work) 477 { 478 479 return queue_work(system_wq, work); 480 } 481 482 /* 483 * queue_work(wq, work) 484 * 485 * If work is not already queued on wq, queue it to be run by wq's 486 * worker thread when it next can. True if it was newly queued, 487 * false if it was already queued. If the work was already 488 * running, queue it to run again. 489 * 490 * Caller must ensure work is not queued to run on a different 491 * workqueue. 492 */ 493 bool 494 queue_work(struct workqueue_struct *wq, struct work_struct *work) 495 { 496 struct workqueue_struct *wq0; 497 bool newly_queued; 498 499 KASSERT(wq != NULL); 500 501 mutex_enter(&wq->wq_lock); 502 if (__predict_true((wq0 = acquire_work(work, wq)) == NULL)) { 503 /* 504 * It wasn't on any workqueue at all. Put it on this 505 * one, and signal the worker thread that there is work 506 * to do. 507 */ 508 TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry); 509 newly_queued = true; 510 cv_broadcast(&wq->wq_cv); 511 } else { 512 /* 513 * It was on a workqueue, which had better be this one. 514 * Requeue it if it has been taken off the queue to 515 * execute and hasn't been requeued yet. The worker 516 * thread should already be running, so no need to 517 * signal it. 518 */ 519 KASSERT(wq0 == wq); 520 if (wq->wq_current_work == work && !wq->wq_requeued) { 521 /* 522 * It has been taken off the queue to execute, 523 * and it hasn't been put back on the queue 524 * again. Put it back on the queue. No need 525 * to signal the worker thread because it will 526 * notice when it reacquires the lock after 527 * doing the work. 528 */ 529 TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry); 530 wq->wq_requeued = true; 531 newly_queued = true; 532 } else { 533 /* It is still on the queue; nothing to do. */ 534 newly_queued = false; 535 } 536 } 537 mutex_exit(&wq->wq_lock); 538 539 return newly_queued; 540 } 541 542 /* 543 * cancel_work(work) 544 * 545 * If work was queued, remove it from the queue and return true. 546 * If work was not queued, return false. Note that work may 547 * already be running; if it hasn't been requeued, then 548 * cancel_work will return false, and either way, cancel_work will 549 * NOT wait for the work to complete. 550 */ 551 bool 552 cancel_work(struct work_struct *work) 553 { 554 struct workqueue_struct *wq; 555 bool cancelled_p = false; 556 557 /* If there's no workqueue, nothing to cancel. */ 558 if ((wq = work->work_queue) == NULL) 559 goto out; 560 561 mutex_enter(&wq->wq_lock); 562 if (__predict_false(work->work_queue != wq)) { 563 /* 564 * It has finished execution or been cancelled by 565 * another thread, and has been moved off the 566 * workqueue, so it's too to cancel. 567 */ 568 cancelled_p = false; 569 } else if (wq->wq_current_work == work) { 570 /* 571 * It has already begun execution, so it's too late to 572 * cancel now. 573 */ 574 cancelled_p = false; 575 } else { 576 /* 577 * It is still on the queue. Take it off the queue and 578 * report successful cancellation. 579 */ 580 TAILQ_REMOVE(&wq->wq_queue, work, work_entry); 581 cancelled_p = true; 582 } 583 mutex_exit(&wq->wq_lock); 584 585 out: return cancelled_p; 586 } 587 588 /* 589 * cancel_work_sync(work) 590 * 591 * If work was queued, remove it from the queue and return true. 592 * If work was not queued, return false. Note that work may 593 * already be running; if it hasn't been requeued, then 594 * cancel_work will return false; either way, if work was 595 * currently running, wait for it to complete. 596 * 597 * May sleep. 598 */ 599 bool 600 cancel_work_sync(struct work_struct *work) 601 { 602 struct workqueue_struct *wq; 603 bool cancelled_p = false; 604 605 /* If there's no workqueue, nothing to cancel. */ 606 if ((wq = work->work_queue) == NULL) 607 goto out; 608 609 mutex_enter(&wq->wq_lock); 610 if (__predict_false(work->work_queue != wq)) { 611 /* 612 * It has finished execution or been cancelled by 613 * another thread, and has been moved off the 614 * workqueue, so it's too to cancel. 615 */ 616 cancelled_p = false; 617 } else if (wq->wq_current_work == work) { 618 /* 619 * It has already begun execution, so it's too late to 620 * cancel now. Wait for it to complete. 621 */ 622 wait_for_current_work(work, wq); 623 cancelled_p = false; 624 } else { 625 /* 626 * It is still on the queue. Take it off the queue and 627 * report successful cancellation. 628 */ 629 TAILQ_REMOVE(&wq->wq_queue, work, work_entry); 630 cancelled_p = true; 631 } 632 mutex_exit(&wq->wq_lock); 633 634 out: return cancelled_p; 635 } 636 637 /* 638 * wait_for_current_work(work, wq) 639 * 640 * wq must be currently executing work. Wait for it to finish. 641 */ 642 static void 643 wait_for_current_work(struct work_struct *work, struct workqueue_struct *wq) 644 { 645 uint64_t gen; 646 647 KASSERT(mutex_owned(&wq->wq_lock)); 648 KASSERT(work->work_queue == wq); 649 KASSERT(wq->wq_current_work == work); 650 651 /* Wait only one generation in case it gets requeued quickly. */ 652 gen = wq->wq_gen; 653 do { 654 cv_wait(&wq->wq_cv, &wq->wq_lock); 655 } while (wq->wq_current_work == work && wq->wq_gen == gen); 656 } 657 658 /* 659 * Delayed work 660 */ 661 662 /* 663 * INIT_DELAYED_WORK(dw, fn) 664 * 665 * Initialize dw for use with a workqueue to call fn in a worker 666 * thread after a delay. There is no corresponding destruction 667 * operation. 668 */ 669 void 670 INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work_struct *)) 671 { 672 673 INIT_WORK(&dw->work, fn); 674 dw->dw_state = DELAYED_WORK_IDLE; 675 dw->dw_resched = -1; 676 677 /* 678 * Defer callout_init until we are going to schedule the 679 * callout, which can then callout_destroy it, because 680 * otherwise since there's no DESTROY_DELAYED_WORK or anything 681 * we have no opportunity to call callout_destroy. 682 */ 683 } 684 685 /* 686 * schedule_delayed_work(dw, ticks) 687 * 688 * If it is not currently scheduled, schedule dw to run after 689 * ticks on system_wq. If currently executing and not already 690 * rescheduled, reschedule it. True if it was newly scheduled, 691 * false if it was already scheduled. 692 * 693 * If ticks == 0, queue it to run as soon as the worker can, 694 * without waiting for the next callout tick to run. 695 */ 696 bool 697 schedule_delayed_work(struct delayed_work *dw, unsigned long ticks) 698 { 699 700 return queue_delayed_work(system_wq, dw, ticks); 701 } 702 703 /* 704 * dw_callout_init(wq, dw) 705 * 706 * Initialize the callout of dw and transition to 707 * DELAYED_WORK_SCHEDULED. Caller must use callout_schedule. 708 */ 709 static void 710 dw_callout_init(struct workqueue_struct *wq, struct delayed_work *dw) 711 { 712 713 KASSERT(mutex_owned(&wq->wq_lock)); 714 KASSERT(dw->work.work_queue == wq); 715 KASSERT(dw->dw_state == DELAYED_WORK_IDLE); 716 717 callout_init(&dw->dw_callout, CALLOUT_MPSAFE); 718 callout_setfunc(&dw->dw_callout, &linux_workqueue_timeout, dw); 719 TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry); 720 dw->dw_state = DELAYED_WORK_SCHEDULED; 721 } 722 723 /* 724 * dw_callout_destroy(wq, dw) 725 * 726 * Destroy the callout of dw and transition to DELAYED_WORK_IDLE. 727 */ 728 static void 729 dw_callout_destroy(struct workqueue_struct *wq, struct delayed_work *dw) 730 { 731 732 KASSERT(mutex_owned(&wq->wq_lock)); 733 KASSERT(dw->work.work_queue == wq); 734 KASSERT(dw->dw_state == DELAYED_WORK_SCHEDULED || 735 dw->dw_state == DELAYED_WORK_RESCHEDULED || 736 dw->dw_state == DELAYED_WORK_CANCELLED); 737 738 TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry); 739 callout_destroy(&dw->dw_callout); 740 dw->dw_resched = -1; 741 dw->dw_state = DELAYED_WORK_IDLE; 742 } 743 744 /* 745 * cancel_delayed_work_done(wq, dw) 746 * 747 * Complete cancellation of a delayed work: transition from 748 * DELAYED_WORK_CANCELLED to DELAYED_WORK_IDLE and off the 749 * workqueue. Caller must not touch dw after this returns. 750 */ 751 static void 752 cancel_delayed_work_done(struct workqueue_struct *wq, struct delayed_work *dw) 753 { 754 755 KASSERT(mutex_owned(&wq->wq_lock)); 756 KASSERT(dw->work.work_queue == wq); 757 KASSERT(dw->dw_state == DELAYED_WORK_CANCELLED); 758 759 dw_callout_destroy(wq, dw); 760 release_work(&dw->work, wq); 761 /* Can't touch dw after this point. */ 762 } 763 764 /* 765 * queue_delayed_work(wq, dw, ticks) 766 * 767 * If it is not currently scheduled, schedule dw to run after 768 * ticks on wq. If currently executing and not already 769 * rescheduled, reschedule it. 770 * 771 * If ticks == 0, queue it to run as soon as the worker can, 772 * without waiting for the next callout tick to run. 773 */ 774 bool 775 queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw, 776 unsigned long ticks) 777 { 778 struct workqueue_struct *wq0; 779 bool newly_queued; 780 781 mutex_enter(&wq->wq_lock); 782 if (__predict_true((wq0 = acquire_work(&dw->work, wq)) == NULL)) { 783 /* 784 * It wasn't on any workqueue at all. Schedule it to 785 * run on this one. 786 */ 787 KASSERT(dw->dw_state == DELAYED_WORK_IDLE); 788 if (ticks == 0) { 789 TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, 790 work_entry); 791 cv_broadcast(&wq->wq_cv); 792 } else { 793 /* 794 * Initialize a callout and schedule to run 795 * after a delay. 796 */ 797 dw_callout_init(wq, dw); 798 callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks)); 799 } 800 newly_queued = true; 801 } else { 802 /* 803 * It was on a workqueue, which had better be this one. 804 * 805 * - If it has already begun to run, and it is not yet 806 * scheduled to run again, schedule it again. 807 * 808 * - If the callout is cancelled, reschedule it. 809 * 810 * - Otherwise, leave it alone. 811 */ 812 KASSERT(wq0 == wq); 813 if (wq->wq_current_work != &dw->work || !wq->wq_requeued) { 814 /* 815 * It is either scheduled, on the queue but not 816 * in progress, or in progress but not on the 817 * queue. 818 */ 819 switch (dw->dw_state) { 820 case DELAYED_WORK_IDLE: 821 /* 822 * It is not scheduled to run, and it 823 * is not on the queue if it is 824 * running. 825 */ 826 if (ticks == 0) { 827 /* 828 * If it's in progress, put it 829 * on the queue to run as soon 830 * as the worker thread gets to 831 * it. No need for a wakeup 832 * because either the worker 833 * thread already knows it is 834 * on the queue, or will check 835 * once it is done executing. 836 */ 837 if (wq->wq_current_work == &dw->work) { 838 KASSERT(!wq->wq_requeued); 839 TAILQ_INSERT_TAIL(&wq->wq_queue, 840 &dw->work, work_entry); 841 wq->wq_requeued = true; 842 } 843 } else { 844 /* 845 * Initialize a callout and 846 * schedule it to run after the 847 * specified delay. 848 */ 849 dw_callout_init(wq, dw); 850 callout_schedule(&dw->dw_callout, 851 MIN(INT_MAX, ticks)); 852 } 853 break; 854 case DELAYED_WORK_SCHEDULED: 855 case DELAYED_WORK_RESCHEDULED: 856 /* 857 * It is already scheduled to run after 858 * a delay. Leave it be. 859 */ 860 break; 861 case DELAYED_WORK_CANCELLED: 862 /* 863 * It was scheduled and the callout has 864 * begun to execute, but it was 865 * cancelled. Reschedule it. 866 */ 867 dw->dw_state = DELAYED_WORK_RESCHEDULED; 868 dw->dw_resched = MIN(INT_MAX, ticks); 869 break; 870 default: 871 panic("invalid delayed work state: %d", 872 dw->dw_state); 873 } 874 } else { 875 /* 876 * It is in progress and it has been requeued. 877 * It cannot be scheduled to run after a delay 878 * at this point. We just leave it be. 879 */ 880 KASSERTMSG((dw->dw_state == DELAYED_WORK_IDLE), 881 "delayed work %p in wrong state: %d", 882 dw, dw->dw_state); 883 } 884 } 885 mutex_exit(&wq->wq_lock); 886 887 return newly_queued; 888 } 889 890 /* 891 * mod_delayed_work(wq, dw, ticks) 892 * 893 * Schedule dw to run after ticks. If currently scheduled, 894 * reschedule it. If currently executing, reschedule it. If 895 * ticks == 0, run without delay. 896 * 897 * True if it modified the timer of an already scheduled work, 898 * false if it newly scheduled the work. 899 */ 900 bool 901 mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw, 902 unsigned long ticks) 903 { 904 struct workqueue_struct *wq0; 905 bool timer_modified; 906 907 mutex_enter(&wq->wq_lock); 908 if ((wq0 = acquire_work(&dw->work, wq)) == NULL) { 909 /* 910 * It wasn't on any workqueue at all. Schedule it to 911 * run on this one. 912 */ 913 KASSERT(dw->dw_state == DELAYED_WORK_IDLE); 914 if (ticks == 0) { 915 /* 916 * Run immediately: put it on the queue and 917 * signal the worker thread. 918 */ 919 TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, 920 work_entry); 921 cv_broadcast(&wq->wq_cv); 922 } else { 923 /* 924 * Initialize a callout and schedule to run 925 * after a delay. 926 */ 927 dw_callout_init(wq, dw); 928 callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks)); 929 } 930 timer_modified = false; 931 } else { 932 /* It was on a workqueue, which had better be this one. */ 933 KASSERT(wq0 == wq); 934 switch (dw->dw_state) { 935 case DELAYED_WORK_IDLE: 936 /* 937 * It is not scheduled: it is on the queue or 938 * it is running or both. 939 */ 940 if (wq->wq_current_work != &dw->work || 941 wq->wq_requeued) { 942 /* 943 * It is on the queue, and it may or 944 * may not be running. 945 */ 946 if (ticks == 0) { 947 /* 948 * We ask it to run 949 * immediately. Leave it on 950 * the queue. 951 */ 952 } else { 953 /* 954 * Take it off the queue and 955 * schedule a callout to run it 956 * after a delay. 957 */ 958 if (wq->wq_requeued) { 959 wq->wq_requeued = false; 960 } else { 961 KASSERT(wq->wq_current_work != 962 &dw->work); 963 } 964 TAILQ_REMOVE(&wq->wq_queue, &dw->work, 965 work_entry); 966 dw_callout_init(wq, dw); 967 callout_schedule(&dw->dw_callout, 968 MIN(INT_MAX, ticks)); 969 } 970 timer_modified = true; 971 } else { 972 /* 973 * It is currently running and has not 974 * been requeued. 975 */ 976 if (ticks == 0) { 977 /* 978 * We ask it to run 979 * immediately. Put it on the 980 * queue again. 981 */ 982 wq->wq_requeued = true; 983 TAILQ_INSERT_TAIL(&wq->wq_queue, 984 &dw->work, work_entry); 985 } else { 986 /* 987 * Schedule a callout to run it 988 * after a delay. 989 */ 990 dw_callout_init(wq, dw); 991 callout_schedule(&dw->dw_callout, 992 MIN(INT_MAX, ticks)); 993 } 994 timer_modified = false; 995 } 996 break; 997 case DELAYED_WORK_SCHEDULED: 998 /* 999 * It is scheduled to run after a delay. Try 1000 * to stop it and reschedule it; if we can't, 1001 * either reschedule it or cancel it to put it 1002 * on the queue, and inform the callout. 1003 */ 1004 if (callout_stop(&dw->dw_callout)) { 1005 /* Can't stop, callout has begun. */ 1006 if (ticks == 0) { 1007 /* 1008 * We don't actually need to do 1009 * anything. The callout will 1010 * queue it as soon as it gets 1011 * the lock. 1012 */ 1013 } else { 1014 /* Ask the callout to reschedule. */ 1015 dw->dw_state = DELAYED_WORK_RESCHEDULED; 1016 dw->dw_resched = MIN(INT_MAX, ticks); 1017 } 1018 } else { 1019 /* We stopped the callout before it began. */ 1020 if (ticks == 0) { 1021 /* 1022 * Run immediately: destroy the 1023 * callout, put it on the 1024 * queue, and signal the worker 1025 * thread. 1026 */ 1027 dw_callout_destroy(wq, dw); 1028 TAILQ_INSERT_TAIL(&wq->wq_queue, 1029 &dw->work, work_entry); 1030 cv_broadcast(&wq->wq_cv); 1031 } else { 1032 /* 1033 * Reschedule the callout. No 1034 * state change. 1035 */ 1036 callout_schedule(&dw->dw_callout, 1037 MIN(INT_MAX, ticks)); 1038 } 1039 } 1040 timer_modified = true; 1041 break; 1042 case DELAYED_WORK_RESCHEDULED: 1043 /* 1044 * Someone rescheduled it after the callout 1045 * started but before the poor thing even had a 1046 * chance to acquire the lock. 1047 */ 1048 if (ticks == 0) { 1049 /* 1050 * We can just switch back to 1051 * DELAYED_WORK_SCHEDULED so that the 1052 * callout will queue the work as soon 1053 * as it gets the lock. 1054 */ 1055 dw->dw_state = DELAYED_WORK_SCHEDULED; 1056 dw->dw_resched = -1; 1057 } else { 1058 /* Change the rescheduled time. */ 1059 dw->dw_resched = ticks; 1060 } 1061 timer_modified = true; 1062 break; 1063 case DELAYED_WORK_CANCELLED: 1064 /* 1065 * Someone cancelled it after the callout 1066 * started but before the poor thing even had a 1067 * chance to acquire the lock. 1068 */ 1069 if (ticks == 0) { 1070 /* 1071 * We can just switch back to 1072 * DELAYED_WORK_SCHEDULED so that the 1073 * callout will queue the work as soon 1074 * as it gets the lock. 1075 */ 1076 dw->dw_state = DELAYED_WORK_SCHEDULED; 1077 } else { 1078 /* Reschedule it. */ 1079 dw->dw_state = DELAYED_WORK_RESCHEDULED; 1080 dw->dw_resched = MIN(INT_MAX, ticks); 1081 } 1082 timer_modified = true; 1083 break; 1084 default: 1085 panic("invalid delayed work state: %d", dw->dw_state); 1086 } 1087 } 1088 mutex_exit(&wq->wq_lock); 1089 1090 return timer_modified; 1091 } 1092 1093 /* 1094 * cancel_delayed_work(dw) 1095 * 1096 * If work was scheduled or queued, remove it from the schedule or 1097 * queue and return true. If work was not scheduled or queued, 1098 * return false. Note that work may already be running; if it 1099 * hasn't been rescheduled or requeued, then cancel_delayed_work 1100 * will return false, and either way, cancel_delayed_work will NOT 1101 * wait for the work to complete. 1102 */ 1103 bool 1104 cancel_delayed_work(struct delayed_work *dw) 1105 { 1106 struct workqueue_struct *wq; 1107 bool cancelled_p; 1108 1109 /* If there's no workqueue, nothing to cancel. */ 1110 if ((wq = dw->work.work_queue) == NULL) 1111 return false; 1112 1113 mutex_enter(&wq->wq_lock); 1114 if (__predict_false(dw->work.work_queue != wq)) { 1115 cancelled_p = false; 1116 } else { 1117 switch (dw->dw_state) { 1118 case DELAYED_WORK_IDLE: 1119 /* 1120 * It is either on the queue or already running 1121 * or both. 1122 */ 1123 if (wq->wq_current_work != &dw->work || 1124 wq->wq_requeued) { 1125 /* 1126 * It is on the queue, and it may or 1127 * may not be running. Remove it from 1128 * the queue. 1129 */ 1130 TAILQ_REMOVE(&wq->wq_queue, &dw->work, 1131 work_entry); 1132 if (wq->wq_current_work == &dw->work) { 1133 /* 1134 * If it is running, then it 1135 * must have been requeued in 1136 * this case, so mark it no 1137 * longer requeued. 1138 */ 1139 KASSERT(wq->wq_requeued); 1140 wq->wq_requeued = false; 1141 } 1142 cancelled_p = true; 1143 } else { 1144 /* 1145 * Too late, it's already running, but 1146 * at least it hasn't been requeued. 1147 */ 1148 cancelled_p = false; 1149 } 1150 break; 1151 case DELAYED_WORK_SCHEDULED: 1152 /* 1153 * If it is scheduled, mark it cancelled and 1154 * try to stop the callout before it starts. 1155 * 1156 * If it's too late and the callout has already 1157 * begun to execute, tough. 1158 * 1159 * If we stopped the callout before it started, 1160 * however, then destroy the callout and 1161 * dissociate it from the workqueue ourselves. 1162 */ 1163 dw->dw_state = DELAYED_WORK_CANCELLED; 1164 cancelled_p = true; 1165 if (!callout_stop(&dw->dw_callout)) 1166 cancel_delayed_work_done(wq, dw); 1167 break; 1168 case DELAYED_WORK_RESCHEDULED: 1169 /* 1170 * If it is being rescheduled, the callout has 1171 * already fired. We must ask it to cancel. 1172 */ 1173 dw->dw_state = DELAYED_WORK_CANCELLED; 1174 dw->dw_resched = -1; 1175 cancelled_p = true; 1176 break; 1177 case DELAYED_WORK_CANCELLED: 1178 /* 1179 * If it is being cancelled, the callout has 1180 * already fired. There is nothing more for us 1181 * to do. Someone else claims credit for 1182 * cancelling it. 1183 */ 1184 cancelled_p = false; 1185 break; 1186 default: 1187 panic("invalid delayed work state: %d", 1188 dw->dw_state); 1189 } 1190 } 1191 mutex_exit(&wq->wq_lock); 1192 1193 return cancelled_p; 1194 } 1195 1196 /* 1197 * cancel_delayed_work_sync(dw) 1198 * 1199 * If work was scheduled or queued, remove it from the schedule or 1200 * queue and return true. If work was not scheduled or queued, 1201 * return false. Note that work may already be running; if it 1202 * hasn't been rescheduled or requeued, then cancel_delayed_work 1203 * will return false; either way, wait for it to complete. 1204 */ 1205 bool 1206 cancel_delayed_work_sync(struct delayed_work *dw) 1207 { 1208 struct workqueue_struct *wq; 1209 bool cancelled_p; 1210 1211 /* If there's no workqueue, nothing to cancel. */ 1212 if ((wq = dw->work.work_queue) == NULL) 1213 return false; 1214 1215 mutex_enter(&wq->wq_lock); 1216 if (__predict_false(dw->work.work_queue != wq)) { 1217 cancelled_p = false; 1218 } else { 1219 switch (dw->dw_state) { 1220 case DELAYED_WORK_IDLE: 1221 /* 1222 * It is either on the queue or already running 1223 * or both. 1224 */ 1225 if (wq->wq_current_work == &dw->work) { 1226 /* 1227 * Too late, it's already running. 1228 * First, make sure it's not requeued. 1229 * Then wait for it to complete. 1230 */ 1231 if (wq->wq_requeued) { 1232 TAILQ_REMOVE(&wq->wq_queue, &dw->work, 1233 work_entry); 1234 wq->wq_requeued = false; 1235 } 1236 wait_for_current_work(&dw->work, wq); 1237 cancelled_p = false; 1238 } else { 1239 /* Got in before it started. Remove it. */ 1240 TAILQ_REMOVE(&wq->wq_queue, &dw->work, 1241 work_entry); 1242 cancelled_p = true; 1243 } 1244 break; 1245 case DELAYED_WORK_SCHEDULED: 1246 /* 1247 * If it is scheduled, mark it cancelled and 1248 * try to stop the callout before it starts. 1249 * 1250 * If it's too late and the callout has already 1251 * begun to execute, we must wait for it to 1252 * complete. But we got in soon enough to ask 1253 * the callout not to run, so we successfully 1254 * cancelled it in that case. 1255 * 1256 * If we stopped the callout before it started, 1257 * then we must destroy the callout and 1258 * dissociate it from the workqueue ourselves. 1259 */ 1260 dw->dw_state = DELAYED_WORK_CANCELLED; 1261 if (!callout_halt(&dw->dw_callout, &wq->wq_lock)) 1262 cancel_delayed_work_done(wq, dw); 1263 cancelled_p = true; 1264 break; 1265 case DELAYED_WORK_RESCHEDULED: 1266 /* 1267 * If it is being rescheduled, the callout has 1268 * already fired. We must ask it to cancel and 1269 * wait for it to complete. 1270 */ 1271 dw->dw_state = DELAYED_WORK_CANCELLED; 1272 dw->dw_resched = -1; 1273 (void)callout_halt(&dw->dw_callout, &wq->wq_lock); 1274 cancelled_p = true; 1275 break; 1276 case DELAYED_WORK_CANCELLED: 1277 /* 1278 * If it is being cancelled, the callout has 1279 * already fired. We need only wait for it to 1280 * complete. Someone else, however, claims 1281 * credit for cancelling it. 1282 */ 1283 (void)callout_halt(&dw->dw_callout, &wq->wq_lock); 1284 cancelled_p = false; 1285 break; 1286 default: 1287 panic("invalid delayed work state: %d", 1288 dw->dw_state); 1289 } 1290 } 1291 mutex_exit(&wq->wq_lock); 1292 1293 return cancelled_p; 1294 } 1295 1296 /* 1297 * Flush 1298 */ 1299 1300 /* 1301 * flush_scheduled_work() 1302 * 1303 * Wait for all work queued on system_wq to complete. This does 1304 * not include delayed work. 1305 */ 1306 void 1307 flush_scheduled_work(void) 1308 { 1309 1310 flush_workqueue(system_wq); 1311 } 1312 1313 /* 1314 * flush_workqueue_locked(wq) 1315 * 1316 * Wait for all work queued on wq to complete. This does not 1317 * include delayed work. 1318 * 1319 * Caller must hold wq's lock. 1320 */ 1321 static void 1322 flush_workqueue_locked(struct workqueue_struct *wq) 1323 { 1324 uint64_t gen; 1325 1326 KASSERT(mutex_owned(&wq->wq_lock)); 1327 1328 /* Get the current generation number. */ 1329 gen = wq->wq_gen; 1330 1331 /* 1332 * If there's a batch of work in progress, we must wait for the 1333 * worker thread to finish that batch. 1334 */ 1335 if (wq->wq_current_work != NULL) 1336 gen++; 1337 1338 /* 1339 * If there's any work yet to be claimed from the queue by the 1340 * worker thread, we must wait for it to finish one more batch 1341 * too. 1342 */ 1343 if (!TAILQ_EMPTY(&wq->wq_queue)) 1344 gen++; 1345 1346 /* Wait until the generation number has caught up. */ 1347 while (wq->wq_gen < gen) 1348 cv_wait(&wq->wq_cv, &wq->wq_lock); 1349 } 1350 1351 /* 1352 * flush_workqueue(wq) 1353 * 1354 * Wait for all work queued on wq to complete. This does not 1355 * include delayed work. 1356 */ 1357 void 1358 flush_workqueue(struct workqueue_struct *wq) 1359 { 1360 1361 mutex_enter(&wq->wq_lock); 1362 flush_workqueue_locked(wq); 1363 mutex_exit(&wq->wq_lock); 1364 } 1365 1366 /* 1367 * flush_work(work) 1368 * 1369 * If work is queued or currently executing, wait for it to 1370 * complete. 1371 */ 1372 void 1373 flush_work(struct work_struct *work) 1374 { 1375 struct workqueue_struct *wq; 1376 1377 /* If there's no workqueue, nothing to flush. */ 1378 if ((wq = work->work_queue) == NULL) 1379 return; 1380 1381 flush_workqueue(wq); 1382 } 1383 1384 /* 1385 * flush_delayed_work(dw) 1386 * 1387 * If dw is scheduled to run after a delay, queue it immediately 1388 * instead. Then, if dw is queued or currently executing, wait 1389 * for it to complete. 1390 */ 1391 void 1392 flush_delayed_work(struct delayed_work *dw) 1393 { 1394 struct workqueue_struct *wq; 1395 1396 /* If there's no workqueue, nothing to flush. */ 1397 if ((wq = dw->work.work_queue) == NULL) 1398 return; 1399 1400 mutex_enter(&wq->wq_lock); 1401 if (__predict_false(dw->work.work_queue != wq)) { 1402 /* 1403 * Moved off the queue already (and possibly to another 1404 * queue, though that would be ill-advised), so it must 1405 * have completed, and we have nothing more to do. 1406 */ 1407 } else { 1408 switch (dw->dw_state) { 1409 case DELAYED_WORK_IDLE: 1410 /* 1411 * It has a workqueue assigned and the callout 1412 * is idle, so it must be in progress or on the 1413 * queue. In that case, we'll wait for it to 1414 * complete. 1415 */ 1416 break; 1417 case DELAYED_WORK_SCHEDULED: 1418 case DELAYED_WORK_RESCHEDULED: 1419 case DELAYED_WORK_CANCELLED: 1420 /* 1421 * The callout is scheduled, and may have even 1422 * started. Mark it as scheduled so that if 1423 * the callout has fired it will queue the work 1424 * itself. Try to stop the callout -- if we 1425 * can, queue the work now; if we can't, wait 1426 * for the callout to complete, which entails 1427 * queueing it. 1428 */ 1429 dw->dw_state = DELAYED_WORK_SCHEDULED; 1430 if (!callout_halt(&dw->dw_callout, &wq->wq_lock)) { 1431 /* 1432 * We stopped it before it ran. No 1433 * state change in the interim is 1434 * possible. Destroy the callout and 1435 * queue it ourselves. 1436 */ 1437 KASSERT(dw->dw_state == 1438 DELAYED_WORK_SCHEDULED); 1439 dw_callout_destroy(wq, dw); 1440 TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, 1441 work_entry); 1442 cv_broadcast(&wq->wq_cv); 1443 } 1444 break; 1445 default: 1446 panic("invalid delayed work state: %d", dw->dw_state); 1447 } 1448 /* 1449 * Waiting for the whole queue to flush is overkill, 1450 * but doesn't hurt. 1451 */ 1452 flush_workqueue_locked(wq); 1453 } 1454 mutex_exit(&wq->wq_lock); 1455 } 1456