linux_work.c revision 1.34 1 1.2 riastrad /* $NetBSD: linux_work.c,v 1.34 2018/08/27 15:04:45 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.34 2018/08/27 15:04:45 riastradh Exp $");
34 1.1 skrll
35 1.1 skrll #include <sys/types.h>
36 1.1 skrll #include <sys/atomic.h>
37 1.1 skrll #include <sys/callout.h>
38 1.1 skrll #include <sys/condvar.h>
39 1.1 skrll #include <sys/errno.h>
40 1.1 skrll #include <sys/kmem.h>
41 1.12 riastrad #include <sys/kthread.h>
42 1.12 riastrad #include <sys/lwp.h>
43 1.1 skrll #include <sys/mutex.h>
44 1.1 skrll #include <sys/queue.h>
45 1.1 skrll
46 1.1 skrll #include <linux/workqueue.h>
47 1.1 skrll
48 1.1 skrll struct workqueue_struct {
49 1.1 skrll kmutex_t wq_lock;
50 1.1 skrll kcondvar_t wq_cv;
51 1.1 skrll TAILQ_HEAD(, delayed_work) wq_delayed;
52 1.12 riastrad TAILQ_HEAD(, work_struct) wq_queue;
53 1.1 skrll struct work_struct *wq_current_work;
54 1.12 riastrad int wq_flags;
55 1.12 riastrad struct lwp *wq_lwp;
56 1.12 riastrad uint64_t wq_gen;
57 1.12 riastrad bool wq_requeued:1;
58 1.12 riastrad bool wq_dying:1;
59 1.1 skrll };
60 1.1 skrll
61 1.12 riastrad static void __dead linux_workqueue_thread(void *);
62 1.12 riastrad static void linux_workqueue_timeout(void *);
63 1.17 riastrad static struct workqueue_struct *
64 1.17 riastrad acquire_work(struct work_struct *,
65 1.17 riastrad struct workqueue_struct *);
66 1.17 riastrad static void release_work(struct work_struct *,
67 1.17 riastrad struct workqueue_struct *);
68 1.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.1 skrll int
84 1.1 skrll linux_workqueue_init(void)
85 1.1 skrll {
86 1.12 riastrad int error;
87 1.3 riastrad
88 1.12 riastrad error = lwp_specific_key_create(&workqueue_key, NULL);
89 1.12 riastrad if (error)
90 1.12 riastrad goto fail0;
91 1.1 skrll
92 1.1 skrll system_wq = alloc_ordered_workqueue("lnxsyswq", 0);
93 1.12 riastrad if (system_wq == NULL) {
94 1.12 riastrad error = ENOMEM;
95 1.12 riastrad goto fail1;
96 1.12 riastrad }
97 1.2 riastrad
98 1.2 riastrad system_long_wq = alloc_ordered_workqueue("lnxlngwq", 0);
99 1.12 riastrad if (system_long_wq == NULL) {
100 1.12 riastrad error = ENOMEM;
101 1.12 riastrad goto fail2;
102 1.12 riastrad }
103 1.1 skrll
104 1.6 riastrad system_power_efficient_wq = alloc_ordered_workqueue("lnxpwrwq", 0);
105 1.12 riastrad if (system_long_wq == NULL) {
106 1.12 riastrad error = ENOMEM;
107 1.12 riastrad goto fail3;
108 1.12 riastrad }
109 1.6 riastrad
110 1.1 skrll return 0;
111 1.2 riastrad
112 1.12 riastrad fail4: __unused
113 1.6 riastrad destroy_workqueue(system_power_efficient_wq);
114 1.12 riastrad fail3: destroy_workqueue(system_long_wq);
115 1.12 riastrad fail2: destroy_workqueue(system_wq);
116 1.12 riastrad fail1: lwp_specific_key_delete(workqueue_key);
117 1.12 riastrad fail0: KASSERT(error);
118 1.12 riastrad return error;
119 1.1 skrll }
120 1.1 skrll
121 1.1 skrll void
122 1.1 skrll linux_workqueue_fini(void)
123 1.1 skrll {
124 1.2 riastrad
125 1.12 riastrad destroy_workqueue(system_power_efficient_wq);
126 1.2 riastrad destroy_workqueue(system_long_wq);
127 1.1 skrll destroy_workqueue(system_wq);
128 1.12 riastrad lwp_specific_key_delete(workqueue_key);
129 1.1 skrll }
130 1.1 skrll
131 1.1 skrll /*
133 1.1 skrll * Workqueues
134 1.1 skrll */
135 1.1 skrll
136 1.12 riastrad struct workqueue_struct *
137 1.1 skrll alloc_ordered_workqueue(const char *name, int flags)
138 1.1 skrll {
139 1.1 skrll struct workqueue_struct *wq;
140 1.1 skrll int error;
141 1.12 riastrad
142 1.1 skrll KASSERT(flags == 0);
143 1.25 riastrad
144 1.1 skrll wq = kmem_zalloc(sizeof(*wq), KM_SLEEP);
145 1.12 riastrad
146 1.1 skrll mutex_init(&wq->wq_lock, MUTEX_DEFAULT, IPL_NONE);
147 1.1 skrll cv_init(&wq->wq_cv, name);
148 1.12 riastrad TAILQ_INIT(&wq->wq_delayed);
149 1.1 skrll TAILQ_INIT(&wq->wq_queue);
150 1.25 riastrad wq->wq_current_work = NULL;
151 1.25 riastrad wq->wq_flags = 0;
152 1.25 riastrad wq->wq_lwp = NULL;
153 1.25 riastrad wq->wq_gen = 0;
154 1.25 riastrad wq->wq_requeued = false;
155 1.1 skrll wq->wq_dying = false;
156 1.12 riastrad
157 1.12 riastrad error = kthread_create(PRI_NONE,
158 1.12 riastrad KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL,
159 1.12 riastrad &linux_workqueue_thread, wq, &wq->wq_lwp, "%s", name);
160 1.12 riastrad if (error)
161 1.3 riastrad goto fail0;
162 1.1 skrll
163 1.12 riastrad return wq;
164 1.12 riastrad
165 1.12 riastrad fail0: KASSERT(TAILQ_EMPTY(&wq->wq_queue));
166 1.12 riastrad KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
167 1.12 riastrad cv_destroy(&wq->wq_cv);
168 1.12 riastrad mutex_destroy(&wq->wq_lock);
169 1.12 riastrad kmem_free(wq, sizeof(*wq));
170 1.1 skrll return NULL;
171 1.1 skrll }
172 1.1 skrll
173 1.1 skrll void
174 1.1 skrll destroy_workqueue(struct workqueue_struct *wq)
175 1.1 skrll {
176 1.1 skrll
177 1.12 riastrad /*
178 1.12 riastrad * Cancel all delayed work. We do this first because any
179 1.12 riastrad * delayed work that that has already timed out, which we can't
180 1.1 skrll * cancel, may have queued new work.
181 1.26 riastrad */
182 1.26 riastrad mutex_enter(&wq->wq_lock);
183 1.26 riastrad while (!TAILQ_EMPTY(&wq->wq_delayed)) {
184 1.1 skrll struct delayed_work *const dw = TAILQ_FIRST(&wq->wq_delayed);
185 1.26 riastrad
186 1.26 riastrad KASSERT(dw->work.work_queue == wq);
187 1.26 riastrad KASSERTMSG((dw->dw_state == DELAYED_WORK_SCHEDULED ||
188 1.26 riastrad dw->dw_state == DELAYED_WORK_RESCHEDULED ||
189 1.26 riastrad dw->dw_state == DELAYED_WORK_CANCELLED),
190 1.26 riastrad "delayed work %p in bad state: %d",
191 1.26 riastrad dw, dw->dw_state);
192 1.26 riastrad
193 1.26 riastrad /*
194 1.26 riastrad * Mark it cancelled and try to stop the callout before
195 1.26 riastrad * it starts.
196 1.26 riastrad *
197 1.26 riastrad * If it's too late and the callout has already begun
198 1.26 riastrad * to execute, then it will notice that we asked to
199 1.26 riastrad * cancel it and remove itself from the queue before
200 1.26 riastrad * returning.
201 1.26 riastrad *
202 1.26 riastrad * If we stopped the callout before it started,
203 1.26 riastrad * however, then we can safely destroy the callout and
204 1.26 riastrad * dissociate it from the workqueue ourselves.
205 1.26 riastrad */
206 1.26 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
207 1.26 riastrad if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
208 1.26 riastrad cancel_delayed_work_done(wq, dw);
209 1.26 riastrad }
210 1.1 skrll mutex_exit(&wq->wq_lock);
211 1.26 riastrad
212 1.26 riastrad /*
213 1.26 riastrad * At this point, no new work can be put on the queue.
214 1.1 skrll */
215 1.12 riastrad
216 1.12 riastrad /* Tell the thread to exit. */
217 1.12 riastrad mutex_enter(&wq->wq_lock);
218 1.12 riastrad wq->wq_dying = true;
219 1.12 riastrad cv_broadcast(&wq->wq_cv);
220 1.12 riastrad mutex_exit(&wq->wq_lock);
221 1.12 riastrad
222 1.12 riastrad /* Wait for it to exit. */
223 1.12 riastrad (void)kthread_join(wq->wq_lwp);
224 1.25 riastrad
225 1.25 riastrad KASSERT(wq->wq_dying);
226 1.25 riastrad KASSERT(!wq->wq_requeued);
227 1.1 skrll KASSERT(wq->wq_flags == 0);
228 1.12 riastrad KASSERT(wq->wq_current_work == NULL);
229 1.12 riastrad KASSERT(TAILQ_EMPTY(&wq->wq_queue));
230 1.1 skrll KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
231 1.1 skrll cv_destroy(&wq->wq_cv);
232 1.1 skrll mutex_destroy(&wq->wq_lock);
233 1.1 skrll
234 1.1 skrll kmem_free(wq, sizeof(*wq));
235 1.1 skrll }
236 1.1 skrll
237 1.12 riastrad /*
239 1.1 skrll * Work thread and callout
240 1.12 riastrad */
241 1.12 riastrad
242 1.1 skrll static void __dead
243 1.12 riastrad linux_workqueue_thread(void *cookie)
244 1.12 riastrad {
245 1.1 skrll struct workqueue_struct *const wq = cookie;
246 1.12 riastrad TAILQ_HEAD(, work_struct) tmp;
247 1.1 skrll
248 1.12 riastrad lwp_setspecific(workqueue_key, wq);
249 1.12 riastrad
250 1.26 riastrad mutex_enter(&wq->wq_lock);
251 1.26 riastrad for (;;) {
252 1.26 riastrad /*
253 1.26 riastrad * Wait until there's activity. If there's no work and
254 1.12 riastrad * we're dying, stop here.
255 1.12 riastrad */
256 1.26 riastrad while (TAILQ_EMPTY(&wq->wq_queue) && !wq->wq_dying)
257 1.26 riastrad cv_wait(&wq->wq_cv, &wq->wq_lock);
258 1.12 riastrad if (TAILQ_EMPTY(&wq->wq_queue)) {
259 1.26 riastrad KASSERT(wq->wq_dying);
260 1.1 skrll break;
261 1.12 riastrad }
262 1.12 riastrad
263 1.12 riastrad /* Grab a batch of work off the queue. */
264 1.12 riastrad KASSERT(!TAILQ_EMPTY(&wq->wq_queue));
265 1.12 riastrad TAILQ_INIT(&tmp);
266 1.12 riastrad TAILQ_CONCAT(&tmp, &wq->wq_queue, work_entry);
267 1.12 riastrad
268 1.12 riastrad /* Process each work item in the batch. */
269 1.12 riastrad while (!TAILQ_EMPTY(&tmp)) {
270 1.18 riastrad struct work_struct *const work = TAILQ_FIRST(&tmp);
271 1.12 riastrad
272 1.12 riastrad KASSERT(work->work_queue == wq);
273 1.12 riastrad TAILQ_REMOVE(&tmp, work, work_entry);
274 1.1 skrll KASSERT(wq->wq_current_work == NULL);
275 1.12 riastrad wq->wq_current_work = work;
276 1.12 riastrad
277 1.12 riastrad mutex_exit(&wq->wq_lock);
278 1.1 skrll (*work->func)(work);
279 1.12 riastrad mutex_enter(&wq->wq_lock);
280 1.12 riastrad
281 1.12 riastrad KASSERT(wq->wq_current_work == work);
282 1.12 riastrad KASSERT(work->work_queue == wq);
283 1.12 riastrad if (wq->wq_requeued)
284 1.17 riastrad wq->wq_requeued = false;
285 1.12 riastrad else
286 1.12 riastrad release_work(work, wq);
287 1.12 riastrad wq->wq_current_work = NULL;
288 1.1 skrll cv_broadcast(&wq->wq_cv);
289 1.12 riastrad }
290 1.12 riastrad
291 1.12 riastrad /* Notify flush that we've completed a batch of work. */
292 1.1 skrll wq->wq_gen++;
293 1.12 riastrad cv_broadcast(&wq->wq_cv);
294 1.1 skrll }
295 1.12 riastrad mutex_exit(&wq->wq_lock);
296 1.1 skrll
297 1.1 skrll kthread_exit(0);
298 1.1 skrll }
299 1.12 riastrad
300 1.1 skrll static void
301 1.12 riastrad linux_workqueue_timeout(void *cookie)
302 1.12 riastrad {
303 1.1 skrll struct delayed_work *const dw = cookie;
304 1.12 riastrad struct workqueue_struct *const wq = dw->work.work_queue;
305 1.14 riastrad
306 1.12 riastrad KASSERT(wq != NULL);
307 1.18 riastrad
308 1.12 riastrad mutex_enter(&wq->wq_lock);
309 1.12 riastrad KASSERT(dw->work.work_queue == wq);
310 1.12 riastrad switch (dw->dw_state) {
311 1.12 riastrad case DELAYED_WORK_IDLE:
312 1.31 riastrad panic("delayed work callout uninitialized: %p", dw);
313 1.12 riastrad case DELAYED_WORK_SCHEDULED:
314 1.12 riastrad dw_callout_destroy(wq, dw);
315 1.12 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, work_entry);
316 1.12 riastrad cv_broadcast(&wq->wq_cv);
317 1.12 riastrad break;
318 1.12 riastrad case DELAYED_WORK_RESCHEDULED:
319 1.12 riastrad dw->dw_state = DELAYED_WORK_SCHEDULED;
320 1.23 riastrad break;
321 1.22 riastrad case DELAYED_WORK_CANCELLED:
322 1.22 riastrad cancel_delayed_work_done(wq, dw);
323 1.12 riastrad /* Can't touch dw any more. */
324 1.12 riastrad goto out;
325 1.12 riastrad default:
326 1.15 riastrad panic("delayed work callout in bad state: %p", dw);
327 1.15 riastrad }
328 1.22 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE ||
329 1.1 skrll dw->dw_state == DELAYED_WORK_SCHEDULED);
330 1.1 skrll out: mutex_exit(&wq->wq_lock);
331 1.12 riastrad }
332 1.12 riastrad
333 1.1 skrll struct work_struct *
334 1.12 riastrad current_work(void)
335 1.1 skrll {
336 1.12 riastrad struct workqueue_struct *wq = lwp_getspecific(workqueue_key);
337 1.12 riastrad
338 1.12 riastrad /* If we're not a workqueue thread, then there's no work. */
339 1.1 skrll if (wq == NULL)
340 1.12 riastrad return NULL;
341 1.12 riastrad
342 1.12 riastrad /*
343 1.12 riastrad * Otherwise, this should be possible only while work is in
344 1.12 riastrad * progress. Return the current work item.
345 1.12 riastrad */
346 1.1 skrll KASSERT(wq->wq_current_work != NULL);
347 1.1 skrll return wq->wq_current_work;
348 1.1 skrll }
349 1.1 skrll
350 1.1 skrll /*
352 1.1 skrll * Work
353 1.1 skrll */
354 1.1 skrll
355 1.1 skrll void
356 1.12 riastrad INIT_WORK(struct work_struct *work, void (*fn)(struct work_struct *))
357 1.4 riastrad {
358 1.1 skrll
359 1.1 skrll work->work_queue = NULL;
360 1.17 riastrad work->func = fn;
361 1.17 riastrad }
362 1.17 riastrad
363 1.17 riastrad static struct workqueue_struct *
364 1.17 riastrad acquire_work(struct work_struct *work, struct workqueue_struct *wq)
365 1.17 riastrad {
366 1.17 riastrad struct workqueue_struct *wq0;
367 1.17 riastrad
368 1.17 riastrad KASSERT(mutex_owned(&wq->wq_lock));
369 1.17 riastrad
370 1.17 riastrad wq0 = atomic_cas_ptr(&work->work_queue, NULL, wq);
371 1.17 riastrad if (wq0 == NULL) {
372 1.17 riastrad membar_enter();
373 1.17 riastrad KASSERT(work->work_queue == wq);
374 1.17 riastrad }
375 1.17 riastrad
376 1.17 riastrad return wq0;
377 1.17 riastrad }
378 1.17 riastrad
379 1.17 riastrad static void
380 1.17 riastrad release_work(struct work_struct *work, struct workqueue_struct *wq)
381 1.17 riastrad {
382 1.17 riastrad
383 1.17 riastrad KASSERT(work->work_queue == wq);
384 1.17 riastrad KASSERT(mutex_owned(&wq->wq_lock));
385 1.17 riastrad
386 1.17 riastrad membar_exit();
387 1.1 skrll work->work_queue = NULL;
388 1.1 skrll }
389 1.1 skrll
390 1.12 riastrad bool
391 1.1 skrll schedule_work(struct work_struct *work)
392 1.1 skrll {
393 1.1 skrll
394 1.1 skrll return queue_work(system_wq, work);
395 1.1 skrll }
396 1.1 skrll
397 1.12 riastrad bool
398 1.1 skrll queue_work(struct workqueue_struct *wq, struct work_struct *work)
399 1.1 skrll {
400 1.1 skrll struct workqueue_struct *wq0;
401 1.1 skrll bool newly_queued;
402 1.12 riastrad
403 1.17 riastrad KASSERT(wq != NULL);
404 1.29 riastrad
405 1.29 riastrad mutex_enter(&wq->wq_lock);
406 1.29 riastrad if (__predict_true((wq0 = acquire_work(work, wq)) == NULL)) {
407 1.29 riastrad /*
408 1.29 riastrad * It wasn't on any workqueue at all. Put it on this
409 1.12 riastrad * one, and signal the worker thread that there is work
410 1.1 skrll * to do.
411 1.29 riastrad */
412 1.12 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry);
413 1.29 riastrad newly_queued = true;
414 1.29 riastrad cv_broadcast(&wq->wq_cv);
415 1.29 riastrad } else {
416 1.29 riastrad /*
417 1.29 riastrad * It was on a workqueue, which had better be this one.
418 1.29 riastrad * Requeue it if it has been taken off the queue to
419 1.29 riastrad * execute and hasn't been requeued yet. The worker
420 1.12 riastrad * thread should already be running, so no need to
421 1.29 riastrad * signal it.
422 1.29 riastrad */
423 1.29 riastrad KASSERT(wq0 == wq);
424 1.29 riastrad if (wq->wq_current_work == work && !wq->wq_requeued) {
425 1.29 riastrad /*
426 1.29 riastrad * It has been taken off the queue to execute,
427 1.29 riastrad * and it hasn't been put back on the queue
428 1.29 riastrad * again. Put it back on the queue. No need
429 1.29 riastrad * to signal the worker thread because it will
430 1.29 riastrad * notice when it reacquires the lock after
431 1.29 riastrad * doing the work.
432 1.29 riastrad */
433 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry);
434 1.29 riastrad wq->wq_requeued = true;
435 1.29 riastrad newly_queued = true;
436 1.29 riastrad } else {
437 1.1 skrll /* It is still on the queue; nothing to do. */
438 1.12 riastrad newly_queued = false;
439 1.1 skrll }
440 1.1 skrll }
441 1.1 skrll mutex_exit(&wq->wq_lock);
442 1.1 skrll
443 1.1 skrll return newly_queued;
444 1.12 riastrad }
445 1.1 skrll
446 1.12 riastrad bool
447 1.1 skrll cancel_work(struct work_struct *work)
448 1.1 skrll {
449 1.13 riastrad struct workqueue_struct *wq;
450 1.13 riastrad bool cancelled_p = false;
451 1.13 riastrad
452 1.13 riastrad /* If there's no workqueue, nothing to cancel. */
453 1.12 riastrad if ((wq = work->work_queue) == NULL)
454 1.12 riastrad goto out;
455 1.29 riastrad
456 1.29 riastrad mutex_enter(&wq->wq_lock);
457 1.29 riastrad if (__predict_false(work->work_queue != wq)) {
458 1.29 riastrad /*
459 1.29 riastrad * It has finished execution or been cancelled by
460 1.12 riastrad * another thread, and has been moved off the
461 1.12 riastrad * workqueue, so it's too to cancel.
462 1.29 riastrad */
463 1.29 riastrad cancelled_p = false;
464 1.29 riastrad } else if (wq->wq_current_work == work) {
465 1.29 riastrad /*
466 1.12 riastrad * It has already begun execution, so it's too late to
467 1.12 riastrad * cancel now.
468 1.29 riastrad */
469 1.29 riastrad cancelled_p = false;
470 1.29 riastrad } else {
471 1.29 riastrad /*
472 1.12 riastrad * It is still on the queue. Take it off the queue and
473 1.1 skrll * report successful cancellation.
474 1.1 skrll */
475 1.12 riastrad TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
476 1.1 skrll cancelled_p = true;
477 1.13 riastrad }
478 1.1 skrll mutex_exit(&wq->wq_lock);
479 1.1 skrll
480 1.12 riastrad out: return cancelled_p;
481 1.12 riastrad }
482 1.1 skrll
483 1.1 skrll bool
484 1.12 riastrad cancel_work_sync(struct work_struct *work)
485 1.1 skrll {
486 1.13 riastrad struct workqueue_struct *wq;
487 1.13 riastrad bool cancelled_p = false;
488 1.13 riastrad
489 1.13 riastrad /* If there's no workqueue, nothing to cancel. */
490 1.1 skrll if ((wq = work->work_queue) == NULL)
491 1.12 riastrad goto out;
492 1.29 riastrad
493 1.29 riastrad mutex_enter(&wq->wq_lock);
494 1.29 riastrad if (__predict_false(work->work_queue != wq)) {
495 1.29 riastrad /*
496 1.29 riastrad * It has finished execution or been cancelled by
497 1.12 riastrad * another thread, and has been moved off the
498 1.12 riastrad * workqueue, so it's too to cancel.
499 1.29 riastrad */
500 1.29 riastrad cancelled_p = false;
501 1.33 riastrad } else if (wq->wq_current_work == work) {
502 1.29 riastrad /*
503 1.33 riastrad * It has already begun execution, so it's too late to
504 1.12 riastrad * cancel now. Wait for it to complete.
505 1.12 riastrad */
506 1.29 riastrad wait_for_current_work(work, wq);
507 1.29 riastrad cancelled_p = false;
508 1.29 riastrad } else {
509 1.29 riastrad /*
510 1.12 riastrad * It is still on the queue. Take it off the queue and
511 1.12 riastrad * report successful cancellation.
512 1.12 riastrad */
513 1.1 skrll TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
514 1.1 skrll cancelled_p = true;
515 1.13 riastrad }
516 1.1 skrll mutex_exit(&wq->wq_lock);
517 1.33 riastrad
518 1.33 riastrad out: return cancelled_p;
519 1.33 riastrad }
520 1.33 riastrad
521 1.33 riastrad /*
522 1.33 riastrad * wait_for_current_work(work, wq)
523 1.33 riastrad *
524 1.33 riastrad * wq must be currently executing work. Wait for it to finish.
525 1.33 riastrad */
526 1.33 riastrad static void
527 1.33 riastrad wait_for_current_work(struct work_struct *work, struct workqueue_struct *wq)
528 1.33 riastrad {
529 1.33 riastrad uint64_t gen;
530 1.33 riastrad
531 1.33 riastrad KASSERT(mutex_owned(&wq->wq_lock));
532 1.33 riastrad KASSERT(work->work_queue == wq);
533 1.33 riastrad KASSERT(wq->wq_current_work == work);
534 1.33 riastrad
535 1.33 riastrad /* Wait only one generation in case it gets requeued quickly. */
536 1.33 riastrad gen = wq->wq_gen;
537 1.33 riastrad do {
538 1.1 skrll cv_wait(&wq->wq_cv, &wq->wq_lock);
539 1.1 skrll } while (wq->wq_current_work == work && wq->wq_gen == gen);
540 1.1 skrll }
541 1.1 skrll
542 1.1 skrll /*
544 1.1 skrll * Delayed work
545 1.1 skrll */
546 1.12 riastrad
547 1.1 skrll void
548 1.12 riastrad INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work_struct *))
549 1.12 riastrad {
550 1.12 riastrad
551 1.12 riastrad INIT_WORK(&dw->work, fn);
552 1.12 riastrad dw->dw_state = DELAYED_WORK_IDLE;
553 1.12 riastrad
554 1.12 riastrad /*
555 1.12 riastrad * Defer callout_init until we are going to schedule the
556 1.1 skrll * callout, which can then callout_destroy it, because
557 1.1 skrll * otherwise since there's no DESTROY_DELAYED_WORK or anything
558 1.1 skrll * we have no opportunity to call callout_destroy.
559 1.1 skrll */
560 1.1 skrll }
561 1.12 riastrad
562 1.1 skrll bool
563 1.1 skrll schedule_delayed_work(struct delayed_work *dw, unsigned long ticks)
564 1.1 skrll {
565 1.29 riastrad
566 1.30 riastrad return queue_delayed_work(system_wq, dw, ticks);
567 1.30 riastrad }
568 1.30 riastrad
569 1.30 riastrad /*
570 1.30 riastrad * dw_callout_init(wq, dw)
571 1.30 riastrad *
572 1.30 riastrad * Initialize the callout of dw and transition to
573 1.30 riastrad * DELAYED_WORK_SCHEDULED. Caller must use callout_schedule.
574 1.30 riastrad */
575 1.30 riastrad static void
576 1.30 riastrad dw_callout_init(struct workqueue_struct *wq, struct delayed_work *dw)
577 1.30 riastrad {
578 1.30 riastrad
579 1.30 riastrad KASSERT(mutex_owned(&wq->wq_lock));
580 1.30 riastrad KASSERT(dw->work.work_queue == wq);
581 1.30 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
582 1.30 riastrad
583 1.30 riastrad callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
584 1.30 riastrad callout_setfunc(&dw->dw_callout, &linux_workqueue_timeout, dw);
585 1.30 riastrad TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
586 1.31 riastrad dw->dw_state = DELAYED_WORK_SCHEDULED;
587 1.31 riastrad }
588 1.31 riastrad
589 1.31 riastrad /*
590 1.31 riastrad * dw_callout_destroy(wq, dw)
591 1.31 riastrad *
592 1.31 riastrad * Destroy the callout of dw and transition to DELAYED_WORK_IDLE.
593 1.31 riastrad */
594 1.31 riastrad static void
595 1.31 riastrad dw_callout_destroy(struct workqueue_struct *wq, struct delayed_work *dw)
596 1.31 riastrad {
597 1.31 riastrad
598 1.31 riastrad KASSERT(mutex_owned(&wq->wq_lock));
599 1.31 riastrad KASSERT(dw->work.work_queue == wq);
600 1.31 riastrad KASSERT(dw->dw_state == DELAYED_WORK_SCHEDULED ||
601 1.31 riastrad dw->dw_state == DELAYED_WORK_RESCHEDULED ||
602 1.31 riastrad dw->dw_state == DELAYED_WORK_CANCELLED);
603 1.31 riastrad
604 1.31 riastrad TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
605 1.31 riastrad callout_destroy(&dw->dw_callout);
606 1.29 riastrad dw->dw_state = DELAYED_WORK_IDLE;
607 1.29 riastrad }
608 1.29 riastrad
609 1.29 riastrad /*
610 1.29 riastrad * cancel_delayed_work_done(wq, dw)
611 1.29 riastrad *
612 1.23 riastrad * Complete cancellation of a delayed work: transition from
613 1.23 riastrad * DELAYED_WORK_CANCELLED to DELAYED_WORK_IDLE and off the
614 1.23 riastrad * workqueue. Caller must not touch dw after this returns.
615 1.23 riastrad */
616 1.23 riastrad static void
617 1.23 riastrad cancel_delayed_work_done(struct workqueue_struct *wq, struct delayed_work *dw)
618 1.23 riastrad {
619 1.31 riastrad
620 1.31 riastrad KASSERT(mutex_owned(&wq->wq_lock));
621 1.23 riastrad KASSERT(dw->work.work_queue == wq);
622 1.23 riastrad KASSERT(dw->dw_state == DELAYED_WORK_CANCELLED);
623 1.23 riastrad
624 1.23 riastrad dw_callout_destroy(wq, dw);
625 1.29 riastrad release_work(&dw->work, wq);
626 1.29 riastrad /* Can't touch dw after this point. */
627 1.29 riastrad }
628 1.29 riastrad
629 1.29 riastrad /*
630 1.29 riastrad * queue_delayed_work(wq, dw, ticks)
631 1.29 riastrad *
632 1.12 riastrad * If it is not currently scheduled, schedule dw to run after
633 1.12 riastrad * ticks. If currently executing and not already rescheduled,
634 1.12 riastrad * reschedule it. If ticks == 0, run without delay.
635 1.12 riastrad */
636 1.12 riastrad bool
637 1.12 riastrad queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
638 1.1 skrll unsigned long ticks)
639 1.12 riastrad {
640 1.17 riastrad struct workqueue_struct *wq0;
641 1.29 riastrad bool newly_queued;
642 1.29 riastrad
643 1.29 riastrad mutex_enter(&wq->wq_lock);
644 1.29 riastrad if (__predict_true((wq0 = acquire_work(&dw->work, wq)) == NULL)) {
645 1.12 riastrad /*
646 1.29 riastrad * It wasn't on any workqueue at all. Schedule it to
647 1.29 riastrad * run on this one.
648 1.29 riastrad */
649 1.29 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
650 1.29 riastrad if (ticks == 0) {
651 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work,
652 1.29 riastrad work_entry);
653 1.29 riastrad cv_broadcast(&wq->wq_cv);
654 1.29 riastrad } else {
655 1.30 riastrad /*
656 1.29 riastrad * Initialize a callout and schedule to run
657 1.29 riastrad * after a delay.
658 1.12 riastrad */
659 1.12 riastrad dw_callout_init(wq, dw);
660 1.29 riastrad callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
661 1.29 riastrad }
662 1.29 riastrad newly_queued = true;
663 1.29 riastrad } else {
664 1.29 riastrad /*
665 1.29 riastrad * It was on a workqueue, which had better be this one.
666 1.29 riastrad *
667 1.29 riastrad * - If it has already begun to run, and it is not yet
668 1.29 riastrad * scheduled to run again, schedule it again.
669 1.29 riastrad *
670 1.12 riastrad * - If the callout is cancelled, reschedule it.
671 1.29 riastrad *
672 1.29 riastrad * - Otherwise, leave it alone.
673 1.29 riastrad */
674 1.29 riastrad KASSERT(wq0 == wq);
675 1.29 riastrad if (wq->wq_current_work != &dw->work || !wq->wq_requeued) {
676 1.29 riastrad /*
677 1.29 riastrad * It is either scheduled, on the queue but not
678 1.29 riastrad * in progress, or in progress but not on the
679 1.29 riastrad * queue.
680 1.29 riastrad */
681 1.29 riastrad switch (dw->dw_state) {
682 1.29 riastrad case DELAYED_WORK_IDLE:
683 1.29 riastrad /*
684 1.29 riastrad * It is not scheduled to run, and it
685 1.29 riastrad * is not on the queue if it is
686 1.29 riastrad * running.
687 1.29 riastrad */
688 1.29 riastrad if (ticks == 0) {
689 1.29 riastrad /*
690 1.29 riastrad * If it's in progress, put it
691 1.29 riastrad * on the queue to run as soon
692 1.29 riastrad * as the worker thread gets to
693 1.29 riastrad * it. No need for a wakeup
694 1.29 riastrad * because either the worker
695 1.29 riastrad * thread already knows it is
696 1.29 riastrad * on the queue, or will check
697 1.29 riastrad * once it is done executing.
698 1.29 riastrad */
699 1.29 riastrad if (wq->wq_current_work == &dw->work) {
700 1.29 riastrad KASSERT(!wq->wq_requeued);
701 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue,
702 1.29 riastrad &dw->work, work_entry);
703 1.29 riastrad wq->wq_requeued = true;
704 1.29 riastrad }
705 1.29 riastrad } else {
706 1.29 riastrad /*
707 1.30 riastrad * Initialize a callout and
708 1.30 riastrad * schedule it to run after the
709 1.30 riastrad * specified delay.
710 1.29 riastrad */
711 1.29 riastrad dw_callout_init(wq, dw);
712 1.29 riastrad callout_schedule(&dw->dw_callout,
713 1.29 riastrad MIN(INT_MAX, ticks));
714 1.29 riastrad }
715 1.29 riastrad break;
716 1.29 riastrad case DELAYED_WORK_SCHEDULED:
717 1.29 riastrad case DELAYED_WORK_RESCHEDULED:
718 1.29 riastrad /*
719 1.29 riastrad * It is already scheduled to run after
720 1.29 riastrad * a delay. Leave it be.
721 1.29 riastrad */
722 1.29 riastrad break;
723 1.29 riastrad case DELAYED_WORK_CANCELLED:
724 1.29 riastrad /*
725 1.29 riastrad * It was scheduled and the callout has
726 1.29 riastrad * begun to execute, but it was
727 1.29 riastrad * cancelled. Reschedule it.
728 1.29 riastrad */
729 1.29 riastrad dw->dw_state = DELAYED_WORK_RESCHEDULED;
730 1.29 riastrad callout_schedule(&dw->dw_callout,
731 1.29 riastrad MIN(INT_MAX, ticks));
732 1.29 riastrad break;
733 1.29 riastrad default:
734 1.29 riastrad panic("invalid delayed work state: %d",
735 1.29 riastrad dw->dw_state);
736 1.29 riastrad }
737 1.29 riastrad } else {
738 1.29 riastrad /*
739 1.29 riastrad * It is in progress and it has been requeued.
740 1.29 riastrad * It cannot be scheduled to run after a delay
741 1.29 riastrad * at this point. We just leave it be.
742 1.29 riastrad */
743 1.1 skrll KASSERTMSG((dw->dw_state == DELAYED_WORK_IDLE),
744 1.12 riastrad "delayed work %p in wrong state: %d",
745 1.1 skrll dw, dw->dw_state);
746 1.1 skrll }
747 1.1 skrll }
748 1.1 skrll mutex_exit(&wq->wq_lock);
749 1.29 riastrad
750 1.29 riastrad return newly_queued;
751 1.29 riastrad }
752 1.29 riastrad
753 1.29 riastrad /*
754 1.29 riastrad * mod_delayed_work(wq, dw, ticks)
755 1.29 riastrad *
756 1.1 skrll * Schedule dw to run after ticks. If currently scheduled,
757 1.1 skrll * reschedule it. If currently executing, reschedule it. If
758 1.1 skrll * ticks == 0, run without delay.
759 1.1 skrll */
760 1.12 riastrad bool
761 1.1 skrll mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
762 1.1 skrll unsigned long ticks)
763 1.12 riastrad {
764 1.17 riastrad struct workqueue_struct *wq0;
765 1.29 riastrad bool timer_modified;
766 1.29 riastrad
767 1.29 riastrad mutex_enter(&wq->wq_lock);
768 1.29 riastrad if ((wq0 = acquire_work(&dw->work, wq)) == NULL) {
769 1.12 riastrad /*
770 1.29 riastrad * It wasn't on any workqueue at all. Schedule it to
771 1.29 riastrad * run on this one.
772 1.29 riastrad */
773 1.29 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
774 1.29 riastrad if (ticks == 0) {
775 1.29 riastrad /*
776 1.29 riastrad * Run immediately: put it on the queue and
777 1.29 riastrad * signal the worker thread.
778 1.29 riastrad */
779 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work,
780 1.29 riastrad work_entry);
781 1.29 riastrad cv_broadcast(&wq->wq_cv);
782 1.29 riastrad } else {
783 1.30 riastrad /*
784 1.30 riastrad * Initialize a callout and schedule to run
785 1.29 riastrad * after a delay.
786 1.12 riastrad */
787 1.12 riastrad dw_callout_init(wq, dw);
788 1.29 riastrad callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
789 1.12 riastrad }
790 1.12 riastrad timer_modified = false;
791 1.12 riastrad } else {
792 1.29 riastrad /* It was on a workqueue, which had better be this one. */
793 1.29 riastrad KASSERT(wq0 == wq);
794 1.29 riastrad switch (dw->dw_state) {
795 1.29 riastrad case DELAYED_WORK_IDLE:
796 1.32 riastrad /*
797 1.32 riastrad * It is not scheduled: it is on the queue or
798 1.29 riastrad * it is running or both.
799 1.32 riastrad */
800 1.32 riastrad if (wq->wq_current_work != &dw->work ||
801 1.29 riastrad wq->wq_requeued) {
802 1.29 riastrad /*
803 1.29 riastrad * It is on the queue, and it may or
804 1.29 riastrad * may not be running.
805 1.29 riastrad */
806 1.29 riastrad if (ticks == 0) {
807 1.29 riastrad /*
808 1.29 riastrad * We ask it to run
809 1.29 riastrad * immediately. Leave it on
810 1.29 riastrad * the queue.
811 1.29 riastrad */
812 1.29 riastrad } else {
813 1.29 riastrad /*
814 1.32 riastrad * Take it off the queue and
815 1.32 riastrad * schedule a callout to run it
816 1.32 riastrad * after a delay.
817 1.32 riastrad */
818 1.32 riastrad if (wq->wq_requeued) {
819 1.32 riastrad wq->wq_requeued = false;
820 1.29 riastrad } else {
821 1.29 riastrad KASSERT(wq->wq_current_work !=
822 1.30 riastrad &dw->work);
823 1.30 riastrad }
824 1.30 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
825 1.29 riastrad work_entry);
826 1.29 riastrad dw_callout_init(wq, dw);
827 1.29 riastrad callout_schedule(&dw->dw_callout,
828 1.12 riastrad MIN(INT_MAX, ticks));
829 1.29 riastrad }
830 1.29 riastrad timer_modified = true;
831 1.12 riastrad } else {
832 1.29 riastrad /*
833 1.29 riastrad * It is currently running and has not
834 1.29 riastrad * been requeued.
835 1.29 riastrad */
836 1.29 riastrad if (ticks == 0) {
837 1.29 riastrad /*
838 1.29 riastrad * We ask it to run
839 1.29 riastrad * immediately. Put it on the
840 1.29 riastrad * queue again.
841 1.29 riastrad */
842 1.29 riastrad wq->wq_requeued = true;
843 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue,
844 1.29 riastrad &dw->work, work_entry);
845 1.29 riastrad } else {
846 1.30 riastrad /*
847 1.30 riastrad * Schedule a callout to run it
848 1.30 riastrad * after a delay.
849 1.29 riastrad */
850 1.12 riastrad dw_callout_init(wq, dw);
851 1.12 riastrad callout_schedule(&dw->dw_callout,
852 1.12 riastrad MIN(INT_MAX, ticks));
853 1.12 riastrad }
854 1.29 riastrad timer_modified = false;
855 1.29 riastrad }
856 1.29 riastrad break;
857 1.29 riastrad case DELAYED_WORK_SCHEDULED:
858 1.29 riastrad /*
859 1.29 riastrad * It is scheduled to run after a delay. Try
860 1.12 riastrad * to stop it and reschedule it; if we can't,
861 1.29 riastrad * either reschedule it or cancel it to put it
862 1.29 riastrad * on the queue, and inform the callout.
863 1.29 riastrad */
864 1.29 riastrad if (callout_stop(&dw->dw_callout)) {
865 1.29 riastrad /* Can't stop, callout has begun. */
866 1.29 riastrad if (ticks == 0) {
867 1.29 riastrad /*
868 1.29 riastrad * We don't actually need to do
869 1.29 riastrad * anything. The callout will
870 1.29 riastrad * queue it as soon as it gets
871 1.29 riastrad * the lock.
872 1.29 riastrad */
873 1.29 riastrad } else {
874 1.29 riastrad /*
875 1.29 riastrad * Schedule callout and tell
876 1.29 riastrad * the instance that's running
877 1.29 riastrad * now that it's been
878 1.29 riastrad * rescheduled.
879 1.29 riastrad */
880 1.12 riastrad dw->dw_state = DELAYED_WORK_RESCHEDULED;
881 1.29 riastrad callout_schedule(&dw->dw_callout,
882 1.29 riastrad MIN(INT_MAX, ticks));
883 1.29 riastrad }
884 1.29 riastrad } else {
885 1.29 riastrad if (ticks == 0) {
886 1.29 riastrad /*
887 1.29 riastrad * Run immediately: destroy the
888 1.31 riastrad * callout, put it on the
889 1.29 riastrad * queue, and signal the worker
890 1.29 riastrad * thread.
891 1.29 riastrad */
892 1.29 riastrad dw_callout_destroy(wq, dw);
893 1.29 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue,
894 1.29 riastrad &dw->work, work_entry);
895 1.29 riastrad cv_broadcast(&wq->wq_cv);
896 1.29 riastrad } else {
897 1.29 riastrad /*
898 1.29 riastrad * Reschedule the callout. No
899 1.29 riastrad * state change.
900 1.12 riastrad */
901 1.12 riastrad callout_schedule(&dw->dw_callout,
902 1.12 riastrad MIN(INT_MAX, ticks));
903 1.12 riastrad }
904 1.12 riastrad }
905 1.12 riastrad timer_modified = true;
906 1.12 riastrad break;
907 1.12 riastrad case DELAYED_WORK_RESCHEDULED:
908 1.12 riastrad case DELAYED_WORK_CANCELLED:
909 1.29 riastrad /*
910 1.12 riastrad * Someone modified the timer _again_, or
911 1.29 riastrad * cancelled it, after the callout started but
912 1.29 riastrad * before the poor thing even had a chance to
913 1.29 riastrad * acquire the lock.
914 1.29 riastrad */
915 1.29 riastrad if (ticks == 0) {
916 1.29 riastrad /*
917 1.29 riastrad * We can just switch back to
918 1.29 riastrad * DELAYED_WORK_SCHEDULED so that the
919 1.29 riastrad * callout will queue the work as soon
920 1.29 riastrad * as it gets the lock.
921 1.29 riastrad */
922 1.29 riastrad dw->dw_state = DELAYED_WORK_SCHEDULED;
923 1.29 riastrad } else {
924 1.29 riastrad /* Reschedule it. */
925 1.12 riastrad callout_schedule(&dw->dw_callout,
926 1.12 riastrad MIN(INT_MAX, ticks));
927 1.12 riastrad dw->dw_state = DELAYED_WORK_RESCHEDULED;
928 1.29 riastrad }
929 1.1 skrll timer_modified = true;
930 1.1 skrll break;
931 1.12 riastrad default:
932 1.1 skrll panic("invalid delayed work state: %d", dw->dw_state);
933 1.1 skrll }
934 1.1 skrll }
935 1.1 skrll mutex_exit(&wq->wq_lock);
936 1.1 skrll
937 1.1 skrll return timer_modified;
938 1.1 skrll }
939 1.12 riastrad
940 1.12 riastrad bool
941 1.1 skrll cancel_delayed_work(struct delayed_work *dw)
942 1.14 riastrad {
943 1.14 riastrad struct workqueue_struct *wq;
944 1.14 riastrad bool cancelled_p;
945 1.14 riastrad
946 1.12 riastrad /* If there's no workqueue, nothing to cancel. */
947 1.12 riastrad if ((wq = dw->work.work_queue) == NULL)
948 1.12 riastrad return false;
949 1.12 riastrad
950 1.12 riastrad mutex_enter(&wq->wq_lock);
951 1.12 riastrad if (__predict_false(dw->work.work_queue != wq)) {
952 1.12 riastrad cancelled_p = false;
953 1.29 riastrad } else {
954 1.29 riastrad switch (dw->dw_state) {
955 1.29 riastrad case DELAYED_WORK_IDLE:
956 1.29 riastrad if (wq->wq_current_work == &dw->work) {
957 1.29 riastrad /*
958 1.12 riastrad * Too late, it's already running. If
959 1.12 riastrad * it's been requeued, tough -- it'll
960 1.12 riastrad * run again.
961 1.12 riastrad */
962 1.12 riastrad cancelled_p = false;
963 1.12 riastrad } else {
964 1.12 riastrad /* Got in before it started. Remove it. */
965 1.12 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
966 1.12 riastrad work_entry);
967 1.21 riastrad cancelled_p = true;
968 1.21 riastrad }
969 1.21 riastrad break;
970 1.21 riastrad case DELAYED_WORK_SCHEDULED:
971 1.21 riastrad /*
972 1.21 riastrad * If it is scheduled, mark it cancelled and
973 1.21 riastrad * try to stop the callout before it starts.
974 1.21 riastrad *
975 1.21 riastrad * If it's too late and the callout has already
976 1.21 riastrad * begun to execute, tough.
977 1.21 riastrad *
978 1.21 riastrad * If we stopped the callout before it started,
979 1.12 riastrad * however, then destroy the callout and
980 1.27 riastrad * dissociate it from the workqueue ourselves.
981 1.27 riastrad */
982 1.16 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
983 1.34 riastrad cancelled_p = true;
984 1.34 riastrad if (!callout_stop(&dw->dw_callout))
985 1.34 riastrad cancel_delayed_work_done(wq, dw);
986 1.34 riastrad break;
987 1.34 riastrad case DELAYED_WORK_RESCHEDULED:
988 1.34 riastrad /*
989 1.34 riastrad * If it is being rescheduled, the callout has
990 1.34 riastrad * already fired. We must ask it to cancel.
991 1.34 riastrad */
992 1.34 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
993 1.34 riastrad cancelled_p = true;
994 1.34 riastrad break;
995 1.34 riastrad case DELAYED_WORK_CANCELLED:
996 1.34 riastrad /*
997 1.34 riastrad * If it is being cancelled, the callout has
998 1.34 riastrad * already fired. There is nothing more for us
999 1.34 riastrad * to do. Someone else claims credit for
1000 1.12 riastrad * cancelling it.
1001 1.12 riastrad */
1002 1.12 riastrad cancelled_p = false;
1003 1.12 riastrad break;
1004 1.1 skrll default:
1005 1.12 riastrad panic("invalid delayed work state: %d",
1006 1.1 skrll dw->dw_state);
1007 1.1 skrll }
1008 1.1 skrll }
1009 1.1 skrll mutex_exit(&wq->wq_lock);
1010 1.1 skrll
1011 1.1 skrll return cancelled_p;
1012 1.1 skrll }
1013 1.12 riastrad
1014 1.24 riastrad bool
1015 1.1 skrll cancel_delayed_work_sync(struct delayed_work *dw)
1016 1.24 riastrad {
1017 1.14 riastrad struct workqueue_struct *wq;
1018 1.24 riastrad bool cancelled_p;
1019 1.14 riastrad
1020 1.12 riastrad /* If there's no workqueue, nothing to cancel. */
1021 1.12 riastrad if ((wq = dw->work.work_queue) == NULL)
1022 1.12 riastrad return false;
1023 1.12 riastrad
1024 1.20 riastrad mutex_enter(&wq->wq_lock);
1025 1.12 riastrad if (__predict_false(dw->work.work_queue != wq)) {
1026 1.12 riastrad cancelled_p = false;
1027 1.29 riastrad } else {
1028 1.29 riastrad switch (dw->dw_state) {
1029 1.29 riastrad case DELAYED_WORK_IDLE:
1030 1.33 riastrad if (wq->wq_current_work == &dw->work) {
1031 1.29 riastrad /*
1032 1.29 riastrad * Too late, it's already running.
1033 1.29 riastrad * First, make sure it's not requeued.
1034 1.29 riastrad * Then wait for it to complete.
1035 1.29 riastrad */
1036 1.29 riastrad if (wq->wq_requeued) {
1037 1.33 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
1038 1.12 riastrad work_entry);
1039 1.12 riastrad wq->wq_requeued = false;
1040 1.12 riastrad }
1041 1.12 riastrad wait_for_current_work(&dw->work, wq);
1042 1.12 riastrad cancelled_p = false;
1043 1.12 riastrad } else {
1044 1.12 riastrad /* Got in before it started. Remove it. */
1045 1.12 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
1046 1.12 riastrad work_entry);
1047 1.12 riastrad cancelled_p = true;
1048 1.20 riastrad }
1049 1.20 riastrad break;
1050 1.20 riastrad case DELAYED_WORK_SCHEDULED:
1051 1.20 riastrad /*
1052 1.20 riastrad * If it is scheduled, mark it cancelled and
1053 1.24 riastrad * try to stop the callout before it starts.
1054 1.24 riastrad *
1055 1.24 riastrad * If it's too late and the callout has already
1056 1.20 riastrad * begun to execute, we must wait for it to
1057 1.20 riastrad * complete. But we got in soon enough to ask
1058 1.20 riastrad * the callout not to run, so we successfully
1059 1.20 riastrad * cancelled it in that case.
1060 1.12 riastrad *
1061 1.12 riastrad * If we stopped the callout before it started,
1062 1.27 riastrad * however, then destroy the callout and
1063 1.27 riastrad * dissociate it from the workqueue ourselves.
1064 1.34 riastrad */
1065 1.34 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
1066 1.34 riastrad if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
1067 1.34 riastrad cancel_delayed_work_done(wq, dw);
1068 1.34 riastrad cancelled_p = true;
1069 1.34 riastrad break;
1070 1.34 riastrad case DELAYED_WORK_RESCHEDULED:
1071 1.34 riastrad /*
1072 1.34 riastrad * If it is being rescheduled, the callout has
1073 1.34 riastrad * already fired. We must ask it to cancel and
1074 1.34 riastrad * wait for it to complete.
1075 1.34 riastrad */
1076 1.34 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
1077 1.34 riastrad (void)callout_halt(&dw->dw_callout, &wq->wq_lock);
1078 1.34 riastrad cancelled_p = true;
1079 1.34 riastrad break;
1080 1.34 riastrad case DELAYED_WORK_CANCELLED:
1081 1.34 riastrad /*
1082 1.34 riastrad * If it is being cancelled, the callout has
1083 1.34 riastrad * already fired. We need only wait for it to
1084 1.34 riastrad * complete. Someone else, however, claims
1085 1.20 riastrad * credit for cancelling it.
1086 1.12 riastrad */
1087 1.12 riastrad (void)callout_halt(&dw->dw_callout, &wq->wq_lock);
1088 1.12 riastrad cancelled_p = false;
1089 1.12 riastrad break;
1090 1.1 skrll default:
1091 1.12 riastrad panic("invalid delayed work state: %d",
1092 1.1 skrll dw->dw_state);
1093 1.1 skrll }
1094 1.1 skrll }
1095 1.12 riastrad mutex_exit(&wq->wq_lock);
1096 1.12 riastrad
1097 1.12 riastrad return cancelled_p;
1098 1.12 riastrad }
1099 1.1 skrll
1100 1.5 riastrad /*
1102 1.5 riastrad * Flush
1103 1.5 riastrad */
1104 1.12 riastrad
1105 1.5 riastrad void
1106 1.5 riastrad flush_scheduled_work(void)
1107 1.28 riastrad {
1108 1.28 riastrad
1109 1.28 riastrad flush_workqueue(system_wq);
1110 1.28 riastrad }
1111 1.28 riastrad
1112 1.28 riastrad static void
1113 1.28 riastrad flush_workqueue_locked(struct workqueue_struct *wq)
1114 1.28 riastrad {
1115 1.28 riastrad uint64_t gen;
1116 1.28 riastrad
1117 1.28 riastrad KASSERT(mutex_owned(&wq->wq_lock));
1118 1.28 riastrad
1119 1.28 riastrad /* Get the current generation number. */
1120 1.28 riastrad gen = wq->wq_gen;
1121 1.28 riastrad
1122 1.28 riastrad /*
1123 1.28 riastrad * If there's a batch of work in progress, we must wait for the
1124 1.28 riastrad * worker thread to finish that batch.
1125 1.28 riastrad */
1126 1.28 riastrad if (wq->wq_current_work != NULL)
1127 1.28 riastrad gen++;
1128 1.28 riastrad
1129 1.28 riastrad /*
1130 1.28 riastrad * If there's any work yet to be claimed from the queue by the
1131 1.28 riastrad * worker thread, we must wait for it to finish one more batch
1132 1.28 riastrad * too.
1133 1.28 riastrad */
1134 1.28 riastrad if (!TAILQ_EMPTY(&wq->wq_queue))
1135 1.28 riastrad gen++;
1136 1.28 riastrad
1137 1.12 riastrad /* Wait until the generation number has caught up. */
1138 1.12 riastrad while (wq->wq_gen < gen)
1139 1.1 skrll cv_wait(&wq->wq_cv, &wq->wq_lock);
1140 1.1 skrll }
1141 1.12 riastrad
1142 1.28 riastrad void
1143 1.12 riastrad flush_workqueue(struct workqueue_struct *wq)
1144 1.1 skrll {
1145 1.1 skrll
1146 1.28 riastrad mutex_enter(&wq->wq_lock);
1147 1.12 riastrad flush_workqueue_locked(wq);
1148 1.1 skrll mutex_exit(&wq->wq_lock);
1149 1.14 riastrad }
1150 1.1 skrll
1151 1.14 riastrad void
1152 1.14 riastrad flush_work(struct work_struct *work)
1153 1.28 riastrad {
1154 1.1 skrll struct workqueue_struct *wq;
1155 1.12 riastrad
1156 1.1 skrll /* If there's no workqueue, nothing to flush. */
1157 1.1 skrll if ((wq = work->work_queue) == NULL)
1158 1.28 riastrad return;
1159 1.12 riastrad
1160 1.1 skrll flush_workqueue(wq);
1161 1.14 riastrad }
1162 1.1 skrll
1163 1.14 riastrad void
1164 1.14 riastrad flush_delayed_work(struct delayed_work *dw)
1165 1.28 riastrad {
1166 1.1 skrll struct workqueue_struct *wq;
1167 1.1 skrll
1168 1.28 riastrad /* If there's no workqueue, nothing to flush. */
1169 1.28 riastrad if ((wq = dw->work.work_queue) == NULL)
1170 1.12 riastrad return;
1171 1.28 riastrad
1172 1.28 riastrad mutex_enter(&wq->wq_lock);
1173 1.28 riastrad if (__predict_true(dw->work.work_queue == wq)) {
1174 1.28 riastrad switch (dw->dw_state) {
1175 1.28 riastrad case DELAYED_WORK_IDLE:
1176 1.28 riastrad /*
1177 1.28 riastrad * It has a workqueue assigned and the callout
1178 1.28 riastrad * is idle, so it must be in progress or on the
1179 1.12 riastrad * queue. In that case, wait for it to
1180 1.12 riastrad * complete. Waiting for the whole queue to
1181 1.12 riastrad * flush is overkill, but doesn't hurt.
1182 1.12 riastrad */
1183 1.28 riastrad flush_workqueue_locked(wq);
1184 1.28 riastrad break;
1185 1.28 riastrad case DELAYED_WORK_SCHEDULED:
1186 1.28 riastrad case DELAYED_WORK_RESCHEDULED:
1187 1.28 riastrad case DELAYED_WORK_CANCELLED:
1188 1.28 riastrad /*
1189 1.28 riastrad * The callout is still scheduled to run.
1190 1.28 riastrad * Notify it that we are cancelling, and try to
1191 1.28 riastrad * stop the callout before it runs.
1192 1.28 riastrad *
1193 1.28 riastrad * If we do stop the callout, we are now
1194 1.28 riastrad * responsible for dissociating the work from
1195 1.28 riastrad * the queue.
1196 1.12 riastrad *
1197 1.28 riastrad * Otherwise, wait for it to complete and
1198 1.28 riastrad * dissociate itself -- it will not put itself
1199 1.12 riastrad * on the workqueue once it is cancelled.
1200 1.12 riastrad */
1201 1.12 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
1202 1.12 riastrad if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
1203 1.1 skrll cancel_delayed_work_done(wq, dw);
1204 1.12 riastrad default:
1205 1.1 skrll panic("invalid delayed work state: %d",
1206 dw->dw_state);
1207 }
1208 }
1209 mutex_exit(&wq->wq_lock);
1210 }
1211