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