workqueue.c 25 KB

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