workqueue.c 20 KB

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