workqueue.c 30 KB

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