Home | History | Annotate | Line # | Download | only in linux
linux_work.c revision 1.30
      1   1.2  riastrad /*	$NetBSD: linux_work.c,v 1.30 2018/08/27 15:03:45 riastradh Exp $	*/
      2   1.1     skrll 
      3   1.1     skrll /*-
      4  1.12  riastrad  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5   1.1     skrll  * All rights reserved.
      6   1.1     skrll  *
      7   1.1     skrll  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     skrll  * by Taylor R. Campbell.
      9   1.1     skrll  *
     10   1.1     skrll  * Redistribution and use in source and binary forms, with or without
     11   1.1     skrll  * modification, are permitted provided that the following conditions
     12   1.1     skrll  * are met:
     13   1.1     skrll  * 1. Redistributions of source code must retain the above copyright
     14   1.1     skrll  *    notice, this list of conditions and the following disclaimer.
     15   1.1     skrll  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     skrll  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     skrll  *    documentation and/or other materials provided with the distribution.
     18   1.1     skrll  *
     19   1.1     skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     skrll  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     skrll  */
     31   1.1     skrll 
     32   1.1     skrll #include <sys/cdefs.h>
     33   1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: linux_work.c,v 1.30 2018/08/27 15:03:45 riastradh Exp $");
     34   1.1     skrll 
     35   1.1     skrll #include <sys/types.h>
     36   1.1     skrll #include <sys/atomic.h>
     37   1.1     skrll #include <sys/callout.h>
     38   1.1     skrll #include <sys/condvar.h>
     39   1.1     skrll #include <sys/errno.h>
     40   1.1     skrll #include <sys/kmem.h>
     41  1.12  riastrad #include <sys/kthread.h>
     42  1.12  riastrad #include <sys/lwp.h>
     43   1.1     skrll #include <sys/mutex.h>
     44   1.1     skrll #include <sys/queue.h>
     45   1.1     skrll 
     46   1.1     skrll #include <linux/workqueue.h>
     47   1.1     skrll 
     48   1.1     skrll struct workqueue_struct {
     49   1.1     skrll 	kmutex_t			wq_lock;
     50   1.1     skrll 	kcondvar_t			wq_cv;
     51   1.1     skrll 	TAILQ_HEAD(, delayed_work)	wq_delayed;
     52  1.12  riastrad 	TAILQ_HEAD(, work_struct)	wq_queue;
     53   1.1     skrll 	struct work_struct		*wq_current_work;
     54  1.12  riastrad 	int				wq_flags;
     55  1.12  riastrad 	struct lwp			*wq_lwp;
     56  1.12  riastrad 	uint64_t			wq_gen;
     57  1.12  riastrad 	bool				wq_requeued:1;
     58  1.12  riastrad 	bool				wq_dying:1;
     59   1.1     skrll };
     60   1.1     skrll 
     61  1.12  riastrad static void __dead	linux_workqueue_thread(void *);
     62  1.12  riastrad static void		linux_workqueue_timeout(void *);
     63  1.17  riastrad static struct workqueue_struct *
     64  1.17  riastrad 			acquire_work(struct work_struct *,
     65  1.17  riastrad 			    struct workqueue_struct *);
     66  1.17  riastrad static void		release_work(struct work_struct *,
     67  1.17  riastrad 			    struct workqueue_struct *);
     68  1.30  riastrad static void		dw_callout_init(struct workqueue_struct *,
     69  1.30  riastrad 			    struct delayed_work *);
     70  1.23  riastrad static void		cancel_delayed_work_done(struct workqueue_struct *,
     71  1.23  riastrad 			    struct delayed_work *);
     72  1.12  riastrad 
     73  1.12  riastrad static specificdata_key_t workqueue_key __read_mostly;
     74  1.12  riastrad 
     75  1.12  riastrad struct workqueue_struct	*system_wq __read_mostly;
     76  1.12  riastrad struct workqueue_struct	*system_long_wq __read_mostly;
     77  1.12  riastrad struct workqueue_struct	*system_power_efficient_wq __read_mostly;
     78   1.3  riastrad 
     79   1.1     skrll int
     80   1.1     skrll linux_workqueue_init(void)
     81   1.1     skrll {
     82  1.12  riastrad 	int error;
     83   1.3  riastrad 
     84  1.12  riastrad 	error = lwp_specific_key_create(&workqueue_key, NULL);
     85  1.12  riastrad 	if (error)
     86  1.12  riastrad 		goto fail0;
     87   1.1     skrll 
     88   1.1     skrll 	system_wq = alloc_ordered_workqueue("lnxsyswq", 0);
     89  1.12  riastrad 	if (system_wq == NULL) {
     90  1.12  riastrad 		error = ENOMEM;
     91  1.12  riastrad 		goto fail1;
     92  1.12  riastrad 	}
     93   1.2  riastrad 
     94   1.2  riastrad 	system_long_wq = alloc_ordered_workqueue("lnxlngwq", 0);
     95  1.12  riastrad 	if (system_long_wq == NULL) {
     96  1.12  riastrad 		error = ENOMEM;
     97  1.12  riastrad 		goto fail2;
     98  1.12  riastrad 	}
     99   1.1     skrll 
    100   1.6  riastrad 	system_power_efficient_wq = alloc_ordered_workqueue("lnxpwrwq", 0);
    101  1.12  riastrad 	if (system_long_wq == NULL) {
    102  1.12  riastrad 		error = ENOMEM;
    103  1.12  riastrad 		goto fail3;
    104  1.12  riastrad 	}
    105   1.6  riastrad 
    106   1.1     skrll 	return 0;
    107   1.2  riastrad 
    108  1.12  riastrad fail4: __unused
    109   1.6  riastrad 	destroy_workqueue(system_power_efficient_wq);
    110  1.12  riastrad fail3:	destroy_workqueue(system_long_wq);
    111  1.12  riastrad fail2:	destroy_workqueue(system_wq);
    112  1.12  riastrad fail1:	lwp_specific_key_delete(workqueue_key);
    113  1.12  riastrad fail0:	KASSERT(error);
    114  1.12  riastrad 	return error;
    115   1.1     skrll }
    116   1.1     skrll 
    117   1.1     skrll void
    118   1.1     skrll linux_workqueue_fini(void)
    119   1.1     skrll {
    120   1.2  riastrad 
    121  1.12  riastrad 	destroy_workqueue(system_power_efficient_wq);
    122   1.2  riastrad 	destroy_workqueue(system_long_wq);
    123   1.1     skrll 	destroy_workqueue(system_wq);
    124  1.12  riastrad 	lwp_specific_key_delete(workqueue_key);
    125   1.1     skrll }
    126   1.1     skrll 
    127   1.1     skrll /*
    129   1.1     skrll  * Workqueues
    130   1.1     skrll  */
    131   1.1     skrll 
    132  1.12  riastrad struct workqueue_struct *
    133   1.1     skrll alloc_ordered_workqueue(const char *name, int flags)
    134   1.1     skrll {
    135   1.1     skrll 	struct workqueue_struct *wq;
    136   1.1     skrll 	int error;
    137  1.12  riastrad 
    138   1.1     skrll 	KASSERT(flags == 0);
    139  1.25  riastrad 
    140   1.1     skrll 	wq = kmem_zalloc(sizeof(*wq), KM_SLEEP);
    141  1.12  riastrad 
    142   1.1     skrll 	mutex_init(&wq->wq_lock, MUTEX_DEFAULT, IPL_NONE);
    143   1.1     skrll 	cv_init(&wq->wq_cv, name);
    144  1.12  riastrad 	TAILQ_INIT(&wq->wq_delayed);
    145   1.1     skrll 	TAILQ_INIT(&wq->wq_queue);
    146  1.25  riastrad 	wq->wq_current_work = NULL;
    147  1.25  riastrad 	wq->wq_flags = 0;
    148  1.25  riastrad 	wq->wq_lwp = NULL;
    149  1.25  riastrad 	wq->wq_gen = 0;
    150  1.25  riastrad 	wq->wq_requeued = false;
    151   1.1     skrll 	wq->wq_dying = false;
    152  1.12  riastrad 
    153  1.12  riastrad 	error = kthread_create(PRI_NONE,
    154  1.12  riastrad 	    KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL,
    155  1.12  riastrad 	    &linux_workqueue_thread, wq, &wq->wq_lwp, "%s", name);
    156  1.12  riastrad 	if (error)
    157   1.3  riastrad 		goto fail0;
    158   1.1     skrll 
    159  1.12  riastrad 	return wq;
    160  1.12  riastrad 
    161  1.12  riastrad fail0:	KASSERT(TAILQ_EMPTY(&wq->wq_queue));
    162  1.12  riastrad 	KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
    163  1.12  riastrad 	cv_destroy(&wq->wq_cv);
    164  1.12  riastrad 	mutex_destroy(&wq->wq_lock);
    165  1.12  riastrad 	kmem_free(wq, sizeof(*wq));
    166   1.1     skrll 	return NULL;
    167   1.1     skrll }
    168   1.1     skrll 
    169   1.1     skrll void
    170   1.1     skrll destroy_workqueue(struct workqueue_struct *wq)
    171   1.1     skrll {
    172   1.1     skrll 
    173  1.12  riastrad 	/*
    174  1.12  riastrad 	 * Cancel all delayed work.  We do this first because any
    175  1.12  riastrad 	 * delayed work that that has already timed out, which we can't
    176   1.1     skrll 	 * cancel, may have queued new work.
    177  1.26  riastrad 	 */
    178  1.26  riastrad 	mutex_enter(&wq->wq_lock);
    179  1.26  riastrad 	while (!TAILQ_EMPTY(&wq->wq_delayed)) {
    180   1.1     skrll 		struct delayed_work *const dw = TAILQ_FIRST(&wq->wq_delayed);
    181  1.26  riastrad 
    182  1.26  riastrad 		KASSERT(dw->work.work_queue == wq);
    183  1.26  riastrad 		KASSERTMSG((dw->dw_state == DELAYED_WORK_SCHEDULED ||
    184  1.26  riastrad 			dw->dw_state == DELAYED_WORK_RESCHEDULED ||
    185  1.26  riastrad 			dw->dw_state == DELAYED_WORK_CANCELLED),
    186  1.26  riastrad 		    "delayed work %p in bad state: %d",
    187  1.26  riastrad 		    dw, dw->dw_state);
    188  1.26  riastrad 
    189  1.26  riastrad 		/*
    190  1.26  riastrad 		 * Mark it cancelled and try to stop the callout before
    191  1.26  riastrad 		 * it starts.
    192  1.26  riastrad 		 *
    193  1.26  riastrad 		 * If it's too late and the callout has already begun
    194  1.26  riastrad 		 * to execute, then it will notice that we asked to
    195  1.26  riastrad 		 * cancel it and remove itself from the queue before
    196  1.26  riastrad 		 * returning.
    197  1.26  riastrad 		 *
    198  1.26  riastrad 		 * If we stopped the callout before it started,
    199  1.26  riastrad 		 * however, then we can safely destroy the callout and
    200  1.26  riastrad 		 * dissociate it from the workqueue ourselves.
    201  1.26  riastrad 		 */
    202  1.26  riastrad 		dw->dw_state = DELAYED_WORK_CANCELLED;
    203  1.26  riastrad 		if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
    204  1.26  riastrad 			cancel_delayed_work_done(wq, dw);
    205  1.26  riastrad 	}
    206   1.1     skrll 	mutex_exit(&wq->wq_lock);
    207  1.26  riastrad 
    208  1.26  riastrad 	/*
    209  1.26  riastrad 	 * At this point, no new work can be put on the queue.
    210   1.1     skrll 	 */
    211  1.12  riastrad 
    212  1.12  riastrad 	/* Tell the thread to exit.  */
    213  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    214  1.12  riastrad 	wq->wq_dying = true;
    215  1.12  riastrad 	cv_broadcast(&wq->wq_cv);
    216  1.12  riastrad 	mutex_exit(&wq->wq_lock);
    217  1.12  riastrad 
    218  1.12  riastrad 	/* Wait for it to exit.  */
    219  1.12  riastrad 	(void)kthread_join(wq->wq_lwp);
    220  1.25  riastrad 
    221  1.25  riastrad 	KASSERT(wq->wq_dying);
    222  1.25  riastrad 	KASSERT(!wq->wq_requeued);
    223   1.1     skrll 	KASSERT(wq->wq_flags == 0);
    224  1.12  riastrad 	KASSERT(wq->wq_current_work == NULL);
    225  1.12  riastrad 	KASSERT(TAILQ_EMPTY(&wq->wq_queue));
    226   1.1     skrll 	KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
    227   1.1     skrll 	cv_destroy(&wq->wq_cv);
    228   1.1     skrll 	mutex_destroy(&wq->wq_lock);
    229   1.1     skrll 
    230   1.1     skrll 	kmem_free(wq, sizeof(*wq));
    231   1.1     skrll }
    232   1.1     skrll 
    233  1.12  riastrad /*
    235   1.1     skrll  * Work thread and callout
    236  1.12  riastrad  */
    237  1.12  riastrad 
    238   1.1     skrll static void __dead
    239  1.12  riastrad linux_workqueue_thread(void *cookie)
    240  1.12  riastrad {
    241   1.1     skrll 	struct workqueue_struct *const wq = cookie;
    242  1.12  riastrad 	TAILQ_HEAD(, work_struct) tmp;
    243   1.1     skrll 
    244  1.12  riastrad 	lwp_setspecific(workqueue_key, wq);
    245  1.12  riastrad 
    246  1.26  riastrad 	mutex_enter(&wq->wq_lock);
    247  1.26  riastrad 	for (;;) {
    248  1.26  riastrad 		/*
    249  1.26  riastrad 		 * Wait until there's activity.  If there's no work and
    250  1.12  riastrad 		 * we're dying, stop here.
    251  1.12  riastrad 		 */
    252  1.26  riastrad 		while (TAILQ_EMPTY(&wq->wq_queue) && !wq->wq_dying)
    253  1.26  riastrad 			cv_wait(&wq->wq_cv, &wq->wq_lock);
    254  1.12  riastrad 		if (TAILQ_EMPTY(&wq->wq_queue)) {
    255  1.26  riastrad 			KASSERT(wq->wq_dying);
    256   1.1     skrll 			break;
    257  1.12  riastrad 		}
    258  1.12  riastrad 
    259  1.12  riastrad 		/* Grab a batch of work off the queue.  */
    260  1.12  riastrad 		KASSERT(!TAILQ_EMPTY(&wq->wq_queue));
    261  1.12  riastrad 		TAILQ_INIT(&tmp);
    262  1.12  riastrad 		TAILQ_CONCAT(&tmp, &wq->wq_queue, work_entry);
    263  1.12  riastrad 
    264  1.12  riastrad 		/* Process each work item in the batch.  */
    265  1.12  riastrad 		while (!TAILQ_EMPTY(&tmp)) {
    266  1.18  riastrad 			struct work_struct *const work = TAILQ_FIRST(&tmp);
    267  1.12  riastrad 
    268  1.12  riastrad 			KASSERT(work->work_queue == wq);
    269  1.12  riastrad 			TAILQ_REMOVE(&tmp, work, work_entry);
    270   1.1     skrll 			KASSERT(wq->wq_current_work == NULL);
    271  1.12  riastrad 			wq->wq_current_work = work;
    272  1.12  riastrad 
    273  1.12  riastrad 			mutex_exit(&wq->wq_lock);
    274   1.1     skrll 			(*work->func)(work);
    275  1.12  riastrad 			mutex_enter(&wq->wq_lock);
    276  1.12  riastrad 
    277  1.12  riastrad 			KASSERT(wq->wq_current_work == work);
    278  1.12  riastrad 			KASSERT(work->work_queue == wq);
    279  1.12  riastrad 			if (wq->wq_requeued)
    280  1.17  riastrad 				wq->wq_requeued = false;
    281  1.12  riastrad 			else
    282  1.12  riastrad 				release_work(work, wq);
    283  1.12  riastrad 			wq->wq_current_work = NULL;
    284   1.1     skrll 			cv_broadcast(&wq->wq_cv);
    285  1.12  riastrad 		}
    286  1.12  riastrad 
    287  1.12  riastrad 		/* Notify flush that we've completed a batch of work.  */
    288   1.1     skrll 		wq->wq_gen++;
    289  1.12  riastrad 		cv_broadcast(&wq->wq_cv);
    290   1.1     skrll 	}
    291  1.12  riastrad 	mutex_exit(&wq->wq_lock);
    292   1.1     skrll 
    293   1.1     skrll 	kthread_exit(0);
    294   1.1     skrll }
    295  1.12  riastrad 
    296   1.1     skrll static void
    297  1.12  riastrad linux_workqueue_timeout(void *cookie)
    298  1.12  riastrad {
    299   1.1     skrll 	struct delayed_work *const dw = cookie;
    300  1.12  riastrad 	struct workqueue_struct *const wq = dw->work.work_queue;
    301  1.14  riastrad 
    302  1.12  riastrad 	KASSERT(wq != NULL);
    303  1.18  riastrad 
    304  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    305  1.12  riastrad 	KASSERT(dw->work.work_queue == wq);
    306  1.12  riastrad 	switch (dw->dw_state) {
    307  1.12  riastrad 	case DELAYED_WORK_IDLE:
    308  1.12  riastrad 		panic("delayed work callout uninitialized: %p", dw);
    309  1.12  riastrad 	case DELAYED_WORK_SCHEDULED:
    310  1.12  riastrad 		dw->dw_state = DELAYED_WORK_IDLE;
    311  1.12  riastrad 		callout_destroy(&dw->dw_callout);
    312  1.12  riastrad 		TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
    313  1.12  riastrad 		TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, work_entry);
    314  1.12  riastrad 		cv_broadcast(&wq->wq_cv);
    315  1.12  riastrad 		break;
    316  1.12  riastrad 	case DELAYED_WORK_RESCHEDULED:
    317  1.12  riastrad 		dw->dw_state = DELAYED_WORK_SCHEDULED;
    318  1.23  riastrad 		break;
    319  1.22  riastrad 	case DELAYED_WORK_CANCELLED:
    320  1.22  riastrad 		cancel_delayed_work_done(wq, dw);
    321  1.12  riastrad 		/* Can't touch dw any more.  */
    322  1.12  riastrad 		goto out;
    323  1.12  riastrad 	default:
    324  1.15  riastrad 		panic("delayed work callout in bad state: %p", dw);
    325  1.15  riastrad 	}
    326  1.22  riastrad 	KASSERT(dw->dw_state == DELAYED_WORK_IDLE ||
    327   1.1     skrll 	    dw->dw_state == DELAYED_WORK_SCHEDULED);
    328   1.1     skrll out:	mutex_exit(&wq->wq_lock);
    329  1.12  riastrad }
    330  1.12  riastrad 
    331   1.1     skrll struct work_struct *
    332  1.12  riastrad current_work(void)
    333   1.1     skrll {
    334  1.12  riastrad 	struct workqueue_struct *wq = lwp_getspecific(workqueue_key);
    335  1.12  riastrad 
    336  1.12  riastrad 	/* If we're not a workqueue thread, then there's no work.  */
    337   1.1     skrll 	if (wq == NULL)
    338  1.12  riastrad 		return NULL;
    339  1.12  riastrad 
    340  1.12  riastrad 	/*
    341  1.12  riastrad 	 * Otherwise, this should be possible only while work is in
    342  1.12  riastrad 	 * progress.  Return the current work item.
    343  1.12  riastrad 	 */
    344   1.1     skrll 	KASSERT(wq->wq_current_work != NULL);
    345   1.1     skrll 	return wq->wq_current_work;
    346   1.1     skrll }
    347   1.1     skrll 
    348   1.1     skrll /*
    350   1.1     skrll  * Work
    351   1.1     skrll  */
    352   1.1     skrll 
    353   1.1     skrll void
    354  1.12  riastrad INIT_WORK(struct work_struct *work, void (*fn)(struct work_struct *))
    355   1.4  riastrad {
    356   1.1     skrll 
    357   1.1     skrll 	work->work_queue = NULL;
    358  1.17  riastrad 	work->func = fn;
    359  1.17  riastrad }
    360  1.17  riastrad 
    361  1.17  riastrad static struct workqueue_struct *
    362  1.17  riastrad acquire_work(struct work_struct *work, struct workqueue_struct *wq)
    363  1.17  riastrad {
    364  1.17  riastrad 	struct workqueue_struct *wq0;
    365  1.17  riastrad 
    366  1.17  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    367  1.17  riastrad 
    368  1.17  riastrad 	wq0 = atomic_cas_ptr(&work->work_queue, NULL, wq);
    369  1.17  riastrad 	if (wq0 == NULL) {
    370  1.17  riastrad 		membar_enter();
    371  1.17  riastrad 		KASSERT(work->work_queue == wq);
    372  1.17  riastrad 	}
    373  1.17  riastrad 
    374  1.17  riastrad 	return wq0;
    375  1.17  riastrad }
    376  1.17  riastrad 
    377  1.17  riastrad static void
    378  1.17  riastrad release_work(struct work_struct *work, struct workqueue_struct *wq)
    379  1.17  riastrad {
    380  1.17  riastrad 
    381  1.17  riastrad 	KASSERT(work->work_queue == wq);
    382  1.17  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    383  1.17  riastrad 
    384  1.17  riastrad 	membar_exit();
    385   1.1     skrll 	work->work_queue = NULL;
    386   1.1     skrll }
    387   1.1     skrll 
    388  1.12  riastrad bool
    389   1.1     skrll schedule_work(struct work_struct *work)
    390   1.1     skrll {
    391   1.1     skrll 
    392   1.1     skrll 	return queue_work(system_wq, work);
    393   1.1     skrll }
    394   1.1     skrll 
    395  1.12  riastrad bool
    396   1.1     skrll queue_work(struct workqueue_struct *wq, struct work_struct *work)
    397   1.1     skrll {
    398   1.1     skrll 	struct workqueue_struct *wq0;
    399   1.1     skrll 	bool newly_queued;
    400  1.12  riastrad 
    401  1.17  riastrad 	KASSERT(wq != NULL);
    402  1.29  riastrad 
    403  1.29  riastrad 	mutex_enter(&wq->wq_lock);
    404  1.29  riastrad 	if (__predict_true((wq0 = acquire_work(work, wq)) == NULL)) {
    405  1.29  riastrad 		/*
    406  1.29  riastrad 		 * It wasn't on any workqueue at all.  Put it on this
    407  1.12  riastrad 		 * one, and signal the worker thread that there is work
    408   1.1     skrll 		 * to do.
    409  1.29  riastrad 		 */
    410  1.12  riastrad 		TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry);
    411  1.29  riastrad 		newly_queued = true;
    412  1.29  riastrad 		cv_broadcast(&wq->wq_cv);
    413  1.29  riastrad 	} else {
    414  1.29  riastrad 		/*
    415  1.29  riastrad 		 * It was on a workqueue, which had better be this one.
    416  1.29  riastrad 		 * Requeue it if it has been taken off the queue to
    417  1.29  riastrad 		 * execute and hasn't been requeued yet.  The worker
    418  1.12  riastrad 		 * thread should already be running, so no need to
    419  1.29  riastrad 		 * signal it.
    420  1.29  riastrad 		 */
    421  1.29  riastrad 		KASSERT(wq0 == wq);
    422  1.29  riastrad 		if (wq->wq_current_work == work && !wq->wq_requeued) {
    423  1.29  riastrad 			/*
    424  1.29  riastrad 			 * It has been taken off the queue to execute,
    425  1.29  riastrad 			 * and it hasn't been put back on the queue
    426  1.29  riastrad 			 * again.  Put it back on the queue.  No need
    427  1.29  riastrad 			 * to signal the worker thread because it will
    428  1.29  riastrad 			 * notice when it reacquires the lock after
    429  1.29  riastrad 			 * doing the work.
    430  1.29  riastrad 			 */
    431  1.29  riastrad 			TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry);
    432  1.29  riastrad 			wq->wq_requeued = true;
    433  1.29  riastrad 			newly_queued = true;
    434  1.29  riastrad 		} else {
    435   1.1     skrll 			/* It is still on the queue; nothing to do.  */
    436  1.12  riastrad 			newly_queued = false;
    437   1.1     skrll 		}
    438   1.1     skrll 	}
    439   1.1     skrll 	mutex_exit(&wq->wq_lock);
    440   1.1     skrll 
    441   1.1     skrll 	return newly_queued;
    442  1.12  riastrad }
    443   1.1     skrll 
    444  1.12  riastrad bool
    445   1.1     skrll cancel_work(struct work_struct *work)
    446   1.1     skrll {
    447  1.13  riastrad 	struct workqueue_struct *wq;
    448  1.13  riastrad 	bool cancelled_p = false;
    449  1.13  riastrad 
    450  1.13  riastrad 	/* If there's no workqueue, nothing to cancel.   */
    451  1.12  riastrad 	if ((wq = work->work_queue) == NULL)
    452  1.12  riastrad 		goto out;
    453  1.29  riastrad 
    454  1.29  riastrad 	mutex_enter(&wq->wq_lock);
    455  1.29  riastrad 	if (__predict_false(work->work_queue != wq)) {
    456  1.29  riastrad 		/*
    457  1.29  riastrad 		 * It has finished execution or been cancelled by
    458  1.12  riastrad 		 * another thread, and has been moved off the
    459  1.12  riastrad 		 * workqueue, so it's too to cancel.
    460  1.29  riastrad 		 */
    461  1.29  riastrad 		cancelled_p = false;
    462  1.29  riastrad 	} else if (wq->wq_current_work == work) {
    463  1.29  riastrad 		/*
    464  1.12  riastrad 		 * It has already begun execution, so it's too late to
    465  1.12  riastrad 		 * cancel now.
    466  1.29  riastrad 		 */
    467  1.29  riastrad 		cancelled_p = false;
    468  1.29  riastrad 	} else {
    469  1.29  riastrad 		/*
    470  1.12  riastrad 		 * It is still on the queue.  Take it off the queue and
    471   1.1     skrll 		 * report successful cancellation.
    472   1.1     skrll 		 */
    473  1.12  riastrad 		TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
    474   1.1     skrll 		cancelled_p = true;
    475  1.13  riastrad 	}
    476   1.1     skrll 	mutex_exit(&wq->wq_lock);
    477   1.1     skrll 
    478  1.12  riastrad out:	return cancelled_p;
    479  1.12  riastrad }
    480   1.1     skrll 
    481   1.1     skrll bool
    482  1.12  riastrad cancel_work_sync(struct work_struct *work)
    483   1.1     skrll {
    484  1.13  riastrad 	struct workqueue_struct *wq;
    485  1.13  riastrad 	bool cancelled_p = false;
    486  1.13  riastrad 
    487  1.13  riastrad 	/* If there's no workqueue, nothing to cancel.   */
    488   1.1     skrll 	if ((wq = work->work_queue) == NULL)
    489  1.12  riastrad 		goto out;
    490  1.29  riastrad 
    491  1.29  riastrad 	mutex_enter(&wq->wq_lock);
    492  1.29  riastrad 	if (__predict_false(work->work_queue != wq)) {
    493  1.29  riastrad 		/*
    494  1.29  riastrad 		 * It has finished execution or been cancelled by
    495  1.12  riastrad 		 * another thread, and has been moved off the
    496  1.12  riastrad 		 * workqueue, so it's too to cancel.
    497  1.29  riastrad 		 */
    498  1.29  riastrad 		cancelled_p = false;
    499  1.29  riastrad 	} else if (wq->wq_current_work == work) {
    500  1.29  riastrad 		/*
    501  1.29  riastrad 		 * It has already begun execution, so it's too late to
    502  1.29  riastrad 		 * cancel now.  Wait for it to complete.  Don't wait
    503  1.12  riastrad 		 * more than one generation in case it gets requeued.
    504  1.12  riastrad 		 */
    505  1.29  riastrad 		uint64_t gen = wq->wq_gen;
    506  1.12  riastrad 		do {
    507  1.12  riastrad 			cv_wait(&wq->wq_cv, &wq->wq_lock);
    508  1.29  riastrad 		} while (wq->wq_current_work == work && wq->wq_gen == gen);
    509  1.29  riastrad 		cancelled_p = false;
    510  1.29  riastrad 	} else {
    511  1.29  riastrad 		/*
    512  1.12  riastrad 		 * It is still on the queue.  Take it off the queue and
    513  1.12  riastrad 		 * report successful cancellation.
    514  1.12  riastrad 		 */
    515   1.1     skrll 		TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
    516   1.1     skrll 		cancelled_p = true;
    517  1.13  riastrad 	}
    518   1.1     skrll 	mutex_exit(&wq->wq_lock);
    519   1.1     skrll 
    520   1.1     skrll out:	return cancelled_p;
    521   1.1     skrll }
    522   1.1     skrll 
    523   1.1     skrll /*
    525   1.1     skrll  * Delayed work
    526   1.1     skrll  */
    527  1.12  riastrad 
    528   1.1     skrll void
    529  1.12  riastrad INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work_struct *))
    530  1.12  riastrad {
    531  1.12  riastrad 
    532  1.12  riastrad 	INIT_WORK(&dw->work, fn);
    533  1.12  riastrad 	dw->dw_state = DELAYED_WORK_IDLE;
    534  1.12  riastrad 
    535  1.12  riastrad 	/*
    536  1.12  riastrad 	 * Defer callout_init until we are going to schedule the
    537   1.1     skrll 	 * callout, which can then callout_destroy it, because
    538   1.1     skrll 	 * otherwise since there's no DESTROY_DELAYED_WORK or anything
    539   1.1     skrll 	 * we have no opportunity to call callout_destroy.
    540   1.1     skrll 	 */
    541   1.1     skrll }
    542  1.12  riastrad 
    543   1.1     skrll bool
    544   1.1     skrll schedule_delayed_work(struct delayed_work *dw, unsigned long ticks)
    545   1.1     skrll {
    546  1.29  riastrad 
    547  1.30  riastrad 	return queue_delayed_work(system_wq, dw, ticks);
    548  1.30  riastrad }
    549  1.30  riastrad 
    550  1.30  riastrad /*
    551  1.30  riastrad  * dw_callout_init(wq, dw)
    552  1.30  riastrad  *
    553  1.30  riastrad  *	Initialize the callout of dw and transition to
    554  1.30  riastrad  *	DELAYED_WORK_SCHEDULED.  Caller must use callout_schedule.
    555  1.30  riastrad  */
    556  1.30  riastrad static void
    557  1.30  riastrad dw_callout_init(struct workqueue_struct *wq, struct delayed_work *dw)
    558  1.30  riastrad {
    559  1.30  riastrad 
    560  1.30  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    561  1.30  riastrad 	KASSERT(dw->work.work_queue == wq);
    562  1.30  riastrad 	KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
    563  1.30  riastrad 
    564  1.30  riastrad 	callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
    565  1.30  riastrad 	callout_setfunc(&dw->dw_callout, &linux_workqueue_timeout, dw);
    566  1.30  riastrad 	TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
    567  1.29  riastrad 	dw->dw_state = DELAYED_WORK_SCHEDULED;
    568  1.29  riastrad }
    569  1.29  riastrad 
    570  1.29  riastrad /*
    571  1.29  riastrad  * cancel_delayed_work_done(wq, dw)
    572  1.29  riastrad  *
    573  1.23  riastrad  *	Complete cancellation of a delayed work: transition from
    574  1.23  riastrad  *	DELAYED_WORK_CANCELLED to DELAYED_WORK_IDLE and off the
    575  1.23  riastrad  *	workqueue.  Caller must not touch dw after this returns.
    576  1.23  riastrad  */
    577  1.23  riastrad static void
    578  1.23  riastrad cancel_delayed_work_done(struct workqueue_struct *wq, struct delayed_work *dw)
    579  1.23  riastrad {
    580  1.23  riastrad 
    581  1.23  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    582  1.23  riastrad 	KASSERT(dw->work.work_queue == wq);
    583  1.23  riastrad 	KASSERT(dw->dw_state == DELAYED_WORK_CANCELLED);
    584  1.23  riastrad 	dw->dw_state = DELAYED_WORK_IDLE;
    585  1.23  riastrad 	callout_destroy(&dw->dw_callout);
    586  1.23  riastrad 	TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
    587  1.29  riastrad 	release_work(&dw->work, wq);
    588  1.29  riastrad 	/* Can't touch dw after this point.  */
    589  1.29  riastrad }
    590  1.29  riastrad 
    591  1.29  riastrad /*
    592  1.29  riastrad  * queue_delayed_work(wq, dw, ticks)
    593  1.29  riastrad  *
    594  1.12  riastrad  *	If it is not currently scheduled, schedule dw to run after
    595  1.12  riastrad  *	ticks.  If currently executing and not already rescheduled,
    596  1.12  riastrad  *	reschedule it.  If ticks == 0, run without delay.
    597  1.12  riastrad  */
    598  1.12  riastrad bool
    599  1.12  riastrad queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
    600   1.1     skrll     unsigned long ticks)
    601  1.12  riastrad {
    602  1.17  riastrad 	struct workqueue_struct *wq0;
    603  1.29  riastrad 	bool newly_queued;
    604  1.29  riastrad 
    605  1.29  riastrad 	mutex_enter(&wq->wq_lock);
    606  1.29  riastrad 	if (__predict_true((wq0 = acquire_work(&dw->work, wq)) == NULL)) {
    607  1.12  riastrad 		/*
    608  1.29  riastrad 		 * It wasn't on any workqueue at all.  Schedule it to
    609  1.29  riastrad 		 * run on this one.
    610  1.29  riastrad 		 */
    611  1.29  riastrad 		KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
    612  1.29  riastrad 		if (ticks == 0) {
    613  1.29  riastrad 			TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work,
    614  1.29  riastrad 			    work_entry);
    615  1.29  riastrad 			cv_broadcast(&wq->wq_cv);
    616  1.29  riastrad 		} else {
    617  1.30  riastrad 			/*
    618  1.29  riastrad 			 * Initialize a callout and schedule to run
    619  1.29  riastrad 			 * after a delay.
    620  1.12  riastrad 			 */
    621  1.12  riastrad 			dw_callout_init(wq, dw);
    622  1.29  riastrad 			callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
    623  1.29  riastrad 		}
    624  1.29  riastrad 		newly_queued = true;
    625  1.29  riastrad 	} else {
    626  1.29  riastrad 		/*
    627  1.29  riastrad 		 * It was on a workqueue, which had better be this one.
    628  1.29  riastrad 		 *
    629  1.29  riastrad 		 * - If it has already begun to run, and it is not yet
    630  1.29  riastrad 		 *   scheduled to run again, schedule it again.
    631  1.29  riastrad 		 *
    632  1.12  riastrad 		 * - If the callout is cancelled, reschedule it.
    633  1.29  riastrad 		 *
    634  1.29  riastrad 		 * - Otherwise, leave it alone.
    635  1.29  riastrad 		 */
    636  1.29  riastrad 		KASSERT(wq0 == wq);
    637  1.29  riastrad 		if (wq->wq_current_work != &dw->work || !wq->wq_requeued) {
    638  1.29  riastrad 			/*
    639  1.29  riastrad 			 * It is either scheduled, on the queue but not
    640  1.29  riastrad 			 * in progress, or in progress but not on the
    641  1.29  riastrad 			 * queue.
    642  1.29  riastrad 			 */
    643  1.29  riastrad 			switch (dw->dw_state) {
    644  1.29  riastrad 			case DELAYED_WORK_IDLE:
    645  1.29  riastrad 				/*
    646  1.29  riastrad 				 * It is not scheduled to run, and it
    647  1.29  riastrad 				 * is not on the queue if it is
    648  1.29  riastrad 				 * running.
    649  1.29  riastrad 				 */
    650  1.29  riastrad 				if (ticks == 0) {
    651  1.29  riastrad 					/*
    652  1.29  riastrad 					 * If it's in progress, put it
    653  1.29  riastrad 					 * on the queue to run as soon
    654  1.29  riastrad 					 * as the worker thread gets to
    655  1.29  riastrad 					 * it.  No need for a wakeup
    656  1.29  riastrad 					 * because either the worker
    657  1.29  riastrad 					 * thread already knows it is
    658  1.29  riastrad 					 * on the queue, or will check
    659  1.29  riastrad 					 * once it is done executing.
    660  1.29  riastrad 					 */
    661  1.29  riastrad 					if (wq->wq_current_work == &dw->work) {
    662  1.29  riastrad 						KASSERT(!wq->wq_requeued);
    663  1.29  riastrad 						TAILQ_INSERT_TAIL(&wq->wq_queue,
    664  1.29  riastrad 						    &dw->work, work_entry);
    665  1.29  riastrad 						wq->wq_requeued = true;
    666  1.29  riastrad 					}
    667  1.29  riastrad 				} else {
    668  1.29  riastrad 					/*
    669  1.30  riastrad 					 * Initialize a callout and
    670  1.30  riastrad 					 * schedule it to run after the
    671  1.30  riastrad 					 * specified delay.
    672  1.29  riastrad 					 */
    673  1.29  riastrad 					dw_callout_init(wq, dw);
    674  1.29  riastrad 					callout_schedule(&dw->dw_callout,
    675  1.29  riastrad 					    MIN(INT_MAX, ticks));
    676  1.29  riastrad 				}
    677  1.29  riastrad 				break;
    678  1.29  riastrad 			case DELAYED_WORK_SCHEDULED:
    679  1.29  riastrad 			case DELAYED_WORK_RESCHEDULED:
    680  1.29  riastrad 				/*
    681  1.29  riastrad 				 * It is already scheduled to run after
    682  1.29  riastrad 				 * a delay.  Leave it be.
    683  1.29  riastrad 				 */
    684  1.29  riastrad 				break;
    685  1.29  riastrad 			case DELAYED_WORK_CANCELLED:
    686  1.29  riastrad 				/*
    687  1.29  riastrad 				 * It was scheduled and the callout has
    688  1.29  riastrad 				 * begun to execute, but it was
    689  1.29  riastrad 				 * cancelled.  Reschedule it.
    690  1.29  riastrad 				 */
    691  1.29  riastrad 				dw->dw_state = DELAYED_WORK_RESCHEDULED;
    692  1.29  riastrad 				callout_schedule(&dw->dw_callout,
    693  1.29  riastrad 				    MIN(INT_MAX, ticks));
    694  1.29  riastrad 				break;
    695  1.29  riastrad 			default:
    696  1.29  riastrad 				panic("invalid delayed work state: %d",
    697  1.29  riastrad 				    dw->dw_state);
    698  1.29  riastrad 			}
    699  1.29  riastrad 		} else {
    700  1.29  riastrad 			/*
    701  1.29  riastrad 			 * It is in progress and it has been requeued.
    702  1.29  riastrad 			 * It cannot be scheduled to run after a delay
    703  1.29  riastrad 			 * at this point.  We just leave it be.
    704  1.29  riastrad 			 */
    705   1.1     skrll 			KASSERTMSG((dw->dw_state == DELAYED_WORK_IDLE),
    706  1.12  riastrad 			    "delayed work %p in wrong state: %d",
    707   1.1     skrll 			    dw, dw->dw_state);
    708   1.1     skrll 		}
    709   1.1     skrll 	}
    710   1.1     skrll 	mutex_exit(&wq->wq_lock);
    711  1.29  riastrad 
    712  1.29  riastrad 	return newly_queued;
    713  1.29  riastrad }
    714  1.29  riastrad 
    715  1.29  riastrad /*
    716  1.29  riastrad  * mod_delayed_work(wq, dw, ticks)
    717  1.29  riastrad  *
    718   1.1     skrll  *	Schedule dw to run after ticks.  If currently scheduled,
    719   1.1     skrll  *	reschedule it.  If currently executing, reschedule it.  If
    720   1.1     skrll  *	ticks == 0, run without delay.
    721   1.1     skrll  */
    722  1.12  riastrad bool
    723   1.1     skrll mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
    724   1.1     skrll     unsigned long ticks)
    725  1.12  riastrad {
    726  1.17  riastrad 	struct workqueue_struct *wq0;
    727  1.29  riastrad 	bool timer_modified;
    728  1.29  riastrad 
    729  1.29  riastrad 	mutex_enter(&wq->wq_lock);
    730  1.29  riastrad 	if ((wq0 = acquire_work(&dw->work, wq)) == NULL) {
    731  1.12  riastrad 		/*
    732  1.29  riastrad 		 * It wasn't on any workqueue at all.  Schedule it to
    733  1.29  riastrad 		 * run on this one.
    734  1.29  riastrad 		 */
    735  1.29  riastrad 		KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
    736  1.29  riastrad 		if (ticks == 0) {
    737  1.29  riastrad 			/*
    738  1.29  riastrad 			 * Run immediately: put it on the queue and
    739  1.29  riastrad 			 * signal the worker thread.
    740  1.29  riastrad 			 */
    741  1.29  riastrad 			TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work,
    742  1.29  riastrad 			    work_entry);
    743  1.29  riastrad 			cv_broadcast(&wq->wq_cv);
    744  1.29  riastrad 		} else {
    745  1.30  riastrad 			/*
    746  1.30  riastrad 			 * Initialize a callout and schedule to run
    747  1.29  riastrad 			 * after a delay.
    748  1.12  riastrad 			 */
    749  1.12  riastrad 			dw_callout_init(wq, dw);
    750  1.29  riastrad 			callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
    751  1.12  riastrad 		}
    752  1.12  riastrad 		timer_modified = false;
    753  1.12  riastrad 	} else {
    754  1.29  riastrad 		/* It was on a workqueue, which had better be this one.  */
    755  1.29  riastrad 		KASSERT(wq0 == wq);
    756  1.29  riastrad 		switch (dw->dw_state) {
    757  1.29  riastrad 		case DELAYED_WORK_IDLE:
    758  1.29  riastrad 			/*
    759  1.29  riastrad 			 * It is not scheduled: it is on the queue or
    760  1.29  riastrad 			 * it is running or both.
    761  1.29  riastrad 			 */
    762  1.29  riastrad 			if (wq->wq_current_work != &dw->work) {
    763  1.29  riastrad 				/* It is on the queue and not yet running.  */
    764  1.29  riastrad 				if (ticks == 0) {
    765  1.29  riastrad 					/*
    766  1.29  riastrad 					 * We ask it to run
    767  1.29  riastrad 					 * immediately.  Leave it on
    768  1.29  riastrad 					 * the queue.
    769  1.29  riastrad 					 */
    770  1.29  riastrad 				} else {
    771  1.29  riastrad 					/*
    772  1.29  riastrad 					 * Take it off the queue and
    773  1.29  riastrad 					 * schedule a callout to run it
    774  1.30  riastrad 					 * after a delay.
    775  1.30  riastrad 					 */
    776  1.30  riastrad 					TAILQ_REMOVE(&wq->wq_queue, &dw->work,
    777  1.29  riastrad 					    work_entry);
    778  1.29  riastrad 					dw_callout_init(wq, dw);
    779  1.29  riastrad 					callout_schedule(&dw->dw_callout,
    780  1.29  riastrad 					    MIN(INT_MAX, ticks));
    781  1.29  riastrad 				}
    782  1.29  riastrad 				timer_modified = true;
    783  1.29  riastrad 			} else if (wq->wq_requeued) {
    784  1.29  riastrad 				/*
    785  1.29  riastrad 				 * It is currently running _and_ it is
    786  1.29  riastrad 				 * on the queue again.
    787  1.29  riastrad 				 */
    788  1.29  riastrad 				if (ticks == 0) {
    789  1.29  riastrad 					/*
    790  1.29  riastrad 					 * We ask it to run
    791  1.29  riastrad 					 * immediately.  Leave it on
    792  1.29  riastrad 					 * the queue.
    793  1.29  riastrad 					 */
    794  1.29  riastrad 				} else {
    795  1.29  riastrad 					/*
    796  1.29  riastrad 					 * Take it off the queue and
    797  1.29  riastrad 					 * schedule a callout to run it
    798  1.29  riastrad 					 * after a delay.
    799  1.30  riastrad 					 */
    800  1.30  riastrad 					wq->wq_requeued = false;
    801  1.30  riastrad 					TAILQ_REMOVE(&wq->wq_queue, &dw->work,
    802  1.29  riastrad 					    work_entry);
    803  1.29  riastrad 					dw_callout_init(wq, dw);
    804  1.29  riastrad 					callout_schedule(&dw->dw_callout,
    805  1.12  riastrad 					    MIN(INT_MAX, ticks));
    806  1.29  riastrad 				}
    807  1.29  riastrad 				timer_modified = true;
    808  1.12  riastrad 			} else {
    809  1.29  riastrad 				/*
    810  1.29  riastrad 				 * It is currently running and has not
    811  1.29  riastrad 				 * been requeued.
    812  1.29  riastrad 				 */
    813  1.29  riastrad 				if (ticks == 0) {
    814  1.29  riastrad 					/*
    815  1.29  riastrad 					 * We ask it to run
    816  1.29  riastrad 					 * immediately.  Put it on the
    817  1.29  riastrad 					 * queue again.
    818  1.29  riastrad 					 */
    819  1.29  riastrad 					wq->wq_requeued = true;
    820  1.29  riastrad 					TAILQ_INSERT_TAIL(&wq->wq_queue,
    821  1.29  riastrad 					    &dw->work, work_entry);
    822  1.29  riastrad 				} else {
    823  1.30  riastrad 					/*
    824  1.30  riastrad 					 * Schedule a callout to run it
    825  1.30  riastrad 					 * after a delay.
    826  1.29  riastrad 					 */
    827  1.12  riastrad 					dw_callout_init(wq, dw);
    828  1.12  riastrad 					callout_schedule(&dw->dw_callout,
    829  1.12  riastrad 					    MIN(INT_MAX, ticks));
    830  1.12  riastrad 				}
    831  1.29  riastrad 				timer_modified = false;
    832  1.29  riastrad 			}
    833  1.29  riastrad 			break;
    834  1.29  riastrad 		case DELAYED_WORK_SCHEDULED:
    835  1.29  riastrad 			/*
    836  1.29  riastrad 			 * It is scheduled to run after a delay.  Try
    837  1.12  riastrad 			 * to stop it and reschedule it; if we can't,
    838  1.29  riastrad 			 * either reschedule it or cancel it to put it
    839  1.29  riastrad 			 * on the queue, and inform the callout.
    840  1.29  riastrad 			 */
    841  1.29  riastrad 			if (callout_stop(&dw->dw_callout)) {
    842  1.29  riastrad 				/* Can't stop, callout has begun.  */
    843  1.29  riastrad 				if (ticks == 0) {
    844  1.29  riastrad 					/*
    845  1.29  riastrad 					 * We don't actually need to do
    846  1.29  riastrad 					 * anything.  The callout will
    847  1.29  riastrad 					 * queue it as soon as it gets
    848  1.29  riastrad 					 * the lock.
    849  1.29  riastrad 					 */
    850  1.29  riastrad 				} else {
    851  1.29  riastrad 					/*
    852  1.29  riastrad 					 * Schedule callout and tell
    853  1.29  riastrad 					 * the instance that's running
    854  1.29  riastrad 					 * now that it's been
    855  1.29  riastrad 					 * rescheduled.
    856  1.29  riastrad 					 */
    857  1.12  riastrad 					dw->dw_state = DELAYED_WORK_RESCHEDULED;
    858  1.29  riastrad 					callout_schedule(&dw->dw_callout,
    859  1.29  riastrad 					    MIN(INT_MAX, ticks));
    860  1.29  riastrad 				}
    861  1.29  riastrad 			} else {
    862  1.29  riastrad 				if (ticks == 0) {
    863  1.29  riastrad 					/*
    864  1.29  riastrad 					 * Run immediately: destroy the
    865  1.29  riastrad 					 * callout, put it on the
    866  1.29  riastrad 					 * queue, and signal the worker
    867  1.29  riastrad 					 * thread.
    868  1.29  riastrad 					 */
    869  1.29  riastrad 					dw->dw_state = DELAYED_WORK_IDLE;
    870  1.29  riastrad 					callout_destroy(&dw->dw_callout);
    871  1.29  riastrad 					TAILQ_REMOVE(&wq->wq_delayed, dw,
    872  1.29  riastrad 					    dw_entry);
    873  1.29  riastrad 					TAILQ_INSERT_TAIL(&wq->wq_queue,
    874  1.29  riastrad 					    &dw->work, work_entry);
    875  1.29  riastrad 					cv_broadcast(&wq->wq_cv);
    876  1.29  riastrad 				} else {
    877  1.29  riastrad 					/*
    878  1.29  riastrad 					 * Reschedule the callout.  No
    879  1.29  riastrad 					 * state change.
    880  1.12  riastrad 					 */
    881  1.12  riastrad 					callout_schedule(&dw->dw_callout,
    882  1.12  riastrad 					    MIN(INT_MAX, ticks));
    883  1.12  riastrad 				}
    884  1.12  riastrad 			}
    885  1.12  riastrad 			timer_modified = true;
    886  1.12  riastrad 			break;
    887  1.12  riastrad 		case DELAYED_WORK_RESCHEDULED:
    888  1.12  riastrad 		case DELAYED_WORK_CANCELLED:
    889  1.29  riastrad 			/*
    890  1.12  riastrad 			 * Someone modified the timer _again_, or
    891  1.29  riastrad 			 * cancelled it, after the callout started but
    892  1.29  riastrad 			 * before the poor thing even had a chance to
    893  1.29  riastrad 			 * acquire the lock.
    894  1.29  riastrad 			 */
    895  1.29  riastrad 			if (ticks == 0) {
    896  1.29  riastrad 				/*
    897  1.29  riastrad 				 * We can just switch back to
    898  1.29  riastrad 				 * DELAYED_WORK_SCHEDULED so that the
    899  1.29  riastrad 				 * callout will queue the work as soon
    900  1.29  riastrad 				 * as it gets the lock.
    901  1.29  riastrad 				 */
    902  1.29  riastrad 				dw->dw_state = DELAYED_WORK_SCHEDULED;
    903  1.29  riastrad 			} else {
    904  1.29  riastrad 				/* Reschedule it.  */
    905  1.12  riastrad 				callout_schedule(&dw->dw_callout,
    906  1.12  riastrad 				    MIN(INT_MAX, ticks));
    907  1.12  riastrad 				dw->dw_state = DELAYED_WORK_RESCHEDULED;
    908  1.29  riastrad 			}
    909   1.1     skrll 			timer_modified = true;
    910   1.1     skrll 			break;
    911  1.12  riastrad 		default:
    912   1.1     skrll 			panic("invalid delayed work state: %d", dw->dw_state);
    913   1.1     skrll 		}
    914   1.1     skrll 	}
    915   1.1     skrll 	mutex_exit(&wq->wq_lock);
    916   1.1     skrll 
    917   1.1     skrll 	return timer_modified;
    918   1.1     skrll }
    919  1.12  riastrad 
    920  1.12  riastrad bool
    921   1.1     skrll cancel_delayed_work(struct delayed_work *dw)
    922  1.14  riastrad {
    923  1.14  riastrad 	struct workqueue_struct *wq;
    924  1.14  riastrad 	bool cancelled_p;
    925  1.14  riastrad 
    926  1.12  riastrad 	/* If there's no workqueue, nothing to cancel.   */
    927  1.12  riastrad 	if ((wq = dw->work.work_queue) == NULL)
    928  1.12  riastrad 		return false;
    929  1.12  riastrad 
    930  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    931  1.12  riastrad 	if (__predict_false(dw->work.work_queue != wq)) {
    932  1.12  riastrad 		cancelled_p = false;
    933  1.29  riastrad 	} else {
    934  1.29  riastrad 		switch (dw->dw_state) {
    935  1.29  riastrad 		case DELAYED_WORK_IDLE:
    936  1.29  riastrad 			if (wq->wq_current_work == &dw->work) {
    937  1.29  riastrad 				/*
    938  1.12  riastrad 				 * Too late, it's already running.  If
    939  1.12  riastrad 				 * it's been requeued, tough -- it'll
    940  1.12  riastrad 				 * run again.
    941  1.12  riastrad 				 */
    942  1.12  riastrad 				cancelled_p = false;
    943  1.12  riastrad 			} else {
    944  1.12  riastrad 				/* Got in before it started.  Remove it.  */
    945  1.12  riastrad 				TAILQ_REMOVE(&wq->wq_queue, &dw->work,
    946  1.12  riastrad 				    work_entry);
    947  1.12  riastrad 				cancelled_p = true;
    948  1.12  riastrad 			}
    949  1.21  riastrad 			break;
    950  1.21  riastrad 		case DELAYED_WORK_SCHEDULED:
    951  1.21  riastrad 		case DELAYED_WORK_RESCHEDULED:
    952  1.21  riastrad 		case DELAYED_WORK_CANCELLED:
    953  1.21  riastrad 			/*
    954  1.21  riastrad 			 * If it is scheduled, mark it cancelled and
    955  1.21  riastrad 			 * try to stop the callout before it starts.
    956  1.21  riastrad 			 *
    957  1.21  riastrad 			 * If it's too late and the callout has already
    958  1.21  riastrad 			 * begun to execute, tough.
    959  1.21  riastrad 			 *
    960  1.21  riastrad 			 * If we stopped the callout before it started,
    961  1.12  riastrad 			 * however, then destroy the callout and
    962  1.27  riastrad 			 * dissociate it from the workqueue ourselves.
    963  1.27  riastrad 			 */
    964  1.16  riastrad 			dw->dw_state = DELAYED_WORK_CANCELLED;
    965  1.12  riastrad 			cancelled_p = true;
    966  1.12  riastrad 			if (!callout_stop(&dw->dw_callout))
    967  1.12  riastrad 				cancel_delayed_work_done(wq, dw);
    968  1.12  riastrad 			break;
    969   1.1     skrll 		default:
    970  1.12  riastrad 			panic("invalid delayed work state: %d",
    971   1.1     skrll 			    dw->dw_state);
    972   1.1     skrll 		}
    973   1.1     skrll 	}
    974   1.1     skrll 	mutex_exit(&wq->wq_lock);
    975   1.1     skrll 
    976   1.1     skrll 	return cancelled_p;
    977   1.1     skrll }
    978  1.12  riastrad 
    979  1.24  riastrad bool
    980   1.1     skrll cancel_delayed_work_sync(struct delayed_work *dw)
    981  1.24  riastrad {
    982  1.14  riastrad 	struct workqueue_struct *wq;
    983  1.24  riastrad 	bool cancelled_p;
    984  1.14  riastrad 
    985  1.12  riastrad 	/* If there's no workqueue, nothing to cancel.  */
    986  1.12  riastrad 	if ((wq = dw->work.work_queue) == NULL)
    987  1.12  riastrad 		return false;
    988  1.12  riastrad 
    989  1.20  riastrad 	mutex_enter(&wq->wq_lock);
    990  1.12  riastrad 	if (__predict_false(dw->work.work_queue != wq)) {
    991  1.12  riastrad 		cancelled_p = false;
    992  1.29  riastrad 	} else {
    993  1.29  riastrad 		switch (dw->dw_state) {
    994  1.29  riastrad 		case DELAYED_WORK_IDLE:
    995  1.29  riastrad 			if (wq->wq_current_work == &dw->work) {
    996  1.29  riastrad 				/*
    997  1.29  riastrad 				 * Too late, it's already running.
    998  1.29  riastrad 				 * First, make sure it's not requeued.
    999  1.29  riastrad 				 * Then wait for it to complete, at
   1000  1.29  riastrad 				 * most one generation.
   1001  1.29  riastrad 				 */
   1002  1.29  riastrad 				uint64_t gen = wq->wq_gen;
   1003  1.29  riastrad 				if (wq->wq_requeued) {
   1004  1.12  riastrad 					TAILQ_REMOVE(&wq->wq_queue, &dw->work,
   1005  1.12  riastrad 					    work_entry);
   1006  1.29  riastrad 					wq->wq_requeued = false;
   1007  1.29  riastrad 				}
   1008  1.12  riastrad 				do {
   1009  1.12  riastrad 					cv_wait(&wq->wq_cv, &wq->wq_lock);
   1010  1.12  riastrad 				} while (wq->wq_current_work == &dw->work &&
   1011  1.12  riastrad 				    wq->wq_gen == gen);
   1012  1.12  riastrad 				cancelled_p = false;
   1013  1.12  riastrad 			} else {
   1014  1.12  riastrad 				/* Got in before it started.  Remove it.  */
   1015  1.12  riastrad 				TAILQ_REMOVE(&wq->wq_queue, &dw->work,
   1016  1.12  riastrad 				    work_entry);
   1017  1.12  riastrad 				cancelled_p = true;
   1018  1.12  riastrad 			}
   1019  1.12  riastrad 			break;
   1020  1.20  riastrad 		case DELAYED_WORK_SCHEDULED:
   1021  1.20  riastrad 		case DELAYED_WORK_RESCHEDULED:
   1022  1.20  riastrad 		case DELAYED_WORK_CANCELLED:
   1023  1.20  riastrad 			/*
   1024  1.20  riastrad 			 * If it is scheduled, mark it cancelled and
   1025  1.24  riastrad 			 * try to stop the callout before it starts.
   1026  1.24  riastrad 			 *
   1027  1.24  riastrad 			 * If it's too late and the callout has already
   1028  1.20  riastrad 			 * begun to execute, we must wait for it to
   1029  1.20  riastrad 			 * complete.  But we got in soon enough to ask
   1030  1.20  riastrad 			 * the callout not to run, so we successfully
   1031  1.20  riastrad 			 * cancelled it in that case.
   1032  1.12  riastrad 			 *
   1033  1.12  riastrad 			 * If we stopped the callout before it started,
   1034  1.20  riastrad 			 * however, then destroy the callout and
   1035  1.27  riastrad 			 * dissociate it from the workqueue ourselves.
   1036  1.27  riastrad 			 */
   1037  1.20  riastrad 			dw->dw_state = DELAYED_WORK_CANCELLED;
   1038  1.12  riastrad 			cancelled_p = true;
   1039  1.12  riastrad 			if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
   1040  1.12  riastrad 				cancel_delayed_work_done(wq, dw);
   1041  1.12  riastrad 			break;
   1042   1.1     skrll 		default:
   1043  1.12  riastrad 			panic("invalid delayed work state: %d",
   1044   1.1     skrll 			    dw->dw_state);
   1045   1.1     skrll 		}
   1046   1.1     skrll 	}
   1047  1.12  riastrad 	mutex_exit(&wq->wq_lock);
   1048  1.12  riastrad 
   1049  1.12  riastrad 	return cancelled_p;
   1050  1.12  riastrad }
   1051   1.1     skrll 
   1052   1.5  riastrad /*
   1054   1.5  riastrad  * Flush
   1055   1.5  riastrad  */
   1056  1.12  riastrad 
   1057   1.5  riastrad void
   1058   1.5  riastrad flush_scheduled_work(void)
   1059  1.28  riastrad {
   1060  1.28  riastrad 
   1061  1.28  riastrad 	flush_workqueue(system_wq);
   1062  1.28  riastrad }
   1063  1.28  riastrad 
   1064  1.28  riastrad static void
   1065  1.28  riastrad flush_workqueue_locked(struct workqueue_struct *wq)
   1066  1.28  riastrad {
   1067  1.28  riastrad 	uint64_t gen;
   1068  1.28  riastrad 
   1069  1.28  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
   1070  1.28  riastrad 
   1071  1.28  riastrad 	/* Get the current generation number.  */
   1072  1.28  riastrad 	gen = wq->wq_gen;
   1073  1.28  riastrad 
   1074  1.28  riastrad 	/*
   1075  1.28  riastrad 	 * If there's a batch of work in progress, we must wait for the
   1076  1.28  riastrad 	 * worker thread to finish that batch.
   1077  1.28  riastrad 	 */
   1078  1.28  riastrad 	if (wq->wq_current_work != NULL)
   1079  1.28  riastrad 		gen++;
   1080  1.28  riastrad 
   1081  1.28  riastrad 	/*
   1082  1.28  riastrad 	 * If there's any work yet to be claimed from the queue by the
   1083  1.28  riastrad 	 * worker thread, we must wait for it to finish one more batch
   1084  1.28  riastrad 	 * too.
   1085  1.28  riastrad 	 */
   1086  1.28  riastrad 	if (!TAILQ_EMPTY(&wq->wq_queue))
   1087  1.28  riastrad 		gen++;
   1088  1.28  riastrad 
   1089  1.12  riastrad 	/* Wait until the generation number has caught up.  */
   1090  1.12  riastrad 	while (wq->wq_gen < gen)
   1091   1.1     skrll 		cv_wait(&wq->wq_cv, &wq->wq_lock);
   1092   1.1     skrll }
   1093  1.12  riastrad 
   1094  1.28  riastrad void
   1095  1.12  riastrad flush_workqueue(struct workqueue_struct *wq)
   1096   1.1     skrll {
   1097   1.1     skrll 
   1098  1.28  riastrad 	mutex_enter(&wq->wq_lock);
   1099  1.12  riastrad 	flush_workqueue_locked(wq);
   1100   1.1     skrll 	mutex_exit(&wq->wq_lock);
   1101  1.14  riastrad }
   1102   1.1     skrll 
   1103  1.14  riastrad void
   1104  1.14  riastrad flush_work(struct work_struct *work)
   1105  1.28  riastrad {
   1106   1.1     skrll 	struct workqueue_struct *wq;
   1107  1.12  riastrad 
   1108   1.1     skrll 	/* If there's no workqueue, nothing to flush.  */
   1109   1.1     skrll 	if ((wq = work->work_queue) == NULL)
   1110  1.28  riastrad 		return;
   1111  1.12  riastrad 
   1112   1.1     skrll 	flush_workqueue(wq);
   1113  1.14  riastrad }
   1114   1.1     skrll 
   1115  1.14  riastrad void
   1116  1.14  riastrad flush_delayed_work(struct delayed_work *dw)
   1117  1.28  riastrad {
   1118   1.1     skrll 	struct workqueue_struct *wq;
   1119   1.1     skrll 
   1120  1.28  riastrad 	/* If there's no workqueue, nothing to flush.  */
   1121  1.28  riastrad 	if ((wq = dw->work.work_queue) == NULL)
   1122  1.12  riastrad 		return;
   1123  1.28  riastrad 
   1124  1.28  riastrad 	mutex_enter(&wq->wq_lock);
   1125  1.28  riastrad 	if (__predict_true(dw->work.work_queue == wq)) {
   1126  1.28  riastrad 		switch (dw->dw_state) {
   1127  1.28  riastrad 		case DELAYED_WORK_IDLE:
   1128  1.28  riastrad 			/*
   1129  1.28  riastrad 			 * It has a workqueue assigned and the callout
   1130  1.28  riastrad 			 * is idle, so it must be in progress or on the
   1131  1.12  riastrad 			 * queue.  In that case, wait for it to
   1132  1.12  riastrad 			 * complete.  Waiting for the whole queue to
   1133  1.12  riastrad 			 * flush is overkill, but doesn't hurt.
   1134  1.12  riastrad 			 */
   1135  1.28  riastrad 			flush_workqueue_locked(wq);
   1136  1.28  riastrad 			break;
   1137  1.28  riastrad 		case DELAYED_WORK_SCHEDULED:
   1138  1.28  riastrad 		case DELAYED_WORK_RESCHEDULED:
   1139  1.28  riastrad 		case DELAYED_WORK_CANCELLED:
   1140  1.28  riastrad 			/*
   1141  1.28  riastrad 			 * The callout is still scheduled to run.
   1142  1.28  riastrad 			 * Notify it that we are cancelling, and try to
   1143  1.28  riastrad 			 * stop the callout before it runs.
   1144  1.28  riastrad 			 *
   1145  1.28  riastrad 			 * If we do stop the callout, we are now
   1146  1.28  riastrad 			 * responsible for dissociating the work from
   1147  1.28  riastrad 			 * the queue.
   1148  1.12  riastrad 			 *
   1149  1.28  riastrad 			 * Otherwise, wait for it to complete and
   1150  1.28  riastrad 			 * dissociate itself -- it will not put itself
   1151  1.12  riastrad 			 * on the workqueue once it is cancelled.
   1152  1.12  riastrad 			 */
   1153  1.12  riastrad 			dw->dw_state = DELAYED_WORK_CANCELLED;
   1154  1.12  riastrad 			if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
   1155   1.1     skrll 				cancel_delayed_work_done(wq, dw);
   1156  1.12  riastrad 		default:
   1157   1.1     skrll 			panic("invalid delayed work state: %d",
   1158                 			    dw->dw_state);
   1159                 		}
   1160                 	}
   1161                 	mutex_exit(&wq->wq_lock);
   1162                 }
   1163