workqueue.c 103 KB

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