workqueue.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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 <clameter@sgi.com>.
  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. /*
  35. * The per-CPU workqueue (if single thread, we always use the first
  36. * possible cpu).
  37. */
  38. struct cpu_workqueue_struct {
  39. spinlock_t lock;
  40. struct list_head worklist;
  41. wait_queue_head_t more_work;
  42. struct workqueue_struct *wq;
  43. struct task_struct *thread;
  44. struct work_struct *current_work;
  45. int run_depth; /* Detect run_workqueue() recursion depth */
  46. } ____cacheline_aligned;
  47. /*
  48. * The externally visible workqueue abstraction is an array of
  49. * per-CPU workqueues:
  50. */
  51. struct workqueue_struct {
  52. struct cpu_workqueue_struct *cpu_wq;
  53. const char *name;
  54. struct list_head list; /* Empty if single thread */
  55. int freezeable; /* Freeze threads during suspend */
  56. };
  57. /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
  58. threads to each one as cpus come/go. */
  59. static DEFINE_MUTEX(workqueue_mutex);
  60. static LIST_HEAD(workqueues);
  61. static int singlethread_cpu;
  62. /* If it's single threaded, it isn't in the list of workqueues. */
  63. static inline int is_single_threaded(struct workqueue_struct *wq)
  64. {
  65. return list_empty(&wq->list);
  66. }
  67. /*
  68. * Set the workqueue on which a work item is to be run
  69. * - Must *only* be called if the pending flag is set
  70. */
  71. static inline void set_wq_data(struct work_struct *work, void *wq)
  72. {
  73. unsigned long new;
  74. BUG_ON(!work_pending(work));
  75. new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
  76. new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
  77. atomic_long_set(&work->data, new);
  78. }
  79. static inline void *get_wq_data(struct work_struct *work)
  80. {
  81. return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
  82. }
  83. static int __run_work(struct cpu_workqueue_struct *cwq, struct work_struct *work)
  84. {
  85. int ret = 0;
  86. unsigned long flags;
  87. spin_lock_irqsave(&cwq->lock, flags);
  88. /*
  89. * We need to re-validate the work info after we've gotten
  90. * the cpu_workqueue lock. We can run the work now iff:
  91. *
  92. * - the wq_data still matches the cpu_workqueue_struct
  93. * - AND the work is still marked pending
  94. * - AND the work is still on a list (which will be this
  95. * workqueue_struct list)
  96. *
  97. * All these conditions are important, because we
  98. * need to protect against the work being run right
  99. * now on another CPU (all but the last one might be
  100. * true if it's currently running and has not been
  101. * released yet, for example).
  102. */
  103. if (get_wq_data(work) == cwq
  104. && work_pending(work)
  105. && !list_empty(&work->entry)) {
  106. work_func_t f = work->func;
  107. cwq->current_work = work;
  108. list_del_init(&work->entry);
  109. spin_unlock_irqrestore(&cwq->lock, flags);
  110. if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
  111. work_release(work);
  112. f(work);
  113. spin_lock_irqsave(&cwq->lock, flags);
  114. cwq->current_work = NULL;
  115. ret = 1;
  116. }
  117. spin_unlock_irqrestore(&cwq->lock, flags);
  118. return ret;
  119. }
  120. /**
  121. * run_scheduled_work - run scheduled work synchronously
  122. * @work: work to run
  123. *
  124. * This checks if the work was pending, and runs it
  125. * synchronously if so. It returns a boolean to indicate
  126. * whether it had any scheduled work to run or not.
  127. *
  128. * NOTE! This _only_ works for normal work_structs. You
  129. * CANNOT use this for delayed work, because the wq data
  130. * for delayed work will not point properly to the per-
  131. * CPU workqueue struct, but will change!
  132. */
  133. int fastcall run_scheduled_work(struct work_struct *work)
  134. {
  135. for (;;) {
  136. struct cpu_workqueue_struct *cwq;
  137. if (!work_pending(work))
  138. return 0;
  139. if (list_empty(&work->entry))
  140. return 0;
  141. /* NOTE! This depends intimately on __queue_work! */
  142. cwq = get_wq_data(work);
  143. if (!cwq)
  144. return 0;
  145. if (__run_work(cwq, work))
  146. return 1;
  147. }
  148. }
  149. EXPORT_SYMBOL(run_scheduled_work);
  150. static void insert_work(struct cpu_workqueue_struct *cwq,
  151. struct work_struct *work, int tail)
  152. {
  153. set_wq_data(work, cwq);
  154. if (tail)
  155. list_add_tail(&work->entry, &cwq->worklist);
  156. else
  157. list_add(&work->entry, &cwq->worklist);
  158. wake_up(&cwq->more_work);
  159. }
  160. /* Preempt must be disabled. */
  161. static void __queue_work(struct cpu_workqueue_struct *cwq,
  162. struct work_struct *work)
  163. {
  164. unsigned long flags;
  165. spin_lock_irqsave(&cwq->lock, flags);
  166. insert_work(cwq, work, 1);
  167. spin_unlock_irqrestore(&cwq->lock, flags);
  168. }
  169. /**
  170. * queue_work - queue work on a workqueue
  171. * @wq: workqueue to use
  172. * @work: work to queue
  173. *
  174. * Returns 0 if @work was already on a queue, non-zero otherwise.
  175. *
  176. * We queue the work to the CPU it was submitted, but there is no
  177. * guarantee that it will be processed by that CPU.
  178. */
  179. int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
  180. {
  181. int ret = 0, cpu = get_cpu();
  182. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  183. if (unlikely(is_single_threaded(wq)))
  184. cpu = singlethread_cpu;
  185. BUG_ON(!list_empty(&work->entry));
  186. __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  187. ret = 1;
  188. }
  189. put_cpu();
  190. return ret;
  191. }
  192. EXPORT_SYMBOL_GPL(queue_work);
  193. void delayed_work_timer_fn(unsigned long __data)
  194. {
  195. struct delayed_work *dwork = (struct delayed_work *)__data;
  196. struct workqueue_struct *wq = get_wq_data(&dwork->work);
  197. int cpu = smp_processor_id();
  198. if (unlikely(is_single_threaded(wq)))
  199. cpu = singlethread_cpu;
  200. __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
  201. }
  202. /**
  203. * queue_delayed_work - queue work on a workqueue after delay
  204. * @wq: workqueue to use
  205. * @dwork: delayable work to queue
  206. * @delay: number of jiffies to wait before queueing
  207. *
  208. * Returns 0 if @work was already on a queue, non-zero otherwise.
  209. */
  210. int fastcall queue_delayed_work(struct workqueue_struct *wq,
  211. struct delayed_work *dwork, unsigned long delay)
  212. {
  213. int ret = 0;
  214. struct timer_list *timer = &dwork->timer;
  215. struct work_struct *work = &dwork->work;
  216. timer_stats_timer_set_start_info(timer);
  217. if (delay == 0)
  218. return queue_work(wq, work);
  219. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  220. BUG_ON(timer_pending(timer));
  221. BUG_ON(!list_empty(&work->entry));
  222. /* This stores wq for the moment, for the timer_fn */
  223. set_wq_data(work, wq);
  224. timer->expires = jiffies + delay;
  225. timer->data = (unsigned long)dwork;
  226. timer->function = delayed_work_timer_fn;
  227. add_timer(timer);
  228. ret = 1;
  229. }
  230. return ret;
  231. }
  232. EXPORT_SYMBOL_GPL(queue_delayed_work);
  233. /**
  234. * queue_delayed_work_on - queue work on specific CPU after delay
  235. * @cpu: CPU number to execute work on
  236. * @wq: workqueue to use
  237. * @dwork: work to queue
  238. * @delay: number of jiffies to wait before queueing
  239. *
  240. * Returns 0 if @work was already on a queue, non-zero otherwise.
  241. */
  242. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  243. struct delayed_work *dwork, unsigned long delay)
  244. {
  245. int ret = 0;
  246. struct timer_list *timer = &dwork->timer;
  247. struct work_struct *work = &dwork->work;
  248. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  249. BUG_ON(timer_pending(timer));
  250. BUG_ON(!list_empty(&work->entry));
  251. /* This stores wq for the moment, for the timer_fn */
  252. set_wq_data(work, wq);
  253. timer->expires = jiffies + delay;
  254. timer->data = (unsigned long)dwork;
  255. timer->function = delayed_work_timer_fn;
  256. add_timer_on(timer, cpu);
  257. ret = 1;
  258. }
  259. return ret;
  260. }
  261. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  262. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  263. {
  264. unsigned long flags;
  265. /*
  266. * Keep taking off work from the queue until
  267. * done.
  268. */
  269. spin_lock_irqsave(&cwq->lock, flags);
  270. cwq->run_depth++;
  271. if (cwq->run_depth > 3) {
  272. /* morton gets to eat his hat */
  273. printk("%s: recursion depth exceeded: %d\n",
  274. __FUNCTION__, cwq->run_depth);
  275. dump_stack();
  276. }
  277. while (!list_empty(&cwq->worklist)) {
  278. struct work_struct *work = list_entry(cwq->worklist.next,
  279. struct work_struct, entry);
  280. work_func_t f = work->func;
  281. cwq->current_work = work;
  282. list_del_init(cwq->worklist.next);
  283. spin_unlock_irqrestore(&cwq->lock, flags);
  284. BUG_ON(get_wq_data(work) != cwq);
  285. if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
  286. work_release(work);
  287. f(work);
  288. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  289. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  290. "%s/0x%08x/%d\n",
  291. current->comm, preempt_count(),
  292. current->pid);
  293. printk(KERN_ERR " last function: ");
  294. print_symbol("%s\n", (unsigned long)f);
  295. debug_show_held_locks(current);
  296. dump_stack();
  297. }
  298. spin_lock_irqsave(&cwq->lock, flags);
  299. cwq->current_work = NULL;
  300. }
  301. cwq->run_depth--;
  302. spin_unlock_irqrestore(&cwq->lock, flags);
  303. }
  304. static int worker_thread(void *__cwq)
  305. {
  306. struct cpu_workqueue_struct *cwq = __cwq;
  307. DECLARE_WAITQUEUE(wait, current);
  308. struct k_sigaction sa;
  309. sigset_t blocked;
  310. if (!cwq->wq->freezeable)
  311. current->flags |= PF_NOFREEZE;
  312. set_user_nice(current, -5);
  313. /* Block and flush all signals */
  314. sigfillset(&blocked);
  315. sigprocmask(SIG_BLOCK, &blocked, NULL);
  316. flush_signals(current);
  317. /*
  318. * We inherited MPOL_INTERLEAVE from the booting kernel.
  319. * Set MPOL_DEFAULT to insure node local allocations.
  320. */
  321. numa_default_policy();
  322. /* SIG_IGN makes children autoreap: see do_notify_parent(). */
  323. sa.sa.sa_handler = SIG_IGN;
  324. sa.sa.sa_flags = 0;
  325. siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
  326. do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
  327. set_current_state(TASK_INTERRUPTIBLE);
  328. while (!kthread_should_stop()) {
  329. if (cwq->wq->freezeable)
  330. try_to_freeze();
  331. add_wait_queue(&cwq->more_work, &wait);
  332. if (list_empty(&cwq->worklist))
  333. schedule();
  334. else
  335. __set_current_state(TASK_RUNNING);
  336. remove_wait_queue(&cwq->more_work, &wait);
  337. if (!list_empty(&cwq->worklist))
  338. run_workqueue(cwq);
  339. set_current_state(TASK_INTERRUPTIBLE);
  340. }
  341. __set_current_state(TASK_RUNNING);
  342. return 0;
  343. }
  344. struct wq_barrier {
  345. struct work_struct work;
  346. struct completion done;
  347. };
  348. static void wq_barrier_func(struct work_struct *work)
  349. {
  350. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  351. complete(&barr->done);
  352. }
  353. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  354. struct wq_barrier *barr, int tail)
  355. {
  356. INIT_WORK(&barr->work, wq_barrier_func);
  357. __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
  358. init_completion(&barr->done);
  359. insert_work(cwq, &barr->work, tail);
  360. }
  361. static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  362. {
  363. if (cwq->thread == current) {
  364. /*
  365. * Probably keventd trying to flush its own queue. So simply run
  366. * it by hand rather than deadlocking.
  367. */
  368. preempt_enable();
  369. /*
  370. * We can still touch *cwq here because we are keventd, and
  371. * hot-unplug will be waiting us to exit.
  372. */
  373. run_workqueue(cwq);
  374. preempt_disable();
  375. } else {
  376. struct wq_barrier barr;
  377. int active = 0;
  378. spin_lock_irq(&cwq->lock);
  379. if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
  380. insert_wq_barrier(cwq, &barr, 1);
  381. active = 1;
  382. }
  383. spin_unlock_irq(&cwq->lock);
  384. if (active) {
  385. preempt_enable();
  386. wait_for_completion(&barr.done);
  387. preempt_disable();
  388. }
  389. }
  390. }
  391. /**
  392. * flush_workqueue - ensure that any scheduled work has run to completion.
  393. * @wq: workqueue to flush
  394. *
  395. * Forces execution of the workqueue and blocks until its completion.
  396. * This is typically used in driver shutdown handlers.
  397. *
  398. * We sleep until all works which were queued on entry have been handled,
  399. * but we are not livelocked by new incoming ones.
  400. *
  401. * This function used to run the workqueues itself. Now we just wait for the
  402. * helper threads to do it.
  403. */
  404. void fastcall flush_workqueue(struct workqueue_struct *wq)
  405. {
  406. preempt_disable(); /* CPU hotplug */
  407. if (is_single_threaded(wq)) {
  408. /* Always use first cpu's area. */
  409. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
  410. } else {
  411. int cpu;
  412. for_each_online_cpu(cpu)
  413. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  414. }
  415. preempt_enable();
  416. }
  417. EXPORT_SYMBOL_GPL(flush_workqueue);
  418. static void wait_on_work(struct cpu_workqueue_struct *cwq,
  419. struct work_struct *work)
  420. {
  421. struct wq_barrier barr;
  422. int running = 0;
  423. spin_lock_irq(&cwq->lock);
  424. if (unlikely(cwq->current_work == work)) {
  425. insert_wq_barrier(cwq, &barr, 0);
  426. running = 1;
  427. }
  428. spin_unlock_irq(&cwq->lock);
  429. if (unlikely(running)) {
  430. mutex_unlock(&workqueue_mutex);
  431. wait_for_completion(&barr.done);
  432. mutex_lock(&workqueue_mutex);
  433. }
  434. }
  435. /**
  436. * flush_work - block until a work_struct's callback has terminated
  437. * @wq: the workqueue on which the work is queued
  438. * @work: the work which is to be flushed
  439. *
  440. * flush_work() will attempt to cancel the work if it is queued. If the work's
  441. * callback appears to be running, flush_work() will block until it has
  442. * completed.
  443. *
  444. * flush_work() is designed to be used when the caller is tearing down data
  445. * structures which the callback function operates upon. It is expected that,
  446. * prior to calling flush_work(), the caller has arranged for the work to not
  447. * be requeued.
  448. */
  449. void flush_work(struct workqueue_struct *wq, struct work_struct *work)
  450. {
  451. struct cpu_workqueue_struct *cwq;
  452. mutex_lock(&workqueue_mutex);
  453. cwq = get_wq_data(work);
  454. /* Was it ever queued ? */
  455. if (!cwq)
  456. goto out;
  457. /*
  458. * This work can't be re-queued, and the lock above protects us
  459. * from take_over_work(), no need to re-check that get_wq_data()
  460. * is still the same when we take cwq->lock.
  461. */
  462. spin_lock_irq(&cwq->lock);
  463. list_del_init(&work->entry);
  464. work_release(work);
  465. spin_unlock_irq(&cwq->lock);
  466. if (is_single_threaded(wq)) {
  467. /* Always use first cpu's area. */
  468. wait_on_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
  469. } else {
  470. int cpu;
  471. for_each_online_cpu(cpu)
  472. wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  473. }
  474. out:
  475. mutex_unlock(&workqueue_mutex);
  476. }
  477. EXPORT_SYMBOL_GPL(flush_work);
  478. static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
  479. int cpu)
  480. {
  481. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  482. struct task_struct *p;
  483. spin_lock_init(&cwq->lock);
  484. cwq->wq = wq;
  485. cwq->thread = NULL;
  486. INIT_LIST_HEAD(&cwq->worklist);
  487. init_waitqueue_head(&cwq->more_work);
  488. if (is_single_threaded(wq))
  489. p = kthread_create(worker_thread, cwq, "%s", wq->name);
  490. else
  491. p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
  492. if (IS_ERR(p))
  493. return NULL;
  494. cwq->thread = p;
  495. return p;
  496. }
  497. struct workqueue_struct *__create_workqueue(const char *name,
  498. int singlethread, int freezeable)
  499. {
  500. int cpu, destroy = 0;
  501. struct workqueue_struct *wq;
  502. struct task_struct *p;
  503. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  504. if (!wq)
  505. return NULL;
  506. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  507. if (!wq->cpu_wq) {
  508. kfree(wq);
  509. return NULL;
  510. }
  511. wq->name = name;
  512. wq->freezeable = freezeable;
  513. mutex_lock(&workqueue_mutex);
  514. if (singlethread) {
  515. INIT_LIST_HEAD(&wq->list);
  516. p = create_workqueue_thread(wq, singlethread_cpu);
  517. if (!p)
  518. destroy = 1;
  519. else
  520. wake_up_process(p);
  521. } else {
  522. list_add(&wq->list, &workqueues);
  523. for_each_online_cpu(cpu) {
  524. p = create_workqueue_thread(wq, cpu);
  525. if (p) {
  526. kthread_bind(p, cpu);
  527. wake_up_process(p);
  528. } else
  529. destroy = 1;
  530. }
  531. }
  532. mutex_unlock(&workqueue_mutex);
  533. /*
  534. * Was there any error during startup? If yes then clean up:
  535. */
  536. if (destroy) {
  537. destroy_workqueue(wq);
  538. wq = NULL;
  539. }
  540. return wq;
  541. }
  542. EXPORT_SYMBOL_GPL(__create_workqueue);
  543. static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
  544. {
  545. struct cpu_workqueue_struct *cwq;
  546. unsigned long flags;
  547. struct task_struct *p;
  548. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  549. spin_lock_irqsave(&cwq->lock, flags);
  550. p = cwq->thread;
  551. cwq->thread = NULL;
  552. spin_unlock_irqrestore(&cwq->lock, flags);
  553. if (p)
  554. kthread_stop(p);
  555. }
  556. /**
  557. * destroy_workqueue - safely terminate a workqueue
  558. * @wq: target workqueue
  559. *
  560. * Safely destroy a workqueue. All work currently pending will be done first.
  561. */
  562. void destroy_workqueue(struct workqueue_struct *wq)
  563. {
  564. int cpu;
  565. flush_workqueue(wq);
  566. /* We don't need the distraction of CPUs appearing and vanishing. */
  567. mutex_lock(&workqueue_mutex);
  568. if (is_single_threaded(wq))
  569. cleanup_workqueue_thread(wq, singlethread_cpu);
  570. else {
  571. for_each_online_cpu(cpu)
  572. cleanup_workqueue_thread(wq, cpu);
  573. list_del(&wq->list);
  574. }
  575. mutex_unlock(&workqueue_mutex);
  576. free_percpu(wq->cpu_wq);
  577. kfree(wq);
  578. }
  579. EXPORT_SYMBOL_GPL(destroy_workqueue);
  580. static struct workqueue_struct *keventd_wq;
  581. /**
  582. * schedule_work - put work task in global workqueue
  583. * @work: job to be done
  584. *
  585. * This puts a job in the kernel-global workqueue.
  586. */
  587. int fastcall schedule_work(struct work_struct *work)
  588. {
  589. return queue_work(keventd_wq, work);
  590. }
  591. EXPORT_SYMBOL(schedule_work);
  592. /**
  593. * schedule_delayed_work - put work task in global workqueue after delay
  594. * @dwork: job to be done
  595. * @delay: number of jiffies to wait or 0 for immediate execution
  596. *
  597. * After waiting for a given time this puts a job in the kernel-global
  598. * workqueue.
  599. */
  600. int fastcall schedule_delayed_work(struct delayed_work *dwork,
  601. unsigned long delay)
  602. {
  603. timer_stats_timer_set_start_info(&dwork->timer);
  604. return queue_delayed_work(keventd_wq, dwork, delay);
  605. }
  606. EXPORT_SYMBOL(schedule_delayed_work);
  607. /**
  608. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  609. * @cpu: cpu to use
  610. * @dwork: job to be done
  611. * @delay: number of jiffies to wait
  612. *
  613. * After waiting for a given time this puts a job in the kernel-global
  614. * workqueue on the specified CPU.
  615. */
  616. int schedule_delayed_work_on(int cpu,
  617. struct delayed_work *dwork, unsigned long delay)
  618. {
  619. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  620. }
  621. EXPORT_SYMBOL(schedule_delayed_work_on);
  622. /**
  623. * schedule_on_each_cpu - call a function on each online CPU from keventd
  624. * @func: the function to call
  625. *
  626. * Returns zero on success.
  627. * Returns -ve errno on failure.
  628. *
  629. * Appears to be racy against CPU hotplug.
  630. *
  631. * schedule_on_each_cpu() is very slow.
  632. */
  633. int schedule_on_each_cpu(work_func_t func)
  634. {
  635. int cpu;
  636. struct work_struct *works;
  637. works = alloc_percpu(struct work_struct);
  638. if (!works)
  639. return -ENOMEM;
  640. preempt_disable(); /* CPU hotplug */
  641. for_each_online_cpu(cpu) {
  642. struct work_struct *work = per_cpu_ptr(works, cpu);
  643. INIT_WORK(work, func);
  644. set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
  645. __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
  646. }
  647. preempt_enable();
  648. flush_workqueue(keventd_wq);
  649. free_percpu(works);
  650. return 0;
  651. }
  652. void flush_scheduled_work(void)
  653. {
  654. flush_workqueue(keventd_wq);
  655. }
  656. EXPORT_SYMBOL(flush_scheduled_work);
  657. void flush_work_keventd(struct work_struct *work)
  658. {
  659. flush_work(keventd_wq, work);
  660. }
  661. EXPORT_SYMBOL(flush_work_keventd);
  662. /**
  663. * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
  664. * @wq: the controlling workqueue structure
  665. * @dwork: the delayed work struct
  666. */
  667. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  668. struct delayed_work *dwork)
  669. {
  670. while (!cancel_delayed_work(dwork))
  671. flush_workqueue(wq);
  672. }
  673. EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
  674. /**
  675. * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
  676. * @dwork: the delayed work struct
  677. */
  678. void cancel_rearming_delayed_work(struct delayed_work *dwork)
  679. {
  680. cancel_rearming_delayed_workqueue(keventd_wq, dwork);
  681. }
  682. EXPORT_SYMBOL(cancel_rearming_delayed_work);
  683. /**
  684. * execute_in_process_context - reliably execute the routine with user context
  685. * @fn: the function to execute
  686. * @ew: guaranteed storage for the execute work structure (must
  687. * be available when the work executes)
  688. *
  689. * Executes the function immediately if process context is available,
  690. * otherwise schedules the function for delayed execution.
  691. *
  692. * Returns: 0 - function was executed
  693. * 1 - function was scheduled for execution
  694. */
  695. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  696. {
  697. if (!in_interrupt()) {
  698. fn(&ew->work);
  699. return 0;
  700. }
  701. INIT_WORK(&ew->work, fn);
  702. schedule_work(&ew->work);
  703. return 1;
  704. }
  705. EXPORT_SYMBOL_GPL(execute_in_process_context);
  706. int keventd_up(void)
  707. {
  708. return keventd_wq != NULL;
  709. }
  710. int current_is_keventd(void)
  711. {
  712. struct cpu_workqueue_struct *cwq;
  713. int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  714. int ret = 0;
  715. BUG_ON(!keventd_wq);
  716. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  717. if (current == cwq->thread)
  718. ret = 1;
  719. return ret;
  720. }
  721. /* Take the work from this (downed) CPU. */
  722. static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
  723. {
  724. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  725. struct list_head list;
  726. struct work_struct *work;
  727. spin_lock_irq(&cwq->lock);
  728. list_replace_init(&cwq->worklist, &list);
  729. while (!list_empty(&list)) {
  730. printk("Taking work for %s\n", wq->name);
  731. work = list_entry(list.next,struct work_struct,entry);
  732. list_del(&work->entry);
  733. __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
  734. }
  735. spin_unlock_irq(&cwq->lock);
  736. }
  737. /* We're holding the cpucontrol mutex here */
  738. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  739. unsigned long action,
  740. void *hcpu)
  741. {
  742. unsigned int hotcpu = (unsigned long)hcpu;
  743. struct workqueue_struct *wq;
  744. switch (action) {
  745. case CPU_UP_PREPARE:
  746. mutex_lock(&workqueue_mutex);
  747. /* Create a new workqueue thread for it. */
  748. list_for_each_entry(wq, &workqueues, list) {
  749. if (!create_workqueue_thread(wq, hotcpu)) {
  750. printk("workqueue for %i failed\n", hotcpu);
  751. return NOTIFY_BAD;
  752. }
  753. }
  754. break;
  755. case CPU_ONLINE:
  756. /* Kick off worker threads. */
  757. list_for_each_entry(wq, &workqueues, list) {
  758. struct cpu_workqueue_struct *cwq;
  759. cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
  760. kthread_bind(cwq->thread, hotcpu);
  761. wake_up_process(cwq->thread);
  762. }
  763. mutex_unlock(&workqueue_mutex);
  764. break;
  765. case CPU_UP_CANCELED:
  766. list_for_each_entry(wq, &workqueues, list) {
  767. if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
  768. continue;
  769. /* Unbind so it can run. */
  770. kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
  771. any_online_cpu(cpu_online_map));
  772. cleanup_workqueue_thread(wq, hotcpu);
  773. }
  774. mutex_unlock(&workqueue_mutex);
  775. break;
  776. case CPU_DOWN_PREPARE:
  777. mutex_lock(&workqueue_mutex);
  778. break;
  779. case CPU_DOWN_FAILED:
  780. mutex_unlock(&workqueue_mutex);
  781. break;
  782. case CPU_DEAD:
  783. list_for_each_entry(wq, &workqueues, list)
  784. cleanup_workqueue_thread(wq, hotcpu);
  785. list_for_each_entry(wq, &workqueues, list)
  786. take_over_work(wq, hotcpu);
  787. mutex_unlock(&workqueue_mutex);
  788. break;
  789. }
  790. return NOTIFY_OK;
  791. }
  792. void init_workqueues(void)
  793. {
  794. singlethread_cpu = first_cpu(cpu_possible_map);
  795. hotcpu_notifier(workqueue_cpu_callback, 0);
  796. keventd_wq = create_workqueue("events");
  797. BUG_ON(!keventd_wq);
  798. }