workqueue.c 25 KB

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