workqueue.c 105 KB

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