workqueue.c 105 KB

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