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