workqueue.c 95 KB

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