workqueue.c 102 KB

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