workqueue.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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_MUTEX(workqueue_mutex);
  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. mutex_lock(&workqueue_mutex);
  282. for_each_online_cpu(cpu)
  283. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  284. mutex_unlock(&workqueue_mutex);
  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. mutex_lock(&workqueue_mutex);
  326. if (singlethread) {
  327. INIT_LIST_HEAD(&wq->list);
  328. p = create_workqueue_thread(wq, singlethread_cpu);
  329. if (!p)
  330. destroy = 1;
  331. else
  332. wake_up_process(p);
  333. } else {
  334. list_add(&wq->list, &workqueues);
  335. for_each_online_cpu(cpu) {
  336. p = create_workqueue_thread(wq, cpu);
  337. if (p) {
  338. kthread_bind(p, cpu);
  339. wake_up_process(p);
  340. } else
  341. destroy = 1;
  342. }
  343. }
  344. mutex_unlock(&workqueue_mutex);
  345. /*
  346. * Was there any error during startup? If yes then clean up:
  347. */
  348. if (destroy) {
  349. destroy_workqueue(wq);
  350. wq = NULL;
  351. }
  352. return wq;
  353. }
  354. EXPORT_SYMBOL_GPL(__create_workqueue);
  355. static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
  356. {
  357. struct cpu_workqueue_struct *cwq;
  358. unsigned long flags;
  359. struct task_struct *p;
  360. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  361. spin_lock_irqsave(&cwq->lock, flags);
  362. p = cwq->thread;
  363. cwq->thread = NULL;
  364. spin_unlock_irqrestore(&cwq->lock, flags);
  365. if (p)
  366. kthread_stop(p);
  367. }
  368. /**
  369. * destroy_workqueue - safely terminate a workqueue
  370. * @wq: target workqueue
  371. *
  372. * Safely destroy a workqueue. All work currently pending will be done first.
  373. */
  374. void destroy_workqueue(struct workqueue_struct *wq)
  375. {
  376. int cpu;
  377. flush_workqueue(wq);
  378. /* We don't need the distraction of CPUs appearing and vanishing. */
  379. mutex_lock(&workqueue_mutex);
  380. if (is_single_threaded(wq))
  381. cleanup_workqueue_thread(wq, singlethread_cpu);
  382. else {
  383. for_each_online_cpu(cpu)
  384. cleanup_workqueue_thread(wq, cpu);
  385. list_del(&wq->list);
  386. }
  387. mutex_unlock(&workqueue_mutex);
  388. free_percpu(wq->cpu_wq);
  389. kfree(wq);
  390. }
  391. EXPORT_SYMBOL_GPL(destroy_workqueue);
  392. static struct workqueue_struct *keventd_wq;
  393. /**
  394. * schedule_work - put work task in global workqueue
  395. * @work: job to be done
  396. *
  397. * This puts a job in the kernel-global workqueue.
  398. */
  399. int fastcall schedule_work(struct work_struct *work)
  400. {
  401. return queue_work(keventd_wq, work);
  402. }
  403. EXPORT_SYMBOL(schedule_work);
  404. /**
  405. * schedule_delayed_work - put work task in global workqueue after delay
  406. * @work: job to be done
  407. * @delay: number of jiffies to wait
  408. *
  409. * After waiting for a given time this puts a job in the kernel-global
  410. * workqueue.
  411. */
  412. int fastcall schedule_delayed_work(struct work_struct *work, unsigned long delay)
  413. {
  414. return queue_delayed_work(keventd_wq, work, delay);
  415. }
  416. EXPORT_SYMBOL(schedule_delayed_work);
  417. /**
  418. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  419. * @cpu: cpu to use
  420. * @work: job to be done
  421. * @delay: number of jiffies to wait
  422. *
  423. * After waiting for a given time this puts a job in the kernel-global
  424. * workqueue on the specified CPU.
  425. */
  426. int schedule_delayed_work_on(int cpu,
  427. struct work_struct *work, unsigned long delay)
  428. {
  429. return queue_delayed_work_on(cpu, keventd_wq, work, delay);
  430. }
  431. EXPORT_SYMBOL(schedule_delayed_work_on);
  432. /**
  433. * schedule_on_each_cpu - call a function on each online CPU from keventd
  434. * @func: the function to call
  435. * @info: a pointer to pass to func()
  436. *
  437. * Returns zero on success.
  438. * Returns -ve errno on failure.
  439. *
  440. * Appears to be racy against CPU hotplug.
  441. *
  442. * schedule_on_each_cpu() is very slow.
  443. */
  444. int schedule_on_each_cpu(void (*func)(void *info), void *info)
  445. {
  446. int cpu;
  447. struct work_struct *works;
  448. works = alloc_percpu(struct work_struct);
  449. if (!works)
  450. return -ENOMEM;
  451. mutex_lock(&workqueue_mutex);
  452. for_each_online_cpu(cpu) {
  453. INIT_WORK(per_cpu_ptr(works, cpu), func, info);
  454. __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu),
  455. per_cpu_ptr(works, cpu));
  456. }
  457. mutex_unlock(&workqueue_mutex);
  458. flush_workqueue(keventd_wq);
  459. free_percpu(works);
  460. return 0;
  461. }
  462. void flush_scheduled_work(void)
  463. {
  464. flush_workqueue(keventd_wq);
  465. }
  466. EXPORT_SYMBOL(flush_scheduled_work);
  467. /**
  468. * cancel_rearming_delayed_workqueue - reliably kill off a delayed
  469. * work whose handler rearms the delayed work.
  470. * @wq: the controlling workqueue structure
  471. * @work: the delayed work struct
  472. */
  473. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  474. struct work_struct *work)
  475. {
  476. while (!cancel_delayed_work(work))
  477. flush_workqueue(wq);
  478. }
  479. EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
  480. /**
  481. * cancel_rearming_delayed_work - reliably kill off a delayed keventd
  482. * work whose handler rearms the delayed work.
  483. * @work: the delayed work struct
  484. */
  485. void cancel_rearming_delayed_work(struct work_struct *work)
  486. {
  487. cancel_rearming_delayed_workqueue(keventd_wq, work);
  488. }
  489. EXPORT_SYMBOL(cancel_rearming_delayed_work);
  490. /**
  491. * execute_in_process_context - reliably execute the routine with user context
  492. * @fn: the function to execute
  493. * @data: data to pass to the function
  494. * @ew: guaranteed storage for the execute work structure (must
  495. * be available when the work executes)
  496. *
  497. * Executes the function immediately if process context is available,
  498. * otherwise schedules the function for delayed execution.
  499. *
  500. * Returns: 0 - function was executed
  501. * 1 - function was scheduled for execution
  502. */
  503. int execute_in_process_context(void (*fn)(void *data), void *data,
  504. struct execute_work *ew)
  505. {
  506. if (!in_interrupt()) {
  507. fn(data);
  508. return 0;
  509. }
  510. INIT_WORK(&ew->work, fn, data);
  511. schedule_work(&ew->work);
  512. return 1;
  513. }
  514. EXPORT_SYMBOL_GPL(execute_in_process_context);
  515. int keventd_up(void)
  516. {
  517. return keventd_wq != NULL;
  518. }
  519. int current_is_keventd(void)
  520. {
  521. struct cpu_workqueue_struct *cwq;
  522. int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  523. int ret = 0;
  524. BUG_ON(!keventd_wq);
  525. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  526. if (current == cwq->thread)
  527. ret = 1;
  528. return ret;
  529. }
  530. #ifdef CONFIG_HOTPLUG_CPU
  531. /* Take the work from this (downed) CPU. */
  532. static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
  533. {
  534. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  535. struct list_head list;
  536. struct work_struct *work;
  537. spin_lock_irq(&cwq->lock);
  538. list_replace_init(&cwq->worklist, &list);
  539. while (!list_empty(&list)) {
  540. printk("Taking work for %s\n", wq->name);
  541. work = list_entry(list.next,struct work_struct,entry);
  542. list_del(&work->entry);
  543. __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
  544. }
  545. spin_unlock_irq(&cwq->lock);
  546. }
  547. /* We're holding the cpucontrol mutex here */
  548. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  549. unsigned long action,
  550. void *hcpu)
  551. {
  552. unsigned int hotcpu = (unsigned long)hcpu;
  553. struct workqueue_struct *wq;
  554. switch (action) {
  555. case CPU_UP_PREPARE:
  556. mutex_lock(&workqueue_mutex);
  557. /* Create a new workqueue thread for it. */
  558. list_for_each_entry(wq, &workqueues, list) {
  559. if (!create_workqueue_thread(wq, hotcpu)) {
  560. printk("workqueue for %i failed\n", hotcpu);
  561. return NOTIFY_BAD;
  562. }
  563. }
  564. break;
  565. case CPU_ONLINE:
  566. /* Kick off worker threads. */
  567. list_for_each_entry(wq, &workqueues, list) {
  568. struct cpu_workqueue_struct *cwq;
  569. cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
  570. kthread_bind(cwq->thread, hotcpu);
  571. wake_up_process(cwq->thread);
  572. }
  573. mutex_unlock(&workqueue_mutex);
  574. break;
  575. case CPU_UP_CANCELED:
  576. list_for_each_entry(wq, &workqueues, list) {
  577. if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
  578. continue;
  579. /* Unbind so it can run. */
  580. kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
  581. any_online_cpu(cpu_online_map));
  582. cleanup_workqueue_thread(wq, hotcpu);
  583. }
  584. mutex_unlock(&workqueue_mutex);
  585. break;
  586. case CPU_DOWN_PREPARE:
  587. mutex_lock(&workqueue_mutex);
  588. break;
  589. case CPU_DOWN_FAILED:
  590. mutex_unlock(&workqueue_mutex);
  591. break;
  592. case CPU_DEAD:
  593. list_for_each_entry(wq, &workqueues, list)
  594. cleanup_workqueue_thread(wq, hotcpu);
  595. list_for_each_entry(wq, &workqueues, list)
  596. take_over_work(wq, hotcpu);
  597. mutex_unlock(&workqueue_mutex);
  598. break;
  599. }
  600. return NOTIFY_OK;
  601. }
  602. #endif
  603. void init_workqueues(void)
  604. {
  605. singlethread_cpu = first_cpu(cpu_possible_map);
  606. hotcpu_notifier(workqueue_cpu_callback, 0);
  607. keventd_wq = create_workqueue("events");
  608. BUG_ON(!keventd_wq);
  609. }