workqueue.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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
  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.
  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. #include <linux/lockdep.h>
  35. /*
  36. * The per-CPU workqueue (if single thread, we always use the first
  37. * possible cpu).
  38. */
  39. struct cpu_workqueue_struct {
  40. spinlock_t lock;
  41. struct list_head worklist;
  42. wait_queue_head_t more_work;
  43. struct work_struct *current_work;
  44. struct workqueue_struct *wq;
  45. struct task_struct *thread;
  46. } ____cacheline_aligned;
  47. /*
  48. * The externally visible workqueue abstraction is an array of
  49. * per-CPU workqueues:
  50. */
  51. struct workqueue_struct {
  52. struct cpu_workqueue_struct *cpu_wq;
  53. struct list_head list;
  54. const char *name;
  55. int singlethread;
  56. int freezeable; /* Freeze threads during suspend */
  57. int rt;
  58. #ifdef CONFIG_LOCKDEP
  59. struct lockdep_map lockdep_map;
  60. #endif
  61. };
  62. /* Serializes the accesses to the list of workqueues. */
  63. static DEFINE_SPINLOCK(workqueue_lock);
  64. static LIST_HEAD(workqueues);
  65. static int singlethread_cpu __read_mostly;
  66. static const struct cpumask *cpu_singlethread_map __read_mostly;
  67. /*
  68. * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
  69. * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
  70. * which comes in between can't use for_each_online_cpu(). We could
  71. * use cpu_possible_map, the cpumask below is more a documentation
  72. * than optimization.
  73. */
  74. static cpumask_var_t cpu_populated_map __read_mostly;
  75. /* If it's single threaded, it isn't in the list of workqueues. */
  76. static inline int is_wq_single_threaded(struct workqueue_struct *wq)
  77. {
  78. return wq->singlethread;
  79. }
  80. static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq)
  81. {
  82. return is_wq_single_threaded(wq)
  83. ? cpu_singlethread_map : cpu_populated_map;
  84. }
  85. static
  86. struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
  87. {
  88. if (unlikely(is_wq_single_threaded(wq)))
  89. cpu = singlethread_cpu;
  90. return per_cpu_ptr(wq->cpu_wq, cpu);
  91. }
  92. /*
  93. * Set the workqueue on which a work item is to be run
  94. * - Must *only* be called if the pending flag is set
  95. */
  96. static inline void set_wq_data(struct work_struct *work,
  97. struct cpu_workqueue_struct *cwq)
  98. {
  99. unsigned long new;
  100. BUG_ON(!work_pending(work));
  101. new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
  102. new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
  103. atomic_long_set(&work->data, new);
  104. }
  105. static inline
  106. struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
  107. {
  108. return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
  109. }
  110. static void insert_work(struct cpu_workqueue_struct *cwq,
  111. struct work_struct *work, struct list_head *head)
  112. {
  113. set_wq_data(work, cwq);
  114. /*
  115. * Ensure that we get the right work->data if we see the
  116. * result of list_add() below, see try_to_grab_pending().
  117. */
  118. smp_wmb();
  119. list_add_tail(&work->entry, head);
  120. wake_up(&cwq->more_work);
  121. }
  122. static void __queue_work(struct cpu_workqueue_struct *cwq,
  123. struct work_struct *work)
  124. {
  125. unsigned long flags;
  126. spin_lock_irqsave(&cwq->lock, flags);
  127. insert_work(cwq, work, &cwq->worklist);
  128. spin_unlock_irqrestore(&cwq->lock, flags);
  129. }
  130. /**
  131. * queue_work - queue work on a workqueue
  132. * @wq: workqueue to use
  133. * @work: work to queue
  134. *
  135. * Returns 0 if @work was already on a queue, non-zero otherwise.
  136. *
  137. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  138. * it can be processed by another CPU.
  139. */
  140. int queue_work(struct workqueue_struct *wq, struct work_struct *work)
  141. {
  142. int ret;
  143. ret = queue_work_on(get_cpu(), wq, work);
  144. put_cpu();
  145. return ret;
  146. }
  147. EXPORT_SYMBOL_GPL(queue_work);
  148. /**
  149. * queue_work_on - queue work on specific cpu
  150. * @cpu: CPU number to execute work on
  151. * @wq: workqueue to use
  152. * @work: work to queue
  153. *
  154. * Returns 0 if @work was already on a queue, non-zero otherwise.
  155. *
  156. * We queue the work to a specific CPU, the caller must ensure it
  157. * can't go away.
  158. */
  159. int
  160. queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
  161. {
  162. int ret = 0;
  163. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  164. BUG_ON(!list_empty(&work->entry));
  165. __queue_work(wq_per_cpu(wq, cpu), work);
  166. ret = 1;
  167. }
  168. return ret;
  169. }
  170. EXPORT_SYMBOL_GPL(queue_work_on);
  171. static void delayed_work_timer_fn(unsigned long __data)
  172. {
  173. struct delayed_work *dwork = (struct delayed_work *)__data;
  174. struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
  175. struct workqueue_struct *wq = cwq->wq;
  176. __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
  177. }
  178. /**
  179. * queue_delayed_work - queue work on a workqueue after delay
  180. * @wq: workqueue to use
  181. * @dwork: delayable work to queue
  182. * @delay: number of jiffies to wait before queueing
  183. *
  184. * Returns 0 if @work was already on a queue, non-zero otherwise.
  185. */
  186. int queue_delayed_work(struct workqueue_struct *wq,
  187. struct delayed_work *dwork, unsigned long delay)
  188. {
  189. if (delay == 0)
  190. return queue_work(wq, &dwork->work);
  191. return queue_delayed_work_on(-1, wq, dwork, delay);
  192. }
  193. EXPORT_SYMBOL_GPL(queue_delayed_work);
  194. /**
  195. * queue_delayed_work_on - queue work on specific CPU after delay
  196. * @cpu: CPU number to execute work on
  197. * @wq: workqueue to use
  198. * @dwork: work to queue
  199. * @delay: number of jiffies to wait before queueing
  200. *
  201. * Returns 0 if @work was already on a queue, non-zero otherwise.
  202. */
  203. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  204. struct delayed_work *dwork, unsigned long delay)
  205. {
  206. int ret = 0;
  207. struct timer_list *timer = &dwork->timer;
  208. struct work_struct *work = &dwork->work;
  209. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  210. BUG_ON(timer_pending(timer));
  211. BUG_ON(!list_empty(&work->entry));
  212. timer_stats_timer_set_start_info(&dwork->timer);
  213. /* This stores cwq for the moment, for the timer_fn */
  214. set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
  215. timer->expires = jiffies + delay;
  216. timer->data = (unsigned long)dwork;
  217. timer->function = delayed_work_timer_fn;
  218. if (unlikely(cpu >= 0))
  219. add_timer_on(timer, cpu);
  220. else
  221. add_timer(timer);
  222. ret = 1;
  223. }
  224. return ret;
  225. }
  226. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  227. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  228. {
  229. spin_lock_irq(&cwq->lock);
  230. while (!list_empty(&cwq->worklist)) {
  231. struct work_struct *work = list_entry(cwq->worklist.next,
  232. struct work_struct, entry);
  233. work_func_t f = work->func;
  234. #ifdef CONFIG_LOCKDEP
  235. /*
  236. * It is permissible to free the struct work_struct
  237. * from inside the function that is called from it,
  238. * this we need to take into account for lockdep too.
  239. * To avoid bogus "held lock freed" warnings as well
  240. * as problems when looking into work->lockdep_map,
  241. * make a copy and use that here.
  242. */
  243. struct lockdep_map lockdep_map = work->lockdep_map;
  244. #endif
  245. cwq->current_work = work;
  246. list_del_init(cwq->worklist.next);
  247. spin_unlock_irq(&cwq->lock);
  248. BUG_ON(get_wq_data(work) != cwq);
  249. work_clear_pending(work);
  250. lock_map_acquire(&cwq->wq->lockdep_map);
  251. lock_map_acquire(&lockdep_map);
  252. f(work);
  253. lock_map_release(&lockdep_map);
  254. lock_map_release(&cwq->wq->lockdep_map);
  255. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  256. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  257. "%s/0x%08x/%d\n",
  258. current->comm, preempt_count(),
  259. task_pid_nr(current));
  260. printk(KERN_ERR " last function: ");
  261. print_symbol("%s\n", (unsigned long)f);
  262. debug_show_held_locks(current);
  263. dump_stack();
  264. }
  265. spin_lock_irq(&cwq->lock);
  266. cwq->current_work = NULL;
  267. }
  268. spin_unlock_irq(&cwq->lock);
  269. }
  270. static int worker_thread(void *__cwq)
  271. {
  272. struct cpu_workqueue_struct *cwq = __cwq;
  273. DEFINE_WAIT(wait);
  274. if (cwq->wq->freezeable)
  275. set_freezable();
  276. set_user_nice(current, -5);
  277. for (;;) {
  278. prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
  279. if (!freezing(current) &&
  280. !kthread_should_stop() &&
  281. list_empty(&cwq->worklist))
  282. schedule();
  283. finish_wait(&cwq->more_work, &wait);
  284. try_to_freeze();
  285. if (kthread_should_stop())
  286. break;
  287. run_workqueue(cwq);
  288. }
  289. return 0;
  290. }
  291. struct wq_barrier {
  292. struct work_struct work;
  293. struct completion done;
  294. };
  295. static void wq_barrier_func(struct work_struct *work)
  296. {
  297. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  298. complete(&barr->done);
  299. }
  300. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  301. struct wq_barrier *barr, struct list_head *head)
  302. {
  303. INIT_WORK(&barr->work, wq_barrier_func);
  304. __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
  305. init_completion(&barr->done);
  306. insert_work(cwq, &barr->work, head);
  307. }
  308. static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  309. {
  310. int active = 0;
  311. struct wq_barrier barr;
  312. WARN_ON(cwq->thread == current);
  313. spin_lock_irq(&cwq->lock);
  314. if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
  315. insert_wq_barrier(cwq, &barr, &cwq->worklist);
  316. active = 1;
  317. }
  318. spin_unlock_irq(&cwq->lock);
  319. if (active)
  320. wait_for_completion(&barr.done);
  321. return active;
  322. }
  323. /**
  324. * flush_workqueue - ensure that any scheduled work has run to completion.
  325. * @wq: workqueue to flush
  326. *
  327. * Forces execution of the workqueue and blocks until its completion.
  328. * This is typically used in driver shutdown handlers.
  329. *
  330. * We sleep until all works which were queued on entry have been handled,
  331. * but we are not livelocked by new incoming ones.
  332. *
  333. * This function used to run the workqueues itself. Now we just wait for the
  334. * helper threads to do it.
  335. */
  336. void flush_workqueue(struct workqueue_struct *wq)
  337. {
  338. const struct cpumask *cpu_map = wq_cpu_map(wq);
  339. int cpu;
  340. might_sleep();
  341. lock_map_acquire(&wq->lockdep_map);
  342. lock_map_release(&wq->lockdep_map);
  343. for_each_cpu(cpu, cpu_map)
  344. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  345. }
  346. EXPORT_SYMBOL_GPL(flush_workqueue);
  347. /**
  348. * flush_work - block until a work_struct's callback has terminated
  349. * @work: the work which is to be flushed
  350. *
  351. * Returns false if @work has already terminated.
  352. *
  353. * It is expected that, prior to calling flush_work(), the caller has
  354. * arranged for the work to not be requeued, otherwise it doesn't make
  355. * sense to use this function.
  356. */
  357. int flush_work(struct work_struct *work)
  358. {
  359. struct cpu_workqueue_struct *cwq;
  360. struct list_head *prev;
  361. struct wq_barrier barr;
  362. might_sleep();
  363. cwq = get_wq_data(work);
  364. if (!cwq)
  365. return 0;
  366. lock_map_acquire(&cwq->wq->lockdep_map);
  367. lock_map_release(&cwq->wq->lockdep_map);
  368. prev = NULL;
  369. spin_lock_irq(&cwq->lock);
  370. if (!list_empty(&work->entry)) {
  371. /*
  372. * See the comment near try_to_grab_pending()->smp_rmb().
  373. * If it was re-queued under us we are not going to wait.
  374. */
  375. smp_rmb();
  376. if (unlikely(cwq != get_wq_data(work)))
  377. goto out;
  378. prev = &work->entry;
  379. } else {
  380. if (cwq->current_work != work)
  381. goto out;
  382. prev = &cwq->worklist;
  383. }
  384. insert_wq_barrier(cwq, &barr, prev->next);
  385. out:
  386. spin_unlock_irq(&cwq->lock);
  387. if (!prev)
  388. return 0;
  389. wait_for_completion(&barr.done);
  390. return 1;
  391. }
  392. EXPORT_SYMBOL_GPL(flush_work);
  393. /*
  394. * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
  395. * so this work can't be re-armed in any way.
  396. */
  397. static int try_to_grab_pending(struct work_struct *work)
  398. {
  399. struct cpu_workqueue_struct *cwq;
  400. int ret = -1;
  401. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
  402. return 0;
  403. /*
  404. * The queueing is in progress, or it is already queued. Try to
  405. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  406. */
  407. cwq = get_wq_data(work);
  408. if (!cwq)
  409. return ret;
  410. spin_lock_irq(&cwq->lock);
  411. if (!list_empty(&work->entry)) {
  412. /*
  413. * This work is queued, but perhaps we locked the wrong cwq.
  414. * In that case we must see the new value after rmb(), see
  415. * insert_work()->wmb().
  416. */
  417. smp_rmb();
  418. if (cwq == get_wq_data(work)) {
  419. list_del_init(&work->entry);
  420. ret = 1;
  421. }
  422. }
  423. spin_unlock_irq(&cwq->lock);
  424. return ret;
  425. }
  426. static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
  427. struct work_struct *work)
  428. {
  429. struct wq_barrier barr;
  430. int running = 0;
  431. spin_lock_irq(&cwq->lock);
  432. if (unlikely(cwq->current_work == work)) {
  433. insert_wq_barrier(cwq, &barr, cwq->worklist.next);
  434. running = 1;
  435. }
  436. spin_unlock_irq(&cwq->lock);
  437. if (unlikely(running))
  438. wait_for_completion(&barr.done);
  439. }
  440. static void wait_on_work(struct work_struct *work)
  441. {
  442. struct cpu_workqueue_struct *cwq;
  443. struct workqueue_struct *wq;
  444. const struct cpumask *cpu_map;
  445. int cpu;
  446. might_sleep();
  447. lock_map_acquire(&work->lockdep_map);
  448. lock_map_release(&work->lockdep_map);
  449. cwq = get_wq_data(work);
  450. if (!cwq)
  451. return;
  452. wq = cwq->wq;
  453. cpu_map = wq_cpu_map(wq);
  454. for_each_cpu(cpu, cpu_map)
  455. wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  456. }
  457. static int __cancel_work_timer(struct work_struct *work,
  458. struct timer_list* timer)
  459. {
  460. int ret;
  461. do {
  462. ret = (timer && likely(del_timer(timer)));
  463. if (!ret)
  464. ret = try_to_grab_pending(work);
  465. wait_on_work(work);
  466. } while (unlikely(ret < 0));
  467. work_clear_pending(work);
  468. return ret;
  469. }
  470. /**
  471. * cancel_work_sync - block until a work_struct's callback has terminated
  472. * @work: the work which is to be flushed
  473. *
  474. * Returns true if @work was pending.
  475. *
  476. * cancel_work_sync() will cancel the work if it is queued. If the work's
  477. * callback appears to be running, cancel_work_sync() will block until it
  478. * has completed.
  479. *
  480. * It is possible to use this function if the work re-queues itself. It can
  481. * cancel the work even if it migrates to another workqueue, however in that
  482. * case it only guarantees that work->func() has completed on the last queued
  483. * workqueue.
  484. *
  485. * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
  486. * pending, otherwise it goes into a busy-wait loop until the timer expires.
  487. *
  488. * The caller must ensure that workqueue_struct on which this work was last
  489. * queued can't be destroyed before this function returns.
  490. */
  491. int cancel_work_sync(struct work_struct *work)
  492. {
  493. return __cancel_work_timer(work, NULL);
  494. }
  495. EXPORT_SYMBOL_GPL(cancel_work_sync);
  496. /**
  497. * cancel_delayed_work_sync - reliably kill off a delayed work.
  498. * @dwork: the delayed work struct
  499. *
  500. * Returns true if @dwork was pending.
  501. *
  502. * It is possible to use this function if @dwork rearms itself via queue_work()
  503. * or queue_delayed_work(). See also the comment for cancel_work_sync().
  504. */
  505. int cancel_delayed_work_sync(struct delayed_work *dwork)
  506. {
  507. return __cancel_work_timer(&dwork->work, &dwork->timer);
  508. }
  509. EXPORT_SYMBOL(cancel_delayed_work_sync);
  510. static struct workqueue_struct *keventd_wq __read_mostly;
  511. /**
  512. * schedule_work - put work task in global workqueue
  513. * @work: job to be done
  514. *
  515. * This puts a job in the kernel-global workqueue.
  516. */
  517. int schedule_work(struct work_struct *work)
  518. {
  519. return queue_work(keventd_wq, work);
  520. }
  521. EXPORT_SYMBOL(schedule_work);
  522. /*
  523. * schedule_work_on - put work task on a specific cpu
  524. * @cpu: cpu to put the work task on
  525. * @work: job to be done
  526. *
  527. * This puts a job on a specific cpu
  528. */
  529. int schedule_work_on(int cpu, struct work_struct *work)
  530. {
  531. return queue_work_on(cpu, keventd_wq, work);
  532. }
  533. EXPORT_SYMBOL(schedule_work_on);
  534. /**
  535. * schedule_delayed_work - put work task in global workqueue after delay
  536. * @dwork: job to be done
  537. * @delay: number of jiffies to wait or 0 for immediate execution
  538. *
  539. * After waiting for a given time this puts a job in the kernel-global
  540. * workqueue.
  541. */
  542. int schedule_delayed_work(struct delayed_work *dwork,
  543. unsigned long delay)
  544. {
  545. return queue_delayed_work(keventd_wq, dwork, delay);
  546. }
  547. EXPORT_SYMBOL(schedule_delayed_work);
  548. /**
  549. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  550. * @cpu: cpu to use
  551. * @dwork: job to be done
  552. * @delay: number of jiffies to wait
  553. *
  554. * After waiting for a given time this puts a job in the kernel-global
  555. * workqueue on the specified CPU.
  556. */
  557. int schedule_delayed_work_on(int cpu,
  558. struct delayed_work *dwork, unsigned long delay)
  559. {
  560. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  561. }
  562. EXPORT_SYMBOL(schedule_delayed_work_on);
  563. /**
  564. * schedule_on_each_cpu - call a function on each online CPU from keventd
  565. * @func: the function to call
  566. *
  567. * Returns zero on success.
  568. * Returns -ve errno on failure.
  569. *
  570. * schedule_on_each_cpu() is very slow.
  571. */
  572. int schedule_on_each_cpu(work_func_t func)
  573. {
  574. int cpu;
  575. struct work_struct *works;
  576. works = alloc_percpu(struct work_struct);
  577. if (!works)
  578. return -ENOMEM;
  579. get_online_cpus();
  580. for_each_online_cpu(cpu) {
  581. struct work_struct *work = per_cpu_ptr(works, cpu);
  582. INIT_WORK(work, func);
  583. schedule_work_on(cpu, work);
  584. }
  585. for_each_online_cpu(cpu)
  586. flush_work(per_cpu_ptr(works, cpu));
  587. put_online_cpus();
  588. free_percpu(works);
  589. return 0;
  590. }
  591. void flush_scheduled_work(void)
  592. {
  593. flush_workqueue(keventd_wq);
  594. }
  595. EXPORT_SYMBOL(flush_scheduled_work);
  596. /**
  597. * execute_in_process_context - reliably execute the routine with user context
  598. * @fn: the function to execute
  599. * @ew: guaranteed storage for the execute work structure (must
  600. * be available when the work executes)
  601. *
  602. * Executes the function immediately if process context is available,
  603. * otherwise schedules the function for delayed execution.
  604. *
  605. * Returns: 0 - function was executed
  606. * 1 - function was scheduled for execution
  607. */
  608. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  609. {
  610. if (!in_interrupt()) {
  611. fn(&ew->work);
  612. return 0;
  613. }
  614. INIT_WORK(&ew->work, fn);
  615. schedule_work(&ew->work);
  616. return 1;
  617. }
  618. EXPORT_SYMBOL_GPL(execute_in_process_context);
  619. int keventd_up(void)
  620. {
  621. return keventd_wq != NULL;
  622. }
  623. int current_is_keventd(void)
  624. {
  625. struct cpu_workqueue_struct *cwq;
  626. int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  627. int ret = 0;
  628. BUG_ON(!keventd_wq);
  629. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  630. if (current == cwq->thread)
  631. ret = 1;
  632. return ret;
  633. }
  634. static struct cpu_workqueue_struct *
  635. init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
  636. {
  637. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  638. cwq->wq = wq;
  639. spin_lock_init(&cwq->lock);
  640. INIT_LIST_HEAD(&cwq->worklist);
  641. init_waitqueue_head(&cwq->more_work);
  642. return cwq;
  643. }
  644. static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  645. {
  646. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  647. struct workqueue_struct *wq = cwq->wq;
  648. const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
  649. struct task_struct *p;
  650. p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
  651. /*
  652. * Nobody can add the work_struct to this cwq,
  653. * if (caller is __create_workqueue)
  654. * nobody should see this wq
  655. * else // caller is CPU_UP_PREPARE
  656. * cpu is not on cpu_online_map
  657. * so we can abort safely.
  658. */
  659. if (IS_ERR(p))
  660. return PTR_ERR(p);
  661. if (cwq->wq->rt)
  662. sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
  663. cwq->thread = p;
  664. return 0;
  665. }
  666. static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  667. {
  668. struct task_struct *p = cwq->thread;
  669. if (p != NULL) {
  670. if (cpu >= 0)
  671. kthread_bind(p, cpu);
  672. wake_up_process(p);
  673. }
  674. }
  675. struct workqueue_struct *__create_workqueue_key(const char *name,
  676. int singlethread,
  677. int freezeable,
  678. int rt,
  679. struct lock_class_key *key,
  680. const char *lock_name)
  681. {
  682. struct workqueue_struct *wq;
  683. struct cpu_workqueue_struct *cwq;
  684. int err = 0, cpu;
  685. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  686. if (!wq)
  687. return NULL;
  688. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  689. if (!wq->cpu_wq) {
  690. kfree(wq);
  691. return NULL;
  692. }
  693. wq->name = name;
  694. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  695. wq->singlethread = singlethread;
  696. wq->freezeable = freezeable;
  697. wq->rt = rt;
  698. INIT_LIST_HEAD(&wq->list);
  699. if (singlethread) {
  700. cwq = init_cpu_workqueue(wq, singlethread_cpu);
  701. err = create_workqueue_thread(cwq, singlethread_cpu);
  702. start_workqueue_thread(cwq, -1);
  703. } else {
  704. cpu_maps_update_begin();
  705. /*
  706. * We must place this wq on list even if the code below fails.
  707. * cpu_down(cpu) can remove cpu from cpu_populated_map before
  708. * destroy_workqueue() takes the lock, in that case we leak
  709. * cwq[cpu]->thread.
  710. */
  711. spin_lock(&workqueue_lock);
  712. list_add(&wq->list, &workqueues);
  713. spin_unlock(&workqueue_lock);
  714. /*
  715. * We must initialize cwqs for each possible cpu even if we
  716. * are going to call destroy_workqueue() finally. Otherwise
  717. * cpu_up() can hit the uninitialized cwq once we drop the
  718. * lock.
  719. */
  720. for_each_possible_cpu(cpu) {
  721. cwq = init_cpu_workqueue(wq, cpu);
  722. if (err || !cpu_online(cpu))
  723. continue;
  724. err = create_workqueue_thread(cwq, cpu);
  725. start_workqueue_thread(cwq, cpu);
  726. }
  727. cpu_maps_update_done();
  728. }
  729. if (err) {
  730. destroy_workqueue(wq);
  731. wq = NULL;
  732. }
  733. return wq;
  734. }
  735. EXPORT_SYMBOL_GPL(__create_workqueue_key);
  736. static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
  737. {
  738. /*
  739. * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
  740. * cpu_add_remove_lock protects cwq->thread.
  741. */
  742. if (cwq->thread == NULL)
  743. return;
  744. lock_map_acquire(&cwq->wq->lockdep_map);
  745. lock_map_release(&cwq->wq->lockdep_map);
  746. flush_cpu_workqueue(cwq);
  747. /*
  748. * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
  749. * a concurrent flush_workqueue() can insert a barrier after us.
  750. * However, in that case run_workqueue() won't return and check
  751. * kthread_should_stop() until it flushes all work_struct's.
  752. * When ->worklist becomes empty it is safe to exit because no
  753. * more work_structs can be queued on this cwq: flush_workqueue
  754. * checks list_empty(), and a "normal" queue_work() can't use
  755. * a dead CPU.
  756. */
  757. kthread_stop(cwq->thread);
  758. cwq->thread = NULL;
  759. }
  760. /**
  761. * destroy_workqueue - safely terminate a workqueue
  762. * @wq: target workqueue
  763. *
  764. * Safely destroy a workqueue. All work currently pending will be done first.
  765. */
  766. void destroy_workqueue(struct workqueue_struct *wq)
  767. {
  768. const struct cpumask *cpu_map = wq_cpu_map(wq);
  769. int cpu;
  770. cpu_maps_update_begin();
  771. spin_lock(&workqueue_lock);
  772. list_del(&wq->list);
  773. spin_unlock(&workqueue_lock);
  774. for_each_cpu(cpu, cpu_map)
  775. cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
  776. cpu_maps_update_done();
  777. free_percpu(wq->cpu_wq);
  778. kfree(wq);
  779. }
  780. EXPORT_SYMBOL_GPL(destroy_workqueue);
  781. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  782. unsigned long action,
  783. void *hcpu)
  784. {
  785. unsigned int cpu = (unsigned long)hcpu;
  786. struct cpu_workqueue_struct *cwq;
  787. struct workqueue_struct *wq;
  788. int ret = NOTIFY_OK;
  789. action &= ~CPU_TASKS_FROZEN;
  790. switch (action) {
  791. case CPU_UP_PREPARE:
  792. cpumask_set_cpu(cpu, cpu_populated_map);
  793. }
  794. undo:
  795. list_for_each_entry(wq, &workqueues, list) {
  796. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  797. switch (action) {
  798. case CPU_UP_PREPARE:
  799. if (!create_workqueue_thread(cwq, cpu))
  800. break;
  801. printk(KERN_ERR "workqueue [%s] for %i failed\n",
  802. wq->name, cpu);
  803. action = CPU_UP_CANCELED;
  804. ret = NOTIFY_BAD;
  805. goto undo;
  806. case CPU_ONLINE:
  807. start_workqueue_thread(cwq, cpu);
  808. break;
  809. case CPU_UP_CANCELED:
  810. start_workqueue_thread(cwq, -1);
  811. case CPU_POST_DEAD:
  812. cleanup_workqueue_thread(cwq);
  813. break;
  814. }
  815. }
  816. switch (action) {
  817. case CPU_UP_CANCELED:
  818. case CPU_POST_DEAD:
  819. cpumask_clear_cpu(cpu, cpu_populated_map);
  820. }
  821. return ret;
  822. }
  823. #ifdef CONFIG_SMP
  824. static struct workqueue_struct *work_on_cpu_wq __read_mostly;
  825. struct work_for_cpu {
  826. struct work_struct work;
  827. long (*fn)(void *);
  828. void *arg;
  829. long ret;
  830. };
  831. static void do_work_for_cpu(struct work_struct *w)
  832. {
  833. struct work_for_cpu *wfc = container_of(w, struct work_for_cpu, work);
  834. wfc->ret = wfc->fn(wfc->arg);
  835. }
  836. /**
  837. * work_on_cpu - run a function in user context on a particular cpu
  838. * @cpu: the cpu to run on
  839. * @fn: the function to run
  840. * @arg: the function arg
  841. *
  842. * This will return the value @fn returns.
  843. * It is up to the caller to ensure that the cpu doesn't go offline.
  844. */
  845. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  846. {
  847. struct work_for_cpu wfc;
  848. INIT_WORK(&wfc.work, do_work_for_cpu);
  849. wfc.fn = fn;
  850. wfc.arg = arg;
  851. queue_work_on(cpu, work_on_cpu_wq, &wfc.work);
  852. flush_work(&wfc.work);
  853. return wfc.ret;
  854. }
  855. EXPORT_SYMBOL_GPL(work_on_cpu);
  856. #endif /* CONFIG_SMP */
  857. void __init init_workqueues(void)
  858. {
  859. alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
  860. cpumask_copy(cpu_populated_map, cpu_online_mask);
  861. singlethread_cpu = cpumask_first(cpu_possible_mask);
  862. cpu_singlethread_map = cpumask_of(singlethread_cpu);
  863. hotcpu_notifier(workqueue_cpu_callback, 0);
  864. keventd_wq = create_workqueue("events");
  865. BUG_ON(!keventd_wq);
  866. #ifdef CONFIG_SMP
  867. work_on_cpu_wq = create_workqueue("work_on_cpu");
  868. BUG_ON(!work_on_cpu_wq);
  869. #endif
  870. }