Home | History | Annotate | Line # | Download | only in linux
linux_work.c revision 1.61
      1  1.61  riastrad /*	$NetBSD: linux_work.c,v 1.61 2022/04/09 23:43:31 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.61  riastrad __KERNEL_RCSID(0, "$NetBSD: linux_work.c,v 1.61 2022/04/09 23:43:31 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.44       ryo #ifndef _MODULE
     45  1.44       ryo #include <sys/once.h>
     46  1.44       ryo #endif
     47   1.1     skrll #include <sys/queue.h>
     48  1.41  riastrad #include <sys/sdt.h>
     49   1.1     skrll 
     50   1.1     skrll #include <linux/workqueue.h>
     51   1.1     skrll 
     52  1.39  riastrad TAILQ_HEAD(work_head, work_struct);
     53  1.39  riastrad TAILQ_HEAD(dwork_head, delayed_work);
     54  1.39  riastrad 
     55   1.1     skrll struct workqueue_struct {
     56  1.39  riastrad 	kmutex_t		wq_lock;
     57  1.39  riastrad 	kcondvar_t		wq_cv;
     58  1.39  riastrad 	struct dwork_head	wq_delayed; /* delayed work scheduled */
     59  1.55  riastrad 	struct work_head	wq_rcu;	    /* RCU work scheduled */
     60  1.39  riastrad 	struct work_head	wq_queue;   /* work to run */
     61  1.39  riastrad 	struct work_head	wq_dqueue;  /* delayed work to run now */
     62  1.39  riastrad 	struct work_struct	*wq_current_work;
     63  1.39  riastrad 	int			wq_flags;
     64  1.39  riastrad 	bool			wq_dying;
     65  1.39  riastrad 	uint64_t		wq_gen;
     66  1.39  riastrad 	struct lwp		*wq_lwp;
     67  1.50  riastrad 	const char		*wq_name;
     68   1.1     skrll };
     69   1.1     skrll 
     70  1.12  riastrad static void __dead	linux_workqueue_thread(void *);
     71  1.12  riastrad static void		linux_workqueue_timeout(void *);
     72  1.39  riastrad static bool		work_claimed(struct work_struct *,
     73  1.39  riastrad 			    struct workqueue_struct *);
     74  1.17  riastrad static struct workqueue_struct *
     75  1.39  riastrad 			work_queue(struct work_struct *);
     76  1.39  riastrad static bool		acquire_work(struct work_struct *,
     77  1.17  riastrad 			    struct workqueue_struct *);
     78  1.17  riastrad static void		release_work(struct work_struct *,
     79  1.17  riastrad 			    struct workqueue_struct *);
     80  1.33  riastrad static void		wait_for_current_work(struct work_struct *,
     81  1.33  riastrad 			    struct workqueue_struct *);
     82  1.30  riastrad static void		dw_callout_init(struct workqueue_struct *,
     83  1.30  riastrad 			    struct delayed_work *);
     84  1.31  riastrad static void		dw_callout_destroy(struct workqueue_struct *,
     85  1.31  riastrad 			    struct delayed_work *);
     86  1.23  riastrad static void		cancel_delayed_work_done(struct workqueue_struct *,
     87  1.23  riastrad 			    struct delayed_work *);
     88  1.12  riastrad 
     89  1.41  riastrad SDT_PROBE_DEFINE2(sdt, linux, work, acquire,
     90  1.41  riastrad     "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
     91  1.41  riastrad SDT_PROBE_DEFINE2(sdt, linux, work, release,
     92  1.41  riastrad     "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
     93  1.41  riastrad SDT_PROBE_DEFINE2(sdt, linux, work, queue,
     94  1.41  riastrad     "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
     95  1.55  riastrad SDT_PROBE_DEFINE2(sdt, linux, work, rcu,
     96  1.55  riastrad     "struct rcu_work *"/*work*/, "struct workqueue_struct *"/*wq*/);
     97  1.41  riastrad SDT_PROBE_DEFINE2(sdt, linux, work, cancel,
     98  1.41  riastrad     "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
     99  1.41  riastrad SDT_PROBE_DEFINE3(sdt, linux, work, schedule,
    100  1.41  riastrad     "struct delayed_work *"/*dw*/, "struct workqueue_struct *"/*wq*/,
    101  1.41  riastrad     "unsigned long"/*ticks*/);
    102  1.41  riastrad SDT_PROBE_DEFINE2(sdt, linux, work, timer,
    103  1.41  riastrad     "struct delayed_work *"/*dw*/, "struct workqueue_struct *"/*wq*/);
    104  1.41  riastrad SDT_PROBE_DEFINE2(sdt, linux, work, wait__start,
    105  1.41  riastrad     "struct delayed_work *"/*dw*/, "struct workqueue_struct *"/*wq*/);
    106  1.41  riastrad SDT_PROBE_DEFINE2(sdt, linux, work, wait__done,
    107  1.41  riastrad     "struct delayed_work *"/*dw*/, "struct workqueue_struct *"/*wq*/);
    108  1.41  riastrad SDT_PROBE_DEFINE2(sdt, linux, work, run,
    109  1.41  riastrad     "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
    110  1.41  riastrad SDT_PROBE_DEFINE2(sdt, linux, work, done,
    111  1.41  riastrad     "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
    112  1.41  riastrad SDT_PROBE_DEFINE1(sdt, linux, work, batch__start,
    113  1.41  riastrad     "struct workqueue_struct *"/*wq*/);
    114  1.41  riastrad SDT_PROBE_DEFINE1(sdt, linux, work, batch__done,
    115  1.41  riastrad     "struct workqueue_struct *"/*wq*/);
    116  1.60  riastrad SDT_PROBE_DEFINE1(sdt, linux, work, flush__self,
    117  1.60  riastrad     "struct workqueue_struct *"/*wq*/);
    118  1.41  riastrad SDT_PROBE_DEFINE1(sdt, linux, work, flush__start,
    119  1.41  riastrad     "struct workqueue_struct *"/*wq*/);
    120  1.41  riastrad SDT_PROBE_DEFINE1(sdt, linux, work, flush__done,
    121  1.41  riastrad     "struct workqueue_struct *"/*wq*/);
    122  1.41  riastrad 
    123  1.12  riastrad static specificdata_key_t workqueue_key __read_mostly;
    124  1.12  riastrad 
    125  1.53  riastrad struct workqueue_struct	*system_highpri_wq __read_mostly;
    126  1.12  riastrad struct workqueue_struct	*system_long_wq __read_mostly;
    127  1.12  riastrad struct workqueue_struct	*system_power_efficient_wq __read_mostly;
    128  1.47  riastrad struct workqueue_struct	*system_unbound_wq __read_mostly;
    129  1.53  riastrad struct workqueue_struct	*system_wq __read_mostly;
    130   1.3  riastrad 
    131  1.39  riastrad static inline uintptr_t
    132  1.39  riastrad atomic_cas_uintptr(volatile uintptr_t *p, uintptr_t old, uintptr_t new)
    133  1.39  riastrad {
    134  1.39  riastrad 
    135  1.39  riastrad 	return (uintptr_t)atomic_cas_ptr(p, (void *)old, (void *)new);
    136  1.39  riastrad }
    137  1.39  riastrad 
    138  1.36  riastrad /*
    139  1.36  riastrad  * linux_workqueue_init()
    140  1.36  riastrad  *
    141  1.36  riastrad  *	Initialize the Linux workqueue subsystem.  Return 0 on success,
    142  1.36  riastrad  *	NetBSD error on failure.
    143  1.36  riastrad  */
    144  1.44       ryo static int
    145  1.44       ryo linux_workqueue_init0(void)
    146   1.1     skrll {
    147  1.12  riastrad 	int error;
    148   1.3  riastrad 
    149  1.12  riastrad 	error = lwp_specific_key_create(&workqueue_key, NULL);
    150  1.12  riastrad 	if (error)
    151  1.53  riastrad 		goto out;
    152   1.1     skrll 
    153  1.53  riastrad 	system_highpri_wq = alloc_ordered_workqueue("lnxhipwq", 0);
    154  1.53  riastrad 	if (system_highpri_wq == NULL) {
    155  1.12  riastrad 		error = ENOMEM;
    156  1.53  riastrad 		goto out;
    157  1.12  riastrad 	}
    158   1.2  riastrad 
    159   1.2  riastrad 	system_long_wq = alloc_ordered_workqueue("lnxlngwq", 0);
    160  1.12  riastrad 	if (system_long_wq == NULL) {
    161  1.12  riastrad 		error = ENOMEM;
    162  1.53  riastrad 		goto out;
    163  1.12  riastrad 	}
    164   1.1     skrll 
    165   1.6  riastrad 	system_power_efficient_wq = alloc_ordered_workqueue("lnxpwrwq", 0);
    166  1.46  riastrad 	if (system_power_efficient_wq == NULL) {
    167  1.12  riastrad 		error = ENOMEM;
    168  1.53  riastrad 		goto out;
    169  1.12  riastrad 	}
    170   1.6  riastrad 
    171  1.47  riastrad 	system_unbound_wq = alloc_ordered_workqueue("lnxubdwq", 0);
    172  1.47  riastrad 	if (system_unbound_wq == NULL) {
    173  1.47  riastrad 		error = ENOMEM;
    174  1.53  riastrad 		goto out;
    175  1.47  riastrad 	}
    176  1.47  riastrad 
    177  1.53  riastrad 	system_wq = alloc_ordered_workqueue("lnxsyswq", 0);
    178  1.53  riastrad 	if (system_wq == NULL) {
    179  1.53  riastrad 		error = ENOMEM;
    180  1.53  riastrad 		goto out;
    181  1.53  riastrad 	}
    182  1.53  riastrad 
    183  1.53  riastrad 	/* Success!  */
    184  1.53  riastrad 	error = 0;
    185  1.53  riastrad 
    186  1.53  riastrad out:	if (error) {
    187  1.53  riastrad 		if (system_highpri_wq)
    188  1.53  riastrad 			destroy_workqueue(system_highpri_wq);
    189  1.53  riastrad 		if (system_long_wq)
    190  1.53  riastrad 			destroy_workqueue(system_long_wq);
    191  1.53  riastrad 		if (system_power_efficient_wq)
    192  1.53  riastrad 			destroy_workqueue(system_power_efficient_wq);
    193  1.53  riastrad 		if (system_unbound_wq)
    194  1.53  riastrad 			destroy_workqueue(system_unbound_wq);
    195  1.53  riastrad 		if (system_wq)
    196  1.53  riastrad 			destroy_workqueue(system_wq);
    197  1.53  riastrad 		if (workqueue_key)
    198  1.53  riastrad 			lwp_specific_key_delete(workqueue_key);
    199  1.53  riastrad 	}
    200   1.2  riastrad 
    201  1.12  riastrad 	return error;
    202   1.1     skrll }
    203   1.1     skrll 
    204  1.36  riastrad /*
    205  1.36  riastrad  * linux_workqueue_fini()
    206  1.36  riastrad  *
    207  1.36  riastrad  *	Destroy the Linux workqueue subsystem.  Never fails.
    208  1.36  riastrad  */
    209  1.44       ryo static void
    210  1.44       ryo linux_workqueue_fini0(void)
    211   1.1     skrll {
    212   1.2  riastrad 
    213  1.12  riastrad 	destroy_workqueue(system_power_efficient_wq);
    214   1.2  riastrad 	destroy_workqueue(system_long_wq);
    215   1.1     skrll 	destroy_workqueue(system_wq);
    216  1.12  riastrad 	lwp_specific_key_delete(workqueue_key);
    217   1.1     skrll }
    218  1.44       ryo 
    219  1.44       ryo #ifndef _MODULE
    220  1.44       ryo static ONCE_DECL(linux_workqueue_init_once);
    221  1.44       ryo #endif
    222  1.44       ryo 
    223  1.44       ryo int
    224  1.44       ryo linux_workqueue_init(void)
    225  1.44       ryo {
    226  1.44       ryo #ifdef _MODULE
    227  1.44       ryo 	return linux_workqueue_init0();
    228  1.44       ryo #else
    229  1.44       ryo 	return INIT_ONCE(&linux_workqueue_init_once, &linux_workqueue_init0);
    230  1.44       ryo #endif
    231  1.44       ryo }
    232  1.44       ryo 
    233  1.44       ryo void
    234  1.44       ryo linux_workqueue_fini(void)
    235  1.44       ryo {
    236  1.44       ryo #ifdef _MODULE
    237  1.44       ryo 	return linux_workqueue_fini0();
    238  1.44       ryo #else
    239  1.44       ryo 	return FINI_ONCE(&linux_workqueue_init_once, &linux_workqueue_fini0);
    240  1.44       ryo #endif
    241  1.44       ryo }
    242   1.1     skrll 
    243   1.1     skrll /*
    245   1.1     skrll  * Workqueues
    246   1.1     skrll  */
    247  1.36  riastrad 
    248  1.52  riastrad /*
    249  1.36  riastrad  * alloc_workqueue(name, flags, max_active)
    250  1.52  riastrad  *
    251  1.52  riastrad  *	Create a workqueue of the given name.  max_active is the
    252  1.52  riastrad  *	maximum number of work items in flight, or 0 for the default.
    253  1.52  riastrad  *	Return NULL on failure, pointer to struct workqueue_struct
    254  1.36  riastrad  *	object on success.
    255   1.1     skrll  */
    256  1.52  riastrad struct workqueue_struct *
    257   1.1     skrll alloc_workqueue(const char *name, int flags, unsigned max_active)
    258   1.1     skrll {
    259   1.1     skrll 	struct workqueue_struct *wq;
    260   1.1     skrll 	int error;
    261  1.52  riastrad 
    262   1.1     skrll 	KASSERT(max_active == 0 || max_active == 1);
    263  1.25  riastrad 
    264   1.1     skrll 	wq = kmem_zalloc(sizeof(*wq), KM_SLEEP);
    265  1.43  riastrad 
    266   1.1     skrll 	mutex_init(&wq->wq_lock, MUTEX_DEFAULT, IPL_VM);
    267   1.1     skrll 	cv_init(&wq->wq_cv, name);
    268  1.55  riastrad 	TAILQ_INIT(&wq->wq_delayed);
    269  1.12  riastrad 	TAILQ_INIT(&wq->wq_rcu);
    270  1.39  riastrad 	TAILQ_INIT(&wq->wq_queue);
    271   1.1     skrll 	TAILQ_INIT(&wq->wq_dqueue);
    272  1.25  riastrad 	wq->wq_current_work = NULL;
    273  1.25  riastrad 	wq->wq_flags = 0;
    274  1.37  riastrad 	wq->wq_dying = false;
    275  1.37  riastrad 	wq->wq_gen = 0;
    276  1.50  riastrad 	wq->wq_lwp = NULL;
    277   1.1     skrll 	wq->wq_name = name;
    278  1.12  riastrad 
    279  1.12  riastrad 	error = kthread_create(PRI_NONE,
    280  1.12  riastrad 	    KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL,
    281  1.12  riastrad 	    &linux_workqueue_thread, wq, &wq->wq_lwp, "%s", name);
    282  1.12  riastrad 	if (error)
    283   1.3  riastrad 		goto fail0;
    284   1.1     skrll 
    285  1.12  riastrad 	return wq;
    286  1.39  riastrad 
    287  1.39  riastrad fail0:	KASSERT(TAILQ_EMPTY(&wq->wq_dqueue));
    288  1.55  riastrad 	KASSERT(TAILQ_EMPTY(&wq->wq_queue));
    289  1.12  riastrad 	KASSERT(TAILQ_EMPTY(&wq->wq_rcu));
    290  1.12  riastrad 	KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
    291  1.12  riastrad 	cv_destroy(&wq->wq_cv);
    292  1.12  riastrad 	mutex_destroy(&wq->wq_lock);
    293  1.12  riastrad 	kmem_free(wq, sizeof(*wq));
    294   1.1     skrll 	return NULL;
    295   1.1     skrll }
    296  1.36  riastrad 
    297  1.52  riastrad /*
    298  1.52  riastrad  * alloc_ordered_workqueue(name, flags)
    299  1.52  riastrad  *
    300  1.52  riastrad  *	Same as alloc_workqueue(name, flags, 1).
    301  1.52  riastrad  */
    302  1.52  riastrad struct workqueue_struct *
    303  1.52  riastrad alloc_ordered_workqueue(const char *name, int flags)
    304  1.52  riastrad {
    305  1.52  riastrad 
    306  1.52  riastrad 	return alloc_workqueue(name, flags, 1);
    307  1.52  riastrad }
    308  1.52  riastrad 
    309  1.36  riastrad /*
    310  1.36  riastrad  * destroy_workqueue(wq)
    311  1.36  riastrad  *
    312  1.36  riastrad  *	Destroy a workqueue created with wq.  Cancel any pending
    313  1.36  riastrad  *	delayed work.  Wait for all queued work to complete.
    314  1.36  riastrad  *
    315  1.36  riastrad  *	May sleep.
    316   1.1     skrll  */
    317   1.1     skrll void
    318   1.1     skrll destroy_workqueue(struct workqueue_struct *wq)
    319   1.1     skrll {
    320   1.1     skrll 
    321  1.12  riastrad 	/*
    322  1.12  riastrad 	 * Cancel all delayed work.  We do this first because any
    323  1.12  riastrad 	 * delayed work that that has already timed out, which we can't
    324   1.1     skrll 	 * cancel, may have queued new work.
    325  1.26  riastrad 	 */
    326  1.26  riastrad 	mutex_enter(&wq->wq_lock);
    327  1.26  riastrad 	while (!TAILQ_EMPTY(&wq->wq_delayed)) {
    328   1.1     skrll 		struct delayed_work *const dw = TAILQ_FIRST(&wq->wq_delayed);
    329  1.39  riastrad 
    330  1.26  riastrad 		KASSERT(work_queue(&dw->work) == wq);
    331  1.26  riastrad 		KASSERTMSG((dw->dw_state == DELAYED_WORK_SCHEDULED ||
    332  1.26  riastrad 			dw->dw_state == DELAYED_WORK_RESCHEDULED ||
    333  1.26  riastrad 			dw->dw_state == DELAYED_WORK_CANCELLED),
    334  1.26  riastrad 		    "delayed work %p in bad state: %d",
    335  1.26  riastrad 		    dw, dw->dw_state);
    336  1.26  riastrad 
    337  1.26  riastrad 		/*
    338  1.26  riastrad 		 * Mark it cancelled and try to stop the callout before
    339  1.26  riastrad 		 * it starts.
    340  1.26  riastrad 		 *
    341  1.26  riastrad 		 * If it's too late and the callout has already begun
    342  1.26  riastrad 		 * to execute, then it will notice that we asked to
    343  1.26  riastrad 		 * cancel it and remove itself from the queue before
    344  1.26  riastrad 		 * returning.
    345  1.26  riastrad 		 *
    346  1.26  riastrad 		 * If we stopped the callout before it started,
    347  1.26  riastrad 		 * however, then we can safely destroy the callout and
    348  1.26  riastrad 		 * dissociate it from the workqueue ourselves.
    349  1.41  riastrad 		 */
    350  1.26  riastrad 		SDT_PROBE2(sdt, linux, work, cancel,  &dw->work, wq);
    351  1.26  riastrad 		dw->dw_state = DELAYED_WORK_CANCELLED;
    352  1.26  riastrad 		if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
    353  1.26  riastrad 			cancel_delayed_work_done(wq, dw);
    354  1.26  riastrad 	}
    355   1.1     skrll 	mutex_exit(&wq->wq_lock);
    356  1.55  riastrad 
    357  1.55  riastrad 	/* Wait for all scheduled RCU work to complete.  */
    358  1.55  riastrad 	mutex_enter(&wq->wq_lock);
    359  1.55  riastrad 	while (!TAILQ_EMPTY(&wq->wq_rcu))
    360  1.55  riastrad 		cv_wait(&wq->wq_cv, &wq->wq_lock);
    361  1.55  riastrad 	mutex_exit(&wq->wq_lock);
    362  1.26  riastrad 
    363  1.26  riastrad 	/*
    364  1.26  riastrad 	 * At this point, no new work can be put on the queue.
    365   1.1     skrll 	 */
    366  1.12  riastrad 
    367  1.12  riastrad 	/* Tell the thread to exit.  */
    368  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    369  1.12  riastrad 	wq->wq_dying = true;
    370  1.12  riastrad 	cv_broadcast(&wq->wq_cv);
    371  1.12  riastrad 	mutex_exit(&wq->wq_lock);
    372  1.12  riastrad 
    373  1.12  riastrad 	/* Wait for it to exit.  */
    374  1.12  riastrad 	(void)kthread_join(wq->wq_lwp);
    375  1.25  riastrad 
    376  1.25  riastrad 	KASSERT(wq->wq_dying);
    377   1.1     skrll 	KASSERT(wq->wq_flags == 0);
    378  1.39  riastrad 	KASSERT(wq->wq_current_work == NULL);
    379  1.12  riastrad 	KASSERT(TAILQ_EMPTY(&wq->wq_dqueue));
    380  1.55  riastrad 	KASSERT(TAILQ_EMPTY(&wq->wq_queue));
    381  1.12  riastrad 	KASSERT(TAILQ_EMPTY(&wq->wq_rcu));
    382   1.1     skrll 	KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
    383   1.1     skrll 	cv_destroy(&wq->wq_cv);
    384   1.1     skrll 	mutex_destroy(&wq->wq_lock);
    385   1.1     skrll 
    386   1.1     skrll 	kmem_free(wq, sizeof(*wq));
    387   1.1     skrll }
    388   1.1     skrll 
    389  1.12  riastrad /*
    391   1.1     skrll  * Work thread and callout
    392  1.36  riastrad  */
    393  1.36  riastrad 
    394  1.36  riastrad /*
    395  1.36  riastrad  * linux_workqueue_thread(cookie)
    396  1.36  riastrad  *
    397  1.36  riastrad  *	Main function for a workqueue's worker thread.  Waits until
    398  1.36  riastrad  *	there is work queued, grabs a batch of work off the queue,
    399  1.36  riastrad  *	executes it all, bumps the generation number, and repeats,
    400  1.12  riastrad  *	until dying.
    401  1.12  riastrad  */
    402   1.1     skrll static void __dead
    403  1.12  riastrad linux_workqueue_thread(void *cookie)
    404  1.45  riastrad {
    405  1.45  riastrad 	struct workqueue_struct *const wq = cookie;
    406  1.39  riastrad 	struct work_head *const q[2] = { &wq->wq_queue, &wq->wq_dqueue };
    407   1.1     skrll 	struct work_struct marker, *work;
    408  1.12  riastrad 	unsigned i;
    409   1.1     skrll 
    410  1.12  riastrad 	lwp_setspecific(workqueue_key, wq);
    411  1.12  riastrad 
    412  1.26  riastrad 	mutex_enter(&wq->wq_lock);
    413  1.26  riastrad 	for (;;) {
    414  1.26  riastrad 		/*
    415  1.26  riastrad 		 * Wait until there's activity.  If there's no work and
    416  1.42  riastrad 		 * we're dying, stop here.
    417  1.42  riastrad 		 */
    418  1.42  riastrad 		if (TAILQ_EMPTY(&wq->wq_queue) &&
    419  1.42  riastrad 		    TAILQ_EMPTY(&wq->wq_dqueue)) {
    420  1.12  riastrad 			if (wq->wq_dying)
    421  1.42  riastrad 				break;
    422  1.26  riastrad 			cv_wait(&wq->wq_cv, &wq->wq_lock);
    423   1.1     skrll 			continue;
    424  1.45  riastrad 		}
    425  1.45  riastrad 
    426  1.45  riastrad 		/*
    427  1.45  riastrad 		 * Start a batch of work.  Use a marker to delimit when
    428  1.45  riastrad 		 * the batch ends so we can advance the generation
    429  1.41  riastrad 		 * after the batch.
    430  1.39  riastrad 		 */
    431  1.45  riastrad 		SDT_PROBE1(sdt, linux, work, batch__start,  wq);
    432  1.45  riastrad 		for (i = 0; i < 2; i++) {
    433  1.45  riastrad 			if (TAILQ_EMPTY(q[i]))
    434  1.45  riastrad 				continue;
    435  1.39  riastrad 			TAILQ_INSERT_TAIL(q[i], &marker, work_entry);
    436  1.39  riastrad 			while ((work = TAILQ_FIRST(q[i])) != &marker) {
    437  1.39  riastrad 				void (*func)(struct work_struct *);
    438  1.39  riastrad 
    439  1.45  riastrad 				KASSERT(work_queue(work) == wq);
    440  1.39  riastrad 				KASSERT(work_claimed(work, wq));
    441  1.39  riastrad 				KASSERTMSG((q[i] != &wq->wq_dqueue ||
    442  1.39  riastrad 					container_of(work, struct delayed_work,
    443  1.39  riastrad 					    work)->dw_state ==
    444  1.39  riastrad 					DELAYED_WORK_IDLE),
    445  1.39  riastrad 				    "delayed work %p queued and scheduled",
    446  1.39  riastrad 				    work);
    447  1.39  riastrad 
    448  1.39  riastrad 				TAILQ_REMOVE(q[i], work, work_entry);
    449  1.39  riastrad 				KASSERT(wq->wq_current_work == NULL);
    450  1.39  riastrad 				wq->wq_current_work = work;
    451  1.39  riastrad 				func = work->func;
    452  1.12  riastrad 				release_work(work, wq);
    453  1.39  riastrad 				/* Can't dereference work after this point.  */
    454  1.41  riastrad 
    455  1.39  riastrad 				mutex_exit(&wq->wq_lock);
    456  1.41  riastrad 				SDT_PROBE2(sdt, linux, work, run,  work, wq);
    457  1.39  riastrad 				(*func)(work);
    458  1.39  riastrad 				SDT_PROBE2(sdt, linux, work, done,  work, wq);
    459  1.39  riastrad 				mutex_enter(&wq->wq_lock);
    460  1.39  riastrad 
    461  1.39  riastrad 				KASSERT(wq->wq_current_work == work);
    462  1.39  riastrad 				wq->wq_current_work = NULL;
    463  1.45  riastrad 				cv_broadcast(&wq->wq_cv);
    464  1.12  riastrad 			}
    465   1.1     skrll 			TAILQ_REMOVE(q[i], &marker, work_entry);
    466  1.57  riastrad 		}
    467  1.12  riastrad 
    468  1.12  riastrad 		/* Notify cancel that we've completed a batch of work.  */
    469  1.41  riastrad 		wq->wq_gen++;
    470   1.1     skrll 		cv_broadcast(&wq->wq_cv);
    471  1.12  riastrad 		SDT_PROBE1(sdt, linux, work, batch__done,  wq);
    472   1.1     skrll 	}
    473  1.12  riastrad 	mutex_exit(&wq->wq_lock);
    474   1.1     skrll 
    475   1.1     skrll 	kthread_exit(0);
    476  1.36  riastrad }
    477  1.36  riastrad 
    478  1.36  riastrad /*
    479  1.36  riastrad  * linux_workqueue_timeout(cookie)
    480  1.36  riastrad  *
    481  1.36  riastrad  *	Delayed work timeout callback.
    482  1.36  riastrad  *
    483  1.36  riastrad  *	- If scheduled, queue it.
    484  1.36  riastrad  *	- If rescheduled, callout_schedule ourselves again.
    485  1.36  riastrad  *	- If cancelled, destroy the callout and release the work from
    486   1.1     skrll  *        the workqueue.
    487  1.12  riastrad  */
    488   1.1     skrll static void
    489  1.12  riastrad linux_workqueue_timeout(void *cookie)
    490  1.39  riastrad {
    491   1.1     skrll 	struct delayed_work *const dw = cookie;
    492  1.39  riastrad 	struct workqueue_struct *const wq = work_queue(&dw->work);
    493  1.39  riastrad 
    494  1.39  riastrad 	KASSERTMSG(wq != NULL,
    495  1.14  riastrad 	    "delayed work %p state %d resched %d",
    496  1.41  riastrad 	    dw, dw->dw_state, dw->dw_resched);
    497  1.41  riastrad 
    498  1.12  riastrad 	SDT_PROBE2(sdt, linux, work, timer,  dw, wq);
    499  1.39  riastrad 
    500  1.12  riastrad 	mutex_enter(&wq->wq_lock);
    501  1.12  riastrad 	KASSERT(work_queue(&dw->work) == wq);
    502  1.12  riastrad 	switch (dw->dw_state) {
    503  1.12  riastrad 	case DELAYED_WORK_IDLE:
    504  1.31  riastrad 		panic("delayed work callout uninitialized: %p", dw);
    505  1.39  riastrad 	case DELAYED_WORK_SCHEDULED:
    506  1.12  riastrad 		dw_callout_destroy(wq, dw);
    507  1.41  riastrad 		TAILQ_INSERT_TAIL(&wq->wq_dqueue, &dw->work, work_entry);
    508  1.12  riastrad 		cv_broadcast(&wq->wq_cv);
    509  1.12  riastrad 		SDT_PROBE2(sdt, linux, work, queue,  &dw->work, wq);
    510  1.35  riastrad 		break;
    511  1.35  riastrad 	case DELAYED_WORK_RESCHEDULED:
    512  1.12  riastrad 		KASSERT(dw->dw_resched >= 0);
    513  1.35  riastrad 		callout_schedule(&dw->dw_callout, dw->dw_resched);
    514  1.12  riastrad 		dw->dw_state = DELAYED_WORK_SCHEDULED;
    515  1.12  riastrad 		dw->dw_resched = -1;
    516  1.23  riastrad 		break;
    517  1.39  riastrad 	case DELAYED_WORK_CANCELLED:
    518  1.22  riastrad 		cancel_delayed_work_done(wq, dw);
    519  1.12  riastrad 		/* Can't dereference dw after this point.  */
    520  1.12  riastrad 		goto out;
    521  1.12  riastrad 	default:
    522  1.15  riastrad 		panic("delayed work callout in bad state: %p", dw);
    523  1.15  riastrad 	}
    524  1.22  riastrad 	KASSERT(dw->dw_state == DELAYED_WORK_IDLE ||
    525   1.1     skrll 	    dw->dw_state == DELAYED_WORK_SCHEDULED);
    526   1.1     skrll out:	mutex_exit(&wq->wq_lock);
    527  1.36  riastrad }
    528  1.36  riastrad 
    529  1.36  riastrad /*
    530  1.36  riastrad  * current_work()
    531  1.36  riastrad  *
    532  1.36  riastrad  *	If in a workqueue worker thread, return the work it is
    533  1.12  riastrad  *	currently executing.  Otherwise return NULL.
    534  1.12  riastrad  */
    535   1.1     skrll struct work_struct *
    536  1.12  riastrad current_work(void)
    537   1.1     skrll {
    538  1.12  riastrad 	struct workqueue_struct *wq = lwp_getspecific(workqueue_key);
    539  1.12  riastrad 
    540  1.12  riastrad 	/* If we're not a workqueue thread, then there's no work.  */
    541   1.1     skrll 	if (wq == NULL)
    542  1.12  riastrad 		return NULL;
    543  1.12  riastrad 
    544  1.12  riastrad 	/*
    545  1.12  riastrad 	 * Otherwise, this should be possible only while work is in
    546  1.12  riastrad 	 * progress.  Return the current work item.
    547  1.12  riastrad 	 */
    548   1.1     skrll 	KASSERT(wq->wq_current_work != NULL);
    549   1.1     skrll 	return wq->wq_current_work;
    550   1.1     skrll }
    551   1.1     skrll 
    552   1.1     skrll /*
    554  1.36  riastrad  * Work
    555  1.36  riastrad  */
    556  1.36  riastrad 
    557  1.36  riastrad /*
    558  1.36  riastrad  * INIT_WORK(work, fn)
    559  1.36  riastrad  *
    560   1.1     skrll  *	Initialize work for use with a workqueue to call fn in a worker
    561   1.1     skrll  *	thread.  There is no corresponding destruction operation.
    562   1.1     skrll  */
    563   1.1     skrll void
    564  1.39  riastrad INIT_WORK(struct work_struct *work, void (*fn)(struct work_struct *))
    565   1.4  riastrad {
    566   1.1     skrll 
    567   1.1     skrll 	work->work_owner = 0;
    568  1.36  riastrad 	work->func = fn;
    569  1.39  riastrad }
    570  1.39  riastrad 
    571  1.39  riastrad /*
    572  1.39  riastrad  * work_claimed(work, wq)
    573  1.39  riastrad  *
    574  1.39  riastrad  *	True if work is currently claimed by a workqueue, meaning it is
    575  1.39  riastrad  *	either on the queue or scheduled in a callout.  The workqueue
    576  1.39  riastrad  *	must be wq, and caller must hold wq's lock.
    577  1.39  riastrad  */
    578  1.39  riastrad static bool
    579  1.39  riastrad work_claimed(struct work_struct *work, struct workqueue_struct *wq)
    580  1.39  riastrad {
    581  1.39  riastrad 
    582  1.54  riastrad 	KASSERT(work_queue(work) == wq);
    583  1.39  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    584  1.39  riastrad 
    585  1.39  riastrad 	return atomic_load_relaxed(&work->work_owner) & 1;
    586  1.49  riastrad }
    587  1.49  riastrad 
    588  1.49  riastrad /*
    589  1.49  riastrad  * work_pending(work)
    590  1.49  riastrad  *
    591  1.49  riastrad  *	True if work is currently claimed by any workqueue, scheduled
    592  1.51  riastrad  *	to run on that workqueue.
    593  1.49  riastrad  */
    594  1.49  riastrad bool
    595  1.54  riastrad work_pending(const struct work_struct *work)
    596  1.49  riastrad {
    597  1.49  riastrad 
    598  1.49  riastrad 	return atomic_load_relaxed(&work->work_owner) & 1;
    599  1.39  riastrad }
    600  1.39  riastrad 
    601  1.39  riastrad /*
    602  1.39  riastrad  * work_queue(work)
    603  1.39  riastrad  *
    604  1.39  riastrad  *	Return the last queue that work was queued on, or NULL if it
    605  1.39  riastrad  *	was never queued.
    606  1.39  riastrad  */
    607  1.39  riastrad static struct workqueue_struct *
    608  1.54  riastrad work_queue(struct work_struct *work)
    609  1.54  riastrad {
    610  1.39  riastrad 
    611  1.39  riastrad 	return (struct workqueue_struct *)
    612  1.39  riastrad 	    (atomic_load_relaxed(&work->work_owner) & ~(uintptr_t)1);
    613  1.36  riastrad }
    614  1.36  riastrad 
    615  1.39  riastrad /*
    616  1.39  riastrad  * acquire_work(work, wq)
    617  1.39  riastrad  *
    618  1.39  riastrad  *	Try to claim work for wq.  If work is already claimed, it must
    619  1.36  riastrad  *	be claimed by wq; return false.  If work is not already
    620  1.36  riastrad  *	claimed, claim it, issue a memory barrier to match any prior
    621  1.36  riastrad  *	release_work, and return true.
    622  1.39  riastrad  *
    623  1.17  riastrad  *	Caller must hold wq's lock.
    624  1.17  riastrad  */
    625  1.39  riastrad static bool
    626  1.17  riastrad acquire_work(struct work_struct *work, struct workqueue_struct *wq)
    627  1.17  riastrad {
    628  1.39  riastrad 	uintptr_t owner0, owner;
    629  1.17  riastrad 
    630  1.39  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    631  1.39  riastrad 	KASSERT(((uintptr_t)wq & 1) == 0);
    632  1.54  riastrad 
    633  1.39  riastrad 	owner = (uintptr_t)wq | 1;
    634  1.39  riastrad 	do {
    635  1.39  riastrad 		owner0 = atomic_load_relaxed(&work->work_owner);
    636  1.39  riastrad 		if (owner0 & 1) {
    637  1.39  riastrad 			KASSERT((owner0 & ~(uintptr_t)1) == (uintptr_t)wq);
    638  1.39  riastrad 			return false;
    639  1.39  riastrad 		}
    640  1.39  riastrad 		KASSERT(owner0 == (uintptr_t)NULL || owner0 == (uintptr_t)wq);
    641  1.39  riastrad 	} while (atomic_cas_uintptr(&work->work_owner, owner0, owner) !=
    642  1.61  riastrad 	    owner0);
    643  1.41  riastrad 
    644  1.39  riastrad 	KASSERT(work_queue(work) == wq);
    645  1.17  riastrad 	membar_acquire();
    646  1.17  riastrad 	SDT_PROBE2(sdt, linux, work, acquire,  work, wq);
    647  1.36  riastrad 	return true;
    648  1.36  riastrad }
    649  1.36  riastrad 
    650  1.36  riastrad /*
    651  1.36  riastrad  * release_work(work, wq)
    652  1.36  riastrad  *
    653  1.36  riastrad  *	Issue a memory barrier to match any subsequent acquire_work and
    654  1.36  riastrad  *	dissociate work from wq.
    655  1.17  riastrad  *
    656  1.17  riastrad  *	Caller must hold wq's lock and work must be associated with wq.
    657  1.17  riastrad  */
    658  1.17  riastrad static void
    659  1.39  riastrad release_work(struct work_struct *work, struct workqueue_struct *wq)
    660  1.17  riastrad {
    661  1.17  riastrad 
    662  1.41  riastrad 	KASSERT(work_queue(work) == wq);
    663  1.61  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    664  1.39  riastrad 
    665  1.39  riastrad 	SDT_PROBE2(sdt, linux, work, release,  work, wq);
    666  1.39  riastrad 	membar_release();
    667  1.54  riastrad 
    668  1.39  riastrad 	/*
    669  1.39  riastrad 	 * Non-interlocked r/m/w is safe here because nobody else can
    670  1.54  riastrad 	 * write to this while the claimed bit is set and the workqueue
    671  1.54  riastrad 	 * lock is held.
    672  1.17  riastrad 	 */
    673  1.17  riastrad 	atomic_store_relaxed(&work->work_owner,
    674  1.36  riastrad 	    atomic_load_relaxed(&work->work_owner) & ~(uintptr_t)1);
    675  1.36  riastrad }
    676  1.36  riastrad 
    677  1.36  riastrad /*
    678  1.36  riastrad  * schedule_work(work)
    679  1.36  riastrad  *
    680  1.36  riastrad  *	If work is not already queued on system_wq, queue it to be run
    681  1.36  riastrad  *	by system_wq's worker thread when it next can.  True if it was
    682  1.36  riastrad  *	newly queued, false if it was already queued.  If the work was
    683  1.36  riastrad  *	already running, queue it to run again.
    684  1.36  riastrad  *
    685   1.1     skrll  *	Caller must ensure work is not queued to run on a different
    686   1.1     skrll  *	workqueue.
    687   1.1     skrll  */
    688  1.12  riastrad bool
    689   1.1     skrll schedule_work(struct work_struct *work)
    690   1.1     skrll {
    691   1.1     skrll 
    692  1.36  riastrad 	return queue_work(system_wq, work);
    693  1.36  riastrad }
    694  1.36  riastrad 
    695  1.36  riastrad /*
    696  1.36  riastrad  * queue_work(wq, work)
    697  1.36  riastrad  *
    698  1.36  riastrad  *	If work is not already queued on wq, queue it to be run by wq's
    699  1.36  riastrad  *	worker thread when it next can.  True if it was newly queued,
    700  1.36  riastrad  *	false if it was already queued.  If the work was already
    701  1.36  riastrad  *	running, queue it to run again.
    702  1.36  riastrad  *
    703   1.1     skrll  *	Caller must ensure work is not queued to run on a different
    704   1.1     skrll  *	workqueue.
    705   1.1     skrll  */
    706   1.1     skrll bool
    707   1.1     skrll queue_work(struct workqueue_struct *wq, struct work_struct *work)
    708   1.1     skrll {
    709   1.1     skrll 	bool newly_queued;
    710  1.12  riastrad 
    711  1.39  riastrad 	KASSERT(wq != NULL);
    712  1.29  riastrad 
    713  1.29  riastrad 	mutex_enter(&wq->wq_lock);
    714  1.29  riastrad 	if (__predict_true(acquire_work(work, wq))) {
    715  1.29  riastrad 		/*
    716  1.29  riastrad 		 * It wasn't on any workqueue at all.  Put it on this
    717  1.12  riastrad 		 * one, and signal the worker thread that there is work
    718  1.39  riastrad 		 * to do.
    719  1.41  riastrad 		 */
    720   1.1     skrll 		TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry);
    721  1.12  riastrad 		cv_broadcast(&wq->wq_cv);
    722  1.29  riastrad 		SDT_PROBE2(sdt, linux, work, queue,  work, wq);
    723  1.39  riastrad 		newly_queued = true;
    724  1.39  riastrad 	} else {
    725  1.29  riastrad 		/*
    726  1.39  riastrad 		 * It was already on this workqueue.  Nothing to do
    727   1.1     skrll 		 * since it is already queued.
    728  1.12  riastrad 		 */
    729   1.1     skrll 		newly_queued = false;
    730   1.1     skrll 	}
    731   1.1     skrll 	mutex_exit(&wq->wq_lock);
    732   1.1     skrll 
    733  1.36  riastrad 	return newly_queued;
    734  1.36  riastrad }
    735  1.36  riastrad 
    736  1.36  riastrad /*
    737  1.39  riastrad  * cancel_work(work)
    738  1.39  riastrad  *
    739  1.36  riastrad  *	If work was queued, remove it from the queue and return true.
    740   1.1     skrll  *	If work was not queued, return false.  Work may still be
    741  1.12  riastrad  *	running when this returns.
    742   1.1     skrll  */
    743  1.12  riastrad bool
    744   1.1     skrll cancel_work(struct work_struct *work)
    745   1.1     skrll {
    746  1.13  riastrad 	struct workqueue_struct *wq;
    747  1.39  riastrad 	bool cancelled_p = false;
    748  1.13  riastrad 
    749  1.13  riastrad 	/* If there's no workqueue, nothing to cancel.   */
    750  1.12  riastrad 	if ((wq = work_queue(work)) == NULL)
    751  1.39  riastrad 		goto out;
    752  1.29  riastrad 
    753  1.29  riastrad 	mutex_enter(&wq->wq_lock);
    754  1.29  riastrad 	if (__predict_false(work_queue(work) != wq)) {
    755  1.29  riastrad 		/*
    756  1.29  riastrad 		 * It has finished execution or been cancelled by
    757  1.12  riastrad 		 * another thread, and has been moved off the
    758  1.12  riastrad 		 * workqueue, so it's too to cancel.
    759  1.39  riastrad 		 */
    760  1.39  riastrad 		cancelled_p = false;
    761  1.39  riastrad 	} else {
    762  1.39  riastrad 		/* Check whether it's on the queue.  */
    763  1.39  riastrad 		if (work_claimed(work, wq)) {
    764  1.39  riastrad 			/*
    765  1.39  riastrad 			 * It is still on the queue.  Take it off the
    766  1.41  riastrad 			 * queue and report successful cancellation.
    767  1.39  riastrad 			 */
    768  1.39  riastrad 			TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
    769  1.39  riastrad 			SDT_PROBE2(sdt, linux, work, cancel,  work, wq);
    770  1.39  riastrad 			release_work(work, wq);
    771  1.39  riastrad 			/* Can't dereference work after this point.  */
    772  1.39  riastrad 			cancelled_p = true;
    773  1.39  riastrad 		} else {
    774   1.1     skrll 			/* Not on the queue.  Couldn't cancel it.  */
    775  1.12  riastrad 			cancelled_p = false;
    776   1.1     skrll 		}
    777  1.13  riastrad 	}
    778   1.1     skrll 	mutex_exit(&wq->wq_lock);
    779   1.1     skrll 
    780  1.36  riastrad out:	return cancelled_p;
    781  1.36  riastrad }
    782  1.36  riastrad 
    783  1.36  riastrad /*
    784  1.39  riastrad  * cancel_work_sync(work)
    785  1.36  riastrad  *
    786  1.36  riastrad  *	If work was queued, remove it from the queue and return true.
    787  1.36  riastrad  *	If work was not queued, return false.  Either way, if work is
    788  1.36  riastrad  *	currently running, wait for it to complete.
    789  1.12  riastrad  *
    790  1.12  riastrad  *	May sleep.
    791   1.1     skrll  */
    792   1.1     skrll bool
    793  1.12  riastrad cancel_work_sync(struct work_struct *work)
    794   1.1     skrll {
    795  1.13  riastrad 	struct workqueue_struct *wq;
    796  1.39  riastrad 	bool cancelled_p = false;
    797  1.13  riastrad 
    798  1.13  riastrad 	/* If there's no workqueue, nothing to cancel.   */
    799   1.1     skrll 	if ((wq = work_queue(work)) == NULL)
    800  1.39  riastrad 		goto out;
    801  1.29  riastrad 
    802  1.29  riastrad 	mutex_enter(&wq->wq_lock);
    803  1.29  riastrad 	if (__predict_false(work_queue(work) != wq)) {
    804  1.39  riastrad 		/*
    805  1.29  riastrad 		 * It has finished execution or been cancelled by
    806  1.12  riastrad 		 * another thread, and has been moved off the
    807  1.12  riastrad 		 * workqueue, so it's too late to cancel.
    808  1.39  riastrad 		 */
    809  1.39  riastrad 		cancelled_p = false;
    810  1.39  riastrad 	} else {
    811  1.39  riastrad 		/* Check whether it's on the queue.  */
    812  1.39  riastrad 		if (work_claimed(work, wq)) {
    813  1.39  riastrad 			/*
    814  1.39  riastrad 			 * It is still on the queue.  Take it off the
    815  1.41  riastrad 			 * queue and report successful cancellation.
    816  1.39  riastrad 			 */
    817  1.39  riastrad 			TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
    818  1.39  riastrad 			SDT_PROBE2(sdt, linux, work, cancel,  work, wq);
    819  1.39  riastrad 			release_work(work, wq);
    820  1.39  riastrad 			/* Can't dereference work after this point.  */
    821  1.39  riastrad 			cancelled_p = true;
    822  1.39  riastrad 		} else {
    823  1.39  riastrad 			/* Not on the queue.  Couldn't cancel it.  */
    824  1.39  riastrad 			cancelled_p = false;
    825  1.39  riastrad 		}
    826  1.12  riastrad 		/* If it's still running, wait for it to complete.  */
    827   1.1     skrll 		if (wq->wq_current_work == work)
    828   1.1     skrll 			wait_for_current_work(work, wq);
    829  1.13  riastrad 	}
    830   1.1     skrll 	mutex_exit(&wq->wq_lock);
    831  1.33  riastrad 
    832  1.33  riastrad out:	return cancelled_p;
    833  1.33  riastrad }
    834  1.33  riastrad 
    835  1.33  riastrad /*
    836  1.39  riastrad  * wait_for_current_work(work, wq)
    837  1.39  riastrad  *
    838  1.33  riastrad  *	wq must be currently executing work.  Wait for it to finish.
    839  1.33  riastrad  *
    840  1.33  riastrad  *	Does not dereference work.
    841  1.33  riastrad  */
    842  1.33  riastrad static void
    843  1.33  riastrad wait_for_current_work(struct work_struct *work, struct workqueue_struct *wq)
    844  1.33  riastrad {
    845  1.33  riastrad 	uint64_t gen;
    846  1.33  riastrad 
    847  1.33  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    848  1.41  riastrad 	KASSERT(wq->wq_current_work == work);
    849  1.33  riastrad 
    850  1.33  riastrad 	/* Wait only one generation in case it gets requeued quickly.  */
    851  1.33  riastrad 	SDT_PROBE2(sdt, linux, work, wait__start,  work, wq);
    852  1.33  riastrad 	gen = wq->wq_gen;
    853  1.41  riastrad 	do {
    854  1.33  riastrad 		cv_wait(&wq->wq_cv, &wq->wq_lock);
    855   1.1     skrll 	} while (wq->wq_current_work == work && wq->wq_gen == gen);
    856   1.1     skrll 	SDT_PROBE2(sdt, linux, work, wait__done,  work, wq);
    857   1.1     skrll }
    858   1.1     skrll 
    859   1.1     skrll /*
    861  1.36  riastrad  * Delayed work
    862  1.36  riastrad  */
    863  1.36  riastrad 
    864  1.36  riastrad /*
    865  1.36  riastrad  * INIT_DELAYED_WORK(dw, fn)
    866  1.36  riastrad  *
    867   1.1     skrll  *	Initialize dw for use with a workqueue to call fn in a worker
    868   1.1     skrll  *	thread after a delay.  There is no corresponding destruction
    869   1.1     skrll  *	operation.
    870  1.12  riastrad  */
    871   1.1     skrll void
    872  1.12  riastrad INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work_struct *))
    873  1.35  riastrad {
    874  1.12  riastrad 
    875  1.12  riastrad 	INIT_WORK(&dw->work, fn);
    876  1.12  riastrad 	dw->dw_state = DELAYED_WORK_IDLE;
    877  1.12  riastrad 	dw->dw_resched = -1;
    878  1.12  riastrad 
    879  1.12  riastrad 	/*
    880  1.12  riastrad 	 * Defer callout_init until we are going to schedule the
    881   1.1     skrll 	 * callout, which can then callout_destroy it, because
    882   1.1     skrll 	 * otherwise since there's no DESTROY_DELAYED_WORK or anything
    883  1.36  riastrad 	 * we have no opportunity to call callout_destroy.
    884  1.36  riastrad 	 */
    885  1.36  riastrad }
    886  1.36  riastrad 
    887  1.36  riastrad /*
    888  1.36  riastrad  * schedule_delayed_work(dw, ticks)
    889  1.36  riastrad  *
    890  1.36  riastrad  *	If it is not currently scheduled, schedule dw to run after
    891  1.36  riastrad  *	ticks on system_wq.  If currently executing and not already
    892  1.36  riastrad  *	rescheduled, reschedule it.  True if it was newly scheduled,
    893  1.36  riastrad  *	false if it was already scheduled.
    894   1.1     skrll  *
    895   1.1     skrll  *	If ticks == 0, queue it to run as soon as the worker can,
    896   1.1     skrll  *	without waiting for the next callout tick to run.
    897  1.12  riastrad  */
    898   1.1     skrll bool
    899   1.1     skrll schedule_delayed_work(struct delayed_work *dw, unsigned long ticks)
    900   1.1     skrll {
    901  1.29  riastrad 
    902  1.30  riastrad 	return queue_delayed_work(system_wq, dw, ticks);
    903  1.30  riastrad }
    904  1.30  riastrad 
    905  1.30  riastrad /*
    906  1.30  riastrad  * dw_callout_init(wq, dw)
    907  1.30  riastrad  *
    908  1.30  riastrad  *	Initialize the callout of dw and transition to
    909  1.30  riastrad  *	DELAYED_WORK_SCHEDULED.  Caller must use callout_schedule.
    910  1.30  riastrad  */
    911  1.30  riastrad static void
    912  1.39  riastrad dw_callout_init(struct workqueue_struct *wq, struct delayed_work *dw)
    913  1.30  riastrad {
    914  1.30  riastrad 
    915  1.30  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    916  1.30  riastrad 	KASSERT(work_queue(&dw->work) == wq);
    917  1.30  riastrad 	KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
    918  1.30  riastrad 
    919  1.30  riastrad 	callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
    920  1.30  riastrad 	callout_setfunc(&dw->dw_callout, &linux_workqueue_timeout, dw);
    921  1.30  riastrad 	TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
    922  1.31  riastrad 	dw->dw_state = DELAYED_WORK_SCHEDULED;
    923  1.31  riastrad }
    924  1.31  riastrad 
    925  1.31  riastrad /*
    926  1.31  riastrad  * dw_callout_destroy(wq, dw)
    927  1.31  riastrad  *
    928  1.31  riastrad  *	Destroy the callout of dw and transition to DELAYED_WORK_IDLE.
    929  1.31  riastrad  */
    930  1.31  riastrad static void
    931  1.39  riastrad dw_callout_destroy(struct workqueue_struct *wq, struct delayed_work *dw)
    932  1.31  riastrad {
    933  1.31  riastrad 
    934  1.31  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    935  1.31  riastrad 	KASSERT(work_queue(&dw->work) == wq);
    936  1.31  riastrad 	KASSERT(dw->dw_state == DELAYED_WORK_SCHEDULED ||
    937  1.31  riastrad 	    dw->dw_state == DELAYED_WORK_RESCHEDULED ||
    938  1.35  riastrad 	    dw->dw_state == DELAYED_WORK_CANCELLED);
    939  1.31  riastrad 
    940  1.31  riastrad 	TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
    941  1.31  riastrad 	callout_destroy(&dw->dw_callout);
    942  1.31  riastrad 	dw->dw_resched = -1;
    943  1.29  riastrad 	dw->dw_state = DELAYED_WORK_IDLE;
    944  1.29  riastrad }
    945  1.29  riastrad 
    946  1.29  riastrad /*
    947  1.39  riastrad  * cancel_delayed_work_done(wq, dw)
    948  1.29  riastrad  *
    949  1.23  riastrad  *	Complete cancellation of a delayed work: transition from
    950  1.23  riastrad  *	DELAYED_WORK_CANCELLED to DELAYED_WORK_IDLE and off the
    951  1.23  riastrad  *	workqueue.  Caller must not dereference dw after this returns.
    952  1.23  riastrad  */
    953  1.23  riastrad static void
    954  1.39  riastrad cancel_delayed_work_done(struct workqueue_struct *wq, struct delayed_work *dw)
    955  1.23  riastrad {
    956  1.31  riastrad 
    957  1.31  riastrad 	KASSERT(mutex_owned(&wq->wq_lock));
    958  1.23  riastrad 	KASSERT(work_queue(&dw->work) == wq);
    959  1.39  riastrad 	KASSERT(dw->dw_state == DELAYED_WORK_CANCELLED);
    960  1.23  riastrad 
    961  1.23  riastrad 	dw_callout_destroy(wq, dw);
    962  1.29  riastrad 	release_work(&dw->work, wq);
    963  1.29  riastrad 	/* Can't dereference dw after this point.  */
    964  1.29  riastrad }
    965  1.29  riastrad 
    966  1.39  riastrad /*
    967  1.39  riastrad  * queue_delayed_work(wq, dw, ticks)
    968  1.36  riastrad  *
    969  1.36  riastrad  *	If it is not currently scheduled, schedule dw to run after
    970  1.36  riastrad  *	ticks on wq.  If currently queued, remove it from the queue
    971  1.29  riastrad  *	first.
    972  1.12  riastrad  *
    973  1.12  riastrad  *	If ticks == 0, queue it to run as soon as the worker can,
    974  1.12  riastrad  *	without waiting for the next callout tick to run.
    975  1.12  riastrad  */
    976  1.12  riastrad bool
    977   1.1     skrll queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
    978  1.12  riastrad     unsigned long ticks)
    979  1.39  riastrad {
    980  1.29  riastrad 	bool newly_queued;
    981  1.29  riastrad 
    982  1.29  riastrad 	mutex_enter(&wq->wq_lock);
    983  1.29  riastrad 	if (__predict_true(acquire_work(&dw->work, wq))) {
    984  1.12  riastrad 		/*
    985  1.29  riastrad 		 * It wasn't on any workqueue at all.  Schedule it to
    986  1.39  riastrad 		 * run on this one.
    987  1.29  riastrad 		 */
    988  1.29  riastrad 		KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
    989  1.41  riastrad 		if (ticks == 0) {
    990  1.29  riastrad 			TAILQ_INSERT_TAIL(&wq->wq_dqueue, &dw->work,
    991  1.29  riastrad 			    work_entry);
    992  1.29  riastrad 			cv_broadcast(&wq->wq_cv);
    993  1.29  riastrad 			SDT_PROBE2(sdt, linux, work, queue,  &dw->work, wq);
    994  1.29  riastrad 		} else {
    995  1.30  riastrad 			/*
    996  1.29  riastrad 			 * Initialize a callout and schedule to run
    997  1.41  riastrad 			 * after a delay.
    998  1.29  riastrad 			 */
    999  1.12  riastrad 			dw_callout_init(wq, dw);
   1000  1.12  riastrad 			callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
   1001  1.39  riastrad 			SDT_PROBE3(sdt, linux, work, schedule,  dw, wq, ticks);
   1002  1.39  riastrad 		}
   1003  1.39  riastrad 		newly_queued = true;
   1004  1.39  riastrad 	} else {
   1005  1.39  riastrad 		/* It was already on this workqueue.  */
   1006  1.39  riastrad 		switch (dw->dw_state) {
   1007  1.39  riastrad 		case DELAYED_WORK_IDLE:
   1008  1.39  riastrad 		case DELAYED_WORK_SCHEDULED:
   1009  1.39  riastrad 		case DELAYED_WORK_RESCHEDULED:
   1010  1.29  riastrad 			/* On the queue or already scheduled.  Leave it.  */
   1011  1.39  riastrad 			newly_queued = false;
   1012  1.39  riastrad 			break;
   1013  1.29  riastrad 		case DELAYED_WORK_CANCELLED:
   1014  1.40  riastrad 			/*
   1015  1.40  riastrad 			 * Scheduled and the callout began, but it was
   1016  1.41  riastrad 			 * cancelled.  Reschedule it.
   1017  1.41  riastrad 			 */
   1018  1.40  riastrad 			if (ticks == 0) {
   1019  1.40  riastrad 				dw->dw_state = DELAYED_WORK_SCHEDULED;
   1020  1.40  riastrad 				SDT_PROBE2(sdt, linux, work, queue,
   1021  1.41  riastrad 				    &dw->work, wq);
   1022  1.41  riastrad 			} else {
   1023  1.40  riastrad 				dw->dw_state = DELAYED_WORK_RESCHEDULED;
   1024  1.39  riastrad 				dw->dw_resched = MIN(INT_MAX, ticks);
   1025  1.39  riastrad 				SDT_PROBE3(sdt, linux, work, schedule,
   1026  1.39  riastrad 				    dw, wq, ticks);
   1027  1.39  riastrad 			}
   1028  1.39  riastrad 			newly_queued = true;
   1029  1.29  riastrad 			break;
   1030   1.1     skrll 		default:
   1031  1.12  riastrad 			panic("invalid delayed work state: %d",
   1032   1.1     skrll 			    dw->dw_state);
   1033   1.1     skrll 		}
   1034   1.1     skrll 	}
   1035   1.1     skrll 	mutex_exit(&wq->wq_lock);
   1036  1.29  riastrad 
   1037  1.29  riastrad 	return newly_queued;
   1038  1.29  riastrad }
   1039  1.39  riastrad 
   1040  1.39  riastrad /*
   1041  1.36  riastrad  * mod_delayed_work(wq, dw, ticks)
   1042  1.36  riastrad  *
   1043  1.36  riastrad  *	Schedule dw to run after ticks.  If scheduled or queued,
   1044  1.29  riastrad  *	reschedule.  If ticks == 0, run without delay.
   1045   1.1     skrll  *
   1046   1.1     skrll  *	True if it modified the timer of an already scheduled work,
   1047   1.1     skrll  *	false if it newly scheduled the work.
   1048   1.1     skrll  */
   1049   1.1     skrll bool
   1050   1.1     skrll mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
   1051  1.12  riastrad     unsigned long ticks)
   1052  1.39  riastrad {
   1053  1.29  riastrad 	bool timer_modified;
   1054  1.29  riastrad 
   1055  1.29  riastrad 	mutex_enter(&wq->wq_lock);
   1056  1.29  riastrad 	if (acquire_work(&dw->work, wq)) {
   1057  1.12  riastrad 		/*
   1058  1.29  riastrad 		 * It wasn't on any workqueue at all.  Schedule it to
   1059  1.29  riastrad 		 * run on this one.
   1060  1.29  riastrad 		 */
   1061  1.29  riastrad 		KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
   1062  1.29  riastrad 		if (ticks == 0) {
   1063  1.39  riastrad 			/*
   1064  1.29  riastrad 			 * Run immediately: put it on the queue and
   1065  1.29  riastrad 			 * signal the worker thread.
   1066  1.41  riastrad 			 */
   1067  1.29  riastrad 			TAILQ_INSERT_TAIL(&wq->wq_dqueue, &dw->work,
   1068  1.29  riastrad 			    work_entry);
   1069  1.29  riastrad 			cv_broadcast(&wq->wq_cv);
   1070  1.29  riastrad 			SDT_PROBE2(sdt, linux, work, queue,  &dw->work, wq);
   1071  1.29  riastrad 		} else {
   1072  1.30  riastrad 			/*
   1073  1.30  riastrad 			 * Initialize a callout and schedule to run
   1074  1.41  riastrad 			 * after a delay.
   1075  1.29  riastrad 			 */
   1076  1.12  riastrad 			dw_callout_init(wq, dw);
   1077  1.12  riastrad 			callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
   1078  1.39  riastrad 			SDT_PROBE3(sdt, linux, work, schedule,  dw, wq, ticks);
   1079  1.12  riastrad 		}
   1080  1.12  riastrad 		timer_modified = false;
   1081  1.39  riastrad 	} else {
   1082  1.39  riastrad 		/* It was already on this workqueue.  */
   1083  1.39  riastrad 		switch (dw->dw_state) {
   1084  1.41  riastrad 		case DELAYED_WORK_IDLE:
   1085  1.41  riastrad 			/* On the queue.  */
   1086  1.41  riastrad 			if (ticks == 0) {
   1087  1.41  riastrad 				/* Leave it be.  */
   1088  1.29  riastrad 				SDT_PROBE2(sdt, linux, work, cancel,
   1089  1.39  riastrad 				    &dw->work, wq);
   1090  1.39  riastrad 				SDT_PROBE2(sdt, linux, work, queue,
   1091  1.39  riastrad 				    &dw->work, wq);
   1092  1.39  riastrad 			} else {
   1093  1.39  riastrad 				/* Remove from the queue and schedule.  */
   1094  1.39  riastrad 				TAILQ_REMOVE(&wq->wq_dqueue, &dw->work,
   1095  1.41  riastrad 				    work_entry);
   1096  1.41  riastrad 				dw_callout_init(wq, dw);
   1097  1.41  riastrad 				callout_schedule(&dw->dw_callout,
   1098  1.41  riastrad 				    MIN(INT_MAX, ticks));
   1099  1.12  riastrad 				SDT_PROBE2(sdt, linux, work, cancel,
   1100  1.39  riastrad 				    &dw->work, wq);
   1101  1.12  riastrad 				SDT_PROBE3(sdt, linux, work, schedule,
   1102  1.12  riastrad 				    dw, wq, ticks);
   1103  1.29  riastrad 			}
   1104  1.29  riastrad 			timer_modified = true;
   1105  1.29  riastrad 			break;
   1106  1.29  riastrad 		case DELAYED_WORK_SCHEDULED:
   1107  1.29  riastrad 			/*
   1108  1.29  riastrad 			 * It is scheduled to run after a delay.  Try
   1109  1.12  riastrad 			 * to stop it and reschedule it; if we can't,
   1110  1.29  riastrad 			 * either reschedule it or cancel it to put it
   1111  1.29  riastrad 			 * on the queue, and inform the callout.
   1112  1.29  riastrad 			 */
   1113  1.29  riastrad 			if (callout_stop(&dw->dw_callout)) {
   1114  1.29  riastrad 				/* Can't stop, callout has begun.  */
   1115  1.29  riastrad 				if (ticks == 0) {
   1116  1.29  riastrad 					/*
   1117  1.29  riastrad 					 * We don't actually need to do
   1118  1.41  riastrad 					 * anything.  The callout will
   1119  1.41  riastrad 					 * queue it as soon as it gets
   1120  1.41  riastrad 					 * the lock.
   1121  1.41  riastrad 					 */
   1122  1.29  riastrad 					SDT_PROBE2(sdt, linux, work, cancel,
   1123  1.35  riastrad 					    &dw->work, wq);
   1124  1.29  riastrad 					SDT_PROBE2(sdt, linux, work, queue,
   1125  1.35  riastrad 					    &dw->work, wq);
   1126  1.41  riastrad 				} else {
   1127  1.41  riastrad 					/* Ask the callout to reschedule.  */
   1128  1.41  riastrad 					dw->dw_state = DELAYED_WORK_RESCHEDULED;
   1129  1.41  riastrad 					dw->dw_resched = MIN(INT_MAX, ticks);
   1130  1.29  riastrad 					SDT_PROBE2(sdt, linux, work, cancel,
   1131  1.12  riastrad 					    &dw->work, wq);
   1132  1.35  riastrad 					SDT_PROBE3(sdt, linux, work, schedule,
   1133  1.29  riastrad 					    dw, wq, ticks);
   1134  1.29  riastrad 				}
   1135  1.29  riastrad 			} else {
   1136  1.29  riastrad 				/* We stopped the callout before it began.  */
   1137  1.29  riastrad 				if (ticks == 0) {
   1138  1.29  riastrad 					/*
   1139  1.29  riastrad 					 * Run immediately: destroy the
   1140  1.31  riastrad 					 * callout, put it on the
   1141  1.39  riastrad 					 * queue, and signal the worker
   1142  1.29  riastrad 					 * thread.
   1143  1.29  riastrad 					 */
   1144  1.41  riastrad 					dw_callout_destroy(wq, dw);
   1145  1.41  riastrad 					TAILQ_INSERT_TAIL(&wq->wq_dqueue,
   1146  1.41  riastrad 					    &dw->work, work_entry);
   1147  1.41  riastrad 					cv_broadcast(&wq->wq_cv);
   1148  1.29  riastrad 					SDT_PROBE2(sdt, linux, work, cancel,
   1149  1.29  riastrad 					    &dw->work, wq);
   1150  1.29  riastrad 					SDT_PROBE2(sdt, linux, work, queue,
   1151  1.29  riastrad 					    &dw->work, wq);
   1152  1.29  riastrad 				} else {
   1153  1.29  riastrad 					/*
   1154  1.29  riastrad 					 * Reschedule the callout.  No
   1155  1.41  riastrad 					 * state change.
   1156  1.41  riastrad 					 */
   1157  1.41  riastrad 					callout_schedule(&dw->dw_callout,
   1158  1.41  riastrad 					    MIN(INT_MAX, ticks));
   1159  1.29  riastrad 					SDT_PROBE2(sdt, linux, work, cancel,
   1160  1.12  riastrad 					    &dw->work, wq);
   1161  1.12  riastrad 					SDT_PROBE3(sdt, linux, work, schedule,
   1162  1.12  riastrad 					    dw, wq, ticks);
   1163  1.12  riastrad 				}
   1164  1.35  riastrad 			}
   1165  1.35  riastrad 			timer_modified = true;
   1166  1.35  riastrad 			break;
   1167  1.35  riastrad 		case DELAYED_WORK_RESCHEDULED:
   1168  1.35  riastrad 			/*
   1169  1.35  riastrad 			 * Someone rescheduled it after the callout
   1170  1.35  riastrad 			 * started but before the poor thing even had a
   1171  1.35  riastrad 			 * chance to acquire the lock.
   1172  1.35  riastrad 			 */
   1173  1.35  riastrad 			if (ticks == 0) {
   1174  1.35  riastrad 				/*
   1175  1.35  riastrad 				 * We can just switch back to
   1176  1.35  riastrad 				 * DELAYED_WORK_SCHEDULED so that the
   1177  1.35  riastrad 				 * callout will queue the work as soon
   1178  1.41  riastrad 				 * as it gets the lock.
   1179  1.41  riastrad 				 */
   1180  1.41  riastrad 				dw->dw_state = DELAYED_WORK_SCHEDULED;
   1181  1.41  riastrad 				dw->dw_resched = -1;
   1182  1.35  riastrad 				SDT_PROBE2(sdt, linux, work, cancel,
   1183  1.35  riastrad 				    &dw->work, wq);
   1184  1.35  riastrad 				SDT_PROBE2(sdt, linux, work, queue,
   1185  1.41  riastrad 				    &dw->work, wq);
   1186  1.41  riastrad 			} else {
   1187  1.41  riastrad 				/* Change the rescheduled time.  */
   1188  1.41  riastrad 				dw->dw_resched = ticks;
   1189  1.35  riastrad 				SDT_PROBE2(sdt, linux, work, cancel,
   1190  1.35  riastrad 				    &dw->work, wq);
   1191  1.35  riastrad 				SDT_PROBE3(sdt, linux, work, schedule,
   1192  1.12  riastrad 				    dw, wq, ticks);
   1193  1.12  riastrad 			}
   1194  1.35  riastrad 			timer_modified = true;
   1195  1.35  riastrad 			break;
   1196  1.35  riastrad 		case DELAYED_WORK_CANCELLED:
   1197  1.12  riastrad 			/*
   1198  1.29  riastrad 			 * Someone cancelled it after the callout
   1199  1.29  riastrad 			 * started but before the poor thing even had a
   1200  1.29  riastrad 			 * chance to acquire the lock.
   1201  1.29  riastrad 			 */
   1202  1.29  riastrad 			if (ticks == 0) {
   1203  1.29  riastrad 				/*
   1204  1.29  riastrad 				 * We can just switch back to
   1205  1.29  riastrad 				 * DELAYED_WORK_SCHEDULED so that the
   1206  1.41  riastrad 				 * callout will queue the work as soon
   1207  1.41  riastrad 				 * as it gets the lock.
   1208  1.29  riastrad 				 */
   1209  1.39  riastrad 				dw->dw_state = DELAYED_WORK_SCHEDULED;
   1210  1.29  riastrad 				SDT_PROBE2(sdt, linux, work, queue,
   1211  1.35  riastrad 				    &dw->work, wq);
   1212  1.41  riastrad 			} else {
   1213  1.41  riastrad 				/* Ask it to reschedule.  */
   1214  1.29  riastrad 				dw->dw_state = DELAYED_WORK_RESCHEDULED;
   1215  1.39  riastrad 				dw->dw_resched = MIN(INT_MAX, ticks);
   1216  1.12  riastrad 				SDT_PROBE3(sdt, linux, work, schedule,
   1217  1.12  riastrad 				    dw, wq, ticks);
   1218  1.29  riastrad 			}
   1219   1.1     skrll 			timer_modified = false;
   1220   1.1     skrll 			break;
   1221  1.12  riastrad 		default:
   1222   1.1     skrll 			panic("invalid delayed work state: %d", dw->dw_state);
   1223   1.1     skrll 		}
   1224   1.1     skrll 	}
   1225   1.1     skrll 	mutex_exit(&wq->wq_lock);
   1226  1.36  riastrad 
   1227  1.36  riastrad 	return timer_modified;
   1228  1.36  riastrad }
   1229  1.36  riastrad 
   1230  1.36  riastrad /*
   1231  1.36  riastrad  * cancel_delayed_work(dw)
   1232  1.36  riastrad  *
   1233  1.36  riastrad  *	If work was scheduled or queued, remove it from the schedule or
   1234  1.36  riastrad  *	queue and return true.  If work was not scheduled or queued,
   1235  1.36  riastrad  *	return false.  Note that work may already be running; if it
   1236   1.1     skrll  *	hasn't been rescheduled or requeued, then cancel_delayed_work
   1237   1.1     skrll  *	will return false, and either way, cancel_delayed_work will NOT
   1238   1.1     skrll  *	wait for the work to complete.
   1239  1.12  riastrad  */
   1240  1.12  riastrad bool
   1241   1.1     skrll cancel_delayed_work(struct delayed_work *dw)
   1242  1.14  riastrad {
   1243  1.39  riastrad 	struct workqueue_struct *wq;
   1244  1.14  riastrad 	bool cancelled_p;
   1245  1.14  riastrad 
   1246  1.12  riastrad 	/* If there's no workqueue, nothing to cancel.   */
   1247  1.39  riastrad 	if ((wq = work_queue(&dw->work)) == NULL)
   1248  1.12  riastrad 		return false;
   1249  1.12  riastrad 
   1250  1.12  riastrad 	mutex_enter(&wq->wq_lock);
   1251  1.12  riastrad 	if (__predict_false(work_queue(&dw->work) != wq)) {
   1252  1.35  riastrad 		cancelled_p = false;
   1253  1.35  riastrad 	} else {
   1254  1.35  riastrad 		switch (dw->dw_state) {
   1255  1.35  riastrad 		case DELAYED_WORK_IDLE:
   1256  1.39  riastrad 			/*
   1257  1.39  riastrad 			 * It is either on the queue or already running
   1258  1.39  riastrad 			 * or both.
   1259  1.12  riastrad 			 */
   1260  1.41  riastrad 			if (work_claimed(&dw->work, wq)) {
   1261  1.41  riastrad 				/* On the queue.  Remove and release.  */
   1262  1.39  riastrad 				TAILQ_REMOVE(&wq->wq_dqueue, &dw->work,
   1263  1.39  riastrad 				    work_entry);
   1264  1.12  riastrad 				SDT_PROBE2(sdt, linux, work, cancel,
   1265  1.35  riastrad 				    &dw->work, wq);
   1266  1.39  riastrad 				release_work(&dw->work, wq);
   1267  1.35  riastrad 				/* Can't dereference dw after this point.  */
   1268  1.12  riastrad 				cancelled_p = true;
   1269  1.12  riastrad 			} else {
   1270  1.12  riastrad 				/* Not on the queue, so didn't cancel.  */
   1271  1.21  riastrad 				cancelled_p = false;
   1272  1.21  riastrad 			}
   1273  1.21  riastrad 			break;
   1274  1.21  riastrad 		case DELAYED_WORK_SCHEDULED:
   1275  1.21  riastrad 			/*
   1276  1.21  riastrad 			 * If it is scheduled, mark it cancelled and
   1277  1.21  riastrad 			 * try to stop the callout before it starts.
   1278  1.21  riastrad 			 *
   1279  1.21  riastrad 			 * If it's too late and the callout has already
   1280  1.21  riastrad 			 * begun to execute, tough.
   1281  1.21  riastrad 			 *
   1282  1.21  riastrad 			 * If we stopped the callout before it started,
   1283  1.12  riastrad 			 * however, then destroy the callout and
   1284  1.41  riastrad 			 * dissociate it from the workqueue ourselves.
   1285  1.27  riastrad 			 */
   1286  1.27  riastrad 			dw->dw_state = DELAYED_WORK_CANCELLED;
   1287  1.16  riastrad 			cancelled_p = true;
   1288  1.34  riastrad 			SDT_PROBE2(sdt, linux, work, cancel,  &dw->work, wq);
   1289  1.34  riastrad 			if (!callout_stop(&dw->dw_callout))
   1290  1.34  riastrad 				cancel_delayed_work_done(wq, dw);
   1291  1.34  riastrad 			break;
   1292  1.34  riastrad 		case DELAYED_WORK_RESCHEDULED:
   1293  1.34  riastrad 			/*
   1294  1.35  riastrad 			 * If it is being rescheduled, the callout has
   1295  1.34  riastrad 			 * already fired.  We must ask it to cancel.
   1296  1.41  riastrad 			 */
   1297  1.34  riastrad 			dw->dw_state = DELAYED_WORK_CANCELLED;
   1298  1.34  riastrad 			dw->dw_resched = -1;
   1299  1.34  riastrad 			cancelled_p = true;
   1300  1.34  riastrad 			SDT_PROBE2(sdt, linux, work, cancel,  &dw->work, wq);
   1301  1.34  riastrad 			break;
   1302  1.34  riastrad 		case DELAYED_WORK_CANCELLED:
   1303  1.34  riastrad 			/*
   1304  1.34  riastrad 			 * If it is being cancelled, the callout has
   1305  1.34  riastrad 			 * already fired.  There is nothing more for us
   1306  1.34  riastrad 			 * to do.  Someone else claims credit for
   1307  1.12  riastrad 			 * cancelling it.
   1308  1.12  riastrad 			 */
   1309  1.12  riastrad 			cancelled_p = false;
   1310  1.12  riastrad 			break;
   1311   1.1     skrll 		default:
   1312  1.12  riastrad 			panic("invalid delayed work state: %d",
   1313   1.1     skrll 			    dw->dw_state);
   1314   1.1     skrll 		}
   1315   1.1     skrll 	}
   1316   1.1     skrll 	mutex_exit(&wq->wq_lock);
   1317  1.36  riastrad 
   1318  1.36  riastrad 	return cancelled_p;
   1319  1.36  riastrad }
   1320  1.36  riastrad 
   1321  1.36  riastrad /*
   1322  1.36  riastrad  * cancel_delayed_work_sync(dw)
   1323  1.36  riastrad  *
   1324  1.36  riastrad  *	If work was scheduled or queued, remove it from the schedule or
   1325  1.36  riastrad  *	queue and return true.  If work was not scheduled or queued,
   1326   1.1     skrll  *	return false.  Note that work may already be running; if it
   1327   1.1     skrll  *	hasn't been rescheduled or requeued, then cancel_delayed_work
   1328   1.1     skrll  *	will return false; either way, wait for it to complete.
   1329  1.12  riastrad  */
   1330  1.24  riastrad bool
   1331   1.1     skrll cancel_delayed_work_sync(struct delayed_work *dw)
   1332  1.24  riastrad {
   1333  1.39  riastrad 	struct workqueue_struct *wq;
   1334  1.24  riastrad 	bool cancelled_p;
   1335  1.14  riastrad 
   1336  1.12  riastrad 	/* If there's no workqueue, nothing to cancel.  */
   1337  1.39  riastrad 	if ((wq = work_queue(&dw->work)) == NULL)
   1338  1.12  riastrad 		return false;
   1339  1.12  riastrad 
   1340  1.20  riastrad 	mutex_enter(&wq->wq_lock);
   1341  1.12  riastrad 	if (__predict_false(work_queue(&dw->work) != wq)) {
   1342  1.35  riastrad 		cancelled_p = false;
   1343  1.35  riastrad 	} else {
   1344  1.35  riastrad 		switch (dw->dw_state) {
   1345  1.35  riastrad 		case DELAYED_WORK_IDLE:
   1346  1.39  riastrad 			/*
   1347  1.39  riastrad 			 * It is either on the queue or already running
   1348  1.39  riastrad 			 * or both.
   1349  1.12  riastrad 			 */
   1350  1.41  riastrad 			if (work_claimed(&dw->work, wq)) {
   1351  1.41  riastrad 				/* On the queue.  Remove and release.  */
   1352  1.39  riastrad 				TAILQ_REMOVE(&wq->wq_dqueue, &dw->work,
   1353  1.39  riastrad 				    work_entry);
   1354  1.12  riastrad 				SDT_PROBE2(sdt, linux, work, cancel,
   1355  1.39  riastrad 				    &dw->work, wq);
   1356  1.39  riastrad 				release_work(&dw->work, wq);
   1357  1.39  riastrad 				/* Can't dereference dw after this point.  */
   1358  1.12  riastrad 				cancelled_p = true;
   1359  1.39  riastrad 			} else {
   1360  1.39  riastrad 				/* Not on the queue, so didn't cancel. */
   1361  1.39  riastrad 				cancelled_p = false;
   1362  1.12  riastrad 			}
   1363  1.12  riastrad 			/* If it's still running, wait for it to complete.  */
   1364  1.12  riastrad 			if (wq->wq_current_work == &dw->work)
   1365  1.20  riastrad 				wait_for_current_work(&dw->work, wq);
   1366  1.20  riastrad 			break;
   1367  1.20  riastrad 		case DELAYED_WORK_SCHEDULED:
   1368  1.20  riastrad 			/*
   1369  1.20  riastrad 			 * If it is scheduled, mark it cancelled and
   1370  1.24  riastrad 			 * try to stop the callout before it starts.
   1371  1.24  riastrad 			 *
   1372  1.24  riastrad 			 * If it's too late and the callout has already
   1373  1.20  riastrad 			 * begun to execute, we must wait for it to
   1374  1.20  riastrad 			 * complete.  But we got in soon enough to ask
   1375  1.35  riastrad 			 * the callout not to run, so we successfully
   1376  1.20  riastrad 			 * cancelled it in that case.
   1377  1.12  riastrad 			 *
   1378  1.12  riastrad 			 * If we stopped the callout before it started,
   1379  1.41  riastrad 			 * then we must destroy the callout and
   1380  1.27  riastrad 			 * dissociate it from the workqueue ourselves.
   1381  1.27  riastrad 			 */
   1382  1.34  riastrad 			dw->dw_state = DELAYED_WORK_CANCELLED;
   1383  1.34  riastrad 			SDT_PROBE2(sdt, linux, work, cancel,  &dw->work, wq);
   1384  1.34  riastrad 			if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
   1385  1.34  riastrad 				cancel_delayed_work_done(wq, dw);
   1386  1.34  riastrad 			cancelled_p = true;
   1387  1.34  riastrad 			break;
   1388  1.34  riastrad 		case DELAYED_WORK_RESCHEDULED:
   1389  1.34  riastrad 			/*
   1390  1.34  riastrad 			 * If it is being rescheduled, the callout has
   1391  1.35  riastrad 			 * already fired.  We must ask it to cancel and
   1392  1.41  riastrad 			 * wait for it to complete.
   1393  1.34  riastrad 			 */
   1394  1.34  riastrad 			dw->dw_state = DELAYED_WORK_CANCELLED;
   1395  1.34  riastrad 			dw->dw_resched = -1;
   1396  1.34  riastrad 			SDT_PROBE2(sdt, linux, work, cancel,  &dw->work, wq);
   1397  1.34  riastrad 			(void)callout_halt(&dw->dw_callout, &wq->wq_lock);
   1398  1.34  riastrad 			cancelled_p = true;
   1399  1.34  riastrad 			break;
   1400  1.34  riastrad 		case DELAYED_WORK_CANCELLED:
   1401  1.34  riastrad 			/*
   1402  1.34  riastrad 			 * If it is being cancelled, the callout has
   1403  1.34  riastrad 			 * already fired.  We need only wait for it to
   1404  1.34  riastrad 			 * complete.  Someone else, however, claims
   1405  1.20  riastrad 			 * credit for cancelling it.
   1406  1.12  riastrad 			 */
   1407  1.12  riastrad 			(void)callout_halt(&dw->dw_callout, &wq->wq_lock);
   1408  1.12  riastrad 			cancelled_p = false;
   1409  1.12  riastrad 			break;
   1410   1.1     skrll 		default:
   1411  1.12  riastrad 			panic("invalid delayed work state: %d",
   1412   1.1     skrll 			    dw->dw_state);
   1413   1.1     skrll 		}
   1414   1.1     skrll 	}
   1415  1.12  riastrad 	mutex_exit(&wq->wq_lock);
   1416  1.12  riastrad 
   1417  1.12  riastrad 	return cancelled_p;
   1418  1.12  riastrad }
   1419   1.1     skrll 
   1420  1.36  riastrad /*
   1422  1.36  riastrad  * Flush
   1423  1.36  riastrad  */
   1424  1.36  riastrad 
   1425  1.36  riastrad /*
   1426   1.5  riastrad  * flush_scheduled_work()
   1427  1.12  riastrad  *
   1428   1.5  riastrad  *	Wait for all work queued on system_wq to complete.  This does
   1429   1.5  riastrad  *	not include delayed work.
   1430  1.12  riastrad  */
   1431   1.5  riastrad void
   1432   1.5  riastrad flush_scheduled_work(void)
   1433  1.57  riastrad {
   1434  1.57  riastrad 
   1435  1.57  riastrad 	flush_workqueue(system_wq);
   1436  1.57  riastrad }
   1437  1.57  riastrad 
   1438  1.57  riastrad struct flush_work {
   1439  1.57  riastrad 	kmutex_t		fw_lock;
   1440  1.57  riastrad 	kcondvar_t		fw_cv;
   1441  1.57  riastrad 	struct work_struct	fw_work;
   1442  1.28  riastrad 	bool			fw_done;
   1443  1.57  riastrad };
   1444  1.28  riastrad 
   1445  1.57  riastrad static void
   1446  1.57  riastrad flush_work_cb(struct work_struct *work)
   1447  1.57  riastrad {
   1448  1.57  riastrad 	struct flush_work *fw = container_of(work, struct flush_work, fw_work);
   1449  1.28  riastrad 
   1450  1.28  riastrad 	mutex_enter(&fw->fw_lock);
   1451  1.36  riastrad 	fw->fw_done = true;
   1452  1.36  riastrad 	cv_broadcast(&fw->fw_cv);
   1453  1.36  riastrad 	mutex_exit(&fw->fw_lock);
   1454  1.36  riastrad }
   1455  1.36  riastrad 
   1456  1.36  riastrad /*
   1457  1.12  riastrad  * flush_workqueue(wq)
   1458  1.12  riastrad  *
   1459   1.1     skrll  *	Wait for all work queued on wq to complete.  This does not
   1460  1.57  riastrad  *	include delayed work.
   1461  1.57  riastrad  */
   1462  1.58  riastrad void
   1463  1.60  riastrad flush_workqueue(struct workqueue_struct *wq)
   1464  1.58  riastrad {
   1465  1.58  riastrad 	struct flush_work fw;
   1466  1.58  riastrad 
   1467  1.57  riastrad 	if (lwp_getspecific(workqueue_key) == wq) {
   1468  1.57  riastrad 		SDT_PROBE1(sdt, linux, work, flush__self,  wq);
   1469  1.57  riastrad 		return;
   1470  1.57  riastrad 	}
   1471  1.57  riastrad 
   1472  1.57  riastrad 	mutex_init(&fw.fw_lock, MUTEX_DEFAULT, IPL_VM);
   1473  1.57  riastrad 	cv_init(&fw.fw_cv, "lxwqflsh");
   1474   1.1     skrll 	INIT_WORK(&fw.fw_work, &flush_work_cb);
   1475  1.57  riastrad 	fw.fw_done = false;
   1476  1.57  riastrad 
   1477  1.57  riastrad 	SDT_PROBE1(sdt, linux, work, flush__start,  wq);
   1478  1.57  riastrad 	queue_work(wq, &fw.fw_work);
   1479  1.57  riastrad 
   1480  1.57  riastrad 	mutex_enter(&fw.fw_lock);
   1481  1.57  riastrad 	while (!fw.fw_done)
   1482  1.57  riastrad 		cv_wait(&fw.fw_cv, &fw.fw_lock);
   1483  1.57  riastrad 	mutex_exit(&fw.fw_lock);
   1484  1.57  riastrad 	SDT_PROBE1(sdt, linux, work, flush__done,  wq);
   1485  1.50  riastrad 
   1486  1.50  riastrad 	KASSERT(fw.fw_done);
   1487  1.50  riastrad 	/* no DESTROY_WORK */
   1488  1.50  riastrad 	cv_destroy(&fw.fw_cv);
   1489  1.50  riastrad 	mutex_destroy(&fw.fw_lock);
   1490  1.50  riastrad }
   1491  1.50  riastrad 
   1492  1.50  riastrad /*
   1493  1.50  riastrad  * drain_workqueue(wq)
   1494  1.50  riastrad  *
   1495  1.50  riastrad  *	Repeatedly flush wq until there is no more work.
   1496  1.57  riastrad  */
   1497  1.50  riastrad void
   1498  1.57  riastrad drain_workqueue(struct workqueue_struct *wq)
   1499  1.50  riastrad {
   1500  1.50  riastrad 	unsigned ntries = 0;
   1501  1.50  riastrad 	bool done;
   1502  1.50  riastrad 
   1503  1.57  riastrad 	do {
   1504  1.57  riastrad 		if (ntries++ == 10 || (ntries % 100) == 0)
   1505  1.57  riastrad 			printf("linux workqueue %s"
   1506  1.57  riastrad 			    ": still clogged after %u flushes",
   1507  1.57  riastrad 			    wq->wq_name, ntries);
   1508  1.57  riastrad 		flush_workqueue(wq);
   1509  1.57  riastrad 		mutex_enter(&wq->wq_lock);
   1510   1.1     skrll 		done = wq->wq_current_work == NULL;
   1511   1.1     skrll 		done &= TAILQ_EMPTY(&wq->wq_queue);
   1512  1.36  riastrad 		done &= TAILQ_EMPTY(&wq->wq_dqueue);
   1513  1.36  riastrad 		mutex_exit(&wq->wq_lock);
   1514  1.36  riastrad 	} while (!done);
   1515  1.36  riastrad }
   1516  1.36  riastrad 
   1517  1.48  riastrad /*
   1518  1.48  riastrad  * flush_work(work)
   1519  1.48  riastrad  *
   1520  1.36  riastrad  *	If work is queued or currently executing, wait for it to
   1521  1.48  riastrad  *	complete.
   1522  1.12  riastrad  *
   1523   1.1     skrll  *	Return true if we waited to flush it, false if it was already
   1524  1.14  riastrad  *	idle.
   1525   1.1     skrll  */
   1526  1.14  riastrad bool
   1527  1.39  riastrad flush_work(struct work_struct *work)
   1528  1.48  riastrad {
   1529   1.1     skrll 	struct workqueue_struct *wq;
   1530  1.12  riastrad 
   1531  1.48  riastrad 	/* If there's no workqueue, nothing to flush.  */
   1532   1.1     skrll 	if ((wq = work_queue(work)) == NULL)
   1533   1.1     skrll 		return false;
   1534  1.36  riastrad 
   1535  1.36  riastrad 	flush_workqueue(wq);
   1536  1.36  riastrad 	return true;
   1537  1.38  riastrad }
   1538  1.38  riastrad 
   1539  1.38  riastrad /*
   1540  1.36  riastrad  * flush_delayed_work(dw)
   1541  1.48  riastrad  *
   1542  1.12  riastrad  *	If dw is scheduled to run after a delay, queue it immediately
   1543   1.1     skrll  *	instead.  Then, if dw is queued or currently executing, wait
   1544  1.14  riastrad  *	for it to complete.
   1545  1.48  riastrad  */
   1546   1.1     skrll bool
   1547  1.14  riastrad flush_delayed_work(struct delayed_work *dw)
   1548  1.39  riastrad {
   1549  1.48  riastrad 	struct workqueue_struct *wq;
   1550   1.1     skrll 	bool waited = false;
   1551   1.1     skrll 
   1552  1.39  riastrad 	/* If there's no workqueue, nothing to flush.  */
   1553  1.38  riastrad 	if ((wq = work_queue(&dw->work)) == NULL)
   1554  1.38  riastrad 		return false;
   1555  1.38  riastrad 
   1556  1.38  riastrad 	mutex_enter(&wq->wq_lock);
   1557  1.38  riastrad 	if (__predict_false(work_queue(&dw->work) != wq)) {
   1558  1.48  riastrad 		/*
   1559  1.38  riastrad 		 * Moved off the queue already (and possibly to another
   1560  1.28  riastrad 		 * queue, though that would be ill-advised), so it must
   1561  1.12  riastrad 		 * have completed, and we have nothing more to do.
   1562  1.28  riastrad 		 */
   1563  1.28  riastrad 		waited = false;
   1564  1.28  riastrad 	} else {
   1565  1.38  riastrad 		switch (dw->dw_state) {
   1566  1.38  riastrad 		case DELAYED_WORK_IDLE:
   1567  1.28  riastrad 			/*
   1568  1.12  riastrad 			 * It has a workqueue assigned and the callout
   1569  1.12  riastrad 			 * is idle, so it must be in progress or on the
   1570  1.35  riastrad 			 * queue.  In that case, we'll wait for it to
   1571  1.35  riastrad 			 * complete.
   1572  1.35  riastrad 			 */
   1573  1.38  riastrad 			break;
   1574  1.38  riastrad 		case DELAYED_WORK_SCHEDULED:
   1575  1.38  riastrad 		case DELAYED_WORK_RESCHEDULED:
   1576  1.38  riastrad 		case DELAYED_WORK_CANCELLED:
   1577  1.38  riastrad 			/*
   1578  1.38  riastrad 			 * The callout is scheduled, and may have even
   1579  1.38  riastrad 			 * started.  Mark it as scheduled so that if
   1580  1.35  riastrad 			 * the callout has fired it will queue the work
   1581  1.38  riastrad 			 * itself.  Try to stop the callout -- if we
   1582  1.38  riastrad 			 * can, queue the work now; if we can't, wait
   1583  1.38  riastrad 			 * for the callout to complete, which entails
   1584  1.38  riastrad 			 * queueing it.
   1585  1.38  riastrad 			 */
   1586  1.38  riastrad 			dw->dw_state = DELAYED_WORK_SCHEDULED;
   1587  1.38  riastrad 			if (!callout_halt(&dw->dw_callout, &wq->wq_lock)) {
   1588  1.38  riastrad 				/*
   1589  1.38  riastrad 				 * We stopped it before it ran.  No
   1590  1.38  riastrad 				 * state change in the interim is
   1591  1.38  riastrad 				 * possible.  Destroy the callout and
   1592  1.39  riastrad 				 * queue it ourselves.
   1593  1.38  riastrad 				 */
   1594  1.38  riastrad 				KASSERT(dw->dw_state ==
   1595  1.41  riastrad 				    DELAYED_WORK_SCHEDULED);
   1596  1.41  riastrad 				dw_callout_destroy(wq, dw);
   1597  1.38  riastrad 				TAILQ_INSERT_TAIL(&wq->wq_dqueue, &dw->work,
   1598  1.35  riastrad 				    work_entry);
   1599  1.12  riastrad 				cv_broadcast(&wq->wq_cv);
   1600  1.38  riastrad 				SDT_PROBE2(sdt, linux, work, queue,
   1601  1.12  riastrad 				    &dw->work, wq);
   1602  1.38  riastrad 			}
   1603  1.38  riastrad 			break;
   1604  1.38  riastrad 		default:
   1605  1.38  riastrad 			panic("invalid delayed work state: %d", dw->dw_state);
   1606  1.57  riastrad 		}
   1607  1.57  riastrad 		/*
   1608  1.57  riastrad 		 * Waiting for the whole queue to flush is overkill,
   1609  1.48  riastrad 		 * but doesn't hurt.
   1610   1.1     skrll 		 */
   1611  1.12  riastrad 		mutex_exit(&wq->wq_lock);
   1612  1.48  riastrad 		flush_workqueue(wq);
   1613  1.48  riastrad 		mutex_enter(&wq->wq_lock);
   1614   1.1     skrll 		waited = true;
   1615  1.49  riastrad 	}
   1616  1.49  riastrad 	mutex_exit(&wq->wq_lock);
   1617  1.49  riastrad 
   1618  1.49  riastrad 	return waited;
   1619  1.49  riastrad }
   1620  1.49  riastrad 
   1621  1.49  riastrad /*
   1622  1.51  riastrad  * delayed_work_pending(dw)
   1623  1.49  riastrad  *
   1624  1.49  riastrad  *	True if dw is currently scheduled to execute, false if not.
   1625  1.49  riastrad  */
   1626  1.49  riastrad bool
   1627  1.55  riastrad delayed_work_pending(const struct delayed_work *dw)
   1628  1.55  riastrad {
   1629  1.55  riastrad 
   1630  1.55  riastrad 	return work_pending(&dw->work);
   1631  1.55  riastrad }
   1632  1.55  riastrad 
   1633  1.55  riastrad /*
   1634  1.55  riastrad  * INIT_RCU_WORK(rw, fn)
   1635  1.55  riastrad  *
   1636  1.55  riastrad  *	Initialize rw for use with a workqueue to call fn in a worker
   1637  1.55  riastrad  *	thread after an RCU grace period.  There is no corresponding
   1638  1.55  riastrad  *	destruction operation.
   1639  1.55  riastrad  */
   1640  1.55  riastrad void
   1641  1.55  riastrad INIT_RCU_WORK(struct rcu_work *rw, void (*fn)(struct work_struct *))
   1642  1.55  riastrad {
   1643  1.55  riastrad 
   1644  1.55  riastrad 	INIT_WORK(&rw->work, fn);
   1645  1.55  riastrad }
   1646  1.55  riastrad 
   1647  1.55  riastrad static void
   1648  1.55  riastrad queue_rcu_work_cb(struct rcu_head *r)
   1649  1.55  riastrad {
   1650  1.55  riastrad 	struct rcu_work *rw = container_of(r, struct rcu_work, rw_rcu);
   1651  1.55  riastrad 	struct workqueue_struct *wq = work_queue(&rw->work);
   1652  1.55  riastrad 
   1653  1.55  riastrad 	mutex_enter(&wq->wq_lock);
   1654  1.55  riastrad 	KASSERT(work_pending(&rw->work));
   1655  1.55  riastrad 	KASSERT(work_queue(&rw->work) == wq);
   1656  1.55  riastrad 	destroy_rcu_head(&rw->rw_rcu);
   1657  1.55  riastrad 	TAILQ_REMOVE(&wq->wq_rcu, &rw->work, work_entry);
   1658  1.55  riastrad 	TAILQ_INSERT_TAIL(&wq->wq_queue, &rw->work, work_entry);
   1659  1.55  riastrad 	cv_broadcast(&wq->wq_cv);
   1660  1.55  riastrad 	SDT_PROBE2(sdt, linux, work, queue,  &rw->work, wq);
   1661  1.55  riastrad 	mutex_exit(&wq->wq_lock);
   1662  1.55  riastrad }
   1663  1.55  riastrad 
   1664  1.55  riastrad /*
   1665  1.55  riastrad  * queue_rcu_work(wq, rw)
   1666  1.55  riastrad  *
   1667  1.55  riastrad  *	Schedule rw to run on wq after an RCU grace period.
   1668  1.55  riastrad  */
   1669  1.55  riastrad void
   1670  1.55  riastrad queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rw)
   1671  1.55  riastrad {
   1672  1.55  riastrad 
   1673  1.55  riastrad 	mutex_enter(&wq->wq_lock);
   1674  1.55  riastrad 	if (acquire_work(&rw->work, wq)) {
   1675  1.55  riastrad 		init_rcu_head(&rw->rw_rcu);
   1676  1.55  riastrad 		SDT_PROBE2(sdt, linux, work, rcu,  rw, wq);
   1677                 		TAILQ_INSERT_TAIL(&wq->wq_rcu, &rw->work, work_entry);
   1678                 		call_rcu(&rw->rw_rcu, &queue_rcu_work_cb);
   1679                 	}
   1680                 	mutex_exit(&wq->wq_lock);
   1681                 }
   1682