workqueue.c 103 KB

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