linux_work.c revision 1.6 1 1.2 riastrad /* $NetBSD: linux_work.c,v 1.6 2018/08/27 07:46:28 riastradh 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.2 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_work.c,v 1.6 2018/08/27 07:46:28 riastradh 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.3 riastrad struct rb_node wq_node;
64 1.3 riastrad struct lwp *wq_lwp;
65 1.3 riastrad
66 1.1 skrll /* XXX The following should all be per-CPU. */
67 1.1 skrll kmutex_t wq_lock;
68 1.1 skrll
69 1.1 skrll /*
70 1.1 skrll * Condvar for when any state related to this workqueue
71 1.1 skrll * changes. XXX Could split this into multiple condvars for
72 1.1 skrll * different purposes, but whatever...
73 1.1 skrll */
74 1.1 skrll kcondvar_t wq_cv;
75 1.1 skrll
76 1.1 skrll TAILQ_HEAD(, delayed_work) wq_delayed;
77 1.1 skrll struct work_struct *wq_current_work;
78 1.1 skrll };
79 1.1 skrll
80 1.1 skrll static void linux_work_lock_init(struct work_struct *);
81 1.1 skrll static void linux_work_lock(struct work_struct *);
82 1.1 skrll static void linux_work_unlock(struct work_struct *);
83 1.1 skrll static bool linux_work_locked(struct work_struct *) __diagused;
84 1.1 skrll
85 1.1 skrll static void linux_wq_barrier(struct work_struct *);
86 1.1 skrll
87 1.1 skrll static void linux_wait_for_cancelled_work(struct work_struct *);
88 1.1 skrll static void linux_wait_for_invoked_work(struct work_struct *);
89 1.1 skrll static void linux_worker(struct work *, void *);
90 1.1 skrll
91 1.1 skrll static void linux_cancel_delayed_work_callout(struct delayed_work *, bool);
92 1.1 skrll static void linux_wait_for_delayed_cancelled_work(struct delayed_work *);
93 1.1 skrll static void linux_worker_intr(void *);
94 1.1 skrll
95 1.1 skrll struct workqueue_struct *system_wq;
96 1.2 riastrad struct workqueue_struct *system_long_wq;
97 1.6 riastrad struct workqueue_struct *system_power_efficient_wq;
98 1.1 skrll
99 1.3 riastrad static struct {
100 1.3 riastrad kmutex_t lock;
101 1.3 riastrad struct rb_tree tree;
102 1.3 riastrad } workqueues __cacheline_aligned;
103 1.3 riastrad
104 1.3 riastrad static const rb_tree_ops_t workqueues_rb_ops;
105 1.3 riastrad
106 1.1 skrll int
107 1.1 skrll linux_workqueue_init(void)
108 1.1 skrll {
109 1.3 riastrad
110 1.3 riastrad mutex_init(&workqueues.lock, MUTEX_DEFAULT, IPL_VM);
111 1.3 riastrad rb_tree_init(&workqueues.tree, &workqueues_rb_ops);
112 1.1 skrll
113 1.1 skrll system_wq = alloc_ordered_workqueue("lnxsyswq", 0);
114 1.1 skrll if (system_wq == NULL)
115 1.2 riastrad goto fail0;
116 1.2 riastrad
117 1.2 riastrad system_long_wq = alloc_ordered_workqueue("lnxlngwq", 0);
118 1.2 riastrad if (system_long_wq == NULL)
119 1.2 riastrad goto fail1;
120 1.1 skrll
121 1.6 riastrad system_power_efficient_wq = alloc_ordered_workqueue("lnxpwrwq", 0);
122 1.6 riastrad if (system_long_wq == NULL)
123 1.6 riastrad goto fail2;
124 1.6 riastrad
125 1.1 skrll return 0;
126 1.2 riastrad
127 1.6 riastrad fail3: __unused
128 1.6 riastrad destroy_workqueue(system_power_efficient_wq);
129 1.6 riastrad fail2: destroy_workqueue(system_long_wq);
130 1.2 riastrad fail1: destroy_workqueue(system_wq);
131 1.3 riastrad fail0: mutex_destroy(&workqueues.lock);
132 1.3 riastrad return ENOMEM;
133 1.1 skrll }
134 1.1 skrll
135 1.1 skrll void
136 1.1 skrll linux_workqueue_fini(void)
137 1.1 skrll {
138 1.2 riastrad
139 1.2 riastrad destroy_workqueue(system_long_wq);
140 1.2 riastrad system_long_wq = NULL;
141 1.1 skrll destroy_workqueue(system_wq);
142 1.1 skrll system_wq = NULL;
143 1.3 riastrad KASSERT(RB_TREE_MIN(&workqueues.tree) == NULL);
144 1.3 riastrad mutex_destroy(&workqueues.lock);
145 1.3 riastrad }
146 1.3 riastrad
147 1.3 riastrad /*
149 1.3 riastrad * Table of workqueue LWPs for validation -- assumes there is only one
150 1.3 riastrad * thread per workqueue.
151 1.3 riastrad *
152 1.3 riastrad * XXX Mega-kludgerific!
153 1.3 riastrad */
154 1.3 riastrad
155 1.3 riastrad static int
156 1.3 riastrad compare_nodes(void *cookie, const void *va, const void *vb)
157 1.3 riastrad {
158 1.3 riastrad const struct workqueue_struct *wa = va;
159 1.3 riastrad const struct workqueue_struct *wb = vb;
160 1.3 riastrad
161 1.3 riastrad if ((uintptr_t)wa->wq_lwp < (uintptr_t)wb->wq_lwp)
162 1.3 riastrad return -1;
163 1.3 riastrad if ((uintptr_t)wa->wq_lwp > (uintptr_t)wb->wq_lwp)
164 1.3 riastrad return +1;
165 1.3 riastrad return 0;
166 1.3 riastrad }
167 1.3 riastrad
168 1.3 riastrad static int
169 1.3 riastrad compare_key(void *cookie, const void *vn, const void *vk)
170 1.3 riastrad {
171 1.3 riastrad const struct workqueue_struct *w = vn;
172 1.3 riastrad const struct lwp *lwp = vk;
173 1.3 riastrad
174 1.3 riastrad if ((uintptr_t)w->wq_lwp < (uintptr_t)lwp)
175 1.3 riastrad return -1;
176 1.3 riastrad if ((uintptr_t)w->wq_lwp > (uintptr_t)lwp)
177 1.3 riastrad return +1;
178 1.3 riastrad return 0;
179 1.3 riastrad }
180 1.3 riastrad
181 1.3 riastrad static const rb_tree_ops_t workqueues_rb_ops = {
182 1.3 riastrad .rbto_compare_nodes = compare_nodes,
183 1.3 riastrad .rbto_compare_key = compare_key,
184 1.3 riastrad .rbto_node_offset = offsetof(struct workqueue_struct, wq_lwp),
185 1.3 riastrad };
186 1.3 riastrad
187 1.3 riastrad struct wq_whoami_work {
188 1.3 riastrad kmutex_t www_lock;
189 1.3 riastrad kcondvar_t www_cv;
190 1.3 riastrad struct workqueue_struct *www_wq;
191 1.3 riastrad struct work_struct www_work;
192 1.3 riastrad };
193 1.3 riastrad
194 1.3 riastrad static void
195 1.3 riastrad workqueue_whoami_work(struct work_struct *work)
196 1.3 riastrad {
197 1.3 riastrad struct wq_whoami_work *www = www;
198 1.3 riastrad struct workqueue_struct *wq = www->www_wq;
199 1.3 riastrad
200 1.3 riastrad KASSERT(wq->wq_lwp == NULL);
201 1.3 riastrad wq->wq_lwp = curlwp;
202 1.3 riastrad
203 1.3 riastrad mutex_enter(&www->www_lock);
204 1.3 riastrad cv_broadcast(&www->www_cv);
205 1.3 riastrad mutex_exit(&www->www_lock);
206 1.3 riastrad }
207 1.3 riastrad
208 1.3 riastrad static void
209 1.3 riastrad workqueue_whoami(struct workqueue_struct *wq)
210 1.3 riastrad {
211 1.3 riastrad struct wq_whoami_work www;
212 1.3 riastrad struct workqueue_struct *collision __diagused;
213 1.3 riastrad
214 1.3 riastrad mutex_init(&www.www_lock, MUTEX_DEFAULT, IPL_NONE);
215 1.3 riastrad cv_init(&www.www_cv, "wqwhoami");
216 1.3 riastrad
217 1.3 riastrad INIT_WORK(&www.www_work, &workqueue_whoami_work);
218 1.3 riastrad queue_work(wq, &www.www_work);
219 1.3 riastrad
220 1.3 riastrad mutex_enter(&www.www_lock);
221 1.3 riastrad while (wq->wq_lwp == NULL)
222 1.3 riastrad cv_wait(&www.www_cv, &www.www_lock);
223 1.3 riastrad mutex_exit(&www.www_lock);
224 1.3 riastrad
225 1.3 riastrad cv_destroy(&www.www_cv);
226 1.3 riastrad mutex_destroy(&www.www_lock);
227 1.3 riastrad
228 1.3 riastrad mutex_enter(&workqueues.lock);
229 1.3 riastrad collision = rb_tree_insert_node(&workqueues.tree, wq);
230 1.3 riastrad mutex_exit(&workqueues.lock);
231 1.3 riastrad
232 1.3 riastrad KASSERT(collision == wq);
233 1.3 riastrad }
234 1.3 riastrad
235 1.3 riastrad struct work_struct *
236 1.3 riastrad current_work(void)
237 1.3 riastrad {
238 1.3 riastrad struct workqueue_struct *wq;
239 1.3 riastrad struct work_struct *work;
240 1.3 riastrad
241 1.3 riastrad mutex_enter(&workqueues.lock);
242 1.3 riastrad wq = rb_tree_find_node(&workqueues.tree, curlwp);
243 1.3 riastrad work = (wq == NULL ? NULL : wq->wq_current_work);
244 1.3 riastrad mutex_exit(&workqueues.lock);
245 1.3 riastrad
246 1.1 skrll return work;
247 1.1 skrll }
248 1.1 skrll
249 1.1 skrll /*
251 1.1 skrll * Workqueues
252 1.1 skrll */
253 1.1 skrll
254 1.1 skrll struct workqueue_struct *
255 1.1 skrll alloc_ordered_workqueue(const char *name, int linux_flags)
256 1.1 skrll {
257 1.1 skrll struct workqueue_struct *wq;
258 1.1 skrll int flags = WQ_MPSAFE;
259 1.1 skrll int error;
260 1.1 skrll
261 1.1 skrll KASSERT(linux_flags == 0);
262 1.1 skrll
263 1.1 skrll wq = kmem_alloc(sizeof(*wq), KM_SLEEP);
264 1.1 skrll error = workqueue_create(&wq->wq_workqueue, name, &linux_worker,
265 1.1 skrll wq, PRI_NONE, IPL_VM, flags);
266 1.1 skrll if (error) {
267 1.1 skrll kmem_free(wq, sizeof(*wq));
268 1.1 skrll return NULL;
269 1.1 skrll }
270 1.1 skrll
271 1.1 skrll mutex_init(&wq->wq_lock, MUTEX_DEFAULT, IPL_VM);
272 1.1 skrll cv_init(&wq->wq_cv, name);
273 1.1 skrll TAILQ_INIT(&wq->wq_delayed);
274 1.3 riastrad wq->wq_current_work = NULL;
275 1.3 riastrad
276 1.3 riastrad workqueue_whoami(wq);
277 1.1 skrll KASSERT(wq->wq_lwp != NULL);
278 1.1 skrll
279 1.1 skrll return wq;
280 1.1 skrll }
281 1.1 skrll
282 1.1 skrll void
283 1.1 skrll destroy_workqueue(struct workqueue_struct *wq)
284 1.1 skrll {
285 1.1 skrll
286 1.1 skrll /*
287 1.1 skrll * Cancel all delayed work.
288 1.1 skrll */
289 1.1 skrll for (;;) {
290 1.1 skrll struct delayed_work *dw;
291 1.1 skrll
292 1.1 skrll mutex_enter(&wq->wq_lock);
293 1.1 skrll if (TAILQ_EMPTY(&wq->wq_delayed)) {
294 1.1 skrll dw = NULL;
295 1.1 skrll } else {
296 1.1 skrll dw = TAILQ_FIRST(&wq->wq_delayed);
297 1.1 skrll TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
298 1.1 skrll }
299 1.1 skrll mutex_exit(&wq->wq_lock);
300 1.1 skrll
301 1.1 skrll if (dw == NULL)
302 1.1 skrll break;
303 1.1 skrll
304 1.1 skrll cancel_delayed_work_sync(dw);
305 1.1 skrll }
306 1.1 skrll
307 1.1 skrll /*
308 1.1 skrll * workqueue_destroy empties the queue; we need not wait for
309 1.1 skrll * completion explicitly. However, we can't destroy the
310 1.1 skrll * condvar or mutex until this is done.
311 1.1 skrll */
312 1.1 skrll workqueue_destroy(wq->wq_workqueue);
313 1.1 skrll KASSERT(wq->wq_current_work == NULL);
314 1.1 skrll wq->wq_workqueue = NULL;
315 1.1 skrll
316 1.1 skrll cv_destroy(&wq->wq_cv);
317 1.1 skrll mutex_destroy(&wq->wq_lock);
318 1.1 skrll
319 1.1 skrll kmem_free(wq, sizeof(*wq));
320 1.1 skrll }
321 1.1 skrll
322 1.1 skrll /*
324 1.1 skrll * Flush
325 1.1 skrll *
326 1.1 skrll * Note: This doesn't cancel or wait for delayed work. This seems to
327 1.1 skrll * match what Linux does (or, doesn't do).
328 1.1 skrll */
329 1.1 skrll
330 1.1 skrll void
331 1.1 skrll flush_scheduled_work(void)
332 1.1 skrll {
333 1.1 skrll flush_workqueue(system_wq);
334 1.1 skrll }
335 1.1 skrll
336 1.1 skrll struct wq_flush_work {
337 1.1 skrll struct work_struct wqfw_work;
338 1.1 skrll struct wq_flush *wqfw_flush;
339 1.1 skrll };
340 1.1 skrll
341 1.1 skrll struct wq_flush {
342 1.1 skrll kmutex_t wqf_lock;
343 1.1 skrll kcondvar_t wqf_cv;
344 1.1 skrll unsigned int wqf_n;
345 1.1 skrll };
346 1.1 skrll
347 1.1 skrll void
348 1.1 skrll flush_work(struct work_struct *work)
349 1.1 skrll {
350 1.1 skrll struct workqueue_struct *const wq = work->w_wq;
351 1.1 skrll
352 1.1 skrll if (wq != NULL)
353 1.1 skrll flush_workqueue(wq);
354 1.1 skrll }
355 1.1 skrll
356 1.1 skrll void
357 1.1 skrll flush_workqueue(struct workqueue_struct *wq)
358 1.1 skrll {
359 1.1 skrll static const struct wq_flush zero_wqf;
360 1.1 skrll struct wq_flush wqf = zero_wqf;
361 1.1 skrll
362 1.1 skrll mutex_init(&wqf.wqf_lock, MUTEX_DEFAULT, IPL_NONE);
363 1.1 skrll cv_init(&wqf.wqf_cv, "lnxwflsh");
364 1.1 skrll
365 1.1 skrll if (1) {
366 1.1 skrll struct wq_flush_work *const wqfw = kmem_zalloc(sizeof(*wqfw),
367 1.1 skrll KM_SLEEP);
368 1.1 skrll
369 1.1 skrll wqf.wqf_n = 1;
370 1.1 skrll wqfw->wqfw_flush = &wqf;
371 1.1 skrll INIT_WORK(&wqfw->wqfw_work, &linux_wq_barrier);
372 1.1 skrll wqfw->wqfw_work.w_wq = wq;
373 1.1 skrll wqfw->wqfw_work.w_state = WORK_PENDING;
374 1.1 skrll workqueue_enqueue(wq->wq_workqueue, &wqfw->wqfw_work.w_wk,
375 1.1 skrll NULL);
376 1.1 skrll } else {
377 1.1 skrll struct cpu_info *ci;
378 1.1 skrll CPU_INFO_ITERATOR cii;
379 1.1 skrll struct wq_flush_work *wqfw;
380 1.1 skrll
381 1.1 skrll panic("per-CPU Linux workqueues don't work yet!");
382 1.1 skrll
383 1.1 skrll wqf.wqf_n = 0;
384 1.1 skrll for (CPU_INFO_FOREACH(cii, ci)) {
385 1.1 skrll wqfw = kmem_zalloc(sizeof(*wqfw), KM_SLEEP);
386 1.1 skrll mutex_enter(&wqf.wqf_lock);
387 1.1 skrll wqf.wqf_n++;
388 1.1 skrll mutex_exit(&wqf.wqf_lock);
389 1.1 skrll wqfw->wqfw_flush = &wqf;
390 1.1 skrll INIT_WORK(&wqfw->wqfw_work, &linux_wq_barrier);
391 1.1 skrll wqfw->wqfw_work.w_state = WORK_PENDING;
392 1.1 skrll wqfw->wqfw_work.w_wq = wq;
393 1.1 skrll workqueue_enqueue(wq->wq_workqueue,
394 1.1 skrll &wqfw->wqfw_work.w_wk, ci);
395 1.1 skrll }
396 1.1 skrll }
397 1.1 skrll
398 1.1 skrll mutex_enter(&wqf.wqf_lock);
399 1.1 skrll while (0 < wqf.wqf_n)
400 1.1 skrll cv_wait(&wqf.wqf_cv, &wqf.wqf_lock);
401 1.1 skrll mutex_exit(&wqf.wqf_lock);
402 1.1 skrll
403 1.1 skrll cv_destroy(&wqf.wqf_cv);
404 1.1 skrll mutex_destroy(&wqf.wqf_lock);
405 1.1 skrll }
406 1.1 skrll
407 1.1 skrll static void
408 1.1 skrll linux_wq_barrier(struct work_struct *work)
409 1.1 skrll {
410 1.1 skrll struct wq_flush_work *const wqfw = container_of(work,
411 1.1 skrll struct wq_flush_work, wqfw_work);
412 1.1 skrll struct wq_flush *const wqf = wqfw->wqfw_flush;
413 1.1 skrll
414 1.1 skrll mutex_enter(&wqf->wqf_lock);
415 1.1 skrll if (--wqf->wqf_n == 0)
416 1.1 skrll cv_broadcast(&wqf->wqf_cv);
417 1.1 skrll mutex_exit(&wqf->wqf_lock);
418 1.1 skrll
419 1.1 skrll kmem_free(wqfw, sizeof(*wqfw));
420 1.1 skrll }
421 1.1 skrll
422 1.1 skrll /*
424 1.1 skrll * Work locking
425 1.1 skrll *
426 1.1 skrll * We use __cpu_simple_lock(9) rather than mutex(9) because Linux code
427 1.1 skrll * does not destroy work, so there is nowhere to call mutex_destroy.
428 1.1 skrll *
429 1.1 skrll * XXX This is getting out of hand... Really, work items shouldn't
430 1.1 skrll * have locks in them at all; instead the workqueues should.
431 1.1 skrll */
432 1.1 skrll
433 1.1 skrll static void
434 1.1 skrll linux_work_lock_init(struct work_struct *work)
435 1.1 skrll {
436 1.1 skrll
437 1.1 skrll __cpu_simple_lock_init(&work->w_lock);
438 1.1 skrll }
439 1.1 skrll
440 1.1 skrll static void
441 1.1 skrll linux_work_lock(struct work_struct *work)
442 1.1 skrll {
443 1.1 skrll struct cpu_info *ci;
444 1.1 skrll int cnt, s;
445 1.1 skrll
446 1.1 skrll /* XXX Copypasta of MUTEX_SPIN_SPLRAISE. */
447 1.1 skrll s = splvm();
448 1.1 skrll ci = curcpu();
449 1.1 skrll cnt = ci->ci_mtx_count--;
450 1.1 skrll __insn_barrier();
451 1.1 skrll if (cnt == 0)
452 1.1 skrll ci->ci_mtx_oldspl = s;
453 1.1 skrll
454 1.1 skrll __cpu_simple_lock(&work->w_lock);
455 1.1 skrll }
456 1.1 skrll
457 1.1 skrll static void
458 1.1 skrll linux_work_unlock(struct work_struct *work)
459 1.1 skrll {
460 1.1 skrll struct cpu_info *ci;
461 1.1 skrll int s;
462 1.1 skrll
463 1.1 skrll __cpu_simple_unlock(&work->w_lock);
464 1.1 skrll
465 1.1 skrll /* XXX Copypasta of MUTEX_SPIN_SPLRESTORE. */
466 1.1 skrll ci = curcpu();
467 1.1 skrll s = ci->ci_mtx_oldspl;
468 1.1 skrll __insn_barrier();
469 1.1 skrll if (++ci->ci_mtx_count == 0)
470 1.1 skrll splx(s);
471 1.1 skrll }
472 1.1 skrll
473 1.1 skrll static bool __diagused
474 1.1 skrll linux_work_locked(struct work_struct *work)
475 1.1 skrll {
476 1.1 skrll return __SIMPLELOCK_LOCKED_P(&work->w_lock);
477 1.1 skrll }
478 1.1 skrll
479 1.1 skrll /*
481 1.1 skrll * Work
482 1.1 skrll */
483 1.1 skrll
484 1.1 skrll void
485 1.1 skrll INIT_WORK(struct work_struct *work, void (*fn)(struct work_struct *))
486 1.4 riastrad {
487 1.1 skrll
488 1.1 skrll linux_work_lock_init(work);
489 1.1 skrll work->w_state = WORK_IDLE;
490 1.1 skrll work->w_wq = NULL;
491 1.1 skrll work->func = fn;
492 1.1 skrll }
493 1.1 skrll
494 1.1 skrll bool
495 1.1 skrll schedule_work(struct work_struct *work)
496 1.1 skrll {
497 1.1 skrll return queue_work(system_wq, work);
498 1.1 skrll }
499 1.1 skrll
500 1.1 skrll bool
501 1.1 skrll queue_work(struct workqueue_struct *wq, struct work_struct *work)
502 1.1 skrll {
503 1.1 skrll /* True if we put it on the queue, false if it was already there. */
504 1.1 skrll bool newly_queued;
505 1.1 skrll
506 1.1 skrll KASSERT(wq != NULL);
507 1.1 skrll
508 1.1 skrll linux_work_lock(work);
509 1.1 skrll switch (work->w_state) {
510 1.1 skrll case WORK_IDLE:
511 1.1 skrll case WORK_INVOKED:
512 1.1 skrll work->w_state = WORK_PENDING;
513 1.1 skrll work->w_wq = wq;
514 1.1 skrll workqueue_enqueue(wq->wq_workqueue, &work->w_wk, NULL);
515 1.1 skrll newly_queued = true;
516 1.1 skrll break;
517 1.1 skrll
518 1.1 skrll case WORK_DELAYED:
519 1.1 skrll panic("queue_work(delayed work %p)", work);
520 1.1 skrll break;
521 1.1 skrll
522 1.1 skrll case WORK_PENDING:
523 1.1 skrll KASSERT(work->w_wq == wq);
524 1.1 skrll newly_queued = false;
525 1.1 skrll break;
526 1.1 skrll
527 1.1 skrll case WORK_CANCELLED:
528 1.1 skrll newly_queued = false;
529 1.1 skrll break;
530 1.1 skrll
531 1.1 skrll case WORK_DELAYED_CANCELLED:
532 1.1 skrll panic("queue_work(delayed work %p)", work);
533 1.1 skrll break;
534 1.1 skrll
535 1.1 skrll default:
536 1.1 skrll panic("work %p in bad state: %d", work, (int)work->w_state);
537 1.1 skrll break;
538 1.1 skrll }
539 1.1 skrll linux_work_unlock(work);
540 1.1 skrll
541 1.1 skrll return newly_queued;
542 1.1 skrll }
543 1.1 skrll
544 1.1 skrll bool
545 1.1 skrll cancel_work_sync(struct work_struct *work)
546 1.1 skrll {
547 1.1 skrll bool cancelled_p = false;
548 1.1 skrll
549 1.1 skrll linux_work_lock(work);
550 1.1 skrll switch (work->w_state) {
551 1.1 skrll case WORK_IDLE: /* Nothing to do. */
552 1.1 skrll break;
553 1.1 skrll
554 1.1 skrll case WORK_DELAYED:
555 1.1 skrll panic("cancel_work_sync(delayed work %p)", work);
556 1.1 skrll break;
557 1.1 skrll
558 1.1 skrll case WORK_PENDING:
559 1.1 skrll work->w_state = WORK_CANCELLED;
560 1.1 skrll linux_wait_for_cancelled_work(work);
561 1.1 skrll cancelled_p = true;
562 1.1 skrll break;
563 1.1 skrll
564 1.1 skrll case WORK_INVOKED:
565 1.1 skrll linux_wait_for_invoked_work(work);
566 1.1 skrll break;
567 1.1 skrll
568 1.1 skrll case WORK_CANCELLED: /* Already done. */
569 1.1 skrll break;
570 1.1 skrll
571 1.1 skrll case WORK_DELAYED_CANCELLED:
572 1.1 skrll panic("cancel_work_sync(delayed work %p)", work);
573 1.1 skrll break;
574 1.1 skrll
575 1.1 skrll default:
576 1.1 skrll panic("work %p in bad state: %d", work, (int)work->w_state);
577 1.1 skrll break;
578 1.1 skrll }
579 1.1 skrll linux_work_unlock(work);
580 1.1 skrll
581 1.1 skrll return cancelled_p;
582 1.1 skrll }
583 1.1 skrll
584 1.1 skrll static void
585 1.1 skrll linux_wait_for_cancelled_work(struct work_struct *work)
586 1.1 skrll {
587 1.1 skrll struct workqueue_struct *wq;
588 1.1 skrll
589 1.1 skrll KASSERT(linux_work_locked(work));
590 1.1 skrll KASSERT(work->w_state == WORK_CANCELLED);
591 1.1 skrll
592 1.1 skrll wq = work->w_wq;
593 1.1 skrll do {
594 1.1 skrll mutex_enter(&wq->wq_lock);
595 1.1 skrll linux_work_unlock(work);
596 1.1 skrll cv_wait(&wq->wq_cv, &wq->wq_lock);
597 1.1 skrll mutex_exit(&wq->wq_lock);
598 1.1 skrll linux_work_lock(work);
599 1.1 skrll } while ((work->w_state == WORK_CANCELLED) && (work->w_wq == wq));
600 1.1 skrll }
601 1.1 skrll
602 1.1 skrll static void
603 1.1 skrll linux_wait_for_invoked_work(struct work_struct *work)
604 1.1 skrll {
605 1.1 skrll struct workqueue_struct *wq;
606 1.1 skrll
607 1.1 skrll KASSERT(linux_work_locked(work));
608 1.1 skrll KASSERT(work->w_state == WORK_INVOKED);
609 1.1 skrll
610 1.1 skrll wq = work->w_wq;
611 1.1 skrll mutex_enter(&wq->wq_lock);
612 1.1 skrll linux_work_unlock(work);
613 1.1 skrll while (wq->wq_current_work == work)
614 1.1 skrll cv_wait(&wq->wq_cv, &wq->wq_lock);
615 1.1 skrll mutex_exit(&wq->wq_lock);
616 1.1 skrll
617 1.1 skrll linux_work_lock(work); /* XXX needless relock */
618 1.1 skrll }
619 1.1 skrll
620 1.1 skrll static void
621 1.1 skrll linux_worker(struct work *wk, void *arg)
622 1.1 skrll {
623 1.1 skrll struct work_struct *const work = container_of(wk, struct work_struct,
624 1.1 skrll w_wk);
625 1.1 skrll struct workqueue_struct *const wq = arg;
626 1.1 skrll
627 1.1 skrll linux_work_lock(work);
628 1.1 skrll switch (work->w_state) {
629 1.1 skrll case WORK_IDLE:
630 1.1 skrll panic("idle work %p got queued: %p", work, wq);
631 1.1 skrll break;
632 1.1 skrll
633 1.1 skrll case WORK_DELAYED:
634 1.1 skrll panic("delayed work %p got queued: %p", work, wq);
635 1.1 skrll break;
636 1.1 skrll
637 1.1 skrll case WORK_PENDING:
638 1.1 skrll KASSERT(work->w_wq == wq);
639 1.1 skrll
640 1.1 skrll /* Get ready to invoke this one. */
641 1.1 skrll mutex_enter(&wq->wq_lock);
642 1.1 skrll work->w_state = WORK_INVOKED;
643 1.1 skrll KASSERT(wq->wq_current_work == NULL);
644 1.4 riastrad wq->wq_current_work = work;
645 1.1 skrll mutex_exit(&wq->wq_lock);
646 1.1 skrll
647 1.1 skrll /* Unlock it and do it. Can't use work after this. */
648 1.1 skrll linux_work_unlock(work);
649 1.1 skrll (*work->func)(work);
650 1.1 skrll
651 1.1 skrll /* All done. Notify anyone waiting for completion. */
652 1.1 skrll mutex_enter(&wq->wq_lock);
653 1.1 skrll KASSERT(wq->wq_current_work == work);
654 1.1 skrll wq->wq_current_work = NULL;
655 1.1 skrll cv_broadcast(&wq->wq_cv);
656 1.1 skrll mutex_exit(&wq->wq_lock);
657 1.1 skrll return;
658 1.1 skrll
659 1.1 skrll case WORK_INVOKED:
660 1.1 skrll panic("invoked work %p got requeued: %p", work, wq);
661 1.1 skrll break;
662 1.1 skrll
663 1.1 skrll case WORK_CANCELLED:
664 1.1 skrll KASSERT(work->w_wq == wq);
665 1.1 skrll
666 1.1 skrll /* Return to idle; notify anyone waiting for cancellation. */
667 1.1 skrll mutex_enter(&wq->wq_lock);
668 1.1 skrll work->w_state = WORK_IDLE;
669 1.1 skrll work->w_wq = NULL;
670 1.1 skrll cv_broadcast(&wq->wq_cv);
671 1.1 skrll mutex_exit(&wq->wq_lock);
672 1.1 skrll break;
673 1.1 skrll
674 1.1 skrll case WORK_DELAYED_CANCELLED:
675 1.1 skrll panic("cancelled delayed work %p got uqeued: %p", work, wq);
676 1.1 skrll break;
677 1.1 skrll
678 1.1 skrll default:
679 1.1 skrll panic("work %p in bad state: %d", work, (int)work->w_state);
680 1.1 skrll break;
681 1.1 skrll }
682 1.1 skrll linux_work_unlock(work);
683 1.1 skrll }
684 1.1 skrll
685 1.1 skrll /*
687 1.1 skrll * Delayed work
688 1.1 skrll */
689 1.1 skrll
690 1.1 skrll void
691 1.1 skrll INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work_struct *))
692 1.1 skrll {
693 1.1 skrll INIT_WORK(&dw->work, fn);
694 1.1 skrll }
695 1.1 skrll
696 1.1 skrll bool
697 1.1 skrll schedule_delayed_work(struct delayed_work *dw, unsigned long ticks)
698 1.1 skrll {
699 1.1 skrll return queue_delayed_work(system_wq, dw, ticks);
700 1.1 skrll }
701 1.1 skrll
702 1.1 skrll bool
703 1.1 skrll queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
704 1.1 skrll unsigned long ticks)
705 1.1 skrll {
706 1.1 skrll bool newly_queued;
707 1.1 skrll
708 1.1 skrll KASSERT(wq != NULL);
709 1.1 skrll
710 1.1 skrll linux_work_lock(&dw->work);
711 1.1 skrll switch (dw->work.w_state) {
712 1.1 skrll case WORK_IDLE:
713 1.1 skrll case WORK_INVOKED:
714 1.1 skrll if (ticks == 0) {
715 1.1 skrll /* Skip the delay and queue it now. */
716 1.1 skrll dw->work.w_state = WORK_PENDING;
717 1.1 skrll dw->work.w_wq = wq;
718 1.1 skrll workqueue_enqueue(wq->wq_workqueue, &dw->work.w_wk,
719 1.1 skrll NULL);
720 1.1 skrll } else {
721 1.1 skrll callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
722 1.1 skrll callout_reset(&dw->dw_callout, ticks,
723 1.1 skrll &linux_worker_intr, dw);
724 1.1 skrll dw->work.w_state = WORK_DELAYED;
725 1.1 skrll dw->work.w_wq = wq;
726 1.1 skrll mutex_enter(&wq->wq_lock);
727 1.1 skrll TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
728 1.1 skrll mutex_exit(&wq->wq_lock);
729 1.1 skrll }
730 1.1 skrll newly_queued = true;
731 1.1 skrll break;
732 1.1 skrll
733 1.1 skrll case WORK_DELAYED:
734 1.1 skrll /*
735 1.1 skrll * Timer is already ticking. Leave it to time out
736 1.1 skrll * whenever it was going to time out, as Linux does --
737 1.1 skrll * neither speed it up nor postpone it.
738 1.1 skrll */
739 1.1 skrll newly_queued = false;
740 1.1 skrll break;
741 1.1 skrll
742 1.1 skrll case WORK_PENDING:
743 1.1 skrll KASSERT(dw->work.w_wq == wq);
744 1.1 skrll newly_queued = false;
745 1.1 skrll break;
746 1.1 skrll
747 1.1 skrll case WORK_CANCELLED:
748 1.1 skrll case WORK_DELAYED_CANCELLED:
749 1.1 skrll /* XXX Wait for cancellation and then queue? */
750 1.1 skrll newly_queued = false;
751 1.1 skrll break;
752 1.1 skrll
753 1.1 skrll default:
754 1.1 skrll panic("delayed work %p in bad state: %d", dw,
755 1.1 skrll (int)dw->work.w_state);
756 1.1 skrll break;
757 1.1 skrll }
758 1.1 skrll linux_work_unlock(&dw->work);
759 1.1 skrll
760 1.1 skrll return newly_queued;
761 1.1 skrll }
762 1.1 skrll
763 1.1 skrll bool
764 1.1 skrll mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
765 1.1 skrll unsigned long ticks)
766 1.1 skrll {
767 1.1 skrll bool timer_modified;
768 1.1 skrll
769 1.1 skrll KASSERT(wq != NULL);
770 1.1 skrll
771 1.1 skrll linux_work_lock(&dw->work);
772 1.1 skrll switch (dw->work.w_state) {
773 1.1 skrll case WORK_IDLE:
774 1.1 skrll case WORK_INVOKED:
775 1.1 skrll if (ticks == 0) {
776 1.1 skrll /* Skip the delay and queue it now. */
777 1.1 skrll dw->work.w_state = WORK_PENDING;
778 1.1 skrll dw->work.w_wq = wq;
779 1.1 skrll workqueue_enqueue(wq->wq_workqueue, &dw->work.w_wk,
780 1.1 skrll NULL);
781 1.1 skrll } else {
782 1.1 skrll callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
783 1.1 skrll callout_reset(&dw->dw_callout, ticks,
784 1.1 skrll &linux_worker_intr, dw);
785 1.1 skrll dw->work.w_state = WORK_DELAYED;
786 1.1 skrll dw->work.w_wq = wq;
787 1.1 skrll mutex_enter(&wq->wq_lock);
788 1.1 skrll TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
789 1.1 skrll mutex_exit(&wq->wq_lock);
790 1.1 skrll }
791 1.1 skrll timer_modified = false;
792 1.1 skrll break;
793 1.1 skrll
794 1.1 skrll case WORK_DELAYED:
795 1.1 skrll /*
796 1.1 skrll * Timer is already ticking. Reschedule it.
797 1.1 skrll */
798 1.1 skrll callout_schedule(&dw->dw_callout, ticks);
799 1.1 skrll timer_modified = true;
800 1.1 skrll break;
801 1.1 skrll
802 1.1 skrll case WORK_PENDING:
803 1.1 skrll KASSERT(dw->work.w_wq == wq);
804 1.1 skrll timer_modified = false;
805 1.1 skrll break;
806 1.1 skrll
807 1.1 skrll case WORK_CANCELLED:
808 1.1 skrll case WORK_DELAYED_CANCELLED:
809 1.1 skrll /* XXX Wait for cancellation and then queue? */
810 1.1 skrll timer_modified = false;
811 1.1 skrll break;
812 1.1 skrll
813 1.1 skrll default:
814 1.1 skrll panic("delayed work %p in bad state: %d", dw,
815 1.1 skrll (int)dw->work.w_state);
816 1.1 skrll break;
817 1.1 skrll }
818 1.1 skrll linux_work_unlock(&dw->work);
819 1.1 skrll
820 1.1 skrll return timer_modified;
821 1.1 skrll }
822 1.1 skrll
823 1.1 skrll bool
824 1.1 skrll cancel_delayed_work(struct delayed_work *dw)
825 1.1 skrll {
826 1.1 skrll bool cancelled_p = false;
827 1.1 skrll
828 1.1 skrll linux_work_lock(&dw->work);
829 1.1 skrll switch (dw->work.w_state) {
830 1.1 skrll case WORK_IDLE: /* Nothing to do. */
831 1.1 skrll break;
832 1.1 skrll
833 1.1 skrll case WORK_DELAYED:
834 1.1 skrll dw->work.w_state = WORK_DELAYED_CANCELLED;
835 1.1 skrll linux_cancel_delayed_work_callout(dw, false);
836 1.1 skrll cancelled_p = true;
837 1.1 skrll break;
838 1.1 skrll
839 1.1 skrll case WORK_PENDING:
840 1.1 skrll dw->work.w_state = WORK_CANCELLED;
841 1.1 skrll cancelled_p = true;
842 1.1 skrll break;
843 1.1 skrll
844 1.1 skrll case WORK_INVOKED: /* Don't wait! */
845 1.1 skrll break;
846 1.1 skrll
847 1.1 skrll case WORK_CANCELLED: /* Already done. */
848 1.1 skrll case WORK_DELAYED_CANCELLED:
849 1.1 skrll break;
850 1.1 skrll
851 1.1 skrll default:
852 1.1 skrll panic("delayed work %p in bad state: %d", dw,
853 1.1 skrll (int)dw->work.w_state);
854 1.1 skrll break;
855 1.1 skrll }
856 1.1 skrll linux_work_unlock(&dw->work);
857 1.1 skrll
858 1.1 skrll return cancelled_p;
859 1.1 skrll }
860 1.1 skrll
861 1.1 skrll bool
862 1.1 skrll cancel_delayed_work_sync(struct delayed_work *dw)
863 1.1 skrll {
864 1.1 skrll bool cancelled_p = false;
865 1.1 skrll
866 1.1 skrll linux_work_lock(&dw->work);
867 1.1 skrll switch (dw->work.w_state) {
868 1.1 skrll case WORK_IDLE: /* Nothing to do. */
869 1.1 skrll break;
870 1.1 skrll
871 1.1 skrll case WORK_DELAYED:
872 1.1 skrll dw->work.w_state = WORK_DELAYED_CANCELLED;
873 1.1 skrll linux_cancel_delayed_work_callout(dw, true);
874 1.1 skrll cancelled_p = true;
875 1.1 skrll break;
876 1.1 skrll
877 1.1 skrll case WORK_PENDING:
878 1.1 skrll dw->work.w_state = WORK_CANCELLED;
879 1.1 skrll linux_wait_for_cancelled_work(&dw->work);
880 1.1 skrll cancelled_p = true;
881 1.1 skrll break;
882 1.1 skrll
883 1.1 skrll case WORK_INVOKED:
884 1.1 skrll linux_wait_for_invoked_work(&dw->work);
885 1.1 skrll break;
886 1.1 skrll
887 1.1 skrll case WORK_CANCELLED: /* Already done. */
888 1.1 skrll break;
889 1.1 skrll
890 1.1 skrll case WORK_DELAYED_CANCELLED:
891 1.1 skrll linux_wait_for_delayed_cancelled_work(dw);
892 1.1 skrll break;
893 1.1 skrll
894 1.1 skrll default:
895 1.1 skrll panic("delayed work %p in bad state: %d", dw,
896 1.1 skrll (int)dw->work.w_state);
897 1.1 skrll break;
898 1.5 riastrad }
899 1.5 riastrad linux_work_unlock(&dw->work);
900 1.5 riastrad
901 1.5 riastrad return cancelled_p;
902 1.5 riastrad }
903 1.5 riastrad
904 1.5 riastrad void
905 1.5 riastrad flush_delayed_work(struct delayed_work *dw)
906 1.5 riastrad {
907 1.1 skrll struct workqueue_struct *wq = dw->work.w_wq;
908 1.1 skrll
909 1.1 skrll if (wq != NULL)
910 1.1 skrll flush_workqueue(wq);
911 1.1 skrll }
912 1.1 skrll
913 1.1 skrll static void
914 1.1 skrll linux_cancel_delayed_work_callout(struct delayed_work *dw, bool wait)
915 1.1 skrll {
916 1.1 skrll bool fired_p;
917 1.1 skrll
918 1.1 skrll KASSERT(linux_work_locked(&dw->work));
919 1.1 skrll KASSERT(dw->work.w_state == WORK_DELAYED_CANCELLED);
920 1.1 skrll
921 1.1 skrll if (wait) {
922 1.1 skrll /*
923 1.1 skrll * We unlock, halt, and then relock, rather than
924 1.1 skrll * passing an interlock to callout_halt, for two
925 1.1 skrll * reasons:
926 1.1 skrll *
927 1.1 skrll * (1) The work lock is not a mutex(9), so we can't use it.
928 1.1 skrll * (2) The WORK_DELAYED_CANCELLED state serves as an interlock.
929 1.1 skrll */
930 1.1 skrll linux_work_unlock(&dw->work);
931 1.1 skrll fired_p = callout_halt(&dw->dw_callout, NULL);
932 1.1 skrll linux_work_lock(&dw->work);
933 1.1 skrll } else {
934 1.1 skrll fired_p = callout_stop(&dw->dw_callout);
935 1.1 skrll }
936 1.1 skrll
937 1.1 skrll /*
938 1.1 skrll * fired_p means we didn't cancel the callout, so it must have
939 1.1 skrll * already begun and will clean up after itself.
940 1.1 skrll *
941 1.1 skrll * !fired_p means we cancelled it so we have to clean up after
942 1.1 skrll * it. Nobody else should have changed the state in that case.
943 1.1 skrll */
944 1.1 skrll if (!fired_p) {
945 1.1 skrll struct workqueue_struct *wq;
946 1.1 skrll
947 1.1 skrll KASSERT(linux_work_locked(&dw->work));
948 1.1 skrll KASSERT(dw->work.w_state == WORK_DELAYED_CANCELLED);
949 1.1 skrll
950 1.1 skrll wq = dw->work.w_wq;
951 1.1 skrll mutex_enter(&wq->wq_lock);
952 1.1 skrll TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
953 1.1 skrll callout_destroy(&dw->dw_callout);
954 1.1 skrll dw->work.w_state = WORK_IDLE;
955 1.1 skrll dw->work.w_wq = NULL;
956 1.1 skrll cv_broadcast(&wq->wq_cv);
957 1.1 skrll mutex_exit(&wq->wq_lock);
958 1.1 skrll }
959 1.1 skrll }
960 1.1 skrll
961 1.1 skrll static void
962 1.1 skrll linux_wait_for_delayed_cancelled_work(struct delayed_work *dw)
963 1.1 skrll {
964 1.1 skrll struct workqueue_struct *wq;
965 1.1 skrll
966 1.1 skrll KASSERT(linux_work_locked(&dw->work));
967 1.1 skrll KASSERT(dw->work.w_state == WORK_DELAYED_CANCELLED);
968 1.1 skrll
969 1.1 skrll wq = dw->work.w_wq;
970 1.1 skrll do {
971 1.1 skrll mutex_enter(&wq->wq_lock);
972 1.1 skrll linux_work_unlock(&dw->work);
973 1.1 skrll cv_wait(&wq->wq_cv, &wq->wq_lock);
974 1.1 skrll mutex_exit(&wq->wq_lock);
975 1.1 skrll linux_work_lock(&dw->work);
976 1.1 skrll } while ((dw->work.w_state == WORK_DELAYED_CANCELLED) &&
977 1.1 skrll (dw->work.w_wq == wq));
978 1.1 skrll }
979 1.1 skrll
980 1.1 skrll static void
981 1.1 skrll linux_worker_intr(void *arg)
982 1.1 skrll {
983 1.1 skrll struct delayed_work *dw = arg;
984 1.1 skrll struct workqueue_struct *wq;
985 1.1 skrll
986 1.1 skrll linux_work_lock(&dw->work);
987 1.1 skrll
988 1.1 skrll KASSERT((dw->work.w_state == WORK_DELAYED) ||
989 1.1 skrll (dw->work.w_state == WORK_DELAYED_CANCELLED));
990 1.1 skrll
991 1.1 skrll wq = dw->work.w_wq;
992 1.1 skrll mutex_enter(&wq->wq_lock);
993 1.1 skrll
994 1.1 skrll /* Queue the work, or return it to idle and alert any cancellers. */
995 1.1 skrll if (__predict_true(dw->work.w_state == WORK_DELAYED)) {
996 1.1 skrll dw->work.w_state = WORK_PENDING;
997 1.1 skrll workqueue_enqueue(dw->work.w_wq->wq_workqueue, &dw->work.w_wk,
998 1.1 skrll NULL);
999 1.1 skrll } else {
1000 1.1 skrll KASSERT(dw->work.w_state == WORK_DELAYED_CANCELLED);
1001 1.1 skrll dw->work.w_state = WORK_IDLE;
1002 1.1 skrll dw->work.w_wq = NULL;
1003 1.1 skrll cv_broadcast(&wq->wq_cv);
1004 1.1 skrll }
1005 1.1 skrll
1006 1.1 skrll /* Either way, the callout is done. */
1007 TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
1008 callout_destroy(&dw->dw_callout);
1009
1010 mutex_exit(&wq->wq_lock);
1011 linux_work_unlock(&dw->work);
1012 }
1013