workqueue.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. /*
  76. * Set the workqueue on which a work item is to be run
  77. * - Must *only* be called if the pending flag is set
  78. */
  79. static inline void set_wq_data(struct work_struct *work, void *wq)
  80. {
  81. unsigned long new;
  82. BUG_ON(!work_pending(work));
  83. new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
  84. new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
  85. atomic_long_set(&work->data, new);
  86. }
  87. static inline void *get_wq_data(struct work_struct *work)
  88. {
  89. return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
  90. }
  91. static int __run_work(struct cpu_workqueue_struct *cwq, struct work_struct *work)
  92. {
  93. int ret = 0;
  94. unsigned long flags;
  95. spin_lock_irqsave(&cwq->lock, flags);
  96. /*
  97. * We need to re-validate the work info after we've gotten
  98. * the cpu_workqueue lock. We can run the work now iff:
  99. *
  100. * - the wq_data still matches the cpu_workqueue_struct
  101. * - AND the work is still marked pending
  102. * - AND the work is still on a list (which will be this
  103. * workqueue_struct list)
  104. *
  105. * All these conditions are important, because we
  106. * need to protect against the work being run right
  107. * now on another CPU (all but the last one might be
  108. * true if it's currently running and has not been
  109. * released yet, for example).
  110. */
  111. if (get_wq_data(work) == cwq
  112. && work_pending(work)
  113. && !list_empty(&work->entry)) {
  114. work_func_t f = work->func;
  115. list_del_init(&work->entry);
  116. spin_unlock_irqrestore(&cwq->lock, flags);
  117. if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
  118. work_release(work);
  119. f(work);
  120. spin_lock_irqsave(&cwq->lock, flags);
  121. cwq->remove_sequence++;
  122. wake_up(&cwq->work_done);
  123. ret = 1;
  124. }
  125. spin_unlock_irqrestore(&cwq->lock, flags);
  126. return ret;
  127. }
  128. /**
  129. * run_scheduled_work - run scheduled work synchronously
  130. * @work: work to run
  131. *
  132. * This checks if the work was pending, and runs it
  133. * synchronously if so. It returns a boolean to indicate
  134. * whether it had any scheduled work to run or not.
  135. *
  136. * NOTE! This _only_ works for normal work_structs. You
  137. * CANNOT use this for delayed work, because the wq data
  138. * for delayed work will not point properly to the per-
  139. * CPU workqueue struct, but will change!
  140. */
  141. int fastcall run_scheduled_work(struct work_struct *work)
  142. {
  143. for (;;) {
  144. struct cpu_workqueue_struct *cwq;
  145. if (!work_pending(work))
  146. return 0;
  147. if (list_empty(&work->entry))
  148. return 0;
  149. /* NOTE! This depends intimately on __queue_work! */
  150. cwq = get_wq_data(work);
  151. if (!cwq)
  152. return 0;
  153. if (__run_work(cwq, work))
  154. return 1;
  155. }
  156. }
  157. EXPORT_SYMBOL(run_scheduled_work);
  158. /* Preempt must be disabled. */
  159. static void __queue_work(struct cpu_workqueue_struct *cwq,
  160. struct work_struct *work)
  161. {
  162. unsigned long flags;
  163. spin_lock_irqsave(&cwq->lock, flags);
  164. set_wq_data(work, cwq);
  165. list_add_tail(&work->entry, &cwq->worklist);
  166. cwq->insert_sequence++;
  167. wake_up(&cwq->more_work);
  168. spin_unlock_irqrestore(&cwq->lock, flags);
  169. }
  170. /**
  171. * queue_work - queue work on a workqueue
  172. * @wq: workqueue to use
  173. * @work: work to queue
  174. *
  175. * Returns 0 if @work was already on a queue, non-zero otherwise.
  176. *
  177. * We queue the work to the CPU it was submitted, but there is no
  178. * guarantee that it will be processed by that CPU.
  179. */
  180. int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
  181. {
  182. int ret = 0, cpu = get_cpu();
  183. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  184. if (unlikely(is_single_threaded(wq)))
  185. cpu = singlethread_cpu;
  186. BUG_ON(!list_empty(&work->entry));
  187. __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  188. ret = 1;
  189. }
  190. put_cpu();
  191. return ret;
  192. }
  193. EXPORT_SYMBOL_GPL(queue_work);
  194. static void delayed_work_timer_fn(unsigned long __data)
  195. {
  196. struct delayed_work *dwork = (struct delayed_work *)__data;
  197. struct workqueue_struct *wq = get_wq_data(&dwork->work);
  198. int cpu = smp_processor_id();
  199. if (unlikely(is_single_threaded(wq)))
  200. cpu = singlethread_cpu;
  201. __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
  202. }
  203. /**
  204. * queue_delayed_work - queue work on a workqueue after delay
  205. * @wq: workqueue to use
  206. * @dwork: delayable work to queue
  207. * @delay: number of jiffies to wait before queueing
  208. *
  209. * Returns 0 if @work was already on a queue, non-zero otherwise.
  210. */
  211. int fastcall queue_delayed_work(struct workqueue_struct *wq,
  212. struct delayed_work *dwork, unsigned long delay)
  213. {
  214. int ret = 0;
  215. struct timer_list *timer = &dwork->timer;
  216. struct work_struct *work = &dwork->work;
  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. list_del_init(cwq->worklist.next);
  282. spin_unlock_irqrestore(&cwq->lock, flags);
  283. BUG_ON(get_wq_data(work) != cwq);
  284. if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
  285. work_release(work);
  286. f(work);
  287. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  288. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  289. "%s/0x%08x/%d\n",
  290. current->comm, preempt_count(),
  291. current->pid);
  292. printk(KERN_ERR " last function: ");
  293. print_symbol("%s\n", (unsigned long)f);
  294. debug_show_held_locks(current);
  295. dump_stack();
  296. }
  297. spin_lock_irqsave(&cwq->lock, flags);
  298. cwq->remove_sequence++;
  299. wake_up(&cwq->work_done);
  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. static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  345. {
  346. if (cwq->thread == current) {
  347. /*
  348. * Probably keventd trying to flush its own queue. So simply run
  349. * it by hand rather than deadlocking.
  350. */
  351. run_workqueue(cwq);
  352. } else {
  353. DEFINE_WAIT(wait);
  354. long sequence_needed;
  355. spin_lock_irq(&cwq->lock);
  356. sequence_needed = cwq->insert_sequence;
  357. while (sequence_needed - cwq->remove_sequence > 0) {
  358. prepare_to_wait(&cwq->work_done, &wait,
  359. TASK_UNINTERRUPTIBLE);
  360. spin_unlock_irq(&cwq->lock);
  361. schedule();
  362. spin_lock_irq(&cwq->lock);
  363. }
  364. finish_wait(&cwq->work_done, &wait);
  365. spin_unlock_irq(&cwq->lock);
  366. }
  367. }
  368. /**
  369. * flush_workqueue - ensure that any scheduled work has run to completion.
  370. * @wq: workqueue to flush
  371. *
  372. * Forces execution of the workqueue and blocks until its completion.
  373. * This is typically used in driver shutdown handlers.
  374. *
  375. * This function will sample each workqueue's current insert_sequence number and
  376. * will sleep until the head sequence is greater than or equal to that. This
  377. * means that we sleep until all works which were queued on entry have been
  378. * handled, but we are not livelocked by new incoming ones.
  379. *
  380. * This function used to run the workqueues itself. Now we just wait for the
  381. * helper threads to do it.
  382. */
  383. void fastcall flush_workqueue(struct workqueue_struct *wq)
  384. {
  385. might_sleep();
  386. if (is_single_threaded(wq)) {
  387. /* Always use first cpu's area. */
  388. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
  389. } else {
  390. int cpu;
  391. mutex_lock(&workqueue_mutex);
  392. for_each_online_cpu(cpu)
  393. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  394. mutex_unlock(&workqueue_mutex);
  395. }
  396. }
  397. EXPORT_SYMBOL_GPL(flush_workqueue);
  398. static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
  399. int cpu, int freezeable)
  400. {
  401. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  402. struct task_struct *p;
  403. spin_lock_init(&cwq->lock);
  404. cwq->wq = wq;
  405. cwq->thread = NULL;
  406. cwq->insert_sequence = 0;
  407. cwq->remove_sequence = 0;
  408. cwq->freezeable = freezeable;
  409. INIT_LIST_HEAD(&cwq->worklist);
  410. init_waitqueue_head(&cwq->more_work);
  411. init_waitqueue_head(&cwq->work_done);
  412. if (is_single_threaded(wq))
  413. p = kthread_create(worker_thread, cwq, "%s", wq->name);
  414. else
  415. p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
  416. if (IS_ERR(p))
  417. return NULL;
  418. cwq->thread = p;
  419. return p;
  420. }
  421. struct workqueue_struct *__create_workqueue(const char *name,
  422. int singlethread, int freezeable)
  423. {
  424. int cpu, destroy = 0;
  425. struct workqueue_struct *wq;
  426. struct task_struct *p;
  427. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  428. if (!wq)
  429. return NULL;
  430. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  431. if (!wq->cpu_wq) {
  432. kfree(wq);
  433. return NULL;
  434. }
  435. wq->name = name;
  436. mutex_lock(&workqueue_mutex);
  437. if (singlethread) {
  438. INIT_LIST_HEAD(&wq->list);
  439. p = create_workqueue_thread(wq, singlethread_cpu, freezeable);
  440. if (!p)
  441. destroy = 1;
  442. else
  443. wake_up_process(p);
  444. } else {
  445. list_add(&wq->list, &workqueues);
  446. for_each_online_cpu(cpu) {
  447. p = create_workqueue_thread(wq, cpu, freezeable);
  448. if (p) {
  449. kthread_bind(p, cpu);
  450. wake_up_process(p);
  451. } else
  452. destroy = 1;
  453. }
  454. }
  455. mutex_unlock(&workqueue_mutex);
  456. /*
  457. * Was there any error during startup? If yes then clean up:
  458. */
  459. if (destroy) {
  460. destroy_workqueue(wq);
  461. wq = NULL;
  462. }
  463. return wq;
  464. }
  465. EXPORT_SYMBOL_GPL(__create_workqueue);
  466. static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
  467. {
  468. struct cpu_workqueue_struct *cwq;
  469. unsigned long flags;
  470. struct task_struct *p;
  471. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  472. spin_lock_irqsave(&cwq->lock, flags);
  473. p = cwq->thread;
  474. cwq->thread = NULL;
  475. spin_unlock_irqrestore(&cwq->lock, flags);
  476. if (p)
  477. kthread_stop(p);
  478. }
  479. /**
  480. * destroy_workqueue - safely terminate a workqueue
  481. * @wq: target workqueue
  482. *
  483. * Safely destroy a workqueue. All work currently pending will be done first.
  484. */
  485. void destroy_workqueue(struct workqueue_struct *wq)
  486. {
  487. int cpu;
  488. flush_workqueue(wq);
  489. /* We don't need the distraction of CPUs appearing and vanishing. */
  490. mutex_lock(&workqueue_mutex);
  491. if (is_single_threaded(wq))
  492. cleanup_workqueue_thread(wq, singlethread_cpu);
  493. else {
  494. for_each_online_cpu(cpu)
  495. cleanup_workqueue_thread(wq, cpu);
  496. list_del(&wq->list);
  497. }
  498. mutex_unlock(&workqueue_mutex);
  499. free_percpu(wq->cpu_wq);
  500. kfree(wq);
  501. }
  502. EXPORT_SYMBOL_GPL(destroy_workqueue);
  503. static struct workqueue_struct *keventd_wq;
  504. /**
  505. * schedule_work - put work task in global workqueue
  506. * @work: job to be done
  507. *
  508. * This puts a job in the kernel-global workqueue.
  509. */
  510. int fastcall schedule_work(struct work_struct *work)
  511. {
  512. return queue_work(keventd_wq, work);
  513. }
  514. EXPORT_SYMBOL(schedule_work);
  515. /**
  516. * schedule_delayed_work - put work task in global workqueue after delay
  517. * @dwork: job to be done
  518. * @delay: number of jiffies to wait or 0 for immediate execution
  519. *
  520. * After waiting for a given time this puts a job in the kernel-global
  521. * workqueue.
  522. */
  523. int fastcall schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
  524. {
  525. return queue_delayed_work(keventd_wq, dwork, delay);
  526. }
  527. EXPORT_SYMBOL(schedule_delayed_work);
  528. /**
  529. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  530. * @cpu: cpu to use
  531. * @dwork: job to be done
  532. * @delay: number of jiffies to wait
  533. *
  534. * After waiting for a given time this puts a job in the kernel-global
  535. * workqueue on the specified CPU.
  536. */
  537. int schedule_delayed_work_on(int cpu,
  538. struct delayed_work *dwork, unsigned long delay)
  539. {
  540. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  541. }
  542. EXPORT_SYMBOL(schedule_delayed_work_on);
  543. /**
  544. * schedule_on_each_cpu - call a function on each online CPU from keventd
  545. * @func: the function to call
  546. *
  547. * Returns zero on success.
  548. * Returns -ve errno on failure.
  549. *
  550. * Appears to be racy against CPU hotplug.
  551. *
  552. * schedule_on_each_cpu() is very slow.
  553. */
  554. int schedule_on_each_cpu(work_func_t func)
  555. {
  556. int cpu;
  557. struct work_struct *works;
  558. works = alloc_percpu(struct work_struct);
  559. if (!works)
  560. return -ENOMEM;
  561. mutex_lock(&workqueue_mutex);
  562. for_each_online_cpu(cpu) {
  563. struct work_struct *work = per_cpu_ptr(works, cpu);
  564. INIT_WORK(work, func);
  565. set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
  566. __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
  567. }
  568. mutex_unlock(&workqueue_mutex);
  569. flush_workqueue(keventd_wq);
  570. free_percpu(works);
  571. return 0;
  572. }
  573. void flush_scheduled_work(void)
  574. {
  575. flush_workqueue(keventd_wq);
  576. }
  577. EXPORT_SYMBOL(flush_scheduled_work);
  578. /**
  579. * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
  580. * @wq: the controlling workqueue structure
  581. * @dwork: the delayed work struct
  582. */
  583. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  584. struct delayed_work *dwork)
  585. {
  586. while (!cancel_delayed_work(dwork))
  587. flush_workqueue(wq);
  588. }
  589. EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
  590. /**
  591. * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
  592. * @dwork: the delayed work struct
  593. */
  594. void cancel_rearming_delayed_work(struct delayed_work *dwork)
  595. {
  596. cancel_rearming_delayed_workqueue(keventd_wq, dwork);
  597. }
  598. EXPORT_SYMBOL(cancel_rearming_delayed_work);
  599. /**
  600. * execute_in_process_context - reliably execute the routine with user context
  601. * @fn: the function to execute
  602. * @ew: guaranteed storage for the execute work structure (must
  603. * be available when the work executes)
  604. *
  605. * Executes the function immediately if process context is available,
  606. * otherwise schedules the function for delayed execution.
  607. *
  608. * Returns: 0 - function was executed
  609. * 1 - function was scheduled for execution
  610. */
  611. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  612. {
  613. if (!in_interrupt()) {
  614. fn(&ew->work);
  615. return 0;
  616. }
  617. INIT_WORK(&ew->work, fn);
  618. schedule_work(&ew->work);
  619. return 1;
  620. }
  621. EXPORT_SYMBOL_GPL(execute_in_process_context);
  622. int keventd_up(void)
  623. {
  624. return keventd_wq != NULL;
  625. }
  626. int current_is_keventd(void)
  627. {
  628. struct cpu_workqueue_struct *cwq;
  629. int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  630. int ret = 0;
  631. BUG_ON(!keventd_wq);
  632. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  633. if (current == cwq->thread)
  634. ret = 1;
  635. return ret;
  636. }
  637. /* Take the work from this (downed) CPU. */
  638. static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
  639. {
  640. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  641. struct list_head list;
  642. struct work_struct *work;
  643. spin_lock_irq(&cwq->lock);
  644. list_replace_init(&cwq->worklist, &list);
  645. while (!list_empty(&list)) {
  646. printk("Taking work for %s\n", wq->name);
  647. work = list_entry(list.next,struct work_struct,entry);
  648. list_del(&work->entry);
  649. __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
  650. }
  651. spin_unlock_irq(&cwq->lock);
  652. }
  653. /* We're holding the cpucontrol mutex here */
  654. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  655. unsigned long action,
  656. void *hcpu)
  657. {
  658. unsigned int hotcpu = (unsigned long)hcpu;
  659. struct workqueue_struct *wq;
  660. switch (action) {
  661. case CPU_UP_PREPARE:
  662. mutex_lock(&workqueue_mutex);
  663. /* Create a new workqueue thread for it. */
  664. list_for_each_entry(wq, &workqueues, list) {
  665. if (!create_workqueue_thread(wq, hotcpu, 0)) {
  666. printk("workqueue for %i failed\n", hotcpu);
  667. return NOTIFY_BAD;
  668. }
  669. }
  670. break;
  671. case CPU_ONLINE:
  672. /* Kick off worker threads. */
  673. list_for_each_entry(wq, &workqueues, list) {
  674. struct cpu_workqueue_struct *cwq;
  675. cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
  676. kthread_bind(cwq->thread, hotcpu);
  677. wake_up_process(cwq->thread);
  678. }
  679. mutex_unlock(&workqueue_mutex);
  680. break;
  681. case CPU_UP_CANCELED:
  682. list_for_each_entry(wq, &workqueues, list) {
  683. if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
  684. continue;
  685. /* Unbind so it can run. */
  686. kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
  687. any_online_cpu(cpu_online_map));
  688. cleanup_workqueue_thread(wq, hotcpu);
  689. }
  690. mutex_unlock(&workqueue_mutex);
  691. break;
  692. case CPU_DOWN_PREPARE:
  693. mutex_lock(&workqueue_mutex);
  694. break;
  695. case CPU_DOWN_FAILED:
  696. mutex_unlock(&workqueue_mutex);
  697. break;
  698. case CPU_DEAD:
  699. list_for_each_entry(wq, &workqueues, list)
  700. cleanup_workqueue_thread(wq, hotcpu);
  701. list_for_each_entry(wq, &workqueues, list)
  702. take_over_work(wq, hotcpu);
  703. mutex_unlock(&workqueue_mutex);
  704. break;
  705. }
  706. return NOTIFY_OK;
  707. }
  708. void init_workqueues(void)
  709. {
  710. singlethread_cpu = first_cpu(cpu_possible_map);
  711. hotcpu_notifier(workqueue_cpu_callback, 0);
  712. keventd_wq = create_workqueue("events");
  713. BUG_ON(!keventd_wq);
  714. }