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