workqueue.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. #include <linux/lockdep.h>
  35. /*
  36. * The per-CPU workqueue (if single thread, we always use the first
  37. * possible cpu).
  38. */
  39. struct cpu_workqueue_struct {
  40. spinlock_t lock;
  41. struct list_head worklist;
  42. wait_queue_head_t more_work;
  43. struct work_struct *current_work;
  44. struct workqueue_struct *wq;
  45. struct task_struct *thread;
  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. #ifdef CONFIG_LOCKDEP
  59. struct lockdep_map lockdep_map;
  60. #endif
  61. };
  62. /* Serializes the accesses to the list of workqueues. */
  63. static DEFINE_SPINLOCK(workqueue_lock);
  64. static LIST_HEAD(workqueues);
  65. static int singlethread_cpu __read_mostly;
  66. static cpumask_t cpu_singlethread_map __read_mostly;
  67. /*
  68. * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
  69. * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
  70. * which comes in between can't use for_each_online_cpu(). We could
  71. * use cpu_possible_map, the cpumask below is more a documentation
  72. * than optimization.
  73. */
  74. static cpumask_t cpu_populated_map __read_mostly;
  75. /* If it's single threaded, it isn't in the list of workqueues. */
  76. static inline int is_single_threaded(struct workqueue_struct *wq)
  77. {
  78. return wq->singlethread;
  79. }
  80. static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq)
  81. {
  82. return is_single_threaded(wq)
  83. ? &cpu_singlethread_map : &cpu_populated_map;
  84. }
  85. static
  86. struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
  87. {
  88. if (unlikely(is_single_threaded(wq)))
  89. cpu = singlethread_cpu;
  90. return per_cpu_ptr(wq->cpu_wq, cpu);
  91. }
  92. /*
  93. * Set the workqueue on which a work item is to be run
  94. * - Must *only* be called if the pending flag is set
  95. */
  96. static inline void set_wq_data(struct work_struct *work,
  97. struct cpu_workqueue_struct *cwq)
  98. {
  99. unsigned long new;
  100. BUG_ON(!work_pending(work));
  101. new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
  102. new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
  103. atomic_long_set(&work->data, new);
  104. }
  105. static inline
  106. struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
  107. {
  108. return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
  109. }
  110. static void insert_work(struct cpu_workqueue_struct *cwq,
  111. struct work_struct *work, int tail)
  112. {
  113. set_wq_data(work, cwq);
  114. /*
  115. * Ensure that we get the right work->data if we see the
  116. * result of list_add() below, see try_to_grab_pending().
  117. */
  118. smp_wmb();
  119. if (tail)
  120. list_add_tail(&work->entry, &cwq->worklist);
  121. else
  122. list_add(&work->entry, &cwq->worklist);
  123. wake_up(&cwq->more_work);
  124. }
  125. /* Preempt must be disabled. */
  126. static void __queue_work(struct cpu_workqueue_struct *cwq,
  127. struct work_struct *work)
  128. {
  129. unsigned long flags;
  130. spin_lock_irqsave(&cwq->lock, flags);
  131. insert_work(cwq, work, 1);
  132. spin_unlock_irqrestore(&cwq->lock, flags);
  133. }
  134. /**
  135. * queue_work - queue work on a workqueue
  136. * @wq: workqueue to use
  137. * @work: work to queue
  138. *
  139. * Returns 0 if @work was already on a queue, non-zero otherwise.
  140. *
  141. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  142. * it can be processed by another CPU.
  143. */
  144. int queue_work(struct workqueue_struct *wq, struct work_struct *work)
  145. {
  146. int ret = 0;
  147. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  148. BUG_ON(!list_empty(&work->entry));
  149. __queue_work(wq_per_cpu(wq, get_cpu()), work);
  150. put_cpu();
  151. ret = 1;
  152. }
  153. return ret;
  154. }
  155. EXPORT_SYMBOL_GPL(queue_work);
  156. static void delayed_work_timer_fn(unsigned long __data)
  157. {
  158. struct delayed_work *dwork = (struct delayed_work *)__data;
  159. struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
  160. struct workqueue_struct *wq = cwq->wq;
  161. __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
  162. }
  163. /**
  164. * queue_delayed_work - queue work on a workqueue after delay
  165. * @wq: workqueue to use
  166. * @dwork: delayable work to queue
  167. * @delay: number of jiffies to wait before queueing
  168. *
  169. * Returns 0 if @work was already on a queue, non-zero otherwise.
  170. */
  171. int queue_delayed_work(struct workqueue_struct *wq,
  172. struct delayed_work *dwork, unsigned long delay)
  173. {
  174. if (delay == 0)
  175. return queue_work(wq, &dwork->work);
  176. return queue_delayed_work_on(-1, wq, dwork, delay);
  177. }
  178. EXPORT_SYMBOL_GPL(queue_delayed_work);
  179. /**
  180. * queue_delayed_work_on - queue work on specific CPU after delay
  181. * @cpu: CPU number to execute work on
  182. * @wq: workqueue to use
  183. * @dwork: work to queue
  184. * @delay: number of jiffies to wait before queueing
  185. *
  186. * Returns 0 if @work was already on a queue, non-zero otherwise.
  187. */
  188. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  189. struct delayed_work *dwork, unsigned long delay)
  190. {
  191. int ret = 0;
  192. struct timer_list *timer = &dwork->timer;
  193. struct work_struct *work = &dwork->work;
  194. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  195. BUG_ON(timer_pending(timer));
  196. BUG_ON(!list_empty(&work->entry));
  197. timer_stats_timer_set_start_info(&dwork->timer);
  198. /* This stores cwq for the moment, for the timer_fn */
  199. set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
  200. timer->expires = jiffies + delay;
  201. timer->data = (unsigned long)dwork;
  202. timer->function = delayed_work_timer_fn;
  203. if (unlikely(cpu >= 0))
  204. add_timer_on(timer, cpu);
  205. else
  206. add_timer(timer);
  207. ret = 1;
  208. }
  209. return ret;
  210. }
  211. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  212. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  213. {
  214. spin_lock_irq(&cwq->lock);
  215. cwq->run_depth++;
  216. if (cwq->run_depth > 3) {
  217. /* morton gets to eat his hat */
  218. printk("%s: recursion depth exceeded: %d\n",
  219. __func__, cwq->run_depth);
  220. dump_stack();
  221. }
  222. while (!list_empty(&cwq->worklist)) {
  223. struct work_struct *work = list_entry(cwq->worklist.next,
  224. struct work_struct, entry);
  225. work_func_t f = work->func;
  226. #ifdef CONFIG_LOCKDEP
  227. /*
  228. * It is permissible to free the struct work_struct
  229. * from inside the function that is called from it,
  230. * this we need to take into account for lockdep too.
  231. * To avoid bogus "held lock freed" warnings as well
  232. * as problems when looking into work->lockdep_map,
  233. * make a copy and use that here.
  234. */
  235. struct lockdep_map lockdep_map = work->lockdep_map;
  236. #endif
  237. cwq->current_work = work;
  238. list_del_init(cwq->worklist.next);
  239. spin_unlock_irq(&cwq->lock);
  240. BUG_ON(get_wq_data(work) != cwq);
  241. work_clear_pending(work);
  242. lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  243. lock_acquire(&lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  244. f(work);
  245. lock_release(&lockdep_map, 1, _THIS_IP_);
  246. lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
  247. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  248. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  249. "%s/0x%08x/%d\n",
  250. current->comm, preempt_count(),
  251. task_pid_nr(current));
  252. printk(KERN_ERR " last function: ");
  253. print_symbol("%s\n", (unsigned long)f);
  254. debug_show_held_locks(current);
  255. dump_stack();
  256. }
  257. spin_lock_irq(&cwq->lock);
  258. cwq->current_work = NULL;
  259. }
  260. cwq->run_depth--;
  261. spin_unlock_irq(&cwq->lock);
  262. }
  263. static int worker_thread(void *__cwq)
  264. {
  265. struct cpu_workqueue_struct *cwq = __cwq;
  266. DEFINE_WAIT(wait);
  267. if (cwq->wq->freezeable)
  268. set_freezable();
  269. set_user_nice(current, -5);
  270. for (;;) {
  271. prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
  272. if (!freezing(current) &&
  273. !kthread_should_stop() &&
  274. list_empty(&cwq->worklist))
  275. schedule();
  276. finish_wait(&cwq->more_work, &wait);
  277. try_to_freeze();
  278. if (kthread_should_stop())
  279. break;
  280. run_workqueue(cwq);
  281. }
  282. return 0;
  283. }
  284. struct wq_barrier {
  285. struct work_struct work;
  286. struct completion done;
  287. };
  288. static void wq_barrier_func(struct work_struct *work)
  289. {
  290. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  291. complete(&barr->done);
  292. }
  293. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  294. struct wq_barrier *barr, int tail)
  295. {
  296. INIT_WORK(&barr->work, wq_barrier_func);
  297. __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
  298. init_completion(&barr->done);
  299. insert_work(cwq, &barr->work, tail);
  300. }
  301. static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  302. {
  303. int active;
  304. if (cwq->thread == current) {
  305. /*
  306. * Probably keventd trying to flush its own queue. So simply run
  307. * it by hand rather than deadlocking.
  308. */
  309. run_workqueue(cwq);
  310. active = 1;
  311. } else {
  312. struct wq_barrier barr;
  313. active = 0;
  314. spin_lock_irq(&cwq->lock);
  315. if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
  316. insert_wq_barrier(cwq, &barr, 1);
  317. active = 1;
  318. }
  319. spin_unlock_irq(&cwq->lock);
  320. if (active)
  321. wait_for_completion(&barr.done);
  322. }
  323. return active;
  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 flush_workqueue(struct workqueue_struct *wq)
  339. {
  340. const cpumask_t *cpu_map = wq_cpu_map(wq);
  341. int cpu;
  342. might_sleep();
  343. lock_acquire(&wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  344. lock_release(&wq->lockdep_map, 1, _THIS_IP_);
  345. for_each_cpu_mask_nr(cpu, *cpu_map)
  346. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  347. }
  348. EXPORT_SYMBOL_GPL(flush_workqueue);
  349. /*
  350. * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
  351. * so this work can't be re-armed in any way.
  352. */
  353. static int try_to_grab_pending(struct work_struct *work)
  354. {
  355. struct cpu_workqueue_struct *cwq;
  356. int ret = -1;
  357. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
  358. return 0;
  359. /*
  360. * The queueing is in progress, or it is already queued. Try to
  361. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  362. */
  363. cwq = get_wq_data(work);
  364. if (!cwq)
  365. return ret;
  366. spin_lock_irq(&cwq->lock);
  367. if (!list_empty(&work->entry)) {
  368. /*
  369. * This work is queued, but perhaps we locked the wrong cwq.
  370. * In that case we must see the new value after rmb(), see
  371. * insert_work()->wmb().
  372. */
  373. smp_rmb();
  374. if (cwq == get_wq_data(work)) {
  375. list_del_init(&work->entry);
  376. ret = 1;
  377. }
  378. }
  379. spin_unlock_irq(&cwq->lock);
  380. return ret;
  381. }
  382. static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
  383. struct work_struct *work)
  384. {
  385. struct wq_barrier barr;
  386. int running = 0;
  387. spin_lock_irq(&cwq->lock);
  388. if (unlikely(cwq->current_work == work)) {
  389. insert_wq_barrier(cwq, &barr, 0);
  390. running = 1;
  391. }
  392. spin_unlock_irq(&cwq->lock);
  393. if (unlikely(running))
  394. wait_for_completion(&barr.done);
  395. }
  396. static void wait_on_work(struct work_struct *work)
  397. {
  398. struct cpu_workqueue_struct *cwq;
  399. struct workqueue_struct *wq;
  400. const cpumask_t *cpu_map;
  401. int cpu;
  402. might_sleep();
  403. lock_acquire(&work->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  404. lock_release(&work->lockdep_map, 1, _THIS_IP_);
  405. cwq = get_wq_data(work);
  406. if (!cwq)
  407. return;
  408. wq = cwq->wq;
  409. cpu_map = wq_cpu_map(wq);
  410. for_each_cpu_mask_nr(cpu, *cpu_map)
  411. wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  412. }
  413. static int __cancel_work_timer(struct work_struct *work,
  414. struct timer_list* timer)
  415. {
  416. int ret;
  417. do {
  418. ret = (timer && likely(del_timer(timer)));
  419. if (!ret)
  420. ret = try_to_grab_pending(work);
  421. wait_on_work(work);
  422. } while (unlikely(ret < 0));
  423. work_clear_pending(work);
  424. return ret;
  425. }
  426. /**
  427. * cancel_work_sync - block until a work_struct's callback has terminated
  428. * @work: the work which is to be flushed
  429. *
  430. * Returns true if @work was pending.
  431. *
  432. * cancel_work_sync() will cancel the work if it is queued. If the work's
  433. * callback appears to be running, cancel_work_sync() will block until it
  434. * has completed.
  435. *
  436. * It is possible to use this function if the work re-queues itself. It can
  437. * cancel the work even if it migrates to another workqueue, however in that
  438. * case it only guarantees that work->func() has completed on the last queued
  439. * workqueue.
  440. *
  441. * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
  442. * pending, otherwise it goes into a busy-wait loop until the timer expires.
  443. *
  444. * The caller must ensure that workqueue_struct on which this work was last
  445. * queued can't be destroyed before this function returns.
  446. */
  447. int cancel_work_sync(struct work_struct *work)
  448. {
  449. return __cancel_work_timer(work, NULL);
  450. }
  451. EXPORT_SYMBOL_GPL(cancel_work_sync);
  452. /**
  453. * cancel_delayed_work_sync - reliably kill off a delayed work.
  454. * @dwork: the delayed work struct
  455. *
  456. * Returns true if @dwork was pending.
  457. *
  458. * It is possible to use this function if @dwork rearms itself via queue_work()
  459. * or queue_delayed_work(). See also the comment for cancel_work_sync().
  460. */
  461. int cancel_delayed_work_sync(struct delayed_work *dwork)
  462. {
  463. return __cancel_work_timer(&dwork->work, &dwork->timer);
  464. }
  465. EXPORT_SYMBOL(cancel_delayed_work_sync);
  466. static struct workqueue_struct *keventd_wq __read_mostly;
  467. /**
  468. * schedule_work - put work task in global workqueue
  469. * @work: job to be done
  470. *
  471. * This puts a job in the kernel-global workqueue.
  472. */
  473. int schedule_work(struct work_struct *work)
  474. {
  475. return queue_work(keventd_wq, work);
  476. }
  477. EXPORT_SYMBOL(schedule_work);
  478. /**
  479. * schedule_delayed_work - put work task in global workqueue after delay
  480. * @dwork: job to be done
  481. * @delay: number of jiffies to wait or 0 for immediate execution
  482. *
  483. * After waiting for a given time this puts a job in the kernel-global
  484. * workqueue.
  485. */
  486. int schedule_delayed_work(struct delayed_work *dwork,
  487. unsigned long delay)
  488. {
  489. return queue_delayed_work(keventd_wq, dwork, delay);
  490. }
  491. EXPORT_SYMBOL(schedule_delayed_work);
  492. /**
  493. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  494. * @cpu: cpu to use
  495. * @dwork: job to be done
  496. * @delay: number of jiffies to wait
  497. *
  498. * After waiting for a given time this puts a job in the kernel-global
  499. * workqueue on the specified CPU.
  500. */
  501. int schedule_delayed_work_on(int cpu,
  502. struct delayed_work *dwork, unsigned long delay)
  503. {
  504. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  505. }
  506. EXPORT_SYMBOL(schedule_delayed_work_on);
  507. /**
  508. * schedule_on_each_cpu - call a function on each online CPU from keventd
  509. * @func: the function to call
  510. *
  511. * Returns zero on success.
  512. * Returns -ve errno on failure.
  513. *
  514. * schedule_on_each_cpu() is very slow.
  515. */
  516. int schedule_on_each_cpu(work_func_t func)
  517. {
  518. int cpu;
  519. struct work_struct *works;
  520. works = alloc_percpu(struct work_struct);
  521. if (!works)
  522. return -ENOMEM;
  523. get_online_cpus();
  524. for_each_online_cpu(cpu) {
  525. struct work_struct *work = per_cpu_ptr(works, cpu);
  526. INIT_WORK(work, func);
  527. set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
  528. __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
  529. }
  530. flush_workqueue(keventd_wq);
  531. put_online_cpus();
  532. free_percpu(works);
  533. return 0;
  534. }
  535. void flush_scheduled_work(void)
  536. {
  537. flush_workqueue(keventd_wq);
  538. }
  539. EXPORT_SYMBOL(flush_scheduled_work);
  540. /**
  541. * execute_in_process_context - reliably execute the routine with user context
  542. * @fn: the function to execute
  543. * @ew: guaranteed storage for the execute work structure (must
  544. * be available when the work executes)
  545. *
  546. * Executes the function immediately if process context is available,
  547. * otherwise schedules the function for delayed execution.
  548. *
  549. * Returns: 0 - function was executed
  550. * 1 - function was scheduled for execution
  551. */
  552. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  553. {
  554. if (!in_interrupt()) {
  555. fn(&ew->work);
  556. return 0;
  557. }
  558. INIT_WORK(&ew->work, fn);
  559. schedule_work(&ew->work);
  560. return 1;
  561. }
  562. EXPORT_SYMBOL_GPL(execute_in_process_context);
  563. int keventd_up(void)
  564. {
  565. return keventd_wq != NULL;
  566. }
  567. int current_is_keventd(void)
  568. {
  569. struct cpu_workqueue_struct *cwq;
  570. int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  571. int ret = 0;
  572. BUG_ON(!keventd_wq);
  573. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  574. if (current == cwq->thread)
  575. ret = 1;
  576. return ret;
  577. }
  578. static struct cpu_workqueue_struct *
  579. init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
  580. {
  581. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  582. cwq->wq = wq;
  583. spin_lock_init(&cwq->lock);
  584. INIT_LIST_HEAD(&cwq->worklist);
  585. init_waitqueue_head(&cwq->more_work);
  586. return cwq;
  587. }
  588. static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  589. {
  590. struct workqueue_struct *wq = cwq->wq;
  591. const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
  592. struct task_struct *p;
  593. p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
  594. /*
  595. * Nobody can add the work_struct to this cwq,
  596. * if (caller is __create_workqueue)
  597. * nobody should see this wq
  598. * else // caller is CPU_UP_PREPARE
  599. * cpu is not on cpu_online_map
  600. * so we can abort safely.
  601. */
  602. if (IS_ERR(p))
  603. return PTR_ERR(p);
  604. cwq->thread = p;
  605. return 0;
  606. }
  607. static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  608. {
  609. struct task_struct *p = cwq->thread;
  610. if (p != NULL) {
  611. if (cpu >= 0)
  612. kthread_bind(p, cpu);
  613. wake_up_process(p);
  614. }
  615. }
  616. struct workqueue_struct *__create_workqueue_key(const char *name,
  617. int singlethread,
  618. int freezeable,
  619. struct lock_class_key *key,
  620. const char *lock_name)
  621. {
  622. struct workqueue_struct *wq;
  623. struct cpu_workqueue_struct *cwq;
  624. int err = 0, cpu;
  625. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  626. if (!wq)
  627. return NULL;
  628. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  629. if (!wq->cpu_wq) {
  630. kfree(wq);
  631. return NULL;
  632. }
  633. wq->name = name;
  634. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  635. wq->singlethread = singlethread;
  636. wq->freezeable = freezeable;
  637. INIT_LIST_HEAD(&wq->list);
  638. if (singlethread) {
  639. cwq = init_cpu_workqueue(wq, singlethread_cpu);
  640. err = create_workqueue_thread(cwq, singlethread_cpu);
  641. start_workqueue_thread(cwq, -1);
  642. } else {
  643. get_online_cpus();
  644. spin_lock(&workqueue_lock);
  645. list_add(&wq->list, &workqueues);
  646. spin_unlock(&workqueue_lock);
  647. for_each_possible_cpu(cpu) {
  648. cwq = init_cpu_workqueue(wq, cpu);
  649. if (err || !cpu_online(cpu))
  650. continue;
  651. err = create_workqueue_thread(cwq, cpu);
  652. start_workqueue_thread(cwq, cpu);
  653. }
  654. put_online_cpus();
  655. }
  656. if (err) {
  657. destroy_workqueue(wq);
  658. wq = NULL;
  659. }
  660. return wq;
  661. }
  662. EXPORT_SYMBOL_GPL(__create_workqueue_key);
  663. static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
  664. {
  665. /*
  666. * Our caller is either destroy_workqueue() or CPU_DEAD,
  667. * get_online_cpus() protects cwq->thread.
  668. */
  669. if (cwq->thread == NULL)
  670. return;
  671. lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  672. lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
  673. flush_cpu_workqueue(cwq);
  674. /*
  675. * If the caller is CPU_DEAD and cwq->worklist was not empty,
  676. * a concurrent flush_workqueue() can insert a barrier after us.
  677. * However, in that case run_workqueue() won't return and check
  678. * kthread_should_stop() until it flushes all work_struct's.
  679. * When ->worklist becomes empty it is safe to exit because no
  680. * more work_structs can be queued on this cwq: flush_workqueue
  681. * checks list_empty(), and a "normal" queue_work() can't use
  682. * a dead CPU.
  683. */
  684. kthread_stop(cwq->thread);
  685. cwq->thread = NULL;
  686. }
  687. /**
  688. * destroy_workqueue - safely terminate a workqueue
  689. * @wq: target workqueue
  690. *
  691. * Safely destroy a workqueue. All work currently pending will be done first.
  692. */
  693. void destroy_workqueue(struct workqueue_struct *wq)
  694. {
  695. const cpumask_t *cpu_map = wq_cpu_map(wq);
  696. int cpu;
  697. get_online_cpus();
  698. spin_lock(&workqueue_lock);
  699. list_del(&wq->list);
  700. spin_unlock(&workqueue_lock);
  701. for_each_cpu_mask_nr(cpu, *cpu_map)
  702. cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
  703. put_online_cpus();
  704. free_percpu(wq->cpu_wq);
  705. kfree(wq);
  706. }
  707. EXPORT_SYMBOL_GPL(destroy_workqueue);
  708. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  709. unsigned long action,
  710. void *hcpu)
  711. {
  712. unsigned int cpu = (unsigned long)hcpu;
  713. struct cpu_workqueue_struct *cwq;
  714. struct workqueue_struct *wq;
  715. action &= ~CPU_TASKS_FROZEN;
  716. switch (action) {
  717. case CPU_UP_PREPARE:
  718. cpu_set(cpu, cpu_populated_map);
  719. }
  720. list_for_each_entry(wq, &workqueues, list) {
  721. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  722. switch (action) {
  723. case CPU_UP_PREPARE:
  724. if (!create_workqueue_thread(cwq, cpu))
  725. break;
  726. printk(KERN_ERR "workqueue [%s] for %i failed\n",
  727. wq->name, cpu);
  728. return NOTIFY_BAD;
  729. case CPU_ONLINE:
  730. start_workqueue_thread(cwq, cpu);
  731. break;
  732. case CPU_UP_CANCELED:
  733. start_workqueue_thread(cwq, -1);
  734. case CPU_DEAD:
  735. cleanup_workqueue_thread(cwq);
  736. break;
  737. }
  738. }
  739. switch (action) {
  740. case CPU_UP_CANCELED:
  741. case CPU_DEAD:
  742. cpu_clear(cpu, cpu_populated_map);
  743. }
  744. return NOTIFY_OK;
  745. }
  746. void __init init_workqueues(void)
  747. {
  748. cpu_populated_map = cpu_online_map;
  749. singlethread_cpu = first_cpu(cpu_possible_map);
  750. cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
  751. hotcpu_notifier(workqueue_cpu_callback, 0);
  752. keventd_wq = create_workqueue("events");
  753. BUG_ON(!keventd_wq);
  754. }