workqueue.c 104 KB

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