workqueue.c 20 KB

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