workqueue.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  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. #include <linux/idr.h>
  36. enum {
  37. /* global_cwq flags */
  38. GCWQ_FREEZING = 1 << 3, /* freeze in progress */
  39. /* worker flags */
  40. WORKER_STARTED = 1 << 0, /* started */
  41. WORKER_DIE = 1 << 1, /* die die die */
  42. WORKER_IDLE = 1 << 2, /* is idle */
  43. WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
  44. /* gcwq->trustee_state */
  45. TRUSTEE_START = 0, /* start */
  46. TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
  47. TRUSTEE_BUTCHER = 2, /* butcher workers */
  48. TRUSTEE_RELEASE = 3, /* release workers */
  49. TRUSTEE_DONE = 4, /* trustee is done */
  50. BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
  51. BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
  52. BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
  53. TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
  54. };
  55. /*
  56. * Structure fields follow one of the following exclusion rules.
  57. *
  58. * I: Set during initialization and read-only afterwards.
  59. *
  60. * L: gcwq->lock protected. Access with gcwq->lock held.
  61. *
  62. * F: wq->flush_mutex protected.
  63. *
  64. * W: workqueue_lock protected.
  65. */
  66. struct global_cwq;
  67. struct cpu_workqueue_struct;
  68. struct worker {
  69. /* on idle list while idle, on busy hash table while busy */
  70. union {
  71. struct list_head entry; /* L: while idle */
  72. struct hlist_node hentry; /* L: while busy */
  73. };
  74. struct work_struct *current_work; /* L: work being processed */
  75. struct list_head scheduled; /* L: scheduled works */
  76. struct task_struct *task; /* I: worker task */
  77. struct global_cwq *gcwq; /* I: the associated gcwq */
  78. struct cpu_workqueue_struct *cwq; /* I: the associated cwq */
  79. unsigned int flags; /* L: flags */
  80. int id; /* I: worker id */
  81. };
  82. /*
  83. * Global per-cpu workqueue.
  84. */
  85. struct global_cwq {
  86. spinlock_t lock; /* the gcwq lock */
  87. unsigned int cpu; /* I: the associated cpu */
  88. unsigned int flags; /* L: GCWQ_* flags */
  89. int nr_workers; /* L: total number of workers */
  90. int nr_idle; /* L: currently idle ones */
  91. /* workers are chained either in the idle_list or busy_hash */
  92. struct list_head idle_list; /* L: list of idle workers */
  93. struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
  94. /* L: hash of busy workers */
  95. struct ida worker_ida; /* L: for worker IDs */
  96. struct task_struct *trustee; /* L: for gcwq shutdown */
  97. unsigned int trustee_state; /* L: trustee state */
  98. wait_queue_head_t trustee_wait; /* trustee wait */
  99. } ____cacheline_aligned_in_smp;
  100. /*
  101. * The per-CPU workqueue (if single thread, we always use the first
  102. * possible cpu). The lower WORK_STRUCT_FLAG_BITS of
  103. * work_struct->data are used for flags and thus cwqs need to be
  104. * aligned at two's power of the number of flag bits.
  105. */
  106. struct cpu_workqueue_struct {
  107. struct global_cwq *gcwq; /* I: the associated gcwq */
  108. struct list_head worklist;
  109. struct worker *worker;
  110. struct workqueue_struct *wq; /* I: the owning workqueue */
  111. int work_color; /* L: current color */
  112. int flush_color; /* L: flushing color */
  113. int nr_in_flight[WORK_NR_COLORS];
  114. /* L: nr of in_flight works */
  115. int nr_active; /* L: nr of active works */
  116. int max_active; /* L: max active works */
  117. struct list_head delayed_works; /* L: delayed works */
  118. };
  119. /*
  120. * Structure used to wait for workqueue flush.
  121. */
  122. struct wq_flusher {
  123. struct list_head list; /* F: list of flushers */
  124. int flush_color; /* F: flush color waiting for */
  125. struct completion done; /* flush completion */
  126. };
  127. /*
  128. * The externally visible workqueue abstraction is an array of
  129. * per-CPU workqueues:
  130. */
  131. struct workqueue_struct {
  132. unsigned int flags; /* I: WQ_* flags */
  133. struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */
  134. struct list_head list; /* W: list of all workqueues */
  135. struct mutex flush_mutex; /* protects wq flushing */
  136. int work_color; /* F: current work color */
  137. int flush_color; /* F: current flush color */
  138. atomic_t nr_cwqs_to_flush; /* flush in progress */
  139. struct wq_flusher *first_flusher; /* F: first flusher */
  140. struct list_head flusher_queue; /* F: flush waiters */
  141. struct list_head flusher_overflow; /* F: flush overflow list */
  142. int saved_max_active; /* I: saved cwq max_active */
  143. const char *name; /* I: workqueue name */
  144. #ifdef CONFIG_LOCKDEP
  145. struct lockdep_map lockdep_map;
  146. #endif
  147. };
  148. #define for_each_busy_worker(worker, i, pos, gcwq) \
  149. for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
  150. hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
  151. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  152. static struct debug_obj_descr work_debug_descr;
  153. /*
  154. * fixup_init is called when:
  155. * - an active object is initialized
  156. */
  157. static int work_fixup_init(void *addr, enum debug_obj_state state)
  158. {
  159. struct work_struct *work = addr;
  160. switch (state) {
  161. case ODEBUG_STATE_ACTIVE:
  162. cancel_work_sync(work);
  163. debug_object_init(work, &work_debug_descr);
  164. return 1;
  165. default:
  166. return 0;
  167. }
  168. }
  169. /*
  170. * fixup_activate is called when:
  171. * - an active object is activated
  172. * - an unknown object is activated (might be a statically initialized object)
  173. */
  174. static int work_fixup_activate(void *addr, enum debug_obj_state state)
  175. {
  176. struct work_struct *work = addr;
  177. switch (state) {
  178. case ODEBUG_STATE_NOTAVAILABLE:
  179. /*
  180. * This is not really a fixup. The work struct was
  181. * statically initialized. We just make sure that it
  182. * is tracked in the object tracker.
  183. */
  184. if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
  185. debug_object_init(work, &work_debug_descr);
  186. debug_object_activate(work, &work_debug_descr);
  187. return 0;
  188. }
  189. WARN_ON_ONCE(1);
  190. return 0;
  191. case ODEBUG_STATE_ACTIVE:
  192. WARN_ON(1);
  193. default:
  194. return 0;
  195. }
  196. }
  197. /*
  198. * fixup_free is called when:
  199. * - an active object is freed
  200. */
  201. static int work_fixup_free(void *addr, enum debug_obj_state state)
  202. {
  203. struct work_struct *work = addr;
  204. switch (state) {
  205. case ODEBUG_STATE_ACTIVE:
  206. cancel_work_sync(work);
  207. debug_object_free(work, &work_debug_descr);
  208. return 1;
  209. default:
  210. return 0;
  211. }
  212. }
  213. static struct debug_obj_descr work_debug_descr = {
  214. .name = "work_struct",
  215. .fixup_init = work_fixup_init,
  216. .fixup_activate = work_fixup_activate,
  217. .fixup_free = work_fixup_free,
  218. };
  219. static inline void debug_work_activate(struct work_struct *work)
  220. {
  221. debug_object_activate(work, &work_debug_descr);
  222. }
  223. static inline void debug_work_deactivate(struct work_struct *work)
  224. {
  225. debug_object_deactivate(work, &work_debug_descr);
  226. }
  227. void __init_work(struct work_struct *work, int onstack)
  228. {
  229. if (onstack)
  230. debug_object_init_on_stack(work, &work_debug_descr);
  231. else
  232. debug_object_init(work, &work_debug_descr);
  233. }
  234. EXPORT_SYMBOL_GPL(__init_work);
  235. void destroy_work_on_stack(struct work_struct *work)
  236. {
  237. debug_object_free(work, &work_debug_descr);
  238. }
  239. EXPORT_SYMBOL_GPL(destroy_work_on_stack);
  240. #else
  241. static inline void debug_work_activate(struct work_struct *work) { }
  242. static inline void debug_work_deactivate(struct work_struct *work) { }
  243. #endif
  244. /* Serializes the accesses to the list of workqueues. */
  245. static DEFINE_SPINLOCK(workqueue_lock);
  246. static LIST_HEAD(workqueues);
  247. static bool workqueue_freezing; /* W: have wqs started freezing? */
  248. static DEFINE_PER_CPU(struct global_cwq, global_cwq);
  249. static int worker_thread(void *__worker);
  250. static int singlethread_cpu __read_mostly;
  251. static struct global_cwq *get_gcwq(unsigned int cpu)
  252. {
  253. return &per_cpu(global_cwq, cpu);
  254. }
  255. static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
  256. struct workqueue_struct *wq)
  257. {
  258. return per_cpu_ptr(wq->cpu_wq, cpu);
  259. }
  260. static struct cpu_workqueue_struct *target_cwq(unsigned int cpu,
  261. struct workqueue_struct *wq)
  262. {
  263. if (unlikely(wq->flags & WQ_SINGLE_THREAD))
  264. cpu = singlethread_cpu;
  265. return get_cwq(cpu, wq);
  266. }
  267. static unsigned int work_color_to_flags(int color)
  268. {
  269. return color << WORK_STRUCT_COLOR_SHIFT;
  270. }
  271. static int get_work_color(struct work_struct *work)
  272. {
  273. return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
  274. ((1 << WORK_STRUCT_COLOR_BITS) - 1);
  275. }
  276. static int work_next_color(int color)
  277. {
  278. return (color + 1) % WORK_NR_COLORS;
  279. }
  280. /*
  281. * Set the workqueue on which a work item is to be run
  282. * - Must *only* be called if the pending flag is set
  283. */
  284. static inline void set_wq_data(struct work_struct *work,
  285. struct cpu_workqueue_struct *cwq,
  286. unsigned long extra_flags)
  287. {
  288. BUG_ON(!work_pending(work));
  289. atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
  290. WORK_STRUCT_PENDING | extra_flags);
  291. }
  292. /*
  293. * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
  294. */
  295. static inline void clear_wq_data(struct work_struct *work)
  296. {
  297. atomic_long_set(&work->data, work_static(work));
  298. }
  299. static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
  300. {
  301. return (void *)(atomic_long_read(&work->data) &
  302. WORK_STRUCT_WQ_DATA_MASK);
  303. }
  304. /**
  305. * busy_worker_head - return the busy hash head for a work
  306. * @gcwq: gcwq of interest
  307. * @work: work to be hashed
  308. *
  309. * Return hash head of @gcwq for @work.
  310. *
  311. * CONTEXT:
  312. * spin_lock_irq(gcwq->lock).
  313. *
  314. * RETURNS:
  315. * Pointer to the hash head.
  316. */
  317. static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
  318. struct work_struct *work)
  319. {
  320. const int base_shift = ilog2(sizeof(struct work_struct));
  321. unsigned long v = (unsigned long)work;
  322. /* simple shift and fold hash, do we need something better? */
  323. v >>= base_shift;
  324. v += v >> BUSY_WORKER_HASH_ORDER;
  325. v &= BUSY_WORKER_HASH_MASK;
  326. return &gcwq->busy_hash[v];
  327. }
  328. /**
  329. * insert_work - insert a work into cwq
  330. * @cwq: cwq @work belongs to
  331. * @work: work to insert
  332. * @head: insertion point
  333. * @extra_flags: extra WORK_STRUCT_* flags to set
  334. *
  335. * Insert @work into @cwq after @head.
  336. *
  337. * CONTEXT:
  338. * spin_lock_irq(gcwq->lock).
  339. */
  340. static void insert_work(struct cpu_workqueue_struct *cwq,
  341. struct work_struct *work, struct list_head *head,
  342. unsigned int extra_flags)
  343. {
  344. /* we own @work, set data and link */
  345. set_wq_data(work, cwq, extra_flags);
  346. /*
  347. * Ensure that we get the right work->data if we see the
  348. * result of list_add() below, see try_to_grab_pending().
  349. */
  350. smp_wmb();
  351. list_add_tail(&work->entry, head);
  352. wake_up_process(cwq->worker->task);
  353. }
  354. static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
  355. struct work_struct *work)
  356. {
  357. struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq);
  358. struct global_cwq *gcwq = cwq->gcwq;
  359. struct list_head *worklist;
  360. unsigned long flags;
  361. debug_work_activate(work);
  362. spin_lock_irqsave(&gcwq->lock, flags);
  363. BUG_ON(!list_empty(&work->entry));
  364. cwq->nr_in_flight[cwq->work_color]++;
  365. if (likely(cwq->nr_active < cwq->max_active)) {
  366. cwq->nr_active++;
  367. worklist = &cwq->worklist;
  368. } else
  369. worklist = &cwq->delayed_works;
  370. insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color));
  371. spin_unlock_irqrestore(&gcwq->lock, flags);
  372. }
  373. /**
  374. * queue_work - queue work on a workqueue
  375. * @wq: workqueue to use
  376. * @work: work to queue
  377. *
  378. * Returns 0 if @work was already on a queue, non-zero otherwise.
  379. *
  380. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  381. * it can be processed by another CPU.
  382. */
  383. int queue_work(struct workqueue_struct *wq, struct work_struct *work)
  384. {
  385. int ret;
  386. ret = queue_work_on(get_cpu(), wq, work);
  387. put_cpu();
  388. return ret;
  389. }
  390. EXPORT_SYMBOL_GPL(queue_work);
  391. /**
  392. * queue_work_on - queue work on specific cpu
  393. * @cpu: CPU number to execute work on
  394. * @wq: workqueue to use
  395. * @work: work to queue
  396. *
  397. * Returns 0 if @work was already on a queue, non-zero otherwise.
  398. *
  399. * We queue the work to a specific CPU, the caller must ensure it
  400. * can't go away.
  401. */
  402. int
  403. queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
  404. {
  405. int ret = 0;
  406. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  407. __queue_work(cpu, wq, work);
  408. ret = 1;
  409. }
  410. return ret;
  411. }
  412. EXPORT_SYMBOL_GPL(queue_work_on);
  413. static void delayed_work_timer_fn(unsigned long __data)
  414. {
  415. struct delayed_work *dwork = (struct delayed_work *)__data;
  416. struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
  417. __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
  418. }
  419. /**
  420. * queue_delayed_work - queue work on a workqueue after delay
  421. * @wq: workqueue to use
  422. * @dwork: delayable work to queue
  423. * @delay: number of jiffies to wait before queueing
  424. *
  425. * Returns 0 if @work was already on a queue, non-zero otherwise.
  426. */
  427. int queue_delayed_work(struct workqueue_struct *wq,
  428. struct delayed_work *dwork, unsigned long delay)
  429. {
  430. if (delay == 0)
  431. return queue_work(wq, &dwork->work);
  432. return queue_delayed_work_on(-1, wq, dwork, delay);
  433. }
  434. EXPORT_SYMBOL_GPL(queue_delayed_work);
  435. /**
  436. * queue_delayed_work_on - queue work on specific CPU after delay
  437. * @cpu: CPU number to execute work on
  438. * @wq: workqueue to use
  439. * @dwork: work to queue
  440. * @delay: number of jiffies to wait before queueing
  441. *
  442. * Returns 0 if @work was already on a queue, non-zero otherwise.
  443. */
  444. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  445. struct delayed_work *dwork, unsigned long delay)
  446. {
  447. int ret = 0;
  448. struct timer_list *timer = &dwork->timer;
  449. struct work_struct *work = &dwork->work;
  450. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  451. BUG_ON(timer_pending(timer));
  452. BUG_ON(!list_empty(&work->entry));
  453. timer_stats_timer_set_start_info(&dwork->timer);
  454. /* This stores cwq for the moment, for the timer_fn */
  455. set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0);
  456. timer->expires = jiffies + delay;
  457. timer->data = (unsigned long)dwork;
  458. timer->function = delayed_work_timer_fn;
  459. if (unlikely(cpu >= 0))
  460. add_timer_on(timer, cpu);
  461. else
  462. add_timer(timer);
  463. ret = 1;
  464. }
  465. return ret;
  466. }
  467. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  468. /**
  469. * worker_enter_idle - enter idle state
  470. * @worker: worker which is entering idle state
  471. *
  472. * @worker is entering idle state. Update stats and idle timer if
  473. * necessary.
  474. *
  475. * LOCKING:
  476. * spin_lock_irq(gcwq->lock).
  477. */
  478. static void worker_enter_idle(struct worker *worker)
  479. {
  480. struct global_cwq *gcwq = worker->gcwq;
  481. BUG_ON(worker->flags & WORKER_IDLE);
  482. BUG_ON(!list_empty(&worker->entry) &&
  483. (worker->hentry.next || worker->hentry.pprev));
  484. worker->flags |= WORKER_IDLE;
  485. gcwq->nr_idle++;
  486. /* idle_list is LIFO */
  487. list_add(&worker->entry, &gcwq->idle_list);
  488. if (unlikely(worker->flags & WORKER_ROGUE))
  489. wake_up_all(&gcwq->trustee_wait);
  490. }
  491. /**
  492. * worker_leave_idle - leave idle state
  493. * @worker: worker which is leaving idle state
  494. *
  495. * @worker is leaving idle state. Update stats.
  496. *
  497. * LOCKING:
  498. * spin_lock_irq(gcwq->lock).
  499. */
  500. static void worker_leave_idle(struct worker *worker)
  501. {
  502. struct global_cwq *gcwq = worker->gcwq;
  503. BUG_ON(!(worker->flags & WORKER_IDLE));
  504. worker->flags &= ~WORKER_IDLE;
  505. gcwq->nr_idle--;
  506. list_del_init(&worker->entry);
  507. }
  508. static struct worker *alloc_worker(void)
  509. {
  510. struct worker *worker;
  511. worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  512. if (worker) {
  513. INIT_LIST_HEAD(&worker->entry);
  514. INIT_LIST_HEAD(&worker->scheduled);
  515. }
  516. return worker;
  517. }
  518. /**
  519. * create_worker - create a new workqueue worker
  520. * @cwq: cwq the new worker will belong to
  521. * @bind: whether to set affinity to @cpu or not
  522. *
  523. * Create a new worker which is bound to @cwq. The returned worker
  524. * can be started by calling start_worker() or destroyed using
  525. * destroy_worker().
  526. *
  527. * CONTEXT:
  528. * Might sleep. Does GFP_KERNEL allocations.
  529. *
  530. * RETURNS:
  531. * Pointer to the newly created worker.
  532. */
  533. static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
  534. {
  535. struct global_cwq *gcwq = cwq->gcwq;
  536. int id = -1;
  537. struct worker *worker = NULL;
  538. spin_lock_irq(&gcwq->lock);
  539. while (ida_get_new(&gcwq->worker_ida, &id)) {
  540. spin_unlock_irq(&gcwq->lock);
  541. if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
  542. goto fail;
  543. spin_lock_irq(&gcwq->lock);
  544. }
  545. spin_unlock_irq(&gcwq->lock);
  546. worker = alloc_worker();
  547. if (!worker)
  548. goto fail;
  549. worker->gcwq = gcwq;
  550. worker->cwq = cwq;
  551. worker->id = id;
  552. worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
  553. gcwq->cpu, id);
  554. if (IS_ERR(worker->task))
  555. goto fail;
  556. /*
  557. * A rogue worker will become a regular one if CPU comes
  558. * online later on. Make sure every worker has
  559. * PF_THREAD_BOUND set.
  560. */
  561. if (bind)
  562. kthread_bind(worker->task, gcwq->cpu);
  563. else
  564. worker->task->flags |= PF_THREAD_BOUND;
  565. return worker;
  566. fail:
  567. if (id >= 0) {
  568. spin_lock_irq(&gcwq->lock);
  569. ida_remove(&gcwq->worker_ida, id);
  570. spin_unlock_irq(&gcwq->lock);
  571. }
  572. kfree(worker);
  573. return NULL;
  574. }
  575. /**
  576. * start_worker - start a newly created worker
  577. * @worker: worker to start
  578. *
  579. * Make the gcwq aware of @worker and start it.
  580. *
  581. * CONTEXT:
  582. * spin_lock_irq(gcwq->lock).
  583. */
  584. static void start_worker(struct worker *worker)
  585. {
  586. worker->flags |= WORKER_STARTED;
  587. worker->gcwq->nr_workers++;
  588. worker_enter_idle(worker);
  589. wake_up_process(worker->task);
  590. }
  591. /**
  592. * destroy_worker - destroy a workqueue worker
  593. * @worker: worker to be destroyed
  594. *
  595. * Destroy @worker and adjust @gcwq stats accordingly.
  596. *
  597. * CONTEXT:
  598. * spin_lock_irq(gcwq->lock) which is released and regrabbed.
  599. */
  600. static void destroy_worker(struct worker *worker)
  601. {
  602. struct global_cwq *gcwq = worker->gcwq;
  603. int id = worker->id;
  604. /* sanity check frenzy */
  605. BUG_ON(worker->current_work);
  606. BUG_ON(!list_empty(&worker->scheduled));
  607. if (worker->flags & WORKER_STARTED)
  608. gcwq->nr_workers--;
  609. if (worker->flags & WORKER_IDLE)
  610. gcwq->nr_idle--;
  611. list_del_init(&worker->entry);
  612. worker->flags |= WORKER_DIE;
  613. spin_unlock_irq(&gcwq->lock);
  614. kthread_stop(worker->task);
  615. kfree(worker);
  616. spin_lock_irq(&gcwq->lock);
  617. ida_remove(&gcwq->worker_ida, id);
  618. }
  619. /**
  620. * move_linked_works - move linked works to a list
  621. * @work: start of series of works to be scheduled
  622. * @head: target list to append @work to
  623. * @nextp: out paramter for nested worklist walking
  624. *
  625. * Schedule linked works starting from @work to @head. Work series to
  626. * be scheduled starts at @work and includes any consecutive work with
  627. * WORK_STRUCT_LINKED set in its predecessor.
  628. *
  629. * If @nextp is not NULL, it's updated to point to the next work of
  630. * the last scheduled work. This allows move_linked_works() to be
  631. * nested inside outer list_for_each_entry_safe().
  632. *
  633. * CONTEXT:
  634. * spin_lock_irq(gcwq->lock).
  635. */
  636. static void move_linked_works(struct work_struct *work, struct list_head *head,
  637. struct work_struct **nextp)
  638. {
  639. struct work_struct *n;
  640. /*
  641. * Linked worklist will always end before the end of the list,
  642. * use NULL for list head.
  643. */
  644. list_for_each_entry_safe_from(work, n, NULL, entry) {
  645. list_move_tail(&work->entry, head);
  646. if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
  647. break;
  648. }
  649. /*
  650. * If we're already inside safe list traversal and have moved
  651. * multiple works to the scheduled queue, the next position
  652. * needs to be updated.
  653. */
  654. if (nextp)
  655. *nextp = n;
  656. }
  657. static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
  658. {
  659. struct work_struct *work = list_first_entry(&cwq->delayed_works,
  660. struct work_struct, entry);
  661. move_linked_works(work, &cwq->worklist, NULL);
  662. cwq->nr_active++;
  663. }
  664. /**
  665. * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
  666. * @cwq: cwq of interest
  667. * @color: color of work which left the queue
  668. *
  669. * A work either has completed or is removed from pending queue,
  670. * decrement nr_in_flight of its cwq and handle workqueue flushing.
  671. *
  672. * CONTEXT:
  673. * spin_lock_irq(gcwq->lock).
  674. */
  675. static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
  676. {
  677. /* ignore uncolored works */
  678. if (color == WORK_NO_COLOR)
  679. return;
  680. cwq->nr_in_flight[color]--;
  681. cwq->nr_active--;
  682. /* one down, submit a delayed one */
  683. if (!list_empty(&cwq->delayed_works) &&
  684. cwq->nr_active < cwq->max_active)
  685. cwq_activate_first_delayed(cwq);
  686. /* is flush in progress and are we at the flushing tip? */
  687. if (likely(cwq->flush_color != color))
  688. return;
  689. /* are there still in-flight works? */
  690. if (cwq->nr_in_flight[color])
  691. return;
  692. /* this cwq is done, clear flush_color */
  693. cwq->flush_color = -1;
  694. /*
  695. * If this was the last cwq, wake up the first flusher. It
  696. * will handle the rest.
  697. */
  698. if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
  699. complete(&cwq->wq->first_flusher->done);
  700. }
  701. /**
  702. * process_one_work - process single work
  703. * @worker: self
  704. * @work: work to process
  705. *
  706. * Process @work. This function contains all the logics necessary to
  707. * process a single work including synchronization against and
  708. * interaction with other workers on the same cpu, queueing and
  709. * flushing. As long as context requirement is met, any worker can
  710. * call this function to process a work.
  711. *
  712. * CONTEXT:
  713. * spin_lock_irq(gcwq->lock) which is released and regrabbed.
  714. */
  715. static void process_one_work(struct worker *worker, struct work_struct *work)
  716. {
  717. struct cpu_workqueue_struct *cwq = worker->cwq;
  718. struct global_cwq *gcwq = cwq->gcwq;
  719. struct hlist_head *bwh = busy_worker_head(gcwq, work);
  720. work_func_t f = work->func;
  721. int work_color;
  722. #ifdef CONFIG_LOCKDEP
  723. /*
  724. * It is permissible to free the struct work_struct from
  725. * inside the function that is called from it, this we need to
  726. * take into account for lockdep too. To avoid bogus "held
  727. * lock freed" warnings as well as problems when looking into
  728. * work->lockdep_map, make a copy and use that here.
  729. */
  730. struct lockdep_map lockdep_map = work->lockdep_map;
  731. #endif
  732. /* claim and process */
  733. debug_work_deactivate(work);
  734. hlist_add_head(&worker->hentry, bwh);
  735. worker->current_work = work;
  736. work_color = get_work_color(work);
  737. list_del_init(&work->entry);
  738. spin_unlock_irq(&gcwq->lock);
  739. BUG_ON(get_wq_data(work) != cwq);
  740. work_clear_pending(work);
  741. lock_map_acquire(&cwq->wq->lockdep_map);
  742. lock_map_acquire(&lockdep_map);
  743. f(work);
  744. lock_map_release(&lockdep_map);
  745. lock_map_release(&cwq->wq->lockdep_map);
  746. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  747. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  748. "%s/0x%08x/%d\n",
  749. current->comm, preempt_count(), task_pid_nr(current));
  750. printk(KERN_ERR " last function: ");
  751. print_symbol("%s\n", (unsigned long)f);
  752. debug_show_held_locks(current);
  753. dump_stack();
  754. }
  755. spin_lock_irq(&gcwq->lock);
  756. /* we're done with it, release */
  757. hlist_del_init(&worker->hentry);
  758. worker->current_work = NULL;
  759. cwq_dec_nr_in_flight(cwq, work_color);
  760. }
  761. /**
  762. * process_scheduled_works - process scheduled works
  763. * @worker: self
  764. *
  765. * Process all scheduled works. Please note that the scheduled list
  766. * may change while processing a work, so this function repeatedly
  767. * fetches a work from the top and executes it.
  768. *
  769. * CONTEXT:
  770. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  771. * multiple times.
  772. */
  773. static void process_scheduled_works(struct worker *worker)
  774. {
  775. while (!list_empty(&worker->scheduled)) {
  776. struct work_struct *work = list_first_entry(&worker->scheduled,
  777. struct work_struct, entry);
  778. process_one_work(worker, work);
  779. }
  780. }
  781. /**
  782. * worker_thread - the worker thread function
  783. * @__worker: self
  784. *
  785. * The cwq worker thread function.
  786. */
  787. static int worker_thread(void *__worker)
  788. {
  789. struct worker *worker = __worker;
  790. struct global_cwq *gcwq = worker->gcwq;
  791. struct cpu_workqueue_struct *cwq = worker->cwq;
  792. woke_up:
  793. spin_lock_irq(&gcwq->lock);
  794. /* DIE can be set only while we're idle, checking here is enough */
  795. if (worker->flags & WORKER_DIE) {
  796. spin_unlock_irq(&gcwq->lock);
  797. return 0;
  798. }
  799. worker_leave_idle(worker);
  800. recheck:
  801. /*
  802. * ->scheduled list can only be filled while a worker is
  803. * preparing to process a work or actually processing it.
  804. * Make sure nobody diddled with it while I was sleeping.
  805. */
  806. BUG_ON(!list_empty(&worker->scheduled));
  807. while (!list_empty(&cwq->worklist)) {
  808. struct work_struct *work =
  809. list_first_entry(&cwq->worklist,
  810. struct work_struct, entry);
  811. /*
  812. * The following is a rather inefficient way to close
  813. * race window against cpu hotplug operations. Will
  814. * be replaced soon.
  815. */
  816. if (unlikely(!(worker->flags & WORKER_ROGUE) &&
  817. !cpumask_equal(&worker->task->cpus_allowed,
  818. get_cpu_mask(gcwq->cpu)))) {
  819. spin_unlock_irq(&gcwq->lock);
  820. set_cpus_allowed_ptr(worker->task,
  821. get_cpu_mask(gcwq->cpu));
  822. cpu_relax();
  823. spin_lock_irq(&gcwq->lock);
  824. goto recheck;
  825. }
  826. if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
  827. /* optimization path, not strictly necessary */
  828. process_one_work(worker, work);
  829. if (unlikely(!list_empty(&worker->scheduled)))
  830. process_scheduled_works(worker);
  831. } else {
  832. move_linked_works(work, &worker->scheduled, NULL);
  833. process_scheduled_works(worker);
  834. }
  835. }
  836. /*
  837. * gcwq->lock is held and there's no work to process, sleep.
  838. * Workers are woken up only while holding gcwq->lock, so
  839. * setting the current state before releasing gcwq->lock is
  840. * enough to prevent losing any event.
  841. */
  842. worker_enter_idle(worker);
  843. __set_current_state(TASK_INTERRUPTIBLE);
  844. spin_unlock_irq(&gcwq->lock);
  845. schedule();
  846. goto woke_up;
  847. }
  848. struct wq_barrier {
  849. struct work_struct work;
  850. struct completion done;
  851. };
  852. static void wq_barrier_func(struct work_struct *work)
  853. {
  854. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  855. complete(&barr->done);
  856. }
  857. /**
  858. * insert_wq_barrier - insert a barrier work
  859. * @cwq: cwq to insert barrier into
  860. * @barr: wq_barrier to insert
  861. * @target: target work to attach @barr to
  862. * @worker: worker currently executing @target, NULL if @target is not executing
  863. *
  864. * @barr is linked to @target such that @barr is completed only after
  865. * @target finishes execution. Please note that the ordering
  866. * guarantee is observed only with respect to @target and on the local
  867. * cpu.
  868. *
  869. * Currently, a queued barrier can't be canceled. This is because
  870. * try_to_grab_pending() can't determine whether the work to be
  871. * grabbed is at the head of the queue and thus can't clear LINKED
  872. * flag of the previous work while there must be a valid next work
  873. * after a work with LINKED flag set.
  874. *
  875. * Note that when @worker is non-NULL, @target may be modified
  876. * underneath us, so we can't reliably determine cwq from @target.
  877. *
  878. * CONTEXT:
  879. * spin_lock_irq(gcwq->lock).
  880. */
  881. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  882. struct wq_barrier *barr,
  883. struct work_struct *target, struct worker *worker)
  884. {
  885. struct list_head *head;
  886. unsigned int linked = 0;
  887. /*
  888. * debugobject calls are safe here even with gcwq->lock locked
  889. * as we know for sure that this will not trigger any of the
  890. * checks and call back into the fixup functions where we
  891. * might deadlock.
  892. */
  893. INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
  894. __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
  895. init_completion(&barr->done);
  896. /*
  897. * If @target is currently being executed, schedule the
  898. * barrier to the worker; otherwise, put it after @target.
  899. */
  900. if (worker)
  901. head = worker->scheduled.next;
  902. else {
  903. unsigned long *bits = work_data_bits(target);
  904. head = target->entry.next;
  905. /* there can already be other linked works, inherit and set */
  906. linked = *bits & WORK_STRUCT_LINKED;
  907. __set_bit(WORK_STRUCT_LINKED_BIT, bits);
  908. }
  909. debug_work_activate(&barr->work);
  910. insert_work(cwq, &barr->work, head,
  911. work_color_to_flags(WORK_NO_COLOR) | linked);
  912. }
  913. /**
  914. * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
  915. * @wq: workqueue being flushed
  916. * @flush_color: new flush color, < 0 for no-op
  917. * @work_color: new work color, < 0 for no-op
  918. *
  919. * Prepare cwqs for workqueue flushing.
  920. *
  921. * If @flush_color is non-negative, flush_color on all cwqs should be
  922. * -1. If no cwq has in-flight commands at the specified color, all
  923. * cwq->flush_color's stay at -1 and %false is returned. If any cwq
  924. * has in flight commands, its cwq->flush_color is set to
  925. * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
  926. * wakeup logic is armed and %true is returned.
  927. *
  928. * The caller should have initialized @wq->first_flusher prior to
  929. * calling this function with non-negative @flush_color. If
  930. * @flush_color is negative, no flush color update is done and %false
  931. * is returned.
  932. *
  933. * If @work_color is non-negative, all cwqs should have the same
  934. * work_color which is previous to @work_color and all will be
  935. * advanced to @work_color.
  936. *
  937. * CONTEXT:
  938. * mutex_lock(wq->flush_mutex).
  939. *
  940. * RETURNS:
  941. * %true if @flush_color >= 0 and there's something to flush. %false
  942. * otherwise.
  943. */
  944. static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
  945. int flush_color, int work_color)
  946. {
  947. bool wait = false;
  948. unsigned int cpu;
  949. if (flush_color >= 0) {
  950. BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
  951. atomic_set(&wq->nr_cwqs_to_flush, 1);
  952. }
  953. for_each_possible_cpu(cpu) {
  954. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  955. struct global_cwq *gcwq = cwq->gcwq;
  956. spin_lock_irq(&gcwq->lock);
  957. if (flush_color >= 0) {
  958. BUG_ON(cwq->flush_color != -1);
  959. if (cwq->nr_in_flight[flush_color]) {
  960. cwq->flush_color = flush_color;
  961. atomic_inc(&wq->nr_cwqs_to_flush);
  962. wait = true;
  963. }
  964. }
  965. if (work_color >= 0) {
  966. BUG_ON(work_color != work_next_color(cwq->work_color));
  967. cwq->work_color = work_color;
  968. }
  969. spin_unlock_irq(&gcwq->lock);
  970. }
  971. if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
  972. complete(&wq->first_flusher->done);
  973. return wait;
  974. }
  975. /**
  976. * flush_workqueue - ensure that any scheduled work has run to completion.
  977. * @wq: workqueue to flush
  978. *
  979. * Forces execution of the workqueue and blocks until its completion.
  980. * This is typically used in driver shutdown handlers.
  981. *
  982. * We sleep until all works which were queued on entry have been handled,
  983. * but we are not livelocked by new incoming ones.
  984. */
  985. void flush_workqueue(struct workqueue_struct *wq)
  986. {
  987. struct wq_flusher this_flusher = {
  988. .list = LIST_HEAD_INIT(this_flusher.list),
  989. .flush_color = -1,
  990. .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
  991. };
  992. int next_color;
  993. lock_map_acquire(&wq->lockdep_map);
  994. lock_map_release(&wq->lockdep_map);
  995. mutex_lock(&wq->flush_mutex);
  996. /*
  997. * Start-to-wait phase
  998. */
  999. next_color = work_next_color(wq->work_color);
  1000. if (next_color != wq->flush_color) {
  1001. /*
  1002. * Color space is not full. The current work_color
  1003. * becomes our flush_color and work_color is advanced
  1004. * by one.
  1005. */
  1006. BUG_ON(!list_empty(&wq->flusher_overflow));
  1007. this_flusher.flush_color = wq->work_color;
  1008. wq->work_color = next_color;
  1009. if (!wq->first_flusher) {
  1010. /* no flush in progress, become the first flusher */
  1011. BUG_ON(wq->flush_color != this_flusher.flush_color);
  1012. wq->first_flusher = &this_flusher;
  1013. if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
  1014. wq->work_color)) {
  1015. /* nothing to flush, done */
  1016. wq->flush_color = next_color;
  1017. wq->first_flusher = NULL;
  1018. goto out_unlock;
  1019. }
  1020. } else {
  1021. /* wait in queue */
  1022. BUG_ON(wq->flush_color == this_flusher.flush_color);
  1023. list_add_tail(&this_flusher.list, &wq->flusher_queue);
  1024. flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
  1025. }
  1026. } else {
  1027. /*
  1028. * Oops, color space is full, wait on overflow queue.
  1029. * The next flush completion will assign us
  1030. * flush_color and transfer to flusher_queue.
  1031. */
  1032. list_add_tail(&this_flusher.list, &wq->flusher_overflow);
  1033. }
  1034. mutex_unlock(&wq->flush_mutex);
  1035. wait_for_completion(&this_flusher.done);
  1036. /*
  1037. * Wake-up-and-cascade phase
  1038. *
  1039. * First flushers are responsible for cascading flushes and
  1040. * handling overflow. Non-first flushers can simply return.
  1041. */
  1042. if (wq->first_flusher != &this_flusher)
  1043. return;
  1044. mutex_lock(&wq->flush_mutex);
  1045. wq->first_flusher = NULL;
  1046. BUG_ON(!list_empty(&this_flusher.list));
  1047. BUG_ON(wq->flush_color != this_flusher.flush_color);
  1048. while (true) {
  1049. struct wq_flusher *next, *tmp;
  1050. /* complete all the flushers sharing the current flush color */
  1051. list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
  1052. if (next->flush_color != wq->flush_color)
  1053. break;
  1054. list_del_init(&next->list);
  1055. complete(&next->done);
  1056. }
  1057. BUG_ON(!list_empty(&wq->flusher_overflow) &&
  1058. wq->flush_color != work_next_color(wq->work_color));
  1059. /* this flush_color is finished, advance by one */
  1060. wq->flush_color = work_next_color(wq->flush_color);
  1061. /* one color has been freed, handle overflow queue */
  1062. if (!list_empty(&wq->flusher_overflow)) {
  1063. /*
  1064. * Assign the same color to all overflowed
  1065. * flushers, advance work_color and append to
  1066. * flusher_queue. This is the start-to-wait
  1067. * phase for these overflowed flushers.
  1068. */
  1069. list_for_each_entry(tmp, &wq->flusher_overflow, list)
  1070. tmp->flush_color = wq->work_color;
  1071. wq->work_color = work_next_color(wq->work_color);
  1072. list_splice_tail_init(&wq->flusher_overflow,
  1073. &wq->flusher_queue);
  1074. flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
  1075. }
  1076. if (list_empty(&wq->flusher_queue)) {
  1077. BUG_ON(wq->flush_color != wq->work_color);
  1078. break;
  1079. }
  1080. /*
  1081. * Need to flush more colors. Make the next flusher
  1082. * the new first flusher and arm cwqs.
  1083. */
  1084. BUG_ON(wq->flush_color == wq->work_color);
  1085. BUG_ON(wq->flush_color != next->flush_color);
  1086. list_del_init(&next->list);
  1087. wq->first_flusher = next;
  1088. if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
  1089. break;
  1090. /*
  1091. * Meh... this color is already done, clear first
  1092. * flusher and repeat cascading.
  1093. */
  1094. wq->first_flusher = NULL;
  1095. }
  1096. out_unlock:
  1097. mutex_unlock(&wq->flush_mutex);
  1098. }
  1099. EXPORT_SYMBOL_GPL(flush_workqueue);
  1100. /**
  1101. * flush_work - block until a work_struct's callback has terminated
  1102. * @work: the work which is to be flushed
  1103. *
  1104. * Returns false if @work has already terminated.
  1105. *
  1106. * It is expected that, prior to calling flush_work(), the caller has
  1107. * arranged for the work to not be requeued, otherwise it doesn't make
  1108. * sense to use this function.
  1109. */
  1110. int flush_work(struct work_struct *work)
  1111. {
  1112. struct worker *worker = NULL;
  1113. struct cpu_workqueue_struct *cwq;
  1114. struct global_cwq *gcwq;
  1115. struct wq_barrier barr;
  1116. might_sleep();
  1117. cwq = get_wq_data(work);
  1118. if (!cwq)
  1119. return 0;
  1120. gcwq = cwq->gcwq;
  1121. lock_map_acquire(&cwq->wq->lockdep_map);
  1122. lock_map_release(&cwq->wq->lockdep_map);
  1123. spin_lock_irq(&gcwq->lock);
  1124. if (!list_empty(&work->entry)) {
  1125. /*
  1126. * See the comment near try_to_grab_pending()->smp_rmb().
  1127. * If it was re-queued under us we are not going to wait.
  1128. */
  1129. smp_rmb();
  1130. if (unlikely(cwq != get_wq_data(work)))
  1131. goto already_gone;
  1132. } else {
  1133. if (cwq->worker && cwq->worker->current_work == work)
  1134. worker = cwq->worker;
  1135. if (!worker)
  1136. goto already_gone;
  1137. }
  1138. insert_wq_barrier(cwq, &barr, work, worker);
  1139. spin_unlock_irq(&gcwq->lock);
  1140. wait_for_completion(&barr.done);
  1141. destroy_work_on_stack(&barr.work);
  1142. return 1;
  1143. already_gone:
  1144. spin_unlock_irq(&gcwq->lock);
  1145. return 0;
  1146. }
  1147. EXPORT_SYMBOL_GPL(flush_work);
  1148. /*
  1149. * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
  1150. * so this work can't be re-armed in any way.
  1151. */
  1152. static int try_to_grab_pending(struct work_struct *work)
  1153. {
  1154. struct global_cwq *gcwq;
  1155. struct cpu_workqueue_struct *cwq;
  1156. int ret = -1;
  1157. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  1158. return 0;
  1159. /*
  1160. * The queueing is in progress, or it is already queued. Try to
  1161. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  1162. */
  1163. cwq = get_wq_data(work);
  1164. if (!cwq)
  1165. return ret;
  1166. gcwq = cwq->gcwq;
  1167. spin_lock_irq(&gcwq->lock);
  1168. if (!list_empty(&work->entry)) {
  1169. /*
  1170. * This work is queued, but perhaps we locked the wrong cwq.
  1171. * In that case we must see the new value after rmb(), see
  1172. * insert_work()->wmb().
  1173. */
  1174. smp_rmb();
  1175. if (cwq == get_wq_data(work)) {
  1176. debug_work_deactivate(work);
  1177. list_del_init(&work->entry);
  1178. cwq_dec_nr_in_flight(cwq, get_work_color(work));
  1179. ret = 1;
  1180. }
  1181. }
  1182. spin_unlock_irq(&gcwq->lock);
  1183. return ret;
  1184. }
  1185. static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
  1186. struct work_struct *work)
  1187. {
  1188. struct global_cwq *gcwq = cwq->gcwq;
  1189. struct wq_barrier barr;
  1190. struct worker *worker;
  1191. spin_lock_irq(&gcwq->lock);
  1192. worker = NULL;
  1193. if (unlikely(cwq->worker && cwq->worker->current_work == work)) {
  1194. worker = cwq->worker;
  1195. insert_wq_barrier(cwq, &barr, work, worker);
  1196. }
  1197. spin_unlock_irq(&gcwq->lock);
  1198. if (unlikely(worker)) {
  1199. wait_for_completion(&barr.done);
  1200. destroy_work_on_stack(&barr.work);
  1201. }
  1202. }
  1203. static void wait_on_work(struct work_struct *work)
  1204. {
  1205. struct cpu_workqueue_struct *cwq;
  1206. struct workqueue_struct *wq;
  1207. int cpu;
  1208. might_sleep();
  1209. lock_map_acquire(&work->lockdep_map);
  1210. lock_map_release(&work->lockdep_map);
  1211. cwq = get_wq_data(work);
  1212. if (!cwq)
  1213. return;
  1214. wq = cwq->wq;
  1215. for_each_possible_cpu(cpu)
  1216. wait_on_cpu_work(get_cwq(cpu, wq), work);
  1217. }
  1218. static int __cancel_work_timer(struct work_struct *work,
  1219. struct timer_list* timer)
  1220. {
  1221. int ret;
  1222. do {
  1223. ret = (timer && likely(del_timer(timer)));
  1224. if (!ret)
  1225. ret = try_to_grab_pending(work);
  1226. wait_on_work(work);
  1227. } while (unlikely(ret < 0));
  1228. clear_wq_data(work);
  1229. return ret;
  1230. }
  1231. /**
  1232. * cancel_work_sync - block until a work_struct's callback has terminated
  1233. * @work: the work which is to be flushed
  1234. *
  1235. * Returns true if @work was pending.
  1236. *
  1237. * cancel_work_sync() will cancel the work if it is queued. If the work's
  1238. * callback appears to be running, cancel_work_sync() will block until it
  1239. * has completed.
  1240. *
  1241. * It is possible to use this function if the work re-queues itself. It can
  1242. * cancel the work even if it migrates to another workqueue, however in that
  1243. * case it only guarantees that work->func() has completed on the last queued
  1244. * workqueue.
  1245. *
  1246. * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
  1247. * pending, otherwise it goes into a busy-wait loop until the timer expires.
  1248. *
  1249. * The caller must ensure that workqueue_struct on which this work was last
  1250. * queued can't be destroyed before this function returns.
  1251. */
  1252. int cancel_work_sync(struct work_struct *work)
  1253. {
  1254. return __cancel_work_timer(work, NULL);
  1255. }
  1256. EXPORT_SYMBOL_GPL(cancel_work_sync);
  1257. /**
  1258. * cancel_delayed_work_sync - reliably kill off a delayed work.
  1259. * @dwork: the delayed work struct
  1260. *
  1261. * Returns true if @dwork was pending.
  1262. *
  1263. * It is possible to use this function if @dwork rearms itself via queue_work()
  1264. * or queue_delayed_work(). See also the comment for cancel_work_sync().
  1265. */
  1266. int cancel_delayed_work_sync(struct delayed_work *dwork)
  1267. {
  1268. return __cancel_work_timer(&dwork->work, &dwork->timer);
  1269. }
  1270. EXPORT_SYMBOL(cancel_delayed_work_sync);
  1271. static struct workqueue_struct *keventd_wq __read_mostly;
  1272. /**
  1273. * schedule_work - put work task in global workqueue
  1274. * @work: job to be done
  1275. *
  1276. * Returns zero if @work was already on the kernel-global workqueue and
  1277. * non-zero otherwise.
  1278. *
  1279. * This puts a job in the kernel-global workqueue if it was not already
  1280. * queued and leaves it in the same position on the kernel-global
  1281. * workqueue otherwise.
  1282. */
  1283. int schedule_work(struct work_struct *work)
  1284. {
  1285. return queue_work(keventd_wq, work);
  1286. }
  1287. EXPORT_SYMBOL(schedule_work);
  1288. /*
  1289. * schedule_work_on - put work task on a specific cpu
  1290. * @cpu: cpu to put the work task on
  1291. * @work: job to be done
  1292. *
  1293. * This puts a job on a specific cpu
  1294. */
  1295. int schedule_work_on(int cpu, struct work_struct *work)
  1296. {
  1297. return queue_work_on(cpu, keventd_wq, work);
  1298. }
  1299. EXPORT_SYMBOL(schedule_work_on);
  1300. /**
  1301. * schedule_delayed_work - put work task in global workqueue after delay
  1302. * @dwork: job to be done
  1303. * @delay: number of jiffies to wait or 0 for immediate execution
  1304. *
  1305. * After waiting for a given time this puts a job in the kernel-global
  1306. * workqueue.
  1307. */
  1308. int schedule_delayed_work(struct delayed_work *dwork,
  1309. unsigned long delay)
  1310. {
  1311. return queue_delayed_work(keventd_wq, dwork, delay);
  1312. }
  1313. EXPORT_SYMBOL(schedule_delayed_work);
  1314. /**
  1315. * flush_delayed_work - block until a dwork_struct's callback has terminated
  1316. * @dwork: the delayed work which is to be flushed
  1317. *
  1318. * Any timeout is cancelled, and any pending work is run immediately.
  1319. */
  1320. void flush_delayed_work(struct delayed_work *dwork)
  1321. {
  1322. if (del_timer_sync(&dwork->timer)) {
  1323. __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
  1324. &dwork->work);
  1325. put_cpu();
  1326. }
  1327. flush_work(&dwork->work);
  1328. }
  1329. EXPORT_SYMBOL(flush_delayed_work);
  1330. /**
  1331. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  1332. * @cpu: cpu to use
  1333. * @dwork: job to be done
  1334. * @delay: number of jiffies to wait
  1335. *
  1336. * After waiting for a given time this puts a job in the kernel-global
  1337. * workqueue on the specified CPU.
  1338. */
  1339. int schedule_delayed_work_on(int cpu,
  1340. struct delayed_work *dwork, unsigned long delay)
  1341. {
  1342. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  1343. }
  1344. EXPORT_SYMBOL(schedule_delayed_work_on);
  1345. /**
  1346. * schedule_on_each_cpu - call a function on each online CPU from keventd
  1347. * @func: the function to call
  1348. *
  1349. * Returns zero on success.
  1350. * Returns -ve errno on failure.
  1351. *
  1352. * schedule_on_each_cpu() is very slow.
  1353. */
  1354. int schedule_on_each_cpu(work_func_t func)
  1355. {
  1356. int cpu;
  1357. int orig = -1;
  1358. struct work_struct *works;
  1359. works = alloc_percpu(struct work_struct);
  1360. if (!works)
  1361. return -ENOMEM;
  1362. get_online_cpus();
  1363. /*
  1364. * When running in keventd don't schedule a work item on
  1365. * itself. Can just call directly because the work queue is
  1366. * already bound. This also is faster.
  1367. */
  1368. if (current_is_keventd())
  1369. orig = raw_smp_processor_id();
  1370. for_each_online_cpu(cpu) {
  1371. struct work_struct *work = per_cpu_ptr(works, cpu);
  1372. INIT_WORK(work, func);
  1373. if (cpu != orig)
  1374. schedule_work_on(cpu, work);
  1375. }
  1376. if (orig >= 0)
  1377. func(per_cpu_ptr(works, orig));
  1378. for_each_online_cpu(cpu)
  1379. flush_work(per_cpu_ptr(works, cpu));
  1380. put_online_cpus();
  1381. free_percpu(works);
  1382. return 0;
  1383. }
  1384. /**
  1385. * flush_scheduled_work - ensure that any scheduled work has run to completion.
  1386. *
  1387. * Forces execution of the kernel-global workqueue and blocks until its
  1388. * completion.
  1389. *
  1390. * Think twice before calling this function! It's very easy to get into
  1391. * trouble if you don't take great care. Either of the following situations
  1392. * will lead to deadlock:
  1393. *
  1394. * One of the work items currently on the workqueue needs to acquire
  1395. * a lock held by your code or its caller.
  1396. *
  1397. * Your code is running in the context of a work routine.
  1398. *
  1399. * They will be detected by lockdep when they occur, but the first might not
  1400. * occur very often. It depends on what work items are on the workqueue and
  1401. * what locks they need, which you have no control over.
  1402. *
  1403. * In most situations flushing the entire workqueue is overkill; you merely
  1404. * need to know that a particular work item isn't queued and isn't running.
  1405. * In such cases you should use cancel_delayed_work_sync() or
  1406. * cancel_work_sync() instead.
  1407. */
  1408. void flush_scheduled_work(void)
  1409. {
  1410. flush_workqueue(keventd_wq);
  1411. }
  1412. EXPORT_SYMBOL(flush_scheduled_work);
  1413. /**
  1414. * execute_in_process_context - reliably execute the routine with user context
  1415. * @fn: the function to execute
  1416. * @ew: guaranteed storage for the execute work structure (must
  1417. * be available when the work executes)
  1418. *
  1419. * Executes the function immediately if process context is available,
  1420. * otherwise schedules the function for delayed execution.
  1421. *
  1422. * Returns: 0 - function was executed
  1423. * 1 - function was scheduled for execution
  1424. */
  1425. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  1426. {
  1427. if (!in_interrupt()) {
  1428. fn(&ew->work);
  1429. return 0;
  1430. }
  1431. INIT_WORK(&ew->work, fn);
  1432. schedule_work(&ew->work);
  1433. return 1;
  1434. }
  1435. EXPORT_SYMBOL_GPL(execute_in_process_context);
  1436. int keventd_up(void)
  1437. {
  1438. return keventd_wq != NULL;
  1439. }
  1440. int current_is_keventd(void)
  1441. {
  1442. struct cpu_workqueue_struct *cwq;
  1443. int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  1444. int ret = 0;
  1445. BUG_ON(!keventd_wq);
  1446. cwq = get_cwq(cpu, keventd_wq);
  1447. if (current == cwq->worker->task)
  1448. ret = 1;
  1449. return ret;
  1450. }
  1451. static struct cpu_workqueue_struct *alloc_cwqs(void)
  1452. {
  1453. /*
  1454. * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
  1455. * Make sure that the alignment isn't lower than that of
  1456. * unsigned long long.
  1457. */
  1458. const size_t size = sizeof(struct cpu_workqueue_struct);
  1459. const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
  1460. __alignof__(unsigned long long));
  1461. struct cpu_workqueue_struct *cwqs;
  1462. #ifndef CONFIG_SMP
  1463. void *ptr;
  1464. /*
  1465. * On UP, percpu allocator doesn't honor alignment parameter
  1466. * and simply uses arch-dependent default. Allocate enough
  1467. * room to align cwq and put an extra pointer at the end
  1468. * pointing back to the originally allocated pointer which
  1469. * will be used for free.
  1470. *
  1471. * FIXME: This really belongs to UP percpu code. Update UP
  1472. * percpu code to honor alignment and remove this ugliness.
  1473. */
  1474. ptr = __alloc_percpu(size + align + sizeof(void *), 1);
  1475. cwqs = PTR_ALIGN(ptr, align);
  1476. *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
  1477. #else
  1478. /* On SMP, percpu allocator can do it itself */
  1479. cwqs = __alloc_percpu(size, align);
  1480. #endif
  1481. /* just in case, make sure it's actually aligned */
  1482. BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
  1483. return cwqs;
  1484. }
  1485. static void free_cwqs(struct cpu_workqueue_struct *cwqs)
  1486. {
  1487. #ifndef CONFIG_SMP
  1488. /* on UP, the pointer to free is stored right after the cwq */
  1489. if (cwqs)
  1490. free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
  1491. #else
  1492. free_percpu(cwqs);
  1493. #endif
  1494. }
  1495. struct workqueue_struct *__create_workqueue_key(const char *name,
  1496. unsigned int flags,
  1497. int max_active,
  1498. struct lock_class_key *key,
  1499. const char *lock_name)
  1500. {
  1501. bool singlethread = flags & WQ_SINGLE_THREAD;
  1502. struct workqueue_struct *wq;
  1503. bool failed = false;
  1504. unsigned int cpu;
  1505. max_active = clamp_val(max_active, 1, INT_MAX);
  1506. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  1507. if (!wq)
  1508. goto err;
  1509. wq->cpu_wq = alloc_cwqs();
  1510. if (!wq->cpu_wq)
  1511. goto err;
  1512. wq->flags = flags;
  1513. wq->saved_max_active = max_active;
  1514. mutex_init(&wq->flush_mutex);
  1515. atomic_set(&wq->nr_cwqs_to_flush, 0);
  1516. INIT_LIST_HEAD(&wq->flusher_queue);
  1517. INIT_LIST_HEAD(&wq->flusher_overflow);
  1518. wq->name = name;
  1519. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  1520. INIT_LIST_HEAD(&wq->list);
  1521. cpu_maps_update_begin();
  1522. /*
  1523. * We must initialize cwqs for each possible cpu even if we
  1524. * are going to call destroy_workqueue() finally. Otherwise
  1525. * cpu_up() can hit the uninitialized cwq once we drop the
  1526. * lock.
  1527. */
  1528. for_each_possible_cpu(cpu) {
  1529. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1530. struct global_cwq *gcwq = get_gcwq(cpu);
  1531. BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
  1532. cwq->gcwq = gcwq;
  1533. cwq->wq = wq;
  1534. cwq->flush_color = -1;
  1535. cwq->max_active = max_active;
  1536. INIT_LIST_HEAD(&cwq->worklist);
  1537. INIT_LIST_HEAD(&cwq->delayed_works);
  1538. if (failed)
  1539. continue;
  1540. cwq->worker = create_worker(cwq,
  1541. cpu_online(cpu) && !singlethread);
  1542. if (cwq->worker)
  1543. start_worker(cwq->worker);
  1544. else
  1545. failed = true;
  1546. }
  1547. /*
  1548. * workqueue_lock protects global freeze state and workqueues
  1549. * list. Grab it, set max_active accordingly and add the new
  1550. * workqueue to workqueues list.
  1551. */
  1552. spin_lock(&workqueue_lock);
  1553. if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
  1554. for_each_possible_cpu(cpu)
  1555. get_cwq(cpu, wq)->max_active = 0;
  1556. list_add(&wq->list, &workqueues);
  1557. spin_unlock(&workqueue_lock);
  1558. cpu_maps_update_done();
  1559. if (failed) {
  1560. destroy_workqueue(wq);
  1561. wq = NULL;
  1562. }
  1563. return wq;
  1564. err:
  1565. if (wq) {
  1566. free_cwqs(wq->cpu_wq);
  1567. kfree(wq);
  1568. }
  1569. return NULL;
  1570. }
  1571. EXPORT_SYMBOL_GPL(__create_workqueue_key);
  1572. /**
  1573. * destroy_workqueue - safely terminate a workqueue
  1574. * @wq: target workqueue
  1575. *
  1576. * Safely destroy a workqueue. All work currently pending will be done first.
  1577. */
  1578. void destroy_workqueue(struct workqueue_struct *wq)
  1579. {
  1580. unsigned int cpu;
  1581. flush_workqueue(wq);
  1582. /*
  1583. * wq list is used to freeze wq, remove from list after
  1584. * flushing is complete in case freeze races us.
  1585. */
  1586. cpu_maps_update_begin();
  1587. spin_lock(&workqueue_lock);
  1588. list_del(&wq->list);
  1589. spin_unlock(&workqueue_lock);
  1590. cpu_maps_update_done();
  1591. for_each_possible_cpu(cpu) {
  1592. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1593. int i;
  1594. if (cwq->worker) {
  1595. spin_lock_irq(&cwq->gcwq->lock);
  1596. destroy_worker(cwq->worker);
  1597. cwq->worker = NULL;
  1598. spin_unlock_irq(&cwq->gcwq->lock);
  1599. }
  1600. for (i = 0; i < WORK_NR_COLORS; i++)
  1601. BUG_ON(cwq->nr_in_flight[i]);
  1602. BUG_ON(cwq->nr_active);
  1603. BUG_ON(!list_empty(&cwq->delayed_works));
  1604. }
  1605. free_cwqs(wq->cpu_wq);
  1606. kfree(wq);
  1607. }
  1608. EXPORT_SYMBOL_GPL(destroy_workqueue);
  1609. /*
  1610. * CPU hotplug.
  1611. *
  1612. * CPU hotplug is implemented by allowing cwqs to be detached from
  1613. * CPU, running with unbound workers and allowing them to be
  1614. * reattached later if the cpu comes back online. A separate thread
  1615. * is created to govern cwqs in such state and is called the trustee.
  1616. *
  1617. * Trustee states and their descriptions.
  1618. *
  1619. * START Command state used on startup. On CPU_DOWN_PREPARE, a
  1620. * new trustee is started with this state.
  1621. *
  1622. * IN_CHARGE Once started, trustee will enter this state after
  1623. * making all existing workers rogue. DOWN_PREPARE waits
  1624. * for trustee to enter this state. After reaching
  1625. * IN_CHARGE, trustee tries to execute the pending
  1626. * worklist until it's empty and the state is set to
  1627. * BUTCHER, or the state is set to RELEASE.
  1628. *
  1629. * BUTCHER Command state which is set by the cpu callback after
  1630. * the cpu has went down. Once this state is set trustee
  1631. * knows that there will be no new works on the worklist
  1632. * and once the worklist is empty it can proceed to
  1633. * killing idle workers.
  1634. *
  1635. * RELEASE Command state which is set by the cpu callback if the
  1636. * cpu down has been canceled or it has come online
  1637. * again. After recognizing this state, trustee stops
  1638. * trying to drain or butcher and transits to DONE.
  1639. *
  1640. * DONE Trustee will enter this state after BUTCHER or RELEASE
  1641. * is complete.
  1642. *
  1643. * trustee CPU draining
  1644. * took over down complete
  1645. * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
  1646. * | | ^
  1647. * | CPU is back online v return workers |
  1648. * ----------------> RELEASE --------------
  1649. */
  1650. /**
  1651. * trustee_wait_event_timeout - timed event wait for trustee
  1652. * @cond: condition to wait for
  1653. * @timeout: timeout in jiffies
  1654. *
  1655. * wait_event_timeout() for trustee to use. Handles locking and
  1656. * checks for RELEASE request.
  1657. *
  1658. * CONTEXT:
  1659. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  1660. * multiple times. To be used by trustee.
  1661. *
  1662. * RETURNS:
  1663. * Positive indicating left time if @cond is satisfied, 0 if timed
  1664. * out, -1 if canceled.
  1665. */
  1666. #define trustee_wait_event_timeout(cond, timeout) ({ \
  1667. long __ret = (timeout); \
  1668. while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
  1669. __ret) { \
  1670. spin_unlock_irq(&gcwq->lock); \
  1671. __wait_event_timeout(gcwq->trustee_wait, (cond) || \
  1672. (gcwq->trustee_state == TRUSTEE_RELEASE), \
  1673. __ret); \
  1674. spin_lock_irq(&gcwq->lock); \
  1675. } \
  1676. gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
  1677. })
  1678. /**
  1679. * trustee_wait_event - event wait for trustee
  1680. * @cond: condition to wait for
  1681. *
  1682. * wait_event() for trustee to use. Automatically handles locking and
  1683. * checks for CANCEL request.
  1684. *
  1685. * CONTEXT:
  1686. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  1687. * multiple times. To be used by trustee.
  1688. *
  1689. * RETURNS:
  1690. * 0 if @cond is satisfied, -1 if canceled.
  1691. */
  1692. #define trustee_wait_event(cond) ({ \
  1693. long __ret1; \
  1694. __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
  1695. __ret1 < 0 ? -1 : 0; \
  1696. })
  1697. static int __cpuinit trustee_thread(void *__gcwq)
  1698. {
  1699. struct global_cwq *gcwq = __gcwq;
  1700. struct worker *worker;
  1701. struct hlist_node *pos;
  1702. int i;
  1703. BUG_ON(gcwq->cpu != smp_processor_id());
  1704. spin_lock_irq(&gcwq->lock);
  1705. /*
  1706. * Make all multithread workers rogue. Trustee must be bound
  1707. * to the target cpu and can't be cancelled.
  1708. */
  1709. BUG_ON(gcwq->cpu != smp_processor_id());
  1710. list_for_each_entry(worker, &gcwq->idle_list, entry)
  1711. if (!(worker->cwq->wq->flags & WQ_SINGLE_THREAD))
  1712. worker->flags |= WORKER_ROGUE;
  1713. for_each_busy_worker(worker, i, pos, gcwq)
  1714. if (!(worker->cwq->wq->flags & WQ_SINGLE_THREAD))
  1715. worker->flags |= WORKER_ROGUE;
  1716. /*
  1717. * We're now in charge. Notify and proceed to drain. We need
  1718. * to keep the gcwq running during the whole CPU down
  1719. * procedure as other cpu hotunplug callbacks may need to
  1720. * flush currently running tasks.
  1721. */
  1722. gcwq->trustee_state = TRUSTEE_IN_CHARGE;
  1723. wake_up_all(&gcwq->trustee_wait);
  1724. /*
  1725. * The original cpu is in the process of dying and may go away
  1726. * anytime now. When that happens, we and all workers would
  1727. * be migrated to other cpus. Try draining any left work.
  1728. * Note that if the gcwq is frozen, there may be frozen works
  1729. * in freezeable cwqs. Don't declare completion while frozen.
  1730. */
  1731. while (gcwq->nr_workers != gcwq->nr_idle ||
  1732. gcwq->flags & GCWQ_FREEZING ||
  1733. gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
  1734. /* give a breather */
  1735. if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
  1736. break;
  1737. }
  1738. /* notify completion */
  1739. gcwq->trustee = NULL;
  1740. gcwq->trustee_state = TRUSTEE_DONE;
  1741. wake_up_all(&gcwq->trustee_wait);
  1742. spin_unlock_irq(&gcwq->lock);
  1743. return 0;
  1744. }
  1745. /**
  1746. * wait_trustee_state - wait for trustee to enter the specified state
  1747. * @gcwq: gcwq the trustee of interest belongs to
  1748. * @state: target state to wait for
  1749. *
  1750. * Wait for the trustee to reach @state. DONE is already matched.
  1751. *
  1752. * CONTEXT:
  1753. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  1754. * multiple times. To be used by cpu_callback.
  1755. */
  1756. static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
  1757. {
  1758. if (!(gcwq->trustee_state == state ||
  1759. gcwq->trustee_state == TRUSTEE_DONE)) {
  1760. spin_unlock_irq(&gcwq->lock);
  1761. __wait_event(gcwq->trustee_wait,
  1762. gcwq->trustee_state == state ||
  1763. gcwq->trustee_state == TRUSTEE_DONE);
  1764. spin_lock_irq(&gcwq->lock);
  1765. }
  1766. }
  1767. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  1768. unsigned long action,
  1769. void *hcpu)
  1770. {
  1771. unsigned int cpu = (unsigned long)hcpu;
  1772. struct global_cwq *gcwq = get_gcwq(cpu);
  1773. struct task_struct *new_trustee = NULL;
  1774. struct worker *worker;
  1775. struct hlist_node *pos;
  1776. unsigned long flags;
  1777. int i;
  1778. action &= ~CPU_TASKS_FROZEN;
  1779. switch (action) {
  1780. case CPU_DOWN_PREPARE:
  1781. new_trustee = kthread_create(trustee_thread, gcwq,
  1782. "workqueue_trustee/%d\n", cpu);
  1783. if (IS_ERR(new_trustee))
  1784. return notifier_from_errno(PTR_ERR(new_trustee));
  1785. kthread_bind(new_trustee, cpu);
  1786. }
  1787. /* some are called w/ irq disabled, don't disturb irq status */
  1788. spin_lock_irqsave(&gcwq->lock, flags);
  1789. switch (action) {
  1790. case CPU_DOWN_PREPARE:
  1791. /* initialize trustee and tell it to acquire the gcwq */
  1792. BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
  1793. gcwq->trustee = new_trustee;
  1794. gcwq->trustee_state = TRUSTEE_START;
  1795. wake_up_process(gcwq->trustee);
  1796. wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
  1797. break;
  1798. case CPU_POST_DEAD:
  1799. gcwq->trustee_state = TRUSTEE_BUTCHER;
  1800. break;
  1801. case CPU_DOWN_FAILED:
  1802. case CPU_ONLINE:
  1803. if (gcwq->trustee_state != TRUSTEE_DONE) {
  1804. gcwq->trustee_state = TRUSTEE_RELEASE;
  1805. wake_up_process(gcwq->trustee);
  1806. wait_trustee_state(gcwq, TRUSTEE_DONE);
  1807. }
  1808. /* clear ROGUE from all multithread workers */
  1809. list_for_each_entry(worker, &gcwq->idle_list, entry)
  1810. if (!(worker->cwq->wq->flags & WQ_SINGLE_THREAD))
  1811. worker->flags &= ~WORKER_ROGUE;
  1812. for_each_busy_worker(worker, i, pos, gcwq)
  1813. if (!(worker->cwq->wq->flags & WQ_SINGLE_THREAD))
  1814. worker->flags &= ~WORKER_ROGUE;
  1815. break;
  1816. }
  1817. spin_unlock_irqrestore(&gcwq->lock, flags);
  1818. return notifier_from_errno(0);
  1819. }
  1820. #ifdef CONFIG_SMP
  1821. struct work_for_cpu {
  1822. struct completion completion;
  1823. long (*fn)(void *);
  1824. void *arg;
  1825. long ret;
  1826. };
  1827. static int do_work_for_cpu(void *_wfc)
  1828. {
  1829. struct work_for_cpu *wfc = _wfc;
  1830. wfc->ret = wfc->fn(wfc->arg);
  1831. complete(&wfc->completion);
  1832. return 0;
  1833. }
  1834. /**
  1835. * work_on_cpu - run a function in user context on a particular cpu
  1836. * @cpu: the cpu to run on
  1837. * @fn: the function to run
  1838. * @arg: the function arg
  1839. *
  1840. * This will return the value @fn returns.
  1841. * It is up to the caller to ensure that the cpu doesn't go offline.
  1842. * The caller must not hold any locks which would prevent @fn from completing.
  1843. */
  1844. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  1845. {
  1846. struct task_struct *sub_thread;
  1847. struct work_for_cpu wfc = {
  1848. .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
  1849. .fn = fn,
  1850. .arg = arg,
  1851. };
  1852. sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
  1853. if (IS_ERR(sub_thread))
  1854. return PTR_ERR(sub_thread);
  1855. kthread_bind(sub_thread, cpu);
  1856. wake_up_process(sub_thread);
  1857. wait_for_completion(&wfc.completion);
  1858. return wfc.ret;
  1859. }
  1860. EXPORT_SYMBOL_GPL(work_on_cpu);
  1861. #endif /* CONFIG_SMP */
  1862. #ifdef CONFIG_FREEZER
  1863. /**
  1864. * freeze_workqueues_begin - begin freezing workqueues
  1865. *
  1866. * Start freezing workqueues. After this function returns, all
  1867. * freezeable workqueues will queue new works to their frozen_works
  1868. * list instead of the cwq ones.
  1869. *
  1870. * CONTEXT:
  1871. * Grabs and releases workqueue_lock and gcwq->lock's.
  1872. */
  1873. void freeze_workqueues_begin(void)
  1874. {
  1875. struct workqueue_struct *wq;
  1876. unsigned int cpu;
  1877. spin_lock(&workqueue_lock);
  1878. BUG_ON(workqueue_freezing);
  1879. workqueue_freezing = true;
  1880. for_each_possible_cpu(cpu) {
  1881. struct global_cwq *gcwq = get_gcwq(cpu);
  1882. spin_lock_irq(&gcwq->lock);
  1883. BUG_ON(gcwq->flags & GCWQ_FREEZING);
  1884. gcwq->flags |= GCWQ_FREEZING;
  1885. list_for_each_entry(wq, &workqueues, list) {
  1886. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1887. if (wq->flags & WQ_FREEZEABLE)
  1888. cwq->max_active = 0;
  1889. }
  1890. spin_unlock_irq(&gcwq->lock);
  1891. }
  1892. spin_unlock(&workqueue_lock);
  1893. }
  1894. /**
  1895. * freeze_workqueues_busy - are freezeable workqueues still busy?
  1896. *
  1897. * Check whether freezing is complete. This function must be called
  1898. * between freeze_workqueues_begin() and thaw_workqueues().
  1899. *
  1900. * CONTEXT:
  1901. * Grabs and releases workqueue_lock.
  1902. *
  1903. * RETURNS:
  1904. * %true if some freezeable workqueues are still busy. %false if
  1905. * freezing is complete.
  1906. */
  1907. bool freeze_workqueues_busy(void)
  1908. {
  1909. struct workqueue_struct *wq;
  1910. unsigned int cpu;
  1911. bool busy = false;
  1912. spin_lock(&workqueue_lock);
  1913. BUG_ON(!workqueue_freezing);
  1914. for_each_possible_cpu(cpu) {
  1915. /*
  1916. * nr_active is monotonically decreasing. It's safe
  1917. * to peek without lock.
  1918. */
  1919. list_for_each_entry(wq, &workqueues, list) {
  1920. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1921. if (!(wq->flags & WQ_FREEZEABLE))
  1922. continue;
  1923. BUG_ON(cwq->nr_active < 0);
  1924. if (cwq->nr_active) {
  1925. busy = true;
  1926. goto out_unlock;
  1927. }
  1928. }
  1929. }
  1930. out_unlock:
  1931. spin_unlock(&workqueue_lock);
  1932. return busy;
  1933. }
  1934. /**
  1935. * thaw_workqueues - thaw workqueues
  1936. *
  1937. * Thaw workqueues. Normal queueing is restored and all collected
  1938. * frozen works are transferred to their respective cwq worklists.
  1939. *
  1940. * CONTEXT:
  1941. * Grabs and releases workqueue_lock and gcwq->lock's.
  1942. */
  1943. void thaw_workqueues(void)
  1944. {
  1945. struct workqueue_struct *wq;
  1946. unsigned int cpu;
  1947. spin_lock(&workqueue_lock);
  1948. if (!workqueue_freezing)
  1949. goto out_unlock;
  1950. for_each_possible_cpu(cpu) {
  1951. struct global_cwq *gcwq = get_gcwq(cpu);
  1952. spin_lock_irq(&gcwq->lock);
  1953. BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
  1954. gcwq->flags &= ~GCWQ_FREEZING;
  1955. list_for_each_entry(wq, &workqueues, list) {
  1956. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1957. if (!(wq->flags & WQ_FREEZEABLE))
  1958. continue;
  1959. /* restore max_active and repopulate worklist */
  1960. cwq->max_active = wq->saved_max_active;
  1961. while (!list_empty(&cwq->delayed_works) &&
  1962. cwq->nr_active < cwq->max_active)
  1963. cwq_activate_first_delayed(cwq);
  1964. wake_up_process(cwq->worker->task);
  1965. }
  1966. spin_unlock_irq(&gcwq->lock);
  1967. }
  1968. workqueue_freezing = false;
  1969. out_unlock:
  1970. spin_unlock(&workqueue_lock);
  1971. }
  1972. #endif /* CONFIG_FREEZER */
  1973. void __init init_workqueues(void)
  1974. {
  1975. unsigned int cpu;
  1976. int i;
  1977. singlethread_cpu = cpumask_first(cpu_possible_mask);
  1978. hotcpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
  1979. /* initialize gcwqs */
  1980. for_each_possible_cpu(cpu) {
  1981. struct global_cwq *gcwq = get_gcwq(cpu);
  1982. spin_lock_init(&gcwq->lock);
  1983. gcwq->cpu = cpu;
  1984. INIT_LIST_HEAD(&gcwq->idle_list);
  1985. for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
  1986. INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
  1987. ida_init(&gcwq->worker_ida);
  1988. gcwq->trustee_state = TRUSTEE_DONE;
  1989. init_waitqueue_head(&gcwq->trustee_wait);
  1990. }
  1991. keventd_wq = create_workqueue("events");
  1992. BUG_ON(!keventd_wq);
  1993. }