workqueue.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. int freezeable; /* Freeze the thread during suspend */
  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. const char *name;
  55. struct list_head list; /* Empty if single thread */
  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->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->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 inline void init_wq_barrier(struct wq_barrier *barr)
  354. {
  355. INIT_WORK(&barr->work, wq_barrier_func);
  356. __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
  357. init_completion(&barr->done);
  358. }
  359. static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  360. {
  361. if (cwq->thread == current) {
  362. /*
  363. * Probably keventd trying to flush its own queue. So simply run
  364. * it by hand rather than deadlocking.
  365. */
  366. preempt_enable();
  367. /*
  368. * We can still touch *cwq here because we are keventd, and
  369. * hot-unplug will be waiting us to exit.
  370. */
  371. run_workqueue(cwq);
  372. preempt_disable();
  373. } else {
  374. struct wq_barrier barr;
  375. init_wq_barrier(&barr);
  376. __queue_work(cwq, &barr.work);
  377. preempt_enable(); /* Can no longer touch *cwq */
  378. wait_for_completion(&barr.done);
  379. preempt_disable();
  380. }
  381. }
  382. /**
  383. * flush_workqueue - ensure that any scheduled work has run to completion.
  384. * @wq: workqueue to flush
  385. *
  386. * Forces execution of the workqueue and blocks until its completion.
  387. * This is typically used in driver shutdown handlers.
  388. *
  389. * We sleep until all works which were queued on entry have been handled,
  390. * but we are not livelocked by new incoming ones.
  391. *
  392. * This function used to run the workqueues itself. Now we just wait for the
  393. * helper threads to do it.
  394. */
  395. void fastcall flush_workqueue(struct workqueue_struct *wq)
  396. {
  397. preempt_disable(); /* CPU hotplug */
  398. if (is_single_threaded(wq)) {
  399. /* Always use first cpu's area. */
  400. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
  401. } else {
  402. int cpu;
  403. for_each_online_cpu(cpu)
  404. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  405. }
  406. preempt_enable();
  407. }
  408. EXPORT_SYMBOL_GPL(flush_workqueue);
  409. static void wait_on_work(struct cpu_workqueue_struct *cwq,
  410. struct work_struct *work)
  411. {
  412. struct wq_barrier barr;
  413. int running = 0;
  414. spin_lock_irq(&cwq->lock);
  415. if (unlikely(cwq->current_work == work)) {
  416. init_wq_barrier(&barr);
  417. insert_work(cwq, &barr.work, 0);
  418. running = 1;
  419. }
  420. spin_unlock_irq(&cwq->lock);
  421. if (unlikely(running)) {
  422. mutex_unlock(&workqueue_mutex);
  423. wait_for_completion(&barr.done);
  424. mutex_lock(&workqueue_mutex);
  425. }
  426. }
  427. /**
  428. * flush_work - block until a work_struct's callback has terminated
  429. * @wq: the workqueue on which the work is queued
  430. * @work: the work which is to be flushed
  431. *
  432. * flush_work() will attempt to cancel the work if it is queued. If the work's
  433. * callback appears to be running, flush_work() will block until it has
  434. * completed.
  435. *
  436. * flush_work() is designed to be used when the caller is tearing down data
  437. * structures which the callback function operates upon. It is expected that,
  438. * prior to calling flush_work(), the caller has arranged for the work to not
  439. * be requeued.
  440. */
  441. void flush_work(struct workqueue_struct *wq, struct work_struct *work)
  442. {
  443. struct cpu_workqueue_struct *cwq;
  444. mutex_lock(&workqueue_mutex);
  445. cwq = get_wq_data(work);
  446. /* Was it ever queued ? */
  447. if (!cwq)
  448. goto out;
  449. /*
  450. * This work can't be re-queued, and the lock above protects us
  451. * from take_over_work(), no need to re-check that get_wq_data()
  452. * is still the same when we take cwq->lock.
  453. */
  454. spin_lock_irq(&cwq->lock);
  455. list_del_init(&work->entry);
  456. work_release(work);
  457. spin_unlock_irq(&cwq->lock);
  458. if (is_single_threaded(wq)) {
  459. /* Always use first cpu's area. */
  460. wait_on_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
  461. } else {
  462. int cpu;
  463. for_each_online_cpu(cpu)
  464. wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  465. }
  466. out:
  467. mutex_unlock(&workqueue_mutex);
  468. }
  469. EXPORT_SYMBOL_GPL(flush_work);
  470. static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
  471. int cpu, int freezeable)
  472. {
  473. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  474. struct task_struct *p;
  475. spin_lock_init(&cwq->lock);
  476. cwq->wq = wq;
  477. cwq->thread = NULL;
  478. cwq->freezeable = freezeable;
  479. INIT_LIST_HEAD(&cwq->worklist);
  480. init_waitqueue_head(&cwq->more_work);
  481. if (is_single_threaded(wq))
  482. p = kthread_create(worker_thread, cwq, "%s", wq->name);
  483. else
  484. p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
  485. if (IS_ERR(p))
  486. return NULL;
  487. cwq->thread = p;
  488. return p;
  489. }
  490. struct workqueue_struct *__create_workqueue(const char *name,
  491. int singlethread, int freezeable)
  492. {
  493. int cpu, destroy = 0;
  494. struct workqueue_struct *wq;
  495. struct task_struct *p;
  496. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  497. if (!wq)
  498. return NULL;
  499. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  500. if (!wq->cpu_wq) {
  501. kfree(wq);
  502. return NULL;
  503. }
  504. wq->name = name;
  505. mutex_lock(&workqueue_mutex);
  506. if (singlethread) {
  507. INIT_LIST_HEAD(&wq->list);
  508. p = create_workqueue_thread(wq, singlethread_cpu, freezeable);
  509. if (!p)
  510. destroy = 1;
  511. else
  512. wake_up_process(p);
  513. } else {
  514. list_add(&wq->list, &workqueues);
  515. for_each_online_cpu(cpu) {
  516. p = create_workqueue_thread(wq, cpu, freezeable);
  517. if (p) {
  518. kthread_bind(p, cpu);
  519. wake_up_process(p);
  520. } else
  521. destroy = 1;
  522. }
  523. }
  524. mutex_unlock(&workqueue_mutex);
  525. /*
  526. * Was there any error during startup? If yes then clean up:
  527. */
  528. if (destroy) {
  529. destroy_workqueue(wq);
  530. wq = NULL;
  531. }
  532. return wq;
  533. }
  534. EXPORT_SYMBOL_GPL(__create_workqueue);
  535. static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
  536. {
  537. struct cpu_workqueue_struct *cwq;
  538. unsigned long flags;
  539. struct task_struct *p;
  540. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  541. spin_lock_irqsave(&cwq->lock, flags);
  542. p = cwq->thread;
  543. cwq->thread = NULL;
  544. spin_unlock_irqrestore(&cwq->lock, flags);
  545. if (p)
  546. kthread_stop(p);
  547. }
  548. /**
  549. * destroy_workqueue - safely terminate a workqueue
  550. * @wq: target workqueue
  551. *
  552. * Safely destroy a workqueue. All work currently pending will be done first.
  553. */
  554. void destroy_workqueue(struct workqueue_struct *wq)
  555. {
  556. int cpu;
  557. flush_workqueue(wq);
  558. /* We don't need the distraction of CPUs appearing and vanishing. */
  559. mutex_lock(&workqueue_mutex);
  560. if (is_single_threaded(wq))
  561. cleanup_workqueue_thread(wq, singlethread_cpu);
  562. else {
  563. for_each_online_cpu(cpu)
  564. cleanup_workqueue_thread(wq, cpu);
  565. list_del(&wq->list);
  566. }
  567. mutex_unlock(&workqueue_mutex);
  568. free_percpu(wq->cpu_wq);
  569. kfree(wq);
  570. }
  571. EXPORT_SYMBOL_GPL(destroy_workqueue);
  572. static struct workqueue_struct *keventd_wq;
  573. /**
  574. * schedule_work - put work task in global workqueue
  575. * @work: job to be done
  576. *
  577. * This puts a job in the kernel-global workqueue.
  578. */
  579. int fastcall schedule_work(struct work_struct *work)
  580. {
  581. return queue_work(keventd_wq, work);
  582. }
  583. EXPORT_SYMBOL(schedule_work);
  584. /**
  585. * schedule_delayed_work - put work task in global workqueue after delay
  586. * @dwork: job to be done
  587. * @delay: number of jiffies to wait or 0 for immediate execution
  588. *
  589. * After waiting for a given time this puts a job in the kernel-global
  590. * workqueue.
  591. */
  592. int fastcall schedule_delayed_work(struct delayed_work *dwork,
  593. unsigned long delay)
  594. {
  595. timer_stats_timer_set_start_info(&dwork->timer);
  596. return queue_delayed_work(keventd_wq, dwork, delay);
  597. }
  598. EXPORT_SYMBOL(schedule_delayed_work);
  599. /**
  600. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  601. * @cpu: cpu to use
  602. * @dwork: job to be done
  603. * @delay: number of jiffies to wait
  604. *
  605. * After waiting for a given time this puts a job in the kernel-global
  606. * workqueue on the specified CPU.
  607. */
  608. int schedule_delayed_work_on(int cpu,
  609. struct delayed_work *dwork, unsigned long delay)
  610. {
  611. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  612. }
  613. EXPORT_SYMBOL(schedule_delayed_work_on);
  614. /**
  615. * schedule_on_each_cpu - call a function on each online CPU from keventd
  616. * @func: the function to call
  617. *
  618. * Returns zero on success.
  619. * Returns -ve errno on failure.
  620. *
  621. * Appears to be racy against CPU hotplug.
  622. *
  623. * schedule_on_each_cpu() is very slow.
  624. */
  625. int schedule_on_each_cpu(work_func_t func)
  626. {
  627. int cpu;
  628. struct work_struct *works;
  629. works = alloc_percpu(struct work_struct);
  630. if (!works)
  631. return -ENOMEM;
  632. preempt_disable(); /* CPU hotplug */
  633. for_each_online_cpu(cpu) {
  634. struct work_struct *work = per_cpu_ptr(works, cpu);
  635. INIT_WORK(work, func);
  636. set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
  637. __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
  638. }
  639. preempt_enable();
  640. flush_workqueue(keventd_wq);
  641. free_percpu(works);
  642. return 0;
  643. }
  644. void flush_scheduled_work(void)
  645. {
  646. flush_workqueue(keventd_wq);
  647. }
  648. EXPORT_SYMBOL(flush_scheduled_work);
  649. void flush_work_keventd(struct work_struct *work)
  650. {
  651. flush_work(keventd_wq, work);
  652. }
  653. EXPORT_SYMBOL(flush_work_keventd);
  654. /**
  655. * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
  656. * @wq: the controlling workqueue structure
  657. * @dwork: the delayed work struct
  658. */
  659. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  660. struct delayed_work *dwork)
  661. {
  662. while (!cancel_delayed_work(dwork))
  663. flush_workqueue(wq);
  664. }
  665. EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
  666. /**
  667. * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
  668. * @dwork: the delayed work struct
  669. */
  670. void cancel_rearming_delayed_work(struct delayed_work *dwork)
  671. {
  672. cancel_rearming_delayed_workqueue(keventd_wq, dwork);
  673. }
  674. EXPORT_SYMBOL(cancel_rearming_delayed_work);
  675. /**
  676. * execute_in_process_context - reliably execute the routine with user context
  677. * @fn: the function to execute
  678. * @ew: guaranteed storage for the execute work structure (must
  679. * be available when the work executes)
  680. *
  681. * Executes the function immediately if process context is available,
  682. * otherwise schedules the function for delayed execution.
  683. *
  684. * Returns: 0 - function was executed
  685. * 1 - function was scheduled for execution
  686. */
  687. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  688. {
  689. if (!in_interrupt()) {
  690. fn(&ew->work);
  691. return 0;
  692. }
  693. INIT_WORK(&ew->work, fn);
  694. schedule_work(&ew->work);
  695. return 1;
  696. }
  697. EXPORT_SYMBOL_GPL(execute_in_process_context);
  698. int keventd_up(void)
  699. {
  700. return keventd_wq != NULL;
  701. }
  702. int current_is_keventd(void)
  703. {
  704. struct cpu_workqueue_struct *cwq;
  705. int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  706. int ret = 0;
  707. BUG_ON(!keventd_wq);
  708. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  709. if (current == cwq->thread)
  710. ret = 1;
  711. return ret;
  712. }
  713. /* Take the work from this (downed) CPU. */
  714. static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
  715. {
  716. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  717. struct list_head list;
  718. struct work_struct *work;
  719. spin_lock_irq(&cwq->lock);
  720. list_replace_init(&cwq->worklist, &list);
  721. while (!list_empty(&list)) {
  722. printk("Taking work for %s\n", wq->name);
  723. work = list_entry(list.next,struct work_struct,entry);
  724. list_del(&work->entry);
  725. __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
  726. }
  727. spin_unlock_irq(&cwq->lock);
  728. }
  729. /* We're holding the cpucontrol mutex here */
  730. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  731. unsigned long action,
  732. void *hcpu)
  733. {
  734. unsigned int hotcpu = (unsigned long)hcpu;
  735. struct workqueue_struct *wq;
  736. switch (action) {
  737. case CPU_UP_PREPARE:
  738. mutex_lock(&workqueue_mutex);
  739. /* Create a new workqueue thread for it. */
  740. list_for_each_entry(wq, &workqueues, list) {
  741. if (!create_workqueue_thread(wq, hotcpu, 0)) {
  742. printk("workqueue for %i failed\n", hotcpu);
  743. return NOTIFY_BAD;
  744. }
  745. }
  746. break;
  747. case CPU_ONLINE:
  748. /* Kick off worker threads. */
  749. list_for_each_entry(wq, &workqueues, list) {
  750. struct cpu_workqueue_struct *cwq;
  751. cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
  752. kthread_bind(cwq->thread, hotcpu);
  753. wake_up_process(cwq->thread);
  754. }
  755. mutex_unlock(&workqueue_mutex);
  756. break;
  757. case CPU_UP_CANCELED:
  758. list_for_each_entry(wq, &workqueues, list) {
  759. if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
  760. continue;
  761. /* Unbind so it can run. */
  762. kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
  763. any_online_cpu(cpu_online_map));
  764. cleanup_workqueue_thread(wq, hotcpu);
  765. }
  766. mutex_unlock(&workqueue_mutex);
  767. break;
  768. case CPU_DOWN_PREPARE:
  769. mutex_lock(&workqueue_mutex);
  770. break;
  771. case CPU_DOWN_FAILED:
  772. mutex_unlock(&workqueue_mutex);
  773. break;
  774. case CPU_DEAD:
  775. list_for_each_entry(wq, &workqueues, list)
  776. cleanup_workqueue_thread(wq, hotcpu);
  777. list_for_each_entry(wq, &workqueues, list)
  778. take_over_work(wq, hotcpu);
  779. mutex_unlock(&workqueue_mutex);
  780. break;
  781. }
  782. return NOTIFY_OK;
  783. }
  784. void init_workqueues(void)
  785. {
  786. singlethread_cpu = first_cpu(cpu_possible_map);
  787. hotcpu_notifier(workqueue_cpu_callback, 0);
  788. keventd_wq = create_workqueue("events");
  789. BUG_ON(!keventd_wq);
  790. }