workqueue.c 104 KB

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