workqueue.c 20 KB

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