workqueue.c 21 KB

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