workqueue.c 19 KB

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