workqueue.c 104 KB

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