workqueue.c 26 KB

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