workqueue.c 104 KB

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