workqueue.c 103 KB

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