workqueue.c 103 KB

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