workqueue.c 21 KB

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