linux_work.c revision 1.37 1 1.2 riastrad /* $NetBSD: linux_work.c,v 1.37 2018/08/27 15:05:30 riastradh Exp $ */
2 1.1 skrll
3 1.1 skrll /*-
4 1.12 riastrad * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 skrll * All rights reserved.
6 1.1 skrll *
7 1.1 skrll * This code is derived from software contributed to The NetBSD Foundation
8 1.1 skrll * by Taylor R. Campbell.
9 1.1 skrll *
10 1.1 skrll * Redistribution and use in source and binary forms, with or without
11 1.1 skrll * modification, are permitted provided that the following conditions
12 1.1 skrll * are met:
13 1.1 skrll * 1. Redistributions of source code must retain the above copyright
14 1.1 skrll * notice, this list of conditions and the following disclaimer.
15 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 skrll * notice, this list of conditions and the following disclaimer in the
17 1.1 skrll * documentation and/or other materials provided with the distribution.
18 1.1 skrll *
19 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.1 skrll */
31 1.1 skrll
32 1.1 skrll #include <sys/cdefs.h>
33 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_work.c,v 1.37 2018/08/27 15:05:30 riastradh Exp $");
34 1.1 skrll
35 1.1 skrll #include <sys/types.h>
36 1.1 skrll #include <sys/atomic.h>
37 1.1 skrll #include <sys/callout.h>
38 1.1 skrll #include <sys/condvar.h>
39 1.1 skrll #include <sys/errno.h>
40 1.1 skrll #include <sys/kmem.h>
41 1.12 riastrad #include <sys/kthread.h>
42 1.12 riastrad #include <sys/lwp.h>
43 1.1 skrll #include <sys/mutex.h>
44 1.1 skrll #include <sys/queue.h>
45 1.1 skrll
46 1.1 skrll #include <linux/workqueue.h>
47 1.1 skrll
48 1.1 skrll struct workqueue_struct {
49 1.1 skrll kmutex_t wq_lock;
50 1.1 skrll kcondvar_t wq_cv;
51 1.1 skrll TAILQ_HEAD(, delayed_work) wq_delayed;
52 1.12 riastrad TAILQ_HEAD(, work_struct) wq_queue;
53 1.1 skrll struct work_struct *wq_current_work;
54 1.12 riastrad int wq_flags;
55 1.37 riastrad bool wq_requeued;
56 1.37 riastrad bool wq_dying;
57 1.37 riastrad uint64_t wq_gen;
58 1.12 riastrad struct lwp *wq_lwp;
59 1.1 skrll };
60 1.1 skrll
61 1.12 riastrad static void __dead linux_workqueue_thread(void *);
62 1.12 riastrad static void linux_workqueue_timeout(void *);
63 1.17 riastrad static struct workqueue_struct *
64 1.17 riastrad acquire_work(struct work_struct *,
65 1.17 riastrad struct workqueue_struct *);
66 1.17 riastrad static void release_work(struct work_struct *,
67 1.17 riastrad struct workqueue_struct *);
68 1.33 riastrad static void wait_for_current_work(struct work_struct *,
69 1.33 riastrad struct workqueue_struct *);
70 1.30 riastrad static void dw_callout_init(struct workqueue_struct *,
71 1.30 riastrad struct delayed_work *);
72 1.31 riastrad static void dw_callout_destroy(struct workqueue_struct *,
73 1.31 riastrad struct delayed_work *);
74 1.23 riastrad static void cancel_delayed_work_done(struct workqueue_struct *,
75 1.23 riastrad struct delayed_work *);
76 1.12 riastrad
77 1.12 riastrad static specificdata_key_t workqueue_key __read_mostly;
78 1.12 riastrad
79 1.12 riastrad struct workqueue_struct *system_wq __read_mostly;
80 1.12 riastrad struct workqueue_struct *system_long_wq __read_mostly;
81 1.12 riastrad struct workqueue_struct *system_power_efficient_wq __read_mostly;
82 1.3 riastrad
83 1.36 riastrad /*
84 1.36 riastrad * linux_workqueue_init()
85 1.36 riastrad *
86 1.36 riastrad * Initialize the Linux workqueue subsystem. Return 0 on success,
87 1.36 riastrad * NetBSD error on failure.
88 1.36 riastrad */
89 1.1 skrll int
90 1.1 skrll linux_workqueue_init(void)
91 1.1 skrll {
92 1.12 riastrad int error;
93 1.3 riastrad
94 1.12 riastrad error = lwp_specific_key_create(&workqueue_key, NULL);
95 1.12 riastrad if (error)
96 1.12 riastrad goto fail0;
97 1.1 skrll
98 1.1 skrll system_wq = alloc_ordered_workqueue("lnxsyswq", 0);
99 1.12 riastrad if (system_wq == NULL) {
100 1.12 riastrad error = ENOMEM;
101 1.12 riastrad goto fail1;
102 1.12 riastrad }
103 1.2 riastrad
104 1.2 riastrad system_long_wq = alloc_ordered_workqueue("lnxlngwq", 0);
105 1.12 riastrad if (system_long_wq == NULL) {
106 1.12 riastrad error = ENOMEM;
107 1.12 riastrad goto fail2;
108 1.12 riastrad }
109 1.1 skrll
110 1.6 riastrad system_power_efficient_wq = alloc_ordered_workqueue("lnxpwrwq", 0);
111 1.12 riastrad if (system_long_wq == NULL) {
112 1.12 riastrad error = ENOMEM;
113 1.12 riastrad goto fail3;
114 1.12 riastrad }
115 1.6 riastrad
116 1.1 skrll return 0;
117 1.2 riastrad
118 1.12 riastrad fail4: __unused
119 1.6 riastrad destroy_workqueue(system_power_efficient_wq);
120 1.12 riastrad fail3: destroy_workqueue(system_long_wq);
121 1.12 riastrad fail2: destroy_workqueue(system_wq);
122 1.12 riastrad fail1: lwp_specific_key_delete(workqueue_key);
123 1.12 riastrad fail0: KASSERT(error);
124 1.12 riastrad return error;
125 1.1 skrll }
126 1.1 skrll
127 1.36 riastrad /*
128 1.36 riastrad * linux_workqueue_fini()
129 1.36 riastrad *
130 1.36 riastrad * Destroy the Linux workqueue subsystem. Never fails.
131 1.36 riastrad */
132 1.1 skrll void
133 1.1 skrll linux_workqueue_fini(void)
134 1.1 skrll {
135 1.2 riastrad
136 1.12 riastrad destroy_workqueue(system_power_efficient_wq);
137 1.2 riastrad destroy_workqueue(system_long_wq);
138 1.1 skrll destroy_workqueue(system_wq);
139 1.12 riastrad lwp_specific_key_delete(workqueue_key);
140 1.1 skrll }
141 1.1 skrll
142 1.1 skrll /*
144 1.1 skrll * Workqueues
145 1.1 skrll */
146 1.36 riastrad
147 1.36 riastrad /*
148 1.36 riastrad * alloc_ordered_workqueue(name, flags)
149 1.36 riastrad *
150 1.36 riastrad * Create a workqueue of the given name. No flags are currently
151 1.36 riastrad * defined. Return NULL on failure, pointer to struct
152 1.36 riastrad * workqueue_struct object on success.
153 1.1 skrll */
154 1.12 riastrad struct workqueue_struct *
155 1.1 skrll alloc_ordered_workqueue(const char *name, int flags)
156 1.1 skrll {
157 1.1 skrll struct workqueue_struct *wq;
158 1.1 skrll int error;
159 1.12 riastrad
160 1.1 skrll KASSERT(flags == 0);
161 1.25 riastrad
162 1.1 skrll wq = kmem_zalloc(sizeof(*wq), KM_SLEEP);
163 1.12 riastrad
164 1.1 skrll mutex_init(&wq->wq_lock, MUTEX_DEFAULT, IPL_NONE);
165 1.1 skrll cv_init(&wq->wq_cv, name);
166 1.12 riastrad TAILQ_INIT(&wq->wq_delayed);
167 1.1 skrll TAILQ_INIT(&wq->wq_queue);
168 1.25 riastrad wq->wq_current_work = NULL;
169 1.25 riastrad wq->wq_flags = 0;
170 1.25 riastrad wq->wq_requeued = false;
171 1.37 riastrad wq->wq_dying = false;
172 1.37 riastrad wq->wq_gen = 0;
173 1.1 skrll wq->wq_lwp = NULL;
174 1.12 riastrad
175 1.12 riastrad error = kthread_create(PRI_NONE,
176 1.12 riastrad KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL,
177 1.12 riastrad &linux_workqueue_thread, wq, &wq->wq_lwp, "%s", name);
178 1.12 riastrad if (error)
179 1.3 riastrad goto fail0;
180 1.1 skrll
181 1.12 riastrad return wq;
182 1.12 riastrad
183 1.12 riastrad fail0: KASSERT(TAILQ_EMPTY(&wq->wq_queue));
184 1.12 riastrad KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
185 1.12 riastrad cv_destroy(&wq->wq_cv);
186 1.12 riastrad mutex_destroy(&wq->wq_lock);
187 1.12 riastrad kmem_free(wq, sizeof(*wq));
188 1.1 skrll return NULL;
189 1.1 skrll }
190 1.36 riastrad
191 1.36 riastrad /*
192 1.36 riastrad * destroy_workqueue(wq)
193 1.36 riastrad *
194 1.36 riastrad * Destroy a workqueue created with wq. Cancel any pending
195 1.36 riastrad * delayed work. Wait for all queued work to complete.
196 1.36 riastrad *
197 1.36 riastrad * May sleep.
198 1.1 skrll */
199 1.1 skrll void
200 1.1 skrll destroy_workqueue(struct workqueue_struct *wq)
201 1.1 skrll {
202 1.1 skrll
203 1.12 riastrad /*
204 1.12 riastrad * Cancel all delayed work. We do this first because any
205 1.12 riastrad * delayed work that that has already timed out, which we can't
206 1.1 skrll * cancel, may have queued new work.
207 1.26 riastrad */
208 1.26 riastrad mutex_enter(&wq->wq_lock);
209 1.26 riastrad while (!TAILQ_EMPTY(&wq->wq_delayed)) {
210 1.1 skrll struct delayed_work *const dw = TAILQ_FIRST(&wq->wq_delayed);
211 1.26 riastrad
212 1.26 riastrad KASSERT(dw->work.work_queue == wq);
213 1.26 riastrad KASSERTMSG((dw->dw_state == DELAYED_WORK_SCHEDULED ||
214 1.26 riastrad dw->dw_state == DELAYED_WORK_RESCHEDULED ||
215 1.26 riastrad dw->dw_state == DELAYED_WORK_CANCELLED),
216 1.26 riastrad "delayed work %p in bad state: %d",
217 1.26 riastrad dw, dw->dw_state);
218 1.26 riastrad
219 1.26 riastrad /*
220 1.26 riastrad * Mark it cancelled and try to stop the callout before
221 1.26 riastrad * it starts.
222 1.26 riastrad *
223 1.26 riastrad * If it's too late and the callout has already begun
224 1.26 riastrad * to execute, then it will notice that we asked to
225 1.26 riastrad * cancel it and remove itself from the queue before
226 1.26 riastrad * returning.
227 1.26 riastrad *
228 1.26 riastrad * If we stopped the callout before it started,
229 1.26 riastrad * however, then we can safely destroy the callout and
230 1.26 riastrad * dissociate it from the workqueue ourselves.
231 1.26 riastrad */
232 1.26 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
233 1.26 riastrad if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
234 1.26 riastrad cancel_delayed_work_done(wq, dw);
235 1.26 riastrad }
236 1.1 skrll mutex_exit(&wq->wq_lock);
237 1.26 riastrad
238 1.26 riastrad /*
239 1.26 riastrad * At this point, no new work can be put on the queue.
240 1.1 skrll */
241 1.12 riastrad
242 1.12 riastrad /* Tell the thread to exit. */
243 1.12 riastrad mutex_enter(&wq->wq_lock);
244 1.12 riastrad wq->wq_dying = true;
245 1.12 riastrad cv_broadcast(&wq->wq_cv);
246 1.12 riastrad mutex_exit(&wq->wq_lock);
247 1.12 riastrad
248 1.12 riastrad /* Wait for it to exit. */
249 1.12 riastrad (void)kthread_join(wq->wq_lwp);
250 1.25 riastrad
251 1.25 riastrad KASSERT(wq->wq_dying);
252 1.25 riastrad KASSERT(!wq->wq_requeued);
253 1.1 skrll KASSERT(wq->wq_flags == 0);
254 1.12 riastrad KASSERT(wq->wq_current_work == NULL);
255 1.12 riastrad KASSERT(TAILQ_EMPTY(&wq->wq_queue));
256 1.1 skrll KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
257 1.1 skrll cv_destroy(&wq->wq_cv);
258 1.1 skrll mutex_destroy(&wq->wq_lock);
259 1.1 skrll
260 1.1 skrll kmem_free(wq, sizeof(*wq));
261 1.1 skrll }
262 1.1 skrll
263 1.12 riastrad /*
265 1.1 skrll * Work thread and callout
266 1.36 riastrad */
267 1.36 riastrad
268 1.36 riastrad /*
269 1.36 riastrad * linux_workqueue_thread(cookie)
270 1.36 riastrad *
271 1.36 riastrad * Main function for a workqueue's worker thread. Waits until
272 1.36 riastrad * there is work queued, grabs a batch of work off the queue,
273 1.36 riastrad * executes it all, bumps the generation number, and repeats,
274 1.12 riastrad * until dying.
275 1.12 riastrad */
276 1.1 skrll static void __dead
277 1.12 riastrad linux_workqueue_thread(void *cookie)
278 1.12 riastrad {
279 1.1 skrll struct workqueue_struct *const wq = cookie;
280 1.12 riastrad TAILQ_HEAD(, work_struct) tmp;
281 1.1 skrll
282 1.12 riastrad lwp_setspecific(workqueue_key, wq);
283 1.12 riastrad
284 1.26 riastrad mutex_enter(&wq->wq_lock);
285 1.26 riastrad for (;;) {
286 1.26 riastrad /*
287 1.26 riastrad * Wait until there's activity. If there's no work and
288 1.12 riastrad * we're dying, stop here.
289 1.12 riastrad */
290 1.26 riastrad while (TAILQ_EMPTY(&wq->wq_queue) && !wq->wq_dying)
291 1.26 riastrad cv_wait(&wq->wq_cv, &wq->wq_lock);
292 1.12 riastrad if (TAILQ_EMPTY(&wq->wq_queue)) {
293 1.26 riastrad KASSERT(wq->wq_dying);
294 1.1 skrll break;
295 1.12 riastrad }
296 1.12 riastrad
297 1.12 riastrad /* Grab a batch of work off the queue. */
298 1.12 riastrad KASSERT(!TAILQ_EMPTY(&wq->wq_queue));
299 1.12 riastrad TAILQ_INIT(&tmp);
300 1.12 riastrad TAILQ_CONCAT(&tmp, &wq->wq_queue, work_entry);
301 1.12 riastrad
302 1.12 riastrad /* Process each work item in the batch. */
303 1.12 riastrad while (!TAILQ_EMPTY(&tmp)) {
304 1.18 riastrad struct work_struct *const work = TAILQ_FIRST(&tmp);
305 1.12 riastrad
306 1.12 riastrad KASSERT(work->work_queue == wq);
307 1.12 riastrad TAILQ_REMOVE(&tmp, work, work_entry);
308 1.1 skrll KASSERT(wq->wq_current_work == NULL);
309 1.12 riastrad wq->wq_current_work = work;
310 1.12 riastrad
311 1.12 riastrad mutex_exit(&wq->wq_lock);
312 1.1 skrll (*work->func)(work);
313 1.12 riastrad mutex_enter(&wq->wq_lock);
314 1.12 riastrad
315 1.12 riastrad KASSERT(wq->wq_current_work == work);
316 1.12 riastrad KASSERT(work->work_queue == wq);
317 1.12 riastrad if (wq->wq_requeued)
318 1.17 riastrad wq->wq_requeued = false;
319 1.12 riastrad else
320 1.12 riastrad release_work(work, wq);
321 1.12 riastrad wq->wq_current_work = NULL;
322 1.1 skrll cv_broadcast(&wq->wq_cv);
323 1.12 riastrad }
324 1.12 riastrad
325 1.12 riastrad /* Notify flush that we've completed a batch of work. */
326 1.1 skrll wq->wq_gen++;
327 1.12 riastrad cv_broadcast(&wq->wq_cv);
328 1.1 skrll }
329 1.12 riastrad mutex_exit(&wq->wq_lock);
330 1.1 skrll
331 1.1 skrll kthread_exit(0);
332 1.36 riastrad }
333 1.36 riastrad
334 1.36 riastrad /*
335 1.36 riastrad * linux_workqueue_timeout(cookie)
336 1.36 riastrad *
337 1.36 riastrad * Delayed work timeout callback.
338 1.36 riastrad *
339 1.36 riastrad * - If scheduled, queue it.
340 1.36 riastrad * - If rescheduled, callout_schedule ourselves again.
341 1.36 riastrad * - If cancelled, destroy the callout and release the work from
342 1.1 skrll * the workqueue.
343 1.12 riastrad */
344 1.1 skrll static void
345 1.12 riastrad linux_workqueue_timeout(void *cookie)
346 1.12 riastrad {
347 1.1 skrll struct delayed_work *const dw = cookie;
348 1.12 riastrad struct workqueue_struct *const wq = dw->work.work_queue;
349 1.14 riastrad
350 1.12 riastrad KASSERT(wq != NULL);
351 1.18 riastrad
352 1.12 riastrad mutex_enter(&wq->wq_lock);
353 1.12 riastrad KASSERT(dw->work.work_queue == wq);
354 1.12 riastrad switch (dw->dw_state) {
355 1.12 riastrad case DELAYED_WORK_IDLE:
356 1.31 riastrad panic("delayed work callout uninitialized: %p", dw);
357 1.12 riastrad case DELAYED_WORK_SCHEDULED:
358 1.12 riastrad dw_callout_destroy(wq, dw);
359 1.12 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, work_entry);
360 1.12 riastrad cv_broadcast(&wq->wq_cv);
361 1.35 riastrad break;
362 1.35 riastrad case DELAYED_WORK_RESCHEDULED:
363 1.12 riastrad KASSERT(dw->dw_resched >= 0);
364 1.35 riastrad callout_schedule(&dw->dw_callout, dw->dw_resched);
365 1.12 riastrad dw->dw_state = DELAYED_WORK_SCHEDULED;
366 1.12 riastrad dw->dw_resched = -1;
367 1.23 riastrad break;
368 1.22 riastrad case DELAYED_WORK_CANCELLED:
369 1.22 riastrad cancel_delayed_work_done(wq, dw);
370 1.12 riastrad /* Can't touch dw any more. */
371 1.12 riastrad goto out;
372 1.12 riastrad default:
373 1.15 riastrad panic("delayed work callout in bad state: %p", dw);
374 1.15 riastrad }
375 1.22 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE ||
376 1.1 skrll dw->dw_state == DELAYED_WORK_SCHEDULED);
377 1.1 skrll out: mutex_exit(&wq->wq_lock);
378 1.36 riastrad }
379 1.36 riastrad
380 1.36 riastrad /*
381 1.36 riastrad * current_work()
382 1.36 riastrad *
383 1.36 riastrad * If in a workqueue worker thread, return the work it is
384 1.12 riastrad * currently executing. Otherwise return NULL.
385 1.12 riastrad */
386 1.1 skrll struct work_struct *
387 1.12 riastrad current_work(void)
388 1.1 skrll {
389 1.12 riastrad struct workqueue_struct *wq = lwp_getspecific(workqueue_key);
390 1.12 riastrad
391 1.12 riastrad /* If we're not a workqueue thread, then there's no work. */
392 1.1 skrll if (wq == NULL)
393 1.12 riastrad return NULL;
394 1.12 riastrad
395 1.12 riastrad /*
396 1.12 riastrad * Otherwise, this should be possible only while work is in
397 1.12 riastrad * progress. Return the current work item.
398 1.12 riastrad */
399 1.1 skrll KASSERT(wq->wq_current_work != NULL);
400 1.1 skrll return wq->wq_current_work;
401 1.1 skrll }
402 1.1 skrll
403 1.1 skrll /*
405 1.36 riastrad * Work
406 1.36 riastrad */
407 1.36 riastrad
408 1.36 riastrad /*
409 1.36 riastrad * INIT_WORK(work, fn)
410 1.36 riastrad *
411 1.1 skrll * Initialize work for use with a workqueue to call fn in a worker
412 1.1 skrll * thread. There is no corresponding destruction operation.
413 1.1 skrll */
414 1.1 skrll void
415 1.12 riastrad INIT_WORK(struct work_struct *work, void (*fn)(struct work_struct *))
416 1.4 riastrad {
417 1.1 skrll
418 1.1 skrll work->work_queue = NULL;
419 1.36 riastrad work->func = fn;
420 1.36 riastrad }
421 1.36 riastrad
422 1.36 riastrad /*
423 1.36 riastrad * acquire_work(work, wq)
424 1.36 riastrad *
425 1.36 riastrad * Try to associate work with wq. If work is already on a
426 1.36 riastrad * workqueue, return that workqueue. Otherwise, set work's queue
427 1.36 riastrad * to wq, issue a memory barrier to match any prior release_work,
428 1.36 riastrad * and return NULL.
429 1.17 riastrad *
430 1.17 riastrad * Caller must hold wq's lock.
431 1.17 riastrad */
432 1.17 riastrad static struct workqueue_struct *
433 1.17 riastrad acquire_work(struct work_struct *work, struct workqueue_struct *wq)
434 1.17 riastrad {
435 1.17 riastrad struct workqueue_struct *wq0;
436 1.17 riastrad
437 1.17 riastrad KASSERT(mutex_owned(&wq->wq_lock));
438 1.17 riastrad
439 1.17 riastrad wq0 = atomic_cas_ptr(&work->work_queue, NULL, wq);
440 1.17 riastrad if (wq0 == NULL) {
441 1.17 riastrad membar_enter();
442 1.17 riastrad KASSERT(work->work_queue == wq);
443 1.17 riastrad }
444 1.17 riastrad
445 1.36 riastrad return wq0;
446 1.36 riastrad }
447 1.36 riastrad
448 1.36 riastrad /*
449 1.36 riastrad * release_work(work, wq)
450 1.36 riastrad *
451 1.36 riastrad * Issue a memory barrier to match any subsequent acquire_work and
452 1.36 riastrad * dissociate work from wq.
453 1.17 riastrad *
454 1.17 riastrad * Caller must hold wq's lock and work must be associated with wq.
455 1.17 riastrad */
456 1.17 riastrad static void
457 1.17 riastrad release_work(struct work_struct *work, struct workqueue_struct *wq)
458 1.17 riastrad {
459 1.17 riastrad
460 1.17 riastrad KASSERT(work->work_queue == wq);
461 1.17 riastrad KASSERT(mutex_owned(&wq->wq_lock));
462 1.17 riastrad
463 1.17 riastrad membar_exit();
464 1.36 riastrad work->work_queue = NULL;
465 1.36 riastrad }
466 1.36 riastrad
467 1.36 riastrad /*
468 1.36 riastrad * schedule_work(work)
469 1.36 riastrad *
470 1.36 riastrad * If work is not already queued on system_wq, queue it to be run
471 1.36 riastrad * by system_wq's worker thread when it next can. True if it was
472 1.36 riastrad * newly queued, false if it was already queued. If the work was
473 1.36 riastrad * already running, queue it to run again.
474 1.36 riastrad *
475 1.1 skrll * Caller must ensure work is not queued to run on a different
476 1.1 skrll * workqueue.
477 1.1 skrll */
478 1.12 riastrad bool
479 1.1 skrll schedule_work(struct work_struct *work)
480 1.1 skrll {
481 1.1 skrll
482 1.36 riastrad return queue_work(system_wq, work);
483 1.36 riastrad }
484 1.36 riastrad
485 1.36 riastrad /*
486 1.36 riastrad * queue_work(wq, work)
487 1.36 riastrad *
488 1.36 riastrad * If work is not already queued on wq, queue it to be run by wq's
489 1.36 riastrad * worker thread when it next can. True if it was newly queued,
490 1.36 riastrad * false if it was already queued. If the work was already
491 1.36 riastrad * running, queue it to run again.
492 1.36 riastrad *
493 1.1 skrll * Caller must ensure work is not queued to run on a different
494 1.1 skrll * workqueue.
495 1.1 skrll */
496 1.12 riastrad bool
497 1.1 skrll queue_work(struct workqueue_struct *wq, struct work_struct *work)
498 1.1 skrll {
499 1.1 skrll struct workqueue_struct *wq0;
500 1.1 skrll bool newly_queued;
501 1.12 riastrad
502 1.17 riastrad KASSERT(wq != NULL);
503 1.29 riastrad
504 1.29 riastrad mutex_enter(&wq->wq_lock);
505 1.29 riastrad if (__predict_true((wq0 = acquire_work(work, wq)) == NULL)) {
506 1.29 riastrad /*
507 1.29 riastrad * It wasn't on any workqueue at all. Put it on this
508 1.12 riastrad * one, and signal the worker thread that there is work
509 1.1 skrll * to do.
510 1.29 riastrad */
511 1.12 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry);
512 1.29 riastrad newly_queued = true;
513 1.29 riastrad cv_broadcast(&wq->wq_cv);
514 1.29 riastrad } else {
515 1.29 riastrad /*
516 1.29 riastrad * It was on a workqueue, which had better be this one.
517 1.29 riastrad * Requeue it if it has been taken off the queue to
518 1.29 riastrad * execute and hasn't been requeued yet. The worker
519 1.12 riastrad * thread should already be running, so no need to
520 1.29 riastrad * signal it.
521 1.29 riastrad */
522 1.29 riastrad KASSERT(wq0 == wq);
523 1.29 riastrad if (wq->wq_current_work == work && !wq->wq_requeued) {
524 1.29 riastrad /*
525 1.29 riastrad * It has been taken off the queue to execute,
526 1.29 riastrad * and it hasn't been put back on the queue
527 1.29 riastrad * again. Put it back on the queue. No need
528 1.29 riastrad * to signal the worker thread because it will
529 1.29 riastrad * notice when it reacquires the lock after
530 1.29 riastrad * doing the work.
531 1.29 riastrad */
532 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry);
533 1.29 riastrad wq->wq_requeued = true;
534 1.29 riastrad newly_queued = true;
535 1.29 riastrad } else {
536 1.1 skrll /* It is still on the queue; nothing to do. */
537 1.12 riastrad newly_queued = false;
538 1.1 skrll }
539 1.1 skrll }
540 1.1 skrll mutex_exit(&wq->wq_lock);
541 1.1 skrll
542 1.36 riastrad return newly_queued;
543 1.36 riastrad }
544 1.36 riastrad
545 1.36 riastrad /*
546 1.36 riastrad * cancel_work(work)
547 1.36 riastrad *
548 1.36 riastrad * If work was queued, remove it from the queue and return true.
549 1.36 riastrad * If work was not queued, return false. Note that work may
550 1.36 riastrad * already be running; if it hasn't been requeued, then
551 1.1 skrll * cancel_work will return false, and either way, cancel_work will
552 1.12 riastrad * NOT wait for the work to complete.
553 1.1 skrll */
554 1.12 riastrad bool
555 1.1 skrll cancel_work(struct work_struct *work)
556 1.1 skrll {
557 1.13 riastrad struct workqueue_struct *wq;
558 1.13 riastrad bool cancelled_p = false;
559 1.13 riastrad
560 1.13 riastrad /* If there's no workqueue, nothing to cancel. */
561 1.12 riastrad if ((wq = work->work_queue) == NULL)
562 1.12 riastrad goto out;
563 1.29 riastrad
564 1.29 riastrad mutex_enter(&wq->wq_lock);
565 1.29 riastrad if (__predict_false(work->work_queue != wq)) {
566 1.29 riastrad /*
567 1.29 riastrad * It has finished execution or been cancelled by
568 1.12 riastrad * another thread, and has been moved off the
569 1.12 riastrad * workqueue, so it's too to cancel.
570 1.29 riastrad */
571 1.29 riastrad cancelled_p = false;
572 1.29 riastrad } else if (wq->wq_current_work == work) {
573 1.29 riastrad /*
574 1.12 riastrad * It has already begun execution, so it's too late to
575 1.12 riastrad * cancel now.
576 1.29 riastrad */
577 1.29 riastrad cancelled_p = false;
578 1.29 riastrad } else {
579 1.29 riastrad /*
580 1.12 riastrad * It is still on the queue. Take it off the queue and
581 1.1 skrll * report successful cancellation.
582 1.1 skrll */
583 1.12 riastrad TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
584 1.1 skrll cancelled_p = true;
585 1.13 riastrad }
586 1.1 skrll mutex_exit(&wq->wq_lock);
587 1.1 skrll
588 1.36 riastrad out: return cancelled_p;
589 1.36 riastrad }
590 1.36 riastrad
591 1.36 riastrad /*
592 1.36 riastrad * cancel_work_sync(work)
593 1.36 riastrad *
594 1.36 riastrad * If work was queued, remove it from the queue and return true.
595 1.36 riastrad * If work was not queued, return false. Note that work may
596 1.36 riastrad * already be running; if it hasn't been requeued, then
597 1.36 riastrad * cancel_work will return false; either way, if work was
598 1.36 riastrad * currently running, wait for it to complete.
599 1.12 riastrad *
600 1.12 riastrad * May sleep.
601 1.1 skrll */
602 1.1 skrll bool
603 1.12 riastrad cancel_work_sync(struct work_struct *work)
604 1.1 skrll {
605 1.13 riastrad struct workqueue_struct *wq;
606 1.13 riastrad bool cancelled_p = false;
607 1.13 riastrad
608 1.13 riastrad /* If there's no workqueue, nothing to cancel. */
609 1.1 skrll if ((wq = work->work_queue) == NULL)
610 1.12 riastrad goto out;
611 1.29 riastrad
612 1.29 riastrad mutex_enter(&wq->wq_lock);
613 1.29 riastrad if (__predict_false(work->work_queue != wq)) {
614 1.29 riastrad /*
615 1.29 riastrad * It has finished execution or been cancelled by
616 1.12 riastrad * another thread, and has been moved off the
617 1.12 riastrad * workqueue, so it's too to cancel.
618 1.29 riastrad */
619 1.29 riastrad cancelled_p = false;
620 1.33 riastrad } else if (wq->wq_current_work == work) {
621 1.29 riastrad /*
622 1.33 riastrad * It has already begun execution, so it's too late to
623 1.12 riastrad * cancel now. Wait for it to complete.
624 1.12 riastrad */
625 1.29 riastrad wait_for_current_work(work, wq);
626 1.29 riastrad cancelled_p = false;
627 1.29 riastrad } else {
628 1.29 riastrad /*
629 1.12 riastrad * It is still on the queue. Take it off the queue and
630 1.12 riastrad * report successful cancellation.
631 1.12 riastrad */
632 1.1 skrll TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
633 1.1 skrll cancelled_p = true;
634 1.13 riastrad }
635 1.1 skrll mutex_exit(&wq->wq_lock);
636 1.33 riastrad
637 1.33 riastrad out: return cancelled_p;
638 1.33 riastrad }
639 1.33 riastrad
640 1.33 riastrad /*
641 1.33 riastrad * wait_for_current_work(work, wq)
642 1.33 riastrad *
643 1.33 riastrad * wq must be currently executing work. Wait for it to finish.
644 1.33 riastrad */
645 1.33 riastrad static void
646 1.33 riastrad wait_for_current_work(struct work_struct *work, struct workqueue_struct *wq)
647 1.33 riastrad {
648 1.33 riastrad uint64_t gen;
649 1.33 riastrad
650 1.33 riastrad KASSERT(mutex_owned(&wq->wq_lock));
651 1.33 riastrad KASSERT(work->work_queue == wq);
652 1.33 riastrad KASSERT(wq->wq_current_work == work);
653 1.33 riastrad
654 1.33 riastrad /* Wait only one generation in case it gets requeued quickly. */
655 1.33 riastrad gen = wq->wq_gen;
656 1.33 riastrad do {
657 1.1 skrll cv_wait(&wq->wq_cv, &wq->wq_lock);
658 1.1 skrll } while (wq->wq_current_work == work && wq->wq_gen == gen);
659 1.1 skrll }
660 1.1 skrll
661 1.1 skrll /*
663 1.36 riastrad * Delayed work
664 1.36 riastrad */
665 1.36 riastrad
666 1.36 riastrad /*
667 1.36 riastrad * INIT_DELAYED_WORK(dw, fn)
668 1.36 riastrad *
669 1.1 skrll * Initialize dw for use with a workqueue to call fn in a worker
670 1.1 skrll * thread after a delay. There is no corresponding destruction
671 1.1 skrll * operation.
672 1.12 riastrad */
673 1.1 skrll void
674 1.12 riastrad INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work_struct *))
675 1.35 riastrad {
676 1.12 riastrad
677 1.12 riastrad INIT_WORK(&dw->work, fn);
678 1.12 riastrad dw->dw_state = DELAYED_WORK_IDLE;
679 1.12 riastrad dw->dw_resched = -1;
680 1.12 riastrad
681 1.12 riastrad /*
682 1.12 riastrad * Defer callout_init until we are going to schedule the
683 1.1 skrll * callout, which can then callout_destroy it, because
684 1.1 skrll * otherwise since there's no DESTROY_DELAYED_WORK or anything
685 1.36 riastrad * we have no opportunity to call callout_destroy.
686 1.36 riastrad */
687 1.36 riastrad }
688 1.36 riastrad
689 1.36 riastrad /*
690 1.36 riastrad * schedule_delayed_work(dw, ticks)
691 1.36 riastrad *
692 1.36 riastrad * If it is not currently scheduled, schedule dw to run after
693 1.36 riastrad * ticks on system_wq. If currently executing and not already
694 1.36 riastrad * rescheduled, reschedule it. True if it was newly scheduled,
695 1.36 riastrad * false if it was already scheduled.
696 1.1 skrll *
697 1.1 skrll * If ticks == 0, queue it to run as soon as the worker can,
698 1.1 skrll * without waiting for the next callout tick to run.
699 1.12 riastrad */
700 1.1 skrll bool
701 1.1 skrll schedule_delayed_work(struct delayed_work *dw, unsigned long ticks)
702 1.1 skrll {
703 1.29 riastrad
704 1.30 riastrad return queue_delayed_work(system_wq, dw, ticks);
705 1.30 riastrad }
706 1.30 riastrad
707 1.30 riastrad /*
708 1.30 riastrad * dw_callout_init(wq, dw)
709 1.30 riastrad *
710 1.30 riastrad * Initialize the callout of dw and transition to
711 1.30 riastrad * DELAYED_WORK_SCHEDULED. Caller must use callout_schedule.
712 1.30 riastrad */
713 1.30 riastrad static void
714 1.30 riastrad dw_callout_init(struct workqueue_struct *wq, struct delayed_work *dw)
715 1.30 riastrad {
716 1.30 riastrad
717 1.30 riastrad KASSERT(mutex_owned(&wq->wq_lock));
718 1.30 riastrad KASSERT(dw->work.work_queue == wq);
719 1.30 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
720 1.30 riastrad
721 1.30 riastrad callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
722 1.30 riastrad callout_setfunc(&dw->dw_callout, &linux_workqueue_timeout, dw);
723 1.30 riastrad TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
724 1.31 riastrad dw->dw_state = DELAYED_WORK_SCHEDULED;
725 1.31 riastrad }
726 1.31 riastrad
727 1.31 riastrad /*
728 1.31 riastrad * dw_callout_destroy(wq, dw)
729 1.31 riastrad *
730 1.31 riastrad * Destroy the callout of dw and transition to DELAYED_WORK_IDLE.
731 1.31 riastrad */
732 1.31 riastrad static void
733 1.31 riastrad dw_callout_destroy(struct workqueue_struct *wq, struct delayed_work *dw)
734 1.31 riastrad {
735 1.31 riastrad
736 1.31 riastrad KASSERT(mutex_owned(&wq->wq_lock));
737 1.31 riastrad KASSERT(dw->work.work_queue == wq);
738 1.31 riastrad KASSERT(dw->dw_state == DELAYED_WORK_SCHEDULED ||
739 1.31 riastrad dw->dw_state == DELAYED_WORK_RESCHEDULED ||
740 1.35 riastrad dw->dw_state == DELAYED_WORK_CANCELLED);
741 1.31 riastrad
742 1.31 riastrad TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
743 1.31 riastrad callout_destroy(&dw->dw_callout);
744 1.31 riastrad dw->dw_resched = -1;
745 1.29 riastrad dw->dw_state = DELAYED_WORK_IDLE;
746 1.29 riastrad }
747 1.29 riastrad
748 1.29 riastrad /*
749 1.29 riastrad * cancel_delayed_work_done(wq, dw)
750 1.29 riastrad *
751 1.23 riastrad * Complete cancellation of a delayed work: transition from
752 1.23 riastrad * DELAYED_WORK_CANCELLED to DELAYED_WORK_IDLE and off the
753 1.23 riastrad * workqueue. Caller must not touch dw after this returns.
754 1.23 riastrad */
755 1.23 riastrad static void
756 1.23 riastrad cancel_delayed_work_done(struct workqueue_struct *wq, struct delayed_work *dw)
757 1.23 riastrad {
758 1.31 riastrad
759 1.31 riastrad KASSERT(mutex_owned(&wq->wq_lock));
760 1.23 riastrad KASSERT(dw->work.work_queue == wq);
761 1.23 riastrad KASSERT(dw->dw_state == DELAYED_WORK_CANCELLED);
762 1.23 riastrad
763 1.23 riastrad dw_callout_destroy(wq, dw);
764 1.29 riastrad release_work(&dw->work, wq);
765 1.29 riastrad /* Can't touch dw after this point. */
766 1.29 riastrad }
767 1.29 riastrad
768 1.36 riastrad /*
769 1.36 riastrad * queue_delayed_work(wq, dw, ticks)
770 1.36 riastrad *
771 1.36 riastrad * If it is not currently scheduled, schedule dw to run after
772 1.36 riastrad * ticks on wq. If currently executing and not already
773 1.29 riastrad * rescheduled, reschedule it.
774 1.12 riastrad *
775 1.12 riastrad * If ticks == 0, queue it to run as soon as the worker can,
776 1.12 riastrad * without waiting for the next callout tick to run.
777 1.12 riastrad */
778 1.12 riastrad bool
779 1.12 riastrad queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
780 1.1 skrll unsigned long ticks)
781 1.12 riastrad {
782 1.17 riastrad struct workqueue_struct *wq0;
783 1.29 riastrad bool newly_queued;
784 1.29 riastrad
785 1.29 riastrad mutex_enter(&wq->wq_lock);
786 1.29 riastrad if (__predict_true((wq0 = acquire_work(&dw->work, wq)) == NULL)) {
787 1.12 riastrad /*
788 1.29 riastrad * It wasn't on any workqueue at all. Schedule it to
789 1.29 riastrad * run on this one.
790 1.29 riastrad */
791 1.29 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
792 1.29 riastrad if (ticks == 0) {
793 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work,
794 1.29 riastrad work_entry);
795 1.29 riastrad cv_broadcast(&wq->wq_cv);
796 1.29 riastrad } else {
797 1.30 riastrad /*
798 1.29 riastrad * Initialize a callout and schedule to run
799 1.29 riastrad * after a delay.
800 1.12 riastrad */
801 1.12 riastrad dw_callout_init(wq, dw);
802 1.29 riastrad callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
803 1.29 riastrad }
804 1.29 riastrad newly_queued = true;
805 1.29 riastrad } else {
806 1.29 riastrad /*
807 1.29 riastrad * It was on a workqueue, which had better be this one.
808 1.29 riastrad *
809 1.29 riastrad * - If it has already begun to run, and it is not yet
810 1.29 riastrad * scheduled to run again, schedule it again.
811 1.29 riastrad *
812 1.12 riastrad * - If the callout is cancelled, reschedule it.
813 1.29 riastrad *
814 1.29 riastrad * - Otherwise, leave it alone.
815 1.29 riastrad */
816 1.29 riastrad KASSERT(wq0 == wq);
817 1.29 riastrad if (wq->wq_current_work != &dw->work || !wq->wq_requeued) {
818 1.29 riastrad /*
819 1.29 riastrad * It is either scheduled, on the queue but not
820 1.29 riastrad * in progress, or in progress but not on the
821 1.29 riastrad * queue.
822 1.29 riastrad */
823 1.29 riastrad switch (dw->dw_state) {
824 1.29 riastrad case DELAYED_WORK_IDLE:
825 1.29 riastrad /*
826 1.29 riastrad * It is not scheduled to run, and it
827 1.29 riastrad * is not on the queue if it is
828 1.29 riastrad * running.
829 1.29 riastrad */
830 1.29 riastrad if (ticks == 0) {
831 1.29 riastrad /*
832 1.29 riastrad * If it's in progress, put it
833 1.29 riastrad * on the queue to run as soon
834 1.29 riastrad * as the worker thread gets to
835 1.29 riastrad * it. No need for a wakeup
836 1.29 riastrad * because either the worker
837 1.29 riastrad * thread already knows it is
838 1.29 riastrad * on the queue, or will check
839 1.29 riastrad * once it is done executing.
840 1.29 riastrad */
841 1.29 riastrad if (wq->wq_current_work == &dw->work) {
842 1.29 riastrad KASSERT(!wq->wq_requeued);
843 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue,
844 1.29 riastrad &dw->work, work_entry);
845 1.29 riastrad wq->wq_requeued = true;
846 1.29 riastrad }
847 1.29 riastrad } else {
848 1.29 riastrad /*
849 1.30 riastrad * Initialize a callout and
850 1.30 riastrad * schedule it to run after the
851 1.30 riastrad * specified delay.
852 1.29 riastrad */
853 1.29 riastrad dw_callout_init(wq, dw);
854 1.29 riastrad callout_schedule(&dw->dw_callout,
855 1.29 riastrad MIN(INT_MAX, ticks));
856 1.29 riastrad }
857 1.29 riastrad break;
858 1.29 riastrad case DELAYED_WORK_SCHEDULED:
859 1.29 riastrad case DELAYED_WORK_RESCHEDULED:
860 1.29 riastrad /*
861 1.29 riastrad * It is already scheduled to run after
862 1.29 riastrad * a delay. Leave it be.
863 1.29 riastrad */
864 1.29 riastrad break;
865 1.29 riastrad case DELAYED_WORK_CANCELLED:
866 1.29 riastrad /*
867 1.29 riastrad * It was scheduled and the callout has
868 1.35 riastrad * begun to execute, but it was
869 1.29 riastrad * cancelled. Reschedule it.
870 1.29 riastrad */
871 1.29 riastrad dw->dw_state = DELAYED_WORK_RESCHEDULED;
872 1.29 riastrad dw->dw_resched = MIN(INT_MAX, ticks);
873 1.29 riastrad break;
874 1.29 riastrad default:
875 1.29 riastrad panic("invalid delayed work state: %d",
876 1.29 riastrad dw->dw_state);
877 1.29 riastrad }
878 1.29 riastrad } else {
879 1.29 riastrad /*
880 1.29 riastrad * It is in progress and it has been requeued.
881 1.29 riastrad * It cannot be scheduled to run after a delay
882 1.29 riastrad * at this point. We just leave it be.
883 1.29 riastrad */
884 1.1 skrll KASSERTMSG((dw->dw_state == DELAYED_WORK_IDLE),
885 1.12 riastrad "delayed work %p in wrong state: %d",
886 1.1 skrll dw, dw->dw_state);
887 1.1 skrll }
888 1.1 skrll }
889 1.1 skrll mutex_exit(&wq->wq_lock);
890 1.29 riastrad
891 1.29 riastrad return newly_queued;
892 1.29 riastrad }
893 1.29 riastrad
894 1.29 riastrad /*
895 1.29 riastrad * mod_delayed_work(wq, dw, ticks)
896 1.36 riastrad *
897 1.36 riastrad * Schedule dw to run after ticks. If currently scheduled,
898 1.36 riastrad * reschedule it. If currently executing, reschedule it. If
899 1.29 riastrad * ticks == 0, run without delay.
900 1.1 skrll *
901 1.1 skrll * True if it modified the timer of an already scheduled work,
902 1.1 skrll * false if it newly scheduled the work.
903 1.1 skrll */
904 1.12 riastrad bool
905 1.1 skrll mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
906 1.1 skrll unsigned long ticks)
907 1.12 riastrad {
908 1.17 riastrad struct workqueue_struct *wq0;
909 1.29 riastrad bool timer_modified;
910 1.29 riastrad
911 1.29 riastrad mutex_enter(&wq->wq_lock);
912 1.29 riastrad if ((wq0 = acquire_work(&dw->work, wq)) == NULL) {
913 1.12 riastrad /*
914 1.29 riastrad * It wasn't on any workqueue at all. Schedule it to
915 1.29 riastrad * run on this one.
916 1.29 riastrad */
917 1.29 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
918 1.29 riastrad if (ticks == 0) {
919 1.29 riastrad /*
920 1.29 riastrad * Run immediately: put it on the queue and
921 1.29 riastrad * signal the worker thread.
922 1.29 riastrad */
923 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work,
924 1.29 riastrad work_entry);
925 1.29 riastrad cv_broadcast(&wq->wq_cv);
926 1.29 riastrad } else {
927 1.30 riastrad /*
928 1.30 riastrad * Initialize a callout and schedule to run
929 1.29 riastrad * after a delay.
930 1.12 riastrad */
931 1.12 riastrad dw_callout_init(wq, dw);
932 1.29 riastrad callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
933 1.12 riastrad }
934 1.12 riastrad timer_modified = false;
935 1.12 riastrad } else {
936 1.29 riastrad /* It was on a workqueue, which had better be this one. */
937 1.29 riastrad KASSERT(wq0 == wq);
938 1.29 riastrad switch (dw->dw_state) {
939 1.29 riastrad case DELAYED_WORK_IDLE:
940 1.32 riastrad /*
941 1.32 riastrad * It is not scheduled: it is on the queue or
942 1.29 riastrad * it is running or both.
943 1.32 riastrad */
944 1.32 riastrad if (wq->wq_current_work != &dw->work ||
945 1.29 riastrad wq->wq_requeued) {
946 1.29 riastrad /*
947 1.29 riastrad * It is on the queue, and it may or
948 1.29 riastrad * may not be running.
949 1.29 riastrad */
950 1.29 riastrad if (ticks == 0) {
951 1.29 riastrad /*
952 1.29 riastrad * We ask it to run
953 1.29 riastrad * immediately. Leave it on
954 1.29 riastrad * the queue.
955 1.29 riastrad */
956 1.29 riastrad } else {
957 1.29 riastrad /*
958 1.32 riastrad * Take it off the queue and
959 1.32 riastrad * schedule a callout to run it
960 1.32 riastrad * after a delay.
961 1.32 riastrad */
962 1.32 riastrad if (wq->wq_requeued) {
963 1.32 riastrad wq->wq_requeued = false;
964 1.29 riastrad } else {
965 1.29 riastrad KASSERT(wq->wq_current_work !=
966 1.30 riastrad &dw->work);
967 1.30 riastrad }
968 1.30 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
969 1.29 riastrad work_entry);
970 1.29 riastrad dw_callout_init(wq, dw);
971 1.29 riastrad callout_schedule(&dw->dw_callout,
972 1.12 riastrad MIN(INT_MAX, ticks));
973 1.29 riastrad }
974 1.29 riastrad timer_modified = true;
975 1.12 riastrad } else {
976 1.29 riastrad /*
977 1.29 riastrad * It is currently running and has not
978 1.29 riastrad * been requeued.
979 1.29 riastrad */
980 1.29 riastrad if (ticks == 0) {
981 1.29 riastrad /*
982 1.29 riastrad * We ask it to run
983 1.29 riastrad * immediately. Put it on the
984 1.29 riastrad * queue again.
985 1.29 riastrad */
986 1.29 riastrad wq->wq_requeued = true;
987 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue,
988 1.29 riastrad &dw->work, work_entry);
989 1.29 riastrad } else {
990 1.30 riastrad /*
991 1.30 riastrad * Schedule a callout to run it
992 1.30 riastrad * after a delay.
993 1.29 riastrad */
994 1.12 riastrad dw_callout_init(wq, dw);
995 1.12 riastrad callout_schedule(&dw->dw_callout,
996 1.12 riastrad MIN(INT_MAX, ticks));
997 1.12 riastrad }
998 1.29 riastrad timer_modified = false;
999 1.29 riastrad }
1000 1.29 riastrad break;
1001 1.29 riastrad case DELAYED_WORK_SCHEDULED:
1002 1.29 riastrad /*
1003 1.29 riastrad * It is scheduled to run after a delay. Try
1004 1.12 riastrad * to stop it and reschedule it; if we can't,
1005 1.29 riastrad * either reschedule it or cancel it to put it
1006 1.29 riastrad * on the queue, and inform the callout.
1007 1.29 riastrad */
1008 1.29 riastrad if (callout_stop(&dw->dw_callout)) {
1009 1.29 riastrad /* Can't stop, callout has begun. */
1010 1.29 riastrad if (ticks == 0) {
1011 1.29 riastrad /*
1012 1.29 riastrad * We don't actually need to do
1013 1.29 riastrad * anything. The callout will
1014 1.35 riastrad * queue it as soon as it gets
1015 1.29 riastrad * the lock.
1016 1.35 riastrad */
1017 1.29 riastrad } else {
1018 1.12 riastrad /* Ask the callout to reschedule. */
1019 1.35 riastrad dw->dw_state = DELAYED_WORK_RESCHEDULED;
1020 1.29 riastrad dw->dw_resched = MIN(INT_MAX, ticks);
1021 1.29 riastrad }
1022 1.29 riastrad } else {
1023 1.29 riastrad /* We stopped the callout before it began. */
1024 1.29 riastrad if (ticks == 0) {
1025 1.29 riastrad /*
1026 1.29 riastrad * Run immediately: destroy the
1027 1.31 riastrad * callout, put it on the
1028 1.29 riastrad * queue, and signal the worker
1029 1.29 riastrad * thread.
1030 1.29 riastrad */
1031 1.29 riastrad dw_callout_destroy(wq, dw);
1032 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue,
1033 1.29 riastrad &dw->work, work_entry);
1034 1.29 riastrad cv_broadcast(&wq->wq_cv);
1035 1.29 riastrad } else {
1036 1.29 riastrad /*
1037 1.29 riastrad * Reschedule the callout. No
1038 1.29 riastrad * state change.
1039 1.12 riastrad */
1040 1.12 riastrad callout_schedule(&dw->dw_callout,
1041 1.12 riastrad MIN(INT_MAX, ticks));
1042 1.12 riastrad }
1043 1.35 riastrad }
1044 1.35 riastrad timer_modified = true;
1045 1.35 riastrad break;
1046 1.35 riastrad case DELAYED_WORK_RESCHEDULED:
1047 1.35 riastrad /*
1048 1.35 riastrad * Someone rescheduled it after the callout
1049 1.35 riastrad * started but before the poor thing even had a
1050 1.35 riastrad * chance to acquire the lock.
1051 1.35 riastrad */
1052 1.35 riastrad if (ticks == 0) {
1053 1.35 riastrad /*
1054 1.35 riastrad * We can just switch back to
1055 1.35 riastrad * DELAYED_WORK_SCHEDULED so that the
1056 1.35 riastrad * callout will queue the work as soon
1057 1.35 riastrad * as it gets the lock.
1058 1.35 riastrad */
1059 1.35 riastrad dw->dw_state = DELAYED_WORK_SCHEDULED;
1060 1.35 riastrad dw->dw_resched = -1;
1061 1.35 riastrad } else {
1062 1.35 riastrad /* Change the rescheduled time. */
1063 1.12 riastrad dw->dw_resched = ticks;
1064 1.12 riastrad }
1065 1.35 riastrad timer_modified = true;
1066 1.35 riastrad break;
1067 1.35 riastrad case DELAYED_WORK_CANCELLED:
1068 1.12 riastrad /*
1069 1.29 riastrad * Someone cancelled it after the callout
1070 1.29 riastrad * started but before the poor thing even had a
1071 1.29 riastrad * chance to acquire the lock.
1072 1.29 riastrad */
1073 1.29 riastrad if (ticks == 0) {
1074 1.29 riastrad /*
1075 1.29 riastrad * We can just switch back to
1076 1.29 riastrad * DELAYED_WORK_SCHEDULED so that the
1077 1.29 riastrad * callout will queue the work as soon
1078 1.29 riastrad * as it gets the lock.
1079 1.29 riastrad */
1080 1.35 riastrad dw->dw_state = DELAYED_WORK_SCHEDULED;
1081 1.29 riastrad } else {
1082 1.12 riastrad /* Reschedule it. */
1083 1.12 riastrad dw->dw_state = DELAYED_WORK_RESCHEDULED;
1084 1.12 riastrad dw->dw_resched = MIN(INT_MAX, ticks);
1085 1.29 riastrad }
1086 1.1 skrll timer_modified = true;
1087 1.1 skrll break;
1088 1.12 riastrad default:
1089 1.1 skrll panic("invalid delayed work state: %d", dw->dw_state);
1090 1.1 skrll }
1091 1.1 skrll }
1092 1.1 skrll mutex_exit(&wq->wq_lock);
1093 1.36 riastrad
1094 1.36 riastrad return timer_modified;
1095 1.36 riastrad }
1096 1.36 riastrad
1097 1.36 riastrad /*
1098 1.36 riastrad * cancel_delayed_work(dw)
1099 1.36 riastrad *
1100 1.36 riastrad * If work was scheduled or queued, remove it from the schedule or
1101 1.36 riastrad * queue and return true. If work was not scheduled or queued,
1102 1.36 riastrad * return false. Note that work may already be running; if it
1103 1.1 skrll * hasn't been rescheduled or requeued, then cancel_delayed_work
1104 1.1 skrll * will return false, and either way, cancel_delayed_work will NOT
1105 1.1 skrll * wait for the work to complete.
1106 1.12 riastrad */
1107 1.12 riastrad bool
1108 1.1 skrll cancel_delayed_work(struct delayed_work *dw)
1109 1.14 riastrad {
1110 1.14 riastrad struct workqueue_struct *wq;
1111 1.14 riastrad bool cancelled_p;
1112 1.14 riastrad
1113 1.12 riastrad /* If there's no workqueue, nothing to cancel. */
1114 1.12 riastrad if ((wq = dw->work.work_queue) == NULL)
1115 1.12 riastrad return false;
1116 1.12 riastrad
1117 1.12 riastrad mutex_enter(&wq->wq_lock);
1118 1.12 riastrad if (__predict_false(dw->work.work_queue != wq)) {
1119 1.35 riastrad cancelled_p = false;
1120 1.35 riastrad } else {
1121 1.35 riastrad switch (dw->dw_state) {
1122 1.35 riastrad case DELAYED_WORK_IDLE:
1123 1.35 riastrad /*
1124 1.35 riastrad * It is either on the queue or already running
1125 1.29 riastrad * or both.
1126 1.35 riastrad */
1127 1.35 riastrad if (wq->wq_current_work != &dw->work ||
1128 1.35 riastrad wq->wq_requeued) {
1129 1.29 riastrad /*
1130 1.12 riastrad * It is on the queue, and it may or
1131 1.12 riastrad * may not be running. Remove it from
1132 1.35 riastrad * the queue.
1133 1.35 riastrad */
1134 1.35 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
1135 1.35 riastrad work_entry);
1136 1.35 riastrad if (wq->wq_current_work == &dw->work) {
1137 1.35 riastrad /*
1138 1.35 riastrad * If it is running, then it
1139 1.35 riastrad * must have been requeued in
1140 1.35 riastrad * this case, so mark it no
1141 1.35 riastrad * longer requeued.
1142 1.12 riastrad */
1143 1.35 riastrad KASSERT(wq->wq_requeued);
1144 1.35 riastrad wq->wq_requeued = false;
1145 1.35 riastrad }
1146 1.35 riastrad cancelled_p = true;
1147 1.35 riastrad } else {
1148 1.35 riastrad /*
1149 1.12 riastrad * Too late, it's already running, but
1150 1.12 riastrad * at least it hasn't been requeued.
1151 1.12 riastrad */
1152 1.21 riastrad cancelled_p = false;
1153 1.21 riastrad }
1154 1.21 riastrad break;
1155 1.21 riastrad case DELAYED_WORK_SCHEDULED:
1156 1.21 riastrad /*
1157 1.21 riastrad * If it is scheduled, mark it cancelled and
1158 1.21 riastrad * try to stop the callout before it starts.
1159 1.21 riastrad *
1160 1.21 riastrad * If it's too late and the callout has already
1161 1.21 riastrad * begun to execute, tough.
1162 1.21 riastrad *
1163 1.21 riastrad * If we stopped the callout before it started,
1164 1.12 riastrad * however, then destroy the callout and
1165 1.27 riastrad * dissociate it from the workqueue ourselves.
1166 1.27 riastrad */
1167 1.16 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
1168 1.34 riastrad cancelled_p = true;
1169 1.34 riastrad if (!callout_stop(&dw->dw_callout))
1170 1.34 riastrad cancel_delayed_work_done(wq, dw);
1171 1.34 riastrad break;
1172 1.34 riastrad case DELAYED_WORK_RESCHEDULED:
1173 1.34 riastrad /*
1174 1.35 riastrad * If it is being rescheduled, the callout has
1175 1.34 riastrad * already fired. We must ask it to cancel.
1176 1.34 riastrad */
1177 1.34 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
1178 1.34 riastrad dw->dw_resched = -1;
1179 1.34 riastrad cancelled_p = true;
1180 1.34 riastrad break;
1181 1.34 riastrad case DELAYED_WORK_CANCELLED:
1182 1.34 riastrad /*
1183 1.34 riastrad * If it is being cancelled, the callout has
1184 1.34 riastrad * already fired. There is nothing more for us
1185 1.34 riastrad * to do. Someone else claims credit for
1186 1.12 riastrad * cancelling it.
1187 1.12 riastrad */
1188 1.12 riastrad cancelled_p = false;
1189 1.12 riastrad break;
1190 1.1 skrll default:
1191 1.12 riastrad panic("invalid delayed work state: %d",
1192 1.1 skrll dw->dw_state);
1193 1.1 skrll }
1194 1.1 skrll }
1195 1.1 skrll mutex_exit(&wq->wq_lock);
1196 1.36 riastrad
1197 1.36 riastrad return cancelled_p;
1198 1.36 riastrad }
1199 1.36 riastrad
1200 1.36 riastrad /*
1201 1.36 riastrad * cancel_delayed_work_sync(dw)
1202 1.36 riastrad *
1203 1.36 riastrad * If work was scheduled or queued, remove it from the schedule or
1204 1.36 riastrad * queue and return true. If work was not scheduled or queued,
1205 1.1 skrll * return false. Note that work may already be running; if it
1206 1.1 skrll * hasn't been rescheduled or requeued, then cancel_delayed_work
1207 1.1 skrll * will return false; either way, wait for it to complete.
1208 1.12 riastrad */
1209 1.24 riastrad bool
1210 1.1 skrll cancel_delayed_work_sync(struct delayed_work *dw)
1211 1.24 riastrad {
1212 1.14 riastrad struct workqueue_struct *wq;
1213 1.24 riastrad bool cancelled_p;
1214 1.14 riastrad
1215 1.12 riastrad /* If there's no workqueue, nothing to cancel. */
1216 1.12 riastrad if ((wq = dw->work.work_queue) == NULL)
1217 1.12 riastrad return false;
1218 1.12 riastrad
1219 1.20 riastrad mutex_enter(&wq->wq_lock);
1220 1.12 riastrad if (__predict_false(dw->work.work_queue != wq)) {
1221 1.35 riastrad cancelled_p = false;
1222 1.35 riastrad } else {
1223 1.35 riastrad switch (dw->dw_state) {
1224 1.35 riastrad case DELAYED_WORK_IDLE:
1225 1.12 riastrad /*
1226 1.29 riastrad * It is either on the queue or already running
1227 1.29 riastrad * or both.
1228 1.29 riastrad */
1229 1.33 riastrad if (wq->wq_current_work == &dw->work) {
1230 1.29 riastrad /*
1231 1.29 riastrad * Too late, it's already running.
1232 1.29 riastrad * First, make sure it's not requeued.
1233 1.29 riastrad * Then wait for it to complete.
1234 1.29 riastrad */
1235 1.29 riastrad if (wq->wq_requeued) {
1236 1.33 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
1237 1.12 riastrad work_entry);
1238 1.12 riastrad wq->wq_requeued = false;
1239 1.12 riastrad }
1240 1.12 riastrad wait_for_current_work(&dw->work, wq);
1241 1.12 riastrad cancelled_p = false;
1242 1.12 riastrad } else {
1243 1.12 riastrad /* Got in before it started. Remove it. */
1244 1.12 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
1245 1.12 riastrad work_entry);
1246 1.12 riastrad cancelled_p = true;
1247 1.20 riastrad }
1248 1.20 riastrad break;
1249 1.20 riastrad case DELAYED_WORK_SCHEDULED:
1250 1.20 riastrad /*
1251 1.20 riastrad * If it is scheduled, mark it cancelled and
1252 1.24 riastrad * try to stop the callout before it starts.
1253 1.24 riastrad *
1254 1.24 riastrad * If it's too late and the callout has already
1255 1.20 riastrad * begun to execute, we must wait for it to
1256 1.20 riastrad * complete. But we got in soon enough to ask
1257 1.35 riastrad * the callout not to run, so we successfully
1258 1.20 riastrad * cancelled it in that case.
1259 1.12 riastrad *
1260 1.12 riastrad * If we stopped the callout before it started,
1261 1.27 riastrad * then we must destroy the callout and
1262 1.27 riastrad * dissociate it from the workqueue ourselves.
1263 1.34 riastrad */
1264 1.34 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
1265 1.34 riastrad if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
1266 1.34 riastrad cancel_delayed_work_done(wq, dw);
1267 1.34 riastrad cancelled_p = true;
1268 1.34 riastrad break;
1269 1.34 riastrad case DELAYED_WORK_RESCHEDULED:
1270 1.34 riastrad /*
1271 1.34 riastrad * If it is being rescheduled, the callout has
1272 1.35 riastrad * already fired. We must ask it to cancel and
1273 1.34 riastrad * wait for it to complete.
1274 1.34 riastrad */
1275 1.34 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
1276 1.34 riastrad dw->dw_resched = -1;
1277 1.34 riastrad (void)callout_halt(&dw->dw_callout, &wq->wq_lock);
1278 1.34 riastrad cancelled_p = true;
1279 1.34 riastrad break;
1280 1.34 riastrad case DELAYED_WORK_CANCELLED:
1281 1.34 riastrad /*
1282 1.34 riastrad * If it is being cancelled, the callout has
1283 1.34 riastrad * already fired. We need only wait for it to
1284 1.34 riastrad * complete. Someone else, however, claims
1285 1.20 riastrad * credit for cancelling it.
1286 1.12 riastrad */
1287 1.12 riastrad (void)callout_halt(&dw->dw_callout, &wq->wq_lock);
1288 1.12 riastrad cancelled_p = false;
1289 1.12 riastrad break;
1290 1.1 skrll default:
1291 1.12 riastrad panic("invalid delayed work state: %d",
1292 1.1 skrll dw->dw_state);
1293 1.1 skrll }
1294 1.1 skrll }
1295 1.12 riastrad mutex_exit(&wq->wq_lock);
1296 1.12 riastrad
1297 1.12 riastrad return cancelled_p;
1298 1.12 riastrad }
1299 1.1 skrll
1300 1.36 riastrad /*
1302 1.36 riastrad * Flush
1303 1.36 riastrad */
1304 1.36 riastrad
1305 1.36 riastrad /*
1306 1.5 riastrad * flush_scheduled_work()
1307 1.12 riastrad *
1308 1.5 riastrad * Wait for all work queued on system_wq to complete. This does
1309 1.5 riastrad * not include delayed work.
1310 1.12 riastrad */
1311 1.5 riastrad void
1312 1.5 riastrad flush_scheduled_work(void)
1313 1.36 riastrad {
1314 1.36 riastrad
1315 1.36 riastrad flush_workqueue(system_wq);
1316 1.36 riastrad }
1317 1.36 riastrad
1318 1.36 riastrad /*
1319 1.36 riastrad * flush_workqueue_locked(wq)
1320 1.36 riastrad *
1321 1.28 riastrad * Wait for all work queued on wq to complete. This does not
1322 1.28 riastrad * include delayed work.
1323 1.28 riastrad *
1324 1.28 riastrad * Caller must hold wq's lock.
1325 1.28 riastrad */
1326 1.28 riastrad static void
1327 1.28 riastrad flush_workqueue_locked(struct workqueue_struct *wq)
1328 1.28 riastrad {
1329 1.28 riastrad uint64_t gen;
1330 1.28 riastrad
1331 1.28 riastrad KASSERT(mutex_owned(&wq->wq_lock));
1332 1.28 riastrad
1333 1.28 riastrad /* Get the current generation number. */
1334 1.28 riastrad gen = wq->wq_gen;
1335 1.28 riastrad
1336 1.28 riastrad /*
1337 1.28 riastrad * If there's a batch of work in progress, we must wait for the
1338 1.28 riastrad * worker thread to finish that batch.
1339 1.28 riastrad */
1340 1.28 riastrad if (wq->wq_current_work != NULL)
1341 1.28 riastrad gen++;
1342 1.28 riastrad
1343 1.28 riastrad /*
1344 1.28 riastrad * If there's any work yet to be claimed from the queue by the
1345 1.28 riastrad * worker thread, we must wait for it to finish one more batch
1346 1.28 riastrad * too.
1347 1.28 riastrad */
1348 1.28 riastrad if (!TAILQ_EMPTY(&wq->wq_queue))
1349 1.28 riastrad gen++;
1350 1.28 riastrad
1351 1.36 riastrad /* Wait until the generation number has caught up. */
1352 1.36 riastrad while (wq->wq_gen < gen)
1353 1.36 riastrad cv_wait(&wq->wq_cv, &wq->wq_lock);
1354 1.36 riastrad }
1355 1.36 riastrad
1356 1.36 riastrad /*
1357 1.12 riastrad * flush_workqueue(wq)
1358 1.12 riastrad *
1359 1.1 skrll * Wait for all work queued on wq to complete. This does not
1360 1.1 skrll * include delayed work.
1361 1.12 riastrad */
1362 1.28 riastrad void
1363 1.12 riastrad flush_workqueue(struct workqueue_struct *wq)
1364 1.1 skrll {
1365 1.1 skrll
1366 1.36 riastrad mutex_enter(&wq->wq_lock);
1367 1.36 riastrad flush_workqueue_locked(wq);
1368 1.36 riastrad mutex_exit(&wq->wq_lock);
1369 1.36 riastrad }
1370 1.36 riastrad
1371 1.36 riastrad /*
1372 1.28 riastrad * flush_work(work)
1373 1.12 riastrad *
1374 1.1 skrll * If work is queued or currently executing, wait for it to
1375 1.14 riastrad * complete.
1376 1.1 skrll */
1377 1.14 riastrad void
1378 1.14 riastrad flush_work(struct work_struct *work)
1379 1.28 riastrad {
1380 1.1 skrll struct workqueue_struct *wq;
1381 1.12 riastrad
1382 1.1 skrll /* If there's no workqueue, nothing to flush. */
1383 1.1 skrll if ((wq = work->work_queue) == NULL)
1384 1.36 riastrad return;
1385 1.36 riastrad
1386 1.36 riastrad flush_workqueue(wq);
1387 1.36 riastrad }
1388 1.36 riastrad
1389 1.36 riastrad /*
1390 1.28 riastrad * flush_delayed_work(dw)
1391 1.12 riastrad *
1392 1.1 skrll * If dw is scheduled to run after a delay, cancel it. If dw is
1393 1.14 riastrad * queued or currently executing, wait for it to complete.
1394 1.1 skrll */
1395 1.14 riastrad void
1396 1.14 riastrad flush_delayed_work(struct delayed_work *dw)
1397 1.28 riastrad {
1398 1.1 skrll struct workqueue_struct *wq;
1399 1.1 skrll
1400 1.28 riastrad /* If there's no workqueue, nothing to flush. */
1401 1.28 riastrad if ((wq = dw->work.work_queue) == NULL)
1402 1.12 riastrad return;
1403 1.28 riastrad
1404 1.28 riastrad mutex_enter(&wq->wq_lock);
1405 1.28 riastrad if (__predict_true(dw->work.work_queue == wq)) {
1406 1.28 riastrad switch (dw->dw_state) {
1407 1.28 riastrad case DELAYED_WORK_IDLE:
1408 1.28 riastrad /*
1409 1.28 riastrad * It has a workqueue assigned and the callout
1410 1.28 riastrad * is idle, so it must be in progress or on the
1411 1.12 riastrad * queue. In that case, wait for it to
1412 1.12 riastrad * complete. Waiting for the whole queue to
1413 1.28 riastrad * flush is overkill, but doesn't hurt.
1414 1.35 riastrad */
1415 1.35 riastrad flush_workqueue_locked(wq);
1416 1.28 riastrad break;
1417 1.35 riastrad case DELAYED_WORK_SCHEDULED:
1418 1.35 riastrad /*
1419 1.35 riastrad * If it is scheduled, mark it cancelled and
1420 1.35 riastrad * try to stop the callout before it starts.
1421 1.28 riastrad *
1422 1.35 riastrad * If it's too late and the callout has already
1423 1.35 riastrad * begun to execute, we must wait for it to
1424 1.35 riastrad * complete. But we got in soon enough to ask
1425 1.28 riastrad * the callout not to run.
1426 1.12 riastrad *
1427 1.28 riastrad * If we stopped the callout before it started,
1428 1.28 riastrad * then we must destroy the callout and
1429 1.35 riastrad * dissociate it from the workqueue ourselves.
1430 1.35 riastrad */
1431 1.35 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
1432 1.35 riastrad if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
1433 1.35 riastrad cancel_delayed_work_done(wq, dw);
1434 1.35 riastrad break;
1435 1.35 riastrad case DELAYED_WORK_RESCHEDULED:
1436 1.35 riastrad /*
1437 1.35 riastrad * If it is being rescheduled, the callout has
1438 1.35 riastrad * already fired. We must ask it to cancel and
1439 1.35 riastrad * wait for it to complete.
1440 1.35 riastrad */
1441 1.35 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
1442 1.35 riastrad dw->dw_resched = -1;
1443 1.35 riastrad (void)callout_halt(&dw->dw_callout, &wq->wq_lock);
1444 1.35 riastrad break;
1445 1.35 riastrad case DELAYED_WORK_CANCELLED:
1446 1.35 riastrad /*
1447 1.35 riastrad * If it is being cancelled, the callout has
1448 1.12 riastrad * already fired. We need only wait for it to
1449 1.12 riastrad * complete.
1450 1.12 riastrad */
1451 1.12 riastrad (void)callout_halt(&dw->dw_callout, &wq->wq_lock);
1452 1.1 skrll break;
1453 1.12 riastrad default:
1454 1.1 skrll panic("invalid delayed work state: %d",
1455 dw->dw_state);
1456 }
1457 }
1458 mutex_exit(&wq->wq_lock);
1459 }
1460