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