workqueue.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * linux/kernel/workqueue.c
  3. *
  4. * Generic mechanism for defining kernel helper threads for running
  5. * arbitrary tasks in process context.
  6. *
  7. * Started by Ingo Molnar, Copyright (C) 2002
  8. *
  9. * Derived from the taskqueue/keventd code by:
  10. *
  11. * David Woodhouse <dwmw2@infradead.org>
  12. * Andrew Morton <andrewm@uow.edu.au>
  13. * Kai Petzke <wpp@marie.physik.tu-berlin.de>
  14. * Theodore Ts'o <tytso@mit.edu>
  15. *
  16. * Made to use alloc_percpu by Christoph Lameter <clameter@sgi.com>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/init.h>
  22. #include <linux/signal.h>
  23. #include <linux/completion.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/slab.h>
  26. #include <linux/cpu.h>
  27. #include <linux/notifier.h>
  28. #include <linux/kthread.h>
  29. #include <linux/hardirq.h>
  30. /*
  31. * The per-CPU workqueue (if single thread, we always use the first
  32. * possible cpu).
  33. *
  34. * The sequence counters are for flush_scheduled_work(). It wants to wait
  35. * until until all currently-scheduled works are completed, but it doesn't
  36. * want to be livelocked by new, incoming ones. So it waits until
  37. * remove_sequence is >= the insert_sequence which pertained when
  38. * flush_scheduled_work() was called.
  39. */
  40. struct cpu_workqueue_struct {
  41. spinlock_t lock;
  42. long remove_sequence; /* Least-recently added (next to run) */
  43. long insert_sequence; /* Next to add */
  44. struct list_head worklist;
  45. wait_queue_head_t more_work;
  46. wait_queue_head_t work_done;
  47. struct workqueue_struct *wq;
  48. struct task_struct *thread;
  49. int run_depth; /* Detect run_workqueue() recursion depth */
  50. } ____cacheline_aligned;
  51. /*
  52. * The externally visible workqueue abstraction is an array of
  53. * per-CPU workqueues:
  54. */
  55. struct workqueue_struct {
  56. struct cpu_workqueue_struct *cpu_wq;
  57. const char *name;
  58. struct list_head list; /* Empty if single thread */
  59. };
  60. /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
  61. threads to each one as cpus come/go. */
  62. static DEFINE_SPINLOCK(workqueue_lock);
  63. static LIST_HEAD(workqueues);
  64. static int singlethread_cpu;
  65. /* If it's single threaded, it isn't in the list of workqueues. */
  66. static inline int is_single_threaded(struct workqueue_struct *wq)
  67. {
  68. return list_empty(&wq->list);
  69. }
  70. /* Preempt must be disabled. */
  71. static void __queue_work(struct cpu_workqueue_struct *cwq,
  72. struct work_struct *work)
  73. {
  74. unsigned long flags;
  75. spin_lock_irqsave(&cwq->lock, flags);
  76. work->wq_data = cwq;
  77. list_add_tail(&work->entry, &cwq->worklist);
  78. cwq->insert_sequence++;
  79. wake_up(&cwq->more_work);
  80. spin_unlock_irqrestore(&cwq->lock, flags);
  81. }
  82. /**
  83. * queue_work - queue work on a workqueue
  84. * @wq: workqueue to use
  85. * @work: work to queue
  86. *
  87. * Returns non-zero if it was successfully added.
  88. *
  89. * We queue the work to the CPU it was submitted, but there is no
  90. * guarantee that it will be processed by that CPU.
  91. */
  92. int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
  93. {
  94. int ret = 0, cpu = get_cpu();
  95. if (!test_and_set_bit(0, &work->pending)) {
  96. if (unlikely(is_single_threaded(wq)))
  97. cpu = singlethread_cpu;
  98. BUG_ON(!list_empty(&work->entry));
  99. __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  100. ret = 1;
  101. }
  102. put_cpu();
  103. return ret;
  104. }
  105. EXPORT_SYMBOL_GPL(queue_work);
  106. static void delayed_work_timer_fn(unsigned long __data)
  107. {
  108. struct work_struct *work = (struct work_struct *)__data;
  109. struct workqueue_struct *wq = work->wq_data;
  110. int cpu = smp_processor_id();
  111. if (unlikely(is_single_threaded(wq)))
  112. cpu = singlethread_cpu;
  113. __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  114. }
  115. /**
  116. * queue_delayed_work - queue work on a workqueue after delay
  117. * @wq: workqueue to use
  118. * @work: work to queue
  119. * @delay: number of jiffies to wait before queueing
  120. *
  121. * Returns non-zero if it was successfully added.
  122. */
  123. int fastcall queue_delayed_work(struct workqueue_struct *wq,
  124. struct work_struct *work, unsigned long delay)
  125. {
  126. int ret = 0;
  127. struct timer_list *timer = &work->timer;
  128. if (!test_and_set_bit(0, &work->pending)) {
  129. BUG_ON(timer_pending(timer));
  130. BUG_ON(!list_empty(&work->entry));
  131. /* This stores wq for the moment, for the timer_fn */
  132. work->wq_data = wq;
  133. timer->expires = jiffies + delay;
  134. timer->data = (unsigned long)work;
  135. timer->function = delayed_work_timer_fn;
  136. add_timer(timer);
  137. ret = 1;
  138. }
  139. return ret;
  140. }
  141. EXPORT_SYMBOL_GPL(queue_delayed_work);
  142. /**
  143. * queue_delayed_work_on - queue work on specific CPU after delay
  144. * @cpu: CPU number to execute work on
  145. * @wq: workqueue to use
  146. * @work: work to queue
  147. * @delay: number of jiffies to wait before queueing
  148. *
  149. * Returns non-zero if it was successfully added.
  150. */
  151. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  152. struct work_struct *work, unsigned long delay)
  153. {
  154. int ret = 0;
  155. struct timer_list *timer = &work->timer;
  156. if (!test_and_set_bit(0, &work->pending)) {
  157. BUG_ON(timer_pending(timer));
  158. BUG_ON(!list_empty(&work->entry));
  159. /* This stores wq for the moment, for the timer_fn */
  160. work->wq_data = wq;
  161. timer->expires = jiffies + delay;
  162. timer->data = (unsigned long)work;
  163. timer->function = delayed_work_timer_fn;
  164. add_timer_on(timer, cpu);
  165. ret = 1;
  166. }
  167. return ret;
  168. }
  169. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  170. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  171. {
  172. unsigned long flags;
  173. /*
  174. * Keep taking off work from the queue until
  175. * done.
  176. */
  177. spin_lock_irqsave(&cwq->lock, flags);
  178. cwq->run_depth++;
  179. if (cwq->run_depth > 3) {
  180. /* morton gets to eat his hat */
  181. printk("%s: recursion depth exceeded: %d\n",
  182. __FUNCTION__, cwq->run_depth);
  183. dump_stack();
  184. }
  185. while (!list_empty(&cwq->worklist)) {
  186. struct work_struct *work = list_entry(cwq->worklist.next,
  187. struct work_struct, entry);
  188. void (*f) (void *) = work->func;
  189. void *data = work->data;
  190. list_del_init(cwq->worklist.next);
  191. spin_unlock_irqrestore(&cwq->lock, flags);
  192. BUG_ON(work->wq_data != cwq);
  193. clear_bit(0, &work->pending);
  194. f(data);
  195. spin_lock_irqsave(&cwq->lock, flags);
  196. cwq->remove_sequence++;
  197. wake_up(&cwq->work_done);
  198. }
  199. cwq->run_depth--;
  200. spin_unlock_irqrestore(&cwq->lock, flags);
  201. }
  202. static int worker_thread(void *__cwq)
  203. {
  204. struct cpu_workqueue_struct *cwq = __cwq;
  205. DECLARE_WAITQUEUE(wait, current);
  206. struct k_sigaction sa;
  207. sigset_t blocked;
  208. current->flags |= PF_NOFREEZE;
  209. set_user_nice(current, -5);
  210. /* Block and flush all signals */
  211. sigfillset(&blocked);
  212. sigprocmask(SIG_BLOCK, &blocked, NULL);
  213. flush_signals(current);
  214. /* SIG_IGN makes children autoreap: see do_notify_parent(). */
  215. sa.sa.sa_handler = SIG_IGN;
  216. sa.sa.sa_flags = 0;
  217. siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
  218. do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
  219. set_current_state(TASK_INTERRUPTIBLE);
  220. while (!kthread_should_stop()) {
  221. add_wait_queue(&cwq->more_work, &wait);
  222. if (list_empty(&cwq->worklist))
  223. schedule();
  224. else
  225. __set_current_state(TASK_RUNNING);
  226. remove_wait_queue(&cwq->more_work, &wait);
  227. if (!list_empty(&cwq->worklist))
  228. run_workqueue(cwq);
  229. set_current_state(TASK_INTERRUPTIBLE);
  230. }
  231. __set_current_state(TASK_RUNNING);
  232. return 0;
  233. }
  234. static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  235. {
  236. if (cwq->thread == current) {
  237. /*
  238. * Probably keventd trying to flush its own queue. So simply run
  239. * it by hand rather than deadlocking.
  240. */
  241. run_workqueue(cwq);
  242. } else {
  243. DEFINE_WAIT(wait);
  244. long sequence_needed;
  245. spin_lock_irq(&cwq->lock);
  246. sequence_needed = cwq->insert_sequence;
  247. while (sequence_needed - cwq->remove_sequence > 0) {
  248. prepare_to_wait(&cwq->work_done, &wait,
  249. TASK_UNINTERRUPTIBLE);
  250. spin_unlock_irq(&cwq->lock);
  251. schedule();
  252. spin_lock_irq(&cwq->lock);
  253. }
  254. finish_wait(&cwq->work_done, &wait);
  255. spin_unlock_irq(&cwq->lock);
  256. }
  257. }
  258. /**
  259. * flush_workqueue - ensure that any scheduled work has run to completion.
  260. * @wq: workqueue to flush
  261. *
  262. * Forces execution of the workqueue and blocks until its completion.
  263. * This is typically used in driver shutdown handlers.
  264. *
  265. * This function will sample each workqueue's current insert_sequence number and
  266. * will sleep until the head sequence is greater than or equal to that. This
  267. * means that we sleep until all works which were queued on entry have been
  268. * handled, but we are not livelocked by new incoming ones.
  269. *
  270. * This function used to run the workqueues itself. Now we just wait for the
  271. * helper threads to do it.
  272. */
  273. void fastcall flush_workqueue(struct workqueue_struct *wq)
  274. {
  275. might_sleep();
  276. if (is_single_threaded(wq)) {
  277. /* Always use first cpu's area. */
  278. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
  279. } else {
  280. int cpu;
  281. lock_cpu_hotplug();
  282. for_each_online_cpu(cpu)
  283. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  284. unlock_cpu_hotplug();
  285. }
  286. }
  287. EXPORT_SYMBOL_GPL(flush_workqueue);
  288. static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
  289. int cpu)
  290. {
  291. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  292. struct task_struct *p;
  293. spin_lock_init(&cwq->lock);
  294. cwq->wq = wq;
  295. cwq->thread = NULL;
  296. cwq->insert_sequence = 0;
  297. cwq->remove_sequence = 0;
  298. INIT_LIST_HEAD(&cwq->worklist);
  299. init_waitqueue_head(&cwq->more_work);
  300. init_waitqueue_head(&cwq->work_done);
  301. if (is_single_threaded(wq))
  302. p = kthread_create(worker_thread, cwq, "%s", wq->name);
  303. else
  304. p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
  305. if (IS_ERR(p))
  306. return NULL;
  307. cwq->thread = p;
  308. return p;
  309. }
  310. struct workqueue_struct *__create_workqueue(const char *name,
  311. int singlethread)
  312. {
  313. int cpu, destroy = 0;
  314. struct workqueue_struct *wq;
  315. struct task_struct *p;
  316. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  317. if (!wq)
  318. return NULL;
  319. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  320. if (!wq->cpu_wq) {
  321. kfree(wq);
  322. return NULL;
  323. }
  324. wq->name = name;
  325. /* We don't need the distraction of CPUs appearing and vanishing. */
  326. lock_cpu_hotplug();
  327. if (singlethread) {
  328. INIT_LIST_HEAD(&wq->list);
  329. p = create_workqueue_thread(wq, singlethread_cpu);
  330. if (!p)
  331. destroy = 1;
  332. else
  333. wake_up_process(p);
  334. } else {
  335. spin_lock(&workqueue_lock);
  336. list_add(&wq->list, &workqueues);
  337. spin_unlock(&workqueue_lock);
  338. for_each_online_cpu(cpu) {
  339. p = create_workqueue_thread(wq, cpu);
  340. if (p) {
  341. kthread_bind(p, cpu);
  342. wake_up_process(p);
  343. } else
  344. destroy = 1;
  345. }
  346. }
  347. unlock_cpu_hotplug();
  348. /*
  349. * Was there any error during startup? If yes then clean up:
  350. */
  351. if (destroy) {
  352. destroy_workqueue(wq);
  353. wq = NULL;
  354. }
  355. return wq;
  356. }
  357. EXPORT_SYMBOL_GPL(__create_workqueue);
  358. static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
  359. {
  360. struct cpu_workqueue_struct *cwq;
  361. unsigned long flags;
  362. struct task_struct *p;
  363. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  364. spin_lock_irqsave(&cwq->lock, flags);
  365. p = cwq->thread;
  366. cwq->thread = NULL;
  367. spin_unlock_irqrestore(&cwq->lock, flags);
  368. if (p)
  369. kthread_stop(p);
  370. }
  371. /**
  372. * destroy_workqueue - safely terminate a workqueue
  373. * @wq: target workqueue
  374. *
  375. * Safely destroy a workqueue. All work currently pending will be done first.
  376. */
  377. void destroy_workqueue(struct workqueue_struct *wq)
  378. {
  379. int cpu;
  380. flush_workqueue(wq);
  381. /* We don't need the distraction of CPUs appearing and vanishing. */
  382. lock_cpu_hotplug();
  383. if (is_single_threaded(wq))
  384. cleanup_workqueue_thread(wq, singlethread_cpu);
  385. else {
  386. for_each_online_cpu(cpu)
  387. cleanup_workqueue_thread(wq, cpu);
  388. spin_lock(&workqueue_lock);
  389. list_del(&wq->list);
  390. spin_unlock(&workqueue_lock);
  391. }
  392. unlock_cpu_hotplug();
  393. free_percpu(wq->cpu_wq);
  394. kfree(wq);
  395. }
  396. EXPORT_SYMBOL_GPL(destroy_workqueue);
  397. static struct workqueue_struct *keventd_wq;
  398. /**
  399. * schedule_work - put work task in global workqueue
  400. * @work: job to be done
  401. *
  402. * This puts a job in the kernel-global workqueue.
  403. */
  404. int fastcall schedule_work(struct work_struct *work)
  405. {
  406. return queue_work(keventd_wq, work);
  407. }
  408. EXPORT_SYMBOL(schedule_work);
  409. /**
  410. * schedule_delayed_work - put work task in global workqueue after delay
  411. * @work: job to be done
  412. * @delay: number of jiffies to wait
  413. *
  414. * After waiting for a given time this puts a job in the kernel-global
  415. * workqueue.
  416. */
  417. int fastcall schedule_delayed_work(struct work_struct *work, unsigned long delay)
  418. {
  419. return queue_delayed_work(keventd_wq, work, delay);
  420. }
  421. EXPORT_SYMBOL(schedule_delayed_work);
  422. /**
  423. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  424. * @cpu: cpu to use
  425. * @work: job to be done
  426. * @delay: number of jiffies to wait
  427. *
  428. * After waiting for a given time this puts a job in the kernel-global
  429. * workqueue on the specified CPU.
  430. */
  431. int schedule_delayed_work_on(int cpu,
  432. struct work_struct *work, unsigned long delay)
  433. {
  434. return queue_delayed_work_on(cpu, keventd_wq, work, delay);
  435. }
  436. EXPORT_SYMBOL(schedule_delayed_work_on);
  437. /**
  438. * schedule_on_each_cpu - call a function on each online CPU from keventd
  439. * @func: the function to call
  440. * @info: a pointer to pass to func()
  441. *
  442. * Returns zero on success.
  443. * Returns -ve errno on failure.
  444. *
  445. * Appears to be racy against CPU hotplug.
  446. *
  447. * schedule_on_each_cpu() is very slow.
  448. */
  449. int schedule_on_each_cpu(void (*func)(void *info), void *info)
  450. {
  451. int cpu;
  452. struct work_struct *works;
  453. works = alloc_percpu(struct work_struct);
  454. if (!works)
  455. return -ENOMEM;
  456. for_each_online_cpu(cpu) {
  457. INIT_WORK(per_cpu_ptr(works, cpu), func, info);
  458. __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu),
  459. per_cpu_ptr(works, cpu));
  460. }
  461. flush_workqueue(keventd_wq);
  462. free_percpu(works);
  463. return 0;
  464. }
  465. void flush_scheduled_work(void)
  466. {
  467. flush_workqueue(keventd_wq);
  468. }
  469. EXPORT_SYMBOL(flush_scheduled_work);
  470. /**
  471. * cancel_rearming_delayed_workqueue - reliably kill off a delayed
  472. * work whose handler rearms the delayed work.
  473. * @wq: the controlling workqueue structure
  474. * @work: the delayed work struct
  475. */
  476. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  477. struct work_struct *work)
  478. {
  479. while (!cancel_delayed_work(work))
  480. flush_workqueue(wq);
  481. }
  482. EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
  483. /**
  484. * cancel_rearming_delayed_work - reliably kill off a delayed keventd
  485. * work whose handler rearms the delayed work.
  486. * @work: the delayed work struct
  487. */
  488. void cancel_rearming_delayed_work(struct work_struct *work)
  489. {
  490. cancel_rearming_delayed_workqueue(keventd_wq, work);
  491. }
  492. EXPORT_SYMBOL(cancel_rearming_delayed_work);
  493. /**
  494. * execute_in_process_context - reliably execute the routine with user context
  495. * @fn: the function to execute
  496. * @data: data to pass to the function
  497. * @ew: guaranteed storage for the execute work structure (must
  498. * be available when the work executes)
  499. *
  500. * Executes the function immediately if process context is available,
  501. * otherwise schedules the function for delayed execution.
  502. *
  503. * Returns: 0 - function was executed
  504. * 1 - function was scheduled for execution
  505. */
  506. int execute_in_process_context(void (*fn)(void *data), void *data,
  507. struct execute_work *ew)
  508. {
  509. if (!in_interrupt()) {
  510. fn(data);
  511. return 0;
  512. }
  513. INIT_WORK(&ew->work, fn, data);
  514. schedule_work(&ew->work);
  515. return 1;
  516. }
  517. EXPORT_SYMBOL_GPL(execute_in_process_context);
  518. int keventd_up(void)
  519. {
  520. return keventd_wq != NULL;
  521. }
  522. int current_is_keventd(void)
  523. {
  524. struct cpu_workqueue_struct *cwq;
  525. int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  526. int ret = 0;
  527. BUG_ON(!keventd_wq);
  528. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  529. if (current == cwq->thread)
  530. ret = 1;
  531. return ret;
  532. }
  533. #ifdef CONFIG_HOTPLUG_CPU
  534. /* Take the work from this (downed) CPU. */
  535. static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
  536. {
  537. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  538. struct list_head list;
  539. struct work_struct *work;
  540. spin_lock_irq(&cwq->lock);
  541. list_replace_init(&cwq->worklist, &list);
  542. while (!list_empty(&list)) {
  543. printk("Taking work for %s\n", wq->name);
  544. work = list_entry(list.next,struct work_struct,entry);
  545. list_del(&work->entry);
  546. __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
  547. }
  548. spin_unlock_irq(&cwq->lock);
  549. }
  550. /* We're holding the cpucontrol mutex here */
  551. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  552. unsigned long action,
  553. void *hcpu)
  554. {
  555. unsigned int hotcpu = (unsigned long)hcpu;
  556. struct workqueue_struct *wq;
  557. switch (action) {
  558. case CPU_UP_PREPARE:
  559. /* Create a new workqueue thread for it. */
  560. list_for_each_entry(wq, &workqueues, list) {
  561. if (!create_workqueue_thread(wq, hotcpu)) {
  562. printk("workqueue for %i failed\n", hotcpu);
  563. return NOTIFY_BAD;
  564. }
  565. }
  566. break;
  567. case CPU_ONLINE:
  568. /* Kick off worker threads. */
  569. list_for_each_entry(wq, &workqueues, list) {
  570. struct cpu_workqueue_struct *cwq;
  571. cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
  572. kthread_bind(cwq->thread, hotcpu);
  573. wake_up_process(cwq->thread);
  574. }
  575. break;
  576. case CPU_UP_CANCELED:
  577. list_for_each_entry(wq, &workqueues, list) {
  578. if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
  579. continue;
  580. /* Unbind so it can run. */
  581. kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
  582. any_online_cpu(cpu_online_map));
  583. cleanup_workqueue_thread(wq, hotcpu);
  584. }
  585. break;
  586. case CPU_DEAD:
  587. list_for_each_entry(wq, &workqueues, list)
  588. cleanup_workqueue_thread(wq, hotcpu);
  589. list_for_each_entry(wq, &workqueues, list)
  590. take_over_work(wq, hotcpu);
  591. break;
  592. }
  593. return NOTIFY_OK;
  594. }
  595. #endif
  596. void init_workqueues(void)
  597. {
  598. singlethread_cpu = first_cpu(cpu_possible_map);
  599. hotcpu_notifier(workqueue_cpu_callback, 0);
  600. keventd_wq = create_workqueue("events");
  601. BUG_ON(!keventd_wq);
  602. }