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