workqueue.c 103 KB

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