workqueue.c 30 KB

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