fair.c 159 KB

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