Home | History | Annotate | Line # | Download | only in linux
linux_work.c revision 1.1
      1  1.1  skrll /*	$NetBSD: linux_work.c,v 1.1 2016/02/24 22:04:15 skrll Exp $	*/
      2  1.1  skrll 
      3  1.1  skrll /*-
      4  1.1  skrll  * Copyright (c) 2013 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.1  skrll __KERNEL_RCSID(0, "$NetBSD: linux_work.c,v 1.1 2016/02/24 22:04:15 skrll Exp $");
     34  1.1  skrll 
     35  1.1  skrll #include <sys/types.h>
     36  1.1  skrll #include <sys/param.h>
     37  1.1  skrll #include <sys/atomic.h>
     38  1.1  skrll #include <sys/callout.h>
     39  1.1  skrll #include <sys/condvar.h>
     40  1.1  skrll #include <sys/errno.h>
     41  1.1  skrll #include <sys/intr.h>
     42  1.1  skrll #include <sys/kmem.h>
     43  1.1  skrll #include <sys/mutex.h>
     44  1.1  skrll #include <sys/queue.h>
     45  1.1  skrll #include <sys/systm.h>
     46  1.1  skrll #include <sys/workqueue.h>
     47  1.1  skrll #include <sys/cpu.h>
     48  1.1  skrll 
     49  1.1  skrll #include <machine/lock.h>
     50  1.1  skrll 
     51  1.1  skrll #include <linux/workqueue.h>
     52  1.1  skrll 
     53  1.1  skrll /* XXX Kludge until we sync with HEAD.  */
     54  1.1  skrll #if DIAGNOSTIC
     55  1.1  skrll #define	__diagused
     56  1.1  skrll #else
     57  1.1  skrll #define	__diagused	__unused
     58  1.1  skrll #endif
     59  1.1  skrll 
     60  1.1  skrll struct workqueue_struct {
     61  1.1  skrll 	struct workqueue		*wq_workqueue;
     62  1.1  skrll 
     63  1.1  skrll 	/* XXX The following should all be per-CPU.  */
     64  1.1  skrll 	kmutex_t			wq_lock;
     65  1.1  skrll 
     66  1.1  skrll 	/*
     67  1.1  skrll 	 * Condvar for when any state related to this workqueue
     68  1.1  skrll 	 * changes.  XXX Could split this into multiple condvars for
     69  1.1  skrll 	 * different purposes, but whatever...
     70  1.1  skrll 	 */
     71  1.1  skrll 	kcondvar_t			wq_cv;
     72  1.1  skrll 
     73  1.1  skrll 	TAILQ_HEAD(, delayed_work)	wq_delayed;
     74  1.1  skrll 	struct work_struct		*wq_current_work;
     75  1.1  skrll };
     76  1.1  skrll 
     77  1.1  skrll static void	linux_work_lock_init(struct work_struct *);
     78  1.1  skrll static void	linux_work_lock(struct work_struct *);
     79  1.1  skrll static void	linux_work_unlock(struct work_struct *);
     80  1.1  skrll static bool	linux_work_locked(struct work_struct *) __diagused;
     81  1.1  skrll 
     82  1.1  skrll static void	linux_wq_barrier(struct work_struct *);
     83  1.1  skrll 
     84  1.1  skrll static void	linux_wait_for_cancelled_work(struct work_struct *);
     85  1.1  skrll static void	linux_wait_for_invoked_work(struct work_struct *);
     86  1.1  skrll static void	linux_worker(struct work *, void *);
     87  1.1  skrll 
     88  1.1  skrll static void	linux_cancel_delayed_work_callout(struct delayed_work *, bool);
     89  1.1  skrll static void	linux_wait_for_delayed_cancelled_work(struct delayed_work *);
     90  1.1  skrll static void	linux_worker_intr(void *);
     91  1.1  skrll 
     92  1.1  skrll struct workqueue_struct		*system_wq;
     93  1.1  skrll 
     94  1.1  skrll int
     95  1.1  skrll linux_workqueue_init(void)
     96  1.1  skrll {
     97  1.1  skrll 
     98  1.1  skrll 	system_wq = alloc_ordered_workqueue("lnxsyswq", 0);
     99  1.1  skrll 	if (system_wq == NULL)
    100  1.1  skrll 		return ENOMEM;
    101  1.1  skrll 
    102  1.1  skrll 	return 0;
    103  1.1  skrll }
    104  1.1  skrll 
    105  1.1  skrll void
    106  1.1  skrll linux_workqueue_fini(void)
    107  1.1  skrll {
    108  1.1  skrll 	destroy_workqueue(system_wq);
    109  1.1  skrll 	system_wq = NULL;
    110  1.1  skrll }
    111  1.1  skrll 
    112  1.1  skrll /*
    114  1.1  skrll  * Workqueues
    115  1.1  skrll  */
    116  1.1  skrll 
    117  1.1  skrll struct workqueue_struct *
    118  1.1  skrll alloc_ordered_workqueue(const char *name, int linux_flags)
    119  1.1  skrll {
    120  1.1  skrll 	struct workqueue_struct *wq;
    121  1.1  skrll 	int flags = WQ_MPSAFE;
    122  1.1  skrll 	int error;
    123  1.1  skrll 
    124  1.1  skrll 	KASSERT(linux_flags == 0);
    125  1.1  skrll 
    126  1.1  skrll 	wq = kmem_alloc(sizeof(*wq), KM_SLEEP);
    127  1.1  skrll 	error = workqueue_create(&wq->wq_workqueue, name, &linux_worker,
    128  1.1  skrll 	    wq, PRI_NONE, IPL_VM, flags);
    129  1.1  skrll 	if (error) {
    130  1.1  skrll 		kmem_free(wq, sizeof(*wq));
    131  1.1  skrll 		return NULL;
    132  1.1  skrll 	}
    133  1.1  skrll 
    134  1.1  skrll 	mutex_init(&wq->wq_lock, MUTEX_DEFAULT, IPL_VM);
    135  1.1  skrll 	cv_init(&wq->wq_cv, name);
    136  1.1  skrll 	TAILQ_INIT(&wq->wq_delayed);
    137  1.1  skrll 	wq->wq_current_work = NULL;
    138  1.1  skrll 
    139  1.1  skrll 	return wq;
    140  1.1  skrll }
    141  1.1  skrll 
    142  1.1  skrll void
    143  1.1  skrll destroy_workqueue(struct workqueue_struct *wq)
    144  1.1  skrll {
    145  1.1  skrll 
    146  1.1  skrll 	/*
    147  1.1  skrll 	 * Cancel all delayed work.
    148  1.1  skrll 	 */
    149  1.1  skrll 	for (;;) {
    150  1.1  skrll 		struct delayed_work *dw;
    151  1.1  skrll 
    152  1.1  skrll 		mutex_enter(&wq->wq_lock);
    153  1.1  skrll 		if (TAILQ_EMPTY(&wq->wq_delayed)) {
    154  1.1  skrll 			dw = NULL;
    155  1.1  skrll 		} else {
    156  1.1  skrll 			dw = TAILQ_FIRST(&wq->wq_delayed);
    157  1.1  skrll 			TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
    158  1.1  skrll 		}
    159  1.1  skrll 		mutex_exit(&wq->wq_lock);
    160  1.1  skrll 
    161  1.1  skrll 		if (dw == NULL)
    162  1.1  skrll 			break;
    163  1.1  skrll 
    164  1.1  skrll 		cancel_delayed_work_sync(dw);
    165  1.1  skrll 	}
    166  1.1  skrll 
    167  1.1  skrll 	/*
    168  1.1  skrll 	 * workqueue_destroy empties the queue; we need not wait for
    169  1.1  skrll 	 * completion explicitly.  However, we can't destroy the
    170  1.1  skrll 	 * condvar or mutex until this is done.
    171  1.1  skrll 	 */
    172  1.1  skrll 	workqueue_destroy(wq->wq_workqueue);
    173  1.1  skrll 	KASSERT(wq->wq_current_work == NULL);
    174  1.1  skrll 	wq->wq_workqueue = NULL;
    175  1.1  skrll 
    176  1.1  skrll 	cv_destroy(&wq->wq_cv);
    177  1.1  skrll 	mutex_destroy(&wq->wq_lock);
    178  1.1  skrll 
    179  1.1  skrll 	kmem_free(wq, sizeof(*wq));
    180  1.1  skrll }
    181  1.1  skrll 
    182  1.1  skrll /*
    184  1.1  skrll  * Flush
    185  1.1  skrll  *
    186  1.1  skrll  * Note:  This doesn't cancel or wait for delayed work.  This seems to
    187  1.1  skrll  * match what Linux does (or, doesn't do).
    188  1.1  skrll  */
    189  1.1  skrll 
    190  1.1  skrll void
    191  1.1  skrll flush_scheduled_work(void)
    192  1.1  skrll {
    193  1.1  skrll 	flush_workqueue(system_wq);
    194  1.1  skrll }
    195  1.1  skrll 
    196  1.1  skrll struct wq_flush_work {
    197  1.1  skrll 	struct work_struct	wqfw_work;
    198  1.1  skrll 	struct wq_flush		*wqfw_flush;
    199  1.1  skrll };
    200  1.1  skrll 
    201  1.1  skrll struct wq_flush {
    202  1.1  skrll 	kmutex_t	wqf_lock;
    203  1.1  skrll 	kcondvar_t	wqf_cv;
    204  1.1  skrll 	unsigned int	wqf_n;
    205  1.1  skrll };
    206  1.1  skrll 
    207  1.1  skrll void
    208  1.1  skrll flush_work(struct work_struct *work)
    209  1.1  skrll {
    210  1.1  skrll 	struct workqueue_struct *const wq = work->w_wq;
    211  1.1  skrll 
    212  1.1  skrll 	if (wq != NULL)
    213  1.1  skrll 		flush_workqueue(wq);
    214  1.1  skrll }
    215  1.1  skrll 
    216  1.1  skrll void
    217  1.1  skrll flush_workqueue(struct workqueue_struct *wq)
    218  1.1  skrll {
    219  1.1  skrll 	static const struct wq_flush zero_wqf;
    220  1.1  skrll 	struct wq_flush wqf = zero_wqf;
    221  1.1  skrll 
    222  1.1  skrll 	mutex_init(&wqf.wqf_lock, MUTEX_DEFAULT, IPL_NONE);
    223  1.1  skrll 	cv_init(&wqf.wqf_cv, "lnxwflsh");
    224  1.1  skrll 
    225  1.1  skrll 	if (1) {
    226  1.1  skrll 		struct wq_flush_work *const wqfw = kmem_zalloc(sizeof(*wqfw),
    227  1.1  skrll 		    KM_SLEEP);
    228  1.1  skrll 
    229  1.1  skrll 		wqf.wqf_n = 1;
    230  1.1  skrll 		wqfw->wqfw_flush = &wqf;
    231  1.1  skrll 		INIT_WORK(&wqfw->wqfw_work, &linux_wq_barrier);
    232  1.1  skrll 		wqfw->wqfw_work.w_wq = wq;
    233  1.1  skrll 		wqfw->wqfw_work.w_state = WORK_PENDING;
    234  1.1  skrll 		workqueue_enqueue(wq->wq_workqueue, &wqfw->wqfw_work.w_wk,
    235  1.1  skrll 		    NULL);
    236  1.1  skrll 	} else {
    237  1.1  skrll 		struct cpu_info *ci;
    238  1.1  skrll 		CPU_INFO_ITERATOR cii;
    239  1.1  skrll 		struct wq_flush_work *wqfw;
    240  1.1  skrll 
    241  1.1  skrll 		panic("per-CPU Linux workqueues don't work yet!");
    242  1.1  skrll 
    243  1.1  skrll 		wqf.wqf_n = 0;
    244  1.1  skrll 		for (CPU_INFO_FOREACH(cii, ci)) {
    245  1.1  skrll 			wqfw = kmem_zalloc(sizeof(*wqfw), KM_SLEEP);
    246  1.1  skrll 			mutex_enter(&wqf.wqf_lock);
    247  1.1  skrll 			wqf.wqf_n++;
    248  1.1  skrll 			mutex_exit(&wqf.wqf_lock);
    249  1.1  skrll 			wqfw->wqfw_flush = &wqf;
    250  1.1  skrll 			INIT_WORK(&wqfw->wqfw_work, &linux_wq_barrier);
    251  1.1  skrll 			wqfw->wqfw_work.w_state = WORK_PENDING;
    252  1.1  skrll 			wqfw->wqfw_work.w_wq = wq;
    253  1.1  skrll 			workqueue_enqueue(wq->wq_workqueue,
    254  1.1  skrll 			    &wqfw->wqfw_work.w_wk, ci);
    255  1.1  skrll 		}
    256  1.1  skrll 	}
    257  1.1  skrll 
    258  1.1  skrll 	mutex_enter(&wqf.wqf_lock);
    259  1.1  skrll 	while (0 < wqf.wqf_n)
    260  1.1  skrll 		cv_wait(&wqf.wqf_cv, &wqf.wqf_lock);
    261  1.1  skrll 	mutex_exit(&wqf.wqf_lock);
    262  1.1  skrll 
    263  1.1  skrll 	cv_destroy(&wqf.wqf_cv);
    264  1.1  skrll 	mutex_destroy(&wqf.wqf_lock);
    265  1.1  skrll }
    266  1.1  skrll 
    267  1.1  skrll static void
    268  1.1  skrll linux_wq_barrier(struct work_struct *work)
    269  1.1  skrll {
    270  1.1  skrll 	struct wq_flush_work *const wqfw = container_of(work,
    271  1.1  skrll 	    struct wq_flush_work, wqfw_work);
    272  1.1  skrll 	struct wq_flush *const wqf = wqfw->wqfw_flush;
    273  1.1  skrll 
    274  1.1  skrll 	mutex_enter(&wqf->wqf_lock);
    275  1.1  skrll 	if (--wqf->wqf_n == 0)
    276  1.1  skrll 		cv_broadcast(&wqf->wqf_cv);
    277  1.1  skrll 	mutex_exit(&wqf->wqf_lock);
    278  1.1  skrll 
    279  1.1  skrll 	kmem_free(wqfw, sizeof(*wqfw));
    280  1.1  skrll }
    281  1.1  skrll 
    282  1.1  skrll /*
    284  1.1  skrll  * Work locking
    285  1.1  skrll  *
    286  1.1  skrll  * We use __cpu_simple_lock(9) rather than mutex(9) because Linux code
    287  1.1  skrll  * does not destroy work, so there is nowhere to call mutex_destroy.
    288  1.1  skrll  *
    289  1.1  skrll  * XXX This is getting out of hand...  Really, work items shouldn't
    290  1.1  skrll  * have locks in them at all; instead the workqueues should.
    291  1.1  skrll  */
    292  1.1  skrll 
    293  1.1  skrll static void
    294  1.1  skrll linux_work_lock_init(struct work_struct *work)
    295  1.1  skrll {
    296  1.1  skrll 
    297  1.1  skrll 	__cpu_simple_lock_init(&work->w_lock);
    298  1.1  skrll }
    299  1.1  skrll 
    300  1.1  skrll static void
    301  1.1  skrll linux_work_lock(struct work_struct *work)
    302  1.1  skrll {
    303  1.1  skrll 	struct cpu_info *ci;
    304  1.1  skrll 	int cnt, s;
    305  1.1  skrll 
    306  1.1  skrll 	/* XXX Copypasta of MUTEX_SPIN_SPLRAISE.  */
    307  1.1  skrll 	s = splvm();
    308  1.1  skrll 	ci = curcpu();
    309  1.1  skrll 	cnt = ci->ci_mtx_count--;
    310  1.1  skrll 	__insn_barrier();
    311  1.1  skrll 	if (cnt == 0)
    312  1.1  skrll 		ci->ci_mtx_oldspl = s;
    313  1.1  skrll 
    314  1.1  skrll 	__cpu_simple_lock(&work->w_lock);
    315  1.1  skrll }
    316  1.1  skrll 
    317  1.1  skrll static void
    318  1.1  skrll linux_work_unlock(struct work_struct *work)
    319  1.1  skrll {
    320  1.1  skrll 	struct cpu_info *ci;
    321  1.1  skrll 	int s;
    322  1.1  skrll 
    323  1.1  skrll 	__cpu_simple_unlock(&work->w_lock);
    324  1.1  skrll 
    325  1.1  skrll 	/* XXX Copypasta of MUTEX_SPIN_SPLRESTORE.  */
    326  1.1  skrll 	ci = curcpu();
    327  1.1  skrll 	s = ci->ci_mtx_oldspl;
    328  1.1  skrll 	__insn_barrier();
    329  1.1  skrll 	if (++ci->ci_mtx_count == 0)
    330  1.1  skrll 		splx(s);
    331  1.1  skrll }
    332  1.1  skrll 
    333  1.1  skrll static bool __diagused
    334  1.1  skrll linux_work_locked(struct work_struct *work)
    335  1.1  skrll {
    336  1.1  skrll 	return __SIMPLELOCK_LOCKED_P(&work->w_lock);
    337  1.1  skrll }
    338  1.1  skrll 
    339  1.1  skrll /*
    341  1.1  skrll  * Work
    342  1.1  skrll  */
    343  1.1  skrll 
    344  1.1  skrll void
    345  1.1  skrll INIT_WORK(struct work_struct *work, void (*fn)(struct work_struct *))
    346  1.1  skrll {
    347  1.1  skrll 
    348  1.1  skrll 	linux_work_lock_init(work);
    349  1.1  skrll 	work->w_state = WORK_IDLE;
    350  1.1  skrll 	work->w_wq = NULL;
    351  1.1  skrll 	work->w_fn = fn;
    352  1.1  skrll }
    353  1.1  skrll 
    354  1.1  skrll bool
    355  1.1  skrll schedule_work(struct work_struct *work)
    356  1.1  skrll {
    357  1.1  skrll 	return queue_work(system_wq, work);
    358  1.1  skrll }
    359  1.1  skrll 
    360  1.1  skrll bool
    361  1.1  skrll queue_work(struct workqueue_struct *wq, struct work_struct *work)
    362  1.1  skrll {
    363  1.1  skrll 	/* True if we put it on the queue, false if it was already there.  */
    364  1.1  skrll 	bool newly_queued;
    365  1.1  skrll 
    366  1.1  skrll 	KASSERT(wq != NULL);
    367  1.1  skrll 
    368  1.1  skrll 	linux_work_lock(work);
    369  1.1  skrll 	switch (work->w_state) {
    370  1.1  skrll 	case WORK_IDLE:
    371  1.1  skrll 	case WORK_INVOKED:
    372  1.1  skrll 		work->w_state = WORK_PENDING;
    373  1.1  skrll 		work->w_wq = wq;
    374  1.1  skrll 		workqueue_enqueue(wq->wq_workqueue, &work->w_wk, NULL);
    375  1.1  skrll 		newly_queued = true;
    376  1.1  skrll 		break;
    377  1.1  skrll 
    378  1.1  skrll 	case WORK_DELAYED:
    379  1.1  skrll 		panic("queue_work(delayed work %p)", work);
    380  1.1  skrll 		break;
    381  1.1  skrll 
    382  1.1  skrll 	case WORK_PENDING:
    383  1.1  skrll 		KASSERT(work->w_wq == wq);
    384  1.1  skrll 		newly_queued = false;
    385  1.1  skrll 		break;
    386  1.1  skrll 
    387  1.1  skrll 	case WORK_CANCELLED:
    388  1.1  skrll 		newly_queued = false;
    389  1.1  skrll 		break;
    390  1.1  skrll 
    391  1.1  skrll 	case WORK_DELAYED_CANCELLED:
    392  1.1  skrll 		panic("queue_work(delayed work %p)", work);
    393  1.1  skrll 		break;
    394  1.1  skrll 
    395  1.1  skrll 	default:
    396  1.1  skrll 		panic("work %p in bad state: %d", work, (int)work->w_state);
    397  1.1  skrll 		break;
    398  1.1  skrll 	}
    399  1.1  skrll 	linux_work_unlock(work);
    400  1.1  skrll 
    401  1.1  skrll 	return newly_queued;
    402  1.1  skrll }
    403  1.1  skrll 
    404  1.1  skrll bool
    405  1.1  skrll cancel_work_sync(struct work_struct *work)
    406  1.1  skrll {
    407  1.1  skrll 	bool cancelled_p = false;
    408  1.1  skrll 
    409  1.1  skrll 	linux_work_lock(work);
    410  1.1  skrll 	switch (work->w_state) {
    411  1.1  skrll 	case WORK_IDLE:		/* Nothing to do.  */
    412  1.1  skrll 		break;
    413  1.1  skrll 
    414  1.1  skrll 	case WORK_DELAYED:
    415  1.1  skrll 		panic("cancel_work_sync(delayed work %p)", work);
    416  1.1  skrll 		break;
    417  1.1  skrll 
    418  1.1  skrll 	case WORK_PENDING:
    419  1.1  skrll 		work->w_state = WORK_CANCELLED;
    420  1.1  skrll 		linux_wait_for_cancelled_work(work);
    421  1.1  skrll 		cancelled_p = true;
    422  1.1  skrll 		break;
    423  1.1  skrll 
    424  1.1  skrll 	case WORK_INVOKED:
    425  1.1  skrll 		linux_wait_for_invoked_work(work);
    426  1.1  skrll 		break;
    427  1.1  skrll 
    428  1.1  skrll 	case WORK_CANCELLED:	/* Already done.  */
    429  1.1  skrll 		break;
    430  1.1  skrll 
    431  1.1  skrll 	case WORK_DELAYED_CANCELLED:
    432  1.1  skrll 		panic("cancel_work_sync(delayed work %p)", work);
    433  1.1  skrll 		break;
    434  1.1  skrll 
    435  1.1  skrll 	default:
    436  1.1  skrll 		panic("work %p in bad state: %d", work, (int)work->w_state);
    437  1.1  skrll 		break;
    438  1.1  skrll 	}
    439  1.1  skrll 	linux_work_unlock(work);
    440  1.1  skrll 
    441  1.1  skrll 	return cancelled_p;
    442  1.1  skrll }
    443  1.1  skrll 
    444  1.1  skrll static void
    445  1.1  skrll linux_wait_for_cancelled_work(struct work_struct *work)
    446  1.1  skrll {
    447  1.1  skrll 	struct workqueue_struct *wq;
    448  1.1  skrll 
    449  1.1  skrll 	KASSERT(linux_work_locked(work));
    450  1.1  skrll 	KASSERT(work->w_state == WORK_CANCELLED);
    451  1.1  skrll 
    452  1.1  skrll 	wq = work->w_wq;
    453  1.1  skrll 	do {
    454  1.1  skrll 		mutex_enter(&wq->wq_lock);
    455  1.1  skrll 		linux_work_unlock(work);
    456  1.1  skrll 		cv_wait(&wq->wq_cv, &wq->wq_lock);
    457  1.1  skrll 		mutex_exit(&wq->wq_lock);
    458  1.1  skrll 		linux_work_lock(work);
    459  1.1  skrll 	} while ((work->w_state == WORK_CANCELLED) && (work->w_wq == wq));
    460  1.1  skrll }
    461  1.1  skrll 
    462  1.1  skrll static void
    463  1.1  skrll linux_wait_for_invoked_work(struct work_struct *work)
    464  1.1  skrll {
    465  1.1  skrll 	struct workqueue_struct *wq;
    466  1.1  skrll 
    467  1.1  skrll 	KASSERT(linux_work_locked(work));
    468  1.1  skrll 	KASSERT(work->w_state == WORK_INVOKED);
    469  1.1  skrll 
    470  1.1  skrll 	wq = work->w_wq;
    471  1.1  skrll 	mutex_enter(&wq->wq_lock);
    472  1.1  skrll 	linux_work_unlock(work);
    473  1.1  skrll 	while (wq->wq_current_work == work)
    474  1.1  skrll 		cv_wait(&wq->wq_cv, &wq->wq_lock);
    475  1.1  skrll 	mutex_exit(&wq->wq_lock);
    476  1.1  skrll 
    477  1.1  skrll 	linux_work_lock(work);	/* XXX needless relock */
    478  1.1  skrll }
    479  1.1  skrll 
    480  1.1  skrll static void
    481  1.1  skrll linux_worker(struct work *wk, void *arg)
    482  1.1  skrll {
    483  1.1  skrll 	struct work_struct *const work = container_of(wk, struct work_struct,
    484  1.1  skrll 	    w_wk);
    485  1.1  skrll 	struct workqueue_struct *const wq = arg;
    486  1.1  skrll 
    487  1.1  skrll 	linux_work_lock(work);
    488  1.1  skrll 	switch (work->w_state) {
    489  1.1  skrll 	case WORK_IDLE:
    490  1.1  skrll 		panic("idle work %p got queued: %p", work, wq);
    491  1.1  skrll 		break;
    492  1.1  skrll 
    493  1.1  skrll 	case WORK_DELAYED:
    494  1.1  skrll 		panic("delayed work %p got queued: %p", work, wq);
    495  1.1  skrll 		break;
    496  1.1  skrll 
    497  1.1  skrll 	case WORK_PENDING:
    498  1.1  skrll 		KASSERT(work->w_wq == wq);
    499  1.1  skrll 
    500  1.1  skrll 		/* Get ready to invoke this one.  */
    501  1.1  skrll 		mutex_enter(&wq->wq_lock);
    502  1.1  skrll 		work->w_state = WORK_INVOKED;
    503  1.1  skrll 		KASSERT(wq->wq_current_work == NULL);
    504  1.1  skrll 		wq->wq_current_work = work;
    505  1.1  skrll 		mutex_exit(&wq->wq_lock);
    506  1.1  skrll 
    507  1.1  skrll 		/* Unlock it and do it.  Can't use work after this.  */
    508  1.1  skrll 		linux_work_unlock(work);
    509  1.1  skrll 		(*work->w_fn)(work);
    510  1.1  skrll 
    511  1.1  skrll 		/* All done.  Notify anyone waiting for completion.  */
    512  1.1  skrll 		mutex_enter(&wq->wq_lock);
    513  1.1  skrll 		KASSERT(wq->wq_current_work == work);
    514  1.1  skrll 		wq->wq_current_work = NULL;
    515  1.1  skrll 		cv_broadcast(&wq->wq_cv);
    516  1.1  skrll 		mutex_exit(&wq->wq_lock);
    517  1.1  skrll 		return;
    518  1.1  skrll 
    519  1.1  skrll 	case WORK_INVOKED:
    520  1.1  skrll 		panic("invoked work %p got requeued: %p", work, wq);
    521  1.1  skrll 		break;
    522  1.1  skrll 
    523  1.1  skrll 	case WORK_CANCELLED:
    524  1.1  skrll 		KASSERT(work->w_wq == wq);
    525  1.1  skrll 
    526  1.1  skrll 		/* Return to idle; notify anyone waiting for cancellation.  */
    527  1.1  skrll 		mutex_enter(&wq->wq_lock);
    528  1.1  skrll 		work->w_state = WORK_IDLE;
    529  1.1  skrll 		work->w_wq = NULL;
    530  1.1  skrll 		cv_broadcast(&wq->wq_cv);
    531  1.1  skrll 		mutex_exit(&wq->wq_lock);
    532  1.1  skrll 		break;
    533  1.1  skrll 
    534  1.1  skrll 	case WORK_DELAYED_CANCELLED:
    535  1.1  skrll 		panic("cancelled delayed work %p got uqeued: %p", work, wq);
    536  1.1  skrll 		break;
    537  1.1  skrll 
    538  1.1  skrll 	default:
    539  1.1  skrll 		panic("work %p in bad state: %d", work, (int)work->w_state);
    540  1.1  skrll 		break;
    541  1.1  skrll 	}
    542  1.1  skrll 	linux_work_unlock(work);
    543  1.1  skrll }
    544  1.1  skrll 
    545  1.1  skrll /*
    547  1.1  skrll  * Delayed work
    548  1.1  skrll  */
    549  1.1  skrll 
    550  1.1  skrll void
    551  1.1  skrll INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work_struct *))
    552  1.1  skrll {
    553  1.1  skrll 	INIT_WORK(&dw->work, fn);
    554  1.1  skrll }
    555  1.1  skrll 
    556  1.1  skrll bool
    557  1.1  skrll schedule_delayed_work(struct delayed_work *dw, unsigned long ticks)
    558  1.1  skrll {
    559  1.1  skrll 	return queue_delayed_work(system_wq, dw, ticks);
    560  1.1  skrll }
    561  1.1  skrll 
    562  1.1  skrll bool
    563  1.1  skrll queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
    564  1.1  skrll     unsigned long ticks)
    565  1.1  skrll {
    566  1.1  skrll 	bool newly_queued;
    567  1.1  skrll 
    568  1.1  skrll 	KASSERT(wq != NULL);
    569  1.1  skrll 
    570  1.1  skrll 	linux_work_lock(&dw->work);
    571  1.1  skrll 	switch (dw->work.w_state) {
    572  1.1  skrll 	case WORK_IDLE:
    573  1.1  skrll 	case WORK_INVOKED:
    574  1.1  skrll 		if (ticks == 0) {
    575  1.1  skrll 			/* Skip the delay and queue it now.  */
    576  1.1  skrll 			dw->work.w_state = WORK_PENDING;
    577  1.1  skrll 			dw->work.w_wq = wq;
    578  1.1  skrll 			workqueue_enqueue(wq->wq_workqueue, &dw->work.w_wk,
    579  1.1  skrll 			    NULL);
    580  1.1  skrll 		} else {
    581  1.1  skrll 			callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
    582  1.1  skrll 			callout_reset(&dw->dw_callout, ticks,
    583  1.1  skrll 			    &linux_worker_intr, dw);
    584  1.1  skrll 			dw->work.w_state = WORK_DELAYED;
    585  1.1  skrll 			dw->work.w_wq = wq;
    586  1.1  skrll 			mutex_enter(&wq->wq_lock);
    587  1.1  skrll 			TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
    588  1.1  skrll 			mutex_exit(&wq->wq_lock);
    589  1.1  skrll 		}
    590  1.1  skrll 		newly_queued = true;
    591  1.1  skrll 		break;
    592  1.1  skrll 
    593  1.1  skrll 	case WORK_DELAYED:
    594  1.1  skrll 		/*
    595  1.1  skrll 		 * Timer is already ticking.  Leave it to time out
    596  1.1  skrll 		 * whenever it was going to time out, as Linux does --
    597  1.1  skrll 		 * neither speed it up nor postpone it.
    598  1.1  skrll 		 */
    599  1.1  skrll 		newly_queued = false;
    600  1.1  skrll 		break;
    601  1.1  skrll 
    602  1.1  skrll 	case WORK_PENDING:
    603  1.1  skrll 		KASSERT(dw->work.w_wq == wq);
    604  1.1  skrll 		newly_queued = false;
    605  1.1  skrll 		break;
    606  1.1  skrll 
    607  1.1  skrll 	case WORK_CANCELLED:
    608  1.1  skrll 	case WORK_DELAYED_CANCELLED:
    609  1.1  skrll 		/* XXX Wait for cancellation and then queue?  */
    610  1.1  skrll 		newly_queued = false;
    611  1.1  skrll 		break;
    612  1.1  skrll 
    613  1.1  skrll 	default:
    614  1.1  skrll 		panic("delayed work %p in bad state: %d", dw,
    615  1.1  skrll 		    (int)dw->work.w_state);
    616  1.1  skrll 		break;
    617  1.1  skrll 	}
    618  1.1  skrll 	linux_work_unlock(&dw->work);
    619  1.1  skrll 
    620  1.1  skrll 	return newly_queued;
    621  1.1  skrll }
    622  1.1  skrll 
    623  1.1  skrll bool
    624  1.1  skrll mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
    625  1.1  skrll     unsigned long ticks)
    626  1.1  skrll {
    627  1.1  skrll 	bool timer_modified;
    628  1.1  skrll 
    629  1.1  skrll 	KASSERT(wq != NULL);
    630  1.1  skrll 
    631  1.1  skrll 	linux_work_lock(&dw->work);
    632  1.1  skrll 	switch (dw->work.w_state) {
    633  1.1  skrll 	case WORK_IDLE:
    634  1.1  skrll 	case WORK_INVOKED:
    635  1.1  skrll 		if (ticks == 0) {
    636  1.1  skrll 			/* Skip the delay and queue it now.  */
    637  1.1  skrll 			dw->work.w_state = WORK_PENDING;
    638  1.1  skrll 			dw->work.w_wq = wq;
    639  1.1  skrll 			workqueue_enqueue(wq->wq_workqueue, &dw->work.w_wk,
    640  1.1  skrll 			    NULL);
    641  1.1  skrll 		} else {
    642  1.1  skrll 			callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
    643  1.1  skrll 			callout_reset(&dw->dw_callout, ticks,
    644  1.1  skrll 			    &linux_worker_intr, dw);
    645  1.1  skrll 			dw->work.w_state = WORK_DELAYED;
    646  1.1  skrll 			dw->work.w_wq = wq;
    647  1.1  skrll 			mutex_enter(&wq->wq_lock);
    648  1.1  skrll 			TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
    649  1.1  skrll 			mutex_exit(&wq->wq_lock);
    650  1.1  skrll 		}
    651  1.1  skrll 		timer_modified = false;
    652  1.1  skrll 		break;
    653  1.1  skrll 
    654  1.1  skrll 	case WORK_DELAYED:
    655  1.1  skrll 		/*
    656  1.1  skrll 		 * Timer is already ticking.  Reschedule it.
    657  1.1  skrll 		 */
    658  1.1  skrll 		callout_schedule(&dw->dw_callout, ticks);
    659  1.1  skrll 		timer_modified = true;
    660  1.1  skrll 		break;
    661  1.1  skrll 
    662  1.1  skrll 	case WORK_PENDING:
    663  1.1  skrll 		KASSERT(dw->work.w_wq == wq);
    664  1.1  skrll 		timer_modified = false;
    665  1.1  skrll 		break;
    666  1.1  skrll 
    667  1.1  skrll 	case WORK_CANCELLED:
    668  1.1  skrll 	case WORK_DELAYED_CANCELLED:
    669  1.1  skrll 		/* XXX Wait for cancellation and then queue?  */
    670  1.1  skrll 		timer_modified = false;
    671  1.1  skrll 		break;
    672  1.1  skrll 
    673  1.1  skrll 	default:
    674  1.1  skrll 		panic("delayed work %p in bad state: %d", dw,
    675  1.1  skrll 		    (int)dw->work.w_state);
    676  1.1  skrll 		break;
    677  1.1  skrll 	}
    678  1.1  skrll 	linux_work_unlock(&dw->work);
    679  1.1  skrll 
    680  1.1  skrll 	return timer_modified;
    681  1.1  skrll }
    682  1.1  skrll 
    683  1.1  skrll bool
    684  1.1  skrll cancel_delayed_work(struct delayed_work *dw)
    685  1.1  skrll {
    686  1.1  skrll 	bool cancelled_p = false;
    687  1.1  skrll 
    688  1.1  skrll 	linux_work_lock(&dw->work);
    689  1.1  skrll 	switch (dw->work.w_state) {
    690  1.1  skrll 	case WORK_IDLE:		/* Nothing to do.  */
    691  1.1  skrll 		break;
    692  1.1  skrll 
    693  1.1  skrll 	case WORK_DELAYED:
    694  1.1  skrll 		dw->work.w_state = WORK_DELAYED_CANCELLED;
    695  1.1  skrll 		linux_cancel_delayed_work_callout(dw, false);
    696  1.1  skrll 		cancelled_p = true;
    697  1.1  skrll 		break;
    698  1.1  skrll 
    699  1.1  skrll 	case WORK_PENDING:
    700  1.1  skrll 		dw->work.w_state = WORK_CANCELLED;
    701  1.1  skrll 		cancelled_p = true;
    702  1.1  skrll 		break;
    703  1.1  skrll 
    704  1.1  skrll 	case WORK_INVOKED:	/* Don't wait!  */
    705  1.1  skrll 		break;
    706  1.1  skrll 
    707  1.1  skrll 	case WORK_CANCELLED:	/* Already done.  */
    708  1.1  skrll 	case WORK_DELAYED_CANCELLED:
    709  1.1  skrll 		break;
    710  1.1  skrll 
    711  1.1  skrll 	default:
    712  1.1  skrll 		panic("delayed work %p in bad state: %d", dw,
    713  1.1  skrll 		    (int)dw->work.w_state);
    714  1.1  skrll 		break;
    715  1.1  skrll 	}
    716  1.1  skrll 	linux_work_unlock(&dw->work);
    717  1.1  skrll 
    718  1.1  skrll 	return cancelled_p;
    719  1.1  skrll }
    720  1.1  skrll 
    721  1.1  skrll bool
    722  1.1  skrll cancel_delayed_work_sync(struct delayed_work *dw)
    723  1.1  skrll {
    724  1.1  skrll 	bool cancelled_p = false;
    725  1.1  skrll 
    726  1.1  skrll 	linux_work_lock(&dw->work);
    727  1.1  skrll 	switch (dw->work.w_state) {
    728  1.1  skrll 	case WORK_IDLE:		/* Nothing to do.  */
    729  1.1  skrll 		break;
    730  1.1  skrll 
    731  1.1  skrll 	case WORK_DELAYED:
    732  1.1  skrll 		dw->work.w_state = WORK_DELAYED_CANCELLED;
    733  1.1  skrll 		linux_cancel_delayed_work_callout(dw, true);
    734  1.1  skrll 		cancelled_p = true;
    735  1.1  skrll 		break;
    736  1.1  skrll 
    737  1.1  skrll 	case WORK_PENDING:
    738  1.1  skrll 		dw->work.w_state = WORK_CANCELLED;
    739  1.1  skrll 		linux_wait_for_cancelled_work(&dw->work);
    740  1.1  skrll 		cancelled_p = true;
    741  1.1  skrll 		break;
    742  1.1  skrll 
    743  1.1  skrll 	case WORK_INVOKED:
    744  1.1  skrll 		linux_wait_for_invoked_work(&dw->work);
    745  1.1  skrll 		break;
    746  1.1  skrll 
    747  1.1  skrll 	case WORK_CANCELLED:	/* Already done.  */
    748  1.1  skrll 		break;
    749  1.1  skrll 
    750  1.1  skrll 	case WORK_DELAYED_CANCELLED:
    751  1.1  skrll 		linux_wait_for_delayed_cancelled_work(dw);
    752  1.1  skrll 		break;
    753  1.1  skrll 
    754  1.1  skrll 	default:
    755  1.1  skrll 		panic("delayed work %p in bad state: %d", dw,
    756  1.1  skrll 		    (int)dw->work.w_state);
    757  1.1  skrll 		break;
    758  1.1  skrll 	}
    759  1.1  skrll 	linux_work_unlock(&dw->work);
    760  1.1  skrll 
    761  1.1  skrll 	return cancelled_p;
    762  1.1  skrll }
    763  1.1  skrll 
    764  1.1  skrll static void
    765  1.1  skrll linux_cancel_delayed_work_callout(struct delayed_work *dw, bool wait)
    766  1.1  skrll {
    767  1.1  skrll 	bool fired_p;
    768  1.1  skrll 
    769  1.1  skrll 	KASSERT(linux_work_locked(&dw->work));
    770  1.1  skrll 	KASSERT(dw->work.w_state == WORK_DELAYED_CANCELLED);
    771  1.1  skrll 
    772  1.1  skrll 	if (wait) {
    773  1.1  skrll 		/*
    774  1.1  skrll 		 * We unlock, halt, and then relock, rather than
    775  1.1  skrll 		 * passing an interlock to callout_halt, for two
    776  1.1  skrll 		 * reasons:
    777  1.1  skrll 		 *
    778  1.1  skrll 		 * (1) The work lock is not a mutex(9), so we can't use it.
    779  1.1  skrll 		 * (2) The WORK_DELAYED_CANCELLED state serves as an interlock.
    780  1.1  skrll 		 */
    781  1.1  skrll 		linux_work_unlock(&dw->work);
    782  1.1  skrll 		fired_p = callout_halt(&dw->dw_callout, NULL);
    783  1.1  skrll 		linux_work_lock(&dw->work);
    784  1.1  skrll 	} else {
    785  1.1  skrll 		fired_p = callout_stop(&dw->dw_callout);
    786  1.1  skrll 	}
    787  1.1  skrll 
    788  1.1  skrll 	/*
    789  1.1  skrll 	 * fired_p means we didn't cancel the callout, so it must have
    790  1.1  skrll 	 * already begun and will clean up after itself.
    791  1.1  skrll 	 *
    792  1.1  skrll 	 * !fired_p means we cancelled it so we have to clean up after
    793  1.1  skrll 	 * it.  Nobody else should have changed the state in that case.
    794  1.1  skrll 	 */
    795  1.1  skrll 	if (!fired_p) {
    796  1.1  skrll 		struct workqueue_struct *wq;
    797  1.1  skrll 
    798  1.1  skrll 		KASSERT(linux_work_locked(&dw->work));
    799  1.1  skrll 		KASSERT(dw->work.w_state == WORK_DELAYED_CANCELLED);
    800  1.1  skrll 
    801  1.1  skrll 		wq = dw->work.w_wq;
    802  1.1  skrll 		mutex_enter(&wq->wq_lock);
    803  1.1  skrll 		TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
    804  1.1  skrll 		callout_destroy(&dw->dw_callout);
    805  1.1  skrll 		dw->work.w_state = WORK_IDLE;
    806  1.1  skrll 		dw->work.w_wq = NULL;
    807  1.1  skrll 		cv_broadcast(&wq->wq_cv);
    808  1.1  skrll 		mutex_exit(&wq->wq_lock);
    809  1.1  skrll 	}
    810  1.1  skrll }
    811  1.1  skrll 
    812  1.1  skrll static void
    813  1.1  skrll linux_wait_for_delayed_cancelled_work(struct delayed_work *dw)
    814  1.1  skrll {
    815  1.1  skrll 	struct workqueue_struct *wq;
    816  1.1  skrll 
    817  1.1  skrll 	KASSERT(linux_work_locked(&dw->work));
    818  1.1  skrll 	KASSERT(dw->work.w_state == WORK_DELAYED_CANCELLED);
    819  1.1  skrll 
    820  1.1  skrll 	wq = dw->work.w_wq;
    821  1.1  skrll 	do {
    822  1.1  skrll 		mutex_enter(&wq->wq_lock);
    823  1.1  skrll 		linux_work_unlock(&dw->work);
    824  1.1  skrll 		cv_wait(&wq->wq_cv, &wq->wq_lock);
    825  1.1  skrll 		mutex_exit(&wq->wq_lock);
    826  1.1  skrll 		linux_work_lock(&dw->work);
    827  1.1  skrll 	} while ((dw->work.w_state == WORK_DELAYED_CANCELLED) &&
    828  1.1  skrll 	    (dw->work.w_wq == wq));
    829  1.1  skrll }
    830  1.1  skrll 
    831  1.1  skrll static void
    832  1.1  skrll linux_worker_intr(void *arg)
    833  1.1  skrll {
    834  1.1  skrll 	struct delayed_work *dw = arg;
    835  1.1  skrll 	struct workqueue_struct *wq;
    836  1.1  skrll 
    837  1.1  skrll 	linux_work_lock(&dw->work);
    838  1.1  skrll 
    839  1.1  skrll 	KASSERT((dw->work.w_state == WORK_DELAYED) ||
    840  1.1  skrll 	    (dw->work.w_state == WORK_DELAYED_CANCELLED));
    841  1.1  skrll 
    842  1.1  skrll 	wq = dw->work.w_wq;
    843  1.1  skrll 	mutex_enter(&wq->wq_lock);
    844  1.1  skrll 
    845  1.1  skrll 	/* Queue the work, or return it to idle and alert any cancellers.  */
    846  1.1  skrll 	if (__predict_true(dw->work.w_state == WORK_DELAYED)) {
    847  1.1  skrll 		dw->work.w_state = WORK_PENDING;
    848  1.1  skrll 		workqueue_enqueue(dw->work.w_wq->wq_workqueue, &dw->work.w_wk,
    849  1.1  skrll 		    NULL);
    850  1.1  skrll 	} else {
    851  1.1  skrll 		KASSERT(dw->work.w_state == WORK_DELAYED_CANCELLED);
    852  1.1  skrll 		dw->work.w_state = WORK_IDLE;
    853  1.1  skrll 		dw->work.w_wq = NULL;
    854  1.1  skrll 		cv_broadcast(&wq->wq_cv);
    855  1.1  skrll 	}
    856  1.1  skrll 
    857  1.1  skrll 	/* Either way, the callout is done.  */
    858  1.1  skrll 	TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
    859             	callout_destroy(&dw->dw_callout);
    860             
    861             	mutex_exit(&wq->wq_lock);
    862             	linux_work_unlock(&dw->work);
    863             }
    864