workqueue.c 102 KB

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