workqueue.c 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861
  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/jhash.h>
  44. #include <linux/hashtable.h>
  45. #include <linux/rculist.h>
  46. #include <linux/nodemask.h>
  47. #include <linux/moduleparam.h>
  48. #include "workqueue_internal.h"
  49. enum {
  50. /*
  51. * worker_pool flags
  52. *
  53. * A bound pool is either associated or disassociated with its CPU.
  54. * While associated (!DISASSOCIATED), all workers are bound to the
  55. * CPU and none has %WORKER_UNBOUND set and concurrency management
  56. * is in effect.
  57. *
  58. * While DISASSOCIATED, the cpu may be offline and all workers have
  59. * %WORKER_UNBOUND set and concurrency management disabled, and may
  60. * be executing on any CPU. The pool behaves as an unbound one.
  61. *
  62. * Note that DISASSOCIATED should be flipped only while holding
  63. * manager_mutex to avoid changing binding state while
  64. * create_worker() is in progress.
  65. */
  66. POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
  67. POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
  68. POOL_FREEZING = 1 << 3, /* freeze in progress */
  69. /* worker flags */
  70. WORKER_STARTED = 1 << 0, /* started */
  71. WORKER_DIE = 1 << 1, /* die die die */
  72. WORKER_IDLE = 1 << 2, /* is idle */
  73. WORKER_PREP = 1 << 3, /* preparing to run works */
  74. WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
  75. WORKER_UNBOUND = 1 << 7, /* worker is unbound */
  76. WORKER_REBOUND = 1 << 8, /* worker was rebound */
  77. WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
  78. WORKER_UNBOUND | WORKER_REBOUND,
  79. NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
  80. UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
  81. BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
  82. MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
  83. IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
  84. MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
  85. /* call for help after 10ms
  86. (min two ticks) */
  87. MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
  88. CREATE_COOLDOWN = HZ, /* time to breath after fail */
  89. /*
  90. * Rescue workers are used only on emergencies and shared by
  91. * all cpus. Give -20.
  92. */
  93. RESCUER_NICE_LEVEL = -20,
  94. HIGHPRI_NICE_LEVEL = -20,
  95. WQ_NAME_LEN = 24,
  96. };
  97. /*
  98. * Structure fields follow one of the following exclusion rules.
  99. *
  100. * I: Modifiable by initialization/destruction paths and read-only for
  101. * everyone else.
  102. *
  103. * P: Preemption protected. Disabling preemption is enough and should
  104. * only be modified and accessed from the local cpu.
  105. *
  106. * L: pool->lock protected. Access with pool->lock held.
  107. *
  108. * X: During normal operation, modification requires pool->lock and should
  109. * be done only from local cpu. Either disabling preemption on local
  110. * cpu or grabbing pool->lock is enough for read access. If
  111. * POOL_DISASSOCIATED is set, it's identical to L.
  112. *
  113. * MG: pool->manager_mutex and pool->lock protected. Writes require both
  114. * locks. Reads can happen under either lock.
  115. *
  116. * PL: wq_pool_mutex protected.
  117. *
  118. * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  119. *
  120. * WQ: wq->mutex protected.
  121. *
  122. * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  123. *
  124. * MD: wq_mayday_lock protected.
  125. */
  126. /* struct worker is defined in workqueue_internal.h */
  127. struct worker_pool {
  128. spinlock_t lock; /* the pool lock */
  129. int cpu; /* I: the associated cpu */
  130. int node; /* I: the associated node ID */
  131. int id; /* I: pool ID */
  132. unsigned int flags; /* X: flags */
  133. struct list_head worklist; /* L: list of pending works */
  134. int nr_workers; /* L: total number of workers */
  135. /* nr_idle includes the ones off idle_list for rebinding */
  136. int nr_idle; /* L: currently idle ones */
  137. struct list_head idle_list; /* X: list of idle workers */
  138. struct timer_list idle_timer; /* L: worker idle timeout */
  139. struct timer_list mayday_timer; /* L: SOS timer for workers */
  140. /* a workers is either on busy_hash or idle_list, or the manager */
  141. DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
  142. /* L: hash of busy workers */
  143. /* see manage_workers() for details on the two manager mutexes */
  144. struct mutex manager_arb; /* manager arbitration */
  145. struct mutex manager_mutex; /* manager exclusion */
  146. struct idr worker_idr; /* MG: worker IDs and iteration */
  147. struct workqueue_attrs *attrs; /* I: worker attributes */
  148. struct hlist_node hash_node; /* PL: unbound_pool_hash node */
  149. int refcnt; /* PL: refcnt for unbound pools */
  150. /*
  151. * The current concurrency level. As it's likely to be accessed
  152. * from other CPUs during try_to_wake_up(), put it in a separate
  153. * cacheline.
  154. */
  155. atomic_t nr_running ____cacheline_aligned_in_smp;
  156. /*
  157. * Destruction of pool is sched-RCU protected to allow dereferences
  158. * from get_work_pool().
  159. */
  160. struct rcu_head rcu;
  161. } ____cacheline_aligned_in_smp;
  162. /*
  163. * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
  164. * of work_struct->data are used for flags and the remaining high bits
  165. * point to the pwq; thus, pwqs need to be aligned at two's power of the
  166. * number of flag bits.
  167. */
  168. struct pool_workqueue {
  169. struct worker_pool *pool; /* I: the associated pool */
  170. struct workqueue_struct *wq; /* I: the owning workqueue */
  171. int work_color; /* L: current color */
  172. int flush_color; /* L: flushing color */
  173. int refcnt; /* L: reference count */
  174. int nr_in_flight[WORK_NR_COLORS];
  175. /* L: nr of in_flight works */
  176. int nr_active; /* L: nr of active works */
  177. int max_active; /* L: max active works */
  178. struct list_head delayed_works; /* L: delayed works */
  179. struct list_head pwqs_node; /* WR: node on wq->pwqs */
  180. struct list_head mayday_node; /* MD: node on wq->maydays */
  181. /*
  182. * Release of unbound pwq is punted to system_wq. See put_pwq()
  183. * and pwq_unbound_release_workfn() for details. pool_workqueue
  184. * itself is also sched-RCU protected so that the first pwq can be
  185. * determined without grabbing wq->mutex.
  186. */
  187. struct work_struct unbound_release_work;
  188. struct rcu_head rcu;
  189. } __aligned(1 << WORK_STRUCT_FLAG_BITS);
  190. /*
  191. * Structure used to wait for workqueue flush.
  192. */
  193. struct wq_flusher {
  194. struct list_head list; /* WQ: list of flushers */
  195. int flush_color; /* WQ: flush color waiting for */
  196. struct completion done; /* flush completion */
  197. };
  198. struct wq_device;
  199. /*
  200. * The externally visible workqueue. It relays the issued work items to
  201. * the appropriate worker_pool through its pool_workqueues.
  202. */
  203. struct workqueue_struct {
  204. struct list_head pwqs; /* WR: all pwqs of this wq */
  205. struct list_head list; /* PL: list of all workqueues */
  206. struct mutex mutex; /* protects this wq */
  207. int work_color; /* WQ: current work color */
  208. int flush_color; /* WQ: current flush color */
  209. atomic_t nr_pwqs_to_flush; /* flush in progress */
  210. struct wq_flusher *first_flusher; /* WQ: first flusher */
  211. struct list_head flusher_queue; /* WQ: flush waiters */
  212. struct list_head flusher_overflow; /* WQ: flush overflow list */
  213. struct list_head maydays; /* MD: pwqs requesting rescue */
  214. struct worker *rescuer; /* I: rescue worker */
  215. int nr_drainers; /* WQ: drain in progress */
  216. int saved_max_active; /* WQ: saved pwq max_active */
  217. struct workqueue_attrs *unbound_attrs; /* WQ: only for unbound wqs */
  218. struct pool_workqueue *dfl_pwq; /* WQ: only for unbound wqs */
  219. #ifdef CONFIG_SYSFS
  220. struct wq_device *wq_dev; /* I: for sysfs interface */
  221. #endif
  222. #ifdef CONFIG_LOCKDEP
  223. struct lockdep_map lockdep_map;
  224. #endif
  225. char name[WQ_NAME_LEN]; /* I: workqueue name */
  226. /* hot fields used during command issue, aligned to cacheline */
  227. unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
  228. struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
  229. struct pool_workqueue __rcu *numa_pwq_tbl[]; /* FR: unbound pwqs indexed by node */
  230. };
  231. static struct kmem_cache *pwq_cache;
  232. static int wq_numa_tbl_len; /* highest possible NUMA node id + 1 */
  233. static cpumask_var_t *wq_numa_possible_cpumask;
  234. /* possible CPUs of each node */
  235. static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
  236. /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
  237. static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
  238. static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
  239. static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
  240. static LIST_HEAD(workqueues); /* PL: list of all workqueues */
  241. static bool workqueue_freezing; /* PL: have wqs started freezing? */
  242. /* the per-cpu worker pools */
  243. static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
  244. cpu_worker_pools);
  245. static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
  246. /* PL: hash of all unbound pools keyed by pool->attrs */
  247. static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
  248. /* I: attributes used when instantiating standard unbound pools on demand */
  249. static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
  250. struct workqueue_struct *system_wq __read_mostly;
  251. EXPORT_SYMBOL_GPL(system_wq);
  252. struct workqueue_struct *system_highpri_wq __read_mostly;
  253. EXPORT_SYMBOL_GPL(system_highpri_wq);
  254. struct workqueue_struct *system_long_wq __read_mostly;
  255. EXPORT_SYMBOL_GPL(system_long_wq);
  256. struct workqueue_struct *system_unbound_wq __read_mostly;
  257. EXPORT_SYMBOL_GPL(system_unbound_wq);
  258. struct workqueue_struct *system_freezable_wq __read_mostly;
  259. EXPORT_SYMBOL_GPL(system_freezable_wq);
  260. static int worker_thread(void *__worker);
  261. static void copy_workqueue_attrs(struct workqueue_attrs *to,
  262. const struct workqueue_attrs *from);
  263. #define CREATE_TRACE_POINTS
  264. #include <trace/events/workqueue.h>
  265. #define assert_rcu_or_pool_mutex() \
  266. rcu_lockdep_assert(rcu_read_lock_sched_held() || \
  267. lockdep_is_held(&wq_pool_mutex), \
  268. "sched RCU or wq_pool_mutex should be held")
  269. #define assert_rcu_or_wq_mutex(wq) \
  270. rcu_lockdep_assert(rcu_read_lock_sched_held() || \
  271. lockdep_is_held(&wq->mutex), \
  272. "sched RCU or wq->mutex should be held")
  273. #ifdef CONFIG_LOCKDEP
  274. #define assert_manager_or_pool_lock(pool) \
  275. WARN_ONCE(debug_locks && \
  276. !lockdep_is_held(&(pool)->manager_mutex) && \
  277. !lockdep_is_held(&(pool)->lock), \
  278. "pool->manager_mutex or ->lock should be held")
  279. #else
  280. #define assert_manager_or_pool_lock(pool) do { } while (0)
  281. #endif
  282. #define for_each_cpu_worker_pool(pool, cpu) \
  283. for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
  284. (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
  285. (pool)++)
  286. /**
  287. * for_each_pool - iterate through all worker_pools in the system
  288. * @pool: iteration cursor
  289. * @pi: integer used for iteration
  290. *
  291. * This must be called either with wq_pool_mutex held or sched RCU read
  292. * locked. If the pool needs to be used beyond the locking in effect, the
  293. * caller is responsible for guaranteeing that the pool stays online.
  294. *
  295. * The if/else clause exists only for the lockdep assertion and can be
  296. * ignored.
  297. */
  298. #define for_each_pool(pool, pi) \
  299. idr_for_each_entry(&worker_pool_idr, pool, pi) \
  300. if (({ assert_rcu_or_pool_mutex(); false; })) { } \
  301. else
  302. /**
  303. * for_each_pool_worker - iterate through all workers of a worker_pool
  304. * @worker: iteration cursor
  305. * @wi: integer used for iteration
  306. * @pool: worker_pool to iterate workers of
  307. *
  308. * This must be called with either @pool->manager_mutex or ->lock held.
  309. *
  310. * The if/else clause exists only for the lockdep assertion and can be
  311. * ignored.
  312. */
  313. #define for_each_pool_worker(worker, wi, pool) \
  314. idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
  315. if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
  316. else
  317. /**
  318. * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
  319. * @pwq: iteration cursor
  320. * @wq: the target workqueue
  321. *
  322. * This must be called either with wq->mutex held or sched RCU read locked.
  323. * If the pwq needs to be used beyond the locking in effect, the caller is
  324. * responsible for guaranteeing that the pwq stays online.
  325. *
  326. * The if/else clause exists only for the lockdep assertion and can be
  327. * ignored.
  328. */
  329. #define for_each_pwq(pwq, wq) \
  330. list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
  331. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  332. else
  333. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  334. static struct debug_obj_descr work_debug_descr;
  335. static void *work_debug_hint(void *addr)
  336. {
  337. return ((struct work_struct *) addr)->func;
  338. }
  339. /*
  340. * fixup_init is called when:
  341. * - an active object is initialized
  342. */
  343. static int work_fixup_init(void *addr, enum debug_obj_state state)
  344. {
  345. struct work_struct *work = addr;
  346. switch (state) {
  347. case ODEBUG_STATE_ACTIVE:
  348. cancel_work_sync(work);
  349. debug_object_init(work, &work_debug_descr);
  350. return 1;
  351. default:
  352. return 0;
  353. }
  354. }
  355. /*
  356. * fixup_activate is called when:
  357. * - an active object is activated
  358. * - an unknown object is activated (might be a statically initialized object)
  359. */
  360. static int work_fixup_activate(void *addr, enum debug_obj_state state)
  361. {
  362. struct work_struct *work = addr;
  363. switch (state) {
  364. case ODEBUG_STATE_NOTAVAILABLE:
  365. /*
  366. * This is not really a fixup. The work struct was
  367. * statically initialized. We just make sure that it
  368. * is tracked in the object tracker.
  369. */
  370. if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
  371. debug_object_init(work, &work_debug_descr);
  372. debug_object_activate(work, &work_debug_descr);
  373. return 0;
  374. }
  375. WARN_ON_ONCE(1);
  376. return 0;
  377. case ODEBUG_STATE_ACTIVE:
  378. WARN_ON(1);
  379. default:
  380. return 0;
  381. }
  382. }
  383. /*
  384. * fixup_free is called when:
  385. * - an active object is freed
  386. */
  387. static int work_fixup_free(void *addr, enum debug_obj_state state)
  388. {
  389. struct work_struct *work = addr;
  390. switch (state) {
  391. case ODEBUG_STATE_ACTIVE:
  392. cancel_work_sync(work);
  393. debug_object_free(work, &work_debug_descr);
  394. return 1;
  395. default:
  396. return 0;
  397. }
  398. }
  399. static struct debug_obj_descr work_debug_descr = {
  400. .name = "work_struct",
  401. .debug_hint = work_debug_hint,
  402. .fixup_init = work_fixup_init,
  403. .fixup_activate = work_fixup_activate,
  404. .fixup_free = work_fixup_free,
  405. };
  406. static inline void debug_work_activate(struct work_struct *work)
  407. {
  408. debug_object_activate(work, &work_debug_descr);
  409. }
  410. static inline void debug_work_deactivate(struct work_struct *work)
  411. {
  412. debug_object_deactivate(work, &work_debug_descr);
  413. }
  414. void __init_work(struct work_struct *work, int onstack)
  415. {
  416. if (onstack)
  417. debug_object_init_on_stack(work, &work_debug_descr);
  418. else
  419. debug_object_init(work, &work_debug_descr);
  420. }
  421. EXPORT_SYMBOL_GPL(__init_work);
  422. void destroy_work_on_stack(struct work_struct *work)
  423. {
  424. debug_object_free(work, &work_debug_descr);
  425. }
  426. EXPORT_SYMBOL_GPL(destroy_work_on_stack);
  427. #else
  428. static inline void debug_work_activate(struct work_struct *work) { }
  429. static inline void debug_work_deactivate(struct work_struct *work) { }
  430. #endif
  431. /* allocate ID and assign it to @pool */
  432. static int worker_pool_assign_id(struct worker_pool *pool)
  433. {
  434. int ret;
  435. lockdep_assert_held(&wq_pool_mutex);
  436. do {
  437. if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
  438. return -ENOMEM;
  439. ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
  440. } while (ret == -EAGAIN);
  441. return ret;
  442. }
  443. /**
  444. * first_pwq - return the first pool_workqueue of the specified workqueue
  445. * @wq: the target workqueue
  446. *
  447. * This must be called either with wq->mutex held or sched RCU read locked.
  448. * If the pwq needs to be used beyond the locking in effect, the caller is
  449. * responsible for guaranteeing that the pwq stays online.
  450. */
  451. static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
  452. {
  453. assert_rcu_or_wq_mutex(wq);
  454. return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
  455. pwqs_node);
  456. }
  457. /**
  458. * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
  459. * @wq: the target workqueue
  460. * @node: the node ID
  461. *
  462. * This must be called either with pwq_lock held or sched RCU read locked.
  463. * If the pwq needs to be used beyond the locking in effect, the caller is
  464. * responsible for guaranteeing that the pwq stays online.
  465. */
  466. static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
  467. int node)
  468. {
  469. assert_rcu_or_wq_mutex(wq);
  470. return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
  471. }
  472. static unsigned int work_color_to_flags(int color)
  473. {
  474. return color << WORK_STRUCT_COLOR_SHIFT;
  475. }
  476. static int get_work_color(struct work_struct *work)
  477. {
  478. return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
  479. ((1 << WORK_STRUCT_COLOR_BITS) - 1);
  480. }
  481. static int work_next_color(int color)
  482. {
  483. return (color + 1) % WORK_NR_COLORS;
  484. }
  485. /*
  486. * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
  487. * contain the pointer to the queued pwq. Once execution starts, the flag
  488. * is cleared and the high bits contain OFFQ flags and pool ID.
  489. *
  490. * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
  491. * and clear_work_data() can be used to set the pwq, pool or clear
  492. * work->data. These functions should only be called while the work is
  493. * owned - ie. while the PENDING bit is set.
  494. *
  495. * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
  496. * corresponding to a work. Pool is available once the work has been
  497. * queued anywhere after initialization until it is sync canceled. pwq is
  498. * available only while the work item is queued.
  499. *
  500. * %WORK_OFFQ_CANCELING is used to mark a work item which is being
  501. * canceled. While being canceled, a work item may have its PENDING set
  502. * but stay off timer and worklist for arbitrarily long and nobody should
  503. * try to steal the PENDING bit.
  504. */
  505. static inline void set_work_data(struct work_struct *work, unsigned long data,
  506. unsigned long flags)
  507. {
  508. WARN_ON_ONCE(!work_pending(work));
  509. atomic_long_set(&work->data, data | flags | work_static(work));
  510. }
  511. static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
  512. unsigned long extra_flags)
  513. {
  514. set_work_data(work, (unsigned long)pwq,
  515. WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
  516. }
  517. static void set_work_pool_and_keep_pending(struct work_struct *work,
  518. int pool_id)
  519. {
  520. set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
  521. WORK_STRUCT_PENDING);
  522. }
  523. static void set_work_pool_and_clear_pending(struct work_struct *work,
  524. int pool_id)
  525. {
  526. /*
  527. * The following wmb is paired with the implied mb in
  528. * test_and_set_bit(PENDING) and ensures all updates to @work made
  529. * here are visible to and precede any updates by the next PENDING
  530. * owner.
  531. */
  532. smp_wmb();
  533. set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
  534. }
  535. static void clear_work_data(struct work_struct *work)
  536. {
  537. smp_wmb(); /* see set_work_pool_and_clear_pending() */
  538. set_work_data(work, WORK_STRUCT_NO_POOL, 0);
  539. }
  540. static struct pool_workqueue *get_work_pwq(struct work_struct *work)
  541. {
  542. unsigned long data = atomic_long_read(&work->data);
  543. if (data & WORK_STRUCT_PWQ)
  544. return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
  545. else
  546. return NULL;
  547. }
  548. /**
  549. * get_work_pool - return the worker_pool a given work was associated with
  550. * @work: the work item of interest
  551. *
  552. * Return the worker_pool @work was last associated with. %NULL if none.
  553. *
  554. * Pools are created and destroyed under wq_pool_mutex, and allows read
  555. * access under sched-RCU read lock. As such, this function should be
  556. * called under wq_pool_mutex or with preemption disabled.
  557. *
  558. * All fields of the returned pool are accessible as long as the above
  559. * mentioned locking is in effect. If the returned pool needs to be used
  560. * beyond the critical section, the caller is responsible for ensuring the
  561. * returned pool is and stays online.
  562. */
  563. static struct worker_pool *get_work_pool(struct work_struct *work)
  564. {
  565. unsigned long data = atomic_long_read(&work->data);
  566. int pool_id;
  567. assert_rcu_or_pool_mutex();
  568. if (data & WORK_STRUCT_PWQ)
  569. return ((struct pool_workqueue *)
  570. (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
  571. pool_id = data >> WORK_OFFQ_POOL_SHIFT;
  572. if (pool_id == WORK_OFFQ_POOL_NONE)
  573. return NULL;
  574. return idr_find(&worker_pool_idr, pool_id);
  575. }
  576. /**
  577. * get_work_pool_id - return the worker pool ID a given work is associated with
  578. * @work: the work item of interest
  579. *
  580. * Return the worker_pool ID @work was last associated with.
  581. * %WORK_OFFQ_POOL_NONE if none.
  582. */
  583. static int get_work_pool_id(struct work_struct *work)
  584. {
  585. unsigned long data = atomic_long_read(&work->data);
  586. if (data & WORK_STRUCT_PWQ)
  587. return ((struct pool_workqueue *)
  588. (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
  589. return data >> WORK_OFFQ_POOL_SHIFT;
  590. }
  591. static void mark_work_canceling(struct work_struct *work)
  592. {
  593. unsigned long pool_id = get_work_pool_id(work);
  594. pool_id <<= WORK_OFFQ_POOL_SHIFT;
  595. set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
  596. }
  597. static bool work_is_canceling(struct work_struct *work)
  598. {
  599. unsigned long data = atomic_long_read(&work->data);
  600. return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
  601. }
  602. /*
  603. * Policy functions. These define the policies on how the global worker
  604. * pools are managed. Unless noted otherwise, these functions assume that
  605. * they're being called with pool->lock held.
  606. */
  607. static bool __need_more_worker(struct worker_pool *pool)
  608. {
  609. return !atomic_read(&pool->nr_running);
  610. }
  611. /*
  612. * Need to wake up a worker? Called from anything but currently
  613. * running workers.
  614. *
  615. * Note that, because unbound workers never contribute to nr_running, this
  616. * function will always return %true for unbound pools as long as the
  617. * worklist isn't empty.
  618. */
  619. static bool need_more_worker(struct worker_pool *pool)
  620. {
  621. return !list_empty(&pool->worklist) && __need_more_worker(pool);
  622. }
  623. /* Can I start working? Called from busy but !running workers. */
  624. static bool may_start_working(struct worker_pool *pool)
  625. {
  626. return pool->nr_idle;
  627. }
  628. /* Do I need to keep working? Called from currently running workers. */
  629. static bool keep_working(struct worker_pool *pool)
  630. {
  631. return !list_empty(&pool->worklist) &&
  632. atomic_read(&pool->nr_running) <= 1;
  633. }
  634. /* Do we need a new worker? Called from manager. */
  635. static bool need_to_create_worker(struct worker_pool *pool)
  636. {
  637. return need_more_worker(pool) && !may_start_working(pool);
  638. }
  639. /* Do I need to be the manager? */
  640. static bool need_to_manage_workers(struct worker_pool *pool)
  641. {
  642. return need_to_create_worker(pool) ||
  643. (pool->flags & POOL_MANAGE_WORKERS);
  644. }
  645. /* Do we have too many workers and should some go away? */
  646. static bool too_many_workers(struct worker_pool *pool)
  647. {
  648. bool managing = mutex_is_locked(&pool->manager_arb);
  649. int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
  650. int nr_busy = pool->nr_workers - nr_idle;
  651. /*
  652. * nr_idle and idle_list may disagree if idle rebinding is in
  653. * progress. Never return %true if idle_list is empty.
  654. */
  655. if (list_empty(&pool->idle_list))
  656. return false;
  657. return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
  658. }
  659. /*
  660. * Wake up functions.
  661. */
  662. /* Return the first worker. Safe with preemption disabled */
  663. static struct worker *first_worker(struct worker_pool *pool)
  664. {
  665. if (unlikely(list_empty(&pool->idle_list)))
  666. return NULL;
  667. return list_first_entry(&pool->idle_list, struct worker, entry);
  668. }
  669. /**
  670. * wake_up_worker - wake up an idle worker
  671. * @pool: worker pool to wake worker from
  672. *
  673. * Wake up the first idle worker of @pool.
  674. *
  675. * CONTEXT:
  676. * spin_lock_irq(pool->lock).
  677. */
  678. static void wake_up_worker(struct worker_pool *pool)
  679. {
  680. struct worker *worker = first_worker(pool);
  681. if (likely(worker))
  682. wake_up_process(worker->task);
  683. }
  684. /**
  685. * wq_worker_waking_up - a worker is waking up
  686. * @task: task waking up
  687. * @cpu: CPU @task is waking up to
  688. *
  689. * This function is called during try_to_wake_up() when a worker is
  690. * being awoken.
  691. *
  692. * CONTEXT:
  693. * spin_lock_irq(rq->lock)
  694. */
  695. void wq_worker_waking_up(struct task_struct *task, int cpu)
  696. {
  697. struct worker *worker = kthread_data(task);
  698. if (!(worker->flags & WORKER_NOT_RUNNING)) {
  699. WARN_ON_ONCE(worker->pool->cpu != cpu);
  700. atomic_inc(&worker->pool->nr_running);
  701. }
  702. }
  703. /**
  704. * wq_worker_sleeping - a worker is going to sleep
  705. * @task: task going to sleep
  706. * @cpu: CPU in question, must be the current CPU number
  707. *
  708. * This function is called during schedule() when a busy worker is
  709. * going to sleep. Worker on the same cpu can be woken up by
  710. * returning pointer to its task.
  711. *
  712. * CONTEXT:
  713. * spin_lock_irq(rq->lock)
  714. *
  715. * RETURNS:
  716. * Worker task on @cpu to wake up, %NULL if none.
  717. */
  718. struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  719. {
  720. struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  721. struct worker_pool *pool;
  722. /*
  723. * Rescuers, which may not have all the fields set up like normal
  724. * workers, also reach here, let's not access anything before
  725. * checking NOT_RUNNING.
  726. */
  727. if (worker->flags & WORKER_NOT_RUNNING)
  728. return NULL;
  729. pool = worker->pool;
  730. /* this can only happen on the local cpu */
  731. if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
  732. return NULL;
  733. /*
  734. * The counterpart of the following dec_and_test, implied mb,
  735. * worklist not empty test sequence is in insert_work().
  736. * Please read comment there.
  737. *
  738. * NOT_RUNNING is clear. This means that we're bound to and
  739. * running on the local cpu w/ rq lock held and preemption
  740. * disabled, which in turn means that none else could be
  741. * manipulating idle_list, so dereferencing idle_list without pool
  742. * lock is safe.
  743. */
  744. if (atomic_dec_and_test(&pool->nr_running) &&
  745. !list_empty(&pool->worklist))
  746. to_wakeup = first_worker(pool);
  747. return to_wakeup ? to_wakeup->task : NULL;
  748. }
  749. /**
  750. * worker_set_flags - set worker flags and adjust nr_running accordingly
  751. * @worker: self
  752. * @flags: flags to set
  753. * @wakeup: wakeup an idle worker if necessary
  754. *
  755. * Set @flags in @worker->flags and adjust nr_running accordingly. If
  756. * nr_running becomes zero and @wakeup is %true, an idle worker is
  757. * woken up.
  758. *
  759. * CONTEXT:
  760. * spin_lock_irq(pool->lock)
  761. */
  762. static inline void worker_set_flags(struct worker *worker, unsigned int flags,
  763. bool wakeup)
  764. {
  765. struct worker_pool *pool = worker->pool;
  766. WARN_ON_ONCE(worker->task != current);
  767. /*
  768. * If transitioning into NOT_RUNNING, adjust nr_running and
  769. * wake up an idle worker as necessary if requested by
  770. * @wakeup.
  771. */
  772. if ((flags & WORKER_NOT_RUNNING) &&
  773. !(worker->flags & WORKER_NOT_RUNNING)) {
  774. if (wakeup) {
  775. if (atomic_dec_and_test(&pool->nr_running) &&
  776. !list_empty(&pool->worklist))
  777. wake_up_worker(pool);
  778. } else
  779. atomic_dec(&pool->nr_running);
  780. }
  781. worker->flags |= flags;
  782. }
  783. /**
  784. * worker_clr_flags - clear worker flags and adjust nr_running accordingly
  785. * @worker: self
  786. * @flags: flags to clear
  787. *
  788. * Clear @flags in @worker->flags and adjust nr_running accordingly.
  789. *
  790. * CONTEXT:
  791. * spin_lock_irq(pool->lock)
  792. */
  793. static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
  794. {
  795. struct worker_pool *pool = worker->pool;
  796. unsigned int oflags = worker->flags;
  797. WARN_ON_ONCE(worker->task != current);
  798. worker->flags &= ~flags;
  799. /*
  800. * If transitioning out of NOT_RUNNING, increment nr_running. Note
  801. * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
  802. * of multiple flags, not a single flag.
  803. */
  804. if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
  805. if (!(worker->flags & WORKER_NOT_RUNNING))
  806. atomic_inc(&pool->nr_running);
  807. }
  808. /**
  809. * find_worker_executing_work - find worker which is executing a work
  810. * @pool: pool of interest
  811. * @work: work to find worker for
  812. *
  813. * Find a worker which is executing @work on @pool by searching
  814. * @pool->busy_hash which is keyed by the address of @work. For a worker
  815. * to match, its current execution should match the address of @work and
  816. * its work function. This is to avoid unwanted dependency between
  817. * unrelated work executions through a work item being recycled while still
  818. * being executed.
  819. *
  820. * This is a bit tricky. A work item may be freed once its execution
  821. * starts and nothing prevents the freed area from being recycled for
  822. * another work item. If the same work item address ends up being reused
  823. * before the original execution finishes, workqueue will identify the
  824. * recycled work item as currently executing and make it wait until the
  825. * current execution finishes, introducing an unwanted dependency.
  826. *
  827. * This function checks the work item address and work function to avoid
  828. * false positives. Note that this isn't complete as one may construct a
  829. * work function which can introduce dependency onto itself through a
  830. * recycled work item. Well, if somebody wants to shoot oneself in the
  831. * foot that badly, there's only so much we can do, and if such deadlock
  832. * actually occurs, it should be easy to locate the culprit work function.
  833. *
  834. * CONTEXT:
  835. * spin_lock_irq(pool->lock).
  836. *
  837. * RETURNS:
  838. * Pointer to worker which is executing @work if found, NULL
  839. * otherwise.
  840. */
  841. static struct worker *find_worker_executing_work(struct worker_pool *pool,
  842. struct work_struct *work)
  843. {
  844. struct worker *worker;
  845. hash_for_each_possible(pool->busy_hash, worker, hentry,
  846. (unsigned long)work)
  847. if (worker->current_work == work &&
  848. worker->current_func == work->func)
  849. return worker;
  850. return NULL;
  851. }
  852. /**
  853. * move_linked_works - move linked works to a list
  854. * @work: start of series of works to be scheduled
  855. * @head: target list to append @work to
  856. * @nextp: out paramter for nested worklist walking
  857. *
  858. * Schedule linked works starting from @work to @head. Work series to
  859. * be scheduled starts at @work and includes any consecutive work with
  860. * WORK_STRUCT_LINKED set in its predecessor.
  861. *
  862. * If @nextp is not NULL, it's updated to point to the next work of
  863. * the last scheduled work. This allows move_linked_works() to be
  864. * nested inside outer list_for_each_entry_safe().
  865. *
  866. * CONTEXT:
  867. * spin_lock_irq(pool->lock).
  868. */
  869. static void move_linked_works(struct work_struct *work, struct list_head *head,
  870. struct work_struct **nextp)
  871. {
  872. struct work_struct *n;
  873. /*
  874. * Linked worklist will always end before the end of the list,
  875. * use NULL for list head.
  876. */
  877. list_for_each_entry_safe_from(work, n, NULL, entry) {
  878. list_move_tail(&work->entry, head);
  879. if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
  880. break;
  881. }
  882. /*
  883. * If we're already inside safe list traversal and have moved
  884. * multiple works to the scheduled queue, the next position
  885. * needs to be updated.
  886. */
  887. if (nextp)
  888. *nextp = n;
  889. }
  890. /**
  891. * get_pwq - get an extra reference on the specified pool_workqueue
  892. * @pwq: pool_workqueue to get
  893. *
  894. * Obtain an extra reference on @pwq. The caller should guarantee that
  895. * @pwq has positive refcnt and be holding the matching pool->lock.
  896. */
  897. static void get_pwq(struct pool_workqueue *pwq)
  898. {
  899. lockdep_assert_held(&pwq->pool->lock);
  900. WARN_ON_ONCE(pwq->refcnt <= 0);
  901. pwq->refcnt++;
  902. }
  903. /**
  904. * put_pwq - put a pool_workqueue reference
  905. * @pwq: pool_workqueue to put
  906. *
  907. * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
  908. * destruction. The caller should be holding the matching pool->lock.
  909. */
  910. static void put_pwq(struct pool_workqueue *pwq)
  911. {
  912. lockdep_assert_held(&pwq->pool->lock);
  913. if (likely(--pwq->refcnt))
  914. return;
  915. if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
  916. return;
  917. /*
  918. * @pwq can't be released under pool->lock, bounce to
  919. * pwq_unbound_release_workfn(). This never recurses on the same
  920. * pool->lock as this path is taken only for unbound workqueues and
  921. * the release work item is scheduled on a per-cpu workqueue. To
  922. * avoid lockdep warning, unbound pool->locks are given lockdep
  923. * subclass of 1 in get_unbound_pool().
  924. */
  925. schedule_work(&pwq->unbound_release_work);
  926. }
  927. /**
  928. * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
  929. * @pwq: pool_workqueue to put (can be %NULL)
  930. *
  931. * put_pwq() with locking. This function also allows %NULL @pwq.
  932. */
  933. static void put_pwq_unlocked(struct pool_workqueue *pwq)
  934. {
  935. if (pwq) {
  936. /*
  937. * As both pwqs and pools are sched-RCU protected, the
  938. * following lock operations are safe.
  939. */
  940. spin_lock_irq(&pwq->pool->lock);
  941. put_pwq(pwq);
  942. spin_unlock_irq(&pwq->pool->lock);
  943. }
  944. }
  945. static void pwq_activate_delayed_work(struct work_struct *work)
  946. {
  947. struct pool_workqueue *pwq = get_work_pwq(work);
  948. trace_workqueue_activate_work(work);
  949. move_linked_works(work, &pwq->pool->worklist, NULL);
  950. __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
  951. pwq->nr_active++;
  952. }
  953. static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
  954. {
  955. struct work_struct *work = list_first_entry(&pwq->delayed_works,
  956. struct work_struct, entry);
  957. pwq_activate_delayed_work(work);
  958. }
  959. /**
  960. * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
  961. * @pwq: pwq of interest
  962. * @color: color of work which left the queue
  963. *
  964. * A work either has completed or is removed from pending queue,
  965. * decrement nr_in_flight of its pwq and handle workqueue flushing.
  966. *
  967. * CONTEXT:
  968. * spin_lock_irq(pool->lock).
  969. */
  970. static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
  971. {
  972. /* uncolored work items don't participate in flushing or nr_active */
  973. if (color == WORK_NO_COLOR)
  974. goto out_put;
  975. pwq->nr_in_flight[color]--;
  976. pwq->nr_active--;
  977. if (!list_empty(&pwq->delayed_works)) {
  978. /* one down, submit a delayed one */
  979. if (pwq->nr_active < pwq->max_active)
  980. pwq_activate_first_delayed(pwq);
  981. }
  982. /* is flush in progress and are we at the flushing tip? */
  983. if (likely(pwq->flush_color != color))
  984. goto out_put;
  985. /* are there still in-flight works? */
  986. if (pwq->nr_in_flight[color])
  987. goto out_put;
  988. /* this pwq is done, clear flush_color */
  989. pwq->flush_color = -1;
  990. /*
  991. * If this was the last pwq, wake up the first flusher. It
  992. * will handle the rest.
  993. */
  994. if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
  995. complete(&pwq->wq->first_flusher->done);
  996. out_put:
  997. put_pwq(pwq);
  998. }
  999. /**
  1000. * try_to_grab_pending - steal work item from worklist and disable irq
  1001. * @work: work item to steal
  1002. * @is_dwork: @work is a delayed_work
  1003. * @flags: place to store irq state
  1004. *
  1005. * Try to grab PENDING bit of @work. This function can handle @work in any
  1006. * stable state - idle, on timer or on worklist. Return values are
  1007. *
  1008. * 1 if @work was pending and we successfully stole PENDING
  1009. * 0 if @work was idle and we claimed PENDING
  1010. * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
  1011. * -ENOENT if someone else is canceling @work, this state may persist
  1012. * for arbitrarily long
  1013. *
  1014. * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
  1015. * interrupted while holding PENDING and @work off queue, irq must be
  1016. * disabled on entry. This, combined with delayed_work->timer being
  1017. * irqsafe, ensures that we return -EAGAIN for finite short period of time.
  1018. *
  1019. * On successful return, >= 0, irq is disabled and the caller is
  1020. * responsible for releasing it using local_irq_restore(*@flags).
  1021. *
  1022. * This function is safe to call from any context including IRQ handler.
  1023. */
  1024. static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  1025. unsigned long *flags)
  1026. {
  1027. struct worker_pool *pool;
  1028. struct pool_workqueue *pwq;
  1029. local_irq_save(*flags);
  1030. /* try to steal the timer if it exists */
  1031. if (is_dwork) {
  1032. struct delayed_work *dwork = to_delayed_work(work);
  1033. /*
  1034. * dwork->timer is irqsafe. If del_timer() fails, it's
  1035. * guaranteed that the timer is not queued anywhere and not
  1036. * running on the local CPU.
  1037. */
  1038. if (likely(del_timer(&dwork->timer)))
  1039. return 1;
  1040. }
  1041. /* try to claim PENDING the normal way */
  1042. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  1043. return 0;
  1044. /*
  1045. * The queueing is in progress, or it is already queued. Try to
  1046. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  1047. */
  1048. pool = get_work_pool(work);
  1049. if (!pool)
  1050. goto fail;
  1051. spin_lock(&pool->lock);
  1052. /*
  1053. * work->data is guaranteed to point to pwq only while the work
  1054. * item is queued on pwq->wq, and both updating work->data to point
  1055. * to pwq on queueing and to pool on dequeueing are done under
  1056. * pwq->pool->lock. This in turn guarantees that, if work->data
  1057. * points to pwq which is associated with a locked pool, the work
  1058. * item is currently queued on that pool.
  1059. */
  1060. pwq = get_work_pwq(work);
  1061. if (pwq && pwq->pool == pool) {
  1062. debug_work_deactivate(work);
  1063. /*
  1064. * A delayed work item cannot be grabbed directly because
  1065. * it might have linked NO_COLOR work items which, if left
  1066. * on the delayed_list, will confuse pwq->nr_active
  1067. * management later on and cause stall. Make sure the work
  1068. * item is activated before grabbing.
  1069. */
  1070. if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
  1071. pwq_activate_delayed_work(work);
  1072. list_del_init(&work->entry);
  1073. pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
  1074. /* work->data points to pwq iff queued, point to pool */
  1075. set_work_pool_and_keep_pending(work, pool->id);
  1076. spin_unlock(&pool->lock);
  1077. return 1;
  1078. }
  1079. spin_unlock(&pool->lock);
  1080. fail:
  1081. local_irq_restore(*flags);
  1082. if (work_is_canceling(work))
  1083. return -ENOENT;
  1084. cpu_relax();
  1085. return -EAGAIN;
  1086. }
  1087. /**
  1088. * insert_work - insert a work into a pool
  1089. * @pwq: pwq @work belongs to
  1090. * @work: work to insert
  1091. * @head: insertion point
  1092. * @extra_flags: extra WORK_STRUCT_* flags to set
  1093. *
  1094. * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
  1095. * work_struct flags.
  1096. *
  1097. * CONTEXT:
  1098. * spin_lock_irq(pool->lock).
  1099. */
  1100. static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
  1101. struct list_head *head, unsigned int extra_flags)
  1102. {
  1103. struct worker_pool *pool = pwq->pool;
  1104. /* we own @work, set data and link */
  1105. set_work_pwq(work, pwq, extra_flags);
  1106. list_add_tail(&work->entry, head);
  1107. get_pwq(pwq);
  1108. /*
  1109. * Ensure either wq_worker_sleeping() sees the above
  1110. * list_add_tail() or we see zero nr_running to avoid workers lying
  1111. * around lazily while there are works to be processed.
  1112. */
  1113. smp_mb();
  1114. if (__need_more_worker(pool))
  1115. wake_up_worker(pool);
  1116. }
  1117. /*
  1118. * Test whether @work is being queued from another work executing on the
  1119. * same workqueue.
  1120. */
  1121. static bool is_chained_work(struct workqueue_struct *wq)
  1122. {
  1123. struct worker *worker;
  1124. worker = current_wq_worker();
  1125. /*
  1126. * Return %true iff I'm a worker execuing a work item on @wq. If
  1127. * I'm @worker, it's safe to dereference it without locking.
  1128. */
  1129. return worker && worker->current_pwq->wq == wq;
  1130. }
  1131. static void __queue_work(int cpu, struct workqueue_struct *wq,
  1132. struct work_struct *work)
  1133. {
  1134. struct pool_workqueue *pwq;
  1135. struct worker_pool *last_pool;
  1136. struct list_head *worklist;
  1137. unsigned int work_flags;
  1138. unsigned int req_cpu = cpu;
  1139. /*
  1140. * While a work item is PENDING && off queue, a task trying to
  1141. * steal the PENDING will busy-loop waiting for it to either get
  1142. * queued or lose PENDING. Grabbing PENDING and queueing should
  1143. * happen with IRQ disabled.
  1144. */
  1145. WARN_ON_ONCE(!irqs_disabled());
  1146. debug_work_activate(work);
  1147. /* if dying, only works from the same workqueue are allowed */
  1148. if (unlikely(wq->flags & __WQ_DRAINING) &&
  1149. WARN_ON_ONCE(!is_chained_work(wq)))
  1150. return;
  1151. retry:
  1152. if (req_cpu == WORK_CPU_UNBOUND)
  1153. cpu = raw_smp_processor_id();
  1154. /* pwq which will be used unless @work is executing elsewhere */
  1155. if (!(wq->flags & WQ_UNBOUND))
  1156. pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
  1157. else
  1158. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  1159. /*
  1160. * If @work was previously on a different pool, it might still be
  1161. * running there, in which case the work needs to be queued on that
  1162. * pool to guarantee non-reentrancy.
  1163. */
  1164. last_pool = get_work_pool(work);
  1165. if (last_pool && last_pool != pwq->pool) {
  1166. struct worker *worker;
  1167. spin_lock(&last_pool->lock);
  1168. worker = find_worker_executing_work(last_pool, work);
  1169. if (worker && worker->current_pwq->wq == wq) {
  1170. pwq = worker->current_pwq;
  1171. } else {
  1172. /* meh... not running there, queue here */
  1173. spin_unlock(&last_pool->lock);
  1174. spin_lock(&pwq->pool->lock);
  1175. }
  1176. } else {
  1177. spin_lock(&pwq->pool->lock);
  1178. }
  1179. /*
  1180. * pwq is determined and locked. For unbound pools, we could have
  1181. * raced with pwq release and it could already be dead. If its
  1182. * refcnt is zero, repeat pwq selection. Note that pwqs never die
  1183. * without another pwq replacing it in the numa_pwq_tbl or while
  1184. * work items are executing on it, so the retrying is guaranteed to
  1185. * make forward-progress.
  1186. */
  1187. if (unlikely(!pwq->refcnt)) {
  1188. if (wq->flags & WQ_UNBOUND) {
  1189. spin_unlock(&pwq->pool->lock);
  1190. cpu_relax();
  1191. goto retry;
  1192. }
  1193. /* oops */
  1194. WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
  1195. wq->name, cpu);
  1196. }
  1197. /* pwq determined, queue */
  1198. trace_workqueue_queue_work(req_cpu, pwq, work);
  1199. if (WARN_ON(!list_empty(&work->entry))) {
  1200. spin_unlock(&pwq->pool->lock);
  1201. return;
  1202. }
  1203. pwq->nr_in_flight[pwq->work_color]++;
  1204. work_flags = work_color_to_flags(pwq->work_color);
  1205. if (likely(pwq->nr_active < pwq->max_active)) {
  1206. trace_workqueue_activate_work(work);
  1207. pwq->nr_active++;
  1208. worklist = &pwq->pool->worklist;
  1209. } else {
  1210. work_flags |= WORK_STRUCT_DELAYED;
  1211. worklist = &pwq->delayed_works;
  1212. }
  1213. insert_work(pwq, work, worklist, work_flags);
  1214. spin_unlock(&pwq->pool->lock);
  1215. }
  1216. /**
  1217. * queue_work_on - queue work on specific cpu
  1218. * @cpu: CPU number to execute work on
  1219. * @wq: workqueue to use
  1220. * @work: work to queue
  1221. *
  1222. * Returns %false if @work was already on a queue, %true otherwise.
  1223. *
  1224. * We queue the work to a specific CPU, the caller must ensure it
  1225. * can't go away.
  1226. */
  1227. bool queue_work_on(int cpu, struct workqueue_struct *wq,
  1228. struct work_struct *work)
  1229. {
  1230. bool ret = false;
  1231. unsigned long flags;
  1232. local_irq_save(flags);
  1233. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  1234. __queue_work(cpu, wq, work);
  1235. ret = true;
  1236. }
  1237. local_irq_restore(flags);
  1238. return ret;
  1239. }
  1240. EXPORT_SYMBOL_GPL(queue_work_on);
  1241. void delayed_work_timer_fn(unsigned long __data)
  1242. {
  1243. struct delayed_work *dwork = (struct delayed_work *)__data;
  1244. /* should have been called from irqsafe timer with irq already off */
  1245. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  1246. }
  1247. EXPORT_SYMBOL(delayed_work_timer_fn);
  1248. static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
  1249. struct delayed_work *dwork, unsigned long delay)
  1250. {
  1251. struct timer_list *timer = &dwork->timer;
  1252. struct work_struct *work = &dwork->work;
  1253. WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
  1254. timer->data != (unsigned long)dwork);
  1255. WARN_ON_ONCE(timer_pending(timer));
  1256. WARN_ON_ONCE(!list_empty(&work->entry));
  1257. /*
  1258. * If @delay is 0, queue @dwork->work immediately. This is for
  1259. * both optimization and correctness. The earliest @timer can
  1260. * expire is on the closest next tick and delayed_work users depend
  1261. * on that there's no such delay when @delay is 0.
  1262. */
  1263. if (!delay) {
  1264. __queue_work(cpu, wq, &dwork->work);
  1265. return;
  1266. }
  1267. timer_stats_timer_set_start_info(&dwork->timer);
  1268. dwork->wq = wq;
  1269. dwork->cpu = cpu;
  1270. timer->expires = jiffies + delay;
  1271. if (unlikely(cpu != WORK_CPU_UNBOUND))
  1272. add_timer_on(timer, cpu);
  1273. else
  1274. add_timer(timer);
  1275. }
  1276. /**
  1277. * queue_delayed_work_on - queue work on specific CPU after delay
  1278. * @cpu: CPU number to execute work on
  1279. * @wq: workqueue to use
  1280. * @dwork: work to queue
  1281. * @delay: number of jiffies to wait before queueing
  1282. *
  1283. * Returns %false if @work was already on a queue, %true otherwise. If
  1284. * @delay is zero and @dwork is idle, it will be scheduled for immediate
  1285. * execution.
  1286. */
  1287. bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  1288. struct delayed_work *dwork, unsigned long delay)
  1289. {
  1290. struct work_struct *work = &dwork->work;
  1291. bool ret = false;
  1292. unsigned long flags;
  1293. /* read the comment in __queue_work() */
  1294. local_irq_save(flags);
  1295. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  1296. __queue_delayed_work(cpu, wq, dwork, delay);
  1297. ret = true;
  1298. }
  1299. local_irq_restore(flags);
  1300. return ret;
  1301. }
  1302. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  1303. /**
  1304. * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
  1305. * @cpu: CPU number to execute work on
  1306. * @wq: workqueue to use
  1307. * @dwork: work to queue
  1308. * @delay: number of jiffies to wait before queueing
  1309. *
  1310. * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
  1311. * modify @dwork's timer so that it expires after @delay. If @delay is
  1312. * zero, @work is guaranteed to be scheduled immediately regardless of its
  1313. * current state.
  1314. *
  1315. * Returns %false if @dwork was idle and queued, %true if @dwork was
  1316. * pending and its timer was modified.
  1317. *
  1318. * This function is safe to call from any context including IRQ handler.
  1319. * See try_to_grab_pending() for details.
  1320. */
  1321. bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
  1322. struct delayed_work *dwork, unsigned long delay)
  1323. {
  1324. unsigned long flags;
  1325. int ret;
  1326. do {
  1327. ret = try_to_grab_pending(&dwork->work, true, &flags);
  1328. } while (unlikely(ret == -EAGAIN));
  1329. if (likely(ret >= 0)) {
  1330. __queue_delayed_work(cpu, wq, dwork, delay);
  1331. local_irq_restore(flags);
  1332. }
  1333. /* -ENOENT from try_to_grab_pending() becomes %true */
  1334. return ret;
  1335. }
  1336. EXPORT_SYMBOL_GPL(mod_delayed_work_on);
  1337. /**
  1338. * worker_enter_idle - enter idle state
  1339. * @worker: worker which is entering idle state
  1340. *
  1341. * @worker is entering idle state. Update stats and idle timer if
  1342. * necessary.
  1343. *
  1344. * LOCKING:
  1345. * spin_lock_irq(pool->lock).
  1346. */
  1347. static void worker_enter_idle(struct worker *worker)
  1348. {
  1349. struct worker_pool *pool = worker->pool;
  1350. if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
  1351. WARN_ON_ONCE(!list_empty(&worker->entry) &&
  1352. (worker->hentry.next || worker->hentry.pprev)))
  1353. return;
  1354. /* can't use worker_set_flags(), also called from start_worker() */
  1355. worker->flags |= WORKER_IDLE;
  1356. pool->nr_idle++;
  1357. worker->last_active = jiffies;
  1358. /* idle_list is LIFO */
  1359. list_add(&worker->entry, &pool->idle_list);
  1360. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  1361. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  1362. /*
  1363. * Sanity check nr_running. Because wq_unbind_fn() releases
  1364. * pool->lock between setting %WORKER_UNBOUND and zapping
  1365. * nr_running, the warning may trigger spuriously. Check iff
  1366. * unbind is not in progress.
  1367. */
  1368. WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
  1369. pool->nr_workers == pool->nr_idle &&
  1370. atomic_read(&pool->nr_running));
  1371. }
  1372. /**
  1373. * worker_leave_idle - leave idle state
  1374. * @worker: worker which is leaving idle state
  1375. *
  1376. * @worker is leaving idle state. Update stats.
  1377. *
  1378. * LOCKING:
  1379. * spin_lock_irq(pool->lock).
  1380. */
  1381. static void worker_leave_idle(struct worker *worker)
  1382. {
  1383. struct worker_pool *pool = worker->pool;
  1384. if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
  1385. return;
  1386. worker_clr_flags(worker, WORKER_IDLE);
  1387. pool->nr_idle--;
  1388. list_del_init(&worker->entry);
  1389. }
  1390. /**
  1391. * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
  1392. * @pool: target worker_pool
  1393. *
  1394. * Bind %current to the cpu of @pool if it is associated and lock @pool.
  1395. *
  1396. * Works which are scheduled while the cpu is online must at least be
  1397. * scheduled to a worker which is bound to the cpu so that if they are
  1398. * flushed from cpu callbacks while cpu is going down, they are
  1399. * guaranteed to execute on the cpu.
  1400. *
  1401. * This function is to be used by unbound workers and rescuers to bind
  1402. * themselves to the target cpu and may race with cpu going down or
  1403. * coming online. kthread_bind() can't be used because it may put the
  1404. * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
  1405. * verbatim as it's best effort and blocking and pool may be
  1406. * [dis]associated in the meantime.
  1407. *
  1408. * This function tries set_cpus_allowed() and locks pool and verifies the
  1409. * binding against %POOL_DISASSOCIATED which is set during
  1410. * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
  1411. * enters idle state or fetches works without dropping lock, it can
  1412. * guarantee the scheduling requirement described in the first paragraph.
  1413. *
  1414. * CONTEXT:
  1415. * Might sleep. Called without any lock but returns with pool->lock
  1416. * held.
  1417. *
  1418. * RETURNS:
  1419. * %true if the associated pool is online (@worker is successfully
  1420. * bound), %false if offline.
  1421. */
  1422. static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
  1423. __acquires(&pool->lock)
  1424. {
  1425. while (true) {
  1426. /*
  1427. * The following call may fail, succeed or succeed
  1428. * without actually migrating the task to the cpu if
  1429. * it races with cpu hotunplug operation. Verify
  1430. * against POOL_DISASSOCIATED.
  1431. */
  1432. if (!(pool->flags & POOL_DISASSOCIATED))
  1433. set_cpus_allowed_ptr(current, pool->attrs->cpumask);
  1434. spin_lock_irq(&pool->lock);
  1435. if (pool->flags & POOL_DISASSOCIATED)
  1436. return false;
  1437. if (task_cpu(current) == pool->cpu &&
  1438. cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
  1439. return true;
  1440. spin_unlock_irq(&pool->lock);
  1441. /*
  1442. * We've raced with CPU hot[un]plug. Give it a breather
  1443. * and retry migration. cond_resched() is required here;
  1444. * otherwise, we might deadlock against cpu_stop trying to
  1445. * bring down the CPU on non-preemptive kernel.
  1446. */
  1447. cpu_relax();
  1448. cond_resched();
  1449. }
  1450. }
  1451. static struct worker *alloc_worker(void)
  1452. {
  1453. struct worker *worker;
  1454. worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  1455. if (worker) {
  1456. INIT_LIST_HEAD(&worker->entry);
  1457. INIT_LIST_HEAD(&worker->scheduled);
  1458. /* on creation a worker is in !idle && prep state */
  1459. worker->flags = WORKER_PREP;
  1460. }
  1461. return worker;
  1462. }
  1463. /**
  1464. * create_worker - create a new workqueue worker
  1465. * @pool: pool the new worker will belong to
  1466. *
  1467. * Create a new worker which is bound to @pool. The returned worker
  1468. * can be started by calling start_worker() or destroyed using
  1469. * destroy_worker().
  1470. *
  1471. * CONTEXT:
  1472. * Might sleep. Does GFP_KERNEL allocations.
  1473. *
  1474. * RETURNS:
  1475. * Pointer to the newly created worker.
  1476. */
  1477. static struct worker *create_worker(struct worker_pool *pool)
  1478. {
  1479. struct worker *worker = NULL;
  1480. int id = -1;
  1481. char id_buf[16];
  1482. lockdep_assert_held(&pool->manager_mutex);
  1483. /*
  1484. * ID is needed to determine kthread name. Allocate ID first
  1485. * without installing the pointer.
  1486. */
  1487. idr_preload(GFP_KERNEL);
  1488. spin_lock_irq(&pool->lock);
  1489. id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
  1490. spin_unlock_irq(&pool->lock);
  1491. idr_preload_end();
  1492. if (id < 0)
  1493. goto fail;
  1494. worker = alloc_worker();
  1495. if (!worker)
  1496. goto fail;
  1497. worker->pool = pool;
  1498. worker->id = id;
  1499. if (pool->cpu >= 0)
  1500. snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
  1501. pool->attrs->nice < 0 ? "H" : "");
  1502. else
  1503. snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
  1504. worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
  1505. "kworker/%s", id_buf);
  1506. if (IS_ERR(worker->task))
  1507. goto fail;
  1508. /*
  1509. * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
  1510. * online CPUs. It'll be re-applied when any of the CPUs come up.
  1511. */
  1512. set_user_nice(worker->task, pool->attrs->nice);
  1513. set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
  1514. /* prevent userland from meddling with cpumask of workqueue workers */
  1515. worker->task->flags |= PF_NO_SETAFFINITY;
  1516. /*
  1517. * The caller is responsible for ensuring %POOL_DISASSOCIATED
  1518. * remains stable across this function. See the comments above the
  1519. * flag definition for details.
  1520. */
  1521. if (pool->flags & POOL_DISASSOCIATED)
  1522. worker->flags |= WORKER_UNBOUND;
  1523. /* successful, commit the pointer to idr */
  1524. spin_lock_irq(&pool->lock);
  1525. idr_replace(&pool->worker_idr, worker, worker->id);
  1526. spin_unlock_irq(&pool->lock);
  1527. return worker;
  1528. fail:
  1529. if (id >= 0) {
  1530. spin_lock_irq(&pool->lock);
  1531. idr_remove(&pool->worker_idr, id);
  1532. spin_unlock_irq(&pool->lock);
  1533. }
  1534. kfree(worker);
  1535. return NULL;
  1536. }
  1537. /**
  1538. * start_worker - start a newly created worker
  1539. * @worker: worker to start
  1540. *
  1541. * Make the pool aware of @worker and start it.
  1542. *
  1543. * CONTEXT:
  1544. * spin_lock_irq(pool->lock).
  1545. */
  1546. static void start_worker(struct worker *worker)
  1547. {
  1548. worker->flags |= WORKER_STARTED;
  1549. worker->pool->nr_workers++;
  1550. worker_enter_idle(worker);
  1551. wake_up_process(worker->task);
  1552. }
  1553. /**
  1554. * create_and_start_worker - create and start a worker for a pool
  1555. * @pool: the target pool
  1556. *
  1557. * Grab the managership of @pool and create and start a new worker for it.
  1558. */
  1559. static int create_and_start_worker(struct worker_pool *pool)
  1560. {
  1561. struct worker *worker;
  1562. mutex_lock(&pool->manager_mutex);
  1563. worker = create_worker(pool);
  1564. if (worker) {
  1565. spin_lock_irq(&pool->lock);
  1566. start_worker(worker);
  1567. spin_unlock_irq(&pool->lock);
  1568. }
  1569. mutex_unlock(&pool->manager_mutex);
  1570. return worker ? 0 : -ENOMEM;
  1571. }
  1572. /**
  1573. * destroy_worker - destroy a workqueue worker
  1574. * @worker: worker to be destroyed
  1575. *
  1576. * Destroy @worker and adjust @pool stats accordingly.
  1577. *
  1578. * CONTEXT:
  1579. * spin_lock_irq(pool->lock) which is released and regrabbed.
  1580. */
  1581. static void destroy_worker(struct worker *worker)
  1582. {
  1583. struct worker_pool *pool = worker->pool;
  1584. lockdep_assert_held(&pool->manager_mutex);
  1585. lockdep_assert_held(&pool->lock);
  1586. /* sanity check frenzy */
  1587. if (WARN_ON(worker->current_work) ||
  1588. WARN_ON(!list_empty(&worker->scheduled)))
  1589. return;
  1590. if (worker->flags & WORKER_STARTED)
  1591. pool->nr_workers--;
  1592. if (worker->flags & WORKER_IDLE)
  1593. pool->nr_idle--;
  1594. list_del_init(&worker->entry);
  1595. worker->flags |= WORKER_DIE;
  1596. idr_remove(&pool->worker_idr, worker->id);
  1597. spin_unlock_irq(&pool->lock);
  1598. kthread_stop(worker->task);
  1599. kfree(worker);
  1600. spin_lock_irq(&pool->lock);
  1601. }
  1602. static void idle_worker_timeout(unsigned long __pool)
  1603. {
  1604. struct worker_pool *pool = (void *)__pool;
  1605. spin_lock_irq(&pool->lock);
  1606. if (too_many_workers(pool)) {
  1607. struct worker *worker;
  1608. unsigned long expires;
  1609. /* idle_list is kept in LIFO order, check the last one */
  1610. worker = list_entry(pool->idle_list.prev, struct worker, entry);
  1611. expires = worker->last_active + IDLE_WORKER_TIMEOUT;
  1612. if (time_before(jiffies, expires))
  1613. mod_timer(&pool->idle_timer, expires);
  1614. else {
  1615. /* it's been idle for too long, wake up manager */
  1616. pool->flags |= POOL_MANAGE_WORKERS;
  1617. wake_up_worker(pool);
  1618. }
  1619. }
  1620. spin_unlock_irq(&pool->lock);
  1621. }
  1622. static void send_mayday(struct work_struct *work)
  1623. {
  1624. struct pool_workqueue *pwq = get_work_pwq(work);
  1625. struct workqueue_struct *wq = pwq->wq;
  1626. lockdep_assert_held(&wq_mayday_lock);
  1627. if (!wq->rescuer)
  1628. return;
  1629. /* mayday mayday mayday */
  1630. if (list_empty(&pwq->mayday_node)) {
  1631. list_add_tail(&pwq->mayday_node, &wq->maydays);
  1632. wake_up_process(wq->rescuer->task);
  1633. }
  1634. }
  1635. static void pool_mayday_timeout(unsigned long __pool)
  1636. {
  1637. struct worker_pool *pool = (void *)__pool;
  1638. struct work_struct *work;
  1639. spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
  1640. spin_lock(&pool->lock);
  1641. if (need_to_create_worker(pool)) {
  1642. /*
  1643. * We've been trying to create a new worker but
  1644. * haven't been successful. We might be hitting an
  1645. * allocation deadlock. Send distress signals to
  1646. * rescuers.
  1647. */
  1648. list_for_each_entry(work, &pool->worklist, entry)
  1649. send_mayday(work);
  1650. }
  1651. spin_unlock(&pool->lock);
  1652. spin_unlock_irq(&wq_mayday_lock);
  1653. mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
  1654. }
  1655. /**
  1656. * maybe_create_worker - create a new worker if necessary
  1657. * @pool: pool to create a new worker for
  1658. *
  1659. * Create a new worker for @pool if necessary. @pool is guaranteed to
  1660. * have at least one idle worker on return from this function. If
  1661. * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
  1662. * sent to all rescuers with works scheduled on @pool to resolve
  1663. * possible allocation deadlock.
  1664. *
  1665. * On return, need_to_create_worker() is guaranteed to be %false and
  1666. * may_start_working() %true.
  1667. *
  1668. * LOCKING:
  1669. * spin_lock_irq(pool->lock) which may be released and regrabbed
  1670. * multiple times. Does GFP_KERNEL allocations. Called only from
  1671. * manager.
  1672. *
  1673. * RETURNS:
  1674. * %false if no action was taken and pool->lock stayed locked, %true
  1675. * otherwise.
  1676. */
  1677. static bool maybe_create_worker(struct worker_pool *pool)
  1678. __releases(&pool->lock)
  1679. __acquires(&pool->lock)
  1680. {
  1681. if (!need_to_create_worker(pool))
  1682. return false;
  1683. restart:
  1684. spin_unlock_irq(&pool->lock);
  1685. /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
  1686. mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
  1687. while (true) {
  1688. struct worker *worker;
  1689. worker = create_worker(pool);
  1690. if (worker) {
  1691. del_timer_sync(&pool->mayday_timer);
  1692. spin_lock_irq(&pool->lock);
  1693. start_worker(worker);
  1694. if (WARN_ON_ONCE(need_to_create_worker(pool)))
  1695. goto restart;
  1696. return true;
  1697. }
  1698. if (!need_to_create_worker(pool))
  1699. break;
  1700. __set_current_state(TASK_INTERRUPTIBLE);
  1701. schedule_timeout(CREATE_COOLDOWN);
  1702. if (!need_to_create_worker(pool))
  1703. break;
  1704. }
  1705. del_timer_sync(&pool->mayday_timer);
  1706. spin_lock_irq(&pool->lock);
  1707. if (need_to_create_worker(pool))
  1708. goto restart;
  1709. return true;
  1710. }
  1711. /**
  1712. * maybe_destroy_worker - destroy workers which have been idle for a while
  1713. * @pool: pool to destroy workers for
  1714. *
  1715. * Destroy @pool workers which have been idle for longer than
  1716. * IDLE_WORKER_TIMEOUT.
  1717. *
  1718. * LOCKING:
  1719. * spin_lock_irq(pool->lock) which may be released and regrabbed
  1720. * multiple times. Called only from manager.
  1721. *
  1722. * RETURNS:
  1723. * %false if no action was taken and pool->lock stayed locked, %true
  1724. * otherwise.
  1725. */
  1726. static bool maybe_destroy_workers(struct worker_pool *pool)
  1727. {
  1728. bool ret = false;
  1729. while (too_many_workers(pool)) {
  1730. struct worker *worker;
  1731. unsigned long expires;
  1732. worker = list_entry(pool->idle_list.prev, struct worker, entry);
  1733. expires = worker->last_active + IDLE_WORKER_TIMEOUT;
  1734. if (time_before(jiffies, expires)) {
  1735. mod_timer(&pool->idle_timer, expires);
  1736. break;
  1737. }
  1738. destroy_worker(worker);
  1739. ret = true;
  1740. }
  1741. return ret;
  1742. }
  1743. /**
  1744. * manage_workers - manage worker pool
  1745. * @worker: self
  1746. *
  1747. * Assume the manager role and manage the worker pool @worker belongs
  1748. * to. At any given time, there can be only zero or one manager per
  1749. * pool. The exclusion is handled automatically by this function.
  1750. *
  1751. * The caller can safely start processing works on false return. On
  1752. * true return, it's guaranteed that need_to_create_worker() is false
  1753. * and may_start_working() is true.
  1754. *
  1755. * CONTEXT:
  1756. * spin_lock_irq(pool->lock) which may be released and regrabbed
  1757. * multiple times. Does GFP_KERNEL allocations.
  1758. *
  1759. * RETURNS:
  1760. * spin_lock_irq(pool->lock) which may be released and regrabbed
  1761. * multiple times. Does GFP_KERNEL allocations.
  1762. */
  1763. static bool manage_workers(struct worker *worker)
  1764. {
  1765. struct worker_pool *pool = worker->pool;
  1766. bool ret = false;
  1767. /*
  1768. * Managership is governed by two mutexes - manager_arb and
  1769. * manager_mutex. manager_arb handles arbitration of manager role.
  1770. * Anyone who successfully grabs manager_arb wins the arbitration
  1771. * and becomes the manager. mutex_trylock() on pool->manager_arb
  1772. * failure while holding pool->lock reliably indicates that someone
  1773. * else is managing the pool and the worker which failed trylock
  1774. * can proceed to executing work items. This means that anyone
  1775. * grabbing manager_arb is responsible for actually performing
  1776. * manager duties. If manager_arb is grabbed and released without
  1777. * actual management, the pool may stall indefinitely.
  1778. *
  1779. * manager_mutex is used for exclusion of actual management
  1780. * operations. The holder of manager_mutex can be sure that none
  1781. * of management operations, including creation and destruction of
  1782. * workers, won't take place until the mutex is released. Because
  1783. * manager_mutex doesn't interfere with manager role arbitration,
  1784. * it is guaranteed that the pool's management, while may be
  1785. * delayed, won't be disturbed by someone else grabbing
  1786. * manager_mutex.
  1787. */
  1788. if (!mutex_trylock(&pool->manager_arb))
  1789. return ret;
  1790. /*
  1791. * With manager arbitration won, manager_mutex would be free in
  1792. * most cases. trylock first without dropping @pool->lock.
  1793. */
  1794. if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
  1795. spin_unlock_irq(&pool->lock);
  1796. mutex_lock(&pool->manager_mutex);
  1797. ret = true;
  1798. }
  1799. pool->flags &= ~POOL_MANAGE_WORKERS;
  1800. /*
  1801. * Destroy and then create so that may_start_working() is true
  1802. * on return.
  1803. */
  1804. ret |= maybe_destroy_workers(pool);
  1805. ret |= maybe_create_worker(pool);
  1806. mutex_unlock(&pool->manager_mutex);
  1807. mutex_unlock(&pool->manager_arb);
  1808. return ret;
  1809. }
  1810. /**
  1811. * process_one_work - process single work
  1812. * @worker: self
  1813. * @work: work to process
  1814. *
  1815. * Process @work. This function contains all the logics necessary to
  1816. * process a single work including synchronization against and
  1817. * interaction with other workers on the same cpu, queueing and
  1818. * flushing. As long as context requirement is met, any worker can
  1819. * call this function to process a work.
  1820. *
  1821. * CONTEXT:
  1822. * spin_lock_irq(pool->lock) which is released and regrabbed.
  1823. */
  1824. static void process_one_work(struct worker *worker, struct work_struct *work)
  1825. __releases(&pool->lock)
  1826. __acquires(&pool->lock)
  1827. {
  1828. struct pool_workqueue *pwq = get_work_pwq(work);
  1829. struct worker_pool *pool = worker->pool;
  1830. bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
  1831. int work_color;
  1832. struct worker *collision;
  1833. #ifdef CONFIG_LOCKDEP
  1834. /*
  1835. * It is permissible to free the struct work_struct from
  1836. * inside the function that is called from it, this we need to
  1837. * take into account for lockdep too. To avoid bogus "held
  1838. * lock freed" warnings as well as problems when looking into
  1839. * work->lockdep_map, make a copy and use that here.
  1840. */
  1841. struct lockdep_map lockdep_map;
  1842. lockdep_copy_map(&lockdep_map, &work->lockdep_map);
  1843. #endif
  1844. /*
  1845. * Ensure we're on the correct CPU. DISASSOCIATED test is
  1846. * necessary to avoid spurious warnings from rescuers servicing the
  1847. * unbound or a disassociated pool.
  1848. */
  1849. WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
  1850. !(pool->flags & POOL_DISASSOCIATED) &&
  1851. raw_smp_processor_id() != pool->cpu);
  1852. /*
  1853. * A single work shouldn't be executed concurrently by
  1854. * multiple workers on a single cpu. Check whether anyone is
  1855. * already processing the work. If so, defer the work to the
  1856. * currently executing one.
  1857. */
  1858. collision = find_worker_executing_work(pool, work);
  1859. if (unlikely(collision)) {
  1860. move_linked_works(work, &collision->scheduled, NULL);
  1861. return;
  1862. }
  1863. /* claim and dequeue */
  1864. debug_work_deactivate(work);
  1865. hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
  1866. worker->current_work = work;
  1867. worker->current_func = work->func;
  1868. worker->current_pwq = pwq;
  1869. work_color = get_work_color(work);
  1870. list_del_init(&work->entry);
  1871. /*
  1872. * CPU intensive works don't participate in concurrency
  1873. * management. They're the scheduler's responsibility.
  1874. */
  1875. if (unlikely(cpu_intensive))
  1876. worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
  1877. /*
  1878. * Unbound pool isn't concurrency managed and work items should be
  1879. * executed ASAP. Wake up another worker if necessary.
  1880. */
  1881. if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
  1882. wake_up_worker(pool);
  1883. /*
  1884. * Record the last pool and clear PENDING which should be the last
  1885. * update to @work. Also, do this inside @pool->lock so that
  1886. * PENDING and queued state changes happen together while IRQ is
  1887. * disabled.
  1888. */
  1889. set_work_pool_and_clear_pending(work, pool->id);
  1890. spin_unlock_irq(&pool->lock);
  1891. lock_map_acquire_read(&pwq->wq->lockdep_map);
  1892. lock_map_acquire(&lockdep_map);
  1893. trace_workqueue_execute_start(work);
  1894. worker->current_func(work);
  1895. /*
  1896. * While we must be careful to not use "work" after this, the trace
  1897. * point will only record its address.
  1898. */
  1899. trace_workqueue_execute_end(work);
  1900. lock_map_release(&lockdep_map);
  1901. lock_map_release(&pwq->wq->lockdep_map);
  1902. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  1903. pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
  1904. " last function: %pf\n",
  1905. current->comm, preempt_count(), task_pid_nr(current),
  1906. worker->current_func);
  1907. debug_show_held_locks(current);
  1908. dump_stack();
  1909. }
  1910. spin_lock_irq(&pool->lock);
  1911. /* clear cpu intensive status */
  1912. if (unlikely(cpu_intensive))
  1913. worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
  1914. /* we're done with it, release */
  1915. hash_del(&worker->hentry);
  1916. worker->current_work = NULL;
  1917. worker->current_func = NULL;
  1918. worker->current_pwq = NULL;
  1919. pwq_dec_nr_in_flight(pwq, work_color);
  1920. }
  1921. /**
  1922. * process_scheduled_works - process scheduled works
  1923. * @worker: self
  1924. *
  1925. * Process all scheduled works. Please note that the scheduled list
  1926. * may change while processing a work, so this function repeatedly
  1927. * fetches a work from the top and executes it.
  1928. *
  1929. * CONTEXT:
  1930. * spin_lock_irq(pool->lock) which may be released and regrabbed
  1931. * multiple times.
  1932. */
  1933. static void process_scheduled_works(struct worker *worker)
  1934. {
  1935. while (!list_empty(&worker->scheduled)) {
  1936. struct work_struct *work = list_first_entry(&worker->scheduled,
  1937. struct work_struct, entry);
  1938. process_one_work(worker, work);
  1939. }
  1940. }
  1941. /**
  1942. * worker_thread - the worker thread function
  1943. * @__worker: self
  1944. *
  1945. * The worker thread function. All workers belong to a worker_pool -
  1946. * either a per-cpu one or dynamic unbound one. These workers process all
  1947. * work items regardless of their specific target workqueue. The only
  1948. * exception is work items which belong to workqueues with a rescuer which
  1949. * will be explained in rescuer_thread().
  1950. */
  1951. static int worker_thread(void *__worker)
  1952. {
  1953. struct worker *worker = __worker;
  1954. struct worker_pool *pool = worker->pool;
  1955. /* tell the scheduler that this is a workqueue worker */
  1956. worker->task->flags |= PF_WQ_WORKER;
  1957. woke_up:
  1958. spin_lock_irq(&pool->lock);
  1959. /* am I supposed to die? */
  1960. if (unlikely(worker->flags & WORKER_DIE)) {
  1961. spin_unlock_irq(&pool->lock);
  1962. WARN_ON_ONCE(!list_empty(&worker->entry));
  1963. worker->task->flags &= ~PF_WQ_WORKER;
  1964. return 0;
  1965. }
  1966. worker_leave_idle(worker);
  1967. recheck:
  1968. /* no more worker necessary? */
  1969. if (!need_more_worker(pool))
  1970. goto sleep;
  1971. /* do we need to manage? */
  1972. if (unlikely(!may_start_working(pool)) && manage_workers(worker))
  1973. goto recheck;
  1974. /*
  1975. * ->scheduled list can only be filled while a worker is
  1976. * preparing to process a work or actually processing it.
  1977. * Make sure nobody diddled with it while I was sleeping.
  1978. */
  1979. WARN_ON_ONCE(!list_empty(&worker->scheduled));
  1980. /*
  1981. * Finish PREP stage. We're guaranteed to have at least one idle
  1982. * worker or that someone else has already assumed the manager
  1983. * role. This is where @worker starts participating in concurrency
  1984. * management if applicable and concurrency management is restored
  1985. * after being rebound. See rebind_workers() for details.
  1986. */
  1987. worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
  1988. do {
  1989. struct work_struct *work =
  1990. list_first_entry(&pool->worklist,
  1991. struct work_struct, entry);
  1992. if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
  1993. /* optimization path, not strictly necessary */
  1994. process_one_work(worker, work);
  1995. if (unlikely(!list_empty(&worker->scheduled)))
  1996. process_scheduled_works(worker);
  1997. } else {
  1998. move_linked_works(work, &worker->scheduled, NULL);
  1999. process_scheduled_works(worker);
  2000. }
  2001. } while (keep_working(pool));
  2002. worker_set_flags(worker, WORKER_PREP, false);
  2003. sleep:
  2004. if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
  2005. goto recheck;
  2006. /*
  2007. * pool->lock is held and there's no work to process and no need to
  2008. * manage, sleep. Workers are woken up only while holding
  2009. * pool->lock or from local cpu, so setting the current state
  2010. * before releasing pool->lock is enough to prevent losing any
  2011. * event.
  2012. */
  2013. worker_enter_idle(worker);
  2014. __set_current_state(TASK_INTERRUPTIBLE);
  2015. spin_unlock_irq(&pool->lock);
  2016. schedule();
  2017. goto woke_up;
  2018. }
  2019. /**
  2020. * rescuer_thread - the rescuer thread function
  2021. * @__rescuer: self
  2022. *
  2023. * Workqueue rescuer thread function. There's one rescuer for each
  2024. * workqueue which has WQ_MEM_RECLAIM set.
  2025. *
  2026. * Regular work processing on a pool may block trying to create a new
  2027. * worker which uses GFP_KERNEL allocation which has slight chance of
  2028. * developing into deadlock if some works currently on the same queue
  2029. * need to be processed to satisfy the GFP_KERNEL allocation. This is
  2030. * the problem rescuer solves.
  2031. *
  2032. * When such condition is possible, the pool summons rescuers of all
  2033. * workqueues which have works queued on the pool and let them process
  2034. * those works so that forward progress can be guaranteed.
  2035. *
  2036. * This should happen rarely.
  2037. */
  2038. static int rescuer_thread(void *__rescuer)
  2039. {
  2040. struct worker *rescuer = __rescuer;
  2041. struct workqueue_struct *wq = rescuer->rescue_wq;
  2042. struct list_head *scheduled = &rescuer->scheduled;
  2043. set_user_nice(current, RESCUER_NICE_LEVEL);
  2044. /*
  2045. * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
  2046. * doesn't participate in concurrency management.
  2047. */
  2048. rescuer->task->flags |= PF_WQ_WORKER;
  2049. repeat:
  2050. set_current_state(TASK_INTERRUPTIBLE);
  2051. if (kthread_should_stop()) {
  2052. __set_current_state(TASK_RUNNING);
  2053. rescuer->task->flags &= ~PF_WQ_WORKER;
  2054. return 0;
  2055. }
  2056. /* see whether any pwq is asking for help */
  2057. spin_lock_irq(&wq_mayday_lock);
  2058. while (!list_empty(&wq->maydays)) {
  2059. struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
  2060. struct pool_workqueue, mayday_node);
  2061. struct worker_pool *pool = pwq->pool;
  2062. struct work_struct *work, *n;
  2063. __set_current_state(TASK_RUNNING);
  2064. list_del_init(&pwq->mayday_node);
  2065. spin_unlock_irq(&wq_mayday_lock);
  2066. /* migrate to the target cpu if possible */
  2067. worker_maybe_bind_and_lock(pool);
  2068. rescuer->pool = pool;
  2069. /*
  2070. * Slurp in all works issued via this workqueue and
  2071. * process'em.
  2072. */
  2073. WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
  2074. list_for_each_entry_safe(work, n, &pool->worklist, entry)
  2075. if (get_work_pwq(work) == pwq)
  2076. move_linked_works(work, scheduled, &n);
  2077. process_scheduled_works(rescuer);
  2078. /*
  2079. * Leave this pool. If keep_working() is %true, notify a
  2080. * regular worker; otherwise, we end up with 0 concurrency
  2081. * and stalling the execution.
  2082. */
  2083. if (keep_working(pool))
  2084. wake_up_worker(pool);
  2085. rescuer->pool = NULL;
  2086. spin_unlock(&pool->lock);
  2087. spin_lock(&wq_mayday_lock);
  2088. }
  2089. spin_unlock_irq(&wq_mayday_lock);
  2090. /* rescuers should never participate in concurrency management */
  2091. WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
  2092. schedule();
  2093. goto repeat;
  2094. }
  2095. struct wq_barrier {
  2096. struct work_struct work;
  2097. struct completion done;
  2098. };
  2099. static void wq_barrier_func(struct work_struct *work)
  2100. {
  2101. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  2102. complete(&barr->done);
  2103. }
  2104. /**
  2105. * insert_wq_barrier - insert a barrier work
  2106. * @pwq: pwq to insert barrier into
  2107. * @barr: wq_barrier to insert
  2108. * @target: target work to attach @barr to
  2109. * @worker: worker currently executing @target, NULL if @target is not executing
  2110. *
  2111. * @barr is linked to @target such that @barr is completed only after
  2112. * @target finishes execution. Please note that the ordering
  2113. * guarantee is observed only with respect to @target and on the local
  2114. * cpu.
  2115. *
  2116. * Currently, a queued barrier can't be canceled. This is because
  2117. * try_to_grab_pending() can't determine whether the work to be
  2118. * grabbed is at the head of the queue and thus can't clear LINKED
  2119. * flag of the previous work while there must be a valid next work
  2120. * after a work with LINKED flag set.
  2121. *
  2122. * Note that when @worker is non-NULL, @target may be modified
  2123. * underneath us, so we can't reliably determine pwq from @target.
  2124. *
  2125. * CONTEXT:
  2126. * spin_lock_irq(pool->lock).
  2127. */
  2128. static void insert_wq_barrier(struct pool_workqueue *pwq,
  2129. struct wq_barrier *barr,
  2130. struct work_struct *target, struct worker *worker)
  2131. {
  2132. struct list_head *head;
  2133. unsigned int linked = 0;
  2134. /*
  2135. * debugobject calls are safe here even with pool->lock locked
  2136. * as we know for sure that this will not trigger any of the
  2137. * checks and call back into the fixup functions where we
  2138. * might deadlock.
  2139. */
  2140. INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
  2141. __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
  2142. init_completion(&barr->done);
  2143. /*
  2144. * If @target is currently being executed, schedule the
  2145. * barrier to the worker; otherwise, put it after @target.
  2146. */
  2147. if (worker)
  2148. head = worker->scheduled.next;
  2149. else {
  2150. unsigned long *bits = work_data_bits(target);
  2151. head = target->entry.next;
  2152. /* there can already be other linked works, inherit and set */
  2153. linked = *bits & WORK_STRUCT_LINKED;
  2154. __set_bit(WORK_STRUCT_LINKED_BIT, bits);
  2155. }
  2156. debug_work_activate(&barr->work);
  2157. insert_work(pwq, &barr->work, head,
  2158. work_color_to_flags(WORK_NO_COLOR) | linked);
  2159. }
  2160. /**
  2161. * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
  2162. * @wq: workqueue being flushed
  2163. * @flush_color: new flush color, < 0 for no-op
  2164. * @work_color: new work color, < 0 for no-op
  2165. *
  2166. * Prepare pwqs for workqueue flushing.
  2167. *
  2168. * If @flush_color is non-negative, flush_color on all pwqs should be
  2169. * -1. If no pwq has in-flight commands at the specified color, all
  2170. * pwq->flush_color's stay at -1 and %false is returned. If any pwq
  2171. * has in flight commands, its pwq->flush_color is set to
  2172. * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
  2173. * wakeup logic is armed and %true is returned.
  2174. *
  2175. * The caller should have initialized @wq->first_flusher prior to
  2176. * calling this function with non-negative @flush_color. If
  2177. * @flush_color is negative, no flush color update is done and %false
  2178. * is returned.
  2179. *
  2180. * If @work_color is non-negative, all pwqs should have the same
  2181. * work_color which is previous to @work_color and all will be
  2182. * advanced to @work_color.
  2183. *
  2184. * CONTEXT:
  2185. * mutex_lock(wq->mutex).
  2186. *
  2187. * RETURNS:
  2188. * %true if @flush_color >= 0 and there's something to flush. %false
  2189. * otherwise.
  2190. */
  2191. static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
  2192. int flush_color, int work_color)
  2193. {
  2194. bool wait = false;
  2195. struct pool_workqueue *pwq;
  2196. if (flush_color >= 0) {
  2197. WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
  2198. atomic_set(&wq->nr_pwqs_to_flush, 1);
  2199. }
  2200. for_each_pwq(pwq, wq) {
  2201. struct worker_pool *pool = pwq->pool;
  2202. spin_lock_irq(&pool->lock);
  2203. if (flush_color >= 0) {
  2204. WARN_ON_ONCE(pwq->flush_color != -1);
  2205. if (pwq->nr_in_flight[flush_color]) {
  2206. pwq->flush_color = flush_color;
  2207. atomic_inc(&wq->nr_pwqs_to_flush);
  2208. wait = true;
  2209. }
  2210. }
  2211. if (work_color >= 0) {
  2212. WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
  2213. pwq->work_color = work_color;
  2214. }
  2215. spin_unlock_irq(&pool->lock);
  2216. }
  2217. if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
  2218. complete(&wq->first_flusher->done);
  2219. return wait;
  2220. }
  2221. /**
  2222. * flush_workqueue - ensure that any scheduled work has run to completion.
  2223. * @wq: workqueue to flush
  2224. *
  2225. * This function sleeps until all work items which were queued on entry
  2226. * have finished execution, but it is not livelocked by new incoming ones.
  2227. */
  2228. void flush_workqueue(struct workqueue_struct *wq)
  2229. {
  2230. struct wq_flusher this_flusher = {
  2231. .list = LIST_HEAD_INIT(this_flusher.list),
  2232. .flush_color = -1,
  2233. .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
  2234. };
  2235. int next_color;
  2236. lock_map_acquire(&wq->lockdep_map);
  2237. lock_map_release(&wq->lockdep_map);
  2238. mutex_lock(&wq->mutex);
  2239. /*
  2240. * Start-to-wait phase
  2241. */
  2242. next_color = work_next_color(wq->work_color);
  2243. if (next_color != wq->flush_color) {
  2244. /*
  2245. * Color space is not full. The current work_color
  2246. * becomes our flush_color and work_color is advanced
  2247. * by one.
  2248. */
  2249. WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
  2250. this_flusher.flush_color = wq->work_color;
  2251. wq->work_color = next_color;
  2252. if (!wq->first_flusher) {
  2253. /* no flush in progress, become the first flusher */
  2254. WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
  2255. wq->first_flusher = &this_flusher;
  2256. if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
  2257. wq->work_color)) {
  2258. /* nothing to flush, done */
  2259. wq->flush_color = next_color;
  2260. wq->first_flusher = NULL;
  2261. goto out_unlock;
  2262. }
  2263. } else {
  2264. /* wait in queue */
  2265. WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
  2266. list_add_tail(&this_flusher.list, &wq->flusher_queue);
  2267. flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
  2268. }
  2269. } else {
  2270. /*
  2271. * Oops, color space is full, wait on overflow queue.
  2272. * The next flush completion will assign us
  2273. * flush_color and transfer to flusher_queue.
  2274. */
  2275. list_add_tail(&this_flusher.list, &wq->flusher_overflow);
  2276. }
  2277. mutex_unlock(&wq->mutex);
  2278. wait_for_completion(&this_flusher.done);
  2279. /*
  2280. * Wake-up-and-cascade phase
  2281. *
  2282. * First flushers are responsible for cascading flushes and
  2283. * handling overflow. Non-first flushers can simply return.
  2284. */
  2285. if (wq->first_flusher != &this_flusher)
  2286. return;
  2287. mutex_lock(&wq->mutex);
  2288. /* we might have raced, check again with mutex held */
  2289. if (wq->first_flusher != &this_flusher)
  2290. goto out_unlock;
  2291. wq->first_flusher = NULL;
  2292. WARN_ON_ONCE(!list_empty(&this_flusher.list));
  2293. WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
  2294. while (true) {
  2295. struct wq_flusher *next, *tmp;
  2296. /* complete all the flushers sharing the current flush color */
  2297. list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
  2298. if (next->flush_color != wq->flush_color)
  2299. break;
  2300. list_del_init(&next->list);
  2301. complete(&next->done);
  2302. }
  2303. WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
  2304. wq->flush_color != work_next_color(wq->work_color));
  2305. /* this flush_color is finished, advance by one */
  2306. wq->flush_color = work_next_color(wq->flush_color);
  2307. /* one color has been freed, handle overflow queue */
  2308. if (!list_empty(&wq->flusher_overflow)) {
  2309. /*
  2310. * Assign the same color to all overflowed
  2311. * flushers, advance work_color and append to
  2312. * flusher_queue. This is the start-to-wait
  2313. * phase for these overflowed flushers.
  2314. */
  2315. list_for_each_entry(tmp, &wq->flusher_overflow, list)
  2316. tmp->flush_color = wq->work_color;
  2317. wq->work_color = work_next_color(wq->work_color);
  2318. list_splice_tail_init(&wq->flusher_overflow,
  2319. &wq->flusher_queue);
  2320. flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
  2321. }
  2322. if (list_empty(&wq->flusher_queue)) {
  2323. WARN_ON_ONCE(wq->flush_color != wq->work_color);
  2324. break;
  2325. }
  2326. /*
  2327. * Need to flush more colors. Make the next flusher
  2328. * the new first flusher and arm pwqs.
  2329. */
  2330. WARN_ON_ONCE(wq->flush_color == wq->work_color);
  2331. WARN_ON_ONCE(wq->flush_color != next->flush_color);
  2332. list_del_init(&next->list);
  2333. wq->first_flusher = next;
  2334. if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
  2335. break;
  2336. /*
  2337. * Meh... this color is already done, clear first
  2338. * flusher and repeat cascading.
  2339. */
  2340. wq->first_flusher = NULL;
  2341. }
  2342. out_unlock:
  2343. mutex_unlock(&wq->mutex);
  2344. }
  2345. EXPORT_SYMBOL_GPL(flush_workqueue);
  2346. /**
  2347. * drain_workqueue - drain a workqueue
  2348. * @wq: workqueue to drain
  2349. *
  2350. * Wait until the workqueue becomes empty. While draining is in progress,
  2351. * only chain queueing is allowed. IOW, only currently pending or running
  2352. * work items on @wq can queue further work items on it. @wq is flushed
  2353. * repeatedly until it becomes empty. The number of flushing is detemined
  2354. * by the depth of chaining and should be relatively short. Whine if it
  2355. * takes too long.
  2356. */
  2357. void drain_workqueue(struct workqueue_struct *wq)
  2358. {
  2359. unsigned int flush_cnt = 0;
  2360. struct pool_workqueue *pwq;
  2361. /*
  2362. * __queue_work() needs to test whether there are drainers, is much
  2363. * hotter than drain_workqueue() and already looks at @wq->flags.
  2364. * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
  2365. */
  2366. mutex_lock(&wq->mutex);
  2367. if (!wq->nr_drainers++)
  2368. wq->flags |= __WQ_DRAINING;
  2369. mutex_unlock(&wq->mutex);
  2370. reflush:
  2371. flush_workqueue(wq);
  2372. mutex_lock(&wq->mutex);
  2373. for_each_pwq(pwq, wq) {
  2374. bool drained;
  2375. spin_lock_irq(&pwq->pool->lock);
  2376. drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
  2377. spin_unlock_irq(&pwq->pool->lock);
  2378. if (drained)
  2379. continue;
  2380. if (++flush_cnt == 10 ||
  2381. (flush_cnt % 100 == 0 && flush_cnt <= 1000))
  2382. pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
  2383. wq->name, flush_cnt);
  2384. mutex_unlock(&wq->mutex);
  2385. goto reflush;
  2386. }
  2387. if (!--wq->nr_drainers)
  2388. wq->flags &= ~__WQ_DRAINING;
  2389. mutex_unlock(&wq->mutex);
  2390. }
  2391. EXPORT_SYMBOL_GPL(drain_workqueue);
  2392. static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
  2393. {
  2394. struct worker *worker = NULL;
  2395. struct worker_pool *pool;
  2396. struct pool_workqueue *pwq;
  2397. might_sleep();
  2398. local_irq_disable();
  2399. pool = get_work_pool(work);
  2400. if (!pool) {
  2401. local_irq_enable();
  2402. return false;
  2403. }
  2404. spin_lock(&pool->lock);
  2405. /* see the comment in try_to_grab_pending() with the same code */
  2406. pwq = get_work_pwq(work);
  2407. if (pwq) {
  2408. if (unlikely(pwq->pool != pool))
  2409. goto already_gone;
  2410. } else {
  2411. worker = find_worker_executing_work(pool, work);
  2412. if (!worker)
  2413. goto already_gone;
  2414. pwq = worker->current_pwq;
  2415. }
  2416. insert_wq_barrier(pwq, barr, work, worker);
  2417. spin_unlock_irq(&pool->lock);
  2418. /*
  2419. * If @max_active is 1 or rescuer is in use, flushing another work
  2420. * item on the same workqueue may lead to deadlock. Make sure the
  2421. * flusher is not running on the same workqueue by verifying write
  2422. * access.
  2423. */
  2424. if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
  2425. lock_map_acquire(&pwq->wq->lockdep_map);
  2426. else
  2427. lock_map_acquire_read(&pwq->wq->lockdep_map);
  2428. lock_map_release(&pwq->wq->lockdep_map);
  2429. return true;
  2430. already_gone:
  2431. spin_unlock_irq(&pool->lock);
  2432. return false;
  2433. }
  2434. /**
  2435. * flush_work - wait for a work to finish executing the last queueing instance
  2436. * @work: the work to flush
  2437. *
  2438. * Wait until @work has finished execution. @work is guaranteed to be idle
  2439. * on return if it hasn't been requeued since flush started.
  2440. *
  2441. * RETURNS:
  2442. * %true if flush_work() waited for the work to finish execution,
  2443. * %false if it was already idle.
  2444. */
  2445. bool flush_work(struct work_struct *work)
  2446. {
  2447. struct wq_barrier barr;
  2448. lock_map_acquire(&work->lockdep_map);
  2449. lock_map_release(&work->lockdep_map);
  2450. if (start_flush_work(work, &barr)) {
  2451. wait_for_completion(&barr.done);
  2452. destroy_work_on_stack(&barr.work);
  2453. return true;
  2454. } else {
  2455. return false;
  2456. }
  2457. }
  2458. EXPORT_SYMBOL_GPL(flush_work);
  2459. static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
  2460. {
  2461. unsigned long flags;
  2462. int ret;
  2463. do {
  2464. ret = try_to_grab_pending(work, is_dwork, &flags);
  2465. /*
  2466. * If someone else is canceling, wait for the same event it
  2467. * would be waiting for before retrying.
  2468. */
  2469. if (unlikely(ret == -ENOENT))
  2470. flush_work(work);
  2471. } while (unlikely(ret < 0));
  2472. /* tell other tasks trying to grab @work to back off */
  2473. mark_work_canceling(work);
  2474. local_irq_restore(flags);
  2475. flush_work(work);
  2476. clear_work_data(work);
  2477. return ret;
  2478. }
  2479. /**
  2480. * cancel_work_sync - cancel a work and wait for it to finish
  2481. * @work: the work to cancel
  2482. *
  2483. * Cancel @work and wait for its execution to finish. This function
  2484. * can be used even if the work re-queues itself or migrates to
  2485. * another workqueue. On return from this function, @work is
  2486. * guaranteed to be not pending or executing on any CPU.
  2487. *
  2488. * cancel_work_sync(&delayed_work->work) must not be used for
  2489. * delayed_work's. Use cancel_delayed_work_sync() instead.
  2490. *
  2491. * The caller must ensure that the workqueue on which @work was last
  2492. * queued can't be destroyed before this function returns.
  2493. *
  2494. * RETURNS:
  2495. * %true if @work was pending, %false otherwise.
  2496. */
  2497. bool cancel_work_sync(struct work_struct *work)
  2498. {
  2499. return __cancel_work_timer(work, false);
  2500. }
  2501. EXPORT_SYMBOL_GPL(cancel_work_sync);
  2502. /**
  2503. * flush_delayed_work - wait for a dwork to finish executing the last queueing
  2504. * @dwork: the delayed work to flush
  2505. *
  2506. * Delayed timer is cancelled and the pending work is queued for
  2507. * immediate execution. Like flush_work(), this function only
  2508. * considers the last queueing instance of @dwork.
  2509. *
  2510. * RETURNS:
  2511. * %true if flush_work() waited for the work to finish execution,
  2512. * %false if it was already idle.
  2513. */
  2514. bool flush_delayed_work(struct delayed_work *dwork)
  2515. {
  2516. local_irq_disable();
  2517. if (del_timer_sync(&dwork->timer))
  2518. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  2519. local_irq_enable();
  2520. return flush_work(&dwork->work);
  2521. }
  2522. EXPORT_SYMBOL(flush_delayed_work);
  2523. /**
  2524. * cancel_delayed_work - cancel a delayed work
  2525. * @dwork: delayed_work to cancel
  2526. *
  2527. * Kill off a pending delayed_work. Returns %true if @dwork was pending
  2528. * and canceled; %false if wasn't pending. Note that the work callback
  2529. * function may still be running on return, unless it returns %true and the
  2530. * work doesn't re-arm itself. Explicitly flush or use
  2531. * cancel_delayed_work_sync() to wait on it.
  2532. *
  2533. * This function is safe to call from any context including IRQ handler.
  2534. */
  2535. bool cancel_delayed_work(struct delayed_work *dwork)
  2536. {
  2537. unsigned long flags;
  2538. int ret;
  2539. do {
  2540. ret = try_to_grab_pending(&dwork->work, true, &flags);
  2541. } while (unlikely(ret == -EAGAIN));
  2542. if (unlikely(ret < 0))
  2543. return false;
  2544. set_work_pool_and_clear_pending(&dwork->work,
  2545. get_work_pool_id(&dwork->work));
  2546. local_irq_restore(flags);
  2547. return ret;
  2548. }
  2549. EXPORT_SYMBOL(cancel_delayed_work);
  2550. /**
  2551. * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
  2552. * @dwork: the delayed work cancel
  2553. *
  2554. * This is cancel_work_sync() for delayed works.
  2555. *
  2556. * RETURNS:
  2557. * %true if @dwork was pending, %false otherwise.
  2558. */
  2559. bool cancel_delayed_work_sync(struct delayed_work *dwork)
  2560. {
  2561. return __cancel_work_timer(&dwork->work, true);
  2562. }
  2563. EXPORT_SYMBOL(cancel_delayed_work_sync);
  2564. /**
  2565. * schedule_on_each_cpu - execute a function synchronously on each online CPU
  2566. * @func: the function to call
  2567. *
  2568. * schedule_on_each_cpu() executes @func on each online CPU using the
  2569. * system workqueue and blocks until all CPUs have completed.
  2570. * schedule_on_each_cpu() is very slow.
  2571. *
  2572. * RETURNS:
  2573. * 0 on success, -errno on failure.
  2574. */
  2575. int schedule_on_each_cpu(work_func_t func)
  2576. {
  2577. int cpu;
  2578. struct work_struct __percpu *works;
  2579. works = alloc_percpu(struct work_struct);
  2580. if (!works)
  2581. return -ENOMEM;
  2582. get_online_cpus();
  2583. for_each_online_cpu(cpu) {
  2584. struct work_struct *work = per_cpu_ptr(works, cpu);
  2585. INIT_WORK(work, func);
  2586. schedule_work_on(cpu, work);
  2587. }
  2588. for_each_online_cpu(cpu)
  2589. flush_work(per_cpu_ptr(works, cpu));
  2590. put_online_cpus();
  2591. free_percpu(works);
  2592. return 0;
  2593. }
  2594. /**
  2595. * flush_scheduled_work - ensure that any scheduled work has run to completion.
  2596. *
  2597. * Forces execution of the kernel-global workqueue and blocks until its
  2598. * completion.
  2599. *
  2600. * Think twice before calling this function! It's very easy to get into
  2601. * trouble if you don't take great care. Either of the following situations
  2602. * will lead to deadlock:
  2603. *
  2604. * One of the work items currently on the workqueue needs to acquire
  2605. * a lock held by your code or its caller.
  2606. *
  2607. * Your code is running in the context of a work routine.
  2608. *
  2609. * They will be detected by lockdep when they occur, but the first might not
  2610. * occur very often. It depends on what work items are on the workqueue and
  2611. * what locks they need, which you have no control over.
  2612. *
  2613. * In most situations flushing the entire workqueue is overkill; you merely
  2614. * need to know that a particular work item isn't queued and isn't running.
  2615. * In such cases you should use cancel_delayed_work_sync() or
  2616. * cancel_work_sync() instead.
  2617. */
  2618. void flush_scheduled_work(void)
  2619. {
  2620. flush_workqueue(system_wq);
  2621. }
  2622. EXPORT_SYMBOL(flush_scheduled_work);
  2623. /**
  2624. * execute_in_process_context - reliably execute the routine with user context
  2625. * @fn: the function to execute
  2626. * @ew: guaranteed storage for the execute work structure (must
  2627. * be available when the work executes)
  2628. *
  2629. * Executes the function immediately if process context is available,
  2630. * otherwise schedules the function for delayed execution.
  2631. *
  2632. * Returns: 0 - function was executed
  2633. * 1 - function was scheduled for execution
  2634. */
  2635. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  2636. {
  2637. if (!in_interrupt()) {
  2638. fn(&ew->work);
  2639. return 0;
  2640. }
  2641. INIT_WORK(&ew->work, fn);
  2642. schedule_work(&ew->work);
  2643. return 1;
  2644. }
  2645. EXPORT_SYMBOL_GPL(execute_in_process_context);
  2646. #ifdef CONFIG_SYSFS
  2647. /*
  2648. * Workqueues with WQ_SYSFS flag set is visible to userland via
  2649. * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
  2650. * following attributes.
  2651. *
  2652. * per_cpu RO bool : whether the workqueue is per-cpu or unbound
  2653. * max_active RW int : maximum number of in-flight work items
  2654. *
  2655. * Unbound workqueues have the following extra attributes.
  2656. *
  2657. * id RO int : the associated pool ID
  2658. * nice RW int : nice value of the workers
  2659. * cpumask RW mask : bitmask of allowed CPUs for the workers
  2660. */
  2661. struct wq_device {
  2662. struct workqueue_struct *wq;
  2663. struct device dev;
  2664. };
  2665. static struct workqueue_struct *dev_to_wq(struct device *dev)
  2666. {
  2667. struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
  2668. return wq_dev->wq;
  2669. }
  2670. static ssize_t wq_per_cpu_show(struct device *dev,
  2671. struct device_attribute *attr, char *buf)
  2672. {
  2673. struct workqueue_struct *wq = dev_to_wq(dev);
  2674. return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
  2675. }
  2676. static ssize_t wq_max_active_show(struct device *dev,
  2677. struct device_attribute *attr, char *buf)
  2678. {
  2679. struct workqueue_struct *wq = dev_to_wq(dev);
  2680. return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
  2681. }
  2682. static ssize_t wq_max_active_store(struct device *dev,
  2683. struct device_attribute *attr,
  2684. const char *buf, size_t count)
  2685. {
  2686. struct workqueue_struct *wq = dev_to_wq(dev);
  2687. int val;
  2688. if (sscanf(buf, "%d", &val) != 1 || val <= 0)
  2689. return -EINVAL;
  2690. workqueue_set_max_active(wq, val);
  2691. return count;
  2692. }
  2693. static struct device_attribute wq_sysfs_attrs[] = {
  2694. __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
  2695. __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
  2696. __ATTR_NULL,
  2697. };
  2698. static ssize_t wq_pool_id_show(struct device *dev,
  2699. struct device_attribute *attr, char *buf)
  2700. {
  2701. struct workqueue_struct *wq = dev_to_wq(dev);
  2702. struct worker_pool *pool;
  2703. int written;
  2704. rcu_read_lock_sched();
  2705. pool = first_pwq(wq)->pool;
  2706. written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
  2707. rcu_read_unlock_sched();
  2708. return written;
  2709. }
  2710. static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
  2711. char *buf)
  2712. {
  2713. struct workqueue_struct *wq = dev_to_wq(dev);
  2714. int written;
  2715. mutex_lock(&wq->mutex);
  2716. written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
  2717. mutex_unlock(&wq->mutex);
  2718. return written;
  2719. }
  2720. /* prepare workqueue_attrs for sysfs store operations */
  2721. static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
  2722. {
  2723. struct workqueue_attrs *attrs;
  2724. attrs = alloc_workqueue_attrs(GFP_KERNEL);
  2725. if (!attrs)
  2726. return NULL;
  2727. mutex_lock(&wq->mutex);
  2728. copy_workqueue_attrs(attrs, wq->unbound_attrs);
  2729. mutex_unlock(&wq->mutex);
  2730. return attrs;
  2731. }
  2732. static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
  2733. const char *buf, size_t count)
  2734. {
  2735. struct workqueue_struct *wq = dev_to_wq(dev);
  2736. struct workqueue_attrs *attrs;
  2737. int ret;
  2738. attrs = wq_sysfs_prep_attrs(wq);
  2739. if (!attrs)
  2740. return -ENOMEM;
  2741. if (sscanf(buf, "%d", &attrs->nice) == 1 &&
  2742. attrs->nice >= -20 && attrs->nice <= 19)
  2743. ret = apply_workqueue_attrs(wq, attrs);
  2744. else
  2745. ret = -EINVAL;
  2746. free_workqueue_attrs(attrs);
  2747. return ret ?: count;
  2748. }
  2749. static ssize_t wq_cpumask_show(struct device *dev,
  2750. struct device_attribute *attr, char *buf)
  2751. {
  2752. struct workqueue_struct *wq = dev_to_wq(dev);
  2753. int written;
  2754. mutex_lock(&wq->mutex);
  2755. written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
  2756. mutex_unlock(&wq->mutex);
  2757. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  2758. return written;
  2759. }
  2760. static ssize_t wq_cpumask_store(struct device *dev,
  2761. struct device_attribute *attr,
  2762. const char *buf, size_t count)
  2763. {
  2764. struct workqueue_struct *wq = dev_to_wq(dev);
  2765. struct workqueue_attrs *attrs;
  2766. int ret;
  2767. attrs = wq_sysfs_prep_attrs(wq);
  2768. if (!attrs)
  2769. return -ENOMEM;
  2770. ret = cpumask_parse(buf, attrs->cpumask);
  2771. if (!ret)
  2772. ret = apply_workqueue_attrs(wq, attrs);
  2773. free_workqueue_attrs(attrs);
  2774. return ret ?: count;
  2775. }
  2776. static struct device_attribute wq_sysfs_unbound_attrs[] = {
  2777. __ATTR(pool_id, 0444, wq_pool_id_show, NULL),
  2778. __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
  2779. __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
  2780. __ATTR_NULL,
  2781. };
  2782. static struct bus_type wq_subsys = {
  2783. .name = "workqueue",
  2784. .dev_attrs = wq_sysfs_attrs,
  2785. };
  2786. static int __init wq_sysfs_init(void)
  2787. {
  2788. return subsys_virtual_register(&wq_subsys, NULL);
  2789. }
  2790. core_initcall(wq_sysfs_init);
  2791. static void wq_device_release(struct device *dev)
  2792. {
  2793. struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
  2794. kfree(wq_dev);
  2795. }
  2796. /**
  2797. * workqueue_sysfs_register - make a workqueue visible in sysfs
  2798. * @wq: the workqueue to register
  2799. *
  2800. * Expose @wq in sysfs under /sys/bus/workqueue/devices.
  2801. * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
  2802. * which is the preferred method.
  2803. *
  2804. * Workqueue user should use this function directly iff it wants to apply
  2805. * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
  2806. * apply_workqueue_attrs() may race against userland updating the
  2807. * attributes.
  2808. *
  2809. * Returns 0 on success, -errno on failure.
  2810. */
  2811. int workqueue_sysfs_register(struct workqueue_struct *wq)
  2812. {
  2813. struct wq_device *wq_dev;
  2814. int ret;
  2815. /*
  2816. * Adjusting max_active or creating new pwqs by applyting
  2817. * attributes breaks ordering guarantee. Disallow exposing ordered
  2818. * workqueues.
  2819. */
  2820. if (WARN_ON(wq->flags & __WQ_ORDERED))
  2821. return -EINVAL;
  2822. wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
  2823. if (!wq_dev)
  2824. return -ENOMEM;
  2825. wq_dev->wq = wq;
  2826. wq_dev->dev.bus = &wq_subsys;
  2827. wq_dev->dev.init_name = wq->name;
  2828. wq_dev->dev.release = wq_device_release;
  2829. /*
  2830. * unbound_attrs are created separately. Suppress uevent until
  2831. * everything is ready.
  2832. */
  2833. dev_set_uevent_suppress(&wq_dev->dev, true);
  2834. ret = device_register(&wq_dev->dev);
  2835. if (ret) {
  2836. kfree(wq_dev);
  2837. wq->wq_dev = NULL;
  2838. return ret;
  2839. }
  2840. if (wq->flags & WQ_UNBOUND) {
  2841. struct device_attribute *attr;
  2842. for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
  2843. ret = device_create_file(&wq_dev->dev, attr);
  2844. if (ret) {
  2845. device_unregister(&wq_dev->dev);
  2846. wq->wq_dev = NULL;
  2847. return ret;
  2848. }
  2849. }
  2850. }
  2851. kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
  2852. return 0;
  2853. }
  2854. /**
  2855. * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
  2856. * @wq: the workqueue to unregister
  2857. *
  2858. * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
  2859. */
  2860. static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
  2861. {
  2862. struct wq_device *wq_dev = wq->wq_dev;
  2863. if (!wq->wq_dev)
  2864. return;
  2865. wq->wq_dev = NULL;
  2866. device_unregister(&wq_dev->dev);
  2867. }
  2868. #else /* CONFIG_SYSFS */
  2869. static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
  2870. #endif /* CONFIG_SYSFS */
  2871. /**
  2872. * free_workqueue_attrs - free a workqueue_attrs
  2873. * @attrs: workqueue_attrs to free
  2874. *
  2875. * Undo alloc_workqueue_attrs().
  2876. */
  2877. void free_workqueue_attrs(struct workqueue_attrs *attrs)
  2878. {
  2879. if (attrs) {
  2880. free_cpumask_var(attrs->cpumask);
  2881. kfree(attrs);
  2882. }
  2883. }
  2884. /**
  2885. * alloc_workqueue_attrs - allocate a workqueue_attrs
  2886. * @gfp_mask: allocation mask to use
  2887. *
  2888. * Allocate a new workqueue_attrs, initialize with default settings and
  2889. * return it. Returns NULL on failure.
  2890. */
  2891. struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
  2892. {
  2893. struct workqueue_attrs *attrs;
  2894. attrs = kzalloc(sizeof(*attrs), gfp_mask);
  2895. if (!attrs)
  2896. goto fail;
  2897. if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
  2898. goto fail;
  2899. cpumask_copy(attrs->cpumask, cpu_possible_mask);
  2900. return attrs;
  2901. fail:
  2902. free_workqueue_attrs(attrs);
  2903. return NULL;
  2904. }
  2905. static void copy_workqueue_attrs(struct workqueue_attrs *to,
  2906. const struct workqueue_attrs *from)
  2907. {
  2908. to->nice = from->nice;
  2909. cpumask_copy(to->cpumask, from->cpumask);
  2910. }
  2911. /* hash value of the content of @attr */
  2912. static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
  2913. {
  2914. u32 hash = 0;
  2915. hash = jhash_1word(attrs->nice, hash);
  2916. hash = jhash(cpumask_bits(attrs->cpumask),
  2917. BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
  2918. return hash;
  2919. }
  2920. /* content equality test */
  2921. static bool wqattrs_equal(const struct workqueue_attrs *a,
  2922. const struct workqueue_attrs *b)
  2923. {
  2924. if (a->nice != b->nice)
  2925. return false;
  2926. if (!cpumask_equal(a->cpumask, b->cpumask))
  2927. return false;
  2928. return true;
  2929. }
  2930. /**
  2931. * init_worker_pool - initialize a newly zalloc'd worker_pool
  2932. * @pool: worker_pool to initialize
  2933. *
  2934. * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
  2935. * Returns 0 on success, -errno on failure. Even on failure, all fields
  2936. * inside @pool proper are initialized and put_unbound_pool() can be called
  2937. * on @pool safely to release it.
  2938. */
  2939. static int init_worker_pool(struct worker_pool *pool)
  2940. {
  2941. spin_lock_init(&pool->lock);
  2942. pool->id = -1;
  2943. pool->cpu = -1;
  2944. pool->node = NUMA_NO_NODE;
  2945. pool->flags |= POOL_DISASSOCIATED;
  2946. INIT_LIST_HEAD(&pool->worklist);
  2947. INIT_LIST_HEAD(&pool->idle_list);
  2948. hash_init(pool->busy_hash);
  2949. init_timer_deferrable(&pool->idle_timer);
  2950. pool->idle_timer.function = idle_worker_timeout;
  2951. pool->idle_timer.data = (unsigned long)pool;
  2952. setup_timer(&pool->mayday_timer, pool_mayday_timeout,
  2953. (unsigned long)pool);
  2954. mutex_init(&pool->manager_arb);
  2955. mutex_init(&pool->manager_mutex);
  2956. idr_init(&pool->worker_idr);
  2957. INIT_HLIST_NODE(&pool->hash_node);
  2958. pool->refcnt = 1;
  2959. /* shouldn't fail above this point */
  2960. pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
  2961. if (!pool->attrs)
  2962. return -ENOMEM;
  2963. return 0;
  2964. }
  2965. static void rcu_free_pool(struct rcu_head *rcu)
  2966. {
  2967. struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
  2968. idr_destroy(&pool->worker_idr);
  2969. free_workqueue_attrs(pool->attrs);
  2970. kfree(pool);
  2971. }
  2972. /**
  2973. * put_unbound_pool - put a worker_pool
  2974. * @pool: worker_pool to put
  2975. *
  2976. * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  2977. * safe manner. get_unbound_pool() calls this function on its failure path
  2978. * and this function should be able to release pools which went through,
  2979. * successfully or not, init_worker_pool().
  2980. *
  2981. * Should be called with wq_pool_mutex held.
  2982. */
  2983. static void put_unbound_pool(struct worker_pool *pool)
  2984. {
  2985. struct worker *worker;
  2986. lockdep_assert_held(&wq_pool_mutex);
  2987. if (--pool->refcnt)
  2988. return;
  2989. /* sanity checks */
  2990. if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
  2991. WARN_ON(!list_empty(&pool->worklist)))
  2992. return;
  2993. /* release id and unhash */
  2994. if (pool->id >= 0)
  2995. idr_remove(&worker_pool_idr, pool->id);
  2996. hash_del(&pool->hash_node);
  2997. /*
  2998. * Become the manager and destroy all workers. Grabbing
  2999. * manager_arb prevents @pool's workers from blocking on
  3000. * manager_mutex.
  3001. */
  3002. mutex_lock(&pool->manager_arb);
  3003. mutex_lock(&pool->manager_mutex);
  3004. spin_lock_irq(&pool->lock);
  3005. while ((worker = first_worker(pool)))
  3006. destroy_worker(worker);
  3007. WARN_ON(pool->nr_workers || pool->nr_idle);
  3008. spin_unlock_irq(&pool->lock);
  3009. mutex_unlock(&pool->manager_mutex);
  3010. mutex_unlock(&pool->manager_arb);
  3011. /* shut down the timers */
  3012. del_timer_sync(&pool->idle_timer);
  3013. del_timer_sync(&pool->mayday_timer);
  3014. /* sched-RCU protected to allow dereferences from get_work_pool() */
  3015. call_rcu_sched(&pool->rcu, rcu_free_pool);
  3016. }
  3017. /**
  3018. * get_unbound_pool - get a worker_pool with the specified attributes
  3019. * @attrs: the attributes of the worker_pool to get
  3020. *
  3021. * Obtain a worker_pool which has the same attributes as @attrs, bump the
  3022. * reference count and return it. If there already is a matching
  3023. * worker_pool, it will be used; otherwise, this function attempts to
  3024. * create a new one. On failure, returns NULL.
  3025. *
  3026. * Should be called with wq_pool_mutex held.
  3027. */
  3028. static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
  3029. {
  3030. u32 hash = wqattrs_hash(attrs);
  3031. struct worker_pool *pool;
  3032. int node;
  3033. lockdep_assert_held(&wq_pool_mutex);
  3034. /* do we already have a matching pool? */
  3035. hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
  3036. if (wqattrs_equal(pool->attrs, attrs)) {
  3037. pool->refcnt++;
  3038. goto out_unlock;
  3039. }
  3040. }
  3041. /* nope, create a new one */
  3042. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  3043. if (!pool || init_worker_pool(pool) < 0)
  3044. goto fail;
  3045. if (workqueue_freezing)
  3046. pool->flags |= POOL_FREEZING;
  3047. lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
  3048. copy_workqueue_attrs(pool->attrs, attrs);
  3049. /* if cpumask is contained inside a NUMA node, we belong to that node */
  3050. if (wq_numa_enabled) {
  3051. for_each_node(node) {
  3052. if (cpumask_subset(pool->attrs->cpumask,
  3053. wq_numa_possible_cpumask[node])) {
  3054. pool->node = node;
  3055. break;
  3056. }
  3057. }
  3058. }
  3059. if (worker_pool_assign_id(pool) < 0)
  3060. goto fail;
  3061. /* create and start the initial worker */
  3062. if (create_and_start_worker(pool) < 0)
  3063. goto fail;
  3064. /* install */
  3065. hash_add(unbound_pool_hash, &pool->hash_node, hash);
  3066. out_unlock:
  3067. return pool;
  3068. fail:
  3069. if (pool)
  3070. put_unbound_pool(pool);
  3071. return NULL;
  3072. }
  3073. static void rcu_free_pwq(struct rcu_head *rcu)
  3074. {
  3075. kmem_cache_free(pwq_cache,
  3076. container_of(rcu, struct pool_workqueue, rcu));
  3077. }
  3078. /*
  3079. * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
  3080. * and needs to be destroyed.
  3081. */
  3082. static void pwq_unbound_release_workfn(struct work_struct *work)
  3083. {
  3084. struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
  3085. unbound_release_work);
  3086. struct workqueue_struct *wq = pwq->wq;
  3087. struct worker_pool *pool = pwq->pool;
  3088. bool is_last;
  3089. if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
  3090. return;
  3091. /*
  3092. * Unlink @pwq. Synchronization against wq->mutex isn't strictly
  3093. * necessary on release but do it anyway. It's easier to verify
  3094. * and consistent with the linking path.
  3095. */
  3096. mutex_lock(&wq->mutex);
  3097. list_del_rcu(&pwq->pwqs_node);
  3098. is_last = list_empty(&wq->pwqs);
  3099. mutex_unlock(&wq->mutex);
  3100. mutex_lock(&wq_pool_mutex);
  3101. put_unbound_pool(pool);
  3102. mutex_unlock(&wq_pool_mutex);
  3103. call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  3104. /*
  3105. * If we're the last pwq going away, @wq is already dead and no one
  3106. * is gonna access it anymore. Free it.
  3107. */
  3108. if (is_last) {
  3109. free_workqueue_attrs(wq->unbound_attrs);
  3110. kfree(wq);
  3111. }
  3112. }
  3113. /**
  3114. * pwq_adjust_max_active - update a pwq's max_active to the current setting
  3115. * @pwq: target pool_workqueue
  3116. *
  3117. * If @pwq isn't freezing, set @pwq->max_active to the associated
  3118. * workqueue's saved_max_active and activate delayed work items
  3119. * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
  3120. */
  3121. static void pwq_adjust_max_active(struct pool_workqueue *pwq)
  3122. {
  3123. struct workqueue_struct *wq = pwq->wq;
  3124. bool freezable = wq->flags & WQ_FREEZABLE;
  3125. /* for @wq->saved_max_active */
  3126. lockdep_assert_held(&wq->mutex);
  3127. /* fast exit for non-freezable wqs */
  3128. if (!freezable && pwq->max_active == wq->saved_max_active)
  3129. return;
  3130. spin_lock_irq(&pwq->pool->lock);
  3131. if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
  3132. pwq->max_active = wq->saved_max_active;
  3133. while (!list_empty(&pwq->delayed_works) &&
  3134. pwq->nr_active < pwq->max_active)
  3135. pwq_activate_first_delayed(pwq);
  3136. /*
  3137. * Need to kick a worker after thawed or an unbound wq's
  3138. * max_active is bumped. It's a slow path. Do it always.
  3139. */
  3140. wake_up_worker(pwq->pool);
  3141. } else {
  3142. pwq->max_active = 0;
  3143. }
  3144. spin_unlock_irq(&pwq->pool->lock);
  3145. }
  3146. /* initialize newly alloced @pwq which is associated with @wq and @pool */
  3147. static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
  3148. struct worker_pool *pool)
  3149. {
  3150. BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
  3151. memset(pwq, 0, sizeof(*pwq));
  3152. pwq->pool = pool;
  3153. pwq->wq = wq;
  3154. pwq->flush_color = -1;
  3155. pwq->refcnt = 1;
  3156. INIT_LIST_HEAD(&pwq->delayed_works);
  3157. INIT_LIST_HEAD(&pwq->pwqs_node);
  3158. INIT_LIST_HEAD(&pwq->mayday_node);
  3159. INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
  3160. }
  3161. /* sync @pwq with the current state of its associated wq and link it */
  3162. static void link_pwq(struct pool_workqueue *pwq)
  3163. {
  3164. struct workqueue_struct *wq = pwq->wq;
  3165. lockdep_assert_held(&wq->mutex);
  3166. /* may be called multiple times, ignore if already linked */
  3167. if (!list_empty(&pwq->pwqs_node))
  3168. return;
  3169. /*
  3170. * Set the matching work_color. This is synchronized with
  3171. * wq->mutex to avoid confusing flush_workqueue().
  3172. */
  3173. pwq->work_color = wq->work_color;
  3174. /* sync max_active to the current setting */
  3175. pwq_adjust_max_active(pwq);
  3176. /* link in @pwq */
  3177. list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
  3178. }
  3179. /* obtain a pool matching @attr and create a pwq associating the pool and @wq */
  3180. static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
  3181. const struct workqueue_attrs *attrs)
  3182. {
  3183. struct worker_pool *pool;
  3184. struct pool_workqueue *pwq;
  3185. lockdep_assert_held(&wq_pool_mutex);
  3186. pool = get_unbound_pool(attrs);
  3187. if (!pool)
  3188. return NULL;
  3189. pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
  3190. if (!pwq) {
  3191. put_unbound_pool(pool);
  3192. return NULL;
  3193. }
  3194. init_pwq(pwq, wq, pool);
  3195. return pwq;
  3196. }
  3197. /* undo alloc_unbound_pwq(), used only in the error path */
  3198. static void free_unbound_pwq(struct pool_workqueue *pwq)
  3199. {
  3200. lockdep_assert_held(&wq_pool_mutex);
  3201. if (pwq) {
  3202. put_unbound_pool(pwq->pool);
  3203. kfree(pwq);
  3204. }
  3205. }
  3206. /**
  3207. * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
  3208. * @attrs: the wq_attrs of interest
  3209. * @node: the target NUMA node
  3210. * @cpu_going_down: if >= 0, the CPU to consider as offline
  3211. * @cpumask: outarg, the resulting cpumask
  3212. *
  3213. * Calculate the cpumask a workqueue with @attrs should use on @node. If
  3214. * @cpu_going_down is >= 0, that cpu is considered offline during
  3215. * calculation. The result is stored in @cpumask. This function returns
  3216. * %true if the resulting @cpumask is different from @attrs->cpumask,
  3217. * %false if equal.
  3218. *
  3219. * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
  3220. * enabled and @node has online CPUs requested by @attrs, the returned
  3221. * cpumask is the intersection of the possible CPUs of @node and
  3222. * @attrs->cpumask.
  3223. *
  3224. * The caller is responsible for ensuring that the cpumask of @node stays
  3225. * stable.
  3226. */
  3227. static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
  3228. int cpu_going_down, cpumask_t *cpumask)
  3229. {
  3230. if (!wq_numa_enabled)
  3231. goto use_dfl;
  3232. /* does @node have any online CPUs @attrs wants? */
  3233. cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
  3234. if (cpu_going_down >= 0)
  3235. cpumask_clear_cpu(cpu_going_down, cpumask);
  3236. if (cpumask_empty(cpumask))
  3237. goto use_dfl;
  3238. /* yeap, return possible CPUs in @node that @attrs wants */
  3239. cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
  3240. return !cpumask_equal(cpumask, attrs->cpumask);
  3241. use_dfl:
  3242. cpumask_copy(cpumask, attrs->cpumask);
  3243. return false;
  3244. }
  3245. /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
  3246. static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
  3247. int node,
  3248. struct pool_workqueue *pwq)
  3249. {
  3250. struct pool_workqueue *old_pwq;
  3251. lockdep_assert_held(&wq->mutex);
  3252. /* link_pwq() can handle duplicate calls */
  3253. link_pwq(pwq);
  3254. old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
  3255. rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
  3256. return old_pwq;
  3257. }
  3258. /**
  3259. * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
  3260. * @wq: the target workqueue
  3261. * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
  3262. *
  3263. * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
  3264. * machines, this function maps a separate pwq to each NUMA node with
  3265. * possibles CPUs in @attrs->cpumask so that work items are affine to the
  3266. * NUMA node it was issued on. Older pwqs are released as in-flight work
  3267. * items finish. Note that a work item which repeatedly requeues itself
  3268. * back-to-back will stay on its current pwq.
  3269. *
  3270. * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
  3271. * failure.
  3272. */
  3273. int apply_workqueue_attrs(struct workqueue_struct *wq,
  3274. const struct workqueue_attrs *attrs)
  3275. {
  3276. struct workqueue_attrs *new_attrs, *tmp_attrs;
  3277. struct pool_workqueue **pwq_tbl, *dfl_pwq;
  3278. int node, ret;
  3279. /* only unbound workqueues can change attributes */
  3280. if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
  3281. return -EINVAL;
  3282. /* creating multiple pwqs breaks ordering guarantee */
  3283. if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
  3284. return -EINVAL;
  3285. pwq_tbl = kzalloc(wq_numa_tbl_len * sizeof(pwq_tbl[0]), GFP_KERNEL);
  3286. new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
  3287. tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
  3288. if (!pwq_tbl || !new_attrs || !tmp_attrs)
  3289. goto enomem;
  3290. /* make a copy of @attrs and sanitize it */
  3291. copy_workqueue_attrs(new_attrs, attrs);
  3292. cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
  3293. /*
  3294. * We may create multiple pwqs with differing cpumasks. Make a
  3295. * copy of @new_attrs which will be modified and used to obtain
  3296. * pools.
  3297. */
  3298. copy_workqueue_attrs(tmp_attrs, new_attrs);
  3299. /*
  3300. * CPUs should stay stable across pwq creations and installations.
  3301. * Pin CPUs, determine the target cpumask for each node and create
  3302. * pwqs accordingly.
  3303. */
  3304. get_online_cpus();
  3305. mutex_lock(&wq_pool_mutex);
  3306. /*
  3307. * If something goes wrong during CPU up/down, we'll fall back to
  3308. * the default pwq covering whole @attrs->cpumask. Always create
  3309. * it even if we don't use it immediately.
  3310. */
  3311. dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
  3312. if (!dfl_pwq)
  3313. goto enomem_pwq;
  3314. for_each_node(node) {
  3315. if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
  3316. pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
  3317. if (!pwq_tbl[node])
  3318. goto enomem_pwq;
  3319. } else {
  3320. dfl_pwq->refcnt++;
  3321. pwq_tbl[node] = dfl_pwq;
  3322. }
  3323. }
  3324. mutex_unlock(&wq_pool_mutex);
  3325. /* all pwqs have been created successfully, let's install'em */
  3326. mutex_lock(&wq->mutex);
  3327. copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
  3328. /* save the previous pwq and install the new one */
  3329. for_each_node(node)
  3330. pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
  3331. /* @dfl_pwq might not have been used, ensure it's linked */
  3332. link_pwq(dfl_pwq);
  3333. swap(wq->dfl_pwq, dfl_pwq);
  3334. mutex_unlock(&wq->mutex);
  3335. /* put the old pwqs */
  3336. for_each_node(node)
  3337. put_pwq_unlocked(pwq_tbl[node]);
  3338. put_pwq_unlocked(dfl_pwq);
  3339. put_online_cpus();
  3340. ret = 0;
  3341. /* fall through */
  3342. out_free:
  3343. free_workqueue_attrs(tmp_attrs);
  3344. free_workqueue_attrs(new_attrs);
  3345. kfree(pwq_tbl);
  3346. return ret;
  3347. enomem_pwq:
  3348. free_unbound_pwq(dfl_pwq);
  3349. for_each_node(node)
  3350. if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
  3351. free_unbound_pwq(pwq_tbl[node]);
  3352. mutex_unlock(&wq_pool_mutex);
  3353. put_online_cpus();
  3354. enomem:
  3355. ret = -ENOMEM;
  3356. goto out_free;
  3357. }
  3358. /**
  3359. * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
  3360. * @wq: the target workqueue
  3361. * @cpu: the CPU coming up or going down
  3362. * @online: whether @cpu is coming up or going down
  3363. *
  3364. * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
  3365. * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
  3366. * @wq accordingly.
  3367. *
  3368. * If NUMA affinity can't be adjusted due to memory allocation failure, it
  3369. * falls back to @wq->dfl_pwq which may not be optimal but is always
  3370. * correct.
  3371. *
  3372. * Note that when the last allowed CPU of a NUMA node goes offline for a
  3373. * workqueue with a cpumask spanning multiple nodes, the workers which were
  3374. * already executing the work items for the workqueue will lose their CPU
  3375. * affinity and may execute on any CPU. This is similar to how per-cpu
  3376. * workqueues behave on CPU_DOWN. If a workqueue user wants strict
  3377. * affinity, it's the user's responsibility to flush the work item from
  3378. * CPU_DOWN_PREPARE.
  3379. */
  3380. static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
  3381. bool online)
  3382. {
  3383. int node = cpu_to_node(cpu);
  3384. int cpu_off = online ? -1 : cpu;
  3385. struct pool_workqueue *old_pwq = NULL, *pwq;
  3386. struct workqueue_attrs *target_attrs;
  3387. cpumask_t *cpumask;
  3388. lockdep_assert_held(&wq_pool_mutex);
  3389. if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
  3390. return;
  3391. /*
  3392. * We don't wanna alloc/free wq_attrs for each wq for each CPU.
  3393. * Let's use a preallocated one. The following buf is protected by
  3394. * CPU hotplug exclusion.
  3395. */
  3396. target_attrs = wq_update_unbound_numa_attrs_buf;
  3397. cpumask = target_attrs->cpumask;
  3398. mutex_lock(&wq->mutex);
  3399. copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
  3400. pwq = unbound_pwq_by_node(wq, node);
  3401. /*
  3402. * Let's determine what needs to be done. If the target cpumask is
  3403. * different from wq's, we need to compare it to @pwq's and create
  3404. * a new one if they don't match. If the target cpumask equals
  3405. * wq's, the default pwq should be used. If @pwq is already the
  3406. * default one, nothing to do; otherwise, install the default one.
  3407. */
  3408. if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
  3409. if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
  3410. goto out_unlock;
  3411. } else {
  3412. if (pwq == wq->dfl_pwq)
  3413. goto out_unlock;
  3414. else
  3415. goto use_dfl_pwq;
  3416. }
  3417. mutex_unlock(&wq->mutex);
  3418. /* create a new pwq */
  3419. pwq = alloc_unbound_pwq(wq, target_attrs);
  3420. if (!pwq) {
  3421. pr_warning("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
  3422. wq->name);
  3423. goto out_unlock;
  3424. }
  3425. /*
  3426. * Install the new pwq. As this function is called only from CPU
  3427. * hotplug callbacks and applying a new attrs is wrapped with
  3428. * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
  3429. * inbetween.
  3430. */
  3431. mutex_lock(&wq->mutex);
  3432. old_pwq = numa_pwq_tbl_install(wq, node, pwq);
  3433. goto out_unlock;
  3434. use_dfl_pwq:
  3435. spin_lock_irq(&wq->dfl_pwq->pool->lock);
  3436. get_pwq(wq->dfl_pwq);
  3437. spin_unlock_irq(&wq->dfl_pwq->pool->lock);
  3438. old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
  3439. out_unlock:
  3440. mutex_unlock(&wq->mutex);
  3441. put_pwq_unlocked(old_pwq);
  3442. }
  3443. static int alloc_and_link_pwqs(struct workqueue_struct *wq)
  3444. {
  3445. bool highpri = wq->flags & WQ_HIGHPRI;
  3446. int cpu;
  3447. if (!(wq->flags & WQ_UNBOUND)) {
  3448. wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
  3449. if (!wq->cpu_pwqs)
  3450. return -ENOMEM;
  3451. for_each_possible_cpu(cpu) {
  3452. struct pool_workqueue *pwq =
  3453. per_cpu_ptr(wq->cpu_pwqs, cpu);
  3454. struct worker_pool *cpu_pools =
  3455. per_cpu(cpu_worker_pools, cpu);
  3456. init_pwq(pwq, wq, &cpu_pools[highpri]);
  3457. mutex_lock(&wq->mutex);
  3458. link_pwq(pwq);
  3459. mutex_unlock(&wq->mutex);
  3460. }
  3461. return 0;
  3462. } else {
  3463. return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
  3464. }
  3465. }
  3466. static int wq_clamp_max_active(int max_active, unsigned int flags,
  3467. const char *name)
  3468. {
  3469. int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
  3470. if (max_active < 1 || max_active > lim)
  3471. pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
  3472. max_active, name, 1, lim);
  3473. return clamp_val(max_active, 1, lim);
  3474. }
  3475. struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
  3476. unsigned int flags,
  3477. int max_active,
  3478. struct lock_class_key *key,
  3479. const char *lock_name, ...)
  3480. {
  3481. size_t tbl_size = 0;
  3482. va_list args;
  3483. struct workqueue_struct *wq;
  3484. struct pool_workqueue *pwq;
  3485. /* allocate wq and format name */
  3486. if (flags & WQ_UNBOUND)
  3487. tbl_size = wq_numa_tbl_len * sizeof(wq->numa_pwq_tbl[0]);
  3488. wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
  3489. if (!wq)
  3490. return NULL;
  3491. if (flags & WQ_UNBOUND) {
  3492. wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
  3493. if (!wq->unbound_attrs)
  3494. goto err_free_wq;
  3495. }
  3496. va_start(args, lock_name);
  3497. vsnprintf(wq->name, sizeof(wq->name), fmt, args);
  3498. va_end(args);
  3499. max_active = max_active ?: WQ_DFL_ACTIVE;
  3500. max_active = wq_clamp_max_active(max_active, flags, wq->name);
  3501. /* init wq */
  3502. wq->flags = flags;
  3503. wq->saved_max_active = max_active;
  3504. mutex_init(&wq->mutex);
  3505. atomic_set(&wq->nr_pwqs_to_flush, 0);
  3506. INIT_LIST_HEAD(&wq->pwqs);
  3507. INIT_LIST_HEAD(&wq->flusher_queue);
  3508. INIT_LIST_HEAD(&wq->flusher_overflow);
  3509. INIT_LIST_HEAD(&wq->maydays);
  3510. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  3511. INIT_LIST_HEAD(&wq->list);
  3512. if (alloc_and_link_pwqs(wq) < 0)
  3513. goto err_free_wq;
  3514. /*
  3515. * Workqueues which may be used during memory reclaim should
  3516. * have a rescuer to guarantee forward progress.
  3517. */
  3518. if (flags & WQ_MEM_RECLAIM) {
  3519. struct worker *rescuer;
  3520. rescuer = alloc_worker();
  3521. if (!rescuer)
  3522. goto err_destroy;
  3523. rescuer->rescue_wq = wq;
  3524. rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
  3525. wq->name);
  3526. if (IS_ERR(rescuer->task)) {
  3527. kfree(rescuer);
  3528. goto err_destroy;
  3529. }
  3530. wq->rescuer = rescuer;
  3531. rescuer->task->flags |= PF_NO_SETAFFINITY;
  3532. wake_up_process(rescuer->task);
  3533. }
  3534. if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
  3535. goto err_destroy;
  3536. /*
  3537. * wq_pool_mutex protects global freeze state and workqueues list.
  3538. * Grab it, adjust max_active and add the new @wq to workqueues
  3539. * list.
  3540. */
  3541. mutex_lock(&wq_pool_mutex);
  3542. mutex_lock(&wq->mutex);
  3543. for_each_pwq(pwq, wq)
  3544. pwq_adjust_max_active(pwq);
  3545. mutex_unlock(&wq->mutex);
  3546. list_add(&wq->list, &workqueues);
  3547. mutex_unlock(&wq_pool_mutex);
  3548. return wq;
  3549. err_free_wq:
  3550. free_workqueue_attrs(wq->unbound_attrs);
  3551. kfree(wq);
  3552. return NULL;
  3553. err_destroy:
  3554. destroy_workqueue(wq);
  3555. return NULL;
  3556. }
  3557. EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
  3558. /**
  3559. * destroy_workqueue - safely terminate a workqueue
  3560. * @wq: target workqueue
  3561. *
  3562. * Safely destroy a workqueue. All work currently pending will be done first.
  3563. */
  3564. void destroy_workqueue(struct workqueue_struct *wq)
  3565. {
  3566. struct pool_workqueue *pwq;
  3567. int node;
  3568. /* drain it before proceeding with destruction */
  3569. drain_workqueue(wq);
  3570. /* sanity checks */
  3571. mutex_lock(&wq->mutex);
  3572. for_each_pwq(pwq, wq) {
  3573. int i;
  3574. for (i = 0; i < WORK_NR_COLORS; i++) {
  3575. if (WARN_ON(pwq->nr_in_flight[i])) {
  3576. mutex_unlock(&wq->mutex);
  3577. return;
  3578. }
  3579. }
  3580. if (WARN_ON(pwq->refcnt > 1) ||
  3581. WARN_ON(pwq->nr_active) ||
  3582. WARN_ON(!list_empty(&pwq->delayed_works))) {
  3583. mutex_unlock(&wq->mutex);
  3584. return;
  3585. }
  3586. }
  3587. mutex_unlock(&wq->mutex);
  3588. /*
  3589. * wq list is used to freeze wq, remove from list after
  3590. * flushing is complete in case freeze races us.
  3591. */
  3592. mutex_lock(&wq_pool_mutex);
  3593. list_del_init(&wq->list);
  3594. mutex_unlock(&wq_pool_mutex);
  3595. workqueue_sysfs_unregister(wq);
  3596. if (wq->rescuer) {
  3597. kthread_stop(wq->rescuer->task);
  3598. kfree(wq->rescuer);
  3599. wq->rescuer = NULL;
  3600. }
  3601. if (!(wq->flags & WQ_UNBOUND)) {
  3602. /*
  3603. * The base ref is never dropped on per-cpu pwqs. Directly
  3604. * free the pwqs and wq.
  3605. */
  3606. free_percpu(wq->cpu_pwqs);
  3607. kfree(wq);
  3608. } else {
  3609. /*
  3610. * We're the sole accessor of @wq at this point. Directly
  3611. * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
  3612. * @wq will be freed when the last pwq is released.
  3613. */
  3614. for_each_node(node) {
  3615. pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
  3616. RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
  3617. put_pwq_unlocked(pwq);
  3618. }
  3619. /*
  3620. * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
  3621. * put. Don't access it afterwards.
  3622. */
  3623. pwq = wq->dfl_pwq;
  3624. wq->dfl_pwq = NULL;
  3625. put_pwq_unlocked(pwq);
  3626. }
  3627. }
  3628. EXPORT_SYMBOL_GPL(destroy_workqueue);
  3629. /**
  3630. * workqueue_set_max_active - adjust max_active of a workqueue
  3631. * @wq: target workqueue
  3632. * @max_active: new max_active value.
  3633. *
  3634. * Set max_active of @wq to @max_active.
  3635. *
  3636. * CONTEXT:
  3637. * Don't call from IRQ context.
  3638. */
  3639. void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
  3640. {
  3641. struct pool_workqueue *pwq;
  3642. /* disallow meddling with max_active for ordered workqueues */
  3643. if (WARN_ON(wq->flags & __WQ_ORDERED))
  3644. return;
  3645. max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
  3646. mutex_lock(&wq->mutex);
  3647. wq->saved_max_active = max_active;
  3648. for_each_pwq(pwq, wq)
  3649. pwq_adjust_max_active(pwq);
  3650. mutex_unlock(&wq->mutex);
  3651. }
  3652. EXPORT_SYMBOL_GPL(workqueue_set_max_active);
  3653. /**
  3654. * current_is_workqueue_rescuer - is %current workqueue rescuer?
  3655. *
  3656. * Determine whether %current is a workqueue rescuer. Can be used from
  3657. * work functions to determine whether it's being run off the rescuer task.
  3658. */
  3659. bool current_is_workqueue_rescuer(void)
  3660. {
  3661. struct worker *worker = current_wq_worker();
  3662. return worker && worker->rescue_wq;
  3663. }
  3664. /**
  3665. * workqueue_congested - test whether a workqueue is congested
  3666. * @cpu: CPU in question
  3667. * @wq: target workqueue
  3668. *
  3669. * Test whether @wq's cpu workqueue for @cpu is congested. There is
  3670. * no synchronization around this function and the test result is
  3671. * unreliable and only useful as advisory hints or for debugging.
  3672. *
  3673. * RETURNS:
  3674. * %true if congested, %false otherwise.
  3675. */
  3676. bool workqueue_congested(int cpu, struct workqueue_struct *wq)
  3677. {
  3678. struct pool_workqueue *pwq;
  3679. bool ret;
  3680. rcu_read_lock_sched();
  3681. if (!(wq->flags & WQ_UNBOUND))
  3682. pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
  3683. else
  3684. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  3685. ret = !list_empty(&pwq->delayed_works);
  3686. rcu_read_unlock_sched();
  3687. return ret;
  3688. }
  3689. EXPORT_SYMBOL_GPL(workqueue_congested);
  3690. /**
  3691. * work_busy - test whether a work is currently pending or running
  3692. * @work: the work to be tested
  3693. *
  3694. * Test whether @work is currently pending or running. There is no
  3695. * synchronization around this function and the test result is
  3696. * unreliable and only useful as advisory hints or for debugging.
  3697. *
  3698. * RETURNS:
  3699. * OR'd bitmask of WORK_BUSY_* bits.
  3700. */
  3701. unsigned int work_busy(struct work_struct *work)
  3702. {
  3703. struct worker_pool *pool;
  3704. unsigned long flags;
  3705. unsigned int ret = 0;
  3706. if (work_pending(work))
  3707. ret |= WORK_BUSY_PENDING;
  3708. local_irq_save(flags);
  3709. pool = get_work_pool(work);
  3710. if (pool) {
  3711. spin_lock(&pool->lock);
  3712. if (find_worker_executing_work(pool, work))
  3713. ret |= WORK_BUSY_RUNNING;
  3714. spin_unlock(&pool->lock);
  3715. }
  3716. local_irq_restore(flags);
  3717. return ret;
  3718. }
  3719. EXPORT_SYMBOL_GPL(work_busy);
  3720. /*
  3721. * CPU hotplug.
  3722. *
  3723. * There are two challenges in supporting CPU hotplug. Firstly, there
  3724. * are a lot of assumptions on strong associations among work, pwq and
  3725. * pool which make migrating pending and scheduled works very
  3726. * difficult to implement without impacting hot paths. Secondly,
  3727. * worker pools serve mix of short, long and very long running works making
  3728. * blocked draining impractical.
  3729. *
  3730. * This is solved by allowing the pools to be disassociated from the CPU
  3731. * running as an unbound one and allowing it to be reattached later if the
  3732. * cpu comes back online.
  3733. */
  3734. static void wq_unbind_fn(struct work_struct *work)
  3735. {
  3736. int cpu = smp_processor_id();
  3737. struct worker_pool *pool;
  3738. struct worker *worker;
  3739. int wi;
  3740. for_each_cpu_worker_pool(pool, cpu) {
  3741. WARN_ON_ONCE(cpu != smp_processor_id());
  3742. mutex_lock(&pool->manager_mutex);
  3743. spin_lock_irq(&pool->lock);
  3744. /*
  3745. * We've blocked all manager operations. Make all workers
  3746. * unbound and set DISASSOCIATED. Before this, all workers
  3747. * except for the ones which are still executing works from
  3748. * before the last CPU down must be on the cpu. After
  3749. * this, they may become diasporas.
  3750. */
  3751. for_each_pool_worker(worker, wi, pool)
  3752. worker->flags |= WORKER_UNBOUND;
  3753. pool->flags |= POOL_DISASSOCIATED;
  3754. spin_unlock_irq(&pool->lock);
  3755. mutex_unlock(&pool->manager_mutex);
  3756. }
  3757. /*
  3758. * Call schedule() so that we cross rq->lock and thus can guarantee
  3759. * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
  3760. * as scheduler callbacks may be invoked from other cpus.
  3761. */
  3762. schedule();
  3763. /*
  3764. * Sched callbacks are disabled now. Zap nr_running. After this,
  3765. * nr_running stays zero and need_more_worker() and keep_working()
  3766. * are always true as long as the worklist is not empty. Pools on
  3767. * @cpu now behave as unbound (in terms of concurrency management)
  3768. * pools which are served by workers tied to the CPU.
  3769. *
  3770. * On return from this function, the current worker would trigger
  3771. * unbound chain execution of pending work items if other workers
  3772. * didn't already.
  3773. */
  3774. for_each_cpu_worker_pool(pool, cpu)
  3775. atomic_set(&pool->nr_running, 0);
  3776. }
  3777. /**
  3778. * rebind_workers - rebind all workers of a pool to the associated CPU
  3779. * @pool: pool of interest
  3780. *
  3781. * @pool->cpu is coming online. Rebind all workers to the CPU.
  3782. */
  3783. static void rebind_workers(struct worker_pool *pool)
  3784. {
  3785. struct worker *worker;
  3786. int wi;
  3787. lockdep_assert_held(&pool->manager_mutex);
  3788. /*
  3789. * Restore CPU affinity of all workers. As all idle workers should
  3790. * be on the run-queue of the associated CPU before any local
  3791. * wake-ups for concurrency management happen, restore CPU affinty
  3792. * of all workers first and then clear UNBOUND. As we're called
  3793. * from CPU_ONLINE, the following shouldn't fail.
  3794. */
  3795. for_each_pool_worker(worker, wi, pool)
  3796. WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
  3797. pool->attrs->cpumask) < 0);
  3798. spin_lock_irq(&pool->lock);
  3799. for_each_pool_worker(worker, wi, pool) {
  3800. unsigned int worker_flags = worker->flags;
  3801. /*
  3802. * A bound idle worker should actually be on the runqueue
  3803. * of the associated CPU for local wake-ups targeting it to
  3804. * work. Kick all idle workers so that they migrate to the
  3805. * associated CPU. Doing this in the same loop as
  3806. * replacing UNBOUND with REBOUND is safe as no worker will
  3807. * be bound before @pool->lock is released.
  3808. */
  3809. if (worker_flags & WORKER_IDLE)
  3810. wake_up_process(worker->task);
  3811. /*
  3812. * We want to clear UNBOUND but can't directly call
  3813. * worker_clr_flags() or adjust nr_running. Atomically
  3814. * replace UNBOUND with another NOT_RUNNING flag REBOUND.
  3815. * @worker will clear REBOUND using worker_clr_flags() when
  3816. * it initiates the next execution cycle thus restoring
  3817. * concurrency management. Note that when or whether
  3818. * @worker clears REBOUND doesn't affect correctness.
  3819. *
  3820. * ACCESS_ONCE() is necessary because @worker->flags may be
  3821. * tested without holding any lock in
  3822. * wq_worker_waking_up(). Without it, NOT_RUNNING test may
  3823. * fail incorrectly leading to premature concurrency
  3824. * management operations.
  3825. */
  3826. WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
  3827. worker_flags |= WORKER_REBOUND;
  3828. worker_flags &= ~WORKER_UNBOUND;
  3829. ACCESS_ONCE(worker->flags) = worker_flags;
  3830. }
  3831. spin_unlock_irq(&pool->lock);
  3832. }
  3833. /**
  3834. * restore_unbound_workers_cpumask - restore cpumask of unbound workers
  3835. * @pool: unbound pool of interest
  3836. * @cpu: the CPU which is coming up
  3837. *
  3838. * An unbound pool may end up with a cpumask which doesn't have any online
  3839. * CPUs. When a worker of such pool get scheduled, the scheduler resets
  3840. * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
  3841. * online CPU before, cpus_allowed of all its workers should be restored.
  3842. */
  3843. static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
  3844. {
  3845. static cpumask_t cpumask;
  3846. struct worker *worker;
  3847. int wi;
  3848. lockdep_assert_held(&pool->manager_mutex);
  3849. /* is @cpu allowed for @pool? */
  3850. if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
  3851. return;
  3852. /* is @cpu the only online CPU? */
  3853. cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
  3854. if (cpumask_weight(&cpumask) != 1)
  3855. return;
  3856. /* as we're called from CPU_ONLINE, the following shouldn't fail */
  3857. for_each_pool_worker(worker, wi, pool)
  3858. WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
  3859. pool->attrs->cpumask) < 0);
  3860. }
  3861. /*
  3862. * Workqueues should be brought up before normal priority CPU notifiers.
  3863. * This will be registered high priority CPU notifier.
  3864. */
  3865. static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
  3866. unsigned long action,
  3867. void *hcpu)
  3868. {
  3869. int cpu = (unsigned long)hcpu;
  3870. struct worker_pool *pool;
  3871. struct workqueue_struct *wq;
  3872. int pi;
  3873. switch (action & ~CPU_TASKS_FROZEN) {
  3874. case CPU_UP_PREPARE:
  3875. for_each_cpu_worker_pool(pool, cpu) {
  3876. if (pool->nr_workers)
  3877. continue;
  3878. if (create_and_start_worker(pool) < 0)
  3879. return NOTIFY_BAD;
  3880. }
  3881. break;
  3882. case CPU_DOWN_FAILED:
  3883. case CPU_ONLINE:
  3884. mutex_lock(&wq_pool_mutex);
  3885. for_each_pool(pool, pi) {
  3886. mutex_lock(&pool->manager_mutex);
  3887. if (pool->cpu == cpu) {
  3888. spin_lock_irq(&pool->lock);
  3889. pool->flags &= ~POOL_DISASSOCIATED;
  3890. spin_unlock_irq(&pool->lock);
  3891. rebind_workers(pool);
  3892. } else if (pool->cpu < 0) {
  3893. restore_unbound_workers_cpumask(pool, cpu);
  3894. }
  3895. mutex_unlock(&pool->manager_mutex);
  3896. }
  3897. /* update NUMA affinity of unbound workqueues */
  3898. list_for_each_entry(wq, &workqueues, list)
  3899. wq_update_unbound_numa(wq, cpu, true);
  3900. mutex_unlock(&wq_pool_mutex);
  3901. break;
  3902. }
  3903. return NOTIFY_OK;
  3904. }
  3905. /*
  3906. * Workqueues should be brought down after normal priority CPU notifiers.
  3907. * This will be registered as low priority CPU notifier.
  3908. */
  3909. static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
  3910. unsigned long action,
  3911. void *hcpu)
  3912. {
  3913. int cpu = (unsigned long)hcpu;
  3914. struct work_struct unbind_work;
  3915. struct workqueue_struct *wq;
  3916. switch (action & ~CPU_TASKS_FROZEN) {
  3917. case CPU_DOWN_PREPARE:
  3918. /* unbinding per-cpu workers should happen on the local CPU */
  3919. INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
  3920. queue_work_on(cpu, system_highpri_wq, &unbind_work);
  3921. /* update NUMA affinity of unbound workqueues */
  3922. mutex_lock(&wq_pool_mutex);
  3923. list_for_each_entry(wq, &workqueues, list)
  3924. wq_update_unbound_numa(wq, cpu, false);
  3925. mutex_unlock(&wq_pool_mutex);
  3926. /* wait for per-cpu unbinding to finish */
  3927. flush_work(&unbind_work);
  3928. break;
  3929. }
  3930. return NOTIFY_OK;
  3931. }
  3932. #ifdef CONFIG_SMP
  3933. struct work_for_cpu {
  3934. struct work_struct work;
  3935. long (*fn)(void *);
  3936. void *arg;
  3937. long ret;
  3938. };
  3939. static void work_for_cpu_fn(struct work_struct *work)
  3940. {
  3941. struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
  3942. wfc->ret = wfc->fn(wfc->arg);
  3943. }
  3944. /**
  3945. * work_on_cpu - run a function in user context on a particular cpu
  3946. * @cpu: the cpu to run on
  3947. * @fn: the function to run
  3948. * @arg: the function arg
  3949. *
  3950. * This will return the value @fn returns.
  3951. * It is up to the caller to ensure that the cpu doesn't go offline.
  3952. * The caller must not hold any locks which would prevent @fn from completing.
  3953. */
  3954. long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
  3955. {
  3956. struct work_for_cpu wfc = { .fn = fn, .arg = arg };
  3957. INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
  3958. schedule_work_on(cpu, &wfc.work);
  3959. flush_work(&wfc.work);
  3960. return wfc.ret;
  3961. }
  3962. EXPORT_SYMBOL_GPL(work_on_cpu);
  3963. #endif /* CONFIG_SMP */
  3964. #ifdef CONFIG_FREEZER
  3965. /**
  3966. * freeze_workqueues_begin - begin freezing workqueues
  3967. *
  3968. * Start freezing workqueues. After this function returns, all freezable
  3969. * workqueues will queue new works to their delayed_works list instead of
  3970. * pool->worklist.
  3971. *
  3972. * CONTEXT:
  3973. * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
  3974. */
  3975. void freeze_workqueues_begin(void)
  3976. {
  3977. struct worker_pool *pool;
  3978. struct workqueue_struct *wq;
  3979. struct pool_workqueue *pwq;
  3980. int pi;
  3981. mutex_lock(&wq_pool_mutex);
  3982. WARN_ON_ONCE(workqueue_freezing);
  3983. workqueue_freezing = true;
  3984. /* set FREEZING */
  3985. for_each_pool(pool, pi) {
  3986. spin_lock_irq(&pool->lock);
  3987. WARN_ON_ONCE(pool->flags & POOL_FREEZING);
  3988. pool->flags |= POOL_FREEZING;
  3989. spin_unlock_irq(&pool->lock);
  3990. }
  3991. list_for_each_entry(wq, &workqueues, list) {
  3992. mutex_lock(&wq->mutex);
  3993. for_each_pwq(pwq, wq)
  3994. pwq_adjust_max_active(pwq);
  3995. mutex_unlock(&wq->mutex);
  3996. }
  3997. mutex_unlock(&wq_pool_mutex);
  3998. }
  3999. /**
  4000. * freeze_workqueues_busy - are freezable workqueues still busy?
  4001. *
  4002. * Check whether freezing is complete. This function must be called
  4003. * between freeze_workqueues_begin() and thaw_workqueues().
  4004. *
  4005. * CONTEXT:
  4006. * Grabs and releases wq_pool_mutex.
  4007. *
  4008. * RETURNS:
  4009. * %true if some freezable workqueues are still busy. %false if freezing
  4010. * is complete.
  4011. */
  4012. bool freeze_workqueues_busy(void)
  4013. {
  4014. bool busy = false;
  4015. struct workqueue_struct *wq;
  4016. struct pool_workqueue *pwq;
  4017. mutex_lock(&wq_pool_mutex);
  4018. WARN_ON_ONCE(!workqueue_freezing);
  4019. list_for_each_entry(wq, &workqueues, list) {
  4020. if (!(wq->flags & WQ_FREEZABLE))
  4021. continue;
  4022. /*
  4023. * nr_active is monotonically decreasing. It's safe
  4024. * to peek without lock.
  4025. */
  4026. rcu_read_lock_sched();
  4027. for_each_pwq(pwq, wq) {
  4028. WARN_ON_ONCE(pwq->nr_active < 0);
  4029. if (pwq->nr_active) {
  4030. busy = true;
  4031. rcu_read_unlock_sched();
  4032. goto out_unlock;
  4033. }
  4034. }
  4035. rcu_read_unlock_sched();
  4036. }
  4037. out_unlock:
  4038. mutex_unlock(&wq_pool_mutex);
  4039. return busy;
  4040. }
  4041. /**
  4042. * thaw_workqueues - thaw workqueues
  4043. *
  4044. * Thaw workqueues. Normal queueing is restored and all collected
  4045. * frozen works are transferred to their respective pool worklists.
  4046. *
  4047. * CONTEXT:
  4048. * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
  4049. */
  4050. void thaw_workqueues(void)
  4051. {
  4052. struct workqueue_struct *wq;
  4053. struct pool_workqueue *pwq;
  4054. struct worker_pool *pool;
  4055. int pi;
  4056. mutex_lock(&wq_pool_mutex);
  4057. if (!workqueue_freezing)
  4058. goto out_unlock;
  4059. /* clear FREEZING */
  4060. for_each_pool(pool, pi) {
  4061. spin_lock_irq(&pool->lock);
  4062. WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
  4063. pool->flags &= ~POOL_FREEZING;
  4064. spin_unlock_irq(&pool->lock);
  4065. }
  4066. /* restore max_active and repopulate worklist */
  4067. list_for_each_entry(wq, &workqueues, list) {
  4068. mutex_lock(&wq->mutex);
  4069. for_each_pwq(pwq, wq)
  4070. pwq_adjust_max_active(pwq);
  4071. mutex_unlock(&wq->mutex);
  4072. }
  4073. workqueue_freezing = false;
  4074. out_unlock:
  4075. mutex_unlock(&wq_pool_mutex);
  4076. }
  4077. #endif /* CONFIG_FREEZER */
  4078. static void __init wq_numa_init(void)
  4079. {
  4080. cpumask_var_t *tbl;
  4081. int node, cpu;
  4082. /* determine NUMA pwq table len - highest node id + 1 */
  4083. for_each_node(node)
  4084. wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
  4085. if (num_possible_nodes() <= 1)
  4086. return;
  4087. wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
  4088. BUG_ON(!wq_update_unbound_numa_attrs_buf);
  4089. /*
  4090. * We want masks of possible CPUs of each node which isn't readily
  4091. * available. Build one from cpu_to_node() which should have been
  4092. * fully initialized by now.
  4093. */
  4094. tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
  4095. BUG_ON(!tbl);
  4096. for_each_node(node)
  4097. BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL, node));
  4098. for_each_possible_cpu(cpu) {
  4099. node = cpu_to_node(cpu);
  4100. if (WARN_ON(node == NUMA_NO_NODE)) {
  4101. pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
  4102. /* happens iff arch is bonkers, let's just proceed */
  4103. return;
  4104. }
  4105. cpumask_set_cpu(cpu, tbl[node]);
  4106. }
  4107. wq_numa_possible_cpumask = tbl;
  4108. wq_numa_enabled = true;
  4109. }
  4110. static int __init init_workqueues(void)
  4111. {
  4112. int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
  4113. int i, cpu;
  4114. /* make sure we have enough bits for OFFQ pool ID */
  4115. BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
  4116. WORK_CPU_END * NR_STD_WORKER_POOLS);
  4117. WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
  4118. pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
  4119. cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
  4120. hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
  4121. wq_numa_init();
  4122. /* initialize CPU pools */
  4123. for_each_possible_cpu(cpu) {
  4124. struct worker_pool *pool;
  4125. i = 0;
  4126. for_each_cpu_worker_pool(pool, cpu) {
  4127. BUG_ON(init_worker_pool(pool));
  4128. pool->cpu = cpu;
  4129. cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
  4130. pool->attrs->nice = std_nice[i++];
  4131. pool->node = cpu_to_node(cpu);
  4132. /* alloc pool ID */
  4133. mutex_lock(&wq_pool_mutex);
  4134. BUG_ON(worker_pool_assign_id(pool));
  4135. mutex_unlock(&wq_pool_mutex);
  4136. }
  4137. }
  4138. /* create the initial worker */
  4139. for_each_online_cpu(cpu) {
  4140. struct worker_pool *pool;
  4141. for_each_cpu_worker_pool(pool, cpu) {
  4142. pool->flags &= ~POOL_DISASSOCIATED;
  4143. BUG_ON(create_and_start_worker(pool) < 0);
  4144. }
  4145. }
  4146. /* create default unbound wq attrs */
  4147. for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
  4148. struct workqueue_attrs *attrs;
  4149. BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
  4150. attrs->nice = std_nice[i];
  4151. unbound_std_wq_attrs[i] = attrs;
  4152. }
  4153. system_wq = alloc_workqueue("events", 0, 0);
  4154. system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
  4155. system_long_wq = alloc_workqueue("events_long", 0, 0);
  4156. system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
  4157. WQ_UNBOUND_MAX_ACTIVE);
  4158. system_freezable_wq = alloc_workqueue("events_freezable",
  4159. WQ_FREEZABLE, 0);
  4160. BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
  4161. !system_unbound_wq || !system_freezable_wq);
  4162. return 0;
  4163. }
  4164. early_initcall(init_workqueues);