workqueue.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. /*
  31. * The per-CPU workqueue (if single thread, we always use the first
  32. * possible cpu).
  33. *
  34. * The sequence counters are for flush_scheduled_work(). It wants to wait
  35. * until until all currently-scheduled works are completed, but it doesn't
  36. * want to be livelocked by new, incoming ones. So it waits until
  37. * remove_sequence is >= the insert_sequence which pertained when
  38. * flush_scheduled_work() was called.
  39. */
  40. struct cpu_workqueue_struct {
  41. spinlock_t lock;
  42. long remove_sequence; /* Least-recently added (next to run) */
  43. long insert_sequence; /* Next to add */
  44. struct list_head worklist;
  45. wait_queue_head_t more_work;
  46. wait_queue_head_t work_done;
  47. struct workqueue_struct *wq;
  48. task_t *thread;
  49. int run_depth; /* Detect run_workqueue() recursion depth */
  50. } ____cacheline_aligned;
  51. /*
  52. * The externally visible workqueue abstraction is an array of
  53. * per-CPU workqueues:
  54. */
  55. struct workqueue_struct {
  56. struct cpu_workqueue_struct *cpu_wq;
  57. const char *name;
  58. struct list_head list; /* Empty if single thread */
  59. };
  60. /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
  61. threads to each one as cpus come/go. */
  62. static DEFINE_SPINLOCK(workqueue_lock);
  63. static LIST_HEAD(workqueues);
  64. static int singlethread_cpu;
  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. /* Preempt must be disabled. */
  71. static void __queue_work(struct cpu_workqueue_struct *cwq,
  72. struct work_struct *work)
  73. {
  74. unsigned long flags;
  75. spin_lock_irqsave(&cwq->lock, flags);
  76. work->wq_data = cwq;
  77. list_add_tail(&work->entry, &cwq->worklist);
  78. cwq->insert_sequence++;
  79. wake_up(&cwq->more_work);
  80. spin_unlock_irqrestore(&cwq->lock, flags);
  81. }
  82. /*
  83. * Queue work on a workqueue. Return non-zero if it was successfully
  84. * added.
  85. *
  86. * We queue the work to the CPU it was submitted, but there is no
  87. * guarantee that it will be processed by that CPU.
  88. */
  89. int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
  90. {
  91. int ret = 0, cpu = get_cpu();
  92. if (!test_and_set_bit(0, &work->pending)) {
  93. if (unlikely(is_single_threaded(wq)))
  94. cpu = singlethread_cpu;
  95. BUG_ON(!list_empty(&work->entry));
  96. __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  97. ret = 1;
  98. }
  99. put_cpu();
  100. return ret;
  101. }
  102. static void delayed_work_timer_fn(unsigned long __data)
  103. {
  104. struct work_struct *work = (struct work_struct *)__data;
  105. struct workqueue_struct *wq = work->wq_data;
  106. int cpu = smp_processor_id();
  107. if (unlikely(is_single_threaded(wq)))
  108. cpu = singlethread_cpu;
  109. __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  110. }
  111. int fastcall queue_delayed_work(struct workqueue_struct *wq,
  112. struct work_struct *work, unsigned long delay)
  113. {
  114. int ret = 0;
  115. struct timer_list *timer = &work->timer;
  116. if (!test_and_set_bit(0, &work->pending)) {
  117. BUG_ON(timer_pending(timer));
  118. BUG_ON(!list_empty(&work->entry));
  119. /* This stores wq for the moment, for the timer_fn */
  120. work->wq_data = wq;
  121. timer->expires = jiffies + delay;
  122. timer->data = (unsigned long)work;
  123. timer->function = delayed_work_timer_fn;
  124. add_timer(timer);
  125. ret = 1;
  126. }
  127. return ret;
  128. }
  129. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  130. struct work_struct *work, unsigned long delay)
  131. {
  132. int ret = 0;
  133. struct timer_list *timer = &work->timer;
  134. if (!test_and_set_bit(0, &work->pending)) {
  135. BUG_ON(timer_pending(timer));
  136. BUG_ON(!list_empty(&work->entry));
  137. /* This stores wq for the moment, for the timer_fn */
  138. work->wq_data = wq;
  139. timer->expires = jiffies + delay;
  140. timer->data = (unsigned long)work;
  141. timer->function = delayed_work_timer_fn;
  142. add_timer_on(timer, cpu);
  143. ret = 1;
  144. }
  145. return ret;
  146. }
  147. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  148. {
  149. unsigned long flags;
  150. /*
  151. * Keep taking off work from the queue until
  152. * done.
  153. */
  154. spin_lock_irqsave(&cwq->lock, flags);
  155. cwq->run_depth++;
  156. if (cwq->run_depth > 3) {
  157. /* morton gets to eat his hat */
  158. printk("%s: recursion depth exceeded: %d\n",
  159. __FUNCTION__, cwq->run_depth);
  160. dump_stack();
  161. }
  162. while (!list_empty(&cwq->worklist)) {
  163. struct work_struct *work = list_entry(cwq->worklist.next,
  164. struct work_struct, entry);
  165. void (*f) (void *) = work->func;
  166. void *data = work->data;
  167. list_del_init(cwq->worklist.next);
  168. spin_unlock_irqrestore(&cwq->lock, flags);
  169. BUG_ON(work->wq_data != cwq);
  170. clear_bit(0, &work->pending);
  171. f(data);
  172. spin_lock_irqsave(&cwq->lock, flags);
  173. cwq->remove_sequence++;
  174. wake_up(&cwq->work_done);
  175. }
  176. cwq->run_depth--;
  177. spin_unlock_irqrestore(&cwq->lock, flags);
  178. }
  179. static int worker_thread(void *__cwq)
  180. {
  181. struct cpu_workqueue_struct *cwq = __cwq;
  182. DECLARE_WAITQUEUE(wait, current);
  183. struct k_sigaction sa;
  184. sigset_t blocked;
  185. current->flags |= PF_NOFREEZE;
  186. set_user_nice(current, -5);
  187. /* Block and flush all signals */
  188. sigfillset(&blocked);
  189. sigprocmask(SIG_BLOCK, &blocked, NULL);
  190. flush_signals(current);
  191. /* SIG_IGN makes children autoreap: see do_notify_parent(). */
  192. sa.sa.sa_handler = SIG_IGN;
  193. sa.sa.sa_flags = 0;
  194. siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
  195. do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
  196. set_current_state(TASK_INTERRUPTIBLE);
  197. while (!kthread_should_stop()) {
  198. add_wait_queue(&cwq->more_work, &wait);
  199. if (list_empty(&cwq->worklist))
  200. schedule();
  201. else
  202. __set_current_state(TASK_RUNNING);
  203. remove_wait_queue(&cwq->more_work, &wait);
  204. if (!list_empty(&cwq->worklist))
  205. run_workqueue(cwq);
  206. set_current_state(TASK_INTERRUPTIBLE);
  207. }
  208. __set_current_state(TASK_RUNNING);
  209. return 0;
  210. }
  211. static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  212. {
  213. if (cwq->thread == current) {
  214. /*
  215. * Probably keventd trying to flush its own queue. So simply run
  216. * it by hand rather than deadlocking.
  217. */
  218. run_workqueue(cwq);
  219. } else {
  220. DEFINE_WAIT(wait);
  221. long sequence_needed;
  222. spin_lock_irq(&cwq->lock);
  223. sequence_needed = cwq->insert_sequence;
  224. while (sequence_needed - cwq->remove_sequence > 0) {
  225. prepare_to_wait(&cwq->work_done, &wait,
  226. TASK_UNINTERRUPTIBLE);
  227. spin_unlock_irq(&cwq->lock);
  228. schedule();
  229. spin_lock_irq(&cwq->lock);
  230. }
  231. finish_wait(&cwq->work_done, &wait);
  232. spin_unlock_irq(&cwq->lock);
  233. }
  234. }
  235. /*
  236. * flush_workqueue - ensure that any scheduled work has run to completion.
  237. *
  238. * Forces execution of the workqueue and blocks until its completion.
  239. * This is typically used in driver shutdown handlers.
  240. *
  241. * This function will sample each workqueue's current insert_sequence number and
  242. * will sleep until the head sequence is greater than or equal to that. This
  243. * means that we sleep until all works which were queued on entry have been
  244. * handled, but we are not livelocked by new incoming ones.
  245. *
  246. * This function used to run the workqueues itself. Now we just wait for the
  247. * helper threads to do it.
  248. */
  249. void fastcall flush_workqueue(struct workqueue_struct *wq)
  250. {
  251. might_sleep();
  252. if (is_single_threaded(wq)) {
  253. /* Always use first cpu's area. */
  254. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
  255. } else {
  256. int cpu;
  257. lock_cpu_hotplug();
  258. for_each_online_cpu(cpu)
  259. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  260. unlock_cpu_hotplug();
  261. }
  262. }
  263. static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
  264. int cpu)
  265. {
  266. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  267. struct task_struct *p;
  268. spin_lock_init(&cwq->lock);
  269. cwq->wq = wq;
  270. cwq->thread = NULL;
  271. cwq->insert_sequence = 0;
  272. cwq->remove_sequence = 0;
  273. INIT_LIST_HEAD(&cwq->worklist);
  274. init_waitqueue_head(&cwq->more_work);
  275. init_waitqueue_head(&cwq->work_done);
  276. if (is_single_threaded(wq))
  277. p = kthread_create(worker_thread, cwq, "%s", wq->name);
  278. else
  279. p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
  280. if (IS_ERR(p))
  281. return NULL;
  282. cwq->thread = p;
  283. return p;
  284. }
  285. struct workqueue_struct *__create_workqueue(const char *name,
  286. int singlethread)
  287. {
  288. int cpu, destroy = 0;
  289. struct workqueue_struct *wq;
  290. struct task_struct *p;
  291. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  292. if (!wq)
  293. return NULL;
  294. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  295. if (!wq->cpu_wq) {
  296. kfree(wq);
  297. return NULL;
  298. }
  299. wq->name = name;
  300. /* We don't need the distraction of CPUs appearing and vanishing. */
  301. lock_cpu_hotplug();
  302. if (singlethread) {
  303. INIT_LIST_HEAD(&wq->list);
  304. p = create_workqueue_thread(wq, singlethread_cpu);
  305. if (!p)
  306. destroy = 1;
  307. else
  308. wake_up_process(p);
  309. } else {
  310. spin_lock(&workqueue_lock);
  311. list_add(&wq->list, &workqueues);
  312. spin_unlock(&workqueue_lock);
  313. for_each_online_cpu(cpu) {
  314. p = create_workqueue_thread(wq, cpu);
  315. if (p) {
  316. kthread_bind(p, cpu);
  317. wake_up_process(p);
  318. } else
  319. destroy = 1;
  320. }
  321. }
  322. unlock_cpu_hotplug();
  323. /*
  324. * Was there any error during startup? If yes then clean up:
  325. */
  326. if (destroy) {
  327. destroy_workqueue(wq);
  328. wq = NULL;
  329. }
  330. return wq;
  331. }
  332. static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
  333. {
  334. struct cpu_workqueue_struct *cwq;
  335. unsigned long flags;
  336. struct task_struct *p;
  337. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  338. spin_lock_irqsave(&cwq->lock, flags);
  339. p = cwq->thread;
  340. cwq->thread = NULL;
  341. spin_unlock_irqrestore(&cwq->lock, flags);
  342. if (p)
  343. kthread_stop(p);
  344. }
  345. void destroy_workqueue(struct workqueue_struct *wq)
  346. {
  347. int cpu;
  348. flush_workqueue(wq);
  349. /* We don't need the distraction of CPUs appearing and vanishing. */
  350. lock_cpu_hotplug();
  351. if (is_single_threaded(wq))
  352. cleanup_workqueue_thread(wq, singlethread_cpu);
  353. else {
  354. for_each_online_cpu(cpu)
  355. cleanup_workqueue_thread(wq, cpu);
  356. spin_lock(&workqueue_lock);
  357. list_del(&wq->list);
  358. spin_unlock(&workqueue_lock);
  359. }
  360. unlock_cpu_hotplug();
  361. free_percpu(wq->cpu_wq);
  362. kfree(wq);
  363. }
  364. static struct workqueue_struct *keventd_wq;
  365. int fastcall schedule_work(struct work_struct *work)
  366. {
  367. return queue_work(keventd_wq, work);
  368. }
  369. int fastcall schedule_delayed_work(struct work_struct *work, unsigned long delay)
  370. {
  371. return queue_delayed_work(keventd_wq, work, delay);
  372. }
  373. int schedule_delayed_work_on(int cpu,
  374. struct work_struct *work, unsigned long delay)
  375. {
  376. return queue_delayed_work_on(cpu, keventd_wq, work, delay);
  377. }
  378. /**
  379. * schedule_on_each_cpu - call a function on each online CPU from keventd
  380. * @func: the function to call
  381. * @info: a pointer to pass to func()
  382. *
  383. * Returns zero on success.
  384. * Returns -ve errno on failure.
  385. *
  386. * Appears to be racy against CPU hotplug.
  387. *
  388. * schedule_on_each_cpu() is very slow.
  389. */
  390. int schedule_on_each_cpu(void (*func)(void *info), void *info)
  391. {
  392. int cpu;
  393. struct work_struct *works;
  394. works = alloc_percpu(struct work_struct);
  395. if (!works)
  396. return -ENOMEM;
  397. for_each_online_cpu(cpu) {
  398. INIT_WORK(per_cpu_ptr(works, cpu), func, info);
  399. __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu),
  400. per_cpu_ptr(works, cpu));
  401. }
  402. flush_workqueue(keventd_wq);
  403. free_percpu(works);
  404. return 0;
  405. }
  406. void flush_scheduled_work(void)
  407. {
  408. flush_workqueue(keventd_wq);
  409. }
  410. /**
  411. * cancel_rearming_delayed_workqueue - reliably kill off a delayed
  412. * work whose handler rearms the delayed work.
  413. * @wq: the controlling workqueue structure
  414. * @work: the delayed work struct
  415. */
  416. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  417. struct work_struct *work)
  418. {
  419. while (!cancel_delayed_work(work))
  420. flush_workqueue(wq);
  421. }
  422. EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
  423. /**
  424. * cancel_rearming_delayed_work - reliably kill off a delayed keventd
  425. * work whose handler rearms the delayed work.
  426. * @work: the delayed work struct
  427. */
  428. void cancel_rearming_delayed_work(struct work_struct *work)
  429. {
  430. cancel_rearming_delayed_workqueue(keventd_wq, work);
  431. }
  432. EXPORT_SYMBOL(cancel_rearming_delayed_work);
  433. /**
  434. * execute_in_process_context - reliably execute the routine with user context
  435. * @fn: the function to execute
  436. * @data: data to pass to the function
  437. * @ew: guaranteed storage for the execute work structure (must
  438. * be available when the work executes)
  439. *
  440. * Executes the function immediately if process context is available,
  441. * otherwise schedules the function for delayed execution.
  442. *
  443. * Returns: 0 - function was executed
  444. * 1 - function was scheduled for execution
  445. */
  446. int execute_in_process_context(void (*fn)(void *data), void *data,
  447. struct execute_work *ew)
  448. {
  449. if (!in_interrupt()) {
  450. fn(data);
  451. return 0;
  452. }
  453. INIT_WORK(&ew->work, fn, data);
  454. schedule_work(&ew->work);
  455. return 1;
  456. }
  457. EXPORT_SYMBOL_GPL(execute_in_process_context);
  458. int keventd_up(void)
  459. {
  460. return keventd_wq != NULL;
  461. }
  462. int current_is_keventd(void)
  463. {
  464. struct cpu_workqueue_struct *cwq;
  465. int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  466. int ret = 0;
  467. BUG_ON(!keventd_wq);
  468. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  469. if (current == cwq->thread)
  470. ret = 1;
  471. return ret;
  472. }
  473. #ifdef CONFIG_HOTPLUG_CPU
  474. /* Take the work from this (downed) CPU. */
  475. static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
  476. {
  477. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  478. struct list_head list;
  479. struct work_struct *work;
  480. spin_lock_irq(&cwq->lock);
  481. list_replace_init(&cwq->worklist, &list);
  482. while (!list_empty(&list)) {
  483. printk("Taking work for %s\n", wq->name);
  484. work = list_entry(list.next,struct work_struct,entry);
  485. list_del(&work->entry);
  486. __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
  487. }
  488. spin_unlock_irq(&cwq->lock);
  489. }
  490. /* We're holding the cpucontrol mutex here */
  491. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  492. unsigned long action,
  493. void *hcpu)
  494. {
  495. unsigned int hotcpu = (unsigned long)hcpu;
  496. struct workqueue_struct *wq;
  497. switch (action) {
  498. case CPU_UP_PREPARE:
  499. /* Create a new workqueue thread for it. */
  500. list_for_each_entry(wq, &workqueues, list) {
  501. if (!create_workqueue_thread(wq, hotcpu)) {
  502. printk("workqueue for %i failed\n", hotcpu);
  503. return NOTIFY_BAD;
  504. }
  505. }
  506. break;
  507. case CPU_ONLINE:
  508. /* Kick off worker threads. */
  509. list_for_each_entry(wq, &workqueues, list) {
  510. struct cpu_workqueue_struct *cwq;
  511. cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
  512. kthread_bind(cwq->thread, hotcpu);
  513. wake_up_process(cwq->thread);
  514. }
  515. break;
  516. case CPU_UP_CANCELED:
  517. list_for_each_entry(wq, &workqueues, list) {
  518. if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
  519. continue;
  520. /* Unbind so it can run. */
  521. kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
  522. any_online_cpu(cpu_online_map));
  523. cleanup_workqueue_thread(wq, hotcpu);
  524. }
  525. break;
  526. case CPU_DEAD:
  527. list_for_each_entry(wq, &workqueues, list)
  528. cleanup_workqueue_thread(wq, hotcpu);
  529. list_for_each_entry(wq, &workqueues, list)
  530. take_over_work(wq, hotcpu);
  531. break;
  532. }
  533. return NOTIFY_OK;
  534. }
  535. #endif
  536. void init_workqueues(void)
  537. {
  538. singlethread_cpu = first_cpu(cpu_possible_map);
  539. hotcpu_notifier(workqueue_cpu_callback, 0);
  540. keventd_wq = create_workqueue("events");
  541. BUG_ON(!keventd_wq);
  542. }
  543. EXPORT_SYMBOL_GPL(__create_workqueue);
  544. EXPORT_SYMBOL_GPL(queue_work);
  545. EXPORT_SYMBOL_GPL(queue_delayed_work);
  546. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  547. EXPORT_SYMBOL_GPL(flush_workqueue);
  548. EXPORT_SYMBOL_GPL(destroy_workqueue);
  549. EXPORT_SYMBOL(schedule_work);
  550. EXPORT_SYMBOL(schedule_delayed_work);
  551. EXPORT_SYMBOL(schedule_delayed_work_on);
  552. EXPORT_SYMBOL(flush_scheduled_work);