workqueue.c 68 KB

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