fair.c 147 KB

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