Home | History | Annotate | Line # | Download | only in linux
linux_work.c revision 1.23
      1   1.2  riastrad /*	$NetBSD: linux_work.c,v 1.23 2018/08/27 15:02:08 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.23 2018/08/27 15:02:08 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.12  riastrad static void		queue_delayed_work_anew(struct workqueue_struct *,
     69  1.12  riastrad 			    struct delayed_work *, unsigned long);
     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.1     skrll 
    140   1.1     skrll 	wq = kmem_alloc(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.1     skrll 	wq->wq_current_work = NULL;
    147  1.12  riastrad 
    148  1.12  riastrad 	error = kthread_create(PRI_NONE,
    149  1.12  riastrad 	    KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL,
    150  1.12  riastrad 	    &linux_workqueue_thread, wq, &wq->wq_lwp, "%s", name);
    151  1.12  riastrad 	if (error)
    152   1.3  riastrad 		goto fail0;
    153   1.1     skrll 
    154  1.12  riastrad 	return wq;
    155  1.12  riastrad 
    156  1.12  riastrad fail0:	KASSERT(TAILQ_EMPTY(&wq->wq_queue));
    157  1.12  riastrad 	KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
    158  1.12  riastrad 	cv_destroy(&wq->wq_cv);
    159  1.12  riastrad 	mutex_destroy(&wq->wq_lock);
    160  1.12  riastrad 	kmem_free(wq, sizeof(*wq));
    161   1.1     skrll 	return NULL;
    162   1.1     skrll }
    163   1.1     skrll 
    164   1.1     skrll void
    165   1.1     skrll destroy_workqueue(struct workqueue_struct *wq)
    166   1.1     skrll {
    167   1.1     skrll 
    168  1.12  riastrad 	/*
    169  1.12  riastrad 	 * Cancel all delayed work.  We do this first because any
    170  1.12  riastrad 	 * delayed work that that has already timed out, which we can't
    171   1.1     skrll 	 * cancel, may have queued new work.
    172   1.1     skrll 	 */
    173  1.12  riastrad 	for (;;) {
    174   1.1     skrll 		struct delayed_work *dw = NULL;
    175   1.1     skrll 
    176  1.12  riastrad 		mutex_enter(&wq->wq_lock);
    177   1.1     skrll 		if (!TAILQ_EMPTY(&wq->wq_delayed)) {
    178  1.12  riastrad 			dw = TAILQ_FIRST(&wq->wq_delayed);
    179  1.12  riastrad 			if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
    180   1.1     skrll 				TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
    181   1.1     skrll 		}
    182   1.1     skrll 		mutex_exit(&wq->wq_lock);
    183   1.1     skrll 
    184   1.1     skrll 		if (dw == NULL)
    185   1.1     skrll 			break;
    186   1.1     skrll 		cancel_delayed_work_sync(dw);
    187   1.1     skrll 	}
    188  1.12  riastrad 
    189  1.12  riastrad 	/* Tell the thread to exit.  */
    190  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    191  1.12  riastrad 	wq->wq_dying = true;
    192  1.12  riastrad 	cv_broadcast(&wq->wq_cv);
    193  1.12  riastrad 	mutex_exit(&wq->wq_lock);
    194  1.12  riastrad 
    195  1.12  riastrad 	/* Wait for it to exit.  */
    196  1.12  riastrad 	(void)kthread_join(wq->wq_lwp);
    197   1.1     skrll 
    198  1.12  riastrad 	KASSERT(wq->wq_current_work == NULL);
    199  1.12  riastrad 	KASSERT(TAILQ_EMPTY(&wq->wq_queue));
    200   1.1     skrll 	KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
    201   1.1     skrll 	cv_destroy(&wq->wq_cv);
    202   1.1     skrll 	mutex_destroy(&wq->wq_lock);
    203   1.1     skrll 
    204   1.1     skrll 	kmem_free(wq, sizeof(*wq));
    205   1.1     skrll }
    206   1.1     skrll 
    207  1.12  riastrad /*
    209   1.1     skrll  * Work thread and callout
    210  1.12  riastrad  */
    211  1.12  riastrad 
    212   1.1     skrll static void __dead
    213  1.12  riastrad linux_workqueue_thread(void *cookie)
    214  1.12  riastrad {
    215   1.1     skrll 	struct workqueue_struct *const wq = cookie;
    216  1.12  riastrad 	TAILQ_HEAD(, work_struct) tmp;
    217   1.1     skrll 
    218  1.12  riastrad 	lwp_setspecific(workqueue_key, wq);
    219  1.12  riastrad 
    220  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    221  1.12  riastrad 	for (;;) {
    222  1.12  riastrad 		/* Wait until there's activity.  If we're dying, stop.  */
    223  1.12  riastrad 		while (TAILQ_EMPTY(&wq->wq_queue) && !wq->wq_dying)
    224  1.12  riastrad 			cv_wait(&wq->wq_cv, &wq->wq_lock);
    225   1.1     skrll 		if (wq->wq_dying)
    226  1.12  riastrad 			break;
    227  1.12  riastrad 
    228  1.12  riastrad 		/* Grab a batch of work off the queue.  */
    229  1.12  riastrad 		KASSERT(!TAILQ_EMPTY(&wq->wq_queue));
    230  1.12  riastrad 		TAILQ_INIT(&tmp);
    231  1.12  riastrad 		TAILQ_CONCAT(&tmp, &wq->wq_queue, work_entry);
    232  1.12  riastrad 
    233  1.12  riastrad 		/* Process each work item in the batch.  */
    234  1.12  riastrad 		while (!TAILQ_EMPTY(&tmp)) {
    235  1.18  riastrad 			struct work_struct *const work = TAILQ_FIRST(&tmp);
    236  1.12  riastrad 
    237  1.12  riastrad 			KASSERT(work->work_queue == wq);
    238  1.12  riastrad 			TAILQ_REMOVE(&tmp, work, work_entry);
    239   1.1     skrll 			KASSERT(wq->wq_current_work == NULL);
    240  1.12  riastrad 			wq->wq_current_work = work;
    241  1.12  riastrad 
    242  1.12  riastrad 			mutex_exit(&wq->wq_lock);
    243   1.1     skrll 			(*work->func)(work);
    244  1.12  riastrad 			mutex_enter(&wq->wq_lock);
    245  1.12  riastrad 
    246  1.12  riastrad 			KASSERT(wq->wq_current_work == work);
    247  1.12  riastrad 			KASSERT(work->work_queue == wq);
    248  1.12  riastrad 			if (wq->wq_requeued)
    249  1.17  riastrad 				wq->wq_requeued = false;
    250  1.12  riastrad 			else
    251  1.12  riastrad 				release_work(work, wq);
    252  1.12  riastrad 			wq->wq_current_work = NULL;
    253   1.1     skrll 			cv_broadcast(&wq->wq_cv);
    254  1.12  riastrad 		}
    255  1.12  riastrad 
    256  1.12  riastrad 		/* Notify flush that we've completed a batch of work.  */
    257   1.1     skrll 		wq->wq_gen++;
    258  1.12  riastrad 		cv_broadcast(&wq->wq_cv);
    259   1.1     skrll 	}
    260  1.12  riastrad 	mutex_exit(&wq->wq_lock);
    261   1.1     skrll 
    262   1.1     skrll 	kthread_exit(0);
    263   1.1     skrll }
    264  1.12  riastrad 
    265   1.1     skrll static void
    266  1.12  riastrad linux_workqueue_timeout(void *cookie)
    267  1.12  riastrad {
    268   1.1     skrll 	struct delayed_work *const dw = cookie;
    269  1.12  riastrad 	struct workqueue_struct *const wq = dw->work.work_queue;
    270  1.14  riastrad 
    271  1.12  riastrad 	KASSERT(wq != NULL);
    272  1.18  riastrad 
    273  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    274  1.12  riastrad 	KASSERT(dw->work.work_queue == wq);
    275  1.12  riastrad 	switch (dw->dw_state) {
    276  1.12  riastrad 	case DELAYED_WORK_IDLE:
    277  1.12  riastrad 		panic("delayed work callout uninitialized: %p", dw);
    278  1.12  riastrad 	case DELAYED_WORK_SCHEDULED:
    279  1.12  riastrad 		dw->dw_state = DELAYED_WORK_IDLE;
    280  1.12  riastrad 		callout_destroy(&dw->dw_callout);
    281  1.12  riastrad 		TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
    282  1.12  riastrad 		TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, work_entry);
    283  1.12  riastrad 		cv_broadcast(&wq->wq_cv);
    284  1.12  riastrad 		break;
    285  1.12  riastrad 	case DELAYED_WORK_RESCHEDULED:
    286  1.12  riastrad 		dw->dw_state = DELAYED_WORK_SCHEDULED;
    287  1.23  riastrad 		break;
    288  1.22  riastrad 	case DELAYED_WORK_CANCELLED:
    289  1.22  riastrad 		cancel_delayed_work_done(wq, dw);
    290  1.12  riastrad 		/* Can't touch dw any more.  */
    291  1.12  riastrad 		goto out;
    292  1.12  riastrad 	default:
    293  1.15  riastrad 		panic("delayed work callout in bad state: %p", dw);
    294  1.15  riastrad 	}
    295  1.22  riastrad 	KASSERT(dw->dw_state == DELAYED_WORK_IDLE ||
    296   1.1     skrll 	    dw->dw_state == DELAYED_WORK_SCHEDULED);
    297   1.1     skrll out:	mutex_exit(&wq->wq_lock);
    298  1.12  riastrad }
    299  1.12  riastrad 
    300   1.1     skrll struct work_struct *
    301  1.12  riastrad current_work(void)
    302   1.1     skrll {
    303  1.12  riastrad 	struct workqueue_struct *wq = lwp_getspecific(workqueue_key);
    304  1.12  riastrad 
    305  1.12  riastrad 	/* If we're not a workqueue thread, then there's no work.  */
    306   1.1     skrll 	if (wq == NULL)
    307  1.12  riastrad 		return NULL;
    308  1.12  riastrad 
    309  1.12  riastrad 	/*
    310  1.12  riastrad 	 * Otherwise, this should be possible only while work is in
    311  1.12  riastrad 	 * progress.  Return the current work item.
    312  1.12  riastrad 	 */
    313   1.1     skrll 	KASSERT(wq->wq_current_work != NULL);
    314   1.1     skrll 	return wq->wq_current_work;
    315   1.1     skrll }
    316   1.1     skrll 
    317   1.1     skrll /*
    319   1.1     skrll  * Work
    320   1.1     skrll  */
    321   1.1     skrll 
    322   1.1     skrll void
    323  1.12  riastrad INIT_WORK(struct work_struct *work, void (*fn)(struct work_struct *))
    324   1.4  riastrad {
    325   1.1     skrll 
    326   1.1     skrll 	work->work_queue = NULL;
    327  1.17  riastrad 	work->func = fn;
    328  1.17  riastrad }
    329  1.17  riastrad 
    330  1.17  riastrad static struct workqueue_struct *
    331  1.17  riastrad acquire_work(struct work_struct *work, struct workqueue_struct *wq)
    332  1.17  riastrad {
    333  1.17  riastrad 	struct workqueue_struct *wq0;
    334  1.17  riastrad 
    335  1.17  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    336  1.17  riastrad 
    337  1.17  riastrad 	wq0 = atomic_cas_ptr(&work->work_queue, NULL, wq);
    338  1.17  riastrad 	if (wq0 == NULL) {
    339  1.17  riastrad 		membar_enter();
    340  1.17  riastrad 		KASSERT(work->work_queue == wq);
    341  1.17  riastrad 	}
    342  1.17  riastrad 
    343  1.17  riastrad 	return wq0;
    344  1.17  riastrad }
    345  1.17  riastrad 
    346  1.17  riastrad static void
    347  1.17  riastrad release_work(struct work_struct *work, struct workqueue_struct *wq)
    348  1.17  riastrad {
    349  1.17  riastrad 
    350  1.17  riastrad 	KASSERT(work->work_queue == wq);
    351  1.17  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    352  1.17  riastrad 
    353  1.17  riastrad 	membar_exit();
    354   1.1     skrll 	work->work_queue = NULL;
    355   1.1     skrll }
    356   1.1     skrll 
    357  1.12  riastrad bool
    358   1.1     skrll schedule_work(struct work_struct *work)
    359   1.1     skrll {
    360   1.1     skrll 
    361   1.1     skrll 	return queue_work(system_wq, work);
    362   1.1     skrll }
    363   1.1     skrll 
    364  1.12  riastrad bool
    365   1.1     skrll queue_work(struct workqueue_struct *wq, struct work_struct *work)
    366   1.1     skrll {
    367   1.1     skrll 	struct workqueue_struct *wq0;
    368   1.1     skrll 	bool newly_queued;
    369  1.12  riastrad 
    370  1.17  riastrad 	KASSERT(wq != NULL);
    371  1.12  riastrad 
    372   1.1     skrll 	mutex_enter(&wq->wq_lock);
    373  1.12  riastrad 	if (__predict_true((wq0 = acquire_work(work, wq)) == NULL)) {
    374  1.12  riastrad 		TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry);
    375   1.1     skrll 		newly_queued = true;
    376   1.1     skrll 	} else {
    377  1.12  riastrad 		KASSERT(wq0 == wq);
    378   1.1     skrll 		newly_queued = false;
    379   1.1     skrll 	}
    380   1.1     skrll 	mutex_exit(&wq->wq_lock);
    381   1.1     skrll 
    382   1.1     skrll 	return newly_queued;
    383  1.12  riastrad }
    384   1.1     skrll 
    385  1.12  riastrad bool
    386   1.1     skrll cancel_work(struct work_struct *work)
    387   1.1     skrll {
    388  1.13  riastrad 	struct workqueue_struct *wq;
    389  1.13  riastrad 	bool cancelled_p = false;
    390  1.13  riastrad 
    391  1.13  riastrad 	/* If there's no workqueue, nothing to cancel.   */
    392  1.12  riastrad 	if ((wq = work->work_queue) == NULL)
    393  1.12  riastrad 		goto out;
    394  1.12  riastrad 
    395  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    396  1.12  riastrad 	if (__predict_false(work->work_queue != wq)) {
    397  1.12  riastrad 		cancelled_p = false;
    398  1.12  riastrad 	} else if (wq->wq_current_work == work) {
    399   1.1     skrll 		cancelled_p = false;
    400   1.1     skrll 	} else {
    401  1.12  riastrad 		TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
    402   1.1     skrll 		cancelled_p = true;
    403  1.13  riastrad 	}
    404   1.1     skrll 	mutex_exit(&wq->wq_lock);
    405   1.1     skrll 
    406  1.12  riastrad out:	return cancelled_p;
    407  1.12  riastrad }
    408   1.1     skrll 
    409   1.1     skrll bool
    410  1.12  riastrad cancel_work_sync(struct work_struct *work)
    411   1.1     skrll {
    412  1.13  riastrad 	struct workqueue_struct *wq;
    413  1.13  riastrad 	bool cancelled_p = false;
    414  1.13  riastrad 
    415  1.13  riastrad 	/* If there's no workqueue, nothing to cancel.   */
    416   1.1     skrll 	if ((wq = work->work_queue) == NULL)
    417  1.12  riastrad 		goto out;
    418  1.12  riastrad 
    419  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    420  1.12  riastrad 	if (__predict_false(work->work_queue != wq)) {
    421  1.12  riastrad 		cancelled_p = false;
    422  1.12  riastrad 	} else if (wq->wq_current_work == work) {
    423  1.12  riastrad 		do {
    424  1.12  riastrad 			cv_wait(&wq->wq_cv, &wq->wq_lock);
    425  1.12  riastrad 		} while (wq->wq_current_work == work);
    426  1.12  riastrad 		cancelled_p = false;
    427  1.12  riastrad 	} else {
    428   1.1     skrll 		TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
    429   1.1     skrll 		cancelled_p = true;
    430  1.13  riastrad 	}
    431   1.1     skrll 	mutex_exit(&wq->wq_lock);
    432   1.1     skrll 
    433   1.1     skrll out:	return cancelled_p;
    434   1.1     skrll }
    435   1.1     skrll 
    436   1.1     skrll /*
    438   1.1     skrll  * Delayed work
    439   1.1     skrll  */
    440  1.12  riastrad 
    441   1.1     skrll void
    442  1.12  riastrad INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work_struct *))
    443  1.12  riastrad {
    444  1.12  riastrad 
    445  1.12  riastrad 	INIT_WORK(&dw->work, fn);
    446  1.12  riastrad 	dw->dw_state = DELAYED_WORK_IDLE;
    447  1.12  riastrad 
    448  1.12  riastrad 	/*
    449  1.12  riastrad 	 * Defer callout_init until we are going to schedule the
    450   1.1     skrll 	 * callout, which can then callout_destroy it, because
    451   1.1     skrll 	 * otherwise since there's no DESTROY_DELAYED_WORK or anything
    452   1.1     skrll 	 * we have no opportunity to call callout_destroy.
    453   1.1     skrll 	 */
    454   1.1     skrll }
    455  1.12  riastrad 
    456   1.1     skrll bool
    457   1.1     skrll schedule_delayed_work(struct delayed_work *dw, unsigned long ticks)
    458   1.1     skrll {
    459  1.12  riastrad 
    460  1.12  riastrad 	return queue_delayed_work(system_wq, dw, ticks);
    461   1.1     skrll }
    462   1.1     skrll 
    463   1.1     skrll static void
    464  1.12  riastrad queue_delayed_work_anew(struct workqueue_struct *wq, struct delayed_work *dw,
    465  1.12  riastrad     unsigned long ticks)
    466  1.12  riastrad {
    467  1.12  riastrad 
    468  1.12  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    469  1.12  riastrad 	KASSERT(dw->work.work_queue == wq);
    470  1.12  riastrad 	KASSERT((dw->dw_state == DELAYED_WORK_IDLE) ||
    471  1.12  riastrad 	    (dw->dw_state == DELAYED_WORK_SCHEDULED));
    472  1.12  riastrad 
    473   1.1     skrll 	if (ticks == 0) {
    474  1.12  riastrad 		if (dw->dw_state == DELAYED_WORK_SCHEDULED) {
    475  1.12  riastrad 			callout_destroy(&dw->dw_callout);
    476  1.12  riastrad 			TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
    477  1.12  riastrad 		} else {
    478  1.12  riastrad 			KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
    479  1.12  riastrad 		}
    480   1.1     skrll 		TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, work_entry);
    481  1.12  riastrad 		dw->dw_state = DELAYED_WORK_IDLE;
    482  1.12  riastrad 	} else {
    483   1.1     skrll 		if (dw->dw_state == DELAYED_WORK_IDLE) {
    484  1.12  riastrad 			callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
    485  1.12  riastrad 			callout_reset(&dw->dw_callout, MIN(INT_MAX, ticks),
    486   1.1     skrll 			    &linux_workqueue_timeout, dw);
    487  1.12  riastrad 			TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
    488  1.12  riastrad 		} else {
    489  1.12  riastrad 			KASSERT(dw->dw_state == DELAYED_WORK_SCHEDULED);
    490   1.1     skrll 		}
    491  1.23  riastrad 		dw->dw_state = DELAYED_WORK_SCHEDULED;
    492  1.23  riastrad 	}
    493  1.23  riastrad }
    494  1.23  riastrad 
    495  1.23  riastrad static void
    496  1.23  riastrad cancel_delayed_work_done(struct workqueue_struct *wq, struct delayed_work *dw)
    497  1.23  riastrad {
    498  1.23  riastrad 
    499  1.23  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    500  1.23  riastrad 	KASSERT(dw->work.work_queue == wq);
    501  1.23  riastrad 	KASSERT(dw->dw_state == DELAYED_WORK_CANCELLED);
    502  1.23  riastrad 	dw->dw_state = DELAYED_WORK_IDLE;
    503  1.23  riastrad 	callout_destroy(&dw->dw_callout);
    504  1.23  riastrad 	TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
    505  1.12  riastrad 	release_work(&dw->work, wq);
    506  1.12  riastrad 	/* Can't touch dw after this point.  */
    507  1.12  riastrad }
    508  1.12  riastrad 
    509  1.12  riastrad bool
    510  1.12  riastrad queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
    511   1.1     skrll     unsigned long ticks)
    512  1.12  riastrad {
    513  1.17  riastrad 	struct workqueue_struct *wq0;
    514  1.12  riastrad 	bool newly_queued;
    515  1.12  riastrad 
    516  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    517  1.12  riastrad 	if (__predict_true((wq0 = acquire_work(&dw->work, wq)) == NULL)) {
    518  1.12  riastrad 		KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
    519   1.1     skrll 		queue_delayed_work_anew(wq, dw, ticks);
    520   1.1     skrll 		newly_queued = true;
    521  1.12  riastrad 	} else {
    522   1.1     skrll 		KASSERT(wq0 == wq);
    523   1.1     skrll 		newly_queued = false;
    524   1.1     skrll 	}
    525   1.1     skrll 	mutex_exit(&wq->wq_lock);
    526   1.1     skrll 
    527   1.1     skrll 	return newly_queued;
    528   1.1     skrll }
    529   1.1     skrll 
    530  1.12  riastrad bool
    531   1.1     skrll mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
    532   1.1     skrll     unsigned long ticks)
    533  1.12  riastrad {
    534  1.17  riastrad 	struct workqueue_struct *wq0;
    535  1.12  riastrad 	bool timer_modified;
    536  1.12  riastrad 
    537  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    538  1.12  riastrad 	if ((wq0 = acquire_work(&dw->work, wq)) == NULL) {
    539  1.12  riastrad 		KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
    540  1.12  riastrad 		queue_delayed_work_anew(wq, dw, ticks);
    541  1.12  riastrad 		timer_modified = false;
    542  1.12  riastrad 	} else {
    543  1.12  riastrad 		KASSERT(wq0 == wq);
    544  1.12  riastrad 		switch (dw->dw_state) {
    545  1.12  riastrad 		case DELAYED_WORK_IDLE:
    546  1.12  riastrad 			if (wq->wq_current_work != &dw->work) {
    547  1.12  riastrad 				/* Work is queued, but hasn't started yet.  */
    548  1.12  riastrad 				TAILQ_REMOVE(&wq->wq_queue, &dw->work,
    549  1.12  riastrad 				    work_entry);
    550  1.12  riastrad 				queue_delayed_work_anew(wq, dw, ticks);
    551  1.12  riastrad 				timer_modified = true;
    552  1.12  riastrad 			} else {
    553  1.12  riastrad 				/*
    554  1.12  riastrad 				 * Too late.  Queue it anew.  If that
    555  1.12  riastrad 				 * would skip the callout because it's
    556  1.12  riastrad 				 * immediate, notify the workqueue.
    557  1.12  riastrad 				 */
    558  1.12  riastrad 				wq->wq_requeued = ticks == 0;
    559  1.12  riastrad 				queue_delayed_work_anew(wq, dw, ticks);
    560  1.12  riastrad 				timer_modified = false;
    561  1.12  riastrad 			}
    562  1.12  riastrad 			break;
    563  1.12  riastrad 		case DELAYED_WORK_SCHEDULED:
    564  1.12  riastrad 			if (callout_stop(&dw->dw_callout)) {
    565  1.12  riastrad 				/*
    566  1.12  riastrad 				 * Too late to stop, but we got in
    567  1.12  riastrad 				 * before the callout acquired the
    568  1.12  riastrad 				 * lock.  Reschedule it and tell it
    569  1.12  riastrad 				 * we've done so.
    570  1.12  riastrad 				 */
    571  1.12  riastrad 				dw->dw_state = DELAYED_WORK_RESCHEDULED;
    572  1.12  riastrad 				callout_schedule(&dw->dw_callout,
    573  1.12  riastrad 				    MIN(INT_MAX, ticks));
    574  1.12  riastrad 			} else {
    575  1.12  riastrad 				/* Stopped it.  Queue it anew.  */
    576  1.12  riastrad 				queue_delayed_work_anew(wq, dw, ticks);
    577  1.12  riastrad 			}
    578  1.12  riastrad 			timer_modified = true;
    579  1.12  riastrad 			break;
    580  1.12  riastrad 		case DELAYED_WORK_RESCHEDULED:
    581  1.12  riastrad 		case DELAYED_WORK_CANCELLED:
    582  1.12  riastrad 			/*
    583  1.12  riastrad 			 * Someone modified the timer _again_, or
    584  1.12  riastrad 			 * cancelled it, after the callout started but
    585  1.12  riastrad 			 * before the poor thing even had a chance to
    586  1.12  riastrad 			 * acquire the lock.  Just reschedule it once
    587  1.12  riastrad 			 * more.
    588  1.12  riastrad 			 */
    589  1.12  riastrad 			callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
    590  1.12  riastrad 			dw->dw_state = DELAYED_WORK_RESCHEDULED;
    591  1.12  riastrad 			timer_modified = true;
    592   1.1     skrll 			break;
    593   1.1     skrll 		default:
    594  1.12  riastrad 			panic("invalid delayed work state: %d",
    595   1.1     skrll 			    dw->dw_state);
    596   1.1     skrll 		}
    597   1.1     skrll 	}
    598   1.1     skrll 	mutex_exit(&wq->wq_lock);
    599   1.1     skrll 
    600   1.1     skrll 	return timer_modified;
    601   1.1     skrll }
    602  1.12  riastrad 
    603  1.12  riastrad bool
    604   1.1     skrll cancel_delayed_work(struct delayed_work *dw)
    605  1.14  riastrad {
    606  1.14  riastrad 	struct workqueue_struct *wq;
    607  1.14  riastrad 	bool cancelled_p;
    608  1.14  riastrad 
    609  1.12  riastrad 	/* If there's no workqueue, nothing to cancel.   */
    610  1.12  riastrad 	if ((wq = dw->work.work_queue) == NULL)
    611  1.12  riastrad 		return false;
    612  1.12  riastrad 
    613  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    614  1.12  riastrad 	if (__predict_false(dw->work.work_queue != wq)) {
    615  1.12  riastrad 		cancelled_p = false;
    616  1.12  riastrad 	} else {
    617  1.12  riastrad 		switch (dw->dw_state) {
    618  1.12  riastrad 		case DELAYED_WORK_IDLE:
    619  1.12  riastrad 			if (wq->wq_current_work == &dw->work) {
    620  1.12  riastrad 				/* Too late, it's already running.  */
    621  1.12  riastrad 				cancelled_p = false;
    622  1.12  riastrad 			} else {
    623  1.12  riastrad 				/* Got in before it started.  Remove it.  */
    624  1.12  riastrad 				TAILQ_REMOVE(&wq->wq_queue, &dw->work,
    625  1.12  riastrad 				    work_entry);
    626  1.12  riastrad 				cancelled_p = true;
    627  1.12  riastrad 			}
    628  1.21  riastrad 			break;
    629  1.21  riastrad 		case DELAYED_WORK_SCHEDULED:
    630  1.21  riastrad 		case DELAYED_WORK_RESCHEDULED:
    631  1.21  riastrad 		case DELAYED_WORK_CANCELLED:
    632  1.21  riastrad 			/*
    633  1.21  riastrad 			 * If it is scheduled, mark it cancelled and
    634  1.21  riastrad 			 * try to stop the callout before it starts.
    635  1.21  riastrad 			 *
    636  1.21  riastrad 			 * If it's too late and the callout has already
    637  1.21  riastrad 			 * begun to execute, tough.
    638  1.21  riastrad 			 *
    639  1.21  riastrad 			 * If we stopped the callout before it started,
    640  1.12  riastrad 			 * however, then destroy the callout and
    641  1.21  riastrad 			 * dissociate it from the workqueue ourselves.
    642  1.21  riastrad 			 */
    643  1.23  riastrad 			dw->dw_state = DELAYED_WORK_CANCELLED;
    644  1.16  riastrad 			cancelled_p = true;
    645  1.12  riastrad 			if (callout_stop(&dw->dw_callout))
    646  1.12  riastrad 				break;
    647  1.12  riastrad 			cancel_delayed_work_done(wq, dw);
    648  1.12  riastrad 			break;
    649   1.1     skrll 		default:
    650  1.12  riastrad 			panic("invalid delayed work state: %d",
    651   1.1     skrll 			    dw->dw_state);
    652   1.1     skrll 		}
    653   1.1     skrll 	}
    654   1.1     skrll 	mutex_exit(&wq->wq_lock);
    655   1.1     skrll 
    656   1.1     skrll 	return cancelled_p;
    657   1.1     skrll }
    658  1.12  riastrad 
    659  1.20  riastrad bool
    660   1.1     skrll cancel_delayed_work_sync(struct delayed_work *dw)
    661  1.20  riastrad {
    662  1.20  riastrad 	struct workqueue_struct *wq;
    663  1.20  riastrad 	bool cancelled_p = false;
    664  1.20  riastrad 
    665  1.20  riastrad retry:
    666  1.14  riastrad 	/*
    667  1.20  riastrad 	 * If there's no workqueue, nothing to cancel, unless we've
    668  1.14  riastrad 	 * started over from cancelling the callout.
    669  1.12  riastrad 	 */
    670  1.12  riastrad 	if ((wq = dw->work.work_queue) == NULL)
    671  1.12  riastrad 		return cancelled_p;
    672  1.12  riastrad 
    673  1.20  riastrad 	mutex_enter(&wq->wq_lock);
    674  1.12  riastrad 	if (__predict_false(dw->work.work_queue != wq)) {
    675  1.12  riastrad 		cancelled_p = false;
    676  1.12  riastrad 	} else {
    677  1.12  riastrad 		switch (dw->dw_state) {
    678  1.12  riastrad 		case DELAYED_WORK_IDLE:
    679  1.12  riastrad 			if (wq->wq_current_work == &dw->work) {
    680  1.12  riastrad 				/* Too late, it's already running.  Wait.  */
    681  1.12  riastrad 				do {
    682  1.12  riastrad 					cv_wait(&wq->wq_cv, &wq->wq_lock);
    683  1.12  riastrad 				} while (wq->wq_current_work == &dw->work);
    684  1.12  riastrad 				cancelled_p = false;
    685  1.12  riastrad 			} else {
    686  1.12  riastrad 				/* Got in before it started.  Remove it.  */
    687  1.12  riastrad 				TAILQ_REMOVE(&wq->wq_queue, &dw->work,
    688  1.12  riastrad 				    work_entry);
    689  1.12  riastrad 				cancelled_p = true;
    690  1.12  riastrad 			}
    691  1.12  riastrad 			break;
    692  1.20  riastrad 		case DELAYED_WORK_SCHEDULED:
    693  1.20  riastrad 		case DELAYED_WORK_RESCHEDULED:
    694  1.20  riastrad 		case DELAYED_WORK_CANCELLED:
    695  1.20  riastrad 			/*
    696  1.20  riastrad 			 * If it is scheduled, mark it cancelled and
    697  1.20  riastrad 			 * try to stop the callout before it starts.
    698  1.20  riastrad 			 *
    699  1.20  riastrad 			 * If it's too late and the callout has already
    700  1.20  riastrad 			 * begun to execute, we must wait for it to
    701  1.20  riastrad 			 * complete.  In that case, the work has been
    702  1.20  riastrad 			 * dissociated from the queue, so we must start
    703  1.20  riastrad 			 * over from the top.
    704  1.12  riastrad 			 *
    705  1.12  riastrad 			 * If we stopped the callout before it started,
    706  1.20  riastrad 			 * however, then destroy the callout and
    707  1.20  riastrad 			 * dissociate it from the workqueue ourselves.
    708  1.20  riastrad 			 */
    709  1.23  riastrad 			dw->dw_state = DELAYED_WORK_CANCELLED;
    710  1.20  riastrad 			cancelled_p = true;
    711  1.12  riastrad 			if (callout_halt(&dw->dw_callout, &wq->wq_lock))
    712  1.12  riastrad 				goto retry;
    713  1.12  riastrad 			cancel_delayed_work_done(wq, dw);
    714  1.12  riastrad 			break;
    715   1.1     skrll 		default:
    716  1.12  riastrad 			panic("invalid delayed work state: %d",
    717   1.1     skrll 			    dw->dw_state);
    718   1.1     skrll 		}
    719   1.1     skrll 	}
    720  1.12  riastrad 	mutex_exit(&wq->wq_lock);
    721  1.12  riastrad 
    722  1.12  riastrad 	return cancelled_p;
    723  1.12  riastrad }
    724   1.1     skrll 
    725   1.5  riastrad /*
    727   1.5  riastrad  * Flush
    728   1.5  riastrad  */
    729  1.12  riastrad 
    730   1.5  riastrad void
    731   1.5  riastrad flush_scheduled_work(void)
    732  1.12  riastrad {
    733  1.12  riastrad 
    734   1.1     skrll 	flush_workqueue(system_wq);
    735  1.12  riastrad }
    736   1.1     skrll 
    737  1.12  riastrad void
    738  1.19  riastrad flush_workqueue(struct workqueue_struct *wq)
    739  1.19  riastrad {
    740  1.19  riastrad 	uint64_t gen;
    741  1.19  riastrad 
    742  1.19  riastrad 	mutex_enter(&wq->wq_lock);
    743  1.19  riastrad 	if (wq->wq_current_work || !TAILQ_EMPTY(&wq->wq_queue)) {
    744  1.12  riastrad 		gen = wq->wq_gen;
    745   1.1     skrll 		do {
    746   1.1     skrll 			cv_wait(&wq->wq_cv, &wq->wq_lock);
    747  1.12  riastrad 		} while (gen == wq->wq_gen);
    748  1.12  riastrad 	}
    749   1.1     skrll 	mutex_exit(&wq->wq_lock);
    750  1.14  riastrad }
    751   1.1     skrll 
    752  1.14  riastrad bool
    753  1.14  riastrad flush_work(struct work_struct *work)
    754  1.12  riastrad {
    755   1.1     skrll 	struct workqueue_struct *wq;
    756  1.12  riastrad 
    757  1.12  riastrad 	/* If there's no workqueue, nothing to flush.  */
    758   1.1     skrll 	if ((wq = work->work_queue) == NULL)
    759   1.1     skrll 		return false;
    760  1.12  riastrad 
    761  1.12  riastrad 	flush_workqueue(wq);
    762   1.1     skrll 	return true;
    763  1.14  riastrad }
    764  1.12  riastrad 
    765   1.1     skrll bool
    766  1.14  riastrad flush_delayed_work(struct delayed_work *dw)
    767  1.14  riastrad {
    768  1.12  riastrad 	struct workqueue_struct *wq;
    769   1.1     skrll 	bool do_flush = false;
    770   1.1     skrll 
    771  1.12  riastrad 	/* If there's no workqueue, nothing to flush.  */
    772  1.12  riastrad 	if ((wq = dw->work.work_queue) == NULL)
    773   1.1     skrll 		return false;
    774  1.12  riastrad 
    775  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    776  1.12  riastrad 	if (__predict_false(dw->work.work_queue != wq)) {
    777  1.12  riastrad 		do_flush = true;
    778  1.12  riastrad 	} else {
    779  1.12  riastrad retry:		switch (dw->dw_state) {
    780  1.12  riastrad 		case DELAYED_WORK_IDLE:
    781  1.12  riastrad 			if (wq->wq_current_work != &dw->work) {
    782  1.12  riastrad 				TAILQ_REMOVE(&wq->wq_queue, &dw->work,
    783  1.12  riastrad 				    work_entry);
    784  1.12  riastrad 			} else {
    785  1.12  riastrad 				do_flush = true;
    786  1.12  riastrad 			}
    787  1.12  riastrad 			break;
    788  1.12  riastrad 		case DELAYED_WORK_SCHEDULED:
    789  1.12  riastrad 		case DELAYED_WORK_RESCHEDULED:
    790  1.12  riastrad 		case DELAYED_WORK_CANCELLED:
    791  1.12  riastrad 			dw->dw_state = DELAYED_WORK_CANCELLED;
    792  1.12  riastrad 			callout_halt(&dw->dw_callout, &wq->wq_lock);
    793   1.1     skrll 			goto retry;
    794  1.12  riastrad 		default:
    795   1.1     skrll 			panic("invalid delayed work state: %d",
    796  1.12  riastrad 			    dw->dw_state);
    797  1.12  riastrad 		}
    798   1.1     skrll 	}
    799  1.12  riastrad 	mutex_exit(&wq->wq_lock);
    800   1.1     skrll 
    801                 	if (do_flush)
    802                 		flush_workqueue(wq);
    803                 
    804                 	return true;
    805                 }
    806