workqueue.c 31 KB

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