workqueue.c 104 KB

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