workqueue.c 103 KB

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