workqueue.c 103 KB

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