workqueue.c 103 KB

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