workqueue.c 100 KB

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