workqueue.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. /*
  107. * Ensure that we get the right work->data if we see the
  108. * result of list_add() below, see try_to_grab_pending().
  109. */
  110. smp_wmb();
  111. if (tail)
  112. list_add_tail(&work->entry, &cwq->worklist);
  113. else
  114. list_add(&work->entry, &cwq->worklist);
  115. wake_up(&cwq->more_work);
  116. }
  117. /* Preempt must be disabled. */
  118. static void __queue_work(struct cpu_workqueue_struct *cwq,
  119. struct work_struct *work)
  120. {
  121. unsigned long flags;
  122. spin_lock_irqsave(&cwq->lock, flags);
  123. insert_work(cwq, work, 1);
  124. spin_unlock_irqrestore(&cwq->lock, flags);
  125. }
  126. /**
  127. * queue_work - queue work on a workqueue
  128. * @wq: workqueue to use
  129. * @work: work to queue
  130. *
  131. * Returns 0 if @work was already on a queue, non-zero otherwise.
  132. *
  133. * We queue the work to the CPU it was submitted, but there is no
  134. * guarantee that it will be processed by that CPU.
  135. */
  136. int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
  137. {
  138. int ret = 0;
  139. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  140. BUG_ON(!list_empty(&work->entry));
  141. __queue_work(wq_per_cpu(wq, get_cpu()), work);
  142. put_cpu();
  143. ret = 1;
  144. }
  145. return ret;
  146. }
  147. EXPORT_SYMBOL_GPL(queue_work);
  148. void delayed_work_timer_fn(unsigned long __data)
  149. {
  150. struct delayed_work *dwork = (struct delayed_work *)__data;
  151. struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
  152. struct workqueue_struct *wq = cwq->wq;
  153. __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
  154. }
  155. /**
  156. * queue_delayed_work - queue work on a workqueue after delay
  157. * @wq: workqueue to use
  158. * @dwork: delayable work to queue
  159. * @delay: number of jiffies to wait before queueing
  160. *
  161. * Returns 0 if @work was already on a queue, non-zero otherwise.
  162. */
  163. int fastcall queue_delayed_work(struct workqueue_struct *wq,
  164. struct delayed_work *dwork, unsigned long delay)
  165. {
  166. timer_stats_timer_set_start_info(&dwork->timer);
  167. if (delay == 0)
  168. return queue_work(wq, &dwork->work);
  169. return queue_delayed_work_on(-1, wq, dwork, delay);
  170. }
  171. EXPORT_SYMBOL_GPL(queue_delayed_work);
  172. /**
  173. * queue_delayed_work_on - queue work on specific CPU after delay
  174. * @cpu: CPU number to execute work on
  175. * @wq: workqueue to use
  176. * @dwork: work to queue
  177. * @delay: number of jiffies to wait before queueing
  178. *
  179. * Returns 0 if @work was already on a queue, non-zero otherwise.
  180. */
  181. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  182. struct delayed_work *dwork, unsigned long delay)
  183. {
  184. int ret = 0;
  185. struct timer_list *timer = &dwork->timer;
  186. struct work_struct *work = &dwork->work;
  187. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  188. BUG_ON(timer_pending(timer));
  189. BUG_ON(!list_empty(&work->entry));
  190. /* This stores cwq for the moment, for the timer_fn */
  191. set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
  192. timer->expires = jiffies + delay;
  193. timer->data = (unsigned long)dwork;
  194. timer->function = delayed_work_timer_fn;
  195. if (unlikely(cpu >= 0))
  196. add_timer_on(timer, cpu);
  197. else
  198. add_timer(timer);
  199. ret = 1;
  200. }
  201. return ret;
  202. }
  203. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  204. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  205. {
  206. spin_lock_irq(&cwq->lock);
  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. cwq->current_work = work;
  219. list_del_init(cwq->worklist.next);
  220. spin_unlock_irq(&cwq->lock);
  221. BUG_ON(get_wq_data(work) != cwq);
  222. work_clear_pending(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_irq(&cwq->lock);
  235. cwq->current_work = NULL;
  236. }
  237. cwq->run_depth--;
  238. spin_unlock_irq(&cwq->lock);
  239. }
  240. /*
  241. * NOTE: the caller must not touch *cwq if this func returns true
  242. */
  243. static int cwq_should_stop(struct cpu_workqueue_struct *cwq)
  244. {
  245. int should_stop = cwq->should_stop;
  246. if (unlikely(should_stop)) {
  247. spin_lock_irq(&cwq->lock);
  248. should_stop = cwq->should_stop && list_empty(&cwq->worklist);
  249. if (should_stop)
  250. cwq->thread = NULL;
  251. spin_unlock_irq(&cwq->lock);
  252. }
  253. return should_stop;
  254. }
  255. static int worker_thread(void *__cwq)
  256. {
  257. struct cpu_workqueue_struct *cwq = __cwq;
  258. DEFINE_WAIT(wait);
  259. if (!cwq->wq->freezeable)
  260. current->flags |= PF_NOFREEZE;
  261. set_user_nice(current, -5);
  262. for (;;) {
  263. prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
  264. if (!freezing(current) && !cwq->should_stop
  265. && list_empty(&cwq->worklist))
  266. schedule();
  267. finish_wait(&cwq->more_work, &wait);
  268. try_to_freeze();
  269. if (cwq_should_stop(cwq))
  270. break;
  271. run_workqueue(cwq);
  272. }
  273. return 0;
  274. }
  275. struct wq_barrier {
  276. struct work_struct work;
  277. struct completion done;
  278. };
  279. static void wq_barrier_func(struct work_struct *work)
  280. {
  281. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  282. complete(&barr->done);
  283. }
  284. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  285. struct wq_barrier *barr, int tail)
  286. {
  287. INIT_WORK(&barr->work, wq_barrier_func);
  288. __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
  289. init_completion(&barr->done);
  290. insert_work(cwq, &barr->work, tail);
  291. }
  292. static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  293. {
  294. if (cwq->thread == current) {
  295. /*
  296. * Probably keventd trying to flush its own queue. So simply run
  297. * it by hand rather than deadlocking.
  298. */
  299. run_workqueue(cwq);
  300. } else {
  301. struct wq_barrier barr;
  302. int active = 0;
  303. spin_lock_irq(&cwq->lock);
  304. if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
  305. insert_wq_barrier(cwq, &barr, 1);
  306. active = 1;
  307. }
  308. spin_unlock_irq(&cwq->lock);
  309. if (active)
  310. wait_for_completion(&barr.done);
  311. }
  312. }
  313. /**
  314. * flush_workqueue - ensure that any scheduled work has run to completion.
  315. * @wq: workqueue to flush
  316. *
  317. * Forces execution of the workqueue and blocks until its completion.
  318. * This is typically used in driver shutdown handlers.
  319. *
  320. * We sleep until all works which were queued on entry have been handled,
  321. * but we are not livelocked by new incoming ones.
  322. *
  323. * This function used to run the workqueues itself. Now we just wait for the
  324. * helper threads to do it.
  325. */
  326. void fastcall flush_workqueue(struct workqueue_struct *wq)
  327. {
  328. const cpumask_t *cpu_map = wq_cpu_map(wq);
  329. int cpu;
  330. might_sleep();
  331. for_each_cpu_mask(cpu, *cpu_map)
  332. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  333. }
  334. EXPORT_SYMBOL_GPL(flush_workqueue);
  335. /*
  336. * Upon a successful return, the caller "owns" WORK_STRUCT_PENDING bit,
  337. * so this work can't be re-armed in any way.
  338. */
  339. static int try_to_grab_pending(struct work_struct *work)
  340. {
  341. struct cpu_workqueue_struct *cwq;
  342. int ret = 0;
  343. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
  344. return 1;
  345. /*
  346. * The queueing is in progress, or it is already queued. Try to
  347. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  348. */
  349. cwq = get_wq_data(work);
  350. if (!cwq)
  351. return ret;
  352. spin_lock_irq(&cwq->lock);
  353. if (!list_empty(&work->entry)) {
  354. /*
  355. * This work is queued, but perhaps we locked the wrong cwq.
  356. * In that case we must see the new value after rmb(), see
  357. * insert_work()->wmb().
  358. */
  359. smp_rmb();
  360. if (cwq == get_wq_data(work)) {
  361. list_del_init(&work->entry);
  362. ret = 1;
  363. }
  364. }
  365. spin_unlock_irq(&cwq->lock);
  366. return ret;
  367. }
  368. static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
  369. struct work_struct *work)
  370. {
  371. struct wq_barrier barr;
  372. int running = 0;
  373. spin_lock_irq(&cwq->lock);
  374. if (unlikely(cwq->current_work == work)) {
  375. insert_wq_barrier(cwq, &barr, 0);
  376. running = 1;
  377. }
  378. spin_unlock_irq(&cwq->lock);
  379. if (unlikely(running))
  380. wait_for_completion(&barr.done);
  381. }
  382. static void wait_on_work(struct work_struct *work)
  383. {
  384. struct cpu_workqueue_struct *cwq;
  385. struct workqueue_struct *wq;
  386. const cpumask_t *cpu_map;
  387. int cpu;
  388. might_sleep();
  389. cwq = get_wq_data(work);
  390. if (!cwq)
  391. return;
  392. wq = cwq->wq;
  393. cpu_map = wq_cpu_map(wq);
  394. for_each_cpu_mask(cpu, *cpu_map)
  395. wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  396. }
  397. /**
  398. * cancel_work_sync - block until a work_struct's callback has terminated
  399. * @work: the work which is to be flushed
  400. *
  401. * cancel_work_sync() will cancel the work if it is queued. If the work's
  402. * callback appears to be running, cancel_work_sync() will block until it
  403. * has completed.
  404. *
  405. * It is possible to use this function if the work re-queues itself. It can
  406. * cancel the work even if it migrates to another workqueue, however in that
  407. * case it only guarantees that work->func() has completed on the last queued
  408. * workqueue.
  409. *
  410. * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
  411. * pending, otherwise it goes into a busy-wait loop until the timer expires.
  412. *
  413. * The caller must ensure that workqueue_struct on which this work was last
  414. * queued can't be destroyed before this function returns.
  415. */
  416. void cancel_work_sync(struct work_struct *work)
  417. {
  418. while (!try_to_grab_pending(work))
  419. cpu_relax();
  420. wait_on_work(work);
  421. work_clear_pending(work);
  422. }
  423. EXPORT_SYMBOL_GPL(cancel_work_sync);
  424. /**
  425. * cancel_rearming_delayed_work - reliably kill off a delayed work.
  426. * @dwork: the delayed work struct
  427. *
  428. * It is possible to use this function if @dwork rearms itself via queue_work()
  429. * or queue_delayed_work(). See also the comment for cancel_work_sync().
  430. */
  431. void cancel_rearming_delayed_work(struct delayed_work *dwork)
  432. {
  433. while (!del_timer(&dwork->timer) &&
  434. !try_to_grab_pending(&dwork->work))
  435. cpu_relax();
  436. wait_on_work(&dwork->work);
  437. work_clear_pending(&dwork->work);
  438. }
  439. EXPORT_SYMBOL(cancel_rearming_delayed_work);
  440. static struct workqueue_struct *keventd_wq __read_mostly;
  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,
  461. unsigned long delay)
  462. {
  463. timer_stats_timer_set_start_info(&dwork->timer);
  464. return queue_delayed_work(keventd_wq, dwork, delay);
  465. }
  466. EXPORT_SYMBOL(schedule_delayed_work);
  467. /**
  468. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  469. * @cpu: cpu to use
  470. * @dwork: job to be done
  471. * @delay: number of jiffies to wait
  472. *
  473. * After waiting for a given time this puts a job in the kernel-global
  474. * workqueue on the specified CPU.
  475. */
  476. int schedule_delayed_work_on(int cpu,
  477. struct delayed_work *dwork, unsigned long delay)
  478. {
  479. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  480. }
  481. EXPORT_SYMBOL(schedule_delayed_work_on);
  482. /**
  483. * schedule_on_each_cpu - call a function on each online CPU from keventd
  484. * @func: the function to call
  485. *
  486. * Returns zero on success.
  487. * Returns -ve errno on failure.
  488. *
  489. * Appears to be racy against CPU hotplug.
  490. *
  491. * schedule_on_each_cpu() is very slow.
  492. */
  493. int schedule_on_each_cpu(work_func_t func)
  494. {
  495. int cpu;
  496. struct work_struct *works;
  497. works = alloc_percpu(struct work_struct);
  498. if (!works)
  499. return -ENOMEM;
  500. preempt_disable(); /* CPU hotplug */
  501. for_each_online_cpu(cpu) {
  502. struct work_struct *work = per_cpu_ptr(works, cpu);
  503. INIT_WORK(work, func);
  504. set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
  505. __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
  506. }
  507. preempt_enable();
  508. flush_workqueue(keventd_wq);
  509. free_percpu(works);
  510. return 0;
  511. }
  512. void flush_scheduled_work(void)
  513. {
  514. flush_workqueue(keventd_wq);
  515. }
  516. EXPORT_SYMBOL(flush_scheduled_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. action &= ~CPU_TASKS_FROZEN;
  688. switch (action) {
  689. case CPU_LOCK_ACQUIRE:
  690. mutex_lock(&workqueue_mutex);
  691. return NOTIFY_OK;
  692. case CPU_LOCK_RELEASE:
  693. mutex_unlock(&workqueue_mutex);
  694. return NOTIFY_OK;
  695. case CPU_UP_PREPARE:
  696. cpu_set(cpu, cpu_populated_map);
  697. }
  698. list_for_each_entry(wq, &workqueues, list) {
  699. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  700. switch (action) {
  701. case CPU_UP_PREPARE:
  702. if (!create_workqueue_thread(cwq, cpu))
  703. break;
  704. printk(KERN_ERR "workqueue for %i failed\n", cpu);
  705. return NOTIFY_BAD;
  706. case CPU_ONLINE:
  707. start_workqueue_thread(cwq, cpu);
  708. break;
  709. case CPU_UP_CANCELED:
  710. start_workqueue_thread(cwq, -1);
  711. case CPU_DEAD:
  712. cleanup_workqueue_thread(cwq, cpu);
  713. break;
  714. }
  715. }
  716. return NOTIFY_OK;
  717. }
  718. void __init init_workqueues(void)
  719. {
  720. cpu_populated_map = cpu_online_map;
  721. singlethread_cpu = first_cpu(cpu_possible_map);
  722. cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
  723. hotcpu_notifier(workqueue_cpu_callback, 0);
  724. keventd_wq = create_workqueue("events");
  725. BUG_ON(!keventd_wq);
  726. }