workqueue.c 18 KB

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