workqueue.c 25 KB

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