workqueue.c 29 KB

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