fair.c 158 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172
  1. /*
  2. * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Interactivity improvements by Mike Galbraith
  7. * (C) 2007 Mike Galbraith <efault@gmx.de>
  8. *
  9. * Various enhancements by Dmitry Adamushko.
  10. * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
  11. *
  12. * Group scheduling enhancements by Srivatsa Vaddagiri
  13. * Copyright IBM Corporation, 2007
  14. * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
  15. *
  16. * Scaled math optimizations by Thomas Gleixner
  17. * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
  18. *
  19. * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
  20. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  21. */
  22. #include <linux/latencytop.h>
  23. #include <linux/sched.h>
  24. #include <linux/cpumask.h>
  25. #include <linux/slab.h>
  26. #include <linux/profile.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/mempolicy.h>
  29. #include <linux/migrate.h>
  30. #include <linux/task_work.h>
  31. #include <trace/events/sched.h>
  32. #include "sched.h"
  33. /*
  34. * Targeted preemption latency for CPU-bound tasks:
  35. * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
  36. *
  37. * NOTE: this latency value is not the same as the concept of
  38. * 'timeslice length' - timeslices in CFS are of variable length
  39. * and have no persistent notion like in traditional, time-slice
  40. * based scheduling concepts.
  41. *
  42. * (to see the precise effective timeslice length of your workload,
  43. * run vmstat and monitor the context-switches (cs) field)
  44. */
  45. unsigned int sysctl_sched_latency = 6000000ULL;
  46. unsigned int normalized_sysctl_sched_latency = 6000000ULL;
  47. /*
  48. * The initial- and re-scaling of tunables is configurable
  49. * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
  50. *
  51. * Options are:
  52. * SCHED_TUNABLESCALING_NONE - unscaled, always *1
  53. * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
  54. * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
  55. */
  56. enum sched_tunable_scaling sysctl_sched_tunable_scaling
  57. = SCHED_TUNABLESCALING_LOG;
  58. /*
  59. * Minimal preemption granularity for CPU-bound tasks:
  60. * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
  61. */
  62. unsigned int sysctl_sched_min_granularity = 750000ULL;
  63. unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
  64. /*
  65. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  66. */
  67. static unsigned int sched_nr_latency = 8;
  68. /*
  69. * After fork, child runs first. If set to 0 (default) then
  70. * parent will (try to) run first.
  71. */
  72. unsigned int sysctl_sched_child_runs_first __read_mostly;
  73. /*
  74. * SCHED_OTHER wake-up granularity.
  75. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  76. *
  77. * This option delays the preemption effects of decoupled workloads
  78. * and reduces their over-scheduling. Synchronous workloads will still
  79. * have immediate wakeup/sleep latencies.
  80. */
  81. unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
  82. unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
  83. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  84. /*
  85. * The exponential sliding window over which load is averaged for shares
  86. * distribution.
  87. * (default: 10msec)
  88. */
  89. unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
  90. #ifdef CONFIG_CFS_BANDWIDTH
  91. /*
  92. * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
  93. * each time a cfs_rq requests quota.
  94. *
  95. * Note: in the case that the slice exceeds the runtime remaining (either due
  96. * to consumption or the quota being specified to be smaller than the slice)
  97. * we will always only issue the remaining available time.
  98. *
  99. * default: 5 msec, units: microseconds
  100. */
  101. unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
  102. #endif
  103. /*
  104. * Increase the granularity value when there are more CPUs,
  105. * because with more CPUs the 'effective latency' as visible
  106. * to users decreases. But the relationship is not linear,
  107. * so pick a second-best guess by going with the log2 of the
  108. * number of CPUs.
  109. *
  110. * This idea comes from the SD scheduler of Con Kolivas:
  111. */
  112. static int get_update_sysctl_factor(void)
  113. {
  114. unsigned int cpus = min_t(int, num_online_cpus(), 8);
  115. unsigned int factor;
  116. switch (sysctl_sched_tunable_scaling) {
  117. case SCHED_TUNABLESCALING_NONE:
  118. factor = 1;
  119. break;
  120. case SCHED_TUNABLESCALING_LINEAR:
  121. factor = cpus;
  122. break;
  123. case SCHED_TUNABLESCALING_LOG:
  124. default:
  125. factor = 1 + ilog2(cpus);
  126. break;
  127. }
  128. return factor;
  129. }
  130. static void update_sysctl(void)
  131. {
  132. unsigned int factor = get_update_sysctl_factor();
  133. #define SET_SYSCTL(name) \
  134. (sysctl_##name = (factor) * normalized_sysctl_##name)
  135. SET_SYSCTL(sched_min_granularity);
  136. SET_SYSCTL(sched_latency);
  137. SET_SYSCTL(sched_wakeup_granularity);
  138. #undef SET_SYSCTL
  139. }
  140. void sched_init_granularity(void)
  141. {
  142. update_sysctl();
  143. }
  144. #if BITS_PER_LONG == 32
  145. # define WMULT_CONST (~0UL)
  146. #else
  147. # define WMULT_CONST (1UL << 32)
  148. #endif
  149. #define WMULT_SHIFT 32
  150. /*
  151. * Shift right and round:
  152. */
  153. #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
  154. /*
  155. * delta *= weight / lw
  156. */
  157. static unsigned long
  158. calc_delta_mine(unsigned long delta_exec, unsigned long weight,
  159. struct load_weight *lw)
  160. {
  161. u64 tmp;
  162. /*
  163. * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
  164. * entities since MIN_SHARES = 2. Treat weight as 1 if less than
  165. * 2^SCHED_LOAD_RESOLUTION.
  166. */
  167. if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
  168. tmp = (u64)delta_exec * scale_load_down(weight);
  169. else
  170. tmp = (u64)delta_exec;
  171. if (!lw->inv_weight) {
  172. unsigned long w = scale_load_down(lw->weight);
  173. if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
  174. lw->inv_weight = 1;
  175. else if (unlikely(!w))
  176. lw->inv_weight = WMULT_CONST;
  177. else
  178. lw->inv_weight = WMULT_CONST / w;
  179. }
  180. /*
  181. * Check whether we'd overflow the 64-bit multiplication:
  182. */
  183. if (unlikely(tmp > WMULT_CONST))
  184. tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
  185. WMULT_SHIFT/2);
  186. else
  187. tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
  188. return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
  189. }
  190. const struct sched_class fair_sched_class;
  191. /**************************************************************
  192. * CFS operations on generic schedulable entities:
  193. */
  194. #ifdef CONFIG_FAIR_GROUP_SCHED
  195. /* cpu runqueue to which this cfs_rq is attached */
  196. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  197. {
  198. return cfs_rq->rq;
  199. }
  200. /* An entity is a task if it doesn't "own" a runqueue */
  201. #define entity_is_task(se) (!se->my_q)
  202. static inline struct task_struct *task_of(struct sched_entity *se)
  203. {
  204. #ifdef CONFIG_SCHED_DEBUG
  205. WARN_ON_ONCE(!entity_is_task(se));
  206. #endif
  207. return container_of(se, struct task_struct, se);
  208. }
  209. /* Walk up scheduling entities hierarchy */
  210. #define for_each_sched_entity(se) \
  211. for (; se; se = se->parent)
  212. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  213. {
  214. return p->se.cfs_rq;
  215. }
  216. /* runqueue on which this entity is (to be) queued */
  217. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  218. {
  219. return se->cfs_rq;
  220. }
  221. /* runqueue "owned" by this group */
  222. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  223. {
  224. return grp->my_q;
  225. }
  226. static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
  227. int force_update);
  228. static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  229. {
  230. if (!cfs_rq->on_list) {
  231. /*
  232. * Ensure we either appear before our parent (if already
  233. * enqueued) or force our parent to appear after us when it is
  234. * enqueued. The fact that we always enqueue bottom-up
  235. * reduces this to two cases.
  236. */
  237. if (cfs_rq->tg->parent &&
  238. cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
  239. list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
  240. &rq_of(cfs_rq)->leaf_cfs_rq_list);
  241. } else {
  242. list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
  243. &rq_of(cfs_rq)->leaf_cfs_rq_list);
  244. }
  245. cfs_rq->on_list = 1;
  246. /* We should have no load, but we need to update last_decay. */
  247. update_cfs_rq_blocked_load(cfs_rq, 0);
  248. }
  249. }
  250. static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  251. {
  252. if (cfs_rq->on_list) {
  253. list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
  254. cfs_rq->on_list = 0;
  255. }
  256. }
  257. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  258. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  259. list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  260. /* Do the two (enqueued) entities belong to the same group ? */
  261. static inline int
  262. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  263. {
  264. if (se->cfs_rq == pse->cfs_rq)
  265. return 1;
  266. return 0;
  267. }
  268. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  269. {
  270. return se->parent;
  271. }
  272. /* return depth at which a sched entity is present in the hierarchy */
  273. static inline int depth_se(struct sched_entity *se)
  274. {
  275. int depth = 0;
  276. for_each_sched_entity(se)
  277. depth++;
  278. return depth;
  279. }
  280. static void
  281. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  282. {
  283. int se_depth, pse_depth;
  284. /*
  285. * preemption test can be made between sibling entities who are in the
  286. * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
  287. * both tasks until we find their ancestors who are siblings of common
  288. * parent.
  289. */
  290. /* First walk up until both entities are at same depth */
  291. se_depth = depth_se(*se);
  292. pse_depth = depth_se(*pse);
  293. while (se_depth > pse_depth) {
  294. se_depth--;
  295. *se = parent_entity(*se);
  296. }
  297. while (pse_depth > se_depth) {
  298. pse_depth--;
  299. *pse = parent_entity(*pse);
  300. }
  301. while (!is_same_group(*se, *pse)) {
  302. *se = parent_entity(*se);
  303. *pse = parent_entity(*pse);
  304. }
  305. }
  306. #else /* !CONFIG_FAIR_GROUP_SCHED */
  307. static inline struct task_struct *task_of(struct sched_entity *se)
  308. {
  309. return container_of(se, struct task_struct, se);
  310. }
  311. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  312. {
  313. return container_of(cfs_rq, struct rq, cfs);
  314. }
  315. #define entity_is_task(se) 1
  316. #define for_each_sched_entity(se) \
  317. for (; se; se = NULL)
  318. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  319. {
  320. return &task_rq(p)->cfs;
  321. }
  322. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  323. {
  324. struct task_struct *p = task_of(se);
  325. struct rq *rq = task_rq(p);
  326. return &rq->cfs;
  327. }
  328. /* runqueue "owned" by this group */
  329. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  330. {
  331. return NULL;
  332. }
  333. static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  334. {
  335. }
  336. static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  337. {
  338. }
  339. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  340. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  341. static inline int
  342. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  343. {
  344. return 1;
  345. }
  346. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  347. {
  348. return NULL;
  349. }
  350. static inline void
  351. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  352. {
  353. }
  354. #endif /* CONFIG_FAIR_GROUP_SCHED */
  355. static __always_inline
  356. void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
  357. /**************************************************************
  358. * Scheduling class tree data structure manipulation methods:
  359. */
  360. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  361. {
  362. s64 delta = (s64)(vruntime - min_vruntime);
  363. if (delta > 0)
  364. min_vruntime = vruntime;
  365. return min_vruntime;
  366. }
  367. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  368. {
  369. s64 delta = (s64)(vruntime - min_vruntime);
  370. if (delta < 0)
  371. min_vruntime = vruntime;
  372. return min_vruntime;
  373. }
  374. static inline int entity_before(struct sched_entity *a,
  375. struct sched_entity *b)
  376. {
  377. return (s64)(a->vruntime - b->vruntime) < 0;
  378. }
  379. static void update_min_vruntime(struct cfs_rq *cfs_rq)
  380. {
  381. u64 vruntime = cfs_rq->min_vruntime;
  382. if (cfs_rq->curr)
  383. vruntime = cfs_rq->curr->vruntime;
  384. if (cfs_rq->rb_leftmost) {
  385. struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
  386. struct sched_entity,
  387. run_node);
  388. if (!cfs_rq->curr)
  389. vruntime = se->vruntime;
  390. else
  391. vruntime = min_vruntime(vruntime, se->vruntime);
  392. }
  393. cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
  394. #ifndef CONFIG_64BIT
  395. smp_wmb();
  396. cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
  397. #endif
  398. }
  399. /*
  400. * Enqueue an entity into the rb-tree:
  401. */
  402. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  403. {
  404. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  405. struct rb_node *parent = NULL;
  406. struct sched_entity *entry;
  407. int leftmost = 1;
  408. /*
  409. * Find the right place in the rbtree:
  410. */
  411. while (*link) {
  412. parent = *link;
  413. entry = rb_entry(parent, struct sched_entity, run_node);
  414. /*
  415. * We dont care about collisions. Nodes with
  416. * the same key stay together.
  417. */
  418. if (entity_before(se, entry)) {
  419. link = &parent->rb_left;
  420. } else {
  421. link = &parent->rb_right;
  422. leftmost = 0;
  423. }
  424. }
  425. /*
  426. * Maintain a cache of leftmost tree entries (it is frequently
  427. * used):
  428. */
  429. if (leftmost)
  430. cfs_rq->rb_leftmost = &se->run_node;
  431. rb_link_node(&se->run_node, parent, link);
  432. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  433. }
  434. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  435. {
  436. if (cfs_rq->rb_leftmost == &se->run_node) {
  437. struct rb_node *next_node;
  438. next_node = rb_next(&se->run_node);
  439. cfs_rq->rb_leftmost = next_node;
  440. }
  441. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  442. }
  443. struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
  444. {
  445. struct rb_node *left = cfs_rq->rb_leftmost;
  446. if (!left)
  447. return NULL;
  448. return rb_entry(left, struct sched_entity, run_node);
  449. }
  450. static struct sched_entity *__pick_next_entity(struct sched_entity *se)
  451. {
  452. struct rb_node *next = rb_next(&se->run_node);
  453. if (!next)
  454. return NULL;
  455. return rb_entry(next, struct sched_entity, run_node);
  456. }
  457. #ifdef CONFIG_SCHED_DEBUG
  458. struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  459. {
  460. struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
  461. if (!last)
  462. return NULL;
  463. return rb_entry(last, struct sched_entity, run_node);
  464. }
  465. /**************************************************************
  466. * Scheduling class statistics methods:
  467. */
  468. int sched_proc_update_handler(struct ctl_table *table, int write,
  469. void __user *buffer, size_t *lenp,
  470. loff_t *ppos)
  471. {
  472. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  473. int factor = get_update_sysctl_factor();
  474. if (ret || !write)
  475. return ret;
  476. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  477. sysctl_sched_min_granularity);
  478. #define WRT_SYSCTL(name) \
  479. (normalized_sysctl_##name = sysctl_##name / (factor))
  480. WRT_SYSCTL(sched_min_granularity);
  481. WRT_SYSCTL(sched_latency);
  482. WRT_SYSCTL(sched_wakeup_granularity);
  483. #undef WRT_SYSCTL
  484. return 0;
  485. }
  486. #endif
  487. /*
  488. * delta /= w
  489. */
  490. static inline unsigned long
  491. calc_delta_fair(unsigned long delta, struct sched_entity *se)
  492. {
  493. if (unlikely(se->load.weight != NICE_0_LOAD))
  494. delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
  495. return delta;
  496. }
  497. /*
  498. * The idea is to set a period in which each task runs once.
  499. *
  500. * When there are too many tasks (sched_nr_latency) we have to stretch
  501. * this period because otherwise the slices get too small.
  502. *
  503. * p = (nr <= nl) ? l : l*nr/nl
  504. */
  505. static u64 __sched_period(unsigned long nr_running)
  506. {
  507. u64 period = sysctl_sched_latency;
  508. unsigned long nr_latency = sched_nr_latency;
  509. if (unlikely(nr_running > nr_latency)) {
  510. period = sysctl_sched_min_granularity;
  511. period *= nr_running;
  512. }
  513. return period;
  514. }
  515. /*
  516. * We calculate the wall-time slice from the period by taking a part
  517. * proportional to the weight.
  518. *
  519. * s = p*P[w/rw]
  520. */
  521. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  522. {
  523. u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
  524. for_each_sched_entity(se) {
  525. struct load_weight *load;
  526. struct load_weight lw;
  527. cfs_rq = cfs_rq_of(se);
  528. load = &cfs_rq->load;
  529. if (unlikely(!se->on_rq)) {
  530. lw = cfs_rq->load;
  531. update_load_add(&lw, se->load.weight);
  532. load = &lw;
  533. }
  534. slice = calc_delta_mine(slice, se->load.weight, load);
  535. }
  536. return slice;
  537. }
  538. /*
  539. * We calculate the vruntime slice of a to be inserted task
  540. *
  541. * vs = s/w
  542. */
  543. static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  544. {
  545. return calc_delta_fair(sched_slice(cfs_rq, se), se);
  546. }
  547. /*
  548. * Update the current task's runtime statistics. Skip current tasks that
  549. * are not in our scheduling class.
  550. */
  551. static inline void
  552. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  553. unsigned long delta_exec)
  554. {
  555. unsigned long delta_exec_weighted;
  556. schedstat_set(curr->statistics.exec_max,
  557. max((u64)delta_exec, curr->statistics.exec_max));
  558. curr->sum_exec_runtime += delta_exec;
  559. schedstat_add(cfs_rq, exec_clock, delta_exec);
  560. delta_exec_weighted = calc_delta_fair(delta_exec, curr);
  561. curr->vruntime += delta_exec_weighted;
  562. update_min_vruntime(cfs_rq);
  563. }
  564. static void update_curr(struct cfs_rq *cfs_rq)
  565. {
  566. struct sched_entity *curr = cfs_rq->curr;
  567. u64 now = rq_of(cfs_rq)->clock_task;
  568. unsigned long delta_exec;
  569. if (unlikely(!curr))
  570. return;
  571. /*
  572. * Get the amount of time the current task was running
  573. * since the last time we changed load (this cannot
  574. * overflow on 32 bits):
  575. */
  576. delta_exec = (unsigned long)(now - curr->exec_start);
  577. if (!delta_exec)
  578. return;
  579. __update_curr(cfs_rq, curr, delta_exec);
  580. curr->exec_start = now;
  581. if (entity_is_task(curr)) {
  582. struct task_struct *curtask = task_of(curr);
  583. trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
  584. cpuacct_charge(curtask, delta_exec);
  585. account_group_exec_runtime(curtask, delta_exec);
  586. }
  587. account_cfs_rq_runtime(cfs_rq, delta_exec);
  588. }
  589. static inline void
  590. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  591. {
  592. schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
  593. }
  594. /*
  595. * Task is being enqueued - update stats:
  596. */
  597. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  598. {
  599. /*
  600. * Are we enqueueing a waiting task? (for current tasks
  601. * a dequeue/enqueue event is a NOP)
  602. */
  603. if (se != cfs_rq->curr)
  604. update_stats_wait_start(cfs_rq, se);
  605. }
  606. static void
  607. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  608. {
  609. schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
  610. rq_of(cfs_rq)->clock - se->statistics.wait_start));
  611. schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
  612. schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
  613. rq_of(cfs_rq)->clock - se->statistics.wait_start);
  614. #ifdef CONFIG_SCHEDSTATS
  615. if (entity_is_task(se)) {
  616. trace_sched_stat_wait(task_of(se),
  617. rq_of(cfs_rq)->clock - se->statistics.wait_start);
  618. }
  619. #endif
  620. schedstat_set(se->statistics.wait_start, 0);
  621. }
  622. static inline void
  623. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  624. {
  625. /*
  626. * Mark the end of the wait period if dequeueing a
  627. * waiting task:
  628. */
  629. if (se != cfs_rq->curr)
  630. update_stats_wait_end(cfs_rq, se);
  631. }
  632. /*
  633. * We are picking a new current task - update its stats:
  634. */
  635. static inline void
  636. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  637. {
  638. /*
  639. * We are starting a new run period:
  640. */
  641. se->exec_start = rq_of(cfs_rq)->clock_task;
  642. }
  643. /**************************************************
  644. * Scheduling class queueing methods:
  645. */
  646. #ifdef CONFIG_NUMA_BALANCING
  647. /*
  648. * numa task sample period in ms
  649. */
  650. unsigned int sysctl_numa_balancing_scan_period_min = 100;
  651. unsigned int sysctl_numa_balancing_scan_period_max = 100*50;
  652. unsigned int sysctl_numa_balancing_scan_period_reset = 100*600;
  653. /* Portion of address space to scan in MB */
  654. unsigned int sysctl_numa_balancing_scan_size = 256;
  655. /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
  656. unsigned int sysctl_numa_balancing_scan_delay = 1000;
  657. static void task_numa_placement(struct task_struct *p)
  658. {
  659. int seq;
  660. if (!p->mm) /* for example, ksmd faulting in a user's mm */
  661. return;
  662. seq = ACCESS_ONCE(p->mm->numa_scan_seq);
  663. if (p->numa_scan_seq == seq)
  664. return;
  665. p->numa_scan_seq = seq;
  666. /* FIXME: Scheduling placement policy hints go here */
  667. }
  668. /*
  669. * Got a PROT_NONE fault for a page on @node.
  670. */
  671. void task_numa_fault(int node, int pages, bool migrated)
  672. {
  673. struct task_struct *p = current;
  674. if (!sched_feat_numa(NUMA))
  675. return;
  676. /* FIXME: Allocate task-specific structure for placement policy here */
  677. /*
  678. * If pages are properly placed (did not migrate) then scan slower.
  679. * This is reset periodically in case of phase changes
  680. */
  681. if (!migrated)
  682. p->numa_scan_period = min(sysctl_numa_balancing_scan_period_max,
  683. p->numa_scan_period + jiffies_to_msecs(10));
  684. task_numa_placement(p);
  685. }
  686. static void reset_ptenuma_scan(struct task_struct *p)
  687. {
  688. ACCESS_ONCE(p->mm->numa_scan_seq)++;
  689. p->mm->numa_scan_offset = 0;
  690. }
  691. /*
  692. * The expensive part of numa migration is done from task_work context.
  693. * Triggered from task_tick_numa().
  694. */
  695. void task_numa_work(struct callback_head *work)
  696. {
  697. unsigned long migrate, next_scan, now = jiffies;
  698. struct task_struct *p = current;
  699. struct mm_struct *mm = p->mm;
  700. struct vm_area_struct *vma;
  701. unsigned long start, end;
  702. long pages;
  703. WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
  704. work->next = work; /* protect against double add */
  705. /*
  706. * Who cares about NUMA placement when they're dying.
  707. *
  708. * NOTE: make sure not to dereference p->mm before this check,
  709. * exit_task_work() happens _after_ exit_mm() so we could be called
  710. * without p->mm even though we still had it when we enqueued this
  711. * work.
  712. */
  713. if (p->flags & PF_EXITING)
  714. return;
  715. /*
  716. * We do not care about task placement until a task runs on a node
  717. * other than the first one used by the address space. This is
  718. * largely because migrations are driven by what CPU the task
  719. * is running on. If it's never scheduled on another node, it'll
  720. * not migrate so why bother trapping the fault.
  721. */
  722. if (mm->first_nid == NUMA_PTE_SCAN_INIT)
  723. mm->first_nid = numa_node_id();
  724. if (mm->first_nid != NUMA_PTE_SCAN_ACTIVE) {
  725. /* Are we running on a new node yet? */
  726. if (numa_node_id() == mm->first_nid &&
  727. !sched_feat_numa(NUMA_FORCE))
  728. return;
  729. mm->first_nid = NUMA_PTE_SCAN_ACTIVE;
  730. }
  731. /*
  732. * Reset the scan period if enough time has gone by. Objective is that
  733. * scanning will be reduced if pages are properly placed. As tasks
  734. * can enter different phases this needs to be re-examined. Lacking
  735. * proper tracking of reference behaviour, this blunt hammer is used.
  736. */
  737. migrate = mm->numa_next_reset;
  738. if (time_after(now, migrate)) {
  739. p->numa_scan_period = sysctl_numa_balancing_scan_period_min;
  740. next_scan = now + msecs_to_jiffies(sysctl_numa_balancing_scan_period_reset);
  741. xchg(&mm->numa_next_reset, next_scan);
  742. }
  743. /*
  744. * Enforce maximal scan/migration frequency..
  745. */
  746. migrate = mm->numa_next_scan;
  747. if (time_before(now, migrate))
  748. return;
  749. if (p->numa_scan_period == 0)
  750. p->numa_scan_period = sysctl_numa_balancing_scan_period_min;
  751. next_scan = now + msecs_to_jiffies(p->numa_scan_period);
  752. if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
  753. return;
  754. /*
  755. * Do not set pte_numa if the current running node is rate-limited.
  756. * This loses statistics on the fault but if we are unwilling to
  757. * migrate to this node, it is less likely we can do useful work
  758. */
  759. if (migrate_ratelimited(numa_node_id()))
  760. return;
  761. start = mm->numa_scan_offset;
  762. pages = sysctl_numa_balancing_scan_size;
  763. pages <<= 20 - PAGE_SHIFT; /* MB in pages */
  764. if (!pages)
  765. return;
  766. down_read(&mm->mmap_sem);
  767. vma = find_vma(mm, start);
  768. if (!vma) {
  769. reset_ptenuma_scan(p);
  770. start = 0;
  771. vma = mm->mmap;
  772. }
  773. for (; vma; vma = vma->vm_next) {
  774. if (!vma_migratable(vma))
  775. continue;
  776. /* Skip small VMAs. They are not likely to be of relevance */
  777. if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
  778. continue;
  779. do {
  780. start = max(start, vma->vm_start);
  781. end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
  782. end = min(end, vma->vm_end);
  783. pages -= change_prot_numa(vma, start, end);
  784. start = end;
  785. if (pages <= 0)
  786. goto out;
  787. } while (end != vma->vm_end);
  788. }
  789. out:
  790. /*
  791. * It is possible to reach the end of the VMA list but the last few VMAs are
  792. * not guaranteed to the vma_migratable. If they are not, we would find the
  793. * !migratable VMA on the next scan but not reset the scanner to the start
  794. * so check it now.
  795. */
  796. if (vma)
  797. mm->numa_scan_offset = start;
  798. else
  799. reset_ptenuma_scan(p);
  800. up_read(&mm->mmap_sem);
  801. }
  802. /*
  803. * Drive the periodic memory faults..
  804. */
  805. void task_tick_numa(struct rq *rq, struct task_struct *curr)
  806. {
  807. struct callback_head *work = &curr->numa_work;
  808. u64 period, now;
  809. /*
  810. * We don't care about NUMA placement if we don't have memory.
  811. */
  812. if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
  813. return;
  814. /*
  815. * Using runtime rather than walltime has the dual advantage that
  816. * we (mostly) drive the selection from busy threads and that the
  817. * task needs to have done some actual work before we bother with
  818. * NUMA placement.
  819. */
  820. now = curr->se.sum_exec_runtime;
  821. period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
  822. if (now - curr->node_stamp > period) {
  823. if (!curr->node_stamp)
  824. curr->numa_scan_period = sysctl_numa_balancing_scan_period_min;
  825. curr->node_stamp = now;
  826. if (!time_before(jiffies, curr->mm->numa_next_scan)) {
  827. init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
  828. task_work_add(curr, work, true);
  829. }
  830. }
  831. }
  832. #else
  833. static void task_tick_numa(struct rq *rq, struct task_struct *curr)
  834. {
  835. }
  836. #endif /* CONFIG_NUMA_BALANCING */
  837. static void
  838. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  839. {
  840. update_load_add(&cfs_rq->load, se->load.weight);
  841. if (!parent_entity(se))
  842. update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
  843. #ifdef CONFIG_SMP
  844. if (entity_is_task(se))
  845. list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
  846. #endif
  847. cfs_rq->nr_running++;
  848. }
  849. static void
  850. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  851. {
  852. update_load_sub(&cfs_rq->load, se->load.weight);
  853. if (!parent_entity(se))
  854. update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
  855. if (entity_is_task(se))
  856. list_del_init(&se->group_node);
  857. cfs_rq->nr_running--;
  858. }
  859. #ifdef CONFIG_FAIR_GROUP_SCHED
  860. # ifdef CONFIG_SMP
  861. static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
  862. {
  863. long tg_weight;
  864. /*
  865. * Use this CPU's actual weight instead of the last load_contribution
  866. * to gain a more accurate current total weight. See
  867. * update_cfs_rq_load_contribution().
  868. */
  869. tg_weight = atomic64_read(&tg->load_avg);
  870. tg_weight -= cfs_rq->tg_load_contrib;
  871. tg_weight += cfs_rq->load.weight;
  872. return tg_weight;
  873. }
  874. static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
  875. {
  876. long tg_weight, load, shares;
  877. tg_weight = calc_tg_weight(tg, cfs_rq);
  878. load = cfs_rq->load.weight;
  879. shares = (tg->shares * load);
  880. if (tg_weight)
  881. shares /= tg_weight;
  882. if (shares < MIN_SHARES)
  883. shares = MIN_SHARES;
  884. if (shares > tg->shares)
  885. shares = tg->shares;
  886. return shares;
  887. }
  888. # else /* CONFIG_SMP */
  889. static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
  890. {
  891. return tg->shares;
  892. }
  893. # endif /* CONFIG_SMP */
  894. static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
  895. unsigned long weight)
  896. {
  897. if (se->on_rq) {
  898. /* commit outstanding execution time */
  899. if (cfs_rq->curr == se)
  900. update_curr(cfs_rq);
  901. account_entity_dequeue(cfs_rq, se);
  902. }
  903. update_load_set(&se->load, weight);
  904. if (se->on_rq)
  905. account_entity_enqueue(cfs_rq, se);
  906. }
  907. static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
  908. static void update_cfs_shares(struct cfs_rq *cfs_rq)
  909. {
  910. struct task_group *tg;
  911. struct sched_entity *se;
  912. long shares;
  913. tg = cfs_rq->tg;
  914. se = tg->se[cpu_of(rq_of(cfs_rq))];
  915. if (!se || throttled_hierarchy(cfs_rq))
  916. return;
  917. #ifndef CONFIG_SMP
  918. if (likely(se->load.weight == tg->shares))
  919. return;
  920. #endif
  921. shares = calc_cfs_shares(cfs_rq, tg);
  922. reweight_entity(cfs_rq_of(se), se, shares);
  923. }
  924. #else /* CONFIG_FAIR_GROUP_SCHED */
  925. static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
  926. {
  927. }
  928. #endif /* CONFIG_FAIR_GROUP_SCHED */
  929. /* Only depends on SMP, FAIR_GROUP_SCHED may be removed when useful in lb */
  930. #if defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)
  931. /*
  932. * We choose a half-life close to 1 scheduling period.
  933. * Note: The tables below are dependent on this value.
  934. */
  935. #define LOAD_AVG_PERIOD 32
  936. #define LOAD_AVG_MAX 47742 /* maximum possible load avg */
  937. #define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_MAX_AVG */
  938. /* Precomputed fixed inverse multiplies for multiplication by y^n */
  939. static const u32 runnable_avg_yN_inv[] = {
  940. 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6,
  941. 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85,
  942. 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581,
  943. 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9,
  944. 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80,
  945. 0x85aac367, 0x82cd8698,
  946. };
  947. /*
  948. * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent
  949. * over-estimates when re-combining.
  950. */
  951. static const u32 runnable_avg_yN_sum[] = {
  952. 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103,
  953. 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082,
  954. 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371,
  955. };
  956. /*
  957. * Approximate:
  958. * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
  959. */
  960. static __always_inline u64 decay_load(u64 val, u64 n)
  961. {
  962. unsigned int local_n;
  963. if (!n)
  964. return val;
  965. else if (unlikely(n > LOAD_AVG_PERIOD * 63))
  966. return 0;
  967. /* after bounds checking we can collapse to 32-bit */
  968. local_n = n;
  969. /*
  970. * As y^PERIOD = 1/2, we can combine
  971. * y^n = 1/2^(n/PERIOD) * k^(n%PERIOD)
  972. * With a look-up table which covers k^n (n<PERIOD)
  973. *
  974. * To achieve constant time decay_load.
  975. */
  976. if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
  977. val >>= local_n / LOAD_AVG_PERIOD;
  978. local_n %= LOAD_AVG_PERIOD;
  979. }
  980. val *= runnable_avg_yN_inv[local_n];
  981. /* We don't use SRR here since we always want to round down. */
  982. return val >> 32;
  983. }
  984. /*
  985. * For updates fully spanning n periods, the contribution to runnable
  986. * average will be: \Sum 1024*y^n
  987. *
  988. * We can compute this reasonably efficiently by combining:
  989. * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD}
  990. */
  991. static u32 __compute_runnable_contrib(u64 n)
  992. {
  993. u32 contrib = 0;
  994. if (likely(n <= LOAD_AVG_PERIOD))
  995. return runnable_avg_yN_sum[n];
  996. else if (unlikely(n >= LOAD_AVG_MAX_N))
  997. return LOAD_AVG_MAX;
  998. /* Compute \Sum k^n combining precomputed values for k^i, \Sum k^j */
  999. do {
  1000. contrib /= 2; /* y^LOAD_AVG_PERIOD = 1/2 */
  1001. contrib += runnable_avg_yN_sum[LOAD_AVG_PERIOD];
  1002. n -= LOAD_AVG_PERIOD;
  1003. } while (n > LOAD_AVG_PERIOD);
  1004. contrib = decay_load(contrib, n);
  1005. return contrib + runnable_avg_yN_sum[n];
  1006. }
  1007. /*
  1008. * We can represent the historical contribution to runnable average as the
  1009. * coefficients of a geometric series. To do this we sub-divide our runnable
  1010. * history into segments of approximately 1ms (1024us); label the segment that
  1011. * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
  1012. *
  1013. * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
  1014. * p0 p1 p2
  1015. * (now) (~1ms ago) (~2ms ago)
  1016. *
  1017. * Let u_i denote the fraction of p_i that the entity was runnable.
  1018. *
  1019. * We then designate the fractions u_i as our co-efficients, yielding the
  1020. * following representation of historical load:
  1021. * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
  1022. *
  1023. * We choose y based on the with of a reasonably scheduling period, fixing:
  1024. * y^32 = 0.5
  1025. *
  1026. * This means that the contribution to load ~32ms ago (u_32) will be weighted
  1027. * approximately half as much as the contribution to load within the last ms
  1028. * (u_0).
  1029. *
  1030. * When a period "rolls over" and we have new u_0`, multiplying the previous
  1031. * sum again by y is sufficient to update:
  1032. * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
  1033. * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
  1034. */
  1035. static __always_inline int __update_entity_runnable_avg(u64 now,
  1036. struct sched_avg *sa,
  1037. int runnable)
  1038. {
  1039. u64 delta, periods;
  1040. u32 runnable_contrib;
  1041. int delta_w, decayed = 0;
  1042. delta = now - sa->last_runnable_update;
  1043. /*
  1044. * This should only happen when time goes backwards, which it
  1045. * unfortunately does during sched clock init when we swap over to TSC.
  1046. */
  1047. if ((s64)delta < 0) {
  1048. sa->last_runnable_update = now;
  1049. return 0;
  1050. }
  1051. /*
  1052. * Use 1024ns as the unit of measurement since it's a reasonable
  1053. * approximation of 1us and fast to compute.
  1054. */
  1055. delta >>= 10;
  1056. if (!delta)
  1057. return 0;
  1058. sa->last_runnable_update = now;
  1059. /* delta_w is the amount already accumulated against our next period */
  1060. delta_w = sa->runnable_avg_period % 1024;
  1061. if (delta + delta_w >= 1024) {
  1062. /* period roll-over */
  1063. decayed = 1;
  1064. /*
  1065. * Now that we know we're crossing a period boundary, figure
  1066. * out how much from delta we need to complete the current
  1067. * period and accrue it.
  1068. */
  1069. delta_w = 1024 - delta_w;
  1070. if (runnable)
  1071. sa->runnable_avg_sum += delta_w;
  1072. sa->runnable_avg_period += delta_w;
  1073. delta -= delta_w;
  1074. /* Figure out how many additional periods this update spans */
  1075. periods = delta / 1024;
  1076. delta %= 1024;
  1077. sa->runnable_avg_sum = decay_load(sa->runnable_avg_sum,
  1078. periods + 1);
  1079. sa->runnable_avg_period = decay_load(sa->runnable_avg_period,
  1080. periods + 1);
  1081. /* Efficiently calculate \sum (1..n_period) 1024*y^i */
  1082. runnable_contrib = __compute_runnable_contrib(periods);
  1083. if (runnable)
  1084. sa->runnable_avg_sum += runnable_contrib;
  1085. sa->runnable_avg_period += runnable_contrib;
  1086. }
  1087. /* Remainder of delta accrued against u_0` */
  1088. if (runnable)
  1089. sa->runnable_avg_sum += delta;
  1090. sa->runnable_avg_period += delta;
  1091. return decayed;
  1092. }
  1093. /* Synchronize an entity's decay with its parenting cfs_rq.*/
  1094. static inline u64 __synchronize_entity_decay(struct sched_entity *se)
  1095. {
  1096. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1097. u64 decays = atomic64_read(&cfs_rq->decay_counter);
  1098. decays -= se->avg.decay_count;
  1099. if (!decays)
  1100. return 0;
  1101. se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
  1102. se->avg.decay_count = 0;
  1103. return decays;
  1104. }
  1105. #ifdef CONFIG_FAIR_GROUP_SCHED
  1106. static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
  1107. int force_update)
  1108. {
  1109. struct task_group *tg = cfs_rq->tg;
  1110. s64 tg_contrib;
  1111. tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg;
  1112. tg_contrib -= cfs_rq->tg_load_contrib;
  1113. if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) {
  1114. atomic64_add(tg_contrib, &tg->load_avg);
  1115. cfs_rq->tg_load_contrib += tg_contrib;
  1116. }
  1117. }
  1118. /*
  1119. * Aggregate cfs_rq runnable averages into an equivalent task_group
  1120. * representation for computing load contributions.
  1121. */
  1122. static inline void __update_tg_runnable_avg(struct sched_avg *sa,
  1123. struct cfs_rq *cfs_rq)
  1124. {
  1125. struct task_group *tg = cfs_rq->tg;
  1126. long contrib;
  1127. /* The fraction of a cpu used by this cfs_rq */
  1128. contrib = div_u64(sa->runnable_avg_sum << NICE_0_SHIFT,
  1129. sa->runnable_avg_period + 1);
  1130. contrib -= cfs_rq->tg_runnable_contrib;
  1131. if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) {
  1132. atomic_add(contrib, &tg->runnable_avg);
  1133. cfs_rq->tg_runnable_contrib += contrib;
  1134. }
  1135. }
  1136. static inline void __update_group_entity_contrib(struct sched_entity *se)
  1137. {
  1138. struct cfs_rq *cfs_rq = group_cfs_rq(se);
  1139. struct task_group *tg = cfs_rq->tg;
  1140. int runnable_avg;
  1141. u64 contrib;
  1142. contrib = cfs_rq->tg_load_contrib * tg->shares;
  1143. se->avg.load_avg_contrib = div64_u64(contrib,
  1144. atomic64_read(&tg->load_avg) + 1);
  1145. /*
  1146. * For group entities we need to compute a correction term in the case
  1147. * that they are consuming <1 cpu so that we would contribute the same
  1148. * load as a task of equal weight.
  1149. *
  1150. * Explicitly co-ordinating this measurement would be expensive, but
  1151. * fortunately the sum of each cpus contribution forms a usable
  1152. * lower-bound on the true value.
  1153. *
  1154. * Consider the aggregate of 2 contributions. Either they are disjoint
  1155. * (and the sum represents true value) or they are disjoint and we are
  1156. * understating by the aggregate of their overlap.
  1157. *
  1158. * Extending this to N cpus, for a given overlap, the maximum amount we
  1159. * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of
  1160. * cpus that overlap for this interval and w_i is the interval width.
  1161. *
  1162. * On a small machine; the first term is well-bounded which bounds the
  1163. * total error since w_i is a subset of the period. Whereas on a
  1164. * larger machine, while this first term can be larger, if w_i is the
  1165. * of consequential size guaranteed to see n_i*w_i quickly converge to
  1166. * our upper bound of 1-cpu.
  1167. */
  1168. runnable_avg = atomic_read(&tg->runnable_avg);
  1169. if (runnable_avg < NICE_0_LOAD) {
  1170. se->avg.load_avg_contrib *= runnable_avg;
  1171. se->avg.load_avg_contrib >>= NICE_0_SHIFT;
  1172. }
  1173. }
  1174. #else
  1175. static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
  1176. int force_update) {}
  1177. static inline void __update_tg_runnable_avg(struct sched_avg *sa,
  1178. struct cfs_rq *cfs_rq) {}
  1179. static inline void __update_group_entity_contrib(struct sched_entity *se) {}
  1180. #endif
  1181. static inline void __update_task_entity_contrib(struct sched_entity *se)
  1182. {
  1183. u32 contrib;
  1184. /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */
  1185. contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight);
  1186. contrib /= (se->avg.runnable_avg_period + 1);
  1187. se->avg.load_avg_contrib = scale_load(contrib);
  1188. }
  1189. /* Compute the current contribution to load_avg by se, return any delta */
  1190. static long __update_entity_load_avg_contrib(struct sched_entity *se)
  1191. {
  1192. long old_contrib = se->avg.load_avg_contrib;
  1193. if (entity_is_task(se)) {
  1194. __update_task_entity_contrib(se);
  1195. } else {
  1196. __update_tg_runnable_avg(&se->avg, group_cfs_rq(se));
  1197. __update_group_entity_contrib(se);
  1198. }
  1199. return se->avg.load_avg_contrib - old_contrib;
  1200. }
  1201. static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
  1202. long load_contrib)
  1203. {
  1204. if (likely(load_contrib < cfs_rq->blocked_load_avg))
  1205. cfs_rq->blocked_load_avg -= load_contrib;
  1206. else
  1207. cfs_rq->blocked_load_avg = 0;
  1208. }
  1209. static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
  1210. /* Update a sched_entity's runnable average */
  1211. static inline void update_entity_load_avg(struct sched_entity *se,
  1212. int update_cfs_rq)
  1213. {
  1214. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1215. long contrib_delta;
  1216. u64 now;
  1217. /*
  1218. * For a group entity we need to use their owned cfs_rq_clock_task() in
  1219. * case they are the parent of a throttled hierarchy.
  1220. */
  1221. if (entity_is_task(se))
  1222. now = cfs_rq_clock_task(cfs_rq);
  1223. else
  1224. now = cfs_rq_clock_task(group_cfs_rq(se));
  1225. if (!__update_entity_runnable_avg(now, &se->avg, se->on_rq))
  1226. return;
  1227. contrib_delta = __update_entity_load_avg_contrib(se);
  1228. if (!update_cfs_rq)
  1229. return;
  1230. if (se->on_rq)
  1231. cfs_rq->runnable_load_avg += contrib_delta;
  1232. else
  1233. subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
  1234. }
  1235. /*
  1236. * Decay the load contributed by all blocked children and account this so that
  1237. * their contribution may appropriately discounted when they wake up.
  1238. */
  1239. static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
  1240. {
  1241. u64 now = cfs_rq_clock_task(cfs_rq) >> 20;
  1242. u64 decays;
  1243. decays = now - cfs_rq->last_decay;
  1244. if (!decays && !force_update)
  1245. return;
  1246. if (atomic64_read(&cfs_rq->removed_load)) {
  1247. u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0);
  1248. subtract_blocked_load_contrib(cfs_rq, removed_load);
  1249. }
  1250. if (decays) {
  1251. cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
  1252. decays);
  1253. atomic64_add(decays, &cfs_rq->decay_counter);
  1254. cfs_rq->last_decay = now;
  1255. }
  1256. __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
  1257. }
  1258. static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
  1259. {
  1260. __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable);
  1261. __update_tg_runnable_avg(&rq->avg, &rq->cfs);
  1262. }
  1263. /* Add the load generated by se into cfs_rq's child load-average */
  1264. static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
  1265. struct sched_entity *se,
  1266. int wakeup)
  1267. {
  1268. /*
  1269. * We track migrations using entity decay_count <= 0, on a wake-up
  1270. * migration we use a negative decay count to track the remote decays
  1271. * accumulated while sleeping.
  1272. */
  1273. if (unlikely(se->avg.decay_count <= 0)) {
  1274. se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task;
  1275. if (se->avg.decay_count) {
  1276. /*
  1277. * In a wake-up migration we have to approximate the
  1278. * time sleeping. This is because we can't synchronize
  1279. * clock_task between the two cpus, and it is not
  1280. * guaranteed to be read-safe. Instead, we can
  1281. * approximate this using our carried decays, which are
  1282. * explicitly atomically readable.
  1283. */
  1284. se->avg.last_runnable_update -= (-se->avg.decay_count)
  1285. << 20;
  1286. update_entity_load_avg(se, 0);
  1287. /* Indicate that we're now synchronized and on-rq */
  1288. se->avg.decay_count = 0;
  1289. }
  1290. wakeup = 0;
  1291. } else {
  1292. __synchronize_entity_decay(se);
  1293. }
  1294. /* migrated tasks did not contribute to our blocked load */
  1295. if (wakeup) {
  1296. subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
  1297. update_entity_load_avg(se, 0);
  1298. }
  1299. cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
  1300. /* we force update consideration on load-balancer moves */
  1301. update_cfs_rq_blocked_load(cfs_rq, !wakeup);
  1302. }
  1303. /*
  1304. * Remove se's load from this cfs_rq child load-average, if the entity is
  1305. * transitioning to a blocked state we track its projected decay using
  1306. * blocked_load_avg.
  1307. */
  1308. static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
  1309. struct sched_entity *se,
  1310. int sleep)
  1311. {
  1312. update_entity_load_avg(se, 1);
  1313. /* we force update consideration on load-balancer moves */
  1314. update_cfs_rq_blocked_load(cfs_rq, !sleep);
  1315. cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
  1316. if (sleep) {
  1317. cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
  1318. se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
  1319. } /* migrations, e.g. sleep=0 leave decay_count == 0 */
  1320. }
  1321. #else
  1322. static inline void update_entity_load_avg(struct sched_entity *se,
  1323. int update_cfs_rq) {}
  1324. static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
  1325. static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
  1326. struct sched_entity *se,
  1327. int wakeup) {}
  1328. static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
  1329. struct sched_entity *se,
  1330. int sleep) {}
  1331. static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
  1332. int force_update) {}
  1333. #endif
  1334. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1335. {
  1336. #ifdef CONFIG_SCHEDSTATS
  1337. struct task_struct *tsk = NULL;
  1338. if (entity_is_task(se))
  1339. tsk = task_of(se);
  1340. if (se->statistics.sleep_start) {
  1341. u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
  1342. if ((s64)delta < 0)
  1343. delta = 0;
  1344. if (unlikely(delta > se->statistics.sleep_max))
  1345. se->statistics.sleep_max = delta;
  1346. se->statistics.sleep_start = 0;
  1347. se->statistics.sum_sleep_runtime += delta;
  1348. if (tsk) {
  1349. account_scheduler_latency(tsk, delta >> 10, 1);
  1350. trace_sched_stat_sleep(tsk, delta);
  1351. }
  1352. }
  1353. if (se->statistics.block_start) {
  1354. u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
  1355. if ((s64)delta < 0)
  1356. delta = 0;
  1357. if (unlikely(delta > se->statistics.block_max))
  1358. se->statistics.block_max = delta;
  1359. se->statistics.block_start = 0;
  1360. se->statistics.sum_sleep_runtime += delta;
  1361. if (tsk) {
  1362. if (tsk->in_iowait) {
  1363. se->statistics.iowait_sum += delta;
  1364. se->statistics.iowait_count++;
  1365. trace_sched_stat_iowait(tsk, delta);
  1366. }
  1367. trace_sched_stat_blocked(tsk, delta);
  1368. /*
  1369. * Blocking time is in units of nanosecs, so shift by
  1370. * 20 to get a milliseconds-range estimation of the
  1371. * amount of time that the task spent sleeping:
  1372. */
  1373. if (unlikely(prof_on == SLEEP_PROFILING)) {
  1374. profile_hits(SLEEP_PROFILING,
  1375. (void *)get_wchan(tsk),
  1376. delta >> 20);
  1377. }
  1378. account_scheduler_latency(tsk, delta >> 10, 0);
  1379. }
  1380. }
  1381. #endif
  1382. }
  1383. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1384. {
  1385. #ifdef CONFIG_SCHED_DEBUG
  1386. s64 d = se->vruntime - cfs_rq->min_vruntime;
  1387. if (d < 0)
  1388. d = -d;
  1389. if (d > 3*sysctl_sched_latency)
  1390. schedstat_inc(cfs_rq, nr_spread_over);
  1391. #endif
  1392. }
  1393. static void
  1394. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  1395. {
  1396. u64 vruntime = cfs_rq->min_vruntime;
  1397. /*
  1398. * The 'current' period is already promised to the current tasks,
  1399. * however the extra weight of the new task will slow them down a
  1400. * little, place the new task so that it fits in the slot that
  1401. * stays open at the end.
  1402. */
  1403. if (initial && sched_feat(START_DEBIT))
  1404. vruntime += sched_vslice(cfs_rq, se);
  1405. /* sleeps up to a single latency don't count. */
  1406. if (!initial) {
  1407. unsigned long thresh = sysctl_sched_latency;
  1408. /*
  1409. * Halve their sleep time's effect, to allow
  1410. * for a gentler effect of sleepers:
  1411. */
  1412. if (sched_feat(GENTLE_FAIR_SLEEPERS))
  1413. thresh >>= 1;
  1414. vruntime -= thresh;
  1415. }
  1416. /* ensure we never gain time by being placed backwards. */
  1417. se->vruntime = max_vruntime(se->vruntime, vruntime);
  1418. }
  1419. static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
  1420. static void
  1421. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
  1422. {
  1423. /*
  1424. * Update the normalized vruntime before updating min_vruntime
  1425. * through callig update_curr().
  1426. */
  1427. if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
  1428. se->vruntime += cfs_rq->min_vruntime;
  1429. /*
  1430. * Update run-time statistics of the 'current'.
  1431. */
  1432. update_curr(cfs_rq);
  1433. enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
  1434. account_entity_enqueue(cfs_rq, se);
  1435. update_cfs_shares(cfs_rq);
  1436. if (flags & ENQUEUE_WAKEUP) {
  1437. place_entity(cfs_rq, se, 0);
  1438. enqueue_sleeper(cfs_rq, se);
  1439. }
  1440. update_stats_enqueue(cfs_rq, se);
  1441. check_spread(cfs_rq, se);
  1442. if (se != cfs_rq->curr)
  1443. __enqueue_entity(cfs_rq, se);
  1444. se->on_rq = 1;
  1445. if (cfs_rq->nr_running == 1) {
  1446. list_add_leaf_cfs_rq(cfs_rq);
  1447. check_enqueue_throttle(cfs_rq);
  1448. }
  1449. }
  1450. static void __clear_buddies_last(struct sched_entity *se)
  1451. {
  1452. for_each_sched_entity(se) {
  1453. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1454. if (cfs_rq->last == se)
  1455. cfs_rq->last = NULL;
  1456. else
  1457. break;
  1458. }
  1459. }
  1460. static void __clear_buddies_next(struct sched_entity *se)
  1461. {
  1462. for_each_sched_entity(se) {
  1463. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1464. if (cfs_rq->next == se)
  1465. cfs_rq->next = NULL;
  1466. else
  1467. break;
  1468. }
  1469. }
  1470. static void __clear_buddies_skip(struct sched_entity *se)
  1471. {
  1472. for_each_sched_entity(se) {
  1473. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1474. if (cfs_rq->skip == se)
  1475. cfs_rq->skip = NULL;
  1476. else
  1477. break;
  1478. }
  1479. }
  1480. static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1481. {
  1482. if (cfs_rq->last == se)
  1483. __clear_buddies_last(se);
  1484. if (cfs_rq->next == se)
  1485. __clear_buddies_next(se);
  1486. if (cfs_rq->skip == se)
  1487. __clear_buddies_skip(se);
  1488. }
  1489. static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
  1490. static void
  1491. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
  1492. {
  1493. /*
  1494. * Update run-time statistics of the 'current'.
  1495. */
  1496. update_curr(cfs_rq);
  1497. dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
  1498. update_stats_dequeue(cfs_rq, se);
  1499. if (flags & DEQUEUE_SLEEP) {
  1500. #ifdef CONFIG_SCHEDSTATS
  1501. if (entity_is_task(se)) {
  1502. struct task_struct *tsk = task_of(se);
  1503. if (tsk->state & TASK_INTERRUPTIBLE)
  1504. se->statistics.sleep_start = rq_of(cfs_rq)->clock;
  1505. if (tsk->state & TASK_UNINTERRUPTIBLE)
  1506. se->statistics.block_start = rq_of(cfs_rq)->clock;
  1507. }
  1508. #endif
  1509. }
  1510. clear_buddies(cfs_rq, se);
  1511. if (se != cfs_rq->curr)
  1512. __dequeue_entity(cfs_rq, se);
  1513. se->on_rq = 0;
  1514. account_entity_dequeue(cfs_rq, se);
  1515. /*
  1516. * Normalize the entity after updating the min_vruntime because the
  1517. * update can refer to the ->curr item and we need to reflect this
  1518. * movement in our normalized position.
  1519. */
  1520. if (!(flags & DEQUEUE_SLEEP))
  1521. se->vruntime -= cfs_rq->min_vruntime;
  1522. /* return excess runtime on last dequeue */
  1523. return_cfs_rq_runtime(cfs_rq);
  1524. update_min_vruntime(cfs_rq);
  1525. update_cfs_shares(cfs_rq);
  1526. }
  1527. /*
  1528. * Preempt the current task with a newly woken task if needed:
  1529. */
  1530. static void
  1531. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  1532. {
  1533. unsigned long ideal_runtime, delta_exec;
  1534. struct sched_entity *se;
  1535. s64 delta;
  1536. ideal_runtime = sched_slice(cfs_rq, curr);
  1537. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  1538. if (delta_exec > ideal_runtime) {
  1539. resched_task(rq_of(cfs_rq)->curr);
  1540. /*
  1541. * The current task ran long enough, ensure it doesn't get
  1542. * re-elected due to buddy favours.
  1543. */
  1544. clear_buddies(cfs_rq, curr);
  1545. return;
  1546. }
  1547. /*
  1548. * Ensure that a task that missed wakeup preemption by a
  1549. * narrow margin doesn't have to wait for a full slice.
  1550. * This also mitigates buddy induced latencies under load.
  1551. */
  1552. if (delta_exec < sysctl_sched_min_granularity)
  1553. return;
  1554. se = __pick_first_entity(cfs_rq);
  1555. delta = curr->vruntime - se->vruntime;
  1556. if (delta < 0)
  1557. return;
  1558. if (delta > ideal_runtime)
  1559. resched_task(rq_of(cfs_rq)->curr);
  1560. }
  1561. static void
  1562. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1563. {
  1564. /* 'current' is not kept within the tree. */
  1565. if (se->on_rq) {
  1566. /*
  1567. * Any task has to be enqueued before it get to execute on
  1568. * a CPU. So account for the time it spent waiting on the
  1569. * runqueue.
  1570. */
  1571. update_stats_wait_end(cfs_rq, se);
  1572. __dequeue_entity(cfs_rq, se);
  1573. }
  1574. update_stats_curr_start(cfs_rq, se);
  1575. cfs_rq->curr = se;
  1576. #ifdef CONFIG_SCHEDSTATS
  1577. /*
  1578. * Track our maximum slice length, if the CPU's load is at
  1579. * least twice that of our own weight (i.e. dont track it
  1580. * when there are only lesser-weight tasks around):
  1581. */
  1582. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  1583. se->statistics.slice_max = max(se->statistics.slice_max,
  1584. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  1585. }
  1586. #endif
  1587. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  1588. }
  1589. static int
  1590. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
  1591. /*
  1592. * Pick the next process, keeping these things in mind, in this order:
  1593. * 1) keep things fair between processes/task groups
  1594. * 2) pick the "next" process, since someone really wants that to run
  1595. * 3) pick the "last" process, for cache locality
  1596. * 4) do not run the "skip" process, if something else is available
  1597. */
  1598. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  1599. {
  1600. struct sched_entity *se = __pick_first_entity(cfs_rq);
  1601. struct sched_entity *left = se;
  1602. /*
  1603. * Avoid running the skip buddy, if running something else can
  1604. * be done without getting too unfair.
  1605. */
  1606. if (cfs_rq->skip == se) {
  1607. struct sched_entity *second = __pick_next_entity(se);
  1608. if (second && wakeup_preempt_entity(second, left) < 1)
  1609. se = second;
  1610. }
  1611. /*
  1612. * Prefer last buddy, try to return the CPU to a preempted task.
  1613. */
  1614. if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
  1615. se = cfs_rq->last;
  1616. /*
  1617. * Someone really wants this to run. If it's not unfair, run it.
  1618. */
  1619. if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
  1620. se = cfs_rq->next;
  1621. clear_buddies(cfs_rq, se);
  1622. return se;
  1623. }
  1624. static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
  1625. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  1626. {
  1627. /*
  1628. * If still on the runqueue then deactivate_task()
  1629. * was not called and update_curr() has to be done:
  1630. */
  1631. if (prev->on_rq)
  1632. update_curr(cfs_rq);
  1633. /* throttle cfs_rqs exceeding runtime */
  1634. check_cfs_rq_runtime(cfs_rq);
  1635. check_spread(cfs_rq, prev);
  1636. if (prev->on_rq) {
  1637. update_stats_wait_start(cfs_rq, prev);
  1638. /* Put 'current' back into the tree. */
  1639. __enqueue_entity(cfs_rq, prev);
  1640. /* in !on_rq case, update occurred at dequeue */
  1641. update_entity_load_avg(prev, 1);
  1642. }
  1643. cfs_rq->curr = NULL;
  1644. }
  1645. static void
  1646. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  1647. {
  1648. /*
  1649. * Update run-time statistics of the 'current'.
  1650. */
  1651. update_curr(cfs_rq);
  1652. /*
  1653. * Ensure that runnable average is periodically updated.
  1654. */
  1655. update_entity_load_avg(curr, 1);
  1656. update_cfs_rq_blocked_load(cfs_rq, 1);
  1657. #ifdef CONFIG_SCHED_HRTICK
  1658. /*
  1659. * queued ticks are scheduled to match the slice, so don't bother
  1660. * validating it and just reschedule.
  1661. */
  1662. if (queued) {
  1663. resched_task(rq_of(cfs_rq)->curr);
  1664. return;
  1665. }
  1666. /*
  1667. * don't let the period tick interfere with the hrtick preemption
  1668. */
  1669. if (!sched_feat(DOUBLE_TICK) &&
  1670. hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  1671. return;
  1672. #endif
  1673. if (cfs_rq->nr_running > 1)
  1674. check_preempt_tick(cfs_rq, curr);
  1675. }
  1676. /**************************************************
  1677. * CFS bandwidth control machinery
  1678. */
  1679. #ifdef CONFIG_CFS_BANDWIDTH
  1680. #ifdef HAVE_JUMP_LABEL
  1681. static struct static_key __cfs_bandwidth_used;
  1682. static inline bool cfs_bandwidth_used(void)
  1683. {
  1684. return static_key_false(&__cfs_bandwidth_used);
  1685. }
  1686. void account_cfs_bandwidth_used(int enabled, int was_enabled)
  1687. {
  1688. /* only need to count groups transitioning between enabled/!enabled */
  1689. if (enabled && !was_enabled)
  1690. static_key_slow_inc(&__cfs_bandwidth_used);
  1691. else if (!enabled && was_enabled)
  1692. static_key_slow_dec(&__cfs_bandwidth_used);
  1693. }
  1694. #else /* HAVE_JUMP_LABEL */
  1695. static bool cfs_bandwidth_used(void)
  1696. {
  1697. return true;
  1698. }
  1699. void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
  1700. #endif /* HAVE_JUMP_LABEL */
  1701. /*
  1702. * default period for cfs group bandwidth.
  1703. * default: 0.1s, units: nanoseconds
  1704. */
  1705. static inline u64 default_cfs_period(void)
  1706. {
  1707. return 100000000ULL;
  1708. }
  1709. static inline u64 sched_cfs_bandwidth_slice(void)
  1710. {
  1711. return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
  1712. }
  1713. /*
  1714. * Replenish runtime according to assigned quota and update expiration time.
  1715. * We use sched_clock_cpu directly instead of rq->clock to avoid adding
  1716. * additional synchronization around rq->lock.
  1717. *
  1718. * requires cfs_b->lock
  1719. */
  1720. void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
  1721. {
  1722. u64 now;
  1723. if (cfs_b->quota == RUNTIME_INF)
  1724. return;
  1725. now = sched_clock_cpu(smp_processor_id());
  1726. cfs_b->runtime = cfs_b->quota;
  1727. cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
  1728. }
  1729. static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
  1730. {
  1731. return &tg->cfs_bandwidth;
  1732. }
  1733. /* rq->task_clock normalized against any time this cfs_rq has spent throttled */
  1734. static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
  1735. {
  1736. if (unlikely(cfs_rq->throttle_count))
  1737. return cfs_rq->throttled_clock_task;
  1738. return rq_of(cfs_rq)->clock_task - cfs_rq->throttled_clock_task_time;
  1739. }
  1740. /* returns 0 on failure to allocate runtime */
  1741. static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1742. {
  1743. struct task_group *tg = cfs_rq->tg;
  1744. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
  1745. u64 amount = 0, min_amount, expires;
  1746. /* note: this is a positive sum as runtime_remaining <= 0 */
  1747. min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
  1748. raw_spin_lock(&cfs_b->lock);
  1749. if (cfs_b->quota == RUNTIME_INF)
  1750. amount = min_amount;
  1751. else {
  1752. /*
  1753. * If the bandwidth pool has become inactive, then at least one
  1754. * period must have elapsed since the last consumption.
  1755. * Refresh the global state and ensure bandwidth timer becomes
  1756. * active.
  1757. */
  1758. if (!cfs_b->timer_active) {
  1759. __refill_cfs_bandwidth_runtime(cfs_b);
  1760. __start_cfs_bandwidth(cfs_b);
  1761. }
  1762. if (cfs_b->runtime > 0) {
  1763. amount = min(cfs_b->runtime, min_amount);
  1764. cfs_b->runtime -= amount;
  1765. cfs_b->idle = 0;
  1766. }
  1767. }
  1768. expires = cfs_b->runtime_expires;
  1769. raw_spin_unlock(&cfs_b->lock);
  1770. cfs_rq->runtime_remaining += amount;
  1771. /*
  1772. * we may have advanced our local expiration to account for allowed
  1773. * spread between our sched_clock and the one on which runtime was
  1774. * issued.
  1775. */
  1776. if ((s64)(expires - cfs_rq->runtime_expires) > 0)
  1777. cfs_rq->runtime_expires = expires;
  1778. return cfs_rq->runtime_remaining > 0;
  1779. }
  1780. /*
  1781. * Note: This depends on the synchronization provided by sched_clock and the
  1782. * fact that rq->clock snapshots this value.
  1783. */
  1784. static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1785. {
  1786. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1787. struct rq *rq = rq_of(cfs_rq);
  1788. /* if the deadline is ahead of our clock, nothing to do */
  1789. if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
  1790. return;
  1791. if (cfs_rq->runtime_remaining < 0)
  1792. return;
  1793. /*
  1794. * If the local deadline has passed we have to consider the
  1795. * possibility that our sched_clock is 'fast' and the global deadline
  1796. * has not truly expired.
  1797. *
  1798. * Fortunately we can check determine whether this the case by checking
  1799. * whether the global deadline has advanced.
  1800. */
  1801. if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
  1802. /* extend local deadline, drift is bounded above by 2 ticks */
  1803. cfs_rq->runtime_expires += TICK_NSEC;
  1804. } else {
  1805. /* global deadline is ahead, expiration has passed */
  1806. cfs_rq->runtime_remaining = 0;
  1807. }
  1808. }
  1809. static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
  1810. unsigned long delta_exec)
  1811. {
  1812. /* dock delta_exec before expiring quota (as it could span periods) */
  1813. cfs_rq->runtime_remaining -= delta_exec;
  1814. expire_cfs_rq_runtime(cfs_rq);
  1815. if (likely(cfs_rq->runtime_remaining > 0))
  1816. return;
  1817. /*
  1818. * if we're unable to extend our runtime we resched so that the active
  1819. * hierarchy can be throttled
  1820. */
  1821. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  1822. resched_task(rq_of(cfs_rq)->curr);
  1823. }
  1824. static __always_inline
  1825. void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
  1826. {
  1827. if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
  1828. return;
  1829. __account_cfs_rq_runtime(cfs_rq, delta_exec);
  1830. }
  1831. static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
  1832. {
  1833. return cfs_bandwidth_used() && cfs_rq->throttled;
  1834. }
  1835. /* check whether cfs_rq, or any parent, is throttled */
  1836. static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
  1837. {
  1838. return cfs_bandwidth_used() && cfs_rq->throttle_count;
  1839. }
  1840. /*
  1841. * Ensure that neither of the group entities corresponding to src_cpu or
  1842. * dest_cpu are members of a throttled hierarchy when performing group
  1843. * load-balance operations.
  1844. */
  1845. static inline int throttled_lb_pair(struct task_group *tg,
  1846. int src_cpu, int dest_cpu)
  1847. {
  1848. struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
  1849. src_cfs_rq = tg->cfs_rq[src_cpu];
  1850. dest_cfs_rq = tg->cfs_rq[dest_cpu];
  1851. return throttled_hierarchy(src_cfs_rq) ||
  1852. throttled_hierarchy(dest_cfs_rq);
  1853. }
  1854. /* updated child weight may affect parent so we have to do this bottom up */
  1855. static int tg_unthrottle_up(struct task_group *tg, void *data)
  1856. {
  1857. struct rq *rq = data;
  1858. struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
  1859. cfs_rq->throttle_count--;
  1860. #ifdef CONFIG_SMP
  1861. if (!cfs_rq->throttle_count) {
  1862. /* adjust cfs_rq_clock_task() */
  1863. cfs_rq->throttled_clock_task_time += rq->clock_task -
  1864. cfs_rq->throttled_clock_task;
  1865. }
  1866. #endif
  1867. return 0;
  1868. }
  1869. static int tg_throttle_down(struct task_group *tg, void *data)
  1870. {
  1871. struct rq *rq = data;
  1872. struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
  1873. /* group is entering throttled state, stop time */
  1874. if (!cfs_rq->throttle_count)
  1875. cfs_rq->throttled_clock_task = rq->clock_task;
  1876. cfs_rq->throttle_count++;
  1877. return 0;
  1878. }
  1879. static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
  1880. {
  1881. struct rq *rq = rq_of(cfs_rq);
  1882. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1883. struct sched_entity *se;
  1884. long task_delta, dequeue = 1;
  1885. se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
  1886. /* freeze hierarchy runnable averages while throttled */
  1887. rcu_read_lock();
  1888. walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
  1889. rcu_read_unlock();
  1890. task_delta = cfs_rq->h_nr_running;
  1891. for_each_sched_entity(se) {
  1892. struct cfs_rq *qcfs_rq = cfs_rq_of(se);
  1893. /* throttled entity or throttle-on-deactivate */
  1894. if (!se->on_rq)
  1895. break;
  1896. if (dequeue)
  1897. dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
  1898. qcfs_rq->h_nr_running -= task_delta;
  1899. if (qcfs_rq->load.weight)
  1900. dequeue = 0;
  1901. }
  1902. if (!se)
  1903. rq->nr_running -= task_delta;
  1904. cfs_rq->throttled = 1;
  1905. cfs_rq->throttled_clock = rq->clock;
  1906. raw_spin_lock(&cfs_b->lock);
  1907. list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
  1908. raw_spin_unlock(&cfs_b->lock);
  1909. }
  1910. void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
  1911. {
  1912. struct rq *rq = rq_of(cfs_rq);
  1913. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1914. struct sched_entity *se;
  1915. int enqueue = 1;
  1916. long task_delta;
  1917. se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
  1918. cfs_rq->throttled = 0;
  1919. raw_spin_lock(&cfs_b->lock);
  1920. cfs_b->throttled_time += rq->clock - cfs_rq->throttled_clock;
  1921. list_del_rcu(&cfs_rq->throttled_list);
  1922. raw_spin_unlock(&cfs_b->lock);
  1923. update_rq_clock(rq);
  1924. /* update hierarchical throttle state */
  1925. walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
  1926. if (!cfs_rq->load.weight)
  1927. return;
  1928. task_delta = cfs_rq->h_nr_running;
  1929. for_each_sched_entity(se) {
  1930. if (se->on_rq)
  1931. enqueue = 0;
  1932. cfs_rq = cfs_rq_of(se);
  1933. if (enqueue)
  1934. enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
  1935. cfs_rq->h_nr_running += task_delta;
  1936. if (cfs_rq_throttled(cfs_rq))
  1937. break;
  1938. }
  1939. if (!se)
  1940. rq->nr_running += task_delta;
  1941. /* determine whether we need to wake up potentially idle cpu */
  1942. if (rq->curr == rq->idle && rq->cfs.nr_running)
  1943. resched_task(rq->curr);
  1944. }
  1945. static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
  1946. u64 remaining, u64 expires)
  1947. {
  1948. struct cfs_rq *cfs_rq;
  1949. u64 runtime = remaining;
  1950. rcu_read_lock();
  1951. list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
  1952. throttled_list) {
  1953. struct rq *rq = rq_of(cfs_rq);
  1954. raw_spin_lock(&rq->lock);
  1955. if (!cfs_rq_throttled(cfs_rq))
  1956. goto next;
  1957. runtime = -cfs_rq->runtime_remaining + 1;
  1958. if (runtime > remaining)
  1959. runtime = remaining;
  1960. remaining -= runtime;
  1961. cfs_rq->runtime_remaining += runtime;
  1962. cfs_rq->runtime_expires = expires;
  1963. /* we check whether we're throttled above */
  1964. if (cfs_rq->runtime_remaining > 0)
  1965. unthrottle_cfs_rq(cfs_rq);
  1966. next:
  1967. raw_spin_unlock(&rq->lock);
  1968. if (!remaining)
  1969. break;
  1970. }
  1971. rcu_read_unlock();
  1972. return remaining;
  1973. }
  1974. /*
  1975. * Responsible for refilling a task_group's bandwidth and unthrottling its
  1976. * cfs_rqs as appropriate. If there has been no activity within the last
  1977. * period the timer is deactivated until scheduling resumes; cfs_b->idle is
  1978. * used to track this state.
  1979. */
  1980. static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
  1981. {
  1982. u64 runtime, runtime_expires;
  1983. int idle = 1, throttled;
  1984. raw_spin_lock(&cfs_b->lock);
  1985. /* no need to continue the timer with no bandwidth constraint */
  1986. if (cfs_b->quota == RUNTIME_INF)
  1987. goto out_unlock;
  1988. throttled = !list_empty(&cfs_b->throttled_cfs_rq);
  1989. /* idle depends on !throttled (for the case of a large deficit) */
  1990. idle = cfs_b->idle && !throttled;
  1991. cfs_b->nr_periods += overrun;
  1992. /* if we're going inactive then everything else can be deferred */
  1993. if (idle)
  1994. goto out_unlock;
  1995. __refill_cfs_bandwidth_runtime(cfs_b);
  1996. if (!throttled) {
  1997. /* mark as potentially idle for the upcoming period */
  1998. cfs_b->idle = 1;
  1999. goto out_unlock;
  2000. }
  2001. /* account preceding periods in which throttling occurred */
  2002. cfs_b->nr_throttled += overrun;
  2003. /*
  2004. * There are throttled entities so we must first use the new bandwidth
  2005. * to unthrottle them before making it generally available. This
  2006. * ensures that all existing debts will be paid before a new cfs_rq is
  2007. * allowed to run.
  2008. */
  2009. runtime = cfs_b->runtime;
  2010. runtime_expires = cfs_b->runtime_expires;
  2011. cfs_b->runtime = 0;
  2012. /*
  2013. * This check is repeated as we are holding onto the new bandwidth
  2014. * while we unthrottle. This can potentially race with an unthrottled
  2015. * group trying to acquire new bandwidth from the global pool.
  2016. */
  2017. while (throttled && runtime > 0) {
  2018. raw_spin_unlock(&cfs_b->lock);
  2019. /* we can't nest cfs_b->lock while distributing bandwidth */
  2020. runtime = distribute_cfs_runtime(cfs_b, runtime,
  2021. runtime_expires);
  2022. raw_spin_lock(&cfs_b->lock);
  2023. throttled = !list_empty(&cfs_b->throttled_cfs_rq);
  2024. }
  2025. /* return (any) remaining runtime */
  2026. cfs_b->runtime = runtime;
  2027. /*
  2028. * While we are ensured activity in the period following an
  2029. * unthrottle, this also covers the case in which the new bandwidth is
  2030. * insufficient to cover the existing bandwidth deficit. (Forcing the
  2031. * timer to remain active while there are any throttled entities.)
  2032. */
  2033. cfs_b->idle = 0;
  2034. out_unlock:
  2035. if (idle)
  2036. cfs_b->timer_active = 0;
  2037. raw_spin_unlock(&cfs_b->lock);
  2038. return idle;
  2039. }
  2040. /* a cfs_rq won't donate quota below this amount */
  2041. static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
  2042. /* minimum remaining period time to redistribute slack quota */
  2043. static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
  2044. /* how long we wait to gather additional slack before distributing */
  2045. static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
  2046. /* are we near the end of the current quota period? */
  2047. static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
  2048. {
  2049. struct hrtimer *refresh_timer = &cfs_b->period_timer;
  2050. u64 remaining;
  2051. /* if the call-back is running a quota refresh is already occurring */
  2052. if (hrtimer_callback_running(refresh_timer))
  2053. return 1;
  2054. /* is a quota refresh about to occur? */
  2055. remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
  2056. if (remaining < min_expire)
  2057. return 1;
  2058. return 0;
  2059. }
  2060. static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
  2061. {
  2062. u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
  2063. /* if there's a quota refresh soon don't bother with slack */
  2064. if (runtime_refresh_within(cfs_b, min_left))
  2065. return;
  2066. start_bandwidth_timer(&cfs_b->slack_timer,
  2067. ns_to_ktime(cfs_bandwidth_slack_period));
  2068. }
  2069. /* we know any runtime found here is valid as update_curr() precedes return */
  2070. static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  2071. {
  2072. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  2073. s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
  2074. if (slack_runtime <= 0)
  2075. return;
  2076. raw_spin_lock(&cfs_b->lock);
  2077. if (cfs_b->quota != RUNTIME_INF &&
  2078. cfs_rq->runtime_expires == cfs_b->runtime_expires) {
  2079. cfs_b->runtime += slack_runtime;
  2080. /* we are under rq->lock, defer unthrottling using a timer */
  2081. if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
  2082. !list_empty(&cfs_b->throttled_cfs_rq))
  2083. start_cfs_slack_bandwidth(cfs_b);
  2084. }
  2085. raw_spin_unlock(&cfs_b->lock);
  2086. /* even if it's not valid for return we don't want to try again */
  2087. cfs_rq->runtime_remaining -= slack_runtime;
  2088. }
  2089. static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  2090. {
  2091. if (!cfs_bandwidth_used())
  2092. return;
  2093. if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
  2094. return;
  2095. __return_cfs_rq_runtime(cfs_rq);
  2096. }
  2097. /*
  2098. * This is done with a timer (instead of inline with bandwidth return) since
  2099. * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
  2100. */
  2101. static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
  2102. {
  2103. u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
  2104. u64 expires;
  2105. /* confirm we're still not at a refresh boundary */
  2106. if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
  2107. return;
  2108. raw_spin_lock(&cfs_b->lock);
  2109. if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
  2110. runtime = cfs_b->runtime;
  2111. cfs_b->runtime = 0;
  2112. }
  2113. expires = cfs_b->runtime_expires;
  2114. raw_spin_unlock(&cfs_b->lock);
  2115. if (!runtime)
  2116. return;
  2117. runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
  2118. raw_spin_lock(&cfs_b->lock);
  2119. if (expires == cfs_b->runtime_expires)
  2120. cfs_b->runtime = runtime;
  2121. raw_spin_unlock(&cfs_b->lock);
  2122. }
  2123. /*
  2124. * When a group wakes up we want to make sure that its quota is not already
  2125. * expired/exceeded, otherwise it may be allowed to steal additional ticks of
  2126. * runtime as update_curr() throttling can not not trigger until it's on-rq.
  2127. */
  2128. static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
  2129. {
  2130. if (!cfs_bandwidth_used())
  2131. return;
  2132. /* an active group must be handled by the update_curr()->put() path */
  2133. if (!cfs_rq->runtime_enabled || cfs_rq->curr)
  2134. return;
  2135. /* ensure the group is not already throttled */
  2136. if (cfs_rq_throttled(cfs_rq))
  2137. return;
  2138. /* update runtime allocation */
  2139. account_cfs_rq_runtime(cfs_rq, 0);
  2140. if (cfs_rq->runtime_remaining <= 0)
  2141. throttle_cfs_rq(cfs_rq);
  2142. }
  2143. /* conditionally throttle active cfs_rq's from put_prev_entity() */
  2144. static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  2145. {
  2146. if (!cfs_bandwidth_used())
  2147. return;
  2148. if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
  2149. return;
  2150. /*
  2151. * it's possible for a throttled entity to be forced into a running
  2152. * state (e.g. set_curr_task), in this case we're finished.
  2153. */
  2154. if (cfs_rq_throttled(cfs_rq))
  2155. return;
  2156. throttle_cfs_rq(cfs_rq);
  2157. }
  2158. static inline u64 default_cfs_period(void);
  2159. static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
  2160. static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
  2161. static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
  2162. {
  2163. struct cfs_bandwidth *cfs_b =
  2164. container_of(timer, struct cfs_bandwidth, slack_timer);
  2165. do_sched_cfs_slack_timer(cfs_b);
  2166. return HRTIMER_NORESTART;
  2167. }
  2168. static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
  2169. {
  2170. struct cfs_bandwidth *cfs_b =
  2171. container_of(timer, struct cfs_bandwidth, period_timer);
  2172. ktime_t now;
  2173. int overrun;
  2174. int idle = 0;
  2175. for (;;) {
  2176. now = hrtimer_cb_get_time(timer);
  2177. overrun = hrtimer_forward(timer, now, cfs_b->period);
  2178. if (!overrun)
  2179. break;
  2180. idle = do_sched_cfs_period_timer(cfs_b, overrun);
  2181. }
  2182. return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
  2183. }
  2184. void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
  2185. {
  2186. raw_spin_lock_init(&cfs_b->lock);
  2187. cfs_b->runtime = 0;
  2188. cfs_b->quota = RUNTIME_INF;
  2189. cfs_b->period = ns_to_ktime(default_cfs_period());
  2190. INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
  2191. hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2192. cfs_b->period_timer.function = sched_cfs_period_timer;
  2193. hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2194. cfs_b->slack_timer.function = sched_cfs_slack_timer;
  2195. }
  2196. static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  2197. {
  2198. cfs_rq->runtime_enabled = 0;
  2199. INIT_LIST_HEAD(&cfs_rq->throttled_list);
  2200. }
  2201. /* requires cfs_b->lock, may release to reprogram timer */
  2202. void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
  2203. {
  2204. /*
  2205. * The timer may be active because we're trying to set a new bandwidth
  2206. * period or because we're racing with the tear-down path
  2207. * (timer_active==0 becomes visible before the hrtimer call-back
  2208. * terminates). In either case we ensure that it's re-programmed
  2209. */
  2210. while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
  2211. raw_spin_unlock(&cfs_b->lock);
  2212. /* ensure cfs_b->lock is available while we wait */
  2213. hrtimer_cancel(&cfs_b->period_timer);
  2214. raw_spin_lock(&cfs_b->lock);
  2215. /* if someone else restarted the timer then we're done */
  2216. if (cfs_b->timer_active)
  2217. return;
  2218. }
  2219. cfs_b->timer_active = 1;
  2220. start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
  2221. }
  2222. static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
  2223. {
  2224. hrtimer_cancel(&cfs_b->period_timer);
  2225. hrtimer_cancel(&cfs_b->slack_timer);
  2226. }
  2227. static void unthrottle_offline_cfs_rqs(struct rq *rq)
  2228. {
  2229. struct cfs_rq *cfs_rq;
  2230. for_each_leaf_cfs_rq(rq, cfs_rq) {
  2231. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  2232. if (!cfs_rq->runtime_enabled)
  2233. continue;
  2234. /*
  2235. * clock_task is not advancing so we just need to make sure
  2236. * there's some valid quota amount
  2237. */
  2238. cfs_rq->runtime_remaining = cfs_b->quota;
  2239. if (cfs_rq_throttled(cfs_rq))
  2240. unthrottle_cfs_rq(cfs_rq);
  2241. }
  2242. }
  2243. #else /* CONFIG_CFS_BANDWIDTH */
  2244. static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
  2245. {
  2246. return rq_of(cfs_rq)->clock_task;
  2247. }
  2248. static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
  2249. unsigned long delta_exec) {}
  2250. static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
  2251. static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
  2252. static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
  2253. static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
  2254. {
  2255. return 0;
  2256. }
  2257. static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
  2258. {
  2259. return 0;
  2260. }
  2261. static inline int throttled_lb_pair(struct task_group *tg,
  2262. int src_cpu, int dest_cpu)
  2263. {
  2264. return 0;
  2265. }
  2266. void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
  2267. #ifdef CONFIG_FAIR_GROUP_SCHED
  2268. static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
  2269. #endif
  2270. static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
  2271. {
  2272. return NULL;
  2273. }
  2274. static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
  2275. static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
  2276. #endif /* CONFIG_CFS_BANDWIDTH */
  2277. /**************************************************
  2278. * CFS operations on tasks:
  2279. */
  2280. #ifdef CONFIG_SCHED_HRTICK
  2281. static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  2282. {
  2283. struct sched_entity *se = &p->se;
  2284. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  2285. WARN_ON(task_rq(p) != rq);
  2286. if (cfs_rq->nr_running > 1) {
  2287. u64 slice = sched_slice(cfs_rq, se);
  2288. u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
  2289. s64 delta = slice - ran;
  2290. if (delta < 0) {
  2291. if (rq->curr == p)
  2292. resched_task(p);
  2293. return;
  2294. }
  2295. /*
  2296. * Don't schedule slices shorter than 10000ns, that just
  2297. * doesn't make sense. Rely on vruntime for fairness.
  2298. */
  2299. if (rq->curr != p)
  2300. delta = max_t(s64, 10000LL, delta);
  2301. hrtick_start(rq, delta);
  2302. }
  2303. }
  2304. /*
  2305. * called from enqueue/dequeue and updates the hrtick when the
  2306. * current task is from our class and nr_running is low enough
  2307. * to matter.
  2308. */
  2309. static void hrtick_update(struct rq *rq)
  2310. {
  2311. struct task_struct *curr = rq->curr;
  2312. if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
  2313. return;
  2314. if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
  2315. hrtick_start_fair(rq, curr);
  2316. }
  2317. #else /* !CONFIG_SCHED_HRTICK */
  2318. static inline void
  2319. hrtick_start_fair(struct rq *rq, struct task_struct *p)
  2320. {
  2321. }
  2322. static inline void hrtick_update(struct rq *rq)
  2323. {
  2324. }
  2325. #endif
  2326. /*
  2327. * The enqueue_task method is called before nr_running is
  2328. * increased. Here we update the fair scheduling stats and
  2329. * then put the task into the rbtree:
  2330. */
  2331. static void
  2332. enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
  2333. {
  2334. struct cfs_rq *cfs_rq;
  2335. struct sched_entity *se = &p->se;
  2336. for_each_sched_entity(se) {
  2337. if (se->on_rq)
  2338. break;
  2339. cfs_rq = cfs_rq_of(se);
  2340. enqueue_entity(cfs_rq, se, flags);
  2341. /*
  2342. * end evaluation on encountering a throttled cfs_rq
  2343. *
  2344. * note: in the case of encountering a throttled cfs_rq we will
  2345. * post the final h_nr_running increment below.
  2346. */
  2347. if (cfs_rq_throttled(cfs_rq))
  2348. break;
  2349. cfs_rq->h_nr_running++;
  2350. flags = ENQUEUE_WAKEUP;
  2351. }
  2352. for_each_sched_entity(se) {
  2353. cfs_rq = cfs_rq_of(se);
  2354. cfs_rq->h_nr_running++;
  2355. if (cfs_rq_throttled(cfs_rq))
  2356. break;
  2357. update_cfs_shares(cfs_rq);
  2358. update_entity_load_avg(se, 1);
  2359. }
  2360. if (!se) {
  2361. update_rq_runnable_avg(rq, rq->nr_running);
  2362. inc_nr_running(rq);
  2363. }
  2364. hrtick_update(rq);
  2365. }
  2366. static void set_next_buddy(struct sched_entity *se);
  2367. /*
  2368. * The dequeue_task method is called before nr_running is
  2369. * decreased. We remove the task from the rbtree and
  2370. * update the fair scheduling stats:
  2371. */
  2372. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
  2373. {
  2374. struct cfs_rq *cfs_rq;
  2375. struct sched_entity *se = &p->se;
  2376. int task_sleep = flags & DEQUEUE_SLEEP;
  2377. for_each_sched_entity(se) {
  2378. cfs_rq = cfs_rq_of(se);
  2379. dequeue_entity(cfs_rq, se, flags);
  2380. /*
  2381. * end evaluation on encountering a throttled cfs_rq
  2382. *
  2383. * note: in the case of encountering a throttled cfs_rq we will
  2384. * post the final h_nr_running decrement below.
  2385. */
  2386. if (cfs_rq_throttled(cfs_rq))
  2387. break;
  2388. cfs_rq->h_nr_running--;
  2389. /* Don't dequeue parent if it has other entities besides us */
  2390. if (cfs_rq->load.weight) {
  2391. /*
  2392. * Bias pick_next to pick a task from this cfs_rq, as
  2393. * p is sleeping when it is within its sched_slice.
  2394. */
  2395. if (task_sleep && parent_entity(se))
  2396. set_next_buddy(parent_entity(se));
  2397. /* avoid re-evaluating load for this entity */
  2398. se = parent_entity(se);
  2399. break;
  2400. }
  2401. flags |= DEQUEUE_SLEEP;
  2402. }
  2403. for_each_sched_entity(se) {
  2404. cfs_rq = cfs_rq_of(se);
  2405. cfs_rq->h_nr_running--;
  2406. if (cfs_rq_throttled(cfs_rq))
  2407. break;
  2408. update_cfs_shares(cfs_rq);
  2409. update_entity_load_avg(se, 1);
  2410. }
  2411. if (!se) {
  2412. dec_nr_running(rq);
  2413. update_rq_runnable_avg(rq, 1);
  2414. }
  2415. hrtick_update(rq);
  2416. }
  2417. #ifdef CONFIG_SMP
  2418. /* Used instead of source_load when we know the type == 0 */
  2419. static unsigned long weighted_cpuload(const int cpu)
  2420. {
  2421. return cpu_rq(cpu)->load.weight;
  2422. }
  2423. /*
  2424. * Return a low guess at the load of a migration-source cpu weighted
  2425. * according to the scheduling class and "nice" value.
  2426. *
  2427. * We want to under-estimate the load of migration sources, to
  2428. * balance conservatively.
  2429. */
  2430. static unsigned long source_load(int cpu, int type)
  2431. {
  2432. struct rq *rq = cpu_rq(cpu);
  2433. unsigned long total = weighted_cpuload(cpu);
  2434. if (type == 0 || !sched_feat(LB_BIAS))
  2435. return total;
  2436. return min(rq->cpu_load[type-1], total);
  2437. }
  2438. /*
  2439. * Return a high guess at the load of a migration-target cpu weighted
  2440. * according to the scheduling class and "nice" value.
  2441. */
  2442. static unsigned long target_load(int cpu, int type)
  2443. {
  2444. struct rq *rq = cpu_rq(cpu);
  2445. unsigned long total = weighted_cpuload(cpu);
  2446. if (type == 0 || !sched_feat(LB_BIAS))
  2447. return total;
  2448. return max(rq->cpu_load[type-1], total);
  2449. }
  2450. static unsigned long power_of(int cpu)
  2451. {
  2452. return cpu_rq(cpu)->cpu_power;
  2453. }
  2454. static unsigned long cpu_avg_load_per_task(int cpu)
  2455. {
  2456. struct rq *rq = cpu_rq(cpu);
  2457. unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
  2458. if (nr_running)
  2459. return rq->load.weight / nr_running;
  2460. return 0;
  2461. }
  2462. static void task_waking_fair(struct task_struct *p)
  2463. {
  2464. struct sched_entity *se = &p->se;
  2465. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  2466. u64 min_vruntime;
  2467. #ifndef CONFIG_64BIT
  2468. u64 min_vruntime_copy;
  2469. do {
  2470. min_vruntime_copy = cfs_rq->min_vruntime_copy;
  2471. smp_rmb();
  2472. min_vruntime = cfs_rq->min_vruntime;
  2473. } while (min_vruntime != min_vruntime_copy);
  2474. #else
  2475. min_vruntime = cfs_rq->min_vruntime;
  2476. #endif
  2477. se->vruntime -= min_vruntime;
  2478. }
  2479. #ifdef CONFIG_FAIR_GROUP_SCHED
  2480. /*
  2481. * effective_load() calculates the load change as seen from the root_task_group
  2482. *
  2483. * Adding load to a group doesn't make a group heavier, but can cause movement
  2484. * of group shares between cpus. Assuming the shares were perfectly aligned one
  2485. * can calculate the shift in shares.
  2486. *
  2487. * Calculate the effective load difference if @wl is added (subtracted) to @tg
  2488. * on this @cpu and results in a total addition (subtraction) of @wg to the
  2489. * total group weight.
  2490. *
  2491. * Given a runqueue weight distribution (rw_i) we can compute a shares
  2492. * distribution (s_i) using:
  2493. *
  2494. * s_i = rw_i / \Sum rw_j (1)
  2495. *
  2496. * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
  2497. * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
  2498. * shares distribution (s_i):
  2499. *
  2500. * rw_i = { 2, 4, 1, 0 }
  2501. * s_i = { 2/7, 4/7, 1/7, 0 }
  2502. *
  2503. * As per wake_affine() we're interested in the load of two CPUs (the CPU the
  2504. * task used to run on and the CPU the waker is running on), we need to
  2505. * compute the effect of waking a task on either CPU and, in case of a sync
  2506. * wakeup, compute the effect of the current task going to sleep.
  2507. *
  2508. * So for a change of @wl to the local @cpu with an overall group weight change
  2509. * of @wl we can compute the new shares distribution (s'_i) using:
  2510. *
  2511. * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
  2512. *
  2513. * Suppose we're interested in CPUs 0 and 1, and want to compute the load
  2514. * differences in waking a task to CPU 0. The additional task changes the
  2515. * weight and shares distributions like:
  2516. *
  2517. * rw'_i = { 3, 4, 1, 0 }
  2518. * s'_i = { 3/8, 4/8, 1/8, 0 }
  2519. *
  2520. * We can then compute the difference in effective weight by using:
  2521. *
  2522. * dw_i = S * (s'_i - s_i) (3)
  2523. *
  2524. * Where 'S' is the group weight as seen by its parent.
  2525. *
  2526. * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
  2527. * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
  2528. * 4/7) times the weight of the group.
  2529. */
  2530. static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
  2531. {
  2532. struct sched_entity *se = tg->se[cpu];
  2533. if (!tg->parent) /* the trivial, non-cgroup case */
  2534. return wl;
  2535. for_each_sched_entity(se) {
  2536. long w, W;
  2537. tg = se->my_q->tg;
  2538. /*
  2539. * W = @wg + \Sum rw_j
  2540. */
  2541. W = wg + calc_tg_weight(tg, se->my_q);
  2542. /*
  2543. * w = rw_i + @wl
  2544. */
  2545. w = se->my_q->load.weight + wl;
  2546. /*
  2547. * wl = S * s'_i; see (2)
  2548. */
  2549. if (W > 0 && w < W)
  2550. wl = (w * tg->shares) / W;
  2551. else
  2552. wl = tg->shares;
  2553. /*
  2554. * Per the above, wl is the new se->load.weight value; since
  2555. * those are clipped to [MIN_SHARES, ...) do so now. See
  2556. * calc_cfs_shares().
  2557. */
  2558. if (wl < MIN_SHARES)
  2559. wl = MIN_SHARES;
  2560. /*
  2561. * wl = dw_i = S * (s'_i - s_i); see (3)
  2562. */
  2563. wl -= se->load.weight;
  2564. /*
  2565. * Recursively apply this logic to all parent groups to compute
  2566. * the final effective load change on the root group. Since
  2567. * only the @tg group gets extra weight, all parent groups can
  2568. * only redistribute existing shares. @wl is the shift in shares
  2569. * resulting from this level per the above.
  2570. */
  2571. wg = 0;
  2572. }
  2573. return wl;
  2574. }
  2575. #else
  2576. static inline unsigned long effective_load(struct task_group *tg, int cpu,
  2577. unsigned long wl, unsigned long wg)
  2578. {
  2579. return wl;
  2580. }
  2581. #endif
  2582. static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
  2583. {
  2584. s64 this_load, load;
  2585. int idx, this_cpu, prev_cpu;
  2586. unsigned long tl_per_task;
  2587. struct task_group *tg;
  2588. unsigned long weight;
  2589. int balanced;
  2590. idx = sd->wake_idx;
  2591. this_cpu = smp_processor_id();
  2592. prev_cpu = task_cpu(p);
  2593. load = source_load(prev_cpu, idx);
  2594. this_load = target_load(this_cpu, idx);
  2595. /*
  2596. * If sync wakeup then subtract the (maximum possible)
  2597. * effect of the currently running task from the load
  2598. * of the current CPU:
  2599. */
  2600. if (sync) {
  2601. tg = task_group(current);
  2602. weight = current->se.load.weight;
  2603. this_load += effective_load(tg, this_cpu, -weight, -weight);
  2604. load += effective_load(tg, prev_cpu, 0, -weight);
  2605. }
  2606. tg = task_group(p);
  2607. weight = p->se.load.weight;
  2608. /*
  2609. * In low-load situations, where prev_cpu is idle and this_cpu is idle
  2610. * due to the sync cause above having dropped this_load to 0, we'll
  2611. * always have an imbalance, but there's really nothing you can do
  2612. * about that, so that's good too.
  2613. *
  2614. * Otherwise check if either cpus are near enough in load to allow this
  2615. * task to be woken on this_cpu.
  2616. */
  2617. if (this_load > 0) {
  2618. s64 this_eff_load, prev_eff_load;
  2619. this_eff_load = 100;
  2620. this_eff_load *= power_of(prev_cpu);
  2621. this_eff_load *= this_load +
  2622. effective_load(tg, this_cpu, weight, weight);
  2623. prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
  2624. prev_eff_load *= power_of(this_cpu);
  2625. prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
  2626. balanced = this_eff_load <= prev_eff_load;
  2627. } else
  2628. balanced = true;
  2629. /*
  2630. * If the currently running task will sleep within
  2631. * a reasonable amount of time then attract this newly
  2632. * woken task:
  2633. */
  2634. if (sync && balanced)
  2635. return 1;
  2636. schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
  2637. tl_per_task = cpu_avg_load_per_task(this_cpu);
  2638. if (balanced ||
  2639. (this_load <= load &&
  2640. this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
  2641. /*
  2642. * This domain has SD_WAKE_AFFINE and
  2643. * p is cache cold in this domain, and
  2644. * there is no bad imbalance.
  2645. */
  2646. schedstat_inc(sd, ttwu_move_affine);
  2647. schedstat_inc(p, se.statistics.nr_wakeups_affine);
  2648. return 1;
  2649. }
  2650. return 0;
  2651. }
  2652. /*
  2653. * find_idlest_group finds and returns the least busy CPU group within the
  2654. * domain.
  2655. */
  2656. static struct sched_group *
  2657. find_idlest_group(struct sched_domain *sd, struct task_struct *p,
  2658. int this_cpu, int load_idx)
  2659. {
  2660. struct sched_group *idlest = NULL, *group = sd->groups;
  2661. unsigned long min_load = ULONG_MAX, this_load = 0;
  2662. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  2663. do {
  2664. unsigned long load, avg_load;
  2665. int local_group;
  2666. int i;
  2667. /* Skip over this group if it has no CPUs allowed */
  2668. if (!cpumask_intersects(sched_group_cpus(group),
  2669. tsk_cpus_allowed(p)))
  2670. continue;
  2671. local_group = cpumask_test_cpu(this_cpu,
  2672. sched_group_cpus(group));
  2673. /* Tally up the load of all CPUs in the group */
  2674. avg_load = 0;
  2675. for_each_cpu(i, sched_group_cpus(group)) {
  2676. /* Bias balancing toward cpus of our domain */
  2677. if (local_group)
  2678. load = source_load(i, load_idx);
  2679. else
  2680. load = target_load(i, load_idx);
  2681. avg_load += load;
  2682. }
  2683. /* Adjust by relative CPU power of the group */
  2684. avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
  2685. if (local_group) {
  2686. this_load = avg_load;
  2687. } else if (avg_load < min_load) {
  2688. min_load = avg_load;
  2689. idlest = group;
  2690. }
  2691. } while (group = group->next, group != sd->groups);
  2692. if (!idlest || 100*this_load < imbalance*min_load)
  2693. return NULL;
  2694. return idlest;
  2695. }
  2696. /*
  2697. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  2698. */
  2699. static int
  2700. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  2701. {
  2702. unsigned long load, min_load = ULONG_MAX;
  2703. int idlest = -1;
  2704. int i;
  2705. /* Traverse only the allowed CPUs */
  2706. for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
  2707. load = weighted_cpuload(i);
  2708. if (load < min_load || (load == min_load && i == this_cpu)) {
  2709. min_load = load;
  2710. idlest = i;
  2711. }
  2712. }
  2713. return idlest;
  2714. }
  2715. /*
  2716. * Try and locate an idle CPU in the sched_domain.
  2717. */
  2718. static int select_idle_sibling(struct task_struct *p, int target)
  2719. {
  2720. int cpu = smp_processor_id();
  2721. int prev_cpu = task_cpu(p);
  2722. struct sched_domain *sd;
  2723. struct sched_group *sg;
  2724. int i;
  2725. /*
  2726. * If the task is going to be woken-up on this cpu and if it is
  2727. * already idle, then it is the right target.
  2728. */
  2729. if (target == cpu && idle_cpu(cpu))
  2730. return cpu;
  2731. /*
  2732. * If the task is going to be woken-up on the cpu where it previously
  2733. * ran and if it is currently idle, then it the right target.
  2734. */
  2735. if (target == prev_cpu && idle_cpu(prev_cpu))
  2736. return prev_cpu;
  2737. /*
  2738. * Otherwise, iterate the domains and find an elegible idle cpu.
  2739. */
  2740. sd = rcu_dereference(per_cpu(sd_llc, target));
  2741. for_each_lower_domain(sd) {
  2742. sg = sd->groups;
  2743. do {
  2744. if (!cpumask_intersects(sched_group_cpus(sg),
  2745. tsk_cpus_allowed(p)))
  2746. goto next;
  2747. for_each_cpu(i, sched_group_cpus(sg)) {
  2748. if (!idle_cpu(i))
  2749. goto next;
  2750. }
  2751. target = cpumask_first_and(sched_group_cpus(sg),
  2752. tsk_cpus_allowed(p));
  2753. goto done;
  2754. next:
  2755. sg = sg->next;
  2756. } while (sg != sd->groups);
  2757. }
  2758. done:
  2759. return target;
  2760. }
  2761. /*
  2762. * sched_balance_self: balance the current task (running on cpu) in domains
  2763. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  2764. * SD_BALANCE_EXEC.
  2765. *
  2766. * Balance, ie. select the least loaded group.
  2767. *
  2768. * Returns the target CPU number, or the same CPU if no balancing is needed.
  2769. *
  2770. * preempt must be disabled.
  2771. */
  2772. static int
  2773. select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
  2774. {
  2775. struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
  2776. int cpu = smp_processor_id();
  2777. int prev_cpu = task_cpu(p);
  2778. int new_cpu = cpu;
  2779. int want_affine = 0;
  2780. int sync = wake_flags & WF_SYNC;
  2781. if (p->nr_cpus_allowed == 1)
  2782. return prev_cpu;
  2783. if (sd_flag & SD_BALANCE_WAKE) {
  2784. if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
  2785. want_affine = 1;
  2786. new_cpu = prev_cpu;
  2787. }
  2788. rcu_read_lock();
  2789. for_each_domain(cpu, tmp) {
  2790. if (!(tmp->flags & SD_LOAD_BALANCE))
  2791. continue;
  2792. /*
  2793. * If both cpu and prev_cpu are part of this domain,
  2794. * cpu is a valid SD_WAKE_AFFINE target.
  2795. */
  2796. if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
  2797. cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
  2798. affine_sd = tmp;
  2799. break;
  2800. }
  2801. if (tmp->flags & sd_flag)
  2802. sd = tmp;
  2803. }
  2804. if (affine_sd) {
  2805. if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
  2806. prev_cpu = cpu;
  2807. new_cpu = select_idle_sibling(p, prev_cpu);
  2808. goto unlock;
  2809. }
  2810. while (sd) {
  2811. int load_idx = sd->forkexec_idx;
  2812. struct sched_group *group;
  2813. int weight;
  2814. if (!(sd->flags & sd_flag)) {
  2815. sd = sd->child;
  2816. continue;
  2817. }
  2818. if (sd_flag & SD_BALANCE_WAKE)
  2819. load_idx = sd->wake_idx;
  2820. group = find_idlest_group(sd, p, cpu, load_idx);
  2821. if (!group) {
  2822. sd = sd->child;
  2823. continue;
  2824. }
  2825. new_cpu = find_idlest_cpu(group, p, cpu);
  2826. if (new_cpu == -1 || new_cpu == cpu) {
  2827. /* Now try balancing at a lower domain level of cpu */
  2828. sd = sd->child;
  2829. continue;
  2830. }
  2831. /* Now try balancing at a lower domain level of new_cpu */
  2832. cpu = new_cpu;
  2833. weight = sd->span_weight;
  2834. sd = NULL;
  2835. for_each_domain(cpu, tmp) {
  2836. if (weight <= tmp->span_weight)
  2837. break;
  2838. if (tmp->flags & sd_flag)
  2839. sd = tmp;
  2840. }
  2841. /* while loop will break here if sd == NULL */
  2842. }
  2843. unlock:
  2844. rcu_read_unlock();
  2845. return new_cpu;
  2846. }
  2847. /*
  2848. * Load-tracking only depends on SMP, FAIR_GROUP_SCHED dependency below may be
  2849. * removed when useful for applications beyond shares distribution (e.g.
  2850. * load-balance).
  2851. */
  2852. #ifdef CONFIG_FAIR_GROUP_SCHED
  2853. /*
  2854. * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
  2855. * cfs_rq_of(p) references at time of call are still valid and identify the
  2856. * previous cpu. However, the caller only guarantees p->pi_lock is held; no
  2857. * other assumptions, including the state of rq->lock, should be made.
  2858. */
  2859. static void
  2860. migrate_task_rq_fair(struct task_struct *p, int next_cpu)
  2861. {
  2862. struct sched_entity *se = &p->se;
  2863. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  2864. /*
  2865. * Load tracking: accumulate removed load so that it can be processed
  2866. * when we next update owning cfs_rq under rq->lock. Tasks contribute
  2867. * to blocked load iff they have a positive decay-count. It can never
  2868. * be negative here since on-rq tasks have decay-count == 0.
  2869. */
  2870. if (se->avg.decay_count) {
  2871. se->avg.decay_count = -__synchronize_entity_decay(se);
  2872. atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load);
  2873. }
  2874. }
  2875. #endif
  2876. #endif /* CONFIG_SMP */
  2877. static unsigned long
  2878. wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
  2879. {
  2880. unsigned long gran = sysctl_sched_wakeup_granularity;
  2881. /*
  2882. * Since its curr running now, convert the gran from real-time
  2883. * to virtual-time in his units.
  2884. *
  2885. * By using 'se' instead of 'curr' we penalize light tasks, so
  2886. * they get preempted easier. That is, if 'se' < 'curr' then
  2887. * the resulting gran will be larger, therefore penalizing the
  2888. * lighter, if otoh 'se' > 'curr' then the resulting gran will
  2889. * be smaller, again penalizing the lighter task.
  2890. *
  2891. * This is especially important for buddies when the leftmost
  2892. * task is higher priority than the buddy.
  2893. */
  2894. return calc_delta_fair(gran, se);
  2895. }
  2896. /*
  2897. * Should 'se' preempt 'curr'.
  2898. *
  2899. * |s1
  2900. * |s2
  2901. * |s3
  2902. * g
  2903. * |<--->|c
  2904. *
  2905. * w(c, s1) = -1
  2906. * w(c, s2) = 0
  2907. * w(c, s3) = 1
  2908. *
  2909. */
  2910. static int
  2911. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
  2912. {
  2913. s64 gran, vdiff = curr->vruntime - se->vruntime;
  2914. if (vdiff <= 0)
  2915. return -1;
  2916. gran = wakeup_gran(curr, se);
  2917. if (vdiff > gran)
  2918. return 1;
  2919. return 0;
  2920. }
  2921. static void set_last_buddy(struct sched_entity *se)
  2922. {
  2923. if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
  2924. return;
  2925. for_each_sched_entity(se)
  2926. cfs_rq_of(se)->last = se;
  2927. }
  2928. static void set_next_buddy(struct sched_entity *se)
  2929. {
  2930. if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
  2931. return;
  2932. for_each_sched_entity(se)
  2933. cfs_rq_of(se)->next = se;
  2934. }
  2935. static void set_skip_buddy(struct sched_entity *se)
  2936. {
  2937. for_each_sched_entity(se)
  2938. cfs_rq_of(se)->skip = se;
  2939. }
  2940. /*
  2941. * Preempt the current task with a newly woken task if needed:
  2942. */
  2943. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  2944. {
  2945. struct task_struct *curr = rq->curr;
  2946. struct sched_entity *se = &curr->se, *pse = &p->se;
  2947. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  2948. int scale = cfs_rq->nr_running >= sched_nr_latency;
  2949. int next_buddy_marked = 0;
  2950. if (unlikely(se == pse))
  2951. return;
  2952. /*
  2953. * This is possible from callers such as move_task(), in which we
  2954. * unconditionally check_prempt_curr() after an enqueue (which may have
  2955. * lead to a throttle). This both saves work and prevents false
  2956. * next-buddy nomination below.
  2957. */
  2958. if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
  2959. return;
  2960. if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
  2961. set_next_buddy(pse);
  2962. next_buddy_marked = 1;
  2963. }
  2964. /*
  2965. * We can come here with TIF_NEED_RESCHED already set from new task
  2966. * wake up path.
  2967. *
  2968. * Note: this also catches the edge-case of curr being in a throttled
  2969. * group (e.g. via set_curr_task), since update_curr() (in the
  2970. * enqueue of curr) will have resulted in resched being set. This
  2971. * prevents us from potentially nominating it as a false LAST_BUDDY
  2972. * below.
  2973. */
  2974. if (test_tsk_need_resched(curr))
  2975. return;
  2976. /* Idle tasks are by definition preempted by non-idle tasks. */
  2977. if (unlikely(curr->policy == SCHED_IDLE) &&
  2978. likely(p->policy != SCHED_IDLE))
  2979. goto preempt;
  2980. /*
  2981. * Batch and idle tasks do not preempt non-idle tasks (their preemption
  2982. * is driven by the tick):
  2983. */
  2984. if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
  2985. return;
  2986. find_matching_se(&se, &pse);
  2987. update_curr(cfs_rq_of(se));
  2988. BUG_ON(!pse);
  2989. if (wakeup_preempt_entity(se, pse) == 1) {
  2990. /*
  2991. * Bias pick_next to pick the sched entity that is
  2992. * triggering this preemption.
  2993. */
  2994. if (!next_buddy_marked)
  2995. set_next_buddy(pse);
  2996. goto preempt;
  2997. }
  2998. return;
  2999. preempt:
  3000. resched_task(curr);
  3001. /*
  3002. * Only set the backward buddy when the current task is still
  3003. * on the rq. This can happen when a wakeup gets interleaved
  3004. * with schedule on the ->pre_schedule() or idle_balance()
  3005. * point, either of which can * drop the rq lock.
  3006. *
  3007. * Also, during early boot the idle thread is in the fair class,
  3008. * for obvious reasons its a bad idea to schedule back to it.
  3009. */
  3010. if (unlikely(!se->on_rq || curr == rq->idle))
  3011. return;
  3012. if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
  3013. set_last_buddy(se);
  3014. }
  3015. static struct task_struct *pick_next_task_fair(struct rq *rq)
  3016. {
  3017. struct task_struct *p;
  3018. struct cfs_rq *cfs_rq = &rq->cfs;
  3019. struct sched_entity *se;
  3020. if (!cfs_rq->nr_running)
  3021. return NULL;
  3022. do {
  3023. se = pick_next_entity(cfs_rq);
  3024. set_next_entity(cfs_rq, se);
  3025. cfs_rq = group_cfs_rq(se);
  3026. } while (cfs_rq);
  3027. p = task_of(se);
  3028. if (hrtick_enabled(rq))
  3029. hrtick_start_fair(rq, p);
  3030. return p;
  3031. }
  3032. /*
  3033. * Account for a descheduled task:
  3034. */
  3035. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  3036. {
  3037. struct sched_entity *se = &prev->se;
  3038. struct cfs_rq *cfs_rq;
  3039. for_each_sched_entity(se) {
  3040. cfs_rq = cfs_rq_of(se);
  3041. put_prev_entity(cfs_rq, se);
  3042. }
  3043. }
  3044. /*
  3045. * sched_yield() is very simple
  3046. *
  3047. * The magic of dealing with the ->skip buddy is in pick_next_entity.
  3048. */
  3049. static void yield_task_fair(struct rq *rq)
  3050. {
  3051. struct task_struct *curr = rq->curr;
  3052. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  3053. struct sched_entity *se = &curr->se;
  3054. /*
  3055. * Are we the only task in the tree?
  3056. */
  3057. if (unlikely(rq->nr_running == 1))
  3058. return;
  3059. clear_buddies(cfs_rq, se);
  3060. if (curr->policy != SCHED_BATCH) {
  3061. update_rq_clock(rq);
  3062. /*
  3063. * Update run-time statistics of the 'current'.
  3064. */
  3065. update_curr(cfs_rq);
  3066. /*
  3067. * Tell update_rq_clock() that we've just updated,
  3068. * so we don't do microscopic update in schedule()
  3069. * and double the fastpath cost.
  3070. */
  3071. rq->skip_clock_update = 1;
  3072. }
  3073. set_skip_buddy(se);
  3074. }
  3075. static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
  3076. {
  3077. struct sched_entity *se = &p->se;
  3078. /* throttled hierarchies are not runnable */
  3079. if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
  3080. return false;
  3081. /* Tell the scheduler that we'd really like pse to run next. */
  3082. set_next_buddy(se);
  3083. yield_task_fair(rq);
  3084. return true;
  3085. }
  3086. #ifdef CONFIG_SMP
  3087. /**************************************************
  3088. * Fair scheduling class load-balancing methods.
  3089. *
  3090. * BASICS
  3091. *
  3092. * The purpose of load-balancing is to achieve the same basic fairness the
  3093. * per-cpu scheduler provides, namely provide a proportional amount of compute
  3094. * time to each task. This is expressed in the following equation:
  3095. *
  3096. * W_i,n/P_i == W_j,n/P_j for all i,j (1)
  3097. *
  3098. * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight
  3099. * W_i,0 is defined as:
  3100. *
  3101. * W_i,0 = \Sum_j w_i,j (2)
  3102. *
  3103. * Where w_i,j is the weight of the j-th runnable task on cpu i. This weight
  3104. * is derived from the nice value as per prio_to_weight[].
  3105. *
  3106. * The weight average is an exponential decay average of the instantaneous
  3107. * weight:
  3108. *
  3109. * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
  3110. *
  3111. * P_i is the cpu power (or compute capacity) of cpu i, typically it is the
  3112. * fraction of 'recent' time available for SCHED_OTHER task execution. But it
  3113. * can also include other factors [XXX].
  3114. *
  3115. * To achieve this balance we define a measure of imbalance which follows
  3116. * directly from (1):
  3117. *
  3118. * imb_i,j = max{ avg(W/P), W_i/P_i } - min{ avg(W/P), W_j/P_j } (4)
  3119. *
  3120. * We them move tasks around to minimize the imbalance. In the continuous
  3121. * function space it is obvious this converges, in the discrete case we get
  3122. * a few fun cases generally called infeasible weight scenarios.
  3123. *
  3124. * [XXX expand on:
  3125. * - infeasible weights;
  3126. * - local vs global optima in the discrete case. ]
  3127. *
  3128. *
  3129. * SCHED DOMAINS
  3130. *
  3131. * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
  3132. * for all i,j solution, we create a tree of cpus that follows the hardware
  3133. * topology where each level pairs two lower groups (or better). This results
  3134. * in O(log n) layers. Furthermore we reduce the number of cpus going up the
  3135. * tree to only the first of the previous level and we decrease the frequency
  3136. * of load-balance at each level inv. proportional to the number of cpus in
  3137. * the groups.
  3138. *
  3139. * This yields:
  3140. *
  3141. * log_2 n 1 n
  3142. * \Sum { --- * --- * 2^i } = O(n) (5)
  3143. * i = 0 2^i 2^i
  3144. * `- size of each group
  3145. * | | `- number of cpus doing load-balance
  3146. * | `- freq
  3147. * `- sum over all levels
  3148. *
  3149. * Coupled with a limit on how many tasks we can migrate every balance pass,
  3150. * this makes (5) the runtime complexity of the balancer.
  3151. *
  3152. * An important property here is that each CPU is still (indirectly) connected
  3153. * to every other cpu in at most O(log n) steps:
  3154. *
  3155. * The adjacency matrix of the resulting graph is given by:
  3156. *
  3157. * log_2 n
  3158. * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
  3159. * k = 0
  3160. *
  3161. * And you'll find that:
  3162. *
  3163. * A^(log_2 n)_i,j != 0 for all i,j (7)
  3164. *
  3165. * Showing there's indeed a path between every cpu in at most O(log n) steps.
  3166. * The task movement gives a factor of O(m), giving a convergence complexity
  3167. * of:
  3168. *
  3169. * O(nm log n), n := nr_cpus, m := nr_tasks (8)
  3170. *
  3171. *
  3172. * WORK CONSERVING
  3173. *
  3174. * In order to avoid CPUs going idle while there's still work to do, new idle
  3175. * balancing is more aggressive and has the newly idle cpu iterate up the domain
  3176. * tree itself instead of relying on other CPUs to bring it work.
  3177. *
  3178. * This adds some complexity to both (5) and (8) but it reduces the total idle
  3179. * time.
  3180. *
  3181. * [XXX more?]
  3182. *
  3183. *
  3184. * CGROUPS
  3185. *
  3186. * Cgroups make a horror show out of (2), instead of a simple sum we get:
  3187. *
  3188. * s_k,i
  3189. * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
  3190. * S_k
  3191. *
  3192. * Where
  3193. *
  3194. * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
  3195. *
  3196. * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i.
  3197. *
  3198. * The big problem is S_k, its a global sum needed to compute a local (W_i)
  3199. * property.
  3200. *
  3201. * [XXX write more on how we solve this.. _after_ merging pjt's patches that
  3202. * rewrite all of this once again.]
  3203. */
  3204. static unsigned long __read_mostly max_load_balance_interval = HZ/10;
  3205. #define LBF_ALL_PINNED 0x01
  3206. #define LBF_NEED_BREAK 0x02
  3207. #define LBF_SOME_PINNED 0x04
  3208. struct lb_env {
  3209. struct sched_domain *sd;
  3210. struct rq *src_rq;
  3211. int src_cpu;
  3212. int dst_cpu;
  3213. struct rq *dst_rq;
  3214. struct cpumask *dst_grpmask;
  3215. int new_dst_cpu;
  3216. enum cpu_idle_type idle;
  3217. long imbalance;
  3218. /* The set of CPUs under consideration for load-balancing */
  3219. struct cpumask *cpus;
  3220. unsigned int flags;
  3221. unsigned int loop;
  3222. unsigned int loop_break;
  3223. unsigned int loop_max;
  3224. };
  3225. /*
  3226. * move_task - move a task from one runqueue to another runqueue.
  3227. * Both runqueues must be locked.
  3228. */
  3229. static void move_task(struct task_struct *p, struct lb_env *env)
  3230. {
  3231. deactivate_task(env->src_rq, p, 0);
  3232. set_task_cpu(p, env->dst_cpu);
  3233. activate_task(env->dst_rq, p, 0);
  3234. check_preempt_curr(env->dst_rq, p, 0);
  3235. }
  3236. /*
  3237. * Is this task likely cache-hot:
  3238. */
  3239. static int
  3240. task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
  3241. {
  3242. s64 delta;
  3243. if (p->sched_class != &fair_sched_class)
  3244. return 0;
  3245. if (unlikely(p->policy == SCHED_IDLE))
  3246. return 0;
  3247. /*
  3248. * Buddy candidates are cache hot:
  3249. */
  3250. if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
  3251. (&p->se == cfs_rq_of(&p->se)->next ||
  3252. &p->se == cfs_rq_of(&p->se)->last))
  3253. return 1;
  3254. if (sysctl_sched_migration_cost == -1)
  3255. return 1;
  3256. if (sysctl_sched_migration_cost == 0)
  3257. return 0;
  3258. delta = now - p->se.exec_start;
  3259. return delta < (s64)sysctl_sched_migration_cost;
  3260. }
  3261. /*
  3262. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  3263. */
  3264. static
  3265. int can_migrate_task(struct task_struct *p, struct lb_env *env)
  3266. {
  3267. int tsk_cache_hot = 0;
  3268. /*
  3269. * We do not migrate tasks that are:
  3270. * 1) running (obviously), or
  3271. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  3272. * 3) are cache-hot on their current CPU.
  3273. */
  3274. if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
  3275. int new_dst_cpu;
  3276. schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
  3277. /*
  3278. * Remember if this task can be migrated to any other cpu in
  3279. * our sched_group. We may want to revisit it if we couldn't
  3280. * meet load balance goals by pulling other tasks on src_cpu.
  3281. *
  3282. * Also avoid computing new_dst_cpu if we have already computed
  3283. * one in current iteration.
  3284. */
  3285. if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
  3286. return 0;
  3287. new_dst_cpu = cpumask_first_and(env->dst_grpmask,
  3288. tsk_cpus_allowed(p));
  3289. if (new_dst_cpu < nr_cpu_ids) {
  3290. env->flags |= LBF_SOME_PINNED;
  3291. env->new_dst_cpu = new_dst_cpu;
  3292. }
  3293. return 0;
  3294. }
  3295. /* Record that we found atleast one task that could run on dst_cpu */
  3296. env->flags &= ~LBF_ALL_PINNED;
  3297. if (task_running(env->src_rq, p)) {
  3298. schedstat_inc(p, se.statistics.nr_failed_migrations_running);
  3299. return 0;
  3300. }
  3301. /*
  3302. * Aggressive migration if:
  3303. * 1) task is cache cold, or
  3304. * 2) too many balance attempts have failed.
  3305. */
  3306. tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
  3307. if (!tsk_cache_hot ||
  3308. env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
  3309. #ifdef CONFIG_SCHEDSTATS
  3310. if (tsk_cache_hot) {
  3311. schedstat_inc(env->sd, lb_hot_gained[env->idle]);
  3312. schedstat_inc(p, se.statistics.nr_forced_migrations);
  3313. }
  3314. #endif
  3315. return 1;
  3316. }
  3317. if (tsk_cache_hot) {
  3318. schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
  3319. return 0;
  3320. }
  3321. return 1;
  3322. }
  3323. /*
  3324. * move_one_task tries to move exactly one task from busiest to this_rq, as
  3325. * part of active balancing operations within "domain".
  3326. * Returns 1 if successful and 0 otherwise.
  3327. *
  3328. * Called with both runqueues locked.
  3329. */
  3330. static int move_one_task(struct lb_env *env)
  3331. {
  3332. struct task_struct *p, *n;
  3333. list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
  3334. if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
  3335. continue;
  3336. if (!can_migrate_task(p, env))
  3337. continue;
  3338. move_task(p, env);
  3339. /*
  3340. * Right now, this is only the second place move_task()
  3341. * is called, so we can safely collect move_task()
  3342. * stats here rather than inside move_task().
  3343. */
  3344. schedstat_inc(env->sd, lb_gained[env->idle]);
  3345. return 1;
  3346. }
  3347. return 0;
  3348. }
  3349. static unsigned long task_h_load(struct task_struct *p);
  3350. static const unsigned int sched_nr_migrate_break = 32;
  3351. /*
  3352. * move_tasks tries to move up to imbalance weighted load from busiest to
  3353. * this_rq, as part of a balancing operation within domain "sd".
  3354. * Returns 1 if successful and 0 otherwise.
  3355. *
  3356. * Called with both runqueues locked.
  3357. */
  3358. static int move_tasks(struct lb_env *env)
  3359. {
  3360. struct list_head *tasks = &env->src_rq->cfs_tasks;
  3361. struct task_struct *p;
  3362. unsigned long load;
  3363. int pulled = 0;
  3364. if (env->imbalance <= 0)
  3365. return 0;
  3366. while (!list_empty(tasks)) {
  3367. p = list_first_entry(tasks, struct task_struct, se.group_node);
  3368. env->loop++;
  3369. /* We've more or less seen every task there is, call it quits */
  3370. if (env->loop > env->loop_max)
  3371. break;
  3372. /* take a breather every nr_migrate tasks */
  3373. if (env->loop > env->loop_break) {
  3374. env->loop_break += sched_nr_migrate_break;
  3375. env->flags |= LBF_NEED_BREAK;
  3376. break;
  3377. }
  3378. if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
  3379. goto next;
  3380. load = task_h_load(p);
  3381. if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
  3382. goto next;
  3383. if ((load / 2) > env->imbalance)
  3384. goto next;
  3385. if (!can_migrate_task(p, env))
  3386. goto next;
  3387. move_task(p, env);
  3388. pulled++;
  3389. env->imbalance -= load;
  3390. #ifdef CONFIG_PREEMPT
  3391. /*
  3392. * NEWIDLE balancing is a source of latency, so preemptible
  3393. * kernels will stop after the first task is pulled to minimize
  3394. * the critical section.
  3395. */
  3396. if (env->idle == CPU_NEWLY_IDLE)
  3397. break;
  3398. #endif
  3399. /*
  3400. * We only want to steal up to the prescribed amount of
  3401. * weighted load.
  3402. */
  3403. if (env->imbalance <= 0)
  3404. break;
  3405. continue;
  3406. next:
  3407. list_move_tail(&p->se.group_node, tasks);
  3408. }
  3409. /*
  3410. * Right now, this is one of only two places move_task() is called,
  3411. * so we can safely collect move_task() stats here rather than
  3412. * inside move_task().
  3413. */
  3414. schedstat_add(env->sd, lb_gained[env->idle], pulled);
  3415. return pulled;
  3416. }
  3417. #ifdef CONFIG_FAIR_GROUP_SCHED
  3418. /*
  3419. * update tg->load_weight by folding this cpu's load_avg
  3420. */
  3421. static void __update_blocked_averages_cpu(struct task_group *tg, int cpu)
  3422. {
  3423. struct sched_entity *se = tg->se[cpu];
  3424. struct cfs_rq *cfs_rq = tg->cfs_rq[cpu];
  3425. /* throttled entities do not contribute to load */
  3426. if (throttled_hierarchy(cfs_rq))
  3427. return;
  3428. update_cfs_rq_blocked_load(cfs_rq, 1);
  3429. if (se) {
  3430. update_entity_load_avg(se, 1);
  3431. /*
  3432. * We pivot on our runnable average having decayed to zero for
  3433. * list removal. This generally implies that all our children
  3434. * have also been removed (modulo rounding error or bandwidth
  3435. * control); however, such cases are rare and we can fix these
  3436. * at enqueue.
  3437. *
  3438. * TODO: fix up out-of-order children on enqueue.
  3439. */
  3440. if (!se->avg.runnable_avg_sum && !cfs_rq->nr_running)
  3441. list_del_leaf_cfs_rq(cfs_rq);
  3442. } else {
  3443. struct rq *rq = rq_of(cfs_rq);
  3444. update_rq_runnable_avg(rq, rq->nr_running);
  3445. }
  3446. }
  3447. static void update_blocked_averages(int cpu)
  3448. {
  3449. struct rq *rq = cpu_rq(cpu);
  3450. struct cfs_rq *cfs_rq;
  3451. unsigned long flags;
  3452. raw_spin_lock_irqsave(&rq->lock, flags);
  3453. update_rq_clock(rq);
  3454. /*
  3455. * Iterates the task_group tree in a bottom up fashion, see
  3456. * list_add_leaf_cfs_rq() for details.
  3457. */
  3458. for_each_leaf_cfs_rq(rq, cfs_rq) {
  3459. /*
  3460. * Note: We may want to consider periodically releasing
  3461. * rq->lock about these updates so that creating many task
  3462. * groups does not result in continually extending hold time.
  3463. */
  3464. __update_blocked_averages_cpu(cfs_rq->tg, rq->cpu);
  3465. }
  3466. raw_spin_unlock_irqrestore(&rq->lock, flags);
  3467. }
  3468. /*
  3469. * Compute the cpu's hierarchical load factor for each task group.
  3470. * This needs to be done in a top-down fashion because the load of a child
  3471. * group is a fraction of its parents load.
  3472. */
  3473. static int tg_load_down(struct task_group *tg, void *data)
  3474. {
  3475. unsigned long load;
  3476. long cpu = (long)data;
  3477. if (!tg->parent) {
  3478. load = cpu_rq(cpu)->load.weight;
  3479. } else {
  3480. load = tg->parent->cfs_rq[cpu]->h_load;
  3481. load *= tg->se[cpu]->load.weight;
  3482. load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
  3483. }
  3484. tg->cfs_rq[cpu]->h_load = load;
  3485. return 0;
  3486. }
  3487. static void update_h_load(long cpu)
  3488. {
  3489. struct rq *rq = cpu_rq(cpu);
  3490. unsigned long now = jiffies;
  3491. if (rq->h_load_throttle == now)
  3492. return;
  3493. rq->h_load_throttle = now;
  3494. rcu_read_lock();
  3495. walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
  3496. rcu_read_unlock();
  3497. }
  3498. static unsigned long task_h_load(struct task_struct *p)
  3499. {
  3500. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  3501. unsigned long load;
  3502. load = p->se.load.weight;
  3503. load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
  3504. return load;
  3505. }
  3506. #else
  3507. static inline void update_blocked_averages(int cpu)
  3508. {
  3509. }
  3510. static inline void update_h_load(long cpu)
  3511. {
  3512. }
  3513. static unsigned long task_h_load(struct task_struct *p)
  3514. {
  3515. return p->se.load.weight;
  3516. }
  3517. #endif
  3518. /********** Helpers for find_busiest_group ************************/
  3519. /*
  3520. * sd_lb_stats - Structure to store the statistics of a sched_domain
  3521. * during load balancing.
  3522. */
  3523. struct sd_lb_stats {
  3524. struct sched_group *busiest; /* Busiest group in this sd */
  3525. struct sched_group *this; /* Local group in this sd */
  3526. unsigned long total_load; /* Total load of all groups in sd */
  3527. unsigned long total_pwr; /* Total power of all groups in sd */
  3528. unsigned long avg_load; /* Average load across all groups in sd */
  3529. /** Statistics of this group */
  3530. unsigned long this_load;
  3531. unsigned long this_load_per_task;
  3532. unsigned long this_nr_running;
  3533. unsigned long this_has_capacity;
  3534. unsigned int this_idle_cpus;
  3535. /* Statistics of the busiest group */
  3536. unsigned int busiest_idle_cpus;
  3537. unsigned long max_load;
  3538. unsigned long busiest_load_per_task;
  3539. unsigned long busiest_nr_running;
  3540. unsigned long busiest_group_capacity;
  3541. unsigned long busiest_has_capacity;
  3542. unsigned int busiest_group_weight;
  3543. int group_imb; /* Is there imbalance in this sd */
  3544. };
  3545. /*
  3546. * sg_lb_stats - stats of a sched_group required for load_balancing
  3547. */
  3548. struct sg_lb_stats {
  3549. unsigned long avg_load; /*Avg load across the CPUs of the group */
  3550. unsigned long group_load; /* Total load over the CPUs of the group */
  3551. unsigned long sum_nr_running; /* Nr tasks running in the group */
  3552. unsigned long sum_weighted_load; /* Weighted load of group's tasks */
  3553. unsigned long group_capacity;
  3554. unsigned long idle_cpus;
  3555. unsigned long group_weight;
  3556. int group_imb; /* Is there an imbalance in the group ? */
  3557. int group_has_capacity; /* Is there extra capacity in the group? */
  3558. };
  3559. /**
  3560. * get_sd_load_idx - Obtain the load index for a given sched domain.
  3561. * @sd: The sched_domain whose load_idx is to be obtained.
  3562. * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
  3563. */
  3564. static inline int get_sd_load_idx(struct sched_domain *sd,
  3565. enum cpu_idle_type idle)
  3566. {
  3567. int load_idx;
  3568. switch (idle) {
  3569. case CPU_NOT_IDLE:
  3570. load_idx = sd->busy_idx;
  3571. break;
  3572. case CPU_NEWLY_IDLE:
  3573. load_idx = sd->newidle_idx;
  3574. break;
  3575. default:
  3576. load_idx = sd->idle_idx;
  3577. break;
  3578. }
  3579. return load_idx;
  3580. }
  3581. unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
  3582. {
  3583. return SCHED_POWER_SCALE;
  3584. }
  3585. unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
  3586. {
  3587. return default_scale_freq_power(sd, cpu);
  3588. }
  3589. unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
  3590. {
  3591. unsigned long weight = sd->span_weight;
  3592. unsigned long smt_gain = sd->smt_gain;
  3593. smt_gain /= weight;
  3594. return smt_gain;
  3595. }
  3596. unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
  3597. {
  3598. return default_scale_smt_power(sd, cpu);
  3599. }
  3600. unsigned long scale_rt_power(int cpu)
  3601. {
  3602. struct rq *rq = cpu_rq(cpu);
  3603. u64 total, available, age_stamp, avg;
  3604. /*
  3605. * Since we're reading these variables without serialization make sure
  3606. * we read them once before doing sanity checks on them.
  3607. */
  3608. age_stamp = ACCESS_ONCE(rq->age_stamp);
  3609. avg = ACCESS_ONCE(rq->rt_avg);
  3610. total = sched_avg_period() + (rq->clock - age_stamp);
  3611. if (unlikely(total < avg)) {
  3612. /* Ensures that power won't end up being negative */
  3613. available = 0;
  3614. } else {
  3615. available = total - avg;
  3616. }
  3617. if (unlikely((s64)total < SCHED_POWER_SCALE))
  3618. total = SCHED_POWER_SCALE;
  3619. total >>= SCHED_POWER_SHIFT;
  3620. return div_u64(available, total);
  3621. }
  3622. static void update_cpu_power(struct sched_domain *sd, int cpu)
  3623. {
  3624. unsigned long weight = sd->span_weight;
  3625. unsigned long power = SCHED_POWER_SCALE;
  3626. struct sched_group *sdg = sd->groups;
  3627. if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
  3628. if (sched_feat(ARCH_POWER))
  3629. power *= arch_scale_smt_power(sd, cpu);
  3630. else
  3631. power *= default_scale_smt_power(sd, cpu);
  3632. power >>= SCHED_POWER_SHIFT;
  3633. }
  3634. sdg->sgp->power_orig = power;
  3635. if (sched_feat(ARCH_POWER))
  3636. power *= arch_scale_freq_power(sd, cpu);
  3637. else
  3638. power *= default_scale_freq_power(sd, cpu);
  3639. power >>= SCHED_POWER_SHIFT;
  3640. power *= scale_rt_power(cpu);
  3641. power >>= SCHED_POWER_SHIFT;
  3642. if (!power)
  3643. power = 1;
  3644. cpu_rq(cpu)->cpu_power = power;
  3645. sdg->sgp->power = power;
  3646. }
  3647. void update_group_power(struct sched_domain *sd, int cpu)
  3648. {
  3649. struct sched_domain *child = sd->child;
  3650. struct sched_group *group, *sdg = sd->groups;
  3651. unsigned long power;
  3652. unsigned long interval;
  3653. interval = msecs_to_jiffies(sd->balance_interval);
  3654. interval = clamp(interval, 1UL, max_load_balance_interval);
  3655. sdg->sgp->next_update = jiffies + interval;
  3656. if (!child) {
  3657. update_cpu_power(sd, cpu);
  3658. return;
  3659. }
  3660. power = 0;
  3661. if (child->flags & SD_OVERLAP) {
  3662. /*
  3663. * SD_OVERLAP domains cannot assume that child groups
  3664. * span the current group.
  3665. */
  3666. for_each_cpu(cpu, sched_group_cpus(sdg))
  3667. power += power_of(cpu);
  3668. } else {
  3669. /*
  3670. * !SD_OVERLAP domains can assume that child groups
  3671. * span the current group.
  3672. */
  3673. group = child->groups;
  3674. do {
  3675. power += group->sgp->power;
  3676. group = group->next;
  3677. } while (group != child->groups);
  3678. }
  3679. sdg->sgp->power_orig = sdg->sgp->power = power;
  3680. }
  3681. /*
  3682. * Try and fix up capacity for tiny siblings, this is needed when
  3683. * things like SD_ASYM_PACKING need f_b_g to select another sibling
  3684. * which on its own isn't powerful enough.
  3685. *
  3686. * See update_sd_pick_busiest() and check_asym_packing().
  3687. */
  3688. static inline int
  3689. fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
  3690. {
  3691. /*
  3692. * Only siblings can have significantly less than SCHED_POWER_SCALE
  3693. */
  3694. if (!(sd->flags & SD_SHARE_CPUPOWER))
  3695. return 0;
  3696. /*
  3697. * If ~90% of the cpu_power is still there, we're good.
  3698. */
  3699. if (group->sgp->power * 32 > group->sgp->power_orig * 29)
  3700. return 1;
  3701. return 0;
  3702. }
  3703. /**
  3704. * update_sg_lb_stats - Update sched_group's statistics for load balancing.
  3705. * @env: The load balancing environment.
  3706. * @group: sched_group whose statistics are to be updated.
  3707. * @load_idx: Load index of sched_domain of this_cpu for load calc.
  3708. * @local_group: Does group contain this_cpu.
  3709. * @balance: Should we balance.
  3710. * @sgs: variable to hold the statistics for this group.
  3711. */
  3712. static inline void update_sg_lb_stats(struct lb_env *env,
  3713. struct sched_group *group, int load_idx,
  3714. int local_group, int *balance, struct sg_lb_stats *sgs)
  3715. {
  3716. unsigned long nr_running, max_nr_running, min_nr_running;
  3717. unsigned long load, max_cpu_load, min_cpu_load;
  3718. unsigned int balance_cpu = -1, first_idle_cpu = 0;
  3719. unsigned long avg_load_per_task = 0;
  3720. int i;
  3721. if (local_group)
  3722. balance_cpu = group_balance_cpu(group);
  3723. /* Tally up the load of all CPUs in the group */
  3724. max_cpu_load = 0;
  3725. min_cpu_load = ~0UL;
  3726. max_nr_running = 0;
  3727. min_nr_running = ~0UL;
  3728. for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
  3729. struct rq *rq = cpu_rq(i);
  3730. nr_running = rq->nr_running;
  3731. /* Bias balancing toward cpus of our domain */
  3732. if (local_group) {
  3733. if (idle_cpu(i) && !first_idle_cpu &&
  3734. cpumask_test_cpu(i, sched_group_mask(group))) {
  3735. first_idle_cpu = 1;
  3736. balance_cpu = i;
  3737. }
  3738. load = target_load(i, load_idx);
  3739. } else {
  3740. load = source_load(i, load_idx);
  3741. if (load > max_cpu_load)
  3742. max_cpu_load = load;
  3743. if (min_cpu_load > load)
  3744. min_cpu_load = load;
  3745. if (nr_running > max_nr_running)
  3746. max_nr_running = nr_running;
  3747. if (min_nr_running > nr_running)
  3748. min_nr_running = nr_running;
  3749. }
  3750. sgs->group_load += load;
  3751. sgs->sum_nr_running += nr_running;
  3752. sgs->sum_weighted_load += weighted_cpuload(i);
  3753. if (idle_cpu(i))
  3754. sgs->idle_cpus++;
  3755. }
  3756. /*
  3757. * First idle cpu or the first cpu(busiest) in this sched group
  3758. * is eligible for doing load balancing at this and above
  3759. * domains. In the newly idle case, we will allow all the cpu's
  3760. * to do the newly idle load balance.
  3761. */
  3762. if (local_group) {
  3763. if (env->idle != CPU_NEWLY_IDLE) {
  3764. if (balance_cpu != env->dst_cpu) {
  3765. *balance = 0;
  3766. return;
  3767. }
  3768. update_group_power(env->sd, env->dst_cpu);
  3769. } else if (time_after_eq(jiffies, group->sgp->next_update))
  3770. update_group_power(env->sd, env->dst_cpu);
  3771. }
  3772. /* Adjust by relative CPU power of the group */
  3773. sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
  3774. /*
  3775. * Consider the group unbalanced when the imbalance is larger
  3776. * than the average weight of a task.
  3777. *
  3778. * APZ: with cgroup the avg task weight can vary wildly and
  3779. * might not be a suitable number - should we keep a
  3780. * normalized nr_running number somewhere that negates
  3781. * the hierarchy?
  3782. */
  3783. if (sgs->sum_nr_running)
  3784. avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
  3785. if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
  3786. (max_nr_running - min_nr_running) > 1)
  3787. sgs->group_imb = 1;
  3788. sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
  3789. SCHED_POWER_SCALE);
  3790. if (!sgs->group_capacity)
  3791. sgs->group_capacity = fix_small_capacity(env->sd, group);
  3792. sgs->group_weight = group->group_weight;
  3793. if (sgs->group_capacity > sgs->sum_nr_running)
  3794. sgs->group_has_capacity = 1;
  3795. }
  3796. /**
  3797. * update_sd_pick_busiest - return 1 on busiest group
  3798. * @env: The load balancing environment.
  3799. * @sds: sched_domain statistics
  3800. * @sg: sched_group candidate to be checked for being the busiest
  3801. * @sgs: sched_group statistics
  3802. *
  3803. * Determine if @sg is a busier group than the previously selected
  3804. * busiest group.
  3805. */
  3806. static bool update_sd_pick_busiest(struct lb_env *env,
  3807. struct sd_lb_stats *sds,
  3808. struct sched_group *sg,
  3809. struct sg_lb_stats *sgs)
  3810. {
  3811. if (sgs->avg_load <= sds->max_load)
  3812. return false;
  3813. if (sgs->sum_nr_running > sgs->group_capacity)
  3814. return true;
  3815. if (sgs->group_imb)
  3816. return true;
  3817. /*
  3818. * ASYM_PACKING needs to move all the work to the lowest
  3819. * numbered CPUs in the group, therefore mark all groups
  3820. * higher than ourself as busy.
  3821. */
  3822. if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
  3823. env->dst_cpu < group_first_cpu(sg)) {
  3824. if (!sds->busiest)
  3825. return true;
  3826. if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
  3827. return true;
  3828. }
  3829. return false;
  3830. }
  3831. /**
  3832. * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
  3833. * @env: The load balancing environment.
  3834. * @balance: Should we balance.
  3835. * @sds: variable to hold the statistics for this sched_domain.
  3836. */
  3837. static inline void update_sd_lb_stats(struct lb_env *env,
  3838. int *balance, struct sd_lb_stats *sds)
  3839. {
  3840. struct sched_domain *child = env->sd->child;
  3841. struct sched_group *sg = env->sd->groups;
  3842. struct sg_lb_stats sgs;
  3843. int load_idx, prefer_sibling = 0;
  3844. if (child && child->flags & SD_PREFER_SIBLING)
  3845. prefer_sibling = 1;
  3846. load_idx = get_sd_load_idx(env->sd, env->idle);
  3847. do {
  3848. int local_group;
  3849. local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
  3850. memset(&sgs, 0, sizeof(sgs));
  3851. update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
  3852. if (local_group && !(*balance))
  3853. return;
  3854. sds->total_load += sgs.group_load;
  3855. sds->total_pwr += sg->sgp->power;
  3856. /*
  3857. * In case the child domain prefers tasks go to siblings
  3858. * first, lower the sg capacity to one so that we'll try
  3859. * and move all the excess tasks away. We lower the capacity
  3860. * of a group only if the local group has the capacity to fit
  3861. * these excess tasks, i.e. nr_running < group_capacity. The
  3862. * extra check prevents the case where you always pull from the
  3863. * heaviest group when it is already under-utilized (possible
  3864. * with a large weight task outweighs the tasks on the system).
  3865. */
  3866. if (prefer_sibling && !local_group && sds->this_has_capacity)
  3867. sgs.group_capacity = min(sgs.group_capacity, 1UL);
  3868. if (local_group) {
  3869. sds->this_load = sgs.avg_load;
  3870. sds->this = sg;
  3871. sds->this_nr_running = sgs.sum_nr_running;
  3872. sds->this_load_per_task = sgs.sum_weighted_load;
  3873. sds->this_has_capacity = sgs.group_has_capacity;
  3874. sds->this_idle_cpus = sgs.idle_cpus;
  3875. } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
  3876. sds->max_load = sgs.avg_load;
  3877. sds->busiest = sg;
  3878. sds->busiest_nr_running = sgs.sum_nr_running;
  3879. sds->busiest_idle_cpus = sgs.idle_cpus;
  3880. sds->busiest_group_capacity = sgs.group_capacity;
  3881. sds->busiest_load_per_task = sgs.sum_weighted_load;
  3882. sds->busiest_has_capacity = sgs.group_has_capacity;
  3883. sds->busiest_group_weight = sgs.group_weight;
  3884. sds->group_imb = sgs.group_imb;
  3885. }
  3886. sg = sg->next;
  3887. } while (sg != env->sd->groups);
  3888. }
  3889. /**
  3890. * check_asym_packing - Check to see if the group is packed into the
  3891. * sched doman.
  3892. *
  3893. * This is primarily intended to used at the sibling level. Some
  3894. * cores like POWER7 prefer to use lower numbered SMT threads. In the
  3895. * case of POWER7, it can move to lower SMT modes only when higher
  3896. * threads are idle. When in lower SMT modes, the threads will
  3897. * perform better since they share less core resources. Hence when we
  3898. * have idle threads, we want them to be the higher ones.
  3899. *
  3900. * This packing function is run on idle threads. It checks to see if
  3901. * the busiest CPU in this domain (core in the P7 case) has a higher
  3902. * CPU number than the packing function is being run on. Here we are
  3903. * assuming lower CPU number will be equivalent to lower a SMT thread
  3904. * number.
  3905. *
  3906. * Returns 1 when packing is required and a task should be moved to
  3907. * this CPU. The amount of the imbalance is returned in *imbalance.
  3908. *
  3909. * @env: The load balancing environment.
  3910. * @sds: Statistics of the sched_domain which is to be packed
  3911. */
  3912. static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
  3913. {
  3914. int busiest_cpu;
  3915. if (!(env->sd->flags & SD_ASYM_PACKING))
  3916. return 0;
  3917. if (!sds->busiest)
  3918. return 0;
  3919. busiest_cpu = group_first_cpu(sds->busiest);
  3920. if (env->dst_cpu > busiest_cpu)
  3921. return 0;
  3922. env->imbalance = DIV_ROUND_CLOSEST(
  3923. sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
  3924. return 1;
  3925. }
  3926. /**
  3927. * fix_small_imbalance - Calculate the minor imbalance that exists
  3928. * amongst the groups of a sched_domain, during
  3929. * load balancing.
  3930. * @env: The load balancing environment.
  3931. * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
  3932. */
  3933. static inline
  3934. void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
  3935. {
  3936. unsigned long tmp, pwr_now = 0, pwr_move = 0;
  3937. unsigned int imbn = 2;
  3938. unsigned long scaled_busy_load_per_task;
  3939. if (sds->this_nr_running) {
  3940. sds->this_load_per_task /= sds->this_nr_running;
  3941. if (sds->busiest_load_per_task >
  3942. sds->this_load_per_task)
  3943. imbn = 1;
  3944. } else {
  3945. sds->this_load_per_task =
  3946. cpu_avg_load_per_task(env->dst_cpu);
  3947. }
  3948. scaled_busy_load_per_task = sds->busiest_load_per_task
  3949. * SCHED_POWER_SCALE;
  3950. scaled_busy_load_per_task /= sds->busiest->sgp->power;
  3951. if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
  3952. (scaled_busy_load_per_task * imbn)) {
  3953. env->imbalance = sds->busiest_load_per_task;
  3954. return;
  3955. }
  3956. /*
  3957. * OK, we don't have enough imbalance to justify moving tasks,
  3958. * however we may be able to increase total CPU power used by
  3959. * moving them.
  3960. */
  3961. pwr_now += sds->busiest->sgp->power *
  3962. min(sds->busiest_load_per_task, sds->max_load);
  3963. pwr_now += sds->this->sgp->power *
  3964. min(sds->this_load_per_task, sds->this_load);
  3965. pwr_now /= SCHED_POWER_SCALE;
  3966. /* Amount of load we'd subtract */
  3967. tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
  3968. sds->busiest->sgp->power;
  3969. if (sds->max_load > tmp)
  3970. pwr_move += sds->busiest->sgp->power *
  3971. min(sds->busiest_load_per_task, sds->max_load - tmp);
  3972. /* Amount of load we'd add */
  3973. if (sds->max_load * sds->busiest->sgp->power <
  3974. sds->busiest_load_per_task * SCHED_POWER_SCALE)
  3975. tmp = (sds->max_load * sds->busiest->sgp->power) /
  3976. sds->this->sgp->power;
  3977. else
  3978. tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
  3979. sds->this->sgp->power;
  3980. pwr_move += sds->this->sgp->power *
  3981. min(sds->this_load_per_task, sds->this_load + tmp);
  3982. pwr_move /= SCHED_POWER_SCALE;
  3983. /* Move if we gain throughput */
  3984. if (pwr_move > pwr_now)
  3985. env->imbalance = sds->busiest_load_per_task;
  3986. }
  3987. /**
  3988. * calculate_imbalance - Calculate the amount of imbalance present within the
  3989. * groups of a given sched_domain during load balance.
  3990. * @env: load balance environment
  3991. * @sds: statistics of the sched_domain whose imbalance is to be calculated.
  3992. */
  3993. static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
  3994. {
  3995. unsigned long max_pull, load_above_capacity = ~0UL;
  3996. sds->busiest_load_per_task /= sds->busiest_nr_running;
  3997. if (sds->group_imb) {
  3998. sds->busiest_load_per_task =
  3999. min(sds->busiest_load_per_task, sds->avg_load);
  4000. }
  4001. /*
  4002. * In the presence of smp nice balancing, certain scenarios can have
  4003. * max load less than avg load(as we skip the groups at or below
  4004. * its cpu_power, while calculating max_load..)
  4005. */
  4006. if (sds->max_load < sds->avg_load) {
  4007. env->imbalance = 0;
  4008. return fix_small_imbalance(env, sds);
  4009. }
  4010. if (!sds->group_imb) {
  4011. /*
  4012. * Don't want to pull so many tasks that a group would go idle.
  4013. */
  4014. load_above_capacity = (sds->busiest_nr_running -
  4015. sds->busiest_group_capacity);
  4016. load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
  4017. load_above_capacity /= sds->busiest->sgp->power;
  4018. }
  4019. /*
  4020. * We're trying to get all the cpus to the average_load, so we don't
  4021. * want to push ourselves above the average load, nor do we wish to
  4022. * reduce the max loaded cpu below the average load. At the same time,
  4023. * we also don't want to reduce the group load below the group capacity
  4024. * (so that we can implement power-savings policies etc). Thus we look
  4025. * for the minimum possible imbalance.
  4026. * Be careful of negative numbers as they'll appear as very large values
  4027. * with unsigned longs.
  4028. */
  4029. max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
  4030. /* How much load to actually move to equalise the imbalance */
  4031. env->imbalance = min(max_pull * sds->busiest->sgp->power,
  4032. (sds->avg_load - sds->this_load) * sds->this->sgp->power)
  4033. / SCHED_POWER_SCALE;
  4034. /*
  4035. * if *imbalance is less than the average load per runnable task
  4036. * there is no guarantee that any tasks will be moved so we'll have
  4037. * a think about bumping its value to force at least one task to be
  4038. * moved
  4039. */
  4040. if (env->imbalance < sds->busiest_load_per_task)
  4041. return fix_small_imbalance(env, sds);
  4042. }
  4043. /******* find_busiest_group() helpers end here *********************/
  4044. /**
  4045. * find_busiest_group - Returns the busiest group within the sched_domain
  4046. * if there is an imbalance. If there isn't an imbalance, and
  4047. * the user has opted for power-savings, it returns a group whose
  4048. * CPUs can be put to idle by rebalancing those tasks elsewhere, if
  4049. * such a group exists.
  4050. *
  4051. * Also calculates the amount of weighted load which should be moved
  4052. * to restore balance.
  4053. *
  4054. * @env: The load balancing environment.
  4055. * @balance: Pointer to a variable indicating if this_cpu
  4056. * is the appropriate cpu to perform load balancing at this_level.
  4057. *
  4058. * Returns: - the busiest group if imbalance exists.
  4059. * - If no imbalance and user has opted for power-savings balance,
  4060. * return the least loaded group whose CPUs can be
  4061. * put to idle by rebalancing its tasks onto our group.
  4062. */
  4063. static struct sched_group *
  4064. find_busiest_group(struct lb_env *env, int *balance)
  4065. {
  4066. struct sd_lb_stats sds;
  4067. memset(&sds, 0, sizeof(sds));
  4068. /*
  4069. * Compute the various statistics relavent for load balancing at
  4070. * this level.
  4071. */
  4072. update_sd_lb_stats(env, balance, &sds);
  4073. /*
  4074. * this_cpu is not the appropriate cpu to perform load balancing at
  4075. * this level.
  4076. */
  4077. if (!(*balance))
  4078. goto ret;
  4079. if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
  4080. check_asym_packing(env, &sds))
  4081. return sds.busiest;
  4082. /* There is no busy sibling group to pull tasks from */
  4083. if (!sds.busiest || sds.busiest_nr_running == 0)
  4084. goto out_balanced;
  4085. sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
  4086. /*
  4087. * If the busiest group is imbalanced the below checks don't
  4088. * work because they assumes all things are equal, which typically
  4089. * isn't true due to cpus_allowed constraints and the like.
  4090. */
  4091. if (sds.group_imb)
  4092. goto force_balance;
  4093. /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
  4094. if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
  4095. !sds.busiest_has_capacity)
  4096. goto force_balance;
  4097. /*
  4098. * If the local group is more busy than the selected busiest group
  4099. * don't try and pull any tasks.
  4100. */
  4101. if (sds.this_load >= sds.max_load)
  4102. goto out_balanced;
  4103. /*
  4104. * Don't pull any tasks if this group is already above the domain
  4105. * average load.
  4106. */
  4107. if (sds.this_load >= sds.avg_load)
  4108. goto out_balanced;
  4109. if (env->idle == CPU_IDLE) {
  4110. /*
  4111. * This cpu is idle. If the busiest group load doesn't
  4112. * have more tasks than the number of available cpu's and
  4113. * there is no imbalance between this and busiest group
  4114. * wrt to idle cpu's, it is balanced.
  4115. */
  4116. if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
  4117. sds.busiest_nr_running <= sds.busiest_group_weight)
  4118. goto out_balanced;
  4119. } else {
  4120. /*
  4121. * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
  4122. * imbalance_pct to be conservative.
  4123. */
  4124. if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
  4125. goto out_balanced;
  4126. }
  4127. force_balance:
  4128. /* Looks like there is an imbalance. Compute it */
  4129. calculate_imbalance(env, &sds);
  4130. return sds.busiest;
  4131. out_balanced:
  4132. ret:
  4133. env->imbalance = 0;
  4134. return NULL;
  4135. }
  4136. /*
  4137. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  4138. */
  4139. static struct rq *find_busiest_queue(struct lb_env *env,
  4140. struct sched_group *group)
  4141. {
  4142. struct rq *busiest = NULL, *rq;
  4143. unsigned long max_load = 0;
  4144. int i;
  4145. for_each_cpu(i, sched_group_cpus(group)) {
  4146. unsigned long power = power_of(i);
  4147. unsigned long capacity = DIV_ROUND_CLOSEST(power,
  4148. SCHED_POWER_SCALE);
  4149. unsigned long wl;
  4150. if (!capacity)
  4151. capacity = fix_small_capacity(env->sd, group);
  4152. if (!cpumask_test_cpu(i, env->cpus))
  4153. continue;
  4154. rq = cpu_rq(i);
  4155. wl = weighted_cpuload(i);
  4156. /*
  4157. * When comparing with imbalance, use weighted_cpuload()
  4158. * which is not scaled with the cpu power.
  4159. */
  4160. if (capacity && rq->nr_running == 1 && wl > env->imbalance)
  4161. continue;
  4162. /*
  4163. * For the load comparisons with the other cpu's, consider
  4164. * the weighted_cpuload() scaled with the cpu power, so that
  4165. * the load can be moved away from the cpu that is potentially
  4166. * running at a lower capacity.
  4167. */
  4168. wl = (wl * SCHED_POWER_SCALE) / power;
  4169. if (wl > max_load) {
  4170. max_load = wl;
  4171. busiest = rq;
  4172. }
  4173. }
  4174. return busiest;
  4175. }
  4176. /*
  4177. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  4178. * so long as it is large enough.
  4179. */
  4180. #define MAX_PINNED_INTERVAL 512
  4181. /* Working cpumask for load_balance and load_balance_newidle. */
  4182. DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
  4183. static int need_active_balance(struct lb_env *env)
  4184. {
  4185. struct sched_domain *sd = env->sd;
  4186. if (env->idle == CPU_NEWLY_IDLE) {
  4187. /*
  4188. * ASYM_PACKING needs to force migrate tasks from busy but
  4189. * higher numbered CPUs in order to pack all tasks in the
  4190. * lowest numbered CPUs.
  4191. */
  4192. if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
  4193. return 1;
  4194. }
  4195. return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
  4196. }
  4197. static int active_load_balance_cpu_stop(void *data);
  4198. /*
  4199. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  4200. * tasks if there is an imbalance.
  4201. */
  4202. static int load_balance(int this_cpu, struct rq *this_rq,
  4203. struct sched_domain *sd, enum cpu_idle_type idle,
  4204. int *balance)
  4205. {
  4206. int ld_moved, cur_ld_moved, active_balance = 0;
  4207. int lb_iterations, max_lb_iterations;
  4208. struct sched_group *group;
  4209. struct rq *busiest;
  4210. unsigned long flags;
  4211. struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
  4212. struct lb_env env = {
  4213. .sd = sd,
  4214. .dst_cpu = this_cpu,
  4215. .dst_rq = this_rq,
  4216. .dst_grpmask = sched_group_cpus(sd->groups),
  4217. .idle = idle,
  4218. .loop_break = sched_nr_migrate_break,
  4219. .cpus = cpus,
  4220. };
  4221. cpumask_copy(cpus, cpu_active_mask);
  4222. max_lb_iterations = cpumask_weight(env.dst_grpmask);
  4223. schedstat_inc(sd, lb_count[idle]);
  4224. redo:
  4225. group = find_busiest_group(&env, balance);
  4226. if (*balance == 0)
  4227. goto out_balanced;
  4228. if (!group) {
  4229. schedstat_inc(sd, lb_nobusyg[idle]);
  4230. goto out_balanced;
  4231. }
  4232. busiest = find_busiest_queue(&env, group);
  4233. if (!busiest) {
  4234. schedstat_inc(sd, lb_nobusyq[idle]);
  4235. goto out_balanced;
  4236. }
  4237. BUG_ON(busiest == env.dst_rq);
  4238. schedstat_add(sd, lb_imbalance[idle], env.imbalance);
  4239. ld_moved = 0;
  4240. lb_iterations = 1;
  4241. if (busiest->nr_running > 1) {
  4242. /*
  4243. * Attempt to move tasks. If find_busiest_group has found
  4244. * an imbalance but busiest->nr_running <= 1, the group is
  4245. * still unbalanced. ld_moved simply stays zero, so it is
  4246. * correctly treated as an imbalance.
  4247. */
  4248. env.flags |= LBF_ALL_PINNED;
  4249. env.src_cpu = busiest->cpu;
  4250. env.src_rq = busiest;
  4251. env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
  4252. update_h_load(env.src_cpu);
  4253. more_balance:
  4254. local_irq_save(flags);
  4255. double_rq_lock(env.dst_rq, busiest);
  4256. /*
  4257. * cur_ld_moved - load moved in current iteration
  4258. * ld_moved - cumulative load moved across iterations
  4259. */
  4260. cur_ld_moved = move_tasks(&env);
  4261. ld_moved += cur_ld_moved;
  4262. double_rq_unlock(env.dst_rq, busiest);
  4263. local_irq_restore(flags);
  4264. if (env.flags & LBF_NEED_BREAK) {
  4265. env.flags &= ~LBF_NEED_BREAK;
  4266. goto more_balance;
  4267. }
  4268. /*
  4269. * some other cpu did the load balance for us.
  4270. */
  4271. if (cur_ld_moved && env.dst_cpu != smp_processor_id())
  4272. resched_cpu(env.dst_cpu);
  4273. /*
  4274. * Revisit (affine) tasks on src_cpu that couldn't be moved to
  4275. * us and move them to an alternate dst_cpu in our sched_group
  4276. * where they can run. The upper limit on how many times we
  4277. * iterate on same src_cpu is dependent on number of cpus in our
  4278. * sched_group.
  4279. *
  4280. * This changes load balance semantics a bit on who can move
  4281. * load to a given_cpu. In addition to the given_cpu itself
  4282. * (or a ilb_cpu acting on its behalf where given_cpu is
  4283. * nohz-idle), we now have balance_cpu in a position to move
  4284. * load to given_cpu. In rare situations, this may cause
  4285. * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
  4286. * _independently_ and at _same_ time to move some load to
  4287. * given_cpu) causing exceess load to be moved to given_cpu.
  4288. * This however should not happen so much in practice and
  4289. * moreover subsequent load balance cycles should correct the
  4290. * excess load moved.
  4291. */
  4292. if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
  4293. lb_iterations++ < max_lb_iterations) {
  4294. env.dst_rq = cpu_rq(env.new_dst_cpu);
  4295. env.dst_cpu = env.new_dst_cpu;
  4296. env.flags &= ~LBF_SOME_PINNED;
  4297. env.loop = 0;
  4298. env.loop_break = sched_nr_migrate_break;
  4299. /*
  4300. * Go back to "more_balance" rather than "redo" since we
  4301. * need to continue with same src_cpu.
  4302. */
  4303. goto more_balance;
  4304. }
  4305. /* All tasks on this runqueue were pinned by CPU affinity */
  4306. if (unlikely(env.flags & LBF_ALL_PINNED)) {
  4307. cpumask_clear_cpu(cpu_of(busiest), cpus);
  4308. if (!cpumask_empty(cpus)) {
  4309. env.loop = 0;
  4310. env.loop_break = sched_nr_migrate_break;
  4311. goto redo;
  4312. }
  4313. goto out_balanced;
  4314. }
  4315. }
  4316. if (!ld_moved) {
  4317. schedstat_inc(sd, lb_failed[idle]);
  4318. /*
  4319. * Increment the failure counter only on periodic balance.
  4320. * We do not want newidle balance, which can be very
  4321. * frequent, pollute the failure counter causing
  4322. * excessive cache_hot migrations and active balances.
  4323. */
  4324. if (idle != CPU_NEWLY_IDLE)
  4325. sd->nr_balance_failed++;
  4326. if (need_active_balance(&env)) {
  4327. raw_spin_lock_irqsave(&busiest->lock, flags);
  4328. /* don't kick the active_load_balance_cpu_stop,
  4329. * if the curr task on busiest cpu can't be
  4330. * moved to this_cpu
  4331. */
  4332. if (!cpumask_test_cpu(this_cpu,
  4333. tsk_cpus_allowed(busiest->curr))) {
  4334. raw_spin_unlock_irqrestore(&busiest->lock,
  4335. flags);
  4336. env.flags |= LBF_ALL_PINNED;
  4337. goto out_one_pinned;
  4338. }
  4339. /*
  4340. * ->active_balance synchronizes accesses to
  4341. * ->active_balance_work. Once set, it's cleared
  4342. * only after active load balance is finished.
  4343. */
  4344. if (!busiest->active_balance) {
  4345. busiest->active_balance = 1;
  4346. busiest->push_cpu = this_cpu;
  4347. active_balance = 1;
  4348. }
  4349. raw_spin_unlock_irqrestore(&busiest->lock, flags);
  4350. if (active_balance) {
  4351. stop_one_cpu_nowait(cpu_of(busiest),
  4352. active_load_balance_cpu_stop, busiest,
  4353. &busiest->active_balance_work);
  4354. }
  4355. /*
  4356. * We've kicked active balancing, reset the failure
  4357. * counter.
  4358. */
  4359. sd->nr_balance_failed = sd->cache_nice_tries+1;
  4360. }
  4361. } else
  4362. sd->nr_balance_failed = 0;
  4363. if (likely(!active_balance)) {
  4364. /* We were unbalanced, so reset the balancing interval */
  4365. sd->balance_interval = sd->min_interval;
  4366. } else {
  4367. /*
  4368. * If we've begun active balancing, start to back off. This
  4369. * case may not be covered by the all_pinned logic if there
  4370. * is only 1 task on the busy runqueue (because we don't call
  4371. * move_tasks).
  4372. */
  4373. if (sd->balance_interval < sd->max_interval)
  4374. sd->balance_interval *= 2;
  4375. }
  4376. goto out;
  4377. out_balanced:
  4378. schedstat_inc(sd, lb_balanced[idle]);
  4379. sd->nr_balance_failed = 0;
  4380. out_one_pinned:
  4381. /* tune up the balancing interval */
  4382. if (((env.flags & LBF_ALL_PINNED) &&
  4383. sd->balance_interval < MAX_PINNED_INTERVAL) ||
  4384. (sd->balance_interval < sd->max_interval))
  4385. sd->balance_interval *= 2;
  4386. ld_moved = 0;
  4387. out:
  4388. return ld_moved;
  4389. }
  4390. /*
  4391. * idle_balance is called by schedule() if this_cpu is about to become
  4392. * idle. Attempts to pull tasks from other CPUs.
  4393. */
  4394. void idle_balance(int this_cpu, struct rq *this_rq)
  4395. {
  4396. struct sched_domain *sd;
  4397. int pulled_task = 0;
  4398. unsigned long next_balance = jiffies + HZ;
  4399. this_rq->idle_stamp = this_rq->clock;
  4400. if (this_rq->avg_idle < sysctl_sched_migration_cost)
  4401. return;
  4402. update_rq_runnable_avg(this_rq, 1);
  4403. /*
  4404. * Drop the rq->lock, but keep IRQ/preempt disabled.
  4405. */
  4406. raw_spin_unlock(&this_rq->lock);
  4407. update_blocked_averages(this_cpu);
  4408. rcu_read_lock();
  4409. for_each_domain(this_cpu, sd) {
  4410. unsigned long interval;
  4411. int balance = 1;
  4412. if (!(sd->flags & SD_LOAD_BALANCE))
  4413. continue;
  4414. if (sd->flags & SD_BALANCE_NEWIDLE) {
  4415. /* If we've pulled tasks over stop searching: */
  4416. pulled_task = load_balance(this_cpu, this_rq,
  4417. sd, CPU_NEWLY_IDLE, &balance);
  4418. }
  4419. interval = msecs_to_jiffies(sd->balance_interval);
  4420. if (time_after(next_balance, sd->last_balance + interval))
  4421. next_balance = sd->last_balance + interval;
  4422. if (pulled_task) {
  4423. this_rq->idle_stamp = 0;
  4424. break;
  4425. }
  4426. }
  4427. rcu_read_unlock();
  4428. raw_spin_lock(&this_rq->lock);
  4429. if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
  4430. /*
  4431. * We are going idle. next_balance may be set based on
  4432. * a busy processor. So reset next_balance.
  4433. */
  4434. this_rq->next_balance = next_balance;
  4435. }
  4436. }
  4437. /*
  4438. * active_load_balance_cpu_stop is run by cpu stopper. It pushes
  4439. * running tasks off the busiest CPU onto idle CPUs. It requires at
  4440. * least 1 task to be running on each physical CPU where possible, and
  4441. * avoids physical / logical imbalances.
  4442. */
  4443. static int active_load_balance_cpu_stop(void *data)
  4444. {
  4445. struct rq *busiest_rq = data;
  4446. int busiest_cpu = cpu_of(busiest_rq);
  4447. int target_cpu = busiest_rq->push_cpu;
  4448. struct rq *target_rq = cpu_rq(target_cpu);
  4449. struct sched_domain *sd;
  4450. raw_spin_lock_irq(&busiest_rq->lock);
  4451. /* make sure the requested cpu hasn't gone down in the meantime */
  4452. if (unlikely(busiest_cpu != smp_processor_id() ||
  4453. !busiest_rq->active_balance))
  4454. goto out_unlock;
  4455. /* Is there any task to move? */
  4456. if (busiest_rq->nr_running <= 1)
  4457. goto out_unlock;
  4458. /*
  4459. * This condition is "impossible", if it occurs
  4460. * we need to fix it. Originally reported by
  4461. * Bjorn Helgaas on a 128-cpu setup.
  4462. */
  4463. BUG_ON(busiest_rq == target_rq);
  4464. /* move a task from busiest_rq to target_rq */
  4465. double_lock_balance(busiest_rq, target_rq);
  4466. /* Search for an sd spanning us and the target CPU. */
  4467. rcu_read_lock();
  4468. for_each_domain(target_cpu, sd) {
  4469. if ((sd->flags & SD_LOAD_BALANCE) &&
  4470. cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
  4471. break;
  4472. }
  4473. if (likely(sd)) {
  4474. struct lb_env env = {
  4475. .sd = sd,
  4476. .dst_cpu = target_cpu,
  4477. .dst_rq = target_rq,
  4478. .src_cpu = busiest_rq->cpu,
  4479. .src_rq = busiest_rq,
  4480. .idle = CPU_IDLE,
  4481. };
  4482. schedstat_inc(sd, alb_count);
  4483. if (move_one_task(&env))
  4484. schedstat_inc(sd, alb_pushed);
  4485. else
  4486. schedstat_inc(sd, alb_failed);
  4487. }
  4488. rcu_read_unlock();
  4489. double_unlock_balance(busiest_rq, target_rq);
  4490. out_unlock:
  4491. busiest_rq->active_balance = 0;
  4492. raw_spin_unlock_irq(&busiest_rq->lock);
  4493. return 0;
  4494. }
  4495. #ifdef CONFIG_NO_HZ
  4496. /*
  4497. * idle load balancing details
  4498. * - When one of the busy CPUs notice that there may be an idle rebalancing
  4499. * needed, they will kick the idle load balancer, which then does idle
  4500. * load balancing for all the idle CPUs.
  4501. */
  4502. static struct {
  4503. cpumask_var_t idle_cpus_mask;
  4504. atomic_t nr_cpus;
  4505. unsigned long next_balance; /* in jiffy units */
  4506. } nohz ____cacheline_aligned;
  4507. static inline int find_new_ilb(int call_cpu)
  4508. {
  4509. int ilb = cpumask_first(nohz.idle_cpus_mask);
  4510. if (ilb < nr_cpu_ids && idle_cpu(ilb))
  4511. return ilb;
  4512. return nr_cpu_ids;
  4513. }
  4514. /*
  4515. * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
  4516. * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
  4517. * CPU (if there is one).
  4518. */
  4519. static void nohz_balancer_kick(int cpu)
  4520. {
  4521. int ilb_cpu;
  4522. nohz.next_balance++;
  4523. ilb_cpu = find_new_ilb(cpu);
  4524. if (ilb_cpu >= nr_cpu_ids)
  4525. return;
  4526. if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
  4527. return;
  4528. /*
  4529. * Use smp_send_reschedule() instead of resched_cpu().
  4530. * This way we generate a sched IPI on the target cpu which
  4531. * is idle. And the softirq performing nohz idle load balance
  4532. * will be run before returning from the IPI.
  4533. */
  4534. smp_send_reschedule(ilb_cpu);
  4535. return;
  4536. }
  4537. static inline void nohz_balance_exit_idle(int cpu)
  4538. {
  4539. if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
  4540. cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
  4541. atomic_dec(&nohz.nr_cpus);
  4542. clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
  4543. }
  4544. }
  4545. static inline void set_cpu_sd_state_busy(void)
  4546. {
  4547. struct sched_domain *sd;
  4548. int cpu = smp_processor_id();
  4549. if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
  4550. return;
  4551. clear_bit(NOHZ_IDLE, nohz_flags(cpu));
  4552. rcu_read_lock();
  4553. for_each_domain(cpu, sd)
  4554. atomic_inc(&sd->groups->sgp->nr_busy_cpus);
  4555. rcu_read_unlock();
  4556. }
  4557. void set_cpu_sd_state_idle(void)
  4558. {
  4559. struct sched_domain *sd;
  4560. int cpu = smp_processor_id();
  4561. if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
  4562. return;
  4563. set_bit(NOHZ_IDLE, nohz_flags(cpu));
  4564. rcu_read_lock();
  4565. for_each_domain(cpu, sd)
  4566. atomic_dec(&sd->groups->sgp->nr_busy_cpus);
  4567. rcu_read_unlock();
  4568. }
  4569. /*
  4570. * This routine will record that the cpu is going idle with tick stopped.
  4571. * This info will be used in performing idle load balancing in the future.
  4572. */
  4573. void nohz_balance_enter_idle(int cpu)
  4574. {
  4575. /*
  4576. * If this cpu is going down, then nothing needs to be done.
  4577. */
  4578. if (!cpu_active(cpu))
  4579. return;
  4580. if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
  4581. return;
  4582. cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
  4583. atomic_inc(&nohz.nr_cpus);
  4584. set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
  4585. }
  4586. static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
  4587. unsigned long action, void *hcpu)
  4588. {
  4589. switch (action & ~CPU_TASKS_FROZEN) {
  4590. case CPU_DYING:
  4591. nohz_balance_exit_idle(smp_processor_id());
  4592. return NOTIFY_OK;
  4593. default:
  4594. return NOTIFY_DONE;
  4595. }
  4596. }
  4597. #endif
  4598. static DEFINE_SPINLOCK(balancing);
  4599. /*
  4600. * Scale the max load_balance interval with the number of CPUs in the system.
  4601. * This trades load-balance latency on larger machines for less cross talk.
  4602. */
  4603. void update_max_interval(void)
  4604. {
  4605. max_load_balance_interval = HZ*num_online_cpus()/10;
  4606. }
  4607. /*
  4608. * It checks each scheduling domain to see if it is due to be balanced,
  4609. * and initiates a balancing operation if so.
  4610. *
  4611. * Balancing parameters are set up in arch_init_sched_domains.
  4612. */
  4613. static void rebalance_domains(int cpu, enum cpu_idle_type idle)
  4614. {
  4615. int balance = 1;
  4616. struct rq *rq = cpu_rq(cpu);
  4617. unsigned long interval;
  4618. struct sched_domain *sd;
  4619. /* Earliest time when we have to do rebalance again */
  4620. unsigned long next_balance = jiffies + 60*HZ;
  4621. int update_next_balance = 0;
  4622. int need_serialize;
  4623. update_blocked_averages(cpu);
  4624. rcu_read_lock();
  4625. for_each_domain(cpu, sd) {
  4626. if (!(sd->flags & SD_LOAD_BALANCE))
  4627. continue;
  4628. interval = sd->balance_interval;
  4629. if (idle != CPU_IDLE)
  4630. interval *= sd->busy_factor;
  4631. /* scale ms to jiffies */
  4632. interval = msecs_to_jiffies(interval);
  4633. interval = clamp(interval, 1UL, max_load_balance_interval);
  4634. need_serialize = sd->flags & SD_SERIALIZE;
  4635. if (need_serialize) {
  4636. if (!spin_trylock(&balancing))
  4637. goto out;
  4638. }
  4639. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  4640. if (load_balance(cpu, rq, sd, idle, &balance)) {
  4641. /*
  4642. * We've pulled tasks over so either we're no
  4643. * longer idle.
  4644. */
  4645. idle = CPU_NOT_IDLE;
  4646. }
  4647. sd->last_balance = jiffies;
  4648. }
  4649. if (need_serialize)
  4650. spin_unlock(&balancing);
  4651. out:
  4652. if (time_after(next_balance, sd->last_balance + interval)) {
  4653. next_balance = sd->last_balance + interval;
  4654. update_next_balance = 1;
  4655. }
  4656. /*
  4657. * Stop the load balance at this level. There is another
  4658. * CPU in our sched group which is doing load balancing more
  4659. * actively.
  4660. */
  4661. if (!balance)
  4662. break;
  4663. }
  4664. rcu_read_unlock();
  4665. /*
  4666. * next_balance will be updated only when there is a need.
  4667. * When the cpu is attached to null domain for ex, it will not be
  4668. * updated.
  4669. */
  4670. if (likely(update_next_balance))
  4671. rq->next_balance = next_balance;
  4672. }
  4673. #ifdef CONFIG_NO_HZ
  4674. /*
  4675. * In CONFIG_NO_HZ case, the idle balance kickee will do the
  4676. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  4677. */
  4678. static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
  4679. {
  4680. struct rq *this_rq = cpu_rq(this_cpu);
  4681. struct rq *rq;
  4682. int balance_cpu;
  4683. if (idle != CPU_IDLE ||
  4684. !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
  4685. goto end;
  4686. for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
  4687. if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
  4688. continue;
  4689. /*
  4690. * If this cpu gets work to do, stop the load balancing
  4691. * work being done for other cpus. Next load
  4692. * balancing owner will pick it up.
  4693. */
  4694. if (need_resched())
  4695. break;
  4696. rq = cpu_rq(balance_cpu);
  4697. raw_spin_lock_irq(&rq->lock);
  4698. update_rq_clock(rq);
  4699. update_idle_cpu_load(rq);
  4700. raw_spin_unlock_irq(&rq->lock);
  4701. rebalance_domains(balance_cpu, CPU_IDLE);
  4702. if (time_after(this_rq->next_balance, rq->next_balance))
  4703. this_rq->next_balance = rq->next_balance;
  4704. }
  4705. nohz.next_balance = this_rq->next_balance;
  4706. end:
  4707. clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
  4708. }
  4709. /*
  4710. * Current heuristic for kicking the idle load balancer in the presence
  4711. * of an idle cpu is the system.
  4712. * - This rq has more than one task.
  4713. * - At any scheduler domain level, this cpu's scheduler group has multiple
  4714. * busy cpu's exceeding the group's power.
  4715. * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
  4716. * domain span are idle.
  4717. */
  4718. static inline int nohz_kick_needed(struct rq *rq, int cpu)
  4719. {
  4720. unsigned long now = jiffies;
  4721. struct sched_domain *sd;
  4722. if (unlikely(idle_cpu(cpu)))
  4723. return 0;
  4724. /*
  4725. * We may be recently in ticked or tickless idle mode. At the first
  4726. * busy tick after returning from idle, we will update the busy stats.
  4727. */
  4728. set_cpu_sd_state_busy();
  4729. nohz_balance_exit_idle(cpu);
  4730. /*
  4731. * None are in tickless mode and hence no need for NOHZ idle load
  4732. * balancing.
  4733. */
  4734. if (likely(!atomic_read(&nohz.nr_cpus)))
  4735. return 0;
  4736. if (time_before(now, nohz.next_balance))
  4737. return 0;
  4738. if (rq->nr_running >= 2)
  4739. goto need_kick;
  4740. rcu_read_lock();
  4741. for_each_domain(cpu, sd) {
  4742. struct sched_group *sg = sd->groups;
  4743. struct sched_group_power *sgp = sg->sgp;
  4744. int nr_busy = atomic_read(&sgp->nr_busy_cpus);
  4745. if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
  4746. goto need_kick_unlock;
  4747. if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
  4748. && (cpumask_first_and(nohz.idle_cpus_mask,
  4749. sched_domain_span(sd)) < cpu))
  4750. goto need_kick_unlock;
  4751. if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
  4752. break;
  4753. }
  4754. rcu_read_unlock();
  4755. return 0;
  4756. need_kick_unlock:
  4757. rcu_read_unlock();
  4758. need_kick:
  4759. return 1;
  4760. }
  4761. #else
  4762. static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
  4763. #endif
  4764. /*
  4765. * run_rebalance_domains is triggered when needed from the scheduler tick.
  4766. * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
  4767. */
  4768. static void run_rebalance_domains(struct softirq_action *h)
  4769. {
  4770. int this_cpu = smp_processor_id();
  4771. struct rq *this_rq = cpu_rq(this_cpu);
  4772. enum cpu_idle_type idle = this_rq->idle_balance ?
  4773. CPU_IDLE : CPU_NOT_IDLE;
  4774. rebalance_domains(this_cpu, idle);
  4775. /*
  4776. * If this cpu has a pending nohz_balance_kick, then do the
  4777. * balancing on behalf of the other idle cpus whose ticks are
  4778. * stopped.
  4779. */
  4780. nohz_idle_balance(this_cpu, idle);
  4781. }
  4782. static inline int on_null_domain(int cpu)
  4783. {
  4784. return !rcu_dereference_sched(cpu_rq(cpu)->sd);
  4785. }
  4786. /*
  4787. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  4788. */
  4789. void trigger_load_balance(struct rq *rq, int cpu)
  4790. {
  4791. /* Don't need to rebalance while attached to NULL domain */
  4792. if (time_after_eq(jiffies, rq->next_balance) &&
  4793. likely(!on_null_domain(cpu)))
  4794. raise_softirq(SCHED_SOFTIRQ);
  4795. #ifdef CONFIG_NO_HZ
  4796. if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
  4797. nohz_balancer_kick(cpu);
  4798. #endif
  4799. }
  4800. static void rq_online_fair(struct rq *rq)
  4801. {
  4802. update_sysctl();
  4803. }
  4804. static void rq_offline_fair(struct rq *rq)
  4805. {
  4806. update_sysctl();
  4807. /* Ensure any throttled groups are reachable by pick_next_task */
  4808. unthrottle_offline_cfs_rqs(rq);
  4809. }
  4810. #endif /* CONFIG_SMP */
  4811. /*
  4812. * scheduler tick hitting a task of our scheduling class:
  4813. */
  4814. static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  4815. {
  4816. struct cfs_rq *cfs_rq;
  4817. struct sched_entity *se = &curr->se;
  4818. for_each_sched_entity(se) {
  4819. cfs_rq = cfs_rq_of(se);
  4820. entity_tick(cfs_rq, se, queued);
  4821. }
  4822. if (sched_feat_numa(NUMA))
  4823. task_tick_numa(rq, curr);
  4824. update_rq_runnable_avg(rq, 1);
  4825. }
  4826. /*
  4827. * called on fork with the child task as argument from the parent's context
  4828. * - child not yet on the tasklist
  4829. * - preemption disabled
  4830. */
  4831. static void task_fork_fair(struct task_struct *p)
  4832. {
  4833. struct cfs_rq *cfs_rq;
  4834. struct sched_entity *se = &p->se, *curr;
  4835. int this_cpu = smp_processor_id();
  4836. struct rq *rq = this_rq();
  4837. unsigned long flags;
  4838. raw_spin_lock_irqsave(&rq->lock, flags);
  4839. update_rq_clock(rq);
  4840. cfs_rq = task_cfs_rq(current);
  4841. curr = cfs_rq->curr;
  4842. if (unlikely(task_cpu(p) != this_cpu)) {
  4843. rcu_read_lock();
  4844. __set_task_cpu(p, this_cpu);
  4845. rcu_read_unlock();
  4846. }
  4847. update_curr(cfs_rq);
  4848. if (curr)
  4849. se->vruntime = curr->vruntime;
  4850. place_entity(cfs_rq, se, 1);
  4851. if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
  4852. /*
  4853. * Upon rescheduling, sched_class::put_prev_task() will place
  4854. * 'current' within the tree based on its new key value.
  4855. */
  4856. swap(curr->vruntime, se->vruntime);
  4857. resched_task(rq->curr);
  4858. }
  4859. se->vruntime -= cfs_rq->min_vruntime;
  4860. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4861. }
  4862. /*
  4863. * Priority of the task has changed. Check to see if we preempt
  4864. * the current task.
  4865. */
  4866. static void
  4867. prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
  4868. {
  4869. if (!p->se.on_rq)
  4870. return;
  4871. /*
  4872. * Reschedule if we are currently running on this runqueue and
  4873. * our priority decreased, or if we are not currently running on
  4874. * this runqueue and our priority is higher than the current's
  4875. */
  4876. if (rq->curr == p) {
  4877. if (p->prio > oldprio)
  4878. resched_task(rq->curr);
  4879. } else
  4880. check_preempt_curr(rq, p, 0);
  4881. }
  4882. static void switched_from_fair(struct rq *rq, struct task_struct *p)
  4883. {
  4884. struct sched_entity *se = &p->se;
  4885. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  4886. /*
  4887. * Ensure the task's vruntime is normalized, so that when its
  4888. * switched back to the fair class the enqueue_entity(.flags=0) will
  4889. * do the right thing.
  4890. *
  4891. * If it was on_rq, then the dequeue_entity(.flags=0) will already
  4892. * have normalized the vruntime, if it was !on_rq, then only when
  4893. * the task is sleeping will it still have non-normalized vruntime.
  4894. */
  4895. if (!se->on_rq && p->state != TASK_RUNNING) {
  4896. /*
  4897. * Fix up our vruntime so that the current sleep doesn't
  4898. * cause 'unlimited' sleep bonus.
  4899. */
  4900. place_entity(cfs_rq, se, 0);
  4901. se->vruntime -= cfs_rq->min_vruntime;
  4902. }
  4903. #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
  4904. /*
  4905. * Remove our load from contribution when we leave sched_fair
  4906. * and ensure we don't carry in an old decay_count if we
  4907. * switch back.
  4908. */
  4909. if (p->se.avg.decay_count) {
  4910. struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
  4911. __synchronize_entity_decay(&p->se);
  4912. subtract_blocked_load_contrib(cfs_rq,
  4913. p->se.avg.load_avg_contrib);
  4914. }
  4915. #endif
  4916. }
  4917. /*
  4918. * We switched to the sched_fair class.
  4919. */
  4920. static void switched_to_fair(struct rq *rq, struct task_struct *p)
  4921. {
  4922. if (!p->se.on_rq)
  4923. return;
  4924. /*
  4925. * We were most likely switched from sched_rt, so
  4926. * kick off the schedule if running, otherwise just see
  4927. * if we can still preempt the current task.
  4928. */
  4929. if (rq->curr == p)
  4930. resched_task(rq->curr);
  4931. else
  4932. check_preempt_curr(rq, p, 0);
  4933. }
  4934. /* Account for a task changing its policy or group.
  4935. *
  4936. * This routine is mostly called to set cfs_rq->curr field when a task
  4937. * migrates between groups/classes.
  4938. */
  4939. static void set_curr_task_fair(struct rq *rq)
  4940. {
  4941. struct sched_entity *se = &rq->curr->se;
  4942. for_each_sched_entity(se) {
  4943. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  4944. set_next_entity(cfs_rq, se);
  4945. /* ensure bandwidth has been allocated on our new cfs_rq */
  4946. account_cfs_rq_runtime(cfs_rq, 0);
  4947. }
  4948. }
  4949. void init_cfs_rq(struct cfs_rq *cfs_rq)
  4950. {
  4951. cfs_rq->tasks_timeline = RB_ROOT;
  4952. cfs_rq->min_vruntime = (u64)(-(1LL << 20));
  4953. #ifndef CONFIG_64BIT
  4954. cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
  4955. #endif
  4956. #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
  4957. atomic64_set(&cfs_rq->decay_counter, 1);
  4958. atomic64_set(&cfs_rq->removed_load, 0);
  4959. #endif
  4960. }
  4961. #ifdef CONFIG_FAIR_GROUP_SCHED
  4962. static void task_move_group_fair(struct task_struct *p, int on_rq)
  4963. {
  4964. struct cfs_rq *cfs_rq;
  4965. /*
  4966. * If the task was not on the rq at the time of this cgroup movement
  4967. * it must have been asleep, sleeping tasks keep their ->vruntime
  4968. * absolute on their old rq until wakeup (needed for the fair sleeper
  4969. * bonus in place_entity()).
  4970. *
  4971. * If it was on the rq, we've just 'preempted' it, which does convert
  4972. * ->vruntime to a relative base.
  4973. *
  4974. * Make sure both cases convert their relative position when migrating
  4975. * to another cgroup's rq. This does somewhat interfere with the
  4976. * fair sleeper stuff for the first placement, but who cares.
  4977. */
  4978. /*
  4979. * When !on_rq, vruntime of the task has usually NOT been normalized.
  4980. * But there are some cases where it has already been normalized:
  4981. *
  4982. * - Moving a forked child which is waiting for being woken up by
  4983. * wake_up_new_task().
  4984. * - Moving a task which has been woken up by try_to_wake_up() and
  4985. * waiting for actually being woken up by sched_ttwu_pending().
  4986. *
  4987. * To prevent boost or penalty in the new cfs_rq caused by delta
  4988. * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
  4989. */
  4990. if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
  4991. on_rq = 1;
  4992. if (!on_rq)
  4993. p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
  4994. set_task_rq(p, task_cpu(p));
  4995. if (!on_rq) {
  4996. cfs_rq = cfs_rq_of(&p->se);
  4997. p->se.vruntime += cfs_rq->min_vruntime;
  4998. #ifdef CONFIG_SMP
  4999. /*
  5000. * migrate_task_rq_fair() will have removed our previous
  5001. * contribution, but we must synchronize for ongoing future
  5002. * decay.
  5003. */
  5004. p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
  5005. cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib;
  5006. #endif
  5007. }
  5008. }
  5009. void free_fair_sched_group(struct task_group *tg)
  5010. {
  5011. int i;
  5012. destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
  5013. for_each_possible_cpu(i) {
  5014. if (tg->cfs_rq)
  5015. kfree(tg->cfs_rq[i]);
  5016. if (tg->se)
  5017. kfree(tg->se[i]);
  5018. }
  5019. kfree(tg->cfs_rq);
  5020. kfree(tg->se);
  5021. }
  5022. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  5023. {
  5024. struct cfs_rq *cfs_rq;
  5025. struct sched_entity *se;
  5026. int i;
  5027. tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
  5028. if (!tg->cfs_rq)
  5029. goto err;
  5030. tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
  5031. if (!tg->se)
  5032. goto err;
  5033. tg->shares = NICE_0_LOAD;
  5034. init_cfs_bandwidth(tg_cfs_bandwidth(tg));
  5035. for_each_possible_cpu(i) {
  5036. cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
  5037. GFP_KERNEL, cpu_to_node(i));
  5038. if (!cfs_rq)
  5039. goto err;
  5040. se = kzalloc_node(sizeof(struct sched_entity),
  5041. GFP_KERNEL, cpu_to_node(i));
  5042. if (!se)
  5043. goto err_free_rq;
  5044. init_cfs_rq(cfs_rq);
  5045. init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
  5046. }
  5047. return 1;
  5048. err_free_rq:
  5049. kfree(cfs_rq);
  5050. err:
  5051. return 0;
  5052. }
  5053. void unregister_fair_sched_group(struct task_group *tg, int cpu)
  5054. {
  5055. struct rq *rq = cpu_rq(cpu);
  5056. unsigned long flags;
  5057. /*
  5058. * Only empty task groups can be destroyed; so we can speculatively
  5059. * check on_list without danger of it being re-added.
  5060. */
  5061. if (!tg->cfs_rq[cpu]->on_list)
  5062. return;
  5063. raw_spin_lock_irqsave(&rq->lock, flags);
  5064. list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
  5065. raw_spin_unlock_irqrestore(&rq->lock, flags);
  5066. }
  5067. void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  5068. struct sched_entity *se, int cpu,
  5069. struct sched_entity *parent)
  5070. {
  5071. struct rq *rq = cpu_rq(cpu);
  5072. cfs_rq->tg = tg;
  5073. cfs_rq->rq = rq;
  5074. init_cfs_rq_runtime(cfs_rq);
  5075. tg->cfs_rq[cpu] = cfs_rq;
  5076. tg->se[cpu] = se;
  5077. /* se could be NULL for root_task_group */
  5078. if (!se)
  5079. return;
  5080. if (!parent)
  5081. se->cfs_rq = &rq->cfs;
  5082. else
  5083. se->cfs_rq = parent->my_q;
  5084. se->my_q = cfs_rq;
  5085. update_load_set(&se->load, 0);
  5086. se->parent = parent;
  5087. }
  5088. static DEFINE_MUTEX(shares_mutex);
  5089. int sched_group_set_shares(struct task_group *tg, unsigned long shares)
  5090. {
  5091. int i;
  5092. unsigned long flags;
  5093. /*
  5094. * We can't change the weight of the root cgroup.
  5095. */
  5096. if (!tg->se[0])
  5097. return -EINVAL;
  5098. shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
  5099. mutex_lock(&shares_mutex);
  5100. if (tg->shares == shares)
  5101. goto done;
  5102. tg->shares = shares;
  5103. for_each_possible_cpu(i) {
  5104. struct rq *rq = cpu_rq(i);
  5105. struct sched_entity *se;
  5106. se = tg->se[i];
  5107. /* Propagate contribution to hierarchy */
  5108. raw_spin_lock_irqsave(&rq->lock, flags);
  5109. for_each_sched_entity(se)
  5110. update_cfs_shares(group_cfs_rq(se));
  5111. raw_spin_unlock_irqrestore(&rq->lock, flags);
  5112. }
  5113. done:
  5114. mutex_unlock(&shares_mutex);
  5115. return 0;
  5116. }
  5117. #else /* CONFIG_FAIR_GROUP_SCHED */
  5118. void free_fair_sched_group(struct task_group *tg) { }
  5119. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  5120. {
  5121. return 1;
  5122. }
  5123. void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
  5124. #endif /* CONFIG_FAIR_GROUP_SCHED */
  5125. static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
  5126. {
  5127. struct sched_entity *se = &task->se;
  5128. unsigned int rr_interval = 0;
  5129. /*
  5130. * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
  5131. * idle runqueue:
  5132. */
  5133. if (rq->cfs.load.weight)
  5134. rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
  5135. return rr_interval;
  5136. }
  5137. /*
  5138. * All the scheduling class methods:
  5139. */
  5140. const struct sched_class fair_sched_class = {
  5141. .next = &idle_sched_class,
  5142. .enqueue_task = enqueue_task_fair,
  5143. .dequeue_task = dequeue_task_fair,
  5144. .yield_task = yield_task_fair,
  5145. .yield_to_task = yield_to_task_fair,
  5146. .check_preempt_curr = check_preempt_wakeup,
  5147. .pick_next_task = pick_next_task_fair,
  5148. .put_prev_task = put_prev_task_fair,
  5149. #ifdef CONFIG_SMP
  5150. .select_task_rq = select_task_rq_fair,
  5151. #ifdef CONFIG_FAIR_GROUP_SCHED
  5152. .migrate_task_rq = migrate_task_rq_fair,
  5153. #endif
  5154. .rq_online = rq_online_fair,
  5155. .rq_offline = rq_offline_fair,
  5156. .task_waking = task_waking_fair,
  5157. #endif
  5158. .set_curr_task = set_curr_task_fair,
  5159. .task_tick = task_tick_fair,
  5160. .task_fork = task_fork_fair,
  5161. .prio_changed = prio_changed_fair,
  5162. .switched_from = switched_from_fair,
  5163. .switched_to = switched_to_fair,
  5164. .get_rr_interval = get_rr_interval_fair,
  5165. #ifdef CONFIG_FAIR_GROUP_SCHED
  5166. .task_move_group = task_move_group_fair,
  5167. #endif
  5168. };
  5169. #ifdef CONFIG_SCHED_DEBUG
  5170. void print_cfs_stats(struct seq_file *m, int cpu)
  5171. {
  5172. struct cfs_rq *cfs_rq;
  5173. rcu_read_lock();
  5174. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  5175. print_cfs_rq(m, cpu, cfs_rq);
  5176. rcu_read_unlock();
  5177. }
  5178. #endif
  5179. __init void init_sched_fair_class(void)
  5180. {
  5181. #ifdef CONFIG_SMP
  5182. open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
  5183. #ifdef CONFIG_NO_HZ
  5184. nohz.next_balance = jiffies;
  5185. zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
  5186. cpu_notifier(sched_ilb_notifier, 0);
  5187. #endif
  5188. #endif /* SMP */
  5189. }