workqueue.c 22 KB

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