workqueue.c 98 KB

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