workqueue.c 19 KB

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