workqueue.c 15 KB

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