workqueue.c 18 KB

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