workqueue.c 24 KB

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