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