workqueue.c 25 KB

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