workqueue.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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. /* optimization, we could use cpu_possible_map */
  64. static cpumask_t cpu_populated_map __read_mostly;
  65. /* If it's single threaded, it isn't in the list of workqueues. */
  66. static inline int is_single_threaded(struct workqueue_struct *wq)
  67. {
  68. return list_empty(&wq->list);
  69. }
  70. /*
  71. * Set the workqueue on which a work item is to be run
  72. * - Must *only* be called if the pending flag is set
  73. */
  74. static inline void set_wq_data(struct work_struct *work, void *wq)
  75. {
  76. unsigned long new;
  77. BUG_ON(!work_pending(work));
  78. new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
  79. new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
  80. atomic_long_set(&work->data, new);
  81. }
  82. static inline void *get_wq_data(struct work_struct *work)
  83. {
  84. return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
  85. }
  86. static void insert_work(struct cpu_workqueue_struct *cwq,
  87. struct work_struct *work, int tail)
  88. {
  89. set_wq_data(work, cwq);
  90. if (tail)
  91. list_add_tail(&work->entry, &cwq->worklist);
  92. else
  93. list_add(&work->entry, &cwq->worklist);
  94. wake_up(&cwq->more_work);
  95. }
  96. /* Preempt must be disabled. */
  97. static void __queue_work(struct cpu_workqueue_struct *cwq,
  98. struct work_struct *work)
  99. {
  100. unsigned long flags;
  101. spin_lock_irqsave(&cwq->lock, flags);
  102. insert_work(cwq, work, 1);
  103. spin_unlock_irqrestore(&cwq->lock, flags);
  104. }
  105. /**
  106. * queue_work - queue work on a workqueue
  107. * @wq: workqueue to use
  108. * @work: work to queue
  109. *
  110. * Returns 0 if @work was already on a queue, non-zero otherwise.
  111. *
  112. * We queue the work to the CPU it was submitted, but there is no
  113. * guarantee that it will be processed by that CPU.
  114. */
  115. int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
  116. {
  117. int ret = 0, cpu = get_cpu();
  118. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  119. if (unlikely(is_single_threaded(wq)))
  120. cpu = singlethread_cpu;
  121. BUG_ON(!list_empty(&work->entry));
  122. __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  123. ret = 1;
  124. }
  125. put_cpu();
  126. return ret;
  127. }
  128. EXPORT_SYMBOL_GPL(queue_work);
  129. void delayed_work_timer_fn(unsigned long __data)
  130. {
  131. struct delayed_work *dwork = (struct delayed_work *)__data;
  132. struct workqueue_struct *wq = get_wq_data(&dwork->work);
  133. int cpu = smp_processor_id();
  134. if (unlikely(is_single_threaded(wq)))
  135. cpu = singlethread_cpu;
  136. __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
  137. }
  138. /**
  139. * queue_delayed_work - queue work on a workqueue after delay
  140. * @wq: workqueue to use
  141. * @dwork: delayable work to queue
  142. * @delay: number of jiffies to wait before queueing
  143. *
  144. * Returns 0 if @work was already on a queue, non-zero otherwise.
  145. */
  146. int fastcall queue_delayed_work(struct workqueue_struct *wq,
  147. struct delayed_work *dwork, unsigned long delay)
  148. {
  149. int ret = 0;
  150. struct timer_list *timer = &dwork->timer;
  151. struct work_struct *work = &dwork->work;
  152. timer_stats_timer_set_start_info(timer);
  153. if (delay == 0)
  154. return queue_work(wq, work);
  155. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  156. BUG_ON(timer_pending(timer));
  157. BUG_ON(!list_empty(&work->entry));
  158. /* This stores wq for the moment, for the timer_fn */
  159. set_wq_data(work, wq);
  160. timer->expires = jiffies + delay;
  161. timer->data = (unsigned long)dwork;
  162. timer->function = delayed_work_timer_fn;
  163. add_timer(timer);
  164. ret = 1;
  165. }
  166. return ret;
  167. }
  168. EXPORT_SYMBOL_GPL(queue_delayed_work);
  169. /**
  170. * queue_delayed_work_on - queue work on specific CPU after delay
  171. * @cpu: CPU number to execute work on
  172. * @wq: workqueue to use
  173. * @dwork: work to queue
  174. * @delay: number of jiffies to wait before queueing
  175. *
  176. * Returns 0 if @work was already on a queue, non-zero otherwise.
  177. */
  178. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  179. struct delayed_work *dwork, unsigned long delay)
  180. {
  181. int ret = 0;
  182. struct timer_list *timer = &dwork->timer;
  183. struct work_struct *work = &dwork->work;
  184. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  185. BUG_ON(timer_pending(timer));
  186. BUG_ON(!list_empty(&work->entry));
  187. /* This stores wq for the moment, for the timer_fn */
  188. set_wq_data(work, wq);
  189. timer->expires = jiffies + delay;
  190. timer->data = (unsigned long)dwork;
  191. timer->function = delayed_work_timer_fn;
  192. add_timer_on(timer, cpu);
  193. ret = 1;
  194. }
  195. return ret;
  196. }
  197. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  198. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  199. {
  200. unsigned long flags;
  201. /*
  202. * Keep taking off work from the queue until
  203. * done.
  204. */
  205. spin_lock_irqsave(&cwq->lock, flags);
  206. cwq->run_depth++;
  207. if (cwq->run_depth > 3) {
  208. /* morton gets to eat his hat */
  209. printk("%s: recursion depth exceeded: %d\n",
  210. __FUNCTION__, cwq->run_depth);
  211. dump_stack();
  212. }
  213. while (!list_empty(&cwq->worklist)) {
  214. struct work_struct *work = list_entry(cwq->worklist.next,
  215. struct work_struct, entry);
  216. work_func_t f = work->func;
  217. cwq->current_work = work;
  218. list_del_init(cwq->worklist.next);
  219. spin_unlock_irqrestore(&cwq->lock, flags);
  220. BUG_ON(get_wq_data(work) != cwq);
  221. if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
  222. work_release(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_irqsave(&cwq->lock, flags);
  235. cwq->current_work = NULL;
  236. }
  237. cwq->run_depth--;
  238. spin_unlock_irqrestore(&cwq->lock, flags);
  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. struct k_sigaction sa;
  260. sigset_t blocked;
  261. if (!cwq->wq->freezeable)
  262. current->flags |= PF_NOFREEZE;
  263. set_user_nice(current, -5);
  264. /* Block and flush all signals */
  265. sigfillset(&blocked);
  266. sigprocmask(SIG_BLOCK, &blocked, NULL);
  267. flush_signals(current);
  268. /*
  269. * We inherited MPOL_INTERLEAVE from the booting kernel.
  270. * Set MPOL_DEFAULT to insure node local allocations.
  271. */
  272. numa_default_policy();
  273. /* SIG_IGN makes children autoreap: see do_notify_parent(). */
  274. sa.sa.sa_handler = SIG_IGN;
  275. sa.sa.sa_flags = 0;
  276. siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
  277. do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
  278. for (;;) {
  279. if (cwq->wq->freezeable)
  280. try_to_freeze();
  281. prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
  282. if (!cwq->should_stop && list_empty(&cwq->worklist))
  283. schedule();
  284. finish_wait(&cwq->more_work, &wait);
  285. if (cwq_should_stop(cwq))
  286. break;
  287. run_workqueue(cwq);
  288. }
  289. return 0;
  290. }
  291. struct wq_barrier {
  292. struct work_struct work;
  293. struct completion done;
  294. };
  295. static void wq_barrier_func(struct work_struct *work)
  296. {
  297. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  298. complete(&barr->done);
  299. }
  300. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  301. struct wq_barrier *barr, int tail)
  302. {
  303. INIT_WORK(&barr->work, wq_barrier_func);
  304. __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
  305. init_completion(&barr->done);
  306. insert_work(cwq, &barr->work, tail);
  307. }
  308. static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  309. {
  310. if (cwq->thread == current) {
  311. /*
  312. * Probably keventd trying to flush its own queue. So simply run
  313. * it by hand rather than deadlocking.
  314. */
  315. run_workqueue(cwq);
  316. } else {
  317. struct wq_barrier barr;
  318. int active = 0;
  319. spin_lock_irq(&cwq->lock);
  320. if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
  321. insert_wq_barrier(cwq, &barr, 1);
  322. active = 1;
  323. }
  324. spin_unlock_irq(&cwq->lock);
  325. if (active)
  326. wait_for_completion(&barr.done);
  327. }
  328. }
  329. /**
  330. * flush_workqueue - ensure that any scheduled work has run to completion.
  331. * @wq: workqueue to flush
  332. *
  333. * Forces execution of the workqueue and blocks until its completion.
  334. * This is typically used in driver shutdown handlers.
  335. *
  336. * We sleep until all works which were queued on entry have been handled,
  337. * but we are not livelocked by new incoming ones.
  338. *
  339. * This function used to run the workqueues itself. Now we just wait for the
  340. * helper threads to do it.
  341. */
  342. void fastcall flush_workqueue(struct workqueue_struct *wq)
  343. {
  344. if (is_single_threaded(wq))
  345. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
  346. else {
  347. int cpu;
  348. for_each_cpu_mask(cpu, cpu_populated_map)
  349. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  350. }
  351. }
  352. EXPORT_SYMBOL_GPL(flush_workqueue);
  353. static void wait_on_work(struct cpu_workqueue_struct *cwq,
  354. struct work_struct *work)
  355. {
  356. struct wq_barrier barr;
  357. int running = 0;
  358. spin_lock_irq(&cwq->lock);
  359. if (unlikely(cwq->current_work == work)) {
  360. insert_wq_barrier(cwq, &barr, 0);
  361. running = 1;
  362. }
  363. spin_unlock_irq(&cwq->lock);
  364. if (unlikely(running))
  365. wait_for_completion(&barr.done);
  366. }
  367. /**
  368. * flush_work - block until a work_struct's callback has terminated
  369. * @wq: the workqueue on which the work is queued
  370. * @work: the work which is to be flushed
  371. *
  372. * flush_work() will attempt to cancel the work if it is queued. If the work's
  373. * callback appears to be running, flush_work() will block until it has
  374. * completed.
  375. *
  376. * flush_work() is designed to be used when the caller is tearing down data
  377. * structures which the callback function operates upon. It is expected that,
  378. * prior to calling flush_work(), the caller has arranged for the work to not
  379. * be requeued.
  380. */
  381. void flush_work(struct workqueue_struct *wq, struct work_struct *work)
  382. {
  383. struct cpu_workqueue_struct *cwq;
  384. cwq = get_wq_data(work);
  385. /* Was it ever queued ? */
  386. if (!cwq)
  387. return;
  388. /*
  389. * This work can't be re-queued, no need to re-check that
  390. * get_wq_data() is still the same when we take cwq->lock.
  391. */
  392. spin_lock_irq(&cwq->lock);
  393. list_del_init(&work->entry);
  394. work_release(work);
  395. spin_unlock_irq(&cwq->lock);
  396. if (is_single_threaded(wq))
  397. wait_on_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
  398. else {
  399. int cpu;
  400. for_each_cpu_mask(cpu, cpu_populated_map)
  401. wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  402. }
  403. }
  404. EXPORT_SYMBOL_GPL(flush_work);
  405. static struct workqueue_struct *keventd_wq;
  406. /**
  407. * schedule_work - put work task in global workqueue
  408. * @work: job to be done
  409. *
  410. * This puts a job in the kernel-global workqueue.
  411. */
  412. int fastcall schedule_work(struct work_struct *work)
  413. {
  414. return queue_work(keventd_wq, work);
  415. }
  416. EXPORT_SYMBOL(schedule_work);
  417. /**
  418. * schedule_delayed_work - put work task in global workqueue after delay
  419. * @dwork: job to be done
  420. * @delay: number of jiffies to wait or 0 for immediate execution
  421. *
  422. * After waiting for a given time this puts a job in the kernel-global
  423. * workqueue.
  424. */
  425. int fastcall schedule_delayed_work(struct delayed_work *dwork,
  426. unsigned long delay)
  427. {
  428. timer_stats_timer_set_start_info(&dwork->timer);
  429. return queue_delayed_work(keventd_wq, dwork, delay);
  430. }
  431. EXPORT_SYMBOL(schedule_delayed_work);
  432. /**
  433. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  434. * @cpu: cpu to use
  435. * @dwork: job to be done
  436. * @delay: number of jiffies to wait
  437. *
  438. * After waiting for a given time this puts a job in the kernel-global
  439. * workqueue on the specified CPU.
  440. */
  441. int schedule_delayed_work_on(int cpu,
  442. struct delayed_work *dwork, unsigned long delay)
  443. {
  444. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  445. }
  446. EXPORT_SYMBOL(schedule_delayed_work_on);
  447. /**
  448. * schedule_on_each_cpu - call a function on each online CPU from keventd
  449. * @func: the function to call
  450. *
  451. * Returns zero on success.
  452. * Returns -ve errno on failure.
  453. *
  454. * Appears to be racy against CPU hotplug.
  455. *
  456. * schedule_on_each_cpu() is very slow.
  457. */
  458. int schedule_on_each_cpu(work_func_t func)
  459. {
  460. int cpu;
  461. struct work_struct *works;
  462. works = alloc_percpu(struct work_struct);
  463. if (!works)
  464. return -ENOMEM;
  465. preempt_disable(); /* CPU hotplug */
  466. for_each_online_cpu(cpu) {
  467. struct work_struct *work = per_cpu_ptr(works, cpu);
  468. INIT_WORK(work, func);
  469. set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
  470. __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
  471. }
  472. preempt_enable();
  473. flush_workqueue(keventd_wq);
  474. free_percpu(works);
  475. return 0;
  476. }
  477. void flush_scheduled_work(void)
  478. {
  479. flush_workqueue(keventd_wq);
  480. }
  481. EXPORT_SYMBOL(flush_scheduled_work);
  482. void flush_work_keventd(struct work_struct *work)
  483. {
  484. flush_work(keventd_wq, work);
  485. }
  486. EXPORT_SYMBOL(flush_work_keventd);
  487. /**
  488. * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
  489. * @wq: the controlling workqueue structure
  490. * @dwork: the delayed work struct
  491. */
  492. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  493. struct delayed_work *dwork)
  494. {
  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. struct cpu_workqueue_struct *cwq;
  650. if (is_single_threaded(wq)) {
  651. cwq = per_cpu_ptr(wq->cpu_wq, singlethread_cpu);
  652. cleanup_workqueue_thread(cwq, singlethread_cpu);
  653. } else {
  654. int cpu;
  655. mutex_lock(&workqueue_mutex);
  656. list_del(&wq->list);
  657. mutex_unlock(&workqueue_mutex);
  658. for_each_cpu_mask(cpu, cpu_populated_map) {
  659. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  660. cleanup_workqueue_thread(cwq, cpu);
  661. }
  662. }
  663. free_percpu(wq->cpu_wq);
  664. kfree(wq);
  665. }
  666. EXPORT_SYMBOL_GPL(destroy_workqueue);
  667. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  668. unsigned long action,
  669. void *hcpu)
  670. {
  671. unsigned int cpu = (unsigned long)hcpu;
  672. struct cpu_workqueue_struct *cwq;
  673. struct workqueue_struct *wq;
  674. switch (action) {
  675. case CPU_LOCK_ACQUIRE:
  676. mutex_lock(&workqueue_mutex);
  677. return NOTIFY_OK;
  678. case CPU_LOCK_RELEASE:
  679. mutex_unlock(&workqueue_mutex);
  680. return NOTIFY_OK;
  681. case CPU_UP_PREPARE:
  682. cpu_set(cpu, cpu_populated_map);
  683. }
  684. list_for_each_entry(wq, &workqueues, list) {
  685. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  686. switch (action) {
  687. case CPU_UP_PREPARE:
  688. if (!create_workqueue_thread(cwq, cpu))
  689. break;
  690. printk(KERN_ERR "workqueue for %i failed\n", cpu);
  691. return NOTIFY_BAD;
  692. case CPU_ONLINE:
  693. wake_up_process(cwq->thread);
  694. break;
  695. case CPU_UP_CANCELED:
  696. if (cwq->thread)
  697. wake_up_process(cwq->thread);
  698. case CPU_DEAD:
  699. cleanup_workqueue_thread(cwq, cpu);
  700. break;
  701. }
  702. }
  703. return NOTIFY_OK;
  704. }
  705. void init_workqueues(void)
  706. {
  707. cpu_populated_map = cpu_online_map;
  708. singlethread_cpu = first_cpu(cpu_possible_map);
  709. hotcpu_notifier(workqueue_cpu_callback, 0);
  710. keventd_wq = create_workqueue("events");
  711. BUG_ON(!keventd_wq);
  712. }