workqueue.c 38 KB

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