1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/export.h>
27#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
37#include <linux/hardirq.h>
38#include <linux/mempolicy.h>
39#include <linux/freezer.h>
40#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
42#include <linux/lockdep.h>
43#include <linux/idr.h>
44
45#include "workqueue_sched.h"
46
47enum {
48
49 GCWQ_MANAGE_WORKERS = 1 << 0,
50 GCWQ_MANAGING_WORKERS = 1 << 1,
51 GCWQ_DISASSOCIATED = 1 << 2,
52 GCWQ_FREEZING = 1 << 3,
53 GCWQ_HIGHPRI_PENDING = 1 << 4,
54
55
56 WORKER_STARTED = 1 << 0,
57 WORKER_DIE = 1 << 1,
58 WORKER_IDLE = 1 << 2,
59 WORKER_PREP = 1 << 3,
60 WORKER_ROGUE = 1 << 4,
61 WORKER_REBIND = 1 << 5,
62 WORKER_CPU_INTENSIVE = 1 << 6,
63 WORKER_UNBOUND = 1 << 7,
64
65 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
66 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
67
68
69 TRUSTEE_START = 0,
70 TRUSTEE_IN_CHARGE = 1,
71 TRUSTEE_BUTCHER = 2,
72 TRUSTEE_RELEASE = 3,
73 TRUSTEE_DONE = 4,
74
75 BUSY_WORKER_HASH_ORDER = 6,
76 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
77 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
78
79 MAX_IDLE_WORKERS_RATIO = 4,
80 IDLE_WORKER_TIMEOUT = 300 * HZ,
81
82 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
83
84
85 MAYDAY_INTERVAL = HZ / 10,
86 CREATE_COOLDOWN = HZ,
87 TRUSTEE_COOLDOWN = HZ / 10,
88
89
90
91
92
93 RESCUER_NICE_LEVEL = -20,
94};
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117struct global_cwq;
118
119
120
121
122
123struct worker {
124
125 union {
126 struct list_head entry;
127 struct hlist_node hentry;
128 };
129
130 struct work_struct *current_work;
131 struct cpu_workqueue_struct *current_cwq;
132 struct list_head scheduled;
133 struct task_struct *task;
134 struct global_cwq *gcwq;
135
136 unsigned long last_active;
137 unsigned int flags;
138 int id;
139 struct work_struct rebind_work;
140};
141
142
143
144
145
146
147struct global_cwq {
148 spinlock_t lock;
149 struct list_head worklist;
150 unsigned int cpu;
151 unsigned int flags;
152
153 int nr_workers;
154 int nr_idle;
155
156
157 struct list_head idle_list;
158 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
159
160
161 struct timer_list idle_timer;
162 struct timer_list mayday_timer;
163
164 struct ida worker_ida;
165
166 struct task_struct *trustee;
167 unsigned int trustee_state;
168 wait_queue_head_t trustee_wait;
169 struct worker *first_idle;
170} ____cacheline_aligned_in_smp;
171
172
173
174
175
176
177struct cpu_workqueue_struct {
178 struct global_cwq *gcwq;
179 struct workqueue_struct *wq;
180 int work_color;
181 int flush_color;
182 int nr_in_flight[WORK_NR_COLORS];
183
184 int nr_active;
185 int max_active;
186 struct list_head delayed_works;
187};
188
189
190
191
192struct wq_flusher {
193 struct list_head list;
194 int flush_color;
195 struct completion done;
196};
197
198
199
200
201
202#ifdef CONFIG_SMP
203typedef cpumask_var_t mayday_mask_t;
204#define mayday_test_and_set_cpu(cpu, mask) \
205 cpumask_test_and_set_cpu((cpu), (mask))
206#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
207#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
208#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
209#define free_mayday_mask(mask) free_cpumask_var((mask))
210#else
211typedef unsigned long mayday_mask_t;
212#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
213#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
214#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
215#define alloc_mayday_mask(maskp, gfp) true
216#define free_mayday_mask(mask) do { } while (0)
217#endif
218
219
220
221
222
223struct workqueue_struct {
224 unsigned int flags;
225 union {
226 struct cpu_workqueue_struct __percpu *pcpu;
227 struct cpu_workqueue_struct *single;
228 unsigned long v;
229 } cpu_wq;
230 struct list_head list;
231
232 struct mutex flush_mutex;
233 int work_color;
234 int flush_color;
235 atomic_t nr_cwqs_to_flush;
236 struct wq_flusher *first_flusher;
237 struct list_head flusher_queue;
238 struct list_head flusher_overflow;
239
240 mayday_mask_t mayday_mask;
241 struct worker *rescuer;
242
243 int nr_drainers;
244 int saved_max_active;
245#ifdef CONFIG_LOCKDEP
246 struct lockdep_map lockdep_map;
247#endif
248 char name[];
249};
250
251struct workqueue_struct *system_wq __read_mostly;
252struct workqueue_struct *system_long_wq __read_mostly;
253struct workqueue_struct *system_nrt_wq __read_mostly;
254struct workqueue_struct *system_unbound_wq __read_mostly;
255struct workqueue_struct *system_freezable_wq __read_mostly;
256struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
257EXPORT_SYMBOL_GPL(system_wq);
258EXPORT_SYMBOL_GPL(system_long_wq);
259EXPORT_SYMBOL_GPL(system_nrt_wq);
260EXPORT_SYMBOL_GPL(system_unbound_wq);
261EXPORT_SYMBOL_GPL(system_freezable_wq);
262EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
263
264#define CREATE_TRACE_POINTS
265#include <trace/events/workqueue.h>
266
267#define for_each_busy_worker(worker, i, pos, gcwq) \
268 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
269 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
270
271static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
272 unsigned int sw)
273{
274 if (cpu < nr_cpu_ids) {
275 if (sw & 1) {
276 cpu = cpumask_next(cpu, mask);
277 if (cpu < nr_cpu_ids)
278 return cpu;
279 }
280 if (sw & 2)
281 return WORK_CPU_UNBOUND;
282 }
283 return WORK_CPU_NONE;
284}
285
286static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
287 struct workqueue_struct *wq)
288{
289 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
290}
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305#define for_each_gcwq_cpu(cpu) \
306 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
307 (cpu) < WORK_CPU_NONE; \
308 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
309
310#define for_each_online_gcwq_cpu(cpu) \
311 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
312 (cpu) < WORK_CPU_NONE; \
313 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
314
315#define for_each_cwq_cpu(cpu, wq) \
316 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
317 (cpu) < WORK_CPU_NONE; \
318 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
319
320#ifdef CONFIG_DEBUG_OBJECTS_WORK
321
322static struct debug_obj_descr work_debug_descr;
323
324static void *work_debug_hint(void *addr)
325{
326 return ((struct work_struct *) addr)->func;
327}
328
329
330
331
332
333static int work_fixup_init(void *addr, enum debug_obj_state state)
334{
335 struct work_struct *work = addr;
336
337 switch (state) {
338 case ODEBUG_STATE_ACTIVE:
339 cancel_work_sync(work);
340 debug_object_init(work, &work_debug_descr);
341 return 1;
342 default:
343 return 0;
344 }
345}
346
347
348
349
350
351
352static int work_fixup_activate(void *addr, enum debug_obj_state state)
353{
354 struct work_struct *work = addr;
355
356 switch (state) {
357
358 case ODEBUG_STATE_NOTAVAILABLE:
359
360
361
362
363
364 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
365 debug_object_init(work, &work_debug_descr);
366 debug_object_activate(work, &work_debug_descr);
367 return 0;
368 }
369 WARN_ON_ONCE(1);
370 return 0;
371
372 case ODEBUG_STATE_ACTIVE:
373 WARN_ON(1);
374
375 default:
376 return 0;
377 }
378}
379
380
381
382
383
384static int work_fixup_free(void *addr, enum debug_obj_state state)
385{
386 struct work_struct *work = addr;
387
388 switch (state) {
389 case ODEBUG_STATE_ACTIVE:
390 cancel_work_sync(work);
391 debug_object_free(work, &work_debug_descr);
392 return 1;
393 default:
394 return 0;
395 }
396}
397
398static struct debug_obj_descr work_debug_descr = {
399 .name = "work_struct",
400 .debug_hint = work_debug_hint,
401 .fixup_init = work_fixup_init,
402 .fixup_activate = work_fixup_activate,
403 .fixup_free = work_fixup_free,
404};
405
406static inline void debug_work_activate(struct work_struct *work)
407{
408 debug_object_activate(work, &work_debug_descr);
409}
410
411static inline void debug_work_deactivate(struct work_struct *work)
412{
413 debug_object_deactivate(work, &work_debug_descr);
414}
415
416void __init_work(struct work_struct *work, int onstack)
417{
418 if (onstack)
419 debug_object_init_on_stack(work, &work_debug_descr);
420 else
421 debug_object_init(work, &work_debug_descr);
422}
423EXPORT_SYMBOL_GPL(__init_work);
424
425void destroy_work_on_stack(struct work_struct *work)
426{
427 debug_object_free(work, &work_debug_descr);
428}
429EXPORT_SYMBOL_GPL(destroy_work_on_stack);
430
431#else
432static inline void debug_work_activate(struct work_struct *work) { }
433static inline void debug_work_deactivate(struct work_struct *work) { }
434#endif
435
436
437static DEFINE_SPINLOCK(workqueue_lock);
438static LIST_HEAD(workqueues);
439static bool workqueue_freezing;
440
441
442
443
444
445
446static DEFINE_PER_CPU(struct global_cwq, global_cwq);
447static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
448
449
450
451
452
453
454static struct global_cwq unbound_global_cwq;
455static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0);
456
457static int worker_thread(void *__worker);
458
459static struct global_cwq *get_gcwq(unsigned int cpu)
460{
461 if (cpu != WORK_CPU_UNBOUND)
462 return &per_cpu(global_cwq, cpu);
463 else
464 return &unbound_global_cwq;
465}
466
467static atomic_t *get_gcwq_nr_running(unsigned int cpu)
468{
469 if (cpu != WORK_CPU_UNBOUND)
470 return &per_cpu(gcwq_nr_running, cpu);
471 else
472 return &unbound_gcwq_nr_running;
473}
474
475static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
476 struct workqueue_struct *wq)
477{
478 if (!(wq->flags & WQ_UNBOUND)) {
479 if (likely(cpu < nr_cpu_ids))
480 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
481 } else if (likely(cpu == WORK_CPU_UNBOUND))
482 return wq->cpu_wq.single;
483 return NULL;
484}
485
486static unsigned int work_color_to_flags(int color)
487{
488 return color << WORK_STRUCT_COLOR_SHIFT;
489}
490
491static int get_work_color(struct work_struct *work)
492{
493 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
494 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
495}
496
497static int work_next_color(int color)
498{
499 return (color + 1) % WORK_NR_COLORS;
500}
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516static inline void set_work_data(struct work_struct *work, unsigned long data,
517 unsigned long flags)
518{
519 BUG_ON(!work_pending(work));
520 atomic_long_set(&work->data, data | flags | work_static(work));
521}
522
523static void set_work_cwq(struct work_struct *work,
524 struct cpu_workqueue_struct *cwq,
525 unsigned long extra_flags)
526{
527 set_work_data(work, (unsigned long)cwq,
528 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
529}
530
531static void set_work_cpu(struct work_struct *work, unsigned int cpu)
532{
533 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
534}
535
536static void clear_work_data(struct work_struct *work)
537{
538 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
539}
540
541static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
542{
543 unsigned long data = atomic_long_read(&work->data);
544
545 if (data & WORK_STRUCT_CWQ)
546 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
547 else
548 return NULL;
549}
550
551static struct global_cwq *get_work_gcwq(struct work_struct *work)
552{
553 unsigned long data = atomic_long_read(&work->data);
554 unsigned int cpu;
555
556 if (data & WORK_STRUCT_CWQ)
557 return ((struct cpu_workqueue_struct *)
558 (data & WORK_STRUCT_WQ_DATA_MASK))->gcwq;
559
560 cpu = data >> WORK_STRUCT_FLAG_BITS;
561 if (cpu == WORK_CPU_NONE)
562 return NULL;
563
564 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
565 return get_gcwq(cpu);
566}
567
568
569
570
571
572
573
574static bool __need_more_worker(struct global_cwq *gcwq)
575{
576 return !atomic_read(get_gcwq_nr_running(gcwq->cpu)) ||
577 gcwq->flags & GCWQ_HIGHPRI_PENDING;
578}
579
580
581
582
583
584static bool need_more_worker(struct global_cwq *gcwq)
585{
586 return !list_empty(&gcwq->worklist) && __need_more_worker(gcwq);
587}
588
589
590static bool may_start_working(struct global_cwq *gcwq)
591{
592 return gcwq->nr_idle;
593}
594
595
596static bool keep_working(struct global_cwq *gcwq)
597{
598 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
599
600 return !list_empty(&gcwq->worklist) &&
601 (atomic_read(nr_running) <= 1 ||
602 gcwq->flags & GCWQ_HIGHPRI_PENDING);
603}
604
605
606static bool need_to_create_worker(struct global_cwq *gcwq)
607{
608 return need_more_worker(gcwq) && !may_start_working(gcwq);
609}
610
611
612static bool need_to_manage_workers(struct global_cwq *gcwq)
613{
614 return need_to_create_worker(gcwq) || gcwq->flags & GCWQ_MANAGE_WORKERS;
615}
616
617
618static bool too_many_workers(struct global_cwq *gcwq)
619{
620 bool managing = gcwq->flags & GCWQ_MANAGING_WORKERS;
621 int nr_idle = gcwq->nr_idle + managing;
622 int nr_busy = gcwq->nr_workers - nr_idle;
623
624 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
625}
626
627
628
629
630
631
632static struct worker *first_worker(struct global_cwq *gcwq)
633{
634 if (unlikely(list_empty(&gcwq->idle_list)))
635 return NULL;
636
637 return list_first_entry(&gcwq->idle_list, struct worker, entry);
638}
639
640
641
642
643
644
645
646
647
648
649static void wake_up_worker(struct global_cwq *gcwq)
650{
651 struct worker *worker = first_worker(gcwq);
652
653 if (likely(worker))
654 wake_up_process(worker->task);
655}
656
657
658
659
660
661
662
663
664
665
666
667
668void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
669{
670 struct worker *worker = kthread_data(task);
671
672 if (!(worker->flags & WORKER_NOT_RUNNING))
673 atomic_inc(get_gcwq_nr_running(cpu));
674}
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691struct task_struct *wq_worker_sleeping(struct task_struct *task,
692 unsigned int cpu)
693{
694 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
695 struct global_cwq *gcwq = get_gcwq(cpu);
696 atomic_t *nr_running = get_gcwq_nr_running(cpu);
697
698 if (worker->flags & WORKER_NOT_RUNNING)
699 return NULL;
700
701
702 BUG_ON(cpu != raw_smp_processor_id());
703
704
705
706
707
708
709
710
711
712
713
714
715 if (atomic_dec_and_test(nr_running) && !list_empty(&gcwq->worklist))
716 to_wakeup = first_worker(gcwq);
717 return to_wakeup ? to_wakeup->task : NULL;
718}
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733static inline void worker_set_flags(struct worker *worker, unsigned int flags,
734 bool wakeup)
735{
736 struct global_cwq *gcwq = worker->gcwq;
737
738 WARN_ON_ONCE(worker->task != current);
739
740
741
742
743
744
745 if ((flags & WORKER_NOT_RUNNING) &&
746 !(worker->flags & WORKER_NOT_RUNNING)) {
747 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
748
749 if (wakeup) {
750 if (atomic_dec_and_test(nr_running) &&
751 !list_empty(&gcwq->worklist))
752 wake_up_worker(gcwq);
753 } else
754 atomic_dec(nr_running);
755 }
756
757 worker->flags |= flags;
758}
759
760
761
762
763
764
765
766
767
768
769
770static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
771{
772 struct global_cwq *gcwq = worker->gcwq;
773 unsigned int oflags = worker->flags;
774
775 WARN_ON_ONCE(worker->task != current);
776
777 worker->flags &= ~flags;
778
779
780
781
782
783
784 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
785 if (!(worker->flags & WORKER_NOT_RUNNING))
786 atomic_inc(get_gcwq_nr_running(gcwq->cpu));
787}
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
803 struct work_struct *work)
804{
805 const int base_shift = ilog2(sizeof(struct work_struct));
806 unsigned long v = (unsigned long)work;
807
808
809 v >>= base_shift;
810 v += v >> BUSY_WORKER_HASH_ORDER;
811 v &= BUSY_WORKER_HASH_MASK;
812
813 return &gcwq->busy_hash[v];
814}
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
834 struct hlist_head *bwh,
835 struct work_struct *work)
836{
837 struct worker *worker;
838 struct hlist_node *tmp;
839
840 hlist_for_each_entry(worker, tmp, bwh, hentry)
841 if (worker->current_work == work)
842 return worker;
843 return NULL;
844}
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
863 struct work_struct *work)
864{
865 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
866 work);
867}
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
888 struct cpu_workqueue_struct *cwq)
889{
890 struct work_struct *twork;
891
892 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
893 return &gcwq->worklist;
894
895 list_for_each_entry(twork, &gcwq->worklist, entry) {
896 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
897
898 if (!(tcwq->wq->flags & WQ_HIGHPRI))
899 break;
900 }
901
902 gcwq->flags |= GCWQ_HIGHPRI_PENDING;
903 return &twork->entry;
904}
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919static void insert_work(struct cpu_workqueue_struct *cwq,
920 struct work_struct *work, struct list_head *head,
921 unsigned int extra_flags)
922{
923 struct global_cwq *gcwq = cwq->gcwq;
924
925
926 set_work_cwq(work, cwq, extra_flags);
927
928
929
930
931
932 smp_wmb();
933
934 list_add_tail(&work->entry, head);
935
936
937
938
939
940
941 smp_mb();
942
943 if (__need_more_worker(gcwq))
944 wake_up_worker(gcwq);
945}
946
947
948
949
950
951
952static bool is_chained_work(struct workqueue_struct *wq)
953{
954 unsigned long flags;
955 unsigned int cpu;
956
957 for_each_gcwq_cpu(cpu) {
958 struct global_cwq *gcwq = get_gcwq(cpu);
959 struct worker *worker;
960 struct hlist_node *pos;
961 int i;
962
963 spin_lock_irqsave(&gcwq->lock, flags);
964 for_each_busy_worker(worker, i, pos, gcwq) {
965 if (worker->task != current)
966 continue;
967 spin_unlock_irqrestore(&gcwq->lock, flags);
968
969
970
971
972 return worker->current_cwq->wq == wq;
973 }
974 spin_unlock_irqrestore(&gcwq->lock, flags);
975 }
976 return false;
977}
978
979static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
980 struct work_struct *work)
981{
982 struct global_cwq *gcwq;
983 struct cpu_workqueue_struct *cwq;
984 struct list_head *worklist;
985 unsigned int work_flags;
986 unsigned long flags;
987
988 debug_work_activate(work);
989
990
991 if (unlikely(wq->flags & WQ_DRAINING) &&
992 WARN_ON_ONCE(!is_chained_work(wq)))
993 return;
994
995
996 if (!(wq->flags & WQ_UNBOUND)) {
997 struct global_cwq *last_gcwq;
998
999 if (unlikely(cpu == WORK_CPU_UNBOUND))
1000 cpu = raw_smp_processor_id();
1001
1002
1003
1004
1005
1006
1007
1008 gcwq = get_gcwq(cpu);
1009 if (wq->flags & WQ_NON_REENTRANT &&
1010 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1011 struct worker *worker;
1012
1013 spin_lock_irqsave(&last_gcwq->lock, flags);
1014
1015 worker = find_worker_executing_work(last_gcwq, work);
1016
1017 if (worker && worker->current_cwq->wq == wq)
1018 gcwq = last_gcwq;
1019 else {
1020
1021 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1022 spin_lock_irqsave(&gcwq->lock, flags);
1023 }
1024 } else
1025 spin_lock_irqsave(&gcwq->lock, flags);
1026 } else {
1027 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1028 spin_lock_irqsave(&gcwq->lock, flags);
1029 }
1030
1031
1032 cwq = get_cwq(gcwq->cpu, wq);
1033 trace_workqueue_queue_work(cpu, cwq, work);
1034
1035 if (WARN_ON(!list_empty(&work->entry))) {
1036 spin_unlock_irqrestore(&gcwq->lock, flags);
1037 return;
1038 }
1039
1040 cwq->nr_in_flight[cwq->work_color]++;
1041 work_flags = work_color_to_flags(cwq->work_color);
1042
1043 if (likely(cwq->nr_active < cwq->max_active)) {
1044 trace_workqueue_activate_work(work);
1045 cwq->nr_active++;
1046 worklist = gcwq_determine_ins_pos(gcwq, cwq);
1047 } else {
1048 work_flags |= WORK_STRUCT_DELAYED;
1049 worklist = &cwq->delayed_works;
1050 }
1051
1052 insert_work(cwq, work, worklist, work_flags);
1053
1054 spin_unlock_irqrestore(&gcwq->lock, flags);
1055}
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067int queue_work(struct workqueue_struct *wq, struct work_struct *work)
1068{
1069 int ret;
1070
1071 ret = queue_work_on(get_cpu(), wq, work);
1072 put_cpu();
1073
1074 return ret;
1075}
1076EXPORT_SYMBOL_GPL(queue_work);
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089int
1090queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1091{
1092 int ret = 0;
1093
1094 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
1095 __queue_work(cpu, wq, work);
1096 ret = 1;
1097 }
1098 return ret;
1099}
1100EXPORT_SYMBOL_GPL(queue_work_on);
1101
1102static void delayed_work_timer_fn(unsigned long __data)
1103{
1104 struct delayed_work *dwork = (struct delayed_work *)__data;
1105 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
1106
1107 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
1108}
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118int queue_delayed_work(struct workqueue_struct *wq,
1119 struct delayed_work *dwork, unsigned long delay)
1120{
1121 if (delay == 0)
1122 return queue_work(wq, &dwork->work);
1123
1124 return queue_delayed_work_on(-1, wq, dwork, delay);
1125}
1126EXPORT_SYMBOL_GPL(queue_delayed_work);
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1138 struct delayed_work *dwork, unsigned long delay)
1139{
1140 int ret = 0;
1141 struct timer_list *timer = &dwork->timer;
1142 struct work_struct *work = &dwork->work;
1143
1144 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
1145 unsigned int lcpu;
1146
1147 BUG_ON(timer_pending(timer));
1148 BUG_ON(!list_empty(&work->entry));
1149
1150 timer_stats_timer_set_start_info(&dwork->timer);
1151
1152
1153
1154
1155
1156
1157 if (!(wq->flags & WQ_UNBOUND)) {
1158 struct global_cwq *gcwq = get_work_gcwq(work);
1159
1160 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1161 lcpu = gcwq->cpu;
1162 else
1163 lcpu = raw_smp_processor_id();
1164 } else
1165 lcpu = WORK_CPU_UNBOUND;
1166
1167 set_work_cwq(work, get_cwq(lcpu, wq), 0);
1168
1169 timer->expires = jiffies + delay;
1170 timer->data = (unsigned long)dwork;
1171 timer->function = delayed_work_timer_fn;
1172
1173 if (unlikely(cpu >= 0))
1174 add_timer_on(timer, cpu);
1175 else
1176 add_timer(timer);
1177 ret = 1;
1178 }
1179 return ret;
1180}
1181EXPORT_SYMBOL_GPL(queue_delayed_work_on);
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193static void worker_enter_idle(struct worker *worker)
1194{
1195 struct global_cwq *gcwq = worker->gcwq;
1196
1197 BUG_ON(worker->flags & WORKER_IDLE);
1198 BUG_ON(!list_empty(&worker->entry) &&
1199 (worker->hentry.next || worker->hentry.pprev));
1200
1201
1202 worker->flags |= WORKER_IDLE;
1203 gcwq->nr_idle++;
1204 worker->last_active = jiffies;
1205
1206
1207 list_add(&worker->entry, &gcwq->idle_list);
1208
1209 if (likely(!(worker->flags & WORKER_ROGUE))) {
1210 if (too_many_workers(gcwq) && !timer_pending(&gcwq->idle_timer))
1211 mod_timer(&gcwq->idle_timer,
1212 jiffies + IDLE_WORKER_TIMEOUT);
1213 } else
1214 wake_up_all(&gcwq->trustee_wait);
1215
1216
1217
1218
1219
1220
1221 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
1222 gcwq->nr_workers == gcwq->nr_idle &&
1223 atomic_read(get_gcwq_nr_running(gcwq->cpu)));
1224}
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235static void worker_leave_idle(struct worker *worker)
1236{
1237 struct global_cwq *gcwq = worker->gcwq;
1238
1239 BUG_ON(!(worker->flags & WORKER_IDLE));
1240 worker_clr_flags(worker, WORKER_IDLE);
1241 gcwq->nr_idle--;
1242 list_del_init(&worker->entry);
1243}
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275static bool worker_maybe_bind_and_lock(struct worker *worker)
1276__acquires(&gcwq->lock)
1277{
1278 struct global_cwq *gcwq = worker->gcwq;
1279 struct task_struct *task = worker->task;
1280
1281 while (true) {
1282
1283
1284
1285
1286
1287
1288 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1289 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
1290
1291 spin_lock_irq(&gcwq->lock);
1292 if (gcwq->flags & GCWQ_DISASSOCIATED)
1293 return false;
1294 if (task_cpu(task) == gcwq->cpu &&
1295 cpumask_equal(¤t->cpus_allowed,
1296 get_cpu_mask(gcwq->cpu)))
1297 return true;
1298 spin_unlock_irq(&gcwq->lock);
1299
1300
1301
1302
1303
1304
1305
1306 cpu_relax();
1307 cond_resched();
1308 }
1309}
1310
1311
1312
1313
1314
1315
1316
1317static void worker_rebind_fn(struct work_struct *work)
1318{
1319 struct worker *worker = container_of(work, struct worker, rebind_work);
1320 struct global_cwq *gcwq = worker->gcwq;
1321
1322 if (worker_maybe_bind_and_lock(worker))
1323 worker_clr_flags(worker, WORKER_REBIND);
1324
1325 spin_unlock_irq(&gcwq->lock);
1326}
1327
1328static struct worker *alloc_worker(void)
1329{
1330 struct worker *worker;
1331
1332 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
1333 if (worker) {
1334 INIT_LIST_HEAD(&worker->entry);
1335 INIT_LIST_HEAD(&worker->scheduled);
1336 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1337
1338 worker->flags = WORKER_PREP;
1339 }
1340 return worker;
1341}
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
1359{
1360 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
1361 struct worker *worker = NULL;
1362 int id = -1;
1363
1364 spin_lock_irq(&gcwq->lock);
1365 while (ida_get_new(&gcwq->worker_ida, &id)) {
1366 spin_unlock_irq(&gcwq->lock);
1367 if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
1368 goto fail;
1369 spin_lock_irq(&gcwq->lock);
1370 }
1371 spin_unlock_irq(&gcwq->lock);
1372
1373 worker = alloc_worker();
1374 if (!worker)
1375 goto fail;
1376
1377 worker->gcwq = gcwq;
1378 worker->id = id;
1379
1380 if (!on_unbound_cpu)
1381 worker->task = kthread_create_on_node(worker_thread,
1382 worker,
1383 cpu_to_node(gcwq->cpu),
1384 "kworker/%u:%d", gcwq->cpu, id);
1385 else
1386 worker->task = kthread_create(worker_thread, worker,
1387 "kworker/u:%d", id);
1388 if (IS_ERR(worker->task))
1389 goto fail;
1390
1391
1392
1393
1394
1395
1396 if (bind && !on_unbound_cpu)
1397 kthread_bind(worker->task, gcwq->cpu);
1398 else {
1399 worker->task->flags |= PF_THREAD_BOUND;
1400 if (on_unbound_cpu)
1401 worker->flags |= WORKER_UNBOUND;
1402 }
1403
1404 return worker;
1405fail:
1406 if (id >= 0) {
1407 spin_lock_irq(&gcwq->lock);
1408 ida_remove(&gcwq->worker_ida, id);
1409 spin_unlock_irq(&gcwq->lock);
1410 }
1411 kfree(worker);
1412 return NULL;
1413}
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424static void start_worker(struct worker *worker)
1425{
1426 worker->flags |= WORKER_STARTED;
1427 worker->gcwq->nr_workers++;
1428 worker_enter_idle(worker);
1429 wake_up_process(worker->task);
1430}
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441static void destroy_worker(struct worker *worker)
1442{
1443 struct global_cwq *gcwq = worker->gcwq;
1444 int id = worker->id;
1445
1446
1447 BUG_ON(worker->current_work);
1448 BUG_ON(!list_empty(&worker->scheduled));
1449
1450 if (worker->flags & WORKER_STARTED)
1451 gcwq->nr_workers--;
1452 if (worker->flags & WORKER_IDLE)
1453 gcwq->nr_idle--;
1454
1455 list_del_init(&worker->entry);
1456 worker->flags |= WORKER_DIE;
1457
1458 spin_unlock_irq(&gcwq->lock);
1459
1460 kthread_stop(worker->task);
1461 kfree(worker);
1462
1463 spin_lock_irq(&gcwq->lock);
1464 ida_remove(&gcwq->worker_ida, id);
1465}
1466
1467static void idle_worker_timeout(unsigned long __gcwq)
1468{
1469 struct global_cwq *gcwq = (void *)__gcwq;
1470
1471 spin_lock_irq(&gcwq->lock);
1472
1473 if (too_many_workers(gcwq)) {
1474 struct worker *worker;
1475 unsigned long expires;
1476
1477
1478 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1479 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1480
1481 if (time_before(jiffies, expires))
1482 mod_timer(&gcwq->idle_timer, expires);
1483 else {
1484
1485 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1486 wake_up_worker(gcwq);
1487 }
1488 }
1489
1490 spin_unlock_irq(&gcwq->lock);
1491}
1492
1493static bool send_mayday(struct work_struct *work)
1494{
1495 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1496 struct workqueue_struct *wq = cwq->wq;
1497 unsigned int cpu;
1498
1499 if (!(wq->flags & WQ_RESCUER))
1500 return false;
1501
1502
1503 cpu = cwq->gcwq->cpu;
1504
1505 if (cpu == WORK_CPU_UNBOUND)
1506 cpu = 0;
1507 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
1508 wake_up_process(wq->rescuer->task);
1509 return true;
1510}
1511
1512static void gcwq_mayday_timeout(unsigned long __gcwq)
1513{
1514 struct global_cwq *gcwq = (void *)__gcwq;
1515 struct work_struct *work;
1516
1517 spin_lock_irq(&gcwq->lock);
1518
1519 if (need_to_create_worker(gcwq)) {
1520
1521
1522
1523
1524
1525
1526 list_for_each_entry(work, &gcwq->worklist, entry)
1527 send_mayday(work);
1528 }
1529
1530 spin_unlock_irq(&gcwq->lock);
1531
1532 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
1533}
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557static bool maybe_create_worker(struct global_cwq *gcwq)
1558__releases(&gcwq->lock)
1559__acquires(&gcwq->lock)
1560{
1561 if (!need_to_create_worker(gcwq))
1562 return false;
1563restart:
1564 spin_unlock_irq(&gcwq->lock);
1565
1566
1567 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1568
1569 while (true) {
1570 struct worker *worker;
1571
1572 worker = create_worker(gcwq, true);
1573 if (worker) {
1574 del_timer_sync(&gcwq->mayday_timer);
1575 spin_lock_irq(&gcwq->lock);
1576 start_worker(worker);
1577 BUG_ON(need_to_create_worker(gcwq));
1578 return true;
1579 }
1580
1581 if (!need_to_create_worker(gcwq))
1582 break;
1583
1584 __set_current_state(TASK_INTERRUPTIBLE);
1585 schedule_timeout(CREATE_COOLDOWN);
1586
1587 if (!need_to_create_worker(gcwq))
1588 break;
1589 }
1590
1591 del_timer_sync(&gcwq->mayday_timer);
1592 spin_lock_irq(&gcwq->lock);
1593 if (need_to_create_worker(gcwq))
1594 goto restart;
1595 return true;
1596}
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613static bool maybe_destroy_workers(struct global_cwq *gcwq)
1614{
1615 bool ret = false;
1616
1617 while (too_many_workers(gcwq)) {
1618 struct worker *worker;
1619 unsigned long expires;
1620
1621 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1622 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1623
1624 if (time_before(jiffies, expires)) {
1625 mod_timer(&gcwq->idle_timer, expires);
1626 break;
1627 }
1628
1629 destroy_worker(worker);
1630 ret = true;
1631 }
1632
1633 return ret;
1634}
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656static bool manage_workers(struct worker *worker)
1657{
1658 struct global_cwq *gcwq = worker->gcwq;
1659 bool ret = false;
1660
1661 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1662 return ret;
1663
1664 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1665 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1666
1667
1668
1669
1670
1671 ret |= maybe_destroy_workers(gcwq);
1672 ret |= maybe_create_worker(gcwq);
1673
1674 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1675
1676
1677
1678
1679
1680 if (unlikely(gcwq->trustee))
1681 wake_up_all(&gcwq->trustee_wait);
1682
1683 return ret;
1684}
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703static void move_linked_works(struct work_struct *work, struct list_head *head,
1704 struct work_struct **nextp)
1705{
1706 struct work_struct *n;
1707
1708
1709
1710
1711
1712 list_for_each_entry_safe_from(work, n, NULL, entry) {
1713 list_move_tail(&work->entry, head);
1714 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1715 break;
1716 }
1717
1718
1719
1720
1721
1722
1723 if (nextp)
1724 *nextp = n;
1725}
1726
1727static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1728{
1729 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1730 struct work_struct, entry);
1731 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
1732
1733 trace_workqueue_activate_work(work);
1734 move_linked_works(work, pos, NULL);
1735 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
1736 cwq->nr_active++;
1737}
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1752 bool delayed)
1753{
1754
1755 if (color == WORK_NO_COLOR)
1756 return;
1757
1758 cwq->nr_in_flight[color]--;
1759
1760 if (!delayed) {
1761 cwq->nr_active--;
1762 if (!list_empty(&cwq->delayed_works)) {
1763
1764 if (cwq->nr_active < cwq->max_active)
1765 cwq_activate_first_delayed(cwq);
1766 }
1767 }
1768
1769
1770 if (likely(cwq->flush_color != color))
1771 return;
1772
1773
1774 if (cwq->nr_in_flight[color])
1775 return;
1776
1777
1778 cwq->flush_color = -1;
1779
1780
1781
1782
1783
1784 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1785 complete(&cwq->wq->first_flusher->done);
1786}
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802static void process_one_work(struct worker *worker, struct work_struct *work)
1803__releases(&gcwq->lock)
1804__acquires(&gcwq->lock)
1805{
1806 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1807 struct global_cwq *gcwq = cwq->gcwq;
1808 struct hlist_head *bwh = busy_worker_head(gcwq, work);
1809 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
1810 work_func_t f = work->func;
1811 int work_color;
1812 struct worker *collision;
1813#ifdef CONFIG_LOCKDEP
1814
1815
1816
1817
1818
1819
1820
1821 struct lockdep_map lockdep_map;
1822
1823 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
1824#endif
1825
1826
1827
1828
1829
1830
1831 collision = __find_worker_executing_work(gcwq, bwh, work);
1832 if (unlikely(collision)) {
1833 move_linked_works(work, &collision->scheduled, NULL);
1834 return;
1835 }
1836
1837
1838 debug_work_deactivate(work);
1839 hlist_add_head(&worker->hentry, bwh);
1840 worker->current_work = work;
1841 worker->current_cwq = cwq;
1842 work_color = get_work_color(work);
1843
1844
1845 set_work_cpu(work, gcwq->cpu);
1846 list_del_init(&work->entry);
1847
1848
1849
1850
1851
1852 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
1853 struct work_struct *nwork = list_first_entry(&gcwq->worklist,
1854 struct work_struct, entry);
1855
1856 if (!list_empty(&gcwq->worklist) &&
1857 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1858 wake_up_worker(gcwq);
1859 else
1860 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1861 }
1862
1863
1864
1865
1866
1867 if (unlikely(cpu_intensive))
1868 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1869
1870 spin_unlock_irq(&gcwq->lock);
1871
1872 smp_wmb();
1873 work_clear_pending(work);
1874
1875 lock_map_acquire_read(&cwq->wq->lockdep_map);
1876 lock_map_acquire(&lockdep_map);
1877 trace_workqueue_execute_start(work);
1878 f(work);
1879
1880
1881
1882
1883 trace_workqueue_execute_end(work);
1884 lock_map_release(&lockdep_map);
1885 lock_map_release(&cwq->wq->lockdep_map);
1886
1887 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1888 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1889 "%s/0x%08x/%d\n",
1890 current->comm, preempt_count(), task_pid_nr(current));
1891 printk(KERN_ERR " last function: ");
1892 print_symbol("%s\n", (unsigned long)f);
1893 debug_show_held_locks(current);
1894 dump_stack();
1895 }
1896
1897 spin_lock_irq(&gcwq->lock);
1898
1899
1900 if (unlikely(cpu_intensive))
1901 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1902
1903
1904 hlist_del_init(&worker->hentry);
1905 worker->current_work = NULL;
1906 worker->current_cwq = NULL;
1907 cwq_dec_nr_in_flight(cwq, work_color, false);
1908}
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922static void process_scheduled_works(struct worker *worker)
1923{
1924 while (!list_empty(&worker->scheduled)) {
1925 struct work_struct *work = list_first_entry(&worker->scheduled,
1926 struct work_struct, entry);
1927 process_one_work(worker, work);
1928 }
1929}
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941static int worker_thread(void *__worker)
1942{
1943 struct worker *worker = __worker;
1944 struct global_cwq *gcwq = worker->gcwq;
1945
1946
1947 worker->task->flags |= PF_WQ_WORKER;
1948woke_up:
1949 spin_lock_irq(&gcwq->lock);
1950
1951
1952 if (worker->flags & WORKER_DIE) {
1953 spin_unlock_irq(&gcwq->lock);
1954 worker->task->flags &= ~PF_WQ_WORKER;
1955 return 0;
1956 }
1957
1958 worker_leave_idle(worker);
1959recheck:
1960
1961 if (!need_more_worker(gcwq))
1962 goto sleep;
1963
1964
1965 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1966 goto recheck;
1967
1968
1969
1970
1971
1972
1973 BUG_ON(!list_empty(&worker->scheduled));
1974
1975
1976
1977
1978
1979
1980 worker_clr_flags(worker, WORKER_PREP);
1981
1982 do {
1983 struct work_struct *work =
1984 list_first_entry(&gcwq->worklist,
1985 struct work_struct, entry);
1986
1987 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1988
1989 process_one_work(worker, work);
1990 if (unlikely(!list_empty(&worker->scheduled)))
1991 process_scheduled_works(worker);
1992 } else {
1993 move_linked_works(work, &worker->scheduled, NULL);
1994 process_scheduled_works(worker);
1995 }
1996 } while (keep_working(gcwq));
1997
1998 worker_set_flags(worker, WORKER_PREP, false);
1999sleep:
2000 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
2001 goto recheck;
2002
2003
2004
2005
2006
2007
2008
2009
2010 worker_enter_idle(worker);
2011 __set_current_state(TASK_INTERRUPTIBLE);
2012 spin_unlock_irq(&gcwq->lock);
2013 schedule();
2014 goto woke_up;
2015}
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036static int rescuer_thread(void *__wq)
2037{
2038 struct workqueue_struct *wq = __wq;
2039 struct worker *rescuer = wq->rescuer;
2040 struct list_head *scheduled = &rescuer->scheduled;
2041 bool is_unbound = wq->flags & WQ_UNBOUND;
2042 unsigned int cpu;
2043
2044 set_user_nice(current, RESCUER_NICE_LEVEL);
2045repeat:
2046 set_current_state(TASK_INTERRUPTIBLE);
2047
2048 if (kthread_should_stop())
2049 return 0;
2050
2051
2052
2053
2054
2055 for_each_mayday_cpu(cpu, wq->mayday_mask) {
2056 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2057 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
2058 struct global_cwq *gcwq = cwq->gcwq;
2059 struct work_struct *work, *n;
2060
2061 __set_current_state(TASK_RUNNING);
2062 mayday_clear_cpu(cpu, wq->mayday_mask);
2063
2064
2065 rescuer->gcwq = gcwq;
2066 worker_maybe_bind_and_lock(rescuer);
2067
2068
2069
2070
2071
2072 BUG_ON(!list_empty(&rescuer->scheduled));
2073 list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
2074 if (get_work_cwq(work) == cwq)
2075 move_linked_works(work, scheduled, &n);
2076
2077 process_scheduled_works(rescuer);
2078
2079
2080
2081
2082
2083
2084 if (keep_working(gcwq))
2085 wake_up_worker(gcwq);
2086
2087 spin_unlock_irq(&gcwq->lock);
2088 }
2089
2090 schedule();
2091 goto repeat;
2092}
2093
2094struct wq_barrier {
2095 struct work_struct work;
2096 struct completion done;
2097};
2098
2099static void wq_barrier_func(struct work_struct *work)
2100{
2101 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2102 complete(&barr->done);
2103}
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
2130 struct wq_barrier *barr,
2131 struct work_struct *target, struct worker *worker)
2132{
2133 struct list_head *head;
2134 unsigned int linked = 0;
2135
2136
2137
2138
2139
2140
2141
2142 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
2143 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
2144 init_completion(&barr->done);
2145
2146
2147
2148
2149
2150 if (worker)
2151 head = worker->scheduled.next;
2152 else {
2153 unsigned long *bits = work_data_bits(target);
2154
2155 head = target->entry.next;
2156
2157 linked = *bits & WORK_STRUCT_LINKED;
2158 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2159 }
2160
2161 debug_work_activate(&barr->work);
2162 insert_work(cwq, &barr->work, head,
2163 work_color_to_flags(WORK_NO_COLOR) | linked);
2164}
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2198 int flush_color, int work_color)
2199{
2200 bool wait = false;
2201 unsigned int cpu;
2202
2203 if (flush_color >= 0) {
2204 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2205 atomic_set(&wq->nr_cwqs_to_flush, 1);
2206 }
2207
2208 for_each_cwq_cpu(cpu, wq) {
2209 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2210 struct global_cwq *gcwq = cwq->gcwq;
2211
2212 spin_lock_irq(&gcwq->lock);
2213
2214 if (flush_color >= 0) {
2215 BUG_ON(cwq->flush_color != -1);
2216
2217 if (cwq->nr_in_flight[flush_color]) {
2218 cwq->flush_color = flush_color;
2219 atomic_inc(&wq->nr_cwqs_to_flush);
2220 wait = true;
2221 }
2222 }
2223
2224 if (work_color >= 0) {
2225 BUG_ON(work_color != work_next_color(cwq->work_color));
2226 cwq->work_color = work_color;
2227 }
2228
2229 spin_unlock_irq(&gcwq->lock);
2230 }
2231
2232 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2233 complete(&wq->first_flusher->done);
2234
2235 return wait;
2236}
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248void flush_workqueue(struct workqueue_struct *wq)
2249{
2250 struct wq_flusher this_flusher = {
2251 .list = LIST_HEAD_INIT(this_flusher.list),
2252 .flush_color = -1,
2253 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2254 };
2255 int next_color;
2256
2257 lock_map_acquire(&wq->lockdep_map);
2258 lock_map_release(&wq->lockdep_map);
2259
2260 mutex_lock(&wq->flush_mutex);
2261
2262
2263
2264
2265 next_color = work_next_color(wq->work_color);
2266
2267 if (next_color != wq->flush_color) {
2268
2269
2270
2271
2272
2273 BUG_ON(!list_empty(&wq->flusher_overflow));
2274 this_flusher.flush_color = wq->work_color;
2275 wq->work_color = next_color;
2276
2277 if (!wq->first_flusher) {
2278
2279 BUG_ON(wq->flush_color != this_flusher.flush_color);
2280
2281 wq->first_flusher = &this_flusher;
2282
2283 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2284 wq->work_color)) {
2285
2286 wq->flush_color = next_color;
2287 wq->first_flusher = NULL;
2288 goto out_unlock;
2289 }
2290 } else {
2291
2292 BUG_ON(wq->flush_color == this_flusher.flush_color);
2293 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2294 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2295 }
2296 } else {
2297
2298
2299
2300
2301
2302 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2303 }
2304
2305 mutex_unlock(&wq->flush_mutex);
2306
2307 wait_for_completion(&this_flusher.done);
2308
2309
2310
2311
2312
2313
2314
2315 if (wq->first_flusher != &this_flusher)
2316 return;
2317
2318 mutex_lock(&wq->flush_mutex);
2319
2320
2321 if (wq->first_flusher != &this_flusher)
2322 goto out_unlock;
2323
2324 wq->first_flusher = NULL;
2325
2326 BUG_ON(!list_empty(&this_flusher.list));
2327 BUG_ON(wq->flush_color != this_flusher.flush_color);
2328
2329 while (true) {
2330 struct wq_flusher *next, *tmp;
2331
2332
2333 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2334 if (next->flush_color != wq->flush_color)
2335 break;
2336 list_del_init(&next->list);
2337 complete(&next->done);
2338 }
2339
2340 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2341 wq->flush_color != work_next_color(wq->work_color));
2342
2343
2344 wq->flush_color = work_next_color(wq->flush_color);
2345
2346
2347 if (!list_empty(&wq->flusher_overflow)) {
2348
2349
2350
2351
2352
2353
2354 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2355 tmp->flush_color = wq->work_color;
2356
2357 wq->work_color = work_next_color(wq->work_color);
2358
2359 list_splice_tail_init(&wq->flusher_overflow,
2360 &wq->flusher_queue);
2361 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2362 }
2363
2364 if (list_empty(&wq->flusher_queue)) {
2365 BUG_ON(wq->flush_color != wq->work_color);
2366 break;
2367 }
2368
2369
2370
2371
2372
2373 BUG_ON(wq->flush_color == wq->work_color);
2374 BUG_ON(wq->flush_color != next->flush_color);
2375
2376 list_del_init(&next->list);
2377 wq->first_flusher = next;
2378
2379 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2380 break;
2381
2382
2383
2384
2385
2386 wq->first_flusher = NULL;
2387 }
2388
2389out_unlock:
2390 mutex_unlock(&wq->flush_mutex);
2391}
2392EXPORT_SYMBOL_GPL(flush_workqueue);
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405void drain_workqueue(struct workqueue_struct *wq)
2406{
2407 unsigned int flush_cnt = 0;
2408 unsigned int cpu;
2409
2410
2411
2412
2413
2414
2415 spin_lock(&workqueue_lock);
2416 if (!wq->nr_drainers++)
2417 wq->flags |= WQ_DRAINING;
2418 spin_unlock(&workqueue_lock);
2419reflush:
2420 flush_workqueue(wq);
2421
2422 for_each_cwq_cpu(cpu, wq) {
2423 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2424 bool drained;
2425
2426 spin_lock_irq(&cwq->gcwq->lock);
2427 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
2428 spin_unlock_irq(&cwq->gcwq->lock);
2429
2430 if (drained)
2431 continue;
2432
2433 if (++flush_cnt == 10 ||
2434 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2435 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2436 wq->name, flush_cnt);
2437 goto reflush;
2438 }
2439
2440 spin_lock(&workqueue_lock);
2441 if (!--wq->nr_drainers)
2442 wq->flags &= ~WQ_DRAINING;
2443 spin_unlock(&workqueue_lock);
2444}
2445EXPORT_SYMBOL_GPL(drain_workqueue);
2446
2447static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2448 bool wait_executing)
2449{
2450 struct worker *worker = NULL;
2451 struct global_cwq *gcwq;
2452 struct cpu_workqueue_struct *cwq;
2453
2454 might_sleep();
2455 gcwq = get_work_gcwq(work);
2456 if (!gcwq)
2457 return false;
2458
2459 spin_lock_irq(&gcwq->lock);
2460 if (!list_empty(&work->entry)) {
2461
2462
2463
2464
2465
2466 smp_rmb();
2467 cwq = get_work_cwq(work);
2468 if (unlikely(!cwq || gcwq != cwq->gcwq))
2469 goto already_gone;
2470 } else if (wait_executing) {
2471 worker = find_worker_executing_work(gcwq, work);
2472 if (!worker)
2473 goto already_gone;
2474 cwq = worker->current_cwq;
2475 } else
2476 goto already_gone;
2477
2478 insert_wq_barrier(cwq, barr, work, worker);
2479 spin_unlock_irq(&gcwq->lock);
2480
2481
2482
2483
2484
2485
2486
2487 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2488 lock_map_acquire(&cwq->wq->lockdep_map);
2489 else
2490 lock_map_acquire_read(&cwq->wq->lockdep_map);
2491 lock_map_release(&cwq->wq->lockdep_map);
2492
2493 return true;
2494already_gone:
2495 spin_unlock_irq(&gcwq->lock);
2496 return false;
2497}
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517bool flush_work(struct work_struct *work)
2518{
2519 struct wq_barrier barr;
2520
2521 lock_map_acquire(&work->lockdep_map);
2522 lock_map_release(&work->lockdep_map);
2523
2524 if (start_flush_work(work, &barr, true)) {
2525 wait_for_completion(&barr.done);
2526 destroy_work_on_stack(&barr.work);
2527 return true;
2528 } else
2529 return false;
2530}
2531EXPORT_SYMBOL_GPL(flush_work);
2532
2533static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2534{
2535 struct wq_barrier barr;
2536 struct worker *worker;
2537
2538 spin_lock_irq(&gcwq->lock);
2539
2540 worker = find_worker_executing_work(gcwq, work);
2541 if (unlikely(worker))
2542 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2543
2544 spin_unlock_irq(&gcwq->lock);
2545
2546 if (unlikely(worker)) {
2547 wait_for_completion(&barr.done);
2548 destroy_work_on_stack(&barr.work);
2549 return true;
2550 } else
2551 return false;
2552}
2553
2554static bool wait_on_work(struct work_struct *work)
2555{
2556 bool ret = false;
2557 int cpu;
2558
2559 might_sleep();
2560
2561 lock_map_acquire(&work->lockdep_map);
2562 lock_map_release(&work->lockdep_map);
2563
2564 for_each_gcwq_cpu(cpu)
2565 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2566 return ret;
2567}
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583bool flush_work_sync(struct work_struct *work)
2584{
2585 struct wq_barrier barr;
2586 bool pending, waited;
2587
2588
2589 pending = start_flush_work(work, &barr, false);
2590
2591
2592 waited = wait_on_work(work);
2593
2594
2595 if (pending) {
2596 wait_for_completion(&barr.done);
2597 destroy_work_on_stack(&barr.work);
2598 }
2599
2600 return pending || waited;
2601}
2602EXPORT_SYMBOL_GPL(flush_work_sync);
2603
2604
2605
2606
2607
2608static int try_to_grab_pending(struct work_struct *work)
2609{
2610 struct global_cwq *gcwq;
2611 int ret = -1;
2612
2613 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
2614 return 0;
2615
2616
2617
2618
2619
2620 gcwq = get_work_gcwq(work);
2621 if (!gcwq)
2622 return ret;
2623
2624 spin_lock_irq(&gcwq->lock);
2625 if (!list_empty(&work->entry)) {
2626
2627
2628
2629
2630
2631 smp_rmb();
2632 if (gcwq == get_work_gcwq(work)) {
2633 debug_work_deactivate(work);
2634 list_del_init(&work->entry);
2635 cwq_dec_nr_in_flight(get_work_cwq(work),
2636 get_work_color(work),
2637 *work_data_bits(work) & WORK_STRUCT_DELAYED);
2638 ret = 1;
2639 }
2640 }
2641 spin_unlock_irq(&gcwq->lock);
2642
2643 return ret;
2644}
2645
2646static bool __cancel_work_timer(struct work_struct *work,
2647 struct timer_list* timer)
2648{
2649 int ret;
2650
2651 do {
2652 ret = (timer && likely(del_timer(timer)));
2653 if (!ret)
2654 ret = try_to_grab_pending(work);
2655 wait_on_work(work);
2656 } while (unlikely(ret < 0));
2657
2658 clear_work_data(work);
2659 return ret;
2660}
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680bool cancel_work_sync(struct work_struct *work)
2681{
2682 return __cancel_work_timer(work, NULL);
2683}
2684EXPORT_SYMBOL_GPL(cancel_work_sync);
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698bool flush_delayed_work(struct delayed_work *dwork)
2699{
2700 if (del_timer_sync(&dwork->timer))
2701 __queue_work(raw_smp_processor_id(),
2702 get_work_cwq(&dwork->work)->wq, &dwork->work);
2703 return flush_work(&dwork->work);
2704}
2705EXPORT_SYMBOL(flush_delayed_work);
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719bool flush_delayed_work_sync(struct delayed_work *dwork)
2720{
2721 if (del_timer_sync(&dwork->timer))
2722 __queue_work(raw_smp_processor_id(),
2723 get_work_cwq(&dwork->work)->wq, &dwork->work);
2724 return flush_work_sync(&dwork->work);
2725}
2726EXPORT_SYMBOL(flush_delayed_work_sync);
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737bool cancel_delayed_work_sync(struct delayed_work *dwork)
2738{
2739 return __cancel_work_timer(&dwork->work, &dwork->timer);
2740}
2741EXPORT_SYMBOL(cancel_delayed_work_sync);
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754int schedule_work(struct work_struct *work)
2755{
2756 return queue_work(system_wq, work);
2757}
2758EXPORT_SYMBOL(schedule_work);
2759
2760
2761
2762
2763
2764
2765
2766
2767int schedule_work_on(int cpu, struct work_struct *work)
2768{
2769 return queue_work_on(cpu, system_wq, work);
2770}
2771EXPORT_SYMBOL(schedule_work_on);
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781int schedule_delayed_work(struct delayed_work *dwork,
2782 unsigned long delay)
2783{
2784 return queue_delayed_work(system_wq, dwork, delay);
2785}
2786EXPORT_SYMBOL(schedule_delayed_work);
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797int schedule_delayed_work_on(int cpu,
2798 struct delayed_work *dwork, unsigned long delay)
2799{
2800 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
2801}
2802EXPORT_SYMBOL(schedule_delayed_work_on);
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815int schedule_on_each_cpu(work_func_t func)
2816{
2817 int cpu;
2818 struct work_struct __percpu *works;
2819
2820 works = alloc_percpu(struct work_struct);
2821 if (!works)
2822 return -ENOMEM;
2823
2824 get_online_cpus();
2825
2826 for_each_online_cpu(cpu) {
2827 struct work_struct *work = per_cpu_ptr(works, cpu);
2828
2829 INIT_WORK(work, func);
2830 schedule_work_on(cpu, work);
2831 }
2832
2833 for_each_online_cpu(cpu)
2834 flush_work(per_cpu_ptr(works, cpu));
2835
2836 put_online_cpus();
2837 free_percpu(works);
2838 return 0;
2839}
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865void flush_scheduled_work(void)
2866{
2867 flush_workqueue(system_wq);
2868}
2869EXPORT_SYMBOL(flush_scheduled_work);
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883int execute_in_process_context(work_func_t fn, struct execute_work *ew)
2884{
2885 if (!in_interrupt()) {
2886 fn(&ew->work);
2887 return 0;
2888 }
2889
2890 INIT_WORK(&ew->work, fn);
2891 schedule_work(&ew->work);
2892
2893 return 1;
2894}
2895EXPORT_SYMBOL_GPL(execute_in_process_context);
2896
2897int keventd_up(void)
2898{
2899 return system_wq != NULL;
2900}
2901
2902static int alloc_cwqs(struct workqueue_struct *wq)
2903{
2904
2905
2906
2907
2908
2909 const size_t size = sizeof(struct cpu_workqueue_struct);
2910 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2911 __alignof__(unsigned long long));
2912
2913 if (!(wq->flags & WQ_UNBOUND))
2914 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
2915 else {
2916 void *ptr;
2917
2918
2919
2920
2921
2922
2923 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2924 if (ptr) {
2925 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2926 *(void **)(wq->cpu_wq.single + 1) = ptr;
2927 }
2928 }
2929
2930
2931 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2932 return wq->cpu_wq.v ? 0 : -ENOMEM;
2933}
2934
2935static void free_cwqs(struct workqueue_struct *wq)
2936{
2937 if (!(wq->flags & WQ_UNBOUND))
2938 free_percpu(wq->cpu_wq.pcpu);
2939 else if (wq->cpu_wq.single) {
2940
2941 kfree(*(void **)(wq->cpu_wq.single + 1));
2942 }
2943}
2944
2945static int wq_clamp_max_active(int max_active, unsigned int flags,
2946 const char *name)
2947{
2948 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2949
2950 if (max_active < 1 || max_active > lim)
2951 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2952 "is out of range, clamping between %d and %d\n",
2953 max_active, name, 1, lim);
2954
2955 return clamp_val(max_active, 1, lim);
2956}
2957
2958struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
2959 unsigned int flags,
2960 int max_active,
2961 struct lock_class_key *key,
2962 const char *lock_name, ...)
2963{
2964 va_list args, args1;
2965 struct workqueue_struct *wq;
2966 unsigned int cpu;
2967 size_t namelen;
2968
2969
2970 va_start(args, lock_name);
2971 va_copy(args1, args);
2972 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
2973
2974 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
2975 if (!wq)
2976 goto err;
2977
2978 vsnprintf(wq->name, namelen, fmt, args1);
2979 va_end(args);
2980 va_end(args1);
2981
2982
2983
2984
2985
2986 if (flags & WQ_MEM_RECLAIM)
2987 flags |= WQ_RESCUER;
2988
2989
2990
2991
2992
2993 if (flags & WQ_UNBOUND)
2994 flags |= WQ_HIGHPRI;
2995
2996 max_active = max_active ?: WQ_DFL_ACTIVE;
2997 max_active = wq_clamp_max_active(max_active, flags, wq->name);
2998
2999
3000 wq->flags = flags;
3001 wq->saved_max_active = max_active;
3002 mutex_init(&wq->flush_mutex);
3003 atomic_set(&wq->nr_cwqs_to_flush, 0);
3004 INIT_LIST_HEAD(&wq->flusher_queue);
3005 INIT_LIST_HEAD(&wq->flusher_overflow);
3006
3007 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
3008 INIT_LIST_HEAD(&wq->list);
3009
3010 if (alloc_cwqs(wq) < 0)
3011 goto err;
3012
3013 for_each_cwq_cpu(cpu, wq) {
3014 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3015 struct global_cwq *gcwq = get_gcwq(cpu);
3016
3017 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
3018 cwq->gcwq = gcwq;
3019 cwq->wq = wq;
3020 cwq->flush_color = -1;
3021 cwq->max_active = max_active;
3022 INIT_LIST_HEAD(&cwq->delayed_works);
3023 }
3024
3025 if (flags & WQ_RESCUER) {
3026 struct worker *rescuer;
3027
3028 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
3029 goto err;
3030
3031 wq->rescuer = rescuer = alloc_worker();
3032 if (!rescuer)
3033 goto err;
3034
3035 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3036 wq->name);
3037 if (IS_ERR(rescuer->task))
3038 goto err;
3039
3040 rescuer->task->flags |= PF_THREAD_BOUND;
3041 wake_up_process(rescuer->task);
3042 }
3043
3044
3045
3046
3047
3048
3049 spin_lock(&workqueue_lock);
3050
3051 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
3052 for_each_cwq_cpu(cpu, wq)
3053 get_cwq(cpu, wq)->max_active = 0;
3054
3055 list_add(&wq->list, &workqueues);
3056
3057 spin_unlock(&workqueue_lock);
3058
3059 return wq;
3060err:
3061 if (wq) {
3062 free_cwqs(wq);
3063 free_mayday_mask(wq->mayday_mask);
3064 kfree(wq->rescuer);
3065 kfree(wq);
3066 }
3067 return NULL;
3068}
3069EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
3070
3071
3072
3073
3074
3075
3076
3077void destroy_workqueue(struct workqueue_struct *wq)
3078{
3079 unsigned int cpu;
3080
3081
3082 drain_workqueue(wq);
3083
3084
3085
3086
3087
3088 spin_lock(&workqueue_lock);
3089 list_del(&wq->list);
3090 spin_unlock(&workqueue_lock);
3091
3092
3093 for_each_cwq_cpu(cpu, wq) {
3094 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3095 int i;
3096
3097 for (i = 0; i < WORK_NR_COLORS; i++)
3098 BUG_ON(cwq->nr_in_flight[i]);
3099 BUG_ON(cwq->nr_active);
3100 BUG_ON(!list_empty(&cwq->delayed_works));
3101 }
3102
3103 if (wq->flags & WQ_RESCUER) {
3104 kthread_stop(wq->rescuer->task);
3105 free_mayday_mask(wq->mayday_mask);
3106 kfree(wq->rescuer);
3107 }
3108
3109 free_cwqs(wq);
3110 kfree(wq);
3111}
3112EXPORT_SYMBOL_GPL(destroy_workqueue);
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3125{
3126 unsigned int cpu;
3127
3128 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
3129
3130 spin_lock(&workqueue_lock);
3131
3132 wq->saved_max_active = max_active;
3133
3134 for_each_cwq_cpu(cpu, wq) {
3135 struct global_cwq *gcwq = get_gcwq(cpu);
3136
3137 spin_lock_irq(&gcwq->lock);
3138
3139 if (!(wq->flags & WQ_FREEZABLE) ||
3140 !(gcwq->flags & GCWQ_FREEZING))
3141 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3142
3143 spin_unlock_irq(&gcwq->lock);
3144 }
3145
3146 spin_unlock(&workqueue_lock);
3147}
3148EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3163{
3164 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3165
3166 return !list_empty(&cwq->delayed_works);
3167}
3168EXPORT_SYMBOL_GPL(workqueue_congested);
3169
3170
3171
3172
3173
3174
3175
3176
3177unsigned int work_cpu(struct work_struct *work)
3178{
3179 struct global_cwq *gcwq = get_work_gcwq(work);
3180
3181 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
3182}
3183EXPORT_SYMBOL_GPL(work_cpu);
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198unsigned int work_busy(struct work_struct *work)
3199{
3200 struct global_cwq *gcwq = get_work_gcwq(work);
3201 unsigned long flags;
3202 unsigned int ret = 0;
3203
3204 if (!gcwq)
3205 return false;
3206
3207 spin_lock_irqsave(&gcwq->lock, flags);
3208
3209 if (work_pending(work))
3210 ret |= WORK_BUSY_PENDING;
3211 if (find_worker_executing_work(gcwq, work))
3212 ret |= WORK_BUSY_RUNNING;
3213
3214 spin_unlock_irqrestore(&gcwq->lock, flags);
3215
3216 return ret;
3217}
3218EXPORT_SYMBOL_GPL(work_busy);
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289#define trustee_wait_event_timeout(cond, timeout) ({ \
3290 long __ret = (timeout); \
3291 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3292 __ret) { \
3293 spin_unlock_irq(&gcwq->lock); \
3294 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3295 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3296 __ret); \
3297 spin_lock_irq(&gcwq->lock); \
3298 } \
3299 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3300})
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316#define trustee_wait_event(cond) ({ \
3317 long __ret1; \
3318 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3319 __ret1 < 0 ? -1 : 0; \
3320})
3321
3322static int __cpuinit trustee_thread(void *__gcwq)
3323{
3324 struct global_cwq *gcwq = __gcwq;
3325 struct worker *worker;
3326 struct work_struct *work;
3327 struct hlist_node *pos;
3328 long rc;
3329 int i;
3330
3331 BUG_ON(gcwq->cpu != smp_processor_id());
3332
3333 spin_lock_irq(&gcwq->lock);
3334
3335
3336
3337
3338
3339 BUG_ON(gcwq->cpu != smp_processor_id());
3340 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3341 BUG_ON(rc < 0);
3342
3343 gcwq->flags |= GCWQ_MANAGING_WORKERS;
3344
3345 list_for_each_entry(worker, &gcwq->idle_list, entry)
3346 worker->flags |= WORKER_ROGUE;
3347
3348 for_each_busy_worker(worker, i, pos, gcwq)
3349 worker->flags |= WORKER_ROGUE;
3350
3351
3352
3353
3354
3355
3356
3357 spin_unlock_irq(&gcwq->lock);
3358 schedule();
3359 spin_lock_irq(&gcwq->lock);
3360
3361
3362
3363
3364
3365
3366
3367 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
3368
3369 spin_unlock_irq(&gcwq->lock);
3370 del_timer_sync(&gcwq->idle_timer);
3371 spin_lock_irq(&gcwq->lock);
3372
3373
3374
3375
3376
3377
3378
3379 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3380 wake_up_all(&gcwq->trustee_wait);
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392 while (gcwq->nr_workers != gcwq->nr_idle ||
3393 gcwq->flags & GCWQ_FREEZING ||
3394 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
3395 int nr_works = 0;
3396
3397 list_for_each_entry(work, &gcwq->worklist, entry) {
3398 send_mayday(work);
3399 nr_works++;
3400 }
3401
3402 list_for_each_entry(worker, &gcwq->idle_list, entry) {
3403 if (!nr_works--)
3404 break;
3405 wake_up_process(worker->task);
3406 }
3407
3408 if (need_to_create_worker(gcwq)) {
3409 spin_unlock_irq(&gcwq->lock);
3410 worker = create_worker(gcwq, false);
3411 spin_lock_irq(&gcwq->lock);
3412 if (worker) {
3413 worker->flags |= WORKER_ROGUE;
3414 start_worker(worker);
3415 }
3416 }
3417
3418
3419 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3420 break;
3421 }
3422
3423
3424
3425
3426
3427
3428 do {
3429 rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
3430 while (!list_empty(&gcwq->idle_list))
3431 destroy_worker(list_first_entry(&gcwq->idle_list,
3432 struct worker, entry));
3433 } while (gcwq->nr_workers && rc >= 0);
3434
3435
3436
3437
3438
3439
3440
3441
3442 WARN_ON(!list_empty(&gcwq->idle_list));
3443
3444 for_each_busy_worker(worker, i, pos, gcwq) {
3445 struct work_struct *rebind_work = &worker->rebind_work;
3446 unsigned long worker_flags = worker->flags;
3447
3448
3449
3450
3451
3452
3453
3454 worker_flags |= WORKER_REBIND;
3455 worker_flags &= ~WORKER_ROGUE;
3456 ACCESS_ONCE(worker->flags) = worker_flags;
3457
3458
3459 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3460 work_data_bits(rebind_work)))
3461 continue;
3462
3463 debug_work_activate(rebind_work);
3464 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
3465 worker->scheduled.next,
3466 work_color_to_flags(WORK_NO_COLOR));
3467 }
3468
3469
3470 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3471
3472
3473 gcwq->trustee = NULL;
3474 gcwq->trustee_state = TRUSTEE_DONE;
3475 wake_up_all(&gcwq->trustee_wait);
3476 spin_unlock_irq(&gcwq->lock);
3477 return 0;
3478}
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
3492__releases(&gcwq->lock)
3493__acquires(&gcwq->lock)
3494{
3495 if (!(gcwq->trustee_state == state ||
3496 gcwq->trustee_state == TRUSTEE_DONE)) {
3497 spin_unlock_irq(&gcwq->lock);
3498 __wait_event(gcwq->trustee_wait,
3499 gcwq->trustee_state == state ||
3500 gcwq->trustee_state == TRUSTEE_DONE);
3501 spin_lock_irq(&gcwq->lock);
3502 }
3503}
3504
3505static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3506 unsigned long action,
3507 void *hcpu)
3508{
3509 unsigned int cpu = (unsigned long)hcpu;
3510 struct global_cwq *gcwq = get_gcwq(cpu);
3511 struct task_struct *new_trustee = NULL;
3512 struct worker *uninitialized_var(new_worker);
3513 unsigned long flags;
3514
3515 action &= ~CPU_TASKS_FROZEN;
3516
3517 switch (action) {
3518 case CPU_DOWN_PREPARE:
3519 new_trustee = kthread_create(trustee_thread, gcwq,
3520 "workqueue_trustee/%d\n", cpu);
3521 if (IS_ERR(new_trustee))
3522 return notifier_from_errno(PTR_ERR(new_trustee));
3523 kthread_bind(new_trustee, cpu);
3524
3525 case CPU_UP_PREPARE:
3526 BUG_ON(gcwq->first_idle);
3527 new_worker = create_worker(gcwq, false);
3528 if (!new_worker) {
3529 if (new_trustee)
3530 kthread_stop(new_trustee);
3531 return NOTIFY_BAD;
3532 }
3533 }
3534
3535
3536 spin_lock_irqsave(&gcwq->lock, flags);
3537
3538 switch (action) {
3539 case CPU_DOWN_PREPARE:
3540
3541 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3542 gcwq->trustee = new_trustee;
3543 gcwq->trustee_state = TRUSTEE_START;
3544 wake_up_process(gcwq->trustee);
3545 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
3546
3547 case CPU_UP_PREPARE:
3548 BUG_ON(gcwq->first_idle);
3549 gcwq->first_idle = new_worker;
3550 break;
3551
3552 case CPU_DYING:
3553
3554
3555
3556
3557
3558
3559 gcwq->flags |= GCWQ_DISASSOCIATED;
3560 break;
3561
3562 case CPU_POST_DEAD:
3563 gcwq->trustee_state = TRUSTEE_BUTCHER;
3564
3565 case CPU_UP_CANCELED:
3566 destroy_worker(gcwq->first_idle);
3567 gcwq->first_idle = NULL;
3568 break;
3569
3570 case CPU_DOWN_FAILED:
3571 case CPU_ONLINE:
3572 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3573 if (gcwq->trustee_state != TRUSTEE_DONE) {
3574 gcwq->trustee_state = TRUSTEE_RELEASE;
3575 wake_up_process(gcwq->trustee);
3576 wait_trustee_state(gcwq, TRUSTEE_DONE);
3577 }
3578
3579
3580
3581
3582
3583
3584 spin_unlock_irq(&gcwq->lock);
3585 kthread_bind(gcwq->first_idle->task, cpu);
3586 spin_lock_irq(&gcwq->lock);
3587 gcwq->flags |= GCWQ_MANAGE_WORKERS;
3588 start_worker(gcwq->first_idle);
3589 gcwq->first_idle = NULL;
3590 break;
3591 }
3592
3593 spin_unlock_irqrestore(&gcwq->lock, flags);
3594
3595 return notifier_from_errno(0);
3596}
3597
3598
3599
3600
3601
3602static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3603 unsigned long action,
3604 void *hcpu)
3605{
3606 switch (action & ~CPU_TASKS_FROZEN) {
3607 case CPU_UP_PREPARE:
3608 case CPU_UP_CANCELED:
3609 case CPU_DOWN_FAILED:
3610 case CPU_ONLINE:
3611 return workqueue_cpu_callback(nfb, action, hcpu);
3612 }
3613 return NOTIFY_OK;
3614}
3615
3616
3617
3618
3619
3620static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3621 unsigned long action,
3622 void *hcpu)
3623{
3624 switch (action & ~CPU_TASKS_FROZEN) {
3625 case CPU_DOWN_PREPARE:
3626 case CPU_DYING:
3627 case CPU_POST_DEAD:
3628 return workqueue_cpu_callback(nfb, action, hcpu);
3629 }
3630 return NOTIFY_OK;
3631}
3632
3633#ifdef CONFIG_SMP
3634
3635struct work_for_cpu {
3636 struct work_struct work;
3637 long (*fn)(void *);
3638 void *arg;
3639 long ret;
3640};
3641
3642static void work_for_cpu_fn(struct work_struct *work)
3643{
3644 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3645
3646 wfc->ret = wfc->fn(wfc->arg);
3647}
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3660{
3661 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
3662
3663 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3664 schedule_work_on(cpu, &wfc.work);
3665 flush_work(&wfc.work);
3666 return wfc.ret;
3667}
3668EXPORT_SYMBOL_GPL(work_on_cpu);
3669#endif
3670
3671#ifdef CONFIG_FREEZER
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683void freeze_workqueues_begin(void)
3684{
3685 unsigned int cpu;
3686
3687 spin_lock(&workqueue_lock);
3688
3689 BUG_ON(workqueue_freezing);
3690 workqueue_freezing = true;
3691
3692 for_each_gcwq_cpu(cpu) {
3693 struct global_cwq *gcwq = get_gcwq(cpu);
3694 struct workqueue_struct *wq;
3695
3696 spin_lock_irq(&gcwq->lock);
3697
3698 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3699 gcwq->flags |= GCWQ_FREEZING;
3700
3701 list_for_each_entry(wq, &workqueues, list) {
3702 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3703
3704 if (cwq && wq->flags & WQ_FREEZABLE)
3705 cwq->max_active = 0;
3706 }
3707
3708 spin_unlock_irq(&gcwq->lock);
3709 }
3710
3711 spin_unlock(&workqueue_lock);
3712}
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727bool freeze_workqueues_busy(void)
3728{
3729 unsigned int cpu;
3730 bool busy = false;
3731
3732 spin_lock(&workqueue_lock);
3733
3734 BUG_ON(!workqueue_freezing);
3735
3736 for_each_gcwq_cpu(cpu) {
3737 struct workqueue_struct *wq;
3738
3739
3740
3741
3742 list_for_each_entry(wq, &workqueues, list) {
3743 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3744
3745 if (!cwq || !(wq->flags & WQ_FREEZABLE))
3746 continue;
3747
3748 BUG_ON(cwq->nr_active < 0);
3749 if (cwq->nr_active) {
3750 busy = true;
3751 goto out_unlock;
3752 }
3753 }
3754 }
3755out_unlock:
3756 spin_unlock(&workqueue_lock);
3757 return busy;
3758}
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769void thaw_workqueues(void)
3770{
3771 unsigned int cpu;
3772
3773 spin_lock(&workqueue_lock);
3774
3775 if (!workqueue_freezing)
3776 goto out_unlock;
3777
3778 for_each_gcwq_cpu(cpu) {
3779 struct global_cwq *gcwq = get_gcwq(cpu);
3780 struct workqueue_struct *wq;
3781
3782 spin_lock_irq(&gcwq->lock);
3783
3784 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3785 gcwq->flags &= ~GCWQ_FREEZING;
3786
3787 list_for_each_entry(wq, &workqueues, list) {
3788 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3789
3790 if (!cwq || !(wq->flags & WQ_FREEZABLE))
3791 continue;
3792
3793
3794 cwq->max_active = wq->saved_max_active;
3795
3796 while (!list_empty(&cwq->delayed_works) &&
3797 cwq->nr_active < cwq->max_active)
3798 cwq_activate_first_delayed(cwq);
3799 }
3800
3801 wake_up_worker(gcwq);
3802
3803 spin_unlock_irq(&gcwq->lock);
3804 }
3805
3806 workqueue_freezing = false;
3807out_unlock:
3808 spin_unlock(&workqueue_lock);
3809}
3810#endif
3811
3812static int __init init_workqueues(void)
3813{
3814 unsigned int cpu;
3815 int i;
3816
3817 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3818 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
3819
3820
3821 for_each_gcwq_cpu(cpu) {
3822 struct global_cwq *gcwq = get_gcwq(cpu);
3823
3824 spin_lock_init(&gcwq->lock);
3825 INIT_LIST_HEAD(&gcwq->worklist);
3826 gcwq->cpu = cpu;
3827 gcwq->flags |= GCWQ_DISASSOCIATED;
3828
3829 INIT_LIST_HEAD(&gcwq->idle_list);
3830 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3831 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3832
3833 init_timer_deferrable(&gcwq->idle_timer);
3834 gcwq->idle_timer.function = idle_worker_timeout;
3835 gcwq->idle_timer.data = (unsigned long)gcwq;
3836
3837 setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
3838 (unsigned long)gcwq);
3839
3840 ida_init(&gcwq->worker_ida);
3841
3842 gcwq->trustee_state = TRUSTEE_DONE;
3843 init_waitqueue_head(&gcwq->trustee_wait);
3844 }
3845
3846
3847 for_each_online_gcwq_cpu(cpu) {
3848 struct global_cwq *gcwq = get_gcwq(cpu);
3849 struct worker *worker;
3850
3851 if (cpu != WORK_CPU_UNBOUND)
3852 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3853 worker = create_worker(gcwq, true);
3854 BUG_ON(!worker);
3855 spin_lock_irq(&gcwq->lock);
3856 start_worker(worker);
3857 spin_unlock_irq(&gcwq->lock);
3858 }
3859
3860 system_wq = alloc_workqueue("events", 0, 0);
3861 system_long_wq = alloc_workqueue("events_long", 0, 0);
3862 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
3863 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3864 WQ_UNBOUND_MAX_ACTIVE);
3865 system_freezable_wq = alloc_workqueue("events_freezable",
3866 WQ_FREEZABLE, 0);
3867 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3868 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
3869 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
3870 !system_unbound_wq || !system_freezable_wq ||
3871 !system_nrt_freezable_wq);
3872 return 0;
3873}
3874early_initcall(init_workqueues);
3875