workqueue.c 16 KB

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