workqueue.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699
  1. /*
  2. * kernel/workqueue.c - generic async execution with shared worker pool
  3. *
  4. * Copyright (C) 2002 Ingo Molnar
  5. *
  6. * Derived from the taskqueue/keventd code by:
  7. * David Woodhouse <dwmw2@infradead.org>
  8. * Andrew Morton
  9. * Kai Petzke <wpp@marie.physik.tu-berlin.de>
  10. * Theodore Ts'o <tytso@mit.edu>
  11. *
  12. * Made to use alloc_percpu by Christoph Lameter.
  13. *
  14. * Copyright (C) 2010 SUSE Linux Products GmbH
  15. * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
  16. *
  17. * This is the generic async execution mechanism. Work items as are
  18. * executed in process context. The worker pool is shared and
  19. * automatically managed. There is one worker pool for each CPU and
  20. * one extra for works which are better served by workers which are
  21. * not bound to any specific CPU.
  22. *
  23. * Please read Documentation/workqueue.txt for details.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/init.h>
  29. #include <linux/signal.h>
  30. #include <linux/completion.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/slab.h>
  33. #include <linux/cpu.h>
  34. #include <linux/notifier.h>
  35. #include <linux/kthread.h>
  36. #include <linux/hardirq.h>
  37. #include <linux/mempolicy.h>
  38. #include <linux/freezer.h>
  39. #include <linux/kallsyms.h>
  40. #include <linux/debug_locks.h>
  41. #include <linux/lockdep.h>
  42. #include <linux/idr.h>
  43. #include "workqueue_sched.h"
  44. enum {
  45. /* global_cwq flags */
  46. GCWQ_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
  47. GCWQ_MANAGING_WORKERS = 1 << 1, /* managing workers */
  48. GCWQ_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
  49. GCWQ_FREEZING = 1 << 3, /* freeze in progress */
  50. GCWQ_HIGHPRI_PENDING = 1 << 4, /* highpri works on queue */
  51. /* worker flags */
  52. WORKER_STARTED = 1 << 0, /* started */
  53. WORKER_DIE = 1 << 1, /* die die die */
  54. WORKER_IDLE = 1 << 2, /* is idle */
  55. WORKER_PREP = 1 << 3, /* preparing to run works */
  56. WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
  57. WORKER_REBIND = 1 << 5, /* mom is home, come back */
  58. WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
  59. WORKER_UNBOUND = 1 << 7, /* worker is unbound */
  60. WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
  61. WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
  62. /* gcwq->trustee_state */
  63. TRUSTEE_START = 0, /* start */
  64. TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
  65. TRUSTEE_BUTCHER = 2, /* butcher workers */
  66. TRUSTEE_RELEASE = 3, /* release workers */
  67. TRUSTEE_DONE = 4, /* trustee is done */
  68. BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
  69. BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
  70. BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
  71. MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
  72. IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
  73. MAYDAY_INITIAL_TIMEOUT = HZ / 100, /* call for help after 10ms */
  74. MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
  75. CREATE_COOLDOWN = HZ, /* time to breath after fail */
  76. TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
  77. /*
  78. * Rescue workers are used only on emergencies and shared by
  79. * all cpus. Give -20.
  80. */
  81. RESCUER_NICE_LEVEL = -20,
  82. };
  83. /*
  84. * Structure fields follow one of the following exclusion rules.
  85. *
  86. * I: Modifiable by initialization/destruction paths and read-only for
  87. * everyone else.
  88. *
  89. * P: Preemption protected. Disabling preemption is enough and should
  90. * only be modified and accessed from the local cpu.
  91. *
  92. * L: gcwq->lock protected. Access with gcwq->lock held.
  93. *
  94. * X: During normal operation, modification requires gcwq->lock and
  95. * should be done only from local cpu. Either disabling preemption
  96. * on local cpu or grabbing gcwq->lock is enough for read access.
  97. * If GCWQ_DISASSOCIATED is set, it's identical to L.
  98. *
  99. * F: wq->flush_mutex protected.
  100. *
  101. * W: workqueue_lock protected.
  102. */
  103. struct global_cwq;
  104. /*
  105. * The poor guys doing the actual heavy lifting. All on-duty workers
  106. * are either serving the manager role, on idle list or on busy hash.
  107. */
  108. struct worker {
  109. /* on idle list while idle, on busy hash table while busy */
  110. union {
  111. struct list_head entry; /* L: while idle */
  112. struct hlist_node hentry; /* L: while busy */
  113. };
  114. struct work_struct *current_work; /* L: work being processed */
  115. struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
  116. struct list_head scheduled; /* L: scheduled works */
  117. struct task_struct *task; /* I: worker task */
  118. struct global_cwq *gcwq; /* I: the associated gcwq */
  119. /* 64 bytes boundary on 64bit, 32 on 32bit */
  120. unsigned long last_active; /* L: last active timestamp */
  121. unsigned int flags; /* X: flags */
  122. int id; /* I: worker id */
  123. struct work_struct rebind_work; /* L: rebind worker to cpu */
  124. };
  125. /*
  126. * Global per-cpu workqueue. There's one and only one for each cpu
  127. * and all works are queued and processed here regardless of their
  128. * target workqueues.
  129. */
  130. struct global_cwq {
  131. spinlock_t lock; /* the gcwq lock */
  132. struct list_head worklist; /* L: list of pending works */
  133. unsigned int cpu; /* I: the associated cpu */
  134. unsigned int flags; /* L: GCWQ_* flags */
  135. int nr_workers; /* L: total number of workers */
  136. int nr_idle; /* L: currently idle ones */
  137. /* workers are chained either in the idle_list or busy_hash */
  138. struct list_head idle_list; /* X: list of idle workers */
  139. struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
  140. /* L: hash of busy workers */
  141. struct timer_list idle_timer; /* L: worker idle timeout */
  142. struct timer_list mayday_timer; /* L: SOS timer for dworkers */
  143. struct ida worker_ida; /* L: for worker IDs */
  144. struct task_struct *trustee; /* L: for gcwq shutdown */
  145. unsigned int trustee_state; /* L: trustee state */
  146. wait_queue_head_t trustee_wait; /* trustee wait */
  147. struct worker *first_idle; /* L: first idle worker */
  148. } ____cacheline_aligned_in_smp;
  149. /*
  150. * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
  151. * work_struct->data are used for flags and thus cwqs need to be
  152. * aligned at two's power of the number of flag bits.
  153. */
  154. struct cpu_workqueue_struct {
  155. struct global_cwq *gcwq; /* I: the associated gcwq */
  156. struct workqueue_struct *wq; /* I: the owning workqueue */
  157. int work_color; /* L: current color */
  158. int flush_color; /* L: flushing color */
  159. int nr_in_flight[WORK_NR_COLORS];
  160. /* L: nr of in_flight works */
  161. int nr_active; /* L: nr of active works */
  162. int max_active; /* L: max active works */
  163. struct list_head delayed_works; /* L: delayed works */
  164. };
  165. /*
  166. * Structure used to wait for workqueue flush.
  167. */
  168. struct wq_flusher {
  169. struct list_head list; /* F: list of flushers */
  170. int flush_color; /* F: flush color waiting for */
  171. struct completion done; /* flush completion */
  172. };
  173. /*
  174. * All cpumasks are assumed to be always set on UP and thus can't be
  175. * used to determine whether there's something to be done.
  176. */
  177. #ifdef CONFIG_SMP
  178. typedef cpumask_var_t mayday_mask_t;
  179. #define mayday_test_and_set_cpu(cpu, mask) \
  180. cpumask_test_and_set_cpu((cpu), (mask))
  181. #define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
  182. #define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
  183. #define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
  184. #define free_mayday_mask(mask) free_cpumask_var((mask))
  185. #else
  186. typedef unsigned long mayday_mask_t;
  187. #define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
  188. #define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
  189. #define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
  190. #define alloc_mayday_mask(maskp, gfp) true
  191. #define free_mayday_mask(mask) do { } while (0)
  192. #endif
  193. /*
  194. * The externally visible workqueue abstraction is an array of
  195. * per-CPU workqueues:
  196. */
  197. struct workqueue_struct {
  198. unsigned int flags; /* I: WQ_* flags */
  199. union {
  200. struct cpu_workqueue_struct __percpu *pcpu;
  201. struct cpu_workqueue_struct *single;
  202. unsigned long v;
  203. } cpu_wq; /* I: cwq's */
  204. struct list_head list; /* W: list of all workqueues */
  205. struct mutex flush_mutex; /* protects wq flushing */
  206. int work_color; /* F: current work color */
  207. int flush_color; /* F: current flush color */
  208. atomic_t nr_cwqs_to_flush; /* flush in progress */
  209. struct wq_flusher *first_flusher; /* F: first flusher */
  210. struct list_head flusher_queue; /* F: flush waiters */
  211. struct list_head flusher_overflow; /* F: flush overflow list */
  212. mayday_mask_t mayday_mask; /* cpus requesting rescue */
  213. struct worker *rescuer; /* I: rescue worker */
  214. int saved_max_active; /* W: saved cwq max_active */
  215. const char *name; /* I: workqueue name */
  216. #ifdef CONFIG_LOCKDEP
  217. struct lockdep_map lockdep_map;
  218. #endif
  219. };
  220. struct workqueue_struct *system_wq __read_mostly;
  221. struct workqueue_struct *system_long_wq __read_mostly;
  222. struct workqueue_struct *system_nrt_wq __read_mostly;
  223. struct workqueue_struct *system_unbound_wq __read_mostly;
  224. EXPORT_SYMBOL_GPL(system_wq);
  225. EXPORT_SYMBOL_GPL(system_long_wq);
  226. EXPORT_SYMBOL_GPL(system_nrt_wq);
  227. EXPORT_SYMBOL_GPL(system_unbound_wq);
  228. #define CREATE_TRACE_POINTS
  229. #include <trace/events/workqueue.h>
  230. #define for_each_busy_worker(worker, i, pos, gcwq) \
  231. for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
  232. hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
  233. static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
  234. unsigned int sw)
  235. {
  236. if (cpu < nr_cpu_ids) {
  237. if (sw & 1) {
  238. cpu = cpumask_next(cpu, mask);
  239. if (cpu < nr_cpu_ids)
  240. return cpu;
  241. }
  242. if (sw & 2)
  243. return WORK_CPU_UNBOUND;
  244. }
  245. return WORK_CPU_NONE;
  246. }
  247. static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
  248. struct workqueue_struct *wq)
  249. {
  250. return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
  251. }
  252. /*
  253. * CPU iterators
  254. *
  255. * An extra gcwq is defined for an invalid cpu number
  256. * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
  257. * specific CPU. The following iterators are similar to
  258. * for_each_*_cpu() iterators but also considers the unbound gcwq.
  259. *
  260. * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
  261. * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
  262. * for_each_cwq_cpu() : possible CPUs for bound workqueues,
  263. * WORK_CPU_UNBOUND for unbound workqueues
  264. */
  265. #define for_each_gcwq_cpu(cpu) \
  266. for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
  267. (cpu) < WORK_CPU_NONE; \
  268. (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
  269. #define for_each_online_gcwq_cpu(cpu) \
  270. for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
  271. (cpu) < WORK_CPU_NONE; \
  272. (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
  273. #define for_each_cwq_cpu(cpu, wq) \
  274. for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
  275. (cpu) < WORK_CPU_NONE; \
  276. (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
  277. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  278. static struct debug_obj_descr work_debug_descr;
  279. /*
  280. * fixup_init is called when:
  281. * - an active object is initialized
  282. */
  283. static int work_fixup_init(void *addr, enum debug_obj_state state)
  284. {
  285. struct work_struct *work = addr;
  286. switch (state) {
  287. case ODEBUG_STATE_ACTIVE:
  288. cancel_work_sync(work);
  289. debug_object_init(work, &work_debug_descr);
  290. return 1;
  291. default:
  292. return 0;
  293. }
  294. }
  295. /*
  296. * fixup_activate is called when:
  297. * - an active object is activated
  298. * - an unknown object is activated (might be a statically initialized object)
  299. */
  300. static int work_fixup_activate(void *addr, enum debug_obj_state state)
  301. {
  302. struct work_struct *work = addr;
  303. switch (state) {
  304. case ODEBUG_STATE_NOTAVAILABLE:
  305. /*
  306. * This is not really a fixup. The work struct was
  307. * statically initialized. We just make sure that it
  308. * is tracked in the object tracker.
  309. */
  310. if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
  311. debug_object_init(work, &work_debug_descr);
  312. debug_object_activate(work, &work_debug_descr);
  313. return 0;
  314. }
  315. WARN_ON_ONCE(1);
  316. return 0;
  317. case ODEBUG_STATE_ACTIVE:
  318. WARN_ON(1);
  319. default:
  320. return 0;
  321. }
  322. }
  323. /*
  324. * fixup_free is called when:
  325. * - an active object is freed
  326. */
  327. static int work_fixup_free(void *addr, enum debug_obj_state state)
  328. {
  329. struct work_struct *work = addr;
  330. switch (state) {
  331. case ODEBUG_STATE_ACTIVE:
  332. cancel_work_sync(work);
  333. debug_object_free(work, &work_debug_descr);
  334. return 1;
  335. default:
  336. return 0;
  337. }
  338. }
  339. static struct debug_obj_descr work_debug_descr = {
  340. .name = "work_struct",
  341. .fixup_init = work_fixup_init,
  342. .fixup_activate = work_fixup_activate,
  343. .fixup_free = work_fixup_free,
  344. };
  345. static inline void debug_work_activate(struct work_struct *work)
  346. {
  347. debug_object_activate(work, &work_debug_descr);
  348. }
  349. static inline void debug_work_deactivate(struct work_struct *work)
  350. {
  351. debug_object_deactivate(work, &work_debug_descr);
  352. }
  353. void __init_work(struct work_struct *work, int onstack)
  354. {
  355. if (onstack)
  356. debug_object_init_on_stack(work, &work_debug_descr);
  357. else
  358. debug_object_init(work, &work_debug_descr);
  359. }
  360. EXPORT_SYMBOL_GPL(__init_work);
  361. void destroy_work_on_stack(struct work_struct *work)
  362. {
  363. debug_object_free(work, &work_debug_descr);
  364. }
  365. EXPORT_SYMBOL_GPL(destroy_work_on_stack);
  366. #else
  367. static inline void debug_work_activate(struct work_struct *work) { }
  368. static inline void debug_work_deactivate(struct work_struct *work) { }
  369. #endif
  370. /* Serializes the accesses to the list of workqueues. */
  371. static DEFINE_SPINLOCK(workqueue_lock);
  372. static LIST_HEAD(workqueues);
  373. static bool workqueue_freezing; /* W: have wqs started freezing? */
  374. /*
  375. * The almighty global cpu workqueues. nr_running is the only field
  376. * which is expected to be used frequently by other cpus via
  377. * try_to_wake_up(). Put it in a separate cacheline.
  378. */
  379. static DEFINE_PER_CPU(struct global_cwq, global_cwq);
  380. static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
  381. /*
  382. * Global cpu workqueue and nr_running counter for unbound gcwq. The
  383. * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
  384. * workers have WORKER_UNBOUND set.
  385. */
  386. static struct global_cwq unbound_global_cwq;
  387. static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0); /* always 0 */
  388. static int worker_thread(void *__worker);
  389. static struct global_cwq *get_gcwq(unsigned int cpu)
  390. {
  391. if (cpu != WORK_CPU_UNBOUND)
  392. return &per_cpu(global_cwq, cpu);
  393. else
  394. return &unbound_global_cwq;
  395. }
  396. static atomic_t *get_gcwq_nr_running(unsigned int cpu)
  397. {
  398. if (cpu != WORK_CPU_UNBOUND)
  399. return &per_cpu(gcwq_nr_running, cpu);
  400. else
  401. return &unbound_gcwq_nr_running;
  402. }
  403. static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
  404. struct workqueue_struct *wq)
  405. {
  406. if (!(wq->flags & WQ_UNBOUND)) {
  407. if (likely(cpu < nr_cpu_ids)) {
  408. #ifdef CONFIG_SMP
  409. return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
  410. #else
  411. return wq->cpu_wq.single;
  412. #endif
  413. }
  414. } else if (likely(cpu == WORK_CPU_UNBOUND))
  415. return wq->cpu_wq.single;
  416. return NULL;
  417. }
  418. static unsigned int work_color_to_flags(int color)
  419. {
  420. return color << WORK_STRUCT_COLOR_SHIFT;
  421. }
  422. static int get_work_color(struct work_struct *work)
  423. {
  424. return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
  425. ((1 << WORK_STRUCT_COLOR_BITS) - 1);
  426. }
  427. static int work_next_color(int color)
  428. {
  429. return (color + 1) % WORK_NR_COLORS;
  430. }
  431. /*
  432. * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
  433. * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
  434. * cleared and the work data contains the cpu number it was last on.
  435. *
  436. * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
  437. * cwq, cpu or clear work->data. These functions should only be
  438. * called while the work is owned - ie. while the PENDING bit is set.
  439. *
  440. * get_work_[g]cwq() can be used to obtain the gcwq or cwq
  441. * corresponding to a work. gcwq is available once the work has been
  442. * queued anywhere after initialization. cwq is available only from
  443. * queueing until execution starts.
  444. */
  445. static inline void set_work_data(struct work_struct *work, unsigned long data,
  446. unsigned long flags)
  447. {
  448. BUG_ON(!work_pending(work));
  449. atomic_long_set(&work->data, data | flags | work_static(work));
  450. }
  451. static void set_work_cwq(struct work_struct *work,
  452. struct cpu_workqueue_struct *cwq,
  453. unsigned long extra_flags)
  454. {
  455. set_work_data(work, (unsigned long)cwq,
  456. WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
  457. }
  458. static void set_work_cpu(struct work_struct *work, unsigned int cpu)
  459. {
  460. set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
  461. }
  462. static void clear_work_data(struct work_struct *work)
  463. {
  464. set_work_data(work, WORK_STRUCT_NO_CPU, 0);
  465. }
  466. static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
  467. {
  468. unsigned long data = atomic_long_read(&work->data);
  469. if (data & WORK_STRUCT_CWQ)
  470. return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
  471. else
  472. return NULL;
  473. }
  474. static struct global_cwq *get_work_gcwq(struct work_struct *work)
  475. {
  476. unsigned long data = atomic_long_read(&work->data);
  477. unsigned int cpu;
  478. if (data & WORK_STRUCT_CWQ)
  479. return ((struct cpu_workqueue_struct *)
  480. (data & WORK_STRUCT_WQ_DATA_MASK))->gcwq;
  481. cpu = data >> WORK_STRUCT_FLAG_BITS;
  482. if (cpu == WORK_CPU_NONE)
  483. return NULL;
  484. BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
  485. return get_gcwq(cpu);
  486. }
  487. /*
  488. * Policy functions. These define the policies on how the global
  489. * worker pool is managed. Unless noted otherwise, these functions
  490. * assume that they're being called with gcwq->lock held.
  491. */
  492. static bool __need_more_worker(struct global_cwq *gcwq)
  493. {
  494. return !atomic_read(get_gcwq_nr_running(gcwq->cpu)) ||
  495. gcwq->flags & GCWQ_HIGHPRI_PENDING;
  496. }
  497. /*
  498. * Need to wake up a worker? Called from anything but currently
  499. * running workers.
  500. */
  501. static bool need_more_worker(struct global_cwq *gcwq)
  502. {
  503. return !list_empty(&gcwq->worklist) && __need_more_worker(gcwq);
  504. }
  505. /* Can I start working? Called from busy but !running workers. */
  506. static bool may_start_working(struct global_cwq *gcwq)
  507. {
  508. return gcwq->nr_idle;
  509. }
  510. /* Do I need to keep working? Called from currently running workers. */
  511. static bool keep_working(struct global_cwq *gcwq)
  512. {
  513. atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
  514. return !list_empty(&gcwq->worklist) &&
  515. (atomic_read(nr_running) <= 1 ||
  516. gcwq->flags & GCWQ_HIGHPRI_PENDING);
  517. }
  518. /* Do we need a new worker? Called from manager. */
  519. static bool need_to_create_worker(struct global_cwq *gcwq)
  520. {
  521. return need_more_worker(gcwq) && !may_start_working(gcwq);
  522. }
  523. /* Do I need to be the manager? */
  524. static bool need_to_manage_workers(struct global_cwq *gcwq)
  525. {
  526. return need_to_create_worker(gcwq) || gcwq->flags & GCWQ_MANAGE_WORKERS;
  527. }
  528. /* Do we have too many workers and should some go away? */
  529. static bool too_many_workers(struct global_cwq *gcwq)
  530. {
  531. bool managing = gcwq->flags & GCWQ_MANAGING_WORKERS;
  532. int nr_idle = gcwq->nr_idle + managing; /* manager is considered idle */
  533. int nr_busy = gcwq->nr_workers - nr_idle;
  534. return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
  535. }
  536. /*
  537. * Wake up functions.
  538. */
  539. /* Return the first worker. Safe with preemption disabled */
  540. static struct worker *first_worker(struct global_cwq *gcwq)
  541. {
  542. if (unlikely(list_empty(&gcwq->idle_list)))
  543. return NULL;
  544. return list_first_entry(&gcwq->idle_list, struct worker, entry);
  545. }
  546. /**
  547. * wake_up_worker - wake up an idle worker
  548. * @gcwq: gcwq to wake worker for
  549. *
  550. * Wake up the first idle worker of @gcwq.
  551. *
  552. * CONTEXT:
  553. * spin_lock_irq(gcwq->lock).
  554. */
  555. static void wake_up_worker(struct global_cwq *gcwq)
  556. {
  557. struct worker *worker = first_worker(gcwq);
  558. if (likely(worker))
  559. wake_up_process(worker->task);
  560. }
  561. /**
  562. * wq_worker_waking_up - a worker is waking up
  563. * @task: task waking up
  564. * @cpu: CPU @task is waking up to
  565. *
  566. * This function is called during try_to_wake_up() when a worker is
  567. * being awoken.
  568. *
  569. * CONTEXT:
  570. * spin_lock_irq(rq->lock)
  571. */
  572. void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
  573. {
  574. struct worker *worker = kthread_data(task);
  575. if (likely(!(worker->flags & WORKER_NOT_RUNNING)))
  576. atomic_inc(get_gcwq_nr_running(cpu));
  577. }
  578. /**
  579. * wq_worker_sleeping - a worker is going to sleep
  580. * @task: task going to sleep
  581. * @cpu: CPU in question, must be the current CPU number
  582. *
  583. * This function is called during schedule() when a busy worker is
  584. * going to sleep. Worker on the same cpu can be woken up by
  585. * returning pointer to its task.
  586. *
  587. * CONTEXT:
  588. * spin_lock_irq(rq->lock)
  589. *
  590. * RETURNS:
  591. * Worker task on @cpu to wake up, %NULL if none.
  592. */
  593. struct task_struct *wq_worker_sleeping(struct task_struct *task,
  594. unsigned int cpu)
  595. {
  596. struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  597. struct global_cwq *gcwq = get_gcwq(cpu);
  598. atomic_t *nr_running = get_gcwq_nr_running(cpu);
  599. if (unlikely(worker->flags & WORKER_NOT_RUNNING))
  600. return NULL;
  601. /* this can only happen on the local cpu */
  602. BUG_ON(cpu != raw_smp_processor_id());
  603. /*
  604. * The counterpart of the following dec_and_test, implied mb,
  605. * worklist not empty test sequence is in insert_work().
  606. * Please read comment there.
  607. *
  608. * NOT_RUNNING is clear. This means that trustee is not in
  609. * charge and we're running on the local cpu w/ rq lock held
  610. * and preemption disabled, which in turn means that none else
  611. * could be manipulating idle_list, so dereferencing idle_list
  612. * without gcwq lock is safe.
  613. */
  614. if (atomic_dec_and_test(nr_running) && !list_empty(&gcwq->worklist))
  615. to_wakeup = first_worker(gcwq);
  616. return to_wakeup ? to_wakeup->task : NULL;
  617. }
  618. /**
  619. * worker_set_flags - set worker flags and adjust nr_running accordingly
  620. * @worker: self
  621. * @flags: flags to set
  622. * @wakeup: wakeup an idle worker if necessary
  623. *
  624. * Set @flags in @worker->flags and adjust nr_running accordingly. If
  625. * nr_running becomes zero and @wakeup is %true, an idle worker is
  626. * woken up.
  627. *
  628. * CONTEXT:
  629. * spin_lock_irq(gcwq->lock)
  630. */
  631. static inline void worker_set_flags(struct worker *worker, unsigned int flags,
  632. bool wakeup)
  633. {
  634. struct global_cwq *gcwq = worker->gcwq;
  635. WARN_ON_ONCE(worker->task != current);
  636. /*
  637. * If transitioning into NOT_RUNNING, adjust nr_running and
  638. * wake up an idle worker as necessary if requested by
  639. * @wakeup.
  640. */
  641. if ((flags & WORKER_NOT_RUNNING) &&
  642. !(worker->flags & WORKER_NOT_RUNNING)) {
  643. atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
  644. if (wakeup) {
  645. if (atomic_dec_and_test(nr_running) &&
  646. !list_empty(&gcwq->worklist))
  647. wake_up_worker(gcwq);
  648. } else
  649. atomic_dec(nr_running);
  650. }
  651. worker->flags |= flags;
  652. }
  653. /**
  654. * worker_clr_flags - clear worker flags and adjust nr_running accordingly
  655. * @worker: self
  656. * @flags: flags to clear
  657. *
  658. * Clear @flags in @worker->flags and adjust nr_running accordingly.
  659. *
  660. * CONTEXT:
  661. * spin_lock_irq(gcwq->lock)
  662. */
  663. static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
  664. {
  665. struct global_cwq *gcwq = worker->gcwq;
  666. unsigned int oflags = worker->flags;
  667. WARN_ON_ONCE(worker->task != current);
  668. worker->flags &= ~flags;
  669. /* if transitioning out of NOT_RUNNING, increment nr_running */
  670. if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
  671. if (!(worker->flags & WORKER_NOT_RUNNING))
  672. atomic_inc(get_gcwq_nr_running(gcwq->cpu));
  673. }
  674. /**
  675. * busy_worker_head - return the busy hash head for a work
  676. * @gcwq: gcwq of interest
  677. * @work: work to be hashed
  678. *
  679. * Return hash head of @gcwq for @work.
  680. *
  681. * CONTEXT:
  682. * spin_lock_irq(gcwq->lock).
  683. *
  684. * RETURNS:
  685. * Pointer to the hash head.
  686. */
  687. static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
  688. struct work_struct *work)
  689. {
  690. const int base_shift = ilog2(sizeof(struct work_struct));
  691. unsigned long v = (unsigned long)work;
  692. /* simple shift and fold hash, do we need something better? */
  693. v >>= base_shift;
  694. v += v >> BUSY_WORKER_HASH_ORDER;
  695. v &= BUSY_WORKER_HASH_MASK;
  696. return &gcwq->busy_hash[v];
  697. }
  698. /**
  699. * __find_worker_executing_work - find worker which is executing a work
  700. * @gcwq: gcwq of interest
  701. * @bwh: hash head as returned by busy_worker_head()
  702. * @work: work to find worker for
  703. *
  704. * Find a worker which is executing @work on @gcwq. @bwh should be
  705. * the hash head obtained by calling busy_worker_head() with the same
  706. * work.
  707. *
  708. * CONTEXT:
  709. * spin_lock_irq(gcwq->lock).
  710. *
  711. * RETURNS:
  712. * Pointer to worker which is executing @work if found, NULL
  713. * otherwise.
  714. */
  715. static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
  716. struct hlist_head *bwh,
  717. struct work_struct *work)
  718. {
  719. struct worker *worker;
  720. struct hlist_node *tmp;
  721. hlist_for_each_entry(worker, tmp, bwh, hentry)
  722. if (worker->current_work == work)
  723. return worker;
  724. return NULL;
  725. }
  726. /**
  727. * find_worker_executing_work - find worker which is executing a work
  728. * @gcwq: gcwq of interest
  729. * @work: work to find worker for
  730. *
  731. * Find a worker which is executing @work on @gcwq. This function is
  732. * identical to __find_worker_executing_work() except that this
  733. * function calculates @bwh itself.
  734. *
  735. * CONTEXT:
  736. * spin_lock_irq(gcwq->lock).
  737. *
  738. * RETURNS:
  739. * Pointer to worker which is executing @work if found, NULL
  740. * otherwise.
  741. */
  742. static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
  743. struct work_struct *work)
  744. {
  745. return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
  746. work);
  747. }
  748. /**
  749. * gcwq_determine_ins_pos - find insertion position
  750. * @gcwq: gcwq of interest
  751. * @cwq: cwq a work is being queued for
  752. *
  753. * A work for @cwq is about to be queued on @gcwq, determine insertion
  754. * position for the work. If @cwq is for HIGHPRI wq, the work is
  755. * queued at the head of the queue but in FIFO order with respect to
  756. * other HIGHPRI works; otherwise, at the end of the queue. This
  757. * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
  758. * there are HIGHPRI works pending.
  759. *
  760. * CONTEXT:
  761. * spin_lock_irq(gcwq->lock).
  762. *
  763. * RETURNS:
  764. * Pointer to inserstion position.
  765. */
  766. static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
  767. struct cpu_workqueue_struct *cwq)
  768. {
  769. struct work_struct *twork;
  770. if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
  771. return &gcwq->worklist;
  772. list_for_each_entry(twork, &gcwq->worklist, entry) {
  773. struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
  774. if (!(tcwq->wq->flags & WQ_HIGHPRI))
  775. break;
  776. }
  777. gcwq->flags |= GCWQ_HIGHPRI_PENDING;
  778. return &twork->entry;
  779. }
  780. /**
  781. * insert_work - insert a work into gcwq
  782. * @cwq: cwq @work belongs to
  783. * @work: work to insert
  784. * @head: insertion point
  785. * @extra_flags: extra WORK_STRUCT_* flags to set
  786. *
  787. * Insert @work which belongs to @cwq into @gcwq after @head.
  788. * @extra_flags is or'd to work_struct flags.
  789. *
  790. * CONTEXT:
  791. * spin_lock_irq(gcwq->lock).
  792. */
  793. static void insert_work(struct cpu_workqueue_struct *cwq,
  794. struct work_struct *work, struct list_head *head,
  795. unsigned int extra_flags)
  796. {
  797. struct global_cwq *gcwq = cwq->gcwq;
  798. /* we own @work, set data and link */
  799. set_work_cwq(work, cwq, extra_flags);
  800. /*
  801. * Ensure that we get the right work->data if we see the
  802. * result of list_add() below, see try_to_grab_pending().
  803. */
  804. smp_wmb();
  805. list_add_tail(&work->entry, head);
  806. /*
  807. * Ensure either worker_sched_deactivated() sees the above
  808. * list_add_tail() or we see zero nr_running to avoid workers
  809. * lying around lazily while there are works to be processed.
  810. */
  811. smp_mb();
  812. if (__need_more_worker(gcwq))
  813. wake_up_worker(gcwq);
  814. }
  815. static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
  816. struct work_struct *work)
  817. {
  818. struct global_cwq *gcwq;
  819. struct cpu_workqueue_struct *cwq;
  820. struct list_head *worklist;
  821. unsigned int work_flags;
  822. unsigned long flags;
  823. debug_work_activate(work);
  824. if (WARN_ON_ONCE(wq->flags & WQ_DYING))
  825. return;
  826. /* determine gcwq to use */
  827. if (!(wq->flags & WQ_UNBOUND)) {
  828. struct global_cwq *last_gcwq;
  829. if (unlikely(cpu == WORK_CPU_UNBOUND))
  830. cpu = raw_smp_processor_id();
  831. /*
  832. * It's multi cpu. If @wq is non-reentrant and @work
  833. * was previously on a different cpu, it might still
  834. * be running there, in which case the work needs to
  835. * be queued on that cpu to guarantee non-reentrance.
  836. */
  837. gcwq = get_gcwq(cpu);
  838. if (wq->flags & WQ_NON_REENTRANT &&
  839. (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
  840. struct worker *worker;
  841. spin_lock_irqsave(&last_gcwq->lock, flags);
  842. worker = find_worker_executing_work(last_gcwq, work);
  843. if (worker && worker->current_cwq->wq == wq)
  844. gcwq = last_gcwq;
  845. else {
  846. /* meh... not running there, queue here */
  847. spin_unlock_irqrestore(&last_gcwq->lock, flags);
  848. spin_lock_irqsave(&gcwq->lock, flags);
  849. }
  850. } else
  851. spin_lock_irqsave(&gcwq->lock, flags);
  852. } else {
  853. gcwq = get_gcwq(WORK_CPU_UNBOUND);
  854. spin_lock_irqsave(&gcwq->lock, flags);
  855. }
  856. /* gcwq determined, get cwq and queue */
  857. cwq = get_cwq(gcwq->cpu, wq);
  858. trace_workqueue_queue_work(cpu, cwq, work);
  859. BUG_ON(!list_empty(&work->entry));
  860. cwq->nr_in_flight[cwq->work_color]++;
  861. work_flags = work_color_to_flags(cwq->work_color);
  862. if (likely(cwq->nr_active < cwq->max_active)) {
  863. trace_workqueue_activate_work(work);
  864. cwq->nr_active++;
  865. worklist = gcwq_determine_ins_pos(gcwq, cwq);
  866. } else {
  867. work_flags |= WORK_STRUCT_DELAYED;
  868. worklist = &cwq->delayed_works;
  869. }
  870. insert_work(cwq, work, worklist, work_flags);
  871. spin_unlock_irqrestore(&gcwq->lock, flags);
  872. }
  873. /**
  874. * queue_work - queue work on a workqueue
  875. * @wq: workqueue to use
  876. * @work: work to queue
  877. *
  878. * Returns 0 if @work was already on a queue, non-zero otherwise.
  879. *
  880. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  881. * it can be processed by another CPU.
  882. */
  883. int queue_work(struct workqueue_struct *wq, struct work_struct *work)
  884. {
  885. int ret;
  886. ret = queue_work_on(get_cpu(), wq, work);
  887. put_cpu();
  888. return ret;
  889. }
  890. EXPORT_SYMBOL_GPL(queue_work);
  891. /**
  892. * queue_work_on - queue work on specific cpu
  893. * @cpu: CPU number to execute work on
  894. * @wq: workqueue to use
  895. * @work: work to queue
  896. *
  897. * Returns 0 if @work was already on a queue, non-zero otherwise.
  898. *
  899. * We queue the work to a specific CPU, the caller must ensure it
  900. * can't go away.
  901. */
  902. int
  903. queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
  904. {
  905. int ret = 0;
  906. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  907. __queue_work(cpu, wq, work);
  908. ret = 1;
  909. }
  910. return ret;
  911. }
  912. EXPORT_SYMBOL_GPL(queue_work_on);
  913. static void delayed_work_timer_fn(unsigned long __data)
  914. {
  915. struct delayed_work *dwork = (struct delayed_work *)__data;
  916. struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
  917. __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
  918. }
  919. /**
  920. * queue_delayed_work - queue work on a workqueue after delay
  921. * @wq: workqueue to use
  922. * @dwork: delayable work to queue
  923. * @delay: number of jiffies to wait before queueing
  924. *
  925. * Returns 0 if @work was already on a queue, non-zero otherwise.
  926. */
  927. int queue_delayed_work(struct workqueue_struct *wq,
  928. struct delayed_work *dwork, unsigned long delay)
  929. {
  930. if (delay == 0)
  931. return queue_work(wq, &dwork->work);
  932. return queue_delayed_work_on(-1, wq, dwork, delay);
  933. }
  934. EXPORT_SYMBOL_GPL(queue_delayed_work);
  935. /**
  936. * queue_delayed_work_on - queue work on specific CPU after delay
  937. * @cpu: CPU number to execute work on
  938. * @wq: workqueue to use
  939. * @dwork: work to queue
  940. * @delay: number of jiffies to wait before queueing
  941. *
  942. * Returns 0 if @work was already on a queue, non-zero otherwise.
  943. */
  944. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  945. struct delayed_work *dwork, unsigned long delay)
  946. {
  947. int ret = 0;
  948. struct timer_list *timer = &dwork->timer;
  949. struct work_struct *work = &dwork->work;
  950. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  951. unsigned int lcpu;
  952. BUG_ON(timer_pending(timer));
  953. BUG_ON(!list_empty(&work->entry));
  954. timer_stats_timer_set_start_info(&dwork->timer);
  955. /*
  956. * This stores cwq for the moment, for the timer_fn.
  957. * Note that the work's gcwq is preserved to allow
  958. * reentrance detection for delayed works.
  959. */
  960. if (!(wq->flags & WQ_UNBOUND)) {
  961. struct global_cwq *gcwq = get_work_gcwq(work);
  962. if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
  963. lcpu = gcwq->cpu;
  964. else
  965. lcpu = raw_smp_processor_id();
  966. } else
  967. lcpu = WORK_CPU_UNBOUND;
  968. set_work_cwq(work, get_cwq(lcpu, wq), 0);
  969. timer->expires = jiffies + delay;
  970. timer->data = (unsigned long)dwork;
  971. timer->function = delayed_work_timer_fn;
  972. if (unlikely(cpu >= 0))
  973. add_timer_on(timer, cpu);
  974. else
  975. add_timer(timer);
  976. ret = 1;
  977. }
  978. return ret;
  979. }
  980. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  981. /**
  982. * worker_enter_idle - enter idle state
  983. * @worker: worker which is entering idle state
  984. *
  985. * @worker is entering idle state. Update stats and idle timer if
  986. * necessary.
  987. *
  988. * LOCKING:
  989. * spin_lock_irq(gcwq->lock).
  990. */
  991. static void worker_enter_idle(struct worker *worker)
  992. {
  993. struct global_cwq *gcwq = worker->gcwq;
  994. BUG_ON(worker->flags & WORKER_IDLE);
  995. BUG_ON(!list_empty(&worker->entry) &&
  996. (worker->hentry.next || worker->hentry.pprev));
  997. /* can't use worker_set_flags(), also called from start_worker() */
  998. worker->flags |= WORKER_IDLE;
  999. gcwq->nr_idle++;
  1000. worker->last_active = jiffies;
  1001. /* idle_list is LIFO */
  1002. list_add(&worker->entry, &gcwq->idle_list);
  1003. if (likely(!(worker->flags & WORKER_ROGUE))) {
  1004. if (too_many_workers(gcwq) && !timer_pending(&gcwq->idle_timer))
  1005. mod_timer(&gcwq->idle_timer,
  1006. jiffies + IDLE_WORKER_TIMEOUT);
  1007. } else
  1008. wake_up_all(&gcwq->trustee_wait);
  1009. /* sanity check nr_running */
  1010. WARN_ON_ONCE(gcwq->nr_workers == gcwq->nr_idle &&
  1011. atomic_read(get_gcwq_nr_running(gcwq->cpu)));
  1012. }
  1013. /**
  1014. * worker_leave_idle - leave idle state
  1015. * @worker: worker which is leaving idle state
  1016. *
  1017. * @worker is leaving idle state. Update stats.
  1018. *
  1019. * LOCKING:
  1020. * spin_lock_irq(gcwq->lock).
  1021. */
  1022. static void worker_leave_idle(struct worker *worker)
  1023. {
  1024. struct global_cwq *gcwq = worker->gcwq;
  1025. BUG_ON(!(worker->flags & WORKER_IDLE));
  1026. worker_clr_flags(worker, WORKER_IDLE);
  1027. gcwq->nr_idle--;
  1028. list_del_init(&worker->entry);
  1029. }
  1030. /**
  1031. * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
  1032. * @worker: self
  1033. *
  1034. * Works which are scheduled while the cpu is online must at least be
  1035. * scheduled to a worker which is bound to the cpu so that if they are
  1036. * flushed from cpu callbacks while cpu is going down, they are
  1037. * guaranteed to execute on the cpu.
  1038. *
  1039. * This function is to be used by rogue workers and rescuers to bind
  1040. * themselves to the target cpu and may race with cpu going down or
  1041. * coming online. kthread_bind() can't be used because it may put the
  1042. * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
  1043. * verbatim as it's best effort and blocking and gcwq may be
  1044. * [dis]associated in the meantime.
  1045. *
  1046. * This function tries set_cpus_allowed() and locks gcwq and verifies
  1047. * the binding against GCWQ_DISASSOCIATED which is set during
  1048. * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
  1049. * idle state or fetches works without dropping lock, it can guarantee
  1050. * the scheduling requirement described in the first paragraph.
  1051. *
  1052. * CONTEXT:
  1053. * Might sleep. Called without any lock but returns with gcwq->lock
  1054. * held.
  1055. *
  1056. * RETURNS:
  1057. * %true if the associated gcwq is online (@worker is successfully
  1058. * bound), %false if offline.
  1059. */
  1060. static bool worker_maybe_bind_and_lock(struct worker *worker)
  1061. __acquires(&gcwq->lock)
  1062. {
  1063. struct global_cwq *gcwq = worker->gcwq;
  1064. struct task_struct *task = worker->task;
  1065. while (true) {
  1066. /*
  1067. * The following call may fail, succeed or succeed
  1068. * without actually migrating the task to the cpu if
  1069. * it races with cpu hotunplug operation. Verify
  1070. * against GCWQ_DISASSOCIATED.
  1071. */
  1072. if (!(gcwq->flags & GCWQ_DISASSOCIATED))
  1073. set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
  1074. spin_lock_irq(&gcwq->lock);
  1075. if (gcwq->flags & GCWQ_DISASSOCIATED)
  1076. return false;
  1077. if (task_cpu(task) == gcwq->cpu &&
  1078. cpumask_equal(&current->cpus_allowed,
  1079. get_cpu_mask(gcwq->cpu)))
  1080. return true;
  1081. spin_unlock_irq(&gcwq->lock);
  1082. /* CPU has come up inbetween, retry migration */
  1083. cpu_relax();
  1084. }
  1085. }
  1086. /*
  1087. * Function for worker->rebind_work used to rebind rogue busy workers
  1088. * to the associated cpu which is coming back online. This is
  1089. * scheduled by cpu up but can race with other cpu hotplug operations
  1090. * and may be executed twice without intervening cpu down.
  1091. */
  1092. static void worker_rebind_fn(struct work_struct *work)
  1093. {
  1094. struct worker *worker = container_of(work, struct worker, rebind_work);
  1095. struct global_cwq *gcwq = worker->gcwq;
  1096. if (worker_maybe_bind_and_lock(worker))
  1097. worker_clr_flags(worker, WORKER_REBIND);
  1098. spin_unlock_irq(&gcwq->lock);
  1099. }
  1100. static struct worker *alloc_worker(void)
  1101. {
  1102. struct worker *worker;
  1103. worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  1104. if (worker) {
  1105. INIT_LIST_HEAD(&worker->entry);
  1106. INIT_LIST_HEAD(&worker->scheduled);
  1107. INIT_WORK(&worker->rebind_work, worker_rebind_fn);
  1108. /* on creation a worker is in !idle && prep state */
  1109. worker->flags = WORKER_PREP;
  1110. }
  1111. return worker;
  1112. }
  1113. /**
  1114. * create_worker - create a new workqueue worker
  1115. * @gcwq: gcwq the new worker will belong to
  1116. * @bind: whether to set affinity to @cpu or not
  1117. *
  1118. * Create a new worker which is bound to @gcwq. The returned worker
  1119. * can be started by calling start_worker() or destroyed using
  1120. * destroy_worker().
  1121. *
  1122. * CONTEXT:
  1123. * Might sleep. Does GFP_KERNEL allocations.
  1124. *
  1125. * RETURNS:
  1126. * Pointer to the newly created worker.
  1127. */
  1128. static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
  1129. {
  1130. bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
  1131. struct worker *worker = NULL;
  1132. int id = -1;
  1133. spin_lock_irq(&gcwq->lock);
  1134. while (ida_get_new(&gcwq->worker_ida, &id)) {
  1135. spin_unlock_irq(&gcwq->lock);
  1136. if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
  1137. goto fail;
  1138. spin_lock_irq(&gcwq->lock);
  1139. }
  1140. spin_unlock_irq(&gcwq->lock);
  1141. worker = alloc_worker();
  1142. if (!worker)
  1143. goto fail;
  1144. worker->gcwq = gcwq;
  1145. worker->id = id;
  1146. if (!on_unbound_cpu)
  1147. worker->task = kthread_create(worker_thread, worker,
  1148. "kworker/%u:%d", gcwq->cpu, id);
  1149. else
  1150. worker->task = kthread_create(worker_thread, worker,
  1151. "kworker/u:%d", id);
  1152. if (IS_ERR(worker->task))
  1153. goto fail;
  1154. /*
  1155. * A rogue worker will become a regular one if CPU comes
  1156. * online later on. Make sure every worker has
  1157. * PF_THREAD_BOUND set.
  1158. */
  1159. if (bind && !on_unbound_cpu)
  1160. kthread_bind(worker->task, gcwq->cpu);
  1161. else {
  1162. worker->task->flags |= PF_THREAD_BOUND;
  1163. if (on_unbound_cpu)
  1164. worker->flags |= WORKER_UNBOUND;
  1165. }
  1166. return worker;
  1167. fail:
  1168. if (id >= 0) {
  1169. spin_lock_irq(&gcwq->lock);
  1170. ida_remove(&gcwq->worker_ida, id);
  1171. spin_unlock_irq(&gcwq->lock);
  1172. }
  1173. kfree(worker);
  1174. return NULL;
  1175. }
  1176. /**
  1177. * start_worker - start a newly created worker
  1178. * @worker: worker to start
  1179. *
  1180. * Make the gcwq aware of @worker and start it.
  1181. *
  1182. * CONTEXT:
  1183. * spin_lock_irq(gcwq->lock).
  1184. */
  1185. static void start_worker(struct worker *worker)
  1186. {
  1187. worker->flags |= WORKER_STARTED;
  1188. worker->gcwq->nr_workers++;
  1189. worker_enter_idle(worker);
  1190. wake_up_process(worker->task);
  1191. }
  1192. /**
  1193. * destroy_worker - destroy a workqueue worker
  1194. * @worker: worker to be destroyed
  1195. *
  1196. * Destroy @worker and adjust @gcwq stats accordingly.
  1197. *
  1198. * CONTEXT:
  1199. * spin_lock_irq(gcwq->lock) which is released and regrabbed.
  1200. */
  1201. static void destroy_worker(struct worker *worker)
  1202. {
  1203. struct global_cwq *gcwq = worker->gcwq;
  1204. int id = worker->id;
  1205. /* sanity check frenzy */
  1206. BUG_ON(worker->current_work);
  1207. BUG_ON(!list_empty(&worker->scheduled));
  1208. if (worker->flags & WORKER_STARTED)
  1209. gcwq->nr_workers--;
  1210. if (worker->flags & WORKER_IDLE)
  1211. gcwq->nr_idle--;
  1212. list_del_init(&worker->entry);
  1213. worker->flags |= WORKER_DIE;
  1214. spin_unlock_irq(&gcwq->lock);
  1215. kthread_stop(worker->task);
  1216. kfree(worker);
  1217. spin_lock_irq(&gcwq->lock);
  1218. ida_remove(&gcwq->worker_ida, id);
  1219. }
  1220. static void idle_worker_timeout(unsigned long __gcwq)
  1221. {
  1222. struct global_cwq *gcwq = (void *)__gcwq;
  1223. spin_lock_irq(&gcwq->lock);
  1224. if (too_many_workers(gcwq)) {
  1225. struct worker *worker;
  1226. unsigned long expires;
  1227. /* idle_list is kept in LIFO order, check the last one */
  1228. worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
  1229. expires = worker->last_active + IDLE_WORKER_TIMEOUT;
  1230. if (time_before(jiffies, expires))
  1231. mod_timer(&gcwq->idle_timer, expires);
  1232. else {
  1233. /* it's been idle for too long, wake up manager */
  1234. gcwq->flags |= GCWQ_MANAGE_WORKERS;
  1235. wake_up_worker(gcwq);
  1236. }
  1237. }
  1238. spin_unlock_irq(&gcwq->lock);
  1239. }
  1240. static bool send_mayday(struct work_struct *work)
  1241. {
  1242. struct cpu_workqueue_struct *cwq = get_work_cwq(work);
  1243. struct workqueue_struct *wq = cwq->wq;
  1244. unsigned int cpu;
  1245. if (!(wq->flags & WQ_RESCUER))
  1246. return false;
  1247. /* mayday mayday mayday */
  1248. cpu = cwq->gcwq->cpu;
  1249. /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
  1250. if (cpu == WORK_CPU_UNBOUND)
  1251. cpu = 0;
  1252. if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
  1253. wake_up_process(wq->rescuer->task);
  1254. return true;
  1255. }
  1256. static void gcwq_mayday_timeout(unsigned long __gcwq)
  1257. {
  1258. struct global_cwq *gcwq = (void *)__gcwq;
  1259. struct work_struct *work;
  1260. spin_lock_irq(&gcwq->lock);
  1261. if (need_to_create_worker(gcwq)) {
  1262. /*
  1263. * We've been trying to create a new worker but
  1264. * haven't been successful. We might be hitting an
  1265. * allocation deadlock. Send distress signals to
  1266. * rescuers.
  1267. */
  1268. list_for_each_entry(work, &gcwq->worklist, entry)
  1269. send_mayday(work);
  1270. }
  1271. spin_unlock_irq(&gcwq->lock);
  1272. mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
  1273. }
  1274. /**
  1275. * maybe_create_worker - create a new worker if necessary
  1276. * @gcwq: gcwq to create a new worker for
  1277. *
  1278. * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
  1279. * have at least one idle worker on return from this function. If
  1280. * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
  1281. * sent to all rescuers with works scheduled on @gcwq to resolve
  1282. * possible allocation deadlock.
  1283. *
  1284. * On return, need_to_create_worker() is guaranteed to be false and
  1285. * may_start_working() true.
  1286. *
  1287. * LOCKING:
  1288. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  1289. * multiple times. Does GFP_KERNEL allocations. Called only from
  1290. * manager.
  1291. *
  1292. * RETURNS:
  1293. * false if no action was taken and gcwq->lock stayed locked, true
  1294. * otherwise.
  1295. */
  1296. static bool maybe_create_worker(struct global_cwq *gcwq)
  1297. __releases(&gcwq->lock)
  1298. __acquires(&gcwq->lock)
  1299. {
  1300. if (!need_to_create_worker(gcwq))
  1301. return false;
  1302. restart:
  1303. spin_unlock_irq(&gcwq->lock);
  1304. /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
  1305. mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
  1306. while (true) {
  1307. struct worker *worker;
  1308. worker = create_worker(gcwq, true);
  1309. if (worker) {
  1310. del_timer_sync(&gcwq->mayday_timer);
  1311. spin_lock_irq(&gcwq->lock);
  1312. start_worker(worker);
  1313. BUG_ON(need_to_create_worker(gcwq));
  1314. return true;
  1315. }
  1316. if (!need_to_create_worker(gcwq))
  1317. break;
  1318. __set_current_state(TASK_INTERRUPTIBLE);
  1319. schedule_timeout(CREATE_COOLDOWN);
  1320. if (!need_to_create_worker(gcwq))
  1321. break;
  1322. }
  1323. del_timer_sync(&gcwq->mayday_timer);
  1324. spin_lock_irq(&gcwq->lock);
  1325. if (need_to_create_worker(gcwq))
  1326. goto restart;
  1327. return true;
  1328. }
  1329. /**
  1330. * maybe_destroy_worker - destroy workers which have been idle for a while
  1331. * @gcwq: gcwq to destroy workers for
  1332. *
  1333. * Destroy @gcwq workers which have been idle for longer than
  1334. * IDLE_WORKER_TIMEOUT.
  1335. *
  1336. * LOCKING:
  1337. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  1338. * multiple times. Called only from manager.
  1339. *
  1340. * RETURNS:
  1341. * false if no action was taken and gcwq->lock stayed locked, true
  1342. * otherwise.
  1343. */
  1344. static bool maybe_destroy_workers(struct global_cwq *gcwq)
  1345. {
  1346. bool ret = false;
  1347. while (too_many_workers(gcwq)) {
  1348. struct worker *worker;
  1349. unsigned long expires;
  1350. worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
  1351. expires = worker->last_active + IDLE_WORKER_TIMEOUT;
  1352. if (time_before(jiffies, expires)) {
  1353. mod_timer(&gcwq->idle_timer, expires);
  1354. break;
  1355. }
  1356. destroy_worker(worker);
  1357. ret = true;
  1358. }
  1359. return ret;
  1360. }
  1361. /**
  1362. * manage_workers - manage worker pool
  1363. * @worker: self
  1364. *
  1365. * Assume the manager role and manage gcwq worker pool @worker belongs
  1366. * to. At any given time, there can be only zero or one manager per
  1367. * gcwq. The exclusion is handled automatically by this function.
  1368. *
  1369. * The caller can safely start processing works on false return. On
  1370. * true return, it's guaranteed that need_to_create_worker() is false
  1371. * and may_start_working() is true.
  1372. *
  1373. * CONTEXT:
  1374. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  1375. * multiple times. Does GFP_KERNEL allocations.
  1376. *
  1377. * RETURNS:
  1378. * false if no action was taken and gcwq->lock stayed locked, true if
  1379. * some action was taken.
  1380. */
  1381. static bool manage_workers(struct worker *worker)
  1382. {
  1383. struct global_cwq *gcwq = worker->gcwq;
  1384. bool ret = false;
  1385. if (gcwq->flags & GCWQ_MANAGING_WORKERS)
  1386. return ret;
  1387. gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
  1388. gcwq->flags |= GCWQ_MANAGING_WORKERS;
  1389. /*
  1390. * Destroy and then create so that may_start_working() is true
  1391. * on return.
  1392. */
  1393. ret |= maybe_destroy_workers(gcwq);
  1394. ret |= maybe_create_worker(gcwq);
  1395. gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
  1396. /*
  1397. * The trustee might be waiting to take over the manager
  1398. * position, tell it we're done.
  1399. */
  1400. if (unlikely(gcwq->trustee))
  1401. wake_up_all(&gcwq->trustee_wait);
  1402. return ret;
  1403. }
  1404. /**
  1405. * move_linked_works - move linked works to a list
  1406. * @work: start of series of works to be scheduled
  1407. * @head: target list to append @work to
  1408. * @nextp: out paramter for nested worklist walking
  1409. *
  1410. * Schedule linked works starting from @work to @head. Work series to
  1411. * be scheduled starts at @work and includes any consecutive work with
  1412. * WORK_STRUCT_LINKED set in its predecessor.
  1413. *
  1414. * If @nextp is not NULL, it's updated to point to the next work of
  1415. * the last scheduled work. This allows move_linked_works() to be
  1416. * nested inside outer list_for_each_entry_safe().
  1417. *
  1418. * CONTEXT:
  1419. * spin_lock_irq(gcwq->lock).
  1420. */
  1421. static void move_linked_works(struct work_struct *work, struct list_head *head,
  1422. struct work_struct **nextp)
  1423. {
  1424. struct work_struct *n;
  1425. /*
  1426. * Linked worklist will always end before the end of the list,
  1427. * use NULL for list head.
  1428. */
  1429. list_for_each_entry_safe_from(work, n, NULL, entry) {
  1430. list_move_tail(&work->entry, head);
  1431. if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
  1432. break;
  1433. }
  1434. /*
  1435. * If we're already inside safe list traversal and have moved
  1436. * multiple works to the scheduled queue, the next position
  1437. * needs to be updated.
  1438. */
  1439. if (nextp)
  1440. *nextp = n;
  1441. }
  1442. static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
  1443. {
  1444. struct work_struct *work = list_first_entry(&cwq->delayed_works,
  1445. struct work_struct, entry);
  1446. struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
  1447. trace_workqueue_activate_work(work);
  1448. move_linked_works(work, pos, NULL);
  1449. __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
  1450. cwq->nr_active++;
  1451. }
  1452. /**
  1453. * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
  1454. * @cwq: cwq of interest
  1455. * @color: color of work which left the queue
  1456. * @delayed: for a delayed work
  1457. *
  1458. * A work either has completed or is removed from pending queue,
  1459. * decrement nr_in_flight of its cwq and handle workqueue flushing.
  1460. *
  1461. * CONTEXT:
  1462. * spin_lock_irq(gcwq->lock).
  1463. */
  1464. static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
  1465. bool delayed)
  1466. {
  1467. /* ignore uncolored works */
  1468. if (color == WORK_NO_COLOR)
  1469. return;
  1470. cwq->nr_in_flight[color]--;
  1471. if (!delayed) {
  1472. cwq->nr_active--;
  1473. if (!list_empty(&cwq->delayed_works)) {
  1474. /* one down, submit a delayed one */
  1475. if (cwq->nr_active < cwq->max_active)
  1476. cwq_activate_first_delayed(cwq);
  1477. }
  1478. }
  1479. /* is flush in progress and are we at the flushing tip? */
  1480. if (likely(cwq->flush_color != color))
  1481. return;
  1482. /* are there still in-flight works? */
  1483. if (cwq->nr_in_flight[color])
  1484. return;
  1485. /* this cwq is done, clear flush_color */
  1486. cwq->flush_color = -1;
  1487. /*
  1488. * If this was the last cwq, wake up the first flusher. It
  1489. * will handle the rest.
  1490. */
  1491. if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
  1492. complete(&cwq->wq->first_flusher->done);
  1493. }
  1494. /**
  1495. * process_one_work - process single work
  1496. * @worker: self
  1497. * @work: work to process
  1498. *
  1499. * Process @work. This function contains all the logics necessary to
  1500. * process a single work including synchronization against and
  1501. * interaction with other workers on the same cpu, queueing and
  1502. * flushing. As long as context requirement is met, any worker can
  1503. * call this function to process a work.
  1504. *
  1505. * CONTEXT:
  1506. * spin_lock_irq(gcwq->lock) which is released and regrabbed.
  1507. */
  1508. static void process_one_work(struct worker *worker, struct work_struct *work)
  1509. __releases(&gcwq->lock)
  1510. __acquires(&gcwq->lock)
  1511. {
  1512. struct cpu_workqueue_struct *cwq = get_work_cwq(work);
  1513. struct global_cwq *gcwq = cwq->gcwq;
  1514. struct hlist_head *bwh = busy_worker_head(gcwq, work);
  1515. bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
  1516. work_func_t f = work->func;
  1517. int work_color;
  1518. struct worker *collision;
  1519. #ifdef CONFIG_LOCKDEP
  1520. /*
  1521. * It is permissible to free the struct work_struct from
  1522. * inside the function that is called from it, this we need to
  1523. * take into account for lockdep too. To avoid bogus "held
  1524. * lock freed" warnings as well as problems when looking into
  1525. * work->lockdep_map, make a copy and use that here.
  1526. */
  1527. struct lockdep_map lockdep_map = work->lockdep_map;
  1528. #endif
  1529. /*
  1530. * A single work shouldn't be executed concurrently by
  1531. * multiple workers on a single cpu. Check whether anyone is
  1532. * already processing the work. If so, defer the work to the
  1533. * currently executing one.
  1534. */
  1535. collision = __find_worker_executing_work(gcwq, bwh, work);
  1536. if (unlikely(collision)) {
  1537. move_linked_works(work, &collision->scheduled, NULL);
  1538. return;
  1539. }
  1540. /* claim and process */
  1541. debug_work_deactivate(work);
  1542. hlist_add_head(&worker->hentry, bwh);
  1543. worker->current_work = work;
  1544. worker->current_cwq = cwq;
  1545. work_color = get_work_color(work);
  1546. /* record the current cpu number in the work data and dequeue */
  1547. set_work_cpu(work, gcwq->cpu);
  1548. list_del_init(&work->entry);
  1549. /*
  1550. * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
  1551. * wake up another worker; otherwise, clear HIGHPRI_PENDING.
  1552. */
  1553. if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
  1554. struct work_struct *nwork = list_first_entry(&gcwq->worklist,
  1555. struct work_struct, entry);
  1556. if (!list_empty(&gcwq->worklist) &&
  1557. get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
  1558. wake_up_worker(gcwq);
  1559. else
  1560. gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
  1561. }
  1562. /*
  1563. * CPU intensive works don't participate in concurrency
  1564. * management. They're the scheduler's responsibility.
  1565. */
  1566. if (unlikely(cpu_intensive))
  1567. worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
  1568. spin_unlock_irq(&gcwq->lock);
  1569. work_clear_pending(work);
  1570. lock_map_acquire(&cwq->wq->lockdep_map);
  1571. lock_map_acquire(&lockdep_map);
  1572. trace_workqueue_execute_start(work);
  1573. f(work);
  1574. /*
  1575. * While we must be careful to not use "work" after this, the trace
  1576. * point will only record its address.
  1577. */
  1578. trace_workqueue_execute_end(work);
  1579. lock_map_release(&lockdep_map);
  1580. lock_map_release(&cwq->wq->lockdep_map);
  1581. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  1582. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  1583. "%s/0x%08x/%d\n",
  1584. current->comm, preempt_count(), task_pid_nr(current));
  1585. printk(KERN_ERR " last function: ");
  1586. print_symbol("%s\n", (unsigned long)f);
  1587. debug_show_held_locks(current);
  1588. dump_stack();
  1589. }
  1590. spin_lock_irq(&gcwq->lock);
  1591. /* clear cpu intensive status */
  1592. if (unlikely(cpu_intensive))
  1593. worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
  1594. /* we're done with it, release */
  1595. hlist_del_init(&worker->hentry);
  1596. worker->current_work = NULL;
  1597. worker->current_cwq = NULL;
  1598. cwq_dec_nr_in_flight(cwq, work_color, false);
  1599. }
  1600. /**
  1601. * process_scheduled_works - process scheduled works
  1602. * @worker: self
  1603. *
  1604. * Process all scheduled works. Please note that the scheduled list
  1605. * may change while processing a work, so this function repeatedly
  1606. * fetches a work from the top and executes it.
  1607. *
  1608. * CONTEXT:
  1609. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  1610. * multiple times.
  1611. */
  1612. static void process_scheduled_works(struct worker *worker)
  1613. {
  1614. while (!list_empty(&worker->scheduled)) {
  1615. struct work_struct *work = list_first_entry(&worker->scheduled,
  1616. struct work_struct, entry);
  1617. process_one_work(worker, work);
  1618. }
  1619. }
  1620. /**
  1621. * worker_thread - the worker thread function
  1622. * @__worker: self
  1623. *
  1624. * The gcwq worker thread function. There's a single dynamic pool of
  1625. * these per each cpu. These workers process all works regardless of
  1626. * their specific target workqueue. The only exception is works which
  1627. * belong to workqueues with a rescuer which will be explained in
  1628. * rescuer_thread().
  1629. */
  1630. static int worker_thread(void *__worker)
  1631. {
  1632. struct worker *worker = __worker;
  1633. struct global_cwq *gcwq = worker->gcwq;
  1634. /* tell the scheduler that this is a workqueue worker */
  1635. worker->task->flags |= PF_WQ_WORKER;
  1636. woke_up:
  1637. spin_lock_irq(&gcwq->lock);
  1638. /* DIE can be set only while we're idle, checking here is enough */
  1639. if (worker->flags & WORKER_DIE) {
  1640. spin_unlock_irq(&gcwq->lock);
  1641. worker->task->flags &= ~PF_WQ_WORKER;
  1642. return 0;
  1643. }
  1644. worker_leave_idle(worker);
  1645. recheck:
  1646. /* no more worker necessary? */
  1647. if (!need_more_worker(gcwq))
  1648. goto sleep;
  1649. /* do we need to manage? */
  1650. if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
  1651. goto recheck;
  1652. /*
  1653. * ->scheduled list can only be filled while a worker is
  1654. * preparing to process a work or actually processing it.
  1655. * Make sure nobody diddled with it while I was sleeping.
  1656. */
  1657. BUG_ON(!list_empty(&worker->scheduled));
  1658. /*
  1659. * When control reaches this point, we're guaranteed to have
  1660. * at least one idle worker or that someone else has already
  1661. * assumed the manager role.
  1662. */
  1663. worker_clr_flags(worker, WORKER_PREP);
  1664. do {
  1665. struct work_struct *work =
  1666. list_first_entry(&gcwq->worklist,
  1667. struct work_struct, entry);
  1668. if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
  1669. /* optimization path, not strictly necessary */
  1670. process_one_work(worker, work);
  1671. if (unlikely(!list_empty(&worker->scheduled)))
  1672. process_scheduled_works(worker);
  1673. } else {
  1674. move_linked_works(work, &worker->scheduled, NULL);
  1675. process_scheduled_works(worker);
  1676. }
  1677. } while (keep_working(gcwq));
  1678. worker_set_flags(worker, WORKER_PREP, false);
  1679. sleep:
  1680. if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
  1681. goto recheck;
  1682. /*
  1683. * gcwq->lock is held and there's no work to process and no
  1684. * need to manage, sleep. Workers are woken up only while
  1685. * holding gcwq->lock or from local cpu, so setting the
  1686. * current state before releasing gcwq->lock is enough to
  1687. * prevent losing any event.
  1688. */
  1689. worker_enter_idle(worker);
  1690. __set_current_state(TASK_INTERRUPTIBLE);
  1691. spin_unlock_irq(&gcwq->lock);
  1692. schedule();
  1693. goto woke_up;
  1694. }
  1695. /**
  1696. * rescuer_thread - the rescuer thread function
  1697. * @__wq: the associated workqueue
  1698. *
  1699. * Workqueue rescuer thread function. There's one rescuer for each
  1700. * workqueue which has WQ_RESCUER set.
  1701. *
  1702. * Regular work processing on a gcwq may block trying to create a new
  1703. * worker which uses GFP_KERNEL allocation which has slight chance of
  1704. * developing into deadlock if some works currently on the same queue
  1705. * need to be processed to satisfy the GFP_KERNEL allocation. This is
  1706. * the problem rescuer solves.
  1707. *
  1708. * When such condition is possible, the gcwq summons rescuers of all
  1709. * workqueues which have works queued on the gcwq and let them process
  1710. * those works so that forward progress can be guaranteed.
  1711. *
  1712. * This should happen rarely.
  1713. */
  1714. static int rescuer_thread(void *__wq)
  1715. {
  1716. struct workqueue_struct *wq = __wq;
  1717. struct worker *rescuer = wq->rescuer;
  1718. struct list_head *scheduled = &rescuer->scheduled;
  1719. bool is_unbound = wq->flags & WQ_UNBOUND;
  1720. unsigned int cpu;
  1721. set_user_nice(current, RESCUER_NICE_LEVEL);
  1722. repeat:
  1723. set_current_state(TASK_INTERRUPTIBLE);
  1724. if (kthread_should_stop())
  1725. return 0;
  1726. /*
  1727. * See whether any cpu is asking for help. Unbounded
  1728. * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
  1729. */
  1730. for_each_mayday_cpu(cpu, wq->mayday_mask) {
  1731. unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
  1732. struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
  1733. struct global_cwq *gcwq = cwq->gcwq;
  1734. struct work_struct *work, *n;
  1735. __set_current_state(TASK_RUNNING);
  1736. mayday_clear_cpu(cpu, wq->mayday_mask);
  1737. /* migrate to the target cpu if possible */
  1738. rescuer->gcwq = gcwq;
  1739. worker_maybe_bind_and_lock(rescuer);
  1740. /*
  1741. * Slurp in all works issued via this workqueue and
  1742. * process'em.
  1743. */
  1744. BUG_ON(!list_empty(&rescuer->scheduled));
  1745. list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
  1746. if (get_work_cwq(work) == cwq)
  1747. move_linked_works(work, scheduled, &n);
  1748. process_scheduled_works(rescuer);
  1749. spin_unlock_irq(&gcwq->lock);
  1750. }
  1751. schedule();
  1752. goto repeat;
  1753. }
  1754. struct wq_barrier {
  1755. struct work_struct work;
  1756. struct completion done;
  1757. };
  1758. static void wq_barrier_func(struct work_struct *work)
  1759. {
  1760. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  1761. complete(&barr->done);
  1762. }
  1763. /**
  1764. * insert_wq_barrier - insert a barrier work
  1765. * @cwq: cwq to insert barrier into
  1766. * @barr: wq_barrier to insert
  1767. * @target: target work to attach @barr to
  1768. * @worker: worker currently executing @target, NULL if @target is not executing
  1769. *
  1770. * @barr is linked to @target such that @barr is completed only after
  1771. * @target finishes execution. Please note that the ordering
  1772. * guarantee is observed only with respect to @target and on the local
  1773. * cpu.
  1774. *
  1775. * Currently, a queued barrier can't be canceled. This is because
  1776. * try_to_grab_pending() can't determine whether the work to be
  1777. * grabbed is at the head of the queue and thus can't clear LINKED
  1778. * flag of the previous work while there must be a valid next work
  1779. * after a work with LINKED flag set.
  1780. *
  1781. * Note that when @worker is non-NULL, @target may be modified
  1782. * underneath us, so we can't reliably determine cwq from @target.
  1783. *
  1784. * CONTEXT:
  1785. * spin_lock_irq(gcwq->lock).
  1786. */
  1787. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  1788. struct wq_barrier *barr,
  1789. struct work_struct *target, struct worker *worker)
  1790. {
  1791. struct list_head *head;
  1792. unsigned int linked = 0;
  1793. /*
  1794. * debugobject calls are safe here even with gcwq->lock locked
  1795. * as we know for sure that this will not trigger any of the
  1796. * checks and call back into the fixup functions where we
  1797. * might deadlock.
  1798. */
  1799. INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
  1800. __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
  1801. init_completion(&barr->done);
  1802. /*
  1803. * If @target is currently being executed, schedule the
  1804. * barrier to the worker; otherwise, put it after @target.
  1805. */
  1806. if (worker)
  1807. head = worker->scheduled.next;
  1808. else {
  1809. unsigned long *bits = work_data_bits(target);
  1810. head = target->entry.next;
  1811. /* there can already be other linked works, inherit and set */
  1812. linked = *bits & WORK_STRUCT_LINKED;
  1813. __set_bit(WORK_STRUCT_LINKED_BIT, bits);
  1814. }
  1815. debug_work_activate(&barr->work);
  1816. insert_work(cwq, &barr->work, head,
  1817. work_color_to_flags(WORK_NO_COLOR) | linked);
  1818. }
  1819. /**
  1820. * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
  1821. * @wq: workqueue being flushed
  1822. * @flush_color: new flush color, < 0 for no-op
  1823. * @work_color: new work color, < 0 for no-op
  1824. *
  1825. * Prepare cwqs for workqueue flushing.
  1826. *
  1827. * If @flush_color is non-negative, flush_color on all cwqs should be
  1828. * -1. If no cwq has in-flight commands at the specified color, all
  1829. * cwq->flush_color's stay at -1 and %false is returned. If any cwq
  1830. * has in flight commands, its cwq->flush_color is set to
  1831. * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
  1832. * wakeup logic is armed and %true is returned.
  1833. *
  1834. * The caller should have initialized @wq->first_flusher prior to
  1835. * calling this function with non-negative @flush_color. If
  1836. * @flush_color is negative, no flush color update is done and %false
  1837. * is returned.
  1838. *
  1839. * If @work_color is non-negative, all cwqs should have the same
  1840. * work_color which is previous to @work_color and all will be
  1841. * advanced to @work_color.
  1842. *
  1843. * CONTEXT:
  1844. * mutex_lock(wq->flush_mutex).
  1845. *
  1846. * RETURNS:
  1847. * %true if @flush_color >= 0 and there's something to flush. %false
  1848. * otherwise.
  1849. */
  1850. static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
  1851. int flush_color, int work_color)
  1852. {
  1853. bool wait = false;
  1854. unsigned int cpu;
  1855. if (flush_color >= 0) {
  1856. BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
  1857. atomic_set(&wq->nr_cwqs_to_flush, 1);
  1858. }
  1859. for_each_cwq_cpu(cpu, wq) {
  1860. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1861. struct global_cwq *gcwq = cwq->gcwq;
  1862. spin_lock_irq(&gcwq->lock);
  1863. if (flush_color >= 0) {
  1864. BUG_ON(cwq->flush_color != -1);
  1865. if (cwq->nr_in_flight[flush_color]) {
  1866. cwq->flush_color = flush_color;
  1867. atomic_inc(&wq->nr_cwqs_to_flush);
  1868. wait = true;
  1869. }
  1870. }
  1871. if (work_color >= 0) {
  1872. BUG_ON(work_color != work_next_color(cwq->work_color));
  1873. cwq->work_color = work_color;
  1874. }
  1875. spin_unlock_irq(&gcwq->lock);
  1876. }
  1877. if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
  1878. complete(&wq->first_flusher->done);
  1879. return wait;
  1880. }
  1881. /**
  1882. * flush_workqueue - ensure that any scheduled work has run to completion.
  1883. * @wq: workqueue to flush
  1884. *
  1885. * Forces execution of the workqueue and blocks until its completion.
  1886. * This is typically used in driver shutdown handlers.
  1887. *
  1888. * We sleep until all works which were queued on entry have been handled,
  1889. * but we are not livelocked by new incoming ones.
  1890. */
  1891. void flush_workqueue(struct workqueue_struct *wq)
  1892. {
  1893. struct wq_flusher this_flusher = {
  1894. .list = LIST_HEAD_INIT(this_flusher.list),
  1895. .flush_color = -1,
  1896. .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
  1897. };
  1898. int next_color;
  1899. lock_map_acquire(&wq->lockdep_map);
  1900. lock_map_release(&wq->lockdep_map);
  1901. mutex_lock(&wq->flush_mutex);
  1902. /*
  1903. * Start-to-wait phase
  1904. */
  1905. next_color = work_next_color(wq->work_color);
  1906. if (next_color != wq->flush_color) {
  1907. /*
  1908. * Color space is not full. The current work_color
  1909. * becomes our flush_color and work_color is advanced
  1910. * by one.
  1911. */
  1912. BUG_ON(!list_empty(&wq->flusher_overflow));
  1913. this_flusher.flush_color = wq->work_color;
  1914. wq->work_color = next_color;
  1915. if (!wq->first_flusher) {
  1916. /* no flush in progress, become the first flusher */
  1917. BUG_ON(wq->flush_color != this_flusher.flush_color);
  1918. wq->first_flusher = &this_flusher;
  1919. if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
  1920. wq->work_color)) {
  1921. /* nothing to flush, done */
  1922. wq->flush_color = next_color;
  1923. wq->first_flusher = NULL;
  1924. goto out_unlock;
  1925. }
  1926. } else {
  1927. /* wait in queue */
  1928. BUG_ON(wq->flush_color == this_flusher.flush_color);
  1929. list_add_tail(&this_flusher.list, &wq->flusher_queue);
  1930. flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
  1931. }
  1932. } else {
  1933. /*
  1934. * Oops, color space is full, wait on overflow queue.
  1935. * The next flush completion will assign us
  1936. * flush_color and transfer to flusher_queue.
  1937. */
  1938. list_add_tail(&this_flusher.list, &wq->flusher_overflow);
  1939. }
  1940. mutex_unlock(&wq->flush_mutex);
  1941. wait_for_completion(&this_flusher.done);
  1942. /*
  1943. * Wake-up-and-cascade phase
  1944. *
  1945. * First flushers are responsible for cascading flushes and
  1946. * handling overflow. Non-first flushers can simply return.
  1947. */
  1948. if (wq->first_flusher != &this_flusher)
  1949. return;
  1950. mutex_lock(&wq->flush_mutex);
  1951. /* we might have raced, check again with mutex held */
  1952. if (wq->first_flusher != &this_flusher)
  1953. goto out_unlock;
  1954. wq->first_flusher = NULL;
  1955. BUG_ON(!list_empty(&this_flusher.list));
  1956. BUG_ON(wq->flush_color != this_flusher.flush_color);
  1957. while (true) {
  1958. struct wq_flusher *next, *tmp;
  1959. /* complete all the flushers sharing the current flush color */
  1960. list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
  1961. if (next->flush_color != wq->flush_color)
  1962. break;
  1963. list_del_init(&next->list);
  1964. complete(&next->done);
  1965. }
  1966. BUG_ON(!list_empty(&wq->flusher_overflow) &&
  1967. wq->flush_color != work_next_color(wq->work_color));
  1968. /* this flush_color is finished, advance by one */
  1969. wq->flush_color = work_next_color(wq->flush_color);
  1970. /* one color has been freed, handle overflow queue */
  1971. if (!list_empty(&wq->flusher_overflow)) {
  1972. /*
  1973. * Assign the same color to all overflowed
  1974. * flushers, advance work_color and append to
  1975. * flusher_queue. This is the start-to-wait
  1976. * phase for these overflowed flushers.
  1977. */
  1978. list_for_each_entry(tmp, &wq->flusher_overflow, list)
  1979. tmp->flush_color = wq->work_color;
  1980. wq->work_color = work_next_color(wq->work_color);
  1981. list_splice_tail_init(&wq->flusher_overflow,
  1982. &wq->flusher_queue);
  1983. flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
  1984. }
  1985. if (list_empty(&wq->flusher_queue)) {
  1986. BUG_ON(wq->flush_color != wq->work_color);
  1987. break;
  1988. }
  1989. /*
  1990. * Need to flush more colors. Make the next flusher
  1991. * the new first flusher and arm cwqs.
  1992. */
  1993. BUG_ON(wq->flush_color == wq->work_color);
  1994. BUG_ON(wq->flush_color != next->flush_color);
  1995. list_del_init(&next->list);
  1996. wq->first_flusher = next;
  1997. if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
  1998. break;
  1999. /*
  2000. * Meh... this color is already done, clear first
  2001. * flusher and repeat cascading.
  2002. */
  2003. wq->first_flusher = NULL;
  2004. }
  2005. out_unlock:
  2006. mutex_unlock(&wq->flush_mutex);
  2007. }
  2008. EXPORT_SYMBOL_GPL(flush_workqueue);
  2009. static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
  2010. bool wait_executing)
  2011. {
  2012. struct worker *worker = NULL;
  2013. struct global_cwq *gcwq;
  2014. struct cpu_workqueue_struct *cwq;
  2015. might_sleep();
  2016. gcwq = get_work_gcwq(work);
  2017. if (!gcwq)
  2018. return false;
  2019. spin_lock_irq(&gcwq->lock);
  2020. if (!list_empty(&work->entry)) {
  2021. /*
  2022. * See the comment near try_to_grab_pending()->smp_rmb().
  2023. * If it was re-queued to a different gcwq under us, we
  2024. * are not going to wait.
  2025. */
  2026. smp_rmb();
  2027. cwq = get_work_cwq(work);
  2028. if (unlikely(!cwq || gcwq != cwq->gcwq))
  2029. goto already_gone;
  2030. } else if (wait_executing) {
  2031. worker = find_worker_executing_work(gcwq, work);
  2032. if (!worker)
  2033. goto already_gone;
  2034. cwq = worker->current_cwq;
  2035. } else
  2036. goto already_gone;
  2037. insert_wq_barrier(cwq, barr, work, worker);
  2038. spin_unlock_irq(&gcwq->lock);
  2039. lock_map_acquire(&cwq->wq->lockdep_map);
  2040. lock_map_release(&cwq->wq->lockdep_map);
  2041. return true;
  2042. already_gone:
  2043. spin_unlock_irq(&gcwq->lock);
  2044. return false;
  2045. }
  2046. /**
  2047. * flush_work - wait for a work to finish executing the last queueing instance
  2048. * @work: the work to flush
  2049. *
  2050. * Wait until @work has finished execution. This function considers
  2051. * only the last queueing instance of @work. If @work has been
  2052. * enqueued across different CPUs on a non-reentrant workqueue or on
  2053. * multiple workqueues, @work might still be executing on return on
  2054. * some of the CPUs from earlier queueing.
  2055. *
  2056. * If @work was queued only on a non-reentrant, ordered or unbound
  2057. * workqueue, @work is guaranteed to be idle on return if it hasn't
  2058. * been requeued since flush started.
  2059. *
  2060. * RETURNS:
  2061. * %true if flush_work() waited for the work to finish execution,
  2062. * %false if it was already idle.
  2063. */
  2064. bool flush_work(struct work_struct *work)
  2065. {
  2066. struct wq_barrier barr;
  2067. if (start_flush_work(work, &barr, true)) {
  2068. wait_for_completion(&barr.done);
  2069. destroy_work_on_stack(&barr.work);
  2070. return true;
  2071. } else
  2072. return false;
  2073. }
  2074. EXPORT_SYMBOL_GPL(flush_work);
  2075. static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
  2076. {
  2077. struct wq_barrier barr;
  2078. struct worker *worker;
  2079. spin_lock_irq(&gcwq->lock);
  2080. worker = find_worker_executing_work(gcwq, work);
  2081. if (unlikely(worker))
  2082. insert_wq_barrier(worker->current_cwq, &barr, work, worker);
  2083. spin_unlock_irq(&gcwq->lock);
  2084. if (unlikely(worker)) {
  2085. wait_for_completion(&barr.done);
  2086. destroy_work_on_stack(&barr.work);
  2087. return true;
  2088. } else
  2089. return false;
  2090. }
  2091. static bool wait_on_work(struct work_struct *work)
  2092. {
  2093. bool ret = false;
  2094. int cpu;
  2095. might_sleep();
  2096. lock_map_acquire(&work->lockdep_map);
  2097. lock_map_release(&work->lockdep_map);
  2098. for_each_gcwq_cpu(cpu)
  2099. ret |= wait_on_cpu_work(get_gcwq(cpu), work);
  2100. return ret;
  2101. }
  2102. /**
  2103. * flush_work_sync - wait until a work has finished execution
  2104. * @work: the work to flush
  2105. *
  2106. * Wait until @work has finished execution. On return, it's
  2107. * guaranteed that all queueing instances of @work which happened
  2108. * before this function is called are finished. In other words, if
  2109. * @work hasn't been requeued since this function was called, @work is
  2110. * guaranteed to be idle on return.
  2111. *
  2112. * RETURNS:
  2113. * %true if flush_work_sync() waited for the work to finish execution,
  2114. * %false if it was already idle.
  2115. */
  2116. bool flush_work_sync(struct work_struct *work)
  2117. {
  2118. struct wq_barrier barr;
  2119. bool pending, waited;
  2120. /* we'll wait for executions separately, queue barr only if pending */
  2121. pending = start_flush_work(work, &barr, false);
  2122. /* wait for executions to finish */
  2123. waited = wait_on_work(work);
  2124. /* wait for the pending one */
  2125. if (pending) {
  2126. wait_for_completion(&barr.done);
  2127. destroy_work_on_stack(&barr.work);
  2128. }
  2129. return pending || waited;
  2130. }
  2131. EXPORT_SYMBOL_GPL(flush_work_sync);
  2132. /*
  2133. * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
  2134. * so this work can't be re-armed in any way.
  2135. */
  2136. static int try_to_grab_pending(struct work_struct *work)
  2137. {
  2138. struct global_cwq *gcwq;
  2139. int ret = -1;
  2140. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  2141. return 0;
  2142. /*
  2143. * The queueing is in progress, or it is already queued. Try to
  2144. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  2145. */
  2146. gcwq = get_work_gcwq(work);
  2147. if (!gcwq)
  2148. return ret;
  2149. spin_lock_irq(&gcwq->lock);
  2150. if (!list_empty(&work->entry)) {
  2151. /*
  2152. * This work is queued, but perhaps we locked the wrong gcwq.
  2153. * In that case we must see the new value after rmb(), see
  2154. * insert_work()->wmb().
  2155. */
  2156. smp_rmb();
  2157. if (gcwq == get_work_gcwq(work)) {
  2158. debug_work_deactivate(work);
  2159. list_del_init(&work->entry);
  2160. cwq_dec_nr_in_flight(get_work_cwq(work),
  2161. get_work_color(work),
  2162. *work_data_bits(work) & WORK_STRUCT_DELAYED);
  2163. ret = 1;
  2164. }
  2165. }
  2166. spin_unlock_irq(&gcwq->lock);
  2167. return ret;
  2168. }
  2169. static bool __cancel_work_timer(struct work_struct *work,
  2170. struct timer_list* timer)
  2171. {
  2172. int ret;
  2173. do {
  2174. ret = (timer && likely(del_timer(timer)));
  2175. if (!ret)
  2176. ret = try_to_grab_pending(work);
  2177. wait_on_work(work);
  2178. } while (unlikely(ret < 0));
  2179. clear_work_data(work);
  2180. return ret;
  2181. }
  2182. /**
  2183. * cancel_work_sync - cancel a work and wait for it to finish
  2184. * @work: the work to cancel
  2185. *
  2186. * Cancel @work and wait for its execution to finish. This function
  2187. * can be used even if the work re-queues itself or migrates to
  2188. * another workqueue. On return from this function, @work is
  2189. * guaranteed to be not pending or executing on any CPU.
  2190. *
  2191. * cancel_work_sync(&delayed_work->work) must not be used for
  2192. * delayed_work's. Use cancel_delayed_work_sync() instead.
  2193. *
  2194. * The caller must ensure that the workqueue on which @work was last
  2195. * queued can't be destroyed before this function returns.
  2196. *
  2197. * RETURNS:
  2198. * %true if @work was pending, %false otherwise.
  2199. */
  2200. bool cancel_work_sync(struct work_struct *work)
  2201. {
  2202. return __cancel_work_timer(work, NULL);
  2203. }
  2204. EXPORT_SYMBOL_GPL(cancel_work_sync);
  2205. /**
  2206. * flush_delayed_work - wait for a dwork to finish executing the last queueing
  2207. * @dwork: the delayed work to flush
  2208. *
  2209. * Delayed timer is cancelled and the pending work is queued for
  2210. * immediate execution. Like flush_work(), this function only
  2211. * considers the last queueing instance of @dwork.
  2212. *
  2213. * RETURNS:
  2214. * %true if flush_work() waited for the work to finish execution,
  2215. * %false if it was already idle.
  2216. */
  2217. bool flush_delayed_work(struct delayed_work *dwork)
  2218. {
  2219. if (del_timer_sync(&dwork->timer))
  2220. __queue_work(raw_smp_processor_id(),
  2221. get_work_cwq(&dwork->work)->wq, &dwork->work);
  2222. return flush_work(&dwork->work);
  2223. }
  2224. EXPORT_SYMBOL(flush_delayed_work);
  2225. /**
  2226. * flush_delayed_work_sync - wait for a dwork to finish
  2227. * @dwork: the delayed work to flush
  2228. *
  2229. * Delayed timer is cancelled and the pending work is queued for
  2230. * execution immediately. Other than timer handling, its behavior
  2231. * is identical to flush_work_sync().
  2232. *
  2233. * RETURNS:
  2234. * %true if flush_work_sync() waited for the work to finish execution,
  2235. * %false if it was already idle.
  2236. */
  2237. bool flush_delayed_work_sync(struct delayed_work *dwork)
  2238. {
  2239. if (del_timer_sync(&dwork->timer))
  2240. __queue_work(raw_smp_processor_id(),
  2241. get_work_cwq(&dwork->work)->wq, &dwork->work);
  2242. return flush_work_sync(&dwork->work);
  2243. }
  2244. EXPORT_SYMBOL(flush_delayed_work_sync);
  2245. /**
  2246. * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
  2247. * @dwork: the delayed work cancel
  2248. *
  2249. * This is cancel_work_sync() for delayed works.
  2250. *
  2251. * RETURNS:
  2252. * %true if @dwork was pending, %false otherwise.
  2253. */
  2254. bool cancel_delayed_work_sync(struct delayed_work *dwork)
  2255. {
  2256. return __cancel_work_timer(&dwork->work, &dwork->timer);
  2257. }
  2258. EXPORT_SYMBOL(cancel_delayed_work_sync);
  2259. /**
  2260. * schedule_work - put work task in global workqueue
  2261. * @work: job to be done
  2262. *
  2263. * Returns zero if @work was already on the kernel-global workqueue and
  2264. * non-zero otherwise.
  2265. *
  2266. * This puts a job in the kernel-global workqueue if it was not already
  2267. * queued and leaves it in the same position on the kernel-global
  2268. * workqueue otherwise.
  2269. */
  2270. int schedule_work(struct work_struct *work)
  2271. {
  2272. return queue_work(system_wq, work);
  2273. }
  2274. EXPORT_SYMBOL(schedule_work);
  2275. /*
  2276. * schedule_work_on - put work task on a specific cpu
  2277. * @cpu: cpu to put the work task on
  2278. * @work: job to be done
  2279. *
  2280. * This puts a job on a specific cpu
  2281. */
  2282. int schedule_work_on(int cpu, struct work_struct *work)
  2283. {
  2284. return queue_work_on(cpu, system_wq, work);
  2285. }
  2286. EXPORT_SYMBOL(schedule_work_on);
  2287. /**
  2288. * schedule_delayed_work - put work task in global workqueue after delay
  2289. * @dwork: job to be done
  2290. * @delay: number of jiffies to wait or 0 for immediate execution
  2291. *
  2292. * After waiting for a given time this puts a job in the kernel-global
  2293. * workqueue.
  2294. */
  2295. int schedule_delayed_work(struct delayed_work *dwork,
  2296. unsigned long delay)
  2297. {
  2298. return queue_delayed_work(system_wq, dwork, delay);
  2299. }
  2300. EXPORT_SYMBOL(schedule_delayed_work);
  2301. /**
  2302. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  2303. * @cpu: cpu to use
  2304. * @dwork: job to be done
  2305. * @delay: number of jiffies to wait
  2306. *
  2307. * After waiting for a given time this puts a job in the kernel-global
  2308. * workqueue on the specified CPU.
  2309. */
  2310. int schedule_delayed_work_on(int cpu,
  2311. struct delayed_work *dwork, unsigned long delay)
  2312. {
  2313. return queue_delayed_work_on(cpu, system_wq, dwork, delay);
  2314. }
  2315. EXPORT_SYMBOL(schedule_delayed_work_on);
  2316. /**
  2317. * schedule_on_each_cpu - execute a function synchronously on each online CPU
  2318. * @func: the function to call
  2319. *
  2320. * schedule_on_each_cpu() executes @func on each online CPU using the
  2321. * system workqueue and blocks until all CPUs have completed.
  2322. * schedule_on_each_cpu() is very slow.
  2323. *
  2324. * RETURNS:
  2325. * 0 on success, -errno on failure.
  2326. */
  2327. int schedule_on_each_cpu(work_func_t func)
  2328. {
  2329. int cpu;
  2330. struct work_struct __percpu *works;
  2331. works = alloc_percpu(struct work_struct);
  2332. if (!works)
  2333. return -ENOMEM;
  2334. get_online_cpus();
  2335. for_each_online_cpu(cpu) {
  2336. struct work_struct *work = per_cpu_ptr(works, cpu);
  2337. INIT_WORK(work, func);
  2338. schedule_work_on(cpu, work);
  2339. }
  2340. for_each_online_cpu(cpu)
  2341. flush_work(per_cpu_ptr(works, cpu));
  2342. put_online_cpus();
  2343. free_percpu(works);
  2344. return 0;
  2345. }
  2346. /**
  2347. * flush_scheduled_work - ensure that any scheduled work has run to completion.
  2348. *
  2349. * Forces execution of the kernel-global workqueue and blocks until its
  2350. * completion.
  2351. *
  2352. * Think twice before calling this function! It's very easy to get into
  2353. * trouble if you don't take great care. Either of the following situations
  2354. * will lead to deadlock:
  2355. *
  2356. * One of the work items currently on the workqueue needs to acquire
  2357. * a lock held by your code or its caller.
  2358. *
  2359. * Your code is running in the context of a work routine.
  2360. *
  2361. * They will be detected by lockdep when they occur, but the first might not
  2362. * occur very often. It depends on what work items are on the workqueue and
  2363. * what locks they need, which you have no control over.
  2364. *
  2365. * In most situations flushing the entire workqueue is overkill; you merely
  2366. * need to know that a particular work item isn't queued and isn't running.
  2367. * In such cases you should use cancel_delayed_work_sync() or
  2368. * cancel_work_sync() instead.
  2369. */
  2370. void flush_scheduled_work(void)
  2371. {
  2372. flush_workqueue(system_wq);
  2373. }
  2374. EXPORT_SYMBOL(flush_scheduled_work);
  2375. /**
  2376. * execute_in_process_context - reliably execute the routine with user context
  2377. * @fn: the function to execute
  2378. * @ew: guaranteed storage for the execute work structure (must
  2379. * be available when the work executes)
  2380. *
  2381. * Executes the function immediately if process context is available,
  2382. * otherwise schedules the function for delayed execution.
  2383. *
  2384. * Returns: 0 - function was executed
  2385. * 1 - function was scheduled for execution
  2386. */
  2387. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  2388. {
  2389. if (!in_interrupt()) {
  2390. fn(&ew->work);
  2391. return 0;
  2392. }
  2393. INIT_WORK(&ew->work, fn);
  2394. schedule_work(&ew->work);
  2395. return 1;
  2396. }
  2397. EXPORT_SYMBOL_GPL(execute_in_process_context);
  2398. int keventd_up(void)
  2399. {
  2400. return system_wq != NULL;
  2401. }
  2402. static int alloc_cwqs(struct workqueue_struct *wq)
  2403. {
  2404. /*
  2405. * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
  2406. * Make sure that the alignment isn't lower than that of
  2407. * unsigned long long.
  2408. */
  2409. const size_t size = sizeof(struct cpu_workqueue_struct);
  2410. const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
  2411. __alignof__(unsigned long long));
  2412. #ifdef CONFIG_SMP
  2413. bool percpu = !(wq->flags & WQ_UNBOUND);
  2414. #else
  2415. bool percpu = false;
  2416. #endif
  2417. if (percpu)
  2418. wq->cpu_wq.pcpu = __alloc_percpu(size, align);
  2419. else {
  2420. void *ptr;
  2421. /*
  2422. * Allocate enough room to align cwq and put an extra
  2423. * pointer at the end pointing back to the originally
  2424. * allocated pointer which will be used for free.
  2425. */
  2426. ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
  2427. if (ptr) {
  2428. wq->cpu_wq.single = PTR_ALIGN(ptr, align);
  2429. *(void **)(wq->cpu_wq.single + 1) = ptr;
  2430. }
  2431. }
  2432. /* just in case, make sure it's actually aligned
  2433. * - this is affected by PERCPU() alignment in vmlinux.lds.S
  2434. */
  2435. BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
  2436. return wq->cpu_wq.v ? 0 : -ENOMEM;
  2437. }
  2438. static void free_cwqs(struct workqueue_struct *wq)
  2439. {
  2440. #ifdef CONFIG_SMP
  2441. bool percpu = !(wq->flags & WQ_UNBOUND);
  2442. #else
  2443. bool percpu = false;
  2444. #endif
  2445. if (percpu)
  2446. free_percpu(wq->cpu_wq.pcpu);
  2447. else if (wq->cpu_wq.single) {
  2448. /* the pointer to free is stored right after the cwq */
  2449. kfree(*(void **)(wq->cpu_wq.single + 1));
  2450. }
  2451. }
  2452. static int wq_clamp_max_active(int max_active, unsigned int flags,
  2453. const char *name)
  2454. {
  2455. int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
  2456. if (max_active < 1 || max_active > lim)
  2457. printk(KERN_WARNING "workqueue: max_active %d requested for %s "
  2458. "is out of range, clamping between %d and %d\n",
  2459. max_active, name, 1, lim);
  2460. return clamp_val(max_active, 1, lim);
  2461. }
  2462. struct workqueue_struct *__alloc_workqueue_key(const char *name,
  2463. unsigned int flags,
  2464. int max_active,
  2465. struct lock_class_key *key,
  2466. const char *lock_name)
  2467. {
  2468. struct workqueue_struct *wq;
  2469. unsigned int cpu;
  2470. /*
  2471. * Workqueues which may be used during memory reclaim should
  2472. * have a rescuer to guarantee forward progress.
  2473. */
  2474. if (flags & WQ_MEM_RECLAIM)
  2475. flags |= WQ_RESCUER;
  2476. /*
  2477. * Unbound workqueues aren't concurrency managed and should be
  2478. * dispatched to workers immediately.
  2479. */
  2480. if (flags & WQ_UNBOUND)
  2481. flags |= WQ_HIGHPRI;
  2482. max_active = max_active ?: WQ_DFL_ACTIVE;
  2483. max_active = wq_clamp_max_active(max_active, flags, name);
  2484. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  2485. if (!wq)
  2486. goto err;
  2487. wq->flags = flags;
  2488. wq->saved_max_active = max_active;
  2489. mutex_init(&wq->flush_mutex);
  2490. atomic_set(&wq->nr_cwqs_to_flush, 0);
  2491. INIT_LIST_HEAD(&wq->flusher_queue);
  2492. INIT_LIST_HEAD(&wq->flusher_overflow);
  2493. wq->name = name;
  2494. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  2495. INIT_LIST_HEAD(&wq->list);
  2496. if (alloc_cwqs(wq) < 0)
  2497. goto err;
  2498. for_each_cwq_cpu(cpu, wq) {
  2499. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  2500. struct global_cwq *gcwq = get_gcwq(cpu);
  2501. BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
  2502. cwq->gcwq = gcwq;
  2503. cwq->wq = wq;
  2504. cwq->flush_color = -1;
  2505. cwq->max_active = max_active;
  2506. INIT_LIST_HEAD(&cwq->delayed_works);
  2507. }
  2508. if (flags & WQ_RESCUER) {
  2509. struct worker *rescuer;
  2510. if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
  2511. goto err;
  2512. wq->rescuer = rescuer = alloc_worker();
  2513. if (!rescuer)
  2514. goto err;
  2515. rescuer->task = kthread_create(rescuer_thread, wq, "%s", name);
  2516. if (IS_ERR(rescuer->task))
  2517. goto err;
  2518. rescuer->task->flags |= PF_THREAD_BOUND;
  2519. wake_up_process(rescuer->task);
  2520. }
  2521. /*
  2522. * workqueue_lock protects global freeze state and workqueues
  2523. * list. Grab it, set max_active accordingly and add the new
  2524. * workqueue to workqueues list.
  2525. */
  2526. spin_lock(&workqueue_lock);
  2527. if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
  2528. for_each_cwq_cpu(cpu, wq)
  2529. get_cwq(cpu, wq)->max_active = 0;
  2530. list_add(&wq->list, &workqueues);
  2531. spin_unlock(&workqueue_lock);
  2532. return wq;
  2533. err:
  2534. if (wq) {
  2535. free_cwqs(wq);
  2536. free_mayday_mask(wq->mayday_mask);
  2537. kfree(wq->rescuer);
  2538. kfree(wq);
  2539. }
  2540. return NULL;
  2541. }
  2542. EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
  2543. /**
  2544. * destroy_workqueue - safely terminate a workqueue
  2545. * @wq: target workqueue
  2546. *
  2547. * Safely destroy a workqueue. All work currently pending will be done first.
  2548. */
  2549. void destroy_workqueue(struct workqueue_struct *wq)
  2550. {
  2551. unsigned int cpu;
  2552. wq->flags |= WQ_DYING;
  2553. flush_workqueue(wq);
  2554. /*
  2555. * wq list is used to freeze wq, remove from list after
  2556. * flushing is complete in case freeze races us.
  2557. */
  2558. spin_lock(&workqueue_lock);
  2559. list_del(&wq->list);
  2560. spin_unlock(&workqueue_lock);
  2561. /* sanity check */
  2562. for_each_cwq_cpu(cpu, wq) {
  2563. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  2564. int i;
  2565. for (i = 0; i < WORK_NR_COLORS; i++)
  2566. BUG_ON(cwq->nr_in_flight[i]);
  2567. BUG_ON(cwq->nr_active);
  2568. BUG_ON(!list_empty(&cwq->delayed_works));
  2569. }
  2570. if (wq->flags & WQ_RESCUER) {
  2571. kthread_stop(wq->rescuer->task);
  2572. free_mayday_mask(wq->mayday_mask);
  2573. kfree(wq->rescuer);
  2574. }
  2575. free_cwqs(wq);
  2576. kfree(wq);
  2577. }
  2578. EXPORT_SYMBOL_GPL(destroy_workqueue);
  2579. /**
  2580. * workqueue_set_max_active - adjust max_active of a workqueue
  2581. * @wq: target workqueue
  2582. * @max_active: new max_active value.
  2583. *
  2584. * Set max_active of @wq to @max_active.
  2585. *
  2586. * CONTEXT:
  2587. * Don't call from IRQ context.
  2588. */
  2589. void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
  2590. {
  2591. unsigned int cpu;
  2592. max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
  2593. spin_lock(&workqueue_lock);
  2594. wq->saved_max_active = max_active;
  2595. for_each_cwq_cpu(cpu, wq) {
  2596. struct global_cwq *gcwq = get_gcwq(cpu);
  2597. spin_lock_irq(&gcwq->lock);
  2598. if (!(wq->flags & WQ_FREEZEABLE) ||
  2599. !(gcwq->flags & GCWQ_FREEZING))
  2600. get_cwq(gcwq->cpu, wq)->max_active = max_active;
  2601. spin_unlock_irq(&gcwq->lock);
  2602. }
  2603. spin_unlock(&workqueue_lock);
  2604. }
  2605. EXPORT_SYMBOL_GPL(workqueue_set_max_active);
  2606. /**
  2607. * workqueue_congested - test whether a workqueue is congested
  2608. * @cpu: CPU in question
  2609. * @wq: target workqueue
  2610. *
  2611. * Test whether @wq's cpu workqueue for @cpu is congested. There is
  2612. * no synchronization around this function and the test result is
  2613. * unreliable and only useful as advisory hints or for debugging.
  2614. *
  2615. * RETURNS:
  2616. * %true if congested, %false otherwise.
  2617. */
  2618. bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
  2619. {
  2620. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  2621. return !list_empty(&cwq->delayed_works);
  2622. }
  2623. EXPORT_SYMBOL_GPL(workqueue_congested);
  2624. /**
  2625. * work_cpu - return the last known associated cpu for @work
  2626. * @work: the work of interest
  2627. *
  2628. * RETURNS:
  2629. * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
  2630. */
  2631. unsigned int work_cpu(struct work_struct *work)
  2632. {
  2633. struct global_cwq *gcwq = get_work_gcwq(work);
  2634. return gcwq ? gcwq->cpu : WORK_CPU_NONE;
  2635. }
  2636. EXPORT_SYMBOL_GPL(work_cpu);
  2637. /**
  2638. * work_busy - test whether a work is currently pending or running
  2639. * @work: the work to be tested
  2640. *
  2641. * Test whether @work is currently pending or running. There is no
  2642. * synchronization around this function and the test result is
  2643. * unreliable and only useful as advisory hints or for debugging.
  2644. * Especially for reentrant wqs, the pending state might hide the
  2645. * running state.
  2646. *
  2647. * RETURNS:
  2648. * OR'd bitmask of WORK_BUSY_* bits.
  2649. */
  2650. unsigned int work_busy(struct work_struct *work)
  2651. {
  2652. struct global_cwq *gcwq = get_work_gcwq(work);
  2653. unsigned long flags;
  2654. unsigned int ret = 0;
  2655. if (!gcwq)
  2656. return false;
  2657. spin_lock_irqsave(&gcwq->lock, flags);
  2658. if (work_pending(work))
  2659. ret |= WORK_BUSY_PENDING;
  2660. if (find_worker_executing_work(gcwq, work))
  2661. ret |= WORK_BUSY_RUNNING;
  2662. spin_unlock_irqrestore(&gcwq->lock, flags);
  2663. return ret;
  2664. }
  2665. EXPORT_SYMBOL_GPL(work_busy);
  2666. /*
  2667. * CPU hotplug.
  2668. *
  2669. * There are two challenges in supporting CPU hotplug. Firstly, there
  2670. * are a lot of assumptions on strong associations among work, cwq and
  2671. * gcwq which make migrating pending and scheduled works very
  2672. * difficult to implement without impacting hot paths. Secondly,
  2673. * gcwqs serve mix of short, long and very long running works making
  2674. * blocked draining impractical.
  2675. *
  2676. * This is solved by allowing a gcwq to be detached from CPU, running
  2677. * it with unbound (rogue) workers and allowing it to be reattached
  2678. * later if the cpu comes back online. A separate thread is created
  2679. * to govern a gcwq in such state and is called the trustee of the
  2680. * gcwq.
  2681. *
  2682. * Trustee states and their descriptions.
  2683. *
  2684. * START Command state used on startup. On CPU_DOWN_PREPARE, a
  2685. * new trustee is started with this state.
  2686. *
  2687. * IN_CHARGE Once started, trustee will enter this state after
  2688. * assuming the manager role and making all existing
  2689. * workers rogue. DOWN_PREPARE waits for trustee to
  2690. * enter this state. After reaching IN_CHARGE, trustee
  2691. * tries to execute the pending worklist until it's empty
  2692. * and the state is set to BUTCHER, or the state is set
  2693. * to RELEASE.
  2694. *
  2695. * BUTCHER Command state which is set by the cpu callback after
  2696. * the cpu has went down. Once this state is set trustee
  2697. * knows that there will be no new works on the worklist
  2698. * and once the worklist is empty it can proceed to
  2699. * killing idle workers.
  2700. *
  2701. * RELEASE Command state which is set by the cpu callback if the
  2702. * cpu down has been canceled or it has come online
  2703. * again. After recognizing this state, trustee stops
  2704. * trying to drain or butcher and clears ROGUE, rebinds
  2705. * all remaining workers back to the cpu and releases
  2706. * manager role.
  2707. *
  2708. * DONE Trustee will enter this state after BUTCHER or RELEASE
  2709. * is complete.
  2710. *
  2711. * trustee CPU draining
  2712. * took over down complete
  2713. * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
  2714. * | | ^
  2715. * | CPU is back online v return workers |
  2716. * ----------------> RELEASE --------------
  2717. */
  2718. /**
  2719. * trustee_wait_event_timeout - timed event wait for trustee
  2720. * @cond: condition to wait for
  2721. * @timeout: timeout in jiffies
  2722. *
  2723. * wait_event_timeout() for trustee to use. Handles locking and
  2724. * checks for RELEASE request.
  2725. *
  2726. * CONTEXT:
  2727. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  2728. * multiple times. To be used by trustee.
  2729. *
  2730. * RETURNS:
  2731. * Positive indicating left time if @cond is satisfied, 0 if timed
  2732. * out, -1 if canceled.
  2733. */
  2734. #define trustee_wait_event_timeout(cond, timeout) ({ \
  2735. long __ret = (timeout); \
  2736. while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
  2737. __ret) { \
  2738. spin_unlock_irq(&gcwq->lock); \
  2739. __wait_event_timeout(gcwq->trustee_wait, (cond) || \
  2740. (gcwq->trustee_state == TRUSTEE_RELEASE), \
  2741. __ret); \
  2742. spin_lock_irq(&gcwq->lock); \
  2743. } \
  2744. gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
  2745. })
  2746. /**
  2747. * trustee_wait_event - event wait for trustee
  2748. * @cond: condition to wait for
  2749. *
  2750. * wait_event() for trustee to use. Automatically handles locking and
  2751. * checks for CANCEL request.
  2752. *
  2753. * CONTEXT:
  2754. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  2755. * multiple times. To be used by trustee.
  2756. *
  2757. * RETURNS:
  2758. * 0 if @cond is satisfied, -1 if canceled.
  2759. */
  2760. #define trustee_wait_event(cond) ({ \
  2761. long __ret1; \
  2762. __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
  2763. __ret1 < 0 ? -1 : 0; \
  2764. })
  2765. static int __cpuinit trustee_thread(void *__gcwq)
  2766. {
  2767. struct global_cwq *gcwq = __gcwq;
  2768. struct worker *worker;
  2769. struct work_struct *work;
  2770. struct hlist_node *pos;
  2771. long rc;
  2772. int i;
  2773. BUG_ON(gcwq->cpu != smp_processor_id());
  2774. spin_lock_irq(&gcwq->lock);
  2775. /*
  2776. * Claim the manager position and make all workers rogue.
  2777. * Trustee must be bound to the target cpu and can't be
  2778. * cancelled.
  2779. */
  2780. BUG_ON(gcwq->cpu != smp_processor_id());
  2781. rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
  2782. BUG_ON(rc < 0);
  2783. gcwq->flags |= GCWQ_MANAGING_WORKERS;
  2784. list_for_each_entry(worker, &gcwq->idle_list, entry)
  2785. worker->flags |= WORKER_ROGUE;
  2786. for_each_busy_worker(worker, i, pos, gcwq)
  2787. worker->flags |= WORKER_ROGUE;
  2788. /*
  2789. * Call schedule() so that we cross rq->lock and thus can
  2790. * guarantee sched callbacks see the rogue flag. This is
  2791. * necessary as scheduler callbacks may be invoked from other
  2792. * cpus.
  2793. */
  2794. spin_unlock_irq(&gcwq->lock);
  2795. schedule();
  2796. spin_lock_irq(&gcwq->lock);
  2797. /*
  2798. * Sched callbacks are disabled now. Zap nr_running. After
  2799. * this, nr_running stays zero and need_more_worker() and
  2800. * keep_working() are always true as long as the worklist is
  2801. * not empty.
  2802. */
  2803. atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
  2804. spin_unlock_irq(&gcwq->lock);
  2805. del_timer_sync(&gcwq->idle_timer);
  2806. spin_lock_irq(&gcwq->lock);
  2807. /*
  2808. * We're now in charge. Notify and proceed to drain. We need
  2809. * to keep the gcwq running during the whole CPU down
  2810. * procedure as other cpu hotunplug callbacks may need to
  2811. * flush currently running tasks.
  2812. */
  2813. gcwq->trustee_state = TRUSTEE_IN_CHARGE;
  2814. wake_up_all(&gcwq->trustee_wait);
  2815. /*
  2816. * The original cpu is in the process of dying and may go away
  2817. * anytime now. When that happens, we and all workers would
  2818. * be migrated to other cpus. Try draining any left work. We
  2819. * want to get it over with ASAP - spam rescuers, wake up as
  2820. * many idlers as necessary and create new ones till the
  2821. * worklist is empty. Note that if the gcwq is frozen, there
  2822. * may be frozen works in freezeable cwqs. Don't declare
  2823. * completion while frozen.
  2824. */
  2825. while (gcwq->nr_workers != gcwq->nr_idle ||
  2826. gcwq->flags & GCWQ_FREEZING ||
  2827. gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
  2828. int nr_works = 0;
  2829. list_for_each_entry(work, &gcwq->worklist, entry) {
  2830. send_mayday(work);
  2831. nr_works++;
  2832. }
  2833. list_for_each_entry(worker, &gcwq->idle_list, entry) {
  2834. if (!nr_works--)
  2835. break;
  2836. wake_up_process(worker->task);
  2837. }
  2838. if (need_to_create_worker(gcwq)) {
  2839. spin_unlock_irq(&gcwq->lock);
  2840. worker = create_worker(gcwq, false);
  2841. spin_lock_irq(&gcwq->lock);
  2842. if (worker) {
  2843. worker->flags |= WORKER_ROGUE;
  2844. start_worker(worker);
  2845. }
  2846. }
  2847. /* give a breather */
  2848. if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
  2849. break;
  2850. }
  2851. /*
  2852. * Either all works have been scheduled and cpu is down, or
  2853. * cpu down has already been canceled. Wait for and butcher
  2854. * all workers till we're canceled.
  2855. */
  2856. do {
  2857. rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
  2858. while (!list_empty(&gcwq->idle_list))
  2859. destroy_worker(list_first_entry(&gcwq->idle_list,
  2860. struct worker, entry));
  2861. } while (gcwq->nr_workers && rc >= 0);
  2862. /*
  2863. * At this point, either draining has completed and no worker
  2864. * is left, or cpu down has been canceled or the cpu is being
  2865. * brought back up. There shouldn't be any idle one left.
  2866. * Tell the remaining busy ones to rebind once it finishes the
  2867. * currently scheduled works by scheduling the rebind_work.
  2868. */
  2869. WARN_ON(!list_empty(&gcwq->idle_list));
  2870. for_each_busy_worker(worker, i, pos, gcwq) {
  2871. struct work_struct *rebind_work = &worker->rebind_work;
  2872. /*
  2873. * Rebind_work may race with future cpu hotplug
  2874. * operations. Use a separate flag to mark that
  2875. * rebinding is scheduled.
  2876. */
  2877. worker->flags |= WORKER_REBIND;
  2878. worker->flags &= ~WORKER_ROGUE;
  2879. /* queue rebind_work, wq doesn't matter, use the default one */
  2880. if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
  2881. work_data_bits(rebind_work)))
  2882. continue;
  2883. debug_work_activate(rebind_work);
  2884. insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
  2885. worker->scheduled.next,
  2886. work_color_to_flags(WORK_NO_COLOR));
  2887. }
  2888. /* relinquish manager role */
  2889. gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
  2890. /* notify completion */
  2891. gcwq->trustee = NULL;
  2892. gcwq->trustee_state = TRUSTEE_DONE;
  2893. wake_up_all(&gcwq->trustee_wait);
  2894. spin_unlock_irq(&gcwq->lock);
  2895. return 0;
  2896. }
  2897. /**
  2898. * wait_trustee_state - wait for trustee to enter the specified state
  2899. * @gcwq: gcwq the trustee of interest belongs to
  2900. * @state: target state to wait for
  2901. *
  2902. * Wait for the trustee to reach @state. DONE is already matched.
  2903. *
  2904. * CONTEXT:
  2905. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  2906. * multiple times. To be used by cpu_callback.
  2907. */
  2908. static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
  2909. __releases(&gcwq->lock)
  2910. __acquires(&gcwq->lock)
  2911. {
  2912. if (!(gcwq->trustee_state == state ||
  2913. gcwq->trustee_state == TRUSTEE_DONE)) {
  2914. spin_unlock_irq(&gcwq->lock);
  2915. __wait_event(gcwq->trustee_wait,
  2916. gcwq->trustee_state == state ||
  2917. gcwq->trustee_state == TRUSTEE_DONE);
  2918. spin_lock_irq(&gcwq->lock);
  2919. }
  2920. }
  2921. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  2922. unsigned long action,
  2923. void *hcpu)
  2924. {
  2925. unsigned int cpu = (unsigned long)hcpu;
  2926. struct global_cwq *gcwq = get_gcwq(cpu);
  2927. struct task_struct *new_trustee = NULL;
  2928. struct worker *uninitialized_var(new_worker);
  2929. unsigned long flags;
  2930. action &= ~CPU_TASKS_FROZEN;
  2931. switch (action) {
  2932. case CPU_DOWN_PREPARE:
  2933. new_trustee = kthread_create(trustee_thread, gcwq,
  2934. "workqueue_trustee/%d\n", cpu);
  2935. if (IS_ERR(new_trustee))
  2936. return notifier_from_errno(PTR_ERR(new_trustee));
  2937. kthread_bind(new_trustee, cpu);
  2938. /* fall through */
  2939. case CPU_UP_PREPARE:
  2940. BUG_ON(gcwq->first_idle);
  2941. new_worker = create_worker(gcwq, false);
  2942. if (!new_worker) {
  2943. if (new_trustee)
  2944. kthread_stop(new_trustee);
  2945. return NOTIFY_BAD;
  2946. }
  2947. }
  2948. /* some are called w/ irq disabled, don't disturb irq status */
  2949. spin_lock_irqsave(&gcwq->lock, flags);
  2950. switch (action) {
  2951. case CPU_DOWN_PREPARE:
  2952. /* initialize trustee and tell it to acquire the gcwq */
  2953. BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
  2954. gcwq->trustee = new_trustee;
  2955. gcwq->trustee_state = TRUSTEE_START;
  2956. wake_up_process(gcwq->trustee);
  2957. wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
  2958. /* fall through */
  2959. case CPU_UP_PREPARE:
  2960. BUG_ON(gcwq->first_idle);
  2961. gcwq->first_idle = new_worker;
  2962. break;
  2963. case CPU_DYING:
  2964. /*
  2965. * Before this, the trustee and all workers except for
  2966. * the ones which are still executing works from
  2967. * before the last CPU down must be on the cpu. After
  2968. * this, they'll all be diasporas.
  2969. */
  2970. gcwq->flags |= GCWQ_DISASSOCIATED;
  2971. break;
  2972. case CPU_POST_DEAD:
  2973. gcwq->trustee_state = TRUSTEE_BUTCHER;
  2974. /* fall through */
  2975. case CPU_UP_CANCELED:
  2976. destroy_worker(gcwq->first_idle);
  2977. gcwq->first_idle = NULL;
  2978. break;
  2979. case CPU_DOWN_FAILED:
  2980. case CPU_ONLINE:
  2981. gcwq->flags &= ~GCWQ_DISASSOCIATED;
  2982. if (gcwq->trustee_state != TRUSTEE_DONE) {
  2983. gcwq->trustee_state = TRUSTEE_RELEASE;
  2984. wake_up_process(gcwq->trustee);
  2985. wait_trustee_state(gcwq, TRUSTEE_DONE);
  2986. }
  2987. /*
  2988. * Trustee is done and there might be no worker left.
  2989. * Put the first_idle in and request a real manager to
  2990. * take a look.
  2991. */
  2992. spin_unlock_irq(&gcwq->lock);
  2993. kthread_bind(gcwq->first_idle->task, cpu);
  2994. spin_lock_irq(&gcwq->lock);
  2995. gcwq->flags |= GCWQ_MANAGE_WORKERS;
  2996. start_worker(gcwq->first_idle);
  2997. gcwq->first_idle = NULL;
  2998. break;
  2999. }
  3000. spin_unlock_irqrestore(&gcwq->lock, flags);
  3001. return notifier_from_errno(0);
  3002. }
  3003. #ifdef CONFIG_SMP
  3004. struct work_for_cpu {
  3005. struct completion completion;
  3006. long (*fn)(void *);
  3007. void *arg;
  3008. long ret;
  3009. };
  3010. static int do_work_for_cpu(void *_wfc)
  3011. {
  3012. struct work_for_cpu *wfc = _wfc;
  3013. wfc->ret = wfc->fn(wfc->arg);
  3014. complete(&wfc->completion);
  3015. return 0;
  3016. }
  3017. /**
  3018. * work_on_cpu - run a function in user context on a particular cpu
  3019. * @cpu: the cpu to run on
  3020. * @fn: the function to run
  3021. * @arg: the function arg
  3022. *
  3023. * This will return the value @fn returns.
  3024. * It is up to the caller to ensure that the cpu doesn't go offline.
  3025. * The caller must not hold any locks which would prevent @fn from completing.
  3026. */
  3027. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  3028. {
  3029. struct task_struct *sub_thread;
  3030. struct work_for_cpu wfc = {
  3031. .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
  3032. .fn = fn,
  3033. .arg = arg,
  3034. };
  3035. sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
  3036. if (IS_ERR(sub_thread))
  3037. return PTR_ERR(sub_thread);
  3038. kthread_bind(sub_thread, cpu);
  3039. wake_up_process(sub_thread);
  3040. wait_for_completion(&wfc.completion);
  3041. return wfc.ret;
  3042. }
  3043. EXPORT_SYMBOL_GPL(work_on_cpu);
  3044. #endif /* CONFIG_SMP */
  3045. #ifdef CONFIG_FREEZER
  3046. /**
  3047. * freeze_workqueues_begin - begin freezing workqueues
  3048. *
  3049. * Start freezing workqueues. After this function returns, all
  3050. * freezeable workqueues will queue new works to their frozen_works
  3051. * list instead of gcwq->worklist.
  3052. *
  3053. * CONTEXT:
  3054. * Grabs and releases workqueue_lock and gcwq->lock's.
  3055. */
  3056. void freeze_workqueues_begin(void)
  3057. {
  3058. unsigned int cpu;
  3059. spin_lock(&workqueue_lock);
  3060. BUG_ON(workqueue_freezing);
  3061. workqueue_freezing = true;
  3062. for_each_gcwq_cpu(cpu) {
  3063. struct global_cwq *gcwq = get_gcwq(cpu);
  3064. struct workqueue_struct *wq;
  3065. spin_lock_irq(&gcwq->lock);
  3066. BUG_ON(gcwq->flags & GCWQ_FREEZING);
  3067. gcwq->flags |= GCWQ_FREEZING;
  3068. list_for_each_entry(wq, &workqueues, list) {
  3069. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  3070. if (cwq && wq->flags & WQ_FREEZEABLE)
  3071. cwq->max_active = 0;
  3072. }
  3073. spin_unlock_irq(&gcwq->lock);
  3074. }
  3075. spin_unlock(&workqueue_lock);
  3076. }
  3077. /**
  3078. * freeze_workqueues_busy - are freezeable workqueues still busy?
  3079. *
  3080. * Check whether freezing is complete. This function must be called
  3081. * between freeze_workqueues_begin() and thaw_workqueues().
  3082. *
  3083. * CONTEXT:
  3084. * Grabs and releases workqueue_lock.
  3085. *
  3086. * RETURNS:
  3087. * %true if some freezeable workqueues are still busy. %false if
  3088. * freezing is complete.
  3089. */
  3090. bool freeze_workqueues_busy(void)
  3091. {
  3092. unsigned int cpu;
  3093. bool busy = false;
  3094. spin_lock(&workqueue_lock);
  3095. BUG_ON(!workqueue_freezing);
  3096. for_each_gcwq_cpu(cpu) {
  3097. struct workqueue_struct *wq;
  3098. /*
  3099. * nr_active is monotonically decreasing. It's safe
  3100. * to peek without lock.
  3101. */
  3102. list_for_each_entry(wq, &workqueues, list) {
  3103. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  3104. if (!cwq || !(wq->flags & WQ_FREEZEABLE))
  3105. continue;
  3106. BUG_ON(cwq->nr_active < 0);
  3107. if (cwq->nr_active) {
  3108. busy = true;
  3109. goto out_unlock;
  3110. }
  3111. }
  3112. }
  3113. out_unlock:
  3114. spin_unlock(&workqueue_lock);
  3115. return busy;
  3116. }
  3117. /**
  3118. * thaw_workqueues - thaw workqueues
  3119. *
  3120. * Thaw workqueues. Normal queueing is restored and all collected
  3121. * frozen works are transferred to their respective gcwq worklists.
  3122. *
  3123. * CONTEXT:
  3124. * Grabs and releases workqueue_lock and gcwq->lock's.
  3125. */
  3126. void thaw_workqueues(void)
  3127. {
  3128. unsigned int cpu;
  3129. spin_lock(&workqueue_lock);
  3130. if (!workqueue_freezing)
  3131. goto out_unlock;
  3132. for_each_gcwq_cpu(cpu) {
  3133. struct global_cwq *gcwq = get_gcwq(cpu);
  3134. struct workqueue_struct *wq;
  3135. spin_lock_irq(&gcwq->lock);
  3136. BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
  3137. gcwq->flags &= ~GCWQ_FREEZING;
  3138. list_for_each_entry(wq, &workqueues, list) {
  3139. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  3140. if (!cwq || !(wq->flags & WQ_FREEZEABLE))
  3141. continue;
  3142. /* restore max_active and repopulate worklist */
  3143. cwq->max_active = wq->saved_max_active;
  3144. while (!list_empty(&cwq->delayed_works) &&
  3145. cwq->nr_active < cwq->max_active)
  3146. cwq_activate_first_delayed(cwq);
  3147. }
  3148. wake_up_worker(gcwq);
  3149. spin_unlock_irq(&gcwq->lock);
  3150. }
  3151. workqueue_freezing = false;
  3152. out_unlock:
  3153. spin_unlock(&workqueue_lock);
  3154. }
  3155. #endif /* CONFIG_FREEZER */
  3156. static int __init init_workqueues(void)
  3157. {
  3158. unsigned int cpu;
  3159. int i;
  3160. cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
  3161. /* initialize gcwqs */
  3162. for_each_gcwq_cpu(cpu) {
  3163. struct global_cwq *gcwq = get_gcwq(cpu);
  3164. spin_lock_init(&gcwq->lock);
  3165. INIT_LIST_HEAD(&gcwq->worklist);
  3166. gcwq->cpu = cpu;
  3167. gcwq->flags |= GCWQ_DISASSOCIATED;
  3168. INIT_LIST_HEAD(&gcwq->idle_list);
  3169. for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
  3170. INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
  3171. init_timer_deferrable(&gcwq->idle_timer);
  3172. gcwq->idle_timer.function = idle_worker_timeout;
  3173. gcwq->idle_timer.data = (unsigned long)gcwq;
  3174. setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
  3175. (unsigned long)gcwq);
  3176. ida_init(&gcwq->worker_ida);
  3177. gcwq->trustee_state = TRUSTEE_DONE;
  3178. init_waitqueue_head(&gcwq->trustee_wait);
  3179. }
  3180. /* create the initial worker */
  3181. for_each_online_gcwq_cpu(cpu) {
  3182. struct global_cwq *gcwq = get_gcwq(cpu);
  3183. struct worker *worker;
  3184. if (cpu != WORK_CPU_UNBOUND)
  3185. gcwq->flags &= ~GCWQ_DISASSOCIATED;
  3186. worker = create_worker(gcwq, true);
  3187. BUG_ON(!worker);
  3188. spin_lock_irq(&gcwq->lock);
  3189. start_worker(worker);
  3190. spin_unlock_irq(&gcwq->lock);
  3191. }
  3192. system_wq = alloc_workqueue("events", 0, 0);
  3193. system_long_wq = alloc_workqueue("events_long", 0, 0);
  3194. system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
  3195. system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
  3196. WQ_UNBOUND_MAX_ACTIVE);
  3197. BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
  3198. !system_unbound_wq);
  3199. return 0;
  3200. }
  3201. early_initcall(init_workqueues);