workqueue.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 <andrewm@uow.edu.au>
  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. /*
  36. * The per-CPU workqueue (if single thread, we always use the first
  37. * possible cpu).
  38. */
  39. struct cpu_workqueue_struct {
  40. spinlock_t lock;
  41. struct list_head worklist;
  42. wait_queue_head_t more_work;
  43. struct work_struct *current_work;
  44. struct workqueue_struct *wq;
  45. struct task_struct *thread;
  46. int run_depth; /* Detect run_workqueue() recursion depth */
  47. } ____cacheline_aligned;
  48. /*
  49. * The externally visible workqueue abstraction is an array of
  50. * per-CPU workqueues:
  51. */
  52. struct workqueue_struct {
  53. struct cpu_workqueue_struct *cpu_wq;
  54. struct list_head list;
  55. const char *name;
  56. int singlethread;
  57. int freezeable; /* Freeze threads during suspend */
  58. #ifdef CONFIG_LOCKDEP
  59. struct lockdep_map lockdep_map;
  60. #endif
  61. };
  62. /* Serializes the accesses to the list of workqueues. */
  63. static DEFINE_SPINLOCK(workqueue_lock);
  64. static LIST_HEAD(workqueues);
  65. static int singlethread_cpu __read_mostly;
  66. static cpumask_t cpu_singlethread_map __read_mostly;
  67. /*
  68. * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
  69. * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
  70. * which comes in between can't use for_each_online_cpu(). We could
  71. * use cpu_possible_map, the cpumask below is more a documentation
  72. * than optimization.
  73. */
  74. static cpumask_t cpu_populated_map __read_mostly;
  75. /* If it's single threaded, it isn't in the list of workqueues. */
  76. static inline int is_single_threaded(struct workqueue_struct *wq)
  77. {
  78. return wq->singlethread;
  79. }
  80. static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq)
  81. {
  82. return is_single_threaded(wq)
  83. ? &cpu_singlethread_map : &cpu_populated_map;
  84. }
  85. static
  86. struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
  87. {
  88. if (unlikely(is_single_threaded(wq)))
  89. cpu = singlethread_cpu;
  90. return per_cpu_ptr(wq->cpu_wq, cpu);
  91. }
  92. /*
  93. * Set the workqueue on which a work item is to be run
  94. * - Must *only* be called if the pending flag is set
  95. */
  96. static inline void set_wq_data(struct work_struct *work,
  97. struct cpu_workqueue_struct *cwq)
  98. {
  99. unsigned long new;
  100. BUG_ON(!work_pending(work));
  101. new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
  102. new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
  103. atomic_long_set(&work->data, new);
  104. }
  105. static inline
  106. struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
  107. {
  108. return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
  109. }
  110. static void insert_work(struct cpu_workqueue_struct *cwq,
  111. struct work_struct *work, struct list_head *head)
  112. {
  113. set_wq_data(work, cwq);
  114. /*
  115. * Ensure that we get the right work->data if we see the
  116. * result of list_add() below, see try_to_grab_pending().
  117. */
  118. smp_wmb();
  119. list_add_tail(&work->entry, head);
  120. wake_up(&cwq->more_work);
  121. }
  122. static void __queue_work(struct cpu_workqueue_struct *cwq,
  123. struct work_struct *work)
  124. {
  125. unsigned long flags;
  126. spin_lock_irqsave(&cwq->lock, flags);
  127. insert_work(cwq, work, &cwq->worklist);
  128. spin_unlock_irqrestore(&cwq->lock, flags);
  129. }
  130. /**
  131. * queue_work - queue work on a workqueue
  132. * @wq: workqueue to use
  133. * @work: work to queue
  134. *
  135. * Returns 0 if @work was already on a queue, non-zero otherwise.
  136. *
  137. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  138. * it can be processed by another CPU.
  139. */
  140. int queue_work(struct workqueue_struct *wq, struct work_struct *work)
  141. {
  142. int ret = 0;
  143. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  144. BUG_ON(!list_empty(&work->entry));
  145. __queue_work(wq_per_cpu(wq, get_cpu()), work);
  146. put_cpu();
  147. ret = 1;
  148. }
  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. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  232. {
  233. spin_lock_irq(&cwq->lock);
  234. cwq->run_depth++;
  235. if (cwq->run_depth > 3) {
  236. /* morton gets to eat his hat */
  237. printk("%s: recursion depth exceeded: %d\n",
  238. __func__, cwq->run_depth);
  239. dump_stack();
  240. }
  241. while (!list_empty(&cwq->worklist)) {
  242. struct work_struct *work = list_entry(cwq->worklist.next,
  243. struct work_struct, entry);
  244. work_func_t f = work->func;
  245. #ifdef CONFIG_LOCKDEP
  246. /*
  247. * It is permissible to free the struct work_struct
  248. * from inside the function that is called from it,
  249. * this we need to take into account for lockdep too.
  250. * To avoid bogus "held lock freed" warnings as well
  251. * as problems when looking into work->lockdep_map,
  252. * make a copy and use that here.
  253. */
  254. struct lockdep_map lockdep_map = work->lockdep_map;
  255. #endif
  256. cwq->current_work = work;
  257. list_del_init(cwq->worklist.next);
  258. spin_unlock_irq(&cwq->lock);
  259. BUG_ON(get_wq_data(work) != cwq);
  260. work_clear_pending(work);
  261. lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  262. lock_acquire(&lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  263. f(work);
  264. lock_release(&lockdep_map, 1, _THIS_IP_);
  265. lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
  266. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  267. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  268. "%s/0x%08x/%d\n",
  269. current->comm, preempt_count(),
  270. task_pid_nr(current));
  271. printk(KERN_ERR " last function: ");
  272. print_symbol("%s\n", (unsigned long)f);
  273. debug_show_held_locks(current);
  274. dump_stack();
  275. }
  276. spin_lock_irq(&cwq->lock);
  277. cwq->current_work = NULL;
  278. }
  279. cwq->run_depth--;
  280. spin_unlock_irq(&cwq->lock);
  281. }
  282. static int worker_thread(void *__cwq)
  283. {
  284. struct cpu_workqueue_struct *cwq = __cwq;
  285. DEFINE_WAIT(wait);
  286. if (cwq->wq->freezeable)
  287. set_freezable();
  288. set_user_nice(current, -5);
  289. for (;;) {
  290. prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
  291. if (!freezing(current) &&
  292. !kthread_should_stop() &&
  293. list_empty(&cwq->worklist))
  294. schedule();
  295. finish_wait(&cwq->more_work, &wait);
  296. try_to_freeze();
  297. if (kthread_should_stop())
  298. break;
  299. run_workqueue(cwq);
  300. }
  301. return 0;
  302. }
  303. struct wq_barrier {
  304. struct work_struct work;
  305. struct completion done;
  306. };
  307. static void wq_barrier_func(struct work_struct *work)
  308. {
  309. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  310. complete(&barr->done);
  311. }
  312. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  313. struct wq_barrier *barr, struct list_head *head)
  314. {
  315. INIT_WORK(&barr->work, wq_barrier_func);
  316. __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
  317. init_completion(&barr->done);
  318. insert_work(cwq, &barr->work, head);
  319. }
  320. static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  321. {
  322. int active;
  323. if (cwq->thread == current) {
  324. /*
  325. * Probably keventd trying to flush its own queue. So simply run
  326. * it by hand rather than deadlocking.
  327. */
  328. run_workqueue(cwq);
  329. active = 1;
  330. } else {
  331. struct wq_barrier barr;
  332. active = 0;
  333. spin_lock_irq(&cwq->lock);
  334. if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
  335. insert_wq_barrier(cwq, &barr, &cwq->worklist);
  336. active = 1;
  337. }
  338. spin_unlock_irq(&cwq->lock);
  339. if (active)
  340. wait_for_completion(&barr.done);
  341. }
  342. return active;
  343. }
  344. /**
  345. * flush_workqueue - ensure that any scheduled work has run to completion.
  346. * @wq: workqueue to flush
  347. *
  348. * Forces execution of the workqueue and blocks until its completion.
  349. * This is typically used in driver shutdown handlers.
  350. *
  351. * We sleep until all works which were queued on entry have been handled,
  352. * but we are not livelocked by new incoming ones.
  353. *
  354. * This function used to run the workqueues itself. Now we just wait for the
  355. * helper threads to do it.
  356. */
  357. void flush_workqueue(struct workqueue_struct *wq)
  358. {
  359. const cpumask_t *cpu_map = wq_cpu_map(wq);
  360. int cpu;
  361. might_sleep();
  362. lock_acquire(&wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  363. lock_release(&wq->lockdep_map, 1, _THIS_IP_);
  364. for_each_cpu_mask_nr(cpu, *cpu_map)
  365. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  366. }
  367. EXPORT_SYMBOL_GPL(flush_workqueue);
  368. /**
  369. * flush_work - block until a work_struct's callback has terminated
  370. * @work: the work which is to be flushed
  371. *
  372. * Returns false if @work has already terminated.
  373. *
  374. * It is expected that, prior to calling flush_work(), the caller has
  375. * arranged for the work to not be requeued, otherwise it doesn't make
  376. * sense to use this function.
  377. */
  378. int flush_work(struct work_struct *work)
  379. {
  380. struct cpu_workqueue_struct *cwq;
  381. struct list_head *prev;
  382. struct wq_barrier barr;
  383. might_sleep();
  384. cwq = get_wq_data(work);
  385. if (!cwq)
  386. return 0;
  387. lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  388. lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
  389. prev = NULL;
  390. spin_lock_irq(&cwq->lock);
  391. if (!list_empty(&work->entry)) {
  392. /*
  393. * See the comment near try_to_grab_pending()->smp_rmb().
  394. * If it was re-queued under us we are not going to wait.
  395. */
  396. smp_rmb();
  397. if (unlikely(cwq != get_wq_data(work)))
  398. goto out;
  399. prev = &work->entry;
  400. } else {
  401. if (cwq->current_work != work)
  402. goto out;
  403. prev = &cwq->worklist;
  404. }
  405. insert_wq_barrier(cwq, &barr, prev->next);
  406. out:
  407. spin_unlock_irq(&cwq->lock);
  408. if (!prev)
  409. return 0;
  410. wait_for_completion(&barr.done);
  411. return 1;
  412. }
  413. EXPORT_SYMBOL_GPL(flush_work);
  414. /*
  415. * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
  416. * so this work can't be re-armed in any way.
  417. */
  418. static int try_to_grab_pending(struct work_struct *work)
  419. {
  420. struct cpu_workqueue_struct *cwq;
  421. int ret = -1;
  422. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
  423. return 0;
  424. /*
  425. * The queueing is in progress, or it is already queued. Try to
  426. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  427. */
  428. cwq = get_wq_data(work);
  429. if (!cwq)
  430. return ret;
  431. spin_lock_irq(&cwq->lock);
  432. if (!list_empty(&work->entry)) {
  433. /*
  434. * This work is queued, but perhaps we locked the wrong cwq.
  435. * In that case we must see the new value after rmb(), see
  436. * insert_work()->wmb().
  437. */
  438. smp_rmb();
  439. if (cwq == get_wq_data(work)) {
  440. list_del_init(&work->entry);
  441. ret = 1;
  442. }
  443. }
  444. spin_unlock_irq(&cwq->lock);
  445. return ret;
  446. }
  447. static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
  448. struct work_struct *work)
  449. {
  450. struct wq_barrier barr;
  451. int running = 0;
  452. spin_lock_irq(&cwq->lock);
  453. if (unlikely(cwq->current_work == work)) {
  454. insert_wq_barrier(cwq, &barr, cwq->worklist.next);
  455. running = 1;
  456. }
  457. spin_unlock_irq(&cwq->lock);
  458. if (unlikely(running))
  459. wait_for_completion(&barr.done);
  460. }
  461. static void wait_on_work(struct work_struct *work)
  462. {
  463. struct cpu_workqueue_struct *cwq;
  464. struct workqueue_struct *wq;
  465. const cpumask_t *cpu_map;
  466. int cpu;
  467. might_sleep();
  468. lock_acquire(&work->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  469. lock_release(&work->lockdep_map, 1, _THIS_IP_);
  470. cwq = get_wq_data(work);
  471. if (!cwq)
  472. return;
  473. wq = cwq->wq;
  474. cpu_map = wq_cpu_map(wq);
  475. for_each_cpu_mask_nr(cpu, *cpu_map)
  476. wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  477. }
  478. static int __cancel_work_timer(struct work_struct *work,
  479. struct timer_list* timer)
  480. {
  481. int ret;
  482. do {
  483. ret = (timer && likely(del_timer(timer)));
  484. if (!ret)
  485. ret = try_to_grab_pending(work);
  486. wait_on_work(work);
  487. } while (unlikely(ret < 0));
  488. work_clear_pending(work);
  489. return ret;
  490. }
  491. /**
  492. * cancel_work_sync - block until a work_struct's callback has terminated
  493. * @work: the work which is to be flushed
  494. *
  495. * Returns true if @work was pending.
  496. *
  497. * cancel_work_sync() will cancel the work if it is queued. If the work's
  498. * callback appears to be running, cancel_work_sync() will block until it
  499. * has completed.
  500. *
  501. * It is possible to use this function if the work re-queues itself. It can
  502. * cancel the work even if it migrates to another workqueue, however in that
  503. * case it only guarantees that work->func() has completed on the last queued
  504. * workqueue.
  505. *
  506. * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
  507. * pending, otherwise it goes into a busy-wait loop until the timer expires.
  508. *
  509. * The caller must ensure that workqueue_struct on which this work was last
  510. * queued can't be destroyed before this function returns.
  511. */
  512. int cancel_work_sync(struct work_struct *work)
  513. {
  514. return __cancel_work_timer(work, NULL);
  515. }
  516. EXPORT_SYMBOL_GPL(cancel_work_sync);
  517. /**
  518. * cancel_delayed_work_sync - reliably kill off a delayed work.
  519. * @dwork: the delayed work struct
  520. *
  521. * Returns true if @dwork was pending.
  522. *
  523. * It is possible to use this function if @dwork rearms itself via queue_work()
  524. * or queue_delayed_work(). See also the comment for cancel_work_sync().
  525. */
  526. int cancel_delayed_work_sync(struct delayed_work *dwork)
  527. {
  528. return __cancel_work_timer(&dwork->work, &dwork->timer);
  529. }
  530. EXPORT_SYMBOL(cancel_delayed_work_sync);
  531. static struct workqueue_struct *keventd_wq __read_mostly;
  532. /**
  533. * schedule_work - put work task in global workqueue
  534. * @work: job to be done
  535. *
  536. * This puts a job in the kernel-global workqueue.
  537. */
  538. int schedule_work(struct work_struct *work)
  539. {
  540. return queue_work(keventd_wq, work);
  541. }
  542. EXPORT_SYMBOL(schedule_work);
  543. /*
  544. * schedule_work_on - put work task on a specific cpu
  545. * @cpu: cpu to put the work task on
  546. * @work: job to be done
  547. *
  548. * This puts a job on a specific cpu
  549. */
  550. int schedule_work_on(int cpu, struct work_struct *work)
  551. {
  552. return queue_work_on(cpu, keventd_wq, work);
  553. }
  554. EXPORT_SYMBOL(schedule_work_on);
  555. /**
  556. * schedule_delayed_work - put work task in global workqueue after delay
  557. * @dwork: job to be done
  558. * @delay: number of jiffies to wait or 0 for immediate execution
  559. *
  560. * After waiting for a given time this puts a job in the kernel-global
  561. * workqueue.
  562. */
  563. int schedule_delayed_work(struct delayed_work *dwork,
  564. unsigned long delay)
  565. {
  566. return queue_delayed_work(keventd_wq, dwork, delay);
  567. }
  568. EXPORT_SYMBOL(schedule_delayed_work);
  569. /**
  570. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  571. * @cpu: cpu to use
  572. * @dwork: job to be done
  573. * @delay: number of jiffies to wait
  574. *
  575. * After waiting for a given time this puts a job in the kernel-global
  576. * workqueue on the specified CPU.
  577. */
  578. int schedule_delayed_work_on(int cpu,
  579. struct delayed_work *dwork, unsigned long delay)
  580. {
  581. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  582. }
  583. EXPORT_SYMBOL(schedule_delayed_work_on);
  584. /**
  585. * schedule_on_each_cpu - call a function on each online CPU from keventd
  586. * @func: the function to call
  587. *
  588. * Returns zero on success.
  589. * Returns -ve errno on failure.
  590. *
  591. * schedule_on_each_cpu() is very slow.
  592. */
  593. int schedule_on_each_cpu(work_func_t func)
  594. {
  595. int cpu;
  596. struct work_struct *works;
  597. works = alloc_percpu(struct work_struct);
  598. if (!works)
  599. return -ENOMEM;
  600. get_online_cpus();
  601. for_each_online_cpu(cpu) {
  602. struct work_struct *work = per_cpu_ptr(works, cpu);
  603. INIT_WORK(work, func);
  604. set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
  605. __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
  606. }
  607. for_each_online_cpu(cpu)
  608. flush_work(per_cpu_ptr(works, cpu));
  609. put_online_cpus();
  610. free_percpu(works);
  611. return 0;
  612. }
  613. void flush_scheduled_work(void)
  614. {
  615. flush_workqueue(keventd_wq);
  616. }
  617. EXPORT_SYMBOL(flush_scheduled_work);
  618. /**
  619. * execute_in_process_context - reliably execute the routine with user context
  620. * @fn: the function to execute
  621. * @ew: guaranteed storage for the execute work structure (must
  622. * be available when the work executes)
  623. *
  624. * Executes the function immediately if process context is available,
  625. * otherwise schedules the function for delayed execution.
  626. *
  627. * Returns: 0 - function was executed
  628. * 1 - function was scheduled for execution
  629. */
  630. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  631. {
  632. if (!in_interrupt()) {
  633. fn(&ew->work);
  634. return 0;
  635. }
  636. INIT_WORK(&ew->work, fn);
  637. schedule_work(&ew->work);
  638. return 1;
  639. }
  640. EXPORT_SYMBOL_GPL(execute_in_process_context);
  641. int keventd_up(void)
  642. {
  643. return keventd_wq != NULL;
  644. }
  645. int current_is_keventd(void)
  646. {
  647. struct cpu_workqueue_struct *cwq;
  648. int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  649. int ret = 0;
  650. BUG_ON(!keventd_wq);
  651. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  652. if (current == cwq->thread)
  653. ret = 1;
  654. return ret;
  655. }
  656. static struct cpu_workqueue_struct *
  657. init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
  658. {
  659. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  660. cwq->wq = wq;
  661. spin_lock_init(&cwq->lock);
  662. INIT_LIST_HEAD(&cwq->worklist);
  663. init_waitqueue_head(&cwq->more_work);
  664. return cwq;
  665. }
  666. static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  667. {
  668. struct workqueue_struct *wq = cwq->wq;
  669. const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
  670. struct task_struct *p;
  671. p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
  672. /*
  673. * Nobody can add the work_struct to this cwq,
  674. * if (caller is __create_workqueue)
  675. * nobody should see this wq
  676. * else // caller is CPU_UP_PREPARE
  677. * cpu is not on cpu_online_map
  678. * so we can abort safely.
  679. */
  680. if (IS_ERR(p))
  681. return PTR_ERR(p);
  682. cwq->thread = p;
  683. return 0;
  684. }
  685. static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  686. {
  687. struct task_struct *p = cwq->thread;
  688. if (p != NULL) {
  689. if (cpu >= 0)
  690. kthread_bind(p, cpu);
  691. wake_up_process(p);
  692. }
  693. }
  694. struct workqueue_struct *__create_workqueue_key(const char *name,
  695. int singlethread,
  696. int freezeable,
  697. struct lock_class_key *key,
  698. const char *lock_name)
  699. {
  700. struct workqueue_struct *wq;
  701. struct cpu_workqueue_struct *cwq;
  702. int err = 0, cpu;
  703. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  704. if (!wq)
  705. return NULL;
  706. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  707. if (!wq->cpu_wq) {
  708. kfree(wq);
  709. return NULL;
  710. }
  711. wq->name = name;
  712. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  713. wq->singlethread = singlethread;
  714. wq->freezeable = freezeable;
  715. INIT_LIST_HEAD(&wq->list);
  716. if (singlethread) {
  717. cwq = init_cpu_workqueue(wq, singlethread_cpu);
  718. err = create_workqueue_thread(cwq, singlethread_cpu);
  719. start_workqueue_thread(cwq, -1);
  720. } else {
  721. cpu_maps_update_begin();
  722. spin_lock(&workqueue_lock);
  723. list_add(&wq->list, &workqueues);
  724. spin_unlock(&workqueue_lock);
  725. for_each_possible_cpu(cpu) {
  726. cwq = init_cpu_workqueue(wq, cpu);
  727. if (err || !cpu_online(cpu))
  728. continue;
  729. err = create_workqueue_thread(cwq, cpu);
  730. start_workqueue_thread(cwq, cpu);
  731. }
  732. cpu_maps_update_done();
  733. }
  734. if (err) {
  735. destroy_workqueue(wq);
  736. wq = NULL;
  737. }
  738. return wq;
  739. }
  740. EXPORT_SYMBOL_GPL(__create_workqueue_key);
  741. static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
  742. {
  743. /*
  744. * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
  745. * cpu_add_remove_lock protects cwq->thread.
  746. */
  747. if (cwq->thread == NULL)
  748. return;
  749. lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
  750. lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
  751. flush_cpu_workqueue(cwq);
  752. /*
  753. * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
  754. * a concurrent flush_workqueue() can insert a barrier after us.
  755. * However, in that case run_workqueue() won't return and check
  756. * kthread_should_stop() until it flushes all work_struct's.
  757. * When ->worklist becomes empty it is safe to exit because no
  758. * more work_structs can be queued on this cwq: flush_workqueue
  759. * checks list_empty(), and a "normal" queue_work() can't use
  760. * a dead CPU.
  761. */
  762. kthread_stop(cwq->thread);
  763. cwq->thread = NULL;
  764. }
  765. /**
  766. * destroy_workqueue - safely terminate a workqueue
  767. * @wq: target workqueue
  768. *
  769. * Safely destroy a workqueue. All work currently pending will be done first.
  770. */
  771. void destroy_workqueue(struct workqueue_struct *wq)
  772. {
  773. const cpumask_t *cpu_map = wq_cpu_map(wq);
  774. int cpu;
  775. cpu_maps_update_begin();
  776. spin_lock(&workqueue_lock);
  777. list_del(&wq->list);
  778. spin_unlock(&workqueue_lock);
  779. for_each_cpu_mask_nr(cpu, *cpu_map)
  780. cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
  781. cpu_maps_update_done();
  782. free_percpu(wq->cpu_wq);
  783. kfree(wq);
  784. }
  785. EXPORT_SYMBOL_GPL(destroy_workqueue);
  786. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  787. unsigned long action,
  788. void *hcpu)
  789. {
  790. unsigned int cpu = (unsigned long)hcpu;
  791. struct cpu_workqueue_struct *cwq;
  792. struct workqueue_struct *wq;
  793. action &= ~CPU_TASKS_FROZEN;
  794. switch (action) {
  795. case CPU_UP_PREPARE:
  796. cpu_set(cpu, cpu_populated_map);
  797. }
  798. list_for_each_entry(wq, &workqueues, list) {
  799. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  800. switch (action) {
  801. case CPU_UP_PREPARE:
  802. if (!create_workqueue_thread(cwq, cpu))
  803. break;
  804. printk(KERN_ERR "workqueue [%s] for %i failed\n",
  805. wq->name, cpu);
  806. return NOTIFY_BAD;
  807. case CPU_ONLINE:
  808. start_workqueue_thread(cwq, cpu);
  809. break;
  810. case CPU_UP_CANCELED:
  811. start_workqueue_thread(cwq, -1);
  812. case CPU_POST_DEAD:
  813. cleanup_workqueue_thread(cwq);
  814. break;
  815. }
  816. }
  817. switch (action) {
  818. case CPU_UP_CANCELED:
  819. case CPU_POST_DEAD:
  820. cpu_clear(cpu, cpu_populated_map);
  821. }
  822. return NOTIFY_OK;
  823. }
  824. void __init init_workqueues(void)
  825. {
  826. cpu_populated_map = cpu_online_map;
  827. singlethread_cpu = first_cpu(cpu_possible_map);
  828. cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
  829. hotcpu_notifier(workqueue_cpu_callback, 0);
  830. keventd_wq = create_workqueue("events");
  831. BUG_ON(!keventd_wq);
  832. }