linux_work.c revision 1.40 1 /* $NetBSD: linux_work.c,v 1.40 2018/08/27 15:06:20 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.40 2018/08/27 15:06:20 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 if (ticks == 0) {
877 dw->dw_state = DELAYED_WORK_SCHEDULED;
878 } else {
879 dw->dw_state = DELAYED_WORK_RESCHEDULED;
880 dw->dw_resched = MIN(INT_MAX, ticks);
881 }
882 newly_queued = true;
883 break;
884 default:
885 panic("invalid delayed work state: %d",
886 dw->dw_state);
887 }
888 }
889 mutex_exit(&wq->wq_lock);
890
891 return newly_queued;
892 }
893
894 /*
895 * mod_delayed_work(wq, dw, ticks)
896 *
897 * Schedule dw to run after ticks. If scheduled or queued,
898 * reschedule. If ticks == 0, run without delay.
899 *
900 * True if it modified the timer of an already scheduled work,
901 * false if it newly scheduled the work.
902 */
903 bool
904 mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
905 unsigned long ticks)
906 {
907 bool timer_modified;
908
909 mutex_enter(&wq->wq_lock);
910 if (acquire_work(&dw->work, wq)) {
911 /*
912 * It wasn't on any workqueue at all. Schedule it to
913 * run on this one.
914 */
915 KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
916 if (ticks == 0) {
917 /*
918 * Run immediately: put it on the queue and
919 * signal the worker thread.
920 */
921 TAILQ_INSERT_TAIL(&wq->wq_dqueue, &dw->work,
922 work_entry);
923 cv_broadcast(&wq->wq_cv);
924 } else {
925 /*
926 * Initialize a callout and schedule to run
927 * after a delay.
928 */
929 dw_callout_init(wq, dw);
930 callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
931 }
932 timer_modified = false;
933 } else {
934 /* It was already on this workqueue. */
935 switch (dw->dw_state) {
936 case DELAYED_WORK_IDLE:
937 /* On the queue. */
938 if (ticks == 0) {
939 /* Leave it be. */
940 } else {
941 /* Remove from the queue and schedule. */
942 TAILQ_REMOVE(&wq->wq_dqueue, &dw->work,
943 work_entry);
944 dw_callout_init(wq, dw);
945 callout_schedule(&dw->dw_callout,
946 MIN(INT_MAX, ticks));
947 }
948 timer_modified = true;
949 break;
950 case DELAYED_WORK_SCHEDULED:
951 /*
952 * It is scheduled to run after a delay. Try
953 * to stop it and reschedule it; if we can't,
954 * either reschedule it or cancel it to put it
955 * on the queue, and inform the callout.
956 */
957 if (callout_stop(&dw->dw_callout)) {
958 /* Can't stop, callout has begun. */
959 if (ticks == 0) {
960 /*
961 * We don't actually need to do
962 * anything. The callout will
963 * queue it as soon as it gets
964 * the lock.
965 */
966 } else {
967 /* Ask the callout to reschedule. */
968 dw->dw_state = DELAYED_WORK_RESCHEDULED;
969 dw->dw_resched = MIN(INT_MAX, ticks);
970 }
971 } else {
972 /* We stopped the callout before it began. */
973 if (ticks == 0) {
974 /*
975 * Run immediately: destroy the
976 * callout, put it on the
977 * queue, and signal the worker
978 * thread.
979 */
980 dw_callout_destroy(wq, dw);
981 TAILQ_INSERT_TAIL(&wq->wq_dqueue,
982 &dw->work, work_entry);
983 cv_broadcast(&wq->wq_cv);
984 } else {
985 /*
986 * Reschedule the callout. No
987 * state change.
988 */
989 callout_schedule(&dw->dw_callout,
990 MIN(INT_MAX, ticks));
991 }
992 }
993 timer_modified = true;
994 break;
995 case DELAYED_WORK_RESCHEDULED:
996 /*
997 * Someone rescheduled it after the callout
998 * started but before the poor thing even had a
999 * chance to acquire the lock.
1000 */
1001 if (ticks == 0) {
1002 /*
1003 * We can just switch back to
1004 * DELAYED_WORK_SCHEDULED so that the
1005 * callout will queue the work as soon
1006 * as it gets the lock.
1007 */
1008 dw->dw_state = DELAYED_WORK_SCHEDULED;
1009 dw->dw_resched = -1;
1010 } else {
1011 /* Change the rescheduled time. */
1012 dw->dw_resched = ticks;
1013 }
1014 timer_modified = true;
1015 break;
1016 case DELAYED_WORK_CANCELLED:
1017 /*
1018 * Someone cancelled it after the callout
1019 * started but before the poor thing even had a
1020 * chance to acquire the lock.
1021 */
1022 if (ticks == 0) {
1023 /*
1024 * We can just switch back to
1025 * DELAYED_WORK_SCHEDULED so that the
1026 * callout will queue the work as soon
1027 * as it gets the lock.
1028 */
1029 dw->dw_state = DELAYED_WORK_SCHEDULED;
1030 } else {
1031 /* Ask it to reschedule. */
1032 dw->dw_state = DELAYED_WORK_RESCHEDULED;
1033 dw->dw_resched = MIN(INT_MAX, ticks);
1034 }
1035 timer_modified = false;
1036 break;
1037 default:
1038 panic("invalid delayed work state: %d", dw->dw_state);
1039 }
1040 }
1041 mutex_exit(&wq->wq_lock);
1042
1043 return timer_modified;
1044 }
1045
1046 /*
1047 * cancel_delayed_work(dw)
1048 *
1049 * If work was scheduled or queued, remove it from the schedule or
1050 * queue and return true. If work was not scheduled or queued,
1051 * return false. Note that work may already be running; if it
1052 * hasn't been rescheduled or requeued, then cancel_delayed_work
1053 * will return false, and either way, cancel_delayed_work will NOT
1054 * wait for the work to complete.
1055 */
1056 bool
1057 cancel_delayed_work(struct delayed_work *dw)
1058 {
1059 struct workqueue_struct *wq;
1060 bool cancelled_p;
1061
1062 /* If there's no workqueue, nothing to cancel. */
1063 if ((wq = work_queue(&dw->work)) == NULL)
1064 return false;
1065
1066 mutex_enter(&wq->wq_lock);
1067 if (__predict_false(work_queue(&dw->work) != wq)) {
1068 cancelled_p = false;
1069 } else {
1070 switch (dw->dw_state) {
1071 case DELAYED_WORK_IDLE:
1072 /*
1073 * It is either on the queue or already running
1074 * or both.
1075 */
1076 if (work_claimed(&dw->work, wq)) {
1077 /* On the queue. Remove and release. */
1078 TAILQ_REMOVE(&wq->wq_dqueue, &dw->work,
1079 work_entry);
1080 release_work(&dw->work, wq);
1081 /* Can't dereference dw after this point. */
1082 cancelled_p = true;
1083 } else {
1084 /* Not on the queue, so didn't cancel. */
1085 cancelled_p = false;
1086 }
1087 break;
1088 case DELAYED_WORK_SCHEDULED:
1089 /*
1090 * If it is scheduled, mark it cancelled and
1091 * try to stop the callout before it starts.
1092 *
1093 * If it's too late and the callout has already
1094 * begun to execute, tough.
1095 *
1096 * If we stopped the callout before it started,
1097 * however, then destroy the callout and
1098 * dissociate it from the workqueue ourselves.
1099 */
1100 dw->dw_state = DELAYED_WORK_CANCELLED;
1101 cancelled_p = true;
1102 if (!callout_stop(&dw->dw_callout))
1103 cancel_delayed_work_done(wq, dw);
1104 break;
1105 case DELAYED_WORK_RESCHEDULED:
1106 /*
1107 * If it is being rescheduled, the callout has
1108 * already fired. We must ask it to cancel.
1109 */
1110 dw->dw_state = DELAYED_WORK_CANCELLED;
1111 dw->dw_resched = -1;
1112 cancelled_p = true;
1113 break;
1114 case DELAYED_WORK_CANCELLED:
1115 /*
1116 * If it is being cancelled, the callout has
1117 * already fired. There is nothing more for us
1118 * to do. Someone else claims credit for
1119 * cancelling it.
1120 */
1121 cancelled_p = false;
1122 break;
1123 default:
1124 panic("invalid delayed work state: %d",
1125 dw->dw_state);
1126 }
1127 }
1128 mutex_exit(&wq->wq_lock);
1129
1130 return cancelled_p;
1131 }
1132
1133 /*
1134 * cancel_delayed_work_sync(dw)
1135 *
1136 * If work was scheduled or queued, remove it from the schedule or
1137 * queue and return true. If work was not scheduled or queued,
1138 * return false. Note that work may already be running; if it
1139 * hasn't been rescheduled or requeued, then cancel_delayed_work
1140 * will return false; either way, wait for it to complete.
1141 */
1142 bool
1143 cancel_delayed_work_sync(struct delayed_work *dw)
1144 {
1145 struct workqueue_struct *wq;
1146 bool cancelled_p;
1147
1148 /* If there's no workqueue, nothing to cancel. */
1149 if ((wq = work_queue(&dw->work)) == NULL)
1150 return false;
1151
1152 mutex_enter(&wq->wq_lock);
1153 if (__predict_false(work_queue(&dw->work) != wq)) {
1154 cancelled_p = false;
1155 } else {
1156 switch (dw->dw_state) {
1157 case DELAYED_WORK_IDLE:
1158 /*
1159 * It is either on the queue or already running
1160 * or both.
1161 */
1162 if (work_claimed(&dw->work, wq)) {
1163 /* On the queue. Remove and release. */
1164 TAILQ_REMOVE(&wq->wq_dqueue, &dw->work,
1165 work_entry);
1166 release_work(&dw->work, wq);
1167 /* Can't dereference dw after this point. */
1168 cancelled_p = true;
1169 } else {
1170 /* Not on the queue, so didn't cancel. */
1171 cancelled_p = false;
1172 }
1173 /* If it's still running, wait for it to complete. */
1174 if (wq->wq_current_work == &dw->work)
1175 wait_for_current_work(&dw->work, wq);
1176 break;
1177 case DELAYED_WORK_SCHEDULED:
1178 /*
1179 * If it is scheduled, mark it cancelled and
1180 * try to stop the callout before it starts.
1181 *
1182 * If it's too late and the callout has already
1183 * begun to execute, we must wait for it to
1184 * complete. But we got in soon enough to ask
1185 * the callout not to run, so we successfully
1186 * cancelled it in that case.
1187 *
1188 * If we stopped the callout before it started,
1189 * then we must destroy the callout and
1190 * dissociate it from the workqueue ourselves.
1191 */
1192 dw->dw_state = DELAYED_WORK_CANCELLED;
1193 if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
1194 cancel_delayed_work_done(wq, dw);
1195 cancelled_p = true;
1196 break;
1197 case DELAYED_WORK_RESCHEDULED:
1198 /*
1199 * If it is being rescheduled, the callout has
1200 * already fired. We must ask it to cancel and
1201 * wait for it to complete.
1202 */
1203 dw->dw_state = DELAYED_WORK_CANCELLED;
1204 dw->dw_resched = -1;
1205 (void)callout_halt(&dw->dw_callout, &wq->wq_lock);
1206 cancelled_p = true;
1207 break;
1208 case DELAYED_WORK_CANCELLED:
1209 /*
1210 * If it is being cancelled, the callout has
1211 * already fired. We need only wait for it to
1212 * complete. Someone else, however, claims
1213 * credit for cancelling it.
1214 */
1215 (void)callout_halt(&dw->dw_callout, &wq->wq_lock);
1216 cancelled_p = false;
1217 break;
1218 default:
1219 panic("invalid delayed work state: %d",
1220 dw->dw_state);
1221 }
1222 }
1223 mutex_exit(&wq->wq_lock);
1224
1225 return cancelled_p;
1226 }
1227
1228 /*
1230 * Flush
1231 */
1232
1233 /*
1234 * flush_scheduled_work()
1235 *
1236 * Wait for all work queued on system_wq to complete. This does
1237 * not include delayed work.
1238 */
1239 void
1240 flush_scheduled_work(void)
1241 {
1242
1243 flush_workqueue(system_wq);
1244 }
1245
1246 /*
1247 * flush_workqueue_locked(wq)
1248 *
1249 * Wait for all work queued on wq to complete. This does not
1250 * include delayed work.
1251 *
1252 * Caller must hold wq's lock.
1253 */
1254 static void
1255 flush_workqueue_locked(struct workqueue_struct *wq)
1256 {
1257 uint64_t gen;
1258
1259 KASSERT(mutex_owned(&wq->wq_lock));
1260
1261 /* Get the current generation number. */
1262 gen = wq->wq_gen;
1263
1264 /*
1265 * If there's a batch of work in progress, we must wait for the
1266 * worker thread to finish that batch.
1267 */
1268 if (wq->wq_current_work != NULL)
1269 gen++;
1270
1271 /*
1272 * If there's any work yet to be claimed from the queue by the
1273 * worker thread, we must wait for it to finish one more batch
1274 * too.
1275 */
1276 if (!TAILQ_EMPTY(&wq->wq_queue) || !TAILQ_EMPTY(&wq->wq_dqueue))
1277 gen++;
1278
1279 /* Wait until the generation number has caught up. */
1280 while (wq->wq_gen < gen)
1281 cv_wait(&wq->wq_cv, &wq->wq_lock);
1282 }
1283
1284 /*
1285 * flush_workqueue(wq)
1286 *
1287 * Wait for all work queued on wq to complete. This does not
1288 * include delayed work.
1289 */
1290 void
1291 flush_workqueue(struct workqueue_struct *wq)
1292 {
1293
1294 mutex_enter(&wq->wq_lock);
1295 flush_workqueue_locked(wq);
1296 mutex_exit(&wq->wq_lock);
1297 }
1298
1299 /*
1300 * flush_work(work)
1301 *
1302 * If work is queued or currently executing, wait for it to
1303 * complete.
1304 */
1305 void
1306 flush_work(struct work_struct *work)
1307 {
1308 struct workqueue_struct *wq;
1309
1310 /* If there's no workqueue, nothing to flush. */
1311 if ((wq = work_queue(work)) == NULL)
1312 return;
1313
1314 flush_workqueue(wq);
1315 }
1316
1317 /*
1318 * flush_delayed_work(dw)
1319 *
1320 * If dw is scheduled to run after a delay, queue it immediately
1321 * instead. Then, if dw is queued or currently executing, wait
1322 * for it to complete.
1323 */
1324 void
1325 flush_delayed_work(struct delayed_work *dw)
1326 {
1327 struct workqueue_struct *wq;
1328
1329 /* If there's no workqueue, nothing to flush. */
1330 if ((wq = work_queue(&dw->work)) == NULL)
1331 return;
1332
1333 mutex_enter(&wq->wq_lock);
1334 if (__predict_false(work_queue(&dw->work) != wq)) {
1335 /*
1336 * Moved off the queue already (and possibly to another
1337 * queue, though that would be ill-advised), so it must
1338 * have completed, and we have nothing more to do.
1339 */
1340 } else {
1341 switch (dw->dw_state) {
1342 case DELAYED_WORK_IDLE:
1343 /*
1344 * It has a workqueue assigned and the callout
1345 * is idle, so it must be in progress or on the
1346 * queue. In that case, we'll wait for it to
1347 * complete.
1348 */
1349 break;
1350 case DELAYED_WORK_SCHEDULED:
1351 case DELAYED_WORK_RESCHEDULED:
1352 case DELAYED_WORK_CANCELLED:
1353 /*
1354 * The callout is scheduled, and may have even
1355 * started. Mark it as scheduled so that if
1356 * the callout has fired it will queue the work
1357 * itself. Try to stop the callout -- if we
1358 * can, queue the work now; if we can't, wait
1359 * for the callout to complete, which entails
1360 * queueing it.
1361 */
1362 dw->dw_state = DELAYED_WORK_SCHEDULED;
1363 if (!callout_halt(&dw->dw_callout, &wq->wq_lock)) {
1364 /*
1365 * We stopped it before it ran. No
1366 * state change in the interim is
1367 * possible. Destroy the callout and
1368 * queue it ourselves.
1369 */
1370 KASSERT(dw->dw_state ==
1371 DELAYED_WORK_SCHEDULED);
1372 dw_callout_destroy(wq, dw);
1373 TAILQ_INSERT_TAIL(&wq->wq_dqueue, &dw->work,
1374 work_entry);
1375 cv_broadcast(&wq->wq_cv);
1376 }
1377 break;
1378 default:
1379 panic("invalid delayed work state: %d", dw->dw_state);
1380 }
1381 /*
1382 * Waiting for the whole queue to flush is overkill,
1383 * but doesn't hurt.
1384 */
1385 flush_workqueue_locked(wq);
1386 }
1387 mutex_exit(&wq->wq_lock);
1388 }
1389