workqueue.c 100 KB

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