workqueue.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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_BIT, 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. 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_BIT, 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_BIT, 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. /**
  352. * process_one_work - process single work
  353. * @cwq: cwq to process work for
  354. * @work: work to process
  355. *
  356. * Process @work. This function contains all the logics necessary to
  357. * process a single work including synchronization against and
  358. * interaction with other workers on the same cpu, queueing and
  359. * flushing. As long as context requirement is met, any worker can
  360. * call this function to process a work.
  361. *
  362. * CONTEXT:
  363. * spin_lock_irq(cwq->lock) which is released and regrabbed.
  364. */
  365. static void process_one_work(struct cpu_workqueue_struct *cwq,
  366. struct work_struct *work)
  367. {
  368. work_func_t f = work->func;
  369. #ifdef CONFIG_LOCKDEP
  370. /*
  371. * It is permissible to free the struct work_struct from
  372. * inside the function that is called from it, this we need to
  373. * take into account for lockdep too. To avoid bogus "held
  374. * lock freed" warnings as well as problems when looking into
  375. * work->lockdep_map, make a copy and use that here.
  376. */
  377. struct lockdep_map lockdep_map = work->lockdep_map;
  378. #endif
  379. /* claim and process */
  380. trace_workqueue_execution(cwq->thread, work);
  381. debug_work_deactivate(work);
  382. cwq->current_work = work;
  383. list_del_init(&work->entry);
  384. spin_unlock_irq(&cwq->lock);
  385. BUG_ON(get_wq_data(work) != cwq);
  386. work_clear_pending(work);
  387. lock_map_acquire(&cwq->wq->lockdep_map);
  388. lock_map_acquire(&lockdep_map);
  389. f(work);
  390. lock_map_release(&lockdep_map);
  391. lock_map_release(&cwq->wq->lockdep_map);
  392. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  393. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  394. "%s/0x%08x/%d\n",
  395. current->comm, preempt_count(), task_pid_nr(current));
  396. printk(KERN_ERR " last function: ");
  397. print_symbol("%s\n", (unsigned long)f);
  398. debug_show_held_locks(current);
  399. dump_stack();
  400. }
  401. spin_lock_irq(&cwq->lock);
  402. /* we're done with it, release */
  403. cwq->current_work = NULL;
  404. }
  405. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  406. {
  407. spin_lock_irq(&cwq->lock);
  408. while (!list_empty(&cwq->worklist)) {
  409. struct work_struct *work = list_entry(cwq->worklist.next,
  410. struct work_struct, entry);
  411. process_one_work(cwq, work);
  412. }
  413. spin_unlock_irq(&cwq->lock);
  414. }
  415. /**
  416. * worker_thread - the worker thread function
  417. * @__cwq: cwq to serve
  418. *
  419. * The cwq worker thread function.
  420. */
  421. static int worker_thread(void *__cwq)
  422. {
  423. struct cpu_workqueue_struct *cwq = __cwq;
  424. DEFINE_WAIT(wait);
  425. if (cwq->wq->flags & WQ_FREEZEABLE)
  426. set_freezable();
  427. for (;;) {
  428. prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
  429. if (!freezing(current) &&
  430. !kthread_should_stop() &&
  431. list_empty(&cwq->worklist))
  432. schedule();
  433. finish_wait(&cwq->more_work, &wait);
  434. try_to_freeze();
  435. if (kthread_should_stop())
  436. break;
  437. run_workqueue(cwq);
  438. }
  439. return 0;
  440. }
  441. struct wq_barrier {
  442. struct work_struct work;
  443. struct completion done;
  444. };
  445. static void wq_barrier_func(struct work_struct *work)
  446. {
  447. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  448. complete(&barr->done);
  449. }
  450. /**
  451. * insert_wq_barrier - insert a barrier work
  452. * @cwq: cwq to insert barrier into
  453. * @barr: wq_barrier to insert
  454. * @head: insertion point
  455. *
  456. * Insert barrier @barr into @cwq before @head.
  457. *
  458. * CONTEXT:
  459. * spin_lock_irq(cwq->lock).
  460. */
  461. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  462. struct wq_barrier *barr, struct list_head *head)
  463. {
  464. /*
  465. * debugobject calls are safe here even with cwq->lock locked
  466. * as we know for sure that this will not trigger any of the
  467. * checks and call back into the fixup functions where we
  468. * might deadlock.
  469. */
  470. INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
  471. __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
  472. init_completion(&barr->done);
  473. debug_work_activate(&barr->work);
  474. insert_work(cwq, &barr->work, head, 0);
  475. }
  476. static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  477. {
  478. int active = 0;
  479. struct wq_barrier barr;
  480. WARN_ON(cwq->thread == current);
  481. spin_lock_irq(&cwq->lock);
  482. if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
  483. insert_wq_barrier(cwq, &barr, &cwq->worklist);
  484. active = 1;
  485. }
  486. spin_unlock_irq(&cwq->lock);
  487. if (active) {
  488. wait_for_completion(&barr.done);
  489. destroy_work_on_stack(&barr.work);
  490. }
  491. return active;
  492. }
  493. /**
  494. * flush_workqueue - ensure that any scheduled work has run to completion.
  495. * @wq: workqueue to flush
  496. *
  497. * Forces execution of the workqueue and blocks until its completion.
  498. * This is typically used in driver shutdown handlers.
  499. *
  500. * We sleep until all works which were queued on entry have been handled,
  501. * but we are not livelocked by new incoming ones.
  502. */
  503. void flush_workqueue(struct workqueue_struct *wq)
  504. {
  505. const struct cpumask *cpu_map = wq_cpu_map(wq);
  506. int cpu;
  507. might_sleep();
  508. lock_map_acquire(&wq->lockdep_map);
  509. lock_map_release(&wq->lockdep_map);
  510. for_each_cpu(cpu, cpu_map)
  511. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  512. }
  513. EXPORT_SYMBOL_GPL(flush_workqueue);
  514. /**
  515. * flush_work - block until a work_struct's callback has terminated
  516. * @work: the work which is to be flushed
  517. *
  518. * Returns false if @work has already terminated.
  519. *
  520. * It is expected that, prior to calling flush_work(), the caller has
  521. * arranged for the work to not be requeued, otherwise it doesn't make
  522. * sense to use this function.
  523. */
  524. int flush_work(struct work_struct *work)
  525. {
  526. struct cpu_workqueue_struct *cwq;
  527. struct list_head *prev;
  528. struct wq_barrier barr;
  529. might_sleep();
  530. cwq = get_wq_data(work);
  531. if (!cwq)
  532. return 0;
  533. lock_map_acquire(&cwq->wq->lockdep_map);
  534. lock_map_release(&cwq->wq->lockdep_map);
  535. spin_lock_irq(&cwq->lock);
  536. if (!list_empty(&work->entry)) {
  537. /*
  538. * See the comment near try_to_grab_pending()->smp_rmb().
  539. * If it was re-queued under us we are not going to wait.
  540. */
  541. smp_rmb();
  542. if (unlikely(cwq != get_wq_data(work)))
  543. goto already_gone;
  544. prev = &work->entry;
  545. } else {
  546. if (cwq->current_work != work)
  547. goto already_gone;
  548. prev = &cwq->worklist;
  549. }
  550. insert_wq_barrier(cwq, &barr, prev->next);
  551. spin_unlock_irq(&cwq->lock);
  552. wait_for_completion(&barr.done);
  553. destroy_work_on_stack(&barr.work);
  554. return 1;
  555. already_gone:
  556. spin_unlock_irq(&cwq->lock);
  557. return 0;
  558. }
  559. EXPORT_SYMBOL_GPL(flush_work);
  560. /*
  561. * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
  562. * so this work can't be re-armed in any way.
  563. */
  564. static int try_to_grab_pending(struct work_struct *work)
  565. {
  566. struct cpu_workqueue_struct *cwq;
  567. int ret = -1;
  568. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  569. return 0;
  570. /*
  571. * The queueing is in progress, or it is already queued. Try to
  572. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  573. */
  574. cwq = get_wq_data(work);
  575. if (!cwq)
  576. return ret;
  577. spin_lock_irq(&cwq->lock);
  578. if (!list_empty(&work->entry)) {
  579. /*
  580. * This work is queued, but perhaps we locked the wrong cwq.
  581. * In that case we must see the new value after rmb(), see
  582. * insert_work()->wmb().
  583. */
  584. smp_rmb();
  585. if (cwq == get_wq_data(work)) {
  586. debug_work_deactivate(work);
  587. list_del_init(&work->entry);
  588. ret = 1;
  589. }
  590. }
  591. spin_unlock_irq(&cwq->lock);
  592. return ret;
  593. }
  594. static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
  595. struct work_struct *work)
  596. {
  597. struct wq_barrier barr;
  598. int running = 0;
  599. spin_lock_irq(&cwq->lock);
  600. if (unlikely(cwq->current_work == work)) {
  601. insert_wq_barrier(cwq, &barr, cwq->worklist.next);
  602. running = 1;
  603. }
  604. spin_unlock_irq(&cwq->lock);
  605. if (unlikely(running)) {
  606. wait_for_completion(&barr.done);
  607. destroy_work_on_stack(&barr.work);
  608. }
  609. }
  610. static void wait_on_work(struct work_struct *work)
  611. {
  612. struct cpu_workqueue_struct *cwq;
  613. struct workqueue_struct *wq;
  614. const struct cpumask *cpu_map;
  615. int cpu;
  616. might_sleep();
  617. lock_map_acquire(&work->lockdep_map);
  618. lock_map_release(&work->lockdep_map);
  619. cwq = get_wq_data(work);
  620. if (!cwq)
  621. return;
  622. wq = cwq->wq;
  623. cpu_map = wq_cpu_map(wq);
  624. for_each_cpu(cpu, cpu_map)
  625. wait_on_cpu_work(get_cwq(cpu, wq), work);
  626. }
  627. static int __cancel_work_timer(struct work_struct *work,
  628. struct timer_list* timer)
  629. {
  630. int ret;
  631. do {
  632. ret = (timer && likely(del_timer(timer)));
  633. if (!ret)
  634. ret = try_to_grab_pending(work);
  635. wait_on_work(work);
  636. } while (unlikely(ret < 0));
  637. clear_wq_data(work);
  638. return ret;
  639. }
  640. /**
  641. * cancel_work_sync - block until a work_struct's callback has terminated
  642. * @work: the work which is to be flushed
  643. *
  644. * Returns true if @work was pending.
  645. *
  646. * cancel_work_sync() will cancel the work if it is queued. If the work's
  647. * callback appears to be running, cancel_work_sync() will block until it
  648. * has completed.
  649. *
  650. * It is possible to use this function if the work re-queues itself. It can
  651. * cancel the work even if it migrates to another workqueue, however in that
  652. * case it only guarantees that work->func() has completed on the last queued
  653. * workqueue.
  654. *
  655. * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
  656. * pending, otherwise it goes into a busy-wait loop until the timer expires.
  657. *
  658. * The caller must ensure that workqueue_struct on which this work was last
  659. * queued can't be destroyed before this function returns.
  660. */
  661. int cancel_work_sync(struct work_struct *work)
  662. {
  663. return __cancel_work_timer(work, NULL);
  664. }
  665. EXPORT_SYMBOL_GPL(cancel_work_sync);
  666. /**
  667. * cancel_delayed_work_sync - reliably kill off a delayed work.
  668. * @dwork: the delayed work struct
  669. *
  670. * Returns true if @dwork was pending.
  671. *
  672. * It is possible to use this function if @dwork rearms itself via queue_work()
  673. * or queue_delayed_work(). See also the comment for cancel_work_sync().
  674. */
  675. int cancel_delayed_work_sync(struct delayed_work *dwork)
  676. {
  677. return __cancel_work_timer(&dwork->work, &dwork->timer);
  678. }
  679. EXPORT_SYMBOL(cancel_delayed_work_sync);
  680. static struct workqueue_struct *keventd_wq __read_mostly;
  681. /**
  682. * schedule_work - put work task in global workqueue
  683. * @work: job to be done
  684. *
  685. * Returns zero if @work was already on the kernel-global workqueue and
  686. * non-zero otherwise.
  687. *
  688. * This puts a job in the kernel-global workqueue if it was not already
  689. * queued and leaves it in the same position on the kernel-global
  690. * workqueue otherwise.
  691. */
  692. int schedule_work(struct work_struct *work)
  693. {
  694. return queue_work(keventd_wq, work);
  695. }
  696. EXPORT_SYMBOL(schedule_work);
  697. /*
  698. * schedule_work_on - put work task on a specific cpu
  699. * @cpu: cpu to put the work task on
  700. * @work: job to be done
  701. *
  702. * This puts a job on a specific cpu
  703. */
  704. int schedule_work_on(int cpu, struct work_struct *work)
  705. {
  706. return queue_work_on(cpu, keventd_wq, work);
  707. }
  708. EXPORT_SYMBOL(schedule_work_on);
  709. /**
  710. * schedule_delayed_work - put work task in global workqueue after delay
  711. * @dwork: job to be done
  712. * @delay: number of jiffies to wait or 0 for immediate execution
  713. *
  714. * After waiting for a given time this puts a job in the kernel-global
  715. * workqueue.
  716. */
  717. int schedule_delayed_work(struct delayed_work *dwork,
  718. unsigned long delay)
  719. {
  720. return queue_delayed_work(keventd_wq, dwork, delay);
  721. }
  722. EXPORT_SYMBOL(schedule_delayed_work);
  723. /**
  724. * flush_delayed_work - block until a dwork_struct's callback has terminated
  725. * @dwork: the delayed work which is to be flushed
  726. *
  727. * Any timeout is cancelled, and any pending work is run immediately.
  728. */
  729. void flush_delayed_work(struct delayed_work *dwork)
  730. {
  731. if (del_timer_sync(&dwork->timer)) {
  732. __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
  733. &dwork->work);
  734. put_cpu();
  735. }
  736. flush_work(&dwork->work);
  737. }
  738. EXPORT_SYMBOL(flush_delayed_work);
  739. /**
  740. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  741. * @cpu: cpu to use
  742. * @dwork: job to be done
  743. * @delay: number of jiffies to wait
  744. *
  745. * After waiting for a given time this puts a job in the kernel-global
  746. * workqueue on the specified CPU.
  747. */
  748. int schedule_delayed_work_on(int cpu,
  749. struct delayed_work *dwork, unsigned long delay)
  750. {
  751. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  752. }
  753. EXPORT_SYMBOL(schedule_delayed_work_on);
  754. /**
  755. * schedule_on_each_cpu - call a function on each online CPU from keventd
  756. * @func: the function to call
  757. *
  758. * Returns zero on success.
  759. * Returns -ve errno on failure.
  760. *
  761. * schedule_on_each_cpu() is very slow.
  762. */
  763. int schedule_on_each_cpu(work_func_t func)
  764. {
  765. int cpu;
  766. int orig = -1;
  767. struct work_struct *works;
  768. works = alloc_percpu(struct work_struct);
  769. if (!works)
  770. return -ENOMEM;
  771. get_online_cpus();
  772. /*
  773. * When running in keventd don't schedule a work item on
  774. * itself. Can just call directly because the work queue is
  775. * already bound. This also is faster.
  776. */
  777. if (current_is_keventd())
  778. orig = raw_smp_processor_id();
  779. for_each_online_cpu(cpu) {
  780. struct work_struct *work = per_cpu_ptr(works, cpu);
  781. INIT_WORK(work, func);
  782. if (cpu != orig)
  783. schedule_work_on(cpu, work);
  784. }
  785. if (orig >= 0)
  786. func(per_cpu_ptr(works, orig));
  787. for_each_online_cpu(cpu)
  788. flush_work(per_cpu_ptr(works, cpu));
  789. put_online_cpus();
  790. free_percpu(works);
  791. return 0;
  792. }
  793. /**
  794. * flush_scheduled_work - ensure that any scheduled work has run to completion.
  795. *
  796. * Forces execution of the kernel-global workqueue and blocks until its
  797. * completion.
  798. *
  799. * Think twice before calling this function! It's very easy to get into
  800. * trouble if you don't take great care. Either of the following situations
  801. * will lead to deadlock:
  802. *
  803. * One of the work items currently on the workqueue needs to acquire
  804. * a lock held by your code or its caller.
  805. *
  806. * Your code is running in the context of a work routine.
  807. *
  808. * They will be detected by lockdep when they occur, but the first might not
  809. * occur very often. It depends on what work items are on the workqueue and
  810. * what locks they need, which you have no control over.
  811. *
  812. * In most situations flushing the entire workqueue is overkill; you merely
  813. * need to know that a particular work item isn't queued and isn't running.
  814. * In such cases you should use cancel_delayed_work_sync() or
  815. * cancel_work_sync() instead.
  816. */
  817. void flush_scheduled_work(void)
  818. {
  819. flush_workqueue(keventd_wq);
  820. }
  821. EXPORT_SYMBOL(flush_scheduled_work);
  822. /**
  823. * execute_in_process_context - reliably execute the routine with user context
  824. * @fn: the function to execute
  825. * @ew: guaranteed storage for the execute work structure (must
  826. * be available when the work executes)
  827. *
  828. * Executes the function immediately if process context is available,
  829. * otherwise schedules the function for delayed execution.
  830. *
  831. * Returns: 0 - function was executed
  832. * 1 - function was scheduled for execution
  833. */
  834. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  835. {
  836. if (!in_interrupt()) {
  837. fn(&ew->work);
  838. return 0;
  839. }
  840. INIT_WORK(&ew->work, fn);
  841. schedule_work(&ew->work);
  842. return 1;
  843. }
  844. EXPORT_SYMBOL_GPL(execute_in_process_context);
  845. int keventd_up(void)
  846. {
  847. return keventd_wq != NULL;
  848. }
  849. int current_is_keventd(void)
  850. {
  851. struct cpu_workqueue_struct *cwq;
  852. int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  853. int ret = 0;
  854. BUG_ON(!keventd_wq);
  855. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  856. if (current == cwq->thread)
  857. ret = 1;
  858. return ret;
  859. }
  860. static struct cpu_workqueue_struct *
  861. init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
  862. {
  863. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  864. cwq->wq = wq;
  865. spin_lock_init(&cwq->lock);
  866. INIT_LIST_HEAD(&cwq->worklist);
  867. init_waitqueue_head(&cwq->more_work);
  868. return cwq;
  869. }
  870. static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  871. {
  872. struct workqueue_struct *wq = cwq->wq;
  873. const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
  874. struct task_struct *p;
  875. p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
  876. /*
  877. * Nobody can add the work_struct to this cwq,
  878. * if (caller is __create_workqueue)
  879. * nobody should see this wq
  880. * else // caller is CPU_UP_PREPARE
  881. * cpu is not on cpu_online_map
  882. * so we can abort safely.
  883. */
  884. if (IS_ERR(p))
  885. return PTR_ERR(p);
  886. cwq->thread = p;
  887. trace_workqueue_creation(cwq->thread, cpu);
  888. return 0;
  889. }
  890. static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  891. {
  892. struct task_struct *p = cwq->thread;
  893. if (p != NULL) {
  894. if (cpu >= 0)
  895. kthread_bind(p, cpu);
  896. wake_up_process(p);
  897. }
  898. }
  899. struct workqueue_struct *__create_workqueue_key(const char *name,
  900. unsigned int flags,
  901. struct lock_class_key *key,
  902. const char *lock_name)
  903. {
  904. struct workqueue_struct *wq;
  905. struct cpu_workqueue_struct *cwq;
  906. int err = 0, cpu;
  907. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  908. if (!wq)
  909. goto err;
  910. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  911. if (!wq->cpu_wq)
  912. goto err;
  913. wq->flags = flags;
  914. wq->name = name;
  915. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  916. INIT_LIST_HEAD(&wq->list);
  917. if (flags & WQ_SINGLE_THREAD) {
  918. cwq = init_cpu_workqueue(wq, singlethread_cpu);
  919. err = create_workqueue_thread(cwq, singlethread_cpu);
  920. start_workqueue_thread(cwq, -1);
  921. } else {
  922. cpu_maps_update_begin();
  923. /*
  924. * We must place this wq on list even if the code below fails.
  925. * cpu_down(cpu) can remove cpu from cpu_populated_map before
  926. * destroy_workqueue() takes the lock, in that case we leak
  927. * cwq[cpu]->thread.
  928. */
  929. spin_lock(&workqueue_lock);
  930. list_add(&wq->list, &workqueues);
  931. spin_unlock(&workqueue_lock);
  932. /*
  933. * We must initialize cwqs for each possible cpu even if we
  934. * are going to call destroy_workqueue() finally. Otherwise
  935. * cpu_up() can hit the uninitialized cwq once we drop the
  936. * lock.
  937. */
  938. for_each_possible_cpu(cpu) {
  939. cwq = init_cpu_workqueue(wq, cpu);
  940. if (err || !cpu_online(cpu))
  941. continue;
  942. err = create_workqueue_thread(cwq, cpu);
  943. start_workqueue_thread(cwq, cpu);
  944. }
  945. cpu_maps_update_done();
  946. }
  947. if (err) {
  948. destroy_workqueue(wq);
  949. wq = NULL;
  950. }
  951. return wq;
  952. err:
  953. if (wq) {
  954. free_percpu(wq->cpu_wq);
  955. kfree(wq);
  956. }
  957. return NULL;
  958. }
  959. EXPORT_SYMBOL_GPL(__create_workqueue_key);
  960. static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
  961. {
  962. /*
  963. * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
  964. * cpu_add_remove_lock protects cwq->thread.
  965. */
  966. if (cwq->thread == NULL)
  967. return;
  968. lock_map_acquire(&cwq->wq->lockdep_map);
  969. lock_map_release(&cwq->wq->lockdep_map);
  970. flush_cpu_workqueue(cwq);
  971. /*
  972. * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
  973. * a concurrent flush_workqueue() can insert a barrier after us.
  974. * However, in that case run_workqueue() won't return and check
  975. * kthread_should_stop() until it flushes all work_struct's.
  976. * When ->worklist becomes empty it is safe to exit because no
  977. * more work_structs can be queued on this cwq: flush_workqueue
  978. * checks list_empty(), and a "normal" queue_work() can't use
  979. * a dead CPU.
  980. */
  981. trace_workqueue_destruction(cwq->thread);
  982. kthread_stop(cwq->thread);
  983. cwq->thread = NULL;
  984. }
  985. /**
  986. * destroy_workqueue - safely terminate a workqueue
  987. * @wq: target workqueue
  988. *
  989. * Safely destroy a workqueue. All work currently pending will be done first.
  990. */
  991. void destroy_workqueue(struct workqueue_struct *wq)
  992. {
  993. const struct cpumask *cpu_map = wq_cpu_map(wq);
  994. int cpu;
  995. cpu_maps_update_begin();
  996. spin_lock(&workqueue_lock);
  997. list_del(&wq->list);
  998. spin_unlock(&workqueue_lock);
  999. for_each_cpu(cpu, cpu_map)
  1000. cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
  1001. cpu_maps_update_done();
  1002. free_percpu(wq->cpu_wq);
  1003. kfree(wq);
  1004. }
  1005. EXPORT_SYMBOL_GPL(destroy_workqueue);
  1006. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  1007. unsigned long action,
  1008. void *hcpu)
  1009. {
  1010. unsigned int cpu = (unsigned long)hcpu;
  1011. struct cpu_workqueue_struct *cwq;
  1012. struct workqueue_struct *wq;
  1013. int err = 0;
  1014. action &= ~CPU_TASKS_FROZEN;
  1015. switch (action) {
  1016. case CPU_UP_PREPARE:
  1017. cpumask_set_cpu(cpu, cpu_populated_map);
  1018. }
  1019. undo:
  1020. list_for_each_entry(wq, &workqueues, list) {
  1021. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  1022. switch (action) {
  1023. case CPU_UP_PREPARE:
  1024. err = create_workqueue_thread(cwq, cpu);
  1025. if (!err)
  1026. break;
  1027. printk(KERN_ERR "workqueue [%s] for %i failed\n",
  1028. wq->name, cpu);
  1029. action = CPU_UP_CANCELED;
  1030. err = -ENOMEM;
  1031. goto undo;
  1032. case CPU_ONLINE:
  1033. start_workqueue_thread(cwq, cpu);
  1034. break;
  1035. case CPU_UP_CANCELED:
  1036. start_workqueue_thread(cwq, -1);
  1037. case CPU_POST_DEAD:
  1038. cleanup_workqueue_thread(cwq);
  1039. break;
  1040. }
  1041. }
  1042. switch (action) {
  1043. case CPU_UP_CANCELED:
  1044. case CPU_POST_DEAD:
  1045. cpumask_clear_cpu(cpu, cpu_populated_map);
  1046. }
  1047. return notifier_from_errno(err);
  1048. }
  1049. #ifdef CONFIG_SMP
  1050. struct work_for_cpu {
  1051. struct completion completion;
  1052. long (*fn)(void *);
  1053. void *arg;
  1054. long ret;
  1055. };
  1056. static int do_work_for_cpu(void *_wfc)
  1057. {
  1058. struct work_for_cpu *wfc = _wfc;
  1059. wfc->ret = wfc->fn(wfc->arg);
  1060. complete(&wfc->completion);
  1061. return 0;
  1062. }
  1063. /**
  1064. * work_on_cpu - run a function in user context on a particular cpu
  1065. * @cpu: the cpu to run on
  1066. * @fn: the function to run
  1067. * @arg: the function arg
  1068. *
  1069. * This will return the value @fn returns.
  1070. * It is up to the caller to ensure that the cpu doesn't go offline.
  1071. * The caller must not hold any locks which would prevent @fn from completing.
  1072. */
  1073. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  1074. {
  1075. struct task_struct *sub_thread;
  1076. struct work_for_cpu wfc = {
  1077. .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
  1078. .fn = fn,
  1079. .arg = arg,
  1080. };
  1081. sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
  1082. if (IS_ERR(sub_thread))
  1083. return PTR_ERR(sub_thread);
  1084. kthread_bind(sub_thread, cpu);
  1085. wake_up_process(sub_thread);
  1086. wait_for_completion(&wfc.completion);
  1087. return wfc.ret;
  1088. }
  1089. EXPORT_SYMBOL_GPL(work_on_cpu);
  1090. #endif /* CONFIG_SMP */
  1091. void __init init_workqueues(void)
  1092. {
  1093. alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
  1094. cpumask_copy(cpu_populated_map, cpu_online_mask);
  1095. singlethread_cpu = cpumask_first(cpu_possible_mask);
  1096. cpu_singlethread_map = cpumask_of(singlethread_cpu);
  1097. hotcpu_notifier(workqueue_cpu_callback, 0);
  1098. keventd_wq = create_workqueue("events");
  1099. BUG_ON(!keventd_wq);
  1100. }