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