workqueue.c 30 KB

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