workqueue.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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
  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.
  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. #define CREATE_TRACE_POINTS
  36. #include <trace/events/workqueue.h>
  37. /*
  38. * Structure fields follow one of the following exclusion rules.
  39. *
  40. * I: Set during initialization and read-only afterwards.
  41. *
  42. * L: cwq->lock protected. Access with cwq->lock held.
  43. *
  44. * W: workqueue_lock protected.
  45. */
  46. /*
  47. * The per-CPU workqueue (if single thread, we always use the first
  48. * possible cpu).
  49. */
  50. struct cpu_workqueue_struct {
  51. spinlock_t lock;
  52. struct list_head worklist;
  53. wait_queue_head_t more_work;
  54. struct work_struct *current_work;
  55. struct workqueue_struct *wq; /* I: the owning workqueue */
  56. struct task_struct *thread;
  57. } ____cacheline_aligned;
  58. /*
  59. * The externally visible workqueue abstraction is an array of
  60. * per-CPU workqueues:
  61. */
  62. struct workqueue_struct {
  63. unsigned int flags; /* I: WQ_* flags */
  64. struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */
  65. struct list_head list; /* W: list of all workqueues */
  66. const char *name; /* I: workqueue name */
  67. #ifdef CONFIG_LOCKDEP
  68. struct lockdep_map lockdep_map;
  69. #endif
  70. };
  71. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  72. static struct debug_obj_descr work_debug_descr;
  73. /*
  74. * fixup_init is called when:
  75. * - an active object is initialized
  76. */
  77. static int work_fixup_init(void *addr, enum debug_obj_state state)
  78. {
  79. struct work_struct *work = addr;
  80. switch (state) {
  81. case ODEBUG_STATE_ACTIVE:
  82. cancel_work_sync(work);
  83. debug_object_init(work, &work_debug_descr);
  84. return 1;
  85. default:
  86. return 0;
  87. }
  88. }
  89. /*
  90. * fixup_activate is called when:
  91. * - an active object is activated
  92. * - an unknown object is activated (might be a statically initialized object)
  93. */
  94. static int work_fixup_activate(void *addr, enum debug_obj_state state)
  95. {
  96. struct work_struct *work = addr;
  97. switch (state) {
  98. case ODEBUG_STATE_NOTAVAILABLE:
  99. /*
  100. * This is not really a fixup. The work struct was
  101. * statically initialized. We just make sure that it
  102. * is tracked in the object tracker.
  103. */
  104. if (test_bit(WORK_STRUCT_STATIC, work_data_bits(work))) {
  105. debug_object_init(work, &work_debug_descr);
  106. debug_object_activate(work, &work_debug_descr);
  107. return 0;
  108. }
  109. WARN_ON_ONCE(1);
  110. return 0;
  111. case ODEBUG_STATE_ACTIVE:
  112. WARN_ON(1);
  113. default:
  114. return 0;
  115. }
  116. }
  117. /*
  118. * fixup_free is called when:
  119. * - an active object is freed
  120. */
  121. static int work_fixup_free(void *addr, enum debug_obj_state state)
  122. {
  123. struct work_struct *work = addr;
  124. switch (state) {
  125. case ODEBUG_STATE_ACTIVE:
  126. cancel_work_sync(work);
  127. debug_object_free(work, &work_debug_descr);
  128. return 1;
  129. default:
  130. return 0;
  131. }
  132. }
  133. static struct debug_obj_descr work_debug_descr = {
  134. .name = "work_struct",
  135. .fixup_init = work_fixup_init,
  136. .fixup_activate = work_fixup_activate,
  137. .fixup_free = work_fixup_free,
  138. };
  139. static inline void debug_work_activate(struct work_struct *work)
  140. {
  141. debug_object_activate(work, &work_debug_descr);
  142. }
  143. static inline void debug_work_deactivate(struct work_struct *work)
  144. {
  145. debug_object_deactivate(work, &work_debug_descr);
  146. }
  147. void __init_work(struct work_struct *work, int onstack)
  148. {
  149. if (onstack)
  150. debug_object_init_on_stack(work, &work_debug_descr);
  151. else
  152. debug_object_init(work, &work_debug_descr);
  153. }
  154. EXPORT_SYMBOL_GPL(__init_work);
  155. void destroy_work_on_stack(struct work_struct *work)
  156. {
  157. debug_object_free(work, &work_debug_descr);
  158. }
  159. EXPORT_SYMBOL_GPL(destroy_work_on_stack);
  160. #else
  161. static inline void debug_work_activate(struct work_struct *work) { }
  162. static inline void debug_work_deactivate(struct work_struct *work) { }
  163. #endif
  164. /* Serializes the accesses to the list of workqueues. */
  165. static DEFINE_SPINLOCK(workqueue_lock);
  166. static LIST_HEAD(workqueues);
  167. static int singlethread_cpu __read_mostly;
  168. static const struct cpumask *cpu_singlethread_map __read_mostly;
  169. /*
  170. * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
  171. * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
  172. * which comes in between can't use for_each_online_cpu(). We could
  173. * use cpu_possible_map, the cpumask below is more a documentation
  174. * than optimization.
  175. */
  176. static cpumask_var_t cpu_populated_map __read_mostly;
  177. /* If it's single threaded, it isn't in the list of workqueues. */
  178. static inline bool is_wq_single_threaded(struct workqueue_struct *wq)
  179. {
  180. return wq->flags & WQ_SINGLE_THREAD;
  181. }
  182. static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq)
  183. {
  184. return is_wq_single_threaded(wq)
  185. ? cpu_singlethread_map : cpu_populated_map;
  186. }
  187. static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
  188. struct workqueue_struct *wq)
  189. {
  190. if (unlikely(is_wq_single_threaded(wq)))
  191. cpu = singlethread_cpu;
  192. return per_cpu_ptr(wq->cpu_wq, cpu);
  193. }
  194. /*
  195. * Set the workqueue on which a work item is to be run
  196. * - Must *only* be called if the pending flag is set
  197. */
  198. static inline void set_wq_data(struct work_struct *work,
  199. struct cpu_workqueue_struct *cwq,
  200. unsigned long extra_flags)
  201. {
  202. BUG_ON(!work_pending(work));
  203. atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
  204. (1UL << WORK_STRUCT_PENDING) | extra_flags);
  205. }
  206. /*
  207. * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
  208. */
  209. static inline void clear_wq_data(struct work_struct *work)
  210. {
  211. atomic_long_set(&work->data, work_static(work));
  212. }
  213. static inline
  214. struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
  215. {
  216. return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
  217. }
  218. /**
  219. * insert_work - insert a work into cwq
  220. * @cwq: cwq @work belongs to
  221. * @work: work to insert
  222. * @head: insertion point
  223. * @extra_flags: extra WORK_STRUCT_* flags to set
  224. *
  225. * Insert @work into @cwq after @head.
  226. *
  227. * CONTEXT:
  228. * spin_lock_irq(cwq->lock).
  229. */
  230. static void insert_work(struct cpu_workqueue_struct *cwq,
  231. struct work_struct *work, struct list_head *head,
  232. unsigned int extra_flags)
  233. {
  234. trace_workqueue_insertion(cwq->thread, work);
  235. /* we own @work, set data and link */
  236. set_wq_data(work, cwq, extra_flags);
  237. /*
  238. * Ensure that we get the right work->data if we see the
  239. * result of list_add() below, see try_to_grab_pending().
  240. */
  241. smp_wmb();
  242. list_add_tail(&work->entry, head);
  243. wake_up(&cwq->more_work);
  244. }
  245. static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
  246. struct work_struct *work)
  247. {
  248. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  249. unsigned long flags;
  250. debug_work_activate(work);
  251. spin_lock_irqsave(&cwq->lock, flags);
  252. BUG_ON(!list_empty(&work->entry));
  253. insert_work(cwq, work, &cwq->worklist, 0);
  254. spin_unlock_irqrestore(&cwq->lock, flags);
  255. }
  256. /**
  257. * queue_work - queue work on a workqueue
  258. * @wq: workqueue to use
  259. * @work: work to queue
  260. *
  261. * Returns 0 if @work was already on a queue, non-zero otherwise.
  262. *
  263. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  264. * it can be processed by another CPU.
  265. */
  266. int queue_work(struct workqueue_struct *wq, struct work_struct *work)
  267. {
  268. int ret;
  269. ret = queue_work_on(get_cpu(), wq, work);
  270. put_cpu();
  271. return ret;
  272. }
  273. EXPORT_SYMBOL_GPL(queue_work);
  274. /**
  275. * queue_work_on - queue work on specific cpu
  276. * @cpu: CPU number to execute work on
  277. * @wq: workqueue to use
  278. * @work: work to queue
  279. *
  280. * Returns 0 if @work was already on a queue, non-zero otherwise.
  281. *
  282. * We queue the work to a specific CPU, the caller must ensure it
  283. * can't go away.
  284. */
  285. int
  286. queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
  287. {
  288. int ret = 0;
  289. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  290. __queue_work(cpu, wq, work);
  291. ret = 1;
  292. }
  293. return ret;
  294. }
  295. EXPORT_SYMBOL_GPL(queue_work_on);
  296. static void delayed_work_timer_fn(unsigned long __data)
  297. {
  298. struct delayed_work *dwork = (struct delayed_work *)__data;
  299. struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
  300. __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
  301. }
  302. /**
  303. * queue_delayed_work - queue work on a workqueue after delay
  304. * @wq: workqueue to use
  305. * @dwork: delayable work to queue
  306. * @delay: number of jiffies to wait before queueing
  307. *
  308. * Returns 0 if @work was already on a queue, non-zero otherwise.
  309. */
  310. int queue_delayed_work(struct workqueue_struct *wq,
  311. struct delayed_work *dwork, unsigned long delay)
  312. {
  313. if (delay == 0)
  314. return queue_work(wq, &dwork->work);
  315. return queue_delayed_work_on(-1, wq, dwork, delay);
  316. }
  317. EXPORT_SYMBOL_GPL(queue_delayed_work);
  318. /**
  319. * queue_delayed_work_on - queue work on specific CPU after delay
  320. * @cpu: CPU number to execute work on
  321. * @wq: workqueue to use
  322. * @dwork: work to queue
  323. * @delay: number of jiffies to wait before queueing
  324. *
  325. * Returns 0 if @work was already on a queue, non-zero otherwise.
  326. */
  327. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  328. struct delayed_work *dwork, unsigned long delay)
  329. {
  330. int ret = 0;
  331. struct timer_list *timer = &dwork->timer;
  332. struct work_struct *work = &dwork->work;
  333. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  334. BUG_ON(timer_pending(timer));
  335. BUG_ON(!list_empty(&work->entry));
  336. timer_stats_timer_set_start_info(&dwork->timer);
  337. /* This stores cwq for the moment, for the timer_fn */
  338. set_wq_data(work, get_cwq(raw_smp_processor_id(), wq), 0);
  339. timer->expires = jiffies + delay;
  340. timer->data = (unsigned long)dwork;
  341. timer->function = delayed_work_timer_fn;
  342. if (unlikely(cpu >= 0))
  343. add_timer_on(timer, cpu);
  344. else
  345. add_timer(timer);
  346. ret = 1;
  347. }
  348. return ret;
  349. }
  350. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  351. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  352. {
  353. spin_lock_irq(&cwq->lock);
  354. while (!list_empty(&cwq->worklist)) {
  355. struct work_struct *work = list_entry(cwq->worklist.next,
  356. struct work_struct, entry);
  357. work_func_t f = work->func;
  358. #ifdef CONFIG_LOCKDEP
  359. /*
  360. * It is permissible to free the struct work_struct
  361. * from inside the function that is called from it,
  362. * this we need to take into account for lockdep too.
  363. * To avoid bogus "held lock freed" warnings as well
  364. * as problems when looking into work->lockdep_map,
  365. * make a copy and use that here.
  366. */
  367. struct lockdep_map lockdep_map = work->lockdep_map;
  368. #endif
  369. trace_workqueue_execution(cwq->thread, work);
  370. debug_work_deactivate(work);
  371. cwq->current_work = work;
  372. list_del_init(cwq->worklist.next);
  373. spin_unlock_irq(&cwq->lock);
  374. BUG_ON(get_wq_data(work) != cwq);
  375. work_clear_pending(work);
  376. lock_map_acquire(&cwq->wq->lockdep_map);
  377. lock_map_acquire(&lockdep_map);
  378. f(work);
  379. lock_map_release(&lockdep_map);
  380. lock_map_release(&cwq->wq->lockdep_map);
  381. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  382. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  383. "%s/0x%08x/%d\n",
  384. current->comm, preempt_count(),
  385. task_pid_nr(current));
  386. printk(KERN_ERR " last function: ");
  387. print_symbol("%s\n", (unsigned long)f);
  388. debug_show_held_locks(current);
  389. dump_stack();
  390. }
  391. spin_lock_irq(&cwq->lock);
  392. cwq->current_work = NULL;
  393. }
  394. spin_unlock_irq(&cwq->lock);
  395. }
  396. /**
  397. * worker_thread - the worker thread function
  398. * @__cwq: cwq to serve
  399. *
  400. * The cwq worker thread function.
  401. */
  402. static int worker_thread(void *__cwq)
  403. {
  404. struct cpu_workqueue_struct *cwq = __cwq;
  405. DEFINE_WAIT(wait);
  406. if (cwq->wq->flags & WQ_FREEZEABLE)
  407. set_freezable();
  408. for (;;) {
  409. prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
  410. if (!freezing(current) &&
  411. !kthread_should_stop() &&
  412. list_empty(&cwq->worklist))
  413. schedule();
  414. finish_wait(&cwq->more_work, &wait);
  415. try_to_freeze();
  416. if (kthread_should_stop())
  417. break;
  418. run_workqueue(cwq);
  419. }
  420. return 0;
  421. }
  422. struct wq_barrier {
  423. struct work_struct work;
  424. struct completion done;
  425. };
  426. static void wq_barrier_func(struct work_struct *work)
  427. {
  428. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  429. complete(&barr->done);
  430. }
  431. /**
  432. * insert_wq_barrier - insert a barrier work
  433. * @cwq: cwq to insert barrier into
  434. * @barr: wq_barrier to insert
  435. * @head: insertion point
  436. *
  437. * Insert barrier @barr into @cwq before @head.
  438. *
  439. * CONTEXT:
  440. * spin_lock_irq(cwq->lock).
  441. */
  442. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  443. struct wq_barrier *barr, struct list_head *head)
  444. {
  445. /*
  446. * debugobject calls are safe here even with cwq->lock locked
  447. * as we know for sure that this will not trigger any of the
  448. * checks and call back into the fixup functions where we
  449. * might deadlock.
  450. */
  451. INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
  452. __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
  453. init_completion(&barr->done);
  454. debug_work_activate(&barr->work);
  455. insert_work(cwq, &barr->work, head, 0);
  456. }
  457. static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  458. {
  459. int active = 0;
  460. struct wq_barrier barr;
  461. WARN_ON(cwq->thread == current);
  462. spin_lock_irq(&cwq->lock);
  463. if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
  464. insert_wq_barrier(cwq, &barr, &cwq->worklist);
  465. active = 1;
  466. }
  467. spin_unlock_irq(&cwq->lock);
  468. if (active) {
  469. wait_for_completion(&barr.done);
  470. destroy_work_on_stack(&barr.work);
  471. }
  472. return active;
  473. }
  474. /**
  475. * flush_workqueue - ensure that any scheduled work has run to completion.
  476. * @wq: workqueue to flush
  477. *
  478. * Forces execution of the workqueue and blocks until its completion.
  479. * This is typically used in driver shutdown handlers.
  480. *
  481. * We sleep until all works which were queued on entry have been handled,
  482. * but we are not livelocked by new incoming ones.
  483. */
  484. void flush_workqueue(struct workqueue_struct *wq)
  485. {
  486. const struct cpumask *cpu_map = wq_cpu_map(wq);
  487. int cpu;
  488. might_sleep();
  489. lock_map_acquire(&wq->lockdep_map);
  490. lock_map_release(&wq->lockdep_map);
  491. for_each_cpu(cpu, cpu_map)
  492. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  493. }
  494. EXPORT_SYMBOL_GPL(flush_workqueue);
  495. /**
  496. * flush_work - block until a work_struct's callback has terminated
  497. * @work: the work which is to be flushed
  498. *
  499. * Returns false if @work has already terminated.
  500. *
  501. * It is expected that, prior to calling flush_work(), the caller has
  502. * arranged for the work to not be requeued, otherwise it doesn't make
  503. * sense to use this function.
  504. */
  505. int flush_work(struct work_struct *work)
  506. {
  507. struct cpu_workqueue_struct *cwq;
  508. struct list_head *prev;
  509. struct wq_barrier barr;
  510. might_sleep();
  511. cwq = get_wq_data(work);
  512. if (!cwq)
  513. return 0;
  514. lock_map_acquire(&cwq->wq->lockdep_map);
  515. lock_map_release(&cwq->wq->lockdep_map);
  516. spin_lock_irq(&cwq->lock);
  517. if (!list_empty(&work->entry)) {
  518. /*
  519. * See the comment near try_to_grab_pending()->smp_rmb().
  520. * If it was re-queued under us we are not going to wait.
  521. */
  522. smp_rmb();
  523. if (unlikely(cwq != get_wq_data(work)))
  524. goto already_gone;
  525. prev = &work->entry;
  526. } else {
  527. if (cwq->current_work != work)
  528. goto already_gone;
  529. prev = &cwq->worklist;
  530. }
  531. insert_wq_barrier(cwq, &barr, prev->next);
  532. spin_unlock_irq(&cwq->lock);
  533. wait_for_completion(&barr.done);
  534. destroy_work_on_stack(&barr.work);
  535. return 1;
  536. already_gone:
  537. spin_unlock_irq(&cwq->lock);
  538. return 0;
  539. }
  540. EXPORT_SYMBOL_GPL(flush_work);
  541. /*
  542. * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
  543. * so this work can't be re-armed in any way.
  544. */
  545. static int try_to_grab_pending(struct work_struct *work)
  546. {
  547. struct cpu_workqueue_struct *cwq;
  548. int ret = -1;
  549. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
  550. return 0;
  551. /*
  552. * The queueing is in progress, or it is already queued. Try to
  553. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  554. */
  555. cwq = get_wq_data(work);
  556. if (!cwq)
  557. return ret;
  558. spin_lock_irq(&cwq->lock);
  559. if (!list_empty(&work->entry)) {
  560. /*
  561. * This work is queued, but perhaps we locked the wrong cwq.
  562. * In that case we must see the new value after rmb(), see
  563. * insert_work()->wmb().
  564. */
  565. smp_rmb();
  566. if (cwq == get_wq_data(work)) {
  567. debug_work_deactivate(work);
  568. list_del_init(&work->entry);
  569. ret = 1;
  570. }
  571. }
  572. spin_unlock_irq(&cwq->lock);
  573. return ret;
  574. }
  575. static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
  576. struct work_struct *work)
  577. {
  578. struct wq_barrier barr;
  579. int running = 0;
  580. spin_lock_irq(&cwq->lock);
  581. if (unlikely(cwq->current_work == work)) {
  582. insert_wq_barrier(cwq, &barr, cwq->worklist.next);
  583. running = 1;
  584. }
  585. spin_unlock_irq(&cwq->lock);
  586. if (unlikely(running)) {
  587. wait_for_completion(&barr.done);
  588. destroy_work_on_stack(&barr.work);
  589. }
  590. }
  591. static void wait_on_work(struct work_struct *work)
  592. {
  593. struct cpu_workqueue_struct *cwq;
  594. struct workqueue_struct *wq;
  595. const struct cpumask *cpu_map;
  596. int cpu;
  597. might_sleep();
  598. lock_map_acquire(&work->lockdep_map);
  599. lock_map_release(&work->lockdep_map);
  600. cwq = get_wq_data(work);
  601. if (!cwq)
  602. return;
  603. wq = cwq->wq;
  604. cpu_map = wq_cpu_map(wq);
  605. for_each_cpu(cpu, cpu_map)
  606. wait_on_cpu_work(get_cwq(cpu, wq), work);
  607. }
  608. static int __cancel_work_timer(struct work_struct *work,
  609. struct timer_list* timer)
  610. {
  611. int ret;
  612. do {
  613. ret = (timer && likely(del_timer(timer)));
  614. if (!ret)
  615. ret = try_to_grab_pending(work);
  616. wait_on_work(work);
  617. } while (unlikely(ret < 0));
  618. clear_wq_data(work);
  619. return ret;
  620. }
  621. /**
  622. * cancel_work_sync - block until a work_struct's callback has terminated
  623. * @work: the work which is to be flushed
  624. *
  625. * Returns true if @work was pending.
  626. *
  627. * cancel_work_sync() will cancel the work if it is queued. If the work's
  628. * callback appears to be running, cancel_work_sync() will block until it
  629. * has completed.
  630. *
  631. * It is possible to use this function if the work re-queues itself. It can
  632. * cancel the work even if it migrates to another workqueue, however in that
  633. * case it only guarantees that work->func() has completed on the last queued
  634. * workqueue.
  635. *
  636. * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
  637. * pending, otherwise it goes into a busy-wait loop until the timer expires.
  638. *
  639. * The caller must ensure that workqueue_struct on which this work was last
  640. * queued can't be destroyed before this function returns.
  641. */
  642. int cancel_work_sync(struct work_struct *work)
  643. {
  644. return __cancel_work_timer(work, NULL);
  645. }
  646. EXPORT_SYMBOL_GPL(cancel_work_sync);
  647. /**
  648. * cancel_delayed_work_sync - reliably kill off a delayed work.
  649. * @dwork: the delayed work struct
  650. *
  651. * Returns true if @dwork was pending.
  652. *
  653. * It is possible to use this function if @dwork rearms itself via queue_work()
  654. * or queue_delayed_work(). See also the comment for cancel_work_sync().
  655. */
  656. int cancel_delayed_work_sync(struct delayed_work *dwork)
  657. {
  658. return __cancel_work_timer(&dwork->work, &dwork->timer);
  659. }
  660. EXPORT_SYMBOL(cancel_delayed_work_sync);
  661. static struct workqueue_struct *keventd_wq __read_mostly;
  662. /**
  663. * schedule_work - put work task in global workqueue
  664. * @work: job to be done
  665. *
  666. * Returns zero if @work was already on the kernel-global workqueue and
  667. * non-zero otherwise.
  668. *
  669. * This puts a job in the kernel-global workqueue if it was not already
  670. * queued and leaves it in the same position on the kernel-global
  671. * workqueue otherwise.
  672. */
  673. int schedule_work(struct work_struct *work)
  674. {
  675. return queue_work(keventd_wq, work);
  676. }
  677. EXPORT_SYMBOL(schedule_work);
  678. /*
  679. * schedule_work_on - put work task on a specific cpu
  680. * @cpu: cpu to put the work task on
  681. * @work: job to be done
  682. *
  683. * This puts a job on a specific cpu
  684. */
  685. int schedule_work_on(int cpu, struct work_struct *work)
  686. {
  687. return queue_work_on(cpu, keventd_wq, work);
  688. }
  689. EXPORT_SYMBOL(schedule_work_on);
  690. /**
  691. * schedule_delayed_work - put work task in global workqueue after delay
  692. * @dwork: job to be done
  693. * @delay: number of jiffies to wait or 0 for immediate execution
  694. *
  695. * After waiting for a given time this puts a job in the kernel-global
  696. * workqueue.
  697. */
  698. int schedule_delayed_work(struct delayed_work *dwork,
  699. unsigned long delay)
  700. {
  701. return queue_delayed_work(keventd_wq, dwork, delay);
  702. }
  703. EXPORT_SYMBOL(schedule_delayed_work);
  704. /**
  705. * flush_delayed_work - block until a dwork_struct's callback has terminated
  706. * @dwork: the delayed work which is to be flushed
  707. *
  708. * Any timeout is cancelled, and any pending work is run immediately.
  709. */
  710. void flush_delayed_work(struct delayed_work *dwork)
  711. {
  712. if (del_timer_sync(&dwork->timer)) {
  713. __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
  714. &dwork->work);
  715. put_cpu();
  716. }
  717. flush_work(&dwork->work);
  718. }
  719. EXPORT_SYMBOL(flush_delayed_work);
  720. /**
  721. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  722. * @cpu: cpu to use
  723. * @dwork: job to be done
  724. * @delay: number of jiffies to wait
  725. *
  726. * After waiting for a given time this puts a job in the kernel-global
  727. * workqueue on the specified CPU.
  728. */
  729. int schedule_delayed_work_on(int cpu,
  730. struct delayed_work *dwork, unsigned long delay)
  731. {
  732. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  733. }
  734. EXPORT_SYMBOL(schedule_delayed_work_on);
  735. /**
  736. * schedule_on_each_cpu - call a function on each online CPU from keventd
  737. * @func: the function to call
  738. *
  739. * Returns zero on success.
  740. * Returns -ve errno on failure.
  741. *
  742. * schedule_on_each_cpu() is very slow.
  743. */
  744. int schedule_on_each_cpu(work_func_t func)
  745. {
  746. int cpu;
  747. int orig = -1;
  748. struct work_struct *works;
  749. works = alloc_percpu(struct work_struct);
  750. if (!works)
  751. return -ENOMEM;
  752. get_online_cpus();
  753. /*
  754. * When running in keventd don't schedule a work item on
  755. * itself. Can just call directly because the work queue is
  756. * already bound. This also is faster.
  757. */
  758. if (current_is_keventd())
  759. orig = raw_smp_processor_id();
  760. for_each_online_cpu(cpu) {
  761. struct work_struct *work = per_cpu_ptr(works, cpu);
  762. INIT_WORK(work, func);
  763. if (cpu != orig)
  764. schedule_work_on(cpu, work);
  765. }
  766. if (orig >= 0)
  767. func(per_cpu_ptr(works, orig));
  768. for_each_online_cpu(cpu)
  769. flush_work(per_cpu_ptr(works, cpu));
  770. put_online_cpus();
  771. free_percpu(works);
  772. return 0;
  773. }
  774. /**
  775. * flush_scheduled_work - ensure that any scheduled work has run to completion.
  776. *
  777. * Forces execution of the kernel-global workqueue and blocks until its
  778. * completion.
  779. *
  780. * Think twice before calling this function! It's very easy to get into
  781. * trouble if you don't take great care. Either of the following situations
  782. * will lead to deadlock:
  783. *
  784. * One of the work items currently on the workqueue needs to acquire
  785. * a lock held by your code or its caller.
  786. *
  787. * Your code is running in the context of a work routine.
  788. *
  789. * They will be detected by lockdep when they occur, but the first might not
  790. * occur very often. It depends on what work items are on the workqueue and
  791. * what locks they need, which you have no control over.
  792. *
  793. * In most situations flushing the entire workqueue is overkill; you merely
  794. * need to know that a particular work item isn't queued and isn't running.
  795. * In such cases you should use cancel_delayed_work_sync() or
  796. * cancel_work_sync() instead.
  797. */
  798. void flush_scheduled_work(void)
  799. {
  800. flush_workqueue(keventd_wq);
  801. }
  802. EXPORT_SYMBOL(flush_scheduled_work);
  803. /**
  804. * execute_in_process_context - reliably execute the routine with user context
  805. * @fn: the function to execute
  806. * @ew: guaranteed storage for the execute work structure (must
  807. * be available when the work executes)
  808. *
  809. * Executes the function immediately if process context is available,
  810. * otherwise schedules the function for delayed execution.
  811. *
  812. * Returns: 0 - function was executed
  813. * 1 - function was scheduled for execution
  814. */
  815. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  816. {
  817. if (!in_interrupt()) {
  818. fn(&ew->work);
  819. return 0;
  820. }
  821. INIT_WORK(&ew->work, fn);
  822. schedule_work(&ew->work);
  823. return 1;
  824. }
  825. EXPORT_SYMBOL_GPL(execute_in_process_context);
  826. int keventd_up(void)
  827. {
  828. return keventd_wq != NULL;
  829. }
  830. int current_is_keventd(void)
  831. {
  832. struct cpu_workqueue_struct *cwq;
  833. int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  834. int ret = 0;
  835. BUG_ON(!keventd_wq);
  836. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  837. if (current == cwq->thread)
  838. ret = 1;
  839. return ret;
  840. }
  841. static struct cpu_workqueue_struct *
  842. init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
  843. {
  844. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  845. cwq->wq = wq;
  846. spin_lock_init(&cwq->lock);
  847. INIT_LIST_HEAD(&cwq->worklist);
  848. init_waitqueue_head(&cwq->more_work);
  849. return cwq;
  850. }
  851. static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  852. {
  853. struct workqueue_struct *wq = cwq->wq;
  854. const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
  855. struct task_struct *p;
  856. p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
  857. /*
  858. * Nobody can add the work_struct to this cwq,
  859. * if (caller is __create_workqueue)
  860. * nobody should see this wq
  861. * else // caller is CPU_UP_PREPARE
  862. * cpu is not on cpu_online_map
  863. * so we can abort safely.
  864. */
  865. if (IS_ERR(p))
  866. return PTR_ERR(p);
  867. cwq->thread = p;
  868. trace_workqueue_creation(cwq->thread, cpu);
  869. return 0;
  870. }
  871. static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  872. {
  873. struct task_struct *p = cwq->thread;
  874. if (p != NULL) {
  875. if (cpu >= 0)
  876. kthread_bind(p, cpu);
  877. wake_up_process(p);
  878. }
  879. }
  880. struct workqueue_struct *__create_workqueue_key(const char *name,
  881. unsigned int flags,
  882. struct lock_class_key *key,
  883. const char *lock_name)
  884. {
  885. struct workqueue_struct *wq;
  886. struct cpu_workqueue_struct *cwq;
  887. int err = 0, cpu;
  888. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  889. if (!wq)
  890. goto err;
  891. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  892. if (!wq->cpu_wq)
  893. goto err;
  894. wq->flags = flags;
  895. wq->name = name;
  896. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  897. INIT_LIST_HEAD(&wq->list);
  898. if (flags & WQ_SINGLE_THREAD) {
  899. cwq = init_cpu_workqueue(wq, singlethread_cpu);
  900. err = create_workqueue_thread(cwq, singlethread_cpu);
  901. start_workqueue_thread(cwq, -1);
  902. } else {
  903. cpu_maps_update_begin();
  904. /*
  905. * We must place this wq on list even if the code below fails.
  906. * cpu_down(cpu) can remove cpu from cpu_populated_map before
  907. * destroy_workqueue() takes the lock, in that case we leak
  908. * cwq[cpu]->thread.
  909. */
  910. spin_lock(&workqueue_lock);
  911. list_add(&wq->list, &workqueues);
  912. spin_unlock(&workqueue_lock);
  913. /*
  914. * We must initialize cwqs for each possible cpu even if we
  915. * are going to call destroy_workqueue() finally. Otherwise
  916. * cpu_up() can hit the uninitialized cwq once we drop the
  917. * lock.
  918. */
  919. for_each_possible_cpu(cpu) {
  920. cwq = init_cpu_workqueue(wq, cpu);
  921. if (err || !cpu_online(cpu))
  922. continue;
  923. err = create_workqueue_thread(cwq, cpu);
  924. start_workqueue_thread(cwq, cpu);
  925. }
  926. cpu_maps_update_done();
  927. }
  928. if (err) {
  929. destroy_workqueue(wq);
  930. wq = NULL;
  931. }
  932. return wq;
  933. err:
  934. if (wq) {
  935. free_percpu(wq->cpu_wq);
  936. kfree(wq);
  937. }
  938. return NULL;
  939. }
  940. EXPORT_SYMBOL_GPL(__create_workqueue_key);
  941. static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
  942. {
  943. /*
  944. * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
  945. * cpu_add_remove_lock protects cwq->thread.
  946. */
  947. if (cwq->thread == NULL)
  948. return;
  949. lock_map_acquire(&cwq->wq->lockdep_map);
  950. lock_map_release(&cwq->wq->lockdep_map);
  951. flush_cpu_workqueue(cwq);
  952. /*
  953. * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
  954. * a concurrent flush_workqueue() can insert a barrier after us.
  955. * However, in that case run_workqueue() won't return and check
  956. * kthread_should_stop() until it flushes all work_struct's.
  957. * When ->worklist becomes empty it is safe to exit because no
  958. * more work_structs can be queued on this cwq: flush_workqueue
  959. * checks list_empty(), and a "normal" queue_work() can't use
  960. * a dead CPU.
  961. */
  962. trace_workqueue_destruction(cwq->thread);
  963. kthread_stop(cwq->thread);
  964. cwq->thread = NULL;
  965. }
  966. /**
  967. * destroy_workqueue - safely terminate a workqueue
  968. * @wq: target workqueue
  969. *
  970. * Safely destroy a workqueue. All work currently pending will be done first.
  971. */
  972. void destroy_workqueue(struct workqueue_struct *wq)
  973. {
  974. const struct cpumask *cpu_map = wq_cpu_map(wq);
  975. int cpu;
  976. cpu_maps_update_begin();
  977. spin_lock(&workqueue_lock);
  978. list_del(&wq->list);
  979. spin_unlock(&workqueue_lock);
  980. for_each_cpu(cpu, cpu_map)
  981. cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
  982. cpu_maps_update_done();
  983. free_percpu(wq->cpu_wq);
  984. kfree(wq);
  985. }
  986. EXPORT_SYMBOL_GPL(destroy_workqueue);
  987. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  988. unsigned long action,
  989. void *hcpu)
  990. {
  991. unsigned int cpu = (unsigned long)hcpu;
  992. struct cpu_workqueue_struct *cwq;
  993. struct workqueue_struct *wq;
  994. int err = 0;
  995. action &= ~CPU_TASKS_FROZEN;
  996. switch (action) {
  997. case CPU_UP_PREPARE:
  998. cpumask_set_cpu(cpu, cpu_populated_map);
  999. }
  1000. undo:
  1001. list_for_each_entry(wq, &workqueues, list) {
  1002. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  1003. switch (action) {
  1004. case CPU_UP_PREPARE:
  1005. err = create_workqueue_thread(cwq, cpu);
  1006. if (!err)
  1007. break;
  1008. printk(KERN_ERR "workqueue [%s] for %i failed\n",
  1009. wq->name, cpu);
  1010. action = CPU_UP_CANCELED;
  1011. err = -ENOMEM;
  1012. goto undo;
  1013. case CPU_ONLINE:
  1014. start_workqueue_thread(cwq, cpu);
  1015. break;
  1016. case CPU_UP_CANCELED:
  1017. start_workqueue_thread(cwq, -1);
  1018. case CPU_POST_DEAD:
  1019. cleanup_workqueue_thread(cwq);
  1020. break;
  1021. }
  1022. }
  1023. switch (action) {
  1024. case CPU_UP_CANCELED:
  1025. case CPU_POST_DEAD:
  1026. cpumask_clear_cpu(cpu, cpu_populated_map);
  1027. }
  1028. return notifier_from_errno(err);
  1029. }
  1030. #ifdef CONFIG_SMP
  1031. struct work_for_cpu {
  1032. struct completion completion;
  1033. long (*fn)(void *);
  1034. void *arg;
  1035. long ret;
  1036. };
  1037. static int do_work_for_cpu(void *_wfc)
  1038. {
  1039. struct work_for_cpu *wfc = _wfc;
  1040. wfc->ret = wfc->fn(wfc->arg);
  1041. complete(&wfc->completion);
  1042. return 0;
  1043. }
  1044. /**
  1045. * work_on_cpu - run a function in user context on a particular cpu
  1046. * @cpu: the cpu to run on
  1047. * @fn: the function to run
  1048. * @arg: the function arg
  1049. *
  1050. * This will return the value @fn returns.
  1051. * It is up to the caller to ensure that the cpu doesn't go offline.
  1052. * The caller must not hold any locks which would prevent @fn from completing.
  1053. */
  1054. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  1055. {
  1056. struct task_struct *sub_thread;
  1057. struct work_for_cpu wfc = {
  1058. .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
  1059. .fn = fn,
  1060. .arg = arg,
  1061. };
  1062. sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
  1063. if (IS_ERR(sub_thread))
  1064. return PTR_ERR(sub_thread);
  1065. kthread_bind(sub_thread, cpu);
  1066. wake_up_process(sub_thread);
  1067. wait_for_completion(&wfc.completion);
  1068. return wfc.ret;
  1069. }
  1070. EXPORT_SYMBOL_GPL(work_on_cpu);
  1071. #endif /* CONFIG_SMP */
  1072. void __init init_workqueues(void)
  1073. {
  1074. alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
  1075. cpumask_copy(cpu_populated_map, cpu_online_mask);
  1076. singlethread_cpu = cpumask_first(cpu_possible_mask);
  1077. cpu_singlethread_map = cpumask_of(singlethread_cpu);
  1078. hotcpu_notifier(workqueue_cpu_callback, 0);
  1079. keventd_wq = create_workqueue("events");
  1080. BUG_ON(!keventd_wq);
  1081. }