workqueue.c 102 KB

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